]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/breakpoint.c
gdb: make bp_locations an std::vector
[thirdparty/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.data (); \
500 BP_TMP < bp_locations.data () + bp_locations.size () && (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.data () + bp_locations.size () \
514 && (*BP_LOCP_TMP)->address == ADDRESS); \
515 BP_LOCP_TMP++)
516
517 /* Chains of all breakpoints defined. */
518
519 static struct breakpoint *breakpoint_chain;
520
521 /* Breakpoint linked list range. */
522
523 using breakpoint_range = next_adapter<breakpoint, breakpoint_iterator>;
524
525 /* Return a range to iterate over all breakpoints. */
526
527 static breakpoint_range
528 all_breakpoints ()
529 {
530 return breakpoint_range (breakpoint_chain);
531 }
532
533 /* Breakpoint linked list range, safe against deletion of the current
534 breakpoint while iterating. */
535
536 using breakpoint_safe_range = basic_safe_range<breakpoint_range>;
537
538 /* Return a range to iterate over all breakpoints. This range is safe against
539 deletion of the current breakpoint while iterating. */
540
541 static breakpoint_safe_range
542 all_breakpoints_safe ()
543 {
544 return breakpoint_safe_range (all_breakpoints ());
545 }
546
547 /* See breakpoint.h. */
548
549 tracepoint_range
550 all_tracepoints ()
551 {
552 return tracepoint_range (breakpoint_chain);
553 }
554
555 /* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
556
557 static std::vector<bp_location *> bp_locations;
558
559 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
560 ADDRESS for the current elements of BP_LOCATIONS which get a valid
561 result from bp_location_has_shadow. You can use it for roughly
562 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
563 an address you need to read. */
564
565 static CORE_ADDR bp_locations_placed_address_before_address_max;
566
567 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
568 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
569 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
570 You can use it for roughly limiting the subrange of BP_LOCATIONS to
571 scan for shadow bytes for an address you need to read. */
572
573 static CORE_ADDR bp_locations_shadow_len_after_address_max;
574
575 /* The locations that no longer correspond to any breakpoint, unlinked
576 from the bp_locations array, but for which a hit may still be
577 reported by a target. */
578 static std::vector<bp_location *> moribund_locations;
579
580 /* Number of last breakpoint made. */
581
582 static int breakpoint_count;
583
584 /* The value of `breakpoint_count' before the last command that
585 created breakpoints. If the last (break-like) command created more
586 than one breakpoint, then the difference between BREAKPOINT_COUNT
587 and PREV_BREAKPOINT_COUNT is more than one. */
588 static int prev_breakpoint_count;
589
590 /* Number of last tracepoint made. */
591
592 static int tracepoint_count;
593
594 static struct cmd_list_element *breakpoint_set_cmdlist;
595 static struct cmd_list_element *breakpoint_show_cmdlist;
596 struct cmd_list_element *save_cmdlist;
597
598 /* See declaration at breakpoint.h. */
599
600 struct breakpoint *
601 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
602 void *user_data)
603 {
604 for (breakpoint *b : all_breakpoints ())
605 if (func (b, user_data) != 0)
606 return b;
607
608 return nullptr;
609 }
610
611 /* Return whether a breakpoint is an active enabled breakpoint. */
612 static int
613 breakpoint_enabled (struct breakpoint *b)
614 {
615 return (b->enable_state == bp_enabled);
616 }
617
618 /* Set breakpoint count to NUM. */
619
620 static void
621 set_breakpoint_count (int num)
622 {
623 prev_breakpoint_count = breakpoint_count;
624 breakpoint_count = num;
625 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
626 }
627
628 /* Used by `start_rbreak_breakpoints' below, to record the current
629 breakpoint count before "rbreak" creates any breakpoint. */
630 static int rbreak_start_breakpoint_count;
631
632 /* Called at the start an "rbreak" command to record the first
633 breakpoint made. */
634
635 scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
636 {
637 rbreak_start_breakpoint_count = breakpoint_count;
638 }
639
640 /* Called at the end of an "rbreak" command to record the last
641 breakpoint made. */
642
643 scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
644 {
645 prev_breakpoint_count = rbreak_start_breakpoint_count;
646 }
647
648 /* Used in run_command to zero the hit count when a new run starts. */
649
650 void
651 clear_breakpoint_hit_counts (void)
652 {
653 for (breakpoint *b : all_breakpoints ())
654 b->hit_count = 0;
655 }
656
657 \f
658 /* Return the breakpoint with the specified number, or NULL
659 if the number does not refer to an existing breakpoint. */
660
661 struct breakpoint *
662 get_breakpoint (int num)
663 {
664 for (breakpoint *b : all_breakpoints ())
665 if (b->number == num)
666 return b;
667
668 return nullptr;
669 }
670
671 \f
672
673 /* Mark locations as "conditions have changed" in case the target supports
674 evaluating conditions on its side. */
675
676 static void
677 mark_breakpoint_modified (struct breakpoint *b)
678 {
679 /* This is only meaningful if the target is
680 evaluating conditions and if the user has
681 opted for condition evaluation on the target's
682 side. */
683 if (gdb_evaluates_breakpoint_condition_p ()
684 || !target_supports_evaluation_of_breakpoint_conditions ())
685 return;
686
687 if (!is_breakpoint (b))
688 return;
689
690 for (bp_location *loc : b->locations ())
691 loc->condition_changed = condition_modified;
692 }
693
694 /* Mark location as "conditions have changed" in case the target supports
695 evaluating conditions on its side. */
696
697 static void
698 mark_breakpoint_location_modified (struct bp_location *loc)
699 {
700 /* This is only meaningful if the target is
701 evaluating conditions and if the user has
702 opted for condition evaluation on the target's
703 side. */
704 if (gdb_evaluates_breakpoint_condition_p ()
705 || !target_supports_evaluation_of_breakpoint_conditions ())
706
707 return;
708
709 if (!is_breakpoint (loc->owner))
710 return;
711
712 loc->condition_changed = condition_modified;
713 }
714
715 /* Sets the condition-evaluation mode using the static global
716 condition_evaluation_mode. */
717
718 static void
719 set_condition_evaluation_mode (const char *args, int from_tty,
720 struct cmd_list_element *c)
721 {
722 const char *old_mode, *new_mode;
723
724 if ((condition_evaluation_mode_1 == condition_evaluation_target)
725 && !target_supports_evaluation_of_breakpoint_conditions ())
726 {
727 condition_evaluation_mode_1 = condition_evaluation_mode;
728 warning (_("Target does not support breakpoint condition evaluation.\n"
729 "Using host evaluation mode instead."));
730 return;
731 }
732
733 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
734 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
735
736 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
737 settings was "auto". */
738 condition_evaluation_mode = condition_evaluation_mode_1;
739
740 /* Only update the mode if the user picked a different one. */
741 if (new_mode != old_mode)
742 {
743 struct bp_location *loc, **loc_tmp;
744 /* If the user switched to a different evaluation mode, we
745 need to synch the changes with the target as follows:
746
747 "host" -> "target": Send all (valid) conditions to the target.
748 "target" -> "host": Remove all the conditions from the target.
749 */
750
751 if (new_mode == condition_evaluation_target)
752 {
753 /* Mark everything modified and synch conditions with the
754 target. */
755 ALL_BP_LOCATIONS (loc, loc_tmp)
756 mark_breakpoint_location_modified (loc);
757 }
758 else
759 {
760 /* Manually mark non-duplicate locations to synch conditions
761 with the target. We do this to remove all the conditions the
762 target knows about. */
763 ALL_BP_LOCATIONS (loc, loc_tmp)
764 if (is_breakpoint (loc->owner) && loc->inserted)
765 loc->needs_update = 1;
766 }
767
768 /* Do the update. */
769 update_global_location_list (UGLL_MAY_INSERT);
770 }
771
772 return;
773 }
774
775 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
776 what "auto" is translating to. */
777
778 static void
779 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
780 struct cmd_list_element *c, const char *value)
781 {
782 if (condition_evaluation_mode == condition_evaluation_auto)
783 fprintf_filtered (file,
784 _("Breakpoint condition evaluation "
785 "mode is %s (currently %s).\n"),
786 value,
787 breakpoint_condition_evaluation_mode ());
788 else
789 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
790 value);
791 }
792
793 /* A comparison function for bp_location AP and BP that is used by
794 bsearch. This comparison function only cares about addresses, unlike
795 the more general bp_location_is_less_than function. */
796
797 static int
798 bp_locations_compare_addrs (const void *ap, const void *bp)
799 {
800 const struct bp_location *a = *(const struct bp_location **) ap;
801 const struct bp_location *b = *(const struct bp_location **) bp;
802
803 if (a->address == b->address)
804 return 0;
805 else
806 return ((a->address > b->address) - (a->address < b->address));
807 }
808
809 /* Helper function to skip all bp_locations with addresses
810 less than ADDRESS. It returns the first bp_location that
811 is greater than or equal to ADDRESS. If none is found, just
812 return NULL. */
813
814 static struct bp_location **
815 get_first_locp_gte_addr (CORE_ADDR address)
816 {
817 struct bp_location dummy_loc;
818 struct bp_location *dummy_locp = &dummy_loc;
819 struct bp_location **locp_found = NULL;
820
821 /* Initialize the dummy location's address field. */
822 dummy_loc.address = address;
823
824 /* Find a close match to the first location at ADDRESS. */
825 locp_found = ((struct bp_location **)
826 bsearch (&dummy_locp, bp_locations.data (), bp_locations.size (),
827 sizeof (struct bp_location **),
828 bp_locations_compare_addrs));
829
830 /* Nothing was found, nothing left to do. */
831 if (locp_found == NULL)
832 return NULL;
833
834 /* We may have found a location that is at ADDRESS but is not the first in the
835 location's list. Go backwards (if possible) and locate the first one. */
836 while ((locp_found - 1) >= bp_locations.data ()
837 && (*(locp_found - 1))->address == address)
838 locp_found--;
839
840 return locp_found;
841 }
842
843 /* Parse COND_STRING in the context of LOC and set as the condition
844 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
845 the number of LOC within its owner. In case of parsing error, mark
846 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
847
848 static void
849 set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
850 int bp_num, int loc_num)
851 {
852 bool has_junk = false;
853 try
854 {
855 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
856 block_for_pc (loc->address), 0);
857 if (*cond_string != 0)
858 has_junk = true;
859 else
860 {
861 loc->cond = std::move (new_exp);
862 if (loc->disabled_by_cond && loc->enabled)
863 printf_filtered (_("Breakpoint %d's condition is now valid at "
864 "location %d, enabling.\n"),
865 bp_num, loc_num);
866
867 loc->disabled_by_cond = false;
868 }
869 }
870 catch (const gdb_exception_error &e)
871 {
872 if (loc->enabled)
873 {
874 /* Warn if a user-enabled location is now becoming disabled-by-cond.
875 BP_NUM is 0 if the breakpoint is being defined for the first
876 time using the "break ... if ..." command, and non-zero if
877 already defined. */
878 if (bp_num != 0)
879 warning (_("failed to validate condition at location %d.%d, "
880 "disabling:\n %s"), bp_num, loc_num, e.what ());
881 else
882 warning (_("failed to validate condition at location %d, "
883 "disabling:\n %s"), loc_num, e.what ());
884 }
885
886 loc->disabled_by_cond = true;
887 }
888
889 if (has_junk)
890 error (_("Garbage '%s' follows condition"), cond_string);
891 }
892
893 void
894 set_breakpoint_condition (struct breakpoint *b, const char *exp,
895 int from_tty, bool force)
896 {
897 if (*exp == 0)
898 {
899 xfree (b->cond_string);
900 b->cond_string = nullptr;
901
902 if (is_watchpoint (b))
903 static_cast<watchpoint *> (b)->cond_exp.reset ();
904 else
905 {
906 int loc_num = 1;
907 for (bp_location *loc : b->locations ())
908 {
909 loc->cond.reset ();
910 if (loc->disabled_by_cond && loc->enabled)
911 printf_filtered (_("Breakpoint %d's condition is now valid at "
912 "location %d, enabling.\n"),
913 b->number, loc_num);
914 loc->disabled_by_cond = false;
915 loc_num++;
916
917 /* No need to free the condition agent expression
918 bytecode (if we have one). We will handle this
919 when we go through update_global_location_list. */
920 }
921 }
922
923 if (from_tty)
924 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
925 }
926 else
927 {
928 if (is_watchpoint (b))
929 {
930 innermost_block_tracker tracker;
931 const char *arg = exp;
932 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
933 if (*arg != 0)
934 error (_("Junk at end of expression"));
935 watchpoint *w = static_cast<watchpoint *> (b);
936 w->cond_exp = std::move (new_exp);
937 w->cond_exp_valid_block = tracker.block ();
938 }
939 else
940 {
941 /* Parse and set condition expressions. We make two passes.
942 In the first, we parse the condition string to see if it
943 is valid in at least one location. If so, the condition
944 would be accepted. So we go ahead and set the locations'
945 conditions. In case no valid case is found, we throw
946 the error and the condition string will be rejected.
947 This two-pass approach is taken to avoid setting the
948 state of locations in case of a reject. */
949 for (bp_location *loc : b->locations ())
950 {
951 try
952 {
953 const char *arg = exp;
954 parse_exp_1 (&arg, loc->address,
955 block_for_pc (loc->address), 0);
956 if (*arg != 0)
957 error (_("Junk at end of expression"));
958 break;
959 }
960 catch (const gdb_exception_error &e)
961 {
962 /* Condition string is invalid. If this happens to
963 be the last loc, abandon (if not forced) or continue
964 (if forced). */
965 if (loc->next == nullptr && !force)
966 throw;
967 }
968 }
969
970 /* If we reach here, the condition is valid at some locations. */
971 int loc_num = 1;
972 for (bp_location *loc : b->locations ())
973 {
974 set_breakpoint_location_condition (exp, loc, b->number, loc_num);
975 loc_num++;
976 }
977 }
978
979 /* We know that the new condition parsed successfully. The
980 condition string of the breakpoint can be safely updated. */
981 xfree (b->cond_string);
982 b->cond_string = xstrdup (exp);
983 b->condition_not_parsed = 0;
984 }
985 mark_breakpoint_modified (b);
986
987 gdb::observers::breakpoint_modified.notify (b);
988 }
989
990 /* See breakpoint.h. */
991
992 void
993 set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
994 bool force)
995 {
996 for (breakpoint *b : all_breakpoints ())
997 if (b->number == bpnum)
998 {
999 /* Check if this breakpoint has a "stop" method implemented in an
1000 extension language. This method and conditions entered into GDB
1001 from the CLI are mutually exclusive. */
1002 const struct extension_language_defn *extlang
1003 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1004
1005 if (extlang != NULL)
1006 {
1007 error (_("Only one stop condition allowed. There is currently"
1008 " a %s stop condition defined for this breakpoint."),
1009 ext_lang_capitalized_name (extlang));
1010 }
1011 set_breakpoint_condition (b, exp, from_tty, force);
1012
1013 if (is_breakpoint (b))
1014 update_global_location_list (UGLL_MAY_INSERT);
1015
1016 return;
1017 }
1018
1019 error (_("No breakpoint number %d."), bpnum);
1020 }
1021
1022 /* The options for the "condition" command. */
1023
1024 struct condition_command_opts
1025 {
1026 /* For "-force". */
1027 bool force_condition = false;
1028 };
1029
1030 static const gdb::option::option_def condition_command_option_defs[] = {
1031
1032 gdb::option::flag_option_def<condition_command_opts> {
1033 "force",
1034 [] (condition_command_opts *opts) { return &opts->force_condition; },
1035 N_("Set the condition even if it is invalid for all current locations."),
1036 },
1037
1038 };
1039
1040 /* Create an option_def_group for the "condition" options, with
1041 CC_OPTS as context. */
1042
1043 static inline gdb::option::option_def_group
1044 make_condition_command_options_def_group (condition_command_opts *cc_opts)
1045 {
1046 return {{condition_command_option_defs}, cc_opts};
1047 }
1048
1049 /* Completion for the "condition" command. */
1050
1051 static void
1052 condition_completer (struct cmd_list_element *cmd,
1053 completion_tracker &tracker,
1054 const char *text, const char * /*word*/)
1055 {
1056 bool has_no_arguments = (*text == '\0');
1057 condition_command_opts cc_opts;
1058 const auto group = make_condition_command_options_def_group (&cc_opts);
1059 if (gdb::option::complete_options
1060 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1061 return;
1062
1063 text = skip_spaces (text);
1064 const char *space = skip_to_space (text);
1065 if (*space == '\0')
1066 {
1067 int len;
1068
1069 if (text[0] == '$')
1070 {
1071 tracker.advance_custom_word_point_by (1);
1072 /* We don't support completion of history indices. */
1073 if (!isdigit (text[1]))
1074 complete_internalvar (tracker, &text[1]);
1075 return;
1076 }
1077
1078 /* Suggest the "-force" flag if no arguments are given. If
1079 arguments were passed, they either already include the flag,
1080 or we are beyond the point of suggesting it because it's
1081 positionally the first argument. */
1082 if (has_no_arguments)
1083 gdb::option::complete_on_all_options (tracker, group);
1084
1085 /* We're completing the breakpoint number. */
1086 len = strlen (text);
1087
1088 for (breakpoint *b : all_breakpoints ())
1089 {
1090 char number[50];
1091
1092 xsnprintf (number, sizeof (number), "%d", b->number);
1093
1094 if (strncmp (number, text, len) == 0)
1095 tracker.add_completion (make_unique_xstrdup (number));
1096 }
1097
1098 return;
1099 }
1100
1101 /* We're completing the expression part. Skip the breakpoint num. */
1102 const char *exp_start = skip_spaces (space);
1103 tracker.advance_custom_word_point_by (exp_start - text);
1104 text = exp_start;
1105 const char *word = advance_to_expression_complete_word_point (tracker, text);
1106 expression_completer (cmd, tracker, text, word);
1107 }
1108
1109 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1110
1111 static void
1112 condition_command (const char *arg, int from_tty)
1113 {
1114 const char *p;
1115 int bnum;
1116
1117 if (arg == 0)
1118 error_no_arg (_("breakpoint number"));
1119
1120 p = arg;
1121
1122 /* Check if the "-force" flag was passed. */
1123 condition_command_opts cc_opts;
1124 const auto group = make_condition_command_options_def_group (&cc_opts);
1125 gdb::option::process_options
1126 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
1127
1128 bnum = get_number (&p);
1129 if (bnum == 0)
1130 error (_("Bad breakpoint argument: '%s'"), arg);
1131
1132 set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
1133 }
1134
1135 /* Check that COMMAND do not contain commands that are suitable
1136 only for tracepoints and not suitable for ordinary breakpoints.
1137 Throw if any such commands is found. */
1138
1139 static void
1140 check_no_tracepoint_commands (struct command_line *commands)
1141 {
1142 struct command_line *c;
1143
1144 for (c = commands; c; c = c->next)
1145 {
1146 if (c->control_type == while_stepping_control)
1147 error (_("The 'while-stepping' command can "
1148 "only be used for tracepoints"));
1149
1150 check_no_tracepoint_commands (c->body_list_0.get ());
1151 check_no_tracepoint_commands (c->body_list_1.get ());
1152
1153 /* Not that command parsing removes leading whitespace and comment
1154 lines and also empty lines. So, we only need to check for
1155 command directly. */
1156 if (strstr (c->line, "collect ") == c->line)
1157 error (_("The 'collect' command can only be used for tracepoints"));
1158
1159 if (strstr (c->line, "teval ") == c->line)
1160 error (_("The 'teval' command can only be used for tracepoints"));
1161 }
1162 }
1163
1164 struct longjmp_breakpoint : public breakpoint
1165 {
1166 ~longjmp_breakpoint () override;
1167 };
1168
1169 /* Encapsulate tests for different types of tracepoints. */
1170
1171 static bool
1172 is_tracepoint_type (bptype type)
1173 {
1174 return (type == bp_tracepoint
1175 || type == bp_fast_tracepoint
1176 || type == bp_static_tracepoint);
1177 }
1178
1179 static bool
1180 is_longjmp_type (bptype type)
1181 {
1182 return type == bp_longjmp || type == bp_exception;
1183 }
1184
1185 /* See breakpoint.h. */
1186
1187 bool
1188 is_tracepoint (const struct breakpoint *b)
1189 {
1190 return is_tracepoint_type (b->type);
1191 }
1192
1193 /* Factory function to create an appropriate instance of breakpoint given
1194 TYPE. */
1195
1196 static std::unique_ptr<breakpoint>
1197 new_breakpoint_from_type (bptype type)
1198 {
1199 breakpoint *b;
1200
1201 if (is_tracepoint_type (type))
1202 b = new tracepoint ();
1203 else if (is_longjmp_type (type))
1204 b = new longjmp_breakpoint ();
1205 else
1206 b = new breakpoint ();
1207
1208 return std::unique_ptr<breakpoint> (b);
1209 }
1210
1211 /* A helper function that validates that COMMANDS are valid for a
1212 breakpoint. This function will throw an exception if a problem is
1213 found. */
1214
1215 static void
1216 validate_commands_for_breakpoint (struct breakpoint *b,
1217 struct command_line *commands)
1218 {
1219 if (is_tracepoint (b))
1220 {
1221 struct tracepoint *t = (struct tracepoint *) b;
1222 struct command_line *c;
1223 struct command_line *while_stepping = 0;
1224
1225 /* Reset the while-stepping step count. The previous commands
1226 might have included a while-stepping action, while the new
1227 ones might not. */
1228 t->step_count = 0;
1229
1230 /* We need to verify that each top-level element of commands is
1231 valid for tracepoints, that there's at most one
1232 while-stepping element, and that the while-stepping's body
1233 has valid tracing commands excluding nested while-stepping.
1234 We also need to validate the tracepoint action line in the
1235 context of the tracepoint --- validate_actionline actually
1236 has side effects, like setting the tracepoint's
1237 while-stepping STEP_COUNT, in addition to checking if the
1238 collect/teval actions parse and make sense in the
1239 tracepoint's context. */
1240 for (c = commands; c; c = c->next)
1241 {
1242 if (c->control_type == while_stepping_control)
1243 {
1244 if (b->type == bp_fast_tracepoint)
1245 error (_("The 'while-stepping' command "
1246 "cannot be used for fast tracepoint"));
1247 else if (b->type == bp_static_tracepoint)
1248 error (_("The 'while-stepping' command "
1249 "cannot be used for static tracepoint"));
1250
1251 if (while_stepping)
1252 error (_("The 'while-stepping' command "
1253 "can be used only once"));
1254 else
1255 while_stepping = c;
1256 }
1257
1258 validate_actionline (c->line, b);
1259 }
1260 if (while_stepping)
1261 {
1262 struct command_line *c2;
1263
1264 gdb_assert (while_stepping->body_list_1 == nullptr);
1265 c2 = while_stepping->body_list_0.get ();
1266 for (; c2; c2 = c2->next)
1267 {
1268 if (c2->control_type == while_stepping_control)
1269 error (_("The 'while-stepping' command cannot be nested"));
1270 }
1271 }
1272 }
1273 else
1274 {
1275 check_no_tracepoint_commands (commands);
1276 }
1277 }
1278
1279 /* Return a vector of all the static tracepoints set at ADDR. The
1280 caller is responsible for releasing the vector. */
1281
1282 std::vector<breakpoint *>
1283 static_tracepoints_here (CORE_ADDR addr)
1284 {
1285 std::vector<breakpoint *> found;
1286
1287 for (breakpoint *b : all_breakpoints ())
1288 if (b->type == bp_static_tracepoint)
1289 {
1290 for (bp_location *loc : b->locations ())
1291 if (loc->address == addr)
1292 found.push_back (b);
1293 }
1294
1295 return found;
1296 }
1297
1298 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1299 validate that only allowed commands are included. */
1300
1301 void
1302 breakpoint_set_commands (struct breakpoint *b,
1303 counted_command_line &&commands)
1304 {
1305 validate_commands_for_breakpoint (b, commands.get ());
1306
1307 b->commands = std::move (commands);
1308 gdb::observers::breakpoint_modified.notify (b);
1309 }
1310
1311 /* Set the internal `silent' flag on the breakpoint. Note that this
1312 is not the same as the "silent" that may appear in the breakpoint's
1313 commands. */
1314
1315 void
1316 breakpoint_set_silent (struct breakpoint *b, int silent)
1317 {
1318 int old_silent = b->silent;
1319
1320 b->silent = silent;
1321 if (old_silent != silent)
1322 gdb::observers::breakpoint_modified.notify (b);
1323 }
1324
1325 /* Set the thread for this breakpoint. If THREAD is -1, make the
1326 breakpoint work for any thread. */
1327
1328 void
1329 breakpoint_set_thread (struct breakpoint *b, int thread)
1330 {
1331 int old_thread = b->thread;
1332
1333 b->thread = thread;
1334 if (old_thread != thread)
1335 gdb::observers::breakpoint_modified.notify (b);
1336 }
1337
1338 /* Set the task for this breakpoint. If TASK is 0, make the
1339 breakpoint work for any task. */
1340
1341 void
1342 breakpoint_set_task (struct breakpoint *b, int task)
1343 {
1344 int old_task = b->task;
1345
1346 b->task = task;
1347 if (old_task != task)
1348 gdb::observers::breakpoint_modified.notify (b);
1349 }
1350
1351 static void
1352 commands_command_1 (const char *arg, int from_tty,
1353 struct command_line *control)
1354 {
1355 counted_command_line cmd;
1356 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1357 NULL after the call to read_command_lines if the user provides an empty
1358 list of command by just typing "end". */
1359 bool cmd_read = false;
1360
1361 std::string new_arg;
1362
1363 if (arg == NULL || !*arg)
1364 {
1365 /* Argument not explicitly given. Synthesize it. */
1366 if (breakpoint_count - prev_breakpoint_count > 1)
1367 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1368 breakpoint_count);
1369 else if (breakpoint_count > 0)
1370 new_arg = string_printf ("%d", breakpoint_count);
1371 }
1372 else
1373 {
1374 /* Create a copy of ARG. This is needed because the "commands"
1375 command may be coming from a script. In that case, the read
1376 line buffer is going to be overwritten in the lambda of
1377 'map_breakpoint_numbers' below when reading the next line
1378 before we are are done parsing the breakpoint numbers. */
1379 new_arg = arg;
1380 }
1381 arg = new_arg.c_str ();
1382
1383 map_breakpoint_numbers
1384 (arg, [&] (breakpoint *b)
1385 {
1386 if (!cmd_read)
1387 {
1388 gdb_assert (cmd == NULL);
1389 if (control != NULL)
1390 cmd = control->body_list_0;
1391 else
1392 {
1393 std::string str
1394 = string_printf (_("Type commands for breakpoint(s) "
1395 "%s, one per line."),
1396 arg);
1397
1398 auto do_validate = [=] (const char *line)
1399 {
1400 validate_actionline (line, b);
1401 };
1402 gdb::function_view<void (const char *)> validator;
1403 if (is_tracepoint (b))
1404 validator = do_validate;
1405
1406 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1407 }
1408 cmd_read = true;
1409 }
1410
1411 /* If a breakpoint was on the list more than once, we don't need to
1412 do anything. */
1413 if (b->commands != cmd)
1414 {
1415 validate_commands_for_breakpoint (b, cmd.get ());
1416 b->commands = cmd;
1417 gdb::observers::breakpoint_modified.notify (b);
1418 }
1419 });
1420 }
1421
1422 static void
1423 commands_command (const char *arg, int from_tty)
1424 {
1425 commands_command_1 (arg, from_tty, NULL);
1426 }
1427
1428 /* Like commands_command, but instead of reading the commands from
1429 input stream, takes them from an already parsed command structure.
1430
1431 This is used by cli-script.c to DTRT with breakpoint commands
1432 that are part of if and while bodies. */
1433 enum command_control_type
1434 commands_from_control_command (const char *arg, struct command_line *cmd)
1435 {
1436 commands_command_1 (arg, 0, cmd);
1437 return simple_control;
1438 }
1439
1440 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1441
1442 static int
1443 bp_location_has_shadow (struct bp_location *bl)
1444 {
1445 if (bl->loc_type != bp_loc_software_breakpoint)
1446 return 0;
1447 if (!bl->inserted)
1448 return 0;
1449 if (bl->target_info.shadow_len == 0)
1450 /* BL isn't valid, or doesn't shadow memory. */
1451 return 0;
1452 return 1;
1453 }
1454
1455 /* Update BUF, which is LEN bytes read from the target address
1456 MEMADDR, by replacing a memory breakpoint with its shadowed
1457 contents.
1458
1459 If READBUF is not NULL, this buffer must not overlap with the of
1460 the breakpoint location's shadow_contents buffer. Otherwise, a
1461 failed assertion internal error will be raised. */
1462
1463 static void
1464 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1465 const gdb_byte *writebuf_org,
1466 ULONGEST memaddr, LONGEST len,
1467 struct bp_target_info *target_info,
1468 struct gdbarch *gdbarch)
1469 {
1470 /* Now do full processing of the found relevant range of elements. */
1471 CORE_ADDR bp_addr = 0;
1472 int bp_size = 0;
1473 int bptoffset = 0;
1474
1475 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1476 current_program_space->aspace, 0))
1477 {
1478 /* The breakpoint is inserted in a different address space. */
1479 return;
1480 }
1481
1482 /* Addresses and length of the part of the breakpoint that
1483 we need to copy. */
1484 bp_addr = target_info->placed_address;
1485 bp_size = target_info->shadow_len;
1486
1487 if (bp_addr + bp_size <= memaddr)
1488 {
1489 /* The breakpoint is entirely before the chunk of memory we are
1490 reading. */
1491 return;
1492 }
1493
1494 if (bp_addr >= memaddr + len)
1495 {
1496 /* The breakpoint is entirely after the chunk of memory we are
1497 reading. */
1498 return;
1499 }
1500
1501 /* Offset within shadow_contents. */
1502 if (bp_addr < memaddr)
1503 {
1504 /* Only copy the second part of the breakpoint. */
1505 bp_size -= memaddr - bp_addr;
1506 bptoffset = memaddr - bp_addr;
1507 bp_addr = memaddr;
1508 }
1509
1510 if (bp_addr + bp_size > memaddr + len)
1511 {
1512 /* Only copy the first part of the breakpoint. */
1513 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1514 }
1515
1516 if (readbuf != NULL)
1517 {
1518 /* Verify that the readbuf buffer does not overlap with the
1519 shadow_contents buffer. */
1520 gdb_assert (target_info->shadow_contents >= readbuf + len
1521 || readbuf >= (target_info->shadow_contents
1522 + target_info->shadow_len));
1523
1524 /* Update the read buffer with this inserted breakpoint's
1525 shadow. */
1526 memcpy (readbuf + bp_addr - memaddr,
1527 target_info->shadow_contents + bptoffset, bp_size);
1528 }
1529 else
1530 {
1531 const unsigned char *bp;
1532 CORE_ADDR addr = target_info->reqstd_address;
1533 int placed_size;
1534
1535 /* Update the shadow with what we want to write to memory. */
1536 memcpy (target_info->shadow_contents + bptoffset,
1537 writebuf_org + bp_addr - memaddr, bp_size);
1538
1539 /* Determine appropriate breakpoint contents and size for this
1540 address. */
1541 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1542
1543 /* Update the final write buffer with this inserted
1544 breakpoint's INSN. */
1545 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1546 }
1547 }
1548
1549 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1550 by replacing any memory breakpoints with their shadowed contents.
1551
1552 If READBUF is not NULL, this buffer must not overlap with any of
1553 the breakpoint location's shadow_contents buffers. Otherwise,
1554 a failed assertion internal error will be raised.
1555
1556 The range of shadowed area by each bp_location is:
1557 bl->address - bp_locations_placed_address_before_address_max
1558 up to bl->address + bp_locations_shadow_len_after_address_max
1559 The range we were requested to resolve shadows for is:
1560 memaddr ... memaddr + len
1561 Thus the safe cutoff boundaries for performance optimization are
1562 memaddr + len <= (bl->address
1563 - bp_locations_placed_address_before_address_max)
1564 and:
1565 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1566
1567 void
1568 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1569 const gdb_byte *writebuf_org,
1570 ULONGEST memaddr, LONGEST len)
1571 {
1572 /* Left boundary, right boundary and median element of our binary
1573 search. */
1574 unsigned bc_l, bc_r, bc;
1575
1576 /* Find BC_L which is a leftmost element which may affect BUF
1577 content. It is safe to report lower value but a failure to
1578 report higher one. */
1579
1580 bc_l = 0;
1581 bc_r = bp_locations.size ();
1582 while (bc_l + 1 < bc_r)
1583 {
1584 struct bp_location *bl;
1585
1586 bc = (bc_l + bc_r) / 2;
1587 bl = bp_locations[bc];
1588
1589 /* Check first BL->ADDRESS will not overflow due to the added
1590 constant. Then advance the left boundary only if we are sure
1591 the BC element can in no way affect the BUF content (MEMADDR
1592 to MEMADDR + LEN range).
1593
1594 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1595 offset so that we cannot miss a breakpoint with its shadow
1596 range tail still reaching MEMADDR. */
1597
1598 if ((bl->address + bp_locations_shadow_len_after_address_max
1599 >= bl->address)
1600 && (bl->address + bp_locations_shadow_len_after_address_max
1601 <= memaddr))
1602 bc_l = bc;
1603 else
1604 bc_r = bc;
1605 }
1606
1607 /* Due to the binary search above, we need to make sure we pick the
1608 first location that's at BC_L's address. E.g., if there are
1609 multiple locations at the same address, BC_L may end up pointing
1610 at a duplicate location, and miss the "master"/"inserted"
1611 location. Say, given locations L1, L2 and L3 at addresses A and
1612 B:
1613
1614 L1@A, L2@A, L3@B, ...
1615
1616 BC_L could end up pointing at location L2, while the "master"
1617 location could be L1. Since the `loc->inserted' flag is only set
1618 on "master" locations, we'd forget to restore the shadow of L1
1619 and L2. */
1620 while (bc_l > 0
1621 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1622 bc_l--;
1623
1624 /* Now do full processing of the found relevant range of elements. */
1625
1626 for (bc = bc_l; bc < bp_locations.size (); bc++)
1627 {
1628 struct bp_location *bl = bp_locations[bc];
1629
1630 /* bp_location array has BL->OWNER always non-NULL. */
1631 if (bl->owner->type == bp_none)
1632 warning (_("reading through apparently deleted breakpoint #%d?"),
1633 bl->owner->number);
1634
1635 /* Performance optimization: any further element can no longer affect BUF
1636 content. */
1637
1638 if (bl->address >= bp_locations_placed_address_before_address_max
1639 && memaddr + len <= (bl->address
1640 - bp_locations_placed_address_before_address_max))
1641 break;
1642
1643 if (!bp_location_has_shadow (bl))
1644 continue;
1645
1646 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1647 memaddr, len, &bl->target_info, bl->gdbarch);
1648 }
1649 }
1650
1651 /* See breakpoint.h. */
1652
1653 bool
1654 is_breakpoint (const struct breakpoint *bpt)
1655 {
1656 return (bpt->type == bp_breakpoint
1657 || bpt->type == bp_hardware_breakpoint
1658 || bpt->type == bp_dprintf);
1659 }
1660
1661 /* Return true if BPT is of any hardware watchpoint kind. */
1662
1663 static bool
1664 is_hardware_watchpoint (const struct breakpoint *bpt)
1665 {
1666 return (bpt->type == bp_hardware_watchpoint
1667 || bpt->type == bp_read_watchpoint
1668 || bpt->type == bp_access_watchpoint);
1669 }
1670
1671 /* See breakpoint.h. */
1672
1673 bool
1674 is_watchpoint (const struct breakpoint *bpt)
1675 {
1676 return (is_hardware_watchpoint (bpt)
1677 || bpt->type == bp_watchpoint);
1678 }
1679
1680 /* Returns true if the current thread and its running state are safe
1681 to evaluate or update watchpoint B. Watchpoints on local
1682 expressions need to be evaluated in the context of the thread that
1683 was current when the watchpoint was created, and, that thread needs
1684 to be stopped to be able to select the correct frame context.
1685 Watchpoints on global expressions can be evaluated on any thread,
1686 and in any state. It is presently left to the target allowing
1687 memory accesses when threads are running. */
1688
1689 static int
1690 watchpoint_in_thread_scope (struct watchpoint *b)
1691 {
1692 return (b->pspace == current_program_space
1693 && (b->watchpoint_thread == null_ptid
1694 || (inferior_ptid == b->watchpoint_thread
1695 && !inferior_thread ()->executing)));
1696 }
1697
1698 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1699 associated bp_watchpoint_scope breakpoint. */
1700
1701 static void
1702 watchpoint_del_at_next_stop (struct watchpoint *w)
1703 {
1704 if (w->related_breakpoint != w)
1705 {
1706 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1707 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1708 w->related_breakpoint->disposition = disp_del_at_next_stop;
1709 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1710 w->related_breakpoint = w;
1711 }
1712 w->disposition = disp_del_at_next_stop;
1713 }
1714
1715 /* Extract a bitfield value from value VAL using the bit parameters contained in
1716 watchpoint W. */
1717
1718 static struct value *
1719 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1720 {
1721 struct value *bit_val;
1722
1723 if (val == NULL)
1724 return NULL;
1725
1726 bit_val = allocate_value (value_type (val));
1727
1728 unpack_value_bitfield (bit_val,
1729 w->val_bitpos,
1730 w->val_bitsize,
1731 value_contents_for_printing (val),
1732 value_offset (val),
1733 val);
1734
1735 return bit_val;
1736 }
1737
1738 /* Allocate a dummy location and add it to B, which must be a software
1739 watchpoint. This is required because even if a software watchpoint
1740 is not watching any memory, bpstat_stop_status requires a location
1741 to be able to report stops. */
1742
1743 static void
1744 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1745 struct program_space *pspace)
1746 {
1747 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1748
1749 b->loc = allocate_bp_location (b);
1750 b->loc->pspace = pspace;
1751 b->loc->address = -1;
1752 b->loc->length = -1;
1753 }
1754
1755 /* Returns true if B is a software watchpoint that is not watching any
1756 memory (e.g., "watch $pc"). */
1757
1758 static bool
1759 is_no_memory_software_watchpoint (struct breakpoint *b)
1760 {
1761 return (b->type == bp_watchpoint
1762 && b->loc != NULL
1763 && b->loc->next == NULL
1764 && b->loc->address == -1
1765 && b->loc->length == -1);
1766 }
1767
1768 /* Assuming that B is a watchpoint:
1769 - Reparse watchpoint expression, if REPARSE is non-zero
1770 - Evaluate expression and store the result in B->val
1771 - Evaluate the condition if there is one, and store the result
1772 in b->loc->cond.
1773 - Update the list of values that must be watched in B->loc.
1774
1775 If the watchpoint disposition is disp_del_at_next_stop, then do
1776 nothing. If this is local watchpoint that is out of scope, delete
1777 it.
1778
1779 Even with `set breakpoint always-inserted on' the watchpoints are
1780 removed + inserted on each stop here. Normal breakpoints must
1781 never be removed because they might be missed by a running thread
1782 when debugging in non-stop mode. On the other hand, hardware
1783 watchpoints (is_hardware_watchpoint; processed here) are specific
1784 to each LWP since they are stored in each LWP's hardware debug
1785 registers. Therefore, such LWP must be stopped first in order to
1786 be able to modify its hardware watchpoints.
1787
1788 Hardware watchpoints must be reset exactly once after being
1789 presented to the user. It cannot be done sooner, because it would
1790 reset the data used to present the watchpoint hit to the user. And
1791 it must not be done later because it could display the same single
1792 watchpoint hit during multiple GDB stops. Note that the latter is
1793 relevant only to the hardware watchpoint types bp_read_watchpoint
1794 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1795 not user-visible - its hit is suppressed if the memory content has
1796 not changed.
1797
1798 The following constraints influence the location where we can reset
1799 hardware watchpoints:
1800
1801 * target_stopped_by_watchpoint and target_stopped_data_address are
1802 called several times when GDB stops.
1803
1804 [linux]
1805 * Multiple hardware watchpoints can be hit at the same time,
1806 causing GDB to stop. GDB only presents one hardware watchpoint
1807 hit at a time as the reason for stopping, and all the other hits
1808 are presented later, one after the other, each time the user
1809 requests the execution to be resumed. Execution is not resumed
1810 for the threads still having pending hit event stored in
1811 LWP_INFO->STATUS. While the watchpoint is already removed from
1812 the inferior on the first stop the thread hit event is kept being
1813 reported from its cached value by linux_nat_stopped_data_address
1814 until the real thread resume happens after the watchpoint gets
1815 presented and thus its LWP_INFO->STATUS gets reset.
1816
1817 Therefore the hardware watchpoint hit can get safely reset on the
1818 watchpoint removal from inferior. */
1819
1820 static void
1821 update_watchpoint (struct watchpoint *b, int reparse)
1822 {
1823 int within_current_scope;
1824 struct frame_id saved_frame_id;
1825 int frame_saved;
1826
1827 /* If this is a local watchpoint, we only want to check if the
1828 watchpoint frame is in scope if the current thread is the thread
1829 that was used to create the watchpoint. */
1830 if (!watchpoint_in_thread_scope (b))
1831 return;
1832
1833 if (b->disposition == disp_del_at_next_stop)
1834 return;
1835
1836 frame_saved = 0;
1837
1838 /* Determine if the watchpoint is within scope. */
1839 if (b->exp_valid_block == NULL)
1840 within_current_scope = 1;
1841 else
1842 {
1843 struct frame_info *fi = get_current_frame ();
1844 struct gdbarch *frame_arch = get_frame_arch (fi);
1845 CORE_ADDR frame_pc = get_frame_pc (fi);
1846
1847 /* If we're at a point where the stack has been destroyed
1848 (e.g. in a function epilogue), unwinding may not work
1849 properly. Do not attempt to recreate locations at this
1850 point. See similar comments in watchpoint_check. */
1851 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1852 return;
1853
1854 /* Save the current frame's ID so we can restore it after
1855 evaluating the watchpoint expression on its own frame. */
1856 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1857 took a frame parameter, so that we didn't have to change the
1858 selected frame. */
1859 frame_saved = 1;
1860 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1861
1862 fi = frame_find_by_id (b->watchpoint_frame);
1863 within_current_scope = (fi != NULL);
1864 if (within_current_scope)
1865 select_frame (fi);
1866 }
1867
1868 /* We don't free locations. They are stored in the bp_location array
1869 and update_global_location_list will eventually delete them and
1870 remove breakpoints if needed. */
1871 b->loc = NULL;
1872
1873 if (within_current_scope && reparse)
1874 {
1875 const char *s;
1876
1877 b->exp.reset ();
1878 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1879 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1880 /* If the meaning of expression itself changed, the old value is
1881 no longer relevant. We don't want to report a watchpoint hit
1882 to the user when the old value and the new value may actually
1883 be completely different objects. */
1884 b->val = NULL;
1885 b->val_valid = false;
1886
1887 /* Note that unlike with breakpoints, the watchpoint's condition
1888 expression is stored in the breakpoint object, not in the
1889 locations (re)created below. */
1890 if (b->cond_string != NULL)
1891 {
1892 b->cond_exp.reset ();
1893
1894 s = b->cond_string;
1895 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1896 }
1897 }
1898
1899 /* If we failed to parse the expression, for example because
1900 it refers to a global variable in a not-yet-loaded shared library,
1901 don't try to insert watchpoint. We don't automatically delete
1902 such watchpoint, though, since failure to parse expression
1903 is different from out-of-scope watchpoint. */
1904 if (!target_has_execution ())
1905 {
1906 /* Without execution, memory can't change. No use to try and
1907 set watchpoint locations. The watchpoint will be reset when
1908 the target gains execution, through breakpoint_re_set. */
1909 if (!can_use_hw_watchpoints)
1910 {
1911 if (b->ops->works_in_software_mode (b))
1912 b->type = bp_watchpoint;
1913 else
1914 error (_("Can't set read/access watchpoint when "
1915 "hardware watchpoints are disabled."));
1916 }
1917 }
1918 else if (within_current_scope && b->exp)
1919 {
1920 std::vector<value_ref_ptr> val_chain;
1921 struct value *v, *result;
1922 struct program_space *frame_pspace;
1923
1924 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
1925 &val_chain, false);
1926
1927 /* Avoid setting b->val if it's already set. The meaning of
1928 b->val is 'the last value' user saw, and we should update
1929 it only if we reported that last value to user. As it
1930 happens, the code that reports it updates b->val directly.
1931 We don't keep track of the memory value for masked
1932 watchpoints. */
1933 if (!b->val_valid && !is_masked_watchpoint (b))
1934 {
1935 if (b->val_bitsize != 0)
1936 v = extract_bitfield_from_watchpoint_value (b, v);
1937 b->val = release_value (v);
1938 b->val_valid = true;
1939 }
1940
1941 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1942
1943 /* Look at each value on the value chain. */
1944 gdb_assert (!val_chain.empty ());
1945 for (const value_ref_ptr &iter : val_chain)
1946 {
1947 v = iter.get ();
1948
1949 /* If it's a memory location, and GDB actually needed
1950 its contents to evaluate the expression, then we
1951 must watch it. If the first value returned is
1952 still lazy, that means an error occurred reading it;
1953 watch it anyway in case it becomes readable. */
1954 if (VALUE_LVAL (v) == lval_memory
1955 && (v == val_chain[0] || ! value_lazy (v)))
1956 {
1957 struct type *vtype = check_typedef (value_type (v));
1958
1959 /* We only watch structs and arrays if user asked
1960 for it explicitly, never if they just happen to
1961 appear in the middle of some value chain. */
1962 if (v == result
1963 || (vtype->code () != TYPE_CODE_STRUCT
1964 && vtype->code () != TYPE_CODE_ARRAY))
1965 {
1966 CORE_ADDR addr;
1967 enum target_hw_bp_type type;
1968 struct bp_location *loc, **tmp;
1969 int bitpos = 0, bitsize = 0;
1970
1971 if (value_bitsize (v) != 0)
1972 {
1973 /* Extract the bit parameters out from the bitfield
1974 sub-expression. */
1975 bitpos = value_bitpos (v);
1976 bitsize = value_bitsize (v);
1977 }
1978 else if (v == result && b->val_bitsize != 0)
1979 {
1980 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1981 lvalue whose bit parameters are saved in the fields
1982 VAL_BITPOS and VAL_BITSIZE. */
1983 bitpos = b->val_bitpos;
1984 bitsize = b->val_bitsize;
1985 }
1986
1987 addr = value_address (v);
1988 if (bitsize != 0)
1989 {
1990 /* Skip the bytes that don't contain the bitfield. */
1991 addr += bitpos / 8;
1992 }
1993
1994 type = hw_write;
1995 if (b->type == bp_read_watchpoint)
1996 type = hw_read;
1997 else if (b->type == bp_access_watchpoint)
1998 type = hw_access;
1999
2000 loc = allocate_bp_location (b);
2001 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
2002 ;
2003 *tmp = loc;
2004 loc->gdbarch = value_type (v)->arch ();
2005
2006 loc->pspace = frame_pspace;
2007 loc->address = address_significant (loc->gdbarch, addr);
2008
2009 if (bitsize != 0)
2010 {
2011 /* Just cover the bytes that make up the bitfield. */
2012 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2013 }
2014 else
2015 loc->length = TYPE_LENGTH (value_type (v));
2016
2017 loc->watchpoint_type = type;
2018 }
2019 }
2020 }
2021
2022 /* Change the type of breakpoint between hardware assisted or
2023 an ordinary watchpoint depending on the hardware support
2024 and free hardware slots. REPARSE is set when the inferior
2025 is started. */
2026 if (reparse)
2027 {
2028 int reg_cnt;
2029 enum bp_loc_type loc_type;
2030
2031 reg_cnt = can_use_hardware_watchpoint (val_chain);
2032
2033 if (reg_cnt)
2034 {
2035 int i, target_resources_ok, other_type_used;
2036 enum bptype type;
2037
2038 /* Use an exact watchpoint when there's only one memory region to be
2039 watched, and only one debug register is needed to watch it. */
2040 b->exact = target_exact_watchpoints && reg_cnt == 1;
2041
2042 /* We need to determine how many resources are already
2043 used for all other hardware watchpoints plus this one
2044 to see if we still have enough resources to also fit
2045 this watchpoint in as well. */
2046
2047 /* If this is a software watchpoint, we try to turn it
2048 to a hardware one -- count resources as if B was of
2049 hardware watchpoint type. */
2050 type = b->type;
2051 if (type == bp_watchpoint)
2052 type = bp_hardware_watchpoint;
2053
2054 /* This watchpoint may or may not have been placed on
2055 the list yet at this point (it won't be in the list
2056 if we're trying to create it for the first time,
2057 through watch_command), so always account for it
2058 manually. */
2059
2060 /* Count resources used by all watchpoints except B. */
2061 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
2062
2063 /* Add in the resources needed for B. */
2064 i += hw_watchpoint_use_count (b);
2065
2066 target_resources_ok
2067 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2068 if (target_resources_ok <= 0)
2069 {
2070 int sw_mode = b->ops->works_in_software_mode (b);
2071
2072 if (target_resources_ok == 0 && !sw_mode)
2073 error (_("Target does not support this type of "
2074 "hardware watchpoint."));
2075 else if (target_resources_ok < 0 && !sw_mode)
2076 error (_("There are not enough available hardware "
2077 "resources for this watchpoint."));
2078
2079 /* Downgrade to software watchpoint. */
2080 b->type = bp_watchpoint;
2081 }
2082 else
2083 {
2084 /* If this was a software watchpoint, we've just
2085 found we have enough resources to turn it to a
2086 hardware watchpoint. Otherwise, this is a
2087 nop. */
2088 b->type = type;
2089 }
2090 }
2091 else if (!b->ops->works_in_software_mode (b))
2092 {
2093 if (!can_use_hw_watchpoints)
2094 error (_("Can't set read/access watchpoint when "
2095 "hardware watchpoints are disabled."));
2096 else
2097 error (_("Expression cannot be implemented with "
2098 "read/access watchpoint."));
2099 }
2100 else
2101 b->type = bp_watchpoint;
2102
2103 loc_type = (b->type == bp_watchpoint? bp_loc_other
2104 : bp_loc_hardware_watchpoint);
2105 for (bp_location *bl : b->locations ())
2106 bl->loc_type = loc_type;
2107 }
2108
2109 /* If a software watchpoint is not watching any memory, then the
2110 above left it without any location set up. But,
2111 bpstat_stop_status requires a location to be able to report
2112 stops, so make sure there's at least a dummy one. */
2113 if (b->type == bp_watchpoint && b->loc == NULL)
2114 software_watchpoint_add_no_memory_location (b, frame_pspace);
2115 }
2116 else if (!within_current_scope)
2117 {
2118 printf_filtered (_("\
2119 Watchpoint %d deleted because the program has left the block\n\
2120 in which its expression is valid.\n"),
2121 b->number);
2122 watchpoint_del_at_next_stop (b);
2123 }
2124
2125 /* Restore the selected frame. */
2126 if (frame_saved)
2127 select_frame (frame_find_by_id (saved_frame_id));
2128 }
2129
2130
2131 /* Returns 1 iff breakpoint location should be
2132 inserted in the inferior. We don't differentiate the type of BL's owner
2133 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2134 breakpoint_ops is not defined, because in insert_bp_location,
2135 tracepoint's insert_location will not be called. */
2136 static int
2137 should_be_inserted (struct bp_location *bl)
2138 {
2139 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2140 return 0;
2141
2142 if (bl->owner->disposition == disp_del_at_next_stop)
2143 return 0;
2144
2145 if (!bl->enabled || bl->disabled_by_cond
2146 || bl->shlib_disabled || bl->duplicate)
2147 return 0;
2148
2149 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2150 return 0;
2151
2152 /* This is set for example, when we're attached to the parent of a
2153 vfork, and have detached from the child. The child is running
2154 free, and we expect it to do an exec or exit, at which point the
2155 OS makes the parent schedulable again (and the target reports
2156 that the vfork is done). Until the child is done with the shared
2157 memory region, do not insert breakpoints in the parent, otherwise
2158 the child could still trip on the parent's breakpoints. Since
2159 the parent is blocked anyway, it won't miss any breakpoint. */
2160 if (bl->pspace->breakpoints_not_allowed)
2161 return 0;
2162
2163 /* Don't insert a breakpoint if we're trying to step past its
2164 location, except if the breakpoint is a single-step breakpoint,
2165 and the breakpoint's thread is the thread which is stepping past
2166 a breakpoint. */
2167 if ((bl->loc_type == bp_loc_software_breakpoint
2168 || bl->loc_type == bp_loc_hardware_breakpoint)
2169 && stepping_past_instruction_at (bl->pspace->aspace,
2170 bl->address)
2171 /* The single-step breakpoint may be inserted at the location
2172 we're trying to step if the instruction branches to itself.
2173 However, the instruction won't be executed at all and it may
2174 break the semantics of the instruction, for example, the
2175 instruction is a conditional branch or updates some flags.
2176 We can't fix it unless GDB is able to emulate the instruction
2177 or switch to displaced stepping. */
2178 && !(bl->owner->type == bp_single_step
2179 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2180 {
2181 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2182 paddress (bl->gdbarch, bl->address));
2183 return 0;
2184 }
2185
2186 /* Don't insert watchpoints if we're trying to step past the
2187 instruction that triggered one. */
2188 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2189 && stepping_past_nonsteppable_watchpoint ())
2190 {
2191 infrun_debug_printf ("stepping past non-steppable watchpoint. "
2192 "skipping watchpoint at %s:%d",
2193 paddress (bl->gdbarch, bl->address), bl->length);
2194 return 0;
2195 }
2196
2197 return 1;
2198 }
2199
2200 /* Same as should_be_inserted but does the check assuming
2201 that the location is not duplicated. */
2202
2203 static int
2204 unduplicated_should_be_inserted (struct bp_location *bl)
2205 {
2206 int result;
2207 const int save_duplicate = bl->duplicate;
2208
2209 bl->duplicate = 0;
2210 result = should_be_inserted (bl);
2211 bl->duplicate = save_duplicate;
2212 return result;
2213 }
2214
2215 /* Parses a conditional described by an expression COND into an
2216 agent expression bytecode suitable for evaluation
2217 by the bytecode interpreter. Return NULL if there was
2218 any error during parsing. */
2219
2220 static agent_expr_up
2221 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2222 {
2223 if (cond == NULL)
2224 return NULL;
2225
2226 agent_expr_up aexpr;
2227
2228 /* We don't want to stop processing, so catch any errors
2229 that may show up. */
2230 try
2231 {
2232 aexpr = gen_eval_for_expr (scope, cond);
2233 }
2234
2235 catch (const gdb_exception_error &ex)
2236 {
2237 /* If we got here, it means the condition could not be parsed to a valid
2238 bytecode expression and thus can't be evaluated on the target's side.
2239 It's no use iterating through the conditions. */
2240 }
2241
2242 /* We have a valid agent expression. */
2243 return aexpr;
2244 }
2245
2246 /* Based on location BL, create a list of breakpoint conditions to be
2247 passed on to the target. If we have duplicated locations with different
2248 conditions, we will add such conditions to the list. The idea is that the
2249 target will evaluate the list of conditions and will only notify GDB when
2250 one of them is true. */
2251
2252 static void
2253 build_target_condition_list (struct bp_location *bl)
2254 {
2255 struct bp_location **locp = NULL, **loc2p;
2256 int null_condition_or_parse_error = 0;
2257 int modified = bl->needs_update;
2258 struct bp_location *loc;
2259
2260 /* Release conditions left over from a previous insert. */
2261 bl->target_info.conditions.clear ();
2262
2263 /* This is only meaningful if the target is
2264 evaluating conditions and if the user has
2265 opted for condition evaluation on the target's
2266 side. */
2267 if (gdb_evaluates_breakpoint_condition_p ()
2268 || !target_supports_evaluation_of_breakpoint_conditions ())
2269 return;
2270
2271 /* Do a first pass to check for locations with no assigned
2272 conditions or conditions that fail to parse to a valid agent
2273 expression bytecode. If any of these happen, then it's no use to
2274 send conditions to the target since this location will always
2275 trigger and generate a response back to GDB. Note we consider
2276 all locations at the same address irrespective of type, i.e.,
2277 even if the locations aren't considered duplicates (e.g.,
2278 software breakpoint and hardware breakpoint at the same
2279 address). */
2280 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2281 {
2282 loc = (*loc2p);
2283 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2284 {
2285 if (modified)
2286 {
2287 /* Re-parse the conditions since something changed. In that
2288 case we already freed the condition bytecodes (see
2289 force_breakpoint_reinsertion). We just
2290 need to parse the condition to bytecodes again. */
2291 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2292 loc->cond.get ());
2293 }
2294
2295 /* If we have a NULL bytecode expression, it means something
2296 went wrong or we have a null condition expression. */
2297 if (!loc->cond_bytecode)
2298 {
2299 null_condition_or_parse_error = 1;
2300 break;
2301 }
2302 }
2303 }
2304
2305 /* If any of these happened, it means we will have to evaluate the conditions
2306 for the location's address on gdb's side. It is no use keeping bytecodes
2307 for all the other duplicate locations, thus we free all of them here.
2308
2309 This is so we have a finer control over which locations' conditions are
2310 being evaluated by GDB or the remote stub. */
2311 if (null_condition_or_parse_error)
2312 {
2313 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2314 {
2315 loc = (*loc2p);
2316 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2317 {
2318 /* Only go as far as the first NULL bytecode is
2319 located. */
2320 if (!loc->cond_bytecode)
2321 return;
2322
2323 loc->cond_bytecode.reset ();
2324 }
2325 }
2326 }
2327
2328 /* No NULL conditions or failed bytecode generation. Build a
2329 condition list for this location's address. If we have software
2330 and hardware locations at the same address, they aren't
2331 considered duplicates, but we still marge all the conditions
2332 anyway, as it's simpler, and doesn't really make a practical
2333 difference. */
2334 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2335 {
2336 loc = (*loc2p);
2337 if (loc->cond
2338 && is_breakpoint (loc->owner)
2339 && loc->pspace->num == bl->pspace->num
2340 && loc->owner->enable_state == bp_enabled
2341 && loc->enabled
2342 && !loc->disabled_by_cond)
2343 {
2344 /* Add the condition to the vector. This will be used later
2345 to send the conditions to the target. */
2346 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2347 }
2348 }
2349
2350 return;
2351 }
2352
2353 /* Parses a command described by string CMD into an agent expression
2354 bytecode suitable for evaluation by the bytecode interpreter.
2355 Return NULL if there was any error during parsing. */
2356
2357 static agent_expr_up
2358 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2359 {
2360 const char *cmdrest;
2361 const char *format_start, *format_end;
2362 struct gdbarch *gdbarch = get_current_arch ();
2363
2364 if (cmd == NULL)
2365 return NULL;
2366
2367 cmdrest = cmd;
2368
2369 if (*cmdrest == ',')
2370 ++cmdrest;
2371 cmdrest = skip_spaces (cmdrest);
2372
2373 if (*cmdrest++ != '"')
2374 error (_("No format string following the location"));
2375
2376 format_start = cmdrest;
2377
2378 format_pieces fpieces (&cmdrest);
2379
2380 format_end = cmdrest;
2381
2382 if (*cmdrest++ != '"')
2383 error (_("Bad format string, non-terminated '\"'."));
2384
2385 cmdrest = skip_spaces (cmdrest);
2386
2387 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2388 error (_("Invalid argument syntax"));
2389
2390 if (*cmdrest == ',')
2391 cmdrest++;
2392 cmdrest = skip_spaces (cmdrest);
2393
2394 /* For each argument, make an expression. */
2395
2396 std::vector<struct expression *> argvec;
2397 while (*cmdrest != '\0')
2398 {
2399 const char *cmd1;
2400
2401 cmd1 = cmdrest;
2402 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2403 argvec.push_back (expr.release ());
2404 cmdrest = cmd1;
2405 if (*cmdrest == ',')
2406 ++cmdrest;
2407 }
2408
2409 agent_expr_up aexpr;
2410
2411 /* We don't want to stop processing, so catch any errors
2412 that may show up. */
2413 try
2414 {
2415 aexpr = gen_printf (scope, gdbarch, 0, 0,
2416 format_start, format_end - format_start,
2417 argvec.size (), argvec.data ());
2418 }
2419 catch (const gdb_exception_error &ex)
2420 {
2421 /* If we got here, it means the command could not be parsed to a valid
2422 bytecode expression and thus can't be evaluated on the target's side.
2423 It's no use iterating through the other commands. */
2424 }
2425
2426 /* We have a valid agent expression, return it. */
2427 return aexpr;
2428 }
2429
2430 /* Based on location BL, create a list of breakpoint commands to be
2431 passed on to the target. If we have duplicated locations with
2432 different commands, we will add any such to the list. */
2433
2434 static void
2435 build_target_command_list (struct bp_location *bl)
2436 {
2437 struct bp_location **locp = NULL, **loc2p;
2438 int null_command_or_parse_error = 0;
2439 int modified = bl->needs_update;
2440 struct bp_location *loc;
2441
2442 /* Clear commands left over from a previous insert. */
2443 bl->target_info.tcommands.clear ();
2444
2445 if (!target_can_run_breakpoint_commands ())
2446 return;
2447
2448 /* For now, limit to agent-style dprintf breakpoints. */
2449 if (dprintf_style != dprintf_style_agent)
2450 return;
2451
2452 /* For now, if we have any location at the same address that isn't a
2453 dprintf, don't install the target-side commands, as that would
2454 make the breakpoint not be reported to the core, and we'd lose
2455 control. */
2456 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2457 {
2458 loc = (*loc2p);
2459 if (is_breakpoint (loc->owner)
2460 && loc->pspace->num == bl->pspace->num
2461 && loc->owner->type != bp_dprintf)
2462 return;
2463 }
2464
2465 /* Do a first pass to check for locations with no assigned
2466 conditions or conditions that fail to parse to a valid agent expression
2467 bytecode. If any of these happen, then it's no use to send conditions
2468 to the target since this location will always trigger and generate a
2469 response back to GDB. */
2470 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2471 {
2472 loc = (*loc2p);
2473 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2474 {
2475 if (modified)
2476 {
2477 /* Re-parse the commands since something changed. In that
2478 case we already freed the command bytecodes (see
2479 force_breakpoint_reinsertion). We just
2480 need to parse the command to bytecodes again. */
2481 loc->cmd_bytecode
2482 = parse_cmd_to_aexpr (bl->address,
2483 loc->owner->extra_string);
2484 }
2485
2486 /* If we have a NULL bytecode expression, it means something
2487 went wrong or we have a null command expression. */
2488 if (!loc->cmd_bytecode)
2489 {
2490 null_command_or_parse_error = 1;
2491 break;
2492 }
2493 }
2494 }
2495
2496 /* If anything failed, then we're not doing target-side commands,
2497 and so clean up. */
2498 if (null_command_or_parse_error)
2499 {
2500 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2501 {
2502 loc = (*loc2p);
2503 if (is_breakpoint (loc->owner)
2504 && loc->pspace->num == bl->pspace->num)
2505 {
2506 /* Only go as far as the first NULL bytecode is
2507 located. */
2508 if (loc->cmd_bytecode == NULL)
2509 return;
2510
2511 loc->cmd_bytecode.reset ();
2512 }
2513 }
2514 }
2515
2516 /* No NULL commands or failed bytecode generation. Build a command
2517 list for all duplicate locations at this location's address.
2518 Note that here we must care for whether the breakpoint location
2519 types are considered duplicates, otherwise, say, if we have a
2520 software and hardware location at the same address, the target
2521 could end up running the commands twice. For the moment, we only
2522 support targets-side commands with dprintf, but it doesn't hurt
2523 to be pedantically correct in case that changes. */
2524 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2525 {
2526 loc = (*loc2p);
2527 if (breakpoint_locations_match (bl, loc)
2528 && loc->owner->extra_string
2529 && is_breakpoint (loc->owner)
2530 && loc->pspace->num == bl->pspace->num
2531 && loc->owner->enable_state == bp_enabled
2532 && loc->enabled
2533 && !loc->disabled_by_cond)
2534 {
2535 /* Add the command to the vector. This will be used later
2536 to send the commands to the target. */
2537 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2538 }
2539 }
2540
2541 bl->target_info.persist = 0;
2542 /* Maybe flag this location as persistent. */
2543 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2544 bl->target_info.persist = 1;
2545 }
2546
2547 /* Return the kind of breakpoint on address *ADDR. Get the kind
2548 of breakpoint according to ADDR except single-step breakpoint.
2549 Get the kind of single-step breakpoint according to the current
2550 registers state. */
2551
2552 static int
2553 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2554 {
2555 if (bl->owner->type == bp_single_step)
2556 {
2557 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2558 struct regcache *regcache;
2559
2560 regcache = get_thread_regcache (thr);
2561
2562 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2563 regcache, addr);
2564 }
2565 else
2566 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2567 }
2568
2569 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2570 location. Any error messages are printed to TMP_ERROR_STREAM; and
2571 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2572 Returns 0 for success, 1 if the bp_location type is not supported or
2573 -1 for failure.
2574
2575 NOTE drow/2003-09-09: This routine could be broken down to an
2576 object-style method for each breakpoint or catchpoint type. */
2577 static int
2578 insert_bp_location (struct bp_location *bl,
2579 struct ui_file *tmp_error_stream,
2580 int *disabled_breaks,
2581 int *hw_breakpoint_error,
2582 int *hw_bp_error_explained_already)
2583 {
2584 gdb_exception bp_excpt;
2585
2586 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2587 return 0;
2588
2589 /* Note we don't initialize bl->target_info, as that wipes out
2590 the breakpoint location's shadow_contents if the breakpoint
2591 is still inserted at that location. This in turn breaks
2592 target_read_memory which depends on these buffers when
2593 a memory read is requested at the breakpoint location:
2594 Once the target_info has been wiped, we fail to see that
2595 we have a breakpoint inserted at that address and thus
2596 read the breakpoint instead of returning the data saved in
2597 the breakpoint location's shadow contents. */
2598 bl->target_info.reqstd_address = bl->address;
2599 bl->target_info.placed_address_space = bl->pspace->aspace;
2600 bl->target_info.length = bl->length;
2601
2602 /* When working with target-side conditions, we must pass all the conditions
2603 for the same breakpoint address down to the target since GDB will not
2604 insert those locations. With a list of breakpoint conditions, the target
2605 can decide when to stop and notify GDB. */
2606
2607 if (is_breakpoint (bl->owner))
2608 {
2609 build_target_condition_list (bl);
2610 build_target_command_list (bl);
2611 /* Reset the modification marker. */
2612 bl->needs_update = 0;
2613 }
2614
2615 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2616 set at a read-only address, then a breakpoint location will have
2617 been changed to hardware breakpoint before we get here. If it is
2618 "off" however, error out before actually trying to insert the
2619 breakpoint, with a nicer error message. */
2620 if (bl->loc_type == bp_loc_software_breakpoint
2621 && !automatic_hardware_breakpoints)
2622 {
2623 mem_region *mr = lookup_mem_region (bl->address);
2624
2625 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2626 {
2627 fprintf_unfiltered (tmp_error_stream,
2628 _("Cannot insert breakpoint %d.\n"
2629 "Cannot set software breakpoint "
2630 "at read-only address %s\n"),
2631 bl->owner->number,
2632 paddress (bl->gdbarch, bl->address));
2633 return 1;
2634 }
2635 }
2636
2637 if (bl->loc_type == bp_loc_software_breakpoint
2638 || bl->loc_type == bp_loc_hardware_breakpoint)
2639 {
2640 /* First check to see if we have to handle an overlay. */
2641 if (overlay_debugging == ovly_off
2642 || bl->section == NULL
2643 || !(section_is_overlay (bl->section)))
2644 {
2645 /* No overlay handling: just set the breakpoint. */
2646 try
2647 {
2648 int val;
2649
2650 val = bl->owner->ops->insert_location (bl);
2651 if (val)
2652 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2653 }
2654 catch (gdb_exception &e)
2655 {
2656 bp_excpt = std::move (e);
2657 }
2658 }
2659 else
2660 {
2661 /* This breakpoint is in an overlay section.
2662 Shall we set a breakpoint at the LMA? */
2663 if (!overlay_events_enabled)
2664 {
2665 /* Yes -- overlay event support is not active,
2666 so we must try to set a breakpoint at the LMA.
2667 This will not work for a hardware breakpoint. */
2668 if (bl->loc_type == bp_loc_hardware_breakpoint)
2669 warning (_("hardware breakpoint %d not supported in overlay!"),
2670 bl->owner->number);
2671 else
2672 {
2673 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2674 bl->section);
2675 /* Set a software (trap) breakpoint at the LMA. */
2676 bl->overlay_target_info = bl->target_info;
2677 bl->overlay_target_info.reqstd_address = addr;
2678
2679 /* No overlay handling: just set the breakpoint. */
2680 try
2681 {
2682 int val;
2683
2684 bl->overlay_target_info.kind
2685 = breakpoint_kind (bl, &addr);
2686 bl->overlay_target_info.placed_address = addr;
2687 val = target_insert_breakpoint (bl->gdbarch,
2688 &bl->overlay_target_info);
2689 if (val)
2690 bp_excpt
2691 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2692 }
2693 catch (gdb_exception &e)
2694 {
2695 bp_excpt = std::move (e);
2696 }
2697
2698 if (bp_excpt.reason != 0)
2699 fprintf_unfiltered (tmp_error_stream,
2700 "Overlay breakpoint %d "
2701 "failed: in ROM?\n",
2702 bl->owner->number);
2703 }
2704 }
2705 /* Shall we set a breakpoint at the VMA? */
2706 if (section_is_mapped (bl->section))
2707 {
2708 /* Yes. This overlay section is mapped into memory. */
2709 try
2710 {
2711 int val;
2712
2713 val = bl->owner->ops->insert_location (bl);
2714 if (val)
2715 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2716 }
2717 catch (gdb_exception &e)
2718 {
2719 bp_excpt = std::move (e);
2720 }
2721 }
2722 else
2723 {
2724 /* No. This breakpoint will not be inserted.
2725 No error, but do not mark the bp as 'inserted'. */
2726 return 0;
2727 }
2728 }
2729
2730 if (bp_excpt.reason != 0)
2731 {
2732 /* Can't set the breakpoint. */
2733
2734 /* In some cases, we might not be able to insert a
2735 breakpoint in a shared library that has already been
2736 removed, but we have not yet processed the shlib unload
2737 event. Unfortunately, some targets that implement
2738 breakpoint insertion themselves can't tell why the
2739 breakpoint insertion failed (e.g., the remote target
2740 doesn't define error codes), so we must treat generic
2741 errors as memory errors. */
2742 if (bp_excpt.reason == RETURN_ERROR
2743 && (bp_excpt.error == GENERIC_ERROR
2744 || bp_excpt.error == MEMORY_ERROR)
2745 && bl->loc_type == bp_loc_software_breakpoint
2746 && (solib_name_from_address (bl->pspace, bl->address)
2747 || shared_objfile_contains_address_p (bl->pspace,
2748 bl->address)))
2749 {
2750 /* See also: disable_breakpoints_in_shlibs. */
2751 bl->shlib_disabled = 1;
2752 gdb::observers::breakpoint_modified.notify (bl->owner);
2753 if (!*disabled_breaks)
2754 {
2755 fprintf_unfiltered (tmp_error_stream,
2756 "Cannot insert breakpoint %d.\n",
2757 bl->owner->number);
2758 fprintf_unfiltered (tmp_error_stream,
2759 "Temporarily disabling shared "
2760 "library breakpoints:\n");
2761 }
2762 *disabled_breaks = 1;
2763 fprintf_unfiltered (tmp_error_stream,
2764 "breakpoint #%d\n", bl->owner->number);
2765 return 0;
2766 }
2767 else
2768 {
2769 if (bl->loc_type == bp_loc_hardware_breakpoint)
2770 {
2771 *hw_breakpoint_error = 1;
2772 *hw_bp_error_explained_already = bp_excpt.message != NULL;
2773 fprintf_unfiltered (tmp_error_stream,
2774 "Cannot insert hardware breakpoint %d%s",
2775 bl->owner->number,
2776 bp_excpt.message ? ":" : ".\n");
2777 if (bp_excpt.message != NULL)
2778 fprintf_unfiltered (tmp_error_stream, "%s.\n",
2779 bp_excpt.what ());
2780 }
2781 else
2782 {
2783 if (bp_excpt.message == NULL)
2784 {
2785 std::string message
2786 = memory_error_message (TARGET_XFER_E_IO,
2787 bl->gdbarch, bl->address);
2788
2789 fprintf_unfiltered (tmp_error_stream,
2790 "Cannot insert breakpoint %d.\n"
2791 "%s\n",
2792 bl->owner->number, message.c_str ());
2793 }
2794 else
2795 {
2796 fprintf_unfiltered (tmp_error_stream,
2797 "Cannot insert breakpoint %d: %s\n",
2798 bl->owner->number,
2799 bp_excpt.what ());
2800 }
2801 }
2802 return 1;
2803
2804 }
2805 }
2806 else
2807 bl->inserted = 1;
2808
2809 return 0;
2810 }
2811
2812 else if (bl->loc_type == bp_loc_hardware_watchpoint
2813 /* NOTE drow/2003-09-08: This state only exists for removing
2814 watchpoints. It's not clear that it's necessary... */
2815 && bl->owner->disposition != disp_del_at_next_stop)
2816 {
2817 int val;
2818
2819 gdb_assert (bl->owner->ops != NULL
2820 && bl->owner->ops->insert_location != NULL);
2821
2822 val = bl->owner->ops->insert_location (bl);
2823
2824 /* If trying to set a read-watchpoint, and it turns out it's not
2825 supported, try emulating one with an access watchpoint. */
2826 if (val == 1 && bl->watchpoint_type == hw_read)
2827 {
2828 struct bp_location *loc, **loc_temp;
2829
2830 /* But don't try to insert it, if there's already another
2831 hw_access location that would be considered a duplicate
2832 of this one. */
2833 ALL_BP_LOCATIONS (loc, loc_temp)
2834 if (loc != bl
2835 && loc->watchpoint_type == hw_access
2836 && watchpoint_locations_match (bl, loc))
2837 {
2838 bl->duplicate = 1;
2839 bl->inserted = 1;
2840 bl->target_info = loc->target_info;
2841 bl->watchpoint_type = hw_access;
2842 val = 0;
2843 break;
2844 }
2845
2846 if (val == 1)
2847 {
2848 bl->watchpoint_type = hw_access;
2849 val = bl->owner->ops->insert_location (bl);
2850
2851 if (val)
2852 /* Back to the original value. */
2853 bl->watchpoint_type = hw_read;
2854 }
2855 }
2856
2857 bl->inserted = (val == 0);
2858 }
2859
2860 else if (bl->owner->type == bp_catchpoint)
2861 {
2862 int val;
2863
2864 gdb_assert (bl->owner->ops != NULL
2865 && bl->owner->ops->insert_location != NULL);
2866
2867 val = bl->owner->ops->insert_location (bl);
2868 if (val)
2869 {
2870 bl->owner->enable_state = bp_disabled;
2871
2872 if (val == 1)
2873 warning (_("\
2874 Error inserting catchpoint %d: Your system does not support this type\n\
2875 of catchpoint."), bl->owner->number);
2876 else
2877 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2878 }
2879
2880 bl->inserted = (val == 0);
2881
2882 /* We've already printed an error message if there was a problem
2883 inserting this catchpoint, and we've disabled the catchpoint,
2884 so just return success. */
2885 return 0;
2886 }
2887
2888 return 0;
2889 }
2890
2891 /* This function is called when program space PSPACE is about to be
2892 deleted. It takes care of updating breakpoints to not reference
2893 PSPACE anymore. */
2894
2895 void
2896 breakpoint_program_space_exit (struct program_space *pspace)
2897 {
2898 struct bp_location *loc, **loc_temp;
2899
2900 /* Remove any breakpoint that was set through this program space. */
2901 for (breakpoint *b : all_breakpoints_safe ())
2902 if (b->pspace == pspace)
2903 delete_breakpoint (b);
2904
2905 /* Breakpoints set through other program spaces could have locations
2906 bound to PSPACE as well. Remove those. */
2907 ALL_BP_LOCATIONS (loc, loc_temp)
2908 {
2909 struct bp_location *tmp;
2910
2911 if (loc->pspace == pspace)
2912 {
2913 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2914 if (loc->owner->loc == loc)
2915 loc->owner->loc = loc->next;
2916 else
2917 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2918 if (tmp->next == loc)
2919 {
2920 tmp->next = loc->next;
2921 break;
2922 }
2923 }
2924 }
2925
2926 /* Now update the global location list to permanently delete the
2927 removed locations above. */
2928 update_global_location_list (UGLL_DONT_INSERT);
2929 }
2930
2931 /* Make sure all breakpoints are inserted in inferior.
2932 Throws exception on any error.
2933 A breakpoint that is already inserted won't be inserted
2934 again, so calling this function twice is safe. */
2935 void
2936 insert_breakpoints (void)
2937 {
2938 for (breakpoint *bpt : all_breakpoints ())
2939 if (is_hardware_watchpoint (bpt))
2940 {
2941 struct watchpoint *w = (struct watchpoint *) bpt;
2942
2943 update_watchpoint (w, 0 /* don't reparse. */);
2944 }
2945
2946 /* Updating watchpoints creates new locations, so update the global
2947 location list. Explicitly tell ugll to insert locations and
2948 ignore breakpoints_always_inserted_mode. Also,
2949 update_global_location_list tries to "upgrade" software
2950 breakpoints to hardware breakpoints to handle "set breakpoint
2951 auto-hw", so we need to call it even if we don't have new
2952 locations. */
2953 update_global_location_list (UGLL_INSERT);
2954 }
2955
2956 /* Invoke CALLBACK for each of bp_location. */
2957
2958 void
2959 iterate_over_bp_locations (gdb::function_view<void (bp_location *)> callback)
2960 {
2961 struct bp_location *loc, **loc_tmp;
2962
2963 ALL_BP_LOCATIONS (loc, loc_tmp)
2964 {
2965 callback (loc);
2966 }
2967 }
2968
2969 /* This is used when we need to synch breakpoint conditions between GDB and the
2970 target. It is the case with deleting and disabling of breakpoints when using
2971 always-inserted mode. */
2972
2973 static void
2974 update_inserted_breakpoint_locations (void)
2975 {
2976 struct bp_location *bl, **blp_tmp;
2977 int error_flag = 0;
2978 int val = 0;
2979 int disabled_breaks = 0;
2980 int hw_breakpoint_error = 0;
2981 int hw_bp_details_reported = 0;
2982
2983 string_file tmp_error_stream;
2984
2985 /* Explicitly mark the warning -- this will only be printed if
2986 there was an error. */
2987 tmp_error_stream.puts ("Warning:\n");
2988
2989 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2990
2991 ALL_BP_LOCATIONS (bl, blp_tmp)
2992 {
2993 /* We only want to update software breakpoints and hardware
2994 breakpoints. */
2995 if (!is_breakpoint (bl->owner))
2996 continue;
2997
2998 /* We only want to update locations that are already inserted
2999 and need updating. This is to avoid unwanted insertion during
3000 deletion of breakpoints. */
3001 if (!bl->inserted || !bl->needs_update)
3002 continue;
3003
3004 switch_to_program_space_and_thread (bl->pspace);
3005
3006 /* For targets that support global breakpoints, there's no need
3007 to select an inferior to insert breakpoint to. In fact, even
3008 if we aren't attached to any process yet, we should still
3009 insert breakpoints. */
3010 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3011 && (inferior_ptid == null_ptid || !target_has_execution ()))
3012 continue;
3013
3014 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3015 &hw_breakpoint_error, &hw_bp_details_reported);
3016 if (val)
3017 error_flag = val;
3018 }
3019
3020 if (error_flag)
3021 {
3022 target_terminal::ours_for_output ();
3023 error_stream (tmp_error_stream);
3024 }
3025 }
3026
3027 /* Used when starting or continuing the program. */
3028
3029 static void
3030 insert_breakpoint_locations (void)
3031 {
3032 struct bp_location *bl, **blp_tmp;
3033 int error_flag = 0;
3034 int val = 0;
3035 int disabled_breaks = 0;
3036 int hw_breakpoint_error = 0;
3037 int hw_bp_error_explained_already = 0;
3038
3039 string_file tmp_error_stream;
3040
3041 /* Explicitly mark the warning -- this will only be printed if
3042 there was an error. */
3043 tmp_error_stream.puts ("Warning:\n");
3044
3045 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3046
3047 ALL_BP_LOCATIONS (bl, blp_tmp)
3048 {
3049 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3050 continue;
3051
3052 /* There is no point inserting thread-specific breakpoints if
3053 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3054 has BL->OWNER always non-NULL. */
3055 if (bl->owner->thread != -1
3056 && !valid_global_thread_id (bl->owner->thread))
3057 continue;
3058
3059 switch_to_program_space_and_thread (bl->pspace);
3060
3061 /* For targets that support global breakpoints, there's no need
3062 to select an inferior to insert breakpoint to. In fact, even
3063 if we aren't attached to any process yet, we should still
3064 insert breakpoints. */
3065 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3066 && (inferior_ptid == null_ptid || !target_has_execution ()))
3067 continue;
3068
3069 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3070 &hw_breakpoint_error, &hw_bp_error_explained_already);
3071 if (val)
3072 error_flag = val;
3073 }
3074
3075 /* If we failed to insert all locations of a watchpoint, remove
3076 them, as half-inserted watchpoint is of limited use. */
3077 for (breakpoint *bpt : all_breakpoints ())
3078 {
3079 int some_failed = 0;
3080
3081 if (!is_hardware_watchpoint (bpt))
3082 continue;
3083
3084 if (!breakpoint_enabled (bpt))
3085 continue;
3086
3087 if (bpt->disposition == disp_del_at_next_stop)
3088 continue;
3089
3090 for (bp_location *loc : bpt->locations ())
3091 if (!loc->inserted && should_be_inserted (loc))
3092 {
3093 some_failed = 1;
3094 break;
3095 }
3096
3097 if (some_failed)
3098 {
3099 for (bp_location *loc : bpt->locations ())
3100 if (loc->inserted)
3101 remove_breakpoint (loc);
3102
3103 hw_breakpoint_error = 1;
3104 tmp_error_stream.printf ("Could not insert "
3105 "hardware watchpoint %d.\n",
3106 bpt->number);
3107 error_flag = -1;
3108 }
3109 }
3110
3111 if (error_flag)
3112 {
3113 /* If a hardware breakpoint or watchpoint was inserted, add a
3114 message about possibly exhausted resources. */
3115 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3116 {
3117 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3118 You may have requested too many hardware breakpoints/watchpoints.\n");
3119 }
3120 target_terminal::ours_for_output ();
3121 error_stream (tmp_error_stream);
3122 }
3123 }
3124
3125 /* Used when the program stops.
3126 Returns zero if successful, or non-zero if there was a problem
3127 removing a breakpoint location. */
3128
3129 int
3130 remove_breakpoints (void)
3131 {
3132 struct bp_location *bl, **blp_tmp;
3133 int val = 0;
3134
3135 ALL_BP_LOCATIONS (bl, blp_tmp)
3136 {
3137 if (bl->inserted && !is_tracepoint (bl->owner))
3138 val |= remove_breakpoint (bl);
3139 }
3140 return val;
3141 }
3142
3143 /* When a thread exits, remove breakpoints that are related to
3144 that thread. */
3145
3146 static void
3147 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3148 {
3149 for (breakpoint *b : all_breakpoints_safe ())
3150 {
3151 if (b->thread == tp->global_num && user_breakpoint_p (b))
3152 {
3153 b->disposition = disp_del_at_next_stop;
3154
3155 printf_filtered (_("\
3156 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3157 b->number, print_thread_id (tp));
3158
3159 /* Hide it from the user. */
3160 b->number = 0;
3161 }
3162 }
3163 }
3164
3165 /* See breakpoint.h. */
3166
3167 void
3168 remove_breakpoints_inf (inferior *inf)
3169 {
3170 struct bp_location *bl, **blp_tmp;
3171 int val;
3172
3173 ALL_BP_LOCATIONS (bl, blp_tmp)
3174 {
3175 if (bl->pspace != inf->pspace)
3176 continue;
3177
3178 if (bl->inserted && !bl->target_info.persist)
3179 {
3180 val = remove_breakpoint (bl);
3181 if (val != 0)
3182 return;
3183 }
3184 }
3185 }
3186
3187 static int internal_breakpoint_number = -1;
3188
3189 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3190 If INTERNAL is non-zero, the breakpoint number will be populated
3191 from internal_breakpoint_number and that variable decremented.
3192 Otherwise the breakpoint number will be populated from
3193 breakpoint_count and that value incremented. Internal breakpoints
3194 do not set the internal var bpnum. */
3195 static void
3196 set_breakpoint_number (int internal, struct breakpoint *b)
3197 {
3198 if (internal)
3199 b->number = internal_breakpoint_number--;
3200 else
3201 {
3202 set_breakpoint_count (breakpoint_count + 1);
3203 b->number = breakpoint_count;
3204 }
3205 }
3206
3207 static struct breakpoint *
3208 create_internal_breakpoint (struct gdbarch *gdbarch,
3209 CORE_ADDR address, enum bptype type,
3210 const struct breakpoint_ops *ops)
3211 {
3212 symtab_and_line sal;
3213 sal.pc = address;
3214 sal.section = find_pc_overlay (sal.pc);
3215 sal.pspace = current_program_space;
3216
3217 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
3218 b->number = internal_breakpoint_number--;
3219 b->disposition = disp_donttouch;
3220
3221 return b;
3222 }
3223
3224 static const char *const longjmp_names[] =
3225 {
3226 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3227 };
3228 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3229
3230 /* Per-objfile data private to breakpoint.c. */
3231 struct breakpoint_objfile_data
3232 {
3233 /* Minimal symbol for "_ovly_debug_event" (if any). */
3234 struct bound_minimal_symbol overlay_msym {};
3235
3236 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3237 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
3238
3239 /* True if we have looked for longjmp probes. */
3240 int longjmp_searched = 0;
3241
3242 /* SystemTap probe points for longjmp (if any). These are non-owning
3243 references. */
3244 std::vector<probe *> longjmp_probes;
3245
3246 /* Minimal symbol for "std::terminate()" (if any). */
3247 struct bound_minimal_symbol terminate_msym {};
3248
3249 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3250 struct bound_minimal_symbol exception_msym {};
3251
3252 /* True if we have looked for exception probes. */
3253 int exception_searched = 0;
3254
3255 /* SystemTap probe points for unwinding (if any). These are non-owning
3256 references. */
3257 std::vector<probe *> exception_probes;
3258 };
3259
3260 static const struct objfile_key<breakpoint_objfile_data>
3261 breakpoint_objfile_key;
3262
3263 /* Minimal symbol not found sentinel. */
3264 static struct minimal_symbol msym_not_found;
3265
3266 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3267
3268 static int
3269 msym_not_found_p (const struct minimal_symbol *msym)
3270 {
3271 return msym == &msym_not_found;
3272 }
3273
3274 /* Return per-objfile data needed by breakpoint.c.
3275 Allocate the data if necessary. */
3276
3277 static struct breakpoint_objfile_data *
3278 get_breakpoint_objfile_data (struct objfile *objfile)
3279 {
3280 struct breakpoint_objfile_data *bp_objfile_data;
3281
3282 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3283 if (bp_objfile_data == NULL)
3284 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3285 return bp_objfile_data;
3286 }
3287
3288 static void
3289 create_overlay_event_breakpoint (void)
3290 {
3291 const char *const func_name = "_ovly_debug_event";
3292
3293 for (objfile *objfile : current_program_space->objfiles ())
3294 {
3295 struct breakpoint *b;
3296 struct breakpoint_objfile_data *bp_objfile_data;
3297 CORE_ADDR addr;
3298 struct explicit_location explicit_loc;
3299
3300 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3301
3302 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3303 continue;
3304
3305 if (bp_objfile_data->overlay_msym.minsym == NULL)
3306 {
3307 struct bound_minimal_symbol m;
3308
3309 m = lookup_minimal_symbol_text (func_name, objfile);
3310 if (m.minsym == NULL)
3311 {
3312 /* Avoid future lookups in this objfile. */
3313 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3314 continue;
3315 }
3316 bp_objfile_data->overlay_msym = m;
3317 }
3318
3319 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3320 b = create_internal_breakpoint (objfile->arch (), addr,
3321 bp_overlay_event,
3322 &internal_breakpoint_ops);
3323 initialize_explicit_location (&explicit_loc);
3324 explicit_loc.function_name = ASTRDUP (func_name);
3325 b->location = new_explicit_location (&explicit_loc);
3326
3327 if (overlay_debugging == ovly_auto)
3328 {
3329 b->enable_state = bp_enabled;
3330 overlay_events_enabled = 1;
3331 }
3332 else
3333 {
3334 b->enable_state = bp_disabled;
3335 overlay_events_enabled = 0;
3336 }
3337 }
3338 }
3339
3340 /* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3341 true if a breakpoint was installed. */
3342
3343 static bool
3344 create_longjmp_master_breakpoint_probe (objfile *objfile)
3345 {
3346 struct gdbarch *gdbarch = objfile->arch ();
3347 struct breakpoint_objfile_data *bp_objfile_data
3348 = get_breakpoint_objfile_data (objfile);
3349
3350 if (!bp_objfile_data->longjmp_searched)
3351 {
3352 std::vector<probe *> ret
3353 = find_probes_in_objfile (objfile, "libc", "longjmp");
3354
3355 if (!ret.empty ())
3356 {
3357 /* We are only interested in checking one element. */
3358 probe *p = ret[0];
3359
3360 if (!p->can_evaluate_arguments ())
3361 {
3362 /* We cannot use the probe interface here,
3363 because it does not know how to evaluate
3364 arguments. */
3365 ret.clear ();
3366 }
3367 }
3368 bp_objfile_data->longjmp_probes = ret;
3369 bp_objfile_data->longjmp_searched = 1;
3370 }
3371
3372 if (bp_objfile_data->longjmp_probes.empty ())
3373 return false;
3374
3375 for (probe *p : bp_objfile_data->longjmp_probes)
3376 {
3377 struct breakpoint *b;
3378
3379 b = create_internal_breakpoint (gdbarch,
3380 p->get_relocated_address (objfile),
3381 bp_longjmp_master,
3382 &internal_breakpoint_ops);
3383 b->location = new_probe_location ("-probe-stap libc:longjmp");
3384 b->enable_state = bp_disabled;
3385 }
3386
3387 return true;
3388 }
3389
3390 /* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3391 Return true if at least one breakpoint was installed. */
3392
3393 static bool
3394 create_longjmp_master_breakpoint_names (objfile *objfile)
3395 {
3396 struct gdbarch *gdbarch = objfile->arch ();
3397 if (!gdbarch_get_longjmp_target_p (gdbarch))
3398 return false;
3399
3400 struct breakpoint_objfile_data *bp_objfile_data
3401 = get_breakpoint_objfile_data (objfile);
3402 unsigned int installed_bp = 0;
3403
3404 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3405 {
3406 struct breakpoint *b;
3407 const char *func_name;
3408 CORE_ADDR addr;
3409 struct explicit_location explicit_loc;
3410
3411 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3412 continue;
3413
3414 func_name = longjmp_names[i];
3415 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3416 {
3417 struct bound_minimal_symbol m;
3418
3419 m = lookup_minimal_symbol_text (func_name, objfile);
3420 if (m.minsym == NULL)
3421 {
3422 /* Prevent future lookups in this objfile. */
3423 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3424 continue;
3425 }
3426 bp_objfile_data->longjmp_msym[i] = m;
3427 }
3428
3429 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3430 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3431 &internal_breakpoint_ops);
3432 initialize_explicit_location (&explicit_loc);
3433 explicit_loc.function_name = ASTRDUP (func_name);
3434 b->location = new_explicit_location (&explicit_loc);
3435 b->enable_state = bp_disabled;
3436 installed_bp++;
3437 }
3438
3439 return installed_bp > 0;
3440 }
3441
3442 /* Create a master longjmp breakpoint. */
3443
3444 static void
3445 create_longjmp_master_breakpoint (void)
3446 {
3447 scoped_restore_current_program_space restore_pspace;
3448
3449 for (struct program_space *pspace : program_spaces)
3450 {
3451 set_current_program_space (pspace);
3452
3453 for (objfile *obj : current_program_space->objfiles ())
3454 {
3455 /* Skip separate debug object, it's handled in the loop below. */
3456 if (obj->separate_debug_objfile_backlink != nullptr)
3457 continue;
3458
3459 /* Try a probe kind breakpoint on main objfile. */
3460 if (create_longjmp_master_breakpoint_probe (obj))
3461 continue;
3462
3463 /* Try longjmp_names kind breakpoints on main and separate_debug
3464 objfiles. */
3465 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3466 if (create_longjmp_master_breakpoint_names (debug_objfile))
3467 break;
3468 }
3469 }
3470 }
3471
3472 /* Create a master std::terminate breakpoint. */
3473 static void
3474 create_std_terminate_master_breakpoint (void)
3475 {
3476 const char *const func_name = "std::terminate()";
3477
3478 scoped_restore_current_program_space restore_pspace;
3479
3480 for (struct program_space *pspace : program_spaces)
3481 {
3482 CORE_ADDR addr;
3483
3484 set_current_program_space (pspace);
3485
3486 for (objfile *objfile : current_program_space->objfiles ())
3487 {
3488 struct breakpoint *b;
3489 struct breakpoint_objfile_data *bp_objfile_data;
3490 struct explicit_location explicit_loc;
3491
3492 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3493
3494 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3495 continue;
3496
3497 if (bp_objfile_data->terminate_msym.minsym == NULL)
3498 {
3499 struct bound_minimal_symbol m;
3500
3501 m = lookup_minimal_symbol (func_name, NULL, objfile);
3502 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3503 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3504 {
3505 /* Prevent future lookups in this objfile. */
3506 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3507 continue;
3508 }
3509 bp_objfile_data->terminate_msym = m;
3510 }
3511
3512 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3513 b = create_internal_breakpoint (objfile->arch (), addr,
3514 bp_std_terminate_master,
3515 &internal_breakpoint_ops);
3516 initialize_explicit_location (&explicit_loc);
3517 explicit_loc.function_name = ASTRDUP (func_name);
3518 b->location = new_explicit_location (&explicit_loc);
3519 b->enable_state = bp_disabled;
3520 }
3521 }
3522 }
3523
3524 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3525 probe. Return true if a breakpoint was installed. */
3526
3527 static bool
3528 create_exception_master_breakpoint_probe (objfile *objfile)
3529 {
3530 struct breakpoint *b;
3531 struct gdbarch *gdbarch;
3532 struct breakpoint_objfile_data *bp_objfile_data;
3533
3534 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3535
3536 /* We prefer the SystemTap probe point if it exists. */
3537 if (!bp_objfile_data->exception_searched)
3538 {
3539 std::vector<probe *> ret
3540 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3541
3542 if (!ret.empty ())
3543 {
3544 /* We are only interested in checking one element. */
3545 probe *p = ret[0];
3546
3547 if (!p->can_evaluate_arguments ())
3548 {
3549 /* We cannot use the probe interface here, because it does
3550 not know how to evaluate arguments. */
3551 ret.clear ();
3552 }
3553 }
3554 bp_objfile_data->exception_probes = ret;
3555 bp_objfile_data->exception_searched = 1;
3556 }
3557
3558 if (bp_objfile_data->exception_probes.empty ())
3559 return false;
3560
3561 gdbarch = objfile->arch ();
3562
3563 for (probe *p : bp_objfile_data->exception_probes)
3564 {
3565 b = create_internal_breakpoint (gdbarch,
3566 p->get_relocated_address (objfile),
3567 bp_exception_master,
3568 &internal_breakpoint_ops);
3569 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3570 b->enable_state = bp_disabled;
3571 }
3572
3573 return true;
3574 }
3575
3576 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3577 _Unwind_DebugHook. Return true if a breakpoint was installed. */
3578
3579 static bool
3580 create_exception_master_breakpoint_hook (objfile *objfile)
3581 {
3582 const char *const func_name = "_Unwind_DebugHook";
3583 struct breakpoint *b;
3584 struct gdbarch *gdbarch;
3585 struct breakpoint_objfile_data *bp_objfile_data;
3586 CORE_ADDR addr;
3587 struct explicit_location explicit_loc;
3588
3589 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3590
3591 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3592 return false;
3593
3594 gdbarch = objfile->arch ();
3595
3596 if (bp_objfile_data->exception_msym.minsym == NULL)
3597 {
3598 struct bound_minimal_symbol debug_hook;
3599
3600 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3601 if (debug_hook.minsym == NULL)
3602 {
3603 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3604 return false;
3605 }
3606
3607 bp_objfile_data->exception_msym = debug_hook;
3608 }
3609
3610 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3611 addr = gdbarch_convert_from_func_ptr_addr
3612 (gdbarch, addr, current_inferior ()->top_target ());
3613 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3614 &internal_breakpoint_ops);
3615 initialize_explicit_location (&explicit_loc);
3616 explicit_loc.function_name = ASTRDUP (func_name);
3617 b->location = new_explicit_location (&explicit_loc);
3618 b->enable_state = bp_disabled;
3619
3620 return true;
3621 }
3622
3623 /* Install a master breakpoint on the unwinder's debug hook. */
3624
3625 static void
3626 create_exception_master_breakpoint (void)
3627 {
3628 for (objfile *obj : current_program_space->objfiles ())
3629 {
3630 /* Skip separate debug object. */
3631 if (obj->separate_debug_objfile_backlink)
3632 continue;
3633
3634 /* Try a probe kind breakpoint. */
3635 if (create_exception_master_breakpoint_probe (obj))
3636 continue;
3637
3638 /* Iterate over main and separate debug objects and try an
3639 _Unwind_DebugHook kind breakpoint. */
3640 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3641 if (create_exception_master_breakpoint_hook (debug_objfile))
3642 break;
3643 }
3644 }
3645
3646 /* Does B have a location spec? */
3647
3648 static int
3649 breakpoint_event_location_empty_p (const struct breakpoint *b)
3650 {
3651 return b->location != NULL && event_location_empty_p (b->location.get ());
3652 }
3653
3654 void
3655 update_breakpoints_after_exec (void)
3656 {
3657 struct bp_location *bploc, **bplocp_tmp;
3658
3659 /* We're about to delete breakpoints from GDB's lists. If the
3660 INSERTED flag is true, GDB will try to lift the breakpoints by
3661 writing the breakpoints' "shadow contents" back into memory. The
3662 "shadow contents" are NOT valid after an exec, so GDB should not
3663 do that. Instead, the target is responsible from marking
3664 breakpoints out as soon as it detects an exec. We don't do that
3665 here instead, because there may be other attempts to delete
3666 breakpoints after detecting an exec and before reaching here. */
3667 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3668 if (bploc->pspace == current_program_space)
3669 gdb_assert (!bploc->inserted);
3670
3671 for (breakpoint *b : all_breakpoints_safe ())
3672 {
3673 if (b->pspace != current_program_space)
3674 continue;
3675
3676 /* Solib breakpoints must be explicitly reset after an exec(). */
3677 if (b->type == bp_shlib_event)
3678 {
3679 delete_breakpoint (b);
3680 continue;
3681 }
3682
3683 /* JIT breakpoints must be explicitly reset after an exec(). */
3684 if (b->type == bp_jit_event)
3685 {
3686 delete_breakpoint (b);
3687 continue;
3688 }
3689
3690 /* Thread event breakpoints must be set anew after an exec(),
3691 as must overlay event and longjmp master breakpoints. */
3692 if (b->type == bp_thread_event || b->type == bp_overlay_event
3693 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3694 || b->type == bp_exception_master)
3695 {
3696 delete_breakpoint (b);
3697 continue;
3698 }
3699
3700 /* Step-resume breakpoints are meaningless after an exec(). */
3701 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3702 {
3703 delete_breakpoint (b);
3704 continue;
3705 }
3706
3707 /* Just like single-step breakpoints. */
3708 if (b->type == bp_single_step)
3709 {
3710 delete_breakpoint (b);
3711 continue;
3712 }
3713
3714 /* Longjmp and longjmp-resume breakpoints are also meaningless
3715 after an exec. */
3716 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3717 || b->type == bp_longjmp_call_dummy
3718 || b->type == bp_exception || b->type == bp_exception_resume)
3719 {
3720 delete_breakpoint (b);
3721 continue;
3722 }
3723
3724 if (b->type == bp_catchpoint)
3725 {
3726 /* For now, none of the bp_catchpoint breakpoints need to
3727 do anything at this point. In the future, if some of
3728 the catchpoints need to something, we will need to add
3729 a new method, and call this method from here. */
3730 continue;
3731 }
3732
3733 /* bp_finish is a special case. The only way we ought to be able
3734 to see one of these when an exec() has happened, is if the user
3735 caught a vfork, and then said "finish". Ordinarily a finish just
3736 carries them to the call-site of the current callee, by setting
3737 a temporary bp there and resuming. But in this case, the finish
3738 will carry them entirely through the vfork & exec.
3739
3740 We don't want to allow a bp_finish to remain inserted now. But
3741 we can't safely delete it, 'cause finish_command has a handle to
3742 the bp on a bpstat, and will later want to delete it. There's a
3743 chance (and I've seen it happen) that if we delete the bp_finish
3744 here, that its storage will get reused by the time finish_command
3745 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3746 We really must allow finish_command to delete a bp_finish.
3747
3748 In the absence of a general solution for the "how do we know
3749 it's safe to delete something others may have handles to?"
3750 problem, what we'll do here is just uninsert the bp_finish, and
3751 let finish_command delete it.
3752
3753 (We know the bp_finish is "doomed" in the sense that it's
3754 momentary, and will be deleted as soon as finish_command sees
3755 the inferior stopped. So it doesn't matter that the bp's
3756 address is probably bogus in the new a.out, unlike e.g., the
3757 solib breakpoints.) */
3758
3759 if (b->type == bp_finish)
3760 {
3761 continue;
3762 }
3763
3764 /* Without a symbolic address, we have little hope of the
3765 pre-exec() address meaning the same thing in the post-exec()
3766 a.out. */
3767 if (breakpoint_event_location_empty_p (b))
3768 {
3769 delete_breakpoint (b);
3770 continue;
3771 }
3772 }
3773 }
3774
3775 int
3776 detach_breakpoints (ptid_t ptid)
3777 {
3778 struct bp_location *bl, **blp_tmp;
3779 int val = 0;
3780 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3781 struct inferior *inf = current_inferior ();
3782
3783 if (ptid.pid () == inferior_ptid.pid ())
3784 error (_("Cannot detach breakpoints of inferior_ptid"));
3785
3786 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3787 inferior_ptid = ptid;
3788 ALL_BP_LOCATIONS (bl, blp_tmp)
3789 {
3790 if (bl->pspace != inf->pspace)
3791 continue;
3792
3793 /* This function must physically remove breakpoints locations
3794 from the specified ptid, without modifying the breakpoint
3795 package's state. Locations of type bp_loc_other are only
3796 maintained at GDB side. So, there is no need to remove
3797 these bp_loc_other locations. Moreover, removing these
3798 would modify the breakpoint package's state. */
3799 if (bl->loc_type == bp_loc_other)
3800 continue;
3801
3802 if (bl->inserted)
3803 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3804 }
3805
3806 return val;
3807 }
3808
3809 /* Remove the breakpoint location BL from the current address space.
3810 Note that this is used to detach breakpoints from a child fork.
3811 When we get here, the child isn't in the inferior list, and neither
3812 do we have objects to represent its address space --- we should
3813 *not* look at bl->pspace->aspace here. */
3814
3815 static int
3816 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3817 {
3818 int val;
3819
3820 /* BL is never in moribund_locations by our callers. */
3821 gdb_assert (bl->owner != NULL);
3822
3823 /* The type of none suggests that owner is actually deleted.
3824 This should not ever happen. */
3825 gdb_assert (bl->owner->type != bp_none);
3826
3827 if (bl->loc_type == bp_loc_software_breakpoint
3828 || bl->loc_type == bp_loc_hardware_breakpoint)
3829 {
3830 /* "Normal" instruction breakpoint: either the standard
3831 trap-instruction bp (bp_breakpoint), or a
3832 bp_hardware_breakpoint. */
3833
3834 /* First check to see if we have to handle an overlay. */
3835 if (overlay_debugging == ovly_off
3836 || bl->section == NULL
3837 || !(section_is_overlay (bl->section)))
3838 {
3839 /* No overlay handling: just remove the breakpoint. */
3840
3841 /* If we're trying to uninsert a memory breakpoint that we
3842 know is set in a dynamic object that is marked
3843 shlib_disabled, then either the dynamic object was
3844 removed with "remove-symbol-file" or with
3845 "nosharedlibrary". In the former case, we don't know
3846 whether another dynamic object might have loaded over the
3847 breakpoint's address -- the user might well let us know
3848 about it next with add-symbol-file (the whole point of
3849 add-symbol-file is letting the user manually maintain a
3850 list of dynamically loaded objects). If we have the
3851 breakpoint's shadow memory, that is, this is a software
3852 breakpoint managed by GDB, check whether the breakpoint
3853 is still inserted in memory, to avoid overwriting wrong
3854 code with stale saved shadow contents. Note that HW
3855 breakpoints don't have shadow memory, as they're
3856 implemented using a mechanism that is not dependent on
3857 being able to modify the target's memory, and as such
3858 they should always be removed. */
3859 if (bl->shlib_disabled
3860 && bl->target_info.shadow_len != 0
3861 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3862 val = 0;
3863 else
3864 val = bl->owner->ops->remove_location (bl, reason);
3865 }
3866 else
3867 {
3868 /* This breakpoint is in an overlay section.
3869 Did we set a breakpoint at the LMA? */
3870 if (!overlay_events_enabled)
3871 {
3872 /* Yes -- overlay event support is not active, so we
3873 should have set a breakpoint at the LMA. Remove it.
3874 */
3875 /* Ignore any failures: if the LMA is in ROM, we will
3876 have already warned when we failed to insert it. */
3877 if (bl->loc_type == bp_loc_hardware_breakpoint)
3878 target_remove_hw_breakpoint (bl->gdbarch,
3879 &bl->overlay_target_info);
3880 else
3881 target_remove_breakpoint (bl->gdbarch,
3882 &bl->overlay_target_info,
3883 reason);
3884 }
3885 /* Did we set a breakpoint at the VMA?
3886 If so, we will have marked the breakpoint 'inserted'. */
3887 if (bl->inserted)
3888 {
3889 /* Yes -- remove it. Previously we did not bother to
3890 remove the breakpoint if the section had been
3891 unmapped, but let's not rely on that being safe. We
3892 don't know what the overlay manager might do. */
3893
3894 /* However, we should remove *software* breakpoints only
3895 if the section is still mapped, or else we overwrite
3896 wrong code with the saved shadow contents. */
3897 if (bl->loc_type == bp_loc_hardware_breakpoint
3898 || section_is_mapped (bl->section))
3899 val = bl->owner->ops->remove_location (bl, reason);
3900 else
3901 val = 0;
3902 }
3903 else
3904 {
3905 /* No -- not inserted, so no need to remove. No error. */
3906 val = 0;
3907 }
3908 }
3909
3910 /* In some cases, we might not be able to remove a breakpoint in
3911 a shared library that has already been removed, but we have
3912 not yet processed the shlib unload event. Similarly for an
3913 unloaded add-symbol-file object - the user might not yet have
3914 had the chance to remove-symbol-file it. shlib_disabled will
3915 be set if the library/object has already been removed, but
3916 the breakpoint hasn't been uninserted yet, e.g., after
3917 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3918 always-inserted mode. */
3919 if (val
3920 && (bl->loc_type == bp_loc_software_breakpoint
3921 && (bl->shlib_disabled
3922 || solib_name_from_address (bl->pspace, bl->address)
3923 || shared_objfile_contains_address_p (bl->pspace,
3924 bl->address))))
3925 val = 0;
3926
3927 if (val)
3928 return val;
3929 bl->inserted = (reason == DETACH_BREAKPOINT);
3930 }
3931 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3932 {
3933 gdb_assert (bl->owner->ops != NULL
3934 && bl->owner->ops->remove_location != NULL);
3935
3936 bl->inserted = (reason == DETACH_BREAKPOINT);
3937 bl->owner->ops->remove_location (bl, reason);
3938
3939 /* Failure to remove any of the hardware watchpoints comes here. */
3940 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3941 warning (_("Could not remove hardware watchpoint %d."),
3942 bl->owner->number);
3943 }
3944 else if (bl->owner->type == bp_catchpoint
3945 && breakpoint_enabled (bl->owner)
3946 && !bl->duplicate)
3947 {
3948 gdb_assert (bl->owner->ops != NULL
3949 && bl->owner->ops->remove_location != NULL);
3950
3951 val = bl->owner->ops->remove_location (bl, reason);
3952 if (val)
3953 return val;
3954
3955 bl->inserted = (reason == DETACH_BREAKPOINT);
3956 }
3957
3958 return 0;
3959 }
3960
3961 static int
3962 remove_breakpoint (struct bp_location *bl)
3963 {
3964 /* BL is never in moribund_locations by our callers. */
3965 gdb_assert (bl->owner != NULL);
3966
3967 /* The type of none suggests that owner is actually deleted.
3968 This should not ever happen. */
3969 gdb_assert (bl->owner->type != bp_none);
3970
3971 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3972
3973 switch_to_program_space_and_thread (bl->pspace);
3974
3975 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
3976 }
3977
3978 /* Clear the "inserted" flag in all breakpoints. */
3979
3980 void
3981 mark_breakpoints_out (void)
3982 {
3983 struct bp_location *bl, **blp_tmp;
3984
3985 ALL_BP_LOCATIONS (bl, blp_tmp)
3986 if (bl->pspace == current_program_space)
3987 bl->inserted = 0;
3988 }
3989
3990 /* Clear the "inserted" flag in all breakpoints and delete any
3991 breakpoints which should go away between runs of the program.
3992
3993 Plus other such housekeeping that has to be done for breakpoints
3994 between runs.
3995
3996 Note: this function gets called at the end of a run (by
3997 generic_mourn_inferior) and when a run begins (by
3998 init_wait_for_inferior). */
3999
4000
4001
4002 void
4003 breakpoint_init_inferior (enum inf_context context)
4004 {
4005 struct program_space *pspace = current_program_space;
4006
4007 /* If breakpoint locations are shared across processes, then there's
4008 nothing to do. */
4009 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4010 return;
4011
4012 mark_breakpoints_out ();
4013
4014 for (breakpoint *b : all_breakpoints_safe ())
4015 {
4016 if (b->loc && b->loc->pspace != pspace)
4017 continue;
4018
4019 switch (b->type)
4020 {
4021 case bp_call_dummy:
4022 case bp_longjmp_call_dummy:
4023
4024 /* If the call dummy breakpoint is at the entry point it will
4025 cause problems when the inferior is rerun, so we better get
4026 rid of it. */
4027
4028 case bp_watchpoint_scope:
4029
4030 /* Also get rid of scope breakpoints. */
4031
4032 case bp_shlib_event:
4033
4034 /* Also remove solib event breakpoints. Their addresses may
4035 have changed since the last time we ran the program.
4036 Actually we may now be debugging against different target;
4037 and so the solib backend that installed this breakpoint may
4038 not be used in by the target. E.g.,
4039
4040 (gdb) file prog-linux
4041 (gdb) run # native linux target
4042 ...
4043 (gdb) kill
4044 (gdb) file prog-win.exe
4045 (gdb) tar rem :9999 # remote Windows gdbserver.
4046 */
4047
4048 case bp_step_resume:
4049
4050 /* Also remove step-resume breakpoints. */
4051
4052 case bp_single_step:
4053
4054 /* Also remove single-step breakpoints. */
4055
4056 delete_breakpoint (b);
4057 break;
4058
4059 case bp_watchpoint:
4060 case bp_hardware_watchpoint:
4061 case bp_read_watchpoint:
4062 case bp_access_watchpoint:
4063 {
4064 struct watchpoint *w = (struct watchpoint *) b;
4065
4066 /* Likewise for watchpoints on local expressions. */
4067 if (w->exp_valid_block != NULL)
4068 delete_breakpoint (b);
4069 else
4070 {
4071 /* Get rid of existing locations, which are no longer
4072 valid. New ones will be created in
4073 update_watchpoint, when the inferior is restarted.
4074 The next update_global_location_list call will
4075 garbage collect them. */
4076 b->loc = NULL;
4077
4078 if (context == inf_starting)
4079 {
4080 /* Reset val field to force reread of starting value in
4081 insert_breakpoints. */
4082 w->val.reset (nullptr);
4083 w->val_valid = false;
4084 }
4085 }
4086 }
4087 break;
4088 default:
4089 break;
4090 }
4091 }
4092
4093 /* Get rid of the moribund locations. */
4094 for (bp_location *bl : moribund_locations)
4095 decref_bp_location (&bl);
4096 moribund_locations.clear ();
4097 }
4098
4099 /* These functions concern about actual breakpoints inserted in the
4100 target --- to e.g. check if we need to do decr_pc adjustment or if
4101 we need to hop over the bkpt --- so we check for address space
4102 match, not program space. */
4103
4104 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4105 exists at PC. It returns ordinary_breakpoint_here if it's an
4106 ordinary breakpoint, or permanent_breakpoint_here if it's a
4107 permanent breakpoint.
4108 - When continuing from a location with an ordinary breakpoint, we
4109 actually single step once before calling insert_breakpoints.
4110 - When continuing from a location with a permanent breakpoint, we
4111 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4112 the target, to advance the PC past the breakpoint. */
4113
4114 enum breakpoint_here
4115 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4116 {
4117 struct bp_location *bl, **blp_tmp;
4118 int any_breakpoint_here = 0;
4119
4120 ALL_BP_LOCATIONS (bl, blp_tmp)
4121 {
4122 if (bl->loc_type != bp_loc_software_breakpoint
4123 && bl->loc_type != bp_loc_hardware_breakpoint)
4124 continue;
4125
4126 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4127 if ((breakpoint_enabled (bl->owner)
4128 || bl->permanent)
4129 && breakpoint_location_address_match (bl, aspace, pc))
4130 {
4131 if (overlay_debugging
4132 && section_is_overlay (bl->section)
4133 && !section_is_mapped (bl->section))
4134 continue; /* unmapped overlay -- can't be a match */
4135 else if (bl->permanent)
4136 return permanent_breakpoint_here;
4137 else
4138 any_breakpoint_here = 1;
4139 }
4140 }
4141
4142 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4143 }
4144
4145 /* See breakpoint.h. */
4146
4147 int
4148 breakpoint_in_range_p (const address_space *aspace,
4149 CORE_ADDR addr, ULONGEST len)
4150 {
4151 struct bp_location *bl, **blp_tmp;
4152
4153 ALL_BP_LOCATIONS (bl, blp_tmp)
4154 {
4155 if (bl->loc_type != bp_loc_software_breakpoint
4156 && bl->loc_type != bp_loc_hardware_breakpoint)
4157 continue;
4158
4159 if ((breakpoint_enabled (bl->owner)
4160 || bl->permanent)
4161 && breakpoint_location_address_range_overlap (bl, aspace,
4162 addr, len))
4163 {
4164 if (overlay_debugging
4165 && section_is_overlay (bl->section)
4166 && !section_is_mapped (bl->section))
4167 {
4168 /* Unmapped overlay -- can't be a match. */
4169 continue;
4170 }
4171
4172 return 1;
4173 }
4174 }
4175
4176 return 0;
4177 }
4178
4179 /* Return true if there's a moribund breakpoint at PC. */
4180
4181 int
4182 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4183 {
4184 for (bp_location *loc : moribund_locations)
4185 if (breakpoint_location_address_match (loc, aspace, pc))
4186 return 1;
4187
4188 return 0;
4189 }
4190
4191 /* Returns non-zero iff BL is inserted at PC, in address space
4192 ASPACE. */
4193
4194 static int
4195 bp_location_inserted_here_p (struct bp_location *bl,
4196 const address_space *aspace, CORE_ADDR pc)
4197 {
4198 if (bl->inserted
4199 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4200 aspace, pc))
4201 {
4202 if (overlay_debugging
4203 && section_is_overlay (bl->section)
4204 && !section_is_mapped (bl->section))
4205 return 0; /* unmapped overlay -- can't be a match */
4206 else
4207 return 1;
4208 }
4209 return 0;
4210 }
4211
4212 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4213
4214 int
4215 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4216 {
4217 struct bp_location **blp, **blp_tmp = NULL;
4218
4219 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4220 {
4221 struct bp_location *bl = *blp;
4222
4223 if (bl->loc_type != bp_loc_software_breakpoint
4224 && bl->loc_type != bp_loc_hardware_breakpoint)
4225 continue;
4226
4227 if (bp_location_inserted_here_p (bl, aspace, pc))
4228 return 1;
4229 }
4230 return 0;
4231 }
4232
4233 /* This function returns non-zero iff there is a software breakpoint
4234 inserted at PC. */
4235
4236 int
4237 software_breakpoint_inserted_here_p (const address_space *aspace,
4238 CORE_ADDR pc)
4239 {
4240 struct bp_location **blp, **blp_tmp = NULL;
4241
4242 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4243 {
4244 struct bp_location *bl = *blp;
4245
4246 if (bl->loc_type != bp_loc_software_breakpoint)
4247 continue;
4248
4249 if (bp_location_inserted_here_p (bl, aspace, pc))
4250 return 1;
4251 }
4252
4253 return 0;
4254 }
4255
4256 /* See breakpoint.h. */
4257
4258 int
4259 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4260 CORE_ADDR pc)
4261 {
4262 struct bp_location **blp, **blp_tmp = NULL;
4263
4264 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4265 {
4266 struct bp_location *bl = *blp;
4267
4268 if (bl->loc_type != bp_loc_hardware_breakpoint)
4269 continue;
4270
4271 if (bp_location_inserted_here_p (bl, aspace, pc))
4272 return 1;
4273 }
4274
4275 return 0;
4276 }
4277
4278 int
4279 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4280 CORE_ADDR addr, ULONGEST len)
4281 {
4282 for (breakpoint *bpt : all_breakpoints ())
4283 {
4284 if (bpt->type != bp_hardware_watchpoint
4285 && bpt->type != bp_access_watchpoint)
4286 continue;
4287
4288 if (!breakpoint_enabled (bpt))
4289 continue;
4290
4291 for (bp_location *loc : bpt->locations ())
4292 if (loc->pspace->aspace == aspace && loc->inserted)
4293 {
4294 CORE_ADDR l, h;
4295
4296 /* Check for intersection. */
4297 l = std::max<CORE_ADDR> (loc->address, addr);
4298 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4299 if (l < h)
4300 return 1;
4301 }
4302 }
4303 return 0;
4304 }
4305
4306 /* See breakpoint.h. */
4307
4308 bool
4309 is_catchpoint (struct breakpoint *b)
4310 {
4311 return (b->type == bp_catchpoint);
4312 }
4313
4314 /* Clear a bpstat so that it says we are not at any breakpoint.
4315 Also free any storage that is part of a bpstat. */
4316
4317 void
4318 bpstat_clear (bpstat *bsp)
4319 {
4320 bpstat p;
4321 bpstat q;
4322
4323 if (bsp == 0)
4324 return;
4325 p = *bsp;
4326 while (p != NULL)
4327 {
4328 q = p->next;
4329 delete p;
4330 p = q;
4331 }
4332 *bsp = NULL;
4333 }
4334
4335 bpstats::bpstats (const bpstats &other)
4336 : next (NULL),
4337 bp_location_at (other.bp_location_at),
4338 breakpoint_at (other.breakpoint_at),
4339 commands (other.commands),
4340 print (other.print),
4341 stop (other.stop),
4342 print_it (other.print_it)
4343 {
4344 if (other.old_val != NULL)
4345 old_val = release_value (value_copy (other.old_val.get ()));
4346 }
4347
4348 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4349 is part of the bpstat is copied as well. */
4350
4351 bpstat
4352 bpstat_copy (bpstat bs)
4353 {
4354 bpstat p = NULL;
4355 bpstat tmp;
4356 bpstat retval = NULL;
4357
4358 if (bs == NULL)
4359 return bs;
4360
4361 for (; bs != NULL; bs = bs->next)
4362 {
4363 tmp = new bpstats (*bs);
4364
4365 if (p == NULL)
4366 /* This is the first thing in the chain. */
4367 retval = tmp;
4368 else
4369 p->next = tmp;
4370 p = tmp;
4371 }
4372 p->next = NULL;
4373 return retval;
4374 }
4375
4376 /* Find the bpstat associated with this breakpoint. */
4377
4378 bpstat
4379 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4380 {
4381 if (bsp == NULL)
4382 return NULL;
4383
4384 for (; bsp != NULL; bsp = bsp->next)
4385 {
4386 if (bsp->breakpoint_at == breakpoint)
4387 return bsp;
4388 }
4389 return NULL;
4390 }
4391
4392 /* See breakpoint.h. */
4393
4394 bool
4395 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4396 {
4397 for (; bsp != NULL; bsp = bsp->next)
4398 {
4399 if (bsp->breakpoint_at == NULL)
4400 {
4401 /* A moribund location can never explain a signal other than
4402 GDB_SIGNAL_TRAP. */
4403 if (sig == GDB_SIGNAL_TRAP)
4404 return true;
4405 }
4406 else
4407 {
4408 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4409 sig))
4410 return true;
4411 }
4412 }
4413
4414 return false;
4415 }
4416
4417 /* Put in *NUM the breakpoint number of the first breakpoint we are
4418 stopped at. *BSP upon return is a bpstat which points to the
4419 remaining breakpoints stopped at (but which is not guaranteed to be
4420 good for anything but further calls to bpstat_num).
4421
4422 Return 0 if passed a bpstat which does not indicate any breakpoints.
4423 Return -1 if stopped at a breakpoint that has been deleted since
4424 we set it.
4425 Return 1 otherwise. */
4426
4427 int
4428 bpstat_num (bpstat *bsp, int *num)
4429 {
4430 struct breakpoint *b;
4431
4432 if ((*bsp) == NULL)
4433 return 0; /* No more breakpoint values */
4434
4435 /* We assume we'll never have several bpstats that correspond to a
4436 single breakpoint -- otherwise, this function might return the
4437 same number more than once and this will look ugly. */
4438 b = (*bsp)->breakpoint_at;
4439 *bsp = (*bsp)->next;
4440 if (b == NULL)
4441 return -1; /* breakpoint that's been deleted since */
4442
4443 *num = b->number; /* We have its number */
4444 return 1;
4445 }
4446
4447 /* See breakpoint.h. */
4448
4449 void
4450 bpstat_clear_actions (void)
4451 {
4452 bpstat bs;
4453
4454 if (inferior_ptid == null_ptid)
4455 return;
4456
4457 thread_info *tp = inferior_thread ();
4458 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4459 {
4460 bs->commands = NULL;
4461 bs->old_val.reset (nullptr);
4462 }
4463 }
4464
4465 /* Called when a command is about to proceed the inferior. */
4466
4467 static void
4468 breakpoint_about_to_proceed (void)
4469 {
4470 if (inferior_ptid != null_ptid)
4471 {
4472 struct thread_info *tp = inferior_thread ();
4473
4474 /* Allow inferior function calls in breakpoint commands to not
4475 interrupt the command list. When the call finishes
4476 successfully, the inferior will be standing at the same
4477 breakpoint as if nothing happened. */
4478 if (tp->control.in_infcall)
4479 return;
4480 }
4481
4482 breakpoint_proceeded = 1;
4483 }
4484
4485 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4486 or its equivalent. */
4487
4488 static int
4489 command_line_is_silent (struct command_line *cmd)
4490 {
4491 return cmd && (strcmp ("silent", cmd->line) == 0);
4492 }
4493
4494 /* Execute all the commands associated with all the breakpoints at
4495 this location. Any of these commands could cause the process to
4496 proceed beyond this point, etc. We look out for such changes by
4497 checking the global "breakpoint_proceeded" after each command.
4498
4499 Returns true if a breakpoint command resumed the inferior. In that
4500 case, it is the caller's responsibility to recall it again with the
4501 bpstat of the current thread. */
4502
4503 static int
4504 bpstat_do_actions_1 (bpstat *bsp)
4505 {
4506 bpstat bs;
4507 int again = 0;
4508
4509 /* Avoid endless recursion if a `source' command is contained
4510 in bs->commands. */
4511 if (executing_breakpoint_commands)
4512 return 0;
4513
4514 scoped_restore save_executing
4515 = make_scoped_restore (&executing_breakpoint_commands, 1);
4516
4517 scoped_restore preventer = prevent_dont_repeat ();
4518
4519 /* This pointer will iterate over the list of bpstat's. */
4520 bs = *bsp;
4521
4522 breakpoint_proceeded = 0;
4523 for (; bs != NULL; bs = bs->next)
4524 {
4525 struct command_line *cmd = NULL;
4526
4527 /* Take ownership of the BSP's command tree, if it has one.
4528
4529 The command tree could legitimately contain commands like
4530 'step' and 'next', which call clear_proceed_status, which
4531 frees stop_bpstat's command tree. To make sure this doesn't
4532 free the tree we're executing out from under us, we need to
4533 take ownership of the tree ourselves. Since a given bpstat's
4534 commands are only executed once, we don't need to copy it; we
4535 can clear the pointer in the bpstat, and make sure we free
4536 the tree when we're done. */
4537 counted_command_line ccmd = bs->commands;
4538 bs->commands = NULL;
4539 if (ccmd != NULL)
4540 cmd = ccmd.get ();
4541 if (command_line_is_silent (cmd))
4542 {
4543 /* The action has been already done by bpstat_stop_status. */
4544 cmd = cmd->next;
4545 }
4546
4547 while (cmd != NULL)
4548 {
4549 execute_control_command (cmd);
4550
4551 if (breakpoint_proceeded)
4552 break;
4553 else
4554 cmd = cmd->next;
4555 }
4556
4557 if (breakpoint_proceeded)
4558 {
4559 if (current_ui->async)
4560 /* If we are in async mode, then the target might be still
4561 running, not stopped at any breakpoint, so nothing for
4562 us to do here -- just return to the event loop. */
4563 ;
4564 else
4565 /* In sync mode, when execute_control_command returns
4566 we're already standing on the next breakpoint.
4567 Breakpoint commands for that stop were not run, since
4568 execute_command does not run breakpoint commands --
4569 only command_line_handler does, but that one is not
4570 involved in execution of breakpoint commands. So, we
4571 can now execute breakpoint commands. It should be
4572 noted that making execute_command do bpstat actions is
4573 not an option -- in this case we'll have recursive
4574 invocation of bpstat for each breakpoint with a
4575 command, and can easily blow up GDB stack. Instead, we
4576 return true, which will trigger the caller to recall us
4577 with the new stop_bpstat. */
4578 again = 1;
4579 break;
4580 }
4581 }
4582 return again;
4583 }
4584
4585 /* Helper for bpstat_do_actions. Get the current thread, if there's
4586 one, is alive and has execution. Return NULL otherwise. */
4587
4588 static thread_info *
4589 get_bpstat_thread ()
4590 {
4591 if (inferior_ptid == null_ptid || !target_has_execution ())
4592 return NULL;
4593
4594 thread_info *tp = inferior_thread ();
4595 if (tp->state == THREAD_EXITED || tp->executing)
4596 return NULL;
4597 return tp;
4598 }
4599
4600 void
4601 bpstat_do_actions (void)
4602 {
4603 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4604 thread_info *tp;
4605
4606 /* Do any commands attached to breakpoint we are stopped at. */
4607 while ((tp = get_bpstat_thread ()) != NULL)
4608 {
4609 /* Since in sync mode, bpstat_do_actions may resume the
4610 inferior, and only return when it is stopped at the next
4611 breakpoint, we keep doing breakpoint actions until it returns
4612 false to indicate the inferior was not resumed. */
4613 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4614 break;
4615 }
4616
4617 cleanup_if_error.release ();
4618 }
4619
4620 /* Print out the (old or new) value associated with a watchpoint. */
4621
4622 static void
4623 watchpoint_value_print (struct value *val, struct ui_file *stream)
4624 {
4625 if (val == NULL)
4626 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4627 else
4628 {
4629 struct value_print_options opts;
4630 get_user_print_options (&opts);
4631 value_print (val, stream, &opts);
4632 }
4633 }
4634
4635 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4636 debugging multiple threads. */
4637
4638 void
4639 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4640 {
4641 if (uiout->is_mi_like_p ())
4642 return;
4643
4644 uiout->text ("\n");
4645
4646 if (show_thread_that_caused_stop ())
4647 {
4648 const char *name;
4649 struct thread_info *thr = inferior_thread ();
4650
4651 uiout->text ("Thread ");
4652 uiout->field_string ("thread-id", print_thread_id (thr));
4653
4654 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4655 if (name != NULL)
4656 {
4657 uiout->text (" \"");
4658 uiout->field_string ("name", name);
4659 uiout->text ("\"");
4660 }
4661
4662 uiout->text (" hit ");
4663 }
4664 }
4665
4666 /* Generic routine for printing messages indicating why we
4667 stopped. The behavior of this function depends on the value
4668 'print_it' in the bpstat structure. Under some circumstances we
4669 may decide not to print anything here and delegate the task to
4670 normal_stop(). */
4671
4672 static enum print_stop_action
4673 print_bp_stop_message (bpstat bs)
4674 {
4675 switch (bs->print_it)
4676 {
4677 case print_it_noop:
4678 /* Nothing should be printed for this bpstat entry. */
4679 return PRINT_UNKNOWN;
4680 break;
4681
4682 case print_it_done:
4683 /* We still want to print the frame, but we already printed the
4684 relevant messages. */
4685 return PRINT_SRC_AND_LOC;
4686 break;
4687
4688 case print_it_normal:
4689 {
4690 struct breakpoint *b = bs->breakpoint_at;
4691
4692 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4693 which has since been deleted. */
4694 if (b == NULL)
4695 return PRINT_UNKNOWN;
4696
4697 /* Normal case. Call the breakpoint's print_it method. */
4698 return b->ops->print_it (bs);
4699 }
4700 break;
4701
4702 default:
4703 internal_error (__FILE__, __LINE__,
4704 _("print_bp_stop_message: unrecognized enum value"));
4705 break;
4706 }
4707 }
4708
4709 /* A helper function that prints a shared library stopped event. */
4710
4711 static void
4712 print_solib_event (int is_catchpoint)
4713 {
4714 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4715 bool any_added = !current_program_space->added_solibs.empty ();
4716
4717 if (!is_catchpoint)
4718 {
4719 if (any_added || any_deleted)
4720 current_uiout->text (_("Stopped due to shared library event:\n"));
4721 else
4722 current_uiout->text (_("Stopped due to shared library event (no "
4723 "libraries added or removed)\n"));
4724 }
4725
4726 if (current_uiout->is_mi_like_p ())
4727 current_uiout->field_string ("reason",
4728 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4729
4730 if (any_deleted)
4731 {
4732 current_uiout->text (_(" Inferior unloaded "));
4733 ui_out_emit_list list_emitter (current_uiout, "removed");
4734 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4735 {
4736 const std::string &name = current_program_space->deleted_solibs[ix];
4737
4738 if (ix > 0)
4739 current_uiout->text (" ");
4740 current_uiout->field_string ("library", name);
4741 current_uiout->text ("\n");
4742 }
4743 }
4744
4745 if (any_added)
4746 {
4747 current_uiout->text (_(" Inferior loaded "));
4748 ui_out_emit_list list_emitter (current_uiout, "added");
4749 bool first = true;
4750 for (so_list *iter : current_program_space->added_solibs)
4751 {
4752 if (!first)
4753 current_uiout->text (" ");
4754 first = false;
4755 current_uiout->field_string ("library", iter->so_name);
4756 current_uiout->text ("\n");
4757 }
4758 }
4759 }
4760
4761 /* Print a message indicating what happened. This is called from
4762 normal_stop(). The input to this routine is the head of the bpstat
4763 list - a list of the eventpoints that caused this stop. KIND is
4764 the target_waitkind for the stopping event. This
4765 routine calls the generic print routine for printing a message
4766 about reasons for stopping. This will print (for example) the
4767 "Breakpoint n," part of the output. The return value of this
4768 routine is one of:
4769
4770 PRINT_UNKNOWN: Means we printed nothing.
4771 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4772 code to print the location. An example is
4773 "Breakpoint 1, " which should be followed by
4774 the location.
4775 PRINT_SRC_ONLY: Means we printed something, but there is no need
4776 to also print the location part of the message.
4777 An example is the catch/throw messages, which
4778 don't require a location appended to the end.
4779 PRINT_NOTHING: We have done some printing and we don't need any
4780 further info to be printed. */
4781
4782 enum print_stop_action
4783 bpstat_print (bpstat bs, int kind)
4784 {
4785 enum print_stop_action val;
4786
4787 /* Maybe another breakpoint in the chain caused us to stop.
4788 (Currently all watchpoints go on the bpstat whether hit or not.
4789 That probably could (should) be changed, provided care is taken
4790 with respect to bpstat_explains_signal). */
4791 for (; bs; bs = bs->next)
4792 {
4793 val = print_bp_stop_message (bs);
4794 if (val == PRINT_SRC_ONLY
4795 || val == PRINT_SRC_AND_LOC
4796 || val == PRINT_NOTHING)
4797 return val;
4798 }
4799
4800 /* If we had hit a shared library event breakpoint,
4801 print_bp_stop_message would print out this message. If we hit an
4802 OS-level shared library event, do the same thing. */
4803 if (kind == TARGET_WAITKIND_LOADED)
4804 {
4805 print_solib_event (0);
4806 return PRINT_NOTHING;
4807 }
4808
4809 /* We reached the end of the chain, or we got a null BS to start
4810 with and nothing was printed. */
4811 return PRINT_UNKNOWN;
4812 }
4813
4814 /* Evaluate the boolean expression EXP and return the result. */
4815
4816 static bool
4817 breakpoint_cond_eval (expression *exp)
4818 {
4819 struct value *mark = value_mark ();
4820 bool res = value_true (evaluate_expression (exp));
4821
4822 value_free_to_mark (mark);
4823 return res;
4824 }
4825
4826 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4827
4828 bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4829 : next (NULL),
4830 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
4831 breakpoint_at (bl->owner),
4832 commands (NULL),
4833 print (0),
4834 stop (0),
4835 print_it (print_it_normal)
4836 {
4837 **bs_link_pointer = this;
4838 *bs_link_pointer = &next;
4839 }
4840
4841 bpstats::bpstats ()
4842 : next (NULL),
4843 breakpoint_at (NULL),
4844 commands (NULL),
4845 print (0),
4846 stop (0),
4847 print_it (print_it_normal)
4848 {
4849 }
4850 \f
4851 /* The target has stopped with waitstatus WS. Check if any hardware
4852 watchpoints have triggered, according to the target. */
4853
4854 int
4855 watchpoints_triggered (struct target_waitstatus *ws)
4856 {
4857 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
4858 CORE_ADDR addr;
4859
4860 if (!stopped_by_watchpoint)
4861 {
4862 /* We were not stopped by a watchpoint. Mark all watchpoints
4863 as not triggered. */
4864 for (breakpoint *b : all_breakpoints ())
4865 if (is_hardware_watchpoint (b))
4866 {
4867 struct watchpoint *w = (struct watchpoint *) b;
4868
4869 w->watchpoint_triggered = watch_triggered_no;
4870 }
4871
4872 return 0;
4873 }
4874
4875 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
4876 {
4877 /* We were stopped by a watchpoint, but we don't know where.
4878 Mark all watchpoints as unknown. */
4879 for (breakpoint *b : all_breakpoints ())
4880 if (is_hardware_watchpoint (b))
4881 {
4882 struct watchpoint *w = (struct watchpoint *) b;
4883
4884 w->watchpoint_triggered = watch_triggered_unknown;
4885 }
4886
4887 return 1;
4888 }
4889
4890 /* The target could report the data address. Mark watchpoints
4891 affected by this data address as triggered, and all others as not
4892 triggered. */
4893
4894 for (breakpoint *b : all_breakpoints ())
4895 if (is_hardware_watchpoint (b))
4896 {
4897 struct watchpoint *w = (struct watchpoint *) b;
4898
4899 w->watchpoint_triggered = watch_triggered_no;
4900 for (bp_location *loc : b->locations ())
4901 {
4902 if (is_masked_watchpoint (b))
4903 {
4904 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4905 CORE_ADDR start = loc->address & w->hw_wp_mask;
4906
4907 if (newaddr == start)
4908 {
4909 w->watchpoint_triggered = watch_triggered_yes;
4910 break;
4911 }
4912 }
4913 /* Exact match not required. Within range is sufficient. */
4914 else if (target_watchpoint_addr_within_range
4915 (current_inferior ()->top_target (), addr, loc->address,
4916 loc->length))
4917 {
4918 w->watchpoint_triggered = watch_triggered_yes;
4919 break;
4920 }
4921 }
4922 }
4923
4924 return 1;
4925 }
4926
4927 /* Possible return values for watchpoint_check. */
4928 enum wp_check_result
4929 {
4930 /* The watchpoint has been deleted. */
4931 WP_DELETED = 1,
4932
4933 /* The value has changed. */
4934 WP_VALUE_CHANGED = 2,
4935
4936 /* The value has not changed. */
4937 WP_VALUE_NOT_CHANGED = 3,
4938
4939 /* Ignore this watchpoint, no matter if the value changed or not. */
4940 WP_IGNORE = 4,
4941 };
4942
4943 #define BP_TEMPFLAG 1
4944 #define BP_HARDWAREFLAG 2
4945
4946 /* Evaluate watchpoint condition expression and check if its value
4947 changed. */
4948
4949 static wp_check_result
4950 watchpoint_check (bpstat bs)
4951 {
4952 struct watchpoint *b;
4953 struct frame_info *fr;
4954 int within_current_scope;
4955
4956 /* BS is built from an existing struct breakpoint. */
4957 gdb_assert (bs->breakpoint_at != NULL);
4958 b = (struct watchpoint *) bs->breakpoint_at;
4959
4960 /* If this is a local watchpoint, we only want to check if the
4961 watchpoint frame is in scope if the current thread is the thread
4962 that was used to create the watchpoint. */
4963 if (!watchpoint_in_thread_scope (b))
4964 return WP_IGNORE;
4965
4966 if (b->exp_valid_block == NULL)
4967 within_current_scope = 1;
4968 else
4969 {
4970 struct frame_info *frame = get_current_frame ();
4971 struct gdbarch *frame_arch = get_frame_arch (frame);
4972 CORE_ADDR frame_pc = get_frame_pc (frame);
4973
4974 /* stack_frame_destroyed_p() returns a non-zero value if we're
4975 still in the function but the stack frame has already been
4976 invalidated. Since we can't rely on the values of local
4977 variables after the stack has been destroyed, we are treating
4978 the watchpoint in that state as `not changed' without further
4979 checking. Don't mark watchpoints as changed if the current
4980 frame is in an epilogue - even if they are in some other
4981 frame, our view of the stack is likely to be wrong and
4982 frame_find_by_id could error out. */
4983 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
4984 return WP_IGNORE;
4985
4986 fr = frame_find_by_id (b->watchpoint_frame);
4987 within_current_scope = (fr != NULL);
4988
4989 /* If we've gotten confused in the unwinder, we might have
4990 returned a frame that can't describe this variable. */
4991 if (within_current_scope)
4992 {
4993 struct symbol *function;
4994
4995 function = get_frame_function (fr);
4996 if (function == NULL
4997 || !contained_in (b->exp_valid_block,
4998 SYMBOL_BLOCK_VALUE (function)))
4999 within_current_scope = 0;
5000 }
5001
5002 if (within_current_scope)
5003 /* If we end up stopping, the current frame will get selected
5004 in normal_stop. So this call to select_frame won't affect
5005 the user. */
5006 select_frame (fr);
5007 }
5008
5009 if (within_current_scope)
5010 {
5011 /* We use value_{,free_to_}mark because it could be a *long*
5012 time before we return to the command level and call
5013 free_all_values. We can't call free_all_values because we
5014 might be in the middle of evaluating a function call. */
5015
5016 struct value *mark;
5017 struct value *new_val;
5018
5019 if (is_masked_watchpoint (b))
5020 /* Since we don't know the exact trigger address (from
5021 stopped_data_address), just tell the user we've triggered
5022 a mask watchpoint. */
5023 return WP_VALUE_CHANGED;
5024
5025 mark = value_mark ();
5026 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
5027 NULL, NULL, false);
5028
5029 if (b->val_bitsize != 0)
5030 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5031
5032 /* We use value_equal_contents instead of value_equal because
5033 the latter coerces an array to a pointer, thus comparing just
5034 the address of the array instead of its contents. This is
5035 not what we want. */
5036 if ((b->val != NULL) != (new_val != NULL)
5037 || (b->val != NULL && !value_equal_contents (b->val.get (),
5038 new_val)))
5039 {
5040 bs->old_val = b->val;
5041 b->val = release_value (new_val);
5042 b->val_valid = true;
5043 if (new_val != NULL)
5044 value_free_to_mark (mark);
5045 return WP_VALUE_CHANGED;
5046 }
5047 else
5048 {
5049 /* Nothing changed. */
5050 value_free_to_mark (mark);
5051 return WP_VALUE_NOT_CHANGED;
5052 }
5053 }
5054 else
5055 {
5056 /* This seems like the only logical thing to do because
5057 if we temporarily ignored the watchpoint, then when
5058 we reenter the block in which it is valid it contains
5059 garbage (in the case of a function, it may have two
5060 garbage values, one before and one after the prologue).
5061 So we can't even detect the first assignment to it and
5062 watch after that (since the garbage may or may not equal
5063 the first value assigned). */
5064 /* We print all the stop information in
5065 breakpoint_ops->print_it, but in this case, by the time we
5066 call breakpoint_ops->print_it this bp will be deleted
5067 already. So we have no choice but print the information
5068 here. */
5069
5070 SWITCH_THRU_ALL_UIS ()
5071 {
5072 struct ui_out *uiout = current_uiout;
5073
5074 if (uiout->is_mi_like_p ())
5075 uiout->field_string
5076 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5077 uiout->message ("\nWatchpoint %pF deleted because the program has "
5078 "left the block in\n"
5079 "which its expression is valid.\n",
5080 signed_field ("wpnum", b->number));
5081 }
5082
5083 /* Make sure the watchpoint's commands aren't executed. */
5084 b->commands = NULL;
5085 watchpoint_del_at_next_stop (b);
5086
5087 return WP_DELETED;
5088 }
5089 }
5090
5091 /* Return true if it looks like target has stopped due to hitting
5092 breakpoint location BL. This function does not check if we should
5093 stop, only if BL explains the stop. */
5094
5095 static int
5096 bpstat_check_location (const struct bp_location *bl,
5097 const address_space *aspace, CORE_ADDR bp_addr,
5098 const struct target_waitstatus *ws)
5099 {
5100 struct breakpoint *b = bl->owner;
5101
5102 /* BL is from an existing breakpoint. */
5103 gdb_assert (b != NULL);
5104
5105 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5106 }
5107
5108 /* Determine if the watched values have actually changed, and we
5109 should stop. If not, set BS->stop to 0. */
5110
5111 static void
5112 bpstat_check_watchpoint (bpstat bs)
5113 {
5114 const struct bp_location *bl;
5115 struct watchpoint *b;
5116
5117 /* BS is built for existing struct breakpoint. */
5118 bl = bs->bp_location_at.get ();
5119 gdb_assert (bl != NULL);
5120 b = (struct watchpoint *) bs->breakpoint_at;
5121 gdb_assert (b != NULL);
5122
5123 {
5124 int must_check_value = 0;
5125
5126 if (b->type == bp_watchpoint)
5127 /* For a software watchpoint, we must always check the
5128 watched value. */
5129 must_check_value = 1;
5130 else if (b->watchpoint_triggered == watch_triggered_yes)
5131 /* We have a hardware watchpoint (read, write, or access)
5132 and the target earlier reported an address watched by
5133 this watchpoint. */
5134 must_check_value = 1;
5135 else if (b->watchpoint_triggered == watch_triggered_unknown
5136 && b->type == bp_hardware_watchpoint)
5137 /* We were stopped by a hardware watchpoint, but the target could
5138 not report the data address. We must check the watchpoint's
5139 value. Access and read watchpoints are out of luck; without
5140 a data address, we can't figure it out. */
5141 must_check_value = 1;
5142
5143 if (must_check_value)
5144 {
5145 wp_check_result e;
5146
5147 try
5148 {
5149 e = watchpoint_check (bs);
5150 }
5151 catch (const gdb_exception &ex)
5152 {
5153 exception_fprintf (gdb_stderr, ex,
5154 "Error evaluating expression "
5155 "for watchpoint %d\n",
5156 b->number);
5157
5158 SWITCH_THRU_ALL_UIS ()
5159 {
5160 printf_filtered (_("Watchpoint %d deleted.\n"),
5161 b->number);
5162 }
5163 watchpoint_del_at_next_stop (b);
5164 e = WP_DELETED;
5165 }
5166
5167 switch (e)
5168 {
5169 case WP_DELETED:
5170 /* We've already printed what needs to be printed. */
5171 bs->print_it = print_it_done;
5172 /* Stop. */
5173 break;
5174 case WP_IGNORE:
5175 bs->print_it = print_it_noop;
5176 bs->stop = 0;
5177 break;
5178 case WP_VALUE_CHANGED:
5179 if (b->type == bp_read_watchpoint)
5180 {
5181 /* There are two cases to consider here:
5182
5183 1. We're watching the triggered memory for reads.
5184 In that case, trust the target, and always report
5185 the watchpoint hit to the user. Even though
5186 reads don't cause value changes, the value may
5187 have changed since the last time it was read, and
5188 since we're not trapping writes, we will not see
5189 those, and as such we should ignore our notion of
5190 old value.
5191
5192 2. We're watching the triggered memory for both
5193 reads and writes. There are two ways this may
5194 happen:
5195
5196 2.1. This is a target that can't break on data
5197 reads only, but can break on accesses (reads or
5198 writes), such as e.g., x86. We detect this case
5199 at the time we try to insert read watchpoints.
5200
5201 2.2. Otherwise, the target supports read
5202 watchpoints, but, the user set an access or write
5203 watchpoint watching the same memory as this read
5204 watchpoint.
5205
5206 If we're watching memory writes as well as reads,
5207 ignore watchpoint hits when we find that the
5208 value hasn't changed, as reads don't cause
5209 changes. This still gives false positives when
5210 the program writes the same value to memory as
5211 what there was already in memory (we will confuse
5212 it for a read), but it's much better than
5213 nothing. */
5214
5215 int other_write_watchpoint = 0;
5216
5217 if (bl->watchpoint_type == hw_read)
5218 {
5219 for (breakpoint *other_b : all_breakpoints ())
5220 if (other_b->type == bp_hardware_watchpoint
5221 || other_b->type == bp_access_watchpoint)
5222 {
5223 struct watchpoint *other_w =
5224 (struct watchpoint *) other_b;
5225
5226 if (other_w->watchpoint_triggered
5227 == watch_triggered_yes)
5228 {
5229 other_write_watchpoint = 1;
5230 break;
5231 }
5232 }
5233 }
5234
5235 if (other_write_watchpoint
5236 || bl->watchpoint_type == hw_access)
5237 {
5238 /* We're watching the same memory for writes,
5239 and the value changed since the last time we
5240 updated it, so this trap must be for a write.
5241 Ignore it. */
5242 bs->print_it = print_it_noop;
5243 bs->stop = 0;
5244 }
5245 }
5246 break;
5247 case WP_VALUE_NOT_CHANGED:
5248 if (b->type == bp_hardware_watchpoint
5249 || b->type == bp_watchpoint)
5250 {
5251 /* Don't stop: write watchpoints shouldn't fire if
5252 the value hasn't changed. */
5253 bs->print_it = print_it_noop;
5254 bs->stop = 0;
5255 }
5256 /* Stop. */
5257 break;
5258 default:
5259 /* Can't happen. */
5260 break;
5261 }
5262 }
5263 else /* must_check_value == 0 */
5264 {
5265 /* This is a case where some watchpoint(s) triggered, but
5266 not at the address of this watchpoint, or else no
5267 watchpoint triggered after all. So don't print
5268 anything for this watchpoint. */
5269 bs->print_it = print_it_noop;
5270 bs->stop = 0;
5271 }
5272 }
5273 }
5274
5275 /* For breakpoints that are currently marked as telling gdb to stop,
5276 check conditions (condition proper, frame, thread and ignore count)
5277 of breakpoint referred to by BS. If we should not stop for this
5278 breakpoint, set BS->stop to 0. */
5279
5280 static void
5281 bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
5282 {
5283 const struct bp_location *bl;
5284 struct breakpoint *b;
5285 /* Assume stop. */
5286 bool condition_result = true;
5287 struct expression *cond;
5288
5289 gdb_assert (bs->stop);
5290
5291 /* BS is built for existing struct breakpoint. */
5292 bl = bs->bp_location_at.get ();
5293 gdb_assert (bl != NULL);
5294 b = bs->breakpoint_at;
5295 gdb_assert (b != NULL);
5296
5297 /* Even if the target evaluated the condition on its end and notified GDB, we
5298 need to do so again since GDB does not know if we stopped due to a
5299 breakpoint or a single step breakpoint. */
5300
5301 if (frame_id_p (b->frame_id)
5302 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5303 {
5304 bs->stop = 0;
5305 return;
5306 }
5307
5308 /* If this is a thread/task-specific breakpoint, don't waste cpu
5309 evaluating the condition if this isn't the specified
5310 thread/task. */
5311 if ((b->thread != -1 && b->thread != thread->global_num)
5312 || (b->task != 0 && b->task != ada_get_task_number (thread)))
5313 {
5314 bs->stop = 0;
5315 return;
5316 }
5317
5318 /* Evaluate extension language breakpoints that have a "stop" method
5319 implemented. */
5320 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5321
5322 if (is_watchpoint (b))
5323 {
5324 struct watchpoint *w = (struct watchpoint *) b;
5325
5326 cond = w->cond_exp.get ();
5327 }
5328 else
5329 cond = bl->cond.get ();
5330
5331 if (cond && b->disposition != disp_del_at_next_stop)
5332 {
5333 int within_current_scope = 1;
5334 struct watchpoint * w;
5335
5336 /* We use value_mark and value_free_to_mark because it could
5337 be a long time before we return to the command level and
5338 call free_all_values. We can't call free_all_values
5339 because we might be in the middle of evaluating a
5340 function call. */
5341 struct value *mark = value_mark ();
5342
5343 if (is_watchpoint (b))
5344 w = (struct watchpoint *) b;
5345 else
5346 w = NULL;
5347
5348 /* Need to select the frame, with all that implies so that
5349 the conditions will have the right context. Because we
5350 use the frame, we will not see an inlined function's
5351 variables when we arrive at a breakpoint at the start
5352 of the inlined function; the current frame will be the
5353 call site. */
5354 if (w == NULL || w->cond_exp_valid_block == NULL)
5355 select_frame (get_current_frame ());
5356 else
5357 {
5358 struct frame_info *frame;
5359
5360 /* For local watchpoint expressions, which particular
5361 instance of a local is being watched matters, so we
5362 keep track of the frame to evaluate the expression
5363 in. To evaluate the condition however, it doesn't
5364 really matter which instantiation of the function
5365 where the condition makes sense triggers the
5366 watchpoint. This allows an expression like "watch
5367 global if q > 10" set in `func', catch writes to
5368 global on all threads that call `func', or catch
5369 writes on all recursive calls of `func' by a single
5370 thread. We simply always evaluate the condition in
5371 the innermost frame that's executing where it makes
5372 sense to evaluate the condition. It seems
5373 intuitive. */
5374 frame = block_innermost_frame (w->cond_exp_valid_block);
5375 if (frame != NULL)
5376 select_frame (frame);
5377 else
5378 within_current_scope = 0;
5379 }
5380 if (within_current_scope)
5381 {
5382 try
5383 {
5384 condition_result = breakpoint_cond_eval (cond);
5385 }
5386 catch (const gdb_exception &ex)
5387 {
5388 exception_fprintf (gdb_stderr, ex,
5389 "Error in testing breakpoint condition:\n");
5390 }
5391 }
5392 else
5393 {
5394 warning (_("Watchpoint condition cannot be tested "
5395 "in the current scope"));
5396 /* If we failed to set the right context for this
5397 watchpoint, unconditionally report it. */
5398 }
5399 /* FIXME-someday, should give breakpoint #. */
5400 value_free_to_mark (mark);
5401 }
5402
5403 if (cond && !condition_result)
5404 {
5405 bs->stop = 0;
5406 }
5407 else if (b->ignore_count > 0)
5408 {
5409 b->ignore_count--;
5410 bs->stop = 0;
5411 /* Increase the hit count even though we don't stop. */
5412 ++(b->hit_count);
5413 gdb::observers::breakpoint_modified.notify (b);
5414 }
5415 }
5416
5417 /* Returns true if we need to track moribund locations of LOC's type
5418 on the current target. */
5419
5420 static int
5421 need_moribund_for_location_type (struct bp_location *loc)
5422 {
5423 return ((loc->loc_type == bp_loc_software_breakpoint
5424 && !target_supports_stopped_by_sw_breakpoint ())
5425 || (loc->loc_type == bp_loc_hardware_breakpoint
5426 && !target_supports_stopped_by_hw_breakpoint ()));
5427 }
5428
5429 /* See breakpoint.h. */
5430
5431 bpstat
5432 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5433 const struct target_waitstatus *ws)
5434 {
5435 bpstat bs_head = NULL, *bs_link = &bs_head;
5436
5437 for (breakpoint *b : all_breakpoints ())
5438 {
5439 if (!breakpoint_enabled (b))
5440 continue;
5441
5442 for (bp_location *bl : b->locations ())
5443 {
5444 /* For hardware watchpoints, we look only at the first
5445 location. The watchpoint_check function will work on the
5446 entire expression, not the individual locations. For
5447 read watchpoints, the watchpoints_triggered function has
5448 checked all locations already. */
5449 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5450 break;
5451
5452 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
5453 continue;
5454
5455 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5456 continue;
5457
5458 /* Come here if it's a watchpoint, or if the break address
5459 matches. */
5460
5461 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5462 explain stop. */
5463
5464 /* Assume we stop. Should we find a watchpoint that is not
5465 actually triggered, or if the condition of the breakpoint
5466 evaluates as false, we'll reset 'stop' to 0. */
5467 bs->stop = 1;
5468 bs->print = 1;
5469
5470 /* If this is a scope breakpoint, mark the associated
5471 watchpoint as triggered so that we will handle the
5472 out-of-scope event. We'll get to the watchpoint next
5473 iteration. */
5474 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5475 {
5476 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5477
5478 w->watchpoint_triggered = watch_triggered_yes;
5479 }
5480 }
5481 }
5482
5483 /* Check if a moribund breakpoint explains the stop. */
5484 if (!target_supports_stopped_by_sw_breakpoint ()
5485 || !target_supports_stopped_by_hw_breakpoint ())
5486 {
5487 for (bp_location *loc : moribund_locations)
5488 {
5489 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5490 && need_moribund_for_location_type (loc))
5491 {
5492 bpstat bs = new bpstats (loc, &bs_link);
5493 /* For hits of moribund locations, we should just proceed. */
5494 bs->stop = 0;
5495 bs->print = 0;
5496 bs->print_it = print_it_noop;
5497 }
5498 }
5499 }
5500
5501 return bs_head;
5502 }
5503
5504 /* See breakpoint.h. */
5505
5506 bpstat
5507 bpstat_stop_status (const address_space *aspace,
5508 CORE_ADDR bp_addr, thread_info *thread,
5509 const struct target_waitstatus *ws,
5510 bpstat stop_chain)
5511 {
5512 struct breakpoint *b = NULL;
5513 /* First item of allocated bpstat's. */
5514 bpstat bs_head = stop_chain;
5515 bpstat bs;
5516 int need_remove_insert;
5517 int removed_any;
5518
5519 /* First, build the bpstat chain with locations that explain a
5520 target stop, while being careful to not set the target running,
5521 as that may invalidate locations (in particular watchpoint
5522 locations are recreated). Resuming will happen here with
5523 breakpoint conditions or watchpoint expressions that include
5524 inferior function calls. */
5525 if (bs_head == NULL)
5526 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5527
5528 /* A bit of special processing for shlib breakpoints. We need to
5529 process solib loading here, so that the lists of loaded and
5530 unloaded libraries are correct before we handle "catch load" and
5531 "catch unload". */
5532 for (bs = bs_head; bs != NULL; bs = bs->next)
5533 {
5534 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5535 {
5536 handle_solib_event ();
5537 break;
5538 }
5539 }
5540
5541 /* Now go through the locations that caused the target to stop, and
5542 check whether we're interested in reporting this stop to higher
5543 layers, or whether we should resume the target transparently. */
5544
5545 removed_any = 0;
5546
5547 for (bs = bs_head; bs != NULL; bs = bs->next)
5548 {
5549 if (!bs->stop)
5550 continue;
5551
5552 b = bs->breakpoint_at;
5553 b->ops->check_status (bs);
5554 if (bs->stop)
5555 {
5556 bpstat_check_breakpoint_conditions (bs, thread);
5557
5558 if (bs->stop)
5559 {
5560 ++(b->hit_count);
5561 gdb::observers::breakpoint_modified.notify (b);
5562
5563 /* We will stop here. */
5564 if (b->disposition == disp_disable)
5565 {
5566 --(b->enable_count);
5567 if (b->enable_count <= 0)
5568 b->enable_state = bp_disabled;
5569 removed_any = 1;
5570 }
5571 if (b->silent)
5572 bs->print = 0;
5573 bs->commands = b->commands;
5574 if (command_line_is_silent (bs->commands
5575 ? bs->commands.get () : NULL))
5576 bs->print = 0;
5577
5578 b->ops->after_condition_true (bs);
5579 }
5580
5581 }
5582
5583 /* Print nothing for this entry if we don't stop or don't
5584 print. */
5585 if (!bs->stop || !bs->print)
5586 bs->print_it = print_it_noop;
5587 }
5588
5589 /* If we aren't stopping, the value of some hardware watchpoint may
5590 not have changed, but the intermediate memory locations we are
5591 watching may have. Don't bother if we're stopping; this will get
5592 done later. */
5593 need_remove_insert = 0;
5594 if (! bpstat_causes_stop (bs_head))
5595 for (bs = bs_head; bs != NULL; bs = bs->next)
5596 if (!bs->stop
5597 && bs->breakpoint_at
5598 && is_hardware_watchpoint (bs->breakpoint_at))
5599 {
5600 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5601
5602 update_watchpoint (w, 0 /* don't reparse. */);
5603 need_remove_insert = 1;
5604 }
5605
5606 if (need_remove_insert)
5607 update_global_location_list (UGLL_MAY_INSERT);
5608 else if (removed_any)
5609 update_global_location_list (UGLL_DONT_INSERT);
5610
5611 return bs_head;
5612 }
5613
5614 static void
5615 handle_jit_event (CORE_ADDR address)
5616 {
5617 struct gdbarch *gdbarch;
5618
5619 infrun_debug_printf ("handling bp_jit_event");
5620
5621 /* Switch terminal for any messages produced by
5622 breakpoint_re_set. */
5623 target_terminal::ours_for_output ();
5624
5625 gdbarch = get_frame_arch (get_current_frame ());
5626 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5627 thus it is expected that its objectfile can be found through
5628 minimal symbol lookup. If it doesn't work (and assert fails), it
5629 most likely means that `jit_breakpoint_re_set` was changes and this
5630 function needs to be updated too. */
5631 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5632 gdb_assert (jit_bp_sym.objfile != nullptr);
5633 jit_event_handler (gdbarch, jit_bp_sym.objfile);
5634
5635 target_terminal::inferior ();
5636 }
5637
5638 /* Prepare WHAT final decision for infrun. */
5639
5640 /* Decide what infrun needs to do with this bpstat. */
5641
5642 struct bpstat_what
5643 bpstat_what (bpstat bs_head)
5644 {
5645 struct bpstat_what retval;
5646 bpstat bs;
5647
5648 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5649 retval.call_dummy = STOP_NONE;
5650 retval.is_longjmp = false;
5651
5652 for (bs = bs_head; bs != NULL; bs = bs->next)
5653 {
5654 /* Extract this BS's action. After processing each BS, we check
5655 if its action overrides all we've seem so far. */
5656 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5657 enum bptype bptype;
5658
5659 if (bs->breakpoint_at == NULL)
5660 {
5661 /* I suspect this can happen if it was a momentary
5662 breakpoint which has since been deleted. */
5663 bptype = bp_none;
5664 }
5665 else
5666 bptype = bs->breakpoint_at->type;
5667
5668 switch (bptype)
5669 {
5670 case bp_none:
5671 break;
5672 case bp_breakpoint:
5673 case bp_hardware_breakpoint:
5674 case bp_single_step:
5675 case bp_until:
5676 case bp_finish:
5677 case bp_shlib_event:
5678 if (bs->stop)
5679 {
5680 if (bs->print)
5681 this_action = BPSTAT_WHAT_STOP_NOISY;
5682 else
5683 this_action = BPSTAT_WHAT_STOP_SILENT;
5684 }
5685 else
5686 this_action = BPSTAT_WHAT_SINGLE;
5687 break;
5688 case bp_watchpoint:
5689 case bp_hardware_watchpoint:
5690 case bp_read_watchpoint:
5691 case bp_access_watchpoint:
5692 if (bs->stop)
5693 {
5694 if (bs->print)
5695 this_action = BPSTAT_WHAT_STOP_NOISY;
5696 else
5697 this_action = BPSTAT_WHAT_STOP_SILENT;
5698 }
5699 else
5700 {
5701 /* There was a watchpoint, but we're not stopping.
5702 This requires no further action. */
5703 }
5704 break;
5705 case bp_longjmp:
5706 case bp_longjmp_call_dummy:
5707 case bp_exception:
5708 if (bs->stop)
5709 {
5710 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5711 retval.is_longjmp = bptype != bp_exception;
5712 }
5713 else
5714 this_action = BPSTAT_WHAT_SINGLE;
5715 break;
5716 case bp_longjmp_resume:
5717 case bp_exception_resume:
5718 if (bs->stop)
5719 {
5720 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5721 retval.is_longjmp = bptype == bp_longjmp_resume;
5722 }
5723 else
5724 this_action = BPSTAT_WHAT_SINGLE;
5725 break;
5726 case bp_step_resume:
5727 if (bs->stop)
5728 this_action = BPSTAT_WHAT_STEP_RESUME;
5729 else
5730 {
5731 /* It is for the wrong frame. */
5732 this_action = BPSTAT_WHAT_SINGLE;
5733 }
5734 break;
5735 case bp_hp_step_resume:
5736 if (bs->stop)
5737 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5738 else
5739 {
5740 /* It is for the wrong frame. */
5741 this_action = BPSTAT_WHAT_SINGLE;
5742 }
5743 break;
5744 case bp_watchpoint_scope:
5745 case bp_thread_event:
5746 case bp_overlay_event:
5747 case bp_longjmp_master:
5748 case bp_std_terminate_master:
5749 case bp_exception_master:
5750 this_action = BPSTAT_WHAT_SINGLE;
5751 break;
5752 case bp_catchpoint:
5753 if (bs->stop)
5754 {
5755 if (bs->print)
5756 this_action = BPSTAT_WHAT_STOP_NOISY;
5757 else
5758 this_action = BPSTAT_WHAT_STOP_SILENT;
5759 }
5760 else
5761 {
5762 /* Some catchpoints are implemented with breakpoints.
5763 For those, we need to step over the breakpoint. */
5764 if (bs->bp_location_at->loc_type != bp_loc_other)
5765 this_action = BPSTAT_WHAT_SINGLE;
5766 }
5767 break;
5768 case bp_jit_event:
5769 this_action = BPSTAT_WHAT_SINGLE;
5770 break;
5771 case bp_call_dummy:
5772 /* Make sure the action is stop (silent or noisy),
5773 so infrun.c pops the dummy frame. */
5774 retval.call_dummy = STOP_STACK_DUMMY;
5775 this_action = BPSTAT_WHAT_STOP_SILENT;
5776 break;
5777 case bp_std_terminate:
5778 /* Make sure the action is stop (silent or noisy),
5779 so infrun.c pops the dummy frame. */
5780 retval.call_dummy = STOP_STD_TERMINATE;
5781 this_action = BPSTAT_WHAT_STOP_SILENT;
5782 break;
5783 case bp_tracepoint:
5784 case bp_fast_tracepoint:
5785 case bp_static_tracepoint:
5786 /* Tracepoint hits should not be reported back to GDB, and
5787 if one got through somehow, it should have been filtered
5788 out already. */
5789 internal_error (__FILE__, __LINE__,
5790 _("bpstat_what: tracepoint encountered"));
5791 break;
5792 case bp_gnu_ifunc_resolver:
5793 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5794 this_action = BPSTAT_WHAT_SINGLE;
5795 break;
5796 case bp_gnu_ifunc_resolver_return:
5797 /* The breakpoint will be removed, execution will restart from the
5798 PC of the former breakpoint. */
5799 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5800 break;
5801
5802 case bp_dprintf:
5803 if (bs->stop)
5804 this_action = BPSTAT_WHAT_STOP_SILENT;
5805 else
5806 this_action = BPSTAT_WHAT_SINGLE;
5807 break;
5808
5809 default:
5810 internal_error (__FILE__, __LINE__,
5811 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5812 }
5813
5814 retval.main_action = std::max (retval.main_action, this_action);
5815 }
5816
5817 return retval;
5818 }
5819
5820 void
5821 bpstat_run_callbacks (bpstat bs_head)
5822 {
5823 bpstat bs;
5824
5825 for (bs = bs_head; bs != NULL; bs = bs->next)
5826 {
5827 struct breakpoint *b = bs->breakpoint_at;
5828
5829 if (b == NULL)
5830 continue;
5831 switch (b->type)
5832 {
5833 case bp_jit_event:
5834 handle_jit_event (bs->bp_location_at->address);
5835 break;
5836 case bp_gnu_ifunc_resolver:
5837 gnu_ifunc_resolver_stop (b);
5838 break;
5839 case bp_gnu_ifunc_resolver_return:
5840 gnu_ifunc_resolver_return_stop (b);
5841 break;
5842 }
5843 }
5844 }
5845
5846 /* See breakpoint.h. */
5847
5848 bool
5849 bpstat_should_step ()
5850 {
5851 for (breakpoint *b : all_breakpoints ())
5852 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5853 return true;
5854
5855 return false;
5856 }
5857
5858 /* See breakpoint.h. */
5859
5860 bool
5861 bpstat_causes_stop (bpstat bs)
5862 {
5863 for (; bs != NULL; bs = bs->next)
5864 if (bs->stop)
5865 return true;
5866
5867 return false;
5868 }
5869
5870 \f
5871
5872 /* Compute a string of spaces suitable to indent the next line
5873 so it starts at the position corresponding to the table column
5874 named COL_NAME in the currently active table of UIOUT. */
5875
5876 static char *
5877 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5878 {
5879 static char wrap_indent[80];
5880 int i, total_width, width, align;
5881 const char *text;
5882
5883 total_width = 0;
5884 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5885 {
5886 if (strcmp (text, col_name) == 0)
5887 {
5888 gdb_assert (total_width < sizeof wrap_indent);
5889 memset (wrap_indent, ' ', total_width);
5890 wrap_indent[total_width] = 0;
5891
5892 return wrap_indent;
5893 }
5894
5895 total_width += width + 1;
5896 }
5897
5898 return NULL;
5899 }
5900
5901 /* Determine if the locations of this breakpoint will have their conditions
5902 evaluated by the target, host or a mix of both. Returns the following:
5903
5904 "host": Host evals condition.
5905 "host or target": Host or Target evals condition.
5906 "target": Target evals condition.
5907 */
5908
5909 static const char *
5910 bp_condition_evaluator (struct breakpoint *b)
5911 {
5912 char host_evals = 0;
5913 char target_evals = 0;
5914
5915 if (!b)
5916 return NULL;
5917
5918 if (!is_breakpoint (b))
5919 return NULL;
5920
5921 if (gdb_evaluates_breakpoint_condition_p ()
5922 || !target_supports_evaluation_of_breakpoint_conditions ())
5923 return condition_evaluation_host;
5924
5925 for (bp_location *bl : b->locations ())
5926 {
5927 if (bl->cond_bytecode)
5928 target_evals++;
5929 else
5930 host_evals++;
5931 }
5932
5933 if (host_evals && target_evals)
5934 return condition_evaluation_both;
5935 else if (target_evals)
5936 return condition_evaluation_target;
5937 else
5938 return condition_evaluation_host;
5939 }
5940
5941 /* Determine the breakpoint location's condition evaluator. This is
5942 similar to bp_condition_evaluator, but for locations. */
5943
5944 static const char *
5945 bp_location_condition_evaluator (struct bp_location *bl)
5946 {
5947 if (bl && !is_breakpoint (bl->owner))
5948 return NULL;
5949
5950 if (gdb_evaluates_breakpoint_condition_p ()
5951 || !target_supports_evaluation_of_breakpoint_conditions ())
5952 return condition_evaluation_host;
5953
5954 if (bl && bl->cond_bytecode)
5955 return condition_evaluation_target;
5956 else
5957 return condition_evaluation_host;
5958 }
5959
5960 /* Print the LOC location out of the list of B->LOC locations. */
5961
5962 static void
5963 print_breakpoint_location (struct breakpoint *b,
5964 struct bp_location *loc)
5965 {
5966 struct ui_out *uiout = current_uiout;
5967
5968 scoped_restore_current_program_space restore_pspace;
5969
5970 if (loc != NULL && loc->shlib_disabled)
5971 loc = NULL;
5972
5973 if (loc != NULL)
5974 set_current_program_space (loc->pspace);
5975
5976 if (b->display_canonical)
5977 uiout->field_string ("what", event_location_to_string (b->location.get ()));
5978 else if (loc && loc->symtab)
5979 {
5980 const struct symbol *sym = loc->symbol;
5981
5982 if (sym)
5983 {
5984 uiout->text ("in ");
5985 uiout->field_string ("func", sym->print_name (),
5986 function_name_style.style ());
5987 uiout->text (" ");
5988 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5989 uiout->text ("at ");
5990 }
5991 uiout->field_string ("file",
5992 symtab_to_filename_for_display (loc->symtab),
5993 file_name_style.style ());
5994 uiout->text (":");
5995
5996 if (uiout->is_mi_like_p ())
5997 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
5998
5999 uiout->field_signed ("line", loc->line_number);
6000 }
6001 else if (loc)
6002 {
6003 string_file stb;
6004
6005 print_address_symbolic (loc->gdbarch, loc->address, &stb,
6006 demangle, "");
6007 uiout->field_stream ("at", stb);
6008 }
6009 else
6010 {
6011 uiout->field_string ("pending",
6012 event_location_to_string (b->location.get ()));
6013 /* If extra_string is available, it could be holding a condition
6014 or dprintf arguments. In either case, make sure it is printed,
6015 too, but only for non-MI streams. */
6016 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
6017 {
6018 if (b->type == bp_dprintf)
6019 uiout->text (",");
6020 else
6021 uiout->text (" ");
6022 uiout->text (b->extra_string);
6023 }
6024 }
6025
6026 if (loc && is_breakpoint (b)
6027 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6028 && bp_condition_evaluator (b) == condition_evaluation_both)
6029 {
6030 uiout->text (" (");
6031 uiout->field_string ("evaluated-by",
6032 bp_location_condition_evaluator (loc));
6033 uiout->text (")");
6034 }
6035 }
6036
6037 static const char *
6038 bptype_string (enum bptype type)
6039 {
6040 struct ep_type_description
6041 {
6042 enum bptype type;
6043 const char *description;
6044 };
6045 static struct ep_type_description bptypes[] =
6046 {
6047 {bp_none, "?deleted?"},
6048 {bp_breakpoint, "breakpoint"},
6049 {bp_hardware_breakpoint, "hw breakpoint"},
6050 {bp_single_step, "sw single-step"},
6051 {bp_until, "until"},
6052 {bp_finish, "finish"},
6053 {bp_watchpoint, "watchpoint"},
6054 {bp_hardware_watchpoint, "hw watchpoint"},
6055 {bp_read_watchpoint, "read watchpoint"},
6056 {bp_access_watchpoint, "acc watchpoint"},
6057 {bp_longjmp, "longjmp"},
6058 {bp_longjmp_resume, "longjmp resume"},
6059 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6060 {bp_exception, "exception"},
6061 {bp_exception_resume, "exception resume"},
6062 {bp_step_resume, "step resume"},
6063 {bp_hp_step_resume, "high-priority step resume"},
6064 {bp_watchpoint_scope, "watchpoint scope"},
6065 {bp_call_dummy, "call dummy"},
6066 {bp_std_terminate, "std::terminate"},
6067 {bp_shlib_event, "shlib events"},
6068 {bp_thread_event, "thread events"},
6069 {bp_overlay_event, "overlay events"},
6070 {bp_longjmp_master, "longjmp master"},
6071 {bp_std_terminate_master, "std::terminate master"},
6072 {bp_exception_master, "exception master"},
6073 {bp_catchpoint, "catchpoint"},
6074 {bp_tracepoint, "tracepoint"},
6075 {bp_fast_tracepoint, "fast tracepoint"},
6076 {bp_static_tracepoint, "static tracepoint"},
6077 {bp_dprintf, "dprintf"},
6078 {bp_jit_event, "jit events"},
6079 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6080 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6081 };
6082
6083 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6084 || ((int) type != bptypes[(int) type].type))
6085 internal_error (__FILE__, __LINE__,
6086 _("bptypes table does not describe type #%d."),
6087 (int) type);
6088
6089 return bptypes[(int) type].description;
6090 }
6091
6092 /* For MI, output a field named 'thread-groups' with a list as the value.
6093 For CLI, prefix the list with the string 'inf'. */
6094
6095 static void
6096 output_thread_groups (struct ui_out *uiout,
6097 const char *field_name,
6098 const std::vector<int> &inf_nums,
6099 int mi_only)
6100 {
6101 int is_mi = uiout->is_mi_like_p ();
6102
6103 /* For backward compatibility, don't display inferiors in CLI unless
6104 there are several. Always display them for MI. */
6105 if (!is_mi && mi_only)
6106 return;
6107
6108 ui_out_emit_list list_emitter (uiout, field_name);
6109
6110 for (size_t i = 0; i < inf_nums.size (); i++)
6111 {
6112 if (is_mi)
6113 {
6114 char mi_group[10];
6115
6116 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
6117 uiout->field_string (NULL, mi_group);
6118 }
6119 else
6120 {
6121 if (i == 0)
6122 uiout->text (" inf ");
6123 else
6124 uiout->text (", ");
6125
6126 uiout->text (plongest (inf_nums[i]));
6127 }
6128 }
6129 }
6130
6131 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6132 instead of going via breakpoint_ops::print_one. This makes "maint
6133 info breakpoints" show the software breakpoint locations of
6134 catchpoints, which are considered internal implementation
6135 detail. */
6136
6137 static void
6138 print_one_breakpoint_location (struct breakpoint *b,
6139 struct bp_location *loc,
6140 int loc_number,
6141 struct bp_location **last_loc,
6142 int allflag, bool raw_loc)
6143 {
6144 struct command_line *l;
6145 static char bpenables[] = "nynny";
6146
6147 struct ui_out *uiout = current_uiout;
6148 int header_of_multiple = 0;
6149 int part_of_multiple = (loc != NULL);
6150 struct value_print_options opts;
6151
6152 get_user_print_options (&opts);
6153
6154 gdb_assert (!loc || loc_number != 0);
6155 /* See comment in print_one_breakpoint concerning treatment of
6156 breakpoints with single disabled location. */
6157 if (loc == NULL
6158 && (b->loc != NULL
6159 && (b->loc->next != NULL
6160 || !b->loc->enabled || b->loc->disabled_by_cond)))
6161 header_of_multiple = 1;
6162 if (loc == NULL)
6163 loc = b->loc;
6164
6165 annotate_record ();
6166
6167 /* 1 */
6168 annotate_field (0);
6169 if (part_of_multiple)
6170 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6171 else
6172 uiout->field_signed ("number", b->number);
6173
6174 /* 2 */
6175 annotate_field (1);
6176 if (part_of_multiple)
6177 uiout->field_skip ("type");
6178 else
6179 uiout->field_string ("type", bptype_string (b->type));
6180
6181 /* 3 */
6182 annotate_field (2);
6183 if (part_of_multiple)
6184 uiout->field_skip ("disp");
6185 else
6186 uiout->field_string ("disp", bpdisp_text (b->disposition));
6187
6188 /* 4 */
6189 annotate_field (3);
6190 /* For locations that are disabled because of an invalid condition,
6191 display "N*" on CLI, where "*" refers to a footnote below the
6192 table. For MI, simply display a "N" without a footnote. */
6193 const char *N = (uiout->is_mi_like_p ()) ? "N" : "N*";
6194 if (part_of_multiple)
6195 uiout->field_string ("enabled", (loc->disabled_by_cond ? N
6196 : (loc->enabled ? "y" : "n")));
6197 else
6198 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6199
6200 /* 5 and 6 */
6201 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
6202 b->ops->print_one (b, last_loc);
6203 else
6204 {
6205 if (is_watchpoint (b))
6206 {
6207 struct watchpoint *w = (struct watchpoint *) b;
6208
6209 /* Field 4, the address, is omitted (which makes the columns
6210 not line up too nicely with the headers, but the effect
6211 is relatively readable). */
6212 if (opts.addressprint)
6213 uiout->field_skip ("addr");
6214 annotate_field (5);
6215 uiout->field_string ("what", w->exp_string);
6216 }
6217 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6218 || is_ada_exception_catchpoint (b))
6219 {
6220 if (opts.addressprint)
6221 {
6222 annotate_field (4);
6223 if (header_of_multiple)
6224 uiout->field_string ("addr", "<MULTIPLE>",
6225 metadata_style.style ());
6226 else if (b->loc == NULL || loc->shlib_disabled)
6227 uiout->field_string ("addr", "<PENDING>",
6228 metadata_style.style ());
6229 else
6230 uiout->field_core_addr ("addr",
6231 loc->gdbarch, loc->address);
6232 }
6233 annotate_field (5);
6234 if (!header_of_multiple)
6235 print_breakpoint_location (b, loc);
6236 if (b->loc)
6237 *last_loc = b->loc;
6238 }
6239 }
6240
6241 if (loc != NULL && !header_of_multiple)
6242 {
6243 std::vector<int> inf_nums;
6244 int mi_only = 1;
6245
6246 for (inferior *inf : all_inferiors ())
6247 {
6248 if (inf->pspace == loc->pspace)
6249 inf_nums.push_back (inf->num);
6250 }
6251
6252 /* For backward compatibility, don't display inferiors in CLI unless
6253 there are several. Always display for MI. */
6254 if (allflag
6255 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6256 && (program_spaces.size () > 1
6257 || number_of_inferiors () > 1)
6258 /* LOC is for existing B, it cannot be in
6259 moribund_locations and thus having NULL OWNER. */
6260 && loc->owner->type != bp_catchpoint))
6261 mi_only = 0;
6262 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6263 }
6264
6265 if (!part_of_multiple)
6266 {
6267 if (b->thread != -1)
6268 {
6269 /* FIXME: This seems to be redundant and lost here; see the
6270 "stop only in" line a little further down. */
6271 uiout->text (" thread ");
6272 uiout->field_signed ("thread", b->thread);
6273 }
6274 else if (b->task != 0)
6275 {
6276 uiout->text (" task ");
6277 uiout->field_signed ("task", b->task);
6278 }
6279 }
6280
6281 uiout->text ("\n");
6282
6283 if (!part_of_multiple)
6284 b->ops->print_one_detail (b, uiout);
6285
6286 if (part_of_multiple && frame_id_p (b->frame_id))
6287 {
6288 annotate_field (6);
6289 uiout->text ("\tstop only in stack frame at ");
6290 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6291 the frame ID. */
6292 uiout->field_core_addr ("frame",
6293 b->gdbarch, b->frame_id.stack_addr);
6294 uiout->text ("\n");
6295 }
6296
6297 if (!part_of_multiple && b->cond_string)
6298 {
6299 annotate_field (7);
6300 if (is_tracepoint (b))
6301 uiout->text ("\ttrace only if ");
6302 else
6303 uiout->text ("\tstop only if ");
6304 uiout->field_string ("cond", b->cond_string);
6305
6306 /* Print whether the target is doing the breakpoint's condition
6307 evaluation. If GDB is doing the evaluation, don't print anything. */
6308 if (is_breakpoint (b)
6309 && breakpoint_condition_evaluation_mode ()
6310 == condition_evaluation_target)
6311 {
6312 uiout->message (" (%pF evals)",
6313 string_field ("evaluated-by",
6314 bp_condition_evaluator (b)));
6315 }
6316 uiout->text ("\n");
6317 }
6318
6319 if (!part_of_multiple && b->thread != -1)
6320 {
6321 /* FIXME should make an annotation for this. */
6322 uiout->text ("\tstop only in thread ");
6323 if (uiout->is_mi_like_p ())
6324 uiout->field_signed ("thread", b->thread);
6325 else
6326 {
6327 struct thread_info *thr = find_thread_global_id (b->thread);
6328
6329 uiout->field_string ("thread", print_thread_id (thr));
6330 }
6331 uiout->text ("\n");
6332 }
6333
6334 if (!part_of_multiple)
6335 {
6336 if (b->hit_count)
6337 {
6338 /* FIXME should make an annotation for this. */
6339 if (is_catchpoint (b))
6340 uiout->text ("\tcatchpoint");
6341 else if (is_tracepoint (b))
6342 uiout->text ("\ttracepoint");
6343 else
6344 uiout->text ("\tbreakpoint");
6345 uiout->text (" already hit ");
6346 uiout->field_signed ("times", b->hit_count);
6347 if (b->hit_count == 1)
6348 uiout->text (" time\n");
6349 else
6350 uiout->text (" times\n");
6351 }
6352 else
6353 {
6354 /* Output the count also if it is zero, but only if this is mi. */
6355 if (uiout->is_mi_like_p ())
6356 uiout->field_signed ("times", b->hit_count);
6357 }
6358 }
6359
6360 if (!part_of_multiple && b->ignore_count)
6361 {
6362 annotate_field (8);
6363 uiout->message ("\tignore next %pF hits\n",
6364 signed_field ("ignore", b->ignore_count));
6365 }
6366
6367 /* Note that an enable count of 1 corresponds to "enable once"
6368 behavior, which is reported by the combination of enablement and
6369 disposition, so we don't need to mention it here. */
6370 if (!part_of_multiple && b->enable_count > 1)
6371 {
6372 annotate_field (8);
6373 uiout->text ("\tdisable after ");
6374 /* Tweak the wording to clarify that ignore and enable counts
6375 are distinct, and have additive effect. */
6376 if (b->ignore_count)
6377 uiout->text ("additional ");
6378 else
6379 uiout->text ("next ");
6380 uiout->field_signed ("enable", b->enable_count);
6381 uiout->text (" hits\n");
6382 }
6383
6384 if (!part_of_multiple && is_tracepoint (b))
6385 {
6386 struct tracepoint *tp = (struct tracepoint *) b;
6387
6388 if (tp->traceframe_usage)
6389 {
6390 uiout->text ("\ttrace buffer usage ");
6391 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6392 uiout->text (" bytes\n");
6393 }
6394 }
6395
6396 l = b->commands ? b->commands.get () : NULL;
6397 if (!part_of_multiple && l)
6398 {
6399 annotate_field (9);
6400 ui_out_emit_tuple tuple_emitter (uiout, "script");
6401 print_command_lines (uiout, l, 4);
6402 }
6403
6404 if (is_tracepoint (b))
6405 {
6406 struct tracepoint *t = (struct tracepoint *) b;
6407
6408 if (!part_of_multiple && t->pass_count)
6409 {
6410 annotate_field (10);
6411 uiout->text ("\tpass count ");
6412 uiout->field_signed ("pass", t->pass_count);
6413 uiout->text (" \n");
6414 }
6415
6416 /* Don't display it when tracepoint or tracepoint location is
6417 pending. */
6418 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6419 {
6420 annotate_field (11);
6421
6422 if (uiout->is_mi_like_p ())
6423 uiout->field_string ("installed",
6424 loc->inserted ? "y" : "n");
6425 else
6426 {
6427 if (loc->inserted)
6428 uiout->text ("\t");
6429 else
6430 uiout->text ("\tnot ");
6431 uiout->text ("installed on target\n");
6432 }
6433 }
6434 }
6435
6436 if (uiout->is_mi_like_p () && !part_of_multiple)
6437 {
6438 if (is_watchpoint (b))
6439 {
6440 struct watchpoint *w = (struct watchpoint *) b;
6441
6442 uiout->field_string ("original-location", w->exp_string);
6443 }
6444 else if (b->location != NULL
6445 && event_location_to_string (b->location.get ()) != NULL)
6446 uiout->field_string ("original-location",
6447 event_location_to_string (b->location.get ()));
6448 }
6449 }
6450
6451 /* See breakpoint.h. */
6452
6453 bool fix_multi_location_breakpoint_output_globally = false;
6454
6455 static void
6456 print_one_breakpoint (struct breakpoint *b,
6457 struct bp_location **last_loc,
6458 int allflag)
6459 {
6460 struct ui_out *uiout = current_uiout;
6461 bool use_fixed_output
6462 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6463 || fix_multi_location_breakpoint_output_globally);
6464
6465 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6466 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
6467
6468 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6469 are outside. */
6470 if (!use_fixed_output)
6471 bkpt_tuple_emitter.reset ();
6472
6473 /* If this breakpoint has custom print function,
6474 it's already printed. Otherwise, print individual
6475 locations, if any. */
6476 if (b->ops == NULL
6477 || b->ops->print_one == NULL
6478 || allflag)
6479 {
6480 /* If breakpoint has a single location that is disabled, we
6481 print it as if it had several locations, since otherwise it's
6482 hard to represent "breakpoint enabled, location disabled"
6483 situation.
6484
6485 Note that while hardware watchpoints have several locations
6486 internally, that's not a property exposed to users.
6487
6488 Likewise, while catchpoints may be implemented with
6489 breakpoints (e.g., catch throw), that's not a property
6490 exposed to users. We do however display the internal
6491 breakpoint locations with "maint info breakpoints". */
6492 if (!is_hardware_watchpoint (b)
6493 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6494 || is_ada_exception_catchpoint (b))
6495 && (allflag
6496 || (b->loc && (b->loc->next
6497 || !b->loc->enabled
6498 || b->loc->disabled_by_cond))))
6499 {
6500 gdb::optional<ui_out_emit_list> locations_list;
6501
6502 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6503 MI record. For later versions, place breakpoint locations in a
6504 list. */
6505 if (uiout->is_mi_like_p () && use_fixed_output)
6506 locations_list.emplace (uiout, "locations");
6507
6508 int n = 1;
6509 for (bp_location *loc : b->locations ())
6510 {
6511 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6512 print_one_breakpoint_location (b, loc, n, last_loc,
6513 allflag, allflag);
6514 n++;
6515 }
6516 }
6517 }
6518 }
6519
6520 static int
6521 breakpoint_address_bits (struct breakpoint *b)
6522 {
6523 int print_address_bits = 0;
6524
6525 /* Software watchpoints that aren't watching memory don't have an
6526 address to print. */
6527 if (is_no_memory_software_watchpoint (b))
6528 return 0;
6529
6530 for (bp_location *loc : b->locations ())
6531 {
6532 int addr_bit;
6533
6534 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6535 if (addr_bit > print_address_bits)
6536 print_address_bits = addr_bit;
6537 }
6538
6539 return print_address_bits;
6540 }
6541
6542 /* See breakpoint.h. */
6543
6544 void
6545 print_breakpoint (breakpoint *b)
6546 {
6547 struct bp_location *dummy_loc = NULL;
6548 print_one_breakpoint (b, &dummy_loc, 0);
6549 }
6550
6551 /* Return true if this breakpoint was set by the user, false if it is
6552 internal or momentary. */
6553
6554 int
6555 user_breakpoint_p (struct breakpoint *b)
6556 {
6557 return b->number > 0;
6558 }
6559
6560 /* See breakpoint.h. */
6561
6562 int
6563 pending_breakpoint_p (struct breakpoint *b)
6564 {
6565 return b->loc == NULL;
6566 }
6567
6568 /* Print information on breakpoints (including watchpoints and tracepoints).
6569
6570 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6571 understood by number_or_range_parser. Only breakpoints included in this
6572 list are then printed.
6573
6574 If SHOW_INTERNAL is true, print internal breakpoints.
6575
6576 If FILTER is non-NULL, call it on each breakpoint and only include the
6577 ones for which it returns true.
6578
6579 Return the total number of breakpoints listed. */
6580
6581 static int
6582 breakpoint_1 (const char *bp_num_list, bool show_internal,
6583 bool (*filter) (const struct breakpoint *))
6584 {
6585 struct bp_location *last_loc = NULL;
6586 int nr_printable_breakpoints;
6587 struct value_print_options opts;
6588 int print_address_bits = 0;
6589 int print_type_col_width = 14;
6590 struct ui_out *uiout = current_uiout;
6591 bool has_disabled_by_cond_location = false;
6592
6593 get_user_print_options (&opts);
6594
6595 /* Compute the number of rows in the table, as well as the size
6596 required for address fields. */
6597 nr_printable_breakpoints = 0;
6598 for (breakpoint *b : all_breakpoints ())
6599 {
6600 /* If we have a filter, only list the breakpoints it accepts. */
6601 if (filter && !filter (b))
6602 continue;
6603
6604 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6605 accept. Skip the others. */
6606 if (bp_num_list != NULL && *bp_num_list != '\0')
6607 {
6608 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6609 continue;
6610 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6611 continue;
6612 }
6613
6614 if (show_internal || user_breakpoint_p (b))
6615 {
6616 int addr_bit, type_len;
6617
6618 addr_bit = breakpoint_address_bits (b);
6619 if (addr_bit > print_address_bits)
6620 print_address_bits = addr_bit;
6621
6622 type_len = strlen (bptype_string (b->type));
6623 if (type_len > print_type_col_width)
6624 print_type_col_width = type_len;
6625
6626 nr_printable_breakpoints++;
6627 }
6628 }
6629
6630 {
6631 ui_out_emit_table table_emitter (uiout,
6632 opts.addressprint ? 6 : 5,
6633 nr_printable_breakpoints,
6634 "BreakpointTable");
6635
6636 if (nr_printable_breakpoints > 0)
6637 annotate_breakpoints_headers ();
6638 if (nr_printable_breakpoints > 0)
6639 annotate_field (0);
6640 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6641 if (nr_printable_breakpoints > 0)
6642 annotate_field (1);
6643 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6644 if (nr_printable_breakpoints > 0)
6645 annotate_field (2);
6646 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6647 if (nr_printable_breakpoints > 0)
6648 annotate_field (3);
6649 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6650 if (opts.addressprint)
6651 {
6652 if (nr_printable_breakpoints > 0)
6653 annotate_field (4);
6654 if (print_address_bits <= 32)
6655 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6656 else
6657 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6658 }
6659 if (nr_printable_breakpoints > 0)
6660 annotate_field (5);
6661 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6662 uiout->table_body ();
6663 if (nr_printable_breakpoints > 0)
6664 annotate_breakpoints_table ();
6665
6666 for (breakpoint *b : all_breakpoints ())
6667 {
6668 QUIT;
6669 /* If we have a filter, only list the breakpoints it accepts. */
6670 if (filter && !filter (b))
6671 continue;
6672
6673 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6674 accept. Skip the others. */
6675
6676 if (bp_num_list != NULL && *bp_num_list != '\0')
6677 {
6678 if (show_internal) /* maintenance info breakpoint */
6679 {
6680 if (parse_and_eval_long (bp_num_list) != b->number)
6681 continue;
6682 }
6683 else /* all others */
6684 {
6685 if (!number_is_in_list (bp_num_list, b->number))
6686 continue;
6687 }
6688 }
6689 /* We only print out user settable breakpoints unless the
6690 show_internal is set. */
6691 if (show_internal || user_breakpoint_p (b))
6692 {
6693 print_one_breakpoint (b, &last_loc, show_internal);
6694 for (bp_location *loc : b->locations ())
6695 if (loc->disabled_by_cond)
6696 has_disabled_by_cond_location = true;
6697 }
6698 }
6699 }
6700
6701 if (nr_printable_breakpoints == 0)
6702 {
6703 /* If there's a filter, let the caller decide how to report
6704 empty list. */
6705 if (!filter)
6706 {
6707 if (bp_num_list == NULL || *bp_num_list == '\0')
6708 uiout->message ("No breakpoints or watchpoints.\n");
6709 else
6710 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6711 bp_num_list);
6712 }
6713 }
6714 else
6715 {
6716 if (last_loc && !server_command)
6717 set_next_address (last_loc->gdbarch, last_loc->address);
6718
6719 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
6720 uiout->message (_("(*): Breakpoint condition is invalid at this "
6721 "location.\n"));
6722 }
6723
6724 /* FIXME? Should this be moved up so that it is only called when
6725 there have been breakpoints? */
6726 annotate_breakpoints_table_end ();
6727
6728 return nr_printable_breakpoints;
6729 }
6730
6731 /* Display the value of default-collect in a way that is generally
6732 compatible with the breakpoint list. */
6733
6734 static void
6735 default_collect_info (void)
6736 {
6737 struct ui_out *uiout = current_uiout;
6738
6739 /* If it has no value (which is frequently the case), say nothing; a
6740 message like "No default-collect." gets in user's face when it's
6741 not wanted. */
6742 if (!*default_collect)
6743 return;
6744
6745 /* The following phrase lines up nicely with per-tracepoint collect
6746 actions. */
6747 uiout->text ("default collect ");
6748 uiout->field_string ("default-collect", default_collect);
6749 uiout->text (" \n");
6750 }
6751
6752 static void
6753 info_breakpoints_command (const char *args, int from_tty)
6754 {
6755 breakpoint_1 (args, false, NULL);
6756
6757 default_collect_info ();
6758 }
6759
6760 static void
6761 info_watchpoints_command (const char *args, int from_tty)
6762 {
6763 int num_printed = breakpoint_1 (args, false, is_watchpoint);
6764 struct ui_out *uiout = current_uiout;
6765
6766 if (num_printed == 0)
6767 {
6768 if (args == NULL || *args == '\0')
6769 uiout->message ("No watchpoints.\n");
6770 else
6771 uiout->message ("No watchpoint matching '%s'.\n", args);
6772 }
6773 }
6774
6775 static void
6776 maintenance_info_breakpoints (const char *args, int from_tty)
6777 {
6778 breakpoint_1 (args, true, NULL);
6779
6780 default_collect_info ();
6781 }
6782
6783 static int
6784 breakpoint_has_pc (struct breakpoint *b,
6785 struct program_space *pspace,
6786 CORE_ADDR pc, struct obj_section *section)
6787 {
6788 for (bp_location *bl : b->locations ())
6789 {
6790 if (bl->pspace == pspace
6791 && bl->address == pc
6792 && (!overlay_debugging || bl->section == section))
6793 return 1;
6794 }
6795 return 0;
6796 }
6797
6798 /* Print a message describing any user-breakpoints set at PC. This
6799 concerns with logical breakpoints, so we match program spaces, not
6800 address spaces. */
6801
6802 static void
6803 describe_other_breakpoints (struct gdbarch *gdbarch,
6804 struct program_space *pspace, CORE_ADDR pc,
6805 struct obj_section *section, int thread)
6806 {
6807 int others = 0;
6808
6809 for (breakpoint *b : all_breakpoints ())
6810 others += (user_breakpoint_p (b)
6811 && breakpoint_has_pc (b, pspace, pc, section));
6812
6813 if (others > 0)
6814 {
6815 if (others == 1)
6816 printf_filtered (_("Note: breakpoint "));
6817 else /* if (others == ???) */
6818 printf_filtered (_("Note: breakpoints "));
6819 for (breakpoint *b : all_breakpoints ())
6820 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6821 {
6822 others--;
6823 printf_filtered ("%d", b->number);
6824 if (b->thread == -1 && thread != -1)
6825 printf_filtered (" (all threads)");
6826 else if (b->thread != -1)
6827 printf_filtered (" (thread %d)", b->thread);
6828 printf_filtered ("%s%s ",
6829 ((b->enable_state == bp_disabled
6830 || b->enable_state == bp_call_disabled)
6831 ? " (disabled)"
6832 : ""),
6833 (others > 1) ? ","
6834 : ((others == 1) ? " and" : ""));
6835 }
6836 current_uiout->message (_("also set at pc %ps.\n"),
6837 styled_string (address_style.style (),
6838 paddress (gdbarch, pc)));
6839 }
6840 }
6841 \f
6842
6843 /* Return true iff it is meaningful to use the address member of LOC.
6844 For some breakpoint types, the locations' address members are
6845 irrelevant and it makes no sense to attempt to compare them to
6846 other addresses (or use them for any other purpose either).
6847
6848 More specifically, software watchpoints and catchpoints that are
6849 not backed by breakpoints always have a zero valued location
6850 address and we don't want to mark breakpoints of any of these types
6851 to be a duplicate of an actual breakpoint location at address
6852 zero. */
6853
6854 static bool
6855 bl_address_is_meaningful (bp_location *loc)
6856 {
6857 return loc->loc_type != bp_loc_other;
6858 }
6859
6860 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6861 true if LOC1 and LOC2 represent the same watchpoint location. */
6862
6863 static int
6864 watchpoint_locations_match (struct bp_location *loc1,
6865 struct bp_location *loc2)
6866 {
6867 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6868 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6869
6870 /* Both of them must exist. */
6871 gdb_assert (w1 != NULL);
6872 gdb_assert (w2 != NULL);
6873
6874 /* If the target can evaluate the condition expression in hardware,
6875 then we we need to insert both watchpoints even if they are at
6876 the same place. Otherwise the watchpoint will only trigger when
6877 the condition of whichever watchpoint was inserted evaluates to
6878 true, not giving a chance for GDB to check the condition of the
6879 other watchpoint. */
6880 if ((w1->cond_exp
6881 && target_can_accel_watchpoint_condition (loc1->address,
6882 loc1->length,
6883 loc1->watchpoint_type,
6884 w1->cond_exp.get ()))
6885 || (w2->cond_exp
6886 && target_can_accel_watchpoint_condition (loc2->address,
6887 loc2->length,
6888 loc2->watchpoint_type,
6889 w2->cond_exp.get ())))
6890 return 0;
6891
6892 /* Note that this checks the owner's type, not the location's. In
6893 case the target does not support read watchpoints, but does
6894 support access watchpoints, we'll have bp_read_watchpoint
6895 watchpoints with hw_access locations. Those should be considered
6896 duplicates of hw_read locations. The hw_read locations will
6897 become hw_access locations later. */
6898 return (loc1->owner->type == loc2->owner->type
6899 && loc1->pspace->aspace == loc2->pspace->aspace
6900 && loc1->address == loc2->address
6901 && loc1->length == loc2->length);
6902 }
6903
6904 /* See breakpoint.h. */
6905
6906 int
6907 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6908 const address_space *aspace2, CORE_ADDR addr2)
6909 {
6910 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6911 || aspace1 == aspace2)
6912 && addr1 == addr2);
6913 }
6914
6915 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6916 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6917 matches ASPACE2. On targets that have global breakpoints, the address
6918 space doesn't really matter. */
6919
6920 static int
6921 breakpoint_address_match_range (const address_space *aspace1,
6922 CORE_ADDR addr1,
6923 int len1, const address_space *aspace2,
6924 CORE_ADDR addr2)
6925 {
6926 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6927 || aspace1 == aspace2)
6928 && addr2 >= addr1 && addr2 < addr1 + len1);
6929 }
6930
6931 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6932 a ranged breakpoint. In most targets, a match happens only if ASPACE
6933 matches the breakpoint's address space. On targets that have global
6934 breakpoints, the address space doesn't really matter. */
6935
6936 static int
6937 breakpoint_location_address_match (struct bp_location *bl,
6938 const address_space *aspace,
6939 CORE_ADDR addr)
6940 {
6941 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6942 aspace, addr)
6943 || (bl->length
6944 && breakpoint_address_match_range (bl->pspace->aspace,
6945 bl->address, bl->length,
6946 aspace, addr)));
6947 }
6948
6949 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6950 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6951 match happens only if ASPACE matches the breakpoint's address
6952 space. On targets that have global breakpoints, the address space
6953 doesn't really matter. */
6954
6955 static int
6956 breakpoint_location_address_range_overlap (struct bp_location *bl,
6957 const address_space *aspace,
6958 CORE_ADDR addr, int len)
6959 {
6960 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6961 || bl->pspace->aspace == aspace)
6962 {
6963 int bl_len = bl->length != 0 ? bl->length : 1;
6964
6965 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6966 return 1;
6967 }
6968 return 0;
6969 }
6970
6971 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6972 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6973 true, otherwise returns false. */
6974
6975 static int
6976 tracepoint_locations_match (struct bp_location *loc1,
6977 struct bp_location *loc2)
6978 {
6979 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6980 /* Since tracepoint locations are never duplicated with others', tracepoint
6981 locations at the same address of different tracepoints are regarded as
6982 different locations. */
6983 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6984 else
6985 return 0;
6986 }
6987
6988 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6989 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
6990 the same location. If SW_HW_BPS_MATCH is true, then software
6991 breakpoint locations and hardware breakpoint locations match,
6992 otherwise they don't. */
6993
6994 static int
6995 breakpoint_locations_match (struct bp_location *loc1,
6996 struct bp_location *loc2,
6997 bool sw_hw_bps_match)
6998 {
6999 int hw_point1, hw_point2;
7000
7001 /* Both of them must not be in moribund_locations. */
7002 gdb_assert (loc1->owner != NULL);
7003 gdb_assert (loc2->owner != NULL);
7004
7005 hw_point1 = is_hardware_watchpoint (loc1->owner);
7006 hw_point2 = is_hardware_watchpoint (loc2->owner);
7007
7008 if (hw_point1 != hw_point2)
7009 return 0;
7010 else if (hw_point1)
7011 return watchpoint_locations_match (loc1, loc2);
7012 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7013 return tracepoint_locations_match (loc1, loc2);
7014 else
7015 /* We compare bp_location.length in order to cover ranged
7016 breakpoints. Keep this in sync with
7017 bp_location_is_less_than. */
7018 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7019 loc2->pspace->aspace, loc2->address)
7020 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
7021 && loc1->length == loc2->length);
7022 }
7023
7024 static void
7025 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7026 int bnum, int have_bnum)
7027 {
7028 /* The longest string possibly returned by hex_string_custom
7029 is 50 chars. These must be at least that big for safety. */
7030 char astr1[64];
7031 char astr2[64];
7032
7033 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7034 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7035 if (have_bnum)
7036 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7037 bnum, astr1, astr2);
7038 else
7039 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7040 }
7041
7042 /* Adjust a breakpoint's address to account for architectural
7043 constraints on breakpoint placement. Return the adjusted address.
7044 Note: Very few targets require this kind of adjustment. For most
7045 targets, this function is simply the identity function. */
7046
7047 static CORE_ADDR
7048 adjust_breakpoint_address (struct gdbarch *gdbarch,
7049 CORE_ADDR bpaddr, enum bptype bptype)
7050 {
7051 if (bptype == bp_watchpoint
7052 || bptype == bp_hardware_watchpoint
7053 || bptype == bp_read_watchpoint
7054 || bptype == bp_access_watchpoint
7055 || bptype == bp_catchpoint)
7056 {
7057 /* Watchpoints and the various bp_catch_* eventpoints should not
7058 have their addresses modified. */
7059 return bpaddr;
7060 }
7061 else if (bptype == bp_single_step)
7062 {
7063 /* Single-step breakpoints should not have their addresses
7064 modified. If there's any architectural constrain that
7065 applies to this address, then it should have already been
7066 taken into account when the breakpoint was created in the
7067 first place. If we didn't do this, stepping through e.g.,
7068 Thumb-2 IT blocks would break. */
7069 return bpaddr;
7070 }
7071 else
7072 {
7073 CORE_ADDR adjusted_bpaddr = bpaddr;
7074
7075 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7076 {
7077 /* Some targets have architectural constraints on the placement
7078 of breakpoint instructions. Obtain the adjusted address. */
7079 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7080 }
7081
7082 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
7083
7084 /* An adjusted breakpoint address can significantly alter
7085 a user's expectations. Print a warning if an adjustment
7086 is required. */
7087 if (adjusted_bpaddr != bpaddr)
7088 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7089
7090 return adjusted_bpaddr;
7091 }
7092 }
7093
7094 static bp_loc_type
7095 bp_location_from_bp_type (bptype type)
7096 {
7097 switch (type)
7098 {
7099 case bp_breakpoint:
7100 case bp_single_step:
7101 case bp_until:
7102 case bp_finish:
7103 case bp_longjmp:
7104 case bp_longjmp_resume:
7105 case bp_longjmp_call_dummy:
7106 case bp_exception:
7107 case bp_exception_resume:
7108 case bp_step_resume:
7109 case bp_hp_step_resume:
7110 case bp_watchpoint_scope:
7111 case bp_call_dummy:
7112 case bp_std_terminate:
7113 case bp_shlib_event:
7114 case bp_thread_event:
7115 case bp_overlay_event:
7116 case bp_jit_event:
7117 case bp_longjmp_master:
7118 case bp_std_terminate_master:
7119 case bp_exception_master:
7120 case bp_gnu_ifunc_resolver:
7121 case bp_gnu_ifunc_resolver_return:
7122 case bp_dprintf:
7123 return bp_loc_software_breakpoint;
7124 case bp_hardware_breakpoint:
7125 return bp_loc_hardware_breakpoint;
7126 case bp_hardware_watchpoint:
7127 case bp_read_watchpoint:
7128 case bp_access_watchpoint:
7129 return bp_loc_hardware_watchpoint;
7130 case bp_watchpoint:
7131 case bp_catchpoint:
7132 case bp_tracepoint:
7133 case bp_fast_tracepoint:
7134 case bp_static_tracepoint:
7135 return bp_loc_other;
7136 default:
7137 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7138 }
7139 }
7140
7141 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7142 {
7143 this->owner = owner;
7144 this->cond_bytecode = NULL;
7145 this->shlib_disabled = 0;
7146 this->enabled = 1;
7147 this->disabled_by_cond = false;
7148
7149 this->loc_type = type;
7150
7151 if (this->loc_type == bp_loc_software_breakpoint
7152 || this->loc_type == bp_loc_hardware_breakpoint)
7153 mark_breakpoint_location_modified (this);
7154
7155 incref ();
7156 }
7157
7158 bp_location::bp_location (breakpoint *owner)
7159 : bp_location::bp_location (owner,
7160 bp_location_from_bp_type (owner->type))
7161 {
7162 }
7163
7164 /* Allocate a struct bp_location. */
7165
7166 static struct bp_location *
7167 allocate_bp_location (struct breakpoint *bpt)
7168 {
7169 return bpt->ops->allocate_location (bpt);
7170 }
7171
7172 /* Decrement reference count. If the reference count reaches 0,
7173 destroy the bp_location. Sets *BLP to NULL. */
7174
7175 static void
7176 decref_bp_location (struct bp_location **blp)
7177 {
7178 bp_location_ref_policy::decref (*blp);
7179 *blp = NULL;
7180 }
7181
7182 /* Add breakpoint B at the end of the global breakpoint chain. */
7183
7184 static breakpoint *
7185 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7186 {
7187 struct breakpoint *b1;
7188 struct breakpoint *result = b.get ();
7189
7190 /* Add this breakpoint to the end of the chain so that a list of
7191 breakpoints will come out in order of increasing numbers. */
7192
7193 b1 = breakpoint_chain;
7194 if (b1 == 0)
7195 breakpoint_chain = b.release ();
7196 else
7197 {
7198 while (b1->next)
7199 b1 = b1->next;
7200 b1->next = b.release ();
7201 }
7202
7203 return result;
7204 }
7205
7206 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7207
7208 static void
7209 init_raw_breakpoint_without_location (struct breakpoint *b,
7210 struct gdbarch *gdbarch,
7211 enum bptype bptype,
7212 const struct breakpoint_ops *ops)
7213 {
7214 gdb_assert (ops != NULL);
7215
7216 b->ops = ops;
7217 b->type = bptype;
7218 b->gdbarch = gdbarch;
7219 b->language = current_language->la_language;
7220 b->input_radix = input_radix;
7221 b->related_breakpoint = b;
7222 }
7223
7224 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7225 that has type BPTYPE and has no locations as yet. */
7226
7227 static struct breakpoint *
7228 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7229 enum bptype bptype,
7230 const struct breakpoint_ops *ops)
7231 {
7232 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7233
7234 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7235 return add_to_breakpoint_chain (std::move (b));
7236 }
7237
7238 /* Initialize loc->function_name. */
7239
7240 static void
7241 set_breakpoint_location_function (struct bp_location *loc)
7242 {
7243 gdb_assert (loc->owner != NULL);
7244
7245 if (loc->owner->type == bp_breakpoint
7246 || loc->owner->type == bp_hardware_breakpoint
7247 || is_tracepoint (loc->owner))
7248 {
7249 const char *function_name;
7250
7251 if (loc->msymbol != NULL
7252 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
7253 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
7254 {
7255 struct breakpoint *b = loc->owner;
7256
7257 function_name = loc->msymbol->linkage_name ();
7258
7259 if (b->type == bp_breakpoint && b->loc == loc
7260 && loc->next == NULL && b->related_breakpoint == b)
7261 {
7262 /* Create only the whole new breakpoint of this type but do not
7263 mess more complicated breakpoints with multiple locations. */
7264 b->type = bp_gnu_ifunc_resolver;
7265 /* Remember the resolver's address for use by the return
7266 breakpoint. */
7267 loc->related_address = loc->address;
7268 }
7269 }
7270 else
7271 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7272
7273 if (function_name)
7274 loc->function_name = xstrdup (function_name);
7275 }
7276 }
7277
7278 /* Attempt to determine architecture of location identified by SAL. */
7279 struct gdbarch *
7280 get_sal_arch (struct symtab_and_line sal)
7281 {
7282 if (sal.section)
7283 return sal.section->objfile->arch ();
7284 if (sal.symtab)
7285 return SYMTAB_OBJFILE (sal.symtab)->arch ();
7286
7287 return NULL;
7288 }
7289
7290 /* Low level routine for partially initializing a breakpoint of type
7291 BPTYPE. The newly created breakpoint's address, section, source
7292 file name, and line number are provided by SAL.
7293
7294 It is expected that the caller will complete the initialization of
7295 the newly created breakpoint struct as well as output any status
7296 information regarding the creation of a new breakpoint. */
7297
7298 static void
7299 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7300 struct symtab_and_line sal, enum bptype bptype,
7301 const struct breakpoint_ops *ops)
7302 {
7303 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7304
7305 add_location_to_breakpoint (b, &sal);
7306
7307 if (bptype != bp_catchpoint)
7308 gdb_assert (sal.pspace != NULL);
7309
7310 /* Store the program space that was used to set the breakpoint,
7311 except for ordinary breakpoints, which are independent of the
7312 program space. */
7313 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7314 b->pspace = sal.pspace;
7315 }
7316
7317 /* set_raw_breakpoint is a low level routine for allocating and
7318 partially initializing a breakpoint of type BPTYPE. The newly
7319 created breakpoint's address, section, source file name, and line
7320 number are provided by SAL. The newly created and partially
7321 initialized breakpoint is added to the breakpoint chain and
7322 is also returned as the value of this function.
7323
7324 It is expected that the caller will complete the initialization of
7325 the newly created breakpoint struct as well as output any status
7326 information regarding the creation of a new breakpoint. In
7327 particular, set_raw_breakpoint does NOT set the breakpoint
7328 number! Care should be taken to not allow an error to occur
7329 prior to completing the initialization of the breakpoint. If this
7330 should happen, a bogus breakpoint will be left on the chain. */
7331
7332 struct breakpoint *
7333 set_raw_breakpoint (struct gdbarch *gdbarch,
7334 struct symtab_and_line sal, enum bptype bptype,
7335 const struct breakpoint_ops *ops)
7336 {
7337 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7338
7339 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7340 return add_to_breakpoint_chain (std::move (b));
7341 }
7342
7343 /* Call this routine when stepping and nexting to enable a breakpoint
7344 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7345 initiated the operation. */
7346
7347 void
7348 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7349 {
7350 int thread = tp->global_num;
7351
7352 /* To avoid having to rescan all objfile symbols at every step,
7353 we maintain a list of continually-inserted but always disabled
7354 longjmp "master" breakpoints. Here, we simply create momentary
7355 clones of those and enable them for the requested thread. */
7356 for (breakpoint *b : all_breakpoints_safe ())
7357 if (b->pspace == current_program_space
7358 && (b->type == bp_longjmp_master
7359 || b->type == bp_exception_master))
7360 {
7361 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7362 struct breakpoint *clone;
7363
7364 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7365 after their removal. */
7366 clone = momentary_breakpoint_from_master (b, type,
7367 &momentary_breakpoint_ops, 1);
7368 clone->thread = thread;
7369 }
7370
7371 tp->initiating_frame = frame;
7372 }
7373
7374 /* Delete all longjmp breakpoints from THREAD. */
7375 void
7376 delete_longjmp_breakpoint (int thread)
7377 {
7378 for (breakpoint *b : all_breakpoints_safe ())
7379 if (b->type == bp_longjmp || b->type == bp_exception)
7380 {
7381 if (b->thread == thread)
7382 delete_breakpoint (b);
7383 }
7384 }
7385
7386 void
7387 delete_longjmp_breakpoint_at_next_stop (int thread)
7388 {
7389 for (breakpoint *b : all_breakpoints_safe ())
7390 if (b->type == bp_longjmp || b->type == bp_exception)
7391 {
7392 if (b->thread == thread)
7393 b->disposition = disp_del_at_next_stop;
7394 }
7395 }
7396
7397 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7398 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7399 pointer to any of them. Return NULL if this system cannot place longjmp
7400 breakpoints. */
7401
7402 struct breakpoint *
7403 set_longjmp_breakpoint_for_call_dummy (void)
7404 {
7405 breakpoint *retval = nullptr;
7406
7407 for (breakpoint *b : all_breakpoints ())
7408 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7409 {
7410 struct breakpoint *new_b;
7411
7412 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7413 &momentary_breakpoint_ops,
7414 1);
7415 new_b->thread = inferior_thread ()->global_num;
7416
7417 /* Link NEW_B into the chain of RETVAL breakpoints. */
7418
7419 gdb_assert (new_b->related_breakpoint == new_b);
7420 if (retval == NULL)
7421 retval = new_b;
7422 new_b->related_breakpoint = retval;
7423 while (retval->related_breakpoint != new_b->related_breakpoint)
7424 retval = retval->related_breakpoint;
7425 retval->related_breakpoint = new_b;
7426 }
7427
7428 return retval;
7429 }
7430
7431 /* Verify all existing dummy frames and their associated breakpoints for
7432 TP. Remove those which can no longer be found in the current frame
7433 stack.
7434
7435 You should call this function only at places where it is safe to currently
7436 unwind the whole stack. Failed stack unwind would discard live dummy
7437 frames. */
7438
7439 void
7440 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7441 {
7442 struct breakpoint *b, *b_tmp;
7443
7444 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7445 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7446 {
7447 struct breakpoint *dummy_b = b->related_breakpoint;
7448
7449 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7450 dummy_b = dummy_b->related_breakpoint;
7451 if (dummy_b->type != bp_call_dummy
7452 || frame_find_by_id (dummy_b->frame_id) != NULL)
7453 continue;
7454
7455 dummy_frame_discard (dummy_b->frame_id, tp);
7456
7457 while (b->related_breakpoint != b)
7458 {
7459 if (b_tmp == b->related_breakpoint)
7460 b_tmp = b->related_breakpoint->next;
7461 delete_breakpoint (b->related_breakpoint);
7462 }
7463 delete_breakpoint (b);
7464 }
7465 }
7466
7467 void
7468 enable_overlay_breakpoints (void)
7469 {
7470 for (breakpoint *b : all_breakpoints ())
7471 if (b->type == bp_overlay_event)
7472 {
7473 b->enable_state = bp_enabled;
7474 update_global_location_list (UGLL_MAY_INSERT);
7475 overlay_events_enabled = 1;
7476 }
7477 }
7478
7479 void
7480 disable_overlay_breakpoints (void)
7481 {
7482 for (breakpoint *b : all_breakpoints ())
7483 if (b->type == bp_overlay_event)
7484 {
7485 b->enable_state = bp_disabled;
7486 update_global_location_list (UGLL_DONT_INSERT);
7487 overlay_events_enabled = 0;
7488 }
7489 }
7490
7491 /* Set an active std::terminate breakpoint for each std::terminate
7492 master breakpoint. */
7493 void
7494 set_std_terminate_breakpoint (void)
7495 {
7496 for (breakpoint *b : all_breakpoints_safe ())
7497 if (b->pspace == current_program_space
7498 && b->type == bp_std_terminate_master)
7499 {
7500 momentary_breakpoint_from_master (b, bp_std_terminate,
7501 &momentary_breakpoint_ops, 1);
7502 }
7503 }
7504
7505 /* Delete all the std::terminate breakpoints. */
7506 void
7507 delete_std_terminate_breakpoint (void)
7508 {
7509 for (breakpoint *b : all_breakpoints_safe ())
7510 if (b->type == bp_std_terminate)
7511 delete_breakpoint (b);
7512 }
7513
7514 struct breakpoint *
7515 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7516 {
7517 struct breakpoint *b;
7518
7519 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7520 &internal_breakpoint_ops);
7521
7522 b->enable_state = bp_enabled;
7523 /* location has to be used or breakpoint_re_set will delete me. */
7524 b->location = new_address_location (b->loc->address, NULL, 0);
7525
7526 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7527
7528 return b;
7529 }
7530
7531 struct lang_and_radix
7532 {
7533 enum language lang;
7534 int radix;
7535 };
7536
7537 /* Create a breakpoint for JIT code registration and unregistration. */
7538
7539 struct breakpoint *
7540 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7541 {
7542 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7543 &internal_breakpoint_ops);
7544 }
7545
7546 /* Remove JIT code registration and unregistration breakpoint(s). */
7547
7548 void
7549 remove_jit_event_breakpoints (void)
7550 {
7551 for (breakpoint *b : all_breakpoints_safe ())
7552 if (b->type == bp_jit_event
7553 && b->loc->pspace == current_program_space)
7554 delete_breakpoint (b);
7555 }
7556
7557 void
7558 remove_solib_event_breakpoints (void)
7559 {
7560 for (breakpoint *b : all_breakpoints_safe ())
7561 if (b->type == bp_shlib_event
7562 && b->loc->pspace == current_program_space)
7563 delete_breakpoint (b);
7564 }
7565
7566 /* See breakpoint.h. */
7567
7568 void
7569 remove_solib_event_breakpoints_at_next_stop (void)
7570 {
7571 for (breakpoint *b : all_breakpoints_safe ())
7572 if (b->type == bp_shlib_event
7573 && b->loc->pspace == current_program_space)
7574 b->disposition = disp_del_at_next_stop;
7575 }
7576
7577 /* Helper for create_solib_event_breakpoint /
7578 create_and_insert_solib_event_breakpoint. Allows specifying which
7579 INSERT_MODE to pass through to update_global_location_list. */
7580
7581 static struct breakpoint *
7582 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7583 enum ugll_insert_mode insert_mode)
7584 {
7585 struct breakpoint *b;
7586
7587 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7588 &internal_breakpoint_ops);
7589 update_global_location_list_nothrow (insert_mode);
7590 return b;
7591 }
7592
7593 struct breakpoint *
7594 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7595 {
7596 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7597 }
7598
7599 /* See breakpoint.h. */
7600
7601 struct breakpoint *
7602 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7603 {
7604 struct breakpoint *b;
7605
7606 /* Explicitly tell update_global_location_list to insert
7607 locations. */
7608 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7609 if (!b->loc->inserted)
7610 {
7611 delete_breakpoint (b);
7612 return NULL;
7613 }
7614 return b;
7615 }
7616
7617 /* Disable any breakpoints that are on code in shared libraries. Only
7618 apply to enabled breakpoints, disabled ones can just stay disabled. */
7619
7620 void
7621 disable_breakpoints_in_shlibs (void)
7622 {
7623 struct bp_location *loc, **locp_tmp;
7624
7625 ALL_BP_LOCATIONS (loc, locp_tmp)
7626 {
7627 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7628 struct breakpoint *b = loc->owner;
7629
7630 /* We apply the check to all breakpoints, including disabled for
7631 those with loc->duplicate set. This is so that when breakpoint
7632 becomes enabled, or the duplicate is removed, gdb will try to
7633 insert all breakpoints. If we don't set shlib_disabled here,
7634 we'll try to insert those breakpoints and fail. */
7635 if (((b->type == bp_breakpoint)
7636 || (b->type == bp_jit_event)
7637 || (b->type == bp_hardware_breakpoint)
7638 || (is_tracepoint (b)))
7639 && loc->pspace == current_program_space
7640 && !loc->shlib_disabled
7641 && solib_name_from_address (loc->pspace, loc->address)
7642 )
7643 {
7644 loc->shlib_disabled = 1;
7645 }
7646 }
7647 }
7648
7649 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7650 notification of unloaded_shlib. Only apply to enabled breakpoints,
7651 disabled ones can just stay disabled. */
7652
7653 static void
7654 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7655 {
7656 struct bp_location *loc, **locp_tmp;
7657 int disabled_shlib_breaks = 0;
7658
7659 ALL_BP_LOCATIONS (loc, locp_tmp)
7660 {
7661 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7662 struct breakpoint *b = loc->owner;
7663
7664 if (solib->pspace == loc->pspace
7665 && !loc->shlib_disabled
7666 && (((b->type == bp_breakpoint
7667 || b->type == bp_jit_event
7668 || b->type == bp_hardware_breakpoint)
7669 && (loc->loc_type == bp_loc_hardware_breakpoint
7670 || loc->loc_type == bp_loc_software_breakpoint))
7671 || is_tracepoint (b))
7672 && solib_contains_address_p (solib, loc->address))
7673 {
7674 loc->shlib_disabled = 1;
7675 /* At this point, we cannot rely on remove_breakpoint
7676 succeeding so we must mark the breakpoint as not inserted
7677 to prevent future errors occurring in remove_breakpoints. */
7678 loc->inserted = 0;
7679
7680 /* This may cause duplicate notifications for the same breakpoint. */
7681 gdb::observers::breakpoint_modified.notify (b);
7682
7683 if (!disabled_shlib_breaks)
7684 {
7685 target_terminal::ours_for_output ();
7686 warning (_("Temporarily disabling breakpoints "
7687 "for unloaded shared library \"%s\""),
7688 solib->so_name);
7689 }
7690 disabled_shlib_breaks = 1;
7691 }
7692 }
7693 }
7694
7695 /* Disable any breakpoints and tracepoints in OBJFILE upon
7696 notification of free_objfile. Only apply to enabled breakpoints,
7697 disabled ones can just stay disabled. */
7698
7699 static void
7700 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7701 {
7702 if (objfile == NULL)
7703 return;
7704
7705 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7706 managed by the user with add-symbol-file/remove-symbol-file.
7707 Similarly to how breakpoints in shared libraries are handled in
7708 response to "nosharedlibrary", mark breakpoints in such modules
7709 shlib_disabled so they end up uninserted on the next global
7710 location list update. Shared libraries not loaded by the user
7711 aren't handled here -- they're already handled in
7712 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7713 solib_unloaded observer. We skip objfiles that are not
7714 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7715 main objfile). */
7716 if ((objfile->flags & OBJF_SHARED) == 0
7717 || (objfile->flags & OBJF_USERLOADED) == 0)
7718 return;
7719
7720 for (breakpoint *b : all_breakpoints ())
7721 {
7722 int bp_modified = 0;
7723
7724 if (!is_breakpoint (b) && !is_tracepoint (b))
7725 continue;
7726
7727 for (bp_location *loc : b->locations ())
7728 {
7729 CORE_ADDR loc_addr = loc->address;
7730
7731 if (loc->loc_type != bp_loc_hardware_breakpoint
7732 && loc->loc_type != bp_loc_software_breakpoint)
7733 continue;
7734
7735 if (loc->shlib_disabled != 0)
7736 continue;
7737
7738 if (objfile->pspace != loc->pspace)
7739 continue;
7740
7741 if (loc->loc_type != bp_loc_hardware_breakpoint
7742 && loc->loc_type != bp_loc_software_breakpoint)
7743 continue;
7744
7745 if (is_addr_in_objfile (loc_addr, objfile))
7746 {
7747 loc->shlib_disabled = 1;
7748 /* At this point, we don't know whether the object was
7749 unmapped from the inferior or not, so leave the
7750 inserted flag alone. We'll handle failure to
7751 uninsert quietly, in case the object was indeed
7752 unmapped. */
7753
7754 mark_breakpoint_location_modified (loc);
7755
7756 bp_modified = 1;
7757 }
7758 }
7759
7760 if (bp_modified)
7761 gdb::observers::breakpoint_modified.notify (b);
7762 }
7763 }
7764
7765 /* FORK & VFORK catchpoints. */
7766
7767 /* An instance of this type is used to represent a fork or vfork
7768 catchpoint. A breakpoint is really of this type iff its ops pointer points
7769 to CATCH_FORK_BREAKPOINT_OPS. */
7770
7771 struct fork_catchpoint : public breakpoint
7772 {
7773 /* Process id of a child process whose forking triggered this
7774 catchpoint. This field is only valid immediately after this
7775 catchpoint has triggered. */
7776 ptid_t forked_inferior_pid;
7777 };
7778
7779 /* Implement the "insert" breakpoint_ops method for fork
7780 catchpoints. */
7781
7782 static int
7783 insert_catch_fork (struct bp_location *bl)
7784 {
7785 return target_insert_fork_catchpoint (inferior_ptid.pid ());
7786 }
7787
7788 /* Implement the "remove" breakpoint_ops method for fork
7789 catchpoints. */
7790
7791 static int
7792 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
7793 {
7794 return target_remove_fork_catchpoint (inferior_ptid.pid ());
7795 }
7796
7797 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7798 catchpoints. */
7799
7800 static int
7801 breakpoint_hit_catch_fork (const struct bp_location *bl,
7802 const address_space *aspace, CORE_ADDR bp_addr,
7803 const struct target_waitstatus *ws)
7804 {
7805 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7806
7807 if (ws->kind != TARGET_WAITKIND_FORKED)
7808 return 0;
7809
7810 c->forked_inferior_pid = ws->value.related_pid;
7811 return 1;
7812 }
7813
7814 /* Implement the "print_it" breakpoint_ops method for fork
7815 catchpoints. */
7816
7817 static enum print_stop_action
7818 print_it_catch_fork (bpstat bs)
7819 {
7820 struct ui_out *uiout = current_uiout;
7821 struct breakpoint *b = bs->breakpoint_at;
7822 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7823
7824 annotate_catchpoint (b->number);
7825 maybe_print_thread_hit_breakpoint (uiout);
7826 if (b->disposition == disp_del)
7827 uiout->text ("Temporary catchpoint ");
7828 else
7829 uiout->text ("Catchpoint ");
7830 if (uiout->is_mi_like_p ())
7831 {
7832 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7833 uiout->field_string ("disp", bpdisp_text (b->disposition));
7834 }
7835 uiout->field_signed ("bkptno", b->number);
7836 uiout->text (" (forked process ");
7837 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7838 uiout->text ("), ");
7839 return PRINT_SRC_AND_LOC;
7840 }
7841
7842 /* Implement the "print_one" breakpoint_ops method for fork
7843 catchpoints. */
7844
7845 static void
7846 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7847 {
7848 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7849 struct value_print_options opts;
7850 struct ui_out *uiout = current_uiout;
7851
7852 get_user_print_options (&opts);
7853
7854 /* Field 4, the address, is omitted (which makes the columns not
7855 line up too nicely with the headers, but the effect is relatively
7856 readable). */
7857 if (opts.addressprint)
7858 uiout->field_skip ("addr");
7859 annotate_field (5);
7860 uiout->text ("fork");
7861 if (c->forked_inferior_pid != null_ptid)
7862 {
7863 uiout->text (", process ");
7864 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7865 uiout->spaces (1);
7866 }
7867
7868 if (uiout->is_mi_like_p ())
7869 uiout->field_string ("catch-type", "fork");
7870 }
7871
7872 /* Implement the "print_mention" breakpoint_ops method for fork
7873 catchpoints. */
7874
7875 static void
7876 print_mention_catch_fork (struct breakpoint *b)
7877 {
7878 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7879 }
7880
7881 /* Implement the "print_recreate" breakpoint_ops method for fork
7882 catchpoints. */
7883
7884 static void
7885 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7886 {
7887 fprintf_unfiltered (fp, "catch fork");
7888 print_recreate_thread (b, fp);
7889 }
7890
7891 /* The breakpoint_ops structure to be used in fork catchpoints. */
7892
7893 static struct breakpoint_ops catch_fork_breakpoint_ops;
7894
7895 /* Implement the "insert" breakpoint_ops method for vfork
7896 catchpoints. */
7897
7898 static int
7899 insert_catch_vfork (struct bp_location *bl)
7900 {
7901 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
7902 }
7903
7904 /* Implement the "remove" breakpoint_ops method for vfork
7905 catchpoints. */
7906
7907 static int
7908 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
7909 {
7910 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
7911 }
7912
7913 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7914 catchpoints. */
7915
7916 static int
7917 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7918 const address_space *aspace, CORE_ADDR bp_addr,
7919 const struct target_waitstatus *ws)
7920 {
7921 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7922
7923 if (ws->kind != TARGET_WAITKIND_VFORKED)
7924 return 0;
7925
7926 c->forked_inferior_pid = ws->value.related_pid;
7927 return 1;
7928 }
7929
7930 /* Implement the "print_it" breakpoint_ops method for vfork
7931 catchpoints. */
7932
7933 static enum print_stop_action
7934 print_it_catch_vfork (bpstat bs)
7935 {
7936 struct ui_out *uiout = current_uiout;
7937 struct breakpoint *b = bs->breakpoint_at;
7938 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7939
7940 annotate_catchpoint (b->number);
7941 maybe_print_thread_hit_breakpoint (uiout);
7942 if (b->disposition == disp_del)
7943 uiout->text ("Temporary catchpoint ");
7944 else
7945 uiout->text ("Catchpoint ");
7946 if (uiout->is_mi_like_p ())
7947 {
7948 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7949 uiout->field_string ("disp", bpdisp_text (b->disposition));
7950 }
7951 uiout->field_signed ("bkptno", b->number);
7952 uiout->text (" (vforked process ");
7953 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7954 uiout->text ("), ");
7955 return PRINT_SRC_AND_LOC;
7956 }
7957
7958 /* Implement the "print_one" breakpoint_ops method for vfork
7959 catchpoints. */
7960
7961 static void
7962 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7963 {
7964 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7965 struct value_print_options opts;
7966 struct ui_out *uiout = current_uiout;
7967
7968 get_user_print_options (&opts);
7969 /* Field 4, the address, is omitted (which makes the columns not
7970 line up too nicely with the headers, but the effect is relatively
7971 readable). */
7972 if (opts.addressprint)
7973 uiout->field_skip ("addr");
7974 annotate_field (5);
7975 uiout->text ("vfork");
7976 if (c->forked_inferior_pid != null_ptid)
7977 {
7978 uiout->text (", process ");
7979 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7980 uiout->spaces (1);
7981 }
7982
7983 if (uiout->is_mi_like_p ())
7984 uiout->field_string ("catch-type", "vfork");
7985 }
7986
7987 /* Implement the "print_mention" breakpoint_ops method for vfork
7988 catchpoints. */
7989
7990 static void
7991 print_mention_catch_vfork (struct breakpoint *b)
7992 {
7993 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7994 }
7995
7996 /* Implement the "print_recreate" breakpoint_ops method for vfork
7997 catchpoints. */
7998
7999 static void
8000 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8001 {
8002 fprintf_unfiltered (fp, "catch vfork");
8003 print_recreate_thread (b, fp);
8004 }
8005
8006 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8007
8008 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8009
8010 /* An instance of this type is used to represent an solib catchpoint.
8011 A breakpoint is really of this type iff its ops pointer points to
8012 CATCH_SOLIB_BREAKPOINT_OPS. */
8013
8014 struct solib_catchpoint : public breakpoint
8015 {
8016 ~solib_catchpoint () override;
8017
8018 /* True for "catch load", false for "catch unload". */
8019 bool is_load;
8020
8021 /* Regular expression to match, if any. COMPILED is only valid when
8022 REGEX is non-NULL. */
8023 char *regex;
8024 std::unique_ptr<compiled_regex> compiled;
8025 };
8026
8027 solib_catchpoint::~solib_catchpoint ()
8028 {
8029 xfree (this->regex);
8030 }
8031
8032 static int
8033 insert_catch_solib (struct bp_location *ignore)
8034 {
8035 return 0;
8036 }
8037
8038 static int
8039 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
8040 {
8041 return 0;
8042 }
8043
8044 static int
8045 breakpoint_hit_catch_solib (const struct bp_location *bl,
8046 const address_space *aspace,
8047 CORE_ADDR bp_addr,
8048 const struct target_waitstatus *ws)
8049 {
8050 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8051
8052 if (ws->kind == TARGET_WAITKIND_LOADED)
8053 return 1;
8054
8055 for (breakpoint *other : all_breakpoints ())
8056 {
8057 if (other == bl->owner)
8058 continue;
8059
8060 if (other->type != bp_shlib_event)
8061 continue;
8062
8063 if (self->pspace != NULL && other->pspace != self->pspace)
8064 continue;
8065
8066 for (bp_location *other_bl : other->locations ())
8067 {
8068 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8069 return 1;
8070 }
8071 }
8072
8073 return 0;
8074 }
8075
8076 static void
8077 check_status_catch_solib (struct bpstats *bs)
8078 {
8079 struct solib_catchpoint *self
8080 = (struct solib_catchpoint *) bs->breakpoint_at;
8081
8082 if (self->is_load)
8083 {
8084 for (so_list *iter : current_program_space->added_solibs)
8085 {
8086 if (!self->regex
8087 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
8088 return;
8089 }
8090 }
8091 else
8092 {
8093 for (const std::string &iter : current_program_space->deleted_solibs)
8094 {
8095 if (!self->regex
8096 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
8097 return;
8098 }
8099 }
8100
8101 bs->stop = 0;
8102 bs->print_it = print_it_noop;
8103 }
8104
8105 static enum print_stop_action
8106 print_it_catch_solib (bpstat bs)
8107 {
8108 struct breakpoint *b = bs->breakpoint_at;
8109 struct ui_out *uiout = current_uiout;
8110
8111 annotate_catchpoint (b->number);
8112 maybe_print_thread_hit_breakpoint (uiout);
8113 if (b->disposition == disp_del)
8114 uiout->text ("Temporary catchpoint ");
8115 else
8116 uiout->text ("Catchpoint ");
8117 uiout->field_signed ("bkptno", b->number);
8118 uiout->text ("\n");
8119 if (uiout->is_mi_like_p ())
8120 uiout->field_string ("disp", bpdisp_text (b->disposition));
8121 print_solib_event (1);
8122 return PRINT_SRC_AND_LOC;
8123 }
8124
8125 static void
8126 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8127 {
8128 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8129 struct value_print_options opts;
8130 struct ui_out *uiout = current_uiout;
8131
8132 get_user_print_options (&opts);
8133 /* Field 4, the address, is omitted (which makes the columns not
8134 line up too nicely with the headers, but the effect is relatively
8135 readable). */
8136 if (opts.addressprint)
8137 {
8138 annotate_field (4);
8139 uiout->field_skip ("addr");
8140 }
8141
8142 std::string msg;
8143 annotate_field (5);
8144 if (self->is_load)
8145 {
8146 if (self->regex)
8147 msg = string_printf (_("load of library matching %s"), self->regex);
8148 else
8149 msg = _("load of library");
8150 }
8151 else
8152 {
8153 if (self->regex)
8154 msg = string_printf (_("unload of library matching %s"), self->regex);
8155 else
8156 msg = _("unload of library");
8157 }
8158 uiout->field_string ("what", msg);
8159
8160 if (uiout->is_mi_like_p ())
8161 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8162 }
8163
8164 static void
8165 print_mention_catch_solib (struct breakpoint *b)
8166 {
8167 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8168
8169 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8170 self->is_load ? "load" : "unload");
8171 }
8172
8173 static void
8174 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8175 {
8176 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8177
8178 fprintf_unfiltered (fp, "%s %s",
8179 b->disposition == disp_del ? "tcatch" : "catch",
8180 self->is_load ? "load" : "unload");
8181 if (self->regex)
8182 fprintf_unfiltered (fp, " %s", self->regex);
8183 fprintf_unfiltered (fp, "\n");
8184 }
8185
8186 static struct breakpoint_ops catch_solib_breakpoint_ops;
8187
8188 /* See breakpoint.h. */
8189
8190 void
8191 add_solib_catchpoint (const char *arg, bool is_load, bool is_temp, bool enabled)
8192 {
8193 struct gdbarch *gdbarch = get_current_arch ();
8194
8195 if (!arg)
8196 arg = "";
8197 arg = skip_spaces (arg);
8198
8199 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
8200
8201 if (*arg != '\0')
8202 {
8203 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8204 _("Invalid regexp")));
8205 c->regex = xstrdup (arg);
8206 }
8207
8208 c->is_load = is_load;
8209 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
8210 &catch_solib_breakpoint_ops);
8211
8212 c->enable_state = enabled ? bp_enabled : bp_disabled;
8213
8214 install_breakpoint (0, std::move (c), 1);
8215 }
8216
8217 /* A helper function that does all the work for "catch load" and
8218 "catch unload". */
8219
8220 static void
8221 catch_load_or_unload (const char *arg, int from_tty, int is_load,
8222 struct cmd_list_element *command)
8223 {
8224 const int enabled = 1;
8225 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
8226
8227 add_solib_catchpoint (arg, is_load, temp, enabled);
8228 }
8229
8230 static void
8231 catch_load_command_1 (const char *arg, int from_tty,
8232 struct cmd_list_element *command)
8233 {
8234 catch_load_or_unload (arg, from_tty, 1, command);
8235 }
8236
8237 static void
8238 catch_unload_command_1 (const char *arg, int from_tty,
8239 struct cmd_list_element *command)
8240 {
8241 catch_load_or_unload (arg, from_tty, 0, command);
8242 }
8243
8244 /* See breakpoint.h. */
8245
8246 void
8247 init_catchpoint (struct breakpoint *b,
8248 struct gdbarch *gdbarch, bool temp,
8249 const char *cond_string,
8250 const struct breakpoint_ops *ops)
8251 {
8252 symtab_and_line sal;
8253 sal.pspace = current_program_space;
8254
8255 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8256
8257 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8258 b->disposition = temp ? disp_del : disp_donttouch;
8259 }
8260
8261 void
8262 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8263 {
8264 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8265 set_breakpoint_number (internal, b);
8266 if (is_tracepoint (b))
8267 set_tracepoint_count (breakpoint_count);
8268 if (!internal)
8269 mention (b);
8270 gdb::observers::breakpoint_created.notify (b);
8271
8272 if (update_gll)
8273 update_global_location_list (UGLL_MAY_INSERT);
8274 }
8275
8276 static void
8277 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8278 bool temp, const char *cond_string,
8279 const struct breakpoint_ops *ops)
8280 {
8281 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
8282
8283 init_catchpoint (c.get (), gdbarch, temp, cond_string, ops);
8284
8285 c->forked_inferior_pid = null_ptid;
8286
8287 install_breakpoint (0, std::move (c), 1);
8288 }
8289
8290 /* Exec catchpoints. */
8291
8292 /* An instance of this type is used to represent an exec catchpoint.
8293 A breakpoint is really of this type iff its ops pointer points to
8294 CATCH_EXEC_BREAKPOINT_OPS. */
8295
8296 struct exec_catchpoint : public breakpoint
8297 {
8298 ~exec_catchpoint () override;
8299
8300 /* Filename of a program whose exec triggered this catchpoint.
8301 This field is only valid immediately after this catchpoint has
8302 triggered. */
8303 char *exec_pathname;
8304 };
8305
8306 /* Exec catchpoint destructor. */
8307
8308 exec_catchpoint::~exec_catchpoint ()
8309 {
8310 xfree (this->exec_pathname);
8311 }
8312
8313 static int
8314 insert_catch_exec (struct bp_location *bl)
8315 {
8316 return target_insert_exec_catchpoint (inferior_ptid.pid ());
8317 }
8318
8319 static int
8320 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8321 {
8322 return target_remove_exec_catchpoint (inferior_ptid.pid ());
8323 }
8324
8325 static int
8326 breakpoint_hit_catch_exec (const struct bp_location *bl,
8327 const address_space *aspace, CORE_ADDR bp_addr,
8328 const struct target_waitstatus *ws)
8329 {
8330 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8331
8332 if (ws->kind != TARGET_WAITKIND_EXECD)
8333 return 0;
8334
8335 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8336 return 1;
8337 }
8338
8339 static enum print_stop_action
8340 print_it_catch_exec (bpstat bs)
8341 {
8342 struct ui_out *uiout = current_uiout;
8343 struct breakpoint *b = bs->breakpoint_at;
8344 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8345
8346 annotate_catchpoint (b->number);
8347 maybe_print_thread_hit_breakpoint (uiout);
8348 if (b->disposition == disp_del)
8349 uiout->text ("Temporary catchpoint ");
8350 else
8351 uiout->text ("Catchpoint ");
8352 if (uiout->is_mi_like_p ())
8353 {
8354 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8355 uiout->field_string ("disp", bpdisp_text (b->disposition));
8356 }
8357 uiout->field_signed ("bkptno", b->number);
8358 uiout->text (" (exec'd ");
8359 uiout->field_string ("new-exec", c->exec_pathname);
8360 uiout->text ("), ");
8361
8362 return PRINT_SRC_AND_LOC;
8363 }
8364
8365 static void
8366 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8367 {
8368 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8369 struct value_print_options opts;
8370 struct ui_out *uiout = current_uiout;
8371
8372 get_user_print_options (&opts);
8373
8374 /* Field 4, the address, is omitted (which makes the columns
8375 not line up too nicely with the headers, but the effect
8376 is relatively readable). */
8377 if (opts.addressprint)
8378 uiout->field_skip ("addr");
8379 annotate_field (5);
8380 uiout->text ("exec");
8381 if (c->exec_pathname != NULL)
8382 {
8383 uiout->text (", program \"");
8384 uiout->field_string ("what", c->exec_pathname);
8385 uiout->text ("\" ");
8386 }
8387
8388 if (uiout->is_mi_like_p ())
8389 uiout->field_string ("catch-type", "exec");
8390 }
8391
8392 static void
8393 print_mention_catch_exec (struct breakpoint *b)
8394 {
8395 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8396 }
8397
8398 /* Implement the "print_recreate" breakpoint_ops method for exec
8399 catchpoints. */
8400
8401 static void
8402 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8403 {
8404 fprintf_unfiltered (fp, "catch exec");
8405 print_recreate_thread (b, fp);
8406 }
8407
8408 static struct breakpoint_ops catch_exec_breakpoint_ops;
8409
8410 static int
8411 hw_breakpoint_used_count (void)
8412 {
8413 int i = 0;
8414
8415 for (breakpoint *b : all_breakpoints ())
8416 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8417 for (bp_location *bl : b->locations ())
8418 {
8419 /* Special types of hardware breakpoints may use more than
8420 one register. */
8421 i += b->ops->resources_needed (bl);
8422 }
8423
8424 return i;
8425 }
8426
8427 /* Returns the resources B would use if it were a hardware
8428 watchpoint. */
8429
8430 static int
8431 hw_watchpoint_use_count (struct breakpoint *b)
8432 {
8433 int i = 0;
8434
8435 if (!breakpoint_enabled (b))
8436 return 0;
8437
8438 for (bp_location *bl : b->locations ())
8439 {
8440 /* Special types of hardware watchpoints may use more than
8441 one register. */
8442 i += b->ops->resources_needed (bl);
8443 }
8444
8445 return i;
8446 }
8447
8448 /* Returns the sum the used resources of all hardware watchpoints of
8449 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8450 the sum of the used resources of all hardware watchpoints of other
8451 types _not_ TYPE. */
8452
8453 static int
8454 hw_watchpoint_used_count_others (struct breakpoint *except,
8455 enum bptype type, int *other_type_used)
8456 {
8457 int i = 0;
8458
8459 *other_type_used = 0;
8460 for (breakpoint *b : all_breakpoints ())
8461 {
8462 if (b == except)
8463 continue;
8464 if (!breakpoint_enabled (b))
8465 continue;
8466
8467 if (b->type == type)
8468 i += hw_watchpoint_use_count (b);
8469 else if (is_hardware_watchpoint (b))
8470 *other_type_used = 1;
8471 }
8472
8473 return i;
8474 }
8475
8476 void
8477 disable_watchpoints_before_interactive_call_start (void)
8478 {
8479 for (breakpoint *b : all_breakpoints ())
8480 if (is_watchpoint (b) && breakpoint_enabled (b))
8481 {
8482 b->enable_state = bp_call_disabled;
8483 update_global_location_list (UGLL_DONT_INSERT);
8484 }
8485 }
8486
8487 void
8488 enable_watchpoints_after_interactive_call_stop (void)
8489 {
8490 for (breakpoint *b : all_breakpoints ())
8491 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8492 {
8493 b->enable_state = bp_enabled;
8494 update_global_location_list (UGLL_MAY_INSERT);
8495 }
8496 }
8497
8498 void
8499 disable_breakpoints_before_startup (void)
8500 {
8501 current_program_space->executing_startup = 1;
8502 update_global_location_list (UGLL_DONT_INSERT);
8503 }
8504
8505 void
8506 enable_breakpoints_after_startup (void)
8507 {
8508 current_program_space->executing_startup = 0;
8509 breakpoint_re_set ();
8510 }
8511
8512 /* Create a new single-step breakpoint for thread THREAD, with no
8513 locations. */
8514
8515 static struct breakpoint *
8516 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8517 {
8518 std::unique_ptr<breakpoint> b (new breakpoint ());
8519
8520 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
8521 &momentary_breakpoint_ops);
8522
8523 b->disposition = disp_donttouch;
8524 b->frame_id = null_frame_id;
8525
8526 b->thread = thread;
8527 gdb_assert (b->thread != 0);
8528
8529 return add_to_breakpoint_chain (std::move (b));
8530 }
8531
8532 /* Set a momentary breakpoint of type TYPE at address specified by
8533 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8534 frame. */
8535
8536 breakpoint_up
8537 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8538 struct frame_id frame_id, enum bptype type)
8539 {
8540 struct breakpoint *b;
8541
8542 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8543 tail-called one. */
8544 gdb_assert (!frame_id_artificial_p (frame_id));
8545
8546 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8547 b->enable_state = bp_enabled;
8548 b->disposition = disp_donttouch;
8549 b->frame_id = frame_id;
8550
8551 b->thread = inferior_thread ()->global_num;
8552
8553 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8554
8555 return breakpoint_up (b);
8556 }
8557
8558 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8559 The new breakpoint will have type TYPE, use OPS as its
8560 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8561
8562 static struct breakpoint *
8563 momentary_breakpoint_from_master (struct breakpoint *orig,
8564 enum bptype type,
8565 const struct breakpoint_ops *ops,
8566 int loc_enabled)
8567 {
8568 struct breakpoint *copy;
8569
8570 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8571 copy->loc = allocate_bp_location (copy);
8572 set_breakpoint_location_function (copy->loc);
8573
8574 copy->loc->gdbarch = orig->loc->gdbarch;
8575 copy->loc->requested_address = orig->loc->requested_address;
8576 copy->loc->address = orig->loc->address;
8577 copy->loc->section = orig->loc->section;
8578 copy->loc->pspace = orig->loc->pspace;
8579 copy->loc->probe = orig->loc->probe;
8580 copy->loc->line_number = orig->loc->line_number;
8581 copy->loc->symtab = orig->loc->symtab;
8582 copy->loc->enabled = loc_enabled;
8583 copy->frame_id = orig->frame_id;
8584 copy->thread = orig->thread;
8585 copy->pspace = orig->pspace;
8586
8587 copy->enable_state = bp_enabled;
8588 copy->disposition = disp_donttouch;
8589 copy->number = internal_breakpoint_number--;
8590
8591 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8592 return copy;
8593 }
8594
8595 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8596 ORIG is NULL. */
8597
8598 struct breakpoint *
8599 clone_momentary_breakpoint (struct breakpoint *orig)
8600 {
8601 /* If there's nothing to clone, then return nothing. */
8602 if (orig == NULL)
8603 return NULL;
8604
8605 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8606 }
8607
8608 breakpoint_up
8609 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8610 enum bptype type)
8611 {
8612 struct symtab_and_line sal;
8613
8614 sal = find_pc_line (pc, 0);
8615 sal.pc = pc;
8616 sal.section = find_pc_overlay (pc);
8617 sal.explicit_pc = 1;
8618
8619 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8620 }
8621 \f
8622
8623 /* Tell the user we have just set a breakpoint B. */
8624
8625 static void
8626 mention (struct breakpoint *b)
8627 {
8628 b->ops->print_mention (b);
8629 current_uiout->text ("\n");
8630 }
8631 \f
8632
8633 static bool bp_loc_is_permanent (struct bp_location *loc);
8634
8635 /* Handle "set breakpoint auto-hw on".
8636
8637 If the explicitly specified breakpoint type is not hardware
8638 breakpoint, check the memory map to see whether the breakpoint
8639 address is in read-only memory.
8640
8641 - location type is not hardware breakpoint, memory is read-only.
8642 We change the type of the location to hardware breakpoint.
8643
8644 - location type is hardware breakpoint, memory is read-write. This
8645 means we've previously made the location hardware one, but then the
8646 memory map changed, so we undo.
8647 */
8648
8649 static void
8650 handle_automatic_hardware_breakpoints (bp_location *bl)
8651 {
8652 if (automatic_hardware_breakpoints
8653 && bl->owner->type != bp_hardware_breakpoint
8654 && (bl->loc_type == bp_loc_software_breakpoint
8655 || bl->loc_type == bp_loc_hardware_breakpoint))
8656 {
8657 /* When breakpoints are removed, remove_breakpoints will use
8658 location types we've just set here, the only possible problem
8659 is that memory map has changed during running program, but
8660 it's not going to work anyway with current gdb. */
8661 mem_region *mr = lookup_mem_region (bl->address);
8662
8663 if (mr != nullptr)
8664 {
8665 enum bp_loc_type new_type;
8666
8667 if (mr->attrib.mode != MEM_RW)
8668 new_type = bp_loc_hardware_breakpoint;
8669 else
8670 new_type = bp_loc_software_breakpoint;
8671
8672 if (new_type != bl->loc_type)
8673 {
8674 static bool said = false;
8675
8676 bl->loc_type = new_type;
8677 if (!said)
8678 {
8679 fprintf_filtered (gdb_stdout,
8680 _("Note: automatically using "
8681 "hardware breakpoints for "
8682 "read-only addresses.\n"));
8683 said = true;
8684 }
8685 }
8686 }
8687 }
8688 }
8689
8690 static struct bp_location *
8691 add_location_to_breakpoint (struct breakpoint *b,
8692 const struct symtab_and_line *sal)
8693 {
8694 struct bp_location *loc, **tmp;
8695 CORE_ADDR adjusted_address;
8696 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8697
8698 if (loc_gdbarch == NULL)
8699 loc_gdbarch = b->gdbarch;
8700
8701 /* Adjust the breakpoint's address prior to allocating a location.
8702 Once we call allocate_bp_location(), that mostly uninitialized
8703 location will be placed on the location chain. Adjustment of the
8704 breakpoint may cause target_read_memory() to be called and we do
8705 not want its scan of the location chain to find a breakpoint and
8706 location that's only been partially initialized. */
8707 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8708 sal->pc, b->type);
8709
8710 /* Sort the locations by their ADDRESS. */
8711 loc = allocate_bp_location (b);
8712 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8713 tmp = &((*tmp)->next))
8714 ;
8715 loc->next = *tmp;
8716 *tmp = loc;
8717
8718 loc->requested_address = sal->pc;
8719 loc->address = adjusted_address;
8720 loc->pspace = sal->pspace;
8721 loc->probe.prob = sal->prob;
8722 loc->probe.objfile = sal->objfile;
8723 gdb_assert (loc->pspace != NULL);
8724 loc->section = sal->section;
8725 loc->gdbarch = loc_gdbarch;
8726 loc->line_number = sal->line;
8727 loc->symtab = sal->symtab;
8728 loc->symbol = sal->symbol;
8729 loc->msymbol = sal->msymbol;
8730 loc->objfile = sal->objfile;
8731
8732 set_breakpoint_location_function (loc);
8733
8734 /* While by definition, permanent breakpoints are already present in the
8735 code, we don't mark the location as inserted. Normally one would expect
8736 that GDB could rely on that breakpoint instruction to stop the program,
8737 thus removing the need to insert its own breakpoint, except that executing
8738 the breakpoint instruction can kill the target instead of reporting a
8739 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8740 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8741 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8742 breakpoint be inserted normally results in QEMU knowing about the GDB
8743 breakpoint, and thus trap before the breakpoint instruction is executed.
8744 (If GDB later needs to continue execution past the permanent breakpoint,
8745 it manually increments the PC, thus avoiding executing the breakpoint
8746 instruction.) */
8747 if (bp_loc_is_permanent (loc))
8748 loc->permanent = 1;
8749
8750 return loc;
8751 }
8752 \f
8753
8754 /* Return true if LOC is pointing to a permanent breakpoint,
8755 return false otherwise. */
8756
8757 static bool
8758 bp_loc_is_permanent (struct bp_location *loc)
8759 {
8760 gdb_assert (loc != NULL);
8761
8762 /* If we have a non-breakpoint-backed catchpoint or a software
8763 watchpoint, just return 0. We should not attempt to read from
8764 the addresses the locations of these breakpoint types point to.
8765 gdbarch_program_breakpoint_here_p, below, will attempt to read
8766 memory. */
8767 if (!bl_address_is_meaningful (loc))
8768 return false;
8769
8770 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8771 switch_to_program_space_and_thread (loc->pspace);
8772 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
8773 }
8774
8775 /* Build a command list for the dprintf corresponding to the current
8776 settings of the dprintf style options. */
8777
8778 static void
8779 update_dprintf_command_list (struct breakpoint *b)
8780 {
8781 char *dprintf_args = b->extra_string;
8782 char *printf_line = NULL;
8783
8784 if (!dprintf_args)
8785 return;
8786
8787 dprintf_args = skip_spaces (dprintf_args);
8788
8789 /* Allow a comma, as it may have terminated a location, but don't
8790 insist on it. */
8791 if (*dprintf_args == ',')
8792 ++dprintf_args;
8793 dprintf_args = skip_spaces (dprintf_args);
8794
8795 if (*dprintf_args != '"')
8796 error (_("Bad format string, missing '\"'."));
8797
8798 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8799 printf_line = xstrprintf ("printf %s", dprintf_args);
8800 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8801 {
8802 if (!dprintf_function)
8803 error (_("No function supplied for dprintf call"));
8804
8805 if (dprintf_channel && strlen (dprintf_channel) > 0)
8806 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8807 dprintf_function,
8808 dprintf_channel,
8809 dprintf_args);
8810 else
8811 printf_line = xstrprintf ("call (void) %s (%s)",
8812 dprintf_function,
8813 dprintf_args);
8814 }
8815 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8816 {
8817 if (target_can_run_breakpoint_commands ())
8818 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8819 else
8820 {
8821 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8822 printf_line = xstrprintf ("printf %s", dprintf_args);
8823 }
8824 }
8825 else
8826 internal_error (__FILE__, __LINE__,
8827 _("Invalid dprintf style."));
8828
8829 gdb_assert (printf_line != NULL);
8830
8831 /* Manufacture a printf sequence. */
8832 struct command_line *printf_cmd_line
8833 = new struct command_line (simple_control, printf_line);
8834 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8835 command_lines_deleter ()));
8836 }
8837
8838 /* Update all dprintf commands, making their command lists reflect
8839 current style settings. */
8840
8841 static void
8842 update_dprintf_commands (const char *args, int from_tty,
8843 struct cmd_list_element *c)
8844 {
8845 for (breakpoint *b : all_breakpoints ())
8846 if (b->type == bp_dprintf)
8847 update_dprintf_command_list (b);
8848 }
8849
8850 /* Create a breakpoint with SAL as location. Use LOCATION
8851 as a description of the location, and COND_STRING
8852 as condition expression. If LOCATION is NULL then create an
8853 "address location" from the address in the SAL. */
8854
8855 static void
8856 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8857 gdb::array_view<const symtab_and_line> sals,
8858 event_location_up &&location,
8859 gdb::unique_xmalloc_ptr<char> filter,
8860 gdb::unique_xmalloc_ptr<char> cond_string,
8861 gdb::unique_xmalloc_ptr<char> extra_string,
8862 enum bptype type, enum bpdisp disposition,
8863 int thread, int task, int ignore_count,
8864 const struct breakpoint_ops *ops, int from_tty,
8865 int enabled, int internal, unsigned flags,
8866 int display_canonical)
8867 {
8868 int i;
8869
8870 if (type == bp_hardware_breakpoint)
8871 {
8872 int target_resources_ok;
8873
8874 i = hw_breakpoint_used_count ();
8875 target_resources_ok =
8876 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8877 i + 1, 0);
8878 if (target_resources_ok == 0)
8879 error (_("No hardware breakpoint support in the target."));
8880 else if (target_resources_ok < 0)
8881 error (_("Hardware breakpoints used exceeds limit."));
8882 }
8883
8884 gdb_assert (!sals.empty ());
8885
8886 for (const auto &sal : sals)
8887 {
8888 struct bp_location *loc;
8889
8890 if (from_tty)
8891 {
8892 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8893 if (!loc_gdbarch)
8894 loc_gdbarch = gdbarch;
8895
8896 describe_other_breakpoints (loc_gdbarch,
8897 sal.pspace, sal.pc, sal.section, thread);
8898 }
8899
8900 if (&sal == &sals[0])
8901 {
8902 init_raw_breakpoint (b, gdbarch, sal, type, ops);
8903 b->thread = thread;
8904 b->task = task;
8905
8906 b->cond_string = cond_string.release ();
8907 b->extra_string = extra_string.release ();
8908 b->ignore_count = ignore_count;
8909 b->enable_state = enabled ? bp_enabled : bp_disabled;
8910 b->disposition = disposition;
8911
8912 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8913 b->loc->inserted = 1;
8914
8915 if (type == bp_static_tracepoint)
8916 {
8917 struct tracepoint *t = (struct tracepoint *) b;
8918 struct static_tracepoint_marker marker;
8919
8920 if (strace_marker_p (b))
8921 {
8922 /* We already know the marker exists, otherwise, we
8923 wouldn't see a sal for it. */
8924 const char *p
8925 = &event_location_to_string (b->location.get ())[3];
8926 const char *endp;
8927
8928 p = skip_spaces (p);
8929
8930 endp = skip_to_space (p);
8931
8932 t->static_trace_marker_id.assign (p, endp - p);
8933
8934 printf_filtered (_("Probed static tracepoint "
8935 "marker \"%s\"\n"),
8936 t->static_trace_marker_id.c_str ());
8937 }
8938 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8939 {
8940 t->static_trace_marker_id = std::move (marker.str_id);
8941
8942 printf_filtered (_("Probed static tracepoint "
8943 "marker \"%s\"\n"),
8944 t->static_trace_marker_id.c_str ());
8945 }
8946 else
8947 warning (_("Couldn't determine the static "
8948 "tracepoint marker to probe"));
8949 }
8950
8951 loc = b->loc;
8952 }
8953 else
8954 {
8955 loc = add_location_to_breakpoint (b, &sal);
8956 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8957 loc->inserted = 1;
8958 }
8959
8960 /* Do not set breakpoint locations conditions yet. As locations
8961 are inserted, they get sorted based on their addresses. Let
8962 the list stabilize to have reliable location numbers. */
8963
8964 /* Dynamic printf requires and uses additional arguments on the
8965 command line, otherwise it's an error. */
8966 if (type == bp_dprintf)
8967 {
8968 if (b->extra_string)
8969 update_dprintf_command_list (b);
8970 else
8971 error (_("Format string required"));
8972 }
8973 else if (b->extra_string)
8974 error (_("Garbage '%s' at end of command"), b->extra_string);
8975 }
8976
8977
8978 /* The order of the locations is now stable. Set the location
8979 condition using the location's number. */
8980 int loc_num = 1;
8981 for (bp_location *loc : b->locations ())
8982 {
8983 if (b->cond_string != nullptr)
8984 set_breakpoint_location_condition (b->cond_string, loc, b->number,
8985 loc_num);
8986
8987 ++loc_num;
8988 }
8989
8990 b->display_canonical = display_canonical;
8991 if (location != NULL)
8992 b->location = std::move (location);
8993 else
8994 b->location = new_address_location (b->loc->address, NULL, 0);
8995 b->filter = std::move (filter);
8996 }
8997
8998 static void
8999 create_breakpoint_sal (struct gdbarch *gdbarch,
9000 gdb::array_view<const symtab_and_line> sals,
9001 event_location_up &&location,
9002 gdb::unique_xmalloc_ptr<char> filter,
9003 gdb::unique_xmalloc_ptr<char> cond_string,
9004 gdb::unique_xmalloc_ptr<char> extra_string,
9005 enum bptype type, enum bpdisp disposition,
9006 int thread, int task, int ignore_count,
9007 const struct breakpoint_ops *ops, int from_tty,
9008 int enabled, int internal, unsigned flags,
9009 int display_canonical)
9010 {
9011 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
9012
9013 init_breakpoint_sal (b.get (), gdbarch,
9014 sals, std::move (location),
9015 std::move (filter),
9016 std::move (cond_string),
9017 std::move (extra_string),
9018 type, disposition,
9019 thread, task, ignore_count,
9020 ops, from_tty,
9021 enabled, internal, flags,
9022 display_canonical);
9023
9024 install_breakpoint (internal, std::move (b), 0);
9025 }
9026
9027 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9028 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9029 value. COND_STRING, if not NULL, specified the condition to be
9030 used for all breakpoints. Essentially the only case where
9031 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9032 function. In that case, it's still not possible to specify
9033 separate conditions for different overloaded functions, so
9034 we take just a single condition string.
9035
9036 NOTE: If the function succeeds, the caller is expected to cleanup
9037 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9038 array contents). If the function fails (error() is called), the
9039 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9040 COND and SALS arrays and each of those arrays contents. */
9041
9042 static void
9043 create_breakpoints_sal (struct gdbarch *gdbarch,
9044 struct linespec_result *canonical,
9045 gdb::unique_xmalloc_ptr<char> cond_string,
9046 gdb::unique_xmalloc_ptr<char> extra_string,
9047 enum bptype type, enum bpdisp disposition,
9048 int thread, int task, int ignore_count,
9049 const struct breakpoint_ops *ops, int from_tty,
9050 int enabled, int internal, unsigned flags)
9051 {
9052 if (canonical->pre_expanded)
9053 gdb_assert (canonical->lsals.size () == 1);
9054
9055 for (const auto &lsal : canonical->lsals)
9056 {
9057 /* Note that 'location' can be NULL in the case of a plain
9058 'break', without arguments. */
9059 event_location_up location
9060 = (canonical->location != NULL
9061 ? copy_event_location (canonical->location.get ()) : NULL);
9062 gdb::unique_xmalloc_ptr<char> filter_string
9063 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
9064
9065 create_breakpoint_sal (gdbarch, lsal.sals,
9066 std::move (location),
9067 std::move (filter_string),
9068 std::move (cond_string),
9069 std::move (extra_string),
9070 type, disposition,
9071 thread, task, ignore_count, ops,
9072 from_tty, enabled, internal, flags,
9073 canonical->special_display);
9074 }
9075 }
9076
9077 /* Parse LOCATION which is assumed to be a SAL specification possibly
9078 followed by conditionals. On return, SALS contains an array of SAL
9079 addresses found. LOCATION points to the end of the SAL (for
9080 linespec locations).
9081
9082 The array and the line spec strings are allocated on the heap, it is
9083 the caller's responsibility to free them. */
9084
9085 static void
9086 parse_breakpoint_sals (struct event_location *location,
9087 struct linespec_result *canonical)
9088 {
9089 struct symtab_and_line cursal;
9090
9091 if (event_location_type (location) == LINESPEC_LOCATION)
9092 {
9093 const char *spec = get_linespec_location (location)->spec_string;
9094
9095 if (spec == NULL)
9096 {
9097 /* The last displayed codepoint, if it's valid, is our default
9098 breakpoint address. */
9099 if (last_displayed_sal_is_valid ())
9100 {
9101 /* Set sal's pspace, pc, symtab, and line to the values
9102 corresponding to the last call to print_frame_info.
9103 Be sure to reinitialize LINE with NOTCURRENT == 0
9104 as the breakpoint line number is inappropriate otherwise.
9105 find_pc_line would adjust PC, re-set it back. */
9106 symtab_and_line sal = get_last_displayed_sal ();
9107 CORE_ADDR pc = sal.pc;
9108
9109 sal = find_pc_line (pc, 0);
9110
9111 /* "break" without arguments is equivalent to "break *PC"
9112 where PC is the last displayed codepoint's address. So
9113 make sure to set sal.explicit_pc to prevent GDB from
9114 trying to expand the list of sals to include all other
9115 instances with the same symtab and line. */
9116 sal.pc = pc;
9117 sal.explicit_pc = 1;
9118
9119 struct linespec_sals lsal;
9120 lsal.sals = {sal};
9121 lsal.canonical = NULL;
9122
9123 canonical->lsals.push_back (std::move (lsal));
9124 return;
9125 }
9126 else
9127 error (_("No default breakpoint address now."));
9128 }
9129 }
9130
9131 /* Force almost all breakpoints to be in terms of the
9132 current_source_symtab (which is decode_line_1's default).
9133 This should produce the results we want almost all of the
9134 time while leaving default_breakpoint_* alone.
9135
9136 ObjC: However, don't match an Objective-C method name which
9137 may have a '+' or '-' succeeded by a '['. */
9138 cursal = get_current_source_symtab_and_line ();
9139 if (last_displayed_sal_is_valid ())
9140 {
9141 const char *spec = NULL;
9142
9143 if (event_location_type (location) == LINESPEC_LOCATION)
9144 spec = get_linespec_location (location)->spec_string;
9145
9146 if (!cursal.symtab
9147 || (spec != NULL
9148 && strchr ("+-", spec[0]) != NULL
9149 && spec[1] != '['))
9150 {
9151 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9152 get_last_displayed_symtab (),
9153 get_last_displayed_line (),
9154 canonical, NULL, NULL);
9155 return;
9156 }
9157 }
9158
9159 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9160 cursal.symtab, cursal.line, canonical, NULL, NULL);
9161 }
9162
9163
9164 /* Convert each SAL into a real PC. Verify that the PC can be
9165 inserted as a breakpoint. If it can't throw an error. */
9166
9167 static void
9168 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
9169 {
9170 for (auto &sal : sals)
9171 resolve_sal_pc (&sal);
9172 }
9173
9174 /* Fast tracepoints may have restrictions on valid locations. For
9175 instance, a fast tracepoint using a jump instead of a trap will
9176 likely have to overwrite more bytes than a trap would, and so can
9177 only be placed where the instruction is longer than the jump, or a
9178 multi-instruction sequence does not have a jump into the middle of
9179 it, etc. */
9180
9181 static void
9182 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9183 gdb::array_view<const symtab_and_line> sals)
9184 {
9185 for (const auto &sal : sals)
9186 {
9187 struct gdbarch *sarch;
9188
9189 sarch = get_sal_arch (sal);
9190 /* We fall back to GDBARCH if there is no architecture
9191 associated with SAL. */
9192 if (sarch == NULL)
9193 sarch = gdbarch;
9194 std::string msg;
9195 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
9196 error (_("May not have a fast tracepoint at %s%s"),
9197 paddress (sarch, sal.pc), msg.c_str ());
9198 }
9199 }
9200
9201 /* Given TOK, a string specification of condition and thread, as
9202 accepted by the 'break' command, extract the condition
9203 string and thread number and set *COND_STRING and *THREAD.
9204 PC identifies the context at which the condition should be parsed.
9205 If no condition is found, *COND_STRING is set to NULL.
9206 If no thread is found, *THREAD is set to -1. */
9207
9208 static void
9209 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9210 char **cond_string, int *thread, int *task,
9211 char **rest)
9212 {
9213 *cond_string = NULL;
9214 *thread = -1;
9215 *task = 0;
9216 *rest = NULL;
9217 bool force = false;
9218
9219 while (tok && *tok)
9220 {
9221 const char *end_tok;
9222 int toklen;
9223 const char *cond_start = NULL;
9224 const char *cond_end = NULL;
9225
9226 tok = skip_spaces (tok);
9227
9228 if ((*tok == '"' || *tok == ',') && rest)
9229 {
9230 *rest = savestring (tok, strlen (tok));
9231 return;
9232 }
9233
9234 end_tok = skip_to_space (tok);
9235
9236 toklen = end_tok - tok;
9237
9238 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9239 {
9240 tok = cond_start = end_tok + 1;
9241 try
9242 {
9243 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9244 }
9245 catch (const gdb_exception_error &)
9246 {
9247 if (!force)
9248 throw;
9249 else
9250 tok = tok + strlen (tok);
9251 }
9252 cond_end = tok;
9253 *cond_string = savestring (cond_start, cond_end - cond_start);
9254 }
9255 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
9256 {
9257 tok = tok + toklen;
9258 force = true;
9259 }
9260 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9261 {
9262 const char *tmptok;
9263 struct thread_info *thr;
9264
9265 tok = end_tok + 1;
9266 thr = parse_thread_id (tok, &tmptok);
9267 if (tok == tmptok)
9268 error (_("Junk after thread keyword."));
9269 *thread = thr->global_num;
9270 tok = tmptok;
9271 }
9272 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9273 {
9274 char *tmptok;
9275
9276 tok = end_tok + 1;
9277 *task = strtol (tok, &tmptok, 0);
9278 if (tok == tmptok)
9279 error (_("Junk after task keyword."));
9280 if (!valid_task_id (*task))
9281 error (_("Unknown task %d."), *task);
9282 tok = tmptok;
9283 }
9284 else if (rest)
9285 {
9286 *rest = savestring (tok, strlen (tok));
9287 return;
9288 }
9289 else
9290 error (_("Junk at end of arguments."));
9291 }
9292 }
9293
9294 /* Call 'find_condition_and_thread' for each sal in SALS until a parse
9295 succeeds. The parsed values are written to COND_STRING, THREAD,
9296 TASK, and REST. See the comment of 'find_condition_and_thread'
9297 for the description of these parameters and INPUT. */
9298
9299 static void
9300 find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
9301 const char *input, char **cond_string,
9302 int *thread, int *task, char **rest)
9303 {
9304 int num_failures = 0;
9305 for (auto &sal : sals)
9306 {
9307 char *cond = nullptr;
9308 int thread_id = 0;
9309 int task_id = 0;
9310 char *remaining = nullptr;
9311
9312 /* Here we want to parse 'arg' to separate condition from thread
9313 number. But because parsing happens in a context and the
9314 contexts of sals might be different, try each until there is
9315 success. Finding one successful parse is sufficient for our
9316 goal. When setting the breakpoint we'll re-parse the
9317 condition in the context of each sal. */
9318 try
9319 {
9320 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
9321 &task_id, &remaining);
9322 *cond_string = cond;
9323 *thread = thread_id;
9324 *task = task_id;
9325 *rest = remaining;
9326 break;
9327 }
9328 catch (const gdb_exception_error &e)
9329 {
9330 num_failures++;
9331 /* If no sal remains, do not continue. */
9332 if (num_failures == sals.size ())
9333 throw;
9334 }
9335 }
9336 }
9337
9338 /* Decode a static tracepoint marker spec. */
9339
9340 static std::vector<symtab_and_line>
9341 decode_static_tracepoint_spec (const char **arg_p)
9342 {
9343 const char *p = &(*arg_p)[3];
9344 const char *endp;
9345
9346 p = skip_spaces (p);
9347
9348 endp = skip_to_space (p);
9349
9350 std::string marker_str (p, endp - p);
9351
9352 std::vector<static_tracepoint_marker> markers
9353 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9354 if (markers.empty ())
9355 error (_("No known static tracepoint marker named %s"),
9356 marker_str.c_str ());
9357
9358 std::vector<symtab_and_line> sals;
9359 sals.reserve (markers.size ());
9360
9361 for (const static_tracepoint_marker &marker : markers)
9362 {
9363 symtab_and_line sal = find_pc_line (marker.address, 0);
9364 sal.pc = marker.address;
9365 sals.push_back (sal);
9366 }
9367
9368 *arg_p = endp;
9369 return sals;
9370 }
9371
9372 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9373 according to IS_TRACEPOINT. */
9374
9375 static const struct breakpoint_ops *
9376 breakpoint_ops_for_event_location_type (enum event_location_type location_type,
9377 bool is_tracepoint)
9378 {
9379 if (is_tracepoint)
9380 {
9381 if (location_type == PROBE_LOCATION)
9382 return &tracepoint_probe_breakpoint_ops;
9383 else
9384 return &tracepoint_breakpoint_ops;
9385 }
9386 else
9387 {
9388 if (location_type == PROBE_LOCATION)
9389 return &bkpt_probe_breakpoint_ops;
9390 else
9391 return &bkpt_breakpoint_ops;
9392 }
9393 }
9394
9395 /* See breakpoint.h. */
9396
9397 const struct breakpoint_ops *
9398 breakpoint_ops_for_event_location (const struct event_location *location,
9399 bool is_tracepoint)
9400 {
9401 if (location != nullptr)
9402 return breakpoint_ops_for_event_location_type
9403 (event_location_type (location), is_tracepoint);
9404 return is_tracepoint ? &tracepoint_breakpoint_ops : &bkpt_breakpoint_ops;
9405 }
9406
9407 /* See breakpoint.h. */
9408
9409 int
9410 create_breakpoint (struct gdbarch *gdbarch,
9411 struct event_location *location,
9412 const char *cond_string,
9413 int thread, const char *extra_string,
9414 bool force_condition, int parse_extra,
9415 int tempflag, enum bptype type_wanted,
9416 int ignore_count,
9417 enum auto_boolean pending_break_support,
9418 const struct breakpoint_ops *ops,
9419 int from_tty, int enabled, int internal,
9420 unsigned flags)
9421 {
9422 struct linespec_result canonical;
9423 int pending = 0;
9424 int task = 0;
9425 int prev_bkpt_count = breakpoint_count;
9426
9427 gdb_assert (ops != NULL);
9428
9429 /* If extra_string isn't useful, set it to NULL. */
9430 if (extra_string != NULL && *extra_string == '\0')
9431 extra_string = NULL;
9432
9433 try
9434 {
9435 ops->create_sals_from_location (location, &canonical, type_wanted);
9436 }
9437 catch (const gdb_exception_error &e)
9438 {
9439 /* If caller is interested in rc value from parse, set
9440 value. */
9441 if (e.error == NOT_FOUND_ERROR)
9442 {
9443 /* If pending breakpoint support is turned off, throw
9444 error. */
9445
9446 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9447 throw;
9448
9449 exception_print (gdb_stderr, e);
9450
9451 /* If pending breakpoint support is auto query and the user
9452 selects no, then simply return the error code. */
9453 if (pending_break_support == AUTO_BOOLEAN_AUTO
9454 && !nquery (_("Make %s pending on future shared library load? "),
9455 bptype_string (type_wanted)))
9456 return 0;
9457
9458 /* At this point, either the user was queried about setting
9459 a pending breakpoint and selected yes, or pending
9460 breakpoint behavior is on and thus a pending breakpoint
9461 is defaulted on behalf of the user. */
9462 pending = 1;
9463 }
9464 else
9465 throw;
9466 }
9467
9468 if (!pending && canonical.lsals.empty ())
9469 return 0;
9470
9471 /* Resolve all line numbers to PC's and verify that the addresses
9472 are ok for the target. */
9473 if (!pending)
9474 {
9475 for (auto &lsal : canonical.lsals)
9476 breakpoint_sals_to_pc (lsal.sals);
9477 }
9478
9479 /* Fast tracepoints may have additional restrictions on location. */
9480 if (!pending && type_wanted == bp_fast_tracepoint)
9481 {
9482 for (const auto &lsal : canonical.lsals)
9483 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9484 }
9485
9486 /* Verify that condition can be parsed, before setting any
9487 breakpoints. Allocate a separate condition expression for each
9488 breakpoint. */
9489 if (!pending)
9490 {
9491 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9492 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9493
9494 if (parse_extra)
9495 {
9496 char *rest;
9497 char *cond;
9498
9499 const linespec_sals &lsal = canonical.lsals[0];
9500
9501 find_condition_and_thread_for_sals (lsal.sals, extra_string,
9502 &cond, &thread, &task, &rest);
9503 cond_string_copy.reset (cond);
9504 extra_string_copy.reset (rest);
9505 }
9506 else
9507 {
9508 if (type_wanted != bp_dprintf
9509 && extra_string != NULL && *extra_string != '\0')
9510 error (_("Garbage '%s' at end of location"), extra_string);
9511
9512 /* Check the validity of the condition. We should error out
9513 if the condition is invalid at all of the locations and
9514 if it is not forced. In the PARSE_EXTRA case above, this
9515 check is done when parsing the EXTRA_STRING. */
9516 if (cond_string != nullptr && !force_condition)
9517 {
9518 int num_failures = 0;
9519 const linespec_sals &lsal = canonical.lsals[0];
9520 for (const auto &sal : lsal.sals)
9521 {
9522 const char *cond = cond_string;
9523 try
9524 {
9525 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
9526 /* One success is sufficient to keep going. */
9527 break;
9528 }
9529 catch (const gdb_exception_error &)
9530 {
9531 num_failures++;
9532 /* If this is the last sal, error out. */
9533 if (num_failures == lsal.sals.size ())
9534 throw;
9535 }
9536 }
9537 }
9538
9539 /* Create a private copy of condition string. */
9540 if (cond_string)
9541 cond_string_copy.reset (xstrdup (cond_string));
9542 /* Create a private copy of any extra string. */
9543 if (extra_string)
9544 extra_string_copy.reset (xstrdup (extra_string));
9545 }
9546
9547 ops->create_breakpoints_sal (gdbarch, &canonical,
9548 std::move (cond_string_copy),
9549 std::move (extra_string_copy),
9550 type_wanted,
9551 tempflag ? disp_del : disp_donttouch,
9552 thread, task, ignore_count, ops,
9553 from_tty, enabled, internal, flags);
9554 }
9555 else
9556 {
9557 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9558
9559 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9560 b->location = copy_event_location (location);
9561
9562 if (parse_extra)
9563 b->cond_string = NULL;
9564 else
9565 {
9566 /* Create a private copy of condition string. */
9567 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
9568 b->thread = thread;
9569 }
9570
9571 /* Create a private copy of any extra string. */
9572 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
9573 b->ignore_count = ignore_count;
9574 b->disposition = tempflag ? disp_del : disp_donttouch;
9575 b->condition_not_parsed = 1;
9576 b->enable_state = enabled ? bp_enabled : bp_disabled;
9577 if ((type_wanted != bp_breakpoint
9578 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9579 b->pspace = current_program_space;
9580
9581 install_breakpoint (internal, std::move (b), 0);
9582 }
9583
9584 if (canonical.lsals.size () > 1)
9585 {
9586 warning (_("Multiple breakpoints were set.\nUse the "
9587 "\"delete\" command to delete unwanted breakpoints."));
9588 prev_breakpoint_count = prev_bkpt_count;
9589 }
9590
9591 update_global_location_list (UGLL_MAY_INSERT);
9592
9593 return 1;
9594 }
9595
9596 /* Set a breakpoint.
9597 ARG is a string describing breakpoint address,
9598 condition, and thread.
9599 FLAG specifies if a breakpoint is hardware on,
9600 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9601 and BP_TEMPFLAG. */
9602
9603 static void
9604 break_command_1 (const char *arg, int flag, int from_tty)
9605 {
9606 int tempflag = flag & BP_TEMPFLAG;
9607 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9608 ? bp_hardware_breakpoint
9609 : bp_breakpoint);
9610
9611 event_location_up location = string_to_event_location (&arg, current_language);
9612 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9613 (location.get (), false /* is_tracepoint */);
9614
9615 create_breakpoint (get_current_arch (),
9616 location.get (),
9617 NULL, 0, arg, false, 1 /* parse arg */,
9618 tempflag, type_wanted,
9619 0 /* Ignore count */,
9620 pending_break_support,
9621 ops,
9622 from_tty,
9623 1 /* enabled */,
9624 0 /* internal */,
9625 0);
9626 }
9627
9628 /* Helper function for break_command_1 and disassemble_command. */
9629
9630 void
9631 resolve_sal_pc (struct symtab_and_line *sal)
9632 {
9633 CORE_ADDR pc;
9634
9635 if (sal->pc == 0 && sal->symtab != NULL)
9636 {
9637 if (!find_line_pc (sal->symtab, sal->line, &pc))
9638 error (_("No line %d in file \"%s\"."),
9639 sal->line, symtab_to_filename_for_display (sal->symtab));
9640 sal->pc = pc;
9641
9642 /* If this SAL corresponds to a breakpoint inserted using a line
9643 number, then skip the function prologue if necessary. */
9644 if (sal->explicit_line)
9645 skip_prologue_sal (sal);
9646 }
9647
9648 if (sal->section == 0 && sal->symtab != NULL)
9649 {
9650 const struct blockvector *bv;
9651 const struct block *b;
9652 struct symbol *sym;
9653
9654 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9655 SYMTAB_COMPUNIT (sal->symtab));
9656 if (bv != NULL)
9657 {
9658 sym = block_linkage_function (b);
9659 if (sym != NULL)
9660 {
9661 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9662 sal->section = sym->obj_section (SYMTAB_OBJFILE (sal->symtab));
9663 }
9664 else
9665 {
9666 /* It really is worthwhile to have the section, so we'll
9667 just have to look harder. This case can be executed
9668 if we have line numbers but no functions (as can
9669 happen in assembly source). */
9670
9671 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9672 switch_to_program_space_and_thread (sal->pspace);
9673
9674 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9675 if (msym.minsym)
9676 sal->section = msym.obj_section ();
9677 }
9678 }
9679 }
9680 }
9681
9682 void
9683 break_command (const char *arg, int from_tty)
9684 {
9685 break_command_1 (arg, 0, from_tty);
9686 }
9687
9688 void
9689 tbreak_command (const char *arg, int from_tty)
9690 {
9691 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9692 }
9693
9694 static void
9695 hbreak_command (const char *arg, int from_tty)
9696 {
9697 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9698 }
9699
9700 static void
9701 thbreak_command (const char *arg, int from_tty)
9702 {
9703 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9704 }
9705
9706 static void
9707 stop_command (const char *arg, int from_tty)
9708 {
9709 printf_filtered (_("Specify the type of breakpoint to set.\n\
9710 Usage: stop in <function | address>\n\
9711 stop at <line>\n"));
9712 }
9713
9714 static void
9715 stopin_command (const char *arg, int from_tty)
9716 {
9717 int badInput = 0;
9718
9719 if (arg == NULL)
9720 badInput = 1;
9721 else if (*arg != '*')
9722 {
9723 const char *argptr = arg;
9724 int hasColon = 0;
9725
9726 /* Look for a ':'. If this is a line number specification, then
9727 say it is bad, otherwise, it should be an address or
9728 function/method name. */
9729 while (*argptr && !hasColon)
9730 {
9731 hasColon = (*argptr == ':');
9732 argptr++;
9733 }
9734
9735 if (hasColon)
9736 badInput = (*argptr != ':'); /* Not a class::method */
9737 else
9738 badInput = isdigit (*arg); /* a simple line number */
9739 }
9740
9741 if (badInput)
9742 printf_filtered (_("Usage: stop in <function | address>\n"));
9743 else
9744 break_command_1 (arg, 0, from_tty);
9745 }
9746
9747 static void
9748 stopat_command (const char *arg, int from_tty)
9749 {
9750 int badInput = 0;
9751
9752 if (arg == NULL || *arg == '*') /* no line number */
9753 badInput = 1;
9754 else
9755 {
9756 const char *argptr = arg;
9757 int hasColon = 0;
9758
9759 /* Look for a ':'. If there is a '::' then get out, otherwise
9760 it is probably a line number. */
9761 while (*argptr && !hasColon)
9762 {
9763 hasColon = (*argptr == ':');
9764 argptr++;
9765 }
9766
9767 if (hasColon)
9768 badInput = (*argptr == ':'); /* we have class::method */
9769 else
9770 badInput = !isdigit (*arg); /* not a line number */
9771 }
9772
9773 if (badInput)
9774 printf_filtered (_("Usage: stop at LINE\n"));
9775 else
9776 break_command_1 (arg, 0, from_tty);
9777 }
9778
9779 /* The dynamic printf command is mostly like a regular breakpoint, but
9780 with a prewired command list consisting of a single output command,
9781 built from extra arguments supplied on the dprintf command
9782 line. */
9783
9784 static void
9785 dprintf_command (const char *arg, int from_tty)
9786 {
9787 event_location_up location = string_to_event_location (&arg, current_language);
9788
9789 /* If non-NULL, ARG should have been advanced past the location;
9790 the next character must be ','. */
9791 if (arg != NULL)
9792 {
9793 if (arg[0] != ',' || arg[1] == '\0')
9794 error (_("Format string required"));
9795 else
9796 {
9797 /* Skip the comma. */
9798 ++arg;
9799 }
9800 }
9801
9802 create_breakpoint (get_current_arch (),
9803 location.get (),
9804 NULL, 0, arg, false, 1 /* parse arg */,
9805 0, bp_dprintf,
9806 0 /* Ignore count */,
9807 pending_break_support,
9808 &dprintf_breakpoint_ops,
9809 from_tty,
9810 1 /* enabled */,
9811 0 /* internal */,
9812 0);
9813 }
9814
9815 static void
9816 agent_printf_command (const char *arg, int from_tty)
9817 {
9818 error (_("May only run agent-printf on the target"));
9819 }
9820
9821 /* Implement the "breakpoint_hit" breakpoint_ops method for
9822 ranged breakpoints. */
9823
9824 static int
9825 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9826 const address_space *aspace,
9827 CORE_ADDR bp_addr,
9828 const struct target_waitstatus *ws)
9829 {
9830 if (ws->kind != TARGET_WAITKIND_STOPPED
9831 || ws->value.sig != GDB_SIGNAL_TRAP)
9832 return 0;
9833
9834 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9835 bl->length, aspace, bp_addr);
9836 }
9837
9838 /* Implement the "resources_needed" breakpoint_ops method for
9839 ranged breakpoints. */
9840
9841 static int
9842 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9843 {
9844 return target_ranged_break_num_registers ();
9845 }
9846
9847 /* Implement the "print_it" breakpoint_ops method for
9848 ranged breakpoints. */
9849
9850 static enum print_stop_action
9851 print_it_ranged_breakpoint (bpstat bs)
9852 {
9853 struct breakpoint *b = bs->breakpoint_at;
9854 struct bp_location *bl = b->loc;
9855 struct ui_out *uiout = current_uiout;
9856
9857 gdb_assert (b->type == bp_hardware_breakpoint);
9858
9859 /* Ranged breakpoints have only one location. */
9860 gdb_assert (bl && bl->next == NULL);
9861
9862 annotate_breakpoint (b->number);
9863
9864 maybe_print_thread_hit_breakpoint (uiout);
9865
9866 if (b->disposition == disp_del)
9867 uiout->text ("Temporary ranged breakpoint ");
9868 else
9869 uiout->text ("Ranged breakpoint ");
9870 if (uiout->is_mi_like_p ())
9871 {
9872 uiout->field_string ("reason",
9873 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9874 uiout->field_string ("disp", bpdisp_text (b->disposition));
9875 }
9876 uiout->field_signed ("bkptno", b->number);
9877 uiout->text (", ");
9878
9879 return PRINT_SRC_AND_LOC;
9880 }
9881
9882 /* Implement the "print_one" breakpoint_ops method for
9883 ranged breakpoints. */
9884
9885 static void
9886 print_one_ranged_breakpoint (struct breakpoint *b,
9887 struct bp_location **last_loc)
9888 {
9889 struct bp_location *bl = b->loc;
9890 struct value_print_options opts;
9891 struct ui_out *uiout = current_uiout;
9892
9893 /* Ranged breakpoints have only one location. */
9894 gdb_assert (bl && bl->next == NULL);
9895
9896 get_user_print_options (&opts);
9897
9898 if (opts.addressprint)
9899 /* We don't print the address range here, it will be printed later
9900 by print_one_detail_ranged_breakpoint. */
9901 uiout->field_skip ("addr");
9902 annotate_field (5);
9903 print_breakpoint_location (b, bl);
9904 *last_loc = bl;
9905 }
9906
9907 /* Implement the "print_one_detail" breakpoint_ops method for
9908 ranged breakpoints. */
9909
9910 static void
9911 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9912 struct ui_out *uiout)
9913 {
9914 CORE_ADDR address_start, address_end;
9915 struct bp_location *bl = b->loc;
9916 string_file stb;
9917
9918 gdb_assert (bl);
9919
9920 address_start = bl->address;
9921 address_end = address_start + bl->length - 1;
9922
9923 uiout->text ("\taddress range: ");
9924 stb.printf ("[%s, %s]",
9925 print_core_address (bl->gdbarch, address_start),
9926 print_core_address (bl->gdbarch, address_end));
9927 uiout->field_stream ("addr", stb);
9928 uiout->text ("\n");
9929 }
9930
9931 /* Implement the "print_mention" breakpoint_ops method for
9932 ranged breakpoints. */
9933
9934 static void
9935 print_mention_ranged_breakpoint (struct breakpoint *b)
9936 {
9937 struct bp_location *bl = b->loc;
9938 struct ui_out *uiout = current_uiout;
9939
9940 gdb_assert (bl);
9941 gdb_assert (b->type == bp_hardware_breakpoint);
9942
9943 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9944 b->number, paddress (bl->gdbarch, bl->address),
9945 paddress (bl->gdbarch, bl->address + bl->length - 1));
9946 }
9947
9948 /* Implement the "print_recreate" breakpoint_ops method for
9949 ranged breakpoints. */
9950
9951 static void
9952 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9953 {
9954 fprintf_unfiltered (fp, "break-range %s, %s",
9955 event_location_to_string (b->location.get ()),
9956 event_location_to_string (b->location_range_end.get ()));
9957 print_recreate_thread (b, fp);
9958 }
9959
9960 /* The breakpoint_ops structure to be used in ranged breakpoints. */
9961
9962 static struct breakpoint_ops ranged_breakpoint_ops;
9963
9964 /* Find the address where the end of the breakpoint range should be
9965 placed, given the SAL of the end of the range. This is so that if
9966 the user provides a line number, the end of the range is set to the
9967 last instruction of the given line. */
9968
9969 static CORE_ADDR
9970 find_breakpoint_range_end (struct symtab_and_line sal)
9971 {
9972 CORE_ADDR end;
9973
9974 /* If the user provided a PC value, use it. Otherwise,
9975 find the address of the end of the given location. */
9976 if (sal.explicit_pc)
9977 end = sal.pc;
9978 else
9979 {
9980 int ret;
9981 CORE_ADDR start;
9982
9983 ret = find_line_pc_range (sal, &start, &end);
9984 if (!ret)
9985 error (_("Could not find location of the end of the range."));
9986
9987 /* find_line_pc_range returns the start of the next line. */
9988 end--;
9989 }
9990
9991 return end;
9992 }
9993
9994 /* Implement the "break-range" CLI command. */
9995
9996 static void
9997 break_range_command (const char *arg, int from_tty)
9998 {
9999 const char *arg_start;
10000 struct linespec_result canonical_start, canonical_end;
10001 int bp_count, can_use_bp, length;
10002 CORE_ADDR end;
10003 struct breakpoint *b;
10004
10005 /* We don't support software ranged breakpoints. */
10006 if (target_ranged_break_num_registers () < 0)
10007 error (_("This target does not support hardware ranged breakpoints."));
10008
10009 bp_count = hw_breakpoint_used_count ();
10010 bp_count += target_ranged_break_num_registers ();
10011 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10012 bp_count, 0);
10013 if (can_use_bp < 0)
10014 error (_("Hardware breakpoints used exceeds limit."));
10015
10016 arg = skip_spaces (arg);
10017 if (arg == NULL || arg[0] == '\0')
10018 error(_("No address range specified."));
10019
10020 arg_start = arg;
10021 event_location_up start_location = string_to_event_location (&arg,
10022 current_language);
10023 parse_breakpoint_sals (start_location.get (), &canonical_start);
10024
10025 if (arg[0] != ',')
10026 error (_("Too few arguments."));
10027 else if (canonical_start.lsals.empty ())
10028 error (_("Could not find location of the beginning of the range."));
10029
10030 const linespec_sals &lsal_start = canonical_start.lsals[0];
10031
10032 if (canonical_start.lsals.size () > 1
10033 || lsal_start.sals.size () != 1)
10034 error (_("Cannot create a ranged breakpoint with multiple locations."));
10035
10036 const symtab_and_line &sal_start = lsal_start.sals[0];
10037 std::string addr_string_start (arg_start, arg - arg_start);
10038
10039 arg++; /* Skip the comma. */
10040 arg = skip_spaces (arg);
10041
10042 /* Parse the end location. */
10043
10044 arg_start = arg;
10045
10046 /* We call decode_line_full directly here instead of using
10047 parse_breakpoint_sals because we need to specify the start location's
10048 symtab and line as the default symtab and line for the end of the
10049 range. This makes it possible to have ranges like "foo.c:27, +14",
10050 where +14 means 14 lines from the start location. */
10051 event_location_up end_location = string_to_event_location (&arg,
10052 current_language);
10053 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10054 sal_start.symtab, sal_start.line,
10055 &canonical_end, NULL, NULL);
10056
10057 if (canonical_end.lsals.empty ())
10058 error (_("Could not find location of the end of the range."));
10059
10060 const linespec_sals &lsal_end = canonical_end.lsals[0];
10061 if (canonical_end.lsals.size () > 1
10062 || lsal_end.sals.size () != 1)
10063 error (_("Cannot create a ranged breakpoint with multiple locations."));
10064
10065 const symtab_and_line &sal_end = lsal_end.sals[0];
10066
10067 end = find_breakpoint_range_end (sal_end);
10068 if (sal_start.pc > end)
10069 error (_("Invalid address range, end precedes start."));
10070
10071 length = end - sal_start.pc + 1;
10072 if (length < 0)
10073 /* Length overflowed. */
10074 error (_("Address range too large."));
10075 else if (length == 1)
10076 {
10077 /* This range is simple enough to be handled by
10078 the `hbreak' command. */
10079 hbreak_command (&addr_string_start[0], 1);
10080
10081 return;
10082 }
10083
10084 /* Now set up the breakpoint. */
10085 b = set_raw_breakpoint (get_current_arch (), sal_start,
10086 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10087 set_breakpoint_count (breakpoint_count + 1);
10088 b->number = breakpoint_count;
10089 b->disposition = disp_donttouch;
10090 b->location = std::move (start_location);
10091 b->location_range_end = std::move (end_location);
10092 b->loc->length = length;
10093
10094 mention (b);
10095 gdb::observers::breakpoint_created.notify (b);
10096 update_global_location_list (UGLL_MAY_INSERT);
10097 }
10098
10099 /* Return non-zero if EXP is verified as constant. Returned zero
10100 means EXP is variable. Also the constant detection may fail for
10101 some constant expressions and in such case still falsely return
10102 zero. */
10103
10104 static bool
10105 watchpoint_exp_is_const (const struct expression *exp)
10106 {
10107 return exp->op->constant_p ();
10108 }
10109
10110 /* Watchpoint destructor. */
10111
10112 watchpoint::~watchpoint ()
10113 {
10114 xfree (this->exp_string);
10115 xfree (this->exp_string_reparse);
10116 }
10117
10118 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10119
10120 static void
10121 re_set_watchpoint (struct breakpoint *b)
10122 {
10123 struct watchpoint *w = (struct watchpoint *) b;
10124
10125 /* Watchpoint can be either on expression using entirely global
10126 variables, or it can be on local variables.
10127
10128 Watchpoints of the first kind are never auto-deleted, and even
10129 persist across program restarts. Since they can use variables
10130 from shared libraries, we need to reparse expression as libraries
10131 are loaded and unloaded.
10132
10133 Watchpoints on local variables can also change meaning as result
10134 of solib event. For example, if a watchpoint uses both a local
10135 and a global variables in expression, it's a local watchpoint,
10136 but unloading of a shared library will make the expression
10137 invalid. This is not a very common use case, but we still
10138 re-evaluate expression, to avoid surprises to the user.
10139
10140 Note that for local watchpoints, we re-evaluate it only if
10141 watchpoints frame id is still valid. If it's not, it means the
10142 watchpoint is out of scope and will be deleted soon. In fact,
10143 I'm not sure we'll ever be called in this case.
10144
10145 If a local watchpoint's frame id is still valid, then
10146 w->exp_valid_block is likewise valid, and we can safely use it.
10147
10148 Don't do anything about disabled watchpoints, since they will be
10149 reevaluated again when enabled. */
10150 update_watchpoint (w, 1 /* reparse */);
10151 }
10152
10153 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10154
10155 static int
10156 insert_watchpoint (struct bp_location *bl)
10157 {
10158 struct watchpoint *w = (struct watchpoint *) bl->owner;
10159 int length = w->exact ? 1 : bl->length;
10160
10161 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10162 w->cond_exp.get ());
10163 }
10164
10165 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10166
10167 static int
10168 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10169 {
10170 struct watchpoint *w = (struct watchpoint *) bl->owner;
10171 int length = w->exact ? 1 : bl->length;
10172
10173 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10174 w->cond_exp.get ());
10175 }
10176
10177 static int
10178 breakpoint_hit_watchpoint (const struct bp_location *bl,
10179 const address_space *aspace, CORE_ADDR bp_addr,
10180 const struct target_waitstatus *ws)
10181 {
10182 struct breakpoint *b = bl->owner;
10183 struct watchpoint *w = (struct watchpoint *) b;
10184
10185 /* Continuable hardware watchpoints are treated as non-existent if the
10186 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10187 some data address). Otherwise gdb won't stop on a break instruction
10188 in the code (not from a breakpoint) when a hardware watchpoint has
10189 been defined. Also skip watchpoints which we know did not trigger
10190 (did not match the data address). */
10191 if (is_hardware_watchpoint (b)
10192 && w->watchpoint_triggered == watch_triggered_no)
10193 return 0;
10194
10195 return 1;
10196 }
10197
10198 static void
10199 check_status_watchpoint (bpstat bs)
10200 {
10201 gdb_assert (is_watchpoint (bs->breakpoint_at));
10202
10203 bpstat_check_watchpoint (bs);
10204 }
10205
10206 /* Implement the "resources_needed" breakpoint_ops method for
10207 hardware watchpoints. */
10208
10209 static int
10210 resources_needed_watchpoint (const struct bp_location *bl)
10211 {
10212 struct watchpoint *w = (struct watchpoint *) bl->owner;
10213 int length = w->exact? 1 : bl->length;
10214
10215 return target_region_ok_for_hw_watchpoint (bl->address, length);
10216 }
10217
10218 /* Implement the "works_in_software_mode" breakpoint_ops method for
10219 hardware watchpoints. */
10220
10221 static int
10222 works_in_software_mode_watchpoint (const struct breakpoint *b)
10223 {
10224 /* Read and access watchpoints only work with hardware support. */
10225 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10226 }
10227
10228 static enum print_stop_action
10229 print_it_watchpoint (bpstat bs)
10230 {
10231 struct breakpoint *b;
10232 enum print_stop_action result;
10233 struct watchpoint *w;
10234 struct ui_out *uiout = current_uiout;
10235
10236 gdb_assert (bs->bp_location_at != NULL);
10237
10238 b = bs->breakpoint_at;
10239 w = (struct watchpoint *) b;
10240
10241 annotate_watchpoint (b->number);
10242 maybe_print_thread_hit_breakpoint (uiout);
10243
10244 string_file stb;
10245
10246 gdb::optional<ui_out_emit_tuple> tuple_emitter;
10247 switch (b->type)
10248 {
10249 case bp_watchpoint:
10250 case bp_hardware_watchpoint:
10251 if (uiout->is_mi_like_p ())
10252 uiout->field_string
10253 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10254 mention (b);
10255 tuple_emitter.emplace (uiout, "value");
10256 uiout->text ("\nOld value = ");
10257 watchpoint_value_print (bs->old_val.get (), &stb);
10258 uiout->field_stream ("old", stb);
10259 uiout->text ("\nNew value = ");
10260 watchpoint_value_print (w->val.get (), &stb);
10261 uiout->field_stream ("new", stb);
10262 uiout->text ("\n");
10263 /* More than one watchpoint may have been triggered. */
10264 result = PRINT_UNKNOWN;
10265 break;
10266
10267 case bp_read_watchpoint:
10268 if (uiout->is_mi_like_p ())
10269 uiout->field_string
10270 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10271 mention (b);
10272 tuple_emitter.emplace (uiout, "value");
10273 uiout->text ("\nValue = ");
10274 watchpoint_value_print (w->val.get (), &stb);
10275 uiout->field_stream ("value", stb);
10276 uiout->text ("\n");
10277 result = PRINT_UNKNOWN;
10278 break;
10279
10280 case bp_access_watchpoint:
10281 if (bs->old_val != NULL)
10282 {
10283 if (uiout->is_mi_like_p ())
10284 uiout->field_string
10285 ("reason",
10286 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10287 mention (b);
10288 tuple_emitter.emplace (uiout, "value");
10289 uiout->text ("\nOld value = ");
10290 watchpoint_value_print (bs->old_val.get (), &stb);
10291 uiout->field_stream ("old", stb);
10292 uiout->text ("\nNew value = ");
10293 }
10294 else
10295 {
10296 mention (b);
10297 if (uiout->is_mi_like_p ())
10298 uiout->field_string
10299 ("reason",
10300 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10301 tuple_emitter.emplace (uiout, "value");
10302 uiout->text ("\nValue = ");
10303 }
10304 watchpoint_value_print (w->val.get (), &stb);
10305 uiout->field_stream ("new", stb);
10306 uiout->text ("\n");
10307 result = PRINT_UNKNOWN;
10308 break;
10309 default:
10310 result = PRINT_UNKNOWN;
10311 }
10312
10313 return result;
10314 }
10315
10316 /* Implement the "print_mention" breakpoint_ops method for hardware
10317 watchpoints. */
10318
10319 static void
10320 print_mention_watchpoint (struct breakpoint *b)
10321 {
10322 struct watchpoint *w = (struct watchpoint *) b;
10323 struct ui_out *uiout = current_uiout;
10324 const char *tuple_name;
10325
10326 switch (b->type)
10327 {
10328 case bp_watchpoint:
10329 uiout->text ("Watchpoint ");
10330 tuple_name = "wpt";
10331 break;
10332 case bp_hardware_watchpoint:
10333 uiout->text ("Hardware watchpoint ");
10334 tuple_name = "wpt";
10335 break;
10336 case bp_read_watchpoint:
10337 uiout->text ("Hardware read watchpoint ");
10338 tuple_name = "hw-rwpt";
10339 break;
10340 case bp_access_watchpoint:
10341 uiout->text ("Hardware access (read/write) watchpoint ");
10342 tuple_name = "hw-awpt";
10343 break;
10344 default:
10345 internal_error (__FILE__, __LINE__,
10346 _("Invalid hardware watchpoint type."));
10347 }
10348
10349 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10350 uiout->field_signed ("number", b->number);
10351 uiout->text (": ");
10352 uiout->field_string ("exp", w->exp_string);
10353 }
10354
10355 /* Implement the "print_recreate" breakpoint_ops method for
10356 watchpoints. */
10357
10358 static void
10359 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10360 {
10361 struct watchpoint *w = (struct watchpoint *) b;
10362
10363 switch (b->type)
10364 {
10365 case bp_watchpoint:
10366 case bp_hardware_watchpoint:
10367 fprintf_unfiltered (fp, "watch");
10368 break;
10369 case bp_read_watchpoint:
10370 fprintf_unfiltered (fp, "rwatch");
10371 break;
10372 case bp_access_watchpoint:
10373 fprintf_unfiltered (fp, "awatch");
10374 break;
10375 default:
10376 internal_error (__FILE__, __LINE__,
10377 _("Invalid watchpoint type."));
10378 }
10379
10380 fprintf_unfiltered (fp, " %s", w->exp_string);
10381 print_recreate_thread (b, fp);
10382 }
10383
10384 /* Implement the "explains_signal" breakpoint_ops method for
10385 watchpoints. */
10386
10387 static int
10388 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10389 {
10390 /* A software watchpoint cannot cause a signal other than
10391 GDB_SIGNAL_TRAP. */
10392 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10393 return 0;
10394
10395 return 1;
10396 }
10397
10398 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10399
10400 static struct breakpoint_ops watchpoint_breakpoint_ops;
10401
10402 /* Implement the "insert" breakpoint_ops method for
10403 masked hardware watchpoints. */
10404
10405 static int
10406 insert_masked_watchpoint (struct bp_location *bl)
10407 {
10408 struct watchpoint *w = (struct watchpoint *) bl->owner;
10409
10410 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10411 bl->watchpoint_type);
10412 }
10413
10414 /* Implement the "remove" breakpoint_ops method for
10415 masked hardware watchpoints. */
10416
10417 static int
10418 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10419 {
10420 struct watchpoint *w = (struct watchpoint *) bl->owner;
10421
10422 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10423 bl->watchpoint_type);
10424 }
10425
10426 /* Implement the "resources_needed" breakpoint_ops method for
10427 masked hardware watchpoints. */
10428
10429 static int
10430 resources_needed_masked_watchpoint (const struct bp_location *bl)
10431 {
10432 struct watchpoint *w = (struct watchpoint *) bl->owner;
10433
10434 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10435 }
10436
10437 /* Implement the "works_in_software_mode" breakpoint_ops method for
10438 masked hardware watchpoints. */
10439
10440 static int
10441 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10442 {
10443 return 0;
10444 }
10445
10446 /* Implement the "print_it" breakpoint_ops method for
10447 masked hardware watchpoints. */
10448
10449 static enum print_stop_action
10450 print_it_masked_watchpoint (bpstat bs)
10451 {
10452 struct breakpoint *b = bs->breakpoint_at;
10453 struct ui_out *uiout = current_uiout;
10454
10455 /* Masked watchpoints have only one location. */
10456 gdb_assert (b->loc && b->loc->next == NULL);
10457
10458 annotate_watchpoint (b->number);
10459 maybe_print_thread_hit_breakpoint (uiout);
10460
10461 switch (b->type)
10462 {
10463 case bp_hardware_watchpoint:
10464 if (uiout->is_mi_like_p ())
10465 uiout->field_string
10466 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10467 break;
10468
10469 case bp_read_watchpoint:
10470 if (uiout->is_mi_like_p ())
10471 uiout->field_string
10472 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10473 break;
10474
10475 case bp_access_watchpoint:
10476 if (uiout->is_mi_like_p ())
10477 uiout->field_string
10478 ("reason",
10479 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10480 break;
10481 default:
10482 internal_error (__FILE__, __LINE__,
10483 _("Invalid hardware watchpoint type."));
10484 }
10485
10486 mention (b);
10487 uiout->text (_("\n\
10488 Check the underlying instruction at PC for the memory\n\
10489 address and value which triggered this watchpoint.\n"));
10490 uiout->text ("\n");
10491
10492 /* More than one watchpoint may have been triggered. */
10493 return PRINT_UNKNOWN;
10494 }
10495
10496 /* Implement the "print_one_detail" breakpoint_ops method for
10497 masked hardware watchpoints. */
10498
10499 static void
10500 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10501 struct ui_out *uiout)
10502 {
10503 struct watchpoint *w = (struct watchpoint *) b;
10504
10505 /* Masked watchpoints have only one location. */
10506 gdb_assert (b->loc && b->loc->next == NULL);
10507
10508 uiout->text ("\tmask ");
10509 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10510 uiout->text ("\n");
10511 }
10512
10513 /* Implement the "print_mention" breakpoint_ops method for
10514 masked hardware watchpoints. */
10515
10516 static void
10517 print_mention_masked_watchpoint (struct breakpoint *b)
10518 {
10519 struct watchpoint *w = (struct watchpoint *) b;
10520 struct ui_out *uiout = current_uiout;
10521 const char *tuple_name;
10522
10523 switch (b->type)
10524 {
10525 case bp_hardware_watchpoint:
10526 uiout->text ("Masked hardware watchpoint ");
10527 tuple_name = "wpt";
10528 break;
10529 case bp_read_watchpoint:
10530 uiout->text ("Masked hardware read watchpoint ");
10531 tuple_name = "hw-rwpt";
10532 break;
10533 case bp_access_watchpoint:
10534 uiout->text ("Masked hardware access (read/write) watchpoint ");
10535 tuple_name = "hw-awpt";
10536 break;
10537 default:
10538 internal_error (__FILE__, __LINE__,
10539 _("Invalid hardware watchpoint type."));
10540 }
10541
10542 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10543 uiout->field_signed ("number", b->number);
10544 uiout->text (": ");
10545 uiout->field_string ("exp", w->exp_string);
10546 }
10547
10548 /* Implement the "print_recreate" breakpoint_ops method for
10549 masked hardware watchpoints. */
10550
10551 static void
10552 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10553 {
10554 struct watchpoint *w = (struct watchpoint *) b;
10555
10556 switch (b->type)
10557 {
10558 case bp_hardware_watchpoint:
10559 fprintf_unfiltered (fp, "watch");
10560 break;
10561 case bp_read_watchpoint:
10562 fprintf_unfiltered (fp, "rwatch");
10563 break;
10564 case bp_access_watchpoint:
10565 fprintf_unfiltered (fp, "awatch");
10566 break;
10567 default:
10568 internal_error (__FILE__, __LINE__,
10569 _("Invalid hardware watchpoint type."));
10570 }
10571
10572 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string,
10573 phex (w->hw_wp_mask, sizeof (CORE_ADDR)));
10574 print_recreate_thread (b, fp);
10575 }
10576
10577 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10578
10579 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10580
10581 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10582
10583 static bool
10584 is_masked_watchpoint (const struct breakpoint *b)
10585 {
10586 return b->ops == &masked_watchpoint_breakpoint_ops;
10587 }
10588
10589 /* accessflag: hw_write: watch write,
10590 hw_read: watch read,
10591 hw_access: watch access (read or write) */
10592 static void
10593 watch_command_1 (const char *arg, int accessflag, int from_tty,
10594 bool just_location, bool internal)
10595 {
10596 struct breakpoint *scope_breakpoint = NULL;
10597 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10598 struct value *result;
10599 int saved_bitpos = 0, saved_bitsize = 0;
10600 const char *exp_start = NULL;
10601 const char *exp_end = NULL;
10602 const char *tok, *end_tok;
10603 int toklen = -1;
10604 const char *cond_start = NULL;
10605 const char *cond_end = NULL;
10606 enum bptype bp_type;
10607 int thread = -1;
10608 /* Flag to indicate whether we are going to use masks for
10609 the hardware watchpoint. */
10610 bool use_mask = false;
10611 CORE_ADDR mask = 0;
10612
10613 /* Make sure that we actually have parameters to parse. */
10614 if (arg != NULL && arg[0] != '\0')
10615 {
10616 const char *value_start;
10617
10618 exp_end = arg + strlen (arg);
10619
10620 /* Look for "parameter value" pairs at the end
10621 of the arguments string. */
10622 for (tok = exp_end - 1; tok > arg; tok--)
10623 {
10624 /* Skip whitespace at the end of the argument list. */
10625 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10626 tok--;
10627
10628 /* Find the beginning of the last token.
10629 This is the value of the parameter. */
10630 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10631 tok--;
10632 value_start = tok + 1;
10633
10634 /* Skip whitespace. */
10635 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10636 tok--;
10637
10638 end_tok = tok;
10639
10640 /* Find the beginning of the second to last token.
10641 This is the parameter itself. */
10642 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10643 tok--;
10644 tok++;
10645 toklen = end_tok - tok + 1;
10646
10647 if (toklen == 6 && startswith (tok, "thread"))
10648 {
10649 struct thread_info *thr;
10650 /* At this point we've found a "thread" token, which means
10651 the user is trying to set a watchpoint that triggers
10652 only in a specific thread. */
10653 const char *endp;
10654
10655 if (thread != -1)
10656 error(_("You can specify only one thread."));
10657
10658 /* Extract the thread ID from the next token. */
10659 thr = parse_thread_id (value_start, &endp);
10660
10661 /* Check if the user provided a valid thread ID. */
10662 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10663 invalid_thread_id_error (value_start);
10664
10665 thread = thr->global_num;
10666 }
10667 else if (toklen == 4 && startswith (tok, "mask"))
10668 {
10669 /* We've found a "mask" token, which means the user wants to
10670 create a hardware watchpoint that is going to have the mask
10671 facility. */
10672 struct value *mask_value, *mark;
10673
10674 if (use_mask)
10675 error(_("You can specify only one mask."));
10676
10677 use_mask = just_location = true;
10678
10679 mark = value_mark ();
10680 mask_value = parse_to_comma_and_eval (&value_start);
10681 mask = value_as_address (mask_value);
10682 value_free_to_mark (mark);
10683 }
10684 else
10685 /* We didn't recognize what we found. We should stop here. */
10686 break;
10687
10688 /* Truncate the string and get rid of the "parameter value" pair before
10689 the arguments string is parsed by the parse_exp_1 function. */
10690 exp_end = tok;
10691 }
10692 }
10693 else
10694 exp_end = arg;
10695
10696 /* Parse the rest of the arguments. From here on out, everything
10697 is in terms of a newly allocated string instead of the original
10698 ARG. */
10699 std::string expression (arg, exp_end - arg);
10700 exp_start = arg = expression.c_str ();
10701 innermost_block_tracker tracker;
10702 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10703 exp_end = arg;
10704 /* Remove trailing whitespace from the expression before saving it.
10705 This makes the eventual display of the expression string a bit
10706 prettier. */
10707 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10708 --exp_end;
10709
10710 /* Checking if the expression is not constant. */
10711 if (watchpoint_exp_is_const (exp.get ()))
10712 {
10713 int len;
10714
10715 len = exp_end - exp_start;
10716 while (len > 0 && isspace (exp_start[len - 1]))
10717 len--;
10718 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10719 }
10720
10721 exp_valid_block = tracker.block ();
10722 struct value *mark = value_mark ();
10723 struct value *val_as_value = nullptr;
10724 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10725 just_location);
10726
10727 if (val_as_value != NULL && just_location)
10728 {
10729 saved_bitpos = value_bitpos (val_as_value);
10730 saved_bitsize = value_bitsize (val_as_value);
10731 }
10732
10733 value_ref_ptr val;
10734 if (just_location)
10735 {
10736 int ret;
10737
10738 exp_valid_block = NULL;
10739 val = release_value (value_addr (result));
10740 value_free_to_mark (mark);
10741
10742 if (use_mask)
10743 {
10744 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10745 mask);
10746 if (ret == -1)
10747 error (_("This target does not support masked watchpoints."));
10748 else if (ret == -2)
10749 error (_("Invalid mask or memory region."));
10750 }
10751 }
10752 else if (val_as_value != NULL)
10753 val = release_value (val_as_value);
10754
10755 tok = skip_spaces (arg);
10756 end_tok = skip_to_space (tok);
10757
10758 toklen = end_tok - tok;
10759 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10760 {
10761 tok = cond_start = end_tok + 1;
10762 innermost_block_tracker if_tracker;
10763 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10764
10765 /* The watchpoint expression may not be local, but the condition
10766 may still be. E.g.: `watch global if local > 0'. */
10767 cond_exp_valid_block = if_tracker.block ();
10768
10769 cond_end = tok;
10770 }
10771 if (*tok)
10772 error (_("Junk at end of command."));
10773
10774 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10775
10776 /* Save this because create_internal_breakpoint below invalidates
10777 'wp_frame'. */
10778 frame_id watchpoint_frame = get_frame_id (wp_frame);
10779
10780 /* If the expression is "local", then set up a "watchpoint scope"
10781 breakpoint at the point where we've left the scope of the watchpoint
10782 expression. Create the scope breakpoint before the watchpoint, so
10783 that we will encounter it first in bpstat_stop_status. */
10784 if (exp_valid_block != NULL && wp_frame != NULL)
10785 {
10786 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10787
10788 if (frame_id_p (caller_frame_id))
10789 {
10790 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10791 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10792
10793 scope_breakpoint
10794 = create_internal_breakpoint (caller_arch, caller_pc,
10795 bp_watchpoint_scope,
10796 &momentary_breakpoint_ops);
10797
10798 /* create_internal_breakpoint could invalidate WP_FRAME. */
10799 wp_frame = NULL;
10800
10801 scope_breakpoint->enable_state = bp_enabled;
10802
10803 /* Automatically delete the breakpoint when it hits. */
10804 scope_breakpoint->disposition = disp_del;
10805
10806 /* Only break in the proper frame (help with recursion). */
10807 scope_breakpoint->frame_id = caller_frame_id;
10808
10809 /* Set the address at which we will stop. */
10810 scope_breakpoint->loc->gdbarch = caller_arch;
10811 scope_breakpoint->loc->requested_address = caller_pc;
10812 scope_breakpoint->loc->address
10813 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10814 scope_breakpoint->loc->requested_address,
10815 scope_breakpoint->type);
10816 }
10817 }
10818
10819 /* Now set up the breakpoint. We create all watchpoints as hardware
10820 watchpoints here even if hardware watchpoints are turned off, a call
10821 to update_watchpoint later in this function will cause the type to
10822 drop back to bp_watchpoint (software watchpoint) if required. */
10823
10824 if (accessflag == hw_read)
10825 bp_type = bp_read_watchpoint;
10826 else if (accessflag == hw_access)
10827 bp_type = bp_access_watchpoint;
10828 else
10829 bp_type = bp_hardware_watchpoint;
10830
10831 std::unique_ptr<watchpoint> w (new watchpoint ());
10832
10833 if (use_mask)
10834 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10835 &masked_watchpoint_breakpoint_ops);
10836 else
10837 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10838 &watchpoint_breakpoint_ops);
10839 w->thread = thread;
10840 w->disposition = disp_donttouch;
10841 w->pspace = current_program_space;
10842 w->exp = std::move (exp);
10843 w->exp_valid_block = exp_valid_block;
10844 w->cond_exp_valid_block = cond_exp_valid_block;
10845 if (just_location)
10846 {
10847 struct type *t = value_type (val.get ());
10848 CORE_ADDR addr = value_as_address (val.get ());
10849
10850 w->exp_string_reparse
10851 = current_language->watch_location_expression (t, addr).release ();
10852
10853 w->exp_string = xstrprintf ("-location %.*s",
10854 (int) (exp_end - exp_start), exp_start);
10855 }
10856 else
10857 w->exp_string = savestring (exp_start, exp_end - exp_start);
10858
10859 if (use_mask)
10860 {
10861 w->hw_wp_mask = mask;
10862 }
10863 else
10864 {
10865 w->val = val;
10866 w->val_bitpos = saved_bitpos;
10867 w->val_bitsize = saved_bitsize;
10868 w->val_valid = true;
10869 }
10870
10871 if (cond_start)
10872 w->cond_string = savestring (cond_start, cond_end - cond_start);
10873 else
10874 w->cond_string = 0;
10875
10876 if (frame_id_p (watchpoint_frame))
10877 {
10878 w->watchpoint_frame = watchpoint_frame;
10879 w->watchpoint_thread = inferior_ptid;
10880 }
10881 else
10882 {
10883 w->watchpoint_frame = null_frame_id;
10884 w->watchpoint_thread = null_ptid;
10885 }
10886
10887 if (scope_breakpoint != NULL)
10888 {
10889 /* The scope breakpoint is related to the watchpoint. We will
10890 need to act on them together. */
10891 w->related_breakpoint = scope_breakpoint;
10892 scope_breakpoint->related_breakpoint = w.get ();
10893 }
10894
10895 if (!just_location)
10896 value_free_to_mark (mark);
10897
10898 /* Finally update the new watchpoint. This creates the locations
10899 that should be inserted. */
10900 update_watchpoint (w.get (), 1);
10901
10902 install_breakpoint (internal, std::move (w), 1);
10903 }
10904
10905 /* Return count of debug registers needed to watch the given expression.
10906 If the watchpoint cannot be handled in hardware return zero. */
10907
10908 static int
10909 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10910 {
10911 int found_memory_cnt = 0;
10912
10913 /* Did the user specifically forbid us to use hardware watchpoints? */
10914 if (!can_use_hw_watchpoints)
10915 return 0;
10916
10917 gdb_assert (!vals.empty ());
10918 struct value *head = vals[0].get ();
10919
10920 /* Make sure that the value of the expression depends only upon
10921 memory contents, and values computed from them within GDB. If we
10922 find any register references or function calls, we can't use a
10923 hardware watchpoint.
10924
10925 The idea here is that evaluating an expression generates a series
10926 of values, one holding the value of every subexpression. (The
10927 expression a*b+c has five subexpressions: a, b, a*b, c, and
10928 a*b+c.) GDB's values hold almost enough information to establish
10929 the criteria given above --- they identify memory lvalues,
10930 register lvalues, computed values, etcetera. So we can evaluate
10931 the expression, and then scan the chain of values that leaves
10932 behind to decide whether we can detect any possible change to the
10933 expression's final value using only hardware watchpoints.
10934
10935 However, I don't think that the values returned by inferior
10936 function calls are special in any way. So this function may not
10937 notice that an expression involving an inferior function call
10938 can't be watched with hardware watchpoints. FIXME. */
10939 for (const value_ref_ptr &iter : vals)
10940 {
10941 struct value *v = iter.get ();
10942
10943 if (VALUE_LVAL (v) == lval_memory)
10944 {
10945 if (v != head && value_lazy (v))
10946 /* A lazy memory lvalue in the chain is one that GDB never
10947 needed to fetch; we either just used its address (e.g.,
10948 `a' in `a.b') or we never needed it at all (e.g., `a'
10949 in `a,b'). This doesn't apply to HEAD; if that is
10950 lazy then it was not readable, but watch it anyway. */
10951 ;
10952 else
10953 {
10954 /* Ahh, memory we actually used! Check if we can cover
10955 it with hardware watchpoints. */
10956 struct type *vtype = check_typedef (value_type (v));
10957
10958 /* We only watch structs and arrays if user asked for it
10959 explicitly, never if they just happen to appear in a
10960 middle of some value chain. */
10961 if (v == head
10962 || (vtype->code () != TYPE_CODE_STRUCT
10963 && vtype->code () != TYPE_CODE_ARRAY))
10964 {
10965 CORE_ADDR vaddr = value_address (v);
10966 int len;
10967 int num_regs;
10968
10969 len = (target_exact_watchpoints
10970 && is_scalar_type_recursive (vtype))?
10971 1 : TYPE_LENGTH (value_type (v));
10972
10973 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10974 if (!num_regs)
10975 return 0;
10976 else
10977 found_memory_cnt += num_regs;
10978 }
10979 }
10980 }
10981 else if (VALUE_LVAL (v) != not_lval
10982 && deprecated_value_modifiable (v) == 0)
10983 return 0; /* These are values from the history (e.g., $1). */
10984 else if (VALUE_LVAL (v) == lval_register)
10985 return 0; /* Cannot watch a register with a HW watchpoint. */
10986 }
10987
10988 /* The expression itself looks suitable for using a hardware
10989 watchpoint, but give the target machine a chance to reject it. */
10990 return found_memory_cnt;
10991 }
10992
10993 void
10994 watch_command_wrapper (const char *arg, int from_tty, bool internal)
10995 {
10996 watch_command_1 (arg, hw_write, from_tty, 0, internal);
10997 }
10998
10999 /* Options for the watch, awatch, and rwatch commands. */
11000
11001 struct watch_options
11002 {
11003 /* For -location. */
11004 bool location = false;
11005 };
11006
11007 /* Definitions of options for the "watch", "awatch", and "rwatch" commands.
11008
11009 Historically GDB always accepted both '-location' and '-l' flags for
11010 these commands (both flags being synonyms). When converting to the
11011 newer option scheme only '-location' is added here. That's fine (for
11012 backward compatibility) as any non-ambiguous prefix of a flag will be
11013 accepted, so '-l', '-loc', are now all accepted.
11014
11015 What this means is that, if in the future, we add any new flag here
11016 that starts with '-l' then this will break backward compatibility, so
11017 please, don't do that! */
11018
11019 static const gdb::option::option_def watch_option_defs[] = {
11020 gdb::option::flag_option_def<watch_options> {
11021 "location",
11022 [] (watch_options *opt) { return &opt->location; },
11023 N_("\
11024 This evaluates EXPRESSION and watches the memory to which is refers.\n\
11025 -l can be used as a short form of -location."),
11026 },
11027 };
11028
11029 /* Returns the option group used by 'watch', 'awatch', and 'rwatch'
11030 commands. */
11031
11032 static gdb::option::option_def_group
11033 make_watch_options_def_group (watch_options *opts)
11034 {
11035 return {{watch_option_defs}, opts};
11036 }
11037
11038 /* A helper function that looks for the "-location" argument and then
11039 calls watch_command_1. */
11040
11041 static void
11042 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
11043 {
11044 watch_options opts;
11045 auto grp = make_watch_options_def_group (&opts);
11046 gdb::option::process_options
11047 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
11048 if (arg != nullptr && *arg == '\0')
11049 arg = nullptr;
11050
11051 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
11052 }
11053
11054 /* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
11055 static void
11056 watch_command_completer (struct cmd_list_element *ignore,
11057 completion_tracker &tracker,
11058 const char *text, const char * /*word*/)
11059 {
11060 const auto group = make_watch_options_def_group (nullptr);
11061 if (gdb::option::complete_options
11062 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
11063 return;
11064
11065 const char *word = advance_to_expression_complete_word_point (tracker, text);
11066 expression_completer (ignore, tracker, text, word);
11067 }
11068
11069 static void
11070 watch_command (const char *arg, int from_tty)
11071 {
11072 watch_maybe_just_location (arg, hw_write, from_tty);
11073 }
11074
11075 void
11076 rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
11077 {
11078 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11079 }
11080
11081 static void
11082 rwatch_command (const char *arg, int from_tty)
11083 {
11084 watch_maybe_just_location (arg, hw_read, from_tty);
11085 }
11086
11087 void
11088 awatch_command_wrapper (const char *arg, int from_tty, bool internal)
11089 {
11090 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11091 }
11092
11093 static void
11094 awatch_command (const char *arg, int from_tty)
11095 {
11096 watch_maybe_just_location (arg, hw_access, from_tty);
11097 }
11098 \f
11099
11100 /* Data for the FSM that manages the until(location)/advance commands
11101 in infcmd.c. Here because it uses the mechanisms of
11102 breakpoints. */
11103
11104 struct until_break_fsm : public thread_fsm
11105 {
11106 /* The thread that was current when the command was executed. */
11107 int thread;
11108
11109 /* The breakpoint set at the return address in the caller frame,
11110 plus breakpoints at all the destination locations. */
11111 std::vector<breakpoint_up> breakpoints;
11112
11113 until_break_fsm (struct interp *cmd_interp, int thread,
11114 std::vector<breakpoint_up> &&breakpoints)
11115 : thread_fsm (cmd_interp),
11116 thread (thread),
11117 breakpoints (std::move (breakpoints))
11118 {
11119 }
11120
11121 void clean_up (struct thread_info *thread) override;
11122 bool should_stop (struct thread_info *thread) override;
11123 enum async_reply_reason do_async_reply_reason () override;
11124 };
11125
11126 /* Implementation of the 'should_stop' FSM method for the
11127 until(location)/advance commands. */
11128
11129 bool
11130 until_break_fsm::should_stop (struct thread_info *tp)
11131 {
11132 for (const breakpoint_up &bp : breakpoints)
11133 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11134 bp.get ()) != NULL)
11135 {
11136 set_finished ();
11137 break;
11138 }
11139
11140 return true;
11141 }
11142
11143 /* Implementation of the 'clean_up' FSM method for the
11144 until(location)/advance commands. */
11145
11146 void
11147 until_break_fsm::clean_up (struct thread_info *)
11148 {
11149 /* Clean up our temporary breakpoints. */
11150 breakpoints.clear ();
11151 delete_longjmp_breakpoint (thread);
11152 }
11153
11154 /* Implementation of the 'async_reply_reason' FSM method for the
11155 until(location)/advance commands. */
11156
11157 enum async_reply_reason
11158 until_break_fsm::do_async_reply_reason ()
11159 {
11160 return EXEC_ASYNC_LOCATION_REACHED;
11161 }
11162
11163 void
11164 until_break_command (const char *arg, int from_tty, int anywhere)
11165 {
11166 struct frame_info *frame;
11167 struct gdbarch *frame_gdbarch;
11168 struct frame_id stack_frame_id;
11169 struct frame_id caller_frame_id;
11170 int thread;
11171 struct thread_info *tp;
11172
11173 clear_proceed_status (0);
11174
11175 /* Set a breakpoint where the user wants it and at return from
11176 this function. */
11177
11178 event_location_up location = string_to_event_location (&arg, current_language);
11179
11180 std::vector<symtab_and_line> sals
11181 = (last_displayed_sal_is_valid ()
11182 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11183 get_last_displayed_symtab (),
11184 get_last_displayed_line ())
11185 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11186 NULL, NULL, 0));
11187
11188 if (sals.empty ())
11189 error (_("Couldn't get information on specified line."));
11190
11191 if (*arg)
11192 error (_("Junk at end of arguments."));
11193
11194 tp = inferior_thread ();
11195 thread = tp->global_num;
11196
11197 /* Note linespec handling above invalidates the frame chain.
11198 Installing a breakpoint also invalidates the frame chain (as it
11199 may need to switch threads), so do any frame handling before
11200 that. */
11201
11202 frame = get_selected_frame (NULL);
11203 frame_gdbarch = get_frame_arch (frame);
11204 stack_frame_id = get_stack_frame_id (frame);
11205 caller_frame_id = frame_unwind_caller_id (frame);
11206
11207 /* Keep within the current frame, or in frames called by the current
11208 one. */
11209
11210 std::vector<breakpoint_up> breakpoints;
11211
11212 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11213
11214 if (frame_id_p (caller_frame_id))
11215 {
11216 struct symtab_and_line sal2;
11217 struct gdbarch *caller_gdbarch;
11218
11219 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11220 sal2.pc = frame_unwind_caller_pc (frame);
11221 caller_gdbarch = frame_unwind_caller_arch (frame);
11222
11223 breakpoint_up caller_breakpoint
11224 = set_momentary_breakpoint (caller_gdbarch, sal2,
11225 caller_frame_id, bp_until);
11226 breakpoints.emplace_back (std::move (caller_breakpoint));
11227
11228 set_longjmp_breakpoint (tp, caller_frame_id);
11229 lj_deleter.emplace (thread);
11230 }
11231
11232 /* set_momentary_breakpoint could invalidate FRAME. */
11233 frame = NULL;
11234
11235 /* If the user told us to continue until a specified location, we
11236 don't specify a frame at which we need to stop. Otherwise,
11237 specify the selected frame, because we want to stop only at the
11238 very same frame. */
11239 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
11240
11241 for (symtab_and_line &sal : sals)
11242 {
11243 resolve_sal_pc (&sal);
11244
11245 breakpoint_up location_breakpoint
11246 = set_momentary_breakpoint (frame_gdbarch, sal,
11247 stop_frame_id, bp_until);
11248 breakpoints.emplace_back (std::move (location_breakpoint));
11249 }
11250
11251 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
11252 std::move (breakpoints));
11253
11254 if (lj_deleter)
11255 lj_deleter->release ();
11256
11257 proceed (-1, GDB_SIGNAL_DEFAULT);
11258 }
11259
11260 /* This function attempts to parse an optional "if <cond>" clause
11261 from the arg string. If one is not found, it returns NULL.
11262
11263 Else, it returns a pointer to the condition string. (It does not
11264 attempt to evaluate the string against a particular block.) And,
11265 it updates arg to point to the first character following the parsed
11266 if clause in the arg string. */
11267
11268 const char *
11269 ep_parse_optional_if_clause (const char **arg)
11270 {
11271 const char *cond_string;
11272
11273 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11274 return NULL;
11275
11276 /* Skip the "if" keyword. */
11277 (*arg) += 2;
11278
11279 /* Skip any extra leading whitespace, and record the start of the
11280 condition string. */
11281 *arg = skip_spaces (*arg);
11282 cond_string = *arg;
11283
11284 /* Assume that the condition occupies the remainder of the arg
11285 string. */
11286 (*arg) += strlen (cond_string);
11287
11288 return cond_string;
11289 }
11290
11291 /* Commands to deal with catching events, such as signals, exceptions,
11292 process start/exit, etc. */
11293
11294 typedef enum
11295 {
11296 catch_fork_temporary, catch_vfork_temporary,
11297 catch_fork_permanent, catch_vfork_permanent
11298 }
11299 catch_fork_kind;
11300
11301 static void
11302 catch_fork_command_1 (const char *arg, int from_tty,
11303 struct cmd_list_element *command)
11304 {
11305 struct gdbarch *gdbarch = get_current_arch ();
11306 const char *cond_string = NULL;
11307 catch_fork_kind fork_kind;
11308
11309 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11310 bool temp = (fork_kind == catch_fork_temporary
11311 || fork_kind == catch_vfork_temporary);
11312
11313 if (!arg)
11314 arg = "";
11315 arg = skip_spaces (arg);
11316
11317 /* The allowed syntax is:
11318 catch [v]fork
11319 catch [v]fork if <cond>
11320
11321 First, check if there's an if clause. */
11322 cond_string = ep_parse_optional_if_clause (&arg);
11323
11324 if ((*arg != '\0') && !isspace (*arg))
11325 error (_("Junk at end of arguments."));
11326
11327 /* If this target supports it, create a fork or vfork catchpoint
11328 and enable reporting of such events. */
11329 switch (fork_kind)
11330 {
11331 case catch_fork_temporary:
11332 case catch_fork_permanent:
11333 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
11334 &catch_fork_breakpoint_ops);
11335 break;
11336 case catch_vfork_temporary:
11337 case catch_vfork_permanent:
11338 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
11339 &catch_vfork_breakpoint_ops);
11340 break;
11341 default:
11342 error (_("unsupported or unknown fork kind; cannot catch it"));
11343 break;
11344 }
11345 }
11346
11347 static void
11348 catch_exec_command_1 (const char *arg, int from_tty,
11349 struct cmd_list_element *command)
11350 {
11351 struct gdbarch *gdbarch = get_current_arch ();
11352 const char *cond_string = NULL;
11353 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
11354
11355 if (!arg)
11356 arg = "";
11357 arg = skip_spaces (arg);
11358
11359 /* The allowed syntax is:
11360 catch exec
11361 catch exec if <cond>
11362
11363 First, check if there's an if clause. */
11364 cond_string = ep_parse_optional_if_clause (&arg);
11365
11366 if ((*arg != '\0') && !isspace (*arg))
11367 error (_("Junk at end of arguments."));
11368
11369 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11370 init_catchpoint (c.get (), gdbarch, temp, cond_string,
11371 &catch_exec_breakpoint_ops);
11372 c->exec_pathname = NULL;
11373
11374 install_breakpoint (0, std::move (c), 1);
11375 }
11376
11377 void
11378 init_ada_exception_breakpoint (struct breakpoint *b,
11379 struct gdbarch *gdbarch,
11380 struct symtab_and_line sal,
11381 const char *addr_string,
11382 const struct breakpoint_ops *ops,
11383 int tempflag,
11384 int enabled,
11385 int from_tty)
11386 {
11387 if (from_tty)
11388 {
11389 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11390 if (!loc_gdbarch)
11391 loc_gdbarch = gdbarch;
11392
11393 describe_other_breakpoints (loc_gdbarch,
11394 sal.pspace, sal.pc, sal.section, -1);
11395 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11396 version for exception catchpoints, because two catchpoints
11397 used for different exception names will use the same address.
11398 In this case, a "breakpoint ... also set at..." warning is
11399 unproductive. Besides, the warning phrasing is also a bit
11400 inappropriate, we should use the word catchpoint, and tell
11401 the user what type of catchpoint it is. The above is good
11402 enough for now, though. */
11403 }
11404
11405 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
11406
11407 b->enable_state = enabled ? bp_enabled : bp_disabled;
11408 b->disposition = tempflag ? disp_del : disp_donttouch;
11409 b->location = string_to_event_location (&addr_string,
11410 language_def (language_ada));
11411 b->language = language_ada;
11412 }
11413
11414 \f
11415
11416 /* Compare two breakpoints and return a strcmp-like result. */
11417
11418 static int
11419 compare_breakpoints (const breakpoint *a, const breakpoint *b)
11420 {
11421 uintptr_t ua = (uintptr_t) a;
11422 uintptr_t ub = (uintptr_t) b;
11423
11424 if (a->number < b->number)
11425 return -1;
11426 else if (a->number > b->number)
11427 return 1;
11428
11429 /* Now sort by address, in case we see, e..g, two breakpoints with
11430 the number 0. */
11431 if (ua < ub)
11432 return -1;
11433 return ua > ub ? 1 : 0;
11434 }
11435
11436 /* Delete breakpoints by address or line. */
11437
11438 static void
11439 clear_command (const char *arg, int from_tty)
11440 {
11441 int default_match;
11442
11443 std::vector<symtab_and_line> decoded_sals;
11444 symtab_and_line last_sal;
11445 gdb::array_view<symtab_and_line> sals;
11446 if (arg)
11447 {
11448 decoded_sals
11449 = decode_line_with_current_source (arg,
11450 (DECODE_LINE_FUNFIRSTLINE
11451 | DECODE_LINE_LIST_MODE));
11452 default_match = 0;
11453 sals = decoded_sals;
11454 }
11455 else
11456 {
11457 /* Set sal's line, symtab, pc, and pspace to the values
11458 corresponding to the last call to print_frame_info. If the
11459 codepoint is not valid, this will set all the fields to 0. */
11460 last_sal = get_last_displayed_sal ();
11461 if (last_sal.symtab == 0)
11462 error (_("No source file specified."));
11463
11464 default_match = 1;
11465 sals = last_sal;
11466 }
11467
11468 /* We don't call resolve_sal_pc here. That's not as bad as it
11469 seems, because all existing breakpoints typically have both
11470 file/line and pc set. So, if clear is given file/line, we can
11471 match this to existing breakpoint without obtaining pc at all.
11472
11473 We only support clearing given the address explicitly
11474 present in breakpoint table. Say, we've set breakpoint
11475 at file:line. There were several PC values for that file:line,
11476 due to optimization, all in one block.
11477
11478 We've picked one PC value. If "clear" is issued with another
11479 PC corresponding to the same file:line, the breakpoint won't
11480 be cleared. We probably can still clear the breakpoint, but
11481 since the other PC value is never presented to user, user
11482 can only find it by guessing, and it does not seem important
11483 to support that. */
11484
11485 /* For each line spec given, delete bps which correspond to it. Do
11486 it in two passes, solely to preserve the current behavior that
11487 from_tty is forced true if we delete more than one
11488 breakpoint. */
11489
11490 std::vector<struct breakpoint *> found;
11491 for (const auto &sal : sals)
11492 {
11493 const char *sal_fullname;
11494
11495 /* If exact pc given, clear bpts at that pc.
11496 If line given (pc == 0), clear all bpts on specified line.
11497 If defaulting, clear all bpts on default line
11498 or at default pc.
11499
11500 defaulting sal.pc != 0 tests to do
11501
11502 0 1 pc
11503 1 1 pc _and_ line
11504 0 0 line
11505 1 0 <can't happen> */
11506
11507 sal_fullname = (sal.symtab == NULL
11508 ? NULL : symtab_to_fullname (sal.symtab));
11509
11510 /* Find all matching breakpoints and add them to 'found'. */
11511 for (breakpoint *b : all_breakpoints ())
11512 {
11513 int match = 0;
11514 /* Are we going to delete b? */
11515 if (b->type != bp_none && !is_watchpoint (b))
11516 {
11517 for (bp_location *loc : b->locations ())
11518 {
11519 /* If the user specified file:line, don't allow a PC
11520 match. This matches historical gdb behavior. */
11521 int pc_match = (!sal.explicit_line
11522 && sal.pc
11523 && (loc->pspace == sal.pspace)
11524 && (loc->address == sal.pc)
11525 && (!section_is_overlay (loc->section)
11526 || loc->section == sal.section));
11527 int line_match = 0;
11528
11529 if ((default_match || sal.explicit_line)
11530 && loc->symtab != NULL
11531 && sal_fullname != NULL
11532 && sal.pspace == loc->pspace
11533 && loc->line_number == sal.line
11534 && filename_cmp (symtab_to_fullname (loc->symtab),
11535 sal_fullname) == 0)
11536 line_match = 1;
11537
11538 if (pc_match || line_match)
11539 {
11540 match = 1;
11541 break;
11542 }
11543 }
11544 }
11545
11546 if (match)
11547 found.push_back (b);
11548 }
11549 }
11550
11551 /* Now go thru the 'found' chain and delete them. */
11552 if (found.empty ())
11553 {
11554 if (arg)
11555 error (_("No breakpoint at %s."), arg);
11556 else
11557 error (_("No breakpoint at this line."));
11558 }
11559
11560 /* Remove duplicates from the vec. */
11561 std::sort (found.begin (), found.end (),
11562 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11563 {
11564 return compare_breakpoints (bp_a, bp_b) < 0;
11565 });
11566 found.erase (std::unique (found.begin (), found.end (),
11567 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11568 {
11569 return compare_breakpoints (bp_a, bp_b) == 0;
11570 }),
11571 found.end ());
11572
11573 if (found.size () > 1)
11574 from_tty = 1; /* Always report if deleted more than one. */
11575 if (from_tty)
11576 {
11577 if (found.size () == 1)
11578 printf_unfiltered (_("Deleted breakpoint "));
11579 else
11580 printf_unfiltered (_("Deleted breakpoints "));
11581 }
11582
11583 for (breakpoint *iter : found)
11584 {
11585 if (from_tty)
11586 printf_unfiltered ("%d ", iter->number);
11587 delete_breakpoint (iter);
11588 }
11589 if (from_tty)
11590 putchar_unfiltered ('\n');
11591 }
11592 \f
11593 /* Delete breakpoint in BS if they are `delete' breakpoints and
11594 all breakpoints that are marked for deletion, whether hit or not.
11595 This is called after any breakpoint is hit, or after errors. */
11596
11597 void
11598 breakpoint_auto_delete (bpstat bs)
11599 {
11600 for (; bs; bs = bs->next)
11601 if (bs->breakpoint_at
11602 && bs->breakpoint_at->disposition == disp_del
11603 && bs->stop)
11604 delete_breakpoint (bs->breakpoint_at);
11605
11606 for (breakpoint *b : all_breakpoints_safe ())
11607 if (b->disposition == disp_del_at_next_stop)
11608 delete_breakpoint (b);
11609 }
11610
11611 /* A comparison function for bp_location AP and BP being interfaced to
11612 std::sort. Sort elements primarily by their ADDRESS (no matter what
11613 bl_address_is_meaningful says), secondarily by ordering first
11614 permanent elements and terciarily just ensuring the array is sorted
11615 stable way despite std::sort being an unstable algorithm. */
11616
11617 static int
11618 bp_location_is_less_than (const bp_location *a, const bp_location *b)
11619 {
11620 if (a->address != b->address)
11621 return a->address < b->address;
11622
11623 /* Sort locations at the same address by their pspace number, keeping
11624 locations of the same inferior (in a multi-inferior environment)
11625 grouped. */
11626
11627 if (a->pspace->num != b->pspace->num)
11628 return a->pspace->num < b->pspace->num;
11629
11630 /* Sort permanent breakpoints first. */
11631 if (a->permanent != b->permanent)
11632 return a->permanent > b->permanent;
11633
11634 /* Sort by type in order to make duplicate determination easier.
11635 See update_global_location_list. This is kept in sync with
11636 breakpoint_locations_match. */
11637 if (a->loc_type < b->loc_type)
11638 return true;
11639
11640 /* Likewise, for range-breakpoints, sort by length. */
11641 if (a->loc_type == bp_loc_hardware_breakpoint
11642 && b->loc_type == bp_loc_hardware_breakpoint
11643 && a->length < b->length)
11644 return true;
11645
11646 /* Make the internal GDB representation stable across GDB runs
11647 where A and B memory inside GDB can differ. Breakpoint locations of
11648 the same type at the same address can be sorted in arbitrary order. */
11649
11650 if (a->owner->number != b->owner->number)
11651 return a->owner->number < b->owner->number;
11652
11653 return a < b;
11654 }
11655
11656 /* Set bp_locations_placed_address_before_address_max and
11657 bp_locations_shadow_len_after_address_max according to the current
11658 content of the bp_locations array. */
11659
11660 static void
11661 bp_locations_target_extensions_update (void)
11662 {
11663 struct bp_location *bl, **blp_tmp;
11664
11665 bp_locations_placed_address_before_address_max = 0;
11666 bp_locations_shadow_len_after_address_max = 0;
11667
11668 ALL_BP_LOCATIONS (bl, blp_tmp)
11669 {
11670 CORE_ADDR start, end, addr;
11671
11672 if (!bp_location_has_shadow (bl))
11673 continue;
11674
11675 start = bl->target_info.placed_address;
11676 end = start + bl->target_info.shadow_len;
11677
11678 gdb_assert (bl->address >= start);
11679 addr = bl->address - start;
11680 if (addr > bp_locations_placed_address_before_address_max)
11681 bp_locations_placed_address_before_address_max = addr;
11682
11683 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11684
11685 gdb_assert (bl->address < end);
11686 addr = end - bl->address;
11687 if (addr > bp_locations_shadow_len_after_address_max)
11688 bp_locations_shadow_len_after_address_max = addr;
11689 }
11690 }
11691
11692 /* Download tracepoint locations if they haven't been. */
11693
11694 static void
11695 download_tracepoint_locations (void)
11696 {
11697 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11698
11699 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11700
11701 for (breakpoint *b : all_tracepoints ())
11702 {
11703 struct tracepoint *t;
11704 int bp_location_downloaded = 0;
11705
11706 if ((b->type == bp_fast_tracepoint
11707 ? !may_insert_fast_tracepoints
11708 : !may_insert_tracepoints))
11709 continue;
11710
11711 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11712 {
11713 if (target_can_download_tracepoint ())
11714 can_download_tracepoint = TRIBOOL_TRUE;
11715 else
11716 can_download_tracepoint = TRIBOOL_FALSE;
11717 }
11718
11719 if (can_download_tracepoint == TRIBOOL_FALSE)
11720 break;
11721
11722 for (bp_location *bl : b->locations ())
11723 {
11724 /* In tracepoint, locations are _never_ duplicated, so
11725 should_be_inserted is equivalent to
11726 unduplicated_should_be_inserted. */
11727 if (!should_be_inserted (bl) || bl->inserted)
11728 continue;
11729
11730 switch_to_program_space_and_thread (bl->pspace);
11731
11732 target_download_tracepoint (bl);
11733
11734 bl->inserted = 1;
11735 bp_location_downloaded = 1;
11736 }
11737 t = (struct tracepoint *) b;
11738 t->number_on_target = b->number;
11739 if (bp_location_downloaded)
11740 gdb::observers::breakpoint_modified.notify (b);
11741 }
11742 }
11743
11744 /* Swap the insertion/duplication state between two locations. */
11745
11746 static void
11747 swap_insertion (struct bp_location *left, struct bp_location *right)
11748 {
11749 const int left_inserted = left->inserted;
11750 const int left_duplicate = left->duplicate;
11751 const int left_needs_update = left->needs_update;
11752 const struct bp_target_info left_target_info = left->target_info;
11753
11754 /* Locations of tracepoints can never be duplicated. */
11755 if (is_tracepoint (left->owner))
11756 gdb_assert (!left->duplicate);
11757 if (is_tracepoint (right->owner))
11758 gdb_assert (!right->duplicate);
11759
11760 left->inserted = right->inserted;
11761 left->duplicate = right->duplicate;
11762 left->needs_update = right->needs_update;
11763 left->target_info = right->target_info;
11764 right->inserted = left_inserted;
11765 right->duplicate = left_duplicate;
11766 right->needs_update = left_needs_update;
11767 right->target_info = left_target_info;
11768 }
11769
11770 /* Force the re-insertion of the locations at ADDRESS. This is called
11771 once a new/deleted/modified duplicate location is found and we are evaluating
11772 conditions on the target's side. Such conditions need to be updated on
11773 the target. */
11774
11775 static void
11776 force_breakpoint_reinsertion (struct bp_location *bl)
11777 {
11778 struct bp_location **locp = NULL, **loc2p;
11779 struct bp_location *loc;
11780 CORE_ADDR address = 0;
11781 int pspace_num;
11782
11783 address = bl->address;
11784 pspace_num = bl->pspace->num;
11785
11786 /* This is only meaningful if the target is
11787 evaluating conditions and if the user has
11788 opted for condition evaluation on the target's
11789 side. */
11790 if (gdb_evaluates_breakpoint_condition_p ()
11791 || !target_supports_evaluation_of_breakpoint_conditions ())
11792 return;
11793
11794 /* Flag all breakpoint locations with this address and
11795 the same program space as the location
11796 as "its condition has changed". We need to
11797 update the conditions on the target's side. */
11798 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11799 {
11800 loc = *loc2p;
11801
11802 if (!is_breakpoint (loc->owner)
11803 || pspace_num != loc->pspace->num)
11804 continue;
11805
11806 /* Flag the location appropriately. We use a different state to
11807 let everyone know that we already updated the set of locations
11808 with addr bl->address and program space bl->pspace. This is so
11809 we don't have to keep calling these functions just to mark locations
11810 that have already been marked. */
11811 loc->condition_changed = condition_updated;
11812
11813 /* Free the agent expression bytecode as well. We will compute
11814 it later on. */
11815 loc->cond_bytecode.reset ();
11816 }
11817 }
11818
11819 /* Called whether new breakpoints are created, or existing breakpoints
11820 deleted, to update the global location list and recompute which
11821 locations are duplicate of which.
11822
11823 The INSERT_MODE flag determines whether locations may not, may, or
11824 shall be inserted now. See 'enum ugll_insert_mode' for more
11825 info. */
11826
11827 static void
11828 update_global_location_list (enum ugll_insert_mode insert_mode)
11829 {
11830 /* Last breakpoint location address that was marked for update. */
11831 CORE_ADDR last_addr = 0;
11832 /* Last breakpoint location program space that was marked for update. */
11833 int last_pspace_num = -1;
11834
11835 /* Used in the duplicates detection below. When iterating over all
11836 bp_locations, points to the first bp_location of a given address.
11837 Breakpoints and watchpoints of different types are never
11838 duplicates of each other. Keep one pointer for each type of
11839 breakpoint/watchpoint, so we only need to loop over all locations
11840 once. */
11841 struct bp_location *bp_loc_first; /* breakpoint */
11842 struct bp_location *wp_loc_first; /* hardware watchpoint */
11843 struct bp_location *awp_loc_first; /* access watchpoint */
11844 struct bp_location *rwp_loc_first; /* read watchpoint */
11845
11846 /* Saved former bp_locations array which we compare against the newly
11847 built bp_locations from the current state of ALL_BREAKPOINTS. */
11848 std::vector<bp_location *> old_locations = std::move (bp_locations);
11849 bp_locations.clear ();
11850
11851 for (breakpoint *b : all_breakpoints ())
11852 for (bp_location *loc : b->locations ())
11853 bp_locations.push_back (loc);
11854
11855 /* See if we need to "upgrade" a software breakpoint to a hardware
11856 breakpoint. Do this before deciding whether locations are
11857 duplicates. Also do this before sorting because sorting order
11858 depends on location type. */
11859 for (bp_location *loc : bp_locations)
11860 if (!loc->inserted && should_be_inserted (loc))
11861 handle_automatic_hardware_breakpoints (loc);
11862
11863 std::sort (bp_locations.begin (), bp_locations.end (),
11864 bp_location_is_less_than);
11865
11866 bp_locations_target_extensions_update ();
11867
11868 /* Identify bp_location instances that are no longer present in the
11869 new list, and therefore should be freed. Note that it's not
11870 necessary that those locations should be removed from inferior --
11871 if there's another location at the same address (previously
11872 marked as duplicate), we don't need to remove/insert the
11873 location.
11874
11875 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11876 and former bp_location array state respectively. */
11877
11878 size_t loc_i = 0;
11879 for (bp_location *old_loc : old_locations)
11880 {
11881 /* Tells if 'old_loc' is found among the new locations. If
11882 not, we have to free it. */
11883 int found_object = 0;
11884 /* Tells if the location should remain inserted in the target. */
11885 int keep_in_target = 0;
11886 int removed = 0;
11887
11888 /* Skip LOCP entries which will definitely never be needed.
11889 Stop either at or being the one matching OLD_LOC. */
11890 while (loc_i < bp_locations.size ()
11891 && bp_locations[loc_i]->address < old_loc->address)
11892 loc_i++;
11893
11894 for (size_t loc2_i = loc_i;
11895 (loc2_i < bp_locations.size ()
11896 && bp_locations[loc2_i]->address == old_loc->address);
11897 loc2_i++)
11898 {
11899 /* Check if this is a new/duplicated location or a duplicated
11900 location that had its condition modified. If so, we want to send
11901 its condition to the target if evaluation of conditions is taking
11902 place there. */
11903 if (bp_locations[loc2_i]->condition_changed == condition_modified
11904 && (last_addr != old_loc->address
11905 || last_pspace_num != old_loc->pspace->num))
11906 {
11907 force_breakpoint_reinsertion (bp_locations[loc2_i]);
11908 last_pspace_num = old_loc->pspace->num;
11909 }
11910
11911 if (bp_locations[loc2_i] == old_loc)
11912 found_object = 1;
11913 }
11914
11915 /* We have already handled this address, update it so that we don't
11916 have to go through updates again. */
11917 last_addr = old_loc->address;
11918
11919 /* Target-side condition evaluation: Handle deleted locations. */
11920 if (!found_object)
11921 force_breakpoint_reinsertion (old_loc);
11922
11923 /* If this location is no longer present, and inserted, look if
11924 there's maybe a new location at the same address. If so,
11925 mark that one inserted, and don't remove this one. This is
11926 needed so that we don't have a time window where a breakpoint
11927 at certain location is not inserted. */
11928
11929 if (old_loc->inserted)
11930 {
11931 /* If the location is inserted now, we might have to remove
11932 it. */
11933
11934 if (found_object && should_be_inserted (old_loc))
11935 {
11936 /* The location is still present in the location list,
11937 and still should be inserted. Don't do anything. */
11938 keep_in_target = 1;
11939 }
11940 else
11941 {
11942 /* This location still exists, but it won't be kept in the
11943 target since it may have been disabled. We proceed to
11944 remove its target-side condition. */
11945
11946 /* The location is either no longer present, or got
11947 disabled. See if there's another location at the
11948 same address, in which case we don't need to remove
11949 this one from the target. */
11950
11951 /* OLD_LOC comes from existing struct breakpoint. */
11952 if (bl_address_is_meaningful (old_loc))
11953 {
11954 for (size_t loc2_i = loc_i;
11955 (loc2_i < bp_locations.size ()
11956 && bp_locations[loc2_i]->address == old_loc->address);
11957 loc2_i++)
11958 {
11959 bp_location *loc2 = bp_locations[loc2_i];
11960
11961 if (loc2 == old_loc)
11962 continue;
11963
11964 if (breakpoint_locations_match (loc2, old_loc))
11965 {
11966 /* Read watchpoint locations are switched to
11967 access watchpoints, if the former are not
11968 supported, but the latter are. */
11969 if (is_hardware_watchpoint (old_loc->owner))
11970 {
11971 gdb_assert (is_hardware_watchpoint (loc2->owner));
11972 loc2->watchpoint_type = old_loc->watchpoint_type;
11973 }
11974
11975 /* loc2 is a duplicated location. We need to check
11976 if it should be inserted in case it will be
11977 unduplicated. */
11978 if (unduplicated_should_be_inserted (loc2))
11979 {
11980 swap_insertion (old_loc, loc2);
11981 keep_in_target = 1;
11982 break;
11983 }
11984 }
11985 }
11986 }
11987 }
11988
11989 if (!keep_in_target)
11990 {
11991 if (remove_breakpoint (old_loc))
11992 {
11993 /* This is just about all we can do. We could keep
11994 this location on the global list, and try to
11995 remove it next time, but there's no particular
11996 reason why we will succeed next time.
11997
11998 Note that at this point, old_loc->owner is still
11999 valid, as delete_breakpoint frees the breakpoint
12000 only after calling us. */
12001 printf_filtered (_("warning: Error removing "
12002 "breakpoint %d\n"),
12003 old_loc->owner->number);
12004 }
12005 removed = 1;
12006 }
12007 }
12008
12009 if (!found_object)
12010 {
12011 if (removed && target_is_non_stop_p ()
12012 && need_moribund_for_location_type (old_loc))
12013 {
12014 /* This location was removed from the target. In
12015 non-stop mode, a race condition is possible where
12016 we've removed a breakpoint, but stop events for that
12017 breakpoint are already queued and will arrive later.
12018 We apply an heuristic to be able to distinguish such
12019 SIGTRAPs from other random SIGTRAPs: we keep this
12020 breakpoint location for a bit, and will retire it
12021 after we see some number of events. The theory here
12022 is that reporting of events should, "on the average",
12023 be fair, so after a while we'll see events from all
12024 threads that have anything of interest, and no longer
12025 need to keep this breakpoint location around. We
12026 don't hold locations forever so to reduce chances of
12027 mistaking a non-breakpoint SIGTRAP for a breakpoint
12028 SIGTRAP.
12029
12030 The heuristic failing can be disastrous on
12031 decr_pc_after_break targets.
12032
12033 On decr_pc_after_break targets, like e.g., x86-linux,
12034 if we fail to recognize a late breakpoint SIGTRAP,
12035 because events_till_retirement has reached 0 too
12036 soon, we'll fail to do the PC adjustment, and report
12037 a random SIGTRAP to the user. When the user resumes
12038 the inferior, it will most likely immediately crash
12039 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12040 corrupted, because of being resumed e.g., in the
12041 middle of a multi-byte instruction, or skipped a
12042 one-byte instruction. This was actually seen happen
12043 on native x86-linux, and should be less rare on
12044 targets that do not support new thread events, like
12045 remote, due to the heuristic depending on
12046 thread_count.
12047
12048 Mistaking a random SIGTRAP for a breakpoint trap
12049 causes similar symptoms (PC adjustment applied when
12050 it shouldn't), but then again, playing with SIGTRAPs
12051 behind the debugger's back is asking for trouble.
12052
12053 Since hardware watchpoint traps are always
12054 distinguishable from other traps, so we don't need to
12055 apply keep hardware watchpoint moribund locations
12056 around. We simply always ignore hardware watchpoint
12057 traps we can no longer explain. */
12058
12059 process_stratum_target *proc_target = nullptr;
12060 for (inferior *inf : all_inferiors ())
12061 if (inf->pspace == old_loc->pspace)
12062 {
12063 proc_target = inf->process_target ();
12064 break;
12065 }
12066 if (proc_target != nullptr)
12067 old_loc->events_till_retirement
12068 = 3 * (thread_count (proc_target) + 1);
12069 else
12070 old_loc->events_till_retirement = 1;
12071 old_loc->owner = NULL;
12072
12073 moribund_locations.push_back (old_loc);
12074 }
12075 else
12076 {
12077 old_loc->owner = NULL;
12078 decref_bp_location (&old_loc);
12079 }
12080 }
12081 }
12082
12083 /* Rescan breakpoints at the same address and section, marking the
12084 first one as "first" and any others as "duplicates". This is so
12085 that the bpt instruction is only inserted once. If we have a
12086 permanent breakpoint at the same place as BPT, make that one the
12087 official one, and the rest as duplicates. Permanent breakpoints
12088 are sorted first for the same address.
12089
12090 Do the same for hardware watchpoints, but also considering the
12091 watchpoint's type (regular/access/read) and length. */
12092
12093 bp_loc_first = NULL;
12094 wp_loc_first = NULL;
12095 awp_loc_first = NULL;
12096 rwp_loc_first = NULL;
12097
12098 bp_location *loc, **locp;
12099 ALL_BP_LOCATIONS (loc, locp)
12100 {
12101 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12102 non-NULL. */
12103 struct bp_location **loc_first_p;
12104 breakpoint *b = loc->owner;
12105
12106 if (!unduplicated_should_be_inserted (loc)
12107 || !bl_address_is_meaningful (loc)
12108 /* Don't detect duplicate for tracepoint locations because they are
12109 never duplicated. See the comments in field `duplicate' of
12110 `struct bp_location'. */
12111 || is_tracepoint (b))
12112 {
12113 /* Clear the condition modification flag. */
12114 loc->condition_changed = condition_unchanged;
12115 continue;
12116 }
12117
12118 if (b->type == bp_hardware_watchpoint)
12119 loc_first_p = &wp_loc_first;
12120 else if (b->type == bp_read_watchpoint)
12121 loc_first_p = &rwp_loc_first;
12122 else if (b->type == bp_access_watchpoint)
12123 loc_first_p = &awp_loc_first;
12124 else
12125 loc_first_p = &bp_loc_first;
12126
12127 if (*loc_first_p == NULL
12128 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12129 || !breakpoint_locations_match (loc, *loc_first_p))
12130 {
12131 *loc_first_p = loc;
12132 loc->duplicate = 0;
12133
12134 if (is_breakpoint (loc->owner) && loc->condition_changed)
12135 {
12136 loc->needs_update = 1;
12137 /* Clear the condition modification flag. */
12138 loc->condition_changed = condition_unchanged;
12139 }
12140 continue;
12141 }
12142
12143
12144 /* This and the above ensure the invariant that the first location
12145 is not duplicated, and is the inserted one.
12146 All following are marked as duplicated, and are not inserted. */
12147 if (loc->inserted)
12148 swap_insertion (loc, *loc_first_p);
12149 loc->duplicate = 1;
12150
12151 /* Clear the condition modification flag. */
12152 loc->condition_changed = condition_unchanged;
12153 }
12154
12155 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12156 {
12157 if (insert_mode != UGLL_DONT_INSERT)
12158 insert_breakpoint_locations ();
12159 else
12160 {
12161 /* Even though the caller told us to not insert new
12162 locations, we may still need to update conditions on the
12163 target's side of breakpoints that were already inserted
12164 if the target is evaluating breakpoint conditions. We
12165 only update conditions for locations that are marked
12166 "needs_update". */
12167 update_inserted_breakpoint_locations ();
12168 }
12169 }
12170
12171 if (insert_mode != UGLL_DONT_INSERT)
12172 download_tracepoint_locations ();
12173 }
12174
12175 void
12176 breakpoint_retire_moribund (void)
12177 {
12178 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12179 {
12180 struct bp_location *loc = moribund_locations[ix];
12181 if (--(loc->events_till_retirement) == 0)
12182 {
12183 decref_bp_location (&loc);
12184 unordered_remove (moribund_locations, ix);
12185 --ix;
12186 }
12187 }
12188 }
12189
12190 static void
12191 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12192 {
12193
12194 try
12195 {
12196 update_global_location_list (insert_mode);
12197 }
12198 catch (const gdb_exception_error &e)
12199 {
12200 }
12201 }
12202
12203 /* Clear BKP from a BPS. */
12204
12205 static void
12206 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12207 {
12208 bpstat bs;
12209
12210 for (bs = bps; bs; bs = bs->next)
12211 if (bs->breakpoint_at == bpt)
12212 {
12213 bs->breakpoint_at = NULL;
12214 bs->old_val = NULL;
12215 /* bs->commands will be freed later. */
12216 }
12217 }
12218
12219 /* Callback for iterate_over_threads. */
12220 static int
12221 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12222 {
12223 struct breakpoint *bpt = (struct breakpoint *) data;
12224
12225 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12226 return 0;
12227 }
12228
12229 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12230 callbacks. */
12231
12232 static void
12233 say_where (struct breakpoint *b)
12234 {
12235 struct value_print_options opts;
12236
12237 get_user_print_options (&opts);
12238
12239 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12240 single string. */
12241 if (b->loc == NULL)
12242 {
12243 /* For pending locations, the output differs slightly based
12244 on b->extra_string. If this is non-NULL, it contains either
12245 a condition or dprintf arguments. */
12246 if (b->extra_string == NULL)
12247 {
12248 printf_filtered (_(" (%s) pending."),
12249 event_location_to_string (b->location.get ()));
12250 }
12251 else if (b->type == bp_dprintf)
12252 {
12253 printf_filtered (_(" (%s,%s) pending."),
12254 event_location_to_string (b->location.get ()),
12255 b->extra_string);
12256 }
12257 else
12258 {
12259 printf_filtered (_(" (%s %s) pending."),
12260 event_location_to_string (b->location.get ()),
12261 b->extra_string);
12262 }
12263 }
12264 else
12265 {
12266 if (opts.addressprint || b->loc->symtab == NULL)
12267 printf_filtered (" at %ps",
12268 styled_string (address_style.style (),
12269 paddress (b->loc->gdbarch,
12270 b->loc->address)));
12271 if (b->loc->symtab != NULL)
12272 {
12273 /* If there is a single location, we can print the location
12274 more nicely. */
12275 if (b->loc->next == NULL)
12276 {
12277 const char *filename
12278 = symtab_to_filename_for_display (b->loc->symtab);
12279 printf_filtered (": file %ps, line %d.",
12280 styled_string (file_name_style.style (),
12281 filename),
12282 b->loc->line_number);
12283 }
12284 else
12285 /* This is not ideal, but each location may have a
12286 different file name, and this at least reflects the
12287 real situation somewhat. */
12288 printf_filtered (": %s.",
12289 event_location_to_string (b->location.get ()));
12290 }
12291
12292 if (b->loc->next)
12293 {
12294 struct bp_location *loc = b->loc;
12295 int n = 0;
12296 for (; loc; loc = loc->next)
12297 ++n;
12298 printf_filtered (" (%d locations)", n);
12299 }
12300 }
12301 }
12302
12303 bp_location::~bp_location ()
12304 {
12305 xfree (function_name);
12306 }
12307
12308 /* Destructor for the breakpoint base class. */
12309
12310 breakpoint::~breakpoint ()
12311 {
12312 xfree (this->cond_string);
12313 xfree (this->extra_string);
12314 }
12315
12316 /* See breakpoint.h. */
12317
12318 bp_locations_range breakpoint::locations ()
12319 {
12320 return bp_locations_range (this->loc);
12321 }
12322
12323 static struct bp_location *
12324 base_breakpoint_allocate_location (struct breakpoint *self)
12325 {
12326 return new bp_location (self);
12327 }
12328
12329 static void
12330 base_breakpoint_re_set (struct breakpoint *b)
12331 {
12332 /* Nothing to re-set. */
12333 }
12334
12335 #define internal_error_pure_virtual_called() \
12336 gdb_assert_not_reached ("pure virtual function called")
12337
12338 static int
12339 base_breakpoint_insert_location (struct bp_location *bl)
12340 {
12341 internal_error_pure_virtual_called ();
12342 }
12343
12344 static int
12345 base_breakpoint_remove_location (struct bp_location *bl,
12346 enum remove_bp_reason reason)
12347 {
12348 internal_error_pure_virtual_called ();
12349 }
12350
12351 static int
12352 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12353 const address_space *aspace,
12354 CORE_ADDR bp_addr,
12355 const struct target_waitstatus *ws)
12356 {
12357 internal_error_pure_virtual_called ();
12358 }
12359
12360 static void
12361 base_breakpoint_check_status (bpstat bs)
12362 {
12363 /* Always stop. */
12364 }
12365
12366 /* A "works_in_software_mode" breakpoint_ops method that just internal
12367 errors. */
12368
12369 static int
12370 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12371 {
12372 internal_error_pure_virtual_called ();
12373 }
12374
12375 /* A "resources_needed" breakpoint_ops method that just internal
12376 errors. */
12377
12378 static int
12379 base_breakpoint_resources_needed (const struct bp_location *bl)
12380 {
12381 internal_error_pure_virtual_called ();
12382 }
12383
12384 static enum print_stop_action
12385 base_breakpoint_print_it (bpstat bs)
12386 {
12387 internal_error_pure_virtual_called ();
12388 }
12389
12390 static void
12391 base_breakpoint_print_one_detail (const struct breakpoint *self,
12392 struct ui_out *uiout)
12393 {
12394 /* nothing */
12395 }
12396
12397 static void
12398 base_breakpoint_print_mention (struct breakpoint *b)
12399 {
12400 internal_error_pure_virtual_called ();
12401 }
12402
12403 static void
12404 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12405 {
12406 internal_error_pure_virtual_called ();
12407 }
12408
12409 static void
12410 base_breakpoint_create_sals_from_location
12411 (struct event_location *location,
12412 struct linespec_result *canonical,
12413 enum bptype type_wanted)
12414 {
12415 internal_error_pure_virtual_called ();
12416 }
12417
12418 static void
12419 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12420 struct linespec_result *c,
12421 gdb::unique_xmalloc_ptr<char> cond_string,
12422 gdb::unique_xmalloc_ptr<char> extra_string,
12423 enum bptype type_wanted,
12424 enum bpdisp disposition,
12425 int thread,
12426 int task, int ignore_count,
12427 const struct breakpoint_ops *o,
12428 int from_tty, int enabled,
12429 int internal, unsigned flags)
12430 {
12431 internal_error_pure_virtual_called ();
12432 }
12433
12434 static std::vector<symtab_and_line>
12435 base_breakpoint_decode_location (struct breakpoint *b,
12436 struct event_location *location,
12437 struct program_space *search_pspace)
12438 {
12439 internal_error_pure_virtual_called ();
12440 }
12441
12442 /* The default 'explains_signal' method. */
12443
12444 static int
12445 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12446 {
12447 return 1;
12448 }
12449
12450 /* The default "after_condition_true" method. */
12451
12452 static void
12453 base_breakpoint_after_condition_true (struct bpstats *bs)
12454 {
12455 /* Nothing to do. */
12456 }
12457
12458 struct breakpoint_ops base_breakpoint_ops =
12459 {
12460 base_breakpoint_allocate_location,
12461 base_breakpoint_re_set,
12462 base_breakpoint_insert_location,
12463 base_breakpoint_remove_location,
12464 base_breakpoint_breakpoint_hit,
12465 base_breakpoint_check_status,
12466 base_breakpoint_resources_needed,
12467 base_breakpoint_works_in_software_mode,
12468 base_breakpoint_print_it,
12469 NULL,
12470 base_breakpoint_print_one_detail,
12471 base_breakpoint_print_mention,
12472 base_breakpoint_print_recreate,
12473 base_breakpoint_create_sals_from_location,
12474 base_breakpoint_create_breakpoints_sal,
12475 base_breakpoint_decode_location,
12476 base_breakpoint_explains_signal,
12477 base_breakpoint_after_condition_true,
12478 };
12479
12480 /* Default breakpoint_ops methods. */
12481
12482 static void
12483 bkpt_re_set (struct breakpoint *b)
12484 {
12485 /* FIXME: is this still reachable? */
12486 if (breakpoint_event_location_empty_p (b))
12487 {
12488 /* Anything without a location can't be re-set. */
12489 delete_breakpoint (b);
12490 return;
12491 }
12492
12493 breakpoint_re_set_default (b);
12494 }
12495
12496 static int
12497 bkpt_insert_location (struct bp_location *bl)
12498 {
12499 CORE_ADDR addr = bl->target_info.reqstd_address;
12500
12501 bl->target_info.kind = breakpoint_kind (bl, &addr);
12502 bl->target_info.placed_address = addr;
12503
12504 if (bl->loc_type == bp_loc_hardware_breakpoint)
12505 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12506 else
12507 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12508 }
12509
12510 static int
12511 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12512 {
12513 if (bl->loc_type == bp_loc_hardware_breakpoint)
12514 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12515 else
12516 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12517 }
12518
12519 static int
12520 bkpt_breakpoint_hit (const struct bp_location *bl,
12521 const address_space *aspace, CORE_ADDR bp_addr,
12522 const struct target_waitstatus *ws)
12523 {
12524 if (ws->kind != TARGET_WAITKIND_STOPPED
12525 || ws->value.sig != GDB_SIGNAL_TRAP)
12526 return 0;
12527
12528 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12529 aspace, bp_addr))
12530 return 0;
12531
12532 if (overlay_debugging /* unmapped overlay section */
12533 && section_is_overlay (bl->section)
12534 && !section_is_mapped (bl->section))
12535 return 0;
12536
12537 return 1;
12538 }
12539
12540 static int
12541 dprintf_breakpoint_hit (const struct bp_location *bl,
12542 const address_space *aspace, CORE_ADDR bp_addr,
12543 const struct target_waitstatus *ws)
12544 {
12545 if (dprintf_style == dprintf_style_agent
12546 && target_can_run_breakpoint_commands ())
12547 {
12548 /* An agent-style dprintf never causes a stop. If we see a trap
12549 for this address it must be for a breakpoint that happens to
12550 be set at the same address. */
12551 return 0;
12552 }
12553
12554 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12555 }
12556
12557 static int
12558 bkpt_resources_needed (const struct bp_location *bl)
12559 {
12560 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12561
12562 return 1;
12563 }
12564
12565 static enum print_stop_action
12566 bkpt_print_it (bpstat bs)
12567 {
12568 struct breakpoint *b;
12569 const struct bp_location *bl;
12570 int bp_temp;
12571 struct ui_out *uiout = current_uiout;
12572
12573 gdb_assert (bs->bp_location_at != NULL);
12574
12575 bl = bs->bp_location_at.get ();
12576 b = bs->breakpoint_at;
12577
12578 bp_temp = b->disposition == disp_del;
12579 if (bl->address != bl->requested_address)
12580 breakpoint_adjustment_warning (bl->requested_address,
12581 bl->address,
12582 b->number, 1);
12583 annotate_breakpoint (b->number);
12584 maybe_print_thread_hit_breakpoint (uiout);
12585
12586 if (uiout->is_mi_like_p ())
12587 {
12588 uiout->field_string ("reason",
12589 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12590 uiout->field_string ("disp", bpdisp_text (b->disposition));
12591 }
12592 if (bp_temp)
12593 uiout->message ("Temporary breakpoint %pF, ",
12594 signed_field ("bkptno", b->number));
12595 else
12596 uiout->message ("Breakpoint %pF, ",
12597 signed_field ("bkptno", b->number));
12598
12599 return PRINT_SRC_AND_LOC;
12600 }
12601
12602 static void
12603 bkpt_print_mention (struct breakpoint *b)
12604 {
12605 if (current_uiout->is_mi_like_p ())
12606 return;
12607
12608 switch (b->type)
12609 {
12610 case bp_breakpoint:
12611 case bp_gnu_ifunc_resolver:
12612 if (b->disposition == disp_del)
12613 printf_filtered (_("Temporary breakpoint"));
12614 else
12615 printf_filtered (_("Breakpoint"));
12616 printf_filtered (_(" %d"), b->number);
12617 if (b->type == bp_gnu_ifunc_resolver)
12618 printf_filtered (_(" at gnu-indirect-function resolver"));
12619 break;
12620 case bp_hardware_breakpoint:
12621 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12622 break;
12623 case bp_dprintf:
12624 printf_filtered (_("Dprintf %d"), b->number);
12625 break;
12626 }
12627
12628 say_where (b);
12629 }
12630
12631 static void
12632 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12633 {
12634 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12635 fprintf_unfiltered (fp, "tbreak");
12636 else if (tp->type == bp_breakpoint)
12637 fprintf_unfiltered (fp, "break");
12638 else if (tp->type == bp_hardware_breakpoint
12639 && tp->disposition == disp_del)
12640 fprintf_unfiltered (fp, "thbreak");
12641 else if (tp->type == bp_hardware_breakpoint)
12642 fprintf_unfiltered (fp, "hbreak");
12643 else
12644 internal_error (__FILE__, __LINE__,
12645 _("unhandled breakpoint type %d"), (int) tp->type);
12646
12647 fprintf_unfiltered (fp, " %s",
12648 event_location_to_string (tp->location.get ()));
12649
12650 /* Print out extra_string if this breakpoint is pending. It might
12651 contain, for example, conditions that were set by the user. */
12652 if (tp->loc == NULL && tp->extra_string != NULL)
12653 fprintf_unfiltered (fp, " %s", tp->extra_string);
12654
12655 print_recreate_thread (tp, fp);
12656 }
12657
12658 static void
12659 bkpt_create_sals_from_location (struct event_location *location,
12660 struct linespec_result *canonical,
12661 enum bptype type_wanted)
12662 {
12663 create_sals_from_location_default (location, canonical, type_wanted);
12664 }
12665
12666 static void
12667 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12668 struct linespec_result *canonical,
12669 gdb::unique_xmalloc_ptr<char> cond_string,
12670 gdb::unique_xmalloc_ptr<char> extra_string,
12671 enum bptype type_wanted,
12672 enum bpdisp disposition,
12673 int thread,
12674 int task, int ignore_count,
12675 const struct breakpoint_ops *ops,
12676 int from_tty, int enabled,
12677 int internal, unsigned flags)
12678 {
12679 create_breakpoints_sal_default (gdbarch, canonical,
12680 std::move (cond_string),
12681 std::move (extra_string),
12682 type_wanted,
12683 disposition, thread, task,
12684 ignore_count, ops, from_tty,
12685 enabled, internal, flags);
12686 }
12687
12688 static std::vector<symtab_and_line>
12689 bkpt_decode_location (struct breakpoint *b,
12690 struct event_location *location,
12691 struct program_space *search_pspace)
12692 {
12693 return decode_location_default (b, location, search_pspace);
12694 }
12695
12696 /* Virtual table for internal breakpoints. */
12697
12698 static void
12699 internal_bkpt_re_set (struct breakpoint *b)
12700 {
12701 switch (b->type)
12702 {
12703 /* Delete overlay event and longjmp master breakpoints; they
12704 will be reset later by breakpoint_re_set. */
12705 case bp_overlay_event:
12706 case bp_longjmp_master:
12707 case bp_std_terminate_master:
12708 case bp_exception_master:
12709 delete_breakpoint (b);
12710 break;
12711
12712 /* This breakpoint is special, it's set up when the inferior
12713 starts and we really don't want to touch it. */
12714 case bp_shlib_event:
12715
12716 /* Like bp_shlib_event, this breakpoint type is special. Once
12717 it is set up, we do not want to touch it. */
12718 case bp_thread_event:
12719 break;
12720 }
12721 }
12722
12723 static void
12724 internal_bkpt_check_status (bpstat bs)
12725 {
12726 if (bs->breakpoint_at->type == bp_shlib_event)
12727 {
12728 /* If requested, stop when the dynamic linker notifies GDB of
12729 events. This allows the user to get control and place
12730 breakpoints in initializer routines for dynamically loaded
12731 objects (among other things). */
12732 bs->stop = stop_on_solib_events;
12733 bs->print = stop_on_solib_events;
12734 }
12735 else
12736 bs->stop = 0;
12737 }
12738
12739 static enum print_stop_action
12740 internal_bkpt_print_it (bpstat bs)
12741 {
12742 struct breakpoint *b;
12743
12744 b = bs->breakpoint_at;
12745
12746 switch (b->type)
12747 {
12748 case bp_shlib_event:
12749 /* Did we stop because the user set the stop_on_solib_events
12750 variable? (If so, we report this as a generic, "Stopped due
12751 to shlib event" message.) */
12752 print_solib_event (0);
12753 break;
12754
12755 case bp_thread_event:
12756 /* Not sure how we will get here.
12757 GDB should not stop for these breakpoints. */
12758 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12759 break;
12760
12761 case bp_overlay_event:
12762 /* By analogy with the thread event, GDB should not stop for these. */
12763 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12764 break;
12765
12766 case bp_longjmp_master:
12767 /* These should never be enabled. */
12768 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12769 break;
12770
12771 case bp_std_terminate_master:
12772 /* These should never be enabled. */
12773 printf_filtered (_("std::terminate Master Breakpoint: "
12774 "gdb should not stop!\n"));
12775 break;
12776
12777 case bp_exception_master:
12778 /* These should never be enabled. */
12779 printf_filtered (_("Exception Master Breakpoint: "
12780 "gdb should not stop!\n"));
12781 break;
12782 }
12783
12784 return PRINT_NOTHING;
12785 }
12786
12787 static void
12788 internal_bkpt_print_mention (struct breakpoint *b)
12789 {
12790 /* Nothing to mention. These breakpoints are internal. */
12791 }
12792
12793 /* Virtual table for momentary breakpoints */
12794
12795 static void
12796 momentary_bkpt_re_set (struct breakpoint *b)
12797 {
12798 /* Keep temporary breakpoints, which can be encountered when we step
12799 over a dlopen call and solib_add is resetting the breakpoints.
12800 Otherwise these should have been blown away via the cleanup chain
12801 or by breakpoint_init_inferior when we rerun the executable. */
12802 }
12803
12804 static void
12805 momentary_bkpt_check_status (bpstat bs)
12806 {
12807 /* Nothing. The point of these breakpoints is causing a stop. */
12808 }
12809
12810 static enum print_stop_action
12811 momentary_bkpt_print_it (bpstat bs)
12812 {
12813 return PRINT_UNKNOWN;
12814 }
12815
12816 static void
12817 momentary_bkpt_print_mention (struct breakpoint *b)
12818 {
12819 /* Nothing to mention. These breakpoints are internal. */
12820 }
12821
12822 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12823
12824 It gets cleared already on the removal of the first one of such placed
12825 breakpoints. This is OK as they get all removed altogether. */
12826
12827 longjmp_breakpoint::~longjmp_breakpoint ()
12828 {
12829 thread_info *tp = find_thread_global_id (this->thread);
12830
12831 if (tp != NULL)
12832 tp->initiating_frame = null_frame_id;
12833 }
12834
12835 /* Specific methods for probe breakpoints. */
12836
12837 static int
12838 bkpt_probe_insert_location (struct bp_location *bl)
12839 {
12840 int v = bkpt_insert_location (bl);
12841
12842 if (v == 0)
12843 {
12844 /* The insertion was successful, now let's set the probe's semaphore
12845 if needed. */
12846 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
12847 }
12848
12849 return v;
12850 }
12851
12852 static int
12853 bkpt_probe_remove_location (struct bp_location *bl,
12854 enum remove_bp_reason reason)
12855 {
12856 /* Let's clear the semaphore before removing the location. */
12857 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
12858
12859 return bkpt_remove_location (bl, reason);
12860 }
12861
12862 static void
12863 bkpt_probe_create_sals_from_location (struct event_location *location,
12864 struct linespec_result *canonical,
12865 enum bptype type_wanted)
12866 {
12867 struct linespec_sals lsal;
12868
12869 lsal.sals = parse_probes (location, NULL, canonical);
12870 lsal.canonical
12871 = xstrdup (event_location_to_string (canonical->location.get ()));
12872 canonical->lsals.push_back (std::move (lsal));
12873 }
12874
12875 static std::vector<symtab_and_line>
12876 bkpt_probe_decode_location (struct breakpoint *b,
12877 struct event_location *location,
12878 struct program_space *search_pspace)
12879 {
12880 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12881 if (sals.empty ())
12882 error (_("probe not found"));
12883 return sals;
12884 }
12885
12886 /* The breakpoint_ops structure to be used in tracepoints. */
12887
12888 static void
12889 tracepoint_re_set (struct breakpoint *b)
12890 {
12891 breakpoint_re_set_default (b);
12892 }
12893
12894 static int
12895 tracepoint_breakpoint_hit (const struct bp_location *bl,
12896 const address_space *aspace, CORE_ADDR bp_addr,
12897 const struct target_waitstatus *ws)
12898 {
12899 /* By definition, the inferior does not report stops at
12900 tracepoints. */
12901 return 0;
12902 }
12903
12904 static void
12905 tracepoint_print_one_detail (const struct breakpoint *self,
12906 struct ui_out *uiout)
12907 {
12908 struct tracepoint *tp = (struct tracepoint *) self;
12909 if (!tp->static_trace_marker_id.empty ())
12910 {
12911 gdb_assert (self->type == bp_static_tracepoint);
12912
12913 uiout->message ("\tmarker id is %pF\n",
12914 string_field ("static-tracepoint-marker-string-id",
12915 tp->static_trace_marker_id.c_str ()));
12916 }
12917 }
12918
12919 static void
12920 tracepoint_print_mention (struct breakpoint *b)
12921 {
12922 if (current_uiout->is_mi_like_p ())
12923 return;
12924
12925 switch (b->type)
12926 {
12927 case bp_tracepoint:
12928 printf_filtered (_("Tracepoint"));
12929 printf_filtered (_(" %d"), b->number);
12930 break;
12931 case bp_fast_tracepoint:
12932 printf_filtered (_("Fast tracepoint"));
12933 printf_filtered (_(" %d"), b->number);
12934 break;
12935 case bp_static_tracepoint:
12936 printf_filtered (_("Static tracepoint"));
12937 printf_filtered (_(" %d"), b->number);
12938 break;
12939 default:
12940 internal_error (__FILE__, __LINE__,
12941 _("unhandled tracepoint type %d"), (int) b->type);
12942 }
12943
12944 say_where (b);
12945 }
12946
12947 static void
12948 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
12949 {
12950 struct tracepoint *tp = (struct tracepoint *) self;
12951
12952 if (self->type == bp_fast_tracepoint)
12953 fprintf_unfiltered (fp, "ftrace");
12954 else if (self->type == bp_static_tracepoint)
12955 fprintf_unfiltered (fp, "strace");
12956 else if (self->type == bp_tracepoint)
12957 fprintf_unfiltered (fp, "trace");
12958 else
12959 internal_error (__FILE__, __LINE__,
12960 _("unhandled tracepoint type %d"), (int) self->type);
12961
12962 fprintf_unfiltered (fp, " %s",
12963 event_location_to_string (self->location.get ()));
12964 print_recreate_thread (self, fp);
12965
12966 if (tp->pass_count)
12967 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
12968 }
12969
12970 static void
12971 tracepoint_create_sals_from_location (struct event_location *location,
12972 struct linespec_result *canonical,
12973 enum bptype type_wanted)
12974 {
12975 create_sals_from_location_default (location, canonical, type_wanted);
12976 }
12977
12978 static void
12979 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12980 struct linespec_result *canonical,
12981 gdb::unique_xmalloc_ptr<char> cond_string,
12982 gdb::unique_xmalloc_ptr<char> extra_string,
12983 enum bptype type_wanted,
12984 enum bpdisp disposition,
12985 int thread,
12986 int task, int ignore_count,
12987 const struct breakpoint_ops *ops,
12988 int from_tty, int enabled,
12989 int internal, unsigned flags)
12990 {
12991 create_breakpoints_sal_default (gdbarch, canonical,
12992 std::move (cond_string),
12993 std::move (extra_string),
12994 type_wanted,
12995 disposition, thread, task,
12996 ignore_count, ops, from_tty,
12997 enabled, internal, flags);
12998 }
12999
13000 static std::vector<symtab_and_line>
13001 tracepoint_decode_location (struct breakpoint *b,
13002 struct event_location *location,
13003 struct program_space *search_pspace)
13004 {
13005 return decode_location_default (b, location, search_pspace);
13006 }
13007
13008 struct breakpoint_ops tracepoint_breakpoint_ops;
13009
13010 /* Virtual table for tracepoints on static probes. */
13011
13012 static void
13013 tracepoint_probe_create_sals_from_location
13014 (struct event_location *location,
13015 struct linespec_result *canonical,
13016 enum bptype type_wanted)
13017 {
13018 /* We use the same method for breakpoint on probes. */
13019 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13020 }
13021
13022 static std::vector<symtab_and_line>
13023 tracepoint_probe_decode_location (struct breakpoint *b,
13024 struct event_location *location,
13025 struct program_space *search_pspace)
13026 {
13027 /* We use the same method for breakpoint on probes. */
13028 return bkpt_probe_decode_location (b, location, search_pspace);
13029 }
13030
13031 /* Dprintf breakpoint_ops methods. */
13032
13033 static void
13034 dprintf_re_set (struct breakpoint *b)
13035 {
13036 breakpoint_re_set_default (b);
13037
13038 /* extra_string should never be non-NULL for dprintf. */
13039 gdb_assert (b->extra_string != NULL);
13040
13041 /* 1 - connect to target 1, that can run breakpoint commands.
13042 2 - create a dprintf, which resolves fine.
13043 3 - disconnect from target 1
13044 4 - connect to target 2, that can NOT run breakpoint commands.
13045
13046 After steps #3/#4, you'll want the dprintf command list to
13047 be updated, because target 1 and 2 may well return different
13048 answers for target_can_run_breakpoint_commands().
13049 Given absence of finer grained resetting, we get to do
13050 it all the time. */
13051 if (b->extra_string != NULL)
13052 update_dprintf_command_list (b);
13053 }
13054
13055 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13056
13057 static void
13058 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13059 {
13060 fprintf_unfiltered (fp, "dprintf %s,%s",
13061 event_location_to_string (tp->location.get ()),
13062 tp->extra_string);
13063 print_recreate_thread (tp, fp);
13064 }
13065
13066 /* Implement the "after_condition_true" breakpoint_ops method for
13067 dprintf.
13068
13069 dprintf's are implemented with regular commands in their command
13070 list, but we run the commands here instead of before presenting the
13071 stop to the user, as dprintf's don't actually cause a stop. This
13072 also makes it so that the commands of multiple dprintfs at the same
13073 address are all handled. */
13074
13075 static void
13076 dprintf_after_condition_true (struct bpstats *bs)
13077 {
13078 struct bpstats tmp_bs;
13079 struct bpstats *tmp_bs_p = &tmp_bs;
13080
13081 /* dprintf's never cause a stop. This wasn't set in the
13082 check_status hook instead because that would make the dprintf's
13083 condition not be evaluated. */
13084 bs->stop = 0;
13085
13086 /* Run the command list here. Take ownership of it instead of
13087 copying. We never want these commands to run later in
13088 bpstat_do_actions, if a breakpoint that causes a stop happens to
13089 be set at same address as this dprintf, or even if running the
13090 commands here throws. */
13091 tmp_bs.commands = bs->commands;
13092 bs->commands = NULL;
13093
13094 bpstat_do_actions_1 (&tmp_bs_p);
13095
13096 /* 'tmp_bs.commands' will usually be NULL by now, but
13097 bpstat_do_actions_1 may return early without processing the whole
13098 list. */
13099 }
13100
13101 /* The breakpoint_ops structure to be used on static tracepoints with
13102 markers (`-m'). */
13103
13104 static void
13105 strace_marker_create_sals_from_location (struct event_location *location,
13106 struct linespec_result *canonical,
13107 enum bptype type_wanted)
13108 {
13109 struct linespec_sals lsal;
13110 const char *arg_start, *arg;
13111
13112 arg = arg_start = get_linespec_location (location)->spec_string;
13113 lsal.sals = decode_static_tracepoint_spec (&arg);
13114
13115 std::string str (arg_start, arg - arg_start);
13116 const char *ptr = str.c_str ();
13117 canonical->location
13118 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
13119
13120 lsal.canonical
13121 = xstrdup (event_location_to_string (canonical->location.get ()));
13122 canonical->lsals.push_back (std::move (lsal));
13123 }
13124
13125 static void
13126 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13127 struct linespec_result *canonical,
13128 gdb::unique_xmalloc_ptr<char> cond_string,
13129 gdb::unique_xmalloc_ptr<char> extra_string,
13130 enum bptype type_wanted,
13131 enum bpdisp disposition,
13132 int thread,
13133 int task, int ignore_count,
13134 const struct breakpoint_ops *ops,
13135 int from_tty, int enabled,
13136 int internal, unsigned flags)
13137 {
13138 const linespec_sals &lsal = canonical->lsals[0];
13139
13140 /* If the user is creating a static tracepoint by marker id
13141 (strace -m MARKER_ID), then store the sals index, so that
13142 breakpoint_re_set can try to match up which of the newly
13143 found markers corresponds to this one, and, don't try to
13144 expand multiple locations for each sal, given than SALS
13145 already should contain all sals for MARKER_ID. */
13146
13147 for (size_t i = 0; i < lsal.sals.size (); i++)
13148 {
13149 event_location_up location
13150 = copy_event_location (canonical->location.get ());
13151
13152 std::unique_ptr<tracepoint> tp (new tracepoint ());
13153 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
13154 std::move (location), NULL,
13155 std::move (cond_string),
13156 std::move (extra_string),
13157 type_wanted, disposition,
13158 thread, task, ignore_count, ops,
13159 from_tty, enabled, internal, flags,
13160 canonical->special_display);
13161 /* Given that its possible to have multiple markers with
13162 the same string id, if the user is creating a static
13163 tracepoint by marker id ("strace -m MARKER_ID"), then
13164 store the sals index, so that breakpoint_re_set can
13165 try to match up which of the newly found markers
13166 corresponds to this one */
13167 tp->static_trace_marker_id_idx = i;
13168
13169 install_breakpoint (internal, std::move (tp), 0);
13170 }
13171 }
13172
13173 static std::vector<symtab_and_line>
13174 strace_marker_decode_location (struct breakpoint *b,
13175 struct event_location *location,
13176 struct program_space *search_pspace)
13177 {
13178 struct tracepoint *tp = (struct tracepoint *) b;
13179 const char *s = get_linespec_location (location)->spec_string;
13180
13181 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13182 if (sals.size () > tp->static_trace_marker_id_idx)
13183 {
13184 sals[0] = sals[tp->static_trace_marker_id_idx];
13185 sals.resize (1);
13186 return sals;
13187 }
13188 else
13189 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
13190 }
13191
13192 static struct breakpoint_ops strace_marker_breakpoint_ops;
13193
13194 static int
13195 strace_marker_p (struct breakpoint *b)
13196 {
13197 return b->ops == &strace_marker_breakpoint_ops;
13198 }
13199
13200 /* Delete a breakpoint and clean up all traces of it in the data
13201 structures. */
13202
13203 void
13204 delete_breakpoint (struct breakpoint *bpt)
13205 {
13206 gdb_assert (bpt != NULL);
13207
13208 /* Has this bp already been deleted? This can happen because
13209 multiple lists can hold pointers to bp's. bpstat lists are
13210 especial culprits.
13211
13212 One example of this happening is a watchpoint's scope bp. When
13213 the scope bp triggers, we notice that the watchpoint is out of
13214 scope, and delete it. We also delete its scope bp. But the
13215 scope bp is marked "auto-deleting", and is already on a bpstat.
13216 That bpstat is then checked for auto-deleting bp's, which are
13217 deleted.
13218
13219 A real solution to this problem might involve reference counts in
13220 bp's, and/or giving them pointers back to their referencing
13221 bpstat's, and teaching delete_breakpoint to only free a bp's
13222 storage when no more references were extent. A cheaper bandaid
13223 was chosen. */
13224 if (bpt->type == bp_none)
13225 return;
13226
13227 /* At least avoid this stale reference until the reference counting
13228 of breakpoints gets resolved. */
13229 if (bpt->related_breakpoint != bpt)
13230 {
13231 struct breakpoint *related;
13232 struct watchpoint *w;
13233
13234 if (bpt->type == bp_watchpoint_scope)
13235 w = (struct watchpoint *) bpt->related_breakpoint;
13236 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13237 w = (struct watchpoint *) bpt;
13238 else
13239 w = NULL;
13240 if (w != NULL)
13241 watchpoint_del_at_next_stop (w);
13242
13243 /* Unlink bpt from the bpt->related_breakpoint ring. */
13244 for (related = bpt; related->related_breakpoint != bpt;
13245 related = related->related_breakpoint);
13246 related->related_breakpoint = bpt->related_breakpoint;
13247 bpt->related_breakpoint = bpt;
13248 }
13249
13250 /* watch_command_1 creates a watchpoint but only sets its number if
13251 update_watchpoint succeeds in creating its bp_locations. If there's
13252 a problem in that process, we'll be asked to delete the half-created
13253 watchpoint. In that case, don't announce the deletion. */
13254 if (bpt->number)
13255 gdb::observers::breakpoint_deleted.notify (bpt);
13256
13257 if (breakpoint_chain == bpt)
13258 breakpoint_chain = bpt->next;
13259
13260 for (breakpoint *b : all_breakpoints ())
13261 if (b->next == bpt)
13262 {
13263 b->next = bpt->next;
13264 break;
13265 }
13266
13267 /* Be sure no bpstat's are pointing at the breakpoint after it's
13268 been freed. */
13269 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13270 in all threads for now. Note that we cannot just remove bpstats
13271 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13272 commands are associated with the bpstat; if we remove it here,
13273 then the later call to bpstat_do_actions (&stop_bpstat); in
13274 event-top.c won't do anything, and temporary breakpoints with
13275 commands won't work. */
13276
13277 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13278
13279 /* Now that breakpoint is removed from breakpoint list, update the
13280 global location list. This will remove locations that used to
13281 belong to this breakpoint. Do this before freeing the breakpoint
13282 itself, since remove_breakpoint looks at location's owner. It
13283 might be better design to have location completely
13284 self-contained, but it's not the case now. */
13285 update_global_location_list (UGLL_DONT_INSERT);
13286
13287 /* On the chance that someone will soon try again to delete this
13288 same bp, we mark it as deleted before freeing its storage. */
13289 bpt->type = bp_none;
13290 delete bpt;
13291 }
13292
13293 /* Iterator function to call a user-provided callback function once
13294 for each of B and its related breakpoints. */
13295
13296 static void
13297 iterate_over_related_breakpoints (struct breakpoint *b,
13298 gdb::function_view<void (breakpoint *)> function)
13299 {
13300 struct breakpoint *related;
13301
13302 related = b;
13303 do
13304 {
13305 struct breakpoint *next;
13306
13307 /* FUNCTION may delete RELATED. */
13308 next = related->related_breakpoint;
13309
13310 if (next == related)
13311 {
13312 /* RELATED is the last ring entry. */
13313 function (related);
13314
13315 /* FUNCTION may have deleted it, so we'd never reach back to
13316 B. There's nothing left to do anyway, so just break
13317 out. */
13318 break;
13319 }
13320 else
13321 function (related);
13322
13323 related = next;
13324 }
13325 while (related != b);
13326 }
13327
13328 static void
13329 delete_command (const char *arg, int from_tty)
13330 {
13331 dont_repeat ();
13332
13333 if (arg == 0)
13334 {
13335 int breaks_to_delete = 0;
13336
13337 /* Delete all breakpoints if no argument. Do not delete
13338 internal breakpoints, these have to be deleted with an
13339 explicit breakpoint number argument. */
13340 for (breakpoint *b : all_breakpoints ())
13341 if (user_breakpoint_p (b))
13342 {
13343 breaks_to_delete = 1;
13344 break;
13345 }
13346
13347 /* Ask user only if there are some breakpoints to delete. */
13348 if (!from_tty
13349 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13350 for (breakpoint *b : all_breakpoints_safe ())
13351 if (user_breakpoint_p (b))
13352 delete_breakpoint (b);
13353 }
13354 else
13355 map_breakpoint_numbers
13356 (arg, [&] (breakpoint *br)
13357 {
13358 iterate_over_related_breakpoints (br, delete_breakpoint);
13359 });
13360 }
13361
13362 /* Return true if all locations of B bound to PSPACE are pending. If
13363 PSPACE is NULL, all locations of all program spaces are
13364 considered. */
13365
13366 static int
13367 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13368 {
13369 for (bp_location *loc : b->locations ())
13370 if ((pspace == NULL
13371 || loc->pspace == pspace)
13372 && !loc->shlib_disabled
13373 && !loc->pspace->executing_startup)
13374 return 0;
13375 return 1;
13376 }
13377
13378 /* Subroutine of update_breakpoint_locations to simplify it.
13379 Return non-zero if multiple fns in list LOC have the same name.
13380 Null names are ignored. */
13381
13382 static int
13383 ambiguous_names_p (struct bp_location *loc)
13384 {
13385 struct bp_location *l;
13386 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
13387 xcalloc, xfree));
13388
13389 for (l = loc; l != NULL; l = l->next)
13390 {
13391 const char **slot;
13392 const char *name = l->function_name;
13393
13394 /* Allow for some names to be NULL, ignore them. */
13395 if (name == NULL)
13396 continue;
13397
13398 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
13399 INSERT);
13400 /* NOTE: We can assume slot != NULL here because xcalloc never
13401 returns NULL. */
13402 if (*slot != NULL)
13403 return 1;
13404 *slot = name;
13405 }
13406
13407 return 0;
13408 }
13409
13410 /* When symbols change, it probably means the sources changed as well,
13411 and it might mean the static tracepoint markers are no longer at
13412 the same address or line numbers they used to be at last we
13413 checked. Losing your static tracepoints whenever you rebuild is
13414 undesirable. This function tries to resync/rematch gdb static
13415 tracepoints with the markers on the target, for static tracepoints
13416 that have not been set by marker id. Static tracepoint that have
13417 been set by marker id are reset by marker id in breakpoint_re_set.
13418 The heuristic is:
13419
13420 1) For a tracepoint set at a specific address, look for a marker at
13421 the old PC. If one is found there, assume to be the same marker.
13422 If the name / string id of the marker found is different from the
13423 previous known name, assume that means the user renamed the marker
13424 in the sources, and output a warning.
13425
13426 2) For a tracepoint set at a given line number, look for a marker
13427 at the new address of the old line number. If one is found there,
13428 assume to be the same marker. If the name / string id of the
13429 marker found is different from the previous known name, assume that
13430 means the user renamed the marker in the sources, and output a
13431 warning.
13432
13433 3) If a marker is no longer found at the same address or line, it
13434 may mean the marker no longer exists. But it may also just mean
13435 the code changed a bit. Maybe the user added a few lines of code
13436 that made the marker move up or down (in line number terms). Ask
13437 the target for info about the marker with the string id as we knew
13438 it. If found, update line number and address in the matching
13439 static tracepoint. This will get confused if there's more than one
13440 marker with the same ID (possible in UST, although unadvised
13441 precisely because it confuses tools). */
13442
13443 static struct symtab_and_line
13444 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13445 {
13446 struct tracepoint *tp = (struct tracepoint *) b;
13447 struct static_tracepoint_marker marker;
13448 CORE_ADDR pc;
13449
13450 pc = sal.pc;
13451 if (sal.line)
13452 find_line_pc (sal.symtab, sal.line, &pc);
13453
13454 if (target_static_tracepoint_marker_at (pc, &marker))
13455 {
13456 if (tp->static_trace_marker_id != marker.str_id)
13457 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13458 b->number, tp->static_trace_marker_id.c_str (),
13459 marker.str_id.c_str ());
13460
13461 tp->static_trace_marker_id = std::move (marker.str_id);
13462
13463 return sal;
13464 }
13465
13466 /* Old marker wasn't found on target at lineno. Try looking it up
13467 by string ID. */
13468 if (!sal.explicit_pc
13469 && sal.line != 0
13470 && sal.symtab != NULL
13471 && !tp->static_trace_marker_id.empty ())
13472 {
13473 std::vector<static_tracepoint_marker> markers
13474 = target_static_tracepoint_markers_by_strid
13475 (tp->static_trace_marker_id.c_str ());
13476
13477 if (!markers.empty ())
13478 {
13479 struct symbol *sym;
13480 struct static_tracepoint_marker *tpmarker;
13481 struct ui_out *uiout = current_uiout;
13482 struct explicit_location explicit_loc;
13483
13484 tpmarker = &markers[0];
13485
13486 tp->static_trace_marker_id = std::move (tpmarker->str_id);
13487
13488 warning (_("marker for static tracepoint %d (%s) not "
13489 "found at previous line number"),
13490 b->number, tp->static_trace_marker_id.c_str ());
13491
13492 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
13493 sym = find_pc_sect_function (tpmarker->address, NULL);
13494 uiout->text ("Now in ");
13495 if (sym)
13496 {
13497 uiout->field_string ("func", sym->print_name (),
13498 function_name_style.style ());
13499 uiout->text (" at ");
13500 }
13501 uiout->field_string ("file",
13502 symtab_to_filename_for_display (sal2.symtab),
13503 file_name_style.style ());
13504 uiout->text (":");
13505
13506 if (uiout->is_mi_like_p ())
13507 {
13508 const char *fullname = symtab_to_fullname (sal2.symtab);
13509
13510 uiout->field_string ("fullname", fullname);
13511 }
13512
13513 uiout->field_signed ("line", sal2.line);
13514 uiout->text ("\n");
13515
13516 b->loc->line_number = sal2.line;
13517 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13518
13519 b->location.reset (NULL);
13520 initialize_explicit_location (&explicit_loc);
13521 explicit_loc.source_filename
13522 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13523 explicit_loc.line_offset.offset = b->loc->line_number;
13524 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13525 b->location = new_explicit_location (&explicit_loc);
13526
13527 /* Might be nice to check if function changed, and warn if
13528 so. */
13529 }
13530 }
13531 return sal;
13532 }
13533
13534 /* Returns 1 iff locations A and B are sufficiently same that
13535 we don't need to report breakpoint as changed. */
13536
13537 static int
13538 locations_are_equal (struct bp_location *a, struct bp_location *b)
13539 {
13540 while (a && b)
13541 {
13542 if (a->address != b->address)
13543 return 0;
13544
13545 if (a->shlib_disabled != b->shlib_disabled)
13546 return 0;
13547
13548 if (a->enabled != b->enabled)
13549 return 0;
13550
13551 if (a->disabled_by_cond != b->disabled_by_cond)
13552 return 0;
13553
13554 a = a->next;
13555 b = b->next;
13556 }
13557
13558 if ((a == NULL) != (b == NULL))
13559 return 0;
13560
13561 return 1;
13562 }
13563
13564 /* Split all locations of B that are bound to PSPACE out of B's
13565 location list to a separate list and return that list's head. If
13566 PSPACE is NULL, hoist out all locations of B. */
13567
13568 static struct bp_location *
13569 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13570 {
13571 struct bp_location head;
13572 struct bp_location *i = b->loc;
13573 struct bp_location **i_link = &b->loc;
13574 struct bp_location *hoisted = &head;
13575
13576 if (pspace == NULL)
13577 {
13578 i = b->loc;
13579 b->loc = NULL;
13580 return i;
13581 }
13582
13583 head.next = NULL;
13584
13585 while (i != NULL)
13586 {
13587 if (i->pspace == pspace)
13588 {
13589 *i_link = i->next;
13590 i->next = NULL;
13591 hoisted->next = i;
13592 hoisted = i;
13593 }
13594 else
13595 i_link = &i->next;
13596 i = *i_link;
13597 }
13598
13599 return head.next;
13600 }
13601
13602 /* Create new breakpoint locations for B (a hardware or software
13603 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13604 zero, then B is a ranged breakpoint. Only recreates locations for
13605 FILTER_PSPACE. Locations of other program spaces are left
13606 untouched. */
13607
13608 void
13609 update_breakpoint_locations (struct breakpoint *b,
13610 struct program_space *filter_pspace,
13611 gdb::array_view<const symtab_and_line> sals,
13612 gdb::array_view<const symtab_and_line> sals_end)
13613 {
13614 struct bp_location *existing_locations;
13615
13616 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
13617 {
13618 /* Ranged breakpoints have only one start location and one end
13619 location. */
13620 b->enable_state = bp_disabled;
13621 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13622 "multiple locations found\n"),
13623 b->number);
13624 return;
13625 }
13626
13627 /* If there's no new locations, and all existing locations are
13628 pending, don't do anything. This optimizes the common case where
13629 all locations are in the same shared library, that was unloaded.
13630 We'd like to retain the location, so that when the library is
13631 loaded again, we don't loose the enabled/disabled status of the
13632 individual locations. */
13633 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
13634 return;
13635
13636 existing_locations = hoist_existing_locations (b, filter_pspace);
13637
13638 for (const auto &sal : sals)
13639 {
13640 struct bp_location *new_loc;
13641
13642 switch_to_program_space_and_thread (sal.pspace);
13643
13644 new_loc = add_location_to_breakpoint (b, &sal);
13645
13646 /* Reparse conditions, they might contain references to the
13647 old symtab. */
13648 if (b->cond_string != NULL)
13649 {
13650 const char *s;
13651
13652 s = b->cond_string;
13653 try
13654 {
13655 new_loc->cond = parse_exp_1 (&s, sal.pc,
13656 block_for_pc (sal.pc),
13657 0);
13658 }
13659 catch (const gdb_exception_error &e)
13660 {
13661 new_loc->disabled_by_cond = true;
13662 }
13663 }
13664
13665 if (!sals_end.empty ())
13666 {
13667 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13668
13669 new_loc->length = end - sals[0].pc + 1;
13670 }
13671 }
13672
13673 /* If possible, carry over 'disable' status from existing
13674 breakpoints. */
13675 {
13676 struct bp_location *e = existing_locations;
13677 /* If there are multiple breakpoints with the same function name,
13678 e.g. for inline functions, comparing function names won't work.
13679 Instead compare pc addresses; this is just a heuristic as things
13680 may have moved, but in practice it gives the correct answer
13681 often enough until a better solution is found. */
13682 int have_ambiguous_names = ambiguous_names_p (b->loc);
13683
13684 for (; e; e = e->next)
13685 {
13686 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
13687 {
13688 if (have_ambiguous_names)
13689 {
13690 for (bp_location *l : b->locations ())
13691 {
13692 /* Ignore software vs hardware location type at
13693 this point, because with "set breakpoint
13694 auto-hw", after a re-set, locations that were
13695 hardware can end up as software, or vice versa.
13696 As mentioned above, this is an heuristic and in
13697 practice should give the correct answer often
13698 enough. */
13699 if (breakpoint_locations_match (e, l, true))
13700 {
13701 l->enabled = e->enabled;
13702 l->disabled_by_cond = e->disabled_by_cond;
13703 break;
13704 }
13705 }
13706 }
13707 else
13708 {
13709 for (bp_location *l : b->locations ())
13710 if (l->function_name
13711 && strcmp (e->function_name, l->function_name) == 0)
13712 {
13713 l->enabled = e->enabled;
13714 l->disabled_by_cond = e->disabled_by_cond;
13715 break;
13716 }
13717 }
13718 }
13719 }
13720 }
13721
13722 if (!locations_are_equal (existing_locations, b->loc))
13723 gdb::observers::breakpoint_modified.notify (b);
13724 }
13725
13726 /* Find the SaL locations corresponding to the given LOCATION.
13727 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13728
13729 static std::vector<symtab_and_line>
13730 location_to_sals (struct breakpoint *b, struct event_location *location,
13731 struct program_space *search_pspace, int *found)
13732 {
13733 struct gdb_exception exception;
13734
13735 gdb_assert (b->ops != NULL);
13736
13737 std::vector<symtab_and_line> sals;
13738
13739 try
13740 {
13741 sals = b->ops->decode_location (b, location, search_pspace);
13742 }
13743 catch (gdb_exception_error &e)
13744 {
13745 int not_found_and_ok = 0;
13746
13747 /* For pending breakpoints, it's expected that parsing will
13748 fail until the right shared library is loaded. User has
13749 already told to create pending breakpoints and don't need
13750 extra messages. If breakpoint is in bp_shlib_disabled
13751 state, then user already saw the message about that
13752 breakpoint being disabled, and don't want to see more
13753 errors. */
13754 if (e.error == NOT_FOUND_ERROR
13755 && (b->condition_not_parsed
13756 || (b->loc != NULL
13757 && search_pspace != NULL
13758 && b->loc->pspace != search_pspace)
13759 || (b->loc && b->loc->shlib_disabled)
13760 || (b->loc && b->loc->pspace->executing_startup)
13761 || b->enable_state == bp_disabled))
13762 not_found_and_ok = 1;
13763
13764 if (!not_found_and_ok)
13765 {
13766 /* We surely don't want to warn about the same breakpoint
13767 10 times. One solution, implemented here, is disable
13768 the breakpoint on error. Another solution would be to
13769 have separate 'warning emitted' flag. Since this
13770 happens only when a binary has changed, I don't know
13771 which approach is better. */
13772 b->enable_state = bp_disabled;
13773 throw;
13774 }
13775
13776 exception = std::move (e);
13777 }
13778
13779 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13780 {
13781 for (auto &sal : sals)
13782 resolve_sal_pc (&sal);
13783 if (b->condition_not_parsed && b->extra_string != NULL)
13784 {
13785 char *cond_string, *extra_string;
13786 int thread, task;
13787
13788 find_condition_and_thread_for_sals (sals, b->extra_string,
13789 &cond_string, &thread,
13790 &task, &extra_string);
13791 gdb_assert (b->cond_string == NULL);
13792 if (cond_string)
13793 b->cond_string = cond_string;
13794 b->thread = thread;
13795 b->task = task;
13796 if (extra_string)
13797 {
13798 xfree (b->extra_string);
13799 b->extra_string = extra_string;
13800 }
13801 b->condition_not_parsed = 0;
13802 }
13803
13804 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13805 sals[0] = update_static_tracepoint (b, sals[0]);
13806
13807 *found = 1;
13808 }
13809 else
13810 *found = 0;
13811
13812 return sals;
13813 }
13814
13815 /* The default re_set method, for typical hardware or software
13816 breakpoints. Reevaluate the breakpoint and recreate its
13817 locations. */
13818
13819 static void
13820 breakpoint_re_set_default (struct breakpoint *b)
13821 {
13822 struct program_space *filter_pspace = current_program_space;
13823 std::vector<symtab_and_line> expanded, expanded_end;
13824
13825 int found;
13826 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13827 filter_pspace, &found);
13828 if (found)
13829 expanded = std::move (sals);
13830
13831 if (b->location_range_end != NULL)
13832 {
13833 std::vector<symtab_and_line> sals_end
13834 = location_to_sals (b, b->location_range_end.get (),
13835 filter_pspace, &found);
13836 if (found)
13837 expanded_end = std::move (sals_end);
13838 }
13839
13840 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
13841 }
13842
13843 /* Default method for creating SALs from an address string. It basically
13844 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13845
13846 static void
13847 create_sals_from_location_default (struct event_location *location,
13848 struct linespec_result *canonical,
13849 enum bptype type_wanted)
13850 {
13851 parse_breakpoint_sals (location, canonical);
13852 }
13853
13854 /* Call create_breakpoints_sal for the given arguments. This is the default
13855 function for the `create_breakpoints_sal' method of
13856 breakpoint_ops. */
13857
13858 static void
13859 create_breakpoints_sal_default (struct gdbarch *gdbarch,
13860 struct linespec_result *canonical,
13861 gdb::unique_xmalloc_ptr<char> cond_string,
13862 gdb::unique_xmalloc_ptr<char> extra_string,
13863 enum bptype type_wanted,
13864 enum bpdisp disposition,
13865 int thread,
13866 int task, int ignore_count,
13867 const struct breakpoint_ops *ops,
13868 int from_tty, int enabled,
13869 int internal, unsigned flags)
13870 {
13871 create_breakpoints_sal (gdbarch, canonical,
13872 std::move (cond_string),
13873 std::move (extra_string),
13874 type_wanted, disposition,
13875 thread, task, ignore_count, ops, from_tty,
13876 enabled, internal, flags);
13877 }
13878
13879 /* Decode the line represented by S by calling decode_line_full. This is the
13880 default function for the `decode_location' method of breakpoint_ops. */
13881
13882 static std::vector<symtab_and_line>
13883 decode_location_default (struct breakpoint *b,
13884 struct event_location *location,
13885 struct program_space *search_pspace)
13886 {
13887 struct linespec_result canonical;
13888
13889 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
13890 NULL, 0, &canonical, multiple_symbols_all,
13891 b->filter.get ());
13892
13893 /* We should get 0 or 1 resulting SALs. */
13894 gdb_assert (canonical.lsals.size () < 2);
13895
13896 if (!canonical.lsals.empty ())
13897 {
13898 const linespec_sals &lsal = canonical.lsals[0];
13899 return std::move (lsal.sals);
13900 }
13901 return {};
13902 }
13903
13904 /* Reset a breakpoint. */
13905
13906 static void
13907 breakpoint_re_set_one (breakpoint *b)
13908 {
13909 input_radix = b->input_radix;
13910 set_language (b->language);
13911
13912 b->ops->re_set (b);
13913 }
13914
13915 /* Re-set breakpoint locations for the current program space.
13916 Locations bound to other program spaces are left untouched. */
13917
13918 void
13919 breakpoint_re_set (void)
13920 {
13921 {
13922 scoped_restore_current_language save_language;
13923 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
13924 scoped_restore_current_pspace_and_thread restore_pspace_thread;
13925
13926 /* breakpoint_re_set_one sets the current_language to the language
13927 of the breakpoint it is resetting (see prepare_re_set_context)
13928 before re-evaluating the breakpoint's location. This change can
13929 unfortunately get undone by accident if the language_mode is set
13930 to auto, and we either switch frames, or more likely in this context,
13931 we select the current frame.
13932
13933 We prevent this by temporarily turning the language_mode to
13934 language_mode_manual. We restore it once all breakpoints
13935 have been reset. */
13936 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13937 language_mode = language_mode_manual;
13938
13939 /* Note: we must not try to insert locations until after all
13940 breakpoints have been re-set. Otherwise, e.g., when re-setting
13941 breakpoint 1, we'd insert the locations of breakpoint 2, which
13942 hadn't been re-set yet, and thus may have stale locations. */
13943
13944 for (breakpoint *b : all_breakpoints_safe ())
13945 {
13946 try
13947 {
13948 breakpoint_re_set_one (b);
13949 }
13950 catch (const gdb_exception &ex)
13951 {
13952 exception_fprintf (gdb_stderr, ex,
13953 "Error in re-setting breakpoint %d: ",
13954 b->number);
13955 }
13956 }
13957
13958 jit_breakpoint_re_set ();
13959 }
13960
13961 create_overlay_event_breakpoint ();
13962 create_longjmp_master_breakpoint ();
13963 create_std_terminate_master_breakpoint ();
13964 create_exception_master_breakpoint ();
13965
13966 /* Now we can insert. */
13967 update_global_location_list (UGLL_MAY_INSERT);
13968 }
13969 \f
13970 /* Reset the thread number of this breakpoint:
13971
13972 - If the breakpoint is for all threads, leave it as-is.
13973 - Else, reset it to the current thread for inferior_ptid. */
13974 void
13975 breakpoint_re_set_thread (struct breakpoint *b)
13976 {
13977 if (b->thread != -1)
13978 {
13979 b->thread = inferior_thread ()->global_num;
13980
13981 /* We're being called after following a fork. The new fork is
13982 selected as current, and unless this was a vfork will have a
13983 different program space from the original thread. Reset that
13984 as well. */
13985 b->loc->pspace = current_program_space;
13986 }
13987 }
13988
13989 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13990 If from_tty is nonzero, it prints a message to that effect,
13991 which ends with a period (no newline). */
13992
13993 void
13994 set_ignore_count (int bptnum, int count, int from_tty)
13995 {
13996 if (count < 0)
13997 count = 0;
13998
13999 for (breakpoint *b : all_breakpoints ())
14000 if (b->number == bptnum)
14001 {
14002 if (is_tracepoint (b))
14003 {
14004 if (from_tty && count != 0)
14005 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14006 bptnum);
14007 return;
14008 }
14009
14010 b->ignore_count = count;
14011 if (from_tty)
14012 {
14013 if (count == 0)
14014 printf_filtered (_("Will stop next time "
14015 "breakpoint %d is reached."),
14016 bptnum);
14017 else if (count == 1)
14018 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14019 bptnum);
14020 else
14021 printf_filtered (_("Will ignore next %d "
14022 "crossings of breakpoint %d."),
14023 count, bptnum);
14024 }
14025 gdb::observers::breakpoint_modified.notify (b);
14026 return;
14027 }
14028
14029 error (_("No breakpoint number %d."), bptnum);
14030 }
14031
14032 /* Command to set ignore-count of breakpoint N to COUNT. */
14033
14034 static void
14035 ignore_command (const char *args, int from_tty)
14036 {
14037 const char *p = args;
14038 int num;
14039
14040 if (p == 0)
14041 error_no_arg (_("a breakpoint number"));
14042
14043 num = get_number (&p);
14044 if (num == 0)
14045 error (_("bad breakpoint number: '%s'"), args);
14046 if (*p == 0)
14047 error (_("Second argument (specified ignore-count) is missing."));
14048
14049 set_ignore_count (num,
14050 longest_to_int (value_as_long (parse_and_eval (p))),
14051 from_tty);
14052 if (from_tty)
14053 printf_filtered ("\n");
14054 }
14055 \f
14056
14057 /* Call FUNCTION on each of the breakpoints with numbers in the range
14058 defined by BP_NUM_RANGE (an inclusive range). */
14059
14060 static void
14061 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
14062 gdb::function_view<void (breakpoint *)> function)
14063 {
14064 if (bp_num_range.first == 0)
14065 {
14066 warning (_("bad breakpoint number at or near '%d'"),
14067 bp_num_range.first);
14068 }
14069 else
14070 {
14071 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
14072 {
14073 bool match = false;
14074
14075 for (breakpoint *b : all_breakpoints_safe ())
14076 if (b->number == i)
14077 {
14078 match = true;
14079 function (b);
14080 break;
14081 }
14082 if (!match)
14083 printf_unfiltered (_("No breakpoint number %d.\n"), i);
14084 }
14085 }
14086 }
14087
14088 /* Call FUNCTION on each of the breakpoints whose numbers are given in
14089 ARGS. */
14090
14091 static void
14092 map_breakpoint_numbers (const char *args,
14093 gdb::function_view<void (breakpoint *)> function)
14094 {
14095 if (args == NULL || *args == '\0')
14096 error_no_arg (_("one or more breakpoint numbers"));
14097
14098 number_or_range_parser parser (args);
14099
14100 while (!parser.finished ())
14101 {
14102 int num = parser.get_number ();
14103 map_breakpoint_number_range (std::make_pair (num, num), function);
14104 }
14105 }
14106
14107 /* Return the breakpoint location structure corresponding to the
14108 BP_NUM and LOC_NUM values. */
14109
14110 static struct bp_location *
14111 find_location_by_number (int bp_num, int loc_num)
14112 {
14113 breakpoint *b = get_breakpoint (bp_num);
14114
14115 if (!b || b->number != bp_num)
14116 error (_("Bad breakpoint number '%d'"), bp_num);
14117
14118 if (loc_num == 0)
14119 error (_("Bad breakpoint location number '%d'"), loc_num);
14120
14121 int n = 0;
14122 for (bp_location *loc : b->locations ())
14123 if (++n == loc_num)
14124 return loc;
14125
14126 error (_("Bad breakpoint location number '%d'"), loc_num);
14127 }
14128
14129 /* Modes of operation for extract_bp_num. */
14130 enum class extract_bp_kind
14131 {
14132 /* Extracting a breakpoint number. */
14133 bp,
14134
14135 /* Extracting a location number. */
14136 loc,
14137 };
14138
14139 /* Extract a breakpoint or location number (as determined by KIND)
14140 from the string starting at START. TRAILER is a character which
14141 can be found after the number. If you don't want a trailer, use
14142 '\0'. If END_OUT is not NULL, it is set to point after the parsed
14143 string. This always returns a positive integer. */
14144
14145 static int
14146 extract_bp_num (extract_bp_kind kind, const char *start,
14147 int trailer, const char **end_out = NULL)
14148 {
14149 const char *end = start;
14150 int num = get_number_trailer (&end, trailer);
14151 if (num < 0)
14152 error (kind == extract_bp_kind::bp
14153 ? _("Negative breakpoint number '%.*s'")
14154 : _("Negative breakpoint location number '%.*s'"),
14155 int (end - start), start);
14156 if (num == 0)
14157 error (kind == extract_bp_kind::bp
14158 ? _("Bad breakpoint number '%.*s'")
14159 : _("Bad breakpoint location number '%.*s'"),
14160 int (end - start), start);
14161
14162 if (end_out != NULL)
14163 *end_out = end;
14164 return num;
14165 }
14166
14167 /* Extract a breakpoint or location range (as determined by KIND) in
14168 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14169 representing the (inclusive) range. The returned pair's elements
14170 are always positive integers. */
14171
14172 static std::pair<int, int>
14173 extract_bp_or_bp_range (extract_bp_kind kind,
14174 const std::string &arg,
14175 std::string::size_type arg_offset)
14176 {
14177 std::pair<int, int> range;
14178 const char *bp_loc = &arg[arg_offset];
14179 std::string::size_type dash = arg.find ('-', arg_offset);
14180 if (dash != std::string::npos)
14181 {
14182 /* bp_loc is a range (x-z). */
14183 if (arg.length () == dash + 1)
14184 error (kind == extract_bp_kind::bp
14185 ? _("Bad breakpoint number at or near: '%s'")
14186 : _("Bad breakpoint location number at or near: '%s'"),
14187 bp_loc);
14188
14189 const char *end;
14190 const char *start_first = bp_loc;
14191 const char *start_second = &arg[dash + 1];
14192 range.first = extract_bp_num (kind, start_first, '-');
14193 range.second = extract_bp_num (kind, start_second, '\0', &end);
14194
14195 if (range.first > range.second)
14196 error (kind == extract_bp_kind::bp
14197 ? _("Inverted breakpoint range at '%.*s'")
14198 : _("Inverted breakpoint location range at '%.*s'"),
14199 int (end - start_first), start_first);
14200 }
14201 else
14202 {
14203 /* bp_loc is a single value. */
14204 range.first = extract_bp_num (kind, bp_loc, '\0');
14205 range.second = range.first;
14206 }
14207 return range;
14208 }
14209
14210 /* Extract the breakpoint/location range specified by ARG. Returns
14211 the breakpoint range in BP_NUM_RANGE, and the location range in
14212 BP_LOC_RANGE.
14213
14214 ARG may be in any of the following forms:
14215
14216 x where 'x' is a breakpoint number.
14217 x-y where 'x' and 'y' specify a breakpoint numbers range.
14218 x.y where 'x' is a breakpoint number and 'y' a location number.
14219 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14220 location number range.
14221 */
14222
14223 static void
14224 extract_bp_number_and_location (const std::string &arg,
14225 std::pair<int, int> &bp_num_range,
14226 std::pair<int, int> &bp_loc_range)
14227 {
14228 std::string::size_type dot = arg.find ('.');
14229
14230 if (dot != std::string::npos)
14231 {
14232 /* Handle 'x.y' and 'x.y-z' cases. */
14233
14234 if (arg.length () == dot + 1 || dot == 0)
14235 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
14236
14237 bp_num_range.first
14238 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14239 bp_num_range.second = bp_num_range.first;
14240
14241 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14242 arg, dot + 1);
14243 }
14244 else
14245 {
14246 /* Handle x and x-y cases. */
14247
14248 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
14249 bp_loc_range.first = 0;
14250 bp_loc_range.second = 0;
14251 }
14252 }
14253
14254 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14255 specifies whether to enable or disable. */
14256
14257 static void
14258 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14259 {
14260 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14261 if (loc != NULL)
14262 {
14263 if (loc->disabled_by_cond && enable)
14264 error (_("Breakpoint %d's condition is invalid at location %d, "
14265 "cannot enable."), bp_num, loc_num);
14266
14267 if (loc->enabled != enable)
14268 {
14269 loc->enabled = enable;
14270 mark_breakpoint_location_modified (loc);
14271 }
14272 if (target_supports_enable_disable_tracepoint ()
14273 && current_trace_status ()->running && loc->owner
14274 && is_tracepoint (loc->owner))
14275 target_disable_tracepoint (loc);
14276 }
14277 update_global_location_list (UGLL_DONT_INSERT);
14278
14279 gdb::observers::breakpoint_modified.notify (loc->owner);
14280 }
14281
14282 /* Enable or disable a range of breakpoint locations. BP_NUM is the
14283 number of the breakpoint, and BP_LOC_RANGE specifies the
14284 (inclusive) range of location numbers of that breakpoint to
14285 enable/disable. ENABLE specifies whether to enable or disable the
14286 location. */
14287
14288 static void
14289 enable_disable_breakpoint_location_range (int bp_num,
14290 std::pair<int, int> &bp_loc_range,
14291 bool enable)
14292 {
14293 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14294 enable_disable_bp_num_loc (bp_num, i, enable);
14295 }
14296
14297 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14298 If from_tty is nonzero, it prints a message to that effect,
14299 which ends with a period (no newline). */
14300
14301 void
14302 disable_breakpoint (struct breakpoint *bpt)
14303 {
14304 /* Never disable a watchpoint scope breakpoint; we want to
14305 hit them when we leave scope so we can delete both the
14306 watchpoint and its scope breakpoint at that time. */
14307 if (bpt->type == bp_watchpoint_scope)
14308 return;
14309
14310 bpt->enable_state = bp_disabled;
14311
14312 /* Mark breakpoint locations modified. */
14313 mark_breakpoint_modified (bpt);
14314
14315 if (target_supports_enable_disable_tracepoint ()
14316 && current_trace_status ()->running && is_tracepoint (bpt))
14317 {
14318 for (bp_location *location : bpt->locations ())
14319 target_disable_tracepoint (location);
14320 }
14321
14322 update_global_location_list (UGLL_DONT_INSERT);
14323
14324 gdb::observers::breakpoint_modified.notify (bpt);
14325 }
14326
14327 /* Enable or disable the breakpoint(s) or breakpoint location(s)
14328 specified in ARGS. ARGS may be in any of the formats handled by
14329 extract_bp_number_and_location. ENABLE specifies whether to enable
14330 or disable the breakpoints/locations. */
14331
14332 static void
14333 enable_disable_command (const char *args, int from_tty, bool enable)
14334 {
14335 if (args == 0)
14336 {
14337 for (breakpoint *bpt : all_breakpoints ())
14338 if (user_breakpoint_p (bpt))
14339 {
14340 if (enable)
14341 enable_breakpoint (bpt);
14342 else
14343 disable_breakpoint (bpt);
14344 }
14345 }
14346 else
14347 {
14348 std::string num = extract_arg (&args);
14349
14350 while (!num.empty ())
14351 {
14352 std::pair<int, int> bp_num_range, bp_loc_range;
14353
14354 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14355
14356 if (bp_loc_range.first == bp_loc_range.second
14357 && bp_loc_range.first == 0)
14358 {
14359 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14360 map_breakpoint_number_range (bp_num_range,
14361 enable
14362 ? enable_breakpoint
14363 : disable_breakpoint);
14364 }
14365 else
14366 {
14367 /* Handle breakpoint ids with formats 'x.y' or
14368 'x.y-z'. */
14369 enable_disable_breakpoint_location_range
14370 (bp_num_range.first, bp_loc_range, enable);
14371 }
14372 num = extract_arg (&args);
14373 }
14374 }
14375 }
14376
14377 /* The disable command disables the specified breakpoints/locations
14378 (or all defined breakpoints) so they're no longer effective in
14379 stopping the inferior. ARGS may be in any of the forms defined in
14380 extract_bp_number_and_location. */
14381
14382 static void
14383 disable_command (const char *args, int from_tty)
14384 {
14385 enable_disable_command (args, from_tty, false);
14386 }
14387
14388 static void
14389 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14390 int count)
14391 {
14392 int target_resources_ok;
14393
14394 if (bpt->type == bp_hardware_breakpoint)
14395 {
14396 int i;
14397 i = hw_breakpoint_used_count ();
14398 target_resources_ok =
14399 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14400 i + 1, 0);
14401 if (target_resources_ok == 0)
14402 error (_("No hardware breakpoint support in the target."));
14403 else if (target_resources_ok < 0)
14404 error (_("Hardware breakpoints used exceeds limit."));
14405 }
14406
14407 if (is_watchpoint (bpt))
14408 {
14409 /* Initialize it just to avoid a GCC false warning. */
14410 enum enable_state orig_enable_state = bp_disabled;
14411
14412 try
14413 {
14414 struct watchpoint *w = (struct watchpoint *) bpt;
14415
14416 orig_enable_state = bpt->enable_state;
14417 bpt->enable_state = bp_enabled;
14418 update_watchpoint (w, 1 /* reparse */);
14419 }
14420 catch (const gdb_exception &e)
14421 {
14422 bpt->enable_state = orig_enable_state;
14423 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14424 bpt->number);
14425 return;
14426 }
14427 }
14428
14429 bpt->enable_state = bp_enabled;
14430
14431 /* Mark breakpoint locations modified. */
14432 mark_breakpoint_modified (bpt);
14433
14434 if (target_supports_enable_disable_tracepoint ()
14435 && current_trace_status ()->running && is_tracepoint (bpt))
14436 {
14437 for (bp_location *location : bpt->locations ())
14438 target_enable_tracepoint (location);
14439 }
14440
14441 bpt->disposition = disposition;
14442 bpt->enable_count = count;
14443 update_global_location_list (UGLL_MAY_INSERT);
14444
14445 gdb::observers::breakpoint_modified.notify (bpt);
14446 }
14447
14448
14449 void
14450 enable_breakpoint (struct breakpoint *bpt)
14451 {
14452 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14453 }
14454
14455 /* The enable command enables the specified breakpoints/locations (or
14456 all defined breakpoints) so they once again become (or continue to
14457 be) effective in stopping the inferior. ARGS may be in any of the
14458 forms defined in extract_bp_number_and_location. */
14459
14460 static void
14461 enable_command (const char *args, int from_tty)
14462 {
14463 enable_disable_command (args, from_tty, true);
14464 }
14465
14466 static void
14467 enable_once_command (const char *args, int from_tty)
14468 {
14469 map_breakpoint_numbers
14470 (args, [&] (breakpoint *b)
14471 {
14472 iterate_over_related_breakpoints
14473 (b, [&] (breakpoint *bpt)
14474 {
14475 enable_breakpoint_disp (bpt, disp_disable, 1);
14476 });
14477 });
14478 }
14479
14480 static void
14481 enable_count_command (const char *args, int from_tty)
14482 {
14483 int count;
14484
14485 if (args == NULL)
14486 error_no_arg (_("hit count"));
14487
14488 count = get_number (&args);
14489
14490 map_breakpoint_numbers
14491 (args, [&] (breakpoint *b)
14492 {
14493 iterate_over_related_breakpoints
14494 (b, [&] (breakpoint *bpt)
14495 {
14496 enable_breakpoint_disp (bpt, disp_disable, count);
14497 });
14498 });
14499 }
14500
14501 static void
14502 enable_delete_command (const char *args, int from_tty)
14503 {
14504 map_breakpoint_numbers
14505 (args, [&] (breakpoint *b)
14506 {
14507 iterate_over_related_breakpoints
14508 (b, [&] (breakpoint *bpt)
14509 {
14510 enable_breakpoint_disp (bpt, disp_del, 1);
14511 });
14512 });
14513 }
14514 \f
14515 /* Invalidate last known value of any hardware watchpoint if
14516 the memory which that value represents has been written to by
14517 GDB itself. */
14518
14519 static void
14520 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14521 CORE_ADDR addr, ssize_t len,
14522 const bfd_byte *data)
14523 {
14524 for (breakpoint *bp : all_breakpoints ())
14525 if (bp->enable_state == bp_enabled
14526 && bp->type == bp_hardware_watchpoint)
14527 {
14528 struct watchpoint *wp = (struct watchpoint *) bp;
14529
14530 if (wp->val_valid && wp->val != nullptr)
14531 {
14532 for (bp_location *loc : bp->locations ())
14533 if (loc->loc_type == bp_loc_hardware_watchpoint
14534 && loc->address + loc->length > addr
14535 && addr + len > loc->address)
14536 {
14537 wp->val = NULL;
14538 wp->val_valid = false;
14539 }
14540 }
14541 }
14542 }
14543
14544 /* Create and insert a breakpoint for software single step. */
14545
14546 void
14547 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14548 const address_space *aspace,
14549 CORE_ADDR next_pc)
14550 {
14551 struct thread_info *tp = inferior_thread ();
14552 struct symtab_and_line sal;
14553 CORE_ADDR pc = next_pc;
14554
14555 if (tp->control.single_step_breakpoints == NULL)
14556 {
14557 tp->control.single_step_breakpoints
14558 = new_single_step_breakpoint (tp->global_num, gdbarch);
14559 }
14560
14561 sal = find_pc_line (pc, 0);
14562 sal.pc = pc;
14563 sal.section = find_pc_overlay (pc);
14564 sal.explicit_pc = 1;
14565 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14566
14567 update_global_location_list (UGLL_INSERT);
14568 }
14569
14570 /* Insert single step breakpoints according to the current state. */
14571
14572 int
14573 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14574 {
14575 struct regcache *regcache = get_current_regcache ();
14576 std::vector<CORE_ADDR> next_pcs;
14577
14578 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14579
14580 if (!next_pcs.empty ())
14581 {
14582 struct frame_info *frame = get_current_frame ();
14583 const address_space *aspace = get_frame_address_space (frame);
14584
14585 for (CORE_ADDR pc : next_pcs)
14586 insert_single_step_breakpoint (gdbarch, aspace, pc);
14587
14588 return 1;
14589 }
14590 else
14591 return 0;
14592 }
14593
14594 /* See breakpoint.h. */
14595
14596 int
14597 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14598 const address_space *aspace,
14599 CORE_ADDR pc)
14600 {
14601 for (bp_location *loc : bp->locations ())
14602 if (loc->inserted
14603 && breakpoint_location_address_match (loc, aspace, pc))
14604 return 1;
14605
14606 return 0;
14607 }
14608
14609 /* Check whether a software single-step breakpoint is inserted at
14610 PC. */
14611
14612 int
14613 single_step_breakpoint_inserted_here_p (const address_space *aspace,
14614 CORE_ADDR pc)
14615 {
14616 for (breakpoint *bpt : all_breakpoints ())
14617 {
14618 if (bpt->type == bp_single_step
14619 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14620 return 1;
14621 }
14622 return 0;
14623 }
14624
14625 /* Tracepoint-specific operations. */
14626
14627 /* Set tracepoint count to NUM. */
14628 static void
14629 set_tracepoint_count (int num)
14630 {
14631 tracepoint_count = num;
14632 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14633 }
14634
14635 static void
14636 trace_command (const char *arg, int from_tty)
14637 {
14638 event_location_up location = string_to_event_location (&arg,
14639 current_language);
14640 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
14641 (location.get (), true /* is_tracepoint */);
14642
14643 create_breakpoint (get_current_arch (),
14644 location.get (),
14645 NULL, 0, arg, false, 1 /* parse arg */,
14646 0 /* tempflag */,
14647 bp_tracepoint /* type_wanted */,
14648 0 /* Ignore count */,
14649 pending_break_support,
14650 ops,
14651 from_tty,
14652 1 /* enabled */,
14653 0 /* internal */, 0);
14654 }
14655
14656 static void
14657 ftrace_command (const char *arg, int from_tty)
14658 {
14659 event_location_up location = string_to_event_location (&arg,
14660 current_language);
14661 create_breakpoint (get_current_arch (),
14662 location.get (),
14663 NULL, 0, arg, false, 1 /* parse arg */,
14664 0 /* tempflag */,
14665 bp_fast_tracepoint /* type_wanted */,
14666 0 /* Ignore count */,
14667 pending_break_support,
14668 &tracepoint_breakpoint_ops,
14669 from_tty,
14670 1 /* enabled */,
14671 0 /* internal */, 0);
14672 }
14673
14674 /* strace command implementation. Creates a static tracepoint. */
14675
14676 static void
14677 strace_command (const char *arg, int from_tty)
14678 {
14679 struct breakpoint_ops *ops;
14680 event_location_up location;
14681
14682 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14683 or with a normal static tracepoint. */
14684 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14685 {
14686 ops = &strace_marker_breakpoint_ops;
14687 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
14688 }
14689 else
14690 {
14691 ops = &tracepoint_breakpoint_ops;
14692 location = string_to_event_location (&arg, current_language);
14693 }
14694
14695 create_breakpoint (get_current_arch (),
14696 location.get (),
14697 NULL, 0, arg, false, 1 /* parse arg */,
14698 0 /* tempflag */,
14699 bp_static_tracepoint /* type_wanted */,
14700 0 /* Ignore count */,
14701 pending_break_support,
14702 ops,
14703 from_tty,
14704 1 /* enabled */,
14705 0 /* internal */, 0);
14706 }
14707
14708 /* Set up a fake reader function that gets command lines from a linked
14709 list that was acquired during tracepoint uploading. */
14710
14711 static struct uploaded_tp *this_utp;
14712 static int next_cmd;
14713
14714 static char *
14715 read_uploaded_action (void)
14716 {
14717 char *rslt = nullptr;
14718
14719 if (next_cmd < this_utp->cmd_strings.size ())
14720 {
14721 rslt = this_utp->cmd_strings[next_cmd].get ();
14722 next_cmd++;
14723 }
14724
14725 return rslt;
14726 }
14727
14728 /* Given information about a tracepoint as recorded on a target (which
14729 can be either a live system or a trace file), attempt to create an
14730 equivalent GDB tracepoint. This is not a reliable process, since
14731 the target does not necessarily have all the information used when
14732 the tracepoint was originally defined. */
14733
14734 struct tracepoint *
14735 create_tracepoint_from_upload (struct uploaded_tp *utp)
14736 {
14737 const char *addr_str;
14738 char small_buf[100];
14739 struct tracepoint *tp;
14740
14741 if (utp->at_string)
14742 addr_str = utp->at_string.get ();
14743 else
14744 {
14745 /* In the absence of a source location, fall back to raw
14746 address. Since there is no way to confirm that the address
14747 means the same thing as when the trace was started, warn the
14748 user. */
14749 warning (_("Uploaded tracepoint %d has no "
14750 "source location, using raw address"),
14751 utp->number);
14752 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14753 addr_str = small_buf;
14754 }
14755
14756 /* There's not much we can do with a sequence of bytecodes. */
14757 if (utp->cond && !utp->cond_string)
14758 warning (_("Uploaded tracepoint %d condition "
14759 "has no source form, ignoring it"),
14760 utp->number);
14761
14762 event_location_up location = string_to_event_location (&addr_str,
14763 current_language);
14764 if (!create_breakpoint (get_current_arch (),
14765 location.get (),
14766 utp->cond_string.get (), -1, addr_str,
14767 false /* force_condition */,
14768 0 /* parse cond/thread */,
14769 0 /* tempflag */,
14770 utp->type /* type_wanted */,
14771 0 /* Ignore count */,
14772 pending_break_support,
14773 &tracepoint_breakpoint_ops,
14774 0 /* from_tty */,
14775 utp->enabled /* enabled */,
14776 0 /* internal */,
14777 CREATE_BREAKPOINT_FLAGS_INSERTED))
14778 return NULL;
14779
14780 /* Get the tracepoint we just created. */
14781 tp = get_tracepoint (tracepoint_count);
14782 gdb_assert (tp != NULL);
14783
14784 if (utp->pass > 0)
14785 {
14786 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14787 tp->number);
14788
14789 trace_pass_command (small_buf, 0);
14790 }
14791
14792 /* If we have uploaded versions of the original commands, set up a
14793 special-purpose "reader" function and call the usual command line
14794 reader, then pass the result to the breakpoint command-setting
14795 function. */
14796 if (!utp->cmd_strings.empty ())
14797 {
14798 counted_command_line cmd_list;
14799
14800 this_utp = utp;
14801 next_cmd = 0;
14802
14803 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
14804
14805 breakpoint_set_commands (tp, std::move (cmd_list));
14806 }
14807 else if (!utp->actions.empty ()
14808 || !utp->step_actions.empty ())
14809 warning (_("Uploaded tracepoint %d actions "
14810 "have no source form, ignoring them"),
14811 utp->number);
14812
14813 /* Copy any status information that might be available. */
14814 tp->hit_count = utp->hit_count;
14815 tp->traceframe_usage = utp->traceframe_usage;
14816
14817 return tp;
14818 }
14819
14820 /* Print information on tracepoint number TPNUM_EXP, or all if
14821 omitted. */
14822
14823 static void
14824 info_tracepoints_command (const char *args, int from_tty)
14825 {
14826 struct ui_out *uiout = current_uiout;
14827 int num_printed;
14828
14829 num_printed = breakpoint_1 (args, false, is_tracepoint);
14830
14831 if (num_printed == 0)
14832 {
14833 if (args == NULL || *args == '\0')
14834 uiout->message ("No tracepoints.\n");
14835 else
14836 uiout->message ("No tracepoint matching '%s'.\n", args);
14837 }
14838
14839 default_collect_info ();
14840 }
14841
14842 /* The 'enable trace' command enables tracepoints.
14843 Not supported by all targets. */
14844 static void
14845 enable_trace_command (const char *args, int from_tty)
14846 {
14847 enable_command (args, from_tty);
14848 }
14849
14850 /* The 'disable trace' command disables tracepoints.
14851 Not supported by all targets. */
14852 static void
14853 disable_trace_command (const char *args, int from_tty)
14854 {
14855 disable_command (args, from_tty);
14856 }
14857
14858 /* Remove a tracepoint (or all if no argument). */
14859 static void
14860 delete_trace_command (const char *arg, int from_tty)
14861 {
14862 dont_repeat ();
14863
14864 if (arg == 0)
14865 {
14866 int breaks_to_delete = 0;
14867
14868 /* Delete all breakpoints if no argument.
14869 Do not delete internal or call-dummy breakpoints, these
14870 have to be deleted with an explicit breakpoint number
14871 argument. */
14872 for (breakpoint *tp : all_tracepoints ())
14873 if (is_tracepoint (tp) && user_breakpoint_p (tp))
14874 {
14875 breaks_to_delete = 1;
14876 break;
14877 }
14878
14879 /* Ask user only if there are some breakpoints to delete. */
14880 if (!from_tty
14881 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14882 {
14883 for (breakpoint *b : all_breakpoints_safe ())
14884 if (is_tracepoint (b) && user_breakpoint_p (b))
14885 delete_breakpoint (b);
14886 }
14887 }
14888 else
14889 map_breakpoint_numbers
14890 (arg, [&] (breakpoint *br)
14891 {
14892 iterate_over_related_breakpoints (br, delete_breakpoint);
14893 });
14894 }
14895
14896 /* Helper function for trace_pass_command. */
14897
14898 static void
14899 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14900 {
14901 tp->pass_count = count;
14902 gdb::observers::breakpoint_modified.notify (tp);
14903 if (from_tty)
14904 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
14905 tp->number, count);
14906 }
14907
14908 /* Set passcount for tracepoint.
14909
14910 First command argument is passcount, second is tracepoint number.
14911 If tracepoint number omitted, apply to most recently defined.
14912 Also accepts special argument "all". */
14913
14914 static void
14915 trace_pass_command (const char *args, int from_tty)
14916 {
14917 struct tracepoint *t1;
14918 ULONGEST count;
14919
14920 if (args == 0 || *args == 0)
14921 error (_("passcount command requires an "
14922 "argument (count + optional TP num)"));
14923
14924 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
14925
14926 args = skip_spaces (args);
14927 if (*args && strncasecmp (args, "all", 3) == 0)
14928 {
14929 args += 3; /* Skip special argument "all". */
14930 if (*args)
14931 error (_("Junk at end of arguments."));
14932
14933 for (breakpoint *b : all_tracepoints ())
14934 {
14935 t1 = (struct tracepoint *) b;
14936 trace_pass_set_count (t1, count, from_tty);
14937 }
14938 }
14939 else if (*args == '\0')
14940 {
14941 t1 = get_tracepoint_by_number (&args, NULL);
14942 if (t1)
14943 trace_pass_set_count (t1, count, from_tty);
14944 }
14945 else
14946 {
14947 number_or_range_parser parser (args);
14948 while (!parser.finished ())
14949 {
14950 t1 = get_tracepoint_by_number (&args, &parser);
14951 if (t1)
14952 trace_pass_set_count (t1, count, from_tty);
14953 }
14954 }
14955 }
14956
14957 struct tracepoint *
14958 get_tracepoint (int num)
14959 {
14960 for (breakpoint *t : all_tracepoints ())
14961 if (t->number == num)
14962 return (struct tracepoint *) t;
14963
14964 return NULL;
14965 }
14966
14967 /* Find the tracepoint with the given target-side number (which may be
14968 different from the tracepoint number after disconnecting and
14969 reconnecting). */
14970
14971 struct tracepoint *
14972 get_tracepoint_by_number_on_target (int num)
14973 {
14974 for (breakpoint *b : all_tracepoints ())
14975 {
14976 struct tracepoint *t = (struct tracepoint *) b;
14977
14978 if (t->number_on_target == num)
14979 return t;
14980 }
14981
14982 return NULL;
14983 }
14984
14985 /* Utility: parse a tracepoint number and look it up in the list.
14986 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
14987 If the argument is missing, the most recent tracepoint
14988 (tracepoint_count) is returned. */
14989
14990 struct tracepoint *
14991 get_tracepoint_by_number (const char **arg,
14992 number_or_range_parser *parser)
14993 {
14994 int tpnum;
14995 const char *instring = arg == NULL ? NULL : *arg;
14996
14997 if (parser != NULL)
14998 {
14999 gdb_assert (!parser->finished ());
15000 tpnum = parser->get_number ();
15001 }
15002 else if (arg == NULL || *arg == NULL || ! **arg)
15003 tpnum = tracepoint_count;
15004 else
15005 tpnum = get_number (arg);
15006
15007 if (tpnum <= 0)
15008 {
15009 if (instring && *instring)
15010 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15011 instring);
15012 else
15013 printf_filtered (_("No previous tracepoint\n"));
15014 return NULL;
15015 }
15016
15017 for (breakpoint *t : all_tracepoints ())
15018 if (t->number == tpnum)
15019 {
15020 return (struct tracepoint *) t;
15021 }
15022
15023 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15024 return NULL;
15025 }
15026
15027 void
15028 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15029 {
15030 if (b->thread != -1)
15031 fprintf_unfiltered (fp, " thread %d", b->thread);
15032
15033 if (b->task != 0)
15034 fprintf_unfiltered (fp, " task %d", b->task);
15035
15036 fprintf_unfiltered (fp, "\n");
15037 }
15038
15039 /* Save information on user settable breakpoints (watchpoints, etc) to
15040 a new script file named FILENAME. If FILTER is non-NULL, call it
15041 on each breakpoint and only include the ones for which it returns
15042 true. */
15043
15044 static void
15045 save_breakpoints (const char *filename, int from_tty,
15046 bool (*filter) (const struct breakpoint *))
15047 {
15048 int any = 0;
15049 int extra_trace_bits = 0;
15050
15051 if (filename == 0 || *filename == 0)
15052 error (_("Argument required (file name in which to save)"));
15053
15054 /* See if we have anything to save. */
15055 for (breakpoint *tp : all_breakpoints ())
15056 {
15057 /* Skip internal and momentary breakpoints. */
15058 if (!user_breakpoint_p (tp))
15059 continue;
15060
15061 /* If we have a filter, only save the breakpoints it accepts. */
15062 if (filter && !filter (tp))
15063 continue;
15064
15065 any = 1;
15066
15067 if (is_tracepoint (tp))
15068 {
15069 extra_trace_bits = 1;
15070
15071 /* We can stop searching. */
15072 break;
15073 }
15074 }
15075
15076 if (!any)
15077 {
15078 warning (_("Nothing to save."));
15079 return;
15080 }
15081
15082 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
15083
15084 stdio_file fp;
15085
15086 if (!fp.open (expanded_filename.get (), "w"))
15087 error (_("Unable to open file '%s' for saving (%s)"),
15088 expanded_filename.get (), safe_strerror (errno));
15089
15090 if (extra_trace_bits)
15091 save_trace_state_variables (&fp);
15092
15093 for (breakpoint *tp : all_breakpoints ())
15094 {
15095 /* Skip internal and momentary breakpoints. */
15096 if (!user_breakpoint_p (tp))
15097 continue;
15098
15099 /* If we have a filter, only save the breakpoints it accepts. */
15100 if (filter && !filter (tp))
15101 continue;
15102
15103 tp->ops->print_recreate (tp, &fp);
15104
15105 /* Note, we can't rely on tp->number for anything, as we can't
15106 assume the recreated breakpoint numbers will match. Use $bpnum
15107 instead. */
15108
15109 if (tp->cond_string)
15110 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15111
15112 if (tp->ignore_count)
15113 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15114
15115 if (tp->type != bp_dprintf && tp->commands)
15116 {
15117 fp.puts (" commands\n");
15118
15119 current_uiout->redirect (&fp);
15120 try
15121 {
15122 print_command_lines (current_uiout, tp->commands.get (), 2);
15123 }
15124 catch (const gdb_exception &ex)
15125 {
15126 current_uiout->redirect (NULL);
15127 throw;
15128 }
15129
15130 current_uiout->redirect (NULL);
15131 fp.puts (" end\n");
15132 }
15133
15134 if (tp->enable_state == bp_disabled)
15135 fp.puts ("disable $bpnum\n");
15136
15137 /* If this is a multi-location breakpoint, check if the locations
15138 should be individually disabled. Watchpoint locations are
15139 special, and not user visible. */
15140 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15141 {
15142 int n = 1;
15143
15144 for (bp_location *loc : tp->locations ())
15145 {
15146 if (!loc->enabled)
15147 fp.printf ("disable $bpnum.%d\n", n);
15148
15149 n++;
15150 }
15151 }
15152 }
15153
15154 if (extra_trace_bits && *default_collect)
15155 fp.printf ("set default-collect %s\n", default_collect);
15156
15157 if (from_tty)
15158 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
15159 }
15160
15161 /* The `save breakpoints' command. */
15162
15163 static void
15164 save_breakpoints_command (const char *args, int from_tty)
15165 {
15166 save_breakpoints (args, from_tty, NULL);
15167 }
15168
15169 /* The `save tracepoints' command. */
15170
15171 static void
15172 save_tracepoints_command (const char *args, int from_tty)
15173 {
15174 save_breakpoints (args, from_tty, is_tracepoint);
15175 }
15176
15177 \f
15178 /* This help string is used to consolidate all the help string for specifying
15179 locations used by several commands. */
15180
15181 #define LOCATION_HELP_STRING \
15182 "Linespecs are colon-separated lists of location parameters, such as\n\
15183 source filename, function name, label name, and line number.\n\
15184 Example: To specify the start of a label named \"the_top\" in the\n\
15185 function \"fact\" in the file \"factorial.c\", use\n\
15186 \"factorial.c:fact:the_top\".\n\
15187 \n\
15188 Address locations begin with \"*\" and specify an exact address in the\n\
15189 program. Example: To specify the fourth byte past the start function\n\
15190 \"main\", use \"*main + 4\".\n\
15191 \n\
15192 Explicit locations are similar to linespecs but use an option/argument\n\
15193 syntax to specify location parameters.\n\
15194 Example: To specify the start of the label named \"the_top\" in the\n\
15195 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15196 -function fact -label the_top\".\n\
15197 \n\
15198 By default, a specified function is matched against the program's\n\
15199 functions in all scopes. For C++, this means in all namespaces and\n\
15200 classes. For Ada, this means in all packages. E.g., in C++,\n\
15201 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15202 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
15203 specified name as a complete fully-qualified name instead."
15204
15205 /* This help string is used for the break, hbreak, tbreak and thbreak
15206 commands. It is defined as a macro to prevent duplication.
15207 COMMAND should be a string constant containing the name of the
15208 command. */
15209
15210 #define BREAK_ARGS_HELP(command) \
15211 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
15212 \t[-force-condition] [if CONDITION]\n\
15213 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15214 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15215 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15216 `-probe-dtrace' (for a DTrace probe).\n\
15217 LOCATION may be a linespec, address, or explicit location as described\n\
15218 below.\n\
15219 \n\
15220 With no LOCATION, uses current execution address of the selected\n\
15221 stack frame. This is useful for breaking on return to a stack frame.\n\
15222 \n\
15223 THREADNUM is the number from \"info threads\".\n\
15224 CONDITION is a boolean expression.\n\
15225 \n\
15226 With the \"-force-condition\" flag, the condition is defined even when\n\
15227 it is invalid for all current locations.\n\
15228 \n" LOCATION_HELP_STRING "\n\n\
15229 Multiple breakpoints at one place are permitted, and useful if their\n\
15230 conditions are different.\n\
15231 \n\
15232 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15233
15234 /* List of subcommands for "catch". */
15235 static struct cmd_list_element *catch_cmdlist;
15236
15237 /* List of subcommands for "tcatch". */
15238 static struct cmd_list_element *tcatch_cmdlist;
15239
15240 void
15241 add_catch_command (const char *name, const char *docstring,
15242 cmd_const_sfunc_ftype *sfunc,
15243 completer_ftype *completer,
15244 void *user_data_catch,
15245 void *user_data_tcatch)
15246 {
15247 struct cmd_list_element *command;
15248
15249 command = add_cmd (name, class_breakpoint, docstring,
15250 &catch_cmdlist);
15251 set_cmd_sfunc (command, sfunc);
15252 set_cmd_context (command, user_data_catch);
15253 set_cmd_completer (command, completer);
15254
15255 command = add_cmd (name, class_breakpoint, docstring,
15256 &tcatch_cmdlist);
15257 set_cmd_sfunc (command, sfunc);
15258 set_cmd_context (command, user_data_tcatch);
15259 set_cmd_completer (command, completer);
15260 }
15261
15262 struct breakpoint *
15263 iterate_over_breakpoints (gdb::function_view<bool (breakpoint *)> callback)
15264 {
15265 for (breakpoint *b : all_breakpoints_safe ())
15266 if (callback (b))
15267 return b;
15268
15269 return NULL;
15270 }
15271
15272 /* Zero if any of the breakpoint's locations could be a location where
15273 functions have been inlined, nonzero otherwise. */
15274
15275 static int
15276 is_non_inline_function (struct breakpoint *b)
15277 {
15278 /* The shared library event breakpoint is set on the address of a
15279 non-inline function. */
15280 if (b->type == bp_shlib_event)
15281 return 1;
15282
15283 return 0;
15284 }
15285
15286 /* Nonzero if the specified PC cannot be a location where functions
15287 have been inlined. */
15288
15289 int
15290 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
15291 const struct target_waitstatus *ws)
15292 {
15293 for (breakpoint *b : all_breakpoints ())
15294 {
15295 if (!is_non_inline_function (b))
15296 continue;
15297
15298 for (bp_location *bl : b->locations ())
15299 {
15300 if (!bl->shlib_disabled
15301 && bpstat_check_location (bl, aspace, pc, ws))
15302 return 1;
15303 }
15304 }
15305
15306 return 0;
15307 }
15308
15309 /* Remove any references to OBJFILE which is going to be freed. */
15310
15311 void
15312 breakpoint_free_objfile (struct objfile *objfile)
15313 {
15314 struct bp_location **locp, *loc;
15315
15316 ALL_BP_LOCATIONS (loc, locp)
15317 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15318 loc->symtab = NULL;
15319 }
15320
15321 void
15322 initialize_breakpoint_ops (void)
15323 {
15324 static int initialized = 0;
15325
15326 struct breakpoint_ops *ops;
15327
15328 if (initialized)
15329 return;
15330 initialized = 1;
15331
15332 /* The breakpoint_ops structure to be inherit by all kinds of
15333 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15334 internal and momentary breakpoints, etc.). */
15335 ops = &bkpt_base_breakpoint_ops;
15336 *ops = base_breakpoint_ops;
15337 ops->re_set = bkpt_re_set;
15338 ops->insert_location = bkpt_insert_location;
15339 ops->remove_location = bkpt_remove_location;
15340 ops->breakpoint_hit = bkpt_breakpoint_hit;
15341 ops->create_sals_from_location = bkpt_create_sals_from_location;
15342 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15343 ops->decode_location = bkpt_decode_location;
15344
15345 /* The breakpoint_ops structure to be used in regular breakpoints. */
15346 ops = &bkpt_breakpoint_ops;
15347 *ops = bkpt_base_breakpoint_ops;
15348 ops->re_set = bkpt_re_set;
15349 ops->resources_needed = bkpt_resources_needed;
15350 ops->print_it = bkpt_print_it;
15351 ops->print_mention = bkpt_print_mention;
15352 ops->print_recreate = bkpt_print_recreate;
15353
15354 /* Ranged breakpoints. */
15355 ops = &ranged_breakpoint_ops;
15356 *ops = bkpt_breakpoint_ops;
15357 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15358 ops->resources_needed = resources_needed_ranged_breakpoint;
15359 ops->print_it = print_it_ranged_breakpoint;
15360 ops->print_one = print_one_ranged_breakpoint;
15361 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15362 ops->print_mention = print_mention_ranged_breakpoint;
15363 ops->print_recreate = print_recreate_ranged_breakpoint;
15364
15365 /* Internal breakpoints. */
15366 ops = &internal_breakpoint_ops;
15367 *ops = bkpt_base_breakpoint_ops;
15368 ops->re_set = internal_bkpt_re_set;
15369 ops->check_status = internal_bkpt_check_status;
15370 ops->print_it = internal_bkpt_print_it;
15371 ops->print_mention = internal_bkpt_print_mention;
15372
15373 /* Momentary breakpoints. */
15374 ops = &momentary_breakpoint_ops;
15375 *ops = bkpt_base_breakpoint_ops;
15376 ops->re_set = momentary_bkpt_re_set;
15377 ops->check_status = momentary_bkpt_check_status;
15378 ops->print_it = momentary_bkpt_print_it;
15379 ops->print_mention = momentary_bkpt_print_mention;
15380
15381 /* Probe breakpoints. */
15382 ops = &bkpt_probe_breakpoint_ops;
15383 *ops = bkpt_breakpoint_ops;
15384 ops->insert_location = bkpt_probe_insert_location;
15385 ops->remove_location = bkpt_probe_remove_location;
15386 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15387 ops->decode_location = bkpt_probe_decode_location;
15388
15389 /* Watchpoints. */
15390 ops = &watchpoint_breakpoint_ops;
15391 *ops = base_breakpoint_ops;
15392 ops->re_set = re_set_watchpoint;
15393 ops->insert_location = insert_watchpoint;
15394 ops->remove_location = remove_watchpoint;
15395 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15396 ops->check_status = check_status_watchpoint;
15397 ops->resources_needed = resources_needed_watchpoint;
15398 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15399 ops->print_it = print_it_watchpoint;
15400 ops->print_mention = print_mention_watchpoint;
15401 ops->print_recreate = print_recreate_watchpoint;
15402 ops->explains_signal = explains_signal_watchpoint;
15403
15404 /* Masked watchpoints. */
15405 ops = &masked_watchpoint_breakpoint_ops;
15406 *ops = watchpoint_breakpoint_ops;
15407 ops->insert_location = insert_masked_watchpoint;
15408 ops->remove_location = remove_masked_watchpoint;
15409 ops->resources_needed = resources_needed_masked_watchpoint;
15410 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15411 ops->print_it = print_it_masked_watchpoint;
15412 ops->print_one_detail = print_one_detail_masked_watchpoint;
15413 ops->print_mention = print_mention_masked_watchpoint;
15414 ops->print_recreate = print_recreate_masked_watchpoint;
15415
15416 /* Tracepoints. */
15417 ops = &tracepoint_breakpoint_ops;
15418 *ops = base_breakpoint_ops;
15419 ops->re_set = tracepoint_re_set;
15420 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15421 ops->print_one_detail = tracepoint_print_one_detail;
15422 ops->print_mention = tracepoint_print_mention;
15423 ops->print_recreate = tracepoint_print_recreate;
15424 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15425 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15426 ops->decode_location = tracepoint_decode_location;
15427
15428 /* Probe tracepoints. */
15429 ops = &tracepoint_probe_breakpoint_ops;
15430 *ops = tracepoint_breakpoint_ops;
15431 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15432 ops->decode_location = tracepoint_probe_decode_location;
15433
15434 /* Static tracepoints with marker (`-m'). */
15435 ops = &strace_marker_breakpoint_ops;
15436 *ops = tracepoint_breakpoint_ops;
15437 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15438 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15439 ops->decode_location = strace_marker_decode_location;
15440
15441 /* Fork catchpoints. */
15442 ops = &catch_fork_breakpoint_ops;
15443 *ops = base_breakpoint_ops;
15444 ops->insert_location = insert_catch_fork;
15445 ops->remove_location = remove_catch_fork;
15446 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15447 ops->print_it = print_it_catch_fork;
15448 ops->print_one = print_one_catch_fork;
15449 ops->print_mention = print_mention_catch_fork;
15450 ops->print_recreate = print_recreate_catch_fork;
15451
15452 /* Vfork catchpoints. */
15453 ops = &catch_vfork_breakpoint_ops;
15454 *ops = base_breakpoint_ops;
15455 ops->insert_location = insert_catch_vfork;
15456 ops->remove_location = remove_catch_vfork;
15457 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15458 ops->print_it = print_it_catch_vfork;
15459 ops->print_one = print_one_catch_vfork;
15460 ops->print_mention = print_mention_catch_vfork;
15461 ops->print_recreate = print_recreate_catch_vfork;
15462
15463 /* Exec catchpoints. */
15464 ops = &catch_exec_breakpoint_ops;
15465 *ops = base_breakpoint_ops;
15466 ops->insert_location = insert_catch_exec;
15467 ops->remove_location = remove_catch_exec;
15468 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15469 ops->print_it = print_it_catch_exec;
15470 ops->print_one = print_one_catch_exec;
15471 ops->print_mention = print_mention_catch_exec;
15472 ops->print_recreate = print_recreate_catch_exec;
15473
15474 /* Solib-related catchpoints. */
15475 ops = &catch_solib_breakpoint_ops;
15476 *ops = base_breakpoint_ops;
15477 ops->insert_location = insert_catch_solib;
15478 ops->remove_location = remove_catch_solib;
15479 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15480 ops->check_status = check_status_catch_solib;
15481 ops->print_it = print_it_catch_solib;
15482 ops->print_one = print_one_catch_solib;
15483 ops->print_mention = print_mention_catch_solib;
15484 ops->print_recreate = print_recreate_catch_solib;
15485
15486 ops = &dprintf_breakpoint_ops;
15487 *ops = bkpt_base_breakpoint_ops;
15488 ops->re_set = dprintf_re_set;
15489 ops->resources_needed = bkpt_resources_needed;
15490 ops->print_it = bkpt_print_it;
15491 ops->print_mention = bkpt_print_mention;
15492 ops->print_recreate = dprintf_print_recreate;
15493 ops->after_condition_true = dprintf_after_condition_true;
15494 ops->breakpoint_hit = dprintf_breakpoint_hit;
15495 }
15496
15497 /* Chain containing all defined "enable breakpoint" subcommands. */
15498
15499 static struct cmd_list_element *enablebreaklist = NULL;
15500
15501 /* See breakpoint.h. */
15502
15503 cmd_list_element *commands_cmd_element = nullptr;
15504
15505 void _initialize_breakpoint ();
15506 void
15507 _initialize_breakpoint ()
15508 {
15509 struct cmd_list_element *c;
15510
15511 initialize_breakpoint_ops ();
15512
15513 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
15514 "breakpoint");
15515 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
15516 "breakpoint");
15517 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
15518 "breakpoint");
15519
15520 breakpoint_chain = 0;
15521 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15522 before a breakpoint is set. */
15523 breakpoint_count = 0;
15524
15525 tracepoint_count = 0;
15526
15527 add_com ("ignore", class_breakpoint, ignore_command, _("\
15528 Set ignore-count of breakpoint number N to COUNT.\n\
15529 Usage is `ignore N COUNT'."));
15530
15531 commands_cmd_element = add_com ("commands", class_breakpoint,
15532 commands_command, _("\
15533 Set commands to be executed when the given breakpoints are hit.\n\
15534 Give a space-separated breakpoint list as argument after \"commands\".\n\
15535 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15536 (e.g. `5-7').\n\
15537 With no argument, the targeted breakpoint is the last one set.\n\
15538 The commands themselves follow starting on the next line.\n\
15539 Type a line containing \"end\" to indicate the end of them.\n\
15540 Give \"silent\" as the first line to make the breakpoint silent;\n\
15541 then no output is printed when it is hit, except what the commands print."));
15542
15543 const auto cc_opts = make_condition_command_options_def_group (nullptr);
15544 static std::string condition_command_help
15545 = gdb::option::build_help (_("\
15546 Specify breakpoint number N to break only if COND is true.\n\
15547 Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
15548 is an expression to be evaluated whenever breakpoint N is reached.\n\
15549 \n\
15550 Options:\n\
15551 %OPTIONS%"), cc_opts);
15552
15553 c = add_com ("condition", class_breakpoint, condition_command,
15554 condition_command_help.c_str ());
15555 set_cmd_completer_handle_brkchars (c, condition_completer);
15556
15557 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15558 Set a temporary breakpoint.\n\
15559 Like \"break\" except the breakpoint is only temporary,\n\
15560 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15561 by using \"enable delete\" on the breakpoint number.\n\
15562 \n"
15563 BREAK_ARGS_HELP ("tbreak")));
15564 set_cmd_completer (c, location_completer);
15565
15566 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15567 Set a hardware assisted breakpoint.\n\
15568 Like \"break\" except the breakpoint requires hardware support,\n\
15569 some target hardware may not have this support.\n\
15570 \n"
15571 BREAK_ARGS_HELP ("hbreak")));
15572 set_cmd_completer (c, location_completer);
15573
15574 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15575 Set a temporary hardware assisted breakpoint.\n\
15576 Like \"hbreak\" except the breakpoint is only temporary,\n\
15577 so it will be deleted when hit.\n\
15578 \n"
15579 BREAK_ARGS_HELP ("thbreak")));
15580 set_cmd_completer (c, location_completer);
15581
15582 cmd_list_element *enable_cmd
15583 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15584 Enable all or some breakpoints.\n\
15585 Usage: enable [BREAKPOINTNUM]...\n\
15586 Give breakpoint numbers (separated by spaces) as arguments.\n\
15587 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15588 This is used to cancel the effect of the \"disable\" command.\n\
15589 With a subcommand you can enable temporarily."),
15590 &enablelist, 1, &cmdlist);
15591
15592 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
15593
15594 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15595 Enable all or some breakpoints.\n\
15596 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
15597 Give breakpoint numbers (separated by spaces) as arguments.\n\
15598 This is used to cancel the effect of the \"disable\" command.\n\
15599 May be abbreviated to simply \"enable\"."),
15600 &enablebreaklist, 1, &enablelist);
15601
15602 add_cmd ("once", no_class, enable_once_command, _("\
15603 Enable some breakpoints for one hit.\n\
15604 Usage: enable breakpoints once BREAKPOINTNUM...\n\
15605 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15606 &enablebreaklist);
15607
15608 add_cmd ("delete", no_class, enable_delete_command, _("\
15609 Enable some breakpoints and delete when hit.\n\
15610 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
15611 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15612 &enablebreaklist);
15613
15614 add_cmd ("count", no_class, enable_count_command, _("\
15615 Enable some breakpoints for COUNT hits.\n\
15616 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
15617 If a breakpoint is hit while enabled in this fashion,\n\
15618 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15619 &enablebreaklist);
15620
15621 add_cmd ("delete", no_class, enable_delete_command, _("\
15622 Enable some breakpoints and delete when hit.\n\
15623 Usage: enable delete BREAKPOINTNUM...\n\
15624 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15625 &enablelist);
15626
15627 add_cmd ("once", no_class, enable_once_command, _("\
15628 Enable some breakpoints for one hit.\n\
15629 Usage: enable once BREAKPOINTNUM...\n\
15630 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15631 &enablelist);
15632
15633 add_cmd ("count", no_class, enable_count_command, _("\
15634 Enable some breakpoints for COUNT hits.\n\
15635 Usage: enable count COUNT BREAKPOINTNUM...\n\
15636 If a breakpoint is hit while enabled in this fashion,\n\
15637 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15638 &enablelist);
15639
15640 cmd_list_element *disable_cmd
15641 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15642 Disable all or some breakpoints.\n\
15643 Usage: disable [BREAKPOINTNUM]...\n\
15644 Arguments are breakpoint numbers with spaces in between.\n\
15645 To disable all breakpoints, give no argument.\n\
15646 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15647 &disablelist, 1, &cmdlist);
15648 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
15649 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
15650
15651 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
15652 Disable all or some breakpoints.\n\
15653 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
15654 Arguments are breakpoint numbers with spaces in between.\n\
15655 To disable all breakpoints, give no argument.\n\
15656 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15657 This command may be abbreviated \"disable\"."),
15658 &disablelist);
15659
15660 cmd_list_element *delete_cmd
15661 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15662 Delete all or some breakpoints.\n\
15663 Usage: delete [BREAKPOINTNUM]...\n\
15664 Arguments are breakpoint numbers with spaces in between.\n\
15665 To delete all breakpoints, give no argument.\n\
15666 \n\
15667 Also a prefix command for deletion of other GDB objects."),
15668 &deletelist, 1, &cmdlist);
15669 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
15670 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
15671
15672 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
15673 Delete all or some breakpoints or auto-display expressions.\n\
15674 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
15675 Arguments are breakpoint numbers with spaces in between.\n\
15676 To delete all breakpoints, give no argument.\n\
15677 This command may be abbreviated \"delete\"."),
15678 &deletelist);
15679
15680 cmd_list_element *clear_cmd
15681 = add_com ("clear", class_breakpoint, clear_command, _("\
15682 Clear breakpoint at specified location.\n\
15683 Argument may be a linespec, explicit, or address location as described below.\n\
15684 \n\
15685 With no argument, clears all breakpoints in the line that the selected frame\n\
15686 is executing in.\n"
15687 "\n" LOCATION_HELP_STRING "\n\n\
15688 See also the \"delete\" command which clears breakpoints by number."));
15689 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
15690
15691 cmd_list_element *break_cmd
15692 = add_com ("break", class_breakpoint, break_command, _("\
15693 Set breakpoint at specified location.\n"
15694 BREAK_ARGS_HELP ("break")));
15695 set_cmd_completer (break_cmd, location_completer);
15696
15697 add_com_alias ("b", break_cmd, class_run, 1);
15698 add_com_alias ("br", break_cmd, class_run, 1);
15699 add_com_alias ("bre", break_cmd, class_run, 1);
15700 add_com_alias ("brea", break_cmd, class_run, 1);
15701
15702 if (dbx_commands)
15703 {
15704 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15705 Break in function/address or break at a line in the current file."),
15706 &stoplist, 1, &cmdlist);
15707 add_cmd ("in", class_breakpoint, stopin_command,
15708 _("Break in function or address."), &stoplist);
15709 add_cmd ("at", class_breakpoint, stopat_command,
15710 _("Break at a line in the current file."), &stoplist);
15711 add_com ("status", class_info, info_breakpoints_command, _("\
15712 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15713 The \"Type\" column indicates one of:\n\
15714 \tbreakpoint - normal breakpoint\n\
15715 \twatchpoint - watchpoint\n\
15716 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15717 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15718 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15719 address and file/line number respectively.\n\
15720 \n\
15721 Convenience variable \"$_\" and default examine address for \"x\"\n\
15722 are set to the address of the last breakpoint listed unless the command\n\
15723 is prefixed with \"server \".\n\n\
15724 Convenience variable \"$bpnum\" contains the number of the last\n\
15725 breakpoint set."));
15726 }
15727
15728 cmd_list_element *info_breakpoints_cmd
15729 = add_info ("breakpoints", info_breakpoints_command, _("\
15730 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15731 The \"Type\" column indicates one of:\n\
15732 \tbreakpoint - normal breakpoint\n\
15733 \twatchpoint - watchpoint\n\
15734 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15735 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15736 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15737 address and file/line number respectively.\n\
15738 \n\
15739 Convenience variable \"$_\" and default examine address for \"x\"\n\
15740 are set to the address of the last breakpoint listed unless the command\n\
15741 is prefixed with \"server \".\n\n\
15742 Convenience variable \"$bpnum\" contains the number of the last\n\
15743 breakpoint set."));
15744
15745 add_info_alias ("b", info_breakpoints_cmd, 1);
15746
15747 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15748 Status of all breakpoints, or breakpoint number NUMBER.\n\
15749 The \"Type\" column indicates one of:\n\
15750 \tbreakpoint - normal breakpoint\n\
15751 \twatchpoint - watchpoint\n\
15752 \tlongjmp - internal breakpoint used to step through longjmp()\n\
15753 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15754 \tuntil - internal breakpoint used by the \"until\" command\n\
15755 \tfinish - internal breakpoint used by the \"finish\" command\n\
15756 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15757 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15758 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15759 address and file/line number respectively.\n\
15760 \n\
15761 Convenience variable \"$_\" and default examine address for \"x\"\n\
15762 are set to the address of the last breakpoint listed unless the command\n\
15763 is prefixed with \"server \".\n\n\
15764 Convenience variable \"$bpnum\" contains the number of the last\n\
15765 breakpoint set."),
15766 &maintenanceinfolist);
15767
15768 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
15769 Set catchpoints to catch events."),
15770 &catch_cmdlist,
15771 0/*allow-unknown*/, &cmdlist);
15772
15773 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
15774 Set temporary catchpoints to catch events."),
15775 &tcatch_cmdlist,
15776 0/*allow-unknown*/, &cmdlist);
15777
15778 add_catch_command ("fork", _("Catch calls to fork."),
15779 catch_fork_command_1,
15780 NULL,
15781 (void *) (uintptr_t) catch_fork_permanent,
15782 (void *) (uintptr_t) catch_fork_temporary);
15783 add_catch_command ("vfork", _("Catch calls to vfork."),
15784 catch_fork_command_1,
15785 NULL,
15786 (void *) (uintptr_t) catch_vfork_permanent,
15787 (void *) (uintptr_t) catch_vfork_temporary);
15788 add_catch_command ("exec", _("Catch calls to exec."),
15789 catch_exec_command_1,
15790 NULL,
15791 CATCH_PERMANENT,
15792 CATCH_TEMPORARY);
15793 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15794 Usage: catch load [REGEX]\n\
15795 If REGEX is given, only stop for libraries matching the regular expression."),
15796 catch_load_command_1,
15797 NULL,
15798 CATCH_PERMANENT,
15799 CATCH_TEMPORARY);
15800 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15801 Usage: catch unload [REGEX]\n\
15802 If REGEX is given, only stop for libraries matching the regular expression."),
15803 catch_unload_command_1,
15804 NULL,
15805 CATCH_PERMANENT,
15806 CATCH_TEMPORARY);
15807
15808 const auto opts = make_watch_options_def_group (nullptr);
15809
15810 static const std::string watch_help = gdb::option::build_help (_("\
15811 Set a watchpoint for EXPRESSION.\n\
15812 Usage: watch [-location] EXPRESSION\n\
15813 \n\
15814 Options:\n\
15815 %OPTIONS%\n\
15816 \n\
15817 A watchpoint stops execution of your program whenever the value of\n\
15818 an expression changes."), opts);
15819 c = add_com ("watch", class_breakpoint, watch_command,
15820 watch_help.c_str ());
15821 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15822
15823 static const std::string rwatch_help = gdb::option::build_help (_("\
15824 Set a read watchpoint for EXPRESSION.\n\
15825 Usage: rwatch [-location] EXPRESSION\n\
15826 \n\
15827 Options:\n\
15828 %OPTIONS%\n\
15829 \n\
15830 A read watchpoint stops execution of your program whenever the value of\n\
15831 an expression is read."), opts);
15832 c = add_com ("rwatch", class_breakpoint, rwatch_command,
15833 rwatch_help.c_str ());
15834 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15835
15836 static const std::string awatch_help = gdb::option::build_help (_("\
15837 Set an access watchpoint for EXPRESSION.\n\
15838 Usage: awatch [-location] EXPRESSION\n\
15839 \n\
15840 Options:\n\
15841 %OPTIONS%\n\
15842 \n\
15843 An access watchpoint stops execution of your program whenever the value\n\
15844 of an expression is either read or written."), opts);
15845 c = add_com ("awatch", class_breakpoint, awatch_command,
15846 awatch_help.c_str ());
15847 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15848
15849 add_info ("watchpoints", info_watchpoints_command, _("\
15850 Status of specified watchpoints (all watchpoints if no argument)."));
15851
15852 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15853 respond to changes - contrary to the description. */
15854 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15855 &can_use_hw_watchpoints, _("\
15856 Set debugger's willingness to use watchpoint hardware."), _("\
15857 Show debugger's willingness to use watchpoint hardware."), _("\
15858 If zero, gdb will not use hardware for new watchpoints, even if\n\
15859 such is available. (However, any hardware watchpoints that were\n\
15860 created before setting this to nonzero, will continue to use watchpoint\n\
15861 hardware.)"),
15862 NULL,
15863 show_can_use_hw_watchpoints,
15864 &setlist, &showlist);
15865
15866 can_use_hw_watchpoints = 1;
15867
15868 /* Tracepoint manipulation commands. */
15869
15870 cmd_list_element *trace_cmd
15871 = add_com ("trace", class_breakpoint, trace_command, _("\
15872 Set a tracepoint at specified location.\n\
15873 \n"
15874 BREAK_ARGS_HELP ("trace") "\n\
15875 Do \"help tracepoints\" for info on other tracepoint commands."));
15876 set_cmd_completer (trace_cmd, location_completer);
15877
15878 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
15879 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
15880 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
15881 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
15882
15883 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
15884 Set a fast tracepoint at specified location.\n\
15885 \n"
15886 BREAK_ARGS_HELP ("ftrace") "\n\
15887 Do \"help tracepoints\" for info on other tracepoint commands."));
15888 set_cmd_completer (c, location_completer);
15889
15890 c = add_com ("strace", class_breakpoint, strace_command, _("\
15891 Set a static tracepoint at location or marker.\n\
15892 \n\
15893 strace [LOCATION] [if CONDITION]\n\
15894 LOCATION may be a linespec, explicit, or address location (described below) \n\
15895 or -m MARKER_ID.\n\n\
15896 If a marker id is specified, probe the marker with that name. With\n\
15897 no LOCATION, uses current execution address of the selected stack frame.\n\
15898 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15899 This collects arbitrary user data passed in the probe point call to the\n\
15900 tracing library. You can inspect it when analyzing the trace buffer,\n\
15901 by printing the $_sdata variable like any other convenience variable.\n\
15902 \n\
15903 CONDITION is a boolean expression.\n\
15904 \n" LOCATION_HELP_STRING "\n\n\
15905 Multiple tracepoints at one place are permitted, and useful if their\n\
15906 conditions are different.\n\
15907 \n\
15908 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15909 Do \"help tracepoints\" for info on other tracepoint commands."));
15910 set_cmd_completer (c, location_completer);
15911
15912 cmd_list_element *info_tracepoints_cmd
15913 = add_info ("tracepoints", info_tracepoints_command, _("\
15914 Status of specified tracepoints (all tracepoints if no argument).\n\
15915 Convenience variable \"$tpnum\" contains the number of the\n\
15916 last tracepoint set."));
15917
15918 add_info_alias ("tp", info_tracepoints_cmd, 1);
15919
15920 cmd_list_element *delete_tracepoints_cmd
15921 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15922 Delete specified tracepoints.\n\
15923 Arguments are tracepoint numbers, separated by spaces.\n\
15924 No argument means delete all tracepoints."),
15925 &deletelist);
15926 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
15927
15928 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15929 Disable specified tracepoints.\n\
15930 Arguments are tracepoint numbers, separated by spaces.\n\
15931 No argument means disable all tracepoints."),
15932 &disablelist);
15933 deprecate_cmd (c, "disable");
15934
15935 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15936 Enable specified tracepoints.\n\
15937 Arguments are tracepoint numbers, separated by spaces.\n\
15938 No argument means enable all tracepoints."),
15939 &enablelist);
15940 deprecate_cmd (c, "enable");
15941
15942 add_com ("passcount", class_trace, trace_pass_command, _("\
15943 Set the passcount for a tracepoint.\n\
15944 The trace will end when the tracepoint has been passed 'count' times.\n\
15945 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15946 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15947
15948 add_basic_prefix_cmd ("save", class_breakpoint,
15949 _("Save breakpoint definitions as a script."),
15950 &save_cmdlist,
15951 0/*allow-unknown*/, &cmdlist);
15952
15953 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15954 Save current breakpoint definitions as a script.\n\
15955 This includes all types of breakpoints (breakpoints, watchpoints,\n\
15956 catchpoints, tracepoints). Use the 'source' command in another debug\n\
15957 session to restore them."),
15958 &save_cmdlist);
15959 set_cmd_completer (c, filename_completer);
15960
15961 cmd_list_element *save_tracepoints_cmd
15962 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
15963 Save current tracepoint definitions as a script.\n\
15964 Use the 'source' command in another debug session to restore them."),
15965 &save_cmdlist);
15966 set_cmd_completer (save_tracepoints_cmd, filename_completer);
15967
15968 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
15969 deprecate_cmd (c, "save tracepoints");
15970
15971 add_basic_prefix_cmd ("breakpoint", class_maintenance, _("\
15972 Breakpoint specific settings.\n\
15973 Configure various breakpoint-specific variables such as\n\
15974 pending breakpoint behavior."),
15975 &breakpoint_set_cmdlist,
15976 0/*allow-unknown*/, &setlist);
15977 add_show_prefix_cmd ("breakpoint", class_maintenance, _("\
15978 Breakpoint specific settings.\n\
15979 Configure various breakpoint-specific variables such as\n\
15980 pending breakpoint behavior."),
15981 &breakpoint_show_cmdlist,
15982 0/*allow-unknown*/, &showlist);
15983
15984 add_setshow_auto_boolean_cmd ("pending", no_class,
15985 &pending_break_support, _("\
15986 Set debugger's behavior regarding pending breakpoints."), _("\
15987 Show debugger's behavior regarding pending breakpoints."), _("\
15988 If on, an unrecognized breakpoint location will cause gdb to create a\n\
15989 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15990 an error. If auto, an unrecognized breakpoint location results in a\n\
15991 user-query to see if a pending breakpoint should be created."),
15992 NULL,
15993 show_pending_break_support,
15994 &breakpoint_set_cmdlist,
15995 &breakpoint_show_cmdlist);
15996
15997 pending_break_support = AUTO_BOOLEAN_AUTO;
15998
15999 add_setshow_boolean_cmd ("auto-hw", no_class,
16000 &automatic_hardware_breakpoints, _("\
16001 Set automatic usage of hardware breakpoints."), _("\
16002 Show automatic usage of hardware breakpoints."), _("\
16003 If set, the debugger will automatically use hardware breakpoints for\n\
16004 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16005 a warning will be emitted for such breakpoints."),
16006 NULL,
16007 show_automatic_hardware_breakpoints,
16008 &breakpoint_set_cmdlist,
16009 &breakpoint_show_cmdlist);
16010
16011 add_setshow_boolean_cmd ("always-inserted", class_support,
16012 &always_inserted_mode, _("\
16013 Set mode for inserting breakpoints."), _("\
16014 Show mode for inserting breakpoints."), _("\
16015 When this mode is on, breakpoints are inserted immediately as soon as\n\
16016 they're created, kept inserted even when execution stops, and removed\n\
16017 only when the user deletes them. When this mode is off (the default),\n\
16018 breakpoints are inserted only when execution continues, and removed\n\
16019 when execution stops."),
16020 NULL,
16021 &show_always_inserted_mode,
16022 &breakpoint_set_cmdlist,
16023 &breakpoint_show_cmdlist);
16024
16025 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16026 condition_evaluation_enums,
16027 &condition_evaluation_mode_1, _("\
16028 Set mode of breakpoint condition evaluation."), _("\
16029 Show mode of breakpoint condition evaluation."), _("\
16030 When this is set to \"host\", breakpoint conditions will be\n\
16031 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16032 breakpoint conditions will be downloaded to the target (if the target\n\
16033 supports such feature) and conditions will be evaluated on the target's side.\n\
16034 If this is set to \"auto\" (default), this will be automatically set to\n\
16035 \"target\" if it supports condition evaluation, otherwise it will\n\
16036 be set to \"host\"."),
16037 &set_condition_evaluation_mode,
16038 &show_condition_evaluation_mode,
16039 &breakpoint_set_cmdlist,
16040 &breakpoint_show_cmdlist);
16041
16042 add_com ("break-range", class_breakpoint, break_range_command, _("\
16043 Set a breakpoint for an address range.\n\
16044 break-range START-LOCATION, END-LOCATION\n\
16045 where START-LOCATION and END-LOCATION can be one of the following:\n\
16046 LINENUM, for that line in the current file,\n\
16047 FILE:LINENUM, for that line in that file,\n\
16048 +OFFSET, for that number of lines after the current line\n\
16049 or the start of the range\n\
16050 FUNCTION, for the first line in that function,\n\
16051 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16052 *ADDRESS, for the instruction at that address.\n\
16053 \n\
16054 The breakpoint will stop execution of the inferior whenever it executes\n\
16055 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16056 range (including START-LOCATION and END-LOCATION)."));
16057
16058 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16059 Set a dynamic printf at specified location.\n\
16060 dprintf location,format string,arg1,arg2,...\n\
16061 location may be a linespec, explicit, or address location.\n"
16062 "\n" LOCATION_HELP_STRING));
16063 set_cmd_completer (c, location_completer);
16064
16065 add_setshow_enum_cmd ("dprintf-style", class_support,
16066 dprintf_style_enums, &dprintf_style, _("\
16067 Set the style of usage for dynamic printf."), _("\
16068 Show the style of usage for dynamic printf."), _("\
16069 This setting chooses how GDB will do a dynamic printf.\n\
16070 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16071 console, as with the \"printf\" command.\n\
16072 If the value is \"call\", the print is done by calling a function in your\n\
16073 program; by default printf(), but you can choose a different function or\n\
16074 output stream by setting dprintf-function and dprintf-channel."),
16075 update_dprintf_commands, NULL,
16076 &setlist, &showlist);
16077
16078 dprintf_function = xstrdup ("printf");
16079 add_setshow_string_cmd ("dprintf-function", class_support,
16080 &dprintf_function, _("\
16081 Set the function to use for dynamic printf."), _("\
16082 Show the function to use for dynamic printf."), NULL,
16083 update_dprintf_commands, NULL,
16084 &setlist, &showlist);
16085
16086 dprintf_channel = xstrdup ("");
16087 add_setshow_string_cmd ("dprintf-channel", class_support,
16088 &dprintf_channel, _("\
16089 Set the channel to use for dynamic printf."), _("\
16090 Show the channel to use for dynamic printf."), NULL,
16091 update_dprintf_commands, NULL,
16092 &setlist, &showlist);
16093
16094 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16095 &disconnected_dprintf, _("\
16096 Set whether dprintf continues after GDB disconnects."), _("\
16097 Show whether dprintf continues after GDB disconnects."), _("\
16098 Use this to let dprintf commands continue to hit and produce output\n\
16099 even if GDB disconnects or detaches from the target."),
16100 NULL,
16101 NULL,
16102 &setlist, &showlist);
16103
16104 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16105 Target agent only formatted printing, like the C \"printf\" function.\n\
16106 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
16107 This supports most C printf format specifications, like %s, %d, etc.\n\
16108 This is useful for formatted output in user-defined commands."));
16109
16110 automatic_hardware_breakpoints = true;
16111
16112 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
16113 "breakpoint");
16114 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
16115 "breakpoint");
16116 }