gdb: pass more const target_waitstatus by reference
[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 #include "cli/cli-decode.h"
71
72 /* readline include files */
73 #include "readline/tilde.h"
74
75 /* readline defines this. */
76 #undef savestring
77
78 #include "mi/mi-common.h"
79 #include "extension.h"
80 #include <algorithm>
81 #include "progspace-and-thread.h"
82 #include "gdbsupport/array-view.h"
83 #include "gdbsupport/gdb_optional.h"
84
85 /* Prototypes for local functions. */
86
87 static void map_breakpoint_numbers (const char *,
88 gdb::function_view<void (breakpoint *)>);
89
90 static void breakpoint_re_set_default (struct breakpoint *);
91
92 static void
93 create_sals_from_location_default (struct event_location *location,
94 struct linespec_result *canonical,
95 enum bptype type_wanted);
96
97 static void create_breakpoints_sal_default (struct gdbarch *,
98 struct linespec_result *,
99 gdb::unique_xmalloc_ptr<char>,
100 gdb::unique_xmalloc_ptr<char>,
101 enum bptype,
102 enum bpdisp, int, int,
103 int,
104 const struct breakpoint_ops *,
105 int, int, int, unsigned);
106
107 static std::vector<symtab_and_line> decode_location_default
108 (struct breakpoint *b, struct event_location *location,
109 struct program_space *search_pspace);
110
111 static int can_use_hardware_watchpoint
112 (const std::vector<value_ref_ptr> &vals);
113
114 static void mention (struct breakpoint *);
115
116 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
117 enum bptype,
118 const struct breakpoint_ops *);
119 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
120 const struct symtab_and_line *);
121
122 /* This function is used in gdbtk sources and thus can not be made
123 static. */
124 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
125 struct symtab_and_line,
126 enum bptype,
127 const struct breakpoint_ops *);
128
129 static struct breakpoint *
130 momentary_breakpoint_from_master (struct breakpoint *orig,
131 enum bptype type,
132 const struct breakpoint_ops *ops,
133 int loc_enabled);
134
135 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
136
137 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
138 CORE_ADDR bpaddr,
139 enum bptype bptype);
140
141 static void describe_other_breakpoints (struct gdbarch *,
142 struct program_space *, CORE_ADDR,
143 struct obj_section *, int);
144
145 static int watchpoint_locations_match (struct bp_location *loc1,
146 struct bp_location *loc2);
147
148 static int breakpoint_locations_match (struct bp_location *loc1,
149 struct bp_location *loc2,
150 bool sw_hw_bps_match = false);
151
152 static int breakpoint_location_address_match (struct bp_location *bl,
153 const struct address_space *aspace,
154 CORE_ADDR addr);
155
156 static int breakpoint_location_address_range_overlap (struct bp_location *,
157 const address_space *,
158 CORE_ADDR, int);
159
160 static int remove_breakpoint (struct bp_location *);
161 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
162
163 static enum print_stop_action print_bp_stop_message (bpstat *bs);
164
165 static int hw_breakpoint_used_count (void);
166
167 static int hw_watchpoint_use_count (struct breakpoint *);
168
169 static int hw_watchpoint_used_count_others (struct breakpoint *except,
170 enum bptype type,
171 int *other_type_used);
172
173 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
174 int count);
175
176 static void decref_bp_location (struct bp_location **loc);
177
178 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
179
180 /* update_global_location_list's modes of operation wrt to whether to
181 insert locations now. */
182 enum ugll_insert_mode
183 {
184 /* Don't insert any breakpoint locations into the inferior, only
185 remove already-inserted locations that no longer should be
186 inserted. Functions that delete a breakpoint or breakpoints
187 should specify this mode, so that deleting a breakpoint doesn't
188 have the side effect of inserting the locations of other
189 breakpoints that are marked not-inserted, but should_be_inserted
190 returns true on them.
191
192 This behavior is useful is situations close to tear-down -- e.g.,
193 after an exec, while the target still has execution, but
194 breakpoint shadows of the previous executable image should *NOT*
195 be restored to the new image; or before detaching, where the
196 target still has execution and wants to delete breakpoints from
197 GDB's lists, and all breakpoints had already been removed from
198 the inferior. */
199 UGLL_DONT_INSERT,
200
201 /* May insert breakpoints iff breakpoints_should_be_inserted_now
202 claims breakpoints should be inserted now. */
203 UGLL_MAY_INSERT,
204
205 /* Insert locations now, irrespective of
206 breakpoints_should_be_inserted_now. E.g., say all threads are
207 stopped right now, and the user did "continue". We need to
208 insert breakpoints _before_ resuming the target, but
209 UGLL_MAY_INSERT wouldn't insert them, because
210 breakpoints_should_be_inserted_now returns false at that point,
211 as no thread is running yet. */
212 UGLL_INSERT
213 };
214
215 static void update_global_location_list (enum ugll_insert_mode);
216
217 static void update_global_location_list_nothrow (enum ugll_insert_mode);
218
219 static void insert_breakpoint_locations (void);
220
221 static void trace_pass_command (const char *, int);
222
223 static void set_tracepoint_count (int num);
224
225 static bool is_masked_watchpoint (const struct breakpoint *b);
226
227 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
228 otherwise. */
229
230 static int strace_marker_p (struct breakpoint *b);
231
232 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
233 that are implemented on top of software or hardware breakpoints
234 (user breakpoints, internal and momentary breakpoints, etc.). */
235 static struct breakpoint_ops bkpt_base_breakpoint_ops;
236
237 /* Internal breakpoints class type. */
238 static struct breakpoint_ops internal_breakpoint_ops;
239
240 /* Momentary breakpoints class type. */
241 static struct breakpoint_ops momentary_breakpoint_ops;
242
243 /* The breakpoint_ops structure to be used in regular user created
244 breakpoints. */
245 struct breakpoint_ops bkpt_breakpoint_ops;
246
247 /* Breakpoints set on probes. */
248 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
249
250 /* Tracepoints set on probes. */
251 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
252
253 /* Dynamic printf class type. */
254 struct breakpoint_ops dprintf_breakpoint_ops;
255
256 /* The style in which to perform a dynamic printf. This is a user
257 option because different output options have different tradeoffs;
258 if GDB does the printing, there is better error handling if there
259 is a problem with any of the arguments, but using an inferior
260 function lets you have special-purpose printers and sending of
261 output to the same place as compiled-in print functions. */
262
263 static const char dprintf_style_gdb[] = "gdb";
264 static const char dprintf_style_call[] = "call";
265 static const char dprintf_style_agent[] = "agent";
266 static const char *const dprintf_style_enums[] = {
267 dprintf_style_gdb,
268 dprintf_style_call,
269 dprintf_style_agent,
270 NULL
271 };
272 static const char *dprintf_style = dprintf_style_gdb;
273
274 /* The function to use for dynamic printf if the preferred style is to
275 call into the inferior. The value is simply a string that is
276 copied into the command, so it can be anything that GDB can
277 evaluate to a callable address, not necessarily a function name. */
278
279 static std::string dprintf_function = "printf";
280
281 /* The channel to use for dynamic printf if the preferred style is to
282 call into the inferior; if a nonempty string, it will be passed to
283 the call as the first argument, with the format string as the
284 second. As with the dprintf function, this can be anything that
285 GDB knows how to evaluate, so in addition to common choices like
286 "stderr", this could be an app-specific expression like
287 "mystreams[curlogger]". */
288
289 static std::string dprintf_channel;
290
291 /* True if dprintf commands should continue to operate even if GDB
292 has disconnected. */
293 static bool disconnected_dprintf = true;
294
295 struct command_line *
296 breakpoint_commands (struct breakpoint *b)
297 {
298 return b->commands ? b->commands.get () : NULL;
299 }
300
301 /* Flag indicating that a command has proceeded the inferior past the
302 current breakpoint. */
303
304 static bool breakpoint_proceeded;
305
306 const char *
307 bpdisp_text (enum bpdisp disp)
308 {
309 /* NOTE: the following values are a part of MI protocol and
310 represent values of 'disp' field returned when inferior stops at
311 a breakpoint. */
312 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
313
314 return bpdisps[(int) disp];
315 }
316
317 /* Prototypes for exported functions. */
318 /* If FALSE, gdb will not use hardware support for watchpoints, even
319 if such is available. */
320 static int can_use_hw_watchpoints;
321
322 static void
323 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
324 struct cmd_list_element *c,
325 const char *value)
326 {
327 fprintf_filtered (file,
328 _("Debugger's willingness to use "
329 "watchpoint hardware is %s.\n"),
330 value);
331 }
332
333 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
334 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
335 for unrecognized breakpoint locations.
336 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
337 static enum auto_boolean pending_break_support;
338 static void
339 show_pending_break_support (struct ui_file *file, int from_tty,
340 struct cmd_list_element *c,
341 const char *value)
342 {
343 fprintf_filtered (file,
344 _("Debugger's behavior regarding "
345 "pending breakpoints is %s.\n"),
346 value);
347 }
348
349 /* If true, gdb will automatically use hardware breakpoints for breakpoints
350 set with "break" but falling in read-only memory.
351 If false, gdb will warn about such breakpoints, but won't automatically
352 use hardware breakpoints. */
353 static bool automatic_hardware_breakpoints;
354 static void
355 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
356 struct cmd_list_element *c,
357 const char *value)
358 {
359 fprintf_filtered (file,
360 _("Automatic usage of hardware breakpoints is %s.\n"),
361 value);
362 }
363
364 /* If on, GDB keeps breakpoints inserted even if the inferior is
365 stopped, and immediately inserts any new breakpoints as soon as
366 they're created. If off (default), GDB keeps breakpoints off of
367 the target as long as possible. That is, it delays inserting
368 breakpoints until the next resume, and removes them again when the
369 target fully stops. This is a bit safer in case GDB crashes while
370 processing user input. */
371 static bool always_inserted_mode = false;
372
373 static void
374 show_always_inserted_mode (struct ui_file *file, int from_tty,
375 struct cmd_list_element *c, const char *value)
376 {
377 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
378 value);
379 }
380
381 /* See breakpoint.h. */
382
383 int
384 breakpoints_should_be_inserted_now (void)
385 {
386 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
387 {
388 /* If breakpoints are global, they should be inserted even if no
389 thread under gdb's control is running, or even if there are
390 no threads under GDB's control yet. */
391 return 1;
392 }
393 else
394 {
395 if (always_inserted_mode)
396 {
397 /* The user wants breakpoints inserted even if all threads
398 are stopped. */
399 return 1;
400 }
401
402 for (inferior *inf : all_inferiors ())
403 if (inf->has_execution ()
404 && threads_are_executing (inf->process_target ()))
405 return 1;
406
407 /* Don't remove breakpoints yet if, even though all threads are
408 stopped, we still have events to process. */
409 for (thread_info *tp : all_non_exited_threads ())
410 if (tp->resumed () && tp->has_pending_waitstatus ())
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 /* See breakpoint.h. */
497
498 breakpoint_range
499 all_breakpoints ()
500 {
501 return breakpoint_range (breakpoint_chain);
502 }
503
504 /* See breakpoint.h. */
505
506 breakpoint_safe_range
507 all_breakpoints_safe ()
508 {
509 return breakpoint_safe_range (all_breakpoints ());
510 }
511
512 /* See breakpoint.h. */
513
514 tracepoint_range
515 all_tracepoints ()
516 {
517 return tracepoint_range (breakpoint_chain);
518 }
519
520 /* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
521
522 static std::vector<bp_location *> bp_locations;
523
524 /* See breakpoint.h. */
525
526 const std::vector<bp_location *> &
527 all_bp_locations ()
528 {
529 return bp_locations;
530 }
531
532 /* Range to iterate over breakpoint locations at a given address. */
533
534 struct bp_locations_at_addr_range
535 {
536 using iterator = std::vector<bp_location *>::iterator;
537
538 bp_locations_at_addr_range (CORE_ADDR addr)
539 {
540 struct compare
541 {
542 bool operator() (const bp_location *loc, CORE_ADDR addr_) const
543 { return loc->address < addr_; }
544
545 bool operator() (CORE_ADDR addr_, const bp_location *loc) const
546 { return addr_ < loc->address; }
547 };
548
549 auto it_pair = std::equal_range (bp_locations.begin (), bp_locations.end (),
550 addr, compare ());
551
552 m_begin = it_pair.first;
553 m_end = it_pair.second;
554 }
555
556 iterator begin () const
557 { return m_begin; }
558
559 iterator end () const
560 { return m_end; }
561
562 private:
563 iterator m_begin;
564 iterator m_end;
565 };
566
567 /* Return a range to iterate over all breakpoint locations exactly at address
568 ADDR.
569
570 If it's needed to iterate multiple times on the same range, it's possible
571 to save the range in a local variable and use it multiple times:
572
573 auto range = all_bp_locations_at_addr (addr);
574
575 for (bp_location *loc : range)
576 // use loc
577
578 for (bp_location *loc : range)
579 // use loc
580
581 This saves a bit of time, as it avoids re-doing the binary searches to find
582 the range's boundaries. Just remember not to change the bp_locations vector
583 in the mean time, as it could make the range's iterators stale. */
584
585 static bp_locations_at_addr_range
586 all_bp_locations_at_addr (CORE_ADDR addr)
587 {
588 return bp_locations_at_addr_range (addr);
589 }
590
591 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
592 ADDRESS for the current elements of BP_LOCATIONS which get a valid
593 result from bp_location_has_shadow. You can use it for roughly
594 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
595 an address you need to read. */
596
597 static CORE_ADDR bp_locations_placed_address_before_address_max;
598
599 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
600 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
601 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
602 You can use it for roughly limiting the subrange of BP_LOCATIONS to
603 scan for shadow bytes for an address you need to read. */
604
605 static CORE_ADDR bp_locations_shadow_len_after_address_max;
606
607 /* The locations that no longer correspond to any breakpoint, unlinked
608 from the bp_locations array, but for which a hit may still be
609 reported by a target. */
610 static std::vector<bp_location *> moribund_locations;
611
612 /* Number of last breakpoint made. */
613
614 static int breakpoint_count;
615
616 /* The value of `breakpoint_count' before the last command that
617 created breakpoints. If the last (break-like) command created more
618 than one breakpoint, then the difference between BREAKPOINT_COUNT
619 and PREV_BREAKPOINT_COUNT is more than one. */
620 static int prev_breakpoint_count;
621
622 /* Number of last tracepoint made. */
623
624 static int tracepoint_count;
625
626 static struct cmd_list_element *breakpoint_set_cmdlist;
627 static struct cmd_list_element *breakpoint_show_cmdlist;
628 struct cmd_list_element *save_cmdlist;
629
630 /* Return whether a breakpoint is an active enabled breakpoint. */
631 static int
632 breakpoint_enabled (struct breakpoint *b)
633 {
634 return (b->enable_state == bp_enabled);
635 }
636
637 /* Set breakpoint count to NUM. */
638
639 static void
640 set_breakpoint_count (int num)
641 {
642 prev_breakpoint_count = breakpoint_count;
643 breakpoint_count = num;
644 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
645 }
646
647 /* Used by `start_rbreak_breakpoints' below, to record the current
648 breakpoint count before "rbreak" creates any breakpoint. */
649 static int rbreak_start_breakpoint_count;
650
651 /* Called at the start an "rbreak" command to record the first
652 breakpoint made. */
653
654 scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
655 {
656 rbreak_start_breakpoint_count = breakpoint_count;
657 }
658
659 /* Called at the end of an "rbreak" command to record the last
660 breakpoint made. */
661
662 scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
663 {
664 prev_breakpoint_count = rbreak_start_breakpoint_count;
665 }
666
667 /* Used in run_command to zero the hit count when a new run starts. */
668
669 void
670 clear_breakpoint_hit_counts (void)
671 {
672 for (breakpoint *b : all_breakpoints ())
673 b->hit_count = 0;
674 }
675
676 \f
677 /* Return the breakpoint with the specified number, or NULL
678 if the number does not refer to an existing breakpoint. */
679
680 struct breakpoint *
681 get_breakpoint (int num)
682 {
683 for (breakpoint *b : all_breakpoints ())
684 if (b->number == num)
685 return b;
686
687 return nullptr;
688 }
689
690 \f
691
692 /* Mark locations as "conditions have changed" in case the target supports
693 evaluating conditions on its side. */
694
695 static void
696 mark_breakpoint_modified (struct breakpoint *b)
697 {
698 /* This is only meaningful if the target is
699 evaluating conditions and if the user has
700 opted for condition evaluation on the target's
701 side. */
702 if (gdb_evaluates_breakpoint_condition_p ()
703 || !target_supports_evaluation_of_breakpoint_conditions ())
704 return;
705
706 if (!is_breakpoint (b))
707 return;
708
709 for (bp_location *loc : b->locations ())
710 loc->condition_changed = condition_modified;
711 }
712
713 /* Mark location as "conditions have changed" in case the target supports
714 evaluating conditions on its side. */
715
716 static void
717 mark_breakpoint_location_modified (struct bp_location *loc)
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
726 return;
727
728 if (!is_breakpoint (loc->owner))
729 return;
730
731 loc->condition_changed = condition_modified;
732 }
733
734 /* Sets the condition-evaluation mode using the static global
735 condition_evaluation_mode. */
736
737 static void
738 set_condition_evaluation_mode (const char *args, int from_tty,
739 struct cmd_list_element *c)
740 {
741 const char *old_mode, *new_mode;
742
743 if ((condition_evaluation_mode_1 == condition_evaluation_target)
744 && !target_supports_evaluation_of_breakpoint_conditions ())
745 {
746 condition_evaluation_mode_1 = condition_evaluation_mode;
747 warning (_("Target does not support breakpoint condition evaluation.\n"
748 "Using host evaluation mode instead."));
749 return;
750 }
751
752 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
753 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
754
755 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
756 settings was "auto". */
757 condition_evaluation_mode = condition_evaluation_mode_1;
758
759 /* Only update the mode if the user picked a different one. */
760 if (new_mode != old_mode)
761 {
762 /* If the user switched to a different evaluation mode, we
763 need to synch the changes with the target as follows:
764
765 "host" -> "target": Send all (valid) conditions to the target.
766 "target" -> "host": Remove all the conditions from the target.
767 */
768
769 if (new_mode == condition_evaluation_target)
770 {
771 /* Mark everything modified and synch conditions with the
772 target. */
773 for (bp_location *loc : all_bp_locations ())
774 mark_breakpoint_location_modified (loc);
775 }
776 else
777 {
778 /* Manually mark non-duplicate locations to synch conditions
779 with the target. We do this to remove all the conditions the
780 target knows about. */
781 for (bp_location *loc : all_bp_locations ())
782 if (is_breakpoint (loc->owner) && loc->inserted)
783 loc->needs_update = 1;
784 }
785
786 /* Do the update. */
787 update_global_location_list (UGLL_MAY_INSERT);
788 }
789
790 return;
791 }
792
793 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
794 what "auto" is translating to. */
795
796 static void
797 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
798 struct cmd_list_element *c, const char *value)
799 {
800 if (condition_evaluation_mode == condition_evaluation_auto)
801 fprintf_filtered (file,
802 _("Breakpoint condition evaluation "
803 "mode is %s (currently %s).\n"),
804 value,
805 breakpoint_condition_evaluation_mode ());
806 else
807 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
808 value);
809 }
810
811 /* Parse COND_STRING in the context of LOC and set as the condition
812 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
813 the number of LOC within its owner. In case of parsing error, mark
814 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
815
816 static void
817 set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
818 int bp_num, int loc_num)
819 {
820 bool has_junk = false;
821 try
822 {
823 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
824 block_for_pc (loc->address), 0);
825 if (*cond_string != 0)
826 has_junk = true;
827 else
828 {
829 loc->cond = std::move (new_exp);
830 if (loc->disabled_by_cond && loc->enabled)
831 printf_filtered (_("Breakpoint %d's condition is now valid at "
832 "location %d, enabling.\n"),
833 bp_num, loc_num);
834
835 loc->disabled_by_cond = false;
836 }
837 }
838 catch (const gdb_exception_error &e)
839 {
840 if (loc->enabled)
841 {
842 /* Warn if a user-enabled location is now becoming disabled-by-cond.
843 BP_NUM is 0 if the breakpoint is being defined for the first
844 time using the "break ... if ..." command, and non-zero if
845 already defined. */
846 if (bp_num != 0)
847 warning (_("failed to validate condition at location %d.%d, "
848 "disabling:\n %s"), bp_num, loc_num, e.what ());
849 else
850 warning (_("failed to validate condition at location %d, "
851 "disabling:\n %s"), loc_num, e.what ());
852 }
853
854 loc->disabled_by_cond = true;
855 }
856
857 if (has_junk)
858 error (_("Garbage '%s' follows condition"), cond_string);
859 }
860
861 void
862 set_breakpoint_condition (struct breakpoint *b, const char *exp,
863 int from_tty, bool force)
864 {
865 if (*exp == 0)
866 {
867 b->cond_string.reset ();
868
869 if (is_watchpoint (b))
870 static_cast<watchpoint *> (b)->cond_exp.reset ();
871 else
872 {
873 int loc_num = 1;
874 for (bp_location *loc : b->locations ())
875 {
876 loc->cond.reset ();
877 if (loc->disabled_by_cond && loc->enabled)
878 printf_filtered (_("Breakpoint %d's condition is now valid at "
879 "location %d, enabling.\n"),
880 b->number, loc_num);
881 loc->disabled_by_cond = false;
882 loc_num++;
883
884 /* No need to free the condition agent expression
885 bytecode (if we have one). We will handle this
886 when we go through update_global_location_list. */
887 }
888 }
889
890 if (from_tty)
891 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
892 }
893 else
894 {
895 if (is_watchpoint (b))
896 {
897 innermost_block_tracker tracker;
898 const char *arg = exp;
899 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
900 if (*arg != 0)
901 error (_("Junk at end of expression"));
902 watchpoint *w = static_cast<watchpoint *> (b);
903 w->cond_exp = std::move (new_exp);
904 w->cond_exp_valid_block = tracker.block ();
905 }
906 else
907 {
908 /* Parse and set condition expressions. We make two passes.
909 In the first, we parse the condition string to see if it
910 is valid in at least one location. If so, the condition
911 would be accepted. So we go ahead and set the locations'
912 conditions. In case no valid case is found, we throw
913 the error and the condition string will be rejected.
914 This two-pass approach is taken to avoid setting the
915 state of locations in case of a reject. */
916 for (bp_location *loc : b->locations ())
917 {
918 try
919 {
920 const char *arg = exp;
921 parse_exp_1 (&arg, loc->address,
922 block_for_pc (loc->address), 0);
923 if (*arg != 0)
924 error (_("Junk at end of expression"));
925 break;
926 }
927 catch (const gdb_exception_error &e)
928 {
929 /* Condition string is invalid. If this happens to
930 be the last loc, abandon (if not forced) or continue
931 (if forced). */
932 if (loc->next == nullptr && !force)
933 throw;
934 }
935 }
936
937 /* If we reach here, the condition is valid at some locations. */
938 int loc_num = 1;
939 for (bp_location *loc : b->locations ())
940 {
941 set_breakpoint_location_condition (exp, loc, b->number, loc_num);
942 loc_num++;
943 }
944 }
945
946 /* We know that the new condition parsed successfully. The
947 condition string of the breakpoint can be safely updated. */
948 b->cond_string = make_unique_xstrdup (exp);
949 b->condition_not_parsed = 0;
950 }
951 mark_breakpoint_modified (b);
952
953 gdb::observers::breakpoint_modified.notify (b);
954 }
955
956 /* See breakpoint.h. */
957
958 void
959 set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
960 bool force)
961 {
962 for (breakpoint *b : all_breakpoints ())
963 if (b->number == bpnum)
964 {
965 /* Check if this breakpoint has a "stop" method implemented in an
966 extension language. This method and conditions entered into GDB
967 from the CLI are mutually exclusive. */
968 const struct extension_language_defn *extlang
969 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
970
971 if (extlang != NULL)
972 {
973 error (_("Only one stop condition allowed. There is currently"
974 " a %s stop condition defined for this breakpoint."),
975 ext_lang_capitalized_name (extlang));
976 }
977 set_breakpoint_condition (b, exp, from_tty, force);
978
979 if (is_breakpoint (b))
980 update_global_location_list (UGLL_MAY_INSERT);
981
982 return;
983 }
984
985 error (_("No breakpoint number %d."), bpnum);
986 }
987
988 /* The options for the "condition" command. */
989
990 struct condition_command_opts
991 {
992 /* For "-force". */
993 bool force_condition = false;
994 };
995
996 static const gdb::option::option_def condition_command_option_defs[] = {
997
998 gdb::option::flag_option_def<condition_command_opts> {
999 "force",
1000 [] (condition_command_opts *opts) { return &opts->force_condition; },
1001 N_("Set the condition even if it is invalid for all current locations."),
1002 },
1003
1004 };
1005
1006 /* Create an option_def_group for the "condition" options, with
1007 CC_OPTS as context. */
1008
1009 static inline gdb::option::option_def_group
1010 make_condition_command_options_def_group (condition_command_opts *cc_opts)
1011 {
1012 return {{condition_command_option_defs}, cc_opts};
1013 }
1014
1015 /* Completion for the "condition" command. */
1016
1017 static void
1018 condition_completer (struct cmd_list_element *cmd,
1019 completion_tracker &tracker,
1020 const char *text, const char * /*word*/)
1021 {
1022 bool has_no_arguments = (*text == '\0');
1023 condition_command_opts cc_opts;
1024 const auto group = make_condition_command_options_def_group (&cc_opts);
1025 if (gdb::option::complete_options
1026 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1027 return;
1028
1029 text = skip_spaces (text);
1030 const char *space = skip_to_space (text);
1031 if (*space == '\0')
1032 {
1033 int len;
1034
1035 if (text[0] == '$')
1036 {
1037 tracker.advance_custom_word_point_by (1);
1038 /* We don't support completion of history indices. */
1039 if (!isdigit (text[1]))
1040 complete_internalvar (tracker, &text[1]);
1041 return;
1042 }
1043
1044 /* Suggest the "-force" flag if no arguments are given. If
1045 arguments were passed, they either already include the flag,
1046 or we are beyond the point of suggesting it because it's
1047 positionally the first argument. */
1048 if (has_no_arguments)
1049 gdb::option::complete_on_all_options (tracker, group);
1050
1051 /* We're completing the breakpoint number. */
1052 len = strlen (text);
1053
1054 for (breakpoint *b : all_breakpoints ())
1055 {
1056 char number[50];
1057
1058 xsnprintf (number, sizeof (number), "%d", b->number);
1059
1060 if (strncmp (number, text, len) == 0)
1061 tracker.add_completion (make_unique_xstrdup (number));
1062 }
1063
1064 return;
1065 }
1066
1067 /* We're completing the expression part. Skip the breakpoint num. */
1068 const char *exp_start = skip_spaces (space);
1069 tracker.advance_custom_word_point_by (exp_start - text);
1070 text = exp_start;
1071 const char *word = advance_to_expression_complete_word_point (tracker, text);
1072 expression_completer (cmd, tracker, text, word);
1073 }
1074
1075 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1076
1077 static void
1078 condition_command (const char *arg, int from_tty)
1079 {
1080 const char *p;
1081 int bnum;
1082
1083 if (arg == 0)
1084 error_no_arg (_("breakpoint number"));
1085
1086 p = arg;
1087
1088 /* Check if the "-force" flag was passed. */
1089 condition_command_opts cc_opts;
1090 const auto group = make_condition_command_options_def_group (&cc_opts);
1091 gdb::option::process_options
1092 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
1093
1094 bnum = get_number (&p);
1095 if (bnum == 0)
1096 error (_("Bad breakpoint argument: '%s'"), arg);
1097
1098 set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
1099 }
1100
1101 /* Check that COMMAND do not contain commands that are suitable
1102 only for tracepoints and not suitable for ordinary breakpoints.
1103 Throw if any such commands is found. */
1104
1105 static void
1106 check_no_tracepoint_commands (struct command_line *commands)
1107 {
1108 struct command_line *c;
1109
1110 for (c = commands; c; c = c->next)
1111 {
1112 if (c->control_type == while_stepping_control)
1113 error (_("The 'while-stepping' command can "
1114 "only be used for tracepoints"));
1115
1116 check_no_tracepoint_commands (c->body_list_0.get ());
1117 check_no_tracepoint_commands (c->body_list_1.get ());
1118
1119 /* Not that command parsing removes leading whitespace and comment
1120 lines and also empty lines. So, we only need to check for
1121 command directly. */
1122 if (strstr (c->line, "collect ") == c->line)
1123 error (_("The 'collect' command can only be used for tracepoints"));
1124
1125 if (strstr (c->line, "teval ") == c->line)
1126 error (_("The 'teval' command can only be used for tracepoints"));
1127 }
1128 }
1129
1130 struct longjmp_breakpoint : public breakpoint
1131 {
1132 ~longjmp_breakpoint () override;
1133 };
1134
1135 /* Encapsulate tests for different types of tracepoints. */
1136
1137 static bool
1138 is_tracepoint_type (bptype type)
1139 {
1140 return (type == bp_tracepoint
1141 || type == bp_fast_tracepoint
1142 || type == bp_static_tracepoint);
1143 }
1144
1145 static bool
1146 is_longjmp_type (bptype type)
1147 {
1148 return type == bp_longjmp || type == bp_exception;
1149 }
1150
1151 /* See breakpoint.h. */
1152
1153 bool
1154 is_tracepoint (const struct breakpoint *b)
1155 {
1156 return is_tracepoint_type (b->type);
1157 }
1158
1159 /* Factory function to create an appropriate instance of breakpoint given
1160 TYPE. */
1161
1162 static std::unique_ptr<breakpoint>
1163 new_breakpoint_from_type (bptype type)
1164 {
1165 breakpoint *b;
1166
1167 if (is_tracepoint_type (type))
1168 b = new tracepoint ();
1169 else if (is_longjmp_type (type))
1170 b = new longjmp_breakpoint ();
1171 else
1172 b = new breakpoint ();
1173
1174 return std::unique_ptr<breakpoint> (b);
1175 }
1176
1177 /* A helper function that validates that COMMANDS are valid for a
1178 breakpoint. This function will throw an exception if a problem is
1179 found. */
1180
1181 static void
1182 validate_commands_for_breakpoint (struct breakpoint *b,
1183 struct command_line *commands)
1184 {
1185 if (is_tracepoint (b))
1186 {
1187 struct tracepoint *t = (struct tracepoint *) b;
1188 struct command_line *c;
1189 struct command_line *while_stepping = 0;
1190
1191 /* Reset the while-stepping step count. The previous commands
1192 might have included a while-stepping action, while the new
1193 ones might not. */
1194 t->step_count = 0;
1195
1196 /* We need to verify that each top-level element of commands is
1197 valid for tracepoints, that there's at most one
1198 while-stepping element, and that the while-stepping's body
1199 has valid tracing commands excluding nested while-stepping.
1200 We also need to validate the tracepoint action line in the
1201 context of the tracepoint --- validate_actionline actually
1202 has side effects, like setting the tracepoint's
1203 while-stepping STEP_COUNT, in addition to checking if the
1204 collect/teval actions parse and make sense in the
1205 tracepoint's context. */
1206 for (c = commands; c; c = c->next)
1207 {
1208 if (c->control_type == while_stepping_control)
1209 {
1210 if (b->type == bp_fast_tracepoint)
1211 error (_("The 'while-stepping' command "
1212 "cannot be used for fast tracepoint"));
1213 else if (b->type == bp_static_tracepoint)
1214 error (_("The 'while-stepping' command "
1215 "cannot be used for static tracepoint"));
1216
1217 if (while_stepping)
1218 error (_("The 'while-stepping' command "
1219 "can be used only once"));
1220 else
1221 while_stepping = c;
1222 }
1223
1224 validate_actionline (c->line, b);
1225 }
1226 if (while_stepping)
1227 {
1228 struct command_line *c2;
1229
1230 gdb_assert (while_stepping->body_list_1 == nullptr);
1231 c2 = while_stepping->body_list_0.get ();
1232 for (; c2; c2 = c2->next)
1233 {
1234 if (c2->control_type == while_stepping_control)
1235 error (_("The 'while-stepping' command cannot be nested"));
1236 }
1237 }
1238 }
1239 else
1240 {
1241 check_no_tracepoint_commands (commands);
1242 }
1243 }
1244
1245 /* Return a vector of all the static tracepoints set at ADDR. The
1246 caller is responsible for releasing the vector. */
1247
1248 std::vector<breakpoint *>
1249 static_tracepoints_here (CORE_ADDR addr)
1250 {
1251 std::vector<breakpoint *> found;
1252
1253 for (breakpoint *b : all_breakpoints ())
1254 if (b->type == bp_static_tracepoint)
1255 {
1256 for (bp_location *loc : b->locations ())
1257 if (loc->address == addr)
1258 found.push_back (b);
1259 }
1260
1261 return found;
1262 }
1263
1264 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1265 validate that only allowed commands are included. */
1266
1267 void
1268 breakpoint_set_commands (struct breakpoint *b,
1269 counted_command_line &&commands)
1270 {
1271 validate_commands_for_breakpoint (b, commands.get ());
1272
1273 b->commands = std::move (commands);
1274 gdb::observers::breakpoint_modified.notify (b);
1275 }
1276
1277 /* Set the internal `silent' flag on the breakpoint. Note that this
1278 is not the same as the "silent" that may appear in the breakpoint's
1279 commands. */
1280
1281 void
1282 breakpoint_set_silent (struct breakpoint *b, int silent)
1283 {
1284 int old_silent = b->silent;
1285
1286 b->silent = silent;
1287 if (old_silent != silent)
1288 gdb::observers::breakpoint_modified.notify (b);
1289 }
1290
1291 /* Set the thread for this breakpoint. If THREAD is -1, make the
1292 breakpoint work for any thread. */
1293
1294 void
1295 breakpoint_set_thread (struct breakpoint *b, int thread)
1296 {
1297 int old_thread = b->thread;
1298
1299 b->thread = thread;
1300 if (old_thread != thread)
1301 gdb::observers::breakpoint_modified.notify (b);
1302 }
1303
1304 /* Set the task for this breakpoint. If TASK is 0, make the
1305 breakpoint work for any task. */
1306
1307 void
1308 breakpoint_set_task (struct breakpoint *b, int task)
1309 {
1310 int old_task = b->task;
1311
1312 b->task = task;
1313 if (old_task != task)
1314 gdb::observers::breakpoint_modified.notify (b);
1315 }
1316
1317 static void
1318 commands_command_1 (const char *arg, int from_tty,
1319 struct command_line *control)
1320 {
1321 counted_command_line cmd;
1322 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1323 NULL after the call to read_command_lines if the user provides an empty
1324 list of command by just typing "end". */
1325 bool cmd_read = false;
1326
1327 std::string new_arg;
1328
1329 if (arg == NULL || !*arg)
1330 {
1331 /* Argument not explicitly given. Synthesize it. */
1332 if (breakpoint_count - prev_breakpoint_count > 1)
1333 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1334 breakpoint_count);
1335 else if (breakpoint_count > 0)
1336 new_arg = string_printf ("%d", breakpoint_count);
1337 }
1338 else
1339 {
1340 /* Create a copy of ARG. This is needed because the "commands"
1341 command may be coming from a script. In that case, the read
1342 line buffer is going to be overwritten in the lambda of
1343 'map_breakpoint_numbers' below when reading the next line
1344 before we are are done parsing the breakpoint numbers. */
1345 new_arg = arg;
1346 }
1347 arg = new_arg.c_str ();
1348
1349 map_breakpoint_numbers
1350 (arg, [&] (breakpoint *b)
1351 {
1352 if (!cmd_read)
1353 {
1354 gdb_assert (cmd == NULL);
1355 if (control != NULL)
1356 cmd = control->body_list_0;
1357 else
1358 {
1359 std::string str
1360 = string_printf (_("Type commands for breakpoint(s) "
1361 "%s, one per line."),
1362 arg);
1363
1364 auto do_validate = [=] (const char *line)
1365 {
1366 validate_actionline (line, b);
1367 };
1368 gdb::function_view<void (const char *)> validator;
1369 if (is_tracepoint (b))
1370 validator = do_validate;
1371
1372 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1373 }
1374 cmd_read = true;
1375 }
1376
1377 /* If a breakpoint was on the list more than once, we don't need to
1378 do anything. */
1379 if (b->commands != cmd)
1380 {
1381 validate_commands_for_breakpoint (b, cmd.get ());
1382 b->commands = cmd;
1383 gdb::observers::breakpoint_modified.notify (b);
1384 }
1385 });
1386 }
1387
1388 static void
1389 commands_command (const char *arg, int from_tty)
1390 {
1391 commands_command_1 (arg, from_tty, NULL);
1392 }
1393
1394 /* Like commands_command, but instead of reading the commands from
1395 input stream, takes them from an already parsed command structure.
1396
1397 This is used by cli-script.c to DTRT with breakpoint commands
1398 that are part of if and while bodies. */
1399 enum command_control_type
1400 commands_from_control_command (const char *arg, struct command_line *cmd)
1401 {
1402 commands_command_1 (arg, 0, cmd);
1403 return simple_control;
1404 }
1405
1406 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1407
1408 static int
1409 bp_location_has_shadow (struct bp_location *bl)
1410 {
1411 if (bl->loc_type != bp_loc_software_breakpoint)
1412 return 0;
1413 if (!bl->inserted)
1414 return 0;
1415 if (bl->target_info.shadow_len == 0)
1416 /* BL isn't valid, or doesn't shadow memory. */
1417 return 0;
1418 return 1;
1419 }
1420
1421 /* Update BUF, which is LEN bytes read from the target address
1422 MEMADDR, by replacing a memory breakpoint with its shadowed
1423 contents.
1424
1425 If READBUF is not NULL, this buffer must not overlap with the of
1426 the breakpoint location's shadow_contents buffer. Otherwise, a
1427 failed assertion internal error will be raised. */
1428
1429 static void
1430 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1431 const gdb_byte *writebuf_org,
1432 ULONGEST memaddr, LONGEST len,
1433 struct bp_target_info *target_info,
1434 struct gdbarch *gdbarch)
1435 {
1436 /* Now do full processing of the found relevant range of elements. */
1437 CORE_ADDR bp_addr = 0;
1438 int bp_size = 0;
1439 int bptoffset = 0;
1440
1441 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1442 current_program_space->aspace, 0))
1443 {
1444 /* The breakpoint is inserted in a different address space. */
1445 return;
1446 }
1447
1448 /* Addresses and length of the part of the breakpoint that
1449 we need to copy. */
1450 bp_addr = target_info->placed_address;
1451 bp_size = target_info->shadow_len;
1452
1453 if (bp_addr + bp_size <= memaddr)
1454 {
1455 /* The breakpoint is entirely before the chunk of memory we are
1456 reading. */
1457 return;
1458 }
1459
1460 if (bp_addr >= memaddr + len)
1461 {
1462 /* The breakpoint is entirely after the chunk of memory we are
1463 reading. */
1464 return;
1465 }
1466
1467 /* Offset within shadow_contents. */
1468 if (bp_addr < memaddr)
1469 {
1470 /* Only copy the second part of the breakpoint. */
1471 bp_size -= memaddr - bp_addr;
1472 bptoffset = memaddr - bp_addr;
1473 bp_addr = memaddr;
1474 }
1475
1476 if (bp_addr + bp_size > memaddr + len)
1477 {
1478 /* Only copy the first part of the breakpoint. */
1479 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1480 }
1481
1482 if (readbuf != NULL)
1483 {
1484 /* Verify that the readbuf buffer does not overlap with the
1485 shadow_contents buffer. */
1486 gdb_assert (target_info->shadow_contents >= readbuf + len
1487 || readbuf >= (target_info->shadow_contents
1488 + target_info->shadow_len));
1489
1490 /* Update the read buffer with this inserted breakpoint's
1491 shadow. */
1492 memcpy (readbuf + bp_addr - memaddr,
1493 target_info->shadow_contents + bptoffset, bp_size);
1494 }
1495 else
1496 {
1497 const unsigned char *bp;
1498 CORE_ADDR addr = target_info->reqstd_address;
1499 int placed_size;
1500
1501 /* Update the shadow with what we want to write to memory. */
1502 memcpy (target_info->shadow_contents + bptoffset,
1503 writebuf_org + bp_addr - memaddr, bp_size);
1504
1505 /* Determine appropriate breakpoint contents and size for this
1506 address. */
1507 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1508
1509 /* Update the final write buffer with this inserted
1510 breakpoint's INSN. */
1511 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1512 }
1513 }
1514
1515 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1516 by replacing any memory breakpoints with their shadowed contents.
1517
1518 If READBUF is not NULL, this buffer must not overlap with any of
1519 the breakpoint location's shadow_contents buffers. Otherwise,
1520 a failed assertion internal error will be raised.
1521
1522 The range of shadowed area by each bp_location is:
1523 bl->address - bp_locations_placed_address_before_address_max
1524 up to bl->address + bp_locations_shadow_len_after_address_max
1525 The range we were requested to resolve shadows for is:
1526 memaddr ... memaddr + len
1527 Thus the safe cutoff boundaries for performance optimization are
1528 memaddr + len <= (bl->address
1529 - bp_locations_placed_address_before_address_max)
1530 and:
1531 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1532
1533 void
1534 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1535 const gdb_byte *writebuf_org,
1536 ULONGEST memaddr, LONGEST len)
1537 {
1538 /* Left boundary, right boundary and median element of our binary
1539 search. */
1540 unsigned bc_l, bc_r, bc;
1541
1542 /* Find BC_L which is a leftmost element which may affect BUF
1543 content. It is safe to report lower value but a failure to
1544 report higher one. */
1545
1546 bc_l = 0;
1547 bc_r = bp_locations.size ();
1548 while (bc_l + 1 < bc_r)
1549 {
1550 struct bp_location *bl;
1551
1552 bc = (bc_l + bc_r) / 2;
1553 bl = bp_locations[bc];
1554
1555 /* Check first BL->ADDRESS will not overflow due to the added
1556 constant. Then advance the left boundary only if we are sure
1557 the BC element can in no way affect the BUF content (MEMADDR
1558 to MEMADDR + LEN range).
1559
1560 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1561 offset so that we cannot miss a breakpoint with its shadow
1562 range tail still reaching MEMADDR. */
1563
1564 if ((bl->address + bp_locations_shadow_len_after_address_max
1565 >= bl->address)
1566 && (bl->address + bp_locations_shadow_len_after_address_max
1567 <= memaddr))
1568 bc_l = bc;
1569 else
1570 bc_r = bc;
1571 }
1572
1573 /* Due to the binary search above, we need to make sure we pick the
1574 first location that's at BC_L's address. E.g., if there are
1575 multiple locations at the same address, BC_L may end up pointing
1576 at a duplicate location, and miss the "master"/"inserted"
1577 location. Say, given locations L1, L2 and L3 at addresses A and
1578 B:
1579
1580 L1@A, L2@A, L3@B, ...
1581
1582 BC_L could end up pointing at location L2, while the "master"
1583 location could be L1. Since the `loc->inserted' flag is only set
1584 on "master" locations, we'd forget to restore the shadow of L1
1585 and L2. */
1586 while (bc_l > 0
1587 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1588 bc_l--;
1589
1590 /* Now do full processing of the found relevant range of elements. */
1591
1592 for (bc = bc_l; bc < bp_locations.size (); bc++)
1593 {
1594 struct bp_location *bl = bp_locations[bc];
1595
1596 /* bp_location array has BL->OWNER always non-NULL. */
1597 if (bl->owner->type == bp_none)
1598 warning (_("reading through apparently deleted breakpoint #%d?"),
1599 bl->owner->number);
1600
1601 /* Performance optimization: any further element can no longer affect BUF
1602 content. */
1603
1604 if (bl->address >= bp_locations_placed_address_before_address_max
1605 && (memaddr + len
1606 <= (bl->address
1607 - bp_locations_placed_address_before_address_max)))
1608 break;
1609
1610 if (!bp_location_has_shadow (bl))
1611 continue;
1612
1613 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1614 memaddr, len, &bl->target_info, bl->gdbarch);
1615 }
1616 }
1617
1618 /* See breakpoint.h. */
1619
1620 bool
1621 is_breakpoint (const struct breakpoint *bpt)
1622 {
1623 return (bpt->type == bp_breakpoint
1624 || bpt->type == bp_hardware_breakpoint
1625 || bpt->type == bp_dprintf);
1626 }
1627
1628 /* Return true if BPT is of any hardware watchpoint kind. */
1629
1630 static bool
1631 is_hardware_watchpoint (const struct breakpoint *bpt)
1632 {
1633 return (bpt->type == bp_hardware_watchpoint
1634 || bpt->type == bp_read_watchpoint
1635 || bpt->type == bp_access_watchpoint);
1636 }
1637
1638 /* See breakpoint.h. */
1639
1640 bool
1641 is_watchpoint (const struct breakpoint *bpt)
1642 {
1643 return (is_hardware_watchpoint (bpt)
1644 || bpt->type == bp_watchpoint);
1645 }
1646
1647 /* Returns true if the current thread and its running state are safe
1648 to evaluate or update watchpoint B. Watchpoints on local
1649 expressions need to be evaluated in the context of the thread that
1650 was current when the watchpoint was created, and, that thread needs
1651 to be stopped to be able to select the correct frame context.
1652 Watchpoints on global expressions can be evaluated on any thread,
1653 and in any state. It is presently left to the target allowing
1654 memory accesses when threads are running. */
1655
1656 static int
1657 watchpoint_in_thread_scope (struct watchpoint *b)
1658 {
1659 return (b->pspace == current_program_space
1660 && (b->watchpoint_thread == null_ptid
1661 || (inferior_ptid == b->watchpoint_thread
1662 && !inferior_thread ()->executing ())));
1663 }
1664
1665 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1666 associated bp_watchpoint_scope breakpoint. */
1667
1668 static void
1669 watchpoint_del_at_next_stop (struct watchpoint *w)
1670 {
1671 if (w->related_breakpoint != w)
1672 {
1673 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1674 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1675 w->related_breakpoint->disposition = disp_del_at_next_stop;
1676 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1677 w->related_breakpoint = w;
1678 }
1679 w->disposition = disp_del_at_next_stop;
1680 }
1681
1682 /* Extract a bitfield value from value VAL using the bit parameters contained in
1683 watchpoint W. */
1684
1685 static struct value *
1686 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1687 {
1688 struct value *bit_val;
1689
1690 if (val == NULL)
1691 return NULL;
1692
1693 bit_val = allocate_value (value_type (val));
1694
1695 unpack_value_bitfield (bit_val,
1696 w->val_bitpos,
1697 w->val_bitsize,
1698 value_contents_for_printing (val).data (),
1699 value_offset (val),
1700 val);
1701
1702 return bit_val;
1703 }
1704
1705 /* Allocate a dummy location and add it to B, which must be a software
1706 watchpoint. This is required because even if a software watchpoint
1707 is not watching any memory, bpstat_stop_status requires a location
1708 to be able to report stops. */
1709
1710 static void
1711 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1712 struct program_space *pspace)
1713 {
1714 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1715
1716 b->loc = allocate_bp_location (b);
1717 b->loc->pspace = pspace;
1718 b->loc->address = -1;
1719 b->loc->length = -1;
1720 }
1721
1722 /* Returns true if B is a software watchpoint that is not watching any
1723 memory (e.g., "watch $pc"). */
1724
1725 static bool
1726 is_no_memory_software_watchpoint (struct breakpoint *b)
1727 {
1728 return (b->type == bp_watchpoint
1729 && b->loc != NULL
1730 && b->loc->next == NULL
1731 && b->loc->address == -1
1732 && b->loc->length == -1);
1733 }
1734
1735 /* Assuming that B is a watchpoint:
1736 - Reparse watchpoint expression, if REPARSE is non-zero
1737 - Evaluate expression and store the result in B->val
1738 - Evaluate the condition if there is one, and store the result
1739 in b->loc->cond.
1740 - Update the list of values that must be watched in B->loc.
1741
1742 If the watchpoint disposition is disp_del_at_next_stop, then do
1743 nothing. If this is local watchpoint that is out of scope, delete
1744 it.
1745
1746 Even with `set breakpoint always-inserted on' the watchpoints are
1747 removed + inserted on each stop here. Normal breakpoints must
1748 never be removed because they might be missed by a running thread
1749 when debugging in non-stop mode. On the other hand, hardware
1750 watchpoints (is_hardware_watchpoint; processed here) are specific
1751 to each LWP since they are stored in each LWP's hardware debug
1752 registers. Therefore, such LWP must be stopped first in order to
1753 be able to modify its hardware watchpoints.
1754
1755 Hardware watchpoints must be reset exactly once after being
1756 presented to the user. It cannot be done sooner, because it would
1757 reset the data used to present the watchpoint hit to the user. And
1758 it must not be done later because it could display the same single
1759 watchpoint hit during multiple GDB stops. Note that the latter is
1760 relevant only to the hardware watchpoint types bp_read_watchpoint
1761 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1762 not user-visible - its hit is suppressed if the memory content has
1763 not changed.
1764
1765 The following constraints influence the location where we can reset
1766 hardware watchpoints:
1767
1768 * target_stopped_by_watchpoint and target_stopped_data_address are
1769 called several times when GDB stops.
1770
1771 [linux]
1772 * Multiple hardware watchpoints can be hit at the same time,
1773 causing GDB to stop. GDB only presents one hardware watchpoint
1774 hit at a time as the reason for stopping, and all the other hits
1775 are presented later, one after the other, each time the user
1776 requests the execution to be resumed. Execution is not resumed
1777 for the threads still having pending hit event stored in
1778 LWP_INFO->STATUS. While the watchpoint is already removed from
1779 the inferior on the first stop the thread hit event is kept being
1780 reported from its cached value by linux_nat_stopped_data_address
1781 until the real thread resume happens after the watchpoint gets
1782 presented and thus its LWP_INFO->STATUS gets reset.
1783
1784 Therefore the hardware watchpoint hit can get safely reset on the
1785 watchpoint removal from inferior. */
1786
1787 static void
1788 update_watchpoint (struct watchpoint *b, int reparse)
1789 {
1790 int within_current_scope;
1791 struct frame_id saved_frame_id;
1792 int frame_saved;
1793
1794 /* If this is a local watchpoint, we only want to check if the
1795 watchpoint frame is in scope if the current thread is the thread
1796 that was used to create the watchpoint. */
1797 if (!watchpoint_in_thread_scope (b))
1798 return;
1799
1800 if (b->disposition == disp_del_at_next_stop)
1801 return;
1802
1803 frame_saved = 0;
1804
1805 /* Determine if the watchpoint is within scope. */
1806 if (b->exp_valid_block == NULL)
1807 within_current_scope = 1;
1808 else
1809 {
1810 struct frame_info *fi = get_current_frame ();
1811 struct gdbarch *frame_arch = get_frame_arch (fi);
1812 CORE_ADDR frame_pc = get_frame_pc (fi);
1813
1814 /* If we're at a point where the stack has been destroyed
1815 (e.g. in a function epilogue), unwinding may not work
1816 properly. Do not attempt to recreate locations at this
1817 point. See similar comments in watchpoint_check. */
1818 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1819 return;
1820
1821 /* Save the current frame's ID so we can restore it after
1822 evaluating the watchpoint expression on its own frame. */
1823 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1824 took a frame parameter, so that we didn't have to change the
1825 selected frame. */
1826 frame_saved = 1;
1827 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1828
1829 fi = frame_find_by_id (b->watchpoint_frame);
1830 within_current_scope = (fi != NULL);
1831 if (within_current_scope)
1832 select_frame (fi);
1833 }
1834
1835 /* We don't free locations. They are stored in the bp_location array
1836 and update_global_location_list will eventually delete them and
1837 remove breakpoints if needed. */
1838 b->loc = NULL;
1839
1840 if (within_current_scope && reparse)
1841 {
1842 const char *s;
1843
1844 b->exp.reset ();
1845 s = (b->exp_string_reparse
1846 ? b->exp_string_reparse.get ()
1847 : b->exp_string.get ());
1848 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1849 /* If the meaning of expression itself changed, the old value is
1850 no longer relevant. We don't want to report a watchpoint hit
1851 to the user when the old value and the new value may actually
1852 be completely different objects. */
1853 b->val = NULL;
1854 b->val_valid = false;
1855
1856 /* Note that unlike with breakpoints, the watchpoint's condition
1857 expression is stored in the breakpoint object, not in the
1858 locations (re)created below. */
1859 if (b->cond_string != NULL)
1860 {
1861 b->cond_exp.reset ();
1862
1863 s = b->cond_string.get ();
1864 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1865 }
1866 }
1867
1868 /* If we failed to parse the expression, for example because
1869 it refers to a global variable in a not-yet-loaded shared library,
1870 don't try to insert watchpoint. We don't automatically delete
1871 such watchpoint, though, since failure to parse expression
1872 is different from out-of-scope watchpoint. */
1873 if (!target_has_execution ())
1874 {
1875 /* Without execution, memory can't change. No use to try and
1876 set watchpoint locations. The watchpoint will be reset when
1877 the target gains execution, through breakpoint_re_set. */
1878 if (!can_use_hw_watchpoints)
1879 {
1880 if (b->ops->works_in_software_mode (b))
1881 b->type = bp_watchpoint;
1882 else
1883 error (_("Can't set read/access watchpoint when "
1884 "hardware watchpoints are disabled."));
1885 }
1886 }
1887 else if (within_current_scope && b->exp)
1888 {
1889 std::vector<value_ref_ptr> val_chain;
1890 struct value *v, *result;
1891 struct program_space *frame_pspace;
1892
1893 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
1894 &val_chain, false);
1895
1896 /* Avoid setting b->val if it's already set. The meaning of
1897 b->val is 'the last value' user saw, and we should update
1898 it only if we reported that last value to user. As it
1899 happens, the code that reports it updates b->val directly.
1900 We don't keep track of the memory value for masked
1901 watchpoints. */
1902 if (!b->val_valid && !is_masked_watchpoint (b))
1903 {
1904 if (b->val_bitsize != 0)
1905 v = extract_bitfield_from_watchpoint_value (b, v);
1906 b->val = release_value (v);
1907 b->val_valid = true;
1908 }
1909
1910 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1911
1912 /* Look at each value on the value chain. */
1913 gdb_assert (!val_chain.empty ());
1914 for (const value_ref_ptr &iter : val_chain)
1915 {
1916 v = iter.get ();
1917
1918 /* If it's a memory location, and GDB actually needed
1919 its contents to evaluate the expression, then we
1920 must watch it. If the first value returned is
1921 still lazy, that means an error occurred reading it;
1922 watch it anyway in case it becomes readable. */
1923 if (VALUE_LVAL (v) == lval_memory
1924 && (v == val_chain[0] || ! value_lazy (v)))
1925 {
1926 struct type *vtype = check_typedef (value_type (v));
1927
1928 /* We only watch structs and arrays if user asked
1929 for it explicitly, never if they just happen to
1930 appear in the middle of some value chain. */
1931 if (v == result
1932 || (vtype->code () != TYPE_CODE_STRUCT
1933 && vtype->code () != TYPE_CODE_ARRAY))
1934 {
1935 CORE_ADDR addr;
1936 enum target_hw_bp_type type;
1937 struct bp_location *loc, **tmp;
1938 int bitpos = 0, bitsize = 0;
1939
1940 if (value_bitsize (v) != 0)
1941 {
1942 /* Extract the bit parameters out from the bitfield
1943 sub-expression. */
1944 bitpos = value_bitpos (v);
1945 bitsize = value_bitsize (v);
1946 }
1947 else if (v == result && b->val_bitsize != 0)
1948 {
1949 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1950 lvalue whose bit parameters are saved in the fields
1951 VAL_BITPOS and VAL_BITSIZE. */
1952 bitpos = b->val_bitpos;
1953 bitsize = b->val_bitsize;
1954 }
1955
1956 addr = value_address (v);
1957 if (bitsize != 0)
1958 {
1959 /* Skip the bytes that don't contain the bitfield. */
1960 addr += bitpos / 8;
1961 }
1962
1963 type = hw_write;
1964 if (b->type == bp_read_watchpoint)
1965 type = hw_read;
1966 else if (b->type == bp_access_watchpoint)
1967 type = hw_access;
1968
1969 loc = allocate_bp_location (b);
1970 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1971 ;
1972 *tmp = loc;
1973 loc->gdbarch = value_type (v)->arch ();
1974
1975 loc->pspace = frame_pspace;
1976 loc->address = address_significant (loc->gdbarch, addr);
1977
1978 if (bitsize != 0)
1979 {
1980 /* Just cover the bytes that make up the bitfield. */
1981 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1982 }
1983 else
1984 loc->length = TYPE_LENGTH (value_type (v));
1985
1986 loc->watchpoint_type = type;
1987 }
1988 }
1989 }
1990
1991 /* Change the type of breakpoint between hardware assisted or
1992 an ordinary watchpoint depending on the hardware support
1993 and free hardware slots. REPARSE is set when the inferior
1994 is started. */
1995 if (reparse)
1996 {
1997 int reg_cnt;
1998 enum bp_loc_type loc_type;
1999
2000 reg_cnt = can_use_hardware_watchpoint (val_chain);
2001
2002 if (reg_cnt)
2003 {
2004 int i, target_resources_ok, other_type_used;
2005 enum bptype type;
2006
2007 /* Use an exact watchpoint when there's only one memory region to be
2008 watched, and only one debug register is needed to watch it. */
2009 b->exact = target_exact_watchpoints && reg_cnt == 1;
2010
2011 /* We need to determine how many resources are already
2012 used for all other hardware watchpoints plus this one
2013 to see if we still have enough resources to also fit
2014 this watchpoint in as well. */
2015
2016 /* If this is a software watchpoint, we try to turn it
2017 to a hardware one -- count resources as if B was of
2018 hardware watchpoint type. */
2019 type = b->type;
2020 if (type == bp_watchpoint)
2021 type = bp_hardware_watchpoint;
2022
2023 /* This watchpoint may or may not have been placed on
2024 the list yet at this point (it won't be in the list
2025 if we're trying to create it for the first time,
2026 through watch_command), so always account for it
2027 manually. */
2028
2029 /* Count resources used by all watchpoints except B. */
2030 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
2031
2032 /* Add in the resources needed for B. */
2033 i += hw_watchpoint_use_count (b);
2034
2035 target_resources_ok
2036 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2037 if (target_resources_ok <= 0)
2038 {
2039 int sw_mode = b->ops->works_in_software_mode (b);
2040
2041 if (target_resources_ok == 0 && !sw_mode)
2042 error (_("Target does not support this type of "
2043 "hardware watchpoint."));
2044 else if (target_resources_ok < 0 && !sw_mode)
2045 error (_("There are not enough available hardware "
2046 "resources for this watchpoint."));
2047
2048 /* Downgrade to software watchpoint. */
2049 b->type = bp_watchpoint;
2050 }
2051 else
2052 {
2053 /* If this was a software watchpoint, we've just
2054 found we have enough resources to turn it to a
2055 hardware watchpoint. Otherwise, this is a
2056 nop. */
2057 b->type = type;
2058 }
2059 }
2060 else if (!b->ops->works_in_software_mode (b))
2061 {
2062 if (!can_use_hw_watchpoints)
2063 error (_("Can't set read/access watchpoint when "
2064 "hardware watchpoints are disabled."));
2065 else
2066 error (_("Expression cannot be implemented with "
2067 "read/access watchpoint."));
2068 }
2069 else
2070 b->type = bp_watchpoint;
2071
2072 loc_type = (b->type == bp_watchpoint? bp_loc_other
2073 : bp_loc_hardware_watchpoint);
2074 for (bp_location *bl : b->locations ())
2075 bl->loc_type = loc_type;
2076 }
2077
2078 /* If a software watchpoint is not watching any memory, then the
2079 above left it without any location set up. But,
2080 bpstat_stop_status requires a location to be able to report
2081 stops, so make sure there's at least a dummy one. */
2082 if (b->type == bp_watchpoint && b->loc == NULL)
2083 software_watchpoint_add_no_memory_location (b, frame_pspace);
2084 }
2085 else if (!within_current_scope)
2086 {
2087 printf_filtered (_("\
2088 Watchpoint %d deleted because the program has left the block\n\
2089 in which its expression is valid.\n"),
2090 b->number);
2091 watchpoint_del_at_next_stop (b);
2092 }
2093
2094 /* Restore the selected frame. */
2095 if (frame_saved)
2096 select_frame (frame_find_by_id (saved_frame_id));
2097 }
2098
2099
2100 /* Returns 1 iff breakpoint location should be
2101 inserted in the inferior. We don't differentiate the type of BL's owner
2102 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2103 breakpoint_ops is not defined, because in insert_bp_location,
2104 tracepoint's insert_location will not be called. */
2105 static int
2106 should_be_inserted (struct bp_location *bl)
2107 {
2108 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2109 return 0;
2110
2111 if (bl->owner->disposition == disp_del_at_next_stop)
2112 return 0;
2113
2114 if (!bl->enabled || bl->disabled_by_cond
2115 || bl->shlib_disabled || bl->duplicate)
2116 return 0;
2117
2118 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2119 return 0;
2120
2121 /* This is set for example, when we're attached to the parent of a
2122 vfork, and have detached from the child. The child is running
2123 free, and we expect it to do an exec or exit, at which point the
2124 OS makes the parent schedulable again (and the target reports
2125 that the vfork is done). Until the child is done with the shared
2126 memory region, do not insert breakpoints in the parent, otherwise
2127 the child could still trip on the parent's breakpoints. Since
2128 the parent is blocked anyway, it won't miss any breakpoint. */
2129 if (bl->pspace->breakpoints_not_allowed)
2130 return 0;
2131
2132 /* Don't insert a breakpoint if we're trying to step past its
2133 location, except if the breakpoint is a single-step breakpoint,
2134 and the breakpoint's thread is the thread which is stepping past
2135 a breakpoint. */
2136 if ((bl->loc_type == bp_loc_software_breakpoint
2137 || bl->loc_type == bp_loc_hardware_breakpoint)
2138 && stepping_past_instruction_at (bl->pspace->aspace,
2139 bl->address)
2140 /* The single-step breakpoint may be inserted at the location
2141 we're trying to step if the instruction branches to itself.
2142 However, the instruction won't be executed at all and it may
2143 break the semantics of the instruction, for example, the
2144 instruction is a conditional branch or updates some flags.
2145 We can't fix it unless GDB is able to emulate the instruction
2146 or switch to displaced stepping. */
2147 && !(bl->owner->type == bp_single_step
2148 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2149 {
2150 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2151 paddress (bl->gdbarch, bl->address));
2152 return 0;
2153 }
2154
2155 /* Don't insert watchpoints if we're trying to step past the
2156 instruction that triggered one. */
2157 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2158 && stepping_past_nonsteppable_watchpoint ())
2159 {
2160 infrun_debug_printf ("stepping past non-steppable watchpoint. "
2161 "skipping watchpoint at %s:%d",
2162 paddress (bl->gdbarch, bl->address), bl->length);
2163 return 0;
2164 }
2165
2166 return 1;
2167 }
2168
2169 /* Same as should_be_inserted but does the check assuming
2170 that the location is not duplicated. */
2171
2172 static int
2173 unduplicated_should_be_inserted (struct bp_location *bl)
2174 {
2175 int result;
2176 const int save_duplicate = bl->duplicate;
2177
2178 bl->duplicate = 0;
2179 result = should_be_inserted (bl);
2180 bl->duplicate = save_duplicate;
2181 return result;
2182 }
2183
2184 /* Parses a conditional described by an expression COND into an
2185 agent expression bytecode suitable for evaluation
2186 by the bytecode interpreter. Return NULL if there was
2187 any error during parsing. */
2188
2189 static agent_expr_up
2190 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2191 {
2192 if (cond == NULL)
2193 return NULL;
2194
2195 agent_expr_up aexpr;
2196
2197 /* We don't want to stop processing, so catch any errors
2198 that may show up. */
2199 try
2200 {
2201 aexpr = gen_eval_for_expr (scope, cond);
2202 }
2203
2204 catch (const gdb_exception_error &ex)
2205 {
2206 /* If we got here, it means the condition could not be parsed to a valid
2207 bytecode expression and thus can't be evaluated on the target's side.
2208 It's no use iterating through the conditions. */
2209 }
2210
2211 /* We have a valid agent expression. */
2212 return aexpr;
2213 }
2214
2215 /* Based on location BL, create a list of breakpoint conditions to be
2216 passed on to the target. If we have duplicated locations with different
2217 conditions, we will add such conditions to the list. The idea is that the
2218 target will evaluate the list of conditions and will only notify GDB when
2219 one of them is true. */
2220
2221 static void
2222 build_target_condition_list (struct bp_location *bl)
2223 {
2224 int null_condition_or_parse_error = 0;
2225 int modified = bl->needs_update;
2226
2227 /* Release conditions left over from a previous insert. */
2228 bl->target_info.conditions.clear ();
2229
2230 /* This is only meaningful if the target is
2231 evaluating conditions and if the user has
2232 opted for condition evaluation on the target's
2233 side. */
2234 if (gdb_evaluates_breakpoint_condition_p ()
2235 || !target_supports_evaluation_of_breakpoint_conditions ())
2236 return;
2237
2238 auto loc_range = all_bp_locations_at_addr (bl->address);
2239
2240 /* Do a first pass to check for locations with no assigned
2241 conditions or conditions that fail to parse to a valid agent
2242 expression bytecode. If any of these happen, then it's no use to
2243 send conditions to the target since this location will always
2244 trigger and generate a response back to GDB. Note we consider
2245 all locations at the same address irrespective of type, i.e.,
2246 even if the locations aren't considered duplicates (e.g.,
2247 software breakpoint and hardware breakpoint at the same
2248 address). */
2249 for (bp_location *loc : loc_range)
2250 {
2251 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2252 {
2253 if (modified)
2254 {
2255 /* Re-parse the conditions since something changed. In that
2256 case we already freed the condition bytecodes (see
2257 force_breakpoint_reinsertion). We just
2258 need to parse the condition to bytecodes again. */
2259 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2260 loc->cond.get ());
2261 }
2262
2263 /* If we have a NULL bytecode expression, it means something
2264 went wrong or we have a null condition expression. */
2265 if (!loc->cond_bytecode)
2266 {
2267 null_condition_or_parse_error = 1;
2268 break;
2269 }
2270 }
2271 }
2272
2273 /* If any of these happened, it means we will have to evaluate the conditions
2274 for the location's address on gdb's side. It is no use keeping bytecodes
2275 for all the other duplicate locations, thus we free all of them here.
2276
2277 This is so we have a finer control over which locations' conditions are
2278 being evaluated by GDB or the remote stub. */
2279 if (null_condition_or_parse_error)
2280 {
2281 for (bp_location *loc : loc_range)
2282 {
2283 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2284 {
2285 /* Only go as far as the first NULL bytecode is
2286 located. */
2287 if (!loc->cond_bytecode)
2288 return;
2289
2290 loc->cond_bytecode.reset ();
2291 }
2292 }
2293 }
2294
2295 /* No NULL conditions or failed bytecode generation. Build a
2296 condition list for this location's address. If we have software
2297 and hardware locations at the same address, they aren't
2298 considered duplicates, but we still marge all the conditions
2299 anyway, as it's simpler, and doesn't really make a practical
2300 difference. */
2301 for (bp_location *loc : loc_range)
2302 if (loc->cond
2303 && is_breakpoint (loc->owner)
2304 && loc->pspace->num == bl->pspace->num
2305 && loc->owner->enable_state == bp_enabled
2306 && loc->enabled
2307 && !loc->disabled_by_cond)
2308 {
2309 /* Add the condition to the vector. This will be used later
2310 to send the conditions to the target. */
2311 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2312 }
2313
2314 return;
2315 }
2316
2317 /* Parses a command described by string CMD into an agent expression
2318 bytecode suitable for evaluation by the bytecode interpreter.
2319 Return NULL if there was any error during parsing. */
2320
2321 static agent_expr_up
2322 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2323 {
2324 const char *cmdrest;
2325 const char *format_start, *format_end;
2326 struct gdbarch *gdbarch = get_current_arch ();
2327
2328 if (cmd == NULL)
2329 return NULL;
2330
2331 cmdrest = cmd;
2332
2333 if (*cmdrest == ',')
2334 ++cmdrest;
2335 cmdrest = skip_spaces (cmdrest);
2336
2337 if (*cmdrest++ != '"')
2338 error (_("No format string following the location"));
2339
2340 format_start = cmdrest;
2341
2342 format_pieces fpieces (&cmdrest);
2343
2344 format_end = cmdrest;
2345
2346 if (*cmdrest++ != '"')
2347 error (_("Bad format string, non-terminated '\"'."));
2348
2349 cmdrest = skip_spaces (cmdrest);
2350
2351 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2352 error (_("Invalid argument syntax"));
2353
2354 if (*cmdrest == ',')
2355 cmdrest++;
2356 cmdrest = skip_spaces (cmdrest);
2357
2358 /* For each argument, make an expression. */
2359
2360 std::vector<struct expression *> argvec;
2361 while (*cmdrest != '\0')
2362 {
2363 const char *cmd1;
2364
2365 cmd1 = cmdrest;
2366 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2367 argvec.push_back (expr.release ());
2368 cmdrest = cmd1;
2369 if (*cmdrest == ',')
2370 ++cmdrest;
2371 }
2372
2373 agent_expr_up aexpr;
2374
2375 /* We don't want to stop processing, so catch any errors
2376 that may show up. */
2377 try
2378 {
2379 aexpr = gen_printf (scope, gdbarch, 0, 0,
2380 format_start, format_end - format_start,
2381 argvec.size (), argvec.data ());
2382 }
2383 catch (const gdb_exception_error &ex)
2384 {
2385 /* If we got here, it means the command could not be parsed to a valid
2386 bytecode expression and thus can't be evaluated on the target's side.
2387 It's no use iterating through the other commands. */
2388 }
2389
2390 /* We have a valid agent expression, return it. */
2391 return aexpr;
2392 }
2393
2394 /* Based on location BL, create a list of breakpoint commands to be
2395 passed on to the target. If we have duplicated locations with
2396 different commands, we will add any such to the list. */
2397
2398 static void
2399 build_target_command_list (struct bp_location *bl)
2400 {
2401 int null_command_or_parse_error = 0;
2402 int modified = bl->needs_update;
2403
2404 /* Clear commands left over from a previous insert. */
2405 bl->target_info.tcommands.clear ();
2406
2407 if (!target_can_run_breakpoint_commands ())
2408 return;
2409
2410 /* For now, limit to agent-style dprintf breakpoints. */
2411 if (dprintf_style != dprintf_style_agent)
2412 return;
2413
2414 auto loc_range = all_bp_locations_at_addr (bl->address);
2415
2416 /* For now, if we have any location at the same address that isn't a
2417 dprintf, don't install the target-side commands, as that would
2418 make the breakpoint not be reported to the core, and we'd lose
2419 control. */
2420 for (bp_location *loc : loc_range)
2421 if (is_breakpoint (loc->owner)
2422 && loc->pspace->num == bl->pspace->num
2423 && loc->owner->type != bp_dprintf)
2424 return;
2425
2426 /* Do a first pass to check for locations with no assigned
2427 conditions or conditions that fail to parse to a valid agent expression
2428 bytecode. If any of these happen, then it's no use to send conditions
2429 to the target since this location will always trigger and generate a
2430 response back to GDB. */
2431 for (bp_location *loc : loc_range)
2432 {
2433 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2434 {
2435 if (modified)
2436 {
2437 /* Re-parse the commands since something changed. In that
2438 case we already freed the command bytecodes (see
2439 force_breakpoint_reinsertion). We just
2440 need to parse the command to bytecodes again. */
2441 loc->cmd_bytecode
2442 = parse_cmd_to_aexpr (bl->address,
2443 loc->owner->extra_string.get ());
2444 }
2445
2446 /* If we have a NULL bytecode expression, it means something
2447 went wrong or we have a null command expression. */
2448 if (!loc->cmd_bytecode)
2449 {
2450 null_command_or_parse_error = 1;
2451 break;
2452 }
2453 }
2454 }
2455
2456 /* If anything failed, then we're not doing target-side commands,
2457 and so clean up. */
2458 if (null_command_or_parse_error)
2459 {
2460 for (bp_location *loc : loc_range)
2461 if (is_breakpoint (loc->owner)
2462 && loc->pspace->num == bl->pspace->num)
2463 {
2464 /* Only go as far as the first NULL bytecode is
2465 located. */
2466 if (loc->cmd_bytecode == NULL)
2467 return;
2468
2469 loc->cmd_bytecode.reset ();
2470 }
2471 }
2472
2473 /* No NULL commands or failed bytecode generation. Build a command
2474 list for all duplicate locations at this location's address.
2475 Note that here we must care for whether the breakpoint location
2476 types are considered duplicates, otherwise, say, if we have a
2477 software and hardware location at the same address, the target
2478 could end up running the commands twice. For the moment, we only
2479 support targets-side commands with dprintf, but it doesn't hurt
2480 to be pedantically correct in case that changes. */
2481 for (bp_location *loc : loc_range)
2482 if (breakpoint_locations_match (bl, loc)
2483 && loc->owner->extra_string
2484 && is_breakpoint (loc->owner)
2485 && loc->pspace->num == bl->pspace->num
2486 && loc->owner->enable_state == bp_enabled
2487 && loc->enabled
2488 && !loc->disabled_by_cond)
2489 {
2490 /* Add the command to the vector. This will be used later
2491 to send the commands to the target. */
2492 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2493 }
2494
2495 bl->target_info.persist = 0;
2496 /* Maybe flag this location as persistent. */
2497 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2498 bl->target_info.persist = 1;
2499 }
2500
2501 /* Return the kind of breakpoint on address *ADDR. Get the kind
2502 of breakpoint according to ADDR except single-step breakpoint.
2503 Get the kind of single-step breakpoint according to the current
2504 registers state. */
2505
2506 static int
2507 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2508 {
2509 if (bl->owner->type == bp_single_step)
2510 {
2511 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2512 struct regcache *regcache;
2513
2514 regcache = get_thread_regcache (thr);
2515
2516 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2517 regcache, addr);
2518 }
2519 else
2520 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2521 }
2522
2523 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2524 location. Any error messages are printed to TMP_ERROR_STREAM; and
2525 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2526 Returns 0 for success, 1 if the bp_location type is not supported or
2527 -1 for failure.
2528
2529 NOTE drow/2003-09-09: This routine could be broken down to an
2530 object-style method for each breakpoint or catchpoint type. */
2531 static int
2532 insert_bp_location (struct bp_location *bl,
2533 struct ui_file *tmp_error_stream,
2534 int *disabled_breaks,
2535 int *hw_breakpoint_error,
2536 int *hw_bp_error_explained_already)
2537 {
2538 gdb_exception bp_excpt;
2539
2540 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2541 return 0;
2542
2543 /* Note we don't initialize bl->target_info, as that wipes out
2544 the breakpoint location's shadow_contents if the breakpoint
2545 is still inserted at that location. This in turn breaks
2546 target_read_memory which depends on these buffers when
2547 a memory read is requested at the breakpoint location:
2548 Once the target_info has been wiped, we fail to see that
2549 we have a breakpoint inserted at that address and thus
2550 read the breakpoint instead of returning the data saved in
2551 the breakpoint location's shadow contents. */
2552 bl->target_info.reqstd_address = bl->address;
2553 bl->target_info.placed_address_space = bl->pspace->aspace;
2554 bl->target_info.length = bl->length;
2555
2556 /* When working with target-side conditions, we must pass all the conditions
2557 for the same breakpoint address down to the target since GDB will not
2558 insert those locations. With a list of breakpoint conditions, the target
2559 can decide when to stop and notify GDB. */
2560
2561 if (is_breakpoint (bl->owner))
2562 {
2563 build_target_condition_list (bl);
2564 build_target_command_list (bl);
2565 /* Reset the modification marker. */
2566 bl->needs_update = 0;
2567 }
2568
2569 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2570 set at a read-only address, then a breakpoint location will have
2571 been changed to hardware breakpoint before we get here. If it is
2572 "off" however, error out before actually trying to insert the
2573 breakpoint, with a nicer error message. */
2574 if (bl->loc_type == bp_loc_software_breakpoint
2575 && !automatic_hardware_breakpoints)
2576 {
2577 mem_region *mr = lookup_mem_region (bl->address);
2578
2579 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2580 {
2581 fprintf_unfiltered (tmp_error_stream,
2582 _("Cannot insert breakpoint %d.\n"
2583 "Cannot set software breakpoint "
2584 "at read-only address %s\n"),
2585 bl->owner->number,
2586 paddress (bl->gdbarch, bl->address));
2587 return 1;
2588 }
2589 }
2590
2591 if (bl->loc_type == bp_loc_software_breakpoint
2592 || bl->loc_type == bp_loc_hardware_breakpoint)
2593 {
2594 /* First check to see if we have to handle an overlay. */
2595 if (overlay_debugging == ovly_off
2596 || bl->section == NULL
2597 || !(section_is_overlay (bl->section)))
2598 {
2599 /* No overlay handling: just set the breakpoint. */
2600 try
2601 {
2602 int val;
2603
2604 val = bl->owner->ops->insert_location (bl);
2605 if (val)
2606 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2607 }
2608 catch (gdb_exception &e)
2609 {
2610 bp_excpt = std::move (e);
2611 }
2612 }
2613 else
2614 {
2615 /* This breakpoint is in an overlay section.
2616 Shall we set a breakpoint at the LMA? */
2617 if (!overlay_events_enabled)
2618 {
2619 /* Yes -- overlay event support is not active,
2620 so we must try to set a breakpoint at the LMA.
2621 This will not work for a hardware breakpoint. */
2622 if (bl->loc_type == bp_loc_hardware_breakpoint)
2623 warning (_("hardware breakpoint %d not supported in overlay!"),
2624 bl->owner->number);
2625 else
2626 {
2627 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2628 bl->section);
2629 /* Set a software (trap) breakpoint at the LMA. */
2630 bl->overlay_target_info = bl->target_info;
2631 bl->overlay_target_info.reqstd_address = addr;
2632
2633 /* No overlay handling: just set the breakpoint. */
2634 try
2635 {
2636 int val;
2637
2638 bl->overlay_target_info.kind
2639 = breakpoint_kind (bl, &addr);
2640 bl->overlay_target_info.placed_address = addr;
2641 val = target_insert_breakpoint (bl->gdbarch,
2642 &bl->overlay_target_info);
2643 if (val)
2644 bp_excpt
2645 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2646 }
2647 catch (gdb_exception &e)
2648 {
2649 bp_excpt = std::move (e);
2650 }
2651
2652 if (bp_excpt.reason != 0)
2653 fprintf_unfiltered (tmp_error_stream,
2654 "Overlay breakpoint %d "
2655 "failed: in ROM?\n",
2656 bl->owner->number);
2657 }
2658 }
2659 /* Shall we set a breakpoint at the VMA? */
2660 if (section_is_mapped (bl->section))
2661 {
2662 /* Yes. This overlay section is mapped into memory. */
2663 try
2664 {
2665 int val;
2666
2667 val = bl->owner->ops->insert_location (bl);
2668 if (val)
2669 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2670 }
2671 catch (gdb_exception &e)
2672 {
2673 bp_excpt = std::move (e);
2674 }
2675 }
2676 else
2677 {
2678 /* No. This breakpoint will not be inserted.
2679 No error, but do not mark the bp as 'inserted'. */
2680 return 0;
2681 }
2682 }
2683
2684 if (bp_excpt.reason != 0)
2685 {
2686 /* Can't set the breakpoint. */
2687
2688 /* If the target has closed then it will have deleted any
2689 breakpoints inserted within the target inferior, as a result
2690 any further attempts to interact with the breakpoint objects
2691 is not possible. Just rethrow the error. */
2692 if (bp_excpt.error == TARGET_CLOSE_ERROR)
2693 throw bp_excpt;
2694 gdb_assert (bl->owner != nullptr);
2695
2696 /* In some cases, we might not be able to insert a
2697 breakpoint in a shared library that has already been
2698 removed, but we have not yet processed the shlib unload
2699 event. Unfortunately, some targets that implement
2700 breakpoint insertion themselves can't tell why the
2701 breakpoint insertion failed (e.g., the remote target
2702 doesn't define error codes), so we must treat generic
2703 errors as memory errors. */
2704 if (bp_excpt.reason == RETURN_ERROR
2705 && (bp_excpt.error == GENERIC_ERROR
2706 || bp_excpt.error == MEMORY_ERROR)
2707 && bl->loc_type == bp_loc_software_breakpoint
2708 && (solib_name_from_address (bl->pspace, bl->address)
2709 || shared_objfile_contains_address_p (bl->pspace,
2710 bl->address)))
2711 {
2712 /* See also: disable_breakpoints_in_shlibs. */
2713 bl->shlib_disabled = 1;
2714 gdb::observers::breakpoint_modified.notify (bl->owner);
2715 if (!*disabled_breaks)
2716 {
2717 fprintf_unfiltered (tmp_error_stream,
2718 "Cannot insert breakpoint %d.\n",
2719 bl->owner->number);
2720 fprintf_unfiltered (tmp_error_stream,
2721 "Temporarily disabling shared "
2722 "library breakpoints:\n");
2723 }
2724 *disabled_breaks = 1;
2725 fprintf_unfiltered (tmp_error_stream,
2726 "breakpoint #%d\n", bl->owner->number);
2727 return 0;
2728 }
2729 else
2730 {
2731 if (bl->loc_type == bp_loc_hardware_breakpoint)
2732 {
2733 *hw_breakpoint_error = 1;
2734 *hw_bp_error_explained_already = bp_excpt.message != NULL;
2735 fprintf_unfiltered (tmp_error_stream,
2736 "Cannot insert hardware breakpoint %d%s",
2737 bl->owner->number,
2738 bp_excpt.message ? ":" : ".\n");
2739 if (bp_excpt.message != NULL)
2740 fprintf_unfiltered (tmp_error_stream, "%s.\n",
2741 bp_excpt.what ());
2742 }
2743 else
2744 {
2745 if (bp_excpt.message == NULL)
2746 {
2747 std::string message
2748 = memory_error_message (TARGET_XFER_E_IO,
2749 bl->gdbarch, bl->address);
2750
2751 fprintf_unfiltered (tmp_error_stream,
2752 "Cannot insert breakpoint %d.\n"
2753 "%s\n",
2754 bl->owner->number, message.c_str ());
2755 }
2756 else
2757 {
2758 fprintf_unfiltered (tmp_error_stream,
2759 "Cannot insert breakpoint %d: %s\n",
2760 bl->owner->number,
2761 bp_excpt.what ());
2762 }
2763 }
2764 return 1;
2765
2766 }
2767 }
2768 else
2769 bl->inserted = 1;
2770
2771 return 0;
2772 }
2773
2774 else if (bl->loc_type == bp_loc_hardware_watchpoint
2775 /* NOTE drow/2003-09-08: This state only exists for removing
2776 watchpoints. It's not clear that it's necessary... */
2777 && bl->owner->disposition != disp_del_at_next_stop)
2778 {
2779 int val;
2780
2781 gdb_assert (bl->owner->ops != NULL
2782 && bl->owner->ops->insert_location != NULL);
2783
2784 val = bl->owner->ops->insert_location (bl);
2785
2786 /* If trying to set a read-watchpoint, and it turns out it's not
2787 supported, try emulating one with an access watchpoint. */
2788 if (val == 1 && bl->watchpoint_type == hw_read)
2789 {
2790 /* But don't try to insert it, if there's already another
2791 hw_access location that would be considered a duplicate
2792 of this one. */
2793 for (bp_location *loc : all_bp_locations ())
2794 if (loc != bl
2795 && loc->watchpoint_type == hw_access
2796 && watchpoint_locations_match (bl, loc))
2797 {
2798 bl->duplicate = 1;
2799 bl->inserted = 1;
2800 bl->target_info = loc->target_info;
2801 bl->watchpoint_type = hw_access;
2802 val = 0;
2803 break;
2804 }
2805
2806 if (val == 1)
2807 {
2808 bl->watchpoint_type = hw_access;
2809 val = bl->owner->ops->insert_location (bl);
2810
2811 if (val)
2812 /* Back to the original value. */
2813 bl->watchpoint_type = hw_read;
2814 }
2815 }
2816
2817 bl->inserted = (val == 0);
2818 }
2819
2820 else if (bl->owner->type == bp_catchpoint)
2821 {
2822 int val;
2823
2824 gdb_assert (bl->owner->ops != NULL
2825 && bl->owner->ops->insert_location != NULL);
2826
2827 val = bl->owner->ops->insert_location (bl);
2828 if (val)
2829 {
2830 bl->owner->enable_state = bp_disabled;
2831
2832 if (val == 1)
2833 warning (_("\
2834 Error inserting catchpoint %d: Your system does not support this type\n\
2835 of catchpoint."), bl->owner->number);
2836 else
2837 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2838 }
2839
2840 bl->inserted = (val == 0);
2841
2842 /* We've already printed an error message if there was a problem
2843 inserting this catchpoint, and we've disabled the catchpoint,
2844 so just return success. */
2845 return 0;
2846 }
2847
2848 return 0;
2849 }
2850
2851 /* This function is called when program space PSPACE is about to be
2852 deleted. It takes care of updating breakpoints to not reference
2853 PSPACE anymore. */
2854
2855 void
2856 breakpoint_program_space_exit (struct program_space *pspace)
2857 {
2858 /* Remove any breakpoint that was set through this program space. */
2859 for (breakpoint *b : all_breakpoints_safe ())
2860 if (b->pspace == pspace)
2861 delete_breakpoint (b);
2862
2863 /* Breakpoints set through other program spaces could have locations
2864 bound to PSPACE as well. Remove those. */
2865 for (bp_location *loc : all_bp_locations ())
2866 {
2867 struct bp_location *tmp;
2868
2869 if (loc->pspace == pspace)
2870 {
2871 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2872 if (loc->owner->loc == loc)
2873 loc->owner->loc = loc->next;
2874 else
2875 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2876 if (tmp->next == loc)
2877 {
2878 tmp->next = loc->next;
2879 break;
2880 }
2881 }
2882 }
2883
2884 /* Now update the global location list to permanently delete the
2885 removed locations above. */
2886 update_global_location_list (UGLL_DONT_INSERT);
2887 }
2888
2889 /* Make sure all breakpoints are inserted in inferior.
2890 Throws exception on any error.
2891 A breakpoint that is already inserted won't be inserted
2892 again, so calling this function twice is safe. */
2893 void
2894 insert_breakpoints (void)
2895 {
2896 for (breakpoint *bpt : all_breakpoints ())
2897 if (is_hardware_watchpoint (bpt))
2898 {
2899 struct watchpoint *w = (struct watchpoint *) bpt;
2900
2901 update_watchpoint (w, 0 /* don't reparse. */);
2902 }
2903
2904 /* Updating watchpoints creates new locations, so update the global
2905 location list. Explicitly tell ugll to insert locations and
2906 ignore breakpoints_always_inserted_mode. Also,
2907 update_global_location_list tries to "upgrade" software
2908 breakpoints to hardware breakpoints to handle "set breakpoint
2909 auto-hw", so we need to call it even if we don't have new
2910 locations. */
2911 update_global_location_list (UGLL_INSERT);
2912 }
2913
2914 /* This is used when we need to synch breakpoint conditions between GDB and the
2915 target. It is the case with deleting and disabling of breakpoints when using
2916 always-inserted mode. */
2917
2918 static void
2919 update_inserted_breakpoint_locations (void)
2920 {
2921 int error_flag = 0;
2922 int val = 0;
2923 int disabled_breaks = 0;
2924 int hw_breakpoint_error = 0;
2925 int hw_bp_details_reported = 0;
2926
2927 string_file tmp_error_stream;
2928
2929 /* Explicitly mark the warning -- this will only be printed if
2930 there was an error. */
2931 tmp_error_stream.puts ("Warning:\n");
2932
2933 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2934
2935 for (bp_location *bl : all_bp_locations ())
2936 {
2937 /* We only want to update software breakpoints and hardware
2938 breakpoints. */
2939 if (!is_breakpoint (bl->owner))
2940 continue;
2941
2942 /* We only want to update locations that are already inserted
2943 and need updating. This is to avoid unwanted insertion during
2944 deletion of breakpoints. */
2945 if (!bl->inserted || !bl->needs_update)
2946 continue;
2947
2948 switch_to_program_space_and_thread (bl->pspace);
2949
2950 /* For targets that support global breakpoints, there's no need
2951 to select an inferior to insert breakpoint to. In fact, even
2952 if we aren't attached to any process yet, we should still
2953 insert breakpoints. */
2954 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2955 && (inferior_ptid == null_ptid || !target_has_execution ()))
2956 continue;
2957
2958 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
2959 &hw_breakpoint_error, &hw_bp_details_reported);
2960 if (val)
2961 error_flag = val;
2962 }
2963
2964 if (error_flag)
2965 {
2966 target_terminal::ours_for_output ();
2967 error_stream (tmp_error_stream);
2968 }
2969 }
2970
2971 /* Used when starting or continuing the program. */
2972
2973 static void
2974 insert_breakpoint_locations (void)
2975 {
2976 int error_flag = 0;
2977 int val = 0;
2978 int disabled_breaks = 0;
2979 int hw_breakpoint_error = 0;
2980 int hw_bp_error_explained_already = 0;
2981
2982 string_file tmp_error_stream;
2983
2984 /* Explicitly mark the warning -- this will only be printed if
2985 there was an error. */
2986 tmp_error_stream.puts ("Warning:\n");
2987
2988 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2989
2990 for (bp_location *bl : all_bp_locations ())
2991 {
2992 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2993 continue;
2994
2995 /* There is no point inserting thread-specific breakpoints if
2996 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2997 has BL->OWNER always non-NULL. */
2998 if (bl->owner->thread != -1
2999 && !valid_global_thread_id (bl->owner->thread))
3000 continue;
3001
3002 switch_to_program_space_and_thread (bl->pspace);
3003
3004 /* For targets that support global breakpoints, there's no need
3005 to select an inferior to insert breakpoint to. In fact, even
3006 if we aren't attached to any process yet, we should still
3007 insert breakpoints. */
3008 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3009 && (inferior_ptid == null_ptid || !target_has_execution ()))
3010 continue;
3011
3012 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3013 &hw_breakpoint_error, &hw_bp_error_explained_already);
3014 if (val)
3015 error_flag = val;
3016 }
3017
3018 /* If we failed to insert all locations of a watchpoint, remove
3019 them, as half-inserted watchpoint is of limited use. */
3020 for (breakpoint *bpt : all_breakpoints ())
3021 {
3022 int some_failed = 0;
3023
3024 if (!is_hardware_watchpoint (bpt))
3025 continue;
3026
3027 if (!breakpoint_enabled (bpt))
3028 continue;
3029
3030 if (bpt->disposition == disp_del_at_next_stop)
3031 continue;
3032
3033 for (bp_location *loc : bpt->locations ())
3034 if (!loc->inserted && should_be_inserted (loc))
3035 {
3036 some_failed = 1;
3037 break;
3038 }
3039
3040 if (some_failed)
3041 {
3042 for (bp_location *loc : bpt->locations ())
3043 if (loc->inserted)
3044 remove_breakpoint (loc);
3045
3046 hw_breakpoint_error = 1;
3047 tmp_error_stream.printf ("Could not insert "
3048 "hardware watchpoint %d.\n",
3049 bpt->number);
3050 error_flag = -1;
3051 }
3052 }
3053
3054 if (error_flag)
3055 {
3056 /* If a hardware breakpoint or watchpoint was inserted, add a
3057 message about possibly exhausted resources. */
3058 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3059 {
3060 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3061 You may have requested too many hardware breakpoints/watchpoints.\n");
3062 }
3063 target_terminal::ours_for_output ();
3064 error_stream (tmp_error_stream);
3065 }
3066 }
3067
3068 /* Used when the program stops.
3069 Returns zero if successful, or non-zero if there was a problem
3070 removing a breakpoint location. */
3071
3072 int
3073 remove_breakpoints (void)
3074 {
3075 int val = 0;
3076
3077 for (bp_location *bl : all_bp_locations ())
3078 if (bl->inserted && !is_tracepoint (bl->owner))
3079 val |= remove_breakpoint (bl);
3080
3081 return val;
3082 }
3083
3084 /* When a thread exits, remove breakpoints that are related to
3085 that thread. */
3086
3087 static void
3088 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3089 {
3090 for (breakpoint *b : all_breakpoints_safe ())
3091 {
3092 if (b->thread == tp->global_num && user_breakpoint_p (b))
3093 {
3094 b->disposition = disp_del_at_next_stop;
3095
3096 printf_filtered (_("\
3097 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3098 b->number, print_thread_id (tp));
3099
3100 /* Hide it from the user. */
3101 b->number = 0;
3102 }
3103 }
3104 }
3105
3106 /* See breakpoint.h. */
3107
3108 void
3109 remove_breakpoints_inf (inferior *inf)
3110 {
3111 int val;
3112
3113 for (bp_location *bl : all_bp_locations ())
3114 {
3115 if (bl->pspace != inf->pspace)
3116 continue;
3117
3118 if (bl->inserted && !bl->target_info.persist)
3119 {
3120 val = remove_breakpoint (bl);
3121 if (val != 0)
3122 return;
3123 }
3124 }
3125 }
3126
3127 static int internal_breakpoint_number = -1;
3128
3129 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3130 If INTERNAL is non-zero, the breakpoint number will be populated
3131 from internal_breakpoint_number and that variable decremented.
3132 Otherwise the breakpoint number will be populated from
3133 breakpoint_count and that value incremented. Internal breakpoints
3134 do not set the internal var bpnum. */
3135 static void
3136 set_breakpoint_number (int internal, struct breakpoint *b)
3137 {
3138 if (internal)
3139 b->number = internal_breakpoint_number--;
3140 else
3141 {
3142 set_breakpoint_count (breakpoint_count + 1);
3143 b->number = breakpoint_count;
3144 }
3145 }
3146
3147 static struct breakpoint *
3148 create_internal_breakpoint (struct gdbarch *gdbarch,
3149 CORE_ADDR address, enum bptype type,
3150 const struct breakpoint_ops *ops)
3151 {
3152 symtab_and_line sal;
3153 sal.pc = address;
3154 sal.section = find_pc_overlay (sal.pc);
3155 sal.pspace = current_program_space;
3156
3157 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
3158 b->number = internal_breakpoint_number--;
3159 b->disposition = disp_donttouch;
3160
3161 return b;
3162 }
3163
3164 static const char *const longjmp_names[] =
3165 {
3166 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3167 };
3168 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3169
3170 /* Per-objfile data private to breakpoint.c. */
3171 struct breakpoint_objfile_data
3172 {
3173 /* Minimal symbol for "_ovly_debug_event" (if any). */
3174 struct bound_minimal_symbol overlay_msym {};
3175
3176 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3177 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
3178
3179 /* True if we have looked for longjmp probes. */
3180 int longjmp_searched = 0;
3181
3182 /* SystemTap probe points for longjmp (if any). These are non-owning
3183 references. */
3184 std::vector<probe *> longjmp_probes;
3185
3186 /* Minimal symbol for "std::terminate()" (if any). */
3187 struct bound_minimal_symbol terminate_msym {};
3188
3189 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3190 struct bound_minimal_symbol exception_msym {};
3191
3192 /* True if we have looked for exception probes. */
3193 int exception_searched = 0;
3194
3195 /* SystemTap probe points for unwinding (if any). These are non-owning
3196 references. */
3197 std::vector<probe *> exception_probes;
3198 };
3199
3200 static const struct objfile_key<breakpoint_objfile_data>
3201 breakpoint_objfile_key;
3202
3203 /* Minimal symbol not found sentinel. */
3204 static struct minimal_symbol msym_not_found;
3205
3206 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3207
3208 static int
3209 msym_not_found_p (const struct minimal_symbol *msym)
3210 {
3211 return msym == &msym_not_found;
3212 }
3213
3214 /* Return per-objfile data needed by breakpoint.c.
3215 Allocate the data if necessary. */
3216
3217 static struct breakpoint_objfile_data *
3218 get_breakpoint_objfile_data (struct objfile *objfile)
3219 {
3220 struct breakpoint_objfile_data *bp_objfile_data;
3221
3222 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3223 if (bp_objfile_data == NULL)
3224 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3225 return bp_objfile_data;
3226 }
3227
3228 static void
3229 create_overlay_event_breakpoint (void)
3230 {
3231 const char *const func_name = "_ovly_debug_event";
3232
3233 for (objfile *objfile : current_program_space->objfiles ())
3234 {
3235 struct breakpoint *b;
3236 struct breakpoint_objfile_data *bp_objfile_data;
3237 CORE_ADDR addr;
3238 struct explicit_location explicit_loc;
3239
3240 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3241
3242 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3243 continue;
3244
3245 if (bp_objfile_data->overlay_msym.minsym == NULL)
3246 {
3247 struct bound_minimal_symbol m;
3248
3249 m = lookup_minimal_symbol_text (func_name, objfile);
3250 if (m.minsym == NULL)
3251 {
3252 /* Avoid future lookups in this objfile. */
3253 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3254 continue;
3255 }
3256 bp_objfile_data->overlay_msym = m;
3257 }
3258
3259 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3260 b = create_internal_breakpoint (objfile->arch (), addr,
3261 bp_overlay_event,
3262 &internal_breakpoint_ops);
3263 initialize_explicit_location (&explicit_loc);
3264 explicit_loc.function_name = ASTRDUP (func_name);
3265 b->location = new_explicit_location (&explicit_loc);
3266
3267 if (overlay_debugging == ovly_auto)
3268 {
3269 b->enable_state = bp_enabled;
3270 overlay_events_enabled = 1;
3271 }
3272 else
3273 {
3274 b->enable_state = bp_disabled;
3275 overlay_events_enabled = 0;
3276 }
3277 }
3278 }
3279
3280 /* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3281 true if a breakpoint was installed. */
3282
3283 static bool
3284 create_longjmp_master_breakpoint_probe (objfile *objfile)
3285 {
3286 struct gdbarch *gdbarch = objfile->arch ();
3287 struct breakpoint_objfile_data *bp_objfile_data
3288 = get_breakpoint_objfile_data (objfile);
3289
3290 if (!bp_objfile_data->longjmp_searched)
3291 {
3292 std::vector<probe *> ret
3293 = find_probes_in_objfile (objfile, "libc", "longjmp");
3294
3295 if (!ret.empty ())
3296 {
3297 /* We are only interested in checking one element. */
3298 probe *p = ret[0];
3299
3300 if (!p->can_evaluate_arguments ())
3301 {
3302 /* We cannot use the probe interface here,
3303 because it does not know how to evaluate
3304 arguments. */
3305 ret.clear ();
3306 }
3307 }
3308 bp_objfile_data->longjmp_probes = ret;
3309 bp_objfile_data->longjmp_searched = 1;
3310 }
3311
3312 if (bp_objfile_data->longjmp_probes.empty ())
3313 return false;
3314
3315 for (probe *p : bp_objfile_data->longjmp_probes)
3316 {
3317 struct breakpoint *b;
3318
3319 b = create_internal_breakpoint (gdbarch,
3320 p->get_relocated_address (objfile),
3321 bp_longjmp_master,
3322 &internal_breakpoint_ops);
3323 b->location = new_probe_location ("-probe-stap libc:longjmp");
3324 b->enable_state = bp_disabled;
3325 }
3326
3327 return true;
3328 }
3329
3330 /* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3331 Return true if at least one breakpoint was installed. */
3332
3333 static bool
3334 create_longjmp_master_breakpoint_names (objfile *objfile)
3335 {
3336 struct gdbarch *gdbarch = objfile->arch ();
3337 if (!gdbarch_get_longjmp_target_p (gdbarch))
3338 return false;
3339
3340 struct breakpoint_objfile_data *bp_objfile_data
3341 = get_breakpoint_objfile_data (objfile);
3342 unsigned int installed_bp = 0;
3343
3344 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3345 {
3346 struct breakpoint *b;
3347 const char *func_name;
3348 CORE_ADDR addr;
3349 struct explicit_location explicit_loc;
3350
3351 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3352 continue;
3353
3354 func_name = longjmp_names[i];
3355 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3356 {
3357 struct bound_minimal_symbol m;
3358
3359 m = lookup_minimal_symbol_text (func_name, objfile);
3360 if (m.minsym == NULL)
3361 {
3362 /* Prevent future lookups in this objfile. */
3363 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3364 continue;
3365 }
3366 bp_objfile_data->longjmp_msym[i] = m;
3367 }
3368
3369 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3370 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3371 &internal_breakpoint_ops);
3372 initialize_explicit_location (&explicit_loc);
3373 explicit_loc.function_name = ASTRDUP (func_name);
3374 b->location = new_explicit_location (&explicit_loc);
3375 b->enable_state = bp_disabled;
3376 installed_bp++;
3377 }
3378
3379 return installed_bp > 0;
3380 }
3381
3382 /* Create a master longjmp breakpoint. */
3383
3384 static void
3385 create_longjmp_master_breakpoint (void)
3386 {
3387 scoped_restore_current_program_space restore_pspace;
3388
3389 for (struct program_space *pspace : program_spaces)
3390 {
3391 set_current_program_space (pspace);
3392
3393 for (objfile *obj : current_program_space->objfiles ())
3394 {
3395 /* Skip separate debug object, it's handled in the loop below. */
3396 if (obj->separate_debug_objfile_backlink != nullptr)
3397 continue;
3398
3399 /* Try a probe kind breakpoint on main objfile. */
3400 if (create_longjmp_master_breakpoint_probe (obj))
3401 continue;
3402
3403 /* Try longjmp_names kind breakpoints on main and separate_debug
3404 objfiles. */
3405 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3406 if (create_longjmp_master_breakpoint_names (debug_objfile))
3407 break;
3408 }
3409 }
3410 }
3411
3412 /* Create a master std::terminate breakpoint. */
3413 static void
3414 create_std_terminate_master_breakpoint (void)
3415 {
3416 const char *const func_name = "std::terminate()";
3417
3418 scoped_restore_current_program_space restore_pspace;
3419
3420 for (struct program_space *pspace : program_spaces)
3421 {
3422 CORE_ADDR addr;
3423
3424 set_current_program_space (pspace);
3425
3426 for (objfile *objfile : current_program_space->objfiles ())
3427 {
3428 struct breakpoint *b;
3429 struct breakpoint_objfile_data *bp_objfile_data;
3430 struct explicit_location explicit_loc;
3431
3432 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3433
3434 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3435 continue;
3436
3437 if (bp_objfile_data->terminate_msym.minsym == NULL)
3438 {
3439 struct bound_minimal_symbol m;
3440
3441 m = lookup_minimal_symbol (func_name, NULL, objfile);
3442 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3443 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3444 {
3445 /* Prevent future lookups in this objfile. */
3446 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3447 continue;
3448 }
3449 bp_objfile_data->terminate_msym = m;
3450 }
3451
3452 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3453 b = create_internal_breakpoint (objfile->arch (), addr,
3454 bp_std_terminate_master,
3455 &internal_breakpoint_ops);
3456 initialize_explicit_location (&explicit_loc);
3457 explicit_loc.function_name = ASTRDUP (func_name);
3458 b->location = new_explicit_location (&explicit_loc);
3459 b->enable_state = bp_disabled;
3460 }
3461 }
3462 }
3463
3464 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3465 probe. Return true if a breakpoint was installed. */
3466
3467 static bool
3468 create_exception_master_breakpoint_probe (objfile *objfile)
3469 {
3470 struct breakpoint *b;
3471 struct gdbarch *gdbarch;
3472 struct breakpoint_objfile_data *bp_objfile_data;
3473
3474 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3475
3476 /* We prefer the SystemTap probe point if it exists. */
3477 if (!bp_objfile_data->exception_searched)
3478 {
3479 std::vector<probe *> ret
3480 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3481
3482 if (!ret.empty ())
3483 {
3484 /* We are only interested in checking one element. */
3485 probe *p = ret[0];
3486
3487 if (!p->can_evaluate_arguments ())
3488 {
3489 /* We cannot use the probe interface here, because it does
3490 not know how to evaluate arguments. */
3491 ret.clear ();
3492 }
3493 }
3494 bp_objfile_data->exception_probes = ret;
3495 bp_objfile_data->exception_searched = 1;
3496 }
3497
3498 if (bp_objfile_data->exception_probes.empty ())
3499 return false;
3500
3501 gdbarch = objfile->arch ();
3502
3503 for (probe *p : bp_objfile_data->exception_probes)
3504 {
3505 b = create_internal_breakpoint (gdbarch,
3506 p->get_relocated_address (objfile),
3507 bp_exception_master,
3508 &internal_breakpoint_ops);
3509 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3510 b->enable_state = bp_disabled;
3511 }
3512
3513 return true;
3514 }
3515
3516 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3517 _Unwind_DebugHook. Return true if a breakpoint was installed. */
3518
3519 static bool
3520 create_exception_master_breakpoint_hook (objfile *objfile)
3521 {
3522 const char *const func_name = "_Unwind_DebugHook";
3523 struct breakpoint *b;
3524 struct gdbarch *gdbarch;
3525 struct breakpoint_objfile_data *bp_objfile_data;
3526 CORE_ADDR addr;
3527 struct explicit_location explicit_loc;
3528
3529 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3530
3531 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3532 return false;
3533
3534 gdbarch = objfile->arch ();
3535
3536 if (bp_objfile_data->exception_msym.minsym == NULL)
3537 {
3538 struct bound_minimal_symbol debug_hook;
3539
3540 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3541 if (debug_hook.minsym == NULL)
3542 {
3543 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3544 return false;
3545 }
3546
3547 bp_objfile_data->exception_msym = debug_hook;
3548 }
3549
3550 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3551 addr = gdbarch_convert_from_func_ptr_addr
3552 (gdbarch, addr, current_inferior ()->top_target ());
3553 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3554 &internal_breakpoint_ops);
3555 initialize_explicit_location (&explicit_loc);
3556 explicit_loc.function_name = ASTRDUP (func_name);
3557 b->location = new_explicit_location (&explicit_loc);
3558 b->enable_state = bp_disabled;
3559
3560 return true;
3561 }
3562
3563 /* Install a master breakpoint on the unwinder's debug hook. */
3564
3565 static void
3566 create_exception_master_breakpoint (void)
3567 {
3568 for (objfile *obj : current_program_space->objfiles ())
3569 {
3570 /* Skip separate debug object. */
3571 if (obj->separate_debug_objfile_backlink)
3572 continue;
3573
3574 /* Try a probe kind breakpoint. */
3575 if (create_exception_master_breakpoint_probe (obj))
3576 continue;
3577
3578 /* Iterate over main and separate debug objects and try an
3579 _Unwind_DebugHook kind breakpoint. */
3580 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3581 if (create_exception_master_breakpoint_hook (debug_objfile))
3582 break;
3583 }
3584 }
3585
3586 /* Does B have a location spec? */
3587
3588 static int
3589 breakpoint_event_location_empty_p (const struct breakpoint *b)
3590 {
3591 return b->location != NULL && event_location_empty_p (b->location.get ());
3592 }
3593
3594 void
3595 update_breakpoints_after_exec (void)
3596 {
3597 /* We're about to delete breakpoints from GDB's lists. If the
3598 INSERTED flag is true, GDB will try to lift the breakpoints by
3599 writing the breakpoints' "shadow contents" back into memory. The
3600 "shadow contents" are NOT valid after an exec, so GDB should not
3601 do that. Instead, the target is responsible from marking
3602 breakpoints out as soon as it detects an exec. We don't do that
3603 here instead, because there may be other attempts to delete
3604 breakpoints after detecting an exec and before reaching here. */
3605 for (bp_location *bploc : all_bp_locations ())
3606 if (bploc->pspace == current_program_space)
3607 gdb_assert (!bploc->inserted);
3608
3609 for (breakpoint *b : all_breakpoints_safe ())
3610 {
3611 if (b->pspace != current_program_space)
3612 continue;
3613
3614 /* Solib breakpoints must be explicitly reset after an exec(). */
3615 if (b->type == bp_shlib_event)
3616 {
3617 delete_breakpoint (b);
3618 continue;
3619 }
3620
3621 /* JIT breakpoints must be explicitly reset after an exec(). */
3622 if (b->type == bp_jit_event)
3623 {
3624 delete_breakpoint (b);
3625 continue;
3626 }
3627
3628 /* Thread event breakpoints must be set anew after an exec(),
3629 as must overlay event and longjmp master breakpoints. */
3630 if (b->type == bp_thread_event || b->type == bp_overlay_event
3631 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3632 || b->type == bp_exception_master)
3633 {
3634 delete_breakpoint (b);
3635 continue;
3636 }
3637
3638 /* Step-resume breakpoints are meaningless after an exec(). */
3639 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3640 {
3641 delete_breakpoint (b);
3642 continue;
3643 }
3644
3645 /* Just like single-step breakpoints. */
3646 if (b->type == bp_single_step)
3647 {
3648 delete_breakpoint (b);
3649 continue;
3650 }
3651
3652 /* Longjmp and longjmp-resume breakpoints are also meaningless
3653 after an exec. */
3654 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3655 || b->type == bp_longjmp_call_dummy
3656 || b->type == bp_exception || b->type == bp_exception_resume)
3657 {
3658 delete_breakpoint (b);
3659 continue;
3660 }
3661
3662 if (b->type == bp_catchpoint)
3663 {
3664 /* For now, none of the bp_catchpoint breakpoints need to
3665 do anything at this point. In the future, if some of
3666 the catchpoints need to something, we will need to add
3667 a new method, and call this method from here. */
3668 continue;
3669 }
3670
3671 /* bp_finish is a special case. The only way we ought to be able
3672 to see one of these when an exec() has happened, is if the user
3673 caught a vfork, and then said "finish". Ordinarily a finish just
3674 carries them to the call-site of the current callee, by setting
3675 a temporary bp there and resuming. But in this case, the finish
3676 will carry them entirely through the vfork & exec.
3677
3678 We don't want to allow a bp_finish to remain inserted now. But
3679 we can't safely delete it, 'cause finish_command has a handle to
3680 the bp on a bpstat, and will later want to delete it. There's a
3681 chance (and I've seen it happen) that if we delete the bp_finish
3682 here, that its storage will get reused by the time finish_command
3683 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3684 We really must allow finish_command to delete a bp_finish.
3685
3686 In the absence of a general solution for the "how do we know
3687 it's safe to delete something others may have handles to?"
3688 problem, what we'll do here is just uninsert the bp_finish, and
3689 let finish_command delete it.
3690
3691 (We know the bp_finish is "doomed" in the sense that it's
3692 momentary, and will be deleted as soon as finish_command sees
3693 the inferior stopped. So it doesn't matter that the bp's
3694 address is probably bogus in the new a.out, unlike e.g., the
3695 solib breakpoints.) */
3696
3697 if (b->type == bp_finish)
3698 {
3699 continue;
3700 }
3701
3702 /* Without a symbolic address, we have little hope of the
3703 pre-exec() address meaning the same thing in the post-exec()
3704 a.out. */
3705 if (breakpoint_event_location_empty_p (b))
3706 {
3707 delete_breakpoint (b);
3708 continue;
3709 }
3710 }
3711 }
3712
3713 int
3714 detach_breakpoints (ptid_t ptid)
3715 {
3716 int val = 0;
3717 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3718 struct inferior *inf = current_inferior ();
3719
3720 if (ptid.pid () == inferior_ptid.pid ())
3721 error (_("Cannot detach breakpoints of inferior_ptid"));
3722
3723 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3724 inferior_ptid = ptid;
3725 for (bp_location *bl : all_bp_locations ())
3726 {
3727 if (bl->pspace != inf->pspace)
3728 continue;
3729
3730 /* This function must physically remove breakpoints locations
3731 from the specified ptid, without modifying the breakpoint
3732 package's state. Locations of type bp_loc_other are only
3733 maintained at GDB side. So, there is no need to remove
3734 these bp_loc_other locations. Moreover, removing these
3735 would modify the breakpoint package's state. */
3736 if (bl->loc_type == bp_loc_other)
3737 continue;
3738
3739 if (bl->inserted)
3740 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3741 }
3742
3743 return val;
3744 }
3745
3746 /* Remove the breakpoint location BL from the current address space.
3747 Note that this is used to detach breakpoints from a child fork.
3748 When we get here, the child isn't in the inferior list, and neither
3749 do we have objects to represent its address space --- we should
3750 *not* look at bl->pspace->aspace here. */
3751
3752 static int
3753 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3754 {
3755 int val;
3756
3757 /* BL is never in moribund_locations by our callers. */
3758 gdb_assert (bl->owner != NULL);
3759
3760 /* The type of none suggests that owner is actually deleted.
3761 This should not ever happen. */
3762 gdb_assert (bl->owner->type != bp_none);
3763
3764 if (bl->loc_type == bp_loc_software_breakpoint
3765 || bl->loc_type == bp_loc_hardware_breakpoint)
3766 {
3767 /* "Normal" instruction breakpoint: either the standard
3768 trap-instruction bp (bp_breakpoint), or a
3769 bp_hardware_breakpoint. */
3770
3771 /* First check to see if we have to handle an overlay. */
3772 if (overlay_debugging == ovly_off
3773 || bl->section == NULL
3774 || !(section_is_overlay (bl->section)))
3775 {
3776 /* No overlay handling: just remove the breakpoint. */
3777
3778 /* If we're trying to uninsert a memory breakpoint that we
3779 know is set in a dynamic object that is marked
3780 shlib_disabled, then either the dynamic object was
3781 removed with "remove-symbol-file" or with
3782 "nosharedlibrary". In the former case, we don't know
3783 whether another dynamic object might have loaded over the
3784 breakpoint's address -- the user might well let us know
3785 about it next with add-symbol-file (the whole point of
3786 add-symbol-file is letting the user manually maintain a
3787 list of dynamically loaded objects). If we have the
3788 breakpoint's shadow memory, that is, this is a software
3789 breakpoint managed by GDB, check whether the breakpoint
3790 is still inserted in memory, to avoid overwriting wrong
3791 code with stale saved shadow contents. Note that HW
3792 breakpoints don't have shadow memory, as they're
3793 implemented using a mechanism that is not dependent on
3794 being able to modify the target's memory, and as such
3795 they should always be removed. */
3796 if (bl->shlib_disabled
3797 && bl->target_info.shadow_len != 0
3798 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3799 val = 0;
3800 else
3801 val = bl->owner->ops->remove_location (bl, reason);
3802 }
3803 else
3804 {
3805 /* This breakpoint is in an overlay section.
3806 Did we set a breakpoint at the LMA? */
3807 if (!overlay_events_enabled)
3808 {
3809 /* Yes -- overlay event support is not active, so we
3810 should have set a breakpoint at the LMA. Remove it.
3811 */
3812 /* Ignore any failures: if the LMA is in ROM, we will
3813 have already warned when we failed to insert it. */
3814 if (bl->loc_type == bp_loc_hardware_breakpoint)
3815 target_remove_hw_breakpoint (bl->gdbarch,
3816 &bl->overlay_target_info);
3817 else
3818 target_remove_breakpoint (bl->gdbarch,
3819 &bl->overlay_target_info,
3820 reason);
3821 }
3822 /* Did we set a breakpoint at the VMA?
3823 If so, we will have marked the breakpoint 'inserted'. */
3824 if (bl->inserted)
3825 {
3826 /* Yes -- remove it. Previously we did not bother to
3827 remove the breakpoint if the section had been
3828 unmapped, but let's not rely on that being safe. We
3829 don't know what the overlay manager might do. */
3830
3831 /* However, we should remove *software* breakpoints only
3832 if the section is still mapped, or else we overwrite
3833 wrong code with the saved shadow contents. */
3834 if (bl->loc_type == bp_loc_hardware_breakpoint
3835 || section_is_mapped (bl->section))
3836 val = bl->owner->ops->remove_location (bl, reason);
3837 else
3838 val = 0;
3839 }
3840 else
3841 {
3842 /* No -- not inserted, so no need to remove. No error. */
3843 val = 0;
3844 }
3845 }
3846
3847 /* In some cases, we might not be able to remove a breakpoint in
3848 a shared library that has already been removed, but we have
3849 not yet processed the shlib unload event. Similarly for an
3850 unloaded add-symbol-file object - the user might not yet have
3851 had the chance to remove-symbol-file it. shlib_disabled will
3852 be set if the library/object has already been removed, but
3853 the breakpoint hasn't been uninserted yet, e.g., after
3854 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3855 always-inserted mode. */
3856 if (val
3857 && (bl->loc_type == bp_loc_software_breakpoint
3858 && (bl->shlib_disabled
3859 || solib_name_from_address (bl->pspace, bl->address)
3860 || shared_objfile_contains_address_p (bl->pspace,
3861 bl->address))))
3862 val = 0;
3863
3864 if (val)
3865 return val;
3866 bl->inserted = (reason == DETACH_BREAKPOINT);
3867 }
3868 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3869 {
3870 gdb_assert (bl->owner->ops != NULL
3871 && bl->owner->ops->remove_location != NULL);
3872
3873 bl->inserted = (reason == DETACH_BREAKPOINT);
3874 bl->owner->ops->remove_location (bl, reason);
3875
3876 /* Failure to remove any of the hardware watchpoints comes here. */
3877 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3878 warning (_("Could not remove hardware watchpoint %d."),
3879 bl->owner->number);
3880 }
3881 else if (bl->owner->type == bp_catchpoint
3882 && breakpoint_enabled (bl->owner)
3883 && !bl->duplicate)
3884 {
3885 gdb_assert (bl->owner->ops != NULL
3886 && bl->owner->ops->remove_location != NULL);
3887
3888 val = bl->owner->ops->remove_location (bl, reason);
3889 if (val)
3890 return val;
3891
3892 bl->inserted = (reason == DETACH_BREAKPOINT);
3893 }
3894
3895 return 0;
3896 }
3897
3898 static int
3899 remove_breakpoint (struct bp_location *bl)
3900 {
3901 /* BL is never in moribund_locations by our callers. */
3902 gdb_assert (bl->owner != NULL);
3903
3904 /* The type of none suggests that owner is actually deleted.
3905 This should not ever happen. */
3906 gdb_assert (bl->owner->type != bp_none);
3907
3908 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3909
3910 switch_to_program_space_and_thread (bl->pspace);
3911
3912 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
3913 }
3914
3915 /* Clear the "inserted" flag in all breakpoints. */
3916
3917 void
3918 mark_breakpoints_out (void)
3919 {
3920 for (bp_location *bl : all_bp_locations ())
3921 if (bl->pspace == current_program_space)
3922 bl->inserted = 0;
3923 }
3924
3925 /* Clear the "inserted" flag in all breakpoints and delete any
3926 breakpoints which should go away between runs of the program.
3927
3928 Plus other such housekeeping that has to be done for breakpoints
3929 between runs.
3930
3931 Note: this function gets called at the end of a run (by
3932 generic_mourn_inferior) and when a run begins (by
3933 init_wait_for_inferior). */
3934
3935
3936
3937 void
3938 breakpoint_init_inferior (enum inf_context context)
3939 {
3940 struct program_space *pspace = current_program_space;
3941
3942 /* If breakpoint locations are shared across processes, then there's
3943 nothing to do. */
3944 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
3945 return;
3946
3947 mark_breakpoints_out ();
3948
3949 for (breakpoint *b : all_breakpoints_safe ())
3950 {
3951 if (b->loc && b->loc->pspace != pspace)
3952 continue;
3953
3954 switch (b->type)
3955 {
3956 case bp_call_dummy:
3957 case bp_longjmp_call_dummy:
3958
3959 /* If the call dummy breakpoint is at the entry point it will
3960 cause problems when the inferior is rerun, so we better get
3961 rid of it. */
3962
3963 case bp_watchpoint_scope:
3964
3965 /* Also get rid of scope breakpoints. */
3966
3967 case bp_shlib_event:
3968
3969 /* Also remove solib event breakpoints. Their addresses may
3970 have changed since the last time we ran the program.
3971 Actually we may now be debugging against different target;
3972 and so the solib backend that installed this breakpoint may
3973 not be used in by the target. E.g.,
3974
3975 (gdb) file prog-linux
3976 (gdb) run # native linux target
3977 ...
3978 (gdb) kill
3979 (gdb) file prog-win.exe
3980 (gdb) tar rem :9999 # remote Windows gdbserver.
3981 */
3982
3983 case bp_step_resume:
3984
3985 /* Also remove step-resume breakpoints. */
3986
3987 case bp_single_step:
3988
3989 /* Also remove single-step breakpoints. */
3990
3991 delete_breakpoint (b);
3992 break;
3993
3994 case bp_watchpoint:
3995 case bp_hardware_watchpoint:
3996 case bp_read_watchpoint:
3997 case bp_access_watchpoint:
3998 {
3999 struct watchpoint *w = (struct watchpoint *) b;
4000
4001 /* Likewise for watchpoints on local expressions. */
4002 if (w->exp_valid_block != NULL)
4003 delete_breakpoint (b);
4004 else
4005 {
4006 /* Get rid of existing locations, which are no longer
4007 valid. New ones will be created in
4008 update_watchpoint, when the inferior is restarted.
4009 The next update_global_location_list call will
4010 garbage collect them. */
4011 b->loc = NULL;
4012
4013 if (context == inf_starting)
4014 {
4015 /* Reset val field to force reread of starting value in
4016 insert_breakpoints. */
4017 w->val.reset (nullptr);
4018 w->val_valid = false;
4019 }
4020 }
4021 }
4022 break;
4023 default:
4024 break;
4025 }
4026 }
4027
4028 /* Get rid of the moribund locations. */
4029 for (bp_location *bl : moribund_locations)
4030 decref_bp_location (&bl);
4031 moribund_locations.clear ();
4032 }
4033
4034 /* These functions concern about actual breakpoints inserted in the
4035 target --- to e.g. check if we need to do decr_pc adjustment or if
4036 we need to hop over the bkpt --- so we check for address space
4037 match, not program space. */
4038
4039 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4040 exists at PC. It returns ordinary_breakpoint_here if it's an
4041 ordinary breakpoint, or permanent_breakpoint_here if it's a
4042 permanent breakpoint.
4043 - When continuing from a location with an ordinary breakpoint, we
4044 actually single step once before calling insert_breakpoints.
4045 - When continuing from a location with a permanent breakpoint, we
4046 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4047 the target, to advance the PC past the breakpoint. */
4048
4049 enum breakpoint_here
4050 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4051 {
4052 int any_breakpoint_here = 0;
4053
4054 for (bp_location *bl : all_bp_locations ())
4055 {
4056 if (bl->loc_type != bp_loc_software_breakpoint
4057 && bl->loc_type != bp_loc_hardware_breakpoint)
4058 continue;
4059
4060 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4061 if ((breakpoint_enabled (bl->owner)
4062 || bl->permanent)
4063 && breakpoint_location_address_match (bl, aspace, pc))
4064 {
4065 if (overlay_debugging
4066 && section_is_overlay (bl->section)
4067 && !section_is_mapped (bl->section))
4068 continue; /* unmapped overlay -- can't be a match */
4069 else if (bl->permanent)
4070 return permanent_breakpoint_here;
4071 else
4072 any_breakpoint_here = 1;
4073 }
4074 }
4075
4076 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4077 }
4078
4079 /* See breakpoint.h. */
4080
4081 int
4082 breakpoint_in_range_p (const address_space *aspace,
4083 CORE_ADDR addr, ULONGEST len)
4084 {
4085 for (bp_location *bl : all_bp_locations ())
4086 {
4087 if (bl->loc_type != bp_loc_software_breakpoint
4088 && bl->loc_type != bp_loc_hardware_breakpoint)
4089 continue;
4090
4091 if ((breakpoint_enabled (bl->owner)
4092 || bl->permanent)
4093 && breakpoint_location_address_range_overlap (bl, aspace,
4094 addr, len))
4095 {
4096 if (overlay_debugging
4097 && section_is_overlay (bl->section)
4098 && !section_is_mapped (bl->section))
4099 {
4100 /* Unmapped overlay -- can't be a match. */
4101 continue;
4102 }
4103
4104 return 1;
4105 }
4106 }
4107
4108 return 0;
4109 }
4110
4111 /* Return true if there's a moribund breakpoint at PC. */
4112
4113 int
4114 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4115 {
4116 for (bp_location *loc : moribund_locations)
4117 if (breakpoint_location_address_match (loc, aspace, pc))
4118 return 1;
4119
4120 return 0;
4121 }
4122
4123 /* Returns non-zero iff BL is inserted at PC, in address space
4124 ASPACE. */
4125
4126 static int
4127 bp_location_inserted_here_p (struct bp_location *bl,
4128 const address_space *aspace, CORE_ADDR pc)
4129 {
4130 if (bl->inserted
4131 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4132 aspace, pc))
4133 {
4134 if (overlay_debugging
4135 && section_is_overlay (bl->section)
4136 && !section_is_mapped (bl->section))
4137 return 0; /* unmapped overlay -- can't be a match */
4138 else
4139 return 1;
4140 }
4141 return 0;
4142 }
4143
4144 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4145
4146 int
4147 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4148 {
4149 for (bp_location *bl : all_bp_locations_at_addr (pc))
4150 {
4151 if (bl->loc_type != bp_loc_software_breakpoint
4152 && bl->loc_type != bp_loc_hardware_breakpoint)
4153 continue;
4154
4155 if (bp_location_inserted_here_p (bl, aspace, pc))
4156 return 1;
4157 }
4158 return 0;
4159 }
4160
4161 /* This function returns non-zero iff there is a software breakpoint
4162 inserted at PC. */
4163
4164 int
4165 software_breakpoint_inserted_here_p (const address_space *aspace,
4166 CORE_ADDR pc)
4167 {
4168 for (bp_location *bl : all_bp_locations_at_addr (pc))
4169 {
4170 if (bl->loc_type != bp_loc_software_breakpoint)
4171 continue;
4172
4173 if (bp_location_inserted_here_p (bl, aspace, pc))
4174 return 1;
4175 }
4176
4177 return 0;
4178 }
4179
4180 /* See breakpoint.h. */
4181
4182 int
4183 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4184 CORE_ADDR pc)
4185 {
4186 for (bp_location *bl : all_bp_locations_at_addr (pc))
4187 {
4188 if (bl->loc_type != bp_loc_hardware_breakpoint)
4189 continue;
4190
4191 if (bp_location_inserted_here_p (bl, aspace, pc))
4192 return 1;
4193 }
4194
4195 return 0;
4196 }
4197
4198 int
4199 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4200 CORE_ADDR addr, ULONGEST len)
4201 {
4202 for (breakpoint *bpt : all_breakpoints ())
4203 {
4204 if (bpt->type != bp_hardware_watchpoint
4205 && bpt->type != bp_access_watchpoint)
4206 continue;
4207
4208 if (!breakpoint_enabled (bpt))
4209 continue;
4210
4211 for (bp_location *loc : bpt->locations ())
4212 if (loc->pspace->aspace == aspace && loc->inserted)
4213 {
4214 CORE_ADDR l, h;
4215
4216 /* Check for intersection. */
4217 l = std::max<CORE_ADDR> (loc->address, addr);
4218 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4219 if (l < h)
4220 return 1;
4221 }
4222 }
4223 return 0;
4224 }
4225
4226 /* See breakpoint.h. */
4227
4228 bool
4229 is_catchpoint (struct breakpoint *b)
4230 {
4231 return (b->type == bp_catchpoint);
4232 }
4233
4234 /* Clear a bpstat so that it says we are not at any breakpoint.
4235 Also free any storage that is part of a bpstat. */
4236
4237 void
4238 bpstat_clear (bpstat **bsp)
4239 {
4240 bpstat *p;
4241 bpstat *q;
4242
4243 if (bsp == 0)
4244 return;
4245 p = *bsp;
4246 while (p != NULL)
4247 {
4248 q = p->next;
4249 delete p;
4250 p = q;
4251 }
4252 *bsp = NULL;
4253 }
4254
4255 bpstat::bpstat (const bpstat &other)
4256 : next (NULL),
4257 bp_location_at (other.bp_location_at),
4258 breakpoint_at (other.breakpoint_at),
4259 commands (other.commands),
4260 print (other.print),
4261 stop (other.stop),
4262 print_it (other.print_it)
4263 {
4264 if (other.old_val != NULL)
4265 old_val = release_value (value_copy (other.old_val.get ()));
4266 }
4267
4268 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4269 is part of the bpstat is copied as well. */
4270
4271 bpstat *
4272 bpstat_copy (bpstat *bs)
4273 {
4274 bpstat *p = nullptr;
4275 bpstat *tmp;
4276 bpstat *retval = nullptr;
4277
4278 if (bs == NULL)
4279 return bs;
4280
4281 for (; bs != NULL; bs = bs->next)
4282 {
4283 tmp = new bpstat (*bs);
4284
4285 if (p == NULL)
4286 /* This is the first thing in the chain. */
4287 retval = tmp;
4288 else
4289 p->next = tmp;
4290 p = tmp;
4291 }
4292 p->next = NULL;
4293 return retval;
4294 }
4295
4296 /* Find the bpstat associated with this breakpoint. */
4297
4298 bpstat *
4299 bpstat_find_breakpoint (bpstat *bsp, struct breakpoint *breakpoint)
4300 {
4301 if (bsp == NULL)
4302 return NULL;
4303
4304 for (; bsp != NULL; bsp = bsp->next)
4305 {
4306 if (bsp->breakpoint_at == breakpoint)
4307 return bsp;
4308 }
4309 return NULL;
4310 }
4311
4312 /* See breakpoint.h. */
4313
4314 bool
4315 bpstat_explains_signal (bpstat *bsp, enum gdb_signal sig)
4316 {
4317 for (; bsp != NULL; bsp = bsp->next)
4318 {
4319 if (bsp->breakpoint_at == NULL)
4320 {
4321 /* A moribund location can never explain a signal other than
4322 GDB_SIGNAL_TRAP. */
4323 if (sig == GDB_SIGNAL_TRAP)
4324 return true;
4325 }
4326 else
4327 {
4328 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4329 sig))
4330 return true;
4331 }
4332 }
4333
4334 return false;
4335 }
4336
4337 /* Put in *NUM the breakpoint number of the first breakpoint we are
4338 stopped at. *BSP upon return is a bpstat which points to the
4339 remaining breakpoints stopped at (but which is not guaranteed to be
4340 good for anything but further calls to bpstat_num).
4341
4342 Return 0 if passed a bpstat which does not indicate any breakpoints.
4343 Return -1 if stopped at a breakpoint that has been deleted since
4344 we set it.
4345 Return 1 otherwise. */
4346
4347 int
4348 bpstat_num (bpstat **bsp, int *num)
4349 {
4350 struct breakpoint *b;
4351
4352 if ((*bsp) == NULL)
4353 return 0; /* No more breakpoint values */
4354
4355 /* We assume we'll never have several bpstats that correspond to a
4356 single breakpoint -- otherwise, this function might return the
4357 same number more than once and this will look ugly. */
4358 b = (*bsp)->breakpoint_at;
4359 *bsp = (*bsp)->next;
4360 if (b == NULL)
4361 return -1; /* breakpoint that's been deleted since */
4362
4363 *num = b->number; /* We have its number */
4364 return 1;
4365 }
4366
4367 /* See breakpoint.h. */
4368
4369 void
4370 bpstat_clear_actions (void)
4371 {
4372 bpstat *bs;
4373
4374 if (inferior_ptid == null_ptid)
4375 return;
4376
4377 thread_info *tp = inferior_thread ();
4378 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4379 {
4380 bs->commands = NULL;
4381 bs->old_val.reset (nullptr);
4382 }
4383 }
4384
4385 /* Called when a command is about to proceed the inferior. */
4386
4387 static void
4388 breakpoint_about_to_proceed (void)
4389 {
4390 if (inferior_ptid != null_ptid)
4391 {
4392 struct thread_info *tp = inferior_thread ();
4393
4394 /* Allow inferior function calls in breakpoint commands to not
4395 interrupt the command list. When the call finishes
4396 successfully, the inferior will be standing at the same
4397 breakpoint as if nothing happened. */
4398 if (tp->control.in_infcall)
4399 return;
4400 }
4401
4402 breakpoint_proceeded = 1;
4403 }
4404
4405 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4406 or its equivalent. */
4407
4408 static int
4409 command_line_is_silent (struct command_line *cmd)
4410 {
4411 return cmd && (strcmp ("silent", cmd->line) == 0);
4412 }
4413
4414 /* Execute all the commands associated with all the breakpoints at
4415 this location. Any of these commands could cause the process to
4416 proceed beyond this point, etc. We look out for such changes by
4417 checking the global "breakpoint_proceeded" after each command.
4418
4419 Returns true if a breakpoint command resumed the inferior. In that
4420 case, it is the caller's responsibility to recall it again with the
4421 bpstat of the current thread. */
4422
4423 static int
4424 bpstat_do_actions_1 (bpstat **bsp)
4425 {
4426 bpstat *bs;
4427 int again = 0;
4428
4429 /* Avoid endless recursion if a `source' command is contained
4430 in bs->commands. */
4431 if (executing_breakpoint_commands)
4432 return 0;
4433
4434 scoped_restore save_executing
4435 = make_scoped_restore (&executing_breakpoint_commands, 1);
4436
4437 scoped_restore preventer = prevent_dont_repeat ();
4438
4439 /* This pointer will iterate over the list of bpstat's. */
4440 bs = *bsp;
4441
4442 breakpoint_proceeded = 0;
4443 for (; bs != NULL; bs = bs->next)
4444 {
4445 struct command_line *cmd = NULL;
4446
4447 /* Take ownership of the BSP's command tree, if it has one.
4448
4449 The command tree could legitimately contain commands like
4450 'step' and 'next', which call clear_proceed_status, which
4451 frees stop_bpstat's command tree. To make sure this doesn't
4452 free the tree we're executing out from under us, we need to
4453 take ownership of the tree ourselves. Since a given bpstat's
4454 commands are only executed once, we don't need to copy it; we
4455 can clear the pointer in the bpstat, and make sure we free
4456 the tree when we're done. */
4457 counted_command_line ccmd = bs->commands;
4458 bs->commands = NULL;
4459 if (ccmd != NULL)
4460 cmd = ccmd.get ();
4461 if (command_line_is_silent (cmd))
4462 {
4463 /* The action has been already done by bpstat_stop_status. */
4464 cmd = cmd->next;
4465 }
4466
4467 while (cmd != NULL)
4468 {
4469 execute_control_command (cmd);
4470
4471 if (breakpoint_proceeded)
4472 break;
4473 else
4474 cmd = cmd->next;
4475 }
4476
4477 if (breakpoint_proceeded)
4478 {
4479 if (current_ui->async)
4480 /* If we are in async mode, then the target might be still
4481 running, not stopped at any breakpoint, so nothing for
4482 us to do here -- just return to the event loop. */
4483 ;
4484 else
4485 /* In sync mode, when execute_control_command returns
4486 we're already standing on the next breakpoint.
4487 Breakpoint commands for that stop were not run, since
4488 execute_command does not run breakpoint commands --
4489 only command_line_handler does, but that one is not
4490 involved in execution of breakpoint commands. So, we
4491 can now execute breakpoint commands. It should be
4492 noted that making execute_command do bpstat actions is
4493 not an option -- in this case we'll have recursive
4494 invocation of bpstat for each breakpoint with a
4495 command, and can easily blow up GDB stack. Instead, we
4496 return true, which will trigger the caller to recall us
4497 with the new stop_bpstat. */
4498 again = 1;
4499 break;
4500 }
4501 }
4502 return again;
4503 }
4504
4505 /* Helper for bpstat_do_actions. Get the current thread, if there's
4506 one, is alive and has execution. Return NULL otherwise. */
4507
4508 static thread_info *
4509 get_bpstat_thread ()
4510 {
4511 if (inferior_ptid == null_ptid || !target_has_execution ())
4512 return NULL;
4513
4514 thread_info *tp = inferior_thread ();
4515 if (tp->state == THREAD_EXITED || tp->executing ())
4516 return NULL;
4517 return tp;
4518 }
4519
4520 void
4521 bpstat_do_actions (void)
4522 {
4523 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4524 thread_info *tp;
4525
4526 /* Do any commands attached to breakpoint we are stopped at. */
4527 while ((tp = get_bpstat_thread ()) != NULL)
4528 {
4529 /* Since in sync mode, bpstat_do_actions may resume the
4530 inferior, and only return when it is stopped at the next
4531 breakpoint, we keep doing breakpoint actions until it returns
4532 false to indicate the inferior was not resumed. */
4533 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4534 break;
4535 }
4536
4537 cleanup_if_error.release ();
4538 }
4539
4540 /* Print out the (old or new) value associated with a watchpoint. */
4541
4542 static void
4543 watchpoint_value_print (struct value *val, struct ui_file *stream)
4544 {
4545 if (val == NULL)
4546 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4547 else
4548 {
4549 struct value_print_options opts;
4550 get_user_print_options (&opts);
4551 value_print (val, stream, &opts);
4552 }
4553 }
4554
4555 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4556 debugging multiple threads. */
4557
4558 void
4559 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4560 {
4561 if (uiout->is_mi_like_p ())
4562 return;
4563
4564 uiout->text ("\n");
4565
4566 if (show_thread_that_caused_stop ())
4567 {
4568 struct thread_info *thr = inferior_thread ();
4569
4570 uiout->text ("Thread ");
4571 uiout->field_string ("thread-id", print_thread_id (thr));
4572
4573 const char *name = thread_name (thr);
4574 if (name != NULL)
4575 {
4576 uiout->text (" \"");
4577 uiout->field_string ("name", name);
4578 uiout->text ("\"");
4579 }
4580
4581 uiout->text (" hit ");
4582 }
4583 }
4584
4585 /* Generic routine for printing messages indicating why we
4586 stopped. The behavior of this function depends on the value
4587 'print_it' in the bpstat structure. Under some circumstances we
4588 may decide not to print anything here and delegate the task to
4589 normal_stop(). */
4590
4591 static enum print_stop_action
4592 print_bp_stop_message (bpstat *bs)
4593 {
4594 switch (bs->print_it)
4595 {
4596 case print_it_noop:
4597 /* Nothing should be printed for this bpstat entry. */
4598 return PRINT_UNKNOWN;
4599 break;
4600
4601 case print_it_done:
4602 /* We still want to print the frame, but we already printed the
4603 relevant messages. */
4604 return PRINT_SRC_AND_LOC;
4605 break;
4606
4607 case print_it_normal:
4608 {
4609 struct breakpoint *b = bs->breakpoint_at;
4610
4611 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4612 which has since been deleted. */
4613 if (b == NULL)
4614 return PRINT_UNKNOWN;
4615
4616 /* Normal case. Call the breakpoint's print_it method. */
4617 return b->ops->print_it (bs);
4618 }
4619 break;
4620
4621 default:
4622 internal_error (__FILE__, __LINE__,
4623 _("print_bp_stop_message: unrecognized enum value"));
4624 break;
4625 }
4626 }
4627
4628 /* A helper function that prints a shared library stopped event. */
4629
4630 static void
4631 print_solib_event (int is_catchpoint)
4632 {
4633 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4634 bool any_added = !current_program_space->added_solibs.empty ();
4635
4636 if (!is_catchpoint)
4637 {
4638 if (any_added || any_deleted)
4639 current_uiout->text (_("Stopped due to shared library event:\n"));
4640 else
4641 current_uiout->text (_("Stopped due to shared library event (no "
4642 "libraries added or removed)\n"));
4643 }
4644
4645 if (current_uiout->is_mi_like_p ())
4646 current_uiout->field_string ("reason",
4647 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4648
4649 if (any_deleted)
4650 {
4651 current_uiout->text (_(" Inferior unloaded "));
4652 ui_out_emit_list list_emitter (current_uiout, "removed");
4653 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4654 {
4655 const std::string &name = current_program_space->deleted_solibs[ix];
4656
4657 if (ix > 0)
4658 current_uiout->text (" ");
4659 current_uiout->field_string ("library", name);
4660 current_uiout->text ("\n");
4661 }
4662 }
4663
4664 if (any_added)
4665 {
4666 current_uiout->text (_(" Inferior loaded "));
4667 ui_out_emit_list list_emitter (current_uiout, "added");
4668 bool first = true;
4669 for (so_list *iter : current_program_space->added_solibs)
4670 {
4671 if (!first)
4672 current_uiout->text (" ");
4673 first = false;
4674 current_uiout->field_string ("library", iter->so_name);
4675 current_uiout->text ("\n");
4676 }
4677 }
4678 }
4679
4680 /* Print a message indicating what happened. This is called from
4681 normal_stop(). The input to this routine is the head of the bpstat
4682 list - a list of the eventpoints that caused this stop. KIND is
4683 the target_waitkind for the stopping event. This
4684 routine calls the generic print routine for printing a message
4685 about reasons for stopping. This will print (for example) the
4686 "Breakpoint n," part of the output. The return value of this
4687 routine is one of:
4688
4689 PRINT_UNKNOWN: Means we printed nothing.
4690 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4691 code to print the location. An example is
4692 "Breakpoint 1, " which should be followed by
4693 the location.
4694 PRINT_SRC_ONLY: Means we printed something, but there is no need
4695 to also print the location part of the message.
4696 An example is the catch/throw messages, which
4697 don't require a location appended to the end.
4698 PRINT_NOTHING: We have done some printing and we don't need any
4699 further info to be printed. */
4700
4701 enum print_stop_action
4702 bpstat_print (bpstat *bs, int kind)
4703 {
4704 enum print_stop_action val;
4705
4706 /* Maybe another breakpoint in the chain caused us to stop.
4707 (Currently all watchpoints go on the bpstat whether hit or not.
4708 That probably could (should) be changed, provided care is taken
4709 with respect to bpstat_explains_signal). */
4710 for (; bs; bs = bs->next)
4711 {
4712 val = print_bp_stop_message (bs);
4713 if (val == PRINT_SRC_ONLY
4714 || val == PRINT_SRC_AND_LOC
4715 || val == PRINT_NOTHING)
4716 return val;
4717 }
4718
4719 /* If we had hit a shared library event breakpoint,
4720 print_bp_stop_message would print out this message. If we hit an
4721 OS-level shared library event, do the same thing. */
4722 if (kind == TARGET_WAITKIND_LOADED)
4723 {
4724 print_solib_event (0);
4725 return PRINT_NOTHING;
4726 }
4727
4728 /* We reached the end of the chain, or we got a null BS to start
4729 with and nothing was printed. */
4730 return PRINT_UNKNOWN;
4731 }
4732
4733 /* Evaluate the boolean expression EXP and return the result. */
4734
4735 static bool
4736 breakpoint_cond_eval (expression *exp)
4737 {
4738 struct value *mark = value_mark ();
4739 bool res = value_true (evaluate_expression (exp));
4740
4741 value_free_to_mark (mark);
4742 return res;
4743 }
4744
4745 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4746
4747 bpstat::bpstat (struct bp_location *bl, bpstat ***bs_link_pointer)
4748 : next (NULL),
4749 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
4750 breakpoint_at (bl->owner),
4751 commands (NULL),
4752 print (0),
4753 stop (0),
4754 print_it (print_it_normal)
4755 {
4756 **bs_link_pointer = this;
4757 *bs_link_pointer = &next;
4758 }
4759
4760 bpstat::bpstat ()
4761 : next (NULL),
4762 breakpoint_at (NULL),
4763 commands (NULL),
4764 print (0),
4765 stop (0),
4766 print_it (print_it_normal)
4767 {
4768 }
4769 \f
4770 /* The target has stopped with waitstatus WS. Check if any hardware
4771 watchpoints have triggered, according to the target. */
4772
4773 int
4774 watchpoints_triggered (const target_waitstatus &ws)
4775 {
4776 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
4777 CORE_ADDR addr;
4778
4779 if (!stopped_by_watchpoint)
4780 {
4781 /* We were not stopped by a watchpoint. Mark all watchpoints
4782 as not triggered. */
4783 for (breakpoint *b : all_breakpoints ())
4784 if (is_hardware_watchpoint (b))
4785 {
4786 struct watchpoint *w = (struct watchpoint *) b;
4787
4788 w->watchpoint_triggered = watch_triggered_no;
4789 }
4790
4791 return 0;
4792 }
4793
4794 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
4795 {
4796 /* We were stopped by a watchpoint, but we don't know where.
4797 Mark all watchpoints as unknown. */
4798 for (breakpoint *b : all_breakpoints ())
4799 if (is_hardware_watchpoint (b))
4800 {
4801 struct watchpoint *w = (struct watchpoint *) b;
4802
4803 w->watchpoint_triggered = watch_triggered_unknown;
4804 }
4805
4806 return 1;
4807 }
4808
4809 /* The target could report the data address. Mark watchpoints
4810 affected by this data address as triggered, and all others as not
4811 triggered. */
4812
4813 for (breakpoint *b : all_breakpoints ())
4814 if (is_hardware_watchpoint (b))
4815 {
4816 struct watchpoint *w = (struct watchpoint *) b;
4817
4818 w->watchpoint_triggered = watch_triggered_no;
4819 for (bp_location *loc : b->locations ())
4820 {
4821 if (is_masked_watchpoint (b))
4822 {
4823 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4824 CORE_ADDR start = loc->address & w->hw_wp_mask;
4825
4826 if (newaddr == start)
4827 {
4828 w->watchpoint_triggered = watch_triggered_yes;
4829 break;
4830 }
4831 }
4832 /* Exact match not required. Within range is sufficient. */
4833 else if (target_watchpoint_addr_within_range
4834 (current_inferior ()->top_target (), addr, loc->address,
4835 loc->length))
4836 {
4837 w->watchpoint_triggered = watch_triggered_yes;
4838 break;
4839 }
4840 }
4841 }
4842
4843 return 1;
4844 }
4845
4846 /* Possible return values for watchpoint_check. */
4847 enum wp_check_result
4848 {
4849 /* The watchpoint has been deleted. */
4850 WP_DELETED = 1,
4851
4852 /* The value has changed. */
4853 WP_VALUE_CHANGED = 2,
4854
4855 /* The value has not changed. */
4856 WP_VALUE_NOT_CHANGED = 3,
4857
4858 /* Ignore this watchpoint, no matter if the value changed or not. */
4859 WP_IGNORE = 4,
4860 };
4861
4862 #define BP_TEMPFLAG 1
4863 #define BP_HARDWAREFLAG 2
4864
4865 /* Evaluate watchpoint condition expression and check if its value
4866 changed. */
4867
4868 static wp_check_result
4869 watchpoint_check (bpstat *bs)
4870 {
4871 struct watchpoint *b;
4872 struct frame_info *fr;
4873 int within_current_scope;
4874
4875 /* BS is built from an existing struct breakpoint. */
4876 gdb_assert (bs->breakpoint_at != NULL);
4877 b = (struct watchpoint *) bs->breakpoint_at;
4878
4879 /* If this is a local watchpoint, we only want to check if the
4880 watchpoint frame is in scope if the current thread is the thread
4881 that was used to create the watchpoint. */
4882 if (!watchpoint_in_thread_scope (b))
4883 return WP_IGNORE;
4884
4885 if (b->exp_valid_block == NULL)
4886 within_current_scope = 1;
4887 else
4888 {
4889 struct frame_info *frame = get_current_frame ();
4890 struct gdbarch *frame_arch = get_frame_arch (frame);
4891 CORE_ADDR frame_pc = get_frame_pc (frame);
4892
4893 /* stack_frame_destroyed_p() returns a non-zero value if we're
4894 still in the function but the stack frame has already been
4895 invalidated. Since we can't rely on the values of local
4896 variables after the stack has been destroyed, we are treating
4897 the watchpoint in that state as `not changed' without further
4898 checking. Don't mark watchpoints as changed if the current
4899 frame is in an epilogue - even if they are in some other
4900 frame, our view of the stack is likely to be wrong and
4901 frame_find_by_id could error out. */
4902 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
4903 return WP_IGNORE;
4904
4905 fr = frame_find_by_id (b->watchpoint_frame);
4906 within_current_scope = (fr != NULL);
4907
4908 /* If we've gotten confused in the unwinder, we might have
4909 returned a frame that can't describe this variable. */
4910 if (within_current_scope)
4911 {
4912 struct symbol *function;
4913
4914 function = get_frame_function (fr);
4915 if (function == NULL
4916 || !contained_in (b->exp_valid_block,
4917 SYMBOL_BLOCK_VALUE (function)))
4918 within_current_scope = 0;
4919 }
4920
4921 if (within_current_scope)
4922 /* If we end up stopping, the current frame will get selected
4923 in normal_stop. So this call to select_frame won't affect
4924 the user. */
4925 select_frame (fr);
4926 }
4927
4928 if (within_current_scope)
4929 {
4930 /* We use value_{,free_to_}mark because it could be a *long*
4931 time before we return to the command level and call
4932 free_all_values. We can't call free_all_values because we
4933 might be in the middle of evaluating a function call. */
4934
4935 struct value *mark;
4936 struct value *new_val;
4937
4938 if (is_masked_watchpoint (b))
4939 /* Since we don't know the exact trigger address (from
4940 stopped_data_address), just tell the user we've triggered
4941 a mask watchpoint. */
4942 return WP_VALUE_CHANGED;
4943
4944 mark = value_mark ();
4945 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
4946 NULL, NULL, false);
4947
4948 if (b->val_bitsize != 0)
4949 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4950
4951 /* We use value_equal_contents instead of value_equal because
4952 the latter coerces an array to a pointer, thus comparing just
4953 the address of the array instead of its contents. This is
4954 not what we want. */
4955 if ((b->val != NULL) != (new_val != NULL)
4956 || (b->val != NULL && !value_equal_contents (b->val.get (),
4957 new_val)))
4958 {
4959 bs->old_val = b->val;
4960 b->val = release_value (new_val);
4961 b->val_valid = true;
4962 if (new_val != NULL)
4963 value_free_to_mark (mark);
4964 return WP_VALUE_CHANGED;
4965 }
4966 else
4967 {
4968 /* Nothing changed. */
4969 value_free_to_mark (mark);
4970 return WP_VALUE_NOT_CHANGED;
4971 }
4972 }
4973 else
4974 {
4975 /* This seems like the only logical thing to do because
4976 if we temporarily ignored the watchpoint, then when
4977 we reenter the block in which it is valid it contains
4978 garbage (in the case of a function, it may have two
4979 garbage values, one before and one after the prologue).
4980 So we can't even detect the first assignment to it and
4981 watch after that (since the garbage may or may not equal
4982 the first value assigned). */
4983 /* We print all the stop information in
4984 breakpoint_ops->print_it, but in this case, by the time we
4985 call breakpoint_ops->print_it this bp will be deleted
4986 already. So we have no choice but print the information
4987 here. */
4988
4989 SWITCH_THRU_ALL_UIS ()
4990 {
4991 struct ui_out *uiout = current_uiout;
4992
4993 if (uiout->is_mi_like_p ())
4994 uiout->field_string
4995 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
4996 uiout->message ("\nWatchpoint %pF deleted because the program has "
4997 "left the block in\n"
4998 "which its expression is valid.\n",
4999 signed_field ("wpnum", b->number));
5000 }
5001
5002 /* Make sure the watchpoint's commands aren't executed. */
5003 b->commands = NULL;
5004 watchpoint_del_at_next_stop (b);
5005
5006 return WP_DELETED;
5007 }
5008 }
5009
5010 /* Return true if it looks like target has stopped due to hitting
5011 breakpoint location BL. This function does not check if we should
5012 stop, only if BL explains the stop. */
5013
5014 static int
5015 bpstat_check_location (const struct bp_location *bl,
5016 const address_space *aspace, CORE_ADDR bp_addr,
5017 const target_waitstatus &ws)
5018 {
5019 struct breakpoint *b = bl->owner;
5020
5021 /* BL is from an existing breakpoint. */
5022 gdb_assert (b != NULL);
5023
5024 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5025 }
5026
5027 /* Determine if the watched values have actually changed, and we
5028 should stop. If not, set BS->stop to 0. */
5029
5030 static void
5031 bpstat_check_watchpoint (bpstat *bs)
5032 {
5033 const struct bp_location *bl;
5034 struct watchpoint *b;
5035
5036 /* BS is built for existing struct breakpoint. */
5037 bl = bs->bp_location_at.get ();
5038 gdb_assert (bl != NULL);
5039 b = (struct watchpoint *) bs->breakpoint_at;
5040 gdb_assert (b != NULL);
5041
5042 {
5043 int must_check_value = 0;
5044
5045 if (b->type == bp_watchpoint)
5046 /* For a software watchpoint, we must always check the
5047 watched value. */
5048 must_check_value = 1;
5049 else if (b->watchpoint_triggered == watch_triggered_yes)
5050 /* We have a hardware watchpoint (read, write, or access)
5051 and the target earlier reported an address watched by
5052 this watchpoint. */
5053 must_check_value = 1;
5054 else if (b->watchpoint_triggered == watch_triggered_unknown
5055 && b->type == bp_hardware_watchpoint)
5056 /* We were stopped by a hardware watchpoint, but the target could
5057 not report the data address. We must check the watchpoint's
5058 value. Access and read watchpoints are out of luck; without
5059 a data address, we can't figure it out. */
5060 must_check_value = 1;
5061
5062 if (must_check_value)
5063 {
5064 wp_check_result e;
5065
5066 try
5067 {
5068 e = watchpoint_check (bs);
5069 }
5070 catch (const gdb_exception &ex)
5071 {
5072 exception_fprintf (gdb_stderr, ex,
5073 "Error evaluating expression "
5074 "for watchpoint %d\n",
5075 b->number);
5076
5077 SWITCH_THRU_ALL_UIS ()
5078 {
5079 printf_filtered (_("Watchpoint %d deleted.\n"),
5080 b->number);
5081 }
5082 watchpoint_del_at_next_stop (b);
5083 e = WP_DELETED;
5084 }
5085
5086 switch (e)
5087 {
5088 case WP_DELETED:
5089 /* We've already printed what needs to be printed. */
5090 bs->print_it = print_it_done;
5091 /* Stop. */
5092 break;
5093 case WP_IGNORE:
5094 bs->print_it = print_it_noop;
5095 bs->stop = 0;
5096 break;
5097 case WP_VALUE_CHANGED:
5098 if (b->type == bp_read_watchpoint)
5099 {
5100 /* There are two cases to consider here:
5101
5102 1. We're watching the triggered memory for reads.
5103 In that case, trust the target, and always report
5104 the watchpoint hit to the user. Even though
5105 reads don't cause value changes, the value may
5106 have changed since the last time it was read, and
5107 since we're not trapping writes, we will not see
5108 those, and as such we should ignore our notion of
5109 old value.
5110
5111 2. We're watching the triggered memory for both
5112 reads and writes. There are two ways this may
5113 happen:
5114
5115 2.1. This is a target that can't break on data
5116 reads only, but can break on accesses (reads or
5117 writes), such as e.g., x86. We detect this case
5118 at the time we try to insert read watchpoints.
5119
5120 2.2. Otherwise, the target supports read
5121 watchpoints, but, the user set an access or write
5122 watchpoint watching the same memory as this read
5123 watchpoint.
5124
5125 If we're watching memory writes as well as reads,
5126 ignore watchpoint hits when we find that the
5127 value hasn't changed, as reads don't cause
5128 changes. This still gives false positives when
5129 the program writes the same value to memory as
5130 what there was already in memory (we will confuse
5131 it for a read), but it's much better than
5132 nothing. */
5133
5134 int other_write_watchpoint = 0;
5135
5136 if (bl->watchpoint_type == hw_read)
5137 {
5138 for (breakpoint *other_b : all_breakpoints ())
5139 if (other_b->type == bp_hardware_watchpoint
5140 || other_b->type == bp_access_watchpoint)
5141 {
5142 struct watchpoint *other_w =
5143 (struct watchpoint *) other_b;
5144
5145 if (other_w->watchpoint_triggered
5146 == watch_triggered_yes)
5147 {
5148 other_write_watchpoint = 1;
5149 break;
5150 }
5151 }
5152 }
5153
5154 if (other_write_watchpoint
5155 || bl->watchpoint_type == hw_access)
5156 {
5157 /* We're watching the same memory for writes,
5158 and the value changed since the last time we
5159 updated it, so this trap must be for a write.
5160 Ignore it. */
5161 bs->print_it = print_it_noop;
5162 bs->stop = 0;
5163 }
5164 }
5165 break;
5166 case WP_VALUE_NOT_CHANGED:
5167 if (b->type == bp_hardware_watchpoint
5168 || b->type == bp_watchpoint)
5169 {
5170 /* Don't stop: write watchpoints shouldn't fire if
5171 the value hasn't changed. */
5172 bs->print_it = print_it_noop;
5173 bs->stop = 0;
5174 }
5175 /* Stop. */
5176 break;
5177 default:
5178 /* Can't happen. */
5179 break;
5180 }
5181 }
5182 else /* must_check_value == 0 */
5183 {
5184 /* This is a case where some watchpoint(s) triggered, but
5185 not at the address of this watchpoint, or else no
5186 watchpoint triggered after all. So don't print
5187 anything for this watchpoint. */
5188 bs->print_it = print_it_noop;
5189 bs->stop = 0;
5190 }
5191 }
5192 }
5193
5194 /* For breakpoints that are currently marked as telling gdb to stop,
5195 check conditions (condition proper, frame, thread and ignore count)
5196 of breakpoint referred to by BS. If we should not stop for this
5197 breakpoint, set BS->stop to 0. */
5198
5199 static void
5200 bpstat_check_breakpoint_conditions (bpstat *bs, thread_info *thread)
5201 {
5202 const struct bp_location *bl;
5203 struct breakpoint *b;
5204 /* Assume stop. */
5205 bool condition_result = true;
5206 struct expression *cond;
5207
5208 gdb_assert (bs->stop);
5209
5210 /* BS is built for existing struct breakpoint. */
5211 bl = bs->bp_location_at.get ();
5212 gdb_assert (bl != NULL);
5213 b = bs->breakpoint_at;
5214 gdb_assert (b != NULL);
5215
5216 /* Even if the target evaluated the condition on its end and notified GDB, we
5217 need to do so again since GDB does not know if we stopped due to a
5218 breakpoint or a single step breakpoint. */
5219
5220 if (frame_id_p (b->frame_id)
5221 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5222 {
5223 bs->stop = 0;
5224 return;
5225 }
5226
5227 /* If this is a thread/task-specific breakpoint, don't waste cpu
5228 evaluating the condition if this isn't the specified
5229 thread/task. */
5230 if ((b->thread != -1 && b->thread != thread->global_num)
5231 || (b->task != 0 && b->task != ada_get_task_number (thread)))
5232 {
5233 bs->stop = 0;
5234 return;
5235 }
5236
5237 /* Evaluate extension language breakpoints that have a "stop" method
5238 implemented. */
5239 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5240
5241 if (is_watchpoint (b))
5242 {
5243 struct watchpoint *w = (struct watchpoint *) b;
5244
5245 cond = w->cond_exp.get ();
5246 }
5247 else
5248 cond = bl->cond.get ();
5249
5250 if (cond && b->disposition != disp_del_at_next_stop)
5251 {
5252 int within_current_scope = 1;
5253 struct watchpoint * w;
5254
5255 /* We use value_mark and value_free_to_mark because it could
5256 be a long time before we return to the command level and
5257 call free_all_values. We can't call free_all_values
5258 because we might be in the middle of evaluating a
5259 function call. */
5260 struct value *mark = value_mark ();
5261
5262 if (is_watchpoint (b))
5263 w = (struct watchpoint *) b;
5264 else
5265 w = NULL;
5266
5267 /* Need to select the frame, with all that implies so that
5268 the conditions will have the right context. Because we
5269 use the frame, we will not see an inlined function's
5270 variables when we arrive at a breakpoint at the start
5271 of the inlined function; the current frame will be the
5272 call site. */
5273 if (w == NULL || w->cond_exp_valid_block == NULL)
5274 select_frame (get_current_frame ());
5275 else
5276 {
5277 struct frame_info *frame;
5278
5279 /* For local watchpoint expressions, which particular
5280 instance of a local is being watched matters, so we
5281 keep track of the frame to evaluate the expression
5282 in. To evaluate the condition however, it doesn't
5283 really matter which instantiation of the function
5284 where the condition makes sense triggers the
5285 watchpoint. This allows an expression like "watch
5286 global if q > 10" set in `func', catch writes to
5287 global on all threads that call `func', or catch
5288 writes on all recursive calls of `func' by a single
5289 thread. We simply always evaluate the condition in
5290 the innermost frame that's executing where it makes
5291 sense to evaluate the condition. It seems
5292 intuitive. */
5293 frame = block_innermost_frame (w->cond_exp_valid_block);
5294 if (frame != NULL)
5295 select_frame (frame);
5296 else
5297 within_current_scope = 0;
5298 }
5299 if (within_current_scope)
5300 {
5301 try
5302 {
5303 condition_result = breakpoint_cond_eval (cond);
5304 }
5305 catch (const gdb_exception &ex)
5306 {
5307 exception_fprintf (gdb_stderr, ex,
5308 "Error in testing breakpoint condition:\n");
5309 }
5310 }
5311 else
5312 {
5313 warning (_("Watchpoint condition cannot be tested "
5314 "in the current scope"));
5315 /* If we failed to set the right context for this
5316 watchpoint, unconditionally report it. */
5317 }
5318 /* FIXME-someday, should give breakpoint #. */
5319 value_free_to_mark (mark);
5320 }
5321
5322 if (cond && !condition_result)
5323 {
5324 bs->stop = 0;
5325 }
5326 else if (b->ignore_count > 0)
5327 {
5328 b->ignore_count--;
5329 bs->stop = 0;
5330 /* Increase the hit count even though we don't stop. */
5331 ++(b->hit_count);
5332 gdb::observers::breakpoint_modified.notify (b);
5333 }
5334 }
5335
5336 /* Returns true if we need to track moribund locations of LOC's type
5337 on the current target. */
5338
5339 static int
5340 need_moribund_for_location_type (struct bp_location *loc)
5341 {
5342 return ((loc->loc_type == bp_loc_software_breakpoint
5343 && !target_supports_stopped_by_sw_breakpoint ())
5344 || (loc->loc_type == bp_loc_hardware_breakpoint
5345 && !target_supports_stopped_by_hw_breakpoint ()));
5346 }
5347
5348 /* See breakpoint.h. */
5349
5350 bpstat *
5351 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5352 const target_waitstatus &ws)
5353 {
5354 bpstat *bs_head = nullptr, **bs_link = &bs_head;
5355
5356 for (breakpoint *b : all_breakpoints ())
5357 {
5358 if (!breakpoint_enabled (b))
5359 continue;
5360
5361 for (bp_location *bl : b->locations ())
5362 {
5363 /* For hardware watchpoints, we look only at the first
5364 location. The watchpoint_check function will work on the
5365 entire expression, not the individual locations. For
5366 read watchpoints, the watchpoints_triggered function has
5367 checked all locations already. */
5368 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5369 break;
5370
5371 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
5372 continue;
5373
5374 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5375 continue;
5376
5377 /* Come here if it's a watchpoint, or if the break address
5378 matches. */
5379
5380 bpstat *bs = new bpstat (bl, &bs_link); /* Alloc a bpstat to
5381 explain stop. */
5382
5383 /* Assume we stop. Should we find a watchpoint that is not
5384 actually triggered, or if the condition of the breakpoint
5385 evaluates as false, we'll reset 'stop' to 0. */
5386 bs->stop = 1;
5387 bs->print = 1;
5388
5389 /* If this is a scope breakpoint, mark the associated
5390 watchpoint as triggered so that we will handle the
5391 out-of-scope event. We'll get to the watchpoint next
5392 iteration. */
5393 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5394 {
5395 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5396
5397 w->watchpoint_triggered = watch_triggered_yes;
5398 }
5399 }
5400 }
5401
5402 /* Check if a moribund breakpoint explains the stop. */
5403 if (!target_supports_stopped_by_sw_breakpoint ()
5404 || !target_supports_stopped_by_hw_breakpoint ())
5405 {
5406 for (bp_location *loc : moribund_locations)
5407 {
5408 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5409 && need_moribund_for_location_type (loc))
5410 {
5411 bpstat *bs = new bpstat (loc, &bs_link);
5412 /* For hits of moribund locations, we should just proceed. */
5413 bs->stop = 0;
5414 bs->print = 0;
5415 bs->print_it = print_it_noop;
5416 }
5417 }
5418 }
5419
5420 return bs_head;
5421 }
5422
5423 /* See breakpoint.h. */
5424
5425 bpstat *
5426 bpstat_stop_status (const address_space *aspace,
5427 CORE_ADDR bp_addr, thread_info *thread,
5428 const target_waitstatus &ws,
5429 bpstat *stop_chain)
5430 {
5431 struct breakpoint *b = NULL;
5432 /* First item of allocated bpstat's. */
5433 bpstat *bs_head = stop_chain;
5434 bpstat *bs;
5435 int need_remove_insert;
5436 int removed_any;
5437
5438 /* First, build the bpstat chain with locations that explain a
5439 target stop, while being careful to not set the target running,
5440 as that may invalidate locations (in particular watchpoint
5441 locations are recreated). Resuming will happen here with
5442 breakpoint conditions or watchpoint expressions that include
5443 inferior function calls. */
5444 if (bs_head == NULL)
5445 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5446
5447 /* A bit of special processing for shlib breakpoints. We need to
5448 process solib loading here, so that the lists of loaded and
5449 unloaded libraries are correct before we handle "catch load" and
5450 "catch unload". */
5451 for (bs = bs_head; bs != NULL; bs = bs->next)
5452 {
5453 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5454 {
5455 handle_solib_event ();
5456 break;
5457 }
5458 }
5459
5460 /* Now go through the locations that caused the target to stop, and
5461 check whether we're interested in reporting this stop to higher
5462 layers, or whether we should resume the target transparently. */
5463
5464 removed_any = 0;
5465
5466 for (bs = bs_head; bs != NULL; bs = bs->next)
5467 {
5468 if (!bs->stop)
5469 continue;
5470
5471 b = bs->breakpoint_at;
5472 b->ops->check_status (bs);
5473 if (bs->stop)
5474 {
5475 bpstat_check_breakpoint_conditions (bs, thread);
5476
5477 if (bs->stop)
5478 {
5479 ++(b->hit_count);
5480
5481 /* We will stop here. */
5482 if (b->disposition == disp_disable)
5483 {
5484 --(b->enable_count);
5485 if (b->enable_count <= 0)
5486 b->enable_state = bp_disabled;
5487 removed_any = 1;
5488 }
5489 gdb::observers::breakpoint_modified.notify (b);
5490 if (b->silent)
5491 bs->print = 0;
5492 bs->commands = b->commands;
5493 if (command_line_is_silent (bs->commands
5494 ? bs->commands.get () : NULL))
5495 bs->print = 0;
5496
5497 b->ops->after_condition_true (bs);
5498 }
5499
5500 }
5501
5502 /* Print nothing for this entry if we don't stop or don't
5503 print. */
5504 if (!bs->stop || !bs->print)
5505 bs->print_it = print_it_noop;
5506 }
5507
5508 /* If we aren't stopping, the value of some hardware watchpoint may
5509 not have changed, but the intermediate memory locations we are
5510 watching may have. Don't bother if we're stopping; this will get
5511 done later. */
5512 need_remove_insert = 0;
5513 if (! bpstat_causes_stop (bs_head))
5514 for (bs = bs_head; bs != NULL; bs = bs->next)
5515 if (!bs->stop
5516 && bs->breakpoint_at
5517 && is_hardware_watchpoint (bs->breakpoint_at))
5518 {
5519 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5520
5521 update_watchpoint (w, 0 /* don't reparse. */);
5522 need_remove_insert = 1;
5523 }
5524
5525 if (need_remove_insert)
5526 update_global_location_list (UGLL_MAY_INSERT);
5527 else if (removed_any)
5528 update_global_location_list (UGLL_DONT_INSERT);
5529
5530 return bs_head;
5531 }
5532
5533 static void
5534 handle_jit_event (CORE_ADDR address)
5535 {
5536 struct gdbarch *gdbarch;
5537
5538 infrun_debug_printf ("handling bp_jit_event");
5539
5540 /* Switch terminal for any messages produced by
5541 breakpoint_re_set. */
5542 target_terminal::ours_for_output ();
5543
5544 gdbarch = get_frame_arch (get_current_frame ());
5545 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5546 thus it is expected that its objectfile can be found through
5547 minimal symbol lookup. If it doesn't work (and assert fails), it
5548 most likely means that `jit_breakpoint_re_set` was changes and this
5549 function needs to be updated too. */
5550 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5551 gdb_assert (jit_bp_sym.objfile != nullptr);
5552 jit_event_handler (gdbarch, jit_bp_sym.objfile);
5553
5554 target_terminal::inferior ();
5555 }
5556
5557 /* Prepare WHAT final decision for infrun. */
5558
5559 /* Decide what infrun needs to do with this bpstat. */
5560
5561 struct bpstat_what
5562 bpstat_what (bpstat *bs_head)
5563 {
5564 struct bpstat_what retval;
5565 bpstat *bs;
5566
5567 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5568 retval.call_dummy = STOP_NONE;
5569 retval.is_longjmp = false;
5570
5571 for (bs = bs_head; bs != NULL; bs = bs->next)
5572 {
5573 /* Extract this BS's action. After processing each BS, we check
5574 if its action overrides all we've seem so far. */
5575 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5576 enum bptype bptype;
5577
5578 if (bs->breakpoint_at == NULL)
5579 {
5580 /* I suspect this can happen if it was a momentary
5581 breakpoint which has since been deleted. */
5582 bptype = bp_none;
5583 }
5584 else
5585 bptype = bs->breakpoint_at->type;
5586
5587 switch (bptype)
5588 {
5589 case bp_none:
5590 break;
5591 case bp_breakpoint:
5592 case bp_hardware_breakpoint:
5593 case bp_single_step:
5594 case bp_until:
5595 case bp_finish:
5596 case bp_shlib_event:
5597 if (bs->stop)
5598 {
5599 if (bs->print)
5600 this_action = BPSTAT_WHAT_STOP_NOISY;
5601 else
5602 this_action = BPSTAT_WHAT_STOP_SILENT;
5603 }
5604 else
5605 this_action = BPSTAT_WHAT_SINGLE;
5606 break;
5607 case bp_watchpoint:
5608 case bp_hardware_watchpoint:
5609 case bp_read_watchpoint:
5610 case bp_access_watchpoint:
5611 if (bs->stop)
5612 {
5613 if (bs->print)
5614 this_action = BPSTAT_WHAT_STOP_NOISY;
5615 else
5616 this_action = BPSTAT_WHAT_STOP_SILENT;
5617 }
5618 else
5619 {
5620 /* There was a watchpoint, but we're not stopping.
5621 This requires no further action. */
5622 }
5623 break;
5624 case bp_longjmp:
5625 case bp_longjmp_call_dummy:
5626 case bp_exception:
5627 if (bs->stop)
5628 {
5629 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5630 retval.is_longjmp = bptype != bp_exception;
5631 }
5632 else
5633 this_action = BPSTAT_WHAT_SINGLE;
5634 break;
5635 case bp_longjmp_resume:
5636 case bp_exception_resume:
5637 if (bs->stop)
5638 {
5639 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5640 retval.is_longjmp = bptype == bp_longjmp_resume;
5641 }
5642 else
5643 this_action = BPSTAT_WHAT_SINGLE;
5644 break;
5645 case bp_step_resume:
5646 if (bs->stop)
5647 this_action = BPSTAT_WHAT_STEP_RESUME;
5648 else
5649 {
5650 /* It is for the wrong frame. */
5651 this_action = BPSTAT_WHAT_SINGLE;
5652 }
5653 break;
5654 case bp_hp_step_resume:
5655 if (bs->stop)
5656 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5657 else
5658 {
5659 /* It is for the wrong frame. */
5660 this_action = BPSTAT_WHAT_SINGLE;
5661 }
5662 break;
5663 case bp_watchpoint_scope:
5664 case bp_thread_event:
5665 case bp_overlay_event:
5666 case bp_longjmp_master:
5667 case bp_std_terminate_master:
5668 case bp_exception_master:
5669 this_action = BPSTAT_WHAT_SINGLE;
5670 break;
5671 case bp_catchpoint:
5672 if (bs->stop)
5673 {
5674 if (bs->print)
5675 this_action = BPSTAT_WHAT_STOP_NOISY;
5676 else
5677 this_action = BPSTAT_WHAT_STOP_SILENT;
5678 }
5679 else
5680 {
5681 /* Some catchpoints are implemented with breakpoints.
5682 For those, we need to step over the breakpoint. */
5683 if (bs->bp_location_at->loc_type != bp_loc_other)
5684 this_action = BPSTAT_WHAT_SINGLE;
5685 }
5686 break;
5687 case bp_jit_event:
5688 this_action = BPSTAT_WHAT_SINGLE;
5689 break;
5690 case bp_call_dummy:
5691 /* Make sure the action is stop (silent or noisy),
5692 so infrun.c pops the dummy frame. */
5693 retval.call_dummy = STOP_STACK_DUMMY;
5694 this_action = BPSTAT_WHAT_STOP_SILENT;
5695 break;
5696 case bp_std_terminate:
5697 /* Make sure the action is stop (silent or noisy),
5698 so infrun.c pops the dummy frame. */
5699 retval.call_dummy = STOP_STD_TERMINATE;
5700 this_action = BPSTAT_WHAT_STOP_SILENT;
5701 break;
5702 case bp_tracepoint:
5703 case bp_fast_tracepoint:
5704 case bp_static_tracepoint:
5705 /* Tracepoint hits should not be reported back to GDB, and
5706 if one got through somehow, it should have been filtered
5707 out already. */
5708 internal_error (__FILE__, __LINE__,
5709 _("bpstat_what: tracepoint encountered"));
5710 break;
5711 case bp_gnu_ifunc_resolver:
5712 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5713 this_action = BPSTAT_WHAT_SINGLE;
5714 break;
5715 case bp_gnu_ifunc_resolver_return:
5716 /* The breakpoint will be removed, execution will restart from the
5717 PC of the former breakpoint. */
5718 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5719 break;
5720
5721 case bp_dprintf:
5722 if (bs->stop)
5723 this_action = BPSTAT_WHAT_STOP_SILENT;
5724 else
5725 this_action = BPSTAT_WHAT_SINGLE;
5726 break;
5727
5728 default:
5729 internal_error (__FILE__, __LINE__,
5730 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5731 }
5732
5733 retval.main_action = std::max (retval.main_action, this_action);
5734 }
5735
5736 return retval;
5737 }
5738
5739 void
5740 bpstat_run_callbacks (bpstat *bs_head)
5741 {
5742 bpstat *bs;
5743
5744 for (bs = bs_head; bs != NULL; bs = bs->next)
5745 {
5746 struct breakpoint *b = bs->breakpoint_at;
5747
5748 if (b == NULL)
5749 continue;
5750 switch (b->type)
5751 {
5752 case bp_jit_event:
5753 handle_jit_event (bs->bp_location_at->address);
5754 break;
5755 case bp_gnu_ifunc_resolver:
5756 gnu_ifunc_resolver_stop (b);
5757 break;
5758 case bp_gnu_ifunc_resolver_return:
5759 gnu_ifunc_resolver_return_stop (b);
5760 break;
5761 }
5762 }
5763 }
5764
5765 /* See breakpoint.h. */
5766
5767 bool
5768 bpstat_should_step ()
5769 {
5770 for (breakpoint *b : all_breakpoints ())
5771 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5772 return true;
5773
5774 return false;
5775 }
5776
5777 /* See breakpoint.h. */
5778
5779 bool
5780 bpstat_causes_stop (bpstat *bs)
5781 {
5782 for (; bs != NULL; bs = bs->next)
5783 if (bs->stop)
5784 return true;
5785
5786 return false;
5787 }
5788
5789 \f
5790
5791 /* Compute a string of spaces suitable to indent the next line
5792 so it starts at the position corresponding to the table column
5793 named COL_NAME in the currently active table of UIOUT. */
5794
5795 static char *
5796 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5797 {
5798 static char wrap_indent[80];
5799 int i, total_width, width, align;
5800 const char *text;
5801
5802 total_width = 0;
5803 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5804 {
5805 if (strcmp (text, col_name) == 0)
5806 {
5807 gdb_assert (total_width < sizeof wrap_indent);
5808 memset (wrap_indent, ' ', total_width);
5809 wrap_indent[total_width] = 0;
5810
5811 return wrap_indent;
5812 }
5813
5814 total_width += width + 1;
5815 }
5816
5817 return NULL;
5818 }
5819
5820 /* Determine if the locations of this breakpoint will have their conditions
5821 evaluated by the target, host or a mix of both. Returns the following:
5822
5823 "host": Host evals condition.
5824 "host or target": Host or Target evals condition.
5825 "target": Target evals condition.
5826 */
5827
5828 static const char *
5829 bp_condition_evaluator (struct breakpoint *b)
5830 {
5831 char host_evals = 0;
5832 char target_evals = 0;
5833
5834 if (!b)
5835 return NULL;
5836
5837 if (!is_breakpoint (b))
5838 return NULL;
5839
5840 if (gdb_evaluates_breakpoint_condition_p ()
5841 || !target_supports_evaluation_of_breakpoint_conditions ())
5842 return condition_evaluation_host;
5843
5844 for (bp_location *bl : b->locations ())
5845 {
5846 if (bl->cond_bytecode)
5847 target_evals++;
5848 else
5849 host_evals++;
5850 }
5851
5852 if (host_evals && target_evals)
5853 return condition_evaluation_both;
5854 else if (target_evals)
5855 return condition_evaluation_target;
5856 else
5857 return condition_evaluation_host;
5858 }
5859
5860 /* Determine the breakpoint location's condition evaluator. This is
5861 similar to bp_condition_evaluator, but for locations. */
5862
5863 static const char *
5864 bp_location_condition_evaluator (struct bp_location *bl)
5865 {
5866 if (bl && !is_breakpoint (bl->owner))
5867 return NULL;
5868
5869 if (gdb_evaluates_breakpoint_condition_p ()
5870 || !target_supports_evaluation_of_breakpoint_conditions ())
5871 return condition_evaluation_host;
5872
5873 if (bl && bl->cond_bytecode)
5874 return condition_evaluation_target;
5875 else
5876 return condition_evaluation_host;
5877 }
5878
5879 /* Print the LOC location out of the list of B->LOC locations. */
5880
5881 static void
5882 print_breakpoint_location (struct breakpoint *b,
5883 struct bp_location *loc)
5884 {
5885 struct ui_out *uiout = current_uiout;
5886
5887 scoped_restore_current_program_space restore_pspace;
5888
5889 if (loc != NULL && loc->shlib_disabled)
5890 loc = NULL;
5891
5892 if (loc != NULL)
5893 set_current_program_space (loc->pspace);
5894
5895 if (b->display_canonical)
5896 uiout->field_string ("what", event_location_to_string (b->location.get ()));
5897 else if (loc && loc->symtab)
5898 {
5899 const struct symbol *sym = loc->symbol;
5900
5901 if (sym)
5902 {
5903 uiout->text ("in ");
5904 uiout->field_string ("func", sym->print_name (),
5905 function_name_style.style ());
5906 uiout->text (" ");
5907 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5908 uiout->text ("at ");
5909 }
5910 uiout->field_string ("file",
5911 symtab_to_filename_for_display (loc->symtab),
5912 file_name_style.style ());
5913 uiout->text (":");
5914
5915 if (uiout->is_mi_like_p ())
5916 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
5917
5918 uiout->field_signed ("line", loc->line_number);
5919 }
5920 else if (loc)
5921 {
5922 string_file stb;
5923
5924 print_address_symbolic (loc->gdbarch, loc->address, &stb,
5925 demangle, "");
5926 uiout->field_stream ("at", stb);
5927 }
5928 else
5929 {
5930 uiout->field_string ("pending",
5931 event_location_to_string (b->location.get ()));
5932 /* If extra_string is available, it could be holding a condition
5933 or dprintf arguments. In either case, make sure it is printed,
5934 too, but only for non-MI streams. */
5935 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
5936 {
5937 if (b->type == bp_dprintf)
5938 uiout->text (",");
5939 else
5940 uiout->text (" ");
5941 uiout->text (b->extra_string.get ());
5942 }
5943 }
5944
5945 if (loc && is_breakpoint (b)
5946 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5947 && bp_condition_evaluator (b) == condition_evaluation_both)
5948 {
5949 uiout->text (" (");
5950 uiout->field_string ("evaluated-by",
5951 bp_location_condition_evaluator (loc));
5952 uiout->text (")");
5953 }
5954 }
5955
5956 static const char *
5957 bptype_string (enum bptype type)
5958 {
5959 struct ep_type_description
5960 {
5961 enum bptype type;
5962 const char *description;
5963 };
5964 static struct ep_type_description bptypes[] =
5965 {
5966 {bp_none, "?deleted?"},
5967 {bp_breakpoint, "breakpoint"},
5968 {bp_hardware_breakpoint, "hw breakpoint"},
5969 {bp_single_step, "sw single-step"},
5970 {bp_until, "until"},
5971 {bp_finish, "finish"},
5972 {bp_watchpoint, "watchpoint"},
5973 {bp_hardware_watchpoint, "hw watchpoint"},
5974 {bp_read_watchpoint, "read watchpoint"},
5975 {bp_access_watchpoint, "acc watchpoint"},
5976 {bp_longjmp, "longjmp"},
5977 {bp_longjmp_resume, "longjmp resume"},
5978 {bp_longjmp_call_dummy, "longjmp for call dummy"},
5979 {bp_exception, "exception"},
5980 {bp_exception_resume, "exception resume"},
5981 {bp_step_resume, "step resume"},
5982 {bp_hp_step_resume, "high-priority step resume"},
5983 {bp_watchpoint_scope, "watchpoint scope"},
5984 {bp_call_dummy, "call dummy"},
5985 {bp_std_terminate, "std::terminate"},
5986 {bp_shlib_event, "shlib events"},
5987 {bp_thread_event, "thread events"},
5988 {bp_overlay_event, "overlay events"},
5989 {bp_longjmp_master, "longjmp master"},
5990 {bp_std_terminate_master, "std::terminate master"},
5991 {bp_exception_master, "exception master"},
5992 {bp_catchpoint, "catchpoint"},
5993 {bp_tracepoint, "tracepoint"},
5994 {bp_fast_tracepoint, "fast tracepoint"},
5995 {bp_static_tracepoint, "static tracepoint"},
5996 {bp_dprintf, "dprintf"},
5997 {bp_jit_event, "jit events"},
5998 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5999 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6000 };
6001
6002 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6003 || ((int) type != bptypes[(int) type].type))
6004 internal_error (__FILE__, __LINE__,
6005 _("bptypes table does not describe type #%d."),
6006 (int) type);
6007
6008 return bptypes[(int) type].description;
6009 }
6010
6011 /* For MI, output a field named 'thread-groups' with a list as the value.
6012 For CLI, prefix the list with the string 'inf'. */
6013
6014 static void
6015 output_thread_groups (struct ui_out *uiout,
6016 const char *field_name,
6017 const std::vector<int> &inf_nums,
6018 int mi_only)
6019 {
6020 int is_mi = uiout->is_mi_like_p ();
6021
6022 /* For backward compatibility, don't display inferiors in CLI unless
6023 there are several. Always display them for MI. */
6024 if (!is_mi && mi_only)
6025 return;
6026
6027 ui_out_emit_list list_emitter (uiout, field_name);
6028
6029 for (size_t i = 0; i < inf_nums.size (); i++)
6030 {
6031 if (is_mi)
6032 {
6033 char mi_group[10];
6034
6035 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
6036 uiout->field_string (NULL, mi_group);
6037 }
6038 else
6039 {
6040 if (i == 0)
6041 uiout->text (" inf ");
6042 else
6043 uiout->text (", ");
6044
6045 uiout->text (plongest (inf_nums[i]));
6046 }
6047 }
6048 }
6049
6050 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6051 instead of going via breakpoint_ops::print_one. This makes "maint
6052 info breakpoints" show the software breakpoint locations of
6053 catchpoints, which are considered internal implementation
6054 detail. */
6055
6056 static void
6057 print_one_breakpoint_location (struct breakpoint *b,
6058 struct bp_location *loc,
6059 int loc_number,
6060 struct bp_location **last_loc,
6061 int allflag, bool raw_loc)
6062 {
6063 struct command_line *l;
6064 static char bpenables[] = "nynny";
6065
6066 struct ui_out *uiout = current_uiout;
6067 int header_of_multiple = 0;
6068 int part_of_multiple = (loc != NULL);
6069 struct value_print_options opts;
6070
6071 get_user_print_options (&opts);
6072
6073 gdb_assert (!loc || loc_number != 0);
6074 /* See comment in print_one_breakpoint concerning treatment of
6075 breakpoints with single disabled location. */
6076 if (loc == NULL
6077 && (b->loc != NULL
6078 && (b->loc->next != NULL
6079 || !b->loc->enabled || b->loc->disabled_by_cond)))
6080 header_of_multiple = 1;
6081 if (loc == NULL)
6082 loc = b->loc;
6083
6084 annotate_record ();
6085
6086 /* 1 */
6087 annotate_field (0);
6088 if (part_of_multiple)
6089 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6090 else
6091 uiout->field_signed ("number", b->number);
6092
6093 /* 2 */
6094 annotate_field (1);
6095 if (part_of_multiple)
6096 uiout->field_skip ("type");
6097 else
6098 uiout->field_string ("type", bptype_string (b->type));
6099
6100 /* 3 */
6101 annotate_field (2);
6102 if (part_of_multiple)
6103 uiout->field_skip ("disp");
6104 else
6105 uiout->field_string ("disp", bpdisp_text (b->disposition));
6106
6107 /* 4 */
6108 annotate_field (3);
6109 /* For locations that are disabled because of an invalid condition,
6110 display "N*" on CLI, where "*" refers to a footnote below the
6111 table. For MI, simply display a "N" without a footnote. */
6112 const char *N = (uiout->is_mi_like_p ()) ? "N" : "N*";
6113 if (part_of_multiple)
6114 uiout->field_string ("enabled", (loc->disabled_by_cond ? N
6115 : (loc->enabled ? "y" : "n")));
6116 else
6117 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6118
6119 /* 5 and 6 */
6120 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
6121 b->ops->print_one (b, last_loc);
6122 else
6123 {
6124 if (is_watchpoint (b))
6125 {
6126 struct watchpoint *w = (struct watchpoint *) b;
6127
6128 /* Field 4, the address, is omitted (which makes the columns
6129 not line up too nicely with the headers, but the effect
6130 is relatively readable). */
6131 if (opts.addressprint)
6132 uiout->field_skip ("addr");
6133 annotate_field (5);
6134 uiout->field_string ("what", w->exp_string.get ());
6135 }
6136 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6137 || is_ada_exception_catchpoint (b))
6138 {
6139 if (opts.addressprint)
6140 {
6141 annotate_field (4);
6142 if (header_of_multiple)
6143 uiout->field_string ("addr", "<MULTIPLE>",
6144 metadata_style.style ());
6145 else if (b->loc == NULL || loc->shlib_disabled)
6146 uiout->field_string ("addr", "<PENDING>",
6147 metadata_style.style ());
6148 else
6149 uiout->field_core_addr ("addr",
6150 loc->gdbarch, loc->address);
6151 }
6152 annotate_field (5);
6153 if (!header_of_multiple)
6154 print_breakpoint_location (b, loc);
6155 if (b->loc)
6156 *last_loc = b->loc;
6157 }
6158 }
6159
6160 if (loc != NULL && !header_of_multiple)
6161 {
6162 std::vector<int> inf_nums;
6163 int mi_only = 1;
6164
6165 for (inferior *inf : all_inferiors ())
6166 {
6167 if (inf->pspace == loc->pspace)
6168 inf_nums.push_back (inf->num);
6169 }
6170
6171 /* For backward compatibility, don't display inferiors in CLI unless
6172 there are several. Always display for MI. */
6173 if (allflag
6174 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6175 && (program_spaces.size () > 1
6176 || number_of_inferiors () > 1)
6177 /* LOC is for existing B, it cannot be in
6178 moribund_locations and thus having NULL OWNER. */
6179 && loc->owner->type != bp_catchpoint))
6180 mi_only = 0;
6181 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6182 }
6183
6184 if (!part_of_multiple)
6185 {
6186 if (b->thread != -1)
6187 {
6188 /* FIXME: This seems to be redundant and lost here; see the
6189 "stop only in" line a little further down. */
6190 uiout->text (" thread ");
6191 uiout->field_signed ("thread", b->thread);
6192 }
6193 else if (b->task != 0)
6194 {
6195 uiout->text (" task ");
6196 uiout->field_signed ("task", b->task);
6197 }
6198 }
6199
6200 uiout->text ("\n");
6201
6202 if (!part_of_multiple)
6203 b->ops->print_one_detail (b, uiout);
6204
6205 if (part_of_multiple && frame_id_p (b->frame_id))
6206 {
6207 annotate_field (6);
6208 uiout->text ("\tstop only in stack frame at ");
6209 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6210 the frame ID. */
6211 uiout->field_core_addr ("frame",
6212 b->gdbarch, b->frame_id.stack_addr);
6213 uiout->text ("\n");
6214 }
6215
6216 if (!part_of_multiple && b->cond_string)
6217 {
6218 annotate_field (7);
6219 if (is_tracepoint (b))
6220 uiout->text ("\ttrace only if ");
6221 else
6222 uiout->text ("\tstop only if ");
6223 uiout->field_string ("cond", b->cond_string.get ());
6224
6225 /* Print whether the target is doing the breakpoint's condition
6226 evaluation. If GDB is doing the evaluation, don't print anything. */
6227 if (is_breakpoint (b)
6228 && breakpoint_condition_evaluation_mode ()
6229 == condition_evaluation_target)
6230 {
6231 uiout->message (" (%pF evals)",
6232 string_field ("evaluated-by",
6233 bp_condition_evaluator (b)));
6234 }
6235 uiout->text ("\n");
6236 }
6237
6238 if (!part_of_multiple && b->thread != -1)
6239 {
6240 /* FIXME should make an annotation for this. */
6241 uiout->text ("\tstop only in thread ");
6242 if (uiout->is_mi_like_p ())
6243 uiout->field_signed ("thread", b->thread);
6244 else
6245 {
6246 struct thread_info *thr = find_thread_global_id (b->thread);
6247
6248 uiout->field_string ("thread", print_thread_id (thr));
6249 }
6250 uiout->text ("\n");
6251 }
6252
6253 if (!part_of_multiple)
6254 {
6255 if (b->hit_count)
6256 {
6257 /* FIXME should make an annotation for this. */
6258 if (is_catchpoint (b))
6259 uiout->text ("\tcatchpoint");
6260 else if (is_tracepoint (b))
6261 uiout->text ("\ttracepoint");
6262 else
6263 uiout->text ("\tbreakpoint");
6264 uiout->text (" already hit ");
6265 uiout->field_signed ("times", b->hit_count);
6266 if (b->hit_count == 1)
6267 uiout->text (" time\n");
6268 else
6269 uiout->text (" times\n");
6270 }
6271 else
6272 {
6273 /* Output the count also if it is zero, but only if this is mi. */
6274 if (uiout->is_mi_like_p ())
6275 uiout->field_signed ("times", b->hit_count);
6276 }
6277 }
6278
6279 if (!part_of_multiple && b->ignore_count)
6280 {
6281 annotate_field (8);
6282 uiout->message ("\tignore next %pF hits\n",
6283 signed_field ("ignore", b->ignore_count));
6284 }
6285
6286 /* Note that an enable count of 1 corresponds to "enable once"
6287 behavior, which is reported by the combination of enablement and
6288 disposition, so we don't need to mention it here. */
6289 if (!part_of_multiple && b->enable_count > 1)
6290 {
6291 annotate_field (8);
6292 uiout->text ("\tdisable after ");
6293 /* Tweak the wording to clarify that ignore and enable counts
6294 are distinct, and have additive effect. */
6295 if (b->ignore_count)
6296 uiout->text ("additional ");
6297 else
6298 uiout->text ("next ");
6299 uiout->field_signed ("enable", b->enable_count);
6300 uiout->text (" hits\n");
6301 }
6302
6303 if (!part_of_multiple && is_tracepoint (b))
6304 {
6305 struct tracepoint *tp = (struct tracepoint *) b;
6306
6307 if (tp->traceframe_usage)
6308 {
6309 uiout->text ("\ttrace buffer usage ");
6310 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6311 uiout->text (" bytes\n");
6312 }
6313 }
6314
6315 l = b->commands ? b->commands.get () : NULL;
6316 if (!part_of_multiple && l)
6317 {
6318 annotate_field (9);
6319 ui_out_emit_tuple tuple_emitter (uiout, "script");
6320 print_command_lines (uiout, l, 4);
6321 }
6322
6323 if (is_tracepoint (b))
6324 {
6325 struct tracepoint *t = (struct tracepoint *) b;
6326
6327 if (!part_of_multiple && t->pass_count)
6328 {
6329 annotate_field (10);
6330 uiout->text ("\tpass count ");
6331 uiout->field_signed ("pass", t->pass_count);
6332 uiout->text (" \n");
6333 }
6334
6335 /* Don't display it when tracepoint or tracepoint location is
6336 pending. */
6337 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6338 {
6339 annotate_field (11);
6340
6341 if (uiout->is_mi_like_p ())
6342 uiout->field_string ("installed",
6343 loc->inserted ? "y" : "n");
6344 else
6345 {
6346 if (loc->inserted)
6347 uiout->text ("\t");
6348 else
6349 uiout->text ("\tnot ");
6350 uiout->text ("installed on target\n");
6351 }
6352 }
6353 }
6354
6355 if (uiout->is_mi_like_p () && !part_of_multiple)
6356 {
6357 if (is_watchpoint (b))
6358 {
6359 struct watchpoint *w = (struct watchpoint *) b;
6360
6361 uiout->field_string ("original-location", w->exp_string.get ());
6362 }
6363 else if (b->location != NULL
6364 && event_location_to_string (b->location.get ()) != NULL)
6365 uiout->field_string ("original-location",
6366 event_location_to_string (b->location.get ()));
6367 }
6368 }
6369
6370 /* See breakpoint.h. */
6371
6372 bool fix_multi_location_breakpoint_output_globally = false;
6373
6374 static void
6375 print_one_breakpoint (struct breakpoint *b,
6376 struct bp_location **last_loc,
6377 int allflag)
6378 {
6379 struct ui_out *uiout = current_uiout;
6380 bool use_fixed_output
6381 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6382 || fix_multi_location_breakpoint_output_globally);
6383
6384 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6385 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
6386
6387 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6388 are outside. */
6389 if (!use_fixed_output)
6390 bkpt_tuple_emitter.reset ();
6391
6392 /* If this breakpoint has custom print function,
6393 it's already printed. Otherwise, print individual
6394 locations, if any. */
6395 if (b->ops == NULL
6396 || b->ops->print_one == NULL
6397 || allflag)
6398 {
6399 /* If breakpoint has a single location that is disabled, we
6400 print it as if it had several locations, since otherwise it's
6401 hard to represent "breakpoint enabled, location disabled"
6402 situation.
6403
6404 Note that while hardware watchpoints have several locations
6405 internally, that's not a property exposed to users.
6406
6407 Likewise, while catchpoints may be implemented with
6408 breakpoints (e.g., catch throw), that's not a property
6409 exposed to users. We do however display the internal
6410 breakpoint locations with "maint info breakpoints". */
6411 if (!is_hardware_watchpoint (b)
6412 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6413 || is_ada_exception_catchpoint (b))
6414 && (allflag
6415 || (b->loc && (b->loc->next
6416 || !b->loc->enabled
6417 || b->loc->disabled_by_cond))))
6418 {
6419 gdb::optional<ui_out_emit_list> locations_list;
6420
6421 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6422 MI record. For later versions, place breakpoint locations in a
6423 list. */
6424 if (uiout->is_mi_like_p () && use_fixed_output)
6425 locations_list.emplace (uiout, "locations");
6426
6427 int n = 1;
6428 for (bp_location *loc : b->locations ())
6429 {
6430 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6431 print_one_breakpoint_location (b, loc, n, last_loc,
6432 allflag, allflag);
6433 n++;
6434 }
6435 }
6436 }
6437 }
6438
6439 static int
6440 breakpoint_address_bits (struct breakpoint *b)
6441 {
6442 int print_address_bits = 0;
6443
6444 /* Software watchpoints that aren't watching memory don't have an
6445 address to print. */
6446 if (is_no_memory_software_watchpoint (b))
6447 return 0;
6448
6449 for (bp_location *loc : b->locations ())
6450 {
6451 int addr_bit;
6452
6453 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6454 if (addr_bit > print_address_bits)
6455 print_address_bits = addr_bit;
6456 }
6457
6458 return print_address_bits;
6459 }
6460
6461 /* See breakpoint.h. */
6462
6463 void
6464 print_breakpoint (breakpoint *b)
6465 {
6466 struct bp_location *dummy_loc = NULL;
6467 print_one_breakpoint (b, &dummy_loc, 0);
6468 }
6469
6470 /* Return true if this breakpoint was set by the user, false if it is
6471 internal or momentary. */
6472
6473 int
6474 user_breakpoint_p (struct breakpoint *b)
6475 {
6476 return b->number > 0;
6477 }
6478
6479 /* See breakpoint.h. */
6480
6481 int
6482 pending_breakpoint_p (struct breakpoint *b)
6483 {
6484 return b->loc == NULL;
6485 }
6486
6487 /* Print information on breakpoints (including watchpoints and tracepoints).
6488
6489 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6490 understood by number_or_range_parser. Only breakpoints included in this
6491 list are then printed.
6492
6493 If SHOW_INTERNAL is true, print internal breakpoints.
6494
6495 If FILTER is non-NULL, call it on each breakpoint and only include the
6496 ones for which it returns true.
6497
6498 Return the total number of breakpoints listed. */
6499
6500 static int
6501 breakpoint_1 (const char *bp_num_list, bool show_internal,
6502 bool (*filter) (const struct breakpoint *))
6503 {
6504 struct bp_location *last_loc = NULL;
6505 int nr_printable_breakpoints;
6506 struct value_print_options opts;
6507 int print_address_bits = 0;
6508 int print_type_col_width = 14;
6509 struct ui_out *uiout = current_uiout;
6510 bool has_disabled_by_cond_location = false;
6511
6512 get_user_print_options (&opts);
6513
6514 /* Compute the number of rows in the table, as well as the size
6515 required for address fields. */
6516 nr_printable_breakpoints = 0;
6517 for (breakpoint *b : all_breakpoints ())
6518 {
6519 /* If we have a filter, only list the breakpoints it accepts. */
6520 if (filter && !filter (b))
6521 continue;
6522
6523 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6524 accept. Skip the others. */
6525 if (bp_num_list != NULL && *bp_num_list != '\0')
6526 {
6527 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6528 continue;
6529 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6530 continue;
6531 }
6532
6533 if (show_internal || user_breakpoint_p (b))
6534 {
6535 int addr_bit, type_len;
6536
6537 addr_bit = breakpoint_address_bits (b);
6538 if (addr_bit > print_address_bits)
6539 print_address_bits = addr_bit;
6540
6541 type_len = strlen (bptype_string (b->type));
6542 if (type_len > print_type_col_width)
6543 print_type_col_width = type_len;
6544
6545 nr_printable_breakpoints++;
6546 }
6547 }
6548
6549 {
6550 ui_out_emit_table table_emitter (uiout,
6551 opts.addressprint ? 6 : 5,
6552 nr_printable_breakpoints,
6553 "BreakpointTable");
6554
6555 if (nr_printable_breakpoints > 0)
6556 annotate_breakpoints_headers ();
6557 if (nr_printable_breakpoints > 0)
6558 annotate_field (0);
6559 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6560 if (nr_printable_breakpoints > 0)
6561 annotate_field (1);
6562 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6563 if (nr_printable_breakpoints > 0)
6564 annotate_field (2);
6565 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6566 if (nr_printable_breakpoints > 0)
6567 annotate_field (3);
6568 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6569 if (opts.addressprint)
6570 {
6571 if (nr_printable_breakpoints > 0)
6572 annotate_field (4);
6573 if (print_address_bits <= 32)
6574 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6575 else
6576 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6577 }
6578 if (nr_printable_breakpoints > 0)
6579 annotate_field (5);
6580 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6581 uiout->table_body ();
6582 if (nr_printable_breakpoints > 0)
6583 annotate_breakpoints_table ();
6584
6585 for (breakpoint *b : all_breakpoints ())
6586 {
6587 QUIT;
6588 /* If we have a filter, only list the breakpoints it accepts. */
6589 if (filter && !filter (b))
6590 continue;
6591
6592 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6593 accept. Skip the others. */
6594
6595 if (bp_num_list != NULL && *bp_num_list != '\0')
6596 {
6597 if (show_internal) /* maintenance info breakpoint */
6598 {
6599 if (parse_and_eval_long (bp_num_list) != b->number)
6600 continue;
6601 }
6602 else /* all others */
6603 {
6604 if (!number_is_in_list (bp_num_list, b->number))
6605 continue;
6606 }
6607 }
6608 /* We only print out user settable breakpoints unless the
6609 show_internal is set. */
6610 if (show_internal || user_breakpoint_p (b))
6611 {
6612 print_one_breakpoint (b, &last_loc, show_internal);
6613 for (bp_location *loc : b->locations ())
6614 if (loc->disabled_by_cond)
6615 has_disabled_by_cond_location = true;
6616 }
6617 }
6618 }
6619
6620 if (nr_printable_breakpoints == 0)
6621 {
6622 /* If there's a filter, let the caller decide how to report
6623 empty list. */
6624 if (!filter)
6625 {
6626 if (bp_num_list == NULL || *bp_num_list == '\0')
6627 uiout->message ("No breakpoints or watchpoints.\n");
6628 else
6629 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6630 bp_num_list);
6631 }
6632 }
6633 else
6634 {
6635 if (last_loc && !server_command)
6636 set_next_address (last_loc->gdbarch, last_loc->address);
6637
6638 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
6639 uiout->message (_("(*): Breakpoint condition is invalid at this "
6640 "location.\n"));
6641 }
6642
6643 /* FIXME? Should this be moved up so that it is only called when
6644 there have been breakpoints? */
6645 annotate_breakpoints_table_end ();
6646
6647 return nr_printable_breakpoints;
6648 }
6649
6650 /* Display the value of default-collect in a way that is generally
6651 compatible with the breakpoint list. */
6652
6653 static void
6654 default_collect_info (void)
6655 {
6656 struct ui_out *uiout = current_uiout;
6657
6658 /* If it has no value (which is frequently the case), say nothing; a
6659 message like "No default-collect." gets in user's face when it's
6660 not wanted. */
6661 if (default_collect.empty ())
6662 return;
6663
6664 /* The following phrase lines up nicely with per-tracepoint collect
6665 actions. */
6666 uiout->text ("default collect ");
6667 uiout->field_string ("default-collect", default_collect);
6668 uiout->text (" \n");
6669 }
6670
6671 static void
6672 info_breakpoints_command (const char *args, int from_tty)
6673 {
6674 breakpoint_1 (args, false, NULL);
6675
6676 default_collect_info ();
6677 }
6678
6679 static void
6680 info_watchpoints_command (const char *args, int from_tty)
6681 {
6682 int num_printed = breakpoint_1 (args, false, is_watchpoint);
6683 struct ui_out *uiout = current_uiout;
6684
6685 if (num_printed == 0)
6686 {
6687 if (args == NULL || *args == '\0')
6688 uiout->message ("No watchpoints.\n");
6689 else
6690 uiout->message ("No watchpoint matching '%s'.\n", args);
6691 }
6692 }
6693
6694 static void
6695 maintenance_info_breakpoints (const char *args, int from_tty)
6696 {
6697 breakpoint_1 (args, true, NULL);
6698
6699 default_collect_info ();
6700 }
6701
6702 static int
6703 breakpoint_has_pc (struct breakpoint *b,
6704 struct program_space *pspace,
6705 CORE_ADDR pc, struct obj_section *section)
6706 {
6707 for (bp_location *bl : b->locations ())
6708 {
6709 if (bl->pspace == pspace
6710 && bl->address == pc
6711 && (!overlay_debugging || bl->section == section))
6712 return 1;
6713 }
6714 return 0;
6715 }
6716
6717 /* Print a message describing any user-breakpoints set at PC. This
6718 concerns with logical breakpoints, so we match program spaces, not
6719 address spaces. */
6720
6721 static void
6722 describe_other_breakpoints (struct gdbarch *gdbarch,
6723 struct program_space *pspace, CORE_ADDR pc,
6724 struct obj_section *section, int thread)
6725 {
6726 int others = 0;
6727
6728 for (breakpoint *b : all_breakpoints ())
6729 others += (user_breakpoint_p (b)
6730 && breakpoint_has_pc (b, pspace, pc, section));
6731
6732 if (others > 0)
6733 {
6734 if (others == 1)
6735 printf_filtered (_("Note: breakpoint "));
6736 else /* if (others == ???) */
6737 printf_filtered (_("Note: breakpoints "));
6738 for (breakpoint *b : all_breakpoints ())
6739 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6740 {
6741 others--;
6742 printf_filtered ("%d", b->number);
6743 if (b->thread == -1 && thread != -1)
6744 printf_filtered (" (all threads)");
6745 else if (b->thread != -1)
6746 printf_filtered (" (thread %d)", b->thread);
6747 printf_filtered ("%s%s ",
6748 ((b->enable_state == bp_disabled
6749 || b->enable_state == bp_call_disabled)
6750 ? " (disabled)"
6751 : ""),
6752 (others > 1) ? ","
6753 : ((others == 1) ? " and" : ""));
6754 }
6755 current_uiout->message (_("also set at pc %ps.\n"),
6756 styled_string (address_style.style (),
6757 paddress (gdbarch, pc)));
6758 }
6759 }
6760 \f
6761
6762 /* Return true iff it is meaningful to use the address member of LOC.
6763 For some breakpoint types, the locations' address members are
6764 irrelevant and it makes no sense to attempt to compare them to
6765 other addresses (or use them for any other purpose either).
6766
6767 More specifically, software watchpoints and catchpoints that are
6768 not backed by breakpoints always have a zero valued location
6769 address and we don't want to mark breakpoints of any of these types
6770 to be a duplicate of an actual breakpoint location at address
6771 zero. */
6772
6773 static bool
6774 bl_address_is_meaningful (bp_location *loc)
6775 {
6776 return loc->loc_type != bp_loc_other;
6777 }
6778
6779 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6780 true if LOC1 and LOC2 represent the same watchpoint location. */
6781
6782 static int
6783 watchpoint_locations_match (struct bp_location *loc1,
6784 struct bp_location *loc2)
6785 {
6786 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6787 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6788
6789 /* Both of them must exist. */
6790 gdb_assert (w1 != NULL);
6791 gdb_assert (w2 != NULL);
6792
6793 /* If the target can evaluate the condition expression in hardware,
6794 then we we need to insert both watchpoints even if they are at
6795 the same place. Otherwise the watchpoint will only trigger when
6796 the condition of whichever watchpoint was inserted evaluates to
6797 true, not giving a chance for GDB to check the condition of the
6798 other watchpoint. */
6799 if ((w1->cond_exp
6800 && target_can_accel_watchpoint_condition (loc1->address,
6801 loc1->length,
6802 loc1->watchpoint_type,
6803 w1->cond_exp.get ()))
6804 || (w2->cond_exp
6805 && target_can_accel_watchpoint_condition (loc2->address,
6806 loc2->length,
6807 loc2->watchpoint_type,
6808 w2->cond_exp.get ())))
6809 return 0;
6810
6811 /* Note that this checks the owner's type, not the location's. In
6812 case the target does not support read watchpoints, but does
6813 support access watchpoints, we'll have bp_read_watchpoint
6814 watchpoints with hw_access locations. Those should be considered
6815 duplicates of hw_read locations. The hw_read locations will
6816 become hw_access locations later. */
6817 return (loc1->owner->type == loc2->owner->type
6818 && loc1->pspace->aspace == loc2->pspace->aspace
6819 && loc1->address == loc2->address
6820 && loc1->length == loc2->length);
6821 }
6822
6823 /* See breakpoint.h. */
6824
6825 int
6826 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6827 const address_space *aspace2, CORE_ADDR addr2)
6828 {
6829 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6830 || aspace1 == aspace2)
6831 && addr1 == addr2);
6832 }
6833
6834 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6835 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6836 matches ASPACE2. On targets that have global breakpoints, the address
6837 space doesn't really matter. */
6838
6839 static int
6840 breakpoint_address_match_range (const address_space *aspace1,
6841 CORE_ADDR addr1,
6842 int len1, const address_space *aspace2,
6843 CORE_ADDR addr2)
6844 {
6845 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6846 || aspace1 == aspace2)
6847 && addr2 >= addr1 && addr2 < addr1 + len1);
6848 }
6849
6850 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6851 a ranged breakpoint. In most targets, a match happens only if ASPACE
6852 matches the breakpoint's address space. On targets that have global
6853 breakpoints, the address space doesn't really matter. */
6854
6855 static int
6856 breakpoint_location_address_match (struct bp_location *bl,
6857 const address_space *aspace,
6858 CORE_ADDR addr)
6859 {
6860 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6861 aspace, addr)
6862 || (bl->length
6863 && breakpoint_address_match_range (bl->pspace->aspace,
6864 bl->address, bl->length,
6865 aspace, addr)));
6866 }
6867
6868 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6869 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6870 match happens only if ASPACE matches the breakpoint's address
6871 space. On targets that have global breakpoints, the address space
6872 doesn't really matter. */
6873
6874 static int
6875 breakpoint_location_address_range_overlap (struct bp_location *bl,
6876 const address_space *aspace,
6877 CORE_ADDR addr, int len)
6878 {
6879 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6880 || bl->pspace->aspace == aspace)
6881 {
6882 int bl_len = bl->length != 0 ? bl->length : 1;
6883
6884 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6885 return 1;
6886 }
6887 return 0;
6888 }
6889
6890 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6891 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6892 true, otherwise returns false. */
6893
6894 static int
6895 tracepoint_locations_match (struct bp_location *loc1,
6896 struct bp_location *loc2)
6897 {
6898 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6899 /* Since tracepoint locations are never duplicated with others', tracepoint
6900 locations at the same address of different tracepoints are regarded as
6901 different locations. */
6902 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6903 else
6904 return 0;
6905 }
6906
6907 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6908 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
6909 the same location. If SW_HW_BPS_MATCH is true, then software
6910 breakpoint locations and hardware breakpoint locations match,
6911 otherwise they don't. */
6912
6913 static int
6914 breakpoint_locations_match (struct bp_location *loc1,
6915 struct bp_location *loc2,
6916 bool sw_hw_bps_match)
6917 {
6918 int hw_point1, hw_point2;
6919
6920 /* Both of them must not be in moribund_locations. */
6921 gdb_assert (loc1->owner != NULL);
6922 gdb_assert (loc2->owner != NULL);
6923
6924 hw_point1 = is_hardware_watchpoint (loc1->owner);
6925 hw_point2 = is_hardware_watchpoint (loc2->owner);
6926
6927 if (hw_point1 != hw_point2)
6928 return 0;
6929 else if (hw_point1)
6930 return watchpoint_locations_match (loc1, loc2);
6931 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6932 return tracepoint_locations_match (loc1, loc2);
6933 else
6934 /* We compare bp_location.length in order to cover ranged
6935 breakpoints. Keep this in sync with
6936 bp_location_is_less_than. */
6937 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6938 loc2->pspace->aspace, loc2->address)
6939 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
6940 && loc1->length == loc2->length);
6941 }
6942
6943 static void
6944 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6945 int bnum, int have_bnum)
6946 {
6947 /* The longest string possibly returned by hex_string_custom
6948 is 50 chars. These must be at least that big for safety. */
6949 char astr1[64];
6950 char astr2[64];
6951
6952 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6953 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
6954 if (have_bnum)
6955 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
6956 bnum, astr1, astr2);
6957 else
6958 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
6959 }
6960
6961 /* Adjust a breakpoint's address to account for architectural
6962 constraints on breakpoint placement. Return the adjusted address.
6963 Note: Very few targets require this kind of adjustment. For most
6964 targets, this function is simply the identity function. */
6965
6966 static CORE_ADDR
6967 adjust_breakpoint_address (struct gdbarch *gdbarch,
6968 CORE_ADDR bpaddr, enum bptype bptype)
6969 {
6970 if (bptype == bp_watchpoint
6971 || bptype == bp_hardware_watchpoint
6972 || bptype == bp_read_watchpoint
6973 || bptype == bp_access_watchpoint
6974 || bptype == bp_catchpoint)
6975 {
6976 /* Watchpoints and the various bp_catch_* eventpoints should not
6977 have their addresses modified. */
6978 return bpaddr;
6979 }
6980 else if (bptype == bp_single_step)
6981 {
6982 /* Single-step breakpoints should not have their addresses
6983 modified. If there's any architectural constrain that
6984 applies to this address, then it should have already been
6985 taken into account when the breakpoint was created in the
6986 first place. If we didn't do this, stepping through e.g.,
6987 Thumb-2 IT blocks would break. */
6988 return bpaddr;
6989 }
6990 else
6991 {
6992 CORE_ADDR adjusted_bpaddr = bpaddr;
6993
6994 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
6995 {
6996 /* Some targets have architectural constraints on the placement
6997 of breakpoint instructions. Obtain the adjusted address. */
6998 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
6999 }
7000
7001 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
7002
7003 /* An adjusted breakpoint address can significantly alter
7004 a user's expectations. Print a warning if an adjustment
7005 is required. */
7006 if (adjusted_bpaddr != bpaddr)
7007 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7008
7009 return adjusted_bpaddr;
7010 }
7011 }
7012
7013 static bp_loc_type
7014 bp_location_from_bp_type (bptype type)
7015 {
7016 switch (type)
7017 {
7018 case bp_breakpoint:
7019 case bp_single_step:
7020 case bp_until:
7021 case bp_finish:
7022 case bp_longjmp:
7023 case bp_longjmp_resume:
7024 case bp_longjmp_call_dummy:
7025 case bp_exception:
7026 case bp_exception_resume:
7027 case bp_step_resume:
7028 case bp_hp_step_resume:
7029 case bp_watchpoint_scope:
7030 case bp_call_dummy:
7031 case bp_std_terminate:
7032 case bp_shlib_event:
7033 case bp_thread_event:
7034 case bp_overlay_event:
7035 case bp_jit_event:
7036 case bp_longjmp_master:
7037 case bp_std_terminate_master:
7038 case bp_exception_master:
7039 case bp_gnu_ifunc_resolver:
7040 case bp_gnu_ifunc_resolver_return:
7041 case bp_dprintf:
7042 return bp_loc_software_breakpoint;
7043 case bp_hardware_breakpoint:
7044 return bp_loc_hardware_breakpoint;
7045 case bp_hardware_watchpoint:
7046 case bp_read_watchpoint:
7047 case bp_access_watchpoint:
7048 return bp_loc_hardware_watchpoint;
7049 case bp_watchpoint:
7050 case bp_catchpoint:
7051 case bp_tracepoint:
7052 case bp_fast_tracepoint:
7053 case bp_static_tracepoint:
7054 return bp_loc_other;
7055 default:
7056 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7057 }
7058 }
7059
7060 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7061 {
7062 this->owner = owner;
7063 this->cond_bytecode = NULL;
7064 this->shlib_disabled = 0;
7065 this->enabled = 1;
7066 this->disabled_by_cond = false;
7067
7068 this->loc_type = type;
7069
7070 if (this->loc_type == bp_loc_software_breakpoint
7071 || this->loc_type == bp_loc_hardware_breakpoint)
7072 mark_breakpoint_location_modified (this);
7073
7074 incref ();
7075 }
7076
7077 bp_location::bp_location (breakpoint *owner)
7078 : bp_location::bp_location (owner,
7079 bp_location_from_bp_type (owner->type))
7080 {
7081 }
7082
7083 /* Allocate a struct bp_location. */
7084
7085 static struct bp_location *
7086 allocate_bp_location (struct breakpoint *bpt)
7087 {
7088 return bpt->ops->allocate_location (bpt);
7089 }
7090
7091 /* Decrement reference count. If the reference count reaches 0,
7092 destroy the bp_location. Sets *BLP to NULL. */
7093
7094 static void
7095 decref_bp_location (struct bp_location **blp)
7096 {
7097 bp_location_ref_policy::decref (*blp);
7098 *blp = NULL;
7099 }
7100
7101 /* Add breakpoint B at the end of the global breakpoint chain. */
7102
7103 static breakpoint *
7104 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7105 {
7106 struct breakpoint *b1;
7107 struct breakpoint *result = b.get ();
7108
7109 /* Add this breakpoint to the end of the chain so that a list of
7110 breakpoints will come out in order of increasing numbers. */
7111
7112 b1 = breakpoint_chain;
7113 if (b1 == 0)
7114 breakpoint_chain = b.release ();
7115 else
7116 {
7117 while (b1->next)
7118 b1 = b1->next;
7119 b1->next = b.release ();
7120 }
7121
7122 return result;
7123 }
7124
7125 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7126
7127 static void
7128 init_raw_breakpoint_without_location (struct breakpoint *b,
7129 struct gdbarch *gdbarch,
7130 enum bptype bptype,
7131 const struct breakpoint_ops *ops)
7132 {
7133 gdb_assert (ops != NULL);
7134
7135 b->ops = ops;
7136 b->type = bptype;
7137 b->gdbarch = gdbarch;
7138 b->language = current_language->la_language;
7139 b->input_radix = input_radix;
7140 b->related_breakpoint = b;
7141 }
7142
7143 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7144 that has type BPTYPE and has no locations as yet. */
7145
7146 static struct breakpoint *
7147 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7148 enum bptype bptype,
7149 const struct breakpoint_ops *ops)
7150 {
7151 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7152
7153 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7154 return add_to_breakpoint_chain (std::move (b));
7155 }
7156
7157 /* Initialize loc->function_name. */
7158
7159 static void
7160 set_breakpoint_location_function (struct bp_location *loc)
7161 {
7162 gdb_assert (loc->owner != NULL);
7163
7164 if (loc->owner->type == bp_breakpoint
7165 || loc->owner->type == bp_hardware_breakpoint
7166 || is_tracepoint (loc->owner))
7167 {
7168 const char *function_name;
7169
7170 if (loc->msymbol != NULL
7171 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
7172 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
7173 {
7174 struct breakpoint *b = loc->owner;
7175
7176 function_name = loc->msymbol->linkage_name ();
7177
7178 if (b->type == bp_breakpoint && b->loc == loc
7179 && loc->next == NULL && b->related_breakpoint == b)
7180 {
7181 /* Create only the whole new breakpoint of this type but do not
7182 mess more complicated breakpoints with multiple locations. */
7183 b->type = bp_gnu_ifunc_resolver;
7184 /* Remember the resolver's address for use by the return
7185 breakpoint. */
7186 loc->related_address = loc->address;
7187 }
7188 }
7189 else
7190 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7191
7192 if (function_name)
7193 loc->function_name = make_unique_xstrdup (function_name);
7194 }
7195 }
7196
7197 /* Attempt to determine architecture of location identified by SAL. */
7198 struct gdbarch *
7199 get_sal_arch (struct symtab_and_line sal)
7200 {
7201 if (sal.section)
7202 return sal.section->objfile->arch ();
7203 if (sal.symtab)
7204 return SYMTAB_OBJFILE (sal.symtab)->arch ();
7205
7206 return NULL;
7207 }
7208
7209 /* Low level routine for partially initializing a breakpoint of type
7210 BPTYPE. The newly created breakpoint's address, section, source
7211 file name, and line number are provided by SAL.
7212
7213 It is expected that the caller will complete the initialization of
7214 the newly created breakpoint struct as well as output any status
7215 information regarding the creation of a new breakpoint. */
7216
7217 static void
7218 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7219 struct symtab_and_line sal, enum bptype bptype,
7220 const struct breakpoint_ops *ops)
7221 {
7222 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7223
7224 add_location_to_breakpoint (b, &sal);
7225
7226 if (bptype != bp_catchpoint)
7227 gdb_assert (sal.pspace != NULL);
7228
7229 /* Store the program space that was used to set the breakpoint,
7230 except for ordinary breakpoints, which are independent of the
7231 program space. */
7232 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7233 b->pspace = sal.pspace;
7234 }
7235
7236 /* set_raw_breakpoint is a low level routine for allocating and
7237 partially initializing a breakpoint of type BPTYPE. The newly
7238 created breakpoint's address, section, source file name, and line
7239 number are provided by SAL. The newly created and partially
7240 initialized breakpoint is added to the breakpoint chain and
7241 is also returned as the value of this function.
7242
7243 It is expected that the caller will complete the initialization of
7244 the newly created breakpoint struct as well as output any status
7245 information regarding the creation of a new breakpoint. In
7246 particular, set_raw_breakpoint does NOT set the breakpoint
7247 number! Care should be taken to not allow an error to occur
7248 prior to completing the initialization of the breakpoint. If this
7249 should happen, a bogus breakpoint will be left on the chain. */
7250
7251 struct breakpoint *
7252 set_raw_breakpoint (struct gdbarch *gdbarch,
7253 struct symtab_and_line sal, enum bptype bptype,
7254 const struct breakpoint_ops *ops)
7255 {
7256 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7257
7258 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7259 return add_to_breakpoint_chain (std::move (b));
7260 }
7261
7262 /* Call this routine when stepping and nexting to enable a breakpoint
7263 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7264 initiated the operation. */
7265
7266 void
7267 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7268 {
7269 int thread = tp->global_num;
7270
7271 /* To avoid having to rescan all objfile symbols at every step,
7272 we maintain a list of continually-inserted but always disabled
7273 longjmp "master" breakpoints. Here, we simply create momentary
7274 clones of those and enable them for the requested thread. */
7275 for (breakpoint *b : all_breakpoints_safe ())
7276 if (b->pspace == current_program_space
7277 && (b->type == bp_longjmp_master
7278 || b->type == bp_exception_master))
7279 {
7280 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7281 struct breakpoint *clone;
7282
7283 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7284 after their removal. */
7285 clone = momentary_breakpoint_from_master (b, type,
7286 &momentary_breakpoint_ops, 1);
7287 clone->thread = thread;
7288 }
7289
7290 tp->initiating_frame = frame;
7291 }
7292
7293 /* Delete all longjmp breakpoints from THREAD. */
7294 void
7295 delete_longjmp_breakpoint (int thread)
7296 {
7297 for (breakpoint *b : all_breakpoints_safe ())
7298 if (b->type == bp_longjmp || b->type == bp_exception)
7299 {
7300 if (b->thread == thread)
7301 delete_breakpoint (b);
7302 }
7303 }
7304
7305 void
7306 delete_longjmp_breakpoint_at_next_stop (int thread)
7307 {
7308 for (breakpoint *b : all_breakpoints_safe ())
7309 if (b->type == bp_longjmp || b->type == bp_exception)
7310 {
7311 if (b->thread == thread)
7312 b->disposition = disp_del_at_next_stop;
7313 }
7314 }
7315
7316 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7317 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7318 pointer to any of them. Return NULL if this system cannot place longjmp
7319 breakpoints. */
7320
7321 struct breakpoint *
7322 set_longjmp_breakpoint_for_call_dummy (void)
7323 {
7324 breakpoint *retval = nullptr;
7325
7326 for (breakpoint *b : all_breakpoints ())
7327 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7328 {
7329 struct breakpoint *new_b;
7330
7331 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7332 &momentary_breakpoint_ops,
7333 1);
7334 new_b->thread = inferior_thread ()->global_num;
7335
7336 /* Link NEW_B into the chain of RETVAL breakpoints. */
7337
7338 gdb_assert (new_b->related_breakpoint == new_b);
7339 if (retval == NULL)
7340 retval = new_b;
7341 new_b->related_breakpoint = retval;
7342 while (retval->related_breakpoint != new_b->related_breakpoint)
7343 retval = retval->related_breakpoint;
7344 retval->related_breakpoint = new_b;
7345 }
7346
7347 return retval;
7348 }
7349
7350 /* Verify all existing dummy frames and their associated breakpoints for
7351 TP. Remove those which can no longer be found in the current frame
7352 stack.
7353
7354 If the unwind fails then there is not sufficient information to discard
7355 dummy frames. In this case, elide the clean up and the dummy frames will
7356 be cleaned up next time this function is called from a location where
7357 unwinding is possible. */
7358
7359 void
7360 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7361 {
7362 struct breakpoint *b, *b_tmp;
7363
7364 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7365 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7366 {
7367 struct breakpoint *dummy_b = b->related_breakpoint;
7368
7369 /* Find the bp_call_dummy breakpoint in the list of breakpoints
7370 chained off b->related_breakpoint. */
7371 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7372 dummy_b = dummy_b->related_breakpoint;
7373
7374 /* If there was no bp_call_dummy breakpoint then there's nothing
7375 more to do. Or, if the dummy frame associated with the
7376 bp_call_dummy is still on the stack then we need to leave this
7377 bp_call_dummy in place. */
7378 if (dummy_b->type != bp_call_dummy
7379 || frame_find_by_id (dummy_b->frame_id) != NULL)
7380 continue;
7381
7382 /* We didn't find the dummy frame on the stack, this could be
7383 because we have longjmp'd to a stack frame that is previous to
7384 the dummy frame, or it could be because the stack unwind is
7385 broken at some point between the longjmp frame and the dummy
7386 frame.
7387
7388 Next we figure out why the stack unwind stopped. If it looks
7389 like the unwind is complete then we assume the dummy frame has
7390 been jumped over, however, if the unwind stopped for an
7391 unexpected reason then we assume the stack unwind is currently
7392 broken, and that we will (eventually) return to the dummy
7393 frame.
7394
7395 It might be tempting to consider using frame_id_inner here, but
7396 that is not safe. There is no guarantee that the stack frames
7397 we are looking at here are even on the same stack as the
7398 original dummy frame, hence frame_id_inner can't be used. See
7399 the comments on frame_id_inner for more details. */
7400 bool unwind_finished_unexpectedly = false;
7401 for (struct frame_info *fi = get_current_frame (); fi != nullptr; )
7402 {
7403 struct frame_info *prev = get_prev_frame (fi);
7404 if (prev == nullptr)
7405 {
7406 /* FI is the last stack frame. Why did this frame not
7407 unwind further? */
7408 auto stop_reason = get_frame_unwind_stop_reason (fi);
7409 if (stop_reason != UNWIND_NO_REASON
7410 && stop_reason != UNWIND_OUTERMOST)
7411 unwind_finished_unexpectedly = true;
7412 }
7413 fi = prev;
7414 }
7415 if (unwind_finished_unexpectedly)
7416 continue;
7417
7418 dummy_frame_discard (dummy_b->frame_id, tp);
7419
7420 while (b->related_breakpoint != b)
7421 {
7422 if (b_tmp == b->related_breakpoint)
7423 b_tmp = b->related_breakpoint->next;
7424 delete_breakpoint (b->related_breakpoint);
7425 }
7426 delete_breakpoint (b);
7427 }
7428 }
7429
7430 void
7431 enable_overlay_breakpoints (void)
7432 {
7433 for (breakpoint *b : all_breakpoints ())
7434 if (b->type == bp_overlay_event)
7435 {
7436 b->enable_state = bp_enabled;
7437 update_global_location_list (UGLL_MAY_INSERT);
7438 overlay_events_enabled = 1;
7439 }
7440 }
7441
7442 void
7443 disable_overlay_breakpoints (void)
7444 {
7445 for (breakpoint *b : all_breakpoints ())
7446 if (b->type == bp_overlay_event)
7447 {
7448 b->enable_state = bp_disabled;
7449 update_global_location_list (UGLL_DONT_INSERT);
7450 overlay_events_enabled = 0;
7451 }
7452 }
7453
7454 /* Set an active std::terminate breakpoint for each std::terminate
7455 master breakpoint. */
7456 void
7457 set_std_terminate_breakpoint (void)
7458 {
7459 for (breakpoint *b : all_breakpoints_safe ())
7460 if (b->pspace == current_program_space
7461 && b->type == bp_std_terminate_master)
7462 {
7463 momentary_breakpoint_from_master (b, bp_std_terminate,
7464 &momentary_breakpoint_ops, 1);
7465 }
7466 }
7467
7468 /* Delete all the std::terminate breakpoints. */
7469 void
7470 delete_std_terminate_breakpoint (void)
7471 {
7472 for (breakpoint *b : all_breakpoints_safe ())
7473 if (b->type == bp_std_terminate)
7474 delete_breakpoint (b);
7475 }
7476
7477 struct breakpoint *
7478 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7479 {
7480 struct breakpoint *b;
7481
7482 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7483 &internal_breakpoint_ops);
7484
7485 b->enable_state = bp_enabled;
7486 /* location has to be used or breakpoint_re_set will delete me. */
7487 b->location = new_address_location (b->loc->address, NULL, 0);
7488
7489 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7490
7491 return b;
7492 }
7493
7494 struct lang_and_radix
7495 {
7496 enum language lang;
7497 int radix;
7498 };
7499
7500 /* Create a breakpoint for JIT code registration and unregistration. */
7501
7502 struct breakpoint *
7503 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7504 {
7505 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7506 &internal_breakpoint_ops);
7507 }
7508
7509 /* Remove JIT code registration and unregistration breakpoint(s). */
7510
7511 void
7512 remove_jit_event_breakpoints (void)
7513 {
7514 for (breakpoint *b : all_breakpoints_safe ())
7515 if (b->type == bp_jit_event
7516 && b->loc->pspace == current_program_space)
7517 delete_breakpoint (b);
7518 }
7519
7520 void
7521 remove_solib_event_breakpoints (void)
7522 {
7523 for (breakpoint *b : all_breakpoints_safe ())
7524 if (b->type == bp_shlib_event
7525 && b->loc->pspace == current_program_space)
7526 delete_breakpoint (b);
7527 }
7528
7529 /* See breakpoint.h. */
7530
7531 void
7532 remove_solib_event_breakpoints_at_next_stop (void)
7533 {
7534 for (breakpoint *b : all_breakpoints_safe ())
7535 if (b->type == bp_shlib_event
7536 && b->loc->pspace == current_program_space)
7537 b->disposition = disp_del_at_next_stop;
7538 }
7539
7540 /* Helper for create_solib_event_breakpoint /
7541 create_and_insert_solib_event_breakpoint. Allows specifying which
7542 INSERT_MODE to pass through to update_global_location_list. */
7543
7544 static struct breakpoint *
7545 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7546 enum ugll_insert_mode insert_mode)
7547 {
7548 struct breakpoint *b;
7549
7550 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7551 &internal_breakpoint_ops);
7552 update_global_location_list_nothrow (insert_mode);
7553 return b;
7554 }
7555
7556 struct breakpoint *
7557 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7558 {
7559 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7560 }
7561
7562 /* See breakpoint.h. */
7563
7564 struct breakpoint *
7565 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7566 {
7567 struct breakpoint *b;
7568
7569 /* Explicitly tell update_global_location_list to insert
7570 locations. */
7571 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7572 if (!b->loc->inserted)
7573 {
7574 delete_breakpoint (b);
7575 return NULL;
7576 }
7577 return b;
7578 }
7579
7580 /* Disable any breakpoints that are on code in shared libraries. Only
7581 apply to enabled breakpoints, disabled ones can just stay disabled. */
7582
7583 void
7584 disable_breakpoints_in_shlibs (void)
7585 {
7586 for (bp_location *loc : all_bp_locations ())
7587 {
7588 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7589 struct breakpoint *b = loc->owner;
7590
7591 /* We apply the check to all breakpoints, including disabled for
7592 those with loc->duplicate set. This is so that when breakpoint
7593 becomes enabled, or the duplicate is removed, gdb will try to
7594 insert all breakpoints. If we don't set shlib_disabled here,
7595 we'll try to insert those breakpoints and fail. */
7596 if (((b->type == bp_breakpoint)
7597 || (b->type == bp_jit_event)
7598 || (b->type == bp_hardware_breakpoint)
7599 || (is_tracepoint (b)))
7600 && loc->pspace == current_program_space
7601 && !loc->shlib_disabled
7602 && solib_name_from_address (loc->pspace, loc->address)
7603 )
7604 {
7605 loc->shlib_disabled = 1;
7606 }
7607 }
7608 }
7609
7610 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7611 notification of unloaded_shlib. Only apply to enabled breakpoints,
7612 disabled ones can just stay disabled. */
7613
7614 static void
7615 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7616 {
7617 int disabled_shlib_breaks = 0;
7618
7619 for (bp_location *loc : all_bp_locations ())
7620 {
7621 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7622 struct breakpoint *b = loc->owner;
7623
7624 if (solib->pspace == loc->pspace
7625 && !loc->shlib_disabled
7626 && (((b->type == bp_breakpoint
7627 || b->type == bp_jit_event
7628 || b->type == bp_hardware_breakpoint)
7629 && (loc->loc_type == bp_loc_hardware_breakpoint
7630 || loc->loc_type == bp_loc_software_breakpoint))
7631 || is_tracepoint (b))
7632 && solib_contains_address_p (solib, loc->address))
7633 {
7634 loc->shlib_disabled = 1;
7635 /* At this point, we cannot rely on remove_breakpoint
7636 succeeding so we must mark the breakpoint as not inserted
7637 to prevent future errors occurring in remove_breakpoints. */
7638 loc->inserted = 0;
7639
7640 /* This may cause duplicate notifications for the same breakpoint. */
7641 gdb::observers::breakpoint_modified.notify (b);
7642
7643 if (!disabled_shlib_breaks)
7644 {
7645 target_terminal::ours_for_output ();
7646 warning (_("Temporarily disabling breakpoints "
7647 "for unloaded shared library \"%s\""),
7648 solib->so_name);
7649 }
7650 disabled_shlib_breaks = 1;
7651 }
7652 }
7653 }
7654
7655 /* Disable any breakpoints and tracepoints in OBJFILE upon
7656 notification of free_objfile. Only apply to enabled breakpoints,
7657 disabled ones can just stay disabled. */
7658
7659 static void
7660 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7661 {
7662 if (objfile == NULL)
7663 return;
7664
7665 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7666 managed by the user with add-symbol-file/remove-symbol-file.
7667 Similarly to how breakpoints in shared libraries are handled in
7668 response to "nosharedlibrary", mark breakpoints in such modules
7669 shlib_disabled so they end up uninserted on the next global
7670 location list update. Shared libraries not loaded by the user
7671 aren't handled here -- they're already handled in
7672 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7673 solib_unloaded observer. We skip objfiles that are not
7674 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7675 main objfile). */
7676 if ((objfile->flags & OBJF_SHARED) == 0
7677 || (objfile->flags & OBJF_USERLOADED) == 0)
7678 return;
7679
7680 for (breakpoint *b : all_breakpoints ())
7681 {
7682 int bp_modified = 0;
7683
7684 if (!is_breakpoint (b) && !is_tracepoint (b))
7685 continue;
7686
7687 for (bp_location *loc : b->locations ())
7688 {
7689 CORE_ADDR loc_addr = loc->address;
7690
7691 if (loc->loc_type != bp_loc_hardware_breakpoint
7692 && loc->loc_type != bp_loc_software_breakpoint)
7693 continue;
7694
7695 if (loc->shlib_disabled != 0)
7696 continue;
7697
7698 if (objfile->pspace != loc->pspace)
7699 continue;
7700
7701 if (loc->loc_type != bp_loc_hardware_breakpoint
7702 && loc->loc_type != bp_loc_software_breakpoint)
7703 continue;
7704
7705 if (is_addr_in_objfile (loc_addr, objfile))
7706 {
7707 loc->shlib_disabled = 1;
7708 /* At this point, we don't know whether the object was
7709 unmapped from the inferior or not, so leave the
7710 inserted flag alone. We'll handle failure to
7711 uninsert quietly, in case the object was indeed
7712 unmapped. */
7713
7714 mark_breakpoint_location_modified (loc);
7715
7716 bp_modified = 1;
7717 }
7718 }
7719
7720 if (bp_modified)
7721 gdb::observers::breakpoint_modified.notify (b);
7722 }
7723 }
7724
7725 /* FORK & VFORK catchpoints. */
7726
7727 /* An instance of this type is used to represent a fork or vfork
7728 catchpoint. A breakpoint is really of this type iff its ops pointer points
7729 to CATCH_FORK_BREAKPOINT_OPS. */
7730
7731 struct fork_catchpoint : public breakpoint
7732 {
7733 /* Process id of a child process whose forking triggered this
7734 catchpoint. This field is only valid immediately after this
7735 catchpoint has triggered. */
7736 ptid_t forked_inferior_pid;
7737 };
7738
7739 /* Implement the "insert" breakpoint_ops method for fork
7740 catchpoints. */
7741
7742 static int
7743 insert_catch_fork (struct bp_location *bl)
7744 {
7745 return target_insert_fork_catchpoint (inferior_ptid.pid ());
7746 }
7747
7748 /* Implement the "remove" breakpoint_ops method for fork
7749 catchpoints. */
7750
7751 static int
7752 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
7753 {
7754 return target_remove_fork_catchpoint (inferior_ptid.pid ());
7755 }
7756
7757 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7758 catchpoints. */
7759
7760 static int
7761 breakpoint_hit_catch_fork (const struct bp_location *bl,
7762 const address_space *aspace, CORE_ADDR bp_addr,
7763 const target_waitstatus &ws)
7764 {
7765 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7766
7767 if (ws.kind () != TARGET_WAITKIND_FORKED)
7768 return 0;
7769
7770 c->forked_inferior_pid = ws.child_ptid ();
7771 return 1;
7772 }
7773
7774 /* Implement the "print_it" breakpoint_ops method for fork
7775 catchpoints. */
7776
7777 static enum print_stop_action
7778 print_it_catch_fork (bpstat *bs)
7779 {
7780 struct ui_out *uiout = current_uiout;
7781 struct breakpoint *b = bs->breakpoint_at;
7782 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7783
7784 annotate_catchpoint (b->number);
7785 maybe_print_thread_hit_breakpoint (uiout);
7786 if (b->disposition == disp_del)
7787 uiout->text ("Temporary catchpoint ");
7788 else
7789 uiout->text ("Catchpoint ");
7790 if (uiout->is_mi_like_p ())
7791 {
7792 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7793 uiout->field_string ("disp", bpdisp_text (b->disposition));
7794 }
7795 uiout->field_signed ("bkptno", b->number);
7796 uiout->text (" (forked process ");
7797 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7798 uiout->text ("), ");
7799 return PRINT_SRC_AND_LOC;
7800 }
7801
7802 /* Implement the "print_one" breakpoint_ops method for fork
7803 catchpoints. */
7804
7805 static void
7806 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7807 {
7808 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7809 struct value_print_options opts;
7810 struct ui_out *uiout = current_uiout;
7811
7812 get_user_print_options (&opts);
7813
7814 /* Field 4, the address, is omitted (which makes the columns not
7815 line up too nicely with the headers, but the effect is relatively
7816 readable). */
7817 if (opts.addressprint)
7818 uiout->field_skip ("addr");
7819 annotate_field (5);
7820 uiout->text ("fork");
7821 if (c->forked_inferior_pid != null_ptid)
7822 {
7823 uiout->text (", process ");
7824 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7825 uiout->spaces (1);
7826 }
7827
7828 if (uiout->is_mi_like_p ())
7829 uiout->field_string ("catch-type", "fork");
7830 }
7831
7832 /* Implement the "print_mention" breakpoint_ops method for fork
7833 catchpoints. */
7834
7835 static void
7836 print_mention_catch_fork (struct breakpoint *b)
7837 {
7838 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7839 }
7840
7841 /* Implement the "print_recreate" breakpoint_ops method for fork
7842 catchpoints. */
7843
7844 static void
7845 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7846 {
7847 fprintf_unfiltered (fp, "catch fork");
7848 print_recreate_thread (b, fp);
7849 }
7850
7851 /* The breakpoint_ops structure to be used in fork catchpoints. */
7852
7853 static struct breakpoint_ops catch_fork_breakpoint_ops;
7854
7855 /* Implement the "insert" breakpoint_ops method for vfork
7856 catchpoints. */
7857
7858 static int
7859 insert_catch_vfork (struct bp_location *bl)
7860 {
7861 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
7862 }
7863
7864 /* Implement the "remove" breakpoint_ops method for vfork
7865 catchpoints. */
7866
7867 static int
7868 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
7869 {
7870 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
7871 }
7872
7873 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7874 catchpoints. */
7875
7876 static int
7877 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7878 const address_space *aspace, CORE_ADDR bp_addr,
7879 const target_waitstatus &ws)
7880 {
7881 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7882
7883 if (ws.kind () != TARGET_WAITKIND_VFORKED)
7884 return 0;
7885
7886 c->forked_inferior_pid = ws.child_ptid ();
7887 return 1;
7888 }
7889
7890 /* Implement the "print_it" breakpoint_ops method for vfork
7891 catchpoints. */
7892
7893 static enum print_stop_action
7894 print_it_catch_vfork (bpstat *bs)
7895 {
7896 struct ui_out *uiout = current_uiout;
7897 struct breakpoint *b = bs->breakpoint_at;
7898 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7899
7900 annotate_catchpoint (b->number);
7901 maybe_print_thread_hit_breakpoint (uiout);
7902 if (b->disposition == disp_del)
7903 uiout->text ("Temporary catchpoint ");
7904 else
7905 uiout->text ("Catchpoint ");
7906 if (uiout->is_mi_like_p ())
7907 {
7908 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7909 uiout->field_string ("disp", bpdisp_text (b->disposition));
7910 }
7911 uiout->field_signed ("bkptno", b->number);
7912 uiout->text (" (vforked process ");
7913 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7914 uiout->text ("), ");
7915 return PRINT_SRC_AND_LOC;
7916 }
7917
7918 /* Implement the "print_one" breakpoint_ops method for vfork
7919 catchpoints. */
7920
7921 static void
7922 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7923 {
7924 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7925 struct value_print_options opts;
7926 struct ui_out *uiout = current_uiout;
7927
7928 get_user_print_options (&opts);
7929 /* Field 4, the address, is omitted (which makes the columns not
7930 line up too nicely with the headers, but the effect is relatively
7931 readable). */
7932 if (opts.addressprint)
7933 uiout->field_skip ("addr");
7934 annotate_field (5);
7935 uiout->text ("vfork");
7936 if (c->forked_inferior_pid != null_ptid)
7937 {
7938 uiout->text (", process ");
7939 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7940 uiout->spaces (1);
7941 }
7942
7943 if (uiout->is_mi_like_p ())
7944 uiout->field_string ("catch-type", "vfork");
7945 }
7946
7947 /* Implement the "print_mention" breakpoint_ops method for vfork
7948 catchpoints. */
7949
7950 static void
7951 print_mention_catch_vfork (struct breakpoint *b)
7952 {
7953 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7954 }
7955
7956 /* Implement the "print_recreate" breakpoint_ops method for vfork
7957 catchpoints. */
7958
7959 static void
7960 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7961 {
7962 fprintf_unfiltered (fp, "catch vfork");
7963 print_recreate_thread (b, fp);
7964 }
7965
7966 /* The breakpoint_ops structure to be used in vfork catchpoints. */
7967
7968 static struct breakpoint_ops catch_vfork_breakpoint_ops;
7969
7970 /* An instance of this type is used to represent an solib catchpoint.
7971 A breakpoint is really of this type iff its ops pointer points to
7972 CATCH_SOLIB_BREAKPOINT_OPS. */
7973
7974 struct solib_catchpoint : public breakpoint
7975 {
7976 /* True for "catch load", false for "catch unload". */
7977 bool is_load;
7978
7979 /* Regular expression to match, if any. COMPILED is only valid when
7980 REGEX is non-NULL. */
7981 gdb::unique_xmalloc_ptr<char> regex;
7982 std::unique_ptr<compiled_regex> compiled;
7983 };
7984
7985 static int
7986 insert_catch_solib (struct bp_location *ignore)
7987 {
7988 return 0;
7989 }
7990
7991 static int
7992 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
7993 {
7994 return 0;
7995 }
7996
7997 static int
7998 breakpoint_hit_catch_solib (const struct bp_location *bl,
7999 const address_space *aspace,
8000 CORE_ADDR bp_addr,
8001 const target_waitstatus &ws)
8002 {
8003 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8004
8005 if (ws.kind () == TARGET_WAITKIND_LOADED)
8006 return 1;
8007
8008 for (breakpoint *other : all_breakpoints ())
8009 {
8010 if (other == bl->owner)
8011 continue;
8012
8013 if (other->type != bp_shlib_event)
8014 continue;
8015
8016 if (self->pspace != NULL && other->pspace != self->pspace)
8017 continue;
8018
8019 for (bp_location *other_bl : other->locations ())
8020 {
8021 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8022 return 1;
8023 }
8024 }
8025
8026 return 0;
8027 }
8028
8029 static void
8030 check_status_catch_solib (struct bpstat *bs)
8031 {
8032 struct solib_catchpoint *self
8033 = (struct solib_catchpoint *) bs->breakpoint_at;
8034
8035 if (self->is_load)
8036 {
8037 for (so_list *iter : current_program_space->added_solibs)
8038 {
8039 if (!self->regex
8040 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
8041 return;
8042 }
8043 }
8044 else
8045 {
8046 for (const std::string &iter : current_program_space->deleted_solibs)
8047 {
8048 if (!self->regex
8049 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
8050 return;
8051 }
8052 }
8053
8054 bs->stop = 0;
8055 bs->print_it = print_it_noop;
8056 }
8057
8058 static enum print_stop_action
8059 print_it_catch_solib (bpstat *bs)
8060 {
8061 struct breakpoint *b = bs->breakpoint_at;
8062 struct ui_out *uiout = current_uiout;
8063
8064 annotate_catchpoint (b->number);
8065 maybe_print_thread_hit_breakpoint (uiout);
8066 if (b->disposition == disp_del)
8067 uiout->text ("Temporary catchpoint ");
8068 else
8069 uiout->text ("Catchpoint ");
8070 uiout->field_signed ("bkptno", b->number);
8071 uiout->text ("\n");
8072 if (uiout->is_mi_like_p ())
8073 uiout->field_string ("disp", bpdisp_text (b->disposition));
8074 print_solib_event (1);
8075 return PRINT_SRC_AND_LOC;
8076 }
8077
8078 static void
8079 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8080 {
8081 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8082 struct value_print_options opts;
8083 struct ui_out *uiout = current_uiout;
8084
8085 get_user_print_options (&opts);
8086 /* Field 4, the address, is omitted (which makes the columns not
8087 line up too nicely with the headers, but the effect is relatively
8088 readable). */
8089 if (opts.addressprint)
8090 {
8091 annotate_field (4);
8092 uiout->field_skip ("addr");
8093 }
8094
8095 std::string msg;
8096 annotate_field (5);
8097 if (self->is_load)
8098 {
8099 if (self->regex)
8100 msg = string_printf (_("load of library matching %s"),
8101 self->regex.get ());
8102 else
8103 msg = _("load of library");
8104 }
8105 else
8106 {
8107 if (self->regex)
8108 msg = string_printf (_("unload of library matching %s"),
8109 self->regex.get ());
8110 else
8111 msg = _("unload of library");
8112 }
8113 uiout->field_string ("what", msg);
8114
8115 if (uiout->is_mi_like_p ())
8116 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8117 }
8118
8119 static void
8120 print_mention_catch_solib (struct breakpoint *b)
8121 {
8122 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8123
8124 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8125 self->is_load ? "load" : "unload");
8126 }
8127
8128 static void
8129 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8130 {
8131 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8132
8133 fprintf_unfiltered (fp, "%s %s",
8134 b->disposition == disp_del ? "tcatch" : "catch",
8135 self->is_load ? "load" : "unload");
8136 if (self->regex)
8137 fprintf_unfiltered (fp, " %s", self->regex.get ());
8138 fprintf_unfiltered (fp, "\n");
8139 }
8140
8141 static struct breakpoint_ops catch_solib_breakpoint_ops;
8142
8143 /* See breakpoint.h. */
8144
8145 void
8146 add_solib_catchpoint (const char *arg, bool is_load, bool is_temp, bool enabled)
8147 {
8148 struct gdbarch *gdbarch = get_current_arch ();
8149
8150 if (!arg)
8151 arg = "";
8152 arg = skip_spaces (arg);
8153
8154 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
8155
8156 if (*arg != '\0')
8157 {
8158 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8159 _("Invalid regexp")));
8160 c->regex = make_unique_xstrdup (arg);
8161 }
8162
8163 c->is_load = is_load;
8164 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
8165 &catch_solib_breakpoint_ops);
8166
8167 c->enable_state = enabled ? bp_enabled : bp_disabled;
8168
8169 install_breakpoint (0, std::move (c), 1);
8170 }
8171
8172 /* A helper function that does all the work for "catch load" and
8173 "catch unload". */
8174
8175 static void
8176 catch_load_or_unload (const char *arg, int from_tty, int is_load,
8177 struct cmd_list_element *command)
8178 {
8179 const int enabled = 1;
8180 bool temp = command->context () == CATCH_TEMPORARY;
8181
8182 add_solib_catchpoint (arg, is_load, temp, enabled);
8183 }
8184
8185 static void
8186 catch_load_command_1 (const char *arg, int from_tty,
8187 struct cmd_list_element *command)
8188 {
8189 catch_load_or_unload (arg, from_tty, 1, command);
8190 }
8191
8192 static void
8193 catch_unload_command_1 (const char *arg, int from_tty,
8194 struct cmd_list_element *command)
8195 {
8196 catch_load_or_unload (arg, from_tty, 0, command);
8197 }
8198
8199 /* See breakpoint.h. */
8200
8201 void
8202 init_catchpoint (struct breakpoint *b,
8203 struct gdbarch *gdbarch, bool temp,
8204 const char *cond_string,
8205 const struct breakpoint_ops *ops)
8206 {
8207 symtab_and_line sal;
8208 sal.pspace = current_program_space;
8209
8210 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8211
8212 if (cond_string == nullptr)
8213 b->cond_string.reset ();
8214 else
8215 b->cond_string = make_unique_xstrdup (cond_string);
8216 b->disposition = temp ? disp_del : disp_donttouch;
8217 }
8218
8219 void
8220 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8221 {
8222 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8223 set_breakpoint_number (internal, b);
8224 if (is_tracepoint (b))
8225 set_tracepoint_count (breakpoint_count);
8226 if (!internal)
8227 mention (b);
8228 gdb::observers::breakpoint_created.notify (b);
8229
8230 if (update_gll)
8231 update_global_location_list (UGLL_MAY_INSERT);
8232 }
8233
8234 static void
8235 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8236 bool temp, const char *cond_string,
8237 const struct breakpoint_ops *ops)
8238 {
8239 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
8240
8241 init_catchpoint (c.get (), gdbarch, temp, cond_string, ops);
8242
8243 c->forked_inferior_pid = null_ptid;
8244
8245 install_breakpoint (0, std::move (c), 1);
8246 }
8247
8248 /* Exec catchpoints. */
8249
8250 /* An instance of this type is used to represent an exec catchpoint.
8251 A breakpoint is really of this type iff its ops pointer points to
8252 CATCH_EXEC_BREAKPOINT_OPS. */
8253
8254 struct exec_catchpoint : public breakpoint
8255 {
8256 /* Filename of a program whose exec triggered this catchpoint.
8257 This field is only valid immediately after this catchpoint has
8258 triggered. */
8259 gdb::unique_xmalloc_ptr<char> exec_pathname;
8260 };
8261
8262 static int
8263 insert_catch_exec (struct bp_location *bl)
8264 {
8265 return target_insert_exec_catchpoint (inferior_ptid.pid ());
8266 }
8267
8268 static int
8269 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8270 {
8271 return target_remove_exec_catchpoint (inferior_ptid.pid ());
8272 }
8273
8274 static int
8275 breakpoint_hit_catch_exec (const struct bp_location *bl,
8276 const address_space *aspace, CORE_ADDR bp_addr,
8277 const target_waitstatus &ws)
8278 {
8279 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8280
8281 if (ws.kind () != TARGET_WAITKIND_EXECD)
8282 return 0;
8283
8284 c->exec_pathname = make_unique_xstrdup (ws.execd_pathname ());
8285 return 1;
8286 }
8287
8288 static enum print_stop_action
8289 print_it_catch_exec (bpstat *bs)
8290 {
8291 struct ui_out *uiout = current_uiout;
8292 struct breakpoint *b = bs->breakpoint_at;
8293 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8294
8295 annotate_catchpoint (b->number);
8296 maybe_print_thread_hit_breakpoint (uiout);
8297 if (b->disposition == disp_del)
8298 uiout->text ("Temporary catchpoint ");
8299 else
8300 uiout->text ("Catchpoint ");
8301 if (uiout->is_mi_like_p ())
8302 {
8303 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8304 uiout->field_string ("disp", bpdisp_text (b->disposition));
8305 }
8306 uiout->field_signed ("bkptno", b->number);
8307 uiout->text (" (exec'd ");
8308 uiout->field_string ("new-exec", c->exec_pathname.get ());
8309 uiout->text ("), ");
8310
8311 return PRINT_SRC_AND_LOC;
8312 }
8313
8314 static void
8315 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8316 {
8317 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8318 struct value_print_options opts;
8319 struct ui_out *uiout = current_uiout;
8320
8321 get_user_print_options (&opts);
8322
8323 /* Field 4, the address, is omitted (which makes the columns
8324 not line up too nicely with the headers, but the effect
8325 is relatively readable). */
8326 if (opts.addressprint)
8327 uiout->field_skip ("addr");
8328 annotate_field (5);
8329 uiout->text ("exec");
8330 if (c->exec_pathname != NULL)
8331 {
8332 uiout->text (", program \"");
8333 uiout->field_string ("what", c->exec_pathname.get ());
8334 uiout->text ("\" ");
8335 }
8336
8337 if (uiout->is_mi_like_p ())
8338 uiout->field_string ("catch-type", "exec");
8339 }
8340
8341 static void
8342 print_mention_catch_exec (struct breakpoint *b)
8343 {
8344 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8345 }
8346
8347 /* Implement the "print_recreate" breakpoint_ops method for exec
8348 catchpoints. */
8349
8350 static void
8351 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8352 {
8353 fprintf_unfiltered (fp, "catch exec");
8354 print_recreate_thread (b, fp);
8355 }
8356
8357 static struct breakpoint_ops catch_exec_breakpoint_ops;
8358
8359 static int
8360 hw_breakpoint_used_count (void)
8361 {
8362 int i = 0;
8363
8364 for (breakpoint *b : all_breakpoints ())
8365 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8366 for (bp_location *bl : b->locations ())
8367 {
8368 /* Special types of hardware breakpoints may use more than
8369 one register. */
8370 i += b->ops->resources_needed (bl);
8371 }
8372
8373 return i;
8374 }
8375
8376 /* Returns the resources B would use if it were a hardware
8377 watchpoint. */
8378
8379 static int
8380 hw_watchpoint_use_count (struct breakpoint *b)
8381 {
8382 int i = 0;
8383
8384 if (!breakpoint_enabled (b))
8385 return 0;
8386
8387 for (bp_location *bl : b->locations ())
8388 {
8389 /* Special types of hardware watchpoints may use more than
8390 one register. */
8391 i += b->ops->resources_needed (bl);
8392 }
8393
8394 return i;
8395 }
8396
8397 /* Returns the sum the used resources of all hardware watchpoints of
8398 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8399 the sum of the used resources of all hardware watchpoints of other
8400 types _not_ TYPE. */
8401
8402 static int
8403 hw_watchpoint_used_count_others (struct breakpoint *except,
8404 enum bptype type, int *other_type_used)
8405 {
8406 int i = 0;
8407
8408 *other_type_used = 0;
8409 for (breakpoint *b : all_breakpoints ())
8410 {
8411 if (b == except)
8412 continue;
8413 if (!breakpoint_enabled (b))
8414 continue;
8415
8416 if (b->type == type)
8417 i += hw_watchpoint_use_count (b);
8418 else if (is_hardware_watchpoint (b))
8419 *other_type_used = 1;
8420 }
8421
8422 return i;
8423 }
8424
8425 void
8426 disable_watchpoints_before_interactive_call_start (void)
8427 {
8428 for (breakpoint *b : all_breakpoints ())
8429 if (is_watchpoint (b) && breakpoint_enabled (b))
8430 {
8431 b->enable_state = bp_call_disabled;
8432 update_global_location_list (UGLL_DONT_INSERT);
8433 }
8434 }
8435
8436 void
8437 enable_watchpoints_after_interactive_call_stop (void)
8438 {
8439 for (breakpoint *b : all_breakpoints ())
8440 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8441 {
8442 b->enable_state = bp_enabled;
8443 update_global_location_list (UGLL_MAY_INSERT);
8444 }
8445 }
8446
8447 void
8448 disable_breakpoints_before_startup (void)
8449 {
8450 current_program_space->executing_startup = 1;
8451 update_global_location_list (UGLL_DONT_INSERT);
8452 }
8453
8454 void
8455 enable_breakpoints_after_startup (void)
8456 {
8457 current_program_space->executing_startup = 0;
8458 breakpoint_re_set ();
8459 }
8460
8461 /* Create a new single-step breakpoint for thread THREAD, with no
8462 locations. */
8463
8464 static struct breakpoint *
8465 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8466 {
8467 std::unique_ptr<breakpoint> b (new breakpoint ());
8468
8469 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
8470 &momentary_breakpoint_ops);
8471
8472 b->disposition = disp_donttouch;
8473 b->frame_id = null_frame_id;
8474
8475 b->thread = thread;
8476 gdb_assert (b->thread != 0);
8477
8478 return add_to_breakpoint_chain (std::move (b));
8479 }
8480
8481 /* Set a momentary breakpoint of type TYPE at address specified by
8482 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8483 frame. */
8484
8485 breakpoint_up
8486 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8487 struct frame_id frame_id, enum bptype type)
8488 {
8489 struct breakpoint *b;
8490
8491 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8492 tail-called one. */
8493 gdb_assert (!frame_id_artificial_p (frame_id));
8494
8495 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8496 b->enable_state = bp_enabled;
8497 b->disposition = disp_donttouch;
8498 b->frame_id = frame_id;
8499
8500 b->thread = inferior_thread ()->global_num;
8501
8502 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8503
8504 return breakpoint_up (b);
8505 }
8506
8507 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8508 The new breakpoint will have type TYPE, use OPS as its
8509 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8510
8511 static struct breakpoint *
8512 momentary_breakpoint_from_master (struct breakpoint *orig,
8513 enum bptype type,
8514 const struct breakpoint_ops *ops,
8515 int loc_enabled)
8516 {
8517 struct breakpoint *copy;
8518
8519 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8520 copy->loc = allocate_bp_location (copy);
8521 set_breakpoint_location_function (copy->loc);
8522
8523 copy->loc->gdbarch = orig->loc->gdbarch;
8524 copy->loc->requested_address = orig->loc->requested_address;
8525 copy->loc->address = orig->loc->address;
8526 copy->loc->section = orig->loc->section;
8527 copy->loc->pspace = orig->loc->pspace;
8528 copy->loc->probe = orig->loc->probe;
8529 copy->loc->line_number = orig->loc->line_number;
8530 copy->loc->symtab = orig->loc->symtab;
8531 copy->loc->enabled = loc_enabled;
8532 copy->frame_id = orig->frame_id;
8533 copy->thread = orig->thread;
8534 copy->pspace = orig->pspace;
8535
8536 copy->enable_state = bp_enabled;
8537 copy->disposition = disp_donttouch;
8538 copy->number = internal_breakpoint_number--;
8539
8540 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8541 return copy;
8542 }
8543
8544 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8545 ORIG is NULL. */
8546
8547 struct breakpoint *
8548 clone_momentary_breakpoint (struct breakpoint *orig)
8549 {
8550 /* If there's nothing to clone, then return nothing. */
8551 if (orig == NULL)
8552 return NULL;
8553
8554 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8555 }
8556
8557 breakpoint_up
8558 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8559 enum bptype type)
8560 {
8561 struct symtab_and_line sal;
8562
8563 sal = find_pc_line (pc, 0);
8564 sal.pc = pc;
8565 sal.section = find_pc_overlay (pc);
8566 sal.explicit_pc = 1;
8567
8568 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8569 }
8570 \f
8571
8572 /* Tell the user we have just set a breakpoint B. */
8573
8574 static void
8575 mention (struct breakpoint *b)
8576 {
8577 b->ops->print_mention (b);
8578 current_uiout->text ("\n");
8579 }
8580 \f
8581
8582 static bool bp_loc_is_permanent (struct bp_location *loc);
8583
8584 /* Handle "set breakpoint auto-hw on".
8585
8586 If the explicitly specified breakpoint type is not hardware
8587 breakpoint, check the memory map to see whether the breakpoint
8588 address is in read-only memory.
8589
8590 - location type is not hardware breakpoint, memory is read-only.
8591 We change the type of the location to hardware breakpoint.
8592
8593 - location type is hardware breakpoint, memory is read-write. This
8594 means we've previously made the location hardware one, but then the
8595 memory map changed, so we undo.
8596 */
8597
8598 static void
8599 handle_automatic_hardware_breakpoints (bp_location *bl)
8600 {
8601 if (automatic_hardware_breakpoints
8602 && bl->owner->type != bp_hardware_breakpoint
8603 && (bl->loc_type == bp_loc_software_breakpoint
8604 || bl->loc_type == bp_loc_hardware_breakpoint))
8605 {
8606 /* When breakpoints are removed, remove_breakpoints will use
8607 location types we've just set here, the only possible problem
8608 is that memory map has changed during running program, but
8609 it's not going to work anyway with current gdb. */
8610 mem_region *mr = lookup_mem_region (bl->address);
8611
8612 if (mr != nullptr)
8613 {
8614 enum bp_loc_type new_type;
8615
8616 if (mr->attrib.mode != MEM_RW)
8617 new_type = bp_loc_hardware_breakpoint;
8618 else
8619 new_type = bp_loc_software_breakpoint;
8620
8621 if (new_type != bl->loc_type)
8622 {
8623 static bool said = false;
8624
8625 bl->loc_type = new_type;
8626 if (!said)
8627 {
8628 fprintf_filtered (gdb_stdout,
8629 _("Note: automatically using "
8630 "hardware breakpoints for "
8631 "read-only addresses.\n"));
8632 said = true;
8633 }
8634 }
8635 }
8636 }
8637 }
8638
8639 static struct bp_location *
8640 add_location_to_breakpoint (struct breakpoint *b,
8641 const struct symtab_and_line *sal)
8642 {
8643 struct bp_location *loc, **tmp;
8644 CORE_ADDR adjusted_address;
8645 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8646
8647 if (loc_gdbarch == NULL)
8648 loc_gdbarch = b->gdbarch;
8649
8650 /* Adjust the breakpoint's address prior to allocating a location.
8651 Once we call allocate_bp_location(), that mostly uninitialized
8652 location will be placed on the location chain. Adjustment of the
8653 breakpoint may cause target_read_memory() to be called and we do
8654 not want its scan of the location chain to find a breakpoint and
8655 location that's only been partially initialized. */
8656 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8657 sal->pc, b->type);
8658
8659 /* Sort the locations by their ADDRESS. */
8660 loc = allocate_bp_location (b);
8661 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8662 tmp = &((*tmp)->next))
8663 ;
8664 loc->next = *tmp;
8665 *tmp = loc;
8666
8667 loc->requested_address = sal->pc;
8668 loc->address = adjusted_address;
8669 loc->pspace = sal->pspace;
8670 loc->probe.prob = sal->prob;
8671 loc->probe.objfile = sal->objfile;
8672 gdb_assert (loc->pspace != NULL);
8673 loc->section = sal->section;
8674 loc->gdbarch = loc_gdbarch;
8675 loc->line_number = sal->line;
8676 loc->symtab = sal->symtab;
8677 loc->symbol = sal->symbol;
8678 loc->msymbol = sal->msymbol;
8679 loc->objfile = sal->objfile;
8680
8681 set_breakpoint_location_function (loc);
8682
8683 /* While by definition, permanent breakpoints are already present in the
8684 code, we don't mark the location as inserted. Normally one would expect
8685 that GDB could rely on that breakpoint instruction to stop the program,
8686 thus removing the need to insert its own breakpoint, except that executing
8687 the breakpoint instruction can kill the target instead of reporting a
8688 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8689 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8690 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8691 breakpoint be inserted normally results in QEMU knowing about the GDB
8692 breakpoint, and thus trap before the breakpoint instruction is executed.
8693 (If GDB later needs to continue execution past the permanent breakpoint,
8694 it manually increments the PC, thus avoiding executing the breakpoint
8695 instruction.) */
8696 if (bp_loc_is_permanent (loc))
8697 loc->permanent = 1;
8698
8699 return loc;
8700 }
8701 \f
8702
8703 /* Return true if LOC is pointing to a permanent breakpoint,
8704 return false otherwise. */
8705
8706 static bool
8707 bp_loc_is_permanent (struct bp_location *loc)
8708 {
8709 gdb_assert (loc != NULL);
8710
8711 /* If we have a non-breakpoint-backed catchpoint or a software
8712 watchpoint, just return 0. We should not attempt to read from
8713 the addresses the locations of these breakpoint types point to.
8714 gdbarch_program_breakpoint_here_p, below, will attempt to read
8715 memory. */
8716 if (!bl_address_is_meaningful (loc))
8717 return false;
8718
8719 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8720 switch_to_program_space_and_thread (loc->pspace);
8721 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
8722 }
8723
8724 /* Build a command list for the dprintf corresponding to the current
8725 settings of the dprintf style options. */
8726
8727 static void
8728 update_dprintf_command_list (struct breakpoint *b)
8729 {
8730 const char *dprintf_args = b->extra_string.get ();
8731 gdb::unique_xmalloc_ptr<char> printf_line = nullptr;
8732
8733 if (!dprintf_args)
8734 return;
8735
8736 dprintf_args = skip_spaces (dprintf_args);
8737
8738 /* Allow a comma, as it may have terminated a location, but don't
8739 insist on it. */
8740 if (*dprintf_args == ',')
8741 ++dprintf_args;
8742 dprintf_args = skip_spaces (dprintf_args);
8743
8744 if (*dprintf_args != '"')
8745 error (_("Bad format string, missing '\"'."));
8746
8747 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8748 printf_line = xstrprintf ("printf %s", dprintf_args);
8749 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8750 {
8751 if (dprintf_function.empty ())
8752 error (_("No function supplied for dprintf call"));
8753
8754 if (!dprintf_channel.empty ())
8755 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8756 dprintf_function.c_str (),
8757 dprintf_channel.c_str (),
8758 dprintf_args);
8759 else
8760 printf_line = xstrprintf ("call (void) %s (%s)",
8761 dprintf_function.c_str (),
8762 dprintf_args);
8763 }
8764 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8765 {
8766 if (target_can_run_breakpoint_commands ())
8767 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8768 else
8769 {
8770 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8771 printf_line = xstrprintf ("printf %s", dprintf_args);
8772 }
8773 }
8774 else
8775 internal_error (__FILE__, __LINE__,
8776 _("Invalid dprintf style."));
8777
8778 gdb_assert (printf_line != NULL);
8779
8780 /* Manufacture a printf sequence. */
8781 struct command_line *printf_cmd_line
8782 = new struct command_line (simple_control, printf_line.release ());
8783 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8784 command_lines_deleter ()));
8785 }
8786
8787 /* Update all dprintf commands, making their command lists reflect
8788 current style settings. */
8789
8790 static void
8791 update_dprintf_commands (const char *args, int from_tty,
8792 struct cmd_list_element *c)
8793 {
8794 for (breakpoint *b : all_breakpoints ())
8795 if (b->type == bp_dprintf)
8796 update_dprintf_command_list (b);
8797 }
8798
8799 /* Create a breakpoint with SAL as location. Use LOCATION
8800 as a description of the location, and COND_STRING
8801 as condition expression. If LOCATION is NULL then create an
8802 "address location" from the address in the SAL. */
8803
8804 static void
8805 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8806 gdb::array_view<const symtab_and_line> sals,
8807 event_location_up &&location,
8808 gdb::unique_xmalloc_ptr<char> filter,
8809 gdb::unique_xmalloc_ptr<char> cond_string,
8810 gdb::unique_xmalloc_ptr<char> extra_string,
8811 enum bptype type, enum bpdisp disposition,
8812 int thread, int task, int ignore_count,
8813 const struct breakpoint_ops *ops, int from_tty,
8814 int enabled, int internal, unsigned flags,
8815 int display_canonical)
8816 {
8817 int i;
8818
8819 if (type == bp_hardware_breakpoint)
8820 {
8821 int target_resources_ok;
8822
8823 i = hw_breakpoint_used_count ();
8824 target_resources_ok =
8825 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8826 i + 1, 0);
8827 if (target_resources_ok == 0)
8828 error (_("No hardware breakpoint support in the target."));
8829 else if (target_resources_ok < 0)
8830 error (_("Hardware breakpoints used exceeds limit."));
8831 }
8832
8833 gdb_assert (!sals.empty ());
8834
8835 for (const auto &sal : sals)
8836 {
8837 struct bp_location *loc;
8838
8839 if (from_tty)
8840 {
8841 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8842 if (!loc_gdbarch)
8843 loc_gdbarch = gdbarch;
8844
8845 describe_other_breakpoints (loc_gdbarch,
8846 sal.pspace, sal.pc, sal.section, thread);
8847 }
8848
8849 if (&sal == &sals[0])
8850 {
8851 init_raw_breakpoint (b, gdbarch, sal, type, ops);
8852 b->thread = thread;
8853 b->task = task;
8854
8855 b->cond_string = std::move (cond_string);
8856 b->extra_string = std::move (extra_string);
8857 b->ignore_count = ignore_count;
8858 b->enable_state = enabled ? bp_enabled : bp_disabled;
8859 b->disposition = disposition;
8860
8861 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8862 b->loc->inserted = 1;
8863
8864 if (type == bp_static_tracepoint)
8865 {
8866 struct tracepoint *t = (struct tracepoint *) b;
8867 struct static_tracepoint_marker marker;
8868
8869 if (strace_marker_p (b))
8870 {
8871 /* We already know the marker exists, otherwise, we
8872 wouldn't see a sal for it. */
8873 const char *p
8874 = &event_location_to_string (b->location.get ())[3];
8875 const char *endp;
8876
8877 p = skip_spaces (p);
8878
8879 endp = skip_to_space (p);
8880
8881 t->static_trace_marker_id.assign (p, endp - p);
8882
8883 printf_filtered (_("Probed static tracepoint "
8884 "marker \"%s\"\n"),
8885 t->static_trace_marker_id.c_str ());
8886 }
8887 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8888 {
8889 t->static_trace_marker_id = std::move (marker.str_id);
8890
8891 printf_filtered (_("Probed static tracepoint "
8892 "marker \"%s\"\n"),
8893 t->static_trace_marker_id.c_str ());
8894 }
8895 else
8896 warning (_("Couldn't determine the static "
8897 "tracepoint marker to probe"));
8898 }
8899
8900 loc = b->loc;
8901 }
8902 else
8903 {
8904 loc = add_location_to_breakpoint (b, &sal);
8905 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8906 loc->inserted = 1;
8907 }
8908
8909 /* Do not set breakpoint locations conditions yet. As locations
8910 are inserted, they get sorted based on their addresses. Let
8911 the list stabilize to have reliable location numbers. */
8912
8913 /* Dynamic printf requires and uses additional arguments on the
8914 command line, otherwise it's an error. */
8915 if (type == bp_dprintf)
8916 {
8917 if (b->extra_string)
8918 update_dprintf_command_list (b);
8919 else
8920 error (_("Format string required"));
8921 }
8922 else if (b->extra_string)
8923 error (_("Garbage '%s' at end of command"), b->extra_string.get ());
8924 }
8925
8926
8927 /* The order of the locations is now stable. Set the location
8928 condition using the location's number. */
8929 int loc_num = 1;
8930 for (bp_location *loc : b->locations ())
8931 {
8932 if (b->cond_string != nullptr)
8933 set_breakpoint_location_condition (b->cond_string.get (), loc,
8934 b->number, loc_num);
8935
8936 ++loc_num;
8937 }
8938
8939 b->display_canonical = display_canonical;
8940 if (location != NULL)
8941 b->location = std::move (location);
8942 else
8943 b->location = new_address_location (b->loc->address, NULL, 0);
8944 b->filter = std::move (filter);
8945 }
8946
8947 static void
8948 create_breakpoint_sal (struct gdbarch *gdbarch,
8949 gdb::array_view<const symtab_and_line> sals,
8950 event_location_up &&location,
8951 gdb::unique_xmalloc_ptr<char> filter,
8952 gdb::unique_xmalloc_ptr<char> cond_string,
8953 gdb::unique_xmalloc_ptr<char> extra_string,
8954 enum bptype type, enum bpdisp disposition,
8955 int thread, int task, int ignore_count,
8956 const struct breakpoint_ops *ops, int from_tty,
8957 int enabled, int internal, unsigned flags,
8958 int display_canonical)
8959 {
8960 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
8961
8962 init_breakpoint_sal (b.get (), gdbarch,
8963 sals, std::move (location),
8964 std::move (filter),
8965 std::move (cond_string),
8966 std::move (extra_string),
8967 type, disposition,
8968 thread, task, ignore_count,
8969 ops, from_tty,
8970 enabled, internal, flags,
8971 display_canonical);
8972
8973 install_breakpoint (internal, std::move (b), 0);
8974 }
8975
8976 /* Add SALS.nelts breakpoints to the breakpoint table. For each
8977 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8978 value. COND_STRING, if not NULL, specified the condition to be
8979 used for all breakpoints. Essentially the only case where
8980 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8981 function. In that case, it's still not possible to specify
8982 separate conditions for different overloaded functions, so
8983 we take just a single condition string.
8984
8985 NOTE: If the function succeeds, the caller is expected to cleanup
8986 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
8987 array contents). If the function fails (error() is called), the
8988 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
8989 COND and SALS arrays and each of those arrays contents. */
8990
8991 static void
8992 create_breakpoints_sal (struct gdbarch *gdbarch,
8993 struct linespec_result *canonical,
8994 gdb::unique_xmalloc_ptr<char> cond_string,
8995 gdb::unique_xmalloc_ptr<char> extra_string,
8996 enum bptype type, enum bpdisp disposition,
8997 int thread, int task, int ignore_count,
8998 const struct breakpoint_ops *ops, int from_tty,
8999 int enabled, int internal, unsigned flags)
9000 {
9001 if (canonical->pre_expanded)
9002 gdb_assert (canonical->lsals.size () == 1);
9003
9004 for (const auto &lsal : canonical->lsals)
9005 {
9006 /* Note that 'location' can be NULL in the case of a plain
9007 'break', without arguments. */
9008 event_location_up location
9009 = (canonical->location != NULL
9010 ? copy_event_location (canonical->location.get ()) : NULL);
9011 gdb::unique_xmalloc_ptr<char> filter_string
9012 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
9013
9014 create_breakpoint_sal (gdbarch, lsal.sals,
9015 std::move (location),
9016 std::move (filter_string),
9017 std::move (cond_string),
9018 std::move (extra_string),
9019 type, disposition,
9020 thread, task, ignore_count, ops,
9021 from_tty, enabled, internal, flags,
9022 canonical->special_display);
9023 }
9024 }
9025
9026 /* Parse LOCATION which is assumed to be a SAL specification possibly
9027 followed by conditionals. On return, SALS contains an array of SAL
9028 addresses found. LOCATION points to the end of the SAL (for
9029 linespec locations).
9030
9031 The array and the line spec strings are allocated on the heap, it is
9032 the caller's responsibility to free them. */
9033
9034 static void
9035 parse_breakpoint_sals (struct event_location *location,
9036 struct linespec_result *canonical)
9037 {
9038 struct symtab_and_line cursal;
9039
9040 if (event_location_type (location) == LINESPEC_LOCATION)
9041 {
9042 const char *spec = get_linespec_location (location)->spec_string;
9043
9044 if (spec == NULL)
9045 {
9046 /* The last displayed codepoint, if it's valid, is our default
9047 breakpoint address. */
9048 if (last_displayed_sal_is_valid ())
9049 {
9050 /* Set sal's pspace, pc, symtab, and line to the values
9051 corresponding to the last call to print_frame_info.
9052 Be sure to reinitialize LINE with NOTCURRENT == 0
9053 as the breakpoint line number is inappropriate otherwise.
9054 find_pc_line would adjust PC, re-set it back. */
9055 symtab_and_line sal = get_last_displayed_sal ();
9056 CORE_ADDR pc = sal.pc;
9057
9058 sal = find_pc_line (pc, 0);
9059
9060 /* "break" without arguments is equivalent to "break *PC"
9061 where PC is the last displayed codepoint's address. So
9062 make sure to set sal.explicit_pc to prevent GDB from
9063 trying to expand the list of sals to include all other
9064 instances with the same symtab and line. */
9065 sal.pc = pc;
9066 sal.explicit_pc = 1;
9067
9068 struct linespec_sals lsal;
9069 lsal.sals = {sal};
9070 lsal.canonical = NULL;
9071
9072 canonical->lsals.push_back (std::move (lsal));
9073 return;
9074 }
9075 else
9076 error (_("No default breakpoint address now."));
9077 }
9078 }
9079
9080 /* Force almost all breakpoints to be in terms of the
9081 current_source_symtab (which is decode_line_1's default).
9082 This should produce the results we want almost all of the
9083 time while leaving default_breakpoint_* alone.
9084
9085 ObjC: However, don't match an Objective-C method name which
9086 may have a '+' or '-' succeeded by a '['. */
9087 cursal = get_current_source_symtab_and_line ();
9088 if (last_displayed_sal_is_valid ())
9089 {
9090 const char *spec = NULL;
9091
9092 if (event_location_type (location) == LINESPEC_LOCATION)
9093 spec = get_linespec_location (location)->spec_string;
9094
9095 if (!cursal.symtab
9096 || (spec != NULL
9097 && strchr ("+-", spec[0]) != NULL
9098 && spec[1] != '['))
9099 {
9100 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9101 get_last_displayed_symtab (),
9102 get_last_displayed_line (),
9103 canonical, NULL, NULL);
9104 return;
9105 }
9106 }
9107
9108 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9109 cursal.symtab, cursal.line, canonical, NULL, NULL);
9110 }
9111
9112
9113 /* Convert each SAL into a real PC. Verify that the PC can be
9114 inserted as a breakpoint. If it can't throw an error. */
9115
9116 static void
9117 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
9118 {
9119 for (auto &sal : sals)
9120 resolve_sal_pc (&sal);
9121 }
9122
9123 /* Fast tracepoints may have restrictions on valid locations. For
9124 instance, a fast tracepoint using a jump instead of a trap will
9125 likely have to overwrite more bytes than a trap would, and so can
9126 only be placed where the instruction is longer than the jump, or a
9127 multi-instruction sequence does not have a jump into the middle of
9128 it, etc. */
9129
9130 static void
9131 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9132 gdb::array_view<const symtab_and_line> sals)
9133 {
9134 for (const auto &sal : sals)
9135 {
9136 struct gdbarch *sarch;
9137
9138 sarch = get_sal_arch (sal);
9139 /* We fall back to GDBARCH if there is no architecture
9140 associated with SAL. */
9141 if (sarch == NULL)
9142 sarch = gdbarch;
9143 std::string msg;
9144 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
9145 error (_("May not have a fast tracepoint at %s%s"),
9146 paddress (sarch, sal.pc), msg.c_str ());
9147 }
9148 }
9149
9150 /* Given TOK, a string specification of condition and thread, as
9151 accepted by the 'break' command, extract the condition
9152 string and thread number and set *COND_STRING and *THREAD.
9153 PC identifies the context at which the condition should be parsed.
9154 If no condition is found, *COND_STRING is set to NULL.
9155 If no thread is found, *THREAD is set to -1. */
9156
9157 static void
9158 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9159 gdb::unique_xmalloc_ptr<char> *cond_string,
9160 int *thread, int *task,
9161 gdb::unique_xmalloc_ptr<char> *rest)
9162 {
9163 cond_string->reset ();
9164 *thread = -1;
9165 *task = 0;
9166 rest->reset ();
9167 bool force = false;
9168
9169 while (tok && *tok)
9170 {
9171 const char *end_tok;
9172 int toklen;
9173 const char *cond_start = NULL;
9174 const char *cond_end = NULL;
9175
9176 tok = skip_spaces (tok);
9177
9178 if ((*tok == '"' || *tok == ',') && rest)
9179 {
9180 rest->reset (savestring (tok, strlen (tok)));
9181 return;
9182 }
9183
9184 end_tok = skip_to_space (tok);
9185
9186 toklen = end_tok - tok;
9187
9188 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9189 {
9190 tok = cond_start = end_tok + 1;
9191 try
9192 {
9193 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9194 }
9195 catch (const gdb_exception_error &)
9196 {
9197 if (!force)
9198 throw;
9199 else
9200 tok = tok + strlen (tok);
9201 }
9202 cond_end = tok;
9203 cond_string->reset (savestring (cond_start, cond_end - cond_start));
9204 }
9205 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
9206 {
9207 tok = tok + toklen;
9208 force = true;
9209 }
9210 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9211 {
9212 const char *tmptok;
9213 struct thread_info *thr;
9214
9215 tok = end_tok + 1;
9216 thr = parse_thread_id (tok, &tmptok);
9217 if (tok == tmptok)
9218 error (_("Junk after thread keyword."));
9219 *thread = thr->global_num;
9220 tok = tmptok;
9221 }
9222 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9223 {
9224 char *tmptok;
9225
9226 tok = end_tok + 1;
9227 *task = strtol (tok, &tmptok, 0);
9228 if (tok == tmptok)
9229 error (_("Junk after task keyword."));
9230 if (!valid_task_id (*task))
9231 error (_("Unknown task %d."), *task);
9232 tok = tmptok;
9233 }
9234 else if (rest)
9235 {
9236 rest->reset (savestring (tok, strlen (tok)));
9237 return;
9238 }
9239 else
9240 error (_("Junk at end of arguments."));
9241 }
9242 }
9243
9244 /* Call 'find_condition_and_thread' for each sal in SALS until a parse
9245 succeeds. The parsed values are written to COND_STRING, THREAD,
9246 TASK, and REST. See the comment of 'find_condition_and_thread'
9247 for the description of these parameters and INPUT. */
9248
9249 static void
9250 find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
9251 const char *input,
9252 gdb::unique_xmalloc_ptr<char> *cond_string,
9253 int *thread, int *task,
9254 gdb::unique_xmalloc_ptr<char> *rest)
9255 {
9256 int num_failures = 0;
9257 for (auto &sal : sals)
9258 {
9259 gdb::unique_xmalloc_ptr<char> cond;
9260 int thread_id = 0;
9261 int task_id = 0;
9262 gdb::unique_xmalloc_ptr<char> remaining;
9263
9264 /* Here we want to parse 'arg' to separate condition from thread
9265 number. But because parsing happens in a context and the
9266 contexts of sals might be different, try each until there is
9267 success. Finding one successful parse is sufficient for our
9268 goal. When setting the breakpoint we'll re-parse the
9269 condition in the context of each sal. */
9270 try
9271 {
9272 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
9273 &task_id, &remaining);
9274 *cond_string = std::move (cond);
9275 *thread = thread_id;
9276 *task = task_id;
9277 *rest = std::move (remaining);
9278 break;
9279 }
9280 catch (const gdb_exception_error &e)
9281 {
9282 num_failures++;
9283 /* If no sal remains, do not continue. */
9284 if (num_failures == sals.size ())
9285 throw;
9286 }
9287 }
9288 }
9289
9290 /* Decode a static tracepoint marker spec. */
9291
9292 static std::vector<symtab_and_line>
9293 decode_static_tracepoint_spec (const char **arg_p)
9294 {
9295 const char *p = &(*arg_p)[3];
9296 const char *endp;
9297
9298 p = skip_spaces (p);
9299
9300 endp = skip_to_space (p);
9301
9302 std::string marker_str (p, endp - p);
9303
9304 std::vector<static_tracepoint_marker> markers
9305 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9306 if (markers.empty ())
9307 error (_("No known static tracepoint marker named %s"),
9308 marker_str.c_str ());
9309
9310 std::vector<symtab_and_line> sals;
9311 sals.reserve (markers.size ());
9312
9313 for (const static_tracepoint_marker &marker : markers)
9314 {
9315 symtab_and_line sal = find_pc_line (marker.address, 0);
9316 sal.pc = marker.address;
9317 sals.push_back (sal);
9318 }
9319
9320 *arg_p = endp;
9321 return sals;
9322 }
9323
9324 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9325 according to IS_TRACEPOINT. */
9326
9327 static const struct breakpoint_ops *
9328 breakpoint_ops_for_event_location_type (enum event_location_type location_type,
9329 bool is_tracepoint)
9330 {
9331 if (is_tracepoint)
9332 {
9333 if (location_type == PROBE_LOCATION)
9334 return &tracepoint_probe_breakpoint_ops;
9335 else
9336 return &tracepoint_breakpoint_ops;
9337 }
9338 else
9339 {
9340 if (location_type == PROBE_LOCATION)
9341 return &bkpt_probe_breakpoint_ops;
9342 else
9343 return &bkpt_breakpoint_ops;
9344 }
9345 }
9346
9347 /* See breakpoint.h. */
9348
9349 const struct breakpoint_ops *
9350 breakpoint_ops_for_event_location (const struct event_location *location,
9351 bool is_tracepoint)
9352 {
9353 if (location != nullptr)
9354 return breakpoint_ops_for_event_location_type
9355 (event_location_type (location), is_tracepoint);
9356 return is_tracepoint ? &tracepoint_breakpoint_ops : &bkpt_breakpoint_ops;
9357 }
9358
9359 /* See breakpoint.h. */
9360
9361 int
9362 create_breakpoint (struct gdbarch *gdbarch,
9363 struct event_location *location,
9364 const char *cond_string,
9365 int thread, const char *extra_string,
9366 bool force_condition, int parse_extra,
9367 int tempflag, enum bptype type_wanted,
9368 int ignore_count,
9369 enum auto_boolean pending_break_support,
9370 const struct breakpoint_ops *ops,
9371 int from_tty, int enabled, int internal,
9372 unsigned flags)
9373 {
9374 struct linespec_result canonical;
9375 int pending = 0;
9376 int task = 0;
9377 int prev_bkpt_count = breakpoint_count;
9378
9379 gdb_assert (ops != NULL);
9380
9381 /* If extra_string isn't useful, set it to NULL. */
9382 if (extra_string != NULL && *extra_string == '\0')
9383 extra_string = NULL;
9384
9385 try
9386 {
9387 ops->create_sals_from_location (location, &canonical, type_wanted);
9388 }
9389 catch (const gdb_exception_error &e)
9390 {
9391 /* If caller is interested in rc value from parse, set
9392 value. */
9393 if (e.error == NOT_FOUND_ERROR)
9394 {
9395 /* If pending breakpoint support is turned off, throw
9396 error. */
9397
9398 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9399 throw;
9400
9401 exception_print (gdb_stderr, e);
9402
9403 /* If pending breakpoint support is auto query and the user
9404 selects no, then simply return the error code. */
9405 if (pending_break_support == AUTO_BOOLEAN_AUTO
9406 && !nquery (_("Make %s pending on future shared library load? "),
9407 bptype_string (type_wanted)))
9408 return 0;
9409
9410 /* At this point, either the user was queried about setting
9411 a pending breakpoint and selected yes, or pending
9412 breakpoint behavior is on and thus a pending breakpoint
9413 is defaulted on behalf of the user. */
9414 pending = 1;
9415 }
9416 else
9417 throw;
9418 }
9419
9420 if (!pending && canonical.lsals.empty ())
9421 return 0;
9422
9423 /* Resolve all line numbers to PC's and verify that the addresses
9424 are ok for the target. */
9425 if (!pending)
9426 {
9427 for (auto &lsal : canonical.lsals)
9428 breakpoint_sals_to_pc (lsal.sals);
9429 }
9430
9431 /* Fast tracepoints may have additional restrictions on location. */
9432 if (!pending && type_wanted == bp_fast_tracepoint)
9433 {
9434 for (const auto &lsal : canonical.lsals)
9435 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9436 }
9437
9438 /* Verify that condition can be parsed, before setting any
9439 breakpoints. Allocate a separate condition expression for each
9440 breakpoint. */
9441 if (!pending)
9442 {
9443 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9444 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9445
9446 if (parse_extra)
9447 {
9448 gdb::unique_xmalloc_ptr<char> rest;
9449 gdb::unique_xmalloc_ptr<char> cond;
9450
9451 const linespec_sals &lsal = canonical.lsals[0];
9452
9453 find_condition_and_thread_for_sals (lsal.sals, extra_string,
9454 &cond, &thread, &task, &rest);
9455 cond_string_copy = std::move (cond);
9456 extra_string_copy = std::move (rest);
9457 }
9458 else
9459 {
9460 if (type_wanted != bp_dprintf
9461 && extra_string != NULL && *extra_string != '\0')
9462 error (_("Garbage '%s' at end of location"), extra_string);
9463
9464 /* Check the validity of the condition. We should error out
9465 if the condition is invalid at all of the locations and
9466 if it is not forced. In the PARSE_EXTRA case above, this
9467 check is done when parsing the EXTRA_STRING. */
9468 if (cond_string != nullptr && !force_condition)
9469 {
9470 int num_failures = 0;
9471 const linespec_sals &lsal = canonical.lsals[0];
9472 for (const auto &sal : lsal.sals)
9473 {
9474 const char *cond = cond_string;
9475 try
9476 {
9477 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
9478 /* One success is sufficient to keep going. */
9479 break;
9480 }
9481 catch (const gdb_exception_error &)
9482 {
9483 num_failures++;
9484 /* If this is the last sal, error out. */
9485 if (num_failures == lsal.sals.size ())
9486 throw;
9487 }
9488 }
9489 }
9490
9491 /* Create a private copy of condition string. */
9492 if (cond_string)
9493 cond_string_copy.reset (xstrdup (cond_string));
9494 /* Create a private copy of any extra string. */
9495 if (extra_string)
9496 extra_string_copy.reset (xstrdup (extra_string));
9497 }
9498
9499 ops->create_breakpoints_sal (gdbarch, &canonical,
9500 std::move (cond_string_copy),
9501 std::move (extra_string_copy),
9502 type_wanted,
9503 tempflag ? disp_del : disp_donttouch,
9504 thread, task, ignore_count, ops,
9505 from_tty, enabled, internal, flags);
9506 }
9507 else
9508 {
9509 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9510
9511 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9512 b->location = copy_event_location (location);
9513
9514 if (parse_extra)
9515 b->cond_string = NULL;
9516 else
9517 {
9518 /* Create a private copy of condition string. */
9519 b->cond_string.reset (cond_string != NULL
9520 ? xstrdup (cond_string)
9521 : NULL);
9522 b->thread = thread;
9523 }
9524
9525 /* Create a private copy of any extra string. */
9526 b->extra_string.reset (extra_string != NULL
9527 ? xstrdup (extra_string)
9528 : NULL);
9529 b->ignore_count = ignore_count;
9530 b->disposition = tempflag ? disp_del : disp_donttouch;
9531 b->condition_not_parsed = 1;
9532 b->enable_state = enabled ? bp_enabled : bp_disabled;
9533 if ((type_wanted != bp_breakpoint
9534 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9535 b->pspace = current_program_space;
9536
9537 install_breakpoint (internal, std::move (b), 0);
9538 }
9539
9540 if (canonical.lsals.size () > 1)
9541 {
9542 warning (_("Multiple breakpoints were set.\nUse the "
9543 "\"delete\" command to delete unwanted breakpoints."));
9544 prev_breakpoint_count = prev_bkpt_count;
9545 }
9546
9547 update_global_location_list (UGLL_MAY_INSERT);
9548
9549 return 1;
9550 }
9551
9552 /* Set a breakpoint.
9553 ARG is a string describing breakpoint address,
9554 condition, and thread.
9555 FLAG specifies if a breakpoint is hardware on,
9556 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9557 and BP_TEMPFLAG. */
9558
9559 static void
9560 break_command_1 (const char *arg, int flag, int from_tty)
9561 {
9562 int tempflag = flag & BP_TEMPFLAG;
9563 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9564 ? bp_hardware_breakpoint
9565 : bp_breakpoint);
9566
9567 event_location_up location = string_to_event_location (&arg, current_language);
9568 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9569 (location.get (), false /* is_tracepoint */);
9570
9571 create_breakpoint (get_current_arch (),
9572 location.get (),
9573 NULL, 0, arg, false, 1 /* parse arg */,
9574 tempflag, type_wanted,
9575 0 /* Ignore count */,
9576 pending_break_support,
9577 ops,
9578 from_tty,
9579 1 /* enabled */,
9580 0 /* internal */,
9581 0);
9582 }
9583
9584 /* Helper function for break_command_1 and disassemble_command. */
9585
9586 void
9587 resolve_sal_pc (struct symtab_and_line *sal)
9588 {
9589 CORE_ADDR pc;
9590
9591 if (sal->pc == 0 && sal->symtab != NULL)
9592 {
9593 if (!find_line_pc (sal->symtab, sal->line, &pc))
9594 error (_("No line %d in file \"%s\"."),
9595 sal->line, symtab_to_filename_for_display (sal->symtab));
9596 sal->pc = pc;
9597
9598 /* If this SAL corresponds to a breakpoint inserted using a line
9599 number, then skip the function prologue if necessary. */
9600 if (sal->explicit_line)
9601 skip_prologue_sal (sal);
9602 }
9603
9604 if (sal->section == 0 && sal->symtab != NULL)
9605 {
9606 const struct blockvector *bv;
9607 const struct block *b;
9608 struct symbol *sym;
9609
9610 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9611 SYMTAB_COMPUNIT (sal->symtab));
9612 if (bv != NULL)
9613 {
9614 sym = block_linkage_function (b);
9615 if (sym != NULL)
9616 {
9617 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9618 sal->section = sym->obj_section (SYMTAB_OBJFILE (sal->symtab));
9619 }
9620 else
9621 {
9622 /* It really is worthwhile to have the section, so we'll
9623 just have to look harder. This case can be executed
9624 if we have line numbers but no functions (as can
9625 happen in assembly source). */
9626
9627 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9628 switch_to_program_space_and_thread (sal->pspace);
9629
9630 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9631 if (msym.minsym)
9632 sal->section = msym.obj_section ();
9633 }
9634 }
9635 }
9636 }
9637
9638 void
9639 break_command (const char *arg, int from_tty)
9640 {
9641 break_command_1 (arg, 0, from_tty);
9642 }
9643
9644 void
9645 tbreak_command (const char *arg, int from_tty)
9646 {
9647 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9648 }
9649
9650 static void
9651 hbreak_command (const char *arg, int from_tty)
9652 {
9653 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9654 }
9655
9656 static void
9657 thbreak_command (const char *arg, int from_tty)
9658 {
9659 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9660 }
9661
9662 static void
9663 stop_command (const char *arg, int from_tty)
9664 {
9665 printf_filtered (_("Specify the type of breakpoint to set.\n\
9666 Usage: stop in <function | address>\n\
9667 stop at <line>\n"));
9668 }
9669
9670 static void
9671 stopin_command (const char *arg, int from_tty)
9672 {
9673 int badInput = 0;
9674
9675 if (arg == NULL)
9676 badInput = 1;
9677 else if (*arg != '*')
9678 {
9679 const char *argptr = arg;
9680 int hasColon = 0;
9681
9682 /* Look for a ':'. If this is a line number specification, then
9683 say it is bad, otherwise, it should be an address or
9684 function/method name. */
9685 while (*argptr && !hasColon)
9686 {
9687 hasColon = (*argptr == ':');
9688 argptr++;
9689 }
9690
9691 if (hasColon)
9692 badInput = (*argptr != ':'); /* Not a class::method */
9693 else
9694 badInput = isdigit (*arg); /* a simple line number */
9695 }
9696
9697 if (badInput)
9698 printf_filtered (_("Usage: stop in <function | address>\n"));
9699 else
9700 break_command_1 (arg, 0, from_tty);
9701 }
9702
9703 static void
9704 stopat_command (const char *arg, int from_tty)
9705 {
9706 int badInput = 0;
9707
9708 if (arg == NULL || *arg == '*') /* no line number */
9709 badInput = 1;
9710 else
9711 {
9712 const char *argptr = arg;
9713 int hasColon = 0;
9714
9715 /* Look for a ':'. If there is a '::' then get out, otherwise
9716 it is probably a line number. */
9717 while (*argptr && !hasColon)
9718 {
9719 hasColon = (*argptr == ':');
9720 argptr++;
9721 }
9722
9723 if (hasColon)
9724 badInput = (*argptr == ':'); /* we have class::method */
9725 else
9726 badInput = !isdigit (*arg); /* not a line number */
9727 }
9728
9729 if (badInput)
9730 printf_filtered (_("Usage: stop at LINE\n"));
9731 else
9732 break_command_1 (arg, 0, from_tty);
9733 }
9734
9735 /* The dynamic printf command is mostly like a regular breakpoint, but
9736 with a prewired command list consisting of a single output command,
9737 built from extra arguments supplied on the dprintf command
9738 line. */
9739
9740 static void
9741 dprintf_command (const char *arg, int from_tty)
9742 {
9743 event_location_up location = string_to_event_location (&arg, current_language);
9744
9745 /* If non-NULL, ARG should have been advanced past the location;
9746 the next character must be ','. */
9747 if (arg != NULL)
9748 {
9749 if (arg[0] != ',' || arg[1] == '\0')
9750 error (_("Format string required"));
9751 else
9752 {
9753 /* Skip the comma. */
9754 ++arg;
9755 }
9756 }
9757
9758 create_breakpoint (get_current_arch (),
9759 location.get (),
9760 NULL, 0, arg, false, 1 /* parse arg */,
9761 0, bp_dprintf,
9762 0 /* Ignore count */,
9763 pending_break_support,
9764 &dprintf_breakpoint_ops,
9765 from_tty,
9766 1 /* enabled */,
9767 0 /* internal */,
9768 0);
9769 }
9770
9771 static void
9772 agent_printf_command (const char *arg, int from_tty)
9773 {
9774 error (_("May only run agent-printf on the target"));
9775 }
9776
9777 /* Implement the "breakpoint_hit" breakpoint_ops method for
9778 ranged breakpoints. */
9779
9780 static int
9781 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9782 const address_space *aspace,
9783 CORE_ADDR bp_addr,
9784 const target_waitstatus &ws)
9785 {
9786 if (ws.kind () != TARGET_WAITKIND_STOPPED
9787 || ws.sig () != GDB_SIGNAL_TRAP)
9788 return 0;
9789
9790 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9791 bl->length, aspace, bp_addr);
9792 }
9793
9794 /* Implement the "resources_needed" breakpoint_ops method for
9795 ranged breakpoints. */
9796
9797 static int
9798 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9799 {
9800 return target_ranged_break_num_registers ();
9801 }
9802
9803 /* Implement the "print_it" breakpoint_ops method for
9804 ranged breakpoints. */
9805
9806 static enum print_stop_action
9807 print_it_ranged_breakpoint (bpstat *bs)
9808 {
9809 struct breakpoint *b = bs->breakpoint_at;
9810 struct bp_location *bl = b->loc;
9811 struct ui_out *uiout = current_uiout;
9812
9813 gdb_assert (b->type == bp_hardware_breakpoint);
9814
9815 /* Ranged breakpoints have only one location. */
9816 gdb_assert (bl && bl->next == NULL);
9817
9818 annotate_breakpoint (b->number);
9819
9820 maybe_print_thread_hit_breakpoint (uiout);
9821
9822 if (b->disposition == disp_del)
9823 uiout->text ("Temporary ranged breakpoint ");
9824 else
9825 uiout->text ("Ranged breakpoint ");
9826 if (uiout->is_mi_like_p ())
9827 {
9828 uiout->field_string ("reason",
9829 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9830 uiout->field_string ("disp", bpdisp_text (b->disposition));
9831 }
9832 uiout->field_signed ("bkptno", b->number);
9833 uiout->text (", ");
9834
9835 return PRINT_SRC_AND_LOC;
9836 }
9837
9838 /* Implement the "print_one" breakpoint_ops method for
9839 ranged breakpoints. */
9840
9841 static void
9842 print_one_ranged_breakpoint (struct breakpoint *b,
9843 struct bp_location **last_loc)
9844 {
9845 struct bp_location *bl = b->loc;
9846 struct value_print_options opts;
9847 struct ui_out *uiout = current_uiout;
9848
9849 /* Ranged breakpoints have only one location. */
9850 gdb_assert (bl && bl->next == NULL);
9851
9852 get_user_print_options (&opts);
9853
9854 if (opts.addressprint)
9855 /* We don't print the address range here, it will be printed later
9856 by print_one_detail_ranged_breakpoint. */
9857 uiout->field_skip ("addr");
9858 annotate_field (5);
9859 print_breakpoint_location (b, bl);
9860 *last_loc = bl;
9861 }
9862
9863 /* Implement the "print_one_detail" breakpoint_ops method for
9864 ranged breakpoints. */
9865
9866 static void
9867 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9868 struct ui_out *uiout)
9869 {
9870 CORE_ADDR address_start, address_end;
9871 struct bp_location *bl = b->loc;
9872 string_file stb;
9873
9874 gdb_assert (bl);
9875
9876 address_start = bl->address;
9877 address_end = address_start + bl->length - 1;
9878
9879 uiout->text ("\taddress range: ");
9880 stb.printf ("[%s, %s]",
9881 print_core_address (bl->gdbarch, address_start),
9882 print_core_address (bl->gdbarch, address_end));
9883 uiout->field_stream ("addr", stb);
9884 uiout->text ("\n");
9885 }
9886
9887 /* Implement the "print_mention" breakpoint_ops method for
9888 ranged breakpoints. */
9889
9890 static void
9891 print_mention_ranged_breakpoint (struct breakpoint *b)
9892 {
9893 struct bp_location *bl = b->loc;
9894 struct ui_out *uiout = current_uiout;
9895
9896 gdb_assert (bl);
9897 gdb_assert (b->type == bp_hardware_breakpoint);
9898
9899 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9900 b->number, paddress (bl->gdbarch, bl->address),
9901 paddress (bl->gdbarch, bl->address + bl->length - 1));
9902 }
9903
9904 /* Implement the "print_recreate" breakpoint_ops method for
9905 ranged breakpoints. */
9906
9907 static void
9908 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9909 {
9910 fprintf_unfiltered (fp, "break-range %s, %s",
9911 event_location_to_string (b->location.get ()),
9912 event_location_to_string (b->location_range_end.get ()));
9913 print_recreate_thread (b, fp);
9914 }
9915
9916 /* The breakpoint_ops structure to be used in ranged breakpoints. */
9917
9918 static struct breakpoint_ops ranged_breakpoint_ops;
9919
9920 /* Find the address where the end of the breakpoint range should be
9921 placed, given the SAL of the end of the range. This is so that if
9922 the user provides a line number, the end of the range is set to the
9923 last instruction of the given line. */
9924
9925 static CORE_ADDR
9926 find_breakpoint_range_end (struct symtab_and_line sal)
9927 {
9928 CORE_ADDR end;
9929
9930 /* If the user provided a PC value, use it. Otherwise,
9931 find the address of the end of the given location. */
9932 if (sal.explicit_pc)
9933 end = sal.pc;
9934 else
9935 {
9936 int ret;
9937 CORE_ADDR start;
9938
9939 ret = find_line_pc_range (sal, &start, &end);
9940 if (!ret)
9941 error (_("Could not find location of the end of the range."));
9942
9943 /* find_line_pc_range returns the start of the next line. */
9944 end--;
9945 }
9946
9947 return end;
9948 }
9949
9950 /* Implement the "break-range" CLI command. */
9951
9952 static void
9953 break_range_command (const char *arg, int from_tty)
9954 {
9955 const char *arg_start;
9956 struct linespec_result canonical_start, canonical_end;
9957 int bp_count, can_use_bp, length;
9958 CORE_ADDR end;
9959 struct breakpoint *b;
9960
9961 /* We don't support software ranged breakpoints. */
9962 if (target_ranged_break_num_registers () < 0)
9963 error (_("This target does not support hardware ranged breakpoints."));
9964
9965 bp_count = hw_breakpoint_used_count ();
9966 bp_count += target_ranged_break_num_registers ();
9967 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9968 bp_count, 0);
9969 if (can_use_bp < 0)
9970 error (_("Hardware breakpoints used exceeds limit."));
9971
9972 arg = skip_spaces (arg);
9973 if (arg == NULL || arg[0] == '\0')
9974 error(_("No address range specified."));
9975
9976 arg_start = arg;
9977 event_location_up start_location = string_to_event_location (&arg,
9978 current_language);
9979 parse_breakpoint_sals (start_location.get (), &canonical_start);
9980
9981 if (arg[0] != ',')
9982 error (_("Too few arguments."));
9983 else if (canonical_start.lsals.empty ())
9984 error (_("Could not find location of the beginning of the range."));
9985
9986 const linespec_sals &lsal_start = canonical_start.lsals[0];
9987
9988 if (canonical_start.lsals.size () > 1
9989 || lsal_start.sals.size () != 1)
9990 error (_("Cannot create a ranged breakpoint with multiple locations."));
9991
9992 const symtab_and_line &sal_start = lsal_start.sals[0];
9993 std::string addr_string_start (arg_start, arg - arg_start);
9994
9995 arg++; /* Skip the comma. */
9996 arg = skip_spaces (arg);
9997
9998 /* Parse the end location. */
9999
10000 arg_start = arg;
10001
10002 /* We call decode_line_full directly here instead of using
10003 parse_breakpoint_sals because we need to specify the start location's
10004 symtab and line as the default symtab and line for the end of the
10005 range. This makes it possible to have ranges like "foo.c:27, +14",
10006 where +14 means 14 lines from the start location. */
10007 event_location_up end_location = string_to_event_location (&arg,
10008 current_language);
10009 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10010 sal_start.symtab, sal_start.line,
10011 &canonical_end, NULL, NULL);
10012
10013 if (canonical_end.lsals.empty ())
10014 error (_("Could not find location of the end of the range."));
10015
10016 const linespec_sals &lsal_end = canonical_end.lsals[0];
10017 if (canonical_end.lsals.size () > 1
10018 || lsal_end.sals.size () != 1)
10019 error (_("Cannot create a ranged breakpoint with multiple locations."));
10020
10021 const symtab_and_line &sal_end = lsal_end.sals[0];
10022
10023 end = find_breakpoint_range_end (sal_end);
10024 if (sal_start.pc > end)
10025 error (_("Invalid address range, end precedes start."));
10026
10027 length = end - sal_start.pc + 1;
10028 if (length < 0)
10029 /* Length overflowed. */
10030 error (_("Address range too large."));
10031 else if (length == 1)
10032 {
10033 /* This range is simple enough to be handled by
10034 the `hbreak' command. */
10035 hbreak_command (&addr_string_start[0], 1);
10036
10037 return;
10038 }
10039
10040 /* Now set up the breakpoint. */
10041 b = set_raw_breakpoint (get_current_arch (), sal_start,
10042 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10043 set_breakpoint_count (breakpoint_count + 1);
10044 b->number = breakpoint_count;
10045 b->disposition = disp_donttouch;
10046 b->location = std::move (start_location);
10047 b->location_range_end = std::move (end_location);
10048 b->loc->length = length;
10049
10050 mention (b);
10051 gdb::observers::breakpoint_created.notify (b);
10052 update_global_location_list (UGLL_MAY_INSERT);
10053 }
10054
10055 /* Return non-zero if EXP is verified as constant. Returned zero
10056 means EXP is variable. Also the constant detection may fail for
10057 some constant expressions and in such case still falsely return
10058 zero. */
10059
10060 static bool
10061 watchpoint_exp_is_const (const struct expression *exp)
10062 {
10063 return exp->op->constant_p ();
10064 }
10065
10066 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10067
10068 static void
10069 re_set_watchpoint (struct breakpoint *b)
10070 {
10071 struct watchpoint *w = (struct watchpoint *) b;
10072
10073 /* Watchpoint can be either on expression using entirely global
10074 variables, or it can be on local variables.
10075
10076 Watchpoints of the first kind are never auto-deleted, and even
10077 persist across program restarts. Since they can use variables
10078 from shared libraries, we need to reparse expression as libraries
10079 are loaded and unloaded.
10080
10081 Watchpoints on local variables can also change meaning as result
10082 of solib event. For example, if a watchpoint uses both a local
10083 and a global variables in expression, it's a local watchpoint,
10084 but unloading of a shared library will make the expression
10085 invalid. This is not a very common use case, but we still
10086 re-evaluate expression, to avoid surprises to the user.
10087
10088 Note that for local watchpoints, we re-evaluate it only if
10089 watchpoints frame id is still valid. If it's not, it means the
10090 watchpoint is out of scope and will be deleted soon. In fact,
10091 I'm not sure we'll ever be called in this case.
10092
10093 If a local watchpoint's frame id is still valid, then
10094 w->exp_valid_block is likewise valid, and we can safely use it.
10095
10096 Don't do anything about disabled watchpoints, since they will be
10097 reevaluated again when enabled. */
10098 update_watchpoint (w, 1 /* reparse */);
10099 }
10100
10101 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10102
10103 static int
10104 insert_watchpoint (struct bp_location *bl)
10105 {
10106 struct watchpoint *w = (struct watchpoint *) bl->owner;
10107 int length = w->exact ? 1 : bl->length;
10108
10109 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10110 w->cond_exp.get ());
10111 }
10112
10113 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10114
10115 static int
10116 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10117 {
10118 struct watchpoint *w = (struct watchpoint *) bl->owner;
10119 int length = w->exact ? 1 : bl->length;
10120
10121 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10122 w->cond_exp.get ());
10123 }
10124
10125 static int
10126 breakpoint_hit_watchpoint (const struct bp_location *bl,
10127 const address_space *aspace, CORE_ADDR bp_addr,
10128 const target_waitstatus &ws)
10129 {
10130 struct breakpoint *b = bl->owner;
10131 struct watchpoint *w = (struct watchpoint *) b;
10132
10133 /* Continuable hardware watchpoints are treated as non-existent if the
10134 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10135 some data address). Otherwise gdb won't stop on a break instruction
10136 in the code (not from a breakpoint) when a hardware watchpoint has
10137 been defined. Also skip watchpoints which we know did not trigger
10138 (did not match the data address). */
10139 if (is_hardware_watchpoint (b)
10140 && w->watchpoint_triggered == watch_triggered_no)
10141 return 0;
10142
10143 return 1;
10144 }
10145
10146 static void
10147 check_status_watchpoint (bpstat *bs)
10148 {
10149 gdb_assert (is_watchpoint (bs->breakpoint_at));
10150
10151 bpstat_check_watchpoint (bs);
10152 }
10153
10154 /* Implement the "resources_needed" breakpoint_ops method for
10155 hardware watchpoints. */
10156
10157 static int
10158 resources_needed_watchpoint (const struct bp_location *bl)
10159 {
10160 struct watchpoint *w = (struct watchpoint *) bl->owner;
10161 int length = w->exact? 1 : bl->length;
10162
10163 return target_region_ok_for_hw_watchpoint (bl->address, length);
10164 }
10165
10166 /* Implement the "works_in_software_mode" breakpoint_ops method for
10167 hardware watchpoints. */
10168
10169 static int
10170 works_in_software_mode_watchpoint (const struct breakpoint *b)
10171 {
10172 /* Read and access watchpoints only work with hardware support. */
10173 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10174 }
10175
10176 static enum print_stop_action
10177 print_it_watchpoint (bpstat *bs)
10178 {
10179 struct breakpoint *b;
10180 enum print_stop_action result;
10181 struct watchpoint *w;
10182 struct ui_out *uiout = current_uiout;
10183
10184 gdb_assert (bs->bp_location_at != NULL);
10185
10186 b = bs->breakpoint_at;
10187 w = (struct watchpoint *) b;
10188
10189 annotate_watchpoint (b->number);
10190 maybe_print_thread_hit_breakpoint (uiout);
10191
10192 string_file stb;
10193
10194 gdb::optional<ui_out_emit_tuple> tuple_emitter;
10195 switch (b->type)
10196 {
10197 case bp_watchpoint:
10198 case bp_hardware_watchpoint:
10199 if (uiout->is_mi_like_p ())
10200 uiout->field_string
10201 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10202 mention (b);
10203 tuple_emitter.emplace (uiout, "value");
10204 uiout->text ("\nOld value = ");
10205 watchpoint_value_print (bs->old_val.get (), &stb);
10206 uiout->field_stream ("old", stb);
10207 uiout->text ("\nNew value = ");
10208 watchpoint_value_print (w->val.get (), &stb);
10209 uiout->field_stream ("new", stb);
10210 uiout->text ("\n");
10211 /* More than one watchpoint may have been triggered. */
10212 result = PRINT_UNKNOWN;
10213 break;
10214
10215 case bp_read_watchpoint:
10216 if (uiout->is_mi_like_p ())
10217 uiout->field_string
10218 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10219 mention (b);
10220 tuple_emitter.emplace (uiout, "value");
10221 uiout->text ("\nValue = ");
10222 watchpoint_value_print (w->val.get (), &stb);
10223 uiout->field_stream ("value", stb);
10224 uiout->text ("\n");
10225 result = PRINT_UNKNOWN;
10226 break;
10227
10228 case bp_access_watchpoint:
10229 if (bs->old_val != NULL)
10230 {
10231 if (uiout->is_mi_like_p ())
10232 uiout->field_string
10233 ("reason",
10234 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10235 mention (b);
10236 tuple_emitter.emplace (uiout, "value");
10237 uiout->text ("\nOld value = ");
10238 watchpoint_value_print (bs->old_val.get (), &stb);
10239 uiout->field_stream ("old", stb);
10240 uiout->text ("\nNew value = ");
10241 }
10242 else
10243 {
10244 mention (b);
10245 if (uiout->is_mi_like_p ())
10246 uiout->field_string
10247 ("reason",
10248 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10249 tuple_emitter.emplace (uiout, "value");
10250 uiout->text ("\nValue = ");
10251 }
10252 watchpoint_value_print (w->val.get (), &stb);
10253 uiout->field_stream ("new", stb);
10254 uiout->text ("\n");
10255 result = PRINT_UNKNOWN;
10256 break;
10257 default:
10258 result = PRINT_UNKNOWN;
10259 }
10260
10261 return result;
10262 }
10263
10264 /* Implement the "print_mention" breakpoint_ops method for hardware
10265 watchpoints. */
10266
10267 static void
10268 print_mention_watchpoint (struct breakpoint *b)
10269 {
10270 struct watchpoint *w = (struct watchpoint *) b;
10271 struct ui_out *uiout = current_uiout;
10272 const char *tuple_name;
10273
10274 switch (b->type)
10275 {
10276 case bp_watchpoint:
10277 uiout->text ("Watchpoint ");
10278 tuple_name = "wpt";
10279 break;
10280 case bp_hardware_watchpoint:
10281 uiout->text ("Hardware watchpoint ");
10282 tuple_name = "wpt";
10283 break;
10284 case bp_read_watchpoint:
10285 uiout->text ("Hardware read watchpoint ");
10286 tuple_name = "hw-rwpt";
10287 break;
10288 case bp_access_watchpoint:
10289 uiout->text ("Hardware access (read/write) watchpoint ");
10290 tuple_name = "hw-awpt";
10291 break;
10292 default:
10293 internal_error (__FILE__, __LINE__,
10294 _("Invalid hardware watchpoint type."));
10295 }
10296
10297 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10298 uiout->field_signed ("number", b->number);
10299 uiout->text (": ");
10300 uiout->field_string ("exp", w->exp_string.get ());
10301 }
10302
10303 /* Implement the "print_recreate" breakpoint_ops method for
10304 watchpoints. */
10305
10306 static void
10307 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10308 {
10309 struct watchpoint *w = (struct watchpoint *) b;
10310
10311 switch (b->type)
10312 {
10313 case bp_watchpoint:
10314 case bp_hardware_watchpoint:
10315 fprintf_unfiltered (fp, "watch");
10316 break;
10317 case bp_read_watchpoint:
10318 fprintf_unfiltered (fp, "rwatch");
10319 break;
10320 case bp_access_watchpoint:
10321 fprintf_unfiltered (fp, "awatch");
10322 break;
10323 default:
10324 internal_error (__FILE__, __LINE__,
10325 _("Invalid watchpoint type."));
10326 }
10327
10328 fprintf_unfiltered (fp, " %s", w->exp_string.get ());
10329 print_recreate_thread (b, fp);
10330 }
10331
10332 /* Implement the "explains_signal" breakpoint_ops method for
10333 watchpoints. */
10334
10335 static int
10336 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10337 {
10338 /* A software watchpoint cannot cause a signal other than
10339 GDB_SIGNAL_TRAP. */
10340 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10341 return 0;
10342
10343 return 1;
10344 }
10345
10346 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10347
10348 static struct breakpoint_ops watchpoint_breakpoint_ops;
10349
10350 /* Implement the "insert" breakpoint_ops method for
10351 masked hardware watchpoints. */
10352
10353 static int
10354 insert_masked_watchpoint (struct bp_location *bl)
10355 {
10356 struct watchpoint *w = (struct watchpoint *) bl->owner;
10357
10358 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10359 bl->watchpoint_type);
10360 }
10361
10362 /* Implement the "remove" breakpoint_ops method for
10363 masked hardware watchpoints. */
10364
10365 static int
10366 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10367 {
10368 struct watchpoint *w = (struct watchpoint *) bl->owner;
10369
10370 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10371 bl->watchpoint_type);
10372 }
10373
10374 /* Implement the "resources_needed" breakpoint_ops method for
10375 masked hardware watchpoints. */
10376
10377 static int
10378 resources_needed_masked_watchpoint (const struct bp_location *bl)
10379 {
10380 struct watchpoint *w = (struct watchpoint *) bl->owner;
10381
10382 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10383 }
10384
10385 /* Implement the "works_in_software_mode" breakpoint_ops method for
10386 masked hardware watchpoints. */
10387
10388 static int
10389 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10390 {
10391 return 0;
10392 }
10393
10394 /* Implement the "print_it" breakpoint_ops method for
10395 masked hardware watchpoints. */
10396
10397 static enum print_stop_action
10398 print_it_masked_watchpoint (bpstat *bs)
10399 {
10400 struct breakpoint *b = bs->breakpoint_at;
10401 struct ui_out *uiout = current_uiout;
10402
10403 /* Masked watchpoints have only one location. */
10404 gdb_assert (b->loc && b->loc->next == NULL);
10405
10406 annotate_watchpoint (b->number);
10407 maybe_print_thread_hit_breakpoint (uiout);
10408
10409 switch (b->type)
10410 {
10411 case bp_hardware_watchpoint:
10412 if (uiout->is_mi_like_p ())
10413 uiout->field_string
10414 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10415 break;
10416
10417 case bp_read_watchpoint:
10418 if (uiout->is_mi_like_p ())
10419 uiout->field_string
10420 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10421 break;
10422
10423 case bp_access_watchpoint:
10424 if (uiout->is_mi_like_p ())
10425 uiout->field_string
10426 ("reason",
10427 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10428 break;
10429 default:
10430 internal_error (__FILE__, __LINE__,
10431 _("Invalid hardware watchpoint type."));
10432 }
10433
10434 mention (b);
10435 uiout->text (_("\n\
10436 Check the underlying instruction at PC for the memory\n\
10437 address and value which triggered this watchpoint.\n"));
10438 uiout->text ("\n");
10439
10440 /* More than one watchpoint may have been triggered. */
10441 return PRINT_UNKNOWN;
10442 }
10443
10444 /* Implement the "print_one_detail" breakpoint_ops method for
10445 masked hardware watchpoints. */
10446
10447 static void
10448 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10449 struct ui_out *uiout)
10450 {
10451 struct watchpoint *w = (struct watchpoint *) b;
10452
10453 /* Masked watchpoints have only one location. */
10454 gdb_assert (b->loc && b->loc->next == NULL);
10455
10456 uiout->text ("\tmask ");
10457 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10458 uiout->text ("\n");
10459 }
10460
10461 /* Implement the "print_mention" breakpoint_ops method for
10462 masked hardware watchpoints. */
10463
10464 static void
10465 print_mention_masked_watchpoint (struct breakpoint *b)
10466 {
10467 struct watchpoint *w = (struct watchpoint *) b;
10468 struct ui_out *uiout = current_uiout;
10469 const char *tuple_name;
10470
10471 switch (b->type)
10472 {
10473 case bp_hardware_watchpoint:
10474 uiout->text ("Masked hardware watchpoint ");
10475 tuple_name = "wpt";
10476 break;
10477 case bp_read_watchpoint:
10478 uiout->text ("Masked hardware read watchpoint ");
10479 tuple_name = "hw-rwpt";
10480 break;
10481 case bp_access_watchpoint:
10482 uiout->text ("Masked hardware access (read/write) watchpoint ");
10483 tuple_name = "hw-awpt";
10484 break;
10485 default:
10486 internal_error (__FILE__, __LINE__,
10487 _("Invalid hardware watchpoint type."));
10488 }
10489
10490 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10491 uiout->field_signed ("number", b->number);
10492 uiout->text (": ");
10493 uiout->field_string ("exp", w->exp_string.get ());
10494 }
10495
10496 /* Implement the "print_recreate" breakpoint_ops method for
10497 masked hardware watchpoints. */
10498
10499 static void
10500 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10501 {
10502 struct watchpoint *w = (struct watchpoint *) b;
10503
10504 switch (b->type)
10505 {
10506 case bp_hardware_watchpoint:
10507 fprintf_unfiltered (fp, "watch");
10508 break;
10509 case bp_read_watchpoint:
10510 fprintf_unfiltered (fp, "rwatch");
10511 break;
10512 case bp_access_watchpoint:
10513 fprintf_unfiltered (fp, "awatch");
10514 break;
10515 default:
10516 internal_error (__FILE__, __LINE__,
10517 _("Invalid hardware watchpoint type."));
10518 }
10519
10520 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string.get (),
10521 phex (w->hw_wp_mask, sizeof (CORE_ADDR)));
10522 print_recreate_thread (b, fp);
10523 }
10524
10525 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10526
10527 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10528
10529 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10530
10531 static bool
10532 is_masked_watchpoint (const struct breakpoint *b)
10533 {
10534 return b->ops == &masked_watchpoint_breakpoint_ops;
10535 }
10536
10537 /* accessflag: hw_write: watch write,
10538 hw_read: watch read,
10539 hw_access: watch access (read or write) */
10540 static void
10541 watch_command_1 (const char *arg, int accessflag, int from_tty,
10542 bool just_location, bool internal)
10543 {
10544 struct breakpoint *scope_breakpoint = NULL;
10545 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10546 struct value *result;
10547 int saved_bitpos = 0, saved_bitsize = 0;
10548 const char *exp_start = NULL;
10549 const char *exp_end = NULL;
10550 const char *tok, *end_tok;
10551 int toklen = -1;
10552 const char *cond_start = NULL;
10553 const char *cond_end = NULL;
10554 enum bptype bp_type;
10555 int thread = -1;
10556 /* Flag to indicate whether we are going to use masks for
10557 the hardware watchpoint. */
10558 bool use_mask = false;
10559 CORE_ADDR mask = 0;
10560
10561 /* Make sure that we actually have parameters to parse. */
10562 if (arg != NULL && arg[0] != '\0')
10563 {
10564 const char *value_start;
10565
10566 exp_end = arg + strlen (arg);
10567
10568 /* Look for "parameter value" pairs at the end
10569 of the arguments string. */
10570 for (tok = exp_end - 1; tok > arg; tok--)
10571 {
10572 /* Skip whitespace at the end of the argument list. */
10573 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10574 tok--;
10575
10576 /* Find the beginning of the last token.
10577 This is the value of the parameter. */
10578 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10579 tok--;
10580 value_start = tok + 1;
10581
10582 /* Skip whitespace. */
10583 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10584 tok--;
10585
10586 end_tok = tok;
10587
10588 /* Find the beginning of the second to last token.
10589 This is the parameter itself. */
10590 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10591 tok--;
10592 tok++;
10593 toklen = end_tok - tok + 1;
10594
10595 if (toklen == 6 && startswith (tok, "thread"))
10596 {
10597 struct thread_info *thr;
10598 /* At this point we've found a "thread" token, which means
10599 the user is trying to set a watchpoint that triggers
10600 only in a specific thread. */
10601 const char *endp;
10602
10603 if (thread != -1)
10604 error(_("You can specify only one thread."));
10605
10606 /* Extract the thread ID from the next token. */
10607 thr = parse_thread_id (value_start, &endp);
10608
10609 /* Check if the user provided a valid thread ID. */
10610 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10611 invalid_thread_id_error (value_start);
10612
10613 thread = thr->global_num;
10614 }
10615 else if (toklen == 4 && startswith (tok, "mask"))
10616 {
10617 /* We've found a "mask" token, which means the user wants to
10618 create a hardware watchpoint that is going to have the mask
10619 facility. */
10620 struct value *mask_value, *mark;
10621
10622 if (use_mask)
10623 error(_("You can specify only one mask."));
10624
10625 use_mask = just_location = true;
10626
10627 mark = value_mark ();
10628 mask_value = parse_to_comma_and_eval (&value_start);
10629 mask = value_as_address (mask_value);
10630 value_free_to_mark (mark);
10631 }
10632 else
10633 /* We didn't recognize what we found. We should stop here. */
10634 break;
10635
10636 /* Truncate the string and get rid of the "parameter value" pair before
10637 the arguments string is parsed by the parse_exp_1 function. */
10638 exp_end = tok;
10639 }
10640 }
10641 else
10642 exp_end = arg;
10643
10644 /* Parse the rest of the arguments. From here on out, everything
10645 is in terms of a newly allocated string instead of the original
10646 ARG. */
10647 std::string expression (arg, exp_end - arg);
10648 exp_start = arg = expression.c_str ();
10649 innermost_block_tracker tracker;
10650 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10651 exp_end = arg;
10652 /* Remove trailing whitespace from the expression before saving it.
10653 This makes the eventual display of the expression string a bit
10654 prettier. */
10655 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10656 --exp_end;
10657
10658 /* Checking if the expression is not constant. */
10659 if (watchpoint_exp_is_const (exp.get ()))
10660 {
10661 int len;
10662
10663 len = exp_end - exp_start;
10664 while (len > 0 && isspace (exp_start[len - 1]))
10665 len--;
10666 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10667 }
10668
10669 exp_valid_block = tracker.block ();
10670 struct value *mark = value_mark ();
10671 struct value *val_as_value = nullptr;
10672 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10673 just_location);
10674
10675 if (val_as_value != NULL && just_location)
10676 {
10677 saved_bitpos = value_bitpos (val_as_value);
10678 saved_bitsize = value_bitsize (val_as_value);
10679 }
10680
10681 value_ref_ptr val;
10682 if (just_location)
10683 {
10684 int ret;
10685
10686 exp_valid_block = NULL;
10687 val = release_value (value_addr (result));
10688 value_free_to_mark (mark);
10689
10690 if (use_mask)
10691 {
10692 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10693 mask);
10694 if (ret == -1)
10695 error (_("This target does not support masked watchpoints."));
10696 else if (ret == -2)
10697 error (_("Invalid mask or memory region."));
10698 }
10699 }
10700 else if (val_as_value != NULL)
10701 val = release_value (val_as_value);
10702
10703 tok = skip_spaces (arg);
10704 end_tok = skip_to_space (tok);
10705
10706 toklen = end_tok - tok;
10707 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10708 {
10709 tok = cond_start = end_tok + 1;
10710 innermost_block_tracker if_tracker;
10711 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10712
10713 /* The watchpoint expression may not be local, but the condition
10714 may still be. E.g.: `watch global if local > 0'. */
10715 cond_exp_valid_block = if_tracker.block ();
10716
10717 cond_end = tok;
10718 }
10719 if (*tok)
10720 error (_("Junk at end of command."));
10721
10722 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10723
10724 /* Save this because create_internal_breakpoint below invalidates
10725 'wp_frame'. */
10726 frame_id watchpoint_frame = get_frame_id (wp_frame);
10727
10728 /* If the expression is "local", then set up a "watchpoint scope"
10729 breakpoint at the point where we've left the scope of the watchpoint
10730 expression. Create the scope breakpoint before the watchpoint, so
10731 that we will encounter it first in bpstat_stop_status. */
10732 if (exp_valid_block != NULL && wp_frame != NULL)
10733 {
10734 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10735
10736 if (frame_id_p (caller_frame_id))
10737 {
10738 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10739 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10740
10741 scope_breakpoint
10742 = create_internal_breakpoint (caller_arch, caller_pc,
10743 bp_watchpoint_scope,
10744 &momentary_breakpoint_ops);
10745
10746 /* create_internal_breakpoint could invalidate WP_FRAME. */
10747 wp_frame = NULL;
10748
10749 scope_breakpoint->enable_state = bp_enabled;
10750
10751 /* Automatically delete the breakpoint when it hits. */
10752 scope_breakpoint->disposition = disp_del;
10753
10754 /* Only break in the proper frame (help with recursion). */
10755 scope_breakpoint->frame_id = caller_frame_id;
10756
10757 /* Set the address at which we will stop. */
10758 scope_breakpoint->loc->gdbarch = caller_arch;
10759 scope_breakpoint->loc->requested_address = caller_pc;
10760 scope_breakpoint->loc->address
10761 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10762 scope_breakpoint->loc->requested_address,
10763 scope_breakpoint->type);
10764 }
10765 }
10766
10767 /* Now set up the breakpoint. We create all watchpoints as hardware
10768 watchpoints here even if hardware watchpoints are turned off, a call
10769 to update_watchpoint later in this function will cause the type to
10770 drop back to bp_watchpoint (software watchpoint) if required. */
10771
10772 if (accessflag == hw_read)
10773 bp_type = bp_read_watchpoint;
10774 else if (accessflag == hw_access)
10775 bp_type = bp_access_watchpoint;
10776 else
10777 bp_type = bp_hardware_watchpoint;
10778
10779 std::unique_ptr<watchpoint> w (new watchpoint ());
10780
10781 if (use_mask)
10782 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10783 &masked_watchpoint_breakpoint_ops);
10784 else
10785 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10786 &watchpoint_breakpoint_ops);
10787 w->thread = thread;
10788 w->disposition = disp_donttouch;
10789 w->pspace = current_program_space;
10790 w->exp = std::move (exp);
10791 w->exp_valid_block = exp_valid_block;
10792 w->cond_exp_valid_block = cond_exp_valid_block;
10793 if (just_location)
10794 {
10795 struct type *t = value_type (val.get ());
10796 CORE_ADDR addr = value_as_address (val.get ());
10797
10798 w->exp_string_reparse
10799 = current_language->watch_location_expression (t, addr);
10800
10801 w->exp_string = xstrprintf ("-location %.*s",
10802 (int) (exp_end - exp_start), exp_start);
10803 }
10804 else
10805 w->exp_string.reset (savestring (exp_start, exp_end - exp_start));
10806
10807 if (use_mask)
10808 {
10809 w->hw_wp_mask = mask;
10810 }
10811 else
10812 {
10813 w->val = val;
10814 w->val_bitpos = saved_bitpos;
10815 w->val_bitsize = saved_bitsize;
10816 w->val_valid = true;
10817 }
10818
10819 if (cond_start)
10820 w->cond_string.reset (savestring (cond_start, cond_end - cond_start));
10821 else
10822 w->cond_string = 0;
10823
10824 if (frame_id_p (watchpoint_frame))
10825 {
10826 w->watchpoint_frame = watchpoint_frame;
10827 w->watchpoint_thread = inferior_ptid;
10828 }
10829 else
10830 {
10831 w->watchpoint_frame = null_frame_id;
10832 w->watchpoint_thread = null_ptid;
10833 }
10834
10835 if (scope_breakpoint != NULL)
10836 {
10837 /* The scope breakpoint is related to the watchpoint. We will
10838 need to act on them together. */
10839 w->related_breakpoint = scope_breakpoint;
10840 scope_breakpoint->related_breakpoint = w.get ();
10841 }
10842
10843 if (!just_location)
10844 value_free_to_mark (mark);
10845
10846 /* Finally update the new watchpoint. This creates the locations
10847 that should be inserted. */
10848 update_watchpoint (w.get (), 1);
10849
10850 install_breakpoint (internal, std::move (w), 1);
10851 }
10852
10853 /* Return count of debug registers needed to watch the given expression.
10854 If the watchpoint cannot be handled in hardware return zero. */
10855
10856 static int
10857 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10858 {
10859 int found_memory_cnt = 0;
10860
10861 /* Did the user specifically forbid us to use hardware watchpoints? */
10862 if (!can_use_hw_watchpoints)
10863 return 0;
10864
10865 gdb_assert (!vals.empty ());
10866 struct value *head = vals[0].get ();
10867
10868 /* Make sure that the value of the expression depends only upon
10869 memory contents, and values computed from them within GDB. If we
10870 find any register references or function calls, we can't use a
10871 hardware watchpoint.
10872
10873 The idea here is that evaluating an expression generates a series
10874 of values, one holding the value of every subexpression. (The
10875 expression a*b+c has five subexpressions: a, b, a*b, c, and
10876 a*b+c.) GDB's values hold almost enough information to establish
10877 the criteria given above --- they identify memory lvalues,
10878 register lvalues, computed values, etcetera. So we can evaluate
10879 the expression, and then scan the chain of values that leaves
10880 behind to decide whether we can detect any possible change to the
10881 expression's final value using only hardware watchpoints.
10882
10883 However, I don't think that the values returned by inferior
10884 function calls are special in any way. So this function may not
10885 notice that an expression involving an inferior function call
10886 can't be watched with hardware watchpoints. FIXME. */
10887 for (const value_ref_ptr &iter : vals)
10888 {
10889 struct value *v = iter.get ();
10890
10891 if (VALUE_LVAL (v) == lval_memory)
10892 {
10893 if (v != head && value_lazy (v))
10894 /* A lazy memory lvalue in the chain is one that GDB never
10895 needed to fetch; we either just used its address (e.g.,
10896 `a' in `a.b') or we never needed it at all (e.g., `a'
10897 in `a,b'). This doesn't apply to HEAD; if that is
10898 lazy then it was not readable, but watch it anyway. */
10899 ;
10900 else
10901 {
10902 /* Ahh, memory we actually used! Check if we can cover
10903 it with hardware watchpoints. */
10904 struct type *vtype = check_typedef (value_type (v));
10905
10906 /* We only watch structs and arrays if user asked for it
10907 explicitly, never if they just happen to appear in a
10908 middle of some value chain. */
10909 if (v == head
10910 || (vtype->code () != TYPE_CODE_STRUCT
10911 && vtype->code () != TYPE_CODE_ARRAY))
10912 {
10913 CORE_ADDR vaddr = value_address (v);
10914 int len;
10915 int num_regs;
10916
10917 len = (target_exact_watchpoints
10918 && is_scalar_type_recursive (vtype))?
10919 1 : TYPE_LENGTH (value_type (v));
10920
10921 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10922 if (!num_regs)
10923 return 0;
10924 else
10925 found_memory_cnt += num_regs;
10926 }
10927 }
10928 }
10929 else if (VALUE_LVAL (v) != not_lval
10930 && deprecated_value_modifiable (v) == 0)
10931 return 0; /* These are values from the history (e.g., $1). */
10932 else if (VALUE_LVAL (v) == lval_register)
10933 return 0; /* Cannot watch a register with a HW watchpoint. */
10934 }
10935
10936 /* The expression itself looks suitable for using a hardware
10937 watchpoint, but give the target machine a chance to reject it. */
10938 return found_memory_cnt;
10939 }
10940
10941 void
10942 watch_command_wrapper (const char *arg, int from_tty, bool internal)
10943 {
10944 watch_command_1 (arg, hw_write, from_tty, 0, internal);
10945 }
10946
10947 /* Options for the watch, awatch, and rwatch commands. */
10948
10949 struct watch_options
10950 {
10951 /* For -location. */
10952 bool location = false;
10953 };
10954
10955 /* Definitions of options for the "watch", "awatch", and "rwatch" commands.
10956
10957 Historically GDB always accepted both '-location' and '-l' flags for
10958 these commands (both flags being synonyms). When converting to the
10959 newer option scheme only '-location' is added here. That's fine (for
10960 backward compatibility) as any non-ambiguous prefix of a flag will be
10961 accepted, so '-l', '-loc', are now all accepted.
10962
10963 What this means is that, if in the future, we add any new flag here
10964 that starts with '-l' then this will break backward compatibility, so
10965 please, don't do that! */
10966
10967 static const gdb::option::option_def watch_option_defs[] = {
10968 gdb::option::flag_option_def<watch_options> {
10969 "location",
10970 [] (watch_options *opt) { return &opt->location; },
10971 N_("\
10972 This evaluates EXPRESSION and watches the memory to which is refers.\n\
10973 -l can be used as a short form of -location."),
10974 },
10975 };
10976
10977 /* Returns the option group used by 'watch', 'awatch', and 'rwatch'
10978 commands. */
10979
10980 static gdb::option::option_def_group
10981 make_watch_options_def_group (watch_options *opts)
10982 {
10983 return {{watch_option_defs}, opts};
10984 }
10985
10986 /* A helper function that looks for the "-location" argument and then
10987 calls watch_command_1. */
10988
10989 static void
10990 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
10991 {
10992 watch_options opts;
10993 auto grp = make_watch_options_def_group (&opts);
10994 gdb::option::process_options
10995 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
10996 if (arg != nullptr && *arg == '\0')
10997 arg = nullptr;
10998
10999 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
11000 }
11001
11002 /* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
11003 static void
11004 watch_command_completer (struct cmd_list_element *ignore,
11005 completion_tracker &tracker,
11006 const char *text, const char * /*word*/)
11007 {
11008 const auto group = make_watch_options_def_group (nullptr);
11009 if (gdb::option::complete_options
11010 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
11011 return;
11012
11013 const char *word = advance_to_expression_complete_word_point (tracker, text);
11014 expression_completer (ignore, tracker, text, word);
11015 }
11016
11017 static void
11018 watch_command (const char *arg, int from_tty)
11019 {
11020 watch_maybe_just_location (arg, hw_write, from_tty);
11021 }
11022
11023 void
11024 rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
11025 {
11026 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11027 }
11028
11029 static void
11030 rwatch_command (const char *arg, int from_tty)
11031 {
11032 watch_maybe_just_location (arg, hw_read, from_tty);
11033 }
11034
11035 void
11036 awatch_command_wrapper (const char *arg, int from_tty, bool internal)
11037 {
11038 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11039 }
11040
11041 static void
11042 awatch_command (const char *arg, int from_tty)
11043 {
11044 watch_maybe_just_location (arg, hw_access, from_tty);
11045 }
11046 \f
11047
11048 /* Data for the FSM that manages the until(location)/advance commands
11049 in infcmd.c. Here because it uses the mechanisms of
11050 breakpoints. */
11051
11052 struct until_break_fsm : public thread_fsm
11053 {
11054 /* The thread that was current when the command was executed. */
11055 int thread;
11056
11057 /* The breakpoint set at the return address in the caller frame,
11058 plus breakpoints at all the destination locations. */
11059 std::vector<breakpoint_up> breakpoints;
11060
11061 until_break_fsm (struct interp *cmd_interp, int thread,
11062 std::vector<breakpoint_up> &&breakpoints)
11063 : thread_fsm (cmd_interp),
11064 thread (thread),
11065 breakpoints (std::move (breakpoints))
11066 {
11067 }
11068
11069 void clean_up (struct thread_info *thread) override;
11070 bool should_stop (struct thread_info *thread) override;
11071 enum async_reply_reason do_async_reply_reason () override;
11072 };
11073
11074 /* Implementation of the 'should_stop' FSM method for the
11075 until(location)/advance commands. */
11076
11077 bool
11078 until_break_fsm::should_stop (struct thread_info *tp)
11079 {
11080 for (const breakpoint_up &bp : breakpoints)
11081 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11082 bp.get ()) != NULL)
11083 {
11084 set_finished ();
11085 break;
11086 }
11087
11088 return true;
11089 }
11090
11091 /* Implementation of the 'clean_up' FSM method for the
11092 until(location)/advance commands. */
11093
11094 void
11095 until_break_fsm::clean_up (struct thread_info *)
11096 {
11097 /* Clean up our temporary breakpoints. */
11098 breakpoints.clear ();
11099 delete_longjmp_breakpoint (thread);
11100 }
11101
11102 /* Implementation of the 'async_reply_reason' FSM method for the
11103 until(location)/advance commands. */
11104
11105 enum async_reply_reason
11106 until_break_fsm::do_async_reply_reason ()
11107 {
11108 return EXEC_ASYNC_LOCATION_REACHED;
11109 }
11110
11111 void
11112 until_break_command (const char *arg, int from_tty, int anywhere)
11113 {
11114 struct frame_info *frame;
11115 struct gdbarch *frame_gdbarch;
11116 struct frame_id stack_frame_id;
11117 struct frame_id caller_frame_id;
11118 int thread;
11119 struct thread_info *tp;
11120
11121 clear_proceed_status (0);
11122
11123 /* Set a breakpoint where the user wants it and at return from
11124 this function. */
11125
11126 event_location_up location = string_to_event_location (&arg, current_language);
11127
11128 std::vector<symtab_and_line> sals
11129 = (last_displayed_sal_is_valid ()
11130 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11131 get_last_displayed_symtab (),
11132 get_last_displayed_line ())
11133 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11134 NULL, NULL, 0));
11135
11136 if (sals.empty ())
11137 error (_("Couldn't get information on specified line."));
11138
11139 if (*arg)
11140 error (_("Junk at end of arguments."));
11141
11142 tp = inferior_thread ();
11143 thread = tp->global_num;
11144
11145 /* Note linespec handling above invalidates the frame chain.
11146 Installing a breakpoint also invalidates the frame chain (as it
11147 may need to switch threads), so do any frame handling before
11148 that. */
11149
11150 frame = get_selected_frame (NULL);
11151 frame_gdbarch = get_frame_arch (frame);
11152 stack_frame_id = get_stack_frame_id (frame);
11153 caller_frame_id = frame_unwind_caller_id (frame);
11154
11155 /* Keep within the current frame, or in frames called by the current
11156 one. */
11157
11158 std::vector<breakpoint_up> breakpoints;
11159
11160 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11161
11162 if (frame_id_p (caller_frame_id))
11163 {
11164 struct symtab_and_line sal2;
11165 struct gdbarch *caller_gdbarch;
11166
11167 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11168 sal2.pc = frame_unwind_caller_pc (frame);
11169 caller_gdbarch = frame_unwind_caller_arch (frame);
11170
11171 breakpoint_up caller_breakpoint
11172 = set_momentary_breakpoint (caller_gdbarch, sal2,
11173 caller_frame_id, bp_until);
11174 breakpoints.emplace_back (std::move (caller_breakpoint));
11175
11176 set_longjmp_breakpoint (tp, caller_frame_id);
11177 lj_deleter.emplace (thread);
11178 }
11179
11180 /* set_momentary_breakpoint could invalidate FRAME. */
11181 frame = NULL;
11182
11183 /* If the user told us to continue until a specified location, we
11184 don't specify a frame at which we need to stop. Otherwise,
11185 specify the selected frame, because we want to stop only at the
11186 very same frame. */
11187 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
11188
11189 for (symtab_and_line &sal : sals)
11190 {
11191 resolve_sal_pc (&sal);
11192
11193 breakpoint_up location_breakpoint
11194 = set_momentary_breakpoint (frame_gdbarch, sal,
11195 stop_frame_id, bp_until);
11196 breakpoints.emplace_back (std::move (location_breakpoint));
11197 }
11198
11199 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
11200 std::move (breakpoints));
11201
11202 if (lj_deleter)
11203 lj_deleter->release ();
11204
11205 proceed (-1, GDB_SIGNAL_DEFAULT);
11206 }
11207
11208 /* This function attempts to parse an optional "if <cond>" clause
11209 from the arg string. If one is not found, it returns NULL.
11210
11211 Else, it returns a pointer to the condition string. (It does not
11212 attempt to evaluate the string against a particular block.) And,
11213 it updates arg to point to the first character following the parsed
11214 if clause in the arg string. */
11215
11216 const char *
11217 ep_parse_optional_if_clause (const char **arg)
11218 {
11219 const char *cond_string;
11220
11221 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11222 return NULL;
11223
11224 /* Skip the "if" keyword. */
11225 (*arg) += 2;
11226
11227 /* Skip any extra leading whitespace, and record the start of the
11228 condition string. */
11229 *arg = skip_spaces (*arg);
11230 cond_string = *arg;
11231
11232 /* Assume that the condition occupies the remainder of the arg
11233 string. */
11234 (*arg) += strlen (cond_string);
11235
11236 return cond_string;
11237 }
11238
11239 /* Commands to deal with catching events, such as signals, exceptions,
11240 process start/exit, etc. */
11241
11242 typedef enum
11243 {
11244 catch_fork_temporary, catch_vfork_temporary,
11245 catch_fork_permanent, catch_vfork_permanent
11246 }
11247 catch_fork_kind;
11248
11249 static void
11250 catch_fork_command_1 (const char *arg, int from_tty,
11251 struct cmd_list_element *command)
11252 {
11253 struct gdbarch *gdbarch = get_current_arch ();
11254 const char *cond_string = NULL;
11255 catch_fork_kind fork_kind;
11256
11257 fork_kind = (catch_fork_kind) (uintptr_t) command->context ();
11258 bool temp = (fork_kind == catch_fork_temporary
11259 || fork_kind == catch_vfork_temporary);
11260
11261 if (!arg)
11262 arg = "";
11263 arg = skip_spaces (arg);
11264
11265 /* The allowed syntax is:
11266 catch [v]fork
11267 catch [v]fork if <cond>
11268
11269 First, check if there's an if clause. */
11270 cond_string = ep_parse_optional_if_clause (&arg);
11271
11272 if ((*arg != '\0') && !isspace (*arg))
11273 error (_("Junk at end of arguments."));
11274
11275 /* If this target supports it, create a fork or vfork catchpoint
11276 and enable reporting of such events. */
11277 switch (fork_kind)
11278 {
11279 case catch_fork_temporary:
11280 case catch_fork_permanent:
11281 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
11282 &catch_fork_breakpoint_ops);
11283 break;
11284 case catch_vfork_temporary:
11285 case catch_vfork_permanent:
11286 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
11287 &catch_vfork_breakpoint_ops);
11288 break;
11289 default:
11290 error (_("unsupported or unknown fork kind; cannot catch it"));
11291 break;
11292 }
11293 }
11294
11295 static void
11296 catch_exec_command_1 (const char *arg, int from_tty,
11297 struct cmd_list_element *command)
11298 {
11299 struct gdbarch *gdbarch = get_current_arch ();
11300 const char *cond_string = NULL;
11301 bool temp = command->context () == CATCH_TEMPORARY;
11302
11303 if (!arg)
11304 arg = "";
11305 arg = skip_spaces (arg);
11306
11307 /* The allowed syntax is:
11308 catch exec
11309 catch exec if <cond>
11310
11311 First, check if there's an if clause. */
11312 cond_string = ep_parse_optional_if_clause (&arg);
11313
11314 if ((*arg != '\0') && !isspace (*arg))
11315 error (_("Junk at end of arguments."));
11316
11317 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11318 init_catchpoint (c.get (), gdbarch, temp, cond_string,
11319 &catch_exec_breakpoint_ops);
11320 c->exec_pathname.reset ();
11321
11322 install_breakpoint (0, std::move (c), 1);
11323 }
11324
11325 void
11326 init_ada_exception_breakpoint (struct breakpoint *b,
11327 struct gdbarch *gdbarch,
11328 struct symtab_and_line sal,
11329 const char *addr_string,
11330 const struct breakpoint_ops *ops,
11331 int tempflag,
11332 int enabled,
11333 int from_tty)
11334 {
11335 if (from_tty)
11336 {
11337 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11338 if (!loc_gdbarch)
11339 loc_gdbarch = gdbarch;
11340
11341 describe_other_breakpoints (loc_gdbarch,
11342 sal.pspace, sal.pc, sal.section, -1);
11343 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11344 version for exception catchpoints, because two catchpoints
11345 used for different exception names will use the same address.
11346 In this case, a "breakpoint ... also set at..." warning is
11347 unproductive. Besides, the warning phrasing is also a bit
11348 inappropriate, we should use the word catchpoint, and tell
11349 the user what type of catchpoint it is. The above is good
11350 enough for now, though. */
11351 }
11352
11353 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
11354
11355 b->enable_state = enabled ? bp_enabled : bp_disabled;
11356 b->disposition = tempflag ? disp_del : disp_donttouch;
11357 b->location = string_to_event_location (&addr_string,
11358 language_def (language_ada));
11359 b->language = language_ada;
11360 }
11361
11362 \f
11363
11364 /* Compare two breakpoints and return a strcmp-like result. */
11365
11366 static int
11367 compare_breakpoints (const breakpoint *a, const breakpoint *b)
11368 {
11369 uintptr_t ua = (uintptr_t) a;
11370 uintptr_t ub = (uintptr_t) b;
11371
11372 if (a->number < b->number)
11373 return -1;
11374 else if (a->number > b->number)
11375 return 1;
11376
11377 /* Now sort by address, in case we see, e..g, two breakpoints with
11378 the number 0. */
11379 if (ua < ub)
11380 return -1;
11381 return ua > ub ? 1 : 0;
11382 }
11383
11384 /* Delete breakpoints by address or line. */
11385
11386 static void
11387 clear_command (const char *arg, int from_tty)
11388 {
11389 int default_match;
11390
11391 std::vector<symtab_and_line> decoded_sals;
11392 symtab_and_line last_sal;
11393 gdb::array_view<symtab_and_line> sals;
11394 if (arg)
11395 {
11396 decoded_sals
11397 = decode_line_with_current_source (arg,
11398 (DECODE_LINE_FUNFIRSTLINE
11399 | DECODE_LINE_LIST_MODE));
11400 default_match = 0;
11401 sals = decoded_sals;
11402 }
11403 else
11404 {
11405 /* Set sal's line, symtab, pc, and pspace to the values
11406 corresponding to the last call to print_frame_info. If the
11407 codepoint is not valid, this will set all the fields to 0. */
11408 last_sal = get_last_displayed_sal ();
11409 if (last_sal.symtab == 0)
11410 error (_("No source file specified."));
11411
11412 default_match = 1;
11413 sals = last_sal;
11414 }
11415
11416 /* We don't call resolve_sal_pc here. That's not as bad as it
11417 seems, because all existing breakpoints typically have both
11418 file/line and pc set. So, if clear is given file/line, we can
11419 match this to existing breakpoint without obtaining pc at all.
11420
11421 We only support clearing given the address explicitly
11422 present in breakpoint table. Say, we've set breakpoint
11423 at file:line. There were several PC values for that file:line,
11424 due to optimization, all in one block.
11425
11426 We've picked one PC value. If "clear" is issued with another
11427 PC corresponding to the same file:line, the breakpoint won't
11428 be cleared. We probably can still clear the breakpoint, but
11429 since the other PC value is never presented to user, user
11430 can only find it by guessing, and it does not seem important
11431 to support that. */
11432
11433 /* For each line spec given, delete bps which correspond to it. Do
11434 it in two passes, solely to preserve the current behavior that
11435 from_tty is forced true if we delete more than one
11436 breakpoint. */
11437
11438 std::vector<struct breakpoint *> found;
11439 for (const auto &sal : sals)
11440 {
11441 const char *sal_fullname;
11442
11443 /* If exact pc given, clear bpts at that pc.
11444 If line given (pc == 0), clear all bpts on specified line.
11445 If defaulting, clear all bpts on default line
11446 or at default pc.
11447
11448 defaulting sal.pc != 0 tests to do
11449
11450 0 1 pc
11451 1 1 pc _and_ line
11452 0 0 line
11453 1 0 <can't happen> */
11454
11455 sal_fullname = (sal.symtab == NULL
11456 ? NULL : symtab_to_fullname (sal.symtab));
11457
11458 /* Find all matching breakpoints and add them to 'found'. */
11459 for (breakpoint *b : all_breakpoints ())
11460 {
11461 int match = 0;
11462 /* Are we going to delete b? */
11463 if (b->type != bp_none && !is_watchpoint (b))
11464 {
11465 for (bp_location *loc : b->locations ())
11466 {
11467 /* If the user specified file:line, don't allow a PC
11468 match. This matches historical gdb behavior. */
11469 int pc_match = (!sal.explicit_line
11470 && sal.pc
11471 && (loc->pspace == sal.pspace)
11472 && (loc->address == sal.pc)
11473 && (!section_is_overlay (loc->section)
11474 || loc->section == sal.section));
11475 int line_match = 0;
11476
11477 if ((default_match || sal.explicit_line)
11478 && loc->symtab != NULL
11479 && sal_fullname != NULL
11480 && sal.pspace == loc->pspace
11481 && loc->line_number == sal.line
11482 && filename_cmp (symtab_to_fullname (loc->symtab),
11483 sal_fullname) == 0)
11484 line_match = 1;
11485
11486 if (pc_match || line_match)
11487 {
11488 match = 1;
11489 break;
11490 }
11491 }
11492 }
11493
11494 if (match)
11495 found.push_back (b);
11496 }
11497 }
11498
11499 /* Now go thru the 'found' chain and delete them. */
11500 if (found.empty ())
11501 {
11502 if (arg)
11503 error (_("No breakpoint at %s."), arg);
11504 else
11505 error (_("No breakpoint at this line."));
11506 }
11507
11508 /* Remove duplicates from the vec. */
11509 std::sort (found.begin (), found.end (),
11510 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11511 {
11512 return compare_breakpoints (bp_a, bp_b) < 0;
11513 });
11514 found.erase (std::unique (found.begin (), found.end (),
11515 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11516 {
11517 return compare_breakpoints (bp_a, bp_b) == 0;
11518 }),
11519 found.end ());
11520
11521 if (found.size () > 1)
11522 from_tty = 1; /* Always report if deleted more than one. */
11523 if (from_tty)
11524 {
11525 if (found.size () == 1)
11526 printf_unfiltered (_("Deleted breakpoint "));
11527 else
11528 printf_unfiltered (_("Deleted breakpoints "));
11529 }
11530
11531 for (breakpoint *iter : found)
11532 {
11533 if (from_tty)
11534 printf_unfiltered ("%d ", iter->number);
11535 delete_breakpoint (iter);
11536 }
11537 if (from_tty)
11538 putchar_unfiltered ('\n');
11539 }
11540 \f
11541 /* Delete breakpoint in BS if they are `delete' breakpoints and
11542 all breakpoints that are marked for deletion, whether hit or not.
11543 This is called after any breakpoint is hit, or after errors. */
11544
11545 void
11546 breakpoint_auto_delete (bpstat *bs)
11547 {
11548 for (; bs; bs = bs->next)
11549 if (bs->breakpoint_at
11550 && bs->breakpoint_at->disposition == disp_del
11551 && bs->stop)
11552 delete_breakpoint (bs->breakpoint_at);
11553
11554 for (breakpoint *b : all_breakpoints_safe ())
11555 if (b->disposition == disp_del_at_next_stop)
11556 delete_breakpoint (b);
11557 }
11558
11559 /* A comparison function for bp_location AP and BP being interfaced to
11560 std::sort. Sort elements primarily by their ADDRESS (no matter what
11561 bl_address_is_meaningful says), secondarily by ordering first
11562 permanent elements and terciarily just ensuring the array is sorted
11563 stable way despite std::sort being an unstable algorithm. */
11564
11565 static int
11566 bp_location_is_less_than (const bp_location *a, const bp_location *b)
11567 {
11568 if (a->address != b->address)
11569 return a->address < b->address;
11570
11571 /* Sort locations at the same address by their pspace number, keeping
11572 locations of the same inferior (in a multi-inferior environment)
11573 grouped. */
11574
11575 if (a->pspace->num != b->pspace->num)
11576 return a->pspace->num < b->pspace->num;
11577
11578 /* Sort permanent breakpoints first. */
11579 if (a->permanent != b->permanent)
11580 return a->permanent > b->permanent;
11581
11582 /* Sort by type in order to make duplicate determination easier.
11583 See update_global_location_list. This is kept in sync with
11584 breakpoint_locations_match. */
11585 if (a->loc_type < b->loc_type)
11586 return true;
11587
11588 /* Likewise, for range-breakpoints, sort by length. */
11589 if (a->loc_type == bp_loc_hardware_breakpoint
11590 && b->loc_type == bp_loc_hardware_breakpoint
11591 && a->length < b->length)
11592 return true;
11593
11594 /* Make the internal GDB representation stable across GDB runs
11595 where A and B memory inside GDB can differ. Breakpoint locations of
11596 the same type at the same address can be sorted in arbitrary order. */
11597
11598 if (a->owner->number != b->owner->number)
11599 return a->owner->number < b->owner->number;
11600
11601 return a < b;
11602 }
11603
11604 /* Set bp_locations_placed_address_before_address_max and
11605 bp_locations_shadow_len_after_address_max according to the current
11606 content of the bp_locations array. */
11607
11608 static void
11609 bp_locations_target_extensions_update (void)
11610 {
11611 bp_locations_placed_address_before_address_max = 0;
11612 bp_locations_shadow_len_after_address_max = 0;
11613
11614 for (bp_location *bl : all_bp_locations ())
11615 {
11616 CORE_ADDR start, end, addr;
11617
11618 if (!bp_location_has_shadow (bl))
11619 continue;
11620
11621 start = bl->target_info.placed_address;
11622 end = start + bl->target_info.shadow_len;
11623
11624 gdb_assert (bl->address >= start);
11625 addr = bl->address - start;
11626 if (addr > bp_locations_placed_address_before_address_max)
11627 bp_locations_placed_address_before_address_max = addr;
11628
11629 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11630
11631 gdb_assert (bl->address < end);
11632 addr = end - bl->address;
11633 if (addr > bp_locations_shadow_len_after_address_max)
11634 bp_locations_shadow_len_after_address_max = addr;
11635 }
11636 }
11637
11638 /* Download tracepoint locations if they haven't been. */
11639
11640 static void
11641 download_tracepoint_locations (void)
11642 {
11643 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11644
11645 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11646
11647 for (breakpoint *b : all_tracepoints ())
11648 {
11649 struct tracepoint *t;
11650 int bp_location_downloaded = 0;
11651
11652 if ((b->type == bp_fast_tracepoint
11653 ? !may_insert_fast_tracepoints
11654 : !may_insert_tracepoints))
11655 continue;
11656
11657 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11658 {
11659 if (target_can_download_tracepoint ())
11660 can_download_tracepoint = TRIBOOL_TRUE;
11661 else
11662 can_download_tracepoint = TRIBOOL_FALSE;
11663 }
11664
11665 if (can_download_tracepoint == TRIBOOL_FALSE)
11666 break;
11667
11668 for (bp_location *bl : b->locations ())
11669 {
11670 /* In tracepoint, locations are _never_ duplicated, so
11671 should_be_inserted is equivalent to
11672 unduplicated_should_be_inserted. */
11673 if (!should_be_inserted (bl) || bl->inserted)
11674 continue;
11675
11676 switch_to_program_space_and_thread (bl->pspace);
11677
11678 target_download_tracepoint (bl);
11679
11680 bl->inserted = 1;
11681 bp_location_downloaded = 1;
11682 }
11683 t = (struct tracepoint *) b;
11684 t->number_on_target = b->number;
11685 if (bp_location_downloaded)
11686 gdb::observers::breakpoint_modified.notify (b);
11687 }
11688 }
11689
11690 /* Swap the insertion/duplication state between two locations. */
11691
11692 static void
11693 swap_insertion (struct bp_location *left, struct bp_location *right)
11694 {
11695 const int left_inserted = left->inserted;
11696 const int left_duplicate = left->duplicate;
11697 const int left_needs_update = left->needs_update;
11698 const struct bp_target_info left_target_info = left->target_info;
11699
11700 /* Locations of tracepoints can never be duplicated. */
11701 if (is_tracepoint (left->owner))
11702 gdb_assert (!left->duplicate);
11703 if (is_tracepoint (right->owner))
11704 gdb_assert (!right->duplicate);
11705
11706 left->inserted = right->inserted;
11707 left->duplicate = right->duplicate;
11708 left->needs_update = right->needs_update;
11709 left->target_info = right->target_info;
11710 right->inserted = left_inserted;
11711 right->duplicate = left_duplicate;
11712 right->needs_update = left_needs_update;
11713 right->target_info = left_target_info;
11714 }
11715
11716 /* Force the re-insertion of the locations at ADDRESS. This is called
11717 once a new/deleted/modified duplicate location is found and we are evaluating
11718 conditions on the target's side. Such conditions need to be updated on
11719 the target. */
11720
11721 static void
11722 force_breakpoint_reinsertion (struct bp_location *bl)
11723 {
11724 CORE_ADDR address = 0;
11725 int pspace_num;
11726
11727 address = bl->address;
11728 pspace_num = bl->pspace->num;
11729
11730 /* This is only meaningful if the target is
11731 evaluating conditions and if the user has
11732 opted for condition evaluation on the target's
11733 side. */
11734 if (gdb_evaluates_breakpoint_condition_p ()
11735 || !target_supports_evaluation_of_breakpoint_conditions ())
11736 return;
11737
11738 /* Flag all breakpoint locations with this address and
11739 the same program space as the location
11740 as "its condition has changed". We need to
11741 update the conditions on the target's side. */
11742 for (bp_location *loc : all_bp_locations_at_addr (address))
11743 {
11744 if (!is_breakpoint (loc->owner)
11745 || pspace_num != loc->pspace->num)
11746 continue;
11747
11748 /* Flag the location appropriately. We use a different state to
11749 let everyone know that we already updated the set of locations
11750 with addr bl->address and program space bl->pspace. This is so
11751 we don't have to keep calling these functions just to mark locations
11752 that have already been marked. */
11753 loc->condition_changed = condition_updated;
11754
11755 /* Free the agent expression bytecode as well. We will compute
11756 it later on. */
11757 loc->cond_bytecode.reset ();
11758 }
11759 }
11760
11761 /* Called whether new breakpoints are created, or existing breakpoints
11762 deleted, to update the global location list and recompute which
11763 locations are duplicate of which.
11764
11765 The INSERT_MODE flag determines whether locations may not, may, or
11766 shall be inserted now. See 'enum ugll_insert_mode' for more
11767 info. */
11768
11769 static void
11770 update_global_location_list (enum ugll_insert_mode insert_mode)
11771 {
11772 /* Last breakpoint location address that was marked for update. */
11773 CORE_ADDR last_addr = 0;
11774 /* Last breakpoint location program space that was marked for update. */
11775 int last_pspace_num = -1;
11776
11777 /* Used in the duplicates detection below. When iterating over all
11778 bp_locations, points to the first bp_location of a given address.
11779 Breakpoints and watchpoints of different types are never
11780 duplicates of each other. Keep one pointer for each type of
11781 breakpoint/watchpoint, so we only need to loop over all locations
11782 once. */
11783 struct bp_location *bp_loc_first; /* breakpoint */
11784 struct bp_location *wp_loc_first; /* hardware watchpoint */
11785 struct bp_location *awp_loc_first; /* access watchpoint */
11786 struct bp_location *rwp_loc_first; /* read watchpoint */
11787
11788 /* Saved former bp_locations array which we compare against the newly
11789 built bp_locations from the current state of ALL_BREAKPOINTS. */
11790 std::vector<bp_location *> old_locations = std::move (bp_locations);
11791 bp_locations.clear ();
11792
11793 for (breakpoint *b : all_breakpoints ())
11794 for (bp_location *loc : b->locations ())
11795 bp_locations.push_back (loc);
11796
11797 /* See if we need to "upgrade" a software breakpoint to a hardware
11798 breakpoint. Do this before deciding whether locations are
11799 duplicates. Also do this before sorting because sorting order
11800 depends on location type. */
11801 for (bp_location *loc : bp_locations)
11802 if (!loc->inserted && should_be_inserted (loc))
11803 handle_automatic_hardware_breakpoints (loc);
11804
11805 std::sort (bp_locations.begin (), bp_locations.end (),
11806 bp_location_is_less_than);
11807
11808 bp_locations_target_extensions_update ();
11809
11810 /* Identify bp_location instances that are no longer present in the
11811 new list, and therefore should be freed. Note that it's not
11812 necessary that those locations should be removed from inferior --
11813 if there's another location at the same address (previously
11814 marked as duplicate), we don't need to remove/insert the
11815 location.
11816
11817 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11818 and former bp_location array state respectively. */
11819
11820 size_t loc_i = 0;
11821 for (bp_location *old_loc : old_locations)
11822 {
11823 /* Tells if 'old_loc' is found among the new locations. If
11824 not, we have to free it. */
11825 int found_object = 0;
11826 /* Tells if the location should remain inserted in the target. */
11827 int keep_in_target = 0;
11828 int removed = 0;
11829
11830 /* Skip LOCP entries which will definitely never be needed.
11831 Stop either at or being the one matching OLD_LOC. */
11832 while (loc_i < bp_locations.size ()
11833 && bp_locations[loc_i]->address < old_loc->address)
11834 loc_i++;
11835
11836 for (size_t loc2_i = loc_i;
11837 (loc2_i < bp_locations.size ()
11838 && bp_locations[loc2_i]->address == old_loc->address);
11839 loc2_i++)
11840 {
11841 /* Check if this is a new/duplicated location or a duplicated
11842 location that had its condition modified. If so, we want to send
11843 its condition to the target if evaluation of conditions is taking
11844 place there. */
11845 if (bp_locations[loc2_i]->condition_changed == condition_modified
11846 && (last_addr != old_loc->address
11847 || last_pspace_num != old_loc->pspace->num))
11848 {
11849 force_breakpoint_reinsertion (bp_locations[loc2_i]);
11850 last_pspace_num = old_loc->pspace->num;
11851 }
11852
11853 if (bp_locations[loc2_i] == old_loc)
11854 found_object = 1;
11855 }
11856
11857 /* We have already handled this address, update it so that we don't
11858 have to go through updates again. */
11859 last_addr = old_loc->address;
11860
11861 /* Target-side condition evaluation: Handle deleted locations. */
11862 if (!found_object)
11863 force_breakpoint_reinsertion (old_loc);
11864
11865 /* If this location is no longer present, and inserted, look if
11866 there's maybe a new location at the same address. If so,
11867 mark that one inserted, and don't remove this one. This is
11868 needed so that we don't have a time window where a breakpoint
11869 at certain location is not inserted. */
11870
11871 if (old_loc->inserted)
11872 {
11873 /* If the location is inserted now, we might have to remove
11874 it. */
11875
11876 if (found_object && should_be_inserted (old_loc))
11877 {
11878 /* The location is still present in the location list,
11879 and still should be inserted. Don't do anything. */
11880 keep_in_target = 1;
11881 }
11882 else
11883 {
11884 /* This location still exists, but it won't be kept in the
11885 target since it may have been disabled. We proceed to
11886 remove its target-side condition. */
11887
11888 /* The location is either no longer present, or got
11889 disabled. See if there's another location at the
11890 same address, in which case we don't need to remove
11891 this one from the target. */
11892
11893 /* OLD_LOC comes from existing struct breakpoint. */
11894 if (bl_address_is_meaningful (old_loc))
11895 {
11896 for (size_t loc2_i = loc_i;
11897 (loc2_i < bp_locations.size ()
11898 && bp_locations[loc2_i]->address == old_loc->address);
11899 loc2_i++)
11900 {
11901 bp_location *loc2 = bp_locations[loc2_i];
11902
11903 if (loc2 == old_loc)
11904 continue;
11905
11906 if (breakpoint_locations_match (loc2, old_loc))
11907 {
11908 /* Read watchpoint locations are switched to
11909 access watchpoints, if the former are not
11910 supported, but the latter are. */
11911 if (is_hardware_watchpoint (old_loc->owner))
11912 {
11913 gdb_assert (is_hardware_watchpoint (loc2->owner));
11914 loc2->watchpoint_type = old_loc->watchpoint_type;
11915 }
11916
11917 /* loc2 is a duplicated location. We need to check
11918 if it should be inserted in case it will be
11919 unduplicated. */
11920 if (unduplicated_should_be_inserted (loc2))
11921 {
11922 swap_insertion (old_loc, loc2);
11923 keep_in_target = 1;
11924 break;
11925 }
11926 }
11927 }
11928 }
11929 }
11930
11931 if (!keep_in_target)
11932 {
11933 if (remove_breakpoint (old_loc))
11934 {
11935 /* This is just about all we can do. We could keep
11936 this location on the global list, and try to
11937 remove it next time, but there's no particular
11938 reason why we will succeed next time.
11939
11940 Note that at this point, old_loc->owner is still
11941 valid, as delete_breakpoint frees the breakpoint
11942 only after calling us. */
11943 printf_filtered (_("warning: Error removing "
11944 "breakpoint %d\n"),
11945 old_loc->owner->number);
11946 }
11947 removed = 1;
11948 }
11949 }
11950
11951 if (!found_object)
11952 {
11953 if (removed && target_is_non_stop_p ()
11954 && need_moribund_for_location_type (old_loc))
11955 {
11956 /* This location was removed from the target. In
11957 non-stop mode, a race condition is possible where
11958 we've removed a breakpoint, but stop events for that
11959 breakpoint are already queued and will arrive later.
11960 We apply an heuristic to be able to distinguish such
11961 SIGTRAPs from other random SIGTRAPs: we keep this
11962 breakpoint location for a bit, and will retire it
11963 after we see some number of events. The theory here
11964 is that reporting of events should, "on the average",
11965 be fair, so after a while we'll see events from all
11966 threads that have anything of interest, and no longer
11967 need to keep this breakpoint location around. We
11968 don't hold locations forever so to reduce chances of
11969 mistaking a non-breakpoint SIGTRAP for a breakpoint
11970 SIGTRAP.
11971
11972 The heuristic failing can be disastrous on
11973 decr_pc_after_break targets.
11974
11975 On decr_pc_after_break targets, like e.g., x86-linux,
11976 if we fail to recognize a late breakpoint SIGTRAP,
11977 because events_till_retirement has reached 0 too
11978 soon, we'll fail to do the PC adjustment, and report
11979 a random SIGTRAP to the user. When the user resumes
11980 the inferior, it will most likely immediately crash
11981 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
11982 corrupted, because of being resumed e.g., in the
11983 middle of a multi-byte instruction, or skipped a
11984 one-byte instruction. This was actually seen happen
11985 on native x86-linux, and should be less rare on
11986 targets that do not support new thread events, like
11987 remote, due to the heuristic depending on
11988 thread_count.
11989
11990 Mistaking a random SIGTRAP for a breakpoint trap
11991 causes similar symptoms (PC adjustment applied when
11992 it shouldn't), but then again, playing with SIGTRAPs
11993 behind the debugger's back is asking for trouble.
11994
11995 Since hardware watchpoint traps are always
11996 distinguishable from other traps, so we don't need to
11997 apply keep hardware watchpoint moribund locations
11998 around. We simply always ignore hardware watchpoint
11999 traps we can no longer explain. */
12000
12001 process_stratum_target *proc_target = nullptr;
12002 for (inferior *inf : all_inferiors ())
12003 if (inf->pspace == old_loc->pspace)
12004 {
12005 proc_target = inf->process_target ();
12006 break;
12007 }
12008 if (proc_target != nullptr)
12009 old_loc->events_till_retirement
12010 = 3 * (thread_count (proc_target) + 1);
12011 else
12012 old_loc->events_till_retirement = 1;
12013 old_loc->owner = NULL;
12014
12015 moribund_locations.push_back (old_loc);
12016 }
12017 else
12018 {
12019 old_loc->owner = NULL;
12020 decref_bp_location (&old_loc);
12021 }
12022 }
12023 }
12024
12025 /* Rescan breakpoints at the same address and section, marking the
12026 first one as "first" and any others as "duplicates". This is so
12027 that the bpt instruction is only inserted once. If we have a
12028 permanent breakpoint at the same place as BPT, make that one the
12029 official one, and the rest as duplicates. Permanent breakpoints
12030 are sorted first for the same address.
12031
12032 Do the same for hardware watchpoints, but also considering the
12033 watchpoint's type (regular/access/read) and length. */
12034
12035 bp_loc_first = NULL;
12036 wp_loc_first = NULL;
12037 awp_loc_first = NULL;
12038 rwp_loc_first = NULL;
12039
12040 for (bp_location *loc : all_bp_locations ())
12041 {
12042 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12043 non-NULL. */
12044 struct bp_location **loc_first_p;
12045 breakpoint *b = loc->owner;
12046
12047 if (!unduplicated_should_be_inserted (loc)
12048 || !bl_address_is_meaningful (loc)
12049 /* Don't detect duplicate for tracepoint locations because they are
12050 never duplicated. See the comments in field `duplicate' of
12051 `struct bp_location'. */
12052 || is_tracepoint (b))
12053 {
12054 /* Clear the condition modification flag. */
12055 loc->condition_changed = condition_unchanged;
12056 continue;
12057 }
12058
12059 if (b->type == bp_hardware_watchpoint)
12060 loc_first_p = &wp_loc_first;
12061 else if (b->type == bp_read_watchpoint)
12062 loc_first_p = &rwp_loc_first;
12063 else if (b->type == bp_access_watchpoint)
12064 loc_first_p = &awp_loc_first;
12065 else
12066 loc_first_p = &bp_loc_first;
12067
12068 if (*loc_first_p == NULL
12069 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12070 || !breakpoint_locations_match (loc, *loc_first_p))
12071 {
12072 *loc_first_p = loc;
12073 loc->duplicate = 0;
12074
12075 if (is_breakpoint (loc->owner) && loc->condition_changed)
12076 {
12077 loc->needs_update = 1;
12078 /* Clear the condition modification flag. */
12079 loc->condition_changed = condition_unchanged;
12080 }
12081 continue;
12082 }
12083
12084
12085 /* This and the above ensure the invariant that the first location
12086 is not duplicated, and is the inserted one.
12087 All following are marked as duplicated, and are not inserted. */
12088 if (loc->inserted)
12089 swap_insertion (loc, *loc_first_p);
12090 loc->duplicate = 1;
12091
12092 /* Clear the condition modification flag. */
12093 loc->condition_changed = condition_unchanged;
12094 }
12095
12096 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12097 {
12098 if (insert_mode != UGLL_DONT_INSERT)
12099 insert_breakpoint_locations ();
12100 else
12101 {
12102 /* Even though the caller told us to not insert new
12103 locations, we may still need to update conditions on the
12104 target's side of breakpoints that were already inserted
12105 if the target is evaluating breakpoint conditions. We
12106 only update conditions for locations that are marked
12107 "needs_update". */
12108 update_inserted_breakpoint_locations ();
12109 }
12110 }
12111
12112 if (insert_mode != UGLL_DONT_INSERT)
12113 download_tracepoint_locations ();
12114 }
12115
12116 void
12117 breakpoint_retire_moribund (void)
12118 {
12119 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12120 {
12121 struct bp_location *loc = moribund_locations[ix];
12122 if (--(loc->events_till_retirement) == 0)
12123 {
12124 decref_bp_location (&loc);
12125 unordered_remove (moribund_locations, ix);
12126 --ix;
12127 }
12128 }
12129 }
12130
12131 static void
12132 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12133 {
12134
12135 try
12136 {
12137 update_global_location_list (insert_mode);
12138 }
12139 catch (const gdb_exception_error &e)
12140 {
12141 }
12142 }
12143
12144 /* Clear BKP from a BPS. */
12145
12146 static void
12147 bpstat_remove_bp_location (bpstat *bps, struct breakpoint *bpt)
12148 {
12149 bpstat *bs;
12150
12151 for (bs = bps; bs; bs = bs->next)
12152 if (bs->breakpoint_at == bpt)
12153 {
12154 bs->breakpoint_at = NULL;
12155 bs->old_val = NULL;
12156 /* bs->commands will be freed later. */
12157 }
12158 }
12159
12160 /* Callback for iterate_over_threads. */
12161 static int
12162 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12163 {
12164 struct breakpoint *bpt = (struct breakpoint *) data;
12165
12166 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12167 return 0;
12168 }
12169
12170 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12171 callbacks. */
12172
12173 static void
12174 say_where (struct breakpoint *b)
12175 {
12176 struct value_print_options opts;
12177
12178 get_user_print_options (&opts);
12179
12180 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12181 single string. */
12182 if (b->loc == NULL)
12183 {
12184 /* For pending locations, the output differs slightly based
12185 on b->extra_string. If this is non-NULL, it contains either
12186 a condition or dprintf arguments. */
12187 if (b->extra_string == NULL)
12188 {
12189 printf_filtered (_(" (%s) pending."),
12190 event_location_to_string (b->location.get ()));
12191 }
12192 else if (b->type == bp_dprintf)
12193 {
12194 printf_filtered (_(" (%s,%s) pending."),
12195 event_location_to_string (b->location.get ()),
12196 b->extra_string.get ());
12197 }
12198 else
12199 {
12200 printf_filtered (_(" (%s %s) pending."),
12201 event_location_to_string (b->location.get ()),
12202 b->extra_string.get ());
12203 }
12204 }
12205 else
12206 {
12207 if (opts.addressprint || b->loc->symtab == NULL)
12208 printf_filtered (" at %ps",
12209 styled_string (address_style.style (),
12210 paddress (b->loc->gdbarch,
12211 b->loc->address)));
12212 if (b->loc->symtab != NULL)
12213 {
12214 /* If there is a single location, we can print the location
12215 more nicely. */
12216 if (b->loc->next == NULL)
12217 {
12218 const char *filename
12219 = symtab_to_filename_for_display (b->loc->symtab);
12220 printf_filtered (": file %ps, line %d.",
12221 styled_string (file_name_style.style (),
12222 filename),
12223 b->loc->line_number);
12224 }
12225 else
12226 /* This is not ideal, but each location may have a
12227 different file name, and this at least reflects the
12228 real situation somewhat. */
12229 printf_filtered (": %s.",
12230 event_location_to_string (b->location.get ()));
12231 }
12232
12233 if (b->loc->next)
12234 {
12235 struct bp_location *loc = b->loc;
12236 int n = 0;
12237 for (; loc; loc = loc->next)
12238 ++n;
12239 printf_filtered (" (%d locations)", n);
12240 }
12241 }
12242 }
12243
12244 /* See breakpoint.h. */
12245
12246 bp_location_range breakpoint::locations ()
12247 {
12248 return bp_location_range (this->loc);
12249 }
12250
12251 static struct bp_location *
12252 base_breakpoint_allocate_location (struct breakpoint *self)
12253 {
12254 return new bp_location (self);
12255 }
12256
12257 static void
12258 base_breakpoint_re_set (struct breakpoint *b)
12259 {
12260 /* Nothing to re-set. */
12261 }
12262
12263 #define internal_error_pure_virtual_called() \
12264 gdb_assert_not_reached ("pure virtual function called")
12265
12266 static int
12267 base_breakpoint_insert_location (struct bp_location *bl)
12268 {
12269 internal_error_pure_virtual_called ();
12270 }
12271
12272 static int
12273 base_breakpoint_remove_location (struct bp_location *bl,
12274 enum remove_bp_reason reason)
12275 {
12276 internal_error_pure_virtual_called ();
12277 }
12278
12279 static int
12280 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12281 const address_space *aspace,
12282 CORE_ADDR bp_addr,
12283 const target_waitstatus &ws)
12284 {
12285 internal_error_pure_virtual_called ();
12286 }
12287
12288 static void
12289 base_breakpoint_check_status (bpstat *bs)
12290 {
12291 /* Always stop. */
12292 }
12293
12294 /* A "works_in_software_mode" breakpoint_ops method that just internal
12295 errors. */
12296
12297 static int
12298 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12299 {
12300 internal_error_pure_virtual_called ();
12301 }
12302
12303 /* A "resources_needed" breakpoint_ops method that just internal
12304 errors. */
12305
12306 static int
12307 base_breakpoint_resources_needed (const struct bp_location *bl)
12308 {
12309 internal_error_pure_virtual_called ();
12310 }
12311
12312 static enum print_stop_action
12313 base_breakpoint_print_it (bpstat *bs)
12314 {
12315 internal_error_pure_virtual_called ();
12316 }
12317
12318 static void
12319 base_breakpoint_print_one_detail (const struct breakpoint *self,
12320 struct ui_out *uiout)
12321 {
12322 /* nothing */
12323 }
12324
12325 static void
12326 base_breakpoint_print_mention (struct breakpoint *b)
12327 {
12328 internal_error_pure_virtual_called ();
12329 }
12330
12331 static void
12332 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12333 {
12334 internal_error_pure_virtual_called ();
12335 }
12336
12337 static void
12338 base_breakpoint_create_sals_from_location
12339 (struct event_location *location,
12340 struct linespec_result *canonical,
12341 enum bptype type_wanted)
12342 {
12343 internal_error_pure_virtual_called ();
12344 }
12345
12346 static void
12347 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12348 struct linespec_result *c,
12349 gdb::unique_xmalloc_ptr<char> cond_string,
12350 gdb::unique_xmalloc_ptr<char> extra_string,
12351 enum bptype type_wanted,
12352 enum bpdisp disposition,
12353 int thread,
12354 int task, int ignore_count,
12355 const struct breakpoint_ops *o,
12356 int from_tty, int enabled,
12357 int internal, unsigned flags)
12358 {
12359 internal_error_pure_virtual_called ();
12360 }
12361
12362 static std::vector<symtab_and_line>
12363 base_breakpoint_decode_location (struct breakpoint *b,
12364 struct event_location *location,
12365 struct program_space *search_pspace)
12366 {
12367 internal_error_pure_virtual_called ();
12368 }
12369
12370 /* The default 'explains_signal' method. */
12371
12372 static int
12373 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12374 {
12375 return 1;
12376 }
12377
12378 /* The default "after_condition_true" method. */
12379
12380 static void
12381 base_breakpoint_after_condition_true (struct bpstat *bs)
12382 {
12383 /* Nothing to do. */
12384 }
12385
12386 struct breakpoint_ops base_breakpoint_ops =
12387 {
12388 base_breakpoint_allocate_location,
12389 base_breakpoint_re_set,
12390 base_breakpoint_insert_location,
12391 base_breakpoint_remove_location,
12392 base_breakpoint_breakpoint_hit,
12393 base_breakpoint_check_status,
12394 base_breakpoint_resources_needed,
12395 base_breakpoint_works_in_software_mode,
12396 base_breakpoint_print_it,
12397 NULL,
12398 base_breakpoint_print_one_detail,
12399 base_breakpoint_print_mention,
12400 base_breakpoint_print_recreate,
12401 base_breakpoint_create_sals_from_location,
12402 base_breakpoint_create_breakpoints_sal,
12403 base_breakpoint_decode_location,
12404 base_breakpoint_explains_signal,
12405 base_breakpoint_after_condition_true,
12406 };
12407
12408 /* Default breakpoint_ops methods. */
12409
12410 static void
12411 bkpt_re_set (struct breakpoint *b)
12412 {
12413 /* FIXME: is this still reachable? */
12414 if (breakpoint_event_location_empty_p (b))
12415 {
12416 /* Anything without a location can't be re-set. */
12417 delete_breakpoint (b);
12418 return;
12419 }
12420
12421 breakpoint_re_set_default (b);
12422 }
12423
12424 static int
12425 bkpt_insert_location (struct bp_location *bl)
12426 {
12427 CORE_ADDR addr = bl->target_info.reqstd_address;
12428
12429 bl->target_info.kind = breakpoint_kind (bl, &addr);
12430 bl->target_info.placed_address = addr;
12431
12432 if (bl->loc_type == bp_loc_hardware_breakpoint)
12433 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12434 else
12435 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12436 }
12437
12438 static int
12439 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12440 {
12441 if (bl->loc_type == bp_loc_hardware_breakpoint)
12442 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12443 else
12444 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12445 }
12446
12447 static int
12448 bkpt_breakpoint_hit (const struct bp_location *bl,
12449 const address_space *aspace, CORE_ADDR bp_addr,
12450 const target_waitstatus &ws)
12451 {
12452 if (ws.kind () != TARGET_WAITKIND_STOPPED
12453 || ws.sig () != GDB_SIGNAL_TRAP)
12454 return 0;
12455
12456 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12457 aspace, bp_addr))
12458 return 0;
12459
12460 if (overlay_debugging /* unmapped overlay section */
12461 && section_is_overlay (bl->section)
12462 && !section_is_mapped (bl->section))
12463 return 0;
12464
12465 return 1;
12466 }
12467
12468 static int
12469 dprintf_breakpoint_hit (const struct bp_location *bl,
12470 const address_space *aspace, CORE_ADDR bp_addr,
12471 const target_waitstatus &ws)
12472 {
12473 if (dprintf_style == dprintf_style_agent
12474 && target_can_run_breakpoint_commands ())
12475 {
12476 /* An agent-style dprintf never causes a stop. If we see a trap
12477 for this address it must be for a breakpoint that happens to
12478 be set at the same address. */
12479 return 0;
12480 }
12481
12482 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12483 }
12484
12485 static int
12486 bkpt_resources_needed (const struct bp_location *bl)
12487 {
12488 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12489
12490 return 1;
12491 }
12492
12493 static enum print_stop_action
12494 bkpt_print_it (bpstat *bs)
12495 {
12496 struct breakpoint *b;
12497 const struct bp_location *bl;
12498 int bp_temp;
12499 struct ui_out *uiout = current_uiout;
12500
12501 gdb_assert (bs->bp_location_at != NULL);
12502
12503 bl = bs->bp_location_at.get ();
12504 b = bs->breakpoint_at;
12505
12506 bp_temp = b->disposition == disp_del;
12507 if (bl->address != bl->requested_address)
12508 breakpoint_adjustment_warning (bl->requested_address,
12509 bl->address,
12510 b->number, 1);
12511 annotate_breakpoint (b->number);
12512 maybe_print_thread_hit_breakpoint (uiout);
12513
12514 if (uiout->is_mi_like_p ())
12515 {
12516 uiout->field_string ("reason",
12517 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12518 uiout->field_string ("disp", bpdisp_text (b->disposition));
12519 }
12520 if (bp_temp)
12521 uiout->message ("Temporary breakpoint %pF, ",
12522 signed_field ("bkptno", b->number));
12523 else
12524 uiout->message ("Breakpoint %pF, ",
12525 signed_field ("bkptno", b->number));
12526
12527 return PRINT_SRC_AND_LOC;
12528 }
12529
12530 static void
12531 bkpt_print_mention (struct breakpoint *b)
12532 {
12533 if (current_uiout->is_mi_like_p ())
12534 return;
12535
12536 switch (b->type)
12537 {
12538 case bp_breakpoint:
12539 case bp_gnu_ifunc_resolver:
12540 if (b->disposition == disp_del)
12541 printf_filtered (_("Temporary breakpoint"));
12542 else
12543 printf_filtered (_("Breakpoint"));
12544 printf_filtered (_(" %d"), b->number);
12545 if (b->type == bp_gnu_ifunc_resolver)
12546 printf_filtered (_(" at gnu-indirect-function resolver"));
12547 break;
12548 case bp_hardware_breakpoint:
12549 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12550 break;
12551 case bp_dprintf:
12552 printf_filtered (_("Dprintf %d"), b->number);
12553 break;
12554 }
12555
12556 say_where (b);
12557 }
12558
12559 static void
12560 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12561 {
12562 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12563 fprintf_unfiltered (fp, "tbreak");
12564 else if (tp->type == bp_breakpoint)
12565 fprintf_unfiltered (fp, "break");
12566 else if (tp->type == bp_hardware_breakpoint
12567 && tp->disposition == disp_del)
12568 fprintf_unfiltered (fp, "thbreak");
12569 else if (tp->type == bp_hardware_breakpoint)
12570 fprintf_unfiltered (fp, "hbreak");
12571 else
12572 internal_error (__FILE__, __LINE__,
12573 _("unhandled breakpoint type %d"), (int) tp->type);
12574
12575 fprintf_unfiltered (fp, " %s",
12576 event_location_to_string (tp->location.get ()));
12577
12578 /* Print out extra_string if this breakpoint is pending. It might
12579 contain, for example, conditions that were set by the user. */
12580 if (tp->loc == NULL && tp->extra_string != NULL)
12581 fprintf_unfiltered (fp, " %s", tp->extra_string.get ());
12582
12583 print_recreate_thread (tp, fp);
12584 }
12585
12586 static void
12587 bkpt_create_sals_from_location (struct event_location *location,
12588 struct linespec_result *canonical,
12589 enum bptype type_wanted)
12590 {
12591 create_sals_from_location_default (location, canonical, type_wanted);
12592 }
12593
12594 static void
12595 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12596 struct linespec_result *canonical,
12597 gdb::unique_xmalloc_ptr<char> cond_string,
12598 gdb::unique_xmalloc_ptr<char> extra_string,
12599 enum bptype type_wanted,
12600 enum bpdisp disposition,
12601 int thread,
12602 int task, int ignore_count,
12603 const struct breakpoint_ops *ops,
12604 int from_tty, int enabled,
12605 int internal, unsigned flags)
12606 {
12607 create_breakpoints_sal_default (gdbarch, canonical,
12608 std::move (cond_string),
12609 std::move (extra_string),
12610 type_wanted,
12611 disposition, thread, task,
12612 ignore_count, ops, from_tty,
12613 enabled, internal, flags);
12614 }
12615
12616 static std::vector<symtab_and_line>
12617 bkpt_decode_location (struct breakpoint *b,
12618 struct event_location *location,
12619 struct program_space *search_pspace)
12620 {
12621 return decode_location_default (b, location, search_pspace);
12622 }
12623
12624 /* Virtual table for internal breakpoints. */
12625
12626 static void
12627 internal_bkpt_re_set (struct breakpoint *b)
12628 {
12629 switch (b->type)
12630 {
12631 /* Delete overlay event and longjmp master breakpoints; they
12632 will be reset later by breakpoint_re_set. */
12633 case bp_overlay_event:
12634 case bp_longjmp_master:
12635 case bp_std_terminate_master:
12636 case bp_exception_master:
12637 delete_breakpoint (b);
12638 break;
12639
12640 /* This breakpoint is special, it's set up when the inferior
12641 starts and we really don't want to touch it. */
12642 case bp_shlib_event:
12643
12644 /* Like bp_shlib_event, this breakpoint type is special. Once
12645 it is set up, we do not want to touch it. */
12646 case bp_thread_event:
12647 break;
12648 }
12649 }
12650
12651 static void
12652 internal_bkpt_check_status (bpstat *bs)
12653 {
12654 if (bs->breakpoint_at->type == bp_shlib_event)
12655 {
12656 /* If requested, stop when the dynamic linker notifies GDB of
12657 events. This allows the user to get control and place
12658 breakpoints in initializer routines for dynamically loaded
12659 objects (among other things). */
12660 bs->stop = stop_on_solib_events;
12661 bs->print = stop_on_solib_events;
12662 }
12663 else
12664 bs->stop = 0;
12665 }
12666
12667 static enum print_stop_action
12668 internal_bkpt_print_it (bpstat *bs)
12669 {
12670 struct breakpoint *b;
12671
12672 b = bs->breakpoint_at;
12673
12674 switch (b->type)
12675 {
12676 case bp_shlib_event:
12677 /* Did we stop because the user set the stop_on_solib_events
12678 variable? (If so, we report this as a generic, "Stopped due
12679 to shlib event" message.) */
12680 print_solib_event (0);
12681 break;
12682
12683 case bp_thread_event:
12684 /* Not sure how we will get here.
12685 GDB should not stop for these breakpoints. */
12686 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12687 break;
12688
12689 case bp_overlay_event:
12690 /* By analogy with the thread event, GDB should not stop for these. */
12691 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12692 break;
12693
12694 case bp_longjmp_master:
12695 /* These should never be enabled. */
12696 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12697 break;
12698
12699 case bp_std_terminate_master:
12700 /* These should never be enabled. */
12701 printf_filtered (_("std::terminate Master Breakpoint: "
12702 "gdb should not stop!\n"));
12703 break;
12704
12705 case bp_exception_master:
12706 /* These should never be enabled. */
12707 printf_filtered (_("Exception Master Breakpoint: "
12708 "gdb should not stop!\n"));
12709 break;
12710 }
12711
12712 return PRINT_NOTHING;
12713 }
12714
12715 static void
12716 internal_bkpt_print_mention (struct breakpoint *b)
12717 {
12718 /* Nothing to mention. These breakpoints are internal. */
12719 }
12720
12721 /* Virtual table for momentary breakpoints */
12722
12723 static void
12724 momentary_bkpt_re_set (struct breakpoint *b)
12725 {
12726 /* Keep temporary breakpoints, which can be encountered when we step
12727 over a dlopen call and solib_add is resetting the breakpoints.
12728 Otherwise these should have been blown away via the cleanup chain
12729 or by breakpoint_init_inferior when we rerun the executable. */
12730 }
12731
12732 static void
12733 momentary_bkpt_check_status (bpstat *bs)
12734 {
12735 /* Nothing. The point of these breakpoints is causing a stop. */
12736 }
12737
12738 static enum print_stop_action
12739 momentary_bkpt_print_it (bpstat *bs)
12740 {
12741 return PRINT_UNKNOWN;
12742 }
12743
12744 static void
12745 momentary_bkpt_print_mention (struct breakpoint *b)
12746 {
12747 /* Nothing to mention. These breakpoints are internal. */
12748 }
12749
12750 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12751
12752 It gets cleared already on the removal of the first one of such placed
12753 breakpoints. This is OK as they get all removed altogether. */
12754
12755 longjmp_breakpoint::~longjmp_breakpoint ()
12756 {
12757 thread_info *tp = find_thread_global_id (this->thread);
12758
12759 if (tp != NULL)
12760 tp->initiating_frame = null_frame_id;
12761 }
12762
12763 /* Specific methods for probe breakpoints. */
12764
12765 static int
12766 bkpt_probe_insert_location (struct bp_location *bl)
12767 {
12768 int v = bkpt_insert_location (bl);
12769
12770 if (v == 0)
12771 {
12772 /* The insertion was successful, now let's set the probe's semaphore
12773 if needed. */
12774 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
12775 }
12776
12777 return v;
12778 }
12779
12780 static int
12781 bkpt_probe_remove_location (struct bp_location *bl,
12782 enum remove_bp_reason reason)
12783 {
12784 /* Let's clear the semaphore before removing the location. */
12785 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
12786
12787 return bkpt_remove_location (bl, reason);
12788 }
12789
12790 static void
12791 bkpt_probe_create_sals_from_location (struct event_location *location,
12792 struct linespec_result *canonical,
12793 enum bptype type_wanted)
12794 {
12795 struct linespec_sals lsal;
12796
12797 lsal.sals = parse_probes (location, NULL, canonical);
12798 lsal.canonical
12799 = xstrdup (event_location_to_string (canonical->location.get ()));
12800 canonical->lsals.push_back (std::move (lsal));
12801 }
12802
12803 static std::vector<symtab_and_line>
12804 bkpt_probe_decode_location (struct breakpoint *b,
12805 struct event_location *location,
12806 struct program_space *search_pspace)
12807 {
12808 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12809 if (sals.empty ())
12810 error (_("probe not found"));
12811 return sals;
12812 }
12813
12814 /* The breakpoint_ops structure to be used in tracepoints. */
12815
12816 static void
12817 tracepoint_re_set (struct breakpoint *b)
12818 {
12819 breakpoint_re_set_default (b);
12820 }
12821
12822 static int
12823 tracepoint_breakpoint_hit (const struct bp_location *bl,
12824 const address_space *aspace, CORE_ADDR bp_addr,
12825 const target_waitstatus &ws)
12826 {
12827 /* By definition, the inferior does not report stops at
12828 tracepoints. */
12829 return 0;
12830 }
12831
12832 static void
12833 tracepoint_print_one_detail (const struct breakpoint *self,
12834 struct ui_out *uiout)
12835 {
12836 struct tracepoint *tp = (struct tracepoint *) self;
12837 if (!tp->static_trace_marker_id.empty ())
12838 {
12839 gdb_assert (self->type == bp_static_tracepoint);
12840
12841 uiout->message ("\tmarker id is %pF\n",
12842 string_field ("static-tracepoint-marker-string-id",
12843 tp->static_trace_marker_id.c_str ()));
12844 }
12845 }
12846
12847 static void
12848 tracepoint_print_mention (struct breakpoint *b)
12849 {
12850 if (current_uiout->is_mi_like_p ())
12851 return;
12852
12853 switch (b->type)
12854 {
12855 case bp_tracepoint:
12856 printf_filtered (_("Tracepoint"));
12857 printf_filtered (_(" %d"), b->number);
12858 break;
12859 case bp_fast_tracepoint:
12860 printf_filtered (_("Fast tracepoint"));
12861 printf_filtered (_(" %d"), b->number);
12862 break;
12863 case bp_static_tracepoint:
12864 printf_filtered (_("Static tracepoint"));
12865 printf_filtered (_(" %d"), b->number);
12866 break;
12867 default:
12868 internal_error (__FILE__, __LINE__,
12869 _("unhandled tracepoint type %d"), (int) b->type);
12870 }
12871
12872 say_where (b);
12873 }
12874
12875 static void
12876 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
12877 {
12878 struct tracepoint *tp = (struct tracepoint *) self;
12879
12880 if (self->type == bp_fast_tracepoint)
12881 fprintf_unfiltered (fp, "ftrace");
12882 else if (self->type == bp_static_tracepoint)
12883 fprintf_unfiltered (fp, "strace");
12884 else if (self->type == bp_tracepoint)
12885 fprintf_unfiltered (fp, "trace");
12886 else
12887 internal_error (__FILE__, __LINE__,
12888 _("unhandled tracepoint type %d"), (int) self->type);
12889
12890 fprintf_unfiltered (fp, " %s",
12891 event_location_to_string (self->location.get ()));
12892 print_recreate_thread (self, fp);
12893
12894 if (tp->pass_count)
12895 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
12896 }
12897
12898 static void
12899 tracepoint_create_sals_from_location (struct event_location *location,
12900 struct linespec_result *canonical,
12901 enum bptype type_wanted)
12902 {
12903 create_sals_from_location_default (location, canonical, type_wanted);
12904 }
12905
12906 static void
12907 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12908 struct linespec_result *canonical,
12909 gdb::unique_xmalloc_ptr<char> cond_string,
12910 gdb::unique_xmalloc_ptr<char> extra_string,
12911 enum bptype type_wanted,
12912 enum bpdisp disposition,
12913 int thread,
12914 int task, int ignore_count,
12915 const struct breakpoint_ops *ops,
12916 int from_tty, int enabled,
12917 int internal, unsigned flags)
12918 {
12919 create_breakpoints_sal_default (gdbarch, canonical,
12920 std::move (cond_string),
12921 std::move (extra_string),
12922 type_wanted,
12923 disposition, thread, task,
12924 ignore_count, ops, from_tty,
12925 enabled, internal, flags);
12926 }
12927
12928 static std::vector<symtab_and_line>
12929 tracepoint_decode_location (struct breakpoint *b,
12930 struct event_location *location,
12931 struct program_space *search_pspace)
12932 {
12933 return decode_location_default (b, location, search_pspace);
12934 }
12935
12936 struct breakpoint_ops tracepoint_breakpoint_ops;
12937
12938 /* Virtual table for tracepoints on static probes. */
12939
12940 static void
12941 tracepoint_probe_create_sals_from_location
12942 (struct event_location *location,
12943 struct linespec_result *canonical,
12944 enum bptype type_wanted)
12945 {
12946 /* We use the same method for breakpoint on probes. */
12947 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
12948 }
12949
12950 static std::vector<symtab_and_line>
12951 tracepoint_probe_decode_location (struct breakpoint *b,
12952 struct event_location *location,
12953 struct program_space *search_pspace)
12954 {
12955 /* We use the same method for breakpoint on probes. */
12956 return bkpt_probe_decode_location (b, location, search_pspace);
12957 }
12958
12959 /* Dprintf breakpoint_ops methods. */
12960
12961 static void
12962 dprintf_re_set (struct breakpoint *b)
12963 {
12964 breakpoint_re_set_default (b);
12965
12966 /* extra_string should never be non-NULL for dprintf. */
12967 gdb_assert (b->extra_string != NULL);
12968
12969 /* 1 - connect to target 1, that can run breakpoint commands.
12970 2 - create a dprintf, which resolves fine.
12971 3 - disconnect from target 1
12972 4 - connect to target 2, that can NOT run breakpoint commands.
12973
12974 After steps #3/#4, you'll want the dprintf command list to
12975 be updated, because target 1 and 2 may well return different
12976 answers for target_can_run_breakpoint_commands().
12977 Given absence of finer grained resetting, we get to do
12978 it all the time. */
12979 if (b->extra_string != NULL)
12980 update_dprintf_command_list (b);
12981 }
12982
12983 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
12984
12985 static void
12986 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12987 {
12988 fprintf_unfiltered (fp, "dprintf %s,%s",
12989 event_location_to_string (tp->location.get ()),
12990 tp->extra_string.get ());
12991 print_recreate_thread (tp, fp);
12992 }
12993
12994 /* Implement the "after_condition_true" breakpoint_ops method for
12995 dprintf.
12996
12997 dprintf's are implemented with regular commands in their command
12998 list, but we run the commands here instead of before presenting the
12999 stop to the user, as dprintf's don't actually cause a stop. This
13000 also makes it so that the commands of multiple dprintfs at the same
13001 address are all handled. */
13002
13003 static void
13004 dprintf_after_condition_true (struct bpstat *bs)
13005 {
13006 /* dprintf's never cause a stop. This wasn't set in the
13007 check_status hook instead because that would make the dprintf's
13008 condition not be evaluated. */
13009 bs->stop = 0;
13010
13011 /* Run the command list here. Take ownership of it instead of
13012 copying. We never want these commands to run later in
13013 bpstat_do_actions, if a breakpoint that causes a stop happens to
13014 be set at same address as this dprintf, or even if running the
13015 commands here throws. */
13016 counted_command_line cmds = std::move (bs->commands);
13017 gdb_assert (cmds != nullptr);
13018 execute_control_commands (cmds.get (), 0);
13019 }
13020
13021 /* The breakpoint_ops structure to be used on static tracepoints with
13022 markers (`-m'). */
13023
13024 static void
13025 strace_marker_create_sals_from_location (struct event_location *location,
13026 struct linespec_result *canonical,
13027 enum bptype type_wanted)
13028 {
13029 struct linespec_sals lsal;
13030 const char *arg_start, *arg;
13031
13032 arg = arg_start = get_linespec_location (location)->spec_string;
13033 lsal.sals = decode_static_tracepoint_spec (&arg);
13034
13035 std::string str (arg_start, arg - arg_start);
13036 const char *ptr = str.c_str ();
13037 canonical->location
13038 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
13039
13040 lsal.canonical
13041 = xstrdup (event_location_to_string (canonical->location.get ()));
13042 canonical->lsals.push_back (std::move (lsal));
13043 }
13044
13045 static void
13046 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13047 struct linespec_result *canonical,
13048 gdb::unique_xmalloc_ptr<char> cond_string,
13049 gdb::unique_xmalloc_ptr<char> extra_string,
13050 enum bptype type_wanted,
13051 enum bpdisp disposition,
13052 int thread,
13053 int task, int ignore_count,
13054 const struct breakpoint_ops *ops,
13055 int from_tty, int enabled,
13056 int internal, unsigned flags)
13057 {
13058 const linespec_sals &lsal = canonical->lsals[0];
13059
13060 /* If the user is creating a static tracepoint by marker id
13061 (strace -m MARKER_ID), then store the sals index, so that
13062 breakpoint_re_set can try to match up which of the newly
13063 found markers corresponds to this one, and, don't try to
13064 expand multiple locations for each sal, given than SALS
13065 already should contain all sals for MARKER_ID. */
13066
13067 for (size_t i = 0; i < lsal.sals.size (); i++)
13068 {
13069 event_location_up location
13070 = copy_event_location (canonical->location.get ());
13071
13072 std::unique_ptr<tracepoint> tp (new tracepoint ());
13073 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
13074 std::move (location), NULL,
13075 std::move (cond_string),
13076 std::move (extra_string),
13077 type_wanted, disposition,
13078 thread, task, ignore_count, ops,
13079 from_tty, enabled, internal, flags,
13080 canonical->special_display);
13081 /* Given that its possible to have multiple markers with
13082 the same string id, if the user is creating a static
13083 tracepoint by marker id ("strace -m MARKER_ID"), then
13084 store the sals index, so that breakpoint_re_set can
13085 try to match up which of the newly found markers
13086 corresponds to this one */
13087 tp->static_trace_marker_id_idx = i;
13088
13089 install_breakpoint (internal, std::move (tp), 0);
13090 }
13091 }
13092
13093 static std::vector<symtab_and_line>
13094 strace_marker_decode_location (struct breakpoint *b,
13095 struct event_location *location,
13096 struct program_space *search_pspace)
13097 {
13098 struct tracepoint *tp = (struct tracepoint *) b;
13099 const char *s = get_linespec_location (location)->spec_string;
13100
13101 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13102 if (sals.size () > tp->static_trace_marker_id_idx)
13103 {
13104 sals[0] = sals[tp->static_trace_marker_id_idx];
13105 sals.resize (1);
13106 return sals;
13107 }
13108 else
13109 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
13110 }
13111
13112 static struct breakpoint_ops strace_marker_breakpoint_ops;
13113
13114 static int
13115 strace_marker_p (struct breakpoint *b)
13116 {
13117 return b->ops == &strace_marker_breakpoint_ops;
13118 }
13119
13120 /* Delete a breakpoint and clean up all traces of it in the data
13121 structures. */
13122
13123 void
13124 delete_breakpoint (struct breakpoint *bpt)
13125 {
13126 gdb_assert (bpt != NULL);
13127
13128 /* Has this bp already been deleted? This can happen because
13129 multiple lists can hold pointers to bp's. bpstat lists are
13130 especial culprits.
13131
13132 One example of this happening is a watchpoint's scope bp. When
13133 the scope bp triggers, we notice that the watchpoint is out of
13134 scope, and delete it. We also delete its scope bp. But the
13135 scope bp is marked "auto-deleting", and is already on a bpstat.
13136 That bpstat is then checked for auto-deleting bp's, which are
13137 deleted.
13138
13139 A real solution to this problem might involve reference counts in
13140 bp's, and/or giving them pointers back to their referencing
13141 bpstat's, and teaching delete_breakpoint to only free a bp's
13142 storage when no more references were extent. A cheaper bandaid
13143 was chosen. */
13144 if (bpt->type == bp_none)
13145 return;
13146
13147 /* At least avoid this stale reference until the reference counting
13148 of breakpoints gets resolved. */
13149 if (bpt->related_breakpoint != bpt)
13150 {
13151 struct breakpoint *related;
13152 struct watchpoint *w;
13153
13154 if (bpt->type == bp_watchpoint_scope)
13155 w = (struct watchpoint *) bpt->related_breakpoint;
13156 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13157 w = (struct watchpoint *) bpt;
13158 else
13159 w = NULL;
13160 if (w != NULL)
13161 watchpoint_del_at_next_stop (w);
13162
13163 /* Unlink bpt from the bpt->related_breakpoint ring. */
13164 for (related = bpt; related->related_breakpoint != bpt;
13165 related = related->related_breakpoint);
13166 related->related_breakpoint = bpt->related_breakpoint;
13167 bpt->related_breakpoint = bpt;
13168 }
13169
13170 /* watch_command_1 creates a watchpoint but only sets its number if
13171 update_watchpoint succeeds in creating its bp_locations. If there's
13172 a problem in that process, we'll be asked to delete the half-created
13173 watchpoint. In that case, don't announce the deletion. */
13174 if (bpt->number)
13175 gdb::observers::breakpoint_deleted.notify (bpt);
13176
13177 if (breakpoint_chain == bpt)
13178 breakpoint_chain = bpt->next;
13179
13180 for (breakpoint *b : all_breakpoints ())
13181 if (b->next == bpt)
13182 {
13183 b->next = bpt->next;
13184 break;
13185 }
13186
13187 /* Be sure no bpstat's are pointing at the breakpoint after it's
13188 been freed. */
13189 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13190 in all threads for now. Note that we cannot just remove bpstats
13191 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13192 commands are associated with the bpstat; if we remove it here,
13193 then the later call to bpstat_do_actions (&stop_bpstat); in
13194 event-top.c won't do anything, and temporary breakpoints with
13195 commands won't work. */
13196
13197 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13198
13199 /* Now that breakpoint is removed from breakpoint list, update the
13200 global location list. This will remove locations that used to
13201 belong to this breakpoint. Do this before freeing the breakpoint
13202 itself, since remove_breakpoint looks at location's owner. It
13203 might be better design to have location completely
13204 self-contained, but it's not the case now. */
13205 update_global_location_list (UGLL_DONT_INSERT);
13206
13207 /* On the chance that someone will soon try again to delete this
13208 same bp, we mark it as deleted before freeing its storage. */
13209 bpt->type = bp_none;
13210 delete bpt;
13211 }
13212
13213 /* Iterator function to call a user-provided callback function once
13214 for each of B and its related breakpoints. */
13215
13216 static void
13217 iterate_over_related_breakpoints (struct breakpoint *b,
13218 gdb::function_view<void (breakpoint *)> function)
13219 {
13220 struct breakpoint *related;
13221
13222 related = b;
13223 do
13224 {
13225 struct breakpoint *next;
13226
13227 /* FUNCTION may delete RELATED. */
13228 next = related->related_breakpoint;
13229
13230 if (next == related)
13231 {
13232 /* RELATED is the last ring entry. */
13233 function (related);
13234
13235 /* FUNCTION may have deleted it, so we'd never reach back to
13236 B. There's nothing left to do anyway, so just break
13237 out. */
13238 break;
13239 }
13240 else
13241 function (related);
13242
13243 related = next;
13244 }
13245 while (related != b);
13246 }
13247
13248 static void
13249 delete_command (const char *arg, int from_tty)
13250 {
13251 dont_repeat ();
13252
13253 if (arg == 0)
13254 {
13255 int breaks_to_delete = 0;
13256
13257 /* Delete all breakpoints if no argument. Do not delete
13258 internal breakpoints, these have to be deleted with an
13259 explicit breakpoint number argument. */
13260 for (breakpoint *b : all_breakpoints ())
13261 if (user_breakpoint_p (b))
13262 {
13263 breaks_to_delete = 1;
13264 break;
13265 }
13266
13267 /* Ask user only if there are some breakpoints to delete. */
13268 if (!from_tty
13269 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13270 for (breakpoint *b : all_breakpoints_safe ())
13271 if (user_breakpoint_p (b))
13272 delete_breakpoint (b);
13273 }
13274 else
13275 map_breakpoint_numbers
13276 (arg, [&] (breakpoint *br)
13277 {
13278 iterate_over_related_breakpoints (br, delete_breakpoint);
13279 });
13280 }
13281
13282 /* Return true if all locations of B bound to PSPACE are pending. If
13283 PSPACE is NULL, all locations of all program spaces are
13284 considered. */
13285
13286 static int
13287 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13288 {
13289 for (bp_location *loc : b->locations ())
13290 if ((pspace == NULL
13291 || loc->pspace == pspace)
13292 && !loc->shlib_disabled
13293 && !loc->pspace->executing_startup)
13294 return 0;
13295 return 1;
13296 }
13297
13298 /* Subroutine of update_breakpoint_locations to simplify it.
13299 Return non-zero if multiple fns in list LOC have the same name.
13300 Null names are ignored. */
13301
13302 static int
13303 ambiguous_names_p (struct bp_location *loc)
13304 {
13305 struct bp_location *l;
13306 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
13307 xcalloc, xfree));
13308
13309 for (l = loc; l != NULL; l = l->next)
13310 {
13311 const char **slot;
13312 const char *name = l->function_name.get ();
13313
13314 /* Allow for some names to be NULL, ignore them. */
13315 if (name == NULL)
13316 continue;
13317
13318 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
13319 INSERT);
13320 /* NOTE: We can assume slot != NULL here because xcalloc never
13321 returns NULL. */
13322 if (*slot != NULL)
13323 return 1;
13324 *slot = name;
13325 }
13326
13327 return 0;
13328 }
13329
13330 /* When symbols change, it probably means the sources changed as well,
13331 and it might mean the static tracepoint markers are no longer at
13332 the same address or line numbers they used to be at last we
13333 checked. Losing your static tracepoints whenever you rebuild is
13334 undesirable. This function tries to resync/rematch gdb static
13335 tracepoints with the markers on the target, for static tracepoints
13336 that have not been set by marker id. Static tracepoint that have
13337 been set by marker id are reset by marker id in breakpoint_re_set.
13338 The heuristic is:
13339
13340 1) For a tracepoint set at a specific address, look for a marker at
13341 the old PC. If one is found there, assume to be the same marker.
13342 If the name / string id of the marker found is different from the
13343 previous known name, assume that means the user renamed the marker
13344 in the sources, and output a warning.
13345
13346 2) For a tracepoint set at a given line number, look for a marker
13347 at the new address of the old line number. If one is found there,
13348 assume to be the same marker. If the name / string id of the
13349 marker found is different from the previous known name, assume that
13350 means the user renamed the marker in the sources, and output a
13351 warning.
13352
13353 3) If a marker is no longer found at the same address or line, it
13354 may mean the marker no longer exists. But it may also just mean
13355 the code changed a bit. Maybe the user added a few lines of code
13356 that made the marker move up or down (in line number terms). Ask
13357 the target for info about the marker with the string id as we knew
13358 it. If found, update line number and address in the matching
13359 static tracepoint. This will get confused if there's more than one
13360 marker with the same ID (possible in UST, although unadvised
13361 precisely because it confuses tools). */
13362
13363 static struct symtab_and_line
13364 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13365 {
13366 struct tracepoint *tp = (struct tracepoint *) b;
13367 struct static_tracepoint_marker marker;
13368 CORE_ADDR pc;
13369
13370 pc = sal.pc;
13371 if (sal.line)
13372 find_line_pc (sal.symtab, sal.line, &pc);
13373
13374 if (target_static_tracepoint_marker_at (pc, &marker))
13375 {
13376 if (tp->static_trace_marker_id != marker.str_id)
13377 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13378 b->number, tp->static_trace_marker_id.c_str (),
13379 marker.str_id.c_str ());
13380
13381 tp->static_trace_marker_id = std::move (marker.str_id);
13382
13383 return sal;
13384 }
13385
13386 /* Old marker wasn't found on target at lineno. Try looking it up
13387 by string ID. */
13388 if (!sal.explicit_pc
13389 && sal.line != 0
13390 && sal.symtab != NULL
13391 && !tp->static_trace_marker_id.empty ())
13392 {
13393 std::vector<static_tracepoint_marker> markers
13394 = target_static_tracepoint_markers_by_strid
13395 (tp->static_trace_marker_id.c_str ());
13396
13397 if (!markers.empty ())
13398 {
13399 struct symbol *sym;
13400 struct static_tracepoint_marker *tpmarker;
13401 struct ui_out *uiout = current_uiout;
13402 struct explicit_location explicit_loc;
13403
13404 tpmarker = &markers[0];
13405
13406 tp->static_trace_marker_id = std::move (tpmarker->str_id);
13407
13408 warning (_("marker for static tracepoint %d (%s) not "
13409 "found at previous line number"),
13410 b->number, tp->static_trace_marker_id.c_str ());
13411
13412 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
13413 sym = find_pc_sect_function (tpmarker->address, NULL);
13414 uiout->text ("Now in ");
13415 if (sym)
13416 {
13417 uiout->field_string ("func", sym->print_name (),
13418 function_name_style.style ());
13419 uiout->text (" at ");
13420 }
13421 uiout->field_string ("file",
13422 symtab_to_filename_for_display (sal2.symtab),
13423 file_name_style.style ());
13424 uiout->text (":");
13425
13426 if (uiout->is_mi_like_p ())
13427 {
13428 const char *fullname = symtab_to_fullname (sal2.symtab);
13429
13430 uiout->field_string ("fullname", fullname);
13431 }
13432
13433 uiout->field_signed ("line", sal2.line);
13434 uiout->text ("\n");
13435
13436 b->loc->line_number = sal2.line;
13437 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13438
13439 b->location.reset (NULL);
13440 initialize_explicit_location (&explicit_loc);
13441 explicit_loc.source_filename
13442 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13443 explicit_loc.line_offset.offset = b->loc->line_number;
13444 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13445 b->location = new_explicit_location (&explicit_loc);
13446
13447 /* Might be nice to check if function changed, and warn if
13448 so. */
13449 }
13450 }
13451 return sal;
13452 }
13453
13454 /* Returns 1 iff locations A and B are sufficiently same that
13455 we don't need to report breakpoint as changed. */
13456
13457 static int
13458 locations_are_equal (struct bp_location *a, struct bp_location *b)
13459 {
13460 while (a && b)
13461 {
13462 if (a->address != b->address)
13463 return 0;
13464
13465 if (a->shlib_disabled != b->shlib_disabled)
13466 return 0;
13467
13468 if (a->enabled != b->enabled)
13469 return 0;
13470
13471 if (a->disabled_by_cond != b->disabled_by_cond)
13472 return 0;
13473
13474 a = a->next;
13475 b = b->next;
13476 }
13477
13478 if ((a == NULL) != (b == NULL))
13479 return 0;
13480
13481 return 1;
13482 }
13483
13484 /* Split all locations of B that are bound to PSPACE out of B's
13485 location list to a separate list and return that list's head. If
13486 PSPACE is NULL, hoist out all locations of B. */
13487
13488 static struct bp_location *
13489 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13490 {
13491 struct bp_location head;
13492 struct bp_location *i = b->loc;
13493 struct bp_location **i_link = &b->loc;
13494 struct bp_location *hoisted = &head;
13495
13496 if (pspace == NULL)
13497 {
13498 i = b->loc;
13499 b->loc = NULL;
13500 return i;
13501 }
13502
13503 head.next = NULL;
13504
13505 while (i != NULL)
13506 {
13507 if (i->pspace == pspace)
13508 {
13509 *i_link = i->next;
13510 i->next = NULL;
13511 hoisted->next = i;
13512 hoisted = i;
13513 }
13514 else
13515 i_link = &i->next;
13516 i = *i_link;
13517 }
13518
13519 return head.next;
13520 }
13521
13522 /* Create new breakpoint locations for B (a hardware or software
13523 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13524 zero, then B is a ranged breakpoint. Only recreates locations for
13525 FILTER_PSPACE. Locations of other program spaces are left
13526 untouched. */
13527
13528 void
13529 update_breakpoint_locations (struct breakpoint *b,
13530 struct program_space *filter_pspace,
13531 gdb::array_view<const symtab_and_line> sals,
13532 gdb::array_view<const symtab_and_line> sals_end)
13533 {
13534 struct bp_location *existing_locations;
13535
13536 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
13537 {
13538 /* Ranged breakpoints have only one start location and one end
13539 location. */
13540 b->enable_state = bp_disabled;
13541 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13542 "multiple locations found\n"),
13543 b->number);
13544 return;
13545 }
13546
13547 /* If there's no new locations, and all existing locations are
13548 pending, don't do anything. This optimizes the common case where
13549 all locations are in the same shared library, that was unloaded.
13550 We'd like to retain the location, so that when the library is
13551 loaded again, we don't loose the enabled/disabled status of the
13552 individual locations. */
13553 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
13554 return;
13555
13556 existing_locations = hoist_existing_locations (b, filter_pspace);
13557
13558 for (const auto &sal : sals)
13559 {
13560 struct bp_location *new_loc;
13561
13562 switch_to_program_space_and_thread (sal.pspace);
13563
13564 new_loc = add_location_to_breakpoint (b, &sal);
13565
13566 /* Reparse conditions, they might contain references to the
13567 old symtab. */
13568 if (b->cond_string != NULL)
13569 {
13570 const char *s;
13571
13572 s = b->cond_string.get ();
13573 try
13574 {
13575 new_loc->cond = parse_exp_1 (&s, sal.pc,
13576 block_for_pc (sal.pc),
13577 0);
13578 }
13579 catch (const gdb_exception_error &e)
13580 {
13581 new_loc->disabled_by_cond = true;
13582 }
13583 }
13584
13585 if (!sals_end.empty ())
13586 {
13587 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13588
13589 new_loc->length = end - sals[0].pc + 1;
13590 }
13591 }
13592
13593 /* If possible, carry over 'disable' status from existing
13594 breakpoints. */
13595 {
13596 struct bp_location *e = existing_locations;
13597 /* If there are multiple breakpoints with the same function name,
13598 e.g. for inline functions, comparing function names won't work.
13599 Instead compare pc addresses; this is just a heuristic as things
13600 may have moved, but in practice it gives the correct answer
13601 often enough until a better solution is found. */
13602 int have_ambiguous_names = ambiguous_names_p (b->loc);
13603
13604 for (; e; e = e->next)
13605 {
13606 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
13607 {
13608 if (have_ambiguous_names)
13609 {
13610 for (bp_location *l : b->locations ())
13611 {
13612 /* Ignore software vs hardware location type at
13613 this point, because with "set breakpoint
13614 auto-hw", after a re-set, locations that were
13615 hardware can end up as software, or vice versa.
13616 As mentioned above, this is an heuristic and in
13617 practice should give the correct answer often
13618 enough. */
13619 if (breakpoint_locations_match (e, l, true))
13620 {
13621 l->enabled = e->enabled;
13622 l->disabled_by_cond = e->disabled_by_cond;
13623 break;
13624 }
13625 }
13626 }
13627 else
13628 {
13629 for (bp_location *l : b->locations ())
13630 if (l->function_name
13631 && strcmp (e->function_name.get (),
13632 l->function_name.get ()) == 0)
13633 {
13634 l->enabled = e->enabled;
13635 l->disabled_by_cond = e->disabled_by_cond;
13636 break;
13637 }
13638 }
13639 }
13640 }
13641 }
13642
13643 if (!locations_are_equal (existing_locations, b->loc))
13644 gdb::observers::breakpoint_modified.notify (b);
13645 }
13646
13647 /* Find the SaL locations corresponding to the given LOCATION.
13648 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13649
13650 static std::vector<symtab_and_line>
13651 location_to_sals (struct breakpoint *b, struct event_location *location,
13652 struct program_space *search_pspace, int *found)
13653 {
13654 struct gdb_exception exception;
13655
13656 gdb_assert (b->ops != NULL);
13657
13658 std::vector<symtab_and_line> sals;
13659
13660 try
13661 {
13662 sals = b->ops->decode_location (b, location, search_pspace);
13663 }
13664 catch (gdb_exception_error &e)
13665 {
13666 int not_found_and_ok = 0;
13667
13668 /* For pending breakpoints, it's expected that parsing will
13669 fail until the right shared library is loaded. User has
13670 already told to create pending breakpoints and don't need
13671 extra messages. If breakpoint is in bp_shlib_disabled
13672 state, then user already saw the message about that
13673 breakpoint being disabled, and don't want to see more
13674 errors. */
13675 if (e.error == NOT_FOUND_ERROR
13676 && (b->condition_not_parsed
13677 || (b->loc != NULL
13678 && search_pspace != NULL
13679 && b->loc->pspace != search_pspace)
13680 || (b->loc && b->loc->shlib_disabled)
13681 || (b->loc && b->loc->pspace->executing_startup)
13682 || b->enable_state == bp_disabled))
13683 not_found_and_ok = 1;
13684
13685 if (!not_found_and_ok)
13686 {
13687 /* We surely don't want to warn about the same breakpoint
13688 10 times. One solution, implemented here, is disable
13689 the breakpoint on error. Another solution would be to
13690 have separate 'warning emitted' flag. Since this
13691 happens only when a binary has changed, I don't know
13692 which approach is better. */
13693 b->enable_state = bp_disabled;
13694 throw;
13695 }
13696
13697 exception = std::move (e);
13698 }
13699
13700 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13701 {
13702 for (auto &sal : sals)
13703 resolve_sal_pc (&sal);
13704 if (b->condition_not_parsed && b->extra_string != NULL)
13705 {
13706 gdb::unique_xmalloc_ptr<char> cond_string, extra_string;
13707 int thread, task;
13708
13709 find_condition_and_thread_for_sals (sals, b->extra_string.get (),
13710 &cond_string, &thread,
13711 &task, &extra_string);
13712 gdb_assert (b->cond_string == NULL);
13713 if (cond_string)
13714 b->cond_string = std::move (cond_string);
13715 b->thread = thread;
13716 b->task = task;
13717 if (extra_string)
13718 b->extra_string = std::move (extra_string);
13719 b->condition_not_parsed = 0;
13720 }
13721
13722 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13723 sals[0] = update_static_tracepoint (b, sals[0]);
13724
13725 *found = 1;
13726 }
13727 else
13728 *found = 0;
13729
13730 return sals;
13731 }
13732
13733 /* The default re_set method, for typical hardware or software
13734 breakpoints. Reevaluate the breakpoint and recreate its
13735 locations. */
13736
13737 static void
13738 breakpoint_re_set_default (struct breakpoint *b)
13739 {
13740 struct program_space *filter_pspace = current_program_space;
13741 std::vector<symtab_and_line> expanded, expanded_end;
13742
13743 int found;
13744 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13745 filter_pspace, &found);
13746 if (found)
13747 expanded = std::move (sals);
13748
13749 if (b->location_range_end != NULL)
13750 {
13751 std::vector<symtab_and_line> sals_end
13752 = location_to_sals (b, b->location_range_end.get (),
13753 filter_pspace, &found);
13754 if (found)
13755 expanded_end = std::move (sals_end);
13756 }
13757
13758 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
13759 }
13760
13761 /* Default method for creating SALs from an address string. It basically
13762 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13763
13764 static void
13765 create_sals_from_location_default (struct event_location *location,
13766 struct linespec_result *canonical,
13767 enum bptype type_wanted)
13768 {
13769 parse_breakpoint_sals (location, canonical);
13770 }
13771
13772 /* Call create_breakpoints_sal for the given arguments. This is the default
13773 function for the `create_breakpoints_sal' method of
13774 breakpoint_ops. */
13775
13776 static void
13777 create_breakpoints_sal_default (struct gdbarch *gdbarch,
13778 struct linespec_result *canonical,
13779 gdb::unique_xmalloc_ptr<char> cond_string,
13780 gdb::unique_xmalloc_ptr<char> extra_string,
13781 enum bptype type_wanted,
13782 enum bpdisp disposition,
13783 int thread,
13784 int task, int ignore_count,
13785 const struct breakpoint_ops *ops,
13786 int from_tty, int enabled,
13787 int internal, unsigned flags)
13788 {
13789 create_breakpoints_sal (gdbarch, canonical,
13790 std::move (cond_string),
13791 std::move (extra_string),
13792 type_wanted, disposition,
13793 thread, task, ignore_count, ops, from_tty,
13794 enabled, internal, flags);
13795 }
13796
13797 /* Decode the line represented by S by calling decode_line_full. This is the
13798 default function for the `decode_location' method of breakpoint_ops. */
13799
13800 static std::vector<symtab_and_line>
13801 decode_location_default (struct breakpoint *b,
13802 struct event_location *location,
13803 struct program_space *search_pspace)
13804 {
13805 struct linespec_result canonical;
13806
13807 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
13808 NULL, 0, &canonical, multiple_symbols_all,
13809 b->filter.get ());
13810
13811 /* We should get 0 or 1 resulting SALs. */
13812 gdb_assert (canonical.lsals.size () < 2);
13813
13814 if (!canonical.lsals.empty ())
13815 {
13816 const linespec_sals &lsal = canonical.lsals[0];
13817 return std::move (lsal.sals);
13818 }
13819 return {};
13820 }
13821
13822 /* Reset a breakpoint. */
13823
13824 static void
13825 breakpoint_re_set_one (breakpoint *b)
13826 {
13827 input_radix = b->input_radix;
13828 set_language (b->language);
13829
13830 b->ops->re_set (b);
13831 }
13832
13833 /* Re-set breakpoint locations for the current program space.
13834 Locations bound to other program spaces are left untouched. */
13835
13836 void
13837 breakpoint_re_set (void)
13838 {
13839 {
13840 scoped_restore_current_language save_language;
13841 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
13842 scoped_restore_current_pspace_and_thread restore_pspace_thread;
13843
13844 /* breakpoint_re_set_one sets the current_language to the language
13845 of the breakpoint it is resetting (see prepare_re_set_context)
13846 before re-evaluating the breakpoint's location. This change can
13847 unfortunately get undone by accident if the language_mode is set
13848 to auto, and we either switch frames, or more likely in this context,
13849 we select the current frame.
13850
13851 We prevent this by temporarily turning the language_mode to
13852 language_mode_manual. We restore it once all breakpoints
13853 have been reset. */
13854 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13855 language_mode = language_mode_manual;
13856
13857 /* Note: we must not try to insert locations until after all
13858 breakpoints have been re-set. Otherwise, e.g., when re-setting
13859 breakpoint 1, we'd insert the locations of breakpoint 2, which
13860 hadn't been re-set yet, and thus may have stale locations. */
13861
13862 for (breakpoint *b : all_breakpoints_safe ())
13863 {
13864 try
13865 {
13866 breakpoint_re_set_one (b);
13867 }
13868 catch (const gdb_exception &ex)
13869 {
13870 exception_fprintf (gdb_stderr, ex,
13871 "Error in re-setting breakpoint %d: ",
13872 b->number);
13873 }
13874 }
13875
13876 jit_breakpoint_re_set ();
13877 }
13878
13879 create_overlay_event_breakpoint ();
13880 create_longjmp_master_breakpoint ();
13881 create_std_terminate_master_breakpoint ();
13882 create_exception_master_breakpoint ();
13883
13884 /* Now we can insert. */
13885 update_global_location_list (UGLL_MAY_INSERT);
13886 }
13887 \f
13888 /* Reset the thread number of this breakpoint:
13889
13890 - If the breakpoint is for all threads, leave it as-is.
13891 - Else, reset it to the current thread for inferior_ptid. */
13892 void
13893 breakpoint_re_set_thread (struct breakpoint *b)
13894 {
13895 if (b->thread != -1)
13896 {
13897 b->thread = inferior_thread ()->global_num;
13898
13899 /* We're being called after following a fork. The new fork is
13900 selected as current, and unless this was a vfork will have a
13901 different program space from the original thread. Reset that
13902 as well. */
13903 b->loc->pspace = current_program_space;
13904 }
13905 }
13906
13907 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13908 If from_tty is nonzero, it prints a message to that effect,
13909 which ends with a period (no newline). */
13910
13911 void
13912 set_ignore_count (int bptnum, int count, int from_tty)
13913 {
13914 if (count < 0)
13915 count = 0;
13916
13917 for (breakpoint *b : all_breakpoints ())
13918 if (b->number == bptnum)
13919 {
13920 if (is_tracepoint (b))
13921 {
13922 if (from_tty && count != 0)
13923 printf_filtered (_("Ignore count ignored for tracepoint %d."),
13924 bptnum);
13925 return;
13926 }
13927
13928 b->ignore_count = count;
13929 if (from_tty)
13930 {
13931 if (count == 0)
13932 printf_filtered (_("Will stop next time "
13933 "breakpoint %d is reached."),
13934 bptnum);
13935 else if (count == 1)
13936 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
13937 bptnum);
13938 else
13939 printf_filtered (_("Will ignore next %d "
13940 "crossings of breakpoint %d."),
13941 count, bptnum);
13942 }
13943 gdb::observers::breakpoint_modified.notify (b);
13944 return;
13945 }
13946
13947 error (_("No breakpoint number %d."), bptnum);
13948 }
13949
13950 /* Command to set ignore-count of breakpoint N to COUNT. */
13951
13952 static void
13953 ignore_command (const char *args, int from_tty)
13954 {
13955 const char *p = args;
13956 int num;
13957
13958 if (p == 0)
13959 error_no_arg (_("a breakpoint number"));
13960
13961 num = get_number (&p);
13962 if (num == 0)
13963 error (_("bad breakpoint number: '%s'"), args);
13964 if (*p == 0)
13965 error (_("Second argument (specified ignore-count) is missing."));
13966
13967 set_ignore_count (num,
13968 longest_to_int (value_as_long (parse_and_eval (p))),
13969 from_tty);
13970 if (from_tty)
13971 printf_filtered ("\n");
13972 }
13973 \f
13974
13975 /* Call FUNCTION on each of the breakpoints with numbers in the range
13976 defined by BP_NUM_RANGE (an inclusive range). */
13977
13978 static void
13979 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13980 gdb::function_view<void (breakpoint *)> function)
13981 {
13982 if (bp_num_range.first == 0)
13983 {
13984 warning (_("bad breakpoint number at or near '%d'"),
13985 bp_num_range.first);
13986 }
13987 else
13988 {
13989 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
13990 {
13991 bool match = false;
13992
13993 for (breakpoint *b : all_breakpoints_safe ())
13994 if (b->number == i)
13995 {
13996 match = true;
13997 function (b);
13998 break;
13999 }
14000 if (!match)
14001 printf_unfiltered (_("No breakpoint number %d.\n"), i);
14002 }
14003 }
14004 }
14005
14006 /* Call FUNCTION on each of the breakpoints whose numbers are given in
14007 ARGS. */
14008
14009 static void
14010 map_breakpoint_numbers (const char *args,
14011 gdb::function_view<void (breakpoint *)> function)
14012 {
14013 if (args == NULL || *args == '\0')
14014 error_no_arg (_("one or more breakpoint numbers"));
14015
14016 number_or_range_parser parser (args);
14017
14018 while (!parser.finished ())
14019 {
14020 int num = parser.get_number ();
14021 map_breakpoint_number_range (std::make_pair (num, num), function);
14022 }
14023 }
14024
14025 /* Return the breakpoint location structure corresponding to the
14026 BP_NUM and LOC_NUM values. */
14027
14028 static struct bp_location *
14029 find_location_by_number (int bp_num, int loc_num)
14030 {
14031 breakpoint *b = get_breakpoint (bp_num);
14032
14033 if (!b || b->number != bp_num)
14034 error (_("Bad breakpoint number '%d'"), bp_num);
14035
14036 if (loc_num == 0)
14037 error (_("Bad breakpoint location number '%d'"), loc_num);
14038
14039 int n = 0;
14040 for (bp_location *loc : b->locations ())
14041 if (++n == loc_num)
14042 return loc;
14043
14044 error (_("Bad breakpoint location number '%d'"), loc_num);
14045 }
14046
14047 /* Modes of operation for extract_bp_num. */
14048 enum class extract_bp_kind
14049 {
14050 /* Extracting a breakpoint number. */
14051 bp,
14052
14053 /* Extracting a location number. */
14054 loc,
14055 };
14056
14057 /* Extract a breakpoint or location number (as determined by KIND)
14058 from the string starting at START. TRAILER is a character which
14059 can be found after the number. If you don't want a trailer, use
14060 '\0'. If END_OUT is not NULL, it is set to point after the parsed
14061 string. This always returns a positive integer. */
14062
14063 static int
14064 extract_bp_num (extract_bp_kind kind, const char *start,
14065 int trailer, const char **end_out = NULL)
14066 {
14067 const char *end = start;
14068 int num = get_number_trailer (&end, trailer);
14069 if (num < 0)
14070 error (kind == extract_bp_kind::bp
14071 ? _("Negative breakpoint number '%.*s'")
14072 : _("Negative breakpoint location number '%.*s'"),
14073 int (end - start), start);
14074 if (num == 0)
14075 error (kind == extract_bp_kind::bp
14076 ? _("Bad breakpoint number '%.*s'")
14077 : _("Bad breakpoint location number '%.*s'"),
14078 int (end - start), start);
14079
14080 if (end_out != NULL)
14081 *end_out = end;
14082 return num;
14083 }
14084
14085 /* Extract a breakpoint or location range (as determined by KIND) in
14086 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14087 representing the (inclusive) range. The returned pair's elements
14088 are always positive integers. */
14089
14090 static std::pair<int, int>
14091 extract_bp_or_bp_range (extract_bp_kind kind,
14092 const std::string &arg,
14093 std::string::size_type arg_offset)
14094 {
14095 std::pair<int, int> range;
14096 const char *bp_loc = &arg[arg_offset];
14097 std::string::size_type dash = arg.find ('-', arg_offset);
14098 if (dash != std::string::npos)
14099 {
14100 /* bp_loc is a range (x-z). */
14101 if (arg.length () == dash + 1)
14102 error (kind == extract_bp_kind::bp
14103 ? _("Bad breakpoint number at or near: '%s'")
14104 : _("Bad breakpoint location number at or near: '%s'"),
14105 bp_loc);
14106
14107 const char *end;
14108 const char *start_first = bp_loc;
14109 const char *start_second = &arg[dash + 1];
14110 range.first = extract_bp_num (kind, start_first, '-');
14111 range.second = extract_bp_num (kind, start_second, '\0', &end);
14112
14113 if (range.first > range.second)
14114 error (kind == extract_bp_kind::bp
14115 ? _("Inverted breakpoint range at '%.*s'")
14116 : _("Inverted breakpoint location range at '%.*s'"),
14117 int (end - start_first), start_first);
14118 }
14119 else
14120 {
14121 /* bp_loc is a single value. */
14122 range.first = extract_bp_num (kind, bp_loc, '\0');
14123 range.second = range.first;
14124 }
14125 return range;
14126 }
14127
14128 /* Extract the breakpoint/location range specified by ARG. Returns
14129 the breakpoint range in BP_NUM_RANGE, and the location range in
14130 BP_LOC_RANGE.
14131
14132 ARG may be in any of the following forms:
14133
14134 x where 'x' is a breakpoint number.
14135 x-y where 'x' and 'y' specify a breakpoint numbers range.
14136 x.y where 'x' is a breakpoint number and 'y' a location number.
14137 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14138 location number range.
14139 */
14140
14141 static void
14142 extract_bp_number_and_location (const std::string &arg,
14143 std::pair<int, int> &bp_num_range,
14144 std::pair<int, int> &bp_loc_range)
14145 {
14146 std::string::size_type dot = arg.find ('.');
14147
14148 if (dot != std::string::npos)
14149 {
14150 /* Handle 'x.y' and 'x.y-z' cases. */
14151
14152 if (arg.length () == dot + 1 || dot == 0)
14153 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
14154
14155 bp_num_range.first
14156 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14157 bp_num_range.second = bp_num_range.first;
14158
14159 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14160 arg, dot + 1);
14161 }
14162 else
14163 {
14164 /* Handle x and x-y cases. */
14165
14166 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
14167 bp_loc_range.first = 0;
14168 bp_loc_range.second = 0;
14169 }
14170 }
14171
14172 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14173 specifies whether to enable or disable. */
14174
14175 static void
14176 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14177 {
14178 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14179 if (loc != NULL)
14180 {
14181 if (loc->disabled_by_cond && enable)
14182 error (_("Breakpoint %d's condition is invalid at location %d, "
14183 "cannot enable."), bp_num, loc_num);
14184
14185 if (loc->enabled != enable)
14186 {
14187 loc->enabled = enable;
14188 mark_breakpoint_location_modified (loc);
14189 }
14190 if (target_supports_enable_disable_tracepoint ()
14191 && current_trace_status ()->running && loc->owner
14192 && is_tracepoint (loc->owner))
14193 target_disable_tracepoint (loc);
14194 }
14195 update_global_location_list (UGLL_DONT_INSERT);
14196
14197 gdb::observers::breakpoint_modified.notify (loc->owner);
14198 }
14199
14200 /* Enable or disable a range of breakpoint locations. BP_NUM is the
14201 number of the breakpoint, and BP_LOC_RANGE specifies the
14202 (inclusive) range of location numbers of that breakpoint to
14203 enable/disable. ENABLE specifies whether to enable or disable the
14204 location. */
14205
14206 static void
14207 enable_disable_breakpoint_location_range (int bp_num,
14208 std::pair<int, int> &bp_loc_range,
14209 bool enable)
14210 {
14211 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14212 enable_disable_bp_num_loc (bp_num, i, enable);
14213 }
14214
14215 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14216 If from_tty is nonzero, it prints a message to that effect,
14217 which ends with a period (no newline). */
14218
14219 void
14220 disable_breakpoint (struct breakpoint *bpt)
14221 {
14222 /* Never disable a watchpoint scope breakpoint; we want to
14223 hit them when we leave scope so we can delete both the
14224 watchpoint and its scope breakpoint at that time. */
14225 if (bpt->type == bp_watchpoint_scope)
14226 return;
14227
14228 bpt->enable_state = bp_disabled;
14229
14230 /* Mark breakpoint locations modified. */
14231 mark_breakpoint_modified (bpt);
14232
14233 if (target_supports_enable_disable_tracepoint ()
14234 && current_trace_status ()->running && is_tracepoint (bpt))
14235 {
14236 for (bp_location *location : bpt->locations ())
14237 target_disable_tracepoint (location);
14238 }
14239
14240 update_global_location_list (UGLL_DONT_INSERT);
14241
14242 gdb::observers::breakpoint_modified.notify (bpt);
14243 }
14244
14245 /* Enable or disable the breakpoint(s) or breakpoint location(s)
14246 specified in ARGS. ARGS may be in any of the formats handled by
14247 extract_bp_number_and_location. ENABLE specifies whether to enable
14248 or disable the breakpoints/locations. */
14249
14250 static void
14251 enable_disable_command (const char *args, int from_tty, bool enable)
14252 {
14253 if (args == 0)
14254 {
14255 for (breakpoint *bpt : all_breakpoints ())
14256 if (user_breakpoint_p (bpt))
14257 {
14258 if (enable)
14259 enable_breakpoint (bpt);
14260 else
14261 disable_breakpoint (bpt);
14262 }
14263 }
14264 else
14265 {
14266 std::string num = extract_arg (&args);
14267
14268 while (!num.empty ())
14269 {
14270 std::pair<int, int> bp_num_range, bp_loc_range;
14271
14272 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14273
14274 if (bp_loc_range.first == bp_loc_range.second
14275 && bp_loc_range.first == 0)
14276 {
14277 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14278 map_breakpoint_number_range (bp_num_range,
14279 enable
14280 ? enable_breakpoint
14281 : disable_breakpoint);
14282 }
14283 else
14284 {
14285 /* Handle breakpoint ids with formats 'x.y' or
14286 'x.y-z'. */
14287 enable_disable_breakpoint_location_range
14288 (bp_num_range.first, bp_loc_range, enable);
14289 }
14290 num = extract_arg (&args);
14291 }
14292 }
14293 }
14294
14295 /* The disable command disables the specified breakpoints/locations
14296 (or all defined breakpoints) so they're no longer effective in
14297 stopping the inferior. ARGS may be in any of the forms defined in
14298 extract_bp_number_and_location. */
14299
14300 static void
14301 disable_command (const char *args, int from_tty)
14302 {
14303 enable_disable_command (args, from_tty, false);
14304 }
14305
14306 static void
14307 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14308 int count)
14309 {
14310 int target_resources_ok;
14311
14312 if (bpt->type == bp_hardware_breakpoint)
14313 {
14314 int i;
14315 i = hw_breakpoint_used_count ();
14316 target_resources_ok =
14317 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14318 i + 1, 0);
14319 if (target_resources_ok == 0)
14320 error (_("No hardware breakpoint support in the target."));
14321 else if (target_resources_ok < 0)
14322 error (_("Hardware breakpoints used exceeds limit."));
14323 }
14324
14325 if (is_watchpoint (bpt))
14326 {
14327 /* Initialize it just to avoid a GCC false warning. */
14328 enum enable_state orig_enable_state = bp_disabled;
14329
14330 try
14331 {
14332 struct watchpoint *w = (struct watchpoint *) bpt;
14333
14334 orig_enable_state = bpt->enable_state;
14335 bpt->enable_state = bp_enabled;
14336 update_watchpoint (w, 1 /* reparse */);
14337 }
14338 catch (const gdb_exception &e)
14339 {
14340 bpt->enable_state = orig_enable_state;
14341 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14342 bpt->number);
14343 return;
14344 }
14345 }
14346
14347 bpt->enable_state = bp_enabled;
14348
14349 /* Mark breakpoint locations modified. */
14350 mark_breakpoint_modified (bpt);
14351
14352 if (target_supports_enable_disable_tracepoint ()
14353 && current_trace_status ()->running && is_tracepoint (bpt))
14354 {
14355 for (bp_location *location : bpt->locations ())
14356 target_enable_tracepoint (location);
14357 }
14358
14359 bpt->disposition = disposition;
14360 bpt->enable_count = count;
14361 update_global_location_list (UGLL_MAY_INSERT);
14362
14363 gdb::observers::breakpoint_modified.notify (bpt);
14364 }
14365
14366
14367 void
14368 enable_breakpoint (struct breakpoint *bpt)
14369 {
14370 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14371 }
14372
14373 /* The enable command enables the specified breakpoints/locations (or
14374 all defined breakpoints) so they once again become (or continue to
14375 be) effective in stopping the inferior. ARGS may be in any of the
14376 forms defined in extract_bp_number_and_location. */
14377
14378 static void
14379 enable_command (const char *args, int from_tty)
14380 {
14381 enable_disable_command (args, from_tty, true);
14382 }
14383
14384 static void
14385 enable_once_command (const char *args, int from_tty)
14386 {
14387 map_breakpoint_numbers
14388 (args, [&] (breakpoint *b)
14389 {
14390 iterate_over_related_breakpoints
14391 (b, [&] (breakpoint *bpt)
14392 {
14393 enable_breakpoint_disp (bpt, disp_disable, 1);
14394 });
14395 });
14396 }
14397
14398 static void
14399 enable_count_command (const char *args, int from_tty)
14400 {
14401 int count;
14402
14403 if (args == NULL)
14404 error_no_arg (_("hit count"));
14405
14406 count = get_number (&args);
14407
14408 map_breakpoint_numbers
14409 (args, [&] (breakpoint *b)
14410 {
14411 iterate_over_related_breakpoints
14412 (b, [&] (breakpoint *bpt)
14413 {
14414 enable_breakpoint_disp (bpt, disp_disable, count);
14415 });
14416 });
14417 }
14418
14419 static void
14420 enable_delete_command (const char *args, int from_tty)
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_del, 1);
14429 });
14430 });
14431 }
14432 \f
14433 /* Invalidate last known value of any hardware watchpoint if
14434 the memory which that value represents has been written to by
14435 GDB itself. */
14436
14437 static void
14438 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14439 CORE_ADDR addr, ssize_t len,
14440 const bfd_byte *data)
14441 {
14442 for (breakpoint *bp : all_breakpoints ())
14443 if (bp->enable_state == bp_enabled
14444 && bp->type == bp_hardware_watchpoint)
14445 {
14446 struct watchpoint *wp = (struct watchpoint *) bp;
14447
14448 if (wp->val_valid && wp->val != nullptr)
14449 {
14450 for (bp_location *loc : bp->locations ())
14451 if (loc->loc_type == bp_loc_hardware_watchpoint
14452 && loc->address + loc->length > addr
14453 && addr + len > loc->address)
14454 {
14455 wp->val = NULL;
14456 wp->val_valid = false;
14457 }
14458 }
14459 }
14460 }
14461
14462 /* Create and insert a breakpoint for software single step. */
14463
14464 void
14465 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14466 const address_space *aspace,
14467 CORE_ADDR next_pc)
14468 {
14469 struct thread_info *tp = inferior_thread ();
14470 struct symtab_and_line sal;
14471 CORE_ADDR pc = next_pc;
14472
14473 if (tp->control.single_step_breakpoints == NULL)
14474 {
14475 tp->control.single_step_breakpoints
14476 = new_single_step_breakpoint (tp->global_num, gdbarch);
14477 }
14478
14479 sal = find_pc_line (pc, 0);
14480 sal.pc = pc;
14481 sal.section = find_pc_overlay (pc);
14482 sal.explicit_pc = 1;
14483 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14484
14485 update_global_location_list (UGLL_INSERT);
14486 }
14487
14488 /* Insert single step breakpoints according to the current state. */
14489
14490 int
14491 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14492 {
14493 struct regcache *regcache = get_current_regcache ();
14494 std::vector<CORE_ADDR> next_pcs;
14495
14496 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14497
14498 if (!next_pcs.empty ())
14499 {
14500 struct frame_info *frame = get_current_frame ();
14501 const address_space *aspace = get_frame_address_space (frame);
14502
14503 for (CORE_ADDR pc : next_pcs)
14504 insert_single_step_breakpoint (gdbarch, aspace, pc);
14505
14506 return 1;
14507 }
14508 else
14509 return 0;
14510 }
14511
14512 /* See breakpoint.h. */
14513
14514 int
14515 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14516 const address_space *aspace,
14517 CORE_ADDR pc)
14518 {
14519 for (bp_location *loc : bp->locations ())
14520 if (loc->inserted
14521 && breakpoint_location_address_match (loc, aspace, pc))
14522 return 1;
14523
14524 return 0;
14525 }
14526
14527 /* Check whether a software single-step breakpoint is inserted at
14528 PC. */
14529
14530 int
14531 single_step_breakpoint_inserted_here_p (const address_space *aspace,
14532 CORE_ADDR pc)
14533 {
14534 for (breakpoint *bpt : all_breakpoints ())
14535 {
14536 if (bpt->type == bp_single_step
14537 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14538 return 1;
14539 }
14540 return 0;
14541 }
14542
14543 /* Tracepoint-specific operations. */
14544
14545 /* Set tracepoint count to NUM. */
14546 static void
14547 set_tracepoint_count (int num)
14548 {
14549 tracepoint_count = num;
14550 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14551 }
14552
14553 static void
14554 trace_command (const char *arg, int from_tty)
14555 {
14556 event_location_up location = string_to_event_location (&arg,
14557 current_language);
14558 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
14559 (location.get (), true /* is_tracepoint */);
14560
14561 create_breakpoint (get_current_arch (),
14562 location.get (),
14563 NULL, 0, arg, false, 1 /* parse arg */,
14564 0 /* tempflag */,
14565 bp_tracepoint /* type_wanted */,
14566 0 /* Ignore count */,
14567 pending_break_support,
14568 ops,
14569 from_tty,
14570 1 /* enabled */,
14571 0 /* internal */, 0);
14572 }
14573
14574 static void
14575 ftrace_command (const char *arg, int from_tty)
14576 {
14577 event_location_up location = string_to_event_location (&arg,
14578 current_language);
14579 create_breakpoint (get_current_arch (),
14580 location.get (),
14581 NULL, 0, arg, false, 1 /* parse arg */,
14582 0 /* tempflag */,
14583 bp_fast_tracepoint /* type_wanted */,
14584 0 /* Ignore count */,
14585 pending_break_support,
14586 &tracepoint_breakpoint_ops,
14587 from_tty,
14588 1 /* enabled */,
14589 0 /* internal */, 0);
14590 }
14591
14592 /* strace command implementation. Creates a static tracepoint. */
14593
14594 static void
14595 strace_command (const char *arg, int from_tty)
14596 {
14597 struct breakpoint_ops *ops;
14598 event_location_up location;
14599
14600 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14601 or with a normal static tracepoint. */
14602 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14603 {
14604 ops = &strace_marker_breakpoint_ops;
14605 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
14606 }
14607 else
14608 {
14609 ops = &tracepoint_breakpoint_ops;
14610 location = string_to_event_location (&arg, current_language);
14611 }
14612
14613 create_breakpoint (get_current_arch (),
14614 location.get (),
14615 NULL, 0, arg, false, 1 /* parse arg */,
14616 0 /* tempflag */,
14617 bp_static_tracepoint /* type_wanted */,
14618 0 /* Ignore count */,
14619 pending_break_support,
14620 ops,
14621 from_tty,
14622 1 /* enabled */,
14623 0 /* internal */, 0);
14624 }
14625
14626 /* Set up a fake reader function that gets command lines from a linked
14627 list that was acquired during tracepoint uploading. */
14628
14629 static struct uploaded_tp *this_utp;
14630 static int next_cmd;
14631
14632 static char *
14633 read_uploaded_action (void)
14634 {
14635 char *rslt = nullptr;
14636
14637 if (next_cmd < this_utp->cmd_strings.size ())
14638 {
14639 rslt = this_utp->cmd_strings[next_cmd].get ();
14640 next_cmd++;
14641 }
14642
14643 return rslt;
14644 }
14645
14646 /* Given information about a tracepoint as recorded on a target (which
14647 can be either a live system or a trace file), attempt to create an
14648 equivalent GDB tracepoint. This is not a reliable process, since
14649 the target does not necessarily have all the information used when
14650 the tracepoint was originally defined. */
14651
14652 struct tracepoint *
14653 create_tracepoint_from_upload (struct uploaded_tp *utp)
14654 {
14655 const char *addr_str;
14656 char small_buf[100];
14657 struct tracepoint *tp;
14658
14659 if (utp->at_string)
14660 addr_str = utp->at_string.get ();
14661 else
14662 {
14663 /* In the absence of a source location, fall back to raw
14664 address. Since there is no way to confirm that the address
14665 means the same thing as when the trace was started, warn the
14666 user. */
14667 warning (_("Uploaded tracepoint %d has no "
14668 "source location, using raw address"),
14669 utp->number);
14670 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14671 addr_str = small_buf;
14672 }
14673
14674 /* There's not much we can do with a sequence of bytecodes. */
14675 if (utp->cond && !utp->cond_string)
14676 warning (_("Uploaded tracepoint %d condition "
14677 "has no source form, ignoring it"),
14678 utp->number);
14679
14680 event_location_up location = string_to_event_location (&addr_str,
14681 current_language);
14682 if (!create_breakpoint (get_current_arch (),
14683 location.get (),
14684 utp->cond_string.get (), -1, addr_str,
14685 false /* force_condition */,
14686 0 /* parse cond/thread */,
14687 0 /* tempflag */,
14688 utp->type /* type_wanted */,
14689 0 /* Ignore count */,
14690 pending_break_support,
14691 &tracepoint_breakpoint_ops,
14692 0 /* from_tty */,
14693 utp->enabled /* enabled */,
14694 0 /* internal */,
14695 CREATE_BREAKPOINT_FLAGS_INSERTED))
14696 return NULL;
14697
14698 /* Get the tracepoint we just created. */
14699 tp = get_tracepoint (tracepoint_count);
14700 gdb_assert (tp != NULL);
14701
14702 if (utp->pass > 0)
14703 {
14704 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14705 tp->number);
14706
14707 trace_pass_command (small_buf, 0);
14708 }
14709
14710 /* If we have uploaded versions of the original commands, set up a
14711 special-purpose "reader" function and call the usual command line
14712 reader, then pass the result to the breakpoint command-setting
14713 function. */
14714 if (!utp->cmd_strings.empty ())
14715 {
14716 counted_command_line cmd_list;
14717
14718 this_utp = utp;
14719 next_cmd = 0;
14720
14721 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
14722
14723 breakpoint_set_commands (tp, std::move (cmd_list));
14724 }
14725 else if (!utp->actions.empty ()
14726 || !utp->step_actions.empty ())
14727 warning (_("Uploaded tracepoint %d actions "
14728 "have no source form, ignoring them"),
14729 utp->number);
14730
14731 /* Copy any status information that might be available. */
14732 tp->hit_count = utp->hit_count;
14733 tp->traceframe_usage = utp->traceframe_usage;
14734
14735 return tp;
14736 }
14737
14738 /* Print information on tracepoint number TPNUM_EXP, or all if
14739 omitted. */
14740
14741 static void
14742 info_tracepoints_command (const char *args, int from_tty)
14743 {
14744 struct ui_out *uiout = current_uiout;
14745 int num_printed;
14746
14747 num_printed = breakpoint_1 (args, false, is_tracepoint);
14748
14749 if (num_printed == 0)
14750 {
14751 if (args == NULL || *args == '\0')
14752 uiout->message ("No tracepoints.\n");
14753 else
14754 uiout->message ("No tracepoint matching '%s'.\n", args);
14755 }
14756
14757 default_collect_info ();
14758 }
14759
14760 /* The 'enable trace' command enables tracepoints.
14761 Not supported by all targets. */
14762 static void
14763 enable_trace_command (const char *args, int from_tty)
14764 {
14765 enable_command (args, from_tty);
14766 }
14767
14768 /* The 'disable trace' command disables tracepoints.
14769 Not supported by all targets. */
14770 static void
14771 disable_trace_command (const char *args, int from_tty)
14772 {
14773 disable_command (args, from_tty);
14774 }
14775
14776 /* Remove a tracepoint (or all if no argument). */
14777 static void
14778 delete_trace_command (const char *arg, int from_tty)
14779 {
14780 dont_repeat ();
14781
14782 if (arg == 0)
14783 {
14784 int breaks_to_delete = 0;
14785
14786 /* Delete all breakpoints if no argument.
14787 Do not delete internal or call-dummy breakpoints, these
14788 have to be deleted with an explicit breakpoint number
14789 argument. */
14790 for (breakpoint *tp : all_tracepoints ())
14791 if (is_tracepoint (tp) && user_breakpoint_p (tp))
14792 {
14793 breaks_to_delete = 1;
14794 break;
14795 }
14796
14797 /* Ask user only if there are some breakpoints to delete. */
14798 if (!from_tty
14799 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14800 {
14801 for (breakpoint *b : all_breakpoints_safe ())
14802 if (is_tracepoint (b) && user_breakpoint_p (b))
14803 delete_breakpoint (b);
14804 }
14805 }
14806 else
14807 map_breakpoint_numbers
14808 (arg, [&] (breakpoint *br)
14809 {
14810 iterate_over_related_breakpoints (br, delete_breakpoint);
14811 });
14812 }
14813
14814 /* Helper function for trace_pass_command. */
14815
14816 static void
14817 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14818 {
14819 tp->pass_count = count;
14820 gdb::observers::breakpoint_modified.notify (tp);
14821 if (from_tty)
14822 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
14823 tp->number, count);
14824 }
14825
14826 /* Set passcount for tracepoint.
14827
14828 First command argument is passcount, second is tracepoint number.
14829 If tracepoint number omitted, apply to most recently defined.
14830 Also accepts special argument "all". */
14831
14832 static void
14833 trace_pass_command (const char *args, int from_tty)
14834 {
14835 struct tracepoint *t1;
14836 ULONGEST count;
14837
14838 if (args == 0 || *args == 0)
14839 error (_("passcount command requires an "
14840 "argument (count + optional TP num)"));
14841
14842 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
14843
14844 args = skip_spaces (args);
14845 if (*args && strncasecmp (args, "all", 3) == 0)
14846 {
14847 args += 3; /* Skip special argument "all". */
14848 if (*args)
14849 error (_("Junk at end of arguments."));
14850
14851 for (breakpoint *b : all_tracepoints ())
14852 {
14853 t1 = (struct tracepoint *) b;
14854 trace_pass_set_count (t1, count, from_tty);
14855 }
14856 }
14857 else if (*args == '\0')
14858 {
14859 t1 = get_tracepoint_by_number (&args, NULL);
14860 if (t1)
14861 trace_pass_set_count (t1, count, from_tty);
14862 }
14863 else
14864 {
14865 number_or_range_parser parser (args);
14866 while (!parser.finished ())
14867 {
14868 t1 = get_tracepoint_by_number (&args, &parser);
14869 if (t1)
14870 trace_pass_set_count (t1, count, from_tty);
14871 }
14872 }
14873 }
14874
14875 struct tracepoint *
14876 get_tracepoint (int num)
14877 {
14878 for (breakpoint *t : all_tracepoints ())
14879 if (t->number == num)
14880 return (struct tracepoint *) t;
14881
14882 return NULL;
14883 }
14884
14885 /* Find the tracepoint with the given target-side number (which may be
14886 different from the tracepoint number after disconnecting and
14887 reconnecting). */
14888
14889 struct tracepoint *
14890 get_tracepoint_by_number_on_target (int num)
14891 {
14892 for (breakpoint *b : all_tracepoints ())
14893 {
14894 struct tracepoint *t = (struct tracepoint *) b;
14895
14896 if (t->number_on_target == num)
14897 return t;
14898 }
14899
14900 return NULL;
14901 }
14902
14903 /* Utility: parse a tracepoint number and look it up in the list.
14904 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
14905 If the argument is missing, the most recent tracepoint
14906 (tracepoint_count) is returned. */
14907
14908 struct tracepoint *
14909 get_tracepoint_by_number (const char **arg,
14910 number_or_range_parser *parser)
14911 {
14912 int tpnum;
14913 const char *instring = arg == NULL ? NULL : *arg;
14914
14915 if (parser != NULL)
14916 {
14917 gdb_assert (!parser->finished ());
14918 tpnum = parser->get_number ();
14919 }
14920 else if (arg == NULL || *arg == NULL || ! **arg)
14921 tpnum = tracepoint_count;
14922 else
14923 tpnum = get_number (arg);
14924
14925 if (tpnum <= 0)
14926 {
14927 if (instring && *instring)
14928 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
14929 instring);
14930 else
14931 printf_filtered (_("No previous tracepoint\n"));
14932 return NULL;
14933 }
14934
14935 for (breakpoint *t : all_tracepoints ())
14936 if (t->number == tpnum)
14937 return (struct tracepoint *) t;
14938
14939 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
14940 return NULL;
14941 }
14942
14943 void
14944 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
14945 {
14946 if (b->thread != -1)
14947 fprintf_unfiltered (fp, " thread %d", b->thread);
14948
14949 if (b->task != 0)
14950 fprintf_unfiltered (fp, " task %d", b->task);
14951
14952 fprintf_unfiltered (fp, "\n");
14953 }
14954
14955 /* Save information on user settable breakpoints (watchpoints, etc) to
14956 a new script file named FILENAME. If FILTER is non-NULL, call it
14957 on each breakpoint and only include the ones for which it returns
14958 true. */
14959
14960 static void
14961 save_breakpoints (const char *filename, int from_tty,
14962 bool (*filter) (const struct breakpoint *))
14963 {
14964 int any = 0;
14965 int extra_trace_bits = 0;
14966
14967 if (filename == 0 || *filename == 0)
14968 error (_("Argument required (file name in which to save)"));
14969
14970 /* See if we have anything to save. */
14971 for (breakpoint *tp : all_breakpoints ())
14972 {
14973 /* Skip internal and momentary breakpoints. */
14974 if (!user_breakpoint_p (tp))
14975 continue;
14976
14977 /* If we have a filter, only save the breakpoints it accepts. */
14978 if (filter && !filter (tp))
14979 continue;
14980
14981 any = 1;
14982
14983 if (is_tracepoint (tp))
14984 {
14985 extra_trace_bits = 1;
14986
14987 /* We can stop searching. */
14988 break;
14989 }
14990 }
14991
14992 if (!any)
14993 {
14994 warning (_("Nothing to save."));
14995 return;
14996 }
14997
14998 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
14999
15000 stdio_file fp;
15001
15002 if (!fp.open (expanded_filename.get (), "w"))
15003 error (_("Unable to open file '%s' for saving (%s)"),
15004 expanded_filename.get (), safe_strerror (errno));
15005
15006 if (extra_trace_bits)
15007 save_trace_state_variables (&fp);
15008
15009 for (breakpoint *tp : all_breakpoints ())
15010 {
15011 /* Skip internal and momentary breakpoints. */
15012 if (!user_breakpoint_p (tp))
15013 continue;
15014
15015 /* If we have a filter, only save the breakpoints it accepts. */
15016 if (filter && !filter (tp))
15017 continue;
15018
15019 tp->ops->print_recreate (tp, &fp);
15020
15021 /* Note, we can't rely on tp->number for anything, as we can't
15022 assume the recreated breakpoint numbers will match. Use $bpnum
15023 instead. */
15024
15025 if (tp->cond_string)
15026 fp.printf (" condition $bpnum %s\n", tp->cond_string.get ());
15027
15028 if (tp->ignore_count)
15029 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15030
15031 if (tp->type != bp_dprintf && tp->commands)
15032 {
15033 fp.puts (" commands\n");
15034
15035 current_uiout->redirect (&fp);
15036 try
15037 {
15038 print_command_lines (current_uiout, tp->commands.get (), 2);
15039 }
15040 catch (const gdb_exception &ex)
15041 {
15042 current_uiout->redirect (NULL);
15043 throw;
15044 }
15045
15046 current_uiout->redirect (NULL);
15047 fp.puts (" end\n");
15048 }
15049
15050 if (tp->enable_state == bp_disabled)
15051 fp.puts ("disable $bpnum\n");
15052
15053 /* If this is a multi-location breakpoint, check if the locations
15054 should be individually disabled. Watchpoint locations are
15055 special, and not user visible. */
15056 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15057 {
15058 int n = 1;
15059
15060 for (bp_location *loc : tp->locations ())
15061 {
15062 if (!loc->enabled)
15063 fp.printf ("disable $bpnum.%d\n", n);
15064
15065 n++;
15066 }
15067 }
15068 }
15069
15070 if (extra_trace_bits && !default_collect.empty ())
15071 fp.printf ("set default-collect %s\n", default_collect.c_str ());
15072
15073 if (from_tty)
15074 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
15075 }
15076
15077 /* The `save breakpoints' command. */
15078
15079 static void
15080 save_breakpoints_command (const char *args, int from_tty)
15081 {
15082 save_breakpoints (args, from_tty, NULL);
15083 }
15084
15085 /* The `save tracepoints' command. */
15086
15087 static void
15088 save_tracepoints_command (const char *args, int from_tty)
15089 {
15090 save_breakpoints (args, from_tty, is_tracepoint);
15091 }
15092
15093 \f
15094 /* This help string is used to consolidate all the help string for specifying
15095 locations used by several commands. */
15096
15097 #define LOCATION_HELP_STRING \
15098 "Linespecs are colon-separated lists of location parameters, such as\n\
15099 source filename, function name, label name, and line number.\n\
15100 Example: To specify the start of a label named \"the_top\" in the\n\
15101 function \"fact\" in the file \"factorial.c\", use\n\
15102 \"factorial.c:fact:the_top\".\n\
15103 \n\
15104 Address locations begin with \"*\" and specify an exact address in the\n\
15105 program. Example: To specify the fourth byte past the start function\n\
15106 \"main\", use \"*main + 4\".\n\
15107 \n\
15108 Explicit locations are similar to linespecs but use an option/argument\n\
15109 syntax to specify location parameters.\n\
15110 Example: To specify the start of the label named \"the_top\" in the\n\
15111 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15112 -function fact -label the_top\".\n\
15113 \n\
15114 By default, a specified function is matched against the program's\n\
15115 functions in all scopes. For C++, this means in all namespaces and\n\
15116 classes. For Ada, this means in all packages. E.g., in C++,\n\
15117 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15118 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
15119 specified name as a complete fully-qualified name instead."
15120
15121 /* This help string is used for the break, hbreak, tbreak and thbreak
15122 commands. It is defined as a macro to prevent duplication.
15123 COMMAND should be a string constant containing the name of the
15124 command. */
15125
15126 #define BREAK_ARGS_HELP(command) \
15127 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
15128 \t[-force-condition] [if CONDITION]\n\
15129 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15130 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15131 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15132 `-probe-dtrace' (for a DTrace probe).\n\
15133 LOCATION may be a linespec, address, or explicit location as described\n\
15134 below.\n\
15135 \n\
15136 With no LOCATION, uses current execution address of the selected\n\
15137 stack frame. This is useful for breaking on return to a stack frame.\n\
15138 \n\
15139 THREADNUM is the number from \"info threads\".\n\
15140 CONDITION is a boolean expression.\n\
15141 \n\
15142 With the \"-force-condition\" flag, the condition is defined even when\n\
15143 it is invalid for all current locations.\n\
15144 \n" LOCATION_HELP_STRING "\n\n\
15145 Multiple breakpoints at one place are permitted, and useful if their\n\
15146 conditions are different.\n\
15147 \n\
15148 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15149
15150 /* List of subcommands for "catch". */
15151 static struct cmd_list_element *catch_cmdlist;
15152
15153 /* List of subcommands for "tcatch". */
15154 static struct cmd_list_element *tcatch_cmdlist;
15155
15156 void
15157 add_catch_command (const char *name, const char *docstring,
15158 cmd_func_ftype *func,
15159 completer_ftype *completer,
15160 void *user_data_catch,
15161 void *user_data_tcatch)
15162 {
15163 struct cmd_list_element *command;
15164
15165 command = add_cmd (name, class_breakpoint, docstring,
15166 &catch_cmdlist);
15167 command->func = func;
15168 command->set_context (user_data_catch);
15169 set_cmd_completer (command, completer);
15170
15171 command = add_cmd (name, class_breakpoint, docstring,
15172 &tcatch_cmdlist);
15173 command->func = func;
15174 command->set_context (user_data_tcatch);
15175 set_cmd_completer (command, completer);
15176 }
15177
15178 /* Zero if any of the breakpoint's locations could be a location where
15179 functions have been inlined, nonzero otherwise. */
15180
15181 static int
15182 is_non_inline_function (struct breakpoint *b)
15183 {
15184 /* The shared library event breakpoint is set on the address of a
15185 non-inline function. */
15186 if (b->type == bp_shlib_event)
15187 return 1;
15188
15189 return 0;
15190 }
15191
15192 /* Nonzero if the specified PC cannot be a location where functions
15193 have been inlined. */
15194
15195 int
15196 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
15197 const target_waitstatus &ws)
15198 {
15199 for (breakpoint *b : all_breakpoints ())
15200 {
15201 if (!is_non_inline_function (b))
15202 continue;
15203
15204 for (bp_location *bl : b->locations ())
15205 {
15206 if (!bl->shlib_disabled
15207 && bpstat_check_location (bl, aspace, pc, ws))
15208 return 1;
15209 }
15210 }
15211
15212 return 0;
15213 }
15214
15215 /* Remove any references to OBJFILE which is going to be freed. */
15216
15217 void
15218 breakpoint_free_objfile (struct objfile *objfile)
15219 {
15220 for (bp_location *loc : all_bp_locations ())
15221 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15222 loc->symtab = NULL;
15223 }
15224
15225 void
15226 initialize_breakpoint_ops (void)
15227 {
15228 static int initialized = 0;
15229
15230 struct breakpoint_ops *ops;
15231
15232 if (initialized)
15233 return;
15234 initialized = 1;
15235
15236 /* The breakpoint_ops structure to be inherit by all kinds of
15237 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15238 internal and momentary breakpoints, etc.). */
15239 ops = &bkpt_base_breakpoint_ops;
15240 *ops = base_breakpoint_ops;
15241 ops->re_set = bkpt_re_set;
15242 ops->insert_location = bkpt_insert_location;
15243 ops->remove_location = bkpt_remove_location;
15244 ops->breakpoint_hit = bkpt_breakpoint_hit;
15245 ops->create_sals_from_location = bkpt_create_sals_from_location;
15246 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15247 ops->decode_location = bkpt_decode_location;
15248
15249 /* The breakpoint_ops structure to be used in regular breakpoints. */
15250 ops = &bkpt_breakpoint_ops;
15251 *ops = bkpt_base_breakpoint_ops;
15252 ops->re_set = bkpt_re_set;
15253 ops->resources_needed = bkpt_resources_needed;
15254 ops->print_it = bkpt_print_it;
15255 ops->print_mention = bkpt_print_mention;
15256 ops->print_recreate = bkpt_print_recreate;
15257
15258 /* Ranged breakpoints. */
15259 ops = &ranged_breakpoint_ops;
15260 *ops = bkpt_breakpoint_ops;
15261 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15262 ops->resources_needed = resources_needed_ranged_breakpoint;
15263 ops->print_it = print_it_ranged_breakpoint;
15264 ops->print_one = print_one_ranged_breakpoint;
15265 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15266 ops->print_mention = print_mention_ranged_breakpoint;
15267 ops->print_recreate = print_recreate_ranged_breakpoint;
15268
15269 /* Internal breakpoints. */
15270 ops = &internal_breakpoint_ops;
15271 *ops = bkpt_base_breakpoint_ops;
15272 ops->re_set = internal_bkpt_re_set;
15273 ops->check_status = internal_bkpt_check_status;
15274 ops->print_it = internal_bkpt_print_it;
15275 ops->print_mention = internal_bkpt_print_mention;
15276
15277 /* Momentary breakpoints. */
15278 ops = &momentary_breakpoint_ops;
15279 *ops = bkpt_base_breakpoint_ops;
15280 ops->re_set = momentary_bkpt_re_set;
15281 ops->check_status = momentary_bkpt_check_status;
15282 ops->print_it = momentary_bkpt_print_it;
15283 ops->print_mention = momentary_bkpt_print_mention;
15284
15285 /* Probe breakpoints. */
15286 ops = &bkpt_probe_breakpoint_ops;
15287 *ops = bkpt_breakpoint_ops;
15288 ops->insert_location = bkpt_probe_insert_location;
15289 ops->remove_location = bkpt_probe_remove_location;
15290 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15291 ops->decode_location = bkpt_probe_decode_location;
15292
15293 /* Watchpoints. */
15294 ops = &watchpoint_breakpoint_ops;
15295 *ops = base_breakpoint_ops;
15296 ops->re_set = re_set_watchpoint;
15297 ops->insert_location = insert_watchpoint;
15298 ops->remove_location = remove_watchpoint;
15299 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15300 ops->check_status = check_status_watchpoint;
15301 ops->resources_needed = resources_needed_watchpoint;
15302 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15303 ops->print_it = print_it_watchpoint;
15304 ops->print_mention = print_mention_watchpoint;
15305 ops->print_recreate = print_recreate_watchpoint;
15306 ops->explains_signal = explains_signal_watchpoint;
15307
15308 /* Masked watchpoints. */
15309 ops = &masked_watchpoint_breakpoint_ops;
15310 *ops = watchpoint_breakpoint_ops;
15311 ops->insert_location = insert_masked_watchpoint;
15312 ops->remove_location = remove_masked_watchpoint;
15313 ops->resources_needed = resources_needed_masked_watchpoint;
15314 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15315 ops->print_it = print_it_masked_watchpoint;
15316 ops->print_one_detail = print_one_detail_masked_watchpoint;
15317 ops->print_mention = print_mention_masked_watchpoint;
15318 ops->print_recreate = print_recreate_masked_watchpoint;
15319
15320 /* Tracepoints. */
15321 ops = &tracepoint_breakpoint_ops;
15322 *ops = base_breakpoint_ops;
15323 ops->re_set = tracepoint_re_set;
15324 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15325 ops->print_one_detail = tracepoint_print_one_detail;
15326 ops->print_mention = tracepoint_print_mention;
15327 ops->print_recreate = tracepoint_print_recreate;
15328 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15329 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15330 ops->decode_location = tracepoint_decode_location;
15331
15332 /* Probe tracepoints. */
15333 ops = &tracepoint_probe_breakpoint_ops;
15334 *ops = tracepoint_breakpoint_ops;
15335 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15336 ops->decode_location = tracepoint_probe_decode_location;
15337
15338 /* Static tracepoints with marker (`-m'). */
15339 ops = &strace_marker_breakpoint_ops;
15340 *ops = tracepoint_breakpoint_ops;
15341 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15342 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15343 ops->decode_location = strace_marker_decode_location;
15344
15345 /* Fork catchpoints. */
15346 ops = &catch_fork_breakpoint_ops;
15347 *ops = base_breakpoint_ops;
15348 ops->insert_location = insert_catch_fork;
15349 ops->remove_location = remove_catch_fork;
15350 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15351 ops->print_it = print_it_catch_fork;
15352 ops->print_one = print_one_catch_fork;
15353 ops->print_mention = print_mention_catch_fork;
15354 ops->print_recreate = print_recreate_catch_fork;
15355
15356 /* Vfork catchpoints. */
15357 ops = &catch_vfork_breakpoint_ops;
15358 *ops = base_breakpoint_ops;
15359 ops->insert_location = insert_catch_vfork;
15360 ops->remove_location = remove_catch_vfork;
15361 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15362 ops->print_it = print_it_catch_vfork;
15363 ops->print_one = print_one_catch_vfork;
15364 ops->print_mention = print_mention_catch_vfork;
15365 ops->print_recreate = print_recreate_catch_vfork;
15366
15367 /* Exec catchpoints. */
15368 ops = &catch_exec_breakpoint_ops;
15369 *ops = base_breakpoint_ops;
15370 ops->insert_location = insert_catch_exec;
15371 ops->remove_location = remove_catch_exec;
15372 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15373 ops->print_it = print_it_catch_exec;
15374 ops->print_one = print_one_catch_exec;
15375 ops->print_mention = print_mention_catch_exec;
15376 ops->print_recreate = print_recreate_catch_exec;
15377
15378 /* Solib-related catchpoints. */
15379 ops = &catch_solib_breakpoint_ops;
15380 *ops = base_breakpoint_ops;
15381 ops->insert_location = insert_catch_solib;
15382 ops->remove_location = remove_catch_solib;
15383 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15384 ops->check_status = check_status_catch_solib;
15385 ops->print_it = print_it_catch_solib;
15386 ops->print_one = print_one_catch_solib;
15387 ops->print_mention = print_mention_catch_solib;
15388 ops->print_recreate = print_recreate_catch_solib;
15389
15390 ops = &dprintf_breakpoint_ops;
15391 *ops = bkpt_base_breakpoint_ops;
15392 ops->re_set = dprintf_re_set;
15393 ops->resources_needed = bkpt_resources_needed;
15394 ops->print_it = bkpt_print_it;
15395 ops->print_mention = bkpt_print_mention;
15396 ops->print_recreate = dprintf_print_recreate;
15397 ops->after_condition_true = dprintf_after_condition_true;
15398 ops->breakpoint_hit = dprintf_breakpoint_hit;
15399 }
15400
15401 /* Chain containing all defined "enable breakpoint" subcommands. */
15402
15403 static struct cmd_list_element *enablebreaklist = NULL;
15404
15405 /* See breakpoint.h. */
15406
15407 cmd_list_element *commands_cmd_element = nullptr;
15408
15409 void _initialize_breakpoint ();
15410 void
15411 _initialize_breakpoint ()
15412 {
15413 struct cmd_list_element *c;
15414
15415 initialize_breakpoint_ops ();
15416
15417 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
15418 "breakpoint");
15419 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
15420 "breakpoint");
15421 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
15422 "breakpoint");
15423
15424 breakpoint_chain = 0;
15425 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15426 before a breakpoint is set. */
15427 breakpoint_count = 0;
15428
15429 tracepoint_count = 0;
15430
15431 add_com ("ignore", class_breakpoint, ignore_command, _("\
15432 Set ignore-count of breakpoint number N to COUNT.\n\
15433 Usage is `ignore N COUNT'."));
15434
15435 commands_cmd_element = add_com ("commands", class_breakpoint,
15436 commands_command, _("\
15437 Set commands to be executed when the given breakpoints are hit.\n\
15438 Give a space-separated breakpoint list as argument after \"commands\".\n\
15439 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15440 (e.g. `5-7').\n\
15441 With no argument, the targeted breakpoint is the last one set.\n\
15442 The commands themselves follow starting on the next line.\n\
15443 Type a line containing \"end\" to indicate the end of them.\n\
15444 Give \"silent\" as the first line to make the breakpoint silent;\n\
15445 then no output is printed when it is hit, except what the commands print."));
15446
15447 const auto cc_opts = make_condition_command_options_def_group (nullptr);
15448 static std::string condition_command_help
15449 = gdb::option::build_help (_("\
15450 Specify breakpoint number N to break only if COND is true.\n\
15451 Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
15452 is an expression to be evaluated whenever breakpoint N is reached.\n\
15453 \n\
15454 Options:\n\
15455 %OPTIONS%"), cc_opts);
15456
15457 c = add_com ("condition", class_breakpoint, condition_command,
15458 condition_command_help.c_str ());
15459 set_cmd_completer_handle_brkchars (c, condition_completer);
15460
15461 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15462 Set a temporary breakpoint.\n\
15463 Like \"break\" except the breakpoint is only temporary,\n\
15464 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15465 by using \"enable delete\" on the breakpoint number.\n\
15466 \n"
15467 BREAK_ARGS_HELP ("tbreak")));
15468 set_cmd_completer (c, location_completer);
15469
15470 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15471 Set a hardware assisted breakpoint.\n\
15472 Like \"break\" except the breakpoint requires hardware support,\n\
15473 some target hardware may not have this support.\n\
15474 \n"
15475 BREAK_ARGS_HELP ("hbreak")));
15476 set_cmd_completer (c, location_completer);
15477
15478 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15479 Set a temporary hardware assisted breakpoint.\n\
15480 Like \"hbreak\" except the breakpoint is only temporary,\n\
15481 so it will be deleted when hit.\n\
15482 \n"
15483 BREAK_ARGS_HELP ("thbreak")));
15484 set_cmd_completer (c, location_completer);
15485
15486 cmd_list_element *enable_cmd
15487 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15488 Enable all or some breakpoints.\n\
15489 Usage: enable [BREAKPOINTNUM]...\n\
15490 Give breakpoint numbers (separated by spaces) as arguments.\n\
15491 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15492 This is used to cancel the effect of the \"disable\" command.\n\
15493 With a subcommand you can enable temporarily."),
15494 &enablelist, 1, &cmdlist);
15495
15496 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
15497
15498 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15499 Enable all or some breakpoints.\n\
15500 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
15501 Give breakpoint numbers (separated by spaces) as arguments.\n\
15502 This is used to cancel the effect of the \"disable\" command.\n\
15503 May be abbreviated to simply \"enable\"."),
15504 &enablebreaklist, 1, &enablelist);
15505
15506 add_cmd ("once", no_class, enable_once_command, _("\
15507 Enable some breakpoints for one hit.\n\
15508 Usage: enable breakpoints once BREAKPOINTNUM...\n\
15509 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15510 &enablebreaklist);
15511
15512 add_cmd ("delete", no_class, enable_delete_command, _("\
15513 Enable some breakpoints and delete when hit.\n\
15514 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
15515 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15516 &enablebreaklist);
15517
15518 add_cmd ("count", no_class, enable_count_command, _("\
15519 Enable some breakpoints for COUNT hits.\n\
15520 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
15521 If a breakpoint is hit while enabled in this fashion,\n\
15522 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15523 &enablebreaklist);
15524
15525 add_cmd ("delete", no_class, enable_delete_command, _("\
15526 Enable some breakpoints and delete when hit.\n\
15527 Usage: enable delete BREAKPOINTNUM...\n\
15528 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15529 &enablelist);
15530
15531 add_cmd ("once", no_class, enable_once_command, _("\
15532 Enable some breakpoints for one hit.\n\
15533 Usage: enable once BREAKPOINTNUM...\n\
15534 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15535 &enablelist);
15536
15537 add_cmd ("count", no_class, enable_count_command, _("\
15538 Enable some breakpoints for COUNT hits.\n\
15539 Usage: enable count COUNT BREAKPOINTNUM...\n\
15540 If a breakpoint is hit while enabled in this fashion,\n\
15541 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15542 &enablelist);
15543
15544 cmd_list_element *disable_cmd
15545 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15546 Disable all or some breakpoints.\n\
15547 Usage: disable [BREAKPOINTNUM]...\n\
15548 Arguments are breakpoint numbers with spaces in between.\n\
15549 To disable all breakpoints, give no argument.\n\
15550 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15551 &disablelist, 1, &cmdlist);
15552 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
15553 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
15554
15555 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
15556 Disable all or some breakpoints.\n\
15557 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
15558 Arguments are breakpoint numbers with spaces in between.\n\
15559 To disable all breakpoints, give no argument.\n\
15560 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15561 This command may be abbreviated \"disable\"."),
15562 &disablelist);
15563
15564 cmd_list_element *delete_cmd
15565 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15566 Delete all or some breakpoints.\n\
15567 Usage: delete [BREAKPOINTNUM]...\n\
15568 Arguments are breakpoint numbers with spaces in between.\n\
15569 To delete all breakpoints, give no argument.\n\
15570 \n\
15571 Also a prefix command for deletion of other GDB objects."),
15572 &deletelist, 1, &cmdlist);
15573 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
15574 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
15575
15576 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
15577 Delete all or some breakpoints or auto-display expressions.\n\
15578 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
15579 Arguments are breakpoint numbers with spaces in between.\n\
15580 To delete all breakpoints, give no argument.\n\
15581 This command may be abbreviated \"delete\"."),
15582 &deletelist);
15583
15584 cmd_list_element *clear_cmd
15585 = add_com ("clear", class_breakpoint, clear_command, _("\
15586 Clear breakpoint at specified location.\n\
15587 Argument may be a linespec, explicit, or address location as described below.\n\
15588 \n\
15589 With no argument, clears all breakpoints in the line that the selected frame\n\
15590 is executing in.\n"
15591 "\n" LOCATION_HELP_STRING "\n\n\
15592 See also the \"delete\" command which clears breakpoints by number."));
15593 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
15594
15595 cmd_list_element *break_cmd
15596 = add_com ("break", class_breakpoint, break_command, _("\
15597 Set breakpoint at specified location.\n"
15598 BREAK_ARGS_HELP ("break")));
15599 set_cmd_completer (break_cmd, location_completer);
15600
15601 add_com_alias ("b", break_cmd, class_run, 1);
15602 add_com_alias ("br", break_cmd, class_run, 1);
15603 add_com_alias ("bre", break_cmd, class_run, 1);
15604 add_com_alias ("brea", break_cmd, class_run, 1);
15605
15606 if (dbx_commands)
15607 {
15608 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15609 Break in function/address or break at a line in the current file."),
15610 &stoplist, 1, &cmdlist);
15611 add_cmd ("in", class_breakpoint, stopin_command,
15612 _("Break in function or address."), &stoplist);
15613 add_cmd ("at", class_breakpoint, stopat_command,
15614 _("Break at a line in the current file."), &stoplist);
15615 add_com ("status", class_info, info_breakpoints_command, _("\
15616 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15617 The \"Type\" column indicates one of:\n\
15618 \tbreakpoint - normal breakpoint\n\
15619 \twatchpoint - watchpoint\n\
15620 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15621 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15622 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15623 address and file/line number respectively.\n\
15624 \n\
15625 Convenience variable \"$_\" and default examine address for \"x\"\n\
15626 are set to the address of the last breakpoint listed unless the command\n\
15627 is prefixed with \"server \".\n\n\
15628 Convenience variable \"$bpnum\" contains the number of the last\n\
15629 breakpoint set."));
15630 }
15631
15632 cmd_list_element *info_breakpoints_cmd
15633 = add_info ("breakpoints", info_breakpoints_command, _("\
15634 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15635 The \"Type\" column indicates one of:\n\
15636 \tbreakpoint - normal breakpoint\n\
15637 \twatchpoint - watchpoint\n\
15638 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15639 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15640 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15641 address and file/line number respectively.\n\
15642 \n\
15643 Convenience variable \"$_\" and default examine address for \"x\"\n\
15644 are set to the address of the last breakpoint listed unless the command\n\
15645 is prefixed with \"server \".\n\n\
15646 Convenience variable \"$bpnum\" contains the number of the last\n\
15647 breakpoint set."));
15648
15649 add_info_alias ("b", info_breakpoints_cmd, 1);
15650
15651 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15652 Status of all breakpoints, or breakpoint number NUMBER.\n\
15653 The \"Type\" column indicates one of:\n\
15654 \tbreakpoint - normal breakpoint\n\
15655 \twatchpoint - watchpoint\n\
15656 \tlongjmp - internal breakpoint used to step through longjmp()\n\
15657 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15658 \tuntil - internal breakpoint used by the \"until\" command\n\
15659 \tfinish - internal breakpoint used by the \"finish\" command\n\
15660 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15661 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15662 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15663 address and file/line number respectively.\n\
15664 \n\
15665 Convenience variable \"$_\" and default examine address for \"x\"\n\
15666 are set to the address of the last breakpoint listed unless the command\n\
15667 is prefixed with \"server \".\n\n\
15668 Convenience variable \"$bpnum\" contains the number of the last\n\
15669 breakpoint set."),
15670 &maintenanceinfolist);
15671
15672 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
15673 Set catchpoints to catch events."),
15674 &catch_cmdlist,
15675 0/*allow-unknown*/, &cmdlist);
15676
15677 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
15678 Set temporary catchpoints to catch events."),
15679 &tcatch_cmdlist,
15680 0/*allow-unknown*/, &cmdlist);
15681
15682 add_catch_command ("fork", _("Catch calls to fork."),
15683 catch_fork_command_1,
15684 NULL,
15685 (void *) (uintptr_t) catch_fork_permanent,
15686 (void *) (uintptr_t) catch_fork_temporary);
15687 add_catch_command ("vfork", _("Catch calls to vfork."),
15688 catch_fork_command_1,
15689 NULL,
15690 (void *) (uintptr_t) catch_vfork_permanent,
15691 (void *) (uintptr_t) catch_vfork_temporary);
15692 add_catch_command ("exec", _("Catch calls to exec."),
15693 catch_exec_command_1,
15694 NULL,
15695 CATCH_PERMANENT,
15696 CATCH_TEMPORARY);
15697 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15698 Usage: catch load [REGEX]\n\
15699 If REGEX is given, only stop for libraries matching the regular expression."),
15700 catch_load_command_1,
15701 NULL,
15702 CATCH_PERMANENT,
15703 CATCH_TEMPORARY);
15704 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15705 Usage: catch unload [REGEX]\n\
15706 If REGEX is given, only stop for libraries matching the regular expression."),
15707 catch_unload_command_1,
15708 NULL,
15709 CATCH_PERMANENT,
15710 CATCH_TEMPORARY);
15711
15712 const auto opts = make_watch_options_def_group (nullptr);
15713
15714 static const std::string watch_help = gdb::option::build_help (_("\
15715 Set a watchpoint for EXPRESSION.\n\
15716 Usage: watch [-location] EXPRESSION\n\
15717 \n\
15718 Options:\n\
15719 %OPTIONS%\n\
15720 \n\
15721 A watchpoint stops execution of your program whenever the value of\n\
15722 an expression changes."), opts);
15723 c = add_com ("watch", class_breakpoint, watch_command,
15724 watch_help.c_str ());
15725 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15726
15727 static const std::string rwatch_help = gdb::option::build_help (_("\
15728 Set a read watchpoint for EXPRESSION.\n\
15729 Usage: rwatch [-location] EXPRESSION\n\
15730 \n\
15731 Options:\n\
15732 %OPTIONS%\n\
15733 \n\
15734 A read watchpoint stops execution of your program whenever the value of\n\
15735 an expression is read."), opts);
15736 c = add_com ("rwatch", class_breakpoint, rwatch_command,
15737 rwatch_help.c_str ());
15738 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15739
15740 static const std::string awatch_help = gdb::option::build_help (_("\
15741 Set an access watchpoint for EXPRESSION.\n\
15742 Usage: awatch [-location] EXPRESSION\n\
15743 \n\
15744 Options:\n\
15745 %OPTIONS%\n\
15746 \n\
15747 An access watchpoint stops execution of your program whenever the value\n\
15748 of an expression is either read or written."), opts);
15749 c = add_com ("awatch", class_breakpoint, awatch_command,
15750 awatch_help.c_str ());
15751 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15752
15753 add_info ("watchpoints", info_watchpoints_command, _("\
15754 Status of specified watchpoints (all watchpoints if no argument)."));
15755
15756 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15757 respond to changes - contrary to the description. */
15758 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15759 &can_use_hw_watchpoints, _("\
15760 Set debugger's willingness to use watchpoint hardware."), _("\
15761 Show debugger's willingness to use watchpoint hardware."), _("\
15762 If zero, gdb will not use hardware for new watchpoints, even if\n\
15763 such is available. (However, any hardware watchpoints that were\n\
15764 created before setting this to nonzero, will continue to use watchpoint\n\
15765 hardware.)"),
15766 NULL,
15767 show_can_use_hw_watchpoints,
15768 &setlist, &showlist);
15769
15770 can_use_hw_watchpoints = 1;
15771
15772 /* Tracepoint manipulation commands. */
15773
15774 cmd_list_element *trace_cmd
15775 = add_com ("trace", class_breakpoint, trace_command, _("\
15776 Set a tracepoint at specified location.\n\
15777 \n"
15778 BREAK_ARGS_HELP ("trace") "\n\
15779 Do \"help tracepoints\" for info on other tracepoint commands."));
15780 set_cmd_completer (trace_cmd, location_completer);
15781
15782 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
15783 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
15784 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
15785 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
15786
15787 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
15788 Set a fast tracepoint at specified location.\n\
15789 \n"
15790 BREAK_ARGS_HELP ("ftrace") "\n\
15791 Do \"help tracepoints\" for info on other tracepoint commands."));
15792 set_cmd_completer (c, location_completer);
15793
15794 c = add_com ("strace", class_breakpoint, strace_command, _("\
15795 Set a static tracepoint at location or marker.\n\
15796 \n\
15797 strace [LOCATION] [if CONDITION]\n\
15798 LOCATION may be a linespec, explicit, or address location (described below) \n\
15799 or -m MARKER_ID.\n\n\
15800 If a marker id is specified, probe the marker with that name. With\n\
15801 no LOCATION, uses current execution address of the selected stack frame.\n\
15802 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15803 This collects arbitrary user data passed in the probe point call to the\n\
15804 tracing library. You can inspect it when analyzing the trace buffer,\n\
15805 by printing the $_sdata variable like any other convenience variable.\n\
15806 \n\
15807 CONDITION is a boolean expression.\n\
15808 \n" LOCATION_HELP_STRING "\n\n\
15809 Multiple tracepoints at one place are permitted, and useful if their\n\
15810 conditions are different.\n\
15811 \n\
15812 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15813 Do \"help tracepoints\" for info on other tracepoint commands."));
15814 set_cmd_completer (c, location_completer);
15815
15816 cmd_list_element *info_tracepoints_cmd
15817 = add_info ("tracepoints", info_tracepoints_command, _("\
15818 Status of specified tracepoints (all tracepoints if no argument).\n\
15819 Convenience variable \"$tpnum\" contains the number of the\n\
15820 last tracepoint set."));
15821
15822 add_info_alias ("tp", info_tracepoints_cmd, 1);
15823
15824 cmd_list_element *delete_tracepoints_cmd
15825 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15826 Delete specified tracepoints.\n\
15827 Arguments are tracepoint numbers, separated by spaces.\n\
15828 No argument means delete all tracepoints."),
15829 &deletelist);
15830 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
15831
15832 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15833 Disable specified tracepoints.\n\
15834 Arguments are tracepoint numbers, separated by spaces.\n\
15835 No argument means disable all tracepoints."),
15836 &disablelist);
15837 deprecate_cmd (c, "disable");
15838
15839 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15840 Enable specified tracepoints.\n\
15841 Arguments are tracepoint numbers, separated by spaces.\n\
15842 No argument means enable all tracepoints."),
15843 &enablelist);
15844 deprecate_cmd (c, "enable");
15845
15846 add_com ("passcount", class_trace, trace_pass_command, _("\
15847 Set the passcount for a tracepoint.\n\
15848 The trace will end when the tracepoint has been passed 'count' times.\n\
15849 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15850 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15851
15852 add_basic_prefix_cmd ("save", class_breakpoint,
15853 _("Save breakpoint definitions as a script."),
15854 &save_cmdlist,
15855 0/*allow-unknown*/, &cmdlist);
15856
15857 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15858 Save current breakpoint definitions as a script.\n\
15859 This includes all types of breakpoints (breakpoints, watchpoints,\n\
15860 catchpoints, tracepoints). Use the 'source' command in another debug\n\
15861 session to restore them."),
15862 &save_cmdlist);
15863 set_cmd_completer (c, filename_completer);
15864
15865 cmd_list_element *save_tracepoints_cmd
15866 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
15867 Save current tracepoint definitions as a script.\n\
15868 Use the 'source' command in another debug session to restore them."),
15869 &save_cmdlist);
15870 set_cmd_completer (save_tracepoints_cmd, filename_completer);
15871
15872 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
15873 deprecate_cmd (c, "save tracepoints");
15874
15875 add_setshow_prefix_cmd ("breakpoint", class_maintenance,
15876 _("\
15877 Breakpoint specific settings.\n\
15878 Configure various breakpoint-specific variables such as\n\
15879 pending breakpoint behavior."),
15880 _("\
15881 Breakpoint specific settings.\n\
15882 Configure various breakpoint-specific variables such as\n\
15883 pending breakpoint behavior."),
15884 &breakpoint_set_cmdlist, &breakpoint_show_cmdlist,
15885 &setlist, &showlist);
15886
15887 add_setshow_auto_boolean_cmd ("pending", no_class,
15888 &pending_break_support, _("\
15889 Set debugger's behavior regarding pending breakpoints."), _("\
15890 Show debugger's behavior regarding pending breakpoints."), _("\
15891 If on, an unrecognized breakpoint location will cause gdb to create a\n\
15892 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15893 an error. If auto, an unrecognized breakpoint location results in a\n\
15894 user-query to see if a pending breakpoint should be created."),
15895 NULL,
15896 show_pending_break_support,
15897 &breakpoint_set_cmdlist,
15898 &breakpoint_show_cmdlist);
15899
15900 pending_break_support = AUTO_BOOLEAN_AUTO;
15901
15902 add_setshow_boolean_cmd ("auto-hw", no_class,
15903 &automatic_hardware_breakpoints, _("\
15904 Set automatic usage of hardware breakpoints."), _("\
15905 Show automatic usage of hardware breakpoints."), _("\
15906 If set, the debugger will automatically use hardware breakpoints for\n\
15907 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15908 a warning will be emitted for such breakpoints."),
15909 NULL,
15910 show_automatic_hardware_breakpoints,
15911 &breakpoint_set_cmdlist,
15912 &breakpoint_show_cmdlist);
15913
15914 add_setshow_boolean_cmd ("always-inserted", class_support,
15915 &always_inserted_mode, _("\
15916 Set mode for inserting breakpoints."), _("\
15917 Show mode for inserting breakpoints."), _("\
15918 When this mode is on, breakpoints are inserted immediately as soon as\n\
15919 they're created, kept inserted even when execution stops, and removed\n\
15920 only when the user deletes them. When this mode is off (the default),\n\
15921 breakpoints are inserted only when execution continues, and removed\n\
15922 when execution stops."),
15923 NULL,
15924 &show_always_inserted_mode,
15925 &breakpoint_set_cmdlist,
15926 &breakpoint_show_cmdlist);
15927
15928 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15929 condition_evaluation_enums,
15930 &condition_evaluation_mode_1, _("\
15931 Set mode of breakpoint condition evaluation."), _("\
15932 Show mode of breakpoint condition evaluation."), _("\
15933 When this is set to \"host\", breakpoint conditions will be\n\
15934 evaluated on the host's side by GDB. When it is set to \"target\",\n\
15935 breakpoint conditions will be downloaded to the target (if the target\n\
15936 supports such feature) and conditions will be evaluated on the target's side.\n\
15937 If this is set to \"auto\" (default), this will be automatically set to\n\
15938 \"target\" if it supports condition evaluation, otherwise it will\n\
15939 be set to \"host\"."),
15940 &set_condition_evaluation_mode,
15941 &show_condition_evaluation_mode,
15942 &breakpoint_set_cmdlist,
15943 &breakpoint_show_cmdlist);
15944
15945 add_com ("break-range", class_breakpoint, break_range_command, _("\
15946 Set a breakpoint for an address range.\n\
15947 break-range START-LOCATION, END-LOCATION\n\
15948 where START-LOCATION and END-LOCATION can be one of the following:\n\
15949 LINENUM, for that line in the current file,\n\
15950 FILE:LINENUM, for that line in that file,\n\
15951 +OFFSET, for that number of lines after the current line\n\
15952 or the start of the range\n\
15953 FUNCTION, for the first line in that function,\n\
15954 FILE:FUNCTION, to distinguish among like-named static functions.\n\
15955 *ADDRESS, for the instruction at that address.\n\
15956 \n\
15957 The breakpoint will stop execution of the inferior whenever it executes\n\
15958 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
15959 range (including START-LOCATION and END-LOCATION)."));
15960
15961 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
15962 Set a dynamic printf at specified location.\n\
15963 dprintf location,format string,arg1,arg2,...\n\
15964 location may be a linespec, explicit, or address location.\n"
15965 "\n" LOCATION_HELP_STRING));
15966 set_cmd_completer (c, location_completer);
15967
15968 add_setshow_enum_cmd ("dprintf-style", class_support,
15969 dprintf_style_enums, &dprintf_style, _("\
15970 Set the style of usage for dynamic printf."), _("\
15971 Show the style of usage for dynamic printf."), _("\
15972 This setting chooses how GDB will do a dynamic printf.\n\
15973 If the value is \"gdb\", then the printing is done by GDB to its own\n\
15974 console, as with the \"printf\" command.\n\
15975 If the value is \"call\", the print is done by calling a function in your\n\
15976 program; by default printf(), but you can choose a different function or\n\
15977 output stream by setting dprintf-function and dprintf-channel."),
15978 update_dprintf_commands, NULL,
15979 &setlist, &showlist);
15980
15981 add_setshow_string_cmd ("dprintf-function", class_support,
15982 &dprintf_function, _("\
15983 Set the function to use for dynamic printf."), _("\
15984 Show the function to use for dynamic printf."), NULL,
15985 update_dprintf_commands, NULL,
15986 &setlist, &showlist);
15987
15988 add_setshow_string_cmd ("dprintf-channel", class_support,
15989 &dprintf_channel, _("\
15990 Set the channel to use for dynamic printf."), _("\
15991 Show the channel to use for dynamic printf."), NULL,
15992 update_dprintf_commands, NULL,
15993 &setlist, &showlist);
15994
15995 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
15996 &disconnected_dprintf, _("\
15997 Set whether dprintf continues after GDB disconnects."), _("\
15998 Show whether dprintf continues after GDB disconnects."), _("\
15999 Use this to let dprintf commands continue to hit and produce output\n\
16000 even if GDB disconnects or detaches from the target."),
16001 NULL,
16002 NULL,
16003 &setlist, &showlist);
16004
16005 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16006 Target agent only formatted printing, like the C \"printf\" function.\n\
16007 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
16008 This supports most C printf format specifications, like %s, %d, etc.\n\
16009 This is useful for formatted output in user-defined commands."));
16010
16011 automatic_hardware_breakpoints = true;
16012
16013 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
16014 "breakpoint");
16015 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
16016 "breakpoint");
16017 }