Return bool from breakpoint_ops::print_one
[binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2022 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include <ctype.h>
23 #include "hashtab.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "breakpoint.h"
27 #include "tracepoint.h"
28 #include "gdbtypes.h"
29 #include "expression.h"
30 #include "gdbcore.h"
31 #include "gdbcmd.h"
32 #include "value.h"
33 #include "command.h"
34 #include "inferior.h"
35 #include "infrun.h"
36 #include "gdbthread.h"
37 #include "target.h"
38 #include "language.h"
39 #include "gdb-demangle.h"
40 #include "filenames.h"
41 #include "annotate.h"
42 #include "symfile.h"
43 #include "objfiles.h"
44 #include "source.h"
45 #include "linespec.h"
46 #include "completer.h"
47 #include "ui-out.h"
48 #include "cli/cli-script.h"
49 #include "block.h"
50 #include "solib.h"
51 #include "solist.h"
52 #include "observable.h"
53 #include "memattr.h"
54 #include "ada-lang.h"
55 #include "top.h"
56 #include "valprint.h"
57 #include "jit.h"
58 #include "parser-defs.h"
59 #include "gdbsupport/gdb_regex.h"
60 #include "probe.h"
61 #include "cli/cli-utils.h"
62 #include "stack.h"
63 #include "ax-gdb.h"
64 #include "dummy-frame.h"
65 #include "interps.h"
66 #include "gdbsupport/format.h"
67 #include "thread-fsm.h"
68 #include "tid-parse.h"
69 #include "cli/cli-style.h"
70 #include "cli/cli-decode.h"
71
72 /* readline include files */
73 #include "readline/tilde.h"
74
75 /* readline defines this. */
76 #undef savestring
77
78 #include "mi/mi-common.h"
79 #include "extension.h"
80 #include <algorithm>
81 #include "progspace-and-thread.h"
82 #include "gdbsupport/array-view.h"
83 #include "gdbsupport/gdb_optional.h"
84
85 /* Prototypes for local functions. */
86
87 static void map_breakpoint_numbers (const char *,
88 gdb::function_view<void (breakpoint *)>);
89
90 static void breakpoint_re_set_default (struct breakpoint *);
91
92 static void
93 create_sals_from_location_default (struct event_location *location,
94 struct linespec_result *canonical,
95 enum bptype type_wanted);
96
97 static void create_breakpoints_sal_default (struct gdbarch *,
98 struct linespec_result *,
99 gdb::unique_xmalloc_ptr<char>,
100 gdb::unique_xmalloc_ptr<char>,
101 enum bptype,
102 enum bpdisp, int, int,
103 int,
104 const struct breakpoint_ops *,
105 int, int, int, unsigned);
106
107 static std::vector<symtab_and_line> decode_location_default
108 (struct breakpoint *b, struct event_location *location,
109 struct program_space *search_pspace);
110
111 static int can_use_hardware_watchpoint
112 (const std::vector<value_ref_ptr> &vals);
113
114 static void mention (struct breakpoint *);
115
116 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
117 enum bptype,
118 const struct breakpoint_ops *);
119 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
120 const struct symtab_and_line *);
121
122 /* This function is used in gdbtk sources and thus can not be made
123 static. */
124 static struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
125 struct symtab_and_line,
126 enum bptype,
127 const struct breakpoint_ops *);
128
129 static struct breakpoint *
130 momentary_breakpoint_from_master (struct breakpoint *orig,
131 enum bptype type,
132 const struct breakpoint_ops *ops,
133 int loc_enabled);
134
135 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
136
137 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
138 CORE_ADDR bpaddr,
139 enum bptype bptype);
140
141 static void describe_other_breakpoints (struct gdbarch *,
142 struct program_space *, CORE_ADDR,
143 struct obj_section *, int);
144
145 static int watchpoint_locations_match (struct bp_location *loc1,
146 struct bp_location *loc2);
147
148 static int breakpoint_locations_match (struct bp_location *loc1,
149 struct bp_location *loc2,
150 bool sw_hw_bps_match = false);
151
152 static int breakpoint_location_address_match (struct bp_location *bl,
153 const struct address_space *aspace,
154 CORE_ADDR addr);
155
156 static int breakpoint_location_address_range_overlap (struct bp_location *,
157 const address_space *,
158 CORE_ADDR, int);
159
160 static int remove_breakpoint (struct bp_location *);
161 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
162
163 static enum print_stop_action print_bp_stop_message (bpstat *bs);
164
165 static int hw_breakpoint_used_count (void);
166
167 static int hw_watchpoint_use_count (struct breakpoint *);
168
169 static int hw_watchpoint_used_count_others (struct breakpoint *except,
170 enum bptype type,
171 int *other_type_used);
172
173 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
174 int count);
175
176 static void decref_bp_location (struct bp_location **loc);
177
178 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
179
180 /* update_global_location_list's modes of operation wrt to whether to
181 insert locations now. */
182 enum ugll_insert_mode
183 {
184 /* Don't insert any breakpoint locations into the inferior, only
185 remove already-inserted locations that no longer should be
186 inserted. Functions that delete a breakpoint or breakpoints
187 should specify this mode, so that deleting a breakpoint doesn't
188 have the side effect of inserting the locations of other
189 breakpoints that are marked not-inserted, but should_be_inserted
190 returns true on them.
191
192 This behavior is useful is situations close to tear-down -- e.g.,
193 after an exec, while the target still has execution, but
194 breakpoint shadows of the previous executable image should *NOT*
195 be restored to the new image; or before detaching, where the
196 target still has execution and wants to delete breakpoints from
197 GDB's lists, and all breakpoints had already been removed from
198 the inferior. */
199 UGLL_DONT_INSERT,
200
201 /* May insert breakpoints iff breakpoints_should_be_inserted_now
202 claims breakpoints should be inserted now. */
203 UGLL_MAY_INSERT,
204
205 /* Insert locations now, irrespective of
206 breakpoints_should_be_inserted_now. E.g., say all threads are
207 stopped right now, and the user did "continue". We need to
208 insert breakpoints _before_ resuming the target, but
209 UGLL_MAY_INSERT wouldn't insert them, because
210 breakpoints_should_be_inserted_now returns false at that point,
211 as no thread is running yet. */
212 UGLL_INSERT
213 };
214
215 static void update_global_location_list (enum ugll_insert_mode);
216
217 static void update_global_location_list_nothrow (enum ugll_insert_mode);
218
219 static void insert_breakpoint_locations (void);
220
221 static void trace_pass_command (const char *, int);
222
223 static void set_tracepoint_count (int num);
224
225 static bool is_masked_watchpoint (const struct breakpoint *b);
226
227 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
228 otherwise. */
229
230 static int strace_marker_p (struct breakpoint *b);
231
232 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
233 that are implemented on top of software or hardware breakpoints
234 (user breakpoints, internal and momentary breakpoints, etc.). */
235 static struct breakpoint_ops bkpt_base_breakpoint_ops;
236
237 /* Internal breakpoints class type. */
238 static struct breakpoint_ops internal_breakpoint_ops;
239
240 /* Momentary breakpoints class type. */
241 static struct breakpoint_ops momentary_breakpoint_ops;
242
243 /* The breakpoint_ops structure to be used in regular user created
244 breakpoints. */
245 struct breakpoint_ops bkpt_breakpoint_ops;
246
247 /* Breakpoints set on probes. */
248 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
249
250 /* Tracepoints set on probes. */
251 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
252
253 /* Dynamic printf class type. */
254 struct breakpoint_ops dprintf_breakpoint_ops;
255
256 /* The 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 gdb_printf (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 gdb_printf (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 gdb_printf (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 gdb_printf (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 gdb_printf (file,
802 _("Breakpoint condition evaluation "
803 "mode is %s (currently %s).\n"),
804 value,
805 breakpoint_condition_evaluation_mode ());
806 else
807 gdb_printf (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 gdb_printf (_("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 gdb_printf (_("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 gdb_printf (_("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 gdb_printf (_("\
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 gdb_printf (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 gdb_printf (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 gdb_printf (tmp_error_stream,
2718 "Cannot insert breakpoint %d.\n",
2719 bl->owner->number);
2720 gdb_printf (tmp_error_stream,
2721 "Temporarily disabling shared "
2722 "library breakpoints:\n");
2723 }
2724 *disabled_breaks = 1;
2725 gdb_printf (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 gdb_printf (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 gdb_printf (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 gdb_printf (tmp_error_stream,
2752 "Cannot insert breakpoint %d.\n"
2753 "%s\n",
2754 bl->owner->number, message.c_str ());
2755 }
2756 else
2757 {
2758 gdb_printf (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 gdb_printf (_("\
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 = bp_objfile_data->overlay_msym.value_address ();
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 = bp_objfile_data->longjmp_msym[i].value_address ();
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 || (m.minsym->type () != mst_text
3443 && m.minsym->type () != 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 = bp_objfile_data->terminate_msym.value_address ();
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 = bp_objfile_data->exception_msym.value_address ();
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 /* See breakpoint.h. */
4629
4630 void
4631 print_solib_event (bool 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 (false);
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, function->value_block ()))
4917 within_current_scope = 0;
4918 }
4919
4920 if (within_current_scope)
4921 /* If we end up stopping, the current frame will get selected
4922 in normal_stop. So this call to select_frame won't affect
4923 the user. */
4924 select_frame (fr);
4925 }
4926
4927 if (within_current_scope)
4928 {
4929 /* We use value_{,free_to_}mark because it could be a *long*
4930 time before we return to the command level and call
4931 free_all_values. We can't call free_all_values because we
4932 might be in the middle of evaluating a function call. */
4933
4934 struct value *mark;
4935 struct value *new_val;
4936
4937 if (is_masked_watchpoint (b))
4938 /* Since we don't know the exact trigger address (from
4939 stopped_data_address), just tell the user we've triggered
4940 a mask watchpoint. */
4941 return WP_VALUE_CHANGED;
4942
4943 mark = value_mark ();
4944 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
4945 NULL, NULL, false);
4946
4947 if (b->val_bitsize != 0)
4948 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4949
4950 /* We use value_equal_contents instead of value_equal because
4951 the latter coerces an array to a pointer, thus comparing just
4952 the address of the array instead of its contents. This is
4953 not what we want. */
4954 if ((b->val != NULL) != (new_val != NULL)
4955 || (b->val != NULL && !value_equal_contents (b->val.get (),
4956 new_val)))
4957 {
4958 bs->old_val = b->val;
4959 b->val = release_value (new_val);
4960 b->val_valid = true;
4961 if (new_val != NULL)
4962 value_free_to_mark (mark);
4963 return WP_VALUE_CHANGED;
4964 }
4965 else
4966 {
4967 /* Nothing changed. */
4968 value_free_to_mark (mark);
4969 return WP_VALUE_NOT_CHANGED;
4970 }
4971 }
4972 else
4973 {
4974 /* This seems like the only logical thing to do because
4975 if we temporarily ignored the watchpoint, then when
4976 we reenter the block in which it is valid it contains
4977 garbage (in the case of a function, it may have two
4978 garbage values, one before and one after the prologue).
4979 So we can't even detect the first assignment to it and
4980 watch after that (since the garbage may or may not equal
4981 the first value assigned). */
4982 /* We print all the stop information in
4983 breakpoint_ops->print_it, but in this case, by the time we
4984 call breakpoint_ops->print_it this bp will be deleted
4985 already. So we have no choice but print the information
4986 here. */
4987
4988 SWITCH_THRU_ALL_UIS ()
4989 {
4990 struct ui_out *uiout = current_uiout;
4991
4992 if (uiout->is_mi_like_p ())
4993 uiout->field_string
4994 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
4995 uiout->message ("\nWatchpoint %pF deleted because the program has "
4996 "left the block in\n"
4997 "which its expression is valid.\n",
4998 signed_field ("wpnum", b->number));
4999 }
5000
5001 /* Make sure the watchpoint's commands aren't executed. */
5002 b->commands = NULL;
5003 watchpoint_del_at_next_stop (b);
5004
5005 return WP_DELETED;
5006 }
5007 }
5008
5009 /* Return true if it looks like target has stopped due to hitting
5010 breakpoint location BL. This function does not check if we should
5011 stop, only if BL explains the stop. */
5012
5013 static int
5014 bpstat_check_location (const struct bp_location *bl,
5015 const address_space *aspace, CORE_ADDR bp_addr,
5016 const target_waitstatus &ws)
5017 {
5018 struct breakpoint *b = bl->owner;
5019
5020 /* BL is from an existing breakpoint. */
5021 gdb_assert (b != NULL);
5022
5023 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5024 }
5025
5026 /* Determine if the watched values have actually changed, and we
5027 should stop. If not, set BS->stop to 0. */
5028
5029 static void
5030 bpstat_check_watchpoint (bpstat *bs)
5031 {
5032 const struct bp_location *bl;
5033 struct watchpoint *b;
5034
5035 /* BS is built for existing struct breakpoint. */
5036 bl = bs->bp_location_at.get ();
5037 gdb_assert (bl != NULL);
5038 b = (struct watchpoint *) bs->breakpoint_at;
5039 gdb_assert (b != NULL);
5040
5041 {
5042 int must_check_value = 0;
5043
5044 if (b->type == bp_watchpoint)
5045 /* For a software watchpoint, we must always check the
5046 watched value. */
5047 must_check_value = 1;
5048 else if (b->watchpoint_triggered == watch_triggered_yes)
5049 /* We have a hardware watchpoint (read, write, or access)
5050 and the target earlier reported an address watched by
5051 this watchpoint. */
5052 must_check_value = 1;
5053 else if (b->watchpoint_triggered == watch_triggered_unknown
5054 && b->type == bp_hardware_watchpoint)
5055 /* We were stopped by a hardware watchpoint, but the target could
5056 not report the data address. We must check the watchpoint's
5057 value. Access and read watchpoints are out of luck; without
5058 a data address, we can't figure it out. */
5059 must_check_value = 1;
5060
5061 if (must_check_value)
5062 {
5063 wp_check_result e;
5064
5065 try
5066 {
5067 e = watchpoint_check (bs);
5068 }
5069 catch (const gdb_exception &ex)
5070 {
5071 exception_fprintf (gdb_stderr, ex,
5072 "Error evaluating expression "
5073 "for watchpoint %d\n",
5074 b->number);
5075
5076 SWITCH_THRU_ALL_UIS ()
5077 {
5078 gdb_printf (_("Watchpoint %d deleted.\n"),
5079 b->number);
5080 }
5081 watchpoint_del_at_next_stop (b);
5082 e = WP_DELETED;
5083 }
5084
5085 switch (e)
5086 {
5087 case WP_DELETED:
5088 /* We've already printed what needs to be printed. */
5089 bs->print_it = print_it_done;
5090 /* Stop. */
5091 break;
5092 case WP_IGNORE:
5093 bs->print_it = print_it_noop;
5094 bs->stop = 0;
5095 break;
5096 case WP_VALUE_CHANGED:
5097 if (b->type == bp_read_watchpoint)
5098 {
5099 /* There are two cases to consider here:
5100
5101 1. We're watching the triggered memory for reads.
5102 In that case, trust the target, and always report
5103 the watchpoint hit to the user. Even though
5104 reads don't cause value changes, the value may
5105 have changed since the last time it was read, and
5106 since we're not trapping writes, we will not see
5107 those, and as such we should ignore our notion of
5108 old value.
5109
5110 2. We're watching the triggered memory for both
5111 reads and writes. There are two ways this may
5112 happen:
5113
5114 2.1. This is a target that can't break on data
5115 reads only, but can break on accesses (reads or
5116 writes), such as e.g., x86. We detect this case
5117 at the time we try to insert read watchpoints.
5118
5119 2.2. Otherwise, the target supports read
5120 watchpoints, but, the user set an access or write
5121 watchpoint watching the same memory as this read
5122 watchpoint.
5123
5124 If we're watching memory writes as well as reads,
5125 ignore watchpoint hits when we find that the
5126 value hasn't changed, as reads don't cause
5127 changes. This still gives false positives when
5128 the program writes the same value to memory as
5129 what there was already in memory (we will confuse
5130 it for a read), but it's much better than
5131 nothing. */
5132
5133 int other_write_watchpoint = 0;
5134
5135 if (bl->watchpoint_type == hw_read)
5136 {
5137 for (breakpoint *other_b : all_breakpoints ())
5138 if (other_b->type == bp_hardware_watchpoint
5139 || other_b->type == bp_access_watchpoint)
5140 {
5141 struct watchpoint *other_w =
5142 (struct watchpoint *) other_b;
5143
5144 if (other_w->watchpoint_triggered
5145 == watch_triggered_yes)
5146 {
5147 other_write_watchpoint = 1;
5148 break;
5149 }
5150 }
5151 }
5152
5153 if (other_write_watchpoint
5154 || bl->watchpoint_type == hw_access)
5155 {
5156 /* We're watching the same memory for writes,
5157 and the value changed since the last time we
5158 updated it, so this trap must be for a write.
5159 Ignore it. */
5160 bs->print_it = print_it_noop;
5161 bs->stop = 0;
5162 }
5163 }
5164 break;
5165 case WP_VALUE_NOT_CHANGED:
5166 if (b->type == bp_hardware_watchpoint
5167 || b->type == bp_watchpoint)
5168 {
5169 /* Don't stop: write watchpoints shouldn't fire if
5170 the value hasn't changed. */
5171 bs->print_it = print_it_noop;
5172 bs->stop = 0;
5173 }
5174 /* Stop. */
5175 break;
5176 default:
5177 /* Can't happen. */
5178 break;
5179 }
5180 }
5181 else /* must_check_value == 0 */
5182 {
5183 /* This is a case where some watchpoint(s) triggered, but
5184 not at the address of this watchpoint, or else no
5185 watchpoint triggered after all. So don't print
5186 anything for this watchpoint. */
5187 bs->print_it = print_it_noop;
5188 bs->stop = 0;
5189 }
5190 }
5191 }
5192
5193 /* For breakpoints that are currently marked as telling gdb to stop,
5194 check conditions (condition proper, frame, thread and ignore count)
5195 of breakpoint referred to by BS. If we should not stop for this
5196 breakpoint, set BS->stop to 0. */
5197
5198 static void
5199 bpstat_check_breakpoint_conditions (bpstat *bs, thread_info *thread)
5200 {
5201 const struct bp_location *bl;
5202 struct breakpoint *b;
5203 /* Assume stop. */
5204 bool condition_result = true;
5205 struct expression *cond;
5206
5207 gdb_assert (bs->stop);
5208
5209 /* BS is built for existing struct breakpoint. */
5210 bl = bs->bp_location_at.get ();
5211 gdb_assert (bl != NULL);
5212 b = bs->breakpoint_at;
5213 gdb_assert (b != NULL);
5214
5215 /* Even if the target evaluated the condition on its end and notified GDB, we
5216 need to do so again since GDB does not know if we stopped due to a
5217 breakpoint or a single step breakpoint. */
5218
5219 if (frame_id_p (b->frame_id)
5220 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5221 {
5222 bs->stop = 0;
5223 return;
5224 }
5225
5226 /* If this is a thread/task-specific breakpoint, don't waste cpu
5227 evaluating the condition if this isn't the specified
5228 thread/task. */
5229 if ((b->thread != -1 && b->thread != thread->global_num)
5230 || (b->task != 0 && b->task != ada_get_task_number (thread)))
5231 {
5232 bs->stop = 0;
5233 return;
5234 }
5235
5236 /* Evaluate extension language breakpoints that have a "stop" method
5237 implemented. */
5238 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5239
5240 if (is_watchpoint (b))
5241 {
5242 struct watchpoint *w = (struct watchpoint *) b;
5243
5244 cond = w->cond_exp.get ();
5245 }
5246 else
5247 cond = bl->cond.get ();
5248
5249 if (cond && b->disposition != disp_del_at_next_stop)
5250 {
5251 int within_current_scope = 1;
5252 struct watchpoint * w;
5253
5254 /* We use value_mark and value_free_to_mark because it could
5255 be a long time before we return to the command level and
5256 call free_all_values. We can't call free_all_values
5257 because we might be in the middle of evaluating a
5258 function call. */
5259 struct value *mark = value_mark ();
5260
5261 if (is_watchpoint (b))
5262 w = (struct watchpoint *) b;
5263 else
5264 w = NULL;
5265
5266 /* Need to select the frame, with all that implies so that
5267 the conditions will have the right context. Because we
5268 use the frame, we will not see an inlined function's
5269 variables when we arrive at a breakpoint at the start
5270 of the inlined function; the current frame will be the
5271 call site. */
5272 if (w == NULL || w->cond_exp_valid_block == NULL)
5273 select_frame (get_current_frame ());
5274 else
5275 {
5276 struct frame_info *frame;
5277
5278 /* For local watchpoint expressions, which particular
5279 instance of a local is being watched matters, so we
5280 keep track of the frame to evaluate the expression
5281 in. To evaluate the condition however, it doesn't
5282 really matter which instantiation of the function
5283 where the condition makes sense triggers the
5284 watchpoint. This allows an expression like "watch
5285 global if q > 10" set in `func', catch writes to
5286 global on all threads that call `func', or catch
5287 writes on all recursive calls of `func' by a single
5288 thread. We simply always evaluate the condition in
5289 the innermost frame that's executing where it makes
5290 sense to evaluate the condition. It seems
5291 intuitive. */
5292 frame = block_innermost_frame (w->cond_exp_valid_block);
5293 if (frame != NULL)
5294 select_frame (frame);
5295 else
5296 within_current_scope = 0;
5297 }
5298 if (within_current_scope)
5299 {
5300 try
5301 {
5302 condition_result = breakpoint_cond_eval (cond);
5303 }
5304 catch (const gdb_exception &ex)
5305 {
5306 exception_fprintf (gdb_stderr, ex,
5307 "Error in testing breakpoint condition:\n");
5308 }
5309 }
5310 else
5311 {
5312 warning (_("Watchpoint condition cannot be tested "
5313 "in the current scope"));
5314 /* If we failed to set the right context for this
5315 watchpoint, unconditionally report it. */
5316 }
5317 /* FIXME-someday, should give breakpoint #. */
5318 value_free_to_mark (mark);
5319 }
5320
5321 if (cond && !condition_result)
5322 {
5323 bs->stop = 0;
5324 }
5325 else if (b->ignore_count > 0)
5326 {
5327 b->ignore_count--;
5328 bs->stop = 0;
5329 /* Increase the hit count even though we don't stop. */
5330 ++(b->hit_count);
5331 gdb::observers::breakpoint_modified.notify (b);
5332 }
5333 }
5334
5335 /* Returns true if we need to track moribund locations of LOC's type
5336 on the current target. */
5337
5338 static int
5339 need_moribund_for_location_type (struct bp_location *loc)
5340 {
5341 return ((loc->loc_type == bp_loc_software_breakpoint
5342 && !target_supports_stopped_by_sw_breakpoint ())
5343 || (loc->loc_type == bp_loc_hardware_breakpoint
5344 && !target_supports_stopped_by_hw_breakpoint ()));
5345 }
5346
5347 /* See breakpoint.h. */
5348
5349 bpstat *
5350 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5351 const target_waitstatus &ws)
5352 {
5353 bpstat *bs_head = nullptr, **bs_link = &bs_head;
5354
5355 for (breakpoint *b : all_breakpoints ())
5356 {
5357 if (!breakpoint_enabled (b))
5358 continue;
5359
5360 for (bp_location *bl : b->locations ())
5361 {
5362 /* For hardware watchpoints, we look only at the first
5363 location. The watchpoint_check function will work on the
5364 entire expression, not the individual locations. For
5365 read watchpoints, the watchpoints_triggered function has
5366 checked all locations already. */
5367 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5368 break;
5369
5370 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
5371 continue;
5372
5373 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5374 continue;
5375
5376 /* Come here if it's a watchpoint, or if the break address
5377 matches. */
5378
5379 bpstat *bs = new bpstat (bl, &bs_link); /* Alloc a bpstat to
5380 explain stop. */
5381
5382 /* Assume we stop. Should we find a watchpoint that is not
5383 actually triggered, or if the condition of the breakpoint
5384 evaluates as false, we'll reset 'stop' to 0. */
5385 bs->stop = 1;
5386 bs->print = 1;
5387
5388 /* If this is a scope breakpoint, mark the associated
5389 watchpoint as triggered so that we will handle the
5390 out-of-scope event. We'll get to the watchpoint next
5391 iteration. */
5392 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5393 {
5394 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5395
5396 w->watchpoint_triggered = watch_triggered_yes;
5397 }
5398 }
5399 }
5400
5401 /* Check if a moribund breakpoint explains the stop. */
5402 if (!target_supports_stopped_by_sw_breakpoint ()
5403 || !target_supports_stopped_by_hw_breakpoint ())
5404 {
5405 for (bp_location *loc : moribund_locations)
5406 {
5407 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5408 && need_moribund_for_location_type (loc))
5409 {
5410 bpstat *bs = new bpstat (loc, &bs_link);
5411 /* For hits of moribund locations, we should just proceed. */
5412 bs->stop = 0;
5413 bs->print = 0;
5414 bs->print_it = print_it_noop;
5415 }
5416 }
5417 }
5418
5419 return bs_head;
5420 }
5421
5422 /* See breakpoint.h. */
5423
5424 bpstat *
5425 bpstat_stop_status (const address_space *aspace,
5426 CORE_ADDR bp_addr, thread_info *thread,
5427 const target_waitstatus &ws,
5428 bpstat *stop_chain)
5429 {
5430 struct breakpoint *b = NULL;
5431 /* First item of allocated bpstat's. */
5432 bpstat *bs_head = stop_chain;
5433 bpstat *bs;
5434 int need_remove_insert;
5435 int removed_any;
5436
5437 /* First, build the bpstat chain with locations that explain a
5438 target stop, while being careful to not set the target running,
5439 as that may invalidate locations (in particular watchpoint
5440 locations are recreated). Resuming will happen here with
5441 breakpoint conditions or watchpoint expressions that include
5442 inferior function calls. */
5443 if (bs_head == NULL)
5444 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5445
5446 /* A bit of special processing for shlib breakpoints. We need to
5447 process solib loading here, so that the lists of loaded and
5448 unloaded libraries are correct before we handle "catch load" and
5449 "catch unload". */
5450 for (bs = bs_head; bs != NULL; bs = bs->next)
5451 {
5452 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5453 {
5454 handle_solib_event ();
5455 break;
5456 }
5457 }
5458
5459 /* Now go through the locations that caused the target to stop, and
5460 check whether we're interested in reporting this stop to higher
5461 layers, or whether we should resume the target transparently. */
5462
5463 removed_any = 0;
5464
5465 for (bs = bs_head; bs != NULL; bs = bs->next)
5466 {
5467 if (!bs->stop)
5468 continue;
5469
5470 b = bs->breakpoint_at;
5471 b->ops->check_status (bs);
5472 if (bs->stop)
5473 {
5474 bpstat_check_breakpoint_conditions (bs, thread);
5475
5476 if (bs->stop)
5477 {
5478 ++(b->hit_count);
5479
5480 /* We will stop here. */
5481 if (b->disposition == disp_disable)
5482 {
5483 --(b->enable_count);
5484 if (b->enable_count <= 0)
5485 b->enable_state = bp_disabled;
5486 removed_any = 1;
5487 }
5488 gdb::observers::breakpoint_modified.notify (b);
5489 if (b->silent)
5490 bs->print = 0;
5491 bs->commands = b->commands;
5492 if (command_line_is_silent (bs->commands
5493 ? bs->commands.get () : NULL))
5494 bs->print = 0;
5495
5496 b->ops->after_condition_true (bs);
5497 }
5498
5499 }
5500
5501 /* Print nothing for this entry if we don't stop or don't
5502 print. */
5503 if (!bs->stop || !bs->print)
5504 bs->print_it = print_it_noop;
5505 }
5506
5507 /* If we aren't stopping, the value of some hardware watchpoint may
5508 not have changed, but the intermediate memory locations we are
5509 watching may have. Don't bother if we're stopping; this will get
5510 done later. */
5511 need_remove_insert = 0;
5512 if (! bpstat_causes_stop (bs_head))
5513 for (bs = bs_head; bs != NULL; bs = bs->next)
5514 if (!bs->stop
5515 && bs->breakpoint_at
5516 && is_hardware_watchpoint (bs->breakpoint_at))
5517 {
5518 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5519
5520 update_watchpoint (w, 0 /* don't reparse. */);
5521 need_remove_insert = 1;
5522 }
5523
5524 if (need_remove_insert)
5525 update_global_location_list (UGLL_MAY_INSERT);
5526 else if (removed_any)
5527 update_global_location_list (UGLL_DONT_INSERT);
5528
5529 return bs_head;
5530 }
5531
5532 /* See breakpoint.h. */
5533
5534 bpstat *
5535 bpstat_stop_status_nowatch (const address_space *aspace, CORE_ADDR bp_addr,
5536 thread_info *thread, const target_waitstatus &ws)
5537 {
5538 gdb_assert (!target_stopped_by_watchpoint ());
5539
5540 /* Clear all watchpoints' 'watchpoint_triggered' value from a
5541 previous stop to avoid confusing bpstat_stop_status. */
5542 watchpoints_triggered (ws);
5543
5544 return bpstat_stop_status (aspace, bp_addr, thread, ws);
5545 }
5546
5547 static void
5548 handle_jit_event (CORE_ADDR address)
5549 {
5550 struct gdbarch *gdbarch;
5551
5552 infrun_debug_printf ("handling bp_jit_event");
5553
5554 /* Switch terminal for any messages produced by
5555 breakpoint_re_set. */
5556 target_terminal::ours_for_output ();
5557
5558 gdbarch = get_frame_arch (get_current_frame ());
5559 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5560 thus it is expected that its objectfile can be found through
5561 minimal symbol lookup. If it doesn't work (and assert fails), it
5562 most likely means that `jit_breakpoint_re_set` was changes and this
5563 function needs to be updated too. */
5564 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5565 gdb_assert (jit_bp_sym.objfile != nullptr);
5566 jit_event_handler (gdbarch, jit_bp_sym.objfile);
5567
5568 target_terminal::inferior ();
5569 }
5570
5571 /* Prepare WHAT final decision for infrun. */
5572
5573 /* Decide what infrun needs to do with this bpstat. */
5574
5575 struct bpstat_what
5576 bpstat_what (bpstat *bs_head)
5577 {
5578 struct bpstat_what retval;
5579 bpstat *bs;
5580
5581 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5582 retval.call_dummy = STOP_NONE;
5583 retval.is_longjmp = false;
5584
5585 for (bs = bs_head; bs != NULL; bs = bs->next)
5586 {
5587 /* Extract this BS's action. After processing each BS, we check
5588 if its action overrides all we've seem so far. */
5589 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5590 enum bptype bptype;
5591
5592 if (bs->breakpoint_at == NULL)
5593 {
5594 /* I suspect this can happen if it was a momentary
5595 breakpoint which has since been deleted. */
5596 bptype = bp_none;
5597 }
5598 else
5599 bptype = bs->breakpoint_at->type;
5600
5601 switch (bptype)
5602 {
5603 case bp_none:
5604 break;
5605 case bp_breakpoint:
5606 case bp_hardware_breakpoint:
5607 case bp_single_step:
5608 case bp_until:
5609 case bp_finish:
5610 case bp_shlib_event:
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 this_action = BPSTAT_WHAT_SINGLE;
5620 break;
5621 case bp_watchpoint:
5622 case bp_hardware_watchpoint:
5623 case bp_read_watchpoint:
5624 case bp_access_watchpoint:
5625 if (bs->stop)
5626 {
5627 if (bs->print)
5628 this_action = BPSTAT_WHAT_STOP_NOISY;
5629 else
5630 this_action = BPSTAT_WHAT_STOP_SILENT;
5631 }
5632 else
5633 {
5634 /* There was a watchpoint, but we're not stopping.
5635 This requires no further action. */
5636 }
5637 break;
5638 case bp_longjmp:
5639 case bp_longjmp_call_dummy:
5640 case bp_exception:
5641 if (bs->stop)
5642 {
5643 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5644 retval.is_longjmp = bptype != bp_exception;
5645 }
5646 else
5647 this_action = BPSTAT_WHAT_SINGLE;
5648 break;
5649 case bp_longjmp_resume:
5650 case bp_exception_resume:
5651 if (bs->stop)
5652 {
5653 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5654 retval.is_longjmp = bptype == bp_longjmp_resume;
5655 }
5656 else
5657 this_action = BPSTAT_WHAT_SINGLE;
5658 break;
5659 case bp_step_resume:
5660 if (bs->stop)
5661 this_action = BPSTAT_WHAT_STEP_RESUME;
5662 else
5663 {
5664 /* It is for the wrong frame. */
5665 this_action = BPSTAT_WHAT_SINGLE;
5666 }
5667 break;
5668 case bp_hp_step_resume:
5669 if (bs->stop)
5670 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5671 else
5672 {
5673 /* It is for the wrong frame. */
5674 this_action = BPSTAT_WHAT_SINGLE;
5675 }
5676 break;
5677 case bp_watchpoint_scope:
5678 case bp_thread_event:
5679 case bp_overlay_event:
5680 case bp_longjmp_master:
5681 case bp_std_terminate_master:
5682 case bp_exception_master:
5683 this_action = BPSTAT_WHAT_SINGLE;
5684 break;
5685 case bp_catchpoint:
5686 if (bs->stop)
5687 {
5688 if (bs->print)
5689 this_action = BPSTAT_WHAT_STOP_NOISY;
5690 else
5691 this_action = BPSTAT_WHAT_STOP_SILENT;
5692 }
5693 else
5694 {
5695 /* Some catchpoints are implemented with breakpoints.
5696 For those, we need to step over the breakpoint. */
5697 if (bs->bp_location_at->loc_type != bp_loc_other)
5698 this_action = BPSTAT_WHAT_SINGLE;
5699 }
5700 break;
5701 case bp_jit_event:
5702 this_action = BPSTAT_WHAT_SINGLE;
5703 break;
5704 case bp_call_dummy:
5705 /* Make sure the action is stop (silent or noisy),
5706 so infrun.c pops the dummy frame. */
5707 retval.call_dummy = STOP_STACK_DUMMY;
5708 this_action = BPSTAT_WHAT_STOP_SILENT;
5709 break;
5710 case bp_std_terminate:
5711 /* Make sure the action is stop (silent or noisy),
5712 so infrun.c pops the dummy frame. */
5713 retval.call_dummy = STOP_STD_TERMINATE;
5714 this_action = BPSTAT_WHAT_STOP_SILENT;
5715 break;
5716 case bp_tracepoint:
5717 case bp_fast_tracepoint:
5718 case bp_static_tracepoint:
5719 /* Tracepoint hits should not be reported back to GDB, and
5720 if one got through somehow, it should have been filtered
5721 out already. */
5722 internal_error (__FILE__, __LINE__,
5723 _("bpstat_what: tracepoint encountered"));
5724 break;
5725 case bp_gnu_ifunc_resolver:
5726 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5727 this_action = BPSTAT_WHAT_SINGLE;
5728 break;
5729 case bp_gnu_ifunc_resolver_return:
5730 /* The breakpoint will be removed, execution will restart from the
5731 PC of the former breakpoint. */
5732 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5733 break;
5734
5735 case bp_dprintf:
5736 if (bs->stop)
5737 this_action = BPSTAT_WHAT_STOP_SILENT;
5738 else
5739 this_action = BPSTAT_WHAT_SINGLE;
5740 break;
5741
5742 default:
5743 internal_error (__FILE__, __LINE__,
5744 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5745 }
5746
5747 retval.main_action = std::max (retval.main_action, this_action);
5748 }
5749
5750 return retval;
5751 }
5752
5753 void
5754 bpstat_run_callbacks (bpstat *bs_head)
5755 {
5756 bpstat *bs;
5757
5758 for (bs = bs_head; bs != NULL; bs = bs->next)
5759 {
5760 struct breakpoint *b = bs->breakpoint_at;
5761
5762 if (b == NULL)
5763 continue;
5764 switch (b->type)
5765 {
5766 case bp_jit_event:
5767 handle_jit_event (bs->bp_location_at->address);
5768 break;
5769 case bp_gnu_ifunc_resolver:
5770 gnu_ifunc_resolver_stop (b);
5771 break;
5772 case bp_gnu_ifunc_resolver_return:
5773 gnu_ifunc_resolver_return_stop (b);
5774 break;
5775 }
5776 }
5777 }
5778
5779 /* See breakpoint.h. */
5780
5781 bool
5782 bpstat_should_step ()
5783 {
5784 for (breakpoint *b : all_breakpoints ())
5785 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5786 return true;
5787
5788 return false;
5789 }
5790
5791 /* See breakpoint.h. */
5792
5793 bool
5794 bpstat_causes_stop (bpstat *bs)
5795 {
5796 for (; bs != NULL; bs = bs->next)
5797 if (bs->stop)
5798 return true;
5799
5800 return false;
5801 }
5802
5803 \f
5804
5805 /* Compute a number of spaces suitable to indent the next line
5806 so it starts at the position corresponding to the table column
5807 named COL_NAME in the currently active table of UIOUT. */
5808
5809 static int
5810 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5811 {
5812 int i, total_width, width, align;
5813 const char *text;
5814
5815 total_width = 0;
5816 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5817 {
5818 if (strcmp (text, col_name) == 0)
5819 return total_width;
5820
5821 total_width += width + 1;
5822 }
5823
5824 return 0;
5825 }
5826
5827 /* Determine if the locations of this breakpoint will have their conditions
5828 evaluated by the target, host or a mix of both. Returns the following:
5829
5830 "host": Host evals condition.
5831 "host or target": Host or Target evals condition.
5832 "target": Target evals condition.
5833 */
5834
5835 static const char *
5836 bp_condition_evaluator (struct breakpoint *b)
5837 {
5838 char host_evals = 0;
5839 char target_evals = 0;
5840
5841 if (!b)
5842 return NULL;
5843
5844 if (!is_breakpoint (b))
5845 return NULL;
5846
5847 if (gdb_evaluates_breakpoint_condition_p ()
5848 || !target_supports_evaluation_of_breakpoint_conditions ())
5849 return condition_evaluation_host;
5850
5851 for (bp_location *bl : b->locations ())
5852 {
5853 if (bl->cond_bytecode)
5854 target_evals++;
5855 else
5856 host_evals++;
5857 }
5858
5859 if (host_evals && target_evals)
5860 return condition_evaluation_both;
5861 else if (target_evals)
5862 return condition_evaluation_target;
5863 else
5864 return condition_evaluation_host;
5865 }
5866
5867 /* Determine the breakpoint location's condition evaluator. This is
5868 similar to bp_condition_evaluator, but for locations. */
5869
5870 static const char *
5871 bp_location_condition_evaluator (struct bp_location *bl)
5872 {
5873 if (bl && !is_breakpoint (bl->owner))
5874 return NULL;
5875
5876 if (gdb_evaluates_breakpoint_condition_p ()
5877 || !target_supports_evaluation_of_breakpoint_conditions ())
5878 return condition_evaluation_host;
5879
5880 if (bl && bl->cond_bytecode)
5881 return condition_evaluation_target;
5882 else
5883 return condition_evaluation_host;
5884 }
5885
5886 /* Print the LOC location out of the list of B->LOC locations. */
5887
5888 static void
5889 print_breakpoint_location (struct breakpoint *b,
5890 struct bp_location *loc)
5891 {
5892 struct ui_out *uiout = current_uiout;
5893
5894 scoped_restore_current_program_space restore_pspace;
5895
5896 if (loc != NULL && loc->shlib_disabled)
5897 loc = NULL;
5898
5899 if (loc != NULL)
5900 set_current_program_space (loc->pspace);
5901
5902 if (b->display_canonical)
5903 uiout->field_string ("what", event_location_to_string (b->location.get ()));
5904 else if (loc && loc->symtab)
5905 {
5906 const struct symbol *sym = loc->symbol;
5907
5908 if (sym)
5909 {
5910 uiout->text ("in ");
5911 uiout->field_string ("func", sym->print_name (),
5912 function_name_style.style ());
5913 uiout->text (" ");
5914 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5915 uiout->text ("at ");
5916 }
5917 uiout->field_string ("file",
5918 symtab_to_filename_for_display (loc->symtab),
5919 file_name_style.style ());
5920 uiout->text (":");
5921
5922 if (uiout->is_mi_like_p ())
5923 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
5924
5925 uiout->field_signed ("line", loc->line_number);
5926 }
5927 else if (loc)
5928 {
5929 string_file stb;
5930
5931 print_address_symbolic (loc->gdbarch, loc->address, &stb,
5932 demangle, "");
5933 uiout->field_stream ("at", stb);
5934 }
5935 else
5936 {
5937 uiout->field_string ("pending",
5938 event_location_to_string (b->location.get ()));
5939 /* If extra_string is available, it could be holding a condition
5940 or dprintf arguments. In either case, make sure it is printed,
5941 too, but only for non-MI streams. */
5942 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
5943 {
5944 if (b->type == bp_dprintf)
5945 uiout->text (",");
5946 else
5947 uiout->text (" ");
5948 uiout->text (b->extra_string.get ());
5949 }
5950 }
5951
5952 if (loc && is_breakpoint (b)
5953 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5954 && bp_condition_evaluator (b) == condition_evaluation_both)
5955 {
5956 uiout->text (" (");
5957 uiout->field_string ("evaluated-by",
5958 bp_location_condition_evaluator (loc));
5959 uiout->text (")");
5960 }
5961 }
5962
5963 static const char *
5964 bptype_string (enum bptype type)
5965 {
5966 struct ep_type_description
5967 {
5968 enum bptype type;
5969 const char *description;
5970 };
5971 static struct ep_type_description bptypes[] =
5972 {
5973 {bp_none, "?deleted?"},
5974 {bp_breakpoint, "breakpoint"},
5975 {bp_hardware_breakpoint, "hw breakpoint"},
5976 {bp_single_step, "sw single-step"},
5977 {bp_until, "until"},
5978 {bp_finish, "finish"},
5979 {bp_watchpoint, "watchpoint"},
5980 {bp_hardware_watchpoint, "hw watchpoint"},
5981 {bp_read_watchpoint, "read watchpoint"},
5982 {bp_access_watchpoint, "acc watchpoint"},
5983 {bp_longjmp, "longjmp"},
5984 {bp_longjmp_resume, "longjmp resume"},
5985 {bp_longjmp_call_dummy, "longjmp for call dummy"},
5986 {bp_exception, "exception"},
5987 {bp_exception_resume, "exception resume"},
5988 {bp_step_resume, "step resume"},
5989 {bp_hp_step_resume, "high-priority step resume"},
5990 {bp_watchpoint_scope, "watchpoint scope"},
5991 {bp_call_dummy, "call dummy"},
5992 {bp_std_terminate, "std::terminate"},
5993 {bp_shlib_event, "shlib events"},
5994 {bp_thread_event, "thread events"},
5995 {bp_overlay_event, "overlay events"},
5996 {bp_longjmp_master, "longjmp master"},
5997 {bp_std_terminate_master, "std::terminate master"},
5998 {bp_exception_master, "exception master"},
5999 {bp_catchpoint, "catchpoint"},
6000 {bp_tracepoint, "tracepoint"},
6001 {bp_fast_tracepoint, "fast tracepoint"},
6002 {bp_static_tracepoint, "static tracepoint"},
6003 {bp_dprintf, "dprintf"},
6004 {bp_jit_event, "jit events"},
6005 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6006 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6007 };
6008
6009 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6010 || ((int) type != bptypes[(int) type].type))
6011 internal_error (__FILE__, __LINE__,
6012 _("bptypes table does not describe type #%d."),
6013 (int) type);
6014
6015 return bptypes[(int) type].description;
6016 }
6017
6018 /* For MI, output a field named 'thread-groups' with a list as the value.
6019 For CLI, prefix the list with the string 'inf'. */
6020
6021 static void
6022 output_thread_groups (struct ui_out *uiout,
6023 const char *field_name,
6024 const std::vector<int> &inf_nums,
6025 int mi_only)
6026 {
6027 int is_mi = uiout->is_mi_like_p ();
6028
6029 /* For backward compatibility, don't display inferiors in CLI unless
6030 there are several. Always display them for MI. */
6031 if (!is_mi && mi_only)
6032 return;
6033
6034 ui_out_emit_list list_emitter (uiout, field_name);
6035
6036 for (size_t i = 0; i < inf_nums.size (); i++)
6037 {
6038 if (is_mi)
6039 {
6040 char mi_group[10];
6041
6042 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
6043 uiout->field_string (NULL, mi_group);
6044 }
6045 else
6046 {
6047 if (i == 0)
6048 uiout->text (" inf ");
6049 else
6050 uiout->text (", ");
6051
6052 uiout->text (plongest (inf_nums[i]));
6053 }
6054 }
6055 }
6056
6057 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6058 instead of going via breakpoint_ops::print_one. This makes "maint
6059 info breakpoints" show the software breakpoint locations of
6060 catchpoints, which are considered internal implementation
6061 detail. Returns true if RAW_LOC is false and if the breakpoint's
6062 print_one method did something; false otherwise. */
6063
6064 static bool
6065 print_one_breakpoint_location (struct breakpoint *b,
6066 struct bp_location *loc,
6067 int loc_number,
6068 struct bp_location **last_loc,
6069 int allflag, bool raw_loc)
6070 {
6071 struct command_line *l;
6072 static char bpenables[] = "nynny";
6073
6074 struct ui_out *uiout = current_uiout;
6075 int header_of_multiple = 0;
6076 int part_of_multiple = (loc != NULL);
6077 struct value_print_options opts;
6078
6079 get_user_print_options (&opts);
6080
6081 gdb_assert (!loc || loc_number != 0);
6082 /* See comment in print_one_breakpoint concerning treatment of
6083 breakpoints with single disabled location. */
6084 if (loc == NULL
6085 && (b->loc != NULL
6086 && (b->loc->next != NULL
6087 || !b->loc->enabled || b->loc->disabled_by_cond)))
6088 header_of_multiple = 1;
6089 if (loc == NULL)
6090 loc = b->loc;
6091
6092 annotate_record ();
6093
6094 /* 1 */
6095 annotate_field (0);
6096 if (part_of_multiple)
6097 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6098 else
6099 uiout->field_signed ("number", b->number);
6100
6101 /* 2 */
6102 annotate_field (1);
6103 if (part_of_multiple)
6104 uiout->field_skip ("type");
6105 else
6106 uiout->field_string ("type", bptype_string (b->type));
6107
6108 /* 3 */
6109 annotate_field (2);
6110 if (part_of_multiple)
6111 uiout->field_skip ("disp");
6112 else
6113 uiout->field_string ("disp", bpdisp_text (b->disposition));
6114
6115 /* 4 */
6116 annotate_field (3);
6117 /* For locations that are disabled because of an invalid condition,
6118 display "N*" on CLI, where "*" refers to a footnote below the
6119 table. For MI, simply display a "N" without a footnote. */
6120 const char *N = (uiout->is_mi_like_p ()) ? "N" : "N*";
6121 if (part_of_multiple)
6122 uiout->field_string ("enabled", (loc->disabled_by_cond ? N
6123 : (loc->enabled ? "y" : "n")));
6124 else
6125 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6126
6127 /* 5 and 6 */
6128 bool result = false;
6129 if (!raw_loc && b->ops != NULL && b->ops->print_one (b, last_loc))
6130 result = true;
6131 else
6132 {
6133 if (is_watchpoint (b))
6134 {
6135 struct watchpoint *w = (struct watchpoint *) b;
6136
6137 /* Field 4, the address, is omitted (which makes the columns
6138 not line up too nicely with the headers, but the effect
6139 is relatively readable). */
6140 if (opts.addressprint)
6141 uiout->field_skip ("addr");
6142 annotate_field (5);
6143 uiout->field_string ("what", w->exp_string.get ());
6144 }
6145 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6146 || is_ada_exception_catchpoint (b))
6147 {
6148 if (opts.addressprint)
6149 {
6150 annotate_field (4);
6151 if (header_of_multiple)
6152 uiout->field_string ("addr", "<MULTIPLE>",
6153 metadata_style.style ());
6154 else if (b->loc == NULL || loc->shlib_disabled)
6155 uiout->field_string ("addr", "<PENDING>",
6156 metadata_style.style ());
6157 else
6158 uiout->field_core_addr ("addr",
6159 loc->gdbarch, loc->address);
6160 }
6161 annotate_field (5);
6162 if (!header_of_multiple)
6163 print_breakpoint_location (b, loc);
6164 if (b->loc)
6165 *last_loc = b->loc;
6166 }
6167 }
6168
6169 if (loc != NULL && !header_of_multiple)
6170 {
6171 std::vector<int> inf_nums;
6172 int mi_only = 1;
6173
6174 for (inferior *inf : all_inferiors ())
6175 {
6176 if (inf->pspace == loc->pspace)
6177 inf_nums.push_back (inf->num);
6178 }
6179
6180 /* For backward compatibility, don't display inferiors in CLI unless
6181 there are several. Always display for MI. */
6182 if (allflag
6183 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6184 && (program_spaces.size () > 1
6185 || number_of_inferiors () > 1)
6186 /* LOC is for existing B, it cannot be in
6187 moribund_locations and thus having NULL OWNER. */
6188 && loc->owner->type != bp_catchpoint))
6189 mi_only = 0;
6190 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6191 }
6192
6193 if (!part_of_multiple)
6194 {
6195 if (b->thread != -1)
6196 {
6197 /* FIXME: This seems to be redundant and lost here; see the
6198 "stop only in" line a little further down. */
6199 uiout->text (" thread ");
6200 uiout->field_signed ("thread", b->thread);
6201 }
6202 else if (b->task != 0)
6203 {
6204 uiout->text (" task ");
6205 uiout->field_signed ("task", b->task);
6206 }
6207 }
6208
6209 uiout->text ("\n");
6210
6211 if (!part_of_multiple)
6212 b->ops->print_one_detail (b, uiout);
6213
6214 if (part_of_multiple && frame_id_p (b->frame_id))
6215 {
6216 annotate_field (6);
6217 uiout->text ("\tstop only in stack frame at ");
6218 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6219 the frame ID. */
6220 uiout->field_core_addr ("frame",
6221 b->gdbarch, b->frame_id.stack_addr);
6222 uiout->text ("\n");
6223 }
6224
6225 if (!part_of_multiple && b->cond_string)
6226 {
6227 annotate_field (7);
6228 if (is_tracepoint (b))
6229 uiout->text ("\ttrace only if ");
6230 else
6231 uiout->text ("\tstop only if ");
6232 uiout->field_string ("cond", b->cond_string.get ());
6233
6234 /* Print whether the target is doing the breakpoint's condition
6235 evaluation. If GDB is doing the evaluation, don't print anything. */
6236 if (is_breakpoint (b)
6237 && breakpoint_condition_evaluation_mode ()
6238 == condition_evaluation_target)
6239 {
6240 uiout->message (" (%pF evals)",
6241 string_field ("evaluated-by",
6242 bp_condition_evaluator (b)));
6243 }
6244 uiout->text ("\n");
6245 }
6246
6247 if (!part_of_multiple && b->thread != -1)
6248 {
6249 /* FIXME should make an annotation for this. */
6250 uiout->text ("\tstop only in thread ");
6251 if (uiout->is_mi_like_p ())
6252 uiout->field_signed ("thread", b->thread);
6253 else
6254 {
6255 struct thread_info *thr = find_thread_global_id (b->thread);
6256
6257 uiout->field_string ("thread", print_thread_id (thr));
6258 }
6259 uiout->text ("\n");
6260 }
6261
6262 if (!part_of_multiple)
6263 {
6264 if (b->hit_count)
6265 {
6266 /* FIXME should make an annotation for this. */
6267 if (is_catchpoint (b))
6268 uiout->text ("\tcatchpoint");
6269 else if (is_tracepoint (b))
6270 uiout->text ("\ttracepoint");
6271 else
6272 uiout->text ("\tbreakpoint");
6273 uiout->text (" already hit ");
6274 uiout->field_signed ("times", b->hit_count);
6275 if (b->hit_count == 1)
6276 uiout->text (" time\n");
6277 else
6278 uiout->text (" times\n");
6279 }
6280 else
6281 {
6282 /* Output the count also if it is zero, but only if this is mi. */
6283 if (uiout->is_mi_like_p ())
6284 uiout->field_signed ("times", b->hit_count);
6285 }
6286 }
6287
6288 if (!part_of_multiple && b->ignore_count)
6289 {
6290 annotate_field (8);
6291 uiout->message ("\tignore next %pF hits\n",
6292 signed_field ("ignore", b->ignore_count));
6293 }
6294
6295 /* Note that an enable count of 1 corresponds to "enable once"
6296 behavior, which is reported by the combination of enablement and
6297 disposition, so we don't need to mention it here. */
6298 if (!part_of_multiple && b->enable_count > 1)
6299 {
6300 annotate_field (8);
6301 uiout->text ("\tdisable after ");
6302 /* Tweak the wording to clarify that ignore and enable counts
6303 are distinct, and have additive effect. */
6304 if (b->ignore_count)
6305 uiout->text ("additional ");
6306 else
6307 uiout->text ("next ");
6308 uiout->field_signed ("enable", b->enable_count);
6309 uiout->text (" hits\n");
6310 }
6311
6312 if (!part_of_multiple && is_tracepoint (b))
6313 {
6314 struct tracepoint *tp = (struct tracepoint *) b;
6315
6316 if (tp->traceframe_usage)
6317 {
6318 uiout->text ("\ttrace buffer usage ");
6319 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6320 uiout->text (" bytes\n");
6321 }
6322 }
6323
6324 l = b->commands ? b->commands.get () : NULL;
6325 if (!part_of_multiple && l)
6326 {
6327 annotate_field (9);
6328 ui_out_emit_tuple tuple_emitter (uiout, "script");
6329 print_command_lines (uiout, l, 4);
6330 }
6331
6332 if (is_tracepoint (b))
6333 {
6334 struct tracepoint *t = (struct tracepoint *) b;
6335
6336 if (!part_of_multiple && t->pass_count)
6337 {
6338 annotate_field (10);
6339 uiout->text ("\tpass count ");
6340 uiout->field_signed ("pass", t->pass_count);
6341 uiout->text (" \n");
6342 }
6343
6344 /* Don't display it when tracepoint or tracepoint location is
6345 pending. */
6346 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6347 {
6348 annotate_field (11);
6349
6350 if (uiout->is_mi_like_p ())
6351 uiout->field_string ("installed",
6352 loc->inserted ? "y" : "n");
6353 else
6354 {
6355 if (loc->inserted)
6356 uiout->text ("\t");
6357 else
6358 uiout->text ("\tnot ");
6359 uiout->text ("installed on target\n");
6360 }
6361 }
6362 }
6363
6364 if (uiout->is_mi_like_p () && !part_of_multiple)
6365 {
6366 if (is_watchpoint (b))
6367 {
6368 struct watchpoint *w = (struct watchpoint *) b;
6369
6370 uiout->field_string ("original-location", w->exp_string.get ());
6371 }
6372 else if (b->location != NULL
6373 && event_location_to_string (b->location.get ()) != NULL)
6374 uiout->field_string ("original-location",
6375 event_location_to_string (b->location.get ()));
6376 }
6377
6378 return result;
6379 }
6380
6381 /* See breakpoint.h. */
6382
6383 bool fix_multi_location_breakpoint_output_globally = false;
6384
6385 static void
6386 print_one_breakpoint (struct breakpoint *b,
6387 struct bp_location **last_loc,
6388 int allflag)
6389 {
6390 struct ui_out *uiout = current_uiout;
6391 bool use_fixed_output
6392 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6393 || fix_multi_location_breakpoint_output_globally);
6394
6395 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6396 bool printed = print_one_breakpoint_location (b, NULL, 0, last_loc,
6397 allflag, false);
6398
6399 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6400 are outside. */
6401 if (!use_fixed_output)
6402 bkpt_tuple_emitter.reset ();
6403
6404 /* If this breakpoint has custom print function,
6405 it's already printed. Otherwise, print individual
6406 locations, if any. */
6407 if (!printed || allflag)
6408 {
6409 /* If breakpoint has a single location that is disabled, we
6410 print it as if it had several locations, since otherwise it's
6411 hard to represent "breakpoint enabled, location disabled"
6412 situation.
6413
6414 Note that while hardware watchpoints have several locations
6415 internally, that's not a property exposed to users.
6416
6417 Likewise, while catchpoints may be implemented with
6418 breakpoints (e.g., catch throw), that's not a property
6419 exposed to users. We do however display the internal
6420 breakpoint locations with "maint info breakpoints". */
6421 if (!is_hardware_watchpoint (b)
6422 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6423 || is_ada_exception_catchpoint (b))
6424 && (allflag
6425 || (b->loc && (b->loc->next
6426 || !b->loc->enabled
6427 || b->loc->disabled_by_cond))))
6428 {
6429 gdb::optional<ui_out_emit_list> locations_list;
6430
6431 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6432 MI record. For later versions, place breakpoint locations in a
6433 list. */
6434 if (uiout->is_mi_like_p () && use_fixed_output)
6435 locations_list.emplace (uiout, "locations");
6436
6437 int n = 1;
6438 for (bp_location *loc : b->locations ())
6439 {
6440 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6441 print_one_breakpoint_location (b, loc, n, last_loc,
6442 allflag, allflag);
6443 n++;
6444 }
6445 }
6446 }
6447 }
6448
6449 static int
6450 breakpoint_address_bits (struct breakpoint *b)
6451 {
6452 int print_address_bits = 0;
6453
6454 /* Software watchpoints that aren't watching memory don't have an
6455 address to print. */
6456 if (is_no_memory_software_watchpoint (b))
6457 return 0;
6458
6459 for (bp_location *loc : b->locations ())
6460 {
6461 int addr_bit;
6462
6463 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6464 if (addr_bit > print_address_bits)
6465 print_address_bits = addr_bit;
6466 }
6467
6468 return print_address_bits;
6469 }
6470
6471 /* See breakpoint.h. */
6472
6473 void
6474 print_breakpoint (breakpoint *b)
6475 {
6476 struct bp_location *dummy_loc = NULL;
6477 print_one_breakpoint (b, &dummy_loc, 0);
6478 }
6479
6480 /* Return true if this breakpoint was set by the user, false if it is
6481 internal or momentary. */
6482
6483 int
6484 user_breakpoint_p (struct breakpoint *b)
6485 {
6486 return b->number > 0;
6487 }
6488
6489 /* See breakpoint.h. */
6490
6491 int
6492 pending_breakpoint_p (struct breakpoint *b)
6493 {
6494 return b->loc == NULL;
6495 }
6496
6497 /* Print information on breakpoints (including watchpoints and tracepoints).
6498
6499 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6500 understood by number_or_range_parser. Only breakpoints included in this
6501 list are then printed.
6502
6503 If SHOW_INTERNAL is true, print internal breakpoints.
6504
6505 If FILTER is non-NULL, call it on each breakpoint and only include the
6506 ones for which it returns true.
6507
6508 Return the total number of breakpoints listed. */
6509
6510 static int
6511 breakpoint_1 (const char *bp_num_list, bool show_internal,
6512 bool (*filter) (const struct breakpoint *))
6513 {
6514 struct bp_location *last_loc = NULL;
6515 int nr_printable_breakpoints;
6516 struct value_print_options opts;
6517 int print_address_bits = 0;
6518 int print_type_col_width = 14;
6519 struct ui_out *uiout = current_uiout;
6520 bool has_disabled_by_cond_location = false;
6521
6522 get_user_print_options (&opts);
6523
6524 /* Compute the number of rows in the table, as well as the size
6525 required for address fields. */
6526 nr_printable_breakpoints = 0;
6527 for (breakpoint *b : all_breakpoints ())
6528 {
6529 /* If we have a filter, only list the breakpoints it accepts. */
6530 if (filter && !filter (b))
6531 continue;
6532
6533 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6534 accept. Skip the others. */
6535 if (bp_num_list != NULL && *bp_num_list != '\0')
6536 {
6537 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6538 continue;
6539 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6540 continue;
6541 }
6542
6543 if (show_internal || user_breakpoint_p (b))
6544 {
6545 int addr_bit, type_len;
6546
6547 addr_bit = breakpoint_address_bits (b);
6548 if (addr_bit > print_address_bits)
6549 print_address_bits = addr_bit;
6550
6551 type_len = strlen (bptype_string (b->type));
6552 if (type_len > print_type_col_width)
6553 print_type_col_width = type_len;
6554
6555 nr_printable_breakpoints++;
6556 }
6557 }
6558
6559 {
6560 ui_out_emit_table table_emitter (uiout,
6561 opts.addressprint ? 6 : 5,
6562 nr_printable_breakpoints,
6563 "BreakpointTable");
6564
6565 if (nr_printable_breakpoints > 0)
6566 annotate_breakpoints_headers ();
6567 if (nr_printable_breakpoints > 0)
6568 annotate_field (0);
6569 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6570 if (nr_printable_breakpoints > 0)
6571 annotate_field (1);
6572 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6573 if (nr_printable_breakpoints > 0)
6574 annotate_field (2);
6575 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6576 if (nr_printable_breakpoints > 0)
6577 annotate_field (3);
6578 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6579 if (opts.addressprint)
6580 {
6581 if (nr_printable_breakpoints > 0)
6582 annotate_field (4);
6583 if (print_address_bits <= 32)
6584 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6585 else
6586 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6587 }
6588 if (nr_printable_breakpoints > 0)
6589 annotate_field (5);
6590 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6591 uiout->table_body ();
6592 if (nr_printable_breakpoints > 0)
6593 annotate_breakpoints_table ();
6594
6595 for (breakpoint *b : all_breakpoints ())
6596 {
6597 QUIT;
6598 /* If we have a filter, only list the breakpoints it accepts. */
6599 if (filter && !filter (b))
6600 continue;
6601
6602 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6603 accept. Skip the others. */
6604
6605 if (bp_num_list != NULL && *bp_num_list != '\0')
6606 {
6607 if (show_internal) /* maintenance info breakpoint */
6608 {
6609 if (parse_and_eval_long (bp_num_list) != b->number)
6610 continue;
6611 }
6612 else /* all others */
6613 {
6614 if (!number_is_in_list (bp_num_list, b->number))
6615 continue;
6616 }
6617 }
6618 /* We only print out user settable breakpoints unless the
6619 show_internal is set. */
6620 if (show_internal || user_breakpoint_p (b))
6621 {
6622 print_one_breakpoint (b, &last_loc, show_internal);
6623 for (bp_location *loc : b->locations ())
6624 if (loc->disabled_by_cond)
6625 has_disabled_by_cond_location = true;
6626 }
6627 }
6628 }
6629
6630 if (nr_printable_breakpoints == 0)
6631 {
6632 /* If there's a filter, let the caller decide how to report
6633 empty list. */
6634 if (!filter)
6635 {
6636 if (bp_num_list == NULL || *bp_num_list == '\0')
6637 uiout->message ("No breakpoints or watchpoints.\n");
6638 else
6639 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6640 bp_num_list);
6641 }
6642 }
6643 else
6644 {
6645 if (last_loc && !server_command)
6646 set_next_address (last_loc->gdbarch, last_loc->address);
6647
6648 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
6649 uiout->message (_("(*): Breakpoint condition is invalid at this "
6650 "location.\n"));
6651 }
6652
6653 /* FIXME? Should this be moved up so that it is only called when
6654 there have been breakpoints? */
6655 annotate_breakpoints_table_end ();
6656
6657 return nr_printable_breakpoints;
6658 }
6659
6660 /* Display the value of default-collect in a way that is generally
6661 compatible with the breakpoint list. */
6662
6663 static void
6664 default_collect_info (void)
6665 {
6666 struct ui_out *uiout = current_uiout;
6667
6668 /* If it has no value (which is frequently the case), say nothing; a
6669 message like "No default-collect." gets in user's face when it's
6670 not wanted. */
6671 if (default_collect.empty ())
6672 return;
6673
6674 /* The following phrase lines up nicely with per-tracepoint collect
6675 actions. */
6676 uiout->text ("default collect ");
6677 uiout->field_string ("default-collect", default_collect);
6678 uiout->text (" \n");
6679 }
6680
6681 static void
6682 info_breakpoints_command (const char *args, int from_tty)
6683 {
6684 breakpoint_1 (args, false, NULL);
6685
6686 default_collect_info ();
6687 }
6688
6689 static void
6690 info_watchpoints_command (const char *args, int from_tty)
6691 {
6692 int num_printed = breakpoint_1 (args, false, is_watchpoint);
6693 struct ui_out *uiout = current_uiout;
6694
6695 if (num_printed == 0)
6696 {
6697 if (args == NULL || *args == '\0')
6698 uiout->message ("No watchpoints.\n");
6699 else
6700 uiout->message ("No watchpoint matching '%s'.\n", args);
6701 }
6702 }
6703
6704 static void
6705 maintenance_info_breakpoints (const char *args, int from_tty)
6706 {
6707 breakpoint_1 (args, true, NULL);
6708
6709 default_collect_info ();
6710 }
6711
6712 static int
6713 breakpoint_has_pc (struct breakpoint *b,
6714 struct program_space *pspace,
6715 CORE_ADDR pc, struct obj_section *section)
6716 {
6717 for (bp_location *bl : b->locations ())
6718 {
6719 if (bl->pspace == pspace
6720 && bl->address == pc
6721 && (!overlay_debugging || bl->section == section))
6722 return 1;
6723 }
6724 return 0;
6725 }
6726
6727 /* Print a message describing any user-breakpoints set at PC. This
6728 concerns with logical breakpoints, so we match program spaces, not
6729 address spaces. */
6730
6731 static void
6732 describe_other_breakpoints (struct gdbarch *gdbarch,
6733 struct program_space *pspace, CORE_ADDR pc,
6734 struct obj_section *section, int thread)
6735 {
6736 int others = 0;
6737
6738 for (breakpoint *b : all_breakpoints ())
6739 others += (user_breakpoint_p (b)
6740 && breakpoint_has_pc (b, pspace, pc, section));
6741
6742 if (others > 0)
6743 {
6744 if (others == 1)
6745 gdb_printf (_("Note: breakpoint "));
6746 else /* if (others == ???) */
6747 gdb_printf (_("Note: breakpoints "));
6748 for (breakpoint *b : all_breakpoints ())
6749 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6750 {
6751 others--;
6752 gdb_printf ("%d", b->number);
6753 if (b->thread == -1 && thread != -1)
6754 gdb_printf (" (all threads)");
6755 else if (b->thread != -1)
6756 gdb_printf (" (thread %d)", b->thread);
6757 gdb_printf ("%s%s ",
6758 ((b->enable_state == bp_disabled
6759 || b->enable_state == bp_call_disabled)
6760 ? " (disabled)"
6761 : ""),
6762 (others > 1) ? ","
6763 : ((others == 1) ? " and" : ""));
6764 }
6765 current_uiout->message (_("also set at pc %ps.\n"),
6766 styled_string (address_style.style (),
6767 paddress (gdbarch, pc)));
6768 }
6769 }
6770 \f
6771
6772 /* Return true iff it is meaningful to use the address member of LOC.
6773 For some breakpoint types, the locations' address members are
6774 irrelevant and it makes no sense to attempt to compare them to
6775 other addresses (or use them for any other purpose either).
6776
6777 More specifically, software watchpoints and catchpoints that are
6778 not backed by breakpoints always have a zero valued location
6779 address and we don't want to mark breakpoints of any of these types
6780 to be a duplicate of an actual breakpoint location at address
6781 zero. */
6782
6783 static bool
6784 bl_address_is_meaningful (bp_location *loc)
6785 {
6786 return loc->loc_type != bp_loc_other;
6787 }
6788
6789 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6790 true if LOC1 and LOC2 represent the same watchpoint location. */
6791
6792 static int
6793 watchpoint_locations_match (struct bp_location *loc1,
6794 struct bp_location *loc2)
6795 {
6796 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6797 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6798
6799 /* Both of them must exist. */
6800 gdb_assert (w1 != NULL);
6801 gdb_assert (w2 != NULL);
6802
6803 /* If the target can evaluate the condition expression in hardware,
6804 then we we need to insert both watchpoints even if they are at
6805 the same place. Otherwise the watchpoint will only trigger when
6806 the condition of whichever watchpoint was inserted evaluates to
6807 true, not giving a chance for GDB to check the condition of the
6808 other watchpoint. */
6809 if ((w1->cond_exp
6810 && target_can_accel_watchpoint_condition (loc1->address,
6811 loc1->length,
6812 loc1->watchpoint_type,
6813 w1->cond_exp.get ()))
6814 || (w2->cond_exp
6815 && target_can_accel_watchpoint_condition (loc2->address,
6816 loc2->length,
6817 loc2->watchpoint_type,
6818 w2->cond_exp.get ())))
6819 return 0;
6820
6821 /* Note that this checks the owner's type, not the location's. In
6822 case the target does not support read watchpoints, but does
6823 support access watchpoints, we'll have bp_read_watchpoint
6824 watchpoints with hw_access locations. Those should be considered
6825 duplicates of hw_read locations. The hw_read locations will
6826 become hw_access locations later. */
6827 return (loc1->owner->type == loc2->owner->type
6828 && loc1->pspace->aspace == loc2->pspace->aspace
6829 && loc1->address == loc2->address
6830 && loc1->length == loc2->length);
6831 }
6832
6833 /* See breakpoint.h. */
6834
6835 int
6836 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6837 const address_space *aspace2, CORE_ADDR addr2)
6838 {
6839 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6840 || aspace1 == aspace2)
6841 && addr1 == addr2);
6842 }
6843
6844 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6845 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6846 matches ASPACE2. On targets that have global breakpoints, the address
6847 space doesn't really matter. */
6848
6849 static int
6850 breakpoint_address_match_range (const address_space *aspace1,
6851 CORE_ADDR addr1,
6852 int len1, const address_space *aspace2,
6853 CORE_ADDR addr2)
6854 {
6855 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6856 || aspace1 == aspace2)
6857 && addr2 >= addr1 && addr2 < addr1 + len1);
6858 }
6859
6860 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6861 a ranged breakpoint. In most targets, a match happens only if ASPACE
6862 matches the breakpoint's address space. On targets that have global
6863 breakpoints, the address space doesn't really matter. */
6864
6865 static int
6866 breakpoint_location_address_match (struct bp_location *bl,
6867 const address_space *aspace,
6868 CORE_ADDR addr)
6869 {
6870 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6871 aspace, addr)
6872 || (bl->length
6873 && breakpoint_address_match_range (bl->pspace->aspace,
6874 bl->address, bl->length,
6875 aspace, addr)));
6876 }
6877
6878 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6879 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6880 match happens only if ASPACE matches the breakpoint's address
6881 space. On targets that have global breakpoints, the address space
6882 doesn't really matter. */
6883
6884 static int
6885 breakpoint_location_address_range_overlap (struct bp_location *bl,
6886 const address_space *aspace,
6887 CORE_ADDR addr, int len)
6888 {
6889 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6890 || bl->pspace->aspace == aspace)
6891 {
6892 int bl_len = bl->length != 0 ? bl->length : 1;
6893
6894 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6895 return 1;
6896 }
6897 return 0;
6898 }
6899
6900 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6901 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6902 true, otherwise returns false. */
6903
6904 static int
6905 tracepoint_locations_match (struct bp_location *loc1,
6906 struct bp_location *loc2)
6907 {
6908 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6909 /* Since tracepoint locations are never duplicated with others', tracepoint
6910 locations at the same address of different tracepoints are regarded as
6911 different locations. */
6912 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6913 else
6914 return 0;
6915 }
6916
6917 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6918 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
6919 the same location. If SW_HW_BPS_MATCH is true, then software
6920 breakpoint locations and hardware breakpoint locations match,
6921 otherwise they don't. */
6922
6923 static int
6924 breakpoint_locations_match (struct bp_location *loc1,
6925 struct bp_location *loc2,
6926 bool sw_hw_bps_match)
6927 {
6928 int hw_point1, hw_point2;
6929
6930 /* Both of them must not be in moribund_locations. */
6931 gdb_assert (loc1->owner != NULL);
6932 gdb_assert (loc2->owner != NULL);
6933
6934 hw_point1 = is_hardware_watchpoint (loc1->owner);
6935 hw_point2 = is_hardware_watchpoint (loc2->owner);
6936
6937 if (hw_point1 != hw_point2)
6938 return 0;
6939 else if (hw_point1)
6940 return watchpoint_locations_match (loc1, loc2);
6941 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6942 return tracepoint_locations_match (loc1, loc2);
6943 else
6944 /* We compare bp_location.length in order to cover ranged
6945 breakpoints. Keep this in sync with
6946 bp_location_is_less_than. */
6947 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6948 loc2->pspace->aspace, loc2->address)
6949 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
6950 && loc1->length == loc2->length);
6951 }
6952
6953 static void
6954 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6955 int bnum, int have_bnum)
6956 {
6957 /* The longest string possibly returned by hex_string_custom
6958 is 50 chars. These must be at least that big for safety. */
6959 char astr1[64];
6960 char astr2[64];
6961
6962 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6963 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
6964 if (have_bnum)
6965 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
6966 bnum, astr1, astr2);
6967 else
6968 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
6969 }
6970
6971 /* Adjust a breakpoint's address to account for architectural
6972 constraints on breakpoint placement. Return the adjusted address.
6973 Note: Very few targets require this kind of adjustment. For most
6974 targets, this function is simply the identity function. */
6975
6976 static CORE_ADDR
6977 adjust_breakpoint_address (struct gdbarch *gdbarch,
6978 CORE_ADDR bpaddr, enum bptype bptype)
6979 {
6980 if (bptype == bp_watchpoint
6981 || bptype == bp_hardware_watchpoint
6982 || bptype == bp_read_watchpoint
6983 || bptype == bp_access_watchpoint
6984 || bptype == bp_catchpoint)
6985 {
6986 /* Watchpoints and the various bp_catch_* eventpoints should not
6987 have their addresses modified. */
6988 return bpaddr;
6989 }
6990 else if (bptype == bp_single_step)
6991 {
6992 /* Single-step breakpoints should not have their addresses
6993 modified. If there's any architectural constrain that
6994 applies to this address, then it should have already been
6995 taken into account when the breakpoint was created in the
6996 first place. If we didn't do this, stepping through e.g.,
6997 Thumb-2 IT blocks would break. */
6998 return bpaddr;
6999 }
7000 else
7001 {
7002 CORE_ADDR adjusted_bpaddr = bpaddr;
7003
7004 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7005 {
7006 /* Some targets have architectural constraints on the placement
7007 of breakpoint instructions. Obtain the adjusted address. */
7008 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7009 }
7010
7011 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
7012
7013 /* An adjusted breakpoint address can significantly alter
7014 a user's expectations. Print a warning if an adjustment
7015 is required. */
7016 if (adjusted_bpaddr != bpaddr)
7017 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7018
7019 return adjusted_bpaddr;
7020 }
7021 }
7022
7023 static bp_loc_type
7024 bp_location_from_bp_type (bptype type)
7025 {
7026 switch (type)
7027 {
7028 case bp_breakpoint:
7029 case bp_single_step:
7030 case bp_until:
7031 case bp_finish:
7032 case bp_longjmp:
7033 case bp_longjmp_resume:
7034 case bp_longjmp_call_dummy:
7035 case bp_exception:
7036 case bp_exception_resume:
7037 case bp_step_resume:
7038 case bp_hp_step_resume:
7039 case bp_watchpoint_scope:
7040 case bp_call_dummy:
7041 case bp_std_terminate:
7042 case bp_shlib_event:
7043 case bp_thread_event:
7044 case bp_overlay_event:
7045 case bp_jit_event:
7046 case bp_longjmp_master:
7047 case bp_std_terminate_master:
7048 case bp_exception_master:
7049 case bp_gnu_ifunc_resolver:
7050 case bp_gnu_ifunc_resolver_return:
7051 case bp_dprintf:
7052 return bp_loc_software_breakpoint;
7053 case bp_hardware_breakpoint:
7054 return bp_loc_hardware_breakpoint;
7055 case bp_hardware_watchpoint:
7056 case bp_read_watchpoint:
7057 case bp_access_watchpoint:
7058 return bp_loc_hardware_watchpoint;
7059 case bp_watchpoint:
7060 case bp_catchpoint:
7061 case bp_tracepoint:
7062 case bp_fast_tracepoint:
7063 case bp_static_tracepoint:
7064 return bp_loc_other;
7065 default:
7066 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7067 }
7068 }
7069
7070 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7071 {
7072 this->owner = owner;
7073 this->cond_bytecode = NULL;
7074 this->shlib_disabled = 0;
7075 this->enabled = 1;
7076 this->disabled_by_cond = false;
7077
7078 this->loc_type = type;
7079
7080 if (this->loc_type == bp_loc_software_breakpoint
7081 || this->loc_type == bp_loc_hardware_breakpoint)
7082 mark_breakpoint_location_modified (this);
7083
7084 incref ();
7085 }
7086
7087 bp_location::bp_location (breakpoint *owner)
7088 : bp_location::bp_location (owner,
7089 bp_location_from_bp_type (owner->type))
7090 {
7091 }
7092
7093 /* Allocate a struct bp_location. */
7094
7095 static struct bp_location *
7096 allocate_bp_location (struct breakpoint *bpt)
7097 {
7098 return bpt->ops->allocate_location (bpt);
7099 }
7100
7101 /* Decrement reference count. If the reference count reaches 0,
7102 destroy the bp_location. Sets *BLP to NULL. */
7103
7104 static void
7105 decref_bp_location (struct bp_location **blp)
7106 {
7107 bp_location_ref_policy::decref (*blp);
7108 *blp = NULL;
7109 }
7110
7111 /* Add breakpoint B at the end of the global breakpoint chain. */
7112
7113 static breakpoint *
7114 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7115 {
7116 struct breakpoint *b1;
7117 struct breakpoint *result = b.get ();
7118
7119 /* Add this breakpoint to the end of the chain so that a list of
7120 breakpoints will come out in order of increasing numbers. */
7121
7122 b1 = breakpoint_chain;
7123 if (b1 == 0)
7124 breakpoint_chain = b.release ();
7125 else
7126 {
7127 while (b1->next)
7128 b1 = b1->next;
7129 b1->next = b.release ();
7130 }
7131
7132 return result;
7133 }
7134
7135 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7136
7137 static void
7138 init_raw_breakpoint_without_location (struct breakpoint *b,
7139 struct gdbarch *gdbarch,
7140 enum bptype bptype,
7141 const struct breakpoint_ops *ops)
7142 {
7143 gdb_assert (ops != NULL);
7144
7145 b->ops = ops;
7146 b->type = bptype;
7147 b->gdbarch = gdbarch;
7148 b->language = current_language->la_language;
7149 b->input_radix = input_radix;
7150 b->related_breakpoint = b;
7151 }
7152
7153 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7154 that has type BPTYPE and has no locations as yet. */
7155
7156 static struct breakpoint *
7157 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7158 enum bptype bptype,
7159 const struct breakpoint_ops *ops)
7160 {
7161 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7162
7163 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7164 return add_to_breakpoint_chain (std::move (b));
7165 }
7166
7167 /* Initialize loc->function_name. */
7168
7169 static void
7170 set_breakpoint_location_function (struct bp_location *loc)
7171 {
7172 gdb_assert (loc->owner != NULL);
7173
7174 if (loc->owner->type == bp_breakpoint
7175 || loc->owner->type == bp_hardware_breakpoint
7176 || is_tracepoint (loc->owner))
7177 {
7178 const char *function_name;
7179
7180 if (loc->msymbol != NULL
7181 && (loc->msymbol->type () == mst_text_gnu_ifunc
7182 || loc->msymbol->type () == mst_data_gnu_ifunc))
7183 {
7184 struct breakpoint *b = loc->owner;
7185
7186 function_name = loc->msymbol->linkage_name ();
7187
7188 if (b->type == bp_breakpoint && b->loc == loc
7189 && loc->next == NULL && b->related_breakpoint == b)
7190 {
7191 /* Create only the whole new breakpoint of this type but do not
7192 mess more complicated breakpoints with multiple locations. */
7193 b->type = bp_gnu_ifunc_resolver;
7194 /* Remember the resolver's address for use by the return
7195 breakpoint. */
7196 loc->related_address = loc->address;
7197 }
7198 }
7199 else
7200 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7201
7202 if (function_name)
7203 loc->function_name = make_unique_xstrdup (function_name);
7204 }
7205 }
7206
7207 /* Attempt to determine architecture of location identified by SAL. */
7208 struct gdbarch *
7209 get_sal_arch (struct symtab_and_line sal)
7210 {
7211 if (sal.section)
7212 return sal.section->objfile->arch ();
7213 if (sal.symtab)
7214 return sal.symtab->compunit ()->objfile ()->arch ();
7215
7216 return NULL;
7217 }
7218
7219 /* Low level routine for partially initializing a breakpoint of type
7220 BPTYPE. The newly created breakpoint's address, section, source
7221 file name, and line number are provided by SAL.
7222
7223 It is expected that the caller will complete the initialization of
7224 the newly created breakpoint struct as well as output any status
7225 information regarding the creation of a new breakpoint. */
7226
7227 static void
7228 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7229 struct symtab_and_line sal, enum bptype bptype,
7230 const struct breakpoint_ops *ops)
7231 {
7232 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7233
7234 add_location_to_breakpoint (b, &sal);
7235
7236 if (bptype != bp_catchpoint)
7237 gdb_assert (sal.pspace != NULL);
7238
7239 /* Store the program space that was used to set the breakpoint,
7240 except for ordinary breakpoints, which are independent of the
7241 program space. */
7242 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7243 b->pspace = sal.pspace;
7244 }
7245
7246 /* set_raw_breakpoint is a low level routine for allocating and
7247 partially initializing a breakpoint of type BPTYPE. The newly
7248 created breakpoint's address, section, source file name, and line
7249 number are provided by SAL. The newly created and partially
7250 initialized breakpoint is added to the breakpoint chain and
7251 is also returned as the value of this function.
7252
7253 It is expected that the caller will complete the initialization of
7254 the newly created breakpoint struct as well as output any status
7255 information regarding the creation of a new breakpoint. In
7256 particular, set_raw_breakpoint does NOT set the breakpoint
7257 number! Care should be taken to not allow an error to occur
7258 prior to completing the initialization of the breakpoint. If this
7259 should happen, a bogus breakpoint will be left on the chain. */
7260
7261 static struct breakpoint *
7262 set_raw_breakpoint (struct gdbarch *gdbarch,
7263 struct symtab_and_line sal, enum bptype bptype,
7264 const struct breakpoint_ops *ops)
7265 {
7266 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7267
7268 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7269 return add_to_breakpoint_chain (std::move (b));
7270 }
7271
7272 /* Call this routine when stepping and nexting to enable a breakpoint
7273 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7274 initiated the operation. */
7275
7276 void
7277 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7278 {
7279 int thread = tp->global_num;
7280
7281 /* To avoid having to rescan all objfile symbols at every step,
7282 we maintain a list of continually-inserted but always disabled
7283 longjmp "master" breakpoints. Here, we simply create momentary
7284 clones of those and enable them for the requested thread. */
7285 for (breakpoint *b : all_breakpoints_safe ())
7286 if (b->pspace == current_program_space
7287 && (b->type == bp_longjmp_master
7288 || b->type == bp_exception_master))
7289 {
7290 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7291 struct breakpoint *clone;
7292
7293 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7294 after their removal. */
7295 clone = momentary_breakpoint_from_master (b, type,
7296 &momentary_breakpoint_ops, 1);
7297 clone->thread = thread;
7298 }
7299
7300 tp->initiating_frame = frame;
7301 }
7302
7303 /* Delete all longjmp breakpoints from THREAD. */
7304 void
7305 delete_longjmp_breakpoint (int thread)
7306 {
7307 for (breakpoint *b : all_breakpoints_safe ())
7308 if (b->type == bp_longjmp || b->type == bp_exception)
7309 {
7310 if (b->thread == thread)
7311 delete_breakpoint (b);
7312 }
7313 }
7314
7315 void
7316 delete_longjmp_breakpoint_at_next_stop (int thread)
7317 {
7318 for (breakpoint *b : all_breakpoints_safe ())
7319 if (b->type == bp_longjmp || b->type == bp_exception)
7320 {
7321 if (b->thread == thread)
7322 b->disposition = disp_del_at_next_stop;
7323 }
7324 }
7325
7326 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7327 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7328 pointer to any of them. Return NULL if this system cannot place longjmp
7329 breakpoints. */
7330
7331 struct breakpoint *
7332 set_longjmp_breakpoint_for_call_dummy (void)
7333 {
7334 breakpoint *retval = nullptr;
7335
7336 for (breakpoint *b : all_breakpoints ())
7337 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7338 {
7339 struct breakpoint *new_b;
7340
7341 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7342 &momentary_breakpoint_ops,
7343 1);
7344 new_b->thread = inferior_thread ()->global_num;
7345
7346 /* Link NEW_B into the chain of RETVAL breakpoints. */
7347
7348 gdb_assert (new_b->related_breakpoint == new_b);
7349 if (retval == NULL)
7350 retval = new_b;
7351 new_b->related_breakpoint = retval;
7352 while (retval->related_breakpoint != new_b->related_breakpoint)
7353 retval = retval->related_breakpoint;
7354 retval->related_breakpoint = new_b;
7355 }
7356
7357 return retval;
7358 }
7359
7360 /* Verify all existing dummy frames and their associated breakpoints for
7361 TP. Remove those which can no longer be found in the current frame
7362 stack.
7363
7364 If the unwind fails then there is not sufficient information to discard
7365 dummy frames. In this case, elide the clean up and the dummy frames will
7366 be cleaned up next time this function is called from a location where
7367 unwinding is possible. */
7368
7369 void
7370 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7371 {
7372 struct breakpoint *b, *b_tmp;
7373
7374 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7375 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7376 {
7377 struct breakpoint *dummy_b = b->related_breakpoint;
7378
7379 /* Find the bp_call_dummy breakpoint in the list of breakpoints
7380 chained off b->related_breakpoint. */
7381 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7382 dummy_b = dummy_b->related_breakpoint;
7383
7384 /* If there was no bp_call_dummy breakpoint then there's nothing
7385 more to do. Or, if the dummy frame associated with the
7386 bp_call_dummy is still on the stack then we need to leave this
7387 bp_call_dummy in place. */
7388 if (dummy_b->type != bp_call_dummy
7389 || frame_find_by_id (dummy_b->frame_id) != NULL)
7390 continue;
7391
7392 /* We didn't find the dummy frame on the stack, this could be
7393 because we have longjmp'd to a stack frame that is previous to
7394 the dummy frame, or it could be because the stack unwind is
7395 broken at some point between the longjmp frame and the dummy
7396 frame.
7397
7398 Next we figure out why the stack unwind stopped. If it looks
7399 like the unwind is complete then we assume the dummy frame has
7400 been jumped over, however, if the unwind stopped for an
7401 unexpected reason then we assume the stack unwind is currently
7402 broken, and that we will (eventually) return to the dummy
7403 frame.
7404
7405 It might be tempting to consider using frame_id_inner here, but
7406 that is not safe. There is no guarantee that the stack frames
7407 we are looking at here are even on the same stack as the
7408 original dummy frame, hence frame_id_inner can't be used. See
7409 the comments on frame_id_inner for more details. */
7410 bool unwind_finished_unexpectedly = false;
7411 for (struct frame_info *fi = get_current_frame (); fi != nullptr; )
7412 {
7413 struct frame_info *prev = get_prev_frame (fi);
7414 if (prev == nullptr)
7415 {
7416 /* FI is the last stack frame. Why did this frame not
7417 unwind further? */
7418 auto stop_reason = get_frame_unwind_stop_reason (fi);
7419 if (stop_reason != UNWIND_NO_REASON
7420 && stop_reason != UNWIND_OUTERMOST)
7421 unwind_finished_unexpectedly = true;
7422 }
7423 fi = prev;
7424 }
7425 if (unwind_finished_unexpectedly)
7426 continue;
7427
7428 dummy_frame_discard (dummy_b->frame_id, tp);
7429
7430 while (b->related_breakpoint != b)
7431 {
7432 if (b_tmp == b->related_breakpoint)
7433 b_tmp = b->related_breakpoint->next;
7434 delete_breakpoint (b->related_breakpoint);
7435 }
7436 delete_breakpoint (b);
7437 }
7438 }
7439
7440 void
7441 enable_overlay_breakpoints (void)
7442 {
7443 for (breakpoint *b : all_breakpoints ())
7444 if (b->type == bp_overlay_event)
7445 {
7446 b->enable_state = bp_enabled;
7447 update_global_location_list (UGLL_MAY_INSERT);
7448 overlay_events_enabled = 1;
7449 }
7450 }
7451
7452 void
7453 disable_overlay_breakpoints (void)
7454 {
7455 for (breakpoint *b : all_breakpoints ())
7456 if (b->type == bp_overlay_event)
7457 {
7458 b->enable_state = bp_disabled;
7459 update_global_location_list (UGLL_DONT_INSERT);
7460 overlay_events_enabled = 0;
7461 }
7462 }
7463
7464 /* Set an active std::terminate breakpoint for each std::terminate
7465 master breakpoint. */
7466 void
7467 set_std_terminate_breakpoint (void)
7468 {
7469 for (breakpoint *b : all_breakpoints_safe ())
7470 if (b->pspace == current_program_space
7471 && b->type == bp_std_terminate_master)
7472 {
7473 momentary_breakpoint_from_master (b, bp_std_terminate,
7474 &momentary_breakpoint_ops, 1);
7475 }
7476 }
7477
7478 /* Delete all the std::terminate breakpoints. */
7479 void
7480 delete_std_terminate_breakpoint (void)
7481 {
7482 for (breakpoint *b : all_breakpoints_safe ())
7483 if (b->type == bp_std_terminate)
7484 delete_breakpoint (b);
7485 }
7486
7487 struct breakpoint *
7488 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7489 {
7490 struct breakpoint *b;
7491
7492 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7493 &internal_breakpoint_ops);
7494
7495 b->enable_state = bp_enabled;
7496 /* location has to be used or breakpoint_re_set will delete me. */
7497 b->location = new_address_location (b->loc->address, NULL, 0);
7498
7499 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7500
7501 return b;
7502 }
7503
7504 struct lang_and_radix
7505 {
7506 enum language lang;
7507 int radix;
7508 };
7509
7510 /* Create a breakpoint for JIT code registration and unregistration. */
7511
7512 struct breakpoint *
7513 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7514 {
7515 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7516 &internal_breakpoint_ops);
7517 }
7518
7519 /* Remove JIT code registration and unregistration breakpoint(s). */
7520
7521 void
7522 remove_jit_event_breakpoints (void)
7523 {
7524 for (breakpoint *b : all_breakpoints_safe ())
7525 if (b->type == bp_jit_event
7526 && b->loc->pspace == current_program_space)
7527 delete_breakpoint (b);
7528 }
7529
7530 void
7531 remove_solib_event_breakpoints (void)
7532 {
7533 for (breakpoint *b : all_breakpoints_safe ())
7534 if (b->type == bp_shlib_event
7535 && b->loc->pspace == current_program_space)
7536 delete_breakpoint (b);
7537 }
7538
7539 /* See breakpoint.h. */
7540
7541 void
7542 remove_solib_event_breakpoints_at_next_stop (void)
7543 {
7544 for (breakpoint *b : all_breakpoints_safe ())
7545 if (b->type == bp_shlib_event
7546 && b->loc->pspace == current_program_space)
7547 b->disposition = disp_del_at_next_stop;
7548 }
7549
7550 /* Helper for create_solib_event_breakpoint /
7551 create_and_insert_solib_event_breakpoint. Allows specifying which
7552 INSERT_MODE to pass through to update_global_location_list. */
7553
7554 static struct breakpoint *
7555 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7556 enum ugll_insert_mode insert_mode)
7557 {
7558 struct breakpoint *b;
7559
7560 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7561 &internal_breakpoint_ops);
7562 update_global_location_list_nothrow (insert_mode);
7563 return b;
7564 }
7565
7566 struct breakpoint *
7567 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7568 {
7569 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7570 }
7571
7572 /* See breakpoint.h. */
7573
7574 struct breakpoint *
7575 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7576 {
7577 struct breakpoint *b;
7578
7579 /* Explicitly tell update_global_location_list to insert
7580 locations. */
7581 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7582 if (!b->loc->inserted)
7583 {
7584 delete_breakpoint (b);
7585 return NULL;
7586 }
7587 return b;
7588 }
7589
7590 /* Disable any breakpoints that are on code in shared libraries. Only
7591 apply to enabled breakpoints, disabled ones can just stay disabled. */
7592
7593 void
7594 disable_breakpoints_in_shlibs (void)
7595 {
7596 for (bp_location *loc : all_bp_locations ())
7597 {
7598 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7599 struct breakpoint *b = loc->owner;
7600
7601 /* We apply the check to all breakpoints, including disabled for
7602 those with loc->duplicate set. This is so that when breakpoint
7603 becomes enabled, or the duplicate is removed, gdb will try to
7604 insert all breakpoints. If we don't set shlib_disabled here,
7605 we'll try to insert those breakpoints and fail. */
7606 if (((b->type == bp_breakpoint)
7607 || (b->type == bp_jit_event)
7608 || (b->type == bp_hardware_breakpoint)
7609 || (is_tracepoint (b)))
7610 && loc->pspace == current_program_space
7611 && !loc->shlib_disabled
7612 && solib_name_from_address (loc->pspace, loc->address)
7613 )
7614 {
7615 loc->shlib_disabled = 1;
7616 }
7617 }
7618 }
7619
7620 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7621 notification of unloaded_shlib. Only apply to enabled breakpoints,
7622 disabled ones can just stay disabled. */
7623
7624 static void
7625 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7626 {
7627 int disabled_shlib_breaks = 0;
7628
7629 for (bp_location *loc : all_bp_locations ())
7630 {
7631 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7632 struct breakpoint *b = loc->owner;
7633
7634 if (solib->pspace == loc->pspace
7635 && !loc->shlib_disabled
7636 && (((b->type == bp_breakpoint
7637 || b->type == bp_jit_event
7638 || b->type == bp_hardware_breakpoint)
7639 && (loc->loc_type == bp_loc_hardware_breakpoint
7640 || loc->loc_type == bp_loc_software_breakpoint))
7641 || is_tracepoint (b))
7642 && solib_contains_address_p (solib, loc->address))
7643 {
7644 loc->shlib_disabled = 1;
7645 /* At this point, we cannot rely on remove_breakpoint
7646 succeeding so we must mark the breakpoint as not inserted
7647 to prevent future errors occurring in remove_breakpoints. */
7648 loc->inserted = 0;
7649
7650 /* This may cause duplicate notifications for the same breakpoint. */
7651 gdb::observers::breakpoint_modified.notify (b);
7652
7653 if (!disabled_shlib_breaks)
7654 {
7655 target_terminal::ours_for_output ();
7656 warning (_("Temporarily disabling breakpoints "
7657 "for unloaded shared library \"%s\""),
7658 solib->so_name);
7659 }
7660 disabled_shlib_breaks = 1;
7661 }
7662 }
7663 }
7664
7665 /* Disable any breakpoints and tracepoints in OBJFILE upon
7666 notification of free_objfile. Only apply to enabled breakpoints,
7667 disabled ones can just stay disabled. */
7668
7669 static void
7670 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7671 {
7672 if (objfile == NULL)
7673 return;
7674
7675 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7676 managed by the user with add-symbol-file/remove-symbol-file.
7677 Similarly to how breakpoints in shared libraries are handled in
7678 response to "nosharedlibrary", mark breakpoints in such modules
7679 shlib_disabled so they end up uninserted on the next global
7680 location list update. Shared libraries not loaded by the user
7681 aren't handled here -- they're already handled in
7682 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7683 solib_unloaded observer. We skip objfiles that are not
7684 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7685 main objfile). */
7686 if ((objfile->flags & OBJF_SHARED) == 0
7687 || (objfile->flags & OBJF_USERLOADED) == 0)
7688 return;
7689
7690 for (breakpoint *b : all_breakpoints ())
7691 {
7692 int bp_modified = 0;
7693
7694 if (!is_breakpoint (b) && !is_tracepoint (b))
7695 continue;
7696
7697 for (bp_location *loc : b->locations ())
7698 {
7699 CORE_ADDR loc_addr = loc->address;
7700
7701 if (loc->loc_type != bp_loc_hardware_breakpoint
7702 && loc->loc_type != bp_loc_software_breakpoint)
7703 continue;
7704
7705 if (loc->shlib_disabled != 0)
7706 continue;
7707
7708 if (objfile->pspace != loc->pspace)
7709 continue;
7710
7711 if (loc->loc_type != bp_loc_hardware_breakpoint
7712 && loc->loc_type != bp_loc_software_breakpoint)
7713 continue;
7714
7715 if (is_addr_in_objfile (loc_addr, objfile))
7716 {
7717 loc->shlib_disabled = 1;
7718 /* At this point, we don't know whether the object was
7719 unmapped from the inferior or not, so leave the
7720 inserted flag alone. We'll handle failure to
7721 uninsert quietly, in case the object was indeed
7722 unmapped. */
7723
7724 mark_breakpoint_location_modified (loc);
7725
7726 bp_modified = 1;
7727 }
7728 }
7729
7730 if (bp_modified)
7731 gdb::observers::breakpoint_modified.notify (b);
7732 }
7733 }
7734
7735 /* See breakpoint.h. */
7736
7737 void
7738 init_catchpoint (struct breakpoint *b,
7739 struct gdbarch *gdbarch, bool temp,
7740 const char *cond_string,
7741 const struct breakpoint_ops *ops)
7742 {
7743 symtab_and_line sal;
7744 sal.pspace = current_program_space;
7745
7746 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
7747
7748 if (cond_string == nullptr)
7749 b->cond_string.reset ();
7750 else
7751 b->cond_string = make_unique_xstrdup (cond_string);
7752 b->disposition = temp ? disp_del : disp_donttouch;
7753 }
7754
7755 void
7756 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
7757 {
7758 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
7759 set_breakpoint_number (internal, b);
7760 if (is_tracepoint (b))
7761 set_tracepoint_count (breakpoint_count);
7762 if (!internal)
7763 mention (b);
7764 gdb::observers::breakpoint_created.notify (b);
7765
7766 if (update_gll)
7767 update_global_location_list (UGLL_MAY_INSERT);
7768 }
7769
7770 static int
7771 hw_breakpoint_used_count (void)
7772 {
7773 int i = 0;
7774
7775 for (breakpoint *b : all_breakpoints ())
7776 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
7777 for (bp_location *bl : b->locations ())
7778 {
7779 /* Special types of hardware breakpoints may use more than
7780 one register. */
7781 i += b->ops->resources_needed (bl);
7782 }
7783
7784 return i;
7785 }
7786
7787 /* Returns the resources B would use if it were a hardware
7788 watchpoint. */
7789
7790 static int
7791 hw_watchpoint_use_count (struct breakpoint *b)
7792 {
7793 int i = 0;
7794
7795 if (!breakpoint_enabled (b))
7796 return 0;
7797
7798 for (bp_location *bl : b->locations ())
7799 {
7800 /* Special types of hardware watchpoints may use more than
7801 one register. */
7802 i += b->ops->resources_needed (bl);
7803 }
7804
7805 return i;
7806 }
7807
7808 /* Returns the sum the used resources of all hardware watchpoints of
7809 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
7810 the sum of the used resources of all hardware watchpoints of other
7811 types _not_ TYPE. */
7812
7813 static int
7814 hw_watchpoint_used_count_others (struct breakpoint *except,
7815 enum bptype type, int *other_type_used)
7816 {
7817 int i = 0;
7818
7819 *other_type_used = 0;
7820 for (breakpoint *b : all_breakpoints ())
7821 {
7822 if (b == except)
7823 continue;
7824 if (!breakpoint_enabled (b))
7825 continue;
7826
7827 if (b->type == type)
7828 i += hw_watchpoint_use_count (b);
7829 else if (is_hardware_watchpoint (b))
7830 *other_type_used = 1;
7831 }
7832
7833 return i;
7834 }
7835
7836 void
7837 disable_watchpoints_before_interactive_call_start (void)
7838 {
7839 for (breakpoint *b : all_breakpoints ())
7840 if (is_watchpoint (b) && breakpoint_enabled (b))
7841 {
7842 b->enable_state = bp_call_disabled;
7843 update_global_location_list (UGLL_DONT_INSERT);
7844 }
7845 }
7846
7847 void
7848 enable_watchpoints_after_interactive_call_stop (void)
7849 {
7850 for (breakpoint *b : all_breakpoints ())
7851 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
7852 {
7853 b->enable_state = bp_enabled;
7854 update_global_location_list (UGLL_MAY_INSERT);
7855 }
7856 }
7857
7858 void
7859 disable_breakpoints_before_startup (void)
7860 {
7861 current_program_space->executing_startup = 1;
7862 update_global_location_list (UGLL_DONT_INSERT);
7863 }
7864
7865 void
7866 enable_breakpoints_after_startup (void)
7867 {
7868 current_program_space->executing_startup = 0;
7869 breakpoint_re_set ();
7870 }
7871
7872 /* Create a new single-step breakpoint for thread THREAD, with no
7873 locations. */
7874
7875 static struct breakpoint *
7876 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
7877 {
7878 std::unique_ptr<breakpoint> b (new breakpoint ());
7879
7880 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
7881 &momentary_breakpoint_ops);
7882
7883 b->disposition = disp_donttouch;
7884 b->frame_id = null_frame_id;
7885
7886 b->thread = thread;
7887 gdb_assert (b->thread != 0);
7888
7889 return add_to_breakpoint_chain (std::move (b));
7890 }
7891
7892 /* Set a momentary breakpoint of type TYPE at address specified by
7893 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
7894 frame. */
7895
7896 breakpoint_up
7897 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
7898 struct frame_id frame_id, enum bptype type)
7899 {
7900 struct breakpoint *b;
7901
7902 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
7903 tail-called one. */
7904 gdb_assert (!frame_id_artificial_p (frame_id));
7905
7906 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
7907 b->enable_state = bp_enabled;
7908 b->disposition = disp_donttouch;
7909 b->frame_id = frame_id;
7910
7911 b->thread = inferior_thread ()->global_num;
7912
7913 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7914
7915 return breakpoint_up (b);
7916 }
7917
7918 /* Make a momentary breakpoint based on the master breakpoint ORIG.
7919 The new breakpoint will have type TYPE, use OPS as its
7920 breakpoint_ops, and will set enabled to LOC_ENABLED. */
7921
7922 static struct breakpoint *
7923 momentary_breakpoint_from_master (struct breakpoint *orig,
7924 enum bptype type,
7925 const struct breakpoint_ops *ops,
7926 int loc_enabled)
7927 {
7928 struct breakpoint *copy;
7929
7930 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
7931 copy->loc = allocate_bp_location (copy);
7932 set_breakpoint_location_function (copy->loc);
7933
7934 copy->loc->gdbarch = orig->loc->gdbarch;
7935 copy->loc->requested_address = orig->loc->requested_address;
7936 copy->loc->address = orig->loc->address;
7937 copy->loc->section = orig->loc->section;
7938 copy->loc->pspace = orig->loc->pspace;
7939 copy->loc->probe = orig->loc->probe;
7940 copy->loc->line_number = orig->loc->line_number;
7941 copy->loc->symtab = orig->loc->symtab;
7942 copy->loc->enabled = loc_enabled;
7943 copy->frame_id = orig->frame_id;
7944 copy->thread = orig->thread;
7945 copy->pspace = orig->pspace;
7946
7947 copy->enable_state = bp_enabled;
7948 copy->disposition = disp_donttouch;
7949 copy->number = internal_breakpoint_number--;
7950
7951 update_global_location_list_nothrow (UGLL_DONT_INSERT);
7952 return copy;
7953 }
7954
7955 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
7956 ORIG is NULL. */
7957
7958 struct breakpoint *
7959 clone_momentary_breakpoint (struct breakpoint *orig)
7960 {
7961 /* If there's nothing to clone, then return nothing. */
7962 if (orig == NULL)
7963 return NULL;
7964
7965 gdb_assert (orig->ops == &momentary_breakpoint_ops);
7966 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
7967 }
7968
7969 breakpoint_up
7970 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
7971 enum bptype type)
7972 {
7973 struct symtab_and_line sal;
7974
7975 sal = find_pc_line (pc, 0);
7976 sal.pc = pc;
7977 sal.section = find_pc_overlay (pc);
7978 sal.explicit_pc = 1;
7979
7980 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
7981 }
7982 \f
7983
7984 /* Tell the user we have just set a breakpoint B. */
7985
7986 static void
7987 mention (struct breakpoint *b)
7988 {
7989 b->ops->print_mention (b);
7990 current_uiout->text ("\n");
7991 }
7992 \f
7993
7994 static bool bp_loc_is_permanent (struct bp_location *loc);
7995
7996 /* Handle "set breakpoint auto-hw on".
7997
7998 If the explicitly specified breakpoint type is not hardware
7999 breakpoint, check the memory map to see whether the breakpoint
8000 address is in read-only memory.
8001
8002 - location type is not hardware breakpoint, memory is read-only.
8003 We change the type of the location to hardware breakpoint.
8004
8005 - location type is hardware breakpoint, memory is read-write. This
8006 means we've previously made the location hardware one, but then the
8007 memory map changed, so we undo.
8008 */
8009
8010 static void
8011 handle_automatic_hardware_breakpoints (bp_location *bl)
8012 {
8013 if (automatic_hardware_breakpoints
8014 && bl->owner->type != bp_hardware_breakpoint
8015 && (bl->loc_type == bp_loc_software_breakpoint
8016 || bl->loc_type == bp_loc_hardware_breakpoint))
8017 {
8018 /* When breakpoints are removed, remove_breakpoints will use
8019 location types we've just set here, the only possible problem
8020 is that memory map has changed during running program, but
8021 it's not going to work anyway with current gdb. */
8022 mem_region *mr = lookup_mem_region (bl->address);
8023
8024 if (mr != nullptr)
8025 {
8026 enum bp_loc_type new_type;
8027
8028 if (mr->attrib.mode != MEM_RW)
8029 new_type = bp_loc_hardware_breakpoint;
8030 else
8031 new_type = bp_loc_software_breakpoint;
8032
8033 if (new_type != bl->loc_type)
8034 {
8035 static bool said = false;
8036
8037 bl->loc_type = new_type;
8038 if (!said)
8039 {
8040 gdb_printf (_("Note: automatically using "
8041 "hardware breakpoints for "
8042 "read-only addresses.\n"));
8043 said = true;
8044 }
8045 }
8046 }
8047 }
8048 }
8049
8050 static struct bp_location *
8051 add_location_to_breakpoint (struct breakpoint *b,
8052 const struct symtab_and_line *sal)
8053 {
8054 struct bp_location *loc, **tmp;
8055 CORE_ADDR adjusted_address;
8056 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8057
8058 if (loc_gdbarch == NULL)
8059 loc_gdbarch = b->gdbarch;
8060
8061 /* Adjust the breakpoint's address prior to allocating a location.
8062 Once we call allocate_bp_location(), that mostly uninitialized
8063 location will be placed on the location chain. Adjustment of the
8064 breakpoint may cause target_read_memory() to be called and we do
8065 not want its scan of the location chain to find a breakpoint and
8066 location that's only been partially initialized. */
8067 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8068 sal->pc, b->type);
8069
8070 /* Sort the locations by their ADDRESS. */
8071 loc = allocate_bp_location (b);
8072 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8073 tmp = &((*tmp)->next))
8074 ;
8075 loc->next = *tmp;
8076 *tmp = loc;
8077
8078 loc->requested_address = sal->pc;
8079 loc->address = adjusted_address;
8080 loc->pspace = sal->pspace;
8081 loc->probe.prob = sal->prob;
8082 loc->probe.objfile = sal->objfile;
8083 gdb_assert (loc->pspace != NULL);
8084 loc->section = sal->section;
8085 loc->gdbarch = loc_gdbarch;
8086 loc->line_number = sal->line;
8087 loc->symtab = sal->symtab;
8088 loc->symbol = sal->symbol;
8089 loc->msymbol = sal->msymbol;
8090 loc->objfile = sal->objfile;
8091
8092 set_breakpoint_location_function (loc);
8093
8094 /* While by definition, permanent breakpoints are already present in the
8095 code, we don't mark the location as inserted. Normally one would expect
8096 that GDB could rely on that breakpoint instruction to stop the program,
8097 thus removing the need to insert its own breakpoint, except that executing
8098 the breakpoint instruction can kill the target instead of reporting a
8099 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8100 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8101 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8102 breakpoint be inserted normally results in QEMU knowing about the GDB
8103 breakpoint, and thus trap before the breakpoint instruction is executed.
8104 (If GDB later needs to continue execution past the permanent breakpoint,
8105 it manually increments the PC, thus avoiding executing the breakpoint
8106 instruction.) */
8107 if (bp_loc_is_permanent (loc))
8108 loc->permanent = 1;
8109
8110 return loc;
8111 }
8112 \f
8113
8114 /* Return true if LOC is pointing to a permanent breakpoint,
8115 return false otherwise. */
8116
8117 static bool
8118 bp_loc_is_permanent (struct bp_location *loc)
8119 {
8120 gdb_assert (loc != NULL);
8121
8122 /* If we have a non-breakpoint-backed catchpoint or a software
8123 watchpoint, just return 0. We should not attempt to read from
8124 the addresses the locations of these breakpoint types point to.
8125 gdbarch_program_breakpoint_here_p, below, will attempt to read
8126 memory. */
8127 if (!bl_address_is_meaningful (loc))
8128 return false;
8129
8130 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8131 switch_to_program_space_and_thread (loc->pspace);
8132 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
8133 }
8134
8135 /* Build a command list for the dprintf corresponding to the current
8136 settings of the dprintf style options. */
8137
8138 static void
8139 update_dprintf_command_list (struct breakpoint *b)
8140 {
8141 const char *dprintf_args = b->extra_string.get ();
8142 gdb::unique_xmalloc_ptr<char> printf_line = nullptr;
8143
8144 if (!dprintf_args)
8145 return;
8146
8147 dprintf_args = skip_spaces (dprintf_args);
8148
8149 /* Allow a comma, as it may have terminated a location, but don't
8150 insist on it. */
8151 if (*dprintf_args == ',')
8152 ++dprintf_args;
8153 dprintf_args = skip_spaces (dprintf_args);
8154
8155 if (*dprintf_args != '"')
8156 error (_("Bad format string, missing '\"'."));
8157
8158 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8159 printf_line = xstrprintf ("printf %s", dprintf_args);
8160 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8161 {
8162 if (dprintf_function.empty ())
8163 error (_("No function supplied for dprintf call"));
8164
8165 if (!dprintf_channel.empty ())
8166 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8167 dprintf_function.c_str (),
8168 dprintf_channel.c_str (),
8169 dprintf_args);
8170 else
8171 printf_line = xstrprintf ("call (void) %s (%s)",
8172 dprintf_function.c_str (),
8173 dprintf_args);
8174 }
8175 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8176 {
8177 if (target_can_run_breakpoint_commands ())
8178 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8179 else
8180 {
8181 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8182 printf_line = xstrprintf ("printf %s", dprintf_args);
8183 }
8184 }
8185 else
8186 internal_error (__FILE__, __LINE__,
8187 _("Invalid dprintf style."));
8188
8189 gdb_assert (printf_line != NULL);
8190
8191 /* Manufacture a printf sequence. */
8192 struct command_line *printf_cmd_line
8193 = new struct command_line (simple_control, printf_line.release ());
8194 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8195 command_lines_deleter ()));
8196 }
8197
8198 /* Update all dprintf commands, making their command lists reflect
8199 current style settings. */
8200
8201 static void
8202 update_dprintf_commands (const char *args, int from_tty,
8203 struct cmd_list_element *c)
8204 {
8205 for (breakpoint *b : all_breakpoints ())
8206 if (b->type == bp_dprintf)
8207 update_dprintf_command_list (b);
8208 }
8209
8210 /* Create a breakpoint with SAL as location. Use LOCATION
8211 as a description of the location, and COND_STRING
8212 as condition expression. If LOCATION is NULL then create an
8213 "address location" from the address in the SAL. */
8214
8215 static void
8216 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8217 gdb::array_view<const symtab_and_line> sals,
8218 event_location_up &&location,
8219 gdb::unique_xmalloc_ptr<char> filter,
8220 gdb::unique_xmalloc_ptr<char> cond_string,
8221 gdb::unique_xmalloc_ptr<char> extra_string,
8222 enum bptype type, enum bpdisp disposition,
8223 int thread, int task, int ignore_count,
8224 const struct breakpoint_ops *ops, int from_tty,
8225 int enabled, int internal, unsigned flags,
8226 int display_canonical)
8227 {
8228 int i;
8229
8230 if (type == bp_hardware_breakpoint)
8231 {
8232 int target_resources_ok;
8233
8234 i = hw_breakpoint_used_count ();
8235 target_resources_ok =
8236 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8237 i + 1, 0);
8238 if (target_resources_ok == 0)
8239 error (_("No hardware breakpoint support in the target."));
8240 else if (target_resources_ok < 0)
8241 error (_("Hardware breakpoints used exceeds limit."));
8242 }
8243
8244 gdb_assert (!sals.empty ());
8245
8246 for (const auto &sal : sals)
8247 {
8248 struct bp_location *loc;
8249
8250 if (from_tty)
8251 {
8252 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8253 if (!loc_gdbarch)
8254 loc_gdbarch = gdbarch;
8255
8256 describe_other_breakpoints (loc_gdbarch,
8257 sal.pspace, sal.pc, sal.section, thread);
8258 }
8259
8260 if (&sal == &sals[0])
8261 {
8262 init_raw_breakpoint (b, gdbarch, sal, type, ops);
8263 b->thread = thread;
8264 b->task = task;
8265
8266 b->cond_string = std::move (cond_string);
8267 b->extra_string = std::move (extra_string);
8268 b->ignore_count = ignore_count;
8269 b->enable_state = enabled ? bp_enabled : bp_disabled;
8270 b->disposition = disposition;
8271
8272 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8273 b->loc->inserted = 1;
8274
8275 if (type == bp_static_tracepoint)
8276 {
8277 struct tracepoint *t = (struct tracepoint *) b;
8278 struct static_tracepoint_marker marker;
8279
8280 if (strace_marker_p (b))
8281 {
8282 /* We already know the marker exists, otherwise, we
8283 wouldn't see a sal for it. */
8284 const char *p
8285 = &event_location_to_string (b->location.get ())[3];
8286 const char *endp;
8287
8288 p = skip_spaces (p);
8289
8290 endp = skip_to_space (p);
8291
8292 t->static_trace_marker_id.assign (p, endp - p);
8293
8294 gdb_printf (_("Probed static tracepoint "
8295 "marker \"%s\"\n"),
8296 t->static_trace_marker_id.c_str ());
8297 }
8298 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8299 {
8300 t->static_trace_marker_id = std::move (marker.str_id);
8301
8302 gdb_printf (_("Probed static tracepoint "
8303 "marker \"%s\"\n"),
8304 t->static_trace_marker_id.c_str ());
8305 }
8306 else
8307 warning (_("Couldn't determine the static "
8308 "tracepoint marker to probe"));
8309 }
8310
8311 loc = b->loc;
8312 }
8313 else
8314 {
8315 loc = add_location_to_breakpoint (b, &sal);
8316 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8317 loc->inserted = 1;
8318 }
8319
8320 /* Do not set breakpoint locations conditions yet. As locations
8321 are inserted, they get sorted based on their addresses. Let
8322 the list stabilize to have reliable location numbers. */
8323
8324 /* Dynamic printf requires and uses additional arguments on the
8325 command line, otherwise it's an error. */
8326 if (type == bp_dprintf)
8327 {
8328 if (b->extra_string)
8329 update_dprintf_command_list (b);
8330 else
8331 error (_("Format string required"));
8332 }
8333 else if (b->extra_string)
8334 error (_("Garbage '%s' at end of command"), b->extra_string.get ());
8335 }
8336
8337
8338 /* The order of the locations is now stable. Set the location
8339 condition using the location's number. */
8340 int loc_num = 1;
8341 for (bp_location *loc : b->locations ())
8342 {
8343 if (b->cond_string != nullptr)
8344 set_breakpoint_location_condition (b->cond_string.get (), loc,
8345 b->number, loc_num);
8346
8347 ++loc_num;
8348 }
8349
8350 b->display_canonical = display_canonical;
8351 if (location != NULL)
8352 b->location = std::move (location);
8353 else
8354 b->location = new_address_location (b->loc->address, NULL, 0);
8355 b->filter = std::move (filter);
8356 }
8357
8358 static void
8359 create_breakpoint_sal (struct gdbarch *gdbarch,
8360 gdb::array_view<const symtab_and_line> sals,
8361 event_location_up &&location,
8362 gdb::unique_xmalloc_ptr<char> filter,
8363 gdb::unique_xmalloc_ptr<char> cond_string,
8364 gdb::unique_xmalloc_ptr<char> extra_string,
8365 enum bptype type, enum bpdisp disposition,
8366 int thread, int task, int ignore_count,
8367 const struct breakpoint_ops *ops, int from_tty,
8368 int enabled, int internal, unsigned flags,
8369 int display_canonical)
8370 {
8371 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
8372
8373 init_breakpoint_sal (b.get (), gdbarch,
8374 sals, std::move (location),
8375 std::move (filter),
8376 std::move (cond_string),
8377 std::move (extra_string),
8378 type, disposition,
8379 thread, task, ignore_count,
8380 ops, from_tty,
8381 enabled, internal, flags,
8382 display_canonical);
8383
8384 install_breakpoint (internal, std::move (b), 0);
8385 }
8386
8387 /* Add SALS.nelts breakpoints to the breakpoint table. For each
8388 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8389 value. COND_STRING, if not NULL, specified the condition to be
8390 used for all breakpoints. Essentially the only case where
8391 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8392 function. In that case, it's still not possible to specify
8393 separate conditions for different overloaded functions, so
8394 we take just a single condition string.
8395
8396 NOTE: If the function succeeds, the caller is expected to cleanup
8397 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
8398 array contents). If the function fails (error() is called), the
8399 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
8400 COND and SALS arrays and each of those arrays contents. */
8401
8402 static void
8403 create_breakpoints_sal (struct gdbarch *gdbarch,
8404 struct linespec_result *canonical,
8405 gdb::unique_xmalloc_ptr<char> cond_string,
8406 gdb::unique_xmalloc_ptr<char> extra_string,
8407 enum bptype type, enum bpdisp disposition,
8408 int thread, int task, int ignore_count,
8409 const struct breakpoint_ops *ops, int from_tty,
8410 int enabled, int internal, unsigned flags)
8411 {
8412 if (canonical->pre_expanded)
8413 gdb_assert (canonical->lsals.size () == 1);
8414
8415 for (const auto &lsal : canonical->lsals)
8416 {
8417 /* Note that 'location' can be NULL in the case of a plain
8418 'break', without arguments. */
8419 event_location_up location
8420 = (canonical->location != NULL
8421 ? copy_event_location (canonical->location.get ()) : NULL);
8422 gdb::unique_xmalloc_ptr<char> filter_string
8423 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
8424
8425 create_breakpoint_sal (gdbarch, lsal.sals,
8426 std::move (location),
8427 std::move (filter_string),
8428 std::move (cond_string),
8429 std::move (extra_string),
8430 type, disposition,
8431 thread, task, ignore_count, ops,
8432 from_tty, enabled, internal, flags,
8433 canonical->special_display);
8434 }
8435 }
8436
8437 /* Parse LOCATION which is assumed to be a SAL specification possibly
8438 followed by conditionals. On return, SALS contains an array of SAL
8439 addresses found. LOCATION points to the end of the SAL (for
8440 linespec locations).
8441
8442 The array and the line spec strings are allocated on the heap, it is
8443 the caller's responsibility to free them. */
8444
8445 static void
8446 parse_breakpoint_sals (struct event_location *location,
8447 struct linespec_result *canonical)
8448 {
8449 struct symtab_and_line cursal;
8450
8451 if (event_location_type (location) == LINESPEC_LOCATION)
8452 {
8453 const char *spec = get_linespec_location (location)->spec_string;
8454
8455 if (spec == NULL)
8456 {
8457 /* The last displayed codepoint, if it's valid, is our default
8458 breakpoint address. */
8459 if (last_displayed_sal_is_valid ())
8460 {
8461 /* Set sal's pspace, pc, symtab, and line to the values
8462 corresponding to the last call to print_frame_info.
8463 Be sure to reinitialize LINE with NOTCURRENT == 0
8464 as the breakpoint line number is inappropriate otherwise.
8465 find_pc_line would adjust PC, re-set it back. */
8466 symtab_and_line sal = get_last_displayed_sal ();
8467 CORE_ADDR pc = sal.pc;
8468
8469 sal = find_pc_line (pc, 0);
8470
8471 /* "break" without arguments is equivalent to "break *PC"
8472 where PC is the last displayed codepoint's address. So
8473 make sure to set sal.explicit_pc to prevent GDB from
8474 trying to expand the list of sals to include all other
8475 instances with the same symtab and line. */
8476 sal.pc = pc;
8477 sal.explicit_pc = 1;
8478
8479 struct linespec_sals lsal;
8480 lsal.sals = {sal};
8481 lsal.canonical = NULL;
8482
8483 canonical->lsals.push_back (std::move (lsal));
8484 return;
8485 }
8486 else
8487 error (_("No default breakpoint address now."));
8488 }
8489 }
8490
8491 /* Force almost all breakpoints to be in terms of the
8492 current_source_symtab (which is decode_line_1's default).
8493 This should produce the results we want almost all of the
8494 time while leaving default_breakpoint_* alone.
8495
8496 ObjC: However, don't match an Objective-C method name which
8497 may have a '+' or '-' succeeded by a '['. */
8498 cursal = get_current_source_symtab_and_line ();
8499 if (last_displayed_sal_is_valid ())
8500 {
8501 const char *spec = NULL;
8502
8503 if (event_location_type (location) == LINESPEC_LOCATION)
8504 spec = get_linespec_location (location)->spec_string;
8505
8506 if (!cursal.symtab
8507 || (spec != NULL
8508 && strchr ("+-", spec[0]) != NULL
8509 && spec[1] != '['))
8510 {
8511 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
8512 get_last_displayed_symtab (),
8513 get_last_displayed_line (),
8514 canonical, NULL, NULL);
8515 return;
8516 }
8517 }
8518
8519 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
8520 cursal.symtab, cursal.line, canonical, NULL, NULL);
8521 }
8522
8523
8524 /* Convert each SAL into a real PC. Verify that the PC can be
8525 inserted as a breakpoint. If it can't throw an error. */
8526
8527 static void
8528 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
8529 {
8530 for (auto &sal : sals)
8531 resolve_sal_pc (&sal);
8532 }
8533
8534 /* Fast tracepoints may have restrictions on valid locations. For
8535 instance, a fast tracepoint using a jump instead of a trap will
8536 likely have to overwrite more bytes than a trap would, and so can
8537 only be placed where the instruction is longer than the jump, or a
8538 multi-instruction sequence does not have a jump into the middle of
8539 it, etc. */
8540
8541 static void
8542 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
8543 gdb::array_view<const symtab_and_line> sals)
8544 {
8545 for (const auto &sal : sals)
8546 {
8547 struct gdbarch *sarch;
8548
8549 sarch = get_sal_arch (sal);
8550 /* We fall back to GDBARCH if there is no architecture
8551 associated with SAL. */
8552 if (sarch == NULL)
8553 sarch = gdbarch;
8554 std::string msg;
8555 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
8556 error (_("May not have a fast tracepoint at %s%s"),
8557 paddress (sarch, sal.pc), msg.c_str ());
8558 }
8559 }
8560
8561 /* Given TOK, a string specification of condition and thread, as
8562 accepted by the 'break' command, extract the condition
8563 string and thread number and set *COND_STRING and *THREAD.
8564 PC identifies the context at which the condition should be parsed.
8565 If no condition is found, *COND_STRING is set to NULL.
8566 If no thread is found, *THREAD is set to -1. */
8567
8568 static void
8569 find_condition_and_thread (const char *tok, CORE_ADDR pc,
8570 gdb::unique_xmalloc_ptr<char> *cond_string,
8571 int *thread, int *task,
8572 gdb::unique_xmalloc_ptr<char> *rest)
8573 {
8574 cond_string->reset ();
8575 *thread = -1;
8576 *task = 0;
8577 rest->reset ();
8578 bool force = false;
8579
8580 while (tok && *tok)
8581 {
8582 const char *end_tok;
8583 int toklen;
8584 const char *cond_start = NULL;
8585 const char *cond_end = NULL;
8586
8587 tok = skip_spaces (tok);
8588
8589 if ((*tok == '"' || *tok == ',') && rest)
8590 {
8591 rest->reset (savestring (tok, strlen (tok)));
8592 return;
8593 }
8594
8595 end_tok = skip_to_space (tok);
8596
8597 toklen = end_tok - tok;
8598
8599 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8600 {
8601 tok = cond_start = end_tok + 1;
8602 try
8603 {
8604 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
8605 }
8606 catch (const gdb_exception_error &)
8607 {
8608 if (!force)
8609 throw;
8610 else
8611 tok = tok + strlen (tok);
8612 }
8613 cond_end = tok;
8614 cond_string->reset (savestring (cond_start, cond_end - cond_start));
8615 }
8616 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
8617 {
8618 tok = tok + toklen;
8619 force = true;
8620 }
8621 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8622 {
8623 const char *tmptok;
8624 struct thread_info *thr;
8625
8626 tok = end_tok + 1;
8627 thr = parse_thread_id (tok, &tmptok);
8628 if (tok == tmptok)
8629 error (_("Junk after thread keyword."));
8630 *thread = thr->global_num;
8631 tok = tmptok;
8632 }
8633 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
8634 {
8635 char *tmptok;
8636
8637 tok = end_tok + 1;
8638 *task = strtol (tok, &tmptok, 0);
8639 if (tok == tmptok)
8640 error (_("Junk after task keyword."));
8641 if (!valid_task_id (*task))
8642 error (_("Unknown task %d."), *task);
8643 tok = tmptok;
8644 }
8645 else if (rest)
8646 {
8647 rest->reset (savestring (tok, strlen (tok)));
8648 return;
8649 }
8650 else
8651 error (_("Junk at end of arguments."));
8652 }
8653 }
8654
8655 /* Call 'find_condition_and_thread' for each sal in SALS until a parse
8656 succeeds. The parsed values are written to COND_STRING, THREAD,
8657 TASK, and REST. See the comment of 'find_condition_and_thread'
8658 for the description of these parameters and INPUT. */
8659
8660 static void
8661 find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
8662 const char *input,
8663 gdb::unique_xmalloc_ptr<char> *cond_string,
8664 int *thread, int *task,
8665 gdb::unique_xmalloc_ptr<char> *rest)
8666 {
8667 int num_failures = 0;
8668 for (auto &sal : sals)
8669 {
8670 gdb::unique_xmalloc_ptr<char> cond;
8671 int thread_id = 0;
8672 int task_id = 0;
8673 gdb::unique_xmalloc_ptr<char> remaining;
8674
8675 /* Here we want to parse 'arg' to separate condition from thread
8676 number. But because parsing happens in a context and the
8677 contexts of sals might be different, try each until there is
8678 success. Finding one successful parse is sufficient for our
8679 goal. When setting the breakpoint we'll re-parse the
8680 condition in the context of each sal. */
8681 try
8682 {
8683 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
8684 &task_id, &remaining);
8685 *cond_string = std::move (cond);
8686 *thread = thread_id;
8687 *task = task_id;
8688 *rest = std::move (remaining);
8689 break;
8690 }
8691 catch (const gdb_exception_error &e)
8692 {
8693 num_failures++;
8694 /* If no sal remains, do not continue. */
8695 if (num_failures == sals.size ())
8696 throw;
8697 }
8698 }
8699 }
8700
8701 /* Decode a static tracepoint marker spec. */
8702
8703 static std::vector<symtab_and_line>
8704 decode_static_tracepoint_spec (const char **arg_p)
8705 {
8706 const char *p = &(*arg_p)[3];
8707 const char *endp;
8708
8709 p = skip_spaces (p);
8710
8711 endp = skip_to_space (p);
8712
8713 std::string marker_str (p, endp - p);
8714
8715 std::vector<static_tracepoint_marker> markers
8716 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
8717 if (markers.empty ())
8718 error (_("No known static tracepoint marker named %s"),
8719 marker_str.c_str ());
8720
8721 std::vector<symtab_and_line> sals;
8722 sals.reserve (markers.size ());
8723
8724 for (const static_tracepoint_marker &marker : markers)
8725 {
8726 symtab_and_line sal = find_pc_line (marker.address, 0);
8727 sal.pc = marker.address;
8728 sals.push_back (sal);
8729 }
8730
8731 *arg_p = endp;
8732 return sals;
8733 }
8734
8735 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
8736 according to IS_TRACEPOINT. */
8737
8738 static const struct breakpoint_ops *
8739 breakpoint_ops_for_event_location_type (enum event_location_type location_type,
8740 bool is_tracepoint)
8741 {
8742 if (is_tracepoint)
8743 {
8744 if (location_type == PROBE_LOCATION)
8745 return &tracepoint_probe_breakpoint_ops;
8746 else
8747 return &tracepoint_breakpoint_ops;
8748 }
8749 else
8750 {
8751 if (location_type == PROBE_LOCATION)
8752 return &bkpt_probe_breakpoint_ops;
8753 else
8754 return &bkpt_breakpoint_ops;
8755 }
8756 }
8757
8758 /* See breakpoint.h. */
8759
8760 const struct breakpoint_ops *
8761 breakpoint_ops_for_event_location (const struct event_location *location,
8762 bool is_tracepoint)
8763 {
8764 if (location != nullptr)
8765 return breakpoint_ops_for_event_location_type
8766 (event_location_type (location), is_tracepoint);
8767 return is_tracepoint ? &tracepoint_breakpoint_ops : &bkpt_breakpoint_ops;
8768 }
8769
8770 /* See breakpoint.h. */
8771
8772 int
8773 create_breakpoint (struct gdbarch *gdbarch,
8774 struct event_location *location,
8775 const char *cond_string,
8776 int thread, const char *extra_string,
8777 bool force_condition, int parse_extra,
8778 int tempflag, enum bptype type_wanted,
8779 int ignore_count,
8780 enum auto_boolean pending_break_support,
8781 const struct breakpoint_ops *ops,
8782 int from_tty, int enabled, int internal,
8783 unsigned flags)
8784 {
8785 struct linespec_result canonical;
8786 int pending = 0;
8787 int task = 0;
8788 int prev_bkpt_count = breakpoint_count;
8789
8790 gdb_assert (ops != NULL);
8791
8792 /* If extra_string isn't useful, set it to NULL. */
8793 if (extra_string != NULL && *extra_string == '\0')
8794 extra_string = NULL;
8795
8796 try
8797 {
8798 ops->create_sals_from_location (location, &canonical, type_wanted);
8799 }
8800 catch (const gdb_exception_error &e)
8801 {
8802 /* If caller is interested in rc value from parse, set
8803 value. */
8804 if (e.error == NOT_FOUND_ERROR)
8805 {
8806 /* If pending breakpoint support is turned off, throw
8807 error. */
8808
8809 if (pending_break_support == AUTO_BOOLEAN_FALSE)
8810 throw;
8811
8812 exception_print (gdb_stderr, e);
8813
8814 /* If pending breakpoint support is auto query and the user
8815 selects no, then simply return the error code. */
8816 if (pending_break_support == AUTO_BOOLEAN_AUTO
8817 && !nquery (_("Make %s pending on future shared library load? "),
8818 bptype_string (type_wanted)))
8819 return 0;
8820
8821 /* At this point, either the user was queried about setting
8822 a pending breakpoint and selected yes, or pending
8823 breakpoint behavior is on and thus a pending breakpoint
8824 is defaulted on behalf of the user. */
8825 pending = 1;
8826 }
8827 else
8828 throw;
8829 }
8830
8831 if (!pending && canonical.lsals.empty ())
8832 return 0;
8833
8834 /* Resolve all line numbers to PC's and verify that the addresses
8835 are ok for the target. */
8836 if (!pending)
8837 {
8838 for (auto &lsal : canonical.lsals)
8839 breakpoint_sals_to_pc (lsal.sals);
8840 }
8841
8842 /* Fast tracepoints may have additional restrictions on location. */
8843 if (!pending && type_wanted == bp_fast_tracepoint)
8844 {
8845 for (const auto &lsal : canonical.lsals)
8846 check_fast_tracepoint_sals (gdbarch, lsal.sals);
8847 }
8848
8849 /* Verify that condition can be parsed, before setting any
8850 breakpoints. Allocate a separate condition expression for each
8851 breakpoint. */
8852 if (!pending)
8853 {
8854 gdb::unique_xmalloc_ptr<char> cond_string_copy;
8855 gdb::unique_xmalloc_ptr<char> extra_string_copy;
8856
8857 if (parse_extra)
8858 {
8859 gdb::unique_xmalloc_ptr<char> rest;
8860 gdb::unique_xmalloc_ptr<char> cond;
8861
8862 const linespec_sals &lsal = canonical.lsals[0];
8863
8864 find_condition_and_thread_for_sals (lsal.sals, extra_string,
8865 &cond, &thread, &task, &rest);
8866 cond_string_copy = std::move (cond);
8867 extra_string_copy = std::move (rest);
8868 }
8869 else
8870 {
8871 if (type_wanted != bp_dprintf
8872 && extra_string != NULL && *extra_string != '\0')
8873 error (_("Garbage '%s' at end of location"), extra_string);
8874
8875 /* Check the validity of the condition. We should error out
8876 if the condition is invalid at all of the locations and
8877 if it is not forced. In the PARSE_EXTRA case above, this
8878 check is done when parsing the EXTRA_STRING. */
8879 if (cond_string != nullptr && !force_condition)
8880 {
8881 int num_failures = 0;
8882 const linespec_sals &lsal = canonical.lsals[0];
8883 for (const auto &sal : lsal.sals)
8884 {
8885 const char *cond = cond_string;
8886 try
8887 {
8888 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
8889 /* One success is sufficient to keep going. */
8890 break;
8891 }
8892 catch (const gdb_exception_error &)
8893 {
8894 num_failures++;
8895 /* If this is the last sal, error out. */
8896 if (num_failures == lsal.sals.size ())
8897 throw;
8898 }
8899 }
8900 }
8901
8902 /* Create a private copy of condition string. */
8903 if (cond_string)
8904 cond_string_copy.reset (xstrdup (cond_string));
8905 /* Create a private copy of any extra string. */
8906 if (extra_string)
8907 extra_string_copy.reset (xstrdup (extra_string));
8908 }
8909
8910 ops->create_breakpoints_sal (gdbarch, &canonical,
8911 std::move (cond_string_copy),
8912 std::move (extra_string_copy),
8913 type_wanted,
8914 tempflag ? disp_del : disp_donttouch,
8915 thread, task, ignore_count, ops,
8916 from_tty, enabled, internal, flags);
8917 }
8918 else
8919 {
8920 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
8921
8922 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
8923 b->location = copy_event_location (location);
8924
8925 if (parse_extra)
8926 b->cond_string = NULL;
8927 else
8928 {
8929 /* Create a private copy of condition string. */
8930 b->cond_string.reset (cond_string != NULL
8931 ? xstrdup (cond_string)
8932 : NULL);
8933 b->thread = thread;
8934 }
8935
8936 /* Create a private copy of any extra string. */
8937 b->extra_string.reset (extra_string != NULL
8938 ? xstrdup (extra_string)
8939 : NULL);
8940 b->ignore_count = ignore_count;
8941 b->disposition = tempflag ? disp_del : disp_donttouch;
8942 b->condition_not_parsed = 1;
8943 b->enable_state = enabled ? bp_enabled : bp_disabled;
8944 if ((type_wanted != bp_breakpoint
8945 && type_wanted != bp_hardware_breakpoint) || thread != -1)
8946 b->pspace = current_program_space;
8947
8948 install_breakpoint (internal, std::move (b), 0);
8949 }
8950
8951 if (canonical.lsals.size () > 1)
8952 {
8953 warning (_("Multiple breakpoints were set.\nUse the "
8954 "\"delete\" command to delete unwanted breakpoints."));
8955 prev_breakpoint_count = prev_bkpt_count;
8956 }
8957
8958 update_global_location_list (UGLL_MAY_INSERT);
8959
8960 return 1;
8961 }
8962
8963 /* Set a breakpoint.
8964 ARG is a string describing breakpoint address,
8965 condition, and thread.
8966 FLAG specifies if a breakpoint is hardware on,
8967 and if breakpoint is temporary, using BP_HARDWARE_FLAG
8968 and BP_TEMPFLAG. */
8969
8970 static void
8971 break_command_1 (const char *arg, int flag, int from_tty)
8972 {
8973 int tempflag = flag & BP_TEMPFLAG;
8974 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
8975 ? bp_hardware_breakpoint
8976 : bp_breakpoint);
8977
8978 event_location_up location = string_to_event_location (&arg, current_language);
8979 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
8980 (location.get (), false /* is_tracepoint */);
8981
8982 create_breakpoint (get_current_arch (),
8983 location.get (),
8984 NULL, 0, arg, false, 1 /* parse arg */,
8985 tempflag, type_wanted,
8986 0 /* Ignore count */,
8987 pending_break_support,
8988 ops,
8989 from_tty,
8990 1 /* enabled */,
8991 0 /* internal */,
8992 0);
8993 }
8994
8995 /* Helper function for break_command_1 and disassemble_command. */
8996
8997 void
8998 resolve_sal_pc (struct symtab_and_line *sal)
8999 {
9000 CORE_ADDR pc;
9001
9002 if (sal->pc == 0 && sal->symtab != NULL)
9003 {
9004 if (!find_line_pc (sal->symtab, sal->line, &pc))
9005 error (_("No line %d in file \"%s\"."),
9006 sal->line, symtab_to_filename_for_display (sal->symtab));
9007 sal->pc = pc;
9008
9009 /* If this SAL corresponds to a breakpoint inserted using a line
9010 number, then skip the function prologue if necessary. */
9011 if (sal->explicit_line)
9012 skip_prologue_sal (sal);
9013 }
9014
9015 if (sal->section == 0 && sal->symtab != NULL)
9016 {
9017 const struct blockvector *bv;
9018 const struct block *b;
9019 struct symbol *sym;
9020
9021 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9022 sal->symtab->compunit ());
9023 if (bv != NULL)
9024 {
9025 sym = block_linkage_function (b);
9026 if (sym != NULL)
9027 {
9028 fixup_symbol_section (sym, sal->symtab->compunit ()->objfile ());
9029 sal->section
9030 = sym->obj_section (sal->symtab->compunit ()->objfile ());
9031 }
9032 else
9033 {
9034 /* It really is worthwhile to have the section, so we'll
9035 just have to look harder. This case can be executed
9036 if we have line numbers but no functions (as can
9037 happen in assembly source). */
9038
9039 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9040 switch_to_program_space_and_thread (sal->pspace);
9041
9042 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9043 if (msym.minsym)
9044 sal->section = msym.obj_section ();
9045 }
9046 }
9047 }
9048 }
9049
9050 void
9051 break_command (const char *arg, int from_tty)
9052 {
9053 break_command_1 (arg, 0, from_tty);
9054 }
9055
9056 void
9057 tbreak_command (const char *arg, int from_tty)
9058 {
9059 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9060 }
9061
9062 static void
9063 hbreak_command (const char *arg, int from_tty)
9064 {
9065 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9066 }
9067
9068 static void
9069 thbreak_command (const char *arg, int from_tty)
9070 {
9071 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9072 }
9073
9074 /* The dynamic printf command is mostly like a regular breakpoint, but
9075 with a prewired command list consisting of a single output command,
9076 built from extra arguments supplied on the dprintf command
9077 line. */
9078
9079 static void
9080 dprintf_command (const char *arg, int from_tty)
9081 {
9082 event_location_up location = string_to_event_location (&arg, current_language);
9083
9084 /* If non-NULL, ARG should have been advanced past the location;
9085 the next character must be ','. */
9086 if (arg != NULL)
9087 {
9088 if (arg[0] != ',' || arg[1] == '\0')
9089 error (_("Format string required"));
9090 else
9091 {
9092 /* Skip the comma. */
9093 ++arg;
9094 }
9095 }
9096
9097 create_breakpoint (get_current_arch (),
9098 location.get (),
9099 NULL, 0, arg, false, 1 /* parse arg */,
9100 0, bp_dprintf,
9101 0 /* Ignore count */,
9102 pending_break_support,
9103 &dprintf_breakpoint_ops,
9104 from_tty,
9105 1 /* enabled */,
9106 0 /* internal */,
9107 0);
9108 }
9109
9110 static void
9111 agent_printf_command (const char *arg, int from_tty)
9112 {
9113 error (_("May only run agent-printf on the target"));
9114 }
9115
9116 /* Implement the "breakpoint_hit" breakpoint_ops method for
9117 ranged breakpoints. */
9118
9119 static int
9120 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9121 const address_space *aspace,
9122 CORE_ADDR bp_addr,
9123 const target_waitstatus &ws)
9124 {
9125 if (ws.kind () != TARGET_WAITKIND_STOPPED
9126 || ws.sig () != GDB_SIGNAL_TRAP)
9127 return 0;
9128
9129 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9130 bl->length, aspace, bp_addr);
9131 }
9132
9133 /* Implement the "resources_needed" breakpoint_ops method for
9134 ranged breakpoints. */
9135
9136 static int
9137 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9138 {
9139 return target_ranged_break_num_registers ();
9140 }
9141
9142 /* Implement the "print_it" breakpoint_ops method for
9143 ranged breakpoints. */
9144
9145 static enum print_stop_action
9146 print_it_ranged_breakpoint (bpstat *bs)
9147 {
9148 struct breakpoint *b = bs->breakpoint_at;
9149 struct bp_location *bl = b->loc;
9150 struct ui_out *uiout = current_uiout;
9151
9152 gdb_assert (b->type == bp_hardware_breakpoint);
9153
9154 /* Ranged breakpoints have only one location. */
9155 gdb_assert (bl && bl->next == NULL);
9156
9157 annotate_breakpoint (b->number);
9158
9159 maybe_print_thread_hit_breakpoint (uiout);
9160
9161 if (b->disposition == disp_del)
9162 uiout->text ("Temporary ranged breakpoint ");
9163 else
9164 uiout->text ("Ranged breakpoint ");
9165 if (uiout->is_mi_like_p ())
9166 {
9167 uiout->field_string ("reason",
9168 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9169 uiout->field_string ("disp", bpdisp_text (b->disposition));
9170 }
9171 uiout->field_signed ("bkptno", b->number);
9172 uiout->text (", ");
9173
9174 return PRINT_SRC_AND_LOC;
9175 }
9176
9177 /* Implement the "print_one" breakpoint_ops method for
9178 ranged breakpoints. */
9179
9180 static bool
9181 print_one_ranged_breakpoint (struct breakpoint *b,
9182 struct bp_location **last_loc)
9183 {
9184 struct bp_location *bl = b->loc;
9185 struct value_print_options opts;
9186 struct ui_out *uiout = current_uiout;
9187
9188 /* Ranged breakpoints have only one location. */
9189 gdb_assert (bl && bl->next == NULL);
9190
9191 get_user_print_options (&opts);
9192
9193 if (opts.addressprint)
9194 /* We don't print the address range here, it will be printed later
9195 by print_one_detail_ranged_breakpoint. */
9196 uiout->field_skip ("addr");
9197 annotate_field (5);
9198 print_breakpoint_location (b, bl);
9199 *last_loc = bl;
9200
9201 return true;
9202 }
9203
9204 /* Implement the "print_one_detail" breakpoint_ops method for
9205 ranged breakpoints. */
9206
9207 static void
9208 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9209 struct ui_out *uiout)
9210 {
9211 CORE_ADDR address_start, address_end;
9212 struct bp_location *bl = b->loc;
9213 string_file stb;
9214
9215 gdb_assert (bl);
9216
9217 address_start = bl->address;
9218 address_end = address_start + bl->length - 1;
9219
9220 uiout->text ("\taddress range: ");
9221 stb.printf ("[%s, %s]",
9222 print_core_address (bl->gdbarch, address_start),
9223 print_core_address (bl->gdbarch, address_end));
9224 uiout->field_stream ("addr", stb);
9225 uiout->text ("\n");
9226 }
9227
9228 /* Implement the "print_mention" breakpoint_ops method for
9229 ranged breakpoints. */
9230
9231 static void
9232 print_mention_ranged_breakpoint (struct breakpoint *b)
9233 {
9234 struct bp_location *bl = b->loc;
9235 struct ui_out *uiout = current_uiout;
9236
9237 gdb_assert (bl);
9238 gdb_assert (b->type == bp_hardware_breakpoint);
9239
9240 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9241 b->number, paddress (bl->gdbarch, bl->address),
9242 paddress (bl->gdbarch, bl->address + bl->length - 1));
9243 }
9244
9245 /* Implement the "print_recreate" breakpoint_ops method for
9246 ranged breakpoints. */
9247
9248 static void
9249 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9250 {
9251 gdb_printf (fp, "break-range %s, %s",
9252 event_location_to_string (b->location.get ()),
9253 event_location_to_string (b->location_range_end.get ()));
9254 print_recreate_thread (b, fp);
9255 }
9256
9257 /* The breakpoint_ops structure to be used in ranged breakpoints. */
9258
9259 static struct breakpoint_ops ranged_breakpoint_ops;
9260
9261 /* Find the address where the end of the breakpoint range should be
9262 placed, given the SAL of the end of the range. This is so that if
9263 the user provides a line number, the end of the range is set to the
9264 last instruction of the given line. */
9265
9266 static CORE_ADDR
9267 find_breakpoint_range_end (struct symtab_and_line sal)
9268 {
9269 CORE_ADDR end;
9270
9271 /* If the user provided a PC value, use it. Otherwise,
9272 find the address of the end of the given location. */
9273 if (sal.explicit_pc)
9274 end = sal.pc;
9275 else
9276 {
9277 int ret;
9278 CORE_ADDR start;
9279
9280 ret = find_line_pc_range (sal, &start, &end);
9281 if (!ret)
9282 error (_("Could not find location of the end of the range."));
9283
9284 /* find_line_pc_range returns the start of the next line. */
9285 end--;
9286 }
9287
9288 return end;
9289 }
9290
9291 /* Implement the "break-range" CLI command. */
9292
9293 static void
9294 break_range_command (const char *arg, int from_tty)
9295 {
9296 const char *arg_start;
9297 struct linespec_result canonical_start, canonical_end;
9298 int bp_count, can_use_bp, length;
9299 CORE_ADDR end;
9300 struct breakpoint *b;
9301
9302 /* We don't support software ranged breakpoints. */
9303 if (target_ranged_break_num_registers () < 0)
9304 error (_("This target does not support hardware ranged breakpoints."));
9305
9306 bp_count = hw_breakpoint_used_count ();
9307 bp_count += target_ranged_break_num_registers ();
9308 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9309 bp_count, 0);
9310 if (can_use_bp < 0)
9311 error (_("Hardware breakpoints used exceeds limit."));
9312
9313 arg = skip_spaces (arg);
9314 if (arg == NULL || arg[0] == '\0')
9315 error(_("No address range specified."));
9316
9317 arg_start = arg;
9318 event_location_up start_location = string_to_event_location (&arg,
9319 current_language);
9320 parse_breakpoint_sals (start_location.get (), &canonical_start);
9321
9322 if (arg[0] != ',')
9323 error (_("Too few arguments."));
9324 else if (canonical_start.lsals.empty ())
9325 error (_("Could not find location of the beginning of the range."));
9326
9327 const linespec_sals &lsal_start = canonical_start.lsals[0];
9328
9329 if (canonical_start.lsals.size () > 1
9330 || lsal_start.sals.size () != 1)
9331 error (_("Cannot create a ranged breakpoint with multiple locations."));
9332
9333 const symtab_and_line &sal_start = lsal_start.sals[0];
9334 std::string addr_string_start (arg_start, arg - arg_start);
9335
9336 arg++; /* Skip the comma. */
9337 arg = skip_spaces (arg);
9338
9339 /* Parse the end location. */
9340
9341 arg_start = arg;
9342
9343 /* We call decode_line_full directly here instead of using
9344 parse_breakpoint_sals because we need to specify the start location's
9345 symtab and line as the default symtab and line for the end of the
9346 range. This makes it possible to have ranges like "foo.c:27, +14",
9347 where +14 means 14 lines from the start location. */
9348 event_location_up end_location = string_to_event_location (&arg,
9349 current_language);
9350 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
9351 sal_start.symtab, sal_start.line,
9352 &canonical_end, NULL, NULL);
9353
9354 if (canonical_end.lsals.empty ())
9355 error (_("Could not find location of the end of the range."));
9356
9357 const linespec_sals &lsal_end = canonical_end.lsals[0];
9358 if (canonical_end.lsals.size () > 1
9359 || lsal_end.sals.size () != 1)
9360 error (_("Cannot create a ranged breakpoint with multiple locations."));
9361
9362 const symtab_and_line &sal_end = lsal_end.sals[0];
9363
9364 end = find_breakpoint_range_end (sal_end);
9365 if (sal_start.pc > end)
9366 error (_("Invalid address range, end precedes start."));
9367
9368 length = end - sal_start.pc + 1;
9369 if (length < 0)
9370 /* Length overflowed. */
9371 error (_("Address range too large."));
9372 else if (length == 1)
9373 {
9374 /* This range is simple enough to be handled by
9375 the `hbreak' command. */
9376 hbreak_command (&addr_string_start[0], 1);
9377
9378 return;
9379 }
9380
9381 /* Now set up the breakpoint. */
9382 b = set_raw_breakpoint (get_current_arch (), sal_start,
9383 bp_hardware_breakpoint, &ranged_breakpoint_ops);
9384 set_breakpoint_count (breakpoint_count + 1);
9385 b->number = breakpoint_count;
9386 b->disposition = disp_donttouch;
9387 b->location = std::move (start_location);
9388 b->location_range_end = std::move (end_location);
9389 b->loc->length = length;
9390
9391 mention (b);
9392 gdb::observers::breakpoint_created.notify (b);
9393 update_global_location_list (UGLL_MAY_INSERT);
9394 }
9395
9396 /* Return non-zero if EXP is verified as constant. Returned zero
9397 means EXP is variable. Also the constant detection may fail for
9398 some constant expressions and in such case still falsely return
9399 zero. */
9400
9401 static bool
9402 watchpoint_exp_is_const (const struct expression *exp)
9403 {
9404 return exp->op->constant_p ();
9405 }
9406
9407 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
9408
9409 static void
9410 re_set_watchpoint (struct breakpoint *b)
9411 {
9412 struct watchpoint *w = (struct watchpoint *) b;
9413
9414 /* Watchpoint can be either on expression using entirely global
9415 variables, or it can be on local variables.
9416
9417 Watchpoints of the first kind are never auto-deleted, and even
9418 persist across program restarts. Since they can use variables
9419 from shared libraries, we need to reparse expression as libraries
9420 are loaded and unloaded.
9421
9422 Watchpoints on local variables can also change meaning as result
9423 of solib event. For example, if a watchpoint uses both a local
9424 and a global variables in expression, it's a local watchpoint,
9425 but unloading of a shared library will make the expression
9426 invalid. This is not a very common use case, but we still
9427 re-evaluate expression, to avoid surprises to the user.
9428
9429 Note that for local watchpoints, we re-evaluate it only if
9430 watchpoints frame id is still valid. If it's not, it means the
9431 watchpoint is out of scope and will be deleted soon. In fact,
9432 I'm not sure we'll ever be called in this case.
9433
9434 If a local watchpoint's frame id is still valid, then
9435 w->exp_valid_block is likewise valid, and we can safely use it.
9436
9437 Don't do anything about disabled watchpoints, since they will be
9438 reevaluated again when enabled. */
9439 update_watchpoint (w, 1 /* reparse */);
9440 }
9441
9442 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
9443
9444 static int
9445 insert_watchpoint (struct bp_location *bl)
9446 {
9447 struct watchpoint *w = (struct watchpoint *) bl->owner;
9448 int length = w->exact ? 1 : bl->length;
9449
9450 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
9451 w->cond_exp.get ());
9452 }
9453
9454 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
9455
9456 static int
9457 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
9458 {
9459 struct watchpoint *w = (struct watchpoint *) bl->owner;
9460 int length = w->exact ? 1 : bl->length;
9461
9462 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
9463 w->cond_exp.get ());
9464 }
9465
9466 static int
9467 breakpoint_hit_watchpoint (const struct bp_location *bl,
9468 const address_space *aspace, CORE_ADDR bp_addr,
9469 const target_waitstatus &ws)
9470 {
9471 struct breakpoint *b = bl->owner;
9472 struct watchpoint *w = (struct watchpoint *) b;
9473
9474 /* Continuable hardware watchpoints are treated as non-existent if the
9475 reason we stopped wasn't a hardware watchpoint (we didn't stop on
9476 some data address). Otherwise gdb won't stop on a break instruction
9477 in the code (not from a breakpoint) when a hardware watchpoint has
9478 been defined. Also skip watchpoints which we know did not trigger
9479 (did not match the data address). */
9480 if (is_hardware_watchpoint (b)
9481 && w->watchpoint_triggered == watch_triggered_no)
9482 return 0;
9483
9484 return 1;
9485 }
9486
9487 static void
9488 check_status_watchpoint (bpstat *bs)
9489 {
9490 gdb_assert (is_watchpoint (bs->breakpoint_at));
9491
9492 bpstat_check_watchpoint (bs);
9493 }
9494
9495 /* Implement the "resources_needed" breakpoint_ops method for
9496 hardware watchpoints. */
9497
9498 static int
9499 resources_needed_watchpoint (const struct bp_location *bl)
9500 {
9501 struct watchpoint *w = (struct watchpoint *) bl->owner;
9502 int length = w->exact? 1 : bl->length;
9503
9504 return target_region_ok_for_hw_watchpoint (bl->address, length);
9505 }
9506
9507 /* Implement the "works_in_software_mode" breakpoint_ops method for
9508 hardware watchpoints. */
9509
9510 static int
9511 works_in_software_mode_watchpoint (const struct breakpoint *b)
9512 {
9513 /* Read and access watchpoints only work with hardware support. */
9514 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9515 }
9516
9517 static enum print_stop_action
9518 print_it_watchpoint (bpstat *bs)
9519 {
9520 struct breakpoint *b;
9521 enum print_stop_action result;
9522 struct watchpoint *w;
9523 struct ui_out *uiout = current_uiout;
9524
9525 gdb_assert (bs->bp_location_at != NULL);
9526
9527 b = bs->breakpoint_at;
9528 w = (struct watchpoint *) b;
9529
9530 annotate_watchpoint (b->number);
9531 maybe_print_thread_hit_breakpoint (uiout);
9532
9533 string_file stb;
9534
9535 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9536 switch (b->type)
9537 {
9538 case bp_watchpoint:
9539 case bp_hardware_watchpoint:
9540 if (uiout->is_mi_like_p ())
9541 uiout->field_string
9542 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
9543 mention (b);
9544 tuple_emitter.emplace (uiout, "value");
9545 uiout->text ("\nOld value = ");
9546 watchpoint_value_print (bs->old_val.get (), &stb);
9547 uiout->field_stream ("old", stb);
9548 uiout->text ("\nNew value = ");
9549 watchpoint_value_print (w->val.get (), &stb);
9550 uiout->field_stream ("new", stb);
9551 uiout->text ("\n");
9552 /* More than one watchpoint may have been triggered. */
9553 result = PRINT_UNKNOWN;
9554 break;
9555
9556 case bp_read_watchpoint:
9557 if (uiout->is_mi_like_p ())
9558 uiout->field_string
9559 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
9560 mention (b);
9561 tuple_emitter.emplace (uiout, "value");
9562 uiout->text ("\nValue = ");
9563 watchpoint_value_print (w->val.get (), &stb);
9564 uiout->field_stream ("value", stb);
9565 uiout->text ("\n");
9566 result = PRINT_UNKNOWN;
9567 break;
9568
9569 case bp_access_watchpoint:
9570 if (bs->old_val != NULL)
9571 {
9572 if (uiout->is_mi_like_p ())
9573 uiout->field_string
9574 ("reason",
9575 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9576 mention (b);
9577 tuple_emitter.emplace (uiout, "value");
9578 uiout->text ("\nOld value = ");
9579 watchpoint_value_print (bs->old_val.get (), &stb);
9580 uiout->field_stream ("old", stb);
9581 uiout->text ("\nNew value = ");
9582 }
9583 else
9584 {
9585 mention (b);
9586 if (uiout->is_mi_like_p ())
9587 uiout->field_string
9588 ("reason",
9589 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9590 tuple_emitter.emplace (uiout, "value");
9591 uiout->text ("\nValue = ");
9592 }
9593 watchpoint_value_print (w->val.get (), &stb);
9594 uiout->field_stream ("new", stb);
9595 uiout->text ("\n");
9596 result = PRINT_UNKNOWN;
9597 break;
9598 default:
9599 result = PRINT_UNKNOWN;
9600 }
9601
9602 return result;
9603 }
9604
9605 /* Implement the "print_mention" breakpoint_ops method for hardware
9606 watchpoints. */
9607
9608 static void
9609 print_mention_watchpoint (struct breakpoint *b)
9610 {
9611 struct watchpoint *w = (struct watchpoint *) b;
9612 struct ui_out *uiout = current_uiout;
9613 const char *tuple_name;
9614
9615 switch (b->type)
9616 {
9617 case bp_watchpoint:
9618 uiout->text ("Watchpoint ");
9619 tuple_name = "wpt";
9620 break;
9621 case bp_hardware_watchpoint:
9622 uiout->text ("Hardware watchpoint ");
9623 tuple_name = "wpt";
9624 break;
9625 case bp_read_watchpoint:
9626 uiout->text ("Hardware read watchpoint ");
9627 tuple_name = "hw-rwpt";
9628 break;
9629 case bp_access_watchpoint:
9630 uiout->text ("Hardware access (read/write) watchpoint ");
9631 tuple_name = "hw-awpt";
9632 break;
9633 default:
9634 internal_error (__FILE__, __LINE__,
9635 _("Invalid hardware watchpoint type."));
9636 }
9637
9638 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
9639 uiout->field_signed ("number", b->number);
9640 uiout->text (": ");
9641 uiout->field_string ("exp", w->exp_string.get ());
9642 }
9643
9644 /* Implement the "print_recreate" breakpoint_ops method for
9645 watchpoints. */
9646
9647 static void
9648 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
9649 {
9650 struct watchpoint *w = (struct watchpoint *) b;
9651
9652 switch (b->type)
9653 {
9654 case bp_watchpoint:
9655 case bp_hardware_watchpoint:
9656 gdb_printf (fp, "watch");
9657 break;
9658 case bp_read_watchpoint:
9659 gdb_printf (fp, "rwatch");
9660 break;
9661 case bp_access_watchpoint:
9662 gdb_printf (fp, "awatch");
9663 break;
9664 default:
9665 internal_error (__FILE__, __LINE__,
9666 _("Invalid watchpoint type."));
9667 }
9668
9669 gdb_printf (fp, " %s", w->exp_string.get ());
9670 print_recreate_thread (b, fp);
9671 }
9672
9673 /* Implement the "explains_signal" breakpoint_ops method for
9674 watchpoints. */
9675
9676 static int
9677 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
9678 {
9679 /* A software watchpoint cannot cause a signal other than
9680 GDB_SIGNAL_TRAP. */
9681 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
9682 return 0;
9683
9684 return 1;
9685 }
9686
9687 /* The breakpoint_ops structure to be used in hardware watchpoints. */
9688
9689 static struct breakpoint_ops watchpoint_breakpoint_ops;
9690
9691 /* Implement the "insert" breakpoint_ops method for
9692 masked hardware watchpoints. */
9693
9694 static int
9695 insert_masked_watchpoint (struct bp_location *bl)
9696 {
9697 struct watchpoint *w = (struct watchpoint *) bl->owner;
9698
9699 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
9700 bl->watchpoint_type);
9701 }
9702
9703 /* Implement the "remove" breakpoint_ops method for
9704 masked hardware watchpoints. */
9705
9706 static int
9707 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
9708 {
9709 struct watchpoint *w = (struct watchpoint *) bl->owner;
9710
9711 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
9712 bl->watchpoint_type);
9713 }
9714
9715 /* Implement the "resources_needed" breakpoint_ops method for
9716 masked hardware watchpoints. */
9717
9718 static int
9719 resources_needed_masked_watchpoint (const struct bp_location *bl)
9720 {
9721 struct watchpoint *w = (struct watchpoint *) bl->owner;
9722
9723 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
9724 }
9725
9726 /* Implement the "works_in_software_mode" breakpoint_ops method for
9727 masked hardware watchpoints. */
9728
9729 static int
9730 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
9731 {
9732 return 0;
9733 }
9734
9735 /* Implement the "print_it" breakpoint_ops method for
9736 masked hardware watchpoints. */
9737
9738 static enum print_stop_action
9739 print_it_masked_watchpoint (bpstat *bs)
9740 {
9741 struct breakpoint *b = bs->breakpoint_at;
9742 struct ui_out *uiout = current_uiout;
9743
9744 /* Masked watchpoints have only one location. */
9745 gdb_assert (b->loc && b->loc->next == NULL);
9746
9747 annotate_watchpoint (b->number);
9748 maybe_print_thread_hit_breakpoint (uiout);
9749
9750 switch (b->type)
9751 {
9752 case bp_hardware_watchpoint:
9753 if (uiout->is_mi_like_p ())
9754 uiout->field_string
9755 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
9756 break;
9757
9758 case bp_read_watchpoint:
9759 if (uiout->is_mi_like_p ())
9760 uiout->field_string
9761 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
9762 break;
9763
9764 case bp_access_watchpoint:
9765 if (uiout->is_mi_like_p ())
9766 uiout->field_string
9767 ("reason",
9768 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9769 break;
9770 default:
9771 internal_error (__FILE__, __LINE__,
9772 _("Invalid hardware watchpoint type."));
9773 }
9774
9775 mention (b);
9776 uiout->text (_("\n\
9777 Check the underlying instruction at PC for the memory\n\
9778 address and value which triggered this watchpoint.\n"));
9779 uiout->text ("\n");
9780
9781 /* More than one watchpoint may have been triggered. */
9782 return PRINT_UNKNOWN;
9783 }
9784
9785 /* Implement the "print_one_detail" breakpoint_ops method for
9786 masked hardware watchpoints. */
9787
9788 static void
9789 print_one_detail_masked_watchpoint (const struct breakpoint *b,
9790 struct ui_out *uiout)
9791 {
9792 struct watchpoint *w = (struct watchpoint *) b;
9793
9794 /* Masked watchpoints have only one location. */
9795 gdb_assert (b->loc && b->loc->next == NULL);
9796
9797 uiout->text ("\tmask ");
9798 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
9799 uiout->text ("\n");
9800 }
9801
9802 /* Implement the "print_mention" breakpoint_ops method for
9803 masked hardware watchpoints. */
9804
9805 static void
9806 print_mention_masked_watchpoint (struct breakpoint *b)
9807 {
9808 struct watchpoint *w = (struct watchpoint *) b;
9809 struct ui_out *uiout = current_uiout;
9810 const char *tuple_name;
9811
9812 switch (b->type)
9813 {
9814 case bp_hardware_watchpoint:
9815 uiout->text ("Masked hardware watchpoint ");
9816 tuple_name = "wpt";
9817 break;
9818 case bp_read_watchpoint:
9819 uiout->text ("Masked hardware read watchpoint ");
9820 tuple_name = "hw-rwpt";
9821 break;
9822 case bp_access_watchpoint:
9823 uiout->text ("Masked hardware access (read/write) watchpoint ");
9824 tuple_name = "hw-awpt";
9825 break;
9826 default:
9827 internal_error (__FILE__, __LINE__,
9828 _("Invalid hardware watchpoint type."));
9829 }
9830
9831 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
9832 uiout->field_signed ("number", b->number);
9833 uiout->text (": ");
9834 uiout->field_string ("exp", w->exp_string.get ());
9835 }
9836
9837 /* Implement the "print_recreate" breakpoint_ops method for
9838 masked hardware watchpoints. */
9839
9840 static void
9841 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
9842 {
9843 struct watchpoint *w = (struct watchpoint *) b;
9844
9845 switch (b->type)
9846 {
9847 case bp_hardware_watchpoint:
9848 gdb_printf (fp, "watch");
9849 break;
9850 case bp_read_watchpoint:
9851 gdb_printf (fp, "rwatch");
9852 break;
9853 case bp_access_watchpoint:
9854 gdb_printf (fp, "awatch");
9855 break;
9856 default:
9857 internal_error (__FILE__, __LINE__,
9858 _("Invalid hardware watchpoint type."));
9859 }
9860
9861 gdb_printf (fp, " %s mask 0x%s", w->exp_string.get (),
9862 phex (w->hw_wp_mask, sizeof (CORE_ADDR)));
9863 print_recreate_thread (b, fp);
9864 }
9865
9866 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
9867
9868 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9869
9870 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
9871
9872 static bool
9873 is_masked_watchpoint (const struct breakpoint *b)
9874 {
9875 return b->ops == &masked_watchpoint_breakpoint_ops;
9876 }
9877
9878 /* accessflag: hw_write: watch write,
9879 hw_read: watch read,
9880 hw_access: watch access (read or write) */
9881 static void
9882 watch_command_1 (const char *arg, int accessflag, int from_tty,
9883 bool just_location, bool internal)
9884 {
9885 struct breakpoint *scope_breakpoint = NULL;
9886 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
9887 struct value *result;
9888 int saved_bitpos = 0, saved_bitsize = 0;
9889 const char *exp_start = NULL;
9890 const char *exp_end = NULL;
9891 const char *tok, *end_tok;
9892 int toklen = -1;
9893 const char *cond_start = NULL;
9894 const char *cond_end = NULL;
9895 enum bptype bp_type;
9896 int thread = -1;
9897 /* Flag to indicate whether we are going to use masks for
9898 the hardware watchpoint. */
9899 bool use_mask = false;
9900 CORE_ADDR mask = 0;
9901 int task = 0;
9902
9903 /* Make sure that we actually have parameters to parse. */
9904 if (arg != NULL && arg[0] != '\0')
9905 {
9906 const char *value_start;
9907
9908 exp_end = arg + strlen (arg);
9909
9910 /* Look for "parameter value" pairs at the end
9911 of the arguments string. */
9912 for (tok = exp_end - 1; tok > arg; tok--)
9913 {
9914 /* Skip whitespace at the end of the argument list. */
9915 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9916 tok--;
9917
9918 /* Find the beginning of the last token.
9919 This is the value of the parameter. */
9920 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9921 tok--;
9922 value_start = tok + 1;
9923
9924 /* Skip whitespace. */
9925 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9926 tok--;
9927
9928 end_tok = tok;
9929
9930 /* Find the beginning of the second to last token.
9931 This is the parameter itself. */
9932 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9933 tok--;
9934 tok++;
9935 toklen = end_tok - tok + 1;
9936
9937 if (toklen == 6 && startswith (tok, "thread"))
9938 {
9939 struct thread_info *thr;
9940 /* At this point we've found a "thread" token, which means
9941 the user is trying to set a watchpoint that triggers
9942 only in a specific thread. */
9943 const char *endp;
9944
9945 if (thread != -1)
9946 error(_("You can specify only one thread."));
9947
9948 /* Extract the thread ID from the next token. */
9949 thr = parse_thread_id (value_start, &endp);
9950
9951 /* Check if the user provided a valid thread ID. */
9952 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
9953 invalid_thread_id_error (value_start);
9954
9955 thread = thr->global_num;
9956 }
9957 else if (toklen == 4 && startswith (tok, "task"))
9958 {
9959 char *tmp;
9960
9961 task = strtol (value_start, &tmp, 0);
9962 if (tmp == value_start)
9963 error (_("Junk after task keyword."));
9964 if (!valid_task_id (task))
9965 error (_("Unknown task %d."), task);
9966 }
9967 else if (toklen == 4 && startswith (tok, "mask"))
9968 {
9969 /* We've found a "mask" token, which means the user wants to
9970 create a hardware watchpoint that is going to have the mask
9971 facility. */
9972 struct value *mask_value, *mark;
9973
9974 if (use_mask)
9975 error(_("You can specify only one mask."));
9976
9977 use_mask = just_location = true;
9978
9979 mark = value_mark ();
9980 mask_value = parse_to_comma_and_eval (&value_start);
9981 mask = value_as_address (mask_value);
9982 value_free_to_mark (mark);
9983 }
9984 else
9985 /* We didn't recognize what we found. We should stop here. */
9986 break;
9987
9988 /* Truncate the string and get rid of the "parameter value" pair before
9989 the arguments string is parsed by the parse_exp_1 function. */
9990 exp_end = tok;
9991 }
9992 }
9993 else
9994 exp_end = arg;
9995
9996 /* Parse the rest of the arguments. From here on out, everything
9997 is in terms of a newly allocated string instead of the original
9998 ARG. */
9999 std::string expression (arg, exp_end - arg);
10000 exp_start = arg = expression.c_str ();
10001 innermost_block_tracker tracker;
10002 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10003 exp_end = arg;
10004 /* Remove trailing whitespace from the expression before saving it.
10005 This makes the eventual display of the expression string a bit
10006 prettier. */
10007 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10008 --exp_end;
10009
10010 /* Checking if the expression is not constant. */
10011 if (watchpoint_exp_is_const (exp.get ()))
10012 {
10013 int len;
10014
10015 len = exp_end - exp_start;
10016 while (len > 0 && isspace (exp_start[len - 1]))
10017 len--;
10018 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10019 }
10020
10021 exp_valid_block = tracker.block ();
10022 struct value *mark = value_mark ();
10023 struct value *val_as_value = nullptr;
10024 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10025 just_location);
10026
10027 if (val_as_value != NULL && just_location)
10028 {
10029 saved_bitpos = value_bitpos (val_as_value);
10030 saved_bitsize = value_bitsize (val_as_value);
10031 }
10032
10033 value_ref_ptr val;
10034 if (just_location)
10035 {
10036 int ret;
10037
10038 exp_valid_block = NULL;
10039 val = release_value (value_addr (result));
10040 value_free_to_mark (mark);
10041
10042 if (use_mask)
10043 {
10044 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10045 mask);
10046 if (ret == -1)
10047 error (_("This target does not support masked watchpoints."));
10048 else if (ret == -2)
10049 error (_("Invalid mask or memory region."));
10050 }
10051 }
10052 else if (val_as_value != NULL)
10053 val = release_value (val_as_value);
10054
10055 tok = skip_spaces (arg);
10056 end_tok = skip_to_space (tok);
10057
10058 toklen = end_tok - tok;
10059 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10060 {
10061 tok = cond_start = end_tok + 1;
10062 innermost_block_tracker if_tracker;
10063 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10064
10065 /* The watchpoint expression may not be local, but the condition
10066 may still be. E.g.: `watch global if local > 0'. */
10067 cond_exp_valid_block = if_tracker.block ();
10068
10069 cond_end = tok;
10070 }
10071 if (*tok)
10072 error (_("Junk at end of command."));
10073
10074 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10075
10076 /* Save this because create_internal_breakpoint below invalidates
10077 'wp_frame'. */
10078 frame_id watchpoint_frame = get_frame_id (wp_frame);
10079
10080 /* If the expression is "local", then set up a "watchpoint scope"
10081 breakpoint at the point where we've left the scope of the watchpoint
10082 expression. Create the scope breakpoint before the watchpoint, so
10083 that we will encounter it first in bpstat_stop_status. */
10084 if (exp_valid_block != NULL && wp_frame != NULL)
10085 {
10086 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10087
10088 if (frame_id_p (caller_frame_id))
10089 {
10090 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10091 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10092
10093 scope_breakpoint
10094 = create_internal_breakpoint (caller_arch, caller_pc,
10095 bp_watchpoint_scope,
10096 &momentary_breakpoint_ops);
10097
10098 /* create_internal_breakpoint could invalidate WP_FRAME. */
10099 wp_frame = NULL;
10100
10101 scope_breakpoint->enable_state = bp_enabled;
10102
10103 /* Automatically delete the breakpoint when it hits. */
10104 scope_breakpoint->disposition = disp_del;
10105
10106 /* Only break in the proper frame (help with recursion). */
10107 scope_breakpoint->frame_id = caller_frame_id;
10108
10109 /* Set the address at which we will stop. */
10110 scope_breakpoint->loc->gdbarch = caller_arch;
10111 scope_breakpoint->loc->requested_address = caller_pc;
10112 scope_breakpoint->loc->address
10113 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10114 scope_breakpoint->loc->requested_address,
10115 scope_breakpoint->type);
10116 }
10117 }
10118
10119 /* Now set up the breakpoint. We create all watchpoints as hardware
10120 watchpoints here even if hardware watchpoints are turned off, a call
10121 to update_watchpoint later in this function will cause the type to
10122 drop back to bp_watchpoint (software watchpoint) if required. */
10123
10124 if (accessflag == hw_read)
10125 bp_type = bp_read_watchpoint;
10126 else if (accessflag == hw_access)
10127 bp_type = bp_access_watchpoint;
10128 else
10129 bp_type = bp_hardware_watchpoint;
10130
10131 std::unique_ptr<watchpoint> w (new watchpoint ());
10132
10133 if (use_mask)
10134 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10135 &masked_watchpoint_breakpoint_ops);
10136 else
10137 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10138 &watchpoint_breakpoint_ops);
10139 w->thread = thread;
10140 w->task = task;
10141 w->disposition = disp_donttouch;
10142 w->pspace = current_program_space;
10143 w->exp = std::move (exp);
10144 w->exp_valid_block = exp_valid_block;
10145 w->cond_exp_valid_block = cond_exp_valid_block;
10146 if (just_location)
10147 {
10148 struct type *t = value_type (val.get ());
10149 CORE_ADDR addr = value_as_address (val.get ());
10150
10151 w->exp_string_reparse
10152 = current_language->watch_location_expression (t, addr);
10153
10154 w->exp_string = xstrprintf ("-location %.*s",
10155 (int) (exp_end - exp_start), exp_start);
10156 }
10157 else
10158 w->exp_string.reset (savestring (exp_start, exp_end - exp_start));
10159
10160 if (use_mask)
10161 {
10162 w->hw_wp_mask = mask;
10163 }
10164 else
10165 {
10166 w->val = val;
10167 w->val_bitpos = saved_bitpos;
10168 w->val_bitsize = saved_bitsize;
10169 w->val_valid = true;
10170 }
10171
10172 if (cond_start)
10173 w->cond_string.reset (savestring (cond_start, cond_end - cond_start));
10174 else
10175 w->cond_string = 0;
10176
10177 if (frame_id_p (watchpoint_frame))
10178 {
10179 w->watchpoint_frame = watchpoint_frame;
10180 w->watchpoint_thread = inferior_ptid;
10181 }
10182 else
10183 {
10184 w->watchpoint_frame = null_frame_id;
10185 w->watchpoint_thread = null_ptid;
10186 }
10187
10188 if (scope_breakpoint != NULL)
10189 {
10190 /* The scope breakpoint is related to the watchpoint. We will
10191 need to act on them together. */
10192 w->related_breakpoint = scope_breakpoint;
10193 scope_breakpoint->related_breakpoint = w.get ();
10194 }
10195
10196 if (!just_location)
10197 value_free_to_mark (mark);
10198
10199 /* Finally update the new watchpoint. This creates the locations
10200 that should be inserted. */
10201 update_watchpoint (w.get (), 1);
10202
10203 install_breakpoint (internal, std::move (w), 1);
10204 }
10205
10206 /* Return count of debug registers needed to watch the given expression.
10207 If the watchpoint cannot be handled in hardware return zero. */
10208
10209 static int
10210 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10211 {
10212 int found_memory_cnt = 0;
10213
10214 /* Did the user specifically forbid us to use hardware watchpoints? */
10215 if (!can_use_hw_watchpoints)
10216 return 0;
10217
10218 gdb_assert (!vals.empty ());
10219 struct value *head = vals[0].get ();
10220
10221 /* Make sure that the value of the expression depends only upon
10222 memory contents, and values computed from them within GDB. If we
10223 find any register references or function calls, we can't use a
10224 hardware watchpoint.
10225
10226 The idea here is that evaluating an expression generates a series
10227 of values, one holding the value of every subexpression. (The
10228 expression a*b+c has five subexpressions: a, b, a*b, c, and
10229 a*b+c.) GDB's values hold almost enough information to establish
10230 the criteria given above --- they identify memory lvalues,
10231 register lvalues, computed values, etcetera. So we can evaluate
10232 the expression, and then scan the chain of values that leaves
10233 behind to decide whether we can detect any possible change to the
10234 expression's final value using only hardware watchpoints.
10235
10236 However, I don't think that the values returned by inferior
10237 function calls are special in any way. So this function may not
10238 notice that an expression involving an inferior function call
10239 can't be watched with hardware watchpoints. FIXME. */
10240 for (const value_ref_ptr &iter : vals)
10241 {
10242 struct value *v = iter.get ();
10243
10244 if (VALUE_LVAL (v) == lval_memory)
10245 {
10246 if (v != head && value_lazy (v))
10247 /* A lazy memory lvalue in the chain is one that GDB never
10248 needed to fetch; we either just used its address (e.g.,
10249 `a' in `a.b') or we never needed it at all (e.g., `a'
10250 in `a,b'). This doesn't apply to HEAD; if that is
10251 lazy then it was not readable, but watch it anyway. */
10252 ;
10253 else
10254 {
10255 /* Ahh, memory we actually used! Check if we can cover
10256 it with hardware watchpoints. */
10257 struct type *vtype = check_typedef (value_type (v));
10258
10259 /* We only watch structs and arrays if user asked for it
10260 explicitly, never if they just happen to appear in a
10261 middle of some value chain. */
10262 if (v == head
10263 || (vtype->code () != TYPE_CODE_STRUCT
10264 && vtype->code () != TYPE_CODE_ARRAY))
10265 {
10266 CORE_ADDR vaddr = value_address (v);
10267 int len;
10268 int num_regs;
10269
10270 len = (target_exact_watchpoints
10271 && is_scalar_type_recursive (vtype))?
10272 1 : TYPE_LENGTH (value_type (v));
10273
10274 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10275 if (!num_regs)
10276 return 0;
10277 else
10278 found_memory_cnt += num_regs;
10279 }
10280 }
10281 }
10282 else if (VALUE_LVAL (v) != not_lval
10283 && deprecated_value_modifiable (v) == 0)
10284 return 0; /* These are values from the history (e.g., $1). */
10285 else if (VALUE_LVAL (v) == lval_register)
10286 return 0; /* Cannot watch a register with a HW watchpoint. */
10287 }
10288
10289 /* The expression itself looks suitable for using a hardware
10290 watchpoint, but give the target machine a chance to reject it. */
10291 return found_memory_cnt;
10292 }
10293
10294 void
10295 watch_command_wrapper (const char *arg, int from_tty, bool internal)
10296 {
10297 watch_command_1 (arg, hw_write, from_tty, 0, internal);
10298 }
10299
10300 /* Options for the watch, awatch, and rwatch commands. */
10301
10302 struct watch_options
10303 {
10304 /* For -location. */
10305 bool location = false;
10306 };
10307
10308 /* Definitions of options for the "watch", "awatch", and "rwatch" commands.
10309
10310 Historically GDB always accepted both '-location' and '-l' flags for
10311 these commands (both flags being synonyms). When converting to the
10312 newer option scheme only '-location' is added here. That's fine (for
10313 backward compatibility) as any non-ambiguous prefix of a flag will be
10314 accepted, so '-l', '-loc', are now all accepted.
10315
10316 What this means is that, if in the future, we add any new flag here
10317 that starts with '-l' then this will break backward compatibility, so
10318 please, don't do that! */
10319
10320 static const gdb::option::option_def watch_option_defs[] = {
10321 gdb::option::flag_option_def<watch_options> {
10322 "location",
10323 [] (watch_options *opt) { return &opt->location; },
10324 N_("\
10325 This evaluates EXPRESSION and watches the memory to which is refers.\n\
10326 -l can be used as a short form of -location."),
10327 },
10328 };
10329
10330 /* Returns the option group used by 'watch', 'awatch', and 'rwatch'
10331 commands. */
10332
10333 static gdb::option::option_def_group
10334 make_watch_options_def_group (watch_options *opts)
10335 {
10336 return {{watch_option_defs}, opts};
10337 }
10338
10339 /* A helper function that looks for the "-location" argument and then
10340 calls watch_command_1. */
10341
10342 static void
10343 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
10344 {
10345 watch_options opts;
10346 auto grp = make_watch_options_def_group (&opts);
10347 gdb::option::process_options
10348 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
10349 if (arg != nullptr && *arg == '\0')
10350 arg = nullptr;
10351
10352 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
10353 }
10354
10355 /* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
10356 static void
10357 watch_command_completer (struct cmd_list_element *ignore,
10358 completion_tracker &tracker,
10359 const char *text, const char * /*word*/)
10360 {
10361 const auto group = make_watch_options_def_group (nullptr);
10362 if (gdb::option::complete_options
10363 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
10364 return;
10365
10366 const char *word = advance_to_expression_complete_word_point (tracker, text);
10367 expression_completer (ignore, tracker, text, word);
10368 }
10369
10370 static void
10371 watch_command (const char *arg, int from_tty)
10372 {
10373 watch_maybe_just_location (arg, hw_write, from_tty);
10374 }
10375
10376 void
10377 rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
10378 {
10379 watch_command_1 (arg, hw_read, from_tty, 0, internal);
10380 }
10381
10382 static void
10383 rwatch_command (const char *arg, int from_tty)
10384 {
10385 watch_maybe_just_location (arg, hw_read, from_tty);
10386 }
10387
10388 void
10389 awatch_command_wrapper (const char *arg, int from_tty, bool internal)
10390 {
10391 watch_command_1 (arg, hw_access, from_tty, 0, internal);
10392 }
10393
10394 static void
10395 awatch_command (const char *arg, int from_tty)
10396 {
10397 watch_maybe_just_location (arg, hw_access, from_tty);
10398 }
10399 \f
10400
10401 /* Data for the FSM that manages the until(location)/advance commands
10402 in infcmd.c. Here because it uses the mechanisms of
10403 breakpoints. */
10404
10405 struct until_break_fsm : public thread_fsm
10406 {
10407 /* The thread that was current when the command was executed. */
10408 int thread;
10409
10410 /* The breakpoint set at the return address in the caller frame,
10411 plus breakpoints at all the destination locations. */
10412 std::vector<breakpoint_up> breakpoints;
10413
10414 until_break_fsm (struct interp *cmd_interp, int thread,
10415 std::vector<breakpoint_up> &&breakpoints)
10416 : thread_fsm (cmd_interp),
10417 thread (thread),
10418 breakpoints (std::move (breakpoints))
10419 {
10420 }
10421
10422 void clean_up (struct thread_info *thread) override;
10423 bool should_stop (struct thread_info *thread) override;
10424 enum async_reply_reason do_async_reply_reason () override;
10425 };
10426
10427 /* Implementation of the 'should_stop' FSM method for the
10428 until(location)/advance commands. */
10429
10430 bool
10431 until_break_fsm::should_stop (struct thread_info *tp)
10432 {
10433 for (const breakpoint_up &bp : breakpoints)
10434 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
10435 bp.get ()) != NULL)
10436 {
10437 set_finished ();
10438 break;
10439 }
10440
10441 return true;
10442 }
10443
10444 /* Implementation of the 'clean_up' FSM method for the
10445 until(location)/advance commands. */
10446
10447 void
10448 until_break_fsm::clean_up (struct thread_info *)
10449 {
10450 /* Clean up our temporary breakpoints. */
10451 breakpoints.clear ();
10452 delete_longjmp_breakpoint (thread);
10453 }
10454
10455 /* Implementation of the 'async_reply_reason' FSM method for the
10456 until(location)/advance commands. */
10457
10458 enum async_reply_reason
10459 until_break_fsm::do_async_reply_reason ()
10460 {
10461 return EXEC_ASYNC_LOCATION_REACHED;
10462 }
10463
10464 void
10465 until_break_command (const char *arg, int from_tty, int anywhere)
10466 {
10467 struct frame_info *frame;
10468 struct gdbarch *frame_gdbarch;
10469 struct frame_id stack_frame_id;
10470 struct frame_id caller_frame_id;
10471 int thread;
10472 struct thread_info *tp;
10473
10474 clear_proceed_status (0);
10475
10476 /* Set a breakpoint where the user wants it and at return from
10477 this function. */
10478
10479 event_location_up location = string_to_event_location (&arg, current_language);
10480
10481 std::vector<symtab_and_line> sals
10482 = (last_displayed_sal_is_valid ()
10483 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10484 get_last_displayed_symtab (),
10485 get_last_displayed_line ())
10486 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
10487 NULL, NULL, 0));
10488
10489 if (sals.empty ())
10490 error (_("Couldn't get information on specified line."));
10491
10492 if (*arg)
10493 error (_("Junk at end of arguments."));
10494
10495 tp = inferior_thread ();
10496 thread = tp->global_num;
10497
10498 /* Note linespec handling above invalidates the frame chain.
10499 Installing a breakpoint also invalidates the frame chain (as it
10500 may need to switch threads), so do any frame handling before
10501 that. */
10502
10503 frame = get_selected_frame (NULL);
10504 frame_gdbarch = get_frame_arch (frame);
10505 stack_frame_id = get_stack_frame_id (frame);
10506 caller_frame_id = frame_unwind_caller_id (frame);
10507
10508 /* Keep within the current frame, or in frames called by the current
10509 one. */
10510
10511 std::vector<breakpoint_up> breakpoints;
10512
10513 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
10514
10515 if (frame_id_p (caller_frame_id))
10516 {
10517 struct symtab_and_line sal2;
10518 struct gdbarch *caller_gdbarch;
10519
10520 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
10521 sal2.pc = frame_unwind_caller_pc (frame);
10522 caller_gdbarch = frame_unwind_caller_arch (frame);
10523
10524 breakpoint_up caller_breakpoint
10525 = set_momentary_breakpoint (caller_gdbarch, sal2,
10526 caller_frame_id, bp_until);
10527 breakpoints.emplace_back (std::move (caller_breakpoint));
10528
10529 set_longjmp_breakpoint (tp, caller_frame_id);
10530 lj_deleter.emplace (thread);
10531 }
10532
10533 /* set_momentary_breakpoint could invalidate FRAME. */
10534 frame = NULL;
10535
10536 /* If the user told us to continue until a specified location, we
10537 don't specify a frame at which we need to stop. Otherwise,
10538 specify the selected frame, because we want to stop only at the
10539 very same frame. */
10540 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
10541
10542 for (symtab_and_line &sal : sals)
10543 {
10544 resolve_sal_pc (&sal);
10545
10546 breakpoint_up location_breakpoint
10547 = set_momentary_breakpoint (frame_gdbarch, sal,
10548 stop_frame_id, bp_until);
10549 breakpoints.emplace_back (std::move (location_breakpoint));
10550 }
10551
10552 tp->set_thread_fsm
10553 (std::unique_ptr<thread_fsm>
10554 (new until_break_fsm (command_interp (), tp->global_num,
10555 std::move (breakpoints))));
10556
10557 if (lj_deleter)
10558 lj_deleter->release ();
10559
10560 proceed (-1, GDB_SIGNAL_DEFAULT);
10561 }
10562
10563 void
10564 init_ada_exception_breakpoint (struct breakpoint *b,
10565 struct gdbarch *gdbarch,
10566 struct symtab_and_line sal,
10567 const char *addr_string,
10568 const struct breakpoint_ops *ops,
10569 int tempflag,
10570 int enabled,
10571 int from_tty)
10572 {
10573 if (from_tty)
10574 {
10575 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
10576 if (!loc_gdbarch)
10577 loc_gdbarch = gdbarch;
10578
10579 describe_other_breakpoints (loc_gdbarch,
10580 sal.pspace, sal.pc, sal.section, -1);
10581 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
10582 version for exception catchpoints, because two catchpoints
10583 used for different exception names will use the same address.
10584 In this case, a "breakpoint ... also set at..." warning is
10585 unproductive. Besides, the warning phrasing is also a bit
10586 inappropriate, we should use the word catchpoint, and tell
10587 the user what type of catchpoint it is. The above is good
10588 enough for now, though. */
10589 }
10590
10591 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
10592
10593 b->enable_state = enabled ? bp_enabled : bp_disabled;
10594 b->disposition = tempflag ? disp_del : disp_donttouch;
10595 b->location = string_to_event_location (&addr_string,
10596 language_def (language_ada));
10597 b->language = language_ada;
10598 }
10599
10600 \f
10601
10602 /* Compare two breakpoints and return a strcmp-like result. */
10603
10604 static int
10605 compare_breakpoints (const breakpoint *a, const breakpoint *b)
10606 {
10607 uintptr_t ua = (uintptr_t) a;
10608 uintptr_t ub = (uintptr_t) b;
10609
10610 if (a->number < b->number)
10611 return -1;
10612 else if (a->number > b->number)
10613 return 1;
10614
10615 /* Now sort by address, in case we see, e..g, two breakpoints with
10616 the number 0. */
10617 if (ua < ub)
10618 return -1;
10619 return ua > ub ? 1 : 0;
10620 }
10621
10622 /* Delete breakpoints by address or line. */
10623
10624 static void
10625 clear_command (const char *arg, int from_tty)
10626 {
10627 int default_match;
10628
10629 std::vector<symtab_and_line> decoded_sals;
10630 symtab_and_line last_sal;
10631 gdb::array_view<symtab_and_line> sals;
10632 if (arg)
10633 {
10634 decoded_sals
10635 = decode_line_with_current_source (arg,
10636 (DECODE_LINE_FUNFIRSTLINE
10637 | DECODE_LINE_LIST_MODE));
10638 default_match = 0;
10639 sals = decoded_sals;
10640 }
10641 else
10642 {
10643 /* Set sal's line, symtab, pc, and pspace to the values
10644 corresponding to the last call to print_frame_info. If the
10645 codepoint is not valid, this will set all the fields to 0. */
10646 last_sal = get_last_displayed_sal ();
10647 if (last_sal.symtab == 0)
10648 error (_("No source file specified."));
10649
10650 default_match = 1;
10651 sals = last_sal;
10652 }
10653
10654 /* We don't call resolve_sal_pc here. That's not as bad as it
10655 seems, because all existing breakpoints typically have both
10656 file/line and pc set. So, if clear is given file/line, we can
10657 match this to existing breakpoint without obtaining pc at all.
10658
10659 We only support clearing given the address explicitly
10660 present in breakpoint table. Say, we've set breakpoint
10661 at file:line. There were several PC values for that file:line,
10662 due to optimization, all in one block.
10663
10664 We've picked one PC value. If "clear" is issued with another
10665 PC corresponding to the same file:line, the breakpoint won't
10666 be cleared. We probably can still clear the breakpoint, but
10667 since the other PC value is never presented to user, user
10668 can only find it by guessing, and it does not seem important
10669 to support that. */
10670
10671 /* For each line spec given, delete bps which correspond to it. Do
10672 it in two passes, solely to preserve the current behavior that
10673 from_tty is forced true if we delete more than one
10674 breakpoint. */
10675
10676 std::vector<struct breakpoint *> found;
10677 for (const auto &sal : sals)
10678 {
10679 const char *sal_fullname;
10680
10681 /* If exact pc given, clear bpts at that pc.
10682 If line given (pc == 0), clear all bpts on specified line.
10683 If defaulting, clear all bpts on default line
10684 or at default pc.
10685
10686 defaulting sal.pc != 0 tests to do
10687
10688 0 1 pc
10689 1 1 pc _and_ line
10690 0 0 line
10691 1 0 <can't happen> */
10692
10693 sal_fullname = (sal.symtab == NULL
10694 ? NULL : symtab_to_fullname (sal.symtab));
10695
10696 /* Find all matching breakpoints and add them to 'found'. */
10697 for (breakpoint *b : all_breakpoints ())
10698 {
10699 int match = 0;
10700 /* Are we going to delete b? */
10701 if (b->type != bp_none && !is_watchpoint (b)
10702 && user_breakpoint_p (b))
10703 {
10704 for (bp_location *loc : b->locations ())
10705 {
10706 /* If the user specified file:line, don't allow a PC
10707 match. This matches historical gdb behavior. */
10708 int pc_match = (!sal.explicit_line
10709 && sal.pc
10710 && (loc->pspace == sal.pspace)
10711 && (loc->address == sal.pc)
10712 && (!section_is_overlay (loc->section)
10713 || loc->section == sal.section));
10714 int line_match = 0;
10715
10716 if ((default_match || sal.explicit_line)
10717 && loc->symtab != NULL
10718 && sal_fullname != NULL
10719 && sal.pspace == loc->pspace
10720 && loc->line_number == sal.line
10721 && filename_cmp (symtab_to_fullname (loc->symtab),
10722 sal_fullname) == 0)
10723 line_match = 1;
10724
10725 if (pc_match || line_match)
10726 {
10727 match = 1;
10728 break;
10729 }
10730 }
10731 }
10732
10733 if (match)
10734 found.push_back (b);
10735 }
10736 }
10737
10738 /* Now go thru the 'found' chain and delete them. */
10739 if (found.empty ())
10740 {
10741 if (arg)
10742 error (_("No breakpoint at %s."), arg);
10743 else
10744 error (_("No breakpoint at this line."));
10745 }
10746
10747 /* Remove duplicates from the vec. */
10748 std::sort (found.begin (), found.end (),
10749 [] (const breakpoint *bp_a, const breakpoint *bp_b)
10750 {
10751 return compare_breakpoints (bp_a, bp_b) < 0;
10752 });
10753 found.erase (std::unique (found.begin (), found.end (),
10754 [] (const breakpoint *bp_a, const breakpoint *bp_b)
10755 {
10756 return compare_breakpoints (bp_a, bp_b) == 0;
10757 }),
10758 found.end ());
10759
10760 if (found.size () > 1)
10761 from_tty = 1; /* Always report if deleted more than one. */
10762 if (from_tty)
10763 {
10764 if (found.size () == 1)
10765 gdb_printf (_("Deleted breakpoint "));
10766 else
10767 gdb_printf (_("Deleted breakpoints "));
10768 }
10769
10770 for (breakpoint *iter : found)
10771 {
10772 if (from_tty)
10773 gdb_printf ("%d ", iter->number);
10774 delete_breakpoint (iter);
10775 }
10776 if (from_tty)
10777 gdb_putc ('\n');
10778 }
10779 \f
10780 /* Delete breakpoint in BS if they are `delete' breakpoints and
10781 all breakpoints that are marked for deletion, whether hit or not.
10782 This is called after any breakpoint is hit, or after errors. */
10783
10784 void
10785 breakpoint_auto_delete (bpstat *bs)
10786 {
10787 for (; bs; bs = bs->next)
10788 if (bs->breakpoint_at
10789 && bs->breakpoint_at->disposition == disp_del
10790 && bs->stop)
10791 delete_breakpoint (bs->breakpoint_at);
10792
10793 for (breakpoint *b : all_breakpoints_safe ())
10794 if (b->disposition == disp_del_at_next_stop)
10795 delete_breakpoint (b);
10796 }
10797
10798 /* A comparison function for bp_location AP and BP being interfaced to
10799 std::sort. Sort elements primarily by their ADDRESS (no matter what
10800 bl_address_is_meaningful says), secondarily by ordering first
10801 permanent elements and terciarily just ensuring the array is sorted
10802 stable way despite std::sort being an unstable algorithm. */
10803
10804 static int
10805 bp_location_is_less_than (const bp_location *a, const bp_location *b)
10806 {
10807 if (a->address != b->address)
10808 return a->address < b->address;
10809
10810 /* Sort locations at the same address by their pspace number, keeping
10811 locations of the same inferior (in a multi-inferior environment)
10812 grouped. */
10813
10814 if (a->pspace->num != b->pspace->num)
10815 return a->pspace->num < b->pspace->num;
10816
10817 /* Sort permanent breakpoints first. */
10818 if (a->permanent != b->permanent)
10819 return a->permanent > b->permanent;
10820
10821 /* Sort by type in order to make duplicate determination easier.
10822 See update_global_location_list. This is kept in sync with
10823 breakpoint_locations_match. */
10824 if (a->loc_type < b->loc_type)
10825 return true;
10826
10827 /* Likewise, for range-breakpoints, sort by length. */
10828 if (a->loc_type == bp_loc_hardware_breakpoint
10829 && b->loc_type == bp_loc_hardware_breakpoint
10830 && a->length < b->length)
10831 return true;
10832
10833 /* Make the internal GDB representation stable across GDB runs
10834 where A and B memory inside GDB can differ. Breakpoint locations of
10835 the same type at the same address can be sorted in arbitrary order. */
10836
10837 if (a->owner->number != b->owner->number)
10838 return a->owner->number < b->owner->number;
10839
10840 return a < b;
10841 }
10842
10843 /* Set bp_locations_placed_address_before_address_max and
10844 bp_locations_shadow_len_after_address_max according to the current
10845 content of the bp_locations array. */
10846
10847 static void
10848 bp_locations_target_extensions_update (void)
10849 {
10850 bp_locations_placed_address_before_address_max = 0;
10851 bp_locations_shadow_len_after_address_max = 0;
10852
10853 for (bp_location *bl : all_bp_locations ())
10854 {
10855 CORE_ADDR start, end, addr;
10856
10857 if (!bp_location_has_shadow (bl))
10858 continue;
10859
10860 start = bl->target_info.placed_address;
10861 end = start + bl->target_info.shadow_len;
10862
10863 gdb_assert (bl->address >= start);
10864 addr = bl->address - start;
10865 if (addr > bp_locations_placed_address_before_address_max)
10866 bp_locations_placed_address_before_address_max = addr;
10867
10868 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
10869
10870 gdb_assert (bl->address < end);
10871 addr = end - bl->address;
10872 if (addr > bp_locations_shadow_len_after_address_max)
10873 bp_locations_shadow_len_after_address_max = addr;
10874 }
10875 }
10876
10877 /* Download tracepoint locations if they haven't been. */
10878
10879 static void
10880 download_tracepoint_locations (void)
10881 {
10882 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
10883
10884 scoped_restore_current_pspace_and_thread restore_pspace_thread;
10885
10886 for (breakpoint *b : all_tracepoints ())
10887 {
10888 struct tracepoint *t;
10889 int bp_location_downloaded = 0;
10890
10891 if ((b->type == bp_fast_tracepoint
10892 ? !may_insert_fast_tracepoints
10893 : !may_insert_tracepoints))
10894 continue;
10895
10896 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
10897 {
10898 if (target_can_download_tracepoint ())
10899 can_download_tracepoint = TRIBOOL_TRUE;
10900 else
10901 can_download_tracepoint = TRIBOOL_FALSE;
10902 }
10903
10904 if (can_download_tracepoint == TRIBOOL_FALSE)
10905 break;
10906
10907 for (bp_location *bl : b->locations ())
10908 {
10909 /* In tracepoint, locations are _never_ duplicated, so
10910 should_be_inserted is equivalent to
10911 unduplicated_should_be_inserted. */
10912 if (!should_be_inserted (bl) || bl->inserted)
10913 continue;
10914
10915 switch_to_program_space_and_thread (bl->pspace);
10916
10917 target_download_tracepoint (bl);
10918
10919 bl->inserted = 1;
10920 bp_location_downloaded = 1;
10921 }
10922 t = (struct tracepoint *) b;
10923 t->number_on_target = b->number;
10924 if (bp_location_downloaded)
10925 gdb::observers::breakpoint_modified.notify (b);
10926 }
10927 }
10928
10929 /* Swap the insertion/duplication state between two locations. */
10930
10931 static void
10932 swap_insertion (struct bp_location *left, struct bp_location *right)
10933 {
10934 const int left_inserted = left->inserted;
10935 const int left_duplicate = left->duplicate;
10936 const int left_needs_update = left->needs_update;
10937 const struct bp_target_info left_target_info = left->target_info;
10938
10939 /* Locations of tracepoints can never be duplicated. */
10940 if (is_tracepoint (left->owner))
10941 gdb_assert (!left->duplicate);
10942 if (is_tracepoint (right->owner))
10943 gdb_assert (!right->duplicate);
10944
10945 left->inserted = right->inserted;
10946 left->duplicate = right->duplicate;
10947 left->needs_update = right->needs_update;
10948 left->target_info = right->target_info;
10949 right->inserted = left_inserted;
10950 right->duplicate = left_duplicate;
10951 right->needs_update = left_needs_update;
10952 right->target_info = left_target_info;
10953 }
10954
10955 /* Force the re-insertion of the locations at ADDRESS. This is called
10956 once a new/deleted/modified duplicate location is found and we are evaluating
10957 conditions on the target's side. Such conditions need to be updated on
10958 the target. */
10959
10960 static void
10961 force_breakpoint_reinsertion (struct bp_location *bl)
10962 {
10963 CORE_ADDR address = 0;
10964 int pspace_num;
10965
10966 address = bl->address;
10967 pspace_num = bl->pspace->num;
10968
10969 /* This is only meaningful if the target is
10970 evaluating conditions and if the user has
10971 opted for condition evaluation on the target's
10972 side. */
10973 if (gdb_evaluates_breakpoint_condition_p ()
10974 || !target_supports_evaluation_of_breakpoint_conditions ())
10975 return;
10976
10977 /* Flag all breakpoint locations with this address and
10978 the same program space as the location
10979 as "its condition has changed". We need to
10980 update the conditions on the target's side. */
10981 for (bp_location *loc : all_bp_locations_at_addr (address))
10982 {
10983 if (!is_breakpoint (loc->owner)
10984 || pspace_num != loc->pspace->num)
10985 continue;
10986
10987 /* Flag the location appropriately. We use a different state to
10988 let everyone know that we already updated the set of locations
10989 with addr bl->address and program space bl->pspace. This is so
10990 we don't have to keep calling these functions just to mark locations
10991 that have already been marked. */
10992 loc->condition_changed = condition_updated;
10993
10994 /* Free the agent expression bytecode as well. We will compute
10995 it later on. */
10996 loc->cond_bytecode.reset ();
10997 }
10998 }
10999
11000 /* Called whether new breakpoints are created, or existing breakpoints
11001 deleted, to update the global location list and recompute which
11002 locations are duplicate of which.
11003
11004 The INSERT_MODE flag determines whether locations may not, may, or
11005 shall be inserted now. See 'enum ugll_insert_mode' for more
11006 info. */
11007
11008 static void
11009 update_global_location_list (enum ugll_insert_mode insert_mode)
11010 {
11011 /* Last breakpoint location address that was marked for update. */
11012 CORE_ADDR last_addr = 0;
11013 /* Last breakpoint location program space that was marked for update. */
11014 int last_pspace_num = -1;
11015
11016 /* Used in the duplicates detection below. When iterating over all
11017 bp_locations, points to the first bp_location of a given address.
11018 Breakpoints and watchpoints of different types are never
11019 duplicates of each other. Keep one pointer for each type of
11020 breakpoint/watchpoint, so we only need to loop over all locations
11021 once. */
11022 struct bp_location *bp_loc_first; /* breakpoint */
11023 struct bp_location *wp_loc_first; /* hardware watchpoint */
11024 struct bp_location *awp_loc_first; /* access watchpoint */
11025 struct bp_location *rwp_loc_first; /* read watchpoint */
11026
11027 /* Saved former bp_locations array which we compare against the newly
11028 built bp_locations from the current state of ALL_BREAKPOINTS. */
11029 std::vector<bp_location *> old_locations = std::move (bp_locations);
11030 bp_locations.clear ();
11031
11032 for (breakpoint *b : all_breakpoints ())
11033 for (bp_location *loc : b->locations ())
11034 bp_locations.push_back (loc);
11035
11036 /* See if we need to "upgrade" a software breakpoint to a hardware
11037 breakpoint. Do this before deciding whether locations are
11038 duplicates. Also do this before sorting because sorting order
11039 depends on location type. */
11040 for (bp_location *loc : bp_locations)
11041 if (!loc->inserted && should_be_inserted (loc))
11042 handle_automatic_hardware_breakpoints (loc);
11043
11044 std::sort (bp_locations.begin (), bp_locations.end (),
11045 bp_location_is_less_than);
11046
11047 bp_locations_target_extensions_update ();
11048
11049 /* Identify bp_location instances that are no longer present in the
11050 new list, and therefore should be freed. Note that it's not
11051 necessary that those locations should be removed from inferior --
11052 if there's another location at the same address (previously
11053 marked as duplicate), we don't need to remove/insert the
11054 location.
11055
11056 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11057 and former bp_location array state respectively. */
11058
11059 size_t loc_i = 0;
11060 for (bp_location *old_loc : old_locations)
11061 {
11062 /* Tells if 'old_loc' is found among the new locations. If
11063 not, we have to free it. */
11064 int found_object = 0;
11065 /* Tells if the location should remain inserted in the target. */
11066 int keep_in_target = 0;
11067 int removed = 0;
11068
11069 /* Skip LOCP entries which will definitely never be needed.
11070 Stop either at or being the one matching OLD_LOC. */
11071 while (loc_i < bp_locations.size ()
11072 && bp_locations[loc_i]->address < old_loc->address)
11073 loc_i++;
11074
11075 for (size_t loc2_i = loc_i;
11076 (loc2_i < bp_locations.size ()
11077 && bp_locations[loc2_i]->address == old_loc->address);
11078 loc2_i++)
11079 {
11080 /* Check if this is a new/duplicated location or a duplicated
11081 location that had its condition modified. If so, we want to send
11082 its condition to the target if evaluation of conditions is taking
11083 place there. */
11084 if (bp_locations[loc2_i]->condition_changed == condition_modified
11085 && (last_addr != old_loc->address
11086 || last_pspace_num != old_loc->pspace->num))
11087 {
11088 force_breakpoint_reinsertion (bp_locations[loc2_i]);
11089 last_pspace_num = old_loc->pspace->num;
11090 }
11091
11092 if (bp_locations[loc2_i] == old_loc)
11093 found_object = 1;
11094 }
11095
11096 /* We have already handled this address, update it so that we don't
11097 have to go through updates again. */
11098 last_addr = old_loc->address;
11099
11100 /* Target-side condition evaluation: Handle deleted locations. */
11101 if (!found_object)
11102 force_breakpoint_reinsertion (old_loc);
11103
11104 /* If this location is no longer present, and inserted, look if
11105 there's maybe a new location at the same address. If so,
11106 mark that one inserted, and don't remove this one. This is
11107 needed so that we don't have a time window where a breakpoint
11108 at certain location is not inserted. */
11109
11110 if (old_loc->inserted)
11111 {
11112 /* If the location is inserted now, we might have to remove
11113 it. */
11114
11115 if (found_object && should_be_inserted (old_loc))
11116 {
11117 /* The location is still present in the location list,
11118 and still should be inserted. Don't do anything. */
11119 keep_in_target = 1;
11120 }
11121 else
11122 {
11123 /* This location still exists, but it won't be kept in the
11124 target since it may have been disabled. We proceed to
11125 remove its target-side condition. */
11126
11127 /* The location is either no longer present, or got
11128 disabled. See if there's another location at the
11129 same address, in which case we don't need to remove
11130 this one from the target. */
11131
11132 /* OLD_LOC comes from existing struct breakpoint. */
11133 if (bl_address_is_meaningful (old_loc))
11134 {
11135 for (size_t loc2_i = loc_i;
11136 (loc2_i < bp_locations.size ()
11137 && bp_locations[loc2_i]->address == old_loc->address);
11138 loc2_i++)
11139 {
11140 bp_location *loc2 = bp_locations[loc2_i];
11141
11142 if (loc2 == old_loc)
11143 continue;
11144
11145 if (breakpoint_locations_match (loc2, old_loc))
11146 {
11147 /* Read watchpoint locations are switched to
11148 access watchpoints, if the former are not
11149 supported, but the latter are. */
11150 if (is_hardware_watchpoint (old_loc->owner))
11151 {
11152 gdb_assert (is_hardware_watchpoint (loc2->owner));
11153 loc2->watchpoint_type = old_loc->watchpoint_type;
11154 }
11155
11156 /* loc2 is a duplicated location. We need to check
11157 if it should be inserted in case it will be
11158 unduplicated. */
11159 if (unduplicated_should_be_inserted (loc2))
11160 {
11161 swap_insertion (old_loc, loc2);
11162 keep_in_target = 1;
11163 break;
11164 }
11165 }
11166 }
11167 }
11168 }
11169
11170 if (!keep_in_target)
11171 {
11172 if (remove_breakpoint (old_loc))
11173 {
11174 /* This is just about all we can do. We could keep
11175 this location on the global list, and try to
11176 remove it next time, but there's no particular
11177 reason why we will succeed next time.
11178
11179 Note that at this point, old_loc->owner is still
11180 valid, as delete_breakpoint frees the breakpoint
11181 only after calling us. */
11182 gdb_printf (_("warning: Error removing "
11183 "breakpoint %d\n"),
11184 old_loc->owner->number);
11185 }
11186 removed = 1;
11187 }
11188 }
11189
11190 if (!found_object)
11191 {
11192 if (removed && target_is_non_stop_p ()
11193 && need_moribund_for_location_type (old_loc))
11194 {
11195 /* This location was removed from the target. In
11196 non-stop mode, a race condition is possible where
11197 we've removed a breakpoint, but stop events for that
11198 breakpoint are already queued and will arrive later.
11199 We apply an heuristic to be able to distinguish such
11200 SIGTRAPs from other random SIGTRAPs: we keep this
11201 breakpoint location for a bit, and will retire it
11202 after we see some number of events. The theory here
11203 is that reporting of events should, "on the average",
11204 be fair, so after a while we'll see events from all
11205 threads that have anything of interest, and no longer
11206 need to keep this breakpoint location around. We
11207 don't hold locations forever so to reduce chances of
11208 mistaking a non-breakpoint SIGTRAP for a breakpoint
11209 SIGTRAP.
11210
11211 The heuristic failing can be disastrous on
11212 decr_pc_after_break targets.
11213
11214 On decr_pc_after_break targets, like e.g., x86-linux,
11215 if we fail to recognize a late breakpoint SIGTRAP,
11216 because events_till_retirement has reached 0 too
11217 soon, we'll fail to do the PC adjustment, and report
11218 a random SIGTRAP to the user. When the user resumes
11219 the inferior, it will most likely immediately crash
11220 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
11221 corrupted, because of being resumed e.g., in the
11222 middle of a multi-byte instruction, or skipped a
11223 one-byte instruction. This was actually seen happen
11224 on native x86-linux, and should be less rare on
11225 targets that do not support new thread events, like
11226 remote, due to the heuristic depending on
11227 thread_count.
11228
11229 Mistaking a random SIGTRAP for a breakpoint trap
11230 causes similar symptoms (PC adjustment applied when
11231 it shouldn't), but then again, playing with SIGTRAPs
11232 behind the debugger's back is asking for trouble.
11233
11234 Since hardware watchpoint traps are always
11235 distinguishable from other traps, so we don't need to
11236 apply keep hardware watchpoint moribund locations
11237 around. We simply always ignore hardware watchpoint
11238 traps we can no longer explain. */
11239
11240 process_stratum_target *proc_target = nullptr;
11241 for (inferior *inf : all_inferiors ())
11242 if (inf->pspace == old_loc->pspace)
11243 {
11244 proc_target = inf->process_target ();
11245 break;
11246 }
11247 if (proc_target != nullptr)
11248 old_loc->events_till_retirement
11249 = 3 * (thread_count (proc_target) + 1);
11250 else
11251 old_loc->events_till_retirement = 1;
11252 old_loc->owner = NULL;
11253
11254 moribund_locations.push_back (old_loc);
11255 }
11256 else
11257 {
11258 old_loc->owner = NULL;
11259 decref_bp_location (&old_loc);
11260 }
11261 }
11262 }
11263
11264 /* Rescan breakpoints at the same address and section, marking the
11265 first one as "first" and any others as "duplicates". This is so
11266 that the bpt instruction is only inserted once. If we have a
11267 permanent breakpoint at the same place as BPT, make that one the
11268 official one, and the rest as duplicates. Permanent breakpoints
11269 are sorted first for the same address.
11270
11271 Do the same for hardware watchpoints, but also considering the
11272 watchpoint's type (regular/access/read) and length. */
11273
11274 bp_loc_first = NULL;
11275 wp_loc_first = NULL;
11276 awp_loc_first = NULL;
11277 rwp_loc_first = NULL;
11278
11279 for (bp_location *loc : all_bp_locations ())
11280 {
11281 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11282 non-NULL. */
11283 struct bp_location **loc_first_p;
11284 breakpoint *b = loc->owner;
11285
11286 if (!unduplicated_should_be_inserted (loc)
11287 || !bl_address_is_meaningful (loc)
11288 /* Don't detect duplicate for tracepoint locations because they are
11289 never duplicated. See the comments in field `duplicate' of
11290 `struct bp_location'. */
11291 || is_tracepoint (b))
11292 {
11293 /* Clear the condition modification flag. */
11294 loc->condition_changed = condition_unchanged;
11295 continue;
11296 }
11297
11298 if (b->type == bp_hardware_watchpoint)
11299 loc_first_p = &wp_loc_first;
11300 else if (b->type == bp_read_watchpoint)
11301 loc_first_p = &rwp_loc_first;
11302 else if (b->type == bp_access_watchpoint)
11303 loc_first_p = &awp_loc_first;
11304 else
11305 loc_first_p = &bp_loc_first;
11306
11307 if (*loc_first_p == NULL
11308 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11309 || !breakpoint_locations_match (loc, *loc_first_p))
11310 {
11311 *loc_first_p = loc;
11312 loc->duplicate = 0;
11313
11314 if (is_breakpoint (loc->owner) && loc->condition_changed)
11315 {
11316 loc->needs_update = 1;
11317 /* Clear the condition modification flag. */
11318 loc->condition_changed = condition_unchanged;
11319 }
11320 continue;
11321 }
11322
11323
11324 /* This and the above ensure the invariant that the first location
11325 is not duplicated, and is the inserted one.
11326 All following are marked as duplicated, and are not inserted. */
11327 if (loc->inserted)
11328 swap_insertion (loc, *loc_first_p);
11329 loc->duplicate = 1;
11330
11331 /* Clear the condition modification flag. */
11332 loc->condition_changed = condition_unchanged;
11333 }
11334
11335 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
11336 {
11337 if (insert_mode != UGLL_DONT_INSERT)
11338 insert_breakpoint_locations ();
11339 else
11340 {
11341 /* Even though the caller told us to not insert new
11342 locations, we may still need to update conditions on the
11343 target's side of breakpoints that were already inserted
11344 if the target is evaluating breakpoint conditions. We
11345 only update conditions for locations that are marked
11346 "needs_update". */
11347 update_inserted_breakpoint_locations ();
11348 }
11349 }
11350
11351 if (insert_mode != UGLL_DONT_INSERT)
11352 download_tracepoint_locations ();
11353 }
11354
11355 void
11356 breakpoint_retire_moribund (void)
11357 {
11358 for (int ix = 0; ix < moribund_locations.size (); ++ix)
11359 {
11360 struct bp_location *loc = moribund_locations[ix];
11361 if (--(loc->events_till_retirement) == 0)
11362 {
11363 decref_bp_location (&loc);
11364 unordered_remove (moribund_locations, ix);
11365 --ix;
11366 }
11367 }
11368 }
11369
11370 static void
11371 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
11372 {
11373
11374 try
11375 {
11376 update_global_location_list (insert_mode);
11377 }
11378 catch (const gdb_exception_error &e)
11379 {
11380 }
11381 }
11382
11383 /* Clear BKP from a BPS. */
11384
11385 static void
11386 bpstat_remove_bp_location (bpstat *bps, struct breakpoint *bpt)
11387 {
11388 bpstat *bs;
11389
11390 for (bs = bps; bs; bs = bs->next)
11391 if (bs->breakpoint_at == bpt)
11392 {
11393 bs->breakpoint_at = NULL;
11394 bs->old_val = NULL;
11395 /* bs->commands will be freed later. */
11396 }
11397 }
11398
11399 /* Callback for iterate_over_threads. */
11400 static int
11401 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
11402 {
11403 struct breakpoint *bpt = (struct breakpoint *) data;
11404
11405 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
11406 return 0;
11407 }
11408
11409 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
11410 callbacks. */
11411
11412 static void
11413 say_where (struct breakpoint *b)
11414 {
11415 struct value_print_options opts;
11416
11417 get_user_print_options (&opts);
11418
11419 /* i18n: cagney/2005-02-11: Below needs to be merged into a
11420 single string. */
11421 if (b->loc == NULL)
11422 {
11423 /* For pending locations, the output differs slightly based
11424 on b->extra_string. If this is non-NULL, it contains either
11425 a condition or dprintf arguments. */
11426 if (b->extra_string == NULL)
11427 {
11428 gdb_printf (_(" (%s) pending."),
11429 event_location_to_string (b->location.get ()));
11430 }
11431 else if (b->type == bp_dprintf)
11432 {
11433 gdb_printf (_(" (%s,%s) pending."),
11434 event_location_to_string (b->location.get ()),
11435 b->extra_string.get ());
11436 }
11437 else
11438 {
11439 gdb_printf (_(" (%s %s) pending."),
11440 event_location_to_string (b->location.get ()),
11441 b->extra_string.get ());
11442 }
11443 }
11444 else
11445 {
11446 if (opts.addressprint || b->loc->symtab == NULL)
11447 gdb_printf (" at %ps",
11448 styled_string (address_style.style (),
11449 paddress (b->loc->gdbarch,
11450 b->loc->address)));
11451 if (b->loc->symtab != NULL)
11452 {
11453 /* If there is a single location, we can print the location
11454 more nicely. */
11455 if (b->loc->next == NULL)
11456 {
11457 const char *filename
11458 = symtab_to_filename_for_display (b->loc->symtab);
11459 gdb_printf (": file %ps, line %d.",
11460 styled_string (file_name_style.style (),
11461 filename),
11462 b->loc->line_number);
11463 }
11464 else
11465 /* This is not ideal, but each location may have a
11466 different file name, and this at least reflects the
11467 real situation somewhat. */
11468 gdb_printf (": %s.",
11469 event_location_to_string (b->location.get ()));
11470 }
11471
11472 if (b->loc->next)
11473 {
11474 struct bp_location *loc = b->loc;
11475 int n = 0;
11476 for (; loc; loc = loc->next)
11477 ++n;
11478 gdb_printf (" (%d locations)", n);
11479 }
11480 }
11481 }
11482
11483 /* See breakpoint.h. */
11484
11485 bp_location_range breakpoint::locations ()
11486 {
11487 return bp_location_range (this->loc);
11488 }
11489
11490 static struct bp_location *
11491 base_breakpoint_allocate_location (struct breakpoint *self)
11492 {
11493 return new bp_location (self);
11494 }
11495
11496 static void
11497 base_breakpoint_re_set (struct breakpoint *b)
11498 {
11499 /* Nothing to re-set. */
11500 }
11501
11502 #define internal_error_pure_virtual_called() \
11503 gdb_assert_not_reached ("pure virtual function called")
11504
11505 static int
11506 base_breakpoint_insert_location (struct bp_location *bl)
11507 {
11508 internal_error_pure_virtual_called ();
11509 }
11510
11511 static int
11512 base_breakpoint_remove_location (struct bp_location *bl,
11513 enum remove_bp_reason reason)
11514 {
11515 internal_error_pure_virtual_called ();
11516 }
11517
11518 static int
11519 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
11520 const address_space *aspace,
11521 CORE_ADDR bp_addr,
11522 const target_waitstatus &ws)
11523 {
11524 internal_error_pure_virtual_called ();
11525 }
11526
11527 static void
11528 base_breakpoint_check_status (bpstat *bs)
11529 {
11530 /* Always stop. */
11531 }
11532
11533 /* A "works_in_software_mode" breakpoint_ops method that just internal
11534 errors. */
11535
11536 static int
11537 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
11538 {
11539 internal_error_pure_virtual_called ();
11540 }
11541
11542 /* A "resources_needed" breakpoint_ops method that just internal
11543 errors. */
11544
11545 static int
11546 base_breakpoint_resources_needed (const struct bp_location *bl)
11547 {
11548 internal_error_pure_virtual_called ();
11549 }
11550
11551 static enum print_stop_action
11552 base_breakpoint_print_it (bpstat *bs)
11553 {
11554 internal_error_pure_virtual_called ();
11555 }
11556
11557 static bool
11558 base_breakpoint_print_one (struct breakpoint *, struct bp_location **)
11559 {
11560 return false;
11561 }
11562
11563 static void
11564 base_breakpoint_print_one_detail (const struct breakpoint *self,
11565 struct ui_out *uiout)
11566 {
11567 /* nothing */
11568 }
11569
11570 static void
11571 base_breakpoint_print_mention (struct breakpoint *b)
11572 {
11573 internal_error_pure_virtual_called ();
11574 }
11575
11576 static void
11577 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
11578 {
11579 internal_error_pure_virtual_called ();
11580 }
11581
11582 static void
11583 base_breakpoint_create_sals_from_location
11584 (struct event_location *location,
11585 struct linespec_result *canonical,
11586 enum bptype type_wanted)
11587 {
11588 internal_error_pure_virtual_called ();
11589 }
11590
11591 static void
11592 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
11593 struct linespec_result *c,
11594 gdb::unique_xmalloc_ptr<char> cond_string,
11595 gdb::unique_xmalloc_ptr<char> extra_string,
11596 enum bptype type_wanted,
11597 enum bpdisp disposition,
11598 int thread,
11599 int task, int ignore_count,
11600 const struct breakpoint_ops *o,
11601 int from_tty, int enabled,
11602 int internal, unsigned flags)
11603 {
11604 internal_error_pure_virtual_called ();
11605 }
11606
11607 static std::vector<symtab_and_line>
11608 base_breakpoint_decode_location (struct breakpoint *b,
11609 struct event_location *location,
11610 struct program_space *search_pspace)
11611 {
11612 internal_error_pure_virtual_called ();
11613 }
11614
11615 /* The default 'explains_signal' method. */
11616
11617 static int
11618 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
11619 {
11620 return 1;
11621 }
11622
11623 /* The default "after_condition_true" method. */
11624
11625 static void
11626 base_breakpoint_after_condition_true (struct bpstat *bs)
11627 {
11628 /* Nothing to do. */
11629 }
11630
11631 struct breakpoint_ops base_breakpoint_ops =
11632 {
11633 base_breakpoint_allocate_location,
11634 base_breakpoint_re_set,
11635 base_breakpoint_insert_location,
11636 base_breakpoint_remove_location,
11637 base_breakpoint_breakpoint_hit,
11638 base_breakpoint_check_status,
11639 base_breakpoint_resources_needed,
11640 base_breakpoint_works_in_software_mode,
11641 base_breakpoint_print_it,
11642 base_breakpoint_print_one,
11643 base_breakpoint_print_one_detail,
11644 base_breakpoint_print_mention,
11645 base_breakpoint_print_recreate,
11646 base_breakpoint_create_sals_from_location,
11647 base_breakpoint_create_breakpoints_sal,
11648 base_breakpoint_decode_location,
11649 base_breakpoint_explains_signal,
11650 base_breakpoint_after_condition_true,
11651 };
11652
11653 /* Default breakpoint_ops methods. */
11654
11655 static void
11656 bkpt_re_set (struct breakpoint *b)
11657 {
11658 /* FIXME: is this still reachable? */
11659 if (breakpoint_event_location_empty_p (b))
11660 {
11661 /* Anything without a location can't be re-set. */
11662 delete_breakpoint (b);
11663 return;
11664 }
11665
11666 breakpoint_re_set_default (b);
11667 }
11668
11669 static int
11670 bkpt_insert_location (struct bp_location *bl)
11671 {
11672 CORE_ADDR addr = bl->target_info.reqstd_address;
11673
11674 bl->target_info.kind = breakpoint_kind (bl, &addr);
11675 bl->target_info.placed_address = addr;
11676
11677 if (bl->loc_type == bp_loc_hardware_breakpoint)
11678 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
11679 else
11680 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
11681 }
11682
11683 static int
11684 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
11685 {
11686 if (bl->loc_type == bp_loc_hardware_breakpoint)
11687 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
11688 else
11689 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
11690 }
11691
11692 static int
11693 bkpt_breakpoint_hit (const struct bp_location *bl,
11694 const address_space *aspace, CORE_ADDR bp_addr,
11695 const target_waitstatus &ws)
11696 {
11697 if (ws.kind () != TARGET_WAITKIND_STOPPED
11698 || ws.sig () != GDB_SIGNAL_TRAP)
11699 return 0;
11700
11701 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
11702 aspace, bp_addr))
11703 return 0;
11704
11705 if (overlay_debugging /* unmapped overlay section */
11706 && section_is_overlay (bl->section)
11707 && !section_is_mapped (bl->section))
11708 return 0;
11709
11710 return 1;
11711 }
11712
11713 static int
11714 dprintf_breakpoint_hit (const struct bp_location *bl,
11715 const address_space *aspace, CORE_ADDR bp_addr,
11716 const target_waitstatus &ws)
11717 {
11718 if (dprintf_style == dprintf_style_agent
11719 && target_can_run_breakpoint_commands ())
11720 {
11721 /* An agent-style dprintf never causes a stop. If we see a trap
11722 for this address it must be for a breakpoint that happens to
11723 be set at the same address. */
11724 return 0;
11725 }
11726
11727 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
11728 }
11729
11730 static int
11731 bkpt_resources_needed (const struct bp_location *bl)
11732 {
11733 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
11734
11735 return 1;
11736 }
11737
11738 static enum print_stop_action
11739 bkpt_print_it (bpstat *bs)
11740 {
11741 struct breakpoint *b;
11742 const struct bp_location *bl;
11743 int bp_temp;
11744 struct ui_out *uiout = current_uiout;
11745
11746 gdb_assert (bs->bp_location_at != NULL);
11747
11748 bl = bs->bp_location_at.get ();
11749 b = bs->breakpoint_at;
11750
11751 bp_temp = b->disposition == disp_del;
11752 if (bl->address != bl->requested_address)
11753 breakpoint_adjustment_warning (bl->requested_address,
11754 bl->address,
11755 b->number, 1);
11756 annotate_breakpoint (b->number);
11757 maybe_print_thread_hit_breakpoint (uiout);
11758
11759 if (uiout->is_mi_like_p ())
11760 {
11761 uiout->field_string ("reason",
11762 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
11763 uiout->field_string ("disp", bpdisp_text (b->disposition));
11764 }
11765 if (bp_temp)
11766 uiout->message ("Temporary breakpoint %pF, ",
11767 signed_field ("bkptno", b->number));
11768 else
11769 uiout->message ("Breakpoint %pF, ",
11770 signed_field ("bkptno", b->number));
11771
11772 return PRINT_SRC_AND_LOC;
11773 }
11774
11775 static void
11776 bkpt_print_mention (struct breakpoint *b)
11777 {
11778 if (current_uiout->is_mi_like_p ())
11779 return;
11780
11781 switch (b->type)
11782 {
11783 case bp_breakpoint:
11784 case bp_gnu_ifunc_resolver:
11785 if (b->disposition == disp_del)
11786 gdb_printf (_("Temporary breakpoint"));
11787 else
11788 gdb_printf (_("Breakpoint"));
11789 gdb_printf (_(" %d"), b->number);
11790 if (b->type == bp_gnu_ifunc_resolver)
11791 gdb_printf (_(" at gnu-indirect-function resolver"));
11792 break;
11793 case bp_hardware_breakpoint:
11794 gdb_printf (_("Hardware assisted breakpoint %d"), b->number);
11795 break;
11796 case bp_dprintf:
11797 gdb_printf (_("Dprintf %d"), b->number);
11798 break;
11799 }
11800
11801 say_where (b);
11802 }
11803
11804 static void
11805 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
11806 {
11807 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
11808 gdb_printf (fp, "tbreak");
11809 else if (tp->type == bp_breakpoint)
11810 gdb_printf (fp, "break");
11811 else if (tp->type == bp_hardware_breakpoint
11812 && tp->disposition == disp_del)
11813 gdb_printf (fp, "thbreak");
11814 else if (tp->type == bp_hardware_breakpoint)
11815 gdb_printf (fp, "hbreak");
11816 else
11817 internal_error (__FILE__, __LINE__,
11818 _("unhandled breakpoint type %d"), (int) tp->type);
11819
11820 gdb_printf (fp, " %s",
11821 event_location_to_string (tp->location.get ()));
11822
11823 /* Print out extra_string if this breakpoint is pending. It might
11824 contain, for example, conditions that were set by the user. */
11825 if (tp->loc == NULL && tp->extra_string != NULL)
11826 gdb_printf (fp, " %s", tp->extra_string.get ());
11827
11828 print_recreate_thread (tp, fp);
11829 }
11830
11831 static std::vector<symtab_and_line>
11832 bkpt_decode_location (struct breakpoint *b,
11833 struct event_location *location,
11834 struct program_space *search_pspace)
11835 {
11836 return decode_location_default (b, location, search_pspace);
11837 }
11838
11839 /* Virtual table for internal breakpoints. */
11840
11841 static void
11842 internal_bkpt_re_set (struct breakpoint *b)
11843 {
11844 switch (b->type)
11845 {
11846 /* Delete overlay event and longjmp master breakpoints; they
11847 will be reset later by breakpoint_re_set. */
11848 case bp_overlay_event:
11849 case bp_longjmp_master:
11850 case bp_std_terminate_master:
11851 case bp_exception_master:
11852 delete_breakpoint (b);
11853 break;
11854
11855 /* This breakpoint is special, it's set up when the inferior
11856 starts and we really don't want to touch it. */
11857 case bp_shlib_event:
11858
11859 /* Like bp_shlib_event, this breakpoint type is special. Once
11860 it is set up, we do not want to touch it. */
11861 case bp_thread_event:
11862 break;
11863 }
11864 }
11865
11866 static void
11867 internal_bkpt_check_status (bpstat *bs)
11868 {
11869 if (bs->breakpoint_at->type == bp_shlib_event)
11870 {
11871 /* If requested, stop when the dynamic linker notifies GDB of
11872 events. This allows the user to get control and place
11873 breakpoints in initializer routines for dynamically loaded
11874 objects (among other things). */
11875 bs->stop = stop_on_solib_events;
11876 bs->print = stop_on_solib_events;
11877 }
11878 else
11879 bs->stop = 0;
11880 }
11881
11882 static enum print_stop_action
11883 internal_bkpt_print_it (bpstat *bs)
11884 {
11885 struct breakpoint *b;
11886
11887 b = bs->breakpoint_at;
11888
11889 switch (b->type)
11890 {
11891 case bp_shlib_event:
11892 /* Did we stop because the user set the stop_on_solib_events
11893 variable? (If so, we report this as a generic, "Stopped due
11894 to shlib event" message.) */
11895 print_solib_event (false);
11896 break;
11897
11898 case bp_thread_event:
11899 /* Not sure how we will get here.
11900 GDB should not stop for these breakpoints. */
11901 gdb_printf (_("Thread Event Breakpoint: gdb should not stop!\n"));
11902 break;
11903
11904 case bp_overlay_event:
11905 /* By analogy with the thread event, GDB should not stop for these. */
11906 gdb_printf (_("Overlay Event Breakpoint: gdb should not stop!\n"));
11907 break;
11908
11909 case bp_longjmp_master:
11910 /* These should never be enabled. */
11911 gdb_printf (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
11912 break;
11913
11914 case bp_std_terminate_master:
11915 /* These should never be enabled. */
11916 gdb_printf (_("std::terminate Master Breakpoint: "
11917 "gdb should not stop!\n"));
11918 break;
11919
11920 case bp_exception_master:
11921 /* These should never be enabled. */
11922 gdb_printf (_("Exception Master Breakpoint: "
11923 "gdb should not stop!\n"));
11924 break;
11925 }
11926
11927 return PRINT_NOTHING;
11928 }
11929
11930 static void
11931 internal_bkpt_print_mention (struct breakpoint *b)
11932 {
11933 /* Nothing to mention. These breakpoints are internal. */
11934 }
11935
11936 /* Virtual table for momentary breakpoints */
11937
11938 static void
11939 momentary_bkpt_re_set (struct breakpoint *b)
11940 {
11941 /* Keep temporary breakpoints, which can be encountered when we step
11942 over a dlopen call and solib_add is resetting the breakpoints.
11943 Otherwise these should have been blown away via the cleanup chain
11944 or by breakpoint_init_inferior when we rerun the executable. */
11945 }
11946
11947 static void
11948 momentary_bkpt_check_status (bpstat *bs)
11949 {
11950 /* Nothing. The point of these breakpoints is causing a stop. */
11951 }
11952
11953 static enum print_stop_action
11954 momentary_bkpt_print_it (bpstat *bs)
11955 {
11956 return PRINT_UNKNOWN;
11957 }
11958
11959 static void
11960 momentary_bkpt_print_mention (struct breakpoint *b)
11961 {
11962 /* Nothing to mention. These breakpoints are internal. */
11963 }
11964
11965 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
11966
11967 It gets cleared already on the removal of the first one of such placed
11968 breakpoints. This is OK as they get all removed altogether. */
11969
11970 longjmp_breakpoint::~longjmp_breakpoint ()
11971 {
11972 thread_info *tp = find_thread_global_id (this->thread);
11973
11974 if (tp != NULL)
11975 tp->initiating_frame = null_frame_id;
11976 }
11977
11978 /* Specific methods for probe breakpoints. */
11979
11980 static int
11981 bkpt_probe_insert_location (struct bp_location *bl)
11982 {
11983 int v = bkpt_insert_location (bl);
11984
11985 if (v == 0)
11986 {
11987 /* The insertion was successful, now let's set the probe's semaphore
11988 if needed. */
11989 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
11990 }
11991
11992 return v;
11993 }
11994
11995 static int
11996 bkpt_probe_remove_location (struct bp_location *bl,
11997 enum remove_bp_reason reason)
11998 {
11999 /* Let's clear the semaphore before removing the location. */
12000 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
12001
12002 return bkpt_remove_location (bl, reason);
12003 }
12004
12005 static void
12006 bkpt_probe_create_sals_from_location (struct event_location *location,
12007 struct linespec_result *canonical,
12008 enum bptype type_wanted)
12009 {
12010 struct linespec_sals lsal;
12011
12012 lsal.sals = parse_probes (location, NULL, canonical);
12013 lsal.canonical
12014 = xstrdup (event_location_to_string (canonical->location.get ()));
12015 canonical->lsals.push_back (std::move (lsal));
12016 }
12017
12018 static std::vector<symtab_and_line>
12019 bkpt_probe_decode_location (struct breakpoint *b,
12020 struct event_location *location,
12021 struct program_space *search_pspace)
12022 {
12023 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12024 if (sals.empty ())
12025 error (_("probe not found"));
12026 return sals;
12027 }
12028
12029 /* The breakpoint_ops structure to be used in tracepoints. */
12030
12031 static void
12032 tracepoint_re_set (struct breakpoint *b)
12033 {
12034 breakpoint_re_set_default (b);
12035 }
12036
12037 static int
12038 tracepoint_breakpoint_hit (const struct bp_location *bl,
12039 const address_space *aspace, CORE_ADDR bp_addr,
12040 const target_waitstatus &ws)
12041 {
12042 /* By definition, the inferior does not report stops at
12043 tracepoints. */
12044 return 0;
12045 }
12046
12047 static void
12048 tracepoint_print_one_detail (const struct breakpoint *self,
12049 struct ui_out *uiout)
12050 {
12051 struct tracepoint *tp = (struct tracepoint *) self;
12052 if (!tp->static_trace_marker_id.empty ())
12053 {
12054 gdb_assert (self->type == bp_static_tracepoint);
12055
12056 uiout->message ("\tmarker id is %pF\n",
12057 string_field ("static-tracepoint-marker-string-id",
12058 tp->static_trace_marker_id.c_str ()));
12059 }
12060 }
12061
12062 static void
12063 tracepoint_print_mention (struct breakpoint *b)
12064 {
12065 if (current_uiout->is_mi_like_p ())
12066 return;
12067
12068 switch (b->type)
12069 {
12070 case bp_tracepoint:
12071 gdb_printf (_("Tracepoint"));
12072 gdb_printf (_(" %d"), b->number);
12073 break;
12074 case bp_fast_tracepoint:
12075 gdb_printf (_("Fast tracepoint"));
12076 gdb_printf (_(" %d"), b->number);
12077 break;
12078 case bp_static_tracepoint:
12079 gdb_printf (_("Static tracepoint"));
12080 gdb_printf (_(" %d"), b->number);
12081 break;
12082 default:
12083 internal_error (__FILE__, __LINE__,
12084 _("unhandled tracepoint type %d"), (int) b->type);
12085 }
12086
12087 say_where (b);
12088 }
12089
12090 static void
12091 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
12092 {
12093 struct tracepoint *tp = (struct tracepoint *) self;
12094
12095 if (self->type == bp_fast_tracepoint)
12096 gdb_printf (fp, "ftrace");
12097 else if (self->type == bp_static_tracepoint)
12098 gdb_printf (fp, "strace");
12099 else if (self->type == bp_tracepoint)
12100 gdb_printf (fp, "trace");
12101 else
12102 internal_error (__FILE__, __LINE__,
12103 _("unhandled tracepoint type %d"), (int) self->type);
12104
12105 gdb_printf (fp, " %s",
12106 event_location_to_string (self->location.get ()));
12107 print_recreate_thread (self, fp);
12108
12109 if (tp->pass_count)
12110 gdb_printf (fp, " passcount %d\n", tp->pass_count);
12111 }
12112
12113 static std::vector<symtab_and_line>
12114 tracepoint_decode_location (struct breakpoint *b,
12115 struct event_location *location,
12116 struct program_space *search_pspace)
12117 {
12118 return decode_location_default (b, location, search_pspace);
12119 }
12120
12121 struct breakpoint_ops tracepoint_breakpoint_ops;
12122
12123 /* Virtual table for tracepoints on static probes. */
12124
12125 static void
12126 tracepoint_probe_create_sals_from_location
12127 (struct event_location *location,
12128 struct linespec_result *canonical,
12129 enum bptype type_wanted)
12130 {
12131 /* We use the same method for breakpoint on probes. */
12132 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
12133 }
12134
12135 static std::vector<symtab_and_line>
12136 tracepoint_probe_decode_location (struct breakpoint *b,
12137 struct event_location *location,
12138 struct program_space *search_pspace)
12139 {
12140 /* We use the same method for breakpoint on probes. */
12141 return bkpt_probe_decode_location (b, location, search_pspace);
12142 }
12143
12144 /* Dprintf breakpoint_ops methods. */
12145
12146 static void
12147 dprintf_re_set (struct breakpoint *b)
12148 {
12149 breakpoint_re_set_default (b);
12150
12151 /* extra_string should never be non-NULL for dprintf. */
12152 gdb_assert (b->extra_string != NULL);
12153
12154 /* 1 - connect to target 1, that can run breakpoint commands.
12155 2 - create a dprintf, which resolves fine.
12156 3 - disconnect from target 1
12157 4 - connect to target 2, that can NOT run breakpoint commands.
12158
12159 After steps #3/#4, you'll want the dprintf command list to
12160 be updated, because target 1 and 2 may well return different
12161 answers for target_can_run_breakpoint_commands().
12162 Given absence of finer grained resetting, we get to do
12163 it all the time. */
12164 if (b->extra_string != NULL)
12165 update_dprintf_command_list (b);
12166 }
12167
12168 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
12169
12170 static void
12171 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12172 {
12173 gdb_printf (fp, "dprintf %s,%s",
12174 event_location_to_string (tp->location.get ()),
12175 tp->extra_string.get ());
12176 print_recreate_thread (tp, fp);
12177 }
12178
12179 /* Implement the "after_condition_true" breakpoint_ops method for
12180 dprintf.
12181
12182 dprintf's are implemented with regular commands in their command
12183 list, but we run the commands here instead of before presenting the
12184 stop to the user, as dprintf's don't actually cause a stop. This
12185 also makes it so that the commands of multiple dprintfs at the same
12186 address are all handled. */
12187
12188 static void
12189 dprintf_after_condition_true (struct bpstat *bs)
12190 {
12191 /* dprintf's never cause a stop. This wasn't set in the
12192 check_status hook instead because that would make the dprintf's
12193 condition not be evaluated. */
12194 bs->stop = 0;
12195
12196 /* Run the command list here. Take ownership of it instead of
12197 copying. We never want these commands to run later in
12198 bpstat_do_actions, if a breakpoint that causes a stop happens to
12199 be set at same address as this dprintf, or even if running the
12200 commands here throws. */
12201 counted_command_line cmds = std::move (bs->commands);
12202 gdb_assert (cmds != nullptr);
12203 execute_control_commands (cmds.get (), 0);
12204 }
12205
12206 /* The breakpoint_ops structure to be used on static tracepoints with
12207 markers (`-m'). */
12208
12209 static void
12210 strace_marker_create_sals_from_location (struct event_location *location,
12211 struct linespec_result *canonical,
12212 enum bptype type_wanted)
12213 {
12214 struct linespec_sals lsal;
12215 const char *arg_start, *arg;
12216
12217 arg = arg_start = get_linespec_location (location)->spec_string;
12218 lsal.sals = decode_static_tracepoint_spec (&arg);
12219
12220 std::string str (arg_start, arg - arg_start);
12221 const char *ptr = str.c_str ();
12222 canonical->location
12223 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
12224
12225 lsal.canonical
12226 = xstrdup (event_location_to_string (canonical->location.get ()));
12227 canonical->lsals.push_back (std::move (lsal));
12228 }
12229
12230 static void
12231 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12232 struct linespec_result *canonical,
12233 gdb::unique_xmalloc_ptr<char> cond_string,
12234 gdb::unique_xmalloc_ptr<char> extra_string,
12235 enum bptype type_wanted,
12236 enum bpdisp disposition,
12237 int thread,
12238 int task, int ignore_count,
12239 const struct breakpoint_ops *ops,
12240 int from_tty, int enabled,
12241 int internal, unsigned flags)
12242 {
12243 const linespec_sals &lsal = canonical->lsals[0];
12244
12245 /* If the user is creating a static tracepoint by marker id
12246 (strace -m MARKER_ID), then store the sals index, so that
12247 breakpoint_re_set can try to match up which of the newly
12248 found markers corresponds to this one, and, don't try to
12249 expand multiple locations for each sal, given than SALS
12250 already should contain all sals for MARKER_ID. */
12251
12252 for (size_t i = 0; i < lsal.sals.size (); i++)
12253 {
12254 event_location_up location
12255 = copy_event_location (canonical->location.get ());
12256
12257 std::unique_ptr<tracepoint> tp (new tracepoint ());
12258 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
12259 std::move (location), NULL,
12260 std::move (cond_string),
12261 std::move (extra_string),
12262 type_wanted, disposition,
12263 thread, task, ignore_count, ops,
12264 from_tty, enabled, internal, flags,
12265 canonical->special_display);
12266 /* Given that its possible to have multiple markers with
12267 the same string id, if the user is creating a static
12268 tracepoint by marker id ("strace -m MARKER_ID"), then
12269 store the sals index, so that breakpoint_re_set can
12270 try to match up which of the newly found markers
12271 corresponds to this one */
12272 tp->static_trace_marker_id_idx = i;
12273
12274 install_breakpoint (internal, std::move (tp), 0);
12275 }
12276 }
12277
12278 static std::vector<symtab_and_line>
12279 strace_marker_decode_location (struct breakpoint *b,
12280 struct event_location *location,
12281 struct program_space *search_pspace)
12282 {
12283 struct tracepoint *tp = (struct tracepoint *) b;
12284 const char *s = get_linespec_location (location)->spec_string;
12285
12286 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
12287 if (sals.size () > tp->static_trace_marker_id_idx)
12288 {
12289 sals[0] = sals[tp->static_trace_marker_id_idx];
12290 sals.resize (1);
12291 return sals;
12292 }
12293 else
12294 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
12295 }
12296
12297 static struct breakpoint_ops strace_marker_breakpoint_ops;
12298
12299 static int
12300 strace_marker_p (struct breakpoint *b)
12301 {
12302 return b->ops == &strace_marker_breakpoint_ops;
12303 }
12304
12305 /* Delete a breakpoint and clean up all traces of it in the data
12306 structures. */
12307
12308 void
12309 delete_breakpoint (struct breakpoint *bpt)
12310 {
12311 gdb_assert (bpt != NULL);
12312
12313 /* Has this bp already been deleted? This can happen because
12314 multiple lists can hold pointers to bp's. bpstat lists are
12315 especial culprits.
12316
12317 One example of this happening is a watchpoint's scope bp. When
12318 the scope bp triggers, we notice that the watchpoint is out of
12319 scope, and delete it. We also delete its scope bp. But the
12320 scope bp is marked "auto-deleting", and is already on a bpstat.
12321 That bpstat is then checked for auto-deleting bp's, which are
12322 deleted.
12323
12324 A real solution to this problem might involve reference counts in
12325 bp's, and/or giving them pointers back to their referencing
12326 bpstat's, and teaching delete_breakpoint to only free a bp's
12327 storage when no more references were extent. A cheaper bandaid
12328 was chosen. */
12329 if (bpt->type == bp_none)
12330 return;
12331
12332 /* At least avoid this stale reference until the reference counting
12333 of breakpoints gets resolved. */
12334 if (bpt->related_breakpoint != bpt)
12335 {
12336 struct breakpoint *related;
12337 struct watchpoint *w;
12338
12339 if (bpt->type == bp_watchpoint_scope)
12340 w = (struct watchpoint *) bpt->related_breakpoint;
12341 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
12342 w = (struct watchpoint *) bpt;
12343 else
12344 w = NULL;
12345 if (w != NULL)
12346 watchpoint_del_at_next_stop (w);
12347
12348 /* Unlink bpt from the bpt->related_breakpoint ring. */
12349 for (related = bpt; related->related_breakpoint != bpt;
12350 related = related->related_breakpoint);
12351 related->related_breakpoint = bpt->related_breakpoint;
12352 bpt->related_breakpoint = bpt;
12353 }
12354
12355 /* watch_command_1 creates a watchpoint but only sets its number if
12356 update_watchpoint succeeds in creating its bp_locations. If there's
12357 a problem in that process, we'll be asked to delete the half-created
12358 watchpoint. In that case, don't announce the deletion. */
12359 if (bpt->number)
12360 gdb::observers::breakpoint_deleted.notify (bpt);
12361
12362 if (breakpoint_chain == bpt)
12363 breakpoint_chain = bpt->next;
12364
12365 for (breakpoint *b : all_breakpoints ())
12366 if (b->next == bpt)
12367 {
12368 b->next = bpt->next;
12369 break;
12370 }
12371
12372 /* Be sure no bpstat's are pointing at the breakpoint after it's
12373 been freed. */
12374 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
12375 in all threads for now. Note that we cannot just remove bpstats
12376 pointing at bpt from the stop_bpstat list entirely, as breakpoint
12377 commands are associated with the bpstat; if we remove it here,
12378 then the later call to bpstat_do_actions (&stop_bpstat); in
12379 event-top.c won't do anything, and temporary breakpoints with
12380 commands won't work. */
12381
12382 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
12383
12384 /* Now that breakpoint is removed from breakpoint list, update the
12385 global location list. This will remove locations that used to
12386 belong to this breakpoint. Do this before freeing the breakpoint
12387 itself, since remove_breakpoint looks at location's owner. It
12388 might be better design to have location completely
12389 self-contained, but it's not the case now. */
12390 update_global_location_list (UGLL_DONT_INSERT);
12391
12392 /* On the chance that someone will soon try again to delete this
12393 same bp, we mark it as deleted before freeing its storage. */
12394 bpt->type = bp_none;
12395 delete bpt;
12396 }
12397
12398 /* Iterator function to call a user-provided callback function once
12399 for each of B and its related breakpoints. */
12400
12401 static void
12402 iterate_over_related_breakpoints (struct breakpoint *b,
12403 gdb::function_view<void (breakpoint *)> function)
12404 {
12405 struct breakpoint *related;
12406
12407 related = b;
12408 do
12409 {
12410 struct breakpoint *next;
12411
12412 /* FUNCTION may delete RELATED. */
12413 next = related->related_breakpoint;
12414
12415 if (next == related)
12416 {
12417 /* RELATED is the last ring entry. */
12418 function (related);
12419
12420 /* FUNCTION may have deleted it, so we'd never reach back to
12421 B. There's nothing left to do anyway, so just break
12422 out. */
12423 break;
12424 }
12425 else
12426 function (related);
12427
12428 related = next;
12429 }
12430 while (related != b);
12431 }
12432
12433 static void
12434 delete_command (const char *arg, int from_tty)
12435 {
12436 dont_repeat ();
12437
12438 if (arg == 0)
12439 {
12440 int breaks_to_delete = 0;
12441
12442 /* Delete all breakpoints if no argument. Do not delete
12443 internal breakpoints, these have to be deleted with an
12444 explicit breakpoint number argument. */
12445 for (breakpoint *b : all_breakpoints ())
12446 if (user_breakpoint_p (b))
12447 {
12448 breaks_to_delete = 1;
12449 break;
12450 }
12451
12452 /* Ask user only if there are some breakpoints to delete. */
12453 if (!from_tty
12454 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
12455 for (breakpoint *b : all_breakpoints_safe ())
12456 if (user_breakpoint_p (b))
12457 delete_breakpoint (b);
12458 }
12459 else
12460 map_breakpoint_numbers
12461 (arg, [&] (breakpoint *br)
12462 {
12463 iterate_over_related_breakpoints (br, delete_breakpoint);
12464 });
12465 }
12466
12467 /* Return true if all locations of B bound to PSPACE are pending. If
12468 PSPACE is NULL, all locations of all program spaces are
12469 considered. */
12470
12471 static int
12472 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
12473 {
12474 for (bp_location *loc : b->locations ())
12475 if ((pspace == NULL
12476 || loc->pspace == pspace)
12477 && !loc->shlib_disabled
12478 && !loc->pspace->executing_startup)
12479 return 0;
12480 return 1;
12481 }
12482
12483 /* Subroutine of update_breakpoint_locations to simplify it.
12484 Return non-zero if multiple fns in list LOC have the same name.
12485 Null names are ignored. */
12486
12487 static int
12488 ambiguous_names_p (struct bp_location *loc)
12489 {
12490 struct bp_location *l;
12491 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
12492 xcalloc, xfree));
12493
12494 for (l = loc; l != NULL; l = l->next)
12495 {
12496 const char **slot;
12497 const char *name = l->function_name.get ();
12498
12499 /* Allow for some names to be NULL, ignore them. */
12500 if (name == NULL)
12501 continue;
12502
12503 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
12504 INSERT);
12505 /* NOTE: We can assume slot != NULL here because xcalloc never
12506 returns NULL. */
12507 if (*slot != NULL)
12508 return 1;
12509 *slot = name;
12510 }
12511
12512 return 0;
12513 }
12514
12515 /* When symbols change, it probably means the sources changed as well,
12516 and it might mean the static tracepoint markers are no longer at
12517 the same address or line numbers they used to be at last we
12518 checked. Losing your static tracepoints whenever you rebuild is
12519 undesirable. This function tries to resync/rematch gdb static
12520 tracepoints with the markers on the target, for static tracepoints
12521 that have not been set by marker id. Static tracepoint that have
12522 been set by marker id are reset by marker id in breakpoint_re_set.
12523 The heuristic is:
12524
12525 1) For a tracepoint set at a specific address, look for a marker at
12526 the old PC. If one is found there, assume to be the same marker.
12527 If the name / string id of the marker found is different from the
12528 previous known name, assume that means the user renamed the marker
12529 in the sources, and output a warning.
12530
12531 2) For a tracepoint set at a given line number, look for a marker
12532 at the new address of the old line number. If one is found there,
12533 assume to be the same marker. If the name / string id of the
12534 marker found is different from the previous known name, assume that
12535 means the user renamed the marker in the sources, and output a
12536 warning.
12537
12538 3) If a marker is no longer found at the same address or line, it
12539 may mean the marker no longer exists. But it may also just mean
12540 the code changed a bit. Maybe the user added a few lines of code
12541 that made the marker move up or down (in line number terms). Ask
12542 the target for info about the marker with the string id as we knew
12543 it. If found, update line number and address in the matching
12544 static tracepoint. This will get confused if there's more than one
12545 marker with the same ID (possible in UST, although unadvised
12546 precisely because it confuses tools). */
12547
12548 static struct symtab_and_line
12549 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
12550 {
12551 struct tracepoint *tp = (struct tracepoint *) b;
12552 struct static_tracepoint_marker marker;
12553 CORE_ADDR pc;
12554
12555 pc = sal.pc;
12556 if (sal.line)
12557 find_line_pc (sal.symtab, sal.line, &pc);
12558
12559 if (target_static_tracepoint_marker_at (pc, &marker))
12560 {
12561 if (tp->static_trace_marker_id != marker.str_id)
12562 warning (_("static tracepoint %d changed probed marker from %s to %s"),
12563 b->number, tp->static_trace_marker_id.c_str (),
12564 marker.str_id.c_str ());
12565
12566 tp->static_trace_marker_id = std::move (marker.str_id);
12567
12568 return sal;
12569 }
12570
12571 /* Old marker wasn't found on target at lineno. Try looking it up
12572 by string ID. */
12573 if (!sal.explicit_pc
12574 && sal.line != 0
12575 && sal.symtab != NULL
12576 && !tp->static_trace_marker_id.empty ())
12577 {
12578 std::vector<static_tracepoint_marker> markers
12579 = target_static_tracepoint_markers_by_strid
12580 (tp->static_trace_marker_id.c_str ());
12581
12582 if (!markers.empty ())
12583 {
12584 struct symbol *sym;
12585 struct static_tracepoint_marker *tpmarker;
12586 struct ui_out *uiout = current_uiout;
12587 struct explicit_location explicit_loc;
12588
12589 tpmarker = &markers[0];
12590
12591 tp->static_trace_marker_id = std::move (tpmarker->str_id);
12592
12593 warning (_("marker for static tracepoint %d (%s) not "
12594 "found at previous line number"),
12595 b->number, tp->static_trace_marker_id.c_str ());
12596
12597 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
12598 sym = find_pc_sect_function (tpmarker->address, NULL);
12599 uiout->text ("Now in ");
12600 if (sym)
12601 {
12602 uiout->field_string ("func", sym->print_name (),
12603 function_name_style.style ());
12604 uiout->text (" at ");
12605 }
12606 uiout->field_string ("file",
12607 symtab_to_filename_for_display (sal2.symtab),
12608 file_name_style.style ());
12609 uiout->text (":");
12610
12611 if (uiout->is_mi_like_p ())
12612 {
12613 const char *fullname = symtab_to_fullname (sal2.symtab);
12614
12615 uiout->field_string ("fullname", fullname);
12616 }
12617
12618 uiout->field_signed ("line", sal2.line);
12619 uiout->text ("\n");
12620
12621 b->loc->line_number = sal2.line;
12622 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
12623
12624 b->location.reset (NULL);
12625 initialize_explicit_location (&explicit_loc);
12626 explicit_loc.source_filename
12627 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
12628 explicit_loc.line_offset.offset = b->loc->line_number;
12629 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
12630 b->location = new_explicit_location (&explicit_loc);
12631
12632 /* Might be nice to check if function changed, and warn if
12633 so. */
12634 }
12635 }
12636 return sal;
12637 }
12638
12639 /* Returns 1 iff locations A and B are sufficiently same that
12640 we don't need to report breakpoint as changed. */
12641
12642 static int
12643 locations_are_equal (struct bp_location *a, struct bp_location *b)
12644 {
12645 while (a && b)
12646 {
12647 if (a->address != b->address)
12648 return 0;
12649
12650 if (a->shlib_disabled != b->shlib_disabled)
12651 return 0;
12652
12653 if (a->enabled != b->enabled)
12654 return 0;
12655
12656 if (a->disabled_by_cond != b->disabled_by_cond)
12657 return 0;
12658
12659 a = a->next;
12660 b = b->next;
12661 }
12662
12663 if ((a == NULL) != (b == NULL))
12664 return 0;
12665
12666 return 1;
12667 }
12668
12669 /* Split all locations of B that are bound to PSPACE out of B's
12670 location list to a separate list and return that list's head. If
12671 PSPACE is NULL, hoist out all locations of B. */
12672
12673 static struct bp_location *
12674 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
12675 {
12676 struct bp_location head;
12677 struct bp_location *i = b->loc;
12678 struct bp_location **i_link = &b->loc;
12679 struct bp_location *hoisted = &head;
12680
12681 if (pspace == NULL)
12682 {
12683 i = b->loc;
12684 b->loc = NULL;
12685 return i;
12686 }
12687
12688 head.next = NULL;
12689
12690 while (i != NULL)
12691 {
12692 if (i->pspace == pspace)
12693 {
12694 *i_link = i->next;
12695 i->next = NULL;
12696 hoisted->next = i;
12697 hoisted = i;
12698 }
12699 else
12700 i_link = &i->next;
12701 i = *i_link;
12702 }
12703
12704 return head.next;
12705 }
12706
12707 /* Create new breakpoint locations for B (a hardware or software
12708 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
12709 zero, then B is a ranged breakpoint. Only recreates locations for
12710 FILTER_PSPACE. Locations of other program spaces are left
12711 untouched. */
12712
12713 void
12714 update_breakpoint_locations (struct breakpoint *b,
12715 struct program_space *filter_pspace,
12716 gdb::array_view<const symtab_and_line> sals,
12717 gdb::array_view<const symtab_and_line> sals_end)
12718 {
12719 struct bp_location *existing_locations;
12720
12721 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
12722 {
12723 /* Ranged breakpoints have only one start location and one end
12724 location. */
12725 b->enable_state = bp_disabled;
12726 gdb_printf (gdb_stderr,
12727 _("Could not reset ranged breakpoint %d: "
12728 "multiple locations found\n"),
12729 b->number);
12730 return;
12731 }
12732
12733 /* If there's no new locations, and all existing locations are
12734 pending, don't do anything. This optimizes the common case where
12735 all locations are in the same shared library, that was unloaded.
12736 We'd like to retain the location, so that when the library is
12737 loaded again, we don't loose the enabled/disabled status of the
12738 individual locations. */
12739 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
12740 return;
12741
12742 existing_locations = hoist_existing_locations (b, filter_pspace);
12743
12744 for (const auto &sal : sals)
12745 {
12746 struct bp_location *new_loc;
12747
12748 switch_to_program_space_and_thread (sal.pspace);
12749
12750 new_loc = add_location_to_breakpoint (b, &sal);
12751
12752 /* Reparse conditions, they might contain references to the
12753 old symtab. */
12754 if (b->cond_string != NULL)
12755 {
12756 const char *s;
12757
12758 s = b->cond_string.get ();
12759 try
12760 {
12761 new_loc->cond = parse_exp_1 (&s, sal.pc,
12762 block_for_pc (sal.pc),
12763 0);
12764 }
12765 catch (const gdb_exception_error &e)
12766 {
12767 new_loc->disabled_by_cond = true;
12768 }
12769 }
12770
12771 if (!sals_end.empty ())
12772 {
12773 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
12774
12775 new_loc->length = end - sals[0].pc + 1;
12776 }
12777 }
12778
12779 /* If possible, carry over 'disable' status from existing
12780 breakpoints. */
12781 {
12782 struct bp_location *e = existing_locations;
12783 /* If there are multiple breakpoints with the same function name,
12784 e.g. for inline functions, comparing function names won't work.
12785 Instead compare pc addresses; this is just a heuristic as things
12786 may have moved, but in practice it gives the correct answer
12787 often enough until a better solution is found. */
12788 int have_ambiguous_names = ambiguous_names_p (b->loc);
12789
12790 for (; e; e = e->next)
12791 {
12792 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
12793 {
12794 if (have_ambiguous_names)
12795 {
12796 for (bp_location *l : b->locations ())
12797 {
12798 /* Ignore software vs hardware location type at
12799 this point, because with "set breakpoint
12800 auto-hw", after a re-set, locations that were
12801 hardware can end up as software, or vice versa.
12802 As mentioned above, this is an heuristic and in
12803 practice should give the correct answer often
12804 enough. */
12805 if (breakpoint_locations_match (e, l, true))
12806 {
12807 l->enabled = e->enabled;
12808 l->disabled_by_cond = e->disabled_by_cond;
12809 break;
12810 }
12811 }
12812 }
12813 else
12814 {
12815 for (bp_location *l : b->locations ())
12816 if (l->function_name
12817 && strcmp (e->function_name.get (),
12818 l->function_name.get ()) == 0)
12819 {
12820 l->enabled = e->enabled;
12821 l->disabled_by_cond = e->disabled_by_cond;
12822 break;
12823 }
12824 }
12825 }
12826 }
12827 }
12828
12829 if (!locations_are_equal (existing_locations, b->loc))
12830 gdb::observers::breakpoint_modified.notify (b);
12831 }
12832
12833 /* Find the SaL locations corresponding to the given LOCATION.
12834 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
12835
12836 static std::vector<symtab_and_line>
12837 location_to_sals (struct breakpoint *b, struct event_location *location,
12838 struct program_space *search_pspace, int *found)
12839 {
12840 struct gdb_exception exception;
12841
12842 gdb_assert (b->ops != NULL);
12843
12844 std::vector<symtab_and_line> sals;
12845
12846 try
12847 {
12848 sals = b->ops->decode_location (b, location, search_pspace);
12849 }
12850 catch (gdb_exception_error &e)
12851 {
12852 int not_found_and_ok = 0;
12853
12854 /* For pending breakpoints, it's expected that parsing will
12855 fail until the right shared library is loaded. User has
12856 already told to create pending breakpoints and don't need
12857 extra messages. If breakpoint is in bp_shlib_disabled
12858 state, then user already saw the message about that
12859 breakpoint being disabled, and don't want to see more
12860 errors. */
12861 if (e.error == NOT_FOUND_ERROR
12862 && (b->condition_not_parsed
12863 || (b->loc != NULL
12864 && search_pspace != NULL
12865 && b->loc->pspace != search_pspace)
12866 || (b->loc && b->loc->shlib_disabled)
12867 || (b->loc && b->loc->pspace->executing_startup)
12868 || b->enable_state == bp_disabled))
12869 not_found_and_ok = 1;
12870
12871 if (!not_found_and_ok)
12872 {
12873 /* We surely don't want to warn about the same breakpoint
12874 10 times. One solution, implemented here, is disable
12875 the breakpoint on error. Another solution would be to
12876 have separate 'warning emitted' flag. Since this
12877 happens only when a binary has changed, I don't know
12878 which approach is better. */
12879 b->enable_state = bp_disabled;
12880 throw;
12881 }
12882
12883 exception = std::move (e);
12884 }
12885
12886 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
12887 {
12888 for (auto &sal : sals)
12889 resolve_sal_pc (&sal);
12890 if (b->condition_not_parsed && b->extra_string != NULL)
12891 {
12892 gdb::unique_xmalloc_ptr<char> cond_string, extra_string;
12893 int thread, task;
12894
12895 find_condition_and_thread_for_sals (sals, b->extra_string.get (),
12896 &cond_string, &thread,
12897 &task, &extra_string);
12898 gdb_assert (b->cond_string == NULL);
12899 if (cond_string)
12900 b->cond_string = std::move (cond_string);
12901 b->thread = thread;
12902 b->task = task;
12903 if (extra_string)
12904 b->extra_string = std::move (extra_string);
12905 b->condition_not_parsed = 0;
12906 }
12907
12908 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
12909 sals[0] = update_static_tracepoint (b, sals[0]);
12910
12911 *found = 1;
12912 }
12913 else
12914 *found = 0;
12915
12916 return sals;
12917 }
12918
12919 /* The default re_set method, for typical hardware or software
12920 breakpoints. Reevaluate the breakpoint and recreate its
12921 locations. */
12922
12923 static void
12924 breakpoint_re_set_default (struct breakpoint *b)
12925 {
12926 struct program_space *filter_pspace = current_program_space;
12927 std::vector<symtab_and_line> expanded, expanded_end;
12928
12929 int found;
12930 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
12931 filter_pspace, &found);
12932 if (found)
12933 expanded = std::move (sals);
12934
12935 if (b->location_range_end != NULL)
12936 {
12937 std::vector<symtab_and_line> sals_end
12938 = location_to_sals (b, b->location_range_end.get (),
12939 filter_pspace, &found);
12940 if (found)
12941 expanded_end = std::move (sals_end);
12942 }
12943
12944 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
12945 }
12946
12947 /* Default method for creating SALs from an address string. It basically
12948 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
12949
12950 static void
12951 create_sals_from_location_default (struct event_location *location,
12952 struct linespec_result *canonical,
12953 enum bptype type_wanted)
12954 {
12955 parse_breakpoint_sals (location, canonical);
12956 }
12957
12958 /* Call create_breakpoints_sal for the given arguments. This is the default
12959 function for the `create_breakpoints_sal' method of
12960 breakpoint_ops. */
12961
12962 static void
12963 create_breakpoints_sal_default (struct gdbarch *gdbarch,
12964 struct linespec_result *canonical,
12965 gdb::unique_xmalloc_ptr<char> cond_string,
12966 gdb::unique_xmalloc_ptr<char> extra_string,
12967 enum bptype type_wanted,
12968 enum bpdisp disposition,
12969 int thread,
12970 int task, int ignore_count,
12971 const struct breakpoint_ops *ops,
12972 int from_tty, int enabled,
12973 int internal, unsigned flags)
12974 {
12975 create_breakpoints_sal (gdbarch, canonical,
12976 std::move (cond_string),
12977 std::move (extra_string),
12978 type_wanted, disposition,
12979 thread, task, ignore_count, ops, from_tty,
12980 enabled, internal, flags);
12981 }
12982
12983 /* Decode the line represented by S by calling decode_line_full. This is the
12984 default function for the `decode_location' method of breakpoint_ops. */
12985
12986 static std::vector<symtab_and_line>
12987 decode_location_default (struct breakpoint *b,
12988 struct event_location *location,
12989 struct program_space *search_pspace)
12990 {
12991 struct linespec_result canonical;
12992
12993 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
12994 NULL, 0, &canonical, multiple_symbols_all,
12995 b->filter.get ());
12996
12997 /* We should get 0 or 1 resulting SALs. */
12998 gdb_assert (canonical.lsals.size () < 2);
12999
13000 if (!canonical.lsals.empty ())
13001 {
13002 const linespec_sals &lsal = canonical.lsals[0];
13003 return std::move (lsal.sals);
13004 }
13005 return {};
13006 }
13007
13008 /* Reset a breakpoint. */
13009
13010 static void
13011 breakpoint_re_set_one (breakpoint *b)
13012 {
13013 input_radix = b->input_radix;
13014 set_language (b->language);
13015
13016 b->ops->re_set (b);
13017 }
13018
13019 /* Re-set breakpoint locations for the current program space.
13020 Locations bound to other program spaces are left untouched. */
13021
13022 void
13023 breakpoint_re_set (void)
13024 {
13025 {
13026 scoped_restore_current_language save_language;
13027 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
13028 scoped_restore_current_pspace_and_thread restore_pspace_thread;
13029
13030 /* breakpoint_re_set_one sets the current_language to the language
13031 of the breakpoint it is resetting (see prepare_re_set_context)
13032 before re-evaluating the breakpoint's location. This change can
13033 unfortunately get undone by accident if the language_mode is set
13034 to auto, and we either switch frames, or more likely in this context,
13035 we select the current frame.
13036
13037 We prevent this by temporarily turning the language_mode to
13038 language_mode_manual. We restore it once all breakpoints
13039 have been reset. */
13040 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13041 language_mode = language_mode_manual;
13042
13043 /* Note: we must not try to insert locations until after all
13044 breakpoints have been re-set. Otherwise, e.g., when re-setting
13045 breakpoint 1, we'd insert the locations of breakpoint 2, which
13046 hadn't been re-set yet, and thus may have stale locations. */
13047
13048 for (breakpoint *b : all_breakpoints_safe ())
13049 {
13050 try
13051 {
13052 breakpoint_re_set_one (b);
13053 }
13054 catch (const gdb_exception &ex)
13055 {
13056 exception_fprintf (gdb_stderr, ex,
13057 "Error in re-setting breakpoint %d: ",
13058 b->number);
13059 }
13060 }
13061
13062 jit_breakpoint_re_set ();
13063 }
13064
13065 create_overlay_event_breakpoint ();
13066 create_longjmp_master_breakpoint ();
13067 create_std_terminate_master_breakpoint ();
13068 create_exception_master_breakpoint ();
13069
13070 /* Now we can insert. */
13071 update_global_location_list (UGLL_MAY_INSERT);
13072 }
13073 \f
13074 /* Reset the thread number of this breakpoint:
13075
13076 - If the breakpoint is for all threads, leave it as-is.
13077 - Else, reset it to the current thread for inferior_ptid. */
13078 void
13079 breakpoint_re_set_thread (struct breakpoint *b)
13080 {
13081 if (b->thread != -1)
13082 {
13083 b->thread = inferior_thread ()->global_num;
13084
13085 /* We're being called after following a fork. The new fork is
13086 selected as current, and unless this was a vfork will have a
13087 different program space from the original thread. Reset that
13088 as well. */
13089 b->loc->pspace = current_program_space;
13090 }
13091 }
13092
13093 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13094 If from_tty is nonzero, it prints a message to that effect,
13095 which ends with a period (no newline). */
13096
13097 void
13098 set_ignore_count (int bptnum, int count, int from_tty)
13099 {
13100 if (count < 0)
13101 count = 0;
13102
13103 for (breakpoint *b : all_breakpoints ())
13104 if (b->number == bptnum)
13105 {
13106 if (is_tracepoint (b))
13107 {
13108 if (from_tty && count != 0)
13109 gdb_printf (_("Ignore count ignored for tracepoint %d."),
13110 bptnum);
13111 return;
13112 }
13113
13114 b->ignore_count = count;
13115 if (from_tty)
13116 {
13117 if (count == 0)
13118 gdb_printf (_("Will stop next time "
13119 "breakpoint %d is reached."),
13120 bptnum);
13121 else if (count == 1)
13122 gdb_printf (_("Will ignore next crossing of breakpoint %d."),
13123 bptnum);
13124 else
13125 gdb_printf (_("Will ignore next %d "
13126 "crossings of breakpoint %d."),
13127 count, bptnum);
13128 }
13129 gdb::observers::breakpoint_modified.notify (b);
13130 return;
13131 }
13132
13133 error (_("No breakpoint number %d."), bptnum);
13134 }
13135
13136 /* Command to set ignore-count of breakpoint N to COUNT. */
13137
13138 static void
13139 ignore_command (const char *args, int from_tty)
13140 {
13141 const char *p = args;
13142 int num;
13143
13144 if (p == 0)
13145 error_no_arg (_("a breakpoint number"));
13146
13147 num = get_number (&p);
13148 if (num == 0)
13149 error (_("bad breakpoint number: '%s'"), args);
13150 if (*p == 0)
13151 error (_("Second argument (specified ignore-count) is missing."));
13152
13153 set_ignore_count (num,
13154 longest_to_int (value_as_long (parse_and_eval (p))),
13155 from_tty);
13156 if (from_tty)
13157 gdb_printf ("\n");
13158 }
13159 \f
13160
13161 /* Call FUNCTION on each of the breakpoints with numbers in the range
13162 defined by BP_NUM_RANGE (an inclusive range). */
13163
13164 static void
13165 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13166 gdb::function_view<void (breakpoint *)> function)
13167 {
13168 if (bp_num_range.first == 0)
13169 {
13170 warning (_("bad breakpoint number at or near '%d'"),
13171 bp_num_range.first);
13172 }
13173 else
13174 {
13175 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
13176 {
13177 bool match = false;
13178
13179 for (breakpoint *b : all_breakpoints_safe ())
13180 if (b->number == i)
13181 {
13182 match = true;
13183 function (b);
13184 break;
13185 }
13186 if (!match)
13187 gdb_printf (_("No breakpoint number %d.\n"), i);
13188 }
13189 }
13190 }
13191
13192 /* Call FUNCTION on each of the breakpoints whose numbers are given in
13193 ARGS. */
13194
13195 static void
13196 map_breakpoint_numbers (const char *args,
13197 gdb::function_view<void (breakpoint *)> function)
13198 {
13199 if (args == NULL || *args == '\0')
13200 error_no_arg (_("one or more breakpoint numbers"));
13201
13202 number_or_range_parser parser (args);
13203
13204 while (!parser.finished ())
13205 {
13206 int num = parser.get_number ();
13207 map_breakpoint_number_range (std::make_pair (num, num), function);
13208 }
13209 }
13210
13211 /* Return the breakpoint location structure corresponding to the
13212 BP_NUM and LOC_NUM values. */
13213
13214 static struct bp_location *
13215 find_location_by_number (int bp_num, int loc_num)
13216 {
13217 breakpoint *b = get_breakpoint (bp_num);
13218
13219 if (!b || b->number != bp_num)
13220 error (_("Bad breakpoint number '%d'"), bp_num);
13221
13222 if (loc_num == 0)
13223 error (_("Bad breakpoint location number '%d'"), loc_num);
13224
13225 int n = 0;
13226 for (bp_location *loc : b->locations ())
13227 if (++n == loc_num)
13228 return loc;
13229
13230 error (_("Bad breakpoint location number '%d'"), loc_num);
13231 }
13232
13233 /* Modes of operation for extract_bp_num. */
13234 enum class extract_bp_kind
13235 {
13236 /* Extracting a breakpoint number. */
13237 bp,
13238
13239 /* Extracting a location number. */
13240 loc,
13241 };
13242
13243 /* Extract a breakpoint or location number (as determined by KIND)
13244 from the string starting at START. TRAILER is a character which
13245 can be found after the number. If you don't want a trailer, use
13246 '\0'. If END_OUT is not NULL, it is set to point after the parsed
13247 string. This always returns a positive integer. */
13248
13249 static int
13250 extract_bp_num (extract_bp_kind kind, const char *start,
13251 int trailer, const char **end_out = NULL)
13252 {
13253 const char *end = start;
13254 int num = get_number_trailer (&end, trailer);
13255 if (num < 0)
13256 error (kind == extract_bp_kind::bp
13257 ? _("Negative breakpoint number '%.*s'")
13258 : _("Negative breakpoint location number '%.*s'"),
13259 int (end - start), start);
13260 if (num == 0)
13261 error (kind == extract_bp_kind::bp
13262 ? _("Bad breakpoint number '%.*s'")
13263 : _("Bad breakpoint location number '%.*s'"),
13264 int (end - start), start);
13265
13266 if (end_out != NULL)
13267 *end_out = end;
13268 return num;
13269 }
13270
13271 /* Extract a breakpoint or location range (as determined by KIND) in
13272 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
13273 representing the (inclusive) range. The returned pair's elements
13274 are always positive integers. */
13275
13276 static std::pair<int, int>
13277 extract_bp_or_bp_range (extract_bp_kind kind,
13278 const std::string &arg,
13279 std::string::size_type arg_offset)
13280 {
13281 std::pair<int, int> range;
13282 const char *bp_loc = &arg[arg_offset];
13283 std::string::size_type dash = arg.find ('-', arg_offset);
13284 if (dash != std::string::npos)
13285 {
13286 /* bp_loc is a range (x-z). */
13287 if (arg.length () == dash + 1)
13288 error (kind == extract_bp_kind::bp
13289 ? _("Bad breakpoint number at or near: '%s'")
13290 : _("Bad breakpoint location number at or near: '%s'"),
13291 bp_loc);
13292
13293 const char *end;
13294 const char *start_first = bp_loc;
13295 const char *start_second = &arg[dash + 1];
13296 range.first = extract_bp_num (kind, start_first, '-');
13297 range.second = extract_bp_num (kind, start_second, '\0', &end);
13298
13299 if (range.first > range.second)
13300 error (kind == extract_bp_kind::bp
13301 ? _("Inverted breakpoint range at '%.*s'")
13302 : _("Inverted breakpoint location range at '%.*s'"),
13303 int (end - start_first), start_first);
13304 }
13305 else
13306 {
13307 /* bp_loc is a single value. */
13308 range.first = extract_bp_num (kind, bp_loc, '\0');
13309 range.second = range.first;
13310 }
13311 return range;
13312 }
13313
13314 /* Extract the breakpoint/location range specified by ARG. Returns
13315 the breakpoint range in BP_NUM_RANGE, and the location range in
13316 BP_LOC_RANGE.
13317
13318 ARG may be in any of the following forms:
13319
13320 x where 'x' is a breakpoint number.
13321 x-y where 'x' and 'y' specify a breakpoint numbers range.
13322 x.y where 'x' is a breakpoint number and 'y' a location number.
13323 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
13324 location number range.
13325 */
13326
13327 static void
13328 extract_bp_number_and_location (const std::string &arg,
13329 std::pair<int, int> &bp_num_range,
13330 std::pair<int, int> &bp_loc_range)
13331 {
13332 std::string::size_type dot = arg.find ('.');
13333
13334 if (dot != std::string::npos)
13335 {
13336 /* Handle 'x.y' and 'x.y-z' cases. */
13337
13338 if (arg.length () == dot + 1 || dot == 0)
13339 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
13340
13341 bp_num_range.first
13342 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
13343 bp_num_range.second = bp_num_range.first;
13344
13345 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
13346 arg, dot + 1);
13347 }
13348 else
13349 {
13350 /* Handle x and x-y cases. */
13351
13352 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
13353 bp_loc_range.first = 0;
13354 bp_loc_range.second = 0;
13355 }
13356 }
13357
13358 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
13359 specifies whether to enable or disable. */
13360
13361 static void
13362 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
13363 {
13364 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
13365 if (loc != NULL)
13366 {
13367 if (loc->disabled_by_cond && enable)
13368 error (_("Breakpoint %d's condition is invalid at location %d, "
13369 "cannot enable."), bp_num, loc_num);
13370
13371 if (loc->enabled != enable)
13372 {
13373 loc->enabled = enable;
13374 mark_breakpoint_location_modified (loc);
13375 }
13376 if (target_supports_enable_disable_tracepoint ()
13377 && current_trace_status ()->running && loc->owner
13378 && is_tracepoint (loc->owner))
13379 target_disable_tracepoint (loc);
13380 }
13381 update_global_location_list (UGLL_DONT_INSERT);
13382
13383 gdb::observers::breakpoint_modified.notify (loc->owner);
13384 }
13385
13386 /* Enable or disable a range of breakpoint locations. BP_NUM is the
13387 number of the breakpoint, and BP_LOC_RANGE specifies the
13388 (inclusive) range of location numbers of that breakpoint to
13389 enable/disable. ENABLE specifies whether to enable or disable the
13390 location. */
13391
13392 static void
13393 enable_disable_breakpoint_location_range (int bp_num,
13394 std::pair<int, int> &bp_loc_range,
13395 bool enable)
13396 {
13397 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
13398 enable_disable_bp_num_loc (bp_num, i, enable);
13399 }
13400
13401 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13402 If from_tty is nonzero, it prints a message to that effect,
13403 which ends with a period (no newline). */
13404
13405 void
13406 disable_breakpoint (struct breakpoint *bpt)
13407 {
13408 /* Never disable a watchpoint scope breakpoint; we want to
13409 hit them when we leave scope so we can delete both the
13410 watchpoint and its scope breakpoint at that time. */
13411 if (bpt->type == bp_watchpoint_scope)
13412 return;
13413
13414 bpt->enable_state = bp_disabled;
13415
13416 /* Mark breakpoint locations modified. */
13417 mark_breakpoint_modified (bpt);
13418
13419 if (target_supports_enable_disable_tracepoint ()
13420 && current_trace_status ()->running && is_tracepoint (bpt))
13421 {
13422 for (bp_location *location : bpt->locations ())
13423 target_disable_tracepoint (location);
13424 }
13425
13426 update_global_location_list (UGLL_DONT_INSERT);
13427
13428 gdb::observers::breakpoint_modified.notify (bpt);
13429 }
13430
13431 /* Enable or disable the breakpoint(s) or breakpoint location(s)
13432 specified in ARGS. ARGS may be in any of the formats handled by
13433 extract_bp_number_and_location. ENABLE specifies whether to enable
13434 or disable the breakpoints/locations. */
13435
13436 static void
13437 enable_disable_command (const char *args, int from_tty, bool enable)
13438 {
13439 if (args == 0)
13440 {
13441 for (breakpoint *bpt : all_breakpoints ())
13442 if (user_breakpoint_p (bpt))
13443 {
13444 if (enable)
13445 enable_breakpoint (bpt);
13446 else
13447 disable_breakpoint (bpt);
13448 }
13449 }
13450 else
13451 {
13452 std::string num = extract_arg (&args);
13453
13454 while (!num.empty ())
13455 {
13456 std::pair<int, int> bp_num_range, bp_loc_range;
13457
13458 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
13459
13460 if (bp_loc_range.first == bp_loc_range.second
13461 && bp_loc_range.first == 0)
13462 {
13463 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
13464 map_breakpoint_number_range (bp_num_range,
13465 enable
13466 ? enable_breakpoint
13467 : disable_breakpoint);
13468 }
13469 else
13470 {
13471 /* Handle breakpoint ids with formats 'x.y' or
13472 'x.y-z'. */
13473 enable_disable_breakpoint_location_range
13474 (bp_num_range.first, bp_loc_range, enable);
13475 }
13476 num = extract_arg (&args);
13477 }
13478 }
13479 }
13480
13481 /* The disable command disables the specified breakpoints/locations
13482 (or all defined breakpoints) so they're no longer effective in
13483 stopping the inferior. ARGS may be in any of the forms defined in
13484 extract_bp_number_and_location. */
13485
13486 static void
13487 disable_command (const char *args, int from_tty)
13488 {
13489 enable_disable_command (args, from_tty, false);
13490 }
13491
13492 static void
13493 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
13494 int count)
13495 {
13496 int target_resources_ok;
13497
13498 if (bpt->type == bp_hardware_breakpoint)
13499 {
13500 int i;
13501 i = hw_breakpoint_used_count ();
13502 target_resources_ok =
13503 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
13504 i + 1, 0);
13505 if (target_resources_ok == 0)
13506 error (_("No hardware breakpoint support in the target."));
13507 else if (target_resources_ok < 0)
13508 error (_("Hardware breakpoints used exceeds limit."));
13509 }
13510
13511 if (is_watchpoint (bpt))
13512 {
13513 /* Initialize it just to avoid a GCC false warning. */
13514 enum enable_state orig_enable_state = bp_disabled;
13515
13516 try
13517 {
13518 struct watchpoint *w = (struct watchpoint *) bpt;
13519
13520 orig_enable_state = bpt->enable_state;
13521 bpt->enable_state = bp_enabled;
13522 update_watchpoint (w, 1 /* reparse */);
13523 }
13524 catch (const gdb_exception &e)
13525 {
13526 bpt->enable_state = orig_enable_state;
13527 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
13528 bpt->number);
13529 return;
13530 }
13531 }
13532
13533 bpt->enable_state = bp_enabled;
13534
13535 /* Mark breakpoint locations modified. */
13536 mark_breakpoint_modified (bpt);
13537
13538 if (target_supports_enable_disable_tracepoint ()
13539 && current_trace_status ()->running && is_tracepoint (bpt))
13540 {
13541 for (bp_location *location : bpt->locations ())
13542 target_enable_tracepoint (location);
13543 }
13544
13545 bpt->disposition = disposition;
13546 bpt->enable_count = count;
13547 update_global_location_list (UGLL_MAY_INSERT);
13548
13549 gdb::observers::breakpoint_modified.notify (bpt);
13550 }
13551
13552
13553 void
13554 enable_breakpoint (struct breakpoint *bpt)
13555 {
13556 enable_breakpoint_disp (bpt, bpt->disposition, 0);
13557 }
13558
13559 /* The enable command enables the specified breakpoints/locations (or
13560 all defined breakpoints) so they once again become (or continue to
13561 be) effective in stopping the inferior. ARGS may be in any of the
13562 forms defined in extract_bp_number_and_location. */
13563
13564 static void
13565 enable_command (const char *args, int from_tty)
13566 {
13567 enable_disable_command (args, from_tty, true);
13568 }
13569
13570 static void
13571 enable_once_command (const char *args, int from_tty)
13572 {
13573 map_breakpoint_numbers
13574 (args, [&] (breakpoint *b)
13575 {
13576 iterate_over_related_breakpoints
13577 (b, [&] (breakpoint *bpt)
13578 {
13579 enable_breakpoint_disp (bpt, disp_disable, 1);
13580 });
13581 });
13582 }
13583
13584 static void
13585 enable_count_command (const char *args, int from_tty)
13586 {
13587 int count;
13588
13589 if (args == NULL)
13590 error_no_arg (_("hit count"));
13591
13592 count = get_number (&args);
13593
13594 map_breakpoint_numbers
13595 (args, [&] (breakpoint *b)
13596 {
13597 iterate_over_related_breakpoints
13598 (b, [&] (breakpoint *bpt)
13599 {
13600 enable_breakpoint_disp (bpt, disp_disable, count);
13601 });
13602 });
13603 }
13604
13605 static void
13606 enable_delete_command (const char *args, int from_tty)
13607 {
13608 map_breakpoint_numbers
13609 (args, [&] (breakpoint *b)
13610 {
13611 iterate_over_related_breakpoints
13612 (b, [&] (breakpoint *bpt)
13613 {
13614 enable_breakpoint_disp (bpt, disp_del, 1);
13615 });
13616 });
13617 }
13618 \f
13619 /* Invalidate last known value of any hardware watchpoint if
13620 the memory which that value represents has been written to by
13621 GDB itself. */
13622
13623 static void
13624 invalidate_bp_value_on_memory_change (struct inferior *inferior,
13625 CORE_ADDR addr, ssize_t len,
13626 const bfd_byte *data)
13627 {
13628 for (breakpoint *bp : all_breakpoints ())
13629 if (bp->enable_state == bp_enabled
13630 && bp->type == bp_hardware_watchpoint)
13631 {
13632 struct watchpoint *wp = (struct watchpoint *) bp;
13633
13634 if (wp->val_valid && wp->val != nullptr)
13635 {
13636 for (bp_location *loc : bp->locations ())
13637 if (loc->loc_type == bp_loc_hardware_watchpoint
13638 && loc->address + loc->length > addr
13639 && addr + len > loc->address)
13640 {
13641 wp->val = NULL;
13642 wp->val_valid = false;
13643 }
13644 }
13645 }
13646 }
13647
13648 /* Create and insert a breakpoint for software single step. */
13649
13650 void
13651 insert_single_step_breakpoint (struct gdbarch *gdbarch,
13652 const address_space *aspace,
13653 CORE_ADDR next_pc)
13654 {
13655 struct thread_info *tp = inferior_thread ();
13656 struct symtab_and_line sal;
13657 CORE_ADDR pc = next_pc;
13658
13659 if (tp->control.single_step_breakpoints == NULL)
13660 {
13661 tp->control.single_step_breakpoints
13662 = new_single_step_breakpoint (tp->global_num, gdbarch);
13663 }
13664
13665 sal = find_pc_line (pc, 0);
13666 sal.pc = pc;
13667 sal.section = find_pc_overlay (pc);
13668 sal.explicit_pc = 1;
13669 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
13670
13671 update_global_location_list (UGLL_INSERT);
13672 }
13673
13674 /* Insert single step breakpoints according to the current state. */
13675
13676 int
13677 insert_single_step_breakpoints (struct gdbarch *gdbarch)
13678 {
13679 struct regcache *regcache = get_current_regcache ();
13680 std::vector<CORE_ADDR> next_pcs;
13681
13682 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
13683
13684 if (!next_pcs.empty ())
13685 {
13686 struct frame_info *frame = get_current_frame ();
13687 const address_space *aspace = get_frame_address_space (frame);
13688
13689 for (CORE_ADDR pc : next_pcs)
13690 insert_single_step_breakpoint (gdbarch, aspace, pc);
13691
13692 return 1;
13693 }
13694 else
13695 return 0;
13696 }
13697
13698 /* See breakpoint.h. */
13699
13700 int
13701 breakpoint_has_location_inserted_here (struct breakpoint *bp,
13702 const address_space *aspace,
13703 CORE_ADDR pc)
13704 {
13705 for (bp_location *loc : bp->locations ())
13706 if (loc->inserted
13707 && breakpoint_location_address_match (loc, aspace, pc))
13708 return 1;
13709
13710 return 0;
13711 }
13712
13713 /* Check whether a software single-step breakpoint is inserted at
13714 PC. */
13715
13716 int
13717 single_step_breakpoint_inserted_here_p (const address_space *aspace,
13718 CORE_ADDR pc)
13719 {
13720 for (breakpoint *bpt : all_breakpoints ())
13721 {
13722 if (bpt->type == bp_single_step
13723 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
13724 return 1;
13725 }
13726 return 0;
13727 }
13728
13729 /* Tracepoint-specific operations. */
13730
13731 /* Set tracepoint count to NUM. */
13732 static void
13733 set_tracepoint_count (int num)
13734 {
13735 tracepoint_count = num;
13736 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
13737 }
13738
13739 static void
13740 trace_command (const char *arg, int from_tty)
13741 {
13742 event_location_up location = string_to_event_location (&arg,
13743 current_language);
13744 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
13745 (location.get (), true /* is_tracepoint */);
13746
13747 create_breakpoint (get_current_arch (),
13748 location.get (),
13749 NULL, 0, arg, false, 1 /* parse arg */,
13750 0 /* tempflag */,
13751 bp_tracepoint /* type_wanted */,
13752 0 /* Ignore count */,
13753 pending_break_support,
13754 ops,
13755 from_tty,
13756 1 /* enabled */,
13757 0 /* internal */, 0);
13758 }
13759
13760 static void
13761 ftrace_command (const char *arg, int from_tty)
13762 {
13763 event_location_up location = string_to_event_location (&arg,
13764 current_language);
13765 create_breakpoint (get_current_arch (),
13766 location.get (),
13767 NULL, 0, arg, false, 1 /* parse arg */,
13768 0 /* tempflag */,
13769 bp_fast_tracepoint /* type_wanted */,
13770 0 /* Ignore count */,
13771 pending_break_support,
13772 &tracepoint_breakpoint_ops,
13773 from_tty,
13774 1 /* enabled */,
13775 0 /* internal */, 0);
13776 }
13777
13778 /* strace command implementation. Creates a static tracepoint. */
13779
13780 static void
13781 strace_command (const char *arg, int from_tty)
13782 {
13783 struct breakpoint_ops *ops;
13784 event_location_up location;
13785
13786 /* Decide if we are dealing with a static tracepoint marker (`-m'),
13787 or with a normal static tracepoint. */
13788 if (arg && startswith (arg, "-m") && isspace (arg[2]))
13789 {
13790 ops = &strace_marker_breakpoint_ops;
13791 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
13792 }
13793 else
13794 {
13795 ops = &tracepoint_breakpoint_ops;
13796 location = string_to_event_location (&arg, current_language);
13797 }
13798
13799 create_breakpoint (get_current_arch (),
13800 location.get (),
13801 NULL, 0, arg, false, 1 /* parse arg */,
13802 0 /* tempflag */,
13803 bp_static_tracepoint /* type_wanted */,
13804 0 /* Ignore count */,
13805 pending_break_support,
13806 ops,
13807 from_tty,
13808 1 /* enabled */,
13809 0 /* internal */, 0);
13810 }
13811
13812 /* Set up a fake reader function that gets command lines from a linked
13813 list that was acquired during tracepoint uploading. */
13814
13815 static struct uploaded_tp *this_utp;
13816 static int next_cmd;
13817
13818 static char *
13819 read_uploaded_action (void)
13820 {
13821 char *rslt = nullptr;
13822
13823 if (next_cmd < this_utp->cmd_strings.size ())
13824 {
13825 rslt = this_utp->cmd_strings[next_cmd].get ();
13826 next_cmd++;
13827 }
13828
13829 return rslt;
13830 }
13831
13832 /* Given information about a tracepoint as recorded on a target (which
13833 can be either a live system or a trace file), attempt to create an
13834 equivalent GDB tracepoint. This is not a reliable process, since
13835 the target does not necessarily have all the information used when
13836 the tracepoint was originally defined. */
13837
13838 struct tracepoint *
13839 create_tracepoint_from_upload (struct uploaded_tp *utp)
13840 {
13841 const char *addr_str;
13842 char small_buf[100];
13843 struct tracepoint *tp;
13844
13845 if (utp->at_string)
13846 addr_str = utp->at_string.get ();
13847 else
13848 {
13849 /* In the absence of a source location, fall back to raw
13850 address. Since there is no way to confirm that the address
13851 means the same thing as when the trace was started, warn the
13852 user. */
13853 warning (_("Uploaded tracepoint %d has no "
13854 "source location, using raw address"),
13855 utp->number);
13856 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
13857 addr_str = small_buf;
13858 }
13859
13860 /* There's not much we can do with a sequence of bytecodes. */
13861 if (utp->cond && !utp->cond_string)
13862 warning (_("Uploaded tracepoint %d condition "
13863 "has no source form, ignoring it"),
13864 utp->number);
13865
13866 event_location_up location = string_to_event_location (&addr_str,
13867 current_language);
13868 if (!create_breakpoint (get_current_arch (),
13869 location.get (),
13870 utp->cond_string.get (), -1, addr_str,
13871 false /* force_condition */,
13872 0 /* parse cond/thread */,
13873 0 /* tempflag */,
13874 utp->type /* type_wanted */,
13875 0 /* Ignore count */,
13876 pending_break_support,
13877 &tracepoint_breakpoint_ops,
13878 0 /* from_tty */,
13879 utp->enabled /* enabled */,
13880 0 /* internal */,
13881 CREATE_BREAKPOINT_FLAGS_INSERTED))
13882 return NULL;
13883
13884 /* Get the tracepoint we just created. */
13885 tp = get_tracepoint (tracepoint_count);
13886 gdb_assert (tp != NULL);
13887
13888 if (utp->pass > 0)
13889 {
13890 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
13891 tp->number);
13892
13893 trace_pass_command (small_buf, 0);
13894 }
13895
13896 /* If we have uploaded versions of the original commands, set up a
13897 special-purpose "reader" function and call the usual command line
13898 reader, then pass the result to the breakpoint command-setting
13899 function. */
13900 if (!utp->cmd_strings.empty ())
13901 {
13902 counted_command_line cmd_list;
13903
13904 this_utp = utp;
13905 next_cmd = 0;
13906
13907 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
13908
13909 breakpoint_set_commands (tp, std::move (cmd_list));
13910 }
13911 else if (!utp->actions.empty ()
13912 || !utp->step_actions.empty ())
13913 warning (_("Uploaded tracepoint %d actions "
13914 "have no source form, ignoring them"),
13915 utp->number);
13916
13917 /* Copy any status information that might be available. */
13918 tp->hit_count = utp->hit_count;
13919 tp->traceframe_usage = utp->traceframe_usage;
13920
13921 return tp;
13922 }
13923
13924 /* Print information on tracepoint number TPNUM_EXP, or all if
13925 omitted. */
13926
13927 static void
13928 info_tracepoints_command (const char *args, int from_tty)
13929 {
13930 struct ui_out *uiout = current_uiout;
13931 int num_printed;
13932
13933 num_printed = breakpoint_1 (args, false, is_tracepoint);
13934
13935 if (num_printed == 0)
13936 {
13937 if (args == NULL || *args == '\0')
13938 uiout->message ("No tracepoints.\n");
13939 else
13940 uiout->message ("No tracepoint matching '%s'.\n", args);
13941 }
13942
13943 default_collect_info ();
13944 }
13945
13946 /* The 'enable trace' command enables tracepoints.
13947 Not supported by all targets. */
13948 static void
13949 enable_trace_command (const char *args, int from_tty)
13950 {
13951 enable_command (args, from_tty);
13952 }
13953
13954 /* The 'disable trace' command disables tracepoints.
13955 Not supported by all targets. */
13956 static void
13957 disable_trace_command (const char *args, int from_tty)
13958 {
13959 disable_command (args, from_tty);
13960 }
13961
13962 /* Remove a tracepoint (or all if no argument). */
13963 static void
13964 delete_trace_command (const char *arg, int from_tty)
13965 {
13966 dont_repeat ();
13967
13968 if (arg == 0)
13969 {
13970 int breaks_to_delete = 0;
13971
13972 /* Delete all breakpoints if no argument.
13973 Do not delete internal or call-dummy breakpoints, these
13974 have to be deleted with an explicit breakpoint number
13975 argument. */
13976 for (breakpoint *tp : all_tracepoints ())
13977 if (is_tracepoint (tp) && user_breakpoint_p (tp))
13978 {
13979 breaks_to_delete = 1;
13980 break;
13981 }
13982
13983 /* Ask user only if there are some breakpoints to delete. */
13984 if (!from_tty
13985 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
13986 {
13987 for (breakpoint *b : all_breakpoints_safe ())
13988 if (is_tracepoint (b) && user_breakpoint_p (b))
13989 delete_breakpoint (b);
13990 }
13991 }
13992 else
13993 map_breakpoint_numbers
13994 (arg, [&] (breakpoint *br)
13995 {
13996 iterate_over_related_breakpoints (br, delete_breakpoint);
13997 });
13998 }
13999
14000 /* Helper function for trace_pass_command. */
14001
14002 static void
14003 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14004 {
14005 tp->pass_count = count;
14006 gdb::observers::breakpoint_modified.notify (tp);
14007 if (from_tty)
14008 gdb_printf (_("Setting tracepoint %d's passcount to %d\n"),
14009 tp->number, count);
14010 }
14011
14012 /* Set passcount for tracepoint.
14013
14014 First command argument is passcount, second is tracepoint number.
14015 If tracepoint number omitted, apply to most recently defined.
14016 Also accepts special argument "all". */
14017
14018 static void
14019 trace_pass_command (const char *args, int from_tty)
14020 {
14021 struct tracepoint *t1;
14022 ULONGEST count;
14023
14024 if (args == 0 || *args == 0)
14025 error (_("passcount command requires an "
14026 "argument (count + optional TP num)"));
14027
14028 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
14029
14030 args = skip_spaces (args);
14031 if (*args && strncasecmp (args, "all", 3) == 0)
14032 {
14033 args += 3; /* Skip special argument "all". */
14034 if (*args)
14035 error (_("Junk at end of arguments."));
14036
14037 for (breakpoint *b : all_tracepoints ())
14038 {
14039 t1 = (struct tracepoint *) b;
14040 trace_pass_set_count (t1, count, from_tty);
14041 }
14042 }
14043 else if (*args == '\0')
14044 {
14045 t1 = get_tracepoint_by_number (&args, NULL);
14046 if (t1)
14047 trace_pass_set_count (t1, count, from_tty);
14048 }
14049 else
14050 {
14051 number_or_range_parser parser (args);
14052 while (!parser.finished ())
14053 {
14054 t1 = get_tracepoint_by_number (&args, &parser);
14055 if (t1)
14056 trace_pass_set_count (t1, count, from_tty);
14057 }
14058 }
14059 }
14060
14061 struct tracepoint *
14062 get_tracepoint (int num)
14063 {
14064 for (breakpoint *t : all_tracepoints ())
14065 if (t->number == num)
14066 return (struct tracepoint *) t;
14067
14068 return NULL;
14069 }
14070
14071 /* Find the tracepoint with the given target-side number (which may be
14072 different from the tracepoint number after disconnecting and
14073 reconnecting). */
14074
14075 struct tracepoint *
14076 get_tracepoint_by_number_on_target (int num)
14077 {
14078 for (breakpoint *b : all_tracepoints ())
14079 {
14080 struct tracepoint *t = (struct tracepoint *) b;
14081
14082 if (t->number_on_target == num)
14083 return t;
14084 }
14085
14086 return NULL;
14087 }
14088
14089 /* Utility: parse a tracepoint number and look it up in the list.
14090 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
14091 If the argument is missing, the most recent tracepoint
14092 (tracepoint_count) is returned. */
14093
14094 struct tracepoint *
14095 get_tracepoint_by_number (const char **arg,
14096 number_or_range_parser *parser)
14097 {
14098 int tpnum;
14099 const char *instring = arg == NULL ? NULL : *arg;
14100
14101 if (parser != NULL)
14102 {
14103 gdb_assert (!parser->finished ());
14104 tpnum = parser->get_number ();
14105 }
14106 else if (arg == NULL || *arg == NULL || ! **arg)
14107 tpnum = tracepoint_count;
14108 else
14109 tpnum = get_number (arg);
14110
14111 if (tpnum <= 0)
14112 {
14113 if (instring && *instring)
14114 gdb_printf (_("bad tracepoint number at or near '%s'\n"),
14115 instring);
14116 else
14117 gdb_printf (_("No previous tracepoint\n"));
14118 return NULL;
14119 }
14120
14121 for (breakpoint *t : all_tracepoints ())
14122 if (t->number == tpnum)
14123 return (struct tracepoint *) t;
14124
14125 gdb_printf ("No tracepoint number %d.\n", tpnum);
14126 return NULL;
14127 }
14128
14129 void
14130 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
14131 {
14132 if (b->thread != -1)
14133 gdb_printf (fp, " thread %d", b->thread);
14134
14135 if (b->task != 0)
14136 gdb_printf (fp, " task %d", b->task);
14137
14138 gdb_printf (fp, "\n");
14139 }
14140
14141 /* Save information on user settable breakpoints (watchpoints, etc) to
14142 a new script file named FILENAME. If FILTER is non-NULL, call it
14143 on each breakpoint and only include the ones for which it returns
14144 true. */
14145
14146 static void
14147 save_breakpoints (const char *filename, int from_tty,
14148 bool (*filter) (const struct breakpoint *))
14149 {
14150 int any = 0;
14151 int extra_trace_bits = 0;
14152
14153 if (filename == 0 || *filename == 0)
14154 error (_("Argument required (file name in which to save)"));
14155
14156 /* See if we have anything to save. */
14157 for (breakpoint *tp : all_breakpoints ())
14158 {
14159 /* Skip internal and momentary breakpoints. */
14160 if (!user_breakpoint_p (tp))
14161 continue;
14162
14163 /* If we have a filter, only save the breakpoints it accepts. */
14164 if (filter && !filter (tp))
14165 continue;
14166
14167 any = 1;
14168
14169 if (is_tracepoint (tp))
14170 {
14171 extra_trace_bits = 1;
14172
14173 /* We can stop searching. */
14174 break;
14175 }
14176 }
14177
14178 if (!any)
14179 {
14180 warning (_("Nothing to save."));
14181 return;
14182 }
14183
14184 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
14185
14186 stdio_file fp;
14187
14188 if (!fp.open (expanded_filename.get (), "w"))
14189 error (_("Unable to open file '%s' for saving (%s)"),
14190 expanded_filename.get (), safe_strerror (errno));
14191
14192 if (extra_trace_bits)
14193 save_trace_state_variables (&fp);
14194
14195 for (breakpoint *tp : all_breakpoints ())
14196 {
14197 /* Skip internal and momentary breakpoints. */
14198 if (!user_breakpoint_p (tp))
14199 continue;
14200
14201 /* If we have a filter, only save the breakpoints it accepts. */
14202 if (filter && !filter (tp))
14203 continue;
14204
14205 tp->ops->print_recreate (tp, &fp);
14206
14207 /* Note, we can't rely on tp->number for anything, as we can't
14208 assume the recreated breakpoint numbers will match. Use $bpnum
14209 instead. */
14210
14211 if (tp->cond_string)
14212 fp.printf (" condition $bpnum %s\n", tp->cond_string.get ());
14213
14214 if (tp->ignore_count)
14215 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
14216
14217 if (tp->type != bp_dprintf && tp->commands)
14218 {
14219 fp.puts (" commands\n");
14220
14221 current_uiout->redirect (&fp);
14222 try
14223 {
14224 print_command_lines (current_uiout, tp->commands.get (), 2);
14225 }
14226 catch (const gdb_exception &ex)
14227 {
14228 current_uiout->redirect (NULL);
14229 throw;
14230 }
14231
14232 current_uiout->redirect (NULL);
14233 fp.puts (" end\n");
14234 }
14235
14236 if (tp->enable_state == bp_disabled)
14237 fp.puts ("disable $bpnum\n");
14238
14239 /* If this is a multi-location breakpoint, check if the locations
14240 should be individually disabled. Watchpoint locations are
14241 special, and not user visible. */
14242 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
14243 {
14244 int n = 1;
14245
14246 for (bp_location *loc : tp->locations ())
14247 {
14248 if (!loc->enabled)
14249 fp.printf ("disable $bpnum.%d\n", n);
14250
14251 n++;
14252 }
14253 }
14254 }
14255
14256 if (extra_trace_bits && !default_collect.empty ())
14257 fp.printf ("set default-collect %s\n", default_collect.c_str ());
14258
14259 if (from_tty)
14260 gdb_printf (_("Saved to file '%s'.\n"), expanded_filename.get ());
14261 }
14262
14263 /* The `save breakpoints' command. */
14264
14265 static void
14266 save_breakpoints_command (const char *args, int from_tty)
14267 {
14268 save_breakpoints (args, from_tty, NULL);
14269 }
14270
14271 /* The `save tracepoints' command. */
14272
14273 static void
14274 save_tracepoints_command (const char *args, int from_tty)
14275 {
14276 save_breakpoints (args, from_tty, is_tracepoint);
14277 }
14278
14279 \f
14280 /* This help string is used to consolidate all the help string for specifying
14281 locations used by several commands. */
14282
14283 #define LOCATION_HELP_STRING \
14284 "Linespecs are colon-separated lists of location parameters, such as\n\
14285 source filename, function name, label name, and line number.\n\
14286 Example: To specify the start of a label named \"the_top\" in the\n\
14287 function \"fact\" in the file \"factorial.c\", use\n\
14288 \"factorial.c:fact:the_top\".\n\
14289 \n\
14290 Address locations begin with \"*\" and specify an exact address in the\n\
14291 program. Example: To specify the fourth byte past the start function\n\
14292 \"main\", use \"*main + 4\".\n\
14293 \n\
14294 Explicit locations are similar to linespecs but use an option/argument\n\
14295 syntax to specify location parameters.\n\
14296 Example: To specify the start of the label named \"the_top\" in the\n\
14297 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
14298 -function fact -label the_top\".\n\
14299 \n\
14300 By default, a specified function is matched against the program's\n\
14301 functions in all scopes. For C++, this means in all namespaces and\n\
14302 classes. For Ada, this means in all packages. E.g., in C++,\n\
14303 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
14304 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
14305 specified name as a complete fully-qualified name instead."
14306
14307 /* This help string is used for the break, hbreak, tbreak and thbreak
14308 commands. It is defined as a macro to prevent duplication.
14309 COMMAND should be a string constant containing the name of the
14310 command. */
14311
14312 #define BREAK_ARGS_HELP(command) \
14313 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
14314 \t[-force-condition] [if CONDITION]\n\
14315 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
14316 probe point. Accepted values are `-probe' (for a generic, automatically\n\
14317 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
14318 `-probe-dtrace' (for a DTrace probe).\n\
14319 LOCATION may be a linespec, address, or explicit location as described\n\
14320 below.\n\
14321 \n\
14322 With no LOCATION, uses current execution address of the selected\n\
14323 stack frame. This is useful for breaking on return to a stack frame.\n\
14324 \n\
14325 THREADNUM is the number from \"info threads\".\n\
14326 CONDITION is a boolean expression.\n\
14327 \n\
14328 With the \"-force-condition\" flag, the condition is defined even when\n\
14329 it is invalid for all current locations.\n\
14330 \n" LOCATION_HELP_STRING "\n\n\
14331 Multiple breakpoints at one place are permitted, and useful if their\n\
14332 conditions are different.\n\
14333 \n\
14334 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
14335
14336 /* List of subcommands for "catch". */
14337 static struct cmd_list_element *catch_cmdlist;
14338
14339 /* List of subcommands for "tcatch". */
14340 static struct cmd_list_element *tcatch_cmdlist;
14341
14342 void
14343 add_catch_command (const char *name, const char *docstring,
14344 cmd_func_ftype *func,
14345 completer_ftype *completer,
14346 void *user_data_catch,
14347 void *user_data_tcatch)
14348 {
14349 struct cmd_list_element *command;
14350
14351 command = add_cmd (name, class_breakpoint, docstring,
14352 &catch_cmdlist);
14353 command->func = func;
14354 command->set_context (user_data_catch);
14355 set_cmd_completer (command, completer);
14356
14357 command = add_cmd (name, class_breakpoint, docstring,
14358 &tcatch_cmdlist);
14359 command->func = func;
14360 command->set_context (user_data_tcatch);
14361 set_cmd_completer (command, completer);
14362 }
14363
14364 /* Zero if any of the breakpoint's locations could be a location where
14365 functions have been inlined, nonzero otherwise. */
14366
14367 static int
14368 is_non_inline_function (struct breakpoint *b)
14369 {
14370 /* The shared library event breakpoint is set on the address of a
14371 non-inline function. */
14372 if (b->type == bp_shlib_event)
14373 return 1;
14374
14375 return 0;
14376 }
14377
14378 /* Nonzero if the specified PC cannot be a location where functions
14379 have been inlined. */
14380
14381 int
14382 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
14383 const target_waitstatus &ws)
14384 {
14385 for (breakpoint *b : all_breakpoints ())
14386 {
14387 if (!is_non_inline_function (b))
14388 continue;
14389
14390 for (bp_location *bl : b->locations ())
14391 {
14392 if (!bl->shlib_disabled
14393 && bpstat_check_location (bl, aspace, pc, ws))
14394 return 1;
14395 }
14396 }
14397
14398 return 0;
14399 }
14400
14401 /* Remove any references to OBJFILE which is going to be freed. */
14402
14403 void
14404 breakpoint_free_objfile (struct objfile *objfile)
14405 {
14406 for (bp_location *loc : all_bp_locations ())
14407 if (loc->symtab != NULL && loc->symtab->compunit ()->objfile () == objfile)
14408 loc->symtab = NULL;
14409 }
14410
14411 void
14412 initialize_breakpoint_ops (void)
14413 {
14414 static int initialized = 0;
14415
14416 struct breakpoint_ops *ops;
14417
14418 if (initialized)
14419 return;
14420 initialized = 1;
14421
14422 /* The breakpoint_ops structure to be inherit by all kinds of
14423 breakpoints (real breakpoints, i.e., user "break" breakpoints,
14424 internal and momentary breakpoints, etc.). */
14425 ops = &bkpt_base_breakpoint_ops;
14426 *ops = base_breakpoint_ops;
14427 ops->re_set = bkpt_re_set;
14428 ops->insert_location = bkpt_insert_location;
14429 ops->remove_location = bkpt_remove_location;
14430 ops->breakpoint_hit = bkpt_breakpoint_hit;
14431 ops->create_sals_from_location = create_sals_from_location_default;
14432 ops->create_breakpoints_sal = create_breakpoints_sal_default;
14433 ops->decode_location = bkpt_decode_location;
14434
14435 /* The breakpoint_ops structure to be used in regular breakpoints. */
14436 ops = &bkpt_breakpoint_ops;
14437 *ops = bkpt_base_breakpoint_ops;
14438 ops->re_set = bkpt_re_set;
14439 ops->resources_needed = bkpt_resources_needed;
14440 ops->print_it = bkpt_print_it;
14441 ops->print_mention = bkpt_print_mention;
14442 ops->print_recreate = bkpt_print_recreate;
14443
14444 /* Ranged breakpoints. */
14445 ops = &ranged_breakpoint_ops;
14446 *ops = bkpt_breakpoint_ops;
14447 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
14448 ops->resources_needed = resources_needed_ranged_breakpoint;
14449 ops->print_it = print_it_ranged_breakpoint;
14450 ops->print_one = print_one_ranged_breakpoint;
14451 ops->print_one_detail = print_one_detail_ranged_breakpoint;
14452 ops->print_mention = print_mention_ranged_breakpoint;
14453 ops->print_recreate = print_recreate_ranged_breakpoint;
14454
14455 /* Internal breakpoints. */
14456 ops = &internal_breakpoint_ops;
14457 *ops = bkpt_base_breakpoint_ops;
14458 ops->re_set = internal_bkpt_re_set;
14459 ops->check_status = internal_bkpt_check_status;
14460 ops->print_it = internal_bkpt_print_it;
14461 ops->print_mention = internal_bkpt_print_mention;
14462
14463 /* Momentary breakpoints. */
14464 ops = &momentary_breakpoint_ops;
14465 *ops = bkpt_base_breakpoint_ops;
14466 ops->re_set = momentary_bkpt_re_set;
14467 ops->check_status = momentary_bkpt_check_status;
14468 ops->print_it = momentary_bkpt_print_it;
14469 ops->print_mention = momentary_bkpt_print_mention;
14470
14471 /* Probe breakpoints. */
14472 ops = &bkpt_probe_breakpoint_ops;
14473 *ops = bkpt_breakpoint_ops;
14474 ops->insert_location = bkpt_probe_insert_location;
14475 ops->remove_location = bkpt_probe_remove_location;
14476 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
14477 ops->decode_location = bkpt_probe_decode_location;
14478
14479 /* Watchpoints. */
14480 ops = &watchpoint_breakpoint_ops;
14481 *ops = base_breakpoint_ops;
14482 ops->re_set = re_set_watchpoint;
14483 ops->insert_location = insert_watchpoint;
14484 ops->remove_location = remove_watchpoint;
14485 ops->breakpoint_hit = breakpoint_hit_watchpoint;
14486 ops->check_status = check_status_watchpoint;
14487 ops->resources_needed = resources_needed_watchpoint;
14488 ops->works_in_software_mode = works_in_software_mode_watchpoint;
14489 ops->print_it = print_it_watchpoint;
14490 ops->print_mention = print_mention_watchpoint;
14491 ops->print_recreate = print_recreate_watchpoint;
14492 ops->explains_signal = explains_signal_watchpoint;
14493
14494 /* Masked watchpoints. */
14495 ops = &masked_watchpoint_breakpoint_ops;
14496 *ops = watchpoint_breakpoint_ops;
14497 ops->insert_location = insert_masked_watchpoint;
14498 ops->remove_location = remove_masked_watchpoint;
14499 ops->resources_needed = resources_needed_masked_watchpoint;
14500 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
14501 ops->print_it = print_it_masked_watchpoint;
14502 ops->print_one_detail = print_one_detail_masked_watchpoint;
14503 ops->print_mention = print_mention_masked_watchpoint;
14504 ops->print_recreate = print_recreate_masked_watchpoint;
14505
14506 /* Tracepoints. */
14507 ops = &tracepoint_breakpoint_ops;
14508 *ops = base_breakpoint_ops;
14509 ops->re_set = tracepoint_re_set;
14510 ops->breakpoint_hit = tracepoint_breakpoint_hit;
14511 ops->print_one_detail = tracepoint_print_one_detail;
14512 ops->print_mention = tracepoint_print_mention;
14513 ops->print_recreate = tracepoint_print_recreate;
14514 ops->create_sals_from_location = create_sals_from_location_default;
14515 ops->create_breakpoints_sal = create_breakpoints_sal_default;
14516 ops->decode_location = tracepoint_decode_location;
14517
14518 /* Probe tracepoints. */
14519 ops = &tracepoint_probe_breakpoint_ops;
14520 *ops = tracepoint_breakpoint_ops;
14521 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
14522 ops->decode_location = tracepoint_probe_decode_location;
14523
14524 /* Static tracepoints with marker (`-m'). */
14525 ops = &strace_marker_breakpoint_ops;
14526 *ops = tracepoint_breakpoint_ops;
14527 ops->create_sals_from_location = strace_marker_create_sals_from_location;
14528 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
14529 ops->decode_location = strace_marker_decode_location;
14530
14531 ops = &dprintf_breakpoint_ops;
14532 *ops = bkpt_base_breakpoint_ops;
14533 ops->re_set = dprintf_re_set;
14534 ops->resources_needed = bkpt_resources_needed;
14535 ops->print_it = bkpt_print_it;
14536 ops->print_mention = bkpt_print_mention;
14537 ops->print_recreate = dprintf_print_recreate;
14538 ops->after_condition_true = dprintf_after_condition_true;
14539 ops->breakpoint_hit = dprintf_breakpoint_hit;
14540 }
14541
14542 /* Chain containing all defined "enable breakpoint" subcommands. */
14543
14544 static struct cmd_list_element *enablebreaklist = NULL;
14545
14546 /* See breakpoint.h. */
14547
14548 cmd_list_element *commands_cmd_element = nullptr;
14549
14550 void _initialize_breakpoint ();
14551 void
14552 _initialize_breakpoint ()
14553 {
14554 struct cmd_list_element *c;
14555
14556 initialize_breakpoint_ops ();
14557
14558 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
14559 "breakpoint");
14560 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
14561 "breakpoint");
14562 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
14563 "breakpoint");
14564
14565 breakpoint_chain = 0;
14566 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
14567 before a breakpoint is set. */
14568 breakpoint_count = 0;
14569
14570 tracepoint_count = 0;
14571
14572 add_com ("ignore", class_breakpoint, ignore_command, _("\
14573 Set ignore-count of breakpoint number N to COUNT.\n\
14574 Usage is `ignore N COUNT'."));
14575
14576 commands_cmd_element = add_com ("commands", class_breakpoint,
14577 commands_command, _("\
14578 Set commands to be executed when the given breakpoints are hit.\n\
14579 Give a space-separated breakpoint list as argument after \"commands\".\n\
14580 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
14581 (e.g. `5-7').\n\
14582 With no argument, the targeted breakpoint is the last one set.\n\
14583 The commands themselves follow starting on the next line.\n\
14584 Type a line containing \"end\" to indicate the end of them.\n\
14585 Give \"silent\" as the first line to make the breakpoint silent;\n\
14586 then no output is printed when it is hit, except what the commands print."));
14587
14588 const auto cc_opts = make_condition_command_options_def_group (nullptr);
14589 static std::string condition_command_help
14590 = gdb::option::build_help (_("\
14591 Specify breakpoint number N to break only if COND is true.\n\
14592 Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
14593 is an expression to be evaluated whenever breakpoint N is reached.\n\
14594 \n\
14595 Options:\n\
14596 %OPTIONS%"), cc_opts);
14597
14598 c = add_com ("condition", class_breakpoint, condition_command,
14599 condition_command_help.c_str ());
14600 set_cmd_completer_handle_brkchars (c, condition_completer);
14601
14602 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
14603 Set a temporary breakpoint.\n\
14604 Like \"break\" except the breakpoint is only temporary,\n\
14605 so it will be deleted when hit. Equivalent to \"break\" followed\n\
14606 by using \"enable delete\" on the breakpoint number.\n\
14607 \n"
14608 BREAK_ARGS_HELP ("tbreak")));
14609 set_cmd_completer (c, location_completer);
14610
14611 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
14612 Set a hardware assisted breakpoint.\n\
14613 Like \"break\" except the breakpoint requires hardware support,\n\
14614 some target hardware may not have this support.\n\
14615 \n"
14616 BREAK_ARGS_HELP ("hbreak")));
14617 set_cmd_completer (c, location_completer);
14618
14619 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
14620 Set a temporary hardware assisted breakpoint.\n\
14621 Like \"hbreak\" except the breakpoint is only temporary,\n\
14622 so it will be deleted when hit.\n\
14623 \n"
14624 BREAK_ARGS_HELP ("thbreak")));
14625 set_cmd_completer (c, location_completer);
14626
14627 cmd_list_element *enable_cmd
14628 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
14629 Enable all or some breakpoints.\n\
14630 Usage: enable [BREAKPOINTNUM]...\n\
14631 Give breakpoint numbers (separated by spaces) as arguments.\n\
14632 With no subcommand, breakpoints are enabled until you command otherwise.\n\
14633 This is used to cancel the effect of the \"disable\" command.\n\
14634 With a subcommand you can enable temporarily."),
14635 &enablelist, 1, &cmdlist);
14636
14637 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
14638
14639 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
14640 Enable all or some breakpoints.\n\
14641 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
14642 Give breakpoint numbers (separated by spaces) as arguments.\n\
14643 This is used to cancel the effect of the \"disable\" command.\n\
14644 May be abbreviated to simply \"enable\"."),
14645 &enablebreaklist, 1, &enablelist);
14646
14647 add_cmd ("once", no_class, enable_once_command, _("\
14648 Enable some breakpoints for one hit.\n\
14649 Usage: enable breakpoints once BREAKPOINTNUM...\n\
14650 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
14651 &enablebreaklist);
14652
14653 add_cmd ("delete", no_class, enable_delete_command, _("\
14654 Enable some breakpoints and delete when hit.\n\
14655 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
14656 If a breakpoint is hit while enabled in this fashion, it is deleted."),
14657 &enablebreaklist);
14658
14659 add_cmd ("count", no_class, enable_count_command, _("\
14660 Enable some breakpoints for COUNT hits.\n\
14661 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
14662 If a breakpoint is hit while enabled in this fashion,\n\
14663 the count is decremented; when it reaches zero, the breakpoint is disabled."),
14664 &enablebreaklist);
14665
14666 add_cmd ("delete", no_class, enable_delete_command, _("\
14667 Enable some breakpoints and delete when hit.\n\
14668 Usage: enable delete BREAKPOINTNUM...\n\
14669 If a breakpoint is hit while enabled in this fashion, it is deleted."),
14670 &enablelist);
14671
14672 add_cmd ("once", no_class, enable_once_command, _("\
14673 Enable some breakpoints for one hit.\n\
14674 Usage: enable once BREAKPOINTNUM...\n\
14675 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
14676 &enablelist);
14677
14678 add_cmd ("count", no_class, enable_count_command, _("\
14679 Enable some breakpoints for COUNT hits.\n\
14680 Usage: enable count COUNT BREAKPOINTNUM...\n\
14681 If a breakpoint is hit while enabled in this fashion,\n\
14682 the count is decremented; when it reaches zero, the breakpoint is disabled."),
14683 &enablelist);
14684
14685 cmd_list_element *disable_cmd
14686 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
14687 Disable all or some breakpoints.\n\
14688 Usage: disable [BREAKPOINTNUM]...\n\
14689 Arguments are breakpoint numbers with spaces in between.\n\
14690 To disable all breakpoints, give no argument.\n\
14691 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
14692 &disablelist, 1, &cmdlist);
14693 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
14694 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
14695
14696 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
14697 Disable all or some breakpoints.\n\
14698 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
14699 Arguments are breakpoint numbers with spaces in between.\n\
14700 To disable all breakpoints, give no argument.\n\
14701 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
14702 This command may be abbreviated \"disable\"."),
14703 &disablelist);
14704
14705 cmd_list_element *delete_cmd
14706 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
14707 Delete all or some breakpoints.\n\
14708 Usage: delete [BREAKPOINTNUM]...\n\
14709 Arguments are breakpoint numbers with spaces in between.\n\
14710 To delete all breakpoints, give no argument.\n\
14711 \n\
14712 Also a prefix command for deletion of other GDB objects."),
14713 &deletelist, 1, &cmdlist);
14714 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
14715 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
14716
14717 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
14718 Delete all or some breakpoints or auto-display expressions.\n\
14719 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
14720 Arguments are breakpoint numbers with spaces in between.\n\
14721 To delete all breakpoints, give no argument.\n\
14722 This command may be abbreviated \"delete\"."),
14723 &deletelist);
14724
14725 cmd_list_element *clear_cmd
14726 = add_com ("clear", class_breakpoint, clear_command, _("\
14727 Clear breakpoint at specified location.\n\
14728 Argument may be a linespec, explicit, or address location as described below.\n\
14729 \n\
14730 With no argument, clears all breakpoints in the line that the selected frame\n\
14731 is executing in.\n"
14732 "\n" LOCATION_HELP_STRING "\n\n\
14733 See also the \"delete\" command which clears breakpoints by number."));
14734 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
14735
14736 cmd_list_element *break_cmd
14737 = add_com ("break", class_breakpoint, break_command, _("\
14738 Set breakpoint at specified location.\n"
14739 BREAK_ARGS_HELP ("break")));
14740 set_cmd_completer (break_cmd, location_completer);
14741
14742 add_com_alias ("b", break_cmd, class_run, 1);
14743 add_com_alias ("br", break_cmd, class_run, 1);
14744 add_com_alias ("bre", break_cmd, class_run, 1);
14745 add_com_alias ("brea", break_cmd, class_run, 1);
14746
14747 cmd_list_element *info_breakpoints_cmd
14748 = add_info ("breakpoints", info_breakpoints_command, _("\
14749 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
14750 The \"Type\" column indicates one of:\n\
14751 \tbreakpoint - normal breakpoint\n\
14752 \twatchpoint - watchpoint\n\
14753 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14754 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14755 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
14756 address and file/line number respectively.\n\
14757 \n\
14758 Convenience variable \"$_\" and default examine address for \"x\"\n\
14759 are set to the address of the last breakpoint listed unless the command\n\
14760 is prefixed with \"server \".\n\n\
14761 Convenience variable \"$bpnum\" contains the number of the last\n\
14762 breakpoint set."));
14763
14764 add_info_alias ("b", info_breakpoints_cmd, 1);
14765
14766 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
14767 Status of all breakpoints, or breakpoint number NUMBER.\n\
14768 The \"Type\" column indicates one of:\n\
14769 \tbreakpoint - normal breakpoint\n\
14770 \twatchpoint - watchpoint\n\
14771 \tlongjmp - internal breakpoint used to step through longjmp()\n\
14772 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
14773 \tuntil - internal breakpoint used by the \"until\" command\n\
14774 \tfinish - internal breakpoint used by the \"finish\" command\n\
14775 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14776 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14777 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
14778 address and file/line number respectively.\n\
14779 \n\
14780 Convenience variable \"$_\" and default examine address for \"x\"\n\
14781 are set to the address of the last breakpoint listed unless the command\n\
14782 is prefixed with \"server \".\n\n\
14783 Convenience variable \"$bpnum\" contains the number of the last\n\
14784 breakpoint set."),
14785 &maintenanceinfolist);
14786
14787 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
14788 Set catchpoints to catch events."),
14789 &catch_cmdlist,
14790 0/*allow-unknown*/, &cmdlist);
14791
14792 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
14793 Set temporary catchpoints to catch events."),
14794 &tcatch_cmdlist,
14795 0/*allow-unknown*/, &cmdlist);
14796
14797 const auto opts = make_watch_options_def_group (nullptr);
14798
14799 static const std::string watch_help = gdb::option::build_help (_("\
14800 Set a watchpoint for EXPRESSION.\n\
14801 Usage: watch [-location] EXPRESSION\n\
14802 \n\
14803 Options:\n\
14804 %OPTIONS%\n\
14805 \n\
14806 A watchpoint stops execution of your program whenever the value of\n\
14807 an expression changes."), opts);
14808 c = add_com ("watch", class_breakpoint, watch_command,
14809 watch_help.c_str ());
14810 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14811
14812 static const std::string rwatch_help = gdb::option::build_help (_("\
14813 Set a read watchpoint for EXPRESSION.\n\
14814 Usage: rwatch [-location] EXPRESSION\n\
14815 \n\
14816 Options:\n\
14817 %OPTIONS%\n\
14818 \n\
14819 A read watchpoint stops execution of your program whenever the value of\n\
14820 an expression is read."), opts);
14821 c = add_com ("rwatch", class_breakpoint, rwatch_command,
14822 rwatch_help.c_str ());
14823 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14824
14825 static const std::string awatch_help = gdb::option::build_help (_("\
14826 Set an access watchpoint for EXPRESSION.\n\
14827 Usage: awatch [-location] EXPRESSION\n\
14828 \n\
14829 Options:\n\
14830 %OPTIONS%\n\
14831 \n\
14832 An access watchpoint stops execution of your program whenever the value\n\
14833 of an expression is either read or written."), opts);
14834 c = add_com ("awatch", class_breakpoint, awatch_command,
14835 awatch_help.c_str ());
14836 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14837
14838 add_info ("watchpoints", info_watchpoints_command, _("\
14839 Status of specified watchpoints (all watchpoints if no argument)."));
14840
14841 /* XXX: cagney/2005-02-23: This should be a boolean, and should
14842 respond to changes - contrary to the description. */
14843 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
14844 &can_use_hw_watchpoints, _("\
14845 Set debugger's willingness to use watchpoint hardware."), _("\
14846 Show debugger's willingness to use watchpoint hardware."), _("\
14847 If zero, gdb will not use hardware for new watchpoints, even if\n\
14848 such is available. (However, any hardware watchpoints that were\n\
14849 created before setting this to nonzero, will continue to use watchpoint\n\
14850 hardware.)"),
14851 NULL,
14852 show_can_use_hw_watchpoints,
14853 &setlist, &showlist);
14854
14855 can_use_hw_watchpoints = 1;
14856
14857 /* Tracepoint manipulation commands. */
14858
14859 cmd_list_element *trace_cmd
14860 = add_com ("trace", class_breakpoint, trace_command, _("\
14861 Set a tracepoint at specified location.\n\
14862 \n"
14863 BREAK_ARGS_HELP ("trace") "\n\
14864 Do \"help tracepoints\" for info on other tracepoint commands."));
14865 set_cmd_completer (trace_cmd, location_completer);
14866
14867 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
14868 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
14869 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
14870 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
14871
14872 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
14873 Set a fast tracepoint at specified location.\n\
14874 \n"
14875 BREAK_ARGS_HELP ("ftrace") "\n\
14876 Do \"help tracepoints\" for info on other tracepoint commands."));
14877 set_cmd_completer (c, location_completer);
14878
14879 c = add_com ("strace", class_breakpoint, strace_command, _("\
14880 Set a static tracepoint at location or marker.\n\
14881 \n\
14882 strace [LOCATION] [if CONDITION]\n\
14883 LOCATION may be a linespec, explicit, or address location (described below) \n\
14884 or -m MARKER_ID.\n\n\
14885 If a marker id is specified, probe the marker with that name. With\n\
14886 no LOCATION, uses current execution address of the selected stack frame.\n\
14887 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
14888 This collects arbitrary user data passed in the probe point call to the\n\
14889 tracing library. You can inspect it when analyzing the trace buffer,\n\
14890 by printing the $_sdata variable like any other convenience variable.\n\
14891 \n\
14892 CONDITION is a boolean expression.\n\
14893 \n" LOCATION_HELP_STRING "\n\n\
14894 Multiple tracepoints at one place are permitted, and useful if their\n\
14895 conditions are different.\n\
14896 \n\
14897 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
14898 Do \"help tracepoints\" for info on other tracepoint commands."));
14899 set_cmd_completer (c, location_completer);
14900
14901 cmd_list_element *info_tracepoints_cmd
14902 = add_info ("tracepoints", info_tracepoints_command, _("\
14903 Status of specified tracepoints (all tracepoints if no argument).\n\
14904 Convenience variable \"$tpnum\" contains the number of the\n\
14905 last tracepoint set."));
14906
14907 add_info_alias ("tp", info_tracepoints_cmd, 1);
14908
14909 cmd_list_element *delete_tracepoints_cmd
14910 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
14911 Delete specified tracepoints.\n\
14912 Arguments are tracepoint numbers, separated by spaces.\n\
14913 No argument means delete all tracepoints."),
14914 &deletelist);
14915 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
14916
14917 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
14918 Disable specified tracepoints.\n\
14919 Arguments are tracepoint numbers, separated by spaces.\n\
14920 No argument means disable all tracepoints."),
14921 &disablelist);
14922 deprecate_cmd (c, "disable");
14923
14924 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
14925 Enable specified tracepoints.\n\
14926 Arguments are tracepoint numbers, separated by spaces.\n\
14927 No argument means enable all tracepoints."),
14928 &enablelist);
14929 deprecate_cmd (c, "enable");
14930
14931 add_com ("passcount", class_trace, trace_pass_command, _("\
14932 Set the passcount for a tracepoint.\n\
14933 The trace will end when the tracepoint has been passed 'count' times.\n\
14934 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
14935 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
14936
14937 add_basic_prefix_cmd ("save", class_breakpoint,
14938 _("Save breakpoint definitions as a script."),
14939 &save_cmdlist,
14940 0/*allow-unknown*/, &cmdlist);
14941
14942 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
14943 Save current breakpoint definitions as a script.\n\
14944 This includes all types of breakpoints (breakpoints, watchpoints,\n\
14945 catchpoints, tracepoints). Use the 'source' command in another debug\n\
14946 session to restore them."),
14947 &save_cmdlist);
14948 set_cmd_completer (c, filename_completer);
14949
14950 cmd_list_element *save_tracepoints_cmd
14951 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
14952 Save current tracepoint definitions as a script.\n\
14953 Use the 'source' command in another debug session to restore them."),
14954 &save_cmdlist);
14955 set_cmd_completer (save_tracepoints_cmd, filename_completer);
14956
14957 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
14958 deprecate_cmd (c, "save tracepoints");
14959
14960 add_setshow_prefix_cmd ("breakpoint", class_maintenance,
14961 _("\
14962 Breakpoint specific settings.\n\
14963 Configure various breakpoint-specific variables such as\n\
14964 pending breakpoint behavior."),
14965 _("\
14966 Breakpoint specific settings.\n\
14967 Configure various breakpoint-specific variables such as\n\
14968 pending breakpoint behavior."),
14969 &breakpoint_set_cmdlist, &breakpoint_show_cmdlist,
14970 &setlist, &showlist);
14971
14972 add_setshow_auto_boolean_cmd ("pending", no_class,
14973 &pending_break_support, _("\
14974 Set debugger's behavior regarding pending breakpoints."), _("\
14975 Show debugger's behavior regarding pending breakpoints."), _("\
14976 If on, an unrecognized breakpoint location will cause gdb to create a\n\
14977 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
14978 an error. If auto, an unrecognized breakpoint location results in a\n\
14979 user-query to see if a pending breakpoint should be created."),
14980 NULL,
14981 show_pending_break_support,
14982 &breakpoint_set_cmdlist,
14983 &breakpoint_show_cmdlist);
14984
14985 pending_break_support = AUTO_BOOLEAN_AUTO;
14986
14987 add_setshow_boolean_cmd ("auto-hw", no_class,
14988 &automatic_hardware_breakpoints, _("\
14989 Set automatic usage of hardware breakpoints."), _("\
14990 Show automatic usage of hardware breakpoints."), _("\
14991 If set, the debugger will automatically use hardware breakpoints for\n\
14992 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
14993 a warning will be emitted for such breakpoints."),
14994 NULL,
14995 show_automatic_hardware_breakpoints,
14996 &breakpoint_set_cmdlist,
14997 &breakpoint_show_cmdlist);
14998
14999 add_setshow_boolean_cmd ("always-inserted", class_support,
15000 &always_inserted_mode, _("\
15001 Set mode for inserting breakpoints."), _("\
15002 Show mode for inserting breakpoints."), _("\
15003 When this mode is on, breakpoints are inserted immediately as soon as\n\
15004 they're created, kept inserted even when execution stops, and removed\n\
15005 only when the user deletes them. When this mode is off (the default),\n\
15006 breakpoints are inserted only when execution continues, and removed\n\
15007 when execution stops."),
15008 NULL,
15009 &show_always_inserted_mode,
15010 &breakpoint_set_cmdlist,
15011 &breakpoint_show_cmdlist);
15012
15013 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15014 condition_evaluation_enums,
15015 &condition_evaluation_mode_1, _("\
15016 Set mode of breakpoint condition evaluation."), _("\
15017 Show mode of breakpoint condition evaluation."), _("\
15018 When this is set to \"host\", breakpoint conditions will be\n\
15019 evaluated on the host's side by GDB. When it is set to \"target\",\n\
15020 breakpoint conditions will be downloaded to the target (if the target\n\
15021 supports such feature) and conditions will be evaluated on the target's side.\n\
15022 If this is set to \"auto\" (default), this will be automatically set to\n\
15023 \"target\" if it supports condition evaluation, otherwise it will\n\
15024 be set to \"host\"."),
15025 &set_condition_evaluation_mode,
15026 &show_condition_evaluation_mode,
15027 &breakpoint_set_cmdlist,
15028 &breakpoint_show_cmdlist);
15029
15030 add_com ("break-range", class_breakpoint, break_range_command, _("\
15031 Set a breakpoint for an address range.\n\
15032 break-range START-LOCATION, END-LOCATION\n\
15033 where START-LOCATION and END-LOCATION can be one of the following:\n\
15034 LINENUM, for that line in the current file,\n\
15035 FILE:LINENUM, for that line in that file,\n\
15036 +OFFSET, for that number of lines after the current line\n\
15037 or the start of the range\n\
15038 FUNCTION, for the first line in that function,\n\
15039 FILE:FUNCTION, to distinguish among like-named static functions.\n\
15040 *ADDRESS, for the instruction at that address.\n\
15041 \n\
15042 The breakpoint will stop execution of the inferior whenever it executes\n\
15043 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
15044 range (including START-LOCATION and END-LOCATION)."));
15045
15046 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
15047 Set a dynamic printf at specified location.\n\
15048 dprintf location,format string,arg1,arg2,...\n\
15049 location may be a linespec, explicit, or address location.\n"
15050 "\n" LOCATION_HELP_STRING));
15051 set_cmd_completer (c, location_completer);
15052
15053 add_setshow_enum_cmd ("dprintf-style", class_support,
15054 dprintf_style_enums, &dprintf_style, _("\
15055 Set the style of usage for dynamic printf."), _("\
15056 Show the style of usage for dynamic printf."), _("\
15057 This setting chooses how GDB will do a dynamic printf.\n\
15058 If the value is \"gdb\", then the printing is done by GDB to its own\n\
15059 console, as with the \"printf\" command.\n\
15060 If the value is \"call\", the print is done by calling a function in your\n\
15061 program; by default printf(), but you can choose a different function or\n\
15062 output stream by setting dprintf-function and dprintf-channel."),
15063 update_dprintf_commands, NULL,
15064 &setlist, &showlist);
15065
15066 add_setshow_string_cmd ("dprintf-function", class_support,
15067 &dprintf_function, _("\
15068 Set the function to use for dynamic printf."), _("\
15069 Show the function to use for dynamic printf."), NULL,
15070 update_dprintf_commands, NULL,
15071 &setlist, &showlist);
15072
15073 add_setshow_string_cmd ("dprintf-channel", class_support,
15074 &dprintf_channel, _("\
15075 Set the channel to use for dynamic printf."), _("\
15076 Show the channel to use for dynamic printf."), NULL,
15077 update_dprintf_commands, NULL,
15078 &setlist, &showlist);
15079
15080 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
15081 &disconnected_dprintf, _("\
15082 Set whether dprintf continues after GDB disconnects."), _("\
15083 Show whether dprintf continues after GDB disconnects."), _("\
15084 Use this to let dprintf commands continue to hit and produce output\n\
15085 even if GDB disconnects or detaches from the target."),
15086 NULL,
15087 NULL,
15088 &setlist, &showlist);
15089
15090 add_com ("agent-printf", class_vars, agent_printf_command, _("\
15091 Target agent only formatted printing, like the C \"printf\" function.\n\
15092 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
15093 This supports most C printf format specifications, like %s, %d, etc.\n\
15094 This is useful for formatted output in user-defined commands."));
15095
15096 automatic_hardware_breakpoints = true;
15097
15098 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
15099 "breakpoint");
15100 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
15101 "breakpoint");
15102 }