gdb: remove iterate_over_bp_locations function
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2021 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include <ctype.h>
23 #include "hashtab.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "breakpoint.h"
27 #include "tracepoint.h"
28 #include "gdbtypes.h"
29 #include "expression.h"
30 #include "gdbcore.h"
31 #include "gdbcmd.h"
32 #include "value.h"
33 #include "command.h"
34 #include "inferior.h"
35 #include "infrun.h"
36 #include "gdbthread.h"
37 #include "target.h"
38 #include "language.h"
39 #include "gdb-demangle.h"
40 #include "filenames.h"
41 #include "annotate.h"
42 #include "symfile.h"
43 #include "objfiles.h"
44 #include "source.h"
45 #include "linespec.h"
46 #include "completer.h"
47 #include "ui-out.h"
48 #include "cli/cli-script.h"
49 #include "block.h"
50 #include "solib.h"
51 #include "solist.h"
52 #include "observable.h"
53 #include "memattr.h"
54 #include "ada-lang.h"
55 #include "top.h"
56 #include "valprint.h"
57 #include "jit.h"
58 #include "parser-defs.h"
59 #include "gdb_regex.h"
60 #include "probe.h"
61 #include "cli/cli-utils.h"
62 #include "stack.h"
63 #include "ax-gdb.h"
64 #include "dummy-frame.h"
65 #include "interps.h"
66 #include "gdbsupport/format.h"
67 #include "thread-fsm.h"
68 #include "tid-parse.h"
69 #include "cli/cli-style.h"
70
71 /* readline include files */
72 #include "readline/tilde.h"
73
74 /* readline defines this. */
75 #undef savestring
76
77 #include "mi/mi-common.h"
78 #include "extension.h"
79 #include <algorithm>
80 #include "progspace-and-thread.h"
81 #include "gdbsupport/array-view.h"
82 #include "gdbsupport/gdb_optional.h"
83
84 /* Prototypes for local functions. */
85
86 static void map_breakpoint_numbers (const char *,
87 gdb::function_view<void (breakpoint *)>);
88
89 static void breakpoint_re_set_default (struct breakpoint *);
90
91 static void
92 create_sals_from_location_default (struct event_location *location,
93 struct linespec_result *canonical,
94 enum bptype type_wanted);
95
96 static void create_breakpoints_sal_default (struct gdbarch *,
97 struct linespec_result *,
98 gdb::unique_xmalloc_ptr<char>,
99 gdb::unique_xmalloc_ptr<char>,
100 enum bptype,
101 enum bpdisp, int, int,
102 int,
103 const struct breakpoint_ops *,
104 int, int, int, unsigned);
105
106 static std::vector<symtab_and_line> decode_location_default
107 (struct breakpoint *b, struct event_location *location,
108 struct program_space *search_pspace);
109
110 static int can_use_hardware_watchpoint
111 (const std::vector<value_ref_ptr> &vals);
112
113 static void mention (struct breakpoint *);
114
115 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
116 enum bptype,
117 const struct breakpoint_ops *);
118 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
119 const struct symtab_and_line *);
120
121 /* This function is used in gdbtk sources and thus can not be made
122 static. */
123 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
124 struct symtab_and_line,
125 enum bptype,
126 const struct breakpoint_ops *);
127
128 static struct breakpoint *
129 momentary_breakpoint_from_master (struct breakpoint *orig,
130 enum bptype type,
131 const struct breakpoint_ops *ops,
132 int loc_enabled);
133
134 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
135
136 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
137 CORE_ADDR bpaddr,
138 enum bptype bptype);
139
140 static void describe_other_breakpoints (struct gdbarch *,
141 struct program_space *, CORE_ADDR,
142 struct obj_section *, int);
143
144 static int watchpoint_locations_match (struct bp_location *loc1,
145 struct bp_location *loc2);
146
147 static int breakpoint_locations_match (struct bp_location *loc1,
148 struct bp_location *loc2,
149 bool sw_hw_bps_match = false);
150
151 static int breakpoint_location_address_match (struct bp_location *bl,
152 const struct address_space *aspace,
153 CORE_ADDR addr);
154
155 static int breakpoint_location_address_range_overlap (struct bp_location *,
156 const address_space *,
157 CORE_ADDR, int);
158
159 static int remove_breakpoint (struct bp_location *);
160 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
161
162 static enum print_stop_action print_bp_stop_message (bpstat bs);
163
164 static int hw_breakpoint_used_count (void);
165
166 static int hw_watchpoint_use_count (struct breakpoint *);
167
168 static int hw_watchpoint_used_count_others (struct breakpoint *except,
169 enum bptype type,
170 int *other_type_used);
171
172 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
173 int count);
174
175 static void decref_bp_location (struct bp_location **loc);
176
177 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
178
179 /* update_global_location_list's modes of operation wrt to whether to
180 insert locations now. */
181 enum ugll_insert_mode
182 {
183 /* Don't insert any breakpoint locations into the inferior, only
184 remove already-inserted locations that no longer should be
185 inserted. Functions that delete a breakpoint or breakpoints
186 should specify this mode, so that deleting a breakpoint doesn't
187 have the side effect of inserting the locations of other
188 breakpoints that are marked not-inserted, but should_be_inserted
189 returns true on them.
190
191 This behavior is useful is situations close to tear-down -- e.g.,
192 after an exec, while the target still has execution, but
193 breakpoint shadows of the previous executable image should *NOT*
194 be restored to the new image; or before detaching, where the
195 target still has execution and wants to delete breakpoints from
196 GDB's lists, and all breakpoints had already been removed from
197 the inferior. */
198 UGLL_DONT_INSERT,
199
200 /* May insert breakpoints iff breakpoints_should_be_inserted_now
201 claims breakpoints should be inserted now. */
202 UGLL_MAY_INSERT,
203
204 /* Insert locations now, irrespective of
205 breakpoints_should_be_inserted_now. E.g., say all threads are
206 stopped right now, and the user did "continue". We need to
207 insert breakpoints _before_ resuming the target, but
208 UGLL_MAY_INSERT wouldn't insert them, because
209 breakpoints_should_be_inserted_now returns false at that point,
210 as no thread is running yet. */
211 UGLL_INSERT
212 };
213
214 static void update_global_location_list (enum ugll_insert_mode);
215
216 static void update_global_location_list_nothrow (enum ugll_insert_mode);
217
218 static void insert_breakpoint_locations (void);
219
220 static void trace_pass_command (const char *, int);
221
222 static void set_tracepoint_count (int num);
223
224 static bool is_masked_watchpoint (const struct breakpoint *b);
225
226 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
227 otherwise. */
228
229 static int strace_marker_p (struct breakpoint *b);
230
231 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
232 that are implemented on top of software or hardware breakpoints
233 (user breakpoints, internal and momentary breakpoints, etc.). */
234 static struct breakpoint_ops bkpt_base_breakpoint_ops;
235
236 /* Internal breakpoints class type. */
237 static struct breakpoint_ops internal_breakpoint_ops;
238
239 /* Momentary breakpoints class type. */
240 static struct breakpoint_ops momentary_breakpoint_ops;
241
242 /* The breakpoint_ops structure to be used in regular user created
243 breakpoints. */
244 struct breakpoint_ops bkpt_breakpoint_ops;
245
246 /* Breakpoints set on probes. */
247 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
248
249 /* Tracepoints set on probes. */
250 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
251
252 /* Dynamic printf class type. */
253 struct breakpoint_ops dprintf_breakpoint_ops;
254
255 /* The style in which to perform a dynamic printf. This is a user
256 option because different output options have different tradeoffs;
257 if GDB does the printing, there is better error handling if there
258 is a problem with any of the arguments, but using an inferior
259 function lets you have special-purpose printers and sending of
260 output to the same place as compiled-in print functions. */
261
262 static const char dprintf_style_gdb[] = "gdb";
263 static const char dprintf_style_call[] = "call";
264 static const char dprintf_style_agent[] = "agent";
265 static const char *const dprintf_style_enums[] = {
266 dprintf_style_gdb,
267 dprintf_style_call,
268 dprintf_style_agent,
269 NULL
270 };
271 static const char *dprintf_style = dprintf_style_gdb;
272
273 /* The function to use for dynamic printf if the preferred style is to
274 call into the inferior. The value is simply a string that is
275 copied into the command, so it can be anything that GDB can
276 evaluate to a callable address, not necessarily a function name. */
277
278 static char *dprintf_function;
279
280 /* The channel to use for dynamic printf if the preferred style is to
281 call into the inferior; if a nonempty string, it will be passed to
282 the call as the first argument, with the format string as the
283 second. As with the dprintf function, this can be anything that
284 GDB knows how to evaluate, so in addition to common choices like
285 "stderr", this could be an app-specific expression like
286 "mystreams[curlogger]". */
287
288 static char *dprintf_channel;
289
290 /* True if dprintf commands should continue to operate even if GDB
291 has disconnected. */
292 static bool disconnected_dprintf = true;
293
294 struct command_line *
295 breakpoint_commands (struct breakpoint *b)
296 {
297 return b->commands ? b->commands.get () : NULL;
298 }
299
300 /* Flag indicating that a command has proceeded the inferior past the
301 current breakpoint. */
302
303 static bool breakpoint_proceeded;
304
305 const char *
306 bpdisp_text (enum bpdisp disp)
307 {
308 /* NOTE: the following values are a part of MI protocol and
309 represent values of 'disp' field returned when inferior stops at
310 a breakpoint. */
311 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
312
313 return bpdisps[(int) disp];
314 }
315
316 /* Prototypes for exported functions. */
317 /* If FALSE, gdb will not use hardware support for watchpoints, even
318 if such is available. */
319 static int can_use_hw_watchpoints;
320
321 static void
322 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
323 struct cmd_list_element *c,
324 const char *value)
325 {
326 fprintf_filtered (file,
327 _("Debugger's willingness to use "
328 "watchpoint hardware is %s.\n"),
329 value);
330 }
331
332 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
333 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
334 for unrecognized breakpoint locations.
335 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
336 static enum auto_boolean pending_break_support;
337 static void
338 show_pending_break_support (struct ui_file *file, int from_tty,
339 struct cmd_list_element *c,
340 const char *value)
341 {
342 fprintf_filtered (file,
343 _("Debugger's behavior regarding "
344 "pending breakpoints is %s.\n"),
345 value);
346 }
347
348 /* If true, gdb will automatically use hardware breakpoints for breakpoints
349 set with "break" but falling in read-only memory.
350 If false, gdb will warn about such breakpoints, but won't automatically
351 use hardware breakpoints. */
352 static bool automatic_hardware_breakpoints;
353 static void
354 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
355 struct cmd_list_element *c,
356 const char *value)
357 {
358 fprintf_filtered (file,
359 _("Automatic usage of hardware breakpoints is %s.\n"),
360 value);
361 }
362
363 /* If on, GDB keeps breakpoints inserted even if the inferior is
364 stopped, and immediately inserts any new breakpoints as soon as
365 they're created. If off (default), GDB keeps breakpoints off of
366 the target as long as possible. That is, it delays inserting
367 breakpoints until the next resume, and removes them again when the
368 target fully stops. This is a bit safer in case GDB crashes while
369 processing user input. */
370 static bool always_inserted_mode = false;
371
372 static void
373 show_always_inserted_mode (struct ui_file *file, int from_tty,
374 struct cmd_list_element *c, const char *value)
375 {
376 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
377 value);
378 }
379
380 /* See breakpoint.h. */
381
382 int
383 breakpoints_should_be_inserted_now (void)
384 {
385 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
386 {
387 /* If breakpoints are global, they should be inserted even if no
388 thread under gdb's control is running, or even if there are
389 no threads under GDB's control yet. */
390 return 1;
391 }
392 else
393 {
394 if (always_inserted_mode)
395 {
396 /* The user wants breakpoints inserted even if all threads
397 are stopped. */
398 return 1;
399 }
400
401 for (inferior *inf : all_inferiors ())
402 if (inf->has_execution ()
403 && threads_are_executing (inf->process_target ()))
404 return 1;
405
406 /* Don't remove breakpoints yet if, even though all threads are
407 stopped, we still have events to process. */
408 for (thread_info *tp : all_non_exited_threads ())
409 if (tp->resumed
410 && tp->suspend.waitstatus_pending_p)
411 return 1;
412 }
413 return 0;
414 }
415
416 static const char condition_evaluation_both[] = "host or target";
417
418 /* Modes for breakpoint condition evaluation. */
419 static const char condition_evaluation_auto[] = "auto";
420 static const char condition_evaluation_host[] = "host";
421 static const char condition_evaluation_target[] = "target";
422 static const char *const condition_evaluation_enums[] = {
423 condition_evaluation_auto,
424 condition_evaluation_host,
425 condition_evaluation_target,
426 NULL
427 };
428
429 /* Global that holds the current mode for breakpoint condition evaluation. */
430 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
431
432 /* Global that we use to display information to the user (gets its value from
433 condition_evaluation_mode_1. */
434 static const char *condition_evaluation_mode = condition_evaluation_auto;
435
436 /* Translate a condition evaluation mode MODE into either "host"
437 or "target". This is used mostly to translate from "auto" to the
438 real setting that is being used. It returns the translated
439 evaluation mode. */
440
441 static const char *
442 translate_condition_evaluation_mode (const char *mode)
443 {
444 if (mode == condition_evaluation_auto)
445 {
446 if (target_supports_evaluation_of_breakpoint_conditions ())
447 return condition_evaluation_target;
448 else
449 return condition_evaluation_host;
450 }
451 else
452 return mode;
453 }
454
455 /* Discovers what condition_evaluation_auto translates to. */
456
457 static const char *
458 breakpoint_condition_evaluation_mode (void)
459 {
460 return translate_condition_evaluation_mode (condition_evaluation_mode);
461 }
462
463 /* Return true if GDB should evaluate breakpoint conditions or false
464 otherwise. */
465
466 static int
467 gdb_evaluates_breakpoint_condition_p (void)
468 {
469 const char *mode = breakpoint_condition_evaluation_mode ();
470
471 return (mode == condition_evaluation_host);
472 }
473
474 /* Are we executing breakpoint commands? */
475 static int executing_breakpoint_commands;
476
477 /* Are overlay event breakpoints enabled? */
478 static int overlay_events_enabled;
479
480 /* See description in breakpoint.h. */
481 bool target_exact_watchpoints = false;
482
483 /* Walk the following statement or block through all breakpoints.
484 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
485 current breakpoint. */
486
487 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
488 for (B = breakpoint_chain; \
489 B ? (TMP=B->next, 1): 0; \
490 B = TMP)
491
492 /* Chains of all breakpoints defined. */
493
494 static struct breakpoint *breakpoint_chain;
495
496 /* 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 /* See declaration at breakpoint.h. */
631
632 struct breakpoint *
633 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
634 void *user_data)
635 {
636 for (breakpoint *b : all_breakpoints ())
637 if (func (b, user_data) != 0)
638 return b;
639
640 return nullptr;
641 }
642
643 /* Return whether a breakpoint is an active enabled breakpoint. */
644 static int
645 breakpoint_enabled (struct breakpoint *b)
646 {
647 return (b->enable_state == bp_enabled);
648 }
649
650 /* Set breakpoint count to NUM. */
651
652 static void
653 set_breakpoint_count (int num)
654 {
655 prev_breakpoint_count = breakpoint_count;
656 breakpoint_count = num;
657 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
658 }
659
660 /* Used by `start_rbreak_breakpoints' below, to record the current
661 breakpoint count before "rbreak" creates any breakpoint. */
662 static int rbreak_start_breakpoint_count;
663
664 /* Called at the start an "rbreak" command to record the first
665 breakpoint made. */
666
667 scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
668 {
669 rbreak_start_breakpoint_count = breakpoint_count;
670 }
671
672 /* Called at the end of an "rbreak" command to record the last
673 breakpoint made. */
674
675 scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
676 {
677 prev_breakpoint_count = rbreak_start_breakpoint_count;
678 }
679
680 /* Used in run_command to zero the hit count when a new run starts. */
681
682 void
683 clear_breakpoint_hit_counts (void)
684 {
685 for (breakpoint *b : all_breakpoints ())
686 b->hit_count = 0;
687 }
688
689 \f
690 /* Return the breakpoint with the specified number, or NULL
691 if the number does not refer to an existing breakpoint. */
692
693 struct breakpoint *
694 get_breakpoint (int num)
695 {
696 for (breakpoint *b : all_breakpoints ())
697 if (b->number == num)
698 return b;
699
700 return nullptr;
701 }
702
703 \f
704
705 /* Mark locations as "conditions have changed" in case the target supports
706 evaluating conditions on its side. */
707
708 static void
709 mark_breakpoint_modified (struct breakpoint *b)
710 {
711 /* This is only meaningful if the target is
712 evaluating conditions and if the user has
713 opted for condition evaluation on the target's
714 side. */
715 if (gdb_evaluates_breakpoint_condition_p ()
716 || !target_supports_evaluation_of_breakpoint_conditions ())
717 return;
718
719 if (!is_breakpoint (b))
720 return;
721
722 for (bp_location *loc : b->locations ())
723 loc->condition_changed = condition_modified;
724 }
725
726 /* Mark location as "conditions have changed" in case the target supports
727 evaluating conditions on its side. */
728
729 static void
730 mark_breakpoint_location_modified (struct bp_location *loc)
731 {
732 /* This is only meaningful if the target is
733 evaluating conditions and if the user has
734 opted for condition evaluation on the target's
735 side. */
736 if (gdb_evaluates_breakpoint_condition_p ()
737 || !target_supports_evaluation_of_breakpoint_conditions ())
738
739 return;
740
741 if (!is_breakpoint (loc->owner))
742 return;
743
744 loc->condition_changed = condition_modified;
745 }
746
747 /* Sets the condition-evaluation mode using the static global
748 condition_evaluation_mode. */
749
750 static void
751 set_condition_evaluation_mode (const char *args, int from_tty,
752 struct cmd_list_element *c)
753 {
754 const char *old_mode, *new_mode;
755
756 if ((condition_evaluation_mode_1 == condition_evaluation_target)
757 && !target_supports_evaluation_of_breakpoint_conditions ())
758 {
759 condition_evaluation_mode_1 = condition_evaluation_mode;
760 warning (_("Target does not support breakpoint condition evaluation.\n"
761 "Using host evaluation mode instead."));
762 return;
763 }
764
765 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
766 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
767
768 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
769 settings was "auto". */
770 condition_evaluation_mode = condition_evaluation_mode_1;
771
772 /* Only update the mode if the user picked a different one. */
773 if (new_mode != old_mode)
774 {
775 /* If the user switched to a different evaluation mode, we
776 need to synch the changes with the target as follows:
777
778 "host" -> "target": Send all (valid) conditions to the target.
779 "target" -> "host": Remove all the conditions from the target.
780 */
781
782 if (new_mode == condition_evaluation_target)
783 {
784 /* Mark everything modified and synch conditions with the
785 target. */
786 for (bp_location *loc : all_bp_locations ())
787 mark_breakpoint_location_modified (loc);
788 }
789 else
790 {
791 /* Manually mark non-duplicate locations to synch conditions
792 with the target. We do this to remove all the conditions the
793 target knows about. */
794 for (bp_location *loc : all_bp_locations ())
795 if (is_breakpoint (loc->owner) && loc->inserted)
796 loc->needs_update = 1;
797 }
798
799 /* Do the update. */
800 update_global_location_list (UGLL_MAY_INSERT);
801 }
802
803 return;
804 }
805
806 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
807 what "auto" is translating to. */
808
809 static void
810 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
811 struct cmd_list_element *c, const char *value)
812 {
813 if (condition_evaluation_mode == condition_evaluation_auto)
814 fprintf_filtered (file,
815 _("Breakpoint condition evaluation "
816 "mode is %s (currently %s).\n"),
817 value,
818 breakpoint_condition_evaluation_mode ());
819 else
820 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
821 value);
822 }
823
824 /* Parse COND_STRING in the context of LOC and set as the condition
825 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
826 the number of LOC within its owner. In case of parsing error, mark
827 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
828
829 static void
830 set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
831 int bp_num, int loc_num)
832 {
833 bool has_junk = false;
834 try
835 {
836 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
837 block_for_pc (loc->address), 0);
838 if (*cond_string != 0)
839 has_junk = true;
840 else
841 {
842 loc->cond = std::move (new_exp);
843 if (loc->disabled_by_cond && loc->enabled)
844 printf_filtered (_("Breakpoint %d's condition is now valid at "
845 "location %d, enabling.\n"),
846 bp_num, loc_num);
847
848 loc->disabled_by_cond = false;
849 }
850 }
851 catch (const gdb_exception_error &e)
852 {
853 if (loc->enabled)
854 {
855 /* Warn if a user-enabled location is now becoming disabled-by-cond.
856 BP_NUM is 0 if the breakpoint is being defined for the first
857 time using the "break ... if ..." command, and non-zero if
858 already defined. */
859 if (bp_num != 0)
860 warning (_("failed to validate condition at location %d.%d, "
861 "disabling:\n %s"), bp_num, loc_num, e.what ());
862 else
863 warning (_("failed to validate condition at location %d, "
864 "disabling:\n %s"), loc_num, e.what ());
865 }
866
867 loc->disabled_by_cond = true;
868 }
869
870 if (has_junk)
871 error (_("Garbage '%s' follows condition"), cond_string);
872 }
873
874 void
875 set_breakpoint_condition (struct breakpoint *b, const char *exp,
876 int from_tty, bool force)
877 {
878 if (*exp == 0)
879 {
880 xfree (b->cond_string);
881 b->cond_string = nullptr;
882
883 if (is_watchpoint (b))
884 static_cast<watchpoint *> (b)->cond_exp.reset ();
885 else
886 {
887 int loc_num = 1;
888 for (bp_location *loc : b->locations ())
889 {
890 loc->cond.reset ();
891 if (loc->disabled_by_cond && loc->enabled)
892 printf_filtered (_("Breakpoint %d's condition is now valid at "
893 "location %d, enabling.\n"),
894 b->number, loc_num);
895 loc->disabled_by_cond = false;
896 loc_num++;
897
898 /* No need to free the condition agent expression
899 bytecode (if we have one). We will handle this
900 when we go through update_global_location_list. */
901 }
902 }
903
904 if (from_tty)
905 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
906 }
907 else
908 {
909 if (is_watchpoint (b))
910 {
911 innermost_block_tracker tracker;
912 const char *arg = exp;
913 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
914 if (*arg != 0)
915 error (_("Junk at end of expression"));
916 watchpoint *w = static_cast<watchpoint *> (b);
917 w->cond_exp = std::move (new_exp);
918 w->cond_exp_valid_block = tracker.block ();
919 }
920 else
921 {
922 /* Parse and set condition expressions. We make two passes.
923 In the first, we parse the condition string to see if it
924 is valid in at least one location. If so, the condition
925 would be accepted. So we go ahead and set the locations'
926 conditions. In case no valid case is found, we throw
927 the error and the condition string will be rejected.
928 This two-pass approach is taken to avoid setting the
929 state of locations in case of a reject. */
930 for (bp_location *loc : b->locations ())
931 {
932 try
933 {
934 const char *arg = exp;
935 parse_exp_1 (&arg, loc->address,
936 block_for_pc (loc->address), 0);
937 if (*arg != 0)
938 error (_("Junk at end of expression"));
939 break;
940 }
941 catch (const gdb_exception_error &e)
942 {
943 /* Condition string is invalid. If this happens to
944 be the last loc, abandon (if not forced) or continue
945 (if forced). */
946 if (loc->next == nullptr && !force)
947 throw;
948 }
949 }
950
951 /* If we reach here, the condition is valid at some locations. */
952 int loc_num = 1;
953 for (bp_location *loc : b->locations ())
954 {
955 set_breakpoint_location_condition (exp, loc, b->number, loc_num);
956 loc_num++;
957 }
958 }
959
960 /* We know that the new condition parsed successfully. The
961 condition string of the breakpoint can be safely updated. */
962 xfree (b->cond_string);
963 b->cond_string = xstrdup (exp);
964 b->condition_not_parsed = 0;
965 }
966 mark_breakpoint_modified (b);
967
968 gdb::observers::breakpoint_modified.notify (b);
969 }
970
971 /* See breakpoint.h. */
972
973 void
974 set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
975 bool force)
976 {
977 for (breakpoint *b : all_breakpoints ())
978 if (b->number == bpnum)
979 {
980 /* Check if this breakpoint has a "stop" method implemented in an
981 extension language. This method and conditions entered into GDB
982 from the CLI are mutually exclusive. */
983 const struct extension_language_defn *extlang
984 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
985
986 if (extlang != NULL)
987 {
988 error (_("Only one stop condition allowed. There is currently"
989 " a %s stop condition defined for this breakpoint."),
990 ext_lang_capitalized_name (extlang));
991 }
992 set_breakpoint_condition (b, exp, from_tty, force);
993
994 if (is_breakpoint (b))
995 update_global_location_list (UGLL_MAY_INSERT);
996
997 return;
998 }
999
1000 error (_("No breakpoint number %d."), bpnum);
1001 }
1002
1003 /* The options for the "condition" command. */
1004
1005 struct condition_command_opts
1006 {
1007 /* For "-force". */
1008 bool force_condition = false;
1009 };
1010
1011 static const gdb::option::option_def condition_command_option_defs[] = {
1012
1013 gdb::option::flag_option_def<condition_command_opts> {
1014 "force",
1015 [] (condition_command_opts *opts) { return &opts->force_condition; },
1016 N_("Set the condition even if it is invalid for all current locations."),
1017 },
1018
1019 };
1020
1021 /* Create an option_def_group for the "condition" options, with
1022 CC_OPTS as context. */
1023
1024 static inline gdb::option::option_def_group
1025 make_condition_command_options_def_group (condition_command_opts *cc_opts)
1026 {
1027 return {{condition_command_option_defs}, cc_opts};
1028 }
1029
1030 /* Completion for the "condition" command. */
1031
1032 static void
1033 condition_completer (struct cmd_list_element *cmd,
1034 completion_tracker &tracker,
1035 const char *text, const char * /*word*/)
1036 {
1037 bool has_no_arguments = (*text == '\0');
1038 condition_command_opts cc_opts;
1039 const auto group = make_condition_command_options_def_group (&cc_opts);
1040 if (gdb::option::complete_options
1041 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1042 return;
1043
1044 text = skip_spaces (text);
1045 const char *space = skip_to_space (text);
1046 if (*space == '\0')
1047 {
1048 int len;
1049
1050 if (text[0] == '$')
1051 {
1052 tracker.advance_custom_word_point_by (1);
1053 /* We don't support completion of history indices. */
1054 if (!isdigit (text[1]))
1055 complete_internalvar (tracker, &text[1]);
1056 return;
1057 }
1058
1059 /* Suggest the "-force" flag if no arguments are given. If
1060 arguments were passed, they either already include the flag,
1061 or we are beyond the point of suggesting it because it's
1062 positionally the first argument. */
1063 if (has_no_arguments)
1064 gdb::option::complete_on_all_options (tracker, group);
1065
1066 /* We're completing the breakpoint number. */
1067 len = strlen (text);
1068
1069 for (breakpoint *b : all_breakpoints ())
1070 {
1071 char number[50];
1072
1073 xsnprintf (number, sizeof (number), "%d", b->number);
1074
1075 if (strncmp (number, text, len) == 0)
1076 tracker.add_completion (make_unique_xstrdup (number));
1077 }
1078
1079 return;
1080 }
1081
1082 /* We're completing the expression part. Skip the breakpoint num. */
1083 const char *exp_start = skip_spaces (space);
1084 tracker.advance_custom_word_point_by (exp_start - text);
1085 text = exp_start;
1086 const char *word = advance_to_expression_complete_word_point (tracker, text);
1087 expression_completer (cmd, tracker, text, word);
1088 }
1089
1090 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1091
1092 static void
1093 condition_command (const char *arg, int from_tty)
1094 {
1095 const char *p;
1096 int bnum;
1097
1098 if (arg == 0)
1099 error_no_arg (_("breakpoint number"));
1100
1101 p = arg;
1102
1103 /* Check if the "-force" flag was passed. */
1104 condition_command_opts cc_opts;
1105 const auto group = make_condition_command_options_def_group (&cc_opts);
1106 gdb::option::process_options
1107 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
1108
1109 bnum = get_number (&p);
1110 if (bnum == 0)
1111 error (_("Bad breakpoint argument: '%s'"), arg);
1112
1113 set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
1114 }
1115
1116 /* Check that COMMAND do not contain commands that are suitable
1117 only for tracepoints and not suitable for ordinary breakpoints.
1118 Throw if any such commands is found. */
1119
1120 static void
1121 check_no_tracepoint_commands (struct command_line *commands)
1122 {
1123 struct command_line *c;
1124
1125 for (c = commands; c; c = c->next)
1126 {
1127 if (c->control_type == while_stepping_control)
1128 error (_("The 'while-stepping' command can "
1129 "only be used for tracepoints"));
1130
1131 check_no_tracepoint_commands (c->body_list_0.get ());
1132 check_no_tracepoint_commands (c->body_list_1.get ());
1133
1134 /* Not that command parsing removes leading whitespace and comment
1135 lines and also empty lines. So, we only need to check for
1136 command directly. */
1137 if (strstr (c->line, "collect ") == c->line)
1138 error (_("The 'collect' command can only be used for tracepoints"));
1139
1140 if (strstr (c->line, "teval ") == c->line)
1141 error (_("The 'teval' command can only be used for tracepoints"));
1142 }
1143 }
1144
1145 struct longjmp_breakpoint : public breakpoint
1146 {
1147 ~longjmp_breakpoint () override;
1148 };
1149
1150 /* Encapsulate tests for different types of tracepoints. */
1151
1152 static bool
1153 is_tracepoint_type (bptype type)
1154 {
1155 return (type == bp_tracepoint
1156 || type == bp_fast_tracepoint
1157 || type == bp_static_tracepoint);
1158 }
1159
1160 static bool
1161 is_longjmp_type (bptype type)
1162 {
1163 return type == bp_longjmp || type == bp_exception;
1164 }
1165
1166 /* See breakpoint.h. */
1167
1168 bool
1169 is_tracepoint (const struct breakpoint *b)
1170 {
1171 return is_tracepoint_type (b->type);
1172 }
1173
1174 /* Factory function to create an appropriate instance of breakpoint given
1175 TYPE. */
1176
1177 static std::unique_ptr<breakpoint>
1178 new_breakpoint_from_type (bptype type)
1179 {
1180 breakpoint *b;
1181
1182 if (is_tracepoint_type (type))
1183 b = new tracepoint ();
1184 else if (is_longjmp_type (type))
1185 b = new longjmp_breakpoint ();
1186 else
1187 b = new breakpoint ();
1188
1189 return std::unique_ptr<breakpoint> (b);
1190 }
1191
1192 /* A helper function that validates that COMMANDS are valid for a
1193 breakpoint. This function will throw an exception if a problem is
1194 found. */
1195
1196 static void
1197 validate_commands_for_breakpoint (struct breakpoint *b,
1198 struct command_line *commands)
1199 {
1200 if (is_tracepoint (b))
1201 {
1202 struct tracepoint *t = (struct tracepoint *) b;
1203 struct command_line *c;
1204 struct command_line *while_stepping = 0;
1205
1206 /* Reset the while-stepping step count. The previous commands
1207 might have included a while-stepping action, while the new
1208 ones might not. */
1209 t->step_count = 0;
1210
1211 /* We need to verify that each top-level element of commands is
1212 valid for tracepoints, that there's at most one
1213 while-stepping element, and that the while-stepping's body
1214 has valid tracing commands excluding nested while-stepping.
1215 We also need to validate the tracepoint action line in the
1216 context of the tracepoint --- validate_actionline actually
1217 has side effects, like setting the tracepoint's
1218 while-stepping STEP_COUNT, in addition to checking if the
1219 collect/teval actions parse and make sense in the
1220 tracepoint's context. */
1221 for (c = commands; c; c = c->next)
1222 {
1223 if (c->control_type == while_stepping_control)
1224 {
1225 if (b->type == bp_fast_tracepoint)
1226 error (_("The 'while-stepping' command "
1227 "cannot be used for fast tracepoint"));
1228 else if (b->type == bp_static_tracepoint)
1229 error (_("The 'while-stepping' command "
1230 "cannot be used for static tracepoint"));
1231
1232 if (while_stepping)
1233 error (_("The 'while-stepping' command "
1234 "can be used only once"));
1235 else
1236 while_stepping = c;
1237 }
1238
1239 validate_actionline (c->line, b);
1240 }
1241 if (while_stepping)
1242 {
1243 struct command_line *c2;
1244
1245 gdb_assert (while_stepping->body_list_1 == nullptr);
1246 c2 = while_stepping->body_list_0.get ();
1247 for (; c2; c2 = c2->next)
1248 {
1249 if (c2->control_type == while_stepping_control)
1250 error (_("The 'while-stepping' command cannot be nested"));
1251 }
1252 }
1253 }
1254 else
1255 {
1256 check_no_tracepoint_commands (commands);
1257 }
1258 }
1259
1260 /* Return a vector of all the static tracepoints set at ADDR. The
1261 caller is responsible for releasing the vector. */
1262
1263 std::vector<breakpoint *>
1264 static_tracepoints_here (CORE_ADDR addr)
1265 {
1266 std::vector<breakpoint *> found;
1267
1268 for (breakpoint *b : all_breakpoints ())
1269 if (b->type == bp_static_tracepoint)
1270 {
1271 for (bp_location *loc : b->locations ())
1272 if (loc->address == addr)
1273 found.push_back (b);
1274 }
1275
1276 return found;
1277 }
1278
1279 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1280 validate that only allowed commands are included. */
1281
1282 void
1283 breakpoint_set_commands (struct breakpoint *b,
1284 counted_command_line &&commands)
1285 {
1286 validate_commands_for_breakpoint (b, commands.get ());
1287
1288 b->commands = std::move (commands);
1289 gdb::observers::breakpoint_modified.notify (b);
1290 }
1291
1292 /* Set the internal `silent' flag on the breakpoint. Note that this
1293 is not the same as the "silent" that may appear in the breakpoint's
1294 commands. */
1295
1296 void
1297 breakpoint_set_silent (struct breakpoint *b, int silent)
1298 {
1299 int old_silent = b->silent;
1300
1301 b->silent = silent;
1302 if (old_silent != silent)
1303 gdb::observers::breakpoint_modified.notify (b);
1304 }
1305
1306 /* Set the thread for this breakpoint. If THREAD is -1, make the
1307 breakpoint work for any thread. */
1308
1309 void
1310 breakpoint_set_thread (struct breakpoint *b, int thread)
1311 {
1312 int old_thread = b->thread;
1313
1314 b->thread = thread;
1315 if (old_thread != thread)
1316 gdb::observers::breakpoint_modified.notify (b);
1317 }
1318
1319 /* Set the task for this breakpoint. If TASK is 0, make the
1320 breakpoint work for any task. */
1321
1322 void
1323 breakpoint_set_task (struct breakpoint *b, int task)
1324 {
1325 int old_task = b->task;
1326
1327 b->task = task;
1328 if (old_task != task)
1329 gdb::observers::breakpoint_modified.notify (b);
1330 }
1331
1332 static void
1333 commands_command_1 (const char *arg, int from_tty,
1334 struct command_line *control)
1335 {
1336 counted_command_line cmd;
1337 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1338 NULL after the call to read_command_lines if the user provides an empty
1339 list of command by just typing "end". */
1340 bool cmd_read = false;
1341
1342 std::string new_arg;
1343
1344 if (arg == NULL || !*arg)
1345 {
1346 /* Argument not explicitly given. Synthesize it. */
1347 if (breakpoint_count - prev_breakpoint_count > 1)
1348 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1349 breakpoint_count);
1350 else if (breakpoint_count > 0)
1351 new_arg = string_printf ("%d", breakpoint_count);
1352 }
1353 else
1354 {
1355 /* Create a copy of ARG. This is needed because the "commands"
1356 command may be coming from a script. In that case, the read
1357 line buffer is going to be overwritten in the lambda of
1358 'map_breakpoint_numbers' below when reading the next line
1359 before we are are done parsing the breakpoint numbers. */
1360 new_arg = arg;
1361 }
1362 arg = new_arg.c_str ();
1363
1364 map_breakpoint_numbers
1365 (arg, [&] (breakpoint *b)
1366 {
1367 if (!cmd_read)
1368 {
1369 gdb_assert (cmd == NULL);
1370 if (control != NULL)
1371 cmd = control->body_list_0;
1372 else
1373 {
1374 std::string str
1375 = string_printf (_("Type commands for breakpoint(s) "
1376 "%s, one per line."),
1377 arg);
1378
1379 auto do_validate = [=] (const char *line)
1380 {
1381 validate_actionline (line, b);
1382 };
1383 gdb::function_view<void (const char *)> validator;
1384 if (is_tracepoint (b))
1385 validator = do_validate;
1386
1387 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1388 }
1389 cmd_read = true;
1390 }
1391
1392 /* If a breakpoint was on the list more than once, we don't need to
1393 do anything. */
1394 if (b->commands != cmd)
1395 {
1396 validate_commands_for_breakpoint (b, cmd.get ());
1397 b->commands = cmd;
1398 gdb::observers::breakpoint_modified.notify (b);
1399 }
1400 });
1401 }
1402
1403 static void
1404 commands_command (const char *arg, int from_tty)
1405 {
1406 commands_command_1 (arg, from_tty, NULL);
1407 }
1408
1409 /* Like commands_command, but instead of reading the commands from
1410 input stream, takes them from an already parsed command structure.
1411
1412 This is used by cli-script.c to DTRT with breakpoint commands
1413 that are part of if and while bodies. */
1414 enum command_control_type
1415 commands_from_control_command (const char *arg, struct command_line *cmd)
1416 {
1417 commands_command_1 (arg, 0, cmd);
1418 return simple_control;
1419 }
1420
1421 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1422
1423 static int
1424 bp_location_has_shadow (struct bp_location *bl)
1425 {
1426 if (bl->loc_type != bp_loc_software_breakpoint)
1427 return 0;
1428 if (!bl->inserted)
1429 return 0;
1430 if (bl->target_info.shadow_len == 0)
1431 /* BL isn't valid, or doesn't shadow memory. */
1432 return 0;
1433 return 1;
1434 }
1435
1436 /* Update BUF, which is LEN bytes read from the target address
1437 MEMADDR, by replacing a memory breakpoint with its shadowed
1438 contents.
1439
1440 If READBUF is not NULL, this buffer must not overlap with the of
1441 the breakpoint location's shadow_contents buffer. Otherwise, a
1442 failed assertion internal error will be raised. */
1443
1444 static void
1445 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1446 const gdb_byte *writebuf_org,
1447 ULONGEST memaddr, LONGEST len,
1448 struct bp_target_info *target_info,
1449 struct gdbarch *gdbarch)
1450 {
1451 /* Now do full processing of the found relevant range of elements. */
1452 CORE_ADDR bp_addr = 0;
1453 int bp_size = 0;
1454 int bptoffset = 0;
1455
1456 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1457 current_program_space->aspace, 0))
1458 {
1459 /* The breakpoint is inserted in a different address space. */
1460 return;
1461 }
1462
1463 /* Addresses and length of the part of the breakpoint that
1464 we need to copy. */
1465 bp_addr = target_info->placed_address;
1466 bp_size = target_info->shadow_len;
1467
1468 if (bp_addr + bp_size <= memaddr)
1469 {
1470 /* The breakpoint is entirely before the chunk of memory we are
1471 reading. */
1472 return;
1473 }
1474
1475 if (bp_addr >= memaddr + len)
1476 {
1477 /* The breakpoint is entirely after the chunk of memory we are
1478 reading. */
1479 return;
1480 }
1481
1482 /* Offset within shadow_contents. */
1483 if (bp_addr < memaddr)
1484 {
1485 /* Only copy the second part of the breakpoint. */
1486 bp_size -= memaddr - bp_addr;
1487 bptoffset = memaddr - bp_addr;
1488 bp_addr = memaddr;
1489 }
1490
1491 if (bp_addr + bp_size > memaddr + len)
1492 {
1493 /* Only copy the first part of the breakpoint. */
1494 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1495 }
1496
1497 if (readbuf != NULL)
1498 {
1499 /* Verify that the readbuf buffer does not overlap with the
1500 shadow_contents buffer. */
1501 gdb_assert (target_info->shadow_contents >= readbuf + len
1502 || readbuf >= (target_info->shadow_contents
1503 + target_info->shadow_len));
1504
1505 /* Update the read buffer with this inserted breakpoint's
1506 shadow. */
1507 memcpy (readbuf + bp_addr - memaddr,
1508 target_info->shadow_contents + bptoffset, bp_size);
1509 }
1510 else
1511 {
1512 const unsigned char *bp;
1513 CORE_ADDR addr = target_info->reqstd_address;
1514 int placed_size;
1515
1516 /* Update the shadow with what we want to write to memory. */
1517 memcpy (target_info->shadow_contents + bptoffset,
1518 writebuf_org + bp_addr - memaddr, bp_size);
1519
1520 /* Determine appropriate breakpoint contents and size for this
1521 address. */
1522 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1523
1524 /* Update the final write buffer with this inserted
1525 breakpoint's INSN. */
1526 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1527 }
1528 }
1529
1530 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1531 by replacing any memory breakpoints with their shadowed contents.
1532
1533 If READBUF is not NULL, this buffer must not overlap with any of
1534 the breakpoint location's shadow_contents buffers. Otherwise,
1535 a failed assertion internal error will be raised.
1536
1537 The range of shadowed area by each bp_location is:
1538 bl->address - bp_locations_placed_address_before_address_max
1539 up to bl->address + bp_locations_shadow_len_after_address_max
1540 The range we were requested to resolve shadows for is:
1541 memaddr ... memaddr + len
1542 Thus the safe cutoff boundaries for performance optimization are
1543 memaddr + len <= (bl->address
1544 - bp_locations_placed_address_before_address_max)
1545 and:
1546 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1547
1548 void
1549 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1550 const gdb_byte *writebuf_org,
1551 ULONGEST memaddr, LONGEST len)
1552 {
1553 /* Left boundary, right boundary and median element of our binary
1554 search. */
1555 unsigned bc_l, bc_r, bc;
1556
1557 /* Find BC_L which is a leftmost element which may affect BUF
1558 content. It is safe to report lower value but a failure to
1559 report higher one. */
1560
1561 bc_l = 0;
1562 bc_r = bp_locations.size ();
1563 while (bc_l + 1 < bc_r)
1564 {
1565 struct bp_location *bl;
1566
1567 bc = (bc_l + bc_r) / 2;
1568 bl = bp_locations[bc];
1569
1570 /* Check first BL->ADDRESS will not overflow due to the added
1571 constant. Then advance the left boundary only if we are sure
1572 the BC element can in no way affect the BUF content (MEMADDR
1573 to MEMADDR + LEN range).
1574
1575 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1576 offset so that we cannot miss a breakpoint with its shadow
1577 range tail still reaching MEMADDR. */
1578
1579 if ((bl->address + bp_locations_shadow_len_after_address_max
1580 >= bl->address)
1581 && (bl->address + bp_locations_shadow_len_after_address_max
1582 <= memaddr))
1583 bc_l = bc;
1584 else
1585 bc_r = bc;
1586 }
1587
1588 /* Due to the binary search above, we need to make sure we pick the
1589 first location that's at BC_L's address. E.g., if there are
1590 multiple locations at the same address, BC_L may end up pointing
1591 at a duplicate location, and miss the "master"/"inserted"
1592 location. Say, given locations L1, L2 and L3 at addresses A and
1593 B:
1594
1595 L1@A, L2@A, L3@B, ...
1596
1597 BC_L could end up pointing at location L2, while the "master"
1598 location could be L1. Since the `loc->inserted' flag is only set
1599 on "master" locations, we'd forget to restore the shadow of L1
1600 and L2. */
1601 while (bc_l > 0
1602 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1603 bc_l--;
1604
1605 /* Now do full processing of the found relevant range of elements. */
1606
1607 for (bc = bc_l; bc < bp_locations.size (); bc++)
1608 {
1609 struct bp_location *bl = bp_locations[bc];
1610
1611 /* bp_location array has BL->OWNER always non-NULL. */
1612 if (bl->owner->type == bp_none)
1613 warning (_("reading through apparently deleted breakpoint #%d?"),
1614 bl->owner->number);
1615
1616 /* Performance optimization: any further element can no longer affect BUF
1617 content. */
1618
1619 if (bl->address >= bp_locations_placed_address_before_address_max
1620 && memaddr + len <= (bl->address
1621 - bp_locations_placed_address_before_address_max))
1622 break;
1623
1624 if (!bp_location_has_shadow (bl))
1625 continue;
1626
1627 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1628 memaddr, len, &bl->target_info, bl->gdbarch);
1629 }
1630 }
1631
1632 /* See breakpoint.h. */
1633
1634 bool
1635 is_breakpoint (const struct breakpoint *bpt)
1636 {
1637 return (bpt->type == bp_breakpoint
1638 || bpt->type == bp_hardware_breakpoint
1639 || bpt->type == bp_dprintf);
1640 }
1641
1642 /* Return true if BPT is of any hardware watchpoint kind. */
1643
1644 static bool
1645 is_hardware_watchpoint (const struct breakpoint *bpt)
1646 {
1647 return (bpt->type == bp_hardware_watchpoint
1648 || bpt->type == bp_read_watchpoint
1649 || bpt->type == bp_access_watchpoint);
1650 }
1651
1652 /* See breakpoint.h. */
1653
1654 bool
1655 is_watchpoint (const struct breakpoint *bpt)
1656 {
1657 return (is_hardware_watchpoint (bpt)
1658 || bpt->type == bp_watchpoint);
1659 }
1660
1661 /* Returns true if the current thread and its running state are safe
1662 to evaluate or update watchpoint B. Watchpoints on local
1663 expressions need to be evaluated in the context of the thread that
1664 was current when the watchpoint was created, and, that thread needs
1665 to be stopped to be able to select the correct frame context.
1666 Watchpoints on global expressions can be evaluated on any thread,
1667 and in any state. It is presently left to the target allowing
1668 memory accesses when threads are running. */
1669
1670 static int
1671 watchpoint_in_thread_scope (struct watchpoint *b)
1672 {
1673 return (b->pspace == current_program_space
1674 && (b->watchpoint_thread == null_ptid
1675 || (inferior_ptid == b->watchpoint_thread
1676 && !inferior_thread ()->executing)));
1677 }
1678
1679 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1680 associated bp_watchpoint_scope breakpoint. */
1681
1682 static void
1683 watchpoint_del_at_next_stop (struct watchpoint *w)
1684 {
1685 if (w->related_breakpoint != w)
1686 {
1687 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1688 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1689 w->related_breakpoint->disposition = disp_del_at_next_stop;
1690 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1691 w->related_breakpoint = w;
1692 }
1693 w->disposition = disp_del_at_next_stop;
1694 }
1695
1696 /* Extract a bitfield value from value VAL using the bit parameters contained in
1697 watchpoint W. */
1698
1699 static struct value *
1700 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1701 {
1702 struct value *bit_val;
1703
1704 if (val == NULL)
1705 return NULL;
1706
1707 bit_val = allocate_value (value_type (val));
1708
1709 unpack_value_bitfield (bit_val,
1710 w->val_bitpos,
1711 w->val_bitsize,
1712 value_contents_for_printing (val),
1713 value_offset (val),
1714 val);
1715
1716 return bit_val;
1717 }
1718
1719 /* Allocate a dummy location and add it to B, which must be a software
1720 watchpoint. This is required because even if a software watchpoint
1721 is not watching any memory, bpstat_stop_status requires a location
1722 to be able to report stops. */
1723
1724 static void
1725 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1726 struct program_space *pspace)
1727 {
1728 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1729
1730 b->loc = allocate_bp_location (b);
1731 b->loc->pspace = pspace;
1732 b->loc->address = -1;
1733 b->loc->length = -1;
1734 }
1735
1736 /* Returns true if B is a software watchpoint that is not watching any
1737 memory (e.g., "watch $pc"). */
1738
1739 static bool
1740 is_no_memory_software_watchpoint (struct breakpoint *b)
1741 {
1742 return (b->type == bp_watchpoint
1743 && b->loc != NULL
1744 && b->loc->next == NULL
1745 && b->loc->address == -1
1746 && b->loc->length == -1);
1747 }
1748
1749 /* Assuming that B is a watchpoint:
1750 - Reparse watchpoint expression, if REPARSE is non-zero
1751 - Evaluate expression and store the result in B->val
1752 - Evaluate the condition if there is one, and store the result
1753 in b->loc->cond.
1754 - Update the list of values that must be watched in B->loc.
1755
1756 If the watchpoint disposition is disp_del_at_next_stop, then do
1757 nothing. If this is local watchpoint that is out of scope, delete
1758 it.
1759
1760 Even with `set breakpoint always-inserted on' the watchpoints are
1761 removed + inserted on each stop here. Normal breakpoints must
1762 never be removed because they might be missed by a running thread
1763 when debugging in non-stop mode. On the other hand, hardware
1764 watchpoints (is_hardware_watchpoint; processed here) are specific
1765 to each LWP since they are stored in each LWP's hardware debug
1766 registers. Therefore, such LWP must be stopped first in order to
1767 be able to modify its hardware watchpoints.
1768
1769 Hardware watchpoints must be reset exactly once after being
1770 presented to the user. It cannot be done sooner, because it would
1771 reset the data used to present the watchpoint hit to the user. And
1772 it must not be done later because it could display the same single
1773 watchpoint hit during multiple GDB stops. Note that the latter is
1774 relevant only to the hardware watchpoint types bp_read_watchpoint
1775 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1776 not user-visible - its hit is suppressed if the memory content has
1777 not changed.
1778
1779 The following constraints influence the location where we can reset
1780 hardware watchpoints:
1781
1782 * target_stopped_by_watchpoint and target_stopped_data_address are
1783 called several times when GDB stops.
1784
1785 [linux]
1786 * Multiple hardware watchpoints can be hit at the same time,
1787 causing GDB to stop. GDB only presents one hardware watchpoint
1788 hit at a time as the reason for stopping, and all the other hits
1789 are presented later, one after the other, each time the user
1790 requests the execution to be resumed. Execution is not resumed
1791 for the threads still having pending hit event stored in
1792 LWP_INFO->STATUS. While the watchpoint is already removed from
1793 the inferior on the first stop the thread hit event is kept being
1794 reported from its cached value by linux_nat_stopped_data_address
1795 until the real thread resume happens after the watchpoint gets
1796 presented and thus its LWP_INFO->STATUS gets reset.
1797
1798 Therefore the hardware watchpoint hit can get safely reset on the
1799 watchpoint removal from inferior. */
1800
1801 static void
1802 update_watchpoint (struct watchpoint *b, int reparse)
1803 {
1804 int within_current_scope;
1805 struct frame_id saved_frame_id;
1806 int frame_saved;
1807
1808 /* If this is a local watchpoint, we only want to check if the
1809 watchpoint frame is in scope if the current thread is the thread
1810 that was used to create the watchpoint. */
1811 if (!watchpoint_in_thread_scope (b))
1812 return;
1813
1814 if (b->disposition == disp_del_at_next_stop)
1815 return;
1816
1817 frame_saved = 0;
1818
1819 /* Determine if the watchpoint is within scope. */
1820 if (b->exp_valid_block == NULL)
1821 within_current_scope = 1;
1822 else
1823 {
1824 struct frame_info *fi = get_current_frame ();
1825 struct gdbarch *frame_arch = get_frame_arch (fi);
1826 CORE_ADDR frame_pc = get_frame_pc (fi);
1827
1828 /* If we're at a point where the stack has been destroyed
1829 (e.g. in a function epilogue), unwinding may not work
1830 properly. Do not attempt to recreate locations at this
1831 point. See similar comments in watchpoint_check. */
1832 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1833 return;
1834
1835 /* Save the current frame's ID so we can restore it after
1836 evaluating the watchpoint expression on its own frame. */
1837 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1838 took a frame parameter, so that we didn't have to change the
1839 selected frame. */
1840 frame_saved = 1;
1841 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1842
1843 fi = frame_find_by_id (b->watchpoint_frame);
1844 within_current_scope = (fi != NULL);
1845 if (within_current_scope)
1846 select_frame (fi);
1847 }
1848
1849 /* We don't free locations. They are stored in the bp_location array
1850 and update_global_location_list will eventually delete them and
1851 remove breakpoints if needed. */
1852 b->loc = NULL;
1853
1854 if (within_current_scope && reparse)
1855 {
1856 const char *s;
1857
1858 b->exp.reset ();
1859 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1860 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1861 /* If the meaning of expression itself changed, the old value is
1862 no longer relevant. We don't want to report a watchpoint hit
1863 to the user when the old value and the new value may actually
1864 be completely different objects. */
1865 b->val = NULL;
1866 b->val_valid = false;
1867
1868 /* Note that unlike with breakpoints, the watchpoint's condition
1869 expression is stored in the breakpoint object, not in the
1870 locations (re)created below. */
1871 if (b->cond_string != NULL)
1872 {
1873 b->cond_exp.reset ();
1874
1875 s = b->cond_string;
1876 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1877 }
1878 }
1879
1880 /* If we failed to parse the expression, for example because
1881 it refers to a global variable in a not-yet-loaded shared library,
1882 don't try to insert watchpoint. We don't automatically delete
1883 such watchpoint, though, since failure to parse expression
1884 is different from out-of-scope watchpoint. */
1885 if (!target_has_execution ())
1886 {
1887 /* Without execution, memory can't change. No use to try and
1888 set watchpoint locations. The watchpoint will be reset when
1889 the target gains execution, through breakpoint_re_set. */
1890 if (!can_use_hw_watchpoints)
1891 {
1892 if (b->ops->works_in_software_mode (b))
1893 b->type = bp_watchpoint;
1894 else
1895 error (_("Can't set read/access watchpoint when "
1896 "hardware watchpoints are disabled."));
1897 }
1898 }
1899 else if (within_current_scope && b->exp)
1900 {
1901 std::vector<value_ref_ptr> val_chain;
1902 struct value *v, *result;
1903 struct program_space *frame_pspace;
1904
1905 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
1906 &val_chain, false);
1907
1908 /* Avoid setting b->val if it's already set. The meaning of
1909 b->val is 'the last value' user saw, and we should update
1910 it only if we reported that last value to user. As it
1911 happens, the code that reports it updates b->val directly.
1912 We don't keep track of the memory value for masked
1913 watchpoints. */
1914 if (!b->val_valid && !is_masked_watchpoint (b))
1915 {
1916 if (b->val_bitsize != 0)
1917 v = extract_bitfield_from_watchpoint_value (b, v);
1918 b->val = release_value (v);
1919 b->val_valid = true;
1920 }
1921
1922 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1923
1924 /* Look at each value on the value chain. */
1925 gdb_assert (!val_chain.empty ());
1926 for (const value_ref_ptr &iter : val_chain)
1927 {
1928 v = iter.get ();
1929
1930 /* If it's a memory location, and GDB actually needed
1931 its contents to evaluate the expression, then we
1932 must watch it. If the first value returned is
1933 still lazy, that means an error occurred reading it;
1934 watch it anyway in case it becomes readable. */
1935 if (VALUE_LVAL (v) == lval_memory
1936 && (v == val_chain[0] || ! value_lazy (v)))
1937 {
1938 struct type *vtype = check_typedef (value_type (v));
1939
1940 /* We only watch structs and arrays if user asked
1941 for it explicitly, never if they just happen to
1942 appear in the middle of some value chain. */
1943 if (v == result
1944 || (vtype->code () != TYPE_CODE_STRUCT
1945 && vtype->code () != TYPE_CODE_ARRAY))
1946 {
1947 CORE_ADDR addr;
1948 enum target_hw_bp_type type;
1949 struct bp_location *loc, **tmp;
1950 int bitpos = 0, bitsize = 0;
1951
1952 if (value_bitsize (v) != 0)
1953 {
1954 /* Extract the bit parameters out from the bitfield
1955 sub-expression. */
1956 bitpos = value_bitpos (v);
1957 bitsize = value_bitsize (v);
1958 }
1959 else if (v == result && b->val_bitsize != 0)
1960 {
1961 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1962 lvalue whose bit parameters are saved in the fields
1963 VAL_BITPOS and VAL_BITSIZE. */
1964 bitpos = b->val_bitpos;
1965 bitsize = b->val_bitsize;
1966 }
1967
1968 addr = value_address (v);
1969 if (bitsize != 0)
1970 {
1971 /* Skip the bytes that don't contain the bitfield. */
1972 addr += bitpos / 8;
1973 }
1974
1975 type = hw_write;
1976 if (b->type == bp_read_watchpoint)
1977 type = hw_read;
1978 else if (b->type == bp_access_watchpoint)
1979 type = hw_access;
1980
1981 loc = allocate_bp_location (b);
1982 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1983 ;
1984 *tmp = loc;
1985 loc->gdbarch = value_type (v)->arch ();
1986
1987 loc->pspace = frame_pspace;
1988 loc->address = address_significant (loc->gdbarch, addr);
1989
1990 if (bitsize != 0)
1991 {
1992 /* Just cover the bytes that make up the bitfield. */
1993 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1994 }
1995 else
1996 loc->length = TYPE_LENGTH (value_type (v));
1997
1998 loc->watchpoint_type = type;
1999 }
2000 }
2001 }
2002
2003 /* Change the type of breakpoint between hardware assisted or
2004 an ordinary watchpoint depending on the hardware support
2005 and free hardware slots. REPARSE is set when the inferior
2006 is started. */
2007 if (reparse)
2008 {
2009 int reg_cnt;
2010 enum bp_loc_type loc_type;
2011
2012 reg_cnt = can_use_hardware_watchpoint (val_chain);
2013
2014 if (reg_cnt)
2015 {
2016 int i, target_resources_ok, other_type_used;
2017 enum bptype type;
2018
2019 /* Use an exact watchpoint when there's only one memory region to be
2020 watched, and only one debug register is needed to watch it. */
2021 b->exact = target_exact_watchpoints && reg_cnt == 1;
2022
2023 /* We need to determine how many resources are already
2024 used for all other hardware watchpoints plus this one
2025 to see if we still have enough resources to also fit
2026 this watchpoint in as well. */
2027
2028 /* If this is a software watchpoint, we try to turn it
2029 to a hardware one -- count resources as if B was of
2030 hardware watchpoint type. */
2031 type = b->type;
2032 if (type == bp_watchpoint)
2033 type = bp_hardware_watchpoint;
2034
2035 /* This watchpoint may or may not have been placed on
2036 the list yet at this point (it won't be in the list
2037 if we're trying to create it for the first time,
2038 through watch_command), so always account for it
2039 manually. */
2040
2041 /* Count resources used by all watchpoints except B. */
2042 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
2043
2044 /* Add in the resources needed for B. */
2045 i += hw_watchpoint_use_count (b);
2046
2047 target_resources_ok
2048 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2049 if (target_resources_ok <= 0)
2050 {
2051 int sw_mode = b->ops->works_in_software_mode (b);
2052
2053 if (target_resources_ok == 0 && !sw_mode)
2054 error (_("Target does not support this type of "
2055 "hardware watchpoint."));
2056 else if (target_resources_ok < 0 && !sw_mode)
2057 error (_("There are not enough available hardware "
2058 "resources for this watchpoint."));
2059
2060 /* Downgrade to software watchpoint. */
2061 b->type = bp_watchpoint;
2062 }
2063 else
2064 {
2065 /* If this was a software watchpoint, we've just
2066 found we have enough resources to turn it to a
2067 hardware watchpoint. Otherwise, this is a
2068 nop. */
2069 b->type = type;
2070 }
2071 }
2072 else if (!b->ops->works_in_software_mode (b))
2073 {
2074 if (!can_use_hw_watchpoints)
2075 error (_("Can't set read/access watchpoint when "
2076 "hardware watchpoints are disabled."));
2077 else
2078 error (_("Expression cannot be implemented with "
2079 "read/access watchpoint."));
2080 }
2081 else
2082 b->type = bp_watchpoint;
2083
2084 loc_type = (b->type == bp_watchpoint? bp_loc_other
2085 : bp_loc_hardware_watchpoint);
2086 for (bp_location *bl : b->locations ())
2087 bl->loc_type = loc_type;
2088 }
2089
2090 /* If a software watchpoint is not watching any memory, then the
2091 above left it without any location set up. But,
2092 bpstat_stop_status requires a location to be able to report
2093 stops, so make sure there's at least a dummy one. */
2094 if (b->type == bp_watchpoint && b->loc == NULL)
2095 software_watchpoint_add_no_memory_location (b, frame_pspace);
2096 }
2097 else if (!within_current_scope)
2098 {
2099 printf_filtered (_("\
2100 Watchpoint %d deleted because the program has left the block\n\
2101 in which its expression is valid.\n"),
2102 b->number);
2103 watchpoint_del_at_next_stop (b);
2104 }
2105
2106 /* Restore the selected frame. */
2107 if (frame_saved)
2108 select_frame (frame_find_by_id (saved_frame_id));
2109 }
2110
2111
2112 /* Returns 1 iff breakpoint location should be
2113 inserted in the inferior. We don't differentiate the type of BL's owner
2114 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2115 breakpoint_ops is not defined, because in insert_bp_location,
2116 tracepoint's insert_location will not be called. */
2117 static int
2118 should_be_inserted (struct bp_location *bl)
2119 {
2120 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2121 return 0;
2122
2123 if (bl->owner->disposition == disp_del_at_next_stop)
2124 return 0;
2125
2126 if (!bl->enabled || bl->disabled_by_cond
2127 || bl->shlib_disabled || bl->duplicate)
2128 return 0;
2129
2130 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2131 return 0;
2132
2133 /* This is set for example, when we're attached to the parent of a
2134 vfork, and have detached from the child. The child is running
2135 free, and we expect it to do an exec or exit, at which point the
2136 OS makes the parent schedulable again (and the target reports
2137 that the vfork is done). Until the child is done with the shared
2138 memory region, do not insert breakpoints in the parent, otherwise
2139 the child could still trip on the parent's breakpoints. Since
2140 the parent is blocked anyway, it won't miss any breakpoint. */
2141 if (bl->pspace->breakpoints_not_allowed)
2142 return 0;
2143
2144 /* Don't insert a breakpoint if we're trying to step past its
2145 location, except if the breakpoint is a single-step breakpoint,
2146 and the breakpoint's thread is the thread which is stepping past
2147 a breakpoint. */
2148 if ((bl->loc_type == bp_loc_software_breakpoint
2149 || bl->loc_type == bp_loc_hardware_breakpoint)
2150 && stepping_past_instruction_at (bl->pspace->aspace,
2151 bl->address)
2152 /* The single-step breakpoint may be inserted at the location
2153 we're trying to step if the instruction branches to itself.
2154 However, the instruction won't be executed at all and it may
2155 break the semantics of the instruction, for example, the
2156 instruction is a conditional branch or updates some flags.
2157 We can't fix it unless GDB is able to emulate the instruction
2158 or switch to displaced stepping. */
2159 && !(bl->owner->type == bp_single_step
2160 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2161 {
2162 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2163 paddress (bl->gdbarch, bl->address));
2164 return 0;
2165 }
2166
2167 /* Don't insert watchpoints if we're trying to step past the
2168 instruction that triggered one. */
2169 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2170 && stepping_past_nonsteppable_watchpoint ())
2171 {
2172 infrun_debug_printf ("stepping past non-steppable watchpoint. "
2173 "skipping watchpoint at %s:%d",
2174 paddress (bl->gdbarch, bl->address), bl->length);
2175 return 0;
2176 }
2177
2178 return 1;
2179 }
2180
2181 /* Same as should_be_inserted but does the check assuming
2182 that the location is not duplicated. */
2183
2184 static int
2185 unduplicated_should_be_inserted (struct bp_location *bl)
2186 {
2187 int result;
2188 const int save_duplicate = bl->duplicate;
2189
2190 bl->duplicate = 0;
2191 result = should_be_inserted (bl);
2192 bl->duplicate = save_duplicate;
2193 return result;
2194 }
2195
2196 /* Parses a conditional described by an expression COND into an
2197 agent expression bytecode suitable for evaluation
2198 by the bytecode interpreter. Return NULL if there was
2199 any error during parsing. */
2200
2201 static agent_expr_up
2202 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2203 {
2204 if (cond == NULL)
2205 return NULL;
2206
2207 agent_expr_up aexpr;
2208
2209 /* We don't want to stop processing, so catch any errors
2210 that may show up. */
2211 try
2212 {
2213 aexpr = gen_eval_for_expr (scope, cond);
2214 }
2215
2216 catch (const gdb_exception_error &ex)
2217 {
2218 /* If we got here, it means the condition could not be parsed to a valid
2219 bytecode expression and thus can't be evaluated on the target's side.
2220 It's no use iterating through the conditions. */
2221 }
2222
2223 /* We have a valid agent expression. */
2224 return aexpr;
2225 }
2226
2227 /* Based on location BL, create a list of breakpoint conditions to be
2228 passed on to the target. If we have duplicated locations with different
2229 conditions, we will add such conditions to the list. The idea is that the
2230 target will evaluate the list of conditions and will only notify GDB when
2231 one of them is true. */
2232
2233 static void
2234 build_target_condition_list (struct bp_location *bl)
2235 {
2236 int null_condition_or_parse_error = 0;
2237 int modified = bl->needs_update;
2238
2239 /* Release conditions left over from a previous insert. */
2240 bl->target_info.conditions.clear ();
2241
2242 /* This is only meaningful if the target is
2243 evaluating conditions and if the user has
2244 opted for condition evaluation on the target's
2245 side. */
2246 if (gdb_evaluates_breakpoint_condition_p ()
2247 || !target_supports_evaluation_of_breakpoint_conditions ())
2248 return;
2249
2250 auto loc_range = all_bp_locations_at_addr (bl->address);
2251
2252 /* Do a first pass to check for locations with no assigned
2253 conditions or conditions that fail to parse to a valid agent
2254 expression bytecode. If any of these happen, then it's no use to
2255 send conditions to the target since this location will always
2256 trigger and generate a response back to GDB. Note we consider
2257 all locations at the same address irrespective of type, i.e.,
2258 even if the locations aren't considered duplicates (e.g.,
2259 software breakpoint and hardware breakpoint at the same
2260 address). */
2261 for (bp_location *loc : loc_range)
2262 {
2263 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2264 {
2265 if (modified)
2266 {
2267 /* Re-parse the conditions since something changed. In that
2268 case we already freed the condition bytecodes (see
2269 force_breakpoint_reinsertion). We just
2270 need to parse the condition to bytecodes again. */
2271 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2272 loc->cond.get ());
2273 }
2274
2275 /* If we have a NULL bytecode expression, it means something
2276 went wrong or we have a null condition expression. */
2277 if (!loc->cond_bytecode)
2278 {
2279 null_condition_or_parse_error = 1;
2280 break;
2281 }
2282 }
2283 }
2284
2285 /* If any of these happened, it means we will have to evaluate the conditions
2286 for the location's address on gdb's side. It is no use keeping bytecodes
2287 for all the other duplicate locations, thus we free all of them here.
2288
2289 This is so we have a finer control over which locations' conditions are
2290 being evaluated by GDB or the remote stub. */
2291 if (null_condition_or_parse_error)
2292 {
2293 for (bp_location *loc : loc_range)
2294 {
2295 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2296 {
2297 /* Only go as far as the first NULL bytecode is
2298 located. */
2299 if (!loc->cond_bytecode)
2300 return;
2301
2302 loc->cond_bytecode.reset ();
2303 }
2304 }
2305 }
2306
2307 /* No NULL conditions or failed bytecode generation. Build a
2308 condition list for this location's address. If we have software
2309 and hardware locations at the same address, they aren't
2310 considered duplicates, but we still marge all the conditions
2311 anyway, as it's simpler, and doesn't really make a practical
2312 difference. */
2313 for (bp_location *loc : loc_range)
2314 if (loc->cond
2315 && is_breakpoint (loc->owner)
2316 && loc->pspace->num == bl->pspace->num
2317 && loc->owner->enable_state == bp_enabled
2318 && loc->enabled
2319 && !loc->disabled_by_cond)
2320 {
2321 /* Add the condition to the vector. This will be used later
2322 to send the conditions to the target. */
2323 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2324 }
2325
2326 return;
2327 }
2328
2329 /* Parses a command described by string CMD into an agent expression
2330 bytecode suitable for evaluation by the bytecode interpreter.
2331 Return NULL if there was any error during parsing. */
2332
2333 static agent_expr_up
2334 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2335 {
2336 const char *cmdrest;
2337 const char *format_start, *format_end;
2338 struct gdbarch *gdbarch = get_current_arch ();
2339
2340 if (cmd == NULL)
2341 return NULL;
2342
2343 cmdrest = cmd;
2344
2345 if (*cmdrest == ',')
2346 ++cmdrest;
2347 cmdrest = skip_spaces (cmdrest);
2348
2349 if (*cmdrest++ != '"')
2350 error (_("No format string following the location"));
2351
2352 format_start = cmdrest;
2353
2354 format_pieces fpieces (&cmdrest);
2355
2356 format_end = cmdrest;
2357
2358 if (*cmdrest++ != '"')
2359 error (_("Bad format string, non-terminated '\"'."));
2360
2361 cmdrest = skip_spaces (cmdrest);
2362
2363 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2364 error (_("Invalid argument syntax"));
2365
2366 if (*cmdrest == ',')
2367 cmdrest++;
2368 cmdrest = skip_spaces (cmdrest);
2369
2370 /* For each argument, make an expression. */
2371
2372 std::vector<struct expression *> argvec;
2373 while (*cmdrest != '\0')
2374 {
2375 const char *cmd1;
2376
2377 cmd1 = cmdrest;
2378 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2379 argvec.push_back (expr.release ());
2380 cmdrest = cmd1;
2381 if (*cmdrest == ',')
2382 ++cmdrest;
2383 }
2384
2385 agent_expr_up aexpr;
2386
2387 /* We don't want to stop processing, so catch any errors
2388 that may show up. */
2389 try
2390 {
2391 aexpr = gen_printf (scope, gdbarch, 0, 0,
2392 format_start, format_end - format_start,
2393 argvec.size (), argvec.data ());
2394 }
2395 catch (const gdb_exception_error &ex)
2396 {
2397 /* If we got here, it means the command could not be parsed to a valid
2398 bytecode expression and thus can't be evaluated on the target's side.
2399 It's no use iterating through the other commands. */
2400 }
2401
2402 /* We have a valid agent expression, return it. */
2403 return aexpr;
2404 }
2405
2406 /* Based on location BL, create a list of breakpoint commands to be
2407 passed on to the target. If we have duplicated locations with
2408 different commands, we will add any such to the list. */
2409
2410 static void
2411 build_target_command_list (struct bp_location *bl)
2412 {
2413 int null_command_or_parse_error = 0;
2414 int modified = bl->needs_update;
2415
2416 /* Clear commands left over from a previous insert. */
2417 bl->target_info.tcommands.clear ();
2418
2419 if (!target_can_run_breakpoint_commands ())
2420 return;
2421
2422 /* For now, limit to agent-style dprintf breakpoints. */
2423 if (dprintf_style != dprintf_style_agent)
2424 return;
2425
2426 auto loc_range = all_bp_locations_at_addr (bl->address);
2427
2428 /* For now, if we have any location at the same address that isn't a
2429 dprintf, don't install the target-side commands, as that would
2430 make the breakpoint not be reported to the core, and we'd lose
2431 control. */
2432 for (bp_location *loc : loc_range)
2433 if (is_breakpoint (loc->owner)
2434 && loc->pspace->num == bl->pspace->num
2435 && loc->owner->type != bp_dprintf)
2436 return;
2437
2438 /* Do a first pass to check for locations with no assigned
2439 conditions or conditions that fail to parse to a valid agent expression
2440 bytecode. If any of these happen, then it's no use to send conditions
2441 to the target since this location will always trigger and generate a
2442 response back to GDB. */
2443 for (bp_location *loc : loc_range)
2444 {
2445 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2446 {
2447 if (modified)
2448 {
2449 /* Re-parse the commands since something changed. In that
2450 case we already freed the command bytecodes (see
2451 force_breakpoint_reinsertion). We just
2452 need to parse the command to bytecodes again. */
2453 loc->cmd_bytecode
2454 = parse_cmd_to_aexpr (bl->address,
2455 loc->owner->extra_string);
2456 }
2457
2458 /* If we have a NULL bytecode expression, it means something
2459 went wrong or we have a null command expression. */
2460 if (!loc->cmd_bytecode)
2461 {
2462 null_command_or_parse_error = 1;
2463 break;
2464 }
2465 }
2466 }
2467
2468 /* If anything failed, then we're not doing target-side commands,
2469 and so clean up. */
2470 if (null_command_or_parse_error)
2471 {
2472 for (bp_location *loc : loc_range)
2473 if (is_breakpoint (loc->owner)
2474 && loc->pspace->num == bl->pspace->num)
2475 {
2476 /* Only go as far as the first NULL bytecode is
2477 located. */
2478 if (loc->cmd_bytecode == NULL)
2479 return;
2480
2481 loc->cmd_bytecode.reset ();
2482 }
2483 }
2484
2485 /* No NULL commands or failed bytecode generation. Build a command
2486 list for all duplicate locations at this location's address.
2487 Note that here we must care for whether the breakpoint location
2488 types are considered duplicates, otherwise, say, if we have a
2489 software and hardware location at the same address, the target
2490 could end up running the commands twice. For the moment, we only
2491 support targets-side commands with dprintf, but it doesn't hurt
2492 to be pedantically correct in case that changes. */
2493 for (bp_location *loc : loc_range)
2494 if (breakpoint_locations_match (bl, loc)
2495 && loc->owner->extra_string
2496 && is_breakpoint (loc->owner)
2497 && loc->pspace->num == bl->pspace->num
2498 && loc->owner->enable_state == bp_enabled
2499 && loc->enabled
2500 && !loc->disabled_by_cond)
2501 {
2502 /* Add the command to the vector. This will be used later
2503 to send the commands to the target. */
2504 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2505 }
2506
2507 bl->target_info.persist = 0;
2508 /* Maybe flag this location as persistent. */
2509 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2510 bl->target_info.persist = 1;
2511 }
2512
2513 /* Return the kind of breakpoint on address *ADDR. Get the kind
2514 of breakpoint according to ADDR except single-step breakpoint.
2515 Get the kind of single-step breakpoint according to the current
2516 registers state. */
2517
2518 static int
2519 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2520 {
2521 if (bl->owner->type == bp_single_step)
2522 {
2523 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2524 struct regcache *regcache;
2525
2526 regcache = get_thread_regcache (thr);
2527
2528 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2529 regcache, addr);
2530 }
2531 else
2532 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2533 }
2534
2535 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2536 location. Any error messages are printed to TMP_ERROR_STREAM; and
2537 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2538 Returns 0 for success, 1 if the bp_location type is not supported or
2539 -1 for failure.
2540
2541 NOTE drow/2003-09-09: This routine could be broken down to an
2542 object-style method for each breakpoint or catchpoint type. */
2543 static int
2544 insert_bp_location (struct bp_location *bl,
2545 struct ui_file *tmp_error_stream,
2546 int *disabled_breaks,
2547 int *hw_breakpoint_error,
2548 int *hw_bp_error_explained_already)
2549 {
2550 gdb_exception bp_excpt;
2551
2552 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2553 return 0;
2554
2555 /* Note we don't initialize bl->target_info, as that wipes out
2556 the breakpoint location's shadow_contents if the breakpoint
2557 is still inserted at that location. This in turn breaks
2558 target_read_memory which depends on these buffers when
2559 a memory read is requested at the breakpoint location:
2560 Once the target_info has been wiped, we fail to see that
2561 we have a breakpoint inserted at that address and thus
2562 read the breakpoint instead of returning the data saved in
2563 the breakpoint location's shadow contents. */
2564 bl->target_info.reqstd_address = bl->address;
2565 bl->target_info.placed_address_space = bl->pspace->aspace;
2566 bl->target_info.length = bl->length;
2567
2568 /* When working with target-side conditions, we must pass all the conditions
2569 for the same breakpoint address down to the target since GDB will not
2570 insert those locations. With a list of breakpoint conditions, the target
2571 can decide when to stop and notify GDB. */
2572
2573 if (is_breakpoint (bl->owner))
2574 {
2575 build_target_condition_list (bl);
2576 build_target_command_list (bl);
2577 /* Reset the modification marker. */
2578 bl->needs_update = 0;
2579 }
2580
2581 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2582 set at a read-only address, then a breakpoint location will have
2583 been changed to hardware breakpoint before we get here. If it is
2584 "off" however, error out before actually trying to insert the
2585 breakpoint, with a nicer error message. */
2586 if (bl->loc_type == bp_loc_software_breakpoint
2587 && !automatic_hardware_breakpoints)
2588 {
2589 mem_region *mr = lookup_mem_region (bl->address);
2590
2591 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2592 {
2593 fprintf_unfiltered (tmp_error_stream,
2594 _("Cannot insert breakpoint %d.\n"
2595 "Cannot set software breakpoint "
2596 "at read-only address %s\n"),
2597 bl->owner->number,
2598 paddress (bl->gdbarch, bl->address));
2599 return 1;
2600 }
2601 }
2602
2603 if (bl->loc_type == bp_loc_software_breakpoint
2604 || bl->loc_type == bp_loc_hardware_breakpoint)
2605 {
2606 /* First check to see if we have to handle an overlay. */
2607 if (overlay_debugging == ovly_off
2608 || bl->section == NULL
2609 || !(section_is_overlay (bl->section)))
2610 {
2611 /* No overlay handling: just set the breakpoint. */
2612 try
2613 {
2614 int val;
2615
2616 val = bl->owner->ops->insert_location (bl);
2617 if (val)
2618 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2619 }
2620 catch (gdb_exception &e)
2621 {
2622 bp_excpt = std::move (e);
2623 }
2624 }
2625 else
2626 {
2627 /* This breakpoint is in an overlay section.
2628 Shall we set a breakpoint at the LMA? */
2629 if (!overlay_events_enabled)
2630 {
2631 /* Yes -- overlay event support is not active,
2632 so we must try to set a breakpoint at the LMA.
2633 This will not work for a hardware breakpoint. */
2634 if (bl->loc_type == bp_loc_hardware_breakpoint)
2635 warning (_("hardware breakpoint %d not supported in overlay!"),
2636 bl->owner->number);
2637 else
2638 {
2639 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2640 bl->section);
2641 /* Set a software (trap) breakpoint at the LMA. */
2642 bl->overlay_target_info = bl->target_info;
2643 bl->overlay_target_info.reqstd_address = addr;
2644
2645 /* No overlay handling: just set the breakpoint. */
2646 try
2647 {
2648 int val;
2649
2650 bl->overlay_target_info.kind
2651 = breakpoint_kind (bl, &addr);
2652 bl->overlay_target_info.placed_address = addr;
2653 val = target_insert_breakpoint (bl->gdbarch,
2654 &bl->overlay_target_info);
2655 if (val)
2656 bp_excpt
2657 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2658 }
2659 catch (gdb_exception &e)
2660 {
2661 bp_excpt = std::move (e);
2662 }
2663
2664 if (bp_excpt.reason != 0)
2665 fprintf_unfiltered (tmp_error_stream,
2666 "Overlay breakpoint %d "
2667 "failed: in ROM?\n",
2668 bl->owner->number);
2669 }
2670 }
2671 /* Shall we set a breakpoint at the VMA? */
2672 if (section_is_mapped (bl->section))
2673 {
2674 /* Yes. This overlay section is mapped into memory. */
2675 try
2676 {
2677 int val;
2678
2679 val = bl->owner->ops->insert_location (bl);
2680 if (val)
2681 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2682 }
2683 catch (gdb_exception &e)
2684 {
2685 bp_excpt = std::move (e);
2686 }
2687 }
2688 else
2689 {
2690 /* No. This breakpoint will not be inserted.
2691 No error, but do not mark the bp as 'inserted'. */
2692 return 0;
2693 }
2694 }
2695
2696 if (bp_excpt.reason != 0)
2697 {
2698 /* Can't set the breakpoint. */
2699
2700 /* In some cases, we might not be able to insert a
2701 breakpoint in a shared library that has already been
2702 removed, but we have not yet processed the shlib unload
2703 event. Unfortunately, some targets that implement
2704 breakpoint insertion themselves can't tell why the
2705 breakpoint insertion failed (e.g., the remote target
2706 doesn't define error codes), so we must treat generic
2707 errors as memory errors. */
2708 if (bp_excpt.reason == RETURN_ERROR
2709 && (bp_excpt.error == GENERIC_ERROR
2710 || bp_excpt.error == MEMORY_ERROR)
2711 && bl->loc_type == bp_loc_software_breakpoint
2712 && (solib_name_from_address (bl->pspace, bl->address)
2713 || shared_objfile_contains_address_p (bl->pspace,
2714 bl->address)))
2715 {
2716 /* See also: disable_breakpoints_in_shlibs. */
2717 bl->shlib_disabled = 1;
2718 gdb::observers::breakpoint_modified.notify (bl->owner);
2719 if (!*disabled_breaks)
2720 {
2721 fprintf_unfiltered (tmp_error_stream,
2722 "Cannot insert breakpoint %d.\n",
2723 bl->owner->number);
2724 fprintf_unfiltered (tmp_error_stream,
2725 "Temporarily disabling shared "
2726 "library breakpoints:\n");
2727 }
2728 *disabled_breaks = 1;
2729 fprintf_unfiltered (tmp_error_stream,
2730 "breakpoint #%d\n", bl->owner->number);
2731 return 0;
2732 }
2733 else
2734 {
2735 if (bl->loc_type == bp_loc_hardware_breakpoint)
2736 {
2737 *hw_breakpoint_error = 1;
2738 *hw_bp_error_explained_already = bp_excpt.message != NULL;
2739 fprintf_unfiltered (tmp_error_stream,
2740 "Cannot insert hardware breakpoint %d%s",
2741 bl->owner->number,
2742 bp_excpt.message ? ":" : ".\n");
2743 if (bp_excpt.message != NULL)
2744 fprintf_unfiltered (tmp_error_stream, "%s.\n",
2745 bp_excpt.what ());
2746 }
2747 else
2748 {
2749 if (bp_excpt.message == NULL)
2750 {
2751 std::string message
2752 = memory_error_message (TARGET_XFER_E_IO,
2753 bl->gdbarch, bl->address);
2754
2755 fprintf_unfiltered (tmp_error_stream,
2756 "Cannot insert breakpoint %d.\n"
2757 "%s\n",
2758 bl->owner->number, message.c_str ());
2759 }
2760 else
2761 {
2762 fprintf_unfiltered (tmp_error_stream,
2763 "Cannot insert breakpoint %d: %s\n",
2764 bl->owner->number,
2765 bp_excpt.what ());
2766 }
2767 }
2768 return 1;
2769
2770 }
2771 }
2772 else
2773 bl->inserted = 1;
2774
2775 return 0;
2776 }
2777
2778 else if (bl->loc_type == bp_loc_hardware_watchpoint
2779 /* NOTE drow/2003-09-08: This state only exists for removing
2780 watchpoints. It's not clear that it's necessary... */
2781 && bl->owner->disposition != disp_del_at_next_stop)
2782 {
2783 int val;
2784
2785 gdb_assert (bl->owner->ops != NULL
2786 && bl->owner->ops->insert_location != NULL);
2787
2788 val = bl->owner->ops->insert_location (bl);
2789
2790 /* If trying to set a read-watchpoint, and it turns out it's not
2791 supported, try emulating one with an access watchpoint. */
2792 if (val == 1 && bl->watchpoint_type == hw_read)
2793 {
2794 /* But don't try to insert it, if there's already another
2795 hw_access location that would be considered a duplicate
2796 of this one. */
2797 for (bp_location *loc : all_bp_locations ())
2798 if (loc != bl
2799 && loc->watchpoint_type == hw_access
2800 && watchpoint_locations_match (bl, loc))
2801 {
2802 bl->duplicate = 1;
2803 bl->inserted = 1;
2804 bl->target_info = loc->target_info;
2805 bl->watchpoint_type = hw_access;
2806 val = 0;
2807 break;
2808 }
2809
2810 if (val == 1)
2811 {
2812 bl->watchpoint_type = hw_access;
2813 val = bl->owner->ops->insert_location (bl);
2814
2815 if (val)
2816 /* Back to the original value. */
2817 bl->watchpoint_type = hw_read;
2818 }
2819 }
2820
2821 bl->inserted = (val == 0);
2822 }
2823
2824 else if (bl->owner->type == bp_catchpoint)
2825 {
2826 int val;
2827
2828 gdb_assert (bl->owner->ops != NULL
2829 && bl->owner->ops->insert_location != NULL);
2830
2831 val = bl->owner->ops->insert_location (bl);
2832 if (val)
2833 {
2834 bl->owner->enable_state = bp_disabled;
2835
2836 if (val == 1)
2837 warning (_("\
2838 Error inserting catchpoint %d: Your system does not support this type\n\
2839 of catchpoint."), bl->owner->number);
2840 else
2841 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2842 }
2843
2844 bl->inserted = (val == 0);
2845
2846 /* We've already printed an error message if there was a problem
2847 inserting this catchpoint, and we've disabled the catchpoint,
2848 so just return success. */
2849 return 0;
2850 }
2851
2852 return 0;
2853 }
2854
2855 /* This function is called when program space PSPACE is about to be
2856 deleted. It takes care of updating breakpoints to not reference
2857 PSPACE anymore. */
2858
2859 void
2860 breakpoint_program_space_exit (struct program_space *pspace)
2861 {
2862 /* Remove any breakpoint that was set through this program space. */
2863 for (breakpoint *b : all_breakpoints_safe ())
2864 if (b->pspace == pspace)
2865 delete_breakpoint (b);
2866
2867 /* Breakpoints set through other program spaces could have locations
2868 bound to PSPACE as well. Remove those. */
2869 for (bp_location *loc : all_bp_locations ())
2870 {
2871 struct bp_location *tmp;
2872
2873 if (loc->pspace == pspace)
2874 {
2875 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2876 if (loc->owner->loc == loc)
2877 loc->owner->loc = loc->next;
2878 else
2879 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2880 if (tmp->next == loc)
2881 {
2882 tmp->next = loc->next;
2883 break;
2884 }
2885 }
2886 }
2887
2888 /* Now update the global location list to permanently delete the
2889 removed locations above. */
2890 update_global_location_list (UGLL_DONT_INSERT);
2891 }
2892
2893 /* Make sure all breakpoints are inserted in inferior.
2894 Throws exception on any error.
2895 A breakpoint that is already inserted won't be inserted
2896 again, so calling this function twice is safe. */
2897 void
2898 insert_breakpoints (void)
2899 {
2900 for (breakpoint *bpt : all_breakpoints ())
2901 if (is_hardware_watchpoint (bpt))
2902 {
2903 struct watchpoint *w = (struct watchpoint *) bpt;
2904
2905 update_watchpoint (w, 0 /* don't reparse. */);
2906 }
2907
2908 /* Updating watchpoints creates new locations, so update the global
2909 location list. Explicitly tell ugll to insert locations and
2910 ignore breakpoints_always_inserted_mode. Also,
2911 update_global_location_list tries to "upgrade" software
2912 breakpoints to hardware breakpoints to handle "set breakpoint
2913 auto-hw", so we need to call it even if we don't have new
2914 locations. */
2915 update_global_location_list (UGLL_INSERT);
2916 }
2917
2918 /* This is used when we need to synch breakpoint conditions between GDB and the
2919 target. It is the case with deleting and disabling of breakpoints when using
2920 always-inserted mode. */
2921
2922 static void
2923 update_inserted_breakpoint_locations (void)
2924 {
2925 int error_flag = 0;
2926 int val = 0;
2927 int disabled_breaks = 0;
2928 int hw_breakpoint_error = 0;
2929 int hw_bp_details_reported = 0;
2930
2931 string_file tmp_error_stream;
2932
2933 /* Explicitly mark the warning -- this will only be printed if
2934 there was an error. */
2935 tmp_error_stream.puts ("Warning:\n");
2936
2937 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2938
2939 for (bp_location *bl : all_bp_locations ())
2940 {
2941 /* We only want to update software breakpoints and hardware
2942 breakpoints. */
2943 if (!is_breakpoint (bl->owner))
2944 continue;
2945
2946 /* We only want to update locations that are already inserted
2947 and need updating. This is to avoid unwanted insertion during
2948 deletion of breakpoints. */
2949 if (!bl->inserted || !bl->needs_update)
2950 continue;
2951
2952 switch_to_program_space_and_thread (bl->pspace);
2953
2954 /* For targets that support global breakpoints, there's no need
2955 to select an inferior to insert breakpoint to. In fact, even
2956 if we aren't attached to any process yet, we should still
2957 insert breakpoints. */
2958 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2959 && (inferior_ptid == null_ptid || !target_has_execution ()))
2960 continue;
2961
2962 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
2963 &hw_breakpoint_error, &hw_bp_details_reported);
2964 if (val)
2965 error_flag = val;
2966 }
2967
2968 if (error_flag)
2969 {
2970 target_terminal::ours_for_output ();
2971 error_stream (tmp_error_stream);
2972 }
2973 }
2974
2975 /* Used when starting or continuing the program. */
2976
2977 static void
2978 insert_breakpoint_locations (void)
2979 {
2980 int error_flag = 0;
2981 int val = 0;
2982 int disabled_breaks = 0;
2983 int hw_breakpoint_error = 0;
2984 int hw_bp_error_explained_already = 0;
2985
2986 string_file tmp_error_stream;
2987
2988 /* Explicitly mark the warning -- this will only be printed if
2989 there was an error. */
2990 tmp_error_stream.puts ("Warning:\n");
2991
2992 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2993
2994 for (bp_location *bl : all_bp_locations ())
2995 {
2996 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2997 continue;
2998
2999 /* There is no point inserting thread-specific breakpoints if
3000 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3001 has BL->OWNER always non-NULL. */
3002 if (bl->owner->thread != -1
3003 && !valid_global_thread_id (bl->owner->thread))
3004 continue;
3005
3006 switch_to_program_space_and_thread (bl->pspace);
3007
3008 /* For targets that support global breakpoints, there's no need
3009 to select an inferior to insert breakpoint to. In fact, even
3010 if we aren't attached to any process yet, we should still
3011 insert breakpoints. */
3012 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3013 && (inferior_ptid == null_ptid || !target_has_execution ()))
3014 continue;
3015
3016 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3017 &hw_breakpoint_error, &hw_bp_error_explained_already);
3018 if (val)
3019 error_flag = val;
3020 }
3021
3022 /* If we failed to insert all locations of a watchpoint, remove
3023 them, as half-inserted watchpoint is of limited use. */
3024 for (breakpoint *bpt : all_breakpoints ())
3025 {
3026 int some_failed = 0;
3027
3028 if (!is_hardware_watchpoint (bpt))
3029 continue;
3030
3031 if (!breakpoint_enabled (bpt))
3032 continue;
3033
3034 if (bpt->disposition == disp_del_at_next_stop)
3035 continue;
3036
3037 for (bp_location *loc : bpt->locations ())
3038 if (!loc->inserted && should_be_inserted (loc))
3039 {
3040 some_failed = 1;
3041 break;
3042 }
3043
3044 if (some_failed)
3045 {
3046 for (bp_location *loc : bpt->locations ())
3047 if (loc->inserted)
3048 remove_breakpoint (loc);
3049
3050 hw_breakpoint_error = 1;
3051 tmp_error_stream.printf ("Could not insert "
3052 "hardware watchpoint %d.\n",
3053 bpt->number);
3054 error_flag = -1;
3055 }
3056 }
3057
3058 if (error_flag)
3059 {
3060 /* If a hardware breakpoint or watchpoint was inserted, add a
3061 message about possibly exhausted resources. */
3062 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3063 {
3064 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3065 You may have requested too many hardware breakpoints/watchpoints.\n");
3066 }
3067 target_terminal::ours_for_output ();
3068 error_stream (tmp_error_stream);
3069 }
3070 }
3071
3072 /* Used when the program stops.
3073 Returns zero if successful, or non-zero if there was a problem
3074 removing a breakpoint location. */
3075
3076 int
3077 remove_breakpoints (void)
3078 {
3079 int val = 0;
3080
3081 for (bp_location *bl : all_bp_locations ())
3082 {
3083 if (bl->inserted && !is_tracepoint (bl->owner))
3084 val |= remove_breakpoint (bl);
3085 }
3086 return val;
3087 }
3088
3089 /* When a thread exits, remove breakpoints that are related to
3090 that thread. */
3091
3092 static void
3093 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3094 {
3095 for (breakpoint *b : all_breakpoints_safe ())
3096 {
3097 if (b->thread == tp->global_num && user_breakpoint_p (b))
3098 {
3099 b->disposition = disp_del_at_next_stop;
3100
3101 printf_filtered (_("\
3102 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3103 b->number, print_thread_id (tp));
3104
3105 /* Hide it from the user. */
3106 b->number = 0;
3107 }
3108 }
3109 }
3110
3111 /* See breakpoint.h. */
3112
3113 void
3114 remove_breakpoints_inf (inferior *inf)
3115 {
3116 int val;
3117
3118 for (bp_location *bl : all_bp_locations ())
3119 {
3120 if (bl->pspace != inf->pspace)
3121 continue;
3122
3123 if (bl->inserted && !bl->target_info.persist)
3124 {
3125 val = remove_breakpoint (bl);
3126 if (val != 0)
3127 return;
3128 }
3129 }
3130 }
3131
3132 static int internal_breakpoint_number = -1;
3133
3134 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3135 If INTERNAL is non-zero, the breakpoint number will be populated
3136 from internal_breakpoint_number and that variable decremented.
3137 Otherwise the breakpoint number will be populated from
3138 breakpoint_count and that value incremented. Internal breakpoints
3139 do not set the internal var bpnum. */
3140 static void
3141 set_breakpoint_number (int internal, struct breakpoint *b)
3142 {
3143 if (internal)
3144 b->number = internal_breakpoint_number--;
3145 else
3146 {
3147 set_breakpoint_count (breakpoint_count + 1);
3148 b->number = breakpoint_count;
3149 }
3150 }
3151
3152 static struct breakpoint *
3153 create_internal_breakpoint (struct gdbarch *gdbarch,
3154 CORE_ADDR address, enum bptype type,
3155 const struct breakpoint_ops *ops)
3156 {
3157 symtab_and_line sal;
3158 sal.pc = address;
3159 sal.section = find_pc_overlay (sal.pc);
3160 sal.pspace = current_program_space;
3161
3162 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
3163 b->number = internal_breakpoint_number--;
3164 b->disposition = disp_donttouch;
3165
3166 return b;
3167 }
3168
3169 static const char *const longjmp_names[] =
3170 {
3171 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3172 };
3173 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3174
3175 /* Per-objfile data private to breakpoint.c. */
3176 struct breakpoint_objfile_data
3177 {
3178 /* Minimal symbol for "_ovly_debug_event" (if any). */
3179 struct bound_minimal_symbol overlay_msym {};
3180
3181 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3182 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
3183
3184 /* True if we have looked for longjmp probes. */
3185 int longjmp_searched = 0;
3186
3187 /* SystemTap probe points for longjmp (if any). These are non-owning
3188 references. */
3189 std::vector<probe *> longjmp_probes;
3190
3191 /* Minimal symbol for "std::terminate()" (if any). */
3192 struct bound_minimal_symbol terminate_msym {};
3193
3194 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3195 struct bound_minimal_symbol exception_msym {};
3196
3197 /* True if we have looked for exception probes. */
3198 int exception_searched = 0;
3199
3200 /* SystemTap probe points for unwinding (if any). These are non-owning
3201 references. */
3202 std::vector<probe *> exception_probes;
3203 };
3204
3205 static const struct objfile_key<breakpoint_objfile_data>
3206 breakpoint_objfile_key;
3207
3208 /* Minimal symbol not found sentinel. */
3209 static struct minimal_symbol msym_not_found;
3210
3211 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3212
3213 static int
3214 msym_not_found_p (const struct minimal_symbol *msym)
3215 {
3216 return msym == &msym_not_found;
3217 }
3218
3219 /* Return per-objfile data needed by breakpoint.c.
3220 Allocate the data if necessary. */
3221
3222 static struct breakpoint_objfile_data *
3223 get_breakpoint_objfile_data (struct objfile *objfile)
3224 {
3225 struct breakpoint_objfile_data *bp_objfile_data;
3226
3227 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3228 if (bp_objfile_data == NULL)
3229 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3230 return bp_objfile_data;
3231 }
3232
3233 static void
3234 create_overlay_event_breakpoint (void)
3235 {
3236 const char *const func_name = "_ovly_debug_event";
3237
3238 for (objfile *objfile : current_program_space->objfiles ())
3239 {
3240 struct breakpoint *b;
3241 struct breakpoint_objfile_data *bp_objfile_data;
3242 CORE_ADDR addr;
3243 struct explicit_location explicit_loc;
3244
3245 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3246
3247 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3248 continue;
3249
3250 if (bp_objfile_data->overlay_msym.minsym == NULL)
3251 {
3252 struct bound_minimal_symbol m;
3253
3254 m = lookup_minimal_symbol_text (func_name, objfile);
3255 if (m.minsym == NULL)
3256 {
3257 /* Avoid future lookups in this objfile. */
3258 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3259 continue;
3260 }
3261 bp_objfile_data->overlay_msym = m;
3262 }
3263
3264 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3265 b = create_internal_breakpoint (objfile->arch (), addr,
3266 bp_overlay_event,
3267 &internal_breakpoint_ops);
3268 initialize_explicit_location (&explicit_loc);
3269 explicit_loc.function_name = ASTRDUP (func_name);
3270 b->location = new_explicit_location (&explicit_loc);
3271
3272 if (overlay_debugging == ovly_auto)
3273 {
3274 b->enable_state = bp_enabled;
3275 overlay_events_enabled = 1;
3276 }
3277 else
3278 {
3279 b->enable_state = bp_disabled;
3280 overlay_events_enabled = 0;
3281 }
3282 }
3283 }
3284
3285 /* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3286 true if a breakpoint was installed. */
3287
3288 static bool
3289 create_longjmp_master_breakpoint_probe (objfile *objfile)
3290 {
3291 struct gdbarch *gdbarch = objfile->arch ();
3292 struct breakpoint_objfile_data *bp_objfile_data
3293 = get_breakpoint_objfile_data (objfile);
3294
3295 if (!bp_objfile_data->longjmp_searched)
3296 {
3297 std::vector<probe *> ret
3298 = find_probes_in_objfile (objfile, "libc", "longjmp");
3299
3300 if (!ret.empty ())
3301 {
3302 /* We are only interested in checking one element. */
3303 probe *p = ret[0];
3304
3305 if (!p->can_evaluate_arguments ())
3306 {
3307 /* We cannot use the probe interface here,
3308 because it does not know how to evaluate
3309 arguments. */
3310 ret.clear ();
3311 }
3312 }
3313 bp_objfile_data->longjmp_probes = ret;
3314 bp_objfile_data->longjmp_searched = 1;
3315 }
3316
3317 if (bp_objfile_data->longjmp_probes.empty ())
3318 return false;
3319
3320 for (probe *p : bp_objfile_data->longjmp_probes)
3321 {
3322 struct breakpoint *b;
3323
3324 b = create_internal_breakpoint (gdbarch,
3325 p->get_relocated_address (objfile),
3326 bp_longjmp_master,
3327 &internal_breakpoint_ops);
3328 b->location = new_probe_location ("-probe-stap libc:longjmp");
3329 b->enable_state = bp_disabled;
3330 }
3331
3332 return true;
3333 }
3334
3335 /* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3336 Return true if at least one breakpoint was installed. */
3337
3338 static bool
3339 create_longjmp_master_breakpoint_names (objfile *objfile)
3340 {
3341 struct gdbarch *gdbarch = objfile->arch ();
3342 if (!gdbarch_get_longjmp_target_p (gdbarch))
3343 return false;
3344
3345 struct breakpoint_objfile_data *bp_objfile_data
3346 = get_breakpoint_objfile_data (objfile);
3347 unsigned int installed_bp = 0;
3348
3349 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3350 {
3351 struct breakpoint *b;
3352 const char *func_name;
3353 CORE_ADDR addr;
3354 struct explicit_location explicit_loc;
3355
3356 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3357 continue;
3358
3359 func_name = longjmp_names[i];
3360 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3361 {
3362 struct bound_minimal_symbol m;
3363
3364 m = lookup_minimal_symbol_text (func_name, objfile);
3365 if (m.minsym == NULL)
3366 {
3367 /* Prevent future lookups in this objfile. */
3368 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3369 continue;
3370 }
3371 bp_objfile_data->longjmp_msym[i] = m;
3372 }
3373
3374 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3375 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3376 &internal_breakpoint_ops);
3377 initialize_explicit_location (&explicit_loc);
3378 explicit_loc.function_name = ASTRDUP (func_name);
3379 b->location = new_explicit_location (&explicit_loc);
3380 b->enable_state = bp_disabled;
3381 installed_bp++;
3382 }
3383
3384 return installed_bp > 0;
3385 }
3386
3387 /* Create a master longjmp breakpoint. */
3388
3389 static void
3390 create_longjmp_master_breakpoint (void)
3391 {
3392 scoped_restore_current_program_space restore_pspace;
3393
3394 for (struct program_space *pspace : program_spaces)
3395 {
3396 set_current_program_space (pspace);
3397
3398 for (objfile *obj : current_program_space->objfiles ())
3399 {
3400 /* Skip separate debug object, it's handled in the loop below. */
3401 if (obj->separate_debug_objfile_backlink != nullptr)
3402 continue;
3403
3404 /* Try a probe kind breakpoint on main objfile. */
3405 if (create_longjmp_master_breakpoint_probe (obj))
3406 continue;
3407
3408 /* Try longjmp_names kind breakpoints on main and separate_debug
3409 objfiles. */
3410 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3411 if (create_longjmp_master_breakpoint_names (debug_objfile))
3412 break;
3413 }
3414 }
3415 }
3416
3417 /* Create a master std::terminate breakpoint. */
3418 static void
3419 create_std_terminate_master_breakpoint (void)
3420 {
3421 const char *const func_name = "std::terminate()";
3422
3423 scoped_restore_current_program_space restore_pspace;
3424
3425 for (struct program_space *pspace : program_spaces)
3426 {
3427 CORE_ADDR addr;
3428
3429 set_current_program_space (pspace);
3430
3431 for (objfile *objfile : current_program_space->objfiles ())
3432 {
3433 struct breakpoint *b;
3434 struct breakpoint_objfile_data *bp_objfile_data;
3435 struct explicit_location explicit_loc;
3436
3437 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3438
3439 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3440 continue;
3441
3442 if (bp_objfile_data->terminate_msym.minsym == NULL)
3443 {
3444 struct bound_minimal_symbol m;
3445
3446 m = lookup_minimal_symbol (func_name, NULL, objfile);
3447 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3448 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3449 {
3450 /* Prevent future lookups in this objfile. */
3451 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3452 continue;
3453 }
3454 bp_objfile_data->terminate_msym = m;
3455 }
3456
3457 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3458 b = create_internal_breakpoint (objfile->arch (), addr,
3459 bp_std_terminate_master,
3460 &internal_breakpoint_ops);
3461 initialize_explicit_location (&explicit_loc);
3462 explicit_loc.function_name = ASTRDUP (func_name);
3463 b->location = new_explicit_location (&explicit_loc);
3464 b->enable_state = bp_disabled;
3465 }
3466 }
3467 }
3468
3469 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3470 probe. Return true if a breakpoint was installed. */
3471
3472 static bool
3473 create_exception_master_breakpoint_probe (objfile *objfile)
3474 {
3475 struct breakpoint *b;
3476 struct gdbarch *gdbarch;
3477 struct breakpoint_objfile_data *bp_objfile_data;
3478
3479 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3480
3481 /* We prefer the SystemTap probe point if it exists. */
3482 if (!bp_objfile_data->exception_searched)
3483 {
3484 std::vector<probe *> ret
3485 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3486
3487 if (!ret.empty ())
3488 {
3489 /* We are only interested in checking one element. */
3490 probe *p = ret[0];
3491
3492 if (!p->can_evaluate_arguments ())
3493 {
3494 /* We cannot use the probe interface here, because it does
3495 not know how to evaluate arguments. */
3496 ret.clear ();
3497 }
3498 }
3499 bp_objfile_data->exception_probes = ret;
3500 bp_objfile_data->exception_searched = 1;
3501 }
3502
3503 if (bp_objfile_data->exception_probes.empty ())
3504 return false;
3505
3506 gdbarch = objfile->arch ();
3507
3508 for (probe *p : bp_objfile_data->exception_probes)
3509 {
3510 b = create_internal_breakpoint (gdbarch,
3511 p->get_relocated_address (objfile),
3512 bp_exception_master,
3513 &internal_breakpoint_ops);
3514 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3515 b->enable_state = bp_disabled;
3516 }
3517
3518 return true;
3519 }
3520
3521 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3522 _Unwind_DebugHook. Return true if a breakpoint was installed. */
3523
3524 static bool
3525 create_exception_master_breakpoint_hook (objfile *objfile)
3526 {
3527 const char *const func_name = "_Unwind_DebugHook";
3528 struct breakpoint *b;
3529 struct gdbarch *gdbarch;
3530 struct breakpoint_objfile_data *bp_objfile_data;
3531 CORE_ADDR addr;
3532 struct explicit_location explicit_loc;
3533
3534 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3535
3536 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3537 return false;
3538
3539 gdbarch = objfile->arch ();
3540
3541 if (bp_objfile_data->exception_msym.minsym == NULL)
3542 {
3543 struct bound_minimal_symbol debug_hook;
3544
3545 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3546 if (debug_hook.minsym == NULL)
3547 {
3548 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3549 return false;
3550 }
3551
3552 bp_objfile_data->exception_msym = debug_hook;
3553 }
3554
3555 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3556 addr = gdbarch_convert_from_func_ptr_addr
3557 (gdbarch, addr, current_inferior ()->top_target ());
3558 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3559 &internal_breakpoint_ops);
3560 initialize_explicit_location (&explicit_loc);
3561 explicit_loc.function_name = ASTRDUP (func_name);
3562 b->location = new_explicit_location (&explicit_loc);
3563 b->enable_state = bp_disabled;
3564
3565 return true;
3566 }
3567
3568 /* Install a master breakpoint on the unwinder's debug hook. */
3569
3570 static void
3571 create_exception_master_breakpoint (void)
3572 {
3573 for (objfile *obj : current_program_space->objfiles ())
3574 {
3575 /* Skip separate debug object. */
3576 if (obj->separate_debug_objfile_backlink)
3577 continue;
3578
3579 /* Try a probe kind breakpoint. */
3580 if (create_exception_master_breakpoint_probe (obj))
3581 continue;
3582
3583 /* Iterate over main and separate debug objects and try an
3584 _Unwind_DebugHook kind breakpoint. */
3585 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3586 if (create_exception_master_breakpoint_hook (debug_objfile))
3587 break;
3588 }
3589 }
3590
3591 /* Does B have a location spec? */
3592
3593 static int
3594 breakpoint_event_location_empty_p (const struct breakpoint *b)
3595 {
3596 return b->location != NULL && event_location_empty_p (b->location.get ());
3597 }
3598
3599 void
3600 update_breakpoints_after_exec (void)
3601 {
3602 /* We're about to delete breakpoints from GDB's lists. If the
3603 INSERTED flag is true, GDB will try to lift the breakpoints by
3604 writing the breakpoints' "shadow contents" back into memory. The
3605 "shadow contents" are NOT valid after an exec, so GDB should not
3606 do that. Instead, the target is responsible from marking
3607 breakpoints out as soon as it detects an exec. We don't do that
3608 here instead, because there may be other attempts to delete
3609 breakpoints after detecting an exec and before reaching here. */
3610 for (bp_location *bploc : all_bp_locations ())
3611 if (bploc->pspace == current_program_space)
3612 gdb_assert (!bploc->inserted);
3613
3614 for (breakpoint *b : all_breakpoints_safe ())
3615 {
3616 if (b->pspace != current_program_space)
3617 continue;
3618
3619 /* Solib breakpoints must be explicitly reset after an exec(). */
3620 if (b->type == bp_shlib_event)
3621 {
3622 delete_breakpoint (b);
3623 continue;
3624 }
3625
3626 /* JIT breakpoints must be explicitly reset after an exec(). */
3627 if (b->type == bp_jit_event)
3628 {
3629 delete_breakpoint (b);
3630 continue;
3631 }
3632
3633 /* Thread event breakpoints must be set anew after an exec(),
3634 as must overlay event and longjmp master breakpoints. */
3635 if (b->type == bp_thread_event || b->type == bp_overlay_event
3636 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3637 || b->type == bp_exception_master)
3638 {
3639 delete_breakpoint (b);
3640 continue;
3641 }
3642
3643 /* Step-resume breakpoints are meaningless after an exec(). */
3644 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3645 {
3646 delete_breakpoint (b);
3647 continue;
3648 }
3649
3650 /* Just like single-step breakpoints. */
3651 if (b->type == bp_single_step)
3652 {
3653 delete_breakpoint (b);
3654 continue;
3655 }
3656
3657 /* Longjmp and longjmp-resume breakpoints are also meaningless
3658 after an exec. */
3659 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3660 || b->type == bp_longjmp_call_dummy
3661 || b->type == bp_exception || b->type == bp_exception_resume)
3662 {
3663 delete_breakpoint (b);
3664 continue;
3665 }
3666
3667 if (b->type == bp_catchpoint)
3668 {
3669 /* For now, none of the bp_catchpoint breakpoints need to
3670 do anything at this point. In the future, if some of
3671 the catchpoints need to something, we will need to add
3672 a new method, and call this method from here. */
3673 continue;
3674 }
3675
3676 /* bp_finish is a special case. The only way we ought to be able
3677 to see one of these when an exec() has happened, is if the user
3678 caught a vfork, and then said "finish". Ordinarily a finish just
3679 carries them to the call-site of the current callee, by setting
3680 a temporary bp there and resuming. But in this case, the finish
3681 will carry them entirely through the vfork & exec.
3682
3683 We don't want to allow a bp_finish to remain inserted now. But
3684 we can't safely delete it, 'cause finish_command has a handle to
3685 the bp on a bpstat, and will later want to delete it. There's a
3686 chance (and I've seen it happen) that if we delete the bp_finish
3687 here, that its storage will get reused by the time finish_command
3688 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3689 We really must allow finish_command to delete a bp_finish.
3690
3691 In the absence of a general solution for the "how do we know
3692 it's safe to delete something others may have handles to?"
3693 problem, what we'll do here is just uninsert the bp_finish, and
3694 let finish_command delete it.
3695
3696 (We know the bp_finish is "doomed" in the sense that it's
3697 momentary, and will be deleted as soon as finish_command sees
3698 the inferior stopped. So it doesn't matter that the bp's
3699 address is probably bogus in the new a.out, unlike e.g., the
3700 solib breakpoints.) */
3701
3702 if (b->type == bp_finish)
3703 {
3704 continue;
3705 }
3706
3707 /* Without a symbolic address, we have little hope of the
3708 pre-exec() address meaning the same thing in the post-exec()
3709 a.out. */
3710 if (breakpoint_event_location_empty_p (b))
3711 {
3712 delete_breakpoint (b);
3713 continue;
3714 }
3715 }
3716 }
3717
3718 int
3719 detach_breakpoints (ptid_t ptid)
3720 {
3721 int val = 0;
3722 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3723 struct inferior *inf = current_inferior ();
3724
3725 if (ptid.pid () == inferior_ptid.pid ())
3726 error (_("Cannot detach breakpoints of inferior_ptid"));
3727
3728 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3729 inferior_ptid = ptid;
3730 for (bp_location *bl : all_bp_locations ())
3731 {
3732 if (bl->pspace != inf->pspace)
3733 continue;
3734
3735 /* This function must physically remove breakpoints locations
3736 from the specified ptid, without modifying the breakpoint
3737 package's state. Locations of type bp_loc_other are only
3738 maintained at GDB side. So, there is no need to remove
3739 these bp_loc_other locations. Moreover, removing these
3740 would modify the breakpoint package's state. */
3741 if (bl->loc_type == bp_loc_other)
3742 continue;
3743
3744 if (bl->inserted)
3745 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3746 }
3747
3748 return val;
3749 }
3750
3751 /* Remove the breakpoint location BL from the current address space.
3752 Note that this is used to detach breakpoints from a child fork.
3753 When we get here, the child isn't in the inferior list, and neither
3754 do we have objects to represent its address space --- we should
3755 *not* look at bl->pspace->aspace here. */
3756
3757 static int
3758 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3759 {
3760 int val;
3761
3762 /* BL is never in moribund_locations by our callers. */
3763 gdb_assert (bl->owner != NULL);
3764
3765 /* The type of none suggests that owner is actually deleted.
3766 This should not ever happen. */
3767 gdb_assert (bl->owner->type != bp_none);
3768
3769 if (bl->loc_type == bp_loc_software_breakpoint
3770 || bl->loc_type == bp_loc_hardware_breakpoint)
3771 {
3772 /* "Normal" instruction breakpoint: either the standard
3773 trap-instruction bp (bp_breakpoint), or a
3774 bp_hardware_breakpoint. */
3775
3776 /* First check to see if we have to handle an overlay. */
3777 if (overlay_debugging == ovly_off
3778 || bl->section == NULL
3779 || !(section_is_overlay (bl->section)))
3780 {
3781 /* No overlay handling: just remove the breakpoint. */
3782
3783 /* If we're trying to uninsert a memory breakpoint that we
3784 know is set in a dynamic object that is marked
3785 shlib_disabled, then either the dynamic object was
3786 removed with "remove-symbol-file" or with
3787 "nosharedlibrary". In the former case, we don't know
3788 whether another dynamic object might have loaded over the
3789 breakpoint's address -- the user might well let us know
3790 about it next with add-symbol-file (the whole point of
3791 add-symbol-file is letting the user manually maintain a
3792 list of dynamically loaded objects). If we have the
3793 breakpoint's shadow memory, that is, this is a software
3794 breakpoint managed by GDB, check whether the breakpoint
3795 is still inserted in memory, to avoid overwriting wrong
3796 code with stale saved shadow contents. Note that HW
3797 breakpoints don't have shadow memory, as they're
3798 implemented using a mechanism that is not dependent on
3799 being able to modify the target's memory, and as such
3800 they should always be removed. */
3801 if (bl->shlib_disabled
3802 && bl->target_info.shadow_len != 0
3803 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3804 val = 0;
3805 else
3806 val = bl->owner->ops->remove_location (bl, reason);
3807 }
3808 else
3809 {
3810 /* This breakpoint is in an overlay section.
3811 Did we set a breakpoint at the LMA? */
3812 if (!overlay_events_enabled)
3813 {
3814 /* Yes -- overlay event support is not active, so we
3815 should have set a breakpoint at the LMA. Remove it.
3816 */
3817 /* Ignore any failures: if the LMA is in ROM, we will
3818 have already warned when we failed to insert it. */
3819 if (bl->loc_type == bp_loc_hardware_breakpoint)
3820 target_remove_hw_breakpoint (bl->gdbarch,
3821 &bl->overlay_target_info);
3822 else
3823 target_remove_breakpoint (bl->gdbarch,
3824 &bl->overlay_target_info,
3825 reason);
3826 }
3827 /* Did we set a breakpoint at the VMA?
3828 If so, we will have marked the breakpoint 'inserted'. */
3829 if (bl->inserted)
3830 {
3831 /* Yes -- remove it. Previously we did not bother to
3832 remove the breakpoint if the section had been
3833 unmapped, but let's not rely on that being safe. We
3834 don't know what the overlay manager might do. */
3835
3836 /* However, we should remove *software* breakpoints only
3837 if the section is still mapped, or else we overwrite
3838 wrong code with the saved shadow contents. */
3839 if (bl->loc_type == bp_loc_hardware_breakpoint
3840 || section_is_mapped (bl->section))
3841 val = bl->owner->ops->remove_location (bl, reason);
3842 else
3843 val = 0;
3844 }
3845 else
3846 {
3847 /* No -- not inserted, so no need to remove. No error. */
3848 val = 0;
3849 }
3850 }
3851
3852 /* In some cases, we might not be able to remove a breakpoint in
3853 a shared library that has already been removed, but we have
3854 not yet processed the shlib unload event. Similarly for an
3855 unloaded add-symbol-file object - the user might not yet have
3856 had the chance to remove-symbol-file it. shlib_disabled will
3857 be set if the library/object has already been removed, but
3858 the breakpoint hasn't been uninserted yet, e.g., after
3859 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3860 always-inserted mode. */
3861 if (val
3862 && (bl->loc_type == bp_loc_software_breakpoint
3863 && (bl->shlib_disabled
3864 || solib_name_from_address (bl->pspace, bl->address)
3865 || shared_objfile_contains_address_p (bl->pspace,
3866 bl->address))))
3867 val = 0;
3868
3869 if (val)
3870 return val;
3871 bl->inserted = (reason == DETACH_BREAKPOINT);
3872 }
3873 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3874 {
3875 gdb_assert (bl->owner->ops != NULL
3876 && bl->owner->ops->remove_location != NULL);
3877
3878 bl->inserted = (reason == DETACH_BREAKPOINT);
3879 bl->owner->ops->remove_location (bl, reason);
3880
3881 /* Failure to remove any of the hardware watchpoints comes here. */
3882 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3883 warning (_("Could not remove hardware watchpoint %d."),
3884 bl->owner->number);
3885 }
3886 else if (bl->owner->type == bp_catchpoint
3887 && breakpoint_enabled (bl->owner)
3888 && !bl->duplicate)
3889 {
3890 gdb_assert (bl->owner->ops != NULL
3891 && bl->owner->ops->remove_location != NULL);
3892
3893 val = bl->owner->ops->remove_location (bl, reason);
3894 if (val)
3895 return val;
3896
3897 bl->inserted = (reason == DETACH_BREAKPOINT);
3898 }
3899
3900 return 0;
3901 }
3902
3903 static int
3904 remove_breakpoint (struct bp_location *bl)
3905 {
3906 /* BL is never in moribund_locations by our callers. */
3907 gdb_assert (bl->owner != NULL);
3908
3909 /* The type of none suggests that owner is actually deleted.
3910 This should not ever happen. */
3911 gdb_assert (bl->owner->type != bp_none);
3912
3913 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3914
3915 switch_to_program_space_and_thread (bl->pspace);
3916
3917 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
3918 }
3919
3920 /* Clear the "inserted" flag in all breakpoints. */
3921
3922 void
3923 mark_breakpoints_out (void)
3924 {
3925 for (bp_location *bl : all_bp_locations ())
3926 if (bl->pspace == current_program_space)
3927 bl->inserted = 0;
3928 }
3929
3930 /* Clear the "inserted" flag in all breakpoints and delete any
3931 breakpoints which should go away between runs of the program.
3932
3933 Plus other such housekeeping that has to be done for breakpoints
3934 between runs.
3935
3936 Note: this function gets called at the end of a run (by
3937 generic_mourn_inferior) and when a run begins (by
3938 init_wait_for_inferior). */
3939
3940
3941
3942 void
3943 breakpoint_init_inferior (enum inf_context context)
3944 {
3945 struct program_space *pspace = current_program_space;
3946
3947 /* If breakpoint locations are shared across processes, then there's
3948 nothing to do. */
3949 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
3950 return;
3951
3952 mark_breakpoints_out ();
3953
3954 for (breakpoint *b : all_breakpoints_safe ())
3955 {
3956 if (b->loc && b->loc->pspace != pspace)
3957 continue;
3958
3959 switch (b->type)
3960 {
3961 case bp_call_dummy:
3962 case bp_longjmp_call_dummy:
3963
3964 /* If the call dummy breakpoint is at the entry point it will
3965 cause problems when the inferior is rerun, so we better get
3966 rid of it. */
3967
3968 case bp_watchpoint_scope:
3969
3970 /* Also get rid of scope breakpoints. */
3971
3972 case bp_shlib_event:
3973
3974 /* Also remove solib event breakpoints. Their addresses may
3975 have changed since the last time we ran the program.
3976 Actually we may now be debugging against different target;
3977 and so the solib backend that installed this breakpoint may
3978 not be used in by the target. E.g.,
3979
3980 (gdb) file prog-linux
3981 (gdb) run # native linux target
3982 ...
3983 (gdb) kill
3984 (gdb) file prog-win.exe
3985 (gdb) tar rem :9999 # remote Windows gdbserver.
3986 */
3987
3988 case bp_step_resume:
3989
3990 /* Also remove step-resume breakpoints. */
3991
3992 case bp_single_step:
3993
3994 /* Also remove single-step breakpoints. */
3995
3996 delete_breakpoint (b);
3997 break;
3998
3999 case bp_watchpoint:
4000 case bp_hardware_watchpoint:
4001 case bp_read_watchpoint:
4002 case bp_access_watchpoint:
4003 {
4004 struct watchpoint *w = (struct watchpoint *) b;
4005
4006 /* Likewise for watchpoints on local expressions. */
4007 if (w->exp_valid_block != NULL)
4008 delete_breakpoint (b);
4009 else
4010 {
4011 /* Get rid of existing locations, which are no longer
4012 valid. New ones will be created in
4013 update_watchpoint, when the inferior is restarted.
4014 The next update_global_location_list call will
4015 garbage collect them. */
4016 b->loc = NULL;
4017
4018 if (context == inf_starting)
4019 {
4020 /* Reset val field to force reread of starting value in
4021 insert_breakpoints. */
4022 w->val.reset (nullptr);
4023 w->val_valid = false;
4024 }
4025 }
4026 }
4027 break;
4028 default:
4029 break;
4030 }
4031 }
4032
4033 /* Get rid of the moribund locations. */
4034 for (bp_location *bl : moribund_locations)
4035 decref_bp_location (&bl);
4036 moribund_locations.clear ();
4037 }
4038
4039 /* These functions concern about actual breakpoints inserted in the
4040 target --- to e.g. check if we need to do decr_pc adjustment or if
4041 we need to hop over the bkpt --- so we check for address space
4042 match, not program space. */
4043
4044 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4045 exists at PC. It returns ordinary_breakpoint_here if it's an
4046 ordinary breakpoint, or permanent_breakpoint_here if it's a
4047 permanent breakpoint.
4048 - When continuing from a location with an ordinary breakpoint, we
4049 actually single step once before calling insert_breakpoints.
4050 - When continuing from a location with a permanent breakpoint, we
4051 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4052 the target, to advance the PC past the breakpoint. */
4053
4054 enum breakpoint_here
4055 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4056 {
4057 int any_breakpoint_here = 0;
4058
4059 for (bp_location *bl : all_bp_locations ())
4060 {
4061 if (bl->loc_type != bp_loc_software_breakpoint
4062 && bl->loc_type != bp_loc_hardware_breakpoint)
4063 continue;
4064
4065 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4066 if ((breakpoint_enabled (bl->owner)
4067 || bl->permanent)
4068 && breakpoint_location_address_match (bl, aspace, pc))
4069 {
4070 if (overlay_debugging
4071 && section_is_overlay (bl->section)
4072 && !section_is_mapped (bl->section))
4073 continue; /* unmapped overlay -- can't be a match */
4074 else if (bl->permanent)
4075 return permanent_breakpoint_here;
4076 else
4077 any_breakpoint_here = 1;
4078 }
4079 }
4080
4081 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4082 }
4083
4084 /* See breakpoint.h. */
4085
4086 int
4087 breakpoint_in_range_p (const address_space *aspace,
4088 CORE_ADDR addr, ULONGEST len)
4089 {
4090 for (bp_location *bl : all_bp_locations ())
4091 {
4092 if (bl->loc_type != bp_loc_software_breakpoint
4093 && bl->loc_type != bp_loc_hardware_breakpoint)
4094 continue;
4095
4096 if ((breakpoint_enabled (bl->owner)
4097 || bl->permanent)
4098 && breakpoint_location_address_range_overlap (bl, aspace,
4099 addr, len))
4100 {
4101 if (overlay_debugging
4102 && section_is_overlay (bl->section)
4103 && !section_is_mapped (bl->section))
4104 {
4105 /* Unmapped overlay -- can't be a match. */
4106 continue;
4107 }
4108
4109 return 1;
4110 }
4111 }
4112
4113 return 0;
4114 }
4115
4116 /* Return true if there's a moribund breakpoint at PC. */
4117
4118 int
4119 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4120 {
4121 for (bp_location *loc : moribund_locations)
4122 if (breakpoint_location_address_match (loc, aspace, pc))
4123 return 1;
4124
4125 return 0;
4126 }
4127
4128 /* Returns non-zero iff BL is inserted at PC, in address space
4129 ASPACE. */
4130
4131 static int
4132 bp_location_inserted_here_p (struct bp_location *bl,
4133 const address_space *aspace, CORE_ADDR pc)
4134 {
4135 if (bl->inserted
4136 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4137 aspace, pc))
4138 {
4139 if (overlay_debugging
4140 && section_is_overlay (bl->section)
4141 && !section_is_mapped (bl->section))
4142 return 0; /* unmapped overlay -- can't be a match */
4143 else
4144 return 1;
4145 }
4146 return 0;
4147 }
4148
4149 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4150
4151 int
4152 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4153 {
4154 for (bp_location *bl : all_bp_locations_at_addr (pc))
4155 {
4156 if (bl->loc_type != bp_loc_software_breakpoint
4157 && bl->loc_type != bp_loc_hardware_breakpoint)
4158 continue;
4159
4160 if (bp_location_inserted_here_p (bl, aspace, pc))
4161 return 1;
4162 }
4163 return 0;
4164 }
4165
4166 /* This function returns non-zero iff there is a software breakpoint
4167 inserted at PC. */
4168
4169 int
4170 software_breakpoint_inserted_here_p (const address_space *aspace,
4171 CORE_ADDR pc)
4172 {
4173 for (bp_location *bl : all_bp_locations_at_addr (pc))
4174 {
4175 if (bl->loc_type != bp_loc_software_breakpoint)
4176 continue;
4177
4178 if (bp_location_inserted_here_p (bl, aspace, pc))
4179 return 1;
4180 }
4181
4182 return 0;
4183 }
4184
4185 /* See breakpoint.h. */
4186
4187 int
4188 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4189 CORE_ADDR pc)
4190 {
4191 for (bp_location *bl : all_bp_locations_at_addr (pc))
4192 {
4193 if (bl->loc_type != bp_loc_hardware_breakpoint)
4194 continue;
4195
4196 if (bp_location_inserted_here_p (bl, aspace, pc))
4197 return 1;
4198 }
4199
4200 return 0;
4201 }
4202
4203 int
4204 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4205 CORE_ADDR addr, ULONGEST len)
4206 {
4207 for (breakpoint *bpt : all_breakpoints ())
4208 {
4209 if (bpt->type != bp_hardware_watchpoint
4210 && bpt->type != bp_access_watchpoint)
4211 continue;
4212
4213 if (!breakpoint_enabled (bpt))
4214 continue;
4215
4216 for (bp_location *loc : bpt->locations ())
4217 if (loc->pspace->aspace == aspace && loc->inserted)
4218 {
4219 CORE_ADDR l, h;
4220
4221 /* Check for intersection. */
4222 l = std::max<CORE_ADDR> (loc->address, addr);
4223 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4224 if (l < h)
4225 return 1;
4226 }
4227 }
4228 return 0;
4229 }
4230
4231 /* See breakpoint.h. */
4232
4233 bool
4234 is_catchpoint (struct breakpoint *b)
4235 {
4236 return (b->type == bp_catchpoint);
4237 }
4238
4239 /* Clear a bpstat so that it says we are not at any breakpoint.
4240 Also free any storage that is part of a bpstat. */
4241
4242 void
4243 bpstat_clear (bpstat *bsp)
4244 {
4245 bpstat p;
4246 bpstat q;
4247
4248 if (bsp == 0)
4249 return;
4250 p = *bsp;
4251 while (p != NULL)
4252 {
4253 q = p->next;
4254 delete p;
4255 p = q;
4256 }
4257 *bsp = NULL;
4258 }
4259
4260 bpstats::bpstats (const bpstats &other)
4261 : next (NULL),
4262 bp_location_at (other.bp_location_at),
4263 breakpoint_at (other.breakpoint_at),
4264 commands (other.commands),
4265 print (other.print),
4266 stop (other.stop),
4267 print_it (other.print_it)
4268 {
4269 if (other.old_val != NULL)
4270 old_val = release_value (value_copy (other.old_val.get ()));
4271 }
4272
4273 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4274 is part of the bpstat is copied as well. */
4275
4276 bpstat
4277 bpstat_copy (bpstat bs)
4278 {
4279 bpstat p = NULL;
4280 bpstat tmp;
4281 bpstat retval = NULL;
4282
4283 if (bs == NULL)
4284 return bs;
4285
4286 for (; bs != NULL; bs = bs->next)
4287 {
4288 tmp = new bpstats (*bs);
4289
4290 if (p == NULL)
4291 /* This is the first thing in the chain. */
4292 retval = tmp;
4293 else
4294 p->next = tmp;
4295 p = tmp;
4296 }
4297 p->next = NULL;
4298 return retval;
4299 }
4300
4301 /* Find the bpstat associated with this breakpoint. */
4302
4303 bpstat
4304 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4305 {
4306 if (bsp == NULL)
4307 return NULL;
4308
4309 for (; bsp != NULL; bsp = bsp->next)
4310 {
4311 if (bsp->breakpoint_at == breakpoint)
4312 return bsp;
4313 }
4314 return NULL;
4315 }
4316
4317 /* See breakpoint.h. */
4318
4319 bool
4320 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4321 {
4322 for (; bsp != NULL; bsp = bsp->next)
4323 {
4324 if (bsp->breakpoint_at == NULL)
4325 {
4326 /* A moribund location can never explain a signal other than
4327 GDB_SIGNAL_TRAP. */
4328 if (sig == GDB_SIGNAL_TRAP)
4329 return true;
4330 }
4331 else
4332 {
4333 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4334 sig))
4335 return true;
4336 }
4337 }
4338
4339 return false;
4340 }
4341
4342 /* Put in *NUM the breakpoint number of the first breakpoint we are
4343 stopped at. *BSP upon return is a bpstat which points to the
4344 remaining breakpoints stopped at (but which is not guaranteed to be
4345 good for anything but further calls to bpstat_num).
4346
4347 Return 0 if passed a bpstat which does not indicate any breakpoints.
4348 Return -1 if stopped at a breakpoint that has been deleted since
4349 we set it.
4350 Return 1 otherwise. */
4351
4352 int
4353 bpstat_num (bpstat *bsp, int *num)
4354 {
4355 struct breakpoint *b;
4356
4357 if ((*bsp) == NULL)
4358 return 0; /* No more breakpoint values */
4359
4360 /* We assume we'll never have several bpstats that correspond to a
4361 single breakpoint -- otherwise, this function might return the
4362 same number more than once and this will look ugly. */
4363 b = (*bsp)->breakpoint_at;
4364 *bsp = (*bsp)->next;
4365 if (b == NULL)
4366 return -1; /* breakpoint that's been deleted since */
4367
4368 *num = b->number; /* We have its number */
4369 return 1;
4370 }
4371
4372 /* See breakpoint.h. */
4373
4374 void
4375 bpstat_clear_actions (void)
4376 {
4377 bpstat bs;
4378
4379 if (inferior_ptid == null_ptid)
4380 return;
4381
4382 thread_info *tp = inferior_thread ();
4383 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4384 {
4385 bs->commands = NULL;
4386 bs->old_val.reset (nullptr);
4387 }
4388 }
4389
4390 /* Called when a command is about to proceed the inferior. */
4391
4392 static void
4393 breakpoint_about_to_proceed (void)
4394 {
4395 if (inferior_ptid != null_ptid)
4396 {
4397 struct thread_info *tp = inferior_thread ();
4398
4399 /* Allow inferior function calls in breakpoint commands to not
4400 interrupt the command list. When the call finishes
4401 successfully, the inferior will be standing at the same
4402 breakpoint as if nothing happened. */
4403 if (tp->control.in_infcall)
4404 return;
4405 }
4406
4407 breakpoint_proceeded = 1;
4408 }
4409
4410 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4411 or its equivalent. */
4412
4413 static int
4414 command_line_is_silent (struct command_line *cmd)
4415 {
4416 return cmd && (strcmp ("silent", cmd->line) == 0);
4417 }
4418
4419 /* Execute all the commands associated with all the breakpoints at
4420 this location. Any of these commands could cause the process to
4421 proceed beyond this point, etc. We look out for such changes by
4422 checking the global "breakpoint_proceeded" after each command.
4423
4424 Returns true if a breakpoint command resumed the inferior. In that
4425 case, it is the caller's responsibility to recall it again with the
4426 bpstat of the current thread. */
4427
4428 static int
4429 bpstat_do_actions_1 (bpstat *bsp)
4430 {
4431 bpstat bs;
4432 int again = 0;
4433
4434 /* Avoid endless recursion if a `source' command is contained
4435 in bs->commands. */
4436 if (executing_breakpoint_commands)
4437 return 0;
4438
4439 scoped_restore save_executing
4440 = make_scoped_restore (&executing_breakpoint_commands, 1);
4441
4442 scoped_restore preventer = prevent_dont_repeat ();
4443
4444 /* This pointer will iterate over the list of bpstat's. */
4445 bs = *bsp;
4446
4447 breakpoint_proceeded = 0;
4448 for (; bs != NULL; bs = bs->next)
4449 {
4450 struct command_line *cmd = NULL;
4451
4452 /* Take ownership of the BSP's command tree, if it has one.
4453
4454 The command tree could legitimately contain commands like
4455 'step' and 'next', which call clear_proceed_status, which
4456 frees stop_bpstat's command tree. To make sure this doesn't
4457 free the tree we're executing out from under us, we need to
4458 take ownership of the tree ourselves. Since a given bpstat's
4459 commands are only executed once, we don't need to copy it; we
4460 can clear the pointer in the bpstat, and make sure we free
4461 the tree when we're done. */
4462 counted_command_line ccmd = bs->commands;
4463 bs->commands = NULL;
4464 if (ccmd != NULL)
4465 cmd = ccmd.get ();
4466 if (command_line_is_silent (cmd))
4467 {
4468 /* The action has been already done by bpstat_stop_status. */
4469 cmd = cmd->next;
4470 }
4471
4472 while (cmd != NULL)
4473 {
4474 execute_control_command (cmd);
4475
4476 if (breakpoint_proceeded)
4477 break;
4478 else
4479 cmd = cmd->next;
4480 }
4481
4482 if (breakpoint_proceeded)
4483 {
4484 if (current_ui->async)
4485 /* If we are in async mode, then the target might be still
4486 running, not stopped at any breakpoint, so nothing for
4487 us to do here -- just return to the event loop. */
4488 ;
4489 else
4490 /* In sync mode, when execute_control_command returns
4491 we're already standing on the next breakpoint.
4492 Breakpoint commands for that stop were not run, since
4493 execute_command does not run breakpoint commands --
4494 only command_line_handler does, but that one is not
4495 involved in execution of breakpoint commands. So, we
4496 can now execute breakpoint commands. It should be
4497 noted that making execute_command do bpstat actions is
4498 not an option -- in this case we'll have recursive
4499 invocation of bpstat for each breakpoint with a
4500 command, and can easily blow up GDB stack. Instead, we
4501 return true, which will trigger the caller to recall us
4502 with the new stop_bpstat. */
4503 again = 1;
4504 break;
4505 }
4506 }
4507 return again;
4508 }
4509
4510 /* Helper for bpstat_do_actions. Get the current thread, if there's
4511 one, is alive and has execution. Return NULL otherwise. */
4512
4513 static thread_info *
4514 get_bpstat_thread ()
4515 {
4516 if (inferior_ptid == null_ptid || !target_has_execution ())
4517 return NULL;
4518
4519 thread_info *tp = inferior_thread ();
4520 if (tp->state == THREAD_EXITED || tp->executing)
4521 return NULL;
4522 return tp;
4523 }
4524
4525 void
4526 bpstat_do_actions (void)
4527 {
4528 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4529 thread_info *tp;
4530
4531 /* Do any commands attached to breakpoint we are stopped at. */
4532 while ((tp = get_bpstat_thread ()) != NULL)
4533 {
4534 /* Since in sync mode, bpstat_do_actions may resume the
4535 inferior, and only return when it is stopped at the next
4536 breakpoint, we keep doing breakpoint actions until it returns
4537 false to indicate the inferior was not resumed. */
4538 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4539 break;
4540 }
4541
4542 cleanup_if_error.release ();
4543 }
4544
4545 /* Print out the (old or new) value associated with a watchpoint. */
4546
4547 static void
4548 watchpoint_value_print (struct value *val, struct ui_file *stream)
4549 {
4550 if (val == NULL)
4551 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4552 else
4553 {
4554 struct value_print_options opts;
4555 get_user_print_options (&opts);
4556 value_print (val, stream, &opts);
4557 }
4558 }
4559
4560 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4561 debugging multiple threads. */
4562
4563 void
4564 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4565 {
4566 if (uiout->is_mi_like_p ())
4567 return;
4568
4569 uiout->text ("\n");
4570
4571 if (show_thread_that_caused_stop ())
4572 {
4573 const char *name;
4574 struct thread_info *thr = inferior_thread ();
4575
4576 uiout->text ("Thread ");
4577 uiout->field_string ("thread-id", print_thread_id (thr));
4578
4579 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4580 if (name != NULL)
4581 {
4582 uiout->text (" \"");
4583 uiout->field_string ("name", name);
4584 uiout->text ("\"");
4585 }
4586
4587 uiout->text (" hit ");
4588 }
4589 }
4590
4591 /* Generic routine for printing messages indicating why we
4592 stopped. The behavior of this function depends on the value
4593 'print_it' in the bpstat structure. Under some circumstances we
4594 may decide not to print anything here and delegate the task to
4595 normal_stop(). */
4596
4597 static enum print_stop_action
4598 print_bp_stop_message (bpstat bs)
4599 {
4600 switch (bs->print_it)
4601 {
4602 case print_it_noop:
4603 /* Nothing should be printed for this bpstat entry. */
4604 return PRINT_UNKNOWN;
4605 break;
4606
4607 case print_it_done:
4608 /* We still want to print the frame, but we already printed the
4609 relevant messages. */
4610 return PRINT_SRC_AND_LOC;
4611 break;
4612
4613 case print_it_normal:
4614 {
4615 struct breakpoint *b = bs->breakpoint_at;
4616
4617 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4618 which has since been deleted. */
4619 if (b == NULL)
4620 return PRINT_UNKNOWN;
4621
4622 /* Normal case. Call the breakpoint's print_it method. */
4623 return b->ops->print_it (bs);
4624 }
4625 break;
4626
4627 default:
4628 internal_error (__FILE__, __LINE__,
4629 _("print_bp_stop_message: unrecognized enum value"));
4630 break;
4631 }
4632 }
4633
4634 /* A helper function that prints a shared library stopped event. */
4635
4636 static void
4637 print_solib_event (int is_catchpoint)
4638 {
4639 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4640 bool any_added = !current_program_space->added_solibs.empty ();
4641
4642 if (!is_catchpoint)
4643 {
4644 if (any_added || any_deleted)
4645 current_uiout->text (_("Stopped due to shared library event:\n"));
4646 else
4647 current_uiout->text (_("Stopped due to shared library event (no "
4648 "libraries added or removed)\n"));
4649 }
4650
4651 if (current_uiout->is_mi_like_p ())
4652 current_uiout->field_string ("reason",
4653 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4654
4655 if (any_deleted)
4656 {
4657 current_uiout->text (_(" Inferior unloaded "));
4658 ui_out_emit_list list_emitter (current_uiout, "removed");
4659 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4660 {
4661 const std::string &name = current_program_space->deleted_solibs[ix];
4662
4663 if (ix > 0)
4664 current_uiout->text (" ");
4665 current_uiout->field_string ("library", name);
4666 current_uiout->text ("\n");
4667 }
4668 }
4669
4670 if (any_added)
4671 {
4672 current_uiout->text (_(" Inferior loaded "));
4673 ui_out_emit_list list_emitter (current_uiout, "added");
4674 bool first = true;
4675 for (so_list *iter : current_program_space->added_solibs)
4676 {
4677 if (!first)
4678 current_uiout->text (" ");
4679 first = false;
4680 current_uiout->field_string ("library", iter->so_name);
4681 current_uiout->text ("\n");
4682 }
4683 }
4684 }
4685
4686 /* Print a message indicating what happened. This is called from
4687 normal_stop(). The input to this routine is the head of the bpstat
4688 list - a list of the eventpoints that caused this stop. KIND is
4689 the target_waitkind for the stopping event. This
4690 routine calls the generic print routine for printing a message
4691 about reasons for stopping. This will print (for example) the
4692 "Breakpoint n," part of the output. The return value of this
4693 routine is one of:
4694
4695 PRINT_UNKNOWN: Means we printed nothing.
4696 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4697 code to print the location. An example is
4698 "Breakpoint 1, " which should be followed by
4699 the location.
4700 PRINT_SRC_ONLY: Means we printed something, but there is no need
4701 to also print the location part of the message.
4702 An example is the catch/throw messages, which
4703 don't require a location appended to the end.
4704 PRINT_NOTHING: We have done some printing and we don't need any
4705 further info to be printed. */
4706
4707 enum print_stop_action
4708 bpstat_print (bpstat bs, int kind)
4709 {
4710 enum print_stop_action val;
4711
4712 /* Maybe another breakpoint in the chain caused us to stop.
4713 (Currently all watchpoints go on the bpstat whether hit or not.
4714 That probably could (should) be changed, provided care is taken
4715 with respect to bpstat_explains_signal). */
4716 for (; bs; bs = bs->next)
4717 {
4718 val = print_bp_stop_message (bs);
4719 if (val == PRINT_SRC_ONLY
4720 || val == PRINT_SRC_AND_LOC
4721 || val == PRINT_NOTHING)
4722 return val;
4723 }
4724
4725 /* If we had hit a shared library event breakpoint,
4726 print_bp_stop_message would print out this message. If we hit an
4727 OS-level shared library event, do the same thing. */
4728 if (kind == TARGET_WAITKIND_LOADED)
4729 {
4730 print_solib_event (0);
4731 return PRINT_NOTHING;
4732 }
4733
4734 /* We reached the end of the chain, or we got a null BS to start
4735 with and nothing was printed. */
4736 return PRINT_UNKNOWN;
4737 }
4738
4739 /* Evaluate the boolean expression EXP and return the result. */
4740
4741 static bool
4742 breakpoint_cond_eval (expression *exp)
4743 {
4744 struct value *mark = value_mark ();
4745 bool res = value_true (evaluate_expression (exp));
4746
4747 value_free_to_mark (mark);
4748 return res;
4749 }
4750
4751 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4752
4753 bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4754 : next (NULL),
4755 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
4756 breakpoint_at (bl->owner),
4757 commands (NULL),
4758 print (0),
4759 stop (0),
4760 print_it (print_it_normal)
4761 {
4762 **bs_link_pointer = this;
4763 *bs_link_pointer = &next;
4764 }
4765
4766 bpstats::bpstats ()
4767 : next (NULL),
4768 breakpoint_at (NULL),
4769 commands (NULL),
4770 print (0),
4771 stop (0),
4772 print_it (print_it_normal)
4773 {
4774 }
4775 \f
4776 /* The target has stopped with waitstatus WS. Check if any hardware
4777 watchpoints have triggered, according to the target. */
4778
4779 int
4780 watchpoints_triggered (struct target_waitstatus *ws)
4781 {
4782 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
4783 CORE_ADDR addr;
4784
4785 if (!stopped_by_watchpoint)
4786 {
4787 /* We were not stopped by a watchpoint. Mark all watchpoints
4788 as not triggered. */
4789 for (breakpoint *b : all_breakpoints ())
4790 if (is_hardware_watchpoint (b))
4791 {
4792 struct watchpoint *w = (struct watchpoint *) b;
4793
4794 w->watchpoint_triggered = watch_triggered_no;
4795 }
4796
4797 return 0;
4798 }
4799
4800 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
4801 {
4802 /* We were stopped by a watchpoint, but we don't know where.
4803 Mark all watchpoints as unknown. */
4804 for (breakpoint *b : all_breakpoints ())
4805 if (is_hardware_watchpoint (b))
4806 {
4807 struct watchpoint *w = (struct watchpoint *) b;
4808
4809 w->watchpoint_triggered = watch_triggered_unknown;
4810 }
4811
4812 return 1;
4813 }
4814
4815 /* The target could report the data address. Mark watchpoints
4816 affected by this data address as triggered, and all others as not
4817 triggered. */
4818
4819 for (breakpoint *b : all_breakpoints ())
4820 if (is_hardware_watchpoint (b))
4821 {
4822 struct watchpoint *w = (struct watchpoint *) b;
4823
4824 w->watchpoint_triggered = watch_triggered_no;
4825 for (bp_location *loc : b->locations ())
4826 {
4827 if (is_masked_watchpoint (b))
4828 {
4829 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4830 CORE_ADDR start = loc->address & w->hw_wp_mask;
4831
4832 if (newaddr == start)
4833 {
4834 w->watchpoint_triggered = watch_triggered_yes;
4835 break;
4836 }
4837 }
4838 /* Exact match not required. Within range is sufficient. */
4839 else if (target_watchpoint_addr_within_range
4840 (current_inferior ()->top_target (), addr, loc->address,
4841 loc->length))
4842 {
4843 w->watchpoint_triggered = watch_triggered_yes;
4844 break;
4845 }
4846 }
4847 }
4848
4849 return 1;
4850 }
4851
4852 /* Possible return values for watchpoint_check. */
4853 enum wp_check_result
4854 {
4855 /* The watchpoint has been deleted. */
4856 WP_DELETED = 1,
4857
4858 /* The value has changed. */
4859 WP_VALUE_CHANGED = 2,
4860
4861 /* The value has not changed. */
4862 WP_VALUE_NOT_CHANGED = 3,
4863
4864 /* Ignore this watchpoint, no matter if the value changed or not. */
4865 WP_IGNORE = 4,
4866 };
4867
4868 #define BP_TEMPFLAG 1
4869 #define BP_HARDWAREFLAG 2
4870
4871 /* Evaluate watchpoint condition expression and check if its value
4872 changed. */
4873
4874 static wp_check_result
4875 watchpoint_check (bpstat bs)
4876 {
4877 struct watchpoint *b;
4878 struct frame_info *fr;
4879 int within_current_scope;
4880
4881 /* BS is built from an existing struct breakpoint. */
4882 gdb_assert (bs->breakpoint_at != NULL);
4883 b = (struct watchpoint *) bs->breakpoint_at;
4884
4885 /* If this is a local watchpoint, we only want to check if the
4886 watchpoint frame is in scope if the current thread is the thread
4887 that was used to create the watchpoint. */
4888 if (!watchpoint_in_thread_scope (b))
4889 return WP_IGNORE;
4890
4891 if (b->exp_valid_block == NULL)
4892 within_current_scope = 1;
4893 else
4894 {
4895 struct frame_info *frame = get_current_frame ();
4896 struct gdbarch *frame_arch = get_frame_arch (frame);
4897 CORE_ADDR frame_pc = get_frame_pc (frame);
4898
4899 /* stack_frame_destroyed_p() returns a non-zero value if we're
4900 still in the function but the stack frame has already been
4901 invalidated. Since we can't rely on the values of local
4902 variables after the stack has been destroyed, we are treating
4903 the watchpoint in that state as `not changed' without further
4904 checking. Don't mark watchpoints as changed if the current
4905 frame is in an epilogue - even if they are in some other
4906 frame, our view of the stack is likely to be wrong and
4907 frame_find_by_id could error out. */
4908 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
4909 return WP_IGNORE;
4910
4911 fr = frame_find_by_id (b->watchpoint_frame);
4912 within_current_scope = (fr != NULL);
4913
4914 /* If we've gotten confused in the unwinder, we might have
4915 returned a frame that can't describe this variable. */
4916 if (within_current_scope)
4917 {
4918 struct symbol *function;
4919
4920 function = get_frame_function (fr);
4921 if (function == NULL
4922 || !contained_in (b->exp_valid_block,
4923 SYMBOL_BLOCK_VALUE (function)))
4924 within_current_scope = 0;
4925 }
4926
4927 if (within_current_scope)
4928 /* If we end up stopping, the current frame will get selected
4929 in normal_stop. So this call to select_frame won't affect
4930 the user. */
4931 select_frame (fr);
4932 }
4933
4934 if (within_current_scope)
4935 {
4936 /* We use value_{,free_to_}mark because it could be a *long*
4937 time before we return to the command level and call
4938 free_all_values. We can't call free_all_values because we
4939 might be in the middle of evaluating a function call. */
4940
4941 struct value *mark;
4942 struct value *new_val;
4943
4944 if (is_masked_watchpoint (b))
4945 /* Since we don't know the exact trigger address (from
4946 stopped_data_address), just tell the user we've triggered
4947 a mask watchpoint. */
4948 return WP_VALUE_CHANGED;
4949
4950 mark = value_mark ();
4951 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
4952 NULL, NULL, false);
4953
4954 if (b->val_bitsize != 0)
4955 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4956
4957 /* We use value_equal_contents instead of value_equal because
4958 the latter coerces an array to a pointer, thus comparing just
4959 the address of the array instead of its contents. This is
4960 not what we want. */
4961 if ((b->val != NULL) != (new_val != NULL)
4962 || (b->val != NULL && !value_equal_contents (b->val.get (),
4963 new_val)))
4964 {
4965 bs->old_val = b->val;
4966 b->val = release_value (new_val);
4967 b->val_valid = true;
4968 if (new_val != NULL)
4969 value_free_to_mark (mark);
4970 return WP_VALUE_CHANGED;
4971 }
4972 else
4973 {
4974 /* Nothing changed. */
4975 value_free_to_mark (mark);
4976 return WP_VALUE_NOT_CHANGED;
4977 }
4978 }
4979 else
4980 {
4981 /* This seems like the only logical thing to do because
4982 if we temporarily ignored the watchpoint, then when
4983 we reenter the block in which it is valid it contains
4984 garbage (in the case of a function, it may have two
4985 garbage values, one before and one after the prologue).
4986 So we can't even detect the first assignment to it and
4987 watch after that (since the garbage may or may not equal
4988 the first value assigned). */
4989 /* We print all the stop information in
4990 breakpoint_ops->print_it, but in this case, by the time we
4991 call breakpoint_ops->print_it this bp will be deleted
4992 already. So we have no choice but print the information
4993 here. */
4994
4995 SWITCH_THRU_ALL_UIS ()
4996 {
4997 struct ui_out *uiout = current_uiout;
4998
4999 if (uiout->is_mi_like_p ())
5000 uiout->field_string
5001 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5002 uiout->message ("\nWatchpoint %pF deleted because the program has "
5003 "left the block in\n"
5004 "which its expression is valid.\n",
5005 signed_field ("wpnum", b->number));
5006 }
5007
5008 /* Make sure the watchpoint's commands aren't executed. */
5009 b->commands = NULL;
5010 watchpoint_del_at_next_stop (b);
5011
5012 return WP_DELETED;
5013 }
5014 }
5015
5016 /* Return true if it looks like target has stopped due to hitting
5017 breakpoint location BL. This function does not check if we should
5018 stop, only if BL explains the stop. */
5019
5020 static int
5021 bpstat_check_location (const struct bp_location *bl,
5022 const address_space *aspace, CORE_ADDR bp_addr,
5023 const struct target_waitstatus *ws)
5024 {
5025 struct breakpoint *b = bl->owner;
5026
5027 /* BL is from an existing breakpoint. */
5028 gdb_assert (b != NULL);
5029
5030 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5031 }
5032
5033 /* Determine if the watched values have actually changed, and we
5034 should stop. If not, set BS->stop to 0. */
5035
5036 static void
5037 bpstat_check_watchpoint (bpstat bs)
5038 {
5039 const struct bp_location *bl;
5040 struct watchpoint *b;
5041
5042 /* BS is built for existing struct breakpoint. */
5043 bl = bs->bp_location_at.get ();
5044 gdb_assert (bl != NULL);
5045 b = (struct watchpoint *) bs->breakpoint_at;
5046 gdb_assert (b != NULL);
5047
5048 {
5049 int must_check_value = 0;
5050
5051 if (b->type == bp_watchpoint)
5052 /* For a software watchpoint, we must always check the
5053 watched value. */
5054 must_check_value = 1;
5055 else if (b->watchpoint_triggered == watch_triggered_yes)
5056 /* We have a hardware watchpoint (read, write, or access)
5057 and the target earlier reported an address watched by
5058 this watchpoint. */
5059 must_check_value = 1;
5060 else if (b->watchpoint_triggered == watch_triggered_unknown
5061 && b->type == bp_hardware_watchpoint)
5062 /* We were stopped by a hardware watchpoint, but the target could
5063 not report the data address. We must check the watchpoint's
5064 value. Access and read watchpoints are out of luck; without
5065 a data address, we can't figure it out. */
5066 must_check_value = 1;
5067
5068 if (must_check_value)
5069 {
5070 wp_check_result e;
5071
5072 try
5073 {
5074 e = watchpoint_check (bs);
5075 }
5076 catch (const gdb_exception &ex)
5077 {
5078 exception_fprintf (gdb_stderr, ex,
5079 "Error evaluating expression "
5080 "for watchpoint %d\n",
5081 b->number);
5082
5083 SWITCH_THRU_ALL_UIS ()
5084 {
5085 printf_filtered (_("Watchpoint %d deleted.\n"),
5086 b->number);
5087 }
5088 watchpoint_del_at_next_stop (b);
5089 e = WP_DELETED;
5090 }
5091
5092 switch (e)
5093 {
5094 case WP_DELETED:
5095 /* We've already printed what needs to be printed. */
5096 bs->print_it = print_it_done;
5097 /* Stop. */
5098 break;
5099 case WP_IGNORE:
5100 bs->print_it = print_it_noop;
5101 bs->stop = 0;
5102 break;
5103 case WP_VALUE_CHANGED:
5104 if (b->type == bp_read_watchpoint)
5105 {
5106 /* There are two cases to consider here:
5107
5108 1. We're watching the triggered memory for reads.
5109 In that case, trust the target, and always report
5110 the watchpoint hit to the user. Even though
5111 reads don't cause value changes, the value may
5112 have changed since the last time it was read, and
5113 since we're not trapping writes, we will not see
5114 those, and as such we should ignore our notion of
5115 old value.
5116
5117 2. We're watching the triggered memory for both
5118 reads and writes. There are two ways this may
5119 happen:
5120
5121 2.1. This is a target that can't break on data
5122 reads only, but can break on accesses (reads or
5123 writes), such as e.g., x86. We detect this case
5124 at the time we try to insert read watchpoints.
5125
5126 2.2. Otherwise, the target supports read
5127 watchpoints, but, the user set an access or write
5128 watchpoint watching the same memory as this read
5129 watchpoint.
5130
5131 If we're watching memory writes as well as reads,
5132 ignore watchpoint hits when we find that the
5133 value hasn't changed, as reads don't cause
5134 changes. This still gives false positives when
5135 the program writes the same value to memory as
5136 what there was already in memory (we will confuse
5137 it for a read), but it's much better than
5138 nothing. */
5139
5140 int other_write_watchpoint = 0;
5141
5142 if (bl->watchpoint_type == hw_read)
5143 {
5144 for (breakpoint *other_b : all_breakpoints ())
5145 if (other_b->type == bp_hardware_watchpoint
5146 || other_b->type == bp_access_watchpoint)
5147 {
5148 struct watchpoint *other_w =
5149 (struct watchpoint *) other_b;
5150
5151 if (other_w->watchpoint_triggered
5152 == watch_triggered_yes)
5153 {
5154 other_write_watchpoint = 1;
5155 break;
5156 }
5157 }
5158 }
5159
5160 if (other_write_watchpoint
5161 || bl->watchpoint_type == hw_access)
5162 {
5163 /* We're watching the same memory for writes,
5164 and the value changed since the last time we
5165 updated it, so this trap must be for a write.
5166 Ignore it. */
5167 bs->print_it = print_it_noop;
5168 bs->stop = 0;
5169 }
5170 }
5171 break;
5172 case WP_VALUE_NOT_CHANGED:
5173 if (b->type == bp_hardware_watchpoint
5174 || b->type == bp_watchpoint)
5175 {
5176 /* Don't stop: write watchpoints shouldn't fire if
5177 the value hasn't changed. */
5178 bs->print_it = print_it_noop;
5179 bs->stop = 0;
5180 }
5181 /* Stop. */
5182 break;
5183 default:
5184 /* Can't happen. */
5185 break;
5186 }
5187 }
5188 else /* must_check_value == 0 */
5189 {
5190 /* This is a case where some watchpoint(s) triggered, but
5191 not at the address of this watchpoint, or else no
5192 watchpoint triggered after all. So don't print
5193 anything for this watchpoint. */
5194 bs->print_it = print_it_noop;
5195 bs->stop = 0;
5196 }
5197 }
5198 }
5199
5200 /* For breakpoints that are currently marked as telling gdb to stop,
5201 check conditions (condition proper, frame, thread and ignore count)
5202 of breakpoint referred to by BS. If we should not stop for this
5203 breakpoint, set BS->stop to 0. */
5204
5205 static void
5206 bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
5207 {
5208 const struct bp_location *bl;
5209 struct breakpoint *b;
5210 /* Assume stop. */
5211 bool condition_result = true;
5212 struct expression *cond;
5213
5214 gdb_assert (bs->stop);
5215
5216 /* BS is built for existing struct breakpoint. */
5217 bl = bs->bp_location_at.get ();
5218 gdb_assert (bl != NULL);
5219 b = bs->breakpoint_at;
5220 gdb_assert (b != NULL);
5221
5222 /* Even if the target evaluated the condition on its end and notified GDB, we
5223 need to do so again since GDB does not know if we stopped due to a
5224 breakpoint or a single step breakpoint. */
5225
5226 if (frame_id_p (b->frame_id)
5227 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5228 {
5229 bs->stop = 0;
5230 return;
5231 }
5232
5233 /* If this is a thread/task-specific breakpoint, don't waste cpu
5234 evaluating the condition if this isn't the specified
5235 thread/task. */
5236 if ((b->thread != -1 && b->thread != thread->global_num)
5237 || (b->task != 0 && b->task != ada_get_task_number (thread)))
5238 {
5239 bs->stop = 0;
5240 return;
5241 }
5242
5243 /* Evaluate extension language breakpoints that have a "stop" method
5244 implemented. */
5245 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5246
5247 if (is_watchpoint (b))
5248 {
5249 struct watchpoint *w = (struct watchpoint *) b;
5250
5251 cond = w->cond_exp.get ();
5252 }
5253 else
5254 cond = bl->cond.get ();
5255
5256 if (cond && b->disposition != disp_del_at_next_stop)
5257 {
5258 int within_current_scope = 1;
5259 struct watchpoint * w;
5260
5261 /* We use value_mark and value_free_to_mark because it could
5262 be a long time before we return to the command level and
5263 call free_all_values. We can't call free_all_values
5264 because we might be in the middle of evaluating a
5265 function call. */
5266 struct value *mark = value_mark ();
5267
5268 if (is_watchpoint (b))
5269 w = (struct watchpoint *) b;
5270 else
5271 w = NULL;
5272
5273 /* Need to select the frame, with all that implies so that
5274 the conditions will have the right context. Because we
5275 use the frame, we will not see an inlined function's
5276 variables when we arrive at a breakpoint at the start
5277 of the inlined function; the current frame will be the
5278 call site. */
5279 if (w == NULL || w->cond_exp_valid_block == NULL)
5280 select_frame (get_current_frame ());
5281 else
5282 {
5283 struct frame_info *frame;
5284
5285 /* For local watchpoint expressions, which particular
5286 instance of a local is being watched matters, so we
5287 keep track of the frame to evaluate the expression
5288 in. To evaluate the condition however, it doesn't
5289 really matter which instantiation of the function
5290 where the condition makes sense triggers the
5291 watchpoint. This allows an expression like "watch
5292 global if q > 10" set in `func', catch writes to
5293 global on all threads that call `func', or catch
5294 writes on all recursive calls of `func' by a single
5295 thread. We simply always evaluate the condition in
5296 the innermost frame that's executing where it makes
5297 sense to evaluate the condition. It seems
5298 intuitive. */
5299 frame = block_innermost_frame (w->cond_exp_valid_block);
5300 if (frame != NULL)
5301 select_frame (frame);
5302 else
5303 within_current_scope = 0;
5304 }
5305 if (within_current_scope)
5306 {
5307 try
5308 {
5309 condition_result = breakpoint_cond_eval (cond);
5310 }
5311 catch (const gdb_exception &ex)
5312 {
5313 exception_fprintf (gdb_stderr, ex,
5314 "Error in testing breakpoint condition:\n");
5315 }
5316 }
5317 else
5318 {
5319 warning (_("Watchpoint condition cannot be tested "
5320 "in the current scope"));
5321 /* If we failed to set the right context for this
5322 watchpoint, unconditionally report it. */
5323 }
5324 /* FIXME-someday, should give breakpoint #. */
5325 value_free_to_mark (mark);
5326 }
5327
5328 if (cond && !condition_result)
5329 {
5330 bs->stop = 0;
5331 }
5332 else if (b->ignore_count > 0)
5333 {
5334 b->ignore_count--;
5335 bs->stop = 0;
5336 /* Increase the hit count even though we don't stop. */
5337 ++(b->hit_count);
5338 gdb::observers::breakpoint_modified.notify (b);
5339 }
5340 }
5341
5342 /* Returns true if we need to track moribund locations of LOC's type
5343 on the current target. */
5344
5345 static int
5346 need_moribund_for_location_type (struct bp_location *loc)
5347 {
5348 return ((loc->loc_type == bp_loc_software_breakpoint
5349 && !target_supports_stopped_by_sw_breakpoint ())
5350 || (loc->loc_type == bp_loc_hardware_breakpoint
5351 && !target_supports_stopped_by_hw_breakpoint ()));
5352 }
5353
5354 /* See breakpoint.h. */
5355
5356 bpstat
5357 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5358 const struct target_waitstatus *ws)
5359 {
5360 bpstat bs_head = NULL, *bs_link = &bs_head;
5361
5362 for (breakpoint *b : all_breakpoints ())
5363 {
5364 if (!breakpoint_enabled (b))
5365 continue;
5366
5367 for (bp_location *bl : b->locations ())
5368 {
5369 /* For hardware watchpoints, we look only at the first
5370 location. The watchpoint_check function will work on the
5371 entire expression, not the individual locations. For
5372 read watchpoints, the watchpoints_triggered function has
5373 checked all locations already. */
5374 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5375 break;
5376
5377 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
5378 continue;
5379
5380 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5381 continue;
5382
5383 /* Come here if it's a watchpoint, or if the break address
5384 matches. */
5385
5386 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5387 explain stop. */
5388
5389 /* Assume we stop. Should we find a watchpoint that is not
5390 actually triggered, or if the condition of the breakpoint
5391 evaluates as false, we'll reset 'stop' to 0. */
5392 bs->stop = 1;
5393 bs->print = 1;
5394
5395 /* If this is a scope breakpoint, mark the associated
5396 watchpoint as triggered so that we will handle the
5397 out-of-scope event. We'll get to the watchpoint next
5398 iteration. */
5399 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5400 {
5401 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5402
5403 w->watchpoint_triggered = watch_triggered_yes;
5404 }
5405 }
5406 }
5407
5408 /* Check if a moribund breakpoint explains the stop. */
5409 if (!target_supports_stopped_by_sw_breakpoint ()
5410 || !target_supports_stopped_by_hw_breakpoint ())
5411 {
5412 for (bp_location *loc : moribund_locations)
5413 {
5414 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5415 && need_moribund_for_location_type (loc))
5416 {
5417 bpstat bs = new bpstats (loc, &bs_link);
5418 /* For hits of moribund locations, we should just proceed. */
5419 bs->stop = 0;
5420 bs->print = 0;
5421 bs->print_it = print_it_noop;
5422 }
5423 }
5424 }
5425
5426 return bs_head;
5427 }
5428
5429 /* See breakpoint.h. */
5430
5431 bpstat
5432 bpstat_stop_status (const address_space *aspace,
5433 CORE_ADDR bp_addr, thread_info *thread,
5434 const struct target_waitstatus *ws,
5435 bpstat stop_chain)
5436 {
5437 struct breakpoint *b = NULL;
5438 /* First item of allocated bpstat's. */
5439 bpstat bs_head = stop_chain;
5440 bpstat bs;
5441 int need_remove_insert;
5442 int removed_any;
5443
5444 /* First, build the bpstat chain with locations that explain a
5445 target stop, while being careful to not set the target running,
5446 as that may invalidate locations (in particular watchpoint
5447 locations are recreated). Resuming will happen here with
5448 breakpoint conditions or watchpoint expressions that include
5449 inferior function calls. */
5450 if (bs_head == NULL)
5451 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5452
5453 /* A bit of special processing for shlib breakpoints. We need to
5454 process solib loading here, so that the lists of loaded and
5455 unloaded libraries are correct before we handle "catch load" and
5456 "catch unload". */
5457 for (bs = bs_head; bs != NULL; bs = bs->next)
5458 {
5459 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5460 {
5461 handle_solib_event ();
5462 break;
5463 }
5464 }
5465
5466 /* Now go through the locations that caused the target to stop, and
5467 check whether we're interested in reporting this stop to higher
5468 layers, or whether we should resume the target transparently. */
5469
5470 removed_any = 0;
5471
5472 for (bs = bs_head; bs != NULL; bs = bs->next)
5473 {
5474 if (!bs->stop)
5475 continue;
5476
5477 b = bs->breakpoint_at;
5478 b->ops->check_status (bs);
5479 if (bs->stop)
5480 {
5481 bpstat_check_breakpoint_conditions (bs, thread);
5482
5483 if (bs->stop)
5484 {
5485 ++(b->hit_count);
5486 gdb::observers::breakpoint_modified.notify (b);
5487
5488 /* We will stop here. */
5489 if (b->disposition == disp_disable)
5490 {
5491 --(b->enable_count);
5492 if (b->enable_count <= 0)
5493 b->enable_state = bp_disabled;
5494 removed_any = 1;
5495 }
5496 if (b->silent)
5497 bs->print = 0;
5498 bs->commands = b->commands;
5499 if (command_line_is_silent (bs->commands
5500 ? bs->commands.get () : NULL))
5501 bs->print = 0;
5502
5503 b->ops->after_condition_true (bs);
5504 }
5505
5506 }
5507
5508 /* Print nothing for this entry if we don't stop or don't
5509 print. */
5510 if (!bs->stop || !bs->print)
5511 bs->print_it = print_it_noop;
5512 }
5513
5514 /* If we aren't stopping, the value of some hardware watchpoint may
5515 not have changed, but the intermediate memory locations we are
5516 watching may have. Don't bother if we're stopping; this will get
5517 done later. */
5518 need_remove_insert = 0;
5519 if (! bpstat_causes_stop (bs_head))
5520 for (bs = bs_head; bs != NULL; bs = bs->next)
5521 if (!bs->stop
5522 && bs->breakpoint_at
5523 && is_hardware_watchpoint (bs->breakpoint_at))
5524 {
5525 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5526
5527 update_watchpoint (w, 0 /* don't reparse. */);
5528 need_remove_insert = 1;
5529 }
5530
5531 if (need_remove_insert)
5532 update_global_location_list (UGLL_MAY_INSERT);
5533 else if (removed_any)
5534 update_global_location_list (UGLL_DONT_INSERT);
5535
5536 return bs_head;
5537 }
5538
5539 static void
5540 handle_jit_event (CORE_ADDR address)
5541 {
5542 struct gdbarch *gdbarch;
5543
5544 infrun_debug_printf ("handling bp_jit_event");
5545
5546 /* Switch terminal for any messages produced by
5547 breakpoint_re_set. */
5548 target_terminal::ours_for_output ();
5549
5550 gdbarch = get_frame_arch (get_current_frame ());
5551 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5552 thus it is expected that its objectfile can be found through
5553 minimal symbol lookup. If it doesn't work (and assert fails), it
5554 most likely means that `jit_breakpoint_re_set` was changes and this
5555 function needs to be updated too. */
5556 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5557 gdb_assert (jit_bp_sym.objfile != nullptr);
5558 jit_event_handler (gdbarch, jit_bp_sym.objfile);
5559
5560 target_terminal::inferior ();
5561 }
5562
5563 /* Prepare WHAT final decision for infrun. */
5564
5565 /* Decide what infrun needs to do with this bpstat. */
5566
5567 struct bpstat_what
5568 bpstat_what (bpstat bs_head)
5569 {
5570 struct bpstat_what retval;
5571 bpstat bs;
5572
5573 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5574 retval.call_dummy = STOP_NONE;
5575 retval.is_longjmp = false;
5576
5577 for (bs = bs_head; bs != NULL; bs = bs->next)
5578 {
5579 /* Extract this BS's action. After processing each BS, we check
5580 if its action overrides all we've seem so far. */
5581 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5582 enum bptype bptype;
5583
5584 if (bs->breakpoint_at == NULL)
5585 {
5586 /* I suspect this can happen if it was a momentary
5587 breakpoint which has since been deleted. */
5588 bptype = bp_none;
5589 }
5590 else
5591 bptype = bs->breakpoint_at->type;
5592
5593 switch (bptype)
5594 {
5595 case bp_none:
5596 break;
5597 case bp_breakpoint:
5598 case bp_hardware_breakpoint:
5599 case bp_single_step:
5600 case bp_until:
5601 case bp_finish:
5602 case bp_shlib_event:
5603 if (bs->stop)
5604 {
5605 if (bs->print)
5606 this_action = BPSTAT_WHAT_STOP_NOISY;
5607 else
5608 this_action = BPSTAT_WHAT_STOP_SILENT;
5609 }
5610 else
5611 this_action = BPSTAT_WHAT_SINGLE;
5612 break;
5613 case bp_watchpoint:
5614 case bp_hardware_watchpoint:
5615 case bp_read_watchpoint:
5616 case bp_access_watchpoint:
5617 if (bs->stop)
5618 {
5619 if (bs->print)
5620 this_action = BPSTAT_WHAT_STOP_NOISY;
5621 else
5622 this_action = BPSTAT_WHAT_STOP_SILENT;
5623 }
5624 else
5625 {
5626 /* There was a watchpoint, but we're not stopping.
5627 This requires no further action. */
5628 }
5629 break;
5630 case bp_longjmp:
5631 case bp_longjmp_call_dummy:
5632 case bp_exception:
5633 if (bs->stop)
5634 {
5635 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5636 retval.is_longjmp = bptype != bp_exception;
5637 }
5638 else
5639 this_action = BPSTAT_WHAT_SINGLE;
5640 break;
5641 case bp_longjmp_resume:
5642 case bp_exception_resume:
5643 if (bs->stop)
5644 {
5645 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5646 retval.is_longjmp = bptype == bp_longjmp_resume;
5647 }
5648 else
5649 this_action = BPSTAT_WHAT_SINGLE;
5650 break;
5651 case bp_step_resume:
5652 if (bs->stop)
5653 this_action = BPSTAT_WHAT_STEP_RESUME;
5654 else
5655 {
5656 /* It is for the wrong frame. */
5657 this_action = BPSTAT_WHAT_SINGLE;
5658 }
5659 break;
5660 case bp_hp_step_resume:
5661 if (bs->stop)
5662 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5663 else
5664 {
5665 /* It is for the wrong frame. */
5666 this_action = BPSTAT_WHAT_SINGLE;
5667 }
5668 break;
5669 case bp_watchpoint_scope:
5670 case bp_thread_event:
5671 case bp_overlay_event:
5672 case bp_longjmp_master:
5673 case bp_std_terminate_master:
5674 case bp_exception_master:
5675 this_action = BPSTAT_WHAT_SINGLE;
5676 break;
5677 case bp_catchpoint:
5678 if (bs->stop)
5679 {
5680 if (bs->print)
5681 this_action = BPSTAT_WHAT_STOP_NOISY;
5682 else
5683 this_action = BPSTAT_WHAT_STOP_SILENT;
5684 }
5685 else
5686 {
5687 /* Some catchpoints are implemented with breakpoints.
5688 For those, we need to step over the breakpoint. */
5689 if (bs->bp_location_at->loc_type != bp_loc_other)
5690 this_action = BPSTAT_WHAT_SINGLE;
5691 }
5692 break;
5693 case bp_jit_event:
5694 this_action = BPSTAT_WHAT_SINGLE;
5695 break;
5696 case bp_call_dummy:
5697 /* Make sure the action is stop (silent or noisy),
5698 so infrun.c pops the dummy frame. */
5699 retval.call_dummy = STOP_STACK_DUMMY;
5700 this_action = BPSTAT_WHAT_STOP_SILENT;
5701 break;
5702 case bp_std_terminate:
5703 /* Make sure the action is stop (silent or noisy),
5704 so infrun.c pops the dummy frame. */
5705 retval.call_dummy = STOP_STD_TERMINATE;
5706 this_action = BPSTAT_WHAT_STOP_SILENT;
5707 break;
5708 case bp_tracepoint:
5709 case bp_fast_tracepoint:
5710 case bp_static_tracepoint:
5711 /* Tracepoint hits should not be reported back to GDB, and
5712 if one got through somehow, it should have been filtered
5713 out already. */
5714 internal_error (__FILE__, __LINE__,
5715 _("bpstat_what: tracepoint encountered"));
5716 break;
5717 case bp_gnu_ifunc_resolver:
5718 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5719 this_action = BPSTAT_WHAT_SINGLE;
5720 break;
5721 case bp_gnu_ifunc_resolver_return:
5722 /* The breakpoint will be removed, execution will restart from the
5723 PC of the former breakpoint. */
5724 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5725 break;
5726
5727 case bp_dprintf:
5728 if (bs->stop)
5729 this_action = BPSTAT_WHAT_STOP_SILENT;
5730 else
5731 this_action = BPSTAT_WHAT_SINGLE;
5732 break;
5733
5734 default:
5735 internal_error (__FILE__, __LINE__,
5736 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5737 }
5738
5739 retval.main_action = std::max (retval.main_action, this_action);
5740 }
5741
5742 return retval;
5743 }
5744
5745 void
5746 bpstat_run_callbacks (bpstat bs_head)
5747 {
5748 bpstat bs;
5749
5750 for (bs = bs_head; bs != NULL; bs = bs->next)
5751 {
5752 struct breakpoint *b = bs->breakpoint_at;
5753
5754 if (b == NULL)
5755 continue;
5756 switch (b->type)
5757 {
5758 case bp_jit_event:
5759 handle_jit_event (bs->bp_location_at->address);
5760 break;
5761 case bp_gnu_ifunc_resolver:
5762 gnu_ifunc_resolver_stop (b);
5763 break;
5764 case bp_gnu_ifunc_resolver_return:
5765 gnu_ifunc_resolver_return_stop (b);
5766 break;
5767 }
5768 }
5769 }
5770
5771 /* See breakpoint.h. */
5772
5773 bool
5774 bpstat_should_step ()
5775 {
5776 for (breakpoint *b : all_breakpoints ())
5777 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5778 return true;
5779
5780 return false;
5781 }
5782
5783 /* See breakpoint.h. */
5784
5785 bool
5786 bpstat_causes_stop (bpstat bs)
5787 {
5788 for (; bs != NULL; bs = bs->next)
5789 if (bs->stop)
5790 return true;
5791
5792 return false;
5793 }
5794
5795 \f
5796
5797 /* Compute a string of spaces suitable to indent the next line
5798 so it starts at the position corresponding to the table column
5799 named COL_NAME in the currently active table of UIOUT. */
5800
5801 static char *
5802 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5803 {
5804 static char wrap_indent[80];
5805 int i, total_width, width, align;
5806 const char *text;
5807
5808 total_width = 0;
5809 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5810 {
5811 if (strcmp (text, col_name) == 0)
5812 {
5813 gdb_assert (total_width < sizeof wrap_indent);
5814 memset (wrap_indent, ' ', total_width);
5815 wrap_indent[total_width] = 0;
5816
5817 return wrap_indent;
5818 }
5819
5820 total_width += width + 1;
5821 }
5822
5823 return NULL;
5824 }
5825
5826 /* Determine if the locations of this breakpoint will have their conditions
5827 evaluated by the target, host or a mix of both. Returns the following:
5828
5829 "host": Host evals condition.
5830 "host or target": Host or Target evals condition.
5831 "target": Target evals condition.
5832 */
5833
5834 static const char *
5835 bp_condition_evaluator (struct breakpoint *b)
5836 {
5837 char host_evals = 0;
5838 char target_evals = 0;
5839
5840 if (!b)
5841 return NULL;
5842
5843 if (!is_breakpoint (b))
5844 return NULL;
5845
5846 if (gdb_evaluates_breakpoint_condition_p ()
5847 || !target_supports_evaluation_of_breakpoint_conditions ())
5848 return condition_evaluation_host;
5849
5850 for (bp_location *bl : b->locations ())
5851 {
5852 if (bl->cond_bytecode)
5853 target_evals++;
5854 else
5855 host_evals++;
5856 }
5857
5858 if (host_evals && target_evals)
5859 return condition_evaluation_both;
5860 else if (target_evals)
5861 return condition_evaluation_target;
5862 else
5863 return condition_evaluation_host;
5864 }
5865
5866 /* Determine the breakpoint location's condition evaluator. This is
5867 similar to bp_condition_evaluator, but for locations. */
5868
5869 static const char *
5870 bp_location_condition_evaluator (struct bp_location *bl)
5871 {
5872 if (bl && !is_breakpoint (bl->owner))
5873 return NULL;
5874
5875 if (gdb_evaluates_breakpoint_condition_p ()
5876 || !target_supports_evaluation_of_breakpoint_conditions ())
5877 return condition_evaluation_host;
5878
5879 if (bl && bl->cond_bytecode)
5880 return condition_evaluation_target;
5881 else
5882 return condition_evaluation_host;
5883 }
5884
5885 /* Print the LOC location out of the list of B->LOC locations. */
5886
5887 static void
5888 print_breakpoint_location (struct breakpoint *b,
5889 struct bp_location *loc)
5890 {
5891 struct ui_out *uiout = current_uiout;
5892
5893 scoped_restore_current_program_space restore_pspace;
5894
5895 if (loc != NULL && loc->shlib_disabled)
5896 loc = NULL;
5897
5898 if (loc != NULL)
5899 set_current_program_space (loc->pspace);
5900
5901 if (b->display_canonical)
5902 uiout->field_string ("what", event_location_to_string (b->location.get ()));
5903 else if (loc && loc->symtab)
5904 {
5905 const struct symbol *sym = loc->symbol;
5906
5907 if (sym)
5908 {
5909 uiout->text ("in ");
5910 uiout->field_string ("func", sym->print_name (),
5911 function_name_style.style ());
5912 uiout->text (" ");
5913 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5914 uiout->text ("at ");
5915 }
5916 uiout->field_string ("file",
5917 symtab_to_filename_for_display (loc->symtab),
5918 file_name_style.style ());
5919 uiout->text (":");
5920
5921 if (uiout->is_mi_like_p ())
5922 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
5923
5924 uiout->field_signed ("line", loc->line_number);
5925 }
5926 else if (loc)
5927 {
5928 string_file stb;
5929
5930 print_address_symbolic (loc->gdbarch, loc->address, &stb,
5931 demangle, "");
5932 uiout->field_stream ("at", stb);
5933 }
5934 else
5935 {
5936 uiout->field_string ("pending",
5937 event_location_to_string (b->location.get ()));
5938 /* If extra_string is available, it could be holding a condition
5939 or dprintf arguments. In either case, make sure it is printed,
5940 too, but only for non-MI streams. */
5941 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
5942 {
5943 if (b->type == bp_dprintf)
5944 uiout->text (",");
5945 else
5946 uiout->text (" ");
5947 uiout->text (b->extra_string);
5948 }
5949 }
5950
5951 if (loc && is_breakpoint (b)
5952 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5953 && bp_condition_evaluator (b) == condition_evaluation_both)
5954 {
5955 uiout->text (" (");
5956 uiout->field_string ("evaluated-by",
5957 bp_location_condition_evaluator (loc));
5958 uiout->text (")");
5959 }
5960 }
5961
5962 static const char *
5963 bptype_string (enum bptype type)
5964 {
5965 struct ep_type_description
5966 {
5967 enum bptype type;
5968 const char *description;
5969 };
5970 static struct ep_type_description bptypes[] =
5971 {
5972 {bp_none, "?deleted?"},
5973 {bp_breakpoint, "breakpoint"},
5974 {bp_hardware_breakpoint, "hw breakpoint"},
5975 {bp_single_step, "sw single-step"},
5976 {bp_until, "until"},
5977 {bp_finish, "finish"},
5978 {bp_watchpoint, "watchpoint"},
5979 {bp_hardware_watchpoint, "hw watchpoint"},
5980 {bp_read_watchpoint, "read watchpoint"},
5981 {bp_access_watchpoint, "acc watchpoint"},
5982 {bp_longjmp, "longjmp"},
5983 {bp_longjmp_resume, "longjmp resume"},
5984 {bp_longjmp_call_dummy, "longjmp for call dummy"},
5985 {bp_exception, "exception"},
5986 {bp_exception_resume, "exception resume"},
5987 {bp_step_resume, "step resume"},
5988 {bp_hp_step_resume, "high-priority step resume"},
5989 {bp_watchpoint_scope, "watchpoint scope"},
5990 {bp_call_dummy, "call dummy"},
5991 {bp_std_terminate, "std::terminate"},
5992 {bp_shlib_event, "shlib events"},
5993 {bp_thread_event, "thread events"},
5994 {bp_overlay_event, "overlay events"},
5995 {bp_longjmp_master, "longjmp master"},
5996 {bp_std_terminate_master, "std::terminate master"},
5997 {bp_exception_master, "exception master"},
5998 {bp_catchpoint, "catchpoint"},
5999 {bp_tracepoint, "tracepoint"},
6000 {bp_fast_tracepoint, "fast tracepoint"},
6001 {bp_static_tracepoint, "static tracepoint"},
6002 {bp_dprintf, "dprintf"},
6003 {bp_jit_event, "jit events"},
6004 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6005 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6006 };
6007
6008 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6009 || ((int) type != bptypes[(int) type].type))
6010 internal_error (__FILE__, __LINE__,
6011 _("bptypes table does not describe type #%d."),
6012 (int) type);
6013
6014 return bptypes[(int) type].description;
6015 }
6016
6017 /* For MI, output a field named 'thread-groups' with a list as the value.
6018 For CLI, prefix the list with the string 'inf'. */
6019
6020 static void
6021 output_thread_groups (struct ui_out *uiout,
6022 const char *field_name,
6023 const std::vector<int> &inf_nums,
6024 int mi_only)
6025 {
6026 int is_mi = uiout->is_mi_like_p ();
6027
6028 /* For backward compatibility, don't display inferiors in CLI unless
6029 there are several. Always display them for MI. */
6030 if (!is_mi && mi_only)
6031 return;
6032
6033 ui_out_emit_list list_emitter (uiout, field_name);
6034
6035 for (size_t i = 0; i < inf_nums.size (); i++)
6036 {
6037 if (is_mi)
6038 {
6039 char mi_group[10];
6040
6041 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
6042 uiout->field_string (NULL, mi_group);
6043 }
6044 else
6045 {
6046 if (i == 0)
6047 uiout->text (" inf ");
6048 else
6049 uiout->text (", ");
6050
6051 uiout->text (plongest (inf_nums[i]));
6052 }
6053 }
6054 }
6055
6056 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6057 instead of going via breakpoint_ops::print_one. This makes "maint
6058 info breakpoints" show the software breakpoint locations of
6059 catchpoints, which are considered internal implementation
6060 detail. */
6061
6062 static void
6063 print_one_breakpoint_location (struct breakpoint *b,
6064 struct bp_location *loc,
6065 int loc_number,
6066 struct bp_location **last_loc,
6067 int allflag, bool raw_loc)
6068 {
6069 struct command_line *l;
6070 static char bpenables[] = "nynny";
6071
6072 struct ui_out *uiout = current_uiout;
6073 int header_of_multiple = 0;
6074 int part_of_multiple = (loc != NULL);
6075 struct value_print_options opts;
6076
6077 get_user_print_options (&opts);
6078
6079 gdb_assert (!loc || loc_number != 0);
6080 /* See comment in print_one_breakpoint concerning treatment of
6081 breakpoints with single disabled location. */
6082 if (loc == NULL
6083 && (b->loc != NULL
6084 && (b->loc->next != NULL
6085 || !b->loc->enabled || b->loc->disabled_by_cond)))
6086 header_of_multiple = 1;
6087 if (loc == NULL)
6088 loc = b->loc;
6089
6090 annotate_record ();
6091
6092 /* 1 */
6093 annotate_field (0);
6094 if (part_of_multiple)
6095 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6096 else
6097 uiout->field_signed ("number", b->number);
6098
6099 /* 2 */
6100 annotate_field (1);
6101 if (part_of_multiple)
6102 uiout->field_skip ("type");
6103 else
6104 uiout->field_string ("type", bptype_string (b->type));
6105
6106 /* 3 */
6107 annotate_field (2);
6108 if (part_of_multiple)
6109 uiout->field_skip ("disp");
6110 else
6111 uiout->field_string ("disp", bpdisp_text (b->disposition));
6112
6113 /* 4 */
6114 annotate_field (3);
6115 /* For locations that are disabled because of an invalid condition,
6116 display "N*" on CLI, where "*" refers to a footnote below the
6117 table. For MI, simply display a "N" without a footnote. */
6118 const char *N = (uiout->is_mi_like_p ()) ? "N" : "N*";
6119 if (part_of_multiple)
6120 uiout->field_string ("enabled", (loc->disabled_by_cond ? N
6121 : (loc->enabled ? "y" : "n")));
6122 else
6123 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6124
6125 /* 5 and 6 */
6126 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
6127 b->ops->print_one (b, last_loc);
6128 else
6129 {
6130 if (is_watchpoint (b))
6131 {
6132 struct watchpoint *w = (struct watchpoint *) b;
6133
6134 /* Field 4, the address, is omitted (which makes the columns
6135 not line up too nicely with the headers, but the effect
6136 is relatively readable). */
6137 if (opts.addressprint)
6138 uiout->field_skip ("addr");
6139 annotate_field (5);
6140 uiout->field_string ("what", w->exp_string);
6141 }
6142 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6143 || is_ada_exception_catchpoint (b))
6144 {
6145 if (opts.addressprint)
6146 {
6147 annotate_field (4);
6148 if (header_of_multiple)
6149 uiout->field_string ("addr", "<MULTIPLE>",
6150 metadata_style.style ());
6151 else if (b->loc == NULL || loc->shlib_disabled)
6152 uiout->field_string ("addr", "<PENDING>",
6153 metadata_style.style ());
6154 else
6155 uiout->field_core_addr ("addr",
6156 loc->gdbarch, loc->address);
6157 }
6158 annotate_field (5);
6159 if (!header_of_multiple)
6160 print_breakpoint_location (b, loc);
6161 if (b->loc)
6162 *last_loc = b->loc;
6163 }
6164 }
6165
6166 if (loc != NULL && !header_of_multiple)
6167 {
6168 std::vector<int> inf_nums;
6169 int mi_only = 1;
6170
6171 for (inferior *inf : all_inferiors ())
6172 {
6173 if (inf->pspace == loc->pspace)
6174 inf_nums.push_back (inf->num);
6175 }
6176
6177 /* For backward compatibility, don't display inferiors in CLI unless
6178 there are several. Always display for MI. */
6179 if (allflag
6180 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6181 && (program_spaces.size () > 1
6182 || number_of_inferiors () > 1)
6183 /* LOC is for existing B, it cannot be in
6184 moribund_locations and thus having NULL OWNER. */
6185 && loc->owner->type != bp_catchpoint))
6186 mi_only = 0;
6187 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6188 }
6189
6190 if (!part_of_multiple)
6191 {
6192 if (b->thread != -1)
6193 {
6194 /* FIXME: This seems to be redundant and lost here; see the
6195 "stop only in" line a little further down. */
6196 uiout->text (" thread ");
6197 uiout->field_signed ("thread", b->thread);
6198 }
6199 else if (b->task != 0)
6200 {
6201 uiout->text (" task ");
6202 uiout->field_signed ("task", b->task);
6203 }
6204 }
6205
6206 uiout->text ("\n");
6207
6208 if (!part_of_multiple)
6209 b->ops->print_one_detail (b, uiout);
6210
6211 if (part_of_multiple && frame_id_p (b->frame_id))
6212 {
6213 annotate_field (6);
6214 uiout->text ("\tstop only in stack frame at ");
6215 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6216 the frame ID. */
6217 uiout->field_core_addr ("frame",
6218 b->gdbarch, b->frame_id.stack_addr);
6219 uiout->text ("\n");
6220 }
6221
6222 if (!part_of_multiple && b->cond_string)
6223 {
6224 annotate_field (7);
6225 if (is_tracepoint (b))
6226 uiout->text ("\ttrace only if ");
6227 else
6228 uiout->text ("\tstop only if ");
6229 uiout->field_string ("cond", b->cond_string);
6230
6231 /* Print whether the target is doing the breakpoint's condition
6232 evaluation. If GDB is doing the evaluation, don't print anything. */
6233 if (is_breakpoint (b)
6234 && breakpoint_condition_evaluation_mode ()
6235 == condition_evaluation_target)
6236 {
6237 uiout->message (" (%pF evals)",
6238 string_field ("evaluated-by",
6239 bp_condition_evaluator (b)));
6240 }
6241 uiout->text ("\n");
6242 }
6243
6244 if (!part_of_multiple && b->thread != -1)
6245 {
6246 /* FIXME should make an annotation for this. */
6247 uiout->text ("\tstop only in thread ");
6248 if (uiout->is_mi_like_p ())
6249 uiout->field_signed ("thread", b->thread);
6250 else
6251 {
6252 struct thread_info *thr = find_thread_global_id (b->thread);
6253
6254 uiout->field_string ("thread", print_thread_id (thr));
6255 }
6256 uiout->text ("\n");
6257 }
6258
6259 if (!part_of_multiple)
6260 {
6261 if (b->hit_count)
6262 {
6263 /* FIXME should make an annotation for this. */
6264 if (is_catchpoint (b))
6265 uiout->text ("\tcatchpoint");
6266 else if (is_tracepoint (b))
6267 uiout->text ("\ttracepoint");
6268 else
6269 uiout->text ("\tbreakpoint");
6270 uiout->text (" already hit ");
6271 uiout->field_signed ("times", b->hit_count);
6272 if (b->hit_count == 1)
6273 uiout->text (" time\n");
6274 else
6275 uiout->text (" times\n");
6276 }
6277 else
6278 {
6279 /* Output the count also if it is zero, but only if this is mi. */
6280 if (uiout->is_mi_like_p ())
6281 uiout->field_signed ("times", b->hit_count);
6282 }
6283 }
6284
6285 if (!part_of_multiple && b->ignore_count)
6286 {
6287 annotate_field (8);
6288 uiout->message ("\tignore next %pF hits\n",
6289 signed_field ("ignore", b->ignore_count));
6290 }
6291
6292 /* Note that an enable count of 1 corresponds to "enable once"
6293 behavior, which is reported by the combination of enablement and
6294 disposition, so we don't need to mention it here. */
6295 if (!part_of_multiple && b->enable_count > 1)
6296 {
6297 annotate_field (8);
6298 uiout->text ("\tdisable after ");
6299 /* Tweak the wording to clarify that ignore and enable counts
6300 are distinct, and have additive effect. */
6301 if (b->ignore_count)
6302 uiout->text ("additional ");
6303 else
6304 uiout->text ("next ");
6305 uiout->field_signed ("enable", b->enable_count);
6306 uiout->text (" hits\n");
6307 }
6308
6309 if (!part_of_multiple && is_tracepoint (b))
6310 {
6311 struct tracepoint *tp = (struct tracepoint *) b;
6312
6313 if (tp->traceframe_usage)
6314 {
6315 uiout->text ("\ttrace buffer usage ");
6316 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6317 uiout->text (" bytes\n");
6318 }
6319 }
6320
6321 l = b->commands ? b->commands.get () : NULL;
6322 if (!part_of_multiple && l)
6323 {
6324 annotate_field (9);
6325 ui_out_emit_tuple tuple_emitter (uiout, "script");
6326 print_command_lines (uiout, l, 4);
6327 }
6328
6329 if (is_tracepoint (b))
6330 {
6331 struct tracepoint *t = (struct tracepoint *) b;
6332
6333 if (!part_of_multiple && t->pass_count)
6334 {
6335 annotate_field (10);
6336 uiout->text ("\tpass count ");
6337 uiout->field_signed ("pass", t->pass_count);
6338 uiout->text (" \n");
6339 }
6340
6341 /* Don't display it when tracepoint or tracepoint location is
6342 pending. */
6343 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6344 {
6345 annotate_field (11);
6346
6347 if (uiout->is_mi_like_p ())
6348 uiout->field_string ("installed",
6349 loc->inserted ? "y" : "n");
6350 else
6351 {
6352 if (loc->inserted)
6353 uiout->text ("\t");
6354 else
6355 uiout->text ("\tnot ");
6356 uiout->text ("installed on target\n");
6357 }
6358 }
6359 }
6360
6361 if (uiout->is_mi_like_p () && !part_of_multiple)
6362 {
6363 if (is_watchpoint (b))
6364 {
6365 struct watchpoint *w = (struct watchpoint *) b;
6366
6367 uiout->field_string ("original-location", w->exp_string);
6368 }
6369 else if (b->location != NULL
6370 && event_location_to_string (b->location.get ()) != NULL)
6371 uiout->field_string ("original-location",
6372 event_location_to_string (b->location.get ()));
6373 }
6374 }
6375
6376 /* See breakpoint.h. */
6377
6378 bool fix_multi_location_breakpoint_output_globally = false;
6379
6380 static void
6381 print_one_breakpoint (struct breakpoint *b,
6382 struct bp_location **last_loc,
6383 int allflag)
6384 {
6385 struct ui_out *uiout = current_uiout;
6386 bool use_fixed_output
6387 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6388 || fix_multi_location_breakpoint_output_globally);
6389
6390 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6391 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
6392
6393 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6394 are outside. */
6395 if (!use_fixed_output)
6396 bkpt_tuple_emitter.reset ();
6397
6398 /* If this breakpoint has custom print function,
6399 it's already printed. Otherwise, print individual
6400 locations, if any. */
6401 if (b->ops == NULL
6402 || b->ops->print_one == NULL
6403 || allflag)
6404 {
6405 /* If breakpoint has a single location that is disabled, we
6406 print it as if it had several locations, since otherwise it's
6407 hard to represent "breakpoint enabled, location disabled"
6408 situation.
6409
6410 Note that while hardware watchpoints have several locations
6411 internally, that's not a property exposed to users.
6412
6413 Likewise, while catchpoints may be implemented with
6414 breakpoints (e.g., catch throw), that's not a property
6415 exposed to users. We do however display the internal
6416 breakpoint locations with "maint info breakpoints". */
6417 if (!is_hardware_watchpoint (b)
6418 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6419 || is_ada_exception_catchpoint (b))
6420 && (allflag
6421 || (b->loc && (b->loc->next
6422 || !b->loc->enabled
6423 || b->loc->disabled_by_cond))))
6424 {
6425 gdb::optional<ui_out_emit_list> locations_list;
6426
6427 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6428 MI record. For later versions, place breakpoint locations in a
6429 list. */
6430 if (uiout->is_mi_like_p () && use_fixed_output)
6431 locations_list.emplace (uiout, "locations");
6432
6433 int n = 1;
6434 for (bp_location *loc : b->locations ())
6435 {
6436 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6437 print_one_breakpoint_location (b, loc, n, last_loc,
6438 allflag, allflag);
6439 n++;
6440 }
6441 }
6442 }
6443 }
6444
6445 static int
6446 breakpoint_address_bits (struct breakpoint *b)
6447 {
6448 int print_address_bits = 0;
6449
6450 /* Software watchpoints that aren't watching memory don't have an
6451 address to print. */
6452 if (is_no_memory_software_watchpoint (b))
6453 return 0;
6454
6455 for (bp_location *loc : b->locations ())
6456 {
6457 int addr_bit;
6458
6459 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6460 if (addr_bit > print_address_bits)
6461 print_address_bits = addr_bit;
6462 }
6463
6464 return print_address_bits;
6465 }
6466
6467 /* See breakpoint.h. */
6468
6469 void
6470 print_breakpoint (breakpoint *b)
6471 {
6472 struct bp_location *dummy_loc = NULL;
6473 print_one_breakpoint (b, &dummy_loc, 0);
6474 }
6475
6476 /* Return true if this breakpoint was set by the user, false if it is
6477 internal or momentary. */
6478
6479 int
6480 user_breakpoint_p (struct breakpoint *b)
6481 {
6482 return b->number > 0;
6483 }
6484
6485 /* See breakpoint.h. */
6486
6487 int
6488 pending_breakpoint_p (struct breakpoint *b)
6489 {
6490 return b->loc == NULL;
6491 }
6492
6493 /* Print information on breakpoints (including watchpoints and tracepoints).
6494
6495 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6496 understood by number_or_range_parser. Only breakpoints included in this
6497 list are then printed.
6498
6499 If SHOW_INTERNAL is true, print internal breakpoints.
6500
6501 If FILTER is non-NULL, call it on each breakpoint and only include the
6502 ones for which it returns true.
6503
6504 Return the total number of breakpoints listed. */
6505
6506 static int
6507 breakpoint_1 (const char *bp_num_list, bool show_internal,
6508 bool (*filter) (const struct breakpoint *))
6509 {
6510 struct bp_location *last_loc = NULL;
6511 int nr_printable_breakpoints;
6512 struct value_print_options opts;
6513 int print_address_bits = 0;
6514 int print_type_col_width = 14;
6515 struct ui_out *uiout = current_uiout;
6516 bool has_disabled_by_cond_location = false;
6517
6518 get_user_print_options (&opts);
6519
6520 /* Compute the number of rows in the table, as well as the size
6521 required for address fields. */
6522 nr_printable_breakpoints = 0;
6523 for (breakpoint *b : all_breakpoints ())
6524 {
6525 /* If we have a filter, only list the breakpoints it accepts. */
6526 if (filter && !filter (b))
6527 continue;
6528
6529 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6530 accept. Skip the others. */
6531 if (bp_num_list != NULL && *bp_num_list != '\0')
6532 {
6533 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6534 continue;
6535 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6536 continue;
6537 }
6538
6539 if (show_internal || user_breakpoint_p (b))
6540 {
6541 int addr_bit, type_len;
6542
6543 addr_bit = breakpoint_address_bits (b);
6544 if (addr_bit > print_address_bits)
6545 print_address_bits = addr_bit;
6546
6547 type_len = strlen (bptype_string (b->type));
6548 if (type_len > print_type_col_width)
6549 print_type_col_width = type_len;
6550
6551 nr_printable_breakpoints++;
6552 }
6553 }
6554
6555 {
6556 ui_out_emit_table table_emitter (uiout,
6557 opts.addressprint ? 6 : 5,
6558 nr_printable_breakpoints,
6559 "BreakpointTable");
6560
6561 if (nr_printable_breakpoints > 0)
6562 annotate_breakpoints_headers ();
6563 if (nr_printable_breakpoints > 0)
6564 annotate_field (0);
6565 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6566 if (nr_printable_breakpoints > 0)
6567 annotate_field (1);
6568 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6569 if (nr_printable_breakpoints > 0)
6570 annotate_field (2);
6571 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6572 if (nr_printable_breakpoints > 0)
6573 annotate_field (3);
6574 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6575 if (opts.addressprint)
6576 {
6577 if (nr_printable_breakpoints > 0)
6578 annotate_field (4);
6579 if (print_address_bits <= 32)
6580 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6581 else
6582 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6583 }
6584 if (nr_printable_breakpoints > 0)
6585 annotate_field (5);
6586 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6587 uiout->table_body ();
6588 if (nr_printable_breakpoints > 0)
6589 annotate_breakpoints_table ();
6590
6591 for (breakpoint *b : all_breakpoints ())
6592 {
6593 QUIT;
6594 /* If we have a filter, only list the breakpoints it accepts. */
6595 if (filter && !filter (b))
6596 continue;
6597
6598 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6599 accept. Skip the others. */
6600
6601 if (bp_num_list != NULL && *bp_num_list != '\0')
6602 {
6603 if (show_internal) /* maintenance info breakpoint */
6604 {
6605 if (parse_and_eval_long (bp_num_list) != b->number)
6606 continue;
6607 }
6608 else /* all others */
6609 {
6610 if (!number_is_in_list (bp_num_list, b->number))
6611 continue;
6612 }
6613 }
6614 /* We only print out user settable breakpoints unless the
6615 show_internal is set. */
6616 if (show_internal || user_breakpoint_p (b))
6617 {
6618 print_one_breakpoint (b, &last_loc, show_internal);
6619 for (bp_location *loc : b->locations ())
6620 if (loc->disabled_by_cond)
6621 has_disabled_by_cond_location = true;
6622 }
6623 }
6624 }
6625
6626 if (nr_printable_breakpoints == 0)
6627 {
6628 /* If there's a filter, let the caller decide how to report
6629 empty list. */
6630 if (!filter)
6631 {
6632 if (bp_num_list == NULL || *bp_num_list == '\0')
6633 uiout->message ("No breakpoints or watchpoints.\n");
6634 else
6635 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6636 bp_num_list);
6637 }
6638 }
6639 else
6640 {
6641 if (last_loc && !server_command)
6642 set_next_address (last_loc->gdbarch, last_loc->address);
6643
6644 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
6645 uiout->message (_("(*): Breakpoint condition is invalid at this "
6646 "location.\n"));
6647 }
6648
6649 /* FIXME? Should this be moved up so that it is only called when
6650 there have been breakpoints? */
6651 annotate_breakpoints_table_end ();
6652
6653 return nr_printable_breakpoints;
6654 }
6655
6656 /* Display the value of default-collect in a way that is generally
6657 compatible with the breakpoint list. */
6658
6659 static void
6660 default_collect_info (void)
6661 {
6662 struct ui_out *uiout = current_uiout;
6663
6664 /* If it has no value (which is frequently the case), say nothing; a
6665 message like "No default-collect." gets in user's face when it's
6666 not wanted. */
6667 if (!*default_collect)
6668 return;
6669
6670 /* The following phrase lines up nicely with per-tracepoint collect
6671 actions. */
6672 uiout->text ("default collect ");
6673 uiout->field_string ("default-collect", default_collect);
6674 uiout->text (" \n");
6675 }
6676
6677 static void
6678 info_breakpoints_command (const char *args, int from_tty)
6679 {
6680 breakpoint_1 (args, false, NULL);
6681
6682 default_collect_info ();
6683 }
6684
6685 static void
6686 info_watchpoints_command (const char *args, int from_tty)
6687 {
6688 int num_printed = breakpoint_1 (args, false, is_watchpoint);
6689 struct ui_out *uiout = current_uiout;
6690
6691 if (num_printed == 0)
6692 {
6693 if (args == NULL || *args == '\0')
6694 uiout->message ("No watchpoints.\n");
6695 else
6696 uiout->message ("No watchpoint matching '%s'.\n", args);
6697 }
6698 }
6699
6700 static void
6701 maintenance_info_breakpoints (const char *args, int from_tty)
6702 {
6703 breakpoint_1 (args, true, NULL);
6704
6705 default_collect_info ();
6706 }
6707
6708 static int
6709 breakpoint_has_pc (struct breakpoint *b,
6710 struct program_space *pspace,
6711 CORE_ADDR pc, struct obj_section *section)
6712 {
6713 for (bp_location *bl : b->locations ())
6714 {
6715 if (bl->pspace == pspace
6716 && bl->address == pc
6717 && (!overlay_debugging || bl->section == section))
6718 return 1;
6719 }
6720 return 0;
6721 }
6722
6723 /* Print a message describing any user-breakpoints set at PC. This
6724 concerns with logical breakpoints, so we match program spaces, not
6725 address spaces. */
6726
6727 static void
6728 describe_other_breakpoints (struct gdbarch *gdbarch,
6729 struct program_space *pspace, CORE_ADDR pc,
6730 struct obj_section *section, int thread)
6731 {
6732 int others = 0;
6733
6734 for (breakpoint *b : all_breakpoints ())
6735 others += (user_breakpoint_p (b)
6736 && breakpoint_has_pc (b, pspace, pc, section));
6737
6738 if (others > 0)
6739 {
6740 if (others == 1)
6741 printf_filtered (_("Note: breakpoint "));
6742 else /* if (others == ???) */
6743 printf_filtered (_("Note: breakpoints "));
6744 for (breakpoint *b : all_breakpoints ())
6745 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6746 {
6747 others--;
6748 printf_filtered ("%d", b->number);
6749 if (b->thread == -1 && thread != -1)
6750 printf_filtered (" (all threads)");
6751 else if (b->thread != -1)
6752 printf_filtered (" (thread %d)", b->thread);
6753 printf_filtered ("%s%s ",
6754 ((b->enable_state == bp_disabled
6755 || b->enable_state == bp_call_disabled)
6756 ? " (disabled)"
6757 : ""),
6758 (others > 1) ? ","
6759 : ((others == 1) ? " and" : ""));
6760 }
6761 current_uiout->message (_("also set at pc %ps.\n"),
6762 styled_string (address_style.style (),
6763 paddress (gdbarch, pc)));
6764 }
6765 }
6766 \f
6767
6768 /* Return true iff it is meaningful to use the address member of LOC.
6769 For some breakpoint types, the locations' address members are
6770 irrelevant and it makes no sense to attempt to compare them to
6771 other addresses (or use them for any other purpose either).
6772
6773 More specifically, software watchpoints and catchpoints that are
6774 not backed by breakpoints always have a zero valued location
6775 address and we don't want to mark breakpoints of any of these types
6776 to be a duplicate of an actual breakpoint location at address
6777 zero. */
6778
6779 static bool
6780 bl_address_is_meaningful (bp_location *loc)
6781 {
6782 return loc->loc_type != bp_loc_other;
6783 }
6784
6785 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6786 true if LOC1 and LOC2 represent the same watchpoint location. */
6787
6788 static int
6789 watchpoint_locations_match (struct bp_location *loc1,
6790 struct bp_location *loc2)
6791 {
6792 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6793 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6794
6795 /* Both of them must exist. */
6796 gdb_assert (w1 != NULL);
6797 gdb_assert (w2 != NULL);
6798
6799 /* If the target can evaluate the condition expression in hardware,
6800 then we we need to insert both watchpoints even if they are at
6801 the same place. Otherwise the watchpoint will only trigger when
6802 the condition of whichever watchpoint was inserted evaluates to
6803 true, not giving a chance for GDB to check the condition of the
6804 other watchpoint. */
6805 if ((w1->cond_exp
6806 && target_can_accel_watchpoint_condition (loc1->address,
6807 loc1->length,
6808 loc1->watchpoint_type,
6809 w1->cond_exp.get ()))
6810 || (w2->cond_exp
6811 && target_can_accel_watchpoint_condition (loc2->address,
6812 loc2->length,
6813 loc2->watchpoint_type,
6814 w2->cond_exp.get ())))
6815 return 0;
6816
6817 /* Note that this checks the owner's type, not the location's. In
6818 case the target does not support read watchpoints, but does
6819 support access watchpoints, we'll have bp_read_watchpoint
6820 watchpoints with hw_access locations. Those should be considered
6821 duplicates of hw_read locations. The hw_read locations will
6822 become hw_access locations later. */
6823 return (loc1->owner->type == loc2->owner->type
6824 && loc1->pspace->aspace == loc2->pspace->aspace
6825 && loc1->address == loc2->address
6826 && loc1->length == loc2->length);
6827 }
6828
6829 /* See breakpoint.h. */
6830
6831 int
6832 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6833 const address_space *aspace2, CORE_ADDR addr2)
6834 {
6835 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6836 || aspace1 == aspace2)
6837 && addr1 == addr2);
6838 }
6839
6840 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6841 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6842 matches ASPACE2. On targets that have global breakpoints, the address
6843 space doesn't really matter. */
6844
6845 static int
6846 breakpoint_address_match_range (const address_space *aspace1,
6847 CORE_ADDR addr1,
6848 int len1, const address_space *aspace2,
6849 CORE_ADDR addr2)
6850 {
6851 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6852 || aspace1 == aspace2)
6853 && addr2 >= addr1 && addr2 < addr1 + len1);
6854 }
6855
6856 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6857 a ranged breakpoint. In most targets, a match happens only if ASPACE
6858 matches the breakpoint's address space. On targets that have global
6859 breakpoints, the address space doesn't really matter. */
6860
6861 static int
6862 breakpoint_location_address_match (struct bp_location *bl,
6863 const address_space *aspace,
6864 CORE_ADDR addr)
6865 {
6866 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6867 aspace, addr)
6868 || (bl->length
6869 && breakpoint_address_match_range (bl->pspace->aspace,
6870 bl->address, bl->length,
6871 aspace, addr)));
6872 }
6873
6874 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6875 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6876 match happens only if ASPACE matches the breakpoint's address
6877 space. On targets that have global breakpoints, the address space
6878 doesn't really matter. */
6879
6880 static int
6881 breakpoint_location_address_range_overlap (struct bp_location *bl,
6882 const address_space *aspace,
6883 CORE_ADDR addr, int len)
6884 {
6885 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6886 || bl->pspace->aspace == aspace)
6887 {
6888 int bl_len = bl->length != 0 ? bl->length : 1;
6889
6890 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6891 return 1;
6892 }
6893 return 0;
6894 }
6895
6896 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6897 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6898 true, otherwise returns false. */
6899
6900 static int
6901 tracepoint_locations_match (struct bp_location *loc1,
6902 struct bp_location *loc2)
6903 {
6904 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6905 /* Since tracepoint locations are never duplicated with others', tracepoint
6906 locations at the same address of different tracepoints are regarded as
6907 different locations. */
6908 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6909 else
6910 return 0;
6911 }
6912
6913 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6914 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
6915 the same location. If SW_HW_BPS_MATCH is true, then software
6916 breakpoint locations and hardware breakpoint locations match,
6917 otherwise they don't. */
6918
6919 static int
6920 breakpoint_locations_match (struct bp_location *loc1,
6921 struct bp_location *loc2,
6922 bool sw_hw_bps_match)
6923 {
6924 int hw_point1, hw_point2;
6925
6926 /* Both of them must not be in moribund_locations. */
6927 gdb_assert (loc1->owner != NULL);
6928 gdb_assert (loc2->owner != NULL);
6929
6930 hw_point1 = is_hardware_watchpoint (loc1->owner);
6931 hw_point2 = is_hardware_watchpoint (loc2->owner);
6932
6933 if (hw_point1 != hw_point2)
6934 return 0;
6935 else if (hw_point1)
6936 return watchpoint_locations_match (loc1, loc2);
6937 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6938 return tracepoint_locations_match (loc1, loc2);
6939 else
6940 /* We compare bp_location.length in order to cover ranged
6941 breakpoints. Keep this in sync with
6942 bp_location_is_less_than. */
6943 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6944 loc2->pspace->aspace, loc2->address)
6945 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
6946 && loc1->length == loc2->length);
6947 }
6948
6949 static void
6950 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6951 int bnum, int have_bnum)
6952 {
6953 /* The longest string possibly returned by hex_string_custom
6954 is 50 chars. These must be at least that big for safety. */
6955 char astr1[64];
6956 char astr2[64];
6957
6958 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6959 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
6960 if (have_bnum)
6961 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
6962 bnum, astr1, astr2);
6963 else
6964 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
6965 }
6966
6967 /* Adjust a breakpoint's address to account for architectural
6968 constraints on breakpoint placement. Return the adjusted address.
6969 Note: Very few targets require this kind of adjustment. For most
6970 targets, this function is simply the identity function. */
6971
6972 static CORE_ADDR
6973 adjust_breakpoint_address (struct gdbarch *gdbarch,
6974 CORE_ADDR bpaddr, enum bptype bptype)
6975 {
6976 if (bptype == bp_watchpoint
6977 || bptype == bp_hardware_watchpoint
6978 || bptype == bp_read_watchpoint
6979 || bptype == bp_access_watchpoint
6980 || bptype == bp_catchpoint)
6981 {
6982 /* Watchpoints and the various bp_catch_* eventpoints should not
6983 have their addresses modified. */
6984 return bpaddr;
6985 }
6986 else if (bptype == bp_single_step)
6987 {
6988 /* Single-step breakpoints should not have their addresses
6989 modified. If there's any architectural constrain that
6990 applies to this address, then it should have already been
6991 taken into account when the breakpoint was created in the
6992 first place. If we didn't do this, stepping through e.g.,
6993 Thumb-2 IT blocks would break. */
6994 return bpaddr;
6995 }
6996 else
6997 {
6998 CORE_ADDR adjusted_bpaddr = bpaddr;
6999
7000 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7001 {
7002 /* Some targets have architectural constraints on the placement
7003 of breakpoint instructions. Obtain the adjusted address. */
7004 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7005 }
7006
7007 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
7008
7009 /* An adjusted breakpoint address can significantly alter
7010 a user's expectations. Print a warning if an adjustment
7011 is required. */
7012 if (adjusted_bpaddr != bpaddr)
7013 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7014
7015 return adjusted_bpaddr;
7016 }
7017 }
7018
7019 static bp_loc_type
7020 bp_location_from_bp_type (bptype type)
7021 {
7022 switch (type)
7023 {
7024 case bp_breakpoint:
7025 case bp_single_step:
7026 case bp_until:
7027 case bp_finish:
7028 case bp_longjmp:
7029 case bp_longjmp_resume:
7030 case bp_longjmp_call_dummy:
7031 case bp_exception:
7032 case bp_exception_resume:
7033 case bp_step_resume:
7034 case bp_hp_step_resume:
7035 case bp_watchpoint_scope:
7036 case bp_call_dummy:
7037 case bp_std_terminate:
7038 case bp_shlib_event:
7039 case bp_thread_event:
7040 case bp_overlay_event:
7041 case bp_jit_event:
7042 case bp_longjmp_master:
7043 case bp_std_terminate_master:
7044 case bp_exception_master:
7045 case bp_gnu_ifunc_resolver:
7046 case bp_gnu_ifunc_resolver_return:
7047 case bp_dprintf:
7048 return bp_loc_software_breakpoint;
7049 case bp_hardware_breakpoint:
7050 return bp_loc_hardware_breakpoint;
7051 case bp_hardware_watchpoint:
7052 case bp_read_watchpoint:
7053 case bp_access_watchpoint:
7054 return bp_loc_hardware_watchpoint;
7055 case bp_watchpoint:
7056 case bp_catchpoint:
7057 case bp_tracepoint:
7058 case bp_fast_tracepoint:
7059 case bp_static_tracepoint:
7060 return bp_loc_other;
7061 default:
7062 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7063 }
7064 }
7065
7066 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7067 {
7068 this->owner = owner;
7069 this->cond_bytecode = NULL;
7070 this->shlib_disabled = 0;
7071 this->enabled = 1;
7072 this->disabled_by_cond = false;
7073
7074 this->loc_type = type;
7075
7076 if (this->loc_type == bp_loc_software_breakpoint
7077 || this->loc_type == bp_loc_hardware_breakpoint)
7078 mark_breakpoint_location_modified (this);
7079
7080 incref ();
7081 }
7082
7083 bp_location::bp_location (breakpoint *owner)
7084 : bp_location::bp_location (owner,
7085 bp_location_from_bp_type (owner->type))
7086 {
7087 }
7088
7089 /* Allocate a struct bp_location. */
7090
7091 static struct bp_location *
7092 allocate_bp_location (struct breakpoint *bpt)
7093 {
7094 return bpt->ops->allocate_location (bpt);
7095 }
7096
7097 /* Decrement reference count. If the reference count reaches 0,
7098 destroy the bp_location. Sets *BLP to NULL. */
7099
7100 static void
7101 decref_bp_location (struct bp_location **blp)
7102 {
7103 bp_location_ref_policy::decref (*blp);
7104 *blp = NULL;
7105 }
7106
7107 /* Add breakpoint B at the end of the global breakpoint chain. */
7108
7109 static breakpoint *
7110 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7111 {
7112 struct breakpoint *b1;
7113 struct breakpoint *result = b.get ();
7114
7115 /* Add this breakpoint to the end of the chain so that a list of
7116 breakpoints will come out in order of increasing numbers. */
7117
7118 b1 = breakpoint_chain;
7119 if (b1 == 0)
7120 breakpoint_chain = b.release ();
7121 else
7122 {
7123 while (b1->next)
7124 b1 = b1->next;
7125 b1->next = b.release ();
7126 }
7127
7128 return result;
7129 }
7130
7131 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7132
7133 static void
7134 init_raw_breakpoint_without_location (struct breakpoint *b,
7135 struct gdbarch *gdbarch,
7136 enum bptype bptype,
7137 const struct breakpoint_ops *ops)
7138 {
7139 gdb_assert (ops != NULL);
7140
7141 b->ops = ops;
7142 b->type = bptype;
7143 b->gdbarch = gdbarch;
7144 b->language = current_language->la_language;
7145 b->input_radix = input_radix;
7146 b->related_breakpoint = b;
7147 }
7148
7149 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7150 that has type BPTYPE and has no locations as yet. */
7151
7152 static struct breakpoint *
7153 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7154 enum bptype bptype,
7155 const struct breakpoint_ops *ops)
7156 {
7157 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7158
7159 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7160 return add_to_breakpoint_chain (std::move (b));
7161 }
7162
7163 /* Initialize loc->function_name. */
7164
7165 static void
7166 set_breakpoint_location_function (struct bp_location *loc)
7167 {
7168 gdb_assert (loc->owner != NULL);
7169
7170 if (loc->owner->type == bp_breakpoint
7171 || loc->owner->type == bp_hardware_breakpoint
7172 || is_tracepoint (loc->owner))
7173 {
7174 const char *function_name;
7175
7176 if (loc->msymbol != NULL
7177 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
7178 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
7179 {
7180 struct breakpoint *b = loc->owner;
7181
7182 function_name = loc->msymbol->linkage_name ();
7183
7184 if (b->type == bp_breakpoint && b->loc == loc
7185 && loc->next == NULL && b->related_breakpoint == b)
7186 {
7187 /* Create only the whole new breakpoint of this type but do not
7188 mess more complicated breakpoints with multiple locations. */
7189 b->type = bp_gnu_ifunc_resolver;
7190 /* Remember the resolver's address for use by the return
7191 breakpoint. */
7192 loc->related_address = loc->address;
7193 }
7194 }
7195 else
7196 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7197
7198 if (function_name)
7199 loc->function_name = xstrdup (function_name);
7200 }
7201 }
7202
7203 /* Attempt to determine architecture of location identified by SAL. */
7204 struct gdbarch *
7205 get_sal_arch (struct symtab_and_line sal)
7206 {
7207 if (sal.section)
7208 return sal.section->objfile->arch ();
7209 if (sal.symtab)
7210 return SYMTAB_OBJFILE (sal.symtab)->arch ();
7211
7212 return NULL;
7213 }
7214
7215 /* Low level routine for partially initializing a breakpoint of type
7216 BPTYPE. The newly created breakpoint's address, section, source
7217 file name, and line number are provided by SAL.
7218
7219 It is expected that the caller will complete the initialization of
7220 the newly created breakpoint struct as well as output any status
7221 information regarding the creation of a new breakpoint. */
7222
7223 static void
7224 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7225 struct symtab_and_line sal, enum bptype bptype,
7226 const struct breakpoint_ops *ops)
7227 {
7228 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7229
7230 add_location_to_breakpoint (b, &sal);
7231
7232 if (bptype != bp_catchpoint)
7233 gdb_assert (sal.pspace != NULL);
7234
7235 /* Store the program space that was used to set the breakpoint,
7236 except for ordinary breakpoints, which are independent of the
7237 program space. */
7238 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7239 b->pspace = sal.pspace;
7240 }
7241
7242 /* set_raw_breakpoint is a low level routine for allocating and
7243 partially initializing a breakpoint of type BPTYPE. The newly
7244 created breakpoint's address, section, source file name, and line
7245 number are provided by SAL. The newly created and partially
7246 initialized breakpoint is added to the breakpoint chain and
7247 is also returned as the value of this function.
7248
7249 It is expected that the caller will complete the initialization of
7250 the newly created breakpoint struct as well as output any status
7251 information regarding the creation of a new breakpoint. In
7252 particular, set_raw_breakpoint does NOT set the breakpoint
7253 number! Care should be taken to not allow an error to occur
7254 prior to completing the initialization of the breakpoint. If this
7255 should happen, a bogus breakpoint will be left on the chain. */
7256
7257 struct breakpoint *
7258 set_raw_breakpoint (struct gdbarch *gdbarch,
7259 struct symtab_and_line sal, enum bptype bptype,
7260 const struct breakpoint_ops *ops)
7261 {
7262 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7263
7264 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7265 return add_to_breakpoint_chain (std::move (b));
7266 }
7267
7268 /* Call this routine when stepping and nexting to enable a breakpoint
7269 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7270 initiated the operation. */
7271
7272 void
7273 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7274 {
7275 int thread = tp->global_num;
7276
7277 /* To avoid having to rescan all objfile symbols at every step,
7278 we maintain a list of continually-inserted but always disabled
7279 longjmp "master" breakpoints. Here, we simply create momentary
7280 clones of those and enable them for the requested thread. */
7281 for (breakpoint *b : all_breakpoints_safe ())
7282 if (b->pspace == current_program_space
7283 && (b->type == bp_longjmp_master
7284 || b->type == bp_exception_master))
7285 {
7286 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7287 struct breakpoint *clone;
7288
7289 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7290 after their removal. */
7291 clone = momentary_breakpoint_from_master (b, type,
7292 &momentary_breakpoint_ops, 1);
7293 clone->thread = thread;
7294 }
7295
7296 tp->initiating_frame = frame;
7297 }
7298
7299 /* Delete all longjmp breakpoints from THREAD. */
7300 void
7301 delete_longjmp_breakpoint (int thread)
7302 {
7303 for (breakpoint *b : all_breakpoints_safe ())
7304 if (b->type == bp_longjmp || b->type == bp_exception)
7305 {
7306 if (b->thread == thread)
7307 delete_breakpoint (b);
7308 }
7309 }
7310
7311 void
7312 delete_longjmp_breakpoint_at_next_stop (int thread)
7313 {
7314 for (breakpoint *b : all_breakpoints_safe ())
7315 if (b->type == bp_longjmp || b->type == bp_exception)
7316 {
7317 if (b->thread == thread)
7318 b->disposition = disp_del_at_next_stop;
7319 }
7320 }
7321
7322 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7323 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7324 pointer to any of them. Return NULL if this system cannot place longjmp
7325 breakpoints. */
7326
7327 struct breakpoint *
7328 set_longjmp_breakpoint_for_call_dummy (void)
7329 {
7330 breakpoint *retval = nullptr;
7331
7332 for (breakpoint *b : all_breakpoints ())
7333 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7334 {
7335 struct breakpoint *new_b;
7336
7337 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7338 &momentary_breakpoint_ops,
7339 1);
7340 new_b->thread = inferior_thread ()->global_num;
7341
7342 /* Link NEW_B into the chain of RETVAL breakpoints. */
7343
7344 gdb_assert (new_b->related_breakpoint == new_b);
7345 if (retval == NULL)
7346 retval = new_b;
7347 new_b->related_breakpoint = retval;
7348 while (retval->related_breakpoint != new_b->related_breakpoint)
7349 retval = retval->related_breakpoint;
7350 retval->related_breakpoint = new_b;
7351 }
7352
7353 return retval;
7354 }
7355
7356 /* Verify all existing dummy frames and their associated breakpoints for
7357 TP. Remove those which can no longer be found in the current frame
7358 stack.
7359
7360 You should call this function only at places where it is safe to currently
7361 unwind the whole stack. Failed stack unwind would discard live dummy
7362 frames. */
7363
7364 void
7365 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7366 {
7367 struct breakpoint *b, *b_tmp;
7368
7369 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7370 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7371 {
7372 struct breakpoint *dummy_b = b->related_breakpoint;
7373
7374 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7375 dummy_b = dummy_b->related_breakpoint;
7376 if (dummy_b->type != bp_call_dummy
7377 || frame_find_by_id (dummy_b->frame_id) != NULL)
7378 continue;
7379
7380 dummy_frame_discard (dummy_b->frame_id, tp);
7381
7382 while (b->related_breakpoint != b)
7383 {
7384 if (b_tmp == b->related_breakpoint)
7385 b_tmp = b->related_breakpoint->next;
7386 delete_breakpoint (b->related_breakpoint);
7387 }
7388 delete_breakpoint (b);
7389 }
7390 }
7391
7392 void
7393 enable_overlay_breakpoints (void)
7394 {
7395 for (breakpoint *b : all_breakpoints ())
7396 if (b->type == bp_overlay_event)
7397 {
7398 b->enable_state = bp_enabled;
7399 update_global_location_list (UGLL_MAY_INSERT);
7400 overlay_events_enabled = 1;
7401 }
7402 }
7403
7404 void
7405 disable_overlay_breakpoints (void)
7406 {
7407 for (breakpoint *b : all_breakpoints ())
7408 if (b->type == bp_overlay_event)
7409 {
7410 b->enable_state = bp_disabled;
7411 update_global_location_list (UGLL_DONT_INSERT);
7412 overlay_events_enabled = 0;
7413 }
7414 }
7415
7416 /* Set an active std::terminate breakpoint for each std::terminate
7417 master breakpoint. */
7418 void
7419 set_std_terminate_breakpoint (void)
7420 {
7421 for (breakpoint *b : all_breakpoints_safe ())
7422 if (b->pspace == current_program_space
7423 && b->type == bp_std_terminate_master)
7424 {
7425 momentary_breakpoint_from_master (b, bp_std_terminate,
7426 &momentary_breakpoint_ops, 1);
7427 }
7428 }
7429
7430 /* Delete all the std::terminate breakpoints. */
7431 void
7432 delete_std_terminate_breakpoint (void)
7433 {
7434 for (breakpoint *b : all_breakpoints_safe ())
7435 if (b->type == bp_std_terminate)
7436 delete_breakpoint (b);
7437 }
7438
7439 struct breakpoint *
7440 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7441 {
7442 struct breakpoint *b;
7443
7444 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7445 &internal_breakpoint_ops);
7446
7447 b->enable_state = bp_enabled;
7448 /* location has to be used or breakpoint_re_set will delete me. */
7449 b->location = new_address_location (b->loc->address, NULL, 0);
7450
7451 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7452
7453 return b;
7454 }
7455
7456 struct lang_and_radix
7457 {
7458 enum language lang;
7459 int radix;
7460 };
7461
7462 /* Create a breakpoint for JIT code registration and unregistration. */
7463
7464 struct breakpoint *
7465 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7466 {
7467 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7468 &internal_breakpoint_ops);
7469 }
7470
7471 /* Remove JIT code registration and unregistration breakpoint(s). */
7472
7473 void
7474 remove_jit_event_breakpoints (void)
7475 {
7476 for (breakpoint *b : all_breakpoints_safe ())
7477 if (b->type == bp_jit_event
7478 && b->loc->pspace == current_program_space)
7479 delete_breakpoint (b);
7480 }
7481
7482 void
7483 remove_solib_event_breakpoints (void)
7484 {
7485 for (breakpoint *b : all_breakpoints_safe ())
7486 if (b->type == bp_shlib_event
7487 && b->loc->pspace == current_program_space)
7488 delete_breakpoint (b);
7489 }
7490
7491 /* See breakpoint.h. */
7492
7493 void
7494 remove_solib_event_breakpoints_at_next_stop (void)
7495 {
7496 for (breakpoint *b : all_breakpoints_safe ())
7497 if (b->type == bp_shlib_event
7498 && b->loc->pspace == current_program_space)
7499 b->disposition = disp_del_at_next_stop;
7500 }
7501
7502 /* Helper for create_solib_event_breakpoint /
7503 create_and_insert_solib_event_breakpoint. Allows specifying which
7504 INSERT_MODE to pass through to update_global_location_list. */
7505
7506 static struct breakpoint *
7507 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7508 enum ugll_insert_mode insert_mode)
7509 {
7510 struct breakpoint *b;
7511
7512 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7513 &internal_breakpoint_ops);
7514 update_global_location_list_nothrow (insert_mode);
7515 return b;
7516 }
7517
7518 struct breakpoint *
7519 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7520 {
7521 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7522 }
7523
7524 /* See breakpoint.h. */
7525
7526 struct breakpoint *
7527 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7528 {
7529 struct breakpoint *b;
7530
7531 /* Explicitly tell update_global_location_list to insert
7532 locations. */
7533 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7534 if (!b->loc->inserted)
7535 {
7536 delete_breakpoint (b);
7537 return NULL;
7538 }
7539 return b;
7540 }
7541
7542 /* Disable any breakpoints that are on code in shared libraries. Only
7543 apply to enabled breakpoints, disabled ones can just stay disabled. */
7544
7545 void
7546 disable_breakpoints_in_shlibs (void)
7547 {
7548 for (bp_location *loc : all_bp_locations ())
7549 {
7550 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7551 struct breakpoint *b = loc->owner;
7552
7553 /* We apply the check to all breakpoints, including disabled for
7554 those with loc->duplicate set. This is so that when breakpoint
7555 becomes enabled, or the duplicate is removed, gdb will try to
7556 insert all breakpoints. If we don't set shlib_disabled here,
7557 we'll try to insert those breakpoints and fail. */
7558 if (((b->type == bp_breakpoint)
7559 || (b->type == bp_jit_event)
7560 || (b->type == bp_hardware_breakpoint)
7561 || (is_tracepoint (b)))
7562 && loc->pspace == current_program_space
7563 && !loc->shlib_disabled
7564 && solib_name_from_address (loc->pspace, loc->address)
7565 )
7566 {
7567 loc->shlib_disabled = 1;
7568 }
7569 }
7570 }
7571
7572 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7573 notification of unloaded_shlib. Only apply to enabled breakpoints,
7574 disabled ones can just stay disabled. */
7575
7576 static void
7577 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7578 {
7579 int disabled_shlib_breaks = 0;
7580
7581 for (bp_location *loc : all_bp_locations ())
7582 {
7583 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7584 struct breakpoint *b = loc->owner;
7585
7586 if (solib->pspace == loc->pspace
7587 && !loc->shlib_disabled
7588 && (((b->type == bp_breakpoint
7589 || b->type == bp_jit_event
7590 || b->type == bp_hardware_breakpoint)
7591 && (loc->loc_type == bp_loc_hardware_breakpoint
7592 || loc->loc_type == bp_loc_software_breakpoint))
7593 || is_tracepoint (b))
7594 && solib_contains_address_p (solib, loc->address))
7595 {
7596 loc->shlib_disabled = 1;
7597 /* At this point, we cannot rely on remove_breakpoint
7598 succeeding so we must mark the breakpoint as not inserted
7599 to prevent future errors occurring in remove_breakpoints. */
7600 loc->inserted = 0;
7601
7602 /* This may cause duplicate notifications for the same breakpoint. */
7603 gdb::observers::breakpoint_modified.notify (b);
7604
7605 if (!disabled_shlib_breaks)
7606 {
7607 target_terminal::ours_for_output ();
7608 warning (_("Temporarily disabling breakpoints "
7609 "for unloaded shared library \"%s\""),
7610 solib->so_name);
7611 }
7612 disabled_shlib_breaks = 1;
7613 }
7614 }
7615 }
7616
7617 /* Disable any breakpoints and tracepoints in OBJFILE upon
7618 notification of free_objfile. Only apply to enabled breakpoints,
7619 disabled ones can just stay disabled. */
7620
7621 static void
7622 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7623 {
7624 if (objfile == NULL)
7625 return;
7626
7627 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7628 managed by the user with add-symbol-file/remove-symbol-file.
7629 Similarly to how breakpoints in shared libraries are handled in
7630 response to "nosharedlibrary", mark breakpoints in such modules
7631 shlib_disabled so they end up uninserted on the next global
7632 location list update. Shared libraries not loaded by the user
7633 aren't handled here -- they're already handled in
7634 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7635 solib_unloaded observer. We skip objfiles that are not
7636 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7637 main objfile). */
7638 if ((objfile->flags & OBJF_SHARED) == 0
7639 || (objfile->flags & OBJF_USERLOADED) == 0)
7640 return;
7641
7642 for (breakpoint *b : all_breakpoints ())
7643 {
7644 int bp_modified = 0;
7645
7646 if (!is_breakpoint (b) && !is_tracepoint (b))
7647 continue;
7648
7649 for (bp_location *loc : b->locations ())
7650 {
7651 CORE_ADDR loc_addr = loc->address;
7652
7653 if (loc->loc_type != bp_loc_hardware_breakpoint
7654 && loc->loc_type != bp_loc_software_breakpoint)
7655 continue;
7656
7657 if (loc->shlib_disabled != 0)
7658 continue;
7659
7660 if (objfile->pspace != loc->pspace)
7661 continue;
7662
7663 if (loc->loc_type != bp_loc_hardware_breakpoint
7664 && loc->loc_type != bp_loc_software_breakpoint)
7665 continue;
7666
7667 if (is_addr_in_objfile (loc_addr, objfile))
7668 {
7669 loc->shlib_disabled = 1;
7670 /* At this point, we don't know whether the object was
7671 unmapped from the inferior or not, so leave the
7672 inserted flag alone. We'll handle failure to
7673 uninsert quietly, in case the object was indeed
7674 unmapped. */
7675
7676 mark_breakpoint_location_modified (loc);
7677
7678 bp_modified = 1;
7679 }
7680 }
7681
7682 if (bp_modified)
7683 gdb::observers::breakpoint_modified.notify (b);
7684 }
7685 }
7686
7687 /* FORK & VFORK catchpoints. */
7688
7689 /* An instance of this type is used to represent a fork or vfork
7690 catchpoint. A breakpoint is really of this type iff its ops pointer points
7691 to CATCH_FORK_BREAKPOINT_OPS. */
7692
7693 struct fork_catchpoint : public breakpoint
7694 {
7695 /* Process id of a child process whose forking triggered this
7696 catchpoint. This field is only valid immediately after this
7697 catchpoint has triggered. */
7698 ptid_t forked_inferior_pid;
7699 };
7700
7701 /* Implement the "insert" breakpoint_ops method for fork
7702 catchpoints. */
7703
7704 static int
7705 insert_catch_fork (struct bp_location *bl)
7706 {
7707 return target_insert_fork_catchpoint (inferior_ptid.pid ());
7708 }
7709
7710 /* Implement the "remove" breakpoint_ops method for fork
7711 catchpoints. */
7712
7713 static int
7714 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
7715 {
7716 return target_remove_fork_catchpoint (inferior_ptid.pid ());
7717 }
7718
7719 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7720 catchpoints. */
7721
7722 static int
7723 breakpoint_hit_catch_fork (const struct bp_location *bl,
7724 const address_space *aspace, CORE_ADDR bp_addr,
7725 const struct target_waitstatus *ws)
7726 {
7727 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7728
7729 if (ws->kind != TARGET_WAITKIND_FORKED)
7730 return 0;
7731
7732 c->forked_inferior_pid = ws->value.related_pid;
7733 return 1;
7734 }
7735
7736 /* Implement the "print_it" breakpoint_ops method for fork
7737 catchpoints. */
7738
7739 static enum print_stop_action
7740 print_it_catch_fork (bpstat bs)
7741 {
7742 struct ui_out *uiout = current_uiout;
7743 struct breakpoint *b = bs->breakpoint_at;
7744 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7745
7746 annotate_catchpoint (b->number);
7747 maybe_print_thread_hit_breakpoint (uiout);
7748 if (b->disposition == disp_del)
7749 uiout->text ("Temporary catchpoint ");
7750 else
7751 uiout->text ("Catchpoint ");
7752 if (uiout->is_mi_like_p ())
7753 {
7754 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7755 uiout->field_string ("disp", bpdisp_text (b->disposition));
7756 }
7757 uiout->field_signed ("bkptno", b->number);
7758 uiout->text (" (forked process ");
7759 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7760 uiout->text ("), ");
7761 return PRINT_SRC_AND_LOC;
7762 }
7763
7764 /* Implement the "print_one" breakpoint_ops method for fork
7765 catchpoints. */
7766
7767 static void
7768 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7769 {
7770 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7771 struct value_print_options opts;
7772 struct ui_out *uiout = current_uiout;
7773
7774 get_user_print_options (&opts);
7775
7776 /* Field 4, the address, is omitted (which makes the columns not
7777 line up too nicely with the headers, but the effect is relatively
7778 readable). */
7779 if (opts.addressprint)
7780 uiout->field_skip ("addr");
7781 annotate_field (5);
7782 uiout->text ("fork");
7783 if (c->forked_inferior_pid != null_ptid)
7784 {
7785 uiout->text (", process ");
7786 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7787 uiout->spaces (1);
7788 }
7789
7790 if (uiout->is_mi_like_p ())
7791 uiout->field_string ("catch-type", "fork");
7792 }
7793
7794 /* Implement the "print_mention" breakpoint_ops method for fork
7795 catchpoints. */
7796
7797 static void
7798 print_mention_catch_fork (struct breakpoint *b)
7799 {
7800 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7801 }
7802
7803 /* Implement the "print_recreate" breakpoint_ops method for fork
7804 catchpoints. */
7805
7806 static void
7807 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7808 {
7809 fprintf_unfiltered (fp, "catch fork");
7810 print_recreate_thread (b, fp);
7811 }
7812
7813 /* The breakpoint_ops structure to be used in fork catchpoints. */
7814
7815 static struct breakpoint_ops catch_fork_breakpoint_ops;
7816
7817 /* Implement the "insert" breakpoint_ops method for vfork
7818 catchpoints. */
7819
7820 static int
7821 insert_catch_vfork (struct bp_location *bl)
7822 {
7823 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
7824 }
7825
7826 /* Implement the "remove" breakpoint_ops method for vfork
7827 catchpoints. */
7828
7829 static int
7830 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
7831 {
7832 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
7833 }
7834
7835 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7836 catchpoints. */
7837
7838 static int
7839 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7840 const address_space *aspace, CORE_ADDR bp_addr,
7841 const struct target_waitstatus *ws)
7842 {
7843 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7844
7845 if (ws->kind != TARGET_WAITKIND_VFORKED)
7846 return 0;
7847
7848 c->forked_inferior_pid = ws->value.related_pid;
7849 return 1;
7850 }
7851
7852 /* Implement the "print_it" breakpoint_ops method for vfork
7853 catchpoints. */
7854
7855 static enum print_stop_action
7856 print_it_catch_vfork (bpstat bs)
7857 {
7858 struct ui_out *uiout = current_uiout;
7859 struct breakpoint *b = bs->breakpoint_at;
7860 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7861
7862 annotate_catchpoint (b->number);
7863 maybe_print_thread_hit_breakpoint (uiout);
7864 if (b->disposition == disp_del)
7865 uiout->text ("Temporary catchpoint ");
7866 else
7867 uiout->text ("Catchpoint ");
7868 if (uiout->is_mi_like_p ())
7869 {
7870 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7871 uiout->field_string ("disp", bpdisp_text (b->disposition));
7872 }
7873 uiout->field_signed ("bkptno", b->number);
7874 uiout->text (" (vforked process ");
7875 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7876 uiout->text ("), ");
7877 return PRINT_SRC_AND_LOC;
7878 }
7879
7880 /* Implement the "print_one" breakpoint_ops method for vfork
7881 catchpoints. */
7882
7883 static void
7884 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7885 {
7886 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7887 struct value_print_options opts;
7888 struct ui_out *uiout = current_uiout;
7889
7890 get_user_print_options (&opts);
7891 /* Field 4, the address, is omitted (which makes the columns not
7892 line up too nicely with the headers, but the effect is relatively
7893 readable). */
7894 if (opts.addressprint)
7895 uiout->field_skip ("addr");
7896 annotate_field (5);
7897 uiout->text ("vfork");
7898 if (c->forked_inferior_pid != null_ptid)
7899 {
7900 uiout->text (", process ");
7901 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7902 uiout->spaces (1);
7903 }
7904
7905 if (uiout->is_mi_like_p ())
7906 uiout->field_string ("catch-type", "vfork");
7907 }
7908
7909 /* Implement the "print_mention" breakpoint_ops method for vfork
7910 catchpoints. */
7911
7912 static void
7913 print_mention_catch_vfork (struct breakpoint *b)
7914 {
7915 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7916 }
7917
7918 /* Implement the "print_recreate" breakpoint_ops method for vfork
7919 catchpoints. */
7920
7921 static void
7922 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7923 {
7924 fprintf_unfiltered (fp, "catch vfork");
7925 print_recreate_thread (b, fp);
7926 }
7927
7928 /* The breakpoint_ops structure to be used in vfork catchpoints. */
7929
7930 static struct breakpoint_ops catch_vfork_breakpoint_ops;
7931
7932 /* An instance of this type is used to represent an solib catchpoint.
7933 A breakpoint is really of this type iff its ops pointer points to
7934 CATCH_SOLIB_BREAKPOINT_OPS. */
7935
7936 struct solib_catchpoint : public breakpoint
7937 {
7938 ~solib_catchpoint () override;
7939
7940 /* True for "catch load", false for "catch unload". */
7941 bool is_load;
7942
7943 /* Regular expression to match, if any. COMPILED is only valid when
7944 REGEX is non-NULL. */
7945 char *regex;
7946 std::unique_ptr<compiled_regex> compiled;
7947 };
7948
7949 solib_catchpoint::~solib_catchpoint ()
7950 {
7951 xfree (this->regex);
7952 }
7953
7954 static int
7955 insert_catch_solib (struct bp_location *ignore)
7956 {
7957 return 0;
7958 }
7959
7960 static int
7961 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
7962 {
7963 return 0;
7964 }
7965
7966 static int
7967 breakpoint_hit_catch_solib (const struct bp_location *bl,
7968 const address_space *aspace,
7969 CORE_ADDR bp_addr,
7970 const struct target_waitstatus *ws)
7971 {
7972 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7973
7974 if (ws->kind == TARGET_WAITKIND_LOADED)
7975 return 1;
7976
7977 for (breakpoint *other : all_breakpoints ())
7978 {
7979 if (other == bl->owner)
7980 continue;
7981
7982 if (other->type != bp_shlib_event)
7983 continue;
7984
7985 if (self->pspace != NULL && other->pspace != self->pspace)
7986 continue;
7987
7988 for (bp_location *other_bl : other->locations ())
7989 {
7990 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7991 return 1;
7992 }
7993 }
7994
7995 return 0;
7996 }
7997
7998 static void
7999 check_status_catch_solib (struct bpstats *bs)
8000 {
8001 struct solib_catchpoint *self
8002 = (struct solib_catchpoint *) bs->breakpoint_at;
8003
8004 if (self->is_load)
8005 {
8006 for (so_list *iter : current_program_space->added_solibs)
8007 {
8008 if (!self->regex
8009 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
8010 return;
8011 }
8012 }
8013 else
8014 {
8015 for (const std::string &iter : current_program_space->deleted_solibs)
8016 {
8017 if (!self->regex
8018 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
8019 return;
8020 }
8021 }
8022
8023 bs->stop = 0;
8024 bs->print_it = print_it_noop;
8025 }
8026
8027 static enum print_stop_action
8028 print_it_catch_solib (bpstat bs)
8029 {
8030 struct breakpoint *b = bs->breakpoint_at;
8031 struct ui_out *uiout = current_uiout;
8032
8033 annotate_catchpoint (b->number);
8034 maybe_print_thread_hit_breakpoint (uiout);
8035 if (b->disposition == disp_del)
8036 uiout->text ("Temporary catchpoint ");
8037 else
8038 uiout->text ("Catchpoint ");
8039 uiout->field_signed ("bkptno", b->number);
8040 uiout->text ("\n");
8041 if (uiout->is_mi_like_p ())
8042 uiout->field_string ("disp", bpdisp_text (b->disposition));
8043 print_solib_event (1);
8044 return PRINT_SRC_AND_LOC;
8045 }
8046
8047 static void
8048 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8049 {
8050 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8051 struct value_print_options opts;
8052 struct ui_out *uiout = current_uiout;
8053
8054 get_user_print_options (&opts);
8055 /* Field 4, the address, is omitted (which makes the columns not
8056 line up too nicely with the headers, but the effect is relatively
8057 readable). */
8058 if (opts.addressprint)
8059 {
8060 annotate_field (4);
8061 uiout->field_skip ("addr");
8062 }
8063
8064 std::string msg;
8065 annotate_field (5);
8066 if (self->is_load)
8067 {
8068 if (self->regex)
8069 msg = string_printf (_("load of library matching %s"), self->regex);
8070 else
8071 msg = _("load of library");
8072 }
8073 else
8074 {
8075 if (self->regex)
8076 msg = string_printf (_("unload of library matching %s"), self->regex);
8077 else
8078 msg = _("unload of library");
8079 }
8080 uiout->field_string ("what", msg);
8081
8082 if (uiout->is_mi_like_p ())
8083 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8084 }
8085
8086 static void
8087 print_mention_catch_solib (struct breakpoint *b)
8088 {
8089 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8090
8091 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8092 self->is_load ? "load" : "unload");
8093 }
8094
8095 static void
8096 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8097 {
8098 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8099
8100 fprintf_unfiltered (fp, "%s %s",
8101 b->disposition == disp_del ? "tcatch" : "catch",
8102 self->is_load ? "load" : "unload");
8103 if (self->regex)
8104 fprintf_unfiltered (fp, " %s", self->regex);
8105 fprintf_unfiltered (fp, "\n");
8106 }
8107
8108 static struct breakpoint_ops catch_solib_breakpoint_ops;
8109
8110 /* See breakpoint.h. */
8111
8112 void
8113 add_solib_catchpoint (const char *arg, bool is_load, bool is_temp, bool enabled)
8114 {
8115 struct gdbarch *gdbarch = get_current_arch ();
8116
8117 if (!arg)
8118 arg = "";
8119 arg = skip_spaces (arg);
8120
8121 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
8122
8123 if (*arg != '\0')
8124 {
8125 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8126 _("Invalid regexp")));
8127 c->regex = xstrdup (arg);
8128 }
8129
8130 c->is_load = is_load;
8131 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
8132 &catch_solib_breakpoint_ops);
8133
8134 c->enable_state = enabled ? bp_enabled : bp_disabled;
8135
8136 install_breakpoint (0, std::move (c), 1);
8137 }
8138
8139 /* A helper function that does all the work for "catch load" and
8140 "catch unload". */
8141
8142 static void
8143 catch_load_or_unload (const char *arg, int from_tty, int is_load,
8144 struct cmd_list_element *command)
8145 {
8146 const int enabled = 1;
8147 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
8148
8149 add_solib_catchpoint (arg, is_load, temp, enabled);
8150 }
8151
8152 static void
8153 catch_load_command_1 (const char *arg, int from_tty,
8154 struct cmd_list_element *command)
8155 {
8156 catch_load_or_unload (arg, from_tty, 1, command);
8157 }
8158
8159 static void
8160 catch_unload_command_1 (const char *arg, int from_tty,
8161 struct cmd_list_element *command)
8162 {
8163 catch_load_or_unload (arg, from_tty, 0, command);
8164 }
8165
8166 /* See breakpoint.h. */
8167
8168 void
8169 init_catchpoint (struct breakpoint *b,
8170 struct gdbarch *gdbarch, bool temp,
8171 const char *cond_string,
8172 const struct breakpoint_ops *ops)
8173 {
8174 symtab_and_line sal;
8175 sal.pspace = current_program_space;
8176
8177 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8178
8179 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8180 b->disposition = temp ? disp_del : disp_donttouch;
8181 }
8182
8183 void
8184 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8185 {
8186 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8187 set_breakpoint_number (internal, b);
8188 if (is_tracepoint (b))
8189 set_tracepoint_count (breakpoint_count);
8190 if (!internal)
8191 mention (b);
8192 gdb::observers::breakpoint_created.notify (b);
8193
8194 if (update_gll)
8195 update_global_location_list (UGLL_MAY_INSERT);
8196 }
8197
8198 static void
8199 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8200 bool temp, const char *cond_string,
8201 const struct breakpoint_ops *ops)
8202 {
8203 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
8204
8205 init_catchpoint (c.get (), gdbarch, temp, cond_string, ops);
8206
8207 c->forked_inferior_pid = null_ptid;
8208
8209 install_breakpoint (0, std::move (c), 1);
8210 }
8211
8212 /* Exec catchpoints. */
8213
8214 /* An instance of this type is used to represent an exec catchpoint.
8215 A breakpoint is really of this type iff its ops pointer points to
8216 CATCH_EXEC_BREAKPOINT_OPS. */
8217
8218 struct exec_catchpoint : public breakpoint
8219 {
8220 ~exec_catchpoint () override;
8221
8222 /* Filename of a program whose exec triggered this catchpoint.
8223 This field is only valid immediately after this catchpoint has
8224 triggered. */
8225 char *exec_pathname;
8226 };
8227
8228 /* Exec catchpoint destructor. */
8229
8230 exec_catchpoint::~exec_catchpoint ()
8231 {
8232 xfree (this->exec_pathname);
8233 }
8234
8235 static int
8236 insert_catch_exec (struct bp_location *bl)
8237 {
8238 return target_insert_exec_catchpoint (inferior_ptid.pid ());
8239 }
8240
8241 static int
8242 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8243 {
8244 return target_remove_exec_catchpoint (inferior_ptid.pid ());
8245 }
8246
8247 static int
8248 breakpoint_hit_catch_exec (const struct bp_location *bl,
8249 const address_space *aspace, CORE_ADDR bp_addr,
8250 const struct target_waitstatus *ws)
8251 {
8252 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8253
8254 if (ws->kind != TARGET_WAITKIND_EXECD)
8255 return 0;
8256
8257 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8258 return 1;
8259 }
8260
8261 static enum print_stop_action
8262 print_it_catch_exec (bpstat bs)
8263 {
8264 struct ui_out *uiout = current_uiout;
8265 struct breakpoint *b = bs->breakpoint_at;
8266 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8267
8268 annotate_catchpoint (b->number);
8269 maybe_print_thread_hit_breakpoint (uiout);
8270 if (b->disposition == disp_del)
8271 uiout->text ("Temporary catchpoint ");
8272 else
8273 uiout->text ("Catchpoint ");
8274 if (uiout->is_mi_like_p ())
8275 {
8276 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8277 uiout->field_string ("disp", bpdisp_text (b->disposition));
8278 }
8279 uiout->field_signed ("bkptno", b->number);
8280 uiout->text (" (exec'd ");
8281 uiout->field_string ("new-exec", c->exec_pathname);
8282 uiout->text ("), ");
8283
8284 return PRINT_SRC_AND_LOC;
8285 }
8286
8287 static void
8288 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8289 {
8290 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8291 struct value_print_options opts;
8292 struct ui_out *uiout = current_uiout;
8293
8294 get_user_print_options (&opts);
8295
8296 /* Field 4, the address, is omitted (which makes the columns
8297 not line up too nicely with the headers, but the effect
8298 is relatively readable). */
8299 if (opts.addressprint)
8300 uiout->field_skip ("addr");
8301 annotate_field (5);
8302 uiout->text ("exec");
8303 if (c->exec_pathname != NULL)
8304 {
8305 uiout->text (", program \"");
8306 uiout->field_string ("what", c->exec_pathname);
8307 uiout->text ("\" ");
8308 }
8309
8310 if (uiout->is_mi_like_p ())
8311 uiout->field_string ("catch-type", "exec");
8312 }
8313
8314 static void
8315 print_mention_catch_exec (struct breakpoint *b)
8316 {
8317 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8318 }
8319
8320 /* Implement the "print_recreate" breakpoint_ops method for exec
8321 catchpoints. */
8322
8323 static void
8324 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8325 {
8326 fprintf_unfiltered (fp, "catch exec");
8327 print_recreate_thread (b, fp);
8328 }
8329
8330 static struct breakpoint_ops catch_exec_breakpoint_ops;
8331
8332 static int
8333 hw_breakpoint_used_count (void)
8334 {
8335 int i = 0;
8336
8337 for (breakpoint *b : all_breakpoints ())
8338 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8339 for (bp_location *bl : b->locations ())
8340 {
8341 /* Special types of hardware breakpoints may use more than
8342 one register. */
8343 i += b->ops->resources_needed (bl);
8344 }
8345
8346 return i;
8347 }
8348
8349 /* Returns the resources B would use if it were a hardware
8350 watchpoint. */
8351
8352 static int
8353 hw_watchpoint_use_count (struct breakpoint *b)
8354 {
8355 int i = 0;
8356
8357 if (!breakpoint_enabled (b))
8358 return 0;
8359
8360 for (bp_location *bl : b->locations ())
8361 {
8362 /* Special types of hardware watchpoints may use more than
8363 one register. */
8364 i += b->ops->resources_needed (bl);
8365 }
8366
8367 return i;
8368 }
8369
8370 /* Returns the sum the used resources of all hardware watchpoints of
8371 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8372 the sum of the used resources of all hardware watchpoints of other
8373 types _not_ TYPE. */
8374
8375 static int
8376 hw_watchpoint_used_count_others (struct breakpoint *except,
8377 enum bptype type, int *other_type_used)
8378 {
8379 int i = 0;
8380
8381 *other_type_used = 0;
8382 for (breakpoint *b : all_breakpoints ())
8383 {
8384 if (b == except)
8385 continue;
8386 if (!breakpoint_enabled (b))
8387 continue;
8388
8389 if (b->type == type)
8390 i += hw_watchpoint_use_count (b);
8391 else if (is_hardware_watchpoint (b))
8392 *other_type_used = 1;
8393 }
8394
8395 return i;
8396 }
8397
8398 void
8399 disable_watchpoints_before_interactive_call_start (void)
8400 {
8401 for (breakpoint *b : all_breakpoints ())
8402 if (is_watchpoint (b) && breakpoint_enabled (b))
8403 {
8404 b->enable_state = bp_call_disabled;
8405 update_global_location_list (UGLL_DONT_INSERT);
8406 }
8407 }
8408
8409 void
8410 enable_watchpoints_after_interactive_call_stop (void)
8411 {
8412 for (breakpoint *b : all_breakpoints ())
8413 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8414 {
8415 b->enable_state = bp_enabled;
8416 update_global_location_list (UGLL_MAY_INSERT);
8417 }
8418 }
8419
8420 void
8421 disable_breakpoints_before_startup (void)
8422 {
8423 current_program_space->executing_startup = 1;
8424 update_global_location_list (UGLL_DONT_INSERT);
8425 }
8426
8427 void
8428 enable_breakpoints_after_startup (void)
8429 {
8430 current_program_space->executing_startup = 0;
8431 breakpoint_re_set ();
8432 }
8433
8434 /* Create a new single-step breakpoint for thread THREAD, with no
8435 locations. */
8436
8437 static struct breakpoint *
8438 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8439 {
8440 std::unique_ptr<breakpoint> b (new breakpoint ());
8441
8442 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
8443 &momentary_breakpoint_ops);
8444
8445 b->disposition = disp_donttouch;
8446 b->frame_id = null_frame_id;
8447
8448 b->thread = thread;
8449 gdb_assert (b->thread != 0);
8450
8451 return add_to_breakpoint_chain (std::move (b));
8452 }
8453
8454 /* Set a momentary breakpoint of type TYPE at address specified by
8455 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8456 frame. */
8457
8458 breakpoint_up
8459 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8460 struct frame_id frame_id, enum bptype type)
8461 {
8462 struct breakpoint *b;
8463
8464 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8465 tail-called one. */
8466 gdb_assert (!frame_id_artificial_p (frame_id));
8467
8468 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8469 b->enable_state = bp_enabled;
8470 b->disposition = disp_donttouch;
8471 b->frame_id = frame_id;
8472
8473 b->thread = inferior_thread ()->global_num;
8474
8475 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8476
8477 return breakpoint_up (b);
8478 }
8479
8480 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8481 The new breakpoint will have type TYPE, use OPS as its
8482 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8483
8484 static struct breakpoint *
8485 momentary_breakpoint_from_master (struct breakpoint *orig,
8486 enum bptype type,
8487 const struct breakpoint_ops *ops,
8488 int loc_enabled)
8489 {
8490 struct breakpoint *copy;
8491
8492 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8493 copy->loc = allocate_bp_location (copy);
8494 set_breakpoint_location_function (copy->loc);
8495
8496 copy->loc->gdbarch = orig->loc->gdbarch;
8497 copy->loc->requested_address = orig->loc->requested_address;
8498 copy->loc->address = orig->loc->address;
8499 copy->loc->section = orig->loc->section;
8500 copy->loc->pspace = orig->loc->pspace;
8501 copy->loc->probe = orig->loc->probe;
8502 copy->loc->line_number = orig->loc->line_number;
8503 copy->loc->symtab = orig->loc->symtab;
8504 copy->loc->enabled = loc_enabled;
8505 copy->frame_id = orig->frame_id;
8506 copy->thread = orig->thread;
8507 copy->pspace = orig->pspace;
8508
8509 copy->enable_state = bp_enabled;
8510 copy->disposition = disp_donttouch;
8511 copy->number = internal_breakpoint_number--;
8512
8513 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8514 return copy;
8515 }
8516
8517 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8518 ORIG is NULL. */
8519
8520 struct breakpoint *
8521 clone_momentary_breakpoint (struct breakpoint *orig)
8522 {
8523 /* If there's nothing to clone, then return nothing. */
8524 if (orig == NULL)
8525 return NULL;
8526
8527 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8528 }
8529
8530 breakpoint_up
8531 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8532 enum bptype type)
8533 {
8534 struct symtab_and_line sal;
8535
8536 sal = find_pc_line (pc, 0);
8537 sal.pc = pc;
8538 sal.section = find_pc_overlay (pc);
8539 sal.explicit_pc = 1;
8540
8541 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8542 }
8543 \f
8544
8545 /* Tell the user we have just set a breakpoint B. */
8546
8547 static void
8548 mention (struct breakpoint *b)
8549 {
8550 b->ops->print_mention (b);
8551 current_uiout->text ("\n");
8552 }
8553 \f
8554
8555 static bool bp_loc_is_permanent (struct bp_location *loc);
8556
8557 /* Handle "set breakpoint auto-hw on".
8558
8559 If the explicitly specified breakpoint type is not hardware
8560 breakpoint, check the memory map to see whether the breakpoint
8561 address is in read-only memory.
8562
8563 - location type is not hardware breakpoint, memory is read-only.
8564 We change the type of the location to hardware breakpoint.
8565
8566 - location type is hardware breakpoint, memory is read-write. This
8567 means we've previously made the location hardware one, but then the
8568 memory map changed, so we undo.
8569 */
8570
8571 static void
8572 handle_automatic_hardware_breakpoints (bp_location *bl)
8573 {
8574 if (automatic_hardware_breakpoints
8575 && bl->owner->type != bp_hardware_breakpoint
8576 && (bl->loc_type == bp_loc_software_breakpoint
8577 || bl->loc_type == bp_loc_hardware_breakpoint))
8578 {
8579 /* When breakpoints are removed, remove_breakpoints will use
8580 location types we've just set here, the only possible problem
8581 is that memory map has changed during running program, but
8582 it's not going to work anyway with current gdb. */
8583 mem_region *mr = lookup_mem_region (bl->address);
8584
8585 if (mr != nullptr)
8586 {
8587 enum bp_loc_type new_type;
8588
8589 if (mr->attrib.mode != MEM_RW)
8590 new_type = bp_loc_hardware_breakpoint;
8591 else
8592 new_type = bp_loc_software_breakpoint;
8593
8594 if (new_type != bl->loc_type)
8595 {
8596 static bool said = false;
8597
8598 bl->loc_type = new_type;
8599 if (!said)
8600 {
8601 fprintf_filtered (gdb_stdout,
8602 _("Note: automatically using "
8603 "hardware breakpoints for "
8604 "read-only addresses.\n"));
8605 said = true;
8606 }
8607 }
8608 }
8609 }
8610 }
8611
8612 static struct bp_location *
8613 add_location_to_breakpoint (struct breakpoint *b,
8614 const struct symtab_and_line *sal)
8615 {
8616 struct bp_location *loc, **tmp;
8617 CORE_ADDR adjusted_address;
8618 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8619
8620 if (loc_gdbarch == NULL)
8621 loc_gdbarch = b->gdbarch;
8622
8623 /* Adjust the breakpoint's address prior to allocating a location.
8624 Once we call allocate_bp_location(), that mostly uninitialized
8625 location will be placed on the location chain. Adjustment of the
8626 breakpoint may cause target_read_memory() to be called and we do
8627 not want its scan of the location chain to find a breakpoint and
8628 location that's only been partially initialized. */
8629 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8630 sal->pc, b->type);
8631
8632 /* Sort the locations by their ADDRESS. */
8633 loc = allocate_bp_location (b);
8634 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8635 tmp = &((*tmp)->next))
8636 ;
8637 loc->next = *tmp;
8638 *tmp = loc;
8639
8640 loc->requested_address = sal->pc;
8641 loc->address = adjusted_address;
8642 loc->pspace = sal->pspace;
8643 loc->probe.prob = sal->prob;
8644 loc->probe.objfile = sal->objfile;
8645 gdb_assert (loc->pspace != NULL);
8646 loc->section = sal->section;
8647 loc->gdbarch = loc_gdbarch;
8648 loc->line_number = sal->line;
8649 loc->symtab = sal->symtab;
8650 loc->symbol = sal->symbol;
8651 loc->msymbol = sal->msymbol;
8652 loc->objfile = sal->objfile;
8653
8654 set_breakpoint_location_function (loc);
8655
8656 /* While by definition, permanent breakpoints are already present in the
8657 code, we don't mark the location as inserted. Normally one would expect
8658 that GDB could rely on that breakpoint instruction to stop the program,
8659 thus removing the need to insert its own breakpoint, except that executing
8660 the breakpoint instruction can kill the target instead of reporting a
8661 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8662 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8663 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8664 breakpoint be inserted normally results in QEMU knowing about the GDB
8665 breakpoint, and thus trap before the breakpoint instruction is executed.
8666 (If GDB later needs to continue execution past the permanent breakpoint,
8667 it manually increments the PC, thus avoiding executing the breakpoint
8668 instruction.) */
8669 if (bp_loc_is_permanent (loc))
8670 loc->permanent = 1;
8671
8672 return loc;
8673 }
8674 \f
8675
8676 /* Return true if LOC is pointing to a permanent breakpoint,
8677 return false otherwise. */
8678
8679 static bool
8680 bp_loc_is_permanent (struct bp_location *loc)
8681 {
8682 gdb_assert (loc != NULL);
8683
8684 /* If we have a non-breakpoint-backed catchpoint or a software
8685 watchpoint, just return 0. We should not attempt to read from
8686 the addresses the locations of these breakpoint types point to.
8687 gdbarch_program_breakpoint_here_p, below, will attempt to read
8688 memory. */
8689 if (!bl_address_is_meaningful (loc))
8690 return false;
8691
8692 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8693 switch_to_program_space_and_thread (loc->pspace);
8694 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
8695 }
8696
8697 /* Build a command list for the dprintf corresponding to the current
8698 settings of the dprintf style options. */
8699
8700 static void
8701 update_dprintf_command_list (struct breakpoint *b)
8702 {
8703 char *dprintf_args = b->extra_string;
8704 char *printf_line = NULL;
8705
8706 if (!dprintf_args)
8707 return;
8708
8709 dprintf_args = skip_spaces (dprintf_args);
8710
8711 /* Allow a comma, as it may have terminated a location, but don't
8712 insist on it. */
8713 if (*dprintf_args == ',')
8714 ++dprintf_args;
8715 dprintf_args = skip_spaces (dprintf_args);
8716
8717 if (*dprintf_args != '"')
8718 error (_("Bad format string, missing '\"'."));
8719
8720 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8721 printf_line = xstrprintf ("printf %s", dprintf_args);
8722 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8723 {
8724 if (!dprintf_function)
8725 error (_("No function supplied for dprintf call"));
8726
8727 if (dprintf_channel && strlen (dprintf_channel) > 0)
8728 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8729 dprintf_function,
8730 dprintf_channel,
8731 dprintf_args);
8732 else
8733 printf_line = xstrprintf ("call (void) %s (%s)",
8734 dprintf_function,
8735 dprintf_args);
8736 }
8737 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8738 {
8739 if (target_can_run_breakpoint_commands ())
8740 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8741 else
8742 {
8743 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8744 printf_line = xstrprintf ("printf %s", dprintf_args);
8745 }
8746 }
8747 else
8748 internal_error (__FILE__, __LINE__,
8749 _("Invalid dprintf style."));
8750
8751 gdb_assert (printf_line != NULL);
8752
8753 /* Manufacture a printf sequence. */
8754 struct command_line *printf_cmd_line
8755 = new struct command_line (simple_control, printf_line);
8756 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8757 command_lines_deleter ()));
8758 }
8759
8760 /* Update all dprintf commands, making their command lists reflect
8761 current style settings. */
8762
8763 static void
8764 update_dprintf_commands (const char *args, int from_tty,
8765 struct cmd_list_element *c)
8766 {
8767 for (breakpoint *b : all_breakpoints ())
8768 if (b->type == bp_dprintf)
8769 update_dprintf_command_list (b);
8770 }
8771
8772 /* Create a breakpoint with SAL as location. Use LOCATION
8773 as a description of the location, and COND_STRING
8774 as condition expression. If LOCATION is NULL then create an
8775 "address location" from the address in the SAL. */
8776
8777 static void
8778 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8779 gdb::array_view<const symtab_and_line> sals,
8780 event_location_up &&location,
8781 gdb::unique_xmalloc_ptr<char> filter,
8782 gdb::unique_xmalloc_ptr<char> cond_string,
8783 gdb::unique_xmalloc_ptr<char> extra_string,
8784 enum bptype type, enum bpdisp disposition,
8785 int thread, int task, int ignore_count,
8786 const struct breakpoint_ops *ops, int from_tty,
8787 int enabled, int internal, unsigned flags,
8788 int display_canonical)
8789 {
8790 int i;
8791
8792 if (type == bp_hardware_breakpoint)
8793 {
8794 int target_resources_ok;
8795
8796 i = hw_breakpoint_used_count ();
8797 target_resources_ok =
8798 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8799 i + 1, 0);
8800 if (target_resources_ok == 0)
8801 error (_("No hardware breakpoint support in the target."));
8802 else if (target_resources_ok < 0)
8803 error (_("Hardware breakpoints used exceeds limit."));
8804 }
8805
8806 gdb_assert (!sals.empty ());
8807
8808 for (const auto &sal : sals)
8809 {
8810 struct bp_location *loc;
8811
8812 if (from_tty)
8813 {
8814 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8815 if (!loc_gdbarch)
8816 loc_gdbarch = gdbarch;
8817
8818 describe_other_breakpoints (loc_gdbarch,
8819 sal.pspace, sal.pc, sal.section, thread);
8820 }
8821
8822 if (&sal == &sals[0])
8823 {
8824 init_raw_breakpoint (b, gdbarch, sal, type, ops);
8825 b->thread = thread;
8826 b->task = task;
8827
8828 b->cond_string = cond_string.release ();
8829 b->extra_string = extra_string.release ();
8830 b->ignore_count = ignore_count;
8831 b->enable_state = enabled ? bp_enabled : bp_disabled;
8832 b->disposition = disposition;
8833
8834 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8835 b->loc->inserted = 1;
8836
8837 if (type == bp_static_tracepoint)
8838 {
8839 struct tracepoint *t = (struct tracepoint *) b;
8840 struct static_tracepoint_marker marker;
8841
8842 if (strace_marker_p (b))
8843 {
8844 /* We already know the marker exists, otherwise, we
8845 wouldn't see a sal for it. */
8846 const char *p
8847 = &event_location_to_string (b->location.get ())[3];
8848 const char *endp;
8849
8850 p = skip_spaces (p);
8851
8852 endp = skip_to_space (p);
8853
8854 t->static_trace_marker_id.assign (p, endp - p);
8855
8856 printf_filtered (_("Probed static tracepoint "
8857 "marker \"%s\"\n"),
8858 t->static_trace_marker_id.c_str ());
8859 }
8860 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8861 {
8862 t->static_trace_marker_id = std::move (marker.str_id);
8863
8864 printf_filtered (_("Probed static tracepoint "
8865 "marker \"%s\"\n"),
8866 t->static_trace_marker_id.c_str ());
8867 }
8868 else
8869 warning (_("Couldn't determine the static "
8870 "tracepoint marker to probe"));
8871 }
8872
8873 loc = b->loc;
8874 }
8875 else
8876 {
8877 loc = add_location_to_breakpoint (b, &sal);
8878 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8879 loc->inserted = 1;
8880 }
8881
8882 /* Do not set breakpoint locations conditions yet. As locations
8883 are inserted, they get sorted based on their addresses. Let
8884 the list stabilize to have reliable location numbers. */
8885
8886 /* Dynamic printf requires and uses additional arguments on the
8887 command line, otherwise it's an error. */
8888 if (type == bp_dprintf)
8889 {
8890 if (b->extra_string)
8891 update_dprintf_command_list (b);
8892 else
8893 error (_("Format string required"));
8894 }
8895 else if (b->extra_string)
8896 error (_("Garbage '%s' at end of command"), b->extra_string);
8897 }
8898
8899
8900 /* The order of the locations is now stable. Set the location
8901 condition using the location's number. */
8902 int loc_num = 1;
8903 for (bp_location *loc : b->locations ())
8904 {
8905 if (b->cond_string != nullptr)
8906 set_breakpoint_location_condition (b->cond_string, loc, b->number,
8907 loc_num);
8908
8909 ++loc_num;
8910 }
8911
8912 b->display_canonical = display_canonical;
8913 if (location != NULL)
8914 b->location = std::move (location);
8915 else
8916 b->location = new_address_location (b->loc->address, NULL, 0);
8917 b->filter = std::move (filter);
8918 }
8919
8920 static void
8921 create_breakpoint_sal (struct gdbarch *gdbarch,
8922 gdb::array_view<const symtab_and_line> sals,
8923 event_location_up &&location,
8924 gdb::unique_xmalloc_ptr<char> filter,
8925 gdb::unique_xmalloc_ptr<char> cond_string,
8926 gdb::unique_xmalloc_ptr<char> extra_string,
8927 enum bptype type, enum bpdisp disposition,
8928 int thread, int task, int ignore_count,
8929 const struct breakpoint_ops *ops, int from_tty,
8930 int enabled, int internal, unsigned flags,
8931 int display_canonical)
8932 {
8933 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
8934
8935 init_breakpoint_sal (b.get (), gdbarch,
8936 sals, std::move (location),
8937 std::move (filter),
8938 std::move (cond_string),
8939 std::move (extra_string),
8940 type, disposition,
8941 thread, task, ignore_count,
8942 ops, from_tty,
8943 enabled, internal, flags,
8944 display_canonical);
8945
8946 install_breakpoint (internal, std::move (b), 0);
8947 }
8948
8949 /* Add SALS.nelts breakpoints to the breakpoint table. For each
8950 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8951 value. COND_STRING, if not NULL, specified the condition to be
8952 used for all breakpoints. Essentially the only case where
8953 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8954 function. In that case, it's still not possible to specify
8955 separate conditions for different overloaded functions, so
8956 we take just a single condition string.
8957
8958 NOTE: If the function succeeds, the caller is expected to cleanup
8959 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
8960 array contents). If the function fails (error() is called), the
8961 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
8962 COND and SALS arrays and each of those arrays contents. */
8963
8964 static void
8965 create_breakpoints_sal (struct gdbarch *gdbarch,
8966 struct linespec_result *canonical,
8967 gdb::unique_xmalloc_ptr<char> cond_string,
8968 gdb::unique_xmalloc_ptr<char> extra_string,
8969 enum bptype type, enum bpdisp disposition,
8970 int thread, int task, int ignore_count,
8971 const struct breakpoint_ops *ops, int from_tty,
8972 int enabled, int internal, unsigned flags)
8973 {
8974 if (canonical->pre_expanded)
8975 gdb_assert (canonical->lsals.size () == 1);
8976
8977 for (const auto &lsal : canonical->lsals)
8978 {
8979 /* Note that 'location' can be NULL in the case of a plain
8980 'break', without arguments. */
8981 event_location_up location
8982 = (canonical->location != NULL
8983 ? copy_event_location (canonical->location.get ()) : NULL);
8984 gdb::unique_xmalloc_ptr<char> filter_string
8985 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
8986
8987 create_breakpoint_sal (gdbarch, lsal.sals,
8988 std::move (location),
8989 std::move (filter_string),
8990 std::move (cond_string),
8991 std::move (extra_string),
8992 type, disposition,
8993 thread, task, ignore_count, ops,
8994 from_tty, enabled, internal, flags,
8995 canonical->special_display);
8996 }
8997 }
8998
8999 /* Parse LOCATION which is assumed to be a SAL specification possibly
9000 followed by conditionals. On return, SALS contains an array of SAL
9001 addresses found. LOCATION points to the end of the SAL (for
9002 linespec locations).
9003
9004 The array and the line spec strings are allocated on the heap, it is
9005 the caller's responsibility to free them. */
9006
9007 static void
9008 parse_breakpoint_sals (struct event_location *location,
9009 struct linespec_result *canonical)
9010 {
9011 struct symtab_and_line cursal;
9012
9013 if (event_location_type (location) == LINESPEC_LOCATION)
9014 {
9015 const char *spec = get_linespec_location (location)->spec_string;
9016
9017 if (spec == NULL)
9018 {
9019 /* The last displayed codepoint, if it's valid, is our default
9020 breakpoint address. */
9021 if (last_displayed_sal_is_valid ())
9022 {
9023 /* Set sal's pspace, pc, symtab, and line to the values
9024 corresponding to the last call to print_frame_info.
9025 Be sure to reinitialize LINE with NOTCURRENT == 0
9026 as the breakpoint line number is inappropriate otherwise.
9027 find_pc_line would adjust PC, re-set it back. */
9028 symtab_and_line sal = get_last_displayed_sal ();
9029 CORE_ADDR pc = sal.pc;
9030
9031 sal = find_pc_line (pc, 0);
9032
9033 /* "break" without arguments is equivalent to "break *PC"
9034 where PC is the last displayed codepoint's address. So
9035 make sure to set sal.explicit_pc to prevent GDB from
9036 trying to expand the list of sals to include all other
9037 instances with the same symtab and line. */
9038 sal.pc = pc;
9039 sal.explicit_pc = 1;
9040
9041 struct linespec_sals lsal;
9042 lsal.sals = {sal};
9043 lsal.canonical = NULL;
9044
9045 canonical->lsals.push_back (std::move (lsal));
9046 return;
9047 }
9048 else
9049 error (_("No default breakpoint address now."));
9050 }
9051 }
9052
9053 /* Force almost all breakpoints to be in terms of the
9054 current_source_symtab (which is decode_line_1's default).
9055 This should produce the results we want almost all of the
9056 time while leaving default_breakpoint_* alone.
9057
9058 ObjC: However, don't match an Objective-C method name which
9059 may have a '+' or '-' succeeded by a '['. */
9060 cursal = get_current_source_symtab_and_line ();
9061 if (last_displayed_sal_is_valid ())
9062 {
9063 const char *spec = NULL;
9064
9065 if (event_location_type (location) == LINESPEC_LOCATION)
9066 spec = get_linespec_location (location)->spec_string;
9067
9068 if (!cursal.symtab
9069 || (spec != NULL
9070 && strchr ("+-", spec[0]) != NULL
9071 && spec[1] != '['))
9072 {
9073 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9074 get_last_displayed_symtab (),
9075 get_last_displayed_line (),
9076 canonical, NULL, NULL);
9077 return;
9078 }
9079 }
9080
9081 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9082 cursal.symtab, cursal.line, canonical, NULL, NULL);
9083 }
9084
9085
9086 /* Convert each SAL into a real PC. Verify that the PC can be
9087 inserted as a breakpoint. If it can't throw an error. */
9088
9089 static void
9090 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
9091 {
9092 for (auto &sal : sals)
9093 resolve_sal_pc (&sal);
9094 }
9095
9096 /* Fast tracepoints may have restrictions on valid locations. For
9097 instance, a fast tracepoint using a jump instead of a trap will
9098 likely have to overwrite more bytes than a trap would, and so can
9099 only be placed where the instruction is longer than the jump, or a
9100 multi-instruction sequence does not have a jump into the middle of
9101 it, etc. */
9102
9103 static void
9104 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9105 gdb::array_view<const symtab_and_line> sals)
9106 {
9107 for (const auto &sal : sals)
9108 {
9109 struct gdbarch *sarch;
9110
9111 sarch = get_sal_arch (sal);
9112 /* We fall back to GDBARCH if there is no architecture
9113 associated with SAL. */
9114 if (sarch == NULL)
9115 sarch = gdbarch;
9116 std::string msg;
9117 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
9118 error (_("May not have a fast tracepoint at %s%s"),
9119 paddress (sarch, sal.pc), msg.c_str ());
9120 }
9121 }
9122
9123 /* Given TOK, a string specification of condition and thread, as
9124 accepted by the 'break' command, extract the condition
9125 string and thread number and set *COND_STRING and *THREAD.
9126 PC identifies the context at which the condition should be parsed.
9127 If no condition is found, *COND_STRING is set to NULL.
9128 If no thread is found, *THREAD is set to -1. */
9129
9130 static void
9131 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9132 char **cond_string, int *thread, int *task,
9133 char **rest)
9134 {
9135 *cond_string = NULL;
9136 *thread = -1;
9137 *task = 0;
9138 *rest = NULL;
9139 bool force = false;
9140
9141 while (tok && *tok)
9142 {
9143 const char *end_tok;
9144 int toklen;
9145 const char *cond_start = NULL;
9146 const char *cond_end = NULL;
9147
9148 tok = skip_spaces (tok);
9149
9150 if ((*tok == '"' || *tok == ',') && rest)
9151 {
9152 *rest = savestring (tok, strlen (tok));
9153 return;
9154 }
9155
9156 end_tok = skip_to_space (tok);
9157
9158 toklen = end_tok - tok;
9159
9160 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9161 {
9162 tok = cond_start = end_tok + 1;
9163 try
9164 {
9165 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9166 }
9167 catch (const gdb_exception_error &)
9168 {
9169 if (!force)
9170 throw;
9171 else
9172 tok = tok + strlen (tok);
9173 }
9174 cond_end = tok;
9175 *cond_string = savestring (cond_start, cond_end - cond_start);
9176 }
9177 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
9178 {
9179 tok = tok + toklen;
9180 force = true;
9181 }
9182 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9183 {
9184 const char *tmptok;
9185 struct thread_info *thr;
9186
9187 tok = end_tok + 1;
9188 thr = parse_thread_id (tok, &tmptok);
9189 if (tok == tmptok)
9190 error (_("Junk after thread keyword."));
9191 *thread = thr->global_num;
9192 tok = tmptok;
9193 }
9194 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9195 {
9196 char *tmptok;
9197
9198 tok = end_tok + 1;
9199 *task = strtol (tok, &tmptok, 0);
9200 if (tok == tmptok)
9201 error (_("Junk after task keyword."));
9202 if (!valid_task_id (*task))
9203 error (_("Unknown task %d."), *task);
9204 tok = tmptok;
9205 }
9206 else if (rest)
9207 {
9208 *rest = savestring (tok, strlen (tok));
9209 return;
9210 }
9211 else
9212 error (_("Junk at end of arguments."));
9213 }
9214 }
9215
9216 /* Call 'find_condition_and_thread' for each sal in SALS until a parse
9217 succeeds. The parsed values are written to COND_STRING, THREAD,
9218 TASK, and REST. See the comment of 'find_condition_and_thread'
9219 for the description of these parameters and INPUT. */
9220
9221 static void
9222 find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
9223 const char *input, char **cond_string,
9224 int *thread, int *task, char **rest)
9225 {
9226 int num_failures = 0;
9227 for (auto &sal : sals)
9228 {
9229 char *cond = nullptr;
9230 int thread_id = 0;
9231 int task_id = 0;
9232 char *remaining = nullptr;
9233
9234 /* Here we want to parse 'arg' to separate condition from thread
9235 number. But because parsing happens in a context and the
9236 contexts of sals might be different, try each until there is
9237 success. Finding one successful parse is sufficient for our
9238 goal. When setting the breakpoint we'll re-parse the
9239 condition in the context of each sal. */
9240 try
9241 {
9242 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
9243 &task_id, &remaining);
9244 *cond_string = cond;
9245 *thread = thread_id;
9246 *task = task_id;
9247 *rest = remaining;
9248 break;
9249 }
9250 catch (const gdb_exception_error &e)
9251 {
9252 num_failures++;
9253 /* If no sal remains, do not continue. */
9254 if (num_failures == sals.size ())
9255 throw;
9256 }
9257 }
9258 }
9259
9260 /* Decode a static tracepoint marker spec. */
9261
9262 static std::vector<symtab_and_line>
9263 decode_static_tracepoint_spec (const char **arg_p)
9264 {
9265 const char *p = &(*arg_p)[3];
9266 const char *endp;
9267
9268 p = skip_spaces (p);
9269
9270 endp = skip_to_space (p);
9271
9272 std::string marker_str (p, endp - p);
9273
9274 std::vector<static_tracepoint_marker> markers
9275 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9276 if (markers.empty ())
9277 error (_("No known static tracepoint marker named %s"),
9278 marker_str.c_str ());
9279
9280 std::vector<symtab_and_line> sals;
9281 sals.reserve (markers.size ());
9282
9283 for (const static_tracepoint_marker &marker : markers)
9284 {
9285 symtab_and_line sal = find_pc_line (marker.address, 0);
9286 sal.pc = marker.address;
9287 sals.push_back (sal);
9288 }
9289
9290 *arg_p = endp;
9291 return sals;
9292 }
9293
9294 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9295 according to IS_TRACEPOINT. */
9296
9297 static const struct breakpoint_ops *
9298 breakpoint_ops_for_event_location_type (enum event_location_type location_type,
9299 bool is_tracepoint)
9300 {
9301 if (is_tracepoint)
9302 {
9303 if (location_type == PROBE_LOCATION)
9304 return &tracepoint_probe_breakpoint_ops;
9305 else
9306 return &tracepoint_breakpoint_ops;
9307 }
9308 else
9309 {
9310 if (location_type == PROBE_LOCATION)
9311 return &bkpt_probe_breakpoint_ops;
9312 else
9313 return &bkpt_breakpoint_ops;
9314 }
9315 }
9316
9317 /* See breakpoint.h. */
9318
9319 const struct breakpoint_ops *
9320 breakpoint_ops_for_event_location (const struct event_location *location,
9321 bool is_tracepoint)
9322 {
9323 if (location != nullptr)
9324 return breakpoint_ops_for_event_location_type
9325 (event_location_type (location), is_tracepoint);
9326 return is_tracepoint ? &tracepoint_breakpoint_ops : &bkpt_breakpoint_ops;
9327 }
9328
9329 /* See breakpoint.h. */
9330
9331 int
9332 create_breakpoint (struct gdbarch *gdbarch,
9333 struct event_location *location,
9334 const char *cond_string,
9335 int thread, const char *extra_string,
9336 bool force_condition, int parse_extra,
9337 int tempflag, enum bptype type_wanted,
9338 int ignore_count,
9339 enum auto_boolean pending_break_support,
9340 const struct breakpoint_ops *ops,
9341 int from_tty, int enabled, int internal,
9342 unsigned flags)
9343 {
9344 struct linespec_result canonical;
9345 int pending = 0;
9346 int task = 0;
9347 int prev_bkpt_count = breakpoint_count;
9348
9349 gdb_assert (ops != NULL);
9350
9351 /* If extra_string isn't useful, set it to NULL. */
9352 if (extra_string != NULL && *extra_string == '\0')
9353 extra_string = NULL;
9354
9355 try
9356 {
9357 ops->create_sals_from_location (location, &canonical, type_wanted);
9358 }
9359 catch (const gdb_exception_error &e)
9360 {
9361 /* If caller is interested in rc value from parse, set
9362 value. */
9363 if (e.error == NOT_FOUND_ERROR)
9364 {
9365 /* If pending breakpoint support is turned off, throw
9366 error. */
9367
9368 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9369 throw;
9370
9371 exception_print (gdb_stderr, e);
9372
9373 /* If pending breakpoint support is auto query and the user
9374 selects no, then simply return the error code. */
9375 if (pending_break_support == AUTO_BOOLEAN_AUTO
9376 && !nquery (_("Make %s pending on future shared library load? "),
9377 bptype_string (type_wanted)))
9378 return 0;
9379
9380 /* At this point, either the user was queried about setting
9381 a pending breakpoint and selected yes, or pending
9382 breakpoint behavior is on and thus a pending breakpoint
9383 is defaulted on behalf of the user. */
9384 pending = 1;
9385 }
9386 else
9387 throw;
9388 }
9389
9390 if (!pending && canonical.lsals.empty ())
9391 return 0;
9392
9393 /* Resolve all line numbers to PC's and verify that the addresses
9394 are ok for the target. */
9395 if (!pending)
9396 {
9397 for (auto &lsal : canonical.lsals)
9398 breakpoint_sals_to_pc (lsal.sals);
9399 }
9400
9401 /* Fast tracepoints may have additional restrictions on location. */
9402 if (!pending && type_wanted == bp_fast_tracepoint)
9403 {
9404 for (const auto &lsal : canonical.lsals)
9405 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9406 }
9407
9408 /* Verify that condition can be parsed, before setting any
9409 breakpoints. Allocate a separate condition expression for each
9410 breakpoint. */
9411 if (!pending)
9412 {
9413 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9414 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9415
9416 if (parse_extra)
9417 {
9418 char *rest;
9419 char *cond;
9420
9421 const linespec_sals &lsal = canonical.lsals[0];
9422
9423 find_condition_and_thread_for_sals (lsal.sals, extra_string,
9424 &cond, &thread, &task, &rest);
9425 cond_string_copy.reset (cond);
9426 extra_string_copy.reset (rest);
9427 }
9428 else
9429 {
9430 if (type_wanted != bp_dprintf
9431 && extra_string != NULL && *extra_string != '\0')
9432 error (_("Garbage '%s' at end of location"), extra_string);
9433
9434 /* Check the validity of the condition. We should error out
9435 if the condition is invalid at all of the locations and
9436 if it is not forced. In the PARSE_EXTRA case above, this
9437 check is done when parsing the EXTRA_STRING. */
9438 if (cond_string != nullptr && !force_condition)
9439 {
9440 int num_failures = 0;
9441 const linespec_sals &lsal = canonical.lsals[0];
9442 for (const auto &sal : lsal.sals)
9443 {
9444 const char *cond = cond_string;
9445 try
9446 {
9447 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
9448 /* One success is sufficient to keep going. */
9449 break;
9450 }
9451 catch (const gdb_exception_error &)
9452 {
9453 num_failures++;
9454 /* If this is the last sal, error out. */
9455 if (num_failures == lsal.sals.size ())
9456 throw;
9457 }
9458 }
9459 }
9460
9461 /* Create a private copy of condition string. */
9462 if (cond_string)
9463 cond_string_copy.reset (xstrdup (cond_string));
9464 /* Create a private copy of any extra string. */
9465 if (extra_string)
9466 extra_string_copy.reset (xstrdup (extra_string));
9467 }
9468
9469 ops->create_breakpoints_sal (gdbarch, &canonical,
9470 std::move (cond_string_copy),
9471 std::move (extra_string_copy),
9472 type_wanted,
9473 tempflag ? disp_del : disp_donttouch,
9474 thread, task, ignore_count, ops,
9475 from_tty, enabled, internal, flags);
9476 }
9477 else
9478 {
9479 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9480
9481 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9482 b->location = copy_event_location (location);
9483
9484 if (parse_extra)
9485 b->cond_string = NULL;
9486 else
9487 {
9488 /* Create a private copy of condition string. */
9489 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
9490 b->thread = thread;
9491 }
9492
9493 /* Create a private copy of any extra string. */
9494 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
9495 b->ignore_count = ignore_count;
9496 b->disposition = tempflag ? disp_del : disp_donttouch;
9497 b->condition_not_parsed = 1;
9498 b->enable_state = enabled ? bp_enabled : bp_disabled;
9499 if ((type_wanted != bp_breakpoint
9500 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9501 b->pspace = current_program_space;
9502
9503 install_breakpoint (internal, std::move (b), 0);
9504 }
9505
9506 if (canonical.lsals.size () > 1)
9507 {
9508 warning (_("Multiple breakpoints were set.\nUse the "
9509 "\"delete\" command to delete unwanted breakpoints."));
9510 prev_breakpoint_count = prev_bkpt_count;
9511 }
9512
9513 update_global_location_list (UGLL_MAY_INSERT);
9514
9515 return 1;
9516 }
9517
9518 /* Set a breakpoint.
9519 ARG is a string describing breakpoint address,
9520 condition, and thread.
9521 FLAG specifies if a breakpoint is hardware on,
9522 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9523 and BP_TEMPFLAG. */
9524
9525 static void
9526 break_command_1 (const char *arg, int flag, int from_tty)
9527 {
9528 int tempflag = flag & BP_TEMPFLAG;
9529 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9530 ? bp_hardware_breakpoint
9531 : bp_breakpoint);
9532
9533 event_location_up location = string_to_event_location (&arg, current_language);
9534 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9535 (location.get (), false /* is_tracepoint */);
9536
9537 create_breakpoint (get_current_arch (),
9538 location.get (),
9539 NULL, 0, arg, false, 1 /* parse arg */,
9540 tempflag, type_wanted,
9541 0 /* Ignore count */,
9542 pending_break_support,
9543 ops,
9544 from_tty,
9545 1 /* enabled */,
9546 0 /* internal */,
9547 0);
9548 }
9549
9550 /* Helper function for break_command_1 and disassemble_command. */
9551
9552 void
9553 resolve_sal_pc (struct symtab_and_line *sal)
9554 {
9555 CORE_ADDR pc;
9556
9557 if (sal->pc == 0 && sal->symtab != NULL)
9558 {
9559 if (!find_line_pc (sal->symtab, sal->line, &pc))
9560 error (_("No line %d in file \"%s\"."),
9561 sal->line, symtab_to_filename_for_display (sal->symtab));
9562 sal->pc = pc;
9563
9564 /* If this SAL corresponds to a breakpoint inserted using a line
9565 number, then skip the function prologue if necessary. */
9566 if (sal->explicit_line)
9567 skip_prologue_sal (sal);
9568 }
9569
9570 if (sal->section == 0 && sal->symtab != NULL)
9571 {
9572 const struct blockvector *bv;
9573 const struct block *b;
9574 struct symbol *sym;
9575
9576 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9577 SYMTAB_COMPUNIT (sal->symtab));
9578 if (bv != NULL)
9579 {
9580 sym = block_linkage_function (b);
9581 if (sym != NULL)
9582 {
9583 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9584 sal->section = sym->obj_section (SYMTAB_OBJFILE (sal->symtab));
9585 }
9586 else
9587 {
9588 /* It really is worthwhile to have the section, so we'll
9589 just have to look harder. This case can be executed
9590 if we have line numbers but no functions (as can
9591 happen in assembly source). */
9592
9593 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9594 switch_to_program_space_and_thread (sal->pspace);
9595
9596 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9597 if (msym.minsym)
9598 sal->section = msym.obj_section ();
9599 }
9600 }
9601 }
9602 }
9603
9604 void
9605 break_command (const char *arg, int from_tty)
9606 {
9607 break_command_1 (arg, 0, from_tty);
9608 }
9609
9610 void
9611 tbreak_command (const char *arg, int from_tty)
9612 {
9613 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9614 }
9615
9616 static void
9617 hbreak_command (const char *arg, int from_tty)
9618 {
9619 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9620 }
9621
9622 static void
9623 thbreak_command (const char *arg, int from_tty)
9624 {
9625 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9626 }
9627
9628 static void
9629 stop_command (const char *arg, int from_tty)
9630 {
9631 printf_filtered (_("Specify the type of breakpoint to set.\n\
9632 Usage: stop in <function | address>\n\
9633 stop at <line>\n"));
9634 }
9635
9636 static void
9637 stopin_command (const char *arg, int from_tty)
9638 {
9639 int badInput = 0;
9640
9641 if (arg == NULL)
9642 badInput = 1;
9643 else if (*arg != '*')
9644 {
9645 const char *argptr = arg;
9646 int hasColon = 0;
9647
9648 /* Look for a ':'. If this is a line number specification, then
9649 say it is bad, otherwise, it should be an address or
9650 function/method name. */
9651 while (*argptr && !hasColon)
9652 {
9653 hasColon = (*argptr == ':');
9654 argptr++;
9655 }
9656
9657 if (hasColon)
9658 badInput = (*argptr != ':'); /* Not a class::method */
9659 else
9660 badInput = isdigit (*arg); /* a simple line number */
9661 }
9662
9663 if (badInput)
9664 printf_filtered (_("Usage: stop in <function | address>\n"));
9665 else
9666 break_command_1 (arg, 0, from_tty);
9667 }
9668
9669 static void
9670 stopat_command (const char *arg, int from_tty)
9671 {
9672 int badInput = 0;
9673
9674 if (arg == NULL || *arg == '*') /* no line number */
9675 badInput = 1;
9676 else
9677 {
9678 const char *argptr = arg;
9679 int hasColon = 0;
9680
9681 /* Look for a ':'. If there is a '::' then get out, otherwise
9682 it is probably a line number. */
9683 while (*argptr && !hasColon)
9684 {
9685 hasColon = (*argptr == ':');
9686 argptr++;
9687 }
9688
9689 if (hasColon)
9690 badInput = (*argptr == ':'); /* we have class::method */
9691 else
9692 badInput = !isdigit (*arg); /* not a line number */
9693 }
9694
9695 if (badInput)
9696 printf_filtered (_("Usage: stop at LINE\n"));
9697 else
9698 break_command_1 (arg, 0, from_tty);
9699 }
9700
9701 /* The dynamic printf command is mostly like a regular breakpoint, but
9702 with a prewired command list consisting of a single output command,
9703 built from extra arguments supplied on the dprintf command
9704 line. */
9705
9706 static void
9707 dprintf_command (const char *arg, int from_tty)
9708 {
9709 event_location_up location = string_to_event_location (&arg, current_language);
9710
9711 /* If non-NULL, ARG should have been advanced past the location;
9712 the next character must be ','. */
9713 if (arg != NULL)
9714 {
9715 if (arg[0] != ',' || arg[1] == '\0')
9716 error (_("Format string required"));
9717 else
9718 {
9719 /* Skip the comma. */
9720 ++arg;
9721 }
9722 }
9723
9724 create_breakpoint (get_current_arch (),
9725 location.get (),
9726 NULL, 0, arg, false, 1 /* parse arg */,
9727 0, bp_dprintf,
9728 0 /* Ignore count */,
9729 pending_break_support,
9730 &dprintf_breakpoint_ops,
9731 from_tty,
9732 1 /* enabled */,
9733 0 /* internal */,
9734 0);
9735 }
9736
9737 static void
9738 agent_printf_command (const char *arg, int from_tty)
9739 {
9740 error (_("May only run agent-printf on the target"));
9741 }
9742
9743 /* Implement the "breakpoint_hit" breakpoint_ops method for
9744 ranged breakpoints. */
9745
9746 static int
9747 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9748 const address_space *aspace,
9749 CORE_ADDR bp_addr,
9750 const struct target_waitstatus *ws)
9751 {
9752 if (ws->kind != TARGET_WAITKIND_STOPPED
9753 || ws->value.sig != GDB_SIGNAL_TRAP)
9754 return 0;
9755
9756 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9757 bl->length, aspace, bp_addr);
9758 }
9759
9760 /* Implement the "resources_needed" breakpoint_ops method for
9761 ranged breakpoints. */
9762
9763 static int
9764 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9765 {
9766 return target_ranged_break_num_registers ();
9767 }
9768
9769 /* Implement the "print_it" breakpoint_ops method for
9770 ranged breakpoints. */
9771
9772 static enum print_stop_action
9773 print_it_ranged_breakpoint (bpstat bs)
9774 {
9775 struct breakpoint *b = bs->breakpoint_at;
9776 struct bp_location *bl = b->loc;
9777 struct ui_out *uiout = current_uiout;
9778
9779 gdb_assert (b->type == bp_hardware_breakpoint);
9780
9781 /* Ranged breakpoints have only one location. */
9782 gdb_assert (bl && bl->next == NULL);
9783
9784 annotate_breakpoint (b->number);
9785
9786 maybe_print_thread_hit_breakpoint (uiout);
9787
9788 if (b->disposition == disp_del)
9789 uiout->text ("Temporary ranged breakpoint ");
9790 else
9791 uiout->text ("Ranged breakpoint ");
9792 if (uiout->is_mi_like_p ())
9793 {
9794 uiout->field_string ("reason",
9795 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9796 uiout->field_string ("disp", bpdisp_text (b->disposition));
9797 }
9798 uiout->field_signed ("bkptno", b->number);
9799 uiout->text (", ");
9800
9801 return PRINT_SRC_AND_LOC;
9802 }
9803
9804 /* Implement the "print_one" breakpoint_ops method for
9805 ranged breakpoints. */
9806
9807 static void
9808 print_one_ranged_breakpoint (struct breakpoint *b,
9809 struct bp_location **last_loc)
9810 {
9811 struct bp_location *bl = b->loc;
9812 struct value_print_options opts;
9813 struct ui_out *uiout = current_uiout;
9814
9815 /* Ranged breakpoints have only one location. */
9816 gdb_assert (bl && bl->next == NULL);
9817
9818 get_user_print_options (&opts);
9819
9820 if (opts.addressprint)
9821 /* We don't print the address range here, it will be printed later
9822 by print_one_detail_ranged_breakpoint. */
9823 uiout->field_skip ("addr");
9824 annotate_field (5);
9825 print_breakpoint_location (b, bl);
9826 *last_loc = bl;
9827 }
9828
9829 /* Implement the "print_one_detail" breakpoint_ops method for
9830 ranged breakpoints. */
9831
9832 static void
9833 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9834 struct ui_out *uiout)
9835 {
9836 CORE_ADDR address_start, address_end;
9837 struct bp_location *bl = b->loc;
9838 string_file stb;
9839
9840 gdb_assert (bl);
9841
9842 address_start = bl->address;
9843 address_end = address_start + bl->length - 1;
9844
9845 uiout->text ("\taddress range: ");
9846 stb.printf ("[%s, %s]",
9847 print_core_address (bl->gdbarch, address_start),
9848 print_core_address (bl->gdbarch, address_end));
9849 uiout->field_stream ("addr", stb);
9850 uiout->text ("\n");
9851 }
9852
9853 /* Implement the "print_mention" breakpoint_ops method for
9854 ranged breakpoints. */
9855
9856 static void
9857 print_mention_ranged_breakpoint (struct breakpoint *b)
9858 {
9859 struct bp_location *bl = b->loc;
9860 struct ui_out *uiout = current_uiout;
9861
9862 gdb_assert (bl);
9863 gdb_assert (b->type == bp_hardware_breakpoint);
9864
9865 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9866 b->number, paddress (bl->gdbarch, bl->address),
9867 paddress (bl->gdbarch, bl->address + bl->length - 1));
9868 }
9869
9870 /* Implement the "print_recreate" breakpoint_ops method for
9871 ranged breakpoints. */
9872
9873 static void
9874 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9875 {
9876 fprintf_unfiltered (fp, "break-range %s, %s",
9877 event_location_to_string (b->location.get ()),
9878 event_location_to_string (b->location_range_end.get ()));
9879 print_recreate_thread (b, fp);
9880 }
9881
9882 /* The breakpoint_ops structure to be used in ranged breakpoints. */
9883
9884 static struct breakpoint_ops ranged_breakpoint_ops;
9885
9886 /* Find the address where the end of the breakpoint range should be
9887 placed, given the SAL of the end of the range. This is so that if
9888 the user provides a line number, the end of the range is set to the
9889 last instruction of the given line. */
9890
9891 static CORE_ADDR
9892 find_breakpoint_range_end (struct symtab_and_line sal)
9893 {
9894 CORE_ADDR end;
9895
9896 /* If the user provided a PC value, use it. Otherwise,
9897 find the address of the end of the given location. */
9898 if (sal.explicit_pc)
9899 end = sal.pc;
9900 else
9901 {
9902 int ret;
9903 CORE_ADDR start;
9904
9905 ret = find_line_pc_range (sal, &start, &end);
9906 if (!ret)
9907 error (_("Could not find location of the end of the range."));
9908
9909 /* find_line_pc_range returns the start of the next line. */
9910 end--;
9911 }
9912
9913 return end;
9914 }
9915
9916 /* Implement the "break-range" CLI command. */
9917
9918 static void
9919 break_range_command (const char *arg, int from_tty)
9920 {
9921 const char *arg_start;
9922 struct linespec_result canonical_start, canonical_end;
9923 int bp_count, can_use_bp, length;
9924 CORE_ADDR end;
9925 struct breakpoint *b;
9926
9927 /* We don't support software ranged breakpoints. */
9928 if (target_ranged_break_num_registers () < 0)
9929 error (_("This target does not support hardware ranged breakpoints."));
9930
9931 bp_count = hw_breakpoint_used_count ();
9932 bp_count += target_ranged_break_num_registers ();
9933 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9934 bp_count, 0);
9935 if (can_use_bp < 0)
9936 error (_("Hardware breakpoints used exceeds limit."));
9937
9938 arg = skip_spaces (arg);
9939 if (arg == NULL || arg[0] == '\0')
9940 error(_("No address range specified."));
9941
9942 arg_start = arg;
9943 event_location_up start_location = string_to_event_location (&arg,
9944 current_language);
9945 parse_breakpoint_sals (start_location.get (), &canonical_start);
9946
9947 if (arg[0] != ',')
9948 error (_("Too few arguments."));
9949 else if (canonical_start.lsals.empty ())
9950 error (_("Could not find location of the beginning of the range."));
9951
9952 const linespec_sals &lsal_start = canonical_start.lsals[0];
9953
9954 if (canonical_start.lsals.size () > 1
9955 || lsal_start.sals.size () != 1)
9956 error (_("Cannot create a ranged breakpoint with multiple locations."));
9957
9958 const symtab_and_line &sal_start = lsal_start.sals[0];
9959 std::string addr_string_start (arg_start, arg - arg_start);
9960
9961 arg++; /* Skip the comma. */
9962 arg = skip_spaces (arg);
9963
9964 /* Parse the end location. */
9965
9966 arg_start = arg;
9967
9968 /* We call decode_line_full directly here instead of using
9969 parse_breakpoint_sals because we need to specify the start location's
9970 symtab and line as the default symtab and line for the end of the
9971 range. This makes it possible to have ranges like "foo.c:27, +14",
9972 where +14 means 14 lines from the start location. */
9973 event_location_up end_location = string_to_event_location (&arg,
9974 current_language);
9975 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
9976 sal_start.symtab, sal_start.line,
9977 &canonical_end, NULL, NULL);
9978
9979 if (canonical_end.lsals.empty ())
9980 error (_("Could not find location of the end of the range."));
9981
9982 const linespec_sals &lsal_end = canonical_end.lsals[0];
9983 if (canonical_end.lsals.size () > 1
9984 || lsal_end.sals.size () != 1)
9985 error (_("Cannot create a ranged breakpoint with multiple locations."));
9986
9987 const symtab_and_line &sal_end = lsal_end.sals[0];
9988
9989 end = find_breakpoint_range_end (sal_end);
9990 if (sal_start.pc > end)
9991 error (_("Invalid address range, end precedes start."));
9992
9993 length = end - sal_start.pc + 1;
9994 if (length < 0)
9995 /* Length overflowed. */
9996 error (_("Address range too large."));
9997 else if (length == 1)
9998 {
9999 /* This range is simple enough to be handled by
10000 the `hbreak' command. */
10001 hbreak_command (&addr_string_start[0], 1);
10002
10003 return;
10004 }
10005
10006 /* Now set up the breakpoint. */
10007 b = set_raw_breakpoint (get_current_arch (), sal_start,
10008 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10009 set_breakpoint_count (breakpoint_count + 1);
10010 b->number = breakpoint_count;
10011 b->disposition = disp_donttouch;
10012 b->location = std::move (start_location);
10013 b->location_range_end = std::move (end_location);
10014 b->loc->length = length;
10015
10016 mention (b);
10017 gdb::observers::breakpoint_created.notify (b);
10018 update_global_location_list (UGLL_MAY_INSERT);
10019 }
10020
10021 /* Return non-zero if EXP is verified as constant. Returned zero
10022 means EXP is variable. Also the constant detection may fail for
10023 some constant expressions and in such case still falsely return
10024 zero. */
10025
10026 static bool
10027 watchpoint_exp_is_const (const struct expression *exp)
10028 {
10029 return exp->op->constant_p ();
10030 }
10031
10032 /* Watchpoint destructor. */
10033
10034 watchpoint::~watchpoint ()
10035 {
10036 xfree (this->exp_string);
10037 xfree (this->exp_string_reparse);
10038 }
10039
10040 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10041
10042 static void
10043 re_set_watchpoint (struct breakpoint *b)
10044 {
10045 struct watchpoint *w = (struct watchpoint *) b;
10046
10047 /* Watchpoint can be either on expression using entirely global
10048 variables, or it can be on local variables.
10049
10050 Watchpoints of the first kind are never auto-deleted, and even
10051 persist across program restarts. Since they can use variables
10052 from shared libraries, we need to reparse expression as libraries
10053 are loaded and unloaded.
10054
10055 Watchpoints on local variables can also change meaning as result
10056 of solib event. For example, if a watchpoint uses both a local
10057 and a global variables in expression, it's a local watchpoint,
10058 but unloading of a shared library will make the expression
10059 invalid. This is not a very common use case, but we still
10060 re-evaluate expression, to avoid surprises to the user.
10061
10062 Note that for local watchpoints, we re-evaluate it only if
10063 watchpoints frame id is still valid. If it's not, it means the
10064 watchpoint is out of scope and will be deleted soon. In fact,
10065 I'm not sure we'll ever be called in this case.
10066
10067 If a local watchpoint's frame id is still valid, then
10068 w->exp_valid_block is likewise valid, and we can safely use it.
10069
10070 Don't do anything about disabled watchpoints, since they will be
10071 reevaluated again when enabled. */
10072 update_watchpoint (w, 1 /* reparse */);
10073 }
10074
10075 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10076
10077 static int
10078 insert_watchpoint (struct bp_location *bl)
10079 {
10080 struct watchpoint *w = (struct watchpoint *) bl->owner;
10081 int length = w->exact ? 1 : bl->length;
10082
10083 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10084 w->cond_exp.get ());
10085 }
10086
10087 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10088
10089 static int
10090 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10091 {
10092 struct watchpoint *w = (struct watchpoint *) bl->owner;
10093 int length = w->exact ? 1 : bl->length;
10094
10095 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10096 w->cond_exp.get ());
10097 }
10098
10099 static int
10100 breakpoint_hit_watchpoint (const struct bp_location *bl,
10101 const address_space *aspace, CORE_ADDR bp_addr,
10102 const struct target_waitstatus *ws)
10103 {
10104 struct breakpoint *b = bl->owner;
10105 struct watchpoint *w = (struct watchpoint *) b;
10106
10107 /* Continuable hardware watchpoints are treated as non-existent if the
10108 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10109 some data address). Otherwise gdb won't stop on a break instruction
10110 in the code (not from a breakpoint) when a hardware watchpoint has
10111 been defined. Also skip watchpoints which we know did not trigger
10112 (did not match the data address). */
10113 if (is_hardware_watchpoint (b)
10114 && w->watchpoint_triggered == watch_triggered_no)
10115 return 0;
10116
10117 return 1;
10118 }
10119
10120 static void
10121 check_status_watchpoint (bpstat bs)
10122 {
10123 gdb_assert (is_watchpoint (bs->breakpoint_at));
10124
10125 bpstat_check_watchpoint (bs);
10126 }
10127
10128 /* Implement the "resources_needed" breakpoint_ops method for
10129 hardware watchpoints. */
10130
10131 static int
10132 resources_needed_watchpoint (const struct bp_location *bl)
10133 {
10134 struct watchpoint *w = (struct watchpoint *) bl->owner;
10135 int length = w->exact? 1 : bl->length;
10136
10137 return target_region_ok_for_hw_watchpoint (bl->address, length);
10138 }
10139
10140 /* Implement the "works_in_software_mode" breakpoint_ops method for
10141 hardware watchpoints. */
10142
10143 static int
10144 works_in_software_mode_watchpoint (const struct breakpoint *b)
10145 {
10146 /* Read and access watchpoints only work with hardware support. */
10147 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10148 }
10149
10150 static enum print_stop_action
10151 print_it_watchpoint (bpstat bs)
10152 {
10153 struct breakpoint *b;
10154 enum print_stop_action result;
10155 struct watchpoint *w;
10156 struct ui_out *uiout = current_uiout;
10157
10158 gdb_assert (bs->bp_location_at != NULL);
10159
10160 b = bs->breakpoint_at;
10161 w = (struct watchpoint *) b;
10162
10163 annotate_watchpoint (b->number);
10164 maybe_print_thread_hit_breakpoint (uiout);
10165
10166 string_file stb;
10167
10168 gdb::optional<ui_out_emit_tuple> tuple_emitter;
10169 switch (b->type)
10170 {
10171 case bp_watchpoint:
10172 case bp_hardware_watchpoint:
10173 if (uiout->is_mi_like_p ())
10174 uiout->field_string
10175 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10176 mention (b);
10177 tuple_emitter.emplace (uiout, "value");
10178 uiout->text ("\nOld value = ");
10179 watchpoint_value_print (bs->old_val.get (), &stb);
10180 uiout->field_stream ("old", stb);
10181 uiout->text ("\nNew value = ");
10182 watchpoint_value_print (w->val.get (), &stb);
10183 uiout->field_stream ("new", stb);
10184 uiout->text ("\n");
10185 /* More than one watchpoint may have been triggered. */
10186 result = PRINT_UNKNOWN;
10187 break;
10188
10189 case bp_read_watchpoint:
10190 if (uiout->is_mi_like_p ())
10191 uiout->field_string
10192 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10193 mention (b);
10194 tuple_emitter.emplace (uiout, "value");
10195 uiout->text ("\nValue = ");
10196 watchpoint_value_print (w->val.get (), &stb);
10197 uiout->field_stream ("value", stb);
10198 uiout->text ("\n");
10199 result = PRINT_UNKNOWN;
10200 break;
10201
10202 case bp_access_watchpoint:
10203 if (bs->old_val != NULL)
10204 {
10205 if (uiout->is_mi_like_p ())
10206 uiout->field_string
10207 ("reason",
10208 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10209 mention (b);
10210 tuple_emitter.emplace (uiout, "value");
10211 uiout->text ("\nOld value = ");
10212 watchpoint_value_print (bs->old_val.get (), &stb);
10213 uiout->field_stream ("old", stb);
10214 uiout->text ("\nNew value = ");
10215 }
10216 else
10217 {
10218 mention (b);
10219 if (uiout->is_mi_like_p ())
10220 uiout->field_string
10221 ("reason",
10222 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10223 tuple_emitter.emplace (uiout, "value");
10224 uiout->text ("\nValue = ");
10225 }
10226 watchpoint_value_print (w->val.get (), &stb);
10227 uiout->field_stream ("new", stb);
10228 uiout->text ("\n");
10229 result = PRINT_UNKNOWN;
10230 break;
10231 default:
10232 result = PRINT_UNKNOWN;
10233 }
10234
10235 return result;
10236 }
10237
10238 /* Implement the "print_mention" breakpoint_ops method for hardware
10239 watchpoints. */
10240
10241 static void
10242 print_mention_watchpoint (struct breakpoint *b)
10243 {
10244 struct watchpoint *w = (struct watchpoint *) b;
10245 struct ui_out *uiout = current_uiout;
10246 const char *tuple_name;
10247
10248 switch (b->type)
10249 {
10250 case bp_watchpoint:
10251 uiout->text ("Watchpoint ");
10252 tuple_name = "wpt";
10253 break;
10254 case bp_hardware_watchpoint:
10255 uiout->text ("Hardware watchpoint ");
10256 tuple_name = "wpt";
10257 break;
10258 case bp_read_watchpoint:
10259 uiout->text ("Hardware read watchpoint ");
10260 tuple_name = "hw-rwpt";
10261 break;
10262 case bp_access_watchpoint:
10263 uiout->text ("Hardware access (read/write) watchpoint ");
10264 tuple_name = "hw-awpt";
10265 break;
10266 default:
10267 internal_error (__FILE__, __LINE__,
10268 _("Invalid hardware watchpoint type."));
10269 }
10270
10271 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10272 uiout->field_signed ("number", b->number);
10273 uiout->text (": ");
10274 uiout->field_string ("exp", w->exp_string);
10275 }
10276
10277 /* Implement the "print_recreate" breakpoint_ops method for
10278 watchpoints. */
10279
10280 static void
10281 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10282 {
10283 struct watchpoint *w = (struct watchpoint *) b;
10284
10285 switch (b->type)
10286 {
10287 case bp_watchpoint:
10288 case bp_hardware_watchpoint:
10289 fprintf_unfiltered (fp, "watch");
10290 break;
10291 case bp_read_watchpoint:
10292 fprintf_unfiltered (fp, "rwatch");
10293 break;
10294 case bp_access_watchpoint:
10295 fprintf_unfiltered (fp, "awatch");
10296 break;
10297 default:
10298 internal_error (__FILE__, __LINE__,
10299 _("Invalid watchpoint type."));
10300 }
10301
10302 fprintf_unfiltered (fp, " %s", w->exp_string);
10303 print_recreate_thread (b, fp);
10304 }
10305
10306 /* Implement the "explains_signal" breakpoint_ops method for
10307 watchpoints. */
10308
10309 static int
10310 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10311 {
10312 /* A software watchpoint cannot cause a signal other than
10313 GDB_SIGNAL_TRAP. */
10314 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10315 return 0;
10316
10317 return 1;
10318 }
10319
10320 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10321
10322 static struct breakpoint_ops watchpoint_breakpoint_ops;
10323
10324 /* Implement the "insert" breakpoint_ops method for
10325 masked hardware watchpoints. */
10326
10327 static int
10328 insert_masked_watchpoint (struct bp_location *bl)
10329 {
10330 struct watchpoint *w = (struct watchpoint *) bl->owner;
10331
10332 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10333 bl->watchpoint_type);
10334 }
10335
10336 /* Implement the "remove" breakpoint_ops method for
10337 masked hardware watchpoints. */
10338
10339 static int
10340 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10341 {
10342 struct watchpoint *w = (struct watchpoint *) bl->owner;
10343
10344 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10345 bl->watchpoint_type);
10346 }
10347
10348 /* Implement the "resources_needed" breakpoint_ops method for
10349 masked hardware watchpoints. */
10350
10351 static int
10352 resources_needed_masked_watchpoint (const struct bp_location *bl)
10353 {
10354 struct watchpoint *w = (struct watchpoint *) bl->owner;
10355
10356 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10357 }
10358
10359 /* Implement the "works_in_software_mode" breakpoint_ops method for
10360 masked hardware watchpoints. */
10361
10362 static int
10363 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10364 {
10365 return 0;
10366 }
10367
10368 /* Implement the "print_it" breakpoint_ops method for
10369 masked hardware watchpoints. */
10370
10371 static enum print_stop_action
10372 print_it_masked_watchpoint (bpstat bs)
10373 {
10374 struct breakpoint *b = bs->breakpoint_at;
10375 struct ui_out *uiout = current_uiout;
10376
10377 /* Masked watchpoints have only one location. */
10378 gdb_assert (b->loc && b->loc->next == NULL);
10379
10380 annotate_watchpoint (b->number);
10381 maybe_print_thread_hit_breakpoint (uiout);
10382
10383 switch (b->type)
10384 {
10385 case bp_hardware_watchpoint:
10386 if (uiout->is_mi_like_p ())
10387 uiout->field_string
10388 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10389 break;
10390
10391 case bp_read_watchpoint:
10392 if (uiout->is_mi_like_p ())
10393 uiout->field_string
10394 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10395 break;
10396
10397 case bp_access_watchpoint:
10398 if (uiout->is_mi_like_p ())
10399 uiout->field_string
10400 ("reason",
10401 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10402 break;
10403 default:
10404 internal_error (__FILE__, __LINE__,
10405 _("Invalid hardware watchpoint type."));
10406 }
10407
10408 mention (b);
10409 uiout->text (_("\n\
10410 Check the underlying instruction at PC for the memory\n\
10411 address and value which triggered this watchpoint.\n"));
10412 uiout->text ("\n");
10413
10414 /* More than one watchpoint may have been triggered. */
10415 return PRINT_UNKNOWN;
10416 }
10417
10418 /* Implement the "print_one_detail" breakpoint_ops method for
10419 masked hardware watchpoints. */
10420
10421 static void
10422 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10423 struct ui_out *uiout)
10424 {
10425 struct watchpoint *w = (struct watchpoint *) b;
10426
10427 /* Masked watchpoints have only one location. */
10428 gdb_assert (b->loc && b->loc->next == NULL);
10429
10430 uiout->text ("\tmask ");
10431 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10432 uiout->text ("\n");
10433 }
10434
10435 /* Implement the "print_mention" breakpoint_ops method for
10436 masked hardware watchpoints. */
10437
10438 static void
10439 print_mention_masked_watchpoint (struct breakpoint *b)
10440 {
10441 struct watchpoint *w = (struct watchpoint *) b;
10442 struct ui_out *uiout = current_uiout;
10443 const char *tuple_name;
10444
10445 switch (b->type)
10446 {
10447 case bp_hardware_watchpoint:
10448 uiout->text ("Masked hardware watchpoint ");
10449 tuple_name = "wpt";
10450 break;
10451 case bp_read_watchpoint:
10452 uiout->text ("Masked hardware read watchpoint ");
10453 tuple_name = "hw-rwpt";
10454 break;
10455 case bp_access_watchpoint:
10456 uiout->text ("Masked hardware access (read/write) watchpoint ");
10457 tuple_name = "hw-awpt";
10458 break;
10459 default:
10460 internal_error (__FILE__, __LINE__,
10461 _("Invalid hardware watchpoint type."));
10462 }
10463
10464 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10465 uiout->field_signed ("number", b->number);
10466 uiout->text (": ");
10467 uiout->field_string ("exp", w->exp_string);
10468 }
10469
10470 /* Implement the "print_recreate" breakpoint_ops method for
10471 masked hardware watchpoints. */
10472
10473 static void
10474 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10475 {
10476 struct watchpoint *w = (struct watchpoint *) b;
10477
10478 switch (b->type)
10479 {
10480 case bp_hardware_watchpoint:
10481 fprintf_unfiltered (fp, "watch");
10482 break;
10483 case bp_read_watchpoint:
10484 fprintf_unfiltered (fp, "rwatch");
10485 break;
10486 case bp_access_watchpoint:
10487 fprintf_unfiltered (fp, "awatch");
10488 break;
10489 default:
10490 internal_error (__FILE__, __LINE__,
10491 _("Invalid hardware watchpoint type."));
10492 }
10493
10494 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string,
10495 phex (w->hw_wp_mask, sizeof (CORE_ADDR)));
10496 print_recreate_thread (b, fp);
10497 }
10498
10499 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10500
10501 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10502
10503 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10504
10505 static bool
10506 is_masked_watchpoint (const struct breakpoint *b)
10507 {
10508 return b->ops == &masked_watchpoint_breakpoint_ops;
10509 }
10510
10511 /* accessflag: hw_write: watch write,
10512 hw_read: watch read,
10513 hw_access: watch access (read or write) */
10514 static void
10515 watch_command_1 (const char *arg, int accessflag, int from_tty,
10516 bool just_location, bool internal)
10517 {
10518 struct breakpoint *scope_breakpoint = NULL;
10519 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10520 struct value *result;
10521 int saved_bitpos = 0, saved_bitsize = 0;
10522 const char *exp_start = NULL;
10523 const char *exp_end = NULL;
10524 const char *tok, *end_tok;
10525 int toklen = -1;
10526 const char *cond_start = NULL;
10527 const char *cond_end = NULL;
10528 enum bptype bp_type;
10529 int thread = -1;
10530 /* Flag to indicate whether we are going to use masks for
10531 the hardware watchpoint. */
10532 bool use_mask = false;
10533 CORE_ADDR mask = 0;
10534
10535 /* Make sure that we actually have parameters to parse. */
10536 if (arg != NULL && arg[0] != '\0')
10537 {
10538 const char *value_start;
10539
10540 exp_end = arg + strlen (arg);
10541
10542 /* Look for "parameter value" pairs at the end
10543 of the arguments string. */
10544 for (tok = exp_end - 1; tok > arg; tok--)
10545 {
10546 /* Skip whitespace at the end of the argument list. */
10547 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10548 tok--;
10549
10550 /* Find the beginning of the last token.
10551 This is the value of the parameter. */
10552 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10553 tok--;
10554 value_start = tok + 1;
10555
10556 /* Skip whitespace. */
10557 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10558 tok--;
10559
10560 end_tok = tok;
10561
10562 /* Find the beginning of the second to last token.
10563 This is the parameter itself. */
10564 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10565 tok--;
10566 tok++;
10567 toklen = end_tok - tok + 1;
10568
10569 if (toklen == 6 && startswith (tok, "thread"))
10570 {
10571 struct thread_info *thr;
10572 /* At this point we've found a "thread" token, which means
10573 the user is trying to set a watchpoint that triggers
10574 only in a specific thread. */
10575 const char *endp;
10576
10577 if (thread != -1)
10578 error(_("You can specify only one thread."));
10579
10580 /* Extract the thread ID from the next token. */
10581 thr = parse_thread_id (value_start, &endp);
10582
10583 /* Check if the user provided a valid thread ID. */
10584 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10585 invalid_thread_id_error (value_start);
10586
10587 thread = thr->global_num;
10588 }
10589 else if (toklen == 4 && startswith (tok, "mask"))
10590 {
10591 /* We've found a "mask" token, which means the user wants to
10592 create a hardware watchpoint that is going to have the mask
10593 facility. */
10594 struct value *mask_value, *mark;
10595
10596 if (use_mask)
10597 error(_("You can specify only one mask."));
10598
10599 use_mask = just_location = true;
10600
10601 mark = value_mark ();
10602 mask_value = parse_to_comma_and_eval (&value_start);
10603 mask = value_as_address (mask_value);
10604 value_free_to_mark (mark);
10605 }
10606 else
10607 /* We didn't recognize what we found. We should stop here. */
10608 break;
10609
10610 /* Truncate the string and get rid of the "parameter value" pair before
10611 the arguments string is parsed by the parse_exp_1 function. */
10612 exp_end = tok;
10613 }
10614 }
10615 else
10616 exp_end = arg;
10617
10618 /* Parse the rest of the arguments. From here on out, everything
10619 is in terms of a newly allocated string instead of the original
10620 ARG. */
10621 std::string expression (arg, exp_end - arg);
10622 exp_start = arg = expression.c_str ();
10623 innermost_block_tracker tracker;
10624 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10625 exp_end = arg;
10626 /* Remove trailing whitespace from the expression before saving it.
10627 This makes the eventual display of the expression string a bit
10628 prettier. */
10629 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10630 --exp_end;
10631
10632 /* Checking if the expression is not constant. */
10633 if (watchpoint_exp_is_const (exp.get ()))
10634 {
10635 int len;
10636
10637 len = exp_end - exp_start;
10638 while (len > 0 && isspace (exp_start[len - 1]))
10639 len--;
10640 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10641 }
10642
10643 exp_valid_block = tracker.block ();
10644 struct value *mark = value_mark ();
10645 struct value *val_as_value = nullptr;
10646 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10647 just_location);
10648
10649 if (val_as_value != NULL && just_location)
10650 {
10651 saved_bitpos = value_bitpos (val_as_value);
10652 saved_bitsize = value_bitsize (val_as_value);
10653 }
10654
10655 value_ref_ptr val;
10656 if (just_location)
10657 {
10658 int ret;
10659
10660 exp_valid_block = NULL;
10661 val = release_value (value_addr (result));
10662 value_free_to_mark (mark);
10663
10664 if (use_mask)
10665 {
10666 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10667 mask);
10668 if (ret == -1)
10669 error (_("This target does not support masked watchpoints."));
10670 else if (ret == -2)
10671 error (_("Invalid mask or memory region."));
10672 }
10673 }
10674 else if (val_as_value != NULL)
10675 val = release_value (val_as_value);
10676
10677 tok = skip_spaces (arg);
10678 end_tok = skip_to_space (tok);
10679
10680 toklen = end_tok - tok;
10681 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10682 {
10683 tok = cond_start = end_tok + 1;
10684 innermost_block_tracker if_tracker;
10685 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10686
10687 /* The watchpoint expression may not be local, but the condition
10688 may still be. E.g.: `watch global if local > 0'. */
10689 cond_exp_valid_block = if_tracker.block ();
10690
10691 cond_end = tok;
10692 }
10693 if (*tok)
10694 error (_("Junk at end of command."));
10695
10696 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10697
10698 /* Save this because create_internal_breakpoint below invalidates
10699 'wp_frame'. */
10700 frame_id watchpoint_frame = get_frame_id (wp_frame);
10701
10702 /* If the expression is "local", then set up a "watchpoint scope"
10703 breakpoint at the point where we've left the scope of the watchpoint
10704 expression. Create the scope breakpoint before the watchpoint, so
10705 that we will encounter it first in bpstat_stop_status. */
10706 if (exp_valid_block != NULL && wp_frame != NULL)
10707 {
10708 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10709
10710 if (frame_id_p (caller_frame_id))
10711 {
10712 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10713 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10714
10715 scope_breakpoint
10716 = create_internal_breakpoint (caller_arch, caller_pc,
10717 bp_watchpoint_scope,
10718 &momentary_breakpoint_ops);
10719
10720 /* create_internal_breakpoint could invalidate WP_FRAME. */
10721 wp_frame = NULL;
10722
10723 scope_breakpoint->enable_state = bp_enabled;
10724
10725 /* Automatically delete the breakpoint when it hits. */
10726 scope_breakpoint->disposition = disp_del;
10727
10728 /* Only break in the proper frame (help with recursion). */
10729 scope_breakpoint->frame_id = caller_frame_id;
10730
10731 /* Set the address at which we will stop. */
10732 scope_breakpoint->loc->gdbarch = caller_arch;
10733 scope_breakpoint->loc->requested_address = caller_pc;
10734 scope_breakpoint->loc->address
10735 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10736 scope_breakpoint->loc->requested_address,
10737 scope_breakpoint->type);
10738 }
10739 }
10740
10741 /* Now set up the breakpoint. We create all watchpoints as hardware
10742 watchpoints here even if hardware watchpoints are turned off, a call
10743 to update_watchpoint later in this function will cause the type to
10744 drop back to bp_watchpoint (software watchpoint) if required. */
10745
10746 if (accessflag == hw_read)
10747 bp_type = bp_read_watchpoint;
10748 else if (accessflag == hw_access)
10749 bp_type = bp_access_watchpoint;
10750 else
10751 bp_type = bp_hardware_watchpoint;
10752
10753 std::unique_ptr<watchpoint> w (new watchpoint ());
10754
10755 if (use_mask)
10756 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10757 &masked_watchpoint_breakpoint_ops);
10758 else
10759 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10760 &watchpoint_breakpoint_ops);
10761 w->thread = thread;
10762 w->disposition = disp_donttouch;
10763 w->pspace = current_program_space;
10764 w->exp = std::move (exp);
10765 w->exp_valid_block = exp_valid_block;
10766 w->cond_exp_valid_block = cond_exp_valid_block;
10767 if (just_location)
10768 {
10769 struct type *t = value_type (val.get ());
10770 CORE_ADDR addr = value_as_address (val.get ());
10771
10772 w->exp_string_reparse
10773 = current_language->watch_location_expression (t, addr).release ();
10774
10775 w->exp_string = xstrprintf ("-location %.*s",
10776 (int) (exp_end - exp_start), exp_start);
10777 }
10778 else
10779 w->exp_string = savestring (exp_start, exp_end - exp_start);
10780
10781 if (use_mask)
10782 {
10783 w->hw_wp_mask = mask;
10784 }
10785 else
10786 {
10787 w->val = val;
10788 w->val_bitpos = saved_bitpos;
10789 w->val_bitsize = saved_bitsize;
10790 w->val_valid = true;
10791 }
10792
10793 if (cond_start)
10794 w->cond_string = savestring (cond_start, cond_end - cond_start);
10795 else
10796 w->cond_string = 0;
10797
10798 if (frame_id_p (watchpoint_frame))
10799 {
10800 w->watchpoint_frame = watchpoint_frame;
10801 w->watchpoint_thread = inferior_ptid;
10802 }
10803 else
10804 {
10805 w->watchpoint_frame = null_frame_id;
10806 w->watchpoint_thread = null_ptid;
10807 }
10808
10809 if (scope_breakpoint != NULL)
10810 {
10811 /* The scope breakpoint is related to the watchpoint. We will
10812 need to act on them together. */
10813 w->related_breakpoint = scope_breakpoint;
10814 scope_breakpoint->related_breakpoint = w.get ();
10815 }
10816
10817 if (!just_location)
10818 value_free_to_mark (mark);
10819
10820 /* Finally update the new watchpoint. This creates the locations
10821 that should be inserted. */
10822 update_watchpoint (w.get (), 1);
10823
10824 install_breakpoint (internal, std::move (w), 1);
10825 }
10826
10827 /* Return count of debug registers needed to watch the given expression.
10828 If the watchpoint cannot be handled in hardware return zero. */
10829
10830 static int
10831 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10832 {
10833 int found_memory_cnt = 0;
10834
10835 /* Did the user specifically forbid us to use hardware watchpoints? */
10836 if (!can_use_hw_watchpoints)
10837 return 0;
10838
10839 gdb_assert (!vals.empty ());
10840 struct value *head = vals[0].get ();
10841
10842 /* Make sure that the value of the expression depends only upon
10843 memory contents, and values computed from them within GDB. If we
10844 find any register references or function calls, we can't use a
10845 hardware watchpoint.
10846
10847 The idea here is that evaluating an expression generates a series
10848 of values, one holding the value of every subexpression. (The
10849 expression a*b+c has five subexpressions: a, b, a*b, c, and
10850 a*b+c.) GDB's values hold almost enough information to establish
10851 the criteria given above --- they identify memory lvalues,
10852 register lvalues, computed values, etcetera. So we can evaluate
10853 the expression, and then scan the chain of values that leaves
10854 behind to decide whether we can detect any possible change to the
10855 expression's final value using only hardware watchpoints.
10856
10857 However, I don't think that the values returned by inferior
10858 function calls are special in any way. So this function may not
10859 notice that an expression involving an inferior function call
10860 can't be watched with hardware watchpoints. FIXME. */
10861 for (const value_ref_ptr &iter : vals)
10862 {
10863 struct value *v = iter.get ();
10864
10865 if (VALUE_LVAL (v) == lval_memory)
10866 {
10867 if (v != head && value_lazy (v))
10868 /* A lazy memory lvalue in the chain is one that GDB never
10869 needed to fetch; we either just used its address (e.g.,
10870 `a' in `a.b') or we never needed it at all (e.g., `a'
10871 in `a,b'). This doesn't apply to HEAD; if that is
10872 lazy then it was not readable, but watch it anyway. */
10873 ;
10874 else
10875 {
10876 /* Ahh, memory we actually used! Check if we can cover
10877 it with hardware watchpoints. */
10878 struct type *vtype = check_typedef (value_type (v));
10879
10880 /* We only watch structs and arrays if user asked for it
10881 explicitly, never if they just happen to appear in a
10882 middle of some value chain. */
10883 if (v == head
10884 || (vtype->code () != TYPE_CODE_STRUCT
10885 && vtype->code () != TYPE_CODE_ARRAY))
10886 {
10887 CORE_ADDR vaddr = value_address (v);
10888 int len;
10889 int num_regs;
10890
10891 len = (target_exact_watchpoints
10892 && is_scalar_type_recursive (vtype))?
10893 1 : TYPE_LENGTH (value_type (v));
10894
10895 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10896 if (!num_regs)
10897 return 0;
10898 else
10899 found_memory_cnt += num_regs;
10900 }
10901 }
10902 }
10903 else if (VALUE_LVAL (v) != not_lval
10904 && deprecated_value_modifiable (v) == 0)
10905 return 0; /* These are values from the history (e.g., $1). */
10906 else if (VALUE_LVAL (v) == lval_register)
10907 return 0; /* Cannot watch a register with a HW watchpoint. */
10908 }
10909
10910 /* The expression itself looks suitable for using a hardware
10911 watchpoint, but give the target machine a chance to reject it. */
10912 return found_memory_cnt;
10913 }
10914
10915 void
10916 watch_command_wrapper (const char *arg, int from_tty, bool internal)
10917 {
10918 watch_command_1 (arg, hw_write, from_tty, 0, internal);
10919 }
10920
10921 /* Options for the watch, awatch, and rwatch commands. */
10922
10923 struct watch_options
10924 {
10925 /* For -location. */
10926 bool location = false;
10927 };
10928
10929 /* Definitions of options for the "watch", "awatch", and "rwatch" commands.
10930
10931 Historically GDB always accepted both '-location' and '-l' flags for
10932 these commands (both flags being synonyms). When converting to the
10933 newer option scheme only '-location' is added here. That's fine (for
10934 backward compatibility) as any non-ambiguous prefix of a flag will be
10935 accepted, so '-l', '-loc', are now all accepted.
10936
10937 What this means is that, if in the future, we add any new flag here
10938 that starts with '-l' then this will break backward compatibility, so
10939 please, don't do that! */
10940
10941 static const gdb::option::option_def watch_option_defs[] = {
10942 gdb::option::flag_option_def<watch_options> {
10943 "location",
10944 [] (watch_options *opt) { return &opt->location; },
10945 N_("\
10946 This evaluates EXPRESSION and watches the memory to which is refers.\n\
10947 -l can be used as a short form of -location."),
10948 },
10949 };
10950
10951 /* Returns the option group used by 'watch', 'awatch', and 'rwatch'
10952 commands. */
10953
10954 static gdb::option::option_def_group
10955 make_watch_options_def_group (watch_options *opts)
10956 {
10957 return {{watch_option_defs}, opts};
10958 }
10959
10960 /* A helper function that looks for the "-location" argument and then
10961 calls watch_command_1. */
10962
10963 static void
10964 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
10965 {
10966 watch_options opts;
10967 auto grp = make_watch_options_def_group (&opts);
10968 gdb::option::process_options
10969 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
10970 if (arg != nullptr && *arg == '\0')
10971 arg = nullptr;
10972
10973 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
10974 }
10975
10976 /* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
10977 static void
10978 watch_command_completer (struct cmd_list_element *ignore,
10979 completion_tracker &tracker,
10980 const char *text, const char * /*word*/)
10981 {
10982 const auto group = make_watch_options_def_group (nullptr);
10983 if (gdb::option::complete_options
10984 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
10985 return;
10986
10987 const char *word = advance_to_expression_complete_word_point (tracker, text);
10988 expression_completer (ignore, tracker, text, word);
10989 }
10990
10991 static void
10992 watch_command (const char *arg, int from_tty)
10993 {
10994 watch_maybe_just_location (arg, hw_write, from_tty);
10995 }
10996
10997 void
10998 rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
10999 {
11000 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11001 }
11002
11003 static void
11004 rwatch_command (const char *arg, int from_tty)
11005 {
11006 watch_maybe_just_location (arg, hw_read, from_tty);
11007 }
11008
11009 void
11010 awatch_command_wrapper (const char *arg, int from_tty, bool internal)
11011 {
11012 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11013 }
11014
11015 static void
11016 awatch_command (const char *arg, int from_tty)
11017 {
11018 watch_maybe_just_location (arg, hw_access, from_tty);
11019 }
11020 \f
11021
11022 /* Data for the FSM that manages the until(location)/advance commands
11023 in infcmd.c. Here because it uses the mechanisms of
11024 breakpoints. */
11025
11026 struct until_break_fsm : public thread_fsm
11027 {
11028 /* The thread that was current when the command was executed. */
11029 int thread;
11030
11031 /* The breakpoint set at the return address in the caller frame,
11032 plus breakpoints at all the destination locations. */
11033 std::vector<breakpoint_up> breakpoints;
11034
11035 until_break_fsm (struct interp *cmd_interp, int thread,
11036 std::vector<breakpoint_up> &&breakpoints)
11037 : thread_fsm (cmd_interp),
11038 thread (thread),
11039 breakpoints (std::move (breakpoints))
11040 {
11041 }
11042
11043 void clean_up (struct thread_info *thread) override;
11044 bool should_stop (struct thread_info *thread) override;
11045 enum async_reply_reason do_async_reply_reason () override;
11046 };
11047
11048 /* Implementation of the 'should_stop' FSM method for the
11049 until(location)/advance commands. */
11050
11051 bool
11052 until_break_fsm::should_stop (struct thread_info *tp)
11053 {
11054 for (const breakpoint_up &bp : breakpoints)
11055 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11056 bp.get ()) != NULL)
11057 {
11058 set_finished ();
11059 break;
11060 }
11061
11062 return true;
11063 }
11064
11065 /* Implementation of the 'clean_up' FSM method for the
11066 until(location)/advance commands. */
11067
11068 void
11069 until_break_fsm::clean_up (struct thread_info *)
11070 {
11071 /* Clean up our temporary breakpoints. */
11072 breakpoints.clear ();
11073 delete_longjmp_breakpoint (thread);
11074 }
11075
11076 /* Implementation of the 'async_reply_reason' FSM method for the
11077 until(location)/advance commands. */
11078
11079 enum async_reply_reason
11080 until_break_fsm::do_async_reply_reason ()
11081 {
11082 return EXEC_ASYNC_LOCATION_REACHED;
11083 }
11084
11085 void
11086 until_break_command (const char *arg, int from_tty, int anywhere)
11087 {
11088 struct frame_info *frame;
11089 struct gdbarch *frame_gdbarch;
11090 struct frame_id stack_frame_id;
11091 struct frame_id caller_frame_id;
11092 int thread;
11093 struct thread_info *tp;
11094
11095 clear_proceed_status (0);
11096
11097 /* Set a breakpoint where the user wants it and at return from
11098 this function. */
11099
11100 event_location_up location = string_to_event_location (&arg, current_language);
11101
11102 std::vector<symtab_and_line> sals
11103 = (last_displayed_sal_is_valid ()
11104 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11105 get_last_displayed_symtab (),
11106 get_last_displayed_line ())
11107 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11108 NULL, NULL, 0));
11109
11110 if (sals.empty ())
11111 error (_("Couldn't get information on specified line."));
11112
11113 if (*arg)
11114 error (_("Junk at end of arguments."));
11115
11116 tp = inferior_thread ();
11117 thread = tp->global_num;
11118
11119 /* Note linespec handling above invalidates the frame chain.
11120 Installing a breakpoint also invalidates the frame chain (as it
11121 may need to switch threads), so do any frame handling before
11122 that. */
11123
11124 frame = get_selected_frame (NULL);
11125 frame_gdbarch = get_frame_arch (frame);
11126 stack_frame_id = get_stack_frame_id (frame);
11127 caller_frame_id = frame_unwind_caller_id (frame);
11128
11129 /* Keep within the current frame, or in frames called by the current
11130 one. */
11131
11132 std::vector<breakpoint_up> breakpoints;
11133
11134 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11135
11136 if (frame_id_p (caller_frame_id))
11137 {
11138 struct symtab_and_line sal2;
11139 struct gdbarch *caller_gdbarch;
11140
11141 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11142 sal2.pc = frame_unwind_caller_pc (frame);
11143 caller_gdbarch = frame_unwind_caller_arch (frame);
11144
11145 breakpoint_up caller_breakpoint
11146 = set_momentary_breakpoint (caller_gdbarch, sal2,
11147 caller_frame_id, bp_until);
11148 breakpoints.emplace_back (std::move (caller_breakpoint));
11149
11150 set_longjmp_breakpoint (tp, caller_frame_id);
11151 lj_deleter.emplace (thread);
11152 }
11153
11154 /* set_momentary_breakpoint could invalidate FRAME. */
11155 frame = NULL;
11156
11157 /* If the user told us to continue until a specified location, we
11158 don't specify a frame at which we need to stop. Otherwise,
11159 specify the selected frame, because we want to stop only at the
11160 very same frame. */
11161 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
11162
11163 for (symtab_and_line &sal : sals)
11164 {
11165 resolve_sal_pc (&sal);
11166
11167 breakpoint_up location_breakpoint
11168 = set_momentary_breakpoint (frame_gdbarch, sal,
11169 stop_frame_id, bp_until);
11170 breakpoints.emplace_back (std::move (location_breakpoint));
11171 }
11172
11173 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
11174 std::move (breakpoints));
11175
11176 if (lj_deleter)
11177 lj_deleter->release ();
11178
11179 proceed (-1, GDB_SIGNAL_DEFAULT);
11180 }
11181
11182 /* This function attempts to parse an optional "if <cond>" clause
11183 from the arg string. If one is not found, it returns NULL.
11184
11185 Else, it returns a pointer to the condition string. (It does not
11186 attempt to evaluate the string against a particular block.) And,
11187 it updates arg to point to the first character following the parsed
11188 if clause in the arg string. */
11189
11190 const char *
11191 ep_parse_optional_if_clause (const char **arg)
11192 {
11193 const char *cond_string;
11194
11195 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11196 return NULL;
11197
11198 /* Skip the "if" keyword. */
11199 (*arg) += 2;
11200
11201 /* Skip any extra leading whitespace, and record the start of the
11202 condition string. */
11203 *arg = skip_spaces (*arg);
11204 cond_string = *arg;
11205
11206 /* Assume that the condition occupies the remainder of the arg
11207 string. */
11208 (*arg) += strlen (cond_string);
11209
11210 return cond_string;
11211 }
11212
11213 /* Commands to deal with catching events, such as signals, exceptions,
11214 process start/exit, etc. */
11215
11216 typedef enum
11217 {
11218 catch_fork_temporary, catch_vfork_temporary,
11219 catch_fork_permanent, catch_vfork_permanent
11220 }
11221 catch_fork_kind;
11222
11223 static void
11224 catch_fork_command_1 (const char *arg, int from_tty,
11225 struct cmd_list_element *command)
11226 {
11227 struct gdbarch *gdbarch = get_current_arch ();
11228 const char *cond_string = NULL;
11229 catch_fork_kind fork_kind;
11230
11231 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11232 bool temp = (fork_kind == catch_fork_temporary
11233 || fork_kind == catch_vfork_temporary);
11234
11235 if (!arg)
11236 arg = "";
11237 arg = skip_spaces (arg);
11238
11239 /* The allowed syntax is:
11240 catch [v]fork
11241 catch [v]fork if <cond>
11242
11243 First, check if there's an if clause. */
11244 cond_string = ep_parse_optional_if_clause (&arg);
11245
11246 if ((*arg != '\0') && !isspace (*arg))
11247 error (_("Junk at end of arguments."));
11248
11249 /* If this target supports it, create a fork or vfork catchpoint
11250 and enable reporting of such events. */
11251 switch (fork_kind)
11252 {
11253 case catch_fork_temporary:
11254 case catch_fork_permanent:
11255 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
11256 &catch_fork_breakpoint_ops);
11257 break;
11258 case catch_vfork_temporary:
11259 case catch_vfork_permanent:
11260 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
11261 &catch_vfork_breakpoint_ops);
11262 break;
11263 default:
11264 error (_("unsupported or unknown fork kind; cannot catch it"));
11265 break;
11266 }
11267 }
11268
11269 static void
11270 catch_exec_command_1 (const char *arg, int from_tty,
11271 struct cmd_list_element *command)
11272 {
11273 struct gdbarch *gdbarch = get_current_arch ();
11274 const char *cond_string = NULL;
11275 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
11276
11277 if (!arg)
11278 arg = "";
11279 arg = skip_spaces (arg);
11280
11281 /* The allowed syntax is:
11282 catch exec
11283 catch exec if <cond>
11284
11285 First, check if there's an if clause. */
11286 cond_string = ep_parse_optional_if_clause (&arg);
11287
11288 if ((*arg != '\0') && !isspace (*arg))
11289 error (_("Junk at end of arguments."));
11290
11291 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11292 init_catchpoint (c.get (), gdbarch, temp, cond_string,
11293 &catch_exec_breakpoint_ops);
11294 c->exec_pathname = NULL;
11295
11296 install_breakpoint (0, std::move (c), 1);
11297 }
11298
11299 void
11300 init_ada_exception_breakpoint (struct breakpoint *b,
11301 struct gdbarch *gdbarch,
11302 struct symtab_and_line sal,
11303 const char *addr_string,
11304 const struct breakpoint_ops *ops,
11305 int tempflag,
11306 int enabled,
11307 int from_tty)
11308 {
11309 if (from_tty)
11310 {
11311 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11312 if (!loc_gdbarch)
11313 loc_gdbarch = gdbarch;
11314
11315 describe_other_breakpoints (loc_gdbarch,
11316 sal.pspace, sal.pc, sal.section, -1);
11317 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11318 version for exception catchpoints, because two catchpoints
11319 used for different exception names will use the same address.
11320 In this case, a "breakpoint ... also set at..." warning is
11321 unproductive. Besides, the warning phrasing is also a bit
11322 inappropriate, we should use the word catchpoint, and tell
11323 the user what type of catchpoint it is. The above is good
11324 enough for now, though. */
11325 }
11326
11327 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
11328
11329 b->enable_state = enabled ? bp_enabled : bp_disabled;
11330 b->disposition = tempflag ? disp_del : disp_donttouch;
11331 b->location = string_to_event_location (&addr_string,
11332 language_def (language_ada));
11333 b->language = language_ada;
11334 }
11335
11336 \f
11337
11338 /* Compare two breakpoints and return a strcmp-like result. */
11339
11340 static int
11341 compare_breakpoints (const breakpoint *a, const breakpoint *b)
11342 {
11343 uintptr_t ua = (uintptr_t) a;
11344 uintptr_t ub = (uintptr_t) b;
11345
11346 if (a->number < b->number)
11347 return -1;
11348 else if (a->number > b->number)
11349 return 1;
11350
11351 /* Now sort by address, in case we see, e..g, two breakpoints with
11352 the number 0. */
11353 if (ua < ub)
11354 return -1;
11355 return ua > ub ? 1 : 0;
11356 }
11357
11358 /* Delete breakpoints by address or line. */
11359
11360 static void
11361 clear_command (const char *arg, int from_tty)
11362 {
11363 int default_match;
11364
11365 std::vector<symtab_and_line> decoded_sals;
11366 symtab_and_line last_sal;
11367 gdb::array_view<symtab_and_line> sals;
11368 if (arg)
11369 {
11370 decoded_sals
11371 = decode_line_with_current_source (arg,
11372 (DECODE_LINE_FUNFIRSTLINE
11373 | DECODE_LINE_LIST_MODE));
11374 default_match = 0;
11375 sals = decoded_sals;
11376 }
11377 else
11378 {
11379 /* Set sal's line, symtab, pc, and pspace to the values
11380 corresponding to the last call to print_frame_info. If the
11381 codepoint is not valid, this will set all the fields to 0. */
11382 last_sal = get_last_displayed_sal ();
11383 if (last_sal.symtab == 0)
11384 error (_("No source file specified."));
11385
11386 default_match = 1;
11387 sals = last_sal;
11388 }
11389
11390 /* We don't call resolve_sal_pc here. That's not as bad as it
11391 seems, because all existing breakpoints typically have both
11392 file/line and pc set. So, if clear is given file/line, we can
11393 match this to existing breakpoint without obtaining pc at all.
11394
11395 We only support clearing given the address explicitly
11396 present in breakpoint table. Say, we've set breakpoint
11397 at file:line. There were several PC values for that file:line,
11398 due to optimization, all in one block.
11399
11400 We've picked one PC value. If "clear" is issued with another
11401 PC corresponding to the same file:line, the breakpoint won't
11402 be cleared. We probably can still clear the breakpoint, but
11403 since the other PC value is never presented to user, user
11404 can only find it by guessing, and it does not seem important
11405 to support that. */
11406
11407 /* For each line spec given, delete bps which correspond to it. Do
11408 it in two passes, solely to preserve the current behavior that
11409 from_tty is forced true if we delete more than one
11410 breakpoint. */
11411
11412 std::vector<struct breakpoint *> found;
11413 for (const auto &sal : sals)
11414 {
11415 const char *sal_fullname;
11416
11417 /* If exact pc given, clear bpts at that pc.
11418 If line given (pc == 0), clear all bpts on specified line.
11419 If defaulting, clear all bpts on default line
11420 or at default pc.
11421
11422 defaulting sal.pc != 0 tests to do
11423
11424 0 1 pc
11425 1 1 pc _and_ line
11426 0 0 line
11427 1 0 <can't happen> */
11428
11429 sal_fullname = (sal.symtab == NULL
11430 ? NULL : symtab_to_fullname (sal.symtab));
11431
11432 /* Find all matching breakpoints and add them to 'found'. */
11433 for (breakpoint *b : all_breakpoints ())
11434 {
11435 int match = 0;
11436 /* Are we going to delete b? */
11437 if (b->type != bp_none && !is_watchpoint (b))
11438 {
11439 for (bp_location *loc : b->locations ())
11440 {
11441 /* If the user specified file:line, don't allow a PC
11442 match. This matches historical gdb behavior. */
11443 int pc_match = (!sal.explicit_line
11444 && sal.pc
11445 && (loc->pspace == sal.pspace)
11446 && (loc->address == sal.pc)
11447 && (!section_is_overlay (loc->section)
11448 || loc->section == sal.section));
11449 int line_match = 0;
11450
11451 if ((default_match || sal.explicit_line)
11452 && loc->symtab != NULL
11453 && sal_fullname != NULL
11454 && sal.pspace == loc->pspace
11455 && loc->line_number == sal.line
11456 && filename_cmp (symtab_to_fullname (loc->symtab),
11457 sal_fullname) == 0)
11458 line_match = 1;
11459
11460 if (pc_match || line_match)
11461 {
11462 match = 1;
11463 break;
11464 }
11465 }
11466 }
11467
11468 if (match)
11469 found.push_back (b);
11470 }
11471 }
11472
11473 /* Now go thru the 'found' chain and delete them. */
11474 if (found.empty ())
11475 {
11476 if (arg)
11477 error (_("No breakpoint at %s."), arg);
11478 else
11479 error (_("No breakpoint at this line."));
11480 }
11481
11482 /* Remove duplicates from the vec. */
11483 std::sort (found.begin (), found.end (),
11484 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11485 {
11486 return compare_breakpoints (bp_a, bp_b) < 0;
11487 });
11488 found.erase (std::unique (found.begin (), found.end (),
11489 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11490 {
11491 return compare_breakpoints (bp_a, bp_b) == 0;
11492 }),
11493 found.end ());
11494
11495 if (found.size () > 1)
11496 from_tty = 1; /* Always report if deleted more than one. */
11497 if (from_tty)
11498 {
11499 if (found.size () == 1)
11500 printf_unfiltered (_("Deleted breakpoint "));
11501 else
11502 printf_unfiltered (_("Deleted breakpoints "));
11503 }
11504
11505 for (breakpoint *iter : found)
11506 {
11507 if (from_tty)
11508 printf_unfiltered ("%d ", iter->number);
11509 delete_breakpoint (iter);
11510 }
11511 if (from_tty)
11512 putchar_unfiltered ('\n');
11513 }
11514 \f
11515 /* Delete breakpoint in BS if they are `delete' breakpoints and
11516 all breakpoints that are marked for deletion, whether hit or not.
11517 This is called after any breakpoint is hit, or after errors. */
11518
11519 void
11520 breakpoint_auto_delete (bpstat bs)
11521 {
11522 for (; bs; bs = bs->next)
11523 if (bs->breakpoint_at
11524 && bs->breakpoint_at->disposition == disp_del
11525 && bs->stop)
11526 delete_breakpoint (bs->breakpoint_at);
11527
11528 for (breakpoint *b : all_breakpoints_safe ())
11529 if (b->disposition == disp_del_at_next_stop)
11530 delete_breakpoint (b);
11531 }
11532
11533 /* A comparison function for bp_location AP and BP being interfaced to
11534 std::sort. Sort elements primarily by their ADDRESS (no matter what
11535 bl_address_is_meaningful says), secondarily by ordering first
11536 permanent elements and terciarily just ensuring the array is sorted
11537 stable way despite std::sort being an unstable algorithm. */
11538
11539 static int
11540 bp_location_is_less_than (const bp_location *a, const bp_location *b)
11541 {
11542 if (a->address != b->address)
11543 return a->address < b->address;
11544
11545 /* Sort locations at the same address by their pspace number, keeping
11546 locations of the same inferior (in a multi-inferior environment)
11547 grouped. */
11548
11549 if (a->pspace->num != b->pspace->num)
11550 return a->pspace->num < b->pspace->num;
11551
11552 /* Sort permanent breakpoints first. */
11553 if (a->permanent != b->permanent)
11554 return a->permanent > b->permanent;
11555
11556 /* Sort by type in order to make duplicate determination easier.
11557 See update_global_location_list. This is kept in sync with
11558 breakpoint_locations_match. */
11559 if (a->loc_type < b->loc_type)
11560 return true;
11561
11562 /* Likewise, for range-breakpoints, sort by length. */
11563 if (a->loc_type == bp_loc_hardware_breakpoint
11564 && b->loc_type == bp_loc_hardware_breakpoint
11565 && a->length < b->length)
11566 return true;
11567
11568 /* Make the internal GDB representation stable across GDB runs
11569 where A and B memory inside GDB can differ. Breakpoint locations of
11570 the same type at the same address can be sorted in arbitrary order. */
11571
11572 if (a->owner->number != b->owner->number)
11573 return a->owner->number < b->owner->number;
11574
11575 return a < b;
11576 }
11577
11578 /* Set bp_locations_placed_address_before_address_max and
11579 bp_locations_shadow_len_after_address_max according to the current
11580 content of the bp_locations array. */
11581
11582 static void
11583 bp_locations_target_extensions_update (void)
11584 {
11585 bp_locations_placed_address_before_address_max = 0;
11586 bp_locations_shadow_len_after_address_max = 0;
11587
11588 for (bp_location *bl : all_bp_locations ())
11589 {
11590 CORE_ADDR start, end, addr;
11591
11592 if (!bp_location_has_shadow (bl))
11593 continue;
11594
11595 start = bl->target_info.placed_address;
11596 end = start + bl->target_info.shadow_len;
11597
11598 gdb_assert (bl->address >= start);
11599 addr = bl->address - start;
11600 if (addr > bp_locations_placed_address_before_address_max)
11601 bp_locations_placed_address_before_address_max = addr;
11602
11603 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11604
11605 gdb_assert (bl->address < end);
11606 addr = end - bl->address;
11607 if (addr > bp_locations_shadow_len_after_address_max)
11608 bp_locations_shadow_len_after_address_max = addr;
11609 }
11610 }
11611
11612 /* Download tracepoint locations if they haven't been. */
11613
11614 static void
11615 download_tracepoint_locations (void)
11616 {
11617 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11618
11619 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11620
11621 for (breakpoint *b : all_tracepoints ())
11622 {
11623 struct tracepoint *t;
11624 int bp_location_downloaded = 0;
11625
11626 if ((b->type == bp_fast_tracepoint
11627 ? !may_insert_fast_tracepoints
11628 : !may_insert_tracepoints))
11629 continue;
11630
11631 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11632 {
11633 if (target_can_download_tracepoint ())
11634 can_download_tracepoint = TRIBOOL_TRUE;
11635 else
11636 can_download_tracepoint = TRIBOOL_FALSE;
11637 }
11638
11639 if (can_download_tracepoint == TRIBOOL_FALSE)
11640 break;
11641
11642 for (bp_location *bl : b->locations ())
11643 {
11644 /* In tracepoint, locations are _never_ duplicated, so
11645 should_be_inserted is equivalent to
11646 unduplicated_should_be_inserted. */
11647 if (!should_be_inserted (bl) || bl->inserted)
11648 continue;
11649
11650 switch_to_program_space_and_thread (bl->pspace);
11651
11652 target_download_tracepoint (bl);
11653
11654 bl->inserted = 1;
11655 bp_location_downloaded = 1;
11656 }
11657 t = (struct tracepoint *) b;
11658 t->number_on_target = b->number;
11659 if (bp_location_downloaded)
11660 gdb::observers::breakpoint_modified.notify (b);
11661 }
11662 }
11663
11664 /* Swap the insertion/duplication state between two locations. */
11665
11666 static void
11667 swap_insertion (struct bp_location *left, struct bp_location *right)
11668 {
11669 const int left_inserted = left->inserted;
11670 const int left_duplicate = left->duplicate;
11671 const int left_needs_update = left->needs_update;
11672 const struct bp_target_info left_target_info = left->target_info;
11673
11674 /* Locations of tracepoints can never be duplicated. */
11675 if (is_tracepoint (left->owner))
11676 gdb_assert (!left->duplicate);
11677 if (is_tracepoint (right->owner))
11678 gdb_assert (!right->duplicate);
11679
11680 left->inserted = right->inserted;
11681 left->duplicate = right->duplicate;
11682 left->needs_update = right->needs_update;
11683 left->target_info = right->target_info;
11684 right->inserted = left_inserted;
11685 right->duplicate = left_duplicate;
11686 right->needs_update = left_needs_update;
11687 right->target_info = left_target_info;
11688 }
11689
11690 /* Force the re-insertion of the locations at ADDRESS. This is called
11691 once a new/deleted/modified duplicate location is found and we are evaluating
11692 conditions on the target's side. Such conditions need to be updated on
11693 the target. */
11694
11695 static void
11696 force_breakpoint_reinsertion (struct bp_location *bl)
11697 {
11698 CORE_ADDR address = 0;
11699 int pspace_num;
11700
11701 address = bl->address;
11702 pspace_num = bl->pspace->num;
11703
11704 /* This is only meaningful if the target is
11705 evaluating conditions and if the user has
11706 opted for condition evaluation on the target's
11707 side. */
11708 if (gdb_evaluates_breakpoint_condition_p ()
11709 || !target_supports_evaluation_of_breakpoint_conditions ())
11710 return;
11711
11712 /* Flag all breakpoint locations with this address and
11713 the same program space as the location
11714 as "its condition has changed". We need to
11715 update the conditions on the target's side. */
11716 for (bp_location *loc : all_bp_locations_at_addr (address))
11717 {
11718 if (!is_breakpoint (loc->owner)
11719 || pspace_num != loc->pspace->num)
11720 continue;
11721
11722 /* Flag the location appropriately. We use a different state to
11723 let everyone know that we already updated the set of locations
11724 with addr bl->address and program space bl->pspace. This is so
11725 we don't have to keep calling these functions just to mark locations
11726 that have already been marked. */
11727 loc->condition_changed = condition_updated;
11728
11729 /* Free the agent expression bytecode as well. We will compute
11730 it later on. */
11731 loc->cond_bytecode.reset ();
11732 }
11733 }
11734
11735 /* Called whether new breakpoints are created, or existing breakpoints
11736 deleted, to update the global location list and recompute which
11737 locations are duplicate of which.
11738
11739 The INSERT_MODE flag determines whether locations may not, may, or
11740 shall be inserted now. See 'enum ugll_insert_mode' for more
11741 info. */
11742
11743 static void
11744 update_global_location_list (enum ugll_insert_mode insert_mode)
11745 {
11746 /* Last breakpoint location address that was marked for update. */
11747 CORE_ADDR last_addr = 0;
11748 /* Last breakpoint location program space that was marked for update. */
11749 int last_pspace_num = -1;
11750
11751 /* Used in the duplicates detection below. When iterating over all
11752 bp_locations, points to the first bp_location of a given address.
11753 Breakpoints and watchpoints of different types are never
11754 duplicates of each other. Keep one pointer for each type of
11755 breakpoint/watchpoint, so we only need to loop over all locations
11756 once. */
11757 struct bp_location *bp_loc_first; /* breakpoint */
11758 struct bp_location *wp_loc_first; /* hardware watchpoint */
11759 struct bp_location *awp_loc_first; /* access watchpoint */
11760 struct bp_location *rwp_loc_first; /* read watchpoint */
11761
11762 /* Saved former bp_locations array which we compare against the newly
11763 built bp_locations from the current state of ALL_BREAKPOINTS. */
11764 std::vector<bp_location *> old_locations = std::move (bp_locations);
11765 bp_locations.clear ();
11766
11767 for (breakpoint *b : all_breakpoints ())
11768 for (bp_location *loc : b->locations ())
11769 bp_locations.push_back (loc);
11770
11771 /* See if we need to "upgrade" a software breakpoint to a hardware
11772 breakpoint. Do this before deciding whether locations are
11773 duplicates. Also do this before sorting because sorting order
11774 depends on location type. */
11775 for (bp_location *loc : bp_locations)
11776 if (!loc->inserted && should_be_inserted (loc))
11777 handle_automatic_hardware_breakpoints (loc);
11778
11779 std::sort (bp_locations.begin (), bp_locations.end (),
11780 bp_location_is_less_than);
11781
11782 bp_locations_target_extensions_update ();
11783
11784 /* Identify bp_location instances that are no longer present in the
11785 new list, and therefore should be freed. Note that it's not
11786 necessary that those locations should be removed from inferior --
11787 if there's another location at the same address (previously
11788 marked as duplicate), we don't need to remove/insert the
11789 location.
11790
11791 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11792 and former bp_location array state respectively. */
11793
11794 size_t loc_i = 0;
11795 for (bp_location *old_loc : old_locations)
11796 {
11797 /* Tells if 'old_loc' is found among the new locations. If
11798 not, we have to free it. */
11799 int found_object = 0;
11800 /* Tells if the location should remain inserted in the target. */
11801 int keep_in_target = 0;
11802 int removed = 0;
11803
11804 /* Skip LOCP entries which will definitely never be needed.
11805 Stop either at or being the one matching OLD_LOC. */
11806 while (loc_i < bp_locations.size ()
11807 && bp_locations[loc_i]->address < old_loc->address)
11808 loc_i++;
11809
11810 for (size_t loc2_i = loc_i;
11811 (loc2_i < bp_locations.size ()
11812 && bp_locations[loc2_i]->address == old_loc->address);
11813 loc2_i++)
11814 {
11815 /* Check if this is a new/duplicated location or a duplicated
11816 location that had its condition modified. If so, we want to send
11817 its condition to the target if evaluation of conditions is taking
11818 place there. */
11819 if (bp_locations[loc2_i]->condition_changed == condition_modified
11820 && (last_addr != old_loc->address
11821 || last_pspace_num != old_loc->pspace->num))
11822 {
11823 force_breakpoint_reinsertion (bp_locations[loc2_i]);
11824 last_pspace_num = old_loc->pspace->num;
11825 }
11826
11827 if (bp_locations[loc2_i] == old_loc)
11828 found_object = 1;
11829 }
11830
11831 /* We have already handled this address, update it so that we don't
11832 have to go through updates again. */
11833 last_addr = old_loc->address;
11834
11835 /* Target-side condition evaluation: Handle deleted locations. */
11836 if (!found_object)
11837 force_breakpoint_reinsertion (old_loc);
11838
11839 /* If this location is no longer present, and inserted, look if
11840 there's maybe a new location at the same address. If so,
11841 mark that one inserted, and don't remove this one. This is
11842 needed so that we don't have a time window where a breakpoint
11843 at certain location is not inserted. */
11844
11845 if (old_loc->inserted)
11846 {
11847 /* If the location is inserted now, we might have to remove
11848 it. */
11849
11850 if (found_object && should_be_inserted (old_loc))
11851 {
11852 /* The location is still present in the location list,
11853 and still should be inserted. Don't do anything. */
11854 keep_in_target = 1;
11855 }
11856 else
11857 {
11858 /* This location still exists, but it won't be kept in the
11859 target since it may have been disabled. We proceed to
11860 remove its target-side condition. */
11861
11862 /* The location is either no longer present, or got
11863 disabled. See if there's another location at the
11864 same address, in which case we don't need to remove
11865 this one from the target. */
11866
11867 /* OLD_LOC comes from existing struct breakpoint. */
11868 if (bl_address_is_meaningful (old_loc))
11869 {
11870 for (size_t loc2_i = loc_i;
11871 (loc2_i < bp_locations.size ()
11872 && bp_locations[loc2_i]->address == old_loc->address);
11873 loc2_i++)
11874 {
11875 bp_location *loc2 = bp_locations[loc2_i];
11876
11877 if (loc2 == old_loc)
11878 continue;
11879
11880 if (breakpoint_locations_match (loc2, old_loc))
11881 {
11882 /* Read watchpoint locations are switched to
11883 access watchpoints, if the former are not
11884 supported, but the latter are. */
11885 if (is_hardware_watchpoint (old_loc->owner))
11886 {
11887 gdb_assert (is_hardware_watchpoint (loc2->owner));
11888 loc2->watchpoint_type = old_loc->watchpoint_type;
11889 }
11890
11891 /* loc2 is a duplicated location. We need to check
11892 if it should be inserted in case it will be
11893 unduplicated. */
11894 if (unduplicated_should_be_inserted (loc2))
11895 {
11896 swap_insertion (old_loc, loc2);
11897 keep_in_target = 1;
11898 break;
11899 }
11900 }
11901 }
11902 }
11903 }
11904
11905 if (!keep_in_target)
11906 {
11907 if (remove_breakpoint (old_loc))
11908 {
11909 /* This is just about all we can do. We could keep
11910 this location on the global list, and try to
11911 remove it next time, but there's no particular
11912 reason why we will succeed next time.
11913
11914 Note that at this point, old_loc->owner is still
11915 valid, as delete_breakpoint frees the breakpoint
11916 only after calling us. */
11917 printf_filtered (_("warning: Error removing "
11918 "breakpoint %d\n"),
11919 old_loc->owner->number);
11920 }
11921 removed = 1;
11922 }
11923 }
11924
11925 if (!found_object)
11926 {
11927 if (removed && target_is_non_stop_p ()
11928 && need_moribund_for_location_type (old_loc))
11929 {
11930 /* This location was removed from the target. In
11931 non-stop mode, a race condition is possible where
11932 we've removed a breakpoint, but stop events for that
11933 breakpoint are already queued and will arrive later.
11934 We apply an heuristic to be able to distinguish such
11935 SIGTRAPs from other random SIGTRAPs: we keep this
11936 breakpoint location for a bit, and will retire it
11937 after we see some number of events. The theory here
11938 is that reporting of events should, "on the average",
11939 be fair, so after a while we'll see events from all
11940 threads that have anything of interest, and no longer
11941 need to keep this breakpoint location around. We
11942 don't hold locations forever so to reduce chances of
11943 mistaking a non-breakpoint SIGTRAP for a breakpoint
11944 SIGTRAP.
11945
11946 The heuristic failing can be disastrous on
11947 decr_pc_after_break targets.
11948
11949 On decr_pc_after_break targets, like e.g., x86-linux,
11950 if we fail to recognize a late breakpoint SIGTRAP,
11951 because events_till_retirement has reached 0 too
11952 soon, we'll fail to do the PC adjustment, and report
11953 a random SIGTRAP to the user. When the user resumes
11954 the inferior, it will most likely immediately crash
11955 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
11956 corrupted, because of being resumed e.g., in the
11957 middle of a multi-byte instruction, or skipped a
11958 one-byte instruction. This was actually seen happen
11959 on native x86-linux, and should be less rare on
11960 targets that do not support new thread events, like
11961 remote, due to the heuristic depending on
11962 thread_count.
11963
11964 Mistaking a random SIGTRAP for a breakpoint trap
11965 causes similar symptoms (PC adjustment applied when
11966 it shouldn't), but then again, playing with SIGTRAPs
11967 behind the debugger's back is asking for trouble.
11968
11969 Since hardware watchpoint traps are always
11970 distinguishable from other traps, so we don't need to
11971 apply keep hardware watchpoint moribund locations
11972 around. We simply always ignore hardware watchpoint
11973 traps we can no longer explain. */
11974
11975 process_stratum_target *proc_target = nullptr;
11976 for (inferior *inf : all_inferiors ())
11977 if (inf->pspace == old_loc->pspace)
11978 {
11979 proc_target = inf->process_target ();
11980 break;
11981 }
11982 if (proc_target != nullptr)
11983 old_loc->events_till_retirement
11984 = 3 * (thread_count (proc_target) + 1);
11985 else
11986 old_loc->events_till_retirement = 1;
11987 old_loc->owner = NULL;
11988
11989 moribund_locations.push_back (old_loc);
11990 }
11991 else
11992 {
11993 old_loc->owner = NULL;
11994 decref_bp_location (&old_loc);
11995 }
11996 }
11997 }
11998
11999 /* Rescan breakpoints at the same address and section, marking the
12000 first one as "first" and any others as "duplicates". This is so
12001 that the bpt instruction is only inserted once. If we have a
12002 permanent breakpoint at the same place as BPT, make that one the
12003 official one, and the rest as duplicates. Permanent breakpoints
12004 are sorted first for the same address.
12005
12006 Do the same for hardware watchpoints, but also considering the
12007 watchpoint's type (regular/access/read) and length. */
12008
12009 bp_loc_first = NULL;
12010 wp_loc_first = NULL;
12011 awp_loc_first = NULL;
12012 rwp_loc_first = NULL;
12013
12014 for (bp_location *loc : all_bp_locations ())
12015 {
12016 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12017 non-NULL. */
12018 struct bp_location **loc_first_p;
12019 breakpoint *b = loc->owner;
12020
12021 if (!unduplicated_should_be_inserted (loc)
12022 || !bl_address_is_meaningful (loc)
12023 /* Don't detect duplicate for tracepoint locations because they are
12024 never duplicated. See the comments in field `duplicate' of
12025 `struct bp_location'. */
12026 || is_tracepoint (b))
12027 {
12028 /* Clear the condition modification flag. */
12029 loc->condition_changed = condition_unchanged;
12030 continue;
12031 }
12032
12033 if (b->type == bp_hardware_watchpoint)
12034 loc_first_p = &wp_loc_first;
12035 else if (b->type == bp_read_watchpoint)
12036 loc_first_p = &rwp_loc_first;
12037 else if (b->type == bp_access_watchpoint)
12038 loc_first_p = &awp_loc_first;
12039 else
12040 loc_first_p = &bp_loc_first;
12041
12042 if (*loc_first_p == NULL
12043 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12044 || !breakpoint_locations_match (loc, *loc_first_p))
12045 {
12046 *loc_first_p = loc;
12047 loc->duplicate = 0;
12048
12049 if (is_breakpoint (loc->owner) && loc->condition_changed)
12050 {
12051 loc->needs_update = 1;
12052 /* Clear the condition modification flag. */
12053 loc->condition_changed = condition_unchanged;
12054 }
12055 continue;
12056 }
12057
12058
12059 /* This and the above ensure the invariant that the first location
12060 is not duplicated, and is the inserted one.
12061 All following are marked as duplicated, and are not inserted. */
12062 if (loc->inserted)
12063 swap_insertion (loc, *loc_first_p);
12064 loc->duplicate = 1;
12065
12066 /* Clear the condition modification flag. */
12067 loc->condition_changed = condition_unchanged;
12068 }
12069
12070 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12071 {
12072 if (insert_mode != UGLL_DONT_INSERT)
12073 insert_breakpoint_locations ();
12074 else
12075 {
12076 /* Even though the caller told us to not insert new
12077 locations, we may still need to update conditions on the
12078 target's side of breakpoints that were already inserted
12079 if the target is evaluating breakpoint conditions. We
12080 only update conditions for locations that are marked
12081 "needs_update". */
12082 update_inserted_breakpoint_locations ();
12083 }
12084 }
12085
12086 if (insert_mode != UGLL_DONT_INSERT)
12087 download_tracepoint_locations ();
12088 }
12089
12090 void
12091 breakpoint_retire_moribund (void)
12092 {
12093 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12094 {
12095 struct bp_location *loc = moribund_locations[ix];
12096 if (--(loc->events_till_retirement) == 0)
12097 {
12098 decref_bp_location (&loc);
12099 unordered_remove (moribund_locations, ix);
12100 --ix;
12101 }
12102 }
12103 }
12104
12105 static void
12106 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12107 {
12108
12109 try
12110 {
12111 update_global_location_list (insert_mode);
12112 }
12113 catch (const gdb_exception_error &e)
12114 {
12115 }
12116 }
12117
12118 /* Clear BKP from a BPS. */
12119
12120 static void
12121 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12122 {
12123 bpstat bs;
12124
12125 for (bs = bps; bs; bs = bs->next)
12126 if (bs->breakpoint_at == bpt)
12127 {
12128 bs->breakpoint_at = NULL;
12129 bs->old_val = NULL;
12130 /* bs->commands will be freed later. */
12131 }
12132 }
12133
12134 /* Callback for iterate_over_threads. */
12135 static int
12136 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12137 {
12138 struct breakpoint *bpt = (struct breakpoint *) data;
12139
12140 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12141 return 0;
12142 }
12143
12144 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12145 callbacks. */
12146
12147 static void
12148 say_where (struct breakpoint *b)
12149 {
12150 struct value_print_options opts;
12151
12152 get_user_print_options (&opts);
12153
12154 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12155 single string. */
12156 if (b->loc == NULL)
12157 {
12158 /* For pending locations, the output differs slightly based
12159 on b->extra_string. If this is non-NULL, it contains either
12160 a condition or dprintf arguments. */
12161 if (b->extra_string == NULL)
12162 {
12163 printf_filtered (_(" (%s) pending."),
12164 event_location_to_string (b->location.get ()));
12165 }
12166 else if (b->type == bp_dprintf)
12167 {
12168 printf_filtered (_(" (%s,%s) pending."),
12169 event_location_to_string (b->location.get ()),
12170 b->extra_string);
12171 }
12172 else
12173 {
12174 printf_filtered (_(" (%s %s) pending."),
12175 event_location_to_string (b->location.get ()),
12176 b->extra_string);
12177 }
12178 }
12179 else
12180 {
12181 if (opts.addressprint || b->loc->symtab == NULL)
12182 printf_filtered (" at %ps",
12183 styled_string (address_style.style (),
12184 paddress (b->loc->gdbarch,
12185 b->loc->address)));
12186 if (b->loc->symtab != NULL)
12187 {
12188 /* If there is a single location, we can print the location
12189 more nicely. */
12190 if (b->loc->next == NULL)
12191 {
12192 const char *filename
12193 = symtab_to_filename_for_display (b->loc->symtab);
12194 printf_filtered (": file %ps, line %d.",
12195 styled_string (file_name_style.style (),
12196 filename),
12197 b->loc->line_number);
12198 }
12199 else
12200 /* This is not ideal, but each location may have a
12201 different file name, and this at least reflects the
12202 real situation somewhat. */
12203 printf_filtered (": %s.",
12204 event_location_to_string (b->location.get ()));
12205 }
12206
12207 if (b->loc->next)
12208 {
12209 struct bp_location *loc = b->loc;
12210 int n = 0;
12211 for (; loc; loc = loc->next)
12212 ++n;
12213 printf_filtered (" (%d locations)", n);
12214 }
12215 }
12216 }
12217
12218 bp_location::~bp_location ()
12219 {
12220 xfree (function_name);
12221 }
12222
12223 /* Destructor for the breakpoint base class. */
12224
12225 breakpoint::~breakpoint ()
12226 {
12227 xfree (this->cond_string);
12228 xfree (this->extra_string);
12229 }
12230
12231 /* See breakpoint.h. */
12232
12233 bp_locations_range breakpoint::locations ()
12234 {
12235 return bp_locations_range (this->loc);
12236 }
12237
12238 static struct bp_location *
12239 base_breakpoint_allocate_location (struct breakpoint *self)
12240 {
12241 return new bp_location (self);
12242 }
12243
12244 static void
12245 base_breakpoint_re_set (struct breakpoint *b)
12246 {
12247 /* Nothing to re-set. */
12248 }
12249
12250 #define internal_error_pure_virtual_called() \
12251 gdb_assert_not_reached ("pure virtual function called")
12252
12253 static int
12254 base_breakpoint_insert_location (struct bp_location *bl)
12255 {
12256 internal_error_pure_virtual_called ();
12257 }
12258
12259 static int
12260 base_breakpoint_remove_location (struct bp_location *bl,
12261 enum remove_bp_reason reason)
12262 {
12263 internal_error_pure_virtual_called ();
12264 }
12265
12266 static int
12267 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12268 const address_space *aspace,
12269 CORE_ADDR bp_addr,
12270 const struct target_waitstatus *ws)
12271 {
12272 internal_error_pure_virtual_called ();
12273 }
12274
12275 static void
12276 base_breakpoint_check_status (bpstat bs)
12277 {
12278 /* Always stop. */
12279 }
12280
12281 /* A "works_in_software_mode" breakpoint_ops method that just internal
12282 errors. */
12283
12284 static int
12285 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12286 {
12287 internal_error_pure_virtual_called ();
12288 }
12289
12290 /* A "resources_needed" breakpoint_ops method that just internal
12291 errors. */
12292
12293 static int
12294 base_breakpoint_resources_needed (const struct bp_location *bl)
12295 {
12296 internal_error_pure_virtual_called ();
12297 }
12298
12299 static enum print_stop_action
12300 base_breakpoint_print_it (bpstat bs)
12301 {
12302 internal_error_pure_virtual_called ();
12303 }
12304
12305 static void
12306 base_breakpoint_print_one_detail (const struct breakpoint *self,
12307 struct ui_out *uiout)
12308 {
12309 /* nothing */
12310 }
12311
12312 static void
12313 base_breakpoint_print_mention (struct breakpoint *b)
12314 {
12315 internal_error_pure_virtual_called ();
12316 }
12317
12318 static void
12319 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12320 {
12321 internal_error_pure_virtual_called ();
12322 }
12323
12324 static void
12325 base_breakpoint_create_sals_from_location
12326 (struct event_location *location,
12327 struct linespec_result *canonical,
12328 enum bptype type_wanted)
12329 {
12330 internal_error_pure_virtual_called ();
12331 }
12332
12333 static void
12334 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12335 struct linespec_result *c,
12336 gdb::unique_xmalloc_ptr<char> cond_string,
12337 gdb::unique_xmalloc_ptr<char> extra_string,
12338 enum bptype type_wanted,
12339 enum bpdisp disposition,
12340 int thread,
12341 int task, int ignore_count,
12342 const struct breakpoint_ops *o,
12343 int from_tty, int enabled,
12344 int internal, unsigned flags)
12345 {
12346 internal_error_pure_virtual_called ();
12347 }
12348
12349 static std::vector<symtab_and_line>
12350 base_breakpoint_decode_location (struct breakpoint *b,
12351 struct event_location *location,
12352 struct program_space *search_pspace)
12353 {
12354 internal_error_pure_virtual_called ();
12355 }
12356
12357 /* The default 'explains_signal' method. */
12358
12359 static int
12360 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12361 {
12362 return 1;
12363 }
12364
12365 /* The default "after_condition_true" method. */
12366
12367 static void
12368 base_breakpoint_after_condition_true (struct bpstats *bs)
12369 {
12370 /* Nothing to do. */
12371 }
12372
12373 struct breakpoint_ops base_breakpoint_ops =
12374 {
12375 base_breakpoint_allocate_location,
12376 base_breakpoint_re_set,
12377 base_breakpoint_insert_location,
12378 base_breakpoint_remove_location,
12379 base_breakpoint_breakpoint_hit,
12380 base_breakpoint_check_status,
12381 base_breakpoint_resources_needed,
12382 base_breakpoint_works_in_software_mode,
12383 base_breakpoint_print_it,
12384 NULL,
12385 base_breakpoint_print_one_detail,
12386 base_breakpoint_print_mention,
12387 base_breakpoint_print_recreate,
12388 base_breakpoint_create_sals_from_location,
12389 base_breakpoint_create_breakpoints_sal,
12390 base_breakpoint_decode_location,
12391 base_breakpoint_explains_signal,
12392 base_breakpoint_after_condition_true,
12393 };
12394
12395 /* Default breakpoint_ops methods. */
12396
12397 static void
12398 bkpt_re_set (struct breakpoint *b)
12399 {
12400 /* FIXME: is this still reachable? */
12401 if (breakpoint_event_location_empty_p (b))
12402 {
12403 /* Anything without a location can't be re-set. */
12404 delete_breakpoint (b);
12405 return;
12406 }
12407
12408 breakpoint_re_set_default (b);
12409 }
12410
12411 static int
12412 bkpt_insert_location (struct bp_location *bl)
12413 {
12414 CORE_ADDR addr = bl->target_info.reqstd_address;
12415
12416 bl->target_info.kind = breakpoint_kind (bl, &addr);
12417 bl->target_info.placed_address = addr;
12418
12419 if (bl->loc_type == bp_loc_hardware_breakpoint)
12420 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12421 else
12422 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12423 }
12424
12425 static int
12426 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12427 {
12428 if (bl->loc_type == bp_loc_hardware_breakpoint)
12429 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12430 else
12431 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12432 }
12433
12434 static int
12435 bkpt_breakpoint_hit (const struct bp_location *bl,
12436 const address_space *aspace, CORE_ADDR bp_addr,
12437 const struct target_waitstatus *ws)
12438 {
12439 if (ws->kind != TARGET_WAITKIND_STOPPED
12440 || ws->value.sig != GDB_SIGNAL_TRAP)
12441 return 0;
12442
12443 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12444 aspace, bp_addr))
12445 return 0;
12446
12447 if (overlay_debugging /* unmapped overlay section */
12448 && section_is_overlay (bl->section)
12449 && !section_is_mapped (bl->section))
12450 return 0;
12451
12452 return 1;
12453 }
12454
12455 static int
12456 dprintf_breakpoint_hit (const struct bp_location *bl,
12457 const address_space *aspace, CORE_ADDR bp_addr,
12458 const struct target_waitstatus *ws)
12459 {
12460 if (dprintf_style == dprintf_style_agent
12461 && target_can_run_breakpoint_commands ())
12462 {
12463 /* An agent-style dprintf never causes a stop. If we see a trap
12464 for this address it must be for a breakpoint that happens to
12465 be set at the same address. */
12466 return 0;
12467 }
12468
12469 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12470 }
12471
12472 static int
12473 bkpt_resources_needed (const struct bp_location *bl)
12474 {
12475 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12476
12477 return 1;
12478 }
12479
12480 static enum print_stop_action
12481 bkpt_print_it (bpstat bs)
12482 {
12483 struct breakpoint *b;
12484 const struct bp_location *bl;
12485 int bp_temp;
12486 struct ui_out *uiout = current_uiout;
12487
12488 gdb_assert (bs->bp_location_at != NULL);
12489
12490 bl = bs->bp_location_at.get ();
12491 b = bs->breakpoint_at;
12492
12493 bp_temp = b->disposition == disp_del;
12494 if (bl->address != bl->requested_address)
12495 breakpoint_adjustment_warning (bl->requested_address,
12496 bl->address,
12497 b->number, 1);
12498 annotate_breakpoint (b->number);
12499 maybe_print_thread_hit_breakpoint (uiout);
12500
12501 if (uiout->is_mi_like_p ())
12502 {
12503 uiout->field_string ("reason",
12504 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12505 uiout->field_string ("disp", bpdisp_text (b->disposition));
12506 }
12507 if (bp_temp)
12508 uiout->message ("Temporary breakpoint %pF, ",
12509 signed_field ("bkptno", b->number));
12510 else
12511 uiout->message ("Breakpoint %pF, ",
12512 signed_field ("bkptno", b->number));
12513
12514 return PRINT_SRC_AND_LOC;
12515 }
12516
12517 static void
12518 bkpt_print_mention (struct breakpoint *b)
12519 {
12520 if (current_uiout->is_mi_like_p ())
12521 return;
12522
12523 switch (b->type)
12524 {
12525 case bp_breakpoint:
12526 case bp_gnu_ifunc_resolver:
12527 if (b->disposition == disp_del)
12528 printf_filtered (_("Temporary breakpoint"));
12529 else
12530 printf_filtered (_("Breakpoint"));
12531 printf_filtered (_(" %d"), b->number);
12532 if (b->type == bp_gnu_ifunc_resolver)
12533 printf_filtered (_(" at gnu-indirect-function resolver"));
12534 break;
12535 case bp_hardware_breakpoint:
12536 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12537 break;
12538 case bp_dprintf:
12539 printf_filtered (_("Dprintf %d"), b->number);
12540 break;
12541 }
12542
12543 say_where (b);
12544 }
12545
12546 static void
12547 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12548 {
12549 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12550 fprintf_unfiltered (fp, "tbreak");
12551 else if (tp->type == bp_breakpoint)
12552 fprintf_unfiltered (fp, "break");
12553 else if (tp->type == bp_hardware_breakpoint
12554 && tp->disposition == disp_del)
12555 fprintf_unfiltered (fp, "thbreak");
12556 else if (tp->type == bp_hardware_breakpoint)
12557 fprintf_unfiltered (fp, "hbreak");
12558 else
12559 internal_error (__FILE__, __LINE__,
12560 _("unhandled breakpoint type %d"), (int) tp->type);
12561
12562 fprintf_unfiltered (fp, " %s",
12563 event_location_to_string (tp->location.get ()));
12564
12565 /* Print out extra_string if this breakpoint is pending. It might
12566 contain, for example, conditions that were set by the user. */
12567 if (tp->loc == NULL && tp->extra_string != NULL)
12568 fprintf_unfiltered (fp, " %s", tp->extra_string);
12569
12570 print_recreate_thread (tp, fp);
12571 }
12572
12573 static void
12574 bkpt_create_sals_from_location (struct event_location *location,
12575 struct linespec_result *canonical,
12576 enum bptype type_wanted)
12577 {
12578 create_sals_from_location_default (location, canonical, type_wanted);
12579 }
12580
12581 static void
12582 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12583 struct linespec_result *canonical,
12584 gdb::unique_xmalloc_ptr<char> cond_string,
12585 gdb::unique_xmalloc_ptr<char> extra_string,
12586 enum bptype type_wanted,
12587 enum bpdisp disposition,
12588 int thread,
12589 int task, int ignore_count,
12590 const struct breakpoint_ops *ops,
12591 int from_tty, int enabled,
12592 int internal, unsigned flags)
12593 {
12594 create_breakpoints_sal_default (gdbarch, canonical,
12595 std::move (cond_string),
12596 std::move (extra_string),
12597 type_wanted,
12598 disposition, thread, task,
12599 ignore_count, ops, from_tty,
12600 enabled, internal, flags);
12601 }
12602
12603 static std::vector<symtab_and_line>
12604 bkpt_decode_location (struct breakpoint *b,
12605 struct event_location *location,
12606 struct program_space *search_pspace)
12607 {
12608 return decode_location_default (b, location, search_pspace);
12609 }
12610
12611 /* Virtual table for internal breakpoints. */
12612
12613 static void
12614 internal_bkpt_re_set (struct breakpoint *b)
12615 {
12616 switch (b->type)
12617 {
12618 /* Delete overlay event and longjmp master breakpoints; they
12619 will be reset later by breakpoint_re_set. */
12620 case bp_overlay_event:
12621 case bp_longjmp_master:
12622 case bp_std_terminate_master:
12623 case bp_exception_master:
12624 delete_breakpoint (b);
12625 break;
12626
12627 /* This breakpoint is special, it's set up when the inferior
12628 starts and we really don't want to touch it. */
12629 case bp_shlib_event:
12630
12631 /* Like bp_shlib_event, this breakpoint type is special. Once
12632 it is set up, we do not want to touch it. */
12633 case bp_thread_event:
12634 break;
12635 }
12636 }
12637
12638 static void
12639 internal_bkpt_check_status (bpstat bs)
12640 {
12641 if (bs->breakpoint_at->type == bp_shlib_event)
12642 {
12643 /* If requested, stop when the dynamic linker notifies GDB of
12644 events. This allows the user to get control and place
12645 breakpoints in initializer routines for dynamically loaded
12646 objects (among other things). */
12647 bs->stop = stop_on_solib_events;
12648 bs->print = stop_on_solib_events;
12649 }
12650 else
12651 bs->stop = 0;
12652 }
12653
12654 static enum print_stop_action
12655 internal_bkpt_print_it (bpstat bs)
12656 {
12657 struct breakpoint *b;
12658
12659 b = bs->breakpoint_at;
12660
12661 switch (b->type)
12662 {
12663 case bp_shlib_event:
12664 /* Did we stop because the user set the stop_on_solib_events
12665 variable? (If so, we report this as a generic, "Stopped due
12666 to shlib event" message.) */
12667 print_solib_event (0);
12668 break;
12669
12670 case bp_thread_event:
12671 /* Not sure how we will get here.
12672 GDB should not stop for these breakpoints. */
12673 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12674 break;
12675
12676 case bp_overlay_event:
12677 /* By analogy with the thread event, GDB should not stop for these. */
12678 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12679 break;
12680
12681 case bp_longjmp_master:
12682 /* These should never be enabled. */
12683 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12684 break;
12685
12686 case bp_std_terminate_master:
12687 /* These should never be enabled. */
12688 printf_filtered (_("std::terminate Master Breakpoint: "
12689 "gdb should not stop!\n"));
12690 break;
12691
12692 case bp_exception_master:
12693 /* These should never be enabled. */
12694 printf_filtered (_("Exception Master Breakpoint: "
12695 "gdb should not stop!\n"));
12696 break;
12697 }
12698
12699 return PRINT_NOTHING;
12700 }
12701
12702 static void
12703 internal_bkpt_print_mention (struct breakpoint *b)
12704 {
12705 /* Nothing to mention. These breakpoints are internal. */
12706 }
12707
12708 /* Virtual table for momentary breakpoints */
12709
12710 static void
12711 momentary_bkpt_re_set (struct breakpoint *b)
12712 {
12713 /* Keep temporary breakpoints, which can be encountered when we step
12714 over a dlopen call and solib_add is resetting the breakpoints.
12715 Otherwise these should have been blown away via the cleanup chain
12716 or by breakpoint_init_inferior when we rerun the executable. */
12717 }
12718
12719 static void
12720 momentary_bkpt_check_status (bpstat bs)
12721 {
12722 /* Nothing. The point of these breakpoints is causing a stop. */
12723 }
12724
12725 static enum print_stop_action
12726 momentary_bkpt_print_it (bpstat bs)
12727 {
12728 return PRINT_UNKNOWN;
12729 }
12730
12731 static void
12732 momentary_bkpt_print_mention (struct breakpoint *b)
12733 {
12734 /* Nothing to mention. These breakpoints are internal. */
12735 }
12736
12737 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12738
12739 It gets cleared already on the removal of the first one of such placed
12740 breakpoints. This is OK as they get all removed altogether. */
12741
12742 longjmp_breakpoint::~longjmp_breakpoint ()
12743 {
12744 thread_info *tp = find_thread_global_id (this->thread);
12745
12746 if (tp != NULL)
12747 tp->initiating_frame = null_frame_id;
12748 }
12749
12750 /* Specific methods for probe breakpoints. */
12751
12752 static int
12753 bkpt_probe_insert_location (struct bp_location *bl)
12754 {
12755 int v = bkpt_insert_location (bl);
12756
12757 if (v == 0)
12758 {
12759 /* The insertion was successful, now let's set the probe's semaphore
12760 if needed. */
12761 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
12762 }
12763
12764 return v;
12765 }
12766
12767 static int
12768 bkpt_probe_remove_location (struct bp_location *bl,
12769 enum remove_bp_reason reason)
12770 {
12771 /* Let's clear the semaphore before removing the location. */
12772 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
12773
12774 return bkpt_remove_location (bl, reason);
12775 }
12776
12777 static void
12778 bkpt_probe_create_sals_from_location (struct event_location *location,
12779 struct linespec_result *canonical,
12780 enum bptype type_wanted)
12781 {
12782 struct linespec_sals lsal;
12783
12784 lsal.sals = parse_probes (location, NULL, canonical);
12785 lsal.canonical
12786 = xstrdup (event_location_to_string (canonical->location.get ()));
12787 canonical->lsals.push_back (std::move (lsal));
12788 }
12789
12790 static std::vector<symtab_and_line>
12791 bkpt_probe_decode_location (struct breakpoint *b,
12792 struct event_location *location,
12793 struct program_space *search_pspace)
12794 {
12795 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12796 if (sals.empty ())
12797 error (_("probe not found"));
12798 return sals;
12799 }
12800
12801 /* The breakpoint_ops structure to be used in tracepoints. */
12802
12803 static void
12804 tracepoint_re_set (struct breakpoint *b)
12805 {
12806 breakpoint_re_set_default (b);
12807 }
12808
12809 static int
12810 tracepoint_breakpoint_hit (const struct bp_location *bl,
12811 const address_space *aspace, CORE_ADDR bp_addr,
12812 const struct target_waitstatus *ws)
12813 {
12814 /* By definition, the inferior does not report stops at
12815 tracepoints. */
12816 return 0;
12817 }
12818
12819 static void
12820 tracepoint_print_one_detail (const struct breakpoint *self,
12821 struct ui_out *uiout)
12822 {
12823 struct tracepoint *tp = (struct tracepoint *) self;
12824 if (!tp->static_trace_marker_id.empty ())
12825 {
12826 gdb_assert (self->type == bp_static_tracepoint);
12827
12828 uiout->message ("\tmarker id is %pF\n",
12829 string_field ("static-tracepoint-marker-string-id",
12830 tp->static_trace_marker_id.c_str ()));
12831 }
12832 }
12833
12834 static void
12835 tracepoint_print_mention (struct breakpoint *b)
12836 {
12837 if (current_uiout->is_mi_like_p ())
12838 return;
12839
12840 switch (b->type)
12841 {
12842 case bp_tracepoint:
12843 printf_filtered (_("Tracepoint"));
12844 printf_filtered (_(" %d"), b->number);
12845 break;
12846 case bp_fast_tracepoint:
12847 printf_filtered (_("Fast tracepoint"));
12848 printf_filtered (_(" %d"), b->number);
12849 break;
12850 case bp_static_tracepoint:
12851 printf_filtered (_("Static tracepoint"));
12852 printf_filtered (_(" %d"), b->number);
12853 break;
12854 default:
12855 internal_error (__FILE__, __LINE__,
12856 _("unhandled tracepoint type %d"), (int) b->type);
12857 }
12858
12859 say_where (b);
12860 }
12861
12862 static void
12863 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
12864 {
12865 struct tracepoint *tp = (struct tracepoint *) self;
12866
12867 if (self->type == bp_fast_tracepoint)
12868 fprintf_unfiltered (fp, "ftrace");
12869 else if (self->type == bp_static_tracepoint)
12870 fprintf_unfiltered (fp, "strace");
12871 else if (self->type == bp_tracepoint)
12872 fprintf_unfiltered (fp, "trace");
12873 else
12874 internal_error (__FILE__, __LINE__,
12875 _("unhandled tracepoint type %d"), (int) self->type);
12876
12877 fprintf_unfiltered (fp, " %s",
12878 event_location_to_string (self->location.get ()));
12879 print_recreate_thread (self, fp);
12880
12881 if (tp->pass_count)
12882 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
12883 }
12884
12885 static void
12886 tracepoint_create_sals_from_location (struct event_location *location,
12887 struct linespec_result *canonical,
12888 enum bptype type_wanted)
12889 {
12890 create_sals_from_location_default (location, canonical, type_wanted);
12891 }
12892
12893 static void
12894 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12895 struct linespec_result *canonical,
12896 gdb::unique_xmalloc_ptr<char> cond_string,
12897 gdb::unique_xmalloc_ptr<char> extra_string,
12898 enum bptype type_wanted,
12899 enum bpdisp disposition,
12900 int thread,
12901 int task, int ignore_count,
12902 const struct breakpoint_ops *ops,
12903 int from_tty, int enabled,
12904 int internal, unsigned flags)
12905 {
12906 create_breakpoints_sal_default (gdbarch, canonical,
12907 std::move (cond_string),
12908 std::move (extra_string),
12909 type_wanted,
12910 disposition, thread, task,
12911 ignore_count, ops, from_tty,
12912 enabled, internal, flags);
12913 }
12914
12915 static std::vector<symtab_and_line>
12916 tracepoint_decode_location (struct breakpoint *b,
12917 struct event_location *location,
12918 struct program_space *search_pspace)
12919 {
12920 return decode_location_default (b, location, search_pspace);
12921 }
12922
12923 struct breakpoint_ops tracepoint_breakpoint_ops;
12924
12925 /* Virtual table for tracepoints on static probes. */
12926
12927 static void
12928 tracepoint_probe_create_sals_from_location
12929 (struct event_location *location,
12930 struct linespec_result *canonical,
12931 enum bptype type_wanted)
12932 {
12933 /* We use the same method for breakpoint on probes. */
12934 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
12935 }
12936
12937 static std::vector<symtab_and_line>
12938 tracepoint_probe_decode_location (struct breakpoint *b,
12939 struct event_location *location,
12940 struct program_space *search_pspace)
12941 {
12942 /* We use the same method for breakpoint on probes. */
12943 return bkpt_probe_decode_location (b, location, search_pspace);
12944 }
12945
12946 /* Dprintf breakpoint_ops methods. */
12947
12948 static void
12949 dprintf_re_set (struct breakpoint *b)
12950 {
12951 breakpoint_re_set_default (b);
12952
12953 /* extra_string should never be non-NULL for dprintf. */
12954 gdb_assert (b->extra_string != NULL);
12955
12956 /* 1 - connect to target 1, that can run breakpoint commands.
12957 2 - create a dprintf, which resolves fine.
12958 3 - disconnect from target 1
12959 4 - connect to target 2, that can NOT run breakpoint commands.
12960
12961 After steps #3/#4, you'll want the dprintf command list to
12962 be updated, because target 1 and 2 may well return different
12963 answers for target_can_run_breakpoint_commands().
12964 Given absence of finer grained resetting, we get to do
12965 it all the time. */
12966 if (b->extra_string != NULL)
12967 update_dprintf_command_list (b);
12968 }
12969
12970 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
12971
12972 static void
12973 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12974 {
12975 fprintf_unfiltered (fp, "dprintf %s,%s",
12976 event_location_to_string (tp->location.get ()),
12977 tp->extra_string);
12978 print_recreate_thread (tp, fp);
12979 }
12980
12981 /* Implement the "after_condition_true" breakpoint_ops method for
12982 dprintf.
12983
12984 dprintf's are implemented with regular commands in their command
12985 list, but we run the commands here instead of before presenting the
12986 stop to the user, as dprintf's don't actually cause a stop. This
12987 also makes it so that the commands of multiple dprintfs at the same
12988 address are all handled. */
12989
12990 static void
12991 dprintf_after_condition_true (struct bpstats *bs)
12992 {
12993 struct bpstats tmp_bs;
12994 struct bpstats *tmp_bs_p = &tmp_bs;
12995
12996 /* dprintf's never cause a stop. This wasn't set in the
12997 check_status hook instead because that would make the dprintf's
12998 condition not be evaluated. */
12999 bs->stop = 0;
13000
13001 /* Run the command list here. Take ownership of it instead of
13002 copying. We never want these commands to run later in
13003 bpstat_do_actions, if a breakpoint that causes a stop happens to
13004 be set at same address as this dprintf, or even if running the
13005 commands here throws. */
13006 tmp_bs.commands = bs->commands;
13007 bs->commands = NULL;
13008
13009 bpstat_do_actions_1 (&tmp_bs_p);
13010
13011 /* 'tmp_bs.commands' will usually be NULL by now, but
13012 bpstat_do_actions_1 may return early without processing the whole
13013 list. */
13014 }
13015
13016 /* The breakpoint_ops structure to be used on static tracepoints with
13017 markers (`-m'). */
13018
13019 static void
13020 strace_marker_create_sals_from_location (struct event_location *location,
13021 struct linespec_result *canonical,
13022 enum bptype type_wanted)
13023 {
13024 struct linespec_sals lsal;
13025 const char *arg_start, *arg;
13026
13027 arg = arg_start = get_linespec_location (location)->spec_string;
13028 lsal.sals = decode_static_tracepoint_spec (&arg);
13029
13030 std::string str (arg_start, arg - arg_start);
13031 const char *ptr = str.c_str ();
13032 canonical->location
13033 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
13034
13035 lsal.canonical
13036 = xstrdup (event_location_to_string (canonical->location.get ()));
13037 canonical->lsals.push_back (std::move (lsal));
13038 }
13039
13040 static void
13041 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13042 struct linespec_result *canonical,
13043 gdb::unique_xmalloc_ptr<char> cond_string,
13044 gdb::unique_xmalloc_ptr<char> extra_string,
13045 enum bptype type_wanted,
13046 enum bpdisp disposition,
13047 int thread,
13048 int task, int ignore_count,
13049 const struct breakpoint_ops *ops,
13050 int from_tty, int enabled,
13051 int internal, unsigned flags)
13052 {
13053 const linespec_sals &lsal = canonical->lsals[0];
13054
13055 /* If the user is creating a static tracepoint by marker id
13056 (strace -m MARKER_ID), then store the sals index, so that
13057 breakpoint_re_set can try to match up which of the newly
13058 found markers corresponds to this one, and, don't try to
13059 expand multiple locations for each sal, given than SALS
13060 already should contain all sals for MARKER_ID. */
13061
13062 for (size_t i = 0; i < lsal.sals.size (); i++)
13063 {
13064 event_location_up location
13065 = copy_event_location (canonical->location.get ());
13066
13067 std::unique_ptr<tracepoint> tp (new tracepoint ());
13068 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
13069 std::move (location), NULL,
13070 std::move (cond_string),
13071 std::move (extra_string),
13072 type_wanted, disposition,
13073 thread, task, ignore_count, ops,
13074 from_tty, enabled, internal, flags,
13075 canonical->special_display);
13076 /* Given that its possible to have multiple markers with
13077 the same string id, if the user is creating a static
13078 tracepoint by marker id ("strace -m MARKER_ID"), then
13079 store the sals index, so that breakpoint_re_set can
13080 try to match up which of the newly found markers
13081 corresponds to this one */
13082 tp->static_trace_marker_id_idx = i;
13083
13084 install_breakpoint (internal, std::move (tp), 0);
13085 }
13086 }
13087
13088 static std::vector<symtab_and_line>
13089 strace_marker_decode_location (struct breakpoint *b,
13090 struct event_location *location,
13091 struct program_space *search_pspace)
13092 {
13093 struct tracepoint *tp = (struct tracepoint *) b;
13094 const char *s = get_linespec_location (location)->spec_string;
13095
13096 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13097 if (sals.size () > tp->static_trace_marker_id_idx)
13098 {
13099 sals[0] = sals[tp->static_trace_marker_id_idx];
13100 sals.resize (1);
13101 return sals;
13102 }
13103 else
13104 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
13105 }
13106
13107 static struct breakpoint_ops strace_marker_breakpoint_ops;
13108
13109 static int
13110 strace_marker_p (struct breakpoint *b)
13111 {
13112 return b->ops == &strace_marker_breakpoint_ops;
13113 }
13114
13115 /* Delete a breakpoint and clean up all traces of it in the data
13116 structures. */
13117
13118 void
13119 delete_breakpoint (struct breakpoint *bpt)
13120 {
13121 gdb_assert (bpt != NULL);
13122
13123 /* Has this bp already been deleted? This can happen because
13124 multiple lists can hold pointers to bp's. bpstat lists are
13125 especial culprits.
13126
13127 One example of this happening is a watchpoint's scope bp. When
13128 the scope bp triggers, we notice that the watchpoint is out of
13129 scope, and delete it. We also delete its scope bp. But the
13130 scope bp is marked "auto-deleting", and is already on a bpstat.
13131 That bpstat is then checked for auto-deleting bp's, which are
13132 deleted.
13133
13134 A real solution to this problem might involve reference counts in
13135 bp's, and/or giving them pointers back to their referencing
13136 bpstat's, and teaching delete_breakpoint to only free a bp's
13137 storage when no more references were extent. A cheaper bandaid
13138 was chosen. */
13139 if (bpt->type == bp_none)
13140 return;
13141
13142 /* At least avoid this stale reference until the reference counting
13143 of breakpoints gets resolved. */
13144 if (bpt->related_breakpoint != bpt)
13145 {
13146 struct breakpoint *related;
13147 struct watchpoint *w;
13148
13149 if (bpt->type == bp_watchpoint_scope)
13150 w = (struct watchpoint *) bpt->related_breakpoint;
13151 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13152 w = (struct watchpoint *) bpt;
13153 else
13154 w = NULL;
13155 if (w != NULL)
13156 watchpoint_del_at_next_stop (w);
13157
13158 /* Unlink bpt from the bpt->related_breakpoint ring. */
13159 for (related = bpt; related->related_breakpoint != bpt;
13160 related = related->related_breakpoint);
13161 related->related_breakpoint = bpt->related_breakpoint;
13162 bpt->related_breakpoint = bpt;
13163 }
13164
13165 /* watch_command_1 creates a watchpoint but only sets its number if
13166 update_watchpoint succeeds in creating its bp_locations. If there's
13167 a problem in that process, we'll be asked to delete the half-created
13168 watchpoint. In that case, don't announce the deletion. */
13169 if (bpt->number)
13170 gdb::observers::breakpoint_deleted.notify (bpt);
13171
13172 if (breakpoint_chain == bpt)
13173 breakpoint_chain = bpt->next;
13174
13175 for (breakpoint *b : all_breakpoints ())
13176 if (b->next == bpt)
13177 {
13178 b->next = bpt->next;
13179 break;
13180 }
13181
13182 /* Be sure no bpstat's are pointing at the breakpoint after it's
13183 been freed. */
13184 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13185 in all threads for now. Note that we cannot just remove bpstats
13186 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13187 commands are associated with the bpstat; if we remove it here,
13188 then the later call to bpstat_do_actions (&stop_bpstat); in
13189 event-top.c won't do anything, and temporary breakpoints with
13190 commands won't work. */
13191
13192 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13193
13194 /* Now that breakpoint is removed from breakpoint list, update the
13195 global location list. This will remove locations that used to
13196 belong to this breakpoint. Do this before freeing the breakpoint
13197 itself, since remove_breakpoint looks at location's owner. It
13198 might be better design to have location completely
13199 self-contained, but it's not the case now. */
13200 update_global_location_list (UGLL_DONT_INSERT);
13201
13202 /* On the chance that someone will soon try again to delete this
13203 same bp, we mark it as deleted before freeing its storage. */
13204 bpt->type = bp_none;
13205 delete bpt;
13206 }
13207
13208 /* Iterator function to call a user-provided callback function once
13209 for each of B and its related breakpoints. */
13210
13211 static void
13212 iterate_over_related_breakpoints (struct breakpoint *b,
13213 gdb::function_view<void (breakpoint *)> function)
13214 {
13215 struct breakpoint *related;
13216
13217 related = b;
13218 do
13219 {
13220 struct breakpoint *next;
13221
13222 /* FUNCTION may delete RELATED. */
13223 next = related->related_breakpoint;
13224
13225 if (next == related)
13226 {
13227 /* RELATED is the last ring entry. */
13228 function (related);
13229
13230 /* FUNCTION may have deleted it, so we'd never reach back to
13231 B. There's nothing left to do anyway, so just break
13232 out. */
13233 break;
13234 }
13235 else
13236 function (related);
13237
13238 related = next;
13239 }
13240 while (related != b);
13241 }
13242
13243 static void
13244 delete_command (const char *arg, int from_tty)
13245 {
13246 dont_repeat ();
13247
13248 if (arg == 0)
13249 {
13250 int breaks_to_delete = 0;
13251
13252 /* Delete all breakpoints if no argument. Do not delete
13253 internal breakpoints, these have to be deleted with an
13254 explicit breakpoint number argument. */
13255 for (breakpoint *b : all_breakpoints ())
13256 if (user_breakpoint_p (b))
13257 {
13258 breaks_to_delete = 1;
13259 break;
13260 }
13261
13262 /* Ask user only if there are some breakpoints to delete. */
13263 if (!from_tty
13264 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13265 for (breakpoint *b : all_breakpoints_safe ())
13266 if (user_breakpoint_p (b))
13267 delete_breakpoint (b);
13268 }
13269 else
13270 map_breakpoint_numbers
13271 (arg, [&] (breakpoint *br)
13272 {
13273 iterate_over_related_breakpoints (br, delete_breakpoint);
13274 });
13275 }
13276
13277 /* Return true if all locations of B bound to PSPACE are pending. If
13278 PSPACE is NULL, all locations of all program spaces are
13279 considered. */
13280
13281 static int
13282 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13283 {
13284 for (bp_location *loc : b->locations ())
13285 if ((pspace == NULL
13286 || loc->pspace == pspace)
13287 && !loc->shlib_disabled
13288 && !loc->pspace->executing_startup)
13289 return 0;
13290 return 1;
13291 }
13292
13293 /* Subroutine of update_breakpoint_locations to simplify it.
13294 Return non-zero if multiple fns in list LOC have the same name.
13295 Null names are ignored. */
13296
13297 static int
13298 ambiguous_names_p (struct bp_location *loc)
13299 {
13300 struct bp_location *l;
13301 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
13302 xcalloc, xfree));
13303
13304 for (l = loc; l != NULL; l = l->next)
13305 {
13306 const char **slot;
13307 const char *name = l->function_name;
13308
13309 /* Allow for some names to be NULL, ignore them. */
13310 if (name == NULL)
13311 continue;
13312
13313 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
13314 INSERT);
13315 /* NOTE: We can assume slot != NULL here because xcalloc never
13316 returns NULL. */
13317 if (*slot != NULL)
13318 return 1;
13319 *slot = name;
13320 }
13321
13322 return 0;
13323 }
13324
13325 /* When symbols change, it probably means the sources changed as well,
13326 and it might mean the static tracepoint markers are no longer at
13327 the same address or line numbers they used to be at last we
13328 checked. Losing your static tracepoints whenever you rebuild is
13329 undesirable. This function tries to resync/rematch gdb static
13330 tracepoints with the markers on the target, for static tracepoints
13331 that have not been set by marker id. Static tracepoint that have
13332 been set by marker id are reset by marker id in breakpoint_re_set.
13333 The heuristic is:
13334
13335 1) For a tracepoint set at a specific address, look for a marker at
13336 the old PC. If one is found there, assume to be the same marker.
13337 If the name / string id of the marker found is different from the
13338 previous known name, assume that means the user renamed the marker
13339 in the sources, and output a warning.
13340
13341 2) For a tracepoint set at a given line number, look for a marker
13342 at the new address of the old line number. If one is found there,
13343 assume to be the same marker. If the name / string id of the
13344 marker found is different from the previous known name, assume that
13345 means the user renamed the marker in the sources, and output a
13346 warning.
13347
13348 3) If a marker is no longer found at the same address or line, it
13349 may mean the marker no longer exists. But it may also just mean
13350 the code changed a bit. Maybe the user added a few lines of code
13351 that made the marker move up or down (in line number terms). Ask
13352 the target for info about the marker with the string id as we knew
13353 it. If found, update line number and address in the matching
13354 static tracepoint. This will get confused if there's more than one
13355 marker with the same ID (possible in UST, although unadvised
13356 precisely because it confuses tools). */
13357
13358 static struct symtab_and_line
13359 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13360 {
13361 struct tracepoint *tp = (struct tracepoint *) b;
13362 struct static_tracepoint_marker marker;
13363 CORE_ADDR pc;
13364
13365 pc = sal.pc;
13366 if (sal.line)
13367 find_line_pc (sal.symtab, sal.line, &pc);
13368
13369 if (target_static_tracepoint_marker_at (pc, &marker))
13370 {
13371 if (tp->static_trace_marker_id != marker.str_id)
13372 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13373 b->number, tp->static_trace_marker_id.c_str (),
13374 marker.str_id.c_str ());
13375
13376 tp->static_trace_marker_id = std::move (marker.str_id);
13377
13378 return sal;
13379 }
13380
13381 /* Old marker wasn't found on target at lineno. Try looking it up
13382 by string ID. */
13383 if (!sal.explicit_pc
13384 && sal.line != 0
13385 && sal.symtab != NULL
13386 && !tp->static_trace_marker_id.empty ())
13387 {
13388 std::vector<static_tracepoint_marker> markers
13389 = target_static_tracepoint_markers_by_strid
13390 (tp->static_trace_marker_id.c_str ());
13391
13392 if (!markers.empty ())
13393 {
13394 struct symbol *sym;
13395 struct static_tracepoint_marker *tpmarker;
13396 struct ui_out *uiout = current_uiout;
13397 struct explicit_location explicit_loc;
13398
13399 tpmarker = &markers[0];
13400
13401 tp->static_trace_marker_id = std::move (tpmarker->str_id);
13402
13403 warning (_("marker for static tracepoint %d (%s) not "
13404 "found at previous line number"),
13405 b->number, tp->static_trace_marker_id.c_str ());
13406
13407 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
13408 sym = find_pc_sect_function (tpmarker->address, NULL);
13409 uiout->text ("Now in ");
13410 if (sym)
13411 {
13412 uiout->field_string ("func", sym->print_name (),
13413 function_name_style.style ());
13414 uiout->text (" at ");
13415 }
13416 uiout->field_string ("file",
13417 symtab_to_filename_for_display (sal2.symtab),
13418 file_name_style.style ());
13419 uiout->text (":");
13420
13421 if (uiout->is_mi_like_p ())
13422 {
13423 const char *fullname = symtab_to_fullname (sal2.symtab);
13424
13425 uiout->field_string ("fullname", fullname);
13426 }
13427
13428 uiout->field_signed ("line", sal2.line);
13429 uiout->text ("\n");
13430
13431 b->loc->line_number = sal2.line;
13432 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13433
13434 b->location.reset (NULL);
13435 initialize_explicit_location (&explicit_loc);
13436 explicit_loc.source_filename
13437 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13438 explicit_loc.line_offset.offset = b->loc->line_number;
13439 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13440 b->location = new_explicit_location (&explicit_loc);
13441
13442 /* Might be nice to check if function changed, and warn if
13443 so. */
13444 }
13445 }
13446 return sal;
13447 }
13448
13449 /* Returns 1 iff locations A and B are sufficiently same that
13450 we don't need to report breakpoint as changed. */
13451
13452 static int
13453 locations_are_equal (struct bp_location *a, struct bp_location *b)
13454 {
13455 while (a && b)
13456 {
13457 if (a->address != b->address)
13458 return 0;
13459
13460 if (a->shlib_disabled != b->shlib_disabled)
13461 return 0;
13462
13463 if (a->enabled != b->enabled)
13464 return 0;
13465
13466 if (a->disabled_by_cond != b->disabled_by_cond)
13467 return 0;
13468
13469 a = a->next;
13470 b = b->next;
13471 }
13472
13473 if ((a == NULL) != (b == NULL))
13474 return 0;
13475
13476 return 1;
13477 }
13478
13479 /* Split all locations of B that are bound to PSPACE out of B's
13480 location list to a separate list and return that list's head. If
13481 PSPACE is NULL, hoist out all locations of B. */
13482
13483 static struct bp_location *
13484 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13485 {
13486 struct bp_location head;
13487 struct bp_location *i = b->loc;
13488 struct bp_location **i_link = &b->loc;
13489 struct bp_location *hoisted = &head;
13490
13491 if (pspace == NULL)
13492 {
13493 i = b->loc;
13494 b->loc = NULL;
13495 return i;
13496 }
13497
13498 head.next = NULL;
13499
13500 while (i != NULL)
13501 {
13502 if (i->pspace == pspace)
13503 {
13504 *i_link = i->next;
13505 i->next = NULL;
13506 hoisted->next = i;
13507 hoisted = i;
13508 }
13509 else
13510 i_link = &i->next;
13511 i = *i_link;
13512 }
13513
13514 return head.next;
13515 }
13516
13517 /* Create new breakpoint locations for B (a hardware or software
13518 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13519 zero, then B is a ranged breakpoint. Only recreates locations for
13520 FILTER_PSPACE. Locations of other program spaces are left
13521 untouched. */
13522
13523 void
13524 update_breakpoint_locations (struct breakpoint *b,
13525 struct program_space *filter_pspace,
13526 gdb::array_view<const symtab_and_line> sals,
13527 gdb::array_view<const symtab_and_line> sals_end)
13528 {
13529 struct bp_location *existing_locations;
13530
13531 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
13532 {
13533 /* Ranged breakpoints have only one start location and one end
13534 location. */
13535 b->enable_state = bp_disabled;
13536 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13537 "multiple locations found\n"),
13538 b->number);
13539 return;
13540 }
13541
13542 /* If there's no new locations, and all existing locations are
13543 pending, don't do anything. This optimizes the common case where
13544 all locations are in the same shared library, that was unloaded.
13545 We'd like to retain the location, so that when the library is
13546 loaded again, we don't loose the enabled/disabled status of the
13547 individual locations. */
13548 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
13549 return;
13550
13551 existing_locations = hoist_existing_locations (b, filter_pspace);
13552
13553 for (const auto &sal : sals)
13554 {
13555 struct bp_location *new_loc;
13556
13557 switch_to_program_space_and_thread (sal.pspace);
13558
13559 new_loc = add_location_to_breakpoint (b, &sal);
13560
13561 /* Reparse conditions, they might contain references to the
13562 old symtab. */
13563 if (b->cond_string != NULL)
13564 {
13565 const char *s;
13566
13567 s = b->cond_string;
13568 try
13569 {
13570 new_loc->cond = parse_exp_1 (&s, sal.pc,
13571 block_for_pc (sal.pc),
13572 0);
13573 }
13574 catch (const gdb_exception_error &e)
13575 {
13576 new_loc->disabled_by_cond = true;
13577 }
13578 }
13579
13580 if (!sals_end.empty ())
13581 {
13582 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13583
13584 new_loc->length = end - sals[0].pc + 1;
13585 }
13586 }
13587
13588 /* If possible, carry over 'disable' status from existing
13589 breakpoints. */
13590 {
13591 struct bp_location *e = existing_locations;
13592 /* If there are multiple breakpoints with the same function name,
13593 e.g. for inline functions, comparing function names won't work.
13594 Instead compare pc addresses; this is just a heuristic as things
13595 may have moved, but in practice it gives the correct answer
13596 often enough until a better solution is found. */
13597 int have_ambiguous_names = ambiguous_names_p (b->loc);
13598
13599 for (; e; e = e->next)
13600 {
13601 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
13602 {
13603 if (have_ambiguous_names)
13604 {
13605 for (bp_location *l : b->locations ())
13606 {
13607 /* Ignore software vs hardware location type at
13608 this point, because with "set breakpoint
13609 auto-hw", after a re-set, locations that were
13610 hardware can end up as software, or vice versa.
13611 As mentioned above, this is an heuristic and in
13612 practice should give the correct answer often
13613 enough. */
13614 if (breakpoint_locations_match (e, l, true))
13615 {
13616 l->enabled = e->enabled;
13617 l->disabled_by_cond = e->disabled_by_cond;
13618 break;
13619 }
13620 }
13621 }
13622 else
13623 {
13624 for (bp_location *l : b->locations ())
13625 if (l->function_name
13626 && strcmp (e->function_name, l->function_name) == 0)
13627 {
13628 l->enabled = e->enabled;
13629 l->disabled_by_cond = e->disabled_by_cond;
13630 break;
13631 }
13632 }
13633 }
13634 }
13635 }
13636
13637 if (!locations_are_equal (existing_locations, b->loc))
13638 gdb::observers::breakpoint_modified.notify (b);
13639 }
13640
13641 /* Find the SaL locations corresponding to the given LOCATION.
13642 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13643
13644 static std::vector<symtab_and_line>
13645 location_to_sals (struct breakpoint *b, struct event_location *location,
13646 struct program_space *search_pspace, int *found)
13647 {
13648 struct gdb_exception exception;
13649
13650 gdb_assert (b->ops != NULL);
13651
13652 std::vector<symtab_and_line> sals;
13653
13654 try
13655 {
13656 sals = b->ops->decode_location (b, location, search_pspace);
13657 }
13658 catch (gdb_exception_error &e)
13659 {
13660 int not_found_and_ok = 0;
13661
13662 /* For pending breakpoints, it's expected that parsing will
13663 fail until the right shared library is loaded. User has
13664 already told to create pending breakpoints and don't need
13665 extra messages. If breakpoint is in bp_shlib_disabled
13666 state, then user already saw the message about that
13667 breakpoint being disabled, and don't want to see more
13668 errors. */
13669 if (e.error == NOT_FOUND_ERROR
13670 && (b->condition_not_parsed
13671 || (b->loc != NULL
13672 && search_pspace != NULL
13673 && b->loc->pspace != search_pspace)
13674 || (b->loc && b->loc->shlib_disabled)
13675 || (b->loc && b->loc->pspace->executing_startup)
13676 || b->enable_state == bp_disabled))
13677 not_found_and_ok = 1;
13678
13679 if (!not_found_and_ok)
13680 {
13681 /* We surely don't want to warn about the same breakpoint
13682 10 times. One solution, implemented here, is disable
13683 the breakpoint on error. Another solution would be to
13684 have separate 'warning emitted' flag. Since this
13685 happens only when a binary has changed, I don't know
13686 which approach is better. */
13687 b->enable_state = bp_disabled;
13688 throw;
13689 }
13690
13691 exception = std::move (e);
13692 }
13693
13694 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13695 {
13696 for (auto &sal : sals)
13697 resolve_sal_pc (&sal);
13698 if (b->condition_not_parsed && b->extra_string != NULL)
13699 {
13700 char *cond_string, *extra_string;
13701 int thread, task;
13702
13703 find_condition_and_thread_for_sals (sals, b->extra_string,
13704 &cond_string, &thread,
13705 &task, &extra_string);
13706 gdb_assert (b->cond_string == NULL);
13707 if (cond_string)
13708 b->cond_string = cond_string;
13709 b->thread = thread;
13710 b->task = task;
13711 if (extra_string)
13712 {
13713 xfree (b->extra_string);
13714 b->extra_string = extra_string;
13715 }
13716 b->condition_not_parsed = 0;
13717 }
13718
13719 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13720 sals[0] = update_static_tracepoint (b, sals[0]);
13721
13722 *found = 1;
13723 }
13724 else
13725 *found = 0;
13726
13727 return sals;
13728 }
13729
13730 /* The default re_set method, for typical hardware or software
13731 breakpoints. Reevaluate the breakpoint and recreate its
13732 locations. */
13733
13734 static void
13735 breakpoint_re_set_default (struct breakpoint *b)
13736 {
13737 struct program_space *filter_pspace = current_program_space;
13738 std::vector<symtab_and_line> expanded, expanded_end;
13739
13740 int found;
13741 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13742 filter_pspace, &found);
13743 if (found)
13744 expanded = std::move (sals);
13745
13746 if (b->location_range_end != NULL)
13747 {
13748 std::vector<symtab_and_line> sals_end
13749 = location_to_sals (b, b->location_range_end.get (),
13750 filter_pspace, &found);
13751 if (found)
13752 expanded_end = std::move (sals_end);
13753 }
13754
13755 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
13756 }
13757
13758 /* Default method for creating SALs from an address string. It basically
13759 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13760
13761 static void
13762 create_sals_from_location_default (struct event_location *location,
13763 struct linespec_result *canonical,
13764 enum bptype type_wanted)
13765 {
13766 parse_breakpoint_sals (location, canonical);
13767 }
13768
13769 /* Call create_breakpoints_sal for the given arguments. This is the default
13770 function for the `create_breakpoints_sal' method of
13771 breakpoint_ops. */
13772
13773 static void
13774 create_breakpoints_sal_default (struct gdbarch *gdbarch,
13775 struct linespec_result *canonical,
13776 gdb::unique_xmalloc_ptr<char> cond_string,
13777 gdb::unique_xmalloc_ptr<char> extra_string,
13778 enum bptype type_wanted,
13779 enum bpdisp disposition,
13780 int thread,
13781 int task, int ignore_count,
13782 const struct breakpoint_ops *ops,
13783 int from_tty, int enabled,
13784 int internal, unsigned flags)
13785 {
13786 create_breakpoints_sal (gdbarch, canonical,
13787 std::move (cond_string),
13788 std::move (extra_string),
13789 type_wanted, disposition,
13790 thread, task, ignore_count, ops, from_tty,
13791 enabled, internal, flags);
13792 }
13793
13794 /* Decode the line represented by S by calling decode_line_full. This is the
13795 default function for the `decode_location' method of breakpoint_ops. */
13796
13797 static std::vector<symtab_and_line>
13798 decode_location_default (struct breakpoint *b,
13799 struct event_location *location,
13800 struct program_space *search_pspace)
13801 {
13802 struct linespec_result canonical;
13803
13804 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
13805 NULL, 0, &canonical, multiple_symbols_all,
13806 b->filter.get ());
13807
13808 /* We should get 0 or 1 resulting SALs. */
13809 gdb_assert (canonical.lsals.size () < 2);
13810
13811 if (!canonical.lsals.empty ())
13812 {
13813 const linespec_sals &lsal = canonical.lsals[0];
13814 return std::move (lsal.sals);
13815 }
13816 return {};
13817 }
13818
13819 /* Reset a breakpoint. */
13820
13821 static void
13822 breakpoint_re_set_one (breakpoint *b)
13823 {
13824 input_radix = b->input_radix;
13825 set_language (b->language);
13826
13827 b->ops->re_set (b);
13828 }
13829
13830 /* Re-set breakpoint locations for the current program space.
13831 Locations bound to other program spaces are left untouched. */
13832
13833 void
13834 breakpoint_re_set (void)
13835 {
13836 {
13837 scoped_restore_current_language save_language;
13838 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
13839 scoped_restore_current_pspace_and_thread restore_pspace_thread;
13840
13841 /* breakpoint_re_set_one sets the current_language to the language
13842 of the breakpoint it is resetting (see prepare_re_set_context)
13843 before re-evaluating the breakpoint's location. This change can
13844 unfortunately get undone by accident if the language_mode is set
13845 to auto, and we either switch frames, or more likely in this context,
13846 we select the current frame.
13847
13848 We prevent this by temporarily turning the language_mode to
13849 language_mode_manual. We restore it once all breakpoints
13850 have been reset. */
13851 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13852 language_mode = language_mode_manual;
13853
13854 /* Note: we must not try to insert locations until after all
13855 breakpoints have been re-set. Otherwise, e.g., when re-setting
13856 breakpoint 1, we'd insert the locations of breakpoint 2, which
13857 hadn't been re-set yet, and thus may have stale locations. */
13858
13859 for (breakpoint *b : all_breakpoints_safe ())
13860 {
13861 try
13862 {
13863 breakpoint_re_set_one (b);
13864 }
13865 catch (const gdb_exception &ex)
13866 {
13867 exception_fprintf (gdb_stderr, ex,
13868 "Error in re-setting breakpoint %d: ",
13869 b->number);
13870 }
13871 }
13872
13873 jit_breakpoint_re_set ();
13874 }
13875
13876 create_overlay_event_breakpoint ();
13877 create_longjmp_master_breakpoint ();
13878 create_std_terminate_master_breakpoint ();
13879 create_exception_master_breakpoint ();
13880
13881 /* Now we can insert. */
13882 update_global_location_list (UGLL_MAY_INSERT);
13883 }
13884 \f
13885 /* Reset the thread number of this breakpoint:
13886
13887 - If the breakpoint is for all threads, leave it as-is.
13888 - Else, reset it to the current thread for inferior_ptid. */
13889 void
13890 breakpoint_re_set_thread (struct breakpoint *b)
13891 {
13892 if (b->thread != -1)
13893 {
13894 b->thread = inferior_thread ()->global_num;
13895
13896 /* We're being called after following a fork. The new fork is
13897 selected as current, and unless this was a vfork will have a
13898 different program space from the original thread. Reset that
13899 as well. */
13900 b->loc->pspace = current_program_space;
13901 }
13902 }
13903
13904 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13905 If from_tty is nonzero, it prints a message to that effect,
13906 which ends with a period (no newline). */
13907
13908 void
13909 set_ignore_count (int bptnum, int count, int from_tty)
13910 {
13911 if (count < 0)
13912 count = 0;
13913
13914 for (breakpoint *b : all_breakpoints ())
13915 if (b->number == bptnum)
13916 {
13917 if (is_tracepoint (b))
13918 {
13919 if (from_tty && count != 0)
13920 printf_filtered (_("Ignore count ignored for tracepoint %d."),
13921 bptnum);
13922 return;
13923 }
13924
13925 b->ignore_count = count;
13926 if (from_tty)
13927 {
13928 if (count == 0)
13929 printf_filtered (_("Will stop next time "
13930 "breakpoint %d is reached."),
13931 bptnum);
13932 else if (count == 1)
13933 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
13934 bptnum);
13935 else
13936 printf_filtered (_("Will ignore next %d "
13937 "crossings of breakpoint %d."),
13938 count, bptnum);
13939 }
13940 gdb::observers::breakpoint_modified.notify (b);
13941 return;
13942 }
13943
13944 error (_("No breakpoint number %d."), bptnum);
13945 }
13946
13947 /* Command to set ignore-count of breakpoint N to COUNT. */
13948
13949 static void
13950 ignore_command (const char *args, int from_tty)
13951 {
13952 const char *p = args;
13953 int num;
13954
13955 if (p == 0)
13956 error_no_arg (_("a breakpoint number"));
13957
13958 num = get_number (&p);
13959 if (num == 0)
13960 error (_("bad breakpoint number: '%s'"), args);
13961 if (*p == 0)
13962 error (_("Second argument (specified ignore-count) is missing."));
13963
13964 set_ignore_count (num,
13965 longest_to_int (value_as_long (parse_and_eval (p))),
13966 from_tty);
13967 if (from_tty)
13968 printf_filtered ("\n");
13969 }
13970 \f
13971
13972 /* Call FUNCTION on each of the breakpoints with numbers in the range
13973 defined by BP_NUM_RANGE (an inclusive range). */
13974
13975 static void
13976 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13977 gdb::function_view<void (breakpoint *)> function)
13978 {
13979 if (bp_num_range.first == 0)
13980 {
13981 warning (_("bad breakpoint number at or near '%d'"),
13982 bp_num_range.first);
13983 }
13984 else
13985 {
13986 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
13987 {
13988 bool match = false;
13989
13990 for (breakpoint *b : all_breakpoints_safe ())
13991 if (b->number == i)
13992 {
13993 match = true;
13994 function (b);
13995 break;
13996 }
13997 if (!match)
13998 printf_unfiltered (_("No breakpoint number %d.\n"), i);
13999 }
14000 }
14001 }
14002
14003 /* Call FUNCTION on each of the breakpoints whose numbers are given in
14004 ARGS. */
14005
14006 static void
14007 map_breakpoint_numbers (const char *args,
14008 gdb::function_view<void (breakpoint *)> function)
14009 {
14010 if (args == NULL || *args == '\0')
14011 error_no_arg (_("one or more breakpoint numbers"));
14012
14013 number_or_range_parser parser (args);
14014
14015 while (!parser.finished ())
14016 {
14017 int num = parser.get_number ();
14018 map_breakpoint_number_range (std::make_pair (num, num), function);
14019 }
14020 }
14021
14022 /* Return the breakpoint location structure corresponding to the
14023 BP_NUM and LOC_NUM values. */
14024
14025 static struct bp_location *
14026 find_location_by_number (int bp_num, int loc_num)
14027 {
14028 breakpoint *b = get_breakpoint (bp_num);
14029
14030 if (!b || b->number != bp_num)
14031 error (_("Bad breakpoint number '%d'"), bp_num);
14032
14033 if (loc_num == 0)
14034 error (_("Bad breakpoint location number '%d'"), loc_num);
14035
14036 int n = 0;
14037 for (bp_location *loc : b->locations ())
14038 if (++n == loc_num)
14039 return loc;
14040
14041 error (_("Bad breakpoint location number '%d'"), loc_num);
14042 }
14043
14044 /* Modes of operation for extract_bp_num. */
14045 enum class extract_bp_kind
14046 {
14047 /* Extracting a breakpoint number. */
14048 bp,
14049
14050 /* Extracting a location number. */
14051 loc,
14052 };
14053
14054 /* Extract a breakpoint or location number (as determined by KIND)
14055 from the string starting at START. TRAILER is a character which
14056 can be found after the number. If you don't want a trailer, use
14057 '\0'. If END_OUT is not NULL, it is set to point after the parsed
14058 string. This always returns a positive integer. */
14059
14060 static int
14061 extract_bp_num (extract_bp_kind kind, const char *start,
14062 int trailer, const char **end_out = NULL)
14063 {
14064 const char *end = start;
14065 int num = get_number_trailer (&end, trailer);
14066 if (num < 0)
14067 error (kind == extract_bp_kind::bp
14068 ? _("Negative breakpoint number '%.*s'")
14069 : _("Negative breakpoint location number '%.*s'"),
14070 int (end - start), start);
14071 if (num == 0)
14072 error (kind == extract_bp_kind::bp
14073 ? _("Bad breakpoint number '%.*s'")
14074 : _("Bad breakpoint location number '%.*s'"),
14075 int (end - start), start);
14076
14077 if (end_out != NULL)
14078 *end_out = end;
14079 return num;
14080 }
14081
14082 /* Extract a breakpoint or location range (as determined by KIND) in
14083 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14084 representing the (inclusive) range. The returned pair's elements
14085 are always positive integers. */
14086
14087 static std::pair<int, int>
14088 extract_bp_or_bp_range (extract_bp_kind kind,
14089 const std::string &arg,
14090 std::string::size_type arg_offset)
14091 {
14092 std::pair<int, int> range;
14093 const char *bp_loc = &arg[arg_offset];
14094 std::string::size_type dash = arg.find ('-', arg_offset);
14095 if (dash != std::string::npos)
14096 {
14097 /* bp_loc is a range (x-z). */
14098 if (arg.length () == dash + 1)
14099 error (kind == extract_bp_kind::bp
14100 ? _("Bad breakpoint number at or near: '%s'")
14101 : _("Bad breakpoint location number at or near: '%s'"),
14102 bp_loc);
14103
14104 const char *end;
14105 const char *start_first = bp_loc;
14106 const char *start_second = &arg[dash + 1];
14107 range.first = extract_bp_num (kind, start_first, '-');
14108 range.second = extract_bp_num (kind, start_second, '\0', &end);
14109
14110 if (range.first > range.second)
14111 error (kind == extract_bp_kind::bp
14112 ? _("Inverted breakpoint range at '%.*s'")
14113 : _("Inverted breakpoint location range at '%.*s'"),
14114 int (end - start_first), start_first);
14115 }
14116 else
14117 {
14118 /* bp_loc is a single value. */
14119 range.first = extract_bp_num (kind, bp_loc, '\0');
14120 range.second = range.first;
14121 }
14122 return range;
14123 }
14124
14125 /* Extract the breakpoint/location range specified by ARG. Returns
14126 the breakpoint range in BP_NUM_RANGE, and the location range in
14127 BP_LOC_RANGE.
14128
14129 ARG may be in any of the following forms:
14130
14131 x where 'x' is a breakpoint number.
14132 x-y where 'x' and 'y' specify a breakpoint numbers range.
14133 x.y where 'x' is a breakpoint number and 'y' a location number.
14134 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14135 location number range.
14136 */
14137
14138 static void
14139 extract_bp_number_and_location (const std::string &arg,
14140 std::pair<int, int> &bp_num_range,
14141 std::pair<int, int> &bp_loc_range)
14142 {
14143 std::string::size_type dot = arg.find ('.');
14144
14145 if (dot != std::string::npos)
14146 {
14147 /* Handle 'x.y' and 'x.y-z' cases. */
14148
14149 if (arg.length () == dot + 1 || dot == 0)
14150 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
14151
14152 bp_num_range.first
14153 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14154 bp_num_range.second = bp_num_range.first;
14155
14156 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14157 arg, dot + 1);
14158 }
14159 else
14160 {
14161 /* Handle x and x-y cases. */
14162
14163 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
14164 bp_loc_range.first = 0;
14165 bp_loc_range.second = 0;
14166 }
14167 }
14168
14169 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14170 specifies whether to enable or disable. */
14171
14172 static void
14173 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14174 {
14175 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14176 if (loc != NULL)
14177 {
14178 if (loc->disabled_by_cond && enable)
14179 error (_("Breakpoint %d's condition is invalid at location %d, "
14180 "cannot enable."), bp_num, loc_num);
14181
14182 if (loc->enabled != enable)
14183 {
14184 loc->enabled = enable;
14185 mark_breakpoint_location_modified (loc);
14186 }
14187 if (target_supports_enable_disable_tracepoint ()
14188 && current_trace_status ()->running && loc->owner
14189 && is_tracepoint (loc->owner))
14190 target_disable_tracepoint (loc);
14191 }
14192 update_global_location_list (UGLL_DONT_INSERT);
14193
14194 gdb::observers::breakpoint_modified.notify (loc->owner);
14195 }
14196
14197 /* Enable or disable a range of breakpoint locations. BP_NUM is the
14198 number of the breakpoint, and BP_LOC_RANGE specifies the
14199 (inclusive) range of location numbers of that breakpoint to
14200 enable/disable. ENABLE specifies whether to enable or disable the
14201 location. */
14202
14203 static void
14204 enable_disable_breakpoint_location_range (int bp_num,
14205 std::pair<int, int> &bp_loc_range,
14206 bool enable)
14207 {
14208 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14209 enable_disable_bp_num_loc (bp_num, i, enable);
14210 }
14211
14212 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14213 If from_tty is nonzero, it prints a message to that effect,
14214 which ends with a period (no newline). */
14215
14216 void
14217 disable_breakpoint (struct breakpoint *bpt)
14218 {
14219 /* Never disable a watchpoint scope breakpoint; we want to
14220 hit them when we leave scope so we can delete both the
14221 watchpoint and its scope breakpoint at that time. */
14222 if (bpt->type == bp_watchpoint_scope)
14223 return;
14224
14225 bpt->enable_state = bp_disabled;
14226
14227 /* Mark breakpoint locations modified. */
14228 mark_breakpoint_modified (bpt);
14229
14230 if (target_supports_enable_disable_tracepoint ()
14231 && current_trace_status ()->running && is_tracepoint (bpt))
14232 {
14233 for (bp_location *location : bpt->locations ())
14234 target_disable_tracepoint (location);
14235 }
14236
14237 update_global_location_list (UGLL_DONT_INSERT);
14238
14239 gdb::observers::breakpoint_modified.notify (bpt);
14240 }
14241
14242 /* Enable or disable the breakpoint(s) or breakpoint location(s)
14243 specified in ARGS. ARGS may be in any of the formats handled by
14244 extract_bp_number_and_location. ENABLE specifies whether to enable
14245 or disable the breakpoints/locations. */
14246
14247 static void
14248 enable_disable_command (const char *args, int from_tty, bool enable)
14249 {
14250 if (args == 0)
14251 {
14252 for (breakpoint *bpt : all_breakpoints ())
14253 if (user_breakpoint_p (bpt))
14254 {
14255 if (enable)
14256 enable_breakpoint (bpt);
14257 else
14258 disable_breakpoint (bpt);
14259 }
14260 }
14261 else
14262 {
14263 std::string num = extract_arg (&args);
14264
14265 while (!num.empty ())
14266 {
14267 std::pair<int, int> bp_num_range, bp_loc_range;
14268
14269 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14270
14271 if (bp_loc_range.first == bp_loc_range.second
14272 && bp_loc_range.first == 0)
14273 {
14274 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14275 map_breakpoint_number_range (bp_num_range,
14276 enable
14277 ? enable_breakpoint
14278 : disable_breakpoint);
14279 }
14280 else
14281 {
14282 /* Handle breakpoint ids with formats 'x.y' or
14283 'x.y-z'. */
14284 enable_disable_breakpoint_location_range
14285 (bp_num_range.first, bp_loc_range, enable);
14286 }
14287 num = extract_arg (&args);
14288 }
14289 }
14290 }
14291
14292 /* The disable command disables the specified breakpoints/locations
14293 (or all defined breakpoints) so they're no longer effective in
14294 stopping the inferior. ARGS may be in any of the forms defined in
14295 extract_bp_number_and_location. */
14296
14297 static void
14298 disable_command (const char *args, int from_tty)
14299 {
14300 enable_disable_command (args, from_tty, false);
14301 }
14302
14303 static void
14304 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14305 int count)
14306 {
14307 int target_resources_ok;
14308
14309 if (bpt->type == bp_hardware_breakpoint)
14310 {
14311 int i;
14312 i = hw_breakpoint_used_count ();
14313 target_resources_ok =
14314 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14315 i + 1, 0);
14316 if (target_resources_ok == 0)
14317 error (_("No hardware breakpoint support in the target."));
14318 else if (target_resources_ok < 0)
14319 error (_("Hardware breakpoints used exceeds limit."));
14320 }
14321
14322 if (is_watchpoint (bpt))
14323 {
14324 /* Initialize it just to avoid a GCC false warning. */
14325 enum enable_state orig_enable_state = bp_disabled;
14326
14327 try
14328 {
14329 struct watchpoint *w = (struct watchpoint *) bpt;
14330
14331 orig_enable_state = bpt->enable_state;
14332 bpt->enable_state = bp_enabled;
14333 update_watchpoint (w, 1 /* reparse */);
14334 }
14335 catch (const gdb_exception &e)
14336 {
14337 bpt->enable_state = orig_enable_state;
14338 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14339 bpt->number);
14340 return;
14341 }
14342 }
14343
14344 bpt->enable_state = bp_enabled;
14345
14346 /* Mark breakpoint locations modified. */
14347 mark_breakpoint_modified (bpt);
14348
14349 if (target_supports_enable_disable_tracepoint ()
14350 && current_trace_status ()->running && is_tracepoint (bpt))
14351 {
14352 for (bp_location *location : bpt->locations ())
14353 target_enable_tracepoint (location);
14354 }
14355
14356 bpt->disposition = disposition;
14357 bpt->enable_count = count;
14358 update_global_location_list (UGLL_MAY_INSERT);
14359
14360 gdb::observers::breakpoint_modified.notify (bpt);
14361 }
14362
14363
14364 void
14365 enable_breakpoint (struct breakpoint *bpt)
14366 {
14367 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14368 }
14369
14370 /* The enable command enables the specified breakpoints/locations (or
14371 all defined breakpoints) so they once again become (or continue to
14372 be) effective in stopping the inferior. ARGS may be in any of the
14373 forms defined in extract_bp_number_and_location. */
14374
14375 static void
14376 enable_command (const char *args, int from_tty)
14377 {
14378 enable_disable_command (args, from_tty, true);
14379 }
14380
14381 static void
14382 enable_once_command (const char *args, int from_tty)
14383 {
14384 map_breakpoint_numbers
14385 (args, [&] (breakpoint *b)
14386 {
14387 iterate_over_related_breakpoints
14388 (b, [&] (breakpoint *bpt)
14389 {
14390 enable_breakpoint_disp (bpt, disp_disable, 1);
14391 });
14392 });
14393 }
14394
14395 static void
14396 enable_count_command (const char *args, int from_tty)
14397 {
14398 int count;
14399
14400 if (args == NULL)
14401 error_no_arg (_("hit count"));
14402
14403 count = get_number (&args);
14404
14405 map_breakpoint_numbers
14406 (args, [&] (breakpoint *b)
14407 {
14408 iterate_over_related_breakpoints
14409 (b, [&] (breakpoint *bpt)
14410 {
14411 enable_breakpoint_disp (bpt, disp_disable, count);
14412 });
14413 });
14414 }
14415
14416 static void
14417 enable_delete_command (const char *args, int from_tty)
14418 {
14419 map_breakpoint_numbers
14420 (args, [&] (breakpoint *b)
14421 {
14422 iterate_over_related_breakpoints
14423 (b, [&] (breakpoint *bpt)
14424 {
14425 enable_breakpoint_disp (bpt, disp_del, 1);
14426 });
14427 });
14428 }
14429 \f
14430 /* Invalidate last known value of any hardware watchpoint if
14431 the memory which that value represents has been written to by
14432 GDB itself. */
14433
14434 static void
14435 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14436 CORE_ADDR addr, ssize_t len,
14437 const bfd_byte *data)
14438 {
14439 for (breakpoint *bp : all_breakpoints ())
14440 if (bp->enable_state == bp_enabled
14441 && bp->type == bp_hardware_watchpoint)
14442 {
14443 struct watchpoint *wp = (struct watchpoint *) bp;
14444
14445 if (wp->val_valid && wp->val != nullptr)
14446 {
14447 for (bp_location *loc : bp->locations ())
14448 if (loc->loc_type == bp_loc_hardware_watchpoint
14449 && loc->address + loc->length > addr
14450 && addr + len > loc->address)
14451 {
14452 wp->val = NULL;
14453 wp->val_valid = false;
14454 }
14455 }
14456 }
14457 }
14458
14459 /* Create and insert a breakpoint for software single step. */
14460
14461 void
14462 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14463 const address_space *aspace,
14464 CORE_ADDR next_pc)
14465 {
14466 struct thread_info *tp = inferior_thread ();
14467 struct symtab_and_line sal;
14468 CORE_ADDR pc = next_pc;
14469
14470 if (tp->control.single_step_breakpoints == NULL)
14471 {
14472 tp->control.single_step_breakpoints
14473 = new_single_step_breakpoint (tp->global_num, gdbarch);
14474 }
14475
14476 sal = find_pc_line (pc, 0);
14477 sal.pc = pc;
14478 sal.section = find_pc_overlay (pc);
14479 sal.explicit_pc = 1;
14480 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14481
14482 update_global_location_list (UGLL_INSERT);
14483 }
14484
14485 /* Insert single step breakpoints according to the current state. */
14486
14487 int
14488 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14489 {
14490 struct regcache *regcache = get_current_regcache ();
14491 std::vector<CORE_ADDR> next_pcs;
14492
14493 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14494
14495 if (!next_pcs.empty ())
14496 {
14497 struct frame_info *frame = get_current_frame ();
14498 const address_space *aspace = get_frame_address_space (frame);
14499
14500 for (CORE_ADDR pc : next_pcs)
14501 insert_single_step_breakpoint (gdbarch, aspace, pc);
14502
14503 return 1;
14504 }
14505 else
14506 return 0;
14507 }
14508
14509 /* See breakpoint.h. */
14510
14511 int
14512 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14513 const address_space *aspace,
14514 CORE_ADDR pc)
14515 {
14516 for (bp_location *loc : bp->locations ())
14517 if (loc->inserted
14518 && breakpoint_location_address_match (loc, aspace, pc))
14519 return 1;
14520
14521 return 0;
14522 }
14523
14524 /* Check whether a software single-step breakpoint is inserted at
14525 PC. */
14526
14527 int
14528 single_step_breakpoint_inserted_here_p (const address_space *aspace,
14529 CORE_ADDR pc)
14530 {
14531 for (breakpoint *bpt : all_breakpoints ())
14532 {
14533 if (bpt->type == bp_single_step
14534 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14535 return 1;
14536 }
14537 return 0;
14538 }
14539
14540 /* Tracepoint-specific operations. */
14541
14542 /* Set tracepoint count to NUM. */
14543 static void
14544 set_tracepoint_count (int num)
14545 {
14546 tracepoint_count = num;
14547 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14548 }
14549
14550 static void
14551 trace_command (const char *arg, int from_tty)
14552 {
14553 event_location_up location = string_to_event_location (&arg,
14554 current_language);
14555 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
14556 (location.get (), true /* is_tracepoint */);
14557
14558 create_breakpoint (get_current_arch (),
14559 location.get (),
14560 NULL, 0, arg, false, 1 /* parse arg */,
14561 0 /* tempflag */,
14562 bp_tracepoint /* type_wanted */,
14563 0 /* Ignore count */,
14564 pending_break_support,
14565 ops,
14566 from_tty,
14567 1 /* enabled */,
14568 0 /* internal */, 0);
14569 }
14570
14571 static void
14572 ftrace_command (const char *arg, int from_tty)
14573 {
14574 event_location_up location = string_to_event_location (&arg,
14575 current_language);
14576 create_breakpoint (get_current_arch (),
14577 location.get (),
14578 NULL, 0, arg, false, 1 /* parse arg */,
14579 0 /* tempflag */,
14580 bp_fast_tracepoint /* type_wanted */,
14581 0 /* Ignore count */,
14582 pending_break_support,
14583 &tracepoint_breakpoint_ops,
14584 from_tty,
14585 1 /* enabled */,
14586 0 /* internal */, 0);
14587 }
14588
14589 /* strace command implementation. Creates a static tracepoint. */
14590
14591 static void
14592 strace_command (const char *arg, int from_tty)
14593 {
14594 struct breakpoint_ops *ops;
14595 event_location_up location;
14596
14597 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14598 or with a normal static tracepoint. */
14599 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14600 {
14601 ops = &strace_marker_breakpoint_ops;
14602 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
14603 }
14604 else
14605 {
14606 ops = &tracepoint_breakpoint_ops;
14607 location = string_to_event_location (&arg, current_language);
14608 }
14609
14610 create_breakpoint (get_current_arch (),
14611 location.get (),
14612 NULL, 0, arg, false, 1 /* parse arg */,
14613 0 /* tempflag */,
14614 bp_static_tracepoint /* type_wanted */,
14615 0 /* Ignore count */,
14616 pending_break_support,
14617 ops,
14618 from_tty,
14619 1 /* enabled */,
14620 0 /* internal */, 0);
14621 }
14622
14623 /* Set up a fake reader function that gets command lines from a linked
14624 list that was acquired during tracepoint uploading. */
14625
14626 static struct uploaded_tp *this_utp;
14627 static int next_cmd;
14628
14629 static char *
14630 read_uploaded_action (void)
14631 {
14632 char *rslt = nullptr;
14633
14634 if (next_cmd < this_utp->cmd_strings.size ())
14635 {
14636 rslt = this_utp->cmd_strings[next_cmd].get ();
14637 next_cmd++;
14638 }
14639
14640 return rslt;
14641 }
14642
14643 /* Given information about a tracepoint as recorded on a target (which
14644 can be either a live system or a trace file), attempt to create an
14645 equivalent GDB tracepoint. This is not a reliable process, since
14646 the target does not necessarily have all the information used when
14647 the tracepoint was originally defined. */
14648
14649 struct tracepoint *
14650 create_tracepoint_from_upload (struct uploaded_tp *utp)
14651 {
14652 const char *addr_str;
14653 char small_buf[100];
14654 struct tracepoint *tp;
14655
14656 if (utp->at_string)
14657 addr_str = utp->at_string.get ();
14658 else
14659 {
14660 /* In the absence of a source location, fall back to raw
14661 address. Since there is no way to confirm that the address
14662 means the same thing as when the trace was started, warn the
14663 user. */
14664 warning (_("Uploaded tracepoint %d has no "
14665 "source location, using raw address"),
14666 utp->number);
14667 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14668 addr_str = small_buf;
14669 }
14670
14671 /* There's not much we can do with a sequence of bytecodes. */
14672 if (utp->cond && !utp->cond_string)
14673 warning (_("Uploaded tracepoint %d condition "
14674 "has no source form, ignoring it"),
14675 utp->number);
14676
14677 event_location_up location = string_to_event_location (&addr_str,
14678 current_language);
14679 if (!create_breakpoint (get_current_arch (),
14680 location.get (),
14681 utp->cond_string.get (), -1, addr_str,
14682 false /* force_condition */,
14683 0 /* parse cond/thread */,
14684 0 /* tempflag */,
14685 utp->type /* type_wanted */,
14686 0 /* Ignore count */,
14687 pending_break_support,
14688 &tracepoint_breakpoint_ops,
14689 0 /* from_tty */,
14690 utp->enabled /* enabled */,
14691 0 /* internal */,
14692 CREATE_BREAKPOINT_FLAGS_INSERTED))
14693 return NULL;
14694
14695 /* Get the tracepoint we just created. */
14696 tp = get_tracepoint (tracepoint_count);
14697 gdb_assert (tp != NULL);
14698
14699 if (utp->pass > 0)
14700 {
14701 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14702 tp->number);
14703
14704 trace_pass_command (small_buf, 0);
14705 }
14706
14707 /* If we have uploaded versions of the original commands, set up a
14708 special-purpose "reader" function and call the usual command line
14709 reader, then pass the result to the breakpoint command-setting
14710 function. */
14711 if (!utp->cmd_strings.empty ())
14712 {
14713 counted_command_line cmd_list;
14714
14715 this_utp = utp;
14716 next_cmd = 0;
14717
14718 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
14719
14720 breakpoint_set_commands (tp, std::move (cmd_list));
14721 }
14722 else if (!utp->actions.empty ()
14723 || !utp->step_actions.empty ())
14724 warning (_("Uploaded tracepoint %d actions "
14725 "have no source form, ignoring them"),
14726 utp->number);
14727
14728 /* Copy any status information that might be available. */
14729 tp->hit_count = utp->hit_count;
14730 tp->traceframe_usage = utp->traceframe_usage;
14731
14732 return tp;
14733 }
14734
14735 /* Print information on tracepoint number TPNUM_EXP, or all if
14736 omitted. */
14737
14738 static void
14739 info_tracepoints_command (const char *args, int from_tty)
14740 {
14741 struct ui_out *uiout = current_uiout;
14742 int num_printed;
14743
14744 num_printed = breakpoint_1 (args, false, is_tracepoint);
14745
14746 if (num_printed == 0)
14747 {
14748 if (args == NULL || *args == '\0')
14749 uiout->message ("No tracepoints.\n");
14750 else
14751 uiout->message ("No tracepoint matching '%s'.\n", args);
14752 }
14753
14754 default_collect_info ();
14755 }
14756
14757 /* The 'enable trace' command enables tracepoints.
14758 Not supported by all targets. */
14759 static void
14760 enable_trace_command (const char *args, int from_tty)
14761 {
14762 enable_command (args, from_tty);
14763 }
14764
14765 /* The 'disable trace' command disables tracepoints.
14766 Not supported by all targets. */
14767 static void
14768 disable_trace_command (const char *args, int from_tty)
14769 {
14770 disable_command (args, from_tty);
14771 }
14772
14773 /* Remove a tracepoint (or all if no argument). */
14774 static void
14775 delete_trace_command (const char *arg, int from_tty)
14776 {
14777 dont_repeat ();
14778
14779 if (arg == 0)
14780 {
14781 int breaks_to_delete = 0;
14782
14783 /* Delete all breakpoints if no argument.
14784 Do not delete internal or call-dummy breakpoints, these
14785 have to be deleted with an explicit breakpoint number
14786 argument. */
14787 for (breakpoint *tp : all_tracepoints ())
14788 if (is_tracepoint (tp) && user_breakpoint_p (tp))
14789 {
14790 breaks_to_delete = 1;
14791 break;
14792 }
14793
14794 /* Ask user only if there are some breakpoints to delete. */
14795 if (!from_tty
14796 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14797 {
14798 for (breakpoint *b : all_breakpoints_safe ())
14799 if (is_tracepoint (b) && user_breakpoint_p (b))
14800 delete_breakpoint (b);
14801 }
14802 }
14803 else
14804 map_breakpoint_numbers
14805 (arg, [&] (breakpoint *br)
14806 {
14807 iterate_over_related_breakpoints (br, delete_breakpoint);
14808 });
14809 }
14810
14811 /* Helper function for trace_pass_command. */
14812
14813 static void
14814 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14815 {
14816 tp->pass_count = count;
14817 gdb::observers::breakpoint_modified.notify (tp);
14818 if (from_tty)
14819 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
14820 tp->number, count);
14821 }
14822
14823 /* Set passcount for tracepoint.
14824
14825 First command argument is passcount, second is tracepoint number.
14826 If tracepoint number omitted, apply to most recently defined.
14827 Also accepts special argument "all". */
14828
14829 static void
14830 trace_pass_command (const char *args, int from_tty)
14831 {
14832 struct tracepoint *t1;
14833 ULONGEST count;
14834
14835 if (args == 0 || *args == 0)
14836 error (_("passcount command requires an "
14837 "argument (count + optional TP num)"));
14838
14839 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
14840
14841 args = skip_spaces (args);
14842 if (*args && strncasecmp (args, "all", 3) == 0)
14843 {
14844 args += 3; /* Skip special argument "all". */
14845 if (*args)
14846 error (_("Junk at end of arguments."));
14847
14848 for (breakpoint *b : all_tracepoints ())
14849 {
14850 t1 = (struct tracepoint *) b;
14851 trace_pass_set_count (t1, count, from_tty);
14852 }
14853 }
14854 else if (*args == '\0')
14855 {
14856 t1 = get_tracepoint_by_number (&args, NULL);
14857 if (t1)
14858 trace_pass_set_count (t1, count, from_tty);
14859 }
14860 else
14861 {
14862 number_or_range_parser parser (args);
14863 while (!parser.finished ())
14864 {
14865 t1 = get_tracepoint_by_number (&args, &parser);
14866 if (t1)
14867 trace_pass_set_count (t1, count, from_tty);
14868 }
14869 }
14870 }
14871
14872 struct tracepoint *
14873 get_tracepoint (int num)
14874 {
14875 for (breakpoint *t : all_tracepoints ())
14876 if (t->number == num)
14877 return (struct tracepoint *) t;
14878
14879 return NULL;
14880 }
14881
14882 /* Find the tracepoint with the given target-side number (which may be
14883 different from the tracepoint number after disconnecting and
14884 reconnecting). */
14885
14886 struct tracepoint *
14887 get_tracepoint_by_number_on_target (int num)
14888 {
14889 for (breakpoint *b : all_tracepoints ())
14890 {
14891 struct tracepoint *t = (struct tracepoint *) b;
14892
14893 if (t->number_on_target == num)
14894 return t;
14895 }
14896
14897 return NULL;
14898 }
14899
14900 /* Utility: parse a tracepoint number and look it up in the list.
14901 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
14902 If the argument is missing, the most recent tracepoint
14903 (tracepoint_count) is returned. */
14904
14905 struct tracepoint *
14906 get_tracepoint_by_number (const char **arg,
14907 number_or_range_parser *parser)
14908 {
14909 int tpnum;
14910 const char *instring = arg == NULL ? NULL : *arg;
14911
14912 if (parser != NULL)
14913 {
14914 gdb_assert (!parser->finished ());
14915 tpnum = parser->get_number ();
14916 }
14917 else if (arg == NULL || *arg == NULL || ! **arg)
14918 tpnum = tracepoint_count;
14919 else
14920 tpnum = get_number (arg);
14921
14922 if (tpnum <= 0)
14923 {
14924 if (instring && *instring)
14925 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
14926 instring);
14927 else
14928 printf_filtered (_("No previous tracepoint\n"));
14929 return NULL;
14930 }
14931
14932 for (breakpoint *t : all_tracepoints ())
14933 if (t->number == tpnum)
14934 {
14935 return (struct tracepoint *) t;
14936 }
14937
14938 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
14939 return NULL;
14940 }
14941
14942 void
14943 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
14944 {
14945 if (b->thread != -1)
14946 fprintf_unfiltered (fp, " thread %d", b->thread);
14947
14948 if (b->task != 0)
14949 fprintf_unfiltered (fp, " task %d", b->task);
14950
14951 fprintf_unfiltered (fp, "\n");
14952 }
14953
14954 /* Save information on user settable breakpoints (watchpoints, etc) to
14955 a new script file named FILENAME. If FILTER is non-NULL, call it
14956 on each breakpoint and only include the ones for which it returns
14957 true. */
14958
14959 static void
14960 save_breakpoints (const char *filename, int from_tty,
14961 bool (*filter) (const struct breakpoint *))
14962 {
14963 int any = 0;
14964 int extra_trace_bits = 0;
14965
14966 if (filename == 0 || *filename == 0)
14967 error (_("Argument required (file name in which to save)"));
14968
14969 /* See if we have anything to save. */
14970 for (breakpoint *tp : all_breakpoints ())
14971 {
14972 /* Skip internal and momentary breakpoints. */
14973 if (!user_breakpoint_p (tp))
14974 continue;
14975
14976 /* If we have a filter, only save the breakpoints it accepts. */
14977 if (filter && !filter (tp))
14978 continue;
14979
14980 any = 1;
14981
14982 if (is_tracepoint (tp))
14983 {
14984 extra_trace_bits = 1;
14985
14986 /* We can stop searching. */
14987 break;
14988 }
14989 }
14990
14991 if (!any)
14992 {
14993 warning (_("Nothing to save."));
14994 return;
14995 }
14996
14997 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
14998
14999 stdio_file fp;
15000
15001 if (!fp.open (expanded_filename.get (), "w"))
15002 error (_("Unable to open file '%s' for saving (%s)"),
15003 expanded_filename.get (), safe_strerror (errno));
15004
15005 if (extra_trace_bits)
15006 save_trace_state_variables (&fp);
15007
15008 for (breakpoint *tp : all_breakpoints ())
15009 {
15010 /* Skip internal and momentary breakpoints. */
15011 if (!user_breakpoint_p (tp))
15012 continue;
15013
15014 /* If we have a filter, only save the breakpoints it accepts. */
15015 if (filter && !filter (tp))
15016 continue;
15017
15018 tp->ops->print_recreate (tp, &fp);
15019
15020 /* Note, we can't rely on tp->number for anything, as we can't
15021 assume the recreated breakpoint numbers will match. Use $bpnum
15022 instead. */
15023
15024 if (tp->cond_string)
15025 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15026
15027 if (tp->ignore_count)
15028 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15029
15030 if (tp->type != bp_dprintf && tp->commands)
15031 {
15032 fp.puts (" commands\n");
15033
15034 current_uiout->redirect (&fp);
15035 try
15036 {
15037 print_command_lines (current_uiout, tp->commands.get (), 2);
15038 }
15039 catch (const gdb_exception &ex)
15040 {
15041 current_uiout->redirect (NULL);
15042 throw;
15043 }
15044
15045 current_uiout->redirect (NULL);
15046 fp.puts (" end\n");
15047 }
15048
15049 if (tp->enable_state == bp_disabled)
15050 fp.puts ("disable $bpnum\n");
15051
15052 /* If this is a multi-location breakpoint, check if the locations
15053 should be individually disabled. Watchpoint locations are
15054 special, and not user visible. */
15055 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15056 {
15057 int n = 1;
15058
15059 for (bp_location *loc : tp->locations ())
15060 {
15061 if (!loc->enabled)
15062 fp.printf ("disable $bpnum.%d\n", n);
15063
15064 n++;
15065 }
15066 }
15067 }
15068
15069 if (extra_trace_bits && *default_collect)
15070 fp.printf ("set default-collect %s\n", default_collect);
15071
15072 if (from_tty)
15073 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
15074 }
15075
15076 /* The `save breakpoints' command. */
15077
15078 static void
15079 save_breakpoints_command (const char *args, int from_tty)
15080 {
15081 save_breakpoints (args, from_tty, NULL);
15082 }
15083
15084 /* The `save tracepoints' command. */
15085
15086 static void
15087 save_tracepoints_command (const char *args, int from_tty)
15088 {
15089 save_breakpoints (args, from_tty, is_tracepoint);
15090 }
15091
15092 \f
15093 /* This help string is used to consolidate all the help string for specifying
15094 locations used by several commands. */
15095
15096 #define LOCATION_HELP_STRING \
15097 "Linespecs are colon-separated lists of location parameters, such as\n\
15098 source filename, function name, label name, and line number.\n\
15099 Example: To specify the start of a label named \"the_top\" in the\n\
15100 function \"fact\" in the file \"factorial.c\", use\n\
15101 \"factorial.c:fact:the_top\".\n\
15102 \n\
15103 Address locations begin with \"*\" and specify an exact address in the\n\
15104 program. Example: To specify the fourth byte past the start function\n\
15105 \"main\", use \"*main + 4\".\n\
15106 \n\
15107 Explicit locations are similar to linespecs but use an option/argument\n\
15108 syntax to specify location parameters.\n\
15109 Example: To specify the start of the label named \"the_top\" in the\n\
15110 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15111 -function fact -label the_top\".\n\
15112 \n\
15113 By default, a specified function is matched against the program's\n\
15114 functions in all scopes. For C++, this means in all namespaces and\n\
15115 classes. For Ada, this means in all packages. E.g., in C++,\n\
15116 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15117 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
15118 specified name as a complete fully-qualified name instead."
15119
15120 /* This help string is used for the break, hbreak, tbreak and thbreak
15121 commands. It is defined as a macro to prevent duplication.
15122 COMMAND should be a string constant containing the name of the
15123 command. */
15124
15125 #define BREAK_ARGS_HELP(command) \
15126 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
15127 \t[-force-condition] [if CONDITION]\n\
15128 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15129 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15130 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15131 `-probe-dtrace' (for a DTrace probe).\n\
15132 LOCATION may be a linespec, address, or explicit location as described\n\
15133 below.\n\
15134 \n\
15135 With no LOCATION, uses current execution address of the selected\n\
15136 stack frame. This is useful for breaking on return to a stack frame.\n\
15137 \n\
15138 THREADNUM is the number from \"info threads\".\n\
15139 CONDITION is a boolean expression.\n\
15140 \n\
15141 With the \"-force-condition\" flag, the condition is defined even when\n\
15142 it is invalid for all current locations.\n\
15143 \n" LOCATION_HELP_STRING "\n\n\
15144 Multiple breakpoints at one place are permitted, and useful if their\n\
15145 conditions are different.\n\
15146 \n\
15147 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15148
15149 /* List of subcommands for "catch". */
15150 static struct cmd_list_element *catch_cmdlist;
15151
15152 /* List of subcommands for "tcatch". */
15153 static struct cmd_list_element *tcatch_cmdlist;
15154
15155 void
15156 add_catch_command (const char *name, const char *docstring,
15157 cmd_const_sfunc_ftype *sfunc,
15158 completer_ftype *completer,
15159 void *user_data_catch,
15160 void *user_data_tcatch)
15161 {
15162 struct cmd_list_element *command;
15163
15164 command = add_cmd (name, class_breakpoint, docstring,
15165 &catch_cmdlist);
15166 set_cmd_sfunc (command, sfunc);
15167 set_cmd_context (command, user_data_catch);
15168 set_cmd_completer (command, completer);
15169
15170 command = add_cmd (name, class_breakpoint, docstring,
15171 &tcatch_cmdlist);
15172 set_cmd_sfunc (command, sfunc);
15173 set_cmd_context (command, user_data_tcatch);
15174 set_cmd_completer (command, completer);
15175 }
15176
15177 /* Zero if any of the breakpoint's locations could be a location where
15178 functions have been inlined, nonzero otherwise. */
15179
15180 static int
15181 is_non_inline_function (struct breakpoint *b)
15182 {
15183 /* The shared library event breakpoint is set on the address of a
15184 non-inline function. */
15185 if (b->type == bp_shlib_event)
15186 return 1;
15187
15188 return 0;
15189 }
15190
15191 /* Nonzero if the specified PC cannot be a location where functions
15192 have been inlined. */
15193
15194 int
15195 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
15196 const struct target_waitstatus *ws)
15197 {
15198 for (breakpoint *b : all_breakpoints ())
15199 {
15200 if (!is_non_inline_function (b))
15201 continue;
15202
15203 for (bp_location *bl : b->locations ())
15204 {
15205 if (!bl->shlib_disabled
15206 && bpstat_check_location (bl, aspace, pc, ws))
15207 return 1;
15208 }
15209 }
15210
15211 return 0;
15212 }
15213
15214 /* Remove any references to OBJFILE which is going to be freed. */
15215
15216 void
15217 breakpoint_free_objfile (struct objfile *objfile)
15218 {
15219 for (bp_location *loc : all_bp_locations ())
15220 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15221 loc->symtab = NULL;
15222 }
15223
15224 void
15225 initialize_breakpoint_ops (void)
15226 {
15227 static int initialized = 0;
15228
15229 struct breakpoint_ops *ops;
15230
15231 if (initialized)
15232 return;
15233 initialized = 1;
15234
15235 /* The breakpoint_ops structure to be inherit by all kinds of
15236 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15237 internal and momentary breakpoints, etc.). */
15238 ops = &bkpt_base_breakpoint_ops;
15239 *ops = base_breakpoint_ops;
15240 ops->re_set = bkpt_re_set;
15241 ops->insert_location = bkpt_insert_location;
15242 ops->remove_location = bkpt_remove_location;
15243 ops->breakpoint_hit = bkpt_breakpoint_hit;
15244 ops->create_sals_from_location = bkpt_create_sals_from_location;
15245 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15246 ops->decode_location = bkpt_decode_location;
15247
15248 /* The breakpoint_ops structure to be used in regular breakpoints. */
15249 ops = &bkpt_breakpoint_ops;
15250 *ops = bkpt_base_breakpoint_ops;
15251 ops->re_set = bkpt_re_set;
15252 ops->resources_needed = bkpt_resources_needed;
15253 ops->print_it = bkpt_print_it;
15254 ops->print_mention = bkpt_print_mention;
15255 ops->print_recreate = bkpt_print_recreate;
15256
15257 /* Ranged breakpoints. */
15258 ops = &ranged_breakpoint_ops;
15259 *ops = bkpt_breakpoint_ops;
15260 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15261 ops->resources_needed = resources_needed_ranged_breakpoint;
15262 ops->print_it = print_it_ranged_breakpoint;
15263 ops->print_one = print_one_ranged_breakpoint;
15264 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15265 ops->print_mention = print_mention_ranged_breakpoint;
15266 ops->print_recreate = print_recreate_ranged_breakpoint;
15267
15268 /* Internal breakpoints. */
15269 ops = &internal_breakpoint_ops;
15270 *ops = bkpt_base_breakpoint_ops;
15271 ops->re_set = internal_bkpt_re_set;
15272 ops->check_status = internal_bkpt_check_status;
15273 ops->print_it = internal_bkpt_print_it;
15274 ops->print_mention = internal_bkpt_print_mention;
15275
15276 /* Momentary breakpoints. */
15277 ops = &momentary_breakpoint_ops;
15278 *ops = bkpt_base_breakpoint_ops;
15279 ops->re_set = momentary_bkpt_re_set;
15280 ops->check_status = momentary_bkpt_check_status;
15281 ops->print_it = momentary_bkpt_print_it;
15282 ops->print_mention = momentary_bkpt_print_mention;
15283
15284 /* Probe breakpoints. */
15285 ops = &bkpt_probe_breakpoint_ops;
15286 *ops = bkpt_breakpoint_ops;
15287 ops->insert_location = bkpt_probe_insert_location;
15288 ops->remove_location = bkpt_probe_remove_location;
15289 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15290 ops->decode_location = bkpt_probe_decode_location;
15291
15292 /* Watchpoints. */
15293 ops = &watchpoint_breakpoint_ops;
15294 *ops = base_breakpoint_ops;
15295 ops->re_set = re_set_watchpoint;
15296 ops->insert_location = insert_watchpoint;
15297 ops->remove_location = remove_watchpoint;
15298 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15299 ops->check_status = check_status_watchpoint;
15300 ops->resources_needed = resources_needed_watchpoint;
15301 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15302 ops->print_it = print_it_watchpoint;
15303 ops->print_mention = print_mention_watchpoint;
15304 ops->print_recreate = print_recreate_watchpoint;
15305 ops->explains_signal = explains_signal_watchpoint;
15306
15307 /* Masked watchpoints. */
15308 ops = &masked_watchpoint_breakpoint_ops;
15309 *ops = watchpoint_breakpoint_ops;
15310 ops->insert_location = insert_masked_watchpoint;
15311 ops->remove_location = remove_masked_watchpoint;
15312 ops->resources_needed = resources_needed_masked_watchpoint;
15313 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15314 ops->print_it = print_it_masked_watchpoint;
15315 ops->print_one_detail = print_one_detail_masked_watchpoint;
15316 ops->print_mention = print_mention_masked_watchpoint;
15317 ops->print_recreate = print_recreate_masked_watchpoint;
15318
15319 /* Tracepoints. */
15320 ops = &tracepoint_breakpoint_ops;
15321 *ops = base_breakpoint_ops;
15322 ops->re_set = tracepoint_re_set;
15323 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15324 ops->print_one_detail = tracepoint_print_one_detail;
15325 ops->print_mention = tracepoint_print_mention;
15326 ops->print_recreate = tracepoint_print_recreate;
15327 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15328 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15329 ops->decode_location = tracepoint_decode_location;
15330
15331 /* Probe tracepoints. */
15332 ops = &tracepoint_probe_breakpoint_ops;
15333 *ops = tracepoint_breakpoint_ops;
15334 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15335 ops->decode_location = tracepoint_probe_decode_location;
15336
15337 /* Static tracepoints with marker (`-m'). */
15338 ops = &strace_marker_breakpoint_ops;
15339 *ops = tracepoint_breakpoint_ops;
15340 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15341 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15342 ops->decode_location = strace_marker_decode_location;
15343
15344 /* Fork catchpoints. */
15345 ops = &catch_fork_breakpoint_ops;
15346 *ops = base_breakpoint_ops;
15347 ops->insert_location = insert_catch_fork;
15348 ops->remove_location = remove_catch_fork;
15349 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15350 ops->print_it = print_it_catch_fork;
15351 ops->print_one = print_one_catch_fork;
15352 ops->print_mention = print_mention_catch_fork;
15353 ops->print_recreate = print_recreate_catch_fork;
15354
15355 /* Vfork catchpoints. */
15356 ops = &catch_vfork_breakpoint_ops;
15357 *ops = base_breakpoint_ops;
15358 ops->insert_location = insert_catch_vfork;
15359 ops->remove_location = remove_catch_vfork;
15360 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15361 ops->print_it = print_it_catch_vfork;
15362 ops->print_one = print_one_catch_vfork;
15363 ops->print_mention = print_mention_catch_vfork;
15364 ops->print_recreate = print_recreate_catch_vfork;
15365
15366 /* Exec catchpoints. */
15367 ops = &catch_exec_breakpoint_ops;
15368 *ops = base_breakpoint_ops;
15369 ops->insert_location = insert_catch_exec;
15370 ops->remove_location = remove_catch_exec;
15371 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15372 ops->print_it = print_it_catch_exec;
15373 ops->print_one = print_one_catch_exec;
15374 ops->print_mention = print_mention_catch_exec;
15375 ops->print_recreate = print_recreate_catch_exec;
15376
15377 /* Solib-related catchpoints. */
15378 ops = &catch_solib_breakpoint_ops;
15379 *ops = base_breakpoint_ops;
15380 ops->insert_location = insert_catch_solib;
15381 ops->remove_location = remove_catch_solib;
15382 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15383 ops->check_status = check_status_catch_solib;
15384 ops->print_it = print_it_catch_solib;
15385 ops->print_one = print_one_catch_solib;
15386 ops->print_mention = print_mention_catch_solib;
15387 ops->print_recreate = print_recreate_catch_solib;
15388
15389 ops = &dprintf_breakpoint_ops;
15390 *ops = bkpt_base_breakpoint_ops;
15391 ops->re_set = dprintf_re_set;
15392 ops->resources_needed = bkpt_resources_needed;
15393 ops->print_it = bkpt_print_it;
15394 ops->print_mention = bkpt_print_mention;
15395 ops->print_recreate = dprintf_print_recreate;
15396 ops->after_condition_true = dprintf_after_condition_true;
15397 ops->breakpoint_hit = dprintf_breakpoint_hit;
15398 }
15399
15400 /* Chain containing all defined "enable breakpoint" subcommands. */
15401
15402 static struct cmd_list_element *enablebreaklist = NULL;
15403
15404 /* See breakpoint.h. */
15405
15406 cmd_list_element *commands_cmd_element = nullptr;
15407
15408 void _initialize_breakpoint ();
15409 void
15410 _initialize_breakpoint ()
15411 {
15412 struct cmd_list_element *c;
15413
15414 initialize_breakpoint_ops ();
15415
15416 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
15417 "breakpoint");
15418 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
15419 "breakpoint");
15420 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
15421 "breakpoint");
15422
15423 breakpoint_chain = 0;
15424 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15425 before a breakpoint is set. */
15426 breakpoint_count = 0;
15427
15428 tracepoint_count = 0;
15429
15430 add_com ("ignore", class_breakpoint, ignore_command, _("\
15431 Set ignore-count of breakpoint number N to COUNT.\n\
15432 Usage is `ignore N COUNT'."));
15433
15434 commands_cmd_element = add_com ("commands", class_breakpoint,
15435 commands_command, _("\
15436 Set commands to be executed when the given breakpoints are hit.\n\
15437 Give a space-separated breakpoint list as argument after \"commands\".\n\
15438 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15439 (e.g. `5-7').\n\
15440 With no argument, the targeted breakpoint is the last one set.\n\
15441 The commands themselves follow starting on the next line.\n\
15442 Type a line containing \"end\" to indicate the end of them.\n\
15443 Give \"silent\" as the first line to make the breakpoint silent;\n\
15444 then no output is printed when it is hit, except what the commands print."));
15445
15446 const auto cc_opts = make_condition_command_options_def_group (nullptr);
15447 static std::string condition_command_help
15448 = gdb::option::build_help (_("\
15449 Specify breakpoint number N to break only if COND is true.\n\
15450 Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
15451 is an expression to be evaluated whenever breakpoint N is reached.\n\
15452 \n\
15453 Options:\n\
15454 %OPTIONS%"), cc_opts);
15455
15456 c = add_com ("condition", class_breakpoint, condition_command,
15457 condition_command_help.c_str ());
15458 set_cmd_completer_handle_brkchars (c, condition_completer);
15459
15460 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15461 Set a temporary breakpoint.\n\
15462 Like \"break\" except the breakpoint is only temporary,\n\
15463 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15464 by using \"enable delete\" on the breakpoint number.\n\
15465 \n"
15466 BREAK_ARGS_HELP ("tbreak")));
15467 set_cmd_completer (c, location_completer);
15468
15469 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15470 Set a hardware assisted breakpoint.\n\
15471 Like \"break\" except the breakpoint requires hardware support,\n\
15472 some target hardware may not have this support.\n\
15473 \n"
15474 BREAK_ARGS_HELP ("hbreak")));
15475 set_cmd_completer (c, location_completer);
15476
15477 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15478 Set a temporary hardware assisted breakpoint.\n\
15479 Like \"hbreak\" except the breakpoint is only temporary,\n\
15480 so it will be deleted when hit.\n\
15481 \n"
15482 BREAK_ARGS_HELP ("thbreak")));
15483 set_cmd_completer (c, location_completer);
15484
15485 cmd_list_element *enable_cmd
15486 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15487 Enable all or some breakpoints.\n\
15488 Usage: enable [BREAKPOINTNUM]...\n\
15489 Give breakpoint numbers (separated by spaces) as arguments.\n\
15490 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15491 This is used to cancel the effect of the \"disable\" command.\n\
15492 With a subcommand you can enable temporarily."),
15493 &enablelist, 1, &cmdlist);
15494
15495 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
15496
15497 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15498 Enable all or some breakpoints.\n\
15499 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
15500 Give breakpoint numbers (separated by spaces) as arguments.\n\
15501 This is used to cancel the effect of the \"disable\" command.\n\
15502 May be abbreviated to simply \"enable\"."),
15503 &enablebreaklist, 1, &enablelist);
15504
15505 add_cmd ("once", no_class, enable_once_command, _("\
15506 Enable some breakpoints for one hit.\n\
15507 Usage: enable breakpoints once BREAKPOINTNUM...\n\
15508 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15509 &enablebreaklist);
15510
15511 add_cmd ("delete", no_class, enable_delete_command, _("\
15512 Enable some breakpoints and delete when hit.\n\
15513 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
15514 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15515 &enablebreaklist);
15516
15517 add_cmd ("count", no_class, enable_count_command, _("\
15518 Enable some breakpoints for COUNT hits.\n\
15519 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
15520 If a breakpoint is hit while enabled in this fashion,\n\
15521 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15522 &enablebreaklist);
15523
15524 add_cmd ("delete", no_class, enable_delete_command, _("\
15525 Enable some breakpoints and delete when hit.\n\
15526 Usage: enable delete BREAKPOINTNUM...\n\
15527 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15528 &enablelist);
15529
15530 add_cmd ("once", no_class, enable_once_command, _("\
15531 Enable some breakpoints for one hit.\n\
15532 Usage: enable once BREAKPOINTNUM...\n\
15533 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15534 &enablelist);
15535
15536 add_cmd ("count", no_class, enable_count_command, _("\
15537 Enable some breakpoints for COUNT hits.\n\
15538 Usage: enable count COUNT BREAKPOINTNUM...\n\
15539 If a breakpoint is hit while enabled in this fashion,\n\
15540 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15541 &enablelist);
15542
15543 cmd_list_element *disable_cmd
15544 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15545 Disable all or some breakpoints.\n\
15546 Usage: disable [BREAKPOINTNUM]...\n\
15547 Arguments are breakpoint numbers with spaces in between.\n\
15548 To disable all breakpoints, give no argument.\n\
15549 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15550 &disablelist, 1, &cmdlist);
15551 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
15552 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
15553
15554 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
15555 Disable all or some breakpoints.\n\
15556 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
15557 Arguments are breakpoint numbers with spaces in between.\n\
15558 To disable all breakpoints, give no argument.\n\
15559 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15560 This command may be abbreviated \"disable\"."),
15561 &disablelist);
15562
15563 cmd_list_element *delete_cmd
15564 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15565 Delete all or some breakpoints.\n\
15566 Usage: delete [BREAKPOINTNUM]...\n\
15567 Arguments are breakpoint numbers with spaces in between.\n\
15568 To delete all breakpoints, give no argument.\n\
15569 \n\
15570 Also a prefix command for deletion of other GDB objects."),
15571 &deletelist, 1, &cmdlist);
15572 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
15573 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
15574
15575 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
15576 Delete all or some breakpoints or auto-display expressions.\n\
15577 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
15578 Arguments are breakpoint numbers with spaces in between.\n\
15579 To delete all breakpoints, give no argument.\n\
15580 This command may be abbreviated \"delete\"."),
15581 &deletelist);
15582
15583 cmd_list_element *clear_cmd
15584 = add_com ("clear", class_breakpoint, clear_command, _("\
15585 Clear breakpoint at specified location.\n\
15586 Argument may be a linespec, explicit, or address location as described below.\n\
15587 \n\
15588 With no argument, clears all breakpoints in the line that the selected frame\n\
15589 is executing in.\n"
15590 "\n" LOCATION_HELP_STRING "\n\n\
15591 See also the \"delete\" command which clears breakpoints by number."));
15592 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
15593
15594 cmd_list_element *break_cmd
15595 = add_com ("break", class_breakpoint, break_command, _("\
15596 Set breakpoint at specified location.\n"
15597 BREAK_ARGS_HELP ("break")));
15598 set_cmd_completer (break_cmd, location_completer);
15599
15600 add_com_alias ("b", break_cmd, class_run, 1);
15601 add_com_alias ("br", break_cmd, class_run, 1);
15602 add_com_alias ("bre", break_cmd, class_run, 1);
15603 add_com_alias ("brea", break_cmd, class_run, 1);
15604
15605 if (dbx_commands)
15606 {
15607 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15608 Break in function/address or break at a line in the current file."),
15609 &stoplist, 1, &cmdlist);
15610 add_cmd ("in", class_breakpoint, stopin_command,
15611 _("Break in function or address."), &stoplist);
15612 add_cmd ("at", class_breakpoint, stopat_command,
15613 _("Break at a line in the current file."), &stoplist);
15614 add_com ("status", class_info, info_breakpoints_command, _("\
15615 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15616 The \"Type\" column indicates one of:\n\
15617 \tbreakpoint - normal breakpoint\n\
15618 \twatchpoint - watchpoint\n\
15619 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15620 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15621 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15622 address and file/line number respectively.\n\
15623 \n\
15624 Convenience variable \"$_\" and default examine address for \"x\"\n\
15625 are set to the address of the last breakpoint listed unless the command\n\
15626 is prefixed with \"server \".\n\n\
15627 Convenience variable \"$bpnum\" contains the number of the last\n\
15628 breakpoint set."));
15629 }
15630
15631 cmd_list_element *info_breakpoints_cmd
15632 = add_info ("breakpoints", info_breakpoints_command, _("\
15633 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15634 The \"Type\" column indicates one of:\n\
15635 \tbreakpoint - normal breakpoint\n\
15636 \twatchpoint - watchpoint\n\
15637 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15638 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15639 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15640 address and file/line number respectively.\n\
15641 \n\
15642 Convenience variable \"$_\" and default examine address for \"x\"\n\
15643 are set to the address of the last breakpoint listed unless the command\n\
15644 is prefixed with \"server \".\n\n\
15645 Convenience variable \"$bpnum\" contains the number of the last\n\
15646 breakpoint set."));
15647
15648 add_info_alias ("b", info_breakpoints_cmd, 1);
15649
15650 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15651 Status of all breakpoints, or breakpoint number NUMBER.\n\
15652 The \"Type\" column indicates one of:\n\
15653 \tbreakpoint - normal breakpoint\n\
15654 \twatchpoint - watchpoint\n\
15655 \tlongjmp - internal breakpoint used to step through longjmp()\n\
15656 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15657 \tuntil - internal breakpoint used by the \"until\" command\n\
15658 \tfinish - internal breakpoint used by the \"finish\" command\n\
15659 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15660 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15661 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15662 address and file/line number respectively.\n\
15663 \n\
15664 Convenience variable \"$_\" and default examine address for \"x\"\n\
15665 are set to the address of the last breakpoint listed unless the command\n\
15666 is prefixed with \"server \".\n\n\
15667 Convenience variable \"$bpnum\" contains the number of the last\n\
15668 breakpoint set."),
15669 &maintenanceinfolist);
15670
15671 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
15672 Set catchpoints to catch events."),
15673 &catch_cmdlist,
15674 0/*allow-unknown*/, &cmdlist);
15675
15676 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
15677 Set temporary catchpoints to catch events."),
15678 &tcatch_cmdlist,
15679 0/*allow-unknown*/, &cmdlist);
15680
15681 add_catch_command ("fork", _("Catch calls to fork."),
15682 catch_fork_command_1,
15683 NULL,
15684 (void *) (uintptr_t) catch_fork_permanent,
15685 (void *) (uintptr_t) catch_fork_temporary);
15686 add_catch_command ("vfork", _("Catch calls to vfork."),
15687 catch_fork_command_1,
15688 NULL,
15689 (void *) (uintptr_t) catch_vfork_permanent,
15690 (void *) (uintptr_t) catch_vfork_temporary);
15691 add_catch_command ("exec", _("Catch calls to exec."),
15692 catch_exec_command_1,
15693 NULL,
15694 CATCH_PERMANENT,
15695 CATCH_TEMPORARY);
15696 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15697 Usage: catch load [REGEX]\n\
15698 If REGEX is given, only stop for libraries matching the regular expression."),
15699 catch_load_command_1,
15700 NULL,
15701 CATCH_PERMANENT,
15702 CATCH_TEMPORARY);
15703 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15704 Usage: catch unload [REGEX]\n\
15705 If REGEX is given, only stop for libraries matching the regular expression."),
15706 catch_unload_command_1,
15707 NULL,
15708 CATCH_PERMANENT,
15709 CATCH_TEMPORARY);
15710
15711 const auto opts = make_watch_options_def_group (nullptr);
15712
15713 static const std::string watch_help = gdb::option::build_help (_("\
15714 Set a watchpoint for EXPRESSION.\n\
15715 Usage: watch [-location] EXPRESSION\n\
15716 \n\
15717 Options:\n\
15718 %OPTIONS%\n\
15719 \n\
15720 A watchpoint stops execution of your program whenever the value of\n\
15721 an expression changes."), opts);
15722 c = add_com ("watch", class_breakpoint, watch_command,
15723 watch_help.c_str ());
15724 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15725
15726 static const std::string rwatch_help = gdb::option::build_help (_("\
15727 Set a read watchpoint for EXPRESSION.\n\
15728 Usage: rwatch [-location] EXPRESSION\n\
15729 \n\
15730 Options:\n\
15731 %OPTIONS%\n\
15732 \n\
15733 A read watchpoint stops execution of your program whenever the value of\n\
15734 an expression is read."), opts);
15735 c = add_com ("rwatch", class_breakpoint, rwatch_command,
15736 rwatch_help.c_str ());
15737 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15738
15739 static const std::string awatch_help = gdb::option::build_help (_("\
15740 Set an access watchpoint for EXPRESSION.\n\
15741 Usage: awatch [-location] EXPRESSION\n\
15742 \n\
15743 Options:\n\
15744 %OPTIONS%\n\
15745 \n\
15746 An access watchpoint stops execution of your program whenever the value\n\
15747 of an expression is either read or written."), opts);
15748 c = add_com ("awatch", class_breakpoint, awatch_command,
15749 awatch_help.c_str ());
15750 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15751
15752 add_info ("watchpoints", info_watchpoints_command, _("\
15753 Status of specified watchpoints (all watchpoints if no argument)."));
15754
15755 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15756 respond to changes - contrary to the description. */
15757 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15758 &can_use_hw_watchpoints, _("\
15759 Set debugger's willingness to use watchpoint hardware."), _("\
15760 Show debugger's willingness to use watchpoint hardware."), _("\
15761 If zero, gdb will not use hardware for new watchpoints, even if\n\
15762 such is available. (However, any hardware watchpoints that were\n\
15763 created before setting this to nonzero, will continue to use watchpoint\n\
15764 hardware.)"),
15765 NULL,
15766 show_can_use_hw_watchpoints,
15767 &setlist, &showlist);
15768
15769 can_use_hw_watchpoints = 1;
15770
15771 /* Tracepoint manipulation commands. */
15772
15773 cmd_list_element *trace_cmd
15774 = add_com ("trace", class_breakpoint, trace_command, _("\
15775 Set a tracepoint at specified location.\n\
15776 \n"
15777 BREAK_ARGS_HELP ("trace") "\n\
15778 Do \"help tracepoints\" for info on other tracepoint commands."));
15779 set_cmd_completer (trace_cmd, location_completer);
15780
15781 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
15782 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
15783 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
15784 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
15785
15786 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
15787 Set a fast tracepoint at specified location.\n\
15788 \n"
15789 BREAK_ARGS_HELP ("ftrace") "\n\
15790 Do \"help tracepoints\" for info on other tracepoint commands."));
15791 set_cmd_completer (c, location_completer);
15792
15793 c = add_com ("strace", class_breakpoint, strace_command, _("\
15794 Set a static tracepoint at location or marker.\n\
15795 \n\
15796 strace [LOCATION] [if CONDITION]\n\
15797 LOCATION may be a linespec, explicit, or address location (described below) \n\
15798 or -m MARKER_ID.\n\n\
15799 If a marker id is specified, probe the marker with that name. With\n\
15800 no LOCATION, uses current execution address of the selected stack frame.\n\
15801 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15802 This collects arbitrary user data passed in the probe point call to the\n\
15803 tracing library. You can inspect it when analyzing the trace buffer,\n\
15804 by printing the $_sdata variable like any other convenience variable.\n\
15805 \n\
15806 CONDITION is a boolean expression.\n\
15807 \n" LOCATION_HELP_STRING "\n\n\
15808 Multiple tracepoints at one place are permitted, and useful if their\n\
15809 conditions are different.\n\
15810 \n\
15811 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15812 Do \"help tracepoints\" for info on other tracepoint commands."));
15813 set_cmd_completer (c, location_completer);
15814
15815 cmd_list_element *info_tracepoints_cmd
15816 = add_info ("tracepoints", info_tracepoints_command, _("\
15817 Status of specified tracepoints (all tracepoints if no argument).\n\
15818 Convenience variable \"$tpnum\" contains the number of the\n\
15819 last tracepoint set."));
15820
15821 add_info_alias ("tp", info_tracepoints_cmd, 1);
15822
15823 cmd_list_element *delete_tracepoints_cmd
15824 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15825 Delete specified tracepoints.\n\
15826 Arguments are tracepoint numbers, separated by spaces.\n\
15827 No argument means delete all tracepoints."),
15828 &deletelist);
15829 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
15830
15831 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15832 Disable specified tracepoints.\n\
15833 Arguments are tracepoint numbers, separated by spaces.\n\
15834 No argument means disable all tracepoints."),
15835 &disablelist);
15836 deprecate_cmd (c, "disable");
15837
15838 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15839 Enable specified tracepoints.\n\
15840 Arguments are tracepoint numbers, separated by spaces.\n\
15841 No argument means enable all tracepoints."),
15842 &enablelist);
15843 deprecate_cmd (c, "enable");
15844
15845 add_com ("passcount", class_trace, trace_pass_command, _("\
15846 Set the passcount for a tracepoint.\n\
15847 The trace will end when the tracepoint has been passed 'count' times.\n\
15848 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15849 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15850
15851 add_basic_prefix_cmd ("save", class_breakpoint,
15852 _("Save breakpoint definitions as a script."),
15853 &save_cmdlist,
15854 0/*allow-unknown*/, &cmdlist);
15855
15856 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15857 Save current breakpoint definitions as a script.\n\
15858 This includes all types of breakpoints (breakpoints, watchpoints,\n\
15859 catchpoints, tracepoints). Use the 'source' command in another debug\n\
15860 session to restore them."),
15861 &save_cmdlist);
15862 set_cmd_completer (c, filename_completer);
15863
15864 cmd_list_element *save_tracepoints_cmd
15865 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
15866 Save current tracepoint definitions as a script.\n\
15867 Use the 'source' command in another debug session to restore them."),
15868 &save_cmdlist);
15869 set_cmd_completer (save_tracepoints_cmd, filename_completer);
15870
15871 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
15872 deprecate_cmd (c, "save tracepoints");
15873
15874 add_basic_prefix_cmd ("breakpoint", class_maintenance, _("\
15875 Breakpoint specific settings.\n\
15876 Configure various breakpoint-specific variables such as\n\
15877 pending breakpoint behavior."),
15878 &breakpoint_set_cmdlist,
15879 0/*allow-unknown*/, &setlist);
15880 add_show_prefix_cmd ("breakpoint", class_maintenance, _("\
15881 Breakpoint specific settings.\n\
15882 Configure various breakpoint-specific variables such as\n\
15883 pending breakpoint behavior."),
15884 &breakpoint_show_cmdlist,
15885 0/*allow-unknown*/, &showlist);
15886
15887 add_setshow_auto_boolean_cmd ("pending", no_class,
15888 &pending_break_support, _("\
15889 Set debugger's behavior regarding pending breakpoints."), _("\
15890 Show debugger's behavior regarding pending breakpoints."), _("\
15891 If on, an unrecognized breakpoint location will cause gdb to create a\n\
15892 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15893 an error. If auto, an unrecognized breakpoint location results in a\n\
15894 user-query to see if a pending breakpoint should be created."),
15895 NULL,
15896 show_pending_break_support,
15897 &breakpoint_set_cmdlist,
15898 &breakpoint_show_cmdlist);
15899
15900 pending_break_support = AUTO_BOOLEAN_AUTO;
15901
15902 add_setshow_boolean_cmd ("auto-hw", no_class,
15903 &automatic_hardware_breakpoints, _("\
15904 Set automatic usage of hardware breakpoints."), _("\
15905 Show automatic usage of hardware breakpoints."), _("\
15906 If set, the debugger will automatically use hardware breakpoints for\n\
15907 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15908 a warning will be emitted for such breakpoints."),
15909 NULL,
15910 show_automatic_hardware_breakpoints,
15911 &breakpoint_set_cmdlist,
15912 &breakpoint_show_cmdlist);
15913
15914 add_setshow_boolean_cmd ("always-inserted", class_support,
15915 &always_inserted_mode, _("\
15916 Set mode for inserting breakpoints."), _("\
15917 Show mode for inserting breakpoints."), _("\
15918 When this mode is on, breakpoints are inserted immediately as soon as\n\
15919 they're created, kept inserted even when execution stops, and removed\n\
15920 only when the user deletes them. When this mode is off (the default),\n\
15921 breakpoints are inserted only when execution continues, and removed\n\
15922 when execution stops."),
15923 NULL,
15924 &show_always_inserted_mode,
15925 &breakpoint_set_cmdlist,
15926 &breakpoint_show_cmdlist);
15927
15928 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15929 condition_evaluation_enums,
15930 &condition_evaluation_mode_1, _("\
15931 Set mode of breakpoint condition evaluation."), _("\
15932 Show mode of breakpoint condition evaluation."), _("\
15933 When this is set to \"host\", breakpoint conditions will be\n\
15934 evaluated on the host's side by GDB. When it is set to \"target\",\n\
15935 breakpoint conditions will be downloaded to the target (if the target\n\
15936 supports such feature) and conditions will be evaluated on the target's side.\n\
15937 If this is set to \"auto\" (default), this will be automatically set to\n\
15938 \"target\" if it supports condition evaluation, otherwise it will\n\
15939 be set to \"host\"."),
15940 &set_condition_evaluation_mode,
15941 &show_condition_evaluation_mode,
15942 &breakpoint_set_cmdlist,
15943 &breakpoint_show_cmdlist);
15944
15945 add_com ("break-range", class_breakpoint, break_range_command, _("\
15946 Set a breakpoint for an address range.\n\
15947 break-range START-LOCATION, END-LOCATION\n\
15948 where START-LOCATION and END-LOCATION can be one of the following:\n\
15949 LINENUM, for that line in the current file,\n\
15950 FILE:LINENUM, for that line in that file,\n\
15951 +OFFSET, for that number of lines after the current line\n\
15952 or the start of the range\n\
15953 FUNCTION, for the first line in that function,\n\
15954 FILE:FUNCTION, to distinguish among like-named static functions.\n\
15955 *ADDRESS, for the instruction at that address.\n\
15956 \n\
15957 The breakpoint will stop execution of the inferior whenever it executes\n\
15958 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
15959 range (including START-LOCATION and END-LOCATION)."));
15960
15961 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
15962 Set a dynamic printf at specified location.\n\
15963 dprintf location,format string,arg1,arg2,...\n\
15964 location may be a linespec, explicit, or address location.\n"
15965 "\n" LOCATION_HELP_STRING));
15966 set_cmd_completer (c, location_completer);
15967
15968 add_setshow_enum_cmd ("dprintf-style", class_support,
15969 dprintf_style_enums, &dprintf_style, _("\
15970 Set the style of usage for dynamic printf."), _("\
15971 Show the style of usage for dynamic printf."), _("\
15972 This setting chooses how GDB will do a dynamic printf.\n\
15973 If the value is \"gdb\", then the printing is done by GDB to its own\n\
15974 console, as with the \"printf\" command.\n\
15975 If the value is \"call\", the print is done by calling a function in your\n\
15976 program; by default printf(), but you can choose a different function or\n\
15977 output stream by setting dprintf-function and dprintf-channel."),
15978 update_dprintf_commands, NULL,
15979 &setlist, &showlist);
15980
15981 dprintf_function = xstrdup ("printf");
15982 add_setshow_string_cmd ("dprintf-function", class_support,
15983 &dprintf_function, _("\
15984 Set the function to use for dynamic printf."), _("\
15985 Show the function to use for dynamic printf."), NULL,
15986 update_dprintf_commands, NULL,
15987 &setlist, &showlist);
15988
15989 dprintf_channel = xstrdup ("");
15990 add_setshow_string_cmd ("dprintf-channel", class_support,
15991 &dprintf_channel, _("\
15992 Set the channel to use for dynamic printf."), _("\
15993 Show the channel to use for dynamic printf."), NULL,
15994 update_dprintf_commands, NULL,
15995 &setlist, &showlist);
15996
15997 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
15998 &disconnected_dprintf, _("\
15999 Set whether dprintf continues after GDB disconnects."), _("\
16000 Show whether dprintf continues after GDB disconnects."), _("\
16001 Use this to let dprintf commands continue to hit and produce output\n\
16002 even if GDB disconnects or detaches from the target."),
16003 NULL,
16004 NULL,
16005 &setlist, &showlist);
16006
16007 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16008 Target agent only formatted printing, like the C \"printf\" function.\n\
16009 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
16010 This supports most C printf format specifications, like %s, %d, etc.\n\
16011 This is useful for formatted output in user-defined commands."));
16012
16013 automatic_hardware_breakpoints = true;
16014
16015 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
16016 "breakpoint");
16017 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
16018 "breakpoint");
16019 }
This page took 0.453217 seconds and 5 git commands to generate.