gdb: add all_breakpoints function
[binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2021 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include <ctype.h>
23 #include "hashtab.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "breakpoint.h"
27 #include "tracepoint.h"
28 #include "gdbtypes.h"
29 #include "expression.h"
30 #include "gdbcore.h"
31 #include "gdbcmd.h"
32 #include "value.h"
33 #include "command.h"
34 #include "inferior.h"
35 #include "infrun.h"
36 #include "gdbthread.h"
37 #include "target.h"
38 #include "language.h"
39 #include "gdb-demangle.h"
40 #include "filenames.h"
41 #include "annotate.h"
42 #include "symfile.h"
43 #include "objfiles.h"
44 #include "source.h"
45 #include "linespec.h"
46 #include "completer.h"
47 #include "ui-out.h"
48 #include "cli/cli-script.h"
49 #include "block.h"
50 #include "solib.h"
51 #include "solist.h"
52 #include "observable.h"
53 #include "memattr.h"
54 #include "ada-lang.h"
55 #include "top.h"
56 #include "valprint.h"
57 #include "jit.h"
58 #include "parser-defs.h"
59 #include "gdb_regex.h"
60 #include "probe.h"
61 #include "cli/cli-utils.h"
62 #include "stack.h"
63 #include "ax-gdb.h"
64 #include "dummy-frame.h"
65 #include "interps.h"
66 #include "gdbsupport/format.h"
67 #include "thread-fsm.h"
68 #include "tid-parse.h"
69 #include "cli/cli-style.h"
70
71 /* readline include files */
72 #include "readline/tilde.h"
73
74 /* readline defines this. */
75 #undef savestring
76
77 #include "mi/mi-common.h"
78 #include "extension.h"
79 #include <algorithm>
80 #include "progspace-and-thread.h"
81 #include "gdbsupport/array-view.h"
82 #include "gdbsupport/gdb_optional.h"
83
84 /* Prototypes for local functions. */
85
86 static void map_breakpoint_numbers (const char *,
87 gdb::function_view<void (breakpoint *)>);
88
89 static void breakpoint_re_set_default (struct breakpoint *);
90
91 static void
92 create_sals_from_location_default (struct event_location *location,
93 struct linespec_result *canonical,
94 enum bptype type_wanted);
95
96 static void create_breakpoints_sal_default (struct gdbarch *,
97 struct linespec_result *,
98 gdb::unique_xmalloc_ptr<char>,
99 gdb::unique_xmalloc_ptr<char>,
100 enum bptype,
101 enum bpdisp, int, int,
102 int,
103 const struct breakpoint_ops *,
104 int, int, int, unsigned);
105
106 static std::vector<symtab_and_line> decode_location_default
107 (struct breakpoint *b, struct event_location *location,
108 struct program_space *search_pspace);
109
110 static int can_use_hardware_watchpoint
111 (const std::vector<value_ref_ptr> &vals);
112
113 static void mention (struct breakpoint *);
114
115 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
116 enum bptype,
117 const struct breakpoint_ops *);
118 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
119 const struct symtab_and_line *);
120
121 /* This function is used in gdbtk sources and thus can not be made
122 static. */
123 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
124 struct symtab_and_line,
125 enum bptype,
126 const struct breakpoint_ops *);
127
128 static struct breakpoint *
129 momentary_breakpoint_from_master (struct breakpoint *orig,
130 enum bptype type,
131 const struct breakpoint_ops *ops,
132 int loc_enabled);
133
134 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
135
136 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
137 CORE_ADDR bpaddr,
138 enum bptype bptype);
139
140 static void describe_other_breakpoints (struct gdbarch *,
141 struct program_space *, CORE_ADDR,
142 struct obj_section *, int);
143
144 static int watchpoint_locations_match (struct bp_location *loc1,
145 struct bp_location *loc2);
146
147 static int breakpoint_locations_match (struct bp_location *loc1,
148 struct bp_location *loc2,
149 bool sw_hw_bps_match = false);
150
151 static int breakpoint_location_address_match (struct bp_location *bl,
152 const struct address_space *aspace,
153 CORE_ADDR addr);
154
155 static int breakpoint_location_address_range_overlap (struct bp_location *,
156 const address_space *,
157 CORE_ADDR, int);
158
159 static int remove_breakpoint (struct bp_location *);
160 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
161
162 static enum print_stop_action print_bp_stop_message (bpstat bs);
163
164 static int hw_breakpoint_used_count (void);
165
166 static int hw_watchpoint_use_count (struct breakpoint *);
167
168 static int hw_watchpoint_used_count_others (struct breakpoint *except,
169 enum bptype type,
170 int *other_type_used);
171
172 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
173 int count);
174
175 static void decref_bp_location (struct bp_location **loc);
176
177 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
178
179 /* update_global_location_list's modes of operation wrt to whether to
180 insert locations now. */
181 enum ugll_insert_mode
182 {
183 /* Don't insert any breakpoint locations into the inferior, only
184 remove already-inserted locations that no longer should be
185 inserted. Functions that delete a breakpoint or breakpoints
186 should specify this mode, so that deleting a breakpoint doesn't
187 have the side effect of inserting the locations of other
188 breakpoints that are marked not-inserted, but should_be_inserted
189 returns true on them.
190
191 This behavior is useful is situations close to tear-down -- e.g.,
192 after an exec, while the target still has execution, but
193 breakpoint shadows of the previous executable image should *NOT*
194 be restored to the new image; or before detaching, where the
195 target still has execution and wants to delete breakpoints from
196 GDB's lists, and all breakpoints had already been removed from
197 the inferior. */
198 UGLL_DONT_INSERT,
199
200 /* May insert breakpoints iff breakpoints_should_be_inserted_now
201 claims breakpoints should be inserted now. */
202 UGLL_MAY_INSERT,
203
204 /* Insert locations now, irrespective of
205 breakpoints_should_be_inserted_now. E.g., say all threads are
206 stopped right now, and the user did "continue". We need to
207 insert breakpoints _before_ resuming the target, but
208 UGLL_MAY_INSERT wouldn't insert them, because
209 breakpoints_should_be_inserted_now returns false at that point,
210 as no thread is running yet. */
211 UGLL_INSERT
212 };
213
214 static void update_global_location_list (enum ugll_insert_mode);
215
216 static void update_global_location_list_nothrow (enum ugll_insert_mode);
217
218 static void insert_breakpoint_locations (void);
219
220 static void trace_pass_command (const char *, int);
221
222 static void set_tracepoint_count (int num);
223
224 static bool is_masked_watchpoint (const struct breakpoint *b);
225
226 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
227
228 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
229 otherwise. */
230
231 static int strace_marker_p (struct breakpoint *b);
232
233 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
234 that are implemented on top of software or hardware breakpoints
235 (user breakpoints, internal and momentary breakpoints, etc.). */
236 static struct breakpoint_ops bkpt_base_breakpoint_ops;
237
238 /* Internal breakpoints class type. */
239 static struct breakpoint_ops internal_breakpoint_ops;
240
241 /* Momentary breakpoints class type. */
242 static struct breakpoint_ops momentary_breakpoint_ops;
243
244 /* The breakpoint_ops structure to be used in regular user created
245 breakpoints. */
246 struct breakpoint_ops bkpt_breakpoint_ops;
247
248 /* Breakpoints set on probes. */
249 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
250
251 /* Tracepoints set on probes. */
252 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
253
254 /* Dynamic printf class type. */
255 struct breakpoint_ops dprintf_breakpoint_ops;
256
257 /* The style in which to perform a dynamic printf. This is a user
258 option because different output options have different tradeoffs;
259 if GDB does the printing, there is better error handling if there
260 is a problem with any of the arguments, but using an inferior
261 function lets you have special-purpose printers and sending of
262 output to the same place as compiled-in print functions. */
263
264 static const char dprintf_style_gdb[] = "gdb";
265 static const char dprintf_style_call[] = "call";
266 static const char dprintf_style_agent[] = "agent";
267 static const char *const dprintf_style_enums[] = {
268 dprintf_style_gdb,
269 dprintf_style_call,
270 dprintf_style_agent,
271 NULL
272 };
273 static const char *dprintf_style = dprintf_style_gdb;
274
275 /* The function to use for dynamic printf if the preferred style is to
276 call into the inferior. The value is simply a string that is
277 copied into the command, so it can be anything that GDB can
278 evaluate to a callable address, not necessarily a function name. */
279
280 static char *dprintf_function;
281
282 /* The channel to use for dynamic printf if the preferred style is to
283 call into the inferior; if a nonempty string, it will be passed to
284 the call as the first argument, with the format string as the
285 second. As with the dprintf function, this can be anything that
286 GDB knows how to evaluate, so in addition to common choices like
287 "stderr", this could be an app-specific expression like
288 "mystreams[curlogger]". */
289
290 static char *dprintf_channel;
291
292 /* True if dprintf commands should continue to operate even if GDB
293 has disconnected. */
294 static bool disconnected_dprintf = true;
295
296 struct command_line *
297 breakpoint_commands (struct breakpoint *b)
298 {
299 return b->commands ? b->commands.get () : NULL;
300 }
301
302 /* Flag indicating that a command has proceeded the inferior past the
303 current breakpoint. */
304
305 static bool breakpoint_proceeded;
306
307 const char *
308 bpdisp_text (enum bpdisp disp)
309 {
310 /* NOTE: the following values are a part of MI protocol and
311 represent values of 'disp' field returned when inferior stops at
312 a breakpoint. */
313 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
314
315 return bpdisps[(int) disp];
316 }
317
318 /* Prototypes for exported functions. */
319 /* If FALSE, gdb will not use hardware support for watchpoints, even
320 if such is available. */
321 static int can_use_hw_watchpoints;
322
323 static void
324 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
325 struct cmd_list_element *c,
326 const char *value)
327 {
328 fprintf_filtered (file,
329 _("Debugger's willingness to use "
330 "watchpoint hardware is %s.\n"),
331 value);
332 }
333
334 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
335 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
336 for unrecognized breakpoint locations.
337 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
338 static enum auto_boolean pending_break_support;
339 static void
340 show_pending_break_support (struct ui_file *file, int from_tty,
341 struct cmd_list_element *c,
342 const char *value)
343 {
344 fprintf_filtered (file,
345 _("Debugger's behavior regarding "
346 "pending breakpoints is %s.\n"),
347 value);
348 }
349
350 /* If true, gdb will automatically use hardware breakpoints for breakpoints
351 set with "break" but falling in read-only memory.
352 If false, gdb will warn about such breakpoints, but won't automatically
353 use hardware breakpoints. */
354 static bool automatic_hardware_breakpoints;
355 static void
356 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
357 struct cmd_list_element *c,
358 const char *value)
359 {
360 fprintf_filtered (file,
361 _("Automatic usage of hardware breakpoints is %s.\n"),
362 value);
363 }
364
365 /* If on, GDB keeps breakpoints inserted even if the inferior is
366 stopped, and immediately inserts any new breakpoints as soon as
367 they're created. If off (default), GDB keeps breakpoints off of
368 the target as long as possible. That is, it delays inserting
369 breakpoints until the next resume, and removes them again when the
370 target fully stops. This is a bit safer in case GDB crashes while
371 processing user input. */
372 static bool always_inserted_mode = false;
373
374 static void
375 show_always_inserted_mode (struct ui_file *file, int from_tty,
376 struct cmd_list_element *c, const char *value)
377 {
378 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
379 value);
380 }
381
382 /* See breakpoint.h. */
383
384 int
385 breakpoints_should_be_inserted_now (void)
386 {
387 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
388 {
389 /* If breakpoints are global, they should be inserted even if no
390 thread under gdb's control is running, or even if there are
391 no threads under GDB's control yet. */
392 return 1;
393 }
394 else
395 {
396 if (always_inserted_mode)
397 {
398 /* The user wants breakpoints inserted even if all threads
399 are stopped. */
400 return 1;
401 }
402
403 for (inferior *inf : all_inferiors ())
404 if (inf->has_execution ()
405 && threads_are_executing (inf->process_target ()))
406 return 1;
407
408 /* Don't remove breakpoints yet if, even though all threads are
409 stopped, we still have events to process. */
410 for (thread_info *tp : all_non_exited_threads ())
411 if (tp->resumed
412 && tp->suspend.waitstatus_pending_p)
413 return 1;
414 }
415 return 0;
416 }
417
418 static const char condition_evaluation_both[] = "host or target";
419
420 /* Modes for breakpoint condition evaluation. */
421 static const char condition_evaluation_auto[] = "auto";
422 static const char condition_evaluation_host[] = "host";
423 static const char condition_evaluation_target[] = "target";
424 static const char *const condition_evaluation_enums[] = {
425 condition_evaluation_auto,
426 condition_evaluation_host,
427 condition_evaluation_target,
428 NULL
429 };
430
431 /* Global that holds the current mode for breakpoint condition evaluation. */
432 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
433
434 /* Global that we use to display information to the user (gets its value from
435 condition_evaluation_mode_1. */
436 static const char *condition_evaluation_mode = condition_evaluation_auto;
437
438 /* Translate a condition evaluation mode MODE into either "host"
439 or "target". This is used mostly to translate from "auto" to the
440 real setting that is being used. It returns the translated
441 evaluation mode. */
442
443 static const char *
444 translate_condition_evaluation_mode (const char *mode)
445 {
446 if (mode == condition_evaluation_auto)
447 {
448 if (target_supports_evaluation_of_breakpoint_conditions ())
449 return condition_evaluation_target;
450 else
451 return condition_evaluation_host;
452 }
453 else
454 return mode;
455 }
456
457 /* Discovers what condition_evaluation_auto translates to. */
458
459 static const char *
460 breakpoint_condition_evaluation_mode (void)
461 {
462 return translate_condition_evaluation_mode (condition_evaluation_mode);
463 }
464
465 /* Return true if GDB should evaluate breakpoint conditions or false
466 otherwise. */
467
468 static int
469 gdb_evaluates_breakpoint_condition_p (void)
470 {
471 const char *mode = breakpoint_condition_evaluation_mode ();
472
473 return (mode == condition_evaluation_host);
474 }
475
476 /* Are we executing breakpoint commands? */
477 static int executing_breakpoint_commands;
478
479 /* Are overlay event breakpoints enabled? */
480 static int overlay_events_enabled;
481
482 /* See description in breakpoint.h. */
483 bool target_exact_watchpoints = false;
484
485 /* Walk the following statement or block through all breakpoints.
486 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
487 current breakpoint. */
488
489 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
490 for (B = breakpoint_chain; \
491 B ? (TMP=B->next, 1): 0; \
492 B = TMP)
493
494 /* Similar iterator for the low-level breakpoints. SAFE variant is
495 not provided so update_global_location_list must not be called
496 while executing the block of ALL_BP_LOCATIONS. */
497
498 #define ALL_BP_LOCATIONS(B,BP_TMP) \
499 for (BP_TMP = bp_locations; \
500 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
501 BP_TMP++)
502
503 /* Iterates through locations with address ADDRESS for the currently selected
504 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
505 to where the loop should start from.
506 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
507 appropriate location to start with. */
508
509 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
510 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
511 BP_LOCP_TMP = BP_LOCP_START; \
512 BP_LOCP_START \
513 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
514 && (*BP_LOCP_TMP)->address == ADDRESS); \
515 BP_LOCP_TMP++)
516
517 /* Iterator for tracepoints only. */
518
519 #define ALL_TRACEPOINTS(B) \
520 for (B = breakpoint_chain; B; B = B->next) \
521 if (is_tracepoint (B))
522
523 /* Chains of all breakpoints defined. */
524
525 static struct breakpoint *breakpoint_chain;
526
527 /* Breakpoint linked list iterator. */
528
529 using breakpoint_iterator = next_iterator<breakpoint>;
530
531 /* Breakpoint linked list range. */
532
533 using breakpoint_range = next_adapter<breakpoint, breakpoint_iterator>;
534
535 /* Return a range to iterate over all breakpoints. */
536
537 static breakpoint_range
538 all_breakpoints ()
539 {
540 return breakpoint_range (breakpoint_chain);
541 }
542
543 /* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
544
545 static struct bp_location **bp_locations;
546
547 /* Number of elements of BP_LOCATIONS. */
548
549 static unsigned bp_locations_count;
550
551 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
552 ADDRESS for the current elements of BP_LOCATIONS which get a valid
553 result from bp_location_has_shadow. You can use it for roughly
554 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
555 an address you need to read. */
556
557 static CORE_ADDR bp_locations_placed_address_before_address_max;
558
559 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
560 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
561 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
562 You can use it for roughly limiting the subrange of BP_LOCATIONS to
563 scan for shadow bytes for an address you need to read. */
564
565 static CORE_ADDR bp_locations_shadow_len_after_address_max;
566
567 /* The locations that no longer correspond to any breakpoint, unlinked
568 from the bp_locations array, but for which a hit may still be
569 reported by a target. */
570 static std::vector<bp_location *> moribund_locations;
571
572 /* Number of last breakpoint made. */
573
574 static int breakpoint_count;
575
576 /* The value of `breakpoint_count' before the last command that
577 created breakpoints. If the last (break-like) command created more
578 than one breakpoint, then the difference between BREAKPOINT_COUNT
579 and PREV_BREAKPOINT_COUNT is more than one. */
580 static int prev_breakpoint_count;
581
582 /* Number of last tracepoint made. */
583
584 static int tracepoint_count;
585
586 static struct cmd_list_element *breakpoint_set_cmdlist;
587 static struct cmd_list_element *breakpoint_show_cmdlist;
588 struct cmd_list_element *save_cmdlist;
589
590 /* See declaration at breakpoint.h. */
591
592 struct breakpoint *
593 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
594 void *user_data)
595 {
596 for (breakpoint *b : all_breakpoints ())
597 if (func (b, user_data) != 0)
598 return b;
599
600 return nullptr;
601 }
602
603 /* Return whether a breakpoint is an active enabled breakpoint. */
604 static int
605 breakpoint_enabled (struct breakpoint *b)
606 {
607 return (b->enable_state == bp_enabled);
608 }
609
610 /* Set breakpoint count to NUM. */
611
612 static void
613 set_breakpoint_count (int num)
614 {
615 prev_breakpoint_count = breakpoint_count;
616 breakpoint_count = num;
617 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
618 }
619
620 /* Used by `start_rbreak_breakpoints' below, to record the current
621 breakpoint count before "rbreak" creates any breakpoint. */
622 static int rbreak_start_breakpoint_count;
623
624 /* Called at the start an "rbreak" command to record the first
625 breakpoint made. */
626
627 scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
628 {
629 rbreak_start_breakpoint_count = breakpoint_count;
630 }
631
632 /* Called at the end of an "rbreak" command to record the last
633 breakpoint made. */
634
635 scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
636 {
637 prev_breakpoint_count = rbreak_start_breakpoint_count;
638 }
639
640 /* Used in run_command to zero the hit count when a new run starts. */
641
642 void
643 clear_breakpoint_hit_counts (void)
644 {
645 for (breakpoint *b : all_breakpoints ())
646 b->hit_count = 0;
647 }
648
649 \f
650 /* Return the breakpoint with the specified number, or NULL
651 if the number does not refer to an existing breakpoint. */
652
653 struct breakpoint *
654 get_breakpoint (int num)
655 {
656 for (breakpoint *b : all_breakpoints ())
657 if (b->number == num)
658 return b;
659
660 return nullptr;
661 }
662
663 \f
664
665 /* Mark locations as "conditions have changed" in case the target supports
666 evaluating conditions on its side. */
667
668 static void
669 mark_breakpoint_modified (struct breakpoint *b)
670 {
671 struct bp_location *loc;
672
673 /* This is only meaningful if the target is
674 evaluating conditions and if the user has
675 opted for condition evaluation on the target's
676 side. */
677 if (gdb_evaluates_breakpoint_condition_p ()
678 || !target_supports_evaluation_of_breakpoint_conditions ())
679 return;
680
681 if (!is_breakpoint (b))
682 return;
683
684 for (loc = b->loc; loc; loc = loc->next)
685 loc->condition_changed = condition_modified;
686 }
687
688 /* Mark location as "conditions have changed" in case the target supports
689 evaluating conditions on its side. */
690
691 static void
692 mark_breakpoint_location_modified (struct bp_location *loc)
693 {
694 /* This is only meaningful if the target is
695 evaluating conditions and if the user has
696 opted for condition evaluation on the target's
697 side. */
698 if (gdb_evaluates_breakpoint_condition_p ()
699 || !target_supports_evaluation_of_breakpoint_conditions ())
700
701 return;
702
703 if (!is_breakpoint (loc->owner))
704 return;
705
706 loc->condition_changed = condition_modified;
707 }
708
709 /* Sets the condition-evaluation mode using the static global
710 condition_evaluation_mode. */
711
712 static void
713 set_condition_evaluation_mode (const char *args, int from_tty,
714 struct cmd_list_element *c)
715 {
716 const char *old_mode, *new_mode;
717
718 if ((condition_evaluation_mode_1 == condition_evaluation_target)
719 && !target_supports_evaluation_of_breakpoint_conditions ())
720 {
721 condition_evaluation_mode_1 = condition_evaluation_mode;
722 warning (_("Target does not support breakpoint condition evaluation.\n"
723 "Using host evaluation mode instead."));
724 return;
725 }
726
727 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
728 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
729
730 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
731 settings was "auto". */
732 condition_evaluation_mode = condition_evaluation_mode_1;
733
734 /* Only update the mode if the user picked a different one. */
735 if (new_mode != old_mode)
736 {
737 struct bp_location *loc, **loc_tmp;
738 /* If the user switched to a different evaluation mode, we
739 need to synch the changes with the target as follows:
740
741 "host" -> "target": Send all (valid) conditions to the target.
742 "target" -> "host": Remove all the conditions from the target.
743 */
744
745 if (new_mode == condition_evaluation_target)
746 {
747 /* Mark everything modified and synch conditions with the
748 target. */
749 ALL_BP_LOCATIONS (loc, loc_tmp)
750 mark_breakpoint_location_modified (loc);
751 }
752 else
753 {
754 /* Manually mark non-duplicate locations to synch conditions
755 with the target. We do this to remove all the conditions the
756 target knows about. */
757 ALL_BP_LOCATIONS (loc, loc_tmp)
758 if (is_breakpoint (loc->owner) && loc->inserted)
759 loc->needs_update = 1;
760 }
761
762 /* Do the update. */
763 update_global_location_list (UGLL_MAY_INSERT);
764 }
765
766 return;
767 }
768
769 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
770 what "auto" is translating to. */
771
772 static void
773 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
774 struct cmd_list_element *c, const char *value)
775 {
776 if (condition_evaluation_mode == condition_evaluation_auto)
777 fprintf_filtered (file,
778 _("Breakpoint condition evaluation "
779 "mode is %s (currently %s).\n"),
780 value,
781 breakpoint_condition_evaluation_mode ());
782 else
783 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
784 value);
785 }
786
787 /* A comparison function for bp_location AP and BP that is used by
788 bsearch. This comparison function only cares about addresses, unlike
789 the more general bp_location_is_less_than function. */
790
791 static int
792 bp_locations_compare_addrs (const void *ap, const void *bp)
793 {
794 const struct bp_location *a = *(const struct bp_location **) ap;
795 const struct bp_location *b = *(const struct bp_location **) bp;
796
797 if (a->address == b->address)
798 return 0;
799 else
800 return ((a->address > b->address) - (a->address < b->address));
801 }
802
803 /* Helper function to skip all bp_locations with addresses
804 less than ADDRESS. It returns the first bp_location that
805 is greater than or equal to ADDRESS. If none is found, just
806 return NULL. */
807
808 static struct bp_location **
809 get_first_locp_gte_addr (CORE_ADDR address)
810 {
811 struct bp_location dummy_loc;
812 struct bp_location *dummy_locp = &dummy_loc;
813 struct bp_location **locp_found = NULL;
814
815 /* Initialize the dummy location's address field. */
816 dummy_loc.address = address;
817
818 /* Find a close match to the first location at ADDRESS. */
819 locp_found = ((struct bp_location **)
820 bsearch (&dummy_locp, bp_locations, bp_locations_count,
821 sizeof (struct bp_location **),
822 bp_locations_compare_addrs));
823
824 /* Nothing was found, nothing left to do. */
825 if (locp_found == NULL)
826 return NULL;
827
828 /* We may have found a location that is at ADDRESS but is not the first in the
829 location's list. Go backwards (if possible) and locate the first one. */
830 while ((locp_found - 1) >= bp_locations
831 && (*(locp_found - 1))->address == address)
832 locp_found--;
833
834 return locp_found;
835 }
836
837 /* Parse COND_STRING in the context of LOC and set as the condition
838 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
839 the number of LOC within its owner. In case of parsing error, mark
840 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
841
842 static void
843 set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
844 int bp_num, int loc_num)
845 {
846 bool has_junk = false;
847 try
848 {
849 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
850 block_for_pc (loc->address), 0);
851 if (*cond_string != 0)
852 has_junk = true;
853 else
854 {
855 loc->cond = std::move (new_exp);
856 if (loc->disabled_by_cond && loc->enabled)
857 printf_filtered (_("Breakpoint %d's condition is now valid at "
858 "location %d, enabling.\n"),
859 bp_num, loc_num);
860
861 loc->disabled_by_cond = false;
862 }
863 }
864 catch (const gdb_exception_error &e)
865 {
866 if (loc->enabled)
867 {
868 /* Warn if a user-enabled location is now becoming disabled-by-cond.
869 BP_NUM is 0 if the breakpoint is being defined for the first
870 time using the "break ... if ..." command, and non-zero if
871 already defined. */
872 if (bp_num != 0)
873 warning (_("failed to validate condition at location %d.%d, "
874 "disabling:\n %s"), bp_num, loc_num, e.what ());
875 else
876 warning (_("failed to validate condition at location %d, "
877 "disabling:\n %s"), loc_num, e.what ());
878 }
879
880 loc->disabled_by_cond = true;
881 }
882
883 if (has_junk)
884 error (_("Garbage '%s' follows condition"), cond_string);
885 }
886
887 void
888 set_breakpoint_condition (struct breakpoint *b, const char *exp,
889 int from_tty, bool force)
890 {
891 if (*exp == 0)
892 {
893 xfree (b->cond_string);
894 b->cond_string = nullptr;
895
896 if (is_watchpoint (b))
897 static_cast<watchpoint *> (b)->cond_exp.reset ();
898 else
899 {
900 int loc_num = 1;
901 for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)
902 {
903 loc->cond.reset ();
904 if (loc->disabled_by_cond && loc->enabled)
905 printf_filtered (_("Breakpoint %d's condition is now valid at "
906 "location %d, enabling.\n"),
907 b->number, loc_num);
908 loc->disabled_by_cond = false;
909 loc_num++;
910
911 /* No need to free the condition agent expression
912 bytecode (if we have one). We will handle this
913 when we go through update_global_location_list. */
914 }
915 }
916
917 if (from_tty)
918 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
919 }
920 else
921 {
922 if (is_watchpoint (b))
923 {
924 innermost_block_tracker tracker;
925 const char *arg = exp;
926 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
927 if (*arg != 0)
928 error (_("Junk at end of expression"));
929 watchpoint *w = static_cast<watchpoint *> (b);
930 w->cond_exp = std::move (new_exp);
931 w->cond_exp_valid_block = tracker.block ();
932 }
933 else
934 {
935 /* Parse and set condition expressions. We make two passes.
936 In the first, we parse the condition string to see if it
937 is valid in at least one location. If so, the condition
938 would be accepted. So we go ahead and set the locations'
939 conditions. In case no valid case is found, we throw
940 the error and the condition string will be rejected.
941 This two-pass approach is taken to avoid setting the
942 state of locations in case of a reject. */
943 for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)
944 {
945 try
946 {
947 const char *arg = exp;
948 parse_exp_1 (&arg, loc->address,
949 block_for_pc (loc->address), 0);
950 if (*arg != 0)
951 error (_("Junk at end of expression"));
952 break;
953 }
954 catch (const gdb_exception_error &e)
955 {
956 /* Condition string is invalid. If this happens to
957 be the last loc, abandon (if not forced) or continue
958 (if forced). */
959 if (loc->next == nullptr && !force)
960 throw;
961 }
962 }
963
964 /* If we reach here, the condition is valid at some locations. */
965 int loc_num = 1;
966 for (bp_location *loc = b->loc; loc != nullptr;
967 loc = loc->next, loc_num++)
968 set_breakpoint_location_condition (exp, loc, b->number, loc_num);
969 }
970
971 /* We know that the new condition parsed successfully. The
972 condition string of the breakpoint can be safely updated. */
973 xfree (b->cond_string);
974 b->cond_string = xstrdup (exp);
975 b->condition_not_parsed = 0;
976 }
977 mark_breakpoint_modified (b);
978
979 gdb::observers::breakpoint_modified.notify (b);
980 }
981
982 /* See breakpoint.h. */
983
984 void
985 set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
986 bool force)
987 {
988 for (breakpoint *b : all_breakpoints ())
989 if (b->number == bpnum)
990 {
991 /* Check if this breakpoint has a "stop" method implemented in an
992 extension language. This method and conditions entered into GDB
993 from the CLI are mutually exclusive. */
994 const struct extension_language_defn *extlang
995 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
996
997 if (extlang != NULL)
998 {
999 error (_("Only one stop condition allowed. There is currently"
1000 " a %s stop condition defined for this breakpoint."),
1001 ext_lang_capitalized_name (extlang));
1002 }
1003 set_breakpoint_condition (b, exp, from_tty, force);
1004
1005 if (is_breakpoint (b))
1006 update_global_location_list (UGLL_MAY_INSERT);
1007
1008 return;
1009 }
1010
1011 error (_("No breakpoint number %d."), bpnum);
1012 }
1013
1014 /* The options for the "condition" command. */
1015
1016 struct condition_command_opts
1017 {
1018 /* For "-force". */
1019 bool force_condition = false;
1020 };
1021
1022 static const gdb::option::option_def condition_command_option_defs[] = {
1023
1024 gdb::option::flag_option_def<condition_command_opts> {
1025 "force",
1026 [] (condition_command_opts *opts) { return &opts->force_condition; },
1027 N_("Set the condition even if it is invalid for all current locations."),
1028 },
1029
1030 };
1031
1032 /* Create an option_def_group for the "condition" options, with
1033 CC_OPTS as context. */
1034
1035 static inline gdb::option::option_def_group
1036 make_condition_command_options_def_group (condition_command_opts *cc_opts)
1037 {
1038 return {{condition_command_option_defs}, cc_opts};
1039 }
1040
1041 /* Completion for the "condition" command. */
1042
1043 static void
1044 condition_completer (struct cmd_list_element *cmd,
1045 completion_tracker &tracker,
1046 const char *text, const char * /*word*/)
1047 {
1048 bool has_no_arguments = (*text == '\0');
1049 condition_command_opts cc_opts;
1050 const auto group = make_condition_command_options_def_group (&cc_opts);
1051 if (gdb::option::complete_options
1052 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1053 return;
1054
1055 text = skip_spaces (text);
1056 const char *space = skip_to_space (text);
1057 if (*space == '\0')
1058 {
1059 int len;
1060
1061 if (text[0] == '$')
1062 {
1063 tracker.advance_custom_word_point_by (1);
1064 /* We don't support completion of history indices. */
1065 if (!isdigit (text[1]))
1066 complete_internalvar (tracker, &text[1]);
1067 return;
1068 }
1069
1070 /* Suggest the "-force" flag if no arguments are given. If
1071 arguments were passed, they either already include the flag,
1072 or we are beyond the point of suggesting it because it's
1073 positionally the first argument. */
1074 if (has_no_arguments)
1075 gdb::option::complete_on_all_options (tracker, group);
1076
1077 /* We're completing the breakpoint number. */
1078 len = strlen (text);
1079
1080 for (breakpoint *b : all_breakpoints ())
1081 {
1082 char number[50];
1083
1084 xsnprintf (number, sizeof (number), "%d", b->number);
1085
1086 if (strncmp (number, text, len) == 0)
1087 tracker.add_completion (make_unique_xstrdup (number));
1088 }
1089
1090 return;
1091 }
1092
1093 /* We're completing the expression part. Skip the breakpoint num. */
1094 const char *exp_start = skip_spaces (space);
1095 tracker.advance_custom_word_point_by (exp_start - text);
1096 text = exp_start;
1097 const char *word = advance_to_expression_complete_word_point (tracker, text);
1098 expression_completer (cmd, tracker, text, word);
1099 }
1100
1101 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1102
1103 static void
1104 condition_command (const char *arg, int from_tty)
1105 {
1106 const char *p;
1107 int bnum;
1108
1109 if (arg == 0)
1110 error_no_arg (_("breakpoint number"));
1111
1112 p = arg;
1113
1114 /* Check if the "-force" flag was passed. */
1115 condition_command_opts cc_opts;
1116 const auto group = make_condition_command_options_def_group (&cc_opts);
1117 gdb::option::process_options
1118 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
1119
1120 bnum = get_number (&p);
1121 if (bnum == 0)
1122 error (_("Bad breakpoint argument: '%s'"), arg);
1123
1124 set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
1125 }
1126
1127 /* Check that COMMAND do not contain commands that are suitable
1128 only for tracepoints and not suitable for ordinary breakpoints.
1129 Throw if any such commands is found. */
1130
1131 static void
1132 check_no_tracepoint_commands (struct command_line *commands)
1133 {
1134 struct command_line *c;
1135
1136 for (c = commands; c; c = c->next)
1137 {
1138 if (c->control_type == while_stepping_control)
1139 error (_("The 'while-stepping' command can "
1140 "only be used for tracepoints"));
1141
1142 check_no_tracepoint_commands (c->body_list_0.get ());
1143 check_no_tracepoint_commands (c->body_list_1.get ());
1144
1145 /* Not that command parsing removes leading whitespace and comment
1146 lines and also empty lines. So, we only need to check for
1147 command directly. */
1148 if (strstr (c->line, "collect ") == c->line)
1149 error (_("The 'collect' command can only be used for tracepoints"));
1150
1151 if (strstr (c->line, "teval ") == c->line)
1152 error (_("The 'teval' command can only be used for tracepoints"));
1153 }
1154 }
1155
1156 struct longjmp_breakpoint : public breakpoint
1157 {
1158 ~longjmp_breakpoint () override;
1159 };
1160
1161 /* Encapsulate tests for different types of tracepoints. */
1162
1163 static bool
1164 is_tracepoint_type (bptype type)
1165 {
1166 return (type == bp_tracepoint
1167 || type == bp_fast_tracepoint
1168 || type == bp_static_tracepoint);
1169 }
1170
1171 static bool
1172 is_longjmp_type (bptype type)
1173 {
1174 return type == bp_longjmp || type == bp_exception;
1175 }
1176
1177 /* See breakpoint.h. */
1178
1179 bool
1180 is_tracepoint (const struct breakpoint *b)
1181 {
1182 return is_tracepoint_type (b->type);
1183 }
1184
1185 /* Factory function to create an appropriate instance of breakpoint given
1186 TYPE. */
1187
1188 static std::unique_ptr<breakpoint>
1189 new_breakpoint_from_type (bptype type)
1190 {
1191 breakpoint *b;
1192
1193 if (is_tracepoint_type (type))
1194 b = new tracepoint ();
1195 else if (is_longjmp_type (type))
1196 b = new longjmp_breakpoint ();
1197 else
1198 b = new breakpoint ();
1199
1200 return std::unique_ptr<breakpoint> (b);
1201 }
1202
1203 /* A helper function that validates that COMMANDS are valid for a
1204 breakpoint. This function will throw an exception if a problem is
1205 found. */
1206
1207 static void
1208 validate_commands_for_breakpoint (struct breakpoint *b,
1209 struct command_line *commands)
1210 {
1211 if (is_tracepoint (b))
1212 {
1213 struct tracepoint *t = (struct tracepoint *) b;
1214 struct command_line *c;
1215 struct command_line *while_stepping = 0;
1216
1217 /* Reset the while-stepping step count. The previous commands
1218 might have included a while-stepping action, while the new
1219 ones might not. */
1220 t->step_count = 0;
1221
1222 /* We need to verify that each top-level element of commands is
1223 valid for tracepoints, that there's at most one
1224 while-stepping element, and that the while-stepping's body
1225 has valid tracing commands excluding nested while-stepping.
1226 We also need to validate the tracepoint action line in the
1227 context of the tracepoint --- validate_actionline actually
1228 has side effects, like setting the tracepoint's
1229 while-stepping STEP_COUNT, in addition to checking if the
1230 collect/teval actions parse and make sense in the
1231 tracepoint's context. */
1232 for (c = commands; c; c = c->next)
1233 {
1234 if (c->control_type == while_stepping_control)
1235 {
1236 if (b->type == bp_fast_tracepoint)
1237 error (_("The 'while-stepping' command "
1238 "cannot be used for fast tracepoint"));
1239 else if (b->type == bp_static_tracepoint)
1240 error (_("The 'while-stepping' command "
1241 "cannot be used for static tracepoint"));
1242
1243 if (while_stepping)
1244 error (_("The 'while-stepping' command "
1245 "can be used only once"));
1246 else
1247 while_stepping = c;
1248 }
1249
1250 validate_actionline (c->line, b);
1251 }
1252 if (while_stepping)
1253 {
1254 struct command_line *c2;
1255
1256 gdb_assert (while_stepping->body_list_1 == nullptr);
1257 c2 = while_stepping->body_list_0.get ();
1258 for (; c2; c2 = c2->next)
1259 {
1260 if (c2->control_type == while_stepping_control)
1261 error (_("The 'while-stepping' command cannot be nested"));
1262 }
1263 }
1264 }
1265 else
1266 {
1267 check_no_tracepoint_commands (commands);
1268 }
1269 }
1270
1271 /* Return a vector of all the static tracepoints set at ADDR. The
1272 caller is responsible for releasing the vector. */
1273
1274 std::vector<breakpoint *>
1275 static_tracepoints_here (CORE_ADDR addr)
1276 {
1277 std::vector<breakpoint *> found;
1278 struct bp_location *loc;
1279
1280 for (breakpoint *b : all_breakpoints ())
1281 if (b->type == bp_static_tracepoint)
1282 {
1283 for (loc = b->loc; loc; loc = loc->next)
1284 if (loc->address == addr)
1285 found.push_back (b);
1286 }
1287
1288 return found;
1289 }
1290
1291 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1292 validate that only allowed commands are included. */
1293
1294 void
1295 breakpoint_set_commands (struct breakpoint *b,
1296 counted_command_line &&commands)
1297 {
1298 validate_commands_for_breakpoint (b, commands.get ());
1299
1300 b->commands = std::move (commands);
1301 gdb::observers::breakpoint_modified.notify (b);
1302 }
1303
1304 /* Set the internal `silent' flag on the breakpoint. Note that this
1305 is not the same as the "silent" that may appear in the breakpoint's
1306 commands. */
1307
1308 void
1309 breakpoint_set_silent (struct breakpoint *b, int silent)
1310 {
1311 int old_silent = b->silent;
1312
1313 b->silent = silent;
1314 if (old_silent != silent)
1315 gdb::observers::breakpoint_modified.notify (b);
1316 }
1317
1318 /* Set the thread for this breakpoint. If THREAD is -1, make the
1319 breakpoint work for any thread. */
1320
1321 void
1322 breakpoint_set_thread (struct breakpoint *b, int thread)
1323 {
1324 int old_thread = b->thread;
1325
1326 b->thread = thread;
1327 if (old_thread != thread)
1328 gdb::observers::breakpoint_modified.notify (b);
1329 }
1330
1331 /* Set the task for this breakpoint. If TASK is 0, make the
1332 breakpoint work for any task. */
1333
1334 void
1335 breakpoint_set_task (struct breakpoint *b, int task)
1336 {
1337 int old_task = b->task;
1338
1339 b->task = task;
1340 if (old_task != task)
1341 gdb::observers::breakpoint_modified.notify (b);
1342 }
1343
1344 static void
1345 commands_command_1 (const char *arg, int from_tty,
1346 struct command_line *control)
1347 {
1348 counted_command_line cmd;
1349 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1350 NULL after the call to read_command_lines if the user provides an empty
1351 list of command by just typing "end". */
1352 bool cmd_read = false;
1353
1354 std::string new_arg;
1355
1356 if (arg == NULL || !*arg)
1357 {
1358 /* Argument not explicitly given. Synthesize it. */
1359 if (breakpoint_count - prev_breakpoint_count > 1)
1360 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1361 breakpoint_count);
1362 else if (breakpoint_count > 0)
1363 new_arg = string_printf ("%d", breakpoint_count);
1364 }
1365 else
1366 {
1367 /* Create a copy of ARG. This is needed because the "commands"
1368 command may be coming from a script. In that case, the read
1369 line buffer is going to be overwritten in the lambda of
1370 'map_breakpoint_numbers' below when reading the next line
1371 before we are are done parsing the breakpoint numbers. */
1372 new_arg = arg;
1373 }
1374 arg = new_arg.c_str ();
1375
1376 map_breakpoint_numbers
1377 (arg, [&] (breakpoint *b)
1378 {
1379 if (!cmd_read)
1380 {
1381 gdb_assert (cmd == NULL);
1382 if (control != NULL)
1383 cmd = control->body_list_0;
1384 else
1385 {
1386 std::string str
1387 = string_printf (_("Type commands for breakpoint(s) "
1388 "%s, one per line."),
1389 arg);
1390
1391 auto do_validate = [=] (const char *line)
1392 {
1393 validate_actionline (line, b);
1394 };
1395 gdb::function_view<void (const char *)> validator;
1396 if (is_tracepoint (b))
1397 validator = do_validate;
1398
1399 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1400 }
1401 cmd_read = true;
1402 }
1403
1404 /* If a breakpoint was on the list more than once, we don't need to
1405 do anything. */
1406 if (b->commands != cmd)
1407 {
1408 validate_commands_for_breakpoint (b, cmd.get ());
1409 b->commands = cmd;
1410 gdb::observers::breakpoint_modified.notify (b);
1411 }
1412 });
1413 }
1414
1415 static void
1416 commands_command (const char *arg, int from_tty)
1417 {
1418 commands_command_1 (arg, from_tty, NULL);
1419 }
1420
1421 /* Like commands_command, but instead of reading the commands from
1422 input stream, takes them from an already parsed command structure.
1423
1424 This is used by cli-script.c to DTRT with breakpoint commands
1425 that are part of if and while bodies. */
1426 enum command_control_type
1427 commands_from_control_command (const char *arg, struct command_line *cmd)
1428 {
1429 commands_command_1 (arg, 0, cmd);
1430 return simple_control;
1431 }
1432
1433 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1434
1435 static int
1436 bp_location_has_shadow (struct bp_location *bl)
1437 {
1438 if (bl->loc_type != bp_loc_software_breakpoint)
1439 return 0;
1440 if (!bl->inserted)
1441 return 0;
1442 if (bl->target_info.shadow_len == 0)
1443 /* BL isn't valid, or doesn't shadow memory. */
1444 return 0;
1445 return 1;
1446 }
1447
1448 /* Update BUF, which is LEN bytes read from the target address
1449 MEMADDR, by replacing a memory breakpoint with its shadowed
1450 contents.
1451
1452 If READBUF is not NULL, this buffer must not overlap with the of
1453 the breakpoint location's shadow_contents buffer. Otherwise, a
1454 failed assertion internal error will be raised. */
1455
1456 static void
1457 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1458 const gdb_byte *writebuf_org,
1459 ULONGEST memaddr, LONGEST len,
1460 struct bp_target_info *target_info,
1461 struct gdbarch *gdbarch)
1462 {
1463 /* Now do full processing of the found relevant range of elements. */
1464 CORE_ADDR bp_addr = 0;
1465 int bp_size = 0;
1466 int bptoffset = 0;
1467
1468 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1469 current_program_space->aspace, 0))
1470 {
1471 /* The breakpoint is inserted in a different address space. */
1472 return;
1473 }
1474
1475 /* Addresses and length of the part of the breakpoint that
1476 we need to copy. */
1477 bp_addr = target_info->placed_address;
1478 bp_size = target_info->shadow_len;
1479
1480 if (bp_addr + bp_size <= memaddr)
1481 {
1482 /* The breakpoint is entirely before the chunk of memory we are
1483 reading. */
1484 return;
1485 }
1486
1487 if (bp_addr >= memaddr + len)
1488 {
1489 /* The breakpoint is entirely after the chunk of memory we are
1490 reading. */
1491 return;
1492 }
1493
1494 /* Offset within shadow_contents. */
1495 if (bp_addr < memaddr)
1496 {
1497 /* Only copy the second part of the breakpoint. */
1498 bp_size -= memaddr - bp_addr;
1499 bptoffset = memaddr - bp_addr;
1500 bp_addr = memaddr;
1501 }
1502
1503 if (bp_addr + bp_size > memaddr + len)
1504 {
1505 /* Only copy the first part of the breakpoint. */
1506 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1507 }
1508
1509 if (readbuf != NULL)
1510 {
1511 /* Verify that the readbuf buffer does not overlap with the
1512 shadow_contents buffer. */
1513 gdb_assert (target_info->shadow_contents >= readbuf + len
1514 || readbuf >= (target_info->shadow_contents
1515 + target_info->shadow_len));
1516
1517 /* Update the read buffer with this inserted breakpoint's
1518 shadow. */
1519 memcpy (readbuf + bp_addr - memaddr,
1520 target_info->shadow_contents + bptoffset, bp_size);
1521 }
1522 else
1523 {
1524 const unsigned char *bp;
1525 CORE_ADDR addr = target_info->reqstd_address;
1526 int placed_size;
1527
1528 /* Update the shadow with what we want to write to memory. */
1529 memcpy (target_info->shadow_contents + bptoffset,
1530 writebuf_org + bp_addr - memaddr, bp_size);
1531
1532 /* Determine appropriate breakpoint contents and size for this
1533 address. */
1534 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1535
1536 /* Update the final write buffer with this inserted
1537 breakpoint's INSN. */
1538 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1539 }
1540 }
1541
1542 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1543 by replacing any memory breakpoints with their shadowed contents.
1544
1545 If READBUF is not NULL, this buffer must not overlap with any of
1546 the breakpoint location's shadow_contents buffers. Otherwise,
1547 a failed assertion internal error will be raised.
1548
1549 The range of shadowed area by each bp_location is:
1550 bl->address - bp_locations_placed_address_before_address_max
1551 up to bl->address + bp_locations_shadow_len_after_address_max
1552 The range we were requested to resolve shadows for is:
1553 memaddr ... memaddr + len
1554 Thus the safe cutoff boundaries for performance optimization are
1555 memaddr + len <= (bl->address
1556 - bp_locations_placed_address_before_address_max)
1557 and:
1558 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1559
1560 void
1561 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1562 const gdb_byte *writebuf_org,
1563 ULONGEST memaddr, LONGEST len)
1564 {
1565 /* Left boundary, right boundary and median element of our binary
1566 search. */
1567 unsigned bc_l, bc_r, bc;
1568
1569 /* Find BC_L which is a leftmost element which may affect BUF
1570 content. It is safe to report lower value but a failure to
1571 report higher one. */
1572
1573 bc_l = 0;
1574 bc_r = bp_locations_count;
1575 while (bc_l + 1 < bc_r)
1576 {
1577 struct bp_location *bl;
1578
1579 bc = (bc_l + bc_r) / 2;
1580 bl = bp_locations[bc];
1581
1582 /* Check first BL->ADDRESS will not overflow due to the added
1583 constant. Then advance the left boundary only if we are sure
1584 the BC element can in no way affect the BUF content (MEMADDR
1585 to MEMADDR + LEN range).
1586
1587 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1588 offset so that we cannot miss a breakpoint with its shadow
1589 range tail still reaching MEMADDR. */
1590
1591 if ((bl->address + bp_locations_shadow_len_after_address_max
1592 >= bl->address)
1593 && (bl->address + bp_locations_shadow_len_after_address_max
1594 <= memaddr))
1595 bc_l = bc;
1596 else
1597 bc_r = bc;
1598 }
1599
1600 /* Due to the binary search above, we need to make sure we pick the
1601 first location that's at BC_L's address. E.g., if there are
1602 multiple locations at the same address, BC_L may end up pointing
1603 at a duplicate location, and miss the "master"/"inserted"
1604 location. Say, given locations L1, L2 and L3 at addresses A and
1605 B:
1606
1607 L1@A, L2@A, L3@B, ...
1608
1609 BC_L could end up pointing at location L2, while the "master"
1610 location could be L1. Since the `loc->inserted' flag is only set
1611 on "master" locations, we'd forget to restore the shadow of L1
1612 and L2. */
1613 while (bc_l > 0
1614 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1615 bc_l--;
1616
1617 /* Now do full processing of the found relevant range of elements. */
1618
1619 for (bc = bc_l; bc < bp_locations_count; bc++)
1620 {
1621 struct bp_location *bl = bp_locations[bc];
1622
1623 /* bp_location array has BL->OWNER always non-NULL. */
1624 if (bl->owner->type == bp_none)
1625 warning (_("reading through apparently deleted breakpoint #%d?"),
1626 bl->owner->number);
1627
1628 /* Performance optimization: any further element can no longer affect BUF
1629 content. */
1630
1631 if (bl->address >= bp_locations_placed_address_before_address_max
1632 && memaddr + len <= (bl->address
1633 - bp_locations_placed_address_before_address_max))
1634 break;
1635
1636 if (!bp_location_has_shadow (bl))
1637 continue;
1638
1639 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1640 memaddr, len, &bl->target_info, bl->gdbarch);
1641 }
1642 }
1643
1644 /* See breakpoint.h. */
1645
1646 bool
1647 is_breakpoint (const struct breakpoint *bpt)
1648 {
1649 return (bpt->type == bp_breakpoint
1650 || bpt->type == bp_hardware_breakpoint
1651 || bpt->type == bp_dprintf);
1652 }
1653
1654 /* Return true if BPT is of any hardware watchpoint kind. */
1655
1656 static bool
1657 is_hardware_watchpoint (const struct breakpoint *bpt)
1658 {
1659 return (bpt->type == bp_hardware_watchpoint
1660 || bpt->type == bp_read_watchpoint
1661 || bpt->type == bp_access_watchpoint);
1662 }
1663
1664 /* See breakpoint.h. */
1665
1666 bool
1667 is_watchpoint (const struct breakpoint *bpt)
1668 {
1669 return (is_hardware_watchpoint (bpt)
1670 || bpt->type == bp_watchpoint);
1671 }
1672
1673 /* Returns true if the current thread and its running state are safe
1674 to evaluate or update watchpoint B. Watchpoints on local
1675 expressions need to be evaluated in the context of the thread that
1676 was current when the watchpoint was created, and, that thread needs
1677 to be stopped to be able to select the correct frame context.
1678 Watchpoints on global expressions can be evaluated on any thread,
1679 and in any state. It is presently left to the target allowing
1680 memory accesses when threads are running. */
1681
1682 static int
1683 watchpoint_in_thread_scope (struct watchpoint *b)
1684 {
1685 return (b->pspace == current_program_space
1686 && (b->watchpoint_thread == null_ptid
1687 || (inferior_ptid == b->watchpoint_thread
1688 && !inferior_thread ()->executing)));
1689 }
1690
1691 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1692 associated bp_watchpoint_scope breakpoint. */
1693
1694 static void
1695 watchpoint_del_at_next_stop (struct watchpoint *w)
1696 {
1697 if (w->related_breakpoint != w)
1698 {
1699 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1700 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1701 w->related_breakpoint->disposition = disp_del_at_next_stop;
1702 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1703 w->related_breakpoint = w;
1704 }
1705 w->disposition = disp_del_at_next_stop;
1706 }
1707
1708 /* Extract a bitfield value from value VAL using the bit parameters contained in
1709 watchpoint W. */
1710
1711 static struct value *
1712 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1713 {
1714 struct value *bit_val;
1715
1716 if (val == NULL)
1717 return NULL;
1718
1719 bit_val = allocate_value (value_type (val));
1720
1721 unpack_value_bitfield (bit_val,
1722 w->val_bitpos,
1723 w->val_bitsize,
1724 value_contents_for_printing (val),
1725 value_offset (val),
1726 val);
1727
1728 return bit_val;
1729 }
1730
1731 /* Allocate a dummy location and add it to B, which must be a software
1732 watchpoint. This is required because even if a software watchpoint
1733 is not watching any memory, bpstat_stop_status requires a location
1734 to be able to report stops. */
1735
1736 static void
1737 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1738 struct program_space *pspace)
1739 {
1740 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1741
1742 b->loc = allocate_bp_location (b);
1743 b->loc->pspace = pspace;
1744 b->loc->address = -1;
1745 b->loc->length = -1;
1746 }
1747
1748 /* Returns true if B is a software watchpoint that is not watching any
1749 memory (e.g., "watch $pc"). */
1750
1751 static bool
1752 is_no_memory_software_watchpoint (struct breakpoint *b)
1753 {
1754 return (b->type == bp_watchpoint
1755 && b->loc != NULL
1756 && b->loc->next == NULL
1757 && b->loc->address == -1
1758 && b->loc->length == -1);
1759 }
1760
1761 /* Assuming that B is a watchpoint:
1762 - Reparse watchpoint expression, if REPARSE is non-zero
1763 - Evaluate expression and store the result in B->val
1764 - Evaluate the condition if there is one, and store the result
1765 in b->loc->cond.
1766 - Update the list of values that must be watched in B->loc.
1767
1768 If the watchpoint disposition is disp_del_at_next_stop, then do
1769 nothing. If this is local watchpoint that is out of scope, delete
1770 it.
1771
1772 Even with `set breakpoint always-inserted on' the watchpoints are
1773 removed + inserted on each stop here. Normal breakpoints must
1774 never be removed because they might be missed by a running thread
1775 when debugging in non-stop mode. On the other hand, hardware
1776 watchpoints (is_hardware_watchpoint; processed here) are specific
1777 to each LWP since they are stored in each LWP's hardware debug
1778 registers. Therefore, such LWP must be stopped first in order to
1779 be able to modify its hardware watchpoints.
1780
1781 Hardware watchpoints must be reset exactly once after being
1782 presented to the user. It cannot be done sooner, because it would
1783 reset the data used to present the watchpoint hit to the user. And
1784 it must not be done later because it could display the same single
1785 watchpoint hit during multiple GDB stops. Note that the latter is
1786 relevant only to the hardware watchpoint types bp_read_watchpoint
1787 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1788 not user-visible - its hit is suppressed if the memory content has
1789 not changed.
1790
1791 The following constraints influence the location where we can reset
1792 hardware watchpoints:
1793
1794 * target_stopped_by_watchpoint and target_stopped_data_address are
1795 called several times when GDB stops.
1796
1797 [linux]
1798 * Multiple hardware watchpoints can be hit at the same time,
1799 causing GDB to stop. GDB only presents one hardware watchpoint
1800 hit at a time as the reason for stopping, and all the other hits
1801 are presented later, one after the other, each time the user
1802 requests the execution to be resumed. Execution is not resumed
1803 for the threads still having pending hit event stored in
1804 LWP_INFO->STATUS. While the watchpoint is already removed from
1805 the inferior on the first stop the thread hit event is kept being
1806 reported from its cached value by linux_nat_stopped_data_address
1807 until the real thread resume happens after the watchpoint gets
1808 presented and thus its LWP_INFO->STATUS gets reset.
1809
1810 Therefore the hardware watchpoint hit can get safely reset on the
1811 watchpoint removal from inferior. */
1812
1813 static void
1814 update_watchpoint (struct watchpoint *b, int reparse)
1815 {
1816 int within_current_scope;
1817 struct frame_id saved_frame_id;
1818 int frame_saved;
1819
1820 /* If this is a local watchpoint, we only want to check if the
1821 watchpoint frame is in scope if the current thread is the thread
1822 that was used to create the watchpoint. */
1823 if (!watchpoint_in_thread_scope (b))
1824 return;
1825
1826 if (b->disposition == disp_del_at_next_stop)
1827 return;
1828
1829 frame_saved = 0;
1830
1831 /* Determine if the watchpoint is within scope. */
1832 if (b->exp_valid_block == NULL)
1833 within_current_scope = 1;
1834 else
1835 {
1836 struct frame_info *fi = get_current_frame ();
1837 struct gdbarch *frame_arch = get_frame_arch (fi);
1838 CORE_ADDR frame_pc = get_frame_pc (fi);
1839
1840 /* If we're at a point where the stack has been destroyed
1841 (e.g. in a function epilogue), unwinding may not work
1842 properly. Do not attempt to recreate locations at this
1843 point. See similar comments in watchpoint_check. */
1844 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1845 return;
1846
1847 /* Save the current frame's ID so we can restore it after
1848 evaluating the watchpoint expression on its own frame. */
1849 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1850 took a frame parameter, so that we didn't have to change the
1851 selected frame. */
1852 frame_saved = 1;
1853 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1854
1855 fi = frame_find_by_id (b->watchpoint_frame);
1856 within_current_scope = (fi != NULL);
1857 if (within_current_scope)
1858 select_frame (fi);
1859 }
1860
1861 /* We don't free locations. They are stored in the bp_location array
1862 and update_global_location_list will eventually delete them and
1863 remove breakpoints if needed. */
1864 b->loc = NULL;
1865
1866 if (within_current_scope && reparse)
1867 {
1868 const char *s;
1869
1870 b->exp.reset ();
1871 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1872 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1873 /* If the meaning of expression itself changed, the old value is
1874 no longer relevant. We don't want to report a watchpoint hit
1875 to the user when the old value and the new value may actually
1876 be completely different objects. */
1877 b->val = NULL;
1878 b->val_valid = false;
1879
1880 /* Note that unlike with breakpoints, the watchpoint's condition
1881 expression is stored in the breakpoint object, not in the
1882 locations (re)created below. */
1883 if (b->cond_string != NULL)
1884 {
1885 b->cond_exp.reset ();
1886
1887 s = b->cond_string;
1888 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1889 }
1890 }
1891
1892 /* If we failed to parse the expression, for example because
1893 it refers to a global variable in a not-yet-loaded shared library,
1894 don't try to insert watchpoint. We don't automatically delete
1895 such watchpoint, though, since failure to parse expression
1896 is different from out-of-scope watchpoint. */
1897 if (!target_has_execution ())
1898 {
1899 /* Without execution, memory can't change. No use to try and
1900 set watchpoint locations. The watchpoint will be reset when
1901 the target gains execution, through breakpoint_re_set. */
1902 if (!can_use_hw_watchpoints)
1903 {
1904 if (b->ops->works_in_software_mode (b))
1905 b->type = bp_watchpoint;
1906 else
1907 error (_("Can't set read/access watchpoint when "
1908 "hardware watchpoints are disabled."));
1909 }
1910 }
1911 else if (within_current_scope && b->exp)
1912 {
1913 std::vector<value_ref_ptr> val_chain;
1914 struct value *v, *result;
1915 struct program_space *frame_pspace;
1916
1917 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
1918 &val_chain, false);
1919
1920 /* Avoid setting b->val if it's already set. The meaning of
1921 b->val is 'the last value' user saw, and we should update
1922 it only if we reported that last value to user. As it
1923 happens, the code that reports it updates b->val directly.
1924 We don't keep track of the memory value for masked
1925 watchpoints. */
1926 if (!b->val_valid && !is_masked_watchpoint (b))
1927 {
1928 if (b->val_bitsize != 0)
1929 v = extract_bitfield_from_watchpoint_value (b, v);
1930 b->val = release_value (v);
1931 b->val_valid = true;
1932 }
1933
1934 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1935
1936 /* Look at each value on the value chain. */
1937 gdb_assert (!val_chain.empty ());
1938 for (const value_ref_ptr &iter : val_chain)
1939 {
1940 v = iter.get ();
1941
1942 /* If it's a memory location, and GDB actually needed
1943 its contents to evaluate the expression, then we
1944 must watch it. If the first value returned is
1945 still lazy, that means an error occurred reading it;
1946 watch it anyway in case it becomes readable. */
1947 if (VALUE_LVAL (v) == lval_memory
1948 && (v == val_chain[0] || ! value_lazy (v)))
1949 {
1950 struct type *vtype = check_typedef (value_type (v));
1951
1952 /* We only watch structs and arrays if user asked
1953 for it explicitly, never if they just happen to
1954 appear in the middle of some value chain. */
1955 if (v == result
1956 || (vtype->code () != TYPE_CODE_STRUCT
1957 && vtype->code () != TYPE_CODE_ARRAY))
1958 {
1959 CORE_ADDR addr;
1960 enum target_hw_bp_type type;
1961 struct bp_location *loc, **tmp;
1962 int bitpos = 0, bitsize = 0;
1963
1964 if (value_bitsize (v) != 0)
1965 {
1966 /* Extract the bit parameters out from the bitfield
1967 sub-expression. */
1968 bitpos = value_bitpos (v);
1969 bitsize = value_bitsize (v);
1970 }
1971 else if (v == result && b->val_bitsize != 0)
1972 {
1973 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1974 lvalue whose bit parameters are saved in the fields
1975 VAL_BITPOS and VAL_BITSIZE. */
1976 bitpos = b->val_bitpos;
1977 bitsize = b->val_bitsize;
1978 }
1979
1980 addr = value_address (v);
1981 if (bitsize != 0)
1982 {
1983 /* Skip the bytes that don't contain the bitfield. */
1984 addr += bitpos / 8;
1985 }
1986
1987 type = hw_write;
1988 if (b->type == bp_read_watchpoint)
1989 type = hw_read;
1990 else if (b->type == bp_access_watchpoint)
1991 type = hw_access;
1992
1993 loc = allocate_bp_location (b);
1994 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1995 ;
1996 *tmp = loc;
1997 loc->gdbarch = value_type (v)->arch ();
1998
1999 loc->pspace = frame_pspace;
2000 loc->address = address_significant (loc->gdbarch, addr);
2001
2002 if (bitsize != 0)
2003 {
2004 /* Just cover the bytes that make up the bitfield. */
2005 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2006 }
2007 else
2008 loc->length = TYPE_LENGTH (value_type (v));
2009
2010 loc->watchpoint_type = type;
2011 }
2012 }
2013 }
2014
2015 /* Change the type of breakpoint between hardware assisted or
2016 an ordinary watchpoint depending on the hardware support
2017 and free hardware slots. REPARSE is set when the inferior
2018 is started. */
2019 if (reparse)
2020 {
2021 int reg_cnt;
2022 enum bp_loc_type loc_type;
2023 struct bp_location *bl;
2024
2025 reg_cnt = can_use_hardware_watchpoint (val_chain);
2026
2027 if (reg_cnt)
2028 {
2029 int i, target_resources_ok, other_type_used;
2030 enum bptype type;
2031
2032 /* Use an exact watchpoint when there's only one memory region to be
2033 watched, and only one debug register is needed to watch it. */
2034 b->exact = target_exact_watchpoints && reg_cnt == 1;
2035
2036 /* We need to determine how many resources are already
2037 used for all other hardware watchpoints plus this one
2038 to see if we still have enough resources to also fit
2039 this watchpoint in as well. */
2040
2041 /* If this is a software watchpoint, we try to turn it
2042 to a hardware one -- count resources as if B was of
2043 hardware watchpoint type. */
2044 type = b->type;
2045 if (type == bp_watchpoint)
2046 type = bp_hardware_watchpoint;
2047
2048 /* This watchpoint may or may not have been placed on
2049 the list yet at this point (it won't be in the list
2050 if we're trying to create it for the first time,
2051 through watch_command), so always account for it
2052 manually. */
2053
2054 /* Count resources used by all watchpoints except B. */
2055 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
2056
2057 /* Add in the resources needed for B. */
2058 i += hw_watchpoint_use_count (b);
2059
2060 target_resources_ok
2061 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2062 if (target_resources_ok <= 0)
2063 {
2064 int sw_mode = b->ops->works_in_software_mode (b);
2065
2066 if (target_resources_ok == 0 && !sw_mode)
2067 error (_("Target does not support this type of "
2068 "hardware watchpoint."));
2069 else if (target_resources_ok < 0 && !sw_mode)
2070 error (_("There are not enough available hardware "
2071 "resources for this watchpoint."));
2072
2073 /* Downgrade to software watchpoint. */
2074 b->type = bp_watchpoint;
2075 }
2076 else
2077 {
2078 /* If this was a software watchpoint, we've just
2079 found we have enough resources to turn it to a
2080 hardware watchpoint. Otherwise, this is a
2081 nop. */
2082 b->type = type;
2083 }
2084 }
2085 else if (!b->ops->works_in_software_mode (b))
2086 {
2087 if (!can_use_hw_watchpoints)
2088 error (_("Can't set read/access watchpoint when "
2089 "hardware watchpoints are disabled."));
2090 else
2091 error (_("Expression cannot be implemented with "
2092 "read/access watchpoint."));
2093 }
2094 else
2095 b->type = bp_watchpoint;
2096
2097 loc_type = (b->type == bp_watchpoint? bp_loc_other
2098 : bp_loc_hardware_watchpoint);
2099 for (bl = b->loc; bl; bl = bl->next)
2100 bl->loc_type = loc_type;
2101 }
2102
2103 /* If a software watchpoint is not watching any memory, then the
2104 above left it without any location set up. But,
2105 bpstat_stop_status requires a location to be able to report
2106 stops, so make sure there's at least a dummy one. */
2107 if (b->type == bp_watchpoint && b->loc == NULL)
2108 software_watchpoint_add_no_memory_location (b, frame_pspace);
2109 }
2110 else if (!within_current_scope)
2111 {
2112 printf_filtered (_("\
2113 Watchpoint %d deleted because the program has left the block\n\
2114 in which its expression is valid.\n"),
2115 b->number);
2116 watchpoint_del_at_next_stop (b);
2117 }
2118
2119 /* Restore the selected frame. */
2120 if (frame_saved)
2121 select_frame (frame_find_by_id (saved_frame_id));
2122 }
2123
2124
2125 /* Returns 1 iff breakpoint location should be
2126 inserted in the inferior. We don't differentiate the type of BL's owner
2127 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2128 breakpoint_ops is not defined, because in insert_bp_location,
2129 tracepoint's insert_location will not be called. */
2130 static int
2131 should_be_inserted (struct bp_location *bl)
2132 {
2133 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2134 return 0;
2135
2136 if (bl->owner->disposition == disp_del_at_next_stop)
2137 return 0;
2138
2139 if (!bl->enabled || bl->disabled_by_cond
2140 || bl->shlib_disabled || bl->duplicate)
2141 return 0;
2142
2143 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2144 return 0;
2145
2146 /* This is set for example, when we're attached to the parent of a
2147 vfork, and have detached from the child. The child is running
2148 free, and we expect it to do an exec or exit, at which point the
2149 OS makes the parent schedulable again (and the target reports
2150 that the vfork is done). Until the child is done with the shared
2151 memory region, do not insert breakpoints in the parent, otherwise
2152 the child could still trip on the parent's breakpoints. Since
2153 the parent is blocked anyway, it won't miss any breakpoint. */
2154 if (bl->pspace->breakpoints_not_allowed)
2155 return 0;
2156
2157 /* Don't insert a breakpoint if we're trying to step past its
2158 location, except if the breakpoint is a single-step breakpoint,
2159 and the breakpoint's thread is the thread which is stepping past
2160 a breakpoint. */
2161 if ((bl->loc_type == bp_loc_software_breakpoint
2162 || bl->loc_type == bp_loc_hardware_breakpoint)
2163 && stepping_past_instruction_at (bl->pspace->aspace,
2164 bl->address)
2165 /* The single-step breakpoint may be inserted at the location
2166 we're trying to step if the instruction branches to itself.
2167 However, the instruction won't be executed at all and it may
2168 break the semantics of the instruction, for example, the
2169 instruction is a conditional branch or updates some flags.
2170 We can't fix it unless GDB is able to emulate the instruction
2171 or switch to displaced stepping. */
2172 && !(bl->owner->type == bp_single_step
2173 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2174 {
2175 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2176 paddress (bl->gdbarch, bl->address));
2177 return 0;
2178 }
2179
2180 /* Don't insert watchpoints if we're trying to step past the
2181 instruction that triggered one. */
2182 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2183 && stepping_past_nonsteppable_watchpoint ())
2184 {
2185 infrun_debug_printf ("stepping past non-steppable watchpoint. "
2186 "skipping watchpoint at %s:%d",
2187 paddress (bl->gdbarch, bl->address), bl->length);
2188 return 0;
2189 }
2190
2191 return 1;
2192 }
2193
2194 /* Same as should_be_inserted but does the check assuming
2195 that the location is not duplicated. */
2196
2197 static int
2198 unduplicated_should_be_inserted (struct bp_location *bl)
2199 {
2200 int result;
2201 const int save_duplicate = bl->duplicate;
2202
2203 bl->duplicate = 0;
2204 result = should_be_inserted (bl);
2205 bl->duplicate = save_duplicate;
2206 return result;
2207 }
2208
2209 /* Parses a conditional described by an expression COND into an
2210 agent expression bytecode suitable for evaluation
2211 by the bytecode interpreter. Return NULL if there was
2212 any error during parsing. */
2213
2214 static agent_expr_up
2215 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2216 {
2217 if (cond == NULL)
2218 return NULL;
2219
2220 agent_expr_up aexpr;
2221
2222 /* We don't want to stop processing, so catch any errors
2223 that may show up. */
2224 try
2225 {
2226 aexpr = gen_eval_for_expr (scope, cond);
2227 }
2228
2229 catch (const gdb_exception_error &ex)
2230 {
2231 /* If we got here, it means the condition could not be parsed to a valid
2232 bytecode expression and thus can't be evaluated on the target's side.
2233 It's no use iterating through the conditions. */
2234 }
2235
2236 /* We have a valid agent expression. */
2237 return aexpr;
2238 }
2239
2240 /* Based on location BL, create a list of breakpoint conditions to be
2241 passed on to the target. If we have duplicated locations with different
2242 conditions, we will add such conditions to the list. The idea is that the
2243 target will evaluate the list of conditions and will only notify GDB when
2244 one of them is true. */
2245
2246 static void
2247 build_target_condition_list (struct bp_location *bl)
2248 {
2249 struct bp_location **locp = NULL, **loc2p;
2250 int null_condition_or_parse_error = 0;
2251 int modified = bl->needs_update;
2252 struct bp_location *loc;
2253
2254 /* Release conditions left over from a previous insert. */
2255 bl->target_info.conditions.clear ();
2256
2257 /* This is only meaningful if the target is
2258 evaluating conditions and if the user has
2259 opted for condition evaluation on the target's
2260 side. */
2261 if (gdb_evaluates_breakpoint_condition_p ()
2262 || !target_supports_evaluation_of_breakpoint_conditions ())
2263 return;
2264
2265 /* Do a first pass to check for locations with no assigned
2266 conditions or conditions that fail to parse to a valid agent
2267 expression bytecode. If any of these happen, then it's no use to
2268 send conditions to the target since this location will always
2269 trigger and generate a response back to GDB. Note we consider
2270 all locations at the same address irrespective of type, i.e.,
2271 even if the locations aren't considered duplicates (e.g.,
2272 software breakpoint and hardware breakpoint at the same
2273 address). */
2274 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2275 {
2276 loc = (*loc2p);
2277 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2278 {
2279 if (modified)
2280 {
2281 /* Re-parse the conditions since something changed. In that
2282 case we already freed the condition bytecodes (see
2283 force_breakpoint_reinsertion). We just
2284 need to parse the condition to bytecodes again. */
2285 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2286 loc->cond.get ());
2287 }
2288
2289 /* If we have a NULL bytecode expression, it means something
2290 went wrong or we have a null condition expression. */
2291 if (!loc->cond_bytecode)
2292 {
2293 null_condition_or_parse_error = 1;
2294 break;
2295 }
2296 }
2297 }
2298
2299 /* If any of these happened, it means we will have to evaluate the conditions
2300 for the location's address on gdb's side. It is no use keeping bytecodes
2301 for all the other duplicate locations, thus we free all of them here.
2302
2303 This is so we have a finer control over which locations' conditions are
2304 being evaluated by GDB or the remote stub. */
2305 if (null_condition_or_parse_error)
2306 {
2307 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2308 {
2309 loc = (*loc2p);
2310 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2311 {
2312 /* Only go as far as the first NULL bytecode is
2313 located. */
2314 if (!loc->cond_bytecode)
2315 return;
2316
2317 loc->cond_bytecode.reset ();
2318 }
2319 }
2320 }
2321
2322 /* No NULL conditions or failed bytecode generation. Build a
2323 condition list for this location's address. If we have software
2324 and hardware locations at the same address, they aren't
2325 considered duplicates, but we still marge all the conditions
2326 anyway, as it's simpler, and doesn't really make a practical
2327 difference. */
2328 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2329 {
2330 loc = (*loc2p);
2331 if (loc->cond
2332 && is_breakpoint (loc->owner)
2333 && loc->pspace->num == bl->pspace->num
2334 && loc->owner->enable_state == bp_enabled
2335 && loc->enabled
2336 && !loc->disabled_by_cond)
2337 {
2338 /* Add the condition to the vector. This will be used later
2339 to send the conditions to the target. */
2340 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2341 }
2342 }
2343
2344 return;
2345 }
2346
2347 /* Parses a command described by string CMD into an agent expression
2348 bytecode suitable for evaluation by the bytecode interpreter.
2349 Return NULL if there was any error during parsing. */
2350
2351 static agent_expr_up
2352 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2353 {
2354 const char *cmdrest;
2355 const char *format_start, *format_end;
2356 struct gdbarch *gdbarch = get_current_arch ();
2357
2358 if (cmd == NULL)
2359 return NULL;
2360
2361 cmdrest = cmd;
2362
2363 if (*cmdrest == ',')
2364 ++cmdrest;
2365 cmdrest = skip_spaces (cmdrest);
2366
2367 if (*cmdrest++ != '"')
2368 error (_("No format string following the location"));
2369
2370 format_start = cmdrest;
2371
2372 format_pieces fpieces (&cmdrest);
2373
2374 format_end = cmdrest;
2375
2376 if (*cmdrest++ != '"')
2377 error (_("Bad format string, non-terminated '\"'."));
2378
2379 cmdrest = skip_spaces (cmdrest);
2380
2381 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2382 error (_("Invalid argument syntax"));
2383
2384 if (*cmdrest == ',')
2385 cmdrest++;
2386 cmdrest = skip_spaces (cmdrest);
2387
2388 /* For each argument, make an expression. */
2389
2390 std::vector<struct expression *> argvec;
2391 while (*cmdrest != '\0')
2392 {
2393 const char *cmd1;
2394
2395 cmd1 = cmdrest;
2396 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2397 argvec.push_back (expr.release ());
2398 cmdrest = cmd1;
2399 if (*cmdrest == ',')
2400 ++cmdrest;
2401 }
2402
2403 agent_expr_up aexpr;
2404
2405 /* We don't want to stop processing, so catch any errors
2406 that may show up. */
2407 try
2408 {
2409 aexpr = gen_printf (scope, gdbarch, 0, 0,
2410 format_start, format_end - format_start,
2411 argvec.size (), argvec.data ());
2412 }
2413 catch (const gdb_exception_error &ex)
2414 {
2415 /* If we got here, it means the command could not be parsed to a valid
2416 bytecode expression and thus can't be evaluated on the target's side.
2417 It's no use iterating through the other commands. */
2418 }
2419
2420 /* We have a valid agent expression, return it. */
2421 return aexpr;
2422 }
2423
2424 /* Based on location BL, create a list of breakpoint commands to be
2425 passed on to the target. If we have duplicated locations with
2426 different commands, we will add any such to the list. */
2427
2428 static void
2429 build_target_command_list (struct bp_location *bl)
2430 {
2431 struct bp_location **locp = NULL, **loc2p;
2432 int null_command_or_parse_error = 0;
2433 int modified = bl->needs_update;
2434 struct bp_location *loc;
2435
2436 /* Clear commands left over from a previous insert. */
2437 bl->target_info.tcommands.clear ();
2438
2439 if (!target_can_run_breakpoint_commands ())
2440 return;
2441
2442 /* For now, limit to agent-style dprintf breakpoints. */
2443 if (dprintf_style != dprintf_style_agent)
2444 return;
2445
2446 /* For now, if we have any location at the same address that isn't a
2447 dprintf, don't install the target-side commands, as that would
2448 make the breakpoint not be reported to the core, and we'd lose
2449 control. */
2450 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2451 {
2452 loc = (*loc2p);
2453 if (is_breakpoint (loc->owner)
2454 && loc->pspace->num == bl->pspace->num
2455 && loc->owner->type != bp_dprintf)
2456 return;
2457 }
2458
2459 /* Do a first pass to check for locations with no assigned
2460 conditions or conditions that fail to parse to a valid agent expression
2461 bytecode. If any of these happen, then it's no use to send conditions
2462 to the target since this location will always trigger and generate a
2463 response back to GDB. */
2464 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2465 {
2466 loc = (*loc2p);
2467 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2468 {
2469 if (modified)
2470 {
2471 /* Re-parse the commands since something changed. In that
2472 case we already freed the command bytecodes (see
2473 force_breakpoint_reinsertion). We just
2474 need to parse the command to bytecodes again. */
2475 loc->cmd_bytecode
2476 = parse_cmd_to_aexpr (bl->address,
2477 loc->owner->extra_string);
2478 }
2479
2480 /* If we have a NULL bytecode expression, it means something
2481 went wrong or we have a null command expression. */
2482 if (!loc->cmd_bytecode)
2483 {
2484 null_command_or_parse_error = 1;
2485 break;
2486 }
2487 }
2488 }
2489
2490 /* If anything failed, then we're not doing target-side commands,
2491 and so clean up. */
2492 if (null_command_or_parse_error)
2493 {
2494 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2495 {
2496 loc = (*loc2p);
2497 if (is_breakpoint (loc->owner)
2498 && loc->pspace->num == bl->pspace->num)
2499 {
2500 /* Only go as far as the first NULL bytecode is
2501 located. */
2502 if (loc->cmd_bytecode == NULL)
2503 return;
2504
2505 loc->cmd_bytecode.reset ();
2506 }
2507 }
2508 }
2509
2510 /* No NULL commands or failed bytecode generation. Build a command
2511 list for all duplicate locations at this location's address.
2512 Note that here we must care for whether the breakpoint location
2513 types are considered duplicates, otherwise, say, if we have a
2514 software and hardware location at the same address, the target
2515 could end up running the commands twice. For the moment, we only
2516 support targets-side commands with dprintf, but it doesn't hurt
2517 to be pedantically correct in case that changes. */
2518 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2519 {
2520 loc = (*loc2p);
2521 if (breakpoint_locations_match (bl, loc)
2522 && loc->owner->extra_string
2523 && is_breakpoint (loc->owner)
2524 && loc->pspace->num == bl->pspace->num
2525 && loc->owner->enable_state == bp_enabled
2526 && loc->enabled
2527 && !loc->disabled_by_cond)
2528 {
2529 /* Add the command to the vector. This will be used later
2530 to send the commands to the target. */
2531 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2532 }
2533 }
2534
2535 bl->target_info.persist = 0;
2536 /* Maybe flag this location as persistent. */
2537 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2538 bl->target_info.persist = 1;
2539 }
2540
2541 /* Return the kind of breakpoint on address *ADDR. Get the kind
2542 of breakpoint according to ADDR except single-step breakpoint.
2543 Get the kind of single-step breakpoint according to the current
2544 registers state. */
2545
2546 static int
2547 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2548 {
2549 if (bl->owner->type == bp_single_step)
2550 {
2551 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2552 struct regcache *regcache;
2553
2554 regcache = get_thread_regcache (thr);
2555
2556 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2557 regcache, addr);
2558 }
2559 else
2560 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2561 }
2562
2563 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2564 location. Any error messages are printed to TMP_ERROR_STREAM; and
2565 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2566 Returns 0 for success, 1 if the bp_location type is not supported or
2567 -1 for failure.
2568
2569 NOTE drow/2003-09-09: This routine could be broken down to an
2570 object-style method for each breakpoint or catchpoint type. */
2571 static int
2572 insert_bp_location (struct bp_location *bl,
2573 struct ui_file *tmp_error_stream,
2574 int *disabled_breaks,
2575 int *hw_breakpoint_error,
2576 int *hw_bp_error_explained_already)
2577 {
2578 gdb_exception bp_excpt;
2579
2580 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2581 return 0;
2582
2583 /* Note we don't initialize bl->target_info, as that wipes out
2584 the breakpoint location's shadow_contents if the breakpoint
2585 is still inserted at that location. This in turn breaks
2586 target_read_memory which depends on these buffers when
2587 a memory read is requested at the breakpoint location:
2588 Once the target_info has been wiped, we fail to see that
2589 we have a breakpoint inserted at that address and thus
2590 read the breakpoint instead of returning the data saved in
2591 the breakpoint location's shadow contents. */
2592 bl->target_info.reqstd_address = bl->address;
2593 bl->target_info.placed_address_space = bl->pspace->aspace;
2594 bl->target_info.length = bl->length;
2595
2596 /* When working with target-side conditions, we must pass all the conditions
2597 for the same breakpoint address down to the target since GDB will not
2598 insert those locations. With a list of breakpoint conditions, the target
2599 can decide when to stop and notify GDB. */
2600
2601 if (is_breakpoint (bl->owner))
2602 {
2603 build_target_condition_list (bl);
2604 build_target_command_list (bl);
2605 /* Reset the modification marker. */
2606 bl->needs_update = 0;
2607 }
2608
2609 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2610 set at a read-only address, then a breakpoint location will have
2611 been changed to hardware breakpoint before we get here. If it is
2612 "off" however, error out before actually trying to insert the
2613 breakpoint, with a nicer error message. */
2614 if (bl->loc_type == bp_loc_software_breakpoint
2615 && !automatic_hardware_breakpoints)
2616 {
2617 mem_region *mr = lookup_mem_region (bl->address);
2618
2619 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2620 {
2621 fprintf_unfiltered (tmp_error_stream,
2622 _("Cannot insert breakpoint %d.\n"
2623 "Cannot set software breakpoint "
2624 "at read-only address %s\n"),
2625 bl->owner->number,
2626 paddress (bl->gdbarch, bl->address));
2627 return 1;
2628 }
2629 }
2630
2631 if (bl->loc_type == bp_loc_software_breakpoint
2632 || bl->loc_type == bp_loc_hardware_breakpoint)
2633 {
2634 /* First check to see if we have to handle an overlay. */
2635 if (overlay_debugging == ovly_off
2636 || bl->section == NULL
2637 || !(section_is_overlay (bl->section)))
2638 {
2639 /* No overlay handling: just set the breakpoint. */
2640 try
2641 {
2642 int val;
2643
2644 val = bl->owner->ops->insert_location (bl);
2645 if (val)
2646 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2647 }
2648 catch (gdb_exception &e)
2649 {
2650 bp_excpt = std::move (e);
2651 }
2652 }
2653 else
2654 {
2655 /* This breakpoint is in an overlay section.
2656 Shall we set a breakpoint at the LMA? */
2657 if (!overlay_events_enabled)
2658 {
2659 /* Yes -- overlay event support is not active,
2660 so we must try to set a breakpoint at the LMA.
2661 This will not work for a hardware breakpoint. */
2662 if (bl->loc_type == bp_loc_hardware_breakpoint)
2663 warning (_("hardware breakpoint %d not supported in overlay!"),
2664 bl->owner->number);
2665 else
2666 {
2667 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2668 bl->section);
2669 /* Set a software (trap) breakpoint at the LMA. */
2670 bl->overlay_target_info = bl->target_info;
2671 bl->overlay_target_info.reqstd_address = addr;
2672
2673 /* No overlay handling: just set the breakpoint. */
2674 try
2675 {
2676 int val;
2677
2678 bl->overlay_target_info.kind
2679 = breakpoint_kind (bl, &addr);
2680 bl->overlay_target_info.placed_address = addr;
2681 val = target_insert_breakpoint (bl->gdbarch,
2682 &bl->overlay_target_info);
2683 if (val)
2684 bp_excpt
2685 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2686 }
2687 catch (gdb_exception &e)
2688 {
2689 bp_excpt = std::move (e);
2690 }
2691
2692 if (bp_excpt.reason != 0)
2693 fprintf_unfiltered (tmp_error_stream,
2694 "Overlay breakpoint %d "
2695 "failed: in ROM?\n",
2696 bl->owner->number);
2697 }
2698 }
2699 /* Shall we set a breakpoint at the VMA? */
2700 if (section_is_mapped (bl->section))
2701 {
2702 /* Yes. This overlay section is mapped into memory. */
2703 try
2704 {
2705 int val;
2706
2707 val = bl->owner->ops->insert_location (bl);
2708 if (val)
2709 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2710 }
2711 catch (gdb_exception &e)
2712 {
2713 bp_excpt = std::move (e);
2714 }
2715 }
2716 else
2717 {
2718 /* No. This breakpoint will not be inserted.
2719 No error, but do not mark the bp as 'inserted'. */
2720 return 0;
2721 }
2722 }
2723
2724 if (bp_excpt.reason != 0)
2725 {
2726 /* Can't set the breakpoint. */
2727
2728 /* In some cases, we might not be able to insert a
2729 breakpoint in a shared library that has already been
2730 removed, but we have not yet processed the shlib unload
2731 event. Unfortunately, some targets that implement
2732 breakpoint insertion themselves can't tell why the
2733 breakpoint insertion failed (e.g., the remote target
2734 doesn't define error codes), so we must treat generic
2735 errors as memory errors. */
2736 if (bp_excpt.reason == RETURN_ERROR
2737 && (bp_excpt.error == GENERIC_ERROR
2738 || bp_excpt.error == MEMORY_ERROR)
2739 && bl->loc_type == bp_loc_software_breakpoint
2740 && (solib_name_from_address (bl->pspace, bl->address)
2741 || shared_objfile_contains_address_p (bl->pspace,
2742 bl->address)))
2743 {
2744 /* See also: disable_breakpoints_in_shlibs. */
2745 bl->shlib_disabled = 1;
2746 gdb::observers::breakpoint_modified.notify (bl->owner);
2747 if (!*disabled_breaks)
2748 {
2749 fprintf_unfiltered (tmp_error_stream,
2750 "Cannot insert breakpoint %d.\n",
2751 bl->owner->number);
2752 fprintf_unfiltered (tmp_error_stream,
2753 "Temporarily disabling shared "
2754 "library breakpoints:\n");
2755 }
2756 *disabled_breaks = 1;
2757 fprintf_unfiltered (tmp_error_stream,
2758 "breakpoint #%d\n", bl->owner->number);
2759 return 0;
2760 }
2761 else
2762 {
2763 if (bl->loc_type == bp_loc_hardware_breakpoint)
2764 {
2765 *hw_breakpoint_error = 1;
2766 *hw_bp_error_explained_already = bp_excpt.message != NULL;
2767 fprintf_unfiltered (tmp_error_stream,
2768 "Cannot insert hardware breakpoint %d%s",
2769 bl->owner->number,
2770 bp_excpt.message ? ":" : ".\n");
2771 if (bp_excpt.message != NULL)
2772 fprintf_unfiltered (tmp_error_stream, "%s.\n",
2773 bp_excpt.what ());
2774 }
2775 else
2776 {
2777 if (bp_excpt.message == NULL)
2778 {
2779 std::string message
2780 = memory_error_message (TARGET_XFER_E_IO,
2781 bl->gdbarch, bl->address);
2782
2783 fprintf_unfiltered (tmp_error_stream,
2784 "Cannot insert breakpoint %d.\n"
2785 "%s\n",
2786 bl->owner->number, message.c_str ());
2787 }
2788 else
2789 {
2790 fprintf_unfiltered (tmp_error_stream,
2791 "Cannot insert breakpoint %d: %s\n",
2792 bl->owner->number,
2793 bp_excpt.what ());
2794 }
2795 }
2796 return 1;
2797
2798 }
2799 }
2800 else
2801 bl->inserted = 1;
2802
2803 return 0;
2804 }
2805
2806 else if (bl->loc_type == bp_loc_hardware_watchpoint
2807 /* NOTE drow/2003-09-08: This state only exists for removing
2808 watchpoints. It's not clear that it's necessary... */
2809 && bl->owner->disposition != disp_del_at_next_stop)
2810 {
2811 int val;
2812
2813 gdb_assert (bl->owner->ops != NULL
2814 && bl->owner->ops->insert_location != NULL);
2815
2816 val = bl->owner->ops->insert_location (bl);
2817
2818 /* If trying to set a read-watchpoint, and it turns out it's not
2819 supported, try emulating one with an access watchpoint. */
2820 if (val == 1 && bl->watchpoint_type == hw_read)
2821 {
2822 struct bp_location *loc, **loc_temp;
2823
2824 /* But don't try to insert it, if there's already another
2825 hw_access location that would be considered a duplicate
2826 of this one. */
2827 ALL_BP_LOCATIONS (loc, loc_temp)
2828 if (loc != bl
2829 && loc->watchpoint_type == hw_access
2830 && watchpoint_locations_match (bl, loc))
2831 {
2832 bl->duplicate = 1;
2833 bl->inserted = 1;
2834 bl->target_info = loc->target_info;
2835 bl->watchpoint_type = hw_access;
2836 val = 0;
2837 break;
2838 }
2839
2840 if (val == 1)
2841 {
2842 bl->watchpoint_type = hw_access;
2843 val = bl->owner->ops->insert_location (bl);
2844
2845 if (val)
2846 /* Back to the original value. */
2847 bl->watchpoint_type = hw_read;
2848 }
2849 }
2850
2851 bl->inserted = (val == 0);
2852 }
2853
2854 else if (bl->owner->type == bp_catchpoint)
2855 {
2856 int val;
2857
2858 gdb_assert (bl->owner->ops != NULL
2859 && bl->owner->ops->insert_location != NULL);
2860
2861 val = bl->owner->ops->insert_location (bl);
2862 if (val)
2863 {
2864 bl->owner->enable_state = bp_disabled;
2865
2866 if (val == 1)
2867 warning (_("\
2868 Error inserting catchpoint %d: Your system does not support this type\n\
2869 of catchpoint."), bl->owner->number);
2870 else
2871 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2872 }
2873
2874 bl->inserted = (val == 0);
2875
2876 /* We've already printed an error message if there was a problem
2877 inserting this catchpoint, and we've disabled the catchpoint,
2878 so just return success. */
2879 return 0;
2880 }
2881
2882 return 0;
2883 }
2884
2885 /* This function is called when program space PSPACE is about to be
2886 deleted. It takes care of updating breakpoints to not reference
2887 PSPACE anymore. */
2888
2889 void
2890 breakpoint_program_space_exit (struct program_space *pspace)
2891 {
2892 struct breakpoint *b, *b_temp;
2893 struct bp_location *loc, **loc_temp;
2894
2895 /* Remove any breakpoint that was set through this program space. */
2896 ALL_BREAKPOINTS_SAFE (b, b_temp)
2897 {
2898 if (b->pspace == pspace)
2899 delete_breakpoint (b);
2900 }
2901
2902 /* Breakpoints set through other program spaces could have locations
2903 bound to PSPACE as well. Remove those. */
2904 ALL_BP_LOCATIONS (loc, loc_temp)
2905 {
2906 struct bp_location *tmp;
2907
2908 if (loc->pspace == pspace)
2909 {
2910 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2911 if (loc->owner->loc == loc)
2912 loc->owner->loc = loc->next;
2913 else
2914 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2915 if (tmp->next == loc)
2916 {
2917 tmp->next = loc->next;
2918 break;
2919 }
2920 }
2921 }
2922
2923 /* Now update the global location list to permanently delete the
2924 removed locations above. */
2925 update_global_location_list (UGLL_DONT_INSERT);
2926 }
2927
2928 /* Make sure all breakpoints are inserted in inferior.
2929 Throws exception on any error.
2930 A breakpoint that is already inserted won't be inserted
2931 again, so calling this function twice is safe. */
2932 void
2933 insert_breakpoints (void)
2934 {
2935 for (breakpoint *bpt : all_breakpoints ())
2936 if (is_hardware_watchpoint (bpt))
2937 {
2938 struct watchpoint *w = (struct watchpoint *) bpt;
2939
2940 update_watchpoint (w, 0 /* don't reparse. */);
2941 }
2942
2943 /* Updating watchpoints creates new locations, so update the global
2944 location list. Explicitly tell ugll to insert locations and
2945 ignore breakpoints_always_inserted_mode. Also,
2946 update_global_location_list tries to "upgrade" software
2947 breakpoints to hardware breakpoints to handle "set breakpoint
2948 auto-hw", so we need to call it even if we don't have new
2949 locations. */
2950 update_global_location_list (UGLL_INSERT);
2951 }
2952
2953 /* Invoke CALLBACK for each of bp_location. */
2954
2955 void
2956 iterate_over_bp_locations (gdb::function_view<void (bp_location *)> callback)
2957 {
2958 struct bp_location *loc, **loc_tmp;
2959
2960 ALL_BP_LOCATIONS (loc, loc_tmp)
2961 {
2962 callback (loc);
2963 }
2964 }
2965
2966 /* This is used when we need to synch breakpoint conditions between GDB and the
2967 target. It is the case with deleting and disabling of breakpoints when using
2968 always-inserted mode. */
2969
2970 static void
2971 update_inserted_breakpoint_locations (void)
2972 {
2973 struct bp_location *bl, **blp_tmp;
2974 int error_flag = 0;
2975 int val = 0;
2976 int disabled_breaks = 0;
2977 int hw_breakpoint_error = 0;
2978 int hw_bp_details_reported = 0;
2979
2980 string_file tmp_error_stream;
2981
2982 /* Explicitly mark the warning -- this will only be printed if
2983 there was an error. */
2984 tmp_error_stream.puts ("Warning:\n");
2985
2986 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2987
2988 ALL_BP_LOCATIONS (bl, blp_tmp)
2989 {
2990 /* We only want to update software breakpoints and hardware
2991 breakpoints. */
2992 if (!is_breakpoint (bl->owner))
2993 continue;
2994
2995 /* We only want to update locations that are already inserted
2996 and need updating. This is to avoid unwanted insertion during
2997 deletion of breakpoints. */
2998 if (!bl->inserted || !bl->needs_update)
2999 continue;
3000
3001 switch_to_program_space_and_thread (bl->pspace);
3002
3003 /* For targets that support global breakpoints, there's no need
3004 to select an inferior to insert breakpoint to. In fact, even
3005 if we aren't attached to any process yet, we should still
3006 insert breakpoints. */
3007 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3008 && (inferior_ptid == null_ptid || !target_has_execution ()))
3009 continue;
3010
3011 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3012 &hw_breakpoint_error, &hw_bp_details_reported);
3013 if (val)
3014 error_flag = val;
3015 }
3016
3017 if (error_flag)
3018 {
3019 target_terminal::ours_for_output ();
3020 error_stream (tmp_error_stream);
3021 }
3022 }
3023
3024 /* Used when starting or continuing the program. */
3025
3026 static void
3027 insert_breakpoint_locations (void)
3028 {
3029 struct bp_location *bl, **blp_tmp;
3030 int error_flag = 0;
3031 int val = 0;
3032 int disabled_breaks = 0;
3033 int hw_breakpoint_error = 0;
3034 int hw_bp_error_explained_already = 0;
3035
3036 string_file tmp_error_stream;
3037
3038 /* Explicitly mark the warning -- this will only be printed if
3039 there was an error. */
3040 tmp_error_stream.puts ("Warning:\n");
3041
3042 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3043
3044 ALL_BP_LOCATIONS (bl, blp_tmp)
3045 {
3046 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3047 continue;
3048
3049 /* There is no point inserting thread-specific breakpoints if
3050 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3051 has BL->OWNER always non-NULL. */
3052 if (bl->owner->thread != -1
3053 && !valid_global_thread_id (bl->owner->thread))
3054 continue;
3055
3056 switch_to_program_space_and_thread (bl->pspace);
3057
3058 /* For targets that support global breakpoints, there's no need
3059 to select an inferior to insert breakpoint to. In fact, even
3060 if we aren't attached to any process yet, we should still
3061 insert breakpoints. */
3062 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3063 && (inferior_ptid == null_ptid || !target_has_execution ()))
3064 continue;
3065
3066 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3067 &hw_breakpoint_error, &hw_bp_error_explained_already);
3068 if (val)
3069 error_flag = val;
3070 }
3071
3072 /* If we failed to insert all locations of a watchpoint, remove
3073 them, as half-inserted watchpoint is of limited use. */
3074 for (breakpoint *bpt : all_breakpoints ())
3075 {
3076 int some_failed = 0;
3077 struct bp_location *loc;
3078
3079 if (!is_hardware_watchpoint (bpt))
3080 continue;
3081
3082 if (!breakpoint_enabled (bpt))
3083 continue;
3084
3085 if (bpt->disposition == disp_del_at_next_stop)
3086 continue;
3087
3088 for (loc = bpt->loc; loc; loc = loc->next)
3089 if (!loc->inserted && should_be_inserted (loc))
3090 {
3091 some_failed = 1;
3092 break;
3093 }
3094 if (some_failed)
3095 {
3096 for (loc = bpt->loc; loc; loc = loc->next)
3097 if (loc->inserted)
3098 remove_breakpoint (loc);
3099
3100 hw_breakpoint_error = 1;
3101 tmp_error_stream.printf ("Could not insert "
3102 "hardware watchpoint %d.\n",
3103 bpt->number);
3104 error_flag = -1;
3105 }
3106 }
3107
3108 if (error_flag)
3109 {
3110 /* If a hardware breakpoint or watchpoint was inserted, add a
3111 message about possibly exhausted resources. */
3112 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3113 {
3114 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3115 You may have requested too many hardware breakpoints/watchpoints.\n");
3116 }
3117 target_terminal::ours_for_output ();
3118 error_stream (tmp_error_stream);
3119 }
3120 }
3121
3122 /* Used when the program stops.
3123 Returns zero if successful, or non-zero if there was a problem
3124 removing a breakpoint location. */
3125
3126 int
3127 remove_breakpoints (void)
3128 {
3129 struct bp_location *bl, **blp_tmp;
3130 int val = 0;
3131
3132 ALL_BP_LOCATIONS (bl, blp_tmp)
3133 {
3134 if (bl->inserted && !is_tracepoint (bl->owner))
3135 val |= remove_breakpoint (bl);
3136 }
3137 return val;
3138 }
3139
3140 /* When a thread exits, remove breakpoints that are related to
3141 that thread. */
3142
3143 static void
3144 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3145 {
3146 struct breakpoint *b, *b_tmp;
3147
3148 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3149 {
3150 if (b->thread == tp->global_num && user_breakpoint_p (b))
3151 {
3152 b->disposition = disp_del_at_next_stop;
3153
3154 printf_filtered (_("\
3155 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3156 b->number, print_thread_id (tp));
3157
3158 /* Hide it from the user. */
3159 b->number = 0;
3160 }
3161 }
3162 }
3163
3164 /* See breakpoint.h. */
3165
3166 void
3167 remove_breakpoints_inf (inferior *inf)
3168 {
3169 struct bp_location *bl, **blp_tmp;
3170 int val;
3171
3172 ALL_BP_LOCATIONS (bl, blp_tmp)
3173 {
3174 if (bl->pspace != inf->pspace)
3175 continue;
3176
3177 if (bl->inserted && !bl->target_info.persist)
3178 {
3179 val = remove_breakpoint (bl);
3180 if (val != 0)
3181 return;
3182 }
3183 }
3184 }
3185
3186 static int internal_breakpoint_number = -1;
3187
3188 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3189 If INTERNAL is non-zero, the breakpoint number will be populated
3190 from internal_breakpoint_number and that variable decremented.
3191 Otherwise the breakpoint number will be populated from
3192 breakpoint_count and that value incremented. Internal breakpoints
3193 do not set the internal var bpnum. */
3194 static void
3195 set_breakpoint_number (int internal, struct breakpoint *b)
3196 {
3197 if (internal)
3198 b->number = internal_breakpoint_number--;
3199 else
3200 {
3201 set_breakpoint_count (breakpoint_count + 1);
3202 b->number = breakpoint_count;
3203 }
3204 }
3205
3206 static struct breakpoint *
3207 create_internal_breakpoint (struct gdbarch *gdbarch,
3208 CORE_ADDR address, enum bptype type,
3209 const struct breakpoint_ops *ops)
3210 {
3211 symtab_and_line sal;
3212 sal.pc = address;
3213 sal.section = find_pc_overlay (sal.pc);
3214 sal.pspace = current_program_space;
3215
3216 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
3217 b->number = internal_breakpoint_number--;
3218 b->disposition = disp_donttouch;
3219
3220 return b;
3221 }
3222
3223 static const char *const longjmp_names[] =
3224 {
3225 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3226 };
3227 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3228
3229 /* Per-objfile data private to breakpoint.c. */
3230 struct breakpoint_objfile_data
3231 {
3232 /* Minimal symbol for "_ovly_debug_event" (if any). */
3233 struct bound_minimal_symbol overlay_msym {};
3234
3235 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3236 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
3237
3238 /* True if we have looked for longjmp probes. */
3239 int longjmp_searched = 0;
3240
3241 /* SystemTap probe points for longjmp (if any). These are non-owning
3242 references. */
3243 std::vector<probe *> longjmp_probes;
3244
3245 /* Minimal symbol for "std::terminate()" (if any). */
3246 struct bound_minimal_symbol terminate_msym {};
3247
3248 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3249 struct bound_minimal_symbol exception_msym {};
3250
3251 /* True if we have looked for exception probes. */
3252 int exception_searched = 0;
3253
3254 /* SystemTap probe points for unwinding (if any). These are non-owning
3255 references. */
3256 std::vector<probe *> exception_probes;
3257 };
3258
3259 static const struct objfile_key<breakpoint_objfile_data>
3260 breakpoint_objfile_key;
3261
3262 /* Minimal symbol not found sentinel. */
3263 static struct minimal_symbol msym_not_found;
3264
3265 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3266
3267 static int
3268 msym_not_found_p (const struct minimal_symbol *msym)
3269 {
3270 return msym == &msym_not_found;
3271 }
3272
3273 /* Return per-objfile data needed by breakpoint.c.
3274 Allocate the data if necessary. */
3275
3276 static struct breakpoint_objfile_data *
3277 get_breakpoint_objfile_data (struct objfile *objfile)
3278 {
3279 struct breakpoint_objfile_data *bp_objfile_data;
3280
3281 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3282 if (bp_objfile_data == NULL)
3283 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3284 return bp_objfile_data;
3285 }
3286
3287 static void
3288 create_overlay_event_breakpoint (void)
3289 {
3290 const char *const func_name = "_ovly_debug_event";
3291
3292 for (objfile *objfile : current_program_space->objfiles ())
3293 {
3294 struct breakpoint *b;
3295 struct breakpoint_objfile_data *bp_objfile_data;
3296 CORE_ADDR addr;
3297 struct explicit_location explicit_loc;
3298
3299 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3300
3301 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3302 continue;
3303
3304 if (bp_objfile_data->overlay_msym.minsym == NULL)
3305 {
3306 struct bound_minimal_symbol m;
3307
3308 m = lookup_minimal_symbol_text (func_name, objfile);
3309 if (m.minsym == NULL)
3310 {
3311 /* Avoid future lookups in this objfile. */
3312 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3313 continue;
3314 }
3315 bp_objfile_data->overlay_msym = m;
3316 }
3317
3318 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3319 b = create_internal_breakpoint (objfile->arch (), addr,
3320 bp_overlay_event,
3321 &internal_breakpoint_ops);
3322 initialize_explicit_location (&explicit_loc);
3323 explicit_loc.function_name = ASTRDUP (func_name);
3324 b->location = new_explicit_location (&explicit_loc);
3325
3326 if (overlay_debugging == ovly_auto)
3327 {
3328 b->enable_state = bp_enabled;
3329 overlay_events_enabled = 1;
3330 }
3331 else
3332 {
3333 b->enable_state = bp_disabled;
3334 overlay_events_enabled = 0;
3335 }
3336 }
3337 }
3338
3339 /* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3340 true if a breakpoint was installed. */
3341
3342 static bool
3343 create_longjmp_master_breakpoint_probe (objfile *objfile)
3344 {
3345 struct gdbarch *gdbarch = objfile->arch ();
3346 struct breakpoint_objfile_data *bp_objfile_data
3347 = get_breakpoint_objfile_data (objfile);
3348
3349 if (!bp_objfile_data->longjmp_searched)
3350 {
3351 std::vector<probe *> ret
3352 = find_probes_in_objfile (objfile, "libc", "longjmp");
3353
3354 if (!ret.empty ())
3355 {
3356 /* We are only interested in checking one element. */
3357 probe *p = ret[0];
3358
3359 if (!p->can_evaluate_arguments ())
3360 {
3361 /* We cannot use the probe interface here,
3362 because it does not know how to evaluate
3363 arguments. */
3364 ret.clear ();
3365 }
3366 }
3367 bp_objfile_data->longjmp_probes = ret;
3368 bp_objfile_data->longjmp_searched = 1;
3369 }
3370
3371 if (bp_objfile_data->longjmp_probes.empty ())
3372 return false;
3373
3374 for (probe *p : bp_objfile_data->longjmp_probes)
3375 {
3376 struct breakpoint *b;
3377
3378 b = create_internal_breakpoint (gdbarch,
3379 p->get_relocated_address (objfile),
3380 bp_longjmp_master,
3381 &internal_breakpoint_ops);
3382 b->location = new_probe_location ("-probe-stap libc:longjmp");
3383 b->enable_state = bp_disabled;
3384 }
3385
3386 return true;
3387 }
3388
3389 /* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3390 Return true if at least one breakpoint was installed. */
3391
3392 static bool
3393 create_longjmp_master_breakpoint_names (objfile *objfile)
3394 {
3395 struct gdbarch *gdbarch = objfile->arch ();
3396 if (!gdbarch_get_longjmp_target_p (gdbarch))
3397 return false;
3398
3399 struct breakpoint_objfile_data *bp_objfile_data
3400 = get_breakpoint_objfile_data (objfile);
3401 unsigned int installed_bp = 0;
3402
3403 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3404 {
3405 struct breakpoint *b;
3406 const char *func_name;
3407 CORE_ADDR addr;
3408 struct explicit_location explicit_loc;
3409
3410 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3411 continue;
3412
3413 func_name = longjmp_names[i];
3414 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3415 {
3416 struct bound_minimal_symbol m;
3417
3418 m = lookup_minimal_symbol_text (func_name, objfile);
3419 if (m.minsym == NULL)
3420 {
3421 /* Prevent future lookups in this objfile. */
3422 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3423 continue;
3424 }
3425 bp_objfile_data->longjmp_msym[i] = m;
3426 }
3427
3428 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3429 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3430 &internal_breakpoint_ops);
3431 initialize_explicit_location (&explicit_loc);
3432 explicit_loc.function_name = ASTRDUP (func_name);
3433 b->location = new_explicit_location (&explicit_loc);
3434 b->enable_state = bp_disabled;
3435 installed_bp++;
3436 }
3437
3438 return installed_bp > 0;
3439 }
3440
3441 /* Create a master longjmp breakpoint. */
3442
3443 static void
3444 create_longjmp_master_breakpoint (void)
3445 {
3446 scoped_restore_current_program_space restore_pspace;
3447
3448 for (struct program_space *pspace : program_spaces)
3449 {
3450 set_current_program_space (pspace);
3451
3452 for (objfile *obj : current_program_space->objfiles ())
3453 {
3454 /* Skip separate debug object, it's handled in the loop below. */
3455 if (obj->separate_debug_objfile_backlink != nullptr)
3456 continue;
3457
3458 /* Try a probe kind breakpoint on main objfile. */
3459 if (create_longjmp_master_breakpoint_probe (obj))
3460 continue;
3461
3462 /* Try longjmp_names kind breakpoints on main and separate_debug
3463 objfiles. */
3464 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3465 if (create_longjmp_master_breakpoint_names (debug_objfile))
3466 break;
3467 }
3468 }
3469 }
3470
3471 /* Create a master std::terminate breakpoint. */
3472 static void
3473 create_std_terminate_master_breakpoint (void)
3474 {
3475 const char *const func_name = "std::terminate()";
3476
3477 scoped_restore_current_program_space restore_pspace;
3478
3479 for (struct program_space *pspace : program_spaces)
3480 {
3481 CORE_ADDR addr;
3482
3483 set_current_program_space (pspace);
3484
3485 for (objfile *objfile : current_program_space->objfiles ())
3486 {
3487 struct breakpoint *b;
3488 struct breakpoint_objfile_data *bp_objfile_data;
3489 struct explicit_location explicit_loc;
3490
3491 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3492
3493 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3494 continue;
3495
3496 if (bp_objfile_data->terminate_msym.minsym == NULL)
3497 {
3498 struct bound_minimal_symbol m;
3499
3500 m = lookup_minimal_symbol (func_name, NULL, objfile);
3501 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3502 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3503 {
3504 /* Prevent future lookups in this objfile. */
3505 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3506 continue;
3507 }
3508 bp_objfile_data->terminate_msym = m;
3509 }
3510
3511 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3512 b = create_internal_breakpoint (objfile->arch (), addr,
3513 bp_std_terminate_master,
3514 &internal_breakpoint_ops);
3515 initialize_explicit_location (&explicit_loc);
3516 explicit_loc.function_name = ASTRDUP (func_name);
3517 b->location = new_explicit_location (&explicit_loc);
3518 b->enable_state = bp_disabled;
3519 }
3520 }
3521 }
3522
3523 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3524 probe. Return true if a breakpoint was installed. */
3525
3526 static bool
3527 create_exception_master_breakpoint_probe (objfile *objfile)
3528 {
3529 struct breakpoint *b;
3530 struct gdbarch *gdbarch;
3531 struct breakpoint_objfile_data *bp_objfile_data;
3532
3533 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3534
3535 /* We prefer the SystemTap probe point if it exists. */
3536 if (!bp_objfile_data->exception_searched)
3537 {
3538 std::vector<probe *> ret
3539 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3540
3541 if (!ret.empty ())
3542 {
3543 /* We are only interested in checking one element. */
3544 probe *p = ret[0];
3545
3546 if (!p->can_evaluate_arguments ())
3547 {
3548 /* We cannot use the probe interface here, because it does
3549 not know how to evaluate arguments. */
3550 ret.clear ();
3551 }
3552 }
3553 bp_objfile_data->exception_probes = ret;
3554 bp_objfile_data->exception_searched = 1;
3555 }
3556
3557 if (bp_objfile_data->exception_probes.empty ())
3558 return false;
3559
3560 gdbarch = objfile->arch ();
3561
3562 for (probe *p : bp_objfile_data->exception_probes)
3563 {
3564 b = create_internal_breakpoint (gdbarch,
3565 p->get_relocated_address (objfile),
3566 bp_exception_master,
3567 &internal_breakpoint_ops);
3568 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3569 b->enable_state = bp_disabled;
3570 }
3571
3572 return true;
3573 }
3574
3575 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3576 _Unwind_DebugHook. Return true if a breakpoint was installed. */
3577
3578 static bool
3579 create_exception_master_breakpoint_hook (objfile *objfile)
3580 {
3581 const char *const func_name = "_Unwind_DebugHook";
3582 struct breakpoint *b;
3583 struct gdbarch *gdbarch;
3584 struct breakpoint_objfile_data *bp_objfile_data;
3585 CORE_ADDR addr;
3586 struct explicit_location explicit_loc;
3587
3588 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3589
3590 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3591 return false;
3592
3593 gdbarch = objfile->arch ();
3594
3595 if (bp_objfile_data->exception_msym.minsym == NULL)
3596 {
3597 struct bound_minimal_symbol debug_hook;
3598
3599 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3600 if (debug_hook.minsym == NULL)
3601 {
3602 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3603 return false;
3604 }
3605
3606 bp_objfile_data->exception_msym = debug_hook;
3607 }
3608
3609 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3610 addr = gdbarch_convert_from_func_ptr_addr
3611 (gdbarch, addr, current_inferior ()->top_target ());
3612 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3613 &internal_breakpoint_ops);
3614 initialize_explicit_location (&explicit_loc);
3615 explicit_loc.function_name = ASTRDUP (func_name);
3616 b->location = new_explicit_location (&explicit_loc);
3617 b->enable_state = bp_disabled;
3618
3619 return true;
3620 }
3621
3622 /* Install a master breakpoint on the unwinder's debug hook. */
3623
3624 static void
3625 create_exception_master_breakpoint (void)
3626 {
3627 for (objfile *obj : current_program_space->objfiles ())
3628 {
3629 /* Skip separate debug object. */
3630 if (obj->separate_debug_objfile_backlink)
3631 continue;
3632
3633 /* Try a probe kind breakpoint. */
3634 if (create_exception_master_breakpoint_probe (obj))
3635 continue;
3636
3637 /* Iterate over main and separate debug objects and try an
3638 _Unwind_DebugHook kind breakpoint. */
3639 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3640 if (create_exception_master_breakpoint_hook (debug_objfile))
3641 break;
3642 }
3643 }
3644
3645 /* Does B have a location spec? */
3646
3647 static int
3648 breakpoint_event_location_empty_p (const struct breakpoint *b)
3649 {
3650 return b->location != NULL && event_location_empty_p (b->location.get ());
3651 }
3652
3653 void
3654 update_breakpoints_after_exec (void)
3655 {
3656 struct breakpoint *b, *b_tmp;
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 ALL_BREAKPOINTS_SAFE (b, b_tmp)
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 breakpoint *b, *b_tmp;
4006 struct program_space *pspace = current_program_space;
4007
4008 /* If breakpoint locations are shared across processes, then there's
4009 nothing to do. */
4010 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4011 return;
4012
4013 mark_breakpoints_out ();
4014
4015 ALL_BREAKPOINTS_SAFE (b, b_tmp)
4016 {
4017 if (b->loc && b->loc->pspace != pspace)
4018 continue;
4019
4020 switch (b->type)
4021 {
4022 case bp_call_dummy:
4023 case bp_longjmp_call_dummy:
4024
4025 /* If the call dummy breakpoint is at the entry point it will
4026 cause problems when the inferior is rerun, so we better get
4027 rid of it. */
4028
4029 case bp_watchpoint_scope:
4030
4031 /* Also get rid of scope breakpoints. */
4032
4033 case bp_shlib_event:
4034
4035 /* Also remove solib event breakpoints. Their addresses may
4036 have changed since the last time we ran the program.
4037 Actually we may now be debugging against different target;
4038 and so the solib backend that installed this breakpoint may
4039 not be used in by the target. E.g.,
4040
4041 (gdb) file prog-linux
4042 (gdb) run # native linux target
4043 ...
4044 (gdb) kill
4045 (gdb) file prog-win.exe
4046 (gdb) tar rem :9999 # remote Windows gdbserver.
4047 */
4048
4049 case bp_step_resume:
4050
4051 /* Also remove step-resume breakpoints. */
4052
4053 case bp_single_step:
4054
4055 /* Also remove single-step breakpoints. */
4056
4057 delete_breakpoint (b);
4058 break;
4059
4060 case bp_watchpoint:
4061 case bp_hardware_watchpoint:
4062 case bp_read_watchpoint:
4063 case bp_access_watchpoint:
4064 {
4065 struct watchpoint *w = (struct watchpoint *) b;
4066
4067 /* Likewise for watchpoints on local expressions. */
4068 if (w->exp_valid_block != NULL)
4069 delete_breakpoint (b);
4070 else
4071 {
4072 /* Get rid of existing locations, which are no longer
4073 valid. New ones will be created in
4074 update_watchpoint, when the inferior is restarted.
4075 The next update_global_location_list call will
4076 garbage collect them. */
4077 b->loc = NULL;
4078
4079 if (context == inf_starting)
4080 {
4081 /* Reset val field to force reread of starting value in
4082 insert_breakpoints. */
4083 w->val.reset (nullptr);
4084 w->val_valid = false;
4085 }
4086 }
4087 }
4088 break;
4089 default:
4090 break;
4091 }
4092 }
4093
4094 /* Get rid of the moribund locations. */
4095 for (bp_location *bl : moribund_locations)
4096 decref_bp_location (&bl);
4097 moribund_locations.clear ();
4098 }
4099
4100 /* These functions concern about actual breakpoints inserted in the
4101 target --- to e.g. check if we need to do decr_pc adjustment or if
4102 we need to hop over the bkpt --- so we check for address space
4103 match, not program space. */
4104
4105 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4106 exists at PC. It returns ordinary_breakpoint_here if it's an
4107 ordinary breakpoint, or permanent_breakpoint_here if it's a
4108 permanent breakpoint.
4109 - When continuing from a location with an ordinary breakpoint, we
4110 actually single step once before calling insert_breakpoints.
4111 - When continuing from a location with a permanent breakpoint, we
4112 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4113 the target, to advance the PC past the breakpoint. */
4114
4115 enum breakpoint_here
4116 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4117 {
4118 struct bp_location *bl, **blp_tmp;
4119 int any_breakpoint_here = 0;
4120
4121 ALL_BP_LOCATIONS (bl, blp_tmp)
4122 {
4123 if (bl->loc_type != bp_loc_software_breakpoint
4124 && bl->loc_type != bp_loc_hardware_breakpoint)
4125 continue;
4126
4127 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4128 if ((breakpoint_enabled (bl->owner)
4129 || bl->permanent)
4130 && breakpoint_location_address_match (bl, aspace, pc))
4131 {
4132 if (overlay_debugging
4133 && section_is_overlay (bl->section)
4134 && !section_is_mapped (bl->section))
4135 continue; /* unmapped overlay -- can't be a match */
4136 else if (bl->permanent)
4137 return permanent_breakpoint_here;
4138 else
4139 any_breakpoint_here = 1;
4140 }
4141 }
4142
4143 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4144 }
4145
4146 /* See breakpoint.h. */
4147
4148 int
4149 breakpoint_in_range_p (const address_space *aspace,
4150 CORE_ADDR addr, ULONGEST len)
4151 {
4152 struct bp_location *bl, **blp_tmp;
4153
4154 ALL_BP_LOCATIONS (bl, blp_tmp)
4155 {
4156 if (bl->loc_type != bp_loc_software_breakpoint
4157 && bl->loc_type != bp_loc_hardware_breakpoint)
4158 continue;
4159
4160 if ((breakpoint_enabled (bl->owner)
4161 || bl->permanent)
4162 && breakpoint_location_address_range_overlap (bl, aspace,
4163 addr, len))
4164 {
4165 if (overlay_debugging
4166 && section_is_overlay (bl->section)
4167 && !section_is_mapped (bl->section))
4168 {
4169 /* Unmapped overlay -- can't be a match. */
4170 continue;
4171 }
4172
4173 return 1;
4174 }
4175 }
4176
4177 return 0;
4178 }
4179
4180 /* Return true if there's a moribund breakpoint at PC. */
4181
4182 int
4183 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4184 {
4185 for (bp_location *loc : moribund_locations)
4186 if (breakpoint_location_address_match (loc, aspace, pc))
4187 return 1;
4188
4189 return 0;
4190 }
4191
4192 /* Returns non-zero iff BL is inserted at PC, in address space
4193 ASPACE. */
4194
4195 static int
4196 bp_location_inserted_here_p (struct bp_location *bl,
4197 const address_space *aspace, CORE_ADDR pc)
4198 {
4199 if (bl->inserted
4200 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4201 aspace, pc))
4202 {
4203 if (overlay_debugging
4204 && section_is_overlay (bl->section)
4205 && !section_is_mapped (bl->section))
4206 return 0; /* unmapped overlay -- can't be a match */
4207 else
4208 return 1;
4209 }
4210 return 0;
4211 }
4212
4213 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4214
4215 int
4216 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4217 {
4218 struct bp_location **blp, **blp_tmp = NULL;
4219
4220 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4221 {
4222 struct bp_location *bl = *blp;
4223
4224 if (bl->loc_type != bp_loc_software_breakpoint
4225 && bl->loc_type != bp_loc_hardware_breakpoint)
4226 continue;
4227
4228 if (bp_location_inserted_here_p (bl, aspace, pc))
4229 return 1;
4230 }
4231 return 0;
4232 }
4233
4234 /* This function returns non-zero iff there is a software breakpoint
4235 inserted at PC. */
4236
4237 int
4238 software_breakpoint_inserted_here_p (const address_space *aspace,
4239 CORE_ADDR pc)
4240 {
4241 struct bp_location **blp, **blp_tmp = NULL;
4242
4243 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4244 {
4245 struct bp_location *bl = *blp;
4246
4247 if (bl->loc_type != bp_loc_software_breakpoint)
4248 continue;
4249
4250 if (bp_location_inserted_here_p (bl, aspace, pc))
4251 return 1;
4252 }
4253
4254 return 0;
4255 }
4256
4257 /* See breakpoint.h. */
4258
4259 int
4260 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4261 CORE_ADDR pc)
4262 {
4263 struct bp_location **blp, **blp_tmp = NULL;
4264
4265 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4266 {
4267 struct bp_location *bl = *blp;
4268
4269 if (bl->loc_type != bp_loc_hardware_breakpoint)
4270 continue;
4271
4272 if (bp_location_inserted_here_p (bl, aspace, pc))
4273 return 1;
4274 }
4275
4276 return 0;
4277 }
4278
4279 int
4280 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4281 CORE_ADDR addr, ULONGEST len)
4282 {
4283 for (breakpoint *bpt : all_breakpoints ())
4284 {
4285 struct bp_location *loc;
4286
4287 if (bpt->type != bp_hardware_watchpoint
4288 && bpt->type != bp_access_watchpoint)
4289 continue;
4290
4291 if (!breakpoint_enabled (bpt))
4292 continue;
4293
4294 for (loc = bpt->loc; loc; loc = loc->next)
4295 if (loc->pspace->aspace == aspace && loc->inserted)
4296 {
4297 CORE_ADDR l, h;
4298
4299 /* Check for intersection. */
4300 l = std::max<CORE_ADDR> (loc->address, addr);
4301 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4302 if (l < h)
4303 return 1;
4304 }
4305 }
4306 return 0;
4307 }
4308
4309 /* See breakpoint.h. */
4310
4311 bool
4312 is_catchpoint (struct breakpoint *b)
4313 {
4314 return (b->type == bp_catchpoint);
4315 }
4316
4317 /* Clear a bpstat so that it says we are not at any breakpoint.
4318 Also free any storage that is part of a bpstat. */
4319
4320 void
4321 bpstat_clear (bpstat *bsp)
4322 {
4323 bpstat p;
4324 bpstat q;
4325
4326 if (bsp == 0)
4327 return;
4328 p = *bsp;
4329 while (p != NULL)
4330 {
4331 q = p->next;
4332 delete p;
4333 p = q;
4334 }
4335 *bsp = NULL;
4336 }
4337
4338 bpstats::bpstats (const bpstats &other)
4339 : next (NULL),
4340 bp_location_at (other.bp_location_at),
4341 breakpoint_at (other.breakpoint_at),
4342 commands (other.commands),
4343 print (other.print),
4344 stop (other.stop),
4345 print_it (other.print_it)
4346 {
4347 if (other.old_val != NULL)
4348 old_val = release_value (value_copy (other.old_val.get ()));
4349 }
4350
4351 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4352 is part of the bpstat is copied as well. */
4353
4354 bpstat
4355 bpstat_copy (bpstat bs)
4356 {
4357 bpstat p = NULL;
4358 bpstat tmp;
4359 bpstat retval = NULL;
4360
4361 if (bs == NULL)
4362 return bs;
4363
4364 for (; bs != NULL; bs = bs->next)
4365 {
4366 tmp = new bpstats (*bs);
4367
4368 if (p == NULL)
4369 /* This is the first thing in the chain. */
4370 retval = tmp;
4371 else
4372 p->next = tmp;
4373 p = tmp;
4374 }
4375 p->next = NULL;
4376 return retval;
4377 }
4378
4379 /* Find the bpstat associated with this breakpoint. */
4380
4381 bpstat
4382 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4383 {
4384 if (bsp == NULL)
4385 return NULL;
4386
4387 for (; bsp != NULL; bsp = bsp->next)
4388 {
4389 if (bsp->breakpoint_at == breakpoint)
4390 return bsp;
4391 }
4392 return NULL;
4393 }
4394
4395 /* See breakpoint.h. */
4396
4397 bool
4398 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4399 {
4400 for (; bsp != NULL; bsp = bsp->next)
4401 {
4402 if (bsp->breakpoint_at == NULL)
4403 {
4404 /* A moribund location can never explain a signal other than
4405 GDB_SIGNAL_TRAP. */
4406 if (sig == GDB_SIGNAL_TRAP)
4407 return true;
4408 }
4409 else
4410 {
4411 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4412 sig))
4413 return true;
4414 }
4415 }
4416
4417 return false;
4418 }
4419
4420 /* Put in *NUM the breakpoint number of the first breakpoint we are
4421 stopped at. *BSP upon return is a bpstat which points to the
4422 remaining breakpoints stopped at (but which is not guaranteed to be
4423 good for anything but further calls to bpstat_num).
4424
4425 Return 0 if passed a bpstat which does not indicate any breakpoints.
4426 Return -1 if stopped at a breakpoint that has been deleted since
4427 we set it.
4428 Return 1 otherwise. */
4429
4430 int
4431 bpstat_num (bpstat *bsp, int *num)
4432 {
4433 struct breakpoint *b;
4434
4435 if ((*bsp) == NULL)
4436 return 0; /* No more breakpoint values */
4437
4438 /* We assume we'll never have several bpstats that correspond to a
4439 single breakpoint -- otherwise, this function might return the
4440 same number more than once and this will look ugly. */
4441 b = (*bsp)->breakpoint_at;
4442 *bsp = (*bsp)->next;
4443 if (b == NULL)
4444 return -1; /* breakpoint that's been deleted since */
4445
4446 *num = b->number; /* We have its number */
4447 return 1;
4448 }
4449
4450 /* See breakpoint.h. */
4451
4452 void
4453 bpstat_clear_actions (void)
4454 {
4455 bpstat bs;
4456
4457 if (inferior_ptid == null_ptid)
4458 return;
4459
4460 thread_info *tp = inferior_thread ();
4461 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4462 {
4463 bs->commands = NULL;
4464 bs->old_val.reset (nullptr);
4465 }
4466 }
4467
4468 /* Called when a command is about to proceed the inferior. */
4469
4470 static void
4471 breakpoint_about_to_proceed (void)
4472 {
4473 if (inferior_ptid != null_ptid)
4474 {
4475 struct thread_info *tp = inferior_thread ();
4476
4477 /* Allow inferior function calls in breakpoint commands to not
4478 interrupt the command list. When the call finishes
4479 successfully, the inferior will be standing at the same
4480 breakpoint as if nothing happened. */
4481 if (tp->control.in_infcall)
4482 return;
4483 }
4484
4485 breakpoint_proceeded = 1;
4486 }
4487
4488 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4489 or its equivalent. */
4490
4491 static int
4492 command_line_is_silent (struct command_line *cmd)
4493 {
4494 return cmd && (strcmp ("silent", cmd->line) == 0);
4495 }
4496
4497 /* Execute all the commands associated with all the breakpoints at
4498 this location. Any of these commands could cause the process to
4499 proceed beyond this point, etc. We look out for such changes by
4500 checking the global "breakpoint_proceeded" after each command.
4501
4502 Returns true if a breakpoint command resumed the inferior. In that
4503 case, it is the caller's responsibility to recall it again with the
4504 bpstat of the current thread. */
4505
4506 static int
4507 bpstat_do_actions_1 (bpstat *bsp)
4508 {
4509 bpstat bs;
4510 int again = 0;
4511
4512 /* Avoid endless recursion if a `source' command is contained
4513 in bs->commands. */
4514 if (executing_breakpoint_commands)
4515 return 0;
4516
4517 scoped_restore save_executing
4518 = make_scoped_restore (&executing_breakpoint_commands, 1);
4519
4520 scoped_restore preventer = prevent_dont_repeat ();
4521
4522 /* This pointer will iterate over the list of bpstat's. */
4523 bs = *bsp;
4524
4525 breakpoint_proceeded = 0;
4526 for (; bs != NULL; bs = bs->next)
4527 {
4528 struct command_line *cmd = NULL;
4529
4530 /* Take ownership of the BSP's command tree, if it has one.
4531
4532 The command tree could legitimately contain commands like
4533 'step' and 'next', which call clear_proceed_status, which
4534 frees stop_bpstat's command tree. To make sure this doesn't
4535 free the tree we're executing out from under us, we need to
4536 take ownership of the tree ourselves. Since a given bpstat's
4537 commands are only executed once, we don't need to copy it; we
4538 can clear the pointer in the bpstat, and make sure we free
4539 the tree when we're done. */
4540 counted_command_line ccmd = bs->commands;
4541 bs->commands = NULL;
4542 if (ccmd != NULL)
4543 cmd = ccmd.get ();
4544 if (command_line_is_silent (cmd))
4545 {
4546 /* The action has been already done by bpstat_stop_status. */
4547 cmd = cmd->next;
4548 }
4549
4550 while (cmd != NULL)
4551 {
4552 execute_control_command (cmd);
4553
4554 if (breakpoint_proceeded)
4555 break;
4556 else
4557 cmd = cmd->next;
4558 }
4559
4560 if (breakpoint_proceeded)
4561 {
4562 if (current_ui->async)
4563 /* If we are in async mode, then the target might be still
4564 running, not stopped at any breakpoint, so nothing for
4565 us to do here -- just return to the event loop. */
4566 ;
4567 else
4568 /* In sync mode, when execute_control_command returns
4569 we're already standing on the next breakpoint.
4570 Breakpoint commands for that stop were not run, since
4571 execute_command does not run breakpoint commands --
4572 only command_line_handler does, but that one is not
4573 involved in execution of breakpoint commands. So, we
4574 can now execute breakpoint commands. It should be
4575 noted that making execute_command do bpstat actions is
4576 not an option -- in this case we'll have recursive
4577 invocation of bpstat for each breakpoint with a
4578 command, and can easily blow up GDB stack. Instead, we
4579 return true, which will trigger the caller to recall us
4580 with the new stop_bpstat. */
4581 again = 1;
4582 break;
4583 }
4584 }
4585 return again;
4586 }
4587
4588 /* Helper for bpstat_do_actions. Get the current thread, if there's
4589 one, is alive and has execution. Return NULL otherwise. */
4590
4591 static thread_info *
4592 get_bpstat_thread ()
4593 {
4594 if (inferior_ptid == null_ptid || !target_has_execution ())
4595 return NULL;
4596
4597 thread_info *tp = inferior_thread ();
4598 if (tp->state == THREAD_EXITED || tp->executing)
4599 return NULL;
4600 return tp;
4601 }
4602
4603 void
4604 bpstat_do_actions (void)
4605 {
4606 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4607 thread_info *tp;
4608
4609 /* Do any commands attached to breakpoint we are stopped at. */
4610 while ((tp = get_bpstat_thread ()) != NULL)
4611 {
4612 /* Since in sync mode, bpstat_do_actions may resume the
4613 inferior, and only return when it is stopped at the next
4614 breakpoint, we keep doing breakpoint actions until it returns
4615 false to indicate the inferior was not resumed. */
4616 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4617 break;
4618 }
4619
4620 cleanup_if_error.release ();
4621 }
4622
4623 /* Print out the (old or new) value associated with a watchpoint. */
4624
4625 static void
4626 watchpoint_value_print (struct value *val, struct ui_file *stream)
4627 {
4628 if (val == NULL)
4629 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4630 else
4631 {
4632 struct value_print_options opts;
4633 get_user_print_options (&opts);
4634 value_print (val, stream, &opts);
4635 }
4636 }
4637
4638 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4639 debugging multiple threads. */
4640
4641 void
4642 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4643 {
4644 if (uiout->is_mi_like_p ())
4645 return;
4646
4647 uiout->text ("\n");
4648
4649 if (show_thread_that_caused_stop ())
4650 {
4651 const char *name;
4652 struct thread_info *thr = inferior_thread ();
4653
4654 uiout->text ("Thread ");
4655 uiout->field_string ("thread-id", print_thread_id (thr));
4656
4657 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4658 if (name != NULL)
4659 {
4660 uiout->text (" \"");
4661 uiout->field_string ("name", name);
4662 uiout->text ("\"");
4663 }
4664
4665 uiout->text (" hit ");
4666 }
4667 }
4668
4669 /* Generic routine for printing messages indicating why we
4670 stopped. The behavior of this function depends on the value
4671 'print_it' in the bpstat structure. Under some circumstances we
4672 may decide not to print anything here and delegate the task to
4673 normal_stop(). */
4674
4675 static enum print_stop_action
4676 print_bp_stop_message (bpstat bs)
4677 {
4678 switch (bs->print_it)
4679 {
4680 case print_it_noop:
4681 /* Nothing should be printed for this bpstat entry. */
4682 return PRINT_UNKNOWN;
4683 break;
4684
4685 case print_it_done:
4686 /* We still want to print the frame, but we already printed the
4687 relevant messages. */
4688 return PRINT_SRC_AND_LOC;
4689 break;
4690
4691 case print_it_normal:
4692 {
4693 struct breakpoint *b = bs->breakpoint_at;
4694
4695 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4696 which has since been deleted. */
4697 if (b == NULL)
4698 return PRINT_UNKNOWN;
4699
4700 /* Normal case. Call the breakpoint's print_it method. */
4701 return b->ops->print_it (bs);
4702 }
4703 break;
4704
4705 default:
4706 internal_error (__FILE__, __LINE__,
4707 _("print_bp_stop_message: unrecognized enum value"));
4708 break;
4709 }
4710 }
4711
4712 /* A helper function that prints a shared library stopped event. */
4713
4714 static void
4715 print_solib_event (int is_catchpoint)
4716 {
4717 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4718 bool any_added = !current_program_space->added_solibs.empty ();
4719
4720 if (!is_catchpoint)
4721 {
4722 if (any_added || any_deleted)
4723 current_uiout->text (_("Stopped due to shared library event:\n"));
4724 else
4725 current_uiout->text (_("Stopped due to shared library event (no "
4726 "libraries added or removed)\n"));
4727 }
4728
4729 if (current_uiout->is_mi_like_p ())
4730 current_uiout->field_string ("reason",
4731 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4732
4733 if (any_deleted)
4734 {
4735 current_uiout->text (_(" Inferior unloaded "));
4736 ui_out_emit_list list_emitter (current_uiout, "removed");
4737 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4738 {
4739 const std::string &name = current_program_space->deleted_solibs[ix];
4740
4741 if (ix > 0)
4742 current_uiout->text (" ");
4743 current_uiout->field_string ("library", name);
4744 current_uiout->text ("\n");
4745 }
4746 }
4747
4748 if (any_added)
4749 {
4750 current_uiout->text (_(" Inferior loaded "));
4751 ui_out_emit_list list_emitter (current_uiout, "added");
4752 bool first = true;
4753 for (so_list *iter : current_program_space->added_solibs)
4754 {
4755 if (!first)
4756 current_uiout->text (" ");
4757 first = false;
4758 current_uiout->field_string ("library", iter->so_name);
4759 current_uiout->text ("\n");
4760 }
4761 }
4762 }
4763
4764 /* Print a message indicating what happened. This is called from
4765 normal_stop(). The input to this routine is the head of the bpstat
4766 list - a list of the eventpoints that caused this stop. KIND is
4767 the target_waitkind for the stopping event. This
4768 routine calls the generic print routine for printing a message
4769 about reasons for stopping. This will print (for example) the
4770 "Breakpoint n," part of the output. The return value of this
4771 routine is one of:
4772
4773 PRINT_UNKNOWN: Means we printed nothing.
4774 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4775 code to print the location. An example is
4776 "Breakpoint 1, " which should be followed by
4777 the location.
4778 PRINT_SRC_ONLY: Means we printed something, but there is no need
4779 to also print the location part of the message.
4780 An example is the catch/throw messages, which
4781 don't require a location appended to the end.
4782 PRINT_NOTHING: We have done some printing and we don't need any
4783 further info to be printed. */
4784
4785 enum print_stop_action
4786 bpstat_print (bpstat bs, int kind)
4787 {
4788 enum print_stop_action val;
4789
4790 /* Maybe another breakpoint in the chain caused us to stop.
4791 (Currently all watchpoints go on the bpstat whether hit or not.
4792 That probably could (should) be changed, provided care is taken
4793 with respect to bpstat_explains_signal). */
4794 for (; bs; bs = bs->next)
4795 {
4796 val = print_bp_stop_message (bs);
4797 if (val == PRINT_SRC_ONLY
4798 || val == PRINT_SRC_AND_LOC
4799 || val == PRINT_NOTHING)
4800 return val;
4801 }
4802
4803 /* If we had hit a shared library event breakpoint,
4804 print_bp_stop_message would print out this message. If we hit an
4805 OS-level shared library event, do the same thing. */
4806 if (kind == TARGET_WAITKIND_LOADED)
4807 {
4808 print_solib_event (0);
4809 return PRINT_NOTHING;
4810 }
4811
4812 /* We reached the end of the chain, or we got a null BS to start
4813 with and nothing was printed. */
4814 return PRINT_UNKNOWN;
4815 }
4816
4817 /* Evaluate the boolean expression EXP and return the result. */
4818
4819 static bool
4820 breakpoint_cond_eval (expression *exp)
4821 {
4822 struct value *mark = value_mark ();
4823 bool res = value_true (evaluate_expression (exp));
4824
4825 value_free_to_mark (mark);
4826 return res;
4827 }
4828
4829 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4830
4831 bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4832 : next (NULL),
4833 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
4834 breakpoint_at (bl->owner),
4835 commands (NULL),
4836 print (0),
4837 stop (0),
4838 print_it (print_it_normal)
4839 {
4840 **bs_link_pointer = this;
4841 *bs_link_pointer = &next;
4842 }
4843
4844 bpstats::bpstats ()
4845 : next (NULL),
4846 breakpoint_at (NULL),
4847 commands (NULL),
4848 print (0),
4849 stop (0),
4850 print_it (print_it_normal)
4851 {
4852 }
4853 \f
4854 /* The target has stopped with waitstatus WS. Check if any hardware
4855 watchpoints have triggered, according to the target. */
4856
4857 int
4858 watchpoints_triggered (struct target_waitstatus *ws)
4859 {
4860 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
4861 CORE_ADDR addr;
4862
4863 if (!stopped_by_watchpoint)
4864 {
4865 /* We were not stopped by a watchpoint. Mark all watchpoints
4866 as not triggered. */
4867 for (breakpoint *b : all_breakpoints ())
4868 if (is_hardware_watchpoint (b))
4869 {
4870 struct watchpoint *w = (struct watchpoint *) b;
4871
4872 w->watchpoint_triggered = watch_triggered_no;
4873 }
4874
4875 return 0;
4876 }
4877
4878 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
4879 {
4880 /* We were stopped by a watchpoint, but we don't know where.
4881 Mark all watchpoints as unknown. */
4882 for (breakpoint *b : all_breakpoints ())
4883 if (is_hardware_watchpoint (b))
4884 {
4885 struct watchpoint *w = (struct watchpoint *) b;
4886
4887 w->watchpoint_triggered = watch_triggered_unknown;
4888 }
4889
4890 return 1;
4891 }
4892
4893 /* The target could report the data address. Mark watchpoints
4894 affected by this data address as triggered, and all others as not
4895 triggered. */
4896
4897 for (breakpoint *b : all_breakpoints ())
4898 if (is_hardware_watchpoint (b))
4899 {
4900 struct watchpoint *w = (struct watchpoint *) b;
4901 struct bp_location *loc;
4902
4903 w->watchpoint_triggered = watch_triggered_no;
4904 for (loc = b->loc; loc; loc = loc->next)
4905 {
4906 if (is_masked_watchpoint (b))
4907 {
4908 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4909 CORE_ADDR start = loc->address & w->hw_wp_mask;
4910
4911 if (newaddr == start)
4912 {
4913 w->watchpoint_triggered = watch_triggered_yes;
4914 break;
4915 }
4916 }
4917 /* Exact match not required. Within range is sufficient. */
4918 else if (target_watchpoint_addr_within_range
4919 (current_inferior ()->top_target (), addr, loc->address,
4920 loc->length))
4921 {
4922 w->watchpoint_triggered = watch_triggered_yes;
4923 break;
4924 }
4925 }
4926 }
4927
4928 return 1;
4929 }
4930
4931 /* Possible return values for watchpoint_check. */
4932 enum wp_check_result
4933 {
4934 /* The watchpoint has been deleted. */
4935 WP_DELETED = 1,
4936
4937 /* The value has changed. */
4938 WP_VALUE_CHANGED = 2,
4939
4940 /* The value has not changed. */
4941 WP_VALUE_NOT_CHANGED = 3,
4942
4943 /* Ignore this watchpoint, no matter if the value changed or not. */
4944 WP_IGNORE = 4,
4945 };
4946
4947 #define BP_TEMPFLAG 1
4948 #define BP_HARDWAREFLAG 2
4949
4950 /* Evaluate watchpoint condition expression and check if its value
4951 changed. */
4952
4953 static wp_check_result
4954 watchpoint_check (bpstat bs)
4955 {
4956 struct watchpoint *b;
4957 struct frame_info *fr;
4958 int within_current_scope;
4959
4960 /* BS is built from an existing struct breakpoint. */
4961 gdb_assert (bs->breakpoint_at != NULL);
4962 b = (struct watchpoint *) bs->breakpoint_at;
4963
4964 /* If this is a local watchpoint, we only want to check if the
4965 watchpoint frame is in scope if the current thread is the thread
4966 that was used to create the watchpoint. */
4967 if (!watchpoint_in_thread_scope (b))
4968 return WP_IGNORE;
4969
4970 if (b->exp_valid_block == NULL)
4971 within_current_scope = 1;
4972 else
4973 {
4974 struct frame_info *frame = get_current_frame ();
4975 struct gdbarch *frame_arch = get_frame_arch (frame);
4976 CORE_ADDR frame_pc = get_frame_pc (frame);
4977
4978 /* stack_frame_destroyed_p() returns a non-zero value if we're
4979 still in the function but the stack frame has already been
4980 invalidated. Since we can't rely on the values of local
4981 variables after the stack has been destroyed, we are treating
4982 the watchpoint in that state as `not changed' without further
4983 checking. Don't mark watchpoints as changed if the current
4984 frame is in an epilogue - even if they are in some other
4985 frame, our view of the stack is likely to be wrong and
4986 frame_find_by_id could error out. */
4987 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
4988 return WP_IGNORE;
4989
4990 fr = frame_find_by_id (b->watchpoint_frame);
4991 within_current_scope = (fr != NULL);
4992
4993 /* If we've gotten confused in the unwinder, we might have
4994 returned a frame that can't describe this variable. */
4995 if (within_current_scope)
4996 {
4997 struct symbol *function;
4998
4999 function = get_frame_function (fr);
5000 if (function == NULL
5001 || !contained_in (b->exp_valid_block,
5002 SYMBOL_BLOCK_VALUE (function)))
5003 within_current_scope = 0;
5004 }
5005
5006 if (within_current_scope)
5007 /* If we end up stopping, the current frame will get selected
5008 in normal_stop. So this call to select_frame won't affect
5009 the user. */
5010 select_frame (fr);
5011 }
5012
5013 if (within_current_scope)
5014 {
5015 /* We use value_{,free_to_}mark because it could be a *long*
5016 time before we return to the command level and call
5017 free_all_values. We can't call free_all_values because we
5018 might be in the middle of evaluating a function call. */
5019
5020 struct value *mark;
5021 struct value *new_val;
5022
5023 if (is_masked_watchpoint (b))
5024 /* Since we don't know the exact trigger address (from
5025 stopped_data_address), just tell the user we've triggered
5026 a mask watchpoint. */
5027 return WP_VALUE_CHANGED;
5028
5029 mark = value_mark ();
5030 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
5031 NULL, NULL, false);
5032
5033 if (b->val_bitsize != 0)
5034 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5035
5036 /* We use value_equal_contents instead of value_equal because
5037 the latter coerces an array to a pointer, thus comparing just
5038 the address of the array instead of its contents. This is
5039 not what we want. */
5040 if ((b->val != NULL) != (new_val != NULL)
5041 || (b->val != NULL && !value_equal_contents (b->val.get (),
5042 new_val)))
5043 {
5044 bs->old_val = b->val;
5045 b->val = release_value (new_val);
5046 b->val_valid = true;
5047 if (new_val != NULL)
5048 value_free_to_mark (mark);
5049 return WP_VALUE_CHANGED;
5050 }
5051 else
5052 {
5053 /* Nothing changed. */
5054 value_free_to_mark (mark);
5055 return WP_VALUE_NOT_CHANGED;
5056 }
5057 }
5058 else
5059 {
5060 /* This seems like the only logical thing to do because
5061 if we temporarily ignored the watchpoint, then when
5062 we reenter the block in which it is valid it contains
5063 garbage (in the case of a function, it may have two
5064 garbage values, one before and one after the prologue).
5065 So we can't even detect the first assignment to it and
5066 watch after that (since the garbage may or may not equal
5067 the first value assigned). */
5068 /* We print all the stop information in
5069 breakpoint_ops->print_it, but in this case, by the time we
5070 call breakpoint_ops->print_it this bp will be deleted
5071 already. So we have no choice but print the information
5072 here. */
5073
5074 SWITCH_THRU_ALL_UIS ()
5075 {
5076 struct ui_out *uiout = current_uiout;
5077
5078 if (uiout->is_mi_like_p ())
5079 uiout->field_string
5080 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5081 uiout->message ("\nWatchpoint %pF deleted because the program has "
5082 "left the block in\n"
5083 "which its expression is valid.\n",
5084 signed_field ("wpnum", b->number));
5085 }
5086
5087 /* Make sure the watchpoint's commands aren't executed. */
5088 b->commands = NULL;
5089 watchpoint_del_at_next_stop (b);
5090
5091 return WP_DELETED;
5092 }
5093 }
5094
5095 /* Return true if it looks like target has stopped due to hitting
5096 breakpoint location BL. This function does not check if we should
5097 stop, only if BL explains the stop. */
5098
5099 static int
5100 bpstat_check_location (const struct bp_location *bl,
5101 const address_space *aspace, CORE_ADDR bp_addr,
5102 const struct target_waitstatus *ws)
5103 {
5104 struct breakpoint *b = bl->owner;
5105
5106 /* BL is from an existing breakpoint. */
5107 gdb_assert (b != NULL);
5108
5109 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5110 }
5111
5112 /* Determine if the watched values have actually changed, and we
5113 should stop. If not, set BS->stop to 0. */
5114
5115 static void
5116 bpstat_check_watchpoint (bpstat bs)
5117 {
5118 const struct bp_location *bl;
5119 struct watchpoint *b;
5120
5121 /* BS is built for existing struct breakpoint. */
5122 bl = bs->bp_location_at.get ();
5123 gdb_assert (bl != NULL);
5124 b = (struct watchpoint *) bs->breakpoint_at;
5125 gdb_assert (b != NULL);
5126
5127 {
5128 int must_check_value = 0;
5129
5130 if (b->type == bp_watchpoint)
5131 /* For a software watchpoint, we must always check the
5132 watched value. */
5133 must_check_value = 1;
5134 else if (b->watchpoint_triggered == watch_triggered_yes)
5135 /* We have a hardware watchpoint (read, write, or access)
5136 and the target earlier reported an address watched by
5137 this watchpoint. */
5138 must_check_value = 1;
5139 else if (b->watchpoint_triggered == watch_triggered_unknown
5140 && b->type == bp_hardware_watchpoint)
5141 /* We were stopped by a hardware watchpoint, but the target could
5142 not report the data address. We must check the watchpoint's
5143 value. Access and read watchpoints are out of luck; without
5144 a data address, we can't figure it out. */
5145 must_check_value = 1;
5146
5147 if (must_check_value)
5148 {
5149 wp_check_result e;
5150
5151 try
5152 {
5153 e = watchpoint_check (bs);
5154 }
5155 catch (const gdb_exception &ex)
5156 {
5157 exception_fprintf (gdb_stderr, ex,
5158 "Error evaluating expression "
5159 "for watchpoint %d\n",
5160 b->number);
5161
5162 SWITCH_THRU_ALL_UIS ()
5163 {
5164 printf_filtered (_("Watchpoint %d deleted.\n"),
5165 b->number);
5166 }
5167 watchpoint_del_at_next_stop (b);
5168 e = WP_DELETED;
5169 }
5170
5171 switch (e)
5172 {
5173 case WP_DELETED:
5174 /* We've already printed what needs to be printed. */
5175 bs->print_it = print_it_done;
5176 /* Stop. */
5177 break;
5178 case WP_IGNORE:
5179 bs->print_it = print_it_noop;
5180 bs->stop = 0;
5181 break;
5182 case WP_VALUE_CHANGED:
5183 if (b->type == bp_read_watchpoint)
5184 {
5185 /* There are two cases to consider here:
5186
5187 1. We're watching the triggered memory for reads.
5188 In that case, trust the target, and always report
5189 the watchpoint hit to the user. Even though
5190 reads don't cause value changes, the value may
5191 have changed since the last time it was read, and
5192 since we're not trapping writes, we will not see
5193 those, and as such we should ignore our notion of
5194 old value.
5195
5196 2. We're watching the triggered memory for both
5197 reads and writes. There are two ways this may
5198 happen:
5199
5200 2.1. This is a target that can't break on data
5201 reads only, but can break on accesses (reads or
5202 writes), such as e.g., x86. We detect this case
5203 at the time we try to insert read watchpoints.
5204
5205 2.2. Otherwise, the target supports read
5206 watchpoints, but, the user set an access or write
5207 watchpoint watching the same memory as this read
5208 watchpoint.
5209
5210 If we're watching memory writes as well as reads,
5211 ignore watchpoint hits when we find that the
5212 value hasn't changed, as reads don't cause
5213 changes. This still gives false positives when
5214 the program writes the same value to memory as
5215 what there was already in memory (we will confuse
5216 it for a read), but it's much better than
5217 nothing. */
5218
5219 int other_write_watchpoint = 0;
5220
5221 if (bl->watchpoint_type == hw_read)
5222 {
5223 for (breakpoint *other_b : all_breakpoints ())
5224 if (other_b->type == bp_hardware_watchpoint
5225 || other_b->type == bp_access_watchpoint)
5226 {
5227 struct watchpoint *other_w =
5228 (struct watchpoint *) other_b;
5229
5230 if (other_w->watchpoint_triggered
5231 == watch_triggered_yes)
5232 {
5233 other_write_watchpoint = 1;
5234 break;
5235 }
5236 }
5237 }
5238
5239 if (other_write_watchpoint
5240 || bl->watchpoint_type == hw_access)
5241 {
5242 /* We're watching the same memory for writes,
5243 and the value changed since the last time we
5244 updated it, so this trap must be for a write.
5245 Ignore it. */
5246 bs->print_it = print_it_noop;
5247 bs->stop = 0;
5248 }
5249 }
5250 break;
5251 case WP_VALUE_NOT_CHANGED:
5252 if (b->type == bp_hardware_watchpoint
5253 || b->type == bp_watchpoint)
5254 {
5255 /* Don't stop: write watchpoints shouldn't fire if
5256 the value hasn't changed. */
5257 bs->print_it = print_it_noop;
5258 bs->stop = 0;
5259 }
5260 /* Stop. */
5261 break;
5262 default:
5263 /* Can't happen. */
5264 break;
5265 }
5266 }
5267 else /* must_check_value == 0 */
5268 {
5269 /* This is a case where some watchpoint(s) triggered, but
5270 not at the address of this watchpoint, or else no
5271 watchpoint triggered after all. So don't print
5272 anything for this watchpoint. */
5273 bs->print_it = print_it_noop;
5274 bs->stop = 0;
5275 }
5276 }
5277 }
5278
5279 /* For breakpoints that are currently marked as telling gdb to stop,
5280 check conditions (condition proper, frame, thread and ignore count)
5281 of breakpoint referred to by BS. If we should not stop for this
5282 breakpoint, set BS->stop to 0. */
5283
5284 static void
5285 bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
5286 {
5287 const struct bp_location *bl;
5288 struct breakpoint *b;
5289 /* Assume stop. */
5290 bool condition_result = true;
5291 struct expression *cond;
5292
5293 gdb_assert (bs->stop);
5294
5295 /* BS is built for existing struct breakpoint. */
5296 bl = bs->bp_location_at.get ();
5297 gdb_assert (bl != NULL);
5298 b = bs->breakpoint_at;
5299 gdb_assert (b != NULL);
5300
5301 /* Even if the target evaluated the condition on its end and notified GDB, we
5302 need to do so again since GDB does not know if we stopped due to a
5303 breakpoint or a single step breakpoint. */
5304
5305 if (frame_id_p (b->frame_id)
5306 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5307 {
5308 bs->stop = 0;
5309 return;
5310 }
5311
5312 /* If this is a thread/task-specific breakpoint, don't waste cpu
5313 evaluating the condition if this isn't the specified
5314 thread/task. */
5315 if ((b->thread != -1 && b->thread != thread->global_num)
5316 || (b->task != 0 && b->task != ada_get_task_number (thread)))
5317 {
5318 bs->stop = 0;
5319 return;
5320 }
5321
5322 /* Evaluate extension language breakpoints that have a "stop" method
5323 implemented. */
5324 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5325
5326 if (is_watchpoint (b))
5327 {
5328 struct watchpoint *w = (struct watchpoint *) b;
5329
5330 cond = w->cond_exp.get ();
5331 }
5332 else
5333 cond = bl->cond.get ();
5334
5335 if (cond && b->disposition != disp_del_at_next_stop)
5336 {
5337 int within_current_scope = 1;
5338 struct watchpoint * w;
5339
5340 /* We use value_mark and value_free_to_mark because it could
5341 be a long time before we return to the command level and
5342 call free_all_values. We can't call free_all_values
5343 because we might be in the middle of evaluating a
5344 function call. */
5345 struct value *mark = value_mark ();
5346
5347 if (is_watchpoint (b))
5348 w = (struct watchpoint *) b;
5349 else
5350 w = NULL;
5351
5352 /* Need to select the frame, with all that implies so that
5353 the conditions will have the right context. Because we
5354 use the frame, we will not see an inlined function's
5355 variables when we arrive at a breakpoint at the start
5356 of the inlined function; the current frame will be the
5357 call site. */
5358 if (w == NULL || w->cond_exp_valid_block == NULL)
5359 select_frame (get_current_frame ());
5360 else
5361 {
5362 struct frame_info *frame;
5363
5364 /* For local watchpoint expressions, which particular
5365 instance of a local is being watched matters, so we
5366 keep track of the frame to evaluate the expression
5367 in. To evaluate the condition however, it doesn't
5368 really matter which instantiation of the function
5369 where the condition makes sense triggers the
5370 watchpoint. This allows an expression like "watch
5371 global if q > 10" set in `func', catch writes to
5372 global on all threads that call `func', or catch
5373 writes on all recursive calls of `func' by a single
5374 thread. We simply always evaluate the condition in
5375 the innermost frame that's executing where it makes
5376 sense to evaluate the condition. It seems
5377 intuitive. */
5378 frame = block_innermost_frame (w->cond_exp_valid_block);
5379 if (frame != NULL)
5380 select_frame (frame);
5381 else
5382 within_current_scope = 0;
5383 }
5384 if (within_current_scope)
5385 {
5386 try
5387 {
5388 condition_result = breakpoint_cond_eval (cond);
5389 }
5390 catch (const gdb_exception &ex)
5391 {
5392 exception_fprintf (gdb_stderr, ex,
5393 "Error in testing breakpoint condition:\n");
5394 }
5395 }
5396 else
5397 {
5398 warning (_("Watchpoint condition cannot be tested "
5399 "in the current scope"));
5400 /* If we failed to set the right context for this
5401 watchpoint, unconditionally report it. */
5402 }
5403 /* FIXME-someday, should give breakpoint #. */
5404 value_free_to_mark (mark);
5405 }
5406
5407 if (cond && !condition_result)
5408 {
5409 bs->stop = 0;
5410 }
5411 else if (b->ignore_count > 0)
5412 {
5413 b->ignore_count--;
5414 bs->stop = 0;
5415 /* Increase the hit count even though we don't stop. */
5416 ++(b->hit_count);
5417 gdb::observers::breakpoint_modified.notify (b);
5418 }
5419 }
5420
5421 /* Returns true if we need to track moribund locations of LOC's type
5422 on the current target. */
5423
5424 static int
5425 need_moribund_for_location_type (struct bp_location *loc)
5426 {
5427 return ((loc->loc_type == bp_loc_software_breakpoint
5428 && !target_supports_stopped_by_sw_breakpoint ())
5429 || (loc->loc_type == bp_loc_hardware_breakpoint
5430 && !target_supports_stopped_by_hw_breakpoint ()));
5431 }
5432
5433 /* See breakpoint.h. */
5434
5435 bpstat
5436 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5437 const struct target_waitstatus *ws)
5438 {
5439 bpstat bs_head = NULL, *bs_link = &bs_head;
5440
5441 for (breakpoint *b : all_breakpoints ())
5442 {
5443 if (!breakpoint_enabled (b))
5444 continue;
5445
5446 for (bp_location *bl = b->loc; bl != NULL; bl = bl->next)
5447 {
5448 /* For hardware watchpoints, we look only at the first
5449 location. The watchpoint_check function will work on the
5450 entire expression, not the individual locations. For
5451 read watchpoints, the watchpoints_triggered function has
5452 checked all locations already. */
5453 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5454 break;
5455
5456 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
5457 continue;
5458
5459 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5460 continue;
5461
5462 /* Come here if it's a watchpoint, or if the break address
5463 matches. */
5464
5465 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5466 explain stop. */
5467
5468 /* Assume we stop. Should we find a watchpoint that is not
5469 actually triggered, or if the condition of the breakpoint
5470 evaluates as false, we'll reset 'stop' to 0. */
5471 bs->stop = 1;
5472 bs->print = 1;
5473
5474 /* If this is a scope breakpoint, mark the associated
5475 watchpoint as triggered so that we will handle the
5476 out-of-scope event. We'll get to the watchpoint next
5477 iteration. */
5478 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5479 {
5480 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5481
5482 w->watchpoint_triggered = watch_triggered_yes;
5483 }
5484 }
5485 }
5486
5487 /* Check if a moribund breakpoint explains the stop. */
5488 if (!target_supports_stopped_by_sw_breakpoint ()
5489 || !target_supports_stopped_by_hw_breakpoint ())
5490 {
5491 for (bp_location *loc : moribund_locations)
5492 {
5493 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5494 && need_moribund_for_location_type (loc))
5495 {
5496 bpstat bs = new bpstats (loc, &bs_link);
5497 /* For hits of moribund locations, we should just proceed. */
5498 bs->stop = 0;
5499 bs->print = 0;
5500 bs->print_it = print_it_noop;
5501 }
5502 }
5503 }
5504
5505 return bs_head;
5506 }
5507
5508 /* See breakpoint.h. */
5509
5510 bpstat
5511 bpstat_stop_status (const address_space *aspace,
5512 CORE_ADDR bp_addr, thread_info *thread,
5513 const struct target_waitstatus *ws,
5514 bpstat stop_chain)
5515 {
5516 struct breakpoint *b = NULL;
5517 /* First item of allocated bpstat's. */
5518 bpstat bs_head = stop_chain;
5519 bpstat bs;
5520 int need_remove_insert;
5521 int removed_any;
5522
5523 /* First, build the bpstat chain with locations that explain a
5524 target stop, while being careful to not set the target running,
5525 as that may invalidate locations (in particular watchpoint
5526 locations are recreated). Resuming will happen here with
5527 breakpoint conditions or watchpoint expressions that include
5528 inferior function calls. */
5529 if (bs_head == NULL)
5530 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5531
5532 /* A bit of special processing for shlib breakpoints. We need to
5533 process solib loading here, so that the lists of loaded and
5534 unloaded libraries are correct before we handle "catch load" and
5535 "catch unload". */
5536 for (bs = bs_head; bs != NULL; bs = bs->next)
5537 {
5538 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5539 {
5540 handle_solib_event ();
5541 break;
5542 }
5543 }
5544
5545 /* Now go through the locations that caused the target to stop, and
5546 check whether we're interested in reporting this stop to higher
5547 layers, or whether we should resume the target transparently. */
5548
5549 removed_any = 0;
5550
5551 for (bs = bs_head; bs != NULL; bs = bs->next)
5552 {
5553 if (!bs->stop)
5554 continue;
5555
5556 b = bs->breakpoint_at;
5557 b->ops->check_status (bs);
5558 if (bs->stop)
5559 {
5560 bpstat_check_breakpoint_conditions (bs, thread);
5561
5562 if (bs->stop)
5563 {
5564 ++(b->hit_count);
5565 gdb::observers::breakpoint_modified.notify (b);
5566
5567 /* We will stop here. */
5568 if (b->disposition == disp_disable)
5569 {
5570 --(b->enable_count);
5571 if (b->enable_count <= 0)
5572 b->enable_state = bp_disabled;
5573 removed_any = 1;
5574 }
5575 if (b->silent)
5576 bs->print = 0;
5577 bs->commands = b->commands;
5578 if (command_line_is_silent (bs->commands
5579 ? bs->commands.get () : NULL))
5580 bs->print = 0;
5581
5582 b->ops->after_condition_true (bs);
5583 }
5584
5585 }
5586
5587 /* Print nothing for this entry if we don't stop or don't
5588 print. */
5589 if (!bs->stop || !bs->print)
5590 bs->print_it = print_it_noop;
5591 }
5592
5593 /* If we aren't stopping, the value of some hardware watchpoint may
5594 not have changed, but the intermediate memory locations we are
5595 watching may have. Don't bother if we're stopping; this will get
5596 done later. */
5597 need_remove_insert = 0;
5598 if (! bpstat_causes_stop (bs_head))
5599 for (bs = bs_head; bs != NULL; bs = bs->next)
5600 if (!bs->stop
5601 && bs->breakpoint_at
5602 && is_hardware_watchpoint (bs->breakpoint_at))
5603 {
5604 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5605
5606 update_watchpoint (w, 0 /* don't reparse. */);
5607 need_remove_insert = 1;
5608 }
5609
5610 if (need_remove_insert)
5611 update_global_location_list (UGLL_MAY_INSERT);
5612 else if (removed_any)
5613 update_global_location_list (UGLL_DONT_INSERT);
5614
5615 return bs_head;
5616 }
5617
5618 static void
5619 handle_jit_event (CORE_ADDR address)
5620 {
5621 struct gdbarch *gdbarch;
5622
5623 infrun_debug_printf ("handling bp_jit_event");
5624
5625 /* Switch terminal for any messages produced by
5626 breakpoint_re_set. */
5627 target_terminal::ours_for_output ();
5628
5629 gdbarch = get_frame_arch (get_current_frame ());
5630 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5631 thus it is expected that its objectfile can be found through
5632 minimal symbol lookup. If it doesn't work (and assert fails), it
5633 most likely means that `jit_breakpoint_re_set` was changes and this
5634 function needs to be updated too. */
5635 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5636 gdb_assert (jit_bp_sym.objfile != nullptr);
5637 jit_event_handler (gdbarch, jit_bp_sym.objfile);
5638
5639 target_terminal::inferior ();
5640 }
5641
5642 /* Prepare WHAT final decision for infrun. */
5643
5644 /* Decide what infrun needs to do with this bpstat. */
5645
5646 struct bpstat_what
5647 bpstat_what (bpstat bs_head)
5648 {
5649 struct bpstat_what retval;
5650 bpstat bs;
5651
5652 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5653 retval.call_dummy = STOP_NONE;
5654 retval.is_longjmp = false;
5655
5656 for (bs = bs_head; bs != NULL; bs = bs->next)
5657 {
5658 /* Extract this BS's action. After processing each BS, we check
5659 if its action overrides all we've seem so far. */
5660 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5661 enum bptype bptype;
5662
5663 if (bs->breakpoint_at == NULL)
5664 {
5665 /* I suspect this can happen if it was a momentary
5666 breakpoint which has since been deleted. */
5667 bptype = bp_none;
5668 }
5669 else
5670 bptype = bs->breakpoint_at->type;
5671
5672 switch (bptype)
5673 {
5674 case bp_none:
5675 break;
5676 case bp_breakpoint:
5677 case bp_hardware_breakpoint:
5678 case bp_single_step:
5679 case bp_until:
5680 case bp_finish:
5681 case bp_shlib_event:
5682 if (bs->stop)
5683 {
5684 if (bs->print)
5685 this_action = BPSTAT_WHAT_STOP_NOISY;
5686 else
5687 this_action = BPSTAT_WHAT_STOP_SILENT;
5688 }
5689 else
5690 this_action = BPSTAT_WHAT_SINGLE;
5691 break;
5692 case bp_watchpoint:
5693 case bp_hardware_watchpoint:
5694 case bp_read_watchpoint:
5695 case bp_access_watchpoint:
5696 if (bs->stop)
5697 {
5698 if (bs->print)
5699 this_action = BPSTAT_WHAT_STOP_NOISY;
5700 else
5701 this_action = BPSTAT_WHAT_STOP_SILENT;
5702 }
5703 else
5704 {
5705 /* There was a watchpoint, but we're not stopping.
5706 This requires no further action. */
5707 }
5708 break;
5709 case bp_longjmp:
5710 case bp_longjmp_call_dummy:
5711 case bp_exception:
5712 if (bs->stop)
5713 {
5714 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5715 retval.is_longjmp = bptype != bp_exception;
5716 }
5717 else
5718 this_action = BPSTAT_WHAT_SINGLE;
5719 break;
5720 case bp_longjmp_resume:
5721 case bp_exception_resume:
5722 if (bs->stop)
5723 {
5724 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5725 retval.is_longjmp = bptype == bp_longjmp_resume;
5726 }
5727 else
5728 this_action = BPSTAT_WHAT_SINGLE;
5729 break;
5730 case bp_step_resume:
5731 if (bs->stop)
5732 this_action = BPSTAT_WHAT_STEP_RESUME;
5733 else
5734 {
5735 /* It is for the wrong frame. */
5736 this_action = BPSTAT_WHAT_SINGLE;
5737 }
5738 break;
5739 case bp_hp_step_resume:
5740 if (bs->stop)
5741 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5742 else
5743 {
5744 /* It is for the wrong frame. */
5745 this_action = BPSTAT_WHAT_SINGLE;
5746 }
5747 break;
5748 case bp_watchpoint_scope:
5749 case bp_thread_event:
5750 case bp_overlay_event:
5751 case bp_longjmp_master:
5752 case bp_std_terminate_master:
5753 case bp_exception_master:
5754 this_action = BPSTAT_WHAT_SINGLE;
5755 break;
5756 case bp_catchpoint:
5757 if (bs->stop)
5758 {
5759 if (bs->print)
5760 this_action = BPSTAT_WHAT_STOP_NOISY;
5761 else
5762 this_action = BPSTAT_WHAT_STOP_SILENT;
5763 }
5764 else
5765 {
5766 /* Some catchpoints are implemented with breakpoints.
5767 For those, we need to step over the breakpoint. */
5768 if (bs->bp_location_at->loc_type != bp_loc_other)
5769 this_action = BPSTAT_WHAT_SINGLE;
5770 }
5771 break;
5772 case bp_jit_event:
5773 this_action = BPSTAT_WHAT_SINGLE;
5774 break;
5775 case bp_call_dummy:
5776 /* Make sure the action is stop (silent or noisy),
5777 so infrun.c pops the dummy frame. */
5778 retval.call_dummy = STOP_STACK_DUMMY;
5779 this_action = BPSTAT_WHAT_STOP_SILENT;
5780 break;
5781 case bp_std_terminate:
5782 /* Make sure the action is stop (silent or noisy),
5783 so infrun.c pops the dummy frame. */
5784 retval.call_dummy = STOP_STD_TERMINATE;
5785 this_action = BPSTAT_WHAT_STOP_SILENT;
5786 break;
5787 case bp_tracepoint:
5788 case bp_fast_tracepoint:
5789 case bp_static_tracepoint:
5790 /* Tracepoint hits should not be reported back to GDB, and
5791 if one got through somehow, it should have been filtered
5792 out already. */
5793 internal_error (__FILE__, __LINE__,
5794 _("bpstat_what: tracepoint encountered"));
5795 break;
5796 case bp_gnu_ifunc_resolver:
5797 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5798 this_action = BPSTAT_WHAT_SINGLE;
5799 break;
5800 case bp_gnu_ifunc_resolver_return:
5801 /* The breakpoint will be removed, execution will restart from the
5802 PC of the former breakpoint. */
5803 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5804 break;
5805
5806 case bp_dprintf:
5807 if (bs->stop)
5808 this_action = BPSTAT_WHAT_STOP_SILENT;
5809 else
5810 this_action = BPSTAT_WHAT_SINGLE;
5811 break;
5812
5813 default:
5814 internal_error (__FILE__, __LINE__,
5815 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5816 }
5817
5818 retval.main_action = std::max (retval.main_action, this_action);
5819 }
5820
5821 return retval;
5822 }
5823
5824 void
5825 bpstat_run_callbacks (bpstat bs_head)
5826 {
5827 bpstat bs;
5828
5829 for (bs = bs_head; bs != NULL; bs = bs->next)
5830 {
5831 struct breakpoint *b = bs->breakpoint_at;
5832
5833 if (b == NULL)
5834 continue;
5835 switch (b->type)
5836 {
5837 case bp_jit_event:
5838 handle_jit_event (bs->bp_location_at->address);
5839 break;
5840 case bp_gnu_ifunc_resolver:
5841 gnu_ifunc_resolver_stop (b);
5842 break;
5843 case bp_gnu_ifunc_resolver_return:
5844 gnu_ifunc_resolver_return_stop (b);
5845 break;
5846 }
5847 }
5848 }
5849
5850 /* See breakpoint.h. */
5851
5852 bool
5853 bpstat_should_step ()
5854 {
5855 for (breakpoint *b : all_breakpoints ())
5856 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5857 return true;
5858
5859 return false;
5860 }
5861
5862 /* See breakpoint.h. */
5863
5864 bool
5865 bpstat_causes_stop (bpstat bs)
5866 {
5867 for (; bs != NULL; bs = bs->next)
5868 if (bs->stop)
5869 return true;
5870
5871 return false;
5872 }
5873
5874 \f
5875
5876 /* Compute a string of spaces suitable to indent the next line
5877 so it starts at the position corresponding to the table column
5878 named COL_NAME in the currently active table of UIOUT. */
5879
5880 static char *
5881 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5882 {
5883 static char wrap_indent[80];
5884 int i, total_width, width, align;
5885 const char *text;
5886
5887 total_width = 0;
5888 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5889 {
5890 if (strcmp (text, col_name) == 0)
5891 {
5892 gdb_assert (total_width < sizeof wrap_indent);
5893 memset (wrap_indent, ' ', total_width);
5894 wrap_indent[total_width] = 0;
5895
5896 return wrap_indent;
5897 }
5898
5899 total_width += width + 1;
5900 }
5901
5902 return NULL;
5903 }
5904
5905 /* Determine if the locations of this breakpoint will have their conditions
5906 evaluated by the target, host or a mix of both. Returns the following:
5907
5908 "host": Host evals condition.
5909 "host or target": Host or Target evals condition.
5910 "target": Target evals condition.
5911 */
5912
5913 static const char *
5914 bp_condition_evaluator (struct breakpoint *b)
5915 {
5916 struct bp_location *bl;
5917 char host_evals = 0;
5918 char target_evals = 0;
5919
5920 if (!b)
5921 return NULL;
5922
5923 if (!is_breakpoint (b))
5924 return NULL;
5925
5926 if (gdb_evaluates_breakpoint_condition_p ()
5927 || !target_supports_evaluation_of_breakpoint_conditions ())
5928 return condition_evaluation_host;
5929
5930 for (bl = b->loc; bl; bl = bl->next)
5931 {
5932 if (bl->cond_bytecode)
5933 target_evals++;
5934 else
5935 host_evals++;
5936 }
5937
5938 if (host_evals && target_evals)
5939 return condition_evaluation_both;
5940 else if (target_evals)
5941 return condition_evaluation_target;
5942 else
5943 return condition_evaluation_host;
5944 }
5945
5946 /* Determine the breakpoint location's condition evaluator. This is
5947 similar to bp_condition_evaluator, but for locations. */
5948
5949 static const char *
5950 bp_location_condition_evaluator (struct bp_location *bl)
5951 {
5952 if (bl && !is_breakpoint (bl->owner))
5953 return NULL;
5954
5955 if (gdb_evaluates_breakpoint_condition_p ()
5956 || !target_supports_evaluation_of_breakpoint_conditions ())
5957 return condition_evaluation_host;
5958
5959 if (bl && bl->cond_bytecode)
5960 return condition_evaluation_target;
5961 else
5962 return condition_evaluation_host;
5963 }
5964
5965 /* Print the LOC location out of the list of B->LOC locations. */
5966
5967 static void
5968 print_breakpoint_location (struct breakpoint *b,
5969 struct bp_location *loc)
5970 {
5971 struct ui_out *uiout = current_uiout;
5972
5973 scoped_restore_current_program_space restore_pspace;
5974
5975 if (loc != NULL && loc->shlib_disabled)
5976 loc = NULL;
5977
5978 if (loc != NULL)
5979 set_current_program_space (loc->pspace);
5980
5981 if (b->display_canonical)
5982 uiout->field_string ("what", event_location_to_string (b->location.get ()));
5983 else if (loc && loc->symtab)
5984 {
5985 const struct symbol *sym = loc->symbol;
5986
5987 if (sym)
5988 {
5989 uiout->text ("in ");
5990 uiout->field_string ("func", sym->print_name (),
5991 function_name_style.style ());
5992 uiout->text (" ");
5993 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5994 uiout->text ("at ");
5995 }
5996 uiout->field_string ("file",
5997 symtab_to_filename_for_display (loc->symtab),
5998 file_name_style.style ());
5999 uiout->text (":");
6000
6001 if (uiout->is_mi_like_p ())
6002 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
6003
6004 uiout->field_signed ("line", loc->line_number);
6005 }
6006 else if (loc)
6007 {
6008 string_file stb;
6009
6010 print_address_symbolic (loc->gdbarch, loc->address, &stb,
6011 demangle, "");
6012 uiout->field_stream ("at", stb);
6013 }
6014 else
6015 {
6016 uiout->field_string ("pending",
6017 event_location_to_string (b->location.get ()));
6018 /* If extra_string is available, it could be holding a condition
6019 or dprintf arguments. In either case, make sure it is printed,
6020 too, but only for non-MI streams. */
6021 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
6022 {
6023 if (b->type == bp_dprintf)
6024 uiout->text (",");
6025 else
6026 uiout->text (" ");
6027 uiout->text (b->extra_string);
6028 }
6029 }
6030
6031 if (loc && is_breakpoint (b)
6032 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6033 && bp_condition_evaluator (b) == condition_evaluation_both)
6034 {
6035 uiout->text (" (");
6036 uiout->field_string ("evaluated-by",
6037 bp_location_condition_evaluator (loc));
6038 uiout->text (")");
6039 }
6040 }
6041
6042 static const char *
6043 bptype_string (enum bptype type)
6044 {
6045 struct ep_type_description
6046 {
6047 enum bptype type;
6048 const char *description;
6049 };
6050 static struct ep_type_description bptypes[] =
6051 {
6052 {bp_none, "?deleted?"},
6053 {bp_breakpoint, "breakpoint"},
6054 {bp_hardware_breakpoint, "hw breakpoint"},
6055 {bp_single_step, "sw single-step"},
6056 {bp_until, "until"},
6057 {bp_finish, "finish"},
6058 {bp_watchpoint, "watchpoint"},
6059 {bp_hardware_watchpoint, "hw watchpoint"},
6060 {bp_read_watchpoint, "read watchpoint"},
6061 {bp_access_watchpoint, "acc watchpoint"},
6062 {bp_longjmp, "longjmp"},
6063 {bp_longjmp_resume, "longjmp resume"},
6064 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6065 {bp_exception, "exception"},
6066 {bp_exception_resume, "exception resume"},
6067 {bp_step_resume, "step resume"},
6068 {bp_hp_step_resume, "high-priority step resume"},
6069 {bp_watchpoint_scope, "watchpoint scope"},
6070 {bp_call_dummy, "call dummy"},
6071 {bp_std_terminate, "std::terminate"},
6072 {bp_shlib_event, "shlib events"},
6073 {bp_thread_event, "thread events"},
6074 {bp_overlay_event, "overlay events"},
6075 {bp_longjmp_master, "longjmp master"},
6076 {bp_std_terminate_master, "std::terminate master"},
6077 {bp_exception_master, "exception master"},
6078 {bp_catchpoint, "catchpoint"},
6079 {bp_tracepoint, "tracepoint"},
6080 {bp_fast_tracepoint, "fast tracepoint"},
6081 {bp_static_tracepoint, "static tracepoint"},
6082 {bp_dprintf, "dprintf"},
6083 {bp_jit_event, "jit events"},
6084 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6085 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6086 };
6087
6088 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6089 || ((int) type != bptypes[(int) type].type))
6090 internal_error (__FILE__, __LINE__,
6091 _("bptypes table does not describe type #%d."),
6092 (int) type);
6093
6094 return bptypes[(int) type].description;
6095 }
6096
6097 /* For MI, output a field named 'thread-groups' with a list as the value.
6098 For CLI, prefix the list with the string 'inf'. */
6099
6100 static void
6101 output_thread_groups (struct ui_out *uiout,
6102 const char *field_name,
6103 const std::vector<int> &inf_nums,
6104 int mi_only)
6105 {
6106 int is_mi = uiout->is_mi_like_p ();
6107
6108 /* For backward compatibility, don't display inferiors in CLI unless
6109 there are several. Always display them for MI. */
6110 if (!is_mi && mi_only)
6111 return;
6112
6113 ui_out_emit_list list_emitter (uiout, field_name);
6114
6115 for (size_t i = 0; i < inf_nums.size (); i++)
6116 {
6117 if (is_mi)
6118 {
6119 char mi_group[10];
6120
6121 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
6122 uiout->field_string (NULL, mi_group);
6123 }
6124 else
6125 {
6126 if (i == 0)
6127 uiout->text (" inf ");
6128 else
6129 uiout->text (", ");
6130
6131 uiout->text (plongest (inf_nums[i]));
6132 }
6133 }
6134 }
6135
6136 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6137 instead of going via breakpoint_ops::print_one. This makes "maint
6138 info breakpoints" show the software breakpoint locations of
6139 catchpoints, which are considered internal implementation
6140 detail. */
6141
6142 static void
6143 print_one_breakpoint_location (struct breakpoint *b,
6144 struct bp_location *loc,
6145 int loc_number,
6146 struct bp_location **last_loc,
6147 int allflag, bool raw_loc)
6148 {
6149 struct command_line *l;
6150 static char bpenables[] = "nynny";
6151
6152 struct ui_out *uiout = current_uiout;
6153 int header_of_multiple = 0;
6154 int part_of_multiple = (loc != NULL);
6155 struct value_print_options opts;
6156
6157 get_user_print_options (&opts);
6158
6159 gdb_assert (!loc || loc_number != 0);
6160 /* See comment in print_one_breakpoint concerning treatment of
6161 breakpoints with single disabled location. */
6162 if (loc == NULL
6163 && (b->loc != NULL
6164 && (b->loc->next != NULL
6165 || !b->loc->enabled || b->loc->disabled_by_cond)))
6166 header_of_multiple = 1;
6167 if (loc == NULL)
6168 loc = b->loc;
6169
6170 annotate_record ();
6171
6172 /* 1 */
6173 annotate_field (0);
6174 if (part_of_multiple)
6175 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6176 else
6177 uiout->field_signed ("number", b->number);
6178
6179 /* 2 */
6180 annotate_field (1);
6181 if (part_of_multiple)
6182 uiout->field_skip ("type");
6183 else
6184 uiout->field_string ("type", bptype_string (b->type));
6185
6186 /* 3 */
6187 annotate_field (2);
6188 if (part_of_multiple)
6189 uiout->field_skip ("disp");
6190 else
6191 uiout->field_string ("disp", bpdisp_text (b->disposition));
6192
6193 /* 4 */
6194 annotate_field (3);
6195 /* For locations that are disabled because of an invalid condition,
6196 display "N*" on CLI, where "*" refers to a footnote below the
6197 table. For MI, simply display a "N" without a footnote. */
6198 const char *N = (uiout->is_mi_like_p ()) ? "N" : "N*";
6199 if (part_of_multiple)
6200 uiout->field_string ("enabled", (loc->disabled_by_cond ? N
6201 : (loc->enabled ? "y" : "n")));
6202 else
6203 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6204
6205 /* 5 and 6 */
6206 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
6207 b->ops->print_one (b, last_loc);
6208 else
6209 {
6210 if (is_watchpoint (b))
6211 {
6212 struct watchpoint *w = (struct watchpoint *) b;
6213
6214 /* Field 4, the address, is omitted (which makes the columns
6215 not line up too nicely with the headers, but the effect
6216 is relatively readable). */
6217 if (opts.addressprint)
6218 uiout->field_skip ("addr");
6219 annotate_field (5);
6220 uiout->field_string ("what", w->exp_string);
6221 }
6222 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6223 || is_ada_exception_catchpoint (b))
6224 {
6225 if (opts.addressprint)
6226 {
6227 annotate_field (4);
6228 if (header_of_multiple)
6229 uiout->field_string ("addr", "<MULTIPLE>",
6230 metadata_style.style ());
6231 else if (b->loc == NULL || loc->shlib_disabled)
6232 uiout->field_string ("addr", "<PENDING>",
6233 metadata_style.style ());
6234 else
6235 uiout->field_core_addr ("addr",
6236 loc->gdbarch, loc->address);
6237 }
6238 annotate_field (5);
6239 if (!header_of_multiple)
6240 print_breakpoint_location (b, loc);
6241 if (b->loc)
6242 *last_loc = b->loc;
6243 }
6244 }
6245
6246 if (loc != NULL && !header_of_multiple)
6247 {
6248 std::vector<int> inf_nums;
6249 int mi_only = 1;
6250
6251 for (inferior *inf : all_inferiors ())
6252 {
6253 if (inf->pspace == loc->pspace)
6254 inf_nums.push_back (inf->num);
6255 }
6256
6257 /* For backward compatibility, don't display inferiors in CLI unless
6258 there are several. Always display for MI. */
6259 if (allflag
6260 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6261 && (program_spaces.size () > 1
6262 || number_of_inferiors () > 1)
6263 /* LOC is for existing B, it cannot be in
6264 moribund_locations and thus having NULL OWNER. */
6265 && loc->owner->type != bp_catchpoint))
6266 mi_only = 0;
6267 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6268 }
6269
6270 if (!part_of_multiple)
6271 {
6272 if (b->thread != -1)
6273 {
6274 /* FIXME: This seems to be redundant and lost here; see the
6275 "stop only in" line a little further down. */
6276 uiout->text (" thread ");
6277 uiout->field_signed ("thread", b->thread);
6278 }
6279 else if (b->task != 0)
6280 {
6281 uiout->text (" task ");
6282 uiout->field_signed ("task", b->task);
6283 }
6284 }
6285
6286 uiout->text ("\n");
6287
6288 if (!part_of_multiple)
6289 b->ops->print_one_detail (b, uiout);
6290
6291 if (part_of_multiple && frame_id_p (b->frame_id))
6292 {
6293 annotate_field (6);
6294 uiout->text ("\tstop only in stack frame at ");
6295 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6296 the frame ID. */
6297 uiout->field_core_addr ("frame",
6298 b->gdbarch, b->frame_id.stack_addr);
6299 uiout->text ("\n");
6300 }
6301
6302 if (!part_of_multiple && b->cond_string)
6303 {
6304 annotate_field (7);
6305 if (is_tracepoint (b))
6306 uiout->text ("\ttrace only if ");
6307 else
6308 uiout->text ("\tstop only if ");
6309 uiout->field_string ("cond", b->cond_string);
6310
6311 /* Print whether the target is doing the breakpoint's condition
6312 evaluation. If GDB is doing the evaluation, don't print anything. */
6313 if (is_breakpoint (b)
6314 && breakpoint_condition_evaluation_mode ()
6315 == condition_evaluation_target)
6316 {
6317 uiout->message (" (%pF evals)",
6318 string_field ("evaluated-by",
6319 bp_condition_evaluator (b)));
6320 }
6321 uiout->text ("\n");
6322 }
6323
6324 if (!part_of_multiple && b->thread != -1)
6325 {
6326 /* FIXME should make an annotation for this. */
6327 uiout->text ("\tstop only in thread ");
6328 if (uiout->is_mi_like_p ())
6329 uiout->field_signed ("thread", b->thread);
6330 else
6331 {
6332 struct thread_info *thr = find_thread_global_id (b->thread);
6333
6334 uiout->field_string ("thread", print_thread_id (thr));
6335 }
6336 uiout->text ("\n");
6337 }
6338
6339 if (!part_of_multiple)
6340 {
6341 if (b->hit_count)
6342 {
6343 /* FIXME should make an annotation for this. */
6344 if (is_catchpoint (b))
6345 uiout->text ("\tcatchpoint");
6346 else if (is_tracepoint (b))
6347 uiout->text ("\ttracepoint");
6348 else
6349 uiout->text ("\tbreakpoint");
6350 uiout->text (" already hit ");
6351 uiout->field_signed ("times", b->hit_count);
6352 if (b->hit_count == 1)
6353 uiout->text (" time\n");
6354 else
6355 uiout->text (" times\n");
6356 }
6357 else
6358 {
6359 /* Output the count also if it is zero, but only if this is mi. */
6360 if (uiout->is_mi_like_p ())
6361 uiout->field_signed ("times", b->hit_count);
6362 }
6363 }
6364
6365 if (!part_of_multiple && b->ignore_count)
6366 {
6367 annotate_field (8);
6368 uiout->message ("\tignore next %pF hits\n",
6369 signed_field ("ignore", b->ignore_count));
6370 }
6371
6372 /* Note that an enable count of 1 corresponds to "enable once"
6373 behavior, which is reported by the combination of enablement and
6374 disposition, so we don't need to mention it here. */
6375 if (!part_of_multiple && b->enable_count > 1)
6376 {
6377 annotate_field (8);
6378 uiout->text ("\tdisable after ");
6379 /* Tweak the wording to clarify that ignore and enable counts
6380 are distinct, and have additive effect. */
6381 if (b->ignore_count)
6382 uiout->text ("additional ");
6383 else
6384 uiout->text ("next ");
6385 uiout->field_signed ("enable", b->enable_count);
6386 uiout->text (" hits\n");
6387 }
6388
6389 if (!part_of_multiple && is_tracepoint (b))
6390 {
6391 struct tracepoint *tp = (struct tracepoint *) b;
6392
6393 if (tp->traceframe_usage)
6394 {
6395 uiout->text ("\ttrace buffer usage ");
6396 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6397 uiout->text (" bytes\n");
6398 }
6399 }
6400
6401 l = b->commands ? b->commands.get () : NULL;
6402 if (!part_of_multiple && l)
6403 {
6404 annotate_field (9);
6405 ui_out_emit_tuple tuple_emitter (uiout, "script");
6406 print_command_lines (uiout, l, 4);
6407 }
6408
6409 if (is_tracepoint (b))
6410 {
6411 struct tracepoint *t = (struct tracepoint *) b;
6412
6413 if (!part_of_multiple && t->pass_count)
6414 {
6415 annotate_field (10);
6416 uiout->text ("\tpass count ");
6417 uiout->field_signed ("pass", t->pass_count);
6418 uiout->text (" \n");
6419 }
6420
6421 /* Don't display it when tracepoint or tracepoint location is
6422 pending. */
6423 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6424 {
6425 annotate_field (11);
6426
6427 if (uiout->is_mi_like_p ())
6428 uiout->field_string ("installed",
6429 loc->inserted ? "y" : "n");
6430 else
6431 {
6432 if (loc->inserted)
6433 uiout->text ("\t");
6434 else
6435 uiout->text ("\tnot ");
6436 uiout->text ("installed on target\n");
6437 }
6438 }
6439 }
6440
6441 if (uiout->is_mi_like_p () && !part_of_multiple)
6442 {
6443 if (is_watchpoint (b))
6444 {
6445 struct watchpoint *w = (struct watchpoint *) b;
6446
6447 uiout->field_string ("original-location", w->exp_string);
6448 }
6449 else if (b->location != NULL
6450 && event_location_to_string (b->location.get ()) != NULL)
6451 uiout->field_string ("original-location",
6452 event_location_to_string (b->location.get ()));
6453 }
6454 }
6455
6456 /* See breakpoint.h. */
6457
6458 bool fix_multi_location_breakpoint_output_globally = false;
6459
6460 static void
6461 print_one_breakpoint (struct breakpoint *b,
6462 struct bp_location **last_loc,
6463 int allflag)
6464 {
6465 struct ui_out *uiout = current_uiout;
6466 bool use_fixed_output
6467 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6468 || fix_multi_location_breakpoint_output_globally);
6469
6470 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6471 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
6472
6473 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6474 are outside. */
6475 if (!use_fixed_output)
6476 bkpt_tuple_emitter.reset ();
6477
6478 /* If this breakpoint has custom print function,
6479 it's already printed. Otherwise, print individual
6480 locations, if any. */
6481 if (b->ops == NULL
6482 || b->ops->print_one == NULL
6483 || allflag)
6484 {
6485 /* If breakpoint has a single location that is disabled, we
6486 print it as if it had several locations, since otherwise it's
6487 hard to represent "breakpoint enabled, location disabled"
6488 situation.
6489
6490 Note that while hardware watchpoints have several locations
6491 internally, that's not a property exposed to users.
6492
6493 Likewise, while catchpoints may be implemented with
6494 breakpoints (e.g., catch throw), that's not a property
6495 exposed to users. We do however display the internal
6496 breakpoint locations with "maint info breakpoints". */
6497 if (!is_hardware_watchpoint (b)
6498 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6499 || is_ada_exception_catchpoint (b))
6500 && (allflag
6501 || (b->loc && (b->loc->next
6502 || !b->loc->enabled
6503 || b->loc->disabled_by_cond))))
6504 {
6505 gdb::optional<ui_out_emit_list> locations_list;
6506
6507 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6508 MI record. For later versions, place breakpoint locations in a
6509 list. */
6510 if (uiout->is_mi_like_p () && use_fixed_output)
6511 locations_list.emplace (uiout, "locations");
6512
6513 int n = 1;
6514 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next, ++n)
6515 {
6516 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6517 print_one_breakpoint_location (b, loc, n, last_loc,
6518 allflag, allflag);
6519 }
6520 }
6521 }
6522 }
6523
6524 static int
6525 breakpoint_address_bits (struct breakpoint *b)
6526 {
6527 int print_address_bits = 0;
6528 struct bp_location *loc;
6529
6530 /* Software watchpoints that aren't watching memory don't have an
6531 address to print. */
6532 if (is_no_memory_software_watchpoint (b))
6533 return 0;
6534
6535 for (loc = b->loc; loc; loc = loc->next)
6536 {
6537 int addr_bit;
6538
6539 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6540 if (addr_bit > print_address_bits)
6541 print_address_bits = addr_bit;
6542 }
6543
6544 return print_address_bits;
6545 }
6546
6547 /* See breakpoint.h. */
6548
6549 void
6550 print_breakpoint (breakpoint *b)
6551 {
6552 struct bp_location *dummy_loc = NULL;
6553 print_one_breakpoint (b, &dummy_loc, 0);
6554 }
6555
6556 /* Return true if this breakpoint was set by the user, false if it is
6557 internal or momentary. */
6558
6559 int
6560 user_breakpoint_p (struct breakpoint *b)
6561 {
6562 return b->number > 0;
6563 }
6564
6565 /* See breakpoint.h. */
6566
6567 int
6568 pending_breakpoint_p (struct breakpoint *b)
6569 {
6570 return b->loc == NULL;
6571 }
6572
6573 /* Print information on breakpoints (including watchpoints and tracepoints).
6574
6575 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6576 understood by number_or_range_parser. Only breakpoints included in this
6577 list are then printed.
6578
6579 If SHOW_INTERNAL is true, print internal breakpoints.
6580
6581 If FILTER is non-NULL, call it on each breakpoint and only include the
6582 ones for which it returns true.
6583
6584 Return the total number of breakpoints listed. */
6585
6586 static int
6587 breakpoint_1 (const char *bp_num_list, bool show_internal,
6588 bool (*filter) (const struct breakpoint *))
6589 {
6590 struct bp_location *last_loc = NULL;
6591 int nr_printable_breakpoints;
6592 struct value_print_options opts;
6593 int print_address_bits = 0;
6594 int print_type_col_width = 14;
6595 struct ui_out *uiout = current_uiout;
6596 bool has_disabled_by_cond_location = false;
6597
6598 get_user_print_options (&opts);
6599
6600 /* Compute the number of rows in the table, as well as the size
6601 required for address fields. */
6602 nr_printable_breakpoints = 0;
6603 for (breakpoint *b : all_breakpoints ())
6604 {
6605 /* If we have a filter, only list the breakpoints it accepts. */
6606 if (filter && !filter (b))
6607 continue;
6608
6609 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6610 accept. Skip the others. */
6611 if (bp_num_list != NULL && *bp_num_list != '\0')
6612 {
6613 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6614 continue;
6615 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6616 continue;
6617 }
6618
6619 if (show_internal || user_breakpoint_p (b))
6620 {
6621 int addr_bit, type_len;
6622
6623 addr_bit = breakpoint_address_bits (b);
6624 if (addr_bit > print_address_bits)
6625 print_address_bits = addr_bit;
6626
6627 type_len = strlen (bptype_string (b->type));
6628 if (type_len > print_type_col_width)
6629 print_type_col_width = type_len;
6630
6631 nr_printable_breakpoints++;
6632 }
6633 }
6634
6635 {
6636 ui_out_emit_table table_emitter (uiout,
6637 opts.addressprint ? 6 : 5,
6638 nr_printable_breakpoints,
6639 "BreakpointTable");
6640
6641 if (nr_printable_breakpoints > 0)
6642 annotate_breakpoints_headers ();
6643 if (nr_printable_breakpoints > 0)
6644 annotate_field (0);
6645 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6646 if (nr_printable_breakpoints > 0)
6647 annotate_field (1);
6648 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6649 if (nr_printable_breakpoints > 0)
6650 annotate_field (2);
6651 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6652 if (nr_printable_breakpoints > 0)
6653 annotate_field (3);
6654 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6655 if (opts.addressprint)
6656 {
6657 if (nr_printable_breakpoints > 0)
6658 annotate_field (4);
6659 if (print_address_bits <= 32)
6660 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6661 else
6662 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6663 }
6664 if (nr_printable_breakpoints > 0)
6665 annotate_field (5);
6666 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6667 uiout->table_body ();
6668 if (nr_printable_breakpoints > 0)
6669 annotate_breakpoints_table ();
6670
6671 for (breakpoint *b : all_breakpoints ())
6672 {
6673 QUIT;
6674 /* If we have a filter, only list the breakpoints it accepts. */
6675 if (filter && !filter (b))
6676 continue;
6677
6678 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6679 accept. Skip the others. */
6680
6681 if (bp_num_list != NULL && *bp_num_list != '\0')
6682 {
6683 if (show_internal) /* maintenance info breakpoint */
6684 {
6685 if (parse_and_eval_long (bp_num_list) != b->number)
6686 continue;
6687 }
6688 else /* all others */
6689 {
6690 if (!number_is_in_list (bp_num_list, b->number))
6691 continue;
6692 }
6693 }
6694 /* We only print out user settable breakpoints unless the
6695 show_internal is set. */
6696 if (show_internal || user_breakpoint_p (b))
6697 {
6698 print_one_breakpoint (b, &last_loc, show_internal);
6699 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
6700 if (loc->disabled_by_cond)
6701 has_disabled_by_cond_location = true;
6702 }
6703 }
6704 }
6705
6706 if (nr_printable_breakpoints == 0)
6707 {
6708 /* If there's a filter, let the caller decide how to report
6709 empty list. */
6710 if (!filter)
6711 {
6712 if (bp_num_list == NULL || *bp_num_list == '\0')
6713 uiout->message ("No breakpoints or watchpoints.\n");
6714 else
6715 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6716 bp_num_list);
6717 }
6718 }
6719 else
6720 {
6721 if (last_loc && !server_command)
6722 set_next_address (last_loc->gdbarch, last_loc->address);
6723
6724 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
6725 uiout->message (_("(*): Breakpoint condition is invalid at this "
6726 "location.\n"));
6727 }
6728
6729 /* FIXME? Should this be moved up so that it is only called when
6730 there have been breakpoints? */
6731 annotate_breakpoints_table_end ();
6732
6733 return nr_printable_breakpoints;
6734 }
6735
6736 /* Display the value of default-collect in a way that is generally
6737 compatible with the breakpoint list. */
6738
6739 static void
6740 default_collect_info (void)
6741 {
6742 struct ui_out *uiout = current_uiout;
6743
6744 /* If it has no value (which is frequently the case), say nothing; a
6745 message like "No default-collect." gets in user's face when it's
6746 not wanted. */
6747 if (!*default_collect)
6748 return;
6749
6750 /* The following phrase lines up nicely with per-tracepoint collect
6751 actions. */
6752 uiout->text ("default collect ");
6753 uiout->field_string ("default-collect", default_collect);
6754 uiout->text (" \n");
6755 }
6756
6757 static void
6758 info_breakpoints_command (const char *args, int from_tty)
6759 {
6760 breakpoint_1 (args, false, NULL);
6761
6762 default_collect_info ();
6763 }
6764
6765 static void
6766 info_watchpoints_command (const char *args, int from_tty)
6767 {
6768 int num_printed = breakpoint_1 (args, false, is_watchpoint);
6769 struct ui_out *uiout = current_uiout;
6770
6771 if (num_printed == 0)
6772 {
6773 if (args == NULL || *args == '\0')
6774 uiout->message ("No watchpoints.\n");
6775 else
6776 uiout->message ("No watchpoint matching '%s'.\n", args);
6777 }
6778 }
6779
6780 static void
6781 maintenance_info_breakpoints (const char *args, int from_tty)
6782 {
6783 breakpoint_1 (args, true, NULL);
6784
6785 default_collect_info ();
6786 }
6787
6788 static int
6789 breakpoint_has_pc (struct breakpoint *b,
6790 struct program_space *pspace,
6791 CORE_ADDR pc, struct obj_section *section)
6792 {
6793 struct bp_location *bl = b->loc;
6794
6795 for (; bl; bl = bl->next)
6796 {
6797 if (bl->pspace == pspace
6798 && bl->address == pc
6799 && (!overlay_debugging || bl->section == section))
6800 return 1;
6801 }
6802 return 0;
6803 }
6804
6805 /* Print a message describing any user-breakpoints set at PC. This
6806 concerns with logical breakpoints, so we match program spaces, not
6807 address spaces. */
6808
6809 static void
6810 describe_other_breakpoints (struct gdbarch *gdbarch,
6811 struct program_space *pspace, CORE_ADDR pc,
6812 struct obj_section *section, int thread)
6813 {
6814 int others = 0;
6815
6816 for (breakpoint *b : all_breakpoints ())
6817 others += (user_breakpoint_p (b)
6818 && breakpoint_has_pc (b, pspace, pc, section));
6819
6820 if (others > 0)
6821 {
6822 if (others == 1)
6823 printf_filtered (_("Note: breakpoint "));
6824 else /* if (others == ???) */
6825 printf_filtered (_("Note: breakpoints "));
6826 for (breakpoint *b : all_breakpoints ())
6827 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6828 {
6829 others--;
6830 printf_filtered ("%d", b->number);
6831 if (b->thread == -1 && thread != -1)
6832 printf_filtered (" (all threads)");
6833 else if (b->thread != -1)
6834 printf_filtered (" (thread %d)", b->thread);
6835 printf_filtered ("%s%s ",
6836 ((b->enable_state == bp_disabled
6837 || b->enable_state == bp_call_disabled)
6838 ? " (disabled)"
6839 : ""),
6840 (others > 1) ? ","
6841 : ((others == 1) ? " and" : ""));
6842 }
6843 current_uiout->message (_("also set at pc %ps.\n"),
6844 styled_string (address_style.style (),
6845 paddress (gdbarch, pc)));
6846 }
6847 }
6848 \f
6849
6850 /* Return true iff it is meaningful to use the address member of LOC.
6851 For some breakpoint types, the locations' address members are
6852 irrelevant and it makes no sense to attempt to compare them to
6853 other addresses (or use them for any other purpose either).
6854
6855 More specifically, software watchpoints and catchpoints that are
6856 not backed by breakpoints always have a zero valued location
6857 address and we don't want to mark breakpoints of any of these types
6858 to be a duplicate of an actual breakpoint location at address
6859 zero. */
6860
6861 static bool
6862 bl_address_is_meaningful (bp_location *loc)
6863 {
6864 return loc->loc_type != bp_loc_other;
6865 }
6866
6867 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6868 true if LOC1 and LOC2 represent the same watchpoint location. */
6869
6870 static int
6871 watchpoint_locations_match (struct bp_location *loc1,
6872 struct bp_location *loc2)
6873 {
6874 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6875 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6876
6877 /* Both of them must exist. */
6878 gdb_assert (w1 != NULL);
6879 gdb_assert (w2 != NULL);
6880
6881 /* If the target can evaluate the condition expression in hardware,
6882 then we we need to insert both watchpoints even if they are at
6883 the same place. Otherwise the watchpoint will only trigger when
6884 the condition of whichever watchpoint was inserted evaluates to
6885 true, not giving a chance for GDB to check the condition of the
6886 other watchpoint. */
6887 if ((w1->cond_exp
6888 && target_can_accel_watchpoint_condition (loc1->address,
6889 loc1->length,
6890 loc1->watchpoint_type,
6891 w1->cond_exp.get ()))
6892 || (w2->cond_exp
6893 && target_can_accel_watchpoint_condition (loc2->address,
6894 loc2->length,
6895 loc2->watchpoint_type,
6896 w2->cond_exp.get ())))
6897 return 0;
6898
6899 /* Note that this checks the owner's type, not the location's. In
6900 case the target does not support read watchpoints, but does
6901 support access watchpoints, we'll have bp_read_watchpoint
6902 watchpoints with hw_access locations. Those should be considered
6903 duplicates of hw_read locations. The hw_read locations will
6904 become hw_access locations later. */
6905 return (loc1->owner->type == loc2->owner->type
6906 && loc1->pspace->aspace == loc2->pspace->aspace
6907 && loc1->address == loc2->address
6908 && loc1->length == loc2->length);
6909 }
6910
6911 /* See breakpoint.h. */
6912
6913 int
6914 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6915 const address_space *aspace2, CORE_ADDR addr2)
6916 {
6917 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6918 || aspace1 == aspace2)
6919 && addr1 == addr2);
6920 }
6921
6922 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6923 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6924 matches ASPACE2. On targets that have global breakpoints, the address
6925 space doesn't really matter. */
6926
6927 static int
6928 breakpoint_address_match_range (const address_space *aspace1,
6929 CORE_ADDR addr1,
6930 int len1, const address_space *aspace2,
6931 CORE_ADDR addr2)
6932 {
6933 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6934 || aspace1 == aspace2)
6935 && addr2 >= addr1 && addr2 < addr1 + len1);
6936 }
6937
6938 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6939 a ranged breakpoint. In most targets, a match happens only if ASPACE
6940 matches the breakpoint's address space. On targets that have global
6941 breakpoints, the address space doesn't really matter. */
6942
6943 static int
6944 breakpoint_location_address_match (struct bp_location *bl,
6945 const address_space *aspace,
6946 CORE_ADDR addr)
6947 {
6948 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6949 aspace, addr)
6950 || (bl->length
6951 && breakpoint_address_match_range (bl->pspace->aspace,
6952 bl->address, bl->length,
6953 aspace, addr)));
6954 }
6955
6956 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6957 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6958 match happens only if ASPACE matches the breakpoint's address
6959 space. On targets that have global breakpoints, the address space
6960 doesn't really matter. */
6961
6962 static int
6963 breakpoint_location_address_range_overlap (struct bp_location *bl,
6964 const address_space *aspace,
6965 CORE_ADDR addr, int len)
6966 {
6967 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6968 || bl->pspace->aspace == aspace)
6969 {
6970 int bl_len = bl->length != 0 ? bl->length : 1;
6971
6972 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6973 return 1;
6974 }
6975 return 0;
6976 }
6977
6978 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6979 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6980 true, otherwise returns false. */
6981
6982 static int
6983 tracepoint_locations_match (struct bp_location *loc1,
6984 struct bp_location *loc2)
6985 {
6986 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6987 /* Since tracepoint locations are never duplicated with others', tracepoint
6988 locations at the same address of different tracepoints are regarded as
6989 different locations. */
6990 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6991 else
6992 return 0;
6993 }
6994
6995 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6996 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
6997 the same location. If SW_HW_BPS_MATCH is true, then software
6998 breakpoint locations and hardware breakpoint locations match,
6999 otherwise they don't. */
7000
7001 static int
7002 breakpoint_locations_match (struct bp_location *loc1,
7003 struct bp_location *loc2,
7004 bool sw_hw_bps_match)
7005 {
7006 int hw_point1, hw_point2;
7007
7008 /* Both of them must not be in moribund_locations. */
7009 gdb_assert (loc1->owner != NULL);
7010 gdb_assert (loc2->owner != NULL);
7011
7012 hw_point1 = is_hardware_watchpoint (loc1->owner);
7013 hw_point2 = is_hardware_watchpoint (loc2->owner);
7014
7015 if (hw_point1 != hw_point2)
7016 return 0;
7017 else if (hw_point1)
7018 return watchpoint_locations_match (loc1, loc2);
7019 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7020 return tracepoint_locations_match (loc1, loc2);
7021 else
7022 /* We compare bp_location.length in order to cover ranged
7023 breakpoints. Keep this in sync with
7024 bp_location_is_less_than. */
7025 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7026 loc2->pspace->aspace, loc2->address)
7027 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
7028 && loc1->length == loc2->length);
7029 }
7030
7031 static void
7032 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7033 int bnum, int have_bnum)
7034 {
7035 /* The longest string possibly returned by hex_string_custom
7036 is 50 chars. These must be at least that big for safety. */
7037 char astr1[64];
7038 char astr2[64];
7039
7040 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7041 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7042 if (have_bnum)
7043 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7044 bnum, astr1, astr2);
7045 else
7046 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7047 }
7048
7049 /* Adjust a breakpoint's address to account for architectural
7050 constraints on breakpoint placement. Return the adjusted address.
7051 Note: Very few targets require this kind of adjustment. For most
7052 targets, this function is simply the identity function. */
7053
7054 static CORE_ADDR
7055 adjust_breakpoint_address (struct gdbarch *gdbarch,
7056 CORE_ADDR bpaddr, enum bptype bptype)
7057 {
7058 if (bptype == bp_watchpoint
7059 || bptype == bp_hardware_watchpoint
7060 || bptype == bp_read_watchpoint
7061 || bptype == bp_access_watchpoint
7062 || bptype == bp_catchpoint)
7063 {
7064 /* Watchpoints and the various bp_catch_* eventpoints should not
7065 have their addresses modified. */
7066 return bpaddr;
7067 }
7068 else if (bptype == bp_single_step)
7069 {
7070 /* Single-step breakpoints should not have their addresses
7071 modified. If there's any architectural constrain that
7072 applies to this address, then it should have already been
7073 taken into account when the breakpoint was created in the
7074 first place. If we didn't do this, stepping through e.g.,
7075 Thumb-2 IT blocks would break. */
7076 return bpaddr;
7077 }
7078 else
7079 {
7080 CORE_ADDR adjusted_bpaddr = bpaddr;
7081
7082 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7083 {
7084 /* Some targets have architectural constraints on the placement
7085 of breakpoint instructions. Obtain the adjusted address. */
7086 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7087 }
7088
7089 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
7090
7091 /* An adjusted breakpoint address can significantly alter
7092 a user's expectations. Print a warning if an adjustment
7093 is required. */
7094 if (adjusted_bpaddr != bpaddr)
7095 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7096
7097 return adjusted_bpaddr;
7098 }
7099 }
7100
7101 static bp_loc_type
7102 bp_location_from_bp_type (bptype type)
7103 {
7104 switch (type)
7105 {
7106 case bp_breakpoint:
7107 case bp_single_step:
7108 case bp_until:
7109 case bp_finish:
7110 case bp_longjmp:
7111 case bp_longjmp_resume:
7112 case bp_longjmp_call_dummy:
7113 case bp_exception:
7114 case bp_exception_resume:
7115 case bp_step_resume:
7116 case bp_hp_step_resume:
7117 case bp_watchpoint_scope:
7118 case bp_call_dummy:
7119 case bp_std_terminate:
7120 case bp_shlib_event:
7121 case bp_thread_event:
7122 case bp_overlay_event:
7123 case bp_jit_event:
7124 case bp_longjmp_master:
7125 case bp_std_terminate_master:
7126 case bp_exception_master:
7127 case bp_gnu_ifunc_resolver:
7128 case bp_gnu_ifunc_resolver_return:
7129 case bp_dprintf:
7130 return bp_loc_software_breakpoint;
7131 case bp_hardware_breakpoint:
7132 return bp_loc_hardware_breakpoint;
7133 case bp_hardware_watchpoint:
7134 case bp_read_watchpoint:
7135 case bp_access_watchpoint:
7136 return bp_loc_hardware_watchpoint;
7137 case bp_watchpoint:
7138 case bp_catchpoint:
7139 case bp_tracepoint:
7140 case bp_fast_tracepoint:
7141 case bp_static_tracepoint:
7142 return bp_loc_other;
7143 default:
7144 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7145 }
7146 }
7147
7148 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7149 {
7150 this->owner = owner;
7151 this->cond_bytecode = NULL;
7152 this->shlib_disabled = 0;
7153 this->enabled = 1;
7154 this->disabled_by_cond = false;
7155
7156 this->loc_type = type;
7157
7158 if (this->loc_type == bp_loc_software_breakpoint
7159 || this->loc_type == bp_loc_hardware_breakpoint)
7160 mark_breakpoint_location_modified (this);
7161
7162 incref ();
7163 }
7164
7165 bp_location::bp_location (breakpoint *owner)
7166 : bp_location::bp_location (owner,
7167 bp_location_from_bp_type (owner->type))
7168 {
7169 }
7170
7171 /* Allocate a struct bp_location. */
7172
7173 static struct bp_location *
7174 allocate_bp_location (struct breakpoint *bpt)
7175 {
7176 return bpt->ops->allocate_location (bpt);
7177 }
7178
7179 /* Decrement reference count. If the reference count reaches 0,
7180 destroy the bp_location. Sets *BLP to NULL. */
7181
7182 static void
7183 decref_bp_location (struct bp_location **blp)
7184 {
7185 bp_location_ref_policy::decref (*blp);
7186 *blp = NULL;
7187 }
7188
7189 /* Add breakpoint B at the end of the global breakpoint chain. */
7190
7191 static breakpoint *
7192 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7193 {
7194 struct breakpoint *b1;
7195 struct breakpoint *result = b.get ();
7196
7197 /* Add this breakpoint to the end of the chain so that a list of
7198 breakpoints will come out in order of increasing numbers. */
7199
7200 b1 = breakpoint_chain;
7201 if (b1 == 0)
7202 breakpoint_chain = b.release ();
7203 else
7204 {
7205 while (b1->next)
7206 b1 = b1->next;
7207 b1->next = b.release ();
7208 }
7209
7210 return result;
7211 }
7212
7213 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7214
7215 static void
7216 init_raw_breakpoint_without_location (struct breakpoint *b,
7217 struct gdbarch *gdbarch,
7218 enum bptype bptype,
7219 const struct breakpoint_ops *ops)
7220 {
7221 gdb_assert (ops != NULL);
7222
7223 b->ops = ops;
7224 b->type = bptype;
7225 b->gdbarch = gdbarch;
7226 b->language = current_language->la_language;
7227 b->input_radix = input_radix;
7228 b->related_breakpoint = b;
7229 }
7230
7231 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7232 that has type BPTYPE and has no locations as yet. */
7233
7234 static struct breakpoint *
7235 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7236 enum bptype bptype,
7237 const struct breakpoint_ops *ops)
7238 {
7239 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7240
7241 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7242 return add_to_breakpoint_chain (std::move (b));
7243 }
7244
7245 /* Initialize loc->function_name. */
7246
7247 static void
7248 set_breakpoint_location_function (struct bp_location *loc)
7249 {
7250 gdb_assert (loc->owner != NULL);
7251
7252 if (loc->owner->type == bp_breakpoint
7253 || loc->owner->type == bp_hardware_breakpoint
7254 || is_tracepoint (loc->owner))
7255 {
7256 const char *function_name;
7257
7258 if (loc->msymbol != NULL
7259 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
7260 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
7261 {
7262 struct breakpoint *b = loc->owner;
7263
7264 function_name = loc->msymbol->linkage_name ();
7265
7266 if (b->type == bp_breakpoint && b->loc == loc
7267 && loc->next == NULL && b->related_breakpoint == b)
7268 {
7269 /* Create only the whole new breakpoint of this type but do not
7270 mess more complicated breakpoints with multiple locations. */
7271 b->type = bp_gnu_ifunc_resolver;
7272 /* Remember the resolver's address for use by the return
7273 breakpoint. */
7274 loc->related_address = loc->address;
7275 }
7276 }
7277 else
7278 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7279
7280 if (function_name)
7281 loc->function_name = xstrdup (function_name);
7282 }
7283 }
7284
7285 /* Attempt to determine architecture of location identified by SAL. */
7286 struct gdbarch *
7287 get_sal_arch (struct symtab_and_line sal)
7288 {
7289 if (sal.section)
7290 return sal.section->objfile->arch ();
7291 if (sal.symtab)
7292 return SYMTAB_OBJFILE (sal.symtab)->arch ();
7293
7294 return NULL;
7295 }
7296
7297 /* Low level routine for partially initializing a breakpoint of type
7298 BPTYPE. The newly created breakpoint's address, section, source
7299 file name, and line number are provided by SAL.
7300
7301 It is expected that the caller will complete the initialization of
7302 the newly created breakpoint struct as well as output any status
7303 information regarding the creation of a new breakpoint. */
7304
7305 static void
7306 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7307 struct symtab_and_line sal, enum bptype bptype,
7308 const struct breakpoint_ops *ops)
7309 {
7310 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7311
7312 add_location_to_breakpoint (b, &sal);
7313
7314 if (bptype != bp_catchpoint)
7315 gdb_assert (sal.pspace != NULL);
7316
7317 /* Store the program space that was used to set the breakpoint,
7318 except for ordinary breakpoints, which are independent of the
7319 program space. */
7320 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7321 b->pspace = sal.pspace;
7322 }
7323
7324 /* set_raw_breakpoint is a low level routine for allocating and
7325 partially initializing a breakpoint of type BPTYPE. The newly
7326 created breakpoint's address, section, source file name, and line
7327 number are provided by SAL. The newly created and partially
7328 initialized breakpoint is added to the breakpoint chain and
7329 is also returned as the value of this function.
7330
7331 It is expected that the caller will complete the initialization of
7332 the newly created breakpoint struct as well as output any status
7333 information regarding the creation of a new breakpoint. In
7334 particular, set_raw_breakpoint does NOT set the breakpoint
7335 number! Care should be taken to not allow an error to occur
7336 prior to completing the initialization of the breakpoint. If this
7337 should happen, a bogus breakpoint will be left on the chain. */
7338
7339 struct breakpoint *
7340 set_raw_breakpoint (struct gdbarch *gdbarch,
7341 struct symtab_and_line sal, enum bptype bptype,
7342 const struct breakpoint_ops *ops)
7343 {
7344 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7345
7346 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7347 return add_to_breakpoint_chain (std::move (b));
7348 }
7349
7350 /* Call this routine when stepping and nexting to enable a breakpoint
7351 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7352 initiated the operation. */
7353
7354 void
7355 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7356 {
7357 struct breakpoint *b, *b_tmp;
7358 int thread = tp->global_num;
7359
7360 /* To avoid having to rescan all objfile symbols at every step,
7361 we maintain a list of continually-inserted but always disabled
7362 longjmp "master" breakpoints. Here, we simply create momentary
7363 clones of those and enable them for the requested thread. */
7364 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7365 if (b->pspace == current_program_space
7366 && (b->type == bp_longjmp_master
7367 || b->type == bp_exception_master))
7368 {
7369 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7370 struct breakpoint *clone;
7371
7372 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7373 after their removal. */
7374 clone = momentary_breakpoint_from_master (b, type,
7375 &momentary_breakpoint_ops, 1);
7376 clone->thread = thread;
7377 }
7378
7379 tp->initiating_frame = frame;
7380 }
7381
7382 /* Delete all longjmp breakpoints from THREAD. */
7383 void
7384 delete_longjmp_breakpoint (int thread)
7385 {
7386 struct breakpoint *b, *b_tmp;
7387
7388 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7389 if (b->type == bp_longjmp || b->type == bp_exception)
7390 {
7391 if (b->thread == thread)
7392 delete_breakpoint (b);
7393 }
7394 }
7395
7396 void
7397 delete_longjmp_breakpoint_at_next_stop (int thread)
7398 {
7399 struct breakpoint *b, *b_tmp;
7400
7401 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7402 if (b->type == bp_longjmp || b->type == bp_exception)
7403 {
7404 if (b->thread == thread)
7405 b->disposition = disp_del_at_next_stop;
7406 }
7407 }
7408
7409 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7410 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7411 pointer to any of them. Return NULL if this system cannot place longjmp
7412 breakpoints. */
7413
7414 struct breakpoint *
7415 set_longjmp_breakpoint_for_call_dummy (void)
7416 {
7417 breakpoint *retval = nullptr;
7418
7419 for (breakpoint *b : all_breakpoints ())
7420 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7421 {
7422 struct breakpoint *new_b;
7423
7424 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7425 &momentary_breakpoint_ops,
7426 1);
7427 new_b->thread = inferior_thread ()->global_num;
7428
7429 /* Link NEW_B into the chain of RETVAL breakpoints. */
7430
7431 gdb_assert (new_b->related_breakpoint == new_b);
7432 if (retval == NULL)
7433 retval = new_b;
7434 new_b->related_breakpoint = retval;
7435 while (retval->related_breakpoint != new_b->related_breakpoint)
7436 retval = retval->related_breakpoint;
7437 retval->related_breakpoint = new_b;
7438 }
7439
7440 return retval;
7441 }
7442
7443 /* Verify all existing dummy frames and their associated breakpoints for
7444 TP. Remove those which can no longer be found in the current frame
7445 stack.
7446
7447 You should call this function only at places where it is safe to currently
7448 unwind the whole stack. Failed stack unwind would discard live dummy
7449 frames. */
7450
7451 void
7452 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7453 {
7454 struct breakpoint *b, *b_tmp;
7455
7456 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7457 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7458 {
7459 struct breakpoint *dummy_b = b->related_breakpoint;
7460
7461 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7462 dummy_b = dummy_b->related_breakpoint;
7463 if (dummy_b->type != bp_call_dummy
7464 || frame_find_by_id (dummy_b->frame_id) != NULL)
7465 continue;
7466
7467 dummy_frame_discard (dummy_b->frame_id, tp);
7468
7469 while (b->related_breakpoint != b)
7470 {
7471 if (b_tmp == b->related_breakpoint)
7472 b_tmp = b->related_breakpoint->next;
7473 delete_breakpoint (b->related_breakpoint);
7474 }
7475 delete_breakpoint (b);
7476 }
7477 }
7478
7479 void
7480 enable_overlay_breakpoints (void)
7481 {
7482 for (breakpoint *b : all_breakpoints ())
7483 if (b->type == bp_overlay_event)
7484 {
7485 b->enable_state = bp_enabled;
7486 update_global_location_list (UGLL_MAY_INSERT);
7487 overlay_events_enabled = 1;
7488 }
7489 }
7490
7491 void
7492 disable_overlay_breakpoints (void)
7493 {
7494 for (breakpoint *b : all_breakpoints ())
7495 if (b->type == bp_overlay_event)
7496 {
7497 b->enable_state = bp_disabled;
7498 update_global_location_list (UGLL_DONT_INSERT);
7499 overlay_events_enabled = 0;
7500 }
7501 }
7502
7503 /* Set an active std::terminate breakpoint for each std::terminate
7504 master breakpoint. */
7505 void
7506 set_std_terminate_breakpoint (void)
7507 {
7508 struct breakpoint *b, *b_tmp;
7509
7510 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7511 if (b->pspace == current_program_space
7512 && b->type == bp_std_terminate_master)
7513 {
7514 momentary_breakpoint_from_master (b, bp_std_terminate,
7515 &momentary_breakpoint_ops, 1);
7516 }
7517 }
7518
7519 /* Delete all the std::terminate breakpoints. */
7520 void
7521 delete_std_terminate_breakpoint (void)
7522 {
7523 struct breakpoint *b, *b_tmp;
7524
7525 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7526 if (b->type == bp_std_terminate)
7527 delete_breakpoint (b);
7528 }
7529
7530 struct breakpoint *
7531 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7532 {
7533 struct breakpoint *b;
7534
7535 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7536 &internal_breakpoint_ops);
7537
7538 b->enable_state = bp_enabled;
7539 /* location has to be used or breakpoint_re_set will delete me. */
7540 b->location = new_address_location (b->loc->address, NULL, 0);
7541
7542 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7543
7544 return b;
7545 }
7546
7547 struct lang_and_radix
7548 {
7549 enum language lang;
7550 int radix;
7551 };
7552
7553 /* Create a breakpoint for JIT code registration and unregistration. */
7554
7555 struct breakpoint *
7556 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7557 {
7558 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7559 &internal_breakpoint_ops);
7560 }
7561
7562 /* Remove JIT code registration and unregistration breakpoint(s). */
7563
7564 void
7565 remove_jit_event_breakpoints (void)
7566 {
7567 struct breakpoint *b, *b_tmp;
7568
7569 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7570 if (b->type == bp_jit_event
7571 && b->loc->pspace == current_program_space)
7572 delete_breakpoint (b);
7573 }
7574
7575 void
7576 remove_solib_event_breakpoints (void)
7577 {
7578 struct breakpoint *b, *b_tmp;
7579
7580 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7581 if (b->type == bp_shlib_event
7582 && b->loc->pspace == current_program_space)
7583 delete_breakpoint (b);
7584 }
7585
7586 /* See breakpoint.h. */
7587
7588 void
7589 remove_solib_event_breakpoints_at_next_stop (void)
7590 {
7591 struct breakpoint *b, *b_tmp;
7592
7593 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7594 if (b->type == bp_shlib_event
7595 && b->loc->pspace == current_program_space)
7596 b->disposition = disp_del_at_next_stop;
7597 }
7598
7599 /* Helper for create_solib_event_breakpoint /
7600 create_and_insert_solib_event_breakpoint. Allows specifying which
7601 INSERT_MODE to pass through to update_global_location_list. */
7602
7603 static struct breakpoint *
7604 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7605 enum ugll_insert_mode insert_mode)
7606 {
7607 struct breakpoint *b;
7608
7609 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7610 &internal_breakpoint_ops);
7611 update_global_location_list_nothrow (insert_mode);
7612 return b;
7613 }
7614
7615 struct breakpoint *
7616 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7617 {
7618 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7619 }
7620
7621 /* See breakpoint.h. */
7622
7623 struct breakpoint *
7624 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7625 {
7626 struct breakpoint *b;
7627
7628 /* Explicitly tell update_global_location_list to insert
7629 locations. */
7630 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7631 if (!b->loc->inserted)
7632 {
7633 delete_breakpoint (b);
7634 return NULL;
7635 }
7636 return b;
7637 }
7638
7639 /* Disable any breakpoints that are on code in shared libraries. Only
7640 apply to enabled breakpoints, disabled ones can just stay disabled. */
7641
7642 void
7643 disable_breakpoints_in_shlibs (void)
7644 {
7645 struct bp_location *loc, **locp_tmp;
7646
7647 ALL_BP_LOCATIONS (loc, locp_tmp)
7648 {
7649 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7650 struct breakpoint *b = loc->owner;
7651
7652 /* We apply the check to all breakpoints, including disabled for
7653 those with loc->duplicate set. This is so that when breakpoint
7654 becomes enabled, or the duplicate is removed, gdb will try to
7655 insert all breakpoints. If we don't set shlib_disabled here,
7656 we'll try to insert those breakpoints and fail. */
7657 if (((b->type == bp_breakpoint)
7658 || (b->type == bp_jit_event)
7659 || (b->type == bp_hardware_breakpoint)
7660 || (is_tracepoint (b)))
7661 && loc->pspace == current_program_space
7662 && !loc->shlib_disabled
7663 && solib_name_from_address (loc->pspace, loc->address)
7664 )
7665 {
7666 loc->shlib_disabled = 1;
7667 }
7668 }
7669 }
7670
7671 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7672 notification of unloaded_shlib. Only apply to enabled breakpoints,
7673 disabled ones can just stay disabled. */
7674
7675 static void
7676 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7677 {
7678 struct bp_location *loc, **locp_tmp;
7679 int disabled_shlib_breaks = 0;
7680
7681 ALL_BP_LOCATIONS (loc, locp_tmp)
7682 {
7683 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7684 struct breakpoint *b = loc->owner;
7685
7686 if (solib->pspace == loc->pspace
7687 && !loc->shlib_disabled
7688 && (((b->type == bp_breakpoint
7689 || b->type == bp_jit_event
7690 || b->type == bp_hardware_breakpoint)
7691 && (loc->loc_type == bp_loc_hardware_breakpoint
7692 || loc->loc_type == bp_loc_software_breakpoint))
7693 || is_tracepoint (b))
7694 && solib_contains_address_p (solib, loc->address))
7695 {
7696 loc->shlib_disabled = 1;
7697 /* At this point, we cannot rely on remove_breakpoint
7698 succeeding so we must mark the breakpoint as not inserted
7699 to prevent future errors occurring in remove_breakpoints. */
7700 loc->inserted = 0;
7701
7702 /* This may cause duplicate notifications for the same breakpoint. */
7703 gdb::observers::breakpoint_modified.notify (b);
7704
7705 if (!disabled_shlib_breaks)
7706 {
7707 target_terminal::ours_for_output ();
7708 warning (_("Temporarily disabling breakpoints "
7709 "for unloaded shared library \"%s\""),
7710 solib->so_name);
7711 }
7712 disabled_shlib_breaks = 1;
7713 }
7714 }
7715 }
7716
7717 /* Disable any breakpoints and tracepoints in OBJFILE upon
7718 notification of free_objfile. Only apply to enabled breakpoints,
7719 disabled ones can just stay disabled. */
7720
7721 static void
7722 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7723 {
7724 if (objfile == NULL)
7725 return;
7726
7727 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7728 managed by the user with add-symbol-file/remove-symbol-file.
7729 Similarly to how breakpoints in shared libraries are handled in
7730 response to "nosharedlibrary", mark breakpoints in such modules
7731 shlib_disabled so they end up uninserted on the next global
7732 location list update. Shared libraries not loaded by the user
7733 aren't handled here -- they're already handled in
7734 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7735 solib_unloaded observer. We skip objfiles that are not
7736 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7737 main objfile). */
7738 if ((objfile->flags & OBJF_SHARED) == 0
7739 || (objfile->flags & OBJF_USERLOADED) == 0)
7740 return;
7741
7742 for (breakpoint *b : all_breakpoints ())
7743 {
7744 struct bp_location *loc;
7745 int bp_modified = 0;
7746
7747 if (!is_breakpoint (b) && !is_tracepoint (b))
7748 continue;
7749
7750 for (loc = b->loc; loc != NULL; loc = loc->next)
7751 {
7752 CORE_ADDR loc_addr = loc->address;
7753
7754 if (loc->loc_type != bp_loc_hardware_breakpoint
7755 && loc->loc_type != bp_loc_software_breakpoint)
7756 continue;
7757
7758 if (loc->shlib_disabled != 0)
7759 continue;
7760
7761 if (objfile->pspace != loc->pspace)
7762 continue;
7763
7764 if (loc->loc_type != bp_loc_hardware_breakpoint
7765 && loc->loc_type != bp_loc_software_breakpoint)
7766 continue;
7767
7768 if (is_addr_in_objfile (loc_addr, objfile))
7769 {
7770 loc->shlib_disabled = 1;
7771 /* At this point, we don't know whether the object was
7772 unmapped from the inferior or not, so leave the
7773 inserted flag alone. We'll handle failure to
7774 uninsert quietly, in case the object was indeed
7775 unmapped. */
7776
7777 mark_breakpoint_location_modified (loc);
7778
7779 bp_modified = 1;
7780 }
7781 }
7782
7783 if (bp_modified)
7784 gdb::observers::breakpoint_modified.notify (b);
7785 }
7786 }
7787
7788 /* FORK & VFORK catchpoints. */
7789
7790 /* An instance of this type is used to represent a fork or vfork
7791 catchpoint. A breakpoint is really of this type iff its ops pointer points
7792 to CATCH_FORK_BREAKPOINT_OPS. */
7793
7794 struct fork_catchpoint : public breakpoint
7795 {
7796 /* Process id of a child process whose forking triggered this
7797 catchpoint. This field is only valid immediately after this
7798 catchpoint has triggered. */
7799 ptid_t forked_inferior_pid;
7800 };
7801
7802 /* Implement the "insert" breakpoint_ops method for fork
7803 catchpoints. */
7804
7805 static int
7806 insert_catch_fork (struct bp_location *bl)
7807 {
7808 return target_insert_fork_catchpoint (inferior_ptid.pid ());
7809 }
7810
7811 /* Implement the "remove" breakpoint_ops method for fork
7812 catchpoints. */
7813
7814 static int
7815 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
7816 {
7817 return target_remove_fork_catchpoint (inferior_ptid.pid ());
7818 }
7819
7820 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7821 catchpoints. */
7822
7823 static int
7824 breakpoint_hit_catch_fork (const struct bp_location *bl,
7825 const address_space *aspace, CORE_ADDR bp_addr,
7826 const struct target_waitstatus *ws)
7827 {
7828 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7829
7830 if (ws->kind != TARGET_WAITKIND_FORKED)
7831 return 0;
7832
7833 c->forked_inferior_pid = ws->value.related_pid;
7834 return 1;
7835 }
7836
7837 /* Implement the "print_it" breakpoint_ops method for fork
7838 catchpoints. */
7839
7840 static enum print_stop_action
7841 print_it_catch_fork (bpstat bs)
7842 {
7843 struct ui_out *uiout = current_uiout;
7844 struct breakpoint *b = bs->breakpoint_at;
7845 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7846
7847 annotate_catchpoint (b->number);
7848 maybe_print_thread_hit_breakpoint (uiout);
7849 if (b->disposition == disp_del)
7850 uiout->text ("Temporary catchpoint ");
7851 else
7852 uiout->text ("Catchpoint ");
7853 if (uiout->is_mi_like_p ())
7854 {
7855 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7856 uiout->field_string ("disp", bpdisp_text (b->disposition));
7857 }
7858 uiout->field_signed ("bkptno", b->number);
7859 uiout->text (" (forked process ");
7860 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7861 uiout->text ("), ");
7862 return PRINT_SRC_AND_LOC;
7863 }
7864
7865 /* Implement the "print_one" breakpoint_ops method for fork
7866 catchpoints. */
7867
7868 static void
7869 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7870 {
7871 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7872 struct value_print_options opts;
7873 struct ui_out *uiout = current_uiout;
7874
7875 get_user_print_options (&opts);
7876
7877 /* Field 4, the address, is omitted (which makes the columns not
7878 line up too nicely with the headers, but the effect is relatively
7879 readable). */
7880 if (opts.addressprint)
7881 uiout->field_skip ("addr");
7882 annotate_field (5);
7883 uiout->text ("fork");
7884 if (c->forked_inferior_pid != null_ptid)
7885 {
7886 uiout->text (", process ");
7887 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7888 uiout->spaces (1);
7889 }
7890
7891 if (uiout->is_mi_like_p ())
7892 uiout->field_string ("catch-type", "fork");
7893 }
7894
7895 /* Implement the "print_mention" breakpoint_ops method for fork
7896 catchpoints. */
7897
7898 static void
7899 print_mention_catch_fork (struct breakpoint *b)
7900 {
7901 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7902 }
7903
7904 /* Implement the "print_recreate" breakpoint_ops method for fork
7905 catchpoints. */
7906
7907 static void
7908 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7909 {
7910 fprintf_unfiltered (fp, "catch fork");
7911 print_recreate_thread (b, fp);
7912 }
7913
7914 /* The breakpoint_ops structure to be used in fork catchpoints. */
7915
7916 static struct breakpoint_ops catch_fork_breakpoint_ops;
7917
7918 /* Implement the "insert" breakpoint_ops method for vfork
7919 catchpoints. */
7920
7921 static int
7922 insert_catch_vfork (struct bp_location *bl)
7923 {
7924 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
7925 }
7926
7927 /* Implement the "remove" breakpoint_ops method for vfork
7928 catchpoints. */
7929
7930 static int
7931 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
7932 {
7933 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
7934 }
7935
7936 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7937 catchpoints. */
7938
7939 static int
7940 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7941 const address_space *aspace, CORE_ADDR bp_addr,
7942 const struct target_waitstatus *ws)
7943 {
7944 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7945
7946 if (ws->kind != TARGET_WAITKIND_VFORKED)
7947 return 0;
7948
7949 c->forked_inferior_pid = ws->value.related_pid;
7950 return 1;
7951 }
7952
7953 /* Implement the "print_it" breakpoint_ops method for vfork
7954 catchpoints. */
7955
7956 static enum print_stop_action
7957 print_it_catch_vfork (bpstat bs)
7958 {
7959 struct ui_out *uiout = current_uiout;
7960 struct breakpoint *b = bs->breakpoint_at;
7961 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7962
7963 annotate_catchpoint (b->number);
7964 maybe_print_thread_hit_breakpoint (uiout);
7965 if (b->disposition == disp_del)
7966 uiout->text ("Temporary catchpoint ");
7967 else
7968 uiout->text ("Catchpoint ");
7969 if (uiout->is_mi_like_p ())
7970 {
7971 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7972 uiout->field_string ("disp", bpdisp_text (b->disposition));
7973 }
7974 uiout->field_signed ("bkptno", b->number);
7975 uiout->text (" (vforked process ");
7976 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7977 uiout->text ("), ");
7978 return PRINT_SRC_AND_LOC;
7979 }
7980
7981 /* Implement the "print_one" breakpoint_ops method for vfork
7982 catchpoints. */
7983
7984 static void
7985 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7986 {
7987 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7988 struct value_print_options opts;
7989 struct ui_out *uiout = current_uiout;
7990
7991 get_user_print_options (&opts);
7992 /* Field 4, the address, is omitted (which makes the columns not
7993 line up too nicely with the headers, but the effect is relatively
7994 readable). */
7995 if (opts.addressprint)
7996 uiout->field_skip ("addr");
7997 annotate_field (5);
7998 uiout->text ("vfork");
7999 if (c->forked_inferior_pid != null_ptid)
8000 {
8001 uiout->text (", process ");
8002 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
8003 uiout->spaces (1);
8004 }
8005
8006 if (uiout->is_mi_like_p ())
8007 uiout->field_string ("catch-type", "vfork");
8008 }
8009
8010 /* Implement the "print_mention" breakpoint_ops method for vfork
8011 catchpoints. */
8012
8013 static void
8014 print_mention_catch_vfork (struct breakpoint *b)
8015 {
8016 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8017 }
8018
8019 /* Implement the "print_recreate" breakpoint_ops method for vfork
8020 catchpoints. */
8021
8022 static void
8023 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8024 {
8025 fprintf_unfiltered (fp, "catch vfork");
8026 print_recreate_thread (b, fp);
8027 }
8028
8029 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8030
8031 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8032
8033 /* An instance of this type is used to represent an solib catchpoint.
8034 A breakpoint is really of this type iff its ops pointer points to
8035 CATCH_SOLIB_BREAKPOINT_OPS. */
8036
8037 struct solib_catchpoint : public breakpoint
8038 {
8039 ~solib_catchpoint () override;
8040
8041 /* True for "catch load", false for "catch unload". */
8042 bool is_load;
8043
8044 /* Regular expression to match, if any. COMPILED is only valid when
8045 REGEX is non-NULL. */
8046 char *regex;
8047 std::unique_ptr<compiled_regex> compiled;
8048 };
8049
8050 solib_catchpoint::~solib_catchpoint ()
8051 {
8052 xfree (this->regex);
8053 }
8054
8055 static int
8056 insert_catch_solib (struct bp_location *ignore)
8057 {
8058 return 0;
8059 }
8060
8061 static int
8062 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
8063 {
8064 return 0;
8065 }
8066
8067 static int
8068 breakpoint_hit_catch_solib (const struct bp_location *bl,
8069 const address_space *aspace,
8070 CORE_ADDR bp_addr,
8071 const struct target_waitstatus *ws)
8072 {
8073 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8074
8075 if (ws->kind == TARGET_WAITKIND_LOADED)
8076 return 1;
8077
8078 for (breakpoint *other : all_breakpoints ())
8079 {
8080 struct bp_location *other_bl;
8081
8082 if (other == bl->owner)
8083 continue;
8084
8085 if (other->type != bp_shlib_event)
8086 continue;
8087
8088 if (self->pspace != NULL && other->pspace != self->pspace)
8089 continue;
8090
8091 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8092 {
8093 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8094 return 1;
8095 }
8096 }
8097
8098 return 0;
8099 }
8100
8101 static void
8102 check_status_catch_solib (struct bpstats *bs)
8103 {
8104 struct solib_catchpoint *self
8105 = (struct solib_catchpoint *) bs->breakpoint_at;
8106
8107 if (self->is_load)
8108 {
8109 for (so_list *iter : current_program_space->added_solibs)
8110 {
8111 if (!self->regex
8112 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
8113 return;
8114 }
8115 }
8116 else
8117 {
8118 for (const std::string &iter : current_program_space->deleted_solibs)
8119 {
8120 if (!self->regex
8121 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
8122 return;
8123 }
8124 }
8125
8126 bs->stop = 0;
8127 bs->print_it = print_it_noop;
8128 }
8129
8130 static enum print_stop_action
8131 print_it_catch_solib (bpstat bs)
8132 {
8133 struct breakpoint *b = bs->breakpoint_at;
8134 struct ui_out *uiout = current_uiout;
8135
8136 annotate_catchpoint (b->number);
8137 maybe_print_thread_hit_breakpoint (uiout);
8138 if (b->disposition == disp_del)
8139 uiout->text ("Temporary catchpoint ");
8140 else
8141 uiout->text ("Catchpoint ");
8142 uiout->field_signed ("bkptno", b->number);
8143 uiout->text ("\n");
8144 if (uiout->is_mi_like_p ())
8145 uiout->field_string ("disp", bpdisp_text (b->disposition));
8146 print_solib_event (1);
8147 return PRINT_SRC_AND_LOC;
8148 }
8149
8150 static void
8151 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8152 {
8153 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8154 struct value_print_options opts;
8155 struct ui_out *uiout = current_uiout;
8156
8157 get_user_print_options (&opts);
8158 /* Field 4, the address, is omitted (which makes the columns not
8159 line up too nicely with the headers, but the effect is relatively
8160 readable). */
8161 if (opts.addressprint)
8162 {
8163 annotate_field (4);
8164 uiout->field_skip ("addr");
8165 }
8166
8167 std::string msg;
8168 annotate_field (5);
8169 if (self->is_load)
8170 {
8171 if (self->regex)
8172 msg = string_printf (_("load of library matching %s"), self->regex);
8173 else
8174 msg = _("load of library");
8175 }
8176 else
8177 {
8178 if (self->regex)
8179 msg = string_printf (_("unload of library matching %s"), self->regex);
8180 else
8181 msg = _("unload of library");
8182 }
8183 uiout->field_string ("what", msg);
8184
8185 if (uiout->is_mi_like_p ())
8186 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8187 }
8188
8189 static void
8190 print_mention_catch_solib (struct breakpoint *b)
8191 {
8192 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8193
8194 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8195 self->is_load ? "load" : "unload");
8196 }
8197
8198 static void
8199 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8200 {
8201 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8202
8203 fprintf_unfiltered (fp, "%s %s",
8204 b->disposition == disp_del ? "tcatch" : "catch",
8205 self->is_load ? "load" : "unload");
8206 if (self->regex)
8207 fprintf_unfiltered (fp, " %s", self->regex);
8208 fprintf_unfiltered (fp, "\n");
8209 }
8210
8211 static struct breakpoint_ops catch_solib_breakpoint_ops;
8212
8213 /* See breakpoint.h. */
8214
8215 void
8216 add_solib_catchpoint (const char *arg, bool is_load, bool is_temp, bool enabled)
8217 {
8218 struct gdbarch *gdbarch = get_current_arch ();
8219
8220 if (!arg)
8221 arg = "";
8222 arg = skip_spaces (arg);
8223
8224 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
8225
8226 if (*arg != '\0')
8227 {
8228 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8229 _("Invalid regexp")));
8230 c->regex = xstrdup (arg);
8231 }
8232
8233 c->is_load = is_load;
8234 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
8235 &catch_solib_breakpoint_ops);
8236
8237 c->enable_state = enabled ? bp_enabled : bp_disabled;
8238
8239 install_breakpoint (0, std::move (c), 1);
8240 }
8241
8242 /* A helper function that does all the work for "catch load" and
8243 "catch unload". */
8244
8245 static void
8246 catch_load_or_unload (const char *arg, int from_tty, int is_load,
8247 struct cmd_list_element *command)
8248 {
8249 const int enabled = 1;
8250 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
8251
8252 add_solib_catchpoint (arg, is_load, temp, enabled);
8253 }
8254
8255 static void
8256 catch_load_command_1 (const char *arg, int from_tty,
8257 struct cmd_list_element *command)
8258 {
8259 catch_load_or_unload (arg, from_tty, 1, command);
8260 }
8261
8262 static void
8263 catch_unload_command_1 (const char *arg, int from_tty,
8264 struct cmd_list_element *command)
8265 {
8266 catch_load_or_unload (arg, from_tty, 0, command);
8267 }
8268
8269 /* See breakpoint.h. */
8270
8271 void
8272 init_catchpoint (struct breakpoint *b,
8273 struct gdbarch *gdbarch, bool temp,
8274 const char *cond_string,
8275 const struct breakpoint_ops *ops)
8276 {
8277 symtab_and_line sal;
8278 sal.pspace = current_program_space;
8279
8280 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8281
8282 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8283 b->disposition = temp ? disp_del : disp_donttouch;
8284 }
8285
8286 void
8287 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8288 {
8289 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8290 set_breakpoint_number (internal, b);
8291 if (is_tracepoint (b))
8292 set_tracepoint_count (breakpoint_count);
8293 if (!internal)
8294 mention (b);
8295 gdb::observers::breakpoint_created.notify (b);
8296
8297 if (update_gll)
8298 update_global_location_list (UGLL_MAY_INSERT);
8299 }
8300
8301 static void
8302 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8303 bool temp, const char *cond_string,
8304 const struct breakpoint_ops *ops)
8305 {
8306 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
8307
8308 init_catchpoint (c.get (), gdbarch, temp, cond_string, ops);
8309
8310 c->forked_inferior_pid = null_ptid;
8311
8312 install_breakpoint (0, std::move (c), 1);
8313 }
8314
8315 /* Exec catchpoints. */
8316
8317 /* An instance of this type is used to represent an exec catchpoint.
8318 A breakpoint is really of this type iff its ops pointer points to
8319 CATCH_EXEC_BREAKPOINT_OPS. */
8320
8321 struct exec_catchpoint : public breakpoint
8322 {
8323 ~exec_catchpoint () override;
8324
8325 /* Filename of a program whose exec triggered this catchpoint.
8326 This field is only valid immediately after this catchpoint has
8327 triggered. */
8328 char *exec_pathname;
8329 };
8330
8331 /* Exec catchpoint destructor. */
8332
8333 exec_catchpoint::~exec_catchpoint ()
8334 {
8335 xfree (this->exec_pathname);
8336 }
8337
8338 static int
8339 insert_catch_exec (struct bp_location *bl)
8340 {
8341 return target_insert_exec_catchpoint (inferior_ptid.pid ());
8342 }
8343
8344 static int
8345 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8346 {
8347 return target_remove_exec_catchpoint (inferior_ptid.pid ());
8348 }
8349
8350 static int
8351 breakpoint_hit_catch_exec (const struct bp_location *bl,
8352 const address_space *aspace, CORE_ADDR bp_addr,
8353 const struct target_waitstatus *ws)
8354 {
8355 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8356
8357 if (ws->kind != TARGET_WAITKIND_EXECD)
8358 return 0;
8359
8360 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8361 return 1;
8362 }
8363
8364 static enum print_stop_action
8365 print_it_catch_exec (bpstat bs)
8366 {
8367 struct ui_out *uiout = current_uiout;
8368 struct breakpoint *b = bs->breakpoint_at;
8369 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8370
8371 annotate_catchpoint (b->number);
8372 maybe_print_thread_hit_breakpoint (uiout);
8373 if (b->disposition == disp_del)
8374 uiout->text ("Temporary catchpoint ");
8375 else
8376 uiout->text ("Catchpoint ");
8377 if (uiout->is_mi_like_p ())
8378 {
8379 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8380 uiout->field_string ("disp", bpdisp_text (b->disposition));
8381 }
8382 uiout->field_signed ("bkptno", b->number);
8383 uiout->text (" (exec'd ");
8384 uiout->field_string ("new-exec", c->exec_pathname);
8385 uiout->text ("), ");
8386
8387 return PRINT_SRC_AND_LOC;
8388 }
8389
8390 static void
8391 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8392 {
8393 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8394 struct value_print_options opts;
8395 struct ui_out *uiout = current_uiout;
8396
8397 get_user_print_options (&opts);
8398
8399 /* Field 4, the address, is omitted (which makes the columns
8400 not line up too nicely with the headers, but the effect
8401 is relatively readable). */
8402 if (opts.addressprint)
8403 uiout->field_skip ("addr");
8404 annotate_field (5);
8405 uiout->text ("exec");
8406 if (c->exec_pathname != NULL)
8407 {
8408 uiout->text (", program \"");
8409 uiout->field_string ("what", c->exec_pathname);
8410 uiout->text ("\" ");
8411 }
8412
8413 if (uiout->is_mi_like_p ())
8414 uiout->field_string ("catch-type", "exec");
8415 }
8416
8417 static void
8418 print_mention_catch_exec (struct breakpoint *b)
8419 {
8420 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8421 }
8422
8423 /* Implement the "print_recreate" breakpoint_ops method for exec
8424 catchpoints. */
8425
8426 static void
8427 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8428 {
8429 fprintf_unfiltered (fp, "catch exec");
8430 print_recreate_thread (b, fp);
8431 }
8432
8433 static struct breakpoint_ops catch_exec_breakpoint_ops;
8434
8435 static int
8436 hw_breakpoint_used_count (void)
8437 {
8438 int i = 0;
8439 struct bp_location *bl;
8440
8441 for (breakpoint *b : all_breakpoints ())
8442 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8443 for (bl = b->loc; bl; bl = bl->next)
8444 {
8445 /* Special types of hardware breakpoints may use more than
8446 one register. */
8447 i += b->ops->resources_needed (bl);
8448 }
8449
8450 return i;
8451 }
8452
8453 /* Returns the resources B would use if it were a hardware
8454 watchpoint. */
8455
8456 static int
8457 hw_watchpoint_use_count (struct breakpoint *b)
8458 {
8459 int i = 0;
8460 struct bp_location *bl;
8461
8462 if (!breakpoint_enabled (b))
8463 return 0;
8464
8465 for (bl = b->loc; bl; bl = bl->next)
8466 {
8467 /* Special types of hardware watchpoints may use more than
8468 one register. */
8469 i += b->ops->resources_needed (bl);
8470 }
8471
8472 return i;
8473 }
8474
8475 /* Returns the sum the used resources of all hardware watchpoints of
8476 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8477 the sum of the used resources of all hardware watchpoints of other
8478 types _not_ TYPE. */
8479
8480 static int
8481 hw_watchpoint_used_count_others (struct breakpoint *except,
8482 enum bptype type, int *other_type_used)
8483 {
8484 int i = 0;
8485
8486 *other_type_used = 0;
8487 for (breakpoint *b : all_breakpoints ())
8488 {
8489 if (b == except)
8490 continue;
8491 if (!breakpoint_enabled (b))
8492 continue;
8493
8494 if (b->type == type)
8495 i += hw_watchpoint_use_count (b);
8496 else if (is_hardware_watchpoint (b))
8497 *other_type_used = 1;
8498 }
8499
8500 return i;
8501 }
8502
8503 void
8504 disable_watchpoints_before_interactive_call_start (void)
8505 {
8506 for (breakpoint *b : all_breakpoints ())
8507 if (is_watchpoint (b) && breakpoint_enabled (b))
8508 {
8509 b->enable_state = bp_call_disabled;
8510 update_global_location_list (UGLL_DONT_INSERT);
8511 }
8512 }
8513
8514 void
8515 enable_watchpoints_after_interactive_call_stop (void)
8516 {
8517 for (breakpoint *b : all_breakpoints ())
8518 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8519 {
8520 b->enable_state = bp_enabled;
8521 update_global_location_list (UGLL_MAY_INSERT);
8522 }
8523 }
8524
8525 void
8526 disable_breakpoints_before_startup (void)
8527 {
8528 current_program_space->executing_startup = 1;
8529 update_global_location_list (UGLL_DONT_INSERT);
8530 }
8531
8532 void
8533 enable_breakpoints_after_startup (void)
8534 {
8535 current_program_space->executing_startup = 0;
8536 breakpoint_re_set ();
8537 }
8538
8539 /* Create a new single-step breakpoint for thread THREAD, with no
8540 locations. */
8541
8542 static struct breakpoint *
8543 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8544 {
8545 std::unique_ptr<breakpoint> b (new breakpoint ());
8546
8547 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
8548 &momentary_breakpoint_ops);
8549
8550 b->disposition = disp_donttouch;
8551 b->frame_id = null_frame_id;
8552
8553 b->thread = thread;
8554 gdb_assert (b->thread != 0);
8555
8556 return add_to_breakpoint_chain (std::move (b));
8557 }
8558
8559 /* Set a momentary breakpoint of type TYPE at address specified by
8560 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8561 frame. */
8562
8563 breakpoint_up
8564 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8565 struct frame_id frame_id, enum bptype type)
8566 {
8567 struct breakpoint *b;
8568
8569 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8570 tail-called one. */
8571 gdb_assert (!frame_id_artificial_p (frame_id));
8572
8573 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8574 b->enable_state = bp_enabled;
8575 b->disposition = disp_donttouch;
8576 b->frame_id = frame_id;
8577
8578 b->thread = inferior_thread ()->global_num;
8579
8580 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8581
8582 return breakpoint_up (b);
8583 }
8584
8585 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8586 The new breakpoint will have type TYPE, use OPS as its
8587 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8588
8589 static struct breakpoint *
8590 momentary_breakpoint_from_master (struct breakpoint *orig,
8591 enum bptype type,
8592 const struct breakpoint_ops *ops,
8593 int loc_enabled)
8594 {
8595 struct breakpoint *copy;
8596
8597 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8598 copy->loc = allocate_bp_location (copy);
8599 set_breakpoint_location_function (copy->loc);
8600
8601 copy->loc->gdbarch = orig->loc->gdbarch;
8602 copy->loc->requested_address = orig->loc->requested_address;
8603 copy->loc->address = orig->loc->address;
8604 copy->loc->section = orig->loc->section;
8605 copy->loc->pspace = orig->loc->pspace;
8606 copy->loc->probe = orig->loc->probe;
8607 copy->loc->line_number = orig->loc->line_number;
8608 copy->loc->symtab = orig->loc->symtab;
8609 copy->loc->enabled = loc_enabled;
8610 copy->frame_id = orig->frame_id;
8611 copy->thread = orig->thread;
8612 copy->pspace = orig->pspace;
8613
8614 copy->enable_state = bp_enabled;
8615 copy->disposition = disp_donttouch;
8616 copy->number = internal_breakpoint_number--;
8617
8618 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8619 return copy;
8620 }
8621
8622 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8623 ORIG is NULL. */
8624
8625 struct breakpoint *
8626 clone_momentary_breakpoint (struct breakpoint *orig)
8627 {
8628 /* If there's nothing to clone, then return nothing. */
8629 if (orig == NULL)
8630 return NULL;
8631
8632 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8633 }
8634
8635 breakpoint_up
8636 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8637 enum bptype type)
8638 {
8639 struct symtab_and_line sal;
8640
8641 sal = find_pc_line (pc, 0);
8642 sal.pc = pc;
8643 sal.section = find_pc_overlay (pc);
8644 sal.explicit_pc = 1;
8645
8646 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8647 }
8648 \f
8649
8650 /* Tell the user we have just set a breakpoint B. */
8651
8652 static void
8653 mention (struct breakpoint *b)
8654 {
8655 b->ops->print_mention (b);
8656 current_uiout->text ("\n");
8657 }
8658 \f
8659
8660 static bool bp_loc_is_permanent (struct bp_location *loc);
8661
8662 /* Handle "set breakpoint auto-hw on".
8663
8664 If the explicitly specified breakpoint type is not hardware
8665 breakpoint, check the memory map to see whether the breakpoint
8666 address is in read-only memory.
8667
8668 - location type is not hardware breakpoint, memory is read-only.
8669 We change the type of the location to hardware breakpoint.
8670
8671 - location type is hardware breakpoint, memory is read-write. This
8672 means we've previously made the location hardware one, but then the
8673 memory map changed, so we undo.
8674 */
8675
8676 static void
8677 handle_automatic_hardware_breakpoints (bp_location *bl)
8678 {
8679 if (automatic_hardware_breakpoints
8680 && bl->owner->type != bp_hardware_breakpoint
8681 && (bl->loc_type == bp_loc_software_breakpoint
8682 || bl->loc_type == bp_loc_hardware_breakpoint))
8683 {
8684 /* When breakpoints are removed, remove_breakpoints will use
8685 location types we've just set here, the only possible problem
8686 is that memory map has changed during running program, but
8687 it's not going to work anyway with current gdb. */
8688 mem_region *mr = lookup_mem_region (bl->address);
8689
8690 if (mr != nullptr)
8691 {
8692 enum bp_loc_type new_type;
8693
8694 if (mr->attrib.mode != MEM_RW)
8695 new_type = bp_loc_hardware_breakpoint;
8696 else
8697 new_type = bp_loc_software_breakpoint;
8698
8699 if (new_type != bl->loc_type)
8700 {
8701 static bool said = false;
8702
8703 bl->loc_type = new_type;
8704 if (!said)
8705 {
8706 fprintf_filtered (gdb_stdout,
8707 _("Note: automatically using "
8708 "hardware breakpoints for "
8709 "read-only addresses.\n"));
8710 said = true;
8711 }
8712 }
8713 }
8714 }
8715 }
8716
8717 static struct bp_location *
8718 add_location_to_breakpoint (struct breakpoint *b,
8719 const struct symtab_and_line *sal)
8720 {
8721 struct bp_location *loc, **tmp;
8722 CORE_ADDR adjusted_address;
8723 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8724
8725 if (loc_gdbarch == NULL)
8726 loc_gdbarch = b->gdbarch;
8727
8728 /* Adjust the breakpoint's address prior to allocating a location.
8729 Once we call allocate_bp_location(), that mostly uninitialized
8730 location will be placed on the location chain. Adjustment of the
8731 breakpoint may cause target_read_memory() to be called and we do
8732 not want its scan of the location chain to find a breakpoint and
8733 location that's only been partially initialized. */
8734 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8735 sal->pc, b->type);
8736
8737 /* Sort the locations by their ADDRESS. */
8738 loc = allocate_bp_location (b);
8739 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8740 tmp = &((*tmp)->next))
8741 ;
8742 loc->next = *tmp;
8743 *tmp = loc;
8744
8745 loc->requested_address = sal->pc;
8746 loc->address = adjusted_address;
8747 loc->pspace = sal->pspace;
8748 loc->probe.prob = sal->prob;
8749 loc->probe.objfile = sal->objfile;
8750 gdb_assert (loc->pspace != NULL);
8751 loc->section = sal->section;
8752 loc->gdbarch = loc_gdbarch;
8753 loc->line_number = sal->line;
8754 loc->symtab = sal->symtab;
8755 loc->symbol = sal->symbol;
8756 loc->msymbol = sal->msymbol;
8757 loc->objfile = sal->objfile;
8758
8759 set_breakpoint_location_function (loc);
8760
8761 /* While by definition, permanent breakpoints are already present in the
8762 code, we don't mark the location as inserted. Normally one would expect
8763 that GDB could rely on that breakpoint instruction to stop the program,
8764 thus removing the need to insert its own breakpoint, except that executing
8765 the breakpoint instruction can kill the target instead of reporting a
8766 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8767 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8768 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8769 breakpoint be inserted normally results in QEMU knowing about the GDB
8770 breakpoint, and thus trap before the breakpoint instruction is executed.
8771 (If GDB later needs to continue execution past the permanent breakpoint,
8772 it manually increments the PC, thus avoiding executing the breakpoint
8773 instruction.) */
8774 if (bp_loc_is_permanent (loc))
8775 loc->permanent = 1;
8776
8777 return loc;
8778 }
8779 \f
8780
8781 /* Return true if LOC is pointing to a permanent breakpoint,
8782 return false otherwise. */
8783
8784 static bool
8785 bp_loc_is_permanent (struct bp_location *loc)
8786 {
8787 gdb_assert (loc != NULL);
8788
8789 /* If we have a non-breakpoint-backed catchpoint or a software
8790 watchpoint, just return 0. We should not attempt to read from
8791 the addresses the locations of these breakpoint types point to.
8792 gdbarch_program_breakpoint_here_p, below, will attempt to read
8793 memory. */
8794 if (!bl_address_is_meaningful (loc))
8795 return false;
8796
8797 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8798 switch_to_program_space_and_thread (loc->pspace);
8799 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
8800 }
8801
8802 /* Build a command list for the dprintf corresponding to the current
8803 settings of the dprintf style options. */
8804
8805 static void
8806 update_dprintf_command_list (struct breakpoint *b)
8807 {
8808 char *dprintf_args = b->extra_string;
8809 char *printf_line = NULL;
8810
8811 if (!dprintf_args)
8812 return;
8813
8814 dprintf_args = skip_spaces (dprintf_args);
8815
8816 /* Allow a comma, as it may have terminated a location, but don't
8817 insist on it. */
8818 if (*dprintf_args == ',')
8819 ++dprintf_args;
8820 dprintf_args = skip_spaces (dprintf_args);
8821
8822 if (*dprintf_args != '"')
8823 error (_("Bad format string, missing '\"'."));
8824
8825 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8826 printf_line = xstrprintf ("printf %s", dprintf_args);
8827 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8828 {
8829 if (!dprintf_function)
8830 error (_("No function supplied for dprintf call"));
8831
8832 if (dprintf_channel && strlen (dprintf_channel) > 0)
8833 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8834 dprintf_function,
8835 dprintf_channel,
8836 dprintf_args);
8837 else
8838 printf_line = xstrprintf ("call (void) %s (%s)",
8839 dprintf_function,
8840 dprintf_args);
8841 }
8842 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8843 {
8844 if (target_can_run_breakpoint_commands ())
8845 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8846 else
8847 {
8848 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8849 printf_line = xstrprintf ("printf %s", dprintf_args);
8850 }
8851 }
8852 else
8853 internal_error (__FILE__, __LINE__,
8854 _("Invalid dprintf style."));
8855
8856 gdb_assert (printf_line != NULL);
8857
8858 /* Manufacture a printf sequence. */
8859 struct command_line *printf_cmd_line
8860 = new struct command_line (simple_control, printf_line);
8861 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8862 command_lines_deleter ()));
8863 }
8864
8865 /* Update all dprintf commands, making their command lists reflect
8866 current style settings. */
8867
8868 static void
8869 update_dprintf_commands (const char *args, int from_tty,
8870 struct cmd_list_element *c)
8871 {
8872 for (breakpoint *b : all_breakpoints ())
8873 if (b->type == bp_dprintf)
8874 update_dprintf_command_list (b);
8875 }
8876
8877 /* Create a breakpoint with SAL as location. Use LOCATION
8878 as a description of the location, and COND_STRING
8879 as condition expression. If LOCATION is NULL then create an
8880 "address location" from the address in the SAL. */
8881
8882 static void
8883 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8884 gdb::array_view<const symtab_and_line> sals,
8885 event_location_up &&location,
8886 gdb::unique_xmalloc_ptr<char> filter,
8887 gdb::unique_xmalloc_ptr<char> cond_string,
8888 gdb::unique_xmalloc_ptr<char> extra_string,
8889 enum bptype type, enum bpdisp disposition,
8890 int thread, int task, int ignore_count,
8891 const struct breakpoint_ops *ops, int from_tty,
8892 int enabled, int internal, unsigned flags,
8893 int display_canonical)
8894 {
8895 int i;
8896
8897 if (type == bp_hardware_breakpoint)
8898 {
8899 int target_resources_ok;
8900
8901 i = hw_breakpoint_used_count ();
8902 target_resources_ok =
8903 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8904 i + 1, 0);
8905 if (target_resources_ok == 0)
8906 error (_("No hardware breakpoint support in the target."));
8907 else if (target_resources_ok < 0)
8908 error (_("Hardware breakpoints used exceeds limit."));
8909 }
8910
8911 gdb_assert (!sals.empty ());
8912
8913 for (const auto &sal : sals)
8914 {
8915 struct bp_location *loc;
8916
8917 if (from_tty)
8918 {
8919 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8920 if (!loc_gdbarch)
8921 loc_gdbarch = gdbarch;
8922
8923 describe_other_breakpoints (loc_gdbarch,
8924 sal.pspace, sal.pc, sal.section, thread);
8925 }
8926
8927 if (&sal == &sals[0])
8928 {
8929 init_raw_breakpoint (b, gdbarch, sal, type, ops);
8930 b->thread = thread;
8931 b->task = task;
8932
8933 b->cond_string = cond_string.release ();
8934 b->extra_string = extra_string.release ();
8935 b->ignore_count = ignore_count;
8936 b->enable_state = enabled ? bp_enabled : bp_disabled;
8937 b->disposition = disposition;
8938
8939 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8940 b->loc->inserted = 1;
8941
8942 if (type == bp_static_tracepoint)
8943 {
8944 struct tracepoint *t = (struct tracepoint *) b;
8945 struct static_tracepoint_marker marker;
8946
8947 if (strace_marker_p (b))
8948 {
8949 /* We already know the marker exists, otherwise, we
8950 wouldn't see a sal for it. */
8951 const char *p
8952 = &event_location_to_string (b->location.get ())[3];
8953 const char *endp;
8954
8955 p = skip_spaces (p);
8956
8957 endp = skip_to_space (p);
8958
8959 t->static_trace_marker_id.assign (p, endp - p);
8960
8961 printf_filtered (_("Probed static tracepoint "
8962 "marker \"%s\"\n"),
8963 t->static_trace_marker_id.c_str ());
8964 }
8965 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8966 {
8967 t->static_trace_marker_id = std::move (marker.str_id);
8968
8969 printf_filtered (_("Probed static tracepoint "
8970 "marker \"%s\"\n"),
8971 t->static_trace_marker_id.c_str ());
8972 }
8973 else
8974 warning (_("Couldn't determine the static "
8975 "tracepoint marker to probe"));
8976 }
8977
8978 loc = b->loc;
8979 }
8980 else
8981 {
8982 loc = add_location_to_breakpoint (b, &sal);
8983 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8984 loc->inserted = 1;
8985 }
8986
8987 /* Do not set breakpoint locations conditions yet. As locations
8988 are inserted, they get sorted based on their addresses. Let
8989 the list stabilize to have reliable location numbers. */
8990
8991 /* Dynamic printf requires and uses additional arguments on the
8992 command line, otherwise it's an error. */
8993 if (type == bp_dprintf)
8994 {
8995 if (b->extra_string)
8996 update_dprintf_command_list (b);
8997 else
8998 error (_("Format string required"));
8999 }
9000 else if (b->extra_string)
9001 error (_("Garbage '%s' at end of command"), b->extra_string);
9002 }
9003
9004
9005 /* The order of the locations is now stable. Set the location
9006 condition using the location's number. */
9007 int loc_num = 1;
9008 for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)
9009 {
9010 if (b->cond_string != nullptr)
9011 set_breakpoint_location_condition (b->cond_string, loc, b->number,
9012 loc_num);
9013
9014 ++loc_num;
9015 }
9016
9017 b->display_canonical = display_canonical;
9018 if (location != NULL)
9019 b->location = std::move (location);
9020 else
9021 b->location = new_address_location (b->loc->address, NULL, 0);
9022 b->filter = std::move (filter);
9023 }
9024
9025 static void
9026 create_breakpoint_sal (struct gdbarch *gdbarch,
9027 gdb::array_view<const symtab_and_line> sals,
9028 event_location_up &&location,
9029 gdb::unique_xmalloc_ptr<char> filter,
9030 gdb::unique_xmalloc_ptr<char> cond_string,
9031 gdb::unique_xmalloc_ptr<char> extra_string,
9032 enum bptype type, enum bpdisp disposition,
9033 int thread, int task, int ignore_count,
9034 const struct breakpoint_ops *ops, int from_tty,
9035 int enabled, int internal, unsigned flags,
9036 int display_canonical)
9037 {
9038 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
9039
9040 init_breakpoint_sal (b.get (), gdbarch,
9041 sals, std::move (location),
9042 std::move (filter),
9043 std::move (cond_string),
9044 std::move (extra_string),
9045 type, disposition,
9046 thread, task, ignore_count,
9047 ops, from_tty,
9048 enabled, internal, flags,
9049 display_canonical);
9050
9051 install_breakpoint (internal, std::move (b), 0);
9052 }
9053
9054 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9055 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9056 value. COND_STRING, if not NULL, specified the condition to be
9057 used for all breakpoints. Essentially the only case where
9058 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9059 function. In that case, it's still not possible to specify
9060 separate conditions for different overloaded functions, so
9061 we take just a single condition string.
9062
9063 NOTE: If the function succeeds, the caller is expected to cleanup
9064 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9065 array contents). If the function fails (error() is called), the
9066 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9067 COND and SALS arrays and each of those arrays contents. */
9068
9069 static void
9070 create_breakpoints_sal (struct gdbarch *gdbarch,
9071 struct linespec_result *canonical,
9072 gdb::unique_xmalloc_ptr<char> cond_string,
9073 gdb::unique_xmalloc_ptr<char> extra_string,
9074 enum bptype type, enum bpdisp disposition,
9075 int thread, int task, int ignore_count,
9076 const struct breakpoint_ops *ops, int from_tty,
9077 int enabled, int internal, unsigned flags)
9078 {
9079 if (canonical->pre_expanded)
9080 gdb_assert (canonical->lsals.size () == 1);
9081
9082 for (const auto &lsal : canonical->lsals)
9083 {
9084 /* Note that 'location' can be NULL in the case of a plain
9085 'break', without arguments. */
9086 event_location_up location
9087 = (canonical->location != NULL
9088 ? copy_event_location (canonical->location.get ()) : NULL);
9089 gdb::unique_xmalloc_ptr<char> filter_string
9090 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
9091
9092 create_breakpoint_sal (gdbarch, lsal.sals,
9093 std::move (location),
9094 std::move (filter_string),
9095 std::move (cond_string),
9096 std::move (extra_string),
9097 type, disposition,
9098 thread, task, ignore_count, ops,
9099 from_tty, enabled, internal, flags,
9100 canonical->special_display);
9101 }
9102 }
9103
9104 /* Parse LOCATION which is assumed to be a SAL specification possibly
9105 followed by conditionals. On return, SALS contains an array of SAL
9106 addresses found. LOCATION points to the end of the SAL (for
9107 linespec locations).
9108
9109 The array and the line spec strings are allocated on the heap, it is
9110 the caller's responsibility to free them. */
9111
9112 static void
9113 parse_breakpoint_sals (struct event_location *location,
9114 struct linespec_result *canonical)
9115 {
9116 struct symtab_and_line cursal;
9117
9118 if (event_location_type (location) == LINESPEC_LOCATION)
9119 {
9120 const char *spec = get_linespec_location (location)->spec_string;
9121
9122 if (spec == NULL)
9123 {
9124 /* The last displayed codepoint, if it's valid, is our default
9125 breakpoint address. */
9126 if (last_displayed_sal_is_valid ())
9127 {
9128 /* Set sal's pspace, pc, symtab, and line to the values
9129 corresponding to the last call to print_frame_info.
9130 Be sure to reinitialize LINE with NOTCURRENT == 0
9131 as the breakpoint line number is inappropriate otherwise.
9132 find_pc_line would adjust PC, re-set it back. */
9133 symtab_and_line sal = get_last_displayed_sal ();
9134 CORE_ADDR pc = sal.pc;
9135
9136 sal = find_pc_line (pc, 0);
9137
9138 /* "break" without arguments is equivalent to "break *PC"
9139 where PC is the last displayed codepoint's address. So
9140 make sure to set sal.explicit_pc to prevent GDB from
9141 trying to expand the list of sals to include all other
9142 instances with the same symtab and line. */
9143 sal.pc = pc;
9144 sal.explicit_pc = 1;
9145
9146 struct linespec_sals lsal;
9147 lsal.sals = {sal};
9148 lsal.canonical = NULL;
9149
9150 canonical->lsals.push_back (std::move (lsal));
9151 return;
9152 }
9153 else
9154 error (_("No default breakpoint address now."));
9155 }
9156 }
9157
9158 /* Force almost all breakpoints to be in terms of the
9159 current_source_symtab (which is decode_line_1's default).
9160 This should produce the results we want almost all of the
9161 time while leaving default_breakpoint_* alone.
9162
9163 ObjC: However, don't match an Objective-C method name which
9164 may have a '+' or '-' succeeded by a '['. */
9165 cursal = get_current_source_symtab_and_line ();
9166 if (last_displayed_sal_is_valid ())
9167 {
9168 const char *spec = NULL;
9169
9170 if (event_location_type (location) == LINESPEC_LOCATION)
9171 spec = get_linespec_location (location)->spec_string;
9172
9173 if (!cursal.symtab
9174 || (spec != NULL
9175 && strchr ("+-", spec[0]) != NULL
9176 && spec[1] != '['))
9177 {
9178 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9179 get_last_displayed_symtab (),
9180 get_last_displayed_line (),
9181 canonical, NULL, NULL);
9182 return;
9183 }
9184 }
9185
9186 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9187 cursal.symtab, cursal.line, canonical, NULL, NULL);
9188 }
9189
9190
9191 /* Convert each SAL into a real PC. Verify that the PC can be
9192 inserted as a breakpoint. If it can't throw an error. */
9193
9194 static void
9195 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
9196 {
9197 for (auto &sal : sals)
9198 resolve_sal_pc (&sal);
9199 }
9200
9201 /* Fast tracepoints may have restrictions on valid locations. For
9202 instance, a fast tracepoint using a jump instead of a trap will
9203 likely have to overwrite more bytes than a trap would, and so can
9204 only be placed where the instruction is longer than the jump, or a
9205 multi-instruction sequence does not have a jump into the middle of
9206 it, etc. */
9207
9208 static void
9209 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9210 gdb::array_view<const symtab_and_line> sals)
9211 {
9212 for (const auto &sal : sals)
9213 {
9214 struct gdbarch *sarch;
9215
9216 sarch = get_sal_arch (sal);
9217 /* We fall back to GDBARCH if there is no architecture
9218 associated with SAL. */
9219 if (sarch == NULL)
9220 sarch = gdbarch;
9221 std::string msg;
9222 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
9223 error (_("May not have a fast tracepoint at %s%s"),
9224 paddress (sarch, sal.pc), msg.c_str ());
9225 }
9226 }
9227
9228 /* Given TOK, a string specification of condition and thread, as
9229 accepted by the 'break' command, extract the condition
9230 string and thread number and set *COND_STRING and *THREAD.
9231 PC identifies the context at which the condition should be parsed.
9232 If no condition is found, *COND_STRING is set to NULL.
9233 If no thread is found, *THREAD is set to -1. */
9234
9235 static void
9236 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9237 char **cond_string, int *thread, int *task,
9238 char **rest)
9239 {
9240 *cond_string = NULL;
9241 *thread = -1;
9242 *task = 0;
9243 *rest = NULL;
9244 bool force = false;
9245
9246 while (tok && *tok)
9247 {
9248 const char *end_tok;
9249 int toklen;
9250 const char *cond_start = NULL;
9251 const char *cond_end = NULL;
9252
9253 tok = skip_spaces (tok);
9254
9255 if ((*tok == '"' || *tok == ',') && rest)
9256 {
9257 *rest = savestring (tok, strlen (tok));
9258 return;
9259 }
9260
9261 end_tok = skip_to_space (tok);
9262
9263 toklen = end_tok - tok;
9264
9265 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9266 {
9267 tok = cond_start = end_tok + 1;
9268 try
9269 {
9270 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9271 }
9272 catch (const gdb_exception_error &)
9273 {
9274 if (!force)
9275 throw;
9276 else
9277 tok = tok + strlen (tok);
9278 }
9279 cond_end = tok;
9280 *cond_string = savestring (cond_start, cond_end - cond_start);
9281 }
9282 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
9283 {
9284 tok = tok + toklen;
9285 force = true;
9286 }
9287 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9288 {
9289 const char *tmptok;
9290 struct thread_info *thr;
9291
9292 tok = end_tok + 1;
9293 thr = parse_thread_id (tok, &tmptok);
9294 if (tok == tmptok)
9295 error (_("Junk after thread keyword."));
9296 *thread = thr->global_num;
9297 tok = tmptok;
9298 }
9299 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9300 {
9301 char *tmptok;
9302
9303 tok = end_tok + 1;
9304 *task = strtol (tok, &tmptok, 0);
9305 if (tok == tmptok)
9306 error (_("Junk after task keyword."));
9307 if (!valid_task_id (*task))
9308 error (_("Unknown task %d."), *task);
9309 tok = tmptok;
9310 }
9311 else if (rest)
9312 {
9313 *rest = savestring (tok, strlen (tok));
9314 return;
9315 }
9316 else
9317 error (_("Junk at end of arguments."));
9318 }
9319 }
9320
9321 /* Call 'find_condition_and_thread' for each sal in SALS until a parse
9322 succeeds. The parsed values are written to COND_STRING, THREAD,
9323 TASK, and REST. See the comment of 'find_condition_and_thread'
9324 for the description of these parameters and INPUT. */
9325
9326 static void
9327 find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
9328 const char *input, char **cond_string,
9329 int *thread, int *task, char **rest)
9330 {
9331 int num_failures = 0;
9332 for (auto &sal : sals)
9333 {
9334 char *cond = nullptr;
9335 int thread_id = 0;
9336 int task_id = 0;
9337 char *remaining = nullptr;
9338
9339 /* Here we want to parse 'arg' to separate condition from thread
9340 number. But because parsing happens in a context and the
9341 contexts of sals might be different, try each until there is
9342 success. Finding one successful parse is sufficient for our
9343 goal. When setting the breakpoint we'll re-parse the
9344 condition in the context of each sal. */
9345 try
9346 {
9347 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
9348 &task_id, &remaining);
9349 *cond_string = cond;
9350 *thread = thread_id;
9351 *task = task_id;
9352 *rest = remaining;
9353 break;
9354 }
9355 catch (const gdb_exception_error &e)
9356 {
9357 num_failures++;
9358 /* If no sal remains, do not continue. */
9359 if (num_failures == sals.size ())
9360 throw;
9361 }
9362 }
9363 }
9364
9365 /* Decode a static tracepoint marker spec. */
9366
9367 static std::vector<symtab_and_line>
9368 decode_static_tracepoint_spec (const char **arg_p)
9369 {
9370 const char *p = &(*arg_p)[3];
9371 const char *endp;
9372
9373 p = skip_spaces (p);
9374
9375 endp = skip_to_space (p);
9376
9377 std::string marker_str (p, endp - p);
9378
9379 std::vector<static_tracepoint_marker> markers
9380 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9381 if (markers.empty ())
9382 error (_("No known static tracepoint marker named %s"),
9383 marker_str.c_str ());
9384
9385 std::vector<symtab_and_line> sals;
9386 sals.reserve (markers.size ());
9387
9388 for (const static_tracepoint_marker &marker : markers)
9389 {
9390 symtab_and_line sal = find_pc_line (marker.address, 0);
9391 sal.pc = marker.address;
9392 sals.push_back (sal);
9393 }
9394
9395 *arg_p = endp;
9396 return sals;
9397 }
9398
9399 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9400 according to IS_TRACEPOINT. */
9401
9402 static const struct breakpoint_ops *
9403 breakpoint_ops_for_event_location_type (enum event_location_type location_type,
9404 bool is_tracepoint)
9405 {
9406 if (is_tracepoint)
9407 {
9408 if (location_type == PROBE_LOCATION)
9409 return &tracepoint_probe_breakpoint_ops;
9410 else
9411 return &tracepoint_breakpoint_ops;
9412 }
9413 else
9414 {
9415 if (location_type == PROBE_LOCATION)
9416 return &bkpt_probe_breakpoint_ops;
9417 else
9418 return &bkpt_breakpoint_ops;
9419 }
9420 }
9421
9422 /* See breakpoint.h. */
9423
9424 const struct breakpoint_ops *
9425 breakpoint_ops_for_event_location (const struct event_location *location,
9426 bool is_tracepoint)
9427 {
9428 if (location != nullptr)
9429 return breakpoint_ops_for_event_location_type
9430 (event_location_type (location), is_tracepoint);
9431 return is_tracepoint ? &tracepoint_breakpoint_ops : &bkpt_breakpoint_ops;
9432 }
9433
9434 /* See breakpoint.h. */
9435
9436 int
9437 create_breakpoint (struct gdbarch *gdbarch,
9438 struct event_location *location,
9439 const char *cond_string,
9440 int thread, const char *extra_string,
9441 bool force_condition, int parse_extra,
9442 int tempflag, enum bptype type_wanted,
9443 int ignore_count,
9444 enum auto_boolean pending_break_support,
9445 const struct breakpoint_ops *ops,
9446 int from_tty, int enabled, int internal,
9447 unsigned flags)
9448 {
9449 struct linespec_result canonical;
9450 int pending = 0;
9451 int task = 0;
9452 int prev_bkpt_count = breakpoint_count;
9453
9454 gdb_assert (ops != NULL);
9455
9456 /* If extra_string isn't useful, set it to NULL. */
9457 if (extra_string != NULL && *extra_string == '\0')
9458 extra_string = NULL;
9459
9460 try
9461 {
9462 ops->create_sals_from_location (location, &canonical, type_wanted);
9463 }
9464 catch (const gdb_exception_error &e)
9465 {
9466 /* If caller is interested in rc value from parse, set
9467 value. */
9468 if (e.error == NOT_FOUND_ERROR)
9469 {
9470 /* If pending breakpoint support is turned off, throw
9471 error. */
9472
9473 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9474 throw;
9475
9476 exception_print (gdb_stderr, e);
9477
9478 /* If pending breakpoint support is auto query and the user
9479 selects no, then simply return the error code. */
9480 if (pending_break_support == AUTO_BOOLEAN_AUTO
9481 && !nquery (_("Make %s pending on future shared library load? "),
9482 bptype_string (type_wanted)))
9483 return 0;
9484
9485 /* At this point, either the user was queried about setting
9486 a pending breakpoint and selected yes, or pending
9487 breakpoint behavior is on and thus a pending breakpoint
9488 is defaulted on behalf of the user. */
9489 pending = 1;
9490 }
9491 else
9492 throw;
9493 }
9494
9495 if (!pending && canonical.lsals.empty ())
9496 return 0;
9497
9498 /* Resolve all line numbers to PC's and verify that the addresses
9499 are ok for the target. */
9500 if (!pending)
9501 {
9502 for (auto &lsal : canonical.lsals)
9503 breakpoint_sals_to_pc (lsal.sals);
9504 }
9505
9506 /* Fast tracepoints may have additional restrictions on location. */
9507 if (!pending && type_wanted == bp_fast_tracepoint)
9508 {
9509 for (const auto &lsal : canonical.lsals)
9510 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9511 }
9512
9513 /* Verify that condition can be parsed, before setting any
9514 breakpoints. Allocate a separate condition expression for each
9515 breakpoint. */
9516 if (!pending)
9517 {
9518 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9519 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9520
9521 if (parse_extra)
9522 {
9523 char *rest;
9524 char *cond;
9525
9526 const linespec_sals &lsal = canonical.lsals[0];
9527
9528 find_condition_and_thread_for_sals (lsal.sals, extra_string,
9529 &cond, &thread, &task, &rest);
9530 cond_string_copy.reset (cond);
9531 extra_string_copy.reset (rest);
9532 }
9533 else
9534 {
9535 if (type_wanted != bp_dprintf
9536 && extra_string != NULL && *extra_string != '\0')
9537 error (_("Garbage '%s' at end of location"), extra_string);
9538
9539 /* Check the validity of the condition. We should error out
9540 if the condition is invalid at all of the locations and
9541 if it is not forced. In the PARSE_EXTRA case above, this
9542 check is done when parsing the EXTRA_STRING. */
9543 if (cond_string != nullptr && !force_condition)
9544 {
9545 int num_failures = 0;
9546 const linespec_sals &lsal = canonical.lsals[0];
9547 for (const auto &sal : lsal.sals)
9548 {
9549 const char *cond = cond_string;
9550 try
9551 {
9552 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
9553 /* One success is sufficient to keep going. */
9554 break;
9555 }
9556 catch (const gdb_exception_error &)
9557 {
9558 num_failures++;
9559 /* If this is the last sal, error out. */
9560 if (num_failures == lsal.sals.size ())
9561 throw;
9562 }
9563 }
9564 }
9565
9566 /* Create a private copy of condition string. */
9567 if (cond_string)
9568 cond_string_copy.reset (xstrdup (cond_string));
9569 /* Create a private copy of any extra string. */
9570 if (extra_string)
9571 extra_string_copy.reset (xstrdup (extra_string));
9572 }
9573
9574 ops->create_breakpoints_sal (gdbarch, &canonical,
9575 std::move (cond_string_copy),
9576 std::move (extra_string_copy),
9577 type_wanted,
9578 tempflag ? disp_del : disp_donttouch,
9579 thread, task, ignore_count, ops,
9580 from_tty, enabled, internal, flags);
9581 }
9582 else
9583 {
9584 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9585
9586 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9587 b->location = copy_event_location (location);
9588
9589 if (parse_extra)
9590 b->cond_string = NULL;
9591 else
9592 {
9593 /* Create a private copy of condition string. */
9594 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
9595 b->thread = thread;
9596 }
9597
9598 /* Create a private copy of any extra string. */
9599 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
9600 b->ignore_count = ignore_count;
9601 b->disposition = tempflag ? disp_del : disp_donttouch;
9602 b->condition_not_parsed = 1;
9603 b->enable_state = enabled ? bp_enabled : bp_disabled;
9604 if ((type_wanted != bp_breakpoint
9605 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9606 b->pspace = current_program_space;
9607
9608 install_breakpoint (internal, std::move (b), 0);
9609 }
9610
9611 if (canonical.lsals.size () > 1)
9612 {
9613 warning (_("Multiple breakpoints were set.\nUse the "
9614 "\"delete\" command to delete unwanted breakpoints."));
9615 prev_breakpoint_count = prev_bkpt_count;
9616 }
9617
9618 update_global_location_list (UGLL_MAY_INSERT);
9619
9620 return 1;
9621 }
9622
9623 /* Set a breakpoint.
9624 ARG is a string describing breakpoint address,
9625 condition, and thread.
9626 FLAG specifies if a breakpoint is hardware on,
9627 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9628 and BP_TEMPFLAG. */
9629
9630 static void
9631 break_command_1 (const char *arg, int flag, int from_tty)
9632 {
9633 int tempflag = flag & BP_TEMPFLAG;
9634 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9635 ? bp_hardware_breakpoint
9636 : bp_breakpoint);
9637
9638 event_location_up location = string_to_event_location (&arg, current_language);
9639 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9640 (location.get (), false /* is_tracepoint */);
9641
9642 create_breakpoint (get_current_arch (),
9643 location.get (),
9644 NULL, 0, arg, false, 1 /* parse arg */,
9645 tempflag, type_wanted,
9646 0 /* Ignore count */,
9647 pending_break_support,
9648 ops,
9649 from_tty,
9650 1 /* enabled */,
9651 0 /* internal */,
9652 0);
9653 }
9654
9655 /* Helper function for break_command_1 and disassemble_command. */
9656
9657 void
9658 resolve_sal_pc (struct symtab_and_line *sal)
9659 {
9660 CORE_ADDR pc;
9661
9662 if (sal->pc == 0 && sal->symtab != NULL)
9663 {
9664 if (!find_line_pc (sal->symtab, sal->line, &pc))
9665 error (_("No line %d in file \"%s\"."),
9666 sal->line, symtab_to_filename_for_display (sal->symtab));
9667 sal->pc = pc;
9668
9669 /* If this SAL corresponds to a breakpoint inserted using a line
9670 number, then skip the function prologue if necessary. */
9671 if (sal->explicit_line)
9672 skip_prologue_sal (sal);
9673 }
9674
9675 if (sal->section == 0 && sal->symtab != NULL)
9676 {
9677 const struct blockvector *bv;
9678 const struct block *b;
9679 struct symbol *sym;
9680
9681 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9682 SYMTAB_COMPUNIT (sal->symtab));
9683 if (bv != NULL)
9684 {
9685 sym = block_linkage_function (b);
9686 if (sym != NULL)
9687 {
9688 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9689 sal->section = sym->obj_section (SYMTAB_OBJFILE (sal->symtab));
9690 }
9691 else
9692 {
9693 /* It really is worthwhile to have the section, so we'll
9694 just have to look harder. This case can be executed
9695 if we have line numbers but no functions (as can
9696 happen in assembly source). */
9697
9698 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9699 switch_to_program_space_and_thread (sal->pspace);
9700
9701 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9702 if (msym.minsym)
9703 sal->section = msym.obj_section ();
9704 }
9705 }
9706 }
9707 }
9708
9709 void
9710 break_command (const char *arg, int from_tty)
9711 {
9712 break_command_1 (arg, 0, from_tty);
9713 }
9714
9715 void
9716 tbreak_command (const char *arg, int from_tty)
9717 {
9718 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9719 }
9720
9721 static void
9722 hbreak_command (const char *arg, int from_tty)
9723 {
9724 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9725 }
9726
9727 static void
9728 thbreak_command (const char *arg, int from_tty)
9729 {
9730 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9731 }
9732
9733 static void
9734 stop_command (const char *arg, int from_tty)
9735 {
9736 printf_filtered (_("Specify the type of breakpoint to set.\n\
9737 Usage: stop in <function | address>\n\
9738 stop at <line>\n"));
9739 }
9740
9741 static void
9742 stopin_command (const char *arg, int from_tty)
9743 {
9744 int badInput = 0;
9745
9746 if (arg == NULL)
9747 badInput = 1;
9748 else if (*arg != '*')
9749 {
9750 const char *argptr = arg;
9751 int hasColon = 0;
9752
9753 /* Look for a ':'. If this is a line number specification, then
9754 say it is bad, otherwise, it should be an address or
9755 function/method name. */
9756 while (*argptr && !hasColon)
9757 {
9758 hasColon = (*argptr == ':');
9759 argptr++;
9760 }
9761
9762 if (hasColon)
9763 badInput = (*argptr != ':'); /* Not a class::method */
9764 else
9765 badInput = isdigit (*arg); /* a simple line number */
9766 }
9767
9768 if (badInput)
9769 printf_filtered (_("Usage: stop in <function | address>\n"));
9770 else
9771 break_command_1 (arg, 0, from_tty);
9772 }
9773
9774 static void
9775 stopat_command (const char *arg, int from_tty)
9776 {
9777 int badInput = 0;
9778
9779 if (arg == NULL || *arg == '*') /* no line number */
9780 badInput = 1;
9781 else
9782 {
9783 const char *argptr = arg;
9784 int hasColon = 0;
9785
9786 /* Look for a ':'. If there is a '::' then get out, otherwise
9787 it is probably a line number. */
9788 while (*argptr && !hasColon)
9789 {
9790 hasColon = (*argptr == ':');
9791 argptr++;
9792 }
9793
9794 if (hasColon)
9795 badInput = (*argptr == ':'); /* we have class::method */
9796 else
9797 badInput = !isdigit (*arg); /* not a line number */
9798 }
9799
9800 if (badInput)
9801 printf_filtered (_("Usage: stop at LINE\n"));
9802 else
9803 break_command_1 (arg, 0, from_tty);
9804 }
9805
9806 /* The dynamic printf command is mostly like a regular breakpoint, but
9807 with a prewired command list consisting of a single output command,
9808 built from extra arguments supplied on the dprintf command
9809 line. */
9810
9811 static void
9812 dprintf_command (const char *arg, int from_tty)
9813 {
9814 event_location_up location = string_to_event_location (&arg, current_language);
9815
9816 /* If non-NULL, ARG should have been advanced past the location;
9817 the next character must be ','. */
9818 if (arg != NULL)
9819 {
9820 if (arg[0] != ',' || arg[1] == '\0')
9821 error (_("Format string required"));
9822 else
9823 {
9824 /* Skip the comma. */
9825 ++arg;
9826 }
9827 }
9828
9829 create_breakpoint (get_current_arch (),
9830 location.get (),
9831 NULL, 0, arg, false, 1 /* parse arg */,
9832 0, bp_dprintf,
9833 0 /* Ignore count */,
9834 pending_break_support,
9835 &dprintf_breakpoint_ops,
9836 from_tty,
9837 1 /* enabled */,
9838 0 /* internal */,
9839 0);
9840 }
9841
9842 static void
9843 agent_printf_command (const char *arg, int from_tty)
9844 {
9845 error (_("May only run agent-printf on the target"));
9846 }
9847
9848 /* Implement the "breakpoint_hit" breakpoint_ops method for
9849 ranged breakpoints. */
9850
9851 static int
9852 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9853 const address_space *aspace,
9854 CORE_ADDR bp_addr,
9855 const struct target_waitstatus *ws)
9856 {
9857 if (ws->kind != TARGET_WAITKIND_STOPPED
9858 || ws->value.sig != GDB_SIGNAL_TRAP)
9859 return 0;
9860
9861 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9862 bl->length, aspace, bp_addr);
9863 }
9864
9865 /* Implement the "resources_needed" breakpoint_ops method for
9866 ranged breakpoints. */
9867
9868 static int
9869 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9870 {
9871 return target_ranged_break_num_registers ();
9872 }
9873
9874 /* Implement the "print_it" breakpoint_ops method for
9875 ranged breakpoints. */
9876
9877 static enum print_stop_action
9878 print_it_ranged_breakpoint (bpstat bs)
9879 {
9880 struct breakpoint *b = bs->breakpoint_at;
9881 struct bp_location *bl = b->loc;
9882 struct ui_out *uiout = current_uiout;
9883
9884 gdb_assert (b->type == bp_hardware_breakpoint);
9885
9886 /* Ranged breakpoints have only one location. */
9887 gdb_assert (bl && bl->next == NULL);
9888
9889 annotate_breakpoint (b->number);
9890
9891 maybe_print_thread_hit_breakpoint (uiout);
9892
9893 if (b->disposition == disp_del)
9894 uiout->text ("Temporary ranged breakpoint ");
9895 else
9896 uiout->text ("Ranged breakpoint ");
9897 if (uiout->is_mi_like_p ())
9898 {
9899 uiout->field_string ("reason",
9900 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9901 uiout->field_string ("disp", bpdisp_text (b->disposition));
9902 }
9903 uiout->field_signed ("bkptno", b->number);
9904 uiout->text (", ");
9905
9906 return PRINT_SRC_AND_LOC;
9907 }
9908
9909 /* Implement the "print_one" breakpoint_ops method for
9910 ranged breakpoints. */
9911
9912 static void
9913 print_one_ranged_breakpoint (struct breakpoint *b,
9914 struct bp_location **last_loc)
9915 {
9916 struct bp_location *bl = b->loc;
9917 struct value_print_options opts;
9918 struct ui_out *uiout = current_uiout;
9919
9920 /* Ranged breakpoints have only one location. */
9921 gdb_assert (bl && bl->next == NULL);
9922
9923 get_user_print_options (&opts);
9924
9925 if (opts.addressprint)
9926 /* We don't print the address range here, it will be printed later
9927 by print_one_detail_ranged_breakpoint. */
9928 uiout->field_skip ("addr");
9929 annotate_field (5);
9930 print_breakpoint_location (b, bl);
9931 *last_loc = bl;
9932 }
9933
9934 /* Implement the "print_one_detail" breakpoint_ops method for
9935 ranged breakpoints. */
9936
9937 static void
9938 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9939 struct ui_out *uiout)
9940 {
9941 CORE_ADDR address_start, address_end;
9942 struct bp_location *bl = b->loc;
9943 string_file stb;
9944
9945 gdb_assert (bl);
9946
9947 address_start = bl->address;
9948 address_end = address_start + bl->length - 1;
9949
9950 uiout->text ("\taddress range: ");
9951 stb.printf ("[%s, %s]",
9952 print_core_address (bl->gdbarch, address_start),
9953 print_core_address (bl->gdbarch, address_end));
9954 uiout->field_stream ("addr", stb);
9955 uiout->text ("\n");
9956 }
9957
9958 /* Implement the "print_mention" breakpoint_ops method for
9959 ranged breakpoints. */
9960
9961 static void
9962 print_mention_ranged_breakpoint (struct breakpoint *b)
9963 {
9964 struct bp_location *bl = b->loc;
9965 struct ui_out *uiout = current_uiout;
9966
9967 gdb_assert (bl);
9968 gdb_assert (b->type == bp_hardware_breakpoint);
9969
9970 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9971 b->number, paddress (bl->gdbarch, bl->address),
9972 paddress (bl->gdbarch, bl->address + bl->length - 1));
9973 }
9974
9975 /* Implement the "print_recreate" breakpoint_ops method for
9976 ranged breakpoints. */
9977
9978 static void
9979 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9980 {
9981 fprintf_unfiltered (fp, "break-range %s, %s",
9982 event_location_to_string (b->location.get ()),
9983 event_location_to_string (b->location_range_end.get ()));
9984 print_recreate_thread (b, fp);
9985 }
9986
9987 /* The breakpoint_ops structure to be used in ranged breakpoints. */
9988
9989 static struct breakpoint_ops ranged_breakpoint_ops;
9990
9991 /* Find the address where the end of the breakpoint range should be
9992 placed, given the SAL of the end of the range. This is so that if
9993 the user provides a line number, the end of the range is set to the
9994 last instruction of the given line. */
9995
9996 static CORE_ADDR
9997 find_breakpoint_range_end (struct symtab_and_line sal)
9998 {
9999 CORE_ADDR end;
10000
10001 /* If the user provided a PC value, use it. Otherwise,
10002 find the address of the end of the given location. */
10003 if (sal.explicit_pc)
10004 end = sal.pc;
10005 else
10006 {
10007 int ret;
10008 CORE_ADDR start;
10009
10010 ret = find_line_pc_range (sal, &start, &end);
10011 if (!ret)
10012 error (_("Could not find location of the end of the range."));
10013
10014 /* find_line_pc_range returns the start of the next line. */
10015 end--;
10016 }
10017
10018 return end;
10019 }
10020
10021 /* Implement the "break-range" CLI command. */
10022
10023 static void
10024 break_range_command (const char *arg, int from_tty)
10025 {
10026 const char *arg_start;
10027 struct linespec_result canonical_start, canonical_end;
10028 int bp_count, can_use_bp, length;
10029 CORE_ADDR end;
10030 struct breakpoint *b;
10031
10032 /* We don't support software ranged breakpoints. */
10033 if (target_ranged_break_num_registers () < 0)
10034 error (_("This target does not support hardware ranged breakpoints."));
10035
10036 bp_count = hw_breakpoint_used_count ();
10037 bp_count += target_ranged_break_num_registers ();
10038 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10039 bp_count, 0);
10040 if (can_use_bp < 0)
10041 error (_("Hardware breakpoints used exceeds limit."));
10042
10043 arg = skip_spaces (arg);
10044 if (arg == NULL || arg[0] == '\0')
10045 error(_("No address range specified."));
10046
10047 arg_start = arg;
10048 event_location_up start_location = string_to_event_location (&arg,
10049 current_language);
10050 parse_breakpoint_sals (start_location.get (), &canonical_start);
10051
10052 if (arg[0] != ',')
10053 error (_("Too few arguments."));
10054 else if (canonical_start.lsals.empty ())
10055 error (_("Could not find location of the beginning of the range."));
10056
10057 const linespec_sals &lsal_start = canonical_start.lsals[0];
10058
10059 if (canonical_start.lsals.size () > 1
10060 || lsal_start.sals.size () != 1)
10061 error (_("Cannot create a ranged breakpoint with multiple locations."));
10062
10063 const symtab_and_line &sal_start = lsal_start.sals[0];
10064 std::string addr_string_start (arg_start, arg - arg_start);
10065
10066 arg++; /* Skip the comma. */
10067 arg = skip_spaces (arg);
10068
10069 /* Parse the end location. */
10070
10071 arg_start = arg;
10072
10073 /* We call decode_line_full directly here instead of using
10074 parse_breakpoint_sals because we need to specify the start location's
10075 symtab and line as the default symtab and line for the end of the
10076 range. This makes it possible to have ranges like "foo.c:27, +14",
10077 where +14 means 14 lines from the start location. */
10078 event_location_up end_location = string_to_event_location (&arg,
10079 current_language);
10080 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10081 sal_start.symtab, sal_start.line,
10082 &canonical_end, NULL, NULL);
10083
10084 if (canonical_end.lsals.empty ())
10085 error (_("Could not find location of the end of the range."));
10086
10087 const linespec_sals &lsal_end = canonical_end.lsals[0];
10088 if (canonical_end.lsals.size () > 1
10089 || lsal_end.sals.size () != 1)
10090 error (_("Cannot create a ranged breakpoint with multiple locations."));
10091
10092 const symtab_and_line &sal_end = lsal_end.sals[0];
10093
10094 end = find_breakpoint_range_end (sal_end);
10095 if (sal_start.pc > end)
10096 error (_("Invalid address range, end precedes start."));
10097
10098 length = end - sal_start.pc + 1;
10099 if (length < 0)
10100 /* Length overflowed. */
10101 error (_("Address range too large."));
10102 else if (length == 1)
10103 {
10104 /* This range is simple enough to be handled by
10105 the `hbreak' command. */
10106 hbreak_command (&addr_string_start[0], 1);
10107
10108 return;
10109 }
10110
10111 /* Now set up the breakpoint. */
10112 b = set_raw_breakpoint (get_current_arch (), sal_start,
10113 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10114 set_breakpoint_count (breakpoint_count + 1);
10115 b->number = breakpoint_count;
10116 b->disposition = disp_donttouch;
10117 b->location = std::move (start_location);
10118 b->location_range_end = std::move (end_location);
10119 b->loc->length = length;
10120
10121 mention (b);
10122 gdb::observers::breakpoint_created.notify (b);
10123 update_global_location_list (UGLL_MAY_INSERT);
10124 }
10125
10126 /* Return non-zero if EXP is verified as constant. Returned zero
10127 means EXP is variable. Also the constant detection may fail for
10128 some constant expressions and in such case still falsely return
10129 zero. */
10130
10131 static bool
10132 watchpoint_exp_is_const (const struct expression *exp)
10133 {
10134 return exp->op->constant_p ();
10135 }
10136
10137 /* Watchpoint destructor. */
10138
10139 watchpoint::~watchpoint ()
10140 {
10141 xfree (this->exp_string);
10142 xfree (this->exp_string_reparse);
10143 }
10144
10145 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10146
10147 static void
10148 re_set_watchpoint (struct breakpoint *b)
10149 {
10150 struct watchpoint *w = (struct watchpoint *) b;
10151
10152 /* Watchpoint can be either on expression using entirely global
10153 variables, or it can be on local variables.
10154
10155 Watchpoints of the first kind are never auto-deleted, and even
10156 persist across program restarts. Since they can use variables
10157 from shared libraries, we need to reparse expression as libraries
10158 are loaded and unloaded.
10159
10160 Watchpoints on local variables can also change meaning as result
10161 of solib event. For example, if a watchpoint uses both a local
10162 and a global variables in expression, it's a local watchpoint,
10163 but unloading of a shared library will make the expression
10164 invalid. This is not a very common use case, but we still
10165 re-evaluate expression, to avoid surprises to the user.
10166
10167 Note that for local watchpoints, we re-evaluate it only if
10168 watchpoints frame id is still valid. If it's not, it means the
10169 watchpoint is out of scope and will be deleted soon. In fact,
10170 I'm not sure we'll ever be called in this case.
10171
10172 If a local watchpoint's frame id is still valid, then
10173 w->exp_valid_block is likewise valid, and we can safely use it.
10174
10175 Don't do anything about disabled watchpoints, since they will be
10176 reevaluated again when enabled. */
10177 update_watchpoint (w, 1 /* reparse */);
10178 }
10179
10180 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10181
10182 static int
10183 insert_watchpoint (struct bp_location *bl)
10184 {
10185 struct watchpoint *w = (struct watchpoint *) bl->owner;
10186 int length = w->exact ? 1 : bl->length;
10187
10188 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10189 w->cond_exp.get ());
10190 }
10191
10192 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10193
10194 static int
10195 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10196 {
10197 struct watchpoint *w = (struct watchpoint *) bl->owner;
10198 int length = w->exact ? 1 : bl->length;
10199
10200 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10201 w->cond_exp.get ());
10202 }
10203
10204 static int
10205 breakpoint_hit_watchpoint (const struct bp_location *bl,
10206 const address_space *aspace, CORE_ADDR bp_addr,
10207 const struct target_waitstatus *ws)
10208 {
10209 struct breakpoint *b = bl->owner;
10210 struct watchpoint *w = (struct watchpoint *) b;
10211
10212 /* Continuable hardware watchpoints are treated as non-existent if the
10213 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10214 some data address). Otherwise gdb won't stop on a break instruction
10215 in the code (not from a breakpoint) when a hardware watchpoint has
10216 been defined. Also skip watchpoints which we know did not trigger
10217 (did not match the data address). */
10218 if (is_hardware_watchpoint (b)
10219 && w->watchpoint_triggered == watch_triggered_no)
10220 return 0;
10221
10222 return 1;
10223 }
10224
10225 static void
10226 check_status_watchpoint (bpstat bs)
10227 {
10228 gdb_assert (is_watchpoint (bs->breakpoint_at));
10229
10230 bpstat_check_watchpoint (bs);
10231 }
10232
10233 /* Implement the "resources_needed" breakpoint_ops method for
10234 hardware watchpoints. */
10235
10236 static int
10237 resources_needed_watchpoint (const struct bp_location *bl)
10238 {
10239 struct watchpoint *w = (struct watchpoint *) bl->owner;
10240 int length = w->exact? 1 : bl->length;
10241
10242 return target_region_ok_for_hw_watchpoint (bl->address, length);
10243 }
10244
10245 /* Implement the "works_in_software_mode" breakpoint_ops method for
10246 hardware watchpoints. */
10247
10248 static int
10249 works_in_software_mode_watchpoint (const struct breakpoint *b)
10250 {
10251 /* Read and access watchpoints only work with hardware support. */
10252 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10253 }
10254
10255 static enum print_stop_action
10256 print_it_watchpoint (bpstat bs)
10257 {
10258 struct breakpoint *b;
10259 enum print_stop_action result;
10260 struct watchpoint *w;
10261 struct ui_out *uiout = current_uiout;
10262
10263 gdb_assert (bs->bp_location_at != NULL);
10264
10265 b = bs->breakpoint_at;
10266 w = (struct watchpoint *) b;
10267
10268 annotate_watchpoint (b->number);
10269 maybe_print_thread_hit_breakpoint (uiout);
10270
10271 string_file stb;
10272
10273 gdb::optional<ui_out_emit_tuple> tuple_emitter;
10274 switch (b->type)
10275 {
10276 case bp_watchpoint:
10277 case bp_hardware_watchpoint:
10278 if (uiout->is_mi_like_p ())
10279 uiout->field_string
10280 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10281 mention (b);
10282 tuple_emitter.emplace (uiout, "value");
10283 uiout->text ("\nOld value = ");
10284 watchpoint_value_print (bs->old_val.get (), &stb);
10285 uiout->field_stream ("old", stb);
10286 uiout->text ("\nNew value = ");
10287 watchpoint_value_print (w->val.get (), &stb);
10288 uiout->field_stream ("new", stb);
10289 uiout->text ("\n");
10290 /* More than one watchpoint may have been triggered. */
10291 result = PRINT_UNKNOWN;
10292 break;
10293
10294 case bp_read_watchpoint:
10295 if (uiout->is_mi_like_p ())
10296 uiout->field_string
10297 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10298 mention (b);
10299 tuple_emitter.emplace (uiout, "value");
10300 uiout->text ("\nValue = ");
10301 watchpoint_value_print (w->val.get (), &stb);
10302 uiout->field_stream ("value", stb);
10303 uiout->text ("\n");
10304 result = PRINT_UNKNOWN;
10305 break;
10306
10307 case bp_access_watchpoint:
10308 if (bs->old_val != NULL)
10309 {
10310 if (uiout->is_mi_like_p ())
10311 uiout->field_string
10312 ("reason",
10313 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10314 mention (b);
10315 tuple_emitter.emplace (uiout, "value");
10316 uiout->text ("\nOld value = ");
10317 watchpoint_value_print (bs->old_val.get (), &stb);
10318 uiout->field_stream ("old", stb);
10319 uiout->text ("\nNew value = ");
10320 }
10321 else
10322 {
10323 mention (b);
10324 if (uiout->is_mi_like_p ())
10325 uiout->field_string
10326 ("reason",
10327 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10328 tuple_emitter.emplace (uiout, "value");
10329 uiout->text ("\nValue = ");
10330 }
10331 watchpoint_value_print (w->val.get (), &stb);
10332 uiout->field_stream ("new", stb);
10333 uiout->text ("\n");
10334 result = PRINT_UNKNOWN;
10335 break;
10336 default:
10337 result = PRINT_UNKNOWN;
10338 }
10339
10340 return result;
10341 }
10342
10343 /* Implement the "print_mention" breakpoint_ops method for hardware
10344 watchpoints. */
10345
10346 static void
10347 print_mention_watchpoint (struct breakpoint *b)
10348 {
10349 struct watchpoint *w = (struct watchpoint *) b;
10350 struct ui_out *uiout = current_uiout;
10351 const char *tuple_name;
10352
10353 switch (b->type)
10354 {
10355 case bp_watchpoint:
10356 uiout->text ("Watchpoint ");
10357 tuple_name = "wpt";
10358 break;
10359 case bp_hardware_watchpoint:
10360 uiout->text ("Hardware watchpoint ");
10361 tuple_name = "wpt";
10362 break;
10363 case bp_read_watchpoint:
10364 uiout->text ("Hardware read watchpoint ");
10365 tuple_name = "hw-rwpt";
10366 break;
10367 case bp_access_watchpoint:
10368 uiout->text ("Hardware access (read/write) watchpoint ");
10369 tuple_name = "hw-awpt";
10370 break;
10371 default:
10372 internal_error (__FILE__, __LINE__,
10373 _("Invalid hardware watchpoint type."));
10374 }
10375
10376 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10377 uiout->field_signed ("number", b->number);
10378 uiout->text (": ");
10379 uiout->field_string ("exp", w->exp_string);
10380 }
10381
10382 /* Implement the "print_recreate" breakpoint_ops method for
10383 watchpoints. */
10384
10385 static void
10386 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10387 {
10388 struct watchpoint *w = (struct watchpoint *) b;
10389
10390 switch (b->type)
10391 {
10392 case bp_watchpoint:
10393 case bp_hardware_watchpoint:
10394 fprintf_unfiltered (fp, "watch");
10395 break;
10396 case bp_read_watchpoint:
10397 fprintf_unfiltered (fp, "rwatch");
10398 break;
10399 case bp_access_watchpoint:
10400 fprintf_unfiltered (fp, "awatch");
10401 break;
10402 default:
10403 internal_error (__FILE__, __LINE__,
10404 _("Invalid watchpoint type."));
10405 }
10406
10407 fprintf_unfiltered (fp, " %s", w->exp_string);
10408 print_recreate_thread (b, fp);
10409 }
10410
10411 /* Implement the "explains_signal" breakpoint_ops method for
10412 watchpoints. */
10413
10414 static int
10415 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10416 {
10417 /* A software watchpoint cannot cause a signal other than
10418 GDB_SIGNAL_TRAP. */
10419 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10420 return 0;
10421
10422 return 1;
10423 }
10424
10425 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10426
10427 static struct breakpoint_ops watchpoint_breakpoint_ops;
10428
10429 /* Implement the "insert" breakpoint_ops method for
10430 masked hardware watchpoints. */
10431
10432 static int
10433 insert_masked_watchpoint (struct bp_location *bl)
10434 {
10435 struct watchpoint *w = (struct watchpoint *) bl->owner;
10436
10437 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10438 bl->watchpoint_type);
10439 }
10440
10441 /* Implement the "remove" breakpoint_ops method for
10442 masked hardware watchpoints. */
10443
10444 static int
10445 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10446 {
10447 struct watchpoint *w = (struct watchpoint *) bl->owner;
10448
10449 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10450 bl->watchpoint_type);
10451 }
10452
10453 /* Implement the "resources_needed" breakpoint_ops method for
10454 masked hardware watchpoints. */
10455
10456 static int
10457 resources_needed_masked_watchpoint (const struct bp_location *bl)
10458 {
10459 struct watchpoint *w = (struct watchpoint *) bl->owner;
10460
10461 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10462 }
10463
10464 /* Implement the "works_in_software_mode" breakpoint_ops method for
10465 masked hardware watchpoints. */
10466
10467 static int
10468 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10469 {
10470 return 0;
10471 }
10472
10473 /* Implement the "print_it" breakpoint_ops method for
10474 masked hardware watchpoints. */
10475
10476 static enum print_stop_action
10477 print_it_masked_watchpoint (bpstat bs)
10478 {
10479 struct breakpoint *b = bs->breakpoint_at;
10480 struct ui_out *uiout = current_uiout;
10481
10482 /* Masked watchpoints have only one location. */
10483 gdb_assert (b->loc && b->loc->next == NULL);
10484
10485 annotate_watchpoint (b->number);
10486 maybe_print_thread_hit_breakpoint (uiout);
10487
10488 switch (b->type)
10489 {
10490 case bp_hardware_watchpoint:
10491 if (uiout->is_mi_like_p ())
10492 uiout->field_string
10493 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10494 break;
10495
10496 case bp_read_watchpoint:
10497 if (uiout->is_mi_like_p ())
10498 uiout->field_string
10499 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10500 break;
10501
10502 case bp_access_watchpoint:
10503 if (uiout->is_mi_like_p ())
10504 uiout->field_string
10505 ("reason",
10506 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10507 break;
10508 default:
10509 internal_error (__FILE__, __LINE__,
10510 _("Invalid hardware watchpoint type."));
10511 }
10512
10513 mention (b);
10514 uiout->text (_("\n\
10515 Check the underlying instruction at PC for the memory\n\
10516 address and value which triggered this watchpoint.\n"));
10517 uiout->text ("\n");
10518
10519 /* More than one watchpoint may have been triggered. */
10520 return PRINT_UNKNOWN;
10521 }
10522
10523 /* Implement the "print_one_detail" breakpoint_ops method for
10524 masked hardware watchpoints. */
10525
10526 static void
10527 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10528 struct ui_out *uiout)
10529 {
10530 struct watchpoint *w = (struct watchpoint *) b;
10531
10532 /* Masked watchpoints have only one location. */
10533 gdb_assert (b->loc && b->loc->next == NULL);
10534
10535 uiout->text ("\tmask ");
10536 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10537 uiout->text ("\n");
10538 }
10539
10540 /* Implement the "print_mention" breakpoint_ops method for
10541 masked hardware watchpoints. */
10542
10543 static void
10544 print_mention_masked_watchpoint (struct breakpoint *b)
10545 {
10546 struct watchpoint *w = (struct watchpoint *) b;
10547 struct ui_out *uiout = current_uiout;
10548 const char *tuple_name;
10549
10550 switch (b->type)
10551 {
10552 case bp_hardware_watchpoint:
10553 uiout->text ("Masked hardware watchpoint ");
10554 tuple_name = "wpt";
10555 break;
10556 case bp_read_watchpoint:
10557 uiout->text ("Masked hardware read watchpoint ");
10558 tuple_name = "hw-rwpt";
10559 break;
10560 case bp_access_watchpoint:
10561 uiout->text ("Masked hardware access (read/write) watchpoint ");
10562 tuple_name = "hw-awpt";
10563 break;
10564 default:
10565 internal_error (__FILE__, __LINE__,
10566 _("Invalid hardware watchpoint type."));
10567 }
10568
10569 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10570 uiout->field_signed ("number", b->number);
10571 uiout->text (": ");
10572 uiout->field_string ("exp", w->exp_string);
10573 }
10574
10575 /* Implement the "print_recreate" breakpoint_ops method for
10576 masked hardware watchpoints. */
10577
10578 static void
10579 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10580 {
10581 struct watchpoint *w = (struct watchpoint *) b;
10582
10583 switch (b->type)
10584 {
10585 case bp_hardware_watchpoint:
10586 fprintf_unfiltered (fp, "watch");
10587 break;
10588 case bp_read_watchpoint:
10589 fprintf_unfiltered (fp, "rwatch");
10590 break;
10591 case bp_access_watchpoint:
10592 fprintf_unfiltered (fp, "awatch");
10593 break;
10594 default:
10595 internal_error (__FILE__, __LINE__,
10596 _("Invalid hardware watchpoint type."));
10597 }
10598
10599 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string,
10600 phex (w->hw_wp_mask, sizeof (CORE_ADDR)));
10601 print_recreate_thread (b, fp);
10602 }
10603
10604 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10605
10606 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10607
10608 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10609
10610 static bool
10611 is_masked_watchpoint (const struct breakpoint *b)
10612 {
10613 return b->ops == &masked_watchpoint_breakpoint_ops;
10614 }
10615
10616 /* accessflag: hw_write: watch write,
10617 hw_read: watch read,
10618 hw_access: watch access (read or write) */
10619 static void
10620 watch_command_1 (const char *arg, int accessflag, int from_tty,
10621 bool just_location, bool internal)
10622 {
10623 struct breakpoint *scope_breakpoint = NULL;
10624 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10625 struct value *result;
10626 int saved_bitpos = 0, saved_bitsize = 0;
10627 const char *exp_start = NULL;
10628 const char *exp_end = NULL;
10629 const char *tok, *end_tok;
10630 int toklen = -1;
10631 const char *cond_start = NULL;
10632 const char *cond_end = NULL;
10633 enum bptype bp_type;
10634 int thread = -1;
10635 /* Flag to indicate whether we are going to use masks for
10636 the hardware watchpoint. */
10637 bool use_mask = false;
10638 CORE_ADDR mask = 0;
10639
10640 /* Make sure that we actually have parameters to parse. */
10641 if (arg != NULL && arg[0] != '\0')
10642 {
10643 const char *value_start;
10644
10645 exp_end = arg + strlen (arg);
10646
10647 /* Look for "parameter value" pairs at the end
10648 of the arguments string. */
10649 for (tok = exp_end - 1; tok > arg; tok--)
10650 {
10651 /* Skip whitespace at the end of the argument list. */
10652 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10653 tok--;
10654
10655 /* Find the beginning of the last token.
10656 This is the value of the parameter. */
10657 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10658 tok--;
10659 value_start = tok + 1;
10660
10661 /* Skip whitespace. */
10662 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10663 tok--;
10664
10665 end_tok = tok;
10666
10667 /* Find the beginning of the second to last token.
10668 This is the parameter itself. */
10669 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10670 tok--;
10671 tok++;
10672 toklen = end_tok - tok + 1;
10673
10674 if (toklen == 6 && startswith (tok, "thread"))
10675 {
10676 struct thread_info *thr;
10677 /* At this point we've found a "thread" token, which means
10678 the user is trying to set a watchpoint that triggers
10679 only in a specific thread. */
10680 const char *endp;
10681
10682 if (thread != -1)
10683 error(_("You can specify only one thread."));
10684
10685 /* Extract the thread ID from the next token. */
10686 thr = parse_thread_id (value_start, &endp);
10687
10688 /* Check if the user provided a valid thread ID. */
10689 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10690 invalid_thread_id_error (value_start);
10691
10692 thread = thr->global_num;
10693 }
10694 else if (toklen == 4 && startswith (tok, "mask"))
10695 {
10696 /* We've found a "mask" token, which means the user wants to
10697 create a hardware watchpoint that is going to have the mask
10698 facility. */
10699 struct value *mask_value, *mark;
10700
10701 if (use_mask)
10702 error(_("You can specify only one mask."));
10703
10704 use_mask = just_location = true;
10705
10706 mark = value_mark ();
10707 mask_value = parse_to_comma_and_eval (&value_start);
10708 mask = value_as_address (mask_value);
10709 value_free_to_mark (mark);
10710 }
10711 else
10712 /* We didn't recognize what we found. We should stop here. */
10713 break;
10714
10715 /* Truncate the string and get rid of the "parameter value" pair before
10716 the arguments string is parsed by the parse_exp_1 function. */
10717 exp_end = tok;
10718 }
10719 }
10720 else
10721 exp_end = arg;
10722
10723 /* Parse the rest of the arguments. From here on out, everything
10724 is in terms of a newly allocated string instead of the original
10725 ARG. */
10726 std::string expression (arg, exp_end - arg);
10727 exp_start = arg = expression.c_str ();
10728 innermost_block_tracker tracker;
10729 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10730 exp_end = arg;
10731 /* Remove trailing whitespace from the expression before saving it.
10732 This makes the eventual display of the expression string a bit
10733 prettier. */
10734 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10735 --exp_end;
10736
10737 /* Checking if the expression is not constant. */
10738 if (watchpoint_exp_is_const (exp.get ()))
10739 {
10740 int len;
10741
10742 len = exp_end - exp_start;
10743 while (len > 0 && isspace (exp_start[len - 1]))
10744 len--;
10745 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10746 }
10747
10748 exp_valid_block = tracker.block ();
10749 struct value *mark = value_mark ();
10750 struct value *val_as_value = nullptr;
10751 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10752 just_location);
10753
10754 if (val_as_value != NULL && just_location)
10755 {
10756 saved_bitpos = value_bitpos (val_as_value);
10757 saved_bitsize = value_bitsize (val_as_value);
10758 }
10759
10760 value_ref_ptr val;
10761 if (just_location)
10762 {
10763 int ret;
10764
10765 exp_valid_block = NULL;
10766 val = release_value (value_addr (result));
10767 value_free_to_mark (mark);
10768
10769 if (use_mask)
10770 {
10771 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10772 mask);
10773 if (ret == -1)
10774 error (_("This target does not support masked watchpoints."));
10775 else if (ret == -2)
10776 error (_("Invalid mask or memory region."));
10777 }
10778 }
10779 else if (val_as_value != NULL)
10780 val = release_value (val_as_value);
10781
10782 tok = skip_spaces (arg);
10783 end_tok = skip_to_space (tok);
10784
10785 toklen = end_tok - tok;
10786 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10787 {
10788 tok = cond_start = end_tok + 1;
10789 innermost_block_tracker if_tracker;
10790 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10791
10792 /* The watchpoint expression may not be local, but the condition
10793 may still be. E.g.: `watch global if local > 0'. */
10794 cond_exp_valid_block = if_tracker.block ();
10795
10796 cond_end = tok;
10797 }
10798 if (*tok)
10799 error (_("Junk at end of command."));
10800
10801 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10802
10803 /* Save this because create_internal_breakpoint below invalidates
10804 'wp_frame'. */
10805 frame_id watchpoint_frame = get_frame_id (wp_frame);
10806
10807 /* If the expression is "local", then set up a "watchpoint scope"
10808 breakpoint at the point where we've left the scope of the watchpoint
10809 expression. Create the scope breakpoint before the watchpoint, so
10810 that we will encounter it first in bpstat_stop_status. */
10811 if (exp_valid_block != NULL && wp_frame != NULL)
10812 {
10813 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10814
10815 if (frame_id_p (caller_frame_id))
10816 {
10817 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10818 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10819
10820 scope_breakpoint
10821 = create_internal_breakpoint (caller_arch, caller_pc,
10822 bp_watchpoint_scope,
10823 &momentary_breakpoint_ops);
10824
10825 /* create_internal_breakpoint could invalidate WP_FRAME. */
10826 wp_frame = NULL;
10827
10828 scope_breakpoint->enable_state = bp_enabled;
10829
10830 /* Automatically delete the breakpoint when it hits. */
10831 scope_breakpoint->disposition = disp_del;
10832
10833 /* Only break in the proper frame (help with recursion). */
10834 scope_breakpoint->frame_id = caller_frame_id;
10835
10836 /* Set the address at which we will stop. */
10837 scope_breakpoint->loc->gdbarch = caller_arch;
10838 scope_breakpoint->loc->requested_address = caller_pc;
10839 scope_breakpoint->loc->address
10840 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10841 scope_breakpoint->loc->requested_address,
10842 scope_breakpoint->type);
10843 }
10844 }
10845
10846 /* Now set up the breakpoint. We create all watchpoints as hardware
10847 watchpoints here even if hardware watchpoints are turned off, a call
10848 to update_watchpoint later in this function will cause the type to
10849 drop back to bp_watchpoint (software watchpoint) if required. */
10850
10851 if (accessflag == hw_read)
10852 bp_type = bp_read_watchpoint;
10853 else if (accessflag == hw_access)
10854 bp_type = bp_access_watchpoint;
10855 else
10856 bp_type = bp_hardware_watchpoint;
10857
10858 std::unique_ptr<watchpoint> w (new watchpoint ());
10859
10860 if (use_mask)
10861 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10862 &masked_watchpoint_breakpoint_ops);
10863 else
10864 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10865 &watchpoint_breakpoint_ops);
10866 w->thread = thread;
10867 w->disposition = disp_donttouch;
10868 w->pspace = current_program_space;
10869 w->exp = std::move (exp);
10870 w->exp_valid_block = exp_valid_block;
10871 w->cond_exp_valid_block = cond_exp_valid_block;
10872 if (just_location)
10873 {
10874 struct type *t = value_type (val.get ());
10875 CORE_ADDR addr = value_as_address (val.get ());
10876
10877 w->exp_string_reparse
10878 = current_language->watch_location_expression (t, addr).release ();
10879
10880 w->exp_string = xstrprintf ("-location %.*s",
10881 (int) (exp_end - exp_start), exp_start);
10882 }
10883 else
10884 w->exp_string = savestring (exp_start, exp_end - exp_start);
10885
10886 if (use_mask)
10887 {
10888 w->hw_wp_mask = mask;
10889 }
10890 else
10891 {
10892 w->val = val;
10893 w->val_bitpos = saved_bitpos;
10894 w->val_bitsize = saved_bitsize;
10895 w->val_valid = true;
10896 }
10897
10898 if (cond_start)
10899 w->cond_string = savestring (cond_start, cond_end - cond_start);
10900 else
10901 w->cond_string = 0;
10902
10903 if (frame_id_p (watchpoint_frame))
10904 {
10905 w->watchpoint_frame = watchpoint_frame;
10906 w->watchpoint_thread = inferior_ptid;
10907 }
10908 else
10909 {
10910 w->watchpoint_frame = null_frame_id;
10911 w->watchpoint_thread = null_ptid;
10912 }
10913
10914 if (scope_breakpoint != NULL)
10915 {
10916 /* The scope breakpoint is related to the watchpoint. We will
10917 need to act on them together. */
10918 w->related_breakpoint = scope_breakpoint;
10919 scope_breakpoint->related_breakpoint = w.get ();
10920 }
10921
10922 if (!just_location)
10923 value_free_to_mark (mark);
10924
10925 /* Finally update the new watchpoint. This creates the locations
10926 that should be inserted. */
10927 update_watchpoint (w.get (), 1);
10928
10929 install_breakpoint (internal, std::move (w), 1);
10930 }
10931
10932 /* Return count of debug registers needed to watch the given expression.
10933 If the watchpoint cannot be handled in hardware return zero. */
10934
10935 static int
10936 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10937 {
10938 int found_memory_cnt = 0;
10939
10940 /* Did the user specifically forbid us to use hardware watchpoints? */
10941 if (!can_use_hw_watchpoints)
10942 return 0;
10943
10944 gdb_assert (!vals.empty ());
10945 struct value *head = vals[0].get ();
10946
10947 /* Make sure that the value of the expression depends only upon
10948 memory contents, and values computed from them within GDB. If we
10949 find any register references or function calls, we can't use a
10950 hardware watchpoint.
10951
10952 The idea here is that evaluating an expression generates a series
10953 of values, one holding the value of every subexpression. (The
10954 expression a*b+c has five subexpressions: a, b, a*b, c, and
10955 a*b+c.) GDB's values hold almost enough information to establish
10956 the criteria given above --- they identify memory lvalues,
10957 register lvalues, computed values, etcetera. So we can evaluate
10958 the expression, and then scan the chain of values that leaves
10959 behind to decide whether we can detect any possible change to the
10960 expression's final value using only hardware watchpoints.
10961
10962 However, I don't think that the values returned by inferior
10963 function calls are special in any way. So this function may not
10964 notice that an expression involving an inferior function call
10965 can't be watched with hardware watchpoints. FIXME. */
10966 for (const value_ref_ptr &iter : vals)
10967 {
10968 struct value *v = iter.get ();
10969
10970 if (VALUE_LVAL (v) == lval_memory)
10971 {
10972 if (v != head && value_lazy (v))
10973 /* A lazy memory lvalue in the chain is one that GDB never
10974 needed to fetch; we either just used its address (e.g.,
10975 `a' in `a.b') or we never needed it at all (e.g., `a'
10976 in `a,b'). This doesn't apply to HEAD; if that is
10977 lazy then it was not readable, but watch it anyway. */
10978 ;
10979 else
10980 {
10981 /* Ahh, memory we actually used! Check if we can cover
10982 it with hardware watchpoints. */
10983 struct type *vtype = check_typedef (value_type (v));
10984
10985 /* We only watch structs and arrays if user asked for it
10986 explicitly, never if they just happen to appear in a
10987 middle of some value chain. */
10988 if (v == head
10989 || (vtype->code () != TYPE_CODE_STRUCT
10990 && vtype->code () != TYPE_CODE_ARRAY))
10991 {
10992 CORE_ADDR vaddr = value_address (v);
10993 int len;
10994 int num_regs;
10995
10996 len = (target_exact_watchpoints
10997 && is_scalar_type_recursive (vtype))?
10998 1 : TYPE_LENGTH (value_type (v));
10999
11000 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11001 if (!num_regs)
11002 return 0;
11003 else
11004 found_memory_cnt += num_regs;
11005 }
11006 }
11007 }
11008 else if (VALUE_LVAL (v) != not_lval
11009 && deprecated_value_modifiable (v) == 0)
11010 return 0; /* These are values from the history (e.g., $1). */
11011 else if (VALUE_LVAL (v) == lval_register)
11012 return 0; /* Cannot watch a register with a HW watchpoint. */
11013 }
11014
11015 /* The expression itself looks suitable for using a hardware
11016 watchpoint, but give the target machine a chance to reject it. */
11017 return found_memory_cnt;
11018 }
11019
11020 void
11021 watch_command_wrapper (const char *arg, int from_tty, bool internal)
11022 {
11023 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11024 }
11025
11026 /* Options for the watch, awatch, and rwatch commands. */
11027
11028 struct watch_options
11029 {
11030 /* For -location. */
11031 bool location = false;
11032 };
11033
11034 /* Definitions of options for the "watch", "awatch", and "rwatch" commands.
11035
11036 Historically GDB always accepted both '-location' and '-l' flags for
11037 these commands (both flags being synonyms). When converting to the
11038 newer option scheme only '-location' is added here. That's fine (for
11039 backward compatibility) as any non-ambiguous prefix of a flag will be
11040 accepted, so '-l', '-loc', are now all accepted.
11041
11042 What this means is that, if in the future, we add any new flag here
11043 that starts with '-l' then this will break backward compatibility, so
11044 please, don't do that! */
11045
11046 static const gdb::option::option_def watch_option_defs[] = {
11047 gdb::option::flag_option_def<watch_options> {
11048 "location",
11049 [] (watch_options *opt) { return &opt->location; },
11050 N_("\
11051 This evaluates EXPRESSION and watches the memory to which is refers.\n\
11052 -l can be used as a short form of -location."),
11053 },
11054 };
11055
11056 /* Returns the option group used by 'watch', 'awatch', and 'rwatch'
11057 commands. */
11058
11059 static gdb::option::option_def_group
11060 make_watch_options_def_group (watch_options *opts)
11061 {
11062 return {{watch_option_defs}, opts};
11063 }
11064
11065 /* A helper function that looks for the "-location" argument and then
11066 calls watch_command_1. */
11067
11068 static void
11069 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
11070 {
11071 watch_options opts;
11072 auto grp = make_watch_options_def_group (&opts);
11073 gdb::option::process_options
11074 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
11075 if (arg != nullptr && *arg == '\0')
11076 arg = nullptr;
11077
11078 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
11079 }
11080
11081 /* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
11082 static void
11083 watch_command_completer (struct cmd_list_element *ignore,
11084 completion_tracker &tracker,
11085 const char *text, const char * /*word*/)
11086 {
11087 const auto group = make_watch_options_def_group (nullptr);
11088 if (gdb::option::complete_options
11089 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
11090 return;
11091
11092 const char *word = advance_to_expression_complete_word_point (tracker, text);
11093 expression_completer (ignore, tracker, text, word);
11094 }
11095
11096 static void
11097 watch_command (const char *arg, int from_tty)
11098 {
11099 watch_maybe_just_location (arg, hw_write, from_tty);
11100 }
11101
11102 void
11103 rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
11104 {
11105 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11106 }
11107
11108 static void
11109 rwatch_command (const char *arg, int from_tty)
11110 {
11111 watch_maybe_just_location (arg, hw_read, from_tty);
11112 }
11113
11114 void
11115 awatch_command_wrapper (const char *arg, int from_tty, bool internal)
11116 {
11117 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11118 }
11119
11120 static void
11121 awatch_command (const char *arg, int from_tty)
11122 {
11123 watch_maybe_just_location (arg, hw_access, from_tty);
11124 }
11125 \f
11126
11127 /* Data for the FSM that manages the until(location)/advance commands
11128 in infcmd.c. Here because it uses the mechanisms of
11129 breakpoints. */
11130
11131 struct until_break_fsm : public thread_fsm
11132 {
11133 /* The thread that was current when the command was executed. */
11134 int thread;
11135
11136 /* The breakpoint set at the return address in the caller frame,
11137 plus breakpoints at all the destination locations. */
11138 std::vector<breakpoint_up> breakpoints;
11139
11140 until_break_fsm (struct interp *cmd_interp, int thread,
11141 std::vector<breakpoint_up> &&breakpoints)
11142 : thread_fsm (cmd_interp),
11143 thread (thread),
11144 breakpoints (std::move (breakpoints))
11145 {
11146 }
11147
11148 void clean_up (struct thread_info *thread) override;
11149 bool should_stop (struct thread_info *thread) override;
11150 enum async_reply_reason do_async_reply_reason () override;
11151 };
11152
11153 /* Implementation of the 'should_stop' FSM method for the
11154 until(location)/advance commands. */
11155
11156 bool
11157 until_break_fsm::should_stop (struct thread_info *tp)
11158 {
11159 for (const breakpoint_up &bp : breakpoints)
11160 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11161 bp.get ()) != NULL)
11162 {
11163 set_finished ();
11164 break;
11165 }
11166
11167 return true;
11168 }
11169
11170 /* Implementation of the 'clean_up' FSM method for the
11171 until(location)/advance commands. */
11172
11173 void
11174 until_break_fsm::clean_up (struct thread_info *)
11175 {
11176 /* Clean up our temporary breakpoints. */
11177 breakpoints.clear ();
11178 delete_longjmp_breakpoint (thread);
11179 }
11180
11181 /* Implementation of the 'async_reply_reason' FSM method for the
11182 until(location)/advance commands. */
11183
11184 enum async_reply_reason
11185 until_break_fsm::do_async_reply_reason ()
11186 {
11187 return EXEC_ASYNC_LOCATION_REACHED;
11188 }
11189
11190 void
11191 until_break_command (const char *arg, int from_tty, int anywhere)
11192 {
11193 struct frame_info *frame;
11194 struct gdbarch *frame_gdbarch;
11195 struct frame_id stack_frame_id;
11196 struct frame_id caller_frame_id;
11197 int thread;
11198 struct thread_info *tp;
11199
11200 clear_proceed_status (0);
11201
11202 /* Set a breakpoint where the user wants it and at return from
11203 this function. */
11204
11205 event_location_up location = string_to_event_location (&arg, current_language);
11206
11207 std::vector<symtab_and_line> sals
11208 = (last_displayed_sal_is_valid ()
11209 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11210 get_last_displayed_symtab (),
11211 get_last_displayed_line ())
11212 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11213 NULL, NULL, 0));
11214
11215 if (sals.empty ())
11216 error (_("Couldn't get information on specified line."));
11217
11218 if (*arg)
11219 error (_("Junk at end of arguments."));
11220
11221 tp = inferior_thread ();
11222 thread = tp->global_num;
11223
11224 /* Note linespec handling above invalidates the frame chain.
11225 Installing a breakpoint also invalidates the frame chain (as it
11226 may need to switch threads), so do any frame handling before
11227 that. */
11228
11229 frame = get_selected_frame (NULL);
11230 frame_gdbarch = get_frame_arch (frame);
11231 stack_frame_id = get_stack_frame_id (frame);
11232 caller_frame_id = frame_unwind_caller_id (frame);
11233
11234 /* Keep within the current frame, or in frames called by the current
11235 one. */
11236
11237 std::vector<breakpoint_up> breakpoints;
11238
11239 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11240
11241 if (frame_id_p (caller_frame_id))
11242 {
11243 struct symtab_and_line sal2;
11244 struct gdbarch *caller_gdbarch;
11245
11246 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11247 sal2.pc = frame_unwind_caller_pc (frame);
11248 caller_gdbarch = frame_unwind_caller_arch (frame);
11249
11250 breakpoint_up caller_breakpoint
11251 = set_momentary_breakpoint (caller_gdbarch, sal2,
11252 caller_frame_id, bp_until);
11253 breakpoints.emplace_back (std::move (caller_breakpoint));
11254
11255 set_longjmp_breakpoint (tp, caller_frame_id);
11256 lj_deleter.emplace (thread);
11257 }
11258
11259 /* set_momentary_breakpoint could invalidate FRAME. */
11260 frame = NULL;
11261
11262 /* If the user told us to continue until a specified location, we
11263 don't specify a frame at which we need to stop. Otherwise,
11264 specify the selected frame, because we want to stop only at the
11265 very same frame. */
11266 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
11267
11268 for (symtab_and_line &sal : sals)
11269 {
11270 resolve_sal_pc (&sal);
11271
11272 breakpoint_up location_breakpoint
11273 = set_momentary_breakpoint (frame_gdbarch, sal,
11274 stop_frame_id, bp_until);
11275 breakpoints.emplace_back (std::move (location_breakpoint));
11276 }
11277
11278 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
11279 std::move (breakpoints));
11280
11281 if (lj_deleter)
11282 lj_deleter->release ();
11283
11284 proceed (-1, GDB_SIGNAL_DEFAULT);
11285 }
11286
11287 /* This function attempts to parse an optional "if <cond>" clause
11288 from the arg string. If one is not found, it returns NULL.
11289
11290 Else, it returns a pointer to the condition string. (It does not
11291 attempt to evaluate the string against a particular block.) And,
11292 it updates arg to point to the first character following the parsed
11293 if clause in the arg string. */
11294
11295 const char *
11296 ep_parse_optional_if_clause (const char **arg)
11297 {
11298 const char *cond_string;
11299
11300 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11301 return NULL;
11302
11303 /* Skip the "if" keyword. */
11304 (*arg) += 2;
11305
11306 /* Skip any extra leading whitespace, and record the start of the
11307 condition string. */
11308 *arg = skip_spaces (*arg);
11309 cond_string = *arg;
11310
11311 /* Assume that the condition occupies the remainder of the arg
11312 string. */
11313 (*arg) += strlen (cond_string);
11314
11315 return cond_string;
11316 }
11317
11318 /* Commands to deal with catching events, such as signals, exceptions,
11319 process start/exit, etc. */
11320
11321 typedef enum
11322 {
11323 catch_fork_temporary, catch_vfork_temporary,
11324 catch_fork_permanent, catch_vfork_permanent
11325 }
11326 catch_fork_kind;
11327
11328 static void
11329 catch_fork_command_1 (const char *arg, int from_tty,
11330 struct cmd_list_element *command)
11331 {
11332 struct gdbarch *gdbarch = get_current_arch ();
11333 const char *cond_string = NULL;
11334 catch_fork_kind fork_kind;
11335
11336 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11337 bool temp = (fork_kind == catch_fork_temporary
11338 || fork_kind == catch_vfork_temporary);
11339
11340 if (!arg)
11341 arg = "";
11342 arg = skip_spaces (arg);
11343
11344 /* The allowed syntax is:
11345 catch [v]fork
11346 catch [v]fork if <cond>
11347
11348 First, check if there's an if clause. */
11349 cond_string = ep_parse_optional_if_clause (&arg);
11350
11351 if ((*arg != '\0') && !isspace (*arg))
11352 error (_("Junk at end of arguments."));
11353
11354 /* If this target supports it, create a fork or vfork catchpoint
11355 and enable reporting of such events. */
11356 switch (fork_kind)
11357 {
11358 case catch_fork_temporary:
11359 case catch_fork_permanent:
11360 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
11361 &catch_fork_breakpoint_ops);
11362 break;
11363 case catch_vfork_temporary:
11364 case catch_vfork_permanent:
11365 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
11366 &catch_vfork_breakpoint_ops);
11367 break;
11368 default:
11369 error (_("unsupported or unknown fork kind; cannot catch it"));
11370 break;
11371 }
11372 }
11373
11374 static void
11375 catch_exec_command_1 (const char *arg, int from_tty,
11376 struct cmd_list_element *command)
11377 {
11378 struct gdbarch *gdbarch = get_current_arch ();
11379 const char *cond_string = NULL;
11380 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
11381
11382 if (!arg)
11383 arg = "";
11384 arg = skip_spaces (arg);
11385
11386 /* The allowed syntax is:
11387 catch exec
11388 catch exec if <cond>
11389
11390 First, check if there's an if clause. */
11391 cond_string = ep_parse_optional_if_clause (&arg);
11392
11393 if ((*arg != '\0') && !isspace (*arg))
11394 error (_("Junk at end of arguments."));
11395
11396 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11397 init_catchpoint (c.get (), gdbarch, temp, cond_string,
11398 &catch_exec_breakpoint_ops);
11399 c->exec_pathname = NULL;
11400
11401 install_breakpoint (0, std::move (c), 1);
11402 }
11403
11404 void
11405 init_ada_exception_breakpoint (struct breakpoint *b,
11406 struct gdbarch *gdbarch,
11407 struct symtab_and_line sal,
11408 const char *addr_string,
11409 const struct breakpoint_ops *ops,
11410 int tempflag,
11411 int enabled,
11412 int from_tty)
11413 {
11414 if (from_tty)
11415 {
11416 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11417 if (!loc_gdbarch)
11418 loc_gdbarch = gdbarch;
11419
11420 describe_other_breakpoints (loc_gdbarch,
11421 sal.pspace, sal.pc, sal.section, -1);
11422 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11423 version for exception catchpoints, because two catchpoints
11424 used for different exception names will use the same address.
11425 In this case, a "breakpoint ... also set at..." warning is
11426 unproductive. Besides, the warning phrasing is also a bit
11427 inappropriate, we should use the word catchpoint, and tell
11428 the user what type of catchpoint it is. The above is good
11429 enough for now, though. */
11430 }
11431
11432 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
11433
11434 b->enable_state = enabled ? bp_enabled : bp_disabled;
11435 b->disposition = tempflag ? disp_del : disp_donttouch;
11436 b->location = string_to_event_location (&addr_string,
11437 language_def (language_ada));
11438 b->language = language_ada;
11439 }
11440
11441 \f
11442
11443 /* Compare two breakpoints and return a strcmp-like result. */
11444
11445 static int
11446 compare_breakpoints (const breakpoint *a, const breakpoint *b)
11447 {
11448 uintptr_t ua = (uintptr_t) a;
11449 uintptr_t ub = (uintptr_t) b;
11450
11451 if (a->number < b->number)
11452 return -1;
11453 else if (a->number > b->number)
11454 return 1;
11455
11456 /* Now sort by address, in case we see, e..g, two breakpoints with
11457 the number 0. */
11458 if (ua < ub)
11459 return -1;
11460 return ua > ub ? 1 : 0;
11461 }
11462
11463 /* Delete breakpoints by address or line. */
11464
11465 static void
11466 clear_command (const char *arg, int from_tty)
11467 {
11468 int default_match;
11469
11470 std::vector<symtab_and_line> decoded_sals;
11471 symtab_and_line last_sal;
11472 gdb::array_view<symtab_and_line> sals;
11473 if (arg)
11474 {
11475 decoded_sals
11476 = decode_line_with_current_source (arg,
11477 (DECODE_LINE_FUNFIRSTLINE
11478 | DECODE_LINE_LIST_MODE));
11479 default_match = 0;
11480 sals = decoded_sals;
11481 }
11482 else
11483 {
11484 /* Set sal's line, symtab, pc, and pspace to the values
11485 corresponding to the last call to print_frame_info. If the
11486 codepoint is not valid, this will set all the fields to 0. */
11487 last_sal = get_last_displayed_sal ();
11488 if (last_sal.symtab == 0)
11489 error (_("No source file specified."));
11490
11491 default_match = 1;
11492 sals = last_sal;
11493 }
11494
11495 /* We don't call resolve_sal_pc here. That's not as bad as it
11496 seems, because all existing breakpoints typically have both
11497 file/line and pc set. So, if clear is given file/line, we can
11498 match this to existing breakpoint without obtaining pc at all.
11499
11500 We only support clearing given the address explicitly
11501 present in breakpoint table. Say, we've set breakpoint
11502 at file:line. There were several PC values for that file:line,
11503 due to optimization, all in one block.
11504
11505 We've picked one PC value. If "clear" is issued with another
11506 PC corresponding to the same file:line, the breakpoint won't
11507 be cleared. We probably can still clear the breakpoint, but
11508 since the other PC value is never presented to user, user
11509 can only find it by guessing, and it does not seem important
11510 to support that. */
11511
11512 /* For each line spec given, delete bps which correspond to it. Do
11513 it in two passes, solely to preserve the current behavior that
11514 from_tty is forced true if we delete more than one
11515 breakpoint. */
11516
11517 std::vector<struct breakpoint *> found;
11518 for (const auto &sal : sals)
11519 {
11520 const char *sal_fullname;
11521
11522 /* If exact pc given, clear bpts at that pc.
11523 If line given (pc == 0), clear all bpts on specified line.
11524 If defaulting, clear all bpts on default line
11525 or at default pc.
11526
11527 defaulting sal.pc != 0 tests to do
11528
11529 0 1 pc
11530 1 1 pc _and_ line
11531 0 0 line
11532 1 0 <can't happen> */
11533
11534 sal_fullname = (sal.symtab == NULL
11535 ? NULL : symtab_to_fullname (sal.symtab));
11536
11537 /* Find all matching breakpoints and add them to 'found'. */
11538 for (breakpoint *b : all_breakpoints ())
11539 {
11540 int match = 0;
11541 /* Are we going to delete b? */
11542 if (b->type != bp_none && !is_watchpoint (b))
11543 {
11544 struct bp_location *loc = b->loc;
11545 for (; loc; loc = loc->next)
11546 {
11547 /* If the user specified file:line, don't allow a PC
11548 match. This matches historical gdb behavior. */
11549 int pc_match = (!sal.explicit_line
11550 && sal.pc
11551 && (loc->pspace == sal.pspace)
11552 && (loc->address == sal.pc)
11553 && (!section_is_overlay (loc->section)
11554 || loc->section == sal.section));
11555 int line_match = 0;
11556
11557 if ((default_match || sal.explicit_line)
11558 && loc->symtab != NULL
11559 && sal_fullname != NULL
11560 && sal.pspace == loc->pspace
11561 && loc->line_number == sal.line
11562 && filename_cmp (symtab_to_fullname (loc->symtab),
11563 sal_fullname) == 0)
11564 line_match = 1;
11565
11566 if (pc_match || line_match)
11567 {
11568 match = 1;
11569 break;
11570 }
11571 }
11572 }
11573
11574 if (match)
11575 found.push_back (b);
11576 }
11577 }
11578
11579 /* Now go thru the 'found' chain and delete them. */
11580 if (found.empty ())
11581 {
11582 if (arg)
11583 error (_("No breakpoint at %s."), arg);
11584 else
11585 error (_("No breakpoint at this line."));
11586 }
11587
11588 /* Remove duplicates from the vec. */
11589 std::sort (found.begin (), found.end (),
11590 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11591 {
11592 return compare_breakpoints (bp_a, bp_b) < 0;
11593 });
11594 found.erase (std::unique (found.begin (), found.end (),
11595 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11596 {
11597 return compare_breakpoints (bp_a, bp_b) == 0;
11598 }),
11599 found.end ());
11600
11601 if (found.size () > 1)
11602 from_tty = 1; /* Always report if deleted more than one. */
11603 if (from_tty)
11604 {
11605 if (found.size () == 1)
11606 printf_unfiltered (_("Deleted breakpoint "));
11607 else
11608 printf_unfiltered (_("Deleted breakpoints "));
11609 }
11610
11611 for (breakpoint *iter : found)
11612 {
11613 if (from_tty)
11614 printf_unfiltered ("%d ", iter->number);
11615 delete_breakpoint (iter);
11616 }
11617 if (from_tty)
11618 putchar_unfiltered ('\n');
11619 }
11620 \f
11621 /* Delete breakpoint in BS if they are `delete' breakpoints and
11622 all breakpoints that are marked for deletion, whether hit or not.
11623 This is called after any breakpoint is hit, or after errors. */
11624
11625 void
11626 breakpoint_auto_delete (bpstat bs)
11627 {
11628 struct breakpoint *b, *b_tmp;
11629
11630 for (; bs; bs = bs->next)
11631 if (bs->breakpoint_at
11632 && bs->breakpoint_at->disposition == disp_del
11633 && bs->stop)
11634 delete_breakpoint (bs->breakpoint_at);
11635
11636 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11637 {
11638 if (b->disposition == disp_del_at_next_stop)
11639 delete_breakpoint (b);
11640 }
11641 }
11642
11643 /* A comparison function for bp_location AP and BP being interfaced to
11644 std::sort. Sort elements primarily by their ADDRESS (no matter what
11645 bl_address_is_meaningful says), secondarily by ordering first
11646 permanent elements and terciarily just ensuring the array is sorted
11647 stable way despite std::sort being an unstable algorithm. */
11648
11649 static int
11650 bp_location_is_less_than (const bp_location *a, const bp_location *b)
11651 {
11652 if (a->address != b->address)
11653 return a->address < b->address;
11654
11655 /* Sort locations at the same address by their pspace number, keeping
11656 locations of the same inferior (in a multi-inferior environment)
11657 grouped. */
11658
11659 if (a->pspace->num != b->pspace->num)
11660 return a->pspace->num < b->pspace->num;
11661
11662 /* Sort permanent breakpoints first. */
11663 if (a->permanent != b->permanent)
11664 return a->permanent > b->permanent;
11665
11666 /* Sort by type in order to make duplicate determination easier.
11667 See update_global_location_list. This is kept in sync with
11668 breakpoint_locations_match. */
11669 if (a->loc_type < b->loc_type)
11670 return true;
11671
11672 /* Likewise, for range-breakpoints, sort by length. */
11673 if (a->loc_type == bp_loc_hardware_breakpoint
11674 && b->loc_type == bp_loc_hardware_breakpoint
11675 && a->length < b->length)
11676 return true;
11677
11678 /* Make the internal GDB representation stable across GDB runs
11679 where A and B memory inside GDB can differ. Breakpoint locations of
11680 the same type at the same address can be sorted in arbitrary order. */
11681
11682 if (a->owner->number != b->owner->number)
11683 return a->owner->number < b->owner->number;
11684
11685 return a < b;
11686 }
11687
11688 /* Set bp_locations_placed_address_before_address_max and
11689 bp_locations_shadow_len_after_address_max according to the current
11690 content of the bp_locations array. */
11691
11692 static void
11693 bp_locations_target_extensions_update (void)
11694 {
11695 struct bp_location *bl, **blp_tmp;
11696
11697 bp_locations_placed_address_before_address_max = 0;
11698 bp_locations_shadow_len_after_address_max = 0;
11699
11700 ALL_BP_LOCATIONS (bl, blp_tmp)
11701 {
11702 CORE_ADDR start, end, addr;
11703
11704 if (!bp_location_has_shadow (bl))
11705 continue;
11706
11707 start = bl->target_info.placed_address;
11708 end = start + bl->target_info.shadow_len;
11709
11710 gdb_assert (bl->address >= start);
11711 addr = bl->address - start;
11712 if (addr > bp_locations_placed_address_before_address_max)
11713 bp_locations_placed_address_before_address_max = addr;
11714
11715 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11716
11717 gdb_assert (bl->address < end);
11718 addr = end - bl->address;
11719 if (addr > bp_locations_shadow_len_after_address_max)
11720 bp_locations_shadow_len_after_address_max = addr;
11721 }
11722 }
11723
11724 /* Download tracepoint locations if they haven't been. */
11725
11726 static void
11727 download_tracepoint_locations (void)
11728 {
11729 struct breakpoint *b;
11730 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11731
11732 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11733
11734 ALL_TRACEPOINTS (b)
11735 {
11736 struct bp_location *bl;
11737 struct tracepoint *t;
11738 int bp_location_downloaded = 0;
11739
11740 if ((b->type == bp_fast_tracepoint
11741 ? !may_insert_fast_tracepoints
11742 : !may_insert_tracepoints))
11743 continue;
11744
11745 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11746 {
11747 if (target_can_download_tracepoint ())
11748 can_download_tracepoint = TRIBOOL_TRUE;
11749 else
11750 can_download_tracepoint = TRIBOOL_FALSE;
11751 }
11752
11753 if (can_download_tracepoint == TRIBOOL_FALSE)
11754 break;
11755
11756 for (bl = b->loc; bl; bl = bl->next)
11757 {
11758 /* In tracepoint, locations are _never_ duplicated, so
11759 should_be_inserted is equivalent to
11760 unduplicated_should_be_inserted. */
11761 if (!should_be_inserted (bl) || bl->inserted)
11762 continue;
11763
11764 switch_to_program_space_and_thread (bl->pspace);
11765
11766 target_download_tracepoint (bl);
11767
11768 bl->inserted = 1;
11769 bp_location_downloaded = 1;
11770 }
11771 t = (struct tracepoint *) b;
11772 t->number_on_target = b->number;
11773 if (bp_location_downloaded)
11774 gdb::observers::breakpoint_modified.notify (b);
11775 }
11776 }
11777
11778 /* Swap the insertion/duplication state between two locations. */
11779
11780 static void
11781 swap_insertion (struct bp_location *left, struct bp_location *right)
11782 {
11783 const int left_inserted = left->inserted;
11784 const int left_duplicate = left->duplicate;
11785 const int left_needs_update = left->needs_update;
11786 const struct bp_target_info left_target_info = left->target_info;
11787
11788 /* Locations of tracepoints can never be duplicated. */
11789 if (is_tracepoint (left->owner))
11790 gdb_assert (!left->duplicate);
11791 if (is_tracepoint (right->owner))
11792 gdb_assert (!right->duplicate);
11793
11794 left->inserted = right->inserted;
11795 left->duplicate = right->duplicate;
11796 left->needs_update = right->needs_update;
11797 left->target_info = right->target_info;
11798 right->inserted = left_inserted;
11799 right->duplicate = left_duplicate;
11800 right->needs_update = left_needs_update;
11801 right->target_info = left_target_info;
11802 }
11803
11804 /* Force the re-insertion of the locations at ADDRESS. This is called
11805 once a new/deleted/modified duplicate location is found and we are evaluating
11806 conditions on the target's side. Such conditions need to be updated on
11807 the target. */
11808
11809 static void
11810 force_breakpoint_reinsertion (struct bp_location *bl)
11811 {
11812 struct bp_location **locp = NULL, **loc2p;
11813 struct bp_location *loc;
11814 CORE_ADDR address = 0;
11815 int pspace_num;
11816
11817 address = bl->address;
11818 pspace_num = bl->pspace->num;
11819
11820 /* This is only meaningful if the target is
11821 evaluating conditions and if the user has
11822 opted for condition evaluation on the target's
11823 side. */
11824 if (gdb_evaluates_breakpoint_condition_p ()
11825 || !target_supports_evaluation_of_breakpoint_conditions ())
11826 return;
11827
11828 /* Flag all breakpoint locations with this address and
11829 the same program space as the location
11830 as "its condition has changed". We need to
11831 update the conditions on the target's side. */
11832 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11833 {
11834 loc = *loc2p;
11835
11836 if (!is_breakpoint (loc->owner)
11837 || pspace_num != loc->pspace->num)
11838 continue;
11839
11840 /* Flag the location appropriately. We use a different state to
11841 let everyone know that we already updated the set of locations
11842 with addr bl->address and program space bl->pspace. This is so
11843 we don't have to keep calling these functions just to mark locations
11844 that have already been marked. */
11845 loc->condition_changed = condition_updated;
11846
11847 /* Free the agent expression bytecode as well. We will compute
11848 it later on. */
11849 loc->cond_bytecode.reset ();
11850 }
11851 }
11852
11853 /* Called whether new breakpoints are created, or existing breakpoints
11854 deleted, to update the global location list and recompute which
11855 locations are duplicate of which.
11856
11857 The INSERT_MODE flag determines whether locations may not, may, or
11858 shall be inserted now. See 'enum ugll_insert_mode' for more
11859 info. */
11860
11861 static void
11862 update_global_location_list (enum ugll_insert_mode insert_mode)
11863 {
11864 struct bp_location **locp, *loc;
11865 /* Last breakpoint location address that was marked for update. */
11866 CORE_ADDR last_addr = 0;
11867 /* Last breakpoint location program space that was marked for update. */
11868 int last_pspace_num = -1;
11869
11870 /* Used in the duplicates detection below. When iterating over all
11871 bp_locations, points to the first bp_location of a given address.
11872 Breakpoints and watchpoints of different types are never
11873 duplicates of each other. Keep one pointer for each type of
11874 breakpoint/watchpoint, so we only need to loop over all locations
11875 once. */
11876 struct bp_location *bp_loc_first; /* breakpoint */
11877 struct bp_location *wp_loc_first; /* hardware watchpoint */
11878 struct bp_location *awp_loc_first; /* access watchpoint */
11879 struct bp_location *rwp_loc_first; /* read watchpoint */
11880
11881 /* Saved former bp_locations array which we compare against the newly
11882 built bp_locations from the current state of ALL_BREAKPOINTS. */
11883 struct bp_location **old_locp;
11884 unsigned old_locations_count;
11885 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
11886
11887 old_locations_count = bp_locations_count;
11888 bp_locations = NULL;
11889 bp_locations_count = 0;
11890
11891 for (breakpoint *b : all_breakpoints ())
11892 for (loc = b->loc; loc; loc = loc->next)
11893 bp_locations_count++;
11894
11895 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11896 locp = bp_locations;
11897 for (breakpoint *b : all_breakpoints ())
11898 for (loc = b->loc; loc; loc = loc->next)
11899 *locp++ = loc;
11900
11901 /* See if we need to "upgrade" a software breakpoint to a hardware
11902 breakpoint. Do this before deciding whether locations are
11903 duplicates. Also do this before sorting because sorting order
11904 depends on location type. */
11905 for (locp = bp_locations;
11906 locp < bp_locations + bp_locations_count;
11907 locp++)
11908 {
11909 loc = *locp;
11910 if (!loc->inserted && should_be_inserted (loc))
11911 handle_automatic_hardware_breakpoints (loc);
11912 }
11913
11914 std::sort (bp_locations, bp_locations + bp_locations_count,
11915 bp_location_is_less_than);
11916
11917 bp_locations_target_extensions_update ();
11918
11919 /* Identify bp_location instances that are no longer present in the
11920 new list, and therefore should be freed. Note that it's not
11921 necessary that those locations should be removed from inferior --
11922 if there's another location at the same address (previously
11923 marked as duplicate), we don't need to remove/insert the
11924 location.
11925
11926 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11927 and former bp_location array state respectively. */
11928
11929 locp = bp_locations;
11930 for (old_locp = old_locations.get ();
11931 old_locp < old_locations.get () + old_locations_count;
11932 old_locp++)
11933 {
11934 struct bp_location *old_loc = *old_locp;
11935 struct bp_location **loc2p;
11936
11937 /* Tells if 'old_loc' is found among the new locations. If
11938 not, we have to free it. */
11939 int found_object = 0;
11940 /* Tells if the location should remain inserted in the target. */
11941 int keep_in_target = 0;
11942 int removed = 0;
11943
11944 /* Skip LOCP entries which will definitely never be needed.
11945 Stop either at or being the one matching OLD_LOC. */
11946 while (locp < bp_locations + bp_locations_count
11947 && (*locp)->address < old_loc->address)
11948 locp++;
11949
11950 for (loc2p = locp;
11951 (loc2p < bp_locations + bp_locations_count
11952 && (*loc2p)->address == old_loc->address);
11953 loc2p++)
11954 {
11955 /* Check if this is a new/duplicated location or a duplicated
11956 location that had its condition modified. If so, we want to send
11957 its condition to the target if evaluation of conditions is taking
11958 place there. */
11959 if ((*loc2p)->condition_changed == condition_modified
11960 && (last_addr != old_loc->address
11961 || last_pspace_num != old_loc->pspace->num))
11962 {
11963 force_breakpoint_reinsertion (*loc2p);
11964 last_pspace_num = old_loc->pspace->num;
11965 }
11966
11967 if (*loc2p == old_loc)
11968 found_object = 1;
11969 }
11970
11971 /* We have already handled this address, update it so that we don't
11972 have to go through updates again. */
11973 last_addr = old_loc->address;
11974
11975 /* Target-side condition evaluation: Handle deleted locations. */
11976 if (!found_object)
11977 force_breakpoint_reinsertion (old_loc);
11978
11979 /* If this location is no longer present, and inserted, look if
11980 there's maybe a new location at the same address. If so,
11981 mark that one inserted, and don't remove this one. This is
11982 needed so that we don't have a time window where a breakpoint
11983 at certain location is not inserted. */
11984
11985 if (old_loc->inserted)
11986 {
11987 /* If the location is inserted now, we might have to remove
11988 it. */
11989
11990 if (found_object && should_be_inserted (old_loc))
11991 {
11992 /* The location is still present in the location list,
11993 and still should be inserted. Don't do anything. */
11994 keep_in_target = 1;
11995 }
11996 else
11997 {
11998 /* This location still exists, but it won't be kept in the
11999 target since it may have been disabled. We proceed to
12000 remove its target-side condition. */
12001
12002 /* The location is either no longer present, or got
12003 disabled. See if there's another location at the
12004 same address, in which case we don't need to remove
12005 this one from the target. */
12006
12007 /* OLD_LOC comes from existing struct breakpoint. */
12008 if (bl_address_is_meaningful (old_loc))
12009 {
12010 for (loc2p = locp;
12011 (loc2p < bp_locations + bp_locations_count
12012 && (*loc2p)->address == old_loc->address);
12013 loc2p++)
12014 {
12015 struct bp_location *loc2 = *loc2p;
12016
12017 if (loc2 == old_loc)
12018 continue;
12019
12020 if (breakpoint_locations_match (loc2, old_loc))
12021 {
12022 /* Read watchpoint locations are switched to
12023 access watchpoints, if the former are not
12024 supported, but the latter are. */
12025 if (is_hardware_watchpoint (old_loc->owner))
12026 {
12027 gdb_assert (is_hardware_watchpoint (loc2->owner));
12028 loc2->watchpoint_type = old_loc->watchpoint_type;
12029 }
12030
12031 /* loc2 is a duplicated location. We need to check
12032 if it should be inserted in case it will be
12033 unduplicated. */
12034 if (unduplicated_should_be_inserted (loc2))
12035 {
12036 swap_insertion (old_loc, loc2);
12037 keep_in_target = 1;
12038 break;
12039 }
12040 }
12041 }
12042 }
12043 }
12044
12045 if (!keep_in_target)
12046 {
12047 if (remove_breakpoint (old_loc))
12048 {
12049 /* This is just about all we can do. We could keep
12050 this location on the global list, and try to
12051 remove it next time, but there's no particular
12052 reason why we will succeed next time.
12053
12054 Note that at this point, old_loc->owner is still
12055 valid, as delete_breakpoint frees the breakpoint
12056 only after calling us. */
12057 printf_filtered (_("warning: Error removing "
12058 "breakpoint %d\n"),
12059 old_loc->owner->number);
12060 }
12061 removed = 1;
12062 }
12063 }
12064
12065 if (!found_object)
12066 {
12067 if (removed && target_is_non_stop_p ()
12068 && need_moribund_for_location_type (old_loc))
12069 {
12070 /* This location was removed from the target. In
12071 non-stop mode, a race condition is possible where
12072 we've removed a breakpoint, but stop events for that
12073 breakpoint are already queued and will arrive later.
12074 We apply an heuristic to be able to distinguish such
12075 SIGTRAPs from other random SIGTRAPs: we keep this
12076 breakpoint location for a bit, and will retire it
12077 after we see some number of events. The theory here
12078 is that reporting of events should, "on the average",
12079 be fair, so after a while we'll see events from all
12080 threads that have anything of interest, and no longer
12081 need to keep this breakpoint location around. We
12082 don't hold locations forever so to reduce chances of
12083 mistaking a non-breakpoint SIGTRAP for a breakpoint
12084 SIGTRAP.
12085
12086 The heuristic failing can be disastrous on
12087 decr_pc_after_break targets.
12088
12089 On decr_pc_after_break targets, like e.g., x86-linux,
12090 if we fail to recognize a late breakpoint SIGTRAP,
12091 because events_till_retirement has reached 0 too
12092 soon, we'll fail to do the PC adjustment, and report
12093 a random SIGTRAP to the user. When the user resumes
12094 the inferior, it will most likely immediately crash
12095 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12096 corrupted, because of being resumed e.g., in the
12097 middle of a multi-byte instruction, or skipped a
12098 one-byte instruction. This was actually seen happen
12099 on native x86-linux, and should be less rare on
12100 targets that do not support new thread events, like
12101 remote, due to the heuristic depending on
12102 thread_count.
12103
12104 Mistaking a random SIGTRAP for a breakpoint trap
12105 causes similar symptoms (PC adjustment applied when
12106 it shouldn't), but then again, playing with SIGTRAPs
12107 behind the debugger's back is asking for trouble.
12108
12109 Since hardware watchpoint traps are always
12110 distinguishable from other traps, so we don't need to
12111 apply keep hardware watchpoint moribund locations
12112 around. We simply always ignore hardware watchpoint
12113 traps we can no longer explain. */
12114
12115 process_stratum_target *proc_target = nullptr;
12116 for (inferior *inf : all_inferiors ())
12117 if (inf->pspace == old_loc->pspace)
12118 {
12119 proc_target = inf->process_target ();
12120 break;
12121 }
12122 if (proc_target != nullptr)
12123 old_loc->events_till_retirement
12124 = 3 * (thread_count (proc_target) + 1);
12125 else
12126 old_loc->events_till_retirement = 1;
12127 old_loc->owner = NULL;
12128
12129 moribund_locations.push_back (old_loc);
12130 }
12131 else
12132 {
12133 old_loc->owner = NULL;
12134 decref_bp_location (&old_loc);
12135 }
12136 }
12137 }
12138
12139 /* Rescan breakpoints at the same address and section, marking the
12140 first one as "first" and any others as "duplicates". This is so
12141 that the bpt instruction is only inserted once. If we have a
12142 permanent breakpoint at the same place as BPT, make that one the
12143 official one, and the rest as duplicates. Permanent breakpoints
12144 are sorted first for the same address.
12145
12146 Do the same for hardware watchpoints, but also considering the
12147 watchpoint's type (regular/access/read) and length. */
12148
12149 bp_loc_first = NULL;
12150 wp_loc_first = NULL;
12151 awp_loc_first = NULL;
12152 rwp_loc_first = NULL;
12153 ALL_BP_LOCATIONS (loc, locp)
12154 {
12155 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12156 non-NULL. */
12157 struct bp_location **loc_first_p;
12158 breakpoint *b = loc->owner;
12159
12160 if (!unduplicated_should_be_inserted (loc)
12161 || !bl_address_is_meaningful (loc)
12162 /* Don't detect duplicate for tracepoint locations because they are
12163 never duplicated. See the comments in field `duplicate' of
12164 `struct bp_location'. */
12165 || is_tracepoint (b))
12166 {
12167 /* Clear the condition modification flag. */
12168 loc->condition_changed = condition_unchanged;
12169 continue;
12170 }
12171
12172 if (b->type == bp_hardware_watchpoint)
12173 loc_first_p = &wp_loc_first;
12174 else if (b->type == bp_read_watchpoint)
12175 loc_first_p = &rwp_loc_first;
12176 else if (b->type == bp_access_watchpoint)
12177 loc_first_p = &awp_loc_first;
12178 else
12179 loc_first_p = &bp_loc_first;
12180
12181 if (*loc_first_p == NULL
12182 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12183 || !breakpoint_locations_match (loc, *loc_first_p))
12184 {
12185 *loc_first_p = loc;
12186 loc->duplicate = 0;
12187
12188 if (is_breakpoint (loc->owner) && loc->condition_changed)
12189 {
12190 loc->needs_update = 1;
12191 /* Clear the condition modification flag. */
12192 loc->condition_changed = condition_unchanged;
12193 }
12194 continue;
12195 }
12196
12197
12198 /* This and the above ensure the invariant that the first location
12199 is not duplicated, and is the inserted one.
12200 All following are marked as duplicated, and are not inserted. */
12201 if (loc->inserted)
12202 swap_insertion (loc, *loc_first_p);
12203 loc->duplicate = 1;
12204
12205 /* Clear the condition modification flag. */
12206 loc->condition_changed = condition_unchanged;
12207 }
12208
12209 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12210 {
12211 if (insert_mode != UGLL_DONT_INSERT)
12212 insert_breakpoint_locations ();
12213 else
12214 {
12215 /* Even though the caller told us to not insert new
12216 locations, we may still need to update conditions on the
12217 target's side of breakpoints that were already inserted
12218 if the target is evaluating breakpoint conditions. We
12219 only update conditions for locations that are marked
12220 "needs_update". */
12221 update_inserted_breakpoint_locations ();
12222 }
12223 }
12224
12225 if (insert_mode != UGLL_DONT_INSERT)
12226 download_tracepoint_locations ();
12227 }
12228
12229 void
12230 breakpoint_retire_moribund (void)
12231 {
12232 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12233 {
12234 struct bp_location *loc = moribund_locations[ix];
12235 if (--(loc->events_till_retirement) == 0)
12236 {
12237 decref_bp_location (&loc);
12238 unordered_remove (moribund_locations, ix);
12239 --ix;
12240 }
12241 }
12242 }
12243
12244 static void
12245 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12246 {
12247
12248 try
12249 {
12250 update_global_location_list (insert_mode);
12251 }
12252 catch (const gdb_exception_error &e)
12253 {
12254 }
12255 }
12256
12257 /* Clear BKP from a BPS. */
12258
12259 static void
12260 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12261 {
12262 bpstat bs;
12263
12264 for (bs = bps; bs; bs = bs->next)
12265 if (bs->breakpoint_at == bpt)
12266 {
12267 bs->breakpoint_at = NULL;
12268 bs->old_val = NULL;
12269 /* bs->commands will be freed later. */
12270 }
12271 }
12272
12273 /* Callback for iterate_over_threads. */
12274 static int
12275 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12276 {
12277 struct breakpoint *bpt = (struct breakpoint *) data;
12278
12279 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12280 return 0;
12281 }
12282
12283 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12284 callbacks. */
12285
12286 static void
12287 say_where (struct breakpoint *b)
12288 {
12289 struct value_print_options opts;
12290
12291 get_user_print_options (&opts);
12292
12293 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12294 single string. */
12295 if (b->loc == NULL)
12296 {
12297 /* For pending locations, the output differs slightly based
12298 on b->extra_string. If this is non-NULL, it contains either
12299 a condition or dprintf arguments. */
12300 if (b->extra_string == NULL)
12301 {
12302 printf_filtered (_(" (%s) pending."),
12303 event_location_to_string (b->location.get ()));
12304 }
12305 else if (b->type == bp_dprintf)
12306 {
12307 printf_filtered (_(" (%s,%s) pending."),
12308 event_location_to_string (b->location.get ()),
12309 b->extra_string);
12310 }
12311 else
12312 {
12313 printf_filtered (_(" (%s %s) pending."),
12314 event_location_to_string (b->location.get ()),
12315 b->extra_string);
12316 }
12317 }
12318 else
12319 {
12320 if (opts.addressprint || b->loc->symtab == NULL)
12321 printf_filtered (" at %ps",
12322 styled_string (address_style.style (),
12323 paddress (b->loc->gdbarch,
12324 b->loc->address)));
12325 if (b->loc->symtab != NULL)
12326 {
12327 /* If there is a single location, we can print the location
12328 more nicely. */
12329 if (b->loc->next == NULL)
12330 {
12331 const char *filename
12332 = symtab_to_filename_for_display (b->loc->symtab);
12333 printf_filtered (": file %ps, line %d.",
12334 styled_string (file_name_style.style (),
12335 filename),
12336 b->loc->line_number);
12337 }
12338 else
12339 /* This is not ideal, but each location may have a
12340 different file name, and this at least reflects the
12341 real situation somewhat. */
12342 printf_filtered (": %s.",
12343 event_location_to_string (b->location.get ()));
12344 }
12345
12346 if (b->loc->next)
12347 {
12348 struct bp_location *loc = b->loc;
12349 int n = 0;
12350 for (; loc; loc = loc->next)
12351 ++n;
12352 printf_filtered (" (%d locations)", n);
12353 }
12354 }
12355 }
12356
12357 bp_location::~bp_location ()
12358 {
12359 xfree (function_name);
12360 }
12361
12362 /* Destructor for the breakpoint base class. */
12363
12364 breakpoint::~breakpoint ()
12365 {
12366 xfree (this->cond_string);
12367 xfree (this->extra_string);
12368 }
12369
12370 static struct bp_location *
12371 base_breakpoint_allocate_location (struct breakpoint *self)
12372 {
12373 return new bp_location (self);
12374 }
12375
12376 static void
12377 base_breakpoint_re_set (struct breakpoint *b)
12378 {
12379 /* Nothing to re-set. */
12380 }
12381
12382 #define internal_error_pure_virtual_called() \
12383 gdb_assert_not_reached ("pure virtual function called")
12384
12385 static int
12386 base_breakpoint_insert_location (struct bp_location *bl)
12387 {
12388 internal_error_pure_virtual_called ();
12389 }
12390
12391 static int
12392 base_breakpoint_remove_location (struct bp_location *bl,
12393 enum remove_bp_reason reason)
12394 {
12395 internal_error_pure_virtual_called ();
12396 }
12397
12398 static int
12399 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12400 const address_space *aspace,
12401 CORE_ADDR bp_addr,
12402 const struct target_waitstatus *ws)
12403 {
12404 internal_error_pure_virtual_called ();
12405 }
12406
12407 static void
12408 base_breakpoint_check_status (bpstat bs)
12409 {
12410 /* Always stop. */
12411 }
12412
12413 /* A "works_in_software_mode" breakpoint_ops method that just internal
12414 errors. */
12415
12416 static int
12417 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12418 {
12419 internal_error_pure_virtual_called ();
12420 }
12421
12422 /* A "resources_needed" breakpoint_ops method that just internal
12423 errors. */
12424
12425 static int
12426 base_breakpoint_resources_needed (const struct bp_location *bl)
12427 {
12428 internal_error_pure_virtual_called ();
12429 }
12430
12431 static enum print_stop_action
12432 base_breakpoint_print_it (bpstat bs)
12433 {
12434 internal_error_pure_virtual_called ();
12435 }
12436
12437 static void
12438 base_breakpoint_print_one_detail (const struct breakpoint *self,
12439 struct ui_out *uiout)
12440 {
12441 /* nothing */
12442 }
12443
12444 static void
12445 base_breakpoint_print_mention (struct breakpoint *b)
12446 {
12447 internal_error_pure_virtual_called ();
12448 }
12449
12450 static void
12451 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12452 {
12453 internal_error_pure_virtual_called ();
12454 }
12455
12456 static void
12457 base_breakpoint_create_sals_from_location
12458 (struct event_location *location,
12459 struct linespec_result *canonical,
12460 enum bptype type_wanted)
12461 {
12462 internal_error_pure_virtual_called ();
12463 }
12464
12465 static void
12466 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12467 struct linespec_result *c,
12468 gdb::unique_xmalloc_ptr<char> cond_string,
12469 gdb::unique_xmalloc_ptr<char> extra_string,
12470 enum bptype type_wanted,
12471 enum bpdisp disposition,
12472 int thread,
12473 int task, int ignore_count,
12474 const struct breakpoint_ops *o,
12475 int from_tty, int enabled,
12476 int internal, unsigned flags)
12477 {
12478 internal_error_pure_virtual_called ();
12479 }
12480
12481 static std::vector<symtab_and_line>
12482 base_breakpoint_decode_location (struct breakpoint *b,
12483 struct event_location *location,
12484 struct program_space *search_pspace)
12485 {
12486 internal_error_pure_virtual_called ();
12487 }
12488
12489 /* The default 'explains_signal' method. */
12490
12491 static int
12492 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12493 {
12494 return 1;
12495 }
12496
12497 /* The default "after_condition_true" method. */
12498
12499 static void
12500 base_breakpoint_after_condition_true (struct bpstats *bs)
12501 {
12502 /* Nothing to do. */
12503 }
12504
12505 struct breakpoint_ops base_breakpoint_ops =
12506 {
12507 base_breakpoint_allocate_location,
12508 base_breakpoint_re_set,
12509 base_breakpoint_insert_location,
12510 base_breakpoint_remove_location,
12511 base_breakpoint_breakpoint_hit,
12512 base_breakpoint_check_status,
12513 base_breakpoint_resources_needed,
12514 base_breakpoint_works_in_software_mode,
12515 base_breakpoint_print_it,
12516 NULL,
12517 base_breakpoint_print_one_detail,
12518 base_breakpoint_print_mention,
12519 base_breakpoint_print_recreate,
12520 base_breakpoint_create_sals_from_location,
12521 base_breakpoint_create_breakpoints_sal,
12522 base_breakpoint_decode_location,
12523 base_breakpoint_explains_signal,
12524 base_breakpoint_after_condition_true,
12525 };
12526
12527 /* Default breakpoint_ops methods. */
12528
12529 static void
12530 bkpt_re_set (struct breakpoint *b)
12531 {
12532 /* FIXME: is this still reachable? */
12533 if (breakpoint_event_location_empty_p (b))
12534 {
12535 /* Anything without a location can't be re-set. */
12536 delete_breakpoint (b);
12537 return;
12538 }
12539
12540 breakpoint_re_set_default (b);
12541 }
12542
12543 static int
12544 bkpt_insert_location (struct bp_location *bl)
12545 {
12546 CORE_ADDR addr = bl->target_info.reqstd_address;
12547
12548 bl->target_info.kind = breakpoint_kind (bl, &addr);
12549 bl->target_info.placed_address = addr;
12550
12551 if (bl->loc_type == bp_loc_hardware_breakpoint)
12552 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12553 else
12554 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12555 }
12556
12557 static int
12558 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12559 {
12560 if (bl->loc_type == bp_loc_hardware_breakpoint)
12561 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12562 else
12563 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12564 }
12565
12566 static int
12567 bkpt_breakpoint_hit (const struct bp_location *bl,
12568 const address_space *aspace, CORE_ADDR bp_addr,
12569 const struct target_waitstatus *ws)
12570 {
12571 if (ws->kind != TARGET_WAITKIND_STOPPED
12572 || ws->value.sig != GDB_SIGNAL_TRAP)
12573 return 0;
12574
12575 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12576 aspace, bp_addr))
12577 return 0;
12578
12579 if (overlay_debugging /* unmapped overlay section */
12580 && section_is_overlay (bl->section)
12581 && !section_is_mapped (bl->section))
12582 return 0;
12583
12584 return 1;
12585 }
12586
12587 static int
12588 dprintf_breakpoint_hit (const struct bp_location *bl,
12589 const address_space *aspace, CORE_ADDR bp_addr,
12590 const struct target_waitstatus *ws)
12591 {
12592 if (dprintf_style == dprintf_style_agent
12593 && target_can_run_breakpoint_commands ())
12594 {
12595 /* An agent-style dprintf never causes a stop. If we see a trap
12596 for this address it must be for a breakpoint that happens to
12597 be set at the same address. */
12598 return 0;
12599 }
12600
12601 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12602 }
12603
12604 static int
12605 bkpt_resources_needed (const struct bp_location *bl)
12606 {
12607 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12608
12609 return 1;
12610 }
12611
12612 static enum print_stop_action
12613 bkpt_print_it (bpstat bs)
12614 {
12615 struct breakpoint *b;
12616 const struct bp_location *bl;
12617 int bp_temp;
12618 struct ui_out *uiout = current_uiout;
12619
12620 gdb_assert (bs->bp_location_at != NULL);
12621
12622 bl = bs->bp_location_at.get ();
12623 b = bs->breakpoint_at;
12624
12625 bp_temp = b->disposition == disp_del;
12626 if (bl->address != bl->requested_address)
12627 breakpoint_adjustment_warning (bl->requested_address,
12628 bl->address,
12629 b->number, 1);
12630 annotate_breakpoint (b->number);
12631 maybe_print_thread_hit_breakpoint (uiout);
12632
12633 if (uiout->is_mi_like_p ())
12634 {
12635 uiout->field_string ("reason",
12636 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12637 uiout->field_string ("disp", bpdisp_text (b->disposition));
12638 }
12639 if (bp_temp)
12640 uiout->message ("Temporary breakpoint %pF, ",
12641 signed_field ("bkptno", b->number));
12642 else
12643 uiout->message ("Breakpoint %pF, ",
12644 signed_field ("bkptno", b->number));
12645
12646 return PRINT_SRC_AND_LOC;
12647 }
12648
12649 static void
12650 bkpt_print_mention (struct breakpoint *b)
12651 {
12652 if (current_uiout->is_mi_like_p ())
12653 return;
12654
12655 switch (b->type)
12656 {
12657 case bp_breakpoint:
12658 case bp_gnu_ifunc_resolver:
12659 if (b->disposition == disp_del)
12660 printf_filtered (_("Temporary breakpoint"));
12661 else
12662 printf_filtered (_("Breakpoint"));
12663 printf_filtered (_(" %d"), b->number);
12664 if (b->type == bp_gnu_ifunc_resolver)
12665 printf_filtered (_(" at gnu-indirect-function resolver"));
12666 break;
12667 case bp_hardware_breakpoint:
12668 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12669 break;
12670 case bp_dprintf:
12671 printf_filtered (_("Dprintf %d"), b->number);
12672 break;
12673 }
12674
12675 say_where (b);
12676 }
12677
12678 static void
12679 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12680 {
12681 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12682 fprintf_unfiltered (fp, "tbreak");
12683 else if (tp->type == bp_breakpoint)
12684 fprintf_unfiltered (fp, "break");
12685 else if (tp->type == bp_hardware_breakpoint
12686 && tp->disposition == disp_del)
12687 fprintf_unfiltered (fp, "thbreak");
12688 else if (tp->type == bp_hardware_breakpoint)
12689 fprintf_unfiltered (fp, "hbreak");
12690 else
12691 internal_error (__FILE__, __LINE__,
12692 _("unhandled breakpoint type %d"), (int) tp->type);
12693
12694 fprintf_unfiltered (fp, " %s",
12695 event_location_to_string (tp->location.get ()));
12696
12697 /* Print out extra_string if this breakpoint is pending. It might
12698 contain, for example, conditions that were set by the user. */
12699 if (tp->loc == NULL && tp->extra_string != NULL)
12700 fprintf_unfiltered (fp, " %s", tp->extra_string);
12701
12702 print_recreate_thread (tp, fp);
12703 }
12704
12705 static void
12706 bkpt_create_sals_from_location (struct event_location *location,
12707 struct linespec_result *canonical,
12708 enum bptype type_wanted)
12709 {
12710 create_sals_from_location_default (location, canonical, type_wanted);
12711 }
12712
12713 static void
12714 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12715 struct linespec_result *canonical,
12716 gdb::unique_xmalloc_ptr<char> cond_string,
12717 gdb::unique_xmalloc_ptr<char> extra_string,
12718 enum bptype type_wanted,
12719 enum bpdisp disposition,
12720 int thread,
12721 int task, int ignore_count,
12722 const struct breakpoint_ops *ops,
12723 int from_tty, int enabled,
12724 int internal, unsigned flags)
12725 {
12726 create_breakpoints_sal_default (gdbarch, canonical,
12727 std::move (cond_string),
12728 std::move (extra_string),
12729 type_wanted,
12730 disposition, thread, task,
12731 ignore_count, ops, from_tty,
12732 enabled, internal, flags);
12733 }
12734
12735 static std::vector<symtab_and_line>
12736 bkpt_decode_location (struct breakpoint *b,
12737 struct event_location *location,
12738 struct program_space *search_pspace)
12739 {
12740 return decode_location_default (b, location, search_pspace);
12741 }
12742
12743 /* Virtual table for internal breakpoints. */
12744
12745 static void
12746 internal_bkpt_re_set (struct breakpoint *b)
12747 {
12748 switch (b->type)
12749 {
12750 /* Delete overlay event and longjmp master breakpoints; they
12751 will be reset later by breakpoint_re_set. */
12752 case bp_overlay_event:
12753 case bp_longjmp_master:
12754 case bp_std_terminate_master:
12755 case bp_exception_master:
12756 delete_breakpoint (b);
12757 break;
12758
12759 /* This breakpoint is special, it's set up when the inferior
12760 starts and we really don't want to touch it. */
12761 case bp_shlib_event:
12762
12763 /* Like bp_shlib_event, this breakpoint type is special. Once
12764 it is set up, we do not want to touch it. */
12765 case bp_thread_event:
12766 break;
12767 }
12768 }
12769
12770 static void
12771 internal_bkpt_check_status (bpstat bs)
12772 {
12773 if (bs->breakpoint_at->type == bp_shlib_event)
12774 {
12775 /* If requested, stop when the dynamic linker notifies GDB of
12776 events. This allows the user to get control and place
12777 breakpoints in initializer routines for dynamically loaded
12778 objects (among other things). */
12779 bs->stop = stop_on_solib_events;
12780 bs->print = stop_on_solib_events;
12781 }
12782 else
12783 bs->stop = 0;
12784 }
12785
12786 static enum print_stop_action
12787 internal_bkpt_print_it (bpstat bs)
12788 {
12789 struct breakpoint *b;
12790
12791 b = bs->breakpoint_at;
12792
12793 switch (b->type)
12794 {
12795 case bp_shlib_event:
12796 /* Did we stop because the user set the stop_on_solib_events
12797 variable? (If so, we report this as a generic, "Stopped due
12798 to shlib event" message.) */
12799 print_solib_event (0);
12800 break;
12801
12802 case bp_thread_event:
12803 /* Not sure how we will get here.
12804 GDB should not stop for these breakpoints. */
12805 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12806 break;
12807
12808 case bp_overlay_event:
12809 /* By analogy with the thread event, GDB should not stop for these. */
12810 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12811 break;
12812
12813 case bp_longjmp_master:
12814 /* These should never be enabled. */
12815 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12816 break;
12817
12818 case bp_std_terminate_master:
12819 /* These should never be enabled. */
12820 printf_filtered (_("std::terminate Master Breakpoint: "
12821 "gdb should not stop!\n"));
12822 break;
12823
12824 case bp_exception_master:
12825 /* These should never be enabled. */
12826 printf_filtered (_("Exception Master Breakpoint: "
12827 "gdb should not stop!\n"));
12828 break;
12829 }
12830
12831 return PRINT_NOTHING;
12832 }
12833
12834 static void
12835 internal_bkpt_print_mention (struct breakpoint *b)
12836 {
12837 /* Nothing to mention. These breakpoints are internal. */
12838 }
12839
12840 /* Virtual table for momentary breakpoints */
12841
12842 static void
12843 momentary_bkpt_re_set (struct breakpoint *b)
12844 {
12845 /* Keep temporary breakpoints, which can be encountered when we step
12846 over a dlopen call and solib_add is resetting the breakpoints.
12847 Otherwise these should have been blown away via the cleanup chain
12848 or by breakpoint_init_inferior when we rerun the executable. */
12849 }
12850
12851 static void
12852 momentary_bkpt_check_status (bpstat bs)
12853 {
12854 /* Nothing. The point of these breakpoints is causing a stop. */
12855 }
12856
12857 static enum print_stop_action
12858 momentary_bkpt_print_it (bpstat bs)
12859 {
12860 return PRINT_UNKNOWN;
12861 }
12862
12863 static void
12864 momentary_bkpt_print_mention (struct breakpoint *b)
12865 {
12866 /* Nothing to mention. These breakpoints are internal. */
12867 }
12868
12869 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12870
12871 It gets cleared already on the removal of the first one of such placed
12872 breakpoints. This is OK as they get all removed altogether. */
12873
12874 longjmp_breakpoint::~longjmp_breakpoint ()
12875 {
12876 thread_info *tp = find_thread_global_id (this->thread);
12877
12878 if (tp != NULL)
12879 tp->initiating_frame = null_frame_id;
12880 }
12881
12882 /* Specific methods for probe breakpoints. */
12883
12884 static int
12885 bkpt_probe_insert_location (struct bp_location *bl)
12886 {
12887 int v = bkpt_insert_location (bl);
12888
12889 if (v == 0)
12890 {
12891 /* The insertion was successful, now let's set the probe's semaphore
12892 if needed. */
12893 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
12894 }
12895
12896 return v;
12897 }
12898
12899 static int
12900 bkpt_probe_remove_location (struct bp_location *bl,
12901 enum remove_bp_reason reason)
12902 {
12903 /* Let's clear the semaphore before removing the location. */
12904 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
12905
12906 return bkpt_remove_location (bl, reason);
12907 }
12908
12909 static void
12910 bkpt_probe_create_sals_from_location (struct event_location *location,
12911 struct linespec_result *canonical,
12912 enum bptype type_wanted)
12913 {
12914 struct linespec_sals lsal;
12915
12916 lsal.sals = parse_probes (location, NULL, canonical);
12917 lsal.canonical
12918 = xstrdup (event_location_to_string (canonical->location.get ()));
12919 canonical->lsals.push_back (std::move (lsal));
12920 }
12921
12922 static std::vector<symtab_and_line>
12923 bkpt_probe_decode_location (struct breakpoint *b,
12924 struct event_location *location,
12925 struct program_space *search_pspace)
12926 {
12927 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12928 if (sals.empty ())
12929 error (_("probe not found"));
12930 return sals;
12931 }
12932
12933 /* The breakpoint_ops structure to be used in tracepoints. */
12934
12935 static void
12936 tracepoint_re_set (struct breakpoint *b)
12937 {
12938 breakpoint_re_set_default (b);
12939 }
12940
12941 static int
12942 tracepoint_breakpoint_hit (const struct bp_location *bl,
12943 const address_space *aspace, CORE_ADDR bp_addr,
12944 const struct target_waitstatus *ws)
12945 {
12946 /* By definition, the inferior does not report stops at
12947 tracepoints. */
12948 return 0;
12949 }
12950
12951 static void
12952 tracepoint_print_one_detail (const struct breakpoint *self,
12953 struct ui_out *uiout)
12954 {
12955 struct tracepoint *tp = (struct tracepoint *) self;
12956 if (!tp->static_trace_marker_id.empty ())
12957 {
12958 gdb_assert (self->type == bp_static_tracepoint);
12959
12960 uiout->message ("\tmarker id is %pF\n",
12961 string_field ("static-tracepoint-marker-string-id",
12962 tp->static_trace_marker_id.c_str ()));
12963 }
12964 }
12965
12966 static void
12967 tracepoint_print_mention (struct breakpoint *b)
12968 {
12969 if (current_uiout->is_mi_like_p ())
12970 return;
12971
12972 switch (b->type)
12973 {
12974 case bp_tracepoint:
12975 printf_filtered (_("Tracepoint"));
12976 printf_filtered (_(" %d"), b->number);
12977 break;
12978 case bp_fast_tracepoint:
12979 printf_filtered (_("Fast tracepoint"));
12980 printf_filtered (_(" %d"), b->number);
12981 break;
12982 case bp_static_tracepoint:
12983 printf_filtered (_("Static tracepoint"));
12984 printf_filtered (_(" %d"), b->number);
12985 break;
12986 default:
12987 internal_error (__FILE__, __LINE__,
12988 _("unhandled tracepoint type %d"), (int) b->type);
12989 }
12990
12991 say_where (b);
12992 }
12993
12994 static void
12995 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
12996 {
12997 struct tracepoint *tp = (struct tracepoint *) self;
12998
12999 if (self->type == bp_fast_tracepoint)
13000 fprintf_unfiltered (fp, "ftrace");
13001 else if (self->type == bp_static_tracepoint)
13002 fprintf_unfiltered (fp, "strace");
13003 else if (self->type == bp_tracepoint)
13004 fprintf_unfiltered (fp, "trace");
13005 else
13006 internal_error (__FILE__, __LINE__,
13007 _("unhandled tracepoint type %d"), (int) self->type);
13008
13009 fprintf_unfiltered (fp, " %s",
13010 event_location_to_string (self->location.get ()));
13011 print_recreate_thread (self, fp);
13012
13013 if (tp->pass_count)
13014 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13015 }
13016
13017 static void
13018 tracepoint_create_sals_from_location (struct event_location *location,
13019 struct linespec_result *canonical,
13020 enum bptype type_wanted)
13021 {
13022 create_sals_from_location_default (location, canonical, type_wanted);
13023 }
13024
13025 static void
13026 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13027 struct linespec_result *canonical,
13028 gdb::unique_xmalloc_ptr<char> cond_string,
13029 gdb::unique_xmalloc_ptr<char> extra_string,
13030 enum bptype type_wanted,
13031 enum bpdisp disposition,
13032 int thread,
13033 int task, int ignore_count,
13034 const struct breakpoint_ops *ops,
13035 int from_tty, int enabled,
13036 int internal, unsigned flags)
13037 {
13038 create_breakpoints_sal_default (gdbarch, canonical,
13039 std::move (cond_string),
13040 std::move (extra_string),
13041 type_wanted,
13042 disposition, thread, task,
13043 ignore_count, ops, from_tty,
13044 enabled, internal, flags);
13045 }
13046
13047 static std::vector<symtab_and_line>
13048 tracepoint_decode_location (struct breakpoint *b,
13049 struct event_location *location,
13050 struct program_space *search_pspace)
13051 {
13052 return decode_location_default (b, location, search_pspace);
13053 }
13054
13055 struct breakpoint_ops tracepoint_breakpoint_ops;
13056
13057 /* Virtual table for tracepoints on static probes. */
13058
13059 static void
13060 tracepoint_probe_create_sals_from_location
13061 (struct event_location *location,
13062 struct linespec_result *canonical,
13063 enum bptype type_wanted)
13064 {
13065 /* We use the same method for breakpoint on probes. */
13066 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13067 }
13068
13069 static std::vector<symtab_and_line>
13070 tracepoint_probe_decode_location (struct breakpoint *b,
13071 struct event_location *location,
13072 struct program_space *search_pspace)
13073 {
13074 /* We use the same method for breakpoint on probes. */
13075 return bkpt_probe_decode_location (b, location, search_pspace);
13076 }
13077
13078 /* Dprintf breakpoint_ops methods. */
13079
13080 static void
13081 dprintf_re_set (struct breakpoint *b)
13082 {
13083 breakpoint_re_set_default (b);
13084
13085 /* extra_string should never be non-NULL for dprintf. */
13086 gdb_assert (b->extra_string != NULL);
13087
13088 /* 1 - connect to target 1, that can run breakpoint commands.
13089 2 - create a dprintf, which resolves fine.
13090 3 - disconnect from target 1
13091 4 - connect to target 2, that can NOT run breakpoint commands.
13092
13093 After steps #3/#4, you'll want the dprintf command list to
13094 be updated, because target 1 and 2 may well return different
13095 answers for target_can_run_breakpoint_commands().
13096 Given absence of finer grained resetting, we get to do
13097 it all the time. */
13098 if (b->extra_string != NULL)
13099 update_dprintf_command_list (b);
13100 }
13101
13102 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13103
13104 static void
13105 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13106 {
13107 fprintf_unfiltered (fp, "dprintf %s,%s",
13108 event_location_to_string (tp->location.get ()),
13109 tp->extra_string);
13110 print_recreate_thread (tp, fp);
13111 }
13112
13113 /* Implement the "after_condition_true" breakpoint_ops method for
13114 dprintf.
13115
13116 dprintf's are implemented with regular commands in their command
13117 list, but we run the commands here instead of before presenting the
13118 stop to the user, as dprintf's don't actually cause a stop. This
13119 also makes it so that the commands of multiple dprintfs at the same
13120 address are all handled. */
13121
13122 static void
13123 dprintf_after_condition_true (struct bpstats *bs)
13124 {
13125 struct bpstats tmp_bs;
13126 struct bpstats *tmp_bs_p = &tmp_bs;
13127
13128 /* dprintf's never cause a stop. This wasn't set in the
13129 check_status hook instead because that would make the dprintf's
13130 condition not be evaluated. */
13131 bs->stop = 0;
13132
13133 /* Run the command list here. Take ownership of it instead of
13134 copying. We never want these commands to run later in
13135 bpstat_do_actions, if a breakpoint that causes a stop happens to
13136 be set at same address as this dprintf, or even if running the
13137 commands here throws. */
13138 tmp_bs.commands = bs->commands;
13139 bs->commands = NULL;
13140
13141 bpstat_do_actions_1 (&tmp_bs_p);
13142
13143 /* 'tmp_bs.commands' will usually be NULL by now, but
13144 bpstat_do_actions_1 may return early without processing the whole
13145 list. */
13146 }
13147
13148 /* The breakpoint_ops structure to be used on static tracepoints with
13149 markers (`-m'). */
13150
13151 static void
13152 strace_marker_create_sals_from_location (struct event_location *location,
13153 struct linespec_result *canonical,
13154 enum bptype type_wanted)
13155 {
13156 struct linespec_sals lsal;
13157 const char *arg_start, *arg;
13158
13159 arg = arg_start = get_linespec_location (location)->spec_string;
13160 lsal.sals = decode_static_tracepoint_spec (&arg);
13161
13162 std::string str (arg_start, arg - arg_start);
13163 const char *ptr = str.c_str ();
13164 canonical->location
13165 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
13166
13167 lsal.canonical
13168 = xstrdup (event_location_to_string (canonical->location.get ()));
13169 canonical->lsals.push_back (std::move (lsal));
13170 }
13171
13172 static void
13173 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13174 struct linespec_result *canonical,
13175 gdb::unique_xmalloc_ptr<char> cond_string,
13176 gdb::unique_xmalloc_ptr<char> extra_string,
13177 enum bptype type_wanted,
13178 enum bpdisp disposition,
13179 int thread,
13180 int task, int ignore_count,
13181 const struct breakpoint_ops *ops,
13182 int from_tty, int enabled,
13183 int internal, unsigned flags)
13184 {
13185 const linespec_sals &lsal = canonical->lsals[0];
13186
13187 /* If the user is creating a static tracepoint by marker id
13188 (strace -m MARKER_ID), then store the sals index, so that
13189 breakpoint_re_set can try to match up which of the newly
13190 found markers corresponds to this one, and, don't try to
13191 expand multiple locations for each sal, given than SALS
13192 already should contain all sals for MARKER_ID. */
13193
13194 for (size_t i = 0; i < lsal.sals.size (); i++)
13195 {
13196 event_location_up location
13197 = copy_event_location (canonical->location.get ());
13198
13199 std::unique_ptr<tracepoint> tp (new tracepoint ());
13200 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
13201 std::move (location), NULL,
13202 std::move (cond_string),
13203 std::move (extra_string),
13204 type_wanted, disposition,
13205 thread, task, ignore_count, ops,
13206 from_tty, enabled, internal, flags,
13207 canonical->special_display);
13208 /* Given that its possible to have multiple markers with
13209 the same string id, if the user is creating a static
13210 tracepoint by marker id ("strace -m MARKER_ID"), then
13211 store the sals index, so that breakpoint_re_set can
13212 try to match up which of the newly found markers
13213 corresponds to this one */
13214 tp->static_trace_marker_id_idx = i;
13215
13216 install_breakpoint (internal, std::move (tp), 0);
13217 }
13218 }
13219
13220 static std::vector<symtab_and_line>
13221 strace_marker_decode_location (struct breakpoint *b,
13222 struct event_location *location,
13223 struct program_space *search_pspace)
13224 {
13225 struct tracepoint *tp = (struct tracepoint *) b;
13226 const char *s = get_linespec_location (location)->spec_string;
13227
13228 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13229 if (sals.size () > tp->static_trace_marker_id_idx)
13230 {
13231 sals[0] = sals[tp->static_trace_marker_id_idx];
13232 sals.resize (1);
13233 return sals;
13234 }
13235 else
13236 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
13237 }
13238
13239 static struct breakpoint_ops strace_marker_breakpoint_ops;
13240
13241 static int
13242 strace_marker_p (struct breakpoint *b)
13243 {
13244 return b->ops == &strace_marker_breakpoint_ops;
13245 }
13246
13247 /* Delete a breakpoint and clean up all traces of it in the data
13248 structures. */
13249
13250 void
13251 delete_breakpoint (struct breakpoint *bpt)
13252 {
13253 gdb_assert (bpt != NULL);
13254
13255 /* Has this bp already been deleted? This can happen because
13256 multiple lists can hold pointers to bp's. bpstat lists are
13257 especial culprits.
13258
13259 One example of this happening is a watchpoint's scope bp. When
13260 the scope bp triggers, we notice that the watchpoint is out of
13261 scope, and delete it. We also delete its scope bp. But the
13262 scope bp is marked "auto-deleting", and is already on a bpstat.
13263 That bpstat is then checked for auto-deleting bp's, which are
13264 deleted.
13265
13266 A real solution to this problem might involve reference counts in
13267 bp's, and/or giving them pointers back to their referencing
13268 bpstat's, and teaching delete_breakpoint to only free a bp's
13269 storage when no more references were extent. A cheaper bandaid
13270 was chosen. */
13271 if (bpt->type == bp_none)
13272 return;
13273
13274 /* At least avoid this stale reference until the reference counting
13275 of breakpoints gets resolved. */
13276 if (bpt->related_breakpoint != bpt)
13277 {
13278 struct breakpoint *related;
13279 struct watchpoint *w;
13280
13281 if (bpt->type == bp_watchpoint_scope)
13282 w = (struct watchpoint *) bpt->related_breakpoint;
13283 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13284 w = (struct watchpoint *) bpt;
13285 else
13286 w = NULL;
13287 if (w != NULL)
13288 watchpoint_del_at_next_stop (w);
13289
13290 /* Unlink bpt from the bpt->related_breakpoint ring. */
13291 for (related = bpt; related->related_breakpoint != bpt;
13292 related = related->related_breakpoint);
13293 related->related_breakpoint = bpt->related_breakpoint;
13294 bpt->related_breakpoint = bpt;
13295 }
13296
13297 /* watch_command_1 creates a watchpoint but only sets its number if
13298 update_watchpoint succeeds in creating its bp_locations. If there's
13299 a problem in that process, we'll be asked to delete the half-created
13300 watchpoint. In that case, don't announce the deletion. */
13301 if (bpt->number)
13302 gdb::observers::breakpoint_deleted.notify (bpt);
13303
13304 if (breakpoint_chain == bpt)
13305 breakpoint_chain = bpt->next;
13306
13307 for (breakpoint *b : all_breakpoints ())
13308 if (b->next == bpt)
13309 {
13310 b->next = bpt->next;
13311 break;
13312 }
13313
13314 /* Be sure no bpstat's are pointing at the breakpoint after it's
13315 been freed. */
13316 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13317 in all threads for now. Note that we cannot just remove bpstats
13318 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13319 commands are associated with the bpstat; if we remove it here,
13320 then the later call to bpstat_do_actions (&stop_bpstat); in
13321 event-top.c won't do anything, and temporary breakpoints with
13322 commands won't work. */
13323
13324 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13325
13326 /* Now that breakpoint is removed from breakpoint list, update the
13327 global location list. This will remove locations that used to
13328 belong to this breakpoint. Do this before freeing the breakpoint
13329 itself, since remove_breakpoint looks at location's owner. It
13330 might be better design to have location completely
13331 self-contained, but it's not the case now. */
13332 update_global_location_list (UGLL_DONT_INSERT);
13333
13334 /* On the chance that someone will soon try again to delete this
13335 same bp, we mark it as deleted before freeing its storage. */
13336 bpt->type = bp_none;
13337 delete bpt;
13338 }
13339
13340 /* Iterator function to call a user-provided callback function once
13341 for each of B and its related breakpoints. */
13342
13343 static void
13344 iterate_over_related_breakpoints (struct breakpoint *b,
13345 gdb::function_view<void (breakpoint *)> function)
13346 {
13347 struct breakpoint *related;
13348
13349 related = b;
13350 do
13351 {
13352 struct breakpoint *next;
13353
13354 /* FUNCTION may delete RELATED. */
13355 next = related->related_breakpoint;
13356
13357 if (next == related)
13358 {
13359 /* RELATED is the last ring entry. */
13360 function (related);
13361
13362 /* FUNCTION may have deleted it, so we'd never reach back to
13363 B. There's nothing left to do anyway, so just break
13364 out. */
13365 break;
13366 }
13367 else
13368 function (related);
13369
13370 related = next;
13371 }
13372 while (related != b);
13373 }
13374
13375 static void
13376 delete_command (const char *arg, int from_tty)
13377 {
13378 breakpoint *b_tmp;
13379
13380 dont_repeat ();
13381
13382 if (arg == 0)
13383 {
13384 int breaks_to_delete = 0;
13385
13386 /* Delete all breakpoints if no argument. Do not delete
13387 internal breakpoints, these have to be deleted with an
13388 explicit breakpoint number argument. */
13389 for (breakpoint *b : all_breakpoints ())
13390 if (user_breakpoint_p (b))
13391 {
13392 breaks_to_delete = 1;
13393 break;
13394 }
13395
13396 /* Ask user only if there are some breakpoints to delete. */
13397 if (!from_tty
13398 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13399 {
13400 breakpoint *b;
13401
13402 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13403 if (user_breakpoint_p (b))
13404 delete_breakpoint (b);
13405 }
13406 }
13407 else
13408 map_breakpoint_numbers
13409 (arg, [&] (breakpoint *br)
13410 {
13411 iterate_over_related_breakpoints (br, delete_breakpoint);
13412 });
13413 }
13414
13415 /* Return true if all locations of B bound to PSPACE are pending. If
13416 PSPACE is NULL, all locations of all program spaces are
13417 considered. */
13418
13419 static int
13420 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13421 {
13422 struct bp_location *loc;
13423
13424 for (loc = b->loc; loc != NULL; loc = loc->next)
13425 if ((pspace == NULL
13426 || loc->pspace == pspace)
13427 && !loc->shlib_disabled
13428 && !loc->pspace->executing_startup)
13429 return 0;
13430 return 1;
13431 }
13432
13433 /* Subroutine of update_breakpoint_locations to simplify it.
13434 Return non-zero if multiple fns in list LOC have the same name.
13435 Null names are ignored. */
13436
13437 static int
13438 ambiguous_names_p (struct bp_location *loc)
13439 {
13440 struct bp_location *l;
13441 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
13442 xcalloc, xfree));
13443
13444 for (l = loc; l != NULL; l = l->next)
13445 {
13446 const char **slot;
13447 const char *name = l->function_name;
13448
13449 /* Allow for some names to be NULL, ignore them. */
13450 if (name == NULL)
13451 continue;
13452
13453 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
13454 INSERT);
13455 /* NOTE: We can assume slot != NULL here because xcalloc never
13456 returns NULL. */
13457 if (*slot != NULL)
13458 return 1;
13459 *slot = name;
13460 }
13461
13462 return 0;
13463 }
13464
13465 /* When symbols change, it probably means the sources changed as well,
13466 and it might mean the static tracepoint markers are no longer at
13467 the same address or line numbers they used to be at last we
13468 checked. Losing your static tracepoints whenever you rebuild is
13469 undesirable. This function tries to resync/rematch gdb static
13470 tracepoints with the markers on the target, for static tracepoints
13471 that have not been set by marker id. Static tracepoint that have
13472 been set by marker id are reset by marker id in breakpoint_re_set.
13473 The heuristic is:
13474
13475 1) For a tracepoint set at a specific address, look for a marker at
13476 the old PC. If one is found there, assume to be the same marker.
13477 If the name / string id of the marker found is different from the
13478 previous known name, assume that means the user renamed the marker
13479 in the sources, and output a warning.
13480
13481 2) For a tracepoint set at a given line number, look for a marker
13482 at the new address of the old line number. If one is found there,
13483 assume to be the same marker. If the name / string id of the
13484 marker found is different from the previous known name, assume that
13485 means the user renamed the marker in the sources, and output a
13486 warning.
13487
13488 3) If a marker is no longer found at the same address or line, it
13489 may mean the marker no longer exists. But it may also just mean
13490 the code changed a bit. Maybe the user added a few lines of code
13491 that made the marker move up or down (in line number terms). Ask
13492 the target for info about the marker with the string id as we knew
13493 it. If found, update line number and address in the matching
13494 static tracepoint. This will get confused if there's more than one
13495 marker with the same ID (possible in UST, although unadvised
13496 precisely because it confuses tools). */
13497
13498 static struct symtab_and_line
13499 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13500 {
13501 struct tracepoint *tp = (struct tracepoint *) b;
13502 struct static_tracepoint_marker marker;
13503 CORE_ADDR pc;
13504
13505 pc = sal.pc;
13506 if (sal.line)
13507 find_line_pc (sal.symtab, sal.line, &pc);
13508
13509 if (target_static_tracepoint_marker_at (pc, &marker))
13510 {
13511 if (tp->static_trace_marker_id != marker.str_id)
13512 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13513 b->number, tp->static_trace_marker_id.c_str (),
13514 marker.str_id.c_str ());
13515
13516 tp->static_trace_marker_id = std::move (marker.str_id);
13517
13518 return sal;
13519 }
13520
13521 /* Old marker wasn't found on target at lineno. Try looking it up
13522 by string ID. */
13523 if (!sal.explicit_pc
13524 && sal.line != 0
13525 && sal.symtab != NULL
13526 && !tp->static_trace_marker_id.empty ())
13527 {
13528 std::vector<static_tracepoint_marker> markers
13529 = target_static_tracepoint_markers_by_strid
13530 (tp->static_trace_marker_id.c_str ());
13531
13532 if (!markers.empty ())
13533 {
13534 struct symbol *sym;
13535 struct static_tracepoint_marker *tpmarker;
13536 struct ui_out *uiout = current_uiout;
13537 struct explicit_location explicit_loc;
13538
13539 tpmarker = &markers[0];
13540
13541 tp->static_trace_marker_id = std::move (tpmarker->str_id);
13542
13543 warning (_("marker for static tracepoint %d (%s) not "
13544 "found at previous line number"),
13545 b->number, tp->static_trace_marker_id.c_str ());
13546
13547 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
13548 sym = find_pc_sect_function (tpmarker->address, NULL);
13549 uiout->text ("Now in ");
13550 if (sym)
13551 {
13552 uiout->field_string ("func", sym->print_name (),
13553 function_name_style.style ());
13554 uiout->text (" at ");
13555 }
13556 uiout->field_string ("file",
13557 symtab_to_filename_for_display (sal2.symtab),
13558 file_name_style.style ());
13559 uiout->text (":");
13560
13561 if (uiout->is_mi_like_p ())
13562 {
13563 const char *fullname = symtab_to_fullname (sal2.symtab);
13564
13565 uiout->field_string ("fullname", fullname);
13566 }
13567
13568 uiout->field_signed ("line", sal2.line);
13569 uiout->text ("\n");
13570
13571 b->loc->line_number = sal2.line;
13572 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13573
13574 b->location.reset (NULL);
13575 initialize_explicit_location (&explicit_loc);
13576 explicit_loc.source_filename
13577 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13578 explicit_loc.line_offset.offset = b->loc->line_number;
13579 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13580 b->location = new_explicit_location (&explicit_loc);
13581
13582 /* Might be nice to check if function changed, and warn if
13583 so. */
13584 }
13585 }
13586 return sal;
13587 }
13588
13589 /* Returns 1 iff locations A and B are sufficiently same that
13590 we don't need to report breakpoint as changed. */
13591
13592 static int
13593 locations_are_equal (struct bp_location *a, struct bp_location *b)
13594 {
13595 while (a && b)
13596 {
13597 if (a->address != b->address)
13598 return 0;
13599
13600 if (a->shlib_disabled != b->shlib_disabled)
13601 return 0;
13602
13603 if (a->enabled != b->enabled)
13604 return 0;
13605
13606 if (a->disabled_by_cond != b->disabled_by_cond)
13607 return 0;
13608
13609 a = a->next;
13610 b = b->next;
13611 }
13612
13613 if ((a == NULL) != (b == NULL))
13614 return 0;
13615
13616 return 1;
13617 }
13618
13619 /* Split all locations of B that are bound to PSPACE out of B's
13620 location list to a separate list and return that list's head. If
13621 PSPACE is NULL, hoist out all locations of B. */
13622
13623 static struct bp_location *
13624 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13625 {
13626 struct bp_location head;
13627 struct bp_location *i = b->loc;
13628 struct bp_location **i_link = &b->loc;
13629 struct bp_location *hoisted = &head;
13630
13631 if (pspace == NULL)
13632 {
13633 i = b->loc;
13634 b->loc = NULL;
13635 return i;
13636 }
13637
13638 head.next = NULL;
13639
13640 while (i != NULL)
13641 {
13642 if (i->pspace == pspace)
13643 {
13644 *i_link = i->next;
13645 i->next = NULL;
13646 hoisted->next = i;
13647 hoisted = i;
13648 }
13649 else
13650 i_link = &i->next;
13651 i = *i_link;
13652 }
13653
13654 return head.next;
13655 }
13656
13657 /* Create new breakpoint locations for B (a hardware or software
13658 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13659 zero, then B is a ranged breakpoint. Only recreates locations for
13660 FILTER_PSPACE. Locations of other program spaces are left
13661 untouched. */
13662
13663 void
13664 update_breakpoint_locations (struct breakpoint *b,
13665 struct program_space *filter_pspace,
13666 gdb::array_view<const symtab_and_line> sals,
13667 gdb::array_view<const symtab_and_line> sals_end)
13668 {
13669 struct bp_location *existing_locations;
13670
13671 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
13672 {
13673 /* Ranged breakpoints have only one start location and one end
13674 location. */
13675 b->enable_state = bp_disabled;
13676 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13677 "multiple locations found\n"),
13678 b->number);
13679 return;
13680 }
13681
13682 /* If there's no new locations, and all existing locations are
13683 pending, don't do anything. This optimizes the common case where
13684 all locations are in the same shared library, that was unloaded.
13685 We'd like to retain the location, so that when the library is
13686 loaded again, we don't loose the enabled/disabled status of the
13687 individual locations. */
13688 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
13689 return;
13690
13691 existing_locations = hoist_existing_locations (b, filter_pspace);
13692
13693 for (const auto &sal : sals)
13694 {
13695 struct bp_location *new_loc;
13696
13697 switch_to_program_space_and_thread (sal.pspace);
13698
13699 new_loc = add_location_to_breakpoint (b, &sal);
13700
13701 /* Reparse conditions, they might contain references to the
13702 old symtab. */
13703 if (b->cond_string != NULL)
13704 {
13705 const char *s;
13706
13707 s = b->cond_string;
13708 try
13709 {
13710 new_loc->cond = parse_exp_1 (&s, sal.pc,
13711 block_for_pc (sal.pc),
13712 0);
13713 }
13714 catch (const gdb_exception_error &e)
13715 {
13716 new_loc->disabled_by_cond = true;
13717 }
13718 }
13719
13720 if (!sals_end.empty ())
13721 {
13722 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13723
13724 new_loc->length = end - sals[0].pc + 1;
13725 }
13726 }
13727
13728 /* If possible, carry over 'disable' status from existing
13729 breakpoints. */
13730 {
13731 struct bp_location *e = existing_locations;
13732 /* If there are multiple breakpoints with the same function name,
13733 e.g. for inline functions, comparing function names won't work.
13734 Instead compare pc addresses; this is just a heuristic as things
13735 may have moved, but in practice it gives the correct answer
13736 often enough until a better solution is found. */
13737 int have_ambiguous_names = ambiguous_names_p (b->loc);
13738
13739 for (; e; e = e->next)
13740 {
13741 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
13742 {
13743 struct bp_location *l = b->loc;
13744 if (have_ambiguous_names)
13745 {
13746 for (; l; l = l->next)
13747 {
13748 /* Ignore software vs hardware location type at
13749 this point, because with "set breakpoint
13750 auto-hw", after a re-set, locations that were
13751 hardware can end up as software, or vice versa.
13752 As mentioned above, this is an heuristic and in
13753 practice should give the correct answer often
13754 enough. */
13755 if (breakpoint_locations_match (e, l, true))
13756 {
13757 l->enabled = e->enabled;
13758 l->disabled_by_cond = e->disabled_by_cond;
13759 break;
13760 }
13761 }
13762 }
13763 else
13764 {
13765 for (; l; l = l->next)
13766 if (l->function_name
13767 && strcmp (e->function_name, l->function_name) == 0)
13768 {
13769 l->enabled = e->enabled;
13770 l->disabled_by_cond = e->disabled_by_cond;
13771 break;
13772 }
13773 }
13774 }
13775 }
13776 }
13777
13778 if (!locations_are_equal (existing_locations, b->loc))
13779 gdb::observers::breakpoint_modified.notify (b);
13780 }
13781
13782 /* Find the SaL locations corresponding to the given LOCATION.
13783 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13784
13785 static std::vector<symtab_and_line>
13786 location_to_sals (struct breakpoint *b, struct event_location *location,
13787 struct program_space *search_pspace, int *found)
13788 {
13789 struct gdb_exception exception;
13790
13791 gdb_assert (b->ops != NULL);
13792
13793 std::vector<symtab_and_line> sals;
13794
13795 try
13796 {
13797 sals = b->ops->decode_location (b, location, search_pspace);
13798 }
13799 catch (gdb_exception_error &e)
13800 {
13801 int not_found_and_ok = 0;
13802
13803 /* For pending breakpoints, it's expected that parsing will
13804 fail until the right shared library is loaded. User has
13805 already told to create pending breakpoints and don't need
13806 extra messages. If breakpoint is in bp_shlib_disabled
13807 state, then user already saw the message about that
13808 breakpoint being disabled, and don't want to see more
13809 errors. */
13810 if (e.error == NOT_FOUND_ERROR
13811 && (b->condition_not_parsed
13812 || (b->loc != NULL
13813 && search_pspace != NULL
13814 && b->loc->pspace != search_pspace)
13815 || (b->loc && b->loc->shlib_disabled)
13816 || (b->loc && b->loc->pspace->executing_startup)
13817 || b->enable_state == bp_disabled))
13818 not_found_and_ok = 1;
13819
13820 if (!not_found_and_ok)
13821 {
13822 /* We surely don't want to warn about the same breakpoint
13823 10 times. One solution, implemented here, is disable
13824 the breakpoint on error. Another solution would be to
13825 have separate 'warning emitted' flag. Since this
13826 happens only when a binary has changed, I don't know
13827 which approach is better. */
13828 b->enable_state = bp_disabled;
13829 throw;
13830 }
13831
13832 exception = std::move (e);
13833 }
13834
13835 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13836 {
13837 for (auto &sal : sals)
13838 resolve_sal_pc (&sal);
13839 if (b->condition_not_parsed && b->extra_string != NULL)
13840 {
13841 char *cond_string, *extra_string;
13842 int thread, task;
13843
13844 find_condition_and_thread_for_sals (sals, b->extra_string,
13845 &cond_string, &thread,
13846 &task, &extra_string);
13847 gdb_assert (b->cond_string == NULL);
13848 if (cond_string)
13849 b->cond_string = cond_string;
13850 b->thread = thread;
13851 b->task = task;
13852 if (extra_string)
13853 {
13854 xfree (b->extra_string);
13855 b->extra_string = extra_string;
13856 }
13857 b->condition_not_parsed = 0;
13858 }
13859
13860 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13861 sals[0] = update_static_tracepoint (b, sals[0]);
13862
13863 *found = 1;
13864 }
13865 else
13866 *found = 0;
13867
13868 return sals;
13869 }
13870
13871 /* The default re_set method, for typical hardware or software
13872 breakpoints. Reevaluate the breakpoint and recreate its
13873 locations. */
13874
13875 static void
13876 breakpoint_re_set_default (struct breakpoint *b)
13877 {
13878 struct program_space *filter_pspace = current_program_space;
13879 std::vector<symtab_and_line> expanded, expanded_end;
13880
13881 int found;
13882 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13883 filter_pspace, &found);
13884 if (found)
13885 expanded = std::move (sals);
13886
13887 if (b->location_range_end != NULL)
13888 {
13889 std::vector<symtab_and_line> sals_end
13890 = location_to_sals (b, b->location_range_end.get (),
13891 filter_pspace, &found);
13892 if (found)
13893 expanded_end = std::move (sals_end);
13894 }
13895
13896 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
13897 }
13898
13899 /* Default method for creating SALs from an address string. It basically
13900 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13901
13902 static void
13903 create_sals_from_location_default (struct event_location *location,
13904 struct linespec_result *canonical,
13905 enum bptype type_wanted)
13906 {
13907 parse_breakpoint_sals (location, canonical);
13908 }
13909
13910 /* Call create_breakpoints_sal for the given arguments. This is the default
13911 function for the `create_breakpoints_sal' method of
13912 breakpoint_ops. */
13913
13914 static void
13915 create_breakpoints_sal_default (struct gdbarch *gdbarch,
13916 struct linespec_result *canonical,
13917 gdb::unique_xmalloc_ptr<char> cond_string,
13918 gdb::unique_xmalloc_ptr<char> extra_string,
13919 enum bptype type_wanted,
13920 enum bpdisp disposition,
13921 int thread,
13922 int task, int ignore_count,
13923 const struct breakpoint_ops *ops,
13924 int from_tty, int enabled,
13925 int internal, unsigned flags)
13926 {
13927 create_breakpoints_sal (gdbarch, canonical,
13928 std::move (cond_string),
13929 std::move (extra_string),
13930 type_wanted, disposition,
13931 thread, task, ignore_count, ops, from_tty,
13932 enabled, internal, flags);
13933 }
13934
13935 /* Decode the line represented by S by calling decode_line_full. This is the
13936 default function for the `decode_location' method of breakpoint_ops. */
13937
13938 static std::vector<symtab_and_line>
13939 decode_location_default (struct breakpoint *b,
13940 struct event_location *location,
13941 struct program_space *search_pspace)
13942 {
13943 struct linespec_result canonical;
13944
13945 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
13946 NULL, 0, &canonical, multiple_symbols_all,
13947 b->filter.get ());
13948
13949 /* We should get 0 or 1 resulting SALs. */
13950 gdb_assert (canonical.lsals.size () < 2);
13951
13952 if (!canonical.lsals.empty ())
13953 {
13954 const linespec_sals &lsal = canonical.lsals[0];
13955 return std::move (lsal.sals);
13956 }
13957 return {};
13958 }
13959
13960 /* Reset a breakpoint. */
13961
13962 static void
13963 breakpoint_re_set_one (breakpoint *b)
13964 {
13965 input_radix = b->input_radix;
13966 set_language (b->language);
13967
13968 b->ops->re_set (b);
13969 }
13970
13971 /* Re-set breakpoint locations for the current program space.
13972 Locations bound to other program spaces are left untouched. */
13973
13974 void
13975 breakpoint_re_set (void)
13976 {
13977 struct breakpoint *b, *b_tmp;
13978
13979 {
13980 scoped_restore_current_language save_language;
13981 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
13982 scoped_restore_current_pspace_and_thread restore_pspace_thread;
13983
13984 /* breakpoint_re_set_one sets the current_language to the language
13985 of the breakpoint it is resetting (see prepare_re_set_context)
13986 before re-evaluating the breakpoint's location. This change can
13987 unfortunately get undone by accident if the language_mode is set
13988 to auto, and we either switch frames, or more likely in this context,
13989 we select the current frame.
13990
13991 We prevent this by temporarily turning the language_mode to
13992 language_mode_manual. We restore it once all breakpoints
13993 have been reset. */
13994 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13995 language_mode = language_mode_manual;
13996
13997 /* Note: we must not try to insert locations until after all
13998 breakpoints have been re-set. Otherwise, e.g., when re-setting
13999 breakpoint 1, we'd insert the locations of breakpoint 2, which
14000 hadn't been re-set yet, and thus may have stale locations. */
14001
14002 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14003 {
14004 try
14005 {
14006 breakpoint_re_set_one (b);
14007 }
14008 catch (const gdb_exception &ex)
14009 {
14010 exception_fprintf (gdb_stderr, ex,
14011 "Error in re-setting breakpoint %d: ",
14012 b->number);
14013 }
14014 }
14015
14016 jit_breakpoint_re_set ();
14017 }
14018
14019 create_overlay_event_breakpoint ();
14020 create_longjmp_master_breakpoint ();
14021 create_std_terminate_master_breakpoint ();
14022 create_exception_master_breakpoint ();
14023
14024 /* Now we can insert. */
14025 update_global_location_list (UGLL_MAY_INSERT);
14026 }
14027 \f
14028 /* Reset the thread number of this breakpoint:
14029
14030 - If the breakpoint is for all threads, leave it as-is.
14031 - Else, reset it to the current thread for inferior_ptid. */
14032 void
14033 breakpoint_re_set_thread (struct breakpoint *b)
14034 {
14035 if (b->thread != -1)
14036 {
14037 b->thread = inferior_thread ()->global_num;
14038
14039 /* We're being called after following a fork. The new fork is
14040 selected as current, and unless this was a vfork will have a
14041 different program space from the original thread. Reset that
14042 as well. */
14043 b->loc->pspace = current_program_space;
14044 }
14045 }
14046
14047 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14048 If from_tty is nonzero, it prints a message to that effect,
14049 which ends with a period (no newline). */
14050
14051 void
14052 set_ignore_count (int bptnum, int count, int from_tty)
14053 {
14054 if (count < 0)
14055 count = 0;
14056
14057 for (breakpoint *b : all_breakpoints ())
14058 if (b->number == bptnum)
14059 {
14060 if (is_tracepoint (b))
14061 {
14062 if (from_tty && count != 0)
14063 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14064 bptnum);
14065 return;
14066 }
14067
14068 b->ignore_count = count;
14069 if (from_tty)
14070 {
14071 if (count == 0)
14072 printf_filtered (_("Will stop next time "
14073 "breakpoint %d is reached."),
14074 bptnum);
14075 else if (count == 1)
14076 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14077 bptnum);
14078 else
14079 printf_filtered (_("Will ignore next %d "
14080 "crossings of breakpoint %d."),
14081 count, bptnum);
14082 }
14083 gdb::observers::breakpoint_modified.notify (b);
14084 return;
14085 }
14086
14087 error (_("No breakpoint number %d."), bptnum);
14088 }
14089
14090 /* Command to set ignore-count of breakpoint N to COUNT. */
14091
14092 static void
14093 ignore_command (const char *args, int from_tty)
14094 {
14095 const char *p = args;
14096 int num;
14097
14098 if (p == 0)
14099 error_no_arg (_("a breakpoint number"));
14100
14101 num = get_number (&p);
14102 if (num == 0)
14103 error (_("bad breakpoint number: '%s'"), args);
14104 if (*p == 0)
14105 error (_("Second argument (specified ignore-count) is missing."));
14106
14107 set_ignore_count (num,
14108 longest_to_int (value_as_long (parse_and_eval (p))),
14109 from_tty);
14110 if (from_tty)
14111 printf_filtered ("\n");
14112 }
14113 \f
14114
14115 /* Call FUNCTION on each of the breakpoints with numbers in the range
14116 defined by BP_NUM_RANGE (an inclusive range). */
14117
14118 static void
14119 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
14120 gdb::function_view<void (breakpoint *)> function)
14121 {
14122 if (bp_num_range.first == 0)
14123 {
14124 warning (_("bad breakpoint number at or near '%d'"),
14125 bp_num_range.first);
14126 }
14127 else
14128 {
14129 struct breakpoint *b, *tmp;
14130
14131 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
14132 {
14133 bool match = false;
14134
14135 ALL_BREAKPOINTS_SAFE (b, tmp)
14136 if (b->number == i)
14137 {
14138 match = true;
14139 function (b);
14140 break;
14141 }
14142 if (!match)
14143 printf_unfiltered (_("No breakpoint number %d.\n"), i);
14144 }
14145 }
14146 }
14147
14148 /* Call FUNCTION on each of the breakpoints whose numbers are given in
14149 ARGS. */
14150
14151 static void
14152 map_breakpoint_numbers (const char *args,
14153 gdb::function_view<void (breakpoint *)> function)
14154 {
14155 if (args == NULL || *args == '\0')
14156 error_no_arg (_("one or more breakpoint numbers"));
14157
14158 number_or_range_parser parser (args);
14159
14160 while (!parser.finished ())
14161 {
14162 int num = parser.get_number ();
14163 map_breakpoint_number_range (std::make_pair (num, num), function);
14164 }
14165 }
14166
14167 /* Return the breakpoint location structure corresponding to the
14168 BP_NUM and LOC_NUM values. */
14169
14170 static struct bp_location *
14171 find_location_by_number (int bp_num, int loc_num)
14172 {
14173 breakpoint *b = get_breakpoint (bp_num);
14174
14175 if (!b || b->number != bp_num)
14176 error (_("Bad breakpoint number '%d'"), bp_num);
14177
14178 if (loc_num == 0)
14179 error (_("Bad breakpoint location number '%d'"), loc_num);
14180
14181 int n = 0;
14182 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
14183 if (++n == loc_num)
14184 return loc;
14185
14186 error (_("Bad breakpoint location number '%d'"), loc_num);
14187 }
14188
14189 /* Modes of operation for extract_bp_num. */
14190 enum class extract_bp_kind
14191 {
14192 /* Extracting a breakpoint number. */
14193 bp,
14194
14195 /* Extracting a location number. */
14196 loc,
14197 };
14198
14199 /* Extract a breakpoint or location number (as determined by KIND)
14200 from the string starting at START. TRAILER is a character which
14201 can be found after the number. If you don't want a trailer, use
14202 '\0'. If END_OUT is not NULL, it is set to point after the parsed
14203 string. This always returns a positive integer. */
14204
14205 static int
14206 extract_bp_num (extract_bp_kind kind, const char *start,
14207 int trailer, const char **end_out = NULL)
14208 {
14209 const char *end = start;
14210 int num = get_number_trailer (&end, trailer);
14211 if (num < 0)
14212 error (kind == extract_bp_kind::bp
14213 ? _("Negative breakpoint number '%.*s'")
14214 : _("Negative breakpoint location number '%.*s'"),
14215 int (end - start), start);
14216 if (num == 0)
14217 error (kind == extract_bp_kind::bp
14218 ? _("Bad breakpoint number '%.*s'")
14219 : _("Bad breakpoint location number '%.*s'"),
14220 int (end - start), start);
14221
14222 if (end_out != NULL)
14223 *end_out = end;
14224 return num;
14225 }
14226
14227 /* Extract a breakpoint or location range (as determined by KIND) in
14228 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14229 representing the (inclusive) range. The returned pair's elements
14230 are always positive integers. */
14231
14232 static std::pair<int, int>
14233 extract_bp_or_bp_range (extract_bp_kind kind,
14234 const std::string &arg,
14235 std::string::size_type arg_offset)
14236 {
14237 std::pair<int, int> range;
14238 const char *bp_loc = &arg[arg_offset];
14239 std::string::size_type dash = arg.find ('-', arg_offset);
14240 if (dash != std::string::npos)
14241 {
14242 /* bp_loc is a range (x-z). */
14243 if (arg.length () == dash + 1)
14244 error (kind == extract_bp_kind::bp
14245 ? _("Bad breakpoint number at or near: '%s'")
14246 : _("Bad breakpoint location number at or near: '%s'"),
14247 bp_loc);
14248
14249 const char *end;
14250 const char *start_first = bp_loc;
14251 const char *start_second = &arg[dash + 1];
14252 range.first = extract_bp_num (kind, start_first, '-');
14253 range.second = extract_bp_num (kind, start_second, '\0', &end);
14254
14255 if (range.first > range.second)
14256 error (kind == extract_bp_kind::bp
14257 ? _("Inverted breakpoint range at '%.*s'")
14258 : _("Inverted breakpoint location range at '%.*s'"),
14259 int (end - start_first), start_first);
14260 }
14261 else
14262 {
14263 /* bp_loc is a single value. */
14264 range.first = extract_bp_num (kind, bp_loc, '\0');
14265 range.second = range.first;
14266 }
14267 return range;
14268 }
14269
14270 /* Extract the breakpoint/location range specified by ARG. Returns
14271 the breakpoint range in BP_NUM_RANGE, and the location range in
14272 BP_LOC_RANGE.
14273
14274 ARG may be in any of the following forms:
14275
14276 x where 'x' is a breakpoint number.
14277 x-y where 'x' and 'y' specify a breakpoint numbers range.
14278 x.y where 'x' is a breakpoint number and 'y' a location number.
14279 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14280 location number range.
14281 */
14282
14283 static void
14284 extract_bp_number_and_location (const std::string &arg,
14285 std::pair<int, int> &bp_num_range,
14286 std::pair<int, int> &bp_loc_range)
14287 {
14288 std::string::size_type dot = arg.find ('.');
14289
14290 if (dot != std::string::npos)
14291 {
14292 /* Handle 'x.y' and 'x.y-z' cases. */
14293
14294 if (arg.length () == dot + 1 || dot == 0)
14295 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
14296
14297 bp_num_range.first
14298 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14299 bp_num_range.second = bp_num_range.first;
14300
14301 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14302 arg, dot + 1);
14303 }
14304 else
14305 {
14306 /* Handle x and x-y cases. */
14307
14308 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
14309 bp_loc_range.first = 0;
14310 bp_loc_range.second = 0;
14311 }
14312 }
14313
14314 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14315 specifies whether to enable or disable. */
14316
14317 static void
14318 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14319 {
14320 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14321 if (loc != NULL)
14322 {
14323 if (loc->disabled_by_cond && enable)
14324 error (_("Breakpoint %d's condition is invalid at location %d, "
14325 "cannot enable."), bp_num, loc_num);
14326
14327 if (loc->enabled != enable)
14328 {
14329 loc->enabled = enable;
14330 mark_breakpoint_location_modified (loc);
14331 }
14332 if (target_supports_enable_disable_tracepoint ()
14333 && current_trace_status ()->running && loc->owner
14334 && is_tracepoint (loc->owner))
14335 target_disable_tracepoint (loc);
14336 }
14337 update_global_location_list (UGLL_DONT_INSERT);
14338
14339 gdb::observers::breakpoint_modified.notify (loc->owner);
14340 }
14341
14342 /* Enable or disable a range of breakpoint locations. BP_NUM is the
14343 number of the breakpoint, and BP_LOC_RANGE specifies the
14344 (inclusive) range of location numbers of that breakpoint to
14345 enable/disable. ENABLE specifies whether to enable or disable the
14346 location. */
14347
14348 static void
14349 enable_disable_breakpoint_location_range (int bp_num,
14350 std::pair<int, int> &bp_loc_range,
14351 bool enable)
14352 {
14353 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14354 enable_disable_bp_num_loc (bp_num, i, enable);
14355 }
14356
14357 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14358 If from_tty is nonzero, it prints a message to that effect,
14359 which ends with a period (no newline). */
14360
14361 void
14362 disable_breakpoint (struct breakpoint *bpt)
14363 {
14364 /* Never disable a watchpoint scope breakpoint; we want to
14365 hit them when we leave scope so we can delete both the
14366 watchpoint and its scope breakpoint at that time. */
14367 if (bpt->type == bp_watchpoint_scope)
14368 return;
14369
14370 bpt->enable_state = bp_disabled;
14371
14372 /* Mark breakpoint locations modified. */
14373 mark_breakpoint_modified (bpt);
14374
14375 if (target_supports_enable_disable_tracepoint ()
14376 && current_trace_status ()->running && is_tracepoint (bpt))
14377 {
14378 struct bp_location *location;
14379
14380 for (location = bpt->loc; location; location = location->next)
14381 target_disable_tracepoint (location);
14382 }
14383
14384 update_global_location_list (UGLL_DONT_INSERT);
14385
14386 gdb::observers::breakpoint_modified.notify (bpt);
14387 }
14388
14389 /* Enable or disable the breakpoint(s) or breakpoint location(s)
14390 specified in ARGS. ARGS may be in any of the formats handled by
14391 extract_bp_number_and_location. ENABLE specifies whether to enable
14392 or disable the breakpoints/locations. */
14393
14394 static void
14395 enable_disable_command (const char *args, int from_tty, bool enable)
14396 {
14397 if (args == 0)
14398 {
14399 for (breakpoint *bpt : all_breakpoints ())
14400 if (user_breakpoint_p (bpt))
14401 {
14402 if (enable)
14403 enable_breakpoint (bpt);
14404 else
14405 disable_breakpoint (bpt);
14406 }
14407 }
14408 else
14409 {
14410 std::string num = extract_arg (&args);
14411
14412 while (!num.empty ())
14413 {
14414 std::pair<int, int> bp_num_range, bp_loc_range;
14415
14416 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14417
14418 if (bp_loc_range.first == bp_loc_range.second
14419 && bp_loc_range.first == 0)
14420 {
14421 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14422 map_breakpoint_number_range (bp_num_range,
14423 enable
14424 ? enable_breakpoint
14425 : disable_breakpoint);
14426 }
14427 else
14428 {
14429 /* Handle breakpoint ids with formats 'x.y' or
14430 'x.y-z'. */
14431 enable_disable_breakpoint_location_range
14432 (bp_num_range.first, bp_loc_range, enable);
14433 }
14434 num = extract_arg (&args);
14435 }
14436 }
14437 }
14438
14439 /* The disable command disables the specified breakpoints/locations
14440 (or all defined breakpoints) so they're no longer effective in
14441 stopping the inferior. ARGS may be in any of the forms defined in
14442 extract_bp_number_and_location. */
14443
14444 static void
14445 disable_command (const char *args, int from_tty)
14446 {
14447 enable_disable_command (args, from_tty, false);
14448 }
14449
14450 static void
14451 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14452 int count)
14453 {
14454 int target_resources_ok;
14455
14456 if (bpt->type == bp_hardware_breakpoint)
14457 {
14458 int i;
14459 i = hw_breakpoint_used_count ();
14460 target_resources_ok =
14461 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14462 i + 1, 0);
14463 if (target_resources_ok == 0)
14464 error (_("No hardware breakpoint support in the target."));
14465 else if (target_resources_ok < 0)
14466 error (_("Hardware breakpoints used exceeds limit."));
14467 }
14468
14469 if (is_watchpoint (bpt))
14470 {
14471 /* Initialize it just to avoid a GCC false warning. */
14472 enum enable_state orig_enable_state = bp_disabled;
14473
14474 try
14475 {
14476 struct watchpoint *w = (struct watchpoint *) bpt;
14477
14478 orig_enable_state = bpt->enable_state;
14479 bpt->enable_state = bp_enabled;
14480 update_watchpoint (w, 1 /* reparse */);
14481 }
14482 catch (const gdb_exception &e)
14483 {
14484 bpt->enable_state = orig_enable_state;
14485 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14486 bpt->number);
14487 return;
14488 }
14489 }
14490
14491 bpt->enable_state = bp_enabled;
14492
14493 /* Mark breakpoint locations modified. */
14494 mark_breakpoint_modified (bpt);
14495
14496 if (target_supports_enable_disable_tracepoint ()
14497 && current_trace_status ()->running && is_tracepoint (bpt))
14498 {
14499 struct bp_location *location;
14500
14501 for (location = bpt->loc; location; location = location->next)
14502 target_enable_tracepoint (location);
14503 }
14504
14505 bpt->disposition = disposition;
14506 bpt->enable_count = count;
14507 update_global_location_list (UGLL_MAY_INSERT);
14508
14509 gdb::observers::breakpoint_modified.notify (bpt);
14510 }
14511
14512
14513 void
14514 enable_breakpoint (struct breakpoint *bpt)
14515 {
14516 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14517 }
14518
14519 /* The enable command enables the specified breakpoints/locations (or
14520 all defined breakpoints) so they once again become (or continue to
14521 be) effective in stopping the inferior. ARGS may be in any of the
14522 forms defined in extract_bp_number_and_location. */
14523
14524 static void
14525 enable_command (const char *args, int from_tty)
14526 {
14527 enable_disable_command (args, from_tty, true);
14528 }
14529
14530 static void
14531 enable_once_command (const char *args, int from_tty)
14532 {
14533 map_breakpoint_numbers
14534 (args, [&] (breakpoint *b)
14535 {
14536 iterate_over_related_breakpoints
14537 (b, [&] (breakpoint *bpt)
14538 {
14539 enable_breakpoint_disp (bpt, disp_disable, 1);
14540 });
14541 });
14542 }
14543
14544 static void
14545 enable_count_command (const char *args, int from_tty)
14546 {
14547 int count;
14548
14549 if (args == NULL)
14550 error_no_arg (_("hit count"));
14551
14552 count = get_number (&args);
14553
14554 map_breakpoint_numbers
14555 (args, [&] (breakpoint *b)
14556 {
14557 iterate_over_related_breakpoints
14558 (b, [&] (breakpoint *bpt)
14559 {
14560 enable_breakpoint_disp (bpt, disp_disable, count);
14561 });
14562 });
14563 }
14564
14565 static void
14566 enable_delete_command (const char *args, int from_tty)
14567 {
14568 map_breakpoint_numbers
14569 (args, [&] (breakpoint *b)
14570 {
14571 iterate_over_related_breakpoints
14572 (b, [&] (breakpoint *bpt)
14573 {
14574 enable_breakpoint_disp (bpt, disp_del, 1);
14575 });
14576 });
14577 }
14578 \f
14579 /* Invalidate last known value of any hardware watchpoint if
14580 the memory which that value represents has been written to by
14581 GDB itself. */
14582
14583 static void
14584 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14585 CORE_ADDR addr, ssize_t len,
14586 const bfd_byte *data)
14587 {
14588 for (breakpoint *bp : all_breakpoints ())
14589 if (bp->enable_state == bp_enabled
14590 && bp->type == bp_hardware_watchpoint)
14591 {
14592 struct watchpoint *wp = (struct watchpoint *) bp;
14593
14594 if (wp->val_valid && wp->val != nullptr)
14595 {
14596 struct bp_location *loc;
14597
14598 for (loc = bp->loc; loc != NULL; loc = loc->next)
14599 if (loc->loc_type == bp_loc_hardware_watchpoint
14600 && loc->address + loc->length > addr
14601 && addr + len > loc->address)
14602 {
14603 wp->val = NULL;
14604 wp->val_valid = false;
14605 }
14606 }
14607 }
14608 }
14609
14610 /* Create and insert a breakpoint for software single step. */
14611
14612 void
14613 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14614 const address_space *aspace,
14615 CORE_ADDR next_pc)
14616 {
14617 struct thread_info *tp = inferior_thread ();
14618 struct symtab_and_line sal;
14619 CORE_ADDR pc = next_pc;
14620
14621 if (tp->control.single_step_breakpoints == NULL)
14622 {
14623 tp->control.single_step_breakpoints
14624 = new_single_step_breakpoint (tp->global_num, gdbarch);
14625 }
14626
14627 sal = find_pc_line (pc, 0);
14628 sal.pc = pc;
14629 sal.section = find_pc_overlay (pc);
14630 sal.explicit_pc = 1;
14631 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14632
14633 update_global_location_list (UGLL_INSERT);
14634 }
14635
14636 /* Insert single step breakpoints according to the current state. */
14637
14638 int
14639 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14640 {
14641 struct regcache *regcache = get_current_regcache ();
14642 std::vector<CORE_ADDR> next_pcs;
14643
14644 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14645
14646 if (!next_pcs.empty ())
14647 {
14648 struct frame_info *frame = get_current_frame ();
14649 const address_space *aspace = get_frame_address_space (frame);
14650
14651 for (CORE_ADDR pc : next_pcs)
14652 insert_single_step_breakpoint (gdbarch, aspace, pc);
14653
14654 return 1;
14655 }
14656 else
14657 return 0;
14658 }
14659
14660 /* See breakpoint.h. */
14661
14662 int
14663 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14664 const address_space *aspace,
14665 CORE_ADDR pc)
14666 {
14667 struct bp_location *loc;
14668
14669 for (loc = bp->loc; loc != NULL; loc = loc->next)
14670 if (loc->inserted
14671 && breakpoint_location_address_match (loc, aspace, pc))
14672 return 1;
14673
14674 return 0;
14675 }
14676
14677 /* Check whether a software single-step breakpoint is inserted at
14678 PC. */
14679
14680 int
14681 single_step_breakpoint_inserted_here_p (const address_space *aspace,
14682 CORE_ADDR pc)
14683 {
14684 for (breakpoint *bpt : all_breakpoints ())
14685 {
14686 if (bpt->type == bp_single_step
14687 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14688 return 1;
14689 }
14690 return 0;
14691 }
14692
14693 /* Tracepoint-specific operations. */
14694
14695 /* Set tracepoint count to NUM. */
14696 static void
14697 set_tracepoint_count (int num)
14698 {
14699 tracepoint_count = num;
14700 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14701 }
14702
14703 static void
14704 trace_command (const char *arg, int from_tty)
14705 {
14706 event_location_up location = string_to_event_location (&arg,
14707 current_language);
14708 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
14709 (location.get (), true /* is_tracepoint */);
14710
14711 create_breakpoint (get_current_arch (),
14712 location.get (),
14713 NULL, 0, arg, false, 1 /* parse arg */,
14714 0 /* tempflag */,
14715 bp_tracepoint /* type_wanted */,
14716 0 /* Ignore count */,
14717 pending_break_support,
14718 ops,
14719 from_tty,
14720 1 /* enabled */,
14721 0 /* internal */, 0);
14722 }
14723
14724 static void
14725 ftrace_command (const char *arg, int from_tty)
14726 {
14727 event_location_up location = string_to_event_location (&arg,
14728 current_language);
14729 create_breakpoint (get_current_arch (),
14730 location.get (),
14731 NULL, 0, arg, false, 1 /* parse arg */,
14732 0 /* tempflag */,
14733 bp_fast_tracepoint /* type_wanted */,
14734 0 /* Ignore count */,
14735 pending_break_support,
14736 &tracepoint_breakpoint_ops,
14737 from_tty,
14738 1 /* enabled */,
14739 0 /* internal */, 0);
14740 }
14741
14742 /* strace command implementation. Creates a static tracepoint. */
14743
14744 static void
14745 strace_command (const char *arg, int from_tty)
14746 {
14747 struct breakpoint_ops *ops;
14748 event_location_up location;
14749
14750 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14751 or with a normal static tracepoint. */
14752 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14753 {
14754 ops = &strace_marker_breakpoint_ops;
14755 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
14756 }
14757 else
14758 {
14759 ops = &tracepoint_breakpoint_ops;
14760 location = string_to_event_location (&arg, current_language);
14761 }
14762
14763 create_breakpoint (get_current_arch (),
14764 location.get (),
14765 NULL, 0, arg, false, 1 /* parse arg */,
14766 0 /* tempflag */,
14767 bp_static_tracepoint /* type_wanted */,
14768 0 /* Ignore count */,
14769 pending_break_support,
14770 ops,
14771 from_tty,
14772 1 /* enabled */,
14773 0 /* internal */, 0);
14774 }
14775
14776 /* Set up a fake reader function that gets command lines from a linked
14777 list that was acquired during tracepoint uploading. */
14778
14779 static struct uploaded_tp *this_utp;
14780 static int next_cmd;
14781
14782 static char *
14783 read_uploaded_action (void)
14784 {
14785 char *rslt = nullptr;
14786
14787 if (next_cmd < this_utp->cmd_strings.size ())
14788 {
14789 rslt = this_utp->cmd_strings[next_cmd].get ();
14790 next_cmd++;
14791 }
14792
14793 return rslt;
14794 }
14795
14796 /* Given information about a tracepoint as recorded on a target (which
14797 can be either a live system or a trace file), attempt to create an
14798 equivalent GDB tracepoint. This is not a reliable process, since
14799 the target does not necessarily have all the information used when
14800 the tracepoint was originally defined. */
14801
14802 struct tracepoint *
14803 create_tracepoint_from_upload (struct uploaded_tp *utp)
14804 {
14805 const char *addr_str;
14806 char small_buf[100];
14807 struct tracepoint *tp;
14808
14809 if (utp->at_string)
14810 addr_str = utp->at_string.get ();
14811 else
14812 {
14813 /* In the absence of a source location, fall back to raw
14814 address. Since there is no way to confirm that the address
14815 means the same thing as when the trace was started, warn the
14816 user. */
14817 warning (_("Uploaded tracepoint %d has no "
14818 "source location, using raw address"),
14819 utp->number);
14820 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14821 addr_str = small_buf;
14822 }
14823
14824 /* There's not much we can do with a sequence of bytecodes. */
14825 if (utp->cond && !utp->cond_string)
14826 warning (_("Uploaded tracepoint %d condition "
14827 "has no source form, ignoring it"),
14828 utp->number);
14829
14830 event_location_up location = string_to_event_location (&addr_str,
14831 current_language);
14832 if (!create_breakpoint (get_current_arch (),
14833 location.get (),
14834 utp->cond_string.get (), -1, addr_str,
14835 false /* force_condition */,
14836 0 /* parse cond/thread */,
14837 0 /* tempflag */,
14838 utp->type /* type_wanted */,
14839 0 /* Ignore count */,
14840 pending_break_support,
14841 &tracepoint_breakpoint_ops,
14842 0 /* from_tty */,
14843 utp->enabled /* enabled */,
14844 0 /* internal */,
14845 CREATE_BREAKPOINT_FLAGS_INSERTED))
14846 return NULL;
14847
14848 /* Get the tracepoint we just created. */
14849 tp = get_tracepoint (tracepoint_count);
14850 gdb_assert (tp != NULL);
14851
14852 if (utp->pass > 0)
14853 {
14854 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14855 tp->number);
14856
14857 trace_pass_command (small_buf, 0);
14858 }
14859
14860 /* If we have uploaded versions of the original commands, set up a
14861 special-purpose "reader" function and call the usual command line
14862 reader, then pass the result to the breakpoint command-setting
14863 function. */
14864 if (!utp->cmd_strings.empty ())
14865 {
14866 counted_command_line cmd_list;
14867
14868 this_utp = utp;
14869 next_cmd = 0;
14870
14871 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
14872
14873 breakpoint_set_commands (tp, std::move (cmd_list));
14874 }
14875 else if (!utp->actions.empty ()
14876 || !utp->step_actions.empty ())
14877 warning (_("Uploaded tracepoint %d actions "
14878 "have no source form, ignoring them"),
14879 utp->number);
14880
14881 /* Copy any status information that might be available. */
14882 tp->hit_count = utp->hit_count;
14883 tp->traceframe_usage = utp->traceframe_usage;
14884
14885 return tp;
14886 }
14887
14888 /* Print information on tracepoint number TPNUM_EXP, or all if
14889 omitted. */
14890
14891 static void
14892 info_tracepoints_command (const char *args, int from_tty)
14893 {
14894 struct ui_out *uiout = current_uiout;
14895 int num_printed;
14896
14897 num_printed = breakpoint_1 (args, false, is_tracepoint);
14898
14899 if (num_printed == 0)
14900 {
14901 if (args == NULL || *args == '\0')
14902 uiout->message ("No tracepoints.\n");
14903 else
14904 uiout->message ("No tracepoint matching '%s'.\n", args);
14905 }
14906
14907 default_collect_info ();
14908 }
14909
14910 /* The 'enable trace' command enables tracepoints.
14911 Not supported by all targets. */
14912 static void
14913 enable_trace_command (const char *args, int from_tty)
14914 {
14915 enable_command (args, from_tty);
14916 }
14917
14918 /* The 'disable trace' command disables tracepoints.
14919 Not supported by all targets. */
14920 static void
14921 disable_trace_command (const char *args, int from_tty)
14922 {
14923 disable_command (args, from_tty);
14924 }
14925
14926 /* Remove a tracepoint (or all if no argument). */
14927 static void
14928 delete_trace_command (const char *arg, int from_tty)
14929 {
14930 struct breakpoint *b, *b_tmp;
14931
14932 dont_repeat ();
14933
14934 if (arg == 0)
14935 {
14936 int breaks_to_delete = 0;
14937
14938 /* Delete all breakpoints if no argument.
14939 Do not delete internal or call-dummy breakpoints, these
14940 have to be deleted with an explicit breakpoint number
14941 argument. */
14942 ALL_TRACEPOINTS (b)
14943 if (is_tracepoint (b) && user_breakpoint_p (b))
14944 {
14945 breaks_to_delete = 1;
14946 break;
14947 }
14948
14949 /* Ask user only if there are some breakpoints to delete. */
14950 if (!from_tty
14951 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14952 {
14953 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14954 if (is_tracepoint (b) && user_breakpoint_p (b))
14955 delete_breakpoint (b);
14956 }
14957 }
14958 else
14959 map_breakpoint_numbers
14960 (arg, [&] (breakpoint *br)
14961 {
14962 iterate_over_related_breakpoints (br, delete_breakpoint);
14963 });
14964 }
14965
14966 /* Helper function for trace_pass_command. */
14967
14968 static void
14969 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14970 {
14971 tp->pass_count = count;
14972 gdb::observers::breakpoint_modified.notify (tp);
14973 if (from_tty)
14974 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
14975 tp->number, count);
14976 }
14977
14978 /* Set passcount for tracepoint.
14979
14980 First command argument is passcount, second is tracepoint number.
14981 If tracepoint number omitted, apply to most recently defined.
14982 Also accepts special argument "all". */
14983
14984 static void
14985 trace_pass_command (const char *args, int from_tty)
14986 {
14987 struct tracepoint *t1;
14988 ULONGEST count;
14989
14990 if (args == 0 || *args == 0)
14991 error (_("passcount command requires an "
14992 "argument (count + optional TP num)"));
14993
14994 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
14995
14996 args = skip_spaces (args);
14997 if (*args && strncasecmp (args, "all", 3) == 0)
14998 {
14999 struct breakpoint *b;
15000
15001 args += 3; /* Skip special argument "all". */
15002 if (*args)
15003 error (_("Junk at end of arguments."));
15004
15005 ALL_TRACEPOINTS (b)
15006 {
15007 t1 = (struct tracepoint *) b;
15008 trace_pass_set_count (t1, count, from_tty);
15009 }
15010 }
15011 else if (*args == '\0')
15012 {
15013 t1 = get_tracepoint_by_number (&args, NULL);
15014 if (t1)
15015 trace_pass_set_count (t1, count, from_tty);
15016 }
15017 else
15018 {
15019 number_or_range_parser parser (args);
15020 while (!parser.finished ())
15021 {
15022 t1 = get_tracepoint_by_number (&args, &parser);
15023 if (t1)
15024 trace_pass_set_count (t1, count, from_tty);
15025 }
15026 }
15027 }
15028
15029 struct tracepoint *
15030 get_tracepoint (int num)
15031 {
15032 struct breakpoint *t;
15033
15034 ALL_TRACEPOINTS (t)
15035 if (t->number == num)
15036 return (struct tracepoint *) t;
15037
15038 return NULL;
15039 }
15040
15041 /* Find the tracepoint with the given target-side number (which may be
15042 different from the tracepoint number after disconnecting and
15043 reconnecting). */
15044
15045 struct tracepoint *
15046 get_tracepoint_by_number_on_target (int num)
15047 {
15048 struct breakpoint *b;
15049
15050 ALL_TRACEPOINTS (b)
15051 {
15052 struct tracepoint *t = (struct tracepoint *) b;
15053
15054 if (t->number_on_target == num)
15055 return t;
15056 }
15057
15058 return NULL;
15059 }
15060
15061 /* Utility: parse a tracepoint number and look it up in the list.
15062 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15063 If the argument is missing, the most recent tracepoint
15064 (tracepoint_count) is returned. */
15065
15066 struct tracepoint *
15067 get_tracepoint_by_number (const char **arg,
15068 number_or_range_parser *parser)
15069 {
15070 struct breakpoint *t;
15071 int tpnum;
15072 const char *instring = arg == NULL ? NULL : *arg;
15073
15074 if (parser != NULL)
15075 {
15076 gdb_assert (!parser->finished ());
15077 tpnum = parser->get_number ();
15078 }
15079 else if (arg == NULL || *arg == NULL || ! **arg)
15080 tpnum = tracepoint_count;
15081 else
15082 tpnum = get_number (arg);
15083
15084 if (tpnum <= 0)
15085 {
15086 if (instring && *instring)
15087 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15088 instring);
15089 else
15090 printf_filtered (_("No previous tracepoint\n"));
15091 return NULL;
15092 }
15093
15094 ALL_TRACEPOINTS (t)
15095 if (t->number == tpnum)
15096 {
15097 return (struct tracepoint *) t;
15098 }
15099
15100 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15101 return NULL;
15102 }
15103
15104 void
15105 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15106 {
15107 if (b->thread != -1)
15108 fprintf_unfiltered (fp, " thread %d", b->thread);
15109
15110 if (b->task != 0)
15111 fprintf_unfiltered (fp, " task %d", b->task);
15112
15113 fprintf_unfiltered (fp, "\n");
15114 }
15115
15116 /* Save information on user settable breakpoints (watchpoints, etc) to
15117 a new script file named FILENAME. If FILTER is non-NULL, call it
15118 on each breakpoint and only include the ones for which it returns
15119 true. */
15120
15121 static void
15122 save_breakpoints (const char *filename, int from_tty,
15123 bool (*filter) (const struct breakpoint *))
15124 {
15125 int any = 0;
15126 int extra_trace_bits = 0;
15127
15128 if (filename == 0 || *filename == 0)
15129 error (_("Argument required (file name in which to save)"));
15130
15131 /* See if we have anything to save. */
15132 for (breakpoint *tp : all_breakpoints ())
15133 {
15134 /* Skip internal and momentary breakpoints. */
15135 if (!user_breakpoint_p (tp))
15136 continue;
15137
15138 /* If we have a filter, only save the breakpoints it accepts. */
15139 if (filter && !filter (tp))
15140 continue;
15141
15142 any = 1;
15143
15144 if (is_tracepoint (tp))
15145 {
15146 extra_trace_bits = 1;
15147
15148 /* We can stop searching. */
15149 break;
15150 }
15151 }
15152
15153 if (!any)
15154 {
15155 warning (_("Nothing to save."));
15156 return;
15157 }
15158
15159 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
15160
15161 stdio_file fp;
15162
15163 if (!fp.open (expanded_filename.get (), "w"))
15164 error (_("Unable to open file '%s' for saving (%s)"),
15165 expanded_filename.get (), safe_strerror (errno));
15166
15167 if (extra_trace_bits)
15168 save_trace_state_variables (&fp);
15169
15170 for (breakpoint *tp : all_breakpoints ())
15171 {
15172 /* Skip internal and momentary breakpoints. */
15173 if (!user_breakpoint_p (tp))
15174 continue;
15175
15176 /* If we have a filter, only save the breakpoints it accepts. */
15177 if (filter && !filter (tp))
15178 continue;
15179
15180 tp->ops->print_recreate (tp, &fp);
15181
15182 /* Note, we can't rely on tp->number for anything, as we can't
15183 assume the recreated breakpoint numbers will match. Use $bpnum
15184 instead. */
15185
15186 if (tp->cond_string)
15187 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15188
15189 if (tp->ignore_count)
15190 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15191
15192 if (tp->type != bp_dprintf && tp->commands)
15193 {
15194 fp.puts (" commands\n");
15195
15196 current_uiout->redirect (&fp);
15197 try
15198 {
15199 print_command_lines (current_uiout, tp->commands.get (), 2);
15200 }
15201 catch (const gdb_exception &ex)
15202 {
15203 current_uiout->redirect (NULL);
15204 throw;
15205 }
15206
15207 current_uiout->redirect (NULL);
15208 fp.puts (" end\n");
15209 }
15210
15211 if (tp->enable_state == bp_disabled)
15212 fp.puts ("disable $bpnum\n");
15213
15214 /* If this is a multi-location breakpoint, check if the locations
15215 should be individually disabled. Watchpoint locations are
15216 special, and not user visible. */
15217 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15218 {
15219 struct bp_location *loc;
15220 int n = 1;
15221
15222 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15223 if (!loc->enabled)
15224 fp.printf ("disable $bpnum.%d\n", n);
15225 }
15226 }
15227
15228 if (extra_trace_bits && *default_collect)
15229 fp.printf ("set default-collect %s\n", default_collect);
15230
15231 if (from_tty)
15232 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
15233 }
15234
15235 /* The `save breakpoints' command. */
15236
15237 static void
15238 save_breakpoints_command (const char *args, int from_tty)
15239 {
15240 save_breakpoints (args, from_tty, NULL);
15241 }
15242
15243 /* The `save tracepoints' command. */
15244
15245 static void
15246 save_tracepoints_command (const char *args, int from_tty)
15247 {
15248 save_breakpoints (args, from_tty, is_tracepoint);
15249 }
15250
15251 /* Create a vector of all tracepoints. */
15252
15253 std::vector<breakpoint *>
15254 all_tracepoints (void)
15255 {
15256 std::vector<breakpoint *> tp_vec;
15257 struct breakpoint *tp;
15258
15259 ALL_TRACEPOINTS (tp)
15260 {
15261 tp_vec.push_back (tp);
15262 }
15263
15264 return tp_vec;
15265 }
15266
15267 \f
15268 /* This help string is used to consolidate all the help string for specifying
15269 locations used by several commands. */
15270
15271 #define LOCATION_HELP_STRING \
15272 "Linespecs are colon-separated lists of location parameters, such as\n\
15273 source filename, function name, label name, and line number.\n\
15274 Example: To specify the start of a label named \"the_top\" in the\n\
15275 function \"fact\" in the file \"factorial.c\", use\n\
15276 \"factorial.c:fact:the_top\".\n\
15277 \n\
15278 Address locations begin with \"*\" and specify an exact address in the\n\
15279 program. Example: To specify the fourth byte past the start function\n\
15280 \"main\", use \"*main + 4\".\n\
15281 \n\
15282 Explicit locations are similar to linespecs but use an option/argument\n\
15283 syntax to specify location parameters.\n\
15284 Example: To specify the start of the label named \"the_top\" in the\n\
15285 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15286 -function fact -label the_top\".\n\
15287 \n\
15288 By default, a specified function is matched against the program's\n\
15289 functions in all scopes. For C++, this means in all namespaces and\n\
15290 classes. For Ada, this means in all packages. E.g., in C++,\n\
15291 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15292 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
15293 specified name as a complete fully-qualified name instead."
15294
15295 /* This help string is used for the break, hbreak, tbreak and thbreak
15296 commands. It is defined as a macro to prevent duplication.
15297 COMMAND should be a string constant containing the name of the
15298 command. */
15299
15300 #define BREAK_ARGS_HELP(command) \
15301 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
15302 \t[-force-condition] [if CONDITION]\n\
15303 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15304 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15305 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15306 `-probe-dtrace' (for a DTrace probe).\n\
15307 LOCATION may be a linespec, address, or explicit location as described\n\
15308 below.\n\
15309 \n\
15310 With no LOCATION, uses current execution address of the selected\n\
15311 stack frame. This is useful for breaking on return to a stack frame.\n\
15312 \n\
15313 THREADNUM is the number from \"info threads\".\n\
15314 CONDITION is a boolean expression.\n\
15315 \n\
15316 With the \"-force-condition\" flag, the condition is defined even when\n\
15317 it is invalid for all current locations.\n\
15318 \n" LOCATION_HELP_STRING "\n\n\
15319 Multiple breakpoints at one place are permitted, and useful if their\n\
15320 conditions are different.\n\
15321 \n\
15322 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15323
15324 /* List of subcommands for "catch". */
15325 static struct cmd_list_element *catch_cmdlist;
15326
15327 /* List of subcommands for "tcatch". */
15328 static struct cmd_list_element *tcatch_cmdlist;
15329
15330 void
15331 add_catch_command (const char *name, const char *docstring,
15332 cmd_const_sfunc_ftype *sfunc,
15333 completer_ftype *completer,
15334 void *user_data_catch,
15335 void *user_data_tcatch)
15336 {
15337 struct cmd_list_element *command;
15338
15339 command = add_cmd (name, class_breakpoint, docstring,
15340 &catch_cmdlist);
15341 set_cmd_sfunc (command, sfunc);
15342 set_cmd_context (command, user_data_catch);
15343 set_cmd_completer (command, completer);
15344
15345 command = add_cmd (name, class_breakpoint, docstring,
15346 &tcatch_cmdlist);
15347 set_cmd_sfunc (command, sfunc);
15348 set_cmd_context (command, user_data_tcatch);
15349 set_cmd_completer (command, completer);
15350 }
15351
15352 struct breakpoint *
15353 iterate_over_breakpoints (gdb::function_view<bool (breakpoint *)> callback)
15354 {
15355 struct breakpoint *b, *b_tmp;
15356
15357 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15358 {
15359 if (callback (b))
15360 return b;
15361 }
15362
15363 return NULL;
15364 }
15365
15366 /* Zero if any of the breakpoint's locations could be a location where
15367 functions have been inlined, nonzero otherwise. */
15368
15369 static int
15370 is_non_inline_function (struct breakpoint *b)
15371 {
15372 /* The shared library event breakpoint is set on the address of a
15373 non-inline function. */
15374 if (b->type == bp_shlib_event)
15375 return 1;
15376
15377 return 0;
15378 }
15379
15380 /* Nonzero if the specified PC cannot be a location where functions
15381 have been inlined. */
15382
15383 int
15384 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
15385 const struct target_waitstatus *ws)
15386 {
15387 struct bp_location *bl;
15388
15389 for (breakpoint *b : all_breakpoints ())
15390 {
15391 if (!is_non_inline_function (b))
15392 continue;
15393
15394 for (bl = b->loc; bl != NULL; bl = bl->next)
15395 {
15396 if (!bl->shlib_disabled
15397 && bpstat_check_location (bl, aspace, pc, ws))
15398 return 1;
15399 }
15400 }
15401
15402 return 0;
15403 }
15404
15405 /* Remove any references to OBJFILE which is going to be freed. */
15406
15407 void
15408 breakpoint_free_objfile (struct objfile *objfile)
15409 {
15410 struct bp_location **locp, *loc;
15411
15412 ALL_BP_LOCATIONS (loc, locp)
15413 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15414 loc->symtab = NULL;
15415 }
15416
15417 void
15418 initialize_breakpoint_ops (void)
15419 {
15420 static int initialized = 0;
15421
15422 struct breakpoint_ops *ops;
15423
15424 if (initialized)
15425 return;
15426 initialized = 1;
15427
15428 /* The breakpoint_ops structure to be inherit by all kinds of
15429 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15430 internal and momentary breakpoints, etc.). */
15431 ops = &bkpt_base_breakpoint_ops;
15432 *ops = base_breakpoint_ops;
15433 ops->re_set = bkpt_re_set;
15434 ops->insert_location = bkpt_insert_location;
15435 ops->remove_location = bkpt_remove_location;
15436 ops->breakpoint_hit = bkpt_breakpoint_hit;
15437 ops->create_sals_from_location = bkpt_create_sals_from_location;
15438 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15439 ops->decode_location = bkpt_decode_location;
15440
15441 /* The breakpoint_ops structure to be used in regular breakpoints. */
15442 ops = &bkpt_breakpoint_ops;
15443 *ops = bkpt_base_breakpoint_ops;
15444 ops->re_set = bkpt_re_set;
15445 ops->resources_needed = bkpt_resources_needed;
15446 ops->print_it = bkpt_print_it;
15447 ops->print_mention = bkpt_print_mention;
15448 ops->print_recreate = bkpt_print_recreate;
15449
15450 /* Ranged breakpoints. */
15451 ops = &ranged_breakpoint_ops;
15452 *ops = bkpt_breakpoint_ops;
15453 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15454 ops->resources_needed = resources_needed_ranged_breakpoint;
15455 ops->print_it = print_it_ranged_breakpoint;
15456 ops->print_one = print_one_ranged_breakpoint;
15457 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15458 ops->print_mention = print_mention_ranged_breakpoint;
15459 ops->print_recreate = print_recreate_ranged_breakpoint;
15460
15461 /* Internal breakpoints. */
15462 ops = &internal_breakpoint_ops;
15463 *ops = bkpt_base_breakpoint_ops;
15464 ops->re_set = internal_bkpt_re_set;
15465 ops->check_status = internal_bkpt_check_status;
15466 ops->print_it = internal_bkpt_print_it;
15467 ops->print_mention = internal_bkpt_print_mention;
15468
15469 /* Momentary breakpoints. */
15470 ops = &momentary_breakpoint_ops;
15471 *ops = bkpt_base_breakpoint_ops;
15472 ops->re_set = momentary_bkpt_re_set;
15473 ops->check_status = momentary_bkpt_check_status;
15474 ops->print_it = momentary_bkpt_print_it;
15475 ops->print_mention = momentary_bkpt_print_mention;
15476
15477 /* Probe breakpoints. */
15478 ops = &bkpt_probe_breakpoint_ops;
15479 *ops = bkpt_breakpoint_ops;
15480 ops->insert_location = bkpt_probe_insert_location;
15481 ops->remove_location = bkpt_probe_remove_location;
15482 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15483 ops->decode_location = bkpt_probe_decode_location;
15484
15485 /* Watchpoints. */
15486 ops = &watchpoint_breakpoint_ops;
15487 *ops = base_breakpoint_ops;
15488 ops->re_set = re_set_watchpoint;
15489 ops->insert_location = insert_watchpoint;
15490 ops->remove_location = remove_watchpoint;
15491 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15492 ops->check_status = check_status_watchpoint;
15493 ops->resources_needed = resources_needed_watchpoint;
15494 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15495 ops->print_it = print_it_watchpoint;
15496 ops->print_mention = print_mention_watchpoint;
15497 ops->print_recreate = print_recreate_watchpoint;
15498 ops->explains_signal = explains_signal_watchpoint;
15499
15500 /* Masked watchpoints. */
15501 ops = &masked_watchpoint_breakpoint_ops;
15502 *ops = watchpoint_breakpoint_ops;
15503 ops->insert_location = insert_masked_watchpoint;
15504 ops->remove_location = remove_masked_watchpoint;
15505 ops->resources_needed = resources_needed_masked_watchpoint;
15506 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15507 ops->print_it = print_it_masked_watchpoint;
15508 ops->print_one_detail = print_one_detail_masked_watchpoint;
15509 ops->print_mention = print_mention_masked_watchpoint;
15510 ops->print_recreate = print_recreate_masked_watchpoint;
15511
15512 /* Tracepoints. */
15513 ops = &tracepoint_breakpoint_ops;
15514 *ops = base_breakpoint_ops;
15515 ops->re_set = tracepoint_re_set;
15516 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15517 ops->print_one_detail = tracepoint_print_one_detail;
15518 ops->print_mention = tracepoint_print_mention;
15519 ops->print_recreate = tracepoint_print_recreate;
15520 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15521 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15522 ops->decode_location = tracepoint_decode_location;
15523
15524 /* Probe tracepoints. */
15525 ops = &tracepoint_probe_breakpoint_ops;
15526 *ops = tracepoint_breakpoint_ops;
15527 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15528 ops->decode_location = tracepoint_probe_decode_location;
15529
15530 /* Static tracepoints with marker (`-m'). */
15531 ops = &strace_marker_breakpoint_ops;
15532 *ops = tracepoint_breakpoint_ops;
15533 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15534 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15535 ops->decode_location = strace_marker_decode_location;
15536
15537 /* Fork catchpoints. */
15538 ops = &catch_fork_breakpoint_ops;
15539 *ops = base_breakpoint_ops;
15540 ops->insert_location = insert_catch_fork;
15541 ops->remove_location = remove_catch_fork;
15542 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15543 ops->print_it = print_it_catch_fork;
15544 ops->print_one = print_one_catch_fork;
15545 ops->print_mention = print_mention_catch_fork;
15546 ops->print_recreate = print_recreate_catch_fork;
15547
15548 /* Vfork catchpoints. */
15549 ops = &catch_vfork_breakpoint_ops;
15550 *ops = base_breakpoint_ops;
15551 ops->insert_location = insert_catch_vfork;
15552 ops->remove_location = remove_catch_vfork;
15553 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15554 ops->print_it = print_it_catch_vfork;
15555 ops->print_one = print_one_catch_vfork;
15556 ops->print_mention = print_mention_catch_vfork;
15557 ops->print_recreate = print_recreate_catch_vfork;
15558
15559 /* Exec catchpoints. */
15560 ops = &catch_exec_breakpoint_ops;
15561 *ops = base_breakpoint_ops;
15562 ops->insert_location = insert_catch_exec;
15563 ops->remove_location = remove_catch_exec;
15564 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15565 ops->print_it = print_it_catch_exec;
15566 ops->print_one = print_one_catch_exec;
15567 ops->print_mention = print_mention_catch_exec;
15568 ops->print_recreate = print_recreate_catch_exec;
15569
15570 /* Solib-related catchpoints. */
15571 ops = &catch_solib_breakpoint_ops;
15572 *ops = base_breakpoint_ops;
15573 ops->insert_location = insert_catch_solib;
15574 ops->remove_location = remove_catch_solib;
15575 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15576 ops->check_status = check_status_catch_solib;
15577 ops->print_it = print_it_catch_solib;
15578 ops->print_one = print_one_catch_solib;
15579 ops->print_mention = print_mention_catch_solib;
15580 ops->print_recreate = print_recreate_catch_solib;
15581
15582 ops = &dprintf_breakpoint_ops;
15583 *ops = bkpt_base_breakpoint_ops;
15584 ops->re_set = dprintf_re_set;
15585 ops->resources_needed = bkpt_resources_needed;
15586 ops->print_it = bkpt_print_it;
15587 ops->print_mention = bkpt_print_mention;
15588 ops->print_recreate = dprintf_print_recreate;
15589 ops->after_condition_true = dprintf_after_condition_true;
15590 ops->breakpoint_hit = dprintf_breakpoint_hit;
15591 }
15592
15593 /* Chain containing all defined "enable breakpoint" subcommands. */
15594
15595 static struct cmd_list_element *enablebreaklist = NULL;
15596
15597 /* See breakpoint.h. */
15598
15599 cmd_list_element *commands_cmd_element = nullptr;
15600
15601 void _initialize_breakpoint ();
15602 void
15603 _initialize_breakpoint ()
15604 {
15605 struct cmd_list_element *c;
15606
15607 initialize_breakpoint_ops ();
15608
15609 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
15610 "breakpoint");
15611 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
15612 "breakpoint");
15613 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
15614 "breakpoint");
15615
15616 breakpoint_chain = 0;
15617 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15618 before a breakpoint is set. */
15619 breakpoint_count = 0;
15620
15621 tracepoint_count = 0;
15622
15623 add_com ("ignore", class_breakpoint, ignore_command, _("\
15624 Set ignore-count of breakpoint number N to COUNT.\n\
15625 Usage is `ignore N COUNT'."));
15626
15627 commands_cmd_element = add_com ("commands", class_breakpoint,
15628 commands_command, _("\
15629 Set commands to be executed when the given breakpoints are hit.\n\
15630 Give a space-separated breakpoint list as argument after \"commands\".\n\
15631 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15632 (e.g. `5-7').\n\
15633 With no argument, the targeted breakpoint is the last one set.\n\
15634 The commands themselves follow starting on the next line.\n\
15635 Type a line containing \"end\" to indicate the end of them.\n\
15636 Give \"silent\" as the first line to make the breakpoint silent;\n\
15637 then no output is printed when it is hit, except what the commands print."));
15638
15639 const auto cc_opts = make_condition_command_options_def_group (nullptr);
15640 static std::string condition_command_help
15641 = gdb::option::build_help (_("\
15642 Specify breakpoint number N to break only if COND is true.\n\
15643 Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
15644 is an expression to be evaluated whenever breakpoint N is reached.\n\
15645 \n\
15646 Options:\n\
15647 %OPTIONS%"), cc_opts);
15648
15649 c = add_com ("condition", class_breakpoint, condition_command,
15650 condition_command_help.c_str ());
15651 set_cmd_completer_handle_brkchars (c, condition_completer);
15652
15653 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15654 Set a temporary breakpoint.\n\
15655 Like \"break\" except the breakpoint is only temporary,\n\
15656 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15657 by using \"enable delete\" on the breakpoint number.\n\
15658 \n"
15659 BREAK_ARGS_HELP ("tbreak")));
15660 set_cmd_completer (c, location_completer);
15661
15662 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15663 Set a hardware assisted breakpoint.\n\
15664 Like \"break\" except the breakpoint requires hardware support,\n\
15665 some target hardware may not have this support.\n\
15666 \n"
15667 BREAK_ARGS_HELP ("hbreak")));
15668 set_cmd_completer (c, location_completer);
15669
15670 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15671 Set a temporary hardware assisted breakpoint.\n\
15672 Like \"hbreak\" except the breakpoint is only temporary,\n\
15673 so it will be deleted when hit.\n\
15674 \n"
15675 BREAK_ARGS_HELP ("thbreak")));
15676 set_cmd_completer (c, location_completer);
15677
15678 cmd_list_element *enable_cmd
15679 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15680 Enable all or some breakpoints.\n\
15681 Usage: enable [BREAKPOINTNUM]...\n\
15682 Give breakpoint numbers (separated by spaces) as arguments.\n\
15683 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15684 This is used to cancel the effect of the \"disable\" command.\n\
15685 With a subcommand you can enable temporarily."),
15686 &enablelist, 1, &cmdlist);
15687
15688 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
15689
15690 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15691 Enable all or some breakpoints.\n\
15692 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
15693 Give breakpoint numbers (separated by spaces) as arguments.\n\
15694 This is used to cancel the effect of the \"disable\" command.\n\
15695 May be abbreviated to simply \"enable\"."),
15696 &enablebreaklist, 1, &enablelist);
15697
15698 add_cmd ("once", no_class, enable_once_command, _("\
15699 Enable some breakpoints for one hit.\n\
15700 Usage: enable breakpoints once BREAKPOINTNUM...\n\
15701 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15702 &enablebreaklist);
15703
15704 add_cmd ("delete", no_class, enable_delete_command, _("\
15705 Enable some breakpoints and delete when hit.\n\
15706 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
15707 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15708 &enablebreaklist);
15709
15710 add_cmd ("count", no_class, enable_count_command, _("\
15711 Enable some breakpoints for COUNT hits.\n\
15712 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
15713 If a breakpoint is hit while enabled in this fashion,\n\
15714 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15715 &enablebreaklist);
15716
15717 add_cmd ("delete", no_class, enable_delete_command, _("\
15718 Enable some breakpoints and delete when hit.\n\
15719 Usage: enable delete BREAKPOINTNUM...\n\
15720 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15721 &enablelist);
15722
15723 add_cmd ("once", no_class, enable_once_command, _("\
15724 Enable some breakpoints for one hit.\n\
15725 Usage: enable once BREAKPOINTNUM...\n\
15726 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15727 &enablelist);
15728
15729 add_cmd ("count", no_class, enable_count_command, _("\
15730 Enable some breakpoints for COUNT hits.\n\
15731 Usage: enable count COUNT BREAKPOINTNUM...\n\
15732 If a breakpoint is hit while enabled in this fashion,\n\
15733 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15734 &enablelist);
15735
15736 cmd_list_element *disable_cmd
15737 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15738 Disable all or some breakpoints.\n\
15739 Usage: disable [BREAKPOINTNUM]...\n\
15740 Arguments are breakpoint numbers with spaces in between.\n\
15741 To disable all breakpoints, give no argument.\n\
15742 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15743 &disablelist, 1, &cmdlist);
15744 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
15745 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
15746
15747 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
15748 Disable all or some breakpoints.\n\
15749 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
15750 Arguments are breakpoint numbers with spaces in between.\n\
15751 To disable all breakpoints, give no argument.\n\
15752 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15753 This command may be abbreviated \"disable\"."),
15754 &disablelist);
15755
15756 cmd_list_element *delete_cmd
15757 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15758 Delete all or some breakpoints.\n\
15759 Usage: delete [BREAKPOINTNUM]...\n\
15760 Arguments are breakpoint numbers with spaces in between.\n\
15761 To delete all breakpoints, give no argument.\n\
15762 \n\
15763 Also a prefix command for deletion of other GDB objects."),
15764 &deletelist, 1, &cmdlist);
15765 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
15766 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
15767
15768 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
15769 Delete all or some breakpoints or auto-display expressions.\n\
15770 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
15771 Arguments are breakpoint numbers with spaces in between.\n\
15772 To delete all breakpoints, give no argument.\n\
15773 This command may be abbreviated \"delete\"."),
15774 &deletelist);
15775
15776 cmd_list_element *clear_cmd
15777 = add_com ("clear", class_breakpoint, clear_command, _("\
15778 Clear breakpoint at specified location.\n\
15779 Argument may be a linespec, explicit, or address location as described below.\n\
15780 \n\
15781 With no argument, clears all breakpoints in the line that the selected frame\n\
15782 is executing in.\n"
15783 "\n" LOCATION_HELP_STRING "\n\n\
15784 See also the \"delete\" command which clears breakpoints by number."));
15785 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
15786
15787 cmd_list_element *break_cmd
15788 = add_com ("break", class_breakpoint, break_command, _("\
15789 Set breakpoint at specified location.\n"
15790 BREAK_ARGS_HELP ("break")));
15791 set_cmd_completer (break_cmd, location_completer);
15792
15793 add_com_alias ("b", break_cmd, class_run, 1);
15794 add_com_alias ("br", break_cmd, class_run, 1);
15795 add_com_alias ("bre", break_cmd, class_run, 1);
15796 add_com_alias ("brea", break_cmd, class_run, 1);
15797
15798 if (dbx_commands)
15799 {
15800 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15801 Break in function/address or break at a line in the current file."),
15802 &stoplist, 1, &cmdlist);
15803 add_cmd ("in", class_breakpoint, stopin_command,
15804 _("Break in function or address."), &stoplist);
15805 add_cmd ("at", class_breakpoint, stopat_command,
15806 _("Break at a line in the current file."), &stoplist);
15807 add_com ("status", class_info, info_breakpoints_command, _("\
15808 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15809 The \"Type\" column indicates one of:\n\
15810 \tbreakpoint - normal breakpoint\n\
15811 \twatchpoint - watchpoint\n\
15812 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15813 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15814 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15815 address and file/line number respectively.\n\
15816 \n\
15817 Convenience variable \"$_\" and default examine address for \"x\"\n\
15818 are set to the address of the last breakpoint listed unless the command\n\
15819 is prefixed with \"server \".\n\n\
15820 Convenience variable \"$bpnum\" contains the number of the last\n\
15821 breakpoint set."));
15822 }
15823
15824 cmd_list_element *info_breakpoints_cmd
15825 = add_info ("breakpoints", info_breakpoints_command, _("\
15826 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15827 The \"Type\" column indicates one of:\n\
15828 \tbreakpoint - normal breakpoint\n\
15829 \twatchpoint - watchpoint\n\
15830 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15831 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15832 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15833 address and file/line number respectively.\n\
15834 \n\
15835 Convenience variable \"$_\" and default examine address for \"x\"\n\
15836 are set to the address of the last breakpoint listed unless the command\n\
15837 is prefixed with \"server \".\n\n\
15838 Convenience variable \"$bpnum\" contains the number of the last\n\
15839 breakpoint set."));
15840
15841 add_info_alias ("b", info_breakpoints_cmd, 1);
15842
15843 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15844 Status of all breakpoints, or breakpoint number NUMBER.\n\
15845 The \"Type\" column indicates one of:\n\
15846 \tbreakpoint - normal breakpoint\n\
15847 \twatchpoint - watchpoint\n\
15848 \tlongjmp - internal breakpoint used to step through longjmp()\n\
15849 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15850 \tuntil - internal breakpoint used by the \"until\" command\n\
15851 \tfinish - internal breakpoint used by the \"finish\" command\n\
15852 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15853 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15854 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15855 address and file/line number respectively.\n\
15856 \n\
15857 Convenience variable \"$_\" and default examine address for \"x\"\n\
15858 are set to the address of the last breakpoint listed unless the command\n\
15859 is prefixed with \"server \".\n\n\
15860 Convenience variable \"$bpnum\" contains the number of the last\n\
15861 breakpoint set."),
15862 &maintenanceinfolist);
15863
15864 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
15865 Set catchpoints to catch events."),
15866 &catch_cmdlist,
15867 0/*allow-unknown*/, &cmdlist);
15868
15869 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
15870 Set temporary catchpoints to catch events."),
15871 &tcatch_cmdlist,
15872 0/*allow-unknown*/, &cmdlist);
15873
15874 add_catch_command ("fork", _("Catch calls to fork."),
15875 catch_fork_command_1,
15876 NULL,
15877 (void *) (uintptr_t) catch_fork_permanent,
15878 (void *) (uintptr_t) catch_fork_temporary);
15879 add_catch_command ("vfork", _("Catch calls to vfork."),
15880 catch_fork_command_1,
15881 NULL,
15882 (void *) (uintptr_t) catch_vfork_permanent,
15883 (void *) (uintptr_t) catch_vfork_temporary);
15884 add_catch_command ("exec", _("Catch calls to exec."),
15885 catch_exec_command_1,
15886 NULL,
15887 CATCH_PERMANENT,
15888 CATCH_TEMPORARY);
15889 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15890 Usage: catch load [REGEX]\n\
15891 If REGEX is given, only stop for libraries matching the regular expression."),
15892 catch_load_command_1,
15893 NULL,
15894 CATCH_PERMANENT,
15895 CATCH_TEMPORARY);
15896 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15897 Usage: catch unload [REGEX]\n\
15898 If REGEX is given, only stop for libraries matching the regular expression."),
15899 catch_unload_command_1,
15900 NULL,
15901 CATCH_PERMANENT,
15902 CATCH_TEMPORARY);
15903
15904 const auto opts = make_watch_options_def_group (nullptr);
15905
15906 static const std::string watch_help = gdb::option::build_help (_("\
15907 Set a watchpoint for EXPRESSION.\n\
15908 Usage: watch [-location] EXPRESSION\n\
15909 \n\
15910 Options:\n\
15911 %OPTIONS%\n\
15912 \n\
15913 A watchpoint stops execution of your program whenever the value of\n\
15914 an expression changes."), opts);
15915 c = add_com ("watch", class_breakpoint, watch_command,
15916 watch_help.c_str ());
15917 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15918
15919 static const std::string rwatch_help = gdb::option::build_help (_("\
15920 Set a read watchpoint for EXPRESSION.\n\
15921 Usage: rwatch [-location] EXPRESSION\n\
15922 \n\
15923 Options:\n\
15924 %OPTIONS%\n\
15925 \n\
15926 A read watchpoint stops execution of your program whenever the value of\n\
15927 an expression is read."), opts);
15928 c = add_com ("rwatch", class_breakpoint, rwatch_command,
15929 rwatch_help.c_str ());
15930 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15931
15932 static const std::string awatch_help = gdb::option::build_help (_("\
15933 Set an access watchpoint for EXPRESSION.\n\
15934 Usage: awatch [-location] EXPRESSION\n\
15935 \n\
15936 Options:\n\
15937 %OPTIONS%\n\
15938 \n\
15939 An access watchpoint stops execution of your program whenever the value\n\
15940 of an expression is either read or written."), opts);
15941 c = add_com ("awatch", class_breakpoint, awatch_command,
15942 awatch_help.c_str ());
15943 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15944
15945 add_info ("watchpoints", info_watchpoints_command, _("\
15946 Status of specified watchpoints (all watchpoints if no argument)."));
15947
15948 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15949 respond to changes - contrary to the description. */
15950 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15951 &can_use_hw_watchpoints, _("\
15952 Set debugger's willingness to use watchpoint hardware."), _("\
15953 Show debugger's willingness to use watchpoint hardware."), _("\
15954 If zero, gdb will not use hardware for new watchpoints, even if\n\
15955 such is available. (However, any hardware watchpoints that were\n\
15956 created before setting this to nonzero, will continue to use watchpoint\n\
15957 hardware.)"),
15958 NULL,
15959 show_can_use_hw_watchpoints,
15960 &setlist, &showlist);
15961
15962 can_use_hw_watchpoints = 1;
15963
15964 /* Tracepoint manipulation commands. */
15965
15966 cmd_list_element *trace_cmd
15967 = add_com ("trace", class_breakpoint, trace_command, _("\
15968 Set a tracepoint at specified location.\n\
15969 \n"
15970 BREAK_ARGS_HELP ("trace") "\n\
15971 Do \"help tracepoints\" for info on other tracepoint commands."));
15972 set_cmd_completer (trace_cmd, location_completer);
15973
15974 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
15975 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
15976 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
15977 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
15978
15979 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
15980 Set a fast tracepoint at specified location.\n\
15981 \n"
15982 BREAK_ARGS_HELP ("ftrace") "\n\
15983 Do \"help tracepoints\" for info on other tracepoint commands."));
15984 set_cmd_completer (c, location_completer);
15985
15986 c = add_com ("strace", class_breakpoint, strace_command, _("\
15987 Set a static tracepoint at location or marker.\n\
15988 \n\
15989 strace [LOCATION] [if CONDITION]\n\
15990 LOCATION may be a linespec, explicit, or address location (described below) \n\
15991 or -m MARKER_ID.\n\n\
15992 If a marker id is specified, probe the marker with that name. With\n\
15993 no LOCATION, uses current execution address of the selected stack frame.\n\
15994 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15995 This collects arbitrary user data passed in the probe point call to the\n\
15996 tracing library. You can inspect it when analyzing the trace buffer,\n\
15997 by printing the $_sdata variable like any other convenience variable.\n\
15998 \n\
15999 CONDITION is a boolean expression.\n\
16000 \n" LOCATION_HELP_STRING "\n\n\
16001 Multiple tracepoints at one place are permitted, and useful if their\n\
16002 conditions are different.\n\
16003 \n\
16004 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16005 Do \"help tracepoints\" for info on other tracepoint commands."));
16006 set_cmd_completer (c, location_completer);
16007
16008 cmd_list_element *info_tracepoints_cmd
16009 = add_info ("tracepoints", info_tracepoints_command, _("\
16010 Status of specified tracepoints (all tracepoints if no argument).\n\
16011 Convenience variable \"$tpnum\" contains the number of the\n\
16012 last tracepoint set."));
16013
16014 add_info_alias ("tp", info_tracepoints_cmd, 1);
16015
16016 cmd_list_element *delete_tracepoints_cmd
16017 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16018 Delete specified tracepoints.\n\
16019 Arguments are tracepoint numbers, separated by spaces.\n\
16020 No argument means delete all tracepoints."),
16021 &deletelist);
16022 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
16023
16024 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16025 Disable specified tracepoints.\n\
16026 Arguments are tracepoint numbers, separated by spaces.\n\
16027 No argument means disable all tracepoints."),
16028 &disablelist);
16029 deprecate_cmd (c, "disable");
16030
16031 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16032 Enable specified tracepoints.\n\
16033 Arguments are tracepoint numbers, separated by spaces.\n\
16034 No argument means enable all tracepoints."),
16035 &enablelist);
16036 deprecate_cmd (c, "enable");
16037
16038 add_com ("passcount", class_trace, trace_pass_command, _("\
16039 Set the passcount for a tracepoint.\n\
16040 The trace will end when the tracepoint has been passed 'count' times.\n\
16041 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16042 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16043
16044 add_basic_prefix_cmd ("save", class_breakpoint,
16045 _("Save breakpoint definitions as a script."),
16046 &save_cmdlist,
16047 0/*allow-unknown*/, &cmdlist);
16048
16049 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16050 Save current breakpoint definitions as a script.\n\
16051 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16052 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16053 session to restore them."),
16054 &save_cmdlist);
16055 set_cmd_completer (c, filename_completer);
16056
16057 cmd_list_element *save_tracepoints_cmd
16058 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16059 Save current tracepoint definitions as a script.\n\
16060 Use the 'source' command in another debug session to restore them."),
16061 &save_cmdlist);
16062 set_cmd_completer (save_tracepoints_cmd, filename_completer);
16063
16064 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
16065 deprecate_cmd (c, "save tracepoints");
16066
16067 add_basic_prefix_cmd ("breakpoint", class_maintenance, _("\
16068 Breakpoint specific settings.\n\
16069 Configure various breakpoint-specific variables such as\n\
16070 pending breakpoint behavior."),
16071 &breakpoint_set_cmdlist,
16072 0/*allow-unknown*/, &setlist);
16073 add_show_prefix_cmd ("breakpoint", class_maintenance, _("\
16074 Breakpoint specific settings.\n\
16075 Configure various breakpoint-specific variables such as\n\
16076 pending breakpoint behavior."),
16077 &breakpoint_show_cmdlist,
16078 0/*allow-unknown*/, &showlist);
16079
16080 add_setshow_auto_boolean_cmd ("pending", no_class,
16081 &pending_break_support, _("\
16082 Set debugger's behavior regarding pending breakpoints."), _("\
16083 Show debugger's behavior regarding pending breakpoints."), _("\
16084 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16085 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16086 an error. If auto, an unrecognized breakpoint location results in a\n\
16087 user-query to see if a pending breakpoint should be created."),
16088 NULL,
16089 show_pending_break_support,
16090 &breakpoint_set_cmdlist,
16091 &breakpoint_show_cmdlist);
16092
16093 pending_break_support = AUTO_BOOLEAN_AUTO;
16094
16095 add_setshow_boolean_cmd ("auto-hw", no_class,
16096 &automatic_hardware_breakpoints, _("\
16097 Set automatic usage of hardware breakpoints."), _("\
16098 Show automatic usage of hardware breakpoints."), _("\
16099 If set, the debugger will automatically use hardware breakpoints for\n\
16100 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16101 a warning will be emitted for such breakpoints."),
16102 NULL,
16103 show_automatic_hardware_breakpoints,
16104 &breakpoint_set_cmdlist,
16105 &breakpoint_show_cmdlist);
16106
16107 add_setshow_boolean_cmd ("always-inserted", class_support,
16108 &always_inserted_mode, _("\
16109 Set mode for inserting breakpoints."), _("\
16110 Show mode for inserting breakpoints."), _("\
16111 When this mode is on, breakpoints are inserted immediately as soon as\n\
16112 they're created, kept inserted even when execution stops, and removed\n\
16113 only when the user deletes them. When this mode is off (the default),\n\
16114 breakpoints are inserted only when execution continues, and removed\n\
16115 when execution stops."),
16116 NULL,
16117 &show_always_inserted_mode,
16118 &breakpoint_set_cmdlist,
16119 &breakpoint_show_cmdlist);
16120
16121 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16122 condition_evaluation_enums,
16123 &condition_evaluation_mode_1, _("\
16124 Set mode of breakpoint condition evaluation."), _("\
16125 Show mode of breakpoint condition evaluation."), _("\
16126 When this is set to \"host\", breakpoint conditions will be\n\
16127 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16128 breakpoint conditions will be downloaded to the target (if the target\n\
16129 supports such feature) and conditions will be evaluated on the target's side.\n\
16130 If this is set to \"auto\" (default), this will be automatically set to\n\
16131 \"target\" if it supports condition evaluation, otherwise it will\n\
16132 be set to \"host\"."),
16133 &set_condition_evaluation_mode,
16134 &show_condition_evaluation_mode,
16135 &breakpoint_set_cmdlist,
16136 &breakpoint_show_cmdlist);
16137
16138 add_com ("break-range", class_breakpoint, break_range_command, _("\
16139 Set a breakpoint for an address range.\n\
16140 break-range START-LOCATION, END-LOCATION\n\
16141 where START-LOCATION and END-LOCATION can be one of the following:\n\
16142 LINENUM, for that line in the current file,\n\
16143 FILE:LINENUM, for that line in that file,\n\
16144 +OFFSET, for that number of lines after the current line\n\
16145 or the start of the range\n\
16146 FUNCTION, for the first line in that function,\n\
16147 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16148 *ADDRESS, for the instruction at that address.\n\
16149 \n\
16150 The breakpoint will stop execution of the inferior whenever it executes\n\
16151 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16152 range (including START-LOCATION and END-LOCATION)."));
16153
16154 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16155 Set a dynamic printf at specified location.\n\
16156 dprintf location,format string,arg1,arg2,...\n\
16157 location may be a linespec, explicit, or address location.\n"
16158 "\n" LOCATION_HELP_STRING));
16159 set_cmd_completer (c, location_completer);
16160
16161 add_setshow_enum_cmd ("dprintf-style", class_support,
16162 dprintf_style_enums, &dprintf_style, _("\
16163 Set the style of usage for dynamic printf."), _("\
16164 Show the style of usage for dynamic printf."), _("\
16165 This setting chooses how GDB will do a dynamic printf.\n\
16166 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16167 console, as with the \"printf\" command.\n\
16168 If the value is \"call\", the print is done by calling a function in your\n\
16169 program; by default printf(), but you can choose a different function or\n\
16170 output stream by setting dprintf-function and dprintf-channel."),
16171 update_dprintf_commands, NULL,
16172 &setlist, &showlist);
16173
16174 dprintf_function = xstrdup ("printf");
16175 add_setshow_string_cmd ("dprintf-function", class_support,
16176 &dprintf_function, _("\
16177 Set the function to use for dynamic printf."), _("\
16178 Show the function to use for dynamic printf."), NULL,
16179 update_dprintf_commands, NULL,
16180 &setlist, &showlist);
16181
16182 dprintf_channel = xstrdup ("");
16183 add_setshow_string_cmd ("dprintf-channel", class_support,
16184 &dprintf_channel, _("\
16185 Set the channel to use for dynamic printf."), _("\
16186 Show the channel to use for dynamic printf."), NULL,
16187 update_dprintf_commands, NULL,
16188 &setlist, &showlist);
16189
16190 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16191 &disconnected_dprintf, _("\
16192 Set whether dprintf continues after GDB disconnects."), _("\
16193 Show whether dprintf continues after GDB disconnects."), _("\
16194 Use this to let dprintf commands continue to hit and produce output\n\
16195 even if GDB disconnects or detaches from the target."),
16196 NULL,
16197 NULL,
16198 &setlist, &showlist);
16199
16200 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16201 Target agent only formatted printing, like the C \"printf\" function.\n\
16202 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
16203 This supports most C printf format specifications, like %s, %d, etc.\n\
16204 This is useful for formatted output in user-defined commands."));
16205
16206 automatic_hardware_breakpoints = true;
16207
16208 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
16209 "breakpoint");
16210 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
16211 "breakpoint");
16212 }