gdb: add all_bp_locations_at_addr function
[binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2021 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 "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
71 /* readline include files */
72 #include "readline/tilde.h"
73
74 /* readline defines this. */
75 #undef savestring
76
77 #include "mi/mi-common.h"
78 #include "extension.h"
79 #include <algorithm>
80 #include "progspace-and-thread.h"
81 #include "gdbsupport/array-view.h"
82 #include "gdbsupport/gdb_optional.h"
83
84 /* Prototypes for local functions. */
85
86 static void map_breakpoint_numbers (const char *,
87 gdb::function_view<void (breakpoint *)>);
88
89 static void breakpoint_re_set_default (struct breakpoint *);
90
91 static void
92 create_sals_from_location_default (struct event_location *location,
93 struct linespec_result *canonical,
94 enum bptype type_wanted);
95
96 static void create_breakpoints_sal_default (struct gdbarch *,
97 struct linespec_result *,
98 gdb::unique_xmalloc_ptr<char>,
99 gdb::unique_xmalloc_ptr<char>,
100 enum bptype,
101 enum bpdisp, int, int,
102 int,
103 const struct breakpoint_ops *,
104 int, int, int, unsigned);
105
106 static std::vector<symtab_and_line> decode_location_default
107 (struct breakpoint *b, struct event_location *location,
108 struct program_space *search_pspace);
109
110 static int can_use_hardware_watchpoint
111 (const std::vector<value_ref_ptr> &vals);
112
113 static void mention (struct breakpoint *);
114
115 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
116 enum bptype,
117 const struct breakpoint_ops *);
118 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
119 const struct symtab_and_line *);
120
121 /* This function is used in gdbtk sources and thus can not be made
122 static. */
123 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
124 struct symtab_and_line,
125 enum bptype,
126 const struct breakpoint_ops *);
127
128 static struct breakpoint *
129 momentary_breakpoint_from_master (struct breakpoint *orig,
130 enum bptype type,
131 const struct breakpoint_ops *ops,
132 int loc_enabled);
133
134 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
135
136 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
137 CORE_ADDR bpaddr,
138 enum bptype bptype);
139
140 static void describe_other_breakpoints (struct gdbarch *,
141 struct program_space *, CORE_ADDR,
142 struct obj_section *, int);
143
144 static int watchpoint_locations_match (struct bp_location *loc1,
145 struct bp_location *loc2);
146
147 static int breakpoint_locations_match (struct bp_location *loc1,
148 struct bp_location *loc2,
149 bool sw_hw_bps_match = false);
150
151 static int breakpoint_location_address_match (struct bp_location *bl,
152 const struct address_space *aspace,
153 CORE_ADDR addr);
154
155 static int breakpoint_location_address_range_overlap (struct bp_location *,
156 const address_space *,
157 CORE_ADDR, int);
158
159 static int remove_breakpoint (struct bp_location *);
160 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
161
162 static enum print_stop_action print_bp_stop_message (bpstat bs);
163
164 static int hw_breakpoint_used_count (void);
165
166 static int hw_watchpoint_use_count (struct breakpoint *);
167
168 static int hw_watchpoint_used_count_others (struct breakpoint *except,
169 enum bptype type,
170 int *other_type_used);
171
172 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
173 int count);
174
175 static void decref_bp_location (struct bp_location **loc);
176
177 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
178
179 /* update_global_location_list's modes of operation wrt to whether to
180 insert locations now. */
181 enum ugll_insert_mode
182 {
183 /* Don't insert any breakpoint locations into the inferior, only
184 remove already-inserted locations that no longer should be
185 inserted. Functions that delete a breakpoint or breakpoints
186 should specify this mode, so that deleting a breakpoint doesn't
187 have the side effect of inserting the locations of other
188 breakpoints that are marked not-inserted, but should_be_inserted
189 returns true on them.
190
191 This behavior is useful is situations close to tear-down -- e.g.,
192 after an exec, while the target still has execution, but
193 breakpoint shadows of the previous executable image should *NOT*
194 be restored to the new image; or before detaching, where the
195 target still has execution and wants to delete breakpoints from
196 GDB's lists, and all breakpoints had already been removed from
197 the inferior. */
198 UGLL_DONT_INSERT,
199
200 /* May insert breakpoints iff breakpoints_should_be_inserted_now
201 claims breakpoints should be inserted now. */
202 UGLL_MAY_INSERT,
203
204 /* Insert locations now, irrespective of
205 breakpoints_should_be_inserted_now. E.g., say all threads are
206 stopped right now, and the user did "continue". We need to
207 insert breakpoints _before_ resuming the target, but
208 UGLL_MAY_INSERT wouldn't insert them, because
209 breakpoints_should_be_inserted_now returns false at that point,
210 as no thread is running yet. */
211 UGLL_INSERT
212 };
213
214 static void update_global_location_list (enum ugll_insert_mode);
215
216 static void update_global_location_list_nothrow (enum ugll_insert_mode);
217
218 static void insert_breakpoint_locations (void);
219
220 static void trace_pass_command (const char *, int);
221
222 static void set_tracepoint_count (int num);
223
224 static bool is_masked_watchpoint (const struct breakpoint *b);
225
226 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
227 otherwise. */
228
229 static int strace_marker_p (struct breakpoint *b);
230
231 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
232 that are implemented on top of software or hardware breakpoints
233 (user breakpoints, internal and momentary breakpoints, etc.). */
234 static struct breakpoint_ops bkpt_base_breakpoint_ops;
235
236 /* Internal breakpoints class type. */
237 static struct breakpoint_ops internal_breakpoint_ops;
238
239 /* Momentary breakpoints class type. */
240 static struct breakpoint_ops momentary_breakpoint_ops;
241
242 /* The breakpoint_ops structure to be used in regular user created
243 breakpoints. */
244 struct breakpoint_ops bkpt_breakpoint_ops;
245
246 /* Breakpoints set on probes. */
247 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
248
249 /* Tracepoints set on probes. */
250 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
251
252 /* Dynamic printf class type. */
253 struct breakpoint_ops dprintf_breakpoint_ops;
254
255 /* The style in which to perform a dynamic printf. This is a user
256 option because different output options have different tradeoffs;
257 if GDB does the printing, there is better error handling if there
258 is a problem with any of the arguments, but using an inferior
259 function lets you have special-purpose printers and sending of
260 output to the same place as compiled-in print functions. */
261
262 static const char dprintf_style_gdb[] = "gdb";
263 static const char dprintf_style_call[] = "call";
264 static const char dprintf_style_agent[] = "agent";
265 static const char *const dprintf_style_enums[] = {
266 dprintf_style_gdb,
267 dprintf_style_call,
268 dprintf_style_agent,
269 NULL
270 };
271 static const char *dprintf_style = dprintf_style_gdb;
272
273 /* The function to use for dynamic printf if the preferred style is to
274 call into the inferior. The value is simply a string that is
275 copied into the command, so it can be anything that GDB can
276 evaluate to a callable address, not necessarily a function name. */
277
278 static char *dprintf_function;
279
280 /* The channel to use for dynamic printf if the preferred style is to
281 call into the inferior; if a nonempty string, it will be passed to
282 the call as the first argument, with the format string as the
283 second. As with the dprintf function, this can be anything that
284 GDB knows how to evaluate, so in addition to common choices like
285 "stderr", this could be an app-specific expression like
286 "mystreams[curlogger]". */
287
288 static char *dprintf_channel;
289
290 /* True if dprintf commands should continue to operate even if GDB
291 has disconnected. */
292 static bool disconnected_dprintf = true;
293
294 struct command_line *
295 breakpoint_commands (struct breakpoint *b)
296 {
297 return b->commands ? b->commands.get () : NULL;
298 }
299
300 /* Flag indicating that a command has proceeded the inferior past the
301 current breakpoint. */
302
303 static bool breakpoint_proceeded;
304
305 const char *
306 bpdisp_text (enum bpdisp disp)
307 {
308 /* NOTE: the following values are a part of MI protocol and
309 represent values of 'disp' field returned when inferior stops at
310 a breakpoint. */
311 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
312
313 return bpdisps[(int) disp];
314 }
315
316 /* Prototypes for exported functions. */
317 /* If FALSE, gdb will not use hardware support for watchpoints, even
318 if such is available. */
319 static int can_use_hw_watchpoints;
320
321 static void
322 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
323 struct cmd_list_element *c,
324 const char *value)
325 {
326 fprintf_filtered (file,
327 _("Debugger's willingness to use "
328 "watchpoint hardware is %s.\n"),
329 value);
330 }
331
332 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
333 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
334 for unrecognized breakpoint locations.
335 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
336 static enum auto_boolean pending_break_support;
337 static void
338 show_pending_break_support (struct ui_file *file, int from_tty,
339 struct cmd_list_element *c,
340 const char *value)
341 {
342 fprintf_filtered (file,
343 _("Debugger's behavior regarding "
344 "pending breakpoints is %s.\n"),
345 value);
346 }
347
348 /* If true, gdb will automatically use hardware breakpoints for breakpoints
349 set with "break" but falling in read-only memory.
350 If false, gdb will warn about such breakpoints, but won't automatically
351 use hardware breakpoints. */
352 static bool automatic_hardware_breakpoints;
353 static void
354 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
355 struct cmd_list_element *c,
356 const char *value)
357 {
358 fprintf_filtered (file,
359 _("Automatic usage of hardware breakpoints is %s.\n"),
360 value);
361 }
362
363 /* If on, GDB keeps breakpoints inserted even if the inferior is
364 stopped, and immediately inserts any new breakpoints as soon as
365 they're created. If off (default), GDB keeps breakpoints off of
366 the target as long as possible. That is, it delays inserting
367 breakpoints until the next resume, and removes them again when the
368 target fully stops. This is a bit safer in case GDB crashes while
369 processing user input. */
370 static bool always_inserted_mode = false;
371
372 static void
373 show_always_inserted_mode (struct ui_file *file, int from_tty,
374 struct cmd_list_element *c, const char *value)
375 {
376 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
377 value);
378 }
379
380 /* See breakpoint.h. */
381
382 int
383 breakpoints_should_be_inserted_now (void)
384 {
385 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
386 {
387 /* If breakpoints are global, they should be inserted even if no
388 thread under gdb's control is running, or even if there are
389 no threads under GDB's control yet. */
390 return 1;
391 }
392 else
393 {
394 if (always_inserted_mode)
395 {
396 /* The user wants breakpoints inserted even if all threads
397 are stopped. */
398 return 1;
399 }
400
401 for (inferior *inf : all_inferiors ())
402 if (inf->has_execution ()
403 && threads_are_executing (inf->process_target ()))
404 return 1;
405
406 /* Don't remove breakpoints yet if, even though all threads are
407 stopped, we still have events to process. */
408 for (thread_info *tp : all_non_exited_threads ())
409 if (tp->resumed
410 && tp->suspend.waitstatus_pending_p)
411 return 1;
412 }
413 return 0;
414 }
415
416 static const char condition_evaluation_both[] = "host or target";
417
418 /* Modes for breakpoint condition evaluation. */
419 static const char condition_evaluation_auto[] = "auto";
420 static const char condition_evaluation_host[] = "host";
421 static const char condition_evaluation_target[] = "target";
422 static const char *const condition_evaluation_enums[] = {
423 condition_evaluation_auto,
424 condition_evaluation_host,
425 condition_evaluation_target,
426 NULL
427 };
428
429 /* Global that holds the current mode for breakpoint condition evaluation. */
430 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
431
432 /* Global that we use to display information to the user (gets its value from
433 condition_evaluation_mode_1. */
434 static const char *condition_evaluation_mode = condition_evaluation_auto;
435
436 /* Translate a condition evaluation mode MODE into either "host"
437 or "target". This is used mostly to translate from "auto" to the
438 real setting that is being used. It returns the translated
439 evaluation mode. */
440
441 static const char *
442 translate_condition_evaluation_mode (const char *mode)
443 {
444 if (mode == condition_evaluation_auto)
445 {
446 if (target_supports_evaluation_of_breakpoint_conditions ())
447 return condition_evaluation_target;
448 else
449 return condition_evaluation_host;
450 }
451 else
452 return mode;
453 }
454
455 /* Discovers what condition_evaluation_auto translates to. */
456
457 static const char *
458 breakpoint_condition_evaluation_mode (void)
459 {
460 return translate_condition_evaluation_mode (condition_evaluation_mode);
461 }
462
463 /* Return true if GDB should evaluate breakpoint conditions or false
464 otherwise. */
465
466 static int
467 gdb_evaluates_breakpoint_condition_p (void)
468 {
469 const char *mode = breakpoint_condition_evaluation_mode ();
470
471 return (mode == condition_evaluation_host);
472 }
473
474 /* Are we executing breakpoint commands? */
475 static int executing_breakpoint_commands;
476
477 /* Are overlay event breakpoints enabled? */
478 static int overlay_events_enabled;
479
480 /* See description in breakpoint.h. */
481 bool target_exact_watchpoints = false;
482
483 /* Walk the following statement or block through all breakpoints.
484 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
485 current breakpoint. */
486
487 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
488 for (B = breakpoint_chain; \
489 B ? (TMP=B->next, 1): 0; \
490 B = TMP)
491
492 /* Chains of all breakpoints defined. */
493
494 static struct breakpoint *breakpoint_chain;
495
496 /* Breakpoint linked list range. */
497
498 using breakpoint_range = next_adapter<breakpoint, breakpoint_iterator>;
499
500 /* Return a range to iterate over all breakpoints. */
501
502 static breakpoint_range
503 all_breakpoints ()
504 {
505 return breakpoint_range (breakpoint_chain);
506 }
507
508 /* Breakpoint linked list range, safe against deletion of the current
509 breakpoint while iterating. */
510
511 using breakpoint_safe_range = basic_safe_range<breakpoint_range>;
512
513 /* Return a range to iterate over all breakpoints. This range is safe against
514 deletion of the current breakpoint while iterating. */
515
516 static breakpoint_safe_range
517 all_breakpoints_safe ()
518 {
519 return breakpoint_safe_range (all_breakpoints ());
520 }
521
522 /* See breakpoint.h. */
523
524 tracepoint_range
525 all_tracepoints ()
526 {
527 return tracepoint_range (breakpoint_chain);
528 }
529
530 /* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
531
532 static std::vector<bp_location *> bp_locations;
533
534 static const std::vector<bp_location *> &
535 all_bp_locations ()
536 {
537 return bp_locations;
538 }
539
540 /* Range to iterate over breakpoint locations at a given address. */
541
542 struct bp_locations_at_addr_range
543 {
544 using iterator = std::vector<bp_location *>::iterator;
545
546 bp_locations_at_addr_range (CORE_ADDR addr)
547 {
548 struct compare
549 {
550 bool operator() (const bp_location *loc, CORE_ADDR addr_) const
551 { return loc->address < addr_; }
552
553 bool operator() (CORE_ADDR addr_, const bp_location *loc) const
554 { return addr_ < loc->address; }
555 };
556
557 auto it_pair = std::equal_range (bp_locations.begin (), bp_locations.end (),
558 addr, compare ());
559
560 m_begin = it_pair.first;
561 m_end = it_pair.second;
562 }
563
564 iterator begin () const
565 { return m_begin; }
566
567 iterator end () const
568 { return m_end; }
569
570 private:
571 iterator m_begin;
572 iterator m_end;
573 };
574
575 /* Return a range to iterate over all breakpoint locations exactly at address
576 ADDR.
577
578 If it's needed to iterate multiple times on the same range, it's possible
579 to save the range in a local variable and use it multiple times:
580
581 auto range = all_bp_locations_at_addr (addr);
582
583 for (bp_location *loc : range)
584 // use loc
585
586 for (bp_location *loc : range)
587 // use loc
588
589 This saves a bit of time, as it avoids re-doing the binary searches to find
590 the range's boundaries. Just remember not to change the bp_locations vector
591 in the mean time, as it could make the range's iterators stale. */
592
593 static bp_locations_at_addr_range
594 all_bp_locations_at_addr (CORE_ADDR addr)
595 {
596 return bp_locations_at_addr_range (addr);
597 }
598
599 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
600 ADDRESS for the current elements of BP_LOCATIONS which get a valid
601 result from bp_location_has_shadow. You can use it for roughly
602 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
603 an address you need to read. */
604
605 static CORE_ADDR bp_locations_placed_address_before_address_max;
606
607 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
608 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
609 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
610 You can use it for roughly limiting the subrange of BP_LOCATIONS to
611 scan for shadow bytes for an address you need to read. */
612
613 static CORE_ADDR bp_locations_shadow_len_after_address_max;
614
615 /* The locations that no longer correspond to any breakpoint, unlinked
616 from the bp_locations array, but for which a hit may still be
617 reported by a target. */
618 static std::vector<bp_location *> moribund_locations;
619
620 /* Number of last breakpoint made. */
621
622 static int breakpoint_count;
623
624 /* The value of `breakpoint_count' before the last command that
625 created breakpoints. If the last (break-like) command created more
626 than one breakpoint, then the difference between BREAKPOINT_COUNT
627 and PREV_BREAKPOINT_COUNT is more than one. */
628 static int prev_breakpoint_count;
629
630 /* Number of last tracepoint made. */
631
632 static int tracepoint_count;
633
634 static struct cmd_list_element *breakpoint_set_cmdlist;
635 static struct cmd_list_element *breakpoint_show_cmdlist;
636 struct cmd_list_element *save_cmdlist;
637
638 /* See declaration at breakpoint.h. */
639
640 struct breakpoint *
641 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
642 void *user_data)
643 {
644 for (breakpoint *b : all_breakpoints ())
645 if (func (b, user_data) != 0)
646 return b;
647
648 return nullptr;
649 }
650
651 /* Return whether a breakpoint is an active enabled breakpoint. */
652 static int
653 breakpoint_enabled (struct breakpoint *b)
654 {
655 return (b->enable_state == bp_enabled);
656 }
657
658 /* Set breakpoint count to NUM. */
659
660 static void
661 set_breakpoint_count (int num)
662 {
663 prev_breakpoint_count = breakpoint_count;
664 breakpoint_count = num;
665 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
666 }
667
668 /* Used by `start_rbreak_breakpoints' below, to record the current
669 breakpoint count before "rbreak" creates any breakpoint. */
670 static int rbreak_start_breakpoint_count;
671
672 /* Called at the start an "rbreak" command to record the first
673 breakpoint made. */
674
675 scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
676 {
677 rbreak_start_breakpoint_count = breakpoint_count;
678 }
679
680 /* Called at the end of an "rbreak" command to record the last
681 breakpoint made. */
682
683 scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
684 {
685 prev_breakpoint_count = rbreak_start_breakpoint_count;
686 }
687
688 /* Used in run_command to zero the hit count when a new run starts. */
689
690 void
691 clear_breakpoint_hit_counts (void)
692 {
693 for (breakpoint *b : all_breakpoints ())
694 b->hit_count = 0;
695 }
696
697 \f
698 /* Return the breakpoint with the specified number, or NULL
699 if the number does not refer to an existing breakpoint. */
700
701 struct breakpoint *
702 get_breakpoint (int num)
703 {
704 for (breakpoint *b : all_breakpoints ())
705 if (b->number == num)
706 return b;
707
708 return nullptr;
709 }
710
711 \f
712
713 /* Mark locations as "conditions have changed" in case the target supports
714 evaluating conditions on its side. */
715
716 static void
717 mark_breakpoint_modified (struct breakpoint *b)
718 {
719 /* This is only meaningful if the target is
720 evaluating conditions and if the user has
721 opted for condition evaluation on the target's
722 side. */
723 if (gdb_evaluates_breakpoint_condition_p ()
724 || !target_supports_evaluation_of_breakpoint_conditions ())
725 return;
726
727 if (!is_breakpoint (b))
728 return;
729
730 for (bp_location *loc : b->locations ())
731 loc->condition_changed = condition_modified;
732 }
733
734 /* Mark location as "conditions have changed" in case the target supports
735 evaluating conditions on its side. */
736
737 static void
738 mark_breakpoint_location_modified (struct bp_location *loc)
739 {
740 /* This is only meaningful if the target is
741 evaluating conditions and if the user has
742 opted for condition evaluation on the target's
743 side. */
744 if (gdb_evaluates_breakpoint_condition_p ()
745 || !target_supports_evaluation_of_breakpoint_conditions ())
746
747 return;
748
749 if (!is_breakpoint (loc->owner))
750 return;
751
752 loc->condition_changed = condition_modified;
753 }
754
755 /* Sets the condition-evaluation mode using the static global
756 condition_evaluation_mode. */
757
758 static void
759 set_condition_evaluation_mode (const char *args, int from_tty,
760 struct cmd_list_element *c)
761 {
762 const char *old_mode, *new_mode;
763
764 if ((condition_evaluation_mode_1 == condition_evaluation_target)
765 && !target_supports_evaluation_of_breakpoint_conditions ())
766 {
767 condition_evaluation_mode_1 = condition_evaluation_mode;
768 warning (_("Target does not support breakpoint condition evaluation.\n"
769 "Using host evaluation mode instead."));
770 return;
771 }
772
773 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
774 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
775
776 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
777 settings was "auto". */
778 condition_evaluation_mode = condition_evaluation_mode_1;
779
780 /* Only update the mode if the user picked a different one. */
781 if (new_mode != old_mode)
782 {
783 /* If the user switched to a different evaluation mode, we
784 need to synch the changes with the target as follows:
785
786 "host" -> "target": Send all (valid) conditions to the target.
787 "target" -> "host": Remove all the conditions from the target.
788 */
789
790 if (new_mode == condition_evaluation_target)
791 {
792 /* Mark everything modified and synch conditions with the
793 target. */
794 for (bp_location *loc : all_bp_locations ())
795 mark_breakpoint_location_modified (loc);
796 }
797 else
798 {
799 /* Manually mark non-duplicate locations to synch conditions
800 with the target. We do this to remove all the conditions the
801 target knows about. */
802 for (bp_location *loc : all_bp_locations ())
803 if (is_breakpoint (loc->owner) && loc->inserted)
804 loc->needs_update = 1;
805 }
806
807 /* Do the update. */
808 update_global_location_list (UGLL_MAY_INSERT);
809 }
810
811 return;
812 }
813
814 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
815 what "auto" is translating to. */
816
817 static void
818 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
819 struct cmd_list_element *c, const char *value)
820 {
821 if (condition_evaluation_mode == condition_evaluation_auto)
822 fprintf_filtered (file,
823 _("Breakpoint condition evaluation "
824 "mode is %s (currently %s).\n"),
825 value,
826 breakpoint_condition_evaluation_mode ());
827 else
828 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
829 value);
830 }
831
832 /* Parse COND_STRING in the context of LOC and set as the condition
833 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
834 the number of LOC within its owner. In case of parsing error, mark
835 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
836
837 static void
838 set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
839 int bp_num, int loc_num)
840 {
841 bool has_junk = false;
842 try
843 {
844 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
845 block_for_pc (loc->address), 0);
846 if (*cond_string != 0)
847 has_junk = true;
848 else
849 {
850 loc->cond = std::move (new_exp);
851 if (loc->disabled_by_cond && loc->enabled)
852 printf_filtered (_("Breakpoint %d's condition is now valid at "
853 "location %d, enabling.\n"),
854 bp_num, loc_num);
855
856 loc->disabled_by_cond = false;
857 }
858 }
859 catch (const gdb_exception_error &e)
860 {
861 if (loc->enabled)
862 {
863 /* Warn if a user-enabled location is now becoming disabled-by-cond.
864 BP_NUM is 0 if the breakpoint is being defined for the first
865 time using the "break ... if ..." command, and non-zero if
866 already defined. */
867 if (bp_num != 0)
868 warning (_("failed to validate condition at location %d.%d, "
869 "disabling:\n %s"), bp_num, loc_num, e.what ());
870 else
871 warning (_("failed to validate condition at location %d, "
872 "disabling:\n %s"), loc_num, e.what ());
873 }
874
875 loc->disabled_by_cond = true;
876 }
877
878 if (has_junk)
879 error (_("Garbage '%s' follows condition"), cond_string);
880 }
881
882 void
883 set_breakpoint_condition (struct breakpoint *b, const char *exp,
884 int from_tty, bool force)
885 {
886 if (*exp == 0)
887 {
888 xfree (b->cond_string);
889 b->cond_string = nullptr;
890
891 if (is_watchpoint (b))
892 static_cast<watchpoint *> (b)->cond_exp.reset ();
893 else
894 {
895 int loc_num = 1;
896 for (bp_location *loc : b->locations ())
897 {
898 loc->cond.reset ();
899 if (loc->disabled_by_cond && loc->enabled)
900 printf_filtered (_("Breakpoint %d's condition is now valid at "
901 "location %d, enabling.\n"),
902 b->number, loc_num);
903 loc->disabled_by_cond = false;
904 loc_num++;
905
906 /* No need to free the condition agent expression
907 bytecode (if we have one). We will handle this
908 when we go through update_global_location_list. */
909 }
910 }
911
912 if (from_tty)
913 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
914 }
915 else
916 {
917 if (is_watchpoint (b))
918 {
919 innermost_block_tracker tracker;
920 const char *arg = exp;
921 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
922 if (*arg != 0)
923 error (_("Junk at end of expression"));
924 watchpoint *w = static_cast<watchpoint *> (b);
925 w->cond_exp = std::move (new_exp);
926 w->cond_exp_valid_block = tracker.block ();
927 }
928 else
929 {
930 /* Parse and set condition expressions. We make two passes.
931 In the first, we parse the condition string to see if it
932 is valid in at least one location. If so, the condition
933 would be accepted. So we go ahead and set the locations'
934 conditions. In case no valid case is found, we throw
935 the error and the condition string will be rejected.
936 This two-pass approach is taken to avoid setting the
937 state of locations in case of a reject. */
938 for (bp_location *loc : b->locations ())
939 {
940 try
941 {
942 const char *arg = exp;
943 parse_exp_1 (&arg, loc->address,
944 block_for_pc (loc->address), 0);
945 if (*arg != 0)
946 error (_("Junk at end of expression"));
947 break;
948 }
949 catch (const gdb_exception_error &e)
950 {
951 /* Condition string is invalid. If this happens to
952 be the last loc, abandon (if not forced) or continue
953 (if forced). */
954 if (loc->next == nullptr && !force)
955 throw;
956 }
957 }
958
959 /* If we reach here, the condition is valid at some locations. */
960 int loc_num = 1;
961 for (bp_location *loc : b->locations ())
962 {
963 set_breakpoint_location_condition (exp, loc, b->number, loc_num);
964 loc_num++;
965 }
966 }
967
968 /* We know that the new condition parsed successfully. The
969 condition string of the breakpoint can be safely updated. */
970 xfree (b->cond_string);
971 b->cond_string = xstrdup (exp);
972 b->condition_not_parsed = 0;
973 }
974 mark_breakpoint_modified (b);
975
976 gdb::observers::breakpoint_modified.notify (b);
977 }
978
979 /* See breakpoint.h. */
980
981 void
982 set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
983 bool force)
984 {
985 for (breakpoint *b : all_breakpoints ())
986 if (b->number == bpnum)
987 {
988 /* Check if this breakpoint has a "stop" method implemented in an
989 extension language. This method and conditions entered into GDB
990 from the CLI are mutually exclusive. */
991 const struct extension_language_defn *extlang
992 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
993
994 if (extlang != NULL)
995 {
996 error (_("Only one stop condition allowed. There is currently"
997 " a %s stop condition defined for this breakpoint."),
998 ext_lang_capitalized_name (extlang));
999 }
1000 set_breakpoint_condition (b, exp, from_tty, force);
1001
1002 if (is_breakpoint (b))
1003 update_global_location_list (UGLL_MAY_INSERT);
1004
1005 return;
1006 }
1007
1008 error (_("No breakpoint number %d."), bpnum);
1009 }
1010
1011 /* The options for the "condition" command. */
1012
1013 struct condition_command_opts
1014 {
1015 /* For "-force". */
1016 bool force_condition = false;
1017 };
1018
1019 static const gdb::option::option_def condition_command_option_defs[] = {
1020
1021 gdb::option::flag_option_def<condition_command_opts> {
1022 "force",
1023 [] (condition_command_opts *opts) { return &opts->force_condition; },
1024 N_("Set the condition even if it is invalid for all current locations."),
1025 },
1026
1027 };
1028
1029 /* Create an option_def_group for the "condition" options, with
1030 CC_OPTS as context. */
1031
1032 static inline gdb::option::option_def_group
1033 make_condition_command_options_def_group (condition_command_opts *cc_opts)
1034 {
1035 return {{condition_command_option_defs}, cc_opts};
1036 }
1037
1038 /* Completion for the "condition" command. */
1039
1040 static void
1041 condition_completer (struct cmd_list_element *cmd,
1042 completion_tracker &tracker,
1043 const char *text, const char * /*word*/)
1044 {
1045 bool has_no_arguments = (*text == '\0');
1046 condition_command_opts cc_opts;
1047 const auto group = make_condition_command_options_def_group (&cc_opts);
1048 if (gdb::option::complete_options
1049 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1050 return;
1051
1052 text = skip_spaces (text);
1053 const char *space = skip_to_space (text);
1054 if (*space == '\0')
1055 {
1056 int len;
1057
1058 if (text[0] == '$')
1059 {
1060 tracker.advance_custom_word_point_by (1);
1061 /* We don't support completion of history indices. */
1062 if (!isdigit (text[1]))
1063 complete_internalvar (tracker, &text[1]);
1064 return;
1065 }
1066
1067 /* Suggest the "-force" flag if no arguments are given. If
1068 arguments were passed, they either already include the flag,
1069 or we are beyond the point of suggesting it because it's
1070 positionally the first argument. */
1071 if (has_no_arguments)
1072 gdb::option::complete_on_all_options (tracker, group);
1073
1074 /* We're completing the breakpoint number. */
1075 len = strlen (text);
1076
1077 for (breakpoint *b : all_breakpoints ())
1078 {
1079 char number[50];
1080
1081 xsnprintf (number, sizeof (number), "%d", b->number);
1082
1083 if (strncmp (number, text, len) == 0)
1084 tracker.add_completion (make_unique_xstrdup (number));
1085 }
1086
1087 return;
1088 }
1089
1090 /* We're completing the expression part. Skip the breakpoint num. */
1091 const char *exp_start = skip_spaces (space);
1092 tracker.advance_custom_word_point_by (exp_start - text);
1093 text = exp_start;
1094 const char *word = advance_to_expression_complete_word_point (tracker, text);
1095 expression_completer (cmd, tracker, text, word);
1096 }
1097
1098 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1099
1100 static void
1101 condition_command (const char *arg, int from_tty)
1102 {
1103 const char *p;
1104 int bnum;
1105
1106 if (arg == 0)
1107 error_no_arg (_("breakpoint number"));
1108
1109 p = arg;
1110
1111 /* Check if the "-force" flag was passed. */
1112 condition_command_opts cc_opts;
1113 const auto group = make_condition_command_options_def_group (&cc_opts);
1114 gdb::option::process_options
1115 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
1116
1117 bnum = get_number (&p);
1118 if (bnum == 0)
1119 error (_("Bad breakpoint argument: '%s'"), arg);
1120
1121 set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
1122 }
1123
1124 /* Check that COMMAND do not contain commands that are suitable
1125 only for tracepoints and not suitable for ordinary breakpoints.
1126 Throw if any such commands is found. */
1127
1128 static void
1129 check_no_tracepoint_commands (struct command_line *commands)
1130 {
1131 struct command_line *c;
1132
1133 for (c = commands; c; c = c->next)
1134 {
1135 if (c->control_type == while_stepping_control)
1136 error (_("The 'while-stepping' command can "
1137 "only be used for tracepoints"));
1138
1139 check_no_tracepoint_commands (c->body_list_0.get ());
1140 check_no_tracepoint_commands (c->body_list_1.get ());
1141
1142 /* Not that command parsing removes leading whitespace and comment
1143 lines and also empty lines. So, we only need to check for
1144 command directly. */
1145 if (strstr (c->line, "collect ") == c->line)
1146 error (_("The 'collect' command can only be used for tracepoints"));
1147
1148 if (strstr (c->line, "teval ") == c->line)
1149 error (_("The 'teval' command can only be used for tracepoints"));
1150 }
1151 }
1152
1153 struct longjmp_breakpoint : public breakpoint
1154 {
1155 ~longjmp_breakpoint () override;
1156 };
1157
1158 /* Encapsulate tests for different types of tracepoints. */
1159
1160 static bool
1161 is_tracepoint_type (bptype type)
1162 {
1163 return (type == bp_tracepoint
1164 || type == bp_fast_tracepoint
1165 || type == bp_static_tracepoint);
1166 }
1167
1168 static bool
1169 is_longjmp_type (bptype type)
1170 {
1171 return type == bp_longjmp || type == bp_exception;
1172 }
1173
1174 /* See breakpoint.h. */
1175
1176 bool
1177 is_tracepoint (const struct breakpoint *b)
1178 {
1179 return is_tracepoint_type (b->type);
1180 }
1181
1182 /* Factory function to create an appropriate instance of breakpoint given
1183 TYPE. */
1184
1185 static std::unique_ptr<breakpoint>
1186 new_breakpoint_from_type (bptype type)
1187 {
1188 breakpoint *b;
1189
1190 if (is_tracepoint_type (type))
1191 b = new tracepoint ();
1192 else if (is_longjmp_type (type))
1193 b = new longjmp_breakpoint ();
1194 else
1195 b = new breakpoint ();
1196
1197 return std::unique_ptr<breakpoint> (b);
1198 }
1199
1200 /* A helper function that validates that COMMANDS are valid for a
1201 breakpoint. This function will throw an exception if a problem is
1202 found. */
1203
1204 static void
1205 validate_commands_for_breakpoint (struct breakpoint *b,
1206 struct command_line *commands)
1207 {
1208 if (is_tracepoint (b))
1209 {
1210 struct tracepoint *t = (struct tracepoint *) b;
1211 struct command_line *c;
1212 struct command_line *while_stepping = 0;
1213
1214 /* Reset the while-stepping step count. The previous commands
1215 might have included a while-stepping action, while the new
1216 ones might not. */
1217 t->step_count = 0;
1218
1219 /* We need to verify that each top-level element of commands is
1220 valid for tracepoints, that there's at most one
1221 while-stepping element, and that the while-stepping's body
1222 has valid tracing commands excluding nested while-stepping.
1223 We also need to validate the tracepoint action line in the
1224 context of the tracepoint --- validate_actionline actually
1225 has side effects, like setting the tracepoint's
1226 while-stepping STEP_COUNT, in addition to checking if the
1227 collect/teval actions parse and make sense in the
1228 tracepoint's context. */
1229 for (c = commands; c; c = c->next)
1230 {
1231 if (c->control_type == while_stepping_control)
1232 {
1233 if (b->type == bp_fast_tracepoint)
1234 error (_("The 'while-stepping' command "
1235 "cannot be used for fast tracepoint"));
1236 else if (b->type == bp_static_tracepoint)
1237 error (_("The 'while-stepping' command "
1238 "cannot be used for static tracepoint"));
1239
1240 if (while_stepping)
1241 error (_("The 'while-stepping' command "
1242 "can be used only once"));
1243 else
1244 while_stepping = c;
1245 }
1246
1247 validate_actionline (c->line, b);
1248 }
1249 if (while_stepping)
1250 {
1251 struct command_line *c2;
1252
1253 gdb_assert (while_stepping->body_list_1 == nullptr);
1254 c2 = while_stepping->body_list_0.get ();
1255 for (; c2; c2 = c2->next)
1256 {
1257 if (c2->control_type == while_stepping_control)
1258 error (_("The 'while-stepping' command cannot be nested"));
1259 }
1260 }
1261 }
1262 else
1263 {
1264 check_no_tracepoint_commands (commands);
1265 }
1266 }
1267
1268 /* Return a vector of all the static tracepoints set at ADDR. The
1269 caller is responsible for releasing the vector. */
1270
1271 std::vector<breakpoint *>
1272 static_tracepoints_here (CORE_ADDR addr)
1273 {
1274 std::vector<breakpoint *> found;
1275
1276 for (breakpoint *b : all_breakpoints ())
1277 if (b->type == bp_static_tracepoint)
1278 {
1279 for (bp_location *loc : b->locations ())
1280 if (loc->address == addr)
1281 found.push_back (b);
1282 }
1283
1284 return found;
1285 }
1286
1287 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1288 validate that only allowed commands are included. */
1289
1290 void
1291 breakpoint_set_commands (struct breakpoint *b,
1292 counted_command_line &&commands)
1293 {
1294 validate_commands_for_breakpoint (b, commands.get ());
1295
1296 b->commands = std::move (commands);
1297 gdb::observers::breakpoint_modified.notify (b);
1298 }
1299
1300 /* Set the internal `silent' flag on the breakpoint. Note that this
1301 is not the same as the "silent" that may appear in the breakpoint's
1302 commands. */
1303
1304 void
1305 breakpoint_set_silent (struct breakpoint *b, int silent)
1306 {
1307 int old_silent = b->silent;
1308
1309 b->silent = silent;
1310 if (old_silent != silent)
1311 gdb::observers::breakpoint_modified.notify (b);
1312 }
1313
1314 /* Set the thread for this breakpoint. If THREAD is -1, make the
1315 breakpoint work for any thread. */
1316
1317 void
1318 breakpoint_set_thread (struct breakpoint *b, int thread)
1319 {
1320 int old_thread = b->thread;
1321
1322 b->thread = thread;
1323 if (old_thread != thread)
1324 gdb::observers::breakpoint_modified.notify (b);
1325 }
1326
1327 /* Set the task for this breakpoint. If TASK is 0, make the
1328 breakpoint work for any task. */
1329
1330 void
1331 breakpoint_set_task (struct breakpoint *b, int task)
1332 {
1333 int old_task = b->task;
1334
1335 b->task = task;
1336 if (old_task != task)
1337 gdb::observers::breakpoint_modified.notify (b);
1338 }
1339
1340 static void
1341 commands_command_1 (const char *arg, int from_tty,
1342 struct command_line *control)
1343 {
1344 counted_command_line cmd;
1345 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1346 NULL after the call to read_command_lines if the user provides an empty
1347 list of command by just typing "end". */
1348 bool cmd_read = false;
1349
1350 std::string new_arg;
1351
1352 if (arg == NULL || !*arg)
1353 {
1354 /* Argument not explicitly given. Synthesize it. */
1355 if (breakpoint_count - prev_breakpoint_count > 1)
1356 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1357 breakpoint_count);
1358 else if (breakpoint_count > 0)
1359 new_arg = string_printf ("%d", breakpoint_count);
1360 }
1361 else
1362 {
1363 /* Create a copy of ARG. This is needed because the "commands"
1364 command may be coming from a script. In that case, the read
1365 line buffer is going to be overwritten in the lambda of
1366 'map_breakpoint_numbers' below when reading the next line
1367 before we are are done parsing the breakpoint numbers. */
1368 new_arg = arg;
1369 }
1370 arg = new_arg.c_str ();
1371
1372 map_breakpoint_numbers
1373 (arg, [&] (breakpoint *b)
1374 {
1375 if (!cmd_read)
1376 {
1377 gdb_assert (cmd == NULL);
1378 if (control != NULL)
1379 cmd = control->body_list_0;
1380 else
1381 {
1382 std::string str
1383 = string_printf (_("Type commands for breakpoint(s) "
1384 "%s, one per line."),
1385 arg);
1386
1387 auto do_validate = [=] (const char *line)
1388 {
1389 validate_actionline (line, b);
1390 };
1391 gdb::function_view<void (const char *)> validator;
1392 if (is_tracepoint (b))
1393 validator = do_validate;
1394
1395 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1396 }
1397 cmd_read = true;
1398 }
1399
1400 /* If a breakpoint was on the list more than once, we don't need to
1401 do anything. */
1402 if (b->commands != cmd)
1403 {
1404 validate_commands_for_breakpoint (b, cmd.get ());
1405 b->commands = cmd;
1406 gdb::observers::breakpoint_modified.notify (b);
1407 }
1408 });
1409 }
1410
1411 static void
1412 commands_command (const char *arg, int from_tty)
1413 {
1414 commands_command_1 (arg, from_tty, NULL);
1415 }
1416
1417 /* Like commands_command, but instead of reading the commands from
1418 input stream, takes them from an already parsed command structure.
1419
1420 This is used by cli-script.c to DTRT with breakpoint commands
1421 that are part of if and while bodies. */
1422 enum command_control_type
1423 commands_from_control_command (const char *arg, struct command_line *cmd)
1424 {
1425 commands_command_1 (arg, 0, cmd);
1426 return simple_control;
1427 }
1428
1429 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1430
1431 static int
1432 bp_location_has_shadow (struct bp_location *bl)
1433 {
1434 if (bl->loc_type != bp_loc_software_breakpoint)
1435 return 0;
1436 if (!bl->inserted)
1437 return 0;
1438 if (bl->target_info.shadow_len == 0)
1439 /* BL isn't valid, or doesn't shadow memory. */
1440 return 0;
1441 return 1;
1442 }
1443
1444 /* Update BUF, which is LEN bytes read from the target address
1445 MEMADDR, by replacing a memory breakpoint with its shadowed
1446 contents.
1447
1448 If READBUF is not NULL, this buffer must not overlap with the of
1449 the breakpoint location's shadow_contents buffer. Otherwise, a
1450 failed assertion internal error will be raised. */
1451
1452 static void
1453 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1454 const gdb_byte *writebuf_org,
1455 ULONGEST memaddr, LONGEST len,
1456 struct bp_target_info *target_info,
1457 struct gdbarch *gdbarch)
1458 {
1459 /* Now do full processing of the found relevant range of elements. */
1460 CORE_ADDR bp_addr = 0;
1461 int bp_size = 0;
1462 int bptoffset = 0;
1463
1464 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1465 current_program_space->aspace, 0))
1466 {
1467 /* The breakpoint is inserted in a different address space. */
1468 return;
1469 }
1470
1471 /* Addresses and length of the part of the breakpoint that
1472 we need to copy. */
1473 bp_addr = target_info->placed_address;
1474 bp_size = target_info->shadow_len;
1475
1476 if (bp_addr + bp_size <= memaddr)
1477 {
1478 /* The breakpoint is entirely before the chunk of memory we are
1479 reading. */
1480 return;
1481 }
1482
1483 if (bp_addr >= memaddr + len)
1484 {
1485 /* The breakpoint is entirely after the chunk of memory we are
1486 reading. */
1487 return;
1488 }
1489
1490 /* Offset within shadow_contents. */
1491 if (bp_addr < memaddr)
1492 {
1493 /* Only copy the second part of the breakpoint. */
1494 bp_size -= memaddr - bp_addr;
1495 bptoffset = memaddr - bp_addr;
1496 bp_addr = memaddr;
1497 }
1498
1499 if (bp_addr + bp_size > memaddr + len)
1500 {
1501 /* Only copy the first part of the breakpoint. */
1502 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1503 }
1504
1505 if (readbuf != NULL)
1506 {
1507 /* Verify that the readbuf buffer does not overlap with the
1508 shadow_contents buffer. */
1509 gdb_assert (target_info->shadow_contents >= readbuf + len
1510 || readbuf >= (target_info->shadow_contents
1511 + target_info->shadow_len));
1512
1513 /* Update the read buffer with this inserted breakpoint's
1514 shadow. */
1515 memcpy (readbuf + bp_addr - memaddr,
1516 target_info->shadow_contents + bptoffset, bp_size);
1517 }
1518 else
1519 {
1520 const unsigned char *bp;
1521 CORE_ADDR addr = target_info->reqstd_address;
1522 int placed_size;
1523
1524 /* Update the shadow with what we want to write to memory. */
1525 memcpy (target_info->shadow_contents + bptoffset,
1526 writebuf_org + bp_addr - memaddr, bp_size);
1527
1528 /* Determine appropriate breakpoint contents and size for this
1529 address. */
1530 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1531
1532 /* Update the final write buffer with this inserted
1533 breakpoint's INSN. */
1534 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1535 }
1536 }
1537
1538 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1539 by replacing any memory breakpoints with their shadowed contents.
1540
1541 If READBUF is not NULL, this buffer must not overlap with any of
1542 the breakpoint location's shadow_contents buffers. Otherwise,
1543 a failed assertion internal error will be raised.
1544
1545 The range of shadowed area by each bp_location is:
1546 bl->address - bp_locations_placed_address_before_address_max
1547 up to bl->address + bp_locations_shadow_len_after_address_max
1548 The range we were requested to resolve shadows for is:
1549 memaddr ... memaddr + len
1550 Thus the safe cutoff boundaries for performance optimization are
1551 memaddr + len <= (bl->address
1552 - bp_locations_placed_address_before_address_max)
1553 and:
1554 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1555
1556 void
1557 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1558 const gdb_byte *writebuf_org,
1559 ULONGEST memaddr, LONGEST len)
1560 {
1561 /* Left boundary, right boundary and median element of our binary
1562 search. */
1563 unsigned bc_l, bc_r, bc;
1564
1565 /* Find BC_L which is a leftmost element which may affect BUF
1566 content. It is safe to report lower value but a failure to
1567 report higher one. */
1568
1569 bc_l = 0;
1570 bc_r = bp_locations.size ();
1571 while (bc_l + 1 < bc_r)
1572 {
1573 struct bp_location *bl;
1574
1575 bc = (bc_l + bc_r) / 2;
1576 bl = bp_locations[bc];
1577
1578 /* Check first BL->ADDRESS will not overflow due to the added
1579 constant. Then advance the left boundary only if we are sure
1580 the BC element can in no way affect the BUF content (MEMADDR
1581 to MEMADDR + LEN range).
1582
1583 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1584 offset so that we cannot miss a breakpoint with its shadow
1585 range tail still reaching MEMADDR. */
1586
1587 if ((bl->address + bp_locations_shadow_len_after_address_max
1588 >= bl->address)
1589 && (bl->address + bp_locations_shadow_len_after_address_max
1590 <= memaddr))
1591 bc_l = bc;
1592 else
1593 bc_r = bc;
1594 }
1595
1596 /* Due to the binary search above, we need to make sure we pick the
1597 first location that's at BC_L's address. E.g., if there are
1598 multiple locations at the same address, BC_L may end up pointing
1599 at a duplicate location, and miss the "master"/"inserted"
1600 location. Say, given locations L1, L2 and L3 at addresses A and
1601 B:
1602
1603 L1@A, L2@A, L3@B, ...
1604
1605 BC_L could end up pointing at location L2, while the "master"
1606 location could be L1. Since the `loc->inserted' flag is only set
1607 on "master" locations, we'd forget to restore the shadow of L1
1608 and L2. */
1609 while (bc_l > 0
1610 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1611 bc_l--;
1612
1613 /* Now do full processing of the found relevant range of elements. */
1614
1615 for (bc = bc_l; bc < bp_locations.size (); bc++)
1616 {
1617 struct bp_location *bl = bp_locations[bc];
1618
1619 /* bp_location array has BL->OWNER always non-NULL. */
1620 if (bl->owner->type == bp_none)
1621 warning (_("reading through apparently deleted breakpoint #%d?"),
1622 bl->owner->number);
1623
1624 /* Performance optimization: any further element can no longer affect BUF
1625 content. */
1626
1627 if (bl->address >= bp_locations_placed_address_before_address_max
1628 && memaddr + len <= (bl->address
1629 - bp_locations_placed_address_before_address_max))
1630 break;
1631
1632 if (!bp_location_has_shadow (bl))
1633 continue;
1634
1635 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1636 memaddr, len, &bl->target_info, bl->gdbarch);
1637 }
1638 }
1639
1640 /* See breakpoint.h. */
1641
1642 bool
1643 is_breakpoint (const struct breakpoint *bpt)
1644 {
1645 return (bpt->type == bp_breakpoint
1646 || bpt->type == bp_hardware_breakpoint
1647 || bpt->type == bp_dprintf);
1648 }
1649
1650 /* Return true if BPT is of any hardware watchpoint kind. */
1651
1652 static bool
1653 is_hardware_watchpoint (const struct breakpoint *bpt)
1654 {
1655 return (bpt->type == bp_hardware_watchpoint
1656 || bpt->type == bp_read_watchpoint
1657 || bpt->type == bp_access_watchpoint);
1658 }
1659
1660 /* See breakpoint.h. */
1661
1662 bool
1663 is_watchpoint (const struct breakpoint *bpt)
1664 {
1665 return (is_hardware_watchpoint (bpt)
1666 || bpt->type == bp_watchpoint);
1667 }
1668
1669 /* Returns true if the current thread and its running state are safe
1670 to evaluate or update watchpoint B. Watchpoints on local
1671 expressions need to be evaluated in the context of the thread that
1672 was current when the watchpoint was created, and, that thread needs
1673 to be stopped to be able to select the correct frame context.
1674 Watchpoints on global expressions can be evaluated on any thread,
1675 and in any state. It is presently left to the target allowing
1676 memory accesses when threads are running. */
1677
1678 static int
1679 watchpoint_in_thread_scope (struct watchpoint *b)
1680 {
1681 return (b->pspace == current_program_space
1682 && (b->watchpoint_thread == null_ptid
1683 || (inferior_ptid == b->watchpoint_thread
1684 && !inferior_thread ()->executing)));
1685 }
1686
1687 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1688 associated bp_watchpoint_scope breakpoint. */
1689
1690 static void
1691 watchpoint_del_at_next_stop (struct watchpoint *w)
1692 {
1693 if (w->related_breakpoint != w)
1694 {
1695 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1696 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1697 w->related_breakpoint->disposition = disp_del_at_next_stop;
1698 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1699 w->related_breakpoint = w;
1700 }
1701 w->disposition = disp_del_at_next_stop;
1702 }
1703
1704 /* Extract a bitfield value from value VAL using the bit parameters contained in
1705 watchpoint W. */
1706
1707 static struct value *
1708 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1709 {
1710 struct value *bit_val;
1711
1712 if (val == NULL)
1713 return NULL;
1714
1715 bit_val = allocate_value (value_type (val));
1716
1717 unpack_value_bitfield (bit_val,
1718 w->val_bitpos,
1719 w->val_bitsize,
1720 value_contents_for_printing (val),
1721 value_offset (val),
1722 val);
1723
1724 return bit_val;
1725 }
1726
1727 /* Allocate a dummy location and add it to B, which must be a software
1728 watchpoint. This is required because even if a software watchpoint
1729 is not watching any memory, bpstat_stop_status requires a location
1730 to be able to report stops. */
1731
1732 static void
1733 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1734 struct program_space *pspace)
1735 {
1736 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1737
1738 b->loc = allocate_bp_location (b);
1739 b->loc->pspace = pspace;
1740 b->loc->address = -1;
1741 b->loc->length = -1;
1742 }
1743
1744 /* Returns true if B is a software watchpoint that is not watching any
1745 memory (e.g., "watch $pc"). */
1746
1747 static bool
1748 is_no_memory_software_watchpoint (struct breakpoint *b)
1749 {
1750 return (b->type == bp_watchpoint
1751 && b->loc != NULL
1752 && b->loc->next == NULL
1753 && b->loc->address == -1
1754 && b->loc->length == -1);
1755 }
1756
1757 /* Assuming that B is a watchpoint:
1758 - Reparse watchpoint expression, if REPARSE is non-zero
1759 - Evaluate expression and store the result in B->val
1760 - Evaluate the condition if there is one, and store the result
1761 in b->loc->cond.
1762 - Update the list of values that must be watched in B->loc.
1763
1764 If the watchpoint disposition is disp_del_at_next_stop, then do
1765 nothing. If this is local watchpoint that is out of scope, delete
1766 it.
1767
1768 Even with `set breakpoint always-inserted on' the watchpoints are
1769 removed + inserted on each stop here. Normal breakpoints must
1770 never be removed because they might be missed by a running thread
1771 when debugging in non-stop mode. On the other hand, hardware
1772 watchpoints (is_hardware_watchpoint; processed here) are specific
1773 to each LWP since they are stored in each LWP's hardware debug
1774 registers. Therefore, such LWP must be stopped first in order to
1775 be able to modify its hardware watchpoints.
1776
1777 Hardware watchpoints must be reset exactly once after being
1778 presented to the user. It cannot be done sooner, because it would
1779 reset the data used to present the watchpoint hit to the user. And
1780 it must not be done later because it could display the same single
1781 watchpoint hit during multiple GDB stops. Note that the latter is
1782 relevant only to the hardware watchpoint types bp_read_watchpoint
1783 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1784 not user-visible - its hit is suppressed if the memory content has
1785 not changed.
1786
1787 The following constraints influence the location where we can reset
1788 hardware watchpoints:
1789
1790 * target_stopped_by_watchpoint and target_stopped_data_address are
1791 called several times when GDB stops.
1792
1793 [linux]
1794 * Multiple hardware watchpoints can be hit at the same time,
1795 causing GDB to stop. GDB only presents one hardware watchpoint
1796 hit at a time as the reason for stopping, and all the other hits
1797 are presented later, one after the other, each time the user
1798 requests the execution to be resumed. Execution is not resumed
1799 for the threads still having pending hit event stored in
1800 LWP_INFO->STATUS. While the watchpoint is already removed from
1801 the inferior on the first stop the thread hit event is kept being
1802 reported from its cached value by linux_nat_stopped_data_address
1803 until the real thread resume happens after the watchpoint gets
1804 presented and thus its LWP_INFO->STATUS gets reset.
1805
1806 Therefore the hardware watchpoint hit can get safely reset on the
1807 watchpoint removal from inferior. */
1808
1809 static void
1810 update_watchpoint (struct watchpoint *b, int reparse)
1811 {
1812 int within_current_scope;
1813 struct frame_id saved_frame_id;
1814 int frame_saved;
1815
1816 /* If this is a local watchpoint, we only want to check if the
1817 watchpoint frame is in scope if the current thread is the thread
1818 that was used to create the watchpoint. */
1819 if (!watchpoint_in_thread_scope (b))
1820 return;
1821
1822 if (b->disposition == disp_del_at_next_stop)
1823 return;
1824
1825 frame_saved = 0;
1826
1827 /* Determine if the watchpoint is within scope. */
1828 if (b->exp_valid_block == NULL)
1829 within_current_scope = 1;
1830 else
1831 {
1832 struct frame_info *fi = get_current_frame ();
1833 struct gdbarch *frame_arch = get_frame_arch (fi);
1834 CORE_ADDR frame_pc = get_frame_pc (fi);
1835
1836 /* If we're at a point where the stack has been destroyed
1837 (e.g. in a function epilogue), unwinding may not work
1838 properly. Do not attempt to recreate locations at this
1839 point. See similar comments in watchpoint_check. */
1840 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1841 return;
1842
1843 /* Save the current frame's ID so we can restore it after
1844 evaluating the watchpoint expression on its own frame. */
1845 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1846 took a frame parameter, so that we didn't have to change the
1847 selected frame. */
1848 frame_saved = 1;
1849 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1850
1851 fi = frame_find_by_id (b->watchpoint_frame);
1852 within_current_scope = (fi != NULL);
1853 if (within_current_scope)
1854 select_frame (fi);
1855 }
1856
1857 /* We don't free locations. They are stored in the bp_location array
1858 and update_global_location_list will eventually delete them and
1859 remove breakpoints if needed. */
1860 b->loc = NULL;
1861
1862 if (within_current_scope && reparse)
1863 {
1864 const char *s;
1865
1866 b->exp.reset ();
1867 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1868 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1869 /* If the meaning of expression itself changed, the old value is
1870 no longer relevant. We don't want to report a watchpoint hit
1871 to the user when the old value and the new value may actually
1872 be completely different objects. */
1873 b->val = NULL;
1874 b->val_valid = false;
1875
1876 /* Note that unlike with breakpoints, the watchpoint's condition
1877 expression is stored in the breakpoint object, not in the
1878 locations (re)created below. */
1879 if (b->cond_string != NULL)
1880 {
1881 b->cond_exp.reset ();
1882
1883 s = b->cond_string;
1884 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1885 }
1886 }
1887
1888 /* If we failed to parse the expression, for example because
1889 it refers to a global variable in a not-yet-loaded shared library,
1890 don't try to insert watchpoint. We don't automatically delete
1891 such watchpoint, though, since failure to parse expression
1892 is different from out-of-scope watchpoint. */
1893 if (!target_has_execution ())
1894 {
1895 /* Without execution, memory can't change. No use to try and
1896 set watchpoint locations. The watchpoint will be reset when
1897 the target gains execution, through breakpoint_re_set. */
1898 if (!can_use_hw_watchpoints)
1899 {
1900 if (b->ops->works_in_software_mode (b))
1901 b->type = bp_watchpoint;
1902 else
1903 error (_("Can't set read/access watchpoint when "
1904 "hardware watchpoints are disabled."));
1905 }
1906 }
1907 else if (within_current_scope && b->exp)
1908 {
1909 std::vector<value_ref_ptr> val_chain;
1910 struct value *v, *result;
1911 struct program_space *frame_pspace;
1912
1913 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
1914 &val_chain, false);
1915
1916 /* Avoid setting b->val if it's already set. The meaning of
1917 b->val is 'the last value' user saw, and we should update
1918 it only if we reported that last value to user. As it
1919 happens, the code that reports it updates b->val directly.
1920 We don't keep track of the memory value for masked
1921 watchpoints. */
1922 if (!b->val_valid && !is_masked_watchpoint (b))
1923 {
1924 if (b->val_bitsize != 0)
1925 v = extract_bitfield_from_watchpoint_value (b, v);
1926 b->val = release_value (v);
1927 b->val_valid = true;
1928 }
1929
1930 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1931
1932 /* Look at each value on the value chain. */
1933 gdb_assert (!val_chain.empty ());
1934 for (const value_ref_ptr &iter : val_chain)
1935 {
1936 v = iter.get ();
1937
1938 /* If it's a memory location, and GDB actually needed
1939 its contents to evaluate the expression, then we
1940 must watch it. If the first value returned is
1941 still lazy, that means an error occurred reading it;
1942 watch it anyway in case it becomes readable. */
1943 if (VALUE_LVAL (v) == lval_memory
1944 && (v == val_chain[0] || ! value_lazy (v)))
1945 {
1946 struct type *vtype = check_typedef (value_type (v));
1947
1948 /* We only watch structs and arrays if user asked
1949 for it explicitly, never if they just happen to
1950 appear in the middle of some value chain. */
1951 if (v == result
1952 || (vtype->code () != TYPE_CODE_STRUCT
1953 && vtype->code () != TYPE_CODE_ARRAY))
1954 {
1955 CORE_ADDR addr;
1956 enum target_hw_bp_type type;
1957 struct bp_location *loc, **tmp;
1958 int bitpos = 0, bitsize = 0;
1959
1960 if (value_bitsize (v) != 0)
1961 {
1962 /* Extract the bit parameters out from the bitfield
1963 sub-expression. */
1964 bitpos = value_bitpos (v);
1965 bitsize = value_bitsize (v);
1966 }
1967 else if (v == result && b->val_bitsize != 0)
1968 {
1969 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1970 lvalue whose bit parameters are saved in the fields
1971 VAL_BITPOS and VAL_BITSIZE. */
1972 bitpos = b->val_bitpos;
1973 bitsize = b->val_bitsize;
1974 }
1975
1976 addr = value_address (v);
1977 if (bitsize != 0)
1978 {
1979 /* Skip the bytes that don't contain the bitfield. */
1980 addr += bitpos / 8;
1981 }
1982
1983 type = hw_write;
1984 if (b->type == bp_read_watchpoint)
1985 type = hw_read;
1986 else if (b->type == bp_access_watchpoint)
1987 type = hw_access;
1988
1989 loc = allocate_bp_location (b);
1990 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1991 ;
1992 *tmp = loc;
1993 loc->gdbarch = value_type (v)->arch ();
1994
1995 loc->pspace = frame_pspace;
1996 loc->address = address_significant (loc->gdbarch, addr);
1997
1998 if (bitsize != 0)
1999 {
2000 /* Just cover the bytes that make up the bitfield. */
2001 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2002 }
2003 else
2004 loc->length = TYPE_LENGTH (value_type (v));
2005
2006 loc->watchpoint_type = type;
2007 }
2008 }
2009 }
2010
2011 /* Change the type of breakpoint between hardware assisted or
2012 an ordinary watchpoint depending on the hardware support
2013 and free hardware slots. REPARSE is set when the inferior
2014 is started. */
2015 if (reparse)
2016 {
2017 int reg_cnt;
2018 enum bp_loc_type loc_type;
2019
2020 reg_cnt = can_use_hardware_watchpoint (val_chain);
2021
2022 if (reg_cnt)
2023 {
2024 int i, target_resources_ok, other_type_used;
2025 enum bptype type;
2026
2027 /* Use an exact watchpoint when there's only one memory region to be
2028 watched, and only one debug register is needed to watch it. */
2029 b->exact = target_exact_watchpoints && reg_cnt == 1;
2030
2031 /* We need to determine how many resources are already
2032 used for all other hardware watchpoints plus this one
2033 to see if we still have enough resources to also fit
2034 this watchpoint in as well. */
2035
2036 /* If this is a software watchpoint, we try to turn it
2037 to a hardware one -- count resources as if B was of
2038 hardware watchpoint type. */
2039 type = b->type;
2040 if (type == bp_watchpoint)
2041 type = bp_hardware_watchpoint;
2042
2043 /* This watchpoint may or may not have been placed on
2044 the list yet at this point (it won't be in the list
2045 if we're trying to create it for the first time,
2046 through watch_command), so always account for it
2047 manually. */
2048
2049 /* Count resources used by all watchpoints except B. */
2050 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
2051
2052 /* Add in the resources needed for B. */
2053 i += hw_watchpoint_use_count (b);
2054
2055 target_resources_ok
2056 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2057 if (target_resources_ok <= 0)
2058 {
2059 int sw_mode = b->ops->works_in_software_mode (b);
2060
2061 if (target_resources_ok == 0 && !sw_mode)
2062 error (_("Target does not support this type of "
2063 "hardware watchpoint."));
2064 else if (target_resources_ok < 0 && !sw_mode)
2065 error (_("There are not enough available hardware "
2066 "resources for this watchpoint."));
2067
2068 /* Downgrade to software watchpoint. */
2069 b->type = bp_watchpoint;
2070 }
2071 else
2072 {
2073 /* If this was a software watchpoint, we've just
2074 found we have enough resources to turn it to a
2075 hardware watchpoint. Otherwise, this is a
2076 nop. */
2077 b->type = type;
2078 }
2079 }
2080 else if (!b->ops->works_in_software_mode (b))
2081 {
2082 if (!can_use_hw_watchpoints)
2083 error (_("Can't set read/access watchpoint when "
2084 "hardware watchpoints are disabled."));
2085 else
2086 error (_("Expression cannot be implemented with "
2087 "read/access watchpoint."));
2088 }
2089 else
2090 b->type = bp_watchpoint;
2091
2092 loc_type = (b->type == bp_watchpoint? bp_loc_other
2093 : bp_loc_hardware_watchpoint);
2094 for (bp_location *bl : b->locations ())
2095 bl->loc_type = loc_type;
2096 }
2097
2098 /* If a software watchpoint is not watching any memory, then the
2099 above left it without any location set up. But,
2100 bpstat_stop_status requires a location to be able to report
2101 stops, so make sure there's at least a dummy one. */
2102 if (b->type == bp_watchpoint && b->loc == NULL)
2103 software_watchpoint_add_no_memory_location (b, frame_pspace);
2104 }
2105 else if (!within_current_scope)
2106 {
2107 printf_filtered (_("\
2108 Watchpoint %d deleted because the program has left the block\n\
2109 in which its expression is valid.\n"),
2110 b->number);
2111 watchpoint_del_at_next_stop (b);
2112 }
2113
2114 /* Restore the selected frame. */
2115 if (frame_saved)
2116 select_frame (frame_find_by_id (saved_frame_id));
2117 }
2118
2119
2120 /* Returns 1 iff breakpoint location should be
2121 inserted in the inferior. We don't differentiate the type of BL's owner
2122 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2123 breakpoint_ops is not defined, because in insert_bp_location,
2124 tracepoint's insert_location will not be called. */
2125 static int
2126 should_be_inserted (struct bp_location *bl)
2127 {
2128 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2129 return 0;
2130
2131 if (bl->owner->disposition == disp_del_at_next_stop)
2132 return 0;
2133
2134 if (!bl->enabled || bl->disabled_by_cond
2135 || bl->shlib_disabled || bl->duplicate)
2136 return 0;
2137
2138 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2139 return 0;
2140
2141 /* This is set for example, when we're attached to the parent of a
2142 vfork, and have detached from the child. The child is running
2143 free, and we expect it to do an exec or exit, at which point the
2144 OS makes the parent schedulable again (and the target reports
2145 that the vfork is done). Until the child is done with the shared
2146 memory region, do not insert breakpoints in the parent, otherwise
2147 the child could still trip on the parent's breakpoints. Since
2148 the parent is blocked anyway, it won't miss any breakpoint. */
2149 if (bl->pspace->breakpoints_not_allowed)
2150 return 0;
2151
2152 /* Don't insert a breakpoint if we're trying to step past its
2153 location, except if the breakpoint is a single-step breakpoint,
2154 and the breakpoint's thread is the thread which is stepping past
2155 a breakpoint. */
2156 if ((bl->loc_type == bp_loc_software_breakpoint
2157 || bl->loc_type == bp_loc_hardware_breakpoint)
2158 && stepping_past_instruction_at (bl->pspace->aspace,
2159 bl->address)
2160 /* The single-step breakpoint may be inserted at the location
2161 we're trying to step if the instruction branches to itself.
2162 However, the instruction won't be executed at all and it may
2163 break the semantics of the instruction, for example, the
2164 instruction is a conditional branch or updates some flags.
2165 We can't fix it unless GDB is able to emulate the instruction
2166 or switch to displaced stepping. */
2167 && !(bl->owner->type == bp_single_step
2168 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2169 {
2170 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2171 paddress (bl->gdbarch, bl->address));
2172 return 0;
2173 }
2174
2175 /* Don't insert watchpoints if we're trying to step past the
2176 instruction that triggered one. */
2177 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2178 && stepping_past_nonsteppable_watchpoint ())
2179 {
2180 infrun_debug_printf ("stepping past non-steppable watchpoint. "
2181 "skipping watchpoint at %s:%d",
2182 paddress (bl->gdbarch, bl->address), bl->length);
2183 return 0;
2184 }
2185
2186 return 1;
2187 }
2188
2189 /* Same as should_be_inserted but does the check assuming
2190 that the location is not duplicated. */
2191
2192 static int
2193 unduplicated_should_be_inserted (struct bp_location *bl)
2194 {
2195 int result;
2196 const int save_duplicate = bl->duplicate;
2197
2198 bl->duplicate = 0;
2199 result = should_be_inserted (bl);
2200 bl->duplicate = save_duplicate;
2201 return result;
2202 }
2203
2204 /* Parses a conditional described by an expression COND into an
2205 agent expression bytecode suitable for evaluation
2206 by the bytecode interpreter. Return NULL if there was
2207 any error during parsing. */
2208
2209 static agent_expr_up
2210 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2211 {
2212 if (cond == NULL)
2213 return NULL;
2214
2215 agent_expr_up aexpr;
2216
2217 /* We don't want to stop processing, so catch any errors
2218 that may show up. */
2219 try
2220 {
2221 aexpr = gen_eval_for_expr (scope, cond);
2222 }
2223
2224 catch (const gdb_exception_error &ex)
2225 {
2226 /* If we got here, it means the condition could not be parsed to a valid
2227 bytecode expression and thus can't be evaluated on the target's side.
2228 It's no use iterating through the conditions. */
2229 }
2230
2231 /* We have a valid agent expression. */
2232 return aexpr;
2233 }
2234
2235 /* Based on location BL, create a list of breakpoint conditions to be
2236 passed on to the target. If we have duplicated locations with different
2237 conditions, we will add such conditions to the list. The idea is that the
2238 target will evaluate the list of conditions and will only notify GDB when
2239 one of them is true. */
2240
2241 static void
2242 build_target_condition_list (struct bp_location *bl)
2243 {
2244 int null_condition_or_parse_error = 0;
2245 int modified = bl->needs_update;
2246
2247 /* Release conditions left over from a previous insert. */
2248 bl->target_info.conditions.clear ();
2249
2250 /* This is only meaningful if the target is
2251 evaluating conditions and if the user has
2252 opted for condition evaluation on the target's
2253 side. */
2254 if (gdb_evaluates_breakpoint_condition_p ()
2255 || !target_supports_evaluation_of_breakpoint_conditions ())
2256 return;
2257
2258 auto loc_range = all_bp_locations_at_addr (bl->address);
2259
2260 /* Do a first pass to check for locations with no assigned
2261 conditions or conditions that fail to parse to a valid agent
2262 expression bytecode. If any of these happen, then it's no use to
2263 send conditions to the target since this location will always
2264 trigger and generate a response back to GDB. Note we consider
2265 all locations at the same address irrespective of type, i.e.,
2266 even if the locations aren't considered duplicates (e.g.,
2267 software breakpoint and hardware breakpoint at the same
2268 address). */
2269 for (bp_location *loc : loc_range)
2270 {
2271 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2272 {
2273 if (modified)
2274 {
2275 /* Re-parse the conditions since something changed. In that
2276 case we already freed the condition bytecodes (see
2277 force_breakpoint_reinsertion). We just
2278 need to parse the condition to bytecodes again. */
2279 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2280 loc->cond.get ());
2281 }
2282
2283 /* If we have a NULL bytecode expression, it means something
2284 went wrong or we have a null condition expression. */
2285 if (!loc->cond_bytecode)
2286 {
2287 null_condition_or_parse_error = 1;
2288 break;
2289 }
2290 }
2291 }
2292
2293 /* If any of these happened, it means we will have to evaluate the conditions
2294 for the location's address on gdb's side. It is no use keeping bytecodes
2295 for all the other duplicate locations, thus we free all of them here.
2296
2297 This is so we have a finer control over which locations' conditions are
2298 being evaluated by GDB or the remote stub. */
2299 if (null_condition_or_parse_error)
2300 {
2301 for (bp_location *loc : loc_range)
2302 {
2303 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2304 {
2305 /* Only go as far as the first NULL bytecode is
2306 located. */
2307 if (!loc->cond_bytecode)
2308 return;
2309
2310 loc->cond_bytecode.reset ();
2311 }
2312 }
2313 }
2314
2315 /* No NULL conditions or failed bytecode generation. Build a
2316 condition list for this location's address. If we have software
2317 and hardware locations at the same address, they aren't
2318 considered duplicates, but we still marge all the conditions
2319 anyway, as it's simpler, and doesn't really make a practical
2320 difference. */
2321 for (bp_location *loc : loc_range)
2322 if (loc->cond
2323 && is_breakpoint (loc->owner)
2324 && loc->pspace->num == bl->pspace->num
2325 && loc->owner->enable_state == bp_enabled
2326 && loc->enabled
2327 && !loc->disabled_by_cond)
2328 {
2329 /* Add the condition to the vector. This will be used later
2330 to send the conditions to the target. */
2331 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2332 }
2333
2334 return;
2335 }
2336
2337 /* Parses a command described by string CMD into an agent expression
2338 bytecode suitable for evaluation by the bytecode interpreter.
2339 Return NULL if there was any error during parsing. */
2340
2341 static agent_expr_up
2342 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2343 {
2344 const char *cmdrest;
2345 const char *format_start, *format_end;
2346 struct gdbarch *gdbarch = get_current_arch ();
2347
2348 if (cmd == NULL)
2349 return NULL;
2350
2351 cmdrest = cmd;
2352
2353 if (*cmdrest == ',')
2354 ++cmdrest;
2355 cmdrest = skip_spaces (cmdrest);
2356
2357 if (*cmdrest++ != '"')
2358 error (_("No format string following the location"));
2359
2360 format_start = cmdrest;
2361
2362 format_pieces fpieces (&cmdrest);
2363
2364 format_end = cmdrest;
2365
2366 if (*cmdrest++ != '"')
2367 error (_("Bad format string, non-terminated '\"'."));
2368
2369 cmdrest = skip_spaces (cmdrest);
2370
2371 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2372 error (_("Invalid argument syntax"));
2373
2374 if (*cmdrest == ',')
2375 cmdrest++;
2376 cmdrest = skip_spaces (cmdrest);
2377
2378 /* For each argument, make an expression. */
2379
2380 std::vector<struct expression *> argvec;
2381 while (*cmdrest != '\0')
2382 {
2383 const char *cmd1;
2384
2385 cmd1 = cmdrest;
2386 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2387 argvec.push_back (expr.release ());
2388 cmdrest = cmd1;
2389 if (*cmdrest == ',')
2390 ++cmdrest;
2391 }
2392
2393 agent_expr_up aexpr;
2394
2395 /* We don't want to stop processing, so catch any errors
2396 that may show up. */
2397 try
2398 {
2399 aexpr = gen_printf (scope, gdbarch, 0, 0,
2400 format_start, format_end - format_start,
2401 argvec.size (), argvec.data ());
2402 }
2403 catch (const gdb_exception_error &ex)
2404 {
2405 /* If we got here, it means the command could not be parsed to a valid
2406 bytecode expression and thus can't be evaluated on the target's side.
2407 It's no use iterating through the other commands. */
2408 }
2409
2410 /* We have a valid agent expression, return it. */
2411 return aexpr;
2412 }
2413
2414 /* Based on location BL, create a list of breakpoint commands to be
2415 passed on to the target. If we have duplicated locations with
2416 different commands, we will add any such to the list. */
2417
2418 static void
2419 build_target_command_list (struct bp_location *bl)
2420 {
2421 int null_command_or_parse_error = 0;
2422 int modified = bl->needs_update;
2423
2424 /* Clear commands left over from a previous insert. */
2425 bl->target_info.tcommands.clear ();
2426
2427 if (!target_can_run_breakpoint_commands ())
2428 return;
2429
2430 /* For now, limit to agent-style dprintf breakpoints. */
2431 if (dprintf_style != dprintf_style_agent)
2432 return;
2433
2434 auto loc_range = all_bp_locations_at_addr (bl->address);
2435
2436 /* For now, if we have any location at the same address that isn't a
2437 dprintf, don't install the target-side commands, as that would
2438 make the breakpoint not be reported to the core, and we'd lose
2439 control. */
2440 for (bp_location *loc : loc_range)
2441 if (is_breakpoint (loc->owner)
2442 && loc->pspace->num == bl->pspace->num
2443 && loc->owner->type != bp_dprintf)
2444 return;
2445
2446 /* Do a first pass to check for locations with no assigned
2447 conditions or conditions that fail to parse to a valid agent expression
2448 bytecode. If any of these happen, then it's no use to send conditions
2449 to the target since this location will always trigger and generate a
2450 response back to GDB. */
2451 for (bp_location *loc : loc_range)
2452 {
2453 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2454 {
2455 if (modified)
2456 {
2457 /* Re-parse the commands since something changed. In that
2458 case we already freed the command bytecodes (see
2459 force_breakpoint_reinsertion). We just
2460 need to parse the command to bytecodes again. */
2461 loc->cmd_bytecode
2462 = parse_cmd_to_aexpr (bl->address,
2463 loc->owner->extra_string);
2464 }
2465
2466 /* If we have a NULL bytecode expression, it means something
2467 went wrong or we have a null command expression. */
2468 if (!loc->cmd_bytecode)
2469 {
2470 null_command_or_parse_error = 1;
2471 break;
2472 }
2473 }
2474 }
2475
2476 /* If anything failed, then we're not doing target-side commands,
2477 and so clean up. */
2478 if (null_command_or_parse_error)
2479 {
2480 for (bp_location *loc : loc_range)
2481 if (is_breakpoint (loc->owner)
2482 && loc->pspace->num == bl->pspace->num)
2483 {
2484 /* Only go as far as the first NULL bytecode is
2485 located. */
2486 if (loc->cmd_bytecode == NULL)
2487 return;
2488
2489 loc->cmd_bytecode.reset ();
2490 }
2491 }
2492
2493 /* No NULL commands or failed bytecode generation. Build a command
2494 list for all duplicate locations at this location's address.
2495 Note that here we must care for whether the breakpoint location
2496 types are considered duplicates, otherwise, say, if we have a
2497 software and hardware location at the same address, the target
2498 could end up running the commands twice. For the moment, we only
2499 support targets-side commands with dprintf, but it doesn't hurt
2500 to be pedantically correct in case that changes. */
2501 for (bp_location *loc : loc_range)
2502 if (breakpoint_locations_match (bl, loc)
2503 && loc->owner->extra_string
2504 && is_breakpoint (loc->owner)
2505 && loc->pspace->num == bl->pspace->num
2506 && loc->owner->enable_state == bp_enabled
2507 && loc->enabled
2508 && !loc->disabled_by_cond)
2509 {
2510 /* Add the command to the vector. This will be used later
2511 to send the commands to the target. */
2512 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2513 }
2514
2515 bl->target_info.persist = 0;
2516 /* Maybe flag this location as persistent. */
2517 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2518 bl->target_info.persist = 1;
2519 }
2520
2521 /* Return the kind of breakpoint on address *ADDR. Get the kind
2522 of breakpoint according to ADDR except single-step breakpoint.
2523 Get the kind of single-step breakpoint according to the current
2524 registers state. */
2525
2526 static int
2527 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2528 {
2529 if (bl->owner->type == bp_single_step)
2530 {
2531 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2532 struct regcache *regcache;
2533
2534 regcache = get_thread_regcache (thr);
2535
2536 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2537 regcache, addr);
2538 }
2539 else
2540 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2541 }
2542
2543 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2544 location. Any error messages are printed to TMP_ERROR_STREAM; and
2545 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2546 Returns 0 for success, 1 if the bp_location type is not supported or
2547 -1 for failure.
2548
2549 NOTE drow/2003-09-09: This routine could be broken down to an
2550 object-style method for each breakpoint or catchpoint type. */
2551 static int
2552 insert_bp_location (struct bp_location *bl,
2553 struct ui_file *tmp_error_stream,
2554 int *disabled_breaks,
2555 int *hw_breakpoint_error,
2556 int *hw_bp_error_explained_already)
2557 {
2558 gdb_exception bp_excpt;
2559
2560 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2561 return 0;
2562
2563 /* Note we don't initialize bl->target_info, as that wipes out
2564 the breakpoint location's shadow_contents if the breakpoint
2565 is still inserted at that location. This in turn breaks
2566 target_read_memory which depends on these buffers when
2567 a memory read is requested at the breakpoint location:
2568 Once the target_info has been wiped, we fail to see that
2569 we have a breakpoint inserted at that address and thus
2570 read the breakpoint instead of returning the data saved in
2571 the breakpoint location's shadow contents. */
2572 bl->target_info.reqstd_address = bl->address;
2573 bl->target_info.placed_address_space = bl->pspace->aspace;
2574 bl->target_info.length = bl->length;
2575
2576 /* When working with target-side conditions, we must pass all the conditions
2577 for the same breakpoint address down to the target since GDB will not
2578 insert those locations. With a list of breakpoint conditions, the target
2579 can decide when to stop and notify GDB. */
2580
2581 if (is_breakpoint (bl->owner))
2582 {
2583 build_target_condition_list (bl);
2584 build_target_command_list (bl);
2585 /* Reset the modification marker. */
2586 bl->needs_update = 0;
2587 }
2588
2589 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2590 set at a read-only address, then a breakpoint location will have
2591 been changed to hardware breakpoint before we get here. If it is
2592 "off" however, error out before actually trying to insert the
2593 breakpoint, with a nicer error message. */
2594 if (bl->loc_type == bp_loc_software_breakpoint
2595 && !automatic_hardware_breakpoints)
2596 {
2597 mem_region *mr = lookup_mem_region (bl->address);
2598
2599 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2600 {
2601 fprintf_unfiltered (tmp_error_stream,
2602 _("Cannot insert breakpoint %d.\n"
2603 "Cannot set software breakpoint "
2604 "at read-only address %s\n"),
2605 bl->owner->number,
2606 paddress (bl->gdbarch, bl->address));
2607 return 1;
2608 }
2609 }
2610
2611 if (bl->loc_type == bp_loc_software_breakpoint
2612 || bl->loc_type == bp_loc_hardware_breakpoint)
2613 {
2614 /* First check to see if we have to handle an overlay. */
2615 if (overlay_debugging == ovly_off
2616 || bl->section == NULL
2617 || !(section_is_overlay (bl->section)))
2618 {
2619 /* No overlay handling: just set the breakpoint. */
2620 try
2621 {
2622 int val;
2623
2624 val = bl->owner->ops->insert_location (bl);
2625 if (val)
2626 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2627 }
2628 catch (gdb_exception &e)
2629 {
2630 bp_excpt = std::move (e);
2631 }
2632 }
2633 else
2634 {
2635 /* This breakpoint is in an overlay section.
2636 Shall we set a breakpoint at the LMA? */
2637 if (!overlay_events_enabled)
2638 {
2639 /* Yes -- overlay event support is not active,
2640 so we must try to set a breakpoint at the LMA.
2641 This will not work for a hardware breakpoint. */
2642 if (bl->loc_type == bp_loc_hardware_breakpoint)
2643 warning (_("hardware breakpoint %d not supported in overlay!"),
2644 bl->owner->number);
2645 else
2646 {
2647 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2648 bl->section);
2649 /* Set a software (trap) breakpoint at the LMA. */
2650 bl->overlay_target_info = bl->target_info;
2651 bl->overlay_target_info.reqstd_address = addr;
2652
2653 /* No overlay handling: just set the breakpoint. */
2654 try
2655 {
2656 int val;
2657
2658 bl->overlay_target_info.kind
2659 = breakpoint_kind (bl, &addr);
2660 bl->overlay_target_info.placed_address = addr;
2661 val = target_insert_breakpoint (bl->gdbarch,
2662 &bl->overlay_target_info);
2663 if (val)
2664 bp_excpt
2665 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2666 }
2667 catch (gdb_exception &e)
2668 {
2669 bp_excpt = std::move (e);
2670 }
2671
2672 if (bp_excpt.reason != 0)
2673 fprintf_unfiltered (tmp_error_stream,
2674 "Overlay breakpoint %d "
2675 "failed: in ROM?\n",
2676 bl->owner->number);
2677 }
2678 }
2679 /* Shall we set a breakpoint at the VMA? */
2680 if (section_is_mapped (bl->section))
2681 {
2682 /* Yes. This overlay section is mapped into memory. */
2683 try
2684 {
2685 int val;
2686
2687 val = bl->owner->ops->insert_location (bl);
2688 if (val)
2689 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2690 }
2691 catch (gdb_exception &e)
2692 {
2693 bp_excpt = std::move (e);
2694 }
2695 }
2696 else
2697 {
2698 /* No. This breakpoint will not be inserted.
2699 No error, but do not mark the bp as 'inserted'. */
2700 return 0;
2701 }
2702 }
2703
2704 if (bp_excpt.reason != 0)
2705 {
2706 /* Can't set the breakpoint. */
2707
2708 /* In some cases, we might not be able to insert a
2709 breakpoint in a shared library that has already been
2710 removed, but we have not yet processed the shlib unload
2711 event. Unfortunately, some targets that implement
2712 breakpoint insertion themselves can't tell why the
2713 breakpoint insertion failed (e.g., the remote target
2714 doesn't define error codes), so we must treat generic
2715 errors as memory errors. */
2716 if (bp_excpt.reason == RETURN_ERROR
2717 && (bp_excpt.error == GENERIC_ERROR
2718 || bp_excpt.error == MEMORY_ERROR)
2719 && bl->loc_type == bp_loc_software_breakpoint
2720 && (solib_name_from_address (bl->pspace, bl->address)
2721 || shared_objfile_contains_address_p (bl->pspace,
2722 bl->address)))
2723 {
2724 /* See also: disable_breakpoints_in_shlibs. */
2725 bl->shlib_disabled = 1;
2726 gdb::observers::breakpoint_modified.notify (bl->owner);
2727 if (!*disabled_breaks)
2728 {
2729 fprintf_unfiltered (tmp_error_stream,
2730 "Cannot insert breakpoint %d.\n",
2731 bl->owner->number);
2732 fprintf_unfiltered (tmp_error_stream,
2733 "Temporarily disabling shared "
2734 "library breakpoints:\n");
2735 }
2736 *disabled_breaks = 1;
2737 fprintf_unfiltered (tmp_error_stream,
2738 "breakpoint #%d\n", bl->owner->number);
2739 return 0;
2740 }
2741 else
2742 {
2743 if (bl->loc_type == bp_loc_hardware_breakpoint)
2744 {
2745 *hw_breakpoint_error = 1;
2746 *hw_bp_error_explained_already = bp_excpt.message != NULL;
2747 fprintf_unfiltered (tmp_error_stream,
2748 "Cannot insert hardware breakpoint %d%s",
2749 bl->owner->number,
2750 bp_excpt.message ? ":" : ".\n");
2751 if (bp_excpt.message != NULL)
2752 fprintf_unfiltered (tmp_error_stream, "%s.\n",
2753 bp_excpt.what ());
2754 }
2755 else
2756 {
2757 if (bp_excpt.message == NULL)
2758 {
2759 std::string message
2760 = memory_error_message (TARGET_XFER_E_IO,
2761 bl->gdbarch, bl->address);
2762
2763 fprintf_unfiltered (tmp_error_stream,
2764 "Cannot insert breakpoint %d.\n"
2765 "%s\n",
2766 bl->owner->number, message.c_str ());
2767 }
2768 else
2769 {
2770 fprintf_unfiltered (tmp_error_stream,
2771 "Cannot insert breakpoint %d: %s\n",
2772 bl->owner->number,
2773 bp_excpt.what ());
2774 }
2775 }
2776 return 1;
2777
2778 }
2779 }
2780 else
2781 bl->inserted = 1;
2782
2783 return 0;
2784 }
2785
2786 else if (bl->loc_type == bp_loc_hardware_watchpoint
2787 /* NOTE drow/2003-09-08: This state only exists for removing
2788 watchpoints. It's not clear that it's necessary... */
2789 && bl->owner->disposition != disp_del_at_next_stop)
2790 {
2791 int val;
2792
2793 gdb_assert (bl->owner->ops != NULL
2794 && bl->owner->ops->insert_location != NULL);
2795
2796 val = bl->owner->ops->insert_location (bl);
2797
2798 /* If trying to set a read-watchpoint, and it turns out it's not
2799 supported, try emulating one with an access watchpoint. */
2800 if (val == 1 && bl->watchpoint_type == hw_read)
2801 {
2802 /* But don't try to insert it, if there's already another
2803 hw_access location that would be considered a duplicate
2804 of this one. */
2805 for (bp_location *loc : all_bp_locations ())
2806 if (loc != bl
2807 && loc->watchpoint_type == hw_access
2808 && watchpoint_locations_match (bl, loc))
2809 {
2810 bl->duplicate = 1;
2811 bl->inserted = 1;
2812 bl->target_info = loc->target_info;
2813 bl->watchpoint_type = hw_access;
2814 val = 0;
2815 break;
2816 }
2817
2818 if (val == 1)
2819 {
2820 bl->watchpoint_type = hw_access;
2821 val = bl->owner->ops->insert_location (bl);
2822
2823 if (val)
2824 /* Back to the original value. */
2825 bl->watchpoint_type = hw_read;
2826 }
2827 }
2828
2829 bl->inserted = (val == 0);
2830 }
2831
2832 else if (bl->owner->type == bp_catchpoint)
2833 {
2834 int val;
2835
2836 gdb_assert (bl->owner->ops != NULL
2837 && bl->owner->ops->insert_location != NULL);
2838
2839 val = bl->owner->ops->insert_location (bl);
2840 if (val)
2841 {
2842 bl->owner->enable_state = bp_disabled;
2843
2844 if (val == 1)
2845 warning (_("\
2846 Error inserting catchpoint %d: Your system does not support this type\n\
2847 of catchpoint."), bl->owner->number);
2848 else
2849 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2850 }
2851
2852 bl->inserted = (val == 0);
2853
2854 /* We've already printed an error message if there was a problem
2855 inserting this catchpoint, and we've disabled the catchpoint,
2856 so just return success. */
2857 return 0;
2858 }
2859
2860 return 0;
2861 }
2862
2863 /* This function is called when program space PSPACE is about to be
2864 deleted. It takes care of updating breakpoints to not reference
2865 PSPACE anymore. */
2866
2867 void
2868 breakpoint_program_space_exit (struct program_space *pspace)
2869 {
2870 /* Remove any breakpoint that was set through this program space. */
2871 for (breakpoint *b : all_breakpoints_safe ())
2872 if (b->pspace == pspace)
2873 delete_breakpoint (b);
2874
2875 /* Breakpoints set through other program spaces could have locations
2876 bound to PSPACE as well. Remove those. */
2877 for (bp_location *loc : all_bp_locations ())
2878 {
2879 struct bp_location *tmp;
2880
2881 if (loc->pspace == pspace)
2882 {
2883 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2884 if (loc->owner->loc == loc)
2885 loc->owner->loc = loc->next;
2886 else
2887 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2888 if (tmp->next == loc)
2889 {
2890 tmp->next = loc->next;
2891 break;
2892 }
2893 }
2894 }
2895
2896 /* Now update the global location list to permanently delete the
2897 removed locations above. */
2898 update_global_location_list (UGLL_DONT_INSERT);
2899 }
2900
2901 /* Make sure all breakpoints are inserted in inferior.
2902 Throws exception on any error.
2903 A breakpoint that is already inserted won't be inserted
2904 again, so calling this function twice is safe. */
2905 void
2906 insert_breakpoints (void)
2907 {
2908 for (breakpoint *bpt : all_breakpoints ())
2909 if (is_hardware_watchpoint (bpt))
2910 {
2911 struct watchpoint *w = (struct watchpoint *) bpt;
2912
2913 update_watchpoint (w, 0 /* don't reparse. */);
2914 }
2915
2916 /* Updating watchpoints creates new locations, so update the global
2917 location list. Explicitly tell ugll to insert locations and
2918 ignore breakpoints_always_inserted_mode. Also,
2919 update_global_location_list tries to "upgrade" software
2920 breakpoints to hardware breakpoints to handle "set breakpoint
2921 auto-hw", so we need to call it even if we don't have new
2922 locations. */
2923 update_global_location_list (UGLL_INSERT);
2924 }
2925
2926 /* Invoke CALLBACK for each of bp_location. */
2927
2928 void
2929 iterate_over_bp_locations (gdb::function_view<void (bp_location *)> callback)
2930 {
2931 for (bp_location *loc : all_bp_locations ())
2932 callback (loc);
2933 }
2934
2935 /* This is used when we need to synch breakpoint conditions between GDB and the
2936 target. It is the case with deleting and disabling of breakpoints when using
2937 always-inserted mode. */
2938
2939 static void
2940 update_inserted_breakpoint_locations (void)
2941 {
2942 int error_flag = 0;
2943 int val = 0;
2944 int disabled_breaks = 0;
2945 int hw_breakpoint_error = 0;
2946 int hw_bp_details_reported = 0;
2947
2948 string_file tmp_error_stream;
2949
2950 /* Explicitly mark the warning -- this will only be printed if
2951 there was an error. */
2952 tmp_error_stream.puts ("Warning:\n");
2953
2954 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2955
2956 for (bp_location *bl : all_bp_locations ())
2957 {
2958 /* We only want to update software breakpoints and hardware
2959 breakpoints. */
2960 if (!is_breakpoint (bl->owner))
2961 continue;
2962
2963 /* We only want to update locations that are already inserted
2964 and need updating. This is to avoid unwanted insertion during
2965 deletion of breakpoints. */
2966 if (!bl->inserted || !bl->needs_update)
2967 continue;
2968
2969 switch_to_program_space_and_thread (bl->pspace);
2970
2971 /* For targets that support global breakpoints, there's no need
2972 to select an inferior to insert breakpoint to. In fact, even
2973 if we aren't attached to any process yet, we should still
2974 insert breakpoints. */
2975 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2976 && (inferior_ptid == null_ptid || !target_has_execution ()))
2977 continue;
2978
2979 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
2980 &hw_breakpoint_error, &hw_bp_details_reported);
2981 if (val)
2982 error_flag = val;
2983 }
2984
2985 if (error_flag)
2986 {
2987 target_terminal::ours_for_output ();
2988 error_stream (tmp_error_stream);
2989 }
2990 }
2991
2992 /* Used when starting or continuing the program. */
2993
2994 static void
2995 insert_breakpoint_locations (void)
2996 {
2997 int error_flag = 0;
2998 int val = 0;
2999 int disabled_breaks = 0;
3000 int hw_breakpoint_error = 0;
3001 int hw_bp_error_explained_already = 0;
3002
3003 string_file tmp_error_stream;
3004
3005 /* Explicitly mark the warning -- this will only be printed if
3006 there was an error. */
3007 tmp_error_stream.puts ("Warning:\n");
3008
3009 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3010
3011 for (bp_location *bl : all_bp_locations ())
3012 {
3013 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3014 continue;
3015
3016 /* There is no point inserting thread-specific breakpoints if
3017 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3018 has BL->OWNER always non-NULL. */
3019 if (bl->owner->thread != -1
3020 && !valid_global_thread_id (bl->owner->thread))
3021 continue;
3022
3023 switch_to_program_space_and_thread (bl->pspace);
3024
3025 /* For targets that support global breakpoints, there's no need
3026 to select an inferior to insert breakpoint to. In fact, even
3027 if we aren't attached to any process yet, we should still
3028 insert breakpoints. */
3029 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3030 && (inferior_ptid == null_ptid || !target_has_execution ()))
3031 continue;
3032
3033 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3034 &hw_breakpoint_error, &hw_bp_error_explained_already);
3035 if (val)
3036 error_flag = val;
3037 }
3038
3039 /* If we failed to insert all locations of a watchpoint, remove
3040 them, as half-inserted watchpoint is of limited use. */
3041 for (breakpoint *bpt : all_breakpoints ())
3042 {
3043 int some_failed = 0;
3044
3045 if (!is_hardware_watchpoint (bpt))
3046 continue;
3047
3048 if (!breakpoint_enabled (bpt))
3049 continue;
3050
3051 if (bpt->disposition == disp_del_at_next_stop)
3052 continue;
3053
3054 for (bp_location *loc : bpt->locations ())
3055 if (!loc->inserted && should_be_inserted (loc))
3056 {
3057 some_failed = 1;
3058 break;
3059 }
3060
3061 if (some_failed)
3062 {
3063 for (bp_location *loc : bpt->locations ())
3064 if (loc->inserted)
3065 remove_breakpoint (loc);
3066
3067 hw_breakpoint_error = 1;
3068 tmp_error_stream.printf ("Could not insert "
3069 "hardware watchpoint %d.\n",
3070 bpt->number);
3071 error_flag = -1;
3072 }
3073 }
3074
3075 if (error_flag)
3076 {
3077 /* If a hardware breakpoint or watchpoint was inserted, add a
3078 message about possibly exhausted resources. */
3079 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3080 {
3081 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3082 You may have requested too many hardware breakpoints/watchpoints.\n");
3083 }
3084 target_terminal::ours_for_output ();
3085 error_stream (tmp_error_stream);
3086 }
3087 }
3088
3089 /* Used when the program stops.
3090 Returns zero if successful, or non-zero if there was a problem
3091 removing a breakpoint location. */
3092
3093 int
3094 remove_breakpoints (void)
3095 {
3096 int val = 0;
3097
3098 for (bp_location *bl : all_bp_locations ())
3099 {
3100 if (bl->inserted && !is_tracepoint (bl->owner))
3101 val |= remove_breakpoint (bl);
3102 }
3103 return val;
3104 }
3105
3106 /* When a thread exits, remove breakpoints that are related to
3107 that thread. */
3108
3109 static void
3110 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3111 {
3112 for (breakpoint *b : all_breakpoints_safe ())
3113 {
3114 if (b->thread == tp->global_num && user_breakpoint_p (b))
3115 {
3116 b->disposition = disp_del_at_next_stop;
3117
3118 printf_filtered (_("\
3119 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3120 b->number, print_thread_id (tp));
3121
3122 /* Hide it from the user. */
3123 b->number = 0;
3124 }
3125 }
3126 }
3127
3128 /* See breakpoint.h. */
3129
3130 void
3131 remove_breakpoints_inf (inferior *inf)
3132 {
3133 int val;
3134
3135 for (bp_location *bl : all_bp_locations ())
3136 {
3137 if (bl->pspace != inf->pspace)
3138 continue;
3139
3140 if (bl->inserted && !bl->target_info.persist)
3141 {
3142 val = remove_breakpoint (bl);
3143 if (val != 0)
3144 return;
3145 }
3146 }
3147 }
3148
3149 static int internal_breakpoint_number = -1;
3150
3151 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3152 If INTERNAL is non-zero, the breakpoint number will be populated
3153 from internal_breakpoint_number and that variable decremented.
3154 Otherwise the breakpoint number will be populated from
3155 breakpoint_count and that value incremented. Internal breakpoints
3156 do not set the internal var bpnum. */
3157 static void
3158 set_breakpoint_number (int internal, struct breakpoint *b)
3159 {
3160 if (internal)
3161 b->number = internal_breakpoint_number--;
3162 else
3163 {
3164 set_breakpoint_count (breakpoint_count + 1);
3165 b->number = breakpoint_count;
3166 }
3167 }
3168
3169 static struct breakpoint *
3170 create_internal_breakpoint (struct gdbarch *gdbarch,
3171 CORE_ADDR address, enum bptype type,
3172 const struct breakpoint_ops *ops)
3173 {
3174 symtab_and_line sal;
3175 sal.pc = address;
3176 sal.section = find_pc_overlay (sal.pc);
3177 sal.pspace = current_program_space;
3178
3179 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
3180 b->number = internal_breakpoint_number--;
3181 b->disposition = disp_donttouch;
3182
3183 return b;
3184 }
3185
3186 static const char *const longjmp_names[] =
3187 {
3188 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3189 };
3190 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3191
3192 /* Per-objfile data private to breakpoint.c. */
3193 struct breakpoint_objfile_data
3194 {
3195 /* Minimal symbol for "_ovly_debug_event" (if any). */
3196 struct bound_minimal_symbol overlay_msym {};
3197
3198 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3199 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
3200
3201 /* True if we have looked for longjmp probes. */
3202 int longjmp_searched = 0;
3203
3204 /* SystemTap probe points for longjmp (if any). These are non-owning
3205 references. */
3206 std::vector<probe *> longjmp_probes;
3207
3208 /* Minimal symbol for "std::terminate()" (if any). */
3209 struct bound_minimal_symbol terminate_msym {};
3210
3211 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3212 struct bound_minimal_symbol exception_msym {};
3213
3214 /* True if we have looked for exception probes. */
3215 int exception_searched = 0;
3216
3217 /* SystemTap probe points for unwinding (if any). These are non-owning
3218 references. */
3219 std::vector<probe *> exception_probes;
3220 };
3221
3222 static const struct objfile_key<breakpoint_objfile_data>
3223 breakpoint_objfile_key;
3224
3225 /* Minimal symbol not found sentinel. */
3226 static struct minimal_symbol msym_not_found;
3227
3228 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3229
3230 static int
3231 msym_not_found_p (const struct minimal_symbol *msym)
3232 {
3233 return msym == &msym_not_found;
3234 }
3235
3236 /* Return per-objfile data needed by breakpoint.c.
3237 Allocate the data if necessary. */
3238
3239 static struct breakpoint_objfile_data *
3240 get_breakpoint_objfile_data (struct objfile *objfile)
3241 {
3242 struct breakpoint_objfile_data *bp_objfile_data;
3243
3244 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3245 if (bp_objfile_data == NULL)
3246 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3247 return bp_objfile_data;
3248 }
3249
3250 static void
3251 create_overlay_event_breakpoint (void)
3252 {
3253 const char *const func_name = "_ovly_debug_event";
3254
3255 for (objfile *objfile : current_program_space->objfiles ())
3256 {
3257 struct breakpoint *b;
3258 struct breakpoint_objfile_data *bp_objfile_data;
3259 CORE_ADDR addr;
3260 struct explicit_location explicit_loc;
3261
3262 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3263
3264 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3265 continue;
3266
3267 if (bp_objfile_data->overlay_msym.minsym == NULL)
3268 {
3269 struct bound_minimal_symbol m;
3270
3271 m = lookup_minimal_symbol_text (func_name, objfile);
3272 if (m.minsym == NULL)
3273 {
3274 /* Avoid future lookups in this objfile. */
3275 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3276 continue;
3277 }
3278 bp_objfile_data->overlay_msym = m;
3279 }
3280
3281 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3282 b = create_internal_breakpoint (objfile->arch (), addr,
3283 bp_overlay_event,
3284 &internal_breakpoint_ops);
3285 initialize_explicit_location (&explicit_loc);
3286 explicit_loc.function_name = ASTRDUP (func_name);
3287 b->location = new_explicit_location (&explicit_loc);
3288
3289 if (overlay_debugging == ovly_auto)
3290 {
3291 b->enable_state = bp_enabled;
3292 overlay_events_enabled = 1;
3293 }
3294 else
3295 {
3296 b->enable_state = bp_disabled;
3297 overlay_events_enabled = 0;
3298 }
3299 }
3300 }
3301
3302 /* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3303 true if a breakpoint was installed. */
3304
3305 static bool
3306 create_longjmp_master_breakpoint_probe (objfile *objfile)
3307 {
3308 struct gdbarch *gdbarch = objfile->arch ();
3309 struct breakpoint_objfile_data *bp_objfile_data
3310 = get_breakpoint_objfile_data (objfile);
3311
3312 if (!bp_objfile_data->longjmp_searched)
3313 {
3314 std::vector<probe *> ret
3315 = find_probes_in_objfile (objfile, "libc", "longjmp");
3316
3317 if (!ret.empty ())
3318 {
3319 /* We are only interested in checking one element. */
3320 probe *p = ret[0];
3321
3322 if (!p->can_evaluate_arguments ())
3323 {
3324 /* We cannot use the probe interface here,
3325 because it does not know how to evaluate
3326 arguments. */
3327 ret.clear ();
3328 }
3329 }
3330 bp_objfile_data->longjmp_probes = ret;
3331 bp_objfile_data->longjmp_searched = 1;
3332 }
3333
3334 if (bp_objfile_data->longjmp_probes.empty ())
3335 return false;
3336
3337 for (probe *p : bp_objfile_data->longjmp_probes)
3338 {
3339 struct breakpoint *b;
3340
3341 b = create_internal_breakpoint (gdbarch,
3342 p->get_relocated_address (objfile),
3343 bp_longjmp_master,
3344 &internal_breakpoint_ops);
3345 b->location = new_probe_location ("-probe-stap libc:longjmp");
3346 b->enable_state = bp_disabled;
3347 }
3348
3349 return true;
3350 }
3351
3352 /* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3353 Return true if at least one breakpoint was installed. */
3354
3355 static bool
3356 create_longjmp_master_breakpoint_names (objfile *objfile)
3357 {
3358 struct gdbarch *gdbarch = objfile->arch ();
3359 if (!gdbarch_get_longjmp_target_p (gdbarch))
3360 return false;
3361
3362 struct breakpoint_objfile_data *bp_objfile_data
3363 = get_breakpoint_objfile_data (objfile);
3364 unsigned int installed_bp = 0;
3365
3366 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3367 {
3368 struct breakpoint *b;
3369 const char *func_name;
3370 CORE_ADDR addr;
3371 struct explicit_location explicit_loc;
3372
3373 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3374 continue;
3375
3376 func_name = longjmp_names[i];
3377 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3378 {
3379 struct bound_minimal_symbol m;
3380
3381 m = lookup_minimal_symbol_text (func_name, objfile);
3382 if (m.minsym == NULL)
3383 {
3384 /* Prevent future lookups in this objfile. */
3385 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3386 continue;
3387 }
3388 bp_objfile_data->longjmp_msym[i] = m;
3389 }
3390
3391 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3392 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3393 &internal_breakpoint_ops);
3394 initialize_explicit_location (&explicit_loc);
3395 explicit_loc.function_name = ASTRDUP (func_name);
3396 b->location = new_explicit_location (&explicit_loc);
3397 b->enable_state = bp_disabled;
3398 installed_bp++;
3399 }
3400
3401 return installed_bp > 0;
3402 }
3403
3404 /* Create a master longjmp breakpoint. */
3405
3406 static void
3407 create_longjmp_master_breakpoint (void)
3408 {
3409 scoped_restore_current_program_space restore_pspace;
3410
3411 for (struct program_space *pspace : program_spaces)
3412 {
3413 set_current_program_space (pspace);
3414
3415 for (objfile *obj : current_program_space->objfiles ())
3416 {
3417 /* Skip separate debug object, it's handled in the loop below. */
3418 if (obj->separate_debug_objfile_backlink != nullptr)
3419 continue;
3420
3421 /* Try a probe kind breakpoint on main objfile. */
3422 if (create_longjmp_master_breakpoint_probe (obj))
3423 continue;
3424
3425 /* Try longjmp_names kind breakpoints on main and separate_debug
3426 objfiles. */
3427 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3428 if (create_longjmp_master_breakpoint_names (debug_objfile))
3429 break;
3430 }
3431 }
3432 }
3433
3434 /* Create a master std::terminate breakpoint. */
3435 static void
3436 create_std_terminate_master_breakpoint (void)
3437 {
3438 const char *const func_name = "std::terminate()";
3439
3440 scoped_restore_current_program_space restore_pspace;
3441
3442 for (struct program_space *pspace : program_spaces)
3443 {
3444 CORE_ADDR addr;
3445
3446 set_current_program_space (pspace);
3447
3448 for (objfile *objfile : current_program_space->objfiles ())
3449 {
3450 struct breakpoint *b;
3451 struct breakpoint_objfile_data *bp_objfile_data;
3452 struct explicit_location explicit_loc;
3453
3454 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3455
3456 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3457 continue;
3458
3459 if (bp_objfile_data->terminate_msym.minsym == NULL)
3460 {
3461 struct bound_minimal_symbol m;
3462
3463 m = lookup_minimal_symbol (func_name, NULL, objfile);
3464 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3465 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3466 {
3467 /* Prevent future lookups in this objfile. */
3468 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3469 continue;
3470 }
3471 bp_objfile_data->terminate_msym = m;
3472 }
3473
3474 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3475 b = create_internal_breakpoint (objfile->arch (), addr,
3476 bp_std_terminate_master,
3477 &internal_breakpoint_ops);
3478 initialize_explicit_location (&explicit_loc);
3479 explicit_loc.function_name = ASTRDUP (func_name);
3480 b->location = new_explicit_location (&explicit_loc);
3481 b->enable_state = bp_disabled;
3482 }
3483 }
3484 }
3485
3486 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3487 probe. Return true if a breakpoint was installed. */
3488
3489 static bool
3490 create_exception_master_breakpoint_probe (objfile *objfile)
3491 {
3492 struct breakpoint *b;
3493 struct gdbarch *gdbarch;
3494 struct breakpoint_objfile_data *bp_objfile_data;
3495
3496 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3497
3498 /* We prefer the SystemTap probe point if it exists. */
3499 if (!bp_objfile_data->exception_searched)
3500 {
3501 std::vector<probe *> ret
3502 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3503
3504 if (!ret.empty ())
3505 {
3506 /* We are only interested in checking one element. */
3507 probe *p = ret[0];
3508
3509 if (!p->can_evaluate_arguments ())
3510 {
3511 /* We cannot use the probe interface here, because it does
3512 not know how to evaluate arguments. */
3513 ret.clear ();
3514 }
3515 }
3516 bp_objfile_data->exception_probes = ret;
3517 bp_objfile_data->exception_searched = 1;
3518 }
3519
3520 if (bp_objfile_data->exception_probes.empty ())
3521 return false;
3522
3523 gdbarch = objfile->arch ();
3524
3525 for (probe *p : bp_objfile_data->exception_probes)
3526 {
3527 b = create_internal_breakpoint (gdbarch,
3528 p->get_relocated_address (objfile),
3529 bp_exception_master,
3530 &internal_breakpoint_ops);
3531 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3532 b->enable_state = bp_disabled;
3533 }
3534
3535 return true;
3536 }
3537
3538 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3539 _Unwind_DebugHook. Return true if a breakpoint was installed. */
3540
3541 static bool
3542 create_exception_master_breakpoint_hook (objfile *objfile)
3543 {
3544 const char *const func_name = "_Unwind_DebugHook";
3545 struct breakpoint *b;
3546 struct gdbarch *gdbarch;
3547 struct breakpoint_objfile_data *bp_objfile_data;
3548 CORE_ADDR addr;
3549 struct explicit_location explicit_loc;
3550
3551 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3552
3553 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3554 return false;
3555
3556 gdbarch = objfile->arch ();
3557
3558 if (bp_objfile_data->exception_msym.minsym == NULL)
3559 {
3560 struct bound_minimal_symbol debug_hook;
3561
3562 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3563 if (debug_hook.minsym == NULL)
3564 {
3565 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3566 return false;
3567 }
3568
3569 bp_objfile_data->exception_msym = debug_hook;
3570 }
3571
3572 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3573 addr = gdbarch_convert_from_func_ptr_addr
3574 (gdbarch, addr, current_inferior ()->top_target ());
3575 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3576 &internal_breakpoint_ops);
3577 initialize_explicit_location (&explicit_loc);
3578 explicit_loc.function_name = ASTRDUP (func_name);
3579 b->location = new_explicit_location (&explicit_loc);
3580 b->enable_state = bp_disabled;
3581
3582 return true;
3583 }
3584
3585 /* Install a master breakpoint on the unwinder's debug hook. */
3586
3587 static void
3588 create_exception_master_breakpoint (void)
3589 {
3590 for (objfile *obj : current_program_space->objfiles ())
3591 {
3592 /* Skip separate debug object. */
3593 if (obj->separate_debug_objfile_backlink)
3594 continue;
3595
3596 /* Try a probe kind breakpoint. */
3597 if (create_exception_master_breakpoint_probe (obj))
3598 continue;
3599
3600 /* Iterate over main and separate debug objects and try an
3601 _Unwind_DebugHook kind breakpoint. */
3602 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3603 if (create_exception_master_breakpoint_hook (debug_objfile))
3604 break;
3605 }
3606 }
3607
3608 /* Does B have a location spec? */
3609
3610 static int
3611 breakpoint_event_location_empty_p (const struct breakpoint *b)
3612 {
3613 return b->location != NULL && event_location_empty_p (b->location.get ());
3614 }
3615
3616 void
3617 update_breakpoints_after_exec (void)
3618 {
3619 /* We're about to delete breakpoints from GDB's lists. If the
3620 INSERTED flag is true, GDB will try to lift the breakpoints by
3621 writing the breakpoints' "shadow contents" back into memory. The
3622 "shadow contents" are NOT valid after an exec, so GDB should not
3623 do that. Instead, the target is responsible from marking
3624 breakpoints out as soon as it detects an exec. We don't do that
3625 here instead, because there may be other attempts to delete
3626 breakpoints after detecting an exec and before reaching here. */
3627 for (bp_location *bploc : all_bp_locations ())
3628 if (bploc->pspace == current_program_space)
3629 gdb_assert (!bploc->inserted);
3630
3631 for (breakpoint *b : all_breakpoints_safe ())
3632 {
3633 if (b->pspace != current_program_space)
3634 continue;
3635
3636 /* Solib breakpoints must be explicitly reset after an exec(). */
3637 if (b->type == bp_shlib_event)
3638 {
3639 delete_breakpoint (b);
3640 continue;
3641 }
3642
3643 /* JIT breakpoints must be explicitly reset after an exec(). */
3644 if (b->type == bp_jit_event)
3645 {
3646 delete_breakpoint (b);
3647 continue;
3648 }
3649
3650 /* Thread event breakpoints must be set anew after an exec(),
3651 as must overlay event and longjmp master breakpoints. */
3652 if (b->type == bp_thread_event || b->type == bp_overlay_event
3653 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3654 || b->type == bp_exception_master)
3655 {
3656 delete_breakpoint (b);
3657 continue;
3658 }
3659
3660 /* Step-resume breakpoints are meaningless after an exec(). */
3661 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3662 {
3663 delete_breakpoint (b);
3664 continue;
3665 }
3666
3667 /* Just like single-step breakpoints. */
3668 if (b->type == bp_single_step)
3669 {
3670 delete_breakpoint (b);
3671 continue;
3672 }
3673
3674 /* Longjmp and longjmp-resume breakpoints are also meaningless
3675 after an exec. */
3676 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3677 || b->type == bp_longjmp_call_dummy
3678 || b->type == bp_exception || b->type == bp_exception_resume)
3679 {
3680 delete_breakpoint (b);
3681 continue;
3682 }
3683
3684 if (b->type == bp_catchpoint)
3685 {
3686 /* For now, none of the bp_catchpoint breakpoints need to
3687 do anything at this point. In the future, if some of
3688 the catchpoints need to something, we will need to add
3689 a new method, and call this method from here. */
3690 continue;
3691 }
3692
3693 /* bp_finish is a special case. The only way we ought to be able
3694 to see one of these when an exec() has happened, is if the user
3695 caught a vfork, and then said "finish". Ordinarily a finish just
3696 carries them to the call-site of the current callee, by setting
3697 a temporary bp there and resuming. But in this case, the finish
3698 will carry them entirely through the vfork & exec.
3699
3700 We don't want to allow a bp_finish to remain inserted now. But
3701 we can't safely delete it, 'cause finish_command has a handle to
3702 the bp on a bpstat, and will later want to delete it. There's a
3703 chance (and I've seen it happen) that if we delete the bp_finish
3704 here, that its storage will get reused by the time finish_command
3705 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3706 We really must allow finish_command to delete a bp_finish.
3707
3708 In the absence of a general solution for the "how do we know
3709 it's safe to delete something others may have handles to?"
3710 problem, what we'll do here is just uninsert the bp_finish, and
3711 let finish_command delete it.
3712
3713 (We know the bp_finish is "doomed" in the sense that it's
3714 momentary, and will be deleted as soon as finish_command sees
3715 the inferior stopped. So it doesn't matter that the bp's
3716 address is probably bogus in the new a.out, unlike e.g., the
3717 solib breakpoints.) */
3718
3719 if (b->type == bp_finish)
3720 {
3721 continue;
3722 }
3723
3724 /* Without a symbolic address, we have little hope of the
3725 pre-exec() address meaning the same thing in the post-exec()
3726 a.out. */
3727 if (breakpoint_event_location_empty_p (b))
3728 {
3729 delete_breakpoint (b);
3730 continue;
3731 }
3732 }
3733 }
3734
3735 int
3736 detach_breakpoints (ptid_t ptid)
3737 {
3738 int val = 0;
3739 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3740 struct inferior *inf = current_inferior ();
3741
3742 if (ptid.pid () == inferior_ptid.pid ())
3743 error (_("Cannot detach breakpoints of inferior_ptid"));
3744
3745 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3746 inferior_ptid = ptid;
3747 for (bp_location *bl : all_bp_locations ())
3748 {
3749 if (bl->pspace != inf->pspace)
3750 continue;
3751
3752 /* This function must physically remove breakpoints locations
3753 from the specified ptid, without modifying the breakpoint
3754 package's state. Locations of type bp_loc_other are only
3755 maintained at GDB side. So, there is no need to remove
3756 these bp_loc_other locations. Moreover, removing these
3757 would modify the breakpoint package's state. */
3758 if (bl->loc_type == bp_loc_other)
3759 continue;
3760
3761 if (bl->inserted)
3762 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3763 }
3764
3765 return val;
3766 }
3767
3768 /* Remove the breakpoint location BL from the current address space.
3769 Note that this is used to detach breakpoints from a child fork.
3770 When we get here, the child isn't in the inferior list, and neither
3771 do we have objects to represent its address space --- we should
3772 *not* look at bl->pspace->aspace here. */
3773
3774 static int
3775 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3776 {
3777 int val;
3778
3779 /* BL is never in moribund_locations by our callers. */
3780 gdb_assert (bl->owner != NULL);
3781
3782 /* The type of none suggests that owner is actually deleted.
3783 This should not ever happen. */
3784 gdb_assert (bl->owner->type != bp_none);
3785
3786 if (bl->loc_type == bp_loc_software_breakpoint
3787 || bl->loc_type == bp_loc_hardware_breakpoint)
3788 {
3789 /* "Normal" instruction breakpoint: either the standard
3790 trap-instruction bp (bp_breakpoint), or a
3791 bp_hardware_breakpoint. */
3792
3793 /* First check to see if we have to handle an overlay. */
3794 if (overlay_debugging == ovly_off
3795 || bl->section == NULL
3796 || !(section_is_overlay (bl->section)))
3797 {
3798 /* No overlay handling: just remove the breakpoint. */
3799
3800 /* If we're trying to uninsert a memory breakpoint that we
3801 know is set in a dynamic object that is marked
3802 shlib_disabled, then either the dynamic object was
3803 removed with "remove-symbol-file" or with
3804 "nosharedlibrary". In the former case, we don't know
3805 whether another dynamic object might have loaded over the
3806 breakpoint's address -- the user might well let us know
3807 about it next with add-symbol-file (the whole point of
3808 add-symbol-file is letting the user manually maintain a
3809 list of dynamically loaded objects). If we have the
3810 breakpoint's shadow memory, that is, this is a software
3811 breakpoint managed by GDB, check whether the breakpoint
3812 is still inserted in memory, to avoid overwriting wrong
3813 code with stale saved shadow contents. Note that HW
3814 breakpoints don't have shadow memory, as they're
3815 implemented using a mechanism that is not dependent on
3816 being able to modify the target's memory, and as such
3817 they should always be removed. */
3818 if (bl->shlib_disabled
3819 && bl->target_info.shadow_len != 0
3820 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3821 val = 0;
3822 else
3823 val = bl->owner->ops->remove_location (bl, reason);
3824 }
3825 else
3826 {
3827 /* This breakpoint is in an overlay section.
3828 Did we set a breakpoint at the LMA? */
3829 if (!overlay_events_enabled)
3830 {
3831 /* Yes -- overlay event support is not active, so we
3832 should have set a breakpoint at the LMA. Remove it.
3833 */
3834 /* Ignore any failures: if the LMA is in ROM, we will
3835 have already warned when we failed to insert it. */
3836 if (bl->loc_type == bp_loc_hardware_breakpoint)
3837 target_remove_hw_breakpoint (bl->gdbarch,
3838 &bl->overlay_target_info);
3839 else
3840 target_remove_breakpoint (bl->gdbarch,
3841 &bl->overlay_target_info,
3842 reason);
3843 }
3844 /* Did we set a breakpoint at the VMA?
3845 If so, we will have marked the breakpoint 'inserted'. */
3846 if (bl->inserted)
3847 {
3848 /* Yes -- remove it. Previously we did not bother to
3849 remove the breakpoint if the section had been
3850 unmapped, but let's not rely on that being safe. We
3851 don't know what the overlay manager might do. */
3852
3853 /* However, we should remove *software* breakpoints only
3854 if the section is still mapped, or else we overwrite
3855 wrong code with the saved shadow contents. */
3856 if (bl->loc_type == bp_loc_hardware_breakpoint
3857 || section_is_mapped (bl->section))
3858 val = bl->owner->ops->remove_location (bl, reason);
3859 else
3860 val = 0;
3861 }
3862 else
3863 {
3864 /* No -- not inserted, so no need to remove. No error. */
3865 val = 0;
3866 }
3867 }
3868
3869 /* In some cases, we might not be able to remove a breakpoint in
3870 a shared library that has already been removed, but we have
3871 not yet processed the shlib unload event. Similarly for an
3872 unloaded add-symbol-file object - the user might not yet have
3873 had the chance to remove-symbol-file it. shlib_disabled will
3874 be set if the library/object has already been removed, but
3875 the breakpoint hasn't been uninserted yet, e.g., after
3876 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3877 always-inserted mode. */
3878 if (val
3879 && (bl->loc_type == bp_loc_software_breakpoint
3880 && (bl->shlib_disabled
3881 || solib_name_from_address (bl->pspace, bl->address)
3882 || shared_objfile_contains_address_p (bl->pspace,
3883 bl->address))))
3884 val = 0;
3885
3886 if (val)
3887 return val;
3888 bl->inserted = (reason == DETACH_BREAKPOINT);
3889 }
3890 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3891 {
3892 gdb_assert (bl->owner->ops != NULL
3893 && bl->owner->ops->remove_location != NULL);
3894
3895 bl->inserted = (reason == DETACH_BREAKPOINT);
3896 bl->owner->ops->remove_location (bl, reason);
3897
3898 /* Failure to remove any of the hardware watchpoints comes here. */
3899 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3900 warning (_("Could not remove hardware watchpoint %d."),
3901 bl->owner->number);
3902 }
3903 else if (bl->owner->type == bp_catchpoint
3904 && breakpoint_enabled (bl->owner)
3905 && !bl->duplicate)
3906 {
3907 gdb_assert (bl->owner->ops != NULL
3908 && bl->owner->ops->remove_location != NULL);
3909
3910 val = bl->owner->ops->remove_location (bl, reason);
3911 if (val)
3912 return val;
3913
3914 bl->inserted = (reason == DETACH_BREAKPOINT);
3915 }
3916
3917 return 0;
3918 }
3919
3920 static int
3921 remove_breakpoint (struct bp_location *bl)
3922 {
3923 /* BL is never in moribund_locations by our callers. */
3924 gdb_assert (bl->owner != NULL);
3925
3926 /* The type of none suggests that owner is actually deleted.
3927 This should not ever happen. */
3928 gdb_assert (bl->owner->type != bp_none);
3929
3930 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3931
3932 switch_to_program_space_and_thread (bl->pspace);
3933
3934 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
3935 }
3936
3937 /* Clear the "inserted" flag in all breakpoints. */
3938
3939 void
3940 mark_breakpoints_out (void)
3941 {
3942 for (bp_location *bl : all_bp_locations ())
3943 if (bl->pspace == current_program_space)
3944 bl->inserted = 0;
3945 }
3946
3947 /* Clear the "inserted" flag in all breakpoints and delete any
3948 breakpoints which should go away between runs of the program.
3949
3950 Plus other such housekeeping that has to be done for breakpoints
3951 between runs.
3952
3953 Note: this function gets called at the end of a run (by
3954 generic_mourn_inferior) and when a run begins (by
3955 init_wait_for_inferior). */
3956
3957
3958
3959 void
3960 breakpoint_init_inferior (enum inf_context context)
3961 {
3962 struct program_space *pspace = current_program_space;
3963
3964 /* If breakpoint locations are shared across processes, then there's
3965 nothing to do. */
3966 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
3967 return;
3968
3969 mark_breakpoints_out ();
3970
3971 for (breakpoint *b : all_breakpoints_safe ())
3972 {
3973 if (b->loc && b->loc->pspace != pspace)
3974 continue;
3975
3976 switch (b->type)
3977 {
3978 case bp_call_dummy:
3979 case bp_longjmp_call_dummy:
3980
3981 /* If the call dummy breakpoint is at the entry point it will
3982 cause problems when the inferior is rerun, so we better get
3983 rid of it. */
3984
3985 case bp_watchpoint_scope:
3986
3987 /* Also get rid of scope breakpoints. */
3988
3989 case bp_shlib_event:
3990
3991 /* Also remove solib event breakpoints. Their addresses may
3992 have changed since the last time we ran the program.
3993 Actually we may now be debugging against different target;
3994 and so the solib backend that installed this breakpoint may
3995 not be used in by the target. E.g.,
3996
3997 (gdb) file prog-linux
3998 (gdb) run # native linux target
3999 ...
4000 (gdb) kill
4001 (gdb) file prog-win.exe
4002 (gdb) tar rem :9999 # remote Windows gdbserver.
4003 */
4004
4005 case bp_step_resume:
4006
4007 /* Also remove step-resume breakpoints. */
4008
4009 case bp_single_step:
4010
4011 /* Also remove single-step breakpoints. */
4012
4013 delete_breakpoint (b);
4014 break;
4015
4016 case bp_watchpoint:
4017 case bp_hardware_watchpoint:
4018 case bp_read_watchpoint:
4019 case bp_access_watchpoint:
4020 {
4021 struct watchpoint *w = (struct watchpoint *) b;
4022
4023 /* Likewise for watchpoints on local expressions. */
4024 if (w->exp_valid_block != NULL)
4025 delete_breakpoint (b);
4026 else
4027 {
4028 /* Get rid of existing locations, which are no longer
4029 valid. New ones will be created in
4030 update_watchpoint, when the inferior is restarted.
4031 The next update_global_location_list call will
4032 garbage collect them. */
4033 b->loc = NULL;
4034
4035 if (context == inf_starting)
4036 {
4037 /* Reset val field to force reread of starting value in
4038 insert_breakpoints. */
4039 w->val.reset (nullptr);
4040 w->val_valid = false;
4041 }
4042 }
4043 }
4044 break;
4045 default:
4046 break;
4047 }
4048 }
4049
4050 /* Get rid of the moribund locations. */
4051 for (bp_location *bl : moribund_locations)
4052 decref_bp_location (&bl);
4053 moribund_locations.clear ();
4054 }
4055
4056 /* These functions concern about actual breakpoints inserted in the
4057 target --- to e.g. check if we need to do decr_pc adjustment or if
4058 we need to hop over the bkpt --- so we check for address space
4059 match, not program space. */
4060
4061 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4062 exists at PC. It returns ordinary_breakpoint_here if it's an
4063 ordinary breakpoint, or permanent_breakpoint_here if it's a
4064 permanent breakpoint.
4065 - When continuing from a location with an ordinary breakpoint, we
4066 actually single step once before calling insert_breakpoints.
4067 - When continuing from a location with a permanent breakpoint, we
4068 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4069 the target, to advance the PC past the breakpoint. */
4070
4071 enum breakpoint_here
4072 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4073 {
4074 int any_breakpoint_here = 0;
4075
4076 for (bp_location *bl : all_bp_locations ())
4077 {
4078 if (bl->loc_type != bp_loc_software_breakpoint
4079 && bl->loc_type != bp_loc_hardware_breakpoint)
4080 continue;
4081
4082 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4083 if ((breakpoint_enabled (bl->owner)
4084 || bl->permanent)
4085 && breakpoint_location_address_match (bl, aspace, pc))
4086 {
4087 if (overlay_debugging
4088 && section_is_overlay (bl->section)
4089 && !section_is_mapped (bl->section))
4090 continue; /* unmapped overlay -- can't be a match */
4091 else if (bl->permanent)
4092 return permanent_breakpoint_here;
4093 else
4094 any_breakpoint_here = 1;
4095 }
4096 }
4097
4098 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4099 }
4100
4101 /* See breakpoint.h. */
4102
4103 int
4104 breakpoint_in_range_p (const address_space *aspace,
4105 CORE_ADDR addr, ULONGEST len)
4106 {
4107 for (bp_location *bl : all_bp_locations ())
4108 {
4109 if (bl->loc_type != bp_loc_software_breakpoint
4110 && bl->loc_type != bp_loc_hardware_breakpoint)
4111 continue;
4112
4113 if ((breakpoint_enabled (bl->owner)
4114 || bl->permanent)
4115 && breakpoint_location_address_range_overlap (bl, aspace,
4116 addr, len))
4117 {
4118 if (overlay_debugging
4119 && section_is_overlay (bl->section)
4120 && !section_is_mapped (bl->section))
4121 {
4122 /* Unmapped overlay -- can't be a match. */
4123 continue;
4124 }
4125
4126 return 1;
4127 }
4128 }
4129
4130 return 0;
4131 }
4132
4133 /* Return true if there's a moribund breakpoint at PC. */
4134
4135 int
4136 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4137 {
4138 for (bp_location *loc : moribund_locations)
4139 if (breakpoint_location_address_match (loc, aspace, pc))
4140 return 1;
4141
4142 return 0;
4143 }
4144
4145 /* Returns non-zero iff BL is inserted at PC, in address space
4146 ASPACE. */
4147
4148 static int
4149 bp_location_inserted_here_p (struct bp_location *bl,
4150 const address_space *aspace, CORE_ADDR pc)
4151 {
4152 if (bl->inserted
4153 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4154 aspace, pc))
4155 {
4156 if (overlay_debugging
4157 && section_is_overlay (bl->section)
4158 && !section_is_mapped (bl->section))
4159 return 0; /* unmapped overlay -- can't be a match */
4160 else
4161 return 1;
4162 }
4163 return 0;
4164 }
4165
4166 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4167
4168 int
4169 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4170 {
4171 for (bp_location *bl : all_bp_locations_at_addr (pc))
4172 {
4173 if (bl->loc_type != bp_loc_software_breakpoint
4174 && bl->loc_type != bp_loc_hardware_breakpoint)
4175 continue;
4176
4177 if (bp_location_inserted_here_p (bl, aspace, pc))
4178 return 1;
4179 }
4180 return 0;
4181 }
4182
4183 /* This function returns non-zero iff there is a software breakpoint
4184 inserted at PC. */
4185
4186 int
4187 software_breakpoint_inserted_here_p (const address_space *aspace,
4188 CORE_ADDR pc)
4189 {
4190 for (bp_location *bl : all_bp_locations_at_addr (pc))
4191 {
4192 if (bl->loc_type != bp_loc_software_breakpoint)
4193 continue;
4194
4195 if (bp_location_inserted_here_p (bl, aspace, pc))
4196 return 1;
4197 }
4198
4199 return 0;
4200 }
4201
4202 /* See breakpoint.h. */
4203
4204 int
4205 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4206 CORE_ADDR pc)
4207 {
4208 for (bp_location *bl : all_bp_locations_at_addr (pc))
4209 {
4210 if (bl->loc_type != bp_loc_hardware_breakpoint)
4211 continue;
4212
4213 if (bp_location_inserted_here_p (bl, aspace, pc))
4214 return 1;
4215 }
4216
4217 return 0;
4218 }
4219
4220 int
4221 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4222 CORE_ADDR addr, ULONGEST len)
4223 {
4224 for (breakpoint *bpt : all_breakpoints ())
4225 {
4226 if (bpt->type != bp_hardware_watchpoint
4227 && bpt->type != bp_access_watchpoint)
4228 continue;
4229
4230 if (!breakpoint_enabled (bpt))
4231 continue;
4232
4233 for (bp_location *loc : bpt->locations ())
4234 if (loc->pspace->aspace == aspace && loc->inserted)
4235 {
4236 CORE_ADDR l, h;
4237
4238 /* Check for intersection. */
4239 l = std::max<CORE_ADDR> (loc->address, addr);
4240 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4241 if (l < h)
4242 return 1;
4243 }
4244 }
4245 return 0;
4246 }
4247
4248 /* See breakpoint.h. */
4249
4250 bool
4251 is_catchpoint (struct breakpoint *b)
4252 {
4253 return (b->type == bp_catchpoint);
4254 }
4255
4256 /* Clear a bpstat so that it says we are not at any breakpoint.
4257 Also free any storage that is part of a bpstat. */
4258
4259 void
4260 bpstat_clear (bpstat *bsp)
4261 {
4262 bpstat p;
4263 bpstat q;
4264
4265 if (bsp == 0)
4266 return;
4267 p = *bsp;
4268 while (p != NULL)
4269 {
4270 q = p->next;
4271 delete p;
4272 p = q;
4273 }
4274 *bsp = NULL;
4275 }
4276
4277 bpstats::bpstats (const bpstats &other)
4278 : next (NULL),
4279 bp_location_at (other.bp_location_at),
4280 breakpoint_at (other.breakpoint_at),
4281 commands (other.commands),
4282 print (other.print),
4283 stop (other.stop),
4284 print_it (other.print_it)
4285 {
4286 if (other.old_val != NULL)
4287 old_val = release_value (value_copy (other.old_val.get ()));
4288 }
4289
4290 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4291 is part of the bpstat is copied as well. */
4292
4293 bpstat
4294 bpstat_copy (bpstat bs)
4295 {
4296 bpstat p = NULL;
4297 bpstat tmp;
4298 bpstat retval = NULL;
4299
4300 if (bs == NULL)
4301 return bs;
4302
4303 for (; bs != NULL; bs = bs->next)
4304 {
4305 tmp = new bpstats (*bs);
4306
4307 if (p == NULL)
4308 /* This is the first thing in the chain. */
4309 retval = tmp;
4310 else
4311 p->next = tmp;
4312 p = tmp;
4313 }
4314 p->next = NULL;
4315 return retval;
4316 }
4317
4318 /* Find the bpstat associated with this breakpoint. */
4319
4320 bpstat
4321 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4322 {
4323 if (bsp == NULL)
4324 return NULL;
4325
4326 for (; bsp != NULL; bsp = bsp->next)
4327 {
4328 if (bsp->breakpoint_at == breakpoint)
4329 return bsp;
4330 }
4331 return NULL;
4332 }
4333
4334 /* See breakpoint.h. */
4335
4336 bool
4337 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4338 {
4339 for (; bsp != NULL; bsp = bsp->next)
4340 {
4341 if (bsp->breakpoint_at == NULL)
4342 {
4343 /* A moribund location can never explain a signal other than
4344 GDB_SIGNAL_TRAP. */
4345 if (sig == GDB_SIGNAL_TRAP)
4346 return true;
4347 }
4348 else
4349 {
4350 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4351 sig))
4352 return true;
4353 }
4354 }
4355
4356 return false;
4357 }
4358
4359 /* Put in *NUM the breakpoint number of the first breakpoint we are
4360 stopped at. *BSP upon return is a bpstat which points to the
4361 remaining breakpoints stopped at (but which is not guaranteed to be
4362 good for anything but further calls to bpstat_num).
4363
4364 Return 0 if passed a bpstat which does not indicate any breakpoints.
4365 Return -1 if stopped at a breakpoint that has been deleted since
4366 we set it.
4367 Return 1 otherwise. */
4368
4369 int
4370 bpstat_num (bpstat *bsp, int *num)
4371 {
4372 struct breakpoint *b;
4373
4374 if ((*bsp) == NULL)
4375 return 0; /* No more breakpoint values */
4376
4377 /* We assume we'll never have several bpstats that correspond to a
4378 single breakpoint -- otherwise, this function might return the
4379 same number more than once and this will look ugly. */
4380 b = (*bsp)->breakpoint_at;
4381 *bsp = (*bsp)->next;
4382 if (b == NULL)
4383 return -1; /* breakpoint that's been deleted since */
4384
4385 *num = b->number; /* We have its number */
4386 return 1;
4387 }
4388
4389 /* See breakpoint.h. */
4390
4391 void
4392 bpstat_clear_actions (void)
4393 {
4394 bpstat bs;
4395
4396 if (inferior_ptid == null_ptid)
4397 return;
4398
4399 thread_info *tp = inferior_thread ();
4400 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4401 {
4402 bs->commands = NULL;
4403 bs->old_val.reset (nullptr);
4404 }
4405 }
4406
4407 /* Called when a command is about to proceed the inferior. */
4408
4409 static void
4410 breakpoint_about_to_proceed (void)
4411 {
4412 if (inferior_ptid != null_ptid)
4413 {
4414 struct thread_info *tp = inferior_thread ();
4415
4416 /* Allow inferior function calls in breakpoint commands to not
4417 interrupt the command list. When the call finishes
4418 successfully, the inferior will be standing at the same
4419 breakpoint as if nothing happened. */
4420 if (tp->control.in_infcall)
4421 return;
4422 }
4423
4424 breakpoint_proceeded = 1;
4425 }
4426
4427 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4428 or its equivalent. */
4429
4430 static int
4431 command_line_is_silent (struct command_line *cmd)
4432 {
4433 return cmd && (strcmp ("silent", cmd->line) == 0);
4434 }
4435
4436 /* Execute all the commands associated with all the breakpoints at
4437 this location. Any of these commands could cause the process to
4438 proceed beyond this point, etc. We look out for such changes by
4439 checking the global "breakpoint_proceeded" after each command.
4440
4441 Returns true if a breakpoint command resumed the inferior. In that
4442 case, it is the caller's responsibility to recall it again with the
4443 bpstat of the current thread. */
4444
4445 static int
4446 bpstat_do_actions_1 (bpstat *bsp)
4447 {
4448 bpstat bs;
4449 int again = 0;
4450
4451 /* Avoid endless recursion if a `source' command is contained
4452 in bs->commands. */
4453 if (executing_breakpoint_commands)
4454 return 0;
4455
4456 scoped_restore save_executing
4457 = make_scoped_restore (&executing_breakpoint_commands, 1);
4458
4459 scoped_restore preventer = prevent_dont_repeat ();
4460
4461 /* This pointer will iterate over the list of bpstat's. */
4462 bs = *bsp;
4463
4464 breakpoint_proceeded = 0;
4465 for (; bs != NULL; bs = bs->next)
4466 {
4467 struct command_line *cmd = NULL;
4468
4469 /* Take ownership of the BSP's command tree, if it has one.
4470
4471 The command tree could legitimately contain commands like
4472 'step' and 'next', which call clear_proceed_status, which
4473 frees stop_bpstat's command tree. To make sure this doesn't
4474 free the tree we're executing out from under us, we need to
4475 take ownership of the tree ourselves. Since a given bpstat's
4476 commands are only executed once, we don't need to copy it; we
4477 can clear the pointer in the bpstat, and make sure we free
4478 the tree when we're done. */
4479 counted_command_line ccmd = bs->commands;
4480 bs->commands = NULL;
4481 if (ccmd != NULL)
4482 cmd = ccmd.get ();
4483 if (command_line_is_silent (cmd))
4484 {
4485 /* The action has been already done by bpstat_stop_status. */
4486 cmd = cmd->next;
4487 }
4488
4489 while (cmd != NULL)
4490 {
4491 execute_control_command (cmd);
4492
4493 if (breakpoint_proceeded)
4494 break;
4495 else
4496 cmd = cmd->next;
4497 }
4498
4499 if (breakpoint_proceeded)
4500 {
4501 if (current_ui->async)
4502 /* If we are in async mode, then the target might be still
4503 running, not stopped at any breakpoint, so nothing for
4504 us to do here -- just return to the event loop. */
4505 ;
4506 else
4507 /* In sync mode, when execute_control_command returns
4508 we're already standing on the next breakpoint.
4509 Breakpoint commands for that stop were not run, since
4510 execute_command does not run breakpoint commands --
4511 only command_line_handler does, but that one is not
4512 involved in execution of breakpoint commands. So, we
4513 can now execute breakpoint commands. It should be
4514 noted that making execute_command do bpstat actions is
4515 not an option -- in this case we'll have recursive
4516 invocation of bpstat for each breakpoint with a
4517 command, and can easily blow up GDB stack. Instead, we
4518 return true, which will trigger the caller to recall us
4519 with the new stop_bpstat. */
4520 again = 1;
4521 break;
4522 }
4523 }
4524 return again;
4525 }
4526
4527 /* Helper for bpstat_do_actions. Get the current thread, if there's
4528 one, is alive and has execution. Return NULL otherwise. */
4529
4530 static thread_info *
4531 get_bpstat_thread ()
4532 {
4533 if (inferior_ptid == null_ptid || !target_has_execution ())
4534 return NULL;
4535
4536 thread_info *tp = inferior_thread ();
4537 if (tp->state == THREAD_EXITED || tp->executing)
4538 return NULL;
4539 return tp;
4540 }
4541
4542 void
4543 bpstat_do_actions (void)
4544 {
4545 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4546 thread_info *tp;
4547
4548 /* Do any commands attached to breakpoint we are stopped at. */
4549 while ((tp = get_bpstat_thread ()) != NULL)
4550 {
4551 /* Since in sync mode, bpstat_do_actions may resume the
4552 inferior, and only return when it is stopped at the next
4553 breakpoint, we keep doing breakpoint actions until it returns
4554 false to indicate the inferior was not resumed. */
4555 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4556 break;
4557 }
4558
4559 cleanup_if_error.release ();
4560 }
4561
4562 /* Print out the (old or new) value associated with a watchpoint. */
4563
4564 static void
4565 watchpoint_value_print (struct value *val, struct ui_file *stream)
4566 {
4567 if (val == NULL)
4568 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4569 else
4570 {
4571 struct value_print_options opts;
4572 get_user_print_options (&opts);
4573 value_print (val, stream, &opts);
4574 }
4575 }
4576
4577 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4578 debugging multiple threads. */
4579
4580 void
4581 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4582 {
4583 if (uiout->is_mi_like_p ())
4584 return;
4585
4586 uiout->text ("\n");
4587
4588 if (show_thread_that_caused_stop ())
4589 {
4590 const char *name;
4591 struct thread_info *thr = inferior_thread ();
4592
4593 uiout->text ("Thread ");
4594 uiout->field_string ("thread-id", print_thread_id (thr));
4595
4596 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4597 if (name != NULL)
4598 {
4599 uiout->text (" \"");
4600 uiout->field_string ("name", name);
4601 uiout->text ("\"");
4602 }
4603
4604 uiout->text (" hit ");
4605 }
4606 }
4607
4608 /* Generic routine for printing messages indicating why we
4609 stopped. The behavior of this function depends on the value
4610 'print_it' in the bpstat structure. Under some circumstances we
4611 may decide not to print anything here and delegate the task to
4612 normal_stop(). */
4613
4614 static enum print_stop_action
4615 print_bp_stop_message (bpstat bs)
4616 {
4617 switch (bs->print_it)
4618 {
4619 case print_it_noop:
4620 /* Nothing should be printed for this bpstat entry. */
4621 return PRINT_UNKNOWN;
4622 break;
4623
4624 case print_it_done:
4625 /* We still want to print the frame, but we already printed the
4626 relevant messages. */
4627 return PRINT_SRC_AND_LOC;
4628 break;
4629
4630 case print_it_normal:
4631 {
4632 struct breakpoint *b = bs->breakpoint_at;
4633
4634 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4635 which has since been deleted. */
4636 if (b == NULL)
4637 return PRINT_UNKNOWN;
4638
4639 /* Normal case. Call the breakpoint's print_it method. */
4640 return b->ops->print_it (bs);
4641 }
4642 break;
4643
4644 default:
4645 internal_error (__FILE__, __LINE__,
4646 _("print_bp_stop_message: unrecognized enum value"));
4647 break;
4648 }
4649 }
4650
4651 /* A helper function that prints a shared library stopped event. */
4652
4653 static void
4654 print_solib_event (int is_catchpoint)
4655 {
4656 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4657 bool any_added = !current_program_space->added_solibs.empty ();
4658
4659 if (!is_catchpoint)
4660 {
4661 if (any_added || any_deleted)
4662 current_uiout->text (_("Stopped due to shared library event:\n"));
4663 else
4664 current_uiout->text (_("Stopped due to shared library event (no "
4665 "libraries added or removed)\n"));
4666 }
4667
4668 if (current_uiout->is_mi_like_p ())
4669 current_uiout->field_string ("reason",
4670 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4671
4672 if (any_deleted)
4673 {
4674 current_uiout->text (_(" Inferior unloaded "));
4675 ui_out_emit_list list_emitter (current_uiout, "removed");
4676 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4677 {
4678 const std::string &name = current_program_space->deleted_solibs[ix];
4679
4680 if (ix > 0)
4681 current_uiout->text (" ");
4682 current_uiout->field_string ("library", name);
4683 current_uiout->text ("\n");
4684 }
4685 }
4686
4687 if (any_added)
4688 {
4689 current_uiout->text (_(" Inferior loaded "));
4690 ui_out_emit_list list_emitter (current_uiout, "added");
4691 bool first = true;
4692 for (so_list *iter : current_program_space->added_solibs)
4693 {
4694 if (!first)
4695 current_uiout->text (" ");
4696 first = false;
4697 current_uiout->field_string ("library", iter->so_name);
4698 current_uiout->text ("\n");
4699 }
4700 }
4701 }
4702
4703 /* Print a message indicating what happened. This is called from
4704 normal_stop(). The input to this routine is the head of the bpstat
4705 list - a list of the eventpoints that caused this stop. KIND is
4706 the target_waitkind for the stopping event. This
4707 routine calls the generic print routine for printing a message
4708 about reasons for stopping. This will print (for example) the
4709 "Breakpoint n," part of the output. The return value of this
4710 routine is one of:
4711
4712 PRINT_UNKNOWN: Means we printed nothing.
4713 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4714 code to print the location. An example is
4715 "Breakpoint 1, " which should be followed by
4716 the location.
4717 PRINT_SRC_ONLY: Means we printed something, but there is no need
4718 to also print the location part of the message.
4719 An example is the catch/throw messages, which
4720 don't require a location appended to the end.
4721 PRINT_NOTHING: We have done some printing and we don't need any
4722 further info to be printed. */
4723
4724 enum print_stop_action
4725 bpstat_print (bpstat bs, int kind)
4726 {
4727 enum print_stop_action val;
4728
4729 /* Maybe another breakpoint in the chain caused us to stop.
4730 (Currently all watchpoints go on the bpstat whether hit or not.
4731 That probably could (should) be changed, provided care is taken
4732 with respect to bpstat_explains_signal). */
4733 for (; bs; bs = bs->next)
4734 {
4735 val = print_bp_stop_message (bs);
4736 if (val == PRINT_SRC_ONLY
4737 || val == PRINT_SRC_AND_LOC
4738 || val == PRINT_NOTHING)
4739 return val;
4740 }
4741
4742 /* If we had hit a shared library event breakpoint,
4743 print_bp_stop_message would print out this message. If we hit an
4744 OS-level shared library event, do the same thing. */
4745 if (kind == TARGET_WAITKIND_LOADED)
4746 {
4747 print_solib_event (0);
4748 return PRINT_NOTHING;
4749 }
4750
4751 /* We reached the end of the chain, or we got a null BS to start
4752 with and nothing was printed. */
4753 return PRINT_UNKNOWN;
4754 }
4755
4756 /* Evaluate the boolean expression EXP and return the result. */
4757
4758 static bool
4759 breakpoint_cond_eval (expression *exp)
4760 {
4761 struct value *mark = value_mark ();
4762 bool res = value_true (evaluate_expression (exp));
4763
4764 value_free_to_mark (mark);
4765 return res;
4766 }
4767
4768 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4769
4770 bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4771 : next (NULL),
4772 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
4773 breakpoint_at (bl->owner),
4774 commands (NULL),
4775 print (0),
4776 stop (0),
4777 print_it (print_it_normal)
4778 {
4779 **bs_link_pointer = this;
4780 *bs_link_pointer = &next;
4781 }
4782
4783 bpstats::bpstats ()
4784 : next (NULL),
4785 breakpoint_at (NULL),
4786 commands (NULL),
4787 print (0),
4788 stop (0),
4789 print_it (print_it_normal)
4790 {
4791 }
4792 \f
4793 /* The target has stopped with waitstatus WS. Check if any hardware
4794 watchpoints have triggered, according to the target. */
4795
4796 int
4797 watchpoints_triggered (struct target_waitstatus *ws)
4798 {
4799 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
4800 CORE_ADDR addr;
4801
4802 if (!stopped_by_watchpoint)
4803 {
4804 /* We were not stopped by a watchpoint. Mark all watchpoints
4805 as not triggered. */
4806 for (breakpoint *b : all_breakpoints ())
4807 if (is_hardware_watchpoint (b))
4808 {
4809 struct watchpoint *w = (struct watchpoint *) b;
4810
4811 w->watchpoint_triggered = watch_triggered_no;
4812 }
4813
4814 return 0;
4815 }
4816
4817 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
4818 {
4819 /* We were stopped by a watchpoint, but we don't know where.
4820 Mark all watchpoints as unknown. */
4821 for (breakpoint *b : all_breakpoints ())
4822 if (is_hardware_watchpoint (b))
4823 {
4824 struct watchpoint *w = (struct watchpoint *) b;
4825
4826 w->watchpoint_triggered = watch_triggered_unknown;
4827 }
4828
4829 return 1;
4830 }
4831
4832 /* The target could report the data address. Mark watchpoints
4833 affected by this data address as triggered, and all others as not
4834 triggered. */
4835
4836 for (breakpoint *b : all_breakpoints ())
4837 if (is_hardware_watchpoint (b))
4838 {
4839 struct watchpoint *w = (struct watchpoint *) b;
4840
4841 w->watchpoint_triggered = watch_triggered_no;
4842 for (bp_location *loc : b->locations ())
4843 {
4844 if (is_masked_watchpoint (b))
4845 {
4846 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4847 CORE_ADDR start = loc->address & w->hw_wp_mask;
4848
4849 if (newaddr == start)
4850 {
4851 w->watchpoint_triggered = watch_triggered_yes;
4852 break;
4853 }
4854 }
4855 /* Exact match not required. Within range is sufficient. */
4856 else if (target_watchpoint_addr_within_range
4857 (current_inferior ()->top_target (), addr, loc->address,
4858 loc->length))
4859 {
4860 w->watchpoint_triggered = watch_triggered_yes;
4861 break;
4862 }
4863 }
4864 }
4865
4866 return 1;
4867 }
4868
4869 /* Possible return values for watchpoint_check. */
4870 enum wp_check_result
4871 {
4872 /* The watchpoint has been deleted. */
4873 WP_DELETED = 1,
4874
4875 /* The value has changed. */
4876 WP_VALUE_CHANGED = 2,
4877
4878 /* The value has not changed. */
4879 WP_VALUE_NOT_CHANGED = 3,
4880
4881 /* Ignore this watchpoint, no matter if the value changed or not. */
4882 WP_IGNORE = 4,
4883 };
4884
4885 #define BP_TEMPFLAG 1
4886 #define BP_HARDWAREFLAG 2
4887
4888 /* Evaluate watchpoint condition expression and check if its value
4889 changed. */
4890
4891 static wp_check_result
4892 watchpoint_check (bpstat bs)
4893 {
4894 struct watchpoint *b;
4895 struct frame_info *fr;
4896 int within_current_scope;
4897
4898 /* BS is built from an existing struct breakpoint. */
4899 gdb_assert (bs->breakpoint_at != NULL);
4900 b = (struct watchpoint *) bs->breakpoint_at;
4901
4902 /* If this is a local watchpoint, we only want to check if the
4903 watchpoint frame is in scope if the current thread is the thread
4904 that was used to create the watchpoint. */
4905 if (!watchpoint_in_thread_scope (b))
4906 return WP_IGNORE;
4907
4908 if (b->exp_valid_block == NULL)
4909 within_current_scope = 1;
4910 else
4911 {
4912 struct frame_info *frame = get_current_frame ();
4913 struct gdbarch *frame_arch = get_frame_arch (frame);
4914 CORE_ADDR frame_pc = get_frame_pc (frame);
4915
4916 /* stack_frame_destroyed_p() returns a non-zero value if we're
4917 still in the function but the stack frame has already been
4918 invalidated. Since we can't rely on the values of local
4919 variables after the stack has been destroyed, we are treating
4920 the watchpoint in that state as `not changed' without further
4921 checking. Don't mark watchpoints as changed if the current
4922 frame is in an epilogue - even if they are in some other
4923 frame, our view of the stack is likely to be wrong and
4924 frame_find_by_id could error out. */
4925 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
4926 return WP_IGNORE;
4927
4928 fr = frame_find_by_id (b->watchpoint_frame);
4929 within_current_scope = (fr != NULL);
4930
4931 /* If we've gotten confused in the unwinder, we might have
4932 returned a frame that can't describe this variable. */
4933 if (within_current_scope)
4934 {
4935 struct symbol *function;
4936
4937 function = get_frame_function (fr);
4938 if (function == NULL
4939 || !contained_in (b->exp_valid_block,
4940 SYMBOL_BLOCK_VALUE (function)))
4941 within_current_scope = 0;
4942 }
4943
4944 if (within_current_scope)
4945 /* If we end up stopping, the current frame will get selected
4946 in normal_stop. So this call to select_frame won't affect
4947 the user. */
4948 select_frame (fr);
4949 }
4950
4951 if (within_current_scope)
4952 {
4953 /* We use value_{,free_to_}mark because it could be a *long*
4954 time before we return to the command level and call
4955 free_all_values. We can't call free_all_values because we
4956 might be in the middle of evaluating a function call. */
4957
4958 struct value *mark;
4959 struct value *new_val;
4960
4961 if (is_masked_watchpoint (b))
4962 /* Since we don't know the exact trigger address (from
4963 stopped_data_address), just tell the user we've triggered
4964 a mask watchpoint. */
4965 return WP_VALUE_CHANGED;
4966
4967 mark = value_mark ();
4968 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
4969 NULL, NULL, false);
4970
4971 if (b->val_bitsize != 0)
4972 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4973
4974 /* We use value_equal_contents instead of value_equal because
4975 the latter coerces an array to a pointer, thus comparing just
4976 the address of the array instead of its contents. This is
4977 not what we want. */
4978 if ((b->val != NULL) != (new_val != NULL)
4979 || (b->val != NULL && !value_equal_contents (b->val.get (),
4980 new_val)))
4981 {
4982 bs->old_val = b->val;
4983 b->val = release_value (new_val);
4984 b->val_valid = true;
4985 if (new_val != NULL)
4986 value_free_to_mark (mark);
4987 return WP_VALUE_CHANGED;
4988 }
4989 else
4990 {
4991 /* Nothing changed. */
4992 value_free_to_mark (mark);
4993 return WP_VALUE_NOT_CHANGED;
4994 }
4995 }
4996 else
4997 {
4998 /* This seems like the only logical thing to do because
4999 if we temporarily ignored the watchpoint, then when
5000 we reenter the block in which it is valid it contains
5001 garbage (in the case of a function, it may have two
5002 garbage values, one before and one after the prologue).
5003 So we can't even detect the first assignment to it and
5004 watch after that (since the garbage may or may not equal
5005 the first value assigned). */
5006 /* We print all the stop information in
5007 breakpoint_ops->print_it, but in this case, by the time we
5008 call breakpoint_ops->print_it this bp will be deleted
5009 already. So we have no choice but print the information
5010 here. */
5011
5012 SWITCH_THRU_ALL_UIS ()
5013 {
5014 struct ui_out *uiout = current_uiout;
5015
5016 if (uiout->is_mi_like_p ())
5017 uiout->field_string
5018 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5019 uiout->message ("\nWatchpoint %pF deleted because the program has "
5020 "left the block in\n"
5021 "which its expression is valid.\n",
5022 signed_field ("wpnum", b->number));
5023 }
5024
5025 /* Make sure the watchpoint's commands aren't executed. */
5026 b->commands = NULL;
5027 watchpoint_del_at_next_stop (b);
5028
5029 return WP_DELETED;
5030 }
5031 }
5032
5033 /* Return true if it looks like target has stopped due to hitting
5034 breakpoint location BL. This function does not check if we should
5035 stop, only if BL explains the stop. */
5036
5037 static int
5038 bpstat_check_location (const struct bp_location *bl,
5039 const address_space *aspace, CORE_ADDR bp_addr,
5040 const struct target_waitstatus *ws)
5041 {
5042 struct breakpoint *b = bl->owner;
5043
5044 /* BL is from an existing breakpoint. */
5045 gdb_assert (b != NULL);
5046
5047 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5048 }
5049
5050 /* Determine if the watched values have actually changed, and we
5051 should stop. If not, set BS->stop to 0. */
5052
5053 static void
5054 bpstat_check_watchpoint (bpstat bs)
5055 {
5056 const struct bp_location *bl;
5057 struct watchpoint *b;
5058
5059 /* BS is built for existing struct breakpoint. */
5060 bl = bs->bp_location_at.get ();
5061 gdb_assert (bl != NULL);
5062 b = (struct watchpoint *) bs->breakpoint_at;
5063 gdb_assert (b != NULL);
5064
5065 {
5066 int must_check_value = 0;
5067
5068 if (b->type == bp_watchpoint)
5069 /* For a software watchpoint, we must always check the
5070 watched value. */
5071 must_check_value = 1;
5072 else if (b->watchpoint_triggered == watch_triggered_yes)
5073 /* We have a hardware watchpoint (read, write, or access)
5074 and the target earlier reported an address watched by
5075 this watchpoint. */
5076 must_check_value = 1;
5077 else if (b->watchpoint_triggered == watch_triggered_unknown
5078 && b->type == bp_hardware_watchpoint)
5079 /* We were stopped by a hardware watchpoint, but the target could
5080 not report the data address. We must check the watchpoint's
5081 value. Access and read watchpoints are out of luck; without
5082 a data address, we can't figure it out. */
5083 must_check_value = 1;
5084
5085 if (must_check_value)
5086 {
5087 wp_check_result e;
5088
5089 try
5090 {
5091 e = watchpoint_check (bs);
5092 }
5093 catch (const gdb_exception &ex)
5094 {
5095 exception_fprintf (gdb_stderr, ex,
5096 "Error evaluating expression "
5097 "for watchpoint %d\n",
5098 b->number);
5099
5100 SWITCH_THRU_ALL_UIS ()
5101 {
5102 printf_filtered (_("Watchpoint %d deleted.\n"),
5103 b->number);
5104 }
5105 watchpoint_del_at_next_stop (b);
5106 e = WP_DELETED;
5107 }
5108
5109 switch (e)
5110 {
5111 case WP_DELETED:
5112 /* We've already printed what needs to be printed. */
5113 bs->print_it = print_it_done;
5114 /* Stop. */
5115 break;
5116 case WP_IGNORE:
5117 bs->print_it = print_it_noop;
5118 bs->stop = 0;
5119 break;
5120 case WP_VALUE_CHANGED:
5121 if (b->type == bp_read_watchpoint)
5122 {
5123 /* There are two cases to consider here:
5124
5125 1. We're watching the triggered memory for reads.
5126 In that case, trust the target, and always report
5127 the watchpoint hit to the user. Even though
5128 reads don't cause value changes, the value may
5129 have changed since the last time it was read, and
5130 since we're not trapping writes, we will not see
5131 those, and as such we should ignore our notion of
5132 old value.
5133
5134 2. We're watching the triggered memory for both
5135 reads and writes. There are two ways this may
5136 happen:
5137
5138 2.1. This is a target that can't break on data
5139 reads only, but can break on accesses (reads or
5140 writes), such as e.g., x86. We detect this case
5141 at the time we try to insert read watchpoints.
5142
5143 2.2. Otherwise, the target supports read
5144 watchpoints, but, the user set an access or write
5145 watchpoint watching the same memory as this read
5146 watchpoint.
5147
5148 If we're watching memory writes as well as reads,
5149 ignore watchpoint hits when we find that the
5150 value hasn't changed, as reads don't cause
5151 changes. This still gives false positives when
5152 the program writes the same value to memory as
5153 what there was already in memory (we will confuse
5154 it for a read), but it's much better than
5155 nothing. */
5156
5157 int other_write_watchpoint = 0;
5158
5159 if (bl->watchpoint_type == hw_read)
5160 {
5161 for (breakpoint *other_b : all_breakpoints ())
5162 if (other_b->type == bp_hardware_watchpoint
5163 || other_b->type == bp_access_watchpoint)
5164 {
5165 struct watchpoint *other_w =
5166 (struct watchpoint *) other_b;
5167
5168 if (other_w->watchpoint_triggered
5169 == watch_triggered_yes)
5170 {
5171 other_write_watchpoint = 1;
5172 break;
5173 }
5174 }
5175 }
5176
5177 if (other_write_watchpoint
5178 || bl->watchpoint_type == hw_access)
5179 {
5180 /* We're watching the same memory for writes,
5181 and the value changed since the last time we
5182 updated it, so this trap must be for a write.
5183 Ignore it. */
5184 bs->print_it = print_it_noop;
5185 bs->stop = 0;
5186 }
5187 }
5188 break;
5189 case WP_VALUE_NOT_CHANGED:
5190 if (b->type == bp_hardware_watchpoint
5191 || b->type == bp_watchpoint)
5192 {
5193 /* Don't stop: write watchpoints shouldn't fire if
5194 the value hasn't changed. */
5195 bs->print_it = print_it_noop;
5196 bs->stop = 0;
5197 }
5198 /* Stop. */
5199 break;
5200 default:
5201 /* Can't happen. */
5202 break;
5203 }
5204 }
5205 else /* must_check_value == 0 */
5206 {
5207 /* This is a case where some watchpoint(s) triggered, but
5208 not at the address of this watchpoint, or else no
5209 watchpoint triggered after all. So don't print
5210 anything for this watchpoint. */
5211 bs->print_it = print_it_noop;
5212 bs->stop = 0;
5213 }
5214 }
5215 }
5216
5217 /* For breakpoints that are currently marked as telling gdb to stop,
5218 check conditions (condition proper, frame, thread and ignore count)
5219 of breakpoint referred to by BS. If we should not stop for this
5220 breakpoint, set BS->stop to 0. */
5221
5222 static void
5223 bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
5224 {
5225 const struct bp_location *bl;
5226 struct breakpoint *b;
5227 /* Assume stop. */
5228 bool condition_result = true;
5229 struct expression *cond;
5230
5231 gdb_assert (bs->stop);
5232
5233 /* BS is built for existing struct breakpoint. */
5234 bl = bs->bp_location_at.get ();
5235 gdb_assert (bl != NULL);
5236 b = bs->breakpoint_at;
5237 gdb_assert (b != NULL);
5238
5239 /* Even if the target evaluated the condition on its end and notified GDB, we
5240 need to do so again since GDB does not know if we stopped due to a
5241 breakpoint or a single step breakpoint. */
5242
5243 if (frame_id_p (b->frame_id)
5244 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5245 {
5246 bs->stop = 0;
5247 return;
5248 }
5249
5250 /* If this is a thread/task-specific breakpoint, don't waste cpu
5251 evaluating the condition if this isn't the specified
5252 thread/task. */
5253 if ((b->thread != -1 && b->thread != thread->global_num)
5254 || (b->task != 0 && b->task != ada_get_task_number (thread)))
5255 {
5256 bs->stop = 0;
5257 return;
5258 }
5259
5260 /* Evaluate extension language breakpoints that have a "stop" method
5261 implemented. */
5262 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5263
5264 if (is_watchpoint (b))
5265 {
5266 struct watchpoint *w = (struct watchpoint *) b;
5267
5268 cond = w->cond_exp.get ();
5269 }
5270 else
5271 cond = bl->cond.get ();
5272
5273 if (cond && b->disposition != disp_del_at_next_stop)
5274 {
5275 int within_current_scope = 1;
5276 struct watchpoint * w;
5277
5278 /* We use value_mark and value_free_to_mark because it could
5279 be a long time before we return to the command level and
5280 call free_all_values. We can't call free_all_values
5281 because we might be in the middle of evaluating a
5282 function call. */
5283 struct value *mark = value_mark ();
5284
5285 if (is_watchpoint (b))
5286 w = (struct watchpoint *) b;
5287 else
5288 w = NULL;
5289
5290 /* Need to select the frame, with all that implies so that
5291 the conditions will have the right context. Because we
5292 use the frame, we will not see an inlined function's
5293 variables when we arrive at a breakpoint at the start
5294 of the inlined function; the current frame will be the
5295 call site. */
5296 if (w == NULL || w->cond_exp_valid_block == NULL)
5297 select_frame (get_current_frame ());
5298 else
5299 {
5300 struct frame_info *frame;
5301
5302 /* For local watchpoint expressions, which particular
5303 instance of a local is being watched matters, so we
5304 keep track of the frame to evaluate the expression
5305 in. To evaluate the condition however, it doesn't
5306 really matter which instantiation of the function
5307 where the condition makes sense triggers the
5308 watchpoint. This allows an expression like "watch
5309 global if q > 10" set in `func', catch writes to
5310 global on all threads that call `func', or catch
5311 writes on all recursive calls of `func' by a single
5312 thread. We simply always evaluate the condition in
5313 the innermost frame that's executing where it makes
5314 sense to evaluate the condition. It seems
5315 intuitive. */
5316 frame = block_innermost_frame (w->cond_exp_valid_block);
5317 if (frame != NULL)
5318 select_frame (frame);
5319 else
5320 within_current_scope = 0;
5321 }
5322 if (within_current_scope)
5323 {
5324 try
5325 {
5326 condition_result = breakpoint_cond_eval (cond);
5327 }
5328 catch (const gdb_exception &ex)
5329 {
5330 exception_fprintf (gdb_stderr, ex,
5331 "Error in testing breakpoint condition:\n");
5332 }
5333 }
5334 else
5335 {
5336 warning (_("Watchpoint condition cannot be tested "
5337 "in the current scope"));
5338 /* If we failed to set the right context for this
5339 watchpoint, unconditionally report it. */
5340 }
5341 /* FIXME-someday, should give breakpoint #. */
5342 value_free_to_mark (mark);
5343 }
5344
5345 if (cond && !condition_result)
5346 {
5347 bs->stop = 0;
5348 }
5349 else if (b->ignore_count > 0)
5350 {
5351 b->ignore_count--;
5352 bs->stop = 0;
5353 /* Increase the hit count even though we don't stop. */
5354 ++(b->hit_count);
5355 gdb::observers::breakpoint_modified.notify (b);
5356 }
5357 }
5358
5359 /* Returns true if we need to track moribund locations of LOC's type
5360 on the current target. */
5361
5362 static int
5363 need_moribund_for_location_type (struct bp_location *loc)
5364 {
5365 return ((loc->loc_type == bp_loc_software_breakpoint
5366 && !target_supports_stopped_by_sw_breakpoint ())
5367 || (loc->loc_type == bp_loc_hardware_breakpoint
5368 && !target_supports_stopped_by_hw_breakpoint ()));
5369 }
5370
5371 /* See breakpoint.h. */
5372
5373 bpstat
5374 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5375 const struct target_waitstatus *ws)
5376 {
5377 bpstat bs_head = NULL, *bs_link = &bs_head;
5378
5379 for (breakpoint *b : all_breakpoints ())
5380 {
5381 if (!breakpoint_enabled (b))
5382 continue;
5383
5384 for (bp_location *bl : b->locations ())
5385 {
5386 /* For hardware watchpoints, we look only at the first
5387 location. The watchpoint_check function will work on the
5388 entire expression, not the individual locations. For
5389 read watchpoints, the watchpoints_triggered function has
5390 checked all locations already. */
5391 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5392 break;
5393
5394 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
5395 continue;
5396
5397 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5398 continue;
5399
5400 /* Come here if it's a watchpoint, or if the break address
5401 matches. */
5402
5403 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5404 explain stop. */
5405
5406 /* Assume we stop. Should we find a watchpoint that is not
5407 actually triggered, or if the condition of the breakpoint
5408 evaluates as false, we'll reset 'stop' to 0. */
5409 bs->stop = 1;
5410 bs->print = 1;
5411
5412 /* If this is a scope breakpoint, mark the associated
5413 watchpoint as triggered so that we will handle the
5414 out-of-scope event. We'll get to the watchpoint next
5415 iteration. */
5416 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5417 {
5418 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5419
5420 w->watchpoint_triggered = watch_triggered_yes;
5421 }
5422 }
5423 }
5424
5425 /* Check if a moribund breakpoint explains the stop. */
5426 if (!target_supports_stopped_by_sw_breakpoint ()
5427 || !target_supports_stopped_by_hw_breakpoint ())
5428 {
5429 for (bp_location *loc : moribund_locations)
5430 {
5431 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5432 && need_moribund_for_location_type (loc))
5433 {
5434 bpstat bs = new bpstats (loc, &bs_link);
5435 /* For hits of moribund locations, we should just proceed. */
5436 bs->stop = 0;
5437 bs->print = 0;
5438 bs->print_it = print_it_noop;
5439 }
5440 }
5441 }
5442
5443 return bs_head;
5444 }
5445
5446 /* See breakpoint.h. */
5447
5448 bpstat
5449 bpstat_stop_status (const address_space *aspace,
5450 CORE_ADDR bp_addr, thread_info *thread,
5451 const struct target_waitstatus *ws,
5452 bpstat stop_chain)
5453 {
5454 struct breakpoint *b = NULL;
5455 /* First item of allocated bpstat's. */
5456 bpstat bs_head = stop_chain;
5457 bpstat bs;
5458 int need_remove_insert;
5459 int removed_any;
5460
5461 /* First, build the bpstat chain with locations that explain a
5462 target stop, while being careful to not set the target running,
5463 as that may invalidate locations (in particular watchpoint
5464 locations are recreated). Resuming will happen here with
5465 breakpoint conditions or watchpoint expressions that include
5466 inferior function calls. */
5467 if (bs_head == NULL)
5468 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5469
5470 /* A bit of special processing for shlib breakpoints. We need to
5471 process solib loading here, so that the lists of loaded and
5472 unloaded libraries are correct before we handle "catch load" and
5473 "catch unload". */
5474 for (bs = bs_head; bs != NULL; bs = bs->next)
5475 {
5476 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5477 {
5478 handle_solib_event ();
5479 break;
5480 }
5481 }
5482
5483 /* Now go through the locations that caused the target to stop, and
5484 check whether we're interested in reporting this stop to higher
5485 layers, or whether we should resume the target transparently. */
5486
5487 removed_any = 0;
5488
5489 for (bs = bs_head; bs != NULL; bs = bs->next)
5490 {
5491 if (!bs->stop)
5492 continue;
5493
5494 b = bs->breakpoint_at;
5495 b->ops->check_status (bs);
5496 if (bs->stop)
5497 {
5498 bpstat_check_breakpoint_conditions (bs, thread);
5499
5500 if (bs->stop)
5501 {
5502 ++(b->hit_count);
5503 gdb::observers::breakpoint_modified.notify (b);
5504
5505 /* We will stop here. */
5506 if (b->disposition == disp_disable)
5507 {
5508 --(b->enable_count);
5509 if (b->enable_count <= 0)
5510 b->enable_state = bp_disabled;
5511 removed_any = 1;
5512 }
5513 if (b->silent)
5514 bs->print = 0;
5515 bs->commands = b->commands;
5516 if (command_line_is_silent (bs->commands
5517 ? bs->commands.get () : NULL))
5518 bs->print = 0;
5519
5520 b->ops->after_condition_true (bs);
5521 }
5522
5523 }
5524
5525 /* Print nothing for this entry if we don't stop or don't
5526 print. */
5527 if (!bs->stop || !bs->print)
5528 bs->print_it = print_it_noop;
5529 }
5530
5531 /* If we aren't stopping, the value of some hardware watchpoint may
5532 not have changed, but the intermediate memory locations we are
5533 watching may have. Don't bother if we're stopping; this will get
5534 done later. */
5535 need_remove_insert = 0;
5536 if (! bpstat_causes_stop (bs_head))
5537 for (bs = bs_head; bs != NULL; bs = bs->next)
5538 if (!bs->stop
5539 && bs->breakpoint_at
5540 && is_hardware_watchpoint (bs->breakpoint_at))
5541 {
5542 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5543
5544 update_watchpoint (w, 0 /* don't reparse. */);
5545 need_remove_insert = 1;
5546 }
5547
5548 if (need_remove_insert)
5549 update_global_location_list (UGLL_MAY_INSERT);
5550 else if (removed_any)
5551 update_global_location_list (UGLL_DONT_INSERT);
5552
5553 return bs_head;
5554 }
5555
5556 static void
5557 handle_jit_event (CORE_ADDR address)
5558 {
5559 struct gdbarch *gdbarch;
5560
5561 infrun_debug_printf ("handling bp_jit_event");
5562
5563 /* Switch terminal for any messages produced by
5564 breakpoint_re_set. */
5565 target_terminal::ours_for_output ();
5566
5567 gdbarch = get_frame_arch (get_current_frame ());
5568 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5569 thus it is expected that its objectfile can be found through
5570 minimal symbol lookup. If it doesn't work (and assert fails), it
5571 most likely means that `jit_breakpoint_re_set` was changes and this
5572 function needs to be updated too. */
5573 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5574 gdb_assert (jit_bp_sym.objfile != nullptr);
5575 jit_event_handler (gdbarch, jit_bp_sym.objfile);
5576
5577 target_terminal::inferior ();
5578 }
5579
5580 /* Prepare WHAT final decision for infrun. */
5581
5582 /* Decide what infrun needs to do with this bpstat. */
5583
5584 struct bpstat_what
5585 bpstat_what (bpstat bs_head)
5586 {
5587 struct bpstat_what retval;
5588 bpstat bs;
5589
5590 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5591 retval.call_dummy = STOP_NONE;
5592 retval.is_longjmp = false;
5593
5594 for (bs = bs_head; bs != NULL; bs = bs->next)
5595 {
5596 /* Extract this BS's action. After processing each BS, we check
5597 if its action overrides all we've seem so far. */
5598 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5599 enum bptype bptype;
5600
5601 if (bs->breakpoint_at == NULL)
5602 {
5603 /* I suspect this can happen if it was a momentary
5604 breakpoint which has since been deleted. */
5605 bptype = bp_none;
5606 }
5607 else
5608 bptype = bs->breakpoint_at->type;
5609
5610 switch (bptype)
5611 {
5612 case bp_none:
5613 break;
5614 case bp_breakpoint:
5615 case bp_hardware_breakpoint:
5616 case bp_single_step:
5617 case bp_until:
5618 case bp_finish:
5619 case bp_shlib_event:
5620 if (bs->stop)
5621 {
5622 if (bs->print)
5623 this_action = BPSTAT_WHAT_STOP_NOISY;
5624 else
5625 this_action = BPSTAT_WHAT_STOP_SILENT;
5626 }
5627 else
5628 this_action = BPSTAT_WHAT_SINGLE;
5629 break;
5630 case bp_watchpoint:
5631 case bp_hardware_watchpoint:
5632 case bp_read_watchpoint:
5633 case bp_access_watchpoint:
5634 if (bs->stop)
5635 {
5636 if (bs->print)
5637 this_action = BPSTAT_WHAT_STOP_NOISY;
5638 else
5639 this_action = BPSTAT_WHAT_STOP_SILENT;
5640 }
5641 else
5642 {
5643 /* There was a watchpoint, but we're not stopping.
5644 This requires no further action. */
5645 }
5646 break;
5647 case bp_longjmp:
5648 case bp_longjmp_call_dummy:
5649 case bp_exception:
5650 if (bs->stop)
5651 {
5652 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5653 retval.is_longjmp = bptype != bp_exception;
5654 }
5655 else
5656 this_action = BPSTAT_WHAT_SINGLE;
5657 break;
5658 case bp_longjmp_resume:
5659 case bp_exception_resume:
5660 if (bs->stop)
5661 {
5662 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5663 retval.is_longjmp = bptype == bp_longjmp_resume;
5664 }
5665 else
5666 this_action = BPSTAT_WHAT_SINGLE;
5667 break;
5668 case bp_step_resume:
5669 if (bs->stop)
5670 this_action = BPSTAT_WHAT_STEP_RESUME;
5671 else
5672 {
5673 /* It is for the wrong frame. */
5674 this_action = BPSTAT_WHAT_SINGLE;
5675 }
5676 break;
5677 case bp_hp_step_resume:
5678 if (bs->stop)
5679 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5680 else
5681 {
5682 /* It is for the wrong frame. */
5683 this_action = BPSTAT_WHAT_SINGLE;
5684 }
5685 break;
5686 case bp_watchpoint_scope:
5687 case bp_thread_event:
5688 case bp_overlay_event:
5689 case bp_longjmp_master:
5690 case bp_std_terminate_master:
5691 case bp_exception_master:
5692 this_action = BPSTAT_WHAT_SINGLE;
5693 break;
5694 case bp_catchpoint:
5695 if (bs->stop)
5696 {
5697 if (bs->print)
5698 this_action = BPSTAT_WHAT_STOP_NOISY;
5699 else
5700 this_action = BPSTAT_WHAT_STOP_SILENT;
5701 }
5702 else
5703 {
5704 /* Some catchpoints are implemented with breakpoints.
5705 For those, we need to step over the breakpoint. */
5706 if (bs->bp_location_at->loc_type != bp_loc_other)
5707 this_action = BPSTAT_WHAT_SINGLE;
5708 }
5709 break;
5710 case bp_jit_event:
5711 this_action = BPSTAT_WHAT_SINGLE;
5712 break;
5713 case bp_call_dummy:
5714 /* Make sure the action is stop (silent or noisy),
5715 so infrun.c pops the dummy frame. */
5716 retval.call_dummy = STOP_STACK_DUMMY;
5717 this_action = BPSTAT_WHAT_STOP_SILENT;
5718 break;
5719 case bp_std_terminate:
5720 /* Make sure the action is stop (silent or noisy),
5721 so infrun.c pops the dummy frame. */
5722 retval.call_dummy = STOP_STD_TERMINATE;
5723 this_action = BPSTAT_WHAT_STOP_SILENT;
5724 break;
5725 case bp_tracepoint:
5726 case bp_fast_tracepoint:
5727 case bp_static_tracepoint:
5728 /* Tracepoint hits should not be reported back to GDB, and
5729 if one got through somehow, it should have been filtered
5730 out already. */
5731 internal_error (__FILE__, __LINE__,
5732 _("bpstat_what: tracepoint encountered"));
5733 break;
5734 case bp_gnu_ifunc_resolver:
5735 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5736 this_action = BPSTAT_WHAT_SINGLE;
5737 break;
5738 case bp_gnu_ifunc_resolver_return:
5739 /* The breakpoint will be removed, execution will restart from the
5740 PC of the former breakpoint. */
5741 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5742 break;
5743
5744 case bp_dprintf:
5745 if (bs->stop)
5746 this_action = BPSTAT_WHAT_STOP_SILENT;
5747 else
5748 this_action = BPSTAT_WHAT_SINGLE;
5749 break;
5750
5751 default:
5752 internal_error (__FILE__, __LINE__,
5753 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5754 }
5755
5756 retval.main_action = std::max (retval.main_action, this_action);
5757 }
5758
5759 return retval;
5760 }
5761
5762 void
5763 bpstat_run_callbacks (bpstat bs_head)
5764 {
5765 bpstat bs;
5766
5767 for (bs = bs_head; bs != NULL; bs = bs->next)
5768 {
5769 struct breakpoint *b = bs->breakpoint_at;
5770
5771 if (b == NULL)
5772 continue;
5773 switch (b->type)
5774 {
5775 case bp_jit_event:
5776 handle_jit_event (bs->bp_location_at->address);
5777 break;
5778 case bp_gnu_ifunc_resolver:
5779 gnu_ifunc_resolver_stop (b);
5780 break;
5781 case bp_gnu_ifunc_resolver_return:
5782 gnu_ifunc_resolver_return_stop (b);
5783 break;
5784 }
5785 }
5786 }
5787
5788 /* See breakpoint.h. */
5789
5790 bool
5791 bpstat_should_step ()
5792 {
5793 for (breakpoint *b : all_breakpoints ())
5794 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5795 return true;
5796
5797 return false;
5798 }
5799
5800 /* See breakpoint.h. */
5801
5802 bool
5803 bpstat_causes_stop (bpstat bs)
5804 {
5805 for (; bs != NULL; bs = bs->next)
5806 if (bs->stop)
5807 return true;
5808
5809 return false;
5810 }
5811
5812 \f
5813
5814 /* Compute a string of spaces suitable to indent the next line
5815 so it starts at the position corresponding to the table column
5816 named COL_NAME in the currently active table of UIOUT. */
5817
5818 static char *
5819 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5820 {
5821 static char wrap_indent[80];
5822 int i, total_width, width, align;
5823 const char *text;
5824
5825 total_width = 0;
5826 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5827 {
5828 if (strcmp (text, col_name) == 0)
5829 {
5830 gdb_assert (total_width < sizeof wrap_indent);
5831 memset (wrap_indent, ' ', total_width);
5832 wrap_indent[total_width] = 0;
5833
5834 return wrap_indent;
5835 }
5836
5837 total_width += width + 1;
5838 }
5839
5840 return NULL;
5841 }
5842
5843 /* Determine if the locations of this breakpoint will have their conditions
5844 evaluated by the target, host or a mix of both. Returns the following:
5845
5846 "host": Host evals condition.
5847 "host or target": Host or Target evals condition.
5848 "target": Target evals condition.
5849 */
5850
5851 static const char *
5852 bp_condition_evaluator (struct breakpoint *b)
5853 {
5854 char host_evals = 0;
5855 char target_evals = 0;
5856
5857 if (!b)
5858 return NULL;
5859
5860 if (!is_breakpoint (b))
5861 return NULL;
5862
5863 if (gdb_evaluates_breakpoint_condition_p ()
5864 || !target_supports_evaluation_of_breakpoint_conditions ())
5865 return condition_evaluation_host;
5866
5867 for (bp_location *bl : b->locations ())
5868 {
5869 if (bl->cond_bytecode)
5870 target_evals++;
5871 else
5872 host_evals++;
5873 }
5874
5875 if (host_evals && target_evals)
5876 return condition_evaluation_both;
5877 else if (target_evals)
5878 return condition_evaluation_target;
5879 else
5880 return condition_evaluation_host;
5881 }
5882
5883 /* Determine the breakpoint location's condition evaluator. This is
5884 similar to bp_condition_evaluator, but for locations. */
5885
5886 static const char *
5887 bp_location_condition_evaluator (struct bp_location *bl)
5888 {
5889 if (bl && !is_breakpoint (bl->owner))
5890 return NULL;
5891
5892 if (gdb_evaluates_breakpoint_condition_p ()
5893 || !target_supports_evaluation_of_breakpoint_conditions ())
5894 return condition_evaluation_host;
5895
5896 if (bl && bl->cond_bytecode)
5897 return condition_evaluation_target;
5898 else
5899 return condition_evaluation_host;
5900 }
5901
5902 /* Print the LOC location out of the list of B->LOC locations. */
5903
5904 static void
5905 print_breakpoint_location (struct breakpoint *b,
5906 struct bp_location *loc)
5907 {
5908 struct ui_out *uiout = current_uiout;
5909
5910 scoped_restore_current_program_space restore_pspace;
5911
5912 if (loc != NULL && loc->shlib_disabled)
5913 loc = NULL;
5914
5915 if (loc != NULL)
5916 set_current_program_space (loc->pspace);
5917
5918 if (b->display_canonical)
5919 uiout->field_string ("what", event_location_to_string (b->location.get ()));
5920 else if (loc && loc->symtab)
5921 {
5922 const struct symbol *sym = loc->symbol;
5923
5924 if (sym)
5925 {
5926 uiout->text ("in ");
5927 uiout->field_string ("func", sym->print_name (),
5928 function_name_style.style ());
5929 uiout->text (" ");
5930 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5931 uiout->text ("at ");
5932 }
5933 uiout->field_string ("file",
5934 symtab_to_filename_for_display (loc->symtab),
5935 file_name_style.style ());
5936 uiout->text (":");
5937
5938 if (uiout->is_mi_like_p ())
5939 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
5940
5941 uiout->field_signed ("line", loc->line_number);
5942 }
5943 else if (loc)
5944 {
5945 string_file stb;
5946
5947 print_address_symbolic (loc->gdbarch, loc->address, &stb,
5948 demangle, "");
5949 uiout->field_stream ("at", stb);
5950 }
5951 else
5952 {
5953 uiout->field_string ("pending",
5954 event_location_to_string (b->location.get ()));
5955 /* If extra_string is available, it could be holding a condition
5956 or dprintf arguments. In either case, make sure it is printed,
5957 too, but only for non-MI streams. */
5958 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
5959 {
5960 if (b->type == bp_dprintf)
5961 uiout->text (",");
5962 else
5963 uiout->text (" ");
5964 uiout->text (b->extra_string);
5965 }
5966 }
5967
5968 if (loc && is_breakpoint (b)
5969 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5970 && bp_condition_evaluator (b) == condition_evaluation_both)
5971 {
5972 uiout->text (" (");
5973 uiout->field_string ("evaluated-by",
5974 bp_location_condition_evaluator (loc));
5975 uiout->text (")");
5976 }
5977 }
5978
5979 static const char *
5980 bptype_string (enum bptype type)
5981 {
5982 struct ep_type_description
5983 {
5984 enum bptype type;
5985 const char *description;
5986 };
5987 static struct ep_type_description bptypes[] =
5988 {
5989 {bp_none, "?deleted?"},
5990 {bp_breakpoint, "breakpoint"},
5991 {bp_hardware_breakpoint, "hw breakpoint"},
5992 {bp_single_step, "sw single-step"},
5993 {bp_until, "until"},
5994 {bp_finish, "finish"},
5995 {bp_watchpoint, "watchpoint"},
5996 {bp_hardware_watchpoint, "hw watchpoint"},
5997 {bp_read_watchpoint, "read watchpoint"},
5998 {bp_access_watchpoint, "acc watchpoint"},
5999 {bp_longjmp, "longjmp"},
6000 {bp_longjmp_resume, "longjmp resume"},
6001 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6002 {bp_exception, "exception"},
6003 {bp_exception_resume, "exception resume"},
6004 {bp_step_resume, "step resume"},
6005 {bp_hp_step_resume, "high-priority step resume"},
6006 {bp_watchpoint_scope, "watchpoint scope"},
6007 {bp_call_dummy, "call dummy"},
6008 {bp_std_terminate, "std::terminate"},
6009 {bp_shlib_event, "shlib events"},
6010 {bp_thread_event, "thread events"},
6011 {bp_overlay_event, "overlay events"},
6012 {bp_longjmp_master, "longjmp master"},
6013 {bp_std_terminate_master, "std::terminate master"},
6014 {bp_exception_master, "exception master"},
6015 {bp_catchpoint, "catchpoint"},
6016 {bp_tracepoint, "tracepoint"},
6017 {bp_fast_tracepoint, "fast tracepoint"},
6018 {bp_static_tracepoint, "static tracepoint"},
6019 {bp_dprintf, "dprintf"},
6020 {bp_jit_event, "jit events"},
6021 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6022 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6023 };
6024
6025 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6026 || ((int) type != bptypes[(int) type].type))
6027 internal_error (__FILE__, __LINE__,
6028 _("bptypes table does not describe type #%d."),
6029 (int) type);
6030
6031 return bptypes[(int) type].description;
6032 }
6033
6034 /* For MI, output a field named 'thread-groups' with a list as the value.
6035 For CLI, prefix the list with the string 'inf'. */
6036
6037 static void
6038 output_thread_groups (struct ui_out *uiout,
6039 const char *field_name,
6040 const std::vector<int> &inf_nums,
6041 int mi_only)
6042 {
6043 int is_mi = uiout->is_mi_like_p ();
6044
6045 /* For backward compatibility, don't display inferiors in CLI unless
6046 there are several. Always display them for MI. */
6047 if (!is_mi && mi_only)
6048 return;
6049
6050 ui_out_emit_list list_emitter (uiout, field_name);
6051
6052 for (size_t i = 0; i < inf_nums.size (); i++)
6053 {
6054 if (is_mi)
6055 {
6056 char mi_group[10];
6057
6058 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
6059 uiout->field_string (NULL, mi_group);
6060 }
6061 else
6062 {
6063 if (i == 0)
6064 uiout->text (" inf ");
6065 else
6066 uiout->text (", ");
6067
6068 uiout->text (plongest (inf_nums[i]));
6069 }
6070 }
6071 }
6072
6073 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6074 instead of going via breakpoint_ops::print_one. This makes "maint
6075 info breakpoints" show the software breakpoint locations of
6076 catchpoints, which are considered internal implementation
6077 detail. */
6078
6079 static void
6080 print_one_breakpoint_location (struct breakpoint *b,
6081 struct bp_location *loc,
6082 int loc_number,
6083 struct bp_location **last_loc,
6084 int allflag, bool raw_loc)
6085 {
6086 struct command_line *l;
6087 static char bpenables[] = "nynny";
6088
6089 struct ui_out *uiout = current_uiout;
6090 int header_of_multiple = 0;
6091 int part_of_multiple = (loc != NULL);
6092 struct value_print_options opts;
6093
6094 get_user_print_options (&opts);
6095
6096 gdb_assert (!loc || loc_number != 0);
6097 /* See comment in print_one_breakpoint concerning treatment of
6098 breakpoints with single disabled location. */
6099 if (loc == NULL
6100 && (b->loc != NULL
6101 && (b->loc->next != NULL
6102 || !b->loc->enabled || b->loc->disabled_by_cond)))
6103 header_of_multiple = 1;
6104 if (loc == NULL)
6105 loc = b->loc;
6106
6107 annotate_record ();
6108
6109 /* 1 */
6110 annotate_field (0);
6111 if (part_of_multiple)
6112 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6113 else
6114 uiout->field_signed ("number", b->number);
6115
6116 /* 2 */
6117 annotate_field (1);
6118 if (part_of_multiple)
6119 uiout->field_skip ("type");
6120 else
6121 uiout->field_string ("type", bptype_string (b->type));
6122
6123 /* 3 */
6124 annotate_field (2);
6125 if (part_of_multiple)
6126 uiout->field_skip ("disp");
6127 else
6128 uiout->field_string ("disp", bpdisp_text (b->disposition));
6129
6130 /* 4 */
6131 annotate_field (3);
6132 /* For locations that are disabled because of an invalid condition,
6133 display "N*" on CLI, where "*" refers to a footnote below the
6134 table. For MI, simply display a "N" without a footnote. */
6135 const char *N = (uiout->is_mi_like_p ()) ? "N" : "N*";
6136 if (part_of_multiple)
6137 uiout->field_string ("enabled", (loc->disabled_by_cond ? N
6138 : (loc->enabled ? "y" : "n")));
6139 else
6140 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6141
6142 /* 5 and 6 */
6143 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
6144 b->ops->print_one (b, last_loc);
6145 else
6146 {
6147 if (is_watchpoint (b))
6148 {
6149 struct watchpoint *w = (struct watchpoint *) b;
6150
6151 /* Field 4, the address, is omitted (which makes the columns
6152 not line up too nicely with the headers, but the effect
6153 is relatively readable). */
6154 if (opts.addressprint)
6155 uiout->field_skip ("addr");
6156 annotate_field (5);
6157 uiout->field_string ("what", w->exp_string);
6158 }
6159 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6160 || is_ada_exception_catchpoint (b))
6161 {
6162 if (opts.addressprint)
6163 {
6164 annotate_field (4);
6165 if (header_of_multiple)
6166 uiout->field_string ("addr", "<MULTIPLE>",
6167 metadata_style.style ());
6168 else if (b->loc == NULL || loc->shlib_disabled)
6169 uiout->field_string ("addr", "<PENDING>",
6170 metadata_style.style ());
6171 else
6172 uiout->field_core_addr ("addr",
6173 loc->gdbarch, loc->address);
6174 }
6175 annotate_field (5);
6176 if (!header_of_multiple)
6177 print_breakpoint_location (b, loc);
6178 if (b->loc)
6179 *last_loc = b->loc;
6180 }
6181 }
6182
6183 if (loc != NULL && !header_of_multiple)
6184 {
6185 std::vector<int> inf_nums;
6186 int mi_only = 1;
6187
6188 for (inferior *inf : all_inferiors ())
6189 {
6190 if (inf->pspace == loc->pspace)
6191 inf_nums.push_back (inf->num);
6192 }
6193
6194 /* For backward compatibility, don't display inferiors in CLI unless
6195 there are several. Always display for MI. */
6196 if (allflag
6197 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6198 && (program_spaces.size () > 1
6199 || number_of_inferiors () > 1)
6200 /* LOC is for existing B, it cannot be in
6201 moribund_locations and thus having NULL OWNER. */
6202 && loc->owner->type != bp_catchpoint))
6203 mi_only = 0;
6204 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6205 }
6206
6207 if (!part_of_multiple)
6208 {
6209 if (b->thread != -1)
6210 {
6211 /* FIXME: This seems to be redundant and lost here; see the
6212 "stop only in" line a little further down. */
6213 uiout->text (" thread ");
6214 uiout->field_signed ("thread", b->thread);
6215 }
6216 else if (b->task != 0)
6217 {
6218 uiout->text (" task ");
6219 uiout->field_signed ("task", b->task);
6220 }
6221 }
6222
6223 uiout->text ("\n");
6224
6225 if (!part_of_multiple)
6226 b->ops->print_one_detail (b, uiout);
6227
6228 if (part_of_multiple && frame_id_p (b->frame_id))
6229 {
6230 annotate_field (6);
6231 uiout->text ("\tstop only in stack frame at ");
6232 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6233 the frame ID. */
6234 uiout->field_core_addr ("frame",
6235 b->gdbarch, b->frame_id.stack_addr);
6236 uiout->text ("\n");
6237 }
6238
6239 if (!part_of_multiple && b->cond_string)
6240 {
6241 annotate_field (7);
6242 if (is_tracepoint (b))
6243 uiout->text ("\ttrace only if ");
6244 else
6245 uiout->text ("\tstop only if ");
6246 uiout->field_string ("cond", b->cond_string);
6247
6248 /* Print whether the target is doing the breakpoint's condition
6249 evaluation. If GDB is doing the evaluation, don't print anything. */
6250 if (is_breakpoint (b)
6251 && breakpoint_condition_evaluation_mode ()
6252 == condition_evaluation_target)
6253 {
6254 uiout->message (" (%pF evals)",
6255 string_field ("evaluated-by",
6256 bp_condition_evaluator (b)));
6257 }
6258 uiout->text ("\n");
6259 }
6260
6261 if (!part_of_multiple && b->thread != -1)
6262 {
6263 /* FIXME should make an annotation for this. */
6264 uiout->text ("\tstop only in thread ");
6265 if (uiout->is_mi_like_p ())
6266 uiout->field_signed ("thread", b->thread);
6267 else
6268 {
6269 struct thread_info *thr = find_thread_global_id (b->thread);
6270
6271 uiout->field_string ("thread", print_thread_id (thr));
6272 }
6273 uiout->text ("\n");
6274 }
6275
6276 if (!part_of_multiple)
6277 {
6278 if (b->hit_count)
6279 {
6280 /* FIXME should make an annotation for this. */
6281 if (is_catchpoint (b))
6282 uiout->text ("\tcatchpoint");
6283 else if (is_tracepoint (b))
6284 uiout->text ("\ttracepoint");
6285 else
6286 uiout->text ("\tbreakpoint");
6287 uiout->text (" already hit ");
6288 uiout->field_signed ("times", b->hit_count);
6289 if (b->hit_count == 1)
6290 uiout->text (" time\n");
6291 else
6292 uiout->text (" times\n");
6293 }
6294 else
6295 {
6296 /* Output the count also if it is zero, but only if this is mi. */
6297 if (uiout->is_mi_like_p ())
6298 uiout->field_signed ("times", b->hit_count);
6299 }
6300 }
6301
6302 if (!part_of_multiple && b->ignore_count)
6303 {
6304 annotate_field (8);
6305 uiout->message ("\tignore next %pF hits\n",
6306 signed_field ("ignore", b->ignore_count));
6307 }
6308
6309 /* Note that an enable count of 1 corresponds to "enable once"
6310 behavior, which is reported by the combination of enablement and
6311 disposition, so we don't need to mention it here. */
6312 if (!part_of_multiple && b->enable_count > 1)
6313 {
6314 annotate_field (8);
6315 uiout->text ("\tdisable after ");
6316 /* Tweak the wording to clarify that ignore and enable counts
6317 are distinct, and have additive effect. */
6318 if (b->ignore_count)
6319 uiout->text ("additional ");
6320 else
6321 uiout->text ("next ");
6322 uiout->field_signed ("enable", b->enable_count);
6323 uiout->text (" hits\n");
6324 }
6325
6326 if (!part_of_multiple && is_tracepoint (b))
6327 {
6328 struct tracepoint *tp = (struct tracepoint *) b;
6329
6330 if (tp->traceframe_usage)
6331 {
6332 uiout->text ("\ttrace buffer usage ");
6333 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6334 uiout->text (" bytes\n");
6335 }
6336 }
6337
6338 l = b->commands ? b->commands.get () : NULL;
6339 if (!part_of_multiple && l)
6340 {
6341 annotate_field (9);
6342 ui_out_emit_tuple tuple_emitter (uiout, "script");
6343 print_command_lines (uiout, l, 4);
6344 }
6345
6346 if (is_tracepoint (b))
6347 {
6348 struct tracepoint *t = (struct tracepoint *) b;
6349
6350 if (!part_of_multiple && t->pass_count)
6351 {
6352 annotate_field (10);
6353 uiout->text ("\tpass count ");
6354 uiout->field_signed ("pass", t->pass_count);
6355 uiout->text (" \n");
6356 }
6357
6358 /* Don't display it when tracepoint or tracepoint location is
6359 pending. */
6360 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6361 {
6362 annotate_field (11);
6363
6364 if (uiout->is_mi_like_p ())
6365 uiout->field_string ("installed",
6366 loc->inserted ? "y" : "n");
6367 else
6368 {
6369 if (loc->inserted)
6370 uiout->text ("\t");
6371 else
6372 uiout->text ("\tnot ");
6373 uiout->text ("installed on target\n");
6374 }
6375 }
6376 }
6377
6378 if (uiout->is_mi_like_p () && !part_of_multiple)
6379 {
6380 if (is_watchpoint (b))
6381 {
6382 struct watchpoint *w = (struct watchpoint *) b;
6383
6384 uiout->field_string ("original-location", w->exp_string);
6385 }
6386 else if (b->location != NULL
6387 && event_location_to_string (b->location.get ()) != NULL)
6388 uiout->field_string ("original-location",
6389 event_location_to_string (b->location.get ()));
6390 }
6391 }
6392
6393 /* See breakpoint.h. */
6394
6395 bool fix_multi_location_breakpoint_output_globally = false;
6396
6397 static void
6398 print_one_breakpoint (struct breakpoint *b,
6399 struct bp_location **last_loc,
6400 int allflag)
6401 {
6402 struct ui_out *uiout = current_uiout;
6403 bool use_fixed_output
6404 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6405 || fix_multi_location_breakpoint_output_globally);
6406
6407 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6408 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
6409
6410 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6411 are outside. */
6412 if (!use_fixed_output)
6413 bkpt_tuple_emitter.reset ();
6414
6415 /* If this breakpoint has custom print function,
6416 it's already printed. Otherwise, print individual
6417 locations, if any. */
6418 if (b->ops == NULL
6419 || b->ops->print_one == NULL
6420 || allflag)
6421 {
6422 /* If breakpoint has a single location that is disabled, we
6423 print it as if it had several locations, since otherwise it's
6424 hard to represent "breakpoint enabled, location disabled"
6425 situation.
6426
6427 Note that while hardware watchpoints have several locations
6428 internally, that's not a property exposed to users.
6429
6430 Likewise, while catchpoints may be implemented with
6431 breakpoints (e.g., catch throw), that's not a property
6432 exposed to users. We do however display the internal
6433 breakpoint locations with "maint info breakpoints". */
6434 if (!is_hardware_watchpoint (b)
6435 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6436 || is_ada_exception_catchpoint (b))
6437 && (allflag
6438 || (b->loc && (b->loc->next
6439 || !b->loc->enabled
6440 || b->loc->disabled_by_cond))))
6441 {
6442 gdb::optional<ui_out_emit_list> locations_list;
6443
6444 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6445 MI record. For later versions, place breakpoint locations in a
6446 list. */
6447 if (uiout->is_mi_like_p () && use_fixed_output)
6448 locations_list.emplace (uiout, "locations");
6449
6450 int n = 1;
6451 for (bp_location *loc : b->locations ())
6452 {
6453 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6454 print_one_breakpoint_location (b, loc, n, last_loc,
6455 allflag, allflag);
6456 n++;
6457 }
6458 }
6459 }
6460 }
6461
6462 static int
6463 breakpoint_address_bits (struct breakpoint *b)
6464 {
6465 int print_address_bits = 0;
6466
6467 /* Software watchpoints that aren't watching memory don't have an
6468 address to print. */
6469 if (is_no_memory_software_watchpoint (b))
6470 return 0;
6471
6472 for (bp_location *loc : b->locations ())
6473 {
6474 int addr_bit;
6475
6476 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6477 if (addr_bit > print_address_bits)
6478 print_address_bits = addr_bit;
6479 }
6480
6481 return print_address_bits;
6482 }
6483
6484 /* See breakpoint.h. */
6485
6486 void
6487 print_breakpoint (breakpoint *b)
6488 {
6489 struct bp_location *dummy_loc = NULL;
6490 print_one_breakpoint (b, &dummy_loc, 0);
6491 }
6492
6493 /* Return true if this breakpoint was set by the user, false if it is
6494 internal or momentary. */
6495
6496 int
6497 user_breakpoint_p (struct breakpoint *b)
6498 {
6499 return b->number > 0;
6500 }
6501
6502 /* See breakpoint.h. */
6503
6504 int
6505 pending_breakpoint_p (struct breakpoint *b)
6506 {
6507 return b->loc == NULL;
6508 }
6509
6510 /* Print information on breakpoints (including watchpoints and tracepoints).
6511
6512 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6513 understood by number_or_range_parser. Only breakpoints included in this
6514 list are then printed.
6515
6516 If SHOW_INTERNAL is true, print internal breakpoints.
6517
6518 If FILTER is non-NULL, call it on each breakpoint and only include the
6519 ones for which it returns true.
6520
6521 Return the total number of breakpoints listed. */
6522
6523 static int
6524 breakpoint_1 (const char *bp_num_list, bool show_internal,
6525 bool (*filter) (const struct breakpoint *))
6526 {
6527 struct bp_location *last_loc = NULL;
6528 int nr_printable_breakpoints;
6529 struct value_print_options opts;
6530 int print_address_bits = 0;
6531 int print_type_col_width = 14;
6532 struct ui_out *uiout = current_uiout;
6533 bool has_disabled_by_cond_location = false;
6534
6535 get_user_print_options (&opts);
6536
6537 /* Compute the number of rows in the table, as well as the size
6538 required for address fields. */
6539 nr_printable_breakpoints = 0;
6540 for (breakpoint *b : all_breakpoints ())
6541 {
6542 /* If we have a filter, only list the breakpoints it accepts. */
6543 if (filter && !filter (b))
6544 continue;
6545
6546 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6547 accept. Skip the others. */
6548 if (bp_num_list != NULL && *bp_num_list != '\0')
6549 {
6550 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6551 continue;
6552 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6553 continue;
6554 }
6555
6556 if (show_internal || user_breakpoint_p (b))
6557 {
6558 int addr_bit, type_len;
6559
6560 addr_bit = breakpoint_address_bits (b);
6561 if (addr_bit > print_address_bits)
6562 print_address_bits = addr_bit;
6563
6564 type_len = strlen (bptype_string (b->type));
6565 if (type_len > print_type_col_width)
6566 print_type_col_width = type_len;
6567
6568 nr_printable_breakpoints++;
6569 }
6570 }
6571
6572 {
6573 ui_out_emit_table table_emitter (uiout,
6574 opts.addressprint ? 6 : 5,
6575 nr_printable_breakpoints,
6576 "BreakpointTable");
6577
6578 if (nr_printable_breakpoints > 0)
6579 annotate_breakpoints_headers ();
6580 if (nr_printable_breakpoints > 0)
6581 annotate_field (0);
6582 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6583 if (nr_printable_breakpoints > 0)
6584 annotate_field (1);
6585 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6586 if (nr_printable_breakpoints > 0)
6587 annotate_field (2);
6588 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6589 if (nr_printable_breakpoints > 0)
6590 annotate_field (3);
6591 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6592 if (opts.addressprint)
6593 {
6594 if (nr_printable_breakpoints > 0)
6595 annotate_field (4);
6596 if (print_address_bits <= 32)
6597 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6598 else
6599 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6600 }
6601 if (nr_printable_breakpoints > 0)
6602 annotate_field (5);
6603 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6604 uiout->table_body ();
6605 if (nr_printable_breakpoints > 0)
6606 annotate_breakpoints_table ();
6607
6608 for (breakpoint *b : all_breakpoints ())
6609 {
6610 QUIT;
6611 /* If we have a filter, only list the breakpoints it accepts. */
6612 if (filter && !filter (b))
6613 continue;
6614
6615 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6616 accept. Skip the others. */
6617
6618 if (bp_num_list != NULL && *bp_num_list != '\0')
6619 {
6620 if (show_internal) /* maintenance info breakpoint */
6621 {
6622 if (parse_and_eval_long (bp_num_list) != b->number)
6623 continue;
6624 }
6625 else /* all others */
6626 {
6627 if (!number_is_in_list (bp_num_list, b->number))
6628 continue;
6629 }
6630 }
6631 /* We only print out user settable breakpoints unless the
6632 show_internal is set. */
6633 if (show_internal || user_breakpoint_p (b))
6634 {
6635 print_one_breakpoint (b, &last_loc, show_internal);
6636 for (bp_location *loc : b->locations ())
6637 if (loc->disabled_by_cond)
6638 has_disabled_by_cond_location = true;
6639 }
6640 }
6641 }
6642
6643 if (nr_printable_breakpoints == 0)
6644 {
6645 /* If there's a filter, let the caller decide how to report
6646 empty list. */
6647 if (!filter)
6648 {
6649 if (bp_num_list == NULL || *bp_num_list == '\0')
6650 uiout->message ("No breakpoints or watchpoints.\n");
6651 else
6652 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6653 bp_num_list);
6654 }
6655 }
6656 else
6657 {
6658 if (last_loc && !server_command)
6659 set_next_address (last_loc->gdbarch, last_loc->address);
6660
6661 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
6662 uiout->message (_("(*): Breakpoint condition is invalid at this "
6663 "location.\n"));
6664 }
6665
6666 /* FIXME? Should this be moved up so that it is only called when
6667 there have been breakpoints? */
6668 annotate_breakpoints_table_end ();
6669
6670 return nr_printable_breakpoints;
6671 }
6672
6673 /* Display the value of default-collect in a way that is generally
6674 compatible with the breakpoint list. */
6675
6676 static void
6677 default_collect_info (void)
6678 {
6679 struct ui_out *uiout = current_uiout;
6680
6681 /* If it has no value (which is frequently the case), say nothing; a
6682 message like "No default-collect." gets in user's face when it's
6683 not wanted. */
6684 if (!*default_collect)
6685 return;
6686
6687 /* The following phrase lines up nicely with per-tracepoint collect
6688 actions. */
6689 uiout->text ("default collect ");
6690 uiout->field_string ("default-collect", default_collect);
6691 uiout->text (" \n");
6692 }
6693
6694 static void
6695 info_breakpoints_command (const char *args, int from_tty)
6696 {
6697 breakpoint_1 (args, false, NULL);
6698
6699 default_collect_info ();
6700 }
6701
6702 static void
6703 info_watchpoints_command (const char *args, int from_tty)
6704 {
6705 int num_printed = breakpoint_1 (args, false, is_watchpoint);
6706 struct ui_out *uiout = current_uiout;
6707
6708 if (num_printed == 0)
6709 {
6710 if (args == NULL || *args == '\0')
6711 uiout->message ("No watchpoints.\n");
6712 else
6713 uiout->message ("No watchpoint matching '%s'.\n", args);
6714 }
6715 }
6716
6717 static void
6718 maintenance_info_breakpoints (const char *args, int from_tty)
6719 {
6720 breakpoint_1 (args, true, NULL);
6721
6722 default_collect_info ();
6723 }
6724
6725 static int
6726 breakpoint_has_pc (struct breakpoint *b,
6727 struct program_space *pspace,
6728 CORE_ADDR pc, struct obj_section *section)
6729 {
6730 for (bp_location *bl : b->locations ())
6731 {
6732 if (bl->pspace == pspace
6733 && bl->address == pc
6734 && (!overlay_debugging || bl->section == section))
6735 return 1;
6736 }
6737 return 0;
6738 }
6739
6740 /* Print a message describing any user-breakpoints set at PC. This
6741 concerns with logical breakpoints, so we match program spaces, not
6742 address spaces. */
6743
6744 static void
6745 describe_other_breakpoints (struct gdbarch *gdbarch,
6746 struct program_space *pspace, CORE_ADDR pc,
6747 struct obj_section *section, int thread)
6748 {
6749 int others = 0;
6750
6751 for (breakpoint *b : all_breakpoints ())
6752 others += (user_breakpoint_p (b)
6753 && breakpoint_has_pc (b, pspace, pc, section));
6754
6755 if (others > 0)
6756 {
6757 if (others == 1)
6758 printf_filtered (_("Note: breakpoint "));
6759 else /* if (others == ???) */
6760 printf_filtered (_("Note: breakpoints "));
6761 for (breakpoint *b : all_breakpoints ())
6762 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6763 {
6764 others--;
6765 printf_filtered ("%d", b->number);
6766 if (b->thread == -1 && thread != -1)
6767 printf_filtered (" (all threads)");
6768 else if (b->thread != -1)
6769 printf_filtered (" (thread %d)", b->thread);
6770 printf_filtered ("%s%s ",
6771 ((b->enable_state == bp_disabled
6772 || b->enable_state == bp_call_disabled)
6773 ? " (disabled)"
6774 : ""),
6775 (others > 1) ? ","
6776 : ((others == 1) ? " and" : ""));
6777 }
6778 current_uiout->message (_("also set at pc %ps.\n"),
6779 styled_string (address_style.style (),
6780 paddress (gdbarch, pc)));
6781 }
6782 }
6783 \f
6784
6785 /* Return true iff it is meaningful to use the address member of LOC.
6786 For some breakpoint types, the locations' address members are
6787 irrelevant and it makes no sense to attempt to compare them to
6788 other addresses (or use them for any other purpose either).
6789
6790 More specifically, software watchpoints and catchpoints that are
6791 not backed by breakpoints always have a zero valued location
6792 address and we don't want to mark breakpoints of any of these types
6793 to be a duplicate of an actual breakpoint location at address
6794 zero. */
6795
6796 static bool
6797 bl_address_is_meaningful (bp_location *loc)
6798 {
6799 return loc->loc_type != bp_loc_other;
6800 }
6801
6802 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6803 true if LOC1 and LOC2 represent the same watchpoint location. */
6804
6805 static int
6806 watchpoint_locations_match (struct bp_location *loc1,
6807 struct bp_location *loc2)
6808 {
6809 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6810 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6811
6812 /* Both of them must exist. */
6813 gdb_assert (w1 != NULL);
6814 gdb_assert (w2 != NULL);
6815
6816 /* If the target can evaluate the condition expression in hardware,
6817 then we we need to insert both watchpoints even if they are at
6818 the same place. Otherwise the watchpoint will only trigger when
6819 the condition of whichever watchpoint was inserted evaluates to
6820 true, not giving a chance for GDB to check the condition of the
6821 other watchpoint. */
6822 if ((w1->cond_exp
6823 && target_can_accel_watchpoint_condition (loc1->address,
6824 loc1->length,
6825 loc1->watchpoint_type,
6826 w1->cond_exp.get ()))
6827 || (w2->cond_exp
6828 && target_can_accel_watchpoint_condition (loc2->address,
6829 loc2->length,
6830 loc2->watchpoint_type,
6831 w2->cond_exp.get ())))
6832 return 0;
6833
6834 /* Note that this checks the owner's type, not the location's. In
6835 case the target does not support read watchpoints, but does
6836 support access watchpoints, we'll have bp_read_watchpoint
6837 watchpoints with hw_access locations. Those should be considered
6838 duplicates of hw_read locations. The hw_read locations will
6839 become hw_access locations later. */
6840 return (loc1->owner->type == loc2->owner->type
6841 && loc1->pspace->aspace == loc2->pspace->aspace
6842 && loc1->address == loc2->address
6843 && loc1->length == loc2->length);
6844 }
6845
6846 /* See breakpoint.h. */
6847
6848 int
6849 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6850 const address_space *aspace2, CORE_ADDR addr2)
6851 {
6852 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6853 || aspace1 == aspace2)
6854 && addr1 == addr2);
6855 }
6856
6857 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6858 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6859 matches ASPACE2. On targets that have global breakpoints, the address
6860 space doesn't really matter. */
6861
6862 static int
6863 breakpoint_address_match_range (const address_space *aspace1,
6864 CORE_ADDR addr1,
6865 int len1, const address_space *aspace2,
6866 CORE_ADDR addr2)
6867 {
6868 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6869 || aspace1 == aspace2)
6870 && addr2 >= addr1 && addr2 < addr1 + len1);
6871 }
6872
6873 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6874 a ranged breakpoint. In most targets, a match happens only if ASPACE
6875 matches the breakpoint's address space. On targets that have global
6876 breakpoints, the address space doesn't really matter. */
6877
6878 static int
6879 breakpoint_location_address_match (struct bp_location *bl,
6880 const address_space *aspace,
6881 CORE_ADDR addr)
6882 {
6883 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6884 aspace, addr)
6885 || (bl->length
6886 && breakpoint_address_match_range (bl->pspace->aspace,
6887 bl->address, bl->length,
6888 aspace, addr)));
6889 }
6890
6891 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6892 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6893 match happens only if ASPACE matches the breakpoint's address
6894 space. On targets that have global breakpoints, the address space
6895 doesn't really matter. */
6896
6897 static int
6898 breakpoint_location_address_range_overlap (struct bp_location *bl,
6899 const address_space *aspace,
6900 CORE_ADDR addr, int len)
6901 {
6902 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6903 || bl->pspace->aspace == aspace)
6904 {
6905 int bl_len = bl->length != 0 ? bl->length : 1;
6906
6907 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6908 return 1;
6909 }
6910 return 0;
6911 }
6912
6913 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6914 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6915 true, otherwise returns false. */
6916
6917 static int
6918 tracepoint_locations_match (struct bp_location *loc1,
6919 struct bp_location *loc2)
6920 {
6921 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6922 /* Since tracepoint locations are never duplicated with others', tracepoint
6923 locations at the same address of different tracepoints are regarded as
6924 different locations. */
6925 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6926 else
6927 return 0;
6928 }
6929
6930 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6931 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
6932 the same location. If SW_HW_BPS_MATCH is true, then software
6933 breakpoint locations and hardware breakpoint locations match,
6934 otherwise they don't. */
6935
6936 static int
6937 breakpoint_locations_match (struct bp_location *loc1,
6938 struct bp_location *loc2,
6939 bool sw_hw_bps_match)
6940 {
6941 int hw_point1, hw_point2;
6942
6943 /* Both of them must not be in moribund_locations. */
6944 gdb_assert (loc1->owner != NULL);
6945 gdb_assert (loc2->owner != NULL);
6946
6947 hw_point1 = is_hardware_watchpoint (loc1->owner);
6948 hw_point2 = is_hardware_watchpoint (loc2->owner);
6949
6950 if (hw_point1 != hw_point2)
6951 return 0;
6952 else if (hw_point1)
6953 return watchpoint_locations_match (loc1, loc2);
6954 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6955 return tracepoint_locations_match (loc1, loc2);
6956 else
6957 /* We compare bp_location.length in order to cover ranged
6958 breakpoints. Keep this in sync with
6959 bp_location_is_less_than. */
6960 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6961 loc2->pspace->aspace, loc2->address)
6962 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
6963 && loc1->length == loc2->length);
6964 }
6965
6966 static void
6967 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6968 int bnum, int have_bnum)
6969 {
6970 /* The longest string possibly returned by hex_string_custom
6971 is 50 chars. These must be at least that big for safety. */
6972 char astr1[64];
6973 char astr2[64];
6974
6975 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6976 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
6977 if (have_bnum)
6978 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
6979 bnum, astr1, astr2);
6980 else
6981 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
6982 }
6983
6984 /* Adjust a breakpoint's address to account for architectural
6985 constraints on breakpoint placement. Return the adjusted address.
6986 Note: Very few targets require this kind of adjustment. For most
6987 targets, this function is simply the identity function. */
6988
6989 static CORE_ADDR
6990 adjust_breakpoint_address (struct gdbarch *gdbarch,
6991 CORE_ADDR bpaddr, enum bptype bptype)
6992 {
6993 if (bptype == bp_watchpoint
6994 || bptype == bp_hardware_watchpoint
6995 || bptype == bp_read_watchpoint
6996 || bptype == bp_access_watchpoint
6997 || bptype == bp_catchpoint)
6998 {
6999 /* Watchpoints and the various bp_catch_* eventpoints should not
7000 have their addresses modified. */
7001 return bpaddr;
7002 }
7003 else if (bptype == bp_single_step)
7004 {
7005 /* Single-step breakpoints should not have their addresses
7006 modified. If there's any architectural constrain that
7007 applies to this address, then it should have already been
7008 taken into account when the breakpoint was created in the
7009 first place. If we didn't do this, stepping through e.g.,
7010 Thumb-2 IT blocks would break. */
7011 return bpaddr;
7012 }
7013 else
7014 {
7015 CORE_ADDR adjusted_bpaddr = bpaddr;
7016
7017 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7018 {
7019 /* Some targets have architectural constraints on the placement
7020 of breakpoint instructions. Obtain the adjusted address. */
7021 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7022 }
7023
7024 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
7025
7026 /* An adjusted breakpoint address can significantly alter
7027 a user's expectations. Print a warning if an adjustment
7028 is required. */
7029 if (adjusted_bpaddr != bpaddr)
7030 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7031
7032 return adjusted_bpaddr;
7033 }
7034 }
7035
7036 static bp_loc_type
7037 bp_location_from_bp_type (bptype type)
7038 {
7039 switch (type)
7040 {
7041 case bp_breakpoint:
7042 case bp_single_step:
7043 case bp_until:
7044 case bp_finish:
7045 case bp_longjmp:
7046 case bp_longjmp_resume:
7047 case bp_longjmp_call_dummy:
7048 case bp_exception:
7049 case bp_exception_resume:
7050 case bp_step_resume:
7051 case bp_hp_step_resume:
7052 case bp_watchpoint_scope:
7053 case bp_call_dummy:
7054 case bp_std_terminate:
7055 case bp_shlib_event:
7056 case bp_thread_event:
7057 case bp_overlay_event:
7058 case bp_jit_event:
7059 case bp_longjmp_master:
7060 case bp_std_terminate_master:
7061 case bp_exception_master:
7062 case bp_gnu_ifunc_resolver:
7063 case bp_gnu_ifunc_resolver_return:
7064 case bp_dprintf:
7065 return bp_loc_software_breakpoint;
7066 case bp_hardware_breakpoint:
7067 return bp_loc_hardware_breakpoint;
7068 case bp_hardware_watchpoint:
7069 case bp_read_watchpoint:
7070 case bp_access_watchpoint:
7071 return bp_loc_hardware_watchpoint;
7072 case bp_watchpoint:
7073 case bp_catchpoint:
7074 case bp_tracepoint:
7075 case bp_fast_tracepoint:
7076 case bp_static_tracepoint:
7077 return bp_loc_other;
7078 default:
7079 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7080 }
7081 }
7082
7083 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7084 {
7085 this->owner = owner;
7086 this->cond_bytecode = NULL;
7087 this->shlib_disabled = 0;
7088 this->enabled = 1;
7089 this->disabled_by_cond = false;
7090
7091 this->loc_type = type;
7092
7093 if (this->loc_type == bp_loc_software_breakpoint
7094 || this->loc_type == bp_loc_hardware_breakpoint)
7095 mark_breakpoint_location_modified (this);
7096
7097 incref ();
7098 }
7099
7100 bp_location::bp_location (breakpoint *owner)
7101 : bp_location::bp_location (owner,
7102 bp_location_from_bp_type (owner->type))
7103 {
7104 }
7105
7106 /* Allocate a struct bp_location. */
7107
7108 static struct bp_location *
7109 allocate_bp_location (struct breakpoint *bpt)
7110 {
7111 return bpt->ops->allocate_location (bpt);
7112 }
7113
7114 /* Decrement reference count. If the reference count reaches 0,
7115 destroy the bp_location. Sets *BLP to NULL. */
7116
7117 static void
7118 decref_bp_location (struct bp_location **blp)
7119 {
7120 bp_location_ref_policy::decref (*blp);
7121 *blp = NULL;
7122 }
7123
7124 /* Add breakpoint B at the end of the global breakpoint chain. */
7125
7126 static breakpoint *
7127 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7128 {
7129 struct breakpoint *b1;
7130 struct breakpoint *result = b.get ();
7131
7132 /* Add this breakpoint to the end of the chain so that a list of
7133 breakpoints will come out in order of increasing numbers. */
7134
7135 b1 = breakpoint_chain;
7136 if (b1 == 0)
7137 breakpoint_chain = b.release ();
7138 else
7139 {
7140 while (b1->next)
7141 b1 = b1->next;
7142 b1->next = b.release ();
7143 }
7144
7145 return result;
7146 }
7147
7148 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7149
7150 static void
7151 init_raw_breakpoint_without_location (struct breakpoint *b,
7152 struct gdbarch *gdbarch,
7153 enum bptype bptype,
7154 const struct breakpoint_ops *ops)
7155 {
7156 gdb_assert (ops != NULL);
7157
7158 b->ops = ops;
7159 b->type = bptype;
7160 b->gdbarch = gdbarch;
7161 b->language = current_language->la_language;
7162 b->input_radix = input_radix;
7163 b->related_breakpoint = b;
7164 }
7165
7166 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7167 that has type BPTYPE and has no locations as yet. */
7168
7169 static struct breakpoint *
7170 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7171 enum bptype bptype,
7172 const struct breakpoint_ops *ops)
7173 {
7174 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7175
7176 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7177 return add_to_breakpoint_chain (std::move (b));
7178 }
7179
7180 /* Initialize loc->function_name. */
7181
7182 static void
7183 set_breakpoint_location_function (struct bp_location *loc)
7184 {
7185 gdb_assert (loc->owner != NULL);
7186
7187 if (loc->owner->type == bp_breakpoint
7188 || loc->owner->type == bp_hardware_breakpoint
7189 || is_tracepoint (loc->owner))
7190 {
7191 const char *function_name;
7192
7193 if (loc->msymbol != NULL
7194 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
7195 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
7196 {
7197 struct breakpoint *b = loc->owner;
7198
7199 function_name = loc->msymbol->linkage_name ();
7200
7201 if (b->type == bp_breakpoint && b->loc == loc
7202 && loc->next == NULL && b->related_breakpoint == b)
7203 {
7204 /* Create only the whole new breakpoint of this type but do not
7205 mess more complicated breakpoints with multiple locations. */
7206 b->type = bp_gnu_ifunc_resolver;
7207 /* Remember the resolver's address for use by the return
7208 breakpoint. */
7209 loc->related_address = loc->address;
7210 }
7211 }
7212 else
7213 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7214
7215 if (function_name)
7216 loc->function_name = xstrdup (function_name);
7217 }
7218 }
7219
7220 /* Attempt to determine architecture of location identified by SAL. */
7221 struct gdbarch *
7222 get_sal_arch (struct symtab_and_line sal)
7223 {
7224 if (sal.section)
7225 return sal.section->objfile->arch ();
7226 if (sal.symtab)
7227 return SYMTAB_OBJFILE (sal.symtab)->arch ();
7228
7229 return NULL;
7230 }
7231
7232 /* Low level routine for partially initializing a breakpoint of type
7233 BPTYPE. The newly created breakpoint's address, section, source
7234 file name, and line number are provided by SAL.
7235
7236 It is expected that the caller will complete the initialization of
7237 the newly created breakpoint struct as well as output any status
7238 information regarding the creation of a new breakpoint. */
7239
7240 static void
7241 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7242 struct symtab_and_line sal, enum bptype bptype,
7243 const struct breakpoint_ops *ops)
7244 {
7245 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7246
7247 add_location_to_breakpoint (b, &sal);
7248
7249 if (bptype != bp_catchpoint)
7250 gdb_assert (sal.pspace != NULL);
7251
7252 /* Store the program space that was used to set the breakpoint,
7253 except for ordinary breakpoints, which are independent of the
7254 program space. */
7255 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7256 b->pspace = sal.pspace;
7257 }
7258
7259 /* set_raw_breakpoint is a low level routine for allocating and
7260 partially initializing a breakpoint of type BPTYPE. The newly
7261 created breakpoint's address, section, source file name, and line
7262 number are provided by SAL. The newly created and partially
7263 initialized breakpoint is added to the breakpoint chain and
7264 is also returned as the value of this function.
7265
7266 It is expected that the caller will complete the initialization of
7267 the newly created breakpoint struct as well as output any status
7268 information regarding the creation of a new breakpoint. In
7269 particular, set_raw_breakpoint does NOT set the breakpoint
7270 number! Care should be taken to not allow an error to occur
7271 prior to completing the initialization of the breakpoint. If this
7272 should happen, a bogus breakpoint will be left on the chain. */
7273
7274 struct breakpoint *
7275 set_raw_breakpoint (struct gdbarch *gdbarch,
7276 struct symtab_and_line sal, enum bptype bptype,
7277 const struct breakpoint_ops *ops)
7278 {
7279 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7280
7281 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7282 return add_to_breakpoint_chain (std::move (b));
7283 }
7284
7285 /* Call this routine when stepping and nexting to enable a breakpoint
7286 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7287 initiated the operation. */
7288
7289 void
7290 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7291 {
7292 int thread = tp->global_num;
7293
7294 /* To avoid having to rescan all objfile symbols at every step,
7295 we maintain a list of continually-inserted but always disabled
7296 longjmp "master" breakpoints. Here, we simply create momentary
7297 clones of those and enable them for the requested thread. */
7298 for (breakpoint *b : all_breakpoints_safe ())
7299 if (b->pspace == current_program_space
7300 && (b->type == bp_longjmp_master
7301 || b->type == bp_exception_master))
7302 {
7303 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7304 struct breakpoint *clone;
7305
7306 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7307 after their removal. */
7308 clone = momentary_breakpoint_from_master (b, type,
7309 &momentary_breakpoint_ops, 1);
7310 clone->thread = thread;
7311 }
7312
7313 tp->initiating_frame = frame;
7314 }
7315
7316 /* Delete all longjmp breakpoints from THREAD. */
7317 void
7318 delete_longjmp_breakpoint (int thread)
7319 {
7320 for (breakpoint *b : all_breakpoints_safe ())
7321 if (b->type == bp_longjmp || b->type == bp_exception)
7322 {
7323 if (b->thread == thread)
7324 delete_breakpoint (b);
7325 }
7326 }
7327
7328 void
7329 delete_longjmp_breakpoint_at_next_stop (int thread)
7330 {
7331 for (breakpoint *b : all_breakpoints_safe ())
7332 if (b->type == bp_longjmp || b->type == bp_exception)
7333 {
7334 if (b->thread == thread)
7335 b->disposition = disp_del_at_next_stop;
7336 }
7337 }
7338
7339 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7340 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7341 pointer to any of them. Return NULL if this system cannot place longjmp
7342 breakpoints. */
7343
7344 struct breakpoint *
7345 set_longjmp_breakpoint_for_call_dummy (void)
7346 {
7347 breakpoint *retval = nullptr;
7348
7349 for (breakpoint *b : all_breakpoints ())
7350 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7351 {
7352 struct breakpoint *new_b;
7353
7354 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7355 &momentary_breakpoint_ops,
7356 1);
7357 new_b->thread = inferior_thread ()->global_num;
7358
7359 /* Link NEW_B into the chain of RETVAL breakpoints. */
7360
7361 gdb_assert (new_b->related_breakpoint == new_b);
7362 if (retval == NULL)
7363 retval = new_b;
7364 new_b->related_breakpoint = retval;
7365 while (retval->related_breakpoint != new_b->related_breakpoint)
7366 retval = retval->related_breakpoint;
7367 retval->related_breakpoint = new_b;
7368 }
7369
7370 return retval;
7371 }
7372
7373 /* Verify all existing dummy frames and their associated breakpoints for
7374 TP. Remove those which can no longer be found in the current frame
7375 stack.
7376
7377 You should call this function only at places where it is safe to currently
7378 unwind the whole stack. Failed stack unwind would discard live dummy
7379 frames. */
7380
7381 void
7382 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7383 {
7384 struct breakpoint *b, *b_tmp;
7385
7386 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7387 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7388 {
7389 struct breakpoint *dummy_b = b->related_breakpoint;
7390
7391 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7392 dummy_b = dummy_b->related_breakpoint;
7393 if (dummy_b->type != bp_call_dummy
7394 || frame_find_by_id (dummy_b->frame_id) != NULL)
7395 continue;
7396
7397 dummy_frame_discard (dummy_b->frame_id, tp);
7398
7399 while (b->related_breakpoint != b)
7400 {
7401 if (b_tmp == b->related_breakpoint)
7402 b_tmp = b->related_breakpoint->next;
7403 delete_breakpoint (b->related_breakpoint);
7404 }
7405 delete_breakpoint (b);
7406 }
7407 }
7408
7409 void
7410 enable_overlay_breakpoints (void)
7411 {
7412 for (breakpoint *b : all_breakpoints ())
7413 if (b->type == bp_overlay_event)
7414 {
7415 b->enable_state = bp_enabled;
7416 update_global_location_list (UGLL_MAY_INSERT);
7417 overlay_events_enabled = 1;
7418 }
7419 }
7420
7421 void
7422 disable_overlay_breakpoints (void)
7423 {
7424 for (breakpoint *b : all_breakpoints ())
7425 if (b->type == bp_overlay_event)
7426 {
7427 b->enable_state = bp_disabled;
7428 update_global_location_list (UGLL_DONT_INSERT);
7429 overlay_events_enabled = 0;
7430 }
7431 }
7432
7433 /* Set an active std::terminate breakpoint for each std::terminate
7434 master breakpoint. */
7435 void
7436 set_std_terminate_breakpoint (void)
7437 {
7438 for (breakpoint *b : all_breakpoints_safe ())
7439 if (b->pspace == current_program_space
7440 && b->type == bp_std_terminate_master)
7441 {
7442 momentary_breakpoint_from_master (b, bp_std_terminate,
7443 &momentary_breakpoint_ops, 1);
7444 }
7445 }
7446
7447 /* Delete all the std::terminate breakpoints. */
7448 void
7449 delete_std_terminate_breakpoint (void)
7450 {
7451 for (breakpoint *b : all_breakpoints_safe ())
7452 if (b->type == bp_std_terminate)
7453 delete_breakpoint (b);
7454 }
7455
7456 struct breakpoint *
7457 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7458 {
7459 struct breakpoint *b;
7460
7461 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7462 &internal_breakpoint_ops);
7463
7464 b->enable_state = bp_enabled;
7465 /* location has to be used or breakpoint_re_set will delete me. */
7466 b->location = new_address_location (b->loc->address, NULL, 0);
7467
7468 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7469
7470 return b;
7471 }
7472
7473 struct lang_and_radix
7474 {
7475 enum language lang;
7476 int radix;
7477 };
7478
7479 /* Create a breakpoint for JIT code registration and unregistration. */
7480
7481 struct breakpoint *
7482 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7483 {
7484 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7485 &internal_breakpoint_ops);
7486 }
7487
7488 /* Remove JIT code registration and unregistration breakpoint(s). */
7489
7490 void
7491 remove_jit_event_breakpoints (void)
7492 {
7493 for (breakpoint *b : all_breakpoints_safe ())
7494 if (b->type == bp_jit_event
7495 && b->loc->pspace == current_program_space)
7496 delete_breakpoint (b);
7497 }
7498
7499 void
7500 remove_solib_event_breakpoints (void)
7501 {
7502 for (breakpoint *b : all_breakpoints_safe ())
7503 if (b->type == bp_shlib_event
7504 && b->loc->pspace == current_program_space)
7505 delete_breakpoint (b);
7506 }
7507
7508 /* See breakpoint.h. */
7509
7510 void
7511 remove_solib_event_breakpoints_at_next_stop (void)
7512 {
7513 for (breakpoint *b : all_breakpoints_safe ())
7514 if (b->type == bp_shlib_event
7515 && b->loc->pspace == current_program_space)
7516 b->disposition = disp_del_at_next_stop;
7517 }
7518
7519 /* Helper for create_solib_event_breakpoint /
7520 create_and_insert_solib_event_breakpoint. Allows specifying which
7521 INSERT_MODE to pass through to update_global_location_list. */
7522
7523 static struct breakpoint *
7524 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7525 enum ugll_insert_mode insert_mode)
7526 {
7527 struct breakpoint *b;
7528
7529 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7530 &internal_breakpoint_ops);
7531 update_global_location_list_nothrow (insert_mode);
7532 return b;
7533 }
7534
7535 struct breakpoint *
7536 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7537 {
7538 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7539 }
7540
7541 /* See breakpoint.h. */
7542
7543 struct breakpoint *
7544 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7545 {
7546 struct breakpoint *b;
7547
7548 /* Explicitly tell update_global_location_list to insert
7549 locations. */
7550 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7551 if (!b->loc->inserted)
7552 {
7553 delete_breakpoint (b);
7554 return NULL;
7555 }
7556 return b;
7557 }
7558
7559 /* Disable any breakpoints that are on code in shared libraries. Only
7560 apply to enabled breakpoints, disabled ones can just stay disabled. */
7561
7562 void
7563 disable_breakpoints_in_shlibs (void)
7564 {
7565 for (bp_location *loc : all_bp_locations ())
7566 {
7567 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7568 struct breakpoint *b = loc->owner;
7569
7570 /* We apply the check to all breakpoints, including disabled for
7571 those with loc->duplicate set. This is so that when breakpoint
7572 becomes enabled, or the duplicate is removed, gdb will try to
7573 insert all breakpoints. If we don't set shlib_disabled here,
7574 we'll try to insert those breakpoints and fail. */
7575 if (((b->type == bp_breakpoint)
7576 || (b->type == bp_jit_event)
7577 || (b->type == bp_hardware_breakpoint)
7578 || (is_tracepoint (b)))
7579 && loc->pspace == current_program_space
7580 && !loc->shlib_disabled
7581 && solib_name_from_address (loc->pspace, loc->address)
7582 )
7583 {
7584 loc->shlib_disabled = 1;
7585 }
7586 }
7587 }
7588
7589 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7590 notification of unloaded_shlib. Only apply to enabled breakpoints,
7591 disabled ones can just stay disabled. */
7592
7593 static void
7594 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7595 {
7596 int disabled_shlib_breaks = 0;
7597
7598 for (bp_location *loc : all_bp_locations ())
7599 {
7600 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7601 struct breakpoint *b = loc->owner;
7602
7603 if (solib->pspace == loc->pspace
7604 && !loc->shlib_disabled
7605 && (((b->type == bp_breakpoint
7606 || b->type == bp_jit_event
7607 || b->type == bp_hardware_breakpoint)
7608 && (loc->loc_type == bp_loc_hardware_breakpoint
7609 || loc->loc_type == bp_loc_software_breakpoint))
7610 || is_tracepoint (b))
7611 && solib_contains_address_p (solib, loc->address))
7612 {
7613 loc->shlib_disabled = 1;
7614 /* At this point, we cannot rely on remove_breakpoint
7615 succeeding so we must mark the breakpoint as not inserted
7616 to prevent future errors occurring in remove_breakpoints. */
7617 loc->inserted = 0;
7618
7619 /* This may cause duplicate notifications for the same breakpoint. */
7620 gdb::observers::breakpoint_modified.notify (b);
7621
7622 if (!disabled_shlib_breaks)
7623 {
7624 target_terminal::ours_for_output ();
7625 warning (_("Temporarily disabling breakpoints "
7626 "for unloaded shared library \"%s\""),
7627 solib->so_name);
7628 }
7629 disabled_shlib_breaks = 1;
7630 }
7631 }
7632 }
7633
7634 /* Disable any breakpoints and tracepoints in OBJFILE upon
7635 notification of free_objfile. Only apply to enabled breakpoints,
7636 disabled ones can just stay disabled. */
7637
7638 static void
7639 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7640 {
7641 if (objfile == NULL)
7642 return;
7643
7644 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7645 managed by the user with add-symbol-file/remove-symbol-file.
7646 Similarly to how breakpoints in shared libraries are handled in
7647 response to "nosharedlibrary", mark breakpoints in such modules
7648 shlib_disabled so they end up uninserted on the next global
7649 location list update. Shared libraries not loaded by the user
7650 aren't handled here -- they're already handled in
7651 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7652 solib_unloaded observer. We skip objfiles that are not
7653 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7654 main objfile). */
7655 if ((objfile->flags & OBJF_SHARED) == 0
7656 || (objfile->flags & OBJF_USERLOADED) == 0)
7657 return;
7658
7659 for (breakpoint *b : all_breakpoints ())
7660 {
7661 int bp_modified = 0;
7662
7663 if (!is_breakpoint (b) && !is_tracepoint (b))
7664 continue;
7665
7666 for (bp_location *loc : b->locations ())
7667 {
7668 CORE_ADDR loc_addr = loc->address;
7669
7670 if (loc->loc_type != bp_loc_hardware_breakpoint
7671 && loc->loc_type != bp_loc_software_breakpoint)
7672 continue;
7673
7674 if (loc->shlib_disabled != 0)
7675 continue;
7676
7677 if (objfile->pspace != loc->pspace)
7678 continue;
7679
7680 if (loc->loc_type != bp_loc_hardware_breakpoint
7681 && loc->loc_type != bp_loc_software_breakpoint)
7682 continue;
7683
7684 if (is_addr_in_objfile (loc_addr, objfile))
7685 {
7686 loc->shlib_disabled = 1;
7687 /* At this point, we don't know whether the object was
7688 unmapped from the inferior or not, so leave the
7689 inserted flag alone. We'll handle failure to
7690 uninsert quietly, in case the object was indeed
7691 unmapped. */
7692
7693 mark_breakpoint_location_modified (loc);
7694
7695 bp_modified = 1;
7696 }
7697 }
7698
7699 if (bp_modified)
7700 gdb::observers::breakpoint_modified.notify (b);
7701 }
7702 }
7703
7704 /* FORK & VFORK catchpoints. */
7705
7706 /* An instance of this type is used to represent a fork or vfork
7707 catchpoint. A breakpoint is really of this type iff its ops pointer points
7708 to CATCH_FORK_BREAKPOINT_OPS. */
7709
7710 struct fork_catchpoint : public breakpoint
7711 {
7712 /* Process id of a child process whose forking triggered this
7713 catchpoint. This field is only valid immediately after this
7714 catchpoint has triggered. */
7715 ptid_t forked_inferior_pid;
7716 };
7717
7718 /* Implement the "insert" breakpoint_ops method for fork
7719 catchpoints. */
7720
7721 static int
7722 insert_catch_fork (struct bp_location *bl)
7723 {
7724 return target_insert_fork_catchpoint (inferior_ptid.pid ());
7725 }
7726
7727 /* Implement the "remove" breakpoint_ops method for fork
7728 catchpoints. */
7729
7730 static int
7731 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
7732 {
7733 return target_remove_fork_catchpoint (inferior_ptid.pid ());
7734 }
7735
7736 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7737 catchpoints. */
7738
7739 static int
7740 breakpoint_hit_catch_fork (const struct bp_location *bl,
7741 const address_space *aspace, CORE_ADDR bp_addr,
7742 const struct target_waitstatus *ws)
7743 {
7744 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7745
7746 if (ws->kind != TARGET_WAITKIND_FORKED)
7747 return 0;
7748
7749 c->forked_inferior_pid = ws->value.related_pid;
7750 return 1;
7751 }
7752
7753 /* Implement the "print_it" breakpoint_ops method for fork
7754 catchpoints. */
7755
7756 static enum print_stop_action
7757 print_it_catch_fork (bpstat bs)
7758 {
7759 struct ui_out *uiout = current_uiout;
7760 struct breakpoint *b = bs->breakpoint_at;
7761 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7762
7763 annotate_catchpoint (b->number);
7764 maybe_print_thread_hit_breakpoint (uiout);
7765 if (b->disposition == disp_del)
7766 uiout->text ("Temporary catchpoint ");
7767 else
7768 uiout->text ("Catchpoint ");
7769 if (uiout->is_mi_like_p ())
7770 {
7771 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7772 uiout->field_string ("disp", bpdisp_text (b->disposition));
7773 }
7774 uiout->field_signed ("bkptno", b->number);
7775 uiout->text (" (forked process ");
7776 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7777 uiout->text ("), ");
7778 return PRINT_SRC_AND_LOC;
7779 }
7780
7781 /* Implement the "print_one" breakpoint_ops method for fork
7782 catchpoints. */
7783
7784 static void
7785 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7786 {
7787 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7788 struct value_print_options opts;
7789 struct ui_out *uiout = current_uiout;
7790
7791 get_user_print_options (&opts);
7792
7793 /* Field 4, the address, is omitted (which makes the columns not
7794 line up too nicely with the headers, but the effect is relatively
7795 readable). */
7796 if (opts.addressprint)
7797 uiout->field_skip ("addr");
7798 annotate_field (5);
7799 uiout->text ("fork");
7800 if (c->forked_inferior_pid != null_ptid)
7801 {
7802 uiout->text (", process ");
7803 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7804 uiout->spaces (1);
7805 }
7806
7807 if (uiout->is_mi_like_p ())
7808 uiout->field_string ("catch-type", "fork");
7809 }
7810
7811 /* Implement the "print_mention" breakpoint_ops method for fork
7812 catchpoints. */
7813
7814 static void
7815 print_mention_catch_fork (struct breakpoint *b)
7816 {
7817 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7818 }
7819
7820 /* Implement the "print_recreate" breakpoint_ops method for fork
7821 catchpoints. */
7822
7823 static void
7824 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7825 {
7826 fprintf_unfiltered (fp, "catch fork");
7827 print_recreate_thread (b, fp);
7828 }
7829
7830 /* The breakpoint_ops structure to be used in fork catchpoints. */
7831
7832 static struct breakpoint_ops catch_fork_breakpoint_ops;
7833
7834 /* Implement the "insert" breakpoint_ops method for vfork
7835 catchpoints. */
7836
7837 static int
7838 insert_catch_vfork (struct bp_location *bl)
7839 {
7840 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
7841 }
7842
7843 /* Implement the "remove" breakpoint_ops method for vfork
7844 catchpoints. */
7845
7846 static int
7847 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
7848 {
7849 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
7850 }
7851
7852 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7853 catchpoints. */
7854
7855 static int
7856 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7857 const address_space *aspace, CORE_ADDR bp_addr,
7858 const struct target_waitstatus *ws)
7859 {
7860 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7861
7862 if (ws->kind != TARGET_WAITKIND_VFORKED)
7863 return 0;
7864
7865 c->forked_inferior_pid = ws->value.related_pid;
7866 return 1;
7867 }
7868
7869 /* Implement the "print_it" breakpoint_ops method for vfork
7870 catchpoints. */
7871
7872 static enum print_stop_action
7873 print_it_catch_vfork (bpstat bs)
7874 {
7875 struct ui_out *uiout = current_uiout;
7876 struct breakpoint *b = bs->breakpoint_at;
7877 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7878
7879 annotate_catchpoint (b->number);
7880 maybe_print_thread_hit_breakpoint (uiout);
7881 if (b->disposition == disp_del)
7882 uiout->text ("Temporary catchpoint ");
7883 else
7884 uiout->text ("Catchpoint ");
7885 if (uiout->is_mi_like_p ())
7886 {
7887 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7888 uiout->field_string ("disp", bpdisp_text (b->disposition));
7889 }
7890 uiout->field_signed ("bkptno", b->number);
7891 uiout->text (" (vforked process ");
7892 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7893 uiout->text ("), ");
7894 return PRINT_SRC_AND_LOC;
7895 }
7896
7897 /* Implement the "print_one" breakpoint_ops method for vfork
7898 catchpoints. */
7899
7900 static void
7901 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7902 {
7903 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7904 struct value_print_options opts;
7905 struct ui_out *uiout = current_uiout;
7906
7907 get_user_print_options (&opts);
7908 /* Field 4, the address, is omitted (which makes the columns not
7909 line up too nicely with the headers, but the effect is relatively
7910 readable). */
7911 if (opts.addressprint)
7912 uiout->field_skip ("addr");
7913 annotate_field (5);
7914 uiout->text ("vfork");
7915 if (c->forked_inferior_pid != null_ptid)
7916 {
7917 uiout->text (", process ");
7918 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7919 uiout->spaces (1);
7920 }
7921
7922 if (uiout->is_mi_like_p ())
7923 uiout->field_string ("catch-type", "vfork");
7924 }
7925
7926 /* Implement the "print_mention" breakpoint_ops method for vfork
7927 catchpoints. */
7928
7929 static void
7930 print_mention_catch_vfork (struct breakpoint *b)
7931 {
7932 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7933 }
7934
7935 /* Implement the "print_recreate" breakpoint_ops method for vfork
7936 catchpoints. */
7937
7938 static void
7939 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7940 {
7941 fprintf_unfiltered (fp, "catch vfork");
7942 print_recreate_thread (b, fp);
7943 }
7944
7945 /* The breakpoint_ops structure to be used in vfork catchpoints. */
7946
7947 static struct breakpoint_ops catch_vfork_breakpoint_ops;
7948
7949 /* An instance of this type is used to represent an solib catchpoint.
7950 A breakpoint is really of this type iff its ops pointer points to
7951 CATCH_SOLIB_BREAKPOINT_OPS. */
7952
7953 struct solib_catchpoint : public breakpoint
7954 {
7955 ~solib_catchpoint () override;
7956
7957 /* True for "catch load", false for "catch unload". */
7958 bool is_load;
7959
7960 /* Regular expression to match, if any. COMPILED is only valid when
7961 REGEX is non-NULL. */
7962 char *regex;
7963 std::unique_ptr<compiled_regex> compiled;
7964 };
7965
7966 solib_catchpoint::~solib_catchpoint ()
7967 {
7968 xfree (this->regex);
7969 }
7970
7971 static int
7972 insert_catch_solib (struct bp_location *ignore)
7973 {
7974 return 0;
7975 }
7976
7977 static int
7978 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
7979 {
7980 return 0;
7981 }
7982
7983 static int
7984 breakpoint_hit_catch_solib (const struct bp_location *bl,
7985 const address_space *aspace,
7986 CORE_ADDR bp_addr,
7987 const struct target_waitstatus *ws)
7988 {
7989 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7990
7991 if (ws->kind == TARGET_WAITKIND_LOADED)
7992 return 1;
7993
7994 for (breakpoint *other : all_breakpoints ())
7995 {
7996 if (other == bl->owner)
7997 continue;
7998
7999 if (other->type != bp_shlib_event)
8000 continue;
8001
8002 if (self->pspace != NULL && other->pspace != self->pspace)
8003 continue;
8004
8005 for (bp_location *other_bl : other->locations ())
8006 {
8007 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8008 return 1;
8009 }
8010 }
8011
8012 return 0;
8013 }
8014
8015 static void
8016 check_status_catch_solib (struct bpstats *bs)
8017 {
8018 struct solib_catchpoint *self
8019 = (struct solib_catchpoint *) bs->breakpoint_at;
8020
8021 if (self->is_load)
8022 {
8023 for (so_list *iter : current_program_space->added_solibs)
8024 {
8025 if (!self->regex
8026 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
8027 return;
8028 }
8029 }
8030 else
8031 {
8032 for (const std::string &iter : current_program_space->deleted_solibs)
8033 {
8034 if (!self->regex
8035 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
8036 return;
8037 }
8038 }
8039
8040 bs->stop = 0;
8041 bs->print_it = print_it_noop;
8042 }
8043
8044 static enum print_stop_action
8045 print_it_catch_solib (bpstat bs)
8046 {
8047 struct breakpoint *b = bs->breakpoint_at;
8048 struct ui_out *uiout = current_uiout;
8049
8050 annotate_catchpoint (b->number);
8051 maybe_print_thread_hit_breakpoint (uiout);
8052 if (b->disposition == disp_del)
8053 uiout->text ("Temporary catchpoint ");
8054 else
8055 uiout->text ("Catchpoint ");
8056 uiout->field_signed ("bkptno", b->number);
8057 uiout->text ("\n");
8058 if (uiout->is_mi_like_p ())
8059 uiout->field_string ("disp", bpdisp_text (b->disposition));
8060 print_solib_event (1);
8061 return PRINT_SRC_AND_LOC;
8062 }
8063
8064 static void
8065 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8066 {
8067 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8068 struct value_print_options opts;
8069 struct ui_out *uiout = current_uiout;
8070
8071 get_user_print_options (&opts);
8072 /* Field 4, the address, is omitted (which makes the columns not
8073 line up too nicely with the headers, but the effect is relatively
8074 readable). */
8075 if (opts.addressprint)
8076 {
8077 annotate_field (4);
8078 uiout->field_skip ("addr");
8079 }
8080
8081 std::string msg;
8082 annotate_field (5);
8083 if (self->is_load)
8084 {
8085 if (self->regex)
8086 msg = string_printf (_("load of library matching %s"), self->regex);
8087 else
8088 msg = _("load of library");
8089 }
8090 else
8091 {
8092 if (self->regex)
8093 msg = string_printf (_("unload of library matching %s"), self->regex);
8094 else
8095 msg = _("unload of library");
8096 }
8097 uiout->field_string ("what", msg);
8098
8099 if (uiout->is_mi_like_p ())
8100 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8101 }
8102
8103 static void
8104 print_mention_catch_solib (struct breakpoint *b)
8105 {
8106 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8107
8108 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8109 self->is_load ? "load" : "unload");
8110 }
8111
8112 static void
8113 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8114 {
8115 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8116
8117 fprintf_unfiltered (fp, "%s %s",
8118 b->disposition == disp_del ? "tcatch" : "catch",
8119 self->is_load ? "load" : "unload");
8120 if (self->regex)
8121 fprintf_unfiltered (fp, " %s", self->regex);
8122 fprintf_unfiltered (fp, "\n");
8123 }
8124
8125 static struct breakpoint_ops catch_solib_breakpoint_ops;
8126
8127 /* See breakpoint.h. */
8128
8129 void
8130 add_solib_catchpoint (const char *arg, bool is_load, bool is_temp, bool enabled)
8131 {
8132 struct gdbarch *gdbarch = get_current_arch ();
8133
8134 if (!arg)
8135 arg = "";
8136 arg = skip_spaces (arg);
8137
8138 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
8139
8140 if (*arg != '\0')
8141 {
8142 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8143 _("Invalid regexp")));
8144 c->regex = xstrdup (arg);
8145 }
8146
8147 c->is_load = is_load;
8148 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
8149 &catch_solib_breakpoint_ops);
8150
8151 c->enable_state = enabled ? bp_enabled : bp_disabled;
8152
8153 install_breakpoint (0, std::move (c), 1);
8154 }
8155
8156 /* A helper function that does all the work for "catch load" and
8157 "catch unload". */
8158
8159 static void
8160 catch_load_or_unload (const char *arg, int from_tty, int is_load,
8161 struct cmd_list_element *command)
8162 {
8163 const int enabled = 1;
8164 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
8165
8166 add_solib_catchpoint (arg, is_load, temp, enabled);
8167 }
8168
8169 static void
8170 catch_load_command_1 (const char *arg, int from_tty,
8171 struct cmd_list_element *command)
8172 {
8173 catch_load_or_unload (arg, from_tty, 1, command);
8174 }
8175
8176 static void
8177 catch_unload_command_1 (const char *arg, int from_tty,
8178 struct cmd_list_element *command)
8179 {
8180 catch_load_or_unload (arg, from_tty, 0, command);
8181 }
8182
8183 /* See breakpoint.h. */
8184
8185 void
8186 init_catchpoint (struct breakpoint *b,
8187 struct gdbarch *gdbarch, bool temp,
8188 const char *cond_string,
8189 const struct breakpoint_ops *ops)
8190 {
8191 symtab_and_line sal;
8192 sal.pspace = current_program_space;
8193
8194 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8195
8196 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8197 b->disposition = temp ? disp_del : disp_donttouch;
8198 }
8199
8200 void
8201 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8202 {
8203 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8204 set_breakpoint_number (internal, b);
8205 if (is_tracepoint (b))
8206 set_tracepoint_count (breakpoint_count);
8207 if (!internal)
8208 mention (b);
8209 gdb::observers::breakpoint_created.notify (b);
8210
8211 if (update_gll)
8212 update_global_location_list (UGLL_MAY_INSERT);
8213 }
8214
8215 static void
8216 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8217 bool temp, const char *cond_string,
8218 const struct breakpoint_ops *ops)
8219 {
8220 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
8221
8222 init_catchpoint (c.get (), gdbarch, temp, cond_string, ops);
8223
8224 c->forked_inferior_pid = null_ptid;
8225
8226 install_breakpoint (0, std::move (c), 1);
8227 }
8228
8229 /* Exec catchpoints. */
8230
8231 /* An instance of this type is used to represent an exec catchpoint.
8232 A breakpoint is really of this type iff its ops pointer points to
8233 CATCH_EXEC_BREAKPOINT_OPS. */
8234
8235 struct exec_catchpoint : public breakpoint
8236 {
8237 ~exec_catchpoint () override;
8238
8239 /* Filename of a program whose exec triggered this catchpoint.
8240 This field is only valid immediately after this catchpoint has
8241 triggered. */
8242 char *exec_pathname;
8243 };
8244
8245 /* Exec catchpoint destructor. */
8246
8247 exec_catchpoint::~exec_catchpoint ()
8248 {
8249 xfree (this->exec_pathname);
8250 }
8251
8252 static int
8253 insert_catch_exec (struct bp_location *bl)
8254 {
8255 return target_insert_exec_catchpoint (inferior_ptid.pid ());
8256 }
8257
8258 static int
8259 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8260 {
8261 return target_remove_exec_catchpoint (inferior_ptid.pid ());
8262 }
8263
8264 static int
8265 breakpoint_hit_catch_exec (const struct bp_location *bl,
8266 const address_space *aspace, CORE_ADDR bp_addr,
8267 const struct target_waitstatus *ws)
8268 {
8269 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8270
8271 if (ws->kind != TARGET_WAITKIND_EXECD)
8272 return 0;
8273
8274 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8275 return 1;
8276 }
8277
8278 static enum print_stop_action
8279 print_it_catch_exec (bpstat bs)
8280 {
8281 struct ui_out *uiout = current_uiout;
8282 struct breakpoint *b = bs->breakpoint_at;
8283 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8284
8285 annotate_catchpoint (b->number);
8286 maybe_print_thread_hit_breakpoint (uiout);
8287 if (b->disposition == disp_del)
8288 uiout->text ("Temporary catchpoint ");
8289 else
8290 uiout->text ("Catchpoint ");
8291 if (uiout->is_mi_like_p ())
8292 {
8293 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8294 uiout->field_string ("disp", bpdisp_text (b->disposition));
8295 }
8296 uiout->field_signed ("bkptno", b->number);
8297 uiout->text (" (exec'd ");
8298 uiout->field_string ("new-exec", c->exec_pathname);
8299 uiout->text ("), ");
8300
8301 return PRINT_SRC_AND_LOC;
8302 }
8303
8304 static void
8305 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8306 {
8307 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8308 struct value_print_options opts;
8309 struct ui_out *uiout = current_uiout;
8310
8311 get_user_print_options (&opts);
8312
8313 /* Field 4, the address, is omitted (which makes the columns
8314 not line up too nicely with the headers, but the effect
8315 is relatively readable). */
8316 if (opts.addressprint)
8317 uiout->field_skip ("addr");
8318 annotate_field (5);
8319 uiout->text ("exec");
8320 if (c->exec_pathname != NULL)
8321 {
8322 uiout->text (", program \"");
8323 uiout->field_string ("what", c->exec_pathname);
8324 uiout->text ("\" ");
8325 }
8326
8327 if (uiout->is_mi_like_p ())
8328 uiout->field_string ("catch-type", "exec");
8329 }
8330
8331 static void
8332 print_mention_catch_exec (struct breakpoint *b)
8333 {
8334 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8335 }
8336
8337 /* Implement the "print_recreate" breakpoint_ops method for exec
8338 catchpoints. */
8339
8340 static void
8341 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8342 {
8343 fprintf_unfiltered (fp, "catch exec");
8344 print_recreate_thread (b, fp);
8345 }
8346
8347 static struct breakpoint_ops catch_exec_breakpoint_ops;
8348
8349 static int
8350 hw_breakpoint_used_count (void)
8351 {
8352 int i = 0;
8353
8354 for (breakpoint *b : all_breakpoints ())
8355 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8356 for (bp_location *bl : b->locations ())
8357 {
8358 /* Special types of hardware breakpoints may use more than
8359 one register. */
8360 i += b->ops->resources_needed (bl);
8361 }
8362
8363 return i;
8364 }
8365
8366 /* Returns the resources B would use if it were a hardware
8367 watchpoint. */
8368
8369 static int
8370 hw_watchpoint_use_count (struct breakpoint *b)
8371 {
8372 int i = 0;
8373
8374 if (!breakpoint_enabled (b))
8375 return 0;
8376
8377 for (bp_location *bl : b->locations ())
8378 {
8379 /* Special types of hardware watchpoints may use more than
8380 one register. */
8381 i += b->ops->resources_needed (bl);
8382 }
8383
8384 return i;
8385 }
8386
8387 /* Returns the sum the used resources of all hardware watchpoints of
8388 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8389 the sum of the used resources of all hardware watchpoints of other
8390 types _not_ TYPE. */
8391
8392 static int
8393 hw_watchpoint_used_count_others (struct breakpoint *except,
8394 enum bptype type, int *other_type_used)
8395 {
8396 int i = 0;
8397
8398 *other_type_used = 0;
8399 for (breakpoint *b : all_breakpoints ())
8400 {
8401 if (b == except)
8402 continue;
8403 if (!breakpoint_enabled (b))
8404 continue;
8405
8406 if (b->type == type)
8407 i += hw_watchpoint_use_count (b);
8408 else if (is_hardware_watchpoint (b))
8409 *other_type_used = 1;
8410 }
8411
8412 return i;
8413 }
8414
8415 void
8416 disable_watchpoints_before_interactive_call_start (void)
8417 {
8418 for (breakpoint *b : all_breakpoints ())
8419 if (is_watchpoint (b) && breakpoint_enabled (b))
8420 {
8421 b->enable_state = bp_call_disabled;
8422 update_global_location_list (UGLL_DONT_INSERT);
8423 }
8424 }
8425
8426 void
8427 enable_watchpoints_after_interactive_call_stop (void)
8428 {
8429 for (breakpoint *b : all_breakpoints ())
8430 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8431 {
8432 b->enable_state = bp_enabled;
8433 update_global_location_list (UGLL_MAY_INSERT);
8434 }
8435 }
8436
8437 void
8438 disable_breakpoints_before_startup (void)
8439 {
8440 current_program_space->executing_startup = 1;
8441 update_global_location_list (UGLL_DONT_INSERT);
8442 }
8443
8444 void
8445 enable_breakpoints_after_startup (void)
8446 {
8447 current_program_space->executing_startup = 0;
8448 breakpoint_re_set ();
8449 }
8450
8451 /* Create a new single-step breakpoint for thread THREAD, with no
8452 locations. */
8453
8454 static struct breakpoint *
8455 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8456 {
8457 std::unique_ptr<breakpoint> b (new breakpoint ());
8458
8459 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
8460 &momentary_breakpoint_ops);
8461
8462 b->disposition = disp_donttouch;
8463 b->frame_id = null_frame_id;
8464
8465 b->thread = thread;
8466 gdb_assert (b->thread != 0);
8467
8468 return add_to_breakpoint_chain (std::move (b));
8469 }
8470
8471 /* Set a momentary breakpoint of type TYPE at address specified by
8472 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8473 frame. */
8474
8475 breakpoint_up
8476 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8477 struct frame_id frame_id, enum bptype type)
8478 {
8479 struct breakpoint *b;
8480
8481 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8482 tail-called one. */
8483 gdb_assert (!frame_id_artificial_p (frame_id));
8484
8485 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8486 b->enable_state = bp_enabled;
8487 b->disposition = disp_donttouch;
8488 b->frame_id = frame_id;
8489
8490 b->thread = inferior_thread ()->global_num;
8491
8492 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8493
8494 return breakpoint_up (b);
8495 }
8496
8497 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8498 The new breakpoint will have type TYPE, use OPS as its
8499 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8500
8501 static struct breakpoint *
8502 momentary_breakpoint_from_master (struct breakpoint *orig,
8503 enum bptype type,
8504 const struct breakpoint_ops *ops,
8505 int loc_enabled)
8506 {
8507 struct breakpoint *copy;
8508
8509 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8510 copy->loc = allocate_bp_location (copy);
8511 set_breakpoint_location_function (copy->loc);
8512
8513 copy->loc->gdbarch = orig->loc->gdbarch;
8514 copy->loc->requested_address = orig->loc->requested_address;
8515 copy->loc->address = orig->loc->address;
8516 copy->loc->section = orig->loc->section;
8517 copy->loc->pspace = orig->loc->pspace;
8518 copy->loc->probe = orig->loc->probe;
8519 copy->loc->line_number = orig->loc->line_number;
8520 copy->loc->symtab = orig->loc->symtab;
8521 copy->loc->enabled = loc_enabled;
8522 copy->frame_id = orig->frame_id;
8523 copy->thread = orig->thread;
8524 copy->pspace = orig->pspace;
8525
8526 copy->enable_state = bp_enabled;
8527 copy->disposition = disp_donttouch;
8528 copy->number = internal_breakpoint_number--;
8529
8530 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8531 return copy;
8532 }
8533
8534 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8535 ORIG is NULL. */
8536
8537 struct breakpoint *
8538 clone_momentary_breakpoint (struct breakpoint *orig)
8539 {
8540 /* If there's nothing to clone, then return nothing. */
8541 if (orig == NULL)
8542 return NULL;
8543
8544 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8545 }
8546
8547 breakpoint_up
8548 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8549 enum bptype type)
8550 {
8551 struct symtab_and_line sal;
8552
8553 sal = find_pc_line (pc, 0);
8554 sal.pc = pc;
8555 sal.section = find_pc_overlay (pc);
8556 sal.explicit_pc = 1;
8557
8558 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8559 }
8560 \f
8561
8562 /* Tell the user we have just set a breakpoint B. */
8563
8564 static void
8565 mention (struct breakpoint *b)
8566 {
8567 b->ops->print_mention (b);
8568 current_uiout->text ("\n");
8569 }
8570 \f
8571
8572 static bool bp_loc_is_permanent (struct bp_location *loc);
8573
8574 /* Handle "set breakpoint auto-hw on".
8575
8576 If the explicitly specified breakpoint type is not hardware
8577 breakpoint, check the memory map to see whether the breakpoint
8578 address is in read-only memory.
8579
8580 - location type is not hardware breakpoint, memory is read-only.
8581 We change the type of the location to hardware breakpoint.
8582
8583 - location type is hardware breakpoint, memory is read-write. This
8584 means we've previously made the location hardware one, but then the
8585 memory map changed, so we undo.
8586 */
8587
8588 static void
8589 handle_automatic_hardware_breakpoints (bp_location *bl)
8590 {
8591 if (automatic_hardware_breakpoints
8592 && bl->owner->type != bp_hardware_breakpoint
8593 && (bl->loc_type == bp_loc_software_breakpoint
8594 || bl->loc_type == bp_loc_hardware_breakpoint))
8595 {
8596 /* When breakpoints are removed, remove_breakpoints will use
8597 location types we've just set here, the only possible problem
8598 is that memory map has changed during running program, but
8599 it's not going to work anyway with current gdb. */
8600 mem_region *mr = lookup_mem_region (bl->address);
8601
8602 if (mr != nullptr)
8603 {
8604 enum bp_loc_type new_type;
8605
8606 if (mr->attrib.mode != MEM_RW)
8607 new_type = bp_loc_hardware_breakpoint;
8608 else
8609 new_type = bp_loc_software_breakpoint;
8610
8611 if (new_type != bl->loc_type)
8612 {
8613 static bool said = false;
8614
8615 bl->loc_type = new_type;
8616 if (!said)
8617 {
8618 fprintf_filtered (gdb_stdout,
8619 _("Note: automatically using "
8620 "hardware breakpoints for "
8621 "read-only addresses.\n"));
8622 said = true;
8623 }
8624 }
8625 }
8626 }
8627 }
8628
8629 static struct bp_location *
8630 add_location_to_breakpoint (struct breakpoint *b,
8631 const struct symtab_and_line *sal)
8632 {
8633 struct bp_location *loc, **tmp;
8634 CORE_ADDR adjusted_address;
8635 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8636
8637 if (loc_gdbarch == NULL)
8638 loc_gdbarch = b->gdbarch;
8639
8640 /* Adjust the breakpoint's address prior to allocating a location.
8641 Once we call allocate_bp_location(), that mostly uninitialized
8642 location will be placed on the location chain. Adjustment of the
8643 breakpoint may cause target_read_memory() to be called and we do
8644 not want its scan of the location chain to find a breakpoint and
8645 location that's only been partially initialized. */
8646 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8647 sal->pc, b->type);
8648
8649 /* Sort the locations by their ADDRESS. */
8650 loc = allocate_bp_location (b);
8651 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8652 tmp = &((*tmp)->next))
8653 ;
8654 loc->next = *tmp;
8655 *tmp = loc;
8656
8657 loc->requested_address = sal->pc;
8658 loc->address = adjusted_address;
8659 loc->pspace = sal->pspace;
8660 loc->probe.prob = sal->prob;
8661 loc->probe.objfile = sal->objfile;
8662 gdb_assert (loc->pspace != NULL);
8663 loc->section = sal->section;
8664 loc->gdbarch = loc_gdbarch;
8665 loc->line_number = sal->line;
8666 loc->symtab = sal->symtab;
8667 loc->symbol = sal->symbol;
8668 loc->msymbol = sal->msymbol;
8669 loc->objfile = sal->objfile;
8670
8671 set_breakpoint_location_function (loc);
8672
8673 /* While by definition, permanent breakpoints are already present in the
8674 code, we don't mark the location as inserted. Normally one would expect
8675 that GDB could rely on that breakpoint instruction to stop the program,
8676 thus removing the need to insert its own breakpoint, except that executing
8677 the breakpoint instruction can kill the target instead of reporting a
8678 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8679 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8680 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8681 breakpoint be inserted normally results in QEMU knowing about the GDB
8682 breakpoint, and thus trap before the breakpoint instruction is executed.
8683 (If GDB later needs to continue execution past the permanent breakpoint,
8684 it manually increments the PC, thus avoiding executing the breakpoint
8685 instruction.) */
8686 if (bp_loc_is_permanent (loc))
8687 loc->permanent = 1;
8688
8689 return loc;
8690 }
8691 \f
8692
8693 /* Return true if LOC is pointing to a permanent breakpoint,
8694 return false otherwise. */
8695
8696 static bool
8697 bp_loc_is_permanent (struct bp_location *loc)
8698 {
8699 gdb_assert (loc != NULL);
8700
8701 /* If we have a non-breakpoint-backed catchpoint or a software
8702 watchpoint, just return 0. We should not attempt to read from
8703 the addresses the locations of these breakpoint types point to.
8704 gdbarch_program_breakpoint_here_p, below, will attempt to read
8705 memory. */
8706 if (!bl_address_is_meaningful (loc))
8707 return false;
8708
8709 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8710 switch_to_program_space_and_thread (loc->pspace);
8711 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
8712 }
8713
8714 /* Build a command list for the dprintf corresponding to the current
8715 settings of the dprintf style options. */
8716
8717 static void
8718 update_dprintf_command_list (struct breakpoint *b)
8719 {
8720 char *dprintf_args = b->extra_string;
8721 char *printf_line = NULL;
8722
8723 if (!dprintf_args)
8724 return;
8725
8726 dprintf_args = skip_spaces (dprintf_args);
8727
8728 /* Allow a comma, as it may have terminated a location, but don't
8729 insist on it. */
8730 if (*dprintf_args == ',')
8731 ++dprintf_args;
8732 dprintf_args = skip_spaces (dprintf_args);
8733
8734 if (*dprintf_args != '"')
8735 error (_("Bad format string, missing '\"'."));
8736
8737 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8738 printf_line = xstrprintf ("printf %s", dprintf_args);
8739 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8740 {
8741 if (!dprintf_function)
8742 error (_("No function supplied for dprintf call"));
8743
8744 if (dprintf_channel && strlen (dprintf_channel) > 0)
8745 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8746 dprintf_function,
8747 dprintf_channel,
8748 dprintf_args);
8749 else
8750 printf_line = xstrprintf ("call (void) %s (%s)",
8751 dprintf_function,
8752 dprintf_args);
8753 }
8754 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8755 {
8756 if (target_can_run_breakpoint_commands ())
8757 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8758 else
8759 {
8760 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8761 printf_line = xstrprintf ("printf %s", dprintf_args);
8762 }
8763 }
8764 else
8765 internal_error (__FILE__, __LINE__,
8766 _("Invalid dprintf style."));
8767
8768 gdb_assert (printf_line != NULL);
8769
8770 /* Manufacture a printf sequence. */
8771 struct command_line *printf_cmd_line
8772 = new struct command_line (simple_control, printf_line);
8773 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8774 command_lines_deleter ()));
8775 }
8776
8777 /* Update all dprintf commands, making their command lists reflect
8778 current style settings. */
8779
8780 static void
8781 update_dprintf_commands (const char *args, int from_tty,
8782 struct cmd_list_element *c)
8783 {
8784 for (breakpoint *b : all_breakpoints ())
8785 if (b->type == bp_dprintf)
8786 update_dprintf_command_list (b);
8787 }
8788
8789 /* Create a breakpoint with SAL as location. Use LOCATION
8790 as a description of the location, and COND_STRING
8791 as condition expression. If LOCATION is NULL then create an
8792 "address location" from the address in the SAL. */
8793
8794 static void
8795 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8796 gdb::array_view<const symtab_and_line> sals,
8797 event_location_up &&location,
8798 gdb::unique_xmalloc_ptr<char> filter,
8799 gdb::unique_xmalloc_ptr<char> cond_string,
8800 gdb::unique_xmalloc_ptr<char> extra_string,
8801 enum bptype type, enum bpdisp disposition,
8802 int thread, int task, int ignore_count,
8803 const struct breakpoint_ops *ops, int from_tty,
8804 int enabled, int internal, unsigned flags,
8805 int display_canonical)
8806 {
8807 int i;
8808
8809 if (type == bp_hardware_breakpoint)
8810 {
8811 int target_resources_ok;
8812
8813 i = hw_breakpoint_used_count ();
8814 target_resources_ok =
8815 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8816 i + 1, 0);
8817 if (target_resources_ok == 0)
8818 error (_("No hardware breakpoint support in the target."));
8819 else if (target_resources_ok < 0)
8820 error (_("Hardware breakpoints used exceeds limit."));
8821 }
8822
8823 gdb_assert (!sals.empty ());
8824
8825 for (const auto &sal : sals)
8826 {
8827 struct bp_location *loc;
8828
8829 if (from_tty)
8830 {
8831 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8832 if (!loc_gdbarch)
8833 loc_gdbarch = gdbarch;
8834
8835 describe_other_breakpoints (loc_gdbarch,
8836 sal.pspace, sal.pc, sal.section, thread);
8837 }
8838
8839 if (&sal == &sals[0])
8840 {
8841 init_raw_breakpoint (b, gdbarch, sal, type, ops);
8842 b->thread = thread;
8843 b->task = task;
8844
8845 b->cond_string = cond_string.release ();
8846 b->extra_string = extra_string.release ();
8847 b->ignore_count = ignore_count;
8848 b->enable_state = enabled ? bp_enabled : bp_disabled;
8849 b->disposition = disposition;
8850
8851 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8852 b->loc->inserted = 1;
8853
8854 if (type == bp_static_tracepoint)
8855 {
8856 struct tracepoint *t = (struct tracepoint *) b;
8857 struct static_tracepoint_marker marker;
8858
8859 if (strace_marker_p (b))
8860 {
8861 /* We already know the marker exists, otherwise, we
8862 wouldn't see a sal for it. */
8863 const char *p
8864 = &event_location_to_string (b->location.get ())[3];
8865 const char *endp;
8866
8867 p = skip_spaces (p);
8868
8869 endp = skip_to_space (p);
8870
8871 t->static_trace_marker_id.assign (p, endp - p);
8872
8873 printf_filtered (_("Probed static tracepoint "
8874 "marker \"%s\"\n"),
8875 t->static_trace_marker_id.c_str ());
8876 }
8877 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8878 {
8879 t->static_trace_marker_id = std::move (marker.str_id);
8880
8881 printf_filtered (_("Probed static tracepoint "
8882 "marker \"%s\"\n"),
8883 t->static_trace_marker_id.c_str ());
8884 }
8885 else
8886 warning (_("Couldn't determine the static "
8887 "tracepoint marker to probe"));
8888 }
8889
8890 loc = b->loc;
8891 }
8892 else
8893 {
8894 loc = add_location_to_breakpoint (b, &sal);
8895 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8896 loc->inserted = 1;
8897 }
8898
8899 /* Do not set breakpoint locations conditions yet. As locations
8900 are inserted, they get sorted based on their addresses. Let
8901 the list stabilize to have reliable location numbers. */
8902
8903 /* Dynamic printf requires and uses additional arguments on the
8904 command line, otherwise it's an error. */
8905 if (type == bp_dprintf)
8906 {
8907 if (b->extra_string)
8908 update_dprintf_command_list (b);
8909 else
8910 error (_("Format string required"));
8911 }
8912 else if (b->extra_string)
8913 error (_("Garbage '%s' at end of command"), b->extra_string);
8914 }
8915
8916
8917 /* The order of the locations is now stable. Set the location
8918 condition using the location's number. */
8919 int loc_num = 1;
8920 for (bp_location *loc : b->locations ())
8921 {
8922 if (b->cond_string != nullptr)
8923 set_breakpoint_location_condition (b->cond_string, loc, b->number,
8924 loc_num);
8925
8926 ++loc_num;
8927 }
8928
8929 b->display_canonical = display_canonical;
8930 if (location != NULL)
8931 b->location = std::move (location);
8932 else
8933 b->location = new_address_location (b->loc->address, NULL, 0);
8934 b->filter = std::move (filter);
8935 }
8936
8937 static void
8938 create_breakpoint_sal (struct gdbarch *gdbarch,
8939 gdb::array_view<const symtab_and_line> sals,
8940 event_location_up &&location,
8941 gdb::unique_xmalloc_ptr<char> filter,
8942 gdb::unique_xmalloc_ptr<char> cond_string,
8943 gdb::unique_xmalloc_ptr<char> extra_string,
8944 enum bptype type, enum bpdisp disposition,
8945 int thread, int task, int ignore_count,
8946 const struct breakpoint_ops *ops, int from_tty,
8947 int enabled, int internal, unsigned flags,
8948 int display_canonical)
8949 {
8950 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
8951
8952 init_breakpoint_sal (b.get (), gdbarch,
8953 sals, std::move (location),
8954 std::move (filter),
8955 std::move (cond_string),
8956 std::move (extra_string),
8957 type, disposition,
8958 thread, task, ignore_count,
8959 ops, from_tty,
8960 enabled, internal, flags,
8961 display_canonical);
8962
8963 install_breakpoint (internal, std::move (b), 0);
8964 }
8965
8966 /* Add SALS.nelts breakpoints to the breakpoint table. For each
8967 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8968 value. COND_STRING, if not NULL, specified the condition to be
8969 used for all breakpoints. Essentially the only case where
8970 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8971 function. In that case, it's still not possible to specify
8972 separate conditions for different overloaded functions, so
8973 we take just a single condition string.
8974
8975 NOTE: If the function succeeds, the caller is expected to cleanup
8976 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
8977 array contents). If the function fails (error() is called), the
8978 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
8979 COND and SALS arrays and each of those arrays contents. */
8980
8981 static void
8982 create_breakpoints_sal (struct gdbarch *gdbarch,
8983 struct linespec_result *canonical,
8984 gdb::unique_xmalloc_ptr<char> cond_string,
8985 gdb::unique_xmalloc_ptr<char> extra_string,
8986 enum bptype type, enum bpdisp disposition,
8987 int thread, int task, int ignore_count,
8988 const struct breakpoint_ops *ops, int from_tty,
8989 int enabled, int internal, unsigned flags)
8990 {
8991 if (canonical->pre_expanded)
8992 gdb_assert (canonical->lsals.size () == 1);
8993
8994 for (const auto &lsal : canonical->lsals)
8995 {
8996 /* Note that 'location' can be NULL in the case of a plain
8997 'break', without arguments. */
8998 event_location_up location
8999 = (canonical->location != NULL
9000 ? copy_event_location (canonical->location.get ()) : NULL);
9001 gdb::unique_xmalloc_ptr<char> filter_string
9002 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
9003
9004 create_breakpoint_sal (gdbarch, lsal.sals,
9005 std::move (location),
9006 std::move (filter_string),
9007 std::move (cond_string),
9008 std::move (extra_string),
9009 type, disposition,
9010 thread, task, ignore_count, ops,
9011 from_tty, enabled, internal, flags,
9012 canonical->special_display);
9013 }
9014 }
9015
9016 /* Parse LOCATION which is assumed to be a SAL specification possibly
9017 followed by conditionals. On return, SALS contains an array of SAL
9018 addresses found. LOCATION points to the end of the SAL (for
9019 linespec locations).
9020
9021 The array and the line spec strings are allocated on the heap, it is
9022 the caller's responsibility to free them. */
9023
9024 static void
9025 parse_breakpoint_sals (struct event_location *location,
9026 struct linespec_result *canonical)
9027 {
9028 struct symtab_and_line cursal;
9029
9030 if (event_location_type (location) == LINESPEC_LOCATION)
9031 {
9032 const char *spec = get_linespec_location (location)->spec_string;
9033
9034 if (spec == NULL)
9035 {
9036 /* The last displayed codepoint, if it's valid, is our default
9037 breakpoint address. */
9038 if (last_displayed_sal_is_valid ())
9039 {
9040 /* Set sal's pspace, pc, symtab, and line to the values
9041 corresponding to the last call to print_frame_info.
9042 Be sure to reinitialize LINE with NOTCURRENT == 0
9043 as the breakpoint line number is inappropriate otherwise.
9044 find_pc_line would adjust PC, re-set it back. */
9045 symtab_and_line sal = get_last_displayed_sal ();
9046 CORE_ADDR pc = sal.pc;
9047
9048 sal = find_pc_line (pc, 0);
9049
9050 /* "break" without arguments is equivalent to "break *PC"
9051 where PC is the last displayed codepoint's address. So
9052 make sure to set sal.explicit_pc to prevent GDB from
9053 trying to expand the list of sals to include all other
9054 instances with the same symtab and line. */
9055 sal.pc = pc;
9056 sal.explicit_pc = 1;
9057
9058 struct linespec_sals lsal;
9059 lsal.sals = {sal};
9060 lsal.canonical = NULL;
9061
9062 canonical->lsals.push_back (std::move (lsal));
9063 return;
9064 }
9065 else
9066 error (_("No default breakpoint address now."));
9067 }
9068 }
9069
9070 /* Force almost all breakpoints to be in terms of the
9071 current_source_symtab (which is decode_line_1's default).
9072 This should produce the results we want almost all of the
9073 time while leaving default_breakpoint_* alone.
9074
9075 ObjC: However, don't match an Objective-C method name which
9076 may have a '+' or '-' succeeded by a '['. */
9077 cursal = get_current_source_symtab_and_line ();
9078 if (last_displayed_sal_is_valid ())
9079 {
9080 const char *spec = NULL;
9081
9082 if (event_location_type (location) == LINESPEC_LOCATION)
9083 spec = get_linespec_location (location)->spec_string;
9084
9085 if (!cursal.symtab
9086 || (spec != NULL
9087 && strchr ("+-", spec[0]) != NULL
9088 && spec[1] != '['))
9089 {
9090 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9091 get_last_displayed_symtab (),
9092 get_last_displayed_line (),
9093 canonical, NULL, NULL);
9094 return;
9095 }
9096 }
9097
9098 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9099 cursal.symtab, cursal.line, canonical, NULL, NULL);
9100 }
9101
9102
9103 /* Convert each SAL into a real PC. Verify that the PC can be
9104 inserted as a breakpoint. If it can't throw an error. */
9105
9106 static void
9107 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
9108 {
9109 for (auto &sal : sals)
9110 resolve_sal_pc (&sal);
9111 }
9112
9113 /* Fast tracepoints may have restrictions on valid locations. For
9114 instance, a fast tracepoint using a jump instead of a trap will
9115 likely have to overwrite more bytes than a trap would, and so can
9116 only be placed where the instruction is longer than the jump, or a
9117 multi-instruction sequence does not have a jump into the middle of
9118 it, etc. */
9119
9120 static void
9121 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9122 gdb::array_view<const symtab_and_line> sals)
9123 {
9124 for (const auto &sal : sals)
9125 {
9126 struct gdbarch *sarch;
9127
9128 sarch = get_sal_arch (sal);
9129 /* We fall back to GDBARCH if there is no architecture
9130 associated with SAL. */
9131 if (sarch == NULL)
9132 sarch = gdbarch;
9133 std::string msg;
9134 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
9135 error (_("May not have a fast tracepoint at %s%s"),
9136 paddress (sarch, sal.pc), msg.c_str ());
9137 }
9138 }
9139
9140 /* Given TOK, a string specification of condition and thread, as
9141 accepted by the 'break' command, extract the condition
9142 string and thread number and set *COND_STRING and *THREAD.
9143 PC identifies the context at which the condition should be parsed.
9144 If no condition is found, *COND_STRING is set to NULL.
9145 If no thread is found, *THREAD is set to -1. */
9146
9147 static void
9148 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9149 char **cond_string, int *thread, int *task,
9150 char **rest)
9151 {
9152 *cond_string = NULL;
9153 *thread = -1;
9154 *task = 0;
9155 *rest = NULL;
9156 bool force = false;
9157
9158 while (tok && *tok)
9159 {
9160 const char *end_tok;
9161 int toklen;
9162 const char *cond_start = NULL;
9163 const char *cond_end = NULL;
9164
9165 tok = skip_spaces (tok);
9166
9167 if ((*tok == '"' || *tok == ',') && rest)
9168 {
9169 *rest = savestring (tok, strlen (tok));
9170 return;
9171 }
9172
9173 end_tok = skip_to_space (tok);
9174
9175 toklen = end_tok - tok;
9176
9177 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9178 {
9179 tok = cond_start = end_tok + 1;
9180 try
9181 {
9182 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9183 }
9184 catch (const gdb_exception_error &)
9185 {
9186 if (!force)
9187 throw;
9188 else
9189 tok = tok + strlen (tok);
9190 }
9191 cond_end = tok;
9192 *cond_string = savestring (cond_start, cond_end - cond_start);
9193 }
9194 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
9195 {
9196 tok = tok + toklen;
9197 force = true;
9198 }
9199 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9200 {
9201 const char *tmptok;
9202 struct thread_info *thr;
9203
9204 tok = end_tok + 1;
9205 thr = parse_thread_id (tok, &tmptok);
9206 if (tok == tmptok)
9207 error (_("Junk after thread keyword."));
9208 *thread = thr->global_num;
9209 tok = tmptok;
9210 }
9211 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9212 {
9213 char *tmptok;
9214
9215 tok = end_tok + 1;
9216 *task = strtol (tok, &tmptok, 0);
9217 if (tok == tmptok)
9218 error (_("Junk after task keyword."));
9219 if (!valid_task_id (*task))
9220 error (_("Unknown task %d."), *task);
9221 tok = tmptok;
9222 }
9223 else if (rest)
9224 {
9225 *rest = savestring (tok, strlen (tok));
9226 return;
9227 }
9228 else
9229 error (_("Junk at end of arguments."));
9230 }
9231 }
9232
9233 /* Call 'find_condition_and_thread' for each sal in SALS until a parse
9234 succeeds. The parsed values are written to COND_STRING, THREAD,
9235 TASK, and REST. See the comment of 'find_condition_and_thread'
9236 for the description of these parameters and INPUT. */
9237
9238 static void
9239 find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
9240 const char *input, char **cond_string,
9241 int *thread, int *task, char **rest)
9242 {
9243 int num_failures = 0;
9244 for (auto &sal : sals)
9245 {
9246 char *cond = nullptr;
9247 int thread_id = 0;
9248 int task_id = 0;
9249 char *remaining = nullptr;
9250
9251 /* Here we want to parse 'arg' to separate condition from thread
9252 number. But because parsing happens in a context and the
9253 contexts of sals might be different, try each until there is
9254 success. Finding one successful parse is sufficient for our
9255 goal. When setting the breakpoint we'll re-parse the
9256 condition in the context of each sal. */
9257 try
9258 {
9259 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
9260 &task_id, &remaining);
9261 *cond_string = cond;
9262 *thread = thread_id;
9263 *task = task_id;
9264 *rest = remaining;
9265 break;
9266 }
9267 catch (const gdb_exception_error &e)
9268 {
9269 num_failures++;
9270 /* If no sal remains, do not continue. */
9271 if (num_failures == sals.size ())
9272 throw;
9273 }
9274 }
9275 }
9276
9277 /* Decode a static tracepoint marker spec. */
9278
9279 static std::vector<symtab_and_line>
9280 decode_static_tracepoint_spec (const char **arg_p)
9281 {
9282 const char *p = &(*arg_p)[3];
9283 const char *endp;
9284
9285 p = skip_spaces (p);
9286
9287 endp = skip_to_space (p);
9288
9289 std::string marker_str (p, endp - p);
9290
9291 std::vector<static_tracepoint_marker> markers
9292 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9293 if (markers.empty ())
9294 error (_("No known static tracepoint marker named %s"),
9295 marker_str.c_str ());
9296
9297 std::vector<symtab_and_line> sals;
9298 sals.reserve (markers.size ());
9299
9300 for (const static_tracepoint_marker &marker : markers)
9301 {
9302 symtab_and_line sal = find_pc_line (marker.address, 0);
9303 sal.pc = marker.address;
9304 sals.push_back (sal);
9305 }
9306
9307 *arg_p = endp;
9308 return sals;
9309 }
9310
9311 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9312 according to IS_TRACEPOINT. */
9313
9314 static const struct breakpoint_ops *
9315 breakpoint_ops_for_event_location_type (enum event_location_type location_type,
9316 bool is_tracepoint)
9317 {
9318 if (is_tracepoint)
9319 {
9320 if (location_type == PROBE_LOCATION)
9321 return &tracepoint_probe_breakpoint_ops;
9322 else
9323 return &tracepoint_breakpoint_ops;
9324 }
9325 else
9326 {
9327 if (location_type == PROBE_LOCATION)
9328 return &bkpt_probe_breakpoint_ops;
9329 else
9330 return &bkpt_breakpoint_ops;
9331 }
9332 }
9333
9334 /* See breakpoint.h. */
9335
9336 const struct breakpoint_ops *
9337 breakpoint_ops_for_event_location (const struct event_location *location,
9338 bool is_tracepoint)
9339 {
9340 if (location != nullptr)
9341 return breakpoint_ops_for_event_location_type
9342 (event_location_type (location), is_tracepoint);
9343 return is_tracepoint ? &tracepoint_breakpoint_ops : &bkpt_breakpoint_ops;
9344 }
9345
9346 /* See breakpoint.h. */
9347
9348 int
9349 create_breakpoint (struct gdbarch *gdbarch,
9350 struct event_location *location,
9351 const char *cond_string,
9352 int thread, const char *extra_string,
9353 bool force_condition, int parse_extra,
9354 int tempflag, enum bptype type_wanted,
9355 int ignore_count,
9356 enum auto_boolean pending_break_support,
9357 const struct breakpoint_ops *ops,
9358 int from_tty, int enabled, int internal,
9359 unsigned flags)
9360 {
9361 struct linespec_result canonical;
9362 int pending = 0;
9363 int task = 0;
9364 int prev_bkpt_count = breakpoint_count;
9365
9366 gdb_assert (ops != NULL);
9367
9368 /* If extra_string isn't useful, set it to NULL. */
9369 if (extra_string != NULL && *extra_string == '\0')
9370 extra_string = NULL;
9371
9372 try
9373 {
9374 ops->create_sals_from_location (location, &canonical, type_wanted);
9375 }
9376 catch (const gdb_exception_error &e)
9377 {
9378 /* If caller is interested in rc value from parse, set
9379 value. */
9380 if (e.error == NOT_FOUND_ERROR)
9381 {
9382 /* If pending breakpoint support is turned off, throw
9383 error. */
9384
9385 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9386 throw;
9387
9388 exception_print (gdb_stderr, e);
9389
9390 /* If pending breakpoint support is auto query and the user
9391 selects no, then simply return the error code. */
9392 if (pending_break_support == AUTO_BOOLEAN_AUTO
9393 && !nquery (_("Make %s pending on future shared library load? "),
9394 bptype_string (type_wanted)))
9395 return 0;
9396
9397 /* At this point, either the user was queried about setting
9398 a pending breakpoint and selected yes, or pending
9399 breakpoint behavior is on and thus a pending breakpoint
9400 is defaulted on behalf of the user. */
9401 pending = 1;
9402 }
9403 else
9404 throw;
9405 }
9406
9407 if (!pending && canonical.lsals.empty ())
9408 return 0;
9409
9410 /* Resolve all line numbers to PC's and verify that the addresses
9411 are ok for the target. */
9412 if (!pending)
9413 {
9414 for (auto &lsal : canonical.lsals)
9415 breakpoint_sals_to_pc (lsal.sals);
9416 }
9417
9418 /* Fast tracepoints may have additional restrictions on location. */
9419 if (!pending && type_wanted == bp_fast_tracepoint)
9420 {
9421 for (const auto &lsal : canonical.lsals)
9422 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9423 }
9424
9425 /* Verify that condition can be parsed, before setting any
9426 breakpoints. Allocate a separate condition expression for each
9427 breakpoint. */
9428 if (!pending)
9429 {
9430 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9431 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9432
9433 if (parse_extra)
9434 {
9435 char *rest;
9436 char *cond;
9437
9438 const linespec_sals &lsal = canonical.lsals[0];
9439
9440 find_condition_and_thread_for_sals (lsal.sals, extra_string,
9441 &cond, &thread, &task, &rest);
9442 cond_string_copy.reset (cond);
9443 extra_string_copy.reset (rest);
9444 }
9445 else
9446 {
9447 if (type_wanted != bp_dprintf
9448 && extra_string != NULL && *extra_string != '\0')
9449 error (_("Garbage '%s' at end of location"), extra_string);
9450
9451 /* Check the validity of the condition. We should error out
9452 if the condition is invalid at all of the locations and
9453 if it is not forced. In the PARSE_EXTRA case above, this
9454 check is done when parsing the EXTRA_STRING. */
9455 if (cond_string != nullptr && !force_condition)
9456 {
9457 int num_failures = 0;
9458 const linespec_sals &lsal = canonical.lsals[0];
9459 for (const auto &sal : lsal.sals)
9460 {
9461 const char *cond = cond_string;
9462 try
9463 {
9464 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
9465 /* One success is sufficient to keep going. */
9466 break;
9467 }
9468 catch (const gdb_exception_error &)
9469 {
9470 num_failures++;
9471 /* If this is the last sal, error out. */
9472 if (num_failures == lsal.sals.size ())
9473 throw;
9474 }
9475 }
9476 }
9477
9478 /* Create a private copy of condition string. */
9479 if (cond_string)
9480 cond_string_copy.reset (xstrdup (cond_string));
9481 /* Create a private copy of any extra string. */
9482 if (extra_string)
9483 extra_string_copy.reset (xstrdup (extra_string));
9484 }
9485
9486 ops->create_breakpoints_sal (gdbarch, &canonical,
9487 std::move (cond_string_copy),
9488 std::move (extra_string_copy),
9489 type_wanted,
9490 tempflag ? disp_del : disp_donttouch,
9491 thread, task, ignore_count, ops,
9492 from_tty, enabled, internal, flags);
9493 }
9494 else
9495 {
9496 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9497
9498 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9499 b->location = copy_event_location (location);
9500
9501 if (parse_extra)
9502 b->cond_string = NULL;
9503 else
9504 {
9505 /* Create a private copy of condition string. */
9506 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
9507 b->thread = thread;
9508 }
9509
9510 /* Create a private copy of any extra string. */
9511 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
9512 b->ignore_count = ignore_count;
9513 b->disposition = tempflag ? disp_del : disp_donttouch;
9514 b->condition_not_parsed = 1;
9515 b->enable_state = enabled ? bp_enabled : bp_disabled;
9516 if ((type_wanted != bp_breakpoint
9517 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9518 b->pspace = current_program_space;
9519
9520 install_breakpoint (internal, std::move (b), 0);
9521 }
9522
9523 if (canonical.lsals.size () > 1)
9524 {
9525 warning (_("Multiple breakpoints were set.\nUse the "
9526 "\"delete\" command to delete unwanted breakpoints."));
9527 prev_breakpoint_count = prev_bkpt_count;
9528 }
9529
9530 update_global_location_list (UGLL_MAY_INSERT);
9531
9532 return 1;
9533 }
9534
9535 /* Set a breakpoint.
9536 ARG is a string describing breakpoint address,
9537 condition, and thread.
9538 FLAG specifies if a breakpoint is hardware on,
9539 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9540 and BP_TEMPFLAG. */
9541
9542 static void
9543 break_command_1 (const char *arg, int flag, int from_tty)
9544 {
9545 int tempflag = flag & BP_TEMPFLAG;
9546 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9547 ? bp_hardware_breakpoint
9548 : bp_breakpoint);
9549
9550 event_location_up location = string_to_event_location (&arg, current_language);
9551 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9552 (location.get (), false /* is_tracepoint */);
9553
9554 create_breakpoint (get_current_arch (),
9555 location.get (),
9556 NULL, 0, arg, false, 1 /* parse arg */,
9557 tempflag, type_wanted,
9558 0 /* Ignore count */,
9559 pending_break_support,
9560 ops,
9561 from_tty,
9562 1 /* enabled */,
9563 0 /* internal */,
9564 0);
9565 }
9566
9567 /* Helper function for break_command_1 and disassemble_command. */
9568
9569 void
9570 resolve_sal_pc (struct symtab_and_line *sal)
9571 {
9572 CORE_ADDR pc;
9573
9574 if (sal->pc == 0 && sal->symtab != NULL)
9575 {
9576 if (!find_line_pc (sal->symtab, sal->line, &pc))
9577 error (_("No line %d in file \"%s\"."),
9578 sal->line, symtab_to_filename_for_display (sal->symtab));
9579 sal->pc = pc;
9580
9581 /* If this SAL corresponds to a breakpoint inserted using a line
9582 number, then skip the function prologue if necessary. */
9583 if (sal->explicit_line)
9584 skip_prologue_sal (sal);
9585 }
9586
9587 if (sal->section == 0 && sal->symtab != NULL)
9588 {
9589 const struct blockvector *bv;
9590 const struct block *b;
9591 struct symbol *sym;
9592
9593 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9594 SYMTAB_COMPUNIT (sal->symtab));
9595 if (bv != NULL)
9596 {
9597 sym = block_linkage_function (b);
9598 if (sym != NULL)
9599 {
9600 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9601 sal->section = sym->obj_section (SYMTAB_OBJFILE (sal->symtab));
9602 }
9603 else
9604 {
9605 /* It really is worthwhile to have the section, so we'll
9606 just have to look harder. This case can be executed
9607 if we have line numbers but no functions (as can
9608 happen in assembly source). */
9609
9610 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9611 switch_to_program_space_and_thread (sal->pspace);
9612
9613 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9614 if (msym.minsym)
9615 sal->section = msym.obj_section ();
9616 }
9617 }
9618 }
9619 }
9620
9621 void
9622 break_command (const char *arg, int from_tty)
9623 {
9624 break_command_1 (arg, 0, from_tty);
9625 }
9626
9627 void
9628 tbreak_command (const char *arg, int from_tty)
9629 {
9630 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9631 }
9632
9633 static void
9634 hbreak_command (const char *arg, int from_tty)
9635 {
9636 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9637 }
9638
9639 static void
9640 thbreak_command (const char *arg, int from_tty)
9641 {
9642 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9643 }
9644
9645 static void
9646 stop_command (const char *arg, int from_tty)
9647 {
9648 printf_filtered (_("Specify the type of breakpoint to set.\n\
9649 Usage: stop in <function | address>\n\
9650 stop at <line>\n"));
9651 }
9652
9653 static void
9654 stopin_command (const char *arg, int from_tty)
9655 {
9656 int badInput = 0;
9657
9658 if (arg == NULL)
9659 badInput = 1;
9660 else if (*arg != '*')
9661 {
9662 const char *argptr = arg;
9663 int hasColon = 0;
9664
9665 /* Look for a ':'. If this is a line number specification, then
9666 say it is bad, otherwise, it should be an address or
9667 function/method name. */
9668 while (*argptr && !hasColon)
9669 {
9670 hasColon = (*argptr == ':');
9671 argptr++;
9672 }
9673
9674 if (hasColon)
9675 badInput = (*argptr != ':'); /* Not a class::method */
9676 else
9677 badInput = isdigit (*arg); /* a simple line number */
9678 }
9679
9680 if (badInput)
9681 printf_filtered (_("Usage: stop in <function | address>\n"));
9682 else
9683 break_command_1 (arg, 0, from_tty);
9684 }
9685
9686 static void
9687 stopat_command (const char *arg, int from_tty)
9688 {
9689 int badInput = 0;
9690
9691 if (arg == NULL || *arg == '*') /* no line number */
9692 badInput = 1;
9693 else
9694 {
9695 const char *argptr = arg;
9696 int hasColon = 0;
9697
9698 /* Look for a ':'. If there is a '::' then get out, otherwise
9699 it is probably a line number. */
9700 while (*argptr && !hasColon)
9701 {
9702 hasColon = (*argptr == ':');
9703 argptr++;
9704 }
9705
9706 if (hasColon)
9707 badInput = (*argptr == ':'); /* we have class::method */
9708 else
9709 badInput = !isdigit (*arg); /* not a line number */
9710 }
9711
9712 if (badInput)
9713 printf_filtered (_("Usage: stop at LINE\n"));
9714 else
9715 break_command_1 (arg, 0, from_tty);
9716 }
9717
9718 /* The dynamic printf command is mostly like a regular breakpoint, but
9719 with a prewired command list consisting of a single output command,
9720 built from extra arguments supplied on the dprintf command
9721 line. */
9722
9723 static void
9724 dprintf_command (const char *arg, int from_tty)
9725 {
9726 event_location_up location = string_to_event_location (&arg, current_language);
9727
9728 /* If non-NULL, ARG should have been advanced past the location;
9729 the next character must be ','. */
9730 if (arg != NULL)
9731 {
9732 if (arg[0] != ',' || arg[1] == '\0')
9733 error (_("Format string required"));
9734 else
9735 {
9736 /* Skip the comma. */
9737 ++arg;
9738 }
9739 }
9740
9741 create_breakpoint (get_current_arch (),
9742 location.get (),
9743 NULL, 0, arg, false, 1 /* parse arg */,
9744 0, bp_dprintf,
9745 0 /* Ignore count */,
9746 pending_break_support,
9747 &dprintf_breakpoint_ops,
9748 from_tty,
9749 1 /* enabled */,
9750 0 /* internal */,
9751 0);
9752 }
9753
9754 static void
9755 agent_printf_command (const char *arg, int from_tty)
9756 {
9757 error (_("May only run agent-printf on the target"));
9758 }
9759
9760 /* Implement the "breakpoint_hit" breakpoint_ops method for
9761 ranged breakpoints. */
9762
9763 static int
9764 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9765 const address_space *aspace,
9766 CORE_ADDR bp_addr,
9767 const struct target_waitstatus *ws)
9768 {
9769 if (ws->kind != TARGET_WAITKIND_STOPPED
9770 || ws->value.sig != GDB_SIGNAL_TRAP)
9771 return 0;
9772
9773 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9774 bl->length, aspace, bp_addr);
9775 }
9776
9777 /* Implement the "resources_needed" breakpoint_ops method for
9778 ranged breakpoints. */
9779
9780 static int
9781 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9782 {
9783 return target_ranged_break_num_registers ();
9784 }
9785
9786 /* Implement the "print_it" breakpoint_ops method for
9787 ranged breakpoints. */
9788
9789 static enum print_stop_action
9790 print_it_ranged_breakpoint (bpstat bs)
9791 {
9792 struct breakpoint *b = bs->breakpoint_at;
9793 struct bp_location *bl = b->loc;
9794 struct ui_out *uiout = current_uiout;
9795
9796 gdb_assert (b->type == bp_hardware_breakpoint);
9797
9798 /* Ranged breakpoints have only one location. */
9799 gdb_assert (bl && bl->next == NULL);
9800
9801 annotate_breakpoint (b->number);
9802
9803 maybe_print_thread_hit_breakpoint (uiout);
9804
9805 if (b->disposition == disp_del)
9806 uiout->text ("Temporary ranged breakpoint ");
9807 else
9808 uiout->text ("Ranged breakpoint ");
9809 if (uiout->is_mi_like_p ())
9810 {
9811 uiout->field_string ("reason",
9812 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9813 uiout->field_string ("disp", bpdisp_text (b->disposition));
9814 }
9815 uiout->field_signed ("bkptno", b->number);
9816 uiout->text (", ");
9817
9818 return PRINT_SRC_AND_LOC;
9819 }
9820
9821 /* Implement the "print_one" breakpoint_ops method for
9822 ranged breakpoints. */
9823
9824 static void
9825 print_one_ranged_breakpoint (struct breakpoint *b,
9826 struct bp_location **last_loc)
9827 {
9828 struct bp_location *bl = b->loc;
9829 struct value_print_options opts;
9830 struct ui_out *uiout = current_uiout;
9831
9832 /* Ranged breakpoints have only one location. */
9833 gdb_assert (bl && bl->next == NULL);
9834
9835 get_user_print_options (&opts);
9836
9837 if (opts.addressprint)
9838 /* We don't print the address range here, it will be printed later
9839 by print_one_detail_ranged_breakpoint. */
9840 uiout->field_skip ("addr");
9841 annotate_field (5);
9842 print_breakpoint_location (b, bl);
9843 *last_loc = bl;
9844 }
9845
9846 /* Implement the "print_one_detail" breakpoint_ops method for
9847 ranged breakpoints. */
9848
9849 static void
9850 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9851 struct ui_out *uiout)
9852 {
9853 CORE_ADDR address_start, address_end;
9854 struct bp_location *bl = b->loc;
9855 string_file stb;
9856
9857 gdb_assert (bl);
9858
9859 address_start = bl->address;
9860 address_end = address_start + bl->length - 1;
9861
9862 uiout->text ("\taddress range: ");
9863 stb.printf ("[%s, %s]",
9864 print_core_address (bl->gdbarch, address_start),
9865 print_core_address (bl->gdbarch, address_end));
9866 uiout->field_stream ("addr", stb);
9867 uiout->text ("\n");
9868 }
9869
9870 /* Implement the "print_mention" breakpoint_ops method for
9871 ranged breakpoints. */
9872
9873 static void
9874 print_mention_ranged_breakpoint (struct breakpoint *b)
9875 {
9876 struct bp_location *bl = b->loc;
9877 struct ui_out *uiout = current_uiout;
9878
9879 gdb_assert (bl);
9880 gdb_assert (b->type == bp_hardware_breakpoint);
9881
9882 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9883 b->number, paddress (bl->gdbarch, bl->address),
9884 paddress (bl->gdbarch, bl->address + bl->length - 1));
9885 }
9886
9887 /* Implement the "print_recreate" breakpoint_ops method for
9888 ranged breakpoints. */
9889
9890 static void
9891 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9892 {
9893 fprintf_unfiltered (fp, "break-range %s, %s",
9894 event_location_to_string (b->location.get ()),
9895 event_location_to_string (b->location_range_end.get ()));
9896 print_recreate_thread (b, fp);
9897 }
9898
9899 /* The breakpoint_ops structure to be used in ranged breakpoints. */
9900
9901 static struct breakpoint_ops ranged_breakpoint_ops;
9902
9903 /* Find the address where the end of the breakpoint range should be
9904 placed, given the SAL of the end of the range. This is so that if
9905 the user provides a line number, the end of the range is set to the
9906 last instruction of the given line. */
9907
9908 static CORE_ADDR
9909 find_breakpoint_range_end (struct symtab_and_line sal)
9910 {
9911 CORE_ADDR end;
9912
9913 /* If the user provided a PC value, use it. Otherwise,
9914 find the address of the end of the given location. */
9915 if (sal.explicit_pc)
9916 end = sal.pc;
9917 else
9918 {
9919 int ret;
9920 CORE_ADDR start;
9921
9922 ret = find_line_pc_range (sal, &start, &end);
9923 if (!ret)
9924 error (_("Could not find location of the end of the range."));
9925
9926 /* find_line_pc_range returns the start of the next line. */
9927 end--;
9928 }
9929
9930 return end;
9931 }
9932
9933 /* Implement the "break-range" CLI command. */
9934
9935 static void
9936 break_range_command (const char *arg, int from_tty)
9937 {
9938 const char *arg_start;
9939 struct linespec_result canonical_start, canonical_end;
9940 int bp_count, can_use_bp, length;
9941 CORE_ADDR end;
9942 struct breakpoint *b;
9943
9944 /* We don't support software ranged breakpoints. */
9945 if (target_ranged_break_num_registers () < 0)
9946 error (_("This target does not support hardware ranged breakpoints."));
9947
9948 bp_count = hw_breakpoint_used_count ();
9949 bp_count += target_ranged_break_num_registers ();
9950 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9951 bp_count, 0);
9952 if (can_use_bp < 0)
9953 error (_("Hardware breakpoints used exceeds limit."));
9954
9955 arg = skip_spaces (arg);
9956 if (arg == NULL || arg[0] == '\0')
9957 error(_("No address range specified."));
9958
9959 arg_start = arg;
9960 event_location_up start_location = string_to_event_location (&arg,
9961 current_language);
9962 parse_breakpoint_sals (start_location.get (), &canonical_start);
9963
9964 if (arg[0] != ',')
9965 error (_("Too few arguments."));
9966 else if (canonical_start.lsals.empty ())
9967 error (_("Could not find location of the beginning of the range."));
9968
9969 const linespec_sals &lsal_start = canonical_start.lsals[0];
9970
9971 if (canonical_start.lsals.size () > 1
9972 || lsal_start.sals.size () != 1)
9973 error (_("Cannot create a ranged breakpoint with multiple locations."));
9974
9975 const symtab_and_line &sal_start = lsal_start.sals[0];
9976 std::string addr_string_start (arg_start, arg - arg_start);
9977
9978 arg++; /* Skip the comma. */
9979 arg = skip_spaces (arg);
9980
9981 /* Parse the end location. */
9982
9983 arg_start = arg;
9984
9985 /* We call decode_line_full directly here instead of using
9986 parse_breakpoint_sals because we need to specify the start location's
9987 symtab and line as the default symtab and line for the end of the
9988 range. This makes it possible to have ranges like "foo.c:27, +14",
9989 where +14 means 14 lines from the start location. */
9990 event_location_up end_location = string_to_event_location (&arg,
9991 current_language);
9992 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
9993 sal_start.symtab, sal_start.line,
9994 &canonical_end, NULL, NULL);
9995
9996 if (canonical_end.lsals.empty ())
9997 error (_("Could not find location of the end of the range."));
9998
9999 const linespec_sals &lsal_end = canonical_end.lsals[0];
10000 if (canonical_end.lsals.size () > 1
10001 || lsal_end.sals.size () != 1)
10002 error (_("Cannot create a ranged breakpoint with multiple locations."));
10003
10004 const symtab_and_line &sal_end = lsal_end.sals[0];
10005
10006 end = find_breakpoint_range_end (sal_end);
10007 if (sal_start.pc > end)
10008 error (_("Invalid address range, end precedes start."));
10009
10010 length = end - sal_start.pc + 1;
10011 if (length < 0)
10012 /* Length overflowed. */
10013 error (_("Address range too large."));
10014 else if (length == 1)
10015 {
10016 /* This range is simple enough to be handled by
10017 the `hbreak' command. */
10018 hbreak_command (&addr_string_start[0], 1);
10019
10020 return;
10021 }
10022
10023 /* Now set up the breakpoint. */
10024 b = set_raw_breakpoint (get_current_arch (), sal_start,
10025 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10026 set_breakpoint_count (breakpoint_count + 1);
10027 b->number = breakpoint_count;
10028 b->disposition = disp_donttouch;
10029 b->location = std::move (start_location);
10030 b->location_range_end = std::move (end_location);
10031 b->loc->length = length;
10032
10033 mention (b);
10034 gdb::observers::breakpoint_created.notify (b);
10035 update_global_location_list (UGLL_MAY_INSERT);
10036 }
10037
10038 /* Return non-zero if EXP is verified as constant. Returned zero
10039 means EXP is variable. Also the constant detection may fail for
10040 some constant expressions and in such case still falsely return
10041 zero. */
10042
10043 static bool
10044 watchpoint_exp_is_const (const struct expression *exp)
10045 {
10046 return exp->op->constant_p ();
10047 }
10048
10049 /* Watchpoint destructor. */
10050
10051 watchpoint::~watchpoint ()
10052 {
10053 xfree (this->exp_string);
10054 xfree (this->exp_string_reparse);
10055 }
10056
10057 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10058
10059 static void
10060 re_set_watchpoint (struct breakpoint *b)
10061 {
10062 struct watchpoint *w = (struct watchpoint *) b;
10063
10064 /* Watchpoint can be either on expression using entirely global
10065 variables, or it can be on local variables.
10066
10067 Watchpoints of the first kind are never auto-deleted, and even
10068 persist across program restarts. Since they can use variables
10069 from shared libraries, we need to reparse expression as libraries
10070 are loaded and unloaded.
10071
10072 Watchpoints on local variables can also change meaning as result
10073 of solib event. For example, if a watchpoint uses both a local
10074 and a global variables in expression, it's a local watchpoint,
10075 but unloading of a shared library will make the expression
10076 invalid. This is not a very common use case, but we still
10077 re-evaluate expression, to avoid surprises to the user.
10078
10079 Note that for local watchpoints, we re-evaluate it only if
10080 watchpoints frame id is still valid. If it's not, it means the
10081 watchpoint is out of scope and will be deleted soon. In fact,
10082 I'm not sure we'll ever be called in this case.
10083
10084 If a local watchpoint's frame id is still valid, then
10085 w->exp_valid_block is likewise valid, and we can safely use it.
10086
10087 Don't do anything about disabled watchpoints, since they will be
10088 reevaluated again when enabled. */
10089 update_watchpoint (w, 1 /* reparse */);
10090 }
10091
10092 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10093
10094 static int
10095 insert_watchpoint (struct bp_location *bl)
10096 {
10097 struct watchpoint *w = (struct watchpoint *) bl->owner;
10098 int length = w->exact ? 1 : bl->length;
10099
10100 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10101 w->cond_exp.get ());
10102 }
10103
10104 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10105
10106 static int
10107 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10108 {
10109 struct watchpoint *w = (struct watchpoint *) bl->owner;
10110 int length = w->exact ? 1 : bl->length;
10111
10112 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10113 w->cond_exp.get ());
10114 }
10115
10116 static int
10117 breakpoint_hit_watchpoint (const struct bp_location *bl,
10118 const address_space *aspace, CORE_ADDR bp_addr,
10119 const struct target_waitstatus *ws)
10120 {
10121 struct breakpoint *b = bl->owner;
10122 struct watchpoint *w = (struct watchpoint *) b;
10123
10124 /* Continuable hardware watchpoints are treated as non-existent if the
10125 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10126 some data address). Otherwise gdb won't stop on a break instruction
10127 in the code (not from a breakpoint) when a hardware watchpoint has
10128 been defined. Also skip watchpoints which we know did not trigger
10129 (did not match the data address). */
10130 if (is_hardware_watchpoint (b)
10131 && w->watchpoint_triggered == watch_triggered_no)
10132 return 0;
10133
10134 return 1;
10135 }
10136
10137 static void
10138 check_status_watchpoint (bpstat bs)
10139 {
10140 gdb_assert (is_watchpoint (bs->breakpoint_at));
10141
10142 bpstat_check_watchpoint (bs);
10143 }
10144
10145 /* Implement the "resources_needed" breakpoint_ops method for
10146 hardware watchpoints. */
10147
10148 static int
10149 resources_needed_watchpoint (const struct bp_location *bl)
10150 {
10151 struct watchpoint *w = (struct watchpoint *) bl->owner;
10152 int length = w->exact? 1 : bl->length;
10153
10154 return target_region_ok_for_hw_watchpoint (bl->address, length);
10155 }
10156
10157 /* Implement the "works_in_software_mode" breakpoint_ops method for
10158 hardware watchpoints. */
10159
10160 static int
10161 works_in_software_mode_watchpoint (const struct breakpoint *b)
10162 {
10163 /* Read and access watchpoints only work with hardware support. */
10164 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10165 }
10166
10167 static enum print_stop_action
10168 print_it_watchpoint (bpstat bs)
10169 {
10170 struct breakpoint *b;
10171 enum print_stop_action result;
10172 struct watchpoint *w;
10173 struct ui_out *uiout = current_uiout;
10174
10175 gdb_assert (bs->bp_location_at != NULL);
10176
10177 b = bs->breakpoint_at;
10178 w = (struct watchpoint *) b;
10179
10180 annotate_watchpoint (b->number);
10181 maybe_print_thread_hit_breakpoint (uiout);
10182
10183 string_file stb;
10184
10185 gdb::optional<ui_out_emit_tuple> tuple_emitter;
10186 switch (b->type)
10187 {
10188 case bp_watchpoint:
10189 case bp_hardware_watchpoint:
10190 if (uiout->is_mi_like_p ())
10191 uiout->field_string
10192 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10193 mention (b);
10194 tuple_emitter.emplace (uiout, "value");
10195 uiout->text ("\nOld value = ");
10196 watchpoint_value_print (bs->old_val.get (), &stb);
10197 uiout->field_stream ("old", stb);
10198 uiout->text ("\nNew value = ");
10199 watchpoint_value_print (w->val.get (), &stb);
10200 uiout->field_stream ("new", stb);
10201 uiout->text ("\n");
10202 /* More than one watchpoint may have been triggered. */
10203 result = PRINT_UNKNOWN;
10204 break;
10205
10206 case bp_read_watchpoint:
10207 if (uiout->is_mi_like_p ())
10208 uiout->field_string
10209 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10210 mention (b);
10211 tuple_emitter.emplace (uiout, "value");
10212 uiout->text ("\nValue = ");
10213 watchpoint_value_print (w->val.get (), &stb);
10214 uiout->field_stream ("value", stb);
10215 uiout->text ("\n");
10216 result = PRINT_UNKNOWN;
10217 break;
10218
10219 case bp_access_watchpoint:
10220 if (bs->old_val != NULL)
10221 {
10222 if (uiout->is_mi_like_p ())
10223 uiout->field_string
10224 ("reason",
10225 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10226 mention (b);
10227 tuple_emitter.emplace (uiout, "value");
10228 uiout->text ("\nOld value = ");
10229 watchpoint_value_print (bs->old_val.get (), &stb);
10230 uiout->field_stream ("old", stb);
10231 uiout->text ("\nNew value = ");
10232 }
10233 else
10234 {
10235 mention (b);
10236 if (uiout->is_mi_like_p ())
10237 uiout->field_string
10238 ("reason",
10239 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10240 tuple_emitter.emplace (uiout, "value");
10241 uiout->text ("\nValue = ");
10242 }
10243 watchpoint_value_print (w->val.get (), &stb);
10244 uiout->field_stream ("new", stb);
10245 uiout->text ("\n");
10246 result = PRINT_UNKNOWN;
10247 break;
10248 default:
10249 result = PRINT_UNKNOWN;
10250 }
10251
10252 return result;
10253 }
10254
10255 /* Implement the "print_mention" breakpoint_ops method for hardware
10256 watchpoints. */
10257
10258 static void
10259 print_mention_watchpoint (struct breakpoint *b)
10260 {
10261 struct watchpoint *w = (struct watchpoint *) b;
10262 struct ui_out *uiout = current_uiout;
10263 const char *tuple_name;
10264
10265 switch (b->type)
10266 {
10267 case bp_watchpoint:
10268 uiout->text ("Watchpoint ");
10269 tuple_name = "wpt";
10270 break;
10271 case bp_hardware_watchpoint:
10272 uiout->text ("Hardware watchpoint ");
10273 tuple_name = "wpt";
10274 break;
10275 case bp_read_watchpoint:
10276 uiout->text ("Hardware read watchpoint ");
10277 tuple_name = "hw-rwpt";
10278 break;
10279 case bp_access_watchpoint:
10280 uiout->text ("Hardware access (read/write) watchpoint ");
10281 tuple_name = "hw-awpt";
10282 break;
10283 default:
10284 internal_error (__FILE__, __LINE__,
10285 _("Invalid hardware watchpoint type."));
10286 }
10287
10288 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10289 uiout->field_signed ("number", b->number);
10290 uiout->text (": ");
10291 uiout->field_string ("exp", w->exp_string);
10292 }
10293
10294 /* Implement the "print_recreate" breakpoint_ops method for
10295 watchpoints. */
10296
10297 static void
10298 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10299 {
10300 struct watchpoint *w = (struct watchpoint *) b;
10301
10302 switch (b->type)
10303 {
10304 case bp_watchpoint:
10305 case bp_hardware_watchpoint:
10306 fprintf_unfiltered (fp, "watch");
10307 break;
10308 case bp_read_watchpoint:
10309 fprintf_unfiltered (fp, "rwatch");
10310 break;
10311 case bp_access_watchpoint:
10312 fprintf_unfiltered (fp, "awatch");
10313 break;
10314 default:
10315 internal_error (__FILE__, __LINE__,
10316 _("Invalid watchpoint type."));
10317 }
10318
10319 fprintf_unfiltered (fp, " %s", w->exp_string);
10320 print_recreate_thread (b, fp);
10321 }
10322
10323 /* Implement the "explains_signal" breakpoint_ops method for
10324 watchpoints. */
10325
10326 static int
10327 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10328 {
10329 /* A software watchpoint cannot cause a signal other than
10330 GDB_SIGNAL_TRAP. */
10331 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10332 return 0;
10333
10334 return 1;
10335 }
10336
10337 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10338
10339 static struct breakpoint_ops watchpoint_breakpoint_ops;
10340
10341 /* Implement the "insert" breakpoint_ops method for
10342 masked hardware watchpoints. */
10343
10344 static int
10345 insert_masked_watchpoint (struct bp_location *bl)
10346 {
10347 struct watchpoint *w = (struct watchpoint *) bl->owner;
10348
10349 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10350 bl->watchpoint_type);
10351 }
10352
10353 /* Implement the "remove" breakpoint_ops method for
10354 masked hardware watchpoints. */
10355
10356 static int
10357 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10358 {
10359 struct watchpoint *w = (struct watchpoint *) bl->owner;
10360
10361 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10362 bl->watchpoint_type);
10363 }
10364
10365 /* Implement the "resources_needed" breakpoint_ops method for
10366 masked hardware watchpoints. */
10367
10368 static int
10369 resources_needed_masked_watchpoint (const struct bp_location *bl)
10370 {
10371 struct watchpoint *w = (struct watchpoint *) bl->owner;
10372
10373 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10374 }
10375
10376 /* Implement the "works_in_software_mode" breakpoint_ops method for
10377 masked hardware watchpoints. */
10378
10379 static int
10380 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10381 {
10382 return 0;
10383 }
10384
10385 /* Implement the "print_it" breakpoint_ops method for
10386 masked hardware watchpoints. */
10387
10388 static enum print_stop_action
10389 print_it_masked_watchpoint (bpstat bs)
10390 {
10391 struct breakpoint *b = bs->breakpoint_at;
10392 struct ui_out *uiout = current_uiout;
10393
10394 /* Masked watchpoints have only one location. */
10395 gdb_assert (b->loc && b->loc->next == NULL);
10396
10397 annotate_watchpoint (b->number);
10398 maybe_print_thread_hit_breakpoint (uiout);
10399
10400 switch (b->type)
10401 {
10402 case bp_hardware_watchpoint:
10403 if (uiout->is_mi_like_p ())
10404 uiout->field_string
10405 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10406 break;
10407
10408 case bp_read_watchpoint:
10409 if (uiout->is_mi_like_p ())
10410 uiout->field_string
10411 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10412 break;
10413
10414 case bp_access_watchpoint:
10415 if (uiout->is_mi_like_p ())
10416 uiout->field_string
10417 ("reason",
10418 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10419 break;
10420 default:
10421 internal_error (__FILE__, __LINE__,
10422 _("Invalid hardware watchpoint type."));
10423 }
10424
10425 mention (b);
10426 uiout->text (_("\n\
10427 Check the underlying instruction at PC for the memory\n\
10428 address and value which triggered this watchpoint.\n"));
10429 uiout->text ("\n");
10430
10431 /* More than one watchpoint may have been triggered. */
10432 return PRINT_UNKNOWN;
10433 }
10434
10435 /* Implement the "print_one_detail" breakpoint_ops method for
10436 masked hardware watchpoints. */
10437
10438 static void
10439 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10440 struct ui_out *uiout)
10441 {
10442 struct watchpoint *w = (struct watchpoint *) b;
10443
10444 /* Masked watchpoints have only one location. */
10445 gdb_assert (b->loc && b->loc->next == NULL);
10446
10447 uiout->text ("\tmask ");
10448 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10449 uiout->text ("\n");
10450 }
10451
10452 /* Implement the "print_mention" breakpoint_ops method for
10453 masked hardware watchpoints. */
10454
10455 static void
10456 print_mention_masked_watchpoint (struct breakpoint *b)
10457 {
10458 struct watchpoint *w = (struct watchpoint *) b;
10459 struct ui_out *uiout = current_uiout;
10460 const char *tuple_name;
10461
10462 switch (b->type)
10463 {
10464 case bp_hardware_watchpoint:
10465 uiout->text ("Masked hardware watchpoint ");
10466 tuple_name = "wpt";
10467 break;
10468 case bp_read_watchpoint:
10469 uiout->text ("Masked hardware read watchpoint ");
10470 tuple_name = "hw-rwpt";
10471 break;
10472 case bp_access_watchpoint:
10473 uiout->text ("Masked hardware access (read/write) watchpoint ");
10474 tuple_name = "hw-awpt";
10475 break;
10476 default:
10477 internal_error (__FILE__, __LINE__,
10478 _("Invalid hardware watchpoint type."));
10479 }
10480
10481 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10482 uiout->field_signed ("number", b->number);
10483 uiout->text (": ");
10484 uiout->field_string ("exp", w->exp_string);
10485 }
10486
10487 /* Implement the "print_recreate" breakpoint_ops method for
10488 masked hardware watchpoints. */
10489
10490 static void
10491 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10492 {
10493 struct watchpoint *w = (struct watchpoint *) b;
10494
10495 switch (b->type)
10496 {
10497 case bp_hardware_watchpoint:
10498 fprintf_unfiltered (fp, "watch");
10499 break;
10500 case bp_read_watchpoint:
10501 fprintf_unfiltered (fp, "rwatch");
10502 break;
10503 case bp_access_watchpoint:
10504 fprintf_unfiltered (fp, "awatch");
10505 break;
10506 default:
10507 internal_error (__FILE__, __LINE__,
10508 _("Invalid hardware watchpoint type."));
10509 }
10510
10511 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string,
10512 phex (w->hw_wp_mask, sizeof (CORE_ADDR)));
10513 print_recreate_thread (b, fp);
10514 }
10515
10516 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10517
10518 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10519
10520 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10521
10522 static bool
10523 is_masked_watchpoint (const struct breakpoint *b)
10524 {
10525 return b->ops == &masked_watchpoint_breakpoint_ops;
10526 }
10527
10528 /* accessflag: hw_write: watch write,
10529 hw_read: watch read,
10530 hw_access: watch access (read or write) */
10531 static void
10532 watch_command_1 (const char *arg, int accessflag, int from_tty,
10533 bool just_location, bool internal)
10534 {
10535 struct breakpoint *scope_breakpoint = NULL;
10536 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10537 struct value *result;
10538 int saved_bitpos = 0, saved_bitsize = 0;
10539 const char *exp_start = NULL;
10540 const char *exp_end = NULL;
10541 const char *tok, *end_tok;
10542 int toklen = -1;
10543 const char *cond_start = NULL;
10544 const char *cond_end = NULL;
10545 enum bptype bp_type;
10546 int thread = -1;
10547 /* Flag to indicate whether we are going to use masks for
10548 the hardware watchpoint. */
10549 bool use_mask = false;
10550 CORE_ADDR mask = 0;
10551
10552 /* Make sure that we actually have parameters to parse. */
10553 if (arg != NULL && arg[0] != '\0')
10554 {
10555 const char *value_start;
10556
10557 exp_end = arg + strlen (arg);
10558
10559 /* Look for "parameter value" pairs at the end
10560 of the arguments string. */
10561 for (tok = exp_end - 1; tok > arg; tok--)
10562 {
10563 /* Skip whitespace at the end of the argument list. */
10564 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10565 tok--;
10566
10567 /* Find the beginning of the last token.
10568 This is the value of the parameter. */
10569 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10570 tok--;
10571 value_start = tok + 1;
10572
10573 /* Skip whitespace. */
10574 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10575 tok--;
10576
10577 end_tok = tok;
10578
10579 /* Find the beginning of the second to last token.
10580 This is the parameter itself. */
10581 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10582 tok--;
10583 tok++;
10584 toklen = end_tok - tok + 1;
10585
10586 if (toklen == 6 && startswith (tok, "thread"))
10587 {
10588 struct thread_info *thr;
10589 /* At this point we've found a "thread" token, which means
10590 the user is trying to set a watchpoint that triggers
10591 only in a specific thread. */
10592 const char *endp;
10593
10594 if (thread != -1)
10595 error(_("You can specify only one thread."));
10596
10597 /* Extract the thread ID from the next token. */
10598 thr = parse_thread_id (value_start, &endp);
10599
10600 /* Check if the user provided a valid thread ID. */
10601 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10602 invalid_thread_id_error (value_start);
10603
10604 thread = thr->global_num;
10605 }
10606 else if (toklen == 4 && startswith (tok, "mask"))
10607 {
10608 /* We've found a "mask" token, which means the user wants to
10609 create a hardware watchpoint that is going to have the mask
10610 facility. */
10611 struct value *mask_value, *mark;
10612
10613 if (use_mask)
10614 error(_("You can specify only one mask."));
10615
10616 use_mask = just_location = true;
10617
10618 mark = value_mark ();
10619 mask_value = parse_to_comma_and_eval (&value_start);
10620 mask = value_as_address (mask_value);
10621 value_free_to_mark (mark);
10622 }
10623 else
10624 /* We didn't recognize what we found. We should stop here. */
10625 break;
10626
10627 /* Truncate the string and get rid of the "parameter value" pair before
10628 the arguments string is parsed by the parse_exp_1 function. */
10629 exp_end = tok;
10630 }
10631 }
10632 else
10633 exp_end = arg;
10634
10635 /* Parse the rest of the arguments. From here on out, everything
10636 is in terms of a newly allocated string instead of the original
10637 ARG. */
10638 std::string expression (arg, exp_end - arg);
10639 exp_start = arg = expression.c_str ();
10640 innermost_block_tracker tracker;
10641 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10642 exp_end = arg;
10643 /* Remove trailing whitespace from the expression before saving it.
10644 This makes the eventual display of the expression string a bit
10645 prettier. */
10646 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10647 --exp_end;
10648
10649 /* Checking if the expression is not constant. */
10650 if (watchpoint_exp_is_const (exp.get ()))
10651 {
10652 int len;
10653
10654 len = exp_end - exp_start;
10655 while (len > 0 && isspace (exp_start[len - 1]))
10656 len--;
10657 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10658 }
10659
10660 exp_valid_block = tracker.block ();
10661 struct value *mark = value_mark ();
10662 struct value *val_as_value = nullptr;
10663 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10664 just_location);
10665
10666 if (val_as_value != NULL && just_location)
10667 {
10668 saved_bitpos = value_bitpos (val_as_value);
10669 saved_bitsize = value_bitsize (val_as_value);
10670 }
10671
10672 value_ref_ptr val;
10673 if (just_location)
10674 {
10675 int ret;
10676
10677 exp_valid_block = NULL;
10678 val = release_value (value_addr (result));
10679 value_free_to_mark (mark);
10680
10681 if (use_mask)
10682 {
10683 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10684 mask);
10685 if (ret == -1)
10686 error (_("This target does not support masked watchpoints."));
10687 else if (ret == -2)
10688 error (_("Invalid mask or memory region."));
10689 }
10690 }
10691 else if (val_as_value != NULL)
10692 val = release_value (val_as_value);
10693
10694 tok = skip_spaces (arg);
10695 end_tok = skip_to_space (tok);
10696
10697 toklen = end_tok - tok;
10698 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10699 {
10700 tok = cond_start = end_tok + 1;
10701 innermost_block_tracker if_tracker;
10702 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10703
10704 /* The watchpoint expression may not be local, but the condition
10705 may still be. E.g.: `watch global if local > 0'. */
10706 cond_exp_valid_block = if_tracker.block ();
10707
10708 cond_end = tok;
10709 }
10710 if (*tok)
10711 error (_("Junk at end of command."));
10712
10713 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10714
10715 /* Save this because create_internal_breakpoint below invalidates
10716 'wp_frame'. */
10717 frame_id watchpoint_frame = get_frame_id (wp_frame);
10718
10719 /* If the expression is "local", then set up a "watchpoint scope"
10720 breakpoint at the point where we've left the scope of the watchpoint
10721 expression. Create the scope breakpoint before the watchpoint, so
10722 that we will encounter it first in bpstat_stop_status. */
10723 if (exp_valid_block != NULL && wp_frame != NULL)
10724 {
10725 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10726
10727 if (frame_id_p (caller_frame_id))
10728 {
10729 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10730 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10731
10732 scope_breakpoint
10733 = create_internal_breakpoint (caller_arch, caller_pc,
10734 bp_watchpoint_scope,
10735 &momentary_breakpoint_ops);
10736
10737 /* create_internal_breakpoint could invalidate WP_FRAME. */
10738 wp_frame = NULL;
10739
10740 scope_breakpoint->enable_state = bp_enabled;
10741
10742 /* Automatically delete the breakpoint when it hits. */
10743 scope_breakpoint->disposition = disp_del;
10744
10745 /* Only break in the proper frame (help with recursion). */
10746 scope_breakpoint->frame_id = caller_frame_id;
10747
10748 /* Set the address at which we will stop. */
10749 scope_breakpoint->loc->gdbarch = caller_arch;
10750 scope_breakpoint->loc->requested_address = caller_pc;
10751 scope_breakpoint->loc->address
10752 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10753 scope_breakpoint->loc->requested_address,
10754 scope_breakpoint->type);
10755 }
10756 }
10757
10758 /* Now set up the breakpoint. We create all watchpoints as hardware
10759 watchpoints here even if hardware watchpoints are turned off, a call
10760 to update_watchpoint later in this function will cause the type to
10761 drop back to bp_watchpoint (software watchpoint) if required. */
10762
10763 if (accessflag == hw_read)
10764 bp_type = bp_read_watchpoint;
10765 else if (accessflag == hw_access)
10766 bp_type = bp_access_watchpoint;
10767 else
10768 bp_type = bp_hardware_watchpoint;
10769
10770 std::unique_ptr<watchpoint> w (new watchpoint ());
10771
10772 if (use_mask)
10773 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10774 &masked_watchpoint_breakpoint_ops);
10775 else
10776 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10777 &watchpoint_breakpoint_ops);
10778 w->thread = thread;
10779 w->disposition = disp_donttouch;
10780 w->pspace = current_program_space;
10781 w->exp = std::move (exp);
10782 w->exp_valid_block = exp_valid_block;
10783 w->cond_exp_valid_block = cond_exp_valid_block;
10784 if (just_location)
10785 {
10786 struct type *t = value_type (val.get ());
10787 CORE_ADDR addr = value_as_address (val.get ());
10788
10789 w->exp_string_reparse
10790 = current_language->watch_location_expression (t, addr).release ();
10791
10792 w->exp_string = xstrprintf ("-location %.*s",
10793 (int) (exp_end - exp_start), exp_start);
10794 }
10795 else
10796 w->exp_string = savestring (exp_start, exp_end - exp_start);
10797
10798 if (use_mask)
10799 {
10800 w->hw_wp_mask = mask;
10801 }
10802 else
10803 {
10804 w->val = val;
10805 w->val_bitpos = saved_bitpos;
10806 w->val_bitsize = saved_bitsize;
10807 w->val_valid = true;
10808 }
10809
10810 if (cond_start)
10811 w->cond_string = savestring (cond_start, cond_end - cond_start);
10812 else
10813 w->cond_string = 0;
10814
10815 if (frame_id_p (watchpoint_frame))
10816 {
10817 w->watchpoint_frame = watchpoint_frame;
10818 w->watchpoint_thread = inferior_ptid;
10819 }
10820 else
10821 {
10822 w->watchpoint_frame = null_frame_id;
10823 w->watchpoint_thread = null_ptid;
10824 }
10825
10826 if (scope_breakpoint != NULL)
10827 {
10828 /* The scope breakpoint is related to the watchpoint. We will
10829 need to act on them together. */
10830 w->related_breakpoint = scope_breakpoint;
10831 scope_breakpoint->related_breakpoint = w.get ();
10832 }
10833
10834 if (!just_location)
10835 value_free_to_mark (mark);
10836
10837 /* Finally update the new watchpoint. This creates the locations
10838 that should be inserted. */
10839 update_watchpoint (w.get (), 1);
10840
10841 install_breakpoint (internal, std::move (w), 1);
10842 }
10843
10844 /* Return count of debug registers needed to watch the given expression.
10845 If the watchpoint cannot be handled in hardware return zero. */
10846
10847 static int
10848 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10849 {
10850 int found_memory_cnt = 0;
10851
10852 /* Did the user specifically forbid us to use hardware watchpoints? */
10853 if (!can_use_hw_watchpoints)
10854 return 0;
10855
10856 gdb_assert (!vals.empty ());
10857 struct value *head = vals[0].get ();
10858
10859 /* Make sure that the value of the expression depends only upon
10860 memory contents, and values computed from them within GDB. If we
10861 find any register references or function calls, we can't use a
10862 hardware watchpoint.
10863
10864 The idea here is that evaluating an expression generates a series
10865 of values, one holding the value of every subexpression. (The
10866 expression a*b+c has five subexpressions: a, b, a*b, c, and
10867 a*b+c.) GDB's values hold almost enough information to establish
10868 the criteria given above --- they identify memory lvalues,
10869 register lvalues, computed values, etcetera. So we can evaluate
10870 the expression, and then scan the chain of values that leaves
10871 behind to decide whether we can detect any possible change to the
10872 expression's final value using only hardware watchpoints.
10873
10874 However, I don't think that the values returned by inferior
10875 function calls are special in any way. So this function may not
10876 notice that an expression involving an inferior function call
10877 can't be watched with hardware watchpoints. FIXME. */
10878 for (const value_ref_ptr &iter : vals)
10879 {
10880 struct value *v = iter.get ();
10881
10882 if (VALUE_LVAL (v) == lval_memory)
10883 {
10884 if (v != head && value_lazy (v))
10885 /* A lazy memory lvalue in the chain is one that GDB never
10886 needed to fetch; we either just used its address (e.g.,
10887 `a' in `a.b') or we never needed it at all (e.g., `a'
10888 in `a,b'). This doesn't apply to HEAD; if that is
10889 lazy then it was not readable, but watch it anyway. */
10890 ;
10891 else
10892 {
10893 /* Ahh, memory we actually used! Check if we can cover
10894 it with hardware watchpoints. */
10895 struct type *vtype = check_typedef (value_type (v));
10896
10897 /* We only watch structs and arrays if user asked for it
10898 explicitly, never if they just happen to appear in a
10899 middle of some value chain. */
10900 if (v == head
10901 || (vtype->code () != TYPE_CODE_STRUCT
10902 && vtype->code () != TYPE_CODE_ARRAY))
10903 {
10904 CORE_ADDR vaddr = value_address (v);
10905 int len;
10906 int num_regs;
10907
10908 len = (target_exact_watchpoints
10909 && is_scalar_type_recursive (vtype))?
10910 1 : TYPE_LENGTH (value_type (v));
10911
10912 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10913 if (!num_regs)
10914 return 0;
10915 else
10916 found_memory_cnt += num_regs;
10917 }
10918 }
10919 }
10920 else if (VALUE_LVAL (v) != not_lval
10921 && deprecated_value_modifiable (v) == 0)
10922 return 0; /* These are values from the history (e.g., $1). */
10923 else if (VALUE_LVAL (v) == lval_register)
10924 return 0; /* Cannot watch a register with a HW watchpoint. */
10925 }
10926
10927 /* The expression itself looks suitable for using a hardware
10928 watchpoint, but give the target machine a chance to reject it. */
10929 return found_memory_cnt;
10930 }
10931
10932 void
10933 watch_command_wrapper (const char *arg, int from_tty, bool internal)
10934 {
10935 watch_command_1 (arg, hw_write, from_tty, 0, internal);
10936 }
10937
10938 /* Options for the watch, awatch, and rwatch commands. */
10939
10940 struct watch_options
10941 {
10942 /* For -location. */
10943 bool location = false;
10944 };
10945
10946 /* Definitions of options for the "watch", "awatch", and "rwatch" commands.
10947
10948 Historically GDB always accepted both '-location' and '-l' flags for
10949 these commands (both flags being synonyms). When converting to the
10950 newer option scheme only '-location' is added here. That's fine (for
10951 backward compatibility) as any non-ambiguous prefix of a flag will be
10952 accepted, so '-l', '-loc', are now all accepted.
10953
10954 What this means is that, if in the future, we add any new flag here
10955 that starts with '-l' then this will break backward compatibility, so
10956 please, don't do that! */
10957
10958 static const gdb::option::option_def watch_option_defs[] = {
10959 gdb::option::flag_option_def<watch_options> {
10960 "location",
10961 [] (watch_options *opt) { return &opt->location; },
10962 N_("\
10963 This evaluates EXPRESSION and watches the memory to which is refers.\n\
10964 -l can be used as a short form of -location."),
10965 },
10966 };
10967
10968 /* Returns the option group used by 'watch', 'awatch', and 'rwatch'
10969 commands. */
10970
10971 static gdb::option::option_def_group
10972 make_watch_options_def_group (watch_options *opts)
10973 {
10974 return {{watch_option_defs}, opts};
10975 }
10976
10977 /* A helper function that looks for the "-location" argument and then
10978 calls watch_command_1. */
10979
10980 static void
10981 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
10982 {
10983 watch_options opts;
10984 auto grp = make_watch_options_def_group (&opts);
10985 gdb::option::process_options
10986 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
10987 if (arg != nullptr && *arg == '\0')
10988 arg = nullptr;
10989
10990 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
10991 }
10992
10993 /* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
10994 static void
10995 watch_command_completer (struct cmd_list_element *ignore,
10996 completion_tracker &tracker,
10997 const char *text, const char * /*word*/)
10998 {
10999 const auto group = make_watch_options_def_group (nullptr);
11000 if (gdb::option::complete_options
11001 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
11002 return;
11003
11004 const char *word = advance_to_expression_complete_word_point (tracker, text);
11005 expression_completer (ignore, tracker, text, word);
11006 }
11007
11008 static void
11009 watch_command (const char *arg, int from_tty)
11010 {
11011 watch_maybe_just_location (arg, hw_write, from_tty);
11012 }
11013
11014 void
11015 rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
11016 {
11017 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11018 }
11019
11020 static void
11021 rwatch_command (const char *arg, int from_tty)
11022 {
11023 watch_maybe_just_location (arg, hw_read, from_tty);
11024 }
11025
11026 void
11027 awatch_command_wrapper (const char *arg, int from_tty, bool internal)
11028 {
11029 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11030 }
11031
11032 static void
11033 awatch_command (const char *arg, int from_tty)
11034 {
11035 watch_maybe_just_location (arg, hw_access, from_tty);
11036 }
11037 \f
11038
11039 /* Data for the FSM that manages the until(location)/advance commands
11040 in infcmd.c. Here because it uses the mechanisms of
11041 breakpoints. */
11042
11043 struct until_break_fsm : public thread_fsm
11044 {
11045 /* The thread that was current when the command was executed. */
11046 int thread;
11047
11048 /* The breakpoint set at the return address in the caller frame,
11049 plus breakpoints at all the destination locations. */
11050 std::vector<breakpoint_up> breakpoints;
11051
11052 until_break_fsm (struct interp *cmd_interp, int thread,
11053 std::vector<breakpoint_up> &&breakpoints)
11054 : thread_fsm (cmd_interp),
11055 thread (thread),
11056 breakpoints (std::move (breakpoints))
11057 {
11058 }
11059
11060 void clean_up (struct thread_info *thread) override;
11061 bool should_stop (struct thread_info *thread) override;
11062 enum async_reply_reason do_async_reply_reason () override;
11063 };
11064
11065 /* Implementation of the 'should_stop' FSM method for the
11066 until(location)/advance commands. */
11067
11068 bool
11069 until_break_fsm::should_stop (struct thread_info *tp)
11070 {
11071 for (const breakpoint_up &bp : breakpoints)
11072 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11073 bp.get ()) != NULL)
11074 {
11075 set_finished ();
11076 break;
11077 }
11078
11079 return true;
11080 }
11081
11082 /* Implementation of the 'clean_up' FSM method for the
11083 until(location)/advance commands. */
11084
11085 void
11086 until_break_fsm::clean_up (struct thread_info *)
11087 {
11088 /* Clean up our temporary breakpoints. */
11089 breakpoints.clear ();
11090 delete_longjmp_breakpoint (thread);
11091 }
11092
11093 /* Implementation of the 'async_reply_reason' FSM method for the
11094 until(location)/advance commands. */
11095
11096 enum async_reply_reason
11097 until_break_fsm::do_async_reply_reason ()
11098 {
11099 return EXEC_ASYNC_LOCATION_REACHED;
11100 }
11101
11102 void
11103 until_break_command (const char *arg, int from_tty, int anywhere)
11104 {
11105 struct frame_info *frame;
11106 struct gdbarch *frame_gdbarch;
11107 struct frame_id stack_frame_id;
11108 struct frame_id caller_frame_id;
11109 int thread;
11110 struct thread_info *tp;
11111
11112 clear_proceed_status (0);
11113
11114 /* Set a breakpoint where the user wants it and at return from
11115 this function. */
11116
11117 event_location_up location = string_to_event_location (&arg, current_language);
11118
11119 std::vector<symtab_and_line> sals
11120 = (last_displayed_sal_is_valid ()
11121 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11122 get_last_displayed_symtab (),
11123 get_last_displayed_line ())
11124 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11125 NULL, NULL, 0));
11126
11127 if (sals.empty ())
11128 error (_("Couldn't get information on specified line."));
11129
11130 if (*arg)
11131 error (_("Junk at end of arguments."));
11132
11133 tp = inferior_thread ();
11134 thread = tp->global_num;
11135
11136 /* Note linespec handling above invalidates the frame chain.
11137 Installing a breakpoint also invalidates the frame chain (as it
11138 may need to switch threads), so do any frame handling before
11139 that. */
11140
11141 frame = get_selected_frame (NULL);
11142 frame_gdbarch = get_frame_arch (frame);
11143 stack_frame_id = get_stack_frame_id (frame);
11144 caller_frame_id = frame_unwind_caller_id (frame);
11145
11146 /* Keep within the current frame, or in frames called by the current
11147 one. */
11148
11149 std::vector<breakpoint_up> breakpoints;
11150
11151 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11152
11153 if (frame_id_p (caller_frame_id))
11154 {
11155 struct symtab_and_line sal2;
11156 struct gdbarch *caller_gdbarch;
11157
11158 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11159 sal2.pc = frame_unwind_caller_pc (frame);
11160 caller_gdbarch = frame_unwind_caller_arch (frame);
11161
11162 breakpoint_up caller_breakpoint
11163 = set_momentary_breakpoint (caller_gdbarch, sal2,
11164 caller_frame_id, bp_until);
11165 breakpoints.emplace_back (std::move (caller_breakpoint));
11166
11167 set_longjmp_breakpoint (tp, caller_frame_id);
11168 lj_deleter.emplace (thread);
11169 }
11170
11171 /* set_momentary_breakpoint could invalidate FRAME. */
11172 frame = NULL;
11173
11174 /* If the user told us to continue until a specified location, we
11175 don't specify a frame at which we need to stop. Otherwise,
11176 specify the selected frame, because we want to stop only at the
11177 very same frame. */
11178 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
11179
11180 for (symtab_and_line &sal : sals)
11181 {
11182 resolve_sal_pc (&sal);
11183
11184 breakpoint_up location_breakpoint
11185 = set_momentary_breakpoint (frame_gdbarch, sal,
11186 stop_frame_id, bp_until);
11187 breakpoints.emplace_back (std::move (location_breakpoint));
11188 }
11189
11190 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
11191 std::move (breakpoints));
11192
11193 if (lj_deleter)
11194 lj_deleter->release ();
11195
11196 proceed (-1, GDB_SIGNAL_DEFAULT);
11197 }
11198
11199 /* This function attempts to parse an optional "if <cond>" clause
11200 from the arg string. If one is not found, it returns NULL.
11201
11202 Else, it returns a pointer to the condition string. (It does not
11203 attempt to evaluate the string against a particular block.) And,
11204 it updates arg to point to the first character following the parsed
11205 if clause in the arg string. */
11206
11207 const char *
11208 ep_parse_optional_if_clause (const char **arg)
11209 {
11210 const char *cond_string;
11211
11212 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11213 return NULL;
11214
11215 /* Skip the "if" keyword. */
11216 (*arg) += 2;
11217
11218 /* Skip any extra leading whitespace, and record the start of the
11219 condition string. */
11220 *arg = skip_spaces (*arg);
11221 cond_string = *arg;
11222
11223 /* Assume that the condition occupies the remainder of the arg
11224 string. */
11225 (*arg) += strlen (cond_string);
11226
11227 return cond_string;
11228 }
11229
11230 /* Commands to deal with catching events, such as signals, exceptions,
11231 process start/exit, etc. */
11232
11233 typedef enum
11234 {
11235 catch_fork_temporary, catch_vfork_temporary,
11236 catch_fork_permanent, catch_vfork_permanent
11237 }
11238 catch_fork_kind;
11239
11240 static void
11241 catch_fork_command_1 (const char *arg, int from_tty,
11242 struct cmd_list_element *command)
11243 {
11244 struct gdbarch *gdbarch = get_current_arch ();
11245 const char *cond_string = NULL;
11246 catch_fork_kind fork_kind;
11247
11248 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11249 bool temp = (fork_kind == catch_fork_temporary
11250 || fork_kind == catch_vfork_temporary);
11251
11252 if (!arg)
11253 arg = "";
11254 arg = skip_spaces (arg);
11255
11256 /* The allowed syntax is:
11257 catch [v]fork
11258 catch [v]fork if <cond>
11259
11260 First, check if there's an if clause. */
11261 cond_string = ep_parse_optional_if_clause (&arg);
11262
11263 if ((*arg != '\0') && !isspace (*arg))
11264 error (_("Junk at end of arguments."));
11265
11266 /* If this target supports it, create a fork or vfork catchpoint
11267 and enable reporting of such events. */
11268 switch (fork_kind)
11269 {
11270 case catch_fork_temporary:
11271 case catch_fork_permanent:
11272 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
11273 &catch_fork_breakpoint_ops);
11274 break;
11275 case catch_vfork_temporary:
11276 case catch_vfork_permanent:
11277 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
11278 &catch_vfork_breakpoint_ops);
11279 break;
11280 default:
11281 error (_("unsupported or unknown fork kind; cannot catch it"));
11282 break;
11283 }
11284 }
11285
11286 static void
11287 catch_exec_command_1 (const char *arg, int from_tty,
11288 struct cmd_list_element *command)
11289 {
11290 struct gdbarch *gdbarch = get_current_arch ();
11291 const char *cond_string = NULL;
11292 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
11293
11294 if (!arg)
11295 arg = "";
11296 arg = skip_spaces (arg);
11297
11298 /* The allowed syntax is:
11299 catch exec
11300 catch exec if <cond>
11301
11302 First, check if there's an if clause. */
11303 cond_string = ep_parse_optional_if_clause (&arg);
11304
11305 if ((*arg != '\0') && !isspace (*arg))
11306 error (_("Junk at end of arguments."));
11307
11308 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11309 init_catchpoint (c.get (), gdbarch, temp, cond_string,
11310 &catch_exec_breakpoint_ops);
11311 c->exec_pathname = NULL;
11312
11313 install_breakpoint (0, std::move (c), 1);
11314 }
11315
11316 void
11317 init_ada_exception_breakpoint (struct breakpoint *b,
11318 struct gdbarch *gdbarch,
11319 struct symtab_and_line sal,
11320 const char *addr_string,
11321 const struct breakpoint_ops *ops,
11322 int tempflag,
11323 int enabled,
11324 int from_tty)
11325 {
11326 if (from_tty)
11327 {
11328 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11329 if (!loc_gdbarch)
11330 loc_gdbarch = gdbarch;
11331
11332 describe_other_breakpoints (loc_gdbarch,
11333 sal.pspace, sal.pc, sal.section, -1);
11334 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11335 version for exception catchpoints, because two catchpoints
11336 used for different exception names will use the same address.
11337 In this case, a "breakpoint ... also set at..." warning is
11338 unproductive. Besides, the warning phrasing is also a bit
11339 inappropriate, we should use the word catchpoint, and tell
11340 the user what type of catchpoint it is. The above is good
11341 enough for now, though. */
11342 }
11343
11344 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
11345
11346 b->enable_state = enabled ? bp_enabled : bp_disabled;
11347 b->disposition = tempflag ? disp_del : disp_donttouch;
11348 b->location = string_to_event_location (&addr_string,
11349 language_def (language_ada));
11350 b->language = language_ada;
11351 }
11352
11353 \f
11354
11355 /* Compare two breakpoints and return a strcmp-like result. */
11356
11357 static int
11358 compare_breakpoints (const breakpoint *a, const breakpoint *b)
11359 {
11360 uintptr_t ua = (uintptr_t) a;
11361 uintptr_t ub = (uintptr_t) b;
11362
11363 if (a->number < b->number)
11364 return -1;
11365 else if (a->number > b->number)
11366 return 1;
11367
11368 /* Now sort by address, in case we see, e..g, two breakpoints with
11369 the number 0. */
11370 if (ua < ub)
11371 return -1;
11372 return ua > ub ? 1 : 0;
11373 }
11374
11375 /* Delete breakpoints by address or line. */
11376
11377 static void
11378 clear_command (const char *arg, int from_tty)
11379 {
11380 int default_match;
11381
11382 std::vector<symtab_and_line> decoded_sals;
11383 symtab_and_line last_sal;
11384 gdb::array_view<symtab_and_line> sals;
11385 if (arg)
11386 {
11387 decoded_sals
11388 = decode_line_with_current_source (arg,
11389 (DECODE_LINE_FUNFIRSTLINE
11390 | DECODE_LINE_LIST_MODE));
11391 default_match = 0;
11392 sals = decoded_sals;
11393 }
11394 else
11395 {
11396 /* Set sal's line, symtab, pc, and pspace to the values
11397 corresponding to the last call to print_frame_info. If the
11398 codepoint is not valid, this will set all the fields to 0. */
11399 last_sal = get_last_displayed_sal ();
11400 if (last_sal.symtab == 0)
11401 error (_("No source file specified."));
11402
11403 default_match = 1;
11404 sals = last_sal;
11405 }
11406
11407 /* We don't call resolve_sal_pc here. That's not as bad as it
11408 seems, because all existing breakpoints typically have both
11409 file/line and pc set. So, if clear is given file/line, we can
11410 match this to existing breakpoint without obtaining pc at all.
11411
11412 We only support clearing given the address explicitly
11413 present in breakpoint table. Say, we've set breakpoint
11414 at file:line. There were several PC values for that file:line,
11415 due to optimization, all in one block.
11416
11417 We've picked one PC value. If "clear" is issued with another
11418 PC corresponding to the same file:line, the breakpoint won't
11419 be cleared. We probably can still clear the breakpoint, but
11420 since the other PC value is never presented to user, user
11421 can only find it by guessing, and it does not seem important
11422 to support that. */
11423
11424 /* For each line spec given, delete bps which correspond to it. Do
11425 it in two passes, solely to preserve the current behavior that
11426 from_tty is forced true if we delete more than one
11427 breakpoint. */
11428
11429 std::vector<struct breakpoint *> found;
11430 for (const auto &sal : sals)
11431 {
11432 const char *sal_fullname;
11433
11434 /* If exact pc given, clear bpts at that pc.
11435 If line given (pc == 0), clear all bpts on specified line.
11436 If defaulting, clear all bpts on default line
11437 or at default pc.
11438
11439 defaulting sal.pc != 0 tests to do
11440
11441 0 1 pc
11442 1 1 pc _and_ line
11443 0 0 line
11444 1 0 <can't happen> */
11445
11446 sal_fullname = (sal.symtab == NULL
11447 ? NULL : symtab_to_fullname (sal.symtab));
11448
11449 /* Find all matching breakpoints and add them to 'found'. */
11450 for (breakpoint *b : all_breakpoints ())
11451 {
11452 int match = 0;
11453 /* Are we going to delete b? */
11454 if (b->type != bp_none && !is_watchpoint (b))
11455 {
11456 for (bp_location *loc : b->locations ())
11457 {
11458 /* If the user specified file:line, don't allow a PC
11459 match. This matches historical gdb behavior. */
11460 int pc_match = (!sal.explicit_line
11461 && sal.pc
11462 && (loc->pspace == sal.pspace)
11463 && (loc->address == sal.pc)
11464 && (!section_is_overlay (loc->section)
11465 || loc->section == sal.section));
11466 int line_match = 0;
11467
11468 if ((default_match || sal.explicit_line)
11469 && loc->symtab != NULL
11470 && sal_fullname != NULL
11471 && sal.pspace == loc->pspace
11472 && loc->line_number == sal.line
11473 && filename_cmp (symtab_to_fullname (loc->symtab),
11474 sal_fullname) == 0)
11475 line_match = 1;
11476
11477 if (pc_match || line_match)
11478 {
11479 match = 1;
11480 break;
11481 }
11482 }
11483 }
11484
11485 if (match)
11486 found.push_back (b);
11487 }
11488 }
11489
11490 /* Now go thru the 'found' chain and delete them. */
11491 if (found.empty ())
11492 {
11493 if (arg)
11494 error (_("No breakpoint at %s."), arg);
11495 else
11496 error (_("No breakpoint at this line."));
11497 }
11498
11499 /* Remove duplicates from the vec. */
11500 std::sort (found.begin (), found.end (),
11501 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11502 {
11503 return compare_breakpoints (bp_a, bp_b) < 0;
11504 });
11505 found.erase (std::unique (found.begin (), found.end (),
11506 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11507 {
11508 return compare_breakpoints (bp_a, bp_b) == 0;
11509 }),
11510 found.end ());
11511
11512 if (found.size () > 1)
11513 from_tty = 1; /* Always report if deleted more than one. */
11514 if (from_tty)
11515 {
11516 if (found.size () == 1)
11517 printf_unfiltered (_("Deleted breakpoint "));
11518 else
11519 printf_unfiltered (_("Deleted breakpoints "));
11520 }
11521
11522 for (breakpoint *iter : found)
11523 {
11524 if (from_tty)
11525 printf_unfiltered ("%d ", iter->number);
11526 delete_breakpoint (iter);
11527 }
11528 if (from_tty)
11529 putchar_unfiltered ('\n');
11530 }
11531 \f
11532 /* Delete breakpoint in BS if they are `delete' breakpoints and
11533 all breakpoints that are marked for deletion, whether hit or not.
11534 This is called after any breakpoint is hit, or after errors. */
11535
11536 void
11537 breakpoint_auto_delete (bpstat bs)
11538 {
11539 for (; bs; bs = bs->next)
11540 if (bs->breakpoint_at
11541 && bs->breakpoint_at->disposition == disp_del
11542 && bs->stop)
11543 delete_breakpoint (bs->breakpoint_at);
11544
11545 for (breakpoint *b : all_breakpoints_safe ())
11546 if (b->disposition == disp_del_at_next_stop)
11547 delete_breakpoint (b);
11548 }
11549
11550 /* A comparison function for bp_location AP and BP being interfaced to
11551 std::sort. Sort elements primarily by their ADDRESS (no matter what
11552 bl_address_is_meaningful says), secondarily by ordering first
11553 permanent elements and terciarily just ensuring the array is sorted
11554 stable way despite std::sort being an unstable algorithm. */
11555
11556 static int
11557 bp_location_is_less_than (const bp_location *a, const bp_location *b)
11558 {
11559 if (a->address != b->address)
11560 return a->address < b->address;
11561
11562 /* Sort locations at the same address by their pspace number, keeping
11563 locations of the same inferior (in a multi-inferior environment)
11564 grouped. */
11565
11566 if (a->pspace->num != b->pspace->num)
11567 return a->pspace->num < b->pspace->num;
11568
11569 /* Sort permanent breakpoints first. */
11570 if (a->permanent != b->permanent)
11571 return a->permanent > b->permanent;
11572
11573 /* Sort by type in order to make duplicate determination easier.
11574 See update_global_location_list. This is kept in sync with
11575 breakpoint_locations_match. */
11576 if (a->loc_type < b->loc_type)
11577 return true;
11578
11579 /* Likewise, for range-breakpoints, sort by length. */
11580 if (a->loc_type == bp_loc_hardware_breakpoint
11581 && b->loc_type == bp_loc_hardware_breakpoint
11582 && a->length < b->length)
11583 return true;
11584
11585 /* Make the internal GDB representation stable across GDB runs
11586 where A and B memory inside GDB can differ. Breakpoint locations of
11587 the same type at the same address can be sorted in arbitrary order. */
11588
11589 if (a->owner->number != b->owner->number)
11590 return a->owner->number < b->owner->number;
11591
11592 return a < b;
11593 }
11594
11595 /* Set bp_locations_placed_address_before_address_max and
11596 bp_locations_shadow_len_after_address_max according to the current
11597 content of the bp_locations array. */
11598
11599 static void
11600 bp_locations_target_extensions_update (void)
11601 {
11602 bp_locations_placed_address_before_address_max = 0;
11603 bp_locations_shadow_len_after_address_max = 0;
11604
11605 for (bp_location *bl : all_bp_locations ())
11606 {
11607 CORE_ADDR start, end, addr;
11608
11609 if (!bp_location_has_shadow (bl))
11610 continue;
11611
11612 start = bl->target_info.placed_address;
11613 end = start + bl->target_info.shadow_len;
11614
11615 gdb_assert (bl->address >= start);
11616 addr = bl->address - start;
11617 if (addr > bp_locations_placed_address_before_address_max)
11618 bp_locations_placed_address_before_address_max = addr;
11619
11620 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11621
11622 gdb_assert (bl->address < end);
11623 addr = end - bl->address;
11624 if (addr > bp_locations_shadow_len_after_address_max)
11625 bp_locations_shadow_len_after_address_max = addr;
11626 }
11627 }
11628
11629 /* Download tracepoint locations if they haven't been. */
11630
11631 static void
11632 download_tracepoint_locations (void)
11633 {
11634 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11635
11636 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11637
11638 for (breakpoint *b : all_tracepoints ())
11639 {
11640 struct tracepoint *t;
11641 int bp_location_downloaded = 0;
11642
11643 if ((b->type == bp_fast_tracepoint
11644 ? !may_insert_fast_tracepoints
11645 : !may_insert_tracepoints))
11646 continue;
11647
11648 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11649 {
11650 if (target_can_download_tracepoint ())
11651 can_download_tracepoint = TRIBOOL_TRUE;
11652 else
11653 can_download_tracepoint = TRIBOOL_FALSE;
11654 }
11655
11656 if (can_download_tracepoint == TRIBOOL_FALSE)
11657 break;
11658
11659 for (bp_location *bl : b->locations ())
11660 {
11661 /* In tracepoint, locations are _never_ duplicated, so
11662 should_be_inserted is equivalent to
11663 unduplicated_should_be_inserted. */
11664 if (!should_be_inserted (bl) || bl->inserted)
11665 continue;
11666
11667 switch_to_program_space_and_thread (bl->pspace);
11668
11669 target_download_tracepoint (bl);
11670
11671 bl->inserted = 1;
11672 bp_location_downloaded = 1;
11673 }
11674 t = (struct tracepoint *) b;
11675 t->number_on_target = b->number;
11676 if (bp_location_downloaded)
11677 gdb::observers::breakpoint_modified.notify (b);
11678 }
11679 }
11680
11681 /* Swap the insertion/duplication state between two locations. */
11682
11683 static void
11684 swap_insertion (struct bp_location *left, struct bp_location *right)
11685 {
11686 const int left_inserted = left->inserted;
11687 const int left_duplicate = left->duplicate;
11688 const int left_needs_update = left->needs_update;
11689 const struct bp_target_info left_target_info = left->target_info;
11690
11691 /* Locations of tracepoints can never be duplicated. */
11692 if (is_tracepoint (left->owner))
11693 gdb_assert (!left->duplicate);
11694 if (is_tracepoint (right->owner))
11695 gdb_assert (!right->duplicate);
11696
11697 left->inserted = right->inserted;
11698 left->duplicate = right->duplicate;
11699 left->needs_update = right->needs_update;
11700 left->target_info = right->target_info;
11701 right->inserted = left_inserted;
11702 right->duplicate = left_duplicate;
11703 right->needs_update = left_needs_update;
11704 right->target_info = left_target_info;
11705 }
11706
11707 /* Force the re-insertion of the locations at ADDRESS. This is called
11708 once a new/deleted/modified duplicate location is found and we are evaluating
11709 conditions on the target's side. Such conditions need to be updated on
11710 the target. */
11711
11712 static void
11713 force_breakpoint_reinsertion (struct bp_location *bl)
11714 {
11715 CORE_ADDR address = 0;
11716 int pspace_num;
11717
11718 address = bl->address;
11719 pspace_num = bl->pspace->num;
11720
11721 /* This is only meaningful if the target is
11722 evaluating conditions and if the user has
11723 opted for condition evaluation on the target's
11724 side. */
11725 if (gdb_evaluates_breakpoint_condition_p ()
11726 || !target_supports_evaluation_of_breakpoint_conditions ())
11727 return;
11728
11729 /* Flag all breakpoint locations with this address and
11730 the same program space as the location
11731 as "its condition has changed". We need to
11732 update the conditions on the target's side. */
11733 for (bp_location *loc : all_bp_locations_at_addr (address))
11734 {
11735 if (!is_breakpoint (loc->owner)
11736 || pspace_num != loc->pspace->num)
11737 continue;
11738
11739 /* Flag the location appropriately. We use a different state to
11740 let everyone know that we already updated the set of locations
11741 with addr bl->address and program space bl->pspace. This is so
11742 we don't have to keep calling these functions just to mark locations
11743 that have already been marked. */
11744 loc->condition_changed = condition_updated;
11745
11746 /* Free the agent expression bytecode as well. We will compute
11747 it later on. */
11748 loc->cond_bytecode.reset ();
11749 }
11750 }
11751
11752 /* Called whether new breakpoints are created, or existing breakpoints
11753 deleted, to update the global location list and recompute which
11754 locations are duplicate of which.
11755
11756 The INSERT_MODE flag determines whether locations may not, may, or
11757 shall be inserted now. See 'enum ugll_insert_mode' for more
11758 info. */
11759
11760 static void
11761 update_global_location_list (enum ugll_insert_mode insert_mode)
11762 {
11763 /* Last breakpoint location address that was marked for update. */
11764 CORE_ADDR last_addr = 0;
11765 /* Last breakpoint location program space that was marked for update. */
11766 int last_pspace_num = -1;
11767
11768 /* Used in the duplicates detection below. When iterating over all
11769 bp_locations, points to the first bp_location of a given address.
11770 Breakpoints and watchpoints of different types are never
11771 duplicates of each other. Keep one pointer for each type of
11772 breakpoint/watchpoint, so we only need to loop over all locations
11773 once. */
11774 struct bp_location *bp_loc_first; /* breakpoint */
11775 struct bp_location *wp_loc_first; /* hardware watchpoint */
11776 struct bp_location *awp_loc_first; /* access watchpoint */
11777 struct bp_location *rwp_loc_first; /* read watchpoint */
11778
11779 /* Saved former bp_locations array which we compare against the newly
11780 built bp_locations from the current state of ALL_BREAKPOINTS. */
11781 std::vector<bp_location *> old_locations = std::move (bp_locations);
11782 bp_locations.clear ();
11783
11784 for (breakpoint *b : all_breakpoints ())
11785 for (bp_location *loc : b->locations ())
11786 bp_locations.push_back (loc);
11787
11788 /* See if we need to "upgrade" a software breakpoint to a hardware
11789 breakpoint. Do this before deciding whether locations are
11790 duplicates. Also do this before sorting because sorting order
11791 depends on location type. */
11792 for (bp_location *loc : bp_locations)
11793 if (!loc->inserted && should_be_inserted (loc))
11794 handle_automatic_hardware_breakpoints (loc);
11795
11796 std::sort (bp_locations.begin (), bp_locations.end (),
11797 bp_location_is_less_than);
11798
11799 bp_locations_target_extensions_update ();
11800
11801 /* Identify bp_location instances that are no longer present in the
11802 new list, and therefore should be freed. Note that it's not
11803 necessary that those locations should be removed from inferior --
11804 if there's another location at the same address (previously
11805 marked as duplicate), we don't need to remove/insert the
11806 location.
11807
11808 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11809 and former bp_location array state respectively. */
11810
11811 size_t loc_i = 0;
11812 for (bp_location *old_loc : old_locations)
11813 {
11814 /* Tells if 'old_loc' is found among the new locations. If
11815 not, we have to free it. */
11816 int found_object = 0;
11817 /* Tells if the location should remain inserted in the target. */
11818 int keep_in_target = 0;
11819 int removed = 0;
11820
11821 /* Skip LOCP entries which will definitely never be needed.
11822 Stop either at or being the one matching OLD_LOC. */
11823 while (loc_i < bp_locations.size ()
11824 && bp_locations[loc_i]->address < old_loc->address)
11825 loc_i++;
11826
11827 for (size_t loc2_i = loc_i;
11828 (loc2_i < bp_locations.size ()
11829 && bp_locations[loc2_i]->address == old_loc->address);
11830 loc2_i++)
11831 {
11832 /* Check if this is a new/duplicated location or a duplicated
11833 location that had its condition modified. If so, we want to send
11834 its condition to the target if evaluation of conditions is taking
11835 place there. */
11836 if (bp_locations[loc2_i]->condition_changed == condition_modified
11837 && (last_addr != old_loc->address
11838 || last_pspace_num != old_loc->pspace->num))
11839 {
11840 force_breakpoint_reinsertion (bp_locations[loc2_i]);
11841 last_pspace_num = old_loc->pspace->num;
11842 }
11843
11844 if (bp_locations[loc2_i] == old_loc)
11845 found_object = 1;
11846 }
11847
11848 /* We have already handled this address, update it so that we don't
11849 have to go through updates again. */
11850 last_addr = old_loc->address;
11851
11852 /* Target-side condition evaluation: Handle deleted locations. */
11853 if (!found_object)
11854 force_breakpoint_reinsertion (old_loc);
11855
11856 /* If this location is no longer present, and inserted, look if
11857 there's maybe a new location at the same address. If so,
11858 mark that one inserted, and don't remove this one. This is
11859 needed so that we don't have a time window where a breakpoint
11860 at certain location is not inserted. */
11861
11862 if (old_loc->inserted)
11863 {
11864 /* If the location is inserted now, we might have to remove
11865 it. */
11866
11867 if (found_object && should_be_inserted (old_loc))
11868 {
11869 /* The location is still present in the location list,
11870 and still should be inserted. Don't do anything. */
11871 keep_in_target = 1;
11872 }
11873 else
11874 {
11875 /* This location still exists, but it won't be kept in the
11876 target since it may have been disabled. We proceed to
11877 remove its target-side condition. */
11878
11879 /* The location is either no longer present, or got
11880 disabled. See if there's another location at the
11881 same address, in which case we don't need to remove
11882 this one from the target. */
11883
11884 /* OLD_LOC comes from existing struct breakpoint. */
11885 if (bl_address_is_meaningful (old_loc))
11886 {
11887 for (size_t loc2_i = loc_i;
11888 (loc2_i < bp_locations.size ()
11889 && bp_locations[loc2_i]->address == old_loc->address);
11890 loc2_i++)
11891 {
11892 bp_location *loc2 = bp_locations[loc2_i];
11893
11894 if (loc2 == old_loc)
11895 continue;
11896
11897 if (breakpoint_locations_match (loc2, old_loc))
11898 {
11899 /* Read watchpoint locations are switched to
11900 access watchpoints, if the former are not
11901 supported, but the latter are. */
11902 if (is_hardware_watchpoint (old_loc->owner))
11903 {
11904 gdb_assert (is_hardware_watchpoint (loc2->owner));
11905 loc2->watchpoint_type = old_loc->watchpoint_type;
11906 }
11907
11908 /* loc2 is a duplicated location. We need to check
11909 if it should be inserted in case it will be
11910 unduplicated. */
11911 if (unduplicated_should_be_inserted (loc2))
11912 {
11913 swap_insertion (old_loc, loc2);
11914 keep_in_target = 1;
11915 break;
11916 }
11917 }
11918 }
11919 }
11920 }
11921
11922 if (!keep_in_target)
11923 {
11924 if (remove_breakpoint (old_loc))
11925 {
11926 /* This is just about all we can do. We could keep
11927 this location on the global list, and try to
11928 remove it next time, but there's no particular
11929 reason why we will succeed next time.
11930
11931 Note that at this point, old_loc->owner is still
11932 valid, as delete_breakpoint frees the breakpoint
11933 only after calling us. */
11934 printf_filtered (_("warning: Error removing "
11935 "breakpoint %d\n"),
11936 old_loc->owner->number);
11937 }
11938 removed = 1;
11939 }
11940 }
11941
11942 if (!found_object)
11943 {
11944 if (removed && target_is_non_stop_p ()
11945 && need_moribund_for_location_type (old_loc))
11946 {
11947 /* This location was removed from the target. In
11948 non-stop mode, a race condition is possible where
11949 we've removed a breakpoint, but stop events for that
11950 breakpoint are already queued and will arrive later.
11951 We apply an heuristic to be able to distinguish such
11952 SIGTRAPs from other random SIGTRAPs: we keep this
11953 breakpoint location for a bit, and will retire it
11954 after we see some number of events. The theory here
11955 is that reporting of events should, "on the average",
11956 be fair, so after a while we'll see events from all
11957 threads that have anything of interest, and no longer
11958 need to keep this breakpoint location around. We
11959 don't hold locations forever so to reduce chances of
11960 mistaking a non-breakpoint SIGTRAP for a breakpoint
11961 SIGTRAP.
11962
11963 The heuristic failing can be disastrous on
11964 decr_pc_after_break targets.
11965
11966 On decr_pc_after_break targets, like e.g., x86-linux,
11967 if we fail to recognize a late breakpoint SIGTRAP,
11968 because events_till_retirement has reached 0 too
11969 soon, we'll fail to do the PC adjustment, and report
11970 a random SIGTRAP to the user. When the user resumes
11971 the inferior, it will most likely immediately crash
11972 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
11973 corrupted, because of being resumed e.g., in the
11974 middle of a multi-byte instruction, or skipped a
11975 one-byte instruction. This was actually seen happen
11976 on native x86-linux, and should be less rare on
11977 targets that do not support new thread events, like
11978 remote, due to the heuristic depending on
11979 thread_count.
11980
11981 Mistaking a random SIGTRAP for a breakpoint trap
11982 causes similar symptoms (PC adjustment applied when
11983 it shouldn't), but then again, playing with SIGTRAPs
11984 behind the debugger's back is asking for trouble.
11985
11986 Since hardware watchpoint traps are always
11987 distinguishable from other traps, so we don't need to
11988 apply keep hardware watchpoint moribund locations
11989 around. We simply always ignore hardware watchpoint
11990 traps we can no longer explain. */
11991
11992 process_stratum_target *proc_target = nullptr;
11993 for (inferior *inf : all_inferiors ())
11994 if (inf->pspace == old_loc->pspace)
11995 {
11996 proc_target = inf->process_target ();
11997 break;
11998 }
11999 if (proc_target != nullptr)
12000 old_loc->events_till_retirement
12001 = 3 * (thread_count (proc_target) + 1);
12002 else
12003 old_loc->events_till_retirement = 1;
12004 old_loc->owner = NULL;
12005
12006 moribund_locations.push_back (old_loc);
12007 }
12008 else
12009 {
12010 old_loc->owner = NULL;
12011 decref_bp_location (&old_loc);
12012 }
12013 }
12014 }
12015
12016 /* Rescan breakpoints at the same address and section, marking the
12017 first one as "first" and any others as "duplicates". This is so
12018 that the bpt instruction is only inserted once. If we have a
12019 permanent breakpoint at the same place as BPT, make that one the
12020 official one, and the rest as duplicates. Permanent breakpoints
12021 are sorted first for the same address.
12022
12023 Do the same for hardware watchpoints, but also considering the
12024 watchpoint's type (regular/access/read) and length. */
12025
12026 bp_loc_first = NULL;
12027 wp_loc_first = NULL;
12028 awp_loc_first = NULL;
12029 rwp_loc_first = NULL;
12030
12031 for (bp_location *loc : all_bp_locations ())
12032 {
12033 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12034 non-NULL. */
12035 struct bp_location **loc_first_p;
12036 breakpoint *b = loc->owner;
12037
12038 if (!unduplicated_should_be_inserted (loc)
12039 || !bl_address_is_meaningful (loc)
12040 /* Don't detect duplicate for tracepoint locations because they are
12041 never duplicated. See the comments in field `duplicate' of
12042 `struct bp_location'. */
12043 || is_tracepoint (b))
12044 {
12045 /* Clear the condition modification flag. */
12046 loc->condition_changed = condition_unchanged;
12047 continue;
12048 }
12049
12050 if (b->type == bp_hardware_watchpoint)
12051 loc_first_p = &wp_loc_first;
12052 else if (b->type == bp_read_watchpoint)
12053 loc_first_p = &rwp_loc_first;
12054 else if (b->type == bp_access_watchpoint)
12055 loc_first_p = &awp_loc_first;
12056 else
12057 loc_first_p = &bp_loc_first;
12058
12059 if (*loc_first_p == NULL
12060 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12061 || !breakpoint_locations_match (loc, *loc_first_p))
12062 {
12063 *loc_first_p = loc;
12064 loc->duplicate = 0;
12065
12066 if (is_breakpoint (loc->owner) && loc->condition_changed)
12067 {
12068 loc->needs_update = 1;
12069 /* Clear the condition modification flag. */
12070 loc->condition_changed = condition_unchanged;
12071 }
12072 continue;
12073 }
12074
12075
12076 /* This and the above ensure the invariant that the first location
12077 is not duplicated, and is the inserted one.
12078 All following are marked as duplicated, and are not inserted. */
12079 if (loc->inserted)
12080 swap_insertion (loc, *loc_first_p);
12081 loc->duplicate = 1;
12082
12083 /* Clear the condition modification flag. */
12084 loc->condition_changed = condition_unchanged;
12085 }
12086
12087 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12088 {
12089 if (insert_mode != UGLL_DONT_INSERT)
12090 insert_breakpoint_locations ();
12091 else
12092 {
12093 /* Even though the caller told us to not insert new
12094 locations, we may still need to update conditions on the
12095 target's side of breakpoints that were already inserted
12096 if the target is evaluating breakpoint conditions. We
12097 only update conditions for locations that are marked
12098 "needs_update". */
12099 update_inserted_breakpoint_locations ();
12100 }
12101 }
12102
12103 if (insert_mode != UGLL_DONT_INSERT)
12104 download_tracepoint_locations ();
12105 }
12106
12107 void
12108 breakpoint_retire_moribund (void)
12109 {
12110 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12111 {
12112 struct bp_location *loc = moribund_locations[ix];
12113 if (--(loc->events_till_retirement) == 0)
12114 {
12115 decref_bp_location (&loc);
12116 unordered_remove (moribund_locations, ix);
12117 --ix;
12118 }
12119 }
12120 }
12121
12122 static void
12123 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12124 {
12125
12126 try
12127 {
12128 update_global_location_list (insert_mode);
12129 }
12130 catch (const gdb_exception_error &e)
12131 {
12132 }
12133 }
12134
12135 /* Clear BKP from a BPS. */
12136
12137 static void
12138 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12139 {
12140 bpstat bs;
12141
12142 for (bs = bps; bs; bs = bs->next)
12143 if (bs->breakpoint_at == bpt)
12144 {
12145 bs->breakpoint_at = NULL;
12146 bs->old_val = NULL;
12147 /* bs->commands will be freed later. */
12148 }
12149 }
12150
12151 /* Callback for iterate_over_threads. */
12152 static int
12153 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12154 {
12155 struct breakpoint *bpt = (struct breakpoint *) data;
12156
12157 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12158 return 0;
12159 }
12160
12161 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12162 callbacks. */
12163
12164 static void
12165 say_where (struct breakpoint *b)
12166 {
12167 struct value_print_options opts;
12168
12169 get_user_print_options (&opts);
12170
12171 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12172 single string. */
12173 if (b->loc == NULL)
12174 {
12175 /* For pending locations, the output differs slightly based
12176 on b->extra_string. If this is non-NULL, it contains either
12177 a condition or dprintf arguments. */
12178 if (b->extra_string == NULL)
12179 {
12180 printf_filtered (_(" (%s) pending."),
12181 event_location_to_string (b->location.get ()));
12182 }
12183 else if (b->type == bp_dprintf)
12184 {
12185 printf_filtered (_(" (%s,%s) pending."),
12186 event_location_to_string (b->location.get ()),
12187 b->extra_string);
12188 }
12189 else
12190 {
12191 printf_filtered (_(" (%s %s) pending."),
12192 event_location_to_string (b->location.get ()),
12193 b->extra_string);
12194 }
12195 }
12196 else
12197 {
12198 if (opts.addressprint || b->loc->symtab == NULL)
12199 printf_filtered (" at %ps",
12200 styled_string (address_style.style (),
12201 paddress (b->loc->gdbarch,
12202 b->loc->address)));
12203 if (b->loc->symtab != NULL)
12204 {
12205 /* If there is a single location, we can print the location
12206 more nicely. */
12207 if (b->loc->next == NULL)
12208 {
12209 const char *filename
12210 = symtab_to_filename_for_display (b->loc->symtab);
12211 printf_filtered (": file %ps, line %d.",
12212 styled_string (file_name_style.style (),
12213 filename),
12214 b->loc->line_number);
12215 }
12216 else
12217 /* This is not ideal, but each location may have a
12218 different file name, and this at least reflects the
12219 real situation somewhat. */
12220 printf_filtered (": %s.",
12221 event_location_to_string (b->location.get ()));
12222 }
12223
12224 if (b->loc->next)
12225 {
12226 struct bp_location *loc = b->loc;
12227 int n = 0;
12228 for (; loc; loc = loc->next)
12229 ++n;
12230 printf_filtered (" (%d locations)", n);
12231 }
12232 }
12233 }
12234
12235 bp_location::~bp_location ()
12236 {
12237 xfree (function_name);
12238 }
12239
12240 /* Destructor for the breakpoint base class. */
12241
12242 breakpoint::~breakpoint ()
12243 {
12244 xfree (this->cond_string);
12245 xfree (this->extra_string);
12246 }
12247
12248 /* See breakpoint.h. */
12249
12250 bp_locations_range breakpoint::locations ()
12251 {
12252 return bp_locations_range (this->loc);
12253 }
12254
12255 static struct bp_location *
12256 base_breakpoint_allocate_location (struct breakpoint *self)
12257 {
12258 return new bp_location (self);
12259 }
12260
12261 static void
12262 base_breakpoint_re_set (struct breakpoint *b)
12263 {
12264 /* Nothing to re-set. */
12265 }
12266
12267 #define internal_error_pure_virtual_called() \
12268 gdb_assert_not_reached ("pure virtual function called")
12269
12270 static int
12271 base_breakpoint_insert_location (struct bp_location *bl)
12272 {
12273 internal_error_pure_virtual_called ();
12274 }
12275
12276 static int
12277 base_breakpoint_remove_location (struct bp_location *bl,
12278 enum remove_bp_reason reason)
12279 {
12280 internal_error_pure_virtual_called ();
12281 }
12282
12283 static int
12284 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12285 const address_space *aspace,
12286 CORE_ADDR bp_addr,
12287 const struct target_waitstatus *ws)
12288 {
12289 internal_error_pure_virtual_called ();
12290 }
12291
12292 static void
12293 base_breakpoint_check_status (bpstat bs)
12294 {
12295 /* Always stop. */
12296 }
12297
12298 /* A "works_in_software_mode" breakpoint_ops method that just internal
12299 errors. */
12300
12301 static int
12302 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12303 {
12304 internal_error_pure_virtual_called ();
12305 }
12306
12307 /* A "resources_needed" breakpoint_ops method that just internal
12308 errors. */
12309
12310 static int
12311 base_breakpoint_resources_needed (const struct bp_location *bl)
12312 {
12313 internal_error_pure_virtual_called ();
12314 }
12315
12316 static enum print_stop_action
12317 base_breakpoint_print_it (bpstat bs)
12318 {
12319 internal_error_pure_virtual_called ();
12320 }
12321
12322 static void
12323 base_breakpoint_print_one_detail (const struct breakpoint *self,
12324 struct ui_out *uiout)
12325 {
12326 /* nothing */
12327 }
12328
12329 static void
12330 base_breakpoint_print_mention (struct breakpoint *b)
12331 {
12332 internal_error_pure_virtual_called ();
12333 }
12334
12335 static void
12336 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12337 {
12338 internal_error_pure_virtual_called ();
12339 }
12340
12341 static void
12342 base_breakpoint_create_sals_from_location
12343 (struct event_location *location,
12344 struct linespec_result *canonical,
12345 enum bptype type_wanted)
12346 {
12347 internal_error_pure_virtual_called ();
12348 }
12349
12350 static void
12351 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12352 struct linespec_result *c,
12353 gdb::unique_xmalloc_ptr<char> cond_string,
12354 gdb::unique_xmalloc_ptr<char> extra_string,
12355 enum bptype type_wanted,
12356 enum bpdisp disposition,
12357 int thread,
12358 int task, int ignore_count,
12359 const struct breakpoint_ops *o,
12360 int from_tty, int enabled,
12361 int internal, unsigned flags)
12362 {
12363 internal_error_pure_virtual_called ();
12364 }
12365
12366 static std::vector<symtab_and_line>
12367 base_breakpoint_decode_location (struct breakpoint *b,
12368 struct event_location *location,
12369 struct program_space *search_pspace)
12370 {
12371 internal_error_pure_virtual_called ();
12372 }
12373
12374 /* The default 'explains_signal' method. */
12375
12376 static int
12377 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12378 {
12379 return 1;
12380 }
12381
12382 /* The default "after_condition_true" method. */
12383
12384 static void
12385 base_breakpoint_after_condition_true (struct bpstats *bs)
12386 {
12387 /* Nothing to do. */
12388 }
12389
12390 struct breakpoint_ops base_breakpoint_ops =
12391 {
12392 base_breakpoint_allocate_location,
12393 base_breakpoint_re_set,
12394 base_breakpoint_insert_location,
12395 base_breakpoint_remove_location,
12396 base_breakpoint_breakpoint_hit,
12397 base_breakpoint_check_status,
12398 base_breakpoint_resources_needed,
12399 base_breakpoint_works_in_software_mode,
12400 base_breakpoint_print_it,
12401 NULL,
12402 base_breakpoint_print_one_detail,
12403 base_breakpoint_print_mention,
12404 base_breakpoint_print_recreate,
12405 base_breakpoint_create_sals_from_location,
12406 base_breakpoint_create_breakpoints_sal,
12407 base_breakpoint_decode_location,
12408 base_breakpoint_explains_signal,
12409 base_breakpoint_after_condition_true,
12410 };
12411
12412 /* Default breakpoint_ops methods. */
12413
12414 static void
12415 bkpt_re_set (struct breakpoint *b)
12416 {
12417 /* FIXME: is this still reachable? */
12418 if (breakpoint_event_location_empty_p (b))
12419 {
12420 /* Anything without a location can't be re-set. */
12421 delete_breakpoint (b);
12422 return;
12423 }
12424
12425 breakpoint_re_set_default (b);
12426 }
12427
12428 static int
12429 bkpt_insert_location (struct bp_location *bl)
12430 {
12431 CORE_ADDR addr = bl->target_info.reqstd_address;
12432
12433 bl->target_info.kind = breakpoint_kind (bl, &addr);
12434 bl->target_info.placed_address = addr;
12435
12436 if (bl->loc_type == bp_loc_hardware_breakpoint)
12437 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12438 else
12439 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12440 }
12441
12442 static int
12443 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12444 {
12445 if (bl->loc_type == bp_loc_hardware_breakpoint)
12446 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12447 else
12448 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12449 }
12450
12451 static int
12452 bkpt_breakpoint_hit (const struct bp_location *bl,
12453 const address_space *aspace, CORE_ADDR bp_addr,
12454 const struct target_waitstatus *ws)
12455 {
12456 if (ws->kind != TARGET_WAITKIND_STOPPED
12457 || ws->value.sig != GDB_SIGNAL_TRAP)
12458 return 0;
12459
12460 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12461 aspace, bp_addr))
12462 return 0;
12463
12464 if (overlay_debugging /* unmapped overlay section */
12465 && section_is_overlay (bl->section)
12466 && !section_is_mapped (bl->section))
12467 return 0;
12468
12469 return 1;
12470 }
12471
12472 static int
12473 dprintf_breakpoint_hit (const struct bp_location *bl,
12474 const address_space *aspace, CORE_ADDR bp_addr,
12475 const struct target_waitstatus *ws)
12476 {
12477 if (dprintf_style == dprintf_style_agent
12478 && target_can_run_breakpoint_commands ())
12479 {
12480 /* An agent-style dprintf never causes a stop. If we see a trap
12481 for this address it must be for a breakpoint that happens to
12482 be set at the same address. */
12483 return 0;
12484 }
12485
12486 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12487 }
12488
12489 static int
12490 bkpt_resources_needed (const struct bp_location *bl)
12491 {
12492 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12493
12494 return 1;
12495 }
12496
12497 static enum print_stop_action
12498 bkpt_print_it (bpstat bs)
12499 {
12500 struct breakpoint *b;
12501 const struct bp_location *bl;
12502 int bp_temp;
12503 struct ui_out *uiout = current_uiout;
12504
12505 gdb_assert (bs->bp_location_at != NULL);
12506
12507 bl = bs->bp_location_at.get ();
12508 b = bs->breakpoint_at;
12509
12510 bp_temp = b->disposition == disp_del;
12511 if (bl->address != bl->requested_address)
12512 breakpoint_adjustment_warning (bl->requested_address,
12513 bl->address,
12514 b->number, 1);
12515 annotate_breakpoint (b->number);
12516 maybe_print_thread_hit_breakpoint (uiout);
12517
12518 if (uiout->is_mi_like_p ())
12519 {
12520 uiout->field_string ("reason",
12521 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12522 uiout->field_string ("disp", bpdisp_text (b->disposition));
12523 }
12524 if (bp_temp)
12525 uiout->message ("Temporary breakpoint %pF, ",
12526 signed_field ("bkptno", b->number));
12527 else
12528 uiout->message ("Breakpoint %pF, ",
12529 signed_field ("bkptno", b->number));
12530
12531 return PRINT_SRC_AND_LOC;
12532 }
12533
12534 static void
12535 bkpt_print_mention (struct breakpoint *b)
12536 {
12537 if (current_uiout->is_mi_like_p ())
12538 return;
12539
12540 switch (b->type)
12541 {
12542 case bp_breakpoint:
12543 case bp_gnu_ifunc_resolver:
12544 if (b->disposition == disp_del)
12545 printf_filtered (_("Temporary breakpoint"));
12546 else
12547 printf_filtered (_("Breakpoint"));
12548 printf_filtered (_(" %d"), b->number);
12549 if (b->type == bp_gnu_ifunc_resolver)
12550 printf_filtered (_(" at gnu-indirect-function resolver"));
12551 break;
12552 case bp_hardware_breakpoint:
12553 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12554 break;
12555 case bp_dprintf:
12556 printf_filtered (_("Dprintf %d"), b->number);
12557 break;
12558 }
12559
12560 say_where (b);
12561 }
12562
12563 static void
12564 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12565 {
12566 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12567 fprintf_unfiltered (fp, "tbreak");
12568 else if (tp->type == bp_breakpoint)
12569 fprintf_unfiltered (fp, "break");
12570 else if (tp->type == bp_hardware_breakpoint
12571 && tp->disposition == disp_del)
12572 fprintf_unfiltered (fp, "thbreak");
12573 else if (tp->type == bp_hardware_breakpoint)
12574 fprintf_unfiltered (fp, "hbreak");
12575 else
12576 internal_error (__FILE__, __LINE__,
12577 _("unhandled breakpoint type %d"), (int) tp->type);
12578
12579 fprintf_unfiltered (fp, " %s",
12580 event_location_to_string (tp->location.get ()));
12581
12582 /* Print out extra_string if this breakpoint is pending. It might
12583 contain, for example, conditions that were set by the user. */
12584 if (tp->loc == NULL && tp->extra_string != NULL)
12585 fprintf_unfiltered (fp, " %s", tp->extra_string);
12586
12587 print_recreate_thread (tp, fp);
12588 }
12589
12590 static void
12591 bkpt_create_sals_from_location (struct event_location *location,
12592 struct linespec_result *canonical,
12593 enum bptype type_wanted)
12594 {
12595 create_sals_from_location_default (location, canonical, type_wanted);
12596 }
12597
12598 static void
12599 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12600 struct linespec_result *canonical,
12601 gdb::unique_xmalloc_ptr<char> cond_string,
12602 gdb::unique_xmalloc_ptr<char> extra_string,
12603 enum bptype type_wanted,
12604 enum bpdisp disposition,
12605 int thread,
12606 int task, int ignore_count,
12607 const struct breakpoint_ops *ops,
12608 int from_tty, int enabled,
12609 int internal, unsigned flags)
12610 {
12611 create_breakpoints_sal_default (gdbarch, canonical,
12612 std::move (cond_string),
12613 std::move (extra_string),
12614 type_wanted,
12615 disposition, thread, task,
12616 ignore_count, ops, from_tty,
12617 enabled, internal, flags);
12618 }
12619
12620 static std::vector<symtab_and_line>
12621 bkpt_decode_location (struct breakpoint *b,
12622 struct event_location *location,
12623 struct program_space *search_pspace)
12624 {
12625 return decode_location_default (b, location, search_pspace);
12626 }
12627
12628 /* Virtual table for internal breakpoints. */
12629
12630 static void
12631 internal_bkpt_re_set (struct breakpoint *b)
12632 {
12633 switch (b->type)
12634 {
12635 /* Delete overlay event and longjmp master breakpoints; they
12636 will be reset later by breakpoint_re_set. */
12637 case bp_overlay_event:
12638 case bp_longjmp_master:
12639 case bp_std_terminate_master:
12640 case bp_exception_master:
12641 delete_breakpoint (b);
12642 break;
12643
12644 /* This breakpoint is special, it's set up when the inferior
12645 starts and we really don't want to touch it. */
12646 case bp_shlib_event:
12647
12648 /* Like bp_shlib_event, this breakpoint type is special. Once
12649 it is set up, we do not want to touch it. */
12650 case bp_thread_event:
12651 break;
12652 }
12653 }
12654
12655 static void
12656 internal_bkpt_check_status (bpstat bs)
12657 {
12658 if (bs->breakpoint_at->type == bp_shlib_event)
12659 {
12660 /* If requested, stop when the dynamic linker notifies GDB of
12661 events. This allows the user to get control and place
12662 breakpoints in initializer routines for dynamically loaded
12663 objects (among other things). */
12664 bs->stop = stop_on_solib_events;
12665 bs->print = stop_on_solib_events;
12666 }
12667 else
12668 bs->stop = 0;
12669 }
12670
12671 static enum print_stop_action
12672 internal_bkpt_print_it (bpstat bs)
12673 {
12674 struct breakpoint *b;
12675
12676 b = bs->breakpoint_at;
12677
12678 switch (b->type)
12679 {
12680 case bp_shlib_event:
12681 /* Did we stop because the user set the stop_on_solib_events
12682 variable? (If so, we report this as a generic, "Stopped due
12683 to shlib event" message.) */
12684 print_solib_event (0);
12685 break;
12686
12687 case bp_thread_event:
12688 /* Not sure how we will get here.
12689 GDB should not stop for these breakpoints. */
12690 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12691 break;
12692
12693 case bp_overlay_event:
12694 /* By analogy with the thread event, GDB should not stop for these. */
12695 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12696 break;
12697
12698 case bp_longjmp_master:
12699 /* These should never be enabled. */
12700 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12701 break;
12702
12703 case bp_std_terminate_master:
12704 /* These should never be enabled. */
12705 printf_filtered (_("std::terminate Master Breakpoint: "
12706 "gdb should not stop!\n"));
12707 break;
12708
12709 case bp_exception_master:
12710 /* These should never be enabled. */
12711 printf_filtered (_("Exception Master Breakpoint: "
12712 "gdb should not stop!\n"));
12713 break;
12714 }
12715
12716 return PRINT_NOTHING;
12717 }
12718
12719 static void
12720 internal_bkpt_print_mention (struct breakpoint *b)
12721 {
12722 /* Nothing to mention. These breakpoints are internal. */
12723 }
12724
12725 /* Virtual table for momentary breakpoints */
12726
12727 static void
12728 momentary_bkpt_re_set (struct breakpoint *b)
12729 {
12730 /* Keep temporary breakpoints, which can be encountered when we step
12731 over a dlopen call and solib_add is resetting the breakpoints.
12732 Otherwise these should have been blown away via the cleanup chain
12733 or by breakpoint_init_inferior when we rerun the executable. */
12734 }
12735
12736 static void
12737 momentary_bkpt_check_status (bpstat bs)
12738 {
12739 /* Nothing. The point of these breakpoints is causing a stop. */
12740 }
12741
12742 static enum print_stop_action
12743 momentary_bkpt_print_it (bpstat bs)
12744 {
12745 return PRINT_UNKNOWN;
12746 }
12747
12748 static void
12749 momentary_bkpt_print_mention (struct breakpoint *b)
12750 {
12751 /* Nothing to mention. These breakpoints are internal. */
12752 }
12753
12754 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12755
12756 It gets cleared already on the removal of the first one of such placed
12757 breakpoints. This is OK as they get all removed altogether. */
12758
12759 longjmp_breakpoint::~longjmp_breakpoint ()
12760 {
12761 thread_info *tp = find_thread_global_id (this->thread);
12762
12763 if (tp != NULL)
12764 tp->initiating_frame = null_frame_id;
12765 }
12766
12767 /* Specific methods for probe breakpoints. */
12768
12769 static int
12770 bkpt_probe_insert_location (struct bp_location *bl)
12771 {
12772 int v = bkpt_insert_location (bl);
12773
12774 if (v == 0)
12775 {
12776 /* The insertion was successful, now let's set the probe's semaphore
12777 if needed. */
12778 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
12779 }
12780
12781 return v;
12782 }
12783
12784 static int
12785 bkpt_probe_remove_location (struct bp_location *bl,
12786 enum remove_bp_reason reason)
12787 {
12788 /* Let's clear the semaphore before removing the location. */
12789 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
12790
12791 return bkpt_remove_location (bl, reason);
12792 }
12793
12794 static void
12795 bkpt_probe_create_sals_from_location (struct event_location *location,
12796 struct linespec_result *canonical,
12797 enum bptype type_wanted)
12798 {
12799 struct linespec_sals lsal;
12800
12801 lsal.sals = parse_probes (location, NULL, canonical);
12802 lsal.canonical
12803 = xstrdup (event_location_to_string (canonical->location.get ()));
12804 canonical->lsals.push_back (std::move (lsal));
12805 }
12806
12807 static std::vector<symtab_and_line>
12808 bkpt_probe_decode_location (struct breakpoint *b,
12809 struct event_location *location,
12810 struct program_space *search_pspace)
12811 {
12812 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12813 if (sals.empty ())
12814 error (_("probe not found"));
12815 return sals;
12816 }
12817
12818 /* The breakpoint_ops structure to be used in tracepoints. */
12819
12820 static void
12821 tracepoint_re_set (struct breakpoint *b)
12822 {
12823 breakpoint_re_set_default (b);
12824 }
12825
12826 static int
12827 tracepoint_breakpoint_hit (const struct bp_location *bl,
12828 const address_space *aspace, CORE_ADDR bp_addr,
12829 const struct target_waitstatus *ws)
12830 {
12831 /* By definition, the inferior does not report stops at
12832 tracepoints. */
12833 return 0;
12834 }
12835
12836 static void
12837 tracepoint_print_one_detail (const struct breakpoint *self,
12838 struct ui_out *uiout)
12839 {
12840 struct tracepoint *tp = (struct tracepoint *) self;
12841 if (!tp->static_trace_marker_id.empty ())
12842 {
12843 gdb_assert (self->type == bp_static_tracepoint);
12844
12845 uiout->message ("\tmarker id is %pF\n",
12846 string_field ("static-tracepoint-marker-string-id",
12847 tp->static_trace_marker_id.c_str ()));
12848 }
12849 }
12850
12851 static void
12852 tracepoint_print_mention (struct breakpoint *b)
12853 {
12854 if (current_uiout->is_mi_like_p ())
12855 return;
12856
12857 switch (b->type)
12858 {
12859 case bp_tracepoint:
12860 printf_filtered (_("Tracepoint"));
12861 printf_filtered (_(" %d"), b->number);
12862 break;
12863 case bp_fast_tracepoint:
12864 printf_filtered (_("Fast tracepoint"));
12865 printf_filtered (_(" %d"), b->number);
12866 break;
12867 case bp_static_tracepoint:
12868 printf_filtered (_("Static tracepoint"));
12869 printf_filtered (_(" %d"), b->number);
12870 break;
12871 default:
12872 internal_error (__FILE__, __LINE__,
12873 _("unhandled tracepoint type %d"), (int) b->type);
12874 }
12875
12876 say_where (b);
12877 }
12878
12879 static void
12880 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
12881 {
12882 struct tracepoint *tp = (struct tracepoint *) self;
12883
12884 if (self->type == bp_fast_tracepoint)
12885 fprintf_unfiltered (fp, "ftrace");
12886 else if (self->type == bp_static_tracepoint)
12887 fprintf_unfiltered (fp, "strace");
12888 else if (self->type == bp_tracepoint)
12889 fprintf_unfiltered (fp, "trace");
12890 else
12891 internal_error (__FILE__, __LINE__,
12892 _("unhandled tracepoint type %d"), (int) self->type);
12893
12894 fprintf_unfiltered (fp, " %s",
12895 event_location_to_string (self->location.get ()));
12896 print_recreate_thread (self, fp);
12897
12898 if (tp->pass_count)
12899 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
12900 }
12901
12902 static void
12903 tracepoint_create_sals_from_location (struct event_location *location,
12904 struct linespec_result *canonical,
12905 enum bptype type_wanted)
12906 {
12907 create_sals_from_location_default (location, canonical, type_wanted);
12908 }
12909
12910 static void
12911 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12912 struct linespec_result *canonical,
12913 gdb::unique_xmalloc_ptr<char> cond_string,
12914 gdb::unique_xmalloc_ptr<char> extra_string,
12915 enum bptype type_wanted,
12916 enum bpdisp disposition,
12917 int thread,
12918 int task, int ignore_count,
12919 const struct breakpoint_ops *ops,
12920 int from_tty, int enabled,
12921 int internal, unsigned flags)
12922 {
12923 create_breakpoints_sal_default (gdbarch, canonical,
12924 std::move (cond_string),
12925 std::move (extra_string),
12926 type_wanted,
12927 disposition, thread, task,
12928 ignore_count, ops, from_tty,
12929 enabled, internal, flags);
12930 }
12931
12932 static std::vector<symtab_and_line>
12933 tracepoint_decode_location (struct breakpoint *b,
12934 struct event_location *location,
12935 struct program_space *search_pspace)
12936 {
12937 return decode_location_default (b, location, search_pspace);
12938 }
12939
12940 struct breakpoint_ops tracepoint_breakpoint_ops;
12941
12942 /* Virtual table for tracepoints on static probes. */
12943
12944 static void
12945 tracepoint_probe_create_sals_from_location
12946 (struct event_location *location,
12947 struct linespec_result *canonical,
12948 enum bptype type_wanted)
12949 {
12950 /* We use the same method for breakpoint on probes. */
12951 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
12952 }
12953
12954 static std::vector<symtab_and_line>
12955 tracepoint_probe_decode_location (struct breakpoint *b,
12956 struct event_location *location,
12957 struct program_space *search_pspace)
12958 {
12959 /* We use the same method for breakpoint on probes. */
12960 return bkpt_probe_decode_location (b, location, search_pspace);
12961 }
12962
12963 /* Dprintf breakpoint_ops methods. */
12964
12965 static void
12966 dprintf_re_set (struct breakpoint *b)
12967 {
12968 breakpoint_re_set_default (b);
12969
12970 /* extra_string should never be non-NULL for dprintf. */
12971 gdb_assert (b->extra_string != NULL);
12972
12973 /* 1 - connect to target 1, that can run breakpoint commands.
12974 2 - create a dprintf, which resolves fine.
12975 3 - disconnect from target 1
12976 4 - connect to target 2, that can NOT run breakpoint commands.
12977
12978 After steps #3/#4, you'll want the dprintf command list to
12979 be updated, because target 1 and 2 may well return different
12980 answers for target_can_run_breakpoint_commands().
12981 Given absence of finer grained resetting, we get to do
12982 it all the time. */
12983 if (b->extra_string != NULL)
12984 update_dprintf_command_list (b);
12985 }
12986
12987 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
12988
12989 static void
12990 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12991 {
12992 fprintf_unfiltered (fp, "dprintf %s,%s",
12993 event_location_to_string (tp->location.get ()),
12994 tp->extra_string);
12995 print_recreate_thread (tp, fp);
12996 }
12997
12998 /* Implement the "after_condition_true" breakpoint_ops method for
12999 dprintf.
13000
13001 dprintf's are implemented with regular commands in their command
13002 list, but we run the commands here instead of before presenting the
13003 stop to the user, as dprintf's don't actually cause a stop. This
13004 also makes it so that the commands of multiple dprintfs at the same
13005 address are all handled. */
13006
13007 static void
13008 dprintf_after_condition_true (struct bpstats *bs)
13009 {
13010 struct bpstats tmp_bs;
13011 struct bpstats *tmp_bs_p = &tmp_bs;
13012
13013 /* dprintf's never cause a stop. This wasn't set in the
13014 check_status hook instead because that would make the dprintf's
13015 condition not be evaluated. */
13016 bs->stop = 0;
13017
13018 /* Run the command list here. Take ownership of it instead of
13019 copying. We never want these commands to run later in
13020 bpstat_do_actions, if a breakpoint that causes a stop happens to
13021 be set at same address as this dprintf, or even if running the
13022 commands here throws. */
13023 tmp_bs.commands = bs->commands;
13024 bs->commands = NULL;
13025
13026 bpstat_do_actions_1 (&tmp_bs_p);
13027
13028 /* 'tmp_bs.commands' will usually be NULL by now, but
13029 bpstat_do_actions_1 may return early without processing the whole
13030 list. */
13031 }
13032
13033 /* The breakpoint_ops structure to be used on static tracepoints with
13034 markers (`-m'). */
13035
13036 static void
13037 strace_marker_create_sals_from_location (struct event_location *location,
13038 struct linespec_result *canonical,
13039 enum bptype type_wanted)
13040 {
13041 struct linespec_sals lsal;
13042 const char *arg_start, *arg;
13043
13044 arg = arg_start = get_linespec_location (location)->spec_string;
13045 lsal.sals = decode_static_tracepoint_spec (&arg);
13046
13047 std::string str (arg_start, arg - arg_start);
13048 const char *ptr = str.c_str ();
13049 canonical->location
13050 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
13051
13052 lsal.canonical
13053 = xstrdup (event_location_to_string (canonical->location.get ()));
13054 canonical->lsals.push_back (std::move (lsal));
13055 }
13056
13057 static void
13058 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13059 struct linespec_result *canonical,
13060 gdb::unique_xmalloc_ptr<char> cond_string,
13061 gdb::unique_xmalloc_ptr<char> extra_string,
13062 enum bptype type_wanted,
13063 enum bpdisp disposition,
13064 int thread,
13065 int task, int ignore_count,
13066 const struct breakpoint_ops *ops,
13067 int from_tty, int enabled,
13068 int internal, unsigned flags)
13069 {
13070 const linespec_sals &lsal = canonical->lsals[0];
13071
13072 /* If the user is creating a static tracepoint by marker id
13073 (strace -m MARKER_ID), then store the sals index, so that
13074 breakpoint_re_set can try to match up which of the newly
13075 found markers corresponds to this one, and, don't try to
13076 expand multiple locations for each sal, given than SALS
13077 already should contain all sals for MARKER_ID. */
13078
13079 for (size_t i = 0; i < lsal.sals.size (); i++)
13080 {
13081 event_location_up location
13082 = copy_event_location (canonical->location.get ());
13083
13084 std::unique_ptr<tracepoint> tp (new tracepoint ());
13085 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
13086 std::move (location), NULL,
13087 std::move (cond_string),
13088 std::move (extra_string),
13089 type_wanted, disposition,
13090 thread, task, ignore_count, ops,
13091 from_tty, enabled, internal, flags,
13092 canonical->special_display);
13093 /* Given that its possible to have multiple markers with
13094 the same string id, if the user is creating a static
13095 tracepoint by marker id ("strace -m MARKER_ID"), then
13096 store the sals index, so that breakpoint_re_set can
13097 try to match up which of the newly found markers
13098 corresponds to this one */
13099 tp->static_trace_marker_id_idx = i;
13100
13101 install_breakpoint (internal, std::move (tp), 0);
13102 }
13103 }
13104
13105 static std::vector<symtab_and_line>
13106 strace_marker_decode_location (struct breakpoint *b,
13107 struct event_location *location,
13108 struct program_space *search_pspace)
13109 {
13110 struct tracepoint *tp = (struct tracepoint *) b;
13111 const char *s = get_linespec_location (location)->spec_string;
13112
13113 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13114 if (sals.size () > tp->static_trace_marker_id_idx)
13115 {
13116 sals[0] = sals[tp->static_trace_marker_id_idx];
13117 sals.resize (1);
13118 return sals;
13119 }
13120 else
13121 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
13122 }
13123
13124 static struct breakpoint_ops strace_marker_breakpoint_ops;
13125
13126 static int
13127 strace_marker_p (struct breakpoint *b)
13128 {
13129 return b->ops == &strace_marker_breakpoint_ops;
13130 }
13131
13132 /* Delete a breakpoint and clean up all traces of it in the data
13133 structures. */
13134
13135 void
13136 delete_breakpoint (struct breakpoint *bpt)
13137 {
13138 gdb_assert (bpt != NULL);
13139
13140 /* Has this bp already been deleted? This can happen because
13141 multiple lists can hold pointers to bp's. bpstat lists are
13142 especial culprits.
13143
13144 One example of this happening is a watchpoint's scope bp. When
13145 the scope bp triggers, we notice that the watchpoint is out of
13146 scope, and delete it. We also delete its scope bp. But the
13147 scope bp is marked "auto-deleting", and is already on a bpstat.
13148 That bpstat is then checked for auto-deleting bp's, which are
13149 deleted.
13150
13151 A real solution to this problem might involve reference counts in
13152 bp's, and/or giving them pointers back to their referencing
13153 bpstat's, and teaching delete_breakpoint to only free a bp's
13154 storage when no more references were extent. A cheaper bandaid
13155 was chosen. */
13156 if (bpt->type == bp_none)
13157 return;
13158
13159 /* At least avoid this stale reference until the reference counting
13160 of breakpoints gets resolved. */
13161 if (bpt->related_breakpoint != bpt)
13162 {
13163 struct breakpoint *related;
13164 struct watchpoint *w;
13165
13166 if (bpt->type == bp_watchpoint_scope)
13167 w = (struct watchpoint *) bpt->related_breakpoint;
13168 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13169 w = (struct watchpoint *) bpt;
13170 else
13171 w = NULL;
13172 if (w != NULL)
13173 watchpoint_del_at_next_stop (w);
13174
13175 /* Unlink bpt from the bpt->related_breakpoint ring. */
13176 for (related = bpt; related->related_breakpoint != bpt;
13177 related = related->related_breakpoint);
13178 related->related_breakpoint = bpt->related_breakpoint;
13179 bpt->related_breakpoint = bpt;
13180 }
13181
13182 /* watch_command_1 creates a watchpoint but only sets its number if
13183 update_watchpoint succeeds in creating its bp_locations. If there's
13184 a problem in that process, we'll be asked to delete the half-created
13185 watchpoint. In that case, don't announce the deletion. */
13186 if (bpt->number)
13187 gdb::observers::breakpoint_deleted.notify (bpt);
13188
13189 if (breakpoint_chain == bpt)
13190 breakpoint_chain = bpt->next;
13191
13192 for (breakpoint *b : all_breakpoints ())
13193 if (b->next == bpt)
13194 {
13195 b->next = bpt->next;
13196 break;
13197 }
13198
13199 /* Be sure no bpstat's are pointing at the breakpoint after it's
13200 been freed. */
13201 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13202 in all threads for now. Note that we cannot just remove bpstats
13203 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13204 commands are associated with the bpstat; if we remove it here,
13205 then the later call to bpstat_do_actions (&stop_bpstat); in
13206 event-top.c won't do anything, and temporary breakpoints with
13207 commands won't work. */
13208
13209 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13210
13211 /* Now that breakpoint is removed from breakpoint list, update the
13212 global location list. This will remove locations that used to
13213 belong to this breakpoint. Do this before freeing the breakpoint
13214 itself, since remove_breakpoint looks at location's owner. It
13215 might be better design to have location completely
13216 self-contained, but it's not the case now. */
13217 update_global_location_list (UGLL_DONT_INSERT);
13218
13219 /* On the chance that someone will soon try again to delete this
13220 same bp, we mark it as deleted before freeing its storage. */
13221 bpt->type = bp_none;
13222 delete bpt;
13223 }
13224
13225 /* Iterator function to call a user-provided callback function once
13226 for each of B and its related breakpoints. */
13227
13228 static void
13229 iterate_over_related_breakpoints (struct breakpoint *b,
13230 gdb::function_view<void (breakpoint *)> function)
13231 {
13232 struct breakpoint *related;
13233
13234 related = b;
13235 do
13236 {
13237 struct breakpoint *next;
13238
13239 /* FUNCTION may delete RELATED. */
13240 next = related->related_breakpoint;
13241
13242 if (next == related)
13243 {
13244 /* RELATED is the last ring entry. */
13245 function (related);
13246
13247 /* FUNCTION may have deleted it, so we'd never reach back to
13248 B. There's nothing left to do anyway, so just break
13249 out. */
13250 break;
13251 }
13252 else
13253 function (related);
13254
13255 related = next;
13256 }
13257 while (related != b);
13258 }
13259
13260 static void
13261 delete_command (const char *arg, int from_tty)
13262 {
13263 dont_repeat ();
13264
13265 if (arg == 0)
13266 {
13267 int breaks_to_delete = 0;
13268
13269 /* Delete all breakpoints if no argument. Do not delete
13270 internal breakpoints, these have to be deleted with an
13271 explicit breakpoint number argument. */
13272 for (breakpoint *b : all_breakpoints ())
13273 if (user_breakpoint_p (b))
13274 {
13275 breaks_to_delete = 1;
13276 break;
13277 }
13278
13279 /* Ask user only if there are some breakpoints to delete. */
13280 if (!from_tty
13281 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13282 for (breakpoint *b : all_breakpoints_safe ())
13283 if (user_breakpoint_p (b))
13284 delete_breakpoint (b);
13285 }
13286 else
13287 map_breakpoint_numbers
13288 (arg, [&] (breakpoint *br)
13289 {
13290 iterate_over_related_breakpoints (br, delete_breakpoint);
13291 });
13292 }
13293
13294 /* Return true if all locations of B bound to PSPACE are pending. If
13295 PSPACE is NULL, all locations of all program spaces are
13296 considered. */
13297
13298 static int
13299 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13300 {
13301 for (bp_location *loc : b->locations ())
13302 if ((pspace == NULL
13303 || loc->pspace == pspace)
13304 && !loc->shlib_disabled
13305 && !loc->pspace->executing_startup)
13306 return 0;
13307 return 1;
13308 }
13309
13310 /* Subroutine of update_breakpoint_locations to simplify it.
13311 Return non-zero if multiple fns in list LOC have the same name.
13312 Null names are ignored. */
13313
13314 static int
13315 ambiguous_names_p (struct bp_location *loc)
13316 {
13317 struct bp_location *l;
13318 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
13319 xcalloc, xfree));
13320
13321 for (l = loc; l != NULL; l = l->next)
13322 {
13323 const char **slot;
13324 const char *name = l->function_name;
13325
13326 /* Allow for some names to be NULL, ignore them. */
13327 if (name == NULL)
13328 continue;
13329
13330 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
13331 INSERT);
13332 /* NOTE: We can assume slot != NULL here because xcalloc never
13333 returns NULL. */
13334 if (*slot != NULL)
13335 return 1;
13336 *slot = name;
13337 }
13338
13339 return 0;
13340 }
13341
13342 /* When symbols change, it probably means the sources changed as well,
13343 and it might mean the static tracepoint markers are no longer at
13344 the same address or line numbers they used to be at last we
13345 checked. Losing your static tracepoints whenever you rebuild is
13346 undesirable. This function tries to resync/rematch gdb static
13347 tracepoints with the markers on the target, for static tracepoints
13348 that have not been set by marker id. Static tracepoint that have
13349 been set by marker id are reset by marker id in breakpoint_re_set.
13350 The heuristic is:
13351
13352 1) For a tracepoint set at a specific address, look for a marker at
13353 the old PC. If one is found there, assume to be the same marker.
13354 If the name / string id of the marker found is different from the
13355 previous known name, assume that means the user renamed the marker
13356 in the sources, and output a warning.
13357
13358 2) For a tracepoint set at a given line number, look for a marker
13359 at the new address of the old line number. If one is found there,
13360 assume to be the same marker. If the name / string id of the
13361 marker found is different from the previous known name, assume that
13362 means the user renamed the marker in the sources, and output a
13363 warning.
13364
13365 3) If a marker is no longer found at the same address or line, it
13366 may mean the marker no longer exists. But it may also just mean
13367 the code changed a bit. Maybe the user added a few lines of code
13368 that made the marker move up or down (in line number terms). Ask
13369 the target for info about the marker with the string id as we knew
13370 it. If found, update line number and address in the matching
13371 static tracepoint. This will get confused if there's more than one
13372 marker with the same ID (possible in UST, although unadvised
13373 precisely because it confuses tools). */
13374
13375 static struct symtab_and_line
13376 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13377 {
13378 struct tracepoint *tp = (struct tracepoint *) b;
13379 struct static_tracepoint_marker marker;
13380 CORE_ADDR pc;
13381
13382 pc = sal.pc;
13383 if (sal.line)
13384 find_line_pc (sal.symtab, sal.line, &pc);
13385
13386 if (target_static_tracepoint_marker_at (pc, &marker))
13387 {
13388 if (tp->static_trace_marker_id != marker.str_id)
13389 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13390 b->number, tp->static_trace_marker_id.c_str (),
13391 marker.str_id.c_str ());
13392
13393 tp->static_trace_marker_id = std::move (marker.str_id);
13394
13395 return sal;
13396 }
13397
13398 /* Old marker wasn't found on target at lineno. Try looking it up
13399 by string ID. */
13400 if (!sal.explicit_pc
13401 && sal.line != 0
13402 && sal.symtab != NULL
13403 && !tp->static_trace_marker_id.empty ())
13404 {
13405 std::vector<static_tracepoint_marker> markers
13406 = target_static_tracepoint_markers_by_strid
13407 (tp->static_trace_marker_id.c_str ());
13408
13409 if (!markers.empty ())
13410 {
13411 struct symbol *sym;
13412 struct static_tracepoint_marker *tpmarker;
13413 struct ui_out *uiout = current_uiout;
13414 struct explicit_location explicit_loc;
13415
13416 tpmarker = &markers[0];
13417
13418 tp->static_trace_marker_id = std::move (tpmarker->str_id);
13419
13420 warning (_("marker for static tracepoint %d (%s) not "
13421 "found at previous line number"),
13422 b->number, tp->static_trace_marker_id.c_str ());
13423
13424 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
13425 sym = find_pc_sect_function (tpmarker->address, NULL);
13426 uiout->text ("Now in ");
13427 if (sym)
13428 {
13429 uiout->field_string ("func", sym->print_name (),
13430 function_name_style.style ());
13431 uiout->text (" at ");
13432 }
13433 uiout->field_string ("file",
13434 symtab_to_filename_for_display (sal2.symtab),
13435 file_name_style.style ());
13436 uiout->text (":");
13437
13438 if (uiout->is_mi_like_p ())
13439 {
13440 const char *fullname = symtab_to_fullname (sal2.symtab);
13441
13442 uiout->field_string ("fullname", fullname);
13443 }
13444
13445 uiout->field_signed ("line", sal2.line);
13446 uiout->text ("\n");
13447
13448 b->loc->line_number = sal2.line;
13449 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13450
13451 b->location.reset (NULL);
13452 initialize_explicit_location (&explicit_loc);
13453 explicit_loc.source_filename
13454 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13455 explicit_loc.line_offset.offset = b->loc->line_number;
13456 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13457 b->location = new_explicit_location (&explicit_loc);
13458
13459 /* Might be nice to check if function changed, and warn if
13460 so. */
13461 }
13462 }
13463 return sal;
13464 }
13465
13466 /* Returns 1 iff locations A and B are sufficiently same that
13467 we don't need to report breakpoint as changed. */
13468
13469 static int
13470 locations_are_equal (struct bp_location *a, struct bp_location *b)
13471 {
13472 while (a && b)
13473 {
13474 if (a->address != b->address)
13475 return 0;
13476
13477 if (a->shlib_disabled != b->shlib_disabled)
13478 return 0;
13479
13480 if (a->enabled != b->enabled)
13481 return 0;
13482
13483 if (a->disabled_by_cond != b->disabled_by_cond)
13484 return 0;
13485
13486 a = a->next;
13487 b = b->next;
13488 }
13489
13490 if ((a == NULL) != (b == NULL))
13491 return 0;
13492
13493 return 1;
13494 }
13495
13496 /* Split all locations of B that are bound to PSPACE out of B's
13497 location list to a separate list and return that list's head. If
13498 PSPACE is NULL, hoist out all locations of B. */
13499
13500 static struct bp_location *
13501 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13502 {
13503 struct bp_location head;
13504 struct bp_location *i = b->loc;
13505 struct bp_location **i_link = &b->loc;
13506 struct bp_location *hoisted = &head;
13507
13508 if (pspace == NULL)
13509 {
13510 i = b->loc;
13511 b->loc = NULL;
13512 return i;
13513 }
13514
13515 head.next = NULL;
13516
13517 while (i != NULL)
13518 {
13519 if (i->pspace == pspace)
13520 {
13521 *i_link = i->next;
13522 i->next = NULL;
13523 hoisted->next = i;
13524 hoisted = i;
13525 }
13526 else
13527 i_link = &i->next;
13528 i = *i_link;
13529 }
13530
13531 return head.next;
13532 }
13533
13534 /* Create new breakpoint locations for B (a hardware or software
13535 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13536 zero, then B is a ranged breakpoint. Only recreates locations for
13537 FILTER_PSPACE. Locations of other program spaces are left
13538 untouched. */
13539
13540 void
13541 update_breakpoint_locations (struct breakpoint *b,
13542 struct program_space *filter_pspace,
13543 gdb::array_view<const symtab_and_line> sals,
13544 gdb::array_view<const symtab_and_line> sals_end)
13545 {
13546 struct bp_location *existing_locations;
13547
13548 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
13549 {
13550 /* Ranged breakpoints have only one start location and one end
13551 location. */
13552 b->enable_state = bp_disabled;
13553 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13554 "multiple locations found\n"),
13555 b->number);
13556 return;
13557 }
13558
13559 /* If there's no new locations, and all existing locations are
13560 pending, don't do anything. This optimizes the common case where
13561 all locations are in the same shared library, that was unloaded.
13562 We'd like to retain the location, so that when the library is
13563 loaded again, we don't loose the enabled/disabled status of the
13564 individual locations. */
13565 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
13566 return;
13567
13568 existing_locations = hoist_existing_locations (b, filter_pspace);
13569
13570 for (const auto &sal : sals)
13571 {
13572 struct bp_location *new_loc;
13573
13574 switch_to_program_space_and_thread (sal.pspace);
13575
13576 new_loc = add_location_to_breakpoint (b, &sal);
13577
13578 /* Reparse conditions, they might contain references to the
13579 old symtab. */
13580 if (b->cond_string != NULL)
13581 {
13582 const char *s;
13583
13584 s = b->cond_string;
13585 try
13586 {
13587 new_loc->cond = parse_exp_1 (&s, sal.pc,
13588 block_for_pc (sal.pc),
13589 0);
13590 }
13591 catch (const gdb_exception_error &e)
13592 {
13593 new_loc->disabled_by_cond = true;
13594 }
13595 }
13596
13597 if (!sals_end.empty ())
13598 {
13599 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13600
13601 new_loc->length = end - sals[0].pc + 1;
13602 }
13603 }
13604
13605 /* If possible, carry over 'disable' status from existing
13606 breakpoints. */
13607 {
13608 struct bp_location *e = existing_locations;
13609 /* If there are multiple breakpoints with the same function name,
13610 e.g. for inline functions, comparing function names won't work.
13611 Instead compare pc addresses; this is just a heuristic as things
13612 may have moved, but in practice it gives the correct answer
13613 often enough until a better solution is found. */
13614 int have_ambiguous_names = ambiguous_names_p (b->loc);
13615
13616 for (; e; e = e->next)
13617 {
13618 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
13619 {
13620 if (have_ambiguous_names)
13621 {
13622 for (bp_location *l : b->locations ())
13623 {
13624 /* Ignore software vs hardware location type at
13625 this point, because with "set breakpoint
13626 auto-hw", after a re-set, locations that were
13627 hardware can end up as software, or vice versa.
13628 As mentioned above, this is an heuristic and in
13629 practice should give the correct answer often
13630 enough. */
13631 if (breakpoint_locations_match (e, l, true))
13632 {
13633 l->enabled = e->enabled;
13634 l->disabled_by_cond = e->disabled_by_cond;
13635 break;
13636 }
13637 }
13638 }
13639 else
13640 {
13641 for (bp_location *l : b->locations ())
13642 if (l->function_name
13643 && strcmp (e->function_name, l->function_name) == 0)
13644 {
13645 l->enabled = e->enabled;
13646 l->disabled_by_cond = e->disabled_by_cond;
13647 break;
13648 }
13649 }
13650 }
13651 }
13652 }
13653
13654 if (!locations_are_equal (existing_locations, b->loc))
13655 gdb::observers::breakpoint_modified.notify (b);
13656 }
13657
13658 /* Find the SaL locations corresponding to the given LOCATION.
13659 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13660
13661 static std::vector<symtab_and_line>
13662 location_to_sals (struct breakpoint *b, struct event_location *location,
13663 struct program_space *search_pspace, int *found)
13664 {
13665 struct gdb_exception exception;
13666
13667 gdb_assert (b->ops != NULL);
13668
13669 std::vector<symtab_and_line> sals;
13670
13671 try
13672 {
13673 sals = b->ops->decode_location (b, location, search_pspace);
13674 }
13675 catch (gdb_exception_error &e)
13676 {
13677 int not_found_and_ok = 0;
13678
13679 /* For pending breakpoints, it's expected that parsing will
13680 fail until the right shared library is loaded. User has
13681 already told to create pending breakpoints and don't need
13682 extra messages. If breakpoint is in bp_shlib_disabled
13683 state, then user already saw the message about that
13684 breakpoint being disabled, and don't want to see more
13685 errors. */
13686 if (e.error == NOT_FOUND_ERROR
13687 && (b->condition_not_parsed
13688 || (b->loc != NULL
13689 && search_pspace != NULL
13690 && b->loc->pspace != search_pspace)
13691 || (b->loc && b->loc->shlib_disabled)
13692 || (b->loc && b->loc->pspace->executing_startup)
13693 || b->enable_state == bp_disabled))
13694 not_found_and_ok = 1;
13695
13696 if (!not_found_and_ok)
13697 {
13698 /* We surely don't want to warn about the same breakpoint
13699 10 times. One solution, implemented here, is disable
13700 the breakpoint on error. Another solution would be to
13701 have separate 'warning emitted' flag. Since this
13702 happens only when a binary has changed, I don't know
13703 which approach is better. */
13704 b->enable_state = bp_disabled;
13705 throw;
13706 }
13707
13708 exception = std::move (e);
13709 }
13710
13711 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13712 {
13713 for (auto &sal : sals)
13714 resolve_sal_pc (&sal);
13715 if (b->condition_not_parsed && b->extra_string != NULL)
13716 {
13717 char *cond_string, *extra_string;
13718 int thread, task;
13719
13720 find_condition_and_thread_for_sals (sals, b->extra_string,
13721 &cond_string, &thread,
13722 &task, &extra_string);
13723 gdb_assert (b->cond_string == NULL);
13724 if (cond_string)
13725 b->cond_string = cond_string;
13726 b->thread = thread;
13727 b->task = task;
13728 if (extra_string)
13729 {
13730 xfree (b->extra_string);
13731 b->extra_string = extra_string;
13732 }
13733 b->condition_not_parsed = 0;
13734 }
13735
13736 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13737 sals[0] = update_static_tracepoint (b, sals[0]);
13738
13739 *found = 1;
13740 }
13741 else
13742 *found = 0;
13743
13744 return sals;
13745 }
13746
13747 /* The default re_set method, for typical hardware or software
13748 breakpoints. Reevaluate the breakpoint and recreate its
13749 locations. */
13750
13751 static void
13752 breakpoint_re_set_default (struct breakpoint *b)
13753 {
13754 struct program_space *filter_pspace = current_program_space;
13755 std::vector<symtab_and_line> expanded, expanded_end;
13756
13757 int found;
13758 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13759 filter_pspace, &found);
13760 if (found)
13761 expanded = std::move (sals);
13762
13763 if (b->location_range_end != NULL)
13764 {
13765 std::vector<symtab_and_line> sals_end
13766 = location_to_sals (b, b->location_range_end.get (),
13767 filter_pspace, &found);
13768 if (found)
13769 expanded_end = std::move (sals_end);
13770 }
13771
13772 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
13773 }
13774
13775 /* Default method for creating SALs from an address string. It basically
13776 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13777
13778 static void
13779 create_sals_from_location_default (struct event_location *location,
13780 struct linespec_result *canonical,
13781 enum bptype type_wanted)
13782 {
13783 parse_breakpoint_sals (location, canonical);
13784 }
13785
13786 /* Call create_breakpoints_sal for the given arguments. This is the default
13787 function for the `create_breakpoints_sal' method of
13788 breakpoint_ops. */
13789
13790 static void
13791 create_breakpoints_sal_default (struct gdbarch *gdbarch,
13792 struct linespec_result *canonical,
13793 gdb::unique_xmalloc_ptr<char> cond_string,
13794 gdb::unique_xmalloc_ptr<char> extra_string,
13795 enum bptype type_wanted,
13796 enum bpdisp disposition,
13797 int thread,
13798 int task, int ignore_count,
13799 const struct breakpoint_ops *ops,
13800 int from_tty, int enabled,
13801 int internal, unsigned flags)
13802 {
13803 create_breakpoints_sal (gdbarch, canonical,
13804 std::move (cond_string),
13805 std::move (extra_string),
13806 type_wanted, disposition,
13807 thread, task, ignore_count, ops, from_tty,
13808 enabled, internal, flags);
13809 }
13810
13811 /* Decode the line represented by S by calling decode_line_full. This is the
13812 default function for the `decode_location' method of breakpoint_ops. */
13813
13814 static std::vector<symtab_and_line>
13815 decode_location_default (struct breakpoint *b,
13816 struct event_location *location,
13817 struct program_space *search_pspace)
13818 {
13819 struct linespec_result canonical;
13820
13821 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
13822 NULL, 0, &canonical, multiple_symbols_all,
13823 b->filter.get ());
13824
13825 /* We should get 0 or 1 resulting SALs. */
13826 gdb_assert (canonical.lsals.size () < 2);
13827
13828 if (!canonical.lsals.empty ())
13829 {
13830 const linespec_sals &lsal = canonical.lsals[0];
13831 return std::move (lsal.sals);
13832 }
13833 return {};
13834 }
13835
13836 /* Reset a breakpoint. */
13837
13838 static void
13839 breakpoint_re_set_one (breakpoint *b)
13840 {
13841 input_radix = b->input_radix;
13842 set_language (b->language);
13843
13844 b->ops->re_set (b);
13845 }
13846
13847 /* Re-set breakpoint locations for the current program space.
13848 Locations bound to other program spaces are left untouched. */
13849
13850 void
13851 breakpoint_re_set (void)
13852 {
13853 {
13854 scoped_restore_current_language save_language;
13855 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
13856 scoped_restore_current_pspace_and_thread restore_pspace_thread;
13857
13858 /* breakpoint_re_set_one sets the current_language to the language
13859 of the breakpoint it is resetting (see prepare_re_set_context)
13860 before re-evaluating the breakpoint's location. This change can
13861 unfortunately get undone by accident if the language_mode is set
13862 to auto, and we either switch frames, or more likely in this context,
13863 we select the current frame.
13864
13865 We prevent this by temporarily turning the language_mode to
13866 language_mode_manual. We restore it once all breakpoints
13867 have been reset. */
13868 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13869 language_mode = language_mode_manual;
13870
13871 /* Note: we must not try to insert locations until after all
13872 breakpoints have been re-set. Otherwise, e.g., when re-setting
13873 breakpoint 1, we'd insert the locations of breakpoint 2, which
13874 hadn't been re-set yet, and thus may have stale locations. */
13875
13876 for (breakpoint *b : all_breakpoints_safe ())
13877 {
13878 try
13879 {
13880 breakpoint_re_set_one (b);
13881 }
13882 catch (const gdb_exception &ex)
13883 {
13884 exception_fprintf (gdb_stderr, ex,
13885 "Error in re-setting breakpoint %d: ",
13886 b->number);
13887 }
13888 }
13889
13890 jit_breakpoint_re_set ();
13891 }
13892
13893 create_overlay_event_breakpoint ();
13894 create_longjmp_master_breakpoint ();
13895 create_std_terminate_master_breakpoint ();
13896 create_exception_master_breakpoint ();
13897
13898 /* Now we can insert. */
13899 update_global_location_list (UGLL_MAY_INSERT);
13900 }
13901 \f
13902 /* Reset the thread number of this breakpoint:
13903
13904 - If the breakpoint is for all threads, leave it as-is.
13905 - Else, reset it to the current thread for inferior_ptid. */
13906 void
13907 breakpoint_re_set_thread (struct breakpoint *b)
13908 {
13909 if (b->thread != -1)
13910 {
13911 b->thread = inferior_thread ()->global_num;
13912
13913 /* We're being called after following a fork. The new fork is
13914 selected as current, and unless this was a vfork will have a
13915 different program space from the original thread. Reset that
13916 as well. */
13917 b->loc->pspace = current_program_space;
13918 }
13919 }
13920
13921 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13922 If from_tty is nonzero, it prints a message to that effect,
13923 which ends with a period (no newline). */
13924
13925 void
13926 set_ignore_count (int bptnum, int count, int from_tty)
13927 {
13928 if (count < 0)
13929 count = 0;
13930
13931 for (breakpoint *b : all_breakpoints ())
13932 if (b->number == bptnum)
13933 {
13934 if (is_tracepoint (b))
13935 {
13936 if (from_tty && count != 0)
13937 printf_filtered (_("Ignore count ignored for tracepoint %d."),
13938 bptnum);
13939 return;
13940 }
13941
13942 b->ignore_count = count;
13943 if (from_tty)
13944 {
13945 if (count == 0)
13946 printf_filtered (_("Will stop next time "
13947 "breakpoint %d is reached."),
13948 bptnum);
13949 else if (count == 1)
13950 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
13951 bptnum);
13952 else
13953 printf_filtered (_("Will ignore next %d "
13954 "crossings of breakpoint %d."),
13955 count, bptnum);
13956 }
13957 gdb::observers::breakpoint_modified.notify (b);
13958 return;
13959 }
13960
13961 error (_("No breakpoint number %d."), bptnum);
13962 }
13963
13964 /* Command to set ignore-count of breakpoint N to COUNT. */
13965
13966 static void
13967 ignore_command (const char *args, int from_tty)
13968 {
13969 const char *p = args;
13970 int num;
13971
13972 if (p == 0)
13973 error_no_arg (_("a breakpoint number"));
13974
13975 num = get_number (&p);
13976 if (num == 0)
13977 error (_("bad breakpoint number: '%s'"), args);
13978 if (*p == 0)
13979 error (_("Second argument (specified ignore-count) is missing."));
13980
13981 set_ignore_count (num,
13982 longest_to_int (value_as_long (parse_and_eval (p))),
13983 from_tty);
13984 if (from_tty)
13985 printf_filtered ("\n");
13986 }
13987 \f
13988
13989 /* Call FUNCTION on each of the breakpoints with numbers in the range
13990 defined by BP_NUM_RANGE (an inclusive range). */
13991
13992 static void
13993 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13994 gdb::function_view<void (breakpoint *)> function)
13995 {
13996 if (bp_num_range.first == 0)
13997 {
13998 warning (_("bad breakpoint number at or near '%d'"),
13999 bp_num_range.first);
14000 }
14001 else
14002 {
14003 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
14004 {
14005 bool match = false;
14006
14007 for (breakpoint *b : all_breakpoints_safe ())
14008 if (b->number == i)
14009 {
14010 match = true;
14011 function (b);
14012 break;
14013 }
14014 if (!match)
14015 printf_unfiltered (_("No breakpoint number %d.\n"), i);
14016 }
14017 }
14018 }
14019
14020 /* Call FUNCTION on each of the breakpoints whose numbers are given in
14021 ARGS. */
14022
14023 static void
14024 map_breakpoint_numbers (const char *args,
14025 gdb::function_view<void (breakpoint *)> function)
14026 {
14027 if (args == NULL || *args == '\0')
14028 error_no_arg (_("one or more breakpoint numbers"));
14029
14030 number_or_range_parser parser (args);
14031
14032 while (!parser.finished ())
14033 {
14034 int num = parser.get_number ();
14035 map_breakpoint_number_range (std::make_pair (num, num), function);
14036 }
14037 }
14038
14039 /* Return the breakpoint location structure corresponding to the
14040 BP_NUM and LOC_NUM values. */
14041
14042 static struct bp_location *
14043 find_location_by_number (int bp_num, int loc_num)
14044 {
14045 breakpoint *b = get_breakpoint (bp_num);
14046
14047 if (!b || b->number != bp_num)
14048 error (_("Bad breakpoint number '%d'"), bp_num);
14049
14050 if (loc_num == 0)
14051 error (_("Bad breakpoint location number '%d'"), loc_num);
14052
14053 int n = 0;
14054 for (bp_location *loc : b->locations ())
14055 if (++n == loc_num)
14056 return loc;
14057
14058 error (_("Bad breakpoint location number '%d'"), loc_num);
14059 }
14060
14061 /* Modes of operation for extract_bp_num. */
14062 enum class extract_bp_kind
14063 {
14064 /* Extracting a breakpoint number. */
14065 bp,
14066
14067 /* Extracting a location number. */
14068 loc,
14069 };
14070
14071 /* Extract a breakpoint or location number (as determined by KIND)
14072 from the string starting at START. TRAILER is a character which
14073 can be found after the number. If you don't want a trailer, use
14074 '\0'. If END_OUT is not NULL, it is set to point after the parsed
14075 string. This always returns a positive integer. */
14076
14077 static int
14078 extract_bp_num (extract_bp_kind kind, const char *start,
14079 int trailer, const char **end_out = NULL)
14080 {
14081 const char *end = start;
14082 int num = get_number_trailer (&end, trailer);
14083 if (num < 0)
14084 error (kind == extract_bp_kind::bp
14085 ? _("Negative breakpoint number '%.*s'")
14086 : _("Negative breakpoint location number '%.*s'"),
14087 int (end - start), start);
14088 if (num == 0)
14089 error (kind == extract_bp_kind::bp
14090 ? _("Bad breakpoint number '%.*s'")
14091 : _("Bad breakpoint location number '%.*s'"),
14092 int (end - start), start);
14093
14094 if (end_out != NULL)
14095 *end_out = end;
14096 return num;
14097 }
14098
14099 /* Extract a breakpoint or location range (as determined by KIND) in
14100 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14101 representing the (inclusive) range. The returned pair's elements
14102 are always positive integers. */
14103
14104 static std::pair<int, int>
14105 extract_bp_or_bp_range (extract_bp_kind kind,
14106 const std::string &arg,
14107 std::string::size_type arg_offset)
14108 {
14109 std::pair<int, int> range;
14110 const char *bp_loc = &arg[arg_offset];
14111 std::string::size_type dash = arg.find ('-', arg_offset);
14112 if (dash != std::string::npos)
14113 {
14114 /* bp_loc is a range (x-z). */
14115 if (arg.length () == dash + 1)
14116 error (kind == extract_bp_kind::bp
14117 ? _("Bad breakpoint number at or near: '%s'")
14118 : _("Bad breakpoint location number at or near: '%s'"),
14119 bp_loc);
14120
14121 const char *end;
14122 const char *start_first = bp_loc;
14123 const char *start_second = &arg[dash + 1];
14124 range.first = extract_bp_num (kind, start_first, '-');
14125 range.second = extract_bp_num (kind, start_second, '\0', &end);
14126
14127 if (range.first > range.second)
14128 error (kind == extract_bp_kind::bp
14129 ? _("Inverted breakpoint range at '%.*s'")
14130 : _("Inverted breakpoint location range at '%.*s'"),
14131 int (end - start_first), start_first);
14132 }
14133 else
14134 {
14135 /* bp_loc is a single value. */
14136 range.first = extract_bp_num (kind, bp_loc, '\0');
14137 range.second = range.first;
14138 }
14139 return range;
14140 }
14141
14142 /* Extract the breakpoint/location range specified by ARG. Returns
14143 the breakpoint range in BP_NUM_RANGE, and the location range in
14144 BP_LOC_RANGE.
14145
14146 ARG may be in any of the following forms:
14147
14148 x where 'x' is a breakpoint number.
14149 x-y where 'x' and 'y' specify a breakpoint numbers range.
14150 x.y where 'x' is a breakpoint number and 'y' a location number.
14151 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14152 location number range.
14153 */
14154
14155 static void
14156 extract_bp_number_and_location (const std::string &arg,
14157 std::pair<int, int> &bp_num_range,
14158 std::pair<int, int> &bp_loc_range)
14159 {
14160 std::string::size_type dot = arg.find ('.');
14161
14162 if (dot != std::string::npos)
14163 {
14164 /* Handle 'x.y' and 'x.y-z' cases. */
14165
14166 if (arg.length () == dot + 1 || dot == 0)
14167 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
14168
14169 bp_num_range.first
14170 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14171 bp_num_range.second = bp_num_range.first;
14172
14173 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14174 arg, dot + 1);
14175 }
14176 else
14177 {
14178 /* Handle x and x-y cases. */
14179
14180 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
14181 bp_loc_range.first = 0;
14182 bp_loc_range.second = 0;
14183 }
14184 }
14185
14186 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14187 specifies whether to enable or disable. */
14188
14189 static void
14190 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14191 {
14192 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14193 if (loc != NULL)
14194 {
14195 if (loc->disabled_by_cond && enable)
14196 error (_("Breakpoint %d's condition is invalid at location %d, "
14197 "cannot enable."), bp_num, loc_num);
14198
14199 if (loc->enabled != enable)
14200 {
14201 loc->enabled = enable;
14202 mark_breakpoint_location_modified (loc);
14203 }
14204 if (target_supports_enable_disable_tracepoint ()
14205 && current_trace_status ()->running && loc->owner
14206 && is_tracepoint (loc->owner))
14207 target_disable_tracepoint (loc);
14208 }
14209 update_global_location_list (UGLL_DONT_INSERT);
14210
14211 gdb::observers::breakpoint_modified.notify (loc->owner);
14212 }
14213
14214 /* Enable or disable a range of breakpoint locations. BP_NUM is the
14215 number of the breakpoint, and BP_LOC_RANGE specifies the
14216 (inclusive) range of location numbers of that breakpoint to
14217 enable/disable. ENABLE specifies whether to enable or disable the
14218 location. */
14219
14220 static void
14221 enable_disable_breakpoint_location_range (int bp_num,
14222 std::pair<int, int> &bp_loc_range,
14223 bool enable)
14224 {
14225 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14226 enable_disable_bp_num_loc (bp_num, i, enable);
14227 }
14228
14229 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14230 If from_tty is nonzero, it prints a message to that effect,
14231 which ends with a period (no newline). */
14232
14233 void
14234 disable_breakpoint (struct breakpoint *bpt)
14235 {
14236 /* Never disable a watchpoint scope breakpoint; we want to
14237 hit them when we leave scope so we can delete both the
14238 watchpoint and its scope breakpoint at that time. */
14239 if (bpt->type == bp_watchpoint_scope)
14240 return;
14241
14242 bpt->enable_state = bp_disabled;
14243
14244 /* Mark breakpoint locations modified. */
14245 mark_breakpoint_modified (bpt);
14246
14247 if (target_supports_enable_disable_tracepoint ()
14248 && current_trace_status ()->running && is_tracepoint (bpt))
14249 {
14250 for (bp_location *location : bpt->locations ())
14251 target_disable_tracepoint (location);
14252 }
14253
14254 update_global_location_list (UGLL_DONT_INSERT);
14255
14256 gdb::observers::breakpoint_modified.notify (bpt);
14257 }
14258
14259 /* Enable or disable the breakpoint(s) or breakpoint location(s)
14260 specified in ARGS. ARGS may be in any of the formats handled by
14261 extract_bp_number_and_location. ENABLE specifies whether to enable
14262 or disable the breakpoints/locations. */
14263
14264 static void
14265 enable_disable_command (const char *args, int from_tty, bool enable)
14266 {
14267 if (args == 0)
14268 {
14269 for (breakpoint *bpt : all_breakpoints ())
14270 if (user_breakpoint_p (bpt))
14271 {
14272 if (enable)
14273 enable_breakpoint (bpt);
14274 else
14275 disable_breakpoint (bpt);
14276 }
14277 }
14278 else
14279 {
14280 std::string num = extract_arg (&args);
14281
14282 while (!num.empty ())
14283 {
14284 std::pair<int, int> bp_num_range, bp_loc_range;
14285
14286 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14287
14288 if (bp_loc_range.first == bp_loc_range.second
14289 && bp_loc_range.first == 0)
14290 {
14291 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14292 map_breakpoint_number_range (bp_num_range,
14293 enable
14294 ? enable_breakpoint
14295 : disable_breakpoint);
14296 }
14297 else
14298 {
14299 /* Handle breakpoint ids with formats 'x.y' or
14300 'x.y-z'. */
14301 enable_disable_breakpoint_location_range
14302 (bp_num_range.first, bp_loc_range, enable);
14303 }
14304 num = extract_arg (&args);
14305 }
14306 }
14307 }
14308
14309 /* The disable command disables the specified breakpoints/locations
14310 (or all defined breakpoints) so they're no longer effective in
14311 stopping the inferior. ARGS may be in any of the forms defined in
14312 extract_bp_number_and_location. */
14313
14314 static void
14315 disable_command (const char *args, int from_tty)
14316 {
14317 enable_disable_command (args, from_tty, false);
14318 }
14319
14320 static void
14321 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14322 int count)
14323 {
14324 int target_resources_ok;
14325
14326 if (bpt->type == bp_hardware_breakpoint)
14327 {
14328 int i;
14329 i = hw_breakpoint_used_count ();
14330 target_resources_ok =
14331 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14332 i + 1, 0);
14333 if (target_resources_ok == 0)
14334 error (_("No hardware breakpoint support in the target."));
14335 else if (target_resources_ok < 0)
14336 error (_("Hardware breakpoints used exceeds limit."));
14337 }
14338
14339 if (is_watchpoint (bpt))
14340 {
14341 /* Initialize it just to avoid a GCC false warning. */
14342 enum enable_state orig_enable_state = bp_disabled;
14343
14344 try
14345 {
14346 struct watchpoint *w = (struct watchpoint *) bpt;
14347
14348 orig_enable_state = bpt->enable_state;
14349 bpt->enable_state = bp_enabled;
14350 update_watchpoint (w, 1 /* reparse */);
14351 }
14352 catch (const gdb_exception &e)
14353 {
14354 bpt->enable_state = orig_enable_state;
14355 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14356 bpt->number);
14357 return;
14358 }
14359 }
14360
14361 bpt->enable_state = bp_enabled;
14362
14363 /* Mark breakpoint locations modified. */
14364 mark_breakpoint_modified (bpt);
14365
14366 if (target_supports_enable_disable_tracepoint ()
14367 && current_trace_status ()->running && is_tracepoint (bpt))
14368 {
14369 for (bp_location *location : bpt->locations ())
14370 target_enable_tracepoint (location);
14371 }
14372
14373 bpt->disposition = disposition;
14374 bpt->enable_count = count;
14375 update_global_location_list (UGLL_MAY_INSERT);
14376
14377 gdb::observers::breakpoint_modified.notify (bpt);
14378 }
14379
14380
14381 void
14382 enable_breakpoint (struct breakpoint *bpt)
14383 {
14384 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14385 }
14386
14387 /* The enable command enables the specified breakpoints/locations (or
14388 all defined breakpoints) so they once again become (or continue to
14389 be) effective in stopping the inferior. ARGS may be in any of the
14390 forms defined in extract_bp_number_and_location. */
14391
14392 static void
14393 enable_command (const char *args, int from_tty)
14394 {
14395 enable_disable_command (args, from_tty, true);
14396 }
14397
14398 static void
14399 enable_once_command (const char *args, int from_tty)
14400 {
14401 map_breakpoint_numbers
14402 (args, [&] (breakpoint *b)
14403 {
14404 iterate_over_related_breakpoints
14405 (b, [&] (breakpoint *bpt)
14406 {
14407 enable_breakpoint_disp (bpt, disp_disable, 1);
14408 });
14409 });
14410 }
14411
14412 static void
14413 enable_count_command (const char *args, int from_tty)
14414 {
14415 int count;
14416
14417 if (args == NULL)
14418 error_no_arg (_("hit count"));
14419
14420 count = get_number (&args);
14421
14422 map_breakpoint_numbers
14423 (args, [&] (breakpoint *b)
14424 {
14425 iterate_over_related_breakpoints
14426 (b, [&] (breakpoint *bpt)
14427 {
14428 enable_breakpoint_disp (bpt, disp_disable, count);
14429 });
14430 });
14431 }
14432
14433 static void
14434 enable_delete_command (const char *args, int from_tty)
14435 {
14436 map_breakpoint_numbers
14437 (args, [&] (breakpoint *b)
14438 {
14439 iterate_over_related_breakpoints
14440 (b, [&] (breakpoint *bpt)
14441 {
14442 enable_breakpoint_disp (bpt, disp_del, 1);
14443 });
14444 });
14445 }
14446 \f
14447 /* Invalidate last known value of any hardware watchpoint if
14448 the memory which that value represents has been written to by
14449 GDB itself. */
14450
14451 static void
14452 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14453 CORE_ADDR addr, ssize_t len,
14454 const bfd_byte *data)
14455 {
14456 for (breakpoint *bp : all_breakpoints ())
14457 if (bp->enable_state == bp_enabled
14458 && bp->type == bp_hardware_watchpoint)
14459 {
14460 struct watchpoint *wp = (struct watchpoint *) bp;
14461
14462 if (wp->val_valid && wp->val != nullptr)
14463 {
14464 for (bp_location *loc : bp->locations ())
14465 if (loc->loc_type == bp_loc_hardware_watchpoint
14466 && loc->address + loc->length > addr
14467 && addr + len > loc->address)
14468 {
14469 wp->val = NULL;
14470 wp->val_valid = false;
14471 }
14472 }
14473 }
14474 }
14475
14476 /* Create and insert a breakpoint for software single step. */
14477
14478 void
14479 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14480 const address_space *aspace,
14481 CORE_ADDR next_pc)
14482 {
14483 struct thread_info *tp = inferior_thread ();
14484 struct symtab_and_line sal;
14485 CORE_ADDR pc = next_pc;
14486
14487 if (tp->control.single_step_breakpoints == NULL)
14488 {
14489 tp->control.single_step_breakpoints
14490 = new_single_step_breakpoint (tp->global_num, gdbarch);
14491 }
14492
14493 sal = find_pc_line (pc, 0);
14494 sal.pc = pc;
14495 sal.section = find_pc_overlay (pc);
14496 sal.explicit_pc = 1;
14497 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14498
14499 update_global_location_list (UGLL_INSERT);
14500 }
14501
14502 /* Insert single step breakpoints according to the current state. */
14503
14504 int
14505 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14506 {
14507 struct regcache *regcache = get_current_regcache ();
14508 std::vector<CORE_ADDR> next_pcs;
14509
14510 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14511
14512 if (!next_pcs.empty ())
14513 {
14514 struct frame_info *frame = get_current_frame ();
14515 const address_space *aspace = get_frame_address_space (frame);
14516
14517 for (CORE_ADDR pc : next_pcs)
14518 insert_single_step_breakpoint (gdbarch, aspace, pc);
14519
14520 return 1;
14521 }
14522 else
14523 return 0;
14524 }
14525
14526 /* See breakpoint.h. */
14527
14528 int
14529 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14530 const address_space *aspace,
14531 CORE_ADDR pc)
14532 {
14533 for (bp_location *loc : bp->locations ())
14534 if (loc->inserted
14535 && breakpoint_location_address_match (loc, aspace, pc))
14536 return 1;
14537
14538 return 0;
14539 }
14540
14541 /* Check whether a software single-step breakpoint is inserted at
14542 PC. */
14543
14544 int
14545 single_step_breakpoint_inserted_here_p (const address_space *aspace,
14546 CORE_ADDR pc)
14547 {
14548 for (breakpoint *bpt : all_breakpoints ())
14549 {
14550 if (bpt->type == bp_single_step
14551 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14552 return 1;
14553 }
14554 return 0;
14555 }
14556
14557 /* Tracepoint-specific operations. */
14558
14559 /* Set tracepoint count to NUM. */
14560 static void
14561 set_tracepoint_count (int num)
14562 {
14563 tracepoint_count = num;
14564 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14565 }
14566
14567 static void
14568 trace_command (const char *arg, int from_tty)
14569 {
14570 event_location_up location = string_to_event_location (&arg,
14571 current_language);
14572 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
14573 (location.get (), true /* is_tracepoint */);
14574
14575 create_breakpoint (get_current_arch (),
14576 location.get (),
14577 NULL, 0, arg, false, 1 /* parse arg */,
14578 0 /* tempflag */,
14579 bp_tracepoint /* type_wanted */,
14580 0 /* Ignore count */,
14581 pending_break_support,
14582 ops,
14583 from_tty,
14584 1 /* enabled */,
14585 0 /* internal */, 0);
14586 }
14587
14588 static void
14589 ftrace_command (const char *arg, int from_tty)
14590 {
14591 event_location_up location = string_to_event_location (&arg,
14592 current_language);
14593 create_breakpoint (get_current_arch (),
14594 location.get (),
14595 NULL, 0, arg, false, 1 /* parse arg */,
14596 0 /* tempflag */,
14597 bp_fast_tracepoint /* type_wanted */,
14598 0 /* Ignore count */,
14599 pending_break_support,
14600 &tracepoint_breakpoint_ops,
14601 from_tty,
14602 1 /* enabled */,
14603 0 /* internal */, 0);
14604 }
14605
14606 /* strace command implementation. Creates a static tracepoint. */
14607
14608 static void
14609 strace_command (const char *arg, int from_tty)
14610 {
14611 struct breakpoint_ops *ops;
14612 event_location_up location;
14613
14614 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14615 or with a normal static tracepoint. */
14616 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14617 {
14618 ops = &strace_marker_breakpoint_ops;
14619 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
14620 }
14621 else
14622 {
14623 ops = &tracepoint_breakpoint_ops;
14624 location = string_to_event_location (&arg, current_language);
14625 }
14626
14627 create_breakpoint (get_current_arch (),
14628 location.get (),
14629 NULL, 0, arg, false, 1 /* parse arg */,
14630 0 /* tempflag */,
14631 bp_static_tracepoint /* type_wanted */,
14632 0 /* Ignore count */,
14633 pending_break_support,
14634 ops,
14635 from_tty,
14636 1 /* enabled */,
14637 0 /* internal */, 0);
14638 }
14639
14640 /* Set up a fake reader function that gets command lines from a linked
14641 list that was acquired during tracepoint uploading. */
14642
14643 static struct uploaded_tp *this_utp;
14644 static int next_cmd;
14645
14646 static char *
14647 read_uploaded_action (void)
14648 {
14649 char *rslt = nullptr;
14650
14651 if (next_cmd < this_utp->cmd_strings.size ())
14652 {
14653 rslt = this_utp->cmd_strings[next_cmd].get ();
14654 next_cmd++;
14655 }
14656
14657 return rslt;
14658 }
14659
14660 /* Given information about a tracepoint as recorded on a target (which
14661 can be either a live system or a trace file), attempt to create an
14662 equivalent GDB tracepoint. This is not a reliable process, since
14663 the target does not necessarily have all the information used when
14664 the tracepoint was originally defined. */
14665
14666 struct tracepoint *
14667 create_tracepoint_from_upload (struct uploaded_tp *utp)
14668 {
14669 const char *addr_str;
14670 char small_buf[100];
14671 struct tracepoint *tp;
14672
14673 if (utp->at_string)
14674 addr_str = utp->at_string.get ();
14675 else
14676 {
14677 /* In the absence of a source location, fall back to raw
14678 address. Since there is no way to confirm that the address
14679 means the same thing as when the trace was started, warn the
14680 user. */
14681 warning (_("Uploaded tracepoint %d has no "
14682 "source location, using raw address"),
14683 utp->number);
14684 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14685 addr_str = small_buf;
14686 }
14687
14688 /* There's not much we can do with a sequence of bytecodes. */
14689 if (utp->cond && !utp->cond_string)
14690 warning (_("Uploaded tracepoint %d condition "
14691 "has no source form, ignoring it"),
14692 utp->number);
14693
14694 event_location_up location = string_to_event_location (&addr_str,
14695 current_language);
14696 if (!create_breakpoint (get_current_arch (),
14697 location.get (),
14698 utp->cond_string.get (), -1, addr_str,
14699 false /* force_condition */,
14700 0 /* parse cond/thread */,
14701 0 /* tempflag */,
14702 utp->type /* type_wanted */,
14703 0 /* Ignore count */,
14704 pending_break_support,
14705 &tracepoint_breakpoint_ops,
14706 0 /* from_tty */,
14707 utp->enabled /* enabled */,
14708 0 /* internal */,
14709 CREATE_BREAKPOINT_FLAGS_INSERTED))
14710 return NULL;
14711
14712 /* Get the tracepoint we just created. */
14713 tp = get_tracepoint (tracepoint_count);
14714 gdb_assert (tp != NULL);
14715
14716 if (utp->pass > 0)
14717 {
14718 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14719 tp->number);
14720
14721 trace_pass_command (small_buf, 0);
14722 }
14723
14724 /* If we have uploaded versions of the original commands, set up a
14725 special-purpose "reader" function and call the usual command line
14726 reader, then pass the result to the breakpoint command-setting
14727 function. */
14728 if (!utp->cmd_strings.empty ())
14729 {
14730 counted_command_line cmd_list;
14731
14732 this_utp = utp;
14733 next_cmd = 0;
14734
14735 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
14736
14737 breakpoint_set_commands (tp, std::move (cmd_list));
14738 }
14739 else if (!utp->actions.empty ()
14740 || !utp->step_actions.empty ())
14741 warning (_("Uploaded tracepoint %d actions "
14742 "have no source form, ignoring them"),
14743 utp->number);
14744
14745 /* Copy any status information that might be available. */
14746 tp->hit_count = utp->hit_count;
14747 tp->traceframe_usage = utp->traceframe_usage;
14748
14749 return tp;
14750 }
14751
14752 /* Print information on tracepoint number TPNUM_EXP, or all if
14753 omitted. */
14754
14755 static void
14756 info_tracepoints_command (const char *args, int from_tty)
14757 {
14758 struct ui_out *uiout = current_uiout;
14759 int num_printed;
14760
14761 num_printed = breakpoint_1 (args, false, is_tracepoint);
14762
14763 if (num_printed == 0)
14764 {
14765 if (args == NULL || *args == '\0')
14766 uiout->message ("No tracepoints.\n");
14767 else
14768 uiout->message ("No tracepoint matching '%s'.\n", args);
14769 }
14770
14771 default_collect_info ();
14772 }
14773
14774 /* The 'enable trace' command enables tracepoints.
14775 Not supported by all targets. */
14776 static void
14777 enable_trace_command (const char *args, int from_tty)
14778 {
14779 enable_command (args, from_tty);
14780 }
14781
14782 /* The 'disable trace' command disables tracepoints.
14783 Not supported by all targets. */
14784 static void
14785 disable_trace_command (const char *args, int from_tty)
14786 {
14787 disable_command (args, from_tty);
14788 }
14789
14790 /* Remove a tracepoint (or all if no argument). */
14791 static void
14792 delete_trace_command (const char *arg, int from_tty)
14793 {
14794 dont_repeat ();
14795
14796 if (arg == 0)
14797 {
14798 int breaks_to_delete = 0;
14799
14800 /* Delete all breakpoints if no argument.
14801 Do not delete internal or call-dummy breakpoints, these
14802 have to be deleted with an explicit breakpoint number
14803 argument. */
14804 for (breakpoint *tp : all_tracepoints ())
14805 if (is_tracepoint (tp) && user_breakpoint_p (tp))
14806 {
14807 breaks_to_delete = 1;
14808 break;
14809 }
14810
14811 /* Ask user only if there are some breakpoints to delete. */
14812 if (!from_tty
14813 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14814 {
14815 for (breakpoint *b : all_breakpoints_safe ())
14816 if (is_tracepoint (b) && user_breakpoint_p (b))
14817 delete_breakpoint (b);
14818 }
14819 }
14820 else
14821 map_breakpoint_numbers
14822 (arg, [&] (breakpoint *br)
14823 {
14824 iterate_over_related_breakpoints (br, delete_breakpoint);
14825 });
14826 }
14827
14828 /* Helper function for trace_pass_command. */
14829
14830 static void
14831 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14832 {
14833 tp->pass_count = count;
14834 gdb::observers::breakpoint_modified.notify (tp);
14835 if (from_tty)
14836 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
14837 tp->number, count);
14838 }
14839
14840 /* Set passcount for tracepoint.
14841
14842 First command argument is passcount, second is tracepoint number.
14843 If tracepoint number omitted, apply to most recently defined.
14844 Also accepts special argument "all". */
14845
14846 static void
14847 trace_pass_command (const char *args, int from_tty)
14848 {
14849 struct tracepoint *t1;
14850 ULONGEST count;
14851
14852 if (args == 0 || *args == 0)
14853 error (_("passcount command requires an "
14854 "argument (count + optional TP num)"));
14855
14856 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
14857
14858 args = skip_spaces (args);
14859 if (*args && strncasecmp (args, "all", 3) == 0)
14860 {
14861 args += 3; /* Skip special argument "all". */
14862 if (*args)
14863 error (_("Junk at end of arguments."));
14864
14865 for (breakpoint *b : all_tracepoints ())
14866 {
14867 t1 = (struct tracepoint *) b;
14868 trace_pass_set_count (t1, count, from_tty);
14869 }
14870 }
14871 else if (*args == '\0')
14872 {
14873 t1 = get_tracepoint_by_number (&args, NULL);
14874 if (t1)
14875 trace_pass_set_count (t1, count, from_tty);
14876 }
14877 else
14878 {
14879 number_or_range_parser parser (args);
14880 while (!parser.finished ())
14881 {
14882 t1 = get_tracepoint_by_number (&args, &parser);
14883 if (t1)
14884 trace_pass_set_count (t1, count, from_tty);
14885 }
14886 }
14887 }
14888
14889 struct tracepoint *
14890 get_tracepoint (int num)
14891 {
14892 for (breakpoint *t : all_tracepoints ())
14893 if (t->number == num)
14894 return (struct tracepoint *) t;
14895
14896 return NULL;
14897 }
14898
14899 /* Find the tracepoint with the given target-side number (which may be
14900 different from the tracepoint number after disconnecting and
14901 reconnecting). */
14902
14903 struct tracepoint *
14904 get_tracepoint_by_number_on_target (int num)
14905 {
14906 for (breakpoint *b : all_tracepoints ())
14907 {
14908 struct tracepoint *t = (struct tracepoint *) b;
14909
14910 if (t->number_on_target == num)
14911 return t;
14912 }
14913
14914 return NULL;
14915 }
14916
14917 /* Utility: parse a tracepoint number and look it up in the list.
14918 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
14919 If the argument is missing, the most recent tracepoint
14920 (tracepoint_count) is returned. */
14921
14922 struct tracepoint *
14923 get_tracepoint_by_number (const char **arg,
14924 number_or_range_parser *parser)
14925 {
14926 int tpnum;
14927 const char *instring = arg == NULL ? NULL : *arg;
14928
14929 if (parser != NULL)
14930 {
14931 gdb_assert (!parser->finished ());
14932 tpnum = parser->get_number ();
14933 }
14934 else if (arg == NULL || *arg == NULL || ! **arg)
14935 tpnum = tracepoint_count;
14936 else
14937 tpnum = get_number (arg);
14938
14939 if (tpnum <= 0)
14940 {
14941 if (instring && *instring)
14942 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
14943 instring);
14944 else
14945 printf_filtered (_("No previous tracepoint\n"));
14946 return NULL;
14947 }
14948
14949 for (breakpoint *t : all_tracepoints ())
14950 if (t->number == tpnum)
14951 {
14952 return (struct tracepoint *) t;
14953 }
14954
14955 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
14956 return NULL;
14957 }
14958
14959 void
14960 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
14961 {
14962 if (b->thread != -1)
14963 fprintf_unfiltered (fp, " thread %d", b->thread);
14964
14965 if (b->task != 0)
14966 fprintf_unfiltered (fp, " task %d", b->task);
14967
14968 fprintf_unfiltered (fp, "\n");
14969 }
14970
14971 /* Save information on user settable breakpoints (watchpoints, etc) to
14972 a new script file named FILENAME. If FILTER is non-NULL, call it
14973 on each breakpoint and only include the ones for which it returns
14974 true. */
14975
14976 static void
14977 save_breakpoints (const char *filename, int from_tty,
14978 bool (*filter) (const struct breakpoint *))
14979 {
14980 int any = 0;
14981 int extra_trace_bits = 0;
14982
14983 if (filename == 0 || *filename == 0)
14984 error (_("Argument required (file name in which to save)"));
14985
14986 /* See if we have anything to save. */
14987 for (breakpoint *tp : all_breakpoints ())
14988 {
14989 /* Skip internal and momentary breakpoints. */
14990 if (!user_breakpoint_p (tp))
14991 continue;
14992
14993 /* If we have a filter, only save the breakpoints it accepts. */
14994 if (filter && !filter (tp))
14995 continue;
14996
14997 any = 1;
14998
14999 if (is_tracepoint (tp))
15000 {
15001 extra_trace_bits = 1;
15002
15003 /* We can stop searching. */
15004 break;
15005 }
15006 }
15007
15008 if (!any)
15009 {
15010 warning (_("Nothing to save."));
15011 return;
15012 }
15013
15014 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
15015
15016 stdio_file fp;
15017
15018 if (!fp.open (expanded_filename.get (), "w"))
15019 error (_("Unable to open file '%s' for saving (%s)"),
15020 expanded_filename.get (), safe_strerror (errno));
15021
15022 if (extra_trace_bits)
15023 save_trace_state_variables (&fp);
15024
15025 for (breakpoint *tp : all_breakpoints ())
15026 {
15027 /* Skip internal and momentary breakpoints. */
15028 if (!user_breakpoint_p (tp))
15029 continue;
15030
15031 /* If we have a filter, only save the breakpoints it accepts. */
15032 if (filter && !filter (tp))
15033 continue;
15034
15035 tp->ops->print_recreate (tp, &fp);
15036
15037 /* Note, we can't rely on tp->number for anything, as we can't
15038 assume the recreated breakpoint numbers will match. Use $bpnum
15039 instead. */
15040
15041 if (tp->cond_string)
15042 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15043
15044 if (tp->ignore_count)
15045 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15046
15047 if (tp->type != bp_dprintf && tp->commands)
15048 {
15049 fp.puts (" commands\n");
15050
15051 current_uiout->redirect (&fp);
15052 try
15053 {
15054 print_command_lines (current_uiout, tp->commands.get (), 2);
15055 }
15056 catch (const gdb_exception &ex)
15057 {
15058 current_uiout->redirect (NULL);
15059 throw;
15060 }
15061
15062 current_uiout->redirect (NULL);
15063 fp.puts (" end\n");
15064 }
15065
15066 if (tp->enable_state == bp_disabled)
15067 fp.puts ("disable $bpnum\n");
15068
15069 /* If this is a multi-location breakpoint, check if the locations
15070 should be individually disabled. Watchpoint locations are
15071 special, and not user visible. */
15072 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15073 {
15074 int n = 1;
15075
15076 for (bp_location *loc : tp->locations ())
15077 {
15078 if (!loc->enabled)
15079 fp.printf ("disable $bpnum.%d\n", n);
15080
15081 n++;
15082 }
15083 }
15084 }
15085
15086 if (extra_trace_bits && *default_collect)
15087 fp.printf ("set default-collect %s\n", default_collect);
15088
15089 if (from_tty)
15090 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
15091 }
15092
15093 /* The `save breakpoints' command. */
15094
15095 static void
15096 save_breakpoints_command (const char *args, int from_tty)
15097 {
15098 save_breakpoints (args, from_tty, NULL);
15099 }
15100
15101 /* The `save tracepoints' command. */
15102
15103 static void
15104 save_tracepoints_command (const char *args, int from_tty)
15105 {
15106 save_breakpoints (args, from_tty, is_tracepoint);
15107 }
15108
15109 \f
15110 /* This help string is used to consolidate all the help string for specifying
15111 locations used by several commands. */
15112
15113 #define LOCATION_HELP_STRING \
15114 "Linespecs are colon-separated lists of location parameters, such as\n\
15115 source filename, function name, label name, and line number.\n\
15116 Example: To specify the start of a label named \"the_top\" in the\n\
15117 function \"fact\" in the file \"factorial.c\", use\n\
15118 \"factorial.c:fact:the_top\".\n\
15119 \n\
15120 Address locations begin with \"*\" and specify an exact address in the\n\
15121 program. Example: To specify the fourth byte past the start function\n\
15122 \"main\", use \"*main + 4\".\n\
15123 \n\
15124 Explicit locations are similar to linespecs but use an option/argument\n\
15125 syntax to specify location parameters.\n\
15126 Example: To specify the start of the label named \"the_top\" in the\n\
15127 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15128 -function fact -label the_top\".\n\
15129 \n\
15130 By default, a specified function is matched against the program's\n\
15131 functions in all scopes. For C++, this means in all namespaces and\n\
15132 classes. For Ada, this means in all packages. E.g., in C++,\n\
15133 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15134 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
15135 specified name as a complete fully-qualified name instead."
15136
15137 /* This help string is used for the break, hbreak, tbreak and thbreak
15138 commands. It is defined as a macro to prevent duplication.
15139 COMMAND should be a string constant containing the name of the
15140 command. */
15141
15142 #define BREAK_ARGS_HELP(command) \
15143 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
15144 \t[-force-condition] [if CONDITION]\n\
15145 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15146 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15147 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15148 `-probe-dtrace' (for a DTrace probe).\n\
15149 LOCATION may be a linespec, address, or explicit location as described\n\
15150 below.\n\
15151 \n\
15152 With no LOCATION, uses current execution address of the selected\n\
15153 stack frame. This is useful for breaking on return to a stack frame.\n\
15154 \n\
15155 THREADNUM is the number from \"info threads\".\n\
15156 CONDITION is a boolean expression.\n\
15157 \n\
15158 With the \"-force-condition\" flag, the condition is defined even when\n\
15159 it is invalid for all current locations.\n\
15160 \n" LOCATION_HELP_STRING "\n\n\
15161 Multiple breakpoints at one place are permitted, and useful if their\n\
15162 conditions are different.\n\
15163 \n\
15164 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15165
15166 /* List of subcommands for "catch". */
15167 static struct cmd_list_element *catch_cmdlist;
15168
15169 /* List of subcommands for "tcatch". */
15170 static struct cmd_list_element *tcatch_cmdlist;
15171
15172 void
15173 add_catch_command (const char *name, const char *docstring,
15174 cmd_const_sfunc_ftype *sfunc,
15175 completer_ftype *completer,
15176 void *user_data_catch,
15177 void *user_data_tcatch)
15178 {
15179 struct cmd_list_element *command;
15180
15181 command = add_cmd (name, class_breakpoint, docstring,
15182 &catch_cmdlist);
15183 set_cmd_sfunc (command, sfunc);
15184 set_cmd_context (command, user_data_catch);
15185 set_cmd_completer (command, completer);
15186
15187 command = add_cmd (name, class_breakpoint, docstring,
15188 &tcatch_cmdlist);
15189 set_cmd_sfunc (command, sfunc);
15190 set_cmd_context (command, user_data_tcatch);
15191 set_cmd_completer (command, completer);
15192 }
15193
15194 struct breakpoint *
15195 iterate_over_breakpoints (gdb::function_view<bool (breakpoint *)> callback)
15196 {
15197 for (breakpoint *b : all_breakpoints_safe ())
15198 if (callback (b))
15199 return b;
15200
15201 return NULL;
15202 }
15203
15204 /* Zero if any of the breakpoint's locations could be a location where
15205 functions have been inlined, nonzero otherwise. */
15206
15207 static int
15208 is_non_inline_function (struct breakpoint *b)
15209 {
15210 /* The shared library event breakpoint is set on the address of a
15211 non-inline function. */
15212 if (b->type == bp_shlib_event)
15213 return 1;
15214
15215 return 0;
15216 }
15217
15218 /* Nonzero if the specified PC cannot be a location where functions
15219 have been inlined. */
15220
15221 int
15222 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
15223 const struct target_waitstatus *ws)
15224 {
15225 for (breakpoint *b : all_breakpoints ())
15226 {
15227 if (!is_non_inline_function (b))
15228 continue;
15229
15230 for (bp_location *bl : b->locations ())
15231 {
15232 if (!bl->shlib_disabled
15233 && bpstat_check_location (bl, aspace, pc, ws))
15234 return 1;
15235 }
15236 }
15237
15238 return 0;
15239 }
15240
15241 /* Remove any references to OBJFILE which is going to be freed. */
15242
15243 void
15244 breakpoint_free_objfile (struct objfile *objfile)
15245 {
15246 for (bp_location *loc : all_bp_locations ())
15247 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15248 loc->symtab = NULL;
15249 }
15250
15251 void
15252 initialize_breakpoint_ops (void)
15253 {
15254 static int initialized = 0;
15255
15256 struct breakpoint_ops *ops;
15257
15258 if (initialized)
15259 return;
15260 initialized = 1;
15261
15262 /* The breakpoint_ops structure to be inherit by all kinds of
15263 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15264 internal and momentary breakpoints, etc.). */
15265 ops = &bkpt_base_breakpoint_ops;
15266 *ops = base_breakpoint_ops;
15267 ops->re_set = bkpt_re_set;
15268 ops->insert_location = bkpt_insert_location;
15269 ops->remove_location = bkpt_remove_location;
15270 ops->breakpoint_hit = bkpt_breakpoint_hit;
15271 ops->create_sals_from_location = bkpt_create_sals_from_location;
15272 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15273 ops->decode_location = bkpt_decode_location;
15274
15275 /* The breakpoint_ops structure to be used in regular breakpoints. */
15276 ops = &bkpt_breakpoint_ops;
15277 *ops = bkpt_base_breakpoint_ops;
15278 ops->re_set = bkpt_re_set;
15279 ops->resources_needed = bkpt_resources_needed;
15280 ops->print_it = bkpt_print_it;
15281 ops->print_mention = bkpt_print_mention;
15282 ops->print_recreate = bkpt_print_recreate;
15283
15284 /* Ranged breakpoints. */
15285 ops = &ranged_breakpoint_ops;
15286 *ops = bkpt_breakpoint_ops;
15287 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15288 ops->resources_needed = resources_needed_ranged_breakpoint;
15289 ops->print_it = print_it_ranged_breakpoint;
15290 ops->print_one = print_one_ranged_breakpoint;
15291 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15292 ops->print_mention = print_mention_ranged_breakpoint;
15293 ops->print_recreate = print_recreate_ranged_breakpoint;
15294
15295 /* Internal breakpoints. */
15296 ops = &internal_breakpoint_ops;
15297 *ops = bkpt_base_breakpoint_ops;
15298 ops->re_set = internal_bkpt_re_set;
15299 ops->check_status = internal_bkpt_check_status;
15300 ops->print_it = internal_bkpt_print_it;
15301 ops->print_mention = internal_bkpt_print_mention;
15302
15303 /* Momentary breakpoints. */
15304 ops = &momentary_breakpoint_ops;
15305 *ops = bkpt_base_breakpoint_ops;
15306 ops->re_set = momentary_bkpt_re_set;
15307 ops->check_status = momentary_bkpt_check_status;
15308 ops->print_it = momentary_bkpt_print_it;
15309 ops->print_mention = momentary_bkpt_print_mention;
15310
15311 /* Probe breakpoints. */
15312 ops = &bkpt_probe_breakpoint_ops;
15313 *ops = bkpt_breakpoint_ops;
15314 ops->insert_location = bkpt_probe_insert_location;
15315 ops->remove_location = bkpt_probe_remove_location;
15316 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15317 ops->decode_location = bkpt_probe_decode_location;
15318
15319 /* Watchpoints. */
15320 ops = &watchpoint_breakpoint_ops;
15321 *ops = base_breakpoint_ops;
15322 ops->re_set = re_set_watchpoint;
15323 ops->insert_location = insert_watchpoint;
15324 ops->remove_location = remove_watchpoint;
15325 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15326 ops->check_status = check_status_watchpoint;
15327 ops->resources_needed = resources_needed_watchpoint;
15328 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15329 ops->print_it = print_it_watchpoint;
15330 ops->print_mention = print_mention_watchpoint;
15331 ops->print_recreate = print_recreate_watchpoint;
15332 ops->explains_signal = explains_signal_watchpoint;
15333
15334 /* Masked watchpoints. */
15335 ops = &masked_watchpoint_breakpoint_ops;
15336 *ops = watchpoint_breakpoint_ops;
15337 ops->insert_location = insert_masked_watchpoint;
15338 ops->remove_location = remove_masked_watchpoint;
15339 ops->resources_needed = resources_needed_masked_watchpoint;
15340 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15341 ops->print_it = print_it_masked_watchpoint;
15342 ops->print_one_detail = print_one_detail_masked_watchpoint;
15343 ops->print_mention = print_mention_masked_watchpoint;
15344 ops->print_recreate = print_recreate_masked_watchpoint;
15345
15346 /* Tracepoints. */
15347 ops = &tracepoint_breakpoint_ops;
15348 *ops = base_breakpoint_ops;
15349 ops->re_set = tracepoint_re_set;
15350 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15351 ops->print_one_detail = tracepoint_print_one_detail;
15352 ops->print_mention = tracepoint_print_mention;
15353 ops->print_recreate = tracepoint_print_recreate;
15354 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15355 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15356 ops->decode_location = tracepoint_decode_location;
15357
15358 /* Probe tracepoints. */
15359 ops = &tracepoint_probe_breakpoint_ops;
15360 *ops = tracepoint_breakpoint_ops;
15361 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15362 ops->decode_location = tracepoint_probe_decode_location;
15363
15364 /* Static tracepoints with marker (`-m'). */
15365 ops = &strace_marker_breakpoint_ops;
15366 *ops = tracepoint_breakpoint_ops;
15367 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15368 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15369 ops->decode_location = strace_marker_decode_location;
15370
15371 /* Fork catchpoints. */
15372 ops = &catch_fork_breakpoint_ops;
15373 *ops = base_breakpoint_ops;
15374 ops->insert_location = insert_catch_fork;
15375 ops->remove_location = remove_catch_fork;
15376 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15377 ops->print_it = print_it_catch_fork;
15378 ops->print_one = print_one_catch_fork;
15379 ops->print_mention = print_mention_catch_fork;
15380 ops->print_recreate = print_recreate_catch_fork;
15381
15382 /* Vfork catchpoints. */
15383 ops = &catch_vfork_breakpoint_ops;
15384 *ops = base_breakpoint_ops;
15385 ops->insert_location = insert_catch_vfork;
15386 ops->remove_location = remove_catch_vfork;
15387 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15388 ops->print_it = print_it_catch_vfork;
15389 ops->print_one = print_one_catch_vfork;
15390 ops->print_mention = print_mention_catch_vfork;
15391 ops->print_recreate = print_recreate_catch_vfork;
15392
15393 /* Exec catchpoints. */
15394 ops = &catch_exec_breakpoint_ops;
15395 *ops = base_breakpoint_ops;
15396 ops->insert_location = insert_catch_exec;
15397 ops->remove_location = remove_catch_exec;
15398 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15399 ops->print_it = print_it_catch_exec;
15400 ops->print_one = print_one_catch_exec;
15401 ops->print_mention = print_mention_catch_exec;
15402 ops->print_recreate = print_recreate_catch_exec;
15403
15404 /* Solib-related catchpoints. */
15405 ops = &catch_solib_breakpoint_ops;
15406 *ops = base_breakpoint_ops;
15407 ops->insert_location = insert_catch_solib;
15408 ops->remove_location = remove_catch_solib;
15409 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15410 ops->check_status = check_status_catch_solib;
15411 ops->print_it = print_it_catch_solib;
15412 ops->print_one = print_one_catch_solib;
15413 ops->print_mention = print_mention_catch_solib;
15414 ops->print_recreate = print_recreate_catch_solib;
15415
15416 ops = &dprintf_breakpoint_ops;
15417 *ops = bkpt_base_breakpoint_ops;
15418 ops->re_set = dprintf_re_set;
15419 ops->resources_needed = bkpt_resources_needed;
15420 ops->print_it = bkpt_print_it;
15421 ops->print_mention = bkpt_print_mention;
15422 ops->print_recreate = dprintf_print_recreate;
15423 ops->after_condition_true = dprintf_after_condition_true;
15424 ops->breakpoint_hit = dprintf_breakpoint_hit;
15425 }
15426
15427 /* Chain containing all defined "enable breakpoint" subcommands. */
15428
15429 static struct cmd_list_element *enablebreaklist = NULL;
15430
15431 /* See breakpoint.h. */
15432
15433 cmd_list_element *commands_cmd_element = nullptr;
15434
15435 void _initialize_breakpoint ();
15436 void
15437 _initialize_breakpoint ()
15438 {
15439 struct cmd_list_element *c;
15440
15441 initialize_breakpoint_ops ();
15442
15443 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
15444 "breakpoint");
15445 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
15446 "breakpoint");
15447 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
15448 "breakpoint");
15449
15450 breakpoint_chain = 0;
15451 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15452 before a breakpoint is set. */
15453 breakpoint_count = 0;
15454
15455 tracepoint_count = 0;
15456
15457 add_com ("ignore", class_breakpoint, ignore_command, _("\
15458 Set ignore-count of breakpoint number N to COUNT.\n\
15459 Usage is `ignore N COUNT'."));
15460
15461 commands_cmd_element = add_com ("commands", class_breakpoint,
15462 commands_command, _("\
15463 Set commands to be executed when the given breakpoints are hit.\n\
15464 Give a space-separated breakpoint list as argument after \"commands\".\n\
15465 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15466 (e.g. `5-7').\n\
15467 With no argument, the targeted breakpoint is the last one set.\n\
15468 The commands themselves follow starting on the next line.\n\
15469 Type a line containing \"end\" to indicate the end of them.\n\
15470 Give \"silent\" as the first line to make the breakpoint silent;\n\
15471 then no output is printed when it is hit, except what the commands print."));
15472
15473 const auto cc_opts = make_condition_command_options_def_group (nullptr);
15474 static std::string condition_command_help
15475 = gdb::option::build_help (_("\
15476 Specify breakpoint number N to break only if COND is true.\n\
15477 Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
15478 is an expression to be evaluated whenever breakpoint N is reached.\n\
15479 \n\
15480 Options:\n\
15481 %OPTIONS%"), cc_opts);
15482
15483 c = add_com ("condition", class_breakpoint, condition_command,
15484 condition_command_help.c_str ());
15485 set_cmd_completer_handle_brkchars (c, condition_completer);
15486
15487 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15488 Set a temporary breakpoint.\n\
15489 Like \"break\" except the breakpoint is only temporary,\n\
15490 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15491 by using \"enable delete\" on the breakpoint number.\n\
15492 \n"
15493 BREAK_ARGS_HELP ("tbreak")));
15494 set_cmd_completer (c, location_completer);
15495
15496 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15497 Set a hardware assisted breakpoint.\n\
15498 Like \"break\" except the breakpoint requires hardware support,\n\
15499 some target hardware may not have this support.\n\
15500 \n"
15501 BREAK_ARGS_HELP ("hbreak")));
15502 set_cmd_completer (c, location_completer);
15503
15504 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15505 Set a temporary hardware assisted breakpoint.\n\
15506 Like \"hbreak\" except the breakpoint is only temporary,\n\
15507 so it will be deleted when hit.\n\
15508 \n"
15509 BREAK_ARGS_HELP ("thbreak")));
15510 set_cmd_completer (c, location_completer);
15511
15512 cmd_list_element *enable_cmd
15513 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15514 Enable all or some breakpoints.\n\
15515 Usage: enable [BREAKPOINTNUM]...\n\
15516 Give breakpoint numbers (separated by spaces) as arguments.\n\
15517 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15518 This is used to cancel the effect of the \"disable\" command.\n\
15519 With a subcommand you can enable temporarily."),
15520 &enablelist, 1, &cmdlist);
15521
15522 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
15523
15524 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15525 Enable all or some breakpoints.\n\
15526 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
15527 Give breakpoint numbers (separated by spaces) as arguments.\n\
15528 This is used to cancel the effect of the \"disable\" command.\n\
15529 May be abbreviated to simply \"enable\"."),
15530 &enablebreaklist, 1, &enablelist);
15531
15532 add_cmd ("once", no_class, enable_once_command, _("\
15533 Enable some breakpoints for one hit.\n\
15534 Usage: enable breakpoints once BREAKPOINTNUM...\n\
15535 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15536 &enablebreaklist);
15537
15538 add_cmd ("delete", no_class, enable_delete_command, _("\
15539 Enable some breakpoints and delete when hit.\n\
15540 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
15541 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15542 &enablebreaklist);
15543
15544 add_cmd ("count", no_class, enable_count_command, _("\
15545 Enable some breakpoints for COUNT hits.\n\
15546 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
15547 If a breakpoint is hit while enabled in this fashion,\n\
15548 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15549 &enablebreaklist);
15550
15551 add_cmd ("delete", no_class, enable_delete_command, _("\
15552 Enable some breakpoints and delete when hit.\n\
15553 Usage: enable delete BREAKPOINTNUM...\n\
15554 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15555 &enablelist);
15556
15557 add_cmd ("once", no_class, enable_once_command, _("\
15558 Enable some breakpoints for one hit.\n\
15559 Usage: enable once BREAKPOINTNUM...\n\
15560 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15561 &enablelist);
15562
15563 add_cmd ("count", no_class, enable_count_command, _("\
15564 Enable some breakpoints for COUNT hits.\n\
15565 Usage: enable count COUNT BREAKPOINTNUM...\n\
15566 If a breakpoint is hit while enabled in this fashion,\n\
15567 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15568 &enablelist);
15569
15570 cmd_list_element *disable_cmd
15571 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15572 Disable all or some breakpoints.\n\
15573 Usage: disable [BREAKPOINTNUM]...\n\
15574 Arguments are breakpoint numbers with spaces in between.\n\
15575 To disable all breakpoints, give no argument.\n\
15576 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15577 &disablelist, 1, &cmdlist);
15578 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
15579 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
15580
15581 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
15582 Disable all or some breakpoints.\n\
15583 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
15584 Arguments are breakpoint numbers with spaces in between.\n\
15585 To disable all breakpoints, give no argument.\n\
15586 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15587 This command may be abbreviated \"disable\"."),
15588 &disablelist);
15589
15590 cmd_list_element *delete_cmd
15591 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15592 Delete all or some breakpoints.\n\
15593 Usage: delete [BREAKPOINTNUM]...\n\
15594 Arguments are breakpoint numbers with spaces in between.\n\
15595 To delete all breakpoints, give no argument.\n\
15596 \n\
15597 Also a prefix command for deletion of other GDB objects."),
15598 &deletelist, 1, &cmdlist);
15599 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
15600 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
15601
15602 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
15603 Delete all or some breakpoints or auto-display expressions.\n\
15604 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
15605 Arguments are breakpoint numbers with spaces in between.\n\
15606 To delete all breakpoints, give no argument.\n\
15607 This command may be abbreviated \"delete\"."),
15608 &deletelist);
15609
15610 cmd_list_element *clear_cmd
15611 = add_com ("clear", class_breakpoint, clear_command, _("\
15612 Clear breakpoint at specified location.\n\
15613 Argument may be a linespec, explicit, or address location as described below.\n\
15614 \n\
15615 With no argument, clears all breakpoints in the line that the selected frame\n\
15616 is executing in.\n"
15617 "\n" LOCATION_HELP_STRING "\n\n\
15618 See also the \"delete\" command which clears breakpoints by number."));
15619 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
15620
15621 cmd_list_element *break_cmd
15622 = add_com ("break", class_breakpoint, break_command, _("\
15623 Set breakpoint at specified location.\n"
15624 BREAK_ARGS_HELP ("break")));
15625 set_cmd_completer (break_cmd, location_completer);
15626
15627 add_com_alias ("b", break_cmd, class_run, 1);
15628 add_com_alias ("br", break_cmd, class_run, 1);
15629 add_com_alias ("bre", break_cmd, class_run, 1);
15630 add_com_alias ("brea", break_cmd, class_run, 1);
15631
15632 if (dbx_commands)
15633 {
15634 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15635 Break in function/address or break at a line in the current file."),
15636 &stoplist, 1, &cmdlist);
15637 add_cmd ("in", class_breakpoint, stopin_command,
15638 _("Break in function or address."), &stoplist);
15639 add_cmd ("at", class_breakpoint, stopat_command,
15640 _("Break at a line in the current file."), &stoplist);
15641 add_com ("status", class_info, info_breakpoints_command, _("\
15642 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15643 The \"Type\" column indicates one of:\n\
15644 \tbreakpoint - normal breakpoint\n\
15645 \twatchpoint - watchpoint\n\
15646 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15647 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15648 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15649 address and file/line number respectively.\n\
15650 \n\
15651 Convenience variable \"$_\" and default examine address for \"x\"\n\
15652 are set to the address of the last breakpoint listed unless the command\n\
15653 is prefixed with \"server \".\n\n\
15654 Convenience variable \"$bpnum\" contains the number of the last\n\
15655 breakpoint set."));
15656 }
15657
15658 cmd_list_element *info_breakpoints_cmd
15659 = add_info ("breakpoints", info_breakpoints_command, _("\
15660 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15661 The \"Type\" column indicates one of:\n\
15662 \tbreakpoint - normal breakpoint\n\
15663 \twatchpoint - watchpoint\n\
15664 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15665 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15666 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15667 address and file/line number respectively.\n\
15668 \n\
15669 Convenience variable \"$_\" and default examine address for \"x\"\n\
15670 are set to the address of the last breakpoint listed unless the command\n\
15671 is prefixed with \"server \".\n\n\
15672 Convenience variable \"$bpnum\" contains the number of the last\n\
15673 breakpoint set."));
15674
15675 add_info_alias ("b", info_breakpoints_cmd, 1);
15676
15677 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15678 Status of all breakpoints, or breakpoint number NUMBER.\n\
15679 The \"Type\" column indicates one of:\n\
15680 \tbreakpoint - normal breakpoint\n\
15681 \twatchpoint - watchpoint\n\
15682 \tlongjmp - internal breakpoint used to step through longjmp()\n\
15683 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15684 \tuntil - internal breakpoint used by the \"until\" command\n\
15685 \tfinish - internal breakpoint used by the \"finish\" command\n\
15686 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15687 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15688 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15689 address and file/line number respectively.\n\
15690 \n\
15691 Convenience variable \"$_\" and default examine address for \"x\"\n\
15692 are set to the address of the last breakpoint listed unless the command\n\
15693 is prefixed with \"server \".\n\n\
15694 Convenience variable \"$bpnum\" contains the number of the last\n\
15695 breakpoint set."),
15696 &maintenanceinfolist);
15697
15698 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
15699 Set catchpoints to catch events."),
15700 &catch_cmdlist,
15701 0/*allow-unknown*/, &cmdlist);
15702
15703 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
15704 Set temporary catchpoints to catch events."),
15705 &tcatch_cmdlist,
15706 0/*allow-unknown*/, &cmdlist);
15707
15708 add_catch_command ("fork", _("Catch calls to fork."),
15709 catch_fork_command_1,
15710 NULL,
15711 (void *) (uintptr_t) catch_fork_permanent,
15712 (void *) (uintptr_t) catch_fork_temporary);
15713 add_catch_command ("vfork", _("Catch calls to vfork."),
15714 catch_fork_command_1,
15715 NULL,
15716 (void *) (uintptr_t) catch_vfork_permanent,
15717 (void *) (uintptr_t) catch_vfork_temporary);
15718 add_catch_command ("exec", _("Catch calls to exec."),
15719 catch_exec_command_1,
15720 NULL,
15721 CATCH_PERMANENT,
15722 CATCH_TEMPORARY);
15723 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15724 Usage: catch load [REGEX]\n\
15725 If REGEX is given, only stop for libraries matching the regular expression."),
15726 catch_load_command_1,
15727 NULL,
15728 CATCH_PERMANENT,
15729 CATCH_TEMPORARY);
15730 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15731 Usage: catch unload [REGEX]\n\
15732 If REGEX is given, only stop for libraries matching the regular expression."),
15733 catch_unload_command_1,
15734 NULL,
15735 CATCH_PERMANENT,
15736 CATCH_TEMPORARY);
15737
15738 const auto opts = make_watch_options_def_group (nullptr);
15739
15740 static const std::string watch_help = gdb::option::build_help (_("\
15741 Set a watchpoint for EXPRESSION.\n\
15742 Usage: watch [-location] EXPRESSION\n\
15743 \n\
15744 Options:\n\
15745 %OPTIONS%\n\
15746 \n\
15747 A watchpoint stops execution of your program whenever the value of\n\
15748 an expression changes."), opts);
15749 c = add_com ("watch", class_breakpoint, watch_command,
15750 watch_help.c_str ());
15751 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15752
15753 static const std::string rwatch_help = gdb::option::build_help (_("\
15754 Set a read watchpoint for EXPRESSION.\n\
15755 Usage: rwatch [-location] EXPRESSION\n\
15756 \n\
15757 Options:\n\
15758 %OPTIONS%\n\
15759 \n\
15760 A read watchpoint stops execution of your program whenever the value of\n\
15761 an expression is read."), opts);
15762 c = add_com ("rwatch", class_breakpoint, rwatch_command,
15763 rwatch_help.c_str ());
15764 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15765
15766 static const std::string awatch_help = gdb::option::build_help (_("\
15767 Set an access watchpoint for EXPRESSION.\n\
15768 Usage: awatch [-location] EXPRESSION\n\
15769 \n\
15770 Options:\n\
15771 %OPTIONS%\n\
15772 \n\
15773 An access watchpoint stops execution of your program whenever the value\n\
15774 of an expression is either read or written."), opts);
15775 c = add_com ("awatch", class_breakpoint, awatch_command,
15776 awatch_help.c_str ());
15777 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15778
15779 add_info ("watchpoints", info_watchpoints_command, _("\
15780 Status of specified watchpoints (all watchpoints if no argument)."));
15781
15782 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15783 respond to changes - contrary to the description. */
15784 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15785 &can_use_hw_watchpoints, _("\
15786 Set debugger's willingness to use watchpoint hardware."), _("\
15787 Show debugger's willingness to use watchpoint hardware."), _("\
15788 If zero, gdb will not use hardware for new watchpoints, even if\n\
15789 such is available. (However, any hardware watchpoints that were\n\
15790 created before setting this to nonzero, will continue to use watchpoint\n\
15791 hardware.)"),
15792 NULL,
15793 show_can_use_hw_watchpoints,
15794 &setlist, &showlist);
15795
15796 can_use_hw_watchpoints = 1;
15797
15798 /* Tracepoint manipulation commands. */
15799
15800 cmd_list_element *trace_cmd
15801 = add_com ("trace", class_breakpoint, trace_command, _("\
15802 Set a tracepoint at specified location.\n\
15803 \n"
15804 BREAK_ARGS_HELP ("trace") "\n\
15805 Do \"help tracepoints\" for info on other tracepoint commands."));
15806 set_cmd_completer (trace_cmd, location_completer);
15807
15808 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
15809 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
15810 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
15811 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
15812
15813 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
15814 Set a fast tracepoint at specified location.\n\
15815 \n"
15816 BREAK_ARGS_HELP ("ftrace") "\n\
15817 Do \"help tracepoints\" for info on other tracepoint commands."));
15818 set_cmd_completer (c, location_completer);
15819
15820 c = add_com ("strace", class_breakpoint, strace_command, _("\
15821 Set a static tracepoint at location or marker.\n\
15822 \n\
15823 strace [LOCATION] [if CONDITION]\n\
15824 LOCATION may be a linespec, explicit, or address location (described below) \n\
15825 or -m MARKER_ID.\n\n\
15826 If a marker id is specified, probe the marker with that name. With\n\
15827 no LOCATION, uses current execution address of the selected stack frame.\n\
15828 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15829 This collects arbitrary user data passed in the probe point call to the\n\
15830 tracing library. You can inspect it when analyzing the trace buffer,\n\
15831 by printing the $_sdata variable like any other convenience variable.\n\
15832 \n\
15833 CONDITION is a boolean expression.\n\
15834 \n" LOCATION_HELP_STRING "\n\n\
15835 Multiple tracepoints at one place are permitted, and useful if their\n\
15836 conditions are different.\n\
15837 \n\
15838 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15839 Do \"help tracepoints\" for info on other tracepoint commands."));
15840 set_cmd_completer (c, location_completer);
15841
15842 cmd_list_element *info_tracepoints_cmd
15843 = add_info ("tracepoints", info_tracepoints_command, _("\
15844 Status of specified tracepoints (all tracepoints if no argument).\n\
15845 Convenience variable \"$tpnum\" contains the number of the\n\
15846 last tracepoint set."));
15847
15848 add_info_alias ("tp", info_tracepoints_cmd, 1);
15849
15850 cmd_list_element *delete_tracepoints_cmd
15851 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15852 Delete specified tracepoints.\n\
15853 Arguments are tracepoint numbers, separated by spaces.\n\
15854 No argument means delete all tracepoints."),
15855 &deletelist);
15856 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
15857
15858 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15859 Disable specified tracepoints.\n\
15860 Arguments are tracepoint numbers, separated by spaces.\n\
15861 No argument means disable all tracepoints."),
15862 &disablelist);
15863 deprecate_cmd (c, "disable");
15864
15865 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15866 Enable specified tracepoints.\n\
15867 Arguments are tracepoint numbers, separated by spaces.\n\
15868 No argument means enable all tracepoints."),
15869 &enablelist);
15870 deprecate_cmd (c, "enable");
15871
15872 add_com ("passcount", class_trace, trace_pass_command, _("\
15873 Set the passcount for a tracepoint.\n\
15874 The trace will end when the tracepoint has been passed 'count' times.\n\
15875 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15876 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15877
15878 add_basic_prefix_cmd ("save", class_breakpoint,
15879 _("Save breakpoint definitions as a script."),
15880 &save_cmdlist,
15881 0/*allow-unknown*/, &cmdlist);
15882
15883 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15884 Save current breakpoint definitions as a script.\n\
15885 This includes all types of breakpoints (breakpoints, watchpoints,\n\
15886 catchpoints, tracepoints). Use the 'source' command in another debug\n\
15887 session to restore them."),
15888 &save_cmdlist);
15889 set_cmd_completer (c, filename_completer);
15890
15891 cmd_list_element *save_tracepoints_cmd
15892 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
15893 Save current tracepoint definitions as a script.\n\
15894 Use the 'source' command in another debug session to restore them."),
15895 &save_cmdlist);
15896 set_cmd_completer (save_tracepoints_cmd, filename_completer);
15897
15898 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
15899 deprecate_cmd (c, "save tracepoints");
15900
15901 add_basic_prefix_cmd ("breakpoint", class_maintenance, _("\
15902 Breakpoint specific settings.\n\
15903 Configure various breakpoint-specific variables such as\n\
15904 pending breakpoint behavior."),
15905 &breakpoint_set_cmdlist,
15906 0/*allow-unknown*/, &setlist);
15907 add_show_prefix_cmd ("breakpoint", class_maintenance, _("\
15908 Breakpoint specific settings.\n\
15909 Configure various breakpoint-specific variables such as\n\
15910 pending breakpoint behavior."),
15911 &breakpoint_show_cmdlist,
15912 0/*allow-unknown*/, &showlist);
15913
15914 add_setshow_auto_boolean_cmd ("pending", no_class,
15915 &pending_break_support, _("\
15916 Set debugger's behavior regarding pending breakpoints."), _("\
15917 Show debugger's behavior regarding pending breakpoints."), _("\
15918 If on, an unrecognized breakpoint location will cause gdb to create a\n\
15919 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15920 an error. If auto, an unrecognized breakpoint location results in a\n\
15921 user-query to see if a pending breakpoint should be created."),
15922 NULL,
15923 show_pending_break_support,
15924 &breakpoint_set_cmdlist,
15925 &breakpoint_show_cmdlist);
15926
15927 pending_break_support = AUTO_BOOLEAN_AUTO;
15928
15929 add_setshow_boolean_cmd ("auto-hw", no_class,
15930 &automatic_hardware_breakpoints, _("\
15931 Set automatic usage of hardware breakpoints."), _("\
15932 Show automatic usage of hardware breakpoints."), _("\
15933 If set, the debugger will automatically use hardware breakpoints for\n\
15934 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15935 a warning will be emitted for such breakpoints."),
15936 NULL,
15937 show_automatic_hardware_breakpoints,
15938 &breakpoint_set_cmdlist,
15939 &breakpoint_show_cmdlist);
15940
15941 add_setshow_boolean_cmd ("always-inserted", class_support,
15942 &always_inserted_mode, _("\
15943 Set mode for inserting breakpoints."), _("\
15944 Show mode for inserting breakpoints."), _("\
15945 When this mode is on, breakpoints are inserted immediately as soon as\n\
15946 they're created, kept inserted even when execution stops, and removed\n\
15947 only when the user deletes them. When this mode is off (the default),\n\
15948 breakpoints are inserted only when execution continues, and removed\n\
15949 when execution stops."),
15950 NULL,
15951 &show_always_inserted_mode,
15952 &breakpoint_set_cmdlist,
15953 &breakpoint_show_cmdlist);
15954
15955 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15956 condition_evaluation_enums,
15957 &condition_evaluation_mode_1, _("\
15958 Set mode of breakpoint condition evaluation."), _("\
15959 Show mode of breakpoint condition evaluation."), _("\
15960 When this is set to \"host\", breakpoint conditions will be\n\
15961 evaluated on the host's side by GDB. When it is set to \"target\",\n\
15962 breakpoint conditions will be downloaded to the target (if the target\n\
15963 supports such feature) and conditions will be evaluated on the target's side.\n\
15964 If this is set to \"auto\" (default), this will be automatically set to\n\
15965 \"target\" if it supports condition evaluation, otherwise it will\n\
15966 be set to \"host\"."),
15967 &set_condition_evaluation_mode,
15968 &show_condition_evaluation_mode,
15969 &breakpoint_set_cmdlist,
15970 &breakpoint_show_cmdlist);
15971
15972 add_com ("break-range", class_breakpoint, break_range_command, _("\
15973 Set a breakpoint for an address range.\n\
15974 break-range START-LOCATION, END-LOCATION\n\
15975 where START-LOCATION and END-LOCATION can be one of the following:\n\
15976 LINENUM, for that line in the current file,\n\
15977 FILE:LINENUM, for that line in that file,\n\
15978 +OFFSET, for that number of lines after the current line\n\
15979 or the start of the range\n\
15980 FUNCTION, for the first line in that function,\n\
15981 FILE:FUNCTION, to distinguish among like-named static functions.\n\
15982 *ADDRESS, for the instruction at that address.\n\
15983 \n\
15984 The breakpoint will stop execution of the inferior whenever it executes\n\
15985 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
15986 range (including START-LOCATION and END-LOCATION)."));
15987
15988 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
15989 Set a dynamic printf at specified location.\n\
15990 dprintf location,format string,arg1,arg2,...\n\
15991 location may be a linespec, explicit, or address location.\n"
15992 "\n" LOCATION_HELP_STRING));
15993 set_cmd_completer (c, location_completer);
15994
15995 add_setshow_enum_cmd ("dprintf-style", class_support,
15996 dprintf_style_enums, &dprintf_style, _("\
15997 Set the style of usage for dynamic printf."), _("\
15998 Show the style of usage for dynamic printf."), _("\
15999 This setting chooses how GDB will do a dynamic printf.\n\
16000 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16001 console, as with the \"printf\" command.\n\
16002 If the value is \"call\", the print is done by calling a function in your\n\
16003 program; by default printf(), but you can choose a different function or\n\
16004 output stream by setting dprintf-function and dprintf-channel."),
16005 update_dprintf_commands, NULL,
16006 &setlist, &showlist);
16007
16008 dprintf_function = xstrdup ("printf");
16009 add_setshow_string_cmd ("dprintf-function", class_support,
16010 &dprintf_function, _("\
16011 Set the function to use for dynamic printf."), _("\
16012 Show the function to use for dynamic printf."), NULL,
16013 update_dprintf_commands, NULL,
16014 &setlist, &showlist);
16015
16016 dprintf_channel = xstrdup ("");
16017 add_setshow_string_cmd ("dprintf-channel", class_support,
16018 &dprintf_channel, _("\
16019 Set the channel to use for dynamic printf."), _("\
16020 Show the channel to use for dynamic printf."), NULL,
16021 update_dprintf_commands, NULL,
16022 &setlist, &showlist);
16023
16024 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16025 &disconnected_dprintf, _("\
16026 Set whether dprintf continues after GDB disconnects."), _("\
16027 Show whether dprintf continues after GDB disconnects."), _("\
16028 Use this to let dprintf commands continue to hit and produce output\n\
16029 even if GDB disconnects or detaches from the target."),
16030 NULL,
16031 NULL,
16032 &setlist, &showlist);
16033
16034 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16035 Target agent only formatted printing, like the C \"printf\" function.\n\
16036 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
16037 This supports most C printf format specifications, like %s, %d, etc.\n\
16038 This is useful for formatted output in user-defined commands."));
16039
16040 automatic_hardware_breakpoints = true;
16041
16042 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
16043 "breakpoint");
16044 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
16045 "breakpoint");
16046 }