gdb: add all_breakpoints_safe 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 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
227
228 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
229 otherwise. */
230
231 static int strace_marker_p (struct breakpoint *b);
232
233 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
234 that are implemented on top of software or hardware breakpoints
235 (user breakpoints, internal and momentary breakpoints, etc.). */
236 static struct breakpoint_ops bkpt_base_breakpoint_ops;
237
238 /* Internal breakpoints class type. */
239 static struct breakpoint_ops internal_breakpoint_ops;
240
241 /* Momentary breakpoints class type. */
242 static struct breakpoint_ops momentary_breakpoint_ops;
243
244 /* The breakpoint_ops structure to be used in regular user created
245 breakpoints. */
246 struct breakpoint_ops bkpt_breakpoint_ops;
247
248 /* Breakpoints set on probes. */
249 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
250
251 /* Tracepoints set on probes. */
252 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
253
254 /* Dynamic printf class type. */
255 struct breakpoint_ops dprintf_breakpoint_ops;
256
257 /* The style in which to perform a dynamic printf. This is a user
258 option because different output options have different tradeoffs;
259 if GDB does the printing, there is better error handling if there
260 is a problem with any of the arguments, but using an inferior
261 function lets you have special-purpose printers and sending of
262 output to the same place as compiled-in print functions. */
263
264 static const char dprintf_style_gdb[] = "gdb";
265 static const char dprintf_style_call[] = "call";
266 static const char dprintf_style_agent[] = "agent";
267 static const char *const dprintf_style_enums[] = {
268 dprintf_style_gdb,
269 dprintf_style_call,
270 dprintf_style_agent,
271 NULL
272 };
273 static const char *dprintf_style = dprintf_style_gdb;
274
275 /* The function to use for dynamic printf if the preferred style is to
276 call into the inferior. The value is simply a string that is
277 copied into the command, so it can be anything that GDB can
278 evaluate to a callable address, not necessarily a function name. */
279
280 static char *dprintf_function;
281
282 /* The channel to use for dynamic printf if the preferred style is to
283 call into the inferior; if a nonempty string, it will be passed to
284 the call as the first argument, with the format string as the
285 second. As with the dprintf function, this can be anything that
286 GDB knows how to evaluate, so in addition to common choices like
287 "stderr", this could be an app-specific expression like
288 "mystreams[curlogger]". */
289
290 static char *dprintf_channel;
291
292 /* True if dprintf commands should continue to operate even if GDB
293 has disconnected. */
294 static bool disconnected_dprintf = true;
295
296 struct command_line *
297 breakpoint_commands (struct breakpoint *b)
298 {
299 return b->commands ? b->commands.get () : NULL;
300 }
301
302 /* Flag indicating that a command has proceeded the inferior past the
303 current breakpoint. */
304
305 static bool breakpoint_proceeded;
306
307 const char *
308 bpdisp_text (enum bpdisp disp)
309 {
310 /* NOTE: the following values are a part of MI protocol and
311 represent values of 'disp' field returned when inferior stops at
312 a breakpoint. */
313 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
314
315 return bpdisps[(int) disp];
316 }
317
318 /* Prototypes for exported functions. */
319 /* If FALSE, gdb will not use hardware support for watchpoints, even
320 if such is available. */
321 static int can_use_hw_watchpoints;
322
323 static void
324 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
325 struct cmd_list_element *c,
326 const char *value)
327 {
328 fprintf_filtered (file,
329 _("Debugger's willingness to use "
330 "watchpoint hardware is %s.\n"),
331 value);
332 }
333
334 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
335 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
336 for unrecognized breakpoint locations.
337 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
338 static enum auto_boolean pending_break_support;
339 static void
340 show_pending_break_support (struct ui_file *file, int from_tty,
341 struct cmd_list_element *c,
342 const char *value)
343 {
344 fprintf_filtered (file,
345 _("Debugger's behavior regarding "
346 "pending breakpoints is %s.\n"),
347 value);
348 }
349
350 /* If true, gdb will automatically use hardware breakpoints for breakpoints
351 set with "break" but falling in read-only memory.
352 If false, gdb will warn about such breakpoints, but won't automatically
353 use hardware breakpoints. */
354 static bool automatic_hardware_breakpoints;
355 static void
356 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
357 struct cmd_list_element *c,
358 const char *value)
359 {
360 fprintf_filtered (file,
361 _("Automatic usage of hardware breakpoints is %s.\n"),
362 value);
363 }
364
365 /* If on, GDB keeps breakpoints inserted even if the inferior is
366 stopped, and immediately inserts any new breakpoints as soon as
367 they're created. If off (default), GDB keeps breakpoints off of
368 the target as long as possible. That is, it delays inserting
369 breakpoints until the next resume, and removes them again when the
370 target fully stops. This is a bit safer in case GDB crashes while
371 processing user input. */
372 static bool always_inserted_mode = false;
373
374 static void
375 show_always_inserted_mode (struct ui_file *file, int from_tty,
376 struct cmd_list_element *c, const char *value)
377 {
378 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
379 value);
380 }
381
382 /* See breakpoint.h. */
383
384 int
385 breakpoints_should_be_inserted_now (void)
386 {
387 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
388 {
389 /* If breakpoints are global, they should be inserted even if no
390 thread under gdb's control is running, or even if there are
391 no threads under GDB's control yet. */
392 return 1;
393 }
394 else
395 {
396 if (always_inserted_mode)
397 {
398 /* The user wants breakpoints inserted even if all threads
399 are stopped. */
400 return 1;
401 }
402
403 for (inferior *inf : all_inferiors ())
404 if (inf->has_execution ()
405 && threads_are_executing (inf->process_target ()))
406 return 1;
407
408 /* Don't remove breakpoints yet if, even though all threads are
409 stopped, we still have events to process. */
410 for (thread_info *tp : all_non_exited_threads ())
411 if (tp->resumed
412 && tp->suspend.waitstatus_pending_p)
413 return 1;
414 }
415 return 0;
416 }
417
418 static const char condition_evaluation_both[] = "host or target";
419
420 /* Modes for breakpoint condition evaluation. */
421 static const char condition_evaluation_auto[] = "auto";
422 static const char condition_evaluation_host[] = "host";
423 static const char condition_evaluation_target[] = "target";
424 static const char *const condition_evaluation_enums[] = {
425 condition_evaluation_auto,
426 condition_evaluation_host,
427 condition_evaluation_target,
428 NULL
429 };
430
431 /* Global that holds the current mode for breakpoint condition evaluation. */
432 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
433
434 /* Global that we use to display information to the user (gets its value from
435 condition_evaluation_mode_1. */
436 static const char *condition_evaluation_mode = condition_evaluation_auto;
437
438 /* Translate a condition evaluation mode MODE into either "host"
439 or "target". This is used mostly to translate from "auto" to the
440 real setting that is being used. It returns the translated
441 evaluation mode. */
442
443 static const char *
444 translate_condition_evaluation_mode (const char *mode)
445 {
446 if (mode == condition_evaluation_auto)
447 {
448 if (target_supports_evaluation_of_breakpoint_conditions ())
449 return condition_evaluation_target;
450 else
451 return condition_evaluation_host;
452 }
453 else
454 return mode;
455 }
456
457 /* Discovers what condition_evaluation_auto translates to. */
458
459 static const char *
460 breakpoint_condition_evaluation_mode (void)
461 {
462 return translate_condition_evaluation_mode (condition_evaluation_mode);
463 }
464
465 /* Return true if GDB should evaluate breakpoint conditions or false
466 otherwise. */
467
468 static int
469 gdb_evaluates_breakpoint_condition_p (void)
470 {
471 const char *mode = breakpoint_condition_evaluation_mode ();
472
473 return (mode == condition_evaluation_host);
474 }
475
476 /* Are we executing breakpoint commands? */
477 static int executing_breakpoint_commands;
478
479 /* Are overlay event breakpoints enabled? */
480 static int overlay_events_enabled;
481
482 /* See description in breakpoint.h. */
483 bool target_exact_watchpoints = false;
484
485 /* Walk the following statement or block through all breakpoints.
486 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
487 current breakpoint. */
488
489 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
490 for (B = breakpoint_chain; \
491 B ? (TMP=B->next, 1): 0; \
492 B = TMP)
493
494 /* Similar iterator for the low-level breakpoints. SAFE variant is
495 not provided so update_global_location_list must not be called
496 while executing the block of ALL_BP_LOCATIONS. */
497
498 #define ALL_BP_LOCATIONS(B,BP_TMP) \
499 for (BP_TMP = bp_locations; \
500 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
501 BP_TMP++)
502
503 /* Iterates through locations with address ADDRESS for the currently selected
504 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
505 to where the loop should start from.
506 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
507 appropriate location to start with. */
508
509 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
510 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
511 BP_LOCP_TMP = BP_LOCP_START; \
512 BP_LOCP_START \
513 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
514 && (*BP_LOCP_TMP)->address == ADDRESS); \
515 BP_LOCP_TMP++)
516
517 /* Iterator for tracepoints only. */
518
519 #define ALL_TRACEPOINTS(B) \
520 for (B = breakpoint_chain; B; B = B->next) \
521 if (is_tracepoint (B))
522
523 /* Chains of all breakpoints defined. */
524
525 static struct breakpoint *breakpoint_chain;
526
527 /* Breakpoint linked list iterator. */
528
529 using breakpoint_iterator = next_iterator<breakpoint>;
530
531 /* Breakpoint linked list range. */
532
533 using breakpoint_range = next_adapter<breakpoint, breakpoint_iterator>;
534
535 /* Return a range to iterate over all breakpoints. */
536
537 static breakpoint_range
538 all_breakpoints ()
539 {
540 return breakpoint_range (breakpoint_chain);
541 }
542
543 /* Breakpoint linked list range, safe against deletion of the current
544 breakpoint while iterating. */
545
546 using breakpoint_safe_range = basic_safe_range<breakpoint_range>;
547
548 /* Return a range to iterate over all breakpoints. This range is safe against
549 deletion of the current breakpoint while iterating. */
550
551 static breakpoint_safe_range
552 all_breakpoints_safe ()
553 {
554 return breakpoint_safe_range (all_breakpoints ());
555 }
556
557 /* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
558
559 static struct bp_location **bp_locations;
560
561 /* Number of elements of BP_LOCATIONS. */
562
563 static unsigned bp_locations_count;
564
565 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
566 ADDRESS for the current elements of BP_LOCATIONS which get a valid
567 result from bp_location_has_shadow. You can use it for roughly
568 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
569 an address you need to read. */
570
571 static CORE_ADDR bp_locations_placed_address_before_address_max;
572
573 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
574 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
575 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
576 You can use it for roughly limiting the subrange of BP_LOCATIONS to
577 scan for shadow bytes for an address you need to read. */
578
579 static CORE_ADDR bp_locations_shadow_len_after_address_max;
580
581 /* The locations that no longer correspond to any breakpoint, unlinked
582 from the bp_locations array, but for which a hit may still be
583 reported by a target. */
584 static std::vector<bp_location *> moribund_locations;
585
586 /* Number of last breakpoint made. */
587
588 static int breakpoint_count;
589
590 /* The value of `breakpoint_count' before the last command that
591 created breakpoints. If the last (break-like) command created more
592 than one breakpoint, then the difference between BREAKPOINT_COUNT
593 and PREV_BREAKPOINT_COUNT is more than one. */
594 static int prev_breakpoint_count;
595
596 /* Number of last tracepoint made. */
597
598 static int tracepoint_count;
599
600 static struct cmd_list_element *breakpoint_set_cmdlist;
601 static struct cmd_list_element *breakpoint_show_cmdlist;
602 struct cmd_list_element *save_cmdlist;
603
604 /* See declaration at breakpoint.h. */
605
606 struct breakpoint *
607 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
608 void *user_data)
609 {
610 for (breakpoint *b : all_breakpoints ())
611 if (func (b, user_data) != 0)
612 return b;
613
614 return nullptr;
615 }
616
617 /* Return whether a breakpoint is an active enabled breakpoint. */
618 static int
619 breakpoint_enabled (struct breakpoint *b)
620 {
621 return (b->enable_state == bp_enabled);
622 }
623
624 /* Set breakpoint count to NUM. */
625
626 static void
627 set_breakpoint_count (int num)
628 {
629 prev_breakpoint_count = breakpoint_count;
630 breakpoint_count = num;
631 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
632 }
633
634 /* Used by `start_rbreak_breakpoints' below, to record the current
635 breakpoint count before "rbreak" creates any breakpoint. */
636 static int rbreak_start_breakpoint_count;
637
638 /* Called at the start an "rbreak" command to record the first
639 breakpoint made. */
640
641 scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
642 {
643 rbreak_start_breakpoint_count = breakpoint_count;
644 }
645
646 /* Called at the end of an "rbreak" command to record the last
647 breakpoint made. */
648
649 scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
650 {
651 prev_breakpoint_count = rbreak_start_breakpoint_count;
652 }
653
654 /* Used in run_command to zero the hit count when a new run starts. */
655
656 void
657 clear_breakpoint_hit_counts (void)
658 {
659 for (breakpoint *b : all_breakpoints ())
660 b->hit_count = 0;
661 }
662
663 \f
664 /* Return the breakpoint with the specified number, or NULL
665 if the number does not refer to an existing breakpoint. */
666
667 struct breakpoint *
668 get_breakpoint (int num)
669 {
670 for (breakpoint *b : all_breakpoints ())
671 if (b->number == num)
672 return b;
673
674 return nullptr;
675 }
676
677 \f
678
679 /* Mark locations as "conditions have changed" in case the target supports
680 evaluating conditions on its side. */
681
682 static void
683 mark_breakpoint_modified (struct breakpoint *b)
684 {
685 struct bp_location *loc;
686
687 /* This is only meaningful if the target is
688 evaluating conditions and if the user has
689 opted for condition evaluation on the target's
690 side. */
691 if (gdb_evaluates_breakpoint_condition_p ()
692 || !target_supports_evaluation_of_breakpoint_conditions ())
693 return;
694
695 if (!is_breakpoint (b))
696 return;
697
698 for (loc = b->loc; loc; loc = loc->next)
699 loc->condition_changed = condition_modified;
700 }
701
702 /* Mark location as "conditions have changed" in case the target supports
703 evaluating conditions on its side. */
704
705 static void
706 mark_breakpoint_location_modified (struct bp_location *loc)
707 {
708 /* This is only meaningful if the target is
709 evaluating conditions and if the user has
710 opted for condition evaluation on the target's
711 side. */
712 if (gdb_evaluates_breakpoint_condition_p ()
713 || !target_supports_evaluation_of_breakpoint_conditions ())
714
715 return;
716
717 if (!is_breakpoint (loc->owner))
718 return;
719
720 loc->condition_changed = condition_modified;
721 }
722
723 /* Sets the condition-evaluation mode using the static global
724 condition_evaluation_mode. */
725
726 static void
727 set_condition_evaluation_mode (const char *args, int from_tty,
728 struct cmd_list_element *c)
729 {
730 const char *old_mode, *new_mode;
731
732 if ((condition_evaluation_mode_1 == condition_evaluation_target)
733 && !target_supports_evaluation_of_breakpoint_conditions ())
734 {
735 condition_evaluation_mode_1 = condition_evaluation_mode;
736 warning (_("Target does not support breakpoint condition evaluation.\n"
737 "Using host evaluation mode instead."));
738 return;
739 }
740
741 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
742 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
743
744 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
745 settings was "auto". */
746 condition_evaluation_mode = condition_evaluation_mode_1;
747
748 /* Only update the mode if the user picked a different one. */
749 if (new_mode != old_mode)
750 {
751 struct bp_location *loc, **loc_tmp;
752 /* If the user switched to a different evaluation mode, we
753 need to synch the changes with the target as follows:
754
755 "host" -> "target": Send all (valid) conditions to the target.
756 "target" -> "host": Remove all the conditions from the target.
757 */
758
759 if (new_mode == condition_evaluation_target)
760 {
761 /* Mark everything modified and synch conditions with the
762 target. */
763 ALL_BP_LOCATIONS (loc, loc_tmp)
764 mark_breakpoint_location_modified (loc);
765 }
766 else
767 {
768 /* Manually mark non-duplicate locations to synch conditions
769 with the target. We do this to remove all the conditions the
770 target knows about. */
771 ALL_BP_LOCATIONS (loc, loc_tmp)
772 if (is_breakpoint (loc->owner) && loc->inserted)
773 loc->needs_update = 1;
774 }
775
776 /* Do the update. */
777 update_global_location_list (UGLL_MAY_INSERT);
778 }
779
780 return;
781 }
782
783 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
784 what "auto" is translating to. */
785
786 static void
787 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
788 struct cmd_list_element *c, const char *value)
789 {
790 if (condition_evaluation_mode == condition_evaluation_auto)
791 fprintf_filtered (file,
792 _("Breakpoint condition evaluation "
793 "mode is %s (currently %s).\n"),
794 value,
795 breakpoint_condition_evaluation_mode ());
796 else
797 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
798 value);
799 }
800
801 /* A comparison function for bp_location AP and BP that is used by
802 bsearch. This comparison function only cares about addresses, unlike
803 the more general bp_location_is_less_than function. */
804
805 static int
806 bp_locations_compare_addrs (const void *ap, const void *bp)
807 {
808 const struct bp_location *a = *(const struct bp_location **) ap;
809 const struct bp_location *b = *(const struct bp_location **) bp;
810
811 if (a->address == b->address)
812 return 0;
813 else
814 return ((a->address > b->address) - (a->address < b->address));
815 }
816
817 /* Helper function to skip all bp_locations with addresses
818 less than ADDRESS. It returns the first bp_location that
819 is greater than or equal to ADDRESS. If none is found, just
820 return NULL. */
821
822 static struct bp_location **
823 get_first_locp_gte_addr (CORE_ADDR address)
824 {
825 struct bp_location dummy_loc;
826 struct bp_location *dummy_locp = &dummy_loc;
827 struct bp_location **locp_found = NULL;
828
829 /* Initialize the dummy location's address field. */
830 dummy_loc.address = address;
831
832 /* Find a close match to the first location at ADDRESS. */
833 locp_found = ((struct bp_location **)
834 bsearch (&dummy_locp, bp_locations, bp_locations_count,
835 sizeof (struct bp_location **),
836 bp_locations_compare_addrs));
837
838 /* Nothing was found, nothing left to do. */
839 if (locp_found == NULL)
840 return NULL;
841
842 /* We may have found a location that is at ADDRESS but is not the first in the
843 location's list. Go backwards (if possible) and locate the first one. */
844 while ((locp_found - 1) >= bp_locations
845 && (*(locp_found - 1))->address == address)
846 locp_found--;
847
848 return locp_found;
849 }
850
851 /* Parse COND_STRING in the context of LOC and set as the condition
852 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
853 the number of LOC within its owner. In case of parsing error, mark
854 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
855
856 static void
857 set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
858 int bp_num, int loc_num)
859 {
860 bool has_junk = false;
861 try
862 {
863 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
864 block_for_pc (loc->address), 0);
865 if (*cond_string != 0)
866 has_junk = true;
867 else
868 {
869 loc->cond = std::move (new_exp);
870 if (loc->disabled_by_cond && loc->enabled)
871 printf_filtered (_("Breakpoint %d's condition is now valid at "
872 "location %d, enabling.\n"),
873 bp_num, loc_num);
874
875 loc->disabled_by_cond = false;
876 }
877 }
878 catch (const gdb_exception_error &e)
879 {
880 if (loc->enabled)
881 {
882 /* Warn if a user-enabled location is now becoming disabled-by-cond.
883 BP_NUM is 0 if the breakpoint is being defined for the first
884 time using the "break ... if ..." command, and non-zero if
885 already defined. */
886 if (bp_num != 0)
887 warning (_("failed to validate condition at location %d.%d, "
888 "disabling:\n %s"), bp_num, loc_num, e.what ());
889 else
890 warning (_("failed to validate condition at location %d, "
891 "disabling:\n %s"), loc_num, e.what ());
892 }
893
894 loc->disabled_by_cond = true;
895 }
896
897 if (has_junk)
898 error (_("Garbage '%s' follows condition"), cond_string);
899 }
900
901 void
902 set_breakpoint_condition (struct breakpoint *b, const char *exp,
903 int from_tty, bool force)
904 {
905 if (*exp == 0)
906 {
907 xfree (b->cond_string);
908 b->cond_string = nullptr;
909
910 if (is_watchpoint (b))
911 static_cast<watchpoint *> (b)->cond_exp.reset ();
912 else
913 {
914 int loc_num = 1;
915 for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)
916 {
917 loc->cond.reset ();
918 if (loc->disabled_by_cond && loc->enabled)
919 printf_filtered (_("Breakpoint %d's condition is now valid at "
920 "location %d, enabling.\n"),
921 b->number, loc_num);
922 loc->disabled_by_cond = false;
923 loc_num++;
924
925 /* No need to free the condition agent expression
926 bytecode (if we have one). We will handle this
927 when we go through update_global_location_list. */
928 }
929 }
930
931 if (from_tty)
932 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
933 }
934 else
935 {
936 if (is_watchpoint (b))
937 {
938 innermost_block_tracker tracker;
939 const char *arg = exp;
940 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
941 if (*arg != 0)
942 error (_("Junk at end of expression"));
943 watchpoint *w = static_cast<watchpoint *> (b);
944 w->cond_exp = std::move (new_exp);
945 w->cond_exp_valid_block = tracker.block ();
946 }
947 else
948 {
949 /* Parse and set condition expressions. We make two passes.
950 In the first, we parse the condition string to see if it
951 is valid in at least one location. If so, the condition
952 would be accepted. So we go ahead and set the locations'
953 conditions. In case no valid case is found, we throw
954 the error and the condition string will be rejected.
955 This two-pass approach is taken to avoid setting the
956 state of locations in case of a reject. */
957 for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)
958 {
959 try
960 {
961 const char *arg = exp;
962 parse_exp_1 (&arg, loc->address,
963 block_for_pc (loc->address), 0);
964 if (*arg != 0)
965 error (_("Junk at end of expression"));
966 break;
967 }
968 catch (const gdb_exception_error &e)
969 {
970 /* Condition string is invalid. If this happens to
971 be the last loc, abandon (if not forced) or continue
972 (if forced). */
973 if (loc->next == nullptr && !force)
974 throw;
975 }
976 }
977
978 /* If we reach here, the condition is valid at some locations. */
979 int loc_num = 1;
980 for (bp_location *loc = b->loc; loc != nullptr;
981 loc = loc->next, loc_num++)
982 set_breakpoint_location_condition (exp, loc, b->number, loc_num);
983 }
984
985 /* We know that the new condition parsed successfully. The
986 condition string of the breakpoint can be safely updated. */
987 xfree (b->cond_string);
988 b->cond_string = xstrdup (exp);
989 b->condition_not_parsed = 0;
990 }
991 mark_breakpoint_modified (b);
992
993 gdb::observers::breakpoint_modified.notify (b);
994 }
995
996 /* See breakpoint.h. */
997
998 void
999 set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
1000 bool force)
1001 {
1002 for (breakpoint *b : all_breakpoints ())
1003 if (b->number == bpnum)
1004 {
1005 /* Check if this breakpoint has a "stop" method implemented in an
1006 extension language. This method and conditions entered into GDB
1007 from the CLI are mutually exclusive. */
1008 const struct extension_language_defn *extlang
1009 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1010
1011 if (extlang != NULL)
1012 {
1013 error (_("Only one stop condition allowed. There is currently"
1014 " a %s stop condition defined for this breakpoint."),
1015 ext_lang_capitalized_name (extlang));
1016 }
1017 set_breakpoint_condition (b, exp, from_tty, force);
1018
1019 if (is_breakpoint (b))
1020 update_global_location_list (UGLL_MAY_INSERT);
1021
1022 return;
1023 }
1024
1025 error (_("No breakpoint number %d."), bpnum);
1026 }
1027
1028 /* The options for the "condition" command. */
1029
1030 struct condition_command_opts
1031 {
1032 /* For "-force". */
1033 bool force_condition = false;
1034 };
1035
1036 static const gdb::option::option_def condition_command_option_defs[] = {
1037
1038 gdb::option::flag_option_def<condition_command_opts> {
1039 "force",
1040 [] (condition_command_opts *opts) { return &opts->force_condition; },
1041 N_("Set the condition even if it is invalid for all current locations."),
1042 },
1043
1044 };
1045
1046 /* Create an option_def_group for the "condition" options, with
1047 CC_OPTS as context. */
1048
1049 static inline gdb::option::option_def_group
1050 make_condition_command_options_def_group (condition_command_opts *cc_opts)
1051 {
1052 return {{condition_command_option_defs}, cc_opts};
1053 }
1054
1055 /* Completion for the "condition" command. */
1056
1057 static void
1058 condition_completer (struct cmd_list_element *cmd,
1059 completion_tracker &tracker,
1060 const char *text, const char * /*word*/)
1061 {
1062 bool has_no_arguments = (*text == '\0');
1063 condition_command_opts cc_opts;
1064 const auto group = make_condition_command_options_def_group (&cc_opts);
1065 if (gdb::option::complete_options
1066 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1067 return;
1068
1069 text = skip_spaces (text);
1070 const char *space = skip_to_space (text);
1071 if (*space == '\0')
1072 {
1073 int len;
1074
1075 if (text[0] == '$')
1076 {
1077 tracker.advance_custom_word_point_by (1);
1078 /* We don't support completion of history indices. */
1079 if (!isdigit (text[1]))
1080 complete_internalvar (tracker, &text[1]);
1081 return;
1082 }
1083
1084 /* Suggest the "-force" flag if no arguments are given. If
1085 arguments were passed, they either already include the flag,
1086 or we are beyond the point of suggesting it because it's
1087 positionally the first argument. */
1088 if (has_no_arguments)
1089 gdb::option::complete_on_all_options (tracker, group);
1090
1091 /* We're completing the breakpoint number. */
1092 len = strlen (text);
1093
1094 for (breakpoint *b : all_breakpoints ())
1095 {
1096 char number[50];
1097
1098 xsnprintf (number, sizeof (number), "%d", b->number);
1099
1100 if (strncmp (number, text, len) == 0)
1101 tracker.add_completion (make_unique_xstrdup (number));
1102 }
1103
1104 return;
1105 }
1106
1107 /* We're completing the expression part. Skip the breakpoint num. */
1108 const char *exp_start = skip_spaces (space);
1109 tracker.advance_custom_word_point_by (exp_start - text);
1110 text = exp_start;
1111 const char *word = advance_to_expression_complete_word_point (tracker, text);
1112 expression_completer (cmd, tracker, text, word);
1113 }
1114
1115 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1116
1117 static void
1118 condition_command (const char *arg, int from_tty)
1119 {
1120 const char *p;
1121 int bnum;
1122
1123 if (arg == 0)
1124 error_no_arg (_("breakpoint number"));
1125
1126 p = arg;
1127
1128 /* Check if the "-force" flag was passed. */
1129 condition_command_opts cc_opts;
1130 const auto group = make_condition_command_options_def_group (&cc_opts);
1131 gdb::option::process_options
1132 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
1133
1134 bnum = get_number (&p);
1135 if (bnum == 0)
1136 error (_("Bad breakpoint argument: '%s'"), arg);
1137
1138 set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
1139 }
1140
1141 /* Check that COMMAND do not contain commands that are suitable
1142 only for tracepoints and not suitable for ordinary breakpoints.
1143 Throw if any such commands is found. */
1144
1145 static void
1146 check_no_tracepoint_commands (struct command_line *commands)
1147 {
1148 struct command_line *c;
1149
1150 for (c = commands; c; c = c->next)
1151 {
1152 if (c->control_type == while_stepping_control)
1153 error (_("The 'while-stepping' command can "
1154 "only be used for tracepoints"));
1155
1156 check_no_tracepoint_commands (c->body_list_0.get ());
1157 check_no_tracepoint_commands (c->body_list_1.get ());
1158
1159 /* Not that command parsing removes leading whitespace and comment
1160 lines and also empty lines. So, we only need to check for
1161 command directly. */
1162 if (strstr (c->line, "collect ") == c->line)
1163 error (_("The 'collect' command can only be used for tracepoints"));
1164
1165 if (strstr (c->line, "teval ") == c->line)
1166 error (_("The 'teval' command can only be used for tracepoints"));
1167 }
1168 }
1169
1170 struct longjmp_breakpoint : public breakpoint
1171 {
1172 ~longjmp_breakpoint () override;
1173 };
1174
1175 /* Encapsulate tests for different types of tracepoints. */
1176
1177 static bool
1178 is_tracepoint_type (bptype type)
1179 {
1180 return (type == bp_tracepoint
1181 || type == bp_fast_tracepoint
1182 || type == bp_static_tracepoint);
1183 }
1184
1185 static bool
1186 is_longjmp_type (bptype type)
1187 {
1188 return type == bp_longjmp || type == bp_exception;
1189 }
1190
1191 /* See breakpoint.h. */
1192
1193 bool
1194 is_tracepoint (const struct breakpoint *b)
1195 {
1196 return is_tracepoint_type (b->type);
1197 }
1198
1199 /* Factory function to create an appropriate instance of breakpoint given
1200 TYPE. */
1201
1202 static std::unique_ptr<breakpoint>
1203 new_breakpoint_from_type (bptype type)
1204 {
1205 breakpoint *b;
1206
1207 if (is_tracepoint_type (type))
1208 b = new tracepoint ();
1209 else if (is_longjmp_type (type))
1210 b = new longjmp_breakpoint ();
1211 else
1212 b = new breakpoint ();
1213
1214 return std::unique_ptr<breakpoint> (b);
1215 }
1216
1217 /* A helper function that validates that COMMANDS are valid for a
1218 breakpoint. This function will throw an exception if a problem is
1219 found. */
1220
1221 static void
1222 validate_commands_for_breakpoint (struct breakpoint *b,
1223 struct command_line *commands)
1224 {
1225 if (is_tracepoint (b))
1226 {
1227 struct tracepoint *t = (struct tracepoint *) b;
1228 struct command_line *c;
1229 struct command_line *while_stepping = 0;
1230
1231 /* Reset the while-stepping step count. The previous commands
1232 might have included a while-stepping action, while the new
1233 ones might not. */
1234 t->step_count = 0;
1235
1236 /* We need to verify that each top-level element of commands is
1237 valid for tracepoints, that there's at most one
1238 while-stepping element, and that the while-stepping's body
1239 has valid tracing commands excluding nested while-stepping.
1240 We also need to validate the tracepoint action line in the
1241 context of the tracepoint --- validate_actionline actually
1242 has side effects, like setting the tracepoint's
1243 while-stepping STEP_COUNT, in addition to checking if the
1244 collect/teval actions parse and make sense in the
1245 tracepoint's context. */
1246 for (c = commands; c; c = c->next)
1247 {
1248 if (c->control_type == while_stepping_control)
1249 {
1250 if (b->type == bp_fast_tracepoint)
1251 error (_("The 'while-stepping' command "
1252 "cannot be used for fast tracepoint"));
1253 else if (b->type == bp_static_tracepoint)
1254 error (_("The 'while-stepping' command "
1255 "cannot be used for static tracepoint"));
1256
1257 if (while_stepping)
1258 error (_("The 'while-stepping' command "
1259 "can be used only once"));
1260 else
1261 while_stepping = c;
1262 }
1263
1264 validate_actionline (c->line, b);
1265 }
1266 if (while_stepping)
1267 {
1268 struct command_line *c2;
1269
1270 gdb_assert (while_stepping->body_list_1 == nullptr);
1271 c2 = while_stepping->body_list_0.get ();
1272 for (; c2; c2 = c2->next)
1273 {
1274 if (c2->control_type == while_stepping_control)
1275 error (_("The 'while-stepping' command cannot be nested"));
1276 }
1277 }
1278 }
1279 else
1280 {
1281 check_no_tracepoint_commands (commands);
1282 }
1283 }
1284
1285 /* Return a vector of all the static tracepoints set at ADDR. The
1286 caller is responsible for releasing the vector. */
1287
1288 std::vector<breakpoint *>
1289 static_tracepoints_here (CORE_ADDR addr)
1290 {
1291 std::vector<breakpoint *> found;
1292 struct bp_location *loc;
1293
1294 for (breakpoint *b : all_breakpoints ())
1295 if (b->type == bp_static_tracepoint)
1296 {
1297 for (loc = b->loc; loc; loc = loc->next)
1298 if (loc->address == addr)
1299 found.push_back (b);
1300 }
1301
1302 return found;
1303 }
1304
1305 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1306 validate that only allowed commands are included. */
1307
1308 void
1309 breakpoint_set_commands (struct breakpoint *b,
1310 counted_command_line &&commands)
1311 {
1312 validate_commands_for_breakpoint (b, commands.get ());
1313
1314 b->commands = std::move (commands);
1315 gdb::observers::breakpoint_modified.notify (b);
1316 }
1317
1318 /* Set the internal `silent' flag on the breakpoint. Note that this
1319 is not the same as the "silent" that may appear in the breakpoint's
1320 commands. */
1321
1322 void
1323 breakpoint_set_silent (struct breakpoint *b, int silent)
1324 {
1325 int old_silent = b->silent;
1326
1327 b->silent = silent;
1328 if (old_silent != silent)
1329 gdb::observers::breakpoint_modified.notify (b);
1330 }
1331
1332 /* Set the thread for this breakpoint. If THREAD is -1, make the
1333 breakpoint work for any thread. */
1334
1335 void
1336 breakpoint_set_thread (struct breakpoint *b, int thread)
1337 {
1338 int old_thread = b->thread;
1339
1340 b->thread = thread;
1341 if (old_thread != thread)
1342 gdb::observers::breakpoint_modified.notify (b);
1343 }
1344
1345 /* Set the task for this breakpoint. If TASK is 0, make the
1346 breakpoint work for any task. */
1347
1348 void
1349 breakpoint_set_task (struct breakpoint *b, int task)
1350 {
1351 int old_task = b->task;
1352
1353 b->task = task;
1354 if (old_task != task)
1355 gdb::observers::breakpoint_modified.notify (b);
1356 }
1357
1358 static void
1359 commands_command_1 (const char *arg, int from_tty,
1360 struct command_line *control)
1361 {
1362 counted_command_line cmd;
1363 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1364 NULL after the call to read_command_lines if the user provides an empty
1365 list of command by just typing "end". */
1366 bool cmd_read = false;
1367
1368 std::string new_arg;
1369
1370 if (arg == NULL || !*arg)
1371 {
1372 /* Argument not explicitly given. Synthesize it. */
1373 if (breakpoint_count - prev_breakpoint_count > 1)
1374 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1375 breakpoint_count);
1376 else if (breakpoint_count > 0)
1377 new_arg = string_printf ("%d", breakpoint_count);
1378 }
1379 else
1380 {
1381 /* Create a copy of ARG. This is needed because the "commands"
1382 command may be coming from a script. In that case, the read
1383 line buffer is going to be overwritten in the lambda of
1384 'map_breakpoint_numbers' below when reading the next line
1385 before we are are done parsing the breakpoint numbers. */
1386 new_arg = arg;
1387 }
1388 arg = new_arg.c_str ();
1389
1390 map_breakpoint_numbers
1391 (arg, [&] (breakpoint *b)
1392 {
1393 if (!cmd_read)
1394 {
1395 gdb_assert (cmd == NULL);
1396 if (control != NULL)
1397 cmd = control->body_list_0;
1398 else
1399 {
1400 std::string str
1401 = string_printf (_("Type commands for breakpoint(s) "
1402 "%s, one per line."),
1403 arg);
1404
1405 auto do_validate = [=] (const char *line)
1406 {
1407 validate_actionline (line, b);
1408 };
1409 gdb::function_view<void (const char *)> validator;
1410 if (is_tracepoint (b))
1411 validator = do_validate;
1412
1413 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1414 }
1415 cmd_read = true;
1416 }
1417
1418 /* If a breakpoint was on the list more than once, we don't need to
1419 do anything. */
1420 if (b->commands != cmd)
1421 {
1422 validate_commands_for_breakpoint (b, cmd.get ());
1423 b->commands = cmd;
1424 gdb::observers::breakpoint_modified.notify (b);
1425 }
1426 });
1427 }
1428
1429 static void
1430 commands_command (const char *arg, int from_tty)
1431 {
1432 commands_command_1 (arg, from_tty, NULL);
1433 }
1434
1435 /* Like commands_command, but instead of reading the commands from
1436 input stream, takes them from an already parsed command structure.
1437
1438 This is used by cli-script.c to DTRT with breakpoint commands
1439 that are part of if and while bodies. */
1440 enum command_control_type
1441 commands_from_control_command (const char *arg, struct command_line *cmd)
1442 {
1443 commands_command_1 (arg, 0, cmd);
1444 return simple_control;
1445 }
1446
1447 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1448
1449 static int
1450 bp_location_has_shadow (struct bp_location *bl)
1451 {
1452 if (bl->loc_type != bp_loc_software_breakpoint)
1453 return 0;
1454 if (!bl->inserted)
1455 return 0;
1456 if (bl->target_info.shadow_len == 0)
1457 /* BL isn't valid, or doesn't shadow memory. */
1458 return 0;
1459 return 1;
1460 }
1461
1462 /* Update BUF, which is LEN bytes read from the target address
1463 MEMADDR, by replacing a memory breakpoint with its shadowed
1464 contents.
1465
1466 If READBUF is not NULL, this buffer must not overlap with the of
1467 the breakpoint location's shadow_contents buffer. Otherwise, a
1468 failed assertion internal error will be raised. */
1469
1470 static void
1471 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1472 const gdb_byte *writebuf_org,
1473 ULONGEST memaddr, LONGEST len,
1474 struct bp_target_info *target_info,
1475 struct gdbarch *gdbarch)
1476 {
1477 /* Now do full processing of the found relevant range of elements. */
1478 CORE_ADDR bp_addr = 0;
1479 int bp_size = 0;
1480 int bptoffset = 0;
1481
1482 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1483 current_program_space->aspace, 0))
1484 {
1485 /* The breakpoint is inserted in a different address space. */
1486 return;
1487 }
1488
1489 /* Addresses and length of the part of the breakpoint that
1490 we need to copy. */
1491 bp_addr = target_info->placed_address;
1492 bp_size = target_info->shadow_len;
1493
1494 if (bp_addr + bp_size <= memaddr)
1495 {
1496 /* The breakpoint is entirely before the chunk of memory we are
1497 reading. */
1498 return;
1499 }
1500
1501 if (bp_addr >= memaddr + len)
1502 {
1503 /* The breakpoint is entirely after the chunk of memory we are
1504 reading. */
1505 return;
1506 }
1507
1508 /* Offset within shadow_contents. */
1509 if (bp_addr < memaddr)
1510 {
1511 /* Only copy the second part of the breakpoint. */
1512 bp_size -= memaddr - bp_addr;
1513 bptoffset = memaddr - bp_addr;
1514 bp_addr = memaddr;
1515 }
1516
1517 if (bp_addr + bp_size > memaddr + len)
1518 {
1519 /* Only copy the first part of the breakpoint. */
1520 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1521 }
1522
1523 if (readbuf != NULL)
1524 {
1525 /* Verify that the readbuf buffer does not overlap with the
1526 shadow_contents buffer. */
1527 gdb_assert (target_info->shadow_contents >= readbuf + len
1528 || readbuf >= (target_info->shadow_contents
1529 + target_info->shadow_len));
1530
1531 /* Update the read buffer with this inserted breakpoint's
1532 shadow. */
1533 memcpy (readbuf + bp_addr - memaddr,
1534 target_info->shadow_contents + bptoffset, bp_size);
1535 }
1536 else
1537 {
1538 const unsigned char *bp;
1539 CORE_ADDR addr = target_info->reqstd_address;
1540 int placed_size;
1541
1542 /* Update the shadow with what we want to write to memory. */
1543 memcpy (target_info->shadow_contents + bptoffset,
1544 writebuf_org + bp_addr - memaddr, bp_size);
1545
1546 /* Determine appropriate breakpoint contents and size for this
1547 address. */
1548 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1549
1550 /* Update the final write buffer with this inserted
1551 breakpoint's INSN. */
1552 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1553 }
1554 }
1555
1556 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1557 by replacing any memory breakpoints with their shadowed contents.
1558
1559 If READBUF is not NULL, this buffer must not overlap with any of
1560 the breakpoint location's shadow_contents buffers. Otherwise,
1561 a failed assertion internal error will be raised.
1562
1563 The range of shadowed area by each bp_location is:
1564 bl->address - bp_locations_placed_address_before_address_max
1565 up to bl->address + bp_locations_shadow_len_after_address_max
1566 The range we were requested to resolve shadows for is:
1567 memaddr ... memaddr + len
1568 Thus the safe cutoff boundaries for performance optimization are
1569 memaddr + len <= (bl->address
1570 - bp_locations_placed_address_before_address_max)
1571 and:
1572 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1573
1574 void
1575 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1576 const gdb_byte *writebuf_org,
1577 ULONGEST memaddr, LONGEST len)
1578 {
1579 /* Left boundary, right boundary and median element of our binary
1580 search. */
1581 unsigned bc_l, bc_r, bc;
1582
1583 /* Find BC_L which is a leftmost element which may affect BUF
1584 content. It is safe to report lower value but a failure to
1585 report higher one. */
1586
1587 bc_l = 0;
1588 bc_r = bp_locations_count;
1589 while (bc_l + 1 < bc_r)
1590 {
1591 struct bp_location *bl;
1592
1593 bc = (bc_l + bc_r) / 2;
1594 bl = bp_locations[bc];
1595
1596 /* Check first BL->ADDRESS will not overflow due to the added
1597 constant. Then advance the left boundary only if we are sure
1598 the BC element can in no way affect the BUF content (MEMADDR
1599 to MEMADDR + LEN range).
1600
1601 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1602 offset so that we cannot miss a breakpoint with its shadow
1603 range tail still reaching MEMADDR. */
1604
1605 if ((bl->address + bp_locations_shadow_len_after_address_max
1606 >= bl->address)
1607 && (bl->address + bp_locations_shadow_len_after_address_max
1608 <= memaddr))
1609 bc_l = bc;
1610 else
1611 bc_r = bc;
1612 }
1613
1614 /* Due to the binary search above, we need to make sure we pick the
1615 first location that's at BC_L's address. E.g., if there are
1616 multiple locations at the same address, BC_L may end up pointing
1617 at a duplicate location, and miss the "master"/"inserted"
1618 location. Say, given locations L1, L2 and L3 at addresses A and
1619 B:
1620
1621 L1@A, L2@A, L3@B, ...
1622
1623 BC_L could end up pointing at location L2, while the "master"
1624 location could be L1. Since the `loc->inserted' flag is only set
1625 on "master" locations, we'd forget to restore the shadow of L1
1626 and L2. */
1627 while (bc_l > 0
1628 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1629 bc_l--;
1630
1631 /* Now do full processing of the found relevant range of elements. */
1632
1633 for (bc = bc_l; bc < bp_locations_count; bc++)
1634 {
1635 struct bp_location *bl = bp_locations[bc];
1636
1637 /* bp_location array has BL->OWNER always non-NULL. */
1638 if (bl->owner->type == bp_none)
1639 warning (_("reading through apparently deleted breakpoint #%d?"),
1640 bl->owner->number);
1641
1642 /* Performance optimization: any further element can no longer affect BUF
1643 content. */
1644
1645 if (bl->address >= bp_locations_placed_address_before_address_max
1646 && memaddr + len <= (bl->address
1647 - bp_locations_placed_address_before_address_max))
1648 break;
1649
1650 if (!bp_location_has_shadow (bl))
1651 continue;
1652
1653 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1654 memaddr, len, &bl->target_info, bl->gdbarch);
1655 }
1656 }
1657
1658 /* See breakpoint.h. */
1659
1660 bool
1661 is_breakpoint (const struct breakpoint *bpt)
1662 {
1663 return (bpt->type == bp_breakpoint
1664 || bpt->type == bp_hardware_breakpoint
1665 || bpt->type == bp_dprintf);
1666 }
1667
1668 /* Return true if BPT is of any hardware watchpoint kind. */
1669
1670 static bool
1671 is_hardware_watchpoint (const struct breakpoint *bpt)
1672 {
1673 return (bpt->type == bp_hardware_watchpoint
1674 || bpt->type == bp_read_watchpoint
1675 || bpt->type == bp_access_watchpoint);
1676 }
1677
1678 /* See breakpoint.h. */
1679
1680 bool
1681 is_watchpoint (const struct breakpoint *bpt)
1682 {
1683 return (is_hardware_watchpoint (bpt)
1684 || bpt->type == bp_watchpoint);
1685 }
1686
1687 /* Returns true if the current thread and its running state are safe
1688 to evaluate or update watchpoint B. Watchpoints on local
1689 expressions need to be evaluated in the context of the thread that
1690 was current when the watchpoint was created, and, that thread needs
1691 to be stopped to be able to select the correct frame context.
1692 Watchpoints on global expressions can be evaluated on any thread,
1693 and in any state. It is presently left to the target allowing
1694 memory accesses when threads are running. */
1695
1696 static int
1697 watchpoint_in_thread_scope (struct watchpoint *b)
1698 {
1699 return (b->pspace == current_program_space
1700 && (b->watchpoint_thread == null_ptid
1701 || (inferior_ptid == b->watchpoint_thread
1702 && !inferior_thread ()->executing)));
1703 }
1704
1705 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1706 associated bp_watchpoint_scope breakpoint. */
1707
1708 static void
1709 watchpoint_del_at_next_stop (struct watchpoint *w)
1710 {
1711 if (w->related_breakpoint != w)
1712 {
1713 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1714 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1715 w->related_breakpoint->disposition = disp_del_at_next_stop;
1716 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1717 w->related_breakpoint = w;
1718 }
1719 w->disposition = disp_del_at_next_stop;
1720 }
1721
1722 /* Extract a bitfield value from value VAL using the bit parameters contained in
1723 watchpoint W. */
1724
1725 static struct value *
1726 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1727 {
1728 struct value *bit_val;
1729
1730 if (val == NULL)
1731 return NULL;
1732
1733 bit_val = allocate_value (value_type (val));
1734
1735 unpack_value_bitfield (bit_val,
1736 w->val_bitpos,
1737 w->val_bitsize,
1738 value_contents_for_printing (val),
1739 value_offset (val),
1740 val);
1741
1742 return bit_val;
1743 }
1744
1745 /* Allocate a dummy location and add it to B, which must be a software
1746 watchpoint. This is required because even if a software watchpoint
1747 is not watching any memory, bpstat_stop_status requires a location
1748 to be able to report stops. */
1749
1750 static void
1751 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1752 struct program_space *pspace)
1753 {
1754 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1755
1756 b->loc = allocate_bp_location (b);
1757 b->loc->pspace = pspace;
1758 b->loc->address = -1;
1759 b->loc->length = -1;
1760 }
1761
1762 /* Returns true if B is a software watchpoint that is not watching any
1763 memory (e.g., "watch $pc"). */
1764
1765 static bool
1766 is_no_memory_software_watchpoint (struct breakpoint *b)
1767 {
1768 return (b->type == bp_watchpoint
1769 && b->loc != NULL
1770 && b->loc->next == NULL
1771 && b->loc->address == -1
1772 && b->loc->length == -1);
1773 }
1774
1775 /* Assuming that B is a watchpoint:
1776 - Reparse watchpoint expression, if REPARSE is non-zero
1777 - Evaluate expression and store the result in B->val
1778 - Evaluate the condition if there is one, and store the result
1779 in b->loc->cond.
1780 - Update the list of values that must be watched in B->loc.
1781
1782 If the watchpoint disposition is disp_del_at_next_stop, then do
1783 nothing. If this is local watchpoint that is out of scope, delete
1784 it.
1785
1786 Even with `set breakpoint always-inserted on' the watchpoints are
1787 removed + inserted on each stop here. Normal breakpoints must
1788 never be removed because they might be missed by a running thread
1789 when debugging in non-stop mode. On the other hand, hardware
1790 watchpoints (is_hardware_watchpoint; processed here) are specific
1791 to each LWP since they are stored in each LWP's hardware debug
1792 registers. Therefore, such LWP must be stopped first in order to
1793 be able to modify its hardware watchpoints.
1794
1795 Hardware watchpoints must be reset exactly once after being
1796 presented to the user. It cannot be done sooner, because it would
1797 reset the data used to present the watchpoint hit to the user. And
1798 it must not be done later because it could display the same single
1799 watchpoint hit during multiple GDB stops. Note that the latter is
1800 relevant only to the hardware watchpoint types bp_read_watchpoint
1801 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1802 not user-visible - its hit is suppressed if the memory content has
1803 not changed.
1804
1805 The following constraints influence the location where we can reset
1806 hardware watchpoints:
1807
1808 * target_stopped_by_watchpoint and target_stopped_data_address are
1809 called several times when GDB stops.
1810
1811 [linux]
1812 * Multiple hardware watchpoints can be hit at the same time,
1813 causing GDB to stop. GDB only presents one hardware watchpoint
1814 hit at a time as the reason for stopping, and all the other hits
1815 are presented later, one after the other, each time the user
1816 requests the execution to be resumed. Execution is not resumed
1817 for the threads still having pending hit event stored in
1818 LWP_INFO->STATUS. While the watchpoint is already removed from
1819 the inferior on the first stop the thread hit event is kept being
1820 reported from its cached value by linux_nat_stopped_data_address
1821 until the real thread resume happens after the watchpoint gets
1822 presented and thus its LWP_INFO->STATUS gets reset.
1823
1824 Therefore the hardware watchpoint hit can get safely reset on the
1825 watchpoint removal from inferior. */
1826
1827 static void
1828 update_watchpoint (struct watchpoint *b, int reparse)
1829 {
1830 int within_current_scope;
1831 struct frame_id saved_frame_id;
1832 int frame_saved;
1833
1834 /* If this is a local watchpoint, we only want to check if the
1835 watchpoint frame is in scope if the current thread is the thread
1836 that was used to create the watchpoint. */
1837 if (!watchpoint_in_thread_scope (b))
1838 return;
1839
1840 if (b->disposition == disp_del_at_next_stop)
1841 return;
1842
1843 frame_saved = 0;
1844
1845 /* Determine if the watchpoint is within scope. */
1846 if (b->exp_valid_block == NULL)
1847 within_current_scope = 1;
1848 else
1849 {
1850 struct frame_info *fi = get_current_frame ();
1851 struct gdbarch *frame_arch = get_frame_arch (fi);
1852 CORE_ADDR frame_pc = get_frame_pc (fi);
1853
1854 /* If we're at a point where the stack has been destroyed
1855 (e.g. in a function epilogue), unwinding may not work
1856 properly. Do not attempt to recreate locations at this
1857 point. See similar comments in watchpoint_check. */
1858 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1859 return;
1860
1861 /* Save the current frame's ID so we can restore it after
1862 evaluating the watchpoint expression on its own frame. */
1863 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1864 took a frame parameter, so that we didn't have to change the
1865 selected frame. */
1866 frame_saved = 1;
1867 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1868
1869 fi = frame_find_by_id (b->watchpoint_frame);
1870 within_current_scope = (fi != NULL);
1871 if (within_current_scope)
1872 select_frame (fi);
1873 }
1874
1875 /* We don't free locations. They are stored in the bp_location array
1876 and update_global_location_list will eventually delete them and
1877 remove breakpoints if needed. */
1878 b->loc = NULL;
1879
1880 if (within_current_scope && reparse)
1881 {
1882 const char *s;
1883
1884 b->exp.reset ();
1885 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1886 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1887 /* If the meaning of expression itself changed, the old value is
1888 no longer relevant. We don't want to report a watchpoint hit
1889 to the user when the old value and the new value may actually
1890 be completely different objects. */
1891 b->val = NULL;
1892 b->val_valid = false;
1893
1894 /* Note that unlike with breakpoints, the watchpoint's condition
1895 expression is stored in the breakpoint object, not in the
1896 locations (re)created below. */
1897 if (b->cond_string != NULL)
1898 {
1899 b->cond_exp.reset ();
1900
1901 s = b->cond_string;
1902 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1903 }
1904 }
1905
1906 /* If we failed to parse the expression, for example because
1907 it refers to a global variable in a not-yet-loaded shared library,
1908 don't try to insert watchpoint. We don't automatically delete
1909 such watchpoint, though, since failure to parse expression
1910 is different from out-of-scope watchpoint. */
1911 if (!target_has_execution ())
1912 {
1913 /* Without execution, memory can't change. No use to try and
1914 set watchpoint locations. The watchpoint will be reset when
1915 the target gains execution, through breakpoint_re_set. */
1916 if (!can_use_hw_watchpoints)
1917 {
1918 if (b->ops->works_in_software_mode (b))
1919 b->type = bp_watchpoint;
1920 else
1921 error (_("Can't set read/access watchpoint when "
1922 "hardware watchpoints are disabled."));
1923 }
1924 }
1925 else if (within_current_scope && b->exp)
1926 {
1927 std::vector<value_ref_ptr> val_chain;
1928 struct value *v, *result;
1929 struct program_space *frame_pspace;
1930
1931 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
1932 &val_chain, false);
1933
1934 /* Avoid setting b->val if it's already set. The meaning of
1935 b->val is 'the last value' user saw, and we should update
1936 it only if we reported that last value to user. As it
1937 happens, the code that reports it updates b->val directly.
1938 We don't keep track of the memory value for masked
1939 watchpoints. */
1940 if (!b->val_valid && !is_masked_watchpoint (b))
1941 {
1942 if (b->val_bitsize != 0)
1943 v = extract_bitfield_from_watchpoint_value (b, v);
1944 b->val = release_value (v);
1945 b->val_valid = true;
1946 }
1947
1948 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1949
1950 /* Look at each value on the value chain. */
1951 gdb_assert (!val_chain.empty ());
1952 for (const value_ref_ptr &iter : val_chain)
1953 {
1954 v = iter.get ();
1955
1956 /* If it's a memory location, and GDB actually needed
1957 its contents to evaluate the expression, then we
1958 must watch it. If the first value returned is
1959 still lazy, that means an error occurred reading it;
1960 watch it anyway in case it becomes readable. */
1961 if (VALUE_LVAL (v) == lval_memory
1962 && (v == val_chain[0] || ! value_lazy (v)))
1963 {
1964 struct type *vtype = check_typedef (value_type (v));
1965
1966 /* We only watch structs and arrays if user asked
1967 for it explicitly, never if they just happen to
1968 appear in the middle of some value chain. */
1969 if (v == result
1970 || (vtype->code () != TYPE_CODE_STRUCT
1971 && vtype->code () != TYPE_CODE_ARRAY))
1972 {
1973 CORE_ADDR addr;
1974 enum target_hw_bp_type type;
1975 struct bp_location *loc, **tmp;
1976 int bitpos = 0, bitsize = 0;
1977
1978 if (value_bitsize (v) != 0)
1979 {
1980 /* Extract the bit parameters out from the bitfield
1981 sub-expression. */
1982 bitpos = value_bitpos (v);
1983 bitsize = value_bitsize (v);
1984 }
1985 else if (v == result && b->val_bitsize != 0)
1986 {
1987 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1988 lvalue whose bit parameters are saved in the fields
1989 VAL_BITPOS and VAL_BITSIZE. */
1990 bitpos = b->val_bitpos;
1991 bitsize = b->val_bitsize;
1992 }
1993
1994 addr = value_address (v);
1995 if (bitsize != 0)
1996 {
1997 /* Skip the bytes that don't contain the bitfield. */
1998 addr += bitpos / 8;
1999 }
2000
2001 type = hw_write;
2002 if (b->type == bp_read_watchpoint)
2003 type = hw_read;
2004 else if (b->type == bp_access_watchpoint)
2005 type = hw_access;
2006
2007 loc = allocate_bp_location (b);
2008 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
2009 ;
2010 *tmp = loc;
2011 loc->gdbarch = value_type (v)->arch ();
2012
2013 loc->pspace = frame_pspace;
2014 loc->address = address_significant (loc->gdbarch, addr);
2015
2016 if (bitsize != 0)
2017 {
2018 /* Just cover the bytes that make up the bitfield. */
2019 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2020 }
2021 else
2022 loc->length = TYPE_LENGTH (value_type (v));
2023
2024 loc->watchpoint_type = type;
2025 }
2026 }
2027 }
2028
2029 /* Change the type of breakpoint between hardware assisted or
2030 an ordinary watchpoint depending on the hardware support
2031 and free hardware slots. REPARSE is set when the inferior
2032 is started. */
2033 if (reparse)
2034 {
2035 int reg_cnt;
2036 enum bp_loc_type loc_type;
2037 struct bp_location *bl;
2038
2039 reg_cnt = can_use_hardware_watchpoint (val_chain);
2040
2041 if (reg_cnt)
2042 {
2043 int i, target_resources_ok, other_type_used;
2044 enum bptype type;
2045
2046 /* Use an exact watchpoint when there's only one memory region to be
2047 watched, and only one debug register is needed to watch it. */
2048 b->exact = target_exact_watchpoints && reg_cnt == 1;
2049
2050 /* We need to determine how many resources are already
2051 used for all other hardware watchpoints plus this one
2052 to see if we still have enough resources to also fit
2053 this watchpoint in as well. */
2054
2055 /* If this is a software watchpoint, we try to turn it
2056 to a hardware one -- count resources as if B was of
2057 hardware watchpoint type. */
2058 type = b->type;
2059 if (type == bp_watchpoint)
2060 type = bp_hardware_watchpoint;
2061
2062 /* This watchpoint may or may not have been placed on
2063 the list yet at this point (it won't be in the list
2064 if we're trying to create it for the first time,
2065 through watch_command), so always account for it
2066 manually. */
2067
2068 /* Count resources used by all watchpoints except B. */
2069 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
2070
2071 /* Add in the resources needed for B. */
2072 i += hw_watchpoint_use_count (b);
2073
2074 target_resources_ok
2075 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2076 if (target_resources_ok <= 0)
2077 {
2078 int sw_mode = b->ops->works_in_software_mode (b);
2079
2080 if (target_resources_ok == 0 && !sw_mode)
2081 error (_("Target does not support this type of "
2082 "hardware watchpoint."));
2083 else if (target_resources_ok < 0 && !sw_mode)
2084 error (_("There are not enough available hardware "
2085 "resources for this watchpoint."));
2086
2087 /* Downgrade to software watchpoint. */
2088 b->type = bp_watchpoint;
2089 }
2090 else
2091 {
2092 /* If this was a software watchpoint, we've just
2093 found we have enough resources to turn it to a
2094 hardware watchpoint. Otherwise, this is a
2095 nop. */
2096 b->type = type;
2097 }
2098 }
2099 else if (!b->ops->works_in_software_mode (b))
2100 {
2101 if (!can_use_hw_watchpoints)
2102 error (_("Can't set read/access watchpoint when "
2103 "hardware watchpoints are disabled."));
2104 else
2105 error (_("Expression cannot be implemented with "
2106 "read/access watchpoint."));
2107 }
2108 else
2109 b->type = bp_watchpoint;
2110
2111 loc_type = (b->type == bp_watchpoint? bp_loc_other
2112 : bp_loc_hardware_watchpoint);
2113 for (bl = b->loc; bl; bl = bl->next)
2114 bl->loc_type = loc_type;
2115 }
2116
2117 /* If a software watchpoint is not watching any memory, then the
2118 above left it without any location set up. But,
2119 bpstat_stop_status requires a location to be able to report
2120 stops, so make sure there's at least a dummy one. */
2121 if (b->type == bp_watchpoint && b->loc == NULL)
2122 software_watchpoint_add_no_memory_location (b, frame_pspace);
2123 }
2124 else if (!within_current_scope)
2125 {
2126 printf_filtered (_("\
2127 Watchpoint %d deleted because the program has left the block\n\
2128 in which its expression is valid.\n"),
2129 b->number);
2130 watchpoint_del_at_next_stop (b);
2131 }
2132
2133 /* Restore the selected frame. */
2134 if (frame_saved)
2135 select_frame (frame_find_by_id (saved_frame_id));
2136 }
2137
2138
2139 /* Returns 1 iff breakpoint location should be
2140 inserted in the inferior. We don't differentiate the type of BL's owner
2141 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2142 breakpoint_ops is not defined, because in insert_bp_location,
2143 tracepoint's insert_location will not be called. */
2144 static int
2145 should_be_inserted (struct bp_location *bl)
2146 {
2147 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2148 return 0;
2149
2150 if (bl->owner->disposition == disp_del_at_next_stop)
2151 return 0;
2152
2153 if (!bl->enabled || bl->disabled_by_cond
2154 || bl->shlib_disabled || bl->duplicate)
2155 return 0;
2156
2157 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2158 return 0;
2159
2160 /* This is set for example, when we're attached to the parent of a
2161 vfork, and have detached from the child. The child is running
2162 free, and we expect it to do an exec or exit, at which point the
2163 OS makes the parent schedulable again (and the target reports
2164 that the vfork is done). Until the child is done with the shared
2165 memory region, do not insert breakpoints in the parent, otherwise
2166 the child could still trip on the parent's breakpoints. Since
2167 the parent is blocked anyway, it won't miss any breakpoint. */
2168 if (bl->pspace->breakpoints_not_allowed)
2169 return 0;
2170
2171 /* Don't insert a breakpoint if we're trying to step past its
2172 location, except if the breakpoint is a single-step breakpoint,
2173 and the breakpoint's thread is the thread which is stepping past
2174 a breakpoint. */
2175 if ((bl->loc_type == bp_loc_software_breakpoint
2176 || bl->loc_type == bp_loc_hardware_breakpoint)
2177 && stepping_past_instruction_at (bl->pspace->aspace,
2178 bl->address)
2179 /* The single-step breakpoint may be inserted at the location
2180 we're trying to step if the instruction branches to itself.
2181 However, the instruction won't be executed at all and it may
2182 break the semantics of the instruction, for example, the
2183 instruction is a conditional branch or updates some flags.
2184 We can't fix it unless GDB is able to emulate the instruction
2185 or switch to displaced stepping. */
2186 && !(bl->owner->type == bp_single_step
2187 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2188 {
2189 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2190 paddress (bl->gdbarch, bl->address));
2191 return 0;
2192 }
2193
2194 /* Don't insert watchpoints if we're trying to step past the
2195 instruction that triggered one. */
2196 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2197 && stepping_past_nonsteppable_watchpoint ())
2198 {
2199 infrun_debug_printf ("stepping past non-steppable watchpoint. "
2200 "skipping watchpoint at %s:%d",
2201 paddress (bl->gdbarch, bl->address), bl->length);
2202 return 0;
2203 }
2204
2205 return 1;
2206 }
2207
2208 /* Same as should_be_inserted but does the check assuming
2209 that the location is not duplicated. */
2210
2211 static int
2212 unduplicated_should_be_inserted (struct bp_location *bl)
2213 {
2214 int result;
2215 const int save_duplicate = bl->duplicate;
2216
2217 bl->duplicate = 0;
2218 result = should_be_inserted (bl);
2219 bl->duplicate = save_duplicate;
2220 return result;
2221 }
2222
2223 /* Parses a conditional described by an expression COND into an
2224 agent expression bytecode suitable for evaluation
2225 by the bytecode interpreter. Return NULL if there was
2226 any error during parsing. */
2227
2228 static agent_expr_up
2229 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2230 {
2231 if (cond == NULL)
2232 return NULL;
2233
2234 agent_expr_up aexpr;
2235
2236 /* We don't want to stop processing, so catch any errors
2237 that may show up. */
2238 try
2239 {
2240 aexpr = gen_eval_for_expr (scope, cond);
2241 }
2242
2243 catch (const gdb_exception_error &ex)
2244 {
2245 /* If we got here, it means the condition could not be parsed to a valid
2246 bytecode expression and thus can't be evaluated on the target's side.
2247 It's no use iterating through the conditions. */
2248 }
2249
2250 /* We have a valid agent expression. */
2251 return aexpr;
2252 }
2253
2254 /* Based on location BL, create a list of breakpoint conditions to be
2255 passed on to the target. If we have duplicated locations with different
2256 conditions, we will add such conditions to the list. The idea is that the
2257 target will evaluate the list of conditions and will only notify GDB when
2258 one of them is true. */
2259
2260 static void
2261 build_target_condition_list (struct bp_location *bl)
2262 {
2263 struct bp_location **locp = NULL, **loc2p;
2264 int null_condition_or_parse_error = 0;
2265 int modified = bl->needs_update;
2266 struct bp_location *loc;
2267
2268 /* Release conditions left over from a previous insert. */
2269 bl->target_info.conditions.clear ();
2270
2271 /* This is only meaningful if the target is
2272 evaluating conditions and if the user has
2273 opted for condition evaluation on the target's
2274 side. */
2275 if (gdb_evaluates_breakpoint_condition_p ()
2276 || !target_supports_evaluation_of_breakpoint_conditions ())
2277 return;
2278
2279 /* Do a first pass to check for locations with no assigned
2280 conditions or conditions that fail to parse to a valid agent
2281 expression bytecode. If any of these happen, then it's no use to
2282 send conditions to the target since this location will always
2283 trigger and generate a response back to GDB. Note we consider
2284 all locations at the same address irrespective of type, i.e.,
2285 even if the locations aren't considered duplicates (e.g.,
2286 software breakpoint and hardware breakpoint at the same
2287 address). */
2288 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2289 {
2290 loc = (*loc2p);
2291 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2292 {
2293 if (modified)
2294 {
2295 /* Re-parse the conditions since something changed. In that
2296 case we already freed the condition bytecodes (see
2297 force_breakpoint_reinsertion). We just
2298 need to parse the condition to bytecodes again. */
2299 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2300 loc->cond.get ());
2301 }
2302
2303 /* If we have a NULL bytecode expression, it means something
2304 went wrong or we have a null condition expression. */
2305 if (!loc->cond_bytecode)
2306 {
2307 null_condition_or_parse_error = 1;
2308 break;
2309 }
2310 }
2311 }
2312
2313 /* If any of these happened, it means we will have to evaluate the conditions
2314 for the location's address on gdb's side. It is no use keeping bytecodes
2315 for all the other duplicate locations, thus we free all of them here.
2316
2317 This is so we have a finer control over which locations' conditions are
2318 being evaluated by GDB or the remote stub. */
2319 if (null_condition_or_parse_error)
2320 {
2321 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2322 {
2323 loc = (*loc2p);
2324 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2325 {
2326 /* Only go as far as the first NULL bytecode is
2327 located. */
2328 if (!loc->cond_bytecode)
2329 return;
2330
2331 loc->cond_bytecode.reset ();
2332 }
2333 }
2334 }
2335
2336 /* No NULL conditions or failed bytecode generation. Build a
2337 condition list for this location's address. If we have software
2338 and hardware locations at the same address, they aren't
2339 considered duplicates, but we still marge all the conditions
2340 anyway, as it's simpler, and doesn't really make a practical
2341 difference. */
2342 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2343 {
2344 loc = (*loc2p);
2345 if (loc->cond
2346 && is_breakpoint (loc->owner)
2347 && loc->pspace->num == bl->pspace->num
2348 && loc->owner->enable_state == bp_enabled
2349 && loc->enabled
2350 && !loc->disabled_by_cond)
2351 {
2352 /* Add the condition to the vector. This will be used later
2353 to send the conditions to the target. */
2354 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2355 }
2356 }
2357
2358 return;
2359 }
2360
2361 /* Parses a command described by string CMD into an agent expression
2362 bytecode suitable for evaluation by the bytecode interpreter.
2363 Return NULL if there was any error during parsing. */
2364
2365 static agent_expr_up
2366 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2367 {
2368 const char *cmdrest;
2369 const char *format_start, *format_end;
2370 struct gdbarch *gdbarch = get_current_arch ();
2371
2372 if (cmd == NULL)
2373 return NULL;
2374
2375 cmdrest = cmd;
2376
2377 if (*cmdrest == ',')
2378 ++cmdrest;
2379 cmdrest = skip_spaces (cmdrest);
2380
2381 if (*cmdrest++ != '"')
2382 error (_("No format string following the location"));
2383
2384 format_start = cmdrest;
2385
2386 format_pieces fpieces (&cmdrest);
2387
2388 format_end = cmdrest;
2389
2390 if (*cmdrest++ != '"')
2391 error (_("Bad format string, non-terminated '\"'."));
2392
2393 cmdrest = skip_spaces (cmdrest);
2394
2395 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2396 error (_("Invalid argument syntax"));
2397
2398 if (*cmdrest == ',')
2399 cmdrest++;
2400 cmdrest = skip_spaces (cmdrest);
2401
2402 /* For each argument, make an expression. */
2403
2404 std::vector<struct expression *> argvec;
2405 while (*cmdrest != '\0')
2406 {
2407 const char *cmd1;
2408
2409 cmd1 = cmdrest;
2410 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2411 argvec.push_back (expr.release ());
2412 cmdrest = cmd1;
2413 if (*cmdrest == ',')
2414 ++cmdrest;
2415 }
2416
2417 agent_expr_up aexpr;
2418
2419 /* We don't want to stop processing, so catch any errors
2420 that may show up. */
2421 try
2422 {
2423 aexpr = gen_printf (scope, gdbarch, 0, 0,
2424 format_start, format_end - format_start,
2425 argvec.size (), argvec.data ());
2426 }
2427 catch (const gdb_exception_error &ex)
2428 {
2429 /* If we got here, it means the command could not be parsed to a valid
2430 bytecode expression and thus can't be evaluated on the target's side.
2431 It's no use iterating through the other commands. */
2432 }
2433
2434 /* We have a valid agent expression, return it. */
2435 return aexpr;
2436 }
2437
2438 /* Based on location BL, create a list of breakpoint commands to be
2439 passed on to the target. If we have duplicated locations with
2440 different commands, we will add any such to the list. */
2441
2442 static void
2443 build_target_command_list (struct bp_location *bl)
2444 {
2445 struct bp_location **locp = NULL, **loc2p;
2446 int null_command_or_parse_error = 0;
2447 int modified = bl->needs_update;
2448 struct bp_location *loc;
2449
2450 /* Clear commands left over from a previous insert. */
2451 bl->target_info.tcommands.clear ();
2452
2453 if (!target_can_run_breakpoint_commands ())
2454 return;
2455
2456 /* For now, limit to agent-style dprintf breakpoints. */
2457 if (dprintf_style != dprintf_style_agent)
2458 return;
2459
2460 /* For now, if we have any location at the same address that isn't a
2461 dprintf, don't install the target-side commands, as that would
2462 make the breakpoint not be reported to the core, and we'd lose
2463 control. */
2464 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2465 {
2466 loc = (*loc2p);
2467 if (is_breakpoint (loc->owner)
2468 && loc->pspace->num == bl->pspace->num
2469 && loc->owner->type != bp_dprintf)
2470 return;
2471 }
2472
2473 /* Do a first pass to check for locations with no assigned
2474 conditions or conditions that fail to parse to a valid agent expression
2475 bytecode. If any of these happen, then it's no use to send conditions
2476 to the target since this location will always trigger and generate a
2477 response back to GDB. */
2478 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2479 {
2480 loc = (*loc2p);
2481 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2482 {
2483 if (modified)
2484 {
2485 /* Re-parse the commands since something changed. In that
2486 case we already freed the command bytecodes (see
2487 force_breakpoint_reinsertion). We just
2488 need to parse the command to bytecodes again. */
2489 loc->cmd_bytecode
2490 = parse_cmd_to_aexpr (bl->address,
2491 loc->owner->extra_string);
2492 }
2493
2494 /* If we have a NULL bytecode expression, it means something
2495 went wrong or we have a null command expression. */
2496 if (!loc->cmd_bytecode)
2497 {
2498 null_command_or_parse_error = 1;
2499 break;
2500 }
2501 }
2502 }
2503
2504 /* If anything failed, then we're not doing target-side commands,
2505 and so clean up. */
2506 if (null_command_or_parse_error)
2507 {
2508 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2509 {
2510 loc = (*loc2p);
2511 if (is_breakpoint (loc->owner)
2512 && loc->pspace->num == bl->pspace->num)
2513 {
2514 /* Only go as far as the first NULL bytecode is
2515 located. */
2516 if (loc->cmd_bytecode == NULL)
2517 return;
2518
2519 loc->cmd_bytecode.reset ();
2520 }
2521 }
2522 }
2523
2524 /* No NULL commands or failed bytecode generation. Build a command
2525 list for all duplicate locations at this location's address.
2526 Note that here we must care for whether the breakpoint location
2527 types are considered duplicates, otherwise, say, if we have a
2528 software and hardware location at the same address, the target
2529 could end up running the commands twice. For the moment, we only
2530 support targets-side commands with dprintf, but it doesn't hurt
2531 to be pedantically correct in case that changes. */
2532 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2533 {
2534 loc = (*loc2p);
2535 if (breakpoint_locations_match (bl, loc)
2536 && loc->owner->extra_string
2537 && is_breakpoint (loc->owner)
2538 && loc->pspace->num == bl->pspace->num
2539 && loc->owner->enable_state == bp_enabled
2540 && loc->enabled
2541 && !loc->disabled_by_cond)
2542 {
2543 /* Add the command to the vector. This will be used later
2544 to send the commands to the target. */
2545 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2546 }
2547 }
2548
2549 bl->target_info.persist = 0;
2550 /* Maybe flag this location as persistent. */
2551 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2552 bl->target_info.persist = 1;
2553 }
2554
2555 /* Return the kind of breakpoint on address *ADDR. Get the kind
2556 of breakpoint according to ADDR except single-step breakpoint.
2557 Get the kind of single-step breakpoint according to the current
2558 registers state. */
2559
2560 static int
2561 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2562 {
2563 if (bl->owner->type == bp_single_step)
2564 {
2565 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2566 struct regcache *regcache;
2567
2568 regcache = get_thread_regcache (thr);
2569
2570 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2571 regcache, addr);
2572 }
2573 else
2574 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2575 }
2576
2577 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2578 location. Any error messages are printed to TMP_ERROR_STREAM; and
2579 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2580 Returns 0 for success, 1 if the bp_location type is not supported or
2581 -1 for failure.
2582
2583 NOTE drow/2003-09-09: This routine could be broken down to an
2584 object-style method for each breakpoint or catchpoint type. */
2585 static int
2586 insert_bp_location (struct bp_location *bl,
2587 struct ui_file *tmp_error_stream,
2588 int *disabled_breaks,
2589 int *hw_breakpoint_error,
2590 int *hw_bp_error_explained_already)
2591 {
2592 gdb_exception bp_excpt;
2593
2594 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2595 return 0;
2596
2597 /* Note we don't initialize bl->target_info, as that wipes out
2598 the breakpoint location's shadow_contents if the breakpoint
2599 is still inserted at that location. This in turn breaks
2600 target_read_memory which depends on these buffers when
2601 a memory read is requested at the breakpoint location:
2602 Once the target_info has been wiped, we fail to see that
2603 we have a breakpoint inserted at that address and thus
2604 read the breakpoint instead of returning the data saved in
2605 the breakpoint location's shadow contents. */
2606 bl->target_info.reqstd_address = bl->address;
2607 bl->target_info.placed_address_space = bl->pspace->aspace;
2608 bl->target_info.length = bl->length;
2609
2610 /* When working with target-side conditions, we must pass all the conditions
2611 for the same breakpoint address down to the target since GDB will not
2612 insert those locations. With a list of breakpoint conditions, the target
2613 can decide when to stop and notify GDB. */
2614
2615 if (is_breakpoint (bl->owner))
2616 {
2617 build_target_condition_list (bl);
2618 build_target_command_list (bl);
2619 /* Reset the modification marker. */
2620 bl->needs_update = 0;
2621 }
2622
2623 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2624 set at a read-only address, then a breakpoint location will have
2625 been changed to hardware breakpoint before we get here. If it is
2626 "off" however, error out before actually trying to insert the
2627 breakpoint, with a nicer error message. */
2628 if (bl->loc_type == bp_loc_software_breakpoint
2629 && !automatic_hardware_breakpoints)
2630 {
2631 mem_region *mr = lookup_mem_region (bl->address);
2632
2633 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2634 {
2635 fprintf_unfiltered (tmp_error_stream,
2636 _("Cannot insert breakpoint %d.\n"
2637 "Cannot set software breakpoint "
2638 "at read-only address %s\n"),
2639 bl->owner->number,
2640 paddress (bl->gdbarch, bl->address));
2641 return 1;
2642 }
2643 }
2644
2645 if (bl->loc_type == bp_loc_software_breakpoint
2646 || bl->loc_type == bp_loc_hardware_breakpoint)
2647 {
2648 /* First check to see if we have to handle an overlay. */
2649 if (overlay_debugging == ovly_off
2650 || bl->section == NULL
2651 || !(section_is_overlay (bl->section)))
2652 {
2653 /* No overlay handling: just set the breakpoint. */
2654 try
2655 {
2656 int val;
2657
2658 val = bl->owner->ops->insert_location (bl);
2659 if (val)
2660 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2661 }
2662 catch (gdb_exception &e)
2663 {
2664 bp_excpt = std::move (e);
2665 }
2666 }
2667 else
2668 {
2669 /* This breakpoint is in an overlay section.
2670 Shall we set a breakpoint at the LMA? */
2671 if (!overlay_events_enabled)
2672 {
2673 /* Yes -- overlay event support is not active,
2674 so we must try to set a breakpoint at the LMA.
2675 This will not work for a hardware breakpoint. */
2676 if (bl->loc_type == bp_loc_hardware_breakpoint)
2677 warning (_("hardware breakpoint %d not supported in overlay!"),
2678 bl->owner->number);
2679 else
2680 {
2681 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2682 bl->section);
2683 /* Set a software (trap) breakpoint at the LMA. */
2684 bl->overlay_target_info = bl->target_info;
2685 bl->overlay_target_info.reqstd_address = addr;
2686
2687 /* No overlay handling: just set the breakpoint. */
2688 try
2689 {
2690 int val;
2691
2692 bl->overlay_target_info.kind
2693 = breakpoint_kind (bl, &addr);
2694 bl->overlay_target_info.placed_address = addr;
2695 val = target_insert_breakpoint (bl->gdbarch,
2696 &bl->overlay_target_info);
2697 if (val)
2698 bp_excpt
2699 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2700 }
2701 catch (gdb_exception &e)
2702 {
2703 bp_excpt = std::move (e);
2704 }
2705
2706 if (bp_excpt.reason != 0)
2707 fprintf_unfiltered (tmp_error_stream,
2708 "Overlay breakpoint %d "
2709 "failed: in ROM?\n",
2710 bl->owner->number);
2711 }
2712 }
2713 /* Shall we set a breakpoint at the VMA? */
2714 if (section_is_mapped (bl->section))
2715 {
2716 /* Yes. This overlay section is mapped into memory. */
2717 try
2718 {
2719 int val;
2720
2721 val = bl->owner->ops->insert_location (bl);
2722 if (val)
2723 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2724 }
2725 catch (gdb_exception &e)
2726 {
2727 bp_excpt = std::move (e);
2728 }
2729 }
2730 else
2731 {
2732 /* No. This breakpoint will not be inserted.
2733 No error, but do not mark the bp as 'inserted'. */
2734 return 0;
2735 }
2736 }
2737
2738 if (bp_excpt.reason != 0)
2739 {
2740 /* Can't set the breakpoint. */
2741
2742 /* In some cases, we might not be able to insert a
2743 breakpoint in a shared library that has already been
2744 removed, but we have not yet processed the shlib unload
2745 event. Unfortunately, some targets that implement
2746 breakpoint insertion themselves can't tell why the
2747 breakpoint insertion failed (e.g., the remote target
2748 doesn't define error codes), so we must treat generic
2749 errors as memory errors. */
2750 if (bp_excpt.reason == RETURN_ERROR
2751 && (bp_excpt.error == GENERIC_ERROR
2752 || bp_excpt.error == MEMORY_ERROR)
2753 && bl->loc_type == bp_loc_software_breakpoint
2754 && (solib_name_from_address (bl->pspace, bl->address)
2755 || shared_objfile_contains_address_p (bl->pspace,
2756 bl->address)))
2757 {
2758 /* See also: disable_breakpoints_in_shlibs. */
2759 bl->shlib_disabled = 1;
2760 gdb::observers::breakpoint_modified.notify (bl->owner);
2761 if (!*disabled_breaks)
2762 {
2763 fprintf_unfiltered (tmp_error_stream,
2764 "Cannot insert breakpoint %d.\n",
2765 bl->owner->number);
2766 fprintf_unfiltered (tmp_error_stream,
2767 "Temporarily disabling shared "
2768 "library breakpoints:\n");
2769 }
2770 *disabled_breaks = 1;
2771 fprintf_unfiltered (tmp_error_stream,
2772 "breakpoint #%d\n", bl->owner->number);
2773 return 0;
2774 }
2775 else
2776 {
2777 if (bl->loc_type == bp_loc_hardware_breakpoint)
2778 {
2779 *hw_breakpoint_error = 1;
2780 *hw_bp_error_explained_already = bp_excpt.message != NULL;
2781 fprintf_unfiltered (tmp_error_stream,
2782 "Cannot insert hardware breakpoint %d%s",
2783 bl->owner->number,
2784 bp_excpt.message ? ":" : ".\n");
2785 if (bp_excpt.message != NULL)
2786 fprintf_unfiltered (tmp_error_stream, "%s.\n",
2787 bp_excpt.what ());
2788 }
2789 else
2790 {
2791 if (bp_excpt.message == NULL)
2792 {
2793 std::string message
2794 = memory_error_message (TARGET_XFER_E_IO,
2795 bl->gdbarch, bl->address);
2796
2797 fprintf_unfiltered (tmp_error_stream,
2798 "Cannot insert breakpoint %d.\n"
2799 "%s\n",
2800 bl->owner->number, message.c_str ());
2801 }
2802 else
2803 {
2804 fprintf_unfiltered (tmp_error_stream,
2805 "Cannot insert breakpoint %d: %s\n",
2806 bl->owner->number,
2807 bp_excpt.what ());
2808 }
2809 }
2810 return 1;
2811
2812 }
2813 }
2814 else
2815 bl->inserted = 1;
2816
2817 return 0;
2818 }
2819
2820 else if (bl->loc_type == bp_loc_hardware_watchpoint
2821 /* NOTE drow/2003-09-08: This state only exists for removing
2822 watchpoints. It's not clear that it's necessary... */
2823 && bl->owner->disposition != disp_del_at_next_stop)
2824 {
2825 int val;
2826
2827 gdb_assert (bl->owner->ops != NULL
2828 && bl->owner->ops->insert_location != NULL);
2829
2830 val = bl->owner->ops->insert_location (bl);
2831
2832 /* If trying to set a read-watchpoint, and it turns out it's not
2833 supported, try emulating one with an access watchpoint. */
2834 if (val == 1 && bl->watchpoint_type == hw_read)
2835 {
2836 struct bp_location *loc, **loc_temp;
2837
2838 /* But don't try to insert it, if there's already another
2839 hw_access location that would be considered a duplicate
2840 of this one. */
2841 ALL_BP_LOCATIONS (loc, loc_temp)
2842 if (loc != bl
2843 && loc->watchpoint_type == hw_access
2844 && watchpoint_locations_match (bl, loc))
2845 {
2846 bl->duplicate = 1;
2847 bl->inserted = 1;
2848 bl->target_info = loc->target_info;
2849 bl->watchpoint_type = hw_access;
2850 val = 0;
2851 break;
2852 }
2853
2854 if (val == 1)
2855 {
2856 bl->watchpoint_type = hw_access;
2857 val = bl->owner->ops->insert_location (bl);
2858
2859 if (val)
2860 /* Back to the original value. */
2861 bl->watchpoint_type = hw_read;
2862 }
2863 }
2864
2865 bl->inserted = (val == 0);
2866 }
2867
2868 else if (bl->owner->type == bp_catchpoint)
2869 {
2870 int val;
2871
2872 gdb_assert (bl->owner->ops != NULL
2873 && bl->owner->ops->insert_location != NULL);
2874
2875 val = bl->owner->ops->insert_location (bl);
2876 if (val)
2877 {
2878 bl->owner->enable_state = bp_disabled;
2879
2880 if (val == 1)
2881 warning (_("\
2882 Error inserting catchpoint %d: Your system does not support this type\n\
2883 of catchpoint."), bl->owner->number);
2884 else
2885 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2886 }
2887
2888 bl->inserted = (val == 0);
2889
2890 /* We've already printed an error message if there was a problem
2891 inserting this catchpoint, and we've disabled the catchpoint,
2892 so just return success. */
2893 return 0;
2894 }
2895
2896 return 0;
2897 }
2898
2899 /* This function is called when program space PSPACE is about to be
2900 deleted. It takes care of updating breakpoints to not reference
2901 PSPACE anymore. */
2902
2903 void
2904 breakpoint_program_space_exit (struct program_space *pspace)
2905 {
2906 struct bp_location *loc, **loc_temp;
2907
2908 /* Remove any breakpoint that was set through this program space. */
2909 for (breakpoint *b : all_breakpoints_safe ())
2910 if (b->pspace == pspace)
2911 delete_breakpoint (b);
2912
2913 /* Breakpoints set through other program spaces could have locations
2914 bound to PSPACE as well. Remove those. */
2915 ALL_BP_LOCATIONS (loc, loc_temp)
2916 {
2917 struct bp_location *tmp;
2918
2919 if (loc->pspace == pspace)
2920 {
2921 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2922 if (loc->owner->loc == loc)
2923 loc->owner->loc = loc->next;
2924 else
2925 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2926 if (tmp->next == loc)
2927 {
2928 tmp->next = loc->next;
2929 break;
2930 }
2931 }
2932 }
2933
2934 /* Now update the global location list to permanently delete the
2935 removed locations above. */
2936 update_global_location_list (UGLL_DONT_INSERT);
2937 }
2938
2939 /* Make sure all breakpoints are inserted in inferior.
2940 Throws exception on any error.
2941 A breakpoint that is already inserted won't be inserted
2942 again, so calling this function twice is safe. */
2943 void
2944 insert_breakpoints (void)
2945 {
2946 for (breakpoint *bpt : all_breakpoints ())
2947 if (is_hardware_watchpoint (bpt))
2948 {
2949 struct watchpoint *w = (struct watchpoint *) bpt;
2950
2951 update_watchpoint (w, 0 /* don't reparse. */);
2952 }
2953
2954 /* Updating watchpoints creates new locations, so update the global
2955 location list. Explicitly tell ugll to insert locations and
2956 ignore breakpoints_always_inserted_mode. Also,
2957 update_global_location_list tries to "upgrade" software
2958 breakpoints to hardware breakpoints to handle "set breakpoint
2959 auto-hw", so we need to call it even if we don't have new
2960 locations. */
2961 update_global_location_list (UGLL_INSERT);
2962 }
2963
2964 /* Invoke CALLBACK for each of bp_location. */
2965
2966 void
2967 iterate_over_bp_locations (gdb::function_view<void (bp_location *)> callback)
2968 {
2969 struct bp_location *loc, **loc_tmp;
2970
2971 ALL_BP_LOCATIONS (loc, loc_tmp)
2972 {
2973 callback (loc);
2974 }
2975 }
2976
2977 /* This is used when we need to synch breakpoint conditions between GDB and the
2978 target. It is the case with deleting and disabling of breakpoints when using
2979 always-inserted mode. */
2980
2981 static void
2982 update_inserted_breakpoint_locations (void)
2983 {
2984 struct bp_location *bl, **blp_tmp;
2985 int error_flag = 0;
2986 int val = 0;
2987 int disabled_breaks = 0;
2988 int hw_breakpoint_error = 0;
2989 int hw_bp_details_reported = 0;
2990
2991 string_file tmp_error_stream;
2992
2993 /* Explicitly mark the warning -- this will only be printed if
2994 there was an error. */
2995 tmp_error_stream.puts ("Warning:\n");
2996
2997 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2998
2999 ALL_BP_LOCATIONS (bl, blp_tmp)
3000 {
3001 /* We only want to update software breakpoints and hardware
3002 breakpoints. */
3003 if (!is_breakpoint (bl->owner))
3004 continue;
3005
3006 /* We only want to update locations that are already inserted
3007 and need updating. This is to avoid unwanted insertion during
3008 deletion of breakpoints. */
3009 if (!bl->inserted || !bl->needs_update)
3010 continue;
3011
3012 switch_to_program_space_and_thread (bl->pspace);
3013
3014 /* For targets that support global breakpoints, there's no need
3015 to select an inferior to insert breakpoint to. In fact, even
3016 if we aren't attached to any process yet, we should still
3017 insert breakpoints. */
3018 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3019 && (inferior_ptid == null_ptid || !target_has_execution ()))
3020 continue;
3021
3022 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3023 &hw_breakpoint_error, &hw_bp_details_reported);
3024 if (val)
3025 error_flag = val;
3026 }
3027
3028 if (error_flag)
3029 {
3030 target_terminal::ours_for_output ();
3031 error_stream (tmp_error_stream);
3032 }
3033 }
3034
3035 /* Used when starting or continuing the program. */
3036
3037 static void
3038 insert_breakpoint_locations (void)
3039 {
3040 struct bp_location *bl, **blp_tmp;
3041 int error_flag = 0;
3042 int val = 0;
3043 int disabled_breaks = 0;
3044 int hw_breakpoint_error = 0;
3045 int hw_bp_error_explained_already = 0;
3046
3047 string_file tmp_error_stream;
3048
3049 /* Explicitly mark the warning -- this will only be printed if
3050 there was an error. */
3051 tmp_error_stream.puts ("Warning:\n");
3052
3053 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3054
3055 ALL_BP_LOCATIONS (bl, blp_tmp)
3056 {
3057 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3058 continue;
3059
3060 /* There is no point inserting thread-specific breakpoints if
3061 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3062 has BL->OWNER always non-NULL. */
3063 if (bl->owner->thread != -1
3064 && !valid_global_thread_id (bl->owner->thread))
3065 continue;
3066
3067 switch_to_program_space_and_thread (bl->pspace);
3068
3069 /* For targets that support global breakpoints, there's no need
3070 to select an inferior to insert breakpoint to. In fact, even
3071 if we aren't attached to any process yet, we should still
3072 insert breakpoints. */
3073 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3074 && (inferior_ptid == null_ptid || !target_has_execution ()))
3075 continue;
3076
3077 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3078 &hw_breakpoint_error, &hw_bp_error_explained_already);
3079 if (val)
3080 error_flag = val;
3081 }
3082
3083 /* If we failed to insert all locations of a watchpoint, remove
3084 them, as half-inserted watchpoint is of limited use. */
3085 for (breakpoint *bpt : all_breakpoints ())
3086 {
3087 int some_failed = 0;
3088 struct bp_location *loc;
3089
3090 if (!is_hardware_watchpoint (bpt))
3091 continue;
3092
3093 if (!breakpoint_enabled (bpt))
3094 continue;
3095
3096 if (bpt->disposition == disp_del_at_next_stop)
3097 continue;
3098
3099 for (loc = bpt->loc; loc; loc = loc->next)
3100 if (!loc->inserted && should_be_inserted (loc))
3101 {
3102 some_failed = 1;
3103 break;
3104 }
3105 if (some_failed)
3106 {
3107 for (loc = bpt->loc; loc; loc = loc->next)
3108 if (loc->inserted)
3109 remove_breakpoint (loc);
3110
3111 hw_breakpoint_error = 1;
3112 tmp_error_stream.printf ("Could not insert "
3113 "hardware watchpoint %d.\n",
3114 bpt->number);
3115 error_flag = -1;
3116 }
3117 }
3118
3119 if (error_flag)
3120 {
3121 /* If a hardware breakpoint or watchpoint was inserted, add a
3122 message about possibly exhausted resources. */
3123 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3124 {
3125 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3126 You may have requested too many hardware breakpoints/watchpoints.\n");
3127 }
3128 target_terminal::ours_for_output ();
3129 error_stream (tmp_error_stream);
3130 }
3131 }
3132
3133 /* Used when the program stops.
3134 Returns zero if successful, or non-zero if there was a problem
3135 removing a breakpoint location. */
3136
3137 int
3138 remove_breakpoints (void)
3139 {
3140 struct bp_location *bl, **blp_tmp;
3141 int val = 0;
3142
3143 ALL_BP_LOCATIONS (bl, blp_tmp)
3144 {
3145 if (bl->inserted && !is_tracepoint (bl->owner))
3146 val |= remove_breakpoint (bl);
3147 }
3148 return val;
3149 }
3150
3151 /* When a thread exits, remove breakpoints that are related to
3152 that thread. */
3153
3154 static void
3155 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3156 {
3157 for (breakpoint *b : all_breakpoints_safe ())
3158 {
3159 if (b->thread == tp->global_num && user_breakpoint_p (b))
3160 {
3161 b->disposition = disp_del_at_next_stop;
3162
3163 printf_filtered (_("\
3164 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3165 b->number, print_thread_id (tp));
3166
3167 /* Hide it from the user. */
3168 b->number = 0;
3169 }
3170 }
3171 }
3172
3173 /* See breakpoint.h. */
3174
3175 void
3176 remove_breakpoints_inf (inferior *inf)
3177 {
3178 struct bp_location *bl, **blp_tmp;
3179 int val;
3180
3181 ALL_BP_LOCATIONS (bl, blp_tmp)
3182 {
3183 if (bl->pspace != inf->pspace)
3184 continue;
3185
3186 if (bl->inserted && !bl->target_info.persist)
3187 {
3188 val = remove_breakpoint (bl);
3189 if (val != 0)
3190 return;
3191 }
3192 }
3193 }
3194
3195 static int internal_breakpoint_number = -1;
3196
3197 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3198 If INTERNAL is non-zero, the breakpoint number will be populated
3199 from internal_breakpoint_number and that variable decremented.
3200 Otherwise the breakpoint number will be populated from
3201 breakpoint_count and that value incremented. Internal breakpoints
3202 do not set the internal var bpnum. */
3203 static void
3204 set_breakpoint_number (int internal, struct breakpoint *b)
3205 {
3206 if (internal)
3207 b->number = internal_breakpoint_number--;
3208 else
3209 {
3210 set_breakpoint_count (breakpoint_count + 1);
3211 b->number = breakpoint_count;
3212 }
3213 }
3214
3215 static struct breakpoint *
3216 create_internal_breakpoint (struct gdbarch *gdbarch,
3217 CORE_ADDR address, enum bptype type,
3218 const struct breakpoint_ops *ops)
3219 {
3220 symtab_and_line sal;
3221 sal.pc = address;
3222 sal.section = find_pc_overlay (sal.pc);
3223 sal.pspace = current_program_space;
3224
3225 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
3226 b->number = internal_breakpoint_number--;
3227 b->disposition = disp_donttouch;
3228
3229 return b;
3230 }
3231
3232 static const char *const longjmp_names[] =
3233 {
3234 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3235 };
3236 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3237
3238 /* Per-objfile data private to breakpoint.c. */
3239 struct breakpoint_objfile_data
3240 {
3241 /* Minimal symbol for "_ovly_debug_event" (if any). */
3242 struct bound_minimal_symbol overlay_msym {};
3243
3244 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3245 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
3246
3247 /* True if we have looked for longjmp probes. */
3248 int longjmp_searched = 0;
3249
3250 /* SystemTap probe points for longjmp (if any). These are non-owning
3251 references. */
3252 std::vector<probe *> longjmp_probes;
3253
3254 /* Minimal symbol for "std::terminate()" (if any). */
3255 struct bound_minimal_symbol terminate_msym {};
3256
3257 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3258 struct bound_minimal_symbol exception_msym {};
3259
3260 /* True if we have looked for exception probes. */
3261 int exception_searched = 0;
3262
3263 /* SystemTap probe points for unwinding (if any). These are non-owning
3264 references. */
3265 std::vector<probe *> exception_probes;
3266 };
3267
3268 static const struct objfile_key<breakpoint_objfile_data>
3269 breakpoint_objfile_key;
3270
3271 /* Minimal symbol not found sentinel. */
3272 static struct minimal_symbol msym_not_found;
3273
3274 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3275
3276 static int
3277 msym_not_found_p (const struct minimal_symbol *msym)
3278 {
3279 return msym == &msym_not_found;
3280 }
3281
3282 /* Return per-objfile data needed by breakpoint.c.
3283 Allocate the data if necessary. */
3284
3285 static struct breakpoint_objfile_data *
3286 get_breakpoint_objfile_data (struct objfile *objfile)
3287 {
3288 struct breakpoint_objfile_data *bp_objfile_data;
3289
3290 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3291 if (bp_objfile_data == NULL)
3292 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3293 return bp_objfile_data;
3294 }
3295
3296 static void
3297 create_overlay_event_breakpoint (void)
3298 {
3299 const char *const func_name = "_ovly_debug_event";
3300
3301 for (objfile *objfile : current_program_space->objfiles ())
3302 {
3303 struct breakpoint *b;
3304 struct breakpoint_objfile_data *bp_objfile_data;
3305 CORE_ADDR addr;
3306 struct explicit_location explicit_loc;
3307
3308 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3309
3310 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3311 continue;
3312
3313 if (bp_objfile_data->overlay_msym.minsym == NULL)
3314 {
3315 struct bound_minimal_symbol m;
3316
3317 m = lookup_minimal_symbol_text (func_name, objfile);
3318 if (m.minsym == NULL)
3319 {
3320 /* Avoid future lookups in this objfile. */
3321 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3322 continue;
3323 }
3324 bp_objfile_data->overlay_msym = m;
3325 }
3326
3327 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3328 b = create_internal_breakpoint (objfile->arch (), addr,
3329 bp_overlay_event,
3330 &internal_breakpoint_ops);
3331 initialize_explicit_location (&explicit_loc);
3332 explicit_loc.function_name = ASTRDUP (func_name);
3333 b->location = new_explicit_location (&explicit_loc);
3334
3335 if (overlay_debugging == ovly_auto)
3336 {
3337 b->enable_state = bp_enabled;
3338 overlay_events_enabled = 1;
3339 }
3340 else
3341 {
3342 b->enable_state = bp_disabled;
3343 overlay_events_enabled = 0;
3344 }
3345 }
3346 }
3347
3348 /* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3349 true if a breakpoint was installed. */
3350
3351 static bool
3352 create_longjmp_master_breakpoint_probe (objfile *objfile)
3353 {
3354 struct gdbarch *gdbarch = objfile->arch ();
3355 struct breakpoint_objfile_data *bp_objfile_data
3356 = get_breakpoint_objfile_data (objfile);
3357
3358 if (!bp_objfile_data->longjmp_searched)
3359 {
3360 std::vector<probe *> ret
3361 = find_probes_in_objfile (objfile, "libc", "longjmp");
3362
3363 if (!ret.empty ())
3364 {
3365 /* We are only interested in checking one element. */
3366 probe *p = ret[0];
3367
3368 if (!p->can_evaluate_arguments ())
3369 {
3370 /* We cannot use the probe interface here,
3371 because it does not know how to evaluate
3372 arguments. */
3373 ret.clear ();
3374 }
3375 }
3376 bp_objfile_data->longjmp_probes = ret;
3377 bp_objfile_data->longjmp_searched = 1;
3378 }
3379
3380 if (bp_objfile_data->longjmp_probes.empty ())
3381 return false;
3382
3383 for (probe *p : bp_objfile_data->longjmp_probes)
3384 {
3385 struct breakpoint *b;
3386
3387 b = create_internal_breakpoint (gdbarch,
3388 p->get_relocated_address (objfile),
3389 bp_longjmp_master,
3390 &internal_breakpoint_ops);
3391 b->location = new_probe_location ("-probe-stap libc:longjmp");
3392 b->enable_state = bp_disabled;
3393 }
3394
3395 return true;
3396 }
3397
3398 /* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3399 Return true if at least one breakpoint was installed. */
3400
3401 static bool
3402 create_longjmp_master_breakpoint_names (objfile *objfile)
3403 {
3404 struct gdbarch *gdbarch = objfile->arch ();
3405 if (!gdbarch_get_longjmp_target_p (gdbarch))
3406 return false;
3407
3408 struct breakpoint_objfile_data *bp_objfile_data
3409 = get_breakpoint_objfile_data (objfile);
3410 unsigned int installed_bp = 0;
3411
3412 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3413 {
3414 struct breakpoint *b;
3415 const char *func_name;
3416 CORE_ADDR addr;
3417 struct explicit_location explicit_loc;
3418
3419 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3420 continue;
3421
3422 func_name = longjmp_names[i];
3423 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3424 {
3425 struct bound_minimal_symbol m;
3426
3427 m = lookup_minimal_symbol_text (func_name, objfile);
3428 if (m.minsym == NULL)
3429 {
3430 /* Prevent future lookups in this objfile. */
3431 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3432 continue;
3433 }
3434 bp_objfile_data->longjmp_msym[i] = m;
3435 }
3436
3437 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3438 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3439 &internal_breakpoint_ops);
3440 initialize_explicit_location (&explicit_loc);
3441 explicit_loc.function_name = ASTRDUP (func_name);
3442 b->location = new_explicit_location (&explicit_loc);
3443 b->enable_state = bp_disabled;
3444 installed_bp++;
3445 }
3446
3447 return installed_bp > 0;
3448 }
3449
3450 /* Create a master longjmp breakpoint. */
3451
3452 static void
3453 create_longjmp_master_breakpoint (void)
3454 {
3455 scoped_restore_current_program_space restore_pspace;
3456
3457 for (struct program_space *pspace : program_spaces)
3458 {
3459 set_current_program_space (pspace);
3460
3461 for (objfile *obj : current_program_space->objfiles ())
3462 {
3463 /* Skip separate debug object, it's handled in the loop below. */
3464 if (obj->separate_debug_objfile_backlink != nullptr)
3465 continue;
3466
3467 /* Try a probe kind breakpoint on main objfile. */
3468 if (create_longjmp_master_breakpoint_probe (obj))
3469 continue;
3470
3471 /* Try longjmp_names kind breakpoints on main and separate_debug
3472 objfiles. */
3473 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3474 if (create_longjmp_master_breakpoint_names (debug_objfile))
3475 break;
3476 }
3477 }
3478 }
3479
3480 /* Create a master std::terminate breakpoint. */
3481 static void
3482 create_std_terminate_master_breakpoint (void)
3483 {
3484 const char *const func_name = "std::terminate()";
3485
3486 scoped_restore_current_program_space restore_pspace;
3487
3488 for (struct program_space *pspace : program_spaces)
3489 {
3490 CORE_ADDR addr;
3491
3492 set_current_program_space (pspace);
3493
3494 for (objfile *objfile : current_program_space->objfiles ())
3495 {
3496 struct breakpoint *b;
3497 struct breakpoint_objfile_data *bp_objfile_data;
3498 struct explicit_location explicit_loc;
3499
3500 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3501
3502 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3503 continue;
3504
3505 if (bp_objfile_data->terminate_msym.minsym == NULL)
3506 {
3507 struct bound_minimal_symbol m;
3508
3509 m = lookup_minimal_symbol (func_name, NULL, objfile);
3510 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3511 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3512 {
3513 /* Prevent future lookups in this objfile. */
3514 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3515 continue;
3516 }
3517 bp_objfile_data->terminate_msym = m;
3518 }
3519
3520 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3521 b = create_internal_breakpoint (objfile->arch (), addr,
3522 bp_std_terminate_master,
3523 &internal_breakpoint_ops);
3524 initialize_explicit_location (&explicit_loc);
3525 explicit_loc.function_name = ASTRDUP (func_name);
3526 b->location = new_explicit_location (&explicit_loc);
3527 b->enable_state = bp_disabled;
3528 }
3529 }
3530 }
3531
3532 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3533 probe. Return true if a breakpoint was installed. */
3534
3535 static bool
3536 create_exception_master_breakpoint_probe (objfile *objfile)
3537 {
3538 struct breakpoint *b;
3539 struct gdbarch *gdbarch;
3540 struct breakpoint_objfile_data *bp_objfile_data;
3541
3542 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3543
3544 /* We prefer the SystemTap probe point if it exists. */
3545 if (!bp_objfile_data->exception_searched)
3546 {
3547 std::vector<probe *> ret
3548 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3549
3550 if (!ret.empty ())
3551 {
3552 /* We are only interested in checking one element. */
3553 probe *p = ret[0];
3554
3555 if (!p->can_evaluate_arguments ())
3556 {
3557 /* We cannot use the probe interface here, because it does
3558 not know how to evaluate arguments. */
3559 ret.clear ();
3560 }
3561 }
3562 bp_objfile_data->exception_probes = ret;
3563 bp_objfile_data->exception_searched = 1;
3564 }
3565
3566 if (bp_objfile_data->exception_probes.empty ())
3567 return false;
3568
3569 gdbarch = objfile->arch ();
3570
3571 for (probe *p : bp_objfile_data->exception_probes)
3572 {
3573 b = create_internal_breakpoint (gdbarch,
3574 p->get_relocated_address (objfile),
3575 bp_exception_master,
3576 &internal_breakpoint_ops);
3577 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3578 b->enable_state = bp_disabled;
3579 }
3580
3581 return true;
3582 }
3583
3584 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3585 _Unwind_DebugHook. Return true if a breakpoint was installed. */
3586
3587 static bool
3588 create_exception_master_breakpoint_hook (objfile *objfile)
3589 {
3590 const char *const func_name = "_Unwind_DebugHook";
3591 struct breakpoint *b;
3592 struct gdbarch *gdbarch;
3593 struct breakpoint_objfile_data *bp_objfile_data;
3594 CORE_ADDR addr;
3595 struct explicit_location explicit_loc;
3596
3597 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3598
3599 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3600 return false;
3601
3602 gdbarch = objfile->arch ();
3603
3604 if (bp_objfile_data->exception_msym.minsym == NULL)
3605 {
3606 struct bound_minimal_symbol debug_hook;
3607
3608 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3609 if (debug_hook.minsym == NULL)
3610 {
3611 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3612 return false;
3613 }
3614
3615 bp_objfile_data->exception_msym = debug_hook;
3616 }
3617
3618 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3619 addr = gdbarch_convert_from_func_ptr_addr
3620 (gdbarch, addr, current_inferior ()->top_target ());
3621 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3622 &internal_breakpoint_ops);
3623 initialize_explicit_location (&explicit_loc);
3624 explicit_loc.function_name = ASTRDUP (func_name);
3625 b->location = new_explicit_location (&explicit_loc);
3626 b->enable_state = bp_disabled;
3627
3628 return true;
3629 }
3630
3631 /* Install a master breakpoint on the unwinder's debug hook. */
3632
3633 static void
3634 create_exception_master_breakpoint (void)
3635 {
3636 for (objfile *obj : current_program_space->objfiles ())
3637 {
3638 /* Skip separate debug object. */
3639 if (obj->separate_debug_objfile_backlink)
3640 continue;
3641
3642 /* Try a probe kind breakpoint. */
3643 if (create_exception_master_breakpoint_probe (obj))
3644 continue;
3645
3646 /* Iterate over main and separate debug objects and try an
3647 _Unwind_DebugHook kind breakpoint. */
3648 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3649 if (create_exception_master_breakpoint_hook (debug_objfile))
3650 break;
3651 }
3652 }
3653
3654 /* Does B have a location spec? */
3655
3656 static int
3657 breakpoint_event_location_empty_p (const struct breakpoint *b)
3658 {
3659 return b->location != NULL && event_location_empty_p (b->location.get ());
3660 }
3661
3662 void
3663 update_breakpoints_after_exec (void)
3664 {
3665 struct bp_location *bploc, **bplocp_tmp;
3666
3667 /* We're about to delete breakpoints from GDB's lists. If the
3668 INSERTED flag is true, GDB will try to lift the breakpoints by
3669 writing the breakpoints' "shadow contents" back into memory. The
3670 "shadow contents" are NOT valid after an exec, so GDB should not
3671 do that. Instead, the target is responsible from marking
3672 breakpoints out as soon as it detects an exec. We don't do that
3673 here instead, because there may be other attempts to delete
3674 breakpoints after detecting an exec and before reaching here. */
3675 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3676 if (bploc->pspace == current_program_space)
3677 gdb_assert (!bploc->inserted);
3678
3679 for (breakpoint *b : all_breakpoints_safe ())
3680 {
3681 if (b->pspace != current_program_space)
3682 continue;
3683
3684 /* Solib breakpoints must be explicitly reset after an exec(). */
3685 if (b->type == bp_shlib_event)
3686 {
3687 delete_breakpoint (b);
3688 continue;
3689 }
3690
3691 /* JIT breakpoints must be explicitly reset after an exec(). */
3692 if (b->type == bp_jit_event)
3693 {
3694 delete_breakpoint (b);
3695 continue;
3696 }
3697
3698 /* Thread event breakpoints must be set anew after an exec(),
3699 as must overlay event and longjmp master breakpoints. */
3700 if (b->type == bp_thread_event || b->type == bp_overlay_event
3701 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3702 || b->type == bp_exception_master)
3703 {
3704 delete_breakpoint (b);
3705 continue;
3706 }
3707
3708 /* Step-resume breakpoints are meaningless after an exec(). */
3709 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3710 {
3711 delete_breakpoint (b);
3712 continue;
3713 }
3714
3715 /* Just like single-step breakpoints. */
3716 if (b->type == bp_single_step)
3717 {
3718 delete_breakpoint (b);
3719 continue;
3720 }
3721
3722 /* Longjmp and longjmp-resume breakpoints are also meaningless
3723 after an exec. */
3724 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3725 || b->type == bp_longjmp_call_dummy
3726 || b->type == bp_exception || b->type == bp_exception_resume)
3727 {
3728 delete_breakpoint (b);
3729 continue;
3730 }
3731
3732 if (b->type == bp_catchpoint)
3733 {
3734 /* For now, none of the bp_catchpoint breakpoints need to
3735 do anything at this point. In the future, if some of
3736 the catchpoints need to something, we will need to add
3737 a new method, and call this method from here. */
3738 continue;
3739 }
3740
3741 /* bp_finish is a special case. The only way we ought to be able
3742 to see one of these when an exec() has happened, is if the user
3743 caught a vfork, and then said "finish". Ordinarily a finish just
3744 carries them to the call-site of the current callee, by setting
3745 a temporary bp there and resuming. But in this case, the finish
3746 will carry them entirely through the vfork & exec.
3747
3748 We don't want to allow a bp_finish to remain inserted now. But
3749 we can't safely delete it, 'cause finish_command has a handle to
3750 the bp on a bpstat, and will later want to delete it. There's a
3751 chance (and I've seen it happen) that if we delete the bp_finish
3752 here, that its storage will get reused by the time finish_command
3753 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3754 We really must allow finish_command to delete a bp_finish.
3755
3756 In the absence of a general solution for the "how do we know
3757 it's safe to delete something others may have handles to?"
3758 problem, what we'll do here is just uninsert the bp_finish, and
3759 let finish_command delete it.
3760
3761 (We know the bp_finish is "doomed" in the sense that it's
3762 momentary, and will be deleted as soon as finish_command sees
3763 the inferior stopped. So it doesn't matter that the bp's
3764 address is probably bogus in the new a.out, unlike e.g., the
3765 solib breakpoints.) */
3766
3767 if (b->type == bp_finish)
3768 {
3769 continue;
3770 }
3771
3772 /* Without a symbolic address, we have little hope of the
3773 pre-exec() address meaning the same thing in the post-exec()
3774 a.out. */
3775 if (breakpoint_event_location_empty_p (b))
3776 {
3777 delete_breakpoint (b);
3778 continue;
3779 }
3780 }
3781 }
3782
3783 int
3784 detach_breakpoints (ptid_t ptid)
3785 {
3786 struct bp_location *bl, **blp_tmp;
3787 int val = 0;
3788 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3789 struct inferior *inf = current_inferior ();
3790
3791 if (ptid.pid () == inferior_ptid.pid ())
3792 error (_("Cannot detach breakpoints of inferior_ptid"));
3793
3794 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3795 inferior_ptid = ptid;
3796 ALL_BP_LOCATIONS (bl, blp_tmp)
3797 {
3798 if (bl->pspace != inf->pspace)
3799 continue;
3800
3801 /* This function must physically remove breakpoints locations
3802 from the specified ptid, without modifying the breakpoint
3803 package's state. Locations of type bp_loc_other are only
3804 maintained at GDB side. So, there is no need to remove
3805 these bp_loc_other locations. Moreover, removing these
3806 would modify the breakpoint package's state. */
3807 if (bl->loc_type == bp_loc_other)
3808 continue;
3809
3810 if (bl->inserted)
3811 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3812 }
3813
3814 return val;
3815 }
3816
3817 /* Remove the breakpoint location BL from the current address space.
3818 Note that this is used to detach breakpoints from a child fork.
3819 When we get here, the child isn't in the inferior list, and neither
3820 do we have objects to represent its address space --- we should
3821 *not* look at bl->pspace->aspace here. */
3822
3823 static int
3824 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3825 {
3826 int val;
3827
3828 /* BL is never in moribund_locations by our callers. */
3829 gdb_assert (bl->owner != NULL);
3830
3831 /* The type of none suggests that owner is actually deleted.
3832 This should not ever happen. */
3833 gdb_assert (bl->owner->type != bp_none);
3834
3835 if (bl->loc_type == bp_loc_software_breakpoint
3836 || bl->loc_type == bp_loc_hardware_breakpoint)
3837 {
3838 /* "Normal" instruction breakpoint: either the standard
3839 trap-instruction bp (bp_breakpoint), or a
3840 bp_hardware_breakpoint. */
3841
3842 /* First check to see if we have to handle an overlay. */
3843 if (overlay_debugging == ovly_off
3844 || bl->section == NULL
3845 || !(section_is_overlay (bl->section)))
3846 {
3847 /* No overlay handling: just remove the breakpoint. */
3848
3849 /* If we're trying to uninsert a memory breakpoint that we
3850 know is set in a dynamic object that is marked
3851 shlib_disabled, then either the dynamic object was
3852 removed with "remove-symbol-file" or with
3853 "nosharedlibrary". In the former case, we don't know
3854 whether another dynamic object might have loaded over the
3855 breakpoint's address -- the user might well let us know
3856 about it next with add-symbol-file (the whole point of
3857 add-symbol-file is letting the user manually maintain a
3858 list of dynamically loaded objects). If we have the
3859 breakpoint's shadow memory, that is, this is a software
3860 breakpoint managed by GDB, check whether the breakpoint
3861 is still inserted in memory, to avoid overwriting wrong
3862 code with stale saved shadow contents. Note that HW
3863 breakpoints don't have shadow memory, as they're
3864 implemented using a mechanism that is not dependent on
3865 being able to modify the target's memory, and as such
3866 they should always be removed. */
3867 if (bl->shlib_disabled
3868 && bl->target_info.shadow_len != 0
3869 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3870 val = 0;
3871 else
3872 val = bl->owner->ops->remove_location (bl, reason);
3873 }
3874 else
3875 {
3876 /* This breakpoint is in an overlay section.
3877 Did we set a breakpoint at the LMA? */
3878 if (!overlay_events_enabled)
3879 {
3880 /* Yes -- overlay event support is not active, so we
3881 should have set a breakpoint at the LMA. Remove it.
3882 */
3883 /* Ignore any failures: if the LMA is in ROM, we will
3884 have already warned when we failed to insert it. */
3885 if (bl->loc_type == bp_loc_hardware_breakpoint)
3886 target_remove_hw_breakpoint (bl->gdbarch,
3887 &bl->overlay_target_info);
3888 else
3889 target_remove_breakpoint (bl->gdbarch,
3890 &bl->overlay_target_info,
3891 reason);
3892 }
3893 /* Did we set a breakpoint at the VMA?
3894 If so, we will have marked the breakpoint 'inserted'. */
3895 if (bl->inserted)
3896 {
3897 /* Yes -- remove it. Previously we did not bother to
3898 remove the breakpoint if the section had been
3899 unmapped, but let's not rely on that being safe. We
3900 don't know what the overlay manager might do. */
3901
3902 /* However, we should remove *software* breakpoints only
3903 if the section is still mapped, or else we overwrite
3904 wrong code with the saved shadow contents. */
3905 if (bl->loc_type == bp_loc_hardware_breakpoint
3906 || section_is_mapped (bl->section))
3907 val = bl->owner->ops->remove_location (bl, reason);
3908 else
3909 val = 0;
3910 }
3911 else
3912 {
3913 /* No -- not inserted, so no need to remove. No error. */
3914 val = 0;
3915 }
3916 }
3917
3918 /* In some cases, we might not be able to remove a breakpoint in
3919 a shared library that has already been removed, but we have
3920 not yet processed the shlib unload event. Similarly for an
3921 unloaded add-symbol-file object - the user might not yet have
3922 had the chance to remove-symbol-file it. shlib_disabled will
3923 be set if the library/object has already been removed, but
3924 the breakpoint hasn't been uninserted yet, e.g., after
3925 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3926 always-inserted mode. */
3927 if (val
3928 && (bl->loc_type == bp_loc_software_breakpoint
3929 && (bl->shlib_disabled
3930 || solib_name_from_address (bl->pspace, bl->address)
3931 || shared_objfile_contains_address_p (bl->pspace,
3932 bl->address))))
3933 val = 0;
3934
3935 if (val)
3936 return val;
3937 bl->inserted = (reason == DETACH_BREAKPOINT);
3938 }
3939 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3940 {
3941 gdb_assert (bl->owner->ops != NULL
3942 && bl->owner->ops->remove_location != NULL);
3943
3944 bl->inserted = (reason == DETACH_BREAKPOINT);
3945 bl->owner->ops->remove_location (bl, reason);
3946
3947 /* Failure to remove any of the hardware watchpoints comes here. */
3948 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3949 warning (_("Could not remove hardware watchpoint %d."),
3950 bl->owner->number);
3951 }
3952 else if (bl->owner->type == bp_catchpoint
3953 && breakpoint_enabled (bl->owner)
3954 && !bl->duplicate)
3955 {
3956 gdb_assert (bl->owner->ops != NULL
3957 && bl->owner->ops->remove_location != NULL);
3958
3959 val = bl->owner->ops->remove_location (bl, reason);
3960 if (val)
3961 return val;
3962
3963 bl->inserted = (reason == DETACH_BREAKPOINT);
3964 }
3965
3966 return 0;
3967 }
3968
3969 static int
3970 remove_breakpoint (struct bp_location *bl)
3971 {
3972 /* BL is never in moribund_locations by our callers. */
3973 gdb_assert (bl->owner != NULL);
3974
3975 /* The type of none suggests that owner is actually deleted.
3976 This should not ever happen. */
3977 gdb_assert (bl->owner->type != bp_none);
3978
3979 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3980
3981 switch_to_program_space_and_thread (bl->pspace);
3982
3983 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
3984 }
3985
3986 /* Clear the "inserted" flag in all breakpoints. */
3987
3988 void
3989 mark_breakpoints_out (void)
3990 {
3991 struct bp_location *bl, **blp_tmp;
3992
3993 ALL_BP_LOCATIONS (bl, blp_tmp)
3994 if (bl->pspace == current_program_space)
3995 bl->inserted = 0;
3996 }
3997
3998 /* Clear the "inserted" flag in all breakpoints and delete any
3999 breakpoints which should go away between runs of the program.
4000
4001 Plus other such housekeeping that has to be done for breakpoints
4002 between runs.
4003
4004 Note: this function gets called at the end of a run (by
4005 generic_mourn_inferior) and when a run begins (by
4006 init_wait_for_inferior). */
4007
4008
4009
4010 void
4011 breakpoint_init_inferior (enum inf_context context)
4012 {
4013 struct program_space *pspace = current_program_space;
4014
4015 /* If breakpoint locations are shared across processes, then there's
4016 nothing to do. */
4017 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4018 return;
4019
4020 mark_breakpoints_out ();
4021
4022 for (breakpoint *b : all_breakpoints_safe ())
4023 {
4024 if (b->loc && b->loc->pspace != pspace)
4025 continue;
4026
4027 switch (b->type)
4028 {
4029 case bp_call_dummy:
4030 case bp_longjmp_call_dummy:
4031
4032 /* If the call dummy breakpoint is at the entry point it will
4033 cause problems when the inferior is rerun, so we better get
4034 rid of it. */
4035
4036 case bp_watchpoint_scope:
4037
4038 /* Also get rid of scope breakpoints. */
4039
4040 case bp_shlib_event:
4041
4042 /* Also remove solib event breakpoints. Their addresses may
4043 have changed since the last time we ran the program.
4044 Actually we may now be debugging against different target;
4045 and so the solib backend that installed this breakpoint may
4046 not be used in by the target. E.g.,
4047
4048 (gdb) file prog-linux
4049 (gdb) run # native linux target
4050 ...
4051 (gdb) kill
4052 (gdb) file prog-win.exe
4053 (gdb) tar rem :9999 # remote Windows gdbserver.
4054 */
4055
4056 case bp_step_resume:
4057
4058 /* Also remove step-resume breakpoints. */
4059
4060 case bp_single_step:
4061
4062 /* Also remove single-step breakpoints. */
4063
4064 delete_breakpoint (b);
4065 break;
4066
4067 case bp_watchpoint:
4068 case bp_hardware_watchpoint:
4069 case bp_read_watchpoint:
4070 case bp_access_watchpoint:
4071 {
4072 struct watchpoint *w = (struct watchpoint *) b;
4073
4074 /* Likewise for watchpoints on local expressions. */
4075 if (w->exp_valid_block != NULL)
4076 delete_breakpoint (b);
4077 else
4078 {
4079 /* Get rid of existing locations, which are no longer
4080 valid. New ones will be created in
4081 update_watchpoint, when the inferior is restarted.
4082 The next update_global_location_list call will
4083 garbage collect them. */
4084 b->loc = NULL;
4085
4086 if (context == inf_starting)
4087 {
4088 /* Reset val field to force reread of starting value in
4089 insert_breakpoints. */
4090 w->val.reset (nullptr);
4091 w->val_valid = false;
4092 }
4093 }
4094 }
4095 break;
4096 default:
4097 break;
4098 }
4099 }
4100
4101 /* Get rid of the moribund locations. */
4102 for (bp_location *bl : moribund_locations)
4103 decref_bp_location (&bl);
4104 moribund_locations.clear ();
4105 }
4106
4107 /* These functions concern about actual breakpoints inserted in the
4108 target --- to e.g. check if we need to do decr_pc adjustment or if
4109 we need to hop over the bkpt --- so we check for address space
4110 match, not program space. */
4111
4112 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4113 exists at PC. It returns ordinary_breakpoint_here if it's an
4114 ordinary breakpoint, or permanent_breakpoint_here if it's a
4115 permanent breakpoint.
4116 - When continuing from a location with an ordinary breakpoint, we
4117 actually single step once before calling insert_breakpoints.
4118 - When continuing from a location with a permanent breakpoint, we
4119 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4120 the target, to advance the PC past the breakpoint. */
4121
4122 enum breakpoint_here
4123 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4124 {
4125 struct bp_location *bl, **blp_tmp;
4126 int any_breakpoint_here = 0;
4127
4128 ALL_BP_LOCATIONS (bl, blp_tmp)
4129 {
4130 if (bl->loc_type != bp_loc_software_breakpoint
4131 && bl->loc_type != bp_loc_hardware_breakpoint)
4132 continue;
4133
4134 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4135 if ((breakpoint_enabled (bl->owner)
4136 || bl->permanent)
4137 && breakpoint_location_address_match (bl, aspace, pc))
4138 {
4139 if (overlay_debugging
4140 && section_is_overlay (bl->section)
4141 && !section_is_mapped (bl->section))
4142 continue; /* unmapped overlay -- can't be a match */
4143 else if (bl->permanent)
4144 return permanent_breakpoint_here;
4145 else
4146 any_breakpoint_here = 1;
4147 }
4148 }
4149
4150 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4151 }
4152
4153 /* See breakpoint.h. */
4154
4155 int
4156 breakpoint_in_range_p (const address_space *aspace,
4157 CORE_ADDR addr, ULONGEST len)
4158 {
4159 struct bp_location *bl, **blp_tmp;
4160
4161 ALL_BP_LOCATIONS (bl, blp_tmp)
4162 {
4163 if (bl->loc_type != bp_loc_software_breakpoint
4164 && bl->loc_type != bp_loc_hardware_breakpoint)
4165 continue;
4166
4167 if ((breakpoint_enabled (bl->owner)
4168 || bl->permanent)
4169 && breakpoint_location_address_range_overlap (bl, aspace,
4170 addr, len))
4171 {
4172 if (overlay_debugging
4173 && section_is_overlay (bl->section)
4174 && !section_is_mapped (bl->section))
4175 {
4176 /* Unmapped overlay -- can't be a match. */
4177 continue;
4178 }
4179
4180 return 1;
4181 }
4182 }
4183
4184 return 0;
4185 }
4186
4187 /* Return true if there's a moribund breakpoint at PC. */
4188
4189 int
4190 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4191 {
4192 for (bp_location *loc : moribund_locations)
4193 if (breakpoint_location_address_match (loc, aspace, pc))
4194 return 1;
4195
4196 return 0;
4197 }
4198
4199 /* Returns non-zero iff BL is inserted at PC, in address space
4200 ASPACE. */
4201
4202 static int
4203 bp_location_inserted_here_p (struct bp_location *bl,
4204 const address_space *aspace, CORE_ADDR pc)
4205 {
4206 if (bl->inserted
4207 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4208 aspace, pc))
4209 {
4210 if (overlay_debugging
4211 && section_is_overlay (bl->section)
4212 && !section_is_mapped (bl->section))
4213 return 0; /* unmapped overlay -- can't be a match */
4214 else
4215 return 1;
4216 }
4217 return 0;
4218 }
4219
4220 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4221
4222 int
4223 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4224 {
4225 struct bp_location **blp, **blp_tmp = NULL;
4226
4227 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4228 {
4229 struct bp_location *bl = *blp;
4230
4231 if (bl->loc_type != bp_loc_software_breakpoint
4232 && bl->loc_type != bp_loc_hardware_breakpoint)
4233 continue;
4234
4235 if (bp_location_inserted_here_p (bl, aspace, pc))
4236 return 1;
4237 }
4238 return 0;
4239 }
4240
4241 /* This function returns non-zero iff there is a software breakpoint
4242 inserted at PC. */
4243
4244 int
4245 software_breakpoint_inserted_here_p (const address_space *aspace,
4246 CORE_ADDR pc)
4247 {
4248 struct bp_location **blp, **blp_tmp = NULL;
4249
4250 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4251 {
4252 struct bp_location *bl = *blp;
4253
4254 if (bl->loc_type != bp_loc_software_breakpoint)
4255 continue;
4256
4257 if (bp_location_inserted_here_p (bl, aspace, pc))
4258 return 1;
4259 }
4260
4261 return 0;
4262 }
4263
4264 /* See breakpoint.h. */
4265
4266 int
4267 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4268 CORE_ADDR pc)
4269 {
4270 struct bp_location **blp, **blp_tmp = NULL;
4271
4272 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4273 {
4274 struct bp_location *bl = *blp;
4275
4276 if (bl->loc_type != bp_loc_hardware_breakpoint)
4277 continue;
4278
4279 if (bp_location_inserted_here_p (bl, aspace, pc))
4280 return 1;
4281 }
4282
4283 return 0;
4284 }
4285
4286 int
4287 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4288 CORE_ADDR addr, ULONGEST len)
4289 {
4290 for (breakpoint *bpt : all_breakpoints ())
4291 {
4292 struct bp_location *loc;
4293
4294 if (bpt->type != bp_hardware_watchpoint
4295 && bpt->type != bp_access_watchpoint)
4296 continue;
4297
4298 if (!breakpoint_enabled (bpt))
4299 continue;
4300
4301 for (loc = bpt->loc; loc; loc = loc->next)
4302 if (loc->pspace->aspace == aspace && loc->inserted)
4303 {
4304 CORE_ADDR l, h;
4305
4306 /* Check for intersection. */
4307 l = std::max<CORE_ADDR> (loc->address, addr);
4308 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4309 if (l < h)
4310 return 1;
4311 }
4312 }
4313 return 0;
4314 }
4315
4316 /* See breakpoint.h. */
4317
4318 bool
4319 is_catchpoint (struct breakpoint *b)
4320 {
4321 return (b->type == bp_catchpoint);
4322 }
4323
4324 /* Clear a bpstat so that it says we are not at any breakpoint.
4325 Also free any storage that is part of a bpstat. */
4326
4327 void
4328 bpstat_clear (bpstat *bsp)
4329 {
4330 bpstat p;
4331 bpstat q;
4332
4333 if (bsp == 0)
4334 return;
4335 p = *bsp;
4336 while (p != NULL)
4337 {
4338 q = p->next;
4339 delete p;
4340 p = q;
4341 }
4342 *bsp = NULL;
4343 }
4344
4345 bpstats::bpstats (const bpstats &other)
4346 : next (NULL),
4347 bp_location_at (other.bp_location_at),
4348 breakpoint_at (other.breakpoint_at),
4349 commands (other.commands),
4350 print (other.print),
4351 stop (other.stop),
4352 print_it (other.print_it)
4353 {
4354 if (other.old_val != NULL)
4355 old_val = release_value (value_copy (other.old_val.get ()));
4356 }
4357
4358 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4359 is part of the bpstat is copied as well. */
4360
4361 bpstat
4362 bpstat_copy (bpstat bs)
4363 {
4364 bpstat p = NULL;
4365 bpstat tmp;
4366 bpstat retval = NULL;
4367
4368 if (bs == NULL)
4369 return bs;
4370
4371 for (; bs != NULL; bs = bs->next)
4372 {
4373 tmp = new bpstats (*bs);
4374
4375 if (p == NULL)
4376 /* This is the first thing in the chain. */
4377 retval = tmp;
4378 else
4379 p->next = tmp;
4380 p = tmp;
4381 }
4382 p->next = NULL;
4383 return retval;
4384 }
4385
4386 /* Find the bpstat associated with this breakpoint. */
4387
4388 bpstat
4389 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4390 {
4391 if (bsp == NULL)
4392 return NULL;
4393
4394 for (; bsp != NULL; bsp = bsp->next)
4395 {
4396 if (bsp->breakpoint_at == breakpoint)
4397 return bsp;
4398 }
4399 return NULL;
4400 }
4401
4402 /* See breakpoint.h. */
4403
4404 bool
4405 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4406 {
4407 for (; bsp != NULL; bsp = bsp->next)
4408 {
4409 if (bsp->breakpoint_at == NULL)
4410 {
4411 /* A moribund location can never explain a signal other than
4412 GDB_SIGNAL_TRAP. */
4413 if (sig == GDB_SIGNAL_TRAP)
4414 return true;
4415 }
4416 else
4417 {
4418 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4419 sig))
4420 return true;
4421 }
4422 }
4423
4424 return false;
4425 }
4426
4427 /* Put in *NUM the breakpoint number of the first breakpoint we are
4428 stopped at. *BSP upon return is a bpstat which points to the
4429 remaining breakpoints stopped at (but which is not guaranteed to be
4430 good for anything but further calls to bpstat_num).
4431
4432 Return 0 if passed a bpstat which does not indicate any breakpoints.
4433 Return -1 if stopped at a breakpoint that has been deleted since
4434 we set it.
4435 Return 1 otherwise. */
4436
4437 int
4438 bpstat_num (bpstat *bsp, int *num)
4439 {
4440 struct breakpoint *b;
4441
4442 if ((*bsp) == NULL)
4443 return 0; /* No more breakpoint values */
4444
4445 /* We assume we'll never have several bpstats that correspond to a
4446 single breakpoint -- otherwise, this function might return the
4447 same number more than once and this will look ugly. */
4448 b = (*bsp)->breakpoint_at;
4449 *bsp = (*bsp)->next;
4450 if (b == NULL)
4451 return -1; /* breakpoint that's been deleted since */
4452
4453 *num = b->number; /* We have its number */
4454 return 1;
4455 }
4456
4457 /* See breakpoint.h. */
4458
4459 void
4460 bpstat_clear_actions (void)
4461 {
4462 bpstat bs;
4463
4464 if (inferior_ptid == null_ptid)
4465 return;
4466
4467 thread_info *tp = inferior_thread ();
4468 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4469 {
4470 bs->commands = NULL;
4471 bs->old_val.reset (nullptr);
4472 }
4473 }
4474
4475 /* Called when a command is about to proceed the inferior. */
4476
4477 static void
4478 breakpoint_about_to_proceed (void)
4479 {
4480 if (inferior_ptid != null_ptid)
4481 {
4482 struct thread_info *tp = inferior_thread ();
4483
4484 /* Allow inferior function calls in breakpoint commands to not
4485 interrupt the command list. When the call finishes
4486 successfully, the inferior will be standing at the same
4487 breakpoint as if nothing happened. */
4488 if (tp->control.in_infcall)
4489 return;
4490 }
4491
4492 breakpoint_proceeded = 1;
4493 }
4494
4495 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4496 or its equivalent. */
4497
4498 static int
4499 command_line_is_silent (struct command_line *cmd)
4500 {
4501 return cmd && (strcmp ("silent", cmd->line) == 0);
4502 }
4503
4504 /* Execute all the commands associated with all the breakpoints at
4505 this location. Any of these commands could cause the process to
4506 proceed beyond this point, etc. We look out for such changes by
4507 checking the global "breakpoint_proceeded" after each command.
4508
4509 Returns true if a breakpoint command resumed the inferior. In that
4510 case, it is the caller's responsibility to recall it again with the
4511 bpstat of the current thread. */
4512
4513 static int
4514 bpstat_do_actions_1 (bpstat *bsp)
4515 {
4516 bpstat bs;
4517 int again = 0;
4518
4519 /* Avoid endless recursion if a `source' command is contained
4520 in bs->commands. */
4521 if (executing_breakpoint_commands)
4522 return 0;
4523
4524 scoped_restore save_executing
4525 = make_scoped_restore (&executing_breakpoint_commands, 1);
4526
4527 scoped_restore preventer = prevent_dont_repeat ();
4528
4529 /* This pointer will iterate over the list of bpstat's. */
4530 bs = *bsp;
4531
4532 breakpoint_proceeded = 0;
4533 for (; bs != NULL; bs = bs->next)
4534 {
4535 struct command_line *cmd = NULL;
4536
4537 /* Take ownership of the BSP's command tree, if it has one.
4538
4539 The command tree could legitimately contain commands like
4540 'step' and 'next', which call clear_proceed_status, which
4541 frees stop_bpstat's command tree. To make sure this doesn't
4542 free the tree we're executing out from under us, we need to
4543 take ownership of the tree ourselves. Since a given bpstat's
4544 commands are only executed once, we don't need to copy it; we
4545 can clear the pointer in the bpstat, and make sure we free
4546 the tree when we're done. */
4547 counted_command_line ccmd = bs->commands;
4548 bs->commands = NULL;
4549 if (ccmd != NULL)
4550 cmd = ccmd.get ();
4551 if (command_line_is_silent (cmd))
4552 {
4553 /* The action has been already done by bpstat_stop_status. */
4554 cmd = cmd->next;
4555 }
4556
4557 while (cmd != NULL)
4558 {
4559 execute_control_command (cmd);
4560
4561 if (breakpoint_proceeded)
4562 break;
4563 else
4564 cmd = cmd->next;
4565 }
4566
4567 if (breakpoint_proceeded)
4568 {
4569 if (current_ui->async)
4570 /* If we are in async mode, then the target might be still
4571 running, not stopped at any breakpoint, so nothing for
4572 us to do here -- just return to the event loop. */
4573 ;
4574 else
4575 /* In sync mode, when execute_control_command returns
4576 we're already standing on the next breakpoint.
4577 Breakpoint commands for that stop were not run, since
4578 execute_command does not run breakpoint commands --
4579 only command_line_handler does, but that one is not
4580 involved in execution of breakpoint commands. So, we
4581 can now execute breakpoint commands. It should be
4582 noted that making execute_command do bpstat actions is
4583 not an option -- in this case we'll have recursive
4584 invocation of bpstat for each breakpoint with a
4585 command, and can easily blow up GDB stack. Instead, we
4586 return true, which will trigger the caller to recall us
4587 with the new stop_bpstat. */
4588 again = 1;
4589 break;
4590 }
4591 }
4592 return again;
4593 }
4594
4595 /* Helper for bpstat_do_actions. Get the current thread, if there's
4596 one, is alive and has execution. Return NULL otherwise. */
4597
4598 static thread_info *
4599 get_bpstat_thread ()
4600 {
4601 if (inferior_ptid == null_ptid || !target_has_execution ())
4602 return NULL;
4603
4604 thread_info *tp = inferior_thread ();
4605 if (tp->state == THREAD_EXITED || tp->executing)
4606 return NULL;
4607 return tp;
4608 }
4609
4610 void
4611 bpstat_do_actions (void)
4612 {
4613 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4614 thread_info *tp;
4615
4616 /* Do any commands attached to breakpoint we are stopped at. */
4617 while ((tp = get_bpstat_thread ()) != NULL)
4618 {
4619 /* Since in sync mode, bpstat_do_actions may resume the
4620 inferior, and only return when it is stopped at the next
4621 breakpoint, we keep doing breakpoint actions until it returns
4622 false to indicate the inferior was not resumed. */
4623 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4624 break;
4625 }
4626
4627 cleanup_if_error.release ();
4628 }
4629
4630 /* Print out the (old or new) value associated with a watchpoint. */
4631
4632 static void
4633 watchpoint_value_print (struct value *val, struct ui_file *stream)
4634 {
4635 if (val == NULL)
4636 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4637 else
4638 {
4639 struct value_print_options opts;
4640 get_user_print_options (&opts);
4641 value_print (val, stream, &opts);
4642 }
4643 }
4644
4645 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4646 debugging multiple threads. */
4647
4648 void
4649 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4650 {
4651 if (uiout->is_mi_like_p ())
4652 return;
4653
4654 uiout->text ("\n");
4655
4656 if (show_thread_that_caused_stop ())
4657 {
4658 const char *name;
4659 struct thread_info *thr = inferior_thread ();
4660
4661 uiout->text ("Thread ");
4662 uiout->field_string ("thread-id", print_thread_id (thr));
4663
4664 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4665 if (name != NULL)
4666 {
4667 uiout->text (" \"");
4668 uiout->field_string ("name", name);
4669 uiout->text ("\"");
4670 }
4671
4672 uiout->text (" hit ");
4673 }
4674 }
4675
4676 /* Generic routine for printing messages indicating why we
4677 stopped. The behavior of this function depends on the value
4678 'print_it' in the bpstat structure. Under some circumstances we
4679 may decide not to print anything here and delegate the task to
4680 normal_stop(). */
4681
4682 static enum print_stop_action
4683 print_bp_stop_message (bpstat bs)
4684 {
4685 switch (bs->print_it)
4686 {
4687 case print_it_noop:
4688 /* Nothing should be printed for this bpstat entry. */
4689 return PRINT_UNKNOWN;
4690 break;
4691
4692 case print_it_done:
4693 /* We still want to print the frame, but we already printed the
4694 relevant messages. */
4695 return PRINT_SRC_AND_LOC;
4696 break;
4697
4698 case print_it_normal:
4699 {
4700 struct breakpoint *b = bs->breakpoint_at;
4701
4702 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4703 which has since been deleted. */
4704 if (b == NULL)
4705 return PRINT_UNKNOWN;
4706
4707 /* Normal case. Call the breakpoint's print_it method. */
4708 return b->ops->print_it (bs);
4709 }
4710 break;
4711
4712 default:
4713 internal_error (__FILE__, __LINE__,
4714 _("print_bp_stop_message: unrecognized enum value"));
4715 break;
4716 }
4717 }
4718
4719 /* A helper function that prints a shared library stopped event. */
4720
4721 static void
4722 print_solib_event (int is_catchpoint)
4723 {
4724 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4725 bool any_added = !current_program_space->added_solibs.empty ();
4726
4727 if (!is_catchpoint)
4728 {
4729 if (any_added || any_deleted)
4730 current_uiout->text (_("Stopped due to shared library event:\n"));
4731 else
4732 current_uiout->text (_("Stopped due to shared library event (no "
4733 "libraries added or removed)\n"));
4734 }
4735
4736 if (current_uiout->is_mi_like_p ())
4737 current_uiout->field_string ("reason",
4738 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4739
4740 if (any_deleted)
4741 {
4742 current_uiout->text (_(" Inferior unloaded "));
4743 ui_out_emit_list list_emitter (current_uiout, "removed");
4744 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4745 {
4746 const std::string &name = current_program_space->deleted_solibs[ix];
4747
4748 if (ix > 0)
4749 current_uiout->text (" ");
4750 current_uiout->field_string ("library", name);
4751 current_uiout->text ("\n");
4752 }
4753 }
4754
4755 if (any_added)
4756 {
4757 current_uiout->text (_(" Inferior loaded "));
4758 ui_out_emit_list list_emitter (current_uiout, "added");
4759 bool first = true;
4760 for (so_list *iter : current_program_space->added_solibs)
4761 {
4762 if (!first)
4763 current_uiout->text (" ");
4764 first = false;
4765 current_uiout->field_string ("library", iter->so_name);
4766 current_uiout->text ("\n");
4767 }
4768 }
4769 }
4770
4771 /* Print a message indicating what happened. This is called from
4772 normal_stop(). The input to this routine is the head of the bpstat
4773 list - a list of the eventpoints that caused this stop. KIND is
4774 the target_waitkind for the stopping event. This
4775 routine calls the generic print routine for printing a message
4776 about reasons for stopping. This will print (for example) the
4777 "Breakpoint n," part of the output. The return value of this
4778 routine is one of:
4779
4780 PRINT_UNKNOWN: Means we printed nothing.
4781 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4782 code to print the location. An example is
4783 "Breakpoint 1, " which should be followed by
4784 the location.
4785 PRINT_SRC_ONLY: Means we printed something, but there is no need
4786 to also print the location part of the message.
4787 An example is the catch/throw messages, which
4788 don't require a location appended to the end.
4789 PRINT_NOTHING: We have done some printing and we don't need any
4790 further info to be printed. */
4791
4792 enum print_stop_action
4793 bpstat_print (bpstat bs, int kind)
4794 {
4795 enum print_stop_action val;
4796
4797 /* Maybe another breakpoint in the chain caused us to stop.
4798 (Currently all watchpoints go on the bpstat whether hit or not.
4799 That probably could (should) be changed, provided care is taken
4800 with respect to bpstat_explains_signal). */
4801 for (; bs; bs = bs->next)
4802 {
4803 val = print_bp_stop_message (bs);
4804 if (val == PRINT_SRC_ONLY
4805 || val == PRINT_SRC_AND_LOC
4806 || val == PRINT_NOTHING)
4807 return val;
4808 }
4809
4810 /* If we had hit a shared library event breakpoint,
4811 print_bp_stop_message would print out this message. If we hit an
4812 OS-level shared library event, do the same thing. */
4813 if (kind == TARGET_WAITKIND_LOADED)
4814 {
4815 print_solib_event (0);
4816 return PRINT_NOTHING;
4817 }
4818
4819 /* We reached the end of the chain, or we got a null BS to start
4820 with and nothing was printed. */
4821 return PRINT_UNKNOWN;
4822 }
4823
4824 /* Evaluate the boolean expression EXP and return the result. */
4825
4826 static bool
4827 breakpoint_cond_eval (expression *exp)
4828 {
4829 struct value *mark = value_mark ();
4830 bool res = value_true (evaluate_expression (exp));
4831
4832 value_free_to_mark (mark);
4833 return res;
4834 }
4835
4836 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4837
4838 bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4839 : next (NULL),
4840 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
4841 breakpoint_at (bl->owner),
4842 commands (NULL),
4843 print (0),
4844 stop (0),
4845 print_it (print_it_normal)
4846 {
4847 **bs_link_pointer = this;
4848 *bs_link_pointer = &next;
4849 }
4850
4851 bpstats::bpstats ()
4852 : next (NULL),
4853 breakpoint_at (NULL),
4854 commands (NULL),
4855 print (0),
4856 stop (0),
4857 print_it (print_it_normal)
4858 {
4859 }
4860 \f
4861 /* The target has stopped with waitstatus WS. Check if any hardware
4862 watchpoints have triggered, according to the target. */
4863
4864 int
4865 watchpoints_triggered (struct target_waitstatus *ws)
4866 {
4867 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
4868 CORE_ADDR addr;
4869
4870 if (!stopped_by_watchpoint)
4871 {
4872 /* We were not stopped by a watchpoint. Mark all watchpoints
4873 as not triggered. */
4874 for (breakpoint *b : all_breakpoints ())
4875 if (is_hardware_watchpoint (b))
4876 {
4877 struct watchpoint *w = (struct watchpoint *) b;
4878
4879 w->watchpoint_triggered = watch_triggered_no;
4880 }
4881
4882 return 0;
4883 }
4884
4885 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
4886 {
4887 /* We were stopped by a watchpoint, but we don't know where.
4888 Mark all watchpoints as unknown. */
4889 for (breakpoint *b : all_breakpoints ())
4890 if (is_hardware_watchpoint (b))
4891 {
4892 struct watchpoint *w = (struct watchpoint *) b;
4893
4894 w->watchpoint_triggered = watch_triggered_unknown;
4895 }
4896
4897 return 1;
4898 }
4899
4900 /* The target could report the data address. Mark watchpoints
4901 affected by this data address as triggered, and all others as not
4902 triggered. */
4903
4904 for (breakpoint *b : all_breakpoints ())
4905 if (is_hardware_watchpoint (b))
4906 {
4907 struct watchpoint *w = (struct watchpoint *) b;
4908 struct bp_location *loc;
4909
4910 w->watchpoint_triggered = watch_triggered_no;
4911 for (loc = b->loc; loc; loc = loc->next)
4912 {
4913 if (is_masked_watchpoint (b))
4914 {
4915 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4916 CORE_ADDR start = loc->address & w->hw_wp_mask;
4917
4918 if (newaddr == start)
4919 {
4920 w->watchpoint_triggered = watch_triggered_yes;
4921 break;
4922 }
4923 }
4924 /* Exact match not required. Within range is sufficient. */
4925 else if (target_watchpoint_addr_within_range
4926 (current_inferior ()->top_target (), addr, loc->address,
4927 loc->length))
4928 {
4929 w->watchpoint_triggered = watch_triggered_yes;
4930 break;
4931 }
4932 }
4933 }
4934
4935 return 1;
4936 }
4937
4938 /* Possible return values for watchpoint_check. */
4939 enum wp_check_result
4940 {
4941 /* The watchpoint has been deleted. */
4942 WP_DELETED = 1,
4943
4944 /* The value has changed. */
4945 WP_VALUE_CHANGED = 2,
4946
4947 /* The value has not changed. */
4948 WP_VALUE_NOT_CHANGED = 3,
4949
4950 /* Ignore this watchpoint, no matter if the value changed or not. */
4951 WP_IGNORE = 4,
4952 };
4953
4954 #define BP_TEMPFLAG 1
4955 #define BP_HARDWAREFLAG 2
4956
4957 /* Evaluate watchpoint condition expression and check if its value
4958 changed. */
4959
4960 static wp_check_result
4961 watchpoint_check (bpstat bs)
4962 {
4963 struct watchpoint *b;
4964 struct frame_info *fr;
4965 int within_current_scope;
4966
4967 /* BS is built from an existing struct breakpoint. */
4968 gdb_assert (bs->breakpoint_at != NULL);
4969 b = (struct watchpoint *) bs->breakpoint_at;
4970
4971 /* If this is a local watchpoint, we only want to check if the
4972 watchpoint frame is in scope if the current thread is the thread
4973 that was used to create the watchpoint. */
4974 if (!watchpoint_in_thread_scope (b))
4975 return WP_IGNORE;
4976
4977 if (b->exp_valid_block == NULL)
4978 within_current_scope = 1;
4979 else
4980 {
4981 struct frame_info *frame = get_current_frame ();
4982 struct gdbarch *frame_arch = get_frame_arch (frame);
4983 CORE_ADDR frame_pc = get_frame_pc (frame);
4984
4985 /* stack_frame_destroyed_p() returns a non-zero value if we're
4986 still in the function but the stack frame has already been
4987 invalidated. Since we can't rely on the values of local
4988 variables after the stack has been destroyed, we are treating
4989 the watchpoint in that state as `not changed' without further
4990 checking. Don't mark watchpoints as changed if the current
4991 frame is in an epilogue - even if they are in some other
4992 frame, our view of the stack is likely to be wrong and
4993 frame_find_by_id could error out. */
4994 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
4995 return WP_IGNORE;
4996
4997 fr = frame_find_by_id (b->watchpoint_frame);
4998 within_current_scope = (fr != NULL);
4999
5000 /* If we've gotten confused in the unwinder, we might have
5001 returned a frame that can't describe this variable. */
5002 if (within_current_scope)
5003 {
5004 struct symbol *function;
5005
5006 function = get_frame_function (fr);
5007 if (function == NULL
5008 || !contained_in (b->exp_valid_block,
5009 SYMBOL_BLOCK_VALUE (function)))
5010 within_current_scope = 0;
5011 }
5012
5013 if (within_current_scope)
5014 /* If we end up stopping, the current frame will get selected
5015 in normal_stop. So this call to select_frame won't affect
5016 the user. */
5017 select_frame (fr);
5018 }
5019
5020 if (within_current_scope)
5021 {
5022 /* We use value_{,free_to_}mark because it could be a *long*
5023 time before we return to the command level and call
5024 free_all_values. We can't call free_all_values because we
5025 might be in the middle of evaluating a function call. */
5026
5027 struct value *mark;
5028 struct value *new_val;
5029
5030 if (is_masked_watchpoint (b))
5031 /* Since we don't know the exact trigger address (from
5032 stopped_data_address), just tell the user we've triggered
5033 a mask watchpoint. */
5034 return WP_VALUE_CHANGED;
5035
5036 mark = value_mark ();
5037 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
5038 NULL, NULL, false);
5039
5040 if (b->val_bitsize != 0)
5041 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5042
5043 /* We use value_equal_contents instead of value_equal because
5044 the latter coerces an array to a pointer, thus comparing just
5045 the address of the array instead of its contents. This is
5046 not what we want. */
5047 if ((b->val != NULL) != (new_val != NULL)
5048 || (b->val != NULL && !value_equal_contents (b->val.get (),
5049 new_val)))
5050 {
5051 bs->old_val = b->val;
5052 b->val = release_value (new_val);
5053 b->val_valid = true;
5054 if (new_val != NULL)
5055 value_free_to_mark (mark);
5056 return WP_VALUE_CHANGED;
5057 }
5058 else
5059 {
5060 /* Nothing changed. */
5061 value_free_to_mark (mark);
5062 return WP_VALUE_NOT_CHANGED;
5063 }
5064 }
5065 else
5066 {
5067 /* This seems like the only logical thing to do because
5068 if we temporarily ignored the watchpoint, then when
5069 we reenter the block in which it is valid it contains
5070 garbage (in the case of a function, it may have two
5071 garbage values, one before and one after the prologue).
5072 So we can't even detect the first assignment to it and
5073 watch after that (since the garbage may or may not equal
5074 the first value assigned). */
5075 /* We print all the stop information in
5076 breakpoint_ops->print_it, but in this case, by the time we
5077 call breakpoint_ops->print_it this bp will be deleted
5078 already. So we have no choice but print the information
5079 here. */
5080
5081 SWITCH_THRU_ALL_UIS ()
5082 {
5083 struct ui_out *uiout = current_uiout;
5084
5085 if (uiout->is_mi_like_p ())
5086 uiout->field_string
5087 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5088 uiout->message ("\nWatchpoint %pF deleted because the program has "
5089 "left the block in\n"
5090 "which its expression is valid.\n",
5091 signed_field ("wpnum", b->number));
5092 }
5093
5094 /* Make sure the watchpoint's commands aren't executed. */
5095 b->commands = NULL;
5096 watchpoint_del_at_next_stop (b);
5097
5098 return WP_DELETED;
5099 }
5100 }
5101
5102 /* Return true if it looks like target has stopped due to hitting
5103 breakpoint location BL. This function does not check if we should
5104 stop, only if BL explains the stop. */
5105
5106 static int
5107 bpstat_check_location (const struct bp_location *bl,
5108 const address_space *aspace, CORE_ADDR bp_addr,
5109 const struct target_waitstatus *ws)
5110 {
5111 struct breakpoint *b = bl->owner;
5112
5113 /* BL is from an existing breakpoint. */
5114 gdb_assert (b != NULL);
5115
5116 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5117 }
5118
5119 /* Determine if the watched values have actually changed, and we
5120 should stop. If not, set BS->stop to 0. */
5121
5122 static void
5123 bpstat_check_watchpoint (bpstat bs)
5124 {
5125 const struct bp_location *bl;
5126 struct watchpoint *b;
5127
5128 /* BS is built for existing struct breakpoint. */
5129 bl = bs->bp_location_at.get ();
5130 gdb_assert (bl != NULL);
5131 b = (struct watchpoint *) bs->breakpoint_at;
5132 gdb_assert (b != NULL);
5133
5134 {
5135 int must_check_value = 0;
5136
5137 if (b->type == bp_watchpoint)
5138 /* For a software watchpoint, we must always check the
5139 watched value. */
5140 must_check_value = 1;
5141 else if (b->watchpoint_triggered == watch_triggered_yes)
5142 /* We have a hardware watchpoint (read, write, or access)
5143 and the target earlier reported an address watched by
5144 this watchpoint. */
5145 must_check_value = 1;
5146 else if (b->watchpoint_triggered == watch_triggered_unknown
5147 && b->type == bp_hardware_watchpoint)
5148 /* We were stopped by a hardware watchpoint, but the target could
5149 not report the data address. We must check the watchpoint's
5150 value. Access and read watchpoints are out of luck; without
5151 a data address, we can't figure it out. */
5152 must_check_value = 1;
5153
5154 if (must_check_value)
5155 {
5156 wp_check_result e;
5157
5158 try
5159 {
5160 e = watchpoint_check (bs);
5161 }
5162 catch (const gdb_exception &ex)
5163 {
5164 exception_fprintf (gdb_stderr, ex,
5165 "Error evaluating expression "
5166 "for watchpoint %d\n",
5167 b->number);
5168
5169 SWITCH_THRU_ALL_UIS ()
5170 {
5171 printf_filtered (_("Watchpoint %d deleted.\n"),
5172 b->number);
5173 }
5174 watchpoint_del_at_next_stop (b);
5175 e = WP_DELETED;
5176 }
5177
5178 switch (e)
5179 {
5180 case WP_DELETED:
5181 /* We've already printed what needs to be printed. */
5182 bs->print_it = print_it_done;
5183 /* Stop. */
5184 break;
5185 case WP_IGNORE:
5186 bs->print_it = print_it_noop;
5187 bs->stop = 0;
5188 break;
5189 case WP_VALUE_CHANGED:
5190 if (b->type == bp_read_watchpoint)
5191 {
5192 /* There are two cases to consider here:
5193
5194 1. We're watching the triggered memory for reads.
5195 In that case, trust the target, and always report
5196 the watchpoint hit to the user. Even though
5197 reads don't cause value changes, the value may
5198 have changed since the last time it was read, and
5199 since we're not trapping writes, we will not see
5200 those, and as such we should ignore our notion of
5201 old value.
5202
5203 2. We're watching the triggered memory for both
5204 reads and writes. There are two ways this may
5205 happen:
5206
5207 2.1. This is a target that can't break on data
5208 reads only, but can break on accesses (reads or
5209 writes), such as e.g., x86. We detect this case
5210 at the time we try to insert read watchpoints.
5211
5212 2.2. Otherwise, the target supports read
5213 watchpoints, but, the user set an access or write
5214 watchpoint watching the same memory as this read
5215 watchpoint.
5216
5217 If we're watching memory writes as well as reads,
5218 ignore watchpoint hits when we find that the
5219 value hasn't changed, as reads don't cause
5220 changes. This still gives false positives when
5221 the program writes the same value to memory as
5222 what there was already in memory (we will confuse
5223 it for a read), but it's much better than
5224 nothing. */
5225
5226 int other_write_watchpoint = 0;
5227
5228 if (bl->watchpoint_type == hw_read)
5229 {
5230 for (breakpoint *other_b : all_breakpoints ())
5231 if (other_b->type == bp_hardware_watchpoint
5232 || other_b->type == bp_access_watchpoint)
5233 {
5234 struct watchpoint *other_w =
5235 (struct watchpoint *) other_b;
5236
5237 if (other_w->watchpoint_triggered
5238 == watch_triggered_yes)
5239 {
5240 other_write_watchpoint = 1;
5241 break;
5242 }
5243 }
5244 }
5245
5246 if (other_write_watchpoint
5247 || bl->watchpoint_type == hw_access)
5248 {
5249 /* We're watching the same memory for writes,
5250 and the value changed since the last time we
5251 updated it, so this trap must be for a write.
5252 Ignore it. */
5253 bs->print_it = print_it_noop;
5254 bs->stop = 0;
5255 }
5256 }
5257 break;
5258 case WP_VALUE_NOT_CHANGED:
5259 if (b->type == bp_hardware_watchpoint
5260 || b->type == bp_watchpoint)
5261 {
5262 /* Don't stop: write watchpoints shouldn't fire if
5263 the value hasn't changed. */
5264 bs->print_it = print_it_noop;
5265 bs->stop = 0;
5266 }
5267 /* Stop. */
5268 break;
5269 default:
5270 /* Can't happen. */
5271 break;
5272 }
5273 }
5274 else /* must_check_value == 0 */
5275 {
5276 /* This is a case where some watchpoint(s) triggered, but
5277 not at the address of this watchpoint, or else no
5278 watchpoint triggered after all. So don't print
5279 anything for this watchpoint. */
5280 bs->print_it = print_it_noop;
5281 bs->stop = 0;
5282 }
5283 }
5284 }
5285
5286 /* For breakpoints that are currently marked as telling gdb to stop,
5287 check conditions (condition proper, frame, thread and ignore count)
5288 of breakpoint referred to by BS. If we should not stop for this
5289 breakpoint, set BS->stop to 0. */
5290
5291 static void
5292 bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
5293 {
5294 const struct bp_location *bl;
5295 struct breakpoint *b;
5296 /* Assume stop. */
5297 bool condition_result = true;
5298 struct expression *cond;
5299
5300 gdb_assert (bs->stop);
5301
5302 /* BS is built for existing struct breakpoint. */
5303 bl = bs->bp_location_at.get ();
5304 gdb_assert (bl != NULL);
5305 b = bs->breakpoint_at;
5306 gdb_assert (b != NULL);
5307
5308 /* Even if the target evaluated the condition on its end and notified GDB, we
5309 need to do so again since GDB does not know if we stopped due to a
5310 breakpoint or a single step breakpoint. */
5311
5312 if (frame_id_p (b->frame_id)
5313 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5314 {
5315 bs->stop = 0;
5316 return;
5317 }
5318
5319 /* If this is a thread/task-specific breakpoint, don't waste cpu
5320 evaluating the condition if this isn't the specified
5321 thread/task. */
5322 if ((b->thread != -1 && b->thread != thread->global_num)
5323 || (b->task != 0 && b->task != ada_get_task_number (thread)))
5324 {
5325 bs->stop = 0;
5326 return;
5327 }
5328
5329 /* Evaluate extension language breakpoints that have a "stop" method
5330 implemented. */
5331 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5332
5333 if (is_watchpoint (b))
5334 {
5335 struct watchpoint *w = (struct watchpoint *) b;
5336
5337 cond = w->cond_exp.get ();
5338 }
5339 else
5340 cond = bl->cond.get ();
5341
5342 if (cond && b->disposition != disp_del_at_next_stop)
5343 {
5344 int within_current_scope = 1;
5345 struct watchpoint * w;
5346
5347 /* We use value_mark and value_free_to_mark because it could
5348 be a long time before we return to the command level and
5349 call free_all_values. We can't call free_all_values
5350 because we might be in the middle of evaluating a
5351 function call. */
5352 struct value *mark = value_mark ();
5353
5354 if (is_watchpoint (b))
5355 w = (struct watchpoint *) b;
5356 else
5357 w = NULL;
5358
5359 /* Need to select the frame, with all that implies so that
5360 the conditions will have the right context. Because we
5361 use the frame, we will not see an inlined function's
5362 variables when we arrive at a breakpoint at the start
5363 of the inlined function; the current frame will be the
5364 call site. */
5365 if (w == NULL || w->cond_exp_valid_block == NULL)
5366 select_frame (get_current_frame ());
5367 else
5368 {
5369 struct frame_info *frame;
5370
5371 /* For local watchpoint expressions, which particular
5372 instance of a local is being watched matters, so we
5373 keep track of the frame to evaluate the expression
5374 in. To evaluate the condition however, it doesn't
5375 really matter which instantiation of the function
5376 where the condition makes sense triggers the
5377 watchpoint. This allows an expression like "watch
5378 global if q > 10" set in `func', catch writes to
5379 global on all threads that call `func', or catch
5380 writes on all recursive calls of `func' by a single
5381 thread. We simply always evaluate the condition in
5382 the innermost frame that's executing where it makes
5383 sense to evaluate the condition. It seems
5384 intuitive. */
5385 frame = block_innermost_frame (w->cond_exp_valid_block);
5386 if (frame != NULL)
5387 select_frame (frame);
5388 else
5389 within_current_scope = 0;
5390 }
5391 if (within_current_scope)
5392 {
5393 try
5394 {
5395 condition_result = breakpoint_cond_eval (cond);
5396 }
5397 catch (const gdb_exception &ex)
5398 {
5399 exception_fprintf (gdb_stderr, ex,
5400 "Error in testing breakpoint condition:\n");
5401 }
5402 }
5403 else
5404 {
5405 warning (_("Watchpoint condition cannot be tested "
5406 "in the current scope"));
5407 /* If we failed to set the right context for this
5408 watchpoint, unconditionally report it. */
5409 }
5410 /* FIXME-someday, should give breakpoint #. */
5411 value_free_to_mark (mark);
5412 }
5413
5414 if (cond && !condition_result)
5415 {
5416 bs->stop = 0;
5417 }
5418 else if (b->ignore_count > 0)
5419 {
5420 b->ignore_count--;
5421 bs->stop = 0;
5422 /* Increase the hit count even though we don't stop. */
5423 ++(b->hit_count);
5424 gdb::observers::breakpoint_modified.notify (b);
5425 }
5426 }
5427
5428 /* Returns true if we need to track moribund locations of LOC's type
5429 on the current target. */
5430
5431 static int
5432 need_moribund_for_location_type (struct bp_location *loc)
5433 {
5434 return ((loc->loc_type == bp_loc_software_breakpoint
5435 && !target_supports_stopped_by_sw_breakpoint ())
5436 || (loc->loc_type == bp_loc_hardware_breakpoint
5437 && !target_supports_stopped_by_hw_breakpoint ()));
5438 }
5439
5440 /* See breakpoint.h. */
5441
5442 bpstat
5443 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5444 const struct target_waitstatus *ws)
5445 {
5446 bpstat bs_head = NULL, *bs_link = &bs_head;
5447
5448 for (breakpoint *b : all_breakpoints ())
5449 {
5450 if (!breakpoint_enabled (b))
5451 continue;
5452
5453 for (bp_location *bl = b->loc; bl != NULL; bl = bl->next)
5454 {
5455 /* For hardware watchpoints, we look only at the first
5456 location. The watchpoint_check function will work on the
5457 entire expression, not the individual locations. For
5458 read watchpoints, the watchpoints_triggered function has
5459 checked all locations already. */
5460 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5461 break;
5462
5463 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
5464 continue;
5465
5466 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5467 continue;
5468
5469 /* Come here if it's a watchpoint, or if the break address
5470 matches. */
5471
5472 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5473 explain stop. */
5474
5475 /* Assume we stop. Should we find a watchpoint that is not
5476 actually triggered, or if the condition of the breakpoint
5477 evaluates as false, we'll reset 'stop' to 0. */
5478 bs->stop = 1;
5479 bs->print = 1;
5480
5481 /* If this is a scope breakpoint, mark the associated
5482 watchpoint as triggered so that we will handle the
5483 out-of-scope event. We'll get to the watchpoint next
5484 iteration. */
5485 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5486 {
5487 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5488
5489 w->watchpoint_triggered = watch_triggered_yes;
5490 }
5491 }
5492 }
5493
5494 /* Check if a moribund breakpoint explains the stop. */
5495 if (!target_supports_stopped_by_sw_breakpoint ()
5496 || !target_supports_stopped_by_hw_breakpoint ())
5497 {
5498 for (bp_location *loc : moribund_locations)
5499 {
5500 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5501 && need_moribund_for_location_type (loc))
5502 {
5503 bpstat bs = new bpstats (loc, &bs_link);
5504 /* For hits of moribund locations, we should just proceed. */
5505 bs->stop = 0;
5506 bs->print = 0;
5507 bs->print_it = print_it_noop;
5508 }
5509 }
5510 }
5511
5512 return bs_head;
5513 }
5514
5515 /* See breakpoint.h. */
5516
5517 bpstat
5518 bpstat_stop_status (const address_space *aspace,
5519 CORE_ADDR bp_addr, thread_info *thread,
5520 const struct target_waitstatus *ws,
5521 bpstat stop_chain)
5522 {
5523 struct breakpoint *b = NULL;
5524 /* First item of allocated bpstat's. */
5525 bpstat bs_head = stop_chain;
5526 bpstat bs;
5527 int need_remove_insert;
5528 int removed_any;
5529
5530 /* First, build the bpstat chain with locations that explain a
5531 target stop, while being careful to not set the target running,
5532 as that may invalidate locations (in particular watchpoint
5533 locations are recreated). Resuming will happen here with
5534 breakpoint conditions or watchpoint expressions that include
5535 inferior function calls. */
5536 if (bs_head == NULL)
5537 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5538
5539 /* A bit of special processing for shlib breakpoints. We need to
5540 process solib loading here, so that the lists of loaded and
5541 unloaded libraries are correct before we handle "catch load" and
5542 "catch unload". */
5543 for (bs = bs_head; bs != NULL; bs = bs->next)
5544 {
5545 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5546 {
5547 handle_solib_event ();
5548 break;
5549 }
5550 }
5551
5552 /* Now go through the locations that caused the target to stop, and
5553 check whether we're interested in reporting this stop to higher
5554 layers, or whether we should resume the target transparently. */
5555
5556 removed_any = 0;
5557
5558 for (bs = bs_head; bs != NULL; bs = bs->next)
5559 {
5560 if (!bs->stop)
5561 continue;
5562
5563 b = bs->breakpoint_at;
5564 b->ops->check_status (bs);
5565 if (bs->stop)
5566 {
5567 bpstat_check_breakpoint_conditions (bs, thread);
5568
5569 if (bs->stop)
5570 {
5571 ++(b->hit_count);
5572 gdb::observers::breakpoint_modified.notify (b);
5573
5574 /* We will stop here. */
5575 if (b->disposition == disp_disable)
5576 {
5577 --(b->enable_count);
5578 if (b->enable_count <= 0)
5579 b->enable_state = bp_disabled;
5580 removed_any = 1;
5581 }
5582 if (b->silent)
5583 bs->print = 0;
5584 bs->commands = b->commands;
5585 if (command_line_is_silent (bs->commands
5586 ? bs->commands.get () : NULL))
5587 bs->print = 0;
5588
5589 b->ops->after_condition_true (bs);
5590 }
5591
5592 }
5593
5594 /* Print nothing for this entry if we don't stop or don't
5595 print. */
5596 if (!bs->stop || !bs->print)
5597 bs->print_it = print_it_noop;
5598 }
5599
5600 /* If we aren't stopping, the value of some hardware watchpoint may
5601 not have changed, but the intermediate memory locations we are
5602 watching may have. Don't bother if we're stopping; this will get
5603 done later. */
5604 need_remove_insert = 0;
5605 if (! bpstat_causes_stop (bs_head))
5606 for (bs = bs_head; bs != NULL; bs = bs->next)
5607 if (!bs->stop
5608 && bs->breakpoint_at
5609 && is_hardware_watchpoint (bs->breakpoint_at))
5610 {
5611 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5612
5613 update_watchpoint (w, 0 /* don't reparse. */);
5614 need_remove_insert = 1;
5615 }
5616
5617 if (need_remove_insert)
5618 update_global_location_list (UGLL_MAY_INSERT);
5619 else if (removed_any)
5620 update_global_location_list (UGLL_DONT_INSERT);
5621
5622 return bs_head;
5623 }
5624
5625 static void
5626 handle_jit_event (CORE_ADDR address)
5627 {
5628 struct gdbarch *gdbarch;
5629
5630 infrun_debug_printf ("handling bp_jit_event");
5631
5632 /* Switch terminal for any messages produced by
5633 breakpoint_re_set. */
5634 target_terminal::ours_for_output ();
5635
5636 gdbarch = get_frame_arch (get_current_frame ());
5637 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5638 thus it is expected that its objectfile can be found through
5639 minimal symbol lookup. If it doesn't work (and assert fails), it
5640 most likely means that `jit_breakpoint_re_set` was changes and this
5641 function needs to be updated too. */
5642 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5643 gdb_assert (jit_bp_sym.objfile != nullptr);
5644 jit_event_handler (gdbarch, jit_bp_sym.objfile);
5645
5646 target_terminal::inferior ();
5647 }
5648
5649 /* Prepare WHAT final decision for infrun. */
5650
5651 /* Decide what infrun needs to do with this bpstat. */
5652
5653 struct bpstat_what
5654 bpstat_what (bpstat bs_head)
5655 {
5656 struct bpstat_what retval;
5657 bpstat bs;
5658
5659 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5660 retval.call_dummy = STOP_NONE;
5661 retval.is_longjmp = false;
5662
5663 for (bs = bs_head; bs != NULL; bs = bs->next)
5664 {
5665 /* Extract this BS's action. After processing each BS, we check
5666 if its action overrides all we've seem so far. */
5667 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5668 enum bptype bptype;
5669
5670 if (bs->breakpoint_at == NULL)
5671 {
5672 /* I suspect this can happen if it was a momentary
5673 breakpoint which has since been deleted. */
5674 bptype = bp_none;
5675 }
5676 else
5677 bptype = bs->breakpoint_at->type;
5678
5679 switch (bptype)
5680 {
5681 case bp_none:
5682 break;
5683 case bp_breakpoint:
5684 case bp_hardware_breakpoint:
5685 case bp_single_step:
5686 case bp_until:
5687 case bp_finish:
5688 case bp_shlib_event:
5689 if (bs->stop)
5690 {
5691 if (bs->print)
5692 this_action = BPSTAT_WHAT_STOP_NOISY;
5693 else
5694 this_action = BPSTAT_WHAT_STOP_SILENT;
5695 }
5696 else
5697 this_action = BPSTAT_WHAT_SINGLE;
5698 break;
5699 case bp_watchpoint:
5700 case bp_hardware_watchpoint:
5701 case bp_read_watchpoint:
5702 case bp_access_watchpoint:
5703 if (bs->stop)
5704 {
5705 if (bs->print)
5706 this_action = BPSTAT_WHAT_STOP_NOISY;
5707 else
5708 this_action = BPSTAT_WHAT_STOP_SILENT;
5709 }
5710 else
5711 {
5712 /* There was a watchpoint, but we're not stopping.
5713 This requires no further action. */
5714 }
5715 break;
5716 case bp_longjmp:
5717 case bp_longjmp_call_dummy:
5718 case bp_exception:
5719 if (bs->stop)
5720 {
5721 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5722 retval.is_longjmp = bptype != bp_exception;
5723 }
5724 else
5725 this_action = BPSTAT_WHAT_SINGLE;
5726 break;
5727 case bp_longjmp_resume:
5728 case bp_exception_resume:
5729 if (bs->stop)
5730 {
5731 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5732 retval.is_longjmp = bptype == bp_longjmp_resume;
5733 }
5734 else
5735 this_action = BPSTAT_WHAT_SINGLE;
5736 break;
5737 case bp_step_resume:
5738 if (bs->stop)
5739 this_action = BPSTAT_WHAT_STEP_RESUME;
5740 else
5741 {
5742 /* It is for the wrong frame. */
5743 this_action = BPSTAT_WHAT_SINGLE;
5744 }
5745 break;
5746 case bp_hp_step_resume:
5747 if (bs->stop)
5748 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5749 else
5750 {
5751 /* It is for the wrong frame. */
5752 this_action = BPSTAT_WHAT_SINGLE;
5753 }
5754 break;
5755 case bp_watchpoint_scope:
5756 case bp_thread_event:
5757 case bp_overlay_event:
5758 case bp_longjmp_master:
5759 case bp_std_terminate_master:
5760 case bp_exception_master:
5761 this_action = BPSTAT_WHAT_SINGLE;
5762 break;
5763 case bp_catchpoint:
5764 if (bs->stop)
5765 {
5766 if (bs->print)
5767 this_action = BPSTAT_WHAT_STOP_NOISY;
5768 else
5769 this_action = BPSTAT_WHAT_STOP_SILENT;
5770 }
5771 else
5772 {
5773 /* Some catchpoints are implemented with breakpoints.
5774 For those, we need to step over the breakpoint. */
5775 if (bs->bp_location_at->loc_type != bp_loc_other)
5776 this_action = BPSTAT_WHAT_SINGLE;
5777 }
5778 break;
5779 case bp_jit_event:
5780 this_action = BPSTAT_WHAT_SINGLE;
5781 break;
5782 case bp_call_dummy:
5783 /* Make sure the action is stop (silent or noisy),
5784 so infrun.c pops the dummy frame. */
5785 retval.call_dummy = STOP_STACK_DUMMY;
5786 this_action = BPSTAT_WHAT_STOP_SILENT;
5787 break;
5788 case bp_std_terminate:
5789 /* Make sure the action is stop (silent or noisy),
5790 so infrun.c pops the dummy frame. */
5791 retval.call_dummy = STOP_STD_TERMINATE;
5792 this_action = BPSTAT_WHAT_STOP_SILENT;
5793 break;
5794 case bp_tracepoint:
5795 case bp_fast_tracepoint:
5796 case bp_static_tracepoint:
5797 /* Tracepoint hits should not be reported back to GDB, and
5798 if one got through somehow, it should have been filtered
5799 out already. */
5800 internal_error (__FILE__, __LINE__,
5801 _("bpstat_what: tracepoint encountered"));
5802 break;
5803 case bp_gnu_ifunc_resolver:
5804 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5805 this_action = BPSTAT_WHAT_SINGLE;
5806 break;
5807 case bp_gnu_ifunc_resolver_return:
5808 /* The breakpoint will be removed, execution will restart from the
5809 PC of the former breakpoint. */
5810 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5811 break;
5812
5813 case bp_dprintf:
5814 if (bs->stop)
5815 this_action = BPSTAT_WHAT_STOP_SILENT;
5816 else
5817 this_action = BPSTAT_WHAT_SINGLE;
5818 break;
5819
5820 default:
5821 internal_error (__FILE__, __LINE__,
5822 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5823 }
5824
5825 retval.main_action = std::max (retval.main_action, this_action);
5826 }
5827
5828 return retval;
5829 }
5830
5831 void
5832 bpstat_run_callbacks (bpstat bs_head)
5833 {
5834 bpstat bs;
5835
5836 for (bs = bs_head; bs != NULL; bs = bs->next)
5837 {
5838 struct breakpoint *b = bs->breakpoint_at;
5839
5840 if (b == NULL)
5841 continue;
5842 switch (b->type)
5843 {
5844 case bp_jit_event:
5845 handle_jit_event (bs->bp_location_at->address);
5846 break;
5847 case bp_gnu_ifunc_resolver:
5848 gnu_ifunc_resolver_stop (b);
5849 break;
5850 case bp_gnu_ifunc_resolver_return:
5851 gnu_ifunc_resolver_return_stop (b);
5852 break;
5853 }
5854 }
5855 }
5856
5857 /* See breakpoint.h. */
5858
5859 bool
5860 bpstat_should_step ()
5861 {
5862 for (breakpoint *b : all_breakpoints ())
5863 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5864 return true;
5865
5866 return false;
5867 }
5868
5869 /* See breakpoint.h. */
5870
5871 bool
5872 bpstat_causes_stop (bpstat bs)
5873 {
5874 for (; bs != NULL; bs = bs->next)
5875 if (bs->stop)
5876 return true;
5877
5878 return false;
5879 }
5880
5881 \f
5882
5883 /* Compute a string of spaces suitable to indent the next line
5884 so it starts at the position corresponding to the table column
5885 named COL_NAME in the currently active table of UIOUT. */
5886
5887 static char *
5888 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5889 {
5890 static char wrap_indent[80];
5891 int i, total_width, width, align;
5892 const char *text;
5893
5894 total_width = 0;
5895 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5896 {
5897 if (strcmp (text, col_name) == 0)
5898 {
5899 gdb_assert (total_width < sizeof wrap_indent);
5900 memset (wrap_indent, ' ', total_width);
5901 wrap_indent[total_width] = 0;
5902
5903 return wrap_indent;
5904 }
5905
5906 total_width += width + 1;
5907 }
5908
5909 return NULL;
5910 }
5911
5912 /* Determine if the locations of this breakpoint will have their conditions
5913 evaluated by the target, host or a mix of both. Returns the following:
5914
5915 "host": Host evals condition.
5916 "host or target": Host or Target evals condition.
5917 "target": Target evals condition.
5918 */
5919
5920 static const char *
5921 bp_condition_evaluator (struct breakpoint *b)
5922 {
5923 struct bp_location *bl;
5924 char host_evals = 0;
5925 char target_evals = 0;
5926
5927 if (!b)
5928 return NULL;
5929
5930 if (!is_breakpoint (b))
5931 return NULL;
5932
5933 if (gdb_evaluates_breakpoint_condition_p ()
5934 || !target_supports_evaluation_of_breakpoint_conditions ())
5935 return condition_evaluation_host;
5936
5937 for (bl = b->loc; bl; bl = bl->next)
5938 {
5939 if (bl->cond_bytecode)
5940 target_evals++;
5941 else
5942 host_evals++;
5943 }
5944
5945 if (host_evals && target_evals)
5946 return condition_evaluation_both;
5947 else if (target_evals)
5948 return condition_evaluation_target;
5949 else
5950 return condition_evaluation_host;
5951 }
5952
5953 /* Determine the breakpoint location's condition evaluator. This is
5954 similar to bp_condition_evaluator, but for locations. */
5955
5956 static const char *
5957 bp_location_condition_evaluator (struct bp_location *bl)
5958 {
5959 if (bl && !is_breakpoint (bl->owner))
5960 return NULL;
5961
5962 if (gdb_evaluates_breakpoint_condition_p ()
5963 || !target_supports_evaluation_of_breakpoint_conditions ())
5964 return condition_evaluation_host;
5965
5966 if (bl && bl->cond_bytecode)
5967 return condition_evaluation_target;
5968 else
5969 return condition_evaluation_host;
5970 }
5971
5972 /* Print the LOC location out of the list of B->LOC locations. */
5973
5974 static void
5975 print_breakpoint_location (struct breakpoint *b,
5976 struct bp_location *loc)
5977 {
5978 struct ui_out *uiout = current_uiout;
5979
5980 scoped_restore_current_program_space restore_pspace;
5981
5982 if (loc != NULL && loc->shlib_disabled)
5983 loc = NULL;
5984
5985 if (loc != NULL)
5986 set_current_program_space (loc->pspace);
5987
5988 if (b->display_canonical)
5989 uiout->field_string ("what", event_location_to_string (b->location.get ()));
5990 else if (loc && loc->symtab)
5991 {
5992 const struct symbol *sym = loc->symbol;
5993
5994 if (sym)
5995 {
5996 uiout->text ("in ");
5997 uiout->field_string ("func", sym->print_name (),
5998 function_name_style.style ());
5999 uiout->text (" ");
6000 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6001 uiout->text ("at ");
6002 }
6003 uiout->field_string ("file",
6004 symtab_to_filename_for_display (loc->symtab),
6005 file_name_style.style ());
6006 uiout->text (":");
6007
6008 if (uiout->is_mi_like_p ())
6009 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
6010
6011 uiout->field_signed ("line", loc->line_number);
6012 }
6013 else if (loc)
6014 {
6015 string_file stb;
6016
6017 print_address_symbolic (loc->gdbarch, loc->address, &stb,
6018 demangle, "");
6019 uiout->field_stream ("at", stb);
6020 }
6021 else
6022 {
6023 uiout->field_string ("pending",
6024 event_location_to_string (b->location.get ()));
6025 /* If extra_string is available, it could be holding a condition
6026 or dprintf arguments. In either case, make sure it is printed,
6027 too, but only for non-MI streams. */
6028 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
6029 {
6030 if (b->type == bp_dprintf)
6031 uiout->text (",");
6032 else
6033 uiout->text (" ");
6034 uiout->text (b->extra_string);
6035 }
6036 }
6037
6038 if (loc && is_breakpoint (b)
6039 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6040 && bp_condition_evaluator (b) == condition_evaluation_both)
6041 {
6042 uiout->text (" (");
6043 uiout->field_string ("evaluated-by",
6044 bp_location_condition_evaluator (loc));
6045 uiout->text (")");
6046 }
6047 }
6048
6049 static const char *
6050 bptype_string (enum bptype type)
6051 {
6052 struct ep_type_description
6053 {
6054 enum bptype type;
6055 const char *description;
6056 };
6057 static struct ep_type_description bptypes[] =
6058 {
6059 {bp_none, "?deleted?"},
6060 {bp_breakpoint, "breakpoint"},
6061 {bp_hardware_breakpoint, "hw breakpoint"},
6062 {bp_single_step, "sw single-step"},
6063 {bp_until, "until"},
6064 {bp_finish, "finish"},
6065 {bp_watchpoint, "watchpoint"},
6066 {bp_hardware_watchpoint, "hw watchpoint"},
6067 {bp_read_watchpoint, "read watchpoint"},
6068 {bp_access_watchpoint, "acc watchpoint"},
6069 {bp_longjmp, "longjmp"},
6070 {bp_longjmp_resume, "longjmp resume"},
6071 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6072 {bp_exception, "exception"},
6073 {bp_exception_resume, "exception resume"},
6074 {bp_step_resume, "step resume"},
6075 {bp_hp_step_resume, "high-priority step resume"},
6076 {bp_watchpoint_scope, "watchpoint scope"},
6077 {bp_call_dummy, "call dummy"},
6078 {bp_std_terminate, "std::terminate"},
6079 {bp_shlib_event, "shlib events"},
6080 {bp_thread_event, "thread events"},
6081 {bp_overlay_event, "overlay events"},
6082 {bp_longjmp_master, "longjmp master"},
6083 {bp_std_terminate_master, "std::terminate master"},
6084 {bp_exception_master, "exception master"},
6085 {bp_catchpoint, "catchpoint"},
6086 {bp_tracepoint, "tracepoint"},
6087 {bp_fast_tracepoint, "fast tracepoint"},
6088 {bp_static_tracepoint, "static tracepoint"},
6089 {bp_dprintf, "dprintf"},
6090 {bp_jit_event, "jit events"},
6091 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6092 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6093 };
6094
6095 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6096 || ((int) type != bptypes[(int) type].type))
6097 internal_error (__FILE__, __LINE__,
6098 _("bptypes table does not describe type #%d."),
6099 (int) type);
6100
6101 return bptypes[(int) type].description;
6102 }
6103
6104 /* For MI, output a field named 'thread-groups' with a list as the value.
6105 For CLI, prefix the list with the string 'inf'. */
6106
6107 static void
6108 output_thread_groups (struct ui_out *uiout,
6109 const char *field_name,
6110 const std::vector<int> &inf_nums,
6111 int mi_only)
6112 {
6113 int is_mi = uiout->is_mi_like_p ();
6114
6115 /* For backward compatibility, don't display inferiors in CLI unless
6116 there are several. Always display them for MI. */
6117 if (!is_mi && mi_only)
6118 return;
6119
6120 ui_out_emit_list list_emitter (uiout, field_name);
6121
6122 for (size_t i = 0; i < inf_nums.size (); i++)
6123 {
6124 if (is_mi)
6125 {
6126 char mi_group[10];
6127
6128 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
6129 uiout->field_string (NULL, mi_group);
6130 }
6131 else
6132 {
6133 if (i == 0)
6134 uiout->text (" inf ");
6135 else
6136 uiout->text (", ");
6137
6138 uiout->text (plongest (inf_nums[i]));
6139 }
6140 }
6141 }
6142
6143 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6144 instead of going via breakpoint_ops::print_one. This makes "maint
6145 info breakpoints" show the software breakpoint locations of
6146 catchpoints, which are considered internal implementation
6147 detail. */
6148
6149 static void
6150 print_one_breakpoint_location (struct breakpoint *b,
6151 struct bp_location *loc,
6152 int loc_number,
6153 struct bp_location **last_loc,
6154 int allflag, bool raw_loc)
6155 {
6156 struct command_line *l;
6157 static char bpenables[] = "nynny";
6158
6159 struct ui_out *uiout = current_uiout;
6160 int header_of_multiple = 0;
6161 int part_of_multiple = (loc != NULL);
6162 struct value_print_options opts;
6163
6164 get_user_print_options (&opts);
6165
6166 gdb_assert (!loc || loc_number != 0);
6167 /* See comment in print_one_breakpoint concerning treatment of
6168 breakpoints with single disabled location. */
6169 if (loc == NULL
6170 && (b->loc != NULL
6171 && (b->loc->next != NULL
6172 || !b->loc->enabled || b->loc->disabled_by_cond)))
6173 header_of_multiple = 1;
6174 if (loc == NULL)
6175 loc = b->loc;
6176
6177 annotate_record ();
6178
6179 /* 1 */
6180 annotate_field (0);
6181 if (part_of_multiple)
6182 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6183 else
6184 uiout->field_signed ("number", b->number);
6185
6186 /* 2 */
6187 annotate_field (1);
6188 if (part_of_multiple)
6189 uiout->field_skip ("type");
6190 else
6191 uiout->field_string ("type", bptype_string (b->type));
6192
6193 /* 3 */
6194 annotate_field (2);
6195 if (part_of_multiple)
6196 uiout->field_skip ("disp");
6197 else
6198 uiout->field_string ("disp", bpdisp_text (b->disposition));
6199
6200 /* 4 */
6201 annotate_field (3);
6202 /* For locations that are disabled because of an invalid condition,
6203 display "N*" on CLI, where "*" refers to a footnote below the
6204 table. For MI, simply display a "N" without a footnote. */
6205 const char *N = (uiout->is_mi_like_p ()) ? "N" : "N*";
6206 if (part_of_multiple)
6207 uiout->field_string ("enabled", (loc->disabled_by_cond ? N
6208 : (loc->enabled ? "y" : "n")));
6209 else
6210 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6211
6212 /* 5 and 6 */
6213 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
6214 b->ops->print_one (b, last_loc);
6215 else
6216 {
6217 if (is_watchpoint (b))
6218 {
6219 struct watchpoint *w = (struct watchpoint *) b;
6220
6221 /* Field 4, the address, is omitted (which makes the columns
6222 not line up too nicely with the headers, but the effect
6223 is relatively readable). */
6224 if (opts.addressprint)
6225 uiout->field_skip ("addr");
6226 annotate_field (5);
6227 uiout->field_string ("what", w->exp_string);
6228 }
6229 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6230 || is_ada_exception_catchpoint (b))
6231 {
6232 if (opts.addressprint)
6233 {
6234 annotate_field (4);
6235 if (header_of_multiple)
6236 uiout->field_string ("addr", "<MULTIPLE>",
6237 metadata_style.style ());
6238 else if (b->loc == NULL || loc->shlib_disabled)
6239 uiout->field_string ("addr", "<PENDING>",
6240 metadata_style.style ());
6241 else
6242 uiout->field_core_addr ("addr",
6243 loc->gdbarch, loc->address);
6244 }
6245 annotate_field (5);
6246 if (!header_of_multiple)
6247 print_breakpoint_location (b, loc);
6248 if (b->loc)
6249 *last_loc = b->loc;
6250 }
6251 }
6252
6253 if (loc != NULL && !header_of_multiple)
6254 {
6255 std::vector<int> inf_nums;
6256 int mi_only = 1;
6257
6258 for (inferior *inf : all_inferiors ())
6259 {
6260 if (inf->pspace == loc->pspace)
6261 inf_nums.push_back (inf->num);
6262 }
6263
6264 /* For backward compatibility, don't display inferiors in CLI unless
6265 there are several. Always display for MI. */
6266 if (allflag
6267 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6268 && (program_spaces.size () > 1
6269 || number_of_inferiors () > 1)
6270 /* LOC is for existing B, it cannot be in
6271 moribund_locations and thus having NULL OWNER. */
6272 && loc->owner->type != bp_catchpoint))
6273 mi_only = 0;
6274 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6275 }
6276
6277 if (!part_of_multiple)
6278 {
6279 if (b->thread != -1)
6280 {
6281 /* FIXME: This seems to be redundant and lost here; see the
6282 "stop only in" line a little further down. */
6283 uiout->text (" thread ");
6284 uiout->field_signed ("thread", b->thread);
6285 }
6286 else if (b->task != 0)
6287 {
6288 uiout->text (" task ");
6289 uiout->field_signed ("task", b->task);
6290 }
6291 }
6292
6293 uiout->text ("\n");
6294
6295 if (!part_of_multiple)
6296 b->ops->print_one_detail (b, uiout);
6297
6298 if (part_of_multiple && frame_id_p (b->frame_id))
6299 {
6300 annotate_field (6);
6301 uiout->text ("\tstop only in stack frame at ");
6302 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6303 the frame ID. */
6304 uiout->field_core_addr ("frame",
6305 b->gdbarch, b->frame_id.stack_addr);
6306 uiout->text ("\n");
6307 }
6308
6309 if (!part_of_multiple && b->cond_string)
6310 {
6311 annotate_field (7);
6312 if (is_tracepoint (b))
6313 uiout->text ("\ttrace only if ");
6314 else
6315 uiout->text ("\tstop only if ");
6316 uiout->field_string ("cond", b->cond_string);
6317
6318 /* Print whether the target is doing the breakpoint's condition
6319 evaluation. If GDB is doing the evaluation, don't print anything. */
6320 if (is_breakpoint (b)
6321 && breakpoint_condition_evaluation_mode ()
6322 == condition_evaluation_target)
6323 {
6324 uiout->message (" (%pF evals)",
6325 string_field ("evaluated-by",
6326 bp_condition_evaluator (b)));
6327 }
6328 uiout->text ("\n");
6329 }
6330
6331 if (!part_of_multiple && b->thread != -1)
6332 {
6333 /* FIXME should make an annotation for this. */
6334 uiout->text ("\tstop only in thread ");
6335 if (uiout->is_mi_like_p ())
6336 uiout->field_signed ("thread", b->thread);
6337 else
6338 {
6339 struct thread_info *thr = find_thread_global_id (b->thread);
6340
6341 uiout->field_string ("thread", print_thread_id (thr));
6342 }
6343 uiout->text ("\n");
6344 }
6345
6346 if (!part_of_multiple)
6347 {
6348 if (b->hit_count)
6349 {
6350 /* FIXME should make an annotation for this. */
6351 if (is_catchpoint (b))
6352 uiout->text ("\tcatchpoint");
6353 else if (is_tracepoint (b))
6354 uiout->text ("\ttracepoint");
6355 else
6356 uiout->text ("\tbreakpoint");
6357 uiout->text (" already hit ");
6358 uiout->field_signed ("times", b->hit_count);
6359 if (b->hit_count == 1)
6360 uiout->text (" time\n");
6361 else
6362 uiout->text (" times\n");
6363 }
6364 else
6365 {
6366 /* Output the count also if it is zero, but only if this is mi. */
6367 if (uiout->is_mi_like_p ())
6368 uiout->field_signed ("times", b->hit_count);
6369 }
6370 }
6371
6372 if (!part_of_multiple && b->ignore_count)
6373 {
6374 annotate_field (8);
6375 uiout->message ("\tignore next %pF hits\n",
6376 signed_field ("ignore", b->ignore_count));
6377 }
6378
6379 /* Note that an enable count of 1 corresponds to "enable once"
6380 behavior, which is reported by the combination of enablement and
6381 disposition, so we don't need to mention it here. */
6382 if (!part_of_multiple && b->enable_count > 1)
6383 {
6384 annotate_field (8);
6385 uiout->text ("\tdisable after ");
6386 /* Tweak the wording to clarify that ignore and enable counts
6387 are distinct, and have additive effect. */
6388 if (b->ignore_count)
6389 uiout->text ("additional ");
6390 else
6391 uiout->text ("next ");
6392 uiout->field_signed ("enable", b->enable_count);
6393 uiout->text (" hits\n");
6394 }
6395
6396 if (!part_of_multiple && is_tracepoint (b))
6397 {
6398 struct tracepoint *tp = (struct tracepoint *) b;
6399
6400 if (tp->traceframe_usage)
6401 {
6402 uiout->text ("\ttrace buffer usage ");
6403 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6404 uiout->text (" bytes\n");
6405 }
6406 }
6407
6408 l = b->commands ? b->commands.get () : NULL;
6409 if (!part_of_multiple && l)
6410 {
6411 annotate_field (9);
6412 ui_out_emit_tuple tuple_emitter (uiout, "script");
6413 print_command_lines (uiout, l, 4);
6414 }
6415
6416 if (is_tracepoint (b))
6417 {
6418 struct tracepoint *t = (struct tracepoint *) b;
6419
6420 if (!part_of_multiple && t->pass_count)
6421 {
6422 annotate_field (10);
6423 uiout->text ("\tpass count ");
6424 uiout->field_signed ("pass", t->pass_count);
6425 uiout->text (" \n");
6426 }
6427
6428 /* Don't display it when tracepoint or tracepoint location is
6429 pending. */
6430 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6431 {
6432 annotate_field (11);
6433
6434 if (uiout->is_mi_like_p ())
6435 uiout->field_string ("installed",
6436 loc->inserted ? "y" : "n");
6437 else
6438 {
6439 if (loc->inserted)
6440 uiout->text ("\t");
6441 else
6442 uiout->text ("\tnot ");
6443 uiout->text ("installed on target\n");
6444 }
6445 }
6446 }
6447
6448 if (uiout->is_mi_like_p () && !part_of_multiple)
6449 {
6450 if (is_watchpoint (b))
6451 {
6452 struct watchpoint *w = (struct watchpoint *) b;
6453
6454 uiout->field_string ("original-location", w->exp_string);
6455 }
6456 else if (b->location != NULL
6457 && event_location_to_string (b->location.get ()) != NULL)
6458 uiout->field_string ("original-location",
6459 event_location_to_string (b->location.get ()));
6460 }
6461 }
6462
6463 /* See breakpoint.h. */
6464
6465 bool fix_multi_location_breakpoint_output_globally = false;
6466
6467 static void
6468 print_one_breakpoint (struct breakpoint *b,
6469 struct bp_location **last_loc,
6470 int allflag)
6471 {
6472 struct ui_out *uiout = current_uiout;
6473 bool use_fixed_output
6474 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6475 || fix_multi_location_breakpoint_output_globally);
6476
6477 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6478 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
6479
6480 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6481 are outside. */
6482 if (!use_fixed_output)
6483 bkpt_tuple_emitter.reset ();
6484
6485 /* If this breakpoint has custom print function,
6486 it's already printed. Otherwise, print individual
6487 locations, if any. */
6488 if (b->ops == NULL
6489 || b->ops->print_one == NULL
6490 || allflag)
6491 {
6492 /* If breakpoint has a single location that is disabled, we
6493 print it as if it had several locations, since otherwise it's
6494 hard to represent "breakpoint enabled, location disabled"
6495 situation.
6496
6497 Note that while hardware watchpoints have several locations
6498 internally, that's not a property exposed to users.
6499
6500 Likewise, while catchpoints may be implemented with
6501 breakpoints (e.g., catch throw), that's not a property
6502 exposed to users. We do however display the internal
6503 breakpoint locations with "maint info breakpoints". */
6504 if (!is_hardware_watchpoint (b)
6505 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6506 || is_ada_exception_catchpoint (b))
6507 && (allflag
6508 || (b->loc && (b->loc->next
6509 || !b->loc->enabled
6510 || b->loc->disabled_by_cond))))
6511 {
6512 gdb::optional<ui_out_emit_list> locations_list;
6513
6514 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6515 MI record. For later versions, place breakpoint locations in a
6516 list. */
6517 if (uiout->is_mi_like_p () && use_fixed_output)
6518 locations_list.emplace (uiout, "locations");
6519
6520 int n = 1;
6521 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next, ++n)
6522 {
6523 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6524 print_one_breakpoint_location (b, loc, n, last_loc,
6525 allflag, allflag);
6526 }
6527 }
6528 }
6529 }
6530
6531 static int
6532 breakpoint_address_bits (struct breakpoint *b)
6533 {
6534 int print_address_bits = 0;
6535 struct bp_location *loc;
6536
6537 /* Software watchpoints that aren't watching memory don't have an
6538 address to print. */
6539 if (is_no_memory_software_watchpoint (b))
6540 return 0;
6541
6542 for (loc = b->loc; loc; loc = loc->next)
6543 {
6544 int addr_bit;
6545
6546 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6547 if (addr_bit > print_address_bits)
6548 print_address_bits = addr_bit;
6549 }
6550
6551 return print_address_bits;
6552 }
6553
6554 /* See breakpoint.h. */
6555
6556 void
6557 print_breakpoint (breakpoint *b)
6558 {
6559 struct bp_location *dummy_loc = NULL;
6560 print_one_breakpoint (b, &dummy_loc, 0);
6561 }
6562
6563 /* Return true if this breakpoint was set by the user, false if it is
6564 internal or momentary. */
6565
6566 int
6567 user_breakpoint_p (struct breakpoint *b)
6568 {
6569 return b->number > 0;
6570 }
6571
6572 /* See breakpoint.h. */
6573
6574 int
6575 pending_breakpoint_p (struct breakpoint *b)
6576 {
6577 return b->loc == NULL;
6578 }
6579
6580 /* Print information on breakpoints (including watchpoints and tracepoints).
6581
6582 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6583 understood by number_or_range_parser. Only breakpoints included in this
6584 list are then printed.
6585
6586 If SHOW_INTERNAL is true, print internal breakpoints.
6587
6588 If FILTER is non-NULL, call it on each breakpoint and only include the
6589 ones for which it returns true.
6590
6591 Return the total number of breakpoints listed. */
6592
6593 static int
6594 breakpoint_1 (const char *bp_num_list, bool show_internal,
6595 bool (*filter) (const struct breakpoint *))
6596 {
6597 struct bp_location *last_loc = NULL;
6598 int nr_printable_breakpoints;
6599 struct value_print_options opts;
6600 int print_address_bits = 0;
6601 int print_type_col_width = 14;
6602 struct ui_out *uiout = current_uiout;
6603 bool has_disabled_by_cond_location = false;
6604
6605 get_user_print_options (&opts);
6606
6607 /* Compute the number of rows in the table, as well as the size
6608 required for address fields. */
6609 nr_printable_breakpoints = 0;
6610 for (breakpoint *b : all_breakpoints ())
6611 {
6612 /* If we have a filter, only list the breakpoints it accepts. */
6613 if (filter && !filter (b))
6614 continue;
6615
6616 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6617 accept. Skip the others. */
6618 if (bp_num_list != NULL && *bp_num_list != '\0')
6619 {
6620 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6621 continue;
6622 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6623 continue;
6624 }
6625
6626 if (show_internal || user_breakpoint_p (b))
6627 {
6628 int addr_bit, type_len;
6629
6630 addr_bit = breakpoint_address_bits (b);
6631 if (addr_bit > print_address_bits)
6632 print_address_bits = addr_bit;
6633
6634 type_len = strlen (bptype_string (b->type));
6635 if (type_len > print_type_col_width)
6636 print_type_col_width = type_len;
6637
6638 nr_printable_breakpoints++;
6639 }
6640 }
6641
6642 {
6643 ui_out_emit_table table_emitter (uiout,
6644 opts.addressprint ? 6 : 5,
6645 nr_printable_breakpoints,
6646 "BreakpointTable");
6647
6648 if (nr_printable_breakpoints > 0)
6649 annotate_breakpoints_headers ();
6650 if (nr_printable_breakpoints > 0)
6651 annotate_field (0);
6652 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6653 if (nr_printable_breakpoints > 0)
6654 annotate_field (1);
6655 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6656 if (nr_printable_breakpoints > 0)
6657 annotate_field (2);
6658 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6659 if (nr_printable_breakpoints > 0)
6660 annotate_field (3);
6661 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6662 if (opts.addressprint)
6663 {
6664 if (nr_printable_breakpoints > 0)
6665 annotate_field (4);
6666 if (print_address_bits <= 32)
6667 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6668 else
6669 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6670 }
6671 if (nr_printable_breakpoints > 0)
6672 annotate_field (5);
6673 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6674 uiout->table_body ();
6675 if (nr_printable_breakpoints > 0)
6676 annotate_breakpoints_table ();
6677
6678 for (breakpoint *b : all_breakpoints ())
6679 {
6680 QUIT;
6681 /* If we have a filter, only list the breakpoints it accepts. */
6682 if (filter && !filter (b))
6683 continue;
6684
6685 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6686 accept. Skip the others. */
6687
6688 if (bp_num_list != NULL && *bp_num_list != '\0')
6689 {
6690 if (show_internal) /* maintenance info breakpoint */
6691 {
6692 if (parse_and_eval_long (bp_num_list) != b->number)
6693 continue;
6694 }
6695 else /* all others */
6696 {
6697 if (!number_is_in_list (bp_num_list, b->number))
6698 continue;
6699 }
6700 }
6701 /* We only print out user settable breakpoints unless the
6702 show_internal is set. */
6703 if (show_internal || user_breakpoint_p (b))
6704 {
6705 print_one_breakpoint (b, &last_loc, show_internal);
6706 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
6707 if (loc->disabled_by_cond)
6708 has_disabled_by_cond_location = true;
6709 }
6710 }
6711 }
6712
6713 if (nr_printable_breakpoints == 0)
6714 {
6715 /* If there's a filter, let the caller decide how to report
6716 empty list. */
6717 if (!filter)
6718 {
6719 if (bp_num_list == NULL || *bp_num_list == '\0')
6720 uiout->message ("No breakpoints or watchpoints.\n");
6721 else
6722 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6723 bp_num_list);
6724 }
6725 }
6726 else
6727 {
6728 if (last_loc && !server_command)
6729 set_next_address (last_loc->gdbarch, last_loc->address);
6730
6731 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
6732 uiout->message (_("(*): Breakpoint condition is invalid at this "
6733 "location.\n"));
6734 }
6735
6736 /* FIXME? Should this be moved up so that it is only called when
6737 there have been breakpoints? */
6738 annotate_breakpoints_table_end ();
6739
6740 return nr_printable_breakpoints;
6741 }
6742
6743 /* Display the value of default-collect in a way that is generally
6744 compatible with the breakpoint list. */
6745
6746 static void
6747 default_collect_info (void)
6748 {
6749 struct ui_out *uiout = current_uiout;
6750
6751 /* If it has no value (which is frequently the case), say nothing; a
6752 message like "No default-collect." gets in user's face when it's
6753 not wanted. */
6754 if (!*default_collect)
6755 return;
6756
6757 /* The following phrase lines up nicely with per-tracepoint collect
6758 actions. */
6759 uiout->text ("default collect ");
6760 uiout->field_string ("default-collect", default_collect);
6761 uiout->text (" \n");
6762 }
6763
6764 static void
6765 info_breakpoints_command (const char *args, int from_tty)
6766 {
6767 breakpoint_1 (args, false, NULL);
6768
6769 default_collect_info ();
6770 }
6771
6772 static void
6773 info_watchpoints_command (const char *args, int from_tty)
6774 {
6775 int num_printed = breakpoint_1 (args, false, is_watchpoint);
6776 struct ui_out *uiout = current_uiout;
6777
6778 if (num_printed == 0)
6779 {
6780 if (args == NULL || *args == '\0')
6781 uiout->message ("No watchpoints.\n");
6782 else
6783 uiout->message ("No watchpoint matching '%s'.\n", args);
6784 }
6785 }
6786
6787 static void
6788 maintenance_info_breakpoints (const char *args, int from_tty)
6789 {
6790 breakpoint_1 (args, true, NULL);
6791
6792 default_collect_info ();
6793 }
6794
6795 static int
6796 breakpoint_has_pc (struct breakpoint *b,
6797 struct program_space *pspace,
6798 CORE_ADDR pc, struct obj_section *section)
6799 {
6800 struct bp_location *bl = b->loc;
6801
6802 for (; bl; bl = bl->next)
6803 {
6804 if (bl->pspace == pspace
6805 && bl->address == pc
6806 && (!overlay_debugging || bl->section == section))
6807 return 1;
6808 }
6809 return 0;
6810 }
6811
6812 /* Print a message describing any user-breakpoints set at PC. This
6813 concerns with logical breakpoints, so we match program spaces, not
6814 address spaces. */
6815
6816 static void
6817 describe_other_breakpoints (struct gdbarch *gdbarch,
6818 struct program_space *pspace, CORE_ADDR pc,
6819 struct obj_section *section, int thread)
6820 {
6821 int others = 0;
6822
6823 for (breakpoint *b : all_breakpoints ())
6824 others += (user_breakpoint_p (b)
6825 && breakpoint_has_pc (b, pspace, pc, section));
6826
6827 if (others > 0)
6828 {
6829 if (others == 1)
6830 printf_filtered (_("Note: breakpoint "));
6831 else /* if (others == ???) */
6832 printf_filtered (_("Note: breakpoints "));
6833 for (breakpoint *b : all_breakpoints ())
6834 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6835 {
6836 others--;
6837 printf_filtered ("%d", b->number);
6838 if (b->thread == -1 && thread != -1)
6839 printf_filtered (" (all threads)");
6840 else if (b->thread != -1)
6841 printf_filtered (" (thread %d)", b->thread);
6842 printf_filtered ("%s%s ",
6843 ((b->enable_state == bp_disabled
6844 || b->enable_state == bp_call_disabled)
6845 ? " (disabled)"
6846 : ""),
6847 (others > 1) ? ","
6848 : ((others == 1) ? " and" : ""));
6849 }
6850 current_uiout->message (_("also set at pc %ps.\n"),
6851 styled_string (address_style.style (),
6852 paddress (gdbarch, pc)));
6853 }
6854 }
6855 \f
6856
6857 /* Return true iff it is meaningful to use the address member of LOC.
6858 For some breakpoint types, the locations' address members are
6859 irrelevant and it makes no sense to attempt to compare them to
6860 other addresses (or use them for any other purpose either).
6861
6862 More specifically, software watchpoints and catchpoints that are
6863 not backed by breakpoints always have a zero valued location
6864 address and we don't want to mark breakpoints of any of these types
6865 to be a duplicate of an actual breakpoint location at address
6866 zero. */
6867
6868 static bool
6869 bl_address_is_meaningful (bp_location *loc)
6870 {
6871 return loc->loc_type != bp_loc_other;
6872 }
6873
6874 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6875 true if LOC1 and LOC2 represent the same watchpoint location. */
6876
6877 static int
6878 watchpoint_locations_match (struct bp_location *loc1,
6879 struct bp_location *loc2)
6880 {
6881 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6882 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6883
6884 /* Both of them must exist. */
6885 gdb_assert (w1 != NULL);
6886 gdb_assert (w2 != NULL);
6887
6888 /* If the target can evaluate the condition expression in hardware,
6889 then we we need to insert both watchpoints even if they are at
6890 the same place. Otherwise the watchpoint will only trigger when
6891 the condition of whichever watchpoint was inserted evaluates to
6892 true, not giving a chance for GDB to check the condition of the
6893 other watchpoint. */
6894 if ((w1->cond_exp
6895 && target_can_accel_watchpoint_condition (loc1->address,
6896 loc1->length,
6897 loc1->watchpoint_type,
6898 w1->cond_exp.get ()))
6899 || (w2->cond_exp
6900 && target_can_accel_watchpoint_condition (loc2->address,
6901 loc2->length,
6902 loc2->watchpoint_type,
6903 w2->cond_exp.get ())))
6904 return 0;
6905
6906 /* Note that this checks the owner's type, not the location's. In
6907 case the target does not support read watchpoints, but does
6908 support access watchpoints, we'll have bp_read_watchpoint
6909 watchpoints with hw_access locations. Those should be considered
6910 duplicates of hw_read locations. The hw_read locations will
6911 become hw_access locations later. */
6912 return (loc1->owner->type == loc2->owner->type
6913 && loc1->pspace->aspace == loc2->pspace->aspace
6914 && loc1->address == loc2->address
6915 && loc1->length == loc2->length);
6916 }
6917
6918 /* See breakpoint.h. */
6919
6920 int
6921 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6922 const address_space *aspace2, CORE_ADDR addr2)
6923 {
6924 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6925 || aspace1 == aspace2)
6926 && addr1 == addr2);
6927 }
6928
6929 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6930 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6931 matches ASPACE2. On targets that have global breakpoints, the address
6932 space doesn't really matter. */
6933
6934 static int
6935 breakpoint_address_match_range (const address_space *aspace1,
6936 CORE_ADDR addr1,
6937 int len1, const address_space *aspace2,
6938 CORE_ADDR addr2)
6939 {
6940 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6941 || aspace1 == aspace2)
6942 && addr2 >= addr1 && addr2 < addr1 + len1);
6943 }
6944
6945 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6946 a ranged breakpoint. In most targets, a match happens only if ASPACE
6947 matches the breakpoint's address space. On targets that have global
6948 breakpoints, the address space doesn't really matter. */
6949
6950 static int
6951 breakpoint_location_address_match (struct bp_location *bl,
6952 const address_space *aspace,
6953 CORE_ADDR addr)
6954 {
6955 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6956 aspace, addr)
6957 || (bl->length
6958 && breakpoint_address_match_range (bl->pspace->aspace,
6959 bl->address, bl->length,
6960 aspace, addr)));
6961 }
6962
6963 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6964 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6965 match happens only if ASPACE matches the breakpoint's address
6966 space. On targets that have global breakpoints, the address space
6967 doesn't really matter. */
6968
6969 static int
6970 breakpoint_location_address_range_overlap (struct bp_location *bl,
6971 const address_space *aspace,
6972 CORE_ADDR addr, int len)
6973 {
6974 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6975 || bl->pspace->aspace == aspace)
6976 {
6977 int bl_len = bl->length != 0 ? bl->length : 1;
6978
6979 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6980 return 1;
6981 }
6982 return 0;
6983 }
6984
6985 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6986 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6987 true, otherwise returns false. */
6988
6989 static int
6990 tracepoint_locations_match (struct bp_location *loc1,
6991 struct bp_location *loc2)
6992 {
6993 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6994 /* Since tracepoint locations are never duplicated with others', tracepoint
6995 locations at the same address of different tracepoints are regarded as
6996 different locations. */
6997 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6998 else
6999 return 0;
7000 }
7001
7002 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7003 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
7004 the same location. If SW_HW_BPS_MATCH is true, then software
7005 breakpoint locations and hardware breakpoint locations match,
7006 otherwise they don't. */
7007
7008 static int
7009 breakpoint_locations_match (struct bp_location *loc1,
7010 struct bp_location *loc2,
7011 bool sw_hw_bps_match)
7012 {
7013 int hw_point1, hw_point2;
7014
7015 /* Both of them must not be in moribund_locations. */
7016 gdb_assert (loc1->owner != NULL);
7017 gdb_assert (loc2->owner != NULL);
7018
7019 hw_point1 = is_hardware_watchpoint (loc1->owner);
7020 hw_point2 = is_hardware_watchpoint (loc2->owner);
7021
7022 if (hw_point1 != hw_point2)
7023 return 0;
7024 else if (hw_point1)
7025 return watchpoint_locations_match (loc1, loc2);
7026 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7027 return tracepoint_locations_match (loc1, loc2);
7028 else
7029 /* We compare bp_location.length in order to cover ranged
7030 breakpoints. Keep this in sync with
7031 bp_location_is_less_than. */
7032 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7033 loc2->pspace->aspace, loc2->address)
7034 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
7035 && loc1->length == loc2->length);
7036 }
7037
7038 static void
7039 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7040 int bnum, int have_bnum)
7041 {
7042 /* The longest string possibly returned by hex_string_custom
7043 is 50 chars. These must be at least that big for safety. */
7044 char astr1[64];
7045 char astr2[64];
7046
7047 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7048 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7049 if (have_bnum)
7050 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7051 bnum, astr1, astr2);
7052 else
7053 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7054 }
7055
7056 /* Adjust a breakpoint's address to account for architectural
7057 constraints on breakpoint placement. Return the adjusted address.
7058 Note: Very few targets require this kind of adjustment. For most
7059 targets, this function is simply the identity function. */
7060
7061 static CORE_ADDR
7062 adjust_breakpoint_address (struct gdbarch *gdbarch,
7063 CORE_ADDR bpaddr, enum bptype bptype)
7064 {
7065 if (bptype == bp_watchpoint
7066 || bptype == bp_hardware_watchpoint
7067 || bptype == bp_read_watchpoint
7068 || bptype == bp_access_watchpoint
7069 || bptype == bp_catchpoint)
7070 {
7071 /* Watchpoints and the various bp_catch_* eventpoints should not
7072 have their addresses modified. */
7073 return bpaddr;
7074 }
7075 else if (bptype == bp_single_step)
7076 {
7077 /* Single-step breakpoints should not have their addresses
7078 modified. If there's any architectural constrain that
7079 applies to this address, then it should have already been
7080 taken into account when the breakpoint was created in the
7081 first place. If we didn't do this, stepping through e.g.,
7082 Thumb-2 IT blocks would break. */
7083 return bpaddr;
7084 }
7085 else
7086 {
7087 CORE_ADDR adjusted_bpaddr = bpaddr;
7088
7089 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7090 {
7091 /* Some targets have architectural constraints on the placement
7092 of breakpoint instructions. Obtain the adjusted address. */
7093 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7094 }
7095
7096 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
7097
7098 /* An adjusted breakpoint address can significantly alter
7099 a user's expectations. Print a warning if an adjustment
7100 is required. */
7101 if (adjusted_bpaddr != bpaddr)
7102 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7103
7104 return adjusted_bpaddr;
7105 }
7106 }
7107
7108 static bp_loc_type
7109 bp_location_from_bp_type (bptype type)
7110 {
7111 switch (type)
7112 {
7113 case bp_breakpoint:
7114 case bp_single_step:
7115 case bp_until:
7116 case bp_finish:
7117 case bp_longjmp:
7118 case bp_longjmp_resume:
7119 case bp_longjmp_call_dummy:
7120 case bp_exception:
7121 case bp_exception_resume:
7122 case bp_step_resume:
7123 case bp_hp_step_resume:
7124 case bp_watchpoint_scope:
7125 case bp_call_dummy:
7126 case bp_std_terminate:
7127 case bp_shlib_event:
7128 case bp_thread_event:
7129 case bp_overlay_event:
7130 case bp_jit_event:
7131 case bp_longjmp_master:
7132 case bp_std_terminate_master:
7133 case bp_exception_master:
7134 case bp_gnu_ifunc_resolver:
7135 case bp_gnu_ifunc_resolver_return:
7136 case bp_dprintf:
7137 return bp_loc_software_breakpoint;
7138 case bp_hardware_breakpoint:
7139 return bp_loc_hardware_breakpoint;
7140 case bp_hardware_watchpoint:
7141 case bp_read_watchpoint:
7142 case bp_access_watchpoint:
7143 return bp_loc_hardware_watchpoint;
7144 case bp_watchpoint:
7145 case bp_catchpoint:
7146 case bp_tracepoint:
7147 case bp_fast_tracepoint:
7148 case bp_static_tracepoint:
7149 return bp_loc_other;
7150 default:
7151 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7152 }
7153 }
7154
7155 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7156 {
7157 this->owner = owner;
7158 this->cond_bytecode = NULL;
7159 this->shlib_disabled = 0;
7160 this->enabled = 1;
7161 this->disabled_by_cond = false;
7162
7163 this->loc_type = type;
7164
7165 if (this->loc_type == bp_loc_software_breakpoint
7166 || this->loc_type == bp_loc_hardware_breakpoint)
7167 mark_breakpoint_location_modified (this);
7168
7169 incref ();
7170 }
7171
7172 bp_location::bp_location (breakpoint *owner)
7173 : bp_location::bp_location (owner,
7174 bp_location_from_bp_type (owner->type))
7175 {
7176 }
7177
7178 /* Allocate a struct bp_location. */
7179
7180 static struct bp_location *
7181 allocate_bp_location (struct breakpoint *bpt)
7182 {
7183 return bpt->ops->allocate_location (bpt);
7184 }
7185
7186 /* Decrement reference count. If the reference count reaches 0,
7187 destroy the bp_location. Sets *BLP to NULL. */
7188
7189 static void
7190 decref_bp_location (struct bp_location **blp)
7191 {
7192 bp_location_ref_policy::decref (*blp);
7193 *blp = NULL;
7194 }
7195
7196 /* Add breakpoint B at the end of the global breakpoint chain. */
7197
7198 static breakpoint *
7199 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7200 {
7201 struct breakpoint *b1;
7202 struct breakpoint *result = b.get ();
7203
7204 /* Add this breakpoint to the end of the chain so that a list of
7205 breakpoints will come out in order of increasing numbers. */
7206
7207 b1 = breakpoint_chain;
7208 if (b1 == 0)
7209 breakpoint_chain = b.release ();
7210 else
7211 {
7212 while (b1->next)
7213 b1 = b1->next;
7214 b1->next = b.release ();
7215 }
7216
7217 return result;
7218 }
7219
7220 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7221
7222 static void
7223 init_raw_breakpoint_without_location (struct breakpoint *b,
7224 struct gdbarch *gdbarch,
7225 enum bptype bptype,
7226 const struct breakpoint_ops *ops)
7227 {
7228 gdb_assert (ops != NULL);
7229
7230 b->ops = ops;
7231 b->type = bptype;
7232 b->gdbarch = gdbarch;
7233 b->language = current_language->la_language;
7234 b->input_radix = input_radix;
7235 b->related_breakpoint = b;
7236 }
7237
7238 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7239 that has type BPTYPE and has no locations as yet. */
7240
7241 static struct breakpoint *
7242 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7243 enum bptype bptype,
7244 const struct breakpoint_ops *ops)
7245 {
7246 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7247
7248 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7249 return add_to_breakpoint_chain (std::move (b));
7250 }
7251
7252 /* Initialize loc->function_name. */
7253
7254 static void
7255 set_breakpoint_location_function (struct bp_location *loc)
7256 {
7257 gdb_assert (loc->owner != NULL);
7258
7259 if (loc->owner->type == bp_breakpoint
7260 || loc->owner->type == bp_hardware_breakpoint
7261 || is_tracepoint (loc->owner))
7262 {
7263 const char *function_name;
7264
7265 if (loc->msymbol != NULL
7266 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
7267 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
7268 {
7269 struct breakpoint *b = loc->owner;
7270
7271 function_name = loc->msymbol->linkage_name ();
7272
7273 if (b->type == bp_breakpoint && b->loc == loc
7274 && loc->next == NULL && b->related_breakpoint == b)
7275 {
7276 /* Create only the whole new breakpoint of this type but do not
7277 mess more complicated breakpoints with multiple locations. */
7278 b->type = bp_gnu_ifunc_resolver;
7279 /* Remember the resolver's address for use by the return
7280 breakpoint. */
7281 loc->related_address = loc->address;
7282 }
7283 }
7284 else
7285 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7286
7287 if (function_name)
7288 loc->function_name = xstrdup (function_name);
7289 }
7290 }
7291
7292 /* Attempt to determine architecture of location identified by SAL. */
7293 struct gdbarch *
7294 get_sal_arch (struct symtab_and_line sal)
7295 {
7296 if (sal.section)
7297 return sal.section->objfile->arch ();
7298 if (sal.symtab)
7299 return SYMTAB_OBJFILE (sal.symtab)->arch ();
7300
7301 return NULL;
7302 }
7303
7304 /* Low level routine for partially initializing a breakpoint of type
7305 BPTYPE. The newly created breakpoint's address, section, source
7306 file name, and line number are provided by SAL.
7307
7308 It is expected that the caller will complete the initialization of
7309 the newly created breakpoint struct as well as output any status
7310 information regarding the creation of a new breakpoint. */
7311
7312 static void
7313 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7314 struct symtab_and_line sal, enum bptype bptype,
7315 const struct breakpoint_ops *ops)
7316 {
7317 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7318
7319 add_location_to_breakpoint (b, &sal);
7320
7321 if (bptype != bp_catchpoint)
7322 gdb_assert (sal.pspace != NULL);
7323
7324 /* Store the program space that was used to set the breakpoint,
7325 except for ordinary breakpoints, which are independent of the
7326 program space. */
7327 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7328 b->pspace = sal.pspace;
7329 }
7330
7331 /* set_raw_breakpoint is a low level routine for allocating and
7332 partially initializing a breakpoint of type BPTYPE. The newly
7333 created breakpoint's address, section, source file name, and line
7334 number are provided by SAL. The newly created and partially
7335 initialized breakpoint is added to the breakpoint chain and
7336 is also returned as the value of this function.
7337
7338 It is expected that the caller will complete the initialization of
7339 the newly created breakpoint struct as well as output any status
7340 information regarding the creation of a new breakpoint. In
7341 particular, set_raw_breakpoint does NOT set the breakpoint
7342 number! Care should be taken to not allow an error to occur
7343 prior to completing the initialization of the breakpoint. If this
7344 should happen, a bogus breakpoint will be left on the chain. */
7345
7346 struct breakpoint *
7347 set_raw_breakpoint (struct gdbarch *gdbarch,
7348 struct symtab_and_line sal, enum bptype bptype,
7349 const struct breakpoint_ops *ops)
7350 {
7351 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7352
7353 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7354 return add_to_breakpoint_chain (std::move (b));
7355 }
7356
7357 /* Call this routine when stepping and nexting to enable a breakpoint
7358 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7359 initiated the operation. */
7360
7361 void
7362 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7363 {
7364 int thread = tp->global_num;
7365
7366 /* To avoid having to rescan all objfile symbols at every step,
7367 we maintain a list of continually-inserted but always disabled
7368 longjmp "master" breakpoints. Here, we simply create momentary
7369 clones of those and enable them for the requested thread. */
7370 for (breakpoint *b : all_breakpoints_safe ())
7371 if (b->pspace == current_program_space
7372 && (b->type == bp_longjmp_master
7373 || b->type == bp_exception_master))
7374 {
7375 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7376 struct breakpoint *clone;
7377
7378 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7379 after their removal. */
7380 clone = momentary_breakpoint_from_master (b, type,
7381 &momentary_breakpoint_ops, 1);
7382 clone->thread = thread;
7383 }
7384
7385 tp->initiating_frame = frame;
7386 }
7387
7388 /* Delete all longjmp breakpoints from THREAD. */
7389 void
7390 delete_longjmp_breakpoint (int thread)
7391 {
7392 for (breakpoint *b : all_breakpoints_safe ())
7393 if (b->type == bp_longjmp || b->type == bp_exception)
7394 {
7395 if (b->thread == thread)
7396 delete_breakpoint (b);
7397 }
7398 }
7399
7400 void
7401 delete_longjmp_breakpoint_at_next_stop (int thread)
7402 {
7403 for (breakpoint *b : all_breakpoints_safe ())
7404 if (b->type == bp_longjmp || b->type == bp_exception)
7405 {
7406 if (b->thread == thread)
7407 b->disposition = disp_del_at_next_stop;
7408 }
7409 }
7410
7411 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7412 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7413 pointer to any of them. Return NULL if this system cannot place longjmp
7414 breakpoints. */
7415
7416 struct breakpoint *
7417 set_longjmp_breakpoint_for_call_dummy (void)
7418 {
7419 breakpoint *retval = nullptr;
7420
7421 for (breakpoint *b : all_breakpoints ())
7422 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7423 {
7424 struct breakpoint *new_b;
7425
7426 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7427 &momentary_breakpoint_ops,
7428 1);
7429 new_b->thread = inferior_thread ()->global_num;
7430
7431 /* Link NEW_B into the chain of RETVAL breakpoints. */
7432
7433 gdb_assert (new_b->related_breakpoint == new_b);
7434 if (retval == NULL)
7435 retval = new_b;
7436 new_b->related_breakpoint = retval;
7437 while (retval->related_breakpoint != new_b->related_breakpoint)
7438 retval = retval->related_breakpoint;
7439 retval->related_breakpoint = new_b;
7440 }
7441
7442 return retval;
7443 }
7444
7445 /* Verify all existing dummy frames and their associated breakpoints for
7446 TP. Remove those which can no longer be found in the current frame
7447 stack.
7448
7449 You should call this function only at places where it is safe to currently
7450 unwind the whole stack. Failed stack unwind would discard live dummy
7451 frames. */
7452
7453 void
7454 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7455 {
7456 struct breakpoint *b, *b_tmp;
7457
7458 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7459 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7460 {
7461 struct breakpoint *dummy_b = b->related_breakpoint;
7462
7463 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7464 dummy_b = dummy_b->related_breakpoint;
7465 if (dummy_b->type != bp_call_dummy
7466 || frame_find_by_id (dummy_b->frame_id) != NULL)
7467 continue;
7468
7469 dummy_frame_discard (dummy_b->frame_id, tp);
7470
7471 while (b->related_breakpoint != b)
7472 {
7473 if (b_tmp == b->related_breakpoint)
7474 b_tmp = b->related_breakpoint->next;
7475 delete_breakpoint (b->related_breakpoint);
7476 }
7477 delete_breakpoint (b);
7478 }
7479 }
7480
7481 void
7482 enable_overlay_breakpoints (void)
7483 {
7484 for (breakpoint *b : all_breakpoints ())
7485 if (b->type == bp_overlay_event)
7486 {
7487 b->enable_state = bp_enabled;
7488 update_global_location_list (UGLL_MAY_INSERT);
7489 overlay_events_enabled = 1;
7490 }
7491 }
7492
7493 void
7494 disable_overlay_breakpoints (void)
7495 {
7496 for (breakpoint *b : all_breakpoints ())
7497 if (b->type == bp_overlay_event)
7498 {
7499 b->enable_state = bp_disabled;
7500 update_global_location_list (UGLL_DONT_INSERT);
7501 overlay_events_enabled = 0;
7502 }
7503 }
7504
7505 /* Set an active std::terminate breakpoint for each std::terminate
7506 master breakpoint. */
7507 void
7508 set_std_terminate_breakpoint (void)
7509 {
7510 for (breakpoint *b : all_breakpoints_safe ())
7511 if (b->pspace == current_program_space
7512 && b->type == bp_std_terminate_master)
7513 {
7514 momentary_breakpoint_from_master (b, bp_std_terminate,
7515 &momentary_breakpoint_ops, 1);
7516 }
7517 }
7518
7519 /* Delete all the std::terminate breakpoints. */
7520 void
7521 delete_std_terminate_breakpoint (void)
7522 {
7523 for (breakpoint *b : all_breakpoints_safe ())
7524 if (b->type == bp_std_terminate)
7525 delete_breakpoint (b);
7526 }
7527
7528 struct breakpoint *
7529 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7530 {
7531 struct breakpoint *b;
7532
7533 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7534 &internal_breakpoint_ops);
7535
7536 b->enable_state = bp_enabled;
7537 /* location has to be used or breakpoint_re_set will delete me. */
7538 b->location = new_address_location (b->loc->address, NULL, 0);
7539
7540 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7541
7542 return b;
7543 }
7544
7545 struct lang_and_radix
7546 {
7547 enum language lang;
7548 int radix;
7549 };
7550
7551 /* Create a breakpoint for JIT code registration and unregistration. */
7552
7553 struct breakpoint *
7554 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7555 {
7556 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7557 &internal_breakpoint_ops);
7558 }
7559
7560 /* Remove JIT code registration and unregistration breakpoint(s). */
7561
7562 void
7563 remove_jit_event_breakpoints (void)
7564 {
7565 for (breakpoint *b : all_breakpoints_safe ())
7566 if (b->type == bp_jit_event
7567 && b->loc->pspace == current_program_space)
7568 delete_breakpoint (b);
7569 }
7570
7571 void
7572 remove_solib_event_breakpoints (void)
7573 {
7574 for (breakpoint *b : all_breakpoints_safe ())
7575 if (b->type == bp_shlib_event
7576 && b->loc->pspace == current_program_space)
7577 delete_breakpoint (b);
7578 }
7579
7580 /* See breakpoint.h. */
7581
7582 void
7583 remove_solib_event_breakpoints_at_next_stop (void)
7584 {
7585 for (breakpoint *b : all_breakpoints_safe ())
7586 if (b->type == bp_shlib_event
7587 && b->loc->pspace == current_program_space)
7588 b->disposition = disp_del_at_next_stop;
7589 }
7590
7591 /* Helper for create_solib_event_breakpoint /
7592 create_and_insert_solib_event_breakpoint. Allows specifying which
7593 INSERT_MODE to pass through to update_global_location_list. */
7594
7595 static struct breakpoint *
7596 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7597 enum ugll_insert_mode insert_mode)
7598 {
7599 struct breakpoint *b;
7600
7601 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7602 &internal_breakpoint_ops);
7603 update_global_location_list_nothrow (insert_mode);
7604 return b;
7605 }
7606
7607 struct breakpoint *
7608 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7609 {
7610 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7611 }
7612
7613 /* See breakpoint.h. */
7614
7615 struct breakpoint *
7616 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7617 {
7618 struct breakpoint *b;
7619
7620 /* Explicitly tell update_global_location_list to insert
7621 locations. */
7622 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7623 if (!b->loc->inserted)
7624 {
7625 delete_breakpoint (b);
7626 return NULL;
7627 }
7628 return b;
7629 }
7630
7631 /* Disable any breakpoints that are on code in shared libraries. Only
7632 apply to enabled breakpoints, disabled ones can just stay disabled. */
7633
7634 void
7635 disable_breakpoints_in_shlibs (void)
7636 {
7637 struct bp_location *loc, **locp_tmp;
7638
7639 ALL_BP_LOCATIONS (loc, locp_tmp)
7640 {
7641 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7642 struct breakpoint *b = loc->owner;
7643
7644 /* We apply the check to all breakpoints, including disabled for
7645 those with loc->duplicate set. This is so that when breakpoint
7646 becomes enabled, or the duplicate is removed, gdb will try to
7647 insert all breakpoints. If we don't set shlib_disabled here,
7648 we'll try to insert those breakpoints and fail. */
7649 if (((b->type == bp_breakpoint)
7650 || (b->type == bp_jit_event)
7651 || (b->type == bp_hardware_breakpoint)
7652 || (is_tracepoint (b)))
7653 && loc->pspace == current_program_space
7654 && !loc->shlib_disabled
7655 && solib_name_from_address (loc->pspace, loc->address)
7656 )
7657 {
7658 loc->shlib_disabled = 1;
7659 }
7660 }
7661 }
7662
7663 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7664 notification of unloaded_shlib. Only apply to enabled breakpoints,
7665 disabled ones can just stay disabled. */
7666
7667 static void
7668 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7669 {
7670 struct bp_location *loc, **locp_tmp;
7671 int disabled_shlib_breaks = 0;
7672
7673 ALL_BP_LOCATIONS (loc, locp_tmp)
7674 {
7675 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7676 struct breakpoint *b = loc->owner;
7677
7678 if (solib->pspace == loc->pspace
7679 && !loc->shlib_disabled
7680 && (((b->type == bp_breakpoint
7681 || b->type == bp_jit_event
7682 || b->type == bp_hardware_breakpoint)
7683 && (loc->loc_type == bp_loc_hardware_breakpoint
7684 || loc->loc_type == bp_loc_software_breakpoint))
7685 || is_tracepoint (b))
7686 && solib_contains_address_p (solib, loc->address))
7687 {
7688 loc->shlib_disabled = 1;
7689 /* At this point, we cannot rely on remove_breakpoint
7690 succeeding so we must mark the breakpoint as not inserted
7691 to prevent future errors occurring in remove_breakpoints. */
7692 loc->inserted = 0;
7693
7694 /* This may cause duplicate notifications for the same breakpoint. */
7695 gdb::observers::breakpoint_modified.notify (b);
7696
7697 if (!disabled_shlib_breaks)
7698 {
7699 target_terminal::ours_for_output ();
7700 warning (_("Temporarily disabling breakpoints "
7701 "for unloaded shared library \"%s\""),
7702 solib->so_name);
7703 }
7704 disabled_shlib_breaks = 1;
7705 }
7706 }
7707 }
7708
7709 /* Disable any breakpoints and tracepoints in OBJFILE upon
7710 notification of free_objfile. Only apply to enabled breakpoints,
7711 disabled ones can just stay disabled. */
7712
7713 static void
7714 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7715 {
7716 if (objfile == NULL)
7717 return;
7718
7719 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7720 managed by the user with add-symbol-file/remove-symbol-file.
7721 Similarly to how breakpoints in shared libraries are handled in
7722 response to "nosharedlibrary", mark breakpoints in such modules
7723 shlib_disabled so they end up uninserted on the next global
7724 location list update. Shared libraries not loaded by the user
7725 aren't handled here -- they're already handled in
7726 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7727 solib_unloaded observer. We skip objfiles that are not
7728 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7729 main objfile). */
7730 if ((objfile->flags & OBJF_SHARED) == 0
7731 || (objfile->flags & OBJF_USERLOADED) == 0)
7732 return;
7733
7734 for (breakpoint *b : all_breakpoints ())
7735 {
7736 struct bp_location *loc;
7737 int bp_modified = 0;
7738
7739 if (!is_breakpoint (b) && !is_tracepoint (b))
7740 continue;
7741
7742 for (loc = b->loc; loc != NULL; loc = loc->next)
7743 {
7744 CORE_ADDR loc_addr = loc->address;
7745
7746 if (loc->loc_type != bp_loc_hardware_breakpoint
7747 && loc->loc_type != bp_loc_software_breakpoint)
7748 continue;
7749
7750 if (loc->shlib_disabled != 0)
7751 continue;
7752
7753 if (objfile->pspace != loc->pspace)
7754 continue;
7755
7756 if (loc->loc_type != bp_loc_hardware_breakpoint
7757 && loc->loc_type != bp_loc_software_breakpoint)
7758 continue;
7759
7760 if (is_addr_in_objfile (loc_addr, objfile))
7761 {
7762 loc->shlib_disabled = 1;
7763 /* At this point, we don't know whether the object was
7764 unmapped from the inferior or not, so leave the
7765 inserted flag alone. We'll handle failure to
7766 uninsert quietly, in case the object was indeed
7767 unmapped. */
7768
7769 mark_breakpoint_location_modified (loc);
7770
7771 bp_modified = 1;
7772 }
7773 }
7774
7775 if (bp_modified)
7776 gdb::observers::breakpoint_modified.notify (b);
7777 }
7778 }
7779
7780 /* FORK & VFORK catchpoints. */
7781
7782 /* An instance of this type is used to represent a fork or vfork
7783 catchpoint. A breakpoint is really of this type iff its ops pointer points
7784 to CATCH_FORK_BREAKPOINT_OPS. */
7785
7786 struct fork_catchpoint : public breakpoint
7787 {
7788 /* Process id of a child process whose forking triggered this
7789 catchpoint. This field is only valid immediately after this
7790 catchpoint has triggered. */
7791 ptid_t forked_inferior_pid;
7792 };
7793
7794 /* Implement the "insert" breakpoint_ops method for fork
7795 catchpoints. */
7796
7797 static int
7798 insert_catch_fork (struct bp_location *bl)
7799 {
7800 return target_insert_fork_catchpoint (inferior_ptid.pid ());
7801 }
7802
7803 /* Implement the "remove" breakpoint_ops method for fork
7804 catchpoints. */
7805
7806 static int
7807 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
7808 {
7809 return target_remove_fork_catchpoint (inferior_ptid.pid ());
7810 }
7811
7812 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7813 catchpoints. */
7814
7815 static int
7816 breakpoint_hit_catch_fork (const struct bp_location *bl,
7817 const address_space *aspace, CORE_ADDR bp_addr,
7818 const struct target_waitstatus *ws)
7819 {
7820 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7821
7822 if (ws->kind != TARGET_WAITKIND_FORKED)
7823 return 0;
7824
7825 c->forked_inferior_pid = ws->value.related_pid;
7826 return 1;
7827 }
7828
7829 /* Implement the "print_it" breakpoint_ops method for fork
7830 catchpoints. */
7831
7832 static enum print_stop_action
7833 print_it_catch_fork (bpstat bs)
7834 {
7835 struct ui_out *uiout = current_uiout;
7836 struct breakpoint *b = bs->breakpoint_at;
7837 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7838
7839 annotate_catchpoint (b->number);
7840 maybe_print_thread_hit_breakpoint (uiout);
7841 if (b->disposition == disp_del)
7842 uiout->text ("Temporary catchpoint ");
7843 else
7844 uiout->text ("Catchpoint ");
7845 if (uiout->is_mi_like_p ())
7846 {
7847 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7848 uiout->field_string ("disp", bpdisp_text (b->disposition));
7849 }
7850 uiout->field_signed ("bkptno", b->number);
7851 uiout->text (" (forked process ");
7852 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7853 uiout->text ("), ");
7854 return PRINT_SRC_AND_LOC;
7855 }
7856
7857 /* Implement the "print_one" breakpoint_ops method for fork
7858 catchpoints. */
7859
7860 static void
7861 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7862 {
7863 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7864 struct value_print_options opts;
7865 struct ui_out *uiout = current_uiout;
7866
7867 get_user_print_options (&opts);
7868
7869 /* Field 4, the address, is omitted (which makes the columns not
7870 line up too nicely with the headers, but the effect is relatively
7871 readable). */
7872 if (opts.addressprint)
7873 uiout->field_skip ("addr");
7874 annotate_field (5);
7875 uiout->text ("fork");
7876 if (c->forked_inferior_pid != null_ptid)
7877 {
7878 uiout->text (", process ");
7879 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7880 uiout->spaces (1);
7881 }
7882
7883 if (uiout->is_mi_like_p ())
7884 uiout->field_string ("catch-type", "fork");
7885 }
7886
7887 /* Implement the "print_mention" breakpoint_ops method for fork
7888 catchpoints. */
7889
7890 static void
7891 print_mention_catch_fork (struct breakpoint *b)
7892 {
7893 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7894 }
7895
7896 /* Implement the "print_recreate" breakpoint_ops method for fork
7897 catchpoints. */
7898
7899 static void
7900 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7901 {
7902 fprintf_unfiltered (fp, "catch fork");
7903 print_recreate_thread (b, fp);
7904 }
7905
7906 /* The breakpoint_ops structure to be used in fork catchpoints. */
7907
7908 static struct breakpoint_ops catch_fork_breakpoint_ops;
7909
7910 /* Implement the "insert" breakpoint_ops method for vfork
7911 catchpoints. */
7912
7913 static int
7914 insert_catch_vfork (struct bp_location *bl)
7915 {
7916 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
7917 }
7918
7919 /* Implement the "remove" breakpoint_ops method for vfork
7920 catchpoints. */
7921
7922 static int
7923 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
7924 {
7925 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
7926 }
7927
7928 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7929 catchpoints. */
7930
7931 static int
7932 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7933 const address_space *aspace, CORE_ADDR bp_addr,
7934 const struct target_waitstatus *ws)
7935 {
7936 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7937
7938 if (ws->kind != TARGET_WAITKIND_VFORKED)
7939 return 0;
7940
7941 c->forked_inferior_pid = ws->value.related_pid;
7942 return 1;
7943 }
7944
7945 /* Implement the "print_it" breakpoint_ops method for vfork
7946 catchpoints. */
7947
7948 static enum print_stop_action
7949 print_it_catch_vfork (bpstat bs)
7950 {
7951 struct ui_out *uiout = current_uiout;
7952 struct breakpoint *b = bs->breakpoint_at;
7953 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7954
7955 annotate_catchpoint (b->number);
7956 maybe_print_thread_hit_breakpoint (uiout);
7957 if (b->disposition == disp_del)
7958 uiout->text ("Temporary catchpoint ");
7959 else
7960 uiout->text ("Catchpoint ");
7961 if (uiout->is_mi_like_p ())
7962 {
7963 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7964 uiout->field_string ("disp", bpdisp_text (b->disposition));
7965 }
7966 uiout->field_signed ("bkptno", b->number);
7967 uiout->text (" (vforked process ");
7968 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7969 uiout->text ("), ");
7970 return PRINT_SRC_AND_LOC;
7971 }
7972
7973 /* Implement the "print_one" breakpoint_ops method for vfork
7974 catchpoints. */
7975
7976 static void
7977 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7978 {
7979 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7980 struct value_print_options opts;
7981 struct ui_out *uiout = current_uiout;
7982
7983 get_user_print_options (&opts);
7984 /* Field 4, the address, is omitted (which makes the columns not
7985 line up too nicely with the headers, but the effect is relatively
7986 readable). */
7987 if (opts.addressprint)
7988 uiout->field_skip ("addr");
7989 annotate_field (5);
7990 uiout->text ("vfork");
7991 if (c->forked_inferior_pid != null_ptid)
7992 {
7993 uiout->text (", process ");
7994 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7995 uiout->spaces (1);
7996 }
7997
7998 if (uiout->is_mi_like_p ())
7999 uiout->field_string ("catch-type", "vfork");
8000 }
8001
8002 /* Implement the "print_mention" breakpoint_ops method for vfork
8003 catchpoints. */
8004
8005 static void
8006 print_mention_catch_vfork (struct breakpoint *b)
8007 {
8008 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8009 }
8010
8011 /* Implement the "print_recreate" breakpoint_ops method for vfork
8012 catchpoints. */
8013
8014 static void
8015 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8016 {
8017 fprintf_unfiltered (fp, "catch vfork");
8018 print_recreate_thread (b, fp);
8019 }
8020
8021 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8022
8023 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8024
8025 /* An instance of this type is used to represent an solib catchpoint.
8026 A breakpoint is really of this type iff its ops pointer points to
8027 CATCH_SOLIB_BREAKPOINT_OPS. */
8028
8029 struct solib_catchpoint : public breakpoint
8030 {
8031 ~solib_catchpoint () override;
8032
8033 /* True for "catch load", false for "catch unload". */
8034 bool is_load;
8035
8036 /* Regular expression to match, if any. COMPILED is only valid when
8037 REGEX is non-NULL. */
8038 char *regex;
8039 std::unique_ptr<compiled_regex> compiled;
8040 };
8041
8042 solib_catchpoint::~solib_catchpoint ()
8043 {
8044 xfree (this->regex);
8045 }
8046
8047 static int
8048 insert_catch_solib (struct bp_location *ignore)
8049 {
8050 return 0;
8051 }
8052
8053 static int
8054 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
8055 {
8056 return 0;
8057 }
8058
8059 static int
8060 breakpoint_hit_catch_solib (const struct bp_location *bl,
8061 const address_space *aspace,
8062 CORE_ADDR bp_addr,
8063 const struct target_waitstatus *ws)
8064 {
8065 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8066
8067 if (ws->kind == TARGET_WAITKIND_LOADED)
8068 return 1;
8069
8070 for (breakpoint *other : all_breakpoints ())
8071 {
8072 struct bp_location *other_bl;
8073
8074 if (other == bl->owner)
8075 continue;
8076
8077 if (other->type != bp_shlib_event)
8078 continue;
8079
8080 if (self->pspace != NULL && other->pspace != self->pspace)
8081 continue;
8082
8083 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8084 {
8085 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8086 return 1;
8087 }
8088 }
8089
8090 return 0;
8091 }
8092
8093 static void
8094 check_status_catch_solib (struct bpstats *bs)
8095 {
8096 struct solib_catchpoint *self
8097 = (struct solib_catchpoint *) bs->breakpoint_at;
8098
8099 if (self->is_load)
8100 {
8101 for (so_list *iter : current_program_space->added_solibs)
8102 {
8103 if (!self->regex
8104 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
8105 return;
8106 }
8107 }
8108 else
8109 {
8110 for (const std::string &iter : current_program_space->deleted_solibs)
8111 {
8112 if (!self->regex
8113 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
8114 return;
8115 }
8116 }
8117
8118 bs->stop = 0;
8119 bs->print_it = print_it_noop;
8120 }
8121
8122 static enum print_stop_action
8123 print_it_catch_solib (bpstat bs)
8124 {
8125 struct breakpoint *b = bs->breakpoint_at;
8126 struct ui_out *uiout = current_uiout;
8127
8128 annotate_catchpoint (b->number);
8129 maybe_print_thread_hit_breakpoint (uiout);
8130 if (b->disposition == disp_del)
8131 uiout->text ("Temporary catchpoint ");
8132 else
8133 uiout->text ("Catchpoint ");
8134 uiout->field_signed ("bkptno", b->number);
8135 uiout->text ("\n");
8136 if (uiout->is_mi_like_p ())
8137 uiout->field_string ("disp", bpdisp_text (b->disposition));
8138 print_solib_event (1);
8139 return PRINT_SRC_AND_LOC;
8140 }
8141
8142 static void
8143 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8144 {
8145 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8146 struct value_print_options opts;
8147 struct ui_out *uiout = current_uiout;
8148
8149 get_user_print_options (&opts);
8150 /* Field 4, the address, is omitted (which makes the columns not
8151 line up too nicely with the headers, but the effect is relatively
8152 readable). */
8153 if (opts.addressprint)
8154 {
8155 annotate_field (4);
8156 uiout->field_skip ("addr");
8157 }
8158
8159 std::string msg;
8160 annotate_field (5);
8161 if (self->is_load)
8162 {
8163 if (self->regex)
8164 msg = string_printf (_("load of library matching %s"), self->regex);
8165 else
8166 msg = _("load of library");
8167 }
8168 else
8169 {
8170 if (self->regex)
8171 msg = string_printf (_("unload of library matching %s"), self->regex);
8172 else
8173 msg = _("unload of library");
8174 }
8175 uiout->field_string ("what", msg);
8176
8177 if (uiout->is_mi_like_p ())
8178 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8179 }
8180
8181 static void
8182 print_mention_catch_solib (struct breakpoint *b)
8183 {
8184 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8185
8186 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8187 self->is_load ? "load" : "unload");
8188 }
8189
8190 static void
8191 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8192 {
8193 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8194
8195 fprintf_unfiltered (fp, "%s %s",
8196 b->disposition == disp_del ? "tcatch" : "catch",
8197 self->is_load ? "load" : "unload");
8198 if (self->regex)
8199 fprintf_unfiltered (fp, " %s", self->regex);
8200 fprintf_unfiltered (fp, "\n");
8201 }
8202
8203 static struct breakpoint_ops catch_solib_breakpoint_ops;
8204
8205 /* See breakpoint.h. */
8206
8207 void
8208 add_solib_catchpoint (const char *arg, bool is_load, bool is_temp, bool enabled)
8209 {
8210 struct gdbarch *gdbarch = get_current_arch ();
8211
8212 if (!arg)
8213 arg = "";
8214 arg = skip_spaces (arg);
8215
8216 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
8217
8218 if (*arg != '\0')
8219 {
8220 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8221 _("Invalid regexp")));
8222 c->regex = xstrdup (arg);
8223 }
8224
8225 c->is_load = is_load;
8226 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
8227 &catch_solib_breakpoint_ops);
8228
8229 c->enable_state = enabled ? bp_enabled : bp_disabled;
8230
8231 install_breakpoint (0, std::move (c), 1);
8232 }
8233
8234 /* A helper function that does all the work for "catch load" and
8235 "catch unload". */
8236
8237 static void
8238 catch_load_or_unload (const char *arg, int from_tty, int is_load,
8239 struct cmd_list_element *command)
8240 {
8241 const int enabled = 1;
8242 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
8243
8244 add_solib_catchpoint (arg, is_load, temp, enabled);
8245 }
8246
8247 static void
8248 catch_load_command_1 (const char *arg, int from_tty,
8249 struct cmd_list_element *command)
8250 {
8251 catch_load_or_unload (arg, from_tty, 1, command);
8252 }
8253
8254 static void
8255 catch_unload_command_1 (const char *arg, int from_tty,
8256 struct cmd_list_element *command)
8257 {
8258 catch_load_or_unload (arg, from_tty, 0, command);
8259 }
8260
8261 /* See breakpoint.h. */
8262
8263 void
8264 init_catchpoint (struct breakpoint *b,
8265 struct gdbarch *gdbarch, bool temp,
8266 const char *cond_string,
8267 const struct breakpoint_ops *ops)
8268 {
8269 symtab_and_line sal;
8270 sal.pspace = current_program_space;
8271
8272 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8273
8274 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8275 b->disposition = temp ? disp_del : disp_donttouch;
8276 }
8277
8278 void
8279 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8280 {
8281 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8282 set_breakpoint_number (internal, b);
8283 if (is_tracepoint (b))
8284 set_tracepoint_count (breakpoint_count);
8285 if (!internal)
8286 mention (b);
8287 gdb::observers::breakpoint_created.notify (b);
8288
8289 if (update_gll)
8290 update_global_location_list (UGLL_MAY_INSERT);
8291 }
8292
8293 static void
8294 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8295 bool temp, const char *cond_string,
8296 const struct breakpoint_ops *ops)
8297 {
8298 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
8299
8300 init_catchpoint (c.get (), gdbarch, temp, cond_string, ops);
8301
8302 c->forked_inferior_pid = null_ptid;
8303
8304 install_breakpoint (0, std::move (c), 1);
8305 }
8306
8307 /* Exec catchpoints. */
8308
8309 /* An instance of this type is used to represent an exec catchpoint.
8310 A breakpoint is really of this type iff its ops pointer points to
8311 CATCH_EXEC_BREAKPOINT_OPS. */
8312
8313 struct exec_catchpoint : public breakpoint
8314 {
8315 ~exec_catchpoint () override;
8316
8317 /* Filename of a program whose exec triggered this catchpoint.
8318 This field is only valid immediately after this catchpoint has
8319 triggered. */
8320 char *exec_pathname;
8321 };
8322
8323 /* Exec catchpoint destructor. */
8324
8325 exec_catchpoint::~exec_catchpoint ()
8326 {
8327 xfree (this->exec_pathname);
8328 }
8329
8330 static int
8331 insert_catch_exec (struct bp_location *bl)
8332 {
8333 return target_insert_exec_catchpoint (inferior_ptid.pid ());
8334 }
8335
8336 static int
8337 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8338 {
8339 return target_remove_exec_catchpoint (inferior_ptid.pid ());
8340 }
8341
8342 static int
8343 breakpoint_hit_catch_exec (const struct bp_location *bl,
8344 const address_space *aspace, CORE_ADDR bp_addr,
8345 const struct target_waitstatus *ws)
8346 {
8347 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8348
8349 if (ws->kind != TARGET_WAITKIND_EXECD)
8350 return 0;
8351
8352 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8353 return 1;
8354 }
8355
8356 static enum print_stop_action
8357 print_it_catch_exec (bpstat bs)
8358 {
8359 struct ui_out *uiout = current_uiout;
8360 struct breakpoint *b = bs->breakpoint_at;
8361 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8362
8363 annotate_catchpoint (b->number);
8364 maybe_print_thread_hit_breakpoint (uiout);
8365 if (b->disposition == disp_del)
8366 uiout->text ("Temporary catchpoint ");
8367 else
8368 uiout->text ("Catchpoint ");
8369 if (uiout->is_mi_like_p ())
8370 {
8371 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8372 uiout->field_string ("disp", bpdisp_text (b->disposition));
8373 }
8374 uiout->field_signed ("bkptno", b->number);
8375 uiout->text (" (exec'd ");
8376 uiout->field_string ("new-exec", c->exec_pathname);
8377 uiout->text ("), ");
8378
8379 return PRINT_SRC_AND_LOC;
8380 }
8381
8382 static void
8383 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8384 {
8385 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8386 struct value_print_options opts;
8387 struct ui_out *uiout = current_uiout;
8388
8389 get_user_print_options (&opts);
8390
8391 /* Field 4, the address, is omitted (which makes the columns
8392 not line up too nicely with the headers, but the effect
8393 is relatively readable). */
8394 if (opts.addressprint)
8395 uiout->field_skip ("addr");
8396 annotate_field (5);
8397 uiout->text ("exec");
8398 if (c->exec_pathname != NULL)
8399 {
8400 uiout->text (", program \"");
8401 uiout->field_string ("what", c->exec_pathname);
8402 uiout->text ("\" ");
8403 }
8404
8405 if (uiout->is_mi_like_p ())
8406 uiout->field_string ("catch-type", "exec");
8407 }
8408
8409 static void
8410 print_mention_catch_exec (struct breakpoint *b)
8411 {
8412 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8413 }
8414
8415 /* Implement the "print_recreate" breakpoint_ops method for exec
8416 catchpoints. */
8417
8418 static void
8419 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8420 {
8421 fprintf_unfiltered (fp, "catch exec");
8422 print_recreate_thread (b, fp);
8423 }
8424
8425 static struct breakpoint_ops catch_exec_breakpoint_ops;
8426
8427 static int
8428 hw_breakpoint_used_count (void)
8429 {
8430 int i = 0;
8431 struct bp_location *bl;
8432
8433 for (breakpoint *b : all_breakpoints ())
8434 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8435 for (bl = b->loc; bl; bl = bl->next)
8436 {
8437 /* Special types of hardware breakpoints may use more than
8438 one register. */
8439 i += b->ops->resources_needed (bl);
8440 }
8441
8442 return i;
8443 }
8444
8445 /* Returns the resources B would use if it were a hardware
8446 watchpoint. */
8447
8448 static int
8449 hw_watchpoint_use_count (struct breakpoint *b)
8450 {
8451 int i = 0;
8452 struct bp_location *bl;
8453
8454 if (!breakpoint_enabled (b))
8455 return 0;
8456
8457 for (bl = b->loc; bl; bl = bl->next)
8458 {
8459 /* Special types of hardware watchpoints may use more than
8460 one register. */
8461 i += b->ops->resources_needed (bl);
8462 }
8463
8464 return i;
8465 }
8466
8467 /* Returns the sum the used resources of all hardware watchpoints of
8468 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8469 the sum of the used resources of all hardware watchpoints of other
8470 types _not_ TYPE. */
8471
8472 static int
8473 hw_watchpoint_used_count_others (struct breakpoint *except,
8474 enum bptype type, int *other_type_used)
8475 {
8476 int i = 0;
8477
8478 *other_type_used = 0;
8479 for (breakpoint *b : all_breakpoints ())
8480 {
8481 if (b == except)
8482 continue;
8483 if (!breakpoint_enabled (b))
8484 continue;
8485
8486 if (b->type == type)
8487 i += hw_watchpoint_use_count (b);
8488 else if (is_hardware_watchpoint (b))
8489 *other_type_used = 1;
8490 }
8491
8492 return i;
8493 }
8494
8495 void
8496 disable_watchpoints_before_interactive_call_start (void)
8497 {
8498 for (breakpoint *b : all_breakpoints ())
8499 if (is_watchpoint (b) && breakpoint_enabled (b))
8500 {
8501 b->enable_state = bp_call_disabled;
8502 update_global_location_list (UGLL_DONT_INSERT);
8503 }
8504 }
8505
8506 void
8507 enable_watchpoints_after_interactive_call_stop (void)
8508 {
8509 for (breakpoint *b : all_breakpoints ())
8510 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8511 {
8512 b->enable_state = bp_enabled;
8513 update_global_location_list (UGLL_MAY_INSERT);
8514 }
8515 }
8516
8517 void
8518 disable_breakpoints_before_startup (void)
8519 {
8520 current_program_space->executing_startup = 1;
8521 update_global_location_list (UGLL_DONT_INSERT);
8522 }
8523
8524 void
8525 enable_breakpoints_after_startup (void)
8526 {
8527 current_program_space->executing_startup = 0;
8528 breakpoint_re_set ();
8529 }
8530
8531 /* Create a new single-step breakpoint for thread THREAD, with no
8532 locations. */
8533
8534 static struct breakpoint *
8535 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8536 {
8537 std::unique_ptr<breakpoint> b (new breakpoint ());
8538
8539 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
8540 &momentary_breakpoint_ops);
8541
8542 b->disposition = disp_donttouch;
8543 b->frame_id = null_frame_id;
8544
8545 b->thread = thread;
8546 gdb_assert (b->thread != 0);
8547
8548 return add_to_breakpoint_chain (std::move (b));
8549 }
8550
8551 /* Set a momentary breakpoint of type TYPE at address specified by
8552 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8553 frame. */
8554
8555 breakpoint_up
8556 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8557 struct frame_id frame_id, enum bptype type)
8558 {
8559 struct breakpoint *b;
8560
8561 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8562 tail-called one. */
8563 gdb_assert (!frame_id_artificial_p (frame_id));
8564
8565 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8566 b->enable_state = bp_enabled;
8567 b->disposition = disp_donttouch;
8568 b->frame_id = frame_id;
8569
8570 b->thread = inferior_thread ()->global_num;
8571
8572 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8573
8574 return breakpoint_up (b);
8575 }
8576
8577 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8578 The new breakpoint will have type TYPE, use OPS as its
8579 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8580
8581 static struct breakpoint *
8582 momentary_breakpoint_from_master (struct breakpoint *orig,
8583 enum bptype type,
8584 const struct breakpoint_ops *ops,
8585 int loc_enabled)
8586 {
8587 struct breakpoint *copy;
8588
8589 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8590 copy->loc = allocate_bp_location (copy);
8591 set_breakpoint_location_function (copy->loc);
8592
8593 copy->loc->gdbarch = orig->loc->gdbarch;
8594 copy->loc->requested_address = orig->loc->requested_address;
8595 copy->loc->address = orig->loc->address;
8596 copy->loc->section = orig->loc->section;
8597 copy->loc->pspace = orig->loc->pspace;
8598 copy->loc->probe = orig->loc->probe;
8599 copy->loc->line_number = orig->loc->line_number;
8600 copy->loc->symtab = orig->loc->symtab;
8601 copy->loc->enabled = loc_enabled;
8602 copy->frame_id = orig->frame_id;
8603 copy->thread = orig->thread;
8604 copy->pspace = orig->pspace;
8605
8606 copy->enable_state = bp_enabled;
8607 copy->disposition = disp_donttouch;
8608 copy->number = internal_breakpoint_number--;
8609
8610 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8611 return copy;
8612 }
8613
8614 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8615 ORIG is NULL. */
8616
8617 struct breakpoint *
8618 clone_momentary_breakpoint (struct breakpoint *orig)
8619 {
8620 /* If there's nothing to clone, then return nothing. */
8621 if (orig == NULL)
8622 return NULL;
8623
8624 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8625 }
8626
8627 breakpoint_up
8628 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8629 enum bptype type)
8630 {
8631 struct symtab_and_line sal;
8632
8633 sal = find_pc_line (pc, 0);
8634 sal.pc = pc;
8635 sal.section = find_pc_overlay (pc);
8636 sal.explicit_pc = 1;
8637
8638 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8639 }
8640 \f
8641
8642 /* Tell the user we have just set a breakpoint B. */
8643
8644 static void
8645 mention (struct breakpoint *b)
8646 {
8647 b->ops->print_mention (b);
8648 current_uiout->text ("\n");
8649 }
8650 \f
8651
8652 static bool bp_loc_is_permanent (struct bp_location *loc);
8653
8654 /* Handle "set breakpoint auto-hw on".
8655
8656 If the explicitly specified breakpoint type is not hardware
8657 breakpoint, check the memory map to see whether the breakpoint
8658 address is in read-only memory.
8659
8660 - location type is not hardware breakpoint, memory is read-only.
8661 We change the type of the location to hardware breakpoint.
8662
8663 - location type is hardware breakpoint, memory is read-write. This
8664 means we've previously made the location hardware one, but then the
8665 memory map changed, so we undo.
8666 */
8667
8668 static void
8669 handle_automatic_hardware_breakpoints (bp_location *bl)
8670 {
8671 if (automatic_hardware_breakpoints
8672 && bl->owner->type != bp_hardware_breakpoint
8673 && (bl->loc_type == bp_loc_software_breakpoint
8674 || bl->loc_type == bp_loc_hardware_breakpoint))
8675 {
8676 /* When breakpoints are removed, remove_breakpoints will use
8677 location types we've just set here, the only possible problem
8678 is that memory map has changed during running program, but
8679 it's not going to work anyway with current gdb. */
8680 mem_region *mr = lookup_mem_region (bl->address);
8681
8682 if (mr != nullptr)
8683 {
8684 enum bp_loc_type new_type;
8685
8686 if (mr->attrib.mode != MEM_RW)
8687 new_type = bp_loc_hardware_breakpoint;
8688 else
8689 new_type = bp_loc_software_breakpoint;
8690
8691 if (new_type != bl->loc_type)
8692 {
8693 static bool said = false;
8694
8695 bl->loc_type = new_type;
8696 if (!said)
8697 {
8698 fprintf_filtered (gdb_stdout,
8699 _("Note: automatically using "
8700 "hardware breakpoints for "
8701 "read-only addresses.\n"));
8702 said = true;
8703 }
8704 }
8705 }
8706 }
8707 }
8708
8709 static struct bp_location *
8710 add_location_to_breakpoint (struct breakpoint *b,
8711 const struct symtab_and_line *sal)
8712 {
8713 struct bp_location *loc, **tmp;
8714 CORE_ADDR adjusted_address;
8715 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8716
8717 if (loc_gdbarch == NULL)
8718 loc_gdbarch = b->gdbarch;
8719
8720 /* Adjust the breakpoint's address prior to allocating a location.
8721 Once we call allocate_bp_location(), that mostly uninitialized
8722 location will be placed on the location chain. Adjustment of the
8723 breakpoint may cause target_read_memory() to be called and we do
8724 not want its scan of the location chain to find a breakpoint and
8725 location that's only been partially initialized. */
8726 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8727 sal->pc, b->type);
8728
8729 /* Sort the locations by their ADDRESS. */
8730 loc = allocate_bp_location (b);
8731 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8732 tmp = &((*tmp)->next))
8733 ;
8734 loc->next = *tmp;
8735 *tmp = loc;
8736
8737 loc->requested_address = sal->pc;
8738 loc->address = adjusted_address;
8739 loc->pspace = sal->pspace;
8740 loc->probe.prob = sal->prob;
8741 loc->probe.objfile = sal->objfile;
8742 gdb_assert (loc->pspace != NULL);
8743 loc->section = sal->section;
8744 loc->gdbarch = loc_gdbarch;
8745 loc->line_number = sal->line;
8746 loc->symtab = sal->symtab;
8747 loc->symbol = sal->symbol;
8748 loc->msymbol = sal->msymbol;
8749 loc->objfile = sal->objfile;
8750
8751 set_breakpoint_location_function (loc);
8752
8753 /* While by definition, permanent breakpoints are already present in the
8754 code, we don't mark the location as inserted. Normally one would expect
8755 that GDB could rely on that breakpoint instruction to stop the program,
8756 thus removing the need to insert its own breakpoint, except that executing
8757 the breakpoint instruction can kill the target instead of reporting a
8758 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8759 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8760 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8761 breakpoint be inserted normally results in QEMU knowing about the GDB
8762 breakpoint, and thus trap before the breakpoint instruction is executed.
8763 (If GDB later needs to continue execution past the permanent breakpoint,
8764 it manually increments the PC, thus avoiding executing the breakpoint
8765 instruction.) */
8766 if (bp_loc_is_permanent (loc))
8767 loc->permanent = 1;
8768
8769 return loc;
8770 }
8771 \f
8772
8773 /* Return true if LOC is pointing to a permanent breakpoint,
8774 return false otherwise. */
8775
8776 static bool
8777 bp_loc_is_permanent (struct bp_location *loc)
8778 {
8779 gdb_assert (loc != NULL);
8780
8781 /* If we have a non-breakpoint-backed catchpoint or a software
8782 watchpoint, just return 0. We should not attempt to read from
8783 the addresses the locations of these breakpoint types point to.
8784 gdbarch_program_breakpoint_here_p, below, will attempt to read
8785 memory. */
8786 if (!bl_address_is_meaningful (loc))
8787 return false;
8788
8789 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8790 switch_to_program_space_and_thread (loc->pspace);
8791 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
8792 }
8793
8794 /* Build a command list for the dprintf corresponding to the current
8795 settings of the dprintf style options. */
8796
8797 static void
8798 update_dprintf_command_list (struct breakpoint *b)
8799 {
8800 char *dprintf_args = b->extra_string;
8801 char *printf_line = NULL;
8802
8803 if (!dprintf_args)
8804 return;
8805
8806 dprintf_args = skip_spaces (dprintf_args);
8807
8808 /* Allow a comma, as it may have terminated a location, but don't
8809 insist on it. */
8810 if (*dprintf_args == ',')
8811 ++dprintf_args;
8812 dprintf_args = skip_spaces (dprintf_args);
8813
8814 if (*dprintf_args != '"')
8815 error (_("Bad format string, missing '\"'."));
8816
8817 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8818 printf_line = xstrprintf ("printf %s", dprintf_args);
8819 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8820 {
8821 if (!dprintf_function)
8822 error (_("No function supplied for dprintf call"));
8823
8824 if (dprintf_channel && strlen (dprintf_channel) > 0)
8825 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8826 dprintf_function,
8827 dprintf_channel,
8828 dprintf_args);
8829 else
8830 printf_line = xstrprintf ("call (void) %s (%s)",
8831 dprintf_function,
8832 dprintf_args);
8833 }
8834 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8835 {
8836 if (target_can_run_breakpoint_commands ())
8837 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8838 else
8839 {
8840 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8841 printf_line = xstrprintf ("printf %s", dprintf_args);
8842 }
8843 }
8844 else
8845 internal_error (__FILE__, __LINE__,
8846 _("Invalid dprintf style."));
8847
8848 gdb_assert (printf_line != NULL);
8849
8850 /* Manufacture a printf sequence. */
8851 struct command_line *printf_cmd_line
8852 = new struct command_line (simple_control, printf_line);
8853 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8854 command_lines_deleter ()));
8855 }
8856
8857 /* Update all dprintf commands, making their command lists reflect
8858 current style settings. */
8859
8860 static void
8861 update_dprintf_commands (const char *args, int from_tty,
8862 struct cmd_list_element *c)
8863 {
8864 for (breakpoint *b : all_breakpoints ())
8865 if (b->type == bp_dprintf)
8866 update_dprintf_command_list (b);
8867 }
8868
8869 /* Create a breakpoint with SAL as location. Use LOCATION
8870 as a description of the location, and COND_STRING
8871 as condition expression. If LOCATION is NULL then create an
8872 "address location" from the address in the SAL. */
8873
8874 static void
8875 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8876 gdb::array_view<const symtab_and_line> sals,
8877 event_location_up &&location,
8878 gdb::unique_xmalloc_ptr<char> filter,
8879 gdb::unique_xmalloc_ptr<char> cond_string,
8880 gdb::unique_xmalloc_ptr<char> extra_string,
8881 enum bptype type, enum bpdisp disposition,
8882 int thread, int task, int ignore_count,
8883 const struct breakpoint_ops *ops, int from_tty,
8884 int enabled, int internal, unsigned flags,
8885 int display_canonical)
8886 {
8887 int i;
8888
8889 if (type == bp_hardware_breakpoint)
8890 {
8891 int target_resources_ok;
8892
8893 i = hw_breakpoint_used_count ();
8894 target_resources_ok =
8895 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8896 i + 1, 0);
8897 if (target_resources_ok == 0)
8898 error (_("No hardware breakpoint support in the target."));
8899 else if (target_resources_ok < 0)
8900 error (_("Hardware breakpoints used exceeds limit."));
8901 }
8902
8903 gdb_assert (!sals.empty ());
8904
8905 for (const auto &sal : sals)
8906 {
8907 struct bp_location *loc;
8908
8909 if (from_tty)
8910 {
8911 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8912 if (!loc_gdbarch)
8913 loc_gdbarch = gdbarch;
8914
8915 describe_other_breakpoints (loc_gdbarch,
8916 sal.pspace, sal.pc, sal.section, thread);
8917 }
8918
8919 if (&sal == &sals[0])
8920 {
8921 init_raw_breakpoint (b, gdbarch, sal, type, ops);
8922 b->thread = thread;
8923 b->task = task;
8924
8925 b->cond_string = cond_string.release ();
8926 b->extra_string = extra_string.release ();
8927 b->ignore_count = ignore_count;
8928 b->enable_state = enabled ? bp_enabled : bp_disabled;
8929 b->disposition = disposition;
8930
8931 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8932 b->loc->inserted = 1;
8933
8934 if (type == bp_static_tracepoint)
8935 {
8936 struct tracepoint *t = (struct tracepoint *) b;
8937 struct static_tracepoint_marker marker;
8938
8939 if (strace_marker_p (b))
8940 {
8941 /* We already know the marker exists, otherwise, we
8942 wouldn't see a sal for it. */
8943 const char *p
8944 = &event_location_to_string (b->location.get ())[3];
8945 const char *endp;
8946
8947 p = skip_spaces (p);
8948
8949 endp = skip_to_space (p);
8950
8951 t->static_trace_marker_id.assign (p, endp - p);
8952
8953 printf_filtered (_("Probed static tracepoint "
8954 "marker \"%s\"\n"),
8955 t->static_trace_marker_id.c_str ());
8956 }
8957 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8958 {
8959 t->static_trace_marker_id = std::move (marker.str_id);
8960
8961 printf_filtered (_("Probed static tracepoint "
8962 "marker \"%s\"\n"),
8963 t->static_trace_marker_id.c_str ());
8964 }
8965 else
8966 warning (_("Couldn't determine the static "
8967 "tracepoint marker to probe"));
8968 }
8969
8970 loc = b->loc;
8971 }
8972 else
8973 {
8974 loc = add_location_to_breakpoint (b, &sal);
8975 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8976 loc->inserted = 1;
8977 }
8978
8979 /* Do not set breakpoint locations conditions yet. As locations
8980 are inserted, they get sorted based on their addresses. Let
8981 the list stabilize to have reliable location numbers. */
8982
8983 /* Dynamic printf requires and uses additional arguments on the
8984 command line, otherwise it's an error. */
8985 if (type == bp_dprintf)
8986 {
8987 if (b->extra_string)
8988 update_dprintf_command_list (b);
8989 else
8990 error (_("Format string required"));
8991 }
8992 else if (b->extra_string)
8993 error (_("Garbage '%s' at end of command"), b->extra_string);
8994 }
8995
8996
8997 /* The order of the locations is now stable. Set the location
8998 condition using the location's number. */
8999 int loc_num = 1;
9000 for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)
9001 {
9002 if (b->cond_string != nullptr)
9003 set_breakpoint_location_condition (b->cond_string, loc, b->number,
9004 loc_num);
9005
9006 ++loc_num;
9007 }
9008
9009 b->display_canonical = display_canonical;
9010 if (location != NULL)
9011 b->location = std::move (location);
9012 else
9013 b->location = new_address_location (b->loc->address, NULL, 0);
9014 b->filter = std::move (filter);
9015 }
9016
9017 static void
9018 create_breakpoint_sal (struct gdbarch *gdbarch,
9019 gdb::array_view<const symtab_and_line> sals,
9020 event_location_up &&location,
9021 gdb::unique_xmalloc_ptr<char> filter,
9022 gdb::unique_xmalloc_ptr<char> cond_string,
9023 gdb::unique_xmalloc_ptr<char> extra_string,
9024 enum bptype type, enum bpdisp disposition,
9025 int thread, int task, int ignore_count,
9026 const struct breakpoint_ops *ops, int from_tty,
9027 int enabled, int internal, unsigned flags,
9028 int display_canonical)
9029 {
9030 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
9031
9032 init_breakpoint_sal (b.get (), gdbarch,
9033 sals, std::move (location),
9034 std::move (filter),
9035 std::move (cond_string),
9036 std::move (extra_string),
9037 type, disposition,
9038 thread, task, ignore_count,
9039 ops, from_tty,
9040 enabled, internal, flags,
9041 display_canonical);
9042
9043 install_breakpoint (internal, std::move (b), 0);
9044 }
9045
9046 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9047 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9048 value. COND_STRING, if not NULL, specified the condition to be
9049 used for all breakpoints. Essentially the only case where
9050 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9051 function. In that case, it's still not possible to specify
9052 separate conditions for different overloaded functions, so
9053 we take just a single condition string.
9054
9055 NOTE: If the function succeeds, the caller is expected to cleanup
9056 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9057 array contents). If the function fails (error() is called), the
9058 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9059 COND and SALS arrays and each of those arrays contents. */
9060
9061 static void
9062 create_breakpoints_sal (struct gdbarch *gdbarch,
9063 struct linespec_result *canonical,
9064 gdb::unique_xmalloc_ptr<char> cond_string,
9065 gdb::unique_xmalloc_ptr<char> extra_string,
9066 enum bptype type, enum bpdisp disposition,
9067 int thread, int task, int ignore_count,
9068 const struct breakpoint_ops *ops, int from_tty,
9069 int enabled, int internal, unsigned flags)
9070 {
9071 if (canonical->pre_expanded)
9072 gdb_assert (canonical->lsals.size () == 1);
9073
9074 for (const auto &lsal : canonical->lsals)
9075 {
9076 /* Note that 'location' can be NULL in the case of a plain
9077 'break', without arguments. */
9078 event_location_up location
9079 = (canonical->location != NULL
9080 ? copy_event_location (canonical->location.get ()) : NULL);
9081 gdb::unique_xmalloc_ptr<char> filter_string
9082 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
9083
9084 create_breakpoint_sal (gdbarch, lsal.sals,
9085 std::move (location),
9086 std::move (filter_string),
9087 std::move (cond_string),
9088 std::move (extra_string),
9089 type, disposition,
9090 thread, task, ignore_count, ops,
9091 from_tty, enabled, internal, flags,
9092 canonical->special_display);
9093 }
9094 }
9095
9096 /* Parse LOCATION which is assumed to be a SAL specification possibly
9097 followed by conditionals. On return, SALS contains an array of SAL
9098 addresses found. LOCATION points to the end of the SAL (for
9099 linespec locations).
9100
9101 The array and the line spec strings are allocated on the heap, it is
9102 the caller's responsibility to free them. */
9103
9104 static void
9105 parse_breakpoint_sals (struct event_location *location,
9106 struct linespec_result *canonical)
9107 {
9108 struct symtab_and_line cursal;
9109
9110 if (event_location_type (location) == LINESPEC_LOCATION)
9111 {
9112 const char *spec = get_linespec_location (location)->spec_string;
9113
9114 if (spec == NULL)
9115 {
9116 /* The last displayed codepoint, if it's valid, is our default
9117 breakpoint address. */
9118 if (last_displayed_sal_is_valid ())
9119 {
9120 /* Set sal's pspace, pc, symtab, and line to the values
9121 corresponding to the last call to print_frame_info.
9122 Be sure to reinitialize LINE with NOTCURRENT == 0
9123 as the breakpoint line number is inappropriate otherwise.
9124 find_pc_line would adjust PC, re-set it back. */
9125 symtab_and_line sal = get_last_displayed_sal ();
9126 CORE_ADDR pc = sal.pc;
9127
9128 sal = find_pc_line (pc, 0);
9129
9130 /* "break" without arguments is equivalent to "break *PC"
9131 where PC is the last displayed codepoint's address. So
9132 make sure to set sal.explicit_pc to prevent GDB from
9133 trying to expand the list of sals to include all other
9134 instances with the same symtab and line. */
9135 sal.pc = pc;
9136 sal.explicit_pc = 1;
9137
9138 struct linespec_sals lsal;
9139 lsal.sals = {sal};
9140 lsal.canonical = NULL;
9141
9142 canonical->lsals.push_back (std::move (lsal));
9143 return;
9144 }
9145 else
9146 error (_("No default breakpoint address now."));
9147 }
9148 }
9149
9150 /* Force almost all breakpoints to be in terms of the
9151 current_source_symtab (which is decode_line_1's default).
9152 This should produce the results we want almost all of the
9153 time while leaving default_breakpoint_* alone.
9154
9155 ObjC: However, don't match an Objective-C method name which
9156 may have a '+' or '-' succeeded by a '['. */
9157 cursal = get_current_source_symtab_and_line ();
9158 if (last_displayed_sal_is_valid ())
9159 {
9160 const char *spec = NULL;
9161
9162 if (event_location_type (location) == LINESPEC_LOCATION)
9163 spec = get_linespec_location (location)->spec_string;
9164
9165 if (!cursal.symtab
9166 || (spec != NULL
9167 && strchr ("+-", spec[0]) != NULL
9168 && spec[1] != '['))
9169 {
9170 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9171 get_last_displayed_symtab (),
9172 get_last_displayed_line (),
9173 canonical, NULL, NULL);
9174 return;
9175 }
9176 }
9177
9178 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9179 cursal.symtab, cursal.line, canonical, NULL, NULL);
9180 }
9181
9182
9183 /* Convert each SAL into a real PC. Verify that the PC can be
9184 inserted as a breakpoint. If it can't throw an error. */
9185
9186 static void
9187 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
9188 {
9189 for (auto &sal : sals)
9190 resolve_sal_pc (&sal);
9191 }
9192
9193 /* Fast tracepoints may have restrictions on valid locations. For
9194 instance, a fast tracepoint using a jump instead of a trap will
9195 likely have to overwrite more bytes than a trap would, and so can
9196 only be placed where the instruction is longer than the jump, or a
9197 multi-instruction sequence does not have a jump into the middle of
9198 it, etc. */
9199
9200 static void
9201 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9202 gdb::array_view<const symtab_and_line> sals)
9203 {
9204 for (const auto &sal : sals)
9205 {
9206 struct gdbarch *sarch;
9207
9208 sarch = get_sal_arch (sal);
9209 /* We fall back to GDBARCH if there is no architecture
9210 associated with SAL. */
9211 if (sarch == NULL)
9212 sarch = gdbarch;
9213 std::string msg;
9214 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
9215 error (_("May not have a fast tracepoint at %s%s"),
9216 paddress (sarch, sal.pc), msg.c_str ());
9217 }
9218 }
9219
9220 /* Given TOK, a string specification of condition and thread, as
9221 accepted by the 'break' command, extract the condition
9222 string and thread number and set *COND_STRING and *THREAD.
9223 PC identifies the context at which the condition should be parsed.
9224 If no condition is found, *COND_STRING is set to NULL.
9225 If no thread is found, *THREAD is set to -1. */
9226
9227 static void
9228 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9229 char **cond_string, int *thread, int *task,
9230 char **rest)
9231 {
9232 *cond_string = NULL;
9233 *thread = -1;
9234 *task = 0;
9235 *rest = NULL;
9236 bool force = false;
9237
9238 while (tok && *tok)
9239 {
9240 const char *end_tok;
9241 int toklen;
9242 const char *cond_start = NULL;
9243 const char *cond_end = NULL;
9244
9245 tok = skip_spaces (tok);
9246
9247 if ((*tok == '"' || *tok == ',') && rest)
9248 {
9249 *rest = savestring (tok, strlen (tok));
9250 return;
9251 }
9252
9253 end_tok = skip_to_space (tok);
9254
9255 toklen = end_tok - tok;
9256
9257 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9258 {
9259 tok = cond_start = end_tok + 1;
9260 try
9261 {
9262 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9263 }
9264 catch (const gdb_exception_error &)
9265 {
9266 if (!force)
9267 throw;
9268 else
9269 tok = tok + strlen (tok);
9270 }
9271 cond_end = tok;
9272 *cond_string = savestring (cond_start, cond_end - cond_start);
9273 }
9274 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
9275 {
9276 tok = tok + toklen;
9277 force = true;
9278 }
9279 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9280 {
9281 const char *tmptok;
9282 struct thread_info *thr;
9283
9284 tok = end_tok + 1;
9285 thr = parse_thread_id (tok, &tmptok);
9286 if (tok == tmptok)
9287 error (_("Junk after thread keyword."));
9288 *thread = thr->global_num;
9289 tok = tmptok;
9290 }
9291 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9292 {
9293 char *tmptok;
9294
9295 tok = end_tok + 1;
9296 *task = strtol (tok, &tmptok, 0);
9297 if (tok == tmptok)
9298 error (_("Junk after task keyword."));
9299 if (!valid_task_id (*task))
9300 error (_("Unknown task %d."), *task);
9301 tok = tmptok;
9302 }
9303 else if (rest)
9304 {
9305 *rest = savestring (tok, strlen (tok));
9306 return;
9307 }
9308 else
9309 error (_("Junk at end of arguments."));
9310 }
9311 }
9312
9313 /* Call 'find_condition_and_thread' for each sal in SALS until a parse
9314 succeeds. The parsed values are written to COND_STRING, THREAD,
9315 TASK, and REST. See the comment of 'find_condition_and_thread'
9316 for the description of these parameters and INPUT. */
9317
9318 static void
9319 find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
9320 const char *input, char **cond_string,
9321 int *thread, int *task, char **rest)
9322 {
9323 int num_failures = 0;
9324 for (auto &sal : sals)
9325 {
9326 char *cond = nullptr;
9327 int thread_id = 0;
9328 int task_id = 0;
9329 char *remaining = nullptr;
9330
9331 /* Here we want to parse 'arg' to separate condition from thread
9332 number. But because parsing happens in a context and the
9333 contexts of sals might be different, try each until there is
9334 success. Finding one successful parse is sufficient for our
9335 goal. When setting the breakpoint we'll re-parse the
9336 condition in the context of each sal. */
9337 try
9338 {
9339 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
9340 &task_id, &remaining);
9341 *cond_string = cond;
9342 *thread = thread_id;
9343 *task = task_id;
9344 *rest = remaining;
9345 break;
9346 }
9347 catch (const gdb_exception_error &e)
9348 {
9349 num_failures++;
9350 /* If no sal remains, do not continue. */
9351 if (num_failures == sals.size ())
9352 throw;
9353 }
9354 }
9355 }
9356
9357 /* Decode a static tracepoint marker spec. */
9358
9359 static std::vector<symtab_and_line>
9360 decode_static_tracepoint_spec (const char **arg_p)
9361 {
9362 const char *p = &(*arg_p)[3];
9363 const char *endp;
9364
9365 p = skip_spaces (p);
9366
9367 endp = skip_to_space (p);
9368
9369 std::string marker_str (p, endp - p);
9370
9371 std::vector<static_tracepoint_marker> markers
9372 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9373 if (markers.empty ())
9374 error (_("No known static tracepoint marker named %s"),
9375 marker_str.c_str ());
9376
9377 std::vector<symtab_and_line> sals;
9378 sals.reserve (markers.size ());
9379
9380 for (const static_tracepoint_marker &marker : markers)
9381 {
9382 symtab_and_line sal = find_pc_line (marker.address, 0);
9383 sal.pc = marker.address;
9384 sals.push_back (sal);
9385 }
9386
9387 *arg_p = endp;
9388 return sals;
9389 }
9390
9391 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9392 according to IS_TRACEPOINT. */
9393
9394 static const struct breakpoint_ops *
9395 breakpoint_ops_for_event_location_type (enum event_location_type location_type,
9396 bool is_tracepoint)
9397 {
9398 if (is_tracepoint)
9399 {
9400 if (location_type == PROBE_LOCATION)
9401 return &tracepoint_probe_breakpoint_ops;
9402 else
9403 return &tracepoint_breakpoint_ops;
9404 }
9405 else
9406 {
9407 if (location_type == PROBE_LOCATION)
9408 return &bkpt_probe_breakpoint_ops;
9409 else
9410 return &bkpt_breakpoint_ops;
9411 }
9412 }
9413
9414 /* See breakpoint.h. */
9415
9416 const struct breakpoint_ops *
9417 breakpoint_ops_for_event_location (const struct event_location *location,
9418 bool is_tracepoint)
9419 {
9420 if (location != nullptr)
9421 return breakpoint_ops_for_event_location_type
9422 (event_location_type (location), is_tracepoint);
9423 return is_tracepoint ? &tracepoint_breakpoint_ops : &bkpt_breakpoint_ops;
9424 }
9425
9426 /* See breakpoint.h. */
9427
9428 int
9429 create_breakpoint (struct gdbarch *gdbarch,
9430 struct event_location *location,
9431 const char *cond_string,
9432 int thread, const char *extra_string,
9433 bool force_condition, int parse_extra,
9434 int tempflag, enum bptype type_wanted,
9435 int ignore_count,
9436 enum auto_boolean pending_break_support,
9437 const struct breakpoint_ops *ops,
9438 int from_tty, int enabled, int internal,
9439 unsigned flags)
9440 {
9441 struct linespec_result canonical;
9442 int pending = 0;
9443 int task = 0;
9444 int prev_bkpt_count = breakpoint_count;
9445
9446 gdb_assert (ops != NULL);
9447
9448 /* If extra_string isn't useful, set it to NULL. */
9449 if (extra_string != NULL && *extra_string == '\0')
9450 extra_string = NULL;
9451
9452 try
9453 {
9454 ops->create_sals_from_location (location, &canonical, type_wanted);
9455 }
9456 catch (const gdb_exception_error &e)
9457 {
9458 /* If caller is interested in rc value from parse, set
9459 value. */
9460 if (e.error == NOT_FOUND_ERROR)
9461 {
9462 /* If pending breakpoint support is turned off, throw
9463 error. */
9464
9465 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9466 throw;
9467
9468 exception_print (gdb_stderr, e);
9469
9470 /* If pending breakpoint support is auto query and the user
9471 selects no, then simply return the error code. */
9472 if (pending_break_support == AUTO_BOOLEAN_AUTO
9473 && !nquery (_("Make %s pending on future shared library load? "),
9474 bptype_string (type_wanted)))
9475 return 0;
9476
9477 /* At this point, either the user was queried about setting
9478 a pending breakpoint and selected yes, or pending
9479 breakpoint behavior is on and thus a pending breakpoint
9480 is defaulted on behalf of the user. */
9481 pending = 1;
9482 }
9483 else
9484 throw;
9485 }
9486
9487 if (!pending && canonical.lsals.empty ())
9488 return 0;
9489
9490 /* Resolve all line numbers to PC's and verify that the addresses
9491 are ok for the target. */
9492 if (!pending)
9493 {
9494 for (auto &lsal : canonical.lsals)
9495 breakpoint_sals_to_pc (lsal.sals);
9496 }
9497
9498 /* Fast tracepoints may have additional restrictions on location. */
9499 if (!pending && type_wanted == bp_fast_tracepoint)
9500 {
9501 for (const auto &lsal : canonical.lsals)
9502 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9503 }
9504
9505 /* Verify that condition can be parsed, before setting any
9506 breakpoints. Allocate a separate condition expression for each
9507 breakpoint. */
9508 if (!pending)
9509 {
9510 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9511 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9512
9513 if (parse_extra)
9514 {
9515 char *rest;
9516 char *cond;
9517
9518 const linespec_sals &lsal = canonical.lsals[0];
9519
9520 find_condition_and_thread_for_sals (lsal.sals, extra_string,
9521 &cond, &thread, &task, &rest);
9522 cond_string_copy.reset (cond);
9523 extra_string_copy.reset (rest);
9524 }
9525 else
9526 {
9527 if (type_wanted != bp_dprintf
9528 && extra_string != NULL && *extra_string != '\0')
9529 error (_("Garbage '%s' at end of location"), extra_string);
9530
9531 /* Check the validity of the condition. We should error out
9532 if the condition is invalid at all of the locations and
9533 if it is not forced. In the PARSE_EXTRA case above, this
9534 check is done when parsing the EXTRA_STRING. */
9535 if (cond_string != nullptr && !force_condition)
9536 {
9537 int num_failures = 0;
9538 const linespec_sals &lsal = canonical.lsals[0];
9539 for (const auto &sal : lsal.sals)
9540 {
9541 const char *cond = cond_string;
9542 try
9543 {
9544 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
9545 /* One success is sufficient to keep going. */
9546 break;
9547 }
9548 catch (const gdb_exception_error &)
9549 {
9550 num_failures++;
9551 /* If this is the last sal, error out. */
9552 if (num_failures == lsal.sals.size ())
9553 throw;
9554 }
9555 }
9556 }
9557
9558 /* Create a private copy of condition string. */
9559 if (cond_string)
9560 cond_string_copy.reset (xstrdup (cond_string));
9561 /* Create a private copy of any extra string. */
9562 if (extra_string)
9563 extra_string_copy.reset (xstrdup (extra_string));
9564 }
9565
9566 ops->create_breakpoints_sal (gdbarch, &canonical,
9567 std::move (cond_string_copy),
9568 std::move (extra_string_copy),
9569 type_wanted,
9570 tempflag ? disp_del : disp_donttouch,
9571 thread, task, ignore_count, ops,
9572 from_tty, enabled, internal, flags);
9573 }
9574 else
9575 {
9576 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9577
9578 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9579 b->location = copy_event_location (location);
9580
9581 if (parse_extra)
9582 b->cond_string = NULL;
9583 else
9584 {
9585 /* Create a private copy of condition string. */
9586 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
9587 b->thread = thread;
9588 }
9589
9590 /* Create a private copy of any extra string. */
9591 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
9592 b->ignore_count = ignore_count;
9593 b->disposition = tempflag ? disp_del : disp_donttouch;
9594 b->condition_not_parsed = 1;
9595 b->enable_state = enabled ? bp_enabled : bp_disabled;
9596 if ((type_wanted != bp_breakpoint
9597 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9598 b->pspace = current_program_space;
9599
9600 install_breakpoint (internal, std::move (b), 0);
9601 }
9602
9603 if (canonical.lsals.size () > 1)
9604 {
9605 warning (_("Multiple breakpoints were set.\nUse the "
9606 "\"delete\" command to delete unwanted breakpoints."));
9607 prev_breakpoint_count = prev_bkpt_count;
9608 }
9609
9610 update_global_location_list (UGLL_MAY_INSERT);
9611
9612 return 1;
9613 }
9614
9615 /* Set a breakpoint.
9616 ARG is a string describing breakpoint address,
9617 condition, and thread.
9618 FLAG specifies if a breakpoint is hardware on,
9619 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9620 and BP_TEMPFLAG. */
9621
9622 static void
9623 break_command_1 (const char *arg, int flag, int from_tty)
9624 {
9625 int tempflag = flag & BP_TEMPFLAG;
9626 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9627 ? bp_hardware_breakpoint
9628 : bp_breakpoint);
9629
9630 event_location_up location = string_to_event_location (&arg, current_language);
9631 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9632 (location.get (), false /* is_tracepoint */);
9633
9634 create_breakpoint (get_current_arch (),
9635 location.get (),
9636 NULL, 0, arg, false, 1 /* parse arg */,
9637 tempflag, type_wanted,
9638 0 /* Ignore count */,
9639 pending_break_support,
9640 ops,
9641 from_tty,
9642 1 /* enabled */,
9643 0 /* internal */,
9644 0);
9645 }
9646
9647 /* Helper function for break_command_1 and disassemble_command. */
9648
9649 void
9650 resolve_sal_pc (struct symtab_and_line *sal)
9651 {
9652 CORE_ADDR pc;
9653
9654 if (sal->pc == 0 && sal->symtab != NULL)
9655 {
9656 if (!find_line_pc (sal->symtab, sal->line, &pc))
9657 error (_("No line %d in file \"%s\"."),
9658 sal->line, symtab_to_filename_for_display (sal->symtab));
9659 sal->pc = pc;
9660
9661 /* If this SAL corresponds to a breakpoint inserted using a line
9662 number, then skip the function prologue if necessary. */
9663 if (sal->explicit_line)
9664 skip_prologue_sal (sal);
9665 }
9666
9667 if (sal->section == 0 && sal->symtab != NULL)
9668 {
9669 const struct blockvector *bv;
9670 const struct block *b;
9671 struct symbol *sym;
9672
9673 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9674 SYMTAB_COMPUNIT (sal->symtab));
9675 if (bv != NULL)
9676 {
9677 sym = block_linkage_function (b);
9678 if (sym != NULL)
9679 {
9680 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9681 sal->section = sym->obj_section (SYMTAB_OBJFILE (sal->symtab));
9682 }
9683 else
9684 {
9685 /* It really is worthwhile to have the section, so we'll
9686 just have to look harder. This case can be executed
9687 if we have line numbers but no functions (as can
9688 happen in assembly source). */
9689
9690 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9691 switch_to_program_space_and_thread (sal->pspace);
9692
9693 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9694 if (msym.minsym)
9695 sal->section = msym.obj_section ();
9696 }
9697 }
9698 }
9699 }
9700
9701 void
9702 break_command (const char *arg, int from_tty)
9703 {
9704 break_command_1 (arg, 0, from_tty);
9705 }
9706
9707 void
9708 tbreak_command (const char *arg, int from_tty)
9709 {
9710 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9711 }
9712
9713 static void
9714 hbreak_command (const char *arg, int from_tty)
9715 {
9716 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9717 }
9718
9719 static void
9720 thbreak_command (const char *arg, int from_tty)
9721 {
9722 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9723 }
9724
9725 static void
9726 stop_command (const char *arg, int from_tty)
9727 {
9728 printf_filtered (_("Specify the type of breakpoint to set.\n\
9729 Usage: stop in <function | address>\n\
9730 stop at <line>\n"));
9731 }
9732
9733 static void
9734 stopin_command (const char *arg, int from_tty)
9735 {
9736 int badInput = 0;
9737
9738 if (arg == NULL)
9739 badInput = 1;
9740 else if (*arg != '*')
9741 {
9742 const char *argptr = arg;
9743 int hasColon = 0;
9744
9745 /* Look for a ':'. If this is a line number specification, then
9746 say it is bad, otherwise, it should be an address or
9747 function/method name. */
9748 while (*argptr && !hasColon)
9749 {
9750 hasColon = (*argptr == ':');
9751 argptr++;
9752 }
9753
9754 if (hasColon)
9755 badInput = (*argptr != ':'); /* Not a class::method */
9756 else
9757 badInput = isdigit (*arg); /* a simple line number */
9758 }
9759
9760 if (badInput)
9761 printf_filtered (_("Usage: stop in <function | address>\n"));
9762 else
9763 break_command_1 (arg, 0, from_tty);
9764 }
9765
9766 static void
9767 stopat_command (const char *arg, int from_tty)
9768 {
9769 int badInput = 0;
9770
9771 if (arg == NULL || *arg == '*') /* no line number */
9772 badInput = 1;
9773 else
9774 {
9775 const char *argptr = arg;
9776 int hasColon = 0;
9777
9778 /* Look for a ':'. If there is a '::' then get out, otherwise
9779 it is probably a line number. */
9780 while (*argptr && !hasColon)
9781 {
9782 hasColon = (*argptr == ':');
9783 argptr++;
9784 }
9785
9786 if (hasColon)
9787 badInput = (*argptr == ':'); /* we have class::method */
9788 else
9789 badInput = !isdigit (*arg); /* not a line number */
9790 }
9791
9792 if (badInput)
9793 printf_filtered (_("Usage: stop at LINE\n"));
9794 else
9795 break_command_1 (arg, 0, from_tty);
9796 }
9797
9798 /* The dynamic printf command is mostly like a regular breakpoint, but
9799 with a prewired command list consisting of a single output command,
9800 built from extra arguments supplied on the dprintf command
9801 line. */
9802
9803 static void
9804 dprintf_command (const char *arg, int from_tty)
9805 {
9806 event_location_up location = string_to_event_location (&arg, current_language);
9807
9808 /* If non-NULL, ARG should have been advanced past the location;
9809 the next character must be ','. */
9810 if (arg != NULL)
9811 {
9812 if (arg[0] != ',' || arg[1] == '\0')
9813 error (_("Format string required"));
9814 else
9815 {
9816 /* Skip the comma. */
9817 ++arg;
9818 }
9819 }
9820
9821 create_breakpoint (get_current_arch (),
9822 location.get (),
9823 NULL, 0, arg, false, 1 /* parse arg */,
9824 0, bp_dprintf,
9825 0 /* Ignore count */,
9826 pending_break_support,
9827 &dprintf_breakpoint_ops,
9828 from_tty,
9829 1 /* enabled */,
9830 0 /* internal */,
9831 0);
9832 }
9833
9834 static void
9835 agent_printf_command (const char *arg, int from_tty)
9836 {
9837 error (_("May only run agent-printf on the target"));
9838 }
9839
9840 /* Implement the "breakpoint_hit" breakpoint_ops method for
9841 ranged breakpoints. */
9842
9843 static int
9844 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9845 const address_space *aspace,
9846 CORE_ADDR bp_addr,
9847 const struct target_waitstatus *ws)
9848 {
9849 if (ws->kind != TARGET_WAITKIND_STOPPED
9850 || ws->value.sig != GDB_SIGNAL_TRAP)
9851 return 0;
9852
9853 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9854 bl->length, aspace, bp_addr);
9855 }
9856
9857 /* Implement the "resources_needed" breakpoint_ops method for
9858 ranged breakpoints. */
9859
9860 static int
9861 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9862 {
9863 return target_ranged_break_num_registers ();
9864 }
9865
9866 /* Implement the "print_it" breakpoint_ops method for
9867 ranged breakpoints. */
9868
9869 static enum print_stop_action
9870 print_it_ranged_breakpoint (bpstat bs)
9871 {
9872 struct breakpoint *b = bs->breakpoint_at;
9873 struct bp_location *bl = b->loc;
9874 struct ui_out *uiout = current_uiout;
9875
9876 gdb_assert (b->type == bp_hardware_breakpoint);
9877
9878 /* Ranged breakpoints have only one location. */
9879 gdb_assert (bl && bl->next == NULL);
9880
9881 annotate_breakpoint (b->number);
9882
9883 maybe_print_thread_hit_breakpoint (uiout);
9884
9885 if (b->disposition == disp_del)
9886 uiout->text ("Temporary ranged breakpoint ");
9887 else
9888 uiout->text ("Ranged breakpoint ");
9889 if (uiout->is_mi_like_p ())
9890 {
9891 uiout->field_string ("reason",
9892 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9893 uiout->field_string ("disp", bpdisp_text (b->disposition));
9894 }
9895 uiout->field_signed ("bkptno", b->number);
9896 uiout->text (", ");
9897
9898 return PRINT_SRC_AND_LOC;
9899 }
9900
9901 /* Implement the "print_one" breakpoint_ops method for
9902 ranged breakpoints. */
9903
9904 static void
9905 print_one_ranged_breakpoint (struct breakpoint *b,
9906 struct bp_location **last_loc)
9907 {
9908 struct bp_location *bl = b->loc;
9909 struct value_print_options opts;
9910 struct ui_out *uiout = current_uiout;
9911
9912 /* Ranged breakpoints have only one location. */
9913 gdb_assert (bl && bl->next == NULL);
9914
9915 get_user_print_options (&opts);
9916
9917 if (opts.addressprint)
9918 /* We don't print the address range here, it will be printed later
9919 by print_one_detail_ranged_breakpoint. */
9920 uiout->field_skip ("addr");
9921 annotate_field (5);
9922 print_breakpoint_location (b, bl);
9923 *last_loc = bl;
9924 }
9925
9926 /* Implement the "print_one_detail" breakpoint_ops method for
9927 ranged breakpoints. */
9928
9929 static void
9930 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9931 struct ui_out *uiout)
9932 {
9933 CORE_ADDR address_start, address_end;
9934 struct bp_location *bl = b->loc;
9935 string_file stb;
9936
9937 gdb_assert (bl);
9938
9939 address_start = bl->address;
9940 address_end = address_start + bl->length - 1;
9941
9942 uiout->text ("\taddress range: ");
9943 stb.printf ("[%s, %s]",
9944 print_core_address (bl->gdbarch, address_start),
9945 print_core_address (bl->gdbarch, address_end));
9946 uiout->field_stream ("addr", stb);
9947 uiout->text ("\n");
9948 }
9949
9950 /* Implement the "print_mention" breakpoint_ops method for
9951 ranged breakpoints. */
9952
9953 static void
9954 print_mention_ranged_breakpoint (struct breakpoint *b)
9955 {
9956 struct bp_location *bl = b->loc;
9957 struct ui_out *uiout = current_uiout;
9958
9959 gdb_assert (bl);
9960 gdb_assert (b->type == bp_hardware_breakpoint);
9961
9962 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9963 b->number, paddress (bl->gdbarch, bl->address),
9964 paddress (bl->gdbarch, bl->address + bl->length - 1));
9965 }
9966
9967 /* Implement the "print_recreate" breakpoint_ops method for
9968 ranged breakpoints. */
9969
9970 static void
9971 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9972 {
9973 fprintf_unfiltered (fp, "break-range %s, %s",
9974 event_location_to_string (b->location.get ()),
9975 event_location_to_string (b->location_range_end.get ()));
9976 print_recreate_thread (b, fp);
9977 }
9978
9979 /* The breakpoint_ops structure to be used in ranged breakpoints. */
9980
9981 static struct breakpoint_ops ranged_breakpoint_ops;
9982
9983 /* Find the address where the end of the breakpoint range should be
9984 placed, given the SAL of the end of the range. This is so that if
9985 the user provides a line number, the end of the range is set to the
9986 last instruction of the given line. */
9987
9988 static CORE_ADDR
9989 find_breakpoint_range_end (struct symtab_and_line sal)
9990 {
9991 CORE_ADDR end;
9992
9993 /* If the user provided a PC value, use it. Otherwise,
9994 find the address of the end of the given location. */
9995 if (sal.explicit_pc)
9996 end = sal.pc;
9997 else
9998 {
9999 int ret;
10000 CORE_ADDR start;
10001
10002 ret = find_line_pc_range (sal, &start, &end);
10003 if (!ret)
10004 error (_("Could not find location of the end of the range."));
10005
10006 /* find_line_pc_range returns the start of the next line. */
10007 end--;
10008 }
10009
10010 return end;
10011 }
10012
10013 /* Implement the "break-range" CLI command. */
10014
10015 static void
10016 break_range_command (const char *arg, int from_tty)
10017 {
10018 const char *arg_start;
10019 struct linespec_result canonical_start, canonical_end;
10020 int bp_count, can_use_bp, length;
10021 CORE_ADDR end;
10022 struct breakpoint *b;
10023
10024 /* We don't support software ranged breakpoints. */
10025 if (target_ranged_break_num_registers () < 0)
10026 error (_("This target does not support hardware ranged breakpoints."));
10027
10028 bp_count = hw_breakpoint_used_count ();
10029 bp_count += target_ranged_break_num_registers ();
10030 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10031 bp_count, 0);
10032 if (can_use_bp < 0)
10033 error (_("Hardware breakpoints used exceeds limit."));
10034
10035 arg = skip_spaces (arg);
10036 if (arg == NULL || arg[0] == '\0')
10037 error(_("No address range specified."));
10038
10039 arg_start = arg;
10040 event_location_up start_location = string_to_event_location (&arg,
10041 current_language);
10042 parse_breakpoint_sals (start_location.get (), &canonical_start);
10043
10044 if (arg[0] != ',')
10045 error (_("Too few arguments."));
10046 else if (canonical_start.lsals.empty ())
10047 error (_("Could not find location of the beginning of the range."));
10048
10049 const linespec_sals &lsal_start = canonical_start.lsals[0];
10050
10051 if (canonical_start.lsals.size () > 1
10052 || lsal_start.sals.size () != 1)
10053 error (_("Cannot create a ranged breakpoint with multiple locations."));
10054
10055 const symtab_and_line &sal_start = lsal_start.sals[0];
10056 std::string addr_string_start (arg_start, arg - arg_start);
10057
10058 arg++; /* Skip the comma. */
10059 arg = skip_spaces (arg);
10060
10061 /* Parse the end location. */
10062
10063 arg_start = arg;
10064
10065 /* We call decode_line_full directly here instead of using
10066 parse_breakpoint_sals because we need to specify the start location's
10067 symtab and line as the default symtab and line for the end of the
10068 range. This makes it possible to have ranges like "foo.c:27, +14",
10069 where +14 means 14 lines from the start location. */
10070 event_location_up end_location = string_to_event_location (&arg,
10071 current_language);
10072 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10073 sal_start.symtab, sal_start.line,
10074 &canonical_end, NULL, NULL);
10075
10076 if (canonical_end.lsals.empty ())
10077 error (_("Could not find location of the end of the range."));
10078
10079 const linespec_sals &lsal_end = canonical_end.lsals[0];
10080 if (canonical_end.lsals.size () > 1
10081 || lsal_end.sals.size () != 1)
10082 error (_("Cannot create a ranged breakpoint with multiple locations."));
10083
10084 const symtab_and_line &sal_end = lsal_end.sals[0];
10085
10086 end = find_breakpoint_range_end (sal_end);
10087 if (sal_start.pc > end)
10088 error (_("Invalid address range, end precedes start."));
10089
10090 length = end - sal_start.pc + 1;
10091 if (length < 0)
10092 /* Length overflowed. */
10093 error (_("Address range too large."));
10094 else if (length == 1)
10095 {
10096 /* This range is simple enough to be handled by
10097 the `hbreak' command. */
10098 hbreak_command (&addr_string_start[0], 1);
10099
10100 return;
10101 }
10102
10103 /* Now set up the breakpoint. */
10104 b = set_raw_breakpoint (get_current_arch (), sal_start,
10105 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10106 set_breakpoint_count (breakpoint_count + 1);
10107 b->number = breakpoint_count;
10108 b->disposition = disp_donttouch;
10109 b->location = std::move (start_location);
10110 b->location_range_end = std::move (end_location);
10111 b->loc->length = length;
10112
10113 mention (b);
10114 gdb::observers::breakpoint_created.notify (b);
10115 update_global_location_list (UGLL_MAY_INSERT);
10116 }
10117
10118 /* Return non-zero if EXP is verified as constant. Returned zero
10119 means EXP is variable. Also the constant detection may fail for
10120 some constant expressions and in such case still falsely return
10121 zero. */
10122
10123 static bool
10124 watchpoint_exp_is_const (const struct expression *exp)
10125 {
10126 return exp->op->constant_p ();
10127 }
10128
10129 /* Watchpoint destructor. */
10130
10131 watchpoint::~watchpoint ()
10132 {
10133 xfree (this->exp_string);
10134 xfree (this->exp_string_reparse);
10135 }
10136
10137 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10138
10139 static void
10140 re_set_watchpoint (struct breakpoint *b)
10141 {
10142 struct watchpoint *w = (struct watchpoint *) b;
10143
10144 /* Watchpoint can be either on expression using entirely global
10145 variables, or it can be on local variables.
10146
10147 Watchpoints of the first kind are never auto-deleted, and even
10148 persist across program restarts. Since they can use variables
10149 from shared libraries, we need to reparse expression as libraries
10150 are loaded and unloaded.
10151
10152 Watchpoints on local variables can also change meaning as result
10153 of solib event. For example, if a watchpoint uses both a local
10154 and a global variables in expression, it's a local watchpoint,
10155 but unloading of a shared library will make the expression
10156 invalid. This is not a very common use case, but we still
10157 re-evaluate expression, to avoid surprises to the user.
10158
10159 Note that for local watchpoints, we re-evaluate it only if
10160 watchpoints frame id is still valid. If it's not, it means the
10161 watchpoint is out of scope and will be deleted soon. In fact,
10162 I'm not sure we'll ever be called in this case.
10163
10164 If a local watchpoint's frame id is still valid, then
10165 w->exp_valid_block is likewise valid, and we can safely use it.
10166
10167 Don't do anything about disabled watchpoints, since they will be
10168 reevaluated again when enabled. */
10169 update_watchpoint (w, 1 /* reparse */);
10170 }
10171
10172 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10173
10174 static int
10175 insert_watchpoint (struct bp_location *bl)
10176 {
10177 struct watchpoint *w = (struct watchpoint *) bl->owner;
10178 int length = w->exact ? 1 : bl->length;
10179
10180 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10181 w->cond_exp.get ());
10182 }
10183
10184 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10185
10186 static int
10187 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10188 {
10189 struct watchpoint *w = (struct watchpoint *) bl->owner;
10190 int length = w->exact ? 1 : bl->length;
10191
10192 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10193 w->cond_exp.get ());
10194 }
10195
10196 static int
10197 breakpoint_hit_watchpoint (const struct bp_location *bl,
10198 const address_space *aspace, CORE_ADDR bp_addr,
10199 const struct target_waitstatus *ws)
10200 {
10201 struct breakpoint *b = bl->owner;
10202 struct watchpoint *w = (struct watchpoint *) b;
10203
10204 /* Continuable hardware watchpoints are treated as non-existent if the
10205 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10206 some data address). Otherwise gdb won't stop on a break instruction
10207 in the code (not from a breakpoint) when a hardware watchpoint has
10208 been defined. Also skip watchpoints which we know did not trigger
10209 (did not match the data address). */
10210 if (is_hardware_watchpoint (b)
10211 && w->watchpoint_triggered == watch_triggered_no)
10212 return 0;
10213
10214 return 1;
10215 }
10216
10217 static void
10218 check_status_watchpoint (bpstat bs)
10219 {
10220 gdb_assert (is_watchpoint (bs->breakpoint_at));
10221
10222 bpstat_check_watchpoint (bs);
10223 }
10224
10225 /* Implement the "resources_needed" breakpoint_ops method for
10226 hardware watchpoints. */
10227
10228 static int
10229 resources_needed_watchpoint (const struct bp_location *bl)
10230 {
10231 struct watchpoint *w = (struct watchpoint *) bl->owner;
10232 int length = w->exact? 1 : bl->length;
10233
10234 return target_region_ok_for_hw_watchpoint (bl->address, length);
10235 }
10236
10237 /* Implement the "works_in_software_mode" breakpoint_ops method for
10238 hardware watchpoints. */
10239
10240 static int
10241 works_in_software_mode_watchpoint (const struct breakpoint *b)
10242 {
10243 /* Read and access watchpoints only work with hardware support. */
10244 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10245 }
10246
10247 static enum print_stop_action
10248 print_it_watchpoint (bpstat bs)
10249 {
10250 struct breakpoint *b;
10251 enum print_stop_action result;
10252 struct watchpoint *w;
10253 struct ui_out *uiout = current_uiout;
10254
10255 gdb_assert (bs->bp_location_at != NULL);
10256
10257 b = bs->breakpoint_at;
10258 w = (struct watchpoint *) b;
10259
10260 annotate_watchpoint (b->number);
10261 maybe_print_thread_hit_breakpoint (uiout);
10262
10263 string_file stb;
10264
10265 gdb::optional<ui_out_emit_tuple> tuple_emitter;
10266 switch (b->type)
10267 {
10268 case bp_watchpoint:
10269 case bp_hardware_watchpoint:
10270 if (uiout->is_mi_like_p ())
10271 uiout->field_string
10272 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10273 mention (b);
10274 tuple_emitter.emplace (uiout, "value");
10275 uiout->text ("\nOld value = ");
10276 watchpoint_value_print (bs->old_val.get (), &stb);
10277 uiout->field_stream ("old", stb);
10278 uiout->text ("\nNew value = ");
10279 watchpoint_value_print (w->val.get (), &stb);
10280 uiout->field_stream ("new", stb);
10281 uiout->text ("\n");
10282 /* More than one watchpoint may have been triggered. */
10283 result = PRINT_UNKNOWN;
10284 break;
10285
10286 case bp_read_watchpoint:
10287 if (uiout->is_mi_like_p ())
10288 uiout->field_string
10289 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10290 mention (b);
10291 tuple_emitter.emplace (uiout, "value");
10292 uiout->text ("\nValue = ");
10293 watchpoint_value_print (w->val.get (), &stb);
10294 uiout->field_stream ("value", stb);
10295 uiout->text ("\n");
10296 result = PRINT_UNKNOWN;
10297 break;
10298
10299 case bp_access_watchpoint:
10300 if (bs->old_val != NULL)
10301 {
10302 if (uiout->is_mi_like_p ())
10303 uiout->field_string
10304 ("reason",
10305 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10306 mention (b);
10307 tuple_emitter.emplace (uiout, "value");
10308 uiout->text ("\nOld value = ");
10309 watchpoint_value_print (bs->old_val.get (), &stb);
10310 uiout->field_stream ("old", stb);
10311 uiout->text ("\nNew value = ");
10312 }
10313 else
10314 {
10315 mention (b);
10316 if (uiout->is_mi_like_p ())
10317 uiout->field_string
10318 ("reason",
10319 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10320 tuple_emitter.emplace (uiout, "value");
10321 uiout->text ("\nValue = ");
10322 }
10323 watchpoint_value_print (w->val.get (), &stb);
10324 uiout->field_stream ("new", stb);
10325 uiout->text ("\n");
10326 result = PRINT_UNKNOWN;
10327 break;
10328 default:
10329 result = PRINT_UNKNOWN;
10330 }
10331
10332 return result;
10333 }
10334
10335 /* Implement the "print_mention" breakpoint_ops method for hardware
10336 watchpoints. */
10337
10338 static void
10339 print_mention_watchpoint (struct breakpoint *b)
10340 {
10341 struct watchpoint *w = (struct watchpoint *) b;
10342 struct ui_out *uiout = current_uiout;
10343 const char *tuple_name;
10344
10345 switch (b->type)
10346 {
10347 case bp_watchpoint:
10348 uiout->text ("Watchpoint ");
10349 tuple_name = "wpt";
10350 break;
10351 case bp_hardware_watchpoint:
10352 uiout->text ("Hardware watchpoint ");
10353 tuple_name = "wpt";
10354 break;
10355 case bp_read_watchpoint:
10356 uiout->text ("Hardware read watchpoint ");
10357 tuple_name = "hw-rwpt";
10358 break;
10359 case bp_access_watchpoint:
10360 uiout->text ("Hardware access (read/write) watchpoint ");
10361 tuple_name = "hw-awpt";
10362 break;
10363 default:
10364 internal_error (__FILE__, __LINE__,
10365 _("Invalid hardware watchpoint type."));
10366 }
10367
10368 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10369 uiout->field_signed ("number", b->number);
10370 uiout->text (": ");
10371 uiout->field_string ("exp", w->exp_string);
10372 }
10373
10374 /* Implement the "print_recreate" breakpoint_ops method for
10375 watchpoints. */
10376
10377 static void
10378 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10379 {
10380 struct watchpoint *w = (struct watchpoint *) b;
10381
10382 switch (b->type)
10383 {
10384 case bp_watchpoint:
10385 case bp_hardware_watchpoint:
10386 fprintf_unfiltered (fp, "watch");
10387 break;
10388 case bp_read_watchpoint:
10389 fprintf_unfiltered (fp, "rwatch");
10390 break;
10391 case bp_access_watchpoint:
10392 fprintf_unfiltered (fp, "awatch");
10393 break;
10394 default:
10395 internal_error (__FILE__, __LINE__,
10396 _("Invalid watchpoint type."));
10397 }
10398
10399 fprintf_unfiltered (fp, " %s", w->exp_string);
10400 print_recreate_thread (b, fp);
10401 }
10402
10403 /* Implement the "explains_signal" breakpoint_ops method for
10404 watchpoints. */
10405
10406 static int
10407 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10408 {
10409 /* A software watchpoint cannot cause a signal other than
10410 GDB_SIGNAL_TRAP. */
10411 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10412 return 0;
10413
10414 return 1;
10415 }
10416
10417 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10418
10419 static struct breakpoint_ops watchpoint_breakpoint_ops;
10420
10421 /* Implement the "insert" breakpoint_ops method for
10422 masked hardware watchpoints. */
10423
10424 static int
10425 insert_masked_watchpoint (struct bp_location *bl)
10426 {
10427 struct watchpoint *w = (struct watchpoint *) bl->owner;
10428
10429 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10430 bl->watchpoint_type);
10431 }
10432
10433 /* Implement the "remove" breakpoint_ops method for
10434 masked hardware watchpoints. */
10435
10436 static int
10437 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10438 {
10439 struct watchpoint *w = (struct watchpoint *) bl->owner;
10440
10441 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10442 bl->watchpoint_type);
10443 }
10444
10445 /* Implement the "resources_needed" breakpoint_ops method for
10446 masked hardware watchpoints. */
10447
10448 static int
10449 resources_needed_masked_watchpoint (const struct bp_location *bl)
10450 {
10451 struct watchpoint *w = (struct watchpoint *) bl->owner;
10452
10453 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10454 }
10455
10456 /* Implement the "works_in_software_mode" breakpoint_ops method for
10457 masked hardware watchpoints. */
10458
10459 static int
10460 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10461 {
10462 return 0;
10463 }
10464
10465 /* Implement the "print_it" breakpoint_ops method for
10466 masked hardware watchpoints. */
10467
10468 static enum print_stop_action
10469 print_it_masked_watchpoint (bpstat bs)
10470 {
10471 struct breakpoint *b = bs->breakpoint_at;
10472 struct ui_out *uiout = current_uiout;
10473
10474 /* Masked watchpoints have only one location. */
10475 gdb_assert (b->loc && b->loc->next == NULL);
10476
10477 annotate_watchpoint (b->number);
10478 maybe_print_thread_hit_breakpoint (uiout);
10479
10480 switch (b->type)
10481 {
10482 case bp_hardware_watchpoint:
10483 if (uiout->is_mi_like_p ())
10484 uiout->field_string
10485 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10486 break;
10487
10488 case bp_read_watchpoint:
10489 if (uiout->is_mi_like_p ())
10490 uiout->field_string
10491 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10492 break;
10493
10494 case bp_access_watchpoint:
10495 if (uiout->is_mi_like_p ())
10496 uiout->field_string
10497 ("reason",
10498 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10499 break;
10500 default:
10501 internal_error (__FILE__, __LINE__,
10502 _("Invalid hardware watchpoint type."));
10503 }
10504
10505 mention (b);
10506 uiout->text (_("\n\
10507 Check the underlying instruction at PC for the memory\n\
10508 address and value which triggered this watchpoint.\n"));
10509 uiout->text ("\n");
10510
10511 /* More than one watchpoint may have been triggered. */
10512 return PRINT_UNKNOWN;
10513 }
10514
10515 /* Implement the "print_one_detail" breakpoint_ops method for
10516 masked hardware watchpoints. */
10517
10518 static void
10519 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10520 struct ui_out *uiout)
10521 {
10522 struct watchpoint *w = (struct watchpoint *) b;
10523
10524 /* Masked watchpoints have only one location. */
10525 gdb_assert (b->loc && b->loc->next == NULL);
10526
10527 uiout->text ("\tmask ");
10528 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10529 uiout->text ("\n");
10530 }
10531
10532 /* Implement the "print_mention" breakpoint_ops method for
10533 masked hardware watchpoints. */
10534
10535 static void
10536 print_mention_masked_watchpoint (struct breakpoint *b)
10537 {
10538 struct watchpoint *w = (struct watchpoint *) b;
10539 struct ui_out *uiout = current_uiout;
10540 const char *tuple_name;
10541
10542 switch (b->type)
10543 {
10544 case bp_hardware_watchpoint:
10545 uiout->text ("Masked hardware watchpoint ");
10546 tuple_name = "wpt";
10547 break;
10548 case bp_read_watchpoint:
10549 uiout->text ("Masked hardware read watchpoint ");
10550 tuple_name = "hw-rwpt";
10551 break;
10552 case bp_access_watchpoint:
10553 uiout->text ("Masked hardware access (read/write) watchpoint ");
10554 tuple_name = "hw-awpt";
10555 break;
10556 default:
10557 internal_error (__FILE__, __LINE__,
10558 _("Invalid hardware watchpoint type."));
10559 }
10560
10561 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10562 uiout->field_signed ("number", b->number);
10563 uiout->text (": ");
10564 uiout->field_string ("exp", w->exp_string);
10565 }
10566
10567 /* Implement the "print_recreate" breakpoint_ops method for
10568 masked hardware watchpoints. */
10569
10570 static void
10571 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10572 {
10573 struct watchpoint *w = (struct watchpoint *) b;
10574
10575 switch (b->type)
10576 {
10577 case bp_hardware_watchpoint:
10578 fprintf_unfiltered (fp, "watch");
10579 break;
10580 case bp_read_watchpoint:
10581 fprintf_unfiltered (fp, "rwatch");
10582 break;
10583 case bp_access_watchpoint:
10584 fprintf_unfiltered (fp, "awatch");
10585 break;
10586 default:
10587 internal_error (__FILE__, __LINE__,
10588 _("Invalid hardware watchpoint type."));
10589 }
10590
10591 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string,
10592 phex (w->hw_wp_mask, sizeof (CORE_ADDR)));
10593 print_recreate_thread (b, fp);
10594 }
10595
10596 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10597
10598 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10599
10600 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10601
10602 static bool
10603 is_masked_watchpoint (const struct breakpoint *b)
10604 {
10605 return b->ops == &masked_watchpoint_breakpoint_ops;
10606 }
10607
10608 /* accessflag: hw_write: watch write,
10609 hw_read: watch read,
10610 hw_access: watch access (read or write) */
10611 static void
10612 watch_command_1 (const char *arg, int accessflag, int from_tty,
10613 bool just_location, bool internal)
10614 {
10615 struct breakpoint *scope_breakpoint = NULL;
10616 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10617 struct value *result;
10618 int saved_bitpos = 0, saved_bitsize = 0;
10619 const char *exp_start = NULL;
10620 const char *exp_end = NULL;
10621 const char *tok, *end_tok;
10622 int toklen = -1;
10623 const char *cond_start = NULL;
10624 const char *cond_end = NULL;
10625 enum bptype bp_type;
10626 int thread = -1;
10627 /* Flag to indicate whether we are going to use masks for
10628 the hardware watchpoint. */
10629 bool use_mask = false;
10630 CORE_ADDR mask = 0;
10631
10632 /* Make sure that we actually have parameters to parse. */
10633 if (arg != NULL && arg[0] != '\0')
10634 {
10635 const char *value_start;
10636
10637 exp_end = arg + strlen (arg);
10638
10639 /* Look for "parameter value" pairs at the end
10640 of the arguments string. */
10641 for (tok = exp_end - 1; tok > arg; tok--)
10642 {
10643 /* Skip whitespace at the end of the argument list. */
10644 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10645 tok--;
10646
10647 /* Find the beginning of the last token.
10648 This is the value of the parameter. */
10649 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10650 tok--;
10651 value_start = tok + 1;
10652
10653 /* Skip whitespace. */
10654 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10655 tok--;
10656
10657 end_tok = tok;
10658
10659 /* Find the beginning of the second to last token.
10660 This is the parameter itself. */
10661 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10662 tok--;
10663 tok++;
10664 toklen = end_tok - tok + 1;
10665
10666 if (toklen == 6 && startswith (tok, "thread"))
10667 {
10668 struct thread_info *thr;
10669 /* At this point we've found a "thread" token, which means
10670 the user is trying to set a watchpoint that triggers
10671 only in a specific thread. */
10672 const char *endp;
10673
10674 if (thread != -1)
10675 error(_("You can specify only one thread."));
10676
10677 /* Extract the thread ID from the next token. */
10678 thr = parse_thread_id (value_start, &endp);
10679
10680 /* Check if the user provided a valid thread ID. */
10681 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10682 invalid_thread_id_error (value_start);
10683
10684 thread = thr->global_num;
10685 }
10686 else if (toklen == 4 && startswith (tok, "mask"))
10687 {
10688 /* We've found a "mask" token, which means the user wants to
10689 create a hardware watchpoint that is going to have the mask
10690 facility. */
10691 struct value *mask_value, *mark;
10692
10693 if (use_mask)
10694 error(_("You can specify only one mask."));
10695
10696 use_mask = just_location = true;
10697
10698 mark = value_mark ();
10699 mask_value = parse_to_comma_and_eval (&value_start);
10700 mask = value_as_address (mask_value);
10701 value_free_to_mark (mark);
10702 }
10703 else
10704 /* We didn't recognize what we found. We should stop here. */
10705 break;
10706
10707 /* Truncate the string and get rid of the "parameter value" pair before
10708 the arguments string is parsed by the parse_exp_1 function. */
10709 exp_end = tok;
10710 }
10711 }
10712 else
10713 exp_end = arg;
10714
10715 /* Parse the rest of the arguments. From here on out, everything
10716 is in terms of a newly allocated string instead of the original
10717 ARG. */
10718 std::string expression (arg, exp_end - arg);
10719 exp_start = arg = expression.c_str ();
10720 innermost_block_tracker tracker;
10721 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10722 exp_end = arg;
10723 /* Remove trailing whitespace from the expression before saving it.
10724 This makes the eventual display of the expression string a bit
10725 prettier. */
10726 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10727 --exp_end;
10728
10729 /* Checking if the expression is not constant. */
10730 if (watchpoint_exp_is_const (exp.get ()))
10731 {
10732 int len;
10733
10734 len = exp_end - exp_start;
10735 while (len > 0 && isspace (exp_start[len - 1]))
10736 len--;
10737 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10738 }
10739
10740 exp_valid_block = tracker.block ();
10741 struct value *mark = value_mark ();
10742 struct value *val_as_value = nullptr;
10743 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10744 just_location);
10745
10746 if (val_as_value != NULL && just_location)
10747 {
10748 saved_bitpos = value_bitpos (val_as_value);
10749 saved_bitsize = value_bitsize (val_as_value);
10750 }
10751
10752 value_ref_ptr val;
10753 if (just_location)
10754 {
10755 int ret;
10756
10757 exp_valid_block = NULL;
10758 val = release_value (value_addr (result));
10759 value_free_to_mark (mark);
10760
10761 if (use_mask)
10762 {
10763 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10764 mask);
10765 if (ret == -1)
10766 error (_("This target does not support masked watchpoints."));
10767 else if (ret == -2)
10768 error (_("Invalid mask or memory region."));
10769 }
10770 }
10771 else if (val_as_value != NULL)
10772 val = release_value (val_as_value);
10773
10774 tok = skip_spaces (arg);
10775 end_tok = skip_to_space (tok);
10776
10777 toklen = end_tok - tok;
10778 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10779 {
10780 tok = cond_start = end_tok + 1;
10781 innermost_block_tracker if_tracker;
10782 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10783
10784 /* The watchpoint expression may not be local, but the condition
10785 may still be. E.g.: `watch global if local > 0'. */
10786 cond_exp_valid_block = if_tracker.block ();
10787
10788 cond_end = tok;
10789 }
10790 if (*tok)
10791 error (_("Junk at end of command."));
10792
10793 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10794
10795 /* Save this because create_internal_breakpoint below invalidates
10796 'wp_frame'. */
10797 frame_id watchpoint_frame = get_frame_id (wp_frame);
10798
10799 /* If the expression is "local", then set up a "watchpoint scope"
10800 breakpoint at the point where we've left the scope of the watchpoint
10801 expression. Create the scope breakpoint before the watchpoint, so
10802 that we will encounter it first in bpstat_stop_status. */
10803 if (exp_valid_block != NULL && wp_frame != NULL)
10804 {
10805 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10806
10807 if (frame_id_p (caller_frame_id))
10808 {
10809 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10810 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10811
10812 scope_breakpoint
10813 = create_internal_breakpoint (caller_arch, caller_pc,
10814 bp_watchpoint_scope,
10815 &momentary_breakpoint_ops);
10816
10817 /* create_internal_breakpoint could invalidate WP_FRAME. */
10818 wp_frame = NULL;
10819
10820 scope_breakpoint->enable_state = bp_enabled;
10821
10822 /* Automatically delete the breakpoint when it hits. */
10823 scope_breakpoint->disposition = disp_del;
10824
10825 /* Only break in the proper frame (help with recursion). */
10826 scope_breakpoint->frame_id = caller_frame_id;
10827
10828 /* Set the address at which we will stop. */
10829 scope_breakpoint->loc->gdbarch = caller_arch;
10830 scope_breakpoint->loc->requested_address = caller_pc;
10831 scope_breakpoint->loc->address
10832 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10833 scope_breakpoint->loc->requested_address,
10834 scope_breakpoint->type);
10835 }
10836 }
10837
10838 /* Now set up the breakpoint. We create all watchpoints as hardware
10839 watchpoints here even if hardware watchpoints are turned off, a call
10840 to update_watchpoint later in this function will cause the type to
10841 drop back to bp_watchpoint (software watchpoint) if required. */
10842
10843 if (accessflag == hw_read)
10844 bp_type = bp_read_watchpoint;
10845 else if (accessflag == hw_access)
10846 bp_type = bp_access_watchpoint;
10847 else
10848 bp_type = bp_hardware_watchpoint;
10849
10850 std::unique_ptr<watchpoint> w (new watchpoint ());
10851
10852 if (use_mask)
10853 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10854 &masked_watchpoint_breakpoint_ops);
10855 else
10856 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10857 &watchpoint_breakpoint_ops);
10858 w->thread = thread;
10859 w->disposition = disp_donttouch;
10860 w->pspace = current_program_space;
10861 w->exp = std::move (exp);
10862 w->exp_valid_block = exp_valid_block;
10863 w->cond_exp_valid_block = cond_exp_valid_block;
10864 if (just_location)
10865 {
10866 struct type *t = value_type (val.get ());
10867 CORE_ADDR addr = value_as_address (val.get ());
10868
10869 w->exp_string_reparse
10870 = current_language->watch_location_expression (t, addr).release ();
10871
10872 w->exp_string = xstrprintf ("-location %.*s",
10873 (int) (exp_end - exp_start), exp_start);
10874 }
10875 else
10876 w->exp_string = savestring (exp_start, exp_end - exp_start);
10877
10878 if (use_mask)
10879 {
10880 w->hw_wp_mask = mask;
10881 }
10882 else
10883 {
10884 w->val = val;
10885 w->val_bitpos = saved_bitpos;
10886 w->val_bitsize = saved_bitsize;
10887 w->val_valid = true;
10888 }
10889
10890 if (cond_start)
10891 w->cond_string = savestring (cond_start, cond_end - cond_start);
10892 else
10893 w->cond_string = 0;
10894
10895 if (frame_id_p (watchpoint_frame))
10896 {
10897 w->watchpoint_frame = watchpoint_frame;
10898 w->watchpoint_thread = inferior_ptid;
10899 }
10900 else
10901 {
10902 w->watchpoint_frame = null_frame_id;
10903 w->watchpoint_thread = null_ptid;
10904 }
10905
10906 if (scope_breakpoint != NULL)
10907 {
10908 /* The scope breakpoint is related to the watchpoint. We will
10909 need to act on them together. */
10910 w->related_breakpoint = scope_breakpoint;
10911 scope_breakpoint->related_breakpoint = w.get ();
10912 }
10913
10914 if (!just_location)
10915 value_free_to_mark (mark);
10916
10917 /* Finally update the new watchpoint. This creates the locations
10918 that should be inserted. */
10919 update_watchpoint (w.get (), 1);
10920
10921 install_breakpoint (internal, std::move (w), 1);
10922 }
10923
10924 /* Return count of debug registers needed to watch the given expression.
10925 If the watchpoint cannot be handled in hardware return zero. */
10926
10927 static int
10928 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10929 {
10930 int found_memory_cnt = 0;
10931
10932 /* Did the user specifically forbid us to use hardware watchpoints? */
10933 if (!can_use_hw_watchpoints)
10934 return 0;
10935
10936 gdb_assert (!vals.empty ());
10937 struct value *head = vals[0].get ();
10938
10939 /* Make sure that the value of the expression depends only upon
10940 memory contents, and values computed from them within GDB. If we
10941 find any register references or function calls, we can't use a
10942 hardware watchpoint.
10943
10944 The idea here is that evaluating an expression generates a series
10945 of values, one holding the value of every subexpression. (The
10946 expression a*b+c has five subexpressions: a, b, a*b, c, and
10947 a*b+c.) GDB's values hold almost enough information to establish
10948 the criteria given above --- they identify memory lvalues,
10949 register lvalues, computed values, etcetera. So we can evaluate
10950 the expression, and then scan the chain of values that leaves
10951 behind to decide whether we can detect any possible change to the
10952 expression's final value using only hardware watchpoints.
10953
10954 However, I don't think that the values returned by inferior
10955 function calls are special in any way. So this function may not
10956 notice that an expression involving an inferior function call
10957 can't be watched with hardware watchpoints. FIXME. */
10958 for (const value_ref_ptr &iter : vals)
10959 {
10960 struct value *v = iter.get ();
10961
10962 if (VALUE_LVAL (v) == lval_memory)
10963 {
10964 if (v != head && value_lazy (v))
10965 /* A lazy memory lvalue in the chain is one that GDB never
10966 needed to fetch; we either just used its address (e.g.,
10967 `a' in `a.b') or we never needed it at all (e.g., `a'
10968 in `a,b'). This doesn't apply to HEAD; if that is
10969 lazy then it was not readable, but watch it anyway. */
10970 ;
10971 else
10972 {
10973 /* Ahh, memory we actually used! Check if we can cover
10974 it with hardware watchpoints. */
10975 struct type *vtype = check_typedef (value_type (v));
10976
10977 /* We only watch structs and arrays if user asked for it
10978 explicitly, never if they just happen to appear in a
10979 middle of some value chain. */
10980 if (v == head
10981 || (vtype->code () != TYPE_CODE_STRUCT
10982 && vtype->code () != TYPE_CODE_ARRAY))
10983 {
10984 CORE_ADDR vaddr = value_address (v);
10985 int len;
10986 int num_regs;
10987
10988 len = (target_exact_watchpoints
10989 && is_scalar_type_recursive (vtype))?
10990 1 : TYPE_LENGTH (value_type (v));
10991
10992 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10993 if (!num_regs)
10994 return 0;
10995 else
10996 found_memory_cnt += num_regs;
10997 }
10998 }
10999 }
11000 else if (VALUE_LVAL (v) != not_lval
11001 && deprecated_value_modifiable (v) == 0)
11002 return 0; /* These are values from the history (e.g., $1). */
11003 else if (VALUE_LVAL (v) == lval_register)
11004 return 0; /* Cannot watch a register with a HW watchpoint. */
11005 }
11006
11007 /* The expression itself looks suitable for using a hardware
11008 watchpoint, but give the target machine a chance to reject it. */
11009 return found_memory_cnt;
11010 }
11011
11012 void
11013 watch_command_wrapper (const char *arg, int from_tty, bool internal)
11014 {
11015 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11016 }
11017
11018 /* Options for the watch, awatch, and rwatch commands. */
11019
11020 struct watch_options
11021 {
11022 /* For -location. */
11023 bool location = false;
11024 };
11025
11026 /* Definitions of options for the "watch", "awatch", and "rwatch" commands.
11027
11028 Historically GDB always accepted both '-location' and '-l' flags for
11029 these commands (both flags being synonyms). When converting to the
11030 newer option scheme only '-location' is added here. That's fine (for
11031 backward compatibility) as any non-ambiguous prefix of a flag will be
11032 accepted, so '-l', '-loc', are now all accepted.
11033
11034 What this means is that, if in the future, we add any new flag here
11035 that starts with '-l' then this will break backward compatibility, so
11036 please, don't do that! */
11037
11038 static const gdb::option::option_def watch_option_defs[] = {
11039 gdb::option::flag_option_def<watch_options> {
11040 "location",
11041 [] (watch_options *opt) { return &opt->location; },
11042 N_("\
11043 This evaluates EXPRESSION and watches the memory to which is refers.\n\
11044 -l can be used as a short form of -location."),
11045 },
11046 };
11047
11048 /* Returns the option group used by 'watch', 'awatch', and 'rwatch'
11049 commands. */
11050
11051 static gdb::option::option_def_group
11052 make_watch_options_def_group (watch_options *opts)
11053 {
11054 return {{watch_option_defs}, opts};
11055 }
11056
11057 /* A helper function that looks for the "-location" argument and then
11058 calls watch_command_1. */
11059
11060 static void
11061 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
11062 {
11063 watch_options opts;
11064 auto grp = make_watch_options_def_group (&opts);
11065 gdb::option::process_options
11066 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
11067 if (arg != nullptr && *arg == '\0')
11068 arg = nullptr;
11069
11070 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
11071 }
11072
11073 /* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
11074 static void
11075 watch_command_completer (struct cmd_list_element *ignore,
11076 completion_tracker &tracker,
11077 const char *text, const char * /*word*/)
11078 {
11079 const auto group = make_watch_options_def_group (nullptr);
11080 if (gdb::option::complete_options
11081 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
11082 return;
11083
11084 const char *word = advance_to_expression_complete_word_point (tracker, text);
11085 expression_completer (ignore, tracker, text, word);
11086 }
11087
11088 static void
11089 watch_command (const char *arg, int from_tty)
11090 {
11091 watch_maybe_just_location (arg, hw_write, from_tty);
11092 }
11093
11094 void
11095 rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
11096 {
11097 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11098 }
11099
11100 static void
11101 rwatch_command (const char *arg, int from_tty)
11102 {
11103 watch_maybe_just_location (arg, hw_read, from_tty);
11104 }
11105
11106 void
11107 awatch_command_wrapper (const char *arg, int from_tty, bool internal)
11108 {
11109 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11110 }
11111
11112 static void
11113 awatch_command (const char *arg, int from_tty)
11114 {
11115 watch_maybe_just_location (arg, hw_access, from_tty);
11116 }
11117 \f
11118
11119 /* Data for the FSM that manages the until(location)/advance commands
11120 in infcmd.c. Here because it uses the mechanisms of
11121 breakpoints. */
11122
11123 struct until_break_fsm : public thread_fsm
11124 {
11125 /* The thread that was current when the command was executed. */
11126 int thread;
11127
11128 /* The breakpoint set at the return address in the caller frame,
11129 plus breakpoints at all the destination locations. */
11130 std::vector<breakpoint_up> breakpoints;
11131
11132 until_break_fsm (struct interp *cmd_interp, int thread,
11133 std::vector<breakpoint_up> &&breakpoints)
11134 : thread_fsm (cmd_interp),
11135 thread (thread),
11136 breakpoints (std::move (breakpoints))
11137 {
11138 }
11139
11140 void clean_up (struct thread_info *thread) override;
11141 bool should_stop (struct thread_info *thread) override;
11142 enum async_reply_reason do_async_reply_reason () override;
11143 };
11144
11145 /* Implementation of the 'should_stop' FSM method for the
11146 until(location)/advance commands. */
11147
11148 bool
11149 until_break_fsm::should_stop (struct thread_info *tp)
11150 {
11151 for (const breakpoint_up &bp : breakpoints)
11152 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11153 bp.get ()) != NULL)
11154 {
11155 set_finished ();
11156 break;
11157 }
11158
11159 return true;
11160 }
11161
11162 /* Implementation of the 'clean_up' FSM method for the
11163 until(location)/advance commands. */
11164
11165 void
11166 until_break_fsm::clean_up (struct thread_info *)
11167 {
11168 /* Clean up our temporary breakpoints. */
11169 breakpoints.clear ();
11170 delete_longjmp_breakpoint (thread);
11171 }
11172
11173 /* Implementation of the 'async_reply_reason' FSM method for the
11174 until(location)/advance commands. */
11175
11176 enum async_reply_reason
11177 until_break_fsm::do_async_reply_reason ()
11178 {
11179 return EXEC_ASYNC_LOCATION_REACHED;
11180 }
11181
11182 void
11183 until_break_command (const char *arg, int from_tty, int anywhere)
11184 {
11185 struct frame_info *frame;
11186 struct gdbarch *frame_gdbarch;
11187 struct frame_id stack_frame_id;
11188 struct frame_id caller_frame_id;
11189 int thread;
11190 struct thread_info *tp;
11191
11192 clear_proceed_status (0);
11193
11194 /* Set a breakpoint where the user wants it and at return from
11195 this function. */
11196
11197 event_location_up location = string_to_event_location (&arg, current_language);
11198
11199 std::vector<symtab_and_line> sals
11200 = (last_displayed_sal_is_valid ()
11201 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11202 get_last_displayed_symtab (),
11203 get_last_displayed_line ())
11204 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11205 NULL, NULL, 0));
11206
11207 if (sals.empty ())
11208 error (_("Couldn't get information on specified line."));
11209
11210 if (*arg)
11211 error (_("Junk at end of arguments."));
11212
11213 tp = inferior_thread ();
11214 thread = tp->global_num;
11215
11216 /* Note linespec handling above invalidates the frame chain.
11217 Installing a breakpoint also invalidates the frame chain (as it
11218 may need to switch threads), so do any frame handling before
11219 that. */
11220
11221 frame = get_selected_frame (NULL);
11222 frame_gdbarch = get_frame_arch (frame);
11223 stack_frame_id = get_stack_frame_id (frame);
11224 caller_frame_id = frame_unwind_caller_id (frame);
11225
11226 /* Keep within the current frame, or in frames called by the current
11227 one. */
11228
11229 std::vector<breakpoint_up> breakpoints;
11230
11231 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11232
11233 if (frame_id_p (caller_frame_id))
11234 {
11235 struct symtab_and_line sal2;
11236 struct gdbarch *caller_gdbarch;
11237
11238 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11239 sal2.pc = frame_unwind_caller_pc (frame);
11240 caller_gdbarch = frame_unwind_caller_arch (frame);
11241
11242 breakpoint_up caller_breakpoint
11243 = set_momentary_breakpoint (caller_gdbarch, sal2,
11244 caller_frame_id, bp_until);
11245 breakpoints.emplace_back (std::move (caller_breakpoint));
11246
11247 set_longjmp_breakpoint (tp, caller_frame_id);
11248 lj_deleter.emplace (thread);
11249 }
11250
11251 /* set_momentary_breakpoint could invalidate FRAME. */
11252 frame = NULL;
11253
11254 /* If the user told us to continue until a specified location, we
11255 don't specify a frame at which we need to stop. Otherwise,
11256 specify the selected frame, because we want to stop only at the
11257 very same frame. */
11258 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
11259
11260 for (symtab_and_line &sal : sals)
11261 {
11262 resolve_sal_pc (&sal);
11263
11264 breakpoint_up location_breakpoint
11265 = set_momentary_breakpoint (frame_gdbarch, sal,
11266 stop_frame_id, bp_until);
11267 breakpoints.emplace_back (std::move (location_breakpoint));
11268 }
11269
11270 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
11271 std::move (breakpoints));
11272
11273 if (lj_deleter)
11274 lj_deleter->release ();
11275
11276 proceed (-1, GDB_SIGNAL_DEFAULT);
11277 }
11278
11279 /* This function attempts to parse an optional "if <cond>" clause
11280 from the arg string. If one is not found, it returns NULL.
11281
11282 Else, it returns a pointer to the condition string. (It does not
11283 attempt to evaluate the string against a particular block.) And,
11284 it updates arg to point to the first character following the parsed
11285 if clause in the arg string. */
11286
11287 const char *
11288 ep_parse_optional_if_clause (const char **arg)
11289 {
11290 const char *cond_string;
11291
11292 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11293 return NULL;
11294
11295 /* Skip the "if" keyword. */
11296 (*arg) += 2;
11297
11298 /* Skip any extra leading whitespace, and record the start of the
11299 condition string. */
11300 *arg = skip_spaces (*arg);
11301 cond_string = *arg;
11302
11303 /* Assume that the condition occupies the remainder of the arg
11304 string. */
11305 (*arg) += strlen (cond_string);
11306
11307 return cond_string;
11308 }
11309
11310 /* Commands to deal with catching events, such as signals, exceptions,
11311 process start/exit, etc. */
11312
11313 typedef enum
11314 {
11315 catch_fork_temporary, catch_vfork_temporary,
11316 catch_fork_permanent, catch_vfork_permanent
11317 }
11318 catch_fork_kind;
11319
11320 static void
11321 catch_fork_command_1 (const char *arg, int from_tty,
11322 struct cmd_list_element *command)
11323 {
11324 struct gdbarch *gdbarch = get_current_arch ();
11325 const char *cond_string = NULL;
11326 catch_fork_kind fork_kind;
11327
11328 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11329 bool temp = (fork_kind == catch_fork_temporary
11330 || fork_kind == catch_vfork_temporary);
11331
11332 if (!arg)
11333 arg = "";
11334 arg = skip_spaces (arg);
11335
11336 /* The allowed syntax is:
11337 catch [v]fork
11338 catch [v]fork if <cond>
11339
11340 First, check if there's an if clause. */
11341 cond_string = ep_parse_optional_if_clause (&arg);
11342
11343 if ((*arg != '\0') && !isspace (*arg))
11344 error (_("Junk at end of arguments."));
11345
11346 /* If this target supports it, create a fork or vfork catchpoint
11347 and enable reporting of such events. */
11348 switch (fork_kind)
11349 {
11350 case catch_fork_temporary:
11351 case catch_fork_permanent:
11352 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
11353 &catch_fork_breakpoint_ops);
11354 break;
11355 case catch_vfork_temporary:
11356 case catch_vfork_permanent:
11357 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
11358 &catch_vfork_breakpoint_ops);
11359 break;
11360 default:
11361 error (_("unsupported or unknown fork kind; cannot catch it"));
11362 break;
11363 }
11364 }
11365
11366 static void
11367 catch_exec_command_1 (const char *arg, int from_tty,
11368 struct cmd_list_element *command)
11369 {
11370 struct gdbarch *gdbarch = get_current_arch ();
11371 const char *cond_string = NULL;
11372 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
11373
11374 if (!arg)
11375 arg = "";
11376 arg = skip_spaces (arg);
11377
11378 /* The allowed syntax is:
11379 catch exec
11380 catch exec if <cond>
11381
11382 First, check if there's an if clause. */
11383 cond_string = ep_parse_optional_if_clause (&arg);
11384
11385 if ((*arg != '\0') && !isspace (*arg))
11386 error (_("Junk at end of arguments."));
11387
11388 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11389 init_catchpoint (c.get (), gdbarch, temp, cond_string,
11390 &catch_exec_breakpoint_ops);
11391 c->exec_pathname = NULL;
11392
11393 install_breakpoint (0, std::move (c), 1);
11394 }
11395
11396 void
11397 init_ada_exception_breakpoint (struct breakpoint *b,
11398 struct gdbarch *gdbarch,
11399 struct symtab_and_line sal,
11400 const char *addr_string,
11401 const struct breakpoint_ops *ops,
11402 int tempflag,
11403 int enabled,
11404 int from_tty)
11405 {
11406 if (from_tty)
11407 {
11408 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11409 if (!loc_gdbarch)
11410 loc_gdbarch = gdbarch;
11411
11412 describe_other_breakpoints (loc_gdbarch,
11413 sal.pspace, sal.pc, sal.section, -1);
11414 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11415 version for exception catchpoints, because two catchpoints
11416 used for different exception names will use the same address.
11417 In this case, a "breakpoint ... also set at..." warning is
11418 unproductive. Besides, the warning phrasing is also a bit
11419 inappropriate, we should use the word catchpoint, and tell
11420 the user what type of catchpoint it is. The above is good
11421 enough for now, though. */
11422 }
11423
11424 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
11425
11426 b->enable_state = enabled ? bp_enabled : bp_disabled;
11427 b->disposition = tempflag ? disp_del : disp_donttouch;
11428 b->location = string_to_event_location (&addr_string,
11429 language_def (language_ada));
11430 b->language = language_ada;
11431 }
11432
11433 \f
11434
11435 /* Compare two breakpoints and return a strcmp-like result. */
11436
11437 static int
11438 compare_breakpoints (const breakpoint *a, const breakpoint *b)
11439 {
11440 uintptr_t ua = (uintptr_t) a;
11441 uintptr_t ub = (uintptr_t) b;
11442
11443 if (a->number < b->number)
11444 return -1;
11445 else if (a->number > b->number)
11446 return 1;
11447
11448 /* Now sort by address, in case we see, e..g, two breakpoints with
11449 the number 0. */
11450 if (ua < ub)
11451 return -1;
11452 return ua > ub ? 1 : 0;
11453 }
11454
11455 /* Delete breakpoints by address or line. */
11456
11457 static void
11458 clear_command (const char *arg, int from_tty)
11459 {
11460 int default_match;
11461
11462 std::vector<symtab_and_line> decoded_sals;
11463 symtab_and_line last_sal;
11464 gdb::array_view<symtab_and_line> sals;
11465 if (arg)
11466 {
11467 decoded_sals
11468 = decode_line_with_current_source (arg,
11469 (DECODE_LINE_FUNFIRSTLINE
11470 | DECODE_LINE_LIST_MODE));
11471 default_match = 0;
11472 sals = decoded_sals;
11473 }
11474 else
11475 {
11476 /* Set sal's line, symtab, pc, and pspace to the values
11477 corresponding to the last call to print_frame_info. If the
11478 codepoint is not valid, this will set all the fields to 0. */
11479 last_sal = get_last_displayed_sal ();
11480 if (last_sal.symtab == 0)
11481 error (_("No source file specified."));
11482
11483 default_match = 1;
11484 sals = last_sal;
11485 }
11486
11487 /* We don't call resolve_sal_pc here. That's not as bad as it
11488 seems, because all existing breakpoints typically have both
11489 file/line and pc set. So, if clear is given file/line, we can
11490 match this to existing breakpoint without obtaining pc at all.
11491
11492 We only support clearing given the address explicitly
11493 present in breakpoint table. Say, we've set breakpoint
11494 at file:line. There were several PC values for that file:line,
11495 due to optimization, all in one block.
11496
11497 We've picked one PC value. If "clear" is issued with another
11498 PC corresponding to the same file:line, the breakpoint won't
11499 be cleared. We probably can still clear the breakpoint, but
11500 since the other PC value is never presented to user, user
11501 can only find it by guessing, and it does not seem important
11502 to support that. */
11503
11504 /* For each line spec given, delete bps which correspond to it. Do
11505 it in two passes, solely to preserve the current behavior that
11506 from_tty is forced true if we delete more than one
11507 breakpoint. */
11508
11509 std::vector<struct breakpoint *> found;
11510 for (const auto &sal : sals)
11511 {
11512 const char *sal_fullname;
11513
11514 /* If exact pc given, clear bpts at that pc.
11515 If line given (pc == 0), clear all bpts on specified line.
11516 If defaulting, clear all bpts on default line
11517 or at default pc.
11518
11519 defaulting sal.pc != 0 tests to do
11520
11521 0 1 pc
11522 1 1 pc _and_ line
11523 0 0 line
11524 1 0 <can't happen> */
11525
11526 sal_fullname = (sal.symtab == NULL
11527 ? NULL : symtab_to_fullname (sal.symtab));
11528
11529 /* Find all matching breakpoints and add them to 'found'. */
11530 for (breakpoint *b : all_breakpoints ())
11531 {
11532 int match = 0;
11533 /* Are we going to delete b? */
11534 if (b->type != bp_none && !is_watchpoint (b))
11535 {
11536 struct bp_location *loc = b->loc;
11537 for (; loc; loc = loc->next)
11538 {
11539 /* If the user specified file:line, don't allow a PC
11540 match. This matches historical gdb behavior. */
11541 int pc_match = (!sal.explicit_line
11542 && sal.pc
11543 && (loc->pspace == sal.pspace)
11544 && (loc->address == sal.pc)
11545 && (!section_is_overlay (loc->section)
11546 || loc->section == sal.section));
11547 int line_match = 0;
11548
11549 if ((default_match || sal.explicit_line)
11550 && loc->symtab != NULL
11551 && sal_fullname != NULL
11552 && sal.pspace == loc->pspace
11553 && loc->line_number == sal.line
11554 && filename_cmp (symtab_to_fullname (loc->symtab),
11555 sal_fullname) == 0)
11556 line_match = 1;
11557
11558 if (pc_match || line_match)
11559 {
11560 match = 1;
11561 break;
11562 }
11563 }
11564 }
11565
11566 if (match)
11567 found.push_back (b);
11568 }
11569 }
11570
11571 /* Now go thru the 'found' chain and delete them. */
11572 if (found.empty ())
11573 {
11574 if (arg)
11575 error (_("No breakpoint at %s."), arg);
11576 else
11577 error (_("No breakpoint at this line."));
11578 }
11579
11580 /* Remove duplicates from the vec. */
11581 std::sort (found.begin (), found.end (),
11582 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11583 {
11584 return compare_breakpoints (bp_a, bp_b) < 0;
11585 });
11586 found.erase (std::unique (found.begin (), found.end (),
11587 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11588 {
11589 return compare_breakpoints (bp_a, bp_b) == 0;
11590 }),
11591 found.end ());
11592
11593 if (found.size () > 1)
11594 from_tty = 1; /* Always report if deleted more than one. */
11595 if (from_tty)
11596 {
11597 if (found.size () == 1)
11598 printf_unfiltered (_("Deleted breakpoint "));
11599 else
11600 printf_unfiltered (_("Deleted breakpoints "));
11601 }
11602
11603 for (breakpoint *iter : found)
11604 {
11605 if (from_tty)
11606 printf_unfiltered ("%d ", iter->number);
11607 delete_breakpoint (iter);
11608 }
11609 if (from_tty)
11610 putchar_unfiltered ('\n');
11611 }
11612 \f
11613 /* Delete breakpoint in BS if they are `delete' breakpoints and
11614 all breakpoints that are marked for deletion, whether hit or not.
11615 This is called after any breakpoint is hit, or after errors. */
11616
11617 void
11618 breakpoint_auto_delete (bpstat bs)
11619 {
11620 for (; bs; bs = bs->next)
11621 if (bs->breakpoint_at
11622 && bs->breakpoint_at->disposition == disp_del
11623 && bs->stop)
11624 delete_breakpoint (bs->breakpoint_at);
11625
11626 for (breakpoint *b : all_breakpoints_safe ())
11627 if (b->disposition == disp_del_at_next_stop)
11628 delete_breakpoint (b);
11629 }
11630
11631 /* A comparison function for bp_location AP and BP being interfaced to
11632 std::sort. Sort elements primarily by their ADDRESS (no matter what
11633 bl_address_is_meaningful says), secondarily by ordering first
11634 permanent elements and terciarily just ensuring the array is sorted
11635 stable way despite std::sort being an unstable algorithm. */
11636
11637 static int
11638 bp_location_is_less_than (const bp_location *a, const bp_location *b)
11639 {
11640 if (a->address != b->address)
11641 return a->address < b->address;
11642
11643 /* Sort locations at the same address by their pspace number, keeping
11644 locations of the same inferior (in a multi-inferior environment)
11645 grouped. */
11646
11647 if (a->pspace->num != b->pspace->num)
11648 return a->pspace->num < b->pspace->num;
11649
11650 /* Sort permanent breakpoints first. */
11651 if (a->permanent != b->permanent)
11652 return a->permanent > b->permanent;
11653
11654 /* Sort by type in order to make duplicate determination easier.
11655 See update_global_location_list. This is kept in sync with
11656 breakpoint_locations_match. */
11657 if (a->loc_type < b->loc_type)
11658 return true;
11659
11660 /* Likewise, for range-breakpoints, sort by length. */
11661 if (a->loc_type == bp_loc_hardware_breakpoint
11662 && b->loc_type == bp_loc_hardware_breakpoint
11663 && a->length < b->length)
11664 return true;
11665
11666 /* Make the internal GDB representation stable across GDB runs
11667 where A and B memory inside GDB can differ. Breakpoint locations of
11668 the same type at the same address can be sorted in arbitrary order. */
11669
11670 if (a->owner->number != b->owner->number)
11671 return a->owner->number < b->owner->number;
11672
11673 return a < b;
11674 }
11675
11676 /* Set bp_locations_placed_address_before_address_max and
11677 bp_locations_shadow_len_after_address_max according to the current
11678 content of the bp_locations array. */
11679
11680 static void
11681 bp_locations_target_extensions_update (void)
11682 {
11683 struct bp_location *bl, **blp_tmp;
11684
11685 bp_locations_placed_address_before_address_max = 0;
11686 bp_locations_shadow_len_after_address_max = 0;
11687
11688 ALL_BP_LOCATIONS (bl, blp_tmp)
11689 {
11690 CORE_ADDR start, end, addr;
11691
11692 if (!bp_location_has_shadow (bl))
11693 continue;
11694
11695 start = bl->target_info.placed_address;
11696 end = start + bl->target_info.shadow_len;
11697
11698 gdb_assert (bl->address >= start);
11699 addr = bl->address - start;
11700 if (addr > bp_locations_placed_address_before_address_max)
11701 bp_locations_placed_address_before_address_max = addr;
11702
11703 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11704
11705 gdb_assert (bl->address < end);
11706 addr = end - bl->address;
11707 if (addr > bp_locations_shadow_len_after_address_max)
11708 bp_locations_shadow_len_after_address_max = addr;
11709 }
11710 }
11711
11712 /* Download tracepoint locations if they haven't been. */
11713
11714 static void
11715 download_tracepoint_locations (void)
11716 {
11717 struct breakpoint *b;
11718 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11719
11720 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11721
11722 ALL_TRACEPOINTS (b)
11723 {
11724 struct bp_location *bl;
11725 struct tracepoint *t;
11726 int bp_location_downloaded = 0;
11727
11728 if ((b->type == bp_fast_tracepoint
11729 ? !may_insert_fast_tracepoints
11730 : !may_insert_tracepoints))
11731 continue;
11732
11733 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11734 {
11735 if (target_can_download_tracepoint ())
11736 can_download_tracepoint = TRIBOOL_TRUE;
11737 else
11738 can_download_tracepoint = TRIBOOL_FALSE;
11739 }
11740
11741 if (can_download_tracepoint == TRIBOOL_FALSE)
11742 break;
11743
11744 for (bl = b->loc; bl; bl = bl->next)
11745 {
11746 /* In tracepoint, locations are _never_ duplicated, so
11747 should_be_inserted is equivalent to
11748 unduplicated_should_be_inserted. */
11749 if (!should_be_inserted (bl) || bl->inserted)
11750 continue;
11751
11752 switch_to_program_space_and_thread (bl->pspace);
11753
11754 target_download_tracepoint (bl);
11755
11756 bl->inserted = 1;
11757 bp_location_downloaded = 1;
11758 }
11759 t = (struct tracepoint *) b;
11760 t->number_on_target = b->number;
11761 if (bp_location_downloaded)
11762 gdb::observers::breakpoint_modified.notify (b);
11763 }
11764 }
11765
11766 /* Swap the insertion/duplication state between two locations. */
11767
11768 static void
11769 swap_insertion (struct bp_location *left, struct bp_location *right)
11770 {
11771 const int left_inserted = left->inserted;
11772 const int left_duplicate = left->duplicate;
11773 const int left_needs_update = left->needs_update;
11774 const struct bp_target_info left_target_info = left->target_info;
11775
11776 /* Locations of tracepoints can never be duplicated. */
11777 if (is_tracepoint (left->owner))
11778 gdb_assert (!left->duplicate);
11779 if (is_tracepoint (right->owner))
11780 gdb_assert (!right->duplicate);
11781
11782 left->inserted = right->inserted;
11783 left->duplicate = right->duplicate;
11784 left->needs_update = right->needs_update;
11785 left->target_info = right->target_info;
11786 right->inserted = left_inserted;
11787 right->duplicate = left_duplicate;
11788 right->needs_update = left_needs_update;
11789 right->target_info = left_target_info;
11790 }
11791
11792 /* Force the re-insertion of the locations at ADDRESS. This is called
11793 once a new/deleted/modified duplicate location is found and we are evaluating
11794 conditions on the target's side. Such conditions need to be updated on
11795 the target. */
11796
11797 static void
11798 force_breakpoint_reinsertion (struct bp_location *bl)
11799 {
11800 struct bp_location **locp = NULL, **loc2p;
11801 struct bp_location *loc;
11802 CORE_ADDR address = 0;
11803 int pspace_num;
11804
11805 address = bl->address;
11806 pspace_num = bl->pspace->num;
11807
11808 /* This is only meaningful if the target is
11809 evaluating conditions and if the user has
11810 opted for condition evaluation on the target's
11811 side. */
11812 if (gdb_evaluates_breakpoint_condition_p ()
11813 || !target_supports_evaluation_of_breakpoint_conditions ())
11814 return;
11815
11816 /* Flag all breakpoint locations with this address and
11817 the same program space as the location
11818 as "its condition has changed". We need to
11819 update the conditions on the target's side. */
11820 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11821 {
11822 loc = *loc2p;
11823
11824 if (!is_breakpoint (loc->owner)
11825 || pspace_num != loc->pspace->num)
11826 continue;
11827
11828 /* Flag the location appropriately. We use a different state to
11829 let everyone know that we already updated the set of locations
11830 with addr bl->address and program space bl->pspace. This is so
11831 we don't have to keep calling these functions just to mark locations
11832 that have already been marked. */
11833 loc->condition_changed = condition_updated;
11834
11835 /* Free the agent expression bytecode as well. We will compute
11836 it later on. */
11837 loc->cond_bytecode.reset ();
11838 }
11839 }
11840
11841 /* Called whether new breakpoints are created, or existing breakpoints
11842 deleted, to update the global location list and recompute which
11843 locations are duplicate of which.
11844
11845 The INSERT_MODE flag determines whether locations may not, may, or
11846 shall be inserted now. See 'enum ugll_insert_mode' for more
11847 info. */
11848
11849 static void
11850 update_global_location_list (enum ugll_insert_mode insert_mode)
11851 {
11852 struct bp_location **locp, *loc;
11853 /* Last breakpoint location address that was marked for update. */
11854 CORE_ADDR last_addr = 0;
11855 /* Last breakpoint location program space that was marked for update. */
11856 int last_pspace_num = -1;
11857
11858 /* Used in the duplicates detection below. When iterating over all
11859 bp_locations, points to the first bp_location of a given address.
11860 Breakpoints and watchpoints of different types are never
11861 duplicates of each other. Keep one pointer for each type of
11862 breakpoint/watchpoint, so we only need to loop over all locations
11863 once. */
11864 struct bp_location *bp_loc_first; /* breakpoint */
11865 struct bp_location *wp_loc_first; /* hardware watchpoint */
11866 struct bp_location *awp_loc_first; /* access watchpoint */
11867 struct bp_location *rwp_loc_first; /* read watchpoint */
11868
11869 /* Saved former bp_locations array which we compare against the newly
11870 built bp_locations from the current state of ALL_BREAKPOINTS. */
11871 struct bp_location **old_locp;
11872 unsigned old_locations_count;
11873 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
11874
11875 old_locations_count = bp_locations_count;
11876 bp_locations = NULL;
11877 bp_locations_count = 0;
11878
11879 for (breakpoint *b : all_breakpoints ())
11880 for (loc = b->loc; loc; loc = loc->next)
11881 bp_locations_count++;
11882
11883 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11884 locp = bp_locations;
11885 for (breakpoint *b : all_breakpoints ())
11886 for (loc = b->loc; loc; loc = loc->next)
11887 *locp++ = loc;
11888
11889 /* See if we need to "upgrade" a software breakpoint to a hardware
11890 breakpoint. Do this before deciding whether locations are
11891 duplicates. Also do this before sorting because sorting order
11892 depends on location type. */
11893 for (locp = bp_locations;
11894 locp < bp_locations + bp_locations_count;
11895 locp++)
11896 {
11897 loc = *locp;
11898 if (!loc->inserted && should_be_inserted (loc))
11899 handle_automatic_hardware_breakpoints (loc);
11900 }
11901
11902 std::sort (bp_locations, bp_locations + bp_locations_count,
11903 bp_location_is_less_than);
11904
11905 bp_locations_target_extensions_update ();
11906
11907 /* Identify bp_location instances that are no longer present in the
11908 new list, and therefore should be freed. Note that it's not
11909 necessary that those locations should be removed from inferior --
11910 if there's another location at the same address (previously
11911 marked as duplicate), we don't need to remove/insert the
11912 location.
11913
11914 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11915 and former bp_location array state respectively. */
11916
11917 locp = bp_locations;
11918 for (old_locp = old_locations.get ();
11919 old_locp < old_locations.get () + old_locations_count;
11920 old_locp++)
11921 {
11922 struct bp_location *old_loc = *old_locp;
11923 struct bp_location **loc2p;
11924
11925 /* Tells if 'old_loc' is found among the new locations. If
11926 not, we have to free it. */
11927 int found_object = 0;
11928 /* Tells if the location should remain inserted in the target. */
11929 int keep_in_target = 0;
11930 int removed = 0;
11931
11932 /* Skip LOCP entries which will definitely never be needed.
11933 Stop either at or being the one matching OLD_LOC. */
11934 while (locp < bp_locations + bp_locations_count
11935 && (*locp)->address < old_loc->address)
11936 locp++;
11937
11938 for (loc2p = locp;
11939 (loc2p < bp_locations + bp_locations_count
11940 && (*loc2p)->address == old_loc->address);
11941 loc2p++)
11942 {
11943 /* Check if this is a new/duplicated location or a duplicated
11944 location that had its condition modified. If so, we want to send
11945 its condition to the target if evaluation of conditions is taking
11946 place there. */
11947 if ((*loc2p)->condition_changed == condition_modified
11948 && (last_addr != old_loc->address
11949 || last_pspace_num != old_loc->pspace->num))
11950 {
11951 force_breakpoint_reinsertion (*loc2p);
11952 last_pspace_num = old_loc->pspace->num;
11953 }
11954
11955 if (*loc2p == old_loc)
11956 found_object = 1;
11957 }
11958
11959 /* We have already handled this address, update it so that we don't
11960 have to go through updates again. */
11961 last_addr = old_loc->address;
11962
11963 /* Target-side condition evaluation: Handle deleted locations. */
11964 if (!found_object)
11965 force_breakpoint_reinsertion (old_loc);
11966
11967 /* If this location is no longer present, and inserted, look if
11968 there's maybe a new location at the same address. If so,
11969 mark that one inserted, and don't remove this one. This is
11970 needed so that we don't have a time window where a breakpoint
11971 at certain location is not inserted. */
11972
11973 if (old_loc->inserted)
11974 {
11975 /* If the location is inserted now, we might have to remove
11976 it. */
11977
11978 if (found_object && should_be_inserted (old_loc))
11979 {
11980 /* The location is still present in the location list,
11981 and still should be inserted. Don't do anything. */
11982 keep_in_target = 1;
11983 }
11984 else
11985 {
11986 /* This location still exists, but it won't be kept in the
11987 target since it may have been disabled. We proceed to
11988 remove its target-side condition. */
11989
11990 /* The location is either no longer present, or got
11991 disabled. See if there's another location at the
11992 same address, in which case we don't need to remove
11993 this one from the target. */
11994
11995 /* OLD_LOC comes from existing struct breakpoint. */
11996 if (bl_address_is_meaningful (old_loc))
11997 {
11998 for (loc2p = locp;
11999 (loc2p < bp_locations + bp_locations_count
12000 && (*loc2p)->address == old_loc->address);
12001 loc2p++)
12002 {
12003 struct bp_location *loc2 = *loc2p;
12004
12005 if (loc2 == old_loc)
12006 continue;
12007
12008 if (breakpoint_locations_match (loc2, old_loc))
12009 {
12010 /* Read watchpoint locations are switched to
12011 access watchpoints, if the former are not
12012 supported, but the latter are. */
12013 if (is_hardware_watchpoint (old_loc->owner))
12014 {
12015 gdb_assert (is_hardware_watchpoint (loc2->owner));
12016 loc2->watchpoint_type = old_loc->watchpoint_type;
12017 }
12018
12019 /* loc2 is a duplicated location. We need to check
12020 if it should be inserted in case it will be
12021 unduplicated. */
12022 if (unduplicated_should_be_inserted (loc2))
12023 {
12024 swap_insertion (old_loc, loc2);
12025 keep_in_target = 1;
12026 break;
12027 }
12028 }
12029 }
12030 }
12031 }
12032
12033 if (!keep_in_target)
12034 {
12035 if (remove_breakpoint (old_loc))
12036 {
12037 /* This is just about all we can do. We could keep
12038 this location on the global list, and try to
12039 remove it next time, but there's no particular
12040 reason why we will succeed next time.
12041
12042 Note that at this point, old_loc->owner is still
12043 valid, as delete_breakpoint frees the breakpoint
12044 only after calling us. */
12045 printf_filtered (_("warning: Error removing "
12046 "breakpoint %d\n"),
12047 old_loc->owner->number);
12048 }
12049 removed = 1;
12050 }
12051 }
12052
12053 if (!found_object)
12054 {
12055 if (removed && target_is_non_stop_p ()
12056 && need_moribund_for_location_type (old_loc))
12057 {
12058 /* This location was removed from the target. In
12059 non-stop mode, a race condition is possible where
12060 we've removed a breakpoint, but stop events for that
12061 breakpoint are already queued and will arrive later.
12062 We apply an heuristic to be able to distinguish such
12063 SIGTRAPs from other random SIGTRAPs: we keep this
12064 breakpoint location for a bit, and will retire it
12065 after we see some number of events. The theory here
12066 is that reporting of events should, "on the average",
12067 be fair, so after a while we'll see events from all
12068 threads that have anything of interest, and no longer
12069 need to keep this breakpoint location around. We
12070 don't hold locations forever so to reduce chances of
12071 mistaking a non-breakpoint SIGTRAP for a breakpoint
12072 SIGTRAP.
12073
12074 The heuristic failing can be disastrous on
12075 decr_pc_after_break targets.
12076
12077 On decr_pc_after_break targets, like e.g., x86-linux,
12078 if we fail to recognize a late breakpoint SIGTRAP,
12079 because events_till_retirement has reached 0 too
12080 soon, we'll fail to do the PC adjustment, and report
12081 a random SIGTRAP to the user. When the user resumes
12082 the inferior, it will most likely immediately crash
12083 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12084 corrupted, because of being resumed e.g., in the
12085 middle of a multi-byte instruction, or skipped a
12086 one-byte instruction. This was actually seen happen
12087 on native x86-linux, and should be less rare on
12088 targets that do not support new thread events, like
12089 remote, due to the heuristic depending on
12090 thread_count.
12091
12092 Mistaking a random SIGTRAP for a breakpoint trap
12093 causes similar symptoms (PC adjustment applied when
12094 it shouldn't), but then again, playing with SIGTRAPs
12095 behind the debugger's back is asking for trouble.
12096
12097 Since hardware watchpoint traps are always
12098 distinguishable from other traps, so we don't need to
12099 apply keep hardware watchpoint moribund locations
12100 around. We simply always ignore hardware watchpoint
12101 traps we can no longer explain. */
12102
12103 process_stratum_target *proc_target = nullptr;
12104 for (inferior *inf : all_inferiors ())
12105 if (inf->pspace == old_loc->pspace)
12106 {
12107 proc_target = inf->process_target ();
12108 break;
12109 }
12110 if (proc_target != nullptr)
12111 old_loc->events_till_retirement
12112 = 3 * (thread_count (proc_target) + 1);
12113 else
12114 old_loc->events_till_retirement = 1;
12115 old_loc->owner = NULL;
12116
12117 moribund_locations.push_back (old_loc);
12118 }
12119 else
12120 {
12121 old_loc->owner = NULL;
12122 decref_bp_location (&old_loc);
12123 }
12124 }
12125 }
12126
12127 /* Rescan breakpoints at the same address and section, marking the
12128 first one as "first" and any others as "duplicates". This is so
12129 that the bpt instruction is only inserted once. If we have a
12130 permanent breakpoint at the same place as BPT, make that one the
12131 official one, and the rest as duplicates. Permanent breakpoints
12132 are sorted first for the same address.
12133
12134 Do the same for hardware watchpoints, but also considering the
12135 watchpoint's type (regular/access/read) and length. */
12136
12137 bp_loc_first = NULL;
12138 wp_loc_first = NULL;
12139 awp_loc_first = NULL;
12140 rwp_loc_first = NULL;
12141 ALL_BP_LOCATIONS (loc, locp)
12142 {
12143 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12144 non-NULL. */
12145 struct bp_location **loc_first_p;
12146 breakpoint *b = loc->owner;
12147
12148 if (!unduplicated_should_be_inserted (loc)
12149 || !bl_address_is_meaningful (loc)
12150 /* Don't detect duplicate for tracepoint locations because they are
12151 never duplicated. See the comments in field `duplicate' of
12152 `struct bp_location'. */
12153 || is_tracepoint (b))
12154 {
12155 /* Clear the condition modification flag. */
12156 loc->condition_changed = condition_unchanged;
12157 continue;
12158 }
12159
12160 if (b->type == bp_hardware_watchpoint)
12161 loc_first_p = &wp_loc_first;
12162 else if (b->type == bp_read_watchpoint)
12163 loc_first_p = &rwp_loc_first;
12164 else if (b->type == bp_access_watchpoint)
12165 loc_first_p = &awp_loc_first;
12166 else
12167 loc_first_p = &bp_loc_first;
12168
12169 if (*loc_first_p == NULL
12170 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12171 || !breakpoint_locations_match (loc, *loc_first_p))
12172 {
12173 *loc_first_p = loc;
12174 loc->duplicate = 0;
12175
12176 if (is_breakpoint (loc->owner) && loc->condition_changed)
12177 {
12178 loc->needs_update = 1;
12179 /* Clear the condition modification flag. */
12180 loc->condition_changed = condition_unchanged;
12181 }
12182 continue;
12183 }
12184
12185
12186 /* This and the above ensure the invariant that the first location
12187 is not duplicated, and is the inserted one.
12188 All following are marked as duplicated, and are not inserted. */
12189 if (loc->inserted)
12190 swap_insertion (loc, *loc_first_p);
12191 loc->duplicate = 1;
12192
12193 /* Clear the condition modification flag. */
12194 loc->condition_changed = condition_unchanged;
12195 }
12196
12197 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12198 {
12199 if (insert_mode != UGLL_DONT_INSERT)
12200 insert_breakpoint_locations ();
12201 else
12202 {
12203 /* Even though the caller told us to not insert new
12204 locations, we may still need to update conditions on the
12205 target's side of breakpoints that were already inserted
12206 if the target is evaluating breakpoint conditions. We
12207 only update conditions for locations that are marked
12208 "needs_update". */
12209 update_inserted_breakpoint_locations ();
12210 }
12211 }
12212
12213 if (insert_mode != UGLL_DONT_INSERT)
12214 download_tracepoint_locations ();
12215 }
12216
12217 void
12218 breakpoint_retire_moribund (void)
12219 {
12220 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12221 {
12222 struct bp_location *loc = moribund_locations[ix];
12223 if (--(loc->events_till_retirement) == 0)
12224 {
12225 decref_bp_location (&loc);
12226 unordered_remove (moribund_locations, ix);
12227 --ix;
12228 }
12229 }
12230 }
12231
12232 static void
12233 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12234 {
12235
12236 try
12237 {
12238 update_global_location_list (insert_mode);
12239 }
12240 catch (const gdb_exception_error &e)
12241 {
12242 }
12243 }
12244
12245 /* Clear BKP from a BPS. */
12246
12247 static void
12248 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12249 {
12250 bpstat bs;
12251
12252 for (bs = bps; bs; bs = bs->next)
12253 if (bs->breakpoint_at == bpt)
12254 {
12255 bs->breakpoint_at = NULL;
12256 bs->old_val = NULL;
12257 /* bs->commands will be freed later. */
12258 }
12259 }
12260
12261 /* Callback for iterate_over_threads. */
12262 static int
12263 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12264 {
12265 struct breakpoint *bpt = (struct breakpoint *) data;
12266
12267 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12268 return 0;
12269 }
12270
12271 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12272 callbacks. */
12273
12274 static void
12275 say_where (struct breakpoint *b)
12276 {
12277 struct value_print_options opts;
12278
12279 get_user_print_options (&opts);
12280
12281 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12282 single string. */
12283 if (b->loc == NULL)
12284 {
12285 /* For pending locations, the output differs slightly based
12286 on b->extra_string. If this is non-NULL, it contains either
12287 a condition or dprintf arguments. */
12288 if (b->extra_string == NULL)
12289 {
12290 printf_filtered (_(" (%s) pending."),
12291 event_location_to_string (b->location.get ()));
12292 }
12293 else if (b->type == bp_dprintf)
12294 {
12295 printf_filtered (_(" (%s,%s) pending."),
12296 event_location_to_string (b->location.get ()),
12297 b->extra_string);
12298 }
12299 else
12300 {
12301 printf_filtered (_(" (%s %s) pending."),
12302 event_location_to_string (b->location.get ()),
12303 b->extra_string);
12304 }
12305 }
12306 else
12307 {
12308 if (opts.addressprint || b->loc->symtab == NULL)
12309 printf_filtered (" at %ps",
12310 styled_string (address_style.style (),
12311 paddress (b->loc->gdbarch,
12312 b->loc->address)));
12313 if (b->loc->symtab != NULL)
12314 {
12315 /* If there is a single location, we can print the location
12316 more nicely. */
12317 if (b->loc->next == NULL)
12318 {
12319 const char *filename
12320 = symtab_to_filename_for_display (b->loc->symtab);
12321 printf_filtered (": file %ps, line %d.",
12322 styled_string (file_name_style.style (),
12323 filename),
12324 b->loc->line_number);
12325 }
12326 else
12327 /* This is not ideal, but each location may have a
12328 different file name, and this at least reflects the
12329 real situation somewhat. */
12330 printf_filtered (": %s.",
12331 event_location_to_string (b->location.get ()));
12332 }
12333
12334 if (b->loc->next)
12335 {
12336 struct bp_location *loc = b->loc;
12337 int n = 0;
12338 for (; loc; loc = loc->next)
12339 ++n;
12340 printf_filtered (" (%d locations)", n);
12341 }
12342 }
12343 }
12344
12345 bp_location::~bp_location ()
12346 {
12347 xfree (function_name);
12348 }
12349
12350 /* Destructor for the breakpoint base class. */
12351
12352 breakpoint::~breakpoint ()
12353 {
12354 xfree (this->cond_string);
12355 xfree (this->extra_string);
12356 }
12357
12358 static struct bp_location *
12359 base_breakpoint_allocate_location (struct breakpoint *self)
12360 {
12361 return new bp_location (self);
12362 }
12363
12364 static void
12365 base_breakpoint_re_set (struct breakpoint *b)
12366 {
12367 /* Nothing to re-set. */
12368 }
12369
12370 #define internal_error_pure_virtual_called() \
12371 gdb_assert_not_reached ("pure virtual function called")
12372
12373 static int
12374 base_breakpoint_insert_location (struct bp_location *bl)
12375 {
12376 internal_error_pure_virtual_called ();
12377 }
12378
12379 static int
12380 base_breakpoint_remove_location (struct bp_location *bl,
12381 enum remove_bp_reason reason)
12382 {
12383 internal_error_pure_virtual_called ();
12384 }
12385
12386 static int
12387 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12388 const address_space *aspace,
12389 CORE_ADDR bp_addr,
12390 const struct target_waitstatus *ws)
12391 {
12392 internal_error_pure_virtual_called ();
12393 }
12394
12395 static void
12396 base_breakpoint_check_status (bpstat bs)
12397 {
12398 /* Always stop. */
12399 }
12400
12401 /* A "works_in_software_mode" breakpoint_ops method that just internal
12402 errors. */
12403
12404 static int
12405 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12406 {
12407 internal_error_pure_virtual_called ();
12408 }
12409
12410 /* A "resources_needed" breakpoint_ops method that just internal
12411 errors. */
12412
12413 static int
12414 base_breakpoint_resources_needed (const struct bp_location *bl)
12415 {
12416 internal_error_pure_virtual_called ();
12417 }
12418
12419 static enum print_stop_action
12420 base_breakpoint_print_it (bpstat bs)
12421 {
12422 internal_error_pure_virtual_called ();
12423 }
12424
12425 static void
12426 base_breakpoint_print_one_detail (const struct breakpoint *self,
12427 struct ui_out *uiout)
12428 {
12429 /* nothing */
12430 }
12431
12432 static void
12433 base_breakpoint_print_mention (struct breakpoint *b)
12434 {
12435 internal_error_pure_virtual_called ();
12436 }
12437
12438 static void
12439 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12440 {
12441 internal_error_pure_virtual_called ();
12442 }
12443
12444 static void
12445 base_breakpoint_create_sals_from_location
12446 (struct event_location *location,
12447 struct linespec_result *canonical,
12448 enum bptype type_wanted)
12449 {
12450 internal_error_pure_virtual_called ();
12451 }
12452
12453 static void
12454 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12455 struct linespec_result *c,
12456 gdb::unique_xmalloc_ptr<char> cond_string,
12457 gdb::unique_xmalloc_ptr<char> extra_string,
12458 enum bptype type_wanted,
12459 enum bpdisp disposition,
12460 int thread,
12461 int task, int ignore_count,
12462 const struct breakpoint_ops *o,
12463 int from_tty, int enabled,
12464 int internal, unsigned flags)
12465 {
12466 internal_error_pure_virtual_called ();
12467 }
12468
12469 static std::vector<symtab_and_line>
12470 base_breakpoint_decode_location (struct breakpoint *b,
12471 struct event_location *location,
12472 struct program_space *search_pspace)
12473 {
12474 internal_error_pure_virtual_called ();
12475 }
12476
12477 /* The default 'explains_signal' method. */
12478
12479 static int
12480 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12481 {
12482 return 1;
12483 }
12484
12485 /* The default "after_condition_true" method. */
12486
12487 static void
12488 base_breakpoint_after_condition_true (struct bpstats *bs)
12489 {
12490 /* Nothing to do. */
12491 }
12492
12493 struct breakpoint_ops base_breakpoint_ops =
12494 {
12495 base_breakpoint_allocate_location,
12496 base_breakpoint_re_set,
12497 base_breakpoint_insert_location,
12498 base_breakpoint_remove_location,
12499 base_breakpoint_breakpoint_hit,
12500 base_breakpoint_check_status,
12501 base_breakpoint_resources_needed,
12502 base_breakpoint_works_in_software_mode,
12503 base_breakpoint_print_it,
12504 NULL,
12505 base_breakpoint_print_one_detail,
12506 base_breakpoint_print_mention,
12507 base_breakpoint_print_recreate,
12508 base_breakpoint_create_sals_from_location,
12509 base_breakpoint_create_breakpoints_sal,
12510 base_breakpoint_decode_location,
12511 base_breakpoint_explains_signal,
12512 base_breakpoint_after_condition_true,
12513 };
12514
12515 /* Default breakpoint_ops methods. */
12516
12517 static void
12518 bkpt_re_set (struct breakpoint *b)
12519 {
12520 /* FIXME: is this still reachable? */
12521 if (breakpoint_event_location_empty_p (b))
12522 {
12523 /* Anything without a location can't be re-set. */
12524 delete_breakpoint (b);
12525 return;
12526 }
12527
12528 breakpoint_re_set_default (b);
12529 }
12530
12531 static int
12532 bkpt_insert_location (struct bp_location *bl)
12533 {
12534 CORE_ADDR addr = bl->target_info.reqstd_address;
12535
12536 bl->target_info.kind = breakpoint_kind (bl, &addr);
12537 bl->target_info.placed_address = addr;
12538
12539 if (bl->loc_type == bp_loc_hardware_breakpoint)
12540 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12541 else
12542 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12543 }
12544
12545 static int
12546 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12547 {
12548 if (bl->loc_type == bp_loc_hardware_breakpoint)
12549 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12550 else
12551 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12552 }
12553
12554 static int
12555 bkpt_breakpoint_hit (const struct bp_location *bl,
12556 const address_space *aspace, CORE_ADDR bp_addr,
12557 const struct target_waitstatus *ws)
12558 {
12559 if (ws->kind != TARGET_WAITKIND_STOPPED
12560 || ws->value.sig != GDB_SIGNAL_TRAP)
12561 return 0;
12562
12563 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12564 aspace, bp_addr))
12565 return 0;
12566
12567 if (overlay_debugging /* unmapped overlay section */
12568 && section_is_overlay (bl->section)
12569 && !section_is_mapped (bl->section))
12570 return 0;
12571
12572 return 1;
12573 }
12574
12575 static int
12576 dprintf_breakpoint_hit (const struct bp_location *bl,
12577 const address_space *aspace, CORE_ADDR bp_addr,
12578 const struct target_waitstatus *ws)
12579 {
12580 if (dprintf_style == dprintf_style_agent
12581 && target_can_run_breakpoint_commands ())
12582 {
12583 /* An agent-style dprintf never causes a stop. If we see a trap
12584 for this address it must be for a breakpoint that happens to
12585 be set at the same address. */
12586 return 0;
12587 }
12588
12589 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12590 }
12591
12592 static int
12593 bkpt_resources_needed (const struct bp_location *bl)
12594 {
12595 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12596
12597 return 1;
12598 }
12599
12600 static enum print_stop_action
12601 bkpt_print_it (bpstat bs)
12602 {
12603 struct breakpoint *b;
12604 const struct bp_location *bl;
12605 int bp_temp;
12606 struct ui_out *uiout = current_uiout;
12607
12608 gdb_assert (bs->bp_location_at != NULL);
12609
12610 bl = bs->bp_location_at.get ();
12611 b = bs->breakpoint_at;
12612
12613 bp_temp = b->disposition == disp_del;
12614 if (bl->address != bl->requested_address)
12615 breakpoint_adjustment_warning (bl->requested_address,
12616 bl->address,
12617 b->number, 1);
12618 annotate_breakpoint (b->number);
12619 maybe_print_thread_hit_breakpoint (uiout);
12620
12621 if (uiout->is_mi_like_p ())
12622 {
12623 uiout->field_string ("reason",
12624 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12625 uiout->field_string ("disp", bpdisp_text (b->disposition));
12626 }
12627 if (bp_temp)
12628 uiout->message ("Temporary breakpoint %pF, ",
12629 signed_field ("bkptno", b->number));
12630 else
12631 uiout->message ("Breakpoint %pF, ",
12632 signed_field ("bkptno", b->number));
12633
12634 return PRINT_SRC_AND_LOC;
12635 }
12636
12637 static void
12638 bkpt_print_mention (struct breakpoint *b)
12639 {
12640 if (current_uiout->is_mi_like_p ())
12641 return;
12642
12643 switch (b->type)
12644 {
12645 case bp_breakpoint:
12646 case bp_gnu_ifunc_resolver:
12647 if (b->disposition == disp_del)
12648 printf_filtered (_("Temporary breakpoint"));
12649 else
12650 printf_filtered (_("Breakpoint"));
12651 printf_filtered (_(" %d"), b->number);
12652 if (b->type == bp_gnu_ifunc_resolver)
12653 printf_filtered (_(" at gnu-indirect-function resolver"));
12654 break;
12655 case bp_hardware_breakpoint:
12656 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12657 break;
12658 case bp_dprintf:
12659 printf_filtered (_("Dprintf %d"), b->number);
12660 break;
12661 }
12662
12663 say_where (b);
12664 }
12665
12666 static void
12667 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12668 {
12669 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12670 fprintf_unfiltered (fp, "tbreak");
12671 else if (tp->type == bp_breakpoint)
12672 fprintf_unfiltered (fp, "break");
12673 else if (tp->type == bp_hardware_breakpoint
12674 && tp->disposition == disp_del)
12675 fprintf_unfiltered (fp, "thbreak");
12676 else if (tp->type == bp_hardware_breakpoint)
12677 fprintf_unfiltered (fp, "hbreak");
12678 else
12679 internal_error (__FILE__, __LINE__,
12680 _("unhandled breakpoint type %d"), (int) tp->type);
12681
12682 fprintf_unfiltered (fp, " %s",
12683 event_location_to_string (tp->location.get ()));
12684
12685 /* Print out extra_string if this breakpoint is pending. It might
12686 contain, for example, conditions that were set by the user. */
12687 if (tp->loc == NULL && tp->extra_string != NULL)
12688 fprintf_unfiltered (fp, " %s", tp->extra_string);
12689
12690 print_recreate_thread (tp, fp);
12691 }
12692
12693 static void
12694 bkpt_create_sals_from_location (struct event_location *location,
12695 struct linespec_result *canonical,
12696 enum bptype type_wanted)
12697 {
12698 create_sals_from_location_default (location, canonical, type_wanted);
12699 }
12700
12701 static void
12702 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12703 struct linespec_result *canonical,
12704 gdb::unique_xmalloc_ptr<char> cond_string,
12705 gdb::unique_xmalloc_ptr<char> extra_string,
12706 enum bptype type_wanted,
12707 enum bpdisp disposition,
12708 int thread,
12709 int task, int ignore_count,
12710 const struct breakpoint_ops *ops,
12711 int from_tty, int enabled,
12712 int internal, unsigned flags)
12713 {
12714 create_breakpoints_sal_default (gdbarch, canonical,
12715 std::move (cond_string),
12716 std::move (extra_string),
12717 type_wanted,
12718 disposition, thread, task,
12719 ignore_count, ops, from_tty,
12720 enabled, internal, flags);
12721 }
12722
12723 static std::vector<symtab_and_line>
12724 bkpt_decode_location (struct breakpoint *b,
12725 struct event_location *location,
12726 struct program_space *search_pspace)
12727 {
12728 return decode_location_default (b, location, search_pspace);
12729 }
12730
12731 /* Virtual table for internal breakpoints. */
12732
12733 static void
12734 internal_bkpt_re_set (struct breakpoint *b)
12735 {
12736 switch (b->type)
12737 {
12738 /* Delete overlay event and longjmp master breakpoints; they
12739 will be reset later by breakpoint_re_set. */
12740 case bp_overlay_event:
12741 case bp_longjmp_master:
12742 case bp_std_terminate_master:
12743 case bp_exception_master:
12744 delete_breakpoint (b);
12745 break;
12746
12747 /* This breakpoint is special, it's set up when the inferior
12748 starts and we really don't want to touch it. */
12749 case bp_shlib_event:
12750
12751 /* Like bp_shlib_event, this breakpoint type is special. Once
12752 it is set up, we do not want to touch it. */
12753 case bp_thread_event:
12754 break;
12755 }
12756 }
12757
12758 static void
12759 internal_bkpt_check_status (bpstat bs)
12760 {
12761 if (bs->breakpoint_at->type == bp_shlib_event)
12762 {
12763 /* If requested, stop when the dynamic linker notifies GDB of
12764 events. This allows the user to get control and place
12765 breakpoints in initializer routines for dynamically loaded
12766 objects (among other things). */
12767 bs->stop = stop_on_solib_events;
12768 bs->print = stop_on_solib_events;
12769 }
12770 else
12771 bs->stop = 0;
12772 }
12773
12774 static enum print_stop_action
12775 internal_bkpt_print_it (bpstat bs)
12776 {
12777 struct breakpoint *b;
12778
12779 b = bs->breakpoint_at;
12780
12781 switch (b->type)
12782 {
12783 case bp_shlib_event:
12784 /* Did we stop because the user set the stop_on_solib_events
12785 variable? (If so, we report this as a generic, "Stopped due
12786 to shlib event" message.) */
12787 print_solib_event (0);
12788 break;
12789
12790 case bp_thread_event:
12791 /* Not sure how we will get here.
12792 GDB should not stop for these breakpoints. */
12793 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12794 break;
12795
12796 case bp_overlay_event:
12797 /* By analogy with the thread event, GDB should not stop for these. */
12798 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12799 break;
12800
12801 case bp_longjmp_master:
12802 /* These should never be enabled. */
12803 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12804 break;
12805
12806 case bp_std_terminate_master:
12807 /* These should never be enabled. */
12808 printf_filtered (_("std::terminate Master Breakpoint: "
12809 "gdb should not stop!\n"));
12810 break;
12811
12812 case bp_exception_master:
12813 /* These should never be enabled. */
12814 printf_filtered (_("Exception Master Breakpoint: "
12815 "gdb should not stop!\n"));
12816 break;
12817 }
12818
12819 return PRINT_NOTHING;
12820 }
12821
12822 static void
12823 internal_bkpt_print_mention (struct breakpoint *b)
12824 {
12825 /* Nothing to mention. These breakpoints are internal. */
12826 }
12827
12828 /* Virtual table for momentary breakpoints */
12829
12830 static void
12831 momentary_bkpt_re_set (struct breakpoint *b)
12832 {
12833 /* Keep temporary breakpoints, which can be encountered when we step
12834 over a dlopen call and solib_add is resetting the breakpoints.
12835 Otherwise these should have been blown away via the cleanup chain
12836 or by breakpoint_init_inferior when we rerun the executable. */
12837 }
12838
12839 static void
12840 momentary_bkpt_check_status (bpstat bs)
12841 {
12842 /* Nothing. The point of these breakpoints is causing a stop. */
12843 }
12844
12845 static enum print_stop_action
12846 momentary_bkpt_print_it (bpstat bs)
12847 {
12848 return PRINT_UNKNOWN;
12849 }
12850
12851 static void
12852 momentary_bkpt_print_mention (struct breakpoint *b)
12853 {
12854 /* Nothing to mention. These breakpoints are internal. */
12855 }
12856
12857 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12858
12859 It gets cleared already on the removal of the first one of such placed
12860 breakpoints. This is OK as they get all removed altogether. */
12861
12862 longjmp_breakpoint::~longjmp_breakpoint ()
12863 {
12864 thread_info *tp = find_thread_global_id (this->thread);
12865
12866 if (tp != NULL)
12867 tp->initiating_frame = null_frame_id;
12868 }
12869
12870 /* Specific methods for probe breakpoints. */
12871
12872 static int
12873 bkpt_probe_insert_location (struct bp_location *bl)
12874 {
12875 int v = bkpt_insert_location (bl);
12876
12877 if (v == 0)
12878 {
12879 /* The insertion was successful, now let's set the probe's semaphore
12880 if needed. */
12881 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
12882 }
12883
12884 return v;
12885 }
12886
12887 static int
12888 bkpt_probe_remove_location (struct bp_location *bl,
12889 enum remove_bp_reason reason)
12890 {
12891 /* Let's clear the semaphore before removing the location. */
12892 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
12893
12894 return bkpt_remove_location (bl, reason);
12895 }
12896
12897 static void
12898 bkpt_probe_create_sals_from_location (struct event_location *location,
12899 struct linespec_result *canonical,
12900 enum bptype type_wanted)
12901 {
12902 struct linespec_sals lsal;
12903
12904 lsal.sals = parse_probes (location, NULL, canonical);
12905 lsal.canonical
12906 = xstrdup (event_location_to_string (canonical->location.get ()));
12907 canonical->lsals.push_back (std::move (lsal));
12908 }
12909
12910 static std::vector<symtab_and_line>
12911 bkpt_probe_decode_location (struct breakpoint *b,
12912 struct event_location *location,
12913 struct program_space *search_pspace)
12914 {
12915 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12916 if (sals.empty ())
12917 error (_("probe not found"));
12918 return sals;
12919 }
12920
12921 /* The breakpoint_ops structure to be used in tracepoints. */
12922
12923 static void
12924 tracepoint_re_set (struct breakpoint *b)
12925 {
12926 breakpoint_re_set_default (b);
12927 }
12928
12929 static int
12930 tracepoint_breakpoint_hit (const struct bp_location *bl,
12931 const address_space *aspace, CORE_ADDR bp_addr,
12932 const struct target_waitstatus *ws)
12933 {
12934 /* By definition, the inferior does not report stops at
12935 tracepoints. */
12936 return 0;
12937 }
12938
12939 static void
12940 tracepoint_print_one_detail (const struct breakpoint *self,
12941 struct ui_out *uiout)
12942 {
12943 struct tracepoint *tp = (struct tracepoint *) self;
12944 if (!tp->static_trace_marker_id.empty ())
12945 {
12946 gdb_assert (self->type == bp_static_tracepoint);
12947
12948 uiout->message ("\tmarker id is %pF\n",
12949 string_field ("static-tracepoint-marker-string-id",
12950 tp->static_trace_marker_id.c_str ()));
12951 }
12952 }
12953
12954 static void
12955 tracepoint_print_mention (struct breakpoint *b)
12956 {
12957 if (current_uiout->is_mi_like_p ())
12958 return;
12959
12960 switch (b->type)
12961 {
12962 case bp_tracepoint:
12963 printf_filtered (_("Tracepoint"));
12964 printf_filtered (_(" %d"), b->number);
12965 break;
12966 case bp_fast_tracepoint:
12967 printf_filtered (_("Fast tracepoint"));
12968 printf_filtered (_(" %d"), b->number);
12969 break;
12970 case bp_static_tracepoint:
12971 printf_filtered (_("Static tracepoint"));
12972 printf_filtered (_(" %d"), b->number);
12973 break;
12974 default:
12975 internal_error (__FILE__, __LINE__,
12976 _("unhandled tracepoint type %d"), (int) b->type);
12977 }
12978
12979 say_where (b);
12980 }
12981
12982 static void
12983 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
12984 {
12985 struct tracepoint *tp = (struct tracepoint *) self;
12986
12987 if (self->type == bp_fast_tracepoint)
12988 fprintf_unfiltered (fp, "ftrace");
12989 else if (self->type == bp_static_tracepoint)
12990 fprintf_unfiltered (fp, "strace");
12991 else if (self->type == bp_tracepoint)
12992 fprintf_unfiltered (fp, "trace");
12993 else
12994 internal_error (__FILE__, __LINE__,
12995 _("unhandled tracepoint type %d"), (int) self->type);
12996
12997 fprintf_unfiltered (fp, " %s",
12998 event_location_to_string (self->location.get ()));
12999 print_recreate_thread (self, fp);
13000
13001 if (tp->pass_count)
13002 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13003 }
13004
13005 static void
13006 tracepoint_create_sals_from_location (struct event_location *location,
13007 struct linespec_result *canonical,
13008 enum bptype type_wanted)
13009 {
13010 create_sals_from_location_default (location, canonical, type_wanted);
13011 }
13012
13013 static void
13014 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13015 struct linespec_result *canonical,
13016 gdb::unique_xmalloc_ptr<char> cond_string,
13017 gdb::unique_xmalloc_ptr<char> extra_string,
13018 enum bptype type_wanted,
13019 enum bpdisp disposition,
13020 int thread,
13021 int task, int ignore_count,
13022 const struct breakpoint_ops *ops,
13023 int from_tty, int enabled,
13024 int internal, unsigned flags)
13025 {
13026 create_breakpoints_sal_default (gdbarch, canonical,
13027 std::move (cond_string),
13028 std::move (extra_string),
13029 type_wanted,
13030 disposition, thread, task,
13031 ignore_count, ops, from_tty,
13032 enabled, internal, flags);
13033 }
13034
13035 static std::vector<symtab_and_line>
13036 tracepoint_decode_location (struct breakpoint *b,
13037 struct event_location *location,
13038 struct program_space *search_pspace)
13039 {
13040 return decode_location_default (b, location, search_pspace);
13041 }
13042
13043 struct breakpoint_ops tracepoint_breakpoint_ops;
13044
13045 /* Virtual table for tracepoints on static probes. */
13046
13047 static void
13048 tracepoint_probe_create_sals_from_location
13049 (struct event_location *location,
13050 struct linespec_result *canonical,
13051 enum bptype type_wanted)
13052 {
13053 /* We use the same method for breakpoint on probes. */
13054 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13055 }
13056
13057 static std::vector<symtab_and_line>
13058 tracepoint_probe_decode_location (struct breakpoint *b,
13059 struct event_location *location,
13060 struct program_space *search_pspace)
13061 {
13062 /* We use the same method for breakpoint on probes. */
13063 return bkpt_probe_decode_location (b, location, search_pspace);
13064 }
13065
13066 /* Dprintf breakpoint_ops methods. */
13067
13068 static void
13069 dprintf_re_set (struct breakpoint *b)
13070 {
13071 breakpoint_re_set_default (b);
13072
13073 /* extra_string should never be non-NULL for dprintf. */
13074 gdb_assert (b->extra_string != NULL);
13075
13076 /* 1 - connect to target 1, that can run breakpoint commands.
13077 2 - create a dprintf, which resolves fine.
13078 3 - disconnect from target 1
13079 4 - connect to target 2, that can NOT run breakpoint commands.
13080
13081 After steps #3/#4, you'll want the dprintf command list to
13082 be updated, because target 1 and 2 may well return different
13083 answers for target_can_run_breakpoint_commands().
13084 Given absence of finer grained resetting, we get to do
13085 it all the time. */
13086 if (b->extra_string != NULL)
13087 update_dprintf_command_list (b);
13088 }
13089
13090 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13091
13092 static void
13093 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13094 {
13095 fprintf_unfiltered (fp, "dprintf %s,%s",
13096 event_location_to_string (tp->location.get ()),
13097 tp->extra_string);
13098 print_recreate_thread (tp, fp);
13099 }
13100
13101 /* Implement the "after_condition_true" breakpoint_ops method for
13102 dprintf.
13103
13104 dprintf's are implemented with regular commands in their command
13105 list, but we run the commands here instead of before presenting the
13106 stop to the user, as dprintf's don't actually cause a stop. This
13107 also makes it so that the commands of multiple dprintfs at the same
13108 address are all handled. */
13109
13110 static void
13111 dprintf_after_condition_true (struct bpstats *bs)
13112 {
13113 struct bpstats tmp_bs;
13114 struct bpstats *tmp_bs_p = &tmp_bs;
13115
13116 /* dprintf's never cause a stop. This wasn't set in the
13117 check_status hook instead because that would make the dprintf's
13118 condition not be evaluated. */
13119 bs->stop = 0;
13120
13121 /* Run the command list here. Take ownership of it instead of
13122 copying. We never want these commands to run later in
13123 bpstat_do_actions, if a breakpoint that causes a stop happens to
13124 be set at same address as this dprintf, or even if running the
13125 commands here throws. */
13126 tmp_bs.commands = bs->commands;
13127 bs->commands = NULL;
13128
13129 bpstat_do_actions_1 (&tmp_bs_p);
13130
13131 /* 'tmp_bs.commands' will usually be NULL by now, but
13132 bpstat_do_actions_1 may return early without processing the whole
13133 list. */
13134 }
13135
13136 /* The breakpoint_ops structure to be used on static tracepoints with
13137 markers (`-m'). */
13138
13139 static void
13140 strace_marker_create_sals_from_location (struct event_location *location,
13141 struct linespec_result *canonical,
13142 enum bptype type_wanted)
13143 {
13144 struct linespec_sals lsal;
13145 const char *arg_start, *arg;
13146
13147 arg = arg_start = get_linespec_location (location)->spec_string;
13148 lsal.sals = decode_static_tracepoint_spec (&arg);
13149
13150 std::string str (arg_start, arg - arg_start);
13151 const char *ptr = str.c_str ();
13152 canonical->location
13153 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
13154
13155 lsal.canonical
13156 = xstrdup (event_location_to_string (canonical->location.get ()));
13157 canonical->lsals.push_back (std::move (lsal));
13158 }
13159
13160 static void
13161 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13162 struct linespec_result *canonical,
13163 gdb::unique_xmalloc_ptr<char> cond_string,
13164 gdb::unique_xmalloc_ptr<char> extra_string,
13165 enum bptype type_wanted,
13166 enum bpdisp disposition,
13167 int thread,
13168 int task, int ignore_count,
13169 const struct breakpoint_ops *ops,
13170 int from_tty, int enabled,
13171 int internal, unsigned flags)
13172 {
13173 const linespec_sals &lsal = canonical->lsals[0];
13174
13175 /* If the user is creating a static tracepoint by marker id
13176 (strace -m MARKER_ID), then store the sals index, so that
13177 breakpoint_re_set can try to match up which of the newly
13178 found markers corresponds to this one, and, don't try to
13179 expand multiple locations for each sal, given than SALS
13180 already should contain all sals for MARKER_ID. */
13181
13182 for (size_t i = 0; i < lsal.sals.size (); i++)
13183 {
13184 event_location_up location
13185 = copy_event_location (canonical->location.get ());
13186
13187 std::unique_ptr<tracepoint> tp (new tracepoint ());
13188 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
13189 std::move (location), NULL,
13190 std::move (cond_string),
13191 std::move (extra_string),
13192 type_wanted, disposition,
13193 thread, task, ignore_count, ops,
13194 from_tty, enabled, internal, flags,
13195 canonical->special_display);
13196 /* Given that its possible to have multiple markers with
13197 the same string id, if the user is creating a static
13198 tracepoint by marker id ("strace -m MARKER_ID"), then
13199 store the sals index, so that breakpoint_re_set can
13200 try to match up which of the newly found markers
13201 corresponds to this one */
13202 tp->static_trace_marker_id_idx = i;
13203
13204 install_breakpoint (internal, std::move (tp), 0);
13205 }
13206 }
13207
13208 static std::vector<symtab_and_line>
13209 strace_marker_decode_location (struct breakpoint *b,
13210 struct event_location *location,
13211 struct program_space *search_pspace)
13212 {
13213 struct tracepoint *tp = (struct tracepoint *) b;
13214 const char *s = get_linespec_location (location)->spec_string;
13215
13216 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13217 if (sals.size () > tp->static_trace_marker_id_idx)
13218 {
13219 sals[0] = sals[tp->static_trace_marker_id_idx];
13220 sals.resize (1);
13221 return sals;
13222 }
13223 else
13224 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
13225 }
13226
13227 static struct breakpoint_ops strace_marker_breakpoint_ops;
13228
13229 static int
13230 strace_marker_p (struct breakpoint *b)
13231 {
13232 return b->ops == &strace_marker_breakpoint_ops;
13233 }
13234
13235 /* Delete a breakpoint and clean up all traces of it in the data
13236 structures. */
13237
13238 void
13239 delete_breakpoint (struct breakpoint *bpt)
13240 {
13241 gdb_assert (bpt != NULL);
13242
13243 /* Has this bp already been deleted? This can happen because
13244 multiple lists can hold pointers to bp's. bpstat lists are
13245 especial culprits.
13246
13247 One example of this happening is a watchpoint's scope bp. When
13248 the scope bp triggers, we notice that the watchpoint is out of
13249 scope, and delete it. We also delete its scope bp. But the
13250 scope bp is marked "auto-deleting", and is already on a bpstat.
13251 That bpstat is then checked for auto-deleting bp's, which are
13252 deleted.
13253
13254 A real solution to this problem might involve reference counts in
13255 bp's, and/or giving them pointers back to their referencing
13256 bpstat's, and teaching delete_breakpoint to only free a bp's
13257 storage when no more references were extent. A cheaper bandaid
13258 was chosen. */
13259 if (bpt->type == bp_none)
13260 return;
13261
13262 /* At least avoid this stale reference until the reference counting
13263 of breakpoints gets resolved. */
13264 if (bpt->related_breakpoint != bpt)
13265 {
13266 struct breakpoint *related;
13267 struct watchpoint *w;
13268
13269 if (bpt->type == bp_watchpoint_scope)
13270 w = (struct watchpoint *) bpt->related_breakpoint;
13271 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13272 w = (struct watchpoint *) bpt;
13273 else
13274 w = NULL;
13275 if (w != NULL)
13276 watchpoint_del_at_next_stop (w);
13277
13278 /* Unlink bpt from the bpt->related_breakpoint ring. */
13279 for (related = bpt; related->related_breakpoint != bpt;
13280 related = related->related_breakpoint);
13281 related->related_breakpoint = bpt->related_breakpoint;
13282 bpt->related_breakpoint = bpt;
13283 }
13284
13285 /* watch_command_1 creates a watchpoint but only sets its number if
13286 update_watchpoint succeeds in creating its bp_locations. If there's
13287 a problem in that process, we'll be asked to delete the half-created
13288 watchpoint. In that case, don't announce the deletion. */
13289 if (bpt->number)
13290 gdb::observers::breakpoint_deleted.notify (bpt);
13291
13292 if (breakpoint_chain == bpt)
13293 breakpoint_chain = bpt->next;
13294
13295 for (breakpoint *b : all_breakpoints ())
13296 if (b->next == bpt)
13297 {
13298 b->next = bpt->next;
13299 break;
13300 }
13301
13302 /* Be sure no bpstat's are pointing at the breakpoint after it's
13303 been freed. */
13304 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13305 in all threads for now. Note that we cannot just remove bpstats
13306 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13307 commands are associated with the bpstat; if we remove it here,
13308 then the later call to bpstat_do_actions (&stop_bpstat); in
13309 event-top.c won't do anything, and temporary breakpoints with
13310 commands won't work. */
13311
13312 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13313
13314 /* Now that breakpoint is removed from breakpoint list, update the
13315 global location list. This will remove locations that used to
13316 belong to this breakpoint. Do this before freeing the breakpoint
13317 itself, since remove_breakpoint looks at location's owner. It
13318 might be better design to have location completely
13319 self-contained, but it's not the case now. */
13320 update_global_location_list (UGLL_DONT_INSERT);
13321
13322 /* On the chance that someone will soon try again to delete this
13323 same bp, we mark it as deleted before freeing its storage. */
13324 bpt->type = bp_none;
13325 delete bpt;
13326 }
13327
13328 /* Iterator function to call a user-provided callback function once
13329 for each of B and its related breakpoints. */
13330
13331 static void
13332 iterate_over_related_breakpoints (struct breakpoint *b,
13333 gdb::function_view<void (breakpoint *)> function)
13334 {
13335 struct breakpoint *related;
13336
13337 related = b;
13338 do
13339 {
13340 struct breakpoint *next;
13341
13342 /* FUNCTION may delete RELATED. */
13343 next = related->related_breakpoint;
13344
13345 if (next == related)
13346 {
13347 /* RELATED is the last ring entry. */
13348 function (related);
13349
13350 /* FUNCTION may have deleted it, so we'd never reach back to
13351 B. There's nothing left to do anyway, so just break
13352 out. */
13353 break;
13354 }
13355 else
13356 function (related);
13357
13358 related = next;
13359 }
13360 while (related != b);
13361 }
13362
13363 static void
13364 delete_command (const char *arg, int from_tty)
13365 {
13366 dont_repeat ();
13367
13368 if (arg == 0)
13369 {
13370 int breaks_to_delete = 0;
13371
13372 /* Delete all breakpoints if no argument. Do not delete
13373 internal breakpoints, these have to be deleted with an
13374 explicit breakpoint number argument. */
13375 for (breakpoint *b : all_breakpoints ())
13376 if (user_breakpoint_p (b))
13377 {
13378 breaks_to_delete = 1;
13379 break;
13380 }
13381
13382 /* Ask user only if there are some breakpoints to delete. */
13383 if (!from_tty
13384 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13385 for (breakpoint *b : all_breakpoints_safe ())
13386 if (user_breakpoint_p (b))
13387 delete_breakpoint (b);
13388 }
13389 else
13390 map_breakpoint_numbers
13391 (arg, [&] (breakpoint *br)
13392 {
13393 iterate_over_related_breakpoints (br, delete_breakpoint);
13394 });
13395 }
13396
13397 /* Return true if all locations of B bound to PSPACE are pending. If
13398 PSPACE is NULL, all locations of all program spaces are
13399 considered. */
13400
13401 static int
13402 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13403 {
13404 struct bp_location *loc;
13405
13406 for (loc = b->loc; loc != NULL; loc = loc->next)
13407 if ((pspace == NULL
13408 || loc->pspace == pspace)
13409 && !loc->shlib_disabled
13410 && !loc->pspace->executing_startup)
13411 return 0;
13412 return 1;
13413 }
13414
13415 /* Subroutine of update_breakpoint_locations to simplify it.
13416 Return non-zero if multiple fns in list LOC have the same name.
13417 Null names are ignored. */
13418
13419 static int
13420 ambiguous_names_p (struct bp_location *loc)
13421 {
13422 struct bp_location *l;
13423 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
13424 xcalloc, xfree));
13425
13426 for (l = loc; l != NULL; l = l->next)
13427 {
13428 const char **slot;
13429 const char *name = l->function_name;
13430
13431 /* Allow for some names to be NULL, ignore them. */
13432 if (name == NULL)
13433 continue;
13434
13435 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
13436 INSERT);
13437 /* NOTE: We can assume slot != NULL here because xcalloc never
13438 returns NULL. */
13439 if (*slot != NULL)
13440 return 1;
13441 *slot = name;
13442 }
13443
13444 return 0;
13445 }
13446
13447 /* When symbols change, it probably means the sources changed as well,
13448 and it might mean the static tracepoint markers are no longer at
13449 the same address or line numbers they used to be at last we
13450 checked. Losing your static tracepoints whenever you rebuild is
13451 undesirable. This function tries to resync/rematch gdb static
13452 tracepoints with the markers on the target, for static tracepoints
13453 that have not been set by marker id. Static tracepoint that have
13454 been set by marker id are reset by marker id in breakpoint_re_set.
13455 The heuristic is:
13456
13457 1) For a tracepoint set at a specific address, look for a marker at
13458 the old PC. If one is found there, assume to be the same marker.
13459 If the name / string id of the marker found is different from the
13460 previous known name, assume that means the user renamed the marker
13461 in the sources, and output a warning.
13462
13463 2) For a tracepoint set at a given line number, look for a marker
13464 at the new address of the old line number. If one is found there,
13465 assume to be the same marker. If the name / string id of the
13466 marker found is different from the previous known name, assume that
13467 means the user renamed the marker in the sources, and output a
13468 warning.
13469
13470 3) If a marker is no longer found at the same address or line, it
13471 may mean the marker no longer exists. But it may also just mean
13472 the code changed a bit. Maybe the user added a few lines of code
13473 that made the marker move up or down (in line number terms). Ask
13474 the target for info about the marker with the string id as we knew
13475 it. If found, update line number and address in the matching
13476 static tracepoint. This will get confused if there's more than one
13477 marker with the same ID (possible in UST, although unadvised
13478 precisely because it confuses tools). */
13479
13480 static struct symtab_and_line
13481 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13482 {
13483 struct tracepoint *tp = (struct tracepoint *) b;
13484 struct static_tracepoint_marker marker;
13485 CORE_ADDR pc;
13486
13487 pc = sal.pc;
13488 if (sal.line)
13489 find_line_pc (sal.symtab, sal.line, &pc);
13490
13491 if (target_static_tracepoint_marker_at (pc, &marker))
13492 {
13493 if (tp->static_trace_marker_id != marker.str_id)
13494 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13495 b->number, tp->static_trace_marker_id.c_str (),
13496 marker.str_id.c_str ());
13497
13498 tp->static_trace_marker_id = std::move (marker.str_id);
13499
13500 return sal;
13501 }
13502
13503 /* Old marker wasn't found on target at lineno. Try looking it up
13504 by string ID. */
13505 if (!sal.explicit_pc
13506 && sal.line != 0
13507 && sal.symtab != NULL
13508 && !tp->static_trace_marker_id.empty ())
13509 {
13510 std::vector<static_tracepoint_marker> markers
13511 = target_static_tracepoint_markers_by_strid
13512 (tp->static_trace_marker_id.c_str ());
13513
13514 if (!markers.empty ())
13515 {
13516 struct symbol *sym;
13517 struct static_tracepoint_marker *tpmarker;
13518 struct ui_out *uiout = current_uiout;
13519 struct explicit_location explicit_loc;
13520
13521 tpmarker = &markers[0];
13522
13523 tp->static_trace_marker_id = std::move (tpmarker->str_id);
13524
13525 warning (_("marker for static tracepoint %d (%s) not "
13526 "found at previous line number"),
13527 b->number, tp->static_trace_marker_id.c_str ());
13528
13529 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
13530 sym = find_pc_sect_function (tpmarker->address, NULL);
13531 uiout->text ("Now in ");
13532 if (sym)
13533 {
13534 uiout->field_string ("func", sym->print_name (),
13535 function_name_style.style ());
13536 uiout->text (" at ");
13537 }
13538 uiout->field_string ("file",
13539 symtab_to_filename_for_display (sal2.symtab),
13540 file_name_style.style ());
13541 uiout->text (":");
13542
13543 if (uiout->is_mi_like_p ())
13544 {
13545 const char *fullname = symtab_to_fullname (sal2.symtab);
13546
13547 uiout->field_string ("fullname", fullname);
13548 }
13549
13550 uiout->field_signed ("line", sal2.line);
13551 uiout->text ("\n");
13552
13553 b->loc->line_number = sal2.line;
13554 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13555
13556 b->location.reset (NULL);
13557 initialize_explicit_location (&explicit_loc);
13558 explicit_loc.source_filename
13559 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13560 explicit_loc.line_offset.offset = b->loc->line_number;
13561 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13562 b->location = new_explicit_location (&explicit_loc);
13563
13564 /* Might be nice to check if function changed, and warn if
13565 so. */
13566 }
13567 }
13568 return sal;
13569 }
13570
13571 /* Returns 1 iff locations A and B are sufficiently same that
13572 we don't need to report breakpoint as changed. */
13573
13574 static int
13575 locations_are_equal (struct bp_location *a, struct bp_location *b)
13576 {
13577 while (a && b)
13578 {
13579 if (a->address != b->address)
13580 return 0;
13581
13582 if (a->shlib_disabled != b->shlib_disabled)
13583 return 0;
13584
13585 if (a->enabled != b->enabled)
13586 return 0;
13587
13588 if (a->disabled_by_cond != b->disabled_by_cond)
13589 return 0;
13590
13591 a = a->next;
13592 b = b->next;
13593 }
13594
13595 if ((a == NULL) != (b == NULL))
13596 return 0;
13597
13598 return 1;
13599 }
13600
13601 /* Split all locations of B that are bound to PSPACE out of B's
13602 location list to a separate list and return that list's head. If
13603 PSPACE is NULL, hoist out all locations of B. */
13604
13605 static struct bp_location *
13606 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13607 {
13608 struct bp_location head;
13609 struct bp_location *i = b->loc;
13610 struct bp_location **i_link = &b->loc;
13611 struct bp_location *hoisted = &head;
13612
13613 if (pspace == NULL)
13614 {
13615 i = b->loc;
13616 b->loc = NULL;
13617 return i;
13618 }
13619
13620 head.next = NULL;
13621
13622 while (i != NULL)
13623 {
13624 if (i->pspace == pspace)
13625 {
13626 *i_link = i->next;
13627 i->next = NULL;
13628 hoisted->next = i;
13629 hoisted = i;
13630 }
13631 else
13632 i_link = &i->next;
13633 i = *i_link;
13634 }
13635
13636 return head.next;
13637 }
13638
13639 /* Create new breakpoint locations for B (a hardware or software
13640 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13641 zero, then B is a ranged breakpoint. Only recreates locations for
13642 FILTER_PSPACE. Locations of other program spaces are left
13643 untouched. */
13644
13645 void
13646 update_breakpoint_locations (struct breakpoint *b,
13647 struct program_space *filter_pspace,
13648 gdb::array_view<const symtab_and_line> sals,
13649 gdb::array_view<const symtab_and_line> sals_end)
13650 {
13651 struct bp_location *existing_locations;
13652
13653 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
13654 {
13655 /* Ranged breakpoints have only one start location and one end
13656 location. */
13657 b->enable_state = bp_disabled;
13658 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13659 "multiple locations found\n"),
13660 b->number);
13661 return;
13662 }
13663
13664 /* If there's no new locations, and all existing locations are
13665 pending, don't do anything. This optimizes the common case where
13666 all locations are in the same shared library, that was unloaded.
13667 We'd like to retain the location, so that when the library is
13668 loaded again, we don't loose the enabled/disabled status of the
13669 individual locations. */
13670 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
13671 return;
13672
13673 existing_locations = hoist_existing_locations (b, filter_pspace);
13674
13675 for (const auto &sal : sals)
13676 {
13677 struct bp_location *new_loc;
13678
13679 switch_to_program_space_and_thread (sal.pspace);
13680
13681 new_loc = add_location_to_breakpoint (b, &sal);
13682
13683 /* Reparse conditions, they might contain references to the
13684 old symtab. */
13685 if (b->cond_string != NULL)
13686 {
13687 const char *s;
13688
13689 s = b->cond_string;
13690 try
13691 {
13692 new_loc->cond = parse_exp_1 (&s, sal.pc,
13693 block_for_pc (sal.pc),
13694 0);
13695 }
13696 catch (const gdb_exception_error &e)
13697 {
13698 new_loc->disabled_by_cond = true;
13699 }
13700 }
13701
13702 if (!sals_end.empty ())
13703 {
13704 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13705
13706 new_loc->length = end - sals[0].pc + 1;
13707 }
13708 }
13709
13710 /* If possible, carry over 'disable' status from existing
13711 breakpoints. */
13712 {
13713 struct bp_location *e = existing_locations;
13714 /* If there are multiple breakpoints with the same function name,
13715 e.g. for inline functions, comparing function names won't work.
13716 Instead compare pc addresses; this is just a heuristic as things
13717 may have moved, but in practice it gives the correct answer
13718 often enough until a better solution is found. */
13719 int have_ambiguous_names = ambiguous_names_p (b->loc);
13720
13721 for (; e; e = e->next)
13722 {
13723 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
13724 {
13725 struct bp_location *l = b->loc;
13726 if (have_ambiguous_names)
13727 {
13728 for (; l; l = l->next)
13729 {
13730 /* Ignore software vs hardware location type at
13731 this point, because with "set breakpoint
13732 auto-hw", after a re-set, locations that were
13733 hardware can end up as software, or vice versa.
13734 As mentioned above, this is an heuristic and in
13735 practice should give the correct answer often
13736 enough. */
13737 if (breakpoint_locations_match (e, l, true))
13738 {
13739 l->enabled = e->enabled;
13740 l->disabled_by_cond = e->disabled_by_cond;
13741 break;
13742 }
13743 }
13744 }
13745 else
13746 {
13747 for (; l; l = l->next)
13748 if (l->function_name
13749 && strcmp (e->function_name, l->function_name) == 0)
13750 {
13751 l->enabled = e->enabled;
13752 l->disabled_by_cond = e->disabled_by_cond;
13753 break;
13754 }
13755 }
13756 }
13757 }
13758 }
13759
13760 if (!locations_are_equal (existing_locations, b->loc))
13761 gdb::observers::breakpoint_modified.notify (b);
13762 }
13763
13764 /* Find the SaL locations corresponding to the given LOCATION.
13765 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13766
13767 static std::vector<symtab_and_line>
13768 location_to_sals (struct breakpoint *b, struct event_location *location,
13769 struct program_space *search_pspace, int *found)
13770 {
13771 struct gdb_exception exception;
13772
13773 gdb_assert (b->ops != NULL);
13774
13775 std::vector<symtab_and_line> sals;
13776
13777 try
13778 {
13779 sals = b->ops->decode_location (b, location, search_pspace);
13780 }
13781 catch (gdb_exception_error &e)
13782 {
13783 int not_found_and_ok = 0;
13784
13785 /* For pending breakpoints, it's expected that parsing will
13786 fail until the right shared library is loaded. User has
13787 already told to create pending breakpoints and don't need
13788 extra messages. If breakpoint is in bp_shlib_disabled
13789 state, then user already saw the message about that
13790 breakpoint being disabled, and don't want to see more
13791 errors. */
13792 if (e.error == NOT_FOUND_ERROR
13793 && (b->condition_not_parsed
13794 || (b->loc != NULL
13795 && search_pspace != NULL
13796 && b->loc->pspace != search_pspace)
13797 || (b->loc && b->loc->shlib_disabled)
13798 || (b->loc && b->loc->pspace->executing_startup)
13799 || b->enable_state == bp_disabled))
13800 not_found_and_ok = 1;
13801
13802 if (!not_found_and_ok)
13803 {
13804 /* We surely don't want to warn about the same breakpoint
13805 10 times. One solution, implemented here, is disable
13806 the breakpoint on error. Another solution would be to
13807 have separate 'warning emitted' flag. Since this
13808 happens only when a binary has changed, I don't know
13809 which approach is better. */
13810 b->enable_state = bp_disabled;
13811 throw;
13812 }
13813
13814 exception = std::move (e);
13815 }
13816
13817 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13818 {
13819 for (auto &sal : sals)
13820 resolve_sal_pc (&sal);
13821 if (b->condition_not_parsed && b->extra_string != NULL)
13822 {
13823 char *cond_string, *extra_string;
13824 int thread, task;
13825
13826 find_condition_and_thread_for_sals (sals, b->extra_string,
13827 &cond_string, &thread,
13828 &task, &extra_string);
13829 gdb_assert (b->cond_string == NULL);
13830 if (cond_string)
13831 b->cond_string = cond_string;
13832 b->thread = thread;
13833 b->task = task;
13834 if (extra_string)
13835 {
13836 xfree (b->extra_string);
13837 b->extra_string = extra_string;
13838 }
13839 b->condition_not_parsed = 0;
13840 }
13841
13842 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13843 sals[0] = update_static_tracepoint (b, sals[0]);
13844
13845 *found = 1;
13846 }
13847 else
13848 *found = 0;
13849
13850 return sals;
13851 }
13852
13853 /* The default re_set method, for typical hardware or software
13854 breakpoints. Reevaluate the breakpoint and recreate its
13855 locations. */
13856
13857 static void
13858 breakpoint_re_set_default (struct breakpoint *b)
13859 {
13860 struct program_space *filter_pspace = current_program_space;
13861 std::vector<symtab_and_line> expanded, expanded_end;
13862
13863 int found;
13864 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13865 filter_pspace, &found);
13866 if (found)
13867 expanded = std::move (sals);
13868
13869 if (b->location_range_end != NULL)
13870 {
13871 std::vector<symtab_and_line> sals_end
13872 = location_to_sals (b, b->location_range_end.get (),
13873 filter_pspace, &found);
13874 if (found)
13875 expanded_end = std::move (sals_end);
13876 }
13877
13878 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
13879 }
13880
13881 /* Default method for creating SALs from an address string. It basically
13882 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13883
13884 static void
13885 create_sals_from_location_default (struct event_location *location,
13886 struct linespec_result *canonical,
13887 enum bptype type_wanted)
13888 {
13889 parse_breakpoint_sals (location, canonical);
13890 }
13891
13892 /* Call create_breakpoints_sal for the given arguments. This is the default
13893 function for the `create_breakpoints_sal' method of
13894 breakpoint_ops. */
13895
13896 static void
13897 create_breakpoints_sal_default (struct gdbarch *gdbarch,
13898 struct linespec_result *canonical,
13899 gdb::unique_xmalloc_ptr<char> cond_string,
13900 gdb::unique_xmalloc_ptr<char> extra_string,
13901 enum bptype type_wanted,
13902 enum bpdisp disposition,
13903 int thread,
13904 int task, int ignore_count,
13905 const struct breakpoint_ops *ops,
13906 int from_tty, int enabled,
13907 int internal, unsigned flags)
13908 {
13909 create_breakpoints_sal (gdbarch, canonical,
13910 std::move (cond_string),
13911 std::move (extra_string),
13912 type_wanted, disposition,
13913 thread, task, ignore_count, ops, from_tty,
13914 enabled, internal, flags);
13915 }
13916
13917 /* Decode the line represented by S by calling decode_line_full. This is the
13918 default function for the `decode_location' method of breakpoint_ops. */
13919
13920 static std::vector<symtab_and_line>
13921 decode_location_default (struct breakpoint *b,
13922 struct event_location *location,
13923 struct program_space *search_pspace)
13924 {
13925 struct linespec_result canonical;
13926
13927 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
13928 NULL, 0, &canonical, multiple_symbols_all,
13929 b->filter.get ());
13930
13931 /* We should get 0 or 1 resulting SALs. */
13932 gdb_assert (canonical.lsals.size () < 2);
13933
13934 if (!canonical.lsals.empty ())
13935 {
13936 const linespec_sals &lsal = canonical.lsals[0];
13937 return std::move (lsal.sals);
13938 }
13939 return {};
13940 }
13941
13942 /* Reset a breakpoint. */
13943
13944 static void
13945 breakpoint_re_set_one (breakpoint *b)
13946 {
13947 input_radix = b->input_radix;
13948 set_language (b->language);
13949
13950 b->ops->re_set (b);
13951 }
13952
13953 /* Re-set breakpoint locations for the current program space.
13954 Locations bound to other program spaces are left untouched. */
13955
13956 void
13957 breakpoint_re_set (void)
13958 {
13959 {
13960 scoped_restore_current_language save_language;
13961 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
13962 scoped_restore_current_pspace_and_thread restore_pspace_thread;
13963
13964 /* breakpoint_re_set_one sets the current_language to the language
13965 of the breakpoint it is resetting (see prepare_re_set_context)
13966 before re-evaluating the breakpoint's location. This change can
13967 unfortunately get undone by accident if the language_mode is set
13968 to auto, and we either switch frames, or more likely in this context,
13969 we select the current frame.
13970
13971 We prevent this by temporarily turning the language_mode to
13972 language_mode_manual. We restore it once all breakpoints
13973 have been reset. */
13974 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13975 language_mode = language_mode_manual;
13976
13977 /* Note: we must not try to insert locations until after all
13978 breakpoints have been re-set. Otherwise, e.g., when re-setting
13979 breakpoint 1, we'd insert the locations of breakpoint 2, which
13980 hadn't been re-set yet, and thus may have stale locations. */
13981
13982 for (breakpoint *b : all_breakpoints_safe ())
13983 {
13984 try
13985 {
13986 breakpoint_re_set_one (b);
13987 }
13988 catch (const gdb_exception &ex)
13989 {
13990 exception_fprintf (gdb_stderr, ex,
13991 "Error in re-setting breakpoint %d: ",
13992 b->number);
13993 }
13994 }
13995
13996 jit_breakpoint_re_set ();
13997 }
13998
13999 create_overlay_event_breakpoint ();
14000 create_longjmp_master_breakpoint ();
14001 create_std_terminate_master_breakpoint ();
14002 create_exception_master_breakpoint ();
14003
14004 /* Now we can insert. */
14005 update_global_location_list (UGLL_MAY_INSERT);
14006 }
14007 \f
14008 /* Reset the thread number of this breakpoint:
14009
14010 - If the breakpoint is for all threads, leave it as-is.
14011 - Else, reset it to the current thread for inferior_ptid. */
14012 void
14013 breakpoint_re_set_thread (struct breakpoint *b)
14014 {
14015 if (b->thread != -1)
14016 {
14017 b->thread = inferior_thread ()->global_num;
14018
14019 /* We're being called after following a fork. The new fork is
14020 selected as current, and unless this was a vfork will have a
14021 different program space from the original thread. Reset that
14022 as well. */
14023 b->loc->pspace = current_program_space;
14024 }
14025 }
14026
14027 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14028 If from_tty is nonzero, it prints a message to that effect,
14029 which ends with a period (no newline). */
14030
14031 void
14032 set_ignore_count (int bptnum, int count, int from_tty)
14033 {
14034 if (count < 0)
14035 count = 0;
14036
14037 for (breakpoint *b : all_breakpoints ())
14038 if (b->number == bptnum)
14039 {
14040 if (is_tracepoint (b))
14041 {
14042 if (from_tty && count != 0)
14043 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14044 bptnum);
14045 return;
14046 }
14047
14048 b->ignore_count = count;
14049 if (from_tty)
14050 {
14051 if (count == 0)
14052 printf_filtered (_("Will stop next time "
14053 "breakpoint %d is reached."),
14054 bptnum);
14055 else if (count == 1)
14056 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14057 bptnum);
14058 else
14059 printf_filtered (_("Will ignore next %d "
14060 "crossings of breakpoint %d."),
14061 count, bptnum);
14062 }
14063 gdb::observers::breakpoint_modified.notify (b);
14064 return;
14065 }
14066
14067 error (_("No breakpoint number %d."), bptnum);
14068 }
14069
14070 /* Command to set ignore-count of breakpoint N to COUNT. */
14071
14072 static void
14073 ignore_command (const char *args, int from_tty)
14074 {
14075 const char *p = args;
14076 int num;
14077
14078 if (p == 0)
14079 error_no_arg (_("a breakpoint number"));
14080
14081 num = get_number (&p);
14082 if (num == 0)
14083 error (_("bad breakpoint number: '%s'"), args);
14084 if (*p == 0)
14085 error (_("Second argument (specified ignore-count) is missing."));
14086
14087 set_ignore_count (num,
14088 longest_to_int (value_as_long (parse_and_eval (p))),
14089 from_tty);
14090 if (from_tty)
14091 printf_filtered ("\n");
14092 }
14093 \f
14094
14095 /* Call FUNCTION on each of the breakpoints with numbers in the range
14096 defined by BP_NUM_RANGE (an inclusive range). */
14097
14098 static void
14099 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
14100 gdb::function_view<void (breakpoint *)> function)
14101 {
14102 if (bp_num_range.first == 0)
14103 {
14104 warning (_("bad breakpoint number at or near '%d'"),
14105 bp_num_range.first);
14106 }
14107 else
14108 {
14109 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
14110 {
14111 bool match = false;
14112
14113 for (breakpoint *b : all_breakpoints_safe ())
14114 if (b->number == i)
14115 {
14116 match = true;
14117 function (b);
14118 break;
14119 }
14120 if (!match)
14121 printf_unfiltered (_("No breakpoint number %d.\n"), i);
14122 }
14123 }
14124 }
14125
14126 /* Call FUNCTION on each of the breakpoints whose numbers are given in
14127 ARGS. */
14128
14129 static void
14130 map_breakpoint_numbers (const char *args,
14131 gdb::function_view<void (breakpoint *)> function)
14132 {
14133 if (args == NULL || *args == '\0')
14134 error_no_arg (_("one or more breakpoint numbers"));
14135
14136 number_or_range_parser parser (args);
14137
14138 while (!parser.finished ())
14139 {
14140 int num = parser.get_number ();
14141 map_breakpoint_number_range (std::make_pair (num, num), function);
14142 }
14143 }
14144
14145 /* Return the breakpoint location structure corresponding to the
14146 BP_NUM and LOC_NUM values. */
14147
14148 static struct bp_location *
14149 find_location_by_number (int bp_num, int loc_num)
14150 {
14151 breakpoint *b = get_breakpoint (bp_num);
14152
14153 if (!b || b->number != bp_num)
14154 error (_("Bad breakpoint number '%d'"), bp_num);
14155
14156 if (loc_num == 0)
14157 error (_("Bad breakpoint location number '%d'"), loc_num);
14158
14159 int n = 0;
14160 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
14161 if (++n == loc_num)
14162 return loc;
14163
14164 error (_("Bad breakpoint location number '%d'"), loc_num);
14165 }
14166
14167 /* Modes of operation for extract_bp_num. */
14168 enum class extract_bp_kind
14169 {
14170 /* Extracting a breakpoint number. */
14171 bp,
14172
14173 /* Extracting a location number. */
14174 loc,
14175 };
14176
14177 /* Extract a breakpoint or location number (as determined by KIND)
14178 from the string starting at START. TRAILER is a character which
14179 can be found after the number. If you don't want a trailer, use
14180 '\0'. If END_OUT is not NULL, it is set to point after the parsed
14181 string. This always returns a positive integer. */
14182
14183 static int
14184 extract_bp_num (extract_bp_kind kind, const char *start,
14185 int trailer, const char **end_out = NULL)
14186 {
14187 const char *end = start;
14188 int num = get_number_trailer (&end, trailer);
14189 if (num < 0)
14190 error (kind == extract_bp_kind::bp
14191 ? _("Negative breakpoint number '%.*s'")
14192 : _("Negative breakpoint location number '%.*s'"),
14193 int (end - start), start);
14194 if (num == 0)
14195 error (kind == extract_bp_kind::bp
14196 ? _("Bad breakpoint number '%.*s'")
14197 : _("Bad breakpoint location number '%.*s'"),
14198 int (end - start), start);
14199
14200 if (end_out != NULL)
14201 *end_out = end;
14202 return num;
14203 }
14204
14205 /* Extract a breakpoint or location range (as determined by KIND) in
14206 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14207 representing the (inclusive) range. The returned pair's elements
14208 are always positive integers. */
14209
14210 static std::pair<int, int>
14211 extract_bp_or_bp_range (extract_bp_kind kind,
14212 const std::string &arg,
14213 std::string::size_type arg_offset)
14214 {
14215 std::pair<int, int> range;
14216 const char *bp_loc = &arg[arg_offset];
14217 std::string::size_type dash = arg.find ('-', arg_offset);
14218 if (dash != std::string::npos)
14219 {
14220 /* bp_loc is a range (x-z). */
14221 if (arg.length () == dash + 1)
14222 error (kind == extract_bp_kind::bp
14223 ? _("Bad breakpoint number at or near: '%s'")
14224 : _("Bad breakpoint location number at or near: '%s'"),
14225 bp_loc);
14226
14227 const char *end;
14228 const char *start_first = bp_loc;
14229 const char *start_second = &arg[dash + 1];
14230 range.first = extract_bp_num (kind, start_first, '-');
14231 range.second = extract_bp_num (kind, start_second, '\0', &end);
14232
14233 if (range.first > range.second)
14234 error (kind == extract_bp_kind::bp
14235 ? _("Inverted breakpoint range at '%.*s'")
14236 : _("Inverted breakpoint location range at '%.*s'"),
14237 int (end - start_first), start_first);
14238 }
14239 else
14240 {
14241 /* bp_loc is a single value. */
14242 range.first = extract_bp_num (kind, bp_loc, '\0');
14243 range.second = range.first;
14244 }
14245 return range;
14246 }
14247
14248 /* Extract the breakpoint/location range specified by ARG. Returns
14249 the breakpoint range in BP_NUM_RANGE, and the location range in
14250 BP_LOC_RANGE.
14251
14252 ARG may be in any of the following forms:
14253
14254 x where 'x' is a breakpoint number.
14255 x-y where 'x' and 'y' specify a breakpoint numbers range.
14256 x.y where 'x' is a breakpoint number and 'y' a location number.
14257 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14258 location number range.
14259 */
14260
14261 static void
14262 extract_bp_number_and_location (const std::string &arg,
14263 std::pair<int, int> &bp_num_range,
14264 std::pair<int, int> &bp_loc_range)
14265 {
14266 std::string::size_type dot = arg.find ('.');
14267
14268 if (dot != std::string::npos)
14269 {
14270 /* Handle 'x.y' and 'x.y-z' cases. */
14271
14272 if (arg.length () == dot + 1 || dot == 0)
14273 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
14274
14275 bp_num_range.first
14276 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14277 bp_num_range.second = bp_num_range.first;
14278
14279 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14280 arg, dot + 1);
14281 }
14282 else
14283 {
14284 /* Handle x and x-y cases. */
14285
14286 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
14287 bp_loc_range.first = 0;
14288 bp_loc_range.second = 0;
14289 }
14290 }
14291
14292 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14293 specifies whether to enable or disable. */
14294
14295 static void
14296 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14297 {
14298 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14299 if (loc != NULL)
14300 {
14301 if (loc->disabled_by_cond && enable)
14302 error (_("Breakpoint %d's condition is invalid at location %d, "
14303 "cannot enable."), bp_num, loc_num);
14304
14305 if (loc->enabled != enable)
14306 {
14307 loc->enabled = enable;
14308 mark_breakpoint_location_modified (loc);
14309 }
14310 if (target_supports_enable_disable_tracepoint ()
14311 && current_trace_status ()->running && loc->owner
14312 && is_tracepoint (loc->owner))
14313 target_disable_tracepoint (loc);
14314 }
14315 update_global_location_list (UGLL_DONT_INSERT);
14316
14317 gdb::observers::breakpoint_modified.notify (loc->owner);
14318 }
14319
14320 /* Enable or disable a range of breakpoint locations. BP_NUM is the
14321 number of the breakpoint, and BP_LOC_RANGE specifies the
14322 (inclusive) range of location numbers of that breakpoint to
14323 enable/disable. ENABLE specifies whether to enable or disable the
14324 location. */
14325
14326 static void
14327 enable_disable_breakpoint_location_range (int bp_num,
14328 std::pair<int, int> &bp_loc_range,
14329 bool enable)
14330 {
14331 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14332 enable_disable_bp_num_loc (bp_num, i, enable);
14333 }
14334
14335 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14336 If from_tty is nonzero, it prints a message to that effect,
14337 which ends with a period (no newline). */
14338
14339 void
14340 disable_breakpoint (struct breakpoint *bpt)
14341 {
14342 /* Never disable a watchpoint scope breakpoint; we want to
14343 hit them when we leave scope so we can delete both the
14344 watchpoint and its scope breakpoint at that time. */
14345 if (bpt->type == bp_watchpoint_scope)
14346 return;
14347
14348 bpt->enable_state = bp_disabled;
14349
14350 /* Mark breakpoint locations modified. */
14351 mark_breakpoint_modified (bpt);
14352
14353 if (target_supports_enable_disable_tracepoint ()
14354 && current_trace_status ()->running && is_tracepoint (bpt))
14355 {
14356 struct bp_location *location;
14357
14358 for (location = bpt->loc; location; location = location->next)
14359 target_disable_tracepoint (location);
14360 }
14361
14362 update_global_location_list (UGLL_DONT_INSERT);
14363
14364 gdb::observers::breakpoint_modified.notify (bpt);
14365 }
14366
14367 /* Enable or disable the breakpoint(s) or breakpoint location(s)
14368 specified in ARGS. ARGS may be in any of the formats handled by
14369 extract_bp_number_and_location. ENABLE specifies whether to enable
14370 or disable the breakpoints/locations. */
14371
14372 static void
14373 enable_disable_command (const char *args, int from_tty, bool enable)
14374 {
14375 if (args == 0)
14376 {
14377 for (breakpoint *bpt : all_breakpoints ())
14378 if (user_breakpoint_p (bpt))
14379 {
14380 if (enable)
14381 enable_breakpoint (bpt);
14382 else
14383 disable_breakpoint (bpt);
14384 }
14385 }
14386 else
14387 {
14388 std::string num = extract_arg (&args);
14389
14390 while (!num.empty ())
14391 {
14392 std::pair<int, int> bp_num_range, bp_loc_range;
14393
14394 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14395
14396 if (bp_loc_range.first == bp_loc_range.second
14397 && bp_loc_range.first == 0)
14398 {
14399 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14400 map_breakpoint_number_range (bp_num_range,
14401 enable
14402 ? enable_breakpoint
14403 : disable_breakpoint);
14404 }
14405 else
14406 {
14407 /* Handle breakpoint ids with formats 'x.y' or
14408 'x.y-z'. */
14409 enable_disable_breakpoint_location_range
14410 (bp_num_range.first, bp_loc_range, enable);
14411 }
14412 num = extract_arg (&args);
14413 }
14414 }
14415 }
14416
14417 /* The disable command disables the specified breakpoints/locations
14418 (or all defined breakpoints) so they're no longer effective in
14419 stopping the inferior. ARGS may be in any of the forms defined in
14420 extract_bp_number_and_location. */
14421
14422 static void
14423 disable_command (const char *args, int from_tty)
14424 {
14425 enable_disable_command (args, from_tty, false);
14426 }
14427
14428 static void
14429 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14430 int count)
14431 {
14432 int target_resources_ok;
14433
14434 if (bpt->type == bp_hardware_breakpoint)
14435 {
14436 int i;
14437 i = hw_breakpoint_used_count ();
14438 target_resources_ok =
14439 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14440 i + 1, 0);
14441 if (target_resources_ok == 0)
14442 error (_("No hardware breakpoint support in the target."));
14443 else if (target_resources_ok < 0)
14444 error (_("Hardware breakpoints used exceeds limit."));
14445 }
14446
14447 if (is_watchpoint (bpt))
14448 {
14449 /* Initialize it just to avoid a GCC false warning. */
14450 enum enable_state orig_enable_state = bp_disabled;
14451
14452 try
14453 {
14454 struct watchpoint *w = (struct watchpoint *) bpt;
14455
14456 orig_enable_state = bpt->enable_state;
14457 bpt->enable_state = bp_enabled;
14458 update_watchpoint (w, 1 /* reparse */);
14459 }
14460 catch (const gdb_exception &e)
14461 {
14462 bpt->enable_state = orig_enable_state;
14463 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14464 bpt->number);
14465 return;
14466 }
14467 }
14468
14469 bpt->enable_state = bp_enabled;
14470
14471 /* Mark breakpoint locations modified. */
14472 mark_breakpoint_modified (bpt);
14473
14474 if (target_supports_enable_disable_tracepoint ()
14475 && current_trace_status ()->running && is_tracepoint (bpt))
14476 {
14477 struct bp_location *location;
14478
14479 for (location = bpt->loc; location; location = location->next)
14480 target_enable_tracepoint (location);
14481 }
14482
14483 bpt->disposition = disposition;
14484 bpt->enable_count = count;
14485 update_global_location_list (UGLL_MAY_INSERT);
14486
14487 gdb::observers::breakpoint_modified.notify (bpt);
14488 }
14489
14490
14491 void
14492 enable_breakpoint (struct breakpoint *bpt)
14493 {
14494 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14495 }
14496
14497 /* The enable command enables the specified breakpoints/locations (or
14498 all defined breakpoints) so they once again become (or continue to
14499 be) effective in stopping the inferior. ARGS may be in any of the
14500 forms defined in extract_bp_number_and_location. */
14501
14502 static void
14503 enable_command (const char *args, int from_tty)
14504 {
14505 enable_disable_command (args, from_tty, true);
14506 }
14507
14508 static void
14509 enable_once_command (const char *args, int from_tty)
14510 {
14511 map_breakpoint_numbers
14512 (args, [&] (breakpoint *b)
14513 {
14514 iterate_over_related_breakpoints
14515 (b, [&] (breakpoint *bpt)
14516 {
14517 enable_breakpoint_disp (bpt, disp_disable, 1);
14518 });
14519 });
14520 }
14521
14522 static void
14523 enable_count_command (const char *args, int from_tty)
14524 {
14525 int count;
14526
14527 if (args == NULL)
14528 error_no_arg (_("hit count"));
14529
14530 count = get_number (&args);
14531
14532 map_breakpoint_numbers
14533 (args, [&] (breakpoint *b)
14534 {
14535 iterate_over_related_breakpoints
14536 (b, [&] (breakpoint *bpt)
14537 {
14538 enable_breakpoint_disp (bpt, disp_disable, count);
14539 });
14540 });
14541 }
14542
14543 static void
14544 enable_delete_command (const char *args, int from_tty)
14545 {
14546 map_breakpoint_numbers
14547 (args, [&] (breakpoint *b)
14548 {
14549 iterate_over_related_breakpoints
14550 (b, [&] (breakpoint *bpt)
14551 {
14552 enable_breakpoint_disp (bpt, disp_del, 1);
14553 });
14554 });
14555 }
14556 \f
14557 /* Invalidate last known value of any hardware watchpoint if
14558 the memory which that value represents has been written to by
14559 GDB itself. */
14560
14561 static void
14562 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14563 CORE_ADDR addr, ssize_t len,
14564 const bfd_byte *data)
14565 {
14566 for (breakpoint *bp : all_breakpoints ())
14567 if (bp->enable_state == bp_enabled
14568 && bp->type == bp_hardware_watchpoint)
14569 {
14570 struct watchpoint *wp = (struct watchpoint *) bp;
14571
14572 if (wp->val_valid && wp->val != nullptr)
14573 {
14574 struct bp_location *loc;
14575
14576 for (loc = bp->loc; loc != NULL; loc = loc->next)
14577 if (loc->loc_type == bp_loc_hardware_watchpoint
14578 && loc->address + loc->length > addr
14579 && addr + len > loc->address)
14580 {
14581 wp->val = NULL;
14582 wp->val_valid = false;
14583 }
14584 }
14585 }
14586 }
14587
14588 /* Create and insert a breakpoint for software single step. */
14589
14590 void
14591 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14592 const address_space *aspace,
14593 CORE_ADDR next_pc)
14594 {
14595 struct thread_info *tp = inferior_thread ();
14596 struct symtab_and_line sal;
14597 CORE_ADDR pc = next_pc;
14598
14599 if (tp->control.single_step_breakpoints == NULL)
14600 {
14601 tp->control.single_step_breakpoints
14602 = new_single_step_breakpoint (tp->global_num, gdbarch);
14603 }
14604
14605 sal = find_pc_line (pc, 0);
14606 sal.pc = pc;
14607 sal.section = find_pc_overlay (pc);
14608 sal.explicit_pc = 1;
14609 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14610
14611 update_global_location_list (UGLL_INSERT);
14612 }
14613
14614 /* Insert single step breakpoints according to the current state. */
14615
14616 int
14617 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14618 {
14619 struct regcache *regcache = get_current_regcache ();
14620 std::vector<CORE_ADDR> next_pcs;
14621
14622 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14623
14624 if (!next_pcs.empty ())
14625 {
14626 struct frame_info *frame = get_current_frame ();
14627 const address_space *aspace = get_frame_address_space (frame);
14628
14629 for (CORE_ADDR pc : next_pcs)
14630 insert_single_step_breakpoint (gdbarch, aspace, pc);
14631
14632 return 1;
14633 }
14634 else
14635 return 0;
14636 }
14637
14638 /* See breakpoint.h. */
14639
14640 int
14641 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14642 const address_space *aspace,
14643 CORE_ADDR pc)
14644 {
14645 struct bp_location *loc;
14646
14647 for (loc = bp->loc; loc != NULL; loc = loc->next)
14648 if (loc->inserted
14649 && breakpoint_location_address_match (loc, aspace, pc))
14650 return 1;
14651
14652 return 0;
14653 }
14654
14655 /* Check whether a software single-step breakpoint is inserted at
14656 PC. */
14657
14658 int
14659 single_step_breakpoint_inserted_here_p (const address_space *aspace,
14660 CORE_ADDR pc)
14661 {
14662 for (breakpoint *bpt : all_breakpoints ())
14663 {
14664 if (bpt->type == bp_single_step
14665 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14666 return 1;
14667 }
14668 return 0;
14669 }
14670
14671 /* Tracepoint-specific operations. */
14672
14673 /* Set tracepoint count to NUM. */
14674 static void
14675 set_tracepoint_count (int num)
14676 {
14677 tracepoint_count = num;
14678 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14679 }
14680
14681 static void
14682 trace_command (const char *arg, int from_tty)
14683 {
14684 event_location_up location = string_to_event_location (&arg,
14685 current_language);
14686 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
14687 (location.get (), true /* is_tracepoint */);
14688
14689 create_breakpoint (get_current_arch (),
14690 location.get (),
14691 NULL, 0, arg, false, 1 /* parse arg */,
14692 0 /* tempflag */,
14693 bp_tracepoint /* type_wanted */,
14694 0 /* Ignore count */,
14695 pending_break_support,
14696 ops,
14697 from_tty,
14698 1 /* enabled */,
14699 0 /* internal */, 0);
14700 }
14701
14702 static void
14703 ftrace_command (const char *arg, int from_tty)
14704 {
14705 event_location_up location = string_to_event_location (&arg,
14706 current_language);
14707 create_breakpoint (get_current_arch (),
14708 location.get (),
14709 NULL, 0, arg, false, 1 /* parse arg */,
14710 0 /* tempflag */,
14711 bp_fast_tracepoint /* type_wanted */,
14712 0 /* Ignore count */,
14713 pending_break_support,
14714 &tracepoint_breakpoint_ops,
14715 from_tty,
14716 1 /* enabled */,
14717 0 /* internal */, 0);
14718 }
14719
14720 /* strace command implementation. Creates a static tracepoint. */
14721
14722 static void
14723 strace_command (const char *arg, int from_tty)
14724 {
14725 struct breakpoint_ops *ops;
14726 event_location_up location;
14727
14728 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14729 or with a normal static tracepoint. */
14730 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14731 {
14732 ops = &strace_marker_breakpoint_ops;
14733 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
14734 }
14735 else
14736 {
14737 ops = &tracepoint_breakpoint_ops;
14738 location = string_to_event_location (&arg, current_language);
14739 }
14740
14741 create_breakpoint (get_current_arch (),
14742 location.get (),
14743 NULL, 0, arg, false, 1 /* parse arg */,
14744 0 /* tempflag */,
14745 bp_static_tracepoint /* type_wanted */,
14746 0 /* Ignore count */,
14747 pending_break_support,
14748 ops,
14749 from_tty,
14750 1 /* enabled */,
14751 0 /* internal */, 0);
14752 }
14753
14754 /* Set up a fake reader function that gets command lines from a linked
14755 list that was acquired during tracepoint uploading. */
14756
14757 static struct uploaded_tp *this_utp;
14758 static int next_cmd;
14759
14760 static char *
14761 read_uploaded_action (void)
14762 {
14763 char *rslt = nullptr;
14764
14765 if (next_cmd < this_utp->cmd_strings.size ())
14766 {
14767 rslt = this_utp->cmd_strings[next_cmd].get ();
14768 next_cmd++;
14769 }
14770
14771 return rslt;
14772 }
14773
14774 /* Given information about a tracepoint as recorded on a target (which
14775 can be either a live system or a trace file), attempt to create an
14776 equivalent GDB tracepoint. This is not a reliable process, since
14777 the target does not necessarily have all the information used when
14778 the tracepoint was originally defined. */
14779
14780 struct tracepoint *
14781 create_tracepoint_from_upload (struct uploaded_tp *utp)
14782 {
14783 const char *addr_str;
14784 char small_buf[100];
14785 struct tracepoint *tp;
14786
14787 if (utp->at_string)
14788 addr_str = utp->at_string.get ();
14789 else
14790 {
14791 /* In the absence of a source location, fall back to raw
14792 address. Since there is no way to confirm that the address
14793 means the same thing as when the trace was started, warn the
14794 user. */
14795 warning (_("Uploaded tracepoint %d has no "
14796 "source location, using raw address"),
14797 utp->number);
14798 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14799 addr_str = small_buf;
14800 }
14801
14802 /* There's not much we can do with a sequence of bytecodes. */
14803 if (utp->cond && !utp->cond_string)
14804 warning (_("Uploaded tracepoint %d condition "
14805 "has no source form, ignoring it"),
14806 utp->number);
14807
14808 event_location_up location = string_to_event_location (&addr_str,
14809 current_language);
14810 if (!create_breakpoint (get_current_arch (),
14811 location.get (),
14812 utp->cond_string.get (), -1, addr_str,
14813 false /* force_condition */,
14814 0 /* parse cond/thread */,
14815 0 /* tempflag */,
14816 utp->type /* type_wanted */,
14817 0 /* Ignore count */,
14818 pending_break_support,
14819 &tracepoint_breakpoint_ops,
14820 0 /* from_tty */,
14821 utp->enabled /* enabled */,
14822 0 /* internal */,
14823 CREATE_BREAKPOINT_FLAGS_INSERTED))
14824 return NULL;
14825
14826 /* Get the tracepoint we just created. */
14827 tp = get_tracepoint (tracepoint_count);
14828 gdb_assert (tp != NULL);
14829
14830 if (utp->pass > 0)
14831 {
14832 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14833 tp->number);
14834
14835 trace_pass_command (small_buf, 0);
14836 }
14837
14838 /* If we have uploaded versions of the original commands, set up a
14839 special-purpose "reader" function and call the usual command line
14840 reader, then pass the result to the breakpoint command-setting
14841 function. */
14842 if (!utp->cmd_strings.empty ())
14843 {
14844 counted_command_line cmd_list;
14845
14846 this_utp = utp;
14847 next_cmd = 0;
14848
14849 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
14850
14851 breakpoint_set_commands (tp, std::move (cmd_list));
14852 }
14853 else if (!utp->actions.empty ()
14854 || !utp->step_actions.empty ())
14855 warning (_("Uploaded tracepoint %d actions "
14856 "have no source form, ignoring them"),
14857 utp->number);
14858
14859 /* Copy any status information that might be available. */
14860 tp->hit_count = utp->hit_count;
14861 tp->traceframe_usage = utp->traceframe_usage;
14862
14863 return tp;
14864 }
14865
14866 /* Print information on tracepoint number TPNUM_EXP, or all if
14867 omitted. */
14868
14869 static void
14870 info_tracepoints_command (const char *args, int from_tty)
14871 {
14872 struct ui_out *uiout = current_uiout;
14873 int num_printed;
14874
14875 num_printed = breakpoint_1 (args, false, is_tracepoint);
14876
14877 if (num_printed == 0)
14878 {
14879 if (args == NULL || *args == '\0')
14880 uiout->message ("No tracepoints.\n");
14881 else
14882 uiout->message ("No tracepoint matching '%s'.\n", args);
14883 }
14884
14885 default_collect_info ();
14886 }
14887
14888 /* The 'enable trace' command enables tracepoints.
14889 Not supported by all targets. */
14890 static void
14891 enable_trace_command (const char *args, int from_tty)
14892 {
14893 enable_command (args, from_tty);
14894 }
14895
14896 /* The 'disable trace' command disables tracepoints.
14897 Not supported by all targets. */
14898 static void
14899 disable_trace_command (const char *args, int from_tty)
14900 {
14901 disable_command (args, from_tty);
14902 }
14903
14904 /* Remove a tracepoint (or all if no argument). */
14905 static void
14906 delete_trace_command (const char *arg, int from_tty)
14907 {
14908 dont_repeat ();
14909
14910 if (arg == 0)
14911 {
14912 int breaks_to_delete = 0;
14913 breakpoint *tp;
14914
14915 /* Delete all breakpoints if no argument.
14916 Do not delete internal or call-dummy breakpoints, these
14917 have to be deleted with an explicit breakpoint number
14918 argument. */
14919 ALL_TRACEPOINTS (tp)
14920 if (is_tracepoint (tp) && user_breakpoint_p (tp))
14921 {
14922 breaks_to_delete = 1;
14923 break;
14924 }
14925
14926 /* Ask user only if there are some breakpoints to delete. */
14927 if (!from_tty
14928 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14929 {
14930 for (breakpoint *b : all_breakpoints_safe ())
14931 if (is_tracepoint (b) && user_breakpoint_p (b))
14932 delete_breakpoint (b);
14933 }
14934 }
14935 else
14936 map_breakpoint_numbers
14937 (arg, [&] (breakpoint *br)
14938 {
14939 iterate_over_related_breakpoints (br, delete_breakpoint);
14940 });
14941 }
14942
14943 /* Helper function for trace_pass_command. */
14944
14945 static void
14946 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14947 {
14948 tp->pass_count = count;
14949 gdb::observers::breakpoint_modified.notify (tp);
14950 if (from_tty)
14951 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
14952 tp->number, count);
14953 }
14954
14955 /* Set passcount for tracepoint.
14956
14957 First command argument is passcount, second is tracepoint number.
14958 If tracepoint number omitted, apply to most recently defined.
14959 Also accepts special argument "all". */
14960
14961 static void
14962 trace_pass_command (const char *args, int from_tty)
14963 {
14964 struct tracepoint *t1;
14965 ULONGEST count;
14966
14967 if (args == 0 || *args == 0)
14968 error (_("passcount command requires an "
14969 "argument (count + optional TP num)"));
14970
14971 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
14972
14973 args = skip_spaces (args);
14974 if (*args && strncasecmp (args, "all", 3) == 0)
14975 {
14976 struct breakpoint *b;
14977
14978 args += 3; /* Skip special argument "all". */
14979 if (*args)
14980 error (_("Junk at end of arguments."));
14981
14982 ALL_TRACEPOINTS (b)
14983 {
14984 t1 = (struct tracepoint *) b;
14985 trace_pass_set_count (t1, count, from_tty);
14986 }
14987 }
14988 else if (*args == '\0')
14989 {
14990 t1 = get_tracepoint_by_number (&args, NULL);
14991 if (t1)
14992 trace_pass_set_count (t1, count, from_tty);
14993 }
14994 else
14995 {
14996 number_or_range_parser parser (args);
14997 while (!parser.finished ())
14998 {
14999 t1 = get_tracepoint_by_number (&args, &parser);
15000 if (t1)
15001 trace_pass_set_count (t1, count, from_tty);
15002 }
15003 }
15004 }
15005
15006 struct tracepoint *
15007 get_tracepoint (int num)
15008 {
15009 struct breakpoint *t;
15010
15011 ALL_TRACEPOINTS (t)
15012 if (t->number == num)
15013 return (struct tracepoint *) t;
15014
15015 return NULL;
15016 }
15017
15018 /* Find the tracepoint with the given target-side number (which may be
15019 different from the tracepoint number after disconnecting and
15020 reconnecting). */
15021
15022 struct tracepoint *
15023 get_tracepoint_by_number_on_target (int num)
15024 {
15025 struct breakpoint *b;
15026
15027 ALL_TRACEPOINTS (b)
15028 {
15029 struct tracepoint *t = (struct tracepoint *) b;
15030
15031 if (t->number_on_target == num)
15032 return t;
15033 }
15034
15035 return NULL;
15036 }
15037
15038 /* Utility: parse a tracepoint number and look it up in the list.
15039 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15040 If the argument is missing, the most recent tracepoint
15041 (tracepoint_count) is returned. */
15042
15043 struct tracepoint *
15044 get_tracepoint_by_number (const char **arg,
15045 number_or_range_parser *parser)
15046 {
15047 struct breakpoint *t;
15048 int tpnum;
15049 const char *instring = arg == NULL ? NULL : *arg;
15050
15051 if (parser != NULL)
15052 {
15053 gdb_assert (!parser->finished ());
15054 tpnum = parser->get_number ();
15055 }
15056 else if (arg == NULL || *arg == NULL || ! **arg)
15057 tpnum = tracepoint_count;
15058 else
15059 tpnum = get_number (arg);
15060
15061 if (tpnum <= 0)
15062 {
15063 if (instring && *instring)
15064 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15065 instring);
15066 else
15067 printf_filtered (_("No previous tracepoint\n"));
15068 return NULL;
15069 }
15070
15071 ALL_TRACEPOINTS (t)
15072 if (t->number == tpnum)
15073 {
15074 return (struct tracepoint *) t;
15075 }
15076
15077 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15078 return NULL;
15079 }
15080
15081 void
15082 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15083 {
15084 if (b->thread != -1)
15085 fprintf_unfiltered (fp, " thread %d", b->thread);
15086
15087 if (b->task != 0)
15088 fprintf_unfiltered (fp, " task %d", b->task);
15089
15090 fprintf_unfiltered (fp, "\n");
15091 }
15092
15093 /* Save information on user settable breakpoints (watchpoints, etc) to
15094 a new script file named FILENAME. If FILTER is non-NULL, call it
15095 on each breakpoint and only include the ones for which it returns
15096 true. */
15097
15098 static void
15099 save_breakpoints (const char *filename, int from_tty,
15100 bool (*filter) (const struct breakpoint *))
15101 {
15102 int any = 0;
15103 int extra_trace_bits = 0;
15104
15105 if (filename == 0 || *filename == 0)
15106 error (_("Argument required (file name in which to save)"));
15107
15108 /* See if we have anything to save. */
15109 for (breakpoint *tp : all_breakpoints ())
15110 {
15111 /* Skip internal and momentary breakpoints. */
15112 if (!user_breakpoint_p (tp))
15113 continue;
15114
15115 /* If we have a filter, only save the breakpoints it accepts. */
15116 if (filter && !filter (tp))
15117 continue;
15118
15119 any = 1;
15120
15121 if (is_tracepoint (tp))
15122 {
15123 extra_trace_bits = 1;
15124
15125 /* We can stop searching. */
15126 break;
15127 }
15128 }
15129
15130 if (!any)
15131 {
15132 warning (_("Nothing to save."));
15133 return;
15134 }
15135
15136 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
15137
15138 stdio_file fp;
15139
15140 if (!fp.open (expanded_filename.get (), "w"))
15141 error (_("Unable to open file '%s' for saving (%s)"),
15142 expanded_filename.get (), safe_strerror (errno));
15143
15144 if (extra_trace_bits)
15145 save_trace_state_variables (&fp);
15146
15147 for (breakpoint *tp : all_breakpoints ())
15148 {
15149 /* Skip internal and momentary breakpoints. */
15150 if (!user_breakpoint_p (tp))
15151 continue;
15152
15153 /* If we have a filter, only save the breakpoints it accepts. */
15154 if (filter && !filter (tp))
15155 continue;
15156
15157 tp->ops->print_recreate (tp, &fp);
15158
15159 /* Note, we can't rely on tp->number for anything, as we can't
15160 assume the recreated breakpoint numbers will match. Use $bpnum
15161 instead. */
15162
15163 if (tp->cond_string)
15164 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15165
15166 if (tp->ignore_count)
15167 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15168
15169 if (tp->type != bp_dprintf && tp->commands)
15170 {
15171 fp.puts (" commands\n");
15172
15173 current_uiout->redirect (&fp);
15174 try
15175 {
15176 print_command_lines (current_uiout, tp->commands.get (), 2);
15177 }
15178 catch (const gdb_exception &ex)
15179 {
15180 current_uiout->redirect (NULL);
15181 throw;
15182 }
15183
15184 current_uiout->redirect (NULL);
15185 fp.puts (" end\n");
15186 }
15187
15188 if (tp->enable_state == bp_disabled)
15189 fp.puts ("disable $bpnum\n");
15190
15191 /* If this is a multi-location breakpoint, check if the locations
15192 should be individually disabled. Watchpoint locations are
15193 special, and not user visible. */
15194 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15195 {
15196 struct bp_location *loc;
15197 int n = 1;
15198
15199 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15200 if (!loc->enabled)
15201 fp.printf ("disable $bpnum.%d\n", n);
15202 }
15203 }
15204
15205 if (extra_trace_bits && *default_collect)
15206 fp.printf ("set default-collect %s\n", default_collect);
15207
15208 if (from_tty)
15209 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
15210 }
15211
15212 /* The `save breakpoints' command. */
15213
15214 static void
15215 save_breakpoints_command (const char *args, int from_tty)
15216 {
15217 save_breakpoints (args, from_tty, NULL);
15218 }
15219
15220 /* The `save tracepoints' command. */
15221
15222 static void
15223 save_tracepoints_command (const char *args, int from_tty)
15224 {
15225 save_breakpoints (args, from_tty, is_tracepoint);
15226 }
15227
15228 /* Create a vector of all tracepoints. */
15229
15230 std::vector<breakpoint *>
15231 all_tracepoints (void)
15232 {
15233 std::vector<breakpoint *> tp_vec;
15234 struct breakpoint *tp;
15235
15236 ALL_TRACEPOINTS (tp)
15237 {
15238 tp_vec.push_back (tp);
15239 }
15240
15241 return tp_vec;
15242 }
15243
15244 \f
15245 /* This help string is used to consolidate all the help string for specifying
15246 locations used by several commands. */
15247
15248 #define LOCATION_HELP_STRING \
15249 "Linespecs are colon-separated lists of location parameters, such as\n\
15250 source filename, function name, label name, and line number.\n\
15251 Example: To specify the start of a label named \"the_top\" in the\n\
15252 function \"fact\" in the file \"factorial.c\", use\n\
15253 \"factorial.c:fact:the_top\".\n\
15254 \n\
15255 Address locations begin with \"*\" and specify an exact address in the\n\
15256 program. Example: To specify the fourth byte past the start function\n\
15257 \"main\", use \"*main + 4\".\n\
15258 \n\
15259 Explicit locations are similar to linespecs but use an option/argument\n\
15260 syntax to specify location parameters.\n\
15261 Example: To specify the start of the label named \"the_top\" in the\n\
15262 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15263 -function fact -label the_top\".\n\
15264 \n\
15265 By default, a specified function is matched against the program's\n\
15266 functions in all scopes. For C++, this means in all namespaces and\n\
15267 classes. For Ada, this means in all packages. E.g., in C++,\n\
15268 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15269 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
15270 specified name as a complete fully-qualified name instead."
15271
15272 /* This help string is used for the break, hbreak, tbreak and thbreak
15273 commands. It is defined as a macro to prevent duplication.
15274 COMMAND should be a string constant containing the name of the
15275 command. */
15276
15277 #define BREAK_ARGS_HELP(command) \
15278 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
15279 \t[-force-condition] [if CONDITION]\n\
15280 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15281 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15282 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15283 `-probe-dtrace' (for a DTrace probe).\n\
15284 LOCATION may be a linespec, address, or explicit location as described\n\
15285 below.\n\
15286 \n\
15287 With no LOCATION, uses current execution address of the selected\n\
15288 stack frame. This is useful for breaking on return to a stack frame.\n\
15289 \n\
15290 THREADNUM is the number from \"info threads\".\n\
15291 CONDITION is a boolean expression.\n\
15292 \n\
15293 With the \"-force-condition\" flag, the condition is defined even when\n\
15294 it is invalid for all current locations.\n\
15295 \n" LOCATION_HELP_STRING "\n\n\
15296 Multiple breakpoints at one place are permitted, and useful if their\n\
15297 conditions are different.\n\
15298 \n\
15299 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15300
15301 /* List of subcommands for "catch". */
15302 static struct cmd_list_element *catch_cmdlist;
15303
15304 /* List of subcommands for "tcatch". */
15305 static struct cmd_list_element *tcatch_cmdlist;
15306
15307 void
15308 add_catch_command (const char *name, const char *docstring,
15309 cmd_const_sfunc_ftype *sfunc,
15310 completer_ftype *completer,
15311 void *user_data_catch,
15312 void *user_data_tcatch)
15313 {
15314 struct cmd_list_element *command;
15315
15316 command = add_cmd (name, class_breakpoint, docstring,
15317 &catch_cmdlist);
15318 set_cmd_sfunc (command, sfunc);
15319 set_cmd_context (command, user_data_catch);
15320 set_cmd_completer (command, completer);
15321
15322 command = add_cmd (name, class_breakpoint, docstring,
15323 &tcatch_cmdlist);
15324 set_cmd_sfunc (command, sfunc);
15325 set_cmd_context (command, user_data_tcatch);
15326 set_cmd_completer (command, completer);
15327 }
15328
15329 struct breakpoint *
15330 iterate_over_breakpoints (gdb::function_view<bool (breakpoint *)> callback)
15331 {
15332 for (breakpoint *b : all_breakpoints_safe ())
15333 if (callback (b))
15334 return b;
15335
15336 return NULL;
15337 }
15338
15339 /* Zero if any of the breakpoint's locations could be a location where
15340 functions have been inlined, nonzero otherwise. */
15341
15342 static int
15343 is_non_inline_function (struct breakpoint *b)
15344 {
15345 /* The shared library event breakpoint is set on the address of a
15346 non-inline function. */
15347 if (b->type == bp_shlib_event)
15348 return 1;
15349
15350 return 0;
15351 }
15352
15353 /* Nonzero if the specified PC cannot be a location where functions
15354 have been inlined. */
15355
15356 int
15357 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
15358 const struct target_waitstatus *ws)
15359 {
15360 struct bp_location *bl;
15361
15362 for (breakpoint *b : all_breakpoints ())
15363 {
15364 if (!is_non_inline_function (b))
15365 continue;
15366
15367 for (bl = b->loc; bl != NULL; bl = bl->next)
15368 {
15369 if (!bl->shlib_disabled
15370 && bpstat_check_location (bl, aspace, pc, ws))
15371 return 1;
15372 }
15373 }
15374
15375 return 0;
15376 }
15377
15378 /* Remove any references to OBJFILE which is going to be freed. */
15379
15380 void
15381 breakpoint_free_objfile (struct objfile *objfile)
15382 {
15383 struct bp_location **locp, *loc;
15384
15385 ALL_BP_LOCATIONS (loc, locp)
15386 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15387 loc->symtab = NULL;
15388 }
15389
15390 void
15391 initialize_breakpoint_ops (void)
15392 {
15393 static int initialized = 0;
15394
15395 struct breakpoint_ops *ops;
15396
15397 if (initialized)
15398 return;
15399 initialized = 1;
15400
15401 /* The breakpoint_ops structure to be inherit by all kinds of
15402 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15403 internal and momentary breakpoints, etc.). */
15404 ops = &bkpt_base_breakpoint_ops;
15405 *ops = base_breakpoint_ops;
15406 ops->re_set = bkpt_re_set;
15407 ops->insert_location = bkpt_insert_location;
15408 ops->remove_location = bkpt_remove_location;
15409 ops->breakpoint_hit = bkpt_breakpoint_hit;
15410 ops->create_sals_from_location = bkpt_create_sals_from_location;
15411 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15412 ops->decode_location = bkpt_decode_location;
15413
15414 /* The breakpoint_ops structure to be used in regular breakpoints. */
15415 ops = &bkpt_breakpoint_ops;
15416 *ops = bkpt_base_breakpoint_ops;
15417 ops->re_set = bkpt_re_set;
15418 ops->resources_needed = bkpt_resources_needed;
15419 ops->print_it = bkpt_print_it;
15420 ops->print_mention = bkpt_print_mention;
15421 ops->print_recreate = bkpt_print_recreate;
15422
15423 /* Ranged breakpoints. */
15424 ops = &ranged_breakpoint_ops;
15425 *ops = bkpt_breakpoint_ops;
15426 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15427 ops->resources_needed = resources_needed_ranged_breakpoint;
15428 ops->print_it = print_it_ranged_breakpoint;
15429 ops->print_one = print_one_ranged_breakpoint;
15430 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15431 ops->print_mention = print_mention_ranged_breakpoint;
15432 ops->print_recreate = print_recreate_ranged_breakpoint;
15433
15434 /* Internal breakpoints. */
15435 ops = &internal_breakpoint_ops;
15436 *ops = bkpt_base_breakpoint_ops;
15437 ops->re_set = internal_bkpt_re_set;
15438 ops->check_status = internal_bkpt_check_status;
15439 ops->print_it = internal_bkpt_print_it;
15440 ops->print_mention = internal_bkpt_print_mention;
15441
15442 /* Momentary breakpoints. */
15443 ops = &momentary_breakpoint_ops;
15444 *ops = bkpt_base_breakpoint_ops;
15445 ops->re_set = momentary_bkpt_re_set;
15446 ops->check_status = momentary_bkpt_check_status;
15447 ops->print_it = momentary_bkpt_print_it;
15448 ops->print_mention = momentary_bkpt_print_mention;
15449
15450 /* Probe breakpoints. */
15451 ops = &bkpt_probe_breakpoint_ops;
15452 *ops = bkpt_breakpoint_ops;
15453 ops->insert_location = bkpt_probe_insert_location;
15454 ops->remove_location = bkpt_probe_remove_location;
15455 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15456 ops->decode_location = bkpt_probe_decode_location;
15457
15458 /* Watchpoints. */
15459 ops = &watchpoint_breakpoint_ops;
15460 *ops = base_breakpoint_ops;
15461 ops->re_set = re_set_watchpoint;
15462 ops->insert_location = insert_watchpoint;
15463 ops->remove_location = remove_watchpoint;
15464 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15465 ops->check_status = check_status_watchpoint;
15466 ops->resources_needed = resources_needed_watchpoint;
15467 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15468 ops->print_it = print_it_watchpoint;
15469 ops->print_mention = print_mention_watchpoint;
15470 ops->print_recreate = print_recreate_watchpoint;
15471 ops->explains_signal = explains_signal_watchpoint;
15472
15473 /* Masked watchpoints. */
15474 ops = &masked_watchpoint_breakpoint_ops;
15475 *ops = watchpoint_breakpoint_ops;
15476 ops->insert_location = insert_masked_watchpoint;
15477 ops->remove_location = remove_masked_watchpoint;
15478 ops->resources_needed = resources_needed_masked_watchpoint;
15479 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15480 ops->print_it = print_it_masked_watchpoint;
15481 ops->print_one_detail = print_one_detail_masked_watchpoint;
15482 ops->print_mention = print_mention_masked_watchpoint;
15483 ops->print_recreate = print_recreate_masked_watchpoint;
15484
15485 /* Tracepoints. */
15486 ops = &tracepoint_breakpoint_ops;
15487 *ops = base_breakpoint_ops;
15488 ops->re_set = tracepoint_re_set;
15489 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15490 ops->print_one_detail = tracepoint_print_one_detail;
15491 ops->print_mention = tracepoint_print_mention;
15492 ops->print_recreate = tracepoint_print_recreate;
15493 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15494 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15495 ops->decode_location = tracepoint_decode_location;
15496
15497 /* Probe tracepoints. */
15498 ops = &tracepoint_probe_breakpoint_ops;
15499 *ops = tracepoint_breakpoint_ops;
15500 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15501 ops->decode_location = tracepoint_probe_decode_location;
15502
15503 /* Static tracepoints with marker (`-m'). */
15504 ops = &strace_marker_breakpoint_ops;
15505 *ops = tracepoint_breakpoint_ops;
15506 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15507 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15508 ops->decode_location = strace_marker_decode_location;
15509
15510 /* Fork catchpoints. */
15511 ops = &catch_fork_breakpoint_ops;
15512 *ops = base_breakpoint_ops;
15513 ops->insert_location = insert_catch_fork;
15514 ops->remove_location = remove_catch_fork;
15515 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15516 ops->print_it = print_it_catch_fork;
15517 ops->print_one = print_one_catch_fork;
15518 ops->print_mention = print_mention_catch_fork;
15519 ops->print_recreate = print_recreate_catch_fork;
15520
15521 /* Vfork catchpoints. */
15522 ops = &catch_vfork_breakpoint_ops;
15523 *ops = base_breakpoint_ops;
15524 ops->insert_location = insert_catch_vfork;
15525 ops->remove_location = remove_catch_vfork;
15526 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15527 ops->print_it = print_it_catch_vfork;
15528 ops->print_one = print_one_catch_vfork;
15529 ops->print_mention = print_mention_catch_vfork;
15530 ops->print_recreate = print_recreate_catch_vfork;
15531
15532 /* Exec catchpoints. */
15533 ops = &catch_exec_breakpoint_ops;
15534 *ops = base_breakpoint_ops;
15535 ops->insert_location = insert_catch_exec;
15536 ops->remove_location = remove_catch_exec;
15537 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15538 ops->print_it = print_it_catch_exec;
15539 ops->print_one = print_one_catch_exec;
15540 ops->print_mention = print_mention_catch_exec;
15541 ops->print_recreate = print_recreate_catch_exec;
15542
15543 /* Solib-related catchpoints. */
15544 ops = &catch_solib_breakpoint_ops;
15545 *ops = base_breakpoint_ops;
15546 ops->insert_location = insert_catch_solib;
15547 ops->remove_location = remove_catch_solib;
15548 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15549 ops->check_status = check_status_catch_solib;
15550 ops->print_it = print_it_catch_solib;
15551 ops->print_one = print_one_catch_solib;
15552 ops->print_mention = print_mention_catch_solib;
15553 ops->print_recreate = print_recreate_catch_solib;
15554
15555 ops = &dprintf_breakpoint_ops;
15556 *ops = bkpt_base_breakpoint_ops;
15557 ops->re_set = dprintf_re_set;
15558 ops->resources_needed = bkpt_resources_needed;
15559 ops->print_it = bkpt_print_it;
15560 ops->print_mention = bkpt_print_mention;
15561 ops->print_recreate = dprintf_print_recreate;
15562 ops->after_condition_true = dprintf_after_condition_true;
15563 ops->breakpoint_hit = dprintf_breakpoint_hit;
15564 }
15565
15566 /* Chain containing all defined "enable breakpoint" subcommands. */
15567
15568 static struct cmd_list_element *enablebreaklist = NULL;
15569
15570 /* See breakpoint.h. */
15571
15572 cmd_list_element *commands_cmd_element = nullptr;
15573
15574 void _initialize_breakpoint ();
15575 void
15576 _initialize_breakpoint ()
15577 {
15578 struct cmd_list_element *c;
15579
15580 initialize_breakpoint_ops ();
15581
15582 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
15583 "breakpoint");
15584 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
15585 "breakpoint");
15586 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
15587 "breakpoint");
15588
15589 breakpoint_chain = 0;
15590 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15591 before a breakpoint is set. */
15592 breakpoint_count = 0;
15593
15594 tracepoint_count = 0;
15595
15596 add_com ("ignore", class_breakpoint, ignore_command, _("\
15597 Set ignore-count of breakpoint number N to COUNT.\n\
15598 Usage is `ignore N COUNT'."));
15599
15600 commands_cmd_element = add_com ("commands", class_breakpoint,
15601 commands_command, _("\
15602 Set commands to be executed when the given breakpoints are hit.\n\
15603 Give a space-separated breakpoint list as argument after \"commands\".\n\
15604 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15605 (e.g. `5-7').\n\
15606 With no argument, the targeted breakpoint is the last one set.\n\
15607 The commands themselves follow starting on the next line.\n\
15608 Type a line containing \"end\" to indicate the end of them.\n\
15609 Give \"silent\" as the first line to make the breakpoint silent;\n\
15610 then no output is printed when it is hit, except what the commands print."));
15611
15612 const auto cc_opts = make_condition_command_options_def_group (nullptr);
15613 static std::string condition_command_help
15614 = gdb::option::build_help (_("\
15615 Specify breakpoint number N to break only if COND is true.\n\
15616 Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
15617 is an expression to be evaluated whenever breakpoint N is reached.\n\
15618 \n\
15619 Options:\n\
15620 %OPTIONS%"), cc_opts);
15621
15622 c = add_com ("condition", class_breakpoint, condition_command,
15623 condition_command_help.c_str ());
15624 set_cmd_completer_handle_brkchars (c, condition_completer);
15625
15626 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15627 Set a temporary breakpoint.\n\
15628 Like \"break\" except the breakpoint is only temporary,\n\
15629 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15630 by using \"enable delete\" on the breakpoint number.\n\
15631 \n"
15632 BREAK_ARGS_HELP ("tbreak")));
15633 set_cmd_completer (c, location_completer);
15634
15635 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15636 Set a hardware assisted breakpoint.\n\
15637 Like \"break\" except the breakpoint requires hardware support,\n\
15638 some target hardware may not have this support.\n\
15639 \n"
15640 BREAK_ARGS_HELP ("hbreak")));
15641 set_cmd_completer (c, location_completer);
15642
15643 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15644 Set a temporary hardware assisted breakpoint.\n\
15645 Like \"hbreak\" except the breakpoint is only temporary,\n\
15646 so it will be deleted when hit.\n\
15647 \n"
15648 BREAK_ARGS_HELP ("thbreak")));
15649 set_cmd_completer (c, location_completer);
15650
15651 cmd_list_element *enable_cmd
15652 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15653 Enable all or some breakpoints.\n\
15654 Usage: enable [BREAKPOINTNUM]...\n\
15655 Give breakpoint numbers (separated by spaces) as arguments.\n\
15656 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15657 This is used to cancel the effect of the \"disable\" command.\n\
15658 With a subcommand you can enable temporarily."),
15659 &enablelist, 1, &cmdlist);
15660
15661 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
15662
15663 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15664 Enable all or some breakpoints.\n\
15665 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
15666 Give breakpoint numbers (separated by spaces) as arguments.\n\
15667 This is used to cancel the effect of the \"disable\" command.\n\
15668 May be abbreviated to simply \"enable\"."),
15669 &enablebreaklist, 1, &enablelist);
15670
15671 add_cmd ("once", no_class, enable_once_command, _("\
15672 Enable some breakpoints for one hit.\n\
15673 Usage: enable breakpoints once BREAKPOINTNUM...\n\
15674 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15675 &enablebreaklist);
15676
15677 add_cmd ("delete", no_class, enable_delete_command, _("\
15678 Enable some breakpoints and delete when hit.\n\
15679 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
15680 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15681 &enablebreaklist);
15682
15683 add_cmd ("count", no_class, enable_count_command, _("\
15684 Enable some breakpoints for COUNT hits.\n\
15685 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
15686 If a breakpoint is hit while enabled in this fashion,\n\
15687 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15688 &enablebreaklist);
15689
15690 add_cmd ("delete", no_class, enable_delete_command, _("\
15691 Enable some breakpoints and delete when hit.\n\
15692 Usage: enable delete BREAKPOINTNUM...\n\
15693 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15694 &enablelist);
15695
15696 add_cmd ("once", no_class, enable_once_command, _("\
15697 Enable some breakpoints for one hit.\n\
15698 Usage: enable once BREAKPOINTNUM...\n\
15699 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15700 &enablelist);
15701
15702 add_cmd ("count", no_class, enable_count_command, _("\
15703 Enable some breakpoints for COUNT hits.\n\
15704 Usage: enable count COUNT BREAKPOINTNUM...\n\
15705 If a breakpoint is hit while enabled in this fashion,\n\
15706 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15707 &enablelist);
15708
15709 cmd_list_element *disable_cmd
15710 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15711 Disable all or some breakpoints.\n\
15712 Usage: disable [BREAKPOINTNUM]...\n\
15713 Arguments are breakpoint numbers with spaces in between.\n\
15714 To disable all breakpoints, give no argument.\n\
15715 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15716 &disablelist, 1, &cmdlist);
15717 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
15718 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
15719
15720 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
15721 Disable all or some breakpoints.\n\
15722 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
15723 Arguments are breakpoint numbers with spaces in between.\n\
15724 To disable all breakpoints, give no argument.\n\
15725 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15726 This command may be abbreviated \"disable\"."),
15727 &disablelist);
15728
15729 cmd_list_element *delete_cmd
15730 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15731 Delete all or some breakpoints.\n\
15732 Usage: delete [BREAKPOINTNUM]...\n\
15733 Arguments are breakpoint numbers with spaces in between.\n\
15734 To delete all breakpoints, give no argument.\n\
15735 \n\
15736 Also a prefix command for deletion of other GDB objects."),
15737 &deletelist, 1, &cmdlist);
15738 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
15739 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
15740
15741 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
15742 Delete all or some breakpoints or auto-display expressions.\n\
15743 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
15744 Arguments are breakpoint numbers with spaces in between.\n\
15745 To delete all breakpoints, give no argument.\n\
15746 This command may be abbreviated \"delete\"."),
15747 &deletelist);
15748
15749 cmd_list_element *clear_cmd
15750 = add_com ("clear", class_breakpoint, clear_command, _("\
15751 Clear breakpoint at specified location.\n\
15752 Argument may be a linespec, explicit, or address location as described below.\n\
15753 \n\
15754 With no argument, clears all breakpoints in the line that the selected frame\n\
15755 is executing in.\n"
15756 "\n" LOCATION_HELP_STRING "\n\n\
15757 See also the \"delete\" command which clears breakpoints by number."));
15758 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
15759
15760 cmd_list_element *break_cmd
15761 = add_com ("break", class_breakpoint, break_command, _("\
15762 Set breakpoint at specified location.\n"
15763 BREAK_ARGS_HELP ("break")));
15764 set_cmd_completer (break_cmd, location_completer);
15765
15766 add_com_alias ("b", break_cmd, class_run, 1);
15767 add_com_alias ("br", break_cmd, class_run, 1);
15768 add_com_alias ("bre", break_cmd, class_run, 1);
15769 add_com_alias ("brea", break_cmd, class_run, 1);
15770
15771 if (dbx_commands)
15772 {
15773 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15774 Break in function/address or break at a line in the current file."),
15775 &stoplist, 1, &cmdlist);
15776 add_cmd ("in", class_breakpoint, stopin_command,
15777 _("Break in function or address."), &stoplist);
15778 add_cmd ("at", class_breakpoint, stopat_command,
15779 _("Break at a line in the current file."), &stoplist);
15780 add_com ("status", class_info, info_breakpoints_command, _("\
15781 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15782 The \"Type\" column indicates one of:\n\
15783 \tbreakpoint - normal breakpoint\n\
15784 \twatchpoint - watchpoint\n\
15785 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15786 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15787 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15788 address and file/line number respectively.\n\
15789 \n\
15790 Convenience variable \"$_\" and default examine address for \"x\"\n\
15791 are set to the address of the last breakpoint listed unless the command\n\
15792 is prefixed with \"server \".\n\n\
15793 Convenience variable \"$bpnum\" contains the number of the last\n\
15794 breakpoint set."));
15795 }
15796
15797 cmd_list_element *info_breakpoints_cmd
15798 = add_info ("breakpoints", info_breakpoints_command, _("\
15799 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15800 The \"Type\" column indicates one of:\n\
15801 \tbreakpoint - normal breakpoint\n\
15802 \twatchpoint - watchpoint\n\
15803 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15804 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15805 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15806 address and file/line number respectively.\n\
15807 \n\
15808 Convenience variable \"$_\" and default examine address for \"x\"\n\
15809 are set to the address of the last breakpoint listed unless the command\n\
15810 is prefixed with \"server \".\n\n\
15811 Convenience variable \"$bpnum\" contains the number of the last\n\
15812 breakpoint set."));
15813
15814 add_info_alias ("b", info_breakpoints_cmd, 1);
15815
15816 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15817 Status of all breakpoints, or breakpoint number NUMBER.\n\
15818 The \"Type\" column indicates one of:\n\
15819 \tbreakpoint - normal breakpoint\n\
15820 \twatchpoint - watchpoint\n\
15821 \tlongjmp - internal breakpoint used to step through longjmp()\n\
15822 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15823 \tuntil - internal breakpoint used by the \"until\" command\n\
15824 \tfinish - internal breakpoint used by the \"finish\" command\n\
15825 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15826 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15827 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15828 address and file/line number respectively.\n\
15829 \n\
15830 Convenience variable \"$_\" and default examine address for \"x\"\n\
15831 are set to the address of the last breakpoint listed unless the command\n\
15832 is prefixed with \"server \".\n\n\
15833 Convenience variable \"$bpnum\" contains the number of the last\n\
15834 breakpoint set."),
15835 &maintenanceinfolist);
15836
15837 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
15838 Set catchpoints to catch events."),
15839 &catch_cmdlist,
15840 0/*allow-unknown*/, &cmdlist);
15841
15842 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
15843 Set temporary catchpoints to catch events."),
15844 &tcatch_cmdlist,
15845 0/*allow-unknown*/, &cmdlist);
15846
15847 add_catch_command ("fork", _("Catch calls to fork."),
15848 catch_fork_command_1,
15849 NULL,
15850 (void *) (uintptr_t) catch_fork_permanent,
15851 (void *) (uintptr_t) catch_fork_temporary);
15852 add_catch_command ("vfork", _("Catch calls to vfork."),
15853 catch_fork_command_1,
15854 NULL,
15855 (void *) (uintptr_t) catch_vfork_permanent,
15856 (void *) (uintptr_t) catch_vfork_temporary);
15857 add_catch_command ("exec", _("Catch calls to exec."),
15858 catch_exec_command_1,
15859 NULL,
15860 CATCH_PERMANENT,
15861 CATCH_TEMPORARY);
15862 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15863 Usage: catch load [REGEX]\n\
15864 If REGEX is given, only stop for libraries matching the regular expression."),
15865 catch_load_command_1,
15866 NULL,
15867 CATCH_PERMANENT,
15868 CATCH_TEMPORARY);
15869 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15870 Usage: catch unload [REGEX]\n\
15871 If REGEX is given, only stop for libraries matching the regular expression."),
15872 catch_unload_command_1,
15873 NULL,
15874 CATCH_PERMANENT,
15875 CATCH_TEMPORARY);
15876
15877 const auto opts = make_watch_options_def_group (nullptr);
15878
15879 static const std::string watch_help = gdb::option::build_help (_("\
15880 Set a watchpoint for EXPRESSION.\n\
15881 Usage: watch [-location] EXPRESSION\n\
15882 \n\
15883 Options:\n\
15884 %OPTIONS%\n\
15885 \n\
15886 A watchpoint stops execution of your program whenever the value of\n\
15887 an expression changes."), opts);
15888 c = add_com ("watch", class_breakpoint, watch_command,
15889 watch_help.c_str ());
15890 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15891
15892 static const std::string rwatch_help = gdb::option::build_help (_("\
15893 Set a read watchpoint for EXPRESSION.\n\
15894 Usage: rwatch [-location] EXPRESSION\n\
15895 \n\
15896 Options:\n\
15897 %OPTIONS%\n\
15898 \n\
15899 A read watchpoint stops execution of your program whenever the value of\n\
15900 an expression is read."), opts);
15901 c = add_com ("rwatch", class_breakpoint, rwatch_command,
15902 rwatch_help.c_str ());
15903 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15904
15905 static const std::string awatch_help = gdb::option::build_help (_("\
15906 Set an access watchpoint for EXPRESSION.\n\
15907 Usage: awatch [-location] EXPRESSION\n\
15908 \n\
15909 Options:\n\
15910 %OPTIONS%\n\
15911 \n\
15912 An access watchpoint stops execution of your program whenever the value\n\
15913 of an expression is either read or written."), opts);
15914 c = add_com ("awatch", class_breakpoint, awatch_command,
15915 awatch_help.c_str ());
15916 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15917
15918 add_info ("watchpoints", info_watchpoints_command, _("\
15919 Status of specified watchpoints (all watchpoints if no argument)."));
15920
15921 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15922 respond to changes - contrary to the description. */
15923 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15924 &can_use_hw_watchpoints, _("\
15925 Set debugger's willingness to use watchpoint hardware."), _("\
15926 Show debugger's willingness to use watchpoint hardware."), _("\
15927 If zero, gdb will not use hardware for new watchpoints, even if\n\
15928 such is available. (However, any hardware watchpoints that were\n\
15929 created before setting this to nonzero, will continue to use watchpoint\n\
15930 hardware.)"),
15931 NULL,
15932 show_can_use_hw_watchpoints,
15933 &setlist, &showlist);
15934
15935 can_use_hw_watchpoints = 1;
15936
15937 /* Tracepoint manipulation commands. */
15938
15939 cmd_list_element *trace_cmd
15940 = add_com ("trace", class_breakpoint, trace_command, _("\
15941 Set a tracepoint at specified location.\n\
15942 \n"
15943 BREAK_ARGS_HELP ("trace") "\n\
15944 Do \"help tracepoints\" for info on other tracepoint commands."));
15945 set_cmd_completer (trace_cmd, location_completer);
15946
15947 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
15948 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
15949 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
15950 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
15951
15952 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
15953 Set a fast tracepoint at specified location.\n\
15954 \n"
15955 BREAK_ARGS_HELP ("ftrace") "\n\
15956 Do \"help tracepoints\" for info on other tracepoint commands."));
15957 set_cmd_completer (c, location_completer);
15958
15959 c = add_com ("strace", class_breakpoint, strace_command, _("\
15960 Set a static tracepoint at location or marker.\n\
15961 \n\
15962 strace [LOCATION] [if CONDITION]\n\
15963 LOCATION may be a linespec, explicit, or address location (described below) \n\
15964 or -m MARKER_ID.\n\n\
15965 If a marker id is specified, probe the marker with that name. With\n\
15966 no LOCATION, uses current execution address of the selected stack frame.\n\
15967 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15968 This collects arbitrary user data passed in the probe point call to the\n\
15969 tracing library. You can inspect it when analyzing the trace buffer,\n\
15970 by printing the $_sdata variable like any other convenience variable.\n\
15971 \n\
15972 CONDITION is a boolean expression.\n\
15973 \n" LOCATION_HELP_STRING "\n\n\
15974 Multiple tracepoints at one place are permitted, and useful if their\n\
15975 conditions are different.\n\
15976 \n\
15977 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15978 Do \"help tracepoints\" for info on other tracepoint commands."));
15979 set_cmd_completer (c, location_completer);
15980
15981 cmd_list_element *info_tracepoints_cmd
15982 = add_info ("tracepoints", info_tracepoints_command, _("\
15983 Status of specified tracepoints (all tracepoints if no argument).\n\
15984 Convenience variable \"$tpnum\" contains the number of the\n\
15985 last tracepoint set."));
15986
15987 add_info_alias ("tp", info_tracepoints_cmd, 1);
15988
15989 cmd_list_element *delete_tracepoints_cmd
15990 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15991 Delete specified tracepoints.\n\
15992 Arguments are tracepoint numbers, separated by spaces.\n\
15993 No argument means delete all tracepoints."),
15994 &deletelist);
15995 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
15996
15997 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15998 Disable specified tracepoints.\n\
15999 Arguments are tracepoint numbers, separated by spaces.\n\
16000 No argument means disable all tracepoints."),
16001 &disablelist);
16002 deprecate_cmd (c, "disable");
16003
16004 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16005 Enable specified tracepoints.\n\
16006 Arguments are tracepoint numbers, separated by spaces.\n\
16007 No argument means enable all tracepoints."),
16008 &enablelist);
16009 deprecate_cmd (c, "enable");
16010
16011 add_com ("passcount", class_trace, trace_pass_command, _("\
16012 Set the passcount for a tracepoint.\n\
16013 The trace will end when the tracepoint has been passed 'count' times.\n\
16014 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16015 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16016
16017 add_basic_prefix_cmd ("save", class_breakpoint,
16018 _("Save breakpoint definitions as a script."),
16019 &save_cmdlist,
16020 0/*allow-unknown*/, &cmdlist);
16021
16022 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16023 Save current breakpoint definitions as a script.\n\
16024 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16025 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16026 session to restore them."),
16027 &save_cmdlist);
16028 set_cmd_completer (c, filename_completer);
16029
16030 cmd_list_element *save_tracepoints_cmd
16031 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16032 Save current tracepoint definitions as a script.\n\
16033 Use the 'source' command in another debug session to restore them."),
16034 &save_cmdlist);
16035 set_cmd_completer (save_tracepoints_cmd, filename_completer);
16036
16037 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
16038 deprecate_cmd (c, "save tracepoints");
16039
16040 add_basic_prefix_cmd ("breakpoint", class_maintenance, _("\
16041 Breakpoint specific settings.\n\
16042 Configure various breakpoint-specific variables such as\n\
16043 pending breakpoint behavior."),
16044 &breakpoint_set_cmdlist,
16045 0/*allow-unknown*/, &setlist);
16046 add_show_prefix_cmd ("breakpoint", class_maintenance, _("\
16047 Breakpoint specific settings.\n\
16048 Configure various breakpoint-specific variables such as\n\
16049 pending breakpoint behavior."),
16050 &breakpoint_show_cmdlist,
16051 0/*allow-unknown*/, &showlist);
16052
16053 add_setshow_auto_boolean_cmd ("pending", no_class,
16054 &pending_break_support, _("\
16055 Set debugger's behavior regarding pending breakpoints."), _("\
16056 Show debugger's behavior regarding pending breakpoints."), _("\
16057 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16058 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16059 an error. If auto, an unrecognized breakpoint location results in a\n\
16060 user-query to see if a pending breakpoint should be created."),
16061 NULL,
16062 show_pending_break_support,
16063 &breakpoint_set_cmdlist,
16064 &breakpoint_show_cmdlist);
16065
16066 pending_break_support = AUTO_BOOLEAN_AUTO;
16067
16068 add_setshow_boolean_cmd ("auto-hw", no_class,
16069 &automatic_hardware_breakpoints, _("\
16070 Set automatic usage of hardware breakpoints."), _("\
16071 Show automatic usage of hardware breakpoints."), _("\
16072 If set, the debugger will automatically use hardware breakpoints for\n\
16073 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16074 a warning will be emitted for such breakpoints."),
16075 NULL,
16076 show_automatic_hardware_breakpoints,
16077 &breakpoint_set_cmdlist,
16078 &breakpoint_show_cmdlist);
16079
16080 add_setshow_boolean_cmd ("always-inserted", class_support,
16081 &always_inserted_mode, _("\
16082 Set mode for inserting breakpoints."), _("\
16083 Show mode for inserting breakpoints."), _("\
16084 When this mode is on, breakpoints are inserted immediately as soon as\n\
16085 they're created, kept inserted even when execution stops, and removed\n\
16086 only when the user deletes them. When this mode is off (the default),\n\
16087 breakpoints are inserted only when execution continues, and removed\n\
16088 when execution stops."),
16089 NULL,
16090 &show_always_inserted_mode,
16091 &breakpoint_set_cmdlist,
16092 &breakpoint_show_cmdlist);
16093
16094 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16095 condition_evaluation_enums,
16096 &condition_evaluation_mode_1, _("\
16097 Set mode of breakpoint condition evaluation."), _("\
16098 Show mode of breakpoint condition evaluation."), _("\
16099 When this is set to \"host\", breakpoint conditions will be\n\
16100 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16101 breakpoint conditions will be downloaded to the target (if the target\n\
16102 supports such feature) and conditions will be evaluated on the target's side.\n\
16103 If this is set to \"auto\" (default), this will be automatically set to\n\
16104 \"target\" if it supports condition evaluation, otherwise it will\n\
16105 be set to \"host\"."),
16106 &set_condition_evaluation_mode,
16107 &show_condition_evaluation_mode,
16108 &breakpoint_set_cmdlist,
16109 &breakpoint_show_cmdlist);
16110
16111 add_com ("break-range", class_breakpoint, break_range_command, _("\
16112 Set a breakpoint for an address range.\n\
16113 break-range START-LOCATION, END-LOCATION\n\
16114 where START-LOCATION and END-LOCATION can be one of the following:\n\
16115 LINENUM, for that line in the current file,\n\
16116 FILE:LINENUM, for that line in that file,\n\
16117 +OFFSET, for that number of lines after the current line\n\
16118 or the start of the range\n\
16119 FUNCTION, for the first line in that function,\n\
16120 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16121 *ADDRESS, for the instruction at that address.\n\
16122 \n\
16123 The breakpoint will stop execution of the inferior whenever it executes\n\
16124 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16125 range (including START-LOCATION and END-LOCATION)."));
16126
16127 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16128 Set a dynamic printf at specified location.\n\
16129 dprintf location,format string,arg1,arg2,...\n\
16130 location may be a linespec, explicit, or address location.\n"
16131 "\n" LOCATION_HELP_STRING));
16132 set_cmd_completer (c, location_completer);
16133
16134 add_setshow_enum_cmd ("dprintf-style", class_support,
16135 dprintf_style_enums, &dprintf_style, _("\
16136 Set the style of usage for dynamic printf."), _("\
16137 Show the style of usage for dynamic printf."), _("\
16138 This setting chooses how GDB will do a dynamic printf.\n\
16139 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16140 console, as with the \"printf\" command.\n\
16141 If the value is \"call\", the print is done by calling a function in your\n\
16142 program; by default printf(), but you can choose a different function or\n\
16143 output stream by setting dprintf-function and dprintf-channel."),
16144 update_dprintf_commands, NULL,
16145 &setlist, &showlist);
16146
16147 dprintf_function = xstrdup ("printf");
16148 add_setshow_string_cmd ("dprintf-function", class_support,
16149 &dprintf_function, _("\
16150 Set the function to use for dynamic printf."), _("\
16151 Show the function to use for dynamic printf."), NULL,
16152 update_dprintf_commands, NULL,
16153 &setlist, &showlist);
16154
16155 dprintf_channel = xstrdup ("");
16156 add_setshow_string_cmd ("dprintf-channel", class_support,
16157 &dprintf_channel, _("\
16158 Set the channel to use for dynamic printf."), _("\
16159 Show the channel to use for dynamic printf."), NULL,
16160 update_dprintf_commands, NULL,
16161 &setlist, &showlist);
16162
16163 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16164 &disconnected_dprintf, _("\
16165 Set whether dprintf continues after GDB disconnects."), _("\
16166 Show whether dprintf continues after GDB disconnects."), _("\
16167 Use this to let dprintf commands continue to hit and produce output\n\
16168 even if GDB disconnects or detaches from the target."),
16169 NULL,
16170 NULL,
16171 &setlist, &showlist);
16172
16173 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16174 Target agent only formatted printing, like the C \"printf\" function.\n\
16175 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
16176 This supports most C printf format specifications, like %s, %d, etc.\n\
16177 This is useful for formatted output in user-defined commands."));
16178
16179 automatic_hardware_breakpoints = true;
16180
16181 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
16182 "breakpoint");
16183 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
16184 "breakpoint");
16185 }
This page took 0.508164 seconds and 4 git commands to generate.