gdb: add all_bp_locations 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 /* Iterates through locations with address ADDRESS for the currently selected
495 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
496 to where the loop should start from.
497 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
498 appropriate location to start with. */
499
500 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
501 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
502 BP_LOCP_TMP = BP_LOCP_START; \
503 BP_LOCP_START \
504 && (BP_LOCP_TMP < bp_locations.data () + bp_locations.size () \
505 && (*BP_LOCP_TMP)->address == ADDRESS); \
506 BP_LOCP_TMP++)
507
508 /* Chains of all breakpoints defined. */
509
510 static struct breakpoint *breakpoint_chain;
511
512 /* Breakpoint linked list range. */
513
514 using breakpoint_range = next_adapter<breakpoint, breakpoint_iterator>;
515
516 /* Return a range to iterate over all breakpoints. */
517
518 static breakpoint_range
519 all_breakpoints ()
520 {
521 return breakpoint_range (breakpoint_chain);
522 }
523
524 /* Breakpoint linked list range, safe against deletion of the current
525 breakpoint while iterating. */
526
527 using breakpoint_safe_range = basic_safe_range<breakpoint_range>;
528
529 /* Return a range to iterate over all breakpoints. This range is safe against
530 deletion of the current breakpoint while iterating. */
531
532 static breakpoint_safe_range
533 all_breakpoints_safe ()
534 {
535 return breakpoint_safe_range (all_breakpoints ());
536 }
537
538 /* See breakpoint.h. */
539
540 tracepoint_range
541 all_tracepoints ()
542 {
543 return tracepoint_range (breakpoint_chain);
544 }
545
546 /* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
547
548 static std::vector<bp_location *> bp_locations;
549
550 static const std::vector<bp_location *> &
551 all_bp_locations ()
552 {
553 return bp_locations;
554 }
555
556 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
557 ADDRESS for the current elements of BP_LOCATIONS which get a valid
558 result from bp_location_has_shadow. You can use it for roughly
559 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
560 an address you need to read. */
561
562 static CORE_ADDR bp_locations_placed_address_before_address_max;
563
564 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
565 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
566 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
567 You can use it for roughly limiting the subrange of BP_LOCATIONS to
568 scan for shadow bytes for an address you need to read. */
569
570 static CORE_ADDR bp_locations_shadow_len_after_address_max;
571
572 /* The locations that no longer correspond to any breakpoint, unlinked
573 from the bp_locations array, but for which a hit may still be
574 reported by a target. */
575 static std::vector<bp_location *> moribund_locations;
576
577 /* Number of last breakpoint made. */
578
579 static int breakpoint_count;
580
581 /* The value of `breakpoint_count' before the last command that
582 created breakpoints. If the last (break-like) command created more
583 than one breakpoint, then the difference between BREAKPOINT_COUNT
584 and PREV_BREAKPOINT_COUNT is more than one. */
585 static int prev_breakpoint_count;
586
587 /* Number of last tracepoint made. */
588
589 static int tracepoint_count;
590
591 static struct cmd_list_element *breakpoint_set_cmdlist;
592 static struct cmd_list_element *breakpoint_show_cmdlist;
593 struct cmd_list_element *save_cmdlist;
594
595 /* See declaration at breakpoint.h. */
596
597 struct breakpoint *
598 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
599 void *user_data)
600 {
601 for (breakpoint *b : all_breakpoints ())
602 if (func (b, user_data) != 0)
603 return b;
604
605 return nullptr;
606 }
607
608 /* Return whether a breakpoint is an active enabled breakpoint. */
609 static int
610 breakpoint_enabled (struct breakpoint *b)
611 {
612 return (b->enable_state == bp_enabled);
613 }
614
615 /* Set breakpoint count to NUM. */
616
617 static void
618 set_breakpoint_count (int num)
619 {
620 prev_breakpoint_count = breakpoint_count;
621 breakpoint_count = num;
622 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
623 }
624
625 /* Used by `start_rbreak_breakpoints' below, to record the current
626 breakpoint count before "rbreak" creates any breakpoint. */
627 static int rbreak_start_breakpoint_count;
628
629 /* Called at the start an "rbreak" command to record the first
630 breakpoint made. */
631
632 scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
633 {
634 rbreak_start_breakpoint_count = breakpoint_count;
635 }
636
637 /* Called at the end of an "rbreak" command to record the last
638 breakpoint made. */
639
640 scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
641 {
642 prev_breakpoint_count = rbreak_start_breakpoint_count;
643 }
644
645 /* Used in run_command to zero the hit count when a new run starts. */
646
647 void
648 clear_breakpoint_hit_counts (void)
649 {
650 for (breakpoint *b : all_breakpoints ())
651 b->hit_count = 0;
652 }
653
654 \f
655 /* Return the breakpoint with the specified number, or NULL
656 if the number does not refer to an existing breakpoint. */
657
658 struct breakpoint *
659 get_breakpoint (int num)
660 {
661 for (breakpoint *b : all_breakpoints ())
662 if (b->number == num)
663 return b;
664
665 return nullptr;
666 }
667
668 \f
669
670 /* Mark locations as "conditions have changed" in case the target supports
671 evaluating conditions on its side. */
672
673 static void
674 mark_breakpoint_modified (struct breakpoint *b)
675 {
676 /* This is only meaningful if the target is
677 evaluating conditions and if the user has
678 opted for condition evaluation on the target's
679 side. */
680 if (gdb_evaluates_breakpoint_condition_p ()
681 || !target_supports_evaluation_of_breakpoint_conditions ())
682 return;
683
684 if (!is_breakpoint (b))
685 return;
686
687 for (bp_location *loc : b->locations ())
688 loc->condition_changed = condition_modified;
689 }
690
691 /* Mark location as "conditions have changed" in case the target supports
692 evaluating conditions on its side. */
693
694 static void
695 mark_breakpoint_location_modified (struct bp_location *loc)
696 {
697 /* This is only meaningful if the target is
698 evaluating conditions and if the user has
699 opted for condition evaluation on the target's
700 side. */
701 if (gdb_evaluates_breakpoint_condition_p ()
702 || !target_supports_evaluation_of_breakpoint_conditions ())
703
704 return;
705
706 if (!is_breakpoint (loc->owner))
707 return;
708
709 loc->condition_changed = condition_modified;
710 }
711
712 /* Sets the condition-evaluation mode using the static global
713 condition_evaluation_mode. */
714
715 static void
716 set_condition_evaluation_mode (const char *args, int from_tty,
717 struct cmd_list_element *c)
718 {
719 const char *old_mode, *new_mode;
720
721 if ((condition_evaluation_mode_1 == condition_evaluation_target)
722 && !target_supports_evaluation_of_breakpoint_conditions ())
723 {
724 condition_evaluation_mode_1 = condition_evaluation_mode;
725 warning (_("Target does not support breakpoint condition evaluation.\n"
726 "Using host evaluation mode instead."));
727 return;
728 }
729
730 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
731 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
732
733 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
734 settings was "auto". */
735 condition_evaluation_mode = condition_evaluation_mode_1;
736
737 /* Only update the mode if the user picked a different one. */
738 if (new_mode != old_mode)
739 {
740 /* If the user switched to a different evaluation mode, we
741 need to synch the changes with the target as follows:
742
743 "host" -> "target": Send all (valid) conditions to the target.
744 "target" -> "host": Remove all the conditions from the target.
745 */
746
747 if (new_mode == condition_evaluation_target)
748 {
749 /* Mark everything modified and synch conditions with the
750 target. */
751 for (bp_location *loc : all_bp_locations ())
752 mark_breakpoint_location_modified (loc);
753 }
754 else
755 {
756 /* Manually mark non-duplicate locations to synch conditions
757 with the target. We do this to remove all the conditions the
758 target knows about. */
759 for (bp_location *loc : all_bp_locations ())
760 if (is_breakpoint (loc->owner) && loc->inserted)
761 loc->needs_update = 1;
762 }
763
764 /* Do the update. */
765 update_global_location_list (UGLL_MAY_INSERT);
766 }
767
768 return;
769 }
770
771 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
772 what "auto" is translating to. */
773
774 static void
775 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
776 struct cmd_list_element *c, const char *value)
777 {
778 if (condition_evaluation_mode == condition_evaluation_auto)
779 fprintf_filtered (file,
780 _("Breakpoint condition evaluation "
781 "mode is %s (currently %s).\n"),
782 value,
783 breakpoint_condition_evaluation_mode ());
784 else
785 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
786 value);
787 }
788
789 /* A comparison function for bp_location AP and BP that is used by
790 bsearch. This comparison function only cares about addresses, unlike
791 the more general bp_location_is_less_than function. */
792
793 static int
794 bp_locations_compare_addrs (const void *ap, const void *bp)
795 {
796 const struct bp_location *a = *(const struct bp_location **) ap;
797 const struct bp_location *b = *(const struct bp_location **) bp;
798
799 if (a->address == b->address)
800 return 0;
801 else
802 return ((a->address > b->address) - (a->address < b->address));
803 }
804
805 /* Helper function to skip all bp_locations with addresses
806 less than ADDRESS. It returns the first bp_location that
807 is greater than or equal to ADDRESS. If none is found, just
808 return NULL. */
809
810 static struct bp_location **
811 get_first_locp_gte_addr (CORE_ADDR address)
812 {
813 struct bp_location dummy_loc;
814 struct bp_location *dummy_locp = &dummy_loc;
815 struct bp_location **locp_found = NULL;
816
817 /* Initialize the dummy location's address field. */
818 dummy_loc.address = address;
819
820 /* Find a close match to the first location at ADDRESS. */
821 locp_found = ((struct bp_location **)
822 bsearch (&dummy_locp, bp_locations.data (), bp_locations.size (),
823 sizeof (struct bp_location **),
824 bp_locations_compare_addrs));
825
826 /* Nothing was found, nothing left to do. */
827 if (locp_found == NULL)
828 return NULL;
829
830 /* We may have found a location that is at ADDRESS but is not the first in the
831 location's list. Go backwards (if possible) and locate the first one. */
832 while ((locp_found - 1) >= bp_locations.data ()
833 && (*(locp_found - 1))->address == address)
834 locp_found--;
835
836 return locp_found;
837 }
838
839 /* Parse COND_STRING in the context of LOC and set as the condition
840 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
841 the number of LOC within its owner. In case of parsing error, mark
842 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
843
844 static void
845 set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
846 int bp_num, int loc_num)
847 {
848 bool has_junk = false;
849 try
850 {
851 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
852 block_for_pc (loc->address), 0);
853 if (*cond_string != 0)
854 has_junk = true;
855 else
856 {
857 loc->cond = std::move (new_exp);
858 if (loc->disabled_by_cond && loc->enabled)
859 printf_filtered (_("Breakpoint %d's condition is now valid at "
860 "location %d, enabling.\n"),
861 bp_num, loc_num);
862
863 loc->disabled_by_cond = false;
864 }
865 }
866 catch (const gdb_exception_error &e)
867 {
868 if (loc->enabled)
869 {
870 /* Warn if a user-enabled location is now becoming disabled-by-cond.
871 BP_NUM is 0 if the breakpoint is being defined for the first
872 time using the "break ... if ..." command, and non-zero if
873 already defined. */
874 if (bp_num != 0)
875 warning (_("failed to validate condition at location %d.%d, "
876 "disabling:\n %s"), bp_num, loc_num, e.what ());
877 else
878 warning (_("failed to validate condition at location %d, "
879 "disabling:\n %s"), loc_num, e.what ());
880 }
881
882 loc->disabled_by_cond = true;
883 }
884
885 if (has_junk)
886 error (_("Garbage '%s' follows condition"), cond_string);
887 }
888
889 void
890 set_breakpoint_condition (struct breakpoint *b, const char *exp,
891 int from_tty, bool force)
892 {
893 if (*exp == 0)
894 {
895 xfree (b->cond_string);
896 b->cond_string = nullptr;
897
898 if (is_watchpoint (b))
899 static_cast<watchpoint *> (b)->cond_exp.reset ();
900 else
901 {
902 int loc_num = 1;
903 for (bp_location *loc : b->locations ())
904 {
905 loc->cond.reset ();
906 if (loc->disabled_by_cond && loc->enabled)
907 printf_filtered (_("Breakpoint %d's condition is now valid at "
908 "location %d, enabling.\n"),
909 b->number, loc_num);
910 loc->disabled_by_cond = false;
911 loc_num++;
912
913 /* No need to free the condition agent expression
914 bytecode (if we have one). We will handle this
915 when we go through update_global_location_list. */
916 }
917 }
918
919 if (from_tty)
920 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
921 }
922 else
923 {
924 if (is_watchpoint (b))
925 {
926 innermost_block_tracker tracker;
927 const char *arg = exp;
928 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
929 if (*arg != 0)
930 error (_("Junk at end of expression"));
931 watchpoint *w = static_cast<watchpoint *> (b);
932 w->cond_exp = std::move (new_exp);
933 w->cond_exp_valid_block = tracker.block ();
934 }
935 else
936 {
937 /* Parse and set condition expressions. We make two passes.
938 In the first, we parse the condition string to see if it
939 is valid in at least one location. If so, the condition
940 would be accepted. So we go ahead and set the locations'
941 conditions. In case no valid case is found, we throw
942 the error and the condition string will be rejected.
943 This two-pass approach is taken to avoid setting the
944 state of locations in case of a reject. */
945 for (bp_location *loc : b->locations ())
946 {
947 try
948 {
949 const char *arg = exp;
950 parse_exp_1 (&arg, loc->address,
951 block_for_pc (loc->address), 0);
952 if (*arg != 0)
953 error (_("Junk at end of expression"));
954 break;
955 }
956 catch (const gdb_exception_error &e)
957 {
958 /* Condition string is invalid. If this happens to
959 be the last loc, abandon (if not forced) or continue
960 (if forced). */
961 if (loc->next == nullptr && !force)
962 throw;
963 }
964 }
965
966 /* If we reach here, the condition is valid at some locations. */
967 int loc_num = 1;
968 for (bp_location *loc : b->locations ())
969 {
970 set_breakpoint_location_condition (exp, loc, b->number, loc_num);
971 loc_num++;
972 }
973 }
974
975 /* We know that the new condition parsed successfully. The
976 condition string of the breakpoint can be safely updated. */
977 xfree (b->cond_string);
978 b->cond_string = xstrdup (exp);
979 b->condition_not_parsed = 0;
980 }
981 mark_breakpoint_modified (b);
982
983 gdb::observers::breakpoint_modified.notify (b);
984 }
985
986 /* See breakpoint.h. */
987
988 void
989 set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
990 bool force)
991 {
992 for (breakpoint *b : all_breakpoints ())
993 if (b->number == bpnum)
994 {
995 /* Check if this breakpoint has a "stop" method implemented in an
996 extension language. This method and conditions entered into GDB
997 from the CLI are mutually exclusive. */
998 const struct extension_language_defn *extlang
999 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1000
1001 if (extlang != NULL)
1002 {
1003 error (_("Only one stop condition allowed. There is currently"
1004 " a %s stop condition defined for this breakpoint."),
1005 ext_lang_capitalized_name (extlang));
1006 }
1007 set_breakpoint_condition (b, exp, from_tty, force);
1008
1009 if (is_breakpoint (b))
1010 update_global_location_list (UGLL_MAY_INSERT);
1011
1012 return;
1013 }
1014
1015 error (_("No breakpoint number %d."), bpnum);
1016 }
1017
1018 /* The options for the "condition" command. */
1019
1020 struct condition_command_opts
1021 {
1022 /* For "-force". */
1023 bool force_condition = false;
1024 };
1025
1026 static const gdb::option::option_def condition_command_option_defs[] = {
1027
1028 gdb::option::flag_option_def<condition_command_opts> {
1029 "force",
1030 [] (condition_command_opts *opts) { return &opts->force_condition; },
1031 N_("Set the condition even if it is invalid for all current locations."),
1032 },
1033
1034 };
1035
1036 /* Create an option_def_group for the "condition" options, with
1037 CC_OPTS as context. */
1038
1039 static inline gdb::option::option_def_group
1040 make_condition_command_options_def_group (condition_command_opts *cc_opts)
1041 {
1042 return {{condition_command_option_defs}, cc_opts};
1043 }
1044
1045 /* Completion for the "condition" command. */
1046
1047 static void
1048 condition_completer (struct cmd_list_element *cmd,
1049 completion_tracker &tracker,
1050 const char *text, const char * /*word*/)
1051 {
1052 bool has_no_arguments = (*text == '\0');
1053 condition_command_opts cc_opts;
1054 const auto group = make_condition_command_options_def_group (&cc_opts);
1055 if (gdb::option::complete_options
1056 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1057 return;
1058
1059 text = skip_spaces (text);
1060 const char *space = skip_to_space (text);
1061 if (*space == '\0')
1062 {
1063 int len;
1064
1065 if (text[0] == '$')
1066 {
1067 tracker.advance_custom_word_point_by (1);
1068 /* We don't support completion of history indices. */
1069 if (!isdigit (text[1]))
1070 complete_internalvar (tracker, &text[1]);
1071 return;
1072 }
1073
1074 /* Suggest the "-force" flag if no arguments are given. If
1075 arguments were passed, they either already include the flag,
1076 or we are beyond the point of suggesting it because it's
1077 positionally the first argument. */
1078 if (has_no_arguments)
1079 gdb::option::complete_on_all_options (tracker, group);
1080
1081 /* We're completing the breakpoint number. */
1082 len = strlen (text);
1083
1084 for (breakpoint *b : all_breakpoints ())
1085 {
1086 char number[50];
1087
1088 xsnprintf (number, sizeof (number), "%d", b->number);
1089
1090 if (strncmp (number, text, len) == 0)
1091 tracker.add_completion (make_unique_xstrdup (number));
1092 }
1093
1094 return;
1095 }
1096
1097 /* We're completing the expression part. Skip the breakpoint num. */
1098 const char *exp_start = skip_spaces (space);
1099 tracker.advance_custom_word_point_by (exp_start - text);
1100 text = exp_start;
1101 const char *word = advance_to_expression_complete_word_point (tracker, text);
1102 expression_completer (cmd, tracker, text, word);
1103 }
1104
1105 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1106
1107 static void
1108 condition_command (const char *arg, int from_tty)
1109 {
1110 const char *p;
1111 int bnum;
1112
1113 if (arg == 0)
1114 error_no_arg (_("breakpoint number"));
1115
1116 p = arg;
1117
1118 /* Check if the "-force" flag was passed. */
1119 condition_command_opts cc_opts;
1120 const auto group = make_condition_command_options_def_group (&cc_opts);
1121 gdb::option::process_options
1122 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
1123
1124 bnum = get_number (&p);
1125 if (bnum == 0)
1126 error (_("Bad breakpoint argument: '%s'"), arg);
1127
1128 set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
1129 }
1130
1131 /* Check that COMMAND do not contain commands that are suitable
1132 only for tracepoints and not suitable for ordinary breakpoints.
1133 Throw if any such commands is found. */
1134
1135 static void
1136 check_no_tracepoint_commands (struct command_line *commands)
1137 {
1138 struct command_line *c;
1139
1140 for (c = commands; c; c = c->next)
1141 {
1142 if (c->control_type == while_stepping_control)
1143 error (_("The 'while-stepping' command can "
1144 "only be used for tracepoints"));
1145
1146 check_no_tracepoint_commands (c->body_list_0.get ());
1147 check_no_tracepoint_commands (c->body_list_1.get ());
1148
1149 /* Not that command parsing removes leading whitespace and comment
1150 lines and also empty lines. So, we only need to check for
1151 command directly. */
1152 if (strstr (c->line, "collect ") == c->line)
1153 error (_("The 'collect' command can only be used for tracepoints"));
1154
1155 if (strstr (c->line, "teval ") == c->line)
1156 error (_("The 'teval' command can only be used for tracepoints"));
1157 }
1158 }
1159
1160 struct longjmp_breakpoint : public breakpoint
1161 {
1162 ~longjmp_breakpoint () override;
1163 };
1164
1165 /* Encapsulate tests for different types of tracepoints. */
1166
1167 static bool
1168 is_tracepoint_type (bptype type)
1169 {
1170 return (type == bp_tracepoint
1171 || type == bp_fast_tracepoint
1172 || type == bp_static_tracepoint);
1173 }
1174
1175 static bool
1176 is_longjmp_type (bptype type)
1177 {
1178 return type == bp_longjmp || type == bp_exception;
1179 }
1180
1181 /* See breakpoint.h. */
1182
1183 bool
1184 is_tracepoint (const struct breakpoint *b)
1185 {
1186 return is_tracepoint_type (b->type);
1187 }
1188
1189 /* Factory function to create an appropriate instance of breakpoint given
1190 TYPE. */
1191
1192 static std::unique_ptr<breakpoint>
1193 new_breakpoint_from_type (bptype type)
1194 {
1195 breakpoint *b;
1196
1197 if (is_tracepoint_type (type))
1198 b = new tracepoint ();
1199 else if (is_longjmp_type (type))
1200 b = new longjmp_breakpoint ();
1201 else
1202 b = new breakpoint ();
1203
1204 return std::unique_ptr<breakpoint> (b);
1205 }
1206
1207 /* A helper function that validates that COMMANDS are valid for a
1208 breakpoint. This function will throw an exception if a problem is
1209 found. */
1210
1211 static void
1212 validate_commands_for_breakpoint (struct breakpoint *b,
1213 struct command_line *commands)
1214 {
1215 if (is_tracepoint (b))
1216 {
1217 struct tracepoint *t = (struct tracepoint *) b;
1218 struct command_line *c;
1219 struct command_line *while_stepping = 0;
1220
1221 /* Reset the while-stepping step count. The previous commands
1222 might have included a while-stepping action, while the new
1223 ones might not. */
1224 t->step_count = 0;
1225
1226 /* We need to verify that each top-level element of commands is
1227 valid for tracepoints, that there's at most one
1228 while-stepping element, and that the while-stepping's body
1229 has valid tracing commands excluding nested while-stepping.
1230 We also need to validate the tracepoint action line in the
1231 context of the tracepoint --- validate_actionline actually
1232 has side effects, like setting the tracepoint's
1233 while-stepping STEP_COUNT, in addition to checking if the
1234 collect/teval actions parse and make sense in the
1235 tracepoint's context. */
1236 for (c = commands; c; c = c->next)
1237 {
1238 if (c->control_type == while_stepping_control)
1239 {
1240 if (b->type == bp_fast_tracepoint)
1241 error (_("The 'while-stepping' command "
1242 "cannot be used for fast tracepoint"));
1243 else if (b->type == bp_static_tracepoint)
1244 error (_("The 'while-stepping' command "
1245 "cannot be used for static tracepoint"));
1246
1247 if (while_stepping)
1248 error (_("The 'while-stepping' command "
1249 "can be used only once"));
1250 else
1251 while_stepping = c;
1252 }
1253
1254 validate_actionline (c->line, b);
1255 }
1256 if (while_stepping)
1257 {
1258 struct command_line *c2;
1259
1260 gdb_assert (while_stepping->body_list_1 == nullptr);
1261 c2 = while_stepping->body_list_0.get ();
1262 for (; c2; c2 = c2->next)
1263 {
1264 if (c2->control_type == while_stepping_control)
1265 error (_("The 'while-stepping' command cannot be nested"));
1266 }
1267 }
1268 }
1269 else
1270 {
1271 check_no_tracepoint_commands (commands);
1272 }
1273 }
1274
1275 /* Return a vector of all the static tracepoints set at ADDR. The
1276 caller is responsible for releasing the vector. */
1277
1278 std::vector<breakpoint *>
1279 static_tracepoints_here (CORE_ADDR addr)
1280 {
1281 std::vector<breakpoint *> found;
1282
1283 for (breakpoint *b : all_breakpoints ())
1284 if (b->type == bp_static_tracepoint)
1285 {
1286 for (bp_location *loc : b->locations ())
1287 if (loc->address == addr)
1288 found.push_back (b);
1289 }
1290
1291 return found;
1292 }
1293
1294 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1295 validate that only allowed commands are included. */
1296
1297 void
1298 breakpoint_set_commands (struct breakpoint *b,
1299 counted_command_line &&commands)
1300 {
1301 validate_commands_for_breakpoint (b, commands.get ());
1302
1303 b->commands = std::move (commands);
1304 gdb::observers::breakpoint_modified.notify (b);
1305 }
1306
1307 /* Set the internal `silent' flag on the breakpoint. Note that this
1308 is not the same as the "silent" that may appear in the breakpoint's
1309 commands. */
1310
1311 void
1312 breakpoint_set_silent (struct breakpoint *b, int silent)
1313 {
1314 int old_silent = b->silent;
1315
1316 b->silent = silent;
1317 if (old_silent != silent)
1318 gdb::observers::breakpoint_modified.notify (b);
1319 }
1320
1321 /* Set the thread for this breakpoint. If THREAD is -1, make the
1322 breakpoint work for any thread. */
1323
1324 void
1325 breakpoint_set_thread (struct breakpoint *b, int thread)
1326 {
1327 int old_thread = b->thread;
1328
1329 b->thread = thread;
1330 if (old_thread != thread)
1331 gdb::observers::breakpoint_modified.notify (b);
1332 }
1333
1334 /* Set the task for this breakpoint. If TASK is 0, make the
1335 breakpoint work for any task. */
1336
1337 void
1338 breakpoint_set_task (struct breakpoint *b, int task)
1339 {
1340 int old_task = b->task;
1341
1342 b->task = task;
1343 if (old_task != task)
1344 gdb::observers::breakpoint_modified.notify (b);
1345 }
1346
1347 static void
1348 commands_command_1 (const char *arg, int from_tty,
1349 struct command_line *control)
1350 {
1351 counted_command_line cmd;
1352 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1353 NULL after the call to read_command_lines if the user provides an empty
1354 list of command by just typing "end". */
1355 bool cmd_read = false;
1356
1357 std::string new_arg;
1358
1359 if (arg == NULL || !*arg)
1360 {
1361 /* Argument not explicitly given. Synthesize it. */
1362 if (breakpoint_count - prev_breakpoint_count > 1)
1363 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1364 breakpoint_count);
1365 else if (breakpoint_count > 0)
1366 new_arg = string_printf ("%d", breakpoint_count);
1367 }
1368 else
1369 {
1370 /* Create a copy of ARG. This is needed because the "commands"
1371 command may be coming from a script. In that case, the read
1372 line buffer is going to be overwritten in the lambda of
1373 'map_breakpoint_numbers' below when reading the next line
1374 before we are are done parsing the breakpoint numbers. */
1375 new_arg = arg;
1376 }
1377 arg = new_arg.c_str ();
1378
1379 map_breakpoint_numbers
1380 (arg, [&] (breakpoint *b)
1381 {
1382 if (!cmd_read)
1383 {
1384 gdb_assert (cmd == NULL);
1385 if (control != NULL)
1386 cmd = control->body_list_0;
1387 else
1388 {
1389 std::string str
1390 = string_printf (_("Type commands for breakpoint(s) "
1391 "%s, one per line."),
1392 arg);
1393
1394 auto do_validate = [=] (const char *line)
1395 {
1396 validate_actionline (line, b);
1397 };
1398 gdb::function_view<void (const char *)> validator;
1399 if (is_tracepoint (b))
1400 validator = do_validate;
1401
1402 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1403 }
1404 cmd_read = true;
1405 }
1406
1407 /* If a breakpoint was on the list more than once, we don't need to
1408 do anything. */
1409 if (b->commands != cmd)
1410 {
1411 validate_commands_for_breakpoint (b, cmd.get ());
1412 b->commands = cmd;
1413 gdb::observers::breakpoint_modified.notify (b);
1414 }
1415 });
1416 }
1417
1418 static void
1419 commands_command (const char *arg, int from_tty)
1420 {
1421 commands_command_1 (arg, from_tty, NULL);
1422 }
1423
1424 /* Like commands_command, but instead of reading the commands from
1425 input stream, takes them from an already parsed command structure.
1426
1427 This is used by cli-script.c to DTRT with breakpoint commands
1428 that are part of if and while bodies. */
1429 enum command_control_type
1430 commands_from_control_command (const char *arg, struct command_line *cmd)
1431 {
1432 commands_command_1 (arg, 0, cmd);
1433 return simple_control;
1434 }
1435
1436 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1437
1438 static int
1439 bp_location_has_shadow (struct bp_location *bl)
1440 {
1441 if (bl->loc_type != bp_loc_software_breakpoint)
1442 return 0;
1443 if (!bl->inserted)
1444 return 0;
1445 if (bl->target_info.shadow_len == 0)
1446 /* BL isn't valid, or doesn't shadow memory. */
1447 return 0;
1448 return 1;
1449 }
1450
1451 /* Update BUF, which is LEN bytes read from the target address
1452 MEMADDR, by replacing a memory breakpoint with its shadowed
1453 contents.
1454
1455 If READBUF is not NULL, this buffer must not overlap with the of
1456 the breakpoint location's shadow_contents buffer. Otherwise, a
1457 failed assertion internal error will be raised. */
1458
1459 static void
1460 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1461 const gdb_byte *writebuf_org,
1462 ULONGEST memaddr, LONGEST len,
1463 struct bp_target_info *target_info,
1464 struct gdbarch *gdbarch)
1465 {
1466 /* Now do full processing of the found relevant range of elements. */
1467 CORE_ADDR bp_addr = 0;
1468 int bp_size = 0;
1469 int bptoffset = 0;
1470
1471 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1472 current_program_space->aspace, 0))
1473 {
1474 /* The breakpoint is inserted in a different address space. */
1475 return;
1476 }
1477
1478 /* Addresses and length of the part of the breakpoint that
1479 we need to copy. */
1480 bp_addr = target_info->placed_address;
1481 bp_size = target_info->shadow_len;
1482
1483 if (bp_addr + bp_size <= memaddr)
1484 {
1485 /* The breakpoint is entirely before the chunk of memory we are
1486 reading. */
1487 return;
1488 }
1489
1490 if (bp_addr >= memaddr + len)
1491 {
1492 /* The breakpoint is entirely after the chunk of memory we are
1493 reading. */
1494 return;
1495 }
1496
1497 /* Offset within shadow_contents. */
1498 if (bp_addr < memaddr)
1499 {
1500 /* Only copy the second part of the breakpoint. */
1501 bp_size -= memaddr - bp_addr;
1502 bptoffset = memaddr - bp_addr;
1503 bp_addr = memaddr;
1504 }
1505
1506 if (bp_addr + bp_size > memaddr + len)
1507 {
1508 /* Only copy the first part of the breakpoint. */
1509 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1510 }
1511
1512 if (readbuf != NULL)
1513 {
1514 /* Verify that the readbuf buffer does not overlap with the
1515 shadow_contents buffer. */
1516 gdb_assert (target_info->shadow_contents >= readbuf + len
1517 || readbuf >= (target_info->shadow_contents
1518 + target_info->shadow_len));
1519
1520 /* Update the read buffer with this inserted breakpoint's
1521 shadow. */
1522 memcpy (readbuf + bp_addr - memaddr,
1523 target_info->shadow_contents + bptoffset, bp_size);
1524 }
1525 else
1526 {
1527 const unsigned char *bp;
1528 CORE_ADDR addr = target_info->reqstd_address;
1529 int placed_size;
1530
1531 /* Update the shadow with what we want to write to memory. */
1532 memcpy (target_info->shadow_contents + bptoffset,
1533 writebuf_org + bp_addr - memaddr, bp_size);
1534
1535 /* Determine appropriate breakpoint contents and size for this
1536 address. */
1537 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1538
1539 /* Update the final write buffer with this inserted
1540 breakpoint's INSN. */
1541 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1542 }
1543 }
1544
1545 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1546 by replacing any memory breakpoints with their shadowed contents.
1547
1548 If READBUF is not NULL, this buffer must not overlap with any of
1549 the breakpoint location's shadow_contents buffers. Otherwise,
1550 a failed assertion internal error will be raised.
1551
1552 The range of shadowed area by each bp_location is:
1553 bl->address - bp_locations_placed_address_before_address_max
1554 up to bl->address + bp_locations_shadow_len_after_address_max
1555 The range we were requested to resolve shadows for is:
1556 memaddr ... memaddr + len
1557 Thus the safe cutoff boundaries for performance optimization are
1558 memaddr + len <= (bl->address
1559 - bp_locations_placed_address_before_address_max)
1560 and:
1561 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1562
1563 void
1564 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1565 const gdb_byte *writebuf_org,
1566 ULONGEST memaddr, LONGEST len)
1567 {
1568 /* Left boundary, right boundary and median element of our binary
1569 search. */
1570 unsigned bc_l, bc_r, bc;
1571
1572 /* Find BC_L which is a leftmost element which may affect BUF
1573 content. It is safe to report lower value but a failure to
1574 report higher one. */
1575
1576 bc_l = 0;
1577 bc_r = bp_locations.size ();
1578 while (bc_l + 1 < bc_r)
1579 {
1580 struct bp_location *bl;
1581
1582 bc = (bc_l + bc_r) / 2;
1583 bl = bp_locations[bc];
1584
1585 /* Check first BL->ADDRESS will not overflow due to the added
1586 constant. Then advance the left boundary only if we are sure
1587 the BC element can in no way affect the BUF content (MEMADDR
1588 to MEMADDR + LEN range).
1589
1590 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1591 offset so that we cannot miss a breakpoint with its shadow
1592 range tail still reaching MEMADDR. */
1593
1594 if ((bl->address + bp_locations_shadow_len_after_address_max
1595 >= bl->address)
1596 && (bl->address + bp_locations_shadow_len_after_address_max
1597 <= memaddr))
1598 bc_l = bc;
1599 else
1600 bc_r = bc;
1601 }
1602
1603 /* Due to the binary search above, we need to make sure we pick the
1604 first location that's at BC_L's address. E.g., if there are
1605 multiple locations at the same address, BC_L may end up pointing
1606 at a duplicate location, and miss the "master"/"inserted"
1607 location. Say, given locations L1, L2 and L3 at addresses A and
1608 B:
1609
1610 L1@A, L2@A, L3@B, ...
1611
1612 BC_L could end up pointing at location L2, while the "master"
1613 location could be L1. Since the `loc->inserted' flag is only set
1614 on "master" locations, we'd forget to restore the shadow of L1
1615 and L2. */
1616 while (bc_l > 0
1617 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1618 bc_l--;
1619
1620 /* Now do full processing of the found relevant range of elements. */
1621
1622 for (bc = bc_l; bc < bp_locations.size (); bc++)
1623 {
1624 struct bp_location *bl = bp_locations[bc];
1625
1626 /* bp_location array has BL->OWNER always non-NULL. */
1627 if (bl->owner->type == bp_none)
1628 warning (_("reading through apparently deleted breakpoint #%d?"),
1629 bl->owner->number);
1630
1631 /* Performance optimization: any further element can no longer affect BUF
1632 content. */
1633
1634 if (bl->address >= bp_locations_placed_address_before_address_max
1635 && memaddr + len <= (bl->address
1636 - bp_locations_placed_address_before_address_max))
1637 break;
1638
1639 if (!bp_location_has_shadow (bl))
1640 continue;
1641
1642 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1643 memaddr, len, &bl->target_info, bl->gdbarch);
1644 }
1645 }
1646
1647 /* See breakpoint.h. */
1648
1649 bool
1650 is_breakpoint (const struct breakpoint *bpt)
1651 {
1652 return (bpt->type == bp_breakpoint
1653 || bpt->type == bp_hardware_breakpoint
1654 || bpt->type == bp_dprintf);
1655 }
1656
1657 /* Return true if BPT is of any hardware watchpoint kind. */
1658
1659 static bool
1660 is_hardware_watchpoint (const struct breakpoint *bpt)
1661 {
1662 return (bpt->type == bp_hardware_watchpoint
1663 || bpt->type == bp_read_watchpoint
1664 || bpt->type == bp_access_watchpoint);
1665 }
1666
1667 /* See breakpoint.h. */
1668
1669 bool
1670 is_watchpoint (const struct breakpoint *bpt)
1671 {
1672 return (is_hardware_watchpoint (bpt)
1673 || bpt->type == bp_watchpoint);
1674 }
1675
1676 /* Returns true if the current thread and its running state are safe
1677 to evaluate or update watchpoint B. Watchpoints on local
1678 expressions need to be evaluated in the context of the thread that
1679 was current when the watchpoint was created, and, that thread needs
1680 to be stopped to be able to select the correct frame context.
1681 Watchpoints on global expressions can be evaluated on any thread,
1682 and in any state. It is presently left to the target allowing
1683 memory accesses when threads are running. */
1684
1685 static int
1686 watchpoint_in_thread_scope (struct watchpoint *b)
1687 {
1688 return (b->pspace == current_program_space
1689 && (b->watchpoint_thread == null_ptid
1690 || (inferior_ptid == b->watchpoint_thread
1691 && !inferior_thread ()->executing)));
1692 }
1693
1694 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1695 associated bp_watchpoint_scope breakpoint. */
1696
1697 static void
1698 watchpoint_del_at_next_stop (struct watchpoint *w)
1699 {
1700 if (w->related_breakpoint != w)
1701 {
1702 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1703 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1704 w->related_breakpoint->disposition = disp_del_at_next_stop;
1705 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1706 w->related_breakpoint = w;
1707 }
1708 w->disposition = disp_del_at_next_stop;
1709 }
1710
1711 /* Extract a bitfield value from value VAL using the bit parameters contained in
1712 watchpoint W. */
1713
1714 static struct value *
1715 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1716 {
1717 struct value *bit_val;
1718
1719 if (val == NULL)
1720 return NULL;
1721
1722 bit_val = allocate_value (value_type (val));
1723
1724 unpack_value_bitfield (bit_val,
1725 w->val_bitpos,
1726 w->val_bitsize,
1727 value_contents_for_printing (val),
1728 value_offset (val),
1729 val);
1730
1731 return bit_val;
1732 }
1733
1734 /* Allocate a dummy location and add it to B, which must be a software
1735 watchpoint. This is required because even if a software watchpoint
1736 is not watching any memory, bpstat_stop_status requires a location
1737 to be able to report stops. */
1738
1739 static void
1740 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1741 struct program_space *pspace)
1742 {
1743 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1744
1745 b->loc = allocate_bp_location (b);
1746 b->loc->pspace = pspace;
1747 b->loc->address = -1;
1748 b->loc->length = -1;
1749 }
1750
1751 /* Returns true if B is a software watchpoint that is not watching any
1752 memory (e.g., "watch $pc"). */
1753
1754 static bool
1755 is_no_memory_software_watchpoint (struct breakpoint *b)
1756 {
1757 return (b->type == bp_watchpoint
1758 && b->loc != NULL
1759 && b->loc->next == NULL
1760 && b->loc->address == -1
1761 && b->loc->length == -1);
1762 }
1763
1764 /* Assuming that B is a watchpoint:
1765 - Reparse watchpoint expression, if REPARSE is non-zero
1766 - Evaluate expression and store the result in B->val
1767 - Evaluate the condition if there is one, and store the result
1768 in b->loc->cond.
1769 - Update the list of values that must be watched in B->loc.
1770
1771 If the watchpoint disposition is disp_del_at_next_stop, then do
1772 nothing. If this is local watchpoint that is out of scope, delete
1773 it.
1774
1775 Even with `set breakpoint always-inserted on' the watchpoints are
1776 removed + inserted on each stop here. Normal breakpoints must
1777 never be removed because they might be missed by a running thread
1778 when debugging in non-stop mode. On the other hand, hardware
1779 watchpoints (is_hardware_watchpoint; processed here) are specific
1780 to each LWP since they are stored in each LWP's hardware debug
1781 registers. Therefore, such LWP must be stopped first in order to
1782 be able to modify its hardware watchpoints.
1783
1784 Hardware watchpoints must be reset exactly once after being
1785 presented to the user. It cannot be done sooner, because it would
1786 reset the data used to present the watchpoint hit to the user. And
1787 it must not be done later because it could display the same single
1788 watchpoint hit during multiple GDB stops. Note that the latter is
1789 relevant only to the hardware watchpoint types bp_read_watchpoint
1790 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1791 not user-visible - its hit is suppressed if the memory content has
1792 not changed.
1793
1794 The following constraints influence the location where we can reset
1795 hardware watchpoints:
1796
1797 * target_stopped_by_watchpoint and target_stopped_data_address are
1798 called several times when GDB stops.
1799
1800 [linux]
1801 * Multiple hardware watchpoints can be hit at the same time,
1802 causing GDB to stop. GDB only presents one hardware watchpoint
1803 hit at a time as the reason for stopping, and all the other hits
1804 are presented later, one after the other, each time the user
1805 requests the execution to be resumed. Execution is not resumed
1806 for the threads still having pending hit event stored in
1807 LWP_INFO->STATUS. While the watchpoint is already removed from
1808 the inferior on the first stop the thread hit event is kept being
1809 reported from its cached value by linux_nat_stopped_data_address
1810 until the real thread resume happens after the watchpoint gets
1811 presented and thus its LWP_INFO->STATUS gets reset.
1812
1813 Therefore the hardware watchpoint hit can get safely reset on the
1814 watchpoint removal from inferior. */
1815
1816 static void
1817 update_watchpoint (struct watchpoint *b, int reparse)
1818 {
1819 int within_current_scope;
1820 struct frame_id saved_frame_id;
1821 int frame_saved;
1822
1823 /* If this is a local watchpoint, we only want to check if the
1824 watchpoint frame is in scope if the current thread is the thread
1825 that was used to create the watchpoint. */
1826 if (!watchpoint_in_thread_scope (b))
1827 return;
1828
1829 if (b->disposition == disp_del_at_next_stop)
1830 return;
1831
1832 frame_saved = 0;
1833
1834 /* Determine if the watchpoint is within scope. */
1835 if (b->exp_valid_block == NULL)
1836 within_current_scope = 1;
1837 else
1838 {
1839 struct frame_info *fi = get_current_frame ();
1840 struct gdbarch *frame_arch = get_frame_arch (fi);
1841 CORE_ADDR frame_pc = get_frame_pc (fi);
1842
1843 /* If we're at a point where the stack has been destroyed
1844 (e.g. in a function epilogue), unwinding may not work
1845 properly. Do not attempt to recreate locations at this
1846 point. See similar comments in watchpoint_check. */
1847 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1848 return;
1849
1850 /* Save the current frame's ID so we can restore it after
1851 evaluating the watchpoint expression on its own frame. */
1852 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1853 took a frame parameter, so that we didn't have to change the
1854 selected frame. */
1855 frame_saved = 1;
1856 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1857
1858 fi = frame_find_by_id (b->watchpoint_frame);
1859 within_current_scope = (fi != NULL);
1860 if (within_current_scope)
1861 select_frame (fi);
1862 }
1863
1864 /* We don't free locations. They are stored in the bp_location array
1865 and update_global_location_list will eventually delete them and
1866 remove breakpoints if needed. */
1867 b->loc = NULL;
1868
1869 if (within_current_scope && reparse)
1870 {
1871 const char *s;
1872
1873 b->exp.reset ();
1874 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1875 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1876 /* If the meaning of expression itself changed, the old value is
1877 no longer relevant. We don't want to report a watchpoint hit
1878 to the user when the old value and the new value may actually
1879 be completely different objects. */
1880 b->val = NULL;
1881 b->val_valid = false;
1882
1883 /* Note that unlike with breakpoints, the watchpoint's condition
1884 expression is stored in the breakpoint object, not in the
1885 locations (re)created below. */
1886 if (b->cond_string != NULL)
1887 {
1888 b->cond_exp.reset ();
1889
1890 s = b->cond_string;
1891 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1892 }
1893 }
1894
1895 /* If we failed to parse the expression, for example because
1896 it refers to a global variable in a not-yet-loaded shared library,
1897 don't try to insert watchpoint. We don't automatically delete
1898 such watchpoint, though, since failure to parse expression
1899 is different from out-of-scope watchpoint. */
1900 if (!target_has_execution ())
1901 {
1902 /* Without execution, memory can't change. No use to try and
1903 set watchpoint locations. The watchpoint will be reset when
1904 the target gains execution, through breakpoint_re_set. */
1905 if (!can_use_hw_watchpoints)
1906 {
1907 if (b->ops->works_in_software_mode (b))
1908 b->type = bp_watchpoint;
1909 else
1910 error (_("Can't set read/access watchpoint when "
1911 "hardware watchpoints are disabled."));
1912 }
1913 }
1914 else if (within_current_scope && b->exp)
1915 {
1916 std::vector<value_ref_ptr> val_chain;
1917 struct value *v, *result;
1918 struct program_space *frame_pspace;
1919
1920 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
1921 &val_chain, false);
1922
1923 /* Avoid setting b->val if it's already set. The meaning of
1924 b->val is 'the last value' user saw, and we should update
1925 it only if we reported that last value to user. As it
1926 happens, the code that reports it updates b->val directly.
1927 We don't keep track of the memory value for masked
1928 watchpoints. */
1929 if (!b->val_valid && !is_masked_watchpoint (b))
1930 {
1931 if (b->val_bitsize != 0)
1932 v = extract_bitfield_from_watchpoint_value (b, v);
1933 b->val = release_value (v);
1934 b->val_valid = true;
1935 }
1936
1937 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1938
1939 /* Look at each value on the value chain. */
1940 gdb_assert (!val_chain.empty ());
1941 for (const value_ref_ptr &iter : val_chain)
1942 {
1943 v = iter.get ();
1944
1945 /* If it's a memory location, and GDB actually needed
1946 its contents to evaluate the expression, then we
1947 must watch it. If the first value returned is
1948 still lazy, that means an error occurred reading it;
1949 watch it anyway in case it becomes readable. */
1950 if (VALUE_LVAL (v) == lval_memory
1951 && (v == val_chain[0] || ! value_lazy (v)))
1952 {
1953 struct type *vtype = check_typedef (value_type (v));
1954
1955 /* We only watch structs and arrays if user asked
1956 for it explicitly, never if they just happen to
1957 appear in the middle of some value chain. */
1958 if (v == result
1959 || (vtype->code () != TYPE_CODE_STRUCT
1960 && vtype->code () != TYPE_CODE_ARRAY))
1961 {
1962 CORE_ADDR addr;
1963 enum target_hw_bp_type type;
1964 struct bp_location *loc, **tmp;
1965 int bitpos = 0, bitsize = 0;
1966
1967 if (value_bitsize (v) != 0)
1968 {
1969 /* Extract the bit parameters out from the bitfield
1970 sub-expression. */
1971 bitpos = value_bitpos (v);
1972 bitsize = value_bitsize (v);
1973 }
1974 else if (v == result && b->val_bitsize != 0)
1975 {
1976 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1977 lvalue whose bit parameters are saved in the fields
1978 VAL_BITPOS and VAL_BITSIZE. */
1979 bitpos = b->val_bitpos;
1980 bitsize = b->val_bitsize;
1981 }
1982
1983 addr = value_address (v);
1984 if (bitsize != 0)
1985 {
1986 /* Skip the bytes that don't contain the bitfield. */
1987 addr += bitpos / 8;
1988 }
1989
1990 type = hw_write;
1991 if (b->type == bp_read_watchpoint)
1992 type = hw_read;
1993 else if (b->type == bp_access_watchpoint)
1994 type = hw_access;
1995
1996 loc = allocate_bp_location (b);
1997 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1998 ;
1999 *tmp = loc;
2000 loc->gdbarch = value_type (v)->arch ();
2001
2002 loc->pspace = frame_pspace;
2003 loc->address = address_significant (loc->gdbarch, addr);
2004
2005 if (bitsize != 0)
2006 {
2007 /* Just cover the bytes that make up the bitfield. */
2008 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2009 }
2010 else
2011 loc->length = TYPE_LENGTH (value_type (v));
2012
2013 loc->watchpoint_type = type;
2014 }
2015 }
2016 }
2017
2018 /* Change the type of breakpoint between hardware assisted or
2019 an ordinary watchpoint depending on the hardware support
2020 and free hardware slots. REPARSE is set when the inferior
2021 is started. */
2022 if (reparse)
2023 {
2024 int reg_cnt;
2025 enum bp_loc_type loc_type;
2026
2027 reg_cnt = can_use_hardware_watchpoint (val_chain);
2028
2029 if (reg_cnt)
2030 {
2031 int i, target_resources_ok, other_type_used;
2032 enum bptype type;
2033
2034 /* Use an exact watchpoint when there's only one memory region to be
2035 watched, and only one debug register is needed to watch it. */
2036 b->exact = target_exact_watchpoints && reg_cnt == 1;
2037
2038 /* We need to determine how many resources are already
2039 used for all other hardware watchpoints plus this one
2040 to see if we still have enough resources to also fit
2041 this watchpoint in as well. */
2042
2043 /* If this is a software watchpoint, we try to turn it
2044 to a hardware one -- count resources as if B was of
2045 hardware watchpoint type. */
2046 type = b->type;
2047 if (type == bp_watchpoint)
2048 type = bp_hardware_watchpoint;
2049
2050 /* This watchpoint may or may not have been placed on
2051 the list yet at this point (it won't be in the list
2052 if we're trying to create it for the first time,
2053 through watch_command), so always account for it
2054 manually. */
2055
2056 /* Count resources used by all watchpoints except B. */
2057 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
2058
2059 /* Add in the resources needed for B. */
2060 i += hw_watchpoint_use_count (b);
2061
2062 target_resources_ok
2063 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2064 if (target_resources_ok <= 0)
2065 {
2066 int sw_mode = b->ops->works_in_software_mode (b);
2067
2068 if (target_resources_ok == 0 && !sw_mode)
2069 error (_("Target does not support this type of "
2070 "hardware watchpoint."));
2071 else if (target_resources_ok < 0 && !sw_mode)
2072 error (_("There are not enough available hardware "
2073 "resources for this watchpoint."));
2074
2075 /* Downgrade to software watchpoint. */
2076 b->type = bp_watchpoint;
2077 }
2078 else
2079 {
2080 /* If this was a software watchpoint, we've just
2081 found we have enough resources to turn it to a
2082 hardware watchpoint. Otherwise, this is a
2083 nop. */
2084 b->type = type;
2085 }
2086 }
2087 else if (!b->ops->works_in_software_mode (b))
2088 {
2089 if (!can_use_hw_watchpoints)
2090 error (_("Can't set read/access watchpoint when "
2091 "hardware watchpoints are disabled."));
2092 else
2093 error (_("Expression cannot be implemented with "
2094 "read/access watchpoint."));
2095 }
2096 else
2097 b->type = bp_watchpoint;
2098
2099 loc_type = (b->type == bp_watchpoint? bp_loc_other
2100 : bp_loc_hardware_watchpoint);
2101 for (bp_location *bl : b->locations ())
2102 bl->loc_type = loc_type;
2103 }
2104
2105 /* If a software watchpoint is not watching any memory, then the
2106 above left it without any location set up. But,
2107 bpstat_stop_status requires a location to be able to report
2108 stops, so make sure there's at least a dummy one. */
2109 if (b->type == bp_watchpoint && b->loc == NULL)
2110 software_watchpoint_add_no_memory_location (b, frame_pspace);
2111 }
2112 else if (!within_current_scope)
2113 {
2114 printf_filtered (_("\
2115 Watchpoint %d deleted because the program has left the block\n\
2116 in which its expression is valid.\n"),
2117 b->number);
2118 watchpoint_del_at_next_stop (b);
2119 }
2120
2121 /* Restore the selected frame. */
2122 if (frame_saved)
2123 select_frame (frame_find_by_id (saved_frame_id));
2124 }
2125
2126
2127 /* Returns 1 iff breakpoint location should be
2128 inserted in the inferior. We don't differentiate the type of BL's owner
2129 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2130 breakpoint_ops is not defined, because in insert_bp_location,
2131 tracepoint's insert_location will not be called. */
2132 static int
2133 should_be_inserted (struct bp_location *bl)
2134 {
2135 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2136 return 0;
2137
2138 if (bl->owner->disposition == disp_del_at_next_stop)
2139 return 0;
2140
2141 if (!bl->enabled || bl->disabled_by_cond
2142 || bl->shlib_disabled || bl->duplicate)
2143 return 0;
2144
2145 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2146 return 0;
2147
2148 /* This is set for example, when we're attached to the parent of a
2149 vfork, and have detached from the child. The child is running
2150 free, and we expect it to do an exec or exit, at which point the
2151 OS makes the parent schedulable again (and the target reports
2152 that the vfork is done). Until the child is done with the shared
2153 memory region, do not insert breakpoints in the parent, otherwise
2154 the child could still trip on the parent's breakpoints. Since
2155 the parent is blocked anyway, it won't miss any breakpoint. */
2156 if (bl->pspace->breakpoints_not_allowed)
2157 return 0;
2158
2159 /* Don't insert a breakpoint if we're trying to step past its
2160 location, except if the breakpoint is a single-step breakpoint,
2161 and the breakpoint's thread is the thread which is stepping past
2162 a breakpoint. */
2163 if ((bl->loc_type == bp_loc_software_breakpoint
2164 || bl->loc_type == bp_loc_hardware_breakpoint)
2165 && stepping_past_instruction_at (bl->pspace->aspace,
2166 bl->address)
2167 /* The single-step breakpoint may be inserted at the location
2168 we're trying to step if the instruction branches to itself.
2169 However, the instruction won't be executed at all and it may
2170 break the semantics of the instruction, for example, the
2171 instruction is a conditional branch or updates some flags.
2172 We can't fix it unless GDB is able to emulate the instruction
2173 or switch to displaced stepping. */
2174 && !(bl->owner->type == bp_single_step
2175 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2176 {
2177 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2178 paddress (bl->gdbarch, bl->address));
2179 return 0;
2180 }
2181
2182 /* Don't insert watchpoints if we're trying to step past the
2183 instruction that triggered one. */
2184 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2185 && stepping_past_nonsteppable_watchpoint ())
2186 {
2187 infrun_debug_printf ("stepping past non-steppable watchpoint. "
2188 "skipping watchpoint at %s:%d",
2189 paddress (bl->gdbarch, bl->address), bl->length);
2190 return 0;
2191 }
2192
2193 return 1;
2194 }
2195
2196 /* Same as should_be_inserted but does the check assuming
2197 that the location is not duplicated. */
2198
2199 static int
2200 unduplicated_should_be_inserted (struct bp_location *bl)
2201 {
2202 int result;
2203 const int save_duplicate = bl->duplicate;
2204
2205 bl->duplicate = 0;
2206 result = should_be_inserted (bl);
2207 bl->duplicate = save_duplicate;
2208 return result;
2209 }
2210
2211 /* Parses a conditional described by an expression COND into an
2212 agent expression bytecode suitable for evaluation
2213 by the bytecode interpreter. Return NULL if there was
2214 any error during parsing. */
2215
2216 static agent_expr_up
2217 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2218 {
2219 if (cond == NULL)
2220 return NULL;
2221
2222 agent_expr_up aexpr;
2223
2224 /* We don't want to stop processing, so catch any errors
2225 that may show up. */
2226 try
2227 {
2228 aexpr = gen_eval_for_expr (scope, cond);
2229 }
2230
2231 catch (const gdb_exception_error &ex)
2232 {
2233 /* If we got here, it means the condition could not be parsed to a valid
2234 bytecode expression and thus can't be evaluated on the target's side.
2235 It's no use iterating through the conditions. */
2236 }
2237
2238 /* We have a valid agent expression. */
2239 return aexpr;
2240 }
2241
2242 /* Based on location BL, create a list of breakpoint conditions to be
2243 passed on to the target. If we have duplicated locations with different
2244 conditions, we will add such conditions to the list. The idea is that the
2245 target will evaluate the list of conditions and will only notify GDB when
2246 one of them is true. */
2247
2248 static void
2249 build_target_condition_list (struct bp_location *bl)
2250 {
2251 struct bp_location **locp = NULL, **loc2p;
2252 int null_condition_or_parse_error = 0;
2253 int modified = bl->needs_update;
2254 struct bp_location *loc;
2255
2256 /* Release conditions left over from a previous insert. */
2257 bl->target_info.conditions.clear ();
2258
2259 /* This is only meaningful if the target is
2260 evaluating conditions and if the user has
2261 opted for condition evaluation on the target's
2262 side. */
2263 if (gdb_evaluates_breakpoint_condition_p ()
2264 || !target_supports_evaluation_of_breakpoint_conditions ())
2265 return;
2266
2267 /* Do a first pass to check for locations with no assigned
2268 conditions or conditions that fail to parse to a valid agent
2269 expression bytecode. If any of these happen, then it's no use to
2270 send conditions to the target since this location will always
2271 trigger and generate a response back to GDB. Note we consider
2272 all locations at the same address irrespective of type, i.e.,
2273 even if the locations aren't considered duplicates (e.g.,
2274 software breakpoint and hardware breakpoint at the same
2275 address). */
2276 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2277 {
2278 loc = (*loc2p);
2279 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2280 {
2281 if (modified)
2282 {
2283 /* Re-parse the conditions since something changed. In that
2284 case we already freed the condition bytecodes (see
2285 force_breakpoint_reinsertion). We just
2286 need to parse the condition to bytecodes again. */
2287 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2288 loc->cond.get ());
2289 }
2290
2291 /* If we have a NULL bytecode expression, it means something
2292 went wrong or we have a null condition expression. */
2293 if (!loc->cond_bytecode)
2294 {
2295 null_condition_or_parse_error = 1;
2296 break;
2297 }
2298 }
2299 }
2300
2301 /* If any of these happened, it means we will have to evaluate the conditions
2302 for the location's address on gdb's side. It is no use keeping bytecodes
2303 for all the other duplicate locations, thus we free all of them here.
2304
2305 This is so we have a finer control over which locations' conditions are
2306 being evaluated by GDB or the remote stub. */
2307 if (null_condition_or_parse_error)
2308 {
2309 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2310 {
2311 loc = (*loc2p);
2312 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2313 {
2314 /* Only go as far as the first NULL bytecode is
2315 located. */
2316 if (!loc->cond_bytecode)
2317 return;
2318
2319 loc->cond_bytecode.reset ();
2320 }
2321 }
2322 }
2323
2324 /* No NULL conditions or failed bytecode generation. Build a
2325 condition list for this location's address. If we have software
2326 and hardware locations at the same address, they aren't
2327 considered duplicates, but we still marge all the conditions
2328 anyway, as it's simpler, and doesn't really make a practical
2329 difference. */
2330 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2331 {
2332 loc = (*loc2p);
2333 if (loc->cond
2334 && is_breakpoint (loc->owner)
2335 && loc->pspace->num == bl->pspace->num
2336 && loc->owner->enable_state == bp_enabled
2337 && loc->enabled
2338 && !loc->disabled_by_cond)
2339 {
2340 /* Add the condition to the vector. This will be used later
2341 to send the conditions to the target. */
2342 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2343 }
2344 }
2345
2346 return;
2347 }
2348
2349 /* Parses a command described by string CMD into an agent expression
2350 bytecode suitable for evaluation by the bytecode interpreter.
2351 Return NULL if there was any error during parsing. */
2352
2353 static agent_expr_up
2354 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2355 {
2356 const char *cmdrest;
2357 const char *format_start, *format_end;
2358 struct gdbarch *gdbarch = get_current_arch ();
2359
2360 if (cmd == NULL)
2361 return NULL;
2362
2363 cmdrest = cmd;
2364
2365 if (*cmdrest == ',')
2366 ++cmdrest;
2367 cmdrest = skip_spaces (cmdrest);
2368
2369 if (*cmdrest++ != '"')
2370 error (_("No format string following the location"));
2371
2372 format_start = cmdrest;
2373
2374 format_pieces fpieces (&cmdrest);
2375
2376 format_end = cmdrest;
2377
2378 if (*cmdrest++ != '"')
2379 error (_("Bad format string, non-terminated '\"'."));
2380
2381 cmdrest = skip_spaces (cmdrest);
2382
2383 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2384 error (_("Invalid argument syntax"));
2385
2386 if (*cmdrest == ',')
2387 cmdrest++;
2388 cmdrest = skip_spaces (cmdrest);
2389
2390 /* For each argument, make an expression. */
2391
2392 std::vector<struct expression *> argvec;
2393 while (*cmdrest != '\0')
2394 {
2395 const char *cmd1;
2396
2397 cmd1 = cmdrest;
2398 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2399 argvec.push_back (expr.release ());
2400 cmdrest = cmd1;
2401 if (*cmdrest == ',')
2402 ++cmdrest;
2403 }
2404
2405 agent_expr_up aexpr;
2406
2407 /* We don't want to stop processing, so catch any errors
2408 that may show up. */
2409 try
2410 {
2411 aexpr = gen_printf (scope, gdbarch, 0, 0,
2412 format_start, format_end - format_start,
2413 argvec.size (), argvec.data ());
2414 }
2415 catch (const gdb_exception_error &ex)
2416 {
2417 /* If we got here, it means the command could not be parsed to a valid
2418 bytecode expression and thus can't be evaluated on the target's side.
2419 It's no use iterating through the other commands. */
2420 }
2421
2422 /* We have a valid agent expression, return it. */
2423 return aexpr;
2424 }
2425
2426 /* Based on location BL, create a list of breakpoint commands to be
2427 passed on to the target. If we have duplicated locations with
2428 different commands, we will add any such to the list. */
2429
2430 static void
2431 build_target_command_list (struct bp_location *bl)
2432 {
2433 struct bp_location **locp = NULL, **loc2p;
2434 int null_command_or_parse_error = 0;
2435 int modified = bl->needs_update;
2436 struct bp_location *loc;
2437
2438 /* Clear commands left over from a previous insert. */
2439 bl->target_info.tcommands.clear ();
2440
2441 if (!target_can_run_breakpoint_commands ())
2442 return;
2443
2444 /* For now, limit to agent-style dprintf breakpoints. */
2445 if (dprintf_style != dprintf_style_agent)
2446 return;
2447
2448 /* For now, if we have any location at the same address that isn't a
2449 dprintf, don't install the target-side commands, as that would
2450 make the breakpoint not be reported to the core, and we'd lose
2451 control. */
2452 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2453 {
2454 loc = (*loc2p);
2455 if (is_breakpoint (loc->owner)
2456 && loc->pspace->num == bl->pspace->num
2457 && loc->owner->type != bp_dprintf)
2458 return;
2459 }
2460
2461 /* Do a first pass to check for locations with no assigned
2462 conditions or conditions that fail to parse to a valid agent expression
2463 bytecode. If any of these happen, then it's no use to send conditions
2464 to the target since this location will always trigger and generate a
2465 response back to GDB. */
2466 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2467 {
2468 loc = (*loc2p);
2469 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2470 {
2471 if (modified)
2472 {
2473 /* Re-parse the commands since something changed. In that
2474 case we already freed the command bytecodes (see
2475 force_breakpoint_reinsertion). We just
2476 need to parse the command to bytecodes again. */
2477 loc->cmd_bytecode
2478 = parse_cmd_to_aexpr (bl->address,
2479 loc->owner->extra_string);
2480 }
2481
2482 /* If we have a NULL bytecode expression, it means something
2483 went wrong or we have a null command expression. */
2484 if (!loc->cmd_bytecode)
2485 {
2486 null_command_or_parse_error = 1;
2487 break;
2488 }
2489 }
2490 }
2491
2492 /* If anything failed, then we're not doing target-side commands,
2493 and so clean up. */
2494 if (null_command_or_parse_error)
2495 {
2496 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2497 {
2498 loc = (*loc2p);
2499 if (is_breakpoint (loc->owner)
2500 && loc->pspace->num == bl->pspace->num)
2501 {
2502 /* Only go as far as the first NULL bytecode is
2503 located. */
2504 if (loc->cmd_bytecode == NULL)
2505 return;
2506
2507 loc->cmd_bytecode.reset ();
2508 }
2509 }
2510 }
2511
2512 /* No NULL commands or failed bytecode generation. Build a command
2513 list for all duplicate locations at this location's address.
2514 Note that here we must care for whether the breakpoint location
2515 types are considered duplicates, otherwise, say, if we have a
2516 software and hardware location at the same address, the target
2517 could end up running the commands twice. For the moment, we only
2518 support targets-side commands with dprintf, but it doesn't hurt
2519 to be pedantically correct in case that changes. */
2520 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2521 {
2522 loc = (*loc2p);
2523 if (breakpoint_locations_match (bl, loc)
2524 && loc->owner->extra_string
2525 && is_breakpoint (loc->owner)
2526 && loc->pspace->num == bl->pspace->num
2527 && loc->owner->enable_state == bp_enabled
2528 && loc->enabled
2529 && !loc->disabled_by_cond)
2530 {
2531 /* Add the command to the vector. This will be used later
2532 to send the commands to the target. */
2533 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2534 }
2535 }
2536
2537 bl->target_info.persist = 0;
2538 /* Maybe flag this location as persistent. */
2539 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2540 bl->target_info.persist = 1;
2541 }
2542
2543 /* Return the kind of breakpoint on address *ADDR. Get the kind
2544 of breakpoint according to ADDR except single-step breakpoint.
2545 Get the kind of single-step breakpoint according to the current
2546 registers state. */
2547
2548 static int
2549 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2550 {
2551 if (bl->owner->type == bp_single_step)
2552 {
2553 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2554 struct regcache *regcache;
2555
2556 regcache = get_thread_regcache (thr);
2557
2558 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2559 regcache, addr);
2560 }
2561 else
2562 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2563 }
2564
2565 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2566 location. Any error messages are printed to TMP_ERROR_STREAM; and
2567 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2568 Returns 0 for success, 1 if the bp_location type is not supported or
2569 -1 for failure.
2570
2571 NOTE drow/2003-09-09: This routine could be broken down to an
2572 object-style method for each breakpoint or catchpoint type. */
2573 static int
2574 insert_bp_location (struct bp_location *bl,
2575 struct ui_file *tmp_error_stream,
2576 int *disabled_breaks,
2577 int *hw_breakpoint_error,
2578 int *hw_bp_error_explained_already)
2579 {
2580 gdb_exception bp_excpt;
2581
2582 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2583 return 0;
2584
2585 /* Note we don't initialize bl->target_info, as that wipes out
2586 the breakpoint location's shadow_contents if the breakpoint
2587 is still inserted at that location. This in turn breaks
2588 target_read_memory which depends on these buffers when
2589 a memory read is requested at the breakpoint location:
2590 Once the target_info has been wiped, we fail to see that
2591 we have a breakpoint inserted at that address and thus
2592 read the breakpoint instead of returning the data saved in
2593 the breakpoint location's shadow contents. */
2594 bl->target_info.reqstd_address = bl->address;
2595 bl->target_info.placed_address_space = bl->pspace->aspace;
2596 bl->target_info.length = bl->length;
2597
2598 /* When working with target-side conditions, we must pass all the conditions
2599 for the same breakpoint address down to the target since GDB will not
2600 insert those locations. With a list of breakpoint conditions, the target
2601 can decide when to stop and notify GDB. */
2602
2603 if (is_breakpoint (bl->owner))
2604 {
2605 build_target_condition_list (bl);
2606 build_target_command_list (bl);
2607 /* Reset the modification marker. */
2608 bl->needs_update = 0;
2609 }
2610
2611 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2612 set at a read-only address, then a breakpoint location will have
2613 been changed to hardware breakpoint before we get here. If it is
2614 "off" however, error out before actually trying to insert the
2615 breakpoint, with a nicer error message. */
2616 if (bl->loc_type == bp_loc_software_breakpoint
2617 && !automatic_hardware_breakpoints)
2618 {
2619 mem_region *mr = lookup_mem_region (bl->address);
2620
2621 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2622 {
2623 fprintf_unfiltered (tmp_error_stream,
2624 _("Cannot insert breakpoint %d.\n"
2625 "Cannot set software breakpoint "
2626 "at read-only address %s\n"),
2627 bl->owner->number,
2628 paddress (bl->gdbarch, bl->address));
2629 return 1;
2630 }
2631 }
2632
2633 if (bl->loc_type == bp_loc_software_breakpoint
2634 || bl->loc_type == bp_loc_hardware_breakpoint)
2635 {
2636 /* First check to see if we have to handle an overlay. */
2637 if (overlay_debugging == ovly_off
2638 || bl->section == NULL
2639 || !(section_is_overlay (bl->section)))
2640 {
2641 /* No overlay handling: just set the breakpoint. */
2642 try
2643 {
2644 int val;
2645
2646 val = bl->owner->ops->insert_location (bl);
2647 if (val)
2648 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2649 }
2650 catch (gdb_exception &e)
2651 {
2652 bp_excpt = std::move (e);
2653 }
2654 }
2655 else
2656 {
2657 /* This breakpoint is in an overlay section.
2658 Shall we set a breakpoint at the LMA? */
2659 if (!overlay_events_enabled)
2660 {
2661 /* Yes -- overlay event support is not active,
2662 so we must try to set a breakpoint at the LMA.
2663 This will not work for a hardware breakpoint. */
2664 if (bl->loc_type == bp_loc_hardware_breakpoint)
2665 warning (_("hardware breakpoint %d not supported in overlay!"),
2666 bl->owner->number);
2667 else
2668 {
2669 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2670 bl->section);
2671 /* Set a software (trap) breakpoint at the LMA. */
2672 bl->overlay_target_info = bl->target_info;
2673 bl->overlay_target_info.reqstd_address = addr;
2674
2675 /* No overlay handling: just set the breakpoint. */
2676 try
2677 {
2678 int val;
2679
2680 bl->overlay_target_info.kind
2681 = breakpoint_kind (bl, &addr);
2682 bl->overlay_target_info.placed_address = addr;
2683 val = target_insert_breakpoint (bl->gdbarch,
2684 &bl->overlay_target_info);
2685 if (val)
2686 bp_excpt
2687 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2688 }
2689 catch (gdb_exception &e)
2690 {
2691 bp_excpt = std::move (e);
2692 }
2693
2694 if (bp_excpt.reason != 0)
2695 fprintf_unfiltered (tmp_error_stream,
2696 "Overlay breakpoint %d "
2697 "failed: in ROM?\n",
2698 bl->owner->number);
2699 }
2700 }
2701 /* Shall we set a breakpoint at the VMA? */
2702 if (section_is_mapped (bl->section))
2703 {
2704 /* Yes. This overlay section is mapped into memory. */
2705 try
2706 {
2707 int val;
2708
2709 val = bl->owner->ops->insert_location (bl);
2710 if (val)
2711 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2712 }
2713 catch (gdb_exception &e)
2714 {
2715 bp_excpt = std::move (e);
2716 }
2717 }
2718 else
2719 {
2720 /* No. This breakpoint will not be inserted.
2721 No error, but do not mark the bp as 'inserted'. */
2722 return 0;
2723 }
2724 }
2725
2726 if (bp_excpt.reason != 0)
2727 {
2728 /* Can't set the breakpoint. */
2729
2730 /* In some cases, we might not be able to insert a
2731 breakpoint in a shared library that has already been
2732 removed, but we have not yet processed the shlib unload
2733 event. Unfortunately, some targets that implement
2734 breakpoint insertion themselves can't tell why the
2735 breakpoint insertion failed (e.g., the remote target
2736 doesn't define error codes), so we must treat generic
2737 errors as memory errors. */
2738 if (bp_excpt.reason == RETURN_ERROR
2739 && (bp_excpt.error == GENERIC_ERROR
2740 || bp_excpt.error == MEMORY_ERROR)
2741 && bl->loc_type == bp_loc_software_breakpoint
2742 && (solib_name_from_address (bl->pspace, bl->address)
2743 || shared_objfile_contains_address_p (bl->pspace,
2744 bl->address)))
2745 {
2746 /* See also: disable_breakpoints_in_shlibs. */
2747 bl->shlib_disabled = 1;
2748 gdb::observers::breakpoint_modified.notify (bl->owner);
2749 if (!*disabled_breaks)
2750 {
2751 fprintf_unfiltered (tmp_error_stream,
2752 "Cannot insert breakpoint %d.\n",
2753 bl->owner->number);
2754 fprintf_unfiltered (tmp_error_stream,
2755 "Temporarily disabling shared "
2756 "library breakpoints:\n");
2757 }
2758 *disabled_breaks = 1;
2759 fprintf_unfiltered (tmp_error_stream,
2760 "breakpoint #%d\n", bl->owner->number);
2761 return 0;
2762 }
2763 else
2764 {
2765 if (bl->loc_type == bp_loc_hardware_breakpoint)
2766 {
2767 *hw_breakpoint_error = 1;
2768 *hw_bp_error_explained_already = bp_excpt.message != NULL;
2769 fprintf_unfiltered (tmp_error_stream,
2770 "Cannot insert hardware breakpoint %d%s",
2771 bl->owner->number,
2772 bp_excpt.message ? ":" : ".\n");
2773 if (bp_excpt.message != NULL)
2774 fprintf_unfiltered (tmp_error_stream, "%s.\n",
2775 bp_excpt.what ());
2776 }
2777 else
2778 {
2779 if (bp_excpt.message == NULL)
2780 {
2781 std::string message
2782 = memory_error_message (TARGET_XFER_E_IO,
2783 bl->gdbarch, bl->address);
2784
2785 fprintf_unfiltered (tmp_error_stream,
2786 "Cannot insert breakpoint %d.\n"
2787 "%s\n",
2788 bl->owner->number, message.c_str ());
2789 }
2790 else
2791 {
2792 fprintf_unfiltered (tmp_error_stream,
2793 "Cannot insert breakpoint %d: %s\n",
2794 bl->owner->number,
2795 bp_excpt.what ());
2796 }
2797 }
2798 return 1;
2799
2800 }
2801 }
2802 else
2803 bl->inserted = 1;
2804
2805 return 0;
2806 }
2807
2808 else if (bl->loc_type == bp_loc_hardware_watchpoint
2809 /* NOTE drow/2003-09-08: This state only exists for removing
2810 watchpoints. It's not clear that it's necessary... */
2811 && bl->owner->disposition != disp_del_at_next_stop)
2812 {
2813 int val;
2814
2815 gdb_assert (bl->owner->ops != NULL
2816 && bl->owner->ops->insert_location != NULL);
2817
2818 val = bl->owner->ops->insert_location (bl);
2819
2820 /* If trying to set a read-watchpoint, and it turns out it's not
2821 supported, try emulating one with an access watchpoint. */
2822 if (val == 1 && bl->watchpoint_type == hw_read)
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 for (bp_location *loc : all_bp_locations ())
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 /* Remove any breakpoint that was set through this program space. */
2893 for (breakpoint *b : all_breakpoints_safe ())
2894 if (b->pspace == pspace)
2895 delete_breakpoint (b);
2896
2897 /* Breakpoints set through other program spaces could have locations
2898 bound to PSPACE as well. Remove those. */
2899 for (bp_location *loc : all_bp_locations ())
2900 {
2901 struct bp_location *tmp;
2902
2903 if (loc->pspace == pspace)
2904 {
2905 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2906 if (loc->owner->loc == loc)
2907 loc->owner->loc = loc->next;
2908 else
2909 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2910 if (tmp->next == loc)
2911 {
2912 tmp->next = loc->next;
2913 break;
2914 }
2915 }
2916 }
2917
2918 /* Now update the global location list to permanently delete the
2919 removed locations above. */
2920 update_global_location_list (UGLL_DONT_INSERT);
2921 }
2922
2923 /* Make sure all breakpoints are inserted in inferior.
2924 Throws exception on any error.
2925 A breakpoint that is already inserted won't be inserted
2926 again, so calling this function twice is safe. */
2927 void
2928 insert_breakpoints (void)
2929 {
2930 for (breakpoint *bpt : all_breakpoints ())
2931 if (is_hardware_watchpoint (bpt))
2932 {
2933 struct watchpoint *w = (struct watchpoint *) bpt;
2934
2935 update_watchpoint (w, 0 /* don't reparse. */);
2936 }
2937
2938 /* Updating watchpoints creates new locations, so update the global
2939 location list. Explicitly tell ugll to insert locations and
2940 ignore breakpoints_always_inserted_mode. Also,
2941 update_global_location_list tries to "upgrade" software
2942 breakpoints to hardware breakpoints to handle "set breakpoint
2943 auto-hw", so we need to call it even if we don't have new
2944 locations. */
2945 update_global_location_list (UGLL_INSERT);
2946 }
2947
2948 /* Invoke CALLBACK for each of bp_location. */
2949
2950 void
2951 iterate_over_bp_locations (gdb::function_view<void (bp_location *)> callback)
2952 {
2953 for (bp_location *loc : all_bp_locations ())
2954 callback (loc);
2955 }
2956
2957 /* This is used when we need to synch breakpoint conditions between GDB and the
2958 target. It is the case with deleting and disabling of breakpoints when using
2959 always-inserted mode. */
2960
2961 static void
2962 update_inserted_breakpoint_locations (void)
2963 {
2964 int error_flag = 0;
2965 int val = 0;
2966 int disabled_breaks = 0;
2967 int hw_breakpoint_error = 0;
2968 int hw_bp_details_reported = 0;
2969
2970 string_file tmp_error_stream;
2971
2972 /* Explicitly mark the warning -- this will only be printed if
2973 there was an error. */
2974 tmp_error_stream.puts ("Warning:\n");
2975
2976 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2977
2978 for (bp_location *bl : all_bp_locations ())
2979 {
2980 /* We only want to update software breakpoints and hardware
2981 breakpoints. */
2982 if (!is_breakpoint (bl->owner))
2983 continue;
2984
2985 /* We only want to update locations that are already inserted
2986 and need updating. This is to avoid unwanted insertion during
2987 deletion of breakpoints. */
2988 if (!bl->inserted || !bl->needs_update)
2989 continue;
2990
2991 switch_to_program_space_and_thread (bl->pspace);
2992
2993 /* For targets that support global breakpoints, there's no need
2994 to select an inferior to insert breakpoint to. In fact, even
2995 if we aren't attached to any process yet, we should still
2996 insert breakpoints. */
2997 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2998 && (inferior_ptid == null_ptid || !target_has_execution ()))
2999 continue;
3000
3001 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3002 &hw_breakpoint_error, &hw_bp_details_reported);
3003 if (val)
3004 error_flag = val;
3005 }
3006
3007 if (error_flag)
3008 {
3009 target_terminal::ours_for_output ();
3010 error_stream (tmp_error_stream);
3011 }
3012 }
3013
3014 /* Used when starting or continuing the program. */
3015
3016 static void
3017 insert_breakpoint_locations (void)
3018 {
3019 int error_flag = 0;
3020 int val = 0;
3021 int disabled_breaks = 0;
3022 int hw_breakpoint_error = 0;
3023 int hw_bp_error_explained_already = 0;
3024
3025 string_file tmp_error_stream;
3026
3027 /* Explicitly mark the warning -- this will only be printed if
3028 there was an error. */
3029 tmp_error_stream.puts ("Warning:\n");
3030
3031 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3032
3033 for (bp_location *bl : all_bp_locations ())
3034 {
3035 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3036 continue;
3037
3038 /* There is no point inserting thread-specific breakpoints if
3039 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3040 has BL->OWNER always non-NULL. */
3041 if (bl->owner->thread != -1
3042 && !valid_global_thread_id (bl->owner->thread))
3043 continue;
3044
3045 switch_to_program_space_and_thread (bl->pspace);
3046
3047 /* For targets that support global breakpoints, there's no need
3048 to select an inferior to insert breakpoint to. In fact, even
3049 if we aren't attached to any process yet, we should still
3050 insert breakpoints. */
3051 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3052 && (inferior_ptid == null_ptid || !target_has_execution ()))
3053 continue;
3054
3055 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3056 &hw_breakpoint_error, &hw_bp_error_explained_already);
3057 if (val)
3058 error_flag = val;
3059 }
3060
3061 /* If we failed to insert all locations of a watchpoint, remove
3062 them, as half-inserted watchpoint is of limited use. */
3063 for (breakpoint *bpt : all_breakpoints ())
3064 {
3065 int some_failed = 0;
3066
3067 if (!is_hardware_watchpoint (bpt))
3068 continue;
3069
3070 if (!breakpoint_enabled (bpt))
3071 continue;
3072
3073 if (bpt->disposition == disp_del_at_next_stop)
3074 continue;
3075
3076 for (bp_location *loc : bpt->locations ())
3077 if (!loc->inserted && should_be_inserted (loc))
3078 {
3079 some_failed = 1;
3080 break;
3081 }
3082
3083 if (some_failed)
3084 {
3085 for (bp_location *loc : bpt->locations ())
3086 if (loc->inserted)
3087 remove_breakpoint (loc);
3088
3089 hw_breakpoint_error = 1;
3090 tmp_error_stream.printf ("Could not insert "
3091 "hardware watchpoint %d.\n",
3092 bpt->number);
3093 error_flag = -1;
3094 }
3095 }
3096
3097 if (error_flag)
3098 {
3099 /* If a hardware breakpoint or watchpoint was inserted, add a
3100 message about possibly exhausted resources. */
3101 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3102 {
3103 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3104 You may have requested too many hardware breakpoints/watchpoints.\n");
3105 }
3106 target_terminal::ours_for_output ();
3107 error_stream (tmp_error_stream);
3108 }
3109 }
3110
3111 /* Used when the program stops.
3112 Returns zero if successful, or non-zero if there was a problem
3113 removing a breakpoint location. */
3114
3115 int
3116 remove_breakpoints (void)
3117 {
3118 int val = 0;
3119
3120 for (bp_location *bl : all_bp_locations ())
3121 {
3122 if (bl->inserted && !is_tracepoint (bl->owner))
3123 val |= remove_breakpoint (bl);
3124 }
3125 return val;
3126 }
3127
3128 /* When a thread exits, remove breakpoints that are related to
3129 that thread. */
3130
3131 static void
3132 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3133 {
3134 for (breakpoint *b : all_breakpoints_safe ())
3135 {
3136 if (b->thread == tp->global_num && user_breakpoint_p (b))
3137 {
3138 b->disposition = disp_del_at_next_stop;
3139
3140 printf_filtered (_("\
3141 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3142 b->number, print_thread_id (tp));
3143
3144 /* Hide it from the user. */
3145 b->number = 0;
3146 }
3147 }
3148 }
3149
3150 /* See breakpoint.h. */
3151
3152 void
3153 remove_breakpoints_inf (inferior *inf)
3154 {
3155 int val;
3156
3157 for (bp_location *bl : all_bp_locations ())
3158 {
3159 if (bl->pspace != inf->pspace)
3160 continue;
3161
3162 if (bl->inserted && !bl->target_info.persist)
3163 {
3164 val = remove_breakpoint (bl);
3165 if (val != 0)
3166 return;
3167 }
3168 }
3169 }
3170
3171 static int internal_breakpoint_number = -1;
3172
3173 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3174 If INTERNAL is non-zero, the breakpoint number will be populated
3175 from internal_breakpoint_number and that variable decremented.
3176 Otherwise the breakpoint number will be populated from
3177 breakpoint_count and that value incremented. Internal breakpoints
3178 do not set the internal var bpnum. */
3179 static void
3180 set_breakpoint_number (int internal, struct breakpoint *b)
3181 {
3182 if (internal)
3183 b->number = internal_breakpoint_number--;
3184 else
3185 {
3186 set_breakpoint_count (breakpoint_count + 1);
3187 b->number = breakpoint_count;
3188 }
3189 }
3190
3191 static struct breakpoint *
3192 create_internal_breakpoint (struct gdbarch *gdbarch,
3193 CORE_ADDR address, enum bptype type,
3194 const struct breakpoint_ops *ops)
3195 {
3196 symtab_and_line sal;
3197 sal.pc = address;
3198 sal.section = find_pc_overlay (sal.pc);
3199 sal.pspace = current_program_space;
3200
3201 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
3202 b->number = internal_breakpoint_number--;
3203 b->disposition = disp_donttouch;
3204
3205 return b;
3206 }
3207
3208 static const char *const longjmp_names[] =
3209 {
3210 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3211 };
3212 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3213
3214 /* Per-objfile data private to breakpoint.c. */
3215 struct breakpoint_objfile_data
3216 {
3217 /* Minimal symbol for "_ovly_debug_event" (if any). */
3218 struct bound_minimal_symbol overlay_msym {};
3219
3220 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3221 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
3222
3223 /* True if we have looked for longjmp probes. */
3224 int longjmp_searched = 0;
3225
3226 /* SystemTap probe points for longjmp (if any). These are non-owning
3227 references. */
3228 std::vector<probe *> longjmp_probes;
3229
3230 /* Minimal symbol for "std::terminate()" (if any). */
3231 struct bound_minimal_symbol terminate_msym {};
3232
3233 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3234 struct bound_minimal_symbol exception_msym {};
3235
3236 /* True if we have looked for exception probes. */
3237 int exception_searched = 0;
3238
3239 /* SystemTap probe points for unwinding (if any). These are non-owning
3240 references. */
3241 std::vector<probe *> exception_probes;
3242 };
3243
3244 static const struct objfile_key<breakpoint_objfile_data>
3245 breakpoint_objfile_key;
3246
3247 /* Minimal symbol not found sentinel. */
3248 static struct minimal_symbol msym_not_found;
3249
3250 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3251
3252 static int
3253 msym_not_found_p (const struct minimal_symbol *msym)
3254 {
3255 return msym == &msym_not_found;
3256 }
3257
3258 /* Return per-objfile data needed by breakpoint.c.
3259 Allocate the data if necessary. */
3260
3261 static struct breakpoint_objfile_data *
3262 get_breakpoint_objfile_data (struct objfile *objfile)
3263 {
3264 struct breakpoint_objfile_data *bp_objfile_data;
3265
3266 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3267 if (bp_objfile_data == NULL)
3268 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3269 return bp_objfile_data;
3270 }
3271
3272 static void
3273 create_overlay_event_breakpoint (void)
3274 {
3275 const char *const func_name = "_ovly_debug_event";
3276
3277 for (objfile *objfile : current_program_space->objfiles ())
3278 {
3279 struct breakpoint *b;
3280 struct breakpoint_objfile_data *bp_objfile_data;
3281 CORE_ADDR addr;
3282 struct explicit_location explicit_loc;
3283
3284 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3285
3286 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3287 continue;
3288
3289 if (bp_objfile_data->overlay_msym.minsym == NULL)
3290 {
3291 struct bound_minimal_symbol m;
3292
3293 m = lookup_minimal_symbol_text (func_name, objfile);
3294 if (m.minsym == NULL)
3295 {
3296 /* Avoid future lookups in this objfile. */
3297 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3298 continue;
3299 }
3300 bp_objfile_data->overlay_msym = m;
3301 }
3302
3303 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3304 b = create_internal_breakpoint (objfile->arch (), addr,
3305 bp_overlay_event,
3306 &internal_breakpoint_ops);
3307 initialize_explicit_location (&explicit_loc);
3308 explicit_loc.function_name = ASTRDUP (func_name);
3309 b->location = new_explicit_location (&explicit_loc);
3310
3311 if (overlay_debugging == ovly_auto)
3312 {
3313 b->enable_state = bp_enabled;
3314 overlay_events_enabled = 1;
3315 }
3316 else
3317 {
3318 b->enable_state = bp_disabled;
3319 overlay_events_enabled = 0;
3320 }
3321 }
3322 }
3323
3324 /* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3325 true if a breakpoint was installed. */
3326
3327 static bool
3328 create_longjmp_master_breakpoint_probe (objfile *objfile)
3329 {
3330 struct gdbarch *gdbarch = objfile->arch ();
3331 struct breakpoint_objfile_data *bp_objfile_data
3332 = get_breakpoint_objfile_data (objfile);
3333
3334 if (!bp_objfile_data->longjmp_searched)
3335 {
3336 std::vector<probe *> ret
3337 = find_probes_in_objfile (objfile, "libc", "longjmp");
3338
3339 if (!ret.empty ())
3340 {
3341 /* We are only interested in checking one element. */
3342 probe *p = ret[0];
3343
3344 if (!p->can_evaluate_arguments ())
3345 {
3346 /* We cannot use the probe interface here,
3347 because it does not know how to evaluate
3348 arguments. */
3349 ret.clear ();
3350 }
3351 }
3352 bp_objfile_data->longjmp_probes = ret;
3353 bp_objfile_data->longjmp_searched = 1;
3354 }
3355
3356 if (bp_objfile_data->longjmp_probes.empty ())
3357 return false;
3358
3359 for (probe *p : bp_objfile_data->longjmp_probes)
3360 {
3361 struct breakpoint *b;
3362
3363 b = create_internal_breakpoint (gdbarch,
3364 p->get_relocated_address (objfile),
3365 bp_longjmp_master,
3366 &internal_breakpoint_ops);
3367 b->location = new_probe_location ("-probe-stap libc:longjmp");
3368 b->enable_state = bp_disabled;
3369 }
3370
3371 return true;
3372 }
3373
3374 /* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3375 Return true if at least one breakpoint was installed. */
3376
3377 static bool
3378 create_longjmp_master_breakpoint_names (objfile *objfile)
3379 {
3380 struct gdbarch *gdbarch = objfile->arch ();
3381 if (!gdbarch_get_longjmp_target_p (gdbarch))
3382 return false;
3383
3384 struct breakpoint_objfile_data *bp_objfile_data
3385 = get_breakpoint_objfile_data (objfile);
3386 unsigned int installed_bp = 0;
3387
3388 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3389 {
3390 struct breakpoint *b;
3391 const char *func_name;
3392 CORE_ADDR addr;
3393 struct explicit_location explicit_loc;
3394
3395 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3396 continue;
3397
3398 func_name = longjmp_names[i];
3399 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3400 {
3401 struct bound_minimal_symbol m;
3402
3403 m = lookup_minimal_symbol_text (func_name, objfile);
3404 if (m.minsym == NULL)
3405 {
3406 /* Prevent future lookups in this objfile. */
3407 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3408 continue;
3409 }
3410 bp_objfile_data->longjmp_msym[i] = m;
3411 }
3412
3413 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3414 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3415 &internal_breakpoint_ops);
3416 initialize_explicit_location (&explicit_loc);
3417 explicit_loc.function_name = ASTRDUP (func_name);
3418 b->location = new_explicit_location (&explicit_loc);
3419 b->enable_state = bp_disabled;
3420 installed_bp++;
3421 }
3422
3423 return installed_bp > 0;
3424 }
3425
3426 /* Create a master longjmp breakpoint. */
3427
3428 static void
3429 create_longjmp_master_breakpoint (void)
3430 {
3431 scoped_restore_current_program_space restore_pspace;
3432
3433 for (struct program_space *pspace : program_spaces)
3434 {
3435 set_current_program_space (pspace);
3436
3437 for (objfile *obj : current_program_space->objfiles ())
3438 {
3439 /* Skip separate debug object, it's handled in the loop below. */
3440 if (obj->separate_debug_objfile_backlink != nullptr)
3441 continue;
3442
3443 /* Try a probe kind breakpoint on main objfile. */
3444 if (create_longjmp_master_breakpoint_probe (obj))
3445 continue;
3446
3447 /* Try longjmp_names kind breakpoints on main and separate_debug
3448 objfiles. */
3449 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3450 if (create_longjmp_master_breakpoint_names (debug_objfile))
3451 break;
3452 }
3453 }
3454 }
3455
3456 /* Create a master std::terminate breakpoint. */
3457 static void
3458 create_std_terminate_master_breakpoint (void)
3459 {
3460 const char *const func_name = "std::terminate()";
3461
3462 scoped_restore_current_program_space restore_pspace;
3463
3464 for (struct program_space *pspace : program_spaces)
3465 {
3466 CORE_ADDR addr;
3467
3468 set_current_program_space (pspace);
3469
3470 for (objfile *objfile : current_program_space->objfiles ())
3471 {
3472 struct breakpoint *b;
3473 struct breakpoint_objfile_data *bp_objfile_data;
3474 struct explicit_location explicit_loc;
3475
3476 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3477
3478 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3479 continue;
3480
3481 if (bp_objfile_data->terminate_msym.minsym == NULL)
3482 {
3483 struct bound_minimal_symbol m;
3484
3485 m = lookup_minimal_symbol (func_name, NULL, objfile);
3486 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3487 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3488 {
3489 /* Prevent future lookups in this objfile. */
3490 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3491 continue;
3492 }
3493 bp_objfile_data->terminate_msym = m;
3494 }
3495
3496 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3497 b = create_internal_breakpoint (objfile->arch (), addr,
3498 bp_std_terminate_master,
3499 &internal_breakpoint_ops);
3500 initialize_explicit_location (&explicit_loc);
3501 explicit_loc.function_name = ASTRDUP (func_name);
3502 b->location = new_explicit_location (&explicit_loc);
3503 b->enable_state = bp_disabled;
3504 }
3505 }
3506 }
3507
3508 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3509 probe. Return true if a breakpoint was installed. */
3510
3511 static bool
3512 create_exception_master_breakpoint_probe (objfile *objfile)
3513 {
3514 struct breakpoint *b;
3515 struct gdbarch *gdbarch;
3516 struct breakpoint_objfile_data *bp_objfile_data;
3517
3518 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3519
3520 /* We prefer the SystemTap probe point if it exists. */
3521 if (!bp_objfile_data->exception_searched)
3522 {
3523 std::vector<probe *> ret
3524 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3525
3526 if (!ret.empty ())
3527 {
3528 /* We are only interested in checking one element. */
3529 probe *p = ret[0];
3530
3531 if (!p->can_evaluate_arguments ())
3532 {
3533 /* We cannot use the probe interface here, because it does
3534 not know how to evaluate arguments. */
3535 ret.clear ();
3536 }
3537 }
3538 bp_objfile_data->exception_probes = ret;
3539 bp_objfile_data->exception_searched = 1;
3540 }
3541
3542 if (bp_objfile_data->exception_probes.empty ())
3543 return false;
3544
3545 gdbarch = objfile->arch ();
3546
3547 for (probe *p : bp_objfile_data->exception_probes)
3548 {
3549 b = create_internal_breakpoint (gdbarch,
3550 p->get_relocated_address (objfile),
3551 bp_exception_master,
3552 &internal_breakpoint_ops);
3553 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3554 b->enable_state = bp_disabled;
3555 }
3556
3557 return true;
3558 }
3559
3560 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3561 _Unwind_DebugHook. Return true if a breakpoint was installed. */
3562
3563 static bool
3564 create_exception_master_breakpoint_hook (objfile *objfile)
3565 {
3566 const char *const func_name = "_Unwind_DebugHook";
3567 struct breakpoint *b;
3568 struct gdbarch *gdbarch;
3569 struct breakpoint_objfile_data *bp_objfile_data;
3570 CORE_ADDR addr;
3571 struct explicit_location explicit_loc;
3572
3573 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3574
3575 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3576 return false;
3577
3578 gdbarch = objfile->arch ();
3579
3580 if (bp_objfile_data->exception_msym.minsym == NULL)
3581 {
3582 struct bound_minimal_symbol debug_hook;
3583
3584 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3585 if (debug_hook.minsym == NULL)
3586 {
3587 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3588 return false;
3589 }
3590
3591 bp_objfile_data->exception_msym = debug_hook;
3592 }
3593
3594 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3595 addr = gdbarch_convert_from_func_ptr_addr
3596 (gdbarch, addr, current_inferior ()->top_target ());
3597 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3598 &internal_breakpoint_ops);
3599 initialize_explicit_location (&explicit_loc);
3600 explicit_loc.function_name = ASTRDUP (func_name);
3601 b->location = new_explicit_location (&explicit_loc);
3602 b->enable_state = bp_disabled;
3603
3604 return true;
3605 }
3606
3607 /* Install a master breakpoint on the unwinder's debug hook. */
3608
3609 static void
3610 create_exception_master_breakpoint (void)
3611 {
3612 for (objfile *obj : current_program_space->objfiles ())
3613 {
3614 /* Skip separate debug object. */
3615 if (obj->separate_debug_objfile_backlink)
3616 continue;
3617
3618 /* Try a probe kind breakpoint. */
3619 if (create_exception_master_breakpoint_probe (obj))
3620 continue;
3621
3622 /* Iterate over main and separate debug objects and try an
3623 _Unwind_DebugHook kind breakpoint. */
3624 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3625 if (create_exception_master_breakpoint_hook (debug_objfile))
3626 break;
3627 }
3628 }
3629
3630 /* Does B have a location spec? */
3631
3632 static int
3633 breakpoint_event_location_empty_p (const struct breakpoint *b)
3634 {
3635 return b->location != NULL && event_location_empty_p (b->location.get ());
3636 }
3637
3638 void
3639 update_breakpoints_after_exec (void)
3640 {
3641 /* We're about to delete breakpoints from GDB's lists. If the
3642 INSERTED flag is true, GDB will try to lift the breakpoints by
3643 writing the breakpoints' "shadow contents" back into memory. The
3644 "shadow contents" are NOT valid after an exec, so GDB should not
3645 do that. Instead, the target is responsible from marking
3646 breakpoints out as soon as it detects an exec. We don't do that
3647 here instead, because there may be other attempts to delete
3648 breakpoints after detecting an exec and before reaching here. */
3649 for (bp_location *bploc : all_bp_locations ())
3650 if (bploc->pspace == current_program_space)
3651 gdb_assert (!bploc->inserted);
3652
3653 for (breakpoint *b : all_breakpoints_safe ())
3654 {
3655 if (b->pspace != current_program_space)
3656 continue;
3657
3658 /* Solib breakpoints must be explicitly reset after an exec(). */
3659 if (b->type == bp_shlib_event)
3660 {
3661 delete_breakpoint (b);
3662 continue;
3663 }
3664
3665 /* JIT breakpoints must be explicitly reset after an exec(). */
3666 if (b->type == bp_jit_event)
3667 {
3668 delete_breakpoint (b);
3669 continue;
3670 }
3671
3672 /* Thread event breakpoints must be set anew after an exec(),
3673 as must overlay event and longjmp master breakpoints. */
3674 if (b->type == bp_thread_event || b->type == bp_overlay_event
3675 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3676 || b->type == bp_exception_master)
3677 {
3678 delete_breakpoint (b);
3679 continue;
3680 }
3681
3682 /* Step-resume breakpoints are meaningless after an exec(). */
3683 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3684 {
3685 delete_breakpoint (b);
3686 continue;
3687 }
3688
3689 /* Just like single-step breakpoints. */
3690 if (b->type == bp_single_step)
3691 {
3692 delete_breakpoint (b);
3693 continue;
3694 }
3695
3696 /* Longjmp and longjmp-resume breakpoints are also meaningless
3697 after an exec. */
3698 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3699 || b->type == bp_longjmp_call_dummy
3700 || b->type == bp_exception || b->type == bp_exception_resume)
3701 {
3702 delete_breakpoint (b);
3703 continue;
3704 }
3705
3706 if (b->type == bp_catchpoint)
3707 {
3708 /* For now, none of the bp_catchpoint breakpoints need to
3709 do anything at this point. In the future, if some of
3710 the catchpoints need to something, we will need to add
3711 a new method, and call this method from here. */
3712 continue;
3713 }
3714
3715 /* bp_finish is a special case. The only way we ought to be able
3716 to see one of these when an exec() has happened, is if the user
3717 caught a vfork, and then said "finish". Ordinarily a finish just
3718 carries them to the call-site of the current callee, by setting
3719 a temporary bp there and resuming. But in this case, the finish
3720 will carry them entirely through the vfork & exec.
3721
3722 We don't want to allow a bp_finish to remain inserted now. But
3723 we can't safely delete it, 'cause finish_command has a handle to
3724 the bp on a bpstat, and will later want to delete it. There's a
3725 chance (and I've seen it happen) that if we delete the bp_finish
3726 here, that its storage will get reused by the time finish_command
3727 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3728 We really must allow finish_command to delete a bp_finish.
3729
3730 In the absence of a general solution for the "how do we know
3731 it's safe to delete something others may have handles to?"
3732 problem, what we'll do here is just uninsert the bp_finish, and
3733 let finish_command delete it.
3734
3735 (We know the bp_finish is "doomed" in the sense that it's
3736 momentary, and will be deleted as soon as finish_command sees
3737 the inferior stopped. So it doesn't matter that the bp's
3738 address is probably bogus in the new a.out, unlike e.g., the
3739 solib breakpoints.) */
3740
3741 if (b->type == bp_finish)
3742 {
3743 continue;
3744 }
3745
3746 /* Without a symbolic address, we have little hope of the
3747 pre-exec() address meaning the same thing in the post-exec()
3748 a.out. */
3749 if (breakpoint_event_location_empty_p (b))
3750 {
3751 delete_breakpoint (b);
3752 continue;
3753 }
3754 }
3755 }
3756
3757 int
3758 detach_breakpoints (ptid_t ptid)
3759 {
3760 int val = 0;
3761 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3762 struct inferior *inf = current_inferior ();
3763
3764 if (ptid.pid () == inferior_ptid.pid ())
3765 error (_("Cannot detach breakpoints of inferior_ptid"));
3766
3767 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3768 inferior_ptid = ptid;
3769 for (bp_location *bl : all_bp_locations ())
3770 {
3771 if (bl->pspace != inf->pspace)
3772 continue;
3773
3774 /* This function must physically remove breakpoints locations
3775 from the specified ptid, without modifying the breakpoint
3776 package's state. Locations of type bp_loc_other are only
3777 maintained at GDB side. So, there is no need to remove
3778 these bp_loc_other locations. Moreover, removing these
3779 would modify the breakpoint package's state. */
3780 if (bl->loc_type == bp_loc_other)
3781 continue;
3782
3783 if (bl->inserted)
3784 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3785 }
3786
3787 return val;
3788 }
3789
3790 /* Remove the breakpoint location BL from the current address space.
3791 Note that this is used to detach breakpoints from a child fork.
3792 When we get here, the child isn't in the inferior list, and neither
3793 do we have objects to represent its address space --- we should
3794 *not* look at bl->pspace->aspace here. */
3795
3796 static int
3797 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3798 {
3799 int val;
3800
3801 /* BL is never in moribund_locations by our callers. */
3802 gdb_assert (bl->owner != NULL);
3803
3804 /* The type of none suggests that owner is actually deleted.
3805 This should not ever happen. */
3806 gdb_assert (bl->owner->type != bp_none);
3807
3808 if (bl->loc_type == bp_loc_software_breakpoint
3809 || bl->loc_type == bp_loc_hardware_breakpoint)
3810 {
3811 /* "Normal" instruction breakpoint: either the standard
3812 trap-instruction bp (bp_breakpoint), or a
3813 bp_hardware_breakpoint. */
3814
3815 /* First check to see if we have to handle an overlay. */
3816 if (overlay_debugging == ovly_off
3817 || bl->section == NULL
3818 || !(section_is_overlay (bl->section)))
3819 {
3820 /* No overlay handling: just remove the breakpoint. */
3821
3822 /* If we're trying to uninsert a memory breakpoint that we
3823 know is set in a dynamic object that is marked
3824 shlib_disabled, then either the dynamic object was
3825 removed with "remove-symbol-file" or with
3826 "nosharedlibrary". In the former case, we don't know
3827 whether another dynamic object might have loaded over the
3828 breakpoint's address -- the user might well let us know
3829 about it next with add-symbol-file (the whole point of
3830 add-symbol-file is letting the user manually maintain a
3831 list of dynamically loaded objects). If we have the
3832 breakpoint's shadow memory, that is, this is a software
3833 breakpoint managed by GDB, check whether the breakpoint
3834 is still inserted in memory, to avoid overwriting wrong
3835 code with stale saved shadow contents. Note that HW
3836 breakpoints don't have shadow memory, as they're
3837 implemented using a mechanism that is not dependent on
3838 being able to modify the target's memory, and as such
3839 they should always be removed. */
3840 if (bl->shlib_disabled
3841 && bl->target_info.shadow_len != 0
3842 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3843 val = 0;
3844 else
3845 val = bl->owner->ops->remove_location (bl, reason);
3846 }
3847 else
3848 {
3849 /* This breakpoint is in an overlay section.
3850 Did we set a breakpoint at the LMA? */
3851 if (!overlay_events_enabled)
3852 {
3853 /* Yes -- overlay event support is not active, so we
3854 should have set a breakpoint at the LMA. Remove it.
3855 */
3856 /* Ignore any failures: if the LMA is in ROM, we will
3857 have already warned when we failed to insert it. */
3858 if (bl->loc_type == bp_loc_hardware_breakpoint)
3859 target_remove_hw_breakpoint (bl->gdbarch,
3860 &bl->overlay_target_info);
3861 else
3862 target_remove_breakpoint (bl->gdbarch,
3863 &bl->overlay_target_info,
3864 reason);
3865 }
3866 /* Did we set a breakpoint at the VMA?
3867 If so, we will have marked the breakpoint 'inserted'. */
3868 if (bl->inserted)
3869 {
3870 /* Yes -- remove it. Previously we did not bother to
3871 remove the breakpoint if the section had been
3872 unmapped, but let's not rely on that being safe. We
3873 don't know what the overlay manager might do. */
3874
3875 /* However, we should remove *software* breakpoints only
3876 if the section is still mapped, or else we overwrite
3877 wrong code with the saved shadow contents. */
3878 if (bl->loc_type == bp_loc_hardware_breakpoint
3879 || section_is_mapped (bl->section))
3880 val = bl->owner->ops->remove_location (bl, reason);
3881 else
3882 val = 0;
3883 }
3884 else
3885 {
3886 /* No -- not inserted, so no need to remove. No error. */
3887 val = 0;
3888 }
3889 }
3890
3891 /* In some cases, we might not be able to remove a breakpoint in
3892 a shared library that has already been removed, but we have
3893 not yet processed the shlib unload event. Similarly for an
3894 unloaded add-symbol-file object - the user might not yet have
3895 had the chance to remove-symbol-file it. shlib_disabled will
3896 be set if the library/object has already been removed, but
3897 the breakpoint hasn't been uninserted yet, e.g., after
3898 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3899 always-inserted mode. */
3900 if (val
3901 && (bl->loc_type == bp_loc_software_breakpoint
3902 && (bl->shlib_disabled
3903 || solib_name_from_address (bl->pspace, bl->address)
3904 || shared_objfile_contains_address_p (bl->pspace,
3905 bl->address))))
3906 val = 0;
3907
3908 if (val)
3909 return val;
3910 bl->inserted = (reason == DETACH_BREAKPOINT);
3911 }
3912 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3913 {
3914 gdb_assert (bl->owner->ops != NULL
3915 && bl->owner->ops->remove_location != NULL);
3916
3917 bl->inserted = (reason == DETACH_BREAKPOINT);
3918 bl->owner->ops->remove_location (bl, reason);
3919
3920 /* Failure to remove any of the hardware watchpoints comes here. */
3921 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3922 warning (_("Could not remove hardware watchpoint %d."),
3923 bl->owner->number);
3924 }
3925 else if (bl->owner->type == bp_catchpoint
3926 && breakpoint_enabled (bl->owner)
3927 && !bl->duplicate)
3928 {
3929 gdb_assert (bl->owner->ops != NULL
3930 && bl->owner->ops->remove_location != NULL);
3931
3932 val = bl->owner->ops->remove_location (bl, reason);
3933 if (val)
3934 return val;
3935
3936 bl->inserted = (reason == DETACH_BREAKPOINT);
3937 }
3938
3939 return 0;
3940 }
3941
3942 static int
3943 remove_breakpoint (struct bp_location *bl)
3944 {
3945 /* BL is never in moribund_locations by our callers. */
3946 gdb_assert (bl->owner != NULL);
3947
3948 /* The type of none suggests that owner is actually deleted.
3949 This should not ever happen. */
3950 gdb_assert (bl->owner->type != bp_none);
3951
3952 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3953
3954 switch_to_program_space_and_thread (bl->pspace);
3955
3956 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
3957 }
3958
3959 /* Clear the "inserted" flag in all breakpoints. */
3960
3961 void
3962 mark_breakpoints_out (void)
3963 {
3964 for (bp_location *bl : all_bp_locations ())
3965 if (bl->pspace == current_program_space)
3966 bl->inserted = 0;
3967 }
3968
3969 /* Clear the "inserted" flag in all breakpoints and delete any
3970 breakpoints which should go away between runs of the program.
3971
3972 Plus other such housekeeping that has to be done for breakpoints
3973 between runs.
3974
3975 Note: this function gets called at the end of a run (by
3976 generic_mourn_inferior) and when a run begins (by
3977 init_wait_for_inferior). */
3978
3979
3980
3981 void
3982 breakpoint_init_inferior (enum inf_context context)
3983 {
3984 struct program_space *pspace = current_program_space;
3985
3986 /* If breakpoint locations are shared across processes, then there's
3987 nothing to do. */
3988 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
3989 return;
3990
3991 mark_breakpoints_out ();
3992
3993 for (breakpoint *b : all_breakpoints_safe ())
3994 {
3995 if (b->loc && b->loc->pspace != pspace)
3996 continue;
3997
3998 switch (b->type)
3999 {
4000 case bp_call_dummy:
4001 case bp_longjmp_call_dummy:
4002
4003 /* If the call dummy breakpoint is at the entry point it will
4004 cause problems when the inferior is rerun, so we better get
4005 rid of it. */
4006
4007 case bp_watchpoint_scope:
4008
4009 /* Also get rid of scope breakpoints. */
4010
4011 case bp_shlib_event:
4012
4013 /* Also remove solib event breakpoints. Their addresses may
4014 have changed since the last time we ran the program.
4015 Actually we may now be debugging against different target;
4016 and so the solib backend that installed this breakpoint may
4017 not be used in by the target. E.g.,
4018
4019 (gdb) file prog-linux
4020 (gdb) run # native linux target
4021 ...
4022 (gdb) kill
4023 (gdb) file prog-win.exe
4024 (gdb) tar rem :9999 # remote Windows gdbserver.
4025 */
4026
4027 case bp_step_resume:
4028
4029 /* Also remove step-resume breakpoints. */
4030
4031 case bp_single_step:
4032
4033 /* Also remove single-step breakpoints. */
4034
4035 delete_breakpoint (b);
4036 break;
4037
4038 case bp_watchpoint:
4039 case bp_hardware_watchpoint:
4040 case bp_read_watchpoint:
4041 case bp_access_watchpoint:
4042 {
4043 struct watchpoint *w = (struct watchpoint *) b;
4044
4045 /* Likewise for watchpoints on local expressions. */
4046 if (w->exp_valid_block != NULL)
4047 delete_breakpoint (b);
4048 else
4049 {
4050 /* Get rid of existing locations, which are no longer
4051 valid. New ones will be created in
4052 update_watchpoint, when the inferior is restarted.
4053 The next update_global_location_list call will
4054 garbage collect them. */
4055 b->loc = NULL;
4056
4057 if (context == inf_starting)
4058 {
4059 /* Reset val field to force reread of starting value in
4060 insert_breakpoints. */
4061 w->val.reset (nullptr);
4062 w->val_valid = false;
4063 }
4064 }
4065 }
4066 break;
4067 default:
4068 break;
4069 }
4070 }
4071
4072 /* Get rid of the moribund locations. */
4073 for (bp_location *bl : moribund_locations)
4074 decref_bp_location (&bl);
4075 moribund_locations.clear ();
4076 }
4077
4078 /* These functions concern about actual breakpoints inserted in the
4079 target --- to e.g. check if we need to do decr_pc adjustment or if
4080 we need to hop over the bkpt --- so we check for address space
4081 match, not program space. */
4082
4083 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4084 exists at PC. It returns ordinary_breakpoint_here if it's an
4085 ordinary breakpoint, or permanent_breakpoint_here if it's a
4086 permanent breakpoint.
4087 - When continuing from a location with an ordinary breakpoint, we
4088 actually single step once before calling insert_breakpoints.
4089 - When continuing from a location with a permanent breakpoint, we
4090 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4091 the target, to advance the PC past the breakpoint. */
4092
4093 enum breakpoint_here
4094 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4095 {
4096 int any_breakpoint_here = 0;
4097
4098 for (bp_location *bl : all_bp_locations ())
4099 {
4100 if (bl->loc_type != bp_loc_software_breakpoint
4101 && bl->loc_type != bp_loc_hardware_breakpoint)
4102 continue;
4103
4104 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4105 if ((breakpoint_enabled (bl->owner)
4106 || bl->permanent)
4107 && breakpoint_location_address_match (bl, aspace, pc))
4108 {
4109 if (overlay_debugging
4110 && section_is_overlay (bl->section)
4111 && !section_is_mapped (bl->section))
4112 continue; /* unmapped overlay -- can't be a match */
4113 else if (bl->permanent)
4114 return permanent_breakpoint_here;
4115 else
4116 any_breakpoint_here = 1;
4117 }
4118 }
4119
4120 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4121 }
4122
4123 /* See breakpoint.h. */
4124
4125 int
4126 breakpoint_in_range_p (const address_space *aspace,
4127 CORE_ADDR addr, ULONGEST len)
4128 {
4129 for (bp_location *bl : all_bp_locations ())
4130 {
4131 if (bl->loc_type != bp_loc_software_breakpoint
4132 && bl->loc_type != bp_loc_hardware_breakpoint)
4133 continue;
4134
4135 if ((breakpoint_enabled (bl->owner)
4136 || bl->permanent)
4137 && breakpoint_location_address_range_overlap (bl, aspace,
4138 addr, len))
4139 {
4140 if (overlay_debugging
4141 && section_is_overlay (bl->section)
4142 && !section_is_mapped (bl->section))
4143 {
4144 /* Unmapped overlay -- can't be a match. */
4145 continue;
4146 }
4147
4148 return 1;
4149 }
4150 }
4151
4152 return 0;
4153 }
4154
4155 /* Return true if there's a moribund breakpoint at PC. */
4156
4157 int
4158 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4159 {
4160 for (bp_location *loc : moribund_locations)
4161 if (breakpoint_location_address_match (loc, aspace, pc))
4162 return 1;
4163
4164 return 0;
4165 }
4166
4167 /* Returns non-zero iff BL is inserted at PC, in address space
4168 ASPACE. */
4169
4170 static int
4171 bp_location_inserted_here_p (struct bp_location *bl,
4172 const address_space *aspace, CORE_ADDR pc)
4173 {
4174 if (bl->inserted
4175 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4176 aspace, pc))
4177 {
4178 if (overlay_debugging
4179 && section_is_overlay (bl->section)
4180 && !section_is_mapped (bl->section))
4181 return 0; /* unmapped overlay -- can't be a match */
4182 else
4183 return 1;
4184 }
4185 return 0;
4186 }
4187
4188 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4189
4190 int
4191 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4192 {
4193 struct bp_location **blp, **blp_tmp = NULL;
4194
4195 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4196 {
4197 struct bp_location *bl = *blp;
4198
4199 if (bl->loc_type != bp_loc_software_breakpoint
4200 && bl->loc_type != bp_loc_hardware_breakpoint)
4201 continue;
4202
4203 if (bp_location_inserted_here_p (bl, aspace, pc))
4204 return 1;
4205 }
4206 return 0;
4207 }
4208
4209 /* This function returns non-zero iff there is a software breakpoint
4210 inserted at PC. */
4211
4212 int
4213 software_breakpoint_inserted_here_p (const address_space *aspace,
4214 CORE_ADDR pc)
4215 {
4216 struct bp_location **blp, **blp_tmp = NULL;
4217
4218 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4219 {
4220 struct bp_location *bl = *blp;
4221
4222 if (bl->loc_type != bp_loc_software_breakpoint)
4223 continue;
4224
4225 if (bp_location_inserted_here_p (bl, aspace, pc))
4226 return 1;
4227 }
4228
4229 return 0;
4230 }
4231
4232 /* See breakpoint.h. */
4233
4234 int
4235 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4236 CORE_ADDR pc)
4237 {
4238 struct bp_location **blp, **blp_tmp = NULL;
4239
4240 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4241 {
4242 struct bp_location *bl = *blp;
4243
4244 if (bl->loc_type != bp_loc_hardware_breakpoint)
4245 continue;
4246
4247 if (bp_location_inserted_here_p (bl, aspace, pc))
4248 return 1;
4249 }
4250
4251 return 0;
4252 }
4253
4254 int
4255 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4256 CORE_ADDR addr, ULONGEST len)
4257 {
4258 for (breakpoint *bpt : all_breakpoints ())
4259 {
4260 if (bpt->type != bp_hardware_watchpoint
4261 && bpt->type != bp_access_watchpoint)
4262 continue;
4263
4264 if (!breakpoint_enabled (bpt))
4265 continue;
4266
4267 for (bp_location *loc : bpt->locations ())
4268 if (loc->pspace->aspace == aspace && loc->inserted)
4269 {
4270 CORE_ADDR l, h;
4271
4272 /* Check for intersection. */
4273 l = std::max<CORE_ADDR> (loc->address, addr);
4274 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4275 if (l < h)
4276 return 1;
4277 }
4278 }
4279 return 0;
4280 }
4281
4282 /* See breakpoint.h. */
4283
4284 bool
4285 is_catchpoint (struct breakpoint *b)
4286 {
4287 return (b->type == bp_catchpoint);
4288 }
4289
4290 /* Clear a bpstat so that it says we are not at any breakpoint.
4291 Also free any storage that is part of a bpstat. */
4292
4293 void
4294 bpstat_clear (bpstat *bsp)
4295 {
4296 bpstat p;
4297 bpstat q;
4298
4299 if (bsp == 0)
4300 return;
4301 p = *bsp;
4302 while (p != NULL)
4303 {
4304 q = p->next;
4305 delete p;
4306 p = q;
4307 }
4308 *bsp = NULL;
4309 }
4310
4311 bpstats::bpstats (const bpstats &other)
4312 : next (NULL),
4313 bp_location_at (other.bp_location_at),
4314 breakpoint_at (other.breakpoint_at),
4315 commands (other.commands),
4316 print (other.print),
4317 stop (other.stop),
4318 print_it (other.print_it)
4319 {
4320 if (other.old_val != NULL)
4321 old_val = release_value (value_copy (other.old_val.get ()));
4322 }
4323
4324 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4325 is part of the bpstat is copied as well. */
4326
4327 bpstat
4328 bpstat_copy (bpstat bs)
4329 {
4330 bpstat p = NULL;
4331 bpstat tmp;
4332 bpstat retval = NULL;
4333
4334 if (bs == NULL)
4335 return bs;
4336
4337 for (; bs != NULL; bs = bs->next)
4338 {
4339 tmp = new bpstats (*bs);
4340
4341 if (p == NULL)
4342 /* This is the first thing in the chain. */
4343 retval = tmp;
4344 else
4345 p->next = tmp;
4346 p = tmp;
4347 }
4348 p->next = NULL;
4349 return retval;
4350 }
4351
4352 /* Find the bpstat associated with this breakpoint. */
4353
4354 bpstat
4355 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4356 {
4357 if (bsp == NULL)
4358 return NULL;
4359
4360 for (; bsp != NULL; bsp = bsp->next)
4361 {
4362 if (bsp->breakpoint_at == breakpoint)
4363 return bsp;
4364 }
4365 return NULL;
4366 }
4367
4368 /* See breakpoint.h. */
4369
4370 bool
4371 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4372 {
4373 for (; bsp != NULL; bsp = bsp->next)
4374 {
4375 if (bsp->breakpoint_at == NULL)
4376 {
4377 /* A moribund location can never explain a signal other than
4378 GDB_SIGNAL_TRAP. */
4379 if (sig == GDB_SIGNAL_TRAP)
4380 return true;
4381 }
4382 else
4383 {
4384 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4385 sig))
4386 return true;
4387 }
4388 }
4389
4390 return false;
4391 }
4392
4393 /* Put in *NUM the breakpoint number of the first breakpoint we are
4394 stopped at. *BSP upon return is a bpstat which points to the
4395 remaining breakpoints stopped at (but which is not guaranteed to be
4396 good for anything but further calls to bpstat_num).
4397
4398 Return 0 if passed a bpstat which does not indicate any breakpoints.
4399 Return -1 if stopped at a breakpoint that has been deleted since
4400 we set it.
4401 Return 1 otherwise. */
4402
4403 int
4404 bpstat_num (bpstat *bsp, int *num)
4405 {
4406 struct breakpoint *b;
4407
4408 if ((*bsp) == NULL)
4409 return 0; /* No more breakpoint values */
4410
4411 /* We assume we'll never have several bpstats that correspond to a
4412 single breakpoint -- otherwise, this function might return the
4413 same number more than once and this will look ugly. */
4414 b = (*bsp)->breakpoint_at;
4415 *bsp = (*bsp)->next;
4416 if (b == NULL)
4417 return -1; /* breakpoint that's been deleted since */
4418
4419 *num = b->number; /* We have its number */
4420 return 1;
4421 }
4422
4423 /* See breakpoint.h. */
4424
4425 void
4426 bpstat_clear_actions (void)
4427 {
4428 bpstat bs;
4429
4430 if (inferior_ptid == null_ptid)
4431 return;
4432
4433 thread_info *tp = inferior_thread ();
4434 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4435 {
4436 bs->commands = NULL;
4437 bs->old_val.reset (nullptr);
4438 }
4439 }
4440
4441 /* Called when a command is about to proceed the inferior. */
4442
4443 static void
4444 breakpoint_about_to_proceed (void)
4445 {
4446 if (inferior_ptid != null_ptid)
4447 {
4448 struct thread_info *tp = inferior_thread ();
4449
4450 /* Allow inferior function calls in breakpoint commands to not
4451 interrupt the command list. When the call finishes
4452 successfully, the inferior will be standing at the same
4453 breakpoint as if nothing happened. */
4454 if (tp->control.in_infcall)
4455 return;
4456 }
4457
4458 breakpoint_proceeded = 1;
4459 }
4460
4461 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4462 or its equivalent. */
4463
4464 static int
4465 command_line_is_silent (struct command_line *cmd)
4466 {
4467 return cmd && (strcmp ("silent", cmd->line) == 0);
4468 }
4469
4470 /* Execute all the commands associated with all the breakpoints at
4471 this location. Any of these commands could cause the process to
4472 proceed beyond this point, etc. We look out for such changes by
4473 checking the global "breakpoint_proceeded" after each command.
4474
4475 Returns true if a breakpoint command resumed the inferior. In that
4476 case, it is the caller's responsibility to recall it again with the
4477 bpstat of the current thread. */
4478
4479 static int
4480 bpstat_do_actions_1 (bpstat *bsp)
4481 {
4482 bpstat bs;
4483 int again = 0;
4484
4485 /* Avoid endless recursion if a `source' command is contained
4486 in bs->commands. */
4487 if (executing_breakpoint_commands)
4488 return 0;
4489
4490 scoped_restore save_executing
4491 = make_scoped_restore (&executing_breakpoint_commands, 1);
4492
4493 scoped_restore preventer = prevent_dont_repeat ();
4494
4495 /* This pointer will iterate over the list of bpstat's. */
4496 bs = *bsp;
4497
4498 breakpoint_proceeded = 0;
4499 for (; bs != NULL; bs = bs->next)
4500 {
4501 struct command_line *cmd = NULL;
4502
4503 /* Take ownership of the BSP's command tree, if it has one.
4504
4505 The command tree could legitimately contain commands like
4506 'step' and 'next', which call clear_proceed_status, which
4507 frees stop_bpstat's command tree. To make sure this doesn't
4508 free the tree we're executing out from under us, we need to
4509 take ownership of the tree ourselves. Since a given bpstat's
4510 commands are only executed once, we don't need to copy it; we
4511 can clear the pointer in the bpstat, and make sure we free
4512 the tree when we're done. */
4513 counted_command_line ccmd = bs->commands;
4514 bs->commands = NULL;
4515 if (ccmd != NULL)
4516 cmd = ccmd.get ();
4517 if (command_line_is_silent (cmd))
4518 {
4519 /* The action has been already done by bpstat_stop_status. */
4520 cmd = cmd->next;
4521 }
4522
4523 while (cmd != NULL)
4524 {
4525 execute_control_command (cmd);
4526
4527 if (breakpoint_proceeded)
4528 break;
4529 else
4530 cmd = cmd->next;
4531 }
4532
4533 if (breakpoint_proceeded)
4534 {
4535 if (current_ui->async)
4536 /* If we are in async mode, then the target might be still
4537 running, not stopped at any breakpoint, so nothing for
4538 us to do here -- just return to the event loop. */
4539 ;
4540 else
4541 /* In sync mode, when execute_control_command returns
4542 we're already standing on the next breakpoint.
4543 Breakpoint commands for that stop were not run, since
4544 execute_command does not run breakpoint commands --
4545 only command_line_handler does, but that one is not
4546 involved in execution of breakpoint commands. So, we
4547 can now execute breakpoint commands. It should be
4548 noted that making execute_command do bpstat actions is
4549 not an option -- in this case we'll have recursive
4550 invocation of bpstat for each breakpoint with a
4551 command, and can easily blow up GDB stack. Instead, we
4552 return true, which will trigger the caller to recall us
4553 with the new stop_bpstat. */
4554 again = 1;
4555 break;
4556 }
4557 }
4558 return again;
4559 }
4560
4561 /* Helper for bpstat_do_actions. Get the current thread, if there's
4562 one, is alive and has execution. Return NULL otherwise. */
4563
4564 static thread_info *
4565 get_bpstat_thread ()
4566 {
4567 if (inferior_ptid == null_ptid || !target_has_execution ())
4568 return NULL;
4569
4570 thread_info *tp = inferior_thread ();
4571 if (tp->state == THREAD_EXITED || tp->executing)
4572 return NULL;
4573 return tp;
4574 }
4575
4576 void
4577 bpstat_do_actions (void)
4578 {
4579 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4580 thread_info *tp;
4581
4582 /* Do any commands attached to breakpoint we are stopped at. */
4583 while ((tp = get_bpstat_thread ()) != NULL)
4584 {
4585 /* Since in sync mode, bpstat_do_actions may resume the
4586 inferior, and only return when it is stopped at the next
4587 breakpoint, we keep doing breakpoint actions until it returns
4588 false to indicate the inferior was not resumed. */
4589 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4590 break;
4591 }
4592
4593 cleanup_if_error.release ();
4594 }
4595
4596 /* Print out the (old or new) value associated with a watchpoint. */
4597
4598 static void
4599 watchpoint_value_print (struct value *val, struct ui_file *stream)
4600 {
4601 if (val == NULL)
4602 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4603 else
4604 {
4605 struct value_print_options opts;
4606 get_user_print_options (&opts);
4607 value_print (val, stream, &opts);
4608 }
4609 }
4610
4611 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4612 debugging multiple threads. */
4613
4614 void
4615 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4616 {
4617 if (uiout->is_mi_like_p ())
4618 return;
4619
4620 uiout->text ("\n");
4621
4622 if (show_thread_that_caused_stop ())
4623 {
4624 const char *name;
4625 struct thread_info *thr = inferior_thread ();
4626
4627 uiout->text ("Thread ");
4628 uiout->field_string ("thread-id", print_thread_id (thr));
4629
4630 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4631 if (name != NULL)
4632 {
4633 uiout->text (" \"");
4634 uiout->field_string ("name", name);
4635 uiout->text ("\"");
4636 }
4637
4638 uiout->text (" hit ");
4639 }
4640 }
4641
4642 /* Generic routine for printing messages indicating why we
4643 stopped. The behavior of this function depends on the value
4644 'print_it' in the bpstat structure. Under some circumstances we
4645 may decide not to print anything here and delegate the task to
4646 normal_stop(). */
4647
4648 static enum print_stop_action
4649 print_bp_stop_message (bpstat bs)
4650 {
4651 switch (bs->print_it)
4652 {
4653 case print_it_noop:
4654 /* Nothing should be printed for this bpstat entry. */
4655 return PRINT_UNKNOWN;
4656 break;
4657
4658 case print_it_done:
4659 /* We still want to print the frame, but we already printed the
4660 relevant messages. */
4661 return PRINT_SRC_AND_LOC;
4662 break;
4663
4664 case print_it_normal:
4665 {
4666 struct breakpoint *b = bs->breakpoint_at;
4667
4668 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4669 which has since been deleted. */
4670 if (b == NULL)
4671 return PRINT_UNKNOWN;
4672
4673 /* Normal case. Call the breakpoint's print_it method. */
4674 return b->ops->print_it (bs);
4675 }
4676 break;
4677
4678 default:
4679 internal_error (__FILE__, __LINE__,
4680 _("print_bp_stop_message: unrecognized enum value"));
4681 break;
4682 }
4683 }
4684
4685 /* A helper function that prints a shared library stopped event. */
4686
4687 static void
4688 print_solib_event (int is_catchpoint)
4689 {
4690 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4691 bool any_added = !current_program_space->added_solibs.empty ();
4692
4693 if (!is_catchpoint)
4694 {
4695 if (any_added || any_deleted)
4696 current_uiout->text (_("Stopped due to shared library event:\n"));
4697 else
4698 current_uiout->text (_("Stopped due to shared library event (no "
4699 "libraries added or removed)\n"));
4700 }
4701
4702 if (current_uiout->is_mi_like_p ())
4703 current_uiout->field_string ("reason",
4704 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4705
4706 if (any_deleted)
4707 {
4708 current_uiout->text (_(" Inferior unloaded "));
4709 ui_out_emit_list list_emitter (current_uiout, "removed");
4710 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4711 {
4712 const std::string &name = current_program_space->deleted_solibs[ix];
4713
4714 if (ix > 0)
4715 current_uiout->text (" ");
4716 current_uiout->field_string ("library", name);
4717 current_uiout->text ("\n");
4718 }
4719 }
4720
4721 if (any_added)
4722 {
4723 current_uiout->text (_(" Inferior loaded "));
4724 ui_out_emit_list list_emitter (current_uiout, "added");
4725 bool first = true;
4726 for (so_list *iter : current_program_space->added_solibs)
4727 {
4728 if (!first)
4729 current_uiout->text (" ");
4730 first = false;
4731 current_uiout->field_string ("library", iter->so_name);
4732 current_uiout->text ("\n");
4733 }
4734 }
4735 }
4736
4737 /* Print a message indicating what happened. This is called from
4738 normal_stop(). The input to this routine is the head of the bpstat
4739 list - a list of the eventpoints that caused this stop. KIND is
4740 the target_waitkind for the stopping event. This
4741 routine calls the generic print routine for printing a message
4742 about reasons for stopping. This will print (for example) the
4743 "Breakpoint n," part of the output. The return value of this
4744 routine is one of:
4745
4746 PRINT_UNKNOWN: Means we printed nothing.
4747 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4748 code to print the location. An example is
4749 "Breakpoint 1, " which should be followed by
4750 the location.
4751 PRINT_SRC_ONLY: Means we printed something, but there is no need
4752 to also print the location part of the message.
4753 An example is the catch/throw messages, which
4754 don't require a location appended to the end.
4755 PRINT_NOTHING: We have done some printing and we don't need any
4756 further info to be printed. */
4757
4758 enum print_stop_action
4759 bpstat_print (bpstat bs, int kind)
4760 {
4761 enum print_stop_action val;
4762
4763 /* Maybe another breakpoint in the chain caused us to stop.
4764 (Currently all watchpoints go on the bpstat whether hit or not.
4765 That probably could (should) be changed, provided care is taken
4766 with respect to bpstat_explains_signal). */
4767 for (; bs; bs = bs->next)
4768 {
4769 val = print_bp_stop_message (bs);
4770 if (val == PRINT_SRC_ONLY
4771 || val == PRINT_SRC_AND_LOC
4772 || val == PRINT_NOTHING)
4773 return val;
4774 }
4775
4776 /* If we had hit a shared library event breakpoint,
4777 print_bp_stop_message would print out this message. If we hit an
4778 OS-level shared library event, do the same thing. */
4779 if (kind == TARGET_WAITKIND_LOADED)
4780 {
4781 print_solib_event (0);
4782 return PRINT_NOTHING;
4783 }
4784
4785 /* We reached the end of the chain, or we got a null BS to start
4786 with and nothing was printed. */
4787 return PRINT_UNKNOWN;
4788 }
4789
4790 /* Evaluate the boolean expression EXP and return the result. */
4791
4792 static bool
4793 breakpoint_cond_eval (expression *exp)
4794 {
4795 struct value *mark = value_mark ();
4796 bool res = value_true (evaluate_expression (exp));
4797
4798 value_free_to_mark (mark);
4799 return res;
4800 }
4801
4802 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4803
4804 bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4805 : next (NULL),
4806 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
4807 breakpoint_at (bl->owner),
4808 commands (NULL),
4809 print (0),
4810 stop (0),
4811 print_it (print_it_normal)
4812 {
4813 **bs_link_pointer = this;
4814 *bs_link_pointer = &next;
4815 }
4816
4817 bpstats::bpstats ()
4818 : next (NULL),
4819 breakpoint_at (NULL),
4820 commands (NULL),
4821 print (0),
4822 stop (0),
4823 print_it (print_it_normal)
4824 {
4825 }
4826 \f
4827 /* The target has stopped with waitstatus WS. Check if any hardware
4828 watchpoints have triggered, according to the target. */
4829
4830 int
4831 watchpoints_triggered (struct target_waitstatus *ws)
4832 {
4833 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
4834 CORE_ADDR addr;
4835
4836 if (!stopped_by_watchpoint)
4837 {
4838 /* We were not stopped by a watchpoint. Mark all watchpoints
4839 as not triggered. */
4840 for (breakpoint *b : all_breakpoints ())
4841 if (is_hardware_watchpoint (b))
4842 {
4843 struct watchpoint *w = (struct watchpoint *) b;
4844
4845 w->watchpoint_triggered = watch_triggered_no;
4846 }
4847
4848 return 0;
4849 }
4850
4851 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
4852 {
4853 /* We were stopped by a watchpoint, but we don't know where.
4854 Mark all watchpoints as unknown. */
4855 for (breakpoint *b : all_breakpoints ())
4856 if (is_hardware_watchpoint (b))
4857 {
4858 struct watchpoint *w = (struct watchpoint *) b;
4859
4860 w->watchpoint_triggered = watch_triggered_unknown;
4861 }
4862
4863 return 1;
4864 }
4865
4866 /* The target could report the data address. Mark watchpoints
4867 affected by this data address as triggered, and all others as not
4868 triggered. */
4869
4870 for (breakpoint *b : all_breakpoints ())
4871 if (is_hardware_watchpoint (b))
4872 {
4873 struct watchpoint *w = (struct watchpoint *) b;
4874
4875 w->watchpoint_triggered = watch_triggered_no;
4876 for (bp_location *loc : b->locations ())
4877 {
4878 if (is_masked_watchpoint (b))
4879 {
4880 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4881 CORE_ADDR start = loc->address & w->hw_wp_mask;
4882
4883 if (newaddr == start)
4884 {
4885 w->watchpoint_triggered = watch_triggered_yes;
4886 break;
4887 }
4888 }
4889 /* Exact match not required. Within range is sufficient. */
4890 else if (target_watchpoint_addr_within_range
4891 (current_inferior ()->top_target (), addr, loc->address,
4892 loc->length))
4893 {
4894 w->watchpoint_triggered = watch_triggered_yes;
4895 break;
4896 }
4897 }
4898 }
4899
4900 return 1;
4901 }
4902
4903 /* Possible return values for watchpoint_check. */
4904 enum wp_check_result
4905 {
4906 /* The watchpoint has been deleted. */
4907 WP_DELETED = 1,
4908
4909 /* The value has changed. */
4910 WP_VALUE_CHANGED = 2,
4911
4912 /* The value has not changed. */
4913 WP_VALUE_NOT_CHANGED = 3,
4914
4915 /* Ignore this watchpoint, no matter if the value changed or not. */
4916 WP_IGNORE = 4,
4917 };
4918
4919 #define BP_TEMPFLAG 1
4920 #define BP_HARDWAREFLAG 2
4921
4922 /* Evaluate watchpoint condition expression and check if its value
4923 changed. */
4924
4925 static wp_check_result
4926 watchpoint_check (bpstat bs)
4927 {
4928 struct watchpoint *b;
4929 struct frame_info *fr;
4930 int within_current_scope;
4931
4932 /* BS is built from an existing struct breakpoint. */
4933 gdb_assert (bs->breakpoint_at != NULL);
4934 b = (struct watchpoint *) bs->breakpoint_at;
4935
4936 /* If this is a local watchpoint, we only want to check if the
4937 watchpoint frame is in scope if the current thread is the thread
4938 that was used to create the watchpoint. */
4939 if (!watchpoint_in_thread_scope (b))
4940 return WP_IGNORE;
4941
4942 if (b->exp_valid_block == NULL)
4943 within_current_scope = 1;
4944 else
4945 {
4946 struct frame_info *frame = get_current_frame ();
4947 struct gdbarch *frame_arch = get_frame_arch (frame);
4948 CORE_ADDR frame_pc = get_frame_pc (frame);
4949
4950 /* stack_frame_destroyed_p() returns a non-zero value if we're
4951 still in the function but the stack frame has already been
4952 invalidated. Since we can't rely on the values of local
4953 variables after the stack has been destroyed, we are treating
4954 the watchpoint in that state as `not changed' without further
4955 checking. Don't mark watchpoints as changed if the current
4956 frame is in an epilogue - even if they are in some other
4957 frame, our view of the stack is likely to be wrong and
4958 frame_find_by_id could error out. */
4959 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
4960 return WP_IGNORE;
4961
4962 fr = frame_find_by_id (b->watchpoint_frame);
4963 within_current_scope = (fr != NULL);
4964
4965 /* If we've gotten confused in the unwinder, we might have
4966 returned a frame that can't describe this variable. */
4967 if (within_current_scope)
4968 {
4969 struct symbol *function;
4970
4971 function = get_frame_function (fr);
4972 if (function == NULL
4973 || !contained_in (b->exp_valid_block,
4974 SYMBOL_BLOCK_VALUE (function)))
4975 within_current_scope = 0;
4976 }
4977
4978 if (within_current_scope)
4979 /* If we end up stopping, the current frame will get selected
4980 in normal_stop. So this call to select_frame won't affect
4981 the user. */
4982 select_frame (fr);
4983 }
4984
4985 if (within_current_scope)
4986 {
4987 /* We use value_{,free_to_}mark because it could be a *long*
4988 time before we return to the command level and call
4989 free_all_values. We can't call free_all_values because we
4990 might be in the middle of evaluating a function call. */
4991
4992 struct value *mark;
4993 struct value *new_val;
4994
4995 if (is_masked_watchpoint (b))
4996 /* Since we don't know the exact trigger address (from
4997 stopped_data_address), just tell the user we've triggered
4998 a mask watchpoint. */
4999 return WP_VALUE_CHANGED;
5000
5001 mark = value_mark ();
5002 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
5003 NULL, NULL, false);
5004
5005 if (b->val_bitsize != 0)
5006 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5007
5008 /* We use value_equal_contents instead of value_equal because
5009 the latter coerces an array to a pointer, thus comparing just
5010 the address of the array instead of its contents. This is
5011 not what we want. */
5012 if ((b->val != NULL) != (new_val != NULL)
5013 || (b->val != NULL && !value_equal_contents (b->val.get (),
5014 new_val)))
5015 {
5016 bs->old_val = b->val;
5017 b->val = release_value (new_val);
5018 b->val_valid = true;
5019 if (new_val != NULL)
5020 value_free_to_mark (mark);
5021 return WP_VALUE_CHANGED;
5022 }
5023 else
5024 {
5025 /* Nothing changed. */
5026 value_free_to_mark (mark);
5027 return WP_VALUE_NOT_CHANGED;
5028 }
5029 }
5030 else
5031 {
5032 /* This seems like the only logical thing to do because
5033 if we temporarily ignored the watchpoint, then when
5034 we reenter the block in which it is valid it contains
5035 garbage (in the case of a function, it may have two
5036 garbage values, one before and one after the prologue).
5037 So we can't even detect the first assignment to it and
5038 watch after that (since the garbage may or may not equal
5039 the first value assigned). */
5040 /* We print all the stop information in
5041 breakpoint_ops->print_it, but in this case, by the time we
5042 call breakpoint_ops->print_it this bp will be deleted
5043 already. So we have no choice but print the information
5044 here. */
5045
5046 SWITCH_THRU_ALL_UIS ()
5047 {
5048 struct ui_out *uiout = current_uiout;
5049
5050 if (uiout->is_mi_like_p ())
5051 uiout->field_string
5052 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5053 uiout->message ("\nWatchpoint %pF deleted because the program has "
5054 "left the block in\n"
5055 "which its expression is valid.\n",
5056 signed_field ("wpnum", b->number));
5057 }
5058
5059 /* Make sure the watchpoint's commands aren't executed. */
5060 b->commands = NULL;
5061 watchpoint_del_at_next_stop (b);
5062
5063 return WP_DELETED;
5064 }
5065 }
5066
5067 /* Return true if it looks like target has stopped due to hitting
5068 breakpoint location BL. This function does not check if we should
5069 stop, only if BL explains the stop. */
5070
5071 static int
5072 bpstat_check_location (const struct bp_location *bl,
5073 const address_space *aspace, CORE_ADDR bp_addr,
5074 const struct target_waitstatus *ws)
5075 {
5076 struct breakpoint *b = bl->owner;
5077
5078 /* BL is from an existing breakpoint. */
5079 gdb_assert (b != NULL);
5080
5081 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5082 }
5083
5084 /* Determine if the watched values have actually changed, and we
5085 should stop. If not, set BS->stop to 0. */
5086
5087 static void
5088 bpstat_check_watchpoint (bpstat bs)
5089 {
5090 const struct bp_location *bl;
5091 struct watchpoint *b;
5092
5093 /* BS is built for existing struct breakpoint. */
5094 bl = bs->bp_location_at.get ();
5095 gdb_assert (bl != NULL);
5096 b = (struct watchpoint *) bs->breakpoint_at;
5097 gdb_assert (b != NULL);
5098
5099 {
5100 int must_check_value = 0;
5101
5102 if (b->type == bp_watchpoint)
5103 /* For a software watchpoint, we must always check the
5104 watched value. */
5105 must_check_value = 1;
5106 else if (b->watchpoint_triggered == watch_triggered_yes)
5107 /* We have a hardware watchpoint (read, write, or access)
5108 and the target earlier reported an address watched by
5109 this watchpoint. */
5110 must_check_value = 1;
5111 else if (b->watchpoint_triggered == watch_triggered_unknown
5112 && b->type == bp_hardware_watchpoint)
5113 /* We were stopped by a hardware watchpoint, but the target could
5114 not report the data address. We must check the watchpoint's
5115 value. Access and read watchpoints are out of luck; without
5116 a data address, we can't figure it out. */
5117 must_check_value = 1;
5118
5119 if (must_check_value)
5120 {
5121 wp_check_result e;
5122
5123 try
5124 {
5125 e = watchpoint_check (bs);
5126 }
5127 catch (const gdb_exception &ex)
5128 {
5129 exception_fprintf (gdb_stderr, ex,
5130 "Error evaluating expression "
5131 "for watchpoint %d\n",
5132 b->number);
5133
5134 SWITCH_THRU_ALL_UIS ()
5135 {
5136 printf_filtered (_("Watchpoint %d deleted.\n"),
5137 b->number);
5138 }
5139 watchpoint_del_at_next_stop (b);
5140 e = WP_DELETED;
5141 }
5142
5143 switch (e)
5144 {
5145 case WP_DELETED:
5146 /* We've already printed what needs to be printed. */
5147 bs->print_it = print_it_done;
5148 /* Stop. */
5149 break;
5150 case WP_IGNORE:
5151 bs->print_it = print_it_noop;
5152 bs->stop = 0;
5153 break;
5154 case WP_VALUE_CHANGED:
5155 if (b->type == bp_read_watchpoint)
5156 {
5157 /* There are two cases to consider here:
5158
5159 1. We're watching the triggered memory for reads.
5160 In that case, trust the target, and always report
5161 the watchpoint hit to the user. Even though
5162 reads don't cause value changes, the value may
5163 have changed since the last time it was read, and
5164 since we're not trapping writes, we will not see
5165 those, and as such we should ignore our notion of
5166 old value.
5167
5168 2. We're watching the triggered memory for both
5169 reads and writes. There are two ways this may
5170 happen:
5171
5172 2.1. This is a target that can't break on data
5173 reads only, but can break on accesses (reads or
5174 writes), such as e.g., x86. We detect this case
5175 at the time we try to insert read watchpoints.
5176
5177 2.2. Otherwise, the target supports read
5178 watchpoints, but, the user set an access or write
5179 watchpoint watching the same memory as this read
5180 watchpoint.
5181
5182 If we're watching memory writes as well as reads,
5183 ignore watchpoint hits when we find that the
5184 value hasn't changed, as reads don't cause
5185 changes. This still gives false positives when
5186 the program writes the same value to memory as
5187 what there was already in memory (we will confuse
5188 it for a read), but it's much better than
5189 nothing. */
5190
5191 int other_write_watchpoint = 0;
5192
5193 if (bl->watchpoint_type == hw_read)
5194 {
5195 for (breakpoint *other_b : all_breakpoints ())
5196 if (other_b->type == bp_hardware_watchpoint
5197 || other_b->type == bp_access_watchpoint)
5198 {
5199 struct watchpoint *other_w =
5200 (struct watchpoint *) other_b;
5201
5202 if (other_w->watchpoint_triggered
5203 == watch_triggered_yes)
5204 {
5205 other_write_watchpoint = 1;
5206 break;
5207 }
5208 }
5209 }
5210
5211 if (other_write_watchpoint
5212 || bl->watchpoint_type == hw_access)
5213 {
5214 /* We're watching the same memory for writes,
5215 and the value changed since the last time we
5216 updated it, so this trap must be for a write.
5217 Ignore it. */
5218 bs->print_it = print_it_noop;
5219 bs->stop = 0;
5220 }
5221 }
5222 break;
5223 case WP_VALUE_NOT_CHANGED:
5224 if (b->type == bp_hardware_watchpoint
5225 || b->type == bp_watchpoint)
5226 {
5227 /* Don't stop: write watchpoints shouldn't fire if
5228 the value hasn't changed. */
5229 bs->print_it = print_it_noop;
5230 bs->stop = 0;
5231 }
5232 /* Stop. */
5233 break;
5234 default:
5235 /* Can't happen. */
5236 break;
5237 }
5238 }
5239 else /* must_check_value == 0 */
5240 {
5241 /* This is a case where some watchpoint(s) triggered, but
5242 not at the address of this watchpoint, or else no
5243 watchpoint triggered after all. So don't print
5244 anything for this watchpoint. */
5245 bs->print_it = print_it_noop;
5246 bs->stop = 0;
5247 }
5248 }
5249 }
5250
5251 /* For breakpoints that are currently marked as telling gdb to stop,
5252 check conditions (condition proper, frame, thread and ignore count)
5253 of breakpoint referred to by BS. If we should not stop for this
5254 breakpoint, set BS->stop to 0. */
5255
5256 static void
5257 bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
5258 {
5259 const struct bp_location *bl;
5260 struct breakpoint *b;
5261 /* Assume stop. */
5262 bool condition_result = true;
5263 struct expression *cond;
5264
5265 gdb_assert (bs->stop);
5266
5267 /* BS is built for existing struct breakpoint. */
5268 bl = bs->bp_location_at.get ();
5269 gdb_assert (bl != NULL);
5270 b = bs->breakpoint_at;
5271 gdb_assert (b != NULL);
5272
5273 /* Even if the target evaluated the condition on its end and notified GDB, we
5274 need to do so again since GDB does not know if we stopped due to a
5275 breakpoint or a single step breakpoint. */
5276
5277 if (frame_id_p (b->frame_id)
5278 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5279 {
5280 bs->stop = 0;
5281 return;
5282 }
5283
5284 /* If this is a thread/task-specific breakpoint, don't waste cpu
5285 evaluating the condition if this isn't the specified
5286 thread/task. */
5287 if ((b->thread != -1 && b->thread != thread->global_num)
5288 || (b->task != 0 && b->task != ada_get_task_number (thread)))
5289 {
5290 bs->stop = 0;
5291 return;
5292 }
5293
5294 /* Evaluate extension language breakpoints that have a "stop" method
5295 implemented. */
5296 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5297
5298 if (is_watchpoint (b))
5299 {
5300 struct watchpoint *w = (struct watchpoint *) b;
5301
5302 cond = w->cond_exp.get ();
5303 }
5304 else
5305 cond = bl->cond.get ();
5306
5307 if (cond && b->disposition != disp_del_at_next_stop)
5308 {
5309 int within_current_scope = 1;
5310 struct watchpoint * w;
5311
5312 /* We use value_mark and value_free_to_mark because it could
5313 be a long time before we return to the command level and
5314 call free_all_values. We can't call free_all_values
5315 because we might be in the middle of evaluating a
5316 function call. */
5317 struct value *mark = value_mark ();
5318
5319 if (is_watchpoint (b))
5320 w = (struct watchpoint *) b;
5321 else
5322 w = NULL;
5323
5324 /* Need to select the frame, with all that implies so that
5325 the conditions will have the right context. Because we
5326 use the frame, we will not see an inlined function's
5327 variables when we arrive at a breakpoint at the start
5328 of the inlined function; the current frame will be the
5329 call site. */
5330 if (w == NULL || w->cond_exp_valid_block == NULL)
5331 select_frame (get_current_frame ());
5332 else
5333 {
5334 struct frame_info *frame;
5335
5336 /* For local watchpoint expressions, which particular
5337 instance of a local is being watched matters, so we
5338 keep track of the frame to evaluate the expression
5339 in. To evaluate the condition however, it doesn't
5340 really matter which instantiation of the function
5341 where the condition makes sense triggers the
5342 watchpoint. This allows an expression like "watch
5343 global if q > 10" set in `func', catch writes to
5344 global on all threads that call `func', or catch
5345 writes on all recursive calls of `func' by a single
5346 thread. We simply always evaluate the condition in
5347 the innermost frame that's executing where it makes
5348 sense to evaluate the condition. It seems
5349 intuitive. */
5350 frame = block_innermost_frame (w->cond_exp_valid_block);
5351 if (frame != NULL)
5352 select_frame (frame);
5353 else
5354 within_current_scope = 0;
5355 }
5356 if (within_current_scope)
5357 {
5358 try
5359 {
5360 condition_result = breakpoint_cond_eval (cond);
5361 }
5362 catch (const gdb_exception &ex)
5363 {
5364 exception_fprintf (gdb_stderr, ex,
5365 "Error in testing breakpoint condition:\n");
5366 }
5367 }
5368 else
5369 {
5370 warning (_("Watchpoint condition cannot be tested "
5371 "in the current scope"));
5372 /* If we failed to set the right context for this
5373 watchpoint, unconditionally report it. */
5374 }
5375 /* FIXME-someday, should give breakpoint #. */
5376 value_free_to_mark (mark);
5377 }
5378
5379 if (cond && !condition_result)
5380 {
5381 bs->stop = 0;
5382 }
5383 else if (b->ignore_count > 0)
5384 {
5385 b->ignore_count--;
5386 bs->stop = 0;
5387 /* Increase the hit count even though we don't stop. */
5388 ++(b->hit_count);
5389 gdb::observers::breakpoint_modified.notify (b);
5390 }
5391 }
5392
5393 /* Returns true if we need to track moribund locations of LOC's type
5394 on the current target. */
5395
5396 static int
5397 need_moribund_for_location_type (struct bp_location *loc)
5398 {
5399 return ((loc->loc_type == bp_loc_software_breakpoint
5400 && !target_supports_stopped_by_sw_breakpoint ())
5401 || (loc->loc_type == bp_loc_hardware_breakpoint
5402 && !target_supports_stopped_by_hw_breakpoint ()));
5403 }
5404
5405 /* See breakpoint.h. */
5406
5407 bpstat
5408 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5409 const struct target_waitstatus *ws)
5410 {
5411 bpstat bs_head = NULL, *bs_link = &bs_head;
5412
5413 for (breakpoint *b : all_breakpoints ())
5414 {
5415 if (!breakpoint_enabled (b))
5416 continue;
5417
5418 for (bp_location *bl : b->locations ())
5419 {
5420 /* For hardware watchpoints, we look only at the first
5421 location. The watchpoint_check function will work on the
5422 entire expression, not the individual locations. For
5423 read watchpoints, the watchpoints_triggered function has
5424 checked all locations already. */
5425 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5426 break;
5427
5428 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
5429 continue;
5430
5431 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5432 continue;
5433
5434 /* Come here if it's a watchpoint, or if the break address
5435 matches. */
5436
5437 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5438 explain stop. */
5439
5440 /* Assume we stop. Should we find a watchpoint that is not
5441 actually triggered, or if the condition of the breakpoint
5442 evaluates as false, we'll reset 'stop' to 0. */
5443 bs->stop = 1;
5444 bs->print = 1;
5445
5446 /* If this is a scope breakpoint, mark the associated
5447 watchpoint as triggered so that we will handle the
5448 out-of-scope event. We'll get to the watchpoint next
5449 iteration. */
5450 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5451 {
5452 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5453
5454 w->watchpoint_triggered = watch_triggered_yes;
5455 }
5456 }
5457 }
5458
5459 /* Check if a moribund breakpoint explains the stop. */
5460 if (!target_supports_stopped_by_sw_breakpoint ()
5461 || !target_supports_stopped_by_hw_breakpoint ())
5462 {
5463 for (bp_location *loc : moribund_locations)
5464 {
5465 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5466 && need_moribund_for_location_type (loc))
5467 {
5468 bpstat bs = new bpstats (loc, &bs_link);
5469 /* For hits of moribund locations, we should just proceed. */
5470 bs->stop = 0;
5471 bs->print = 0;
5472 bs->print_it = print_it_noop;
5473 }
5474 }
5475 }
5476
5477 return bs_head;
5478 }
5479
5480 /* See breakpoint.h. */
5481
5482 bpstat
5483 bpstat_stop_status (const address_space *aspace,
5484 CORE_ADDR bp_addr, thread_info *thread,
5485 const struct target_waitstatus *ws,
5486 bpstat stop_chain)
5487 {
5488 struct breakpoint *b = NULL;
5489 /* First item of allocated bpstat's. */
5490 bpstat bs_head = stop_chain;
5491 bpstat bs;
5492 int need_remove_insert;
5493 int removed_any;
5494
5495 /* First, build the bpstat chain with locations that explain a
5496 target stop, while being careful to not set the target running,
5497 as that may invalidate locations (in particular watchpoint
5498 locations are recreated). Resuming will happen here with
5499 breakpoint conditions or watchpoint expressions that include
5500 inferior function calls. */
5501 if (bs_head == NULL)
5502 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5503
5504 /* A bit of special processing for shlib breakpoints. We need to
5505 process solib loading here, so that the lists of loaded and
5506 unloaded libraries are correct before we handle "catch load" and
5507 "catch unload". */
5508 for (bs = bs_head; bs != NULL; bs = bs->next)
5509 {
5510 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5511 {
5512 handle_solib_event ();
5513 break;
5514 }
5515 }
5516
5517 /* Now go through the locations that caused the target to stop, and
5518 check whether we're interested in reporting this stop to higher
5519 layers, or whether we should resume the target transparently. */
5520
5521 removed_any = 0;
5522
5523 for (bs = bs_head; bs != NULL; bs = bs->next)
5524 {
5525 if (!bs->stop)
5526 continue;
5527
5528 b = bs->breakpoint_at;
5529 b->ops->check_status (bs);
5530 if (bs->stop)
5531 {
5532 bpstat_check_breakpoint_conditions (bs, thread);
5533
5534 if (bs->stop)
5535 {
5536 ++(b->hit_count);
5537 gdb::observers::breakpoint_modified.notify (b);
5538
5539 /* We will stop here. */
5540 if (b->disposition == disp_disable)
5541 {
5542 --(b->enable_count);
5543 if (b->enable_count <= 0)
5544 b->enable_state = bp_disabled;
5545 removed_any = 1;
5546 }
5547 if (b->silent)
5548 bs->print = 0;
5549 bs->commands = b->commands;
5550 if (command_line_is_silent (bs->commands
5551 ? bs->commands.get () : NULL))
5552 bs->print = 0;
5553
5554 b->ops->after_condition_true (bs);
5555 }
5556
5557 }
5558
5559 /* Print nothing for this entry if we don't stop or don't
5560 print. */
5561 if (!bs->stop || !bs->print)
5562 bs->print_it = print_it_noop;
5563 }
5564
5565 /* If we aren't stopping, the value of some hardware watchpoint may
5566 not have changed, but the intermediate memory locations we are
5567 watching may have. Don't bother if we're stopping; this will get
5568 done later. */
5569 need_remove_insert = 0;
5570 if (! bpstat_causes_stop (bs_head))
5571 for (bs = bs_head; bs != NULL; bs = bs->next)
5572 if (!bs->stop
5573 && bs->breakpoint_at
5574 && is_hardware_watchpoint (bs->breakpoint_at))
5575 {
5576 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5577
5578 update_watchpoint (w, 0 /* don't reparse. */);
5579 need_remove_insert = 1;
5580 }
5581
5582 if (need_remove_insert)
5583 update_global_location_list (UGLL_MAY_INSERT);
5584 else if (removed_any)
5585 update_global_location_list (UGLL_DONT_INSERT);
5586
5587 return bs_head;
5588 }
5589
5590 static void
5591 handle_jit_event (CORE_ADDR address)
5592 {
5593 struct gdbarch *gdbarch;
5594
5595 infrun_debug_printf ("handling bp_jit_event");
5596
5597 /* Switch terminal for any messages produced by
5598 breakpoint_re_set. */
5599 target_terminal::ours_for_output ();
5600
5601 gdbarch = get_frame_arch (get_current_frame ());
5602 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5603 thus it is expected that its objectfile can be found through
5604 minimal symbol lookup. If it doesn't work (and assert fails), it
5605 most likely means that `jit_breakpoint_re_set` was changes and this
5606 function needs to be updated too. */
5607 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5608 gdb_assert (jit_bp_sym.objfile != nullptr);
5609 jit_event_handler (gdbarch, jit_bp_sym.objfile);
5610
5611 target_terminal::inferior ();
5612 }
5613
5614 /* Prepare WHAT final decision for infrun. */
5615
5616 /* Decide what infrun needs to do with this bpstat. */
5617
5618 struct bpstat_what
5619 bpstat_what (bpstat bs_head)
5620 {
5621 struct bpstat_what retval;
5622 bpstat bs;
5623
5624 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5625 retval.call_dummy = STOP_NONE;
5626 retval.is_longjmp = false;
5627
5628 for (bs = bs_head; bs != NULL; bs = bs->next)
5629 {
5630 /* Extract this BS's action. After processing each BS, we check
5631 if its action overrides all we've seem so far. */
5632 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5633 enum bptype bptype;
5634
5635 if (bs->breakpoint_at == NULL)
5636 {
5637 /* I suspect this can happen if it was a momentary
5638 breakpoint which has since been deleted. */
5639 bptype = bp_none;
5640 }
5641 else
5642 bptype = bs->breakpoint_at->type;
5643
5644 switch (bptype)
5645 {
5646 case bp_none:
5647 break;
5648 case bp_breakpoint:
5649 case bp_hardware_breakpoint:
5650 case bp_single_step:
5651 case bp_until:
5652 case bp_finish:
5653 case bp_shlib_event:
5654 if (bs->stop)
5655 {
5656 if (bs->print)
5657 this_action = BPSTAT_WHAT_STOP_NOISY;
5658 else
5659 this_action = BPSTAT_WHAT_STOP_SILENT;
5660 }
5661 else
5662 this_action = BPSTAT_WHAT_SINGLE;
5663 break;
5664 case bp_watchpoint:
5665 case bp_hardware_watchpoint:
5666 case bp_read_watchpoint:
5667 case bp_access_watchpoint:
5668 if (bs->stop)
5669 {
5670 if (bs->print)
5671 this_action = BPSTAT_WHAT_STOP_NOISY;
5672 else
5673 this_action = BPSTAT_WHAT_STOP_SILENT;
5674 }
5675 else
5676 {
5677 /* There was a watchpoint, but we're not stopping.
5678 This requires no further action. */
5679 }
5680 break;
5681 case bp_longjmp:
5682 case bp_longjmp_call_dummy:
5683 case bp_exception:
5684 if (bs->stop)
5685 {
5686 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5687 retval.is_longjmp = bptype != bp_exception;
5688 }
5689 else
5690 this_action = BPSTAT_WHAT_SINGLE;
5691 break;
5692 case bp_longjmp_resume:
5693 case bp_exception_resume:
5694 if (bs->stop)
5695 {
5696 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5697 retval.is_longjmp = bptype == bp_longjmp_resume;
5698 }
5699 else
5700 this_action = BPSTAT_WHAT_SINGLE;
5701 break;
5702 case bp_step_resume:
5703 if (bs->stop)
5704 this_action = BPSTAT_WHAT_STEP_RESUME;
5705 else
5706 {
5707 /* It is for the wrong frame. */
5708 this_action = BPSTAT_WHAT_SINGLE;
5709 }
5710 break;
5711 case bp_hp_step_resume:
5712 if (bs->stop)
5713 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5714 else
5715 {
5716 /* It is for the wrong frame. */
5717 this_action = BPSTAT_WHAT_SINGLE;
5718 }
5719 break;
5720 case bp_watchpoint_scope:
5721 case bp_thread_event:
5722 case bp_overlay_event:
5723 case bp_longjmp_master:
5724 case bp_std_terminate_master:
5725 case bp_exception_master:
5726 this_action = BPSTAT_WHAT_SINGLE;
5727 break;
5728 case bp_catchpoint:
5729 if (bs->stop)
5730 {
5731 if (bs->print)
5732 this_action = BPSTAT_WHAT_STOP_NOISY;
5733 else
5734 this_action = BPSTAT_WHAT_STOP_SILENT;
5735 }
5736 else
5737 {
5738 /* Some catchpoints are implemented with breakpoints.
5739 For those, we need to step over the breakpoint. */
5740 if (bs->bp_location_at->loc_type != bp_loc_other)
5741 this_action = BPSTAT_WHAT_SINGLE;
5742 }
5743 break;
5744 case bp_jit_event:
5745 this_action = BPSTAT_WHAT_SINGLE;
5746 break;
5747 case bp_call_dummy:
5748 /* Make sure the action is stop (silent or noisy),
5749 so infrun.c pops the dummy frame. */
5750 retval.call_dummy = STOP_STACK_DUMMY;
5751 this_action = BPSTAT_WHAT_STOP_SILENT;
5752 break;
5753 case bp_std_terminate:
5754 /* Make sure the action is stop (silent or noisy),
5755 so infrun.c pops the dummy frame. */
5756 retval.call_dummy = STOP_STD_TERMINATE;
5757 this_action = BPSTAT_WHAT_STOP_SILENT;
5758 break;
5759 case bp_tracepoint:
5760 case bp_fast_tracepoint:
5761 case bp_static_tracepoint:
5762 /* Tracepoint hits should not be reported back to GDB, and
5763 if one got through somehow, it should have been filtered
5764 out already. */
5765 internal_error (__FILE__, __LINE__,
5766 _("bpstat_what: tracepoint encountered"));
5767 break;
5768 case bp_gnu_ifunc_resolver:
5769 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5770 this_action = BPSTAT_WHAT_SINGLE;
5771 break;
5772 case bp_gnu_ifunc_resolver_return:
5773 /* The breakpoint will be removed, execution will restart from the
5774 PC of the former breakpoint. */
5775 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5776 break;
5777
5778 case bp_dprintf:
5779 if (bs->stop)
5780 this_action = BPSTAT_WHAT_STOP_SILENT;
5781 else
5782 this_action = BPSTAT_WHAT_SINGLE;
5783 break;
5784
5785 default:
5786 internal_error (__FILE__, __LINE__,
5787 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5788 }
5789
5790 retval.main_action = std::max (retval.main_action, this_action);
5791 }
5792
5793 return retval;
5794 }
5795
5796 void
5797 bpstat_run_callbacks (bpstat bs_head)
5798 {
5799 bpstat bs;
5800
5801 for (bs = bs_head; bs != NULL; bs = bs->next)
5802 {
5803 struct breakpoint *b = bs->breakpoint_at;
5804
5805 if (b == NULL)
5806 continue;
5807 switch (b->type)
5808 {
5809 case bp_jit_event:
5810 handle_jit_event (bs->bp_location_at->address);
5811 break;
5812 case bp_gnu_ifunc_resolver:
5813 gnu_ifunc_resolver_stop (b);
5814 break;
5815 case bp_gnu_ifunc_resolver_return:
5816 gnu_ifunc_resolver_return_stop (b);
5817 break;
5818 }
5819 }
5820 }
5821
5822 /* See breakpoint.h. */
5823
5824 bool
5825 bpstat_should_step ()
5826 {
5827 for (breakpoint *b : all_breakpoints ())
5828 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5829 return true;
5830
5831 return false;
5832 }
5833
5834 /* See breakpoint.h. */
5835
5836 bool
5837 bpstat_causes_stop (bpstat bs)
5838 {
5839 for (; bs != NULL; bs = bs->next)
5840 if (bs->stop)
5841 return true;
5842
5843 return false;
5844 }
5845
5846 \f
5847
5848 /* Compute a string of spaces suitable to indent the next line
5849 so it starts at the position corresponding to the table column
5850 named COL_NAME in the currently active table of UIOUT. */
5851
5852 static char *
5853 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5854 {
5855 static char wrap_indent[80];
5856 int i, total_width, width, align;
5857 const char *text;
5858
5859 total_width = 0;
5860 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5861 {
5862 if (strcmp (text, col_name) == 0)
5863 {
5864 gdb_assert (total_width < sizeof wrap_indent);
5865 memset (wrap_indent, ' ', total_width);
5866 wrap_indent[total_width] = 0;
5867
5868 return wrap_indent;
5869 }
5870
5871 total_width += width + 1;
5872 }
5873
5874 return NULL;
5875 }
5876
5877 /* Determine if the locations of this breakpoint will have their conditions
5878 evaluated by the target, host or a mix of both. Returns the following:
5879
5880 "host": Host evals condition.
5881 "host or target": Host or Target evals condition.
5882 "target": Target evals condition.
5883 */
5884
5885 static const char *
5886 bp_condition_evaluator (struct breakpoint *b)
5887 {
5888 char host_evals = 0;
5889 char target_evals = 0;
5890
5891 if (!b)
5892 return NULL;
5893
5894 if (!is_breakpoint (b))
5895 return NULL;
5896
5897 if (gdb_evaluates_breakpoint_condition_p ()
5898 || !target_supports_evaluation_of_breakpoint_conditions ())
5899 return condition_evaluation_host;
5900
5901 for (bp_location *bl : b->locations ())
5902 {
5903 if (bl->cond_bytecode)
5904 target_evals++;
5905 else
5906 host_evals++;
5907 }
5908
5909 if (host_evals && target_evals)
5910 return condition_evaluation_both;
5911 else if (target_evals)
5912 return condition_evaluation_target;
5913 else
5914 return condition_evaluation_host;
5915 }
5916
5917 /* Determine the breakpoint location's condition evaluator. This is
5918 similar to bp_condition_evaluator, but for locations. */
5919
5920 static const char *
5921 bp_location_condition_evaluator (struct bp_location *bl)
5922 {
5923 if (bl && !is_breakpoint (bl->owner))
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 if (bl && bl->cond_bytecode)
5931 return condition_evaluation_target;
5932 else
5933 return condition_evaluation_host;
5934 }
5935
5936 /* Print the LOC location out of the list of B->LOC locations. */
5937
5938 static void
5939 print_breakpoint_location (struct breakpoint *b,
5940 struct bp_location *loc)
5941 {
5942 struct ui_out *uiout = current_uiout;
5943
5944 scoped_restore_current_program_space restore_pspace;
5945
5946 if (loc != NULL && loc->shlib_disabled)
5947 loc = NULL;
5948
5949 if (loc != NULL)
5950 set_current_program_space (loc->pspace);
5951
5952 if (b->display_canonical)
5953 uiout->field_string ("what", event_location_to_string (b->location.get ()));
5954 else if (loc && loc->symtab)
5955 {
5956 const struct symbol *sym = loc->symbol;
5957
5958 if (sym)
5959 {
5960 uiout->text ("in ");
5961 uiout->field_string ("func", sym->print_name (),
5962 function_name_style.style ());
5963 uiout->text (" ");
5964 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5965 uiout->text ("at ");
5966 }
5967 uiout->field_string ("file",
5968 symtab_to_filename_for_display (loc->symtab),
5969 file_name_style.style ());
5970 uiout->text (":");
5971
5972 if (uiout->is_mi_like_p ())
5973 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
5974
5975 uiout->field_signed ("line", loc->line_number);
5976 }
5977 else if (loc)
5978 {
5979 string_file stb;
5980
5981 print_address_symbolic (loc->gdbarch, loc->address, &stb,
5982 demangle, "");
5983 uiout->field_stream ("at", stb);
5984 }
5985 else
5986 {
5987 uiout->field_string ("pending",
5988 event_location_to_string (b->location.get ()));
5989 /* If extra_string is available, it could be holding a condition
5990 or dprintf arguments. In either case, make sure it is printed,
5991 too, but only for non-MI streams. */
5992 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
5993 {
5994 if (b->type == bp_dprintf)
5995 uiout->text (",");
5996 else
5997 uiout->text (" ");
5998 uiout->text (b->extra_string);
5999 }
6000 }
6001
6002 if (loc && is_breakpoint (b)
6003 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6004 && bp_condition_evaluator (b) == condition_evaluation_both)
6005 {
6006 uiout->text (" (");
6007 uiout->field_string ("evaluated-by",
6008 bp_location_condition_evaluator (loc));
6009 uiout->text (")");
6010 }
6011 }
6012
6013 static const char *
6014 bptype_string (enum bptype type)
6015 {
6016 struct ep_type_description
6017 {
6018 enum bptype type;
6019 const char *description;
6020 };
6021 static struct ep_type_description bptypes[] =
6022 {
6023 {bp_none, "?deleted?"},
6024 {bp_breakpoint, "breakpoint"},
6025 {bp_hardware_breakpoint, "hw breakpoint"},
6026 {bp_single_step, "sw single-step"},
6027 {bp_until, "until"},
6028 {bp_finish, "finish"},
6029 {bp_watchpoint, "watchpoint"},
6030 {bp_hardware_watchpoint, "hw watchpoint"},
6031 {bp_read_watchpoint, "read watchpoint"},
6032 {bp_access_watchpoint, "acc watchpoint"},
6033 {bp_longjmp, "longjmp"},
6034 {bp_longjmp_resume, "longjmp resume"},
6035 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6036 {bp_exception, "exception"},
6037 {bp_exception_resume, "exception resume"},
6038 {bp_step_resume, "step resume"},
6039 {bp_hp_step_resume, "high-priority step resume"},
6040 {bp_watchpoint_scope, "watchpoint scope"},
6041 {bp_call_dummy, "call dummy"},
6042 {bp_std_terminate, "std::terminate"},
6043 {bp_shlib_event, "shlib events"},
6044 {bp_thread_event, "thread events"},
6045 {bp_overlay_event, "overlay events"},
6046 {bp_longjmp_master, "longjmp master"},
6047 {bp_std_terminate_master, "std::terminate master"},
6048 {bp_exception_master, "exception master"},
6049 {bp_catchpoint, "catchpoint"},
6050 {bp_tracepoint, "tracepoint"},
6051 {bp_fast_tracepoint, "fast tracepoint"},
6052 {bp_static_tracepoint, "static tracepoint"},
6053 {bp_dprintf, "dprintf"},
6054 {bp_jit_event, "jit events"},
6055 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6056 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6057 };
6058
6059 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6060 || ((int) type != bptypes[(int) type].type))
6061 internal_error (__FILE__, __LINE__,
6062 _("bptypes table does not describe type #%d."),
6063 (int) type);
6064
6065 return bptypes[(int) type].description;
6066 }
6067
6068 /* For MI, output a field named 'thread-groups' with a list as the value.
6069 For CLI, prefix the list with the string 'inf'. */
6070
6071 static void
6072 output_thread_groups (struct ui_out *uiout,
6073 const char *field_name,
6074 const std::vector<int> &inf_nums,
6075 int mi_only)
6076 {
6077 int is_mi = uiout->is_mi_like_p ();
6078
6079 /* For backward compatibility, don't display inferiors in CLI unless
6080 there are several. Always display them for MI. */
6081 if (!is_mi && mi_only)
6082 return;
6083
6084 ui_out_emit_list list_emitter (uiout, field_name);
6085
6086 for (size_t i = 0; i < inf_nums.size (); i++)
6087 {
6088 if (is_mi)
6089 {
6090 char mi_group[10];
6091
6092 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
6093 uiout->field_string (NULL, mi_group);
6094 }
6095 else
6096 {
6097 if (i == 0)
6098 uiout->text (" inf ");
6099 else
6100 uiout->text (", ");
6101
6102 uiout->text (plongest (inf_nums[i]));
6103 }
6104 }
6105 }
6106
6107 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6108 instead of going via breakpoint_ops::print_one. This makes "maint
6109 info breakpoints" show the software breakpoint locations of
6110 catchpoints, which are considered internal implementation
6111 detail. */
6112
6113 static void
6114 print_one_breakpoint_location (struct breakpoint *b,
6115 struct bp_location *loc,
6116 int loc_number,
6117 struct bp_location **last_loc,
6118 int allflag, bool raw_loc)
6119 {
6120 struct command_line *l;
6121 static char bpenables[] = "nynny";
6122
6123 struct ui_out *uiout = current_uiout;
6124 int header_of_multiple = 0;
6125 int part_of_multiple = (loc != NULL);
6126 struct value_print_options opts;
6127
6128 get_user_print_options (&opts);
6129
6130 gdb_assert (!loc || loc_number != 0);
6131 /* See comment in print_one_breakpoint concerning treatment of
6132 breakpoints with single disabled location. */
6133 if (loc == NULL
6134 && (b->loc != NULL
6135 && (b->loc->next != NULL
6136 || !b->loc->enabled || b->loc->disabled_by_cond)))
6137 header_of_multiple = 1;
6138 if (loc == NULL)
6139 loc = b->loc;
6140
6141 annotate_record ();
6142
6143 /* 1 */
6144 annotate_field (0);
6145 if (part_of_multiple)
6146 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6147 else
6148 uiout->field_signed ("number", b->number);
6149
6150 /* 2 */
6151 annotate_field (1);
6152 if (part_of_multiple)
6153 uiout->field_skip ("type");
6154 else
6155 uiout->field_string ("type", bptype_string (b->type));
6156
6157 /* 3 */
6158 annotate_field (2);
6159 if (part_of_multiple)
6160 uiout->field_skip ("disp");
6161 else
6162 uiout->field_string ("disp", bpdisp_text (b->disposition));
6163
6164 /* 4 */
6165 annotate_field (3);
6166 /* For locations that are disabled because of an invalid condition,
6167 display "N*" on CLI, where "*" refers to a footnote below the
6168 table. For MI, simply display a "N" without a footnote. */
6169 const char *N = (uiout->is_mi_like_p ()) ? "N" : "N*";
6170 if (part_of_multiple)
6171 uiout->field_string ("enabled", (loc->disabled_by_cond ? N
6172 : (loc->enabled ? "y" : "n")));
6173 else
6174 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6175
6176 /* 5 and 6 */
6177 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
6178 b->ops->print_one (b, last_loc);
6179 else
6180 {
6181 if (is_watchpoint (b))
6182 {
6183 struct watchpoint *w = (struct watchpoint *) b;
6184
6185 /* Field 4, the address, is omitted (which makes the columns
6186 not line up too nicely with the headers, but the effect
6187 is relatively readable). */
6188 if (opts.addressprint)
6189 uiout->field_skip ("addr");
6190 annotate_field (5);
6191 uiout->field_string ("what", w->exp_string);
6192 }
6193 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6194 || is_ada_exception_catchpoint (b))
6195 {
6196 if (opts.addressprint)
6197 {
6198 annotate_field (4);
6199 if (header_of_multiple)
6200 uiout->field_string ("addr", "<MULTIPLE>",
6201 metadata_style.style ());
6202 else if (b->loc == NULL || loc->shlib_disabled)
6203 uiout->field_string ("addr", "<PENDING>",
6204 metadata_style.style ());
6205 else
6206 uiout->field_core_addr ("addr",
6207 loc->gdbarch, loc->address);
6208 }
6209 annotate_field (5);
6210 if (!header_of_multiple)
6211 print_breakpoint_location (b, loc);
6212 if (b->loc)
6213 *last_loc = b->loc;
6214 }
6215 }
6216
6217 if (loc != NULL && !header_of_multiple)
6218 {
6219 std::vector<int> inf_nums;
6220 int mi_only = 1;
6221
6222 for (inferior *inf : all_inferiors ())
6223 {
6224 if (inf->pspace == loc->pspace)
6225 inf_nums.push_back (inf->num);
6226 }
6227
6228 /* For backward compatibility, don't display inferiors in CLI unless
6229 there are several. Always display for MI. */
6230 if (allflag
6231 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6232 && (program_spaces.size () > 1
6233 || number_of_inferiors () > 1)
6234 /* LOC is for existing B, it cannot be in
6235 moribund_locations and thus having NULL OWNER. */
6236 && loc->owner->type != bp_catchpoint))
6237 mi_only = 0;
6238 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6239 }
6240
6241 if (!part_of_multiple)
6242 {
6243 if (b->thread != -1)
6244 {
6245 /* FIXME: This seems to be redundant and lost here; see the
6246 "stop only in" line a little further down. */
6247 uiout->text (" thread ");
6248 uiout->field_signed ("thread", b->thread);
6249 }
6250 else if (b->task != 0)
6251 {
6252 uiout->text (" task ");
6253 uiout->field_signed ("task", b->task);
6254 }
6255 }
6256
6257 uiout->text ("\n");
6258
6259 if (!part_of_multiple)
6260 b->ops->print_one_detail (b, uiout);
6261
6262 if (part_of_multiple && frame_id_p (b->frame_id))
6263 {
6264 annotate_field (6);
6265 uiout->text ("\tstop only in stack frame at ");
6266 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6267 the frame ID. */
6268 uiout->field_core_addr ("frame",
6269 b->gdbarch, b->frame_id.stack_addr);
6270 uiout->text ("\n");
6271 }
6272
6273 if (!part_of_multiple && b->cond_string)
6274 {
6275 annotate_field (7);
6276 if (is_tracepoint (b))
6277 uiout->text ("\ttrace only if ");
6278 else
6279 uiout->text ("\tstop only if ");
6280 uiout->field_string ("cond", b->cond_string);
6281
6282 /* Print whether the target is doing the breakpoint's condition
6283 evaluation. If GDB is doing the evaluation, don't print anything. */
6284 if (is_breakpoint (b)
6285 && breakpoint_condition_evaluation_mode ()
6286 == condition_evaluation_target)
6287 {
6288 uiout->message (" (%pF evals)",
6289 string_field ("evaluated-by",
6290 bp_condition_evaluator (b)));
6291 }
6292 uiout->text ("\n");
6293 }
6294
6295 if (!part_of_multiple && b->thread != -1)
6296 {
6297 /* FIXME should make an annotation for this. */
6298 uiout->text ("\tstop only in thread ");
6299 if (uiout->is_mi_like_p ())
6300 uiout->field_signed ("thread", b->thread);
6301 else
6302 {
6303 struct thread_info *thr = find_thread_global_id (b->thread);
6304
6305 uiout->field_string ("thread", print_thread_id (thr));
6306 }
6307 uiout->text ("\n");
6308 }
6309
6310 if (!part_of_multiple)
6311 {
6312 if (b->hit_count)
6313 {
6314 /* FIXME should make an annotation for this. */
6315 if (is_catchpoint (b))
6316 uiout->text ("\tcatchpoint");
6317 else if (is_tracepoint (b))
6318 uiout->text ("\ttracepoint");
6319 else
6320 uiout->text ("\tbreakpoint");
6321 uiout->text (" already hit ");
6322 uiout->field_signed ("times", b->hit_count);
6323 if (b->hit_count == 1)
6324 uiout->text (" time\n");
6325 else
6326 uiout->text (" times\n");
6327 }
6328 else
6329 {
6330 /* Output the count also if it is zero, but only if this is mi. */
6331 if (uiout->is_mi_like_p ())
6332 uiout->field_signed ("times", b->hit_count);
6333 }
6334 }
6335
6336 if (!part_of_multiple && b->ignore_count)
6337 {
6338 annotate_field (8);
6339 uiout->message ("\tignore next %pF hits\n",
6340 signed_field ("ignore", b->ignore_count));
6341 }
6342
6343 /* Note that an enable count of 1 corresponds to "enable once"
6344 behavior, which is reported by the combination of enablement and
6345 disposition, so we don't need to mention it here. */
6346 if (!part_of_multiple && b->enable_count > 1)
6347 {
6348 annotate_field (8);
6349 uiout->text ("\tdisable after ");
6350 /* Tweak the wording to clarify that ignore and enable counts
6351 are distinct, and have additive effect. */
6352 if (b->ignore_count)
6353 uiout->text ("additional ");
6354 else
6355 uiout->text ("next ");
6356 uiout->field_signed ("enable", b->enable_count);
6357 uiout->text (" hits\n");
6358 }
6359
6360 if (!part_of_multiple && is_tracepoint (b))
6361 {
6362 struct tracepoint *tp = (struct tracepoint *) b;
6363
6364 if (tp->traceframe_usage)
6365 {
6366 uiout->text ("\ttrace buffer usage ");
6367 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6368 uiout->text (" bytes\n");
6369 }
6370 }
6371
6372 l = b->commands ? b->commands.get () : NULL;
6373 if (!part_of_multiple && l)
6374 {
6375 annotate_field (9);
6376 ui_out_emit_tuple tuple_emitter (uiout, "script");
6377 print_command_lines (uiout, l, 4);
6378 }
6379
6380 if (is_tracepoint (b))
6381 {
6382 struct tracepoint *t = (struct tracepoint *) b;
6383
6384 if (!part_of_multiple && t->pass_count)
6385 {
6386 annotate_field (10);
6387 uiout->text ("\tpass count ");
6388 uiout->field_signed ("pass", t->pass_count);
6389 uiout->text (" \n");
6390 }
6391
6392 /* Don't display it when tracepoint or tracepoint location is
6393 pending. */
6394 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6395 {
6396 annotate_field (11);
6397
6398 if (uiout->is_mi_like_p ())
6399 uiout->field_string ("installed",
6400 loc->inserted ? "y" : "n");
6401 else
6402 {
6403 if (loc->inserted)
6404 uiout->text ("\t");
6405 else
6406 uiout->text ("\tnot ");
6407 uiout->text ("installed on target\n");
6408 }
6409 }
6410 }
6411
6412 if (uiout->is_mi_like_p () && !part_of_multiple)
6413 {
6414 if (is_watchpoint (b))
6415 {
6416 struct watchpoint *w = (struct watchpoint *) b;
6417
6418 uiout->field_string ("original-location", w->exp_string);
6419 }
6420 else if (b->location != NULL
6421 && event_location_to_string (b->location.get ()) != NULL)
6422 uiout->field_string ("original-location",
6423 event_location_to_string (b->location.get ()));
6424 }
6425 }
6426
6427 /* See breakpoint.h. */
6428
6429 bool fix_multi_location_breakpoint_output_globally = false;
6430
6431 static void
6432 print_one_breakpoint (struct breakpoint *b,
6433 struct bp_location **last_loc,
6434 int allflag)
6435 {
6436 struct ui_out *uiout = current_uiout;
6437 bool use_fixed_output
6438 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6439 || fix_multi_location_breakpoint_output_globally);
6440
6441 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6442 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
6443
6444 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6445 are outside. */
6446 if (!use_fixed_output)
6447 bkpt_tuple_emitter.reset ();
6448
6449 /* If this breakpoint has custom print function,
6450 it's already printed. Otherwise, print individual
6451 locations, if any. */
6452 if (b->ops == NULL
6453 || b->ops->print_one == NULL
6454 || allflag)
6455 {
6456 /* If breakpoint has a single location that is disabled, we
6457 print it as if it had several locations, since otherwise it's
6458 hard to represent "breakpoint enabled, location disabled"
6459 situation.
6460
6461 Note that while hardware watchpoints have several locations
6462 internally, that's not a property exposed to users.
6463
6464 Likewise, while catchpoints may be implemented with
6465 breakpoints (e.g., catch throw), that's not a property
6466 exposed to users. We do however display the internal
6467 breakpoint locations with "maint info breakpoints". */
6468 if (!is_hardware_watchpoint (b)
6469 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6470 || is_ada_exception_catchpoint (b))
6471 && (allflag
6472 || (b->loc && (b->loc->next
6473 || !b->loc->enabled
6474 || b->loc->disabled_by_cond))))
6475 {
6476 gdb::optional<ui_out_emit_list> locations_list;
6477
6478 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6479 MI record. For later versions, place breakpoint locations in a
6480 list. */
6481 if (uiout->is_mi_like_p () && use_fixed_output)
6482 locations_list.emplace (uiout, "locations");
6483
6484 int n = 1;
6485 for (bp_location *loc : b->locations ())
6486 {
6487 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6488 print_one_breakpoint_location (b, loc, n, last_loc,
6489 allflag, allflag);
6490 n++;
6491 }
6492 }
6493 }
6494 }
6495
6496 static int
6497 breakpoint_address_bits (struct breakpoint *b)
6498 {
6499 int print_address_bits = 0;
6500
6501 /* Software watchpoints that aren't watching memory don't have an
6502 address to print. */
6503 if (is_no_memory_software_watchpoint (b))
6504 return 0;
6505
6506 for (bp_location *loc : b->locations ())
6507 {
6508 int addr_bit;
6509
6510 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6511 if (addr_bit > print_address_bits)
6512 print_address_bits = addr_bit;
6513 }
6514
6515 return print_address_bits;
6516 }
6517
6518 /* See breakpoint.h. */
6519
6520 void
6521 print_breakpoint (breakpoint *b)
6522 {
6523 struct bp_location *dummy_loc = NULL;
6524 print_one_breakpoint (b, &dummy_loc, 0);
6525 }
6526
6527 /* Return true if this breakpoint was set by the user, false if it is
6528 internal or momentary. */
6529
6530 int
6531 user_breakpoint_p (struct breakpoint *b)
6532 {
6533 return b->number > 0;
6534 }
6535
6536 /* See breakpoint.h. */
6537
6538 int
6539 pending_breakpoint_p (struct breakpoint *b)
6540 {
6541 return b->loc == NULL;
6542 }
6543
6544 /* Print information on breakpoints (including watchpoints and tracepoints).
6545
6546 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6547 understood by number_or_range_parser. Only breakpoints included in this
6548 list are then printed.
6549
6550 If SHOW_INTERNAL is true, print internal breakpoints.
6551
6552 If FILTER is non-NULL, call it on each breakpoint and only include the
6553 ones for which it returns true.
6554
6555 Return the total number of breakpoints listed. */
6556
6557 static int
6558 breakpoint_1 (const char *bp_num_list, bool show_internal,
6559 bool (*filter) (const struct breakpoint *))
6560 {
6561 struct bp_location *last_loc = NULL;
6562 int nr_printable_breakpoints;
6563 struct value_print_options opts;
6564 int print_address_bits = 0;
6565 int print_type_col_width = 14;
6566 struct ui_out *uiout = current_uiout;
6567 bool has_disabled_by_cond_location = false;
6568
6569 get_user_print_options (&opts);
6570
6571 /* Compute the number of rows in the table, as well as the size
6572 required for address fields. */
6573 nr_printable_breakpoints = 0;
6574 for (breakpoint *b : all_breakpoints ())
6575 {
6576 /* If we have a filter, only list the breakpoints it accepts. */
6577 if (filter && !filter (b))
6578 continue;
6579
6580 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6581 accept. Skip the others. */
6582 if (bp_num_list != NULL && *bp_num_list != '\0')
6583 {
6584 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6585 continue;
6586 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6587 continue;
6588 }
6589
6590 if (show_internal || user_breakpoint_p (b))
6591 {
6592 int addr_bit, type_len;
6593
6594 addr_bit = breakpoint_address_bits (b);
6595 if (addr_bit > print_address_bits)
6596 print_address_bits = addr_bit;
6597
6598 type_len = strlen (bptype_string (b->type));
6599 if (type_len > print_type_col_width)
6600 print_type_col_width = type_len;
6601
6602 nr_printable_breakpoints++;
6603 }
6604 }
6605
6606 {
6607 ui_out_emit_table table_emitter (uiout,
6608 opts.addressprint ? 6 : 5,
6609 nr_printable_breakpoints,
6610 "BreakpointTable");
6611
6612 if (nr_printable_breakpoints > 0)
6613 annotate_breakpoints_headers ();
6614 if (nr_printable_breakpoints > 0)
6615 annotate_field (0);
6616 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6617 if (nr_printable_breakpoints > 0)
6618 annotate_field (1);
6619 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6620 if (nr_printable_breakpoints > 0)
6621 annotate_field (2);
6622 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6623 if (nr_printable_breakpoints > 0)
6624 annotate_field (3);
6625 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6626 if (opts.addressprint)
6627 {
6628 if (nr_printable_breakpoints > 0)
6629 annotate_field (4);
6630 if (print_address_bits <= 32)
6631 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6632 else
6633 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6634 }
6635 if (nr_printable_breakpoints > 0)
6636 annotate_field (5);
6637 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6638 uiout->table_body ();
6639 if (nr_printable_breakpoints > 0)
6640 annotate_breakpoints_table ();
6641
6642 for (breakpoint *b : all_breakpoints ())
6643 {
6644 QUIT;
6645 /* If we have a filter, only list the breakpoints it accepts. */
6646 if (filter && !filter (b))
6647 continue;
6648
6649 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6650 accept. Skip the others. */
6651
6652 if (bp_num_list != NULL && *bp_num_list != '\0')
6653 {
6654 if (show_internal) /* maintenance info breakpoint */
6655 {
6656 if (parse_and_eval_long (bp_num_list) != b->number)
6657 continue;
6658 }
6659 else /* all others */
6660 {
6661 if (!number_is_in_list (bp_num_list, b->number))
6662 continue;
6663 }
6664 }
6665 /* We only print out user settable breakpoints unless the
6666 show_internal is set. */
6667 if (show_internal || user_breakpoint_p (b))
6668 {
6669 print_one_breakpoint (b, &last_loc, show_internal);
6670 for (bp_location *loc : b->locations ())
6671 if (loc->disabled_by_cond)
6672 has_disabled_by_cond_location = true;
6673 }
6674 }
6675 }
6676
6677 if (nr_printable_breakpoints == 0)
6678 {
6679 /* If there's a filter, let the caller decide how to report
6680 empty list. */
6681 if (!filter)
6682 {
6683 if (bp_num_list == NULL || *bp_num_list == '\0')
6684 uiout->message ("No breakpoints or watchpoints.\n");
6685 else
6686 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6687 bp_num_list);
6688 }
6689 }
6690 else
6691 {
6692 if (last_loc && !server_command)
6693 set_next_address (last_loc->gdbarch, last_loc->address);
6694
6695 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
6696 uiout->message (_("(*): Breakpoint condition is invalid at this "
6697 "location.\n"));
6698 }
6699
6700 /* FIXME? Should this be moved up so that it is only called when
6701 there have been breakpoints? */
6702 annotate_breakpoints_table_end ();
6703
6704 return nr_printable_breakpoints;
6705 }
6706
6707 /* Display the value of default-collect in a way that is generally
6708 compatible with the breakpoint list. */
6709
6710 static void
6711 default_collect_info (void)
6712 {
6713 struct ui_out *uiout = current_uiout;
6714
6715 /* If it has no value (which is frequently the case), say nothing; a
6716 message like "No default-collect." gets in user's face when it's
6717 not wanted. */
6718 if (!*default_collect)
6719 return;
6720
6721 /* The following phrase lines up nicely with per-tracepoint collect
6722 actions. */
6723 uiout->text ("default collect ");
6724 uiout->field_string ("default-collect", default_collect);
6725 uiout->text (" \n");
6726 }
6727
6728 static void
6729 info_breakpoints_command (const char *args, int from_tty)
6730 {
6731 breakpoint_1 (args, false, NULL);
6732
6733 default_collect_info ();
6734 }
6735
6736 static void
6737 info_watchpoints_command (const char *args, int from_tty)
6738 {
6739 int num_printed = breakpoint_1 (args, false, is_watchpoint);
6740 struct ui_out *uiout = current_uiout;
6741
6742 if (num_printed == 0)
6743 {
6744 if (args == NULL || *args == '\0')
6745 uiout->message ("No watchpoints.\n");
6746 else
6747 uiout->message ("No watchpoint matching '%s'.\n", args);
6748 }
6749 }
6750
6751 static void
6752 maintenance_info_breakpoints (const char *args, int from_tty)
6753 {
6754 breakpoint_1 (args, true, NULL);
6755
6756 default_collect_info ();
6757 }
6758
6759 static int
6760 breakpoint_has_pc (struct breakpoint *b,
6761 struct program_space *pspace,
6762 CORE_ADDR pc, struct obj_section *section)
6763 {
6764 for (bp_location *bl : b->locations ())
6765 {
6766 if (bl->pspace == pspace
6767 && bl->address == pc
6768 && (!overlay_debugging || bl->section == section))
6769 return 1;
6770 }
6771 return 0;
6772 }
6773
6774 /* Print a message describing any user-breakpoints set at PC. This
6775 concerns with logical breakpoints, so we match program spaces, not
6776 address spaces. */
6777
6778 static void
6779 describe_other_breakpoints (struct gdbarch *gdbarch,
6780 struct program_space *pspace, CORE_ADDR pc,
6781 struct obj_section *section, int thread)
6782 {
6783 int others = 0;
6784
6785 for (breakpoint *b : all_breakpoints ())
6786 others += (user_breakpoint_p (b)
6787 && breakpoint_has_pc (b, pspace, pc, section));
6788
6789 if (others > 0)
6790 {
6791 if (others == 1)
6792 printf_filtered (_("Note: breakpoint "));
6793 else /* if (others == ???) */
6794 printf_filtered (_("Note: breakpoints "));
6795 for (breakpoint *b : all_breakpoints ())
6796 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6797 {
6798 others--;
6799 printf_filtered ("%d", b->number);
6800 if (b->thread == -1 && thread != -1)
6801 printf_filtered (" (all threads)");
6802 else if (b->thread != -1)
6803 printf_filtered (" (thread %d)", b->thread);
6804 printf_filtered ("%s%s ",
6805 ((b->enable_state == bp_disabled
6806 || b->enable_state == bp_call_disabled)
6807 ? " (disabled)"
6808 : ""),
6809 (others > 1) ? ","
6810 : ((others == 1) ? " and" : ""));
6811 }
6812 current_uiout->message (_("also set at pc %ps.\n"),
6813 styled_string (address_style.style (),
6814 paddress (gdbarch, pc)));
6815 }
6816 }
6817 \f
6818
6819 /* Return true iff it is meaningful to use the address member of LOC.
6820 For some breakpoint types, the locations' address members are
6821 irrelevant and it makes no sense to attempt to compare them to
6822 other addresses (or use them for any other purpose either).
6823
6824 More specifically, software watchpoints and catchpoints that are
6825 not backed by breakpoints always have a zero valued location
6826 address and we don't want to mark breakpoints of any of these types
6827 to be a duplicate of an actual breakpoint location at address
6828 zero. */
6829
6830 static bool
6831 bl_address_is_meaningful (bp_location *loc)
6832 {
6833 return loc->loc_type != bp_loc_other;
6834 }
6835
6836 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6837 true if LOC1 and LOC2 represent the same watchpoint location. */
6838
6839 static int
6840 watchpoint_locations_match (struct bp_location *loc1,
6841 struct bp_location *loc2)
6842 {
6843 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6844 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6845
6846 /* Both of them must exist. */
6847 gdb_assert (w1 != NULL);
6848 gdb_assert (w2 != NULL);
6849
6850 /* If the target can evaluate the condition expression in hardware,
6851 then we we need to insert both watchpoints even if they are at
6852 the same place. Otherwise the watchpoint will only trigger when
6853 the condition of whichever watchpoint was inserted evaluates to
6854 true, not giving a chance for GDB to check the condition of the
6855 other watchpoint. */
6856 if ((w1->cond_exp
6857 && target_can_accel_watchpoint_condition (loc1->address,
6858 loc1->length,
6859 loc1->watchpoint_type,
6860 w1->cond_exp.get ()))
6861 || (w2->cond_exp
6862 && target_can_accel_watchpoint_condition (loc2->address,
6863 loc2->length,
6864 loc2->watchpoint_type,
6865 w2->cond_exp.get ())))
6866 return 0;
6867
6868 /* Note that this checks the owner's type, not the location's. In
6869 case the target does not support read watchpoints, but does
6870 support access watchpoints, we'll have bp_read_watchpoint
6871 watchpoints with hw_access locations. Those should be considered
6872 duplicates of hw_read locations. The hw_read locations will
6873 become hw_access locations later. */
6874 return (loc1->owner->type == loc2->owner->type
6875 && loc1->pspace->aspace == loc2->pspace->aspace
6876 && loc1->address == loc2->address
6877 && loc1->length == loc2->length);
6878 }
6879
6880 /* See breakpoint.h. */
6881
6882 int
6883 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6884 const address_space *aspace2, CORE_ADDR addr2)
6885 {
6886 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6887 || aspace1 == aspace2)
6888 && addr1 == addr2);
6889 }
6890
6891 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6892 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6893 matches ASPACE2. On targets that have global breakpoints, the address
6894 space doesn't really matter. */
6895
6896 static int
6897 breakpoint_address_match_range (const address_space *aspace1,
6898 CORE_ADDR addr1,
6899 int len1, const address_space *aspace2,
6900 CORE_ADDR addr2)
6901 {
6902 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6903 || aspace1 == aspace2)
6904 && addr2 >= addr1 && addr2 < addr1 + len1);
6905 }
6906
6907 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6908 a ranged breakpoint. In most targets, a match happens only if ASPACE
6909 matches the breakpoint's address space. On targets that have global
6910 breakpoints, the address space doesn't really matter. */
6911
6912 static int
6913 breakpoint_location_address_match (struct bp_location *bl,
6914 const address_space *aspace,
6915 CORE_ADDR addr)
6916 {
6917 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6918 aspace, addr)
6919 || (bl->length
6920 && breakpoint_address_match_range (bl->pspace->aspace,
6921 bl->address, bl->length,
6922 aspace, addr)));
6923 }
6924
6925 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6926 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6927 match happens only if ASPACE matches the breakpoint's address
6928 space. On targets that have global breakpoints, the address space
6929 doesn't really matter. */
6930
6931 static int
6932 breakpoint_location_address_range_overlap (struct bp_location *bl,
6933 const address_space *aspace,
6934 CORE_ADDR addr, int len)
6935 {
6936 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6937 || bl->pspace->aspace == aspace)
6938 {
6939 int bl_len = bl->length != 0 ? bl->length : 1;
6940
6941 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6942 return 1;
6943 }
6944 return 0;
6945 }
6946
6947 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6948 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6949 true, otherwise returns false. */
6950
6951 static int
6952 tracepoint_locations_match (struct bp_location *loc1,
6953 struct bp_location *loc2)
6954 {
6955 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6956 /* Since tracepoint locations are never duplicated with others', tracepoint
6957 locations at the same address of different tracepoints are regarded as
6958 different locations. */
6959 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6960 else
6961 return 0;
6962 }
6963
6964 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6965 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
6966 the same location. If SW_HW_BPS_MATCH is true, then software
6967 breakpoint locations and hardware breakpoint locations match,
6968 otherwise they don't. */
6969
6970 static int
6971 breakpoint_locations_match (struct bp_location *loc1,
6972 struct bp_location *loc2,
6973 bool sw_hw_bps_match)
6974 {
6975 int hw_point1, hw_point2;
6976
6977 /* Both of them must not be in moribund_locations. */
6978 gdb_assert (loc1->owner != NULL);
6979 gdb_assert (loc2->owner != NULL);
6980
6981 hw_point1 = is_hardware_watchpoint (loc1->owner);
6982 hw_point2 = is_hardware_watchpoint (loc2->owner);
6983
6984 if (hw_point1 != hw_point2)
6985 return 0;
6986 else if (hw_point1)
6987 return watchpoint_locations_match (loc1, loc2);
6988 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6989 return tracepoint_locations_match (loc1, loc2);
6990 else
6991 /* We compare bp_location.length in order to cover ranged
6992 breakpoints. Keep this in sync with
6993 bp_location_is_less_than. */
6994 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6995 loc2->pspace->aspace, loc2->address)
6996 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
6997 && loc1->length == loc2->length);
6998 }
6999
7000 static void
7001 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7002 int bnum, int have_bnum)
7003 {
7004 /* The longest string possibly returned by hex_string_custom
7005 is 50 chars. These must be at least that big for safety. */
7006 char astr1[64];
7007 char astr2[64];
7008
7009 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7010 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7011 if (have_bnum)
7012 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7013 bnum, astr1, astr2);
7014 else
7015 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7016 }
7017
7018 /* Adjust a breakpoint's address to account for architectural
7019 constraints on breakpoint placement. Return the adjusted address.
7020 Note: Very few targets require this kind of adjustment. For most
7021 targets, this function is simply the identity function. */
7022
7023 static CORE_ADDR
7024 adjust_breakpoint_address (struct gdbarch *gdbarch,
7025 CORE_ADDR bpaddr, enum bptype bptype)
7026 {
7027 if (bptype == bp_watchpoint
7028 || bptype == bp_hardware_watchpoint
7029 || bptype == bp_read_watchpoint
7030 || bptype == bp_access_watchpoint
7031 || bptype == bp_catchpoint)
7032 {
7033 /* Watchpoints and the various bp_catch_* eventpoints should not
7034 have their addresses modified. */
7035 return bpaddr;
7036 }
7037 else if (bptype == bp_single_step)
7038 {
7039 /* Single-step breakpoints should not have their addresses
7040 modified. If there's any architectural constrain that
7041 applies to this address, then it should have already been
7042 taken into account when the breakpoint was created in the
7043 first place. If we didn't do this, stepping through e.g.,
7044 Thumb-2 IT blocks would break. */
7045 return bpaddr;
7046 }
7047 else
7048 {
7049 CORE_ADDR adjusted_bpaddr = bpaddr;
7050
7051 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7052 {
7053 /* Some targets have architectural constraints on the placement
7054 of breakpoint instructions. Obtain the adjusted address. */
7055 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7056 }
7057
7058 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
7059
7060 /* An adjusted breakpoint address can significantly alter
7061 a user's expectations. Print a warning if an adjustment
7062 is required. */
7063 if (adjusted_bpaddr != bpaddr)
7064 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7065
7066 return adjusted_bpaddr;
7067 }
7068 }
7069
7070 static bp_loc_type
7071 bp_location_from_bp_type (bptype type)
7072 {
7073 switch (type)
7074 {
7075 case bp_breakpoint:
7076 case bp_single_step:
7077 case bp_until:
7078 case bp_finish:
7079 case bp_longjmp:
7080 case bp_longjmp_resume:
7081 case bp_longjmp_call_dummy:
7082 case bp_exception:
7083 case bp_exception_resume:
7084 case bp_step_resume:
7085 case bp_hp_step_resume:
7086 case bp_watchpoint_scope:
7087 case bp_call_dummy:
7088 case bp_std_terminate:
7089 case bp_shlib_event:
7090 case bp_thread_event:
7091 case bp_overlay_event:
7092 case bp_jit_event:
7093 case bp_longjmp_master:
7094 case bp_std_terminate_master:
7095 case bp_exception_master:
7096 case bp_gnu_ifunc_resolver:
7097 case bp_gnu_ifunc_resolver_return:
7098 case bp_dprintf:
7099 return bp_loc_software_breakpoint;
7100 case bp_hardware_breakpoint:
7101 return bp_loc_hardware_breakpoint;
7102 case bp_hardware_watchpoint:
7103 case bp_read_watchpoint:
7104 case bp_access_watchpoint:
7105 return bp_loc_hardware_watchpoint;
7106 case bp_watchpoint:
7107 case bp_catchpoint:
7108 case bp_tracepoint:
7109 case bp_fast_tracepoint:
7110 case bp_static_tracepoint:
7111 return bp_loc_other;
7112 default:
7113 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7114 }
7115 }
7116
7117 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7118 {
7119 this->owner = owner;
7120 this->cond_bytecode = NULL;
7121 this->shlib_disabled = 0;
7122 this->enabled = 1;
7123 this->disabled_by_cond = false;
7124
7125 this->loc_type = type;
7126
7127 if (this->loc_type == bp_loc_software_breakpoint
7128 || this->loc_type == bp_loc_hardware_breakpoint)
7129 mark_breakpoint_location_modified (this);
7130
7131 incref ();
7132 }
7133
7134 bp_location::bp_location (breakpoint *owner)
7135 : bp_location::bp_location (owner,
7136 bp_location_from_bp_type (owner->type))
7137 {
7138 }
7139
7140 /* Allocate a struct bp_location. */
7141
7142 static struct bp_location *
7143 allocate_bp_location (struct breakpoint *bpt)
7144 {
7145 return bpt->ops->allocate_location (bpt);
7146 }
7147
7148 /* Decrement reference count. If the reference count reaches 0,
7149 destroy the bp_location. Sets *BLP to NULL. */
7150
7151 static void
7152 decref_bp_location (struct bp_location **blp)
7153 {
7154 bp_location_ref_policy::decref (*blp);
7155 *blp = NULL;
7156 }
7157
7158 /* Add breakpoint B at the end of the global breakpoint chain. */
7159
7160 static breakpoint *
7161 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7162 {
7163 struct breakpoint *b1;
7164 struct breakpoint *result = b.get ();
7165
7166 /* Add this breakpoint to the end of the chain so that a list of
7167 breakpoints will come out in order of increasing numbers. */
7168
7169 b1 = breakpoint_chain;
7170 if (b1 == 0)
7171 breakpoint_chain = b.release ();
7172 else
7173 {
7174 while (b1->next)
7175 b1 = b1->next;
7176 b1->next = b.release ();
7177 }
7178
7179 return result;
7180 }
7181
7182 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7183
7184 static void
7185 init_raw_breakpoint_without_location (struct breakpoint *b,
7186 struct gdbarch *gdbarch,
7187 enum bptype bptype,
7188 const struct breakpoint_ops *ops)
7189 {
7190 gdb_assert (ops != NULL);
7191
7192 b->ops = ops;
7193 b->type = bptype;
7194 b->gdbarch = gdbarch;
7195 b->language = current_language->la_language;
7196 b->input_radix = input_radix;
7197 b->related_breakpoint = b;
7198 }
7199
7200 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7201 that has type BPTYPE and has no locations as yet. */
7202
7203 static struct breakpoint *
7204 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7205 enum bptype bptype,
7206 const struct breakpoint_ops *ops)
7207 {
7208 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7209
7210 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7211 return add_to_breakpoint_chain (std::move (b));
7212 }
7213
7214 /* Initialize loc->function_name. */
7215
7216 static void
7217 set_breakpoint_location_function (struct bp_location *loc)
7218 {
7219 gdb_assert (loc->owner != NULL);
7220
7221 if (loc->owner->type == bp_breakpoint
7222 || loc->owner->type == bp_hardware_breakpoint
7223 || is_tracepoint (loc->owner))
7224 {
7225 const char *function_name;
7226
7227 if (loc->msymbol != NULL
7228 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
7229 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
7230 {
7231 struct breakpoint *b = loc->owner;
7232
7233 function_name = loc->msymbol->linkage_name ();
7234
7235 if (b->type == bp_breakpoint && b->loc == loc
7236 && loc->next == NULL && b->related_breakpoint == b)
7237 {
7238 /* Create only the whole new breakpoint of this type but do not
7239 mess more complicated breakpoints with multiple locations. */
7240 b->type = bp_gnu_ifunc_resolver;
7241 /* Remember the resolver's address for use by the return
7242 breakpoint. */
7243 loc->related_address = loc->address;
7244 }
7245 }
7246 else
7247 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7248
7249 if (function_name)
7250 loc->function_name = xstrdup (function_name);
7251 }
7252 }
7253
7254 /* Attempt to determine architecture of location identified by SAL. */
7255 struct gdbarch *
7256 get_sal_arch (struct symtab_and_line sal)
7257 {
7258 if (sal.section)
7259 return sal.section->objfile->arch ();
7260 if (sal.symtab)
7261 return SYMTAB_OBJFILE (sal.symtab)->arch ();
7262
7263 return NULL;
7264 }
7265
7266 /* Low level routine for partially initializing a breakpoint of type
7267 BPTYPE. The newly created breakpoint's address, section, source
7268 file name, and line number are provided by SAL.
7269
7270 It is expected that the caller will complete the initialization of
7271 the newly created breakpoint struct as well as output any status
7272 information regarding the creation of a new breakpoint. */
7273
7274 static void
7275 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7276 struct symtab_and_line sal, enum bptype bptype,
7277 const struct breakpoint_ops *ops)
7278 {
7279 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7280
7281 add_location_to_breakpoint (b, &sal);
7282
7283 if (bptype != bp_catchpoint)
7284 gdb_assert (sal.pspace != NULL);
7285
7286 /* Store the program space that was used to set the breakpoint,
7287 except for ordinary breakpoints, which are independent of the
7288 program space. */
7289 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7290 b->pspace = sal.pspace;
7291 }
7292
7293 /* set_raw_breakpoint is a low level routine for allocating and
7294 partially initializing a breakpoint of type BPTYPE. The newly
7295 created breakpoint's address, section, source file name, and line
7296 number are provided by SAL. The newly created and partially
7297 initialized breakpoint is added to the breakpoint chain and
7298 is also returned as the value of this function.
7299
7300 It is expected that the caller will complete the initialization of
7301 the newly created breakpoint struct as well as output any status
7302 information regarding the creation of a new breakpoint. In
7303 particular, set_raw_breakpoint does NOT set the breakpoint
7304 number! Care should be taken to not allow an error to occur
7305 prior to completing the initialization of the breakpoint. If this
7306 should happen, a bogus breakpoint will be left on the chain. */
7307
7308 struct breakpoint *
7309 set_raw_breakpoint (struct gdbarch *gdbarch,
7310 struct symtab_and_line sal, enum bptype bptype,
7311 const struct breakpoint_ops *ops)
7312 {
7313 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7314
7315 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7316 return add_to_breakpoint_chain (std::move (b));
7317 }
7318
7319 /* Call this routine when stepping and nexting to enable a breakpoint
7320 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7321 initiated the operation. */
7322
7323 void
7324 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7325 {
7326 int thread = tp->global_num;
7327
7328 /* To avoid having to rescan all objfile symbols at every step,
7329 we maintain a list of continually-inserted but always disabled
7330 longjmp "master" breakpoints. Here, we simply create momentary
7331 clones of those and enable them for the requested thread. */
7332 for (breakpoint *b : all_breakpoints_safe ())
7333 if (b->pspace == current_program_space
7334 && (b->type == bp_longjmp_master
7335 || b->type == bp_exception_master))
7336 {
7337 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7338 struct breakpoint *clone;
7339
7340 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7341 after their removal. */
7342 clone = momentary_breakpoint_from_master (b, type,
7343 &momentary_breakpoint_ops, 1);
7344 clone->thread = thread;
7345 }
7346
7347 tp->initiating_frame = frame;
7348 }
7349
7350 /* Delete all longjmp breakpoints from THREAD. */
7351 void
7352 delete_longjmp_breakpoint (int thread)
7353 {
7354 for (breakpoint *b : all_breakpoints_safe ())
7355 if (b->type == bp_longjmp || b->type == bp_exception)
7356 {
7357 if (b->thread == thread)
7358 delete_breakpoint (b);
7359 }
7360 }
7361
7362 void
7363 delete_longjmp_breakpoint_at_next_stop (int thread)
7364 {
7365 for (breakpoint *b : all_breakpoints_safe ())
7366 if (b->type == bp_longjmp || b->type == bp_exception)
7367 {
7368 if (b->thread == thread)
7369 b->disposition = disp_del_at_next_stop;
7370 }
7371 }
7372
7373 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7374 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7375 pointer to any of them. Return NULL if this system cannot place longjmp
7376 breakpoints. */
7377
7378 struct breakpoint *
7379 set_longjmp_breakpoint_for_call_dummy (void)
7380 {
7381 breakpoint *retval = nullptr;
7382
7383 for (breakpoint *b : all_breakpoints ())
7384 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7385 {
7386 struct breakpoint *new_b;
7387
7388 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7389 &momentary_breakpoint_ops,
7390 1);
7391 new_b->thread = inferior_thread ()->global_num;
7392
7393 /* Link NEW_B into the chain of RETVAL breakpoints. */
7394
7395 gdb_assert (new_b->related_breakpoint == new_b);
7396 if (retval == NULL)
7397 retval = new_b;
7398 new_b->related_breakpoint = retval;
7399 while (retval->related_breakpoint != new_b->related_breakpoint)
7400 retval = retval->related_breakpoint;
7401 retval->related_breakpoint = new_b;
7402 }
7403
7404 return retval;
7405 }
7406
7407 /* Verify all existing dummy frames and their associated breakpoints for
7408 TP. Remove those which can no longer be found in the current frame
7409 stack.
7410
7411 You should call this function only at places where it is safe to currently
7412 unwind the whole stack. Failed stack unwind would discard live dummy
7413 frames. */
7414
7415 void
7416 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7417 {
7418 struct breakpoint *b, *b_tmp;
7419
7420 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7421 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7422 {
7423 struct breakpoint *dummy_b = b->related_breakpoint;
7424
7425 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7426 dummy_b = dummy_b->related_breakpoint;
7427 if (dummy_b->type != bp_call_dummy
7428 || frame_find_by_id (dummy_b->frame_id) != NULL)
7429 continue;
7430
7431 dummy_frame_discard (dummy_b->frame_id, tp);
7432
7433 while (b->related_breakpoint != b)
7434 {
7435 if (b_tmp == b->related_breakpoint)
7436 b_tmp = b->related_breakpoint->next;
7437 delete_breakpoint (b->related_breakpoint);
7438 }
7439 delete_breakpoint (b);
7440 }
7441 }
7442
7443 void
7444 enable_overlay_breakpoints (void)
7445 {
7446 for (breakpoint *b : all_breakpoints ())
7447 if (b->type == bp_overlay_event)
7448 {
7449 b->enable_state = bp_enabled;
7450 update_global_location_list (UGLL_MAY_INSERT);
7451 overlay_events_enabled = 1;
7452 }
7453 }
7454
7455 void
7456 disable_overlay_breakpoints (void)
7457 {
7458 for (breakpoint *b : all_breakpoints ())
7459 if (b->type == bp_overlay_event)
7460 {
7461 b->enable_state = bp_disabled;
7462 update_global_location_list (UGLL_DONT_INSERT);
7463 overlay_events_enabled = 0;
7464 }
7465 }
7466
7467 /* Set an active std::terminate breakpoint for each std::terminate
7468 master breakpoint. */
7469 void
7470 set_std_terminate_breakpoint (void)
7471 {
7472 for (breakpoint *b : all_breakpoints_safe ())
7473 if (b->pspace == current_program_space
7474 && b->type == bp_std_terminate_master)
7475 {
7476 momentary_breakpoint_from_master (b, bp_std_terminate,
7477 &momentary_breakpoint_ops, 1);
7478 }
7479 }
7480
7481 /* Delete all the std::terminate breakpoints. */
7482 void
7483 delete_std_terminate_breakpoint (void)
7484 {
7485 for (breakpoint *b : all_breakpoints_safe ())
7486 if (b->type == bp_std_terminate)
7487 delete_breakpoint (b);
7488 }
7489
7490 struct breakpoint *
7491 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7492 {
7493 struct breakpoint *b;
7494
7495 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7496 &internal_breakpoint_ops);
7497
7498 b->enable_state = bp_enabled;
7499 /* location has to be used or breakpoint_re_set will delete me. */
7500 b->location = new_address_location (b->loc->address, NULL, 0);
7501
7502 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7503
7504 return b;
7505 }
7506
7507 struct lang_and_radix
7508 {
7509 enum language lang;
7510 int radix;
7511 };
7512
7513 /* Create a breakpoint for JIT code registration and unregistration. */
7514
7515 struct breakpoint *
7516 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7517 {
7518 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7519 &internal_breakpoint_ops);
7520 }
7521
7522 /* Remove JIT code registration and unregistration breakpoint(s). */
7523
7524 void
7525 remove_jit_event_breakpoints (void)
7526 {
7527 for (breakpoint *b : all_breakpoints_safe ())
7528 if (b->type == bp_jit_event
7529 && b->loc->pspace == current_program_space)
7530 delete_breakpoint (b);
7531 }
7532
7533 void
7534 remove_solib_event_breakpoints (void)
7535 {
7536 for (breakpoint *b : all_breakpoints_safe ())
7537 if (b->type == bp_shlib_event
7538 && b->loc->pspace == current_program_space)
7539 delete_breakpoint (b);
7540 }
7541
7542 /* See breakpoint.h. */
7543
7544 void
7545 remove_solib_event_breakpoints_at_next_stop (void)
7546 {
7547 for (breakpoint *b : all_breakpoints_safe ())
7548 if (b->type == bp_shlib_event
7549 && b->loc->pspace == current_program_space)
7550 b->disposition = disp_del_at_next_stop;
7551 }
7552
7553 /* Helper for create_solib_event_breakpoint /
7554 create_and_insert_solib_event_breakpoint. Allows specifying which
7555 INSERT_MODE to pass through to update_global_location_list. */
7556
7557 static struct breakpoint *
7558 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7559 enum ugll_insert_mode insert_mode)
7560 {
7561 struct breakpoint *b;
7562
7563 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7564 &internal_breakpoint_ops);
7565 update_global_location_list_nothrow (insert_mode);
7566 return b;
7567 }
7568
7569 struct breakpoint *
7570 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7571 {
7572 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7573 }
7574
7575 /* See breakpoint.h. */
7576
7577 struct breakpoint *
7578 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7579 {
7580 struct breakpoint *b;
7581
7582 /* Explicitly tell update_global_location_list to insert
7583 locations. */
7584 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7585 if (!b->loc->inserted)
7586 {
7587 delete_breakpoint (b);
7588 return NULL;
7589 }
7590 return b;
7591 }
7592
7593 /* Disable any breakpoints that are on code in shared libraries. Only
7594 apply to enabled breakpoints, disabled ones can just stay disabled. */
7595
7596 void
7597 disable_breakpoints_in_shlibs (void)
7598 {
7599 for (bp_location *loc : all_bp_locations ())
7600 {
7601 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7602 struct breakpoint *b = loc->owner;
7603
7604 /* We apply the check to all breakpoints, including disabled for
7605 those with loc->duplicate set. This is so that when breakpoint
7606 becomes enabled, or the duplicate is removed, gdb will try to
7607 insert all breakpoints. If we don't set shlib_disabled here,
7608 we'll try to insert those breakpoints and fail. */
7609 if (((b->type == bp_breakpoint)
7610 || (b->type == bp_jit_event)
7611 || (b->type == bp_hardware_breakpoint)
7612 || (is_tracepoint (b)))
7613 && loc->pspace == current_program_space
7614 && !loc->shlib_disabled
7615 && solib_name_from_address (loc->pspace, loc->address)
7616 )
7617 {
7618 loc->shlib_disabled = 1;
7619 }
7620 }
7621 }
7622
7623 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7624 notification of unloaded_shlib. Only apply to enabled breakpoints,
7625 disabled ones can just stay disabled. */
7626
7627 static void
7628 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7629 {
7630 int disabled_shlib_breaks = 0;
7631
7632 for (bp_location *loc : all_bp_locations ())
7633 {
7634 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7635 struct breakpoint *b = loc->owner;
7636
7637 if (solib->pspace == loc->pspace
7638 && !loc->shlib_disabled
7639 && (((b->type == bp_breakpoint
7640 || b->type == bp_jit_event
7641 || b->type == bp_hardware_breakpoint)
7642 && (loc->loc_type == bp_loc_hardware_breakpoint
7643 || loc->loc_type == bp_loc_software_breakpoint))
7644 || is_tracepoint (b))
7645 && solib_contains_address_p (solib, loc->address))
7646 {
7647 loc->shlib_disabled = 1;
7648 /* At this point, we cannot rely on remove_breakpoint
7649 succeeding so we must mark the breakpoint as not inserted
7650 to prevent future errors occurring in remove_breakpoints. */
7651 loc->inserted = 0;
7652
7653 /* This may cause duplicate notifications for the same breakpoint. */
7654 gdb::observers::breakpoint_modified.notify (b);
7655
7656 if (!disabled_shlib_breaks)
7657 {
7658 target_terminal::ours_for_output ();
7659 warning (_("Temporarily disabling breakpoints "
7660 "for unloaded shared library \"%s\""),
7661 solib->so_name);
7662 }
7663 disabled_shlib_breaks = 1;
7664 }
7665 }
7666 }
7667
7668 /* Disable any breakpoints and tracepoints in OBJFILE upon
7669 notification of free_objfile. Only apply to enabled breakpoints,
7670 disabled ones can just stay disabled. */
7671
7672 static void
7673 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7674 {
7675 if (objfile == NULL)
7676 return;
7677
7678 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7679 managed by the user with add-symbol-file/remove-symbol-file.
7680 Similarly to how breakpoints in shared libraries are handled in
7681 response to "nosharedlibrary", mark breakpoints in such modules
7682 shlib_disabled so they end up uninserted on the next global
7683 location list update. Shared libraries not loaded by the user
7684 aren't handled here -- they're already handled in
7685 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7686 solib_unloaded observer. We skip objfiles that are not
7687 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7688 main objfile). */
7689 if ((objfile->flags & OBJF_SHARED) == 0
7690 || (objfile->flags & OBJF_USERLOADED) == 0)
7691 return;
7692
7693 for (breakpoint *b : all_breakpoints ())
7694 {
7695 int bp_modified = 0;
7696
7697 if (!is_breakpoint (b) && !is_tracepoint (b))
7698 continue;
7699
7700 for (bp_location *loc : b->locations ())
7701 {
7702 CORE_ADDR loc_addr = loc->address;
7703
7704 if (loc->loc_type != bp_loc_hardware_breakpoint
7705 && loc->loc_type != bp_loc_software_breakpoint)
7706 continue;
7707
7708 if (loc->shlib_disabled != 0)
7709 continue;
7710
7711 if (objfile->pspace != loc->pspace)
7712 continue;
7713
7714 if (loc->loc_type != bp_loc_hardware_breakpoint
7715 && loc->loc_type != bp_loc_software_breakpoint)
7716 continue;
7717
7718 if (is_addr_in_objfile (loc_addr, objfile))
7719 {
7720 loc->shlib_disabled = 1;
7721 /* At this point, we don't know whether the object was
7722 unmapped from the inferior or not, so leave the
7723 inserted flag alone. We'll handle failure to
7724 uninsert quietly, in case the object was indeed
7725 unmapped. */
7726
7727 mark_breakpoint_location_modified (loc);
7728
7729 bp_modified = 1;
7730 }
7731 }
7732
7733 if (bp_modified)
7734 gdb::observers::breakpoint_modified.notify (b);
7735 }
7736 }
7737
7738 /* FORK & VFORK catchpoints. */
7739
7740 /* An instance of this type is used to represent a fork or vfork
7741 catchpoint. A breakpoint is really of this type iff its ops pointer points
7742 to CATCH_FORK_BREAKPOINT_OPS. */
7743
7744 struct fork_catchpoint : public breakpoint
7745 {
7746 /* Process id of a child process whose forking triggered this
7747 catchpoint. This field is only valid immediately after this
7748 catchpoint has triggered. */
7749 ptid_t forked_inferior_pid;
7750 };
7751
7752 /* Implement the "insert" breakpoint_ops method for fork
7753 catchpoints. */
7754
7755 static int
7756 insert_catch_fork (struct bp_location *bl)
7757 {
7758 return target_insert_fork_catchpoint (inferior_ptid.pid ());
7759 }
7760
7761 /* Implement the "remove" breakpoint_ops method for fork
7762 catchpoints. */
7763
7764 static int
7765 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
7766 {
7767 return target_remove_fork_catchpoint (inferior_ptid.pid ());
7768 }
7769
7770 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7771 catchpoints. */
7772
7773 static int
7774 breakpoint_hit_catch_fork (const struct bp_location *bl,
7775 const address_space *aspace, CORE_ADDR bp_addr,
7776 const struct target_waitstatus *ws)
7777 {
7778 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7779
7780 if (ws->kind != TARGET_WAITKIND_FORKED)
7781 return 0;
7782
7783 c->forked_inferior_pid = ws->value.related_pid;
7784 return 1;
7785 }
7786
7787 /* Implement the "print_it" breakpoint_ops method for fork
7788 catchpoints. */
7789
7790 static enum print_stop_action
7791 print_it_catch_fork (bpstat bs)
7792 {
7793 struct ui_out *uiout = current_uiout;
7794 struct breakpoint *b = bs->breakpoint_at;
7795 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7796
7797 annotate_catchpoint (b->number);
7798 maybe_print_thread_hit_breakpoint (uiout);
7799 if (b->disposition == disp_del)
7800 uiout->text ("Temporary catchpoint ");
7801 else
7802 uiout->text ("Catchpoint ");
7803 if (uiout->is_mi_like_p ())
7804 {
7805 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7806 uiout->field_string ("disp", bpdisp_text (b->disposition));
7807 }
7808 uiout->field_signed ("bkptno", b->number);
7809 uiout->text (" (forked process ");
7810 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7811 uiout->text ("), ");
7812 return PRINT_SRC_AND_LOC;
7813 }
7814
7815 /* Implement the "print_one" breakpoint_ops method for fork
7816 catchpoints. */
7817
7818 static void
7819 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7820 {
7821 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7822 struct value_print_options opts;
7823 struct ui_out *uiout = current_uiout;
7824
7825 get_user_print_options (&opts);
7826
7827 /* Field 4, the address, is omitted (which makes the columns not
7828 line up too nicely with the headers, but the effect is relatively
7829 readable). */
7830 if (opts.addressprint)
7831 uiout->field_skip ("addr");
7832 annotate_field (5);
7833 uiout->text ("fork");
7834 if (c->forked_inferior_pid != null_ptid)
7835 {
7836 uiout->text (", process ");
7837 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7838 uiout->spaces (1);
7839 }
7840
7841 if (uiout->is_mi_like_p ())
7842 uiout->field_string ("catch-type", "fork");
7843 }
7844
7845 /* Implement the "print_mention" breakpoint_ops method for fork
7846 catchpoints. */
7847
7848 static void
7849 print_mention_catch_fork (struct breakpoint *b)
7850 {
7851 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7852 }
7853
7854 /* Implement the "print_recreate" breakpoint_ops method for fork
7855 catchpoints. */
7856
7857 static void
7858 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7859 {
7860 fprintf_unfiltered (fp, "catch fork");
7861 print_recreate_thread (b, fp);
7862 }
7863
7864 /* The breakpoint_ops structure to be used in fork catchpoints. */
7865
7866 static struct breakpoint_ops catch_fork_breakpoint_ops;
7867
7868 /* Implement the "insert" breakpoint_ops method for vfork
7869 catchpoints. */
7870
7871 static int
7872 insert_catch_vfork (struct bp_location *bl)
7873 {
7874 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
7875 }
7876
7877 /* Implement the "remove" breakpoint_ops method for vfork
7878 catchpoints. */
7879
7880 static int
7881 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
7882 {
7883 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
7884 }
7885
7886 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7887 catchpoints. */
7888
7889 static int
7890 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7891 const address_space *aspace, CORE_ADDR bp_addr,
7892 const struct target_waitstatus *ws)
7893 {
7894 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7895
7896 if (ws->kind != TARGET_WAITKIND_VFORKED)
7897 return 0;
7898
7899 c->forked_inferior_pid = ws->value.related_pid;
7900 return 1;
7901 }
7902
7903 /* Implement the "print_it" breakpoint_ops method for vfork
7904 catchpoints. */
7905
7906 static enum print_stop_action
7907 print_it_catch_vfork (bpstat bs)
7908 {
7909 struct ui_out *uiout = current_uiout;
7910 struct breakpoint *b = bs->breakpoint_at;
7911 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7912
7913 annotate_catchpoint (b->number);
7914 maybe_print_thread_hit_breakpoint (uiout);
7915 if (b->disposition == disp_del)
7916 uiout->text ("Temporary catchpoint ");
7917 else
7918 uiout->text ("Catchpoint ");
7919 if (uiout->is_mi_like_p ())
7920 {
7921 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7922 uiout->field_string ("disp", bpdisp_text (b->disposition));
7923 }
7924 uiout->field_signed ("bkptno", b->number);
7925 uiout->text (" (vforked process ");
7926 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7927 uiout->text ("), ");
7928 return PRINT_SRC_AND_LOC;
7929 }
7930
7931 /* Implement the "print_one" breakpoint_ops method for vfork
7932 catchpoints. */
7933
7934 static void
7935 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7936 {
7937 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7938 struct value_print_options opts;
7939 struct ui_out *uiout = current_uiout;
7940
7941 get_user_print_options (&opts);
7942 /* Field 4, the address, is omitted (which makes the columns not
7943 line up too nicely with the headers, but the effect is relatively
7944 readable). */
7945 if (opts.addressprint)
7946 uiout->field_skip ("addr");
7947 annotate_field (5);
7948 uiout->text ("vfork");
7949 if (c->forked_inferior_pid != null_ptid)
7950 {
7951 uiout->text (", process ");
7952 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7953 uiout->spaces (1);
7954 }
7955
7956 if (uiout->is_mi_like_p ())
7957 uiout->field_string ("catch-type", "vfork");
7958 }
7959
7960 /* Implement the "print_mention" breakpoint_ops method for vfork
7961 catchpoints. */
7962
7963 static void
7964 print_mention_catch_vfork (struct breakpoint *b)
7965 {
7966 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7967 }
7968
7969 /* Implement the "print_recreate" breakpoint_ops method for vfork
7970 catchpoints. */
7971
7972 static void
7973 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7974 {
7975 fprintf_unfiltered (fp, "catch vfork");
7976 print_recreate_thread (b, fp);
7977 }
7978
7979 /* The breakpoint_ops structure to be used in vfork catchpoints. */
7980
7981 static struct breakpoint_ops catch_vfork_breakpoint_ops;
7982
7983 /* An instance of this type is used to represent an solib catchpoint.
7984 A breakpoint is really of this type iff its ops pointer points to
7985 CATCH_SOLIB_BREAKPOINT_OPS. */
7986
7987 struct solib_catchpoint : public breakpoint
7988 {
7989 ~solib_catchpoint () override;
7990
7991 /* True for "catch load", false for "catch unload". */
7992 bool is_load;
7993
7994 /* Regular expression to match, if any. COMPILED is only valid when
7995 REGEX is non-NULL. */
7996 char *regex;
7997 std::unique_ptr<compiled_regex> compiled;
7998 };
7999
8000 solib_catchpoint::~solib_catchpoint ()
8001 {
8002 xfree (this->regex);
8003 }
8004
8005 static int
8006 insert_catch_solib (struct bp_location *ignore)
8007 {
8008 return 0;
8009 }
8010
8011 static int
8012 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
8013 {
8014 return 0;
8015 }
8016
8017 static int
8018 breakpoint_hit_catch_solib (const struct bp_location *bl,
8019 const address_space *aspace,
8020 CORE_ADDR bp_addr,
8021 const struct target_waitstatus *ws)
8022 {
8023 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8024
8025 if (ws->kind == TARGET_WAITKIND_LOADED)
8026 return 1;
8027
8028 for (breakpoint *other : all_breakpoints ())
8029 {
8030 if (other == bl->owner)
8031 continue;
8032
8033 if (other->type != bp_shlib_event)
8034 continue;
8035
8036 if (self->pspace != NULL && other->pspace != self->pspace)
8037 continue;
8038
8039 for (bp_location *other_bl : other->locations ())
8040 {
8041 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8042 return 1;
8043 }
8044 }
8045
8046 return 0;
8047 }
8048
8049 static void
8050 check_status_catch_solib (struct bpstats *bs)
8051 {
8052 struct solib_catchpoint *self
8053 = (struct solib_catchpoint *) bs->breakpoint_at;
8054
8055 if (self->is_load)
8056 {
8057 for (so_list *iter : current_program_space->added_solibs)
8058 {
8059 if (!self->regex
8060 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
8061 return;
8062 }
8063 }
8064 else
8065 {
8066 for (const std::string &iter : current_program_space->deleted_solibs)
8067 {
8068 if (!self->regex
8069 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
8070 return;
8071 }
8072 }
8073
8074 bs->stop = 0;
8075 bs->print_it = print_it_noop;
8076 }
8077
8078 static enum print_stop_action
8079 print_it_catch_solib (bpstat bs)
8080 {
8081 struct breakpoint *b = bs->breakpoint_at;
8082 struct ui_out *uiout = current_uiout;
8083
8084 annotate_catchpoint (b->number);
8085 maybe_print_thread_hit_breakpoint (uiout);
8086 if (b->disposition == disp_del)
8087 uiout->text ("Temporary catchpoint ");
8088 else
8089 uiout->text ("Catchpoint ");
8090 uiout->field_signed ("bkptno", b->number);
8091 uiout->text ("\n");
8092 if (uiout->is_mi_like_p ())
8093 uiout->field_string ("disp", bpdisp_text (b->disposition));
8094 print_solib_event (1);
8095 return PRINT_SRC_AND_LOC;
8096 }
8097
8098 static void
8099 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8100 {
8101 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8102 struct value_print_options opts;
8103 struct ui_out *uiout = current_uiout;
8104
8105 get_user_print_options (&opts);
8106 /* Field 4, the address, is omitted (which makes the columns not
8107 line up too nicely with the headers, but the effect is relatively
8108 readable). */
8109 if (opts.addressprint)
8110 {
8111 annotate_field (4);
8112 uiout->field_skip ("addr");
8113 }
8114
8115 std::string msg;
8116 annotate_field (5);
8117 if (self->is_load)
8118 {
8119 if (self->regex)
8120 msg = string_printf (_("load of library matching %s"), self->regex);
8121 else
8122 msg = _("load of library");
8123 }
8124 else
8125 {
8126 if (self->regex)
8127 msg = string_printf (_("unload of library matching %s"), self->regex);
8128 else
8129 msg = _("unload of library");
8130 }
8131 uiout->field_string ("what", msg);
8132
8133 if (uiout->is_mi_like_p ())
8134 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8135 }
8136
8137 static void
8138 print_mention_catch_solib (struct breakpoint *b)
8139 {
8140 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8141
8142 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8143 self->is_load ? "load" : "unload");
8144 }
8145
8146 static void
8147 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8148 {
8149 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8150
8151 fprintf_unfiltered (fp, "%s %s",
8152 b->disposition == disp_del ? "tcatch" : "catch",
8153 self->is_load ? "load" : "unload");
8154 if (self->regex)
8155 fprintf_unfiltered (fp, " %s", self->regex);
8156 fprintf_unfiltered (fp, "\n");
8157 }
8158
8159 static struct breakpoint_ops catch_solib_breakpoint_ops;
8160
8161 /* See breakpoint.h. */
8162
8163 void
8164 add_solib_catchpoint (const char *arg, bool is_load, bool is_temp, bool enabled)
8165 {
8166 struct gdbarch *gdbarch = get_current_arch ();
8167
8168 if (!arg)
8169 arg = "";
8170 arg = skip_spaces (arg);
8171
8172 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
8173
8174 if (*arg != '\0')
8175 {
8176 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8177 _("Invalid regexp")));
8178 c->regex = xstrdup (arg);
8179 }
8180
8181 c->is_load = is_load;
8182 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
8183 &catch_solib_breakpoint_ops);
8184
8185 c->enable_state = enabled ? bp_enabled : bp_disabled;
8186
8187 install_breakpoint (0, std::move (c), 1);
8188 }
8189
8190 /* A helper function that does all the work for "catch load" and
8191 "catch unload". */
8192
8193 static void
8194 catch_load_or_unload (const char *arg, int from_tty, int is_load,
8195 struct cmd_list_element *command)
8196 {
8197 const int enabled = 1;
8198 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
8199
8200 add_solib_catchpoint (arg, is_load, temp, enabled);
8201 }
8202
8203 static void
8204 catch_load_command_1 (const char *arg, int from_tty,
8205 struct cmd_list_element *command)
8206 {
8207 catch_load_or_unload (arg, from_tty, 1, command);
8208 }
8209
8210 static void
8211 catch_unload_command_1 (const char *arg, int from_tty,
8212 struct cmd_list_element *command)
8213 {
8214 catch_load_or_unload (arg, from_tty, 0, command);
8215 }
8216
8217 /* See breakpoint.h. */
8218
8219 void
8220 init_catchpoint (struct breakpoint *b,
8221 struct gdbarch *gdbarch, bool temp,
8222 const char *cond_string,
8223 const struct breakpoint_ops *ops)
8224 {
8225 symtab_and_line sal;
8226 sal.pspace = current_program_space;
8227
8228 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8229
8230 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8231 b->disposition = temp ? disp_del : disp_donttouch;
8232 }
8233
8234 void
8235 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8236 {
8237 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8238 set_breakpoint_number (internal, b);
8239 if (is_tracepoint (b))
8240 set_tracepoint_count (breakpoint_count);
8241 if (!internal)
8242 mention (b);
8243 gdb::observers::breakpoint_created.notify (b);
8244
8245 if (update_gll)
8246 update_global_location_list (UGLL_MAY_INSERT);
8247 }
8248
8249 static void
8250 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8251 bool temp, const char *cond_string,
8252 const struct breakpoint_ops *ops)
8253 {
8254 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
8255
8256 init_catchpoint (c.get (), gdbarch, temp, cond_string, ops);
8257
8258 c->forked_inferior_pid = null_ptid;
8259
8260 install_breakpoint (0, std::move (c), 1);
8261 }
8262
8263 /* Exec catchpoints. */
8264
8265 /* An instance of this type is used to represent an exec catchpoint.
8266 A breakpoint is really of this type iff its ops pointer points to
8267 CATCH_EXEC_BREAKPOINT_OPS. */
8268
8269 struct exec_catchpoint : public breakpoint
8270 {
8271 ~exec_catchpoint () override;
8272
8273 /* Filename of a program whose exec triggered this catchpoint.
8274 This field is only valid immediately after this catchpoint has
8275 triggered. */
8276 char *exec_pathname;
8277 };
8278
8279 /* Exec catchpoint destructor. */
8280
8281 exec_catchpoint::~exec_catchpoint ()
8282 {
8283 xfree (this->exec_pathname);
8284 }
8285
8286 static int
8287 insert_catch_exec (struct bp_location *bl)
8288 {
8289 return target_insert_exec_catchpoint (inferior_ptid.pid ());
8290 }
8291
8292 static int
8293 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8294 {
8295 return target_remove_exec_catchpoint (inferior_ptid.pid ());
8296 }
8297
8298 static int
8299 breakpoint_hit_catch_exec (const struct bp_location *bl,
8300 const address_space *aspace, CORE_ADDR bp_addr,
8301 const struct target_waitstatus *ws)
8302 {
8303 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8304
8305 if (ws->kind != TARGET_WAITKIND_EXECD)
8306 return 0;
8307
8308 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8309 return 1;
8310 }
8311
8312 static enum print_stop_action
8313 print_it_catch_exec (bpstat bs)
8314 {
8315 struct ui_out *uiout = current_uiout;
8316 struct breakpoint *b = bs->breakpoint_at;
8317 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8318
8319 annotate_catchpoint (b->number);
8320 maybe_print_thread_hit_breakpoint (uiout);
8321 if (b->disposition == disp_del)
8322 uiout->text ("Temporary catchpoint ");
8323 else
8324 uiout->text ("Catchpoint ");
8325 if (uiout->is_mi_like_p ())
8326 {
8327 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8328 uiout->field_string ("disp", bpdisp_text (b->disposition));
8329 }
8330 uiout->field_signed ("bkptno", b->number);
8331 uiout->text (" (exec'd ");
8332 uiout->field_string ("new-exec", c->exec_pathname);
8333 uiout->text ("), ");
8334
8335 return PRINT_SRC_AND_LOC;
8336 }
8337
8338 static void
8339 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8340 {
8341 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8342 struct value_print_options opts;
8343 struct ui_out *uiout = current_uiout;
8344
8345 get_user_print_options (&opts);
8346
8347 /* Field 4, the address, is omitted (which makes the columns
8348 not line up too nicely with the headers, but the effect
8349 is relatively readable). */
8350 if (opts.addressprint)
8351 uiout->field_skip ("addr");
8352 annotate_field (5);
8353 uiout->text ("exec");
8354 if (c->exec_pathname != NULL)
8355 {
8356 uiout->text (", program \"");
8357 uiout->field_string ("what", c->exec_pathname);
8358 uiout->text ("\" ");
8359 }
8360
8361 if (uiout->is_mi_like_p ())
8362 uiout->field_string ("catch-type", "exec");
8363 }
8364
8365 static void
8366 print_mention_catch_exec (struct breakpoint *b)
8367 {
8368 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8369 }
8370
8371 /* Implement the "print_recreate" breakpoint_ops method for exec
8372 catchpoints. */
8373
8374 static void
8375 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8376 {
8377 fprintf_unfiltered (fp, "catch exec");
8378 print_recreate_thread (b, fp);
8379 }
8380
8381 static struct breakpoint_ops catch_exec_breakpoint_ops;
8382
8383 static int
8384 hw_breakpoint_used_count (void)
8385 {
8386 int i = 0;
8387
8388 for (breakpoint *b : all_breakpoints ())
8389 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8390 for (bp_location *bl : b->locations ())
8391 {
8392 /* Special types of hardware breakpoints may use more than
8393 one register. */
8394 i += b->ops->resources_needed (bl);
8395 }
8396
8397 return i;
8398 }
8399
8400 /* Returns the resources B would use if it were a hardware
8401 watchpoint. */
8402
8403 static int
8404 hw_watchpoint_use_count (struct breakpoint *b)
8405 {
8406 int i = 0;
8407
8408 if (!breakpoint_enabled (b))
8409 return 0;
8410
8411 for (bp_location *bl : b->locations ())
8412 {
8413 /* Special types of hardware watchpoints may use more than
8414 one register. */
8415 i += b->ops->resources_needed (bl);
8416 }
8417
8418 return i;
8419 }
8420
8421 /* Returns the sum the used resources of all hardware watchpoints of
8422 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8423 the sum of the used resources of all hardware watchpoints of other
8424 types _not_ TYPE. */
8425
8426 static int
8427 hw_watchpoint_used_count_others (struct breakpoint *except,
8428 enum bptype type, int *other_type_used)
8429 {
8430 int i = 0;
8431
8432 *other_type_used = 0;
8433 for (breakpoint *b : all_breakpoints ())
8434 {
8435 if (b == except)
8436 continue;
8437 if (!breakpoint_enabled (b))
8438 continue;
8439
8440 if (b->type == type)
8441 i += hw_watchpoint_use_count (b);
8442 else if (is_hardware_watchpoint (b))
8443 *other_type_used = 1;
8444 }
8445
8446 return i;
8447 }
8448
8449 void
8450 disable_watchpoints_before_interactive_call_start (void)
8451 {
8452 for (breakpoint *b : all_breakpoints ())
8453 if (is_watchpoint (b) && breakpoint_enabled (b))
8454 {
8455 b->enable_state = bp_call_disabled;
8456 update_global_location_list (UGLL_DONT_INSERT);
8457 }
8458 }
8459
8460 void
8461 enable_watchpoints_after_interactive_call_stop (void)
8462 {
8463 for (breakpoint *b : all_breakpoints ())
8464 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8465 {
8466 b->enable_state = bp_enabled;
8467 update_global_location_list (UGLL_MAY_INSERT);
8468 }
8469 }
8470
8471 void
8472 disable_breakpoints_before_startup (void)
8473 {
8474 current_program_space->executing_startup = 1;
8475 update_global_location_list (UGLL_DONT_INSERT);
8476 }
8477
8478 void
8479 enable_breakpoints_after_startup (void)
8480 {
8481 current_program_space->executing_startup = 0;
8482 breakpoint_re_set ();
8483 }
8484
8485 /* Create a new single-step breakpoint for thread THREAD, with no
8486 locations. */
8487
8488 static struct breakpoint *
8489 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8490 {
8491 std::unique_ptr<breakpoint> b (new breakpoint ());
8492
8493 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
8494 &momentary_breakpoint_ops);
8495
8496 b->disposition = disp_donttouch;
8497 b->frame_id = null_frame_id;
8498
8499 b->thread = thread;
8500 gdb_assert (b->thread != 0);
8501
8502 return add_to_breakpoint_chain (std::move (b));
8503 }
8504
8505 /* Set a momentary breakpoint of type TYPE at address specified by
8506 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8507 frame. */
8508
8509 breakpoint_up
8510 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8511 struct frame_id frame_id, enum bptype type)
8512 {
8513 struct breakpoint *b;
8514
8515 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8516 tail-called one. */
8517 gdb_assert (!frame_id_artificial_p (frame_id));
8518
8519 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8520 b->enable_state = bp_enabled;
8521 b->disposition = disp_donttouch;
8522 b->frame_id = frame_id;
8523
8524 b->thread = inferior_thread ()->global_num;
8525
8526 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8527
8528 return breakpoint_up (b);
8529 }
8530
8531 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8532 The new breakpoint will have type TYPE, use OPS as its
8533 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8534
8535 static struct breakpoint *
8536 momentary_breakpoint_from_master (struct breakpoint *orig,
8537 enum bptype type,
8538 const struct breakpoint_ops *ops,
8539 int loc_enabled)
8540 {
8541 struct breakpoint *copy;
8542
8543 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8544 copy->loc = allocate_bp_location (copy);
8545 set_breakpoint_location_function (copy->loc);
8546
8547 copy->loc->gdbarch = orig->loc->gdbarch;
8548 copy->loc->requested_address = orig->loc->requested_address;
8549 copy->loc->address = orig->loc->address;
8550 copy->loc->section = orig->loc->section;
8551 copy->loc->pspace = orig->loc->pspace;
8552 copy->loc->probe = orig->loc->probe;
8553 copy->loc->line_number = orig->loc->line_number;
8554 copy->loc->symtab = orig->loc->symtab;
8555 copy->loc->enabled = loc_enabled;
8556 copy->frame_id = orig->frame_id;
8557 copy->thread = orig->thread;
8558 copy->pspace = orig->pspace;
8559
8560 copy->enable_state = bp_enabled;
8561 copy->disposition = disp_donttouch;
8562 copy->number = internal_breakpoint_number--;
8563
8564 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8565 return copy;
8566 }
8567
8568 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8569 ORIG is NULL. */
8570
8571 struct breakpoint *
8572 clone_momentary_breakpoint (struct breakpoint *orig)
8573 {
8574 /* If there's nothing to clone, then return nothing. */
8575 if (orig == NULL)
8576 return NULL;
8577
8578 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8579 }
8580
8581 breakpoint_up
8582 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8583 enum bptype type)
8584 {
8585 struct symtab_and_line sal;
8586
8587 sal = find_pc_line (pc, 0);
8588 sal.pc = pc;
8589 sal.section = find_pc_overlay (pc);
8590 sal.explicit_pc = 1;
8591
8592 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8593 }
8594 \f
8595
8596 /* Tell the user we have just set a breakpoint B. */
8597
8598 static void
8599 mention (struct breakpoint *b)
8600 {
8601 b->ops->print_mention (b);
8602 current_uiout->text ("\n");
8603 }
8604 \f
8605
8606 static bool bp_loc_is_permanent (struct bp_location *loc);
8607
8608 /* Handle "set breakpoint auto-hw on".
8609
8610 If the explicitly specified breakpoint type is not hardware
8611 breakpoint, check the memory map to see whether the breakpoint
8612 address is in read-only memory.
8613
8614 - location type is not hardware breakpoint, memory is read-only.
8615 We change the type of the location to hardware breakpoint.
8616
8617 - location type is hardware breakpoint, memory is read-write. This
8618 means we've previously made the location hardware one, but then the
8619 memory map changed, so we undo.
8620 */
8621
8622 static void
8623 handle_automatic_hardware_breakpoints (bp_location *bl)
8624 {
8625 if (automatic_hardware_breakpoints
8626 && bl->owner->type != bp_hardware_breakpoint
8627 && (bl->loc_type == bp_loc_software_breakpoint
8628 || bl->loc_type == bp_loc_hardware_breakpoint))
8629 {
8630 /* When breakpoints are removed, remove_breakpoints will use
8631 location types we've just set here, the only possible problem
8632 is that memory map has changed during running program, but
8633 it's not going to work anyway with current gdb. */
8634 mem_region *mr = lookup_mem_region (bl->address);
8635
8636 if (mr != nullptr)
8637 {
8638 enum bp_loc_type new_type;
8639
8640 if (mr->attrib.mode != MEM_RW)
8641 new_type = bp_loc_hardware_breakpoint;
8642 else
8643 new_type = bp_loc_software_breakpoint;
8644
8645 if (new_type != bl->loc_type)
8646 {
8647 static bool said = false;
8648
8649 bl->loc_type = new_type;
8650 if (!said)
8651 {
8652 fprintf_filtered (gdb_stdout,
8653 _("Note: automatically using "
8654 "hardware breakpoints for "
8655 "read-only addresses.\n"));
8656 said = true;
8657 }
8658 }
8659 }
8660 }
8661 }
8662
8663 static struct bp_location *
8664 add_location_to_breakpoint (struct breakpoint *b,
8665 const struct symtab_and_line *sal)
8666 {
8667 struct bp_location *loc, **tmp;
8668 CORE_ADDR adjusted_address;
8669 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8670
8671 if (loc_gdbarch == NULL)
8672 loc_gdbarch = b->gdbarch;
8673
8674 /* Adjust the breakpoint's address prior to allocating a location.
8675 Once we call allocate_bp_location(), that mostly uninitialized
8676 location will be placed on the location chain. Adjustment of the
8677 breakpoint may cause target_read_memory() to be called and we do
8678 not want its scan of the location chain to find a breakpoint and
8679 location that's only been partially initialized. */
8680 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8681 sal->pc, b->type);
8682
8683 /* Sort the locations by their ADDRESS. */
8684 loc = allocate_bp_location (b);
8685 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8686 tmp = &((*tmp)->next))
8687 ;
8688 loc->next = *tmp;
8689 *tmp = loc;
8690
8691 loc->requested_address = sal->pc;
8692 loc->address = adjusted_address;
8693 loc->pspace = sal->pspace;
8694 loc->probe.prob = sal->prob;
8695 loc->probe.objfile = sal->objfile;
8696 gdb_assert (loc->pspace != NULL);
8697 loc->section = sal->section;
8698 loc->gdbarch = loc_gdbarch;
8699 loc->line_number = sal->line;
8700 loc->symtab = sal->symtab;
8701 loc->symbol = sal->symbol;
8702 loc->msymbol = sal->msymbol;
8703 loc->objfile = sal->objfile;
8704
8705 set_breakpoint_location_function (loc);
8706
8707 /* While by definition, permanent breakpoints are already present in the
8708 code, we don't mark the location as inserted. Normally one would expect
8709 that GDB could rely on that breakpoint instruction to stop the program,
8710 thus removing the need to insert its own breakpoint, except that executing
8711 the breakpoint instruction can kill the target instead of reporting a
8712 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8713 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8714 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8715 breakpoint be inserted normally results in QEMU knowing about the GDB
8716 breakpoint, and thus trap before the breakpoint instruction is executed.
8717 (If GDB later needs to continue execution past the permanent breakpoint,
8718 it manually increments the PC, thus avoiding executing the breakpoint
8719 instruction.) */
8720 if (bp_loc_is_permanent (loc))
8721 loc->permanent = 1;
8722
8723 return loc;
8724 }
8725 \f
8726
8727 /* Return true if LOC is pointing to a permanent breakpoint,
8728 return false otherwise. */
8729
8730 static bool
8731 bp_loc_is_permanent (struct bp_location *loc)
8732 {
8733 gdb_assert (loc != NULL);
8734
8735 /* If we have a non-breakpoint-backed catchpoint or a software
8736 watchpoint, just return 0. We should not attempt to read from
8737 the addresses the locations of these breakpoint types point to.
8738 gdbarch_program_breakpoint_here_p, below, will attempt to read
8739 memory. */
8740 if (!bl_address_is_meaningful (loc))
8741 return false;
8742
8743 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8744 switch_to_program_space_and_thread (loc->pspace);
8745 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
8746 }
8747
8748 /* Build a command list for the dprintf corresponding to the current
8749 settings of the dprintf style options. */
8750
8751 static void
8752 update_dprintf_command_list (struct breakpoint *b)
8753 {
8754 char *dprintf_args = b->extra_string;
8755 char *printf_line = NULL;
8756
8757 if (!dprintf_args)
8758 return;
8759
8760 dprintf_args = skip_spaces (dprintf_args);
8761
8762 /* Allow a comma, as it may have terminated a location, but don't
8763 insist on it. */
8764 if (*dprintf_args == ',')
8765 ++dprintf_args;
8766 dprintf_args = skip_spaces (dprintf_args);
8767
8768 if (*dprintf_args != '"')
8769 error (_("Bad format string, missing '\"'."));
8770
8771 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8772 printf_line = xstrprintf ("printf %s", dprintf_args);
8773 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8774 {
8775 if (!dprintf_function)
8776 error (_("No function supplied for dprintf call"));
8777
8778 if (dprintf_channel && strlen (dprintf_channel) > 0)
8779 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8780 dprintf_function,
8781 dprintf_channel,
8782 dprintf_args);
8783 else
8784 printf_line = xstrprintf ("call (void) %s (%s)",
8785 dprintf_function,
8786 dprintf_args);
8787 }
8788 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8789 {
8790 if (target_can_run_breakpoint_commands ())
8791 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8792 else
8793 {
8794 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8795 printf_line = xstrprintf ("printf %s", dprintf_args);
8796 }
8797 }
8798 else
8799 internal_error (__FILE__, __LINE__,
8800 _("Invalid dprintf style."));
8801
8802 gdb_assert (printf_line != NULL);
8803
8804 /* Manufacture a printf sequence. */
8805 struct command_line *printf_cmd_line
8806 = new struct command_line (simple_control, printf_line);
8807 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8808 command_lines_deleter ()));
8809 }
8810
8811 /* Update all dprintf commands, making their command lists reflect
8812 current style settings. */
8813
8814 static void
8815 update_dprintf_commands (const char *args, int from_tty,
8816 struct cmd_list_element *c)
8817 {
8818 for (breakpoint *b : all_breakpoints ())
8819 if (b->type == bp_dprintf)
8820 update_dprintf_command_list (b);
8821 }
8822
8823 /* Create a breakpoint with SAL as location. Use LOCATION
8824 as a description of the location, and COND_STRING
8825 as condition expression. If LOCATION is NULL then create an
8826 "address location" from the address in the SAL. */
8827
8828 static void
8829 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8830 gdb::array_view<const symtab_and_line> sals,
8831 event_location_up &&location,
8832 gdb::unique_xmalloc_ptr<char> filter,
8833 gdb::unique_xmalloc_ptr<char> cond_string,
8834 gdb::unique_xmalloc_ptr<char> extra_string,
8835 enum bptype type, enum bpdisp disposition,
8836 int thread, int task, int ignore_count,
8837 const struct breakpoint_ops *ops, int from_tty,
8838 int enabled, int internal, unsigned flags,
8839 int display_canonical)
8840 {
8841 int i;
8842
8843 if (type == bp_hardware_breakpoint)
8844 {
8845 int target_resources_ok;
8846
8847 i = hw_breakpoint_used_count ();
8848 target_resources_ok =
8849 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8850 i + 1, 0);
8851 if (target_resources_ok == 0)
8852 error (_("No hardware breakpoint support in the target."));
8853 else if (target_resources_ok < 0)
8854 error (_("Hardware breakpoints used exceeds limit."));
8855 }
8856
8857 gdb_assert (!sals.empty ());
8858
8859 for (const auto &sal : sals)
8860 {
8861 struct bp_location *loc;
8862
8863 if (from_tty)
8864 {
8865 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8866 if (!loc_gdbarch)
8867 loc_gdbarch = gdbarch;
8868
8869 describe_other_breakpoints (loc_gdbarch,
8870 sal.pspace, sal.pc, sal.section, thread);
8871 }
8872
8873 if (&sal == &sals[0])
8874 {
8875 init_raw_breakpoint (b, gdbarch, sal, type, ops);
8876 b->thread = thread;
8877 b->task = task;
8878
8879 b->cond_string = cond_string.release ();
8880 b->extra_string = extra_string.release ();
8881 b->ignore_count = ignore_count;
8882 b->enable_state = enabled ? bp_enabled : bp_disabled;
8883 b->disposition = disposition;
8884
8885 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8886 b->loc->inserted = 1;
8887
8888 if (type == bp_static_tracepoint)
8889 {
8890 struct tracepoint *t = (struct tracepoint *) b;
8891 struct static_tracepoint_marker marker;
8892
8893 if (strace_marker_p (b))
8894 {
8895 /* We already know the marker exists, otherwise, we
8896 wouldn't see a sal for it. */
8897 const char *p
8898 = &event_location_to_string (b->location.get ())[3];
8899 const char *endp;
8900
8901 p = skip_spaces (p);
8902
8903 endp = skip_to_space (p);
8904
8905 t->static_trace_marker_id.assign (p, endp - p);
8906
8907 printf_filtered (_("Probed static tracepoint "
8908 "marker \"%s\"\n"),
8909 t->static_trace_marker_id.c_str ());
8910 }
8911 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8912 {
8913 t->static_trace_marker_id = std::move (marker.str_id);
8914
8915 printf_filtered (_("Probed static tracepoint "
8916 "marker \"%s\"\n"),
8917 t->static_trace_marker_id.c_str ());
8918 }
8919 else
8920 warning (_("Couldn't determine the static "
8921 "tracepoint marker to probe"));
8922 }
8923
8924 loc = b->loc;
8925 }
8926 else
8927 {
8928 loc = add_location_to_breakpoint (b, &sal);
8929 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8930 loc->inserted = 1;
8931 }
8932
8933 /* Do not set breakpoint locations conditions yet. As locations
8934 are inserted, they get sorted based on their addresses. Let
8935 the list stabilize to have reliable location numbers. */
8936
8937 /* Dynamic printf requires and uses additional arguments on the
8938 command line, otherwise it's an error. */
8939 if (type == bp_dprintf)
8940 {
8941 if (b->extra_string)
8942 update_dprintf_command_list (b);
8943 else
8944 error (_("Format string required"));
8945 }
8946 else if (b->extra_string)
8947 error (_("Garbage '%s' at end of command"), b->extra_string);
8948 }
8949
8950
8951 /* The order of the locations is now stable. Set the location
8952 condition using the location's number. */
8953 int loc_num = 1;
8954 for (bp_location *loc : b->locations ())
8955 {
8956 if (b->cond_string != nullptr)
8957 set_breakpoint_location_condition (b->cond_string, loc, b->number,
8958 loc_num);
8959
8960 ++loc_num;
8961 }
8962
8963 b->display_canonical = display_canonical;
8964 if (location != NULL)
8965 b->location = std::move (location);
8966 else
8967 b->location = new_address_location (b->loc->address, NULL, 0);
8968 b->filter = std::move (filter);
8969 }
8970
8971 static void
8972 create_breakpoint_sal (struct gdbarch *gdbarch,
8973 gdb::array_view<const symtab_and_line> sals,
8974 event_location_up &&location,
8975 gdb::unique_xmalloc_ptr<char> filter,
8976 gdb::unique_xmalloc_ptr<char> cond_string,
8977 gdb::unique_xmalloc_ptr<char> extra_string,
8978 enum bptype type, enum bpdisp disposition,
8979 int thread, int task, int ignore_count,
8980 const struct breakpoint_ops *ops, int from_tty,
8981 int enabled, int internal, unsigned flags,
8982 int display_canonical)
8983 {
8984 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
8985
8986 init_breakpoint_sal (b.get (), gdbarch,
8987 sals, std::move (location),
8988 std::move (filter),
8989 std::move (cond_string),
8990 std::move (extra_string),
8991 type, disposition,
8992 thread, task, ignore_count,
8993 ops, from_tty,
8994 enabled, internal, flags,
8995 display_canonical);
8996
8997 install_breakpoint (internal, std::move (b), 0);
8998 }
8999
9000 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9001 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9002 value. COND_STRING, if not NULL, specified the condition to be
9003 used for all breakpoints. Essentially the only case where
9004 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9005 function. In that case, it's still not possible to specify
9006 separate conditions for different overloaded functions, so
9007 we take just a single condition string.
9008
9009 NOTE: If the function succeeds, the caller is expected to cleanup
9010 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9011 array contents). If the function fails (error() is called), the
9012 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9013 COND and SALS arrays and each of those arrays contents. */
9014
9015 static void
9016 create_breakpoints_sal (struct gdbarch *gdbarch,
9017 struct linespec_result *canonical,
9018 gdb::unique_xmalloc_ptr<char> cond_string,
9019 gdb::unique_xmalloc_ptr<char> extra_string,
9020 enum bptype type, enum bpdisp disposition,
9021 int thread, int task, int ignore_count,
9022 const struct breakpoint_ops *ops, int from_tty,
9023 int enabled, int internal, unsigned flags)
9024 {
9025 if (canonical->pre_expanded)
9026 gdb_assert (canonical->lsals.size () == 1);
9027
9028 for (const auto &lsal : canonical->lsals)
9029 {
9030 /* Note that 'location' can be NULL in the case of a plain
9031 'break', without arguments. */
9032 event_location_up location
9033 = (canonical->location != NULL
9034 ? copy_event_location (canonical->location.get ()) : NULL);
9035 gdb::unique_xmalloc_ptr<char> filter_string
9036 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
9037
9038 create_breakpoint_sal (gdbarch, lsal.sals,
9039 std::move (location),
9040 std::move (filter_string),
9041 std::move (cond_string),
9042 std::move (extra_string),
9043 type, disposition,
9044 thread, task, ignore_count, ops,
9045 from_tty, enabled, internal, flags,
9046 canonical->special_display);
9047 }
9048 }
9049
9050 /* Parse LOCATION which is assumed to be a SAL specification possibly
9051 followed by conditionals. On return, SALS contains an array of SAL
9052 addresses found. LOCATION points to the end of the SAL (for
9053 linespec locations).
9054
9055 The array and the line spec strings are allocated on the heap, it is
9056 the caller's responsibility to free them. */
9057
9058 static void
9059 parse_breakpoint_sals (struct event_location *location,
9060 struct linespec_result *canonical)
9061 {
9062 struct symtab_and_line cursal;
9063
9064 if (event_location_type (location) == LINESPEC_LOCATION)
9065 {
9066 const char *spec = get_linespec_location (location)->spec_string;
9067
9068 if (spec == NULL)
9069 {
9070 /* The last displayed codepoint, if it's valid, is our default
9071 breakpoint address. */
9072 if (last_displayed_sal_is_valid ())
9073 {
9074 /* Set sal's pspace, pc, symtab, and line to the values
9075 corresponding to the last call to print_frame_info.
9076 Be sure to reinitialize LINE with NOTCURRENT == 0
9077 as the breakpoint line number is inappropriate otherwise.
9078 find_pc_line would adjust PC, re-set it back. */
9079 symtab_and_line sal = get_last_displayed_sal ();
9080 CORE_ADDR pc = sal.pc;
9081
9082 sal = find_pc_line (pc, 0);
9083
9084 /* "break" without arguments is equivalent to "break *PC"
9085 where PC is the last displayed codepoint's address. So
9086 make sure to set sal.explicit_pc to prevent GDB from
9087 trying to expand the list of sals to include all other
9088 instances with the same symtab and line. */
9089 sal.pc = pc;
9090 sal.explicit_pc = 1;
9091
9092 struct linespec_sals lsal;
9093 lsal.sals = {sal};
9094 lsal.canonical = NULL;
9095
9096 canonical->lsals.push_back (std::move (lsal));
9097 return;
9098 }
9099 else
9100 error (_("No default breakpoint address now."));
9101 }
9102 }
9103
9104 /* Force almost all breakpoints to be in terms of the
9105 current_source_symtab (which is decode_line_1's default).
9106 This should produce the results we want almost all of the
9107 time while leaving default_breakpoint_* alone.
9108
9109 ObjC: However, don't match an Objective-C method name which
9110 may have a '+' or '-' succeeded by a '['. */
9111 cursal = get_current_source_symtab_and_line ();
9112 if (last_displayed_sal_is_valid ())
9113 {
9114 const char *spec = NULL;
9115
9116 if (event_location_type (location) == LINESPEC_LOCATION)
9117 spec = get_linespec_location (location)->spec_string;
9118
9119 if (!cursal.symtab
9120 || (spec != NULL
9121 && strchr ("+-", spec[0]) != NULL
9122 && spec[1] != '['))
9123 {
9124 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9125 get_last_displayed_symtab (),
9126 get_last_displayed_line (),
9127 canonical, NULL, NULL);
9128 return;
9129 }
9130 }
9131
9132 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9133 cursal.symtab, cursal.line, canonical, NULL, NULL);
9134 }
9135
9136
9137 /* Convert each SAL into a real PC. Verify that the PC can be
9138 inserted as a breakpoint. If it can't throw an error. */
9139
9140 static void
9141 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
9142 {
9143 for (auto &sal : sals)
9144 resolve_sal_pc (&sal);
9145 }
9146
9147 /* Fast tracepoints may have restrictions on valid locations. For
9148 instance, a fast tracepoint using a jump instead of a trap will
9149 likely have to overwrite more bytes than a trap would, and so can
9150 only be placed where the instruction is longer than the jump, or a
9151 multi-instruction sequence does not have a jump into the middle of
9152 it, etc. */
9153
9154 static void
9155 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9156 gdb::array_view<const symtab_and_line> sals)
9157 {
9158 for (const auto &sal : sals)
9159 {
9160 struct gdbarch *sarch;
9161
9162 sarch = get_sal_arch (sal);
9163 /* We fall back to GDBARCH if there is no architecture
9164 associated with SAL. */
9165 if (sarch == NULL)
9166 sarch = gdbarch;
9167 std::string msg;
9168 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
9169 error (_("May not have a fast tracepoint at %s%s"),
9170 paddress (sarch, sal.pc), msg.c_str ());
9171 }
9172 }
9173
9174 /* Given TOK, a string specification of condition and thread, as
9175 accepted by the 'break' command, extract the condition
9176 string and thread number and set *COND_STRING and *THREAD.
9177 PC identifies the context at which the condition should be parsed.
9178 If no condition is found, *COND_STRING is set to NULL.
9179 If no thread is found, *THREAD is set to -1. */
9180
9181 static void
9182 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9183 char **cond_string, int *thread, int *task,
9184 char **rest)
9185 {
9186 *cond_string = NULL;
9187 *thread = -1;
9188 *task = 0;
9189 *rest = NULL;
9190 bool force = false;
9191
9192 while (tok && *tok)
9193 {
9194 const char *end_tok;
9195 int toklen;
9196 const char *cond_start = NULL;
9197 const char *cond_end = NULL;
9198
9199 tok = skip_spaces (tok);
9200
9201 if ((*tok == '"' || *tok == ',') && rest)
9202 {
9203 *rest = savestring (tok, strlen (tok));
9204 return;
9205 }
9206
9207 end_tok = skip_to_space (tok);
9208
9209 toklen = end_tok - tok;
9210
9211 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9212 {
9213 tok = cond_start = end_tok + 1;
9214 try
9215 {
9216 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9217 }
9218 catch (const gdb_exception_error &)
9219 {
9220 if (!force)
9221 throw;
9222 else
9223 tok = tok + strlen (tok);
9224 }
9225 cond_end = tok;
9226 *cond_string = savestring (cond_start, cond_end - cond_start);
9227 }
9228 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
9229 {
9230 tok = tok + toklen;
9231 force = true;
9232 }
9233 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9234 {
9235 const char *tmptok;
9236 struct thread_info *thr;
9237
9238 tok = end_tok + 1;
9239 thr = parse_thread_id (tok, &tmptok);
9240 if (tok == tmptok)
9241 error (_("Junk after thread keyword."));
9242 *thread = thr->global_num;
9243 tok = tmptok;
9244 }
9245 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9246 {
9247 char *tmptok;
9248
9249 tok = end_tok + 1;
9250 *task = strtol (tok, &tmptok, 0);
9251 if (tok == tmptok)
9252 error (_("Junk after task keyword."));
9253 if (!valid_task_id (*task))
9254 error (_("Unknown task %d."), *task);
9255 tok = tmptok;
9256 }
9257 else if (rest)
9258 {
9259 *rest = savestring (tok, strlen (tok));
9260 return;
9261 }
9262 else
9263 error (_("Junk at end of arguments."));
9264 }
9265 }
9266
9267 /* Call 'find_condition_and_thread' for each sal in SALS until a parse
9268 succeeds. The parsed values are written to COND_STRING, THREAD,
9269 TASK, and REST. See the comment of 'find_condition_and_thread'
9270 for the description of these parameters and INPUT. */
9271
9272 static void
9273 find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
9274 const char *input, char **cond_string,
9275 int *thread, int *task, char **rest)
9276 {
9277 int num_failures = 0;
9278 for (auto &sal : sals)
9279 {
9280 char *cond = nullptr;
9281 int thread_id = 0;
9282 int task_id = 0;
9283 char *remaining = nullptr;
9284
9285 /* Here we want to parse 'arg' to separate condition from thread
9286 number. But because parsing happens in a context and the
9287 contexts of sals might be different, try each until there is
9288 success. Finding one successful parse is sufficient for our
9289 goal. When setting the breakpoint we'll re-parse the
9290 condition in the context of each sal. */
9291 try
9292 {
9293 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
9294 &task_id, &remaining);
9295 *cond_string = cond;
9296 *thread = thread_id;
9297 *task = task_id;
9298 *rest = remaining;
9299 break;
9300 }
9301 catch (const gdb_exception_error &e)
9302 {
9303 num_failures++;
9304 /* If no sal remains, do not continue. */
9305 if (num_failures == sals.size ())
9306 throw;
9307 }
9308 }
9309 }
9310
9311 /* Decode a static tracepoint marker spec. */
9312
9313 static std::vector<symtab_and_line>
9314 decode_static_tracepoint_spec (const char **arg_p)
9315 {
9316 const char *p = &(*arg_p)[3];
9317 const char *endp;
9318
9319 p = skip_spaces (p);
9320
9321 endp = skip_to_space (p);
9322
9323 std::string marker_str (p, endp - p);
9324
9325 std::vector<static_tracepoint_marker> markers
9326 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9327 if (markers.empty ())
9328 error (_("No known static tracepoint marker named %s"),
9329 marker_str.c_str ());
9330
9331 std::vector<symtab_and_line> sals;
9332 sals.reserve (markers.size ());
9333
9334 for (const static_tracepoint_marker &marker : markers)
9335 {
9336 symtab_and_line sal = find_pc_line (marker.address, 0);
9337 sal.pc = marker.address;
9338 sals.push_back (sal);
9339 }
9340
9341 *arg_p = endp;
9342 return sals;
9343 }
9344
9345 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9346 according to IS_TRACEPOINT. */
9347
9348 static const struct breakpoint_ops *
9349 breakpoint_ops_for_event_location_type (enum event_location_type location_type,
9350 bool is_tracepoint)
9351 {
9352 if (is_tracepoint)
9353 {
9354 if (location_type == PROBE_LOCATION)
9355 return &tracepoint_probe_breakpoint_ops;
9356 else
9357 return &tracepoint_breakpoint_ops;
9358 }
9359 else
9360 {
9361 if (location_type == PROBE_LOCATION)
9362 return &bkpt_probe_breakpoint_ops;
9363 else
9364 return &bkpt_breakpoint_ops;
9365 }
9366 }
9367
9368 /* See breakpoint.h. */
9369
9370 const struct breakpoint_ops *
9371 breakpoint_ops_for_event_location (const struct event_location *location,
9372 bool is_tracepoint)
9373 {
9374 if (location != nullptr)
9375 return breakpoint_ops_for_event_location_type
9376 (event_location_type (location), is_tracepoint);
9377 return is_tracepoint ? &tracepoint_breakpoint_ops : &bkpt_breakpoint_ops;
9378 }
9379
9380 /* See breakpoint.h. */
9381
9382 int
9383 create_breakpoint (struct gdbarch *gdbarch,
9384 struct event_location *location,
9385 const char *cond_string,
9386 int thread, const char *extra_string,
9387 bool force_condition, int parse_extra,
9388 int tempflag, enum bptype type_wanted,
9389 int ignore_count,
9390 enum auto_boolean pending_break_support,
9391 const struct breakpoint_ops *ops,
9392 int from_tty, int enabled, int internal,
9393 unsigned flags)
9394 {
9395 struct linespec_result canonical;
9396 int pending = 0;
9397 int task = 0;
9398 int prev_bkpt_count = breakpoint_count;
9399
9400 gdb_assert (ops != NULL);
9401
9402 /* If extra_string isn't useful, set it to NULL. */
9403 if (extra_string != NULL && *extra_string == '\0')
9404 extra_string = NULL;
9405
9406 try
9407 {
9408 ops->create_sals_from_location (location, &canonical, type_wanted);
9409 }
9410 catch (const gdb_exception_error &e)
9411 {
9412 /* If caller is interested in rc value from parse, set
9413 value. */
9414 if (e.error == NOT_FOUND_ERROR)
9415 {
9416 /* If pending breakpoint support is turned off, throw
9417 error. */
9418
9419 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9420 throw;
9421
9422 exception_print (gdb_stderr, e);
9423
9424 /* If pending breakpoint support is auto query and the user
9425 selects no, then simply return the error code. */
9426 if (pending_break_support == AUTO_BOOLEAN_AUTO
9427 && !nquery (_("Make %s pending on future shared library load? "),
9428 bptype_string (type_wanted)))
9429 return 0;
9430
9431 /* At this point, either the user was queried about setting
9432 a pending breakpoint and selected yes, or pending
9433 breakpoint behavior is on and thus a pending breakpoint
9434 is defaulted on behalf of the user. */
9435 pending = 1;
9436 }
9437 else
9438 throw;
9439 }
9440
9441 if (!pending && canonical.lsals.empty ())
9442 return 0;
9443
9444 /* Resolve all line numbers to PC's and verify that the addresses
9445 are ok for the target. */
9446 if (!pending)
9447 {
9448 for (auto &lsal : canonical.lsals)
9449 breakpoint_sals_to_pc (lsal.sals);
9450 }
9451
9452 /* Fast tracepoints may have additional restrictions on location. */
9453 if (!pending && type_wanted == bp_fast_tracepoint)
9454 {
9455 for (const auto &lsal : canonical.lsals)
9456 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9457 }
9458
9459 /* Verify that condition can be parsed, before setting any
9460 breakpoints. Allocate a separate condition expression for each
9461 breakpoint. */
9462 if (!pending)
9463 {
9464 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9465 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9466
9467 if (parse_extra)
9468 {
9469 char *rest;
9470 char *cond;
9471
9472 const linespec_sals &lsal = canonical.lsals[0];
9473
9474 find_condition_and_thread_for_sals (lsal.sals, extra_string,
9475 &cond, &thread, &task, &rest);
9476 cond_string_copy.reset (cond);
9477 extra_string_copy.reset (rest);
9478 }
9479 else
9480 {
9481 if (type_wanted != bp_dprintf
9482 && extra_string != NULL && *extra_string != '\0')
9483 error (_("Garbage '%s' at end of location"), extra_string);
9484
9485 /* Check the validity of the condition. We should error out
9486 if the condition is invalid at all of the locations and
9487 if it is not forced. In the PARSE_EXTRA case above, this
9488 check is done when parsing the EXTRA_STRING. */
9489 if (cond_string != nullptr && !force_condition)
9490 {
9491 int num_failures = 0;
9492 const linespec_sals &lsal = canonical.lsals[0];
9493 for (const auto &sal : lsal.sals)
9494 {
9495 const char *cond = cond_string;
9496 try
9497 {
9498 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
9499 /* One success is sufficient to keep going. */
9500 break;
9501 }
9502 catch (const gdb_exception_error &)
9503 {
9504 num_failures++;
9505 /* If this is the last sal, error out. */
9506 if (num_failures == lsal.sals.size ())
9507 throw;
9508 }
9509 }
9510 }
9511
9512 /* Create a private copy of condition string. */
9513 if (cond_string)
9514 cond_string_copy.reset (xstrdup (cond_string));
9515 /* Create a private copy of any extra string. */
9516 if (extra_string)
9517 extra_string_copy.reset (xstrdup (extra_string));
9518 }
9519
9520 ops->create_breakpoints_sal (gdbarch, &canonical,
9521 std::move (cond_string_copy),
9522 std::move (extra_string_copy),
9523 type_wanted,
9524 tempflag ? disp_del : disp_donttouch,
9525 thread, task, ignore_count, ops,
9526 from_tty, enabled, internal, flags);
9527 }
9528 else
9529 {
9530 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9531
9532 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9533 b->location = copy_event_location (location);
9534
9535 if (parse_extra)
9536 b->cond_string = NULL;
9537 else
9538 {
9539 /* Create a private copy of condition string. */
9540 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
9541 b->thread = thread;
9542 }
9543
9544 /* Create a private copy of any extra string. */
9545 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
9546 b->ignore_count = ignore_count;
9547 b->disposition = tempflag ? disp_del : disp_donttouch;
9548 b->condition_not_parsed = 1;
9549 b->enable_state = enabled ? bp_enabled : bp_disabled;
9550 if ((type_wanted != bp_breakpoint
9551 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9552 b->pspace = current_program_space;
9553
9554 install_breakpoint (internal, std::move (b), 0);
9555 }
9556
9557 if (canonical.lsals.size () > 1)
9558 {
9559 warning (_("Multiple breakpoints were set.\nUse the "
9560 "\"delete\" command to delete unwanted breakpoints."));
9561 prev_breakpoint_count = prev_bkpt_count;
9562 }
9563
9564 update_global_location_list (UGLL_MAY_INSERT);
9565
9566 return 1;
9567 }
9568
9569 /* Set a breakpoint.
9570 ARG is a string describing breakpoint address,
9571 condition, and thread.
9572 FLAG specifies if a breakpoint is hardware on,
9573 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9574 and BP_TEMPFLAG. */
9575
9576 static void
9577 break_command_1 (const char *arg, int flag, int from_tty)
9578 {
9579 int tempflag = flag & BP_TEMPFLAG;
9580 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9581 ? bp_hardware_breakpoint
9582 : bp_breakpoint);
9583
9584 event_location_up location = string_to_event_location (&arg, current_language);
9585 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9586 (location.get (), false /* is_tracepoint */);
9587
9588 create_breakpoint (get_current_arch (),
9589 location.get (),
9590 NULL, 0, arg, false, 1 /* parse arg */,
9591 tempflag, type_wanted,
9592 0 /* Ignore count */,
9593 pending_break_support,
9594 ops,
9595 from_tty,
9596 1 /* enabled */,
9597 0 /* internal */,
9598 0);
9599 }
9600
9601 /* Helper function for break_command_1 and disassemble_command. */
9602
9603 void
9604 resolve_sal_pc (struct symtab_and_line *sal)
9605 {
9606 CORE_ADDR pc;
9607
9608 if (sal->pc == 0 && sal->symtab != NULL)
9609 {
9610 if (!find_line_pc (sal->symtab, sal->line, &pc))
9611 error (_("No line %d in file \"%s\"."),
9612 sal->line, symtab_to_filename_for_display (sal->symtab));
9613 sal->pc = pc;
9614
9615 /* If this SAL corresponds to a breakpoint inserted using a line
9616 number, then skip the function prologue if necessary. */
9617 if (sal->explicit_line)
9618 skip_prologue_sal (sal);
9619 }
9620
9621 if (sal->section == 0 && sal->symtab != NULL)
9622 {
9623 const struct blockvector *bv;
9624 const struct block *b;
9625 struct symbol *sym;
9626
9627 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9628 SYMTAB_COMPUNIT (sal->symtab));
9629 if (bv != NULL)
9630 {
9631 sym = block_linkage_function (b);
9632 if (sym != NULL)
9633 {
9634 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9635 sal->section = sym->obj_section (SYMTAB_OBJFILE (sal->symtab));
9636 }
9637 else
9638 {
9639 /* It really is worthwhile to have the section, so we'll
9640 just have to look harder. This case can be executed
9641 if we have line numbers but no functions (as can
9642 happen in assembly source). */
9643
9644 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9645 switch_to_program_space_and_thread (sal->pspace);
9646
9647 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9648 if (msym.minsym)
9649 sal->section = msym.obj_section ();
9650 }
9651 }
9652 }
9653 }
9654
9655 void
9656 break_command (const char *arg, int from_tty)
9657 {
9658 break_command_1 (arg, 0, from_tty);
9659 }
9660
9661 void
9662 tbreak_command (const char *arg, int from_tty)
9663 {
9664 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9665 }
9666
9667 static void
9668 hbreak_command (const char *arg, int from_tty)
9669 {
9670 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9671 }
9672
9673 static void
9674 thbreak_command (const char *arg, int from_tty)
9675 {
9676 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9677 }
9678
9679 static void
9680 stop_command (const char *arg, int from_tty)
9681 {
9682 printf_filtered (_("Specify the type of breakpoint to set.\n\
9683 Usage: stop in <function | address>\n\
9684 stop at <line>\n"));
9685 }
9686
9687 static void
9688 stopin_command (const char *arg, int from_tty)
9689 {
9690 int badInput = 0;
9691
9692 if (arg == NULL)
9693 badInput = 1;
9694 else if (*arg != '*')
9695 {
9696 const char *argptr = arg;
9697 int hasColon = 0;
9698
9699 /* Look for a ':'. If this is a line number specification, then
9700 say it is bad, otherwise, it should be an address or
9701 function/method name. */
9702 while (*argptr && !hasColon)
9703 {
9704 hasColon = (*argptr == ':');
9705 argptr++;
9706 }
9707
9708 if (hasColon)
9709 badInput = (*argptr != ':'); /* Not a class::method */
9710 else
9711 badInput = isdigit (*arg); /* a simple line number */
9712 }
9713
9714 if (badInput)
9715 printf_filtered (_("Usage: stop in <function | address>\n"));
9716 else
9717 break_command_1 (arg, 0, from_tty);
9718 }
9719
9720 static void
9721 stopat_command (const char *arg, int from_tty)
9722 {
9723 int badInput = 0;
9724
9725 if (arg == NULL || *arg == '*') /* no line number */
9726 badInput = 1;
9727 else
9728 {
9729 const char *argptr = arg;
9730 int hasColon = 0;
9731
9732 /* Look for a ':'. If there is a '::' then get out, otherwise
9733 it is probably a line number. */
9734 while (*argptr && !hasColon)
9735 {
9736 hasColon = (*argptr == ':');
9737 argptr++;
9738 }
9739
9740 if (hasColon)
9741 badInput = (*argptr == ':'); /* we have class::method */
9742 else
9743 badInput = !isdigit (*arg); /* not a line number */
9744 }
9745
9746 if (badInput)
9747 printf_filtered (_("Usage: stop at LINE\n"));
9748 else
9749 break_command_1 (arg, 0, from_tty);
9750 }
9751
9752 /* The dynamic printf command is mostly like a regular breakpoint, but
9753 with a prewired command list consisting of a single output command,
9754 built from extra arguments supplied on the dprintf command
9755 line. */
9756
9757 static void
9758 dprintf_command (const char *arg, int from_tty)
9759 {
9760 event_location_up location = string_to_event_location (&arg, current_language);
9761
9762 /* If non-NULL, ARG should have been advanced past the location;
9763 the next character must be ','. */
9764 if (arg != NULL)
9765 {
9766 if (arg[0] != ',' || arg[1] == '\0')
9767 error (_("Format string required"));
9768 else
9769 {
9770 /* Skip the comma. */
9771 ++arg;
9772 }
9773 }
9774
9775 create_breakpoint (get_current_arch (),
9776 location.get (),
9777 NULL, 0, arg, false, 1 /* parse arg */,
9778 0, bp_dprintf,
9779 0 /* Ignore count */,
9780 pending_break_support,
9781 &dprintf_breakpoint_ops,
9782 from_tty,
9783 1 /* enabled */,
9784 0 /* internal */,
9785 0);
9786 }
9787
9788 static void
9789 agent_printf_command (const char *arg, int from_tty)
9790 {
9791 error (_("May only run agent-printf on the target"));
9792 }
9793
9794 /* Implement the "breakpoint_hit" breakpoint_ops method for
9795 ranged breakpoints. */
9796
9797 static int
9798 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9799 const address_space *aspace,
9800 CORE_ADDR bp_addr,
9801 const struct target_waitstatus *ws)
9802 {
9803 if (ws->kind != TARGET_WAITKIND_STOPPED
9804 || ws->value.sig != GDB_SIGNAL_TRAP)
9805 return 0;
9806
9807 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9808 bl->length, aspace, bp_addr);
9809 }
9810
9811 /* Implement the "resources_needed" breakpoint_ops method for
9812 ranged breakpoints. */
9813
9814 static int
9815 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9816 {
9817 return target_ranged_break_num_registers ();
9818 }
9819
9820 /* Implement the "print_it" breakpoint_ops method for
9821 ranged breakpoints. */
9822
9823 static enum print_stop_action
9824 print_it_ranged_breakpoint (bpstat bs)
9825 {
9826 struct breakpoint *b = bs->breakpoint_at;
9827 struct bp_location *bl = b->loc;
9828 struct ui_out *uiout = current_uiout;
9829
9830 gdb_assert (b->type == bp_hardware_breakpoint);
9831
9832 /* Ranged breakpoints have only one location. */
9833 gdb_assert (bl && bl->next == NULL);
9834
9835 annotate_breakpoint (b->number);
9836
9837 maybe_print_thread_hit_breakpoint (uiout);
9838
9839 if (b->disposition == disp_del)
9840 uiout->text ("Temporary ranged breakpoint ");
9841 else
9842 uiout->text ("Ranged breakpoint ");
9843 if (uiout->is_mi_like_p ())
9844 {
9845 uiout->field_string ("reason",
9846 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9847 uiout->field_string ("disp", bpdisp_text (b->disposition));
9848 }
9849 uiout->field_signed ("bkptno", b->number);
9850 uiout->text (", ");
9851
9852 return PRINT_SRC_AND_LOC;
9853 }
9854
9855 /* Implement the "print_one" breakpoint_ops method for
9856 ranged breakpoints. */
9857
9858 static void
9859 print_one_ranged_breakpoint (struct breakpoint *b,
9860 struct bp_location **last_loc)
9861 {
9862 struct bp_location *bl = b->loc;
9863 struct value_print_options opts;
9864 struct ui_out *uiout = current_uiout;
9865
9866 /* Ranged breakpoints have only one location. */
9867 gdb_assert (bl && bl->next == NULL);
9868
9869 get_user_print_options (&opts);
9870
9871 if (opts.addressprint)
9872 /* We don't print the address range here, it will be printed later
9873 by print_one_detail_ranged_breakpoint. */
9874 uiout->field_skip ("addr");
9875 annotate_field (5);
9876 print_breakpoint_location (b, bl);
9877 *last_loc = bl;
9878 }
9879
9880 /* Implement the "print_one_detail" breakpoint_ops method for
9881 ranged breakpoints. */
9882
9883 static void
9884 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9885 struct ui_out *uiout)
9886 {
9887 CORE_ADDR address_start, address_end;
9888 struct bp_location *bl = b->loc;
9889 string_file stb;
9890
9891 gdb_assert (bl);
9892
9893 address_start = bl->address;
9894 address_end = address_start + bl->length - 1;
9895
9896 uiout->text ("\taddress range: ");
9897 stb.printf ("[%s, %s]",
9898 print_core_address (bl->gdbarch, address_start),
9899 print_core_address (bl->gdbarch, address_end));
9900 uiout->field_stream ("addr", stb);
9901 uiout->text ("\n");
9902 }
9903
9904 /* Implement the "print_mention" breakpoint_ops method for
9905 ranged breakpoints. */
9906
9907 static void
9908 print_mention_ranged_breakpoint (struct breakpoint *b)
9909 {
9910 struct bp_location *bl = b->loc;
9911 struct ui_out *uiout = current_uiout;
9912
9913 gdb_assert (bl);
9914 gdb_assert (b->type == bp_hardware_breakpoint);
9915
9916 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9917 b->number, paddress (bl->gdbarch, bl->address),
9918 paddress (bl->gdbarch, bl->address + bl->length - 1));
9919 }
9920
9921 /* Implement the "print_recreate" breakpoint_ops method for
9922 ranged breakpoints. */
9923
9924 static void
9925 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9926 {
9927 fprintf_unfiltered (fp, "break-range %s, %s",
9928 event_location_to_string (b->location.get ()),
9929 event_location_to_string (b->location_range_end.get ()));
9930 print_recreate_thread (b, fp);
9931 }
9932
9933 /* The breakpoint_ops structure to be used in ranged breakpoints. */
9934
9935 static struct breakpoint_ops ranged_breakpoint_ops;
9936
9937 /* Find the address where the end of the breakpoint range should be
9938 placed, given the SAL of the end of the range. This is so that if
9939 the user provides a line number, the end of the range is set to the
9940 last instruction of the given line. */
9941
9942 static CORE_ADDR
9943 find_breakpoint_range_end (struct symtab_and_line sal)
9944 {
9945 CORE_ADDR end;
9946
9947 /* If the user provided a PC value, use it. Otherwise,
9948 find the address of the end of the given location. */
9949 if (sal.explicit_pc)
9950 end = sal.pc;
9951 else
9952 {
9953 int ret;
9954 CORE_ADDR start;
9955
9956 ret = find_line_pc_range (sal, &start, &end);
9957 if (!ret)
9958 error (_("Could not find location of the end of the range."));
9959
9960 /* find_line_pc_range returns the start of the next line. */
9961 end--;
9962 }
9963
9964 return end;
9965 }
9966
9967 /* Implement the "break-range" CLI command. */
9968
9969 static void
9970 break_range_command (const char *arg, int from_tty)
9971 {
9972 const char *arg_start;
9973 struct linespec_result canonical_start, canonical_end;
9974 int bp_count, can_use_bp, length;
9975 CORE_ADDR end;
9976 struct breakpoint *b;
9977
9978 /* We don't support software ranged breakpoints. */
9979 if (target_ranged_break_num_registers () < 0)
9980 error (_("This target does not support hardware ranged breakpoints."));
9981
9982 bp_count = hw_breakpoint_used_count ();
9983 bp_count += target_ranged_break_num_registers ();
9984 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9985 bp_count, 0);
9986 if (can_use_bp < 0)
9987 error (_("Hardware breakpoints used exceeds limit."));
9988
9989 arg = skip_spaces (arg);
9990 if (arg == NULL || arg[0] == '\0')
9991 error(_("No address range specified."));
9992
9993 arg_start = arg;
9994 event_location_up start_location = string_to_event_location (&arg,
9995 current_language);
9996 parse_breakpoint_sals (start_location.get (), &canonical_start);
9997
9998 if (arg[0] != ',')
9999 error (_("Too few arguments."));
10000 else if (canonical_start.lsals.empty ())
10001 error (_("Could not find location of the beginning of the range."));
10002
10003 const linespec_sals &lsal_start = canonical_start.lsals[0];
10004
10005 if (canonical_start.lsals.size () > 1
10006 || lsal_start.sals.size () != 1)
10007 error (_("Cannot create a ranged breakpoint with multiple locations."));
10008
10009 const symtab_and_line &sal_start = lsal_start.sals[0];
10010 std::string addr_string_start (arg_start, arg - arg_start);
10011
10012 arg++; /* Skip the comma. */
10013 arg = skip_spaces (arg);
10014
10015 /* Parse the end location. */
10016
10017 arg_start = arg;
10018
10019 /* We call decode_line_full directly here instead of using
10020 parse_breakpoint_sals because we need to specify the start location's
10021 symtab and line as the default symtab and line for the end of the
10022 range. This makes it possible to have ranges like "foo.c:27, +14",
10023 where +14 means 14 lines from the start location. */
10024 event_location_up end_location = string_to_event_location (&arg,
10025 current_language);
10026 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10027 sal_start.symtab, sal_start.line,
10028 &canonical_end, NULL, NULL);
10029
10030 if (canonical_end.lsals.empty ())
10031 error (_("Could not find location of the end of the range."));
10032
10033 const linespec_sals &lsal_end = canonical_end.lsals[0];
10034 if (canonical_end.lsals.size () > 1
10035 || lsal_end.sals.size () != 1)
10036 error (_("Cannot create a ranged breakpoint with multiple locations."));
10037
10038 const symtab_and_line &sal_end = lsal_end.sals[0];
10039
10040 end = find_breakpoint_range_end (sal_end);
10041 if (sal_start.pc > end)
10042 error (_("Invalid address range, end precedes start."));
10043
10044 length = end - sal_start.pc + 1;
10045 if (length < 0)
10046 /* Length overflowed. */
10047 error (_("Address range too large."));
10048 else if (length == 1)
10049 {
10050 /* This range is simple enough to be handled by
10051 the `hbreak' command. */
10052 hbreak_command (&addr_string_start[0], 1);
10053
10054 return;
10055 }
10056
10057 /* Now set up the breakpoint. */
10058 b = set_raw_breakpoint (get_current_arch (), sal_start,
10059 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10060 set_breakpoint_count (breakpoint_count + 1);
10061 b->number = breakpoint_count;
10062 b->disposition = disp_donttouch;
10063 b->location = std::move (start_location);
10064 b->location_range_end = std::move (end_location);
10065 b->loc->length = length;
10066
10067 mention (b);
10068 gdb::observers::breakpoint_created.notify (b);
10069 update_global_location_list (UGLL_MAY_INSERT);
10070 }
10071
10072 /* Return non-zero if EXP is verified as constant. Returned zero
10073 means EXP is variable. Also the constant detection may fail for
10074 some constant expressions and in such case still falsely return
10075 zero. */
10076
10077 static bool
10078 watchpoint_exp_is_const (const struct expression *exp)
10079 {
10080 return exp->op->constant_p ();
10081 }
10082
10083 /* Watchpoint destructor. */
10084
10085 watchpoint::~watchpoint ()
10086 {
10087 xfree (this->exp_string);
10088 xfree (this->exp_string_reparse);
10089 }
10090
10091 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10092
10093 static void
10094 re_set_watchpoint (struct breakpoint *b)
10095 {
10096 struct watchpoint *w = (struct watchpoint *) b;
10097
10098 /* Watchpoint can be either on expression using entirely global
10099 variables, or it can be on local variables.
10100
10101 Watchpoints of the first kind are never auto-deleted, and even
10102 persist across program restarts. Since they can use variables
10103 from shared libraries, we need to reparse expression as libraries
10104 are loaded and unloaded.
10105
10106 Watchpoints on local variables can also change meaning as result
10107 of solib event. For example, if a watchpoint uses both a local
10108 and a global variables in expression, it's a local watchpoint,
10109 but unloading of a shared library will make the expression
10110 invalid. This is not a very common use case, but we still
10111 re-evaluate expression, to avoid surprises to the user.
10112
10113 Note that for local watchpoints, we re-evaluate it only if
10114 watchpoints frame id is still valid. If it's not, it means the
10115 watchpoint is out of scope and will be deleted soon. In fact,
10116 I'm not sure we'll ever be called in this case.
10117
10118 If a local watchpoint's frame id is still valid, then
10119 w->exp_valid_block is likewise valid, and we can safely use it.
10120
10121 Don't do anything about disabled watchpoints, since they will be
10122 reevaluated again when enabled. */
10123 update_watchpoint (w, 1 /* reparse */);
10124 }
10125
10126 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10127
10128 static int
10129 insert_watchpoint (struct bp_location *bl)
10130 {
10131 struct watchpoint *w = (struct watchpoint *) bl->owner;
10132 int length = w->exact ? 1 : bl->length;
10133
10134 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10135 w->cond_exp.get ());
10136 }
10137
10138 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10139
10140 static int
10141 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10142 {
10143 struct watchpoint *w = (struct watchpoint *) bl->owner;
10144 int length = w->exact ? 1 : bl->length;
10145
10146 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10147 w->cond_exp.get ());
10148 }
10149
10150 static int
10151 breakpoint_hit_watchpoint (const struct bp_location *bl,
10152 const address_space *aspace, CORE_ADDR bp_addr,
10153 const struct target_waitstatus *ws)
10154 {
10155 struct breakpoint *b = bl->owner;
10156 struct watchpoint *w = (struct watchpoint *) b;
10157
10158 /* Continuable hardware watchpoints are treated as non-existent if the
10159 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10160 some data address). Otherwise gdb won't stop on a break instruction
10161 in the code (not from a breakpoint) when a hardware watchpoint has
10162 been defined. Also skip watchpoints which we know did not trigger
10163 (did not match the data address). */
10164 if (is_hardware_watchpoint (b)
10165 && w->watchpoint_triggered == watch_triggered_no)
10166 return 0;
10167
10168 return 1;
10169 }
10170
10171 static void
10172 check_status_watchpoint (bpstat bs)
10173 {
10174 gdb_assert (is_watchpoint (bs->breakpoint_at));
10175
10176 bpstat_check_watchpoint (bs);
10177 }
10178
10179 /* Implement the "resources_needed" breakpoint_ops method for
10180 hardware watchpoints. */
10181
10182 static int
10183 resources_needed_watchpoint (const 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_region_ok_for_hw_watchpoint (bl->address, length);
10189 }
10190
10191 /* Implement the "works_in_software_mode" breakpoint_ops method for
10192 hardware watchpoints. */
10193
10194 static int
10195 works_in_software_mode_watchpoint (const struct breakpoint *b)
10196 {
10197 /* Read and access watchpoints only work with hardware support. */
10198 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10199 }
10200
10201 static enum print_stop_action
10202 print_it_watchpoint (bpstat bs)
10203 {
10204 struct breakpoint *b;
10205 enum print_stop_action result;
10206 struct watchpoint *w;
10207 struct ui_out *uiout = current_uiout;
10208
10209 gdb_assert (bs->bp_location_at != NULL);
10210
10211 b = bs->breakpoint_at;
10212 w = (struct watchpoint *) b;
10213
10214 annotate_watchpoint (b->number);
10215 maybe_print_thread_hit_breakpoint (uiout);
10216
10217 string_file stb;
10218
10219 gdb::optional<ui_out_emit_tuple> tuple_emitter;
10220 switch (b->type)
10221 {
10222 case bp_watchpoint:
10223 case bp_hardware_watchpoint:
10224 if (uiout->is_mi_like_p ())
10225 uiout->field_string
10226 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10227 mention (b);
10228 tuple_emitter.emplace (uiout, "value");
10229 uiout->text ("\nOld value = ");
10230 watchpoint_value_print (bs->old_val.get (), &stb);
10231 uiout->field_stream ("old", stb);
10232 uiout->text ("\nNew value = ");
10233 watchpoint_value_print (w->val.get (), &stb);
10234 uiout->field_stream ("new", stb);
10235 uiout->text ("\n");
10236 /* More than one watchpoint may have been triggered. */
10237 result = PRINT_UNKNOWN;
10238 break;
10239
10240 case bp_read_watchpoint:
10241 if (uiout->is_mi_like_p ())
10242 uiout->field_string
10243 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10244 mention (b);
10245 tuple_emitter.emplace (uiout, "value");
10246 uiout->text ("\nValue = ");
10247 watchpoint_value_print (w->val.get (), &stb);
10248 uiout->field_stream ("value", stb);
10249 uiout->text ("\n");
10250 result = PRINT_UNKNOWN;
10251 break;
10252
10253 case bp_access_watchpoint:
10254 if (bs->old_val != NULL)
10255 {
10256 if (uiout->is_mi_like_p ())
10257 uiout->field_string
10258 ("reason",
10259 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10260 mention (b);
10261 tuple_emitter.emplace (uiout, "value");
10262 uiout->text ("\nOld value = ");
10263 watchpoint_value_print (bs->old_val.get (), &stb);
10264 uiout->field_stream ("old", stb);
10265 uiout->text ("\nNew value = ");
10266 }
10267 else
10268 {
10269 mention (b);
10270 if (uiout->is_mi_like_p ())
10271 uiout->field_string
10272 ("reason",
10273 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10274 tuple_emitter.emplace (uiout, "value");
10275 uiout->text ("\nValue = ");
10276 }
10277 watchpoint_value_print (w->val.get (), &stb);
10278 uiout->field_stream ("new", stb);
10279 uiout->text ("\n");
10280 result = PRINT_UNKNOWN;
10281 break;
10282 default:
10283 result = PRINT_UNKNOWN;
10284 }
10285
10286 return result;
10287 }
10288
10289 /* Implement the "print_mention" breakpoint_ops method for hardware
10290 watchpoints. */
10291
10292 static void
10293 print_mention_watchpoint (struct breakpoint *b)
10294 {
10295 struct watchpoint *w = (struct watchpoint *) b;
10296 struct ui_out *uiout = current_uiout;
10297 const char *tuple_name;
10298
10299 switch (b->type)
10300 {
10301 case bp_watchpoint:
10302 uiout->text ("Watchpoint ");
10303 tuple_name = "wpt";
10304 break;
10305 case bp_hardware_watchpoint:
10306 uiout->text ("Hardware watchpoint ");
10307 tuple_name = "wpt";
10308 break;
10309 case bp_read_watchpoint:
10310 uiout->text ("Hardware read watchpoint ");
10311 tuple_name = "hw-rwpt";
10312 break;
10313 case bp_access_watchpoint:
10314 uiout->text ("Hardware access (read/write) watchpoint ");
10315 tuple_name = "hw-awpt";
10316 break;
10317 default:
10318 internal_error (__FILE__, __LINE__,
10319 _("Invalid hardware watchpoint type."));
10320 }
10321
10322 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10323 uiout->field_signed ("number", b->number);
10324 uiout->text (": ");
10325 uiout->field_string ("exp", w->exp_string);
10326 }
10327
10328 /* Implement the "print_recreate" breakpoint_ops method for
10329 watchpoints. */
10330
10331 static void
10332 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10333 {
10334 struct watchpoint *w = (struct watchpoint *) b;
10335
10336 switch (b->type)
10337 {
10338 case bp_watchpoint:
10339 case bp_hardware_watchpoint:
10340 fprintf_unfiltered (fp, "watch");
10341 break;
10342 case bp_read_watchpoint:
10343 fprintf_unfiltered (fp, "rwatch");
10344 break;
10345 case bp_access_watchpoint:
10346 fprintf_unfiltered (fp, "awatch");
10347 break;
10348 default:
10349 internal_error (__FILE__, __LINE__,
10350 _("Invalid watchpoint type."));
10351 }
10352
10353 fprintf_unfiltered (fp, " %s", w->exp_string);
10354 print_recreate_thread (b, fp);
10355 }
10356
10357 /* Implement the "explains_signal" breakpoint_ops method for
10358 watchpoints. */
10359
10360 static int
10361 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10362 {
10363 /* A software watchpoint cannot cause a signal other than
10364 GDB_SIGNAL_TRAP. */
10365 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10366 return 0;
10367
10368 return 1;
10369 }
10370
10371 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10372
10373 static struct breakpoint_ops watchpoint_breakpoint_ops;
10374
10375 /* Implement the "insert" breakpoint_ops method for
10376 masked hardware watchpoints. */
10377
10378 static int
10379 insert_masked_watchpoint (struct bp_location *bl)
10380 {
10381 struct watchpoint *w = (struct watchpoint *) bl->owner;
10382
10383 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10384 bl->watchpoint_type);
10385 }
10386
10387 /* Implement the "remove" breakpoint_ops method for
10388 masked hardware watchpoints. */
10389
10390 static int
10391 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10392 {
10393 struct watchpoint *w = (struct watchpoint *) bl->owner;
10394
10395 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10396 bl->watchpoint_type);
10397 }
10398
10399 /* Implement the "resources_needed" breakpoint_ops method for
10400 masked hardware watchpoints. */
10401
10402 static int
10403 resources_needed_masked_watchpoint (const struct bp_location *bl)
10404 {
10405 struct watchpoint *w = (struct watchpoint *) bl->owner;
10406
10407 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10408 }
10409
10410 /* Implement the "works_in_software_mode" breakpoint_ops method for
10411 masked hardware watchpoints. */
10412
10413 static int
10414 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10415 {
10416 return 0;
10417 }
10418
10419 /* Implement the "print_it" breakpoint_ops method for
10420 masked hardware watchpoints. */
10421
10422 static enum print_stop_action
10423 print_it_masked_watchpoint (bpstat bs)
10424 {
10425 struct breakpoint *b = bs->breakpoint_at;
10426 struct ui_out *uiout = current_uiout;
10427
10428 /* Masked watchpoints have only one location. */
10429 gdb_assert (b->loc && b->loc->next == NULL);
10430
10431 annotate_watchpoint (b->number);
10432 maybe_print_thread_hit_breakpoint (uiout);
10433
10434 switch (b->type)
10435 {
10436 case bp_hardware_watchpoint:
10437 if (uiout->is_mi_like_p ())
10438 uiout->field_string
10439 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10440 break;
10441
10442 case bp_read_watchpoint:
10443 if (uiout->is_mi_like_p ())
10444 uiout->field_string
10445 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10446 break;
10447
10448 case bp_access_watchpoint:
10449 if (uiout->is_mi_like_p ())
10450 uiout->field_string
10451 ("reason",
10452 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10453 break;
10454 default:
10455 internal_error (__FILE__, __LINE__,
10456 _("Invalid hardware watchpoint type."));
10457 }
10458
10459 mention (b);
10460 uiout->text (_("\n\
10461 Check the underlying instruction at PC for the memory\n\
10462 address and value which triggered this watchpoint.\n"));
10463 uiout->text ("\n");
10464
10465 /* More than one watchpoint may have been triggered. */
10466 return PRINT_UNKNOWN;
10467 }
10468
10469 /* Implement the "print_one_detail" breakpoint_ops method for
10470 masked hardware watchpoints. */
10471
10472 static void
10473 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10474 struct ui_out *uiout)
10475 {
10476 struct watchpoint *w = (struct watchpoint *) b;
10477
10478 /* Masked watchpoints have only one location. */
10479 gdb_assert (b->loc && b->loc->next == NULL);
10480
10481 uiout->text ("\tmask ");
10482 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10483 uiout->text ("\n");
10484 }
10485
10486 /* Implement the "print_mention" breakpoint_ops method for
10487 masked hardware watchpoints. */
10488
10489 static void
10490 print_mention_masked_watchpoint (struct breakpoint *b)
10491 {
10492 struct watchpoint *w = (struct watchpoint *) b;
10493 struct ui_out *uiout = current_uiout;
10494 const char *tuple_name;
10495
10496 switch (b->type)
10497 {
10498 case bp_hardware_watchpoint:
10499 uiout->text ("Masked hardware watchpoint ");
10500 tuple_name = "wpt";
10501 break;
10502 case bp_read_watchpoint:
10503 uiout->text ("Masked hardware read watchpoint ");
10504 tuple_name = "hw-rwpt";
10505 break;
10506 case bp_access_watchpoint:
10507 uiout->text ("Masked hardware access (read/write) watchpoint ");
10508 tuple_name = "hw-awpt";
10509 break;
10510 default:
10511 internal_error (__FILE__, __LINE__,
10512 _("Invalid hardware watchpoint type."));
10513 }
10514
10515 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10516 uiout->field_signed ("number", b->number);
10517 uiout->text (": ");
10518 uiout->field_string ("exp", w->exp_string);
10519 }
10520
10521 /* Implement the "print_recreate" breakpoint_ops method for
10522 masked hardware watchpoints. */
10523
10524 static void
10525 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10526 {
10527 struct watchpoint *w = (struct watchpoint *) b;
10528
10529 switch (b->type)
10530 {
10531 case bp_hardware_watchpoint:
10532 fprintf_unfiltered (fp, "watch");
10533 break;
10534 case bp_read_watchpoint:
10535 fprintf_unfiltered (fp, "rwatch");
10536 break;
10537 case bp_access_watchpoint:
10538 fprintf_unfiltered (fp, "awatch");
10539 break;
10540 default:
10541 internal_error (__FILE__, __LINE__,
10542 _("Invalid hardware watchpoint type."));
10543 }
10544
10545 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string,
10546 phex (w->hw_wp_mask, sizeof (CORE_ADDR)));
10547 print_recreate_thread (b, fp);
10548 }
10549
10550 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10551
10552 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10553
10554 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10555
10556 static bool
10557 is_masked_watchpoint (const struct breakpoint *b)
10558 {
10559 return b->ops == &masked_watchpoint_breakpoint_ops;
10560 }
10561
10562 /* accessflag: hw_write: watch write,
10563 hw_read: watch read,
10564 hw_access: watch access (read or write) */
10565 static void
10566 watch_command_1 (const char *arg, int accessflag, int from_tty,
10567 bool just_location, bool internal)
10568 {
10569 struct breakpoint *scope_breakpoint = NULL;
10570 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10571 struct value *result;
10572 int saved_bitpos = 0, saved_bitsize = 0;
10573 const char *exp_start = NULL;
10574 const char *exp_end = NULL;
10575 const char *tok, *end_tok;
10576 int toklen = -1;
10577 const char *cond_start = NULL;
10578 const char *cond_end = NULL;
10579 enum bptype bp_type;
10580 int thread = -1;
10581 /* Flag to indicate whether we are going to use masks for
10582 the hardware watchpoint. */
10583 bool use_mask = false;
10584 CORE_ADDR mask = 0;
10585
10586 /* Make sure that we actually have parameters to parse. */
10587 if (arg != NULL && arg[0] != '\0')
10588 {
10589 const char *value_start;
10590
10591 exp_end = arg + strlen (arg);
10592
10593 /* Look for "parameter value" pairs at the end
10594 of the arguments string. */
10595 for (tok = exp_end - 1; tok > arg; tok--)
10596 {
10597 /* Skip whitespace at the end of the argument list. */
10598 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10599 tok--;
10600
10601 /* Find the beginning of the last token.
10602 This is the value of the parameter. */
10603 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10604 tok--;
10605 value_start = tok + 1;
10606
10607 /* Skip whitespace. */
10608 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10609 tok--;
10610
10611 end_tok = tok;
10612
10613 /* Find the beginning of the second to last token.
10614 This is the parameter itself. */
10615 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10616 tok--;
10617 tok++;
10618 toklen = end_tok - tok + 1;
10619
10620 if (toklen == 6 && startswith (tok, "thread"))
10621 {
10622 struct thread_info *thr;
10623 /* At this point we've found a "thread" token, which means
10624 the user is trying to set a watchpoint that triggers
10625 only in a specific thread. */
10626 const char *endp;
10627
10628 if (thread != -1)
10629 error(_("You can specify only one thread."));
10630
10631 /* Extract the thread ID from the next token. */
10632 thr = parse_thread_id (value_start, &endp);
10633
10634 /* Check if the user provided a valid thread ID. */
10635 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10636 invalid_thread_id_error (value_start);
10637
10638 thread = thr->global_num;
10639 }
10640 else if (toklen == 4 && startswith (tok, "mask"))
10641 {
10642 /* We've found a "mask" token, which means the user wants to
10643 create a hardware watchpoint that is going to have the mask
10644 facility. */
10645 struct value *mask_value, *mark;
10646
10647 if (use_mask)
10648 error(_("You can specify only one mask."));
10649
10650 use_mask = just_location = true;
10651
10652 mark = value_mark ();
10653 mask_value = parse_to_comma_and_eval (&value_start);
10654 mask = value_as_address (mask_value);
10655 value_free_to_mark (mark);
10656 }
10657 else
10658 /* We didn't recognize what we found. We should stop here. */
10659 break;
10660
10661 /* Truncate the string and get rid of the "parameter value" pair before
10662 the arguments string is parsed by the parse_exp_1 function. */
10663 exp_end = tok;
10664 }
10665 }
10666 else
10667 exp_end = arg;
10668
10669 /* Parse the rest of the arguments. From here on out, everything
10670 is in terms of a newly allocated string instead of the original
10671 ARG. */
10672 std::string expression (arg, exp_end - arg);
10673 exp_start = arg = expression.c_str ();
10674 innermost_block_tracker tracker;
10675 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10676 exp_end = arg;
10677 /* Remove trailing whitespace from the expression before saving it.
10678 This makes the eventual display of the expression string a bit
10679 prettier. */
10680 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10681 --exp_end;
10682
10683 /* Checking if the expression is not constant. */
10684 if (watchpoint_exp_is_const (exp.get ()))
10685 {
10686 int len;
10687
10688 len = exp_end - exp_start;
10689 while (len > 0 && isspace (exp_start[len - 1]))
10690 len--;
10691 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10692 }
10693
10694 exp_valid_block = tracker.block ();
10695 struct value *mark = value_mark ();
10696 struct value *val_as_value = nullptr;
10697 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10698 just_location);
10699
10700 if (val_as_value != NULL && just_location)
10701 {
10702 saved_bitpos = value_bitpos (val_as_value);
10703 saved_bitsize = value_bitsize (val_as_value);
10704 }
10705
10706 value_ref_ptr val;
10707 if (just_location)
10708 {
10709 int ret;
10710
10711 exp_valid_block = NULL;
10712 val = release_value (value_addr (result));
10713 value_free_to_mark (mark);
10714
10715 if (use_mask)
10716 {
10717 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10718 mask);
10719 if (ret == -1)
10720 error (_("This target does not support masked watchpoints."));
10721 else if (ret == -2)
10722 error (_("Invalid mask or memory region."));
10723 }
10724 }
10725 else if (val_as_value != NULL)
10726 val = release_value (val_as_value);
10727
10728 tok = skip_spaces (arg);
10729 end_tok = skip_to_space (tok);
10730
10731 toklen = end_tok - tok;
10732 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10733 {
10734 tok = cond_start = end_tok + 1;
10735 innermost_block_tracker if_tracker;
10736 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10737
10738 /* The watchpoint expression may not be local, but the condition
10739 may still be. E.g.: `watch global if local > 0'. */
10740 cond_exp_valid_block = if_tracker.block ();
10741
10742 cond_end = tok;
10743 }
10744 if (*tok)
10745 error (_("Junk at end of command."));
10746
10747 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10748
10749 /* Save this because create_internal_breakpoint below invalidates
10750 'wp_frame'. */
10751 frame_id watchpoint_frame = get_frame_id (wp_frame);
10752
10753 /* If the expression is "local", then set up a "watchpoint scope"
10754 breakpoint at the point where we've left the scope of the watchpoint
10755 expression. Create the scope breakpoint before the watchpoint, so
10756 that we will encounter it first in bpstat_stop_status. */
10757 if (exp_valid_block != NULL && wp_frame != NULL)
10758 {
10759 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10760
10761 if (frame_id_p (caller_frame_id))
10762 {
10763 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10764 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10765
10766 scope_breakpoint
10767 = create_internal_breakpoint (caller_arch, caller_pc,
10768 bp_watchpoint_scope,
10769 &momentary_breakpoint_ops);
10770
10771 /* create_internal_breakpoint could invalidate WP_FRAME. */
10772 wp_frame = NULL;
10773
10774 scope_breakpoint->enable_state = bp_enabled;
10775
10776 /* Automatically delete the breakpoint when it hits. */
10777 scope_breakpoint->disposition = disp_del;
10778
10779 /* Only break in the proper frame (help with recursion). */
10780 scope_breakpoint->frame_id = caller_frame_id;
10781
10782 /* Set the address at which we will stop. */
10783 scope_breakpoint->loc->gdbarch = caller_arch;
10784 scope_breakpoint->loc->requested_address = caller_pc;
10785 scope_breakpoint->loc->address
10786 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10787 scope_breakpoint->loc->requested_address,
10788 scope_breakpoint->type);
10789 }
10790 }
10791
10792 /* Now set up the breakpoint. We create all watchpoints as hardware
10793 watchpoints here even if hardware watchpoints are turned off, a call
10794 to update_watchpoint later in this function will cause the type to
10795 drop back to bp_watchpoint (software watchpoint) if required. */
10796
10797 if (accessflag == hw_read)
10798 bp_type = bp_read_watchpoint;
10799 else if (accessflag == hw_access)
10800 bp_type = bp_access_watchpoint;
10801 else
10802 bp_type = bp_hardware_watchpoint;
10803
10804 std::unique_ptr<watchpoint> w (new watchpoint ());
10805
10806 if (use_mask)
10807 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10808 &masked_watchpoint_breakpoint_ops);
10809 else
10810 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10811 &watchpoint_breakpoint_ops);
10812 w->thread = thread;
10813 w->disposition = disp_donttouch;
10814 w->pspace = current_program_space;
10815 w->exp = std::move (exp);
10816 w->exp_valid_block = exp_valid_block;
10817 w->cond_exp_valid_block = cond_exp_valid_block;
10818 if (just_location)
10819 {
10820 struct type *t = value_type (val.get ());
10821 CORE_ADDR addr = value_as_address (val.get ());
10822
10823 w->exp_string_reparse
10824 = current_language->watch_location_expression (t, addr).release ();
10825
10826 w->exp_string = xstrprintf ("-location %.*s",
10827 (int) (exp_end - exp_start), exp_start);
10828 }
10829 else
10830 w->exp_string = savestring (exp_start, exp_end - exp_start);
10831
10832 if (use_mask)
10833 {
10834 w->hw_wp_mask = mask;
10835 }
10836 else
10837 {
10838 w->val = val;
10839 w->val_bitpos = saved_bitpos;
10840 w->val_bitsize = saved_bitsize;
10841 w->val_valid = true;
10842 }
10843
10844 if (cond_start)
10845 w->cond_string = savestring (cond_start, cond_end - cond_start);
10846 else
10847 w->cond_string = 0;
10848
10849 if (frame_id_p (watchpoint_frame))
10850 {
10851 w->watchpoint_frame = watchpoint_frame;
10852 w->watchpoint_thread = inferior_ptid;
10853 }
10854 else
10855 {
10856 w->watchpoint_frame = null_frame_id;
10857 w->watchpoint_thread = null_ptid;
10858 }
10859
10860 if (scope_breakpoint != NULL)
10861 {
10862 /* The scope breakpoint is related to the watchpoint. We will
10863 need to act on them together. */
10864 w->related_breakpoint = scope_breakpoint;
10865 scope_breakpoint->related_breakpoint = w.get ();
10866 }
10867
10868 if (!just_location)
10869 value_free_to_mark (mark);
10870
10871 /* Finally update the new watchpoint. This creates the locations
10872 that should be inserted. */
10873 update_watchpoint (w.get (), 1);
10874
10875 install_breakpoint (internal, std::move (w), 1);
10876 }
10877
10878 /* Return count of debug registers needed to watch the given expression.
10879 If the watchpoint cannot be handled in hardware return zero. */
10880
10881 static int
10882 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10883 {
10884 int found_memory_cnt = 0;
10885
10886 /* Did the user specifically forbid us to use hardware watchpoints? */
10887 if (!can_use_hw_watchpoints)
10888 return 0;
10889
10890 gdb_assert (!vals.empty ());
10891 struct value *head = vals[0].get ();
10892
10893 /* Make sure that the value of the expression depends only upon
10894 memory contents, and values computed from them within GDB. If we
10895 find any register references or function calls, we can't use a
10896 hardware watchpoint.
10897
10898 The idea here is that evaluating an expression generates a series
10899 of values, one holding the value of every subexpression. (The
10900 expression a*b+c has five subexpressions: a, b, a*b, c, and
10901 a*b+c.) GDB's values hold almost enough information to establish
10902 the criteria given above --- they identify memory lvalues,
10903 register lvalues, computed values, etcetera. So we can evaluate
10904 the expression, and then scan the chain of values that leaves
10905 behind to decide whether we can detect any possible change to the
10906 expression's final value using only hardware watchpoints.
10907
10908 However, I don't think that the values returned by inferior
10909 function calls are special in any way. So this function may not
10910 notice that an expression involving an inferior function call
10911 can't be watched with hardware watchpoints. FIXME. */
10912 for (const value_ref_ptr &iter : vals)
10913 {
10914 struct value *v = iter.get ();
10915
10916 if (VALUE_LVAL (v) == lval_memory)
10917 {
10918 if (v != head && value_lazy (v))
10919 /* A lazy memory lvalue in the chain is one that GDB never
10920 needed to fetch; we either just used its address (e.g.,
10921 `a' in `a.b') or we never needed it at all (e.g., `a'
10922 in `a,b'). This doesn't apply to HEAD; if that is
10923 lazy then it was not readable, but watch it anyway. */
10924 ;
10925 else
10926 {
10927 /* Ahh, memory we actually used! Check if we can cover
10928 it with hardware watchpoints. */
10929 struct type *vtype = check_typedef (value_type (v));
10930
10931 /* We only watch structs and arrays if user asked for it
10932 explicitly, never if they just happen to appear in a
10933 middle of some value chain. */
10934 if (v == head
10935 || (vtype->code () != TYPE_CODE_STRUCT
10936 && vtype->code () != TYPE_CODE_ARRAY))
10937 {
10938 CORE_ADDR vaddr = value_address (v);
10939 int len;
10940 int num_regs;
10941
10942 len = (target_exact_watchpoints
10943 && is_scalar_type_recursive (vtype))?
10944 1 : TYPE_LENGTH (value_type (v));
10945
10946 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10947 if (!num_regs)
10948 return 0;
10949 else
10950 found_memory_cnt += num_regs;
10951 }
10952 }
10953 }
10954 else if (VALUE_LVAL (v) != not_lval
10955 && deprecated_value_modifiable (v) == 0)
10956 return 0; /* These are values from the history (e.g., $1). */
10957 else if (VALUE_LVAL (v) == lval_register)
10958 return 0; /* Cannot watch a register with a HW watchpoint. */
10959 }
10960
10961 /* The expression itself looks suitable for using a hardware
10962 watchpoint, but give the target machine a chance to reject it. */
10963 return found_memory_cnt;
10964 }
10965
10966 void
10967 watch_command_wrapper (const char *arg, int from_tty, bool internal)
10968 {
10969 watch_command_1 (arg, hw_write, from_tty, 0, internal);
10970 }
10971
10972 /* Options for the watch, awatch, and rwatch commands. */
10973
10974 struct watch_options
10975 {
10976 /* For -location. */
10977 bool location = false;
10978 };
10979
10980 /* Definitions of options for the "watch", "awatch", and "rwatch" commands.
10981
10982 Historically GDB always accepted both '-location' and '-l' flags for
10983 these commands (both flags being synonyms). When converting to the
10984 newer option scheme only '-location' is added here. That's fine (for
10985 backward compatibility) as any non-ambiguous prefix of a flag will be
10986 accepted, so '-l', '-loc', are now all accepted.
10987
10988 What this means is that, if in the future, we add any new flag here
10989 that starts with '-l' then this will break backward compatibility, so
10990 please, don't do that! */
10991
10992 static const gdb::option::option_def watch_option_defs[] = {
10993 gdb::option::flag_option_def<watch_options> {
10994 "location",
10995 [] (watch_options *opt) { return &opt->location; },
10996 N_("\
10997 This evaluates EXPRESSION and watches the memory to which is refers.\n\
10998 -l can be used as a short form of -location."),
10999 },
11000 };
11001
11002 /* Returns the option group used by 'watch', 'awatch', and 'rwatch'
11003 commands. */
11004
11005 static gdb::option::option_def_group
11006 make_watch_options_def_group (watch_options *opts)
11007 {
11008 return {{watch_option_defs}, opts};
11009 }
11010
11011 /* A helper function that looks for the "-location" argument and then
11012 calls watch_command_1. */
11013
11014 static void
11015 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
11016 {
11017 watch_options opts;
11018 auto grp = make_watch_options_def_group (&opts);
11019 gdb::option::process_options
11020 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
11021 if (arg != nullptr && *arg == '\0')
11022 arg = nullptr;
11023
11024 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
11025 }
11026
11027 /* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
11028 static void
11029 watch_command_completer (struct cmd_list_element *ignore,
11030 completion_tracker &tracker,
11031 const char *text, const char * /*word*/)
11032 {
11033 const auto group = make_watch_options_def_group (nullptr);
11034 if (gdb::option::complete_options
11035 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
11036 return;
11037
11038 const char *word = advance_to_expression_complete_word_point (tracker, text);
11039 expression_completer (ignore, tracker, text, word);
11040 }
11041
11042 static void
11043 watch_command (const char *arg, int from_tty)
11044 {
11045 watch_maybe_just_location (arg, hw_write, from_tty);
11046 }
11047
11048 void
11049 rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
11050 {
11051 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11052 }
11053
11054 static void
11055 rwatch_command (const char *arg, int from_tty)
11056 {
11057 watch_maybe_just_location (arg, hw_read, from_tty);
11058 }
11059
11060 void
11061 awatch_command_wrapper (const char *arg, int from_tty, bool internal)
11062 {
11063 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11064 }
11065
11066 static void
11067 awatch_command (const char *arg, int from_tty)
11068 {
11069 watch_maybe_just_location (arg, hw_access, from_tty);
11070 }
11071 \f
11072
11073 /* Data for the FSM that manages the until(location)/advance commands
11074 in infcmd.c. Here because it uses the mechanisms of
11075 breakpoints. */
11076
11077 struct until_break_fsm : public thread_fsm
11078 {
11079 /* The thread that was current when the command was executed. */
11080 int thread;
11081
11082 /* The breakpoint set at the return address in the caller frame,
11083 plus breakpoints at all the destination locations. */
11084 std::vector<breakpoint_up> breakpoints;
11085
11086 until_break_fsm (struct interp *cmd_interp, int thread,
11087 std::vector<breakpoint_up> &&breakpoints)
11088 : thread_fsm (cmd_interp),
11089 thread (thread),
11090 breakpoints (std::move (breakpoints))
11091 {
11092 }
11093
11094 void clean_up (struct thread_info *thread) override;
11095 bool should_stop (struct thread_info *thread) override;
11096 enum async_reply_reason do_async_reply_reason () override;
11097 };
11098
11099 /* Implementation of the 'should_stop' FSM method for the
11100 until(location)/advance commands. */
11101
11102 bool
11103 until_break_fsm::should_stop (struct thread_info *tp)
11104 {
11105 for (const breakpoint_up &bp : breakpoints)
11106 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11107 bp.get ()) != NULL)
11108 {
11109 set_finished ();
11110 break;
11111 }
11112
11113 return true;
11114 }
11115
11116 /* Implementation of the 'clean_up' FSM method for the
11117 until(location)/advance commands. */
11118
11119 void
11120 until_break_fsm::clean_up (struct thread_info *)
11121 {
11122 /* Clean up our temporary breakpoints. */
11123 breakpoints.clear ();
11124 delete_longjmp_breakpoint (thread);
11125 }
11126
11127 /* Implementation of the 'async_reply_reason' FSM method for the
11128 until(location)/advance commands. */
11129
11130 enum async_reply_reason
11131 until_break_fsm::do_async_reply_reason ()
11132 {
11133 return EXEC_ASYNC_LOCATION_REACHED;
11134 }
11135
11136 void
11137 until_break_command (const char *arg, int from_tty, int anywhere)
11138 {
11139 struct frame_info *frame;
11140 struct gdbarch *frame_gdbarch;
11141 struct frame_id stack_frame_id;
11142 struct frame_id caller_frame_id;
11143 int thread;
11144 struct thread_info *tp;
11145
11146 clear_proceed_status (0);
11147
11148 /* Set a breakpoint where the user wants it and at return from
11149 this function. */
11150
11151 event_location_up location = string_to_event_location (&arg, current_language);
11152
11153 std::vector<symtab_and_line> sals
11154 = (last_displayed_sal_is_valid ()
11155 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11156 get_last_displayed_symtab (),
11157 get_last_displayed_line ())
11158 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11159 NULL, NULL, 0));
11160
11161 if (sals.empty ())
11162 error (_("Couldn't get information on specified line."));
11163
11164 if (*arg)
11165 error (_("Junk at end of arguments."));
11166
11167 tp = inferior_thread ();
11168 thread = tp->global_num;
11169
11170 /* Note linespec handling above invalidates the frame chain.
11171 Installing a breakpoint also invalidates the frame chain (as it
11172 may need to switch threads), so do any frame handling before
11173 that. */
11174
11175 frame = get_selected_frame (NULL);
11176 frame_gdbarch = get_frame_arch (frame);
11177 stack_frame_id = get_stack_frame_id (frame);
11178 caller_frame_id = frame_unwind_caller_id (frame);
11179
11180 /* Keep within the current frame, or in frames called by the current
11181 one. */
11182
11183 std::vector<breakpoint_up> breakpoints;
11184
11185 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11186
11187 if (frame_id_p (caller_frame_id))
11188 {
11189 struct symtab_and_line sal2;
11190 struct gdbarch *caller_gdbarch;
11191
11192 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11193 sal2.pc = frame_unwind_caller_pc (frame);
11194 caller_gdbarch = frame_unwind_caller_arch (frame);
11195
11196 breakpoint_up caller_breakpoint
11197 = set_momentary_breakpoint (caller_gdbarch, sal2,
11198 caller_frame_id, bp_until);
11199 breakpoints.emplace_back (std::move (caller_breakpoint));
11200
11201 set_longjmp_breakpoint (tp, caller_frame_id);
11202 lj_deleter.emplace (thread);
11203 }
11204
11205 /* set_momentary_breakpoint could invalidate FRAME. */
11206 frame = NULL;
11207
11208 /* If the user told us to continue until a specified location, we
11209 don't specify a frame at which we need to stop. Otherwise,
11210 specify the selected frame, because we want to stop only at the
11211 very same frame. */
11212 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
11213
11214 for (symtab_and_line &sal : sals)
11215 {
11216 resolve_sal_pc (&sal);
11217
11218 breakpoint_up location_breakpoint
11219 = set_momentary_breakpoint (frame_gdbarch, sal,
11220 stop_frame_id, bp_until);
11221 breakpoints.emplace_back (std::move (location_breakpoint));
11222 }
11223
11224 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
11225 std::move (breakpoints));
11226
11227 if (lj_deleter)
11228 lj_deleter->release ();
11229
11230 proceed (-1, GDB_SIGNAL_DEFAULT);
11231 }
11232
11233 /* This function attempts to parse an optional "if <cond>" clause
11234 from the arg string. If one is not found, it returns NULL.
11235
11236 Else, it returns a pointer to the condition string. (It does not
11237 attempt to evaluate the string against a particular block.) And,
11238 it updates arg to point to the first character following the parsed
11239 if clause in the arg string. */
11240
11241 const char *
11242 ep_parse_optional_if_clause (const char **arg)
11243 {
11244 const char *cond_string;
11245
11246 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11247 return NULL;
11248
11249 /* Skip the "if" keyword. */
11250 (*arg) += 2;
11251
11252 /* Skip any extra leading whitespace, and record the start of the
11253 condition string. */
11254 *arg = skip_spaces (*arg);
11255 cond_string = *arg;
11256
11257 /* Assume that the condition occupies the remainder of the arg
11258 string. */
11259 (*arg) += strlen (cond_string);
11260
11261 return cond_string;
11262 }
11263
11264 /* Commands to deal with catching events, such as signals, exceptions,
11265 process start/exit, etc. */
11266
11267 typedef enum
11268 {
11269 catch_fork_temporary, catch_vfork_temporary,
11270 catch_fork_permanent, catch_vfork_permanent
11271 }
11272 catch_fork_kind;
11273
11274 static void
11275 catch_fork_command_1 (const char *arg, int from_tty,
11276 struct cmd_list_element *command)
11277 {
11278 struct gdbarch *gdbarch = get_current_arch ();
11279 const char *cond_string = NULL;
11280 catch_fork_kind fork_kind;
11281
11282 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11283 bool temp = (fork_kind == catch_fork_temporary
11284 || fork_kind == catch_vfork_temporary);
11285
11286 if (!arg)
11287 arg = "";
11288 arg = skip_spaces (arg);
11289
11290 /* The allowed syntax is:
11291 catch [v]fork
11292 catch [v]fork if <cond>
11293
11294 First, check if there's an if clause. */
11295 cond_string = ep_parse_optional_if_clause (&arg);
11296
11297 if ((*arg != '\0') && !isspace (*arg))
11298 error (_("Junk at end of arguments."));
11299
11300 /* If this target supports it, create a fork or vfork catchpoint
11301 and enable reporting of such events. */
11302 switch (fork_kind)
11303 {
11304 case catch_fork_temporary:
11305 case catch_fork_permanent:
11306 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
11307 &catch_fork_breakpoint_ops);
11308 break;
11309 case catch_vfork_temporary:
11310 case catch_vfork_permanent:
11311 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
11312 &catch_vfork_breakpoint_ops);
11313 break;
11314 default:
11315 error (_("unsupported or unknown fork kind; cannot catch it"));
11316 break;
11317 }
11318 }
11319
11320 static void
11321 catch_exec_command_1 (const char *arg, int from_tty,
11322 struct cmd_list_element *command)
11323 {
11324 struct gdbarch *gdbarch = get_current_arch ();
11325 const char *cond_string = NULL;
11326 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
11327
11328 if (!arg)
11329 arg = "";
11330 arg = skip_spaces (arg);
11331
11332 /* The allowed syntax is:
11333 catch exec
11334 catch exec if <cond>
11335
11336 First, check if there's an if clause. */
11337 cond_string = ep_parse_optional_if_clause (&arg);
11338
11339 if ((*arg != '\0') && !isspace (*arg))
11340 error (_("Junk at end of arguments."));
11341
11342 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11343 init_catchpoint (c.get (), gdbarch, temp, cond_string,
11344 &catch_exec_breakpoint_ops);
11345 c->exec_pathname = NULL;
11346
11347 install_breakpoint (0, std::move (c), 1);
11348 }
11349
11350 void
11351 init_ada_exception_breakpoint (struct breakpoint *b,
11352 struct gdbarch *gdbarch,
11353 struct symtab_and_line sal,
11354 const char *addr_string,
11355 const struct breakpoint_ops *ops,
11356 int tempflag,
11357 int enabled,
11358 int from_tty)
11359 {
11360 if (from_tty)
11361 {
11362 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11363 if (!loc_gdbarch)
11364 loc_gdbarch = gdbarch;
11365
11366 describe_other_breakpoints (loc_gdbarch,
11367 sal.pspace, sal.pc, sal.section, -1);
11368 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11369 version for exception catchpoints, because two catchpoints
11370 used for different exception names will use the same address.
11371 In this case, a "breakpoint ... also set at..." warning is
11372 unproductive. Besides, the warning phrasing is also a bit
11373 inappropriate, we should use the word catchpoint, and tell
11374 the user what type of catchpoint it is. The above is good
11375 enough for now, though. */
11376 }
11377
11378 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
11379
11380 b->enable_state = enabled ? bp_enabled : bp_disabled;
11381 b->disposition = tempflag ? disp_del : disp_donttouch;
11382 b->location = string_to_event_location (&addr_string,
11383 language_def (language_ada));
11384 b->language = language_ada;
11385 }
11386
11387 \f
11388
11389 /* Compare two breakpoints and return a strcmp-like result. */
11390
11391 static int
11392 compare_breakpoints (const breakpoint *a, const breakpoint *b)
11393 {
11394 uintptr_t ua = (uintptr_t) a;
11395 uintptr_t ub = (uintptr_t) b;
11396
11397 if (a->number < b->number)
11398 return -1;
11399 else if (a->number > b->number)
11400 return 1;
11401
11402 /* Now sort by address, in case we see, e..g, two breakpoints with
11403 the number 0. */
11404 if (ua < ub)
11405 return -1;
11406 return ua > ub ? 1 : 0;
11407 }
11408
11409 /* Delete breakpoints by address or line. */
11410
11411 static void
11412 clear_command (const char *arg, int from_tty)
11413 {
11414 int default_match;
11415
11416 std::vector<symtab_and_line> decoded_sals;
11417 symtab_and_line last_sal;
11418 gdb::array_view<symtab_and_line> sals;
11419 if (arg)
11420 {
11421 decoded_sals
11422 = decode_line_with_current_source (arg,
11423 (DECODE_LINE_FUNFIRSTLINE
11424 | DECODE_LINE_LIST_MODE));
11425 default_match = 0;
11426 sals = decoded_sals;
11427 }
11428 else
11429 {
11430 /* Set sal's line, symtab, pc, and pspace to the values
11431 corresponding to the last call to print_frame_info. If the
11432 codepoint is not valid, this will set all the fields to 0. */
11433 last_sal = get_last_displayed_sal ();
11434 if (last_sal.symtab == 0)
11435 error (_("No source file specified."));
11436
11437 default_match = 1;
11438 sals = last_sal;
11439 }
11440
11441 /* We don't call resolve_sal_pc here. That's not as bad as it
11442 seems, because all existing breakpoints typically have both
11443 file/line and pc set. So, if clear is given file/line, we can
11444 match this to existing breakpoint without obtaining pc at all.
11445
11446 We only support clearing given the address explicitly
11447 present in breakpoint table. Say, we've set breakpoint
11448 at file:line. There were several PC values for that file:line,
11449 due to optimization, all in one block.
11450
11451 We've picked one PC value. If "clear" is issued with another
11452 PC corresponding to the same file:line, the breakpoint won't
11453 be cleared. We probably can still clear the breakpoint, but
11454 since the other PC value is never presented to user, user
11455 can only find it by guessing, and it does not seem important
11456 to support that. */
11457
11458 /* For each line spec given, delete bps which correspond to it. Do
11459 it in two passes, solely to preserve the current behavior that
11460 from_tty is forced true if we delete more than one
11461 breakpoint. */
11462
11463 std::vector<struct breakpoint *> found;
11464 for (const auto &sal : sals)
11465 {
11466 const char *sal_fullname;
11467
11468 /* If exact pc given, clear bpts at that pc.
11469 If line given (pc == 0), clear all bpts on specified line.
11470 If defaulting, clear all bpts on default line
11471 or at default pc.
11472
11473 defaulting sal.pc != 0 tests to do
11474
11475 0 1 pc
11476 1 1 pc _and_ line
11477 0 0 line
11478 1 0 <can't happen> */
11479
11480 sal_fullname = (sal.symtab == NULL
11481 ? NULL : symtab_to_fullname (sal.symtab));
11482
11483 /* Find all matching breakpoints and add them to 'found'. */
11484 for (breakpoint *b : all_breakpoints ())
11485 {
11486 int match = 0;
11487 /* Are we going to delete b? */
11488 if (b->type != bp_none && !is_watchpoint (b))
11489 {
11490 for (bp_location *loc : b->locations ())
11491 {
11492 /* If the user specified file:line, don't allow a PC
11493 match. This matches historical gdb behavior. */
11494 int pc_match = (!sal.explicit_line
11495 && sal.pc
11496 && (loc->pspace == sal.pspace)
11497 && (loc->address == sal.pc)
11498 && (!section_is_overlay (loc->section)
11499 || loc->section == sal.section));
11500 int line_match = 0;
11501
11502 if ((default_match || sal.explicit_line)
11503 && loc->symtab != NULL
11504 && sal_fullname != NULL
11505 && sal.pspace == loc->pspace
11506 && loc->line_number == sal.line
11507 && filename_cmp (symtab_to_fullname (loc->symtab),
11508 sal_fullname) == 0)
11509 line_match = 1;
11510
11511 if (pc_match || line_match)
11512 {
11513 match = 1;
11514 break;
11515 }
11516 }
11517 }
11518
11519 if (match)
11520 found.push_back (b);
11521 }
11522 }
11523
11524 /* Now go thru the 'found' chain and delete them. */
11525 if (found.empty ())
11526 {
11527 if (arg)
11528 error (_("No breakpoint at %s."), arg);
11529 else
11530 error (_("No breakpoint at this line."));
11531 }
11532
11533 /* Remove duplicates from the vec. */
11534 std::sort (found.begin (), found.end (),
11535 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11536 {
11537 return compare_breakpoints (bp_a, bp_b) < 0;
11538 });
11539 found.erase (std::unique (found.begin (), found.end (),
11540 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11541 {
11542 return compare_breakpoints (bp_a, bp_b) == 0;
11543 }),
11544 found.end ());
11545
11546 if (found.size () > 1)
11547 from_tty = 1; /* Always report if deleted more than one. */
11548 if (from_tty)
11549 {
11550 if (found.size () == 1)
11551 printf_unfiltered (_("Deleted breakpoint "));
11552 else
11553 printf_unfiltered (_("Deleted breakpoints "));
11554 }
11555
11556 for (breakpoint *iter : found)
11557 {
11558 if (from_tty)
11559 printf_unfiltered ("%d ", iter->number);
11560 delete_breakpoint (iter);
11561 }
11562 if (from_tty)
11563 putchar_unfiltered ('\n');
11564 }
11565 \f
11566 /* Delete breakpoint in BS if they are `delete' breakpoints and
11567 all breakpoints that are marked for deletion, whether hit or not.
11568 This is called after any breakpoint is hit, or after errors. */
11569
11570 void
11571 breakpoint_auto_delete (bpstat bs)
11572 {
11573 for (; bs; bs = bs->next)
11574 if (bs->breakpoint_at
11575 && bs->breakpoint_at->disposition == disp_del
11576 && bs->stop)
11577 delete_breakpoint (bs->breakpoint_at);
11578
11579 for (breakpoint *b : all_breakpoints_safe ())
11580 if (b->disposition == disp_del_at_next_stop)
11581 delete_breakpoint (b);
11582 }
11583
11584 /* A comparison function for bp_location AP and BP being interfaced to
11585 std::sort. Sort elements primarily by their ADDRESS (no matter what
11586 bl_address_is_meaningful says), secondarily by ordering first
11587 permanent elements and terciarily just ensuring the array is sorted
11588 stable way despite std::sort being an unstable algorithm. */
11589
11590 static int
11591 bp_location_is_less_than (const bp_location *a, const bp_location *b)
11592 {
11593 if (a->address != b->address)
11594 return a->address < b->address;
11595
11596 /* Sort locations at the same address by their pspace number, keeping
11597 locations of the same inferior (in a multi-inferior environment)
11598 grouped. */
11599
11600 if (a->pspace->num != b->pspace->num)
11601 return a->pspace->num < b->pspace->num;
11602
11603 /* Sort permanent breakpoints first. */
11604 if (a->permanent != b->permanent)
11605 return a->permanent > b->permanent;
11606
11607 /* Sort by type in order to make duplicate determination easier.
11608 See update_global_location_list. This is kept in sync with
11609 breakpoint_locations_match. */
11610 if (a->loc_type < b->loc_type)
11611 return true;
11612
11613 /* Likewise, for range-breakpoints, sort by length. */
11614 if (a->loc_type == bp_loc_hardware_breakpoint
11615 && b->loc_type == bp_loc_hardware_breakpoint
11616 && a->length < b->length)
11617 return true;
11618
11619 /* Make the internal GDB representation stable across GDB runs
11620 where A and B memory inside GDB can differ. Breakpoint locations of
11621 the same type at the same address can be sorted in arbitrary order. */
11622
11623 if (a->owner->number != b->owner->number)
11624 return a->owner->number < b->owner->number;
11625
11626 return a < b;
11627 }
11628
11629 /* Set bp_locations_placed_address_before_address_max and
11630 bp_locations_shadow_len_after_address_max according to the current
11631 content of the bp_locations array. */
11632
11633 static void
11634 bp_locations_target_extensions_update (void)
11635 {
11636 bp_locations_placed_address_before_address_max = 0;
11637 bp_locations_shadow_len_after_address_max = 0;
11638
11639 for (bp_location *bl : all_bp_locations ())
11640 {
11641 CORE_ADDR start, end, addr;
11642
11643 if (!bp_location_has_shadow (bl))
11644 continue;
11645
11646 start = bl->target_info.placed_address;
11647 end = start + bl->target_info.shadow_len;
11648
11649 gdb_assert (bl->address >= start);
11650 addr = bl->address - start;
11651 if (addr > bp_locations_placed_address_before_address_max)
11652 bp_locations_placed_address_before_address_max = addr;
11653
11654 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11655
11656 gdb_assert (bl->address < end);
11657 addr = end - bl->address;
11658 if (addr > bp_locations_shadow_len_after_address_max)
11659 bp_locations_shadow_len_after_address_max = addr;
11660 }
11661 }
11662
11663 /* Download tracepoint locations if they haven't been. */
11664
11665 static void
11666 download_tracepoint_locations (void)
11667 {
11668 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11669
11670 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11671
11672 for (breakpoint *b : all_tracepoints ())
11673 {
11674 struct tracepoint *t;
11675 int bp_location_downloaded = 0;
11676
11677 if ((b->type == bp_fast_tracepoint
11678 ? !may_insert_fast_tracepoints
11679 : !may_insert_tracepoints))
11680 continue;
11681
11682 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11683 {
11684 if (target_can_download_tracepoint ())
11685 can_download_tracepoint = TRIBOOL_TRUE;
11686 else
11687 can_download_tracepoint = TRIBOOL_FALSE;
11688 }
11689
11690 if (can_download_tracepoint == TRIBOOL_FALSE)
11691 break;
11692
11693 for (bp_location *bl : b->locations ())
11694 {
11695 /* In tracepoint, locations are _never_ duplicated, so
11696 should_be_inserted is equivalent to
11697 unduplicated_should_be_inserted. */
11698 if (!should_be_inserted (bl) || bl->inserted)
11699 continue;
11700
11701 switch_to_program_space_and_thread (bl->pspace);
11702
11703 target_download_tracepoint (bl);
11704
11705 bl->inserted = 1;
11706 bp_location_downloaded = 1;
11707 }
11708 t = (struct tracepoint *) b;
11709 t->number_on_target = b->number;
11710 if (bp_location_downloaded)
11711 gdb::observers::breakpoint_modified.notify (b);
11712 }
11713 }
11714
11715 /* Swap the insertion/duplication state between two locations. */
11716
11717 static void
11718 swap_insertion (struct bp_location *left, struct bp_location *right)
11719 {
11720 const int left_inserted = left->inserted;
11721 const int left_duplicate = left->duplicate;
11722 const int left_needs_update = left->needs_update;
11723 const struct bp_target_info left_target_info = left->target_info;
11724
11725 /* Locations of tracepoints can never be duplicated. */
11726 if (is_tracepoint (left->owner))
11727 gdb_assert (!left->duplicate);
11728 if (is_tracepoint (right->owner))
11729 gdb_assert (!right->duplicate);
11730
11731 left->inserted = right->inserted;
11732 left->duplicate = right->duplicate;
11733 left->needs_update = right->needs_update;
11734 left->target_info = right->target_info;
11735 right->inserted = left_inserted;
11736 right->duplicate = left_duplicate;
11737 right->needs_update = left_needs_update;
11738 right->target_info = left_target_info;
11739 }
11740
11741 /* Force the re-insertion of the locations at ADDRESS. This is called
11742 once a new/deleted/modified duplicate location is found and we are evaluating
11743 conditions on the target's side. Such conditions need to be updated on
11744 the target. */
11745
11746 static void
11747 force_breakpoint_reinsertion (struct bp_location *bl)
11748 {
11749 struct bp_location **locp = NULL, **loc2p;
11750 struct bp_location *loc;
11751 CORE_ADDR address = 0;
11752 int pspace_num;
11753
11754 address = bl->address;
11755 pspace_num = bl->pspace->num;
11756
11757 /* This is only meaningful if the target is
11758 evaluating conditions and if the user has
11759 opted for condition evaluation on the target's
11760 side. */
11761 if (gdb_evaluates_breakpoint_condition_p ()
11762 || !target_supports_evaluation_of_breakpoint_conditions ())
11763 return;
11764
11765 /* Flag all breakpoint locations with this address and
11766 the same program space as the location
11767 as "its condition has changed". We need to
11768 update the conditions on the target's side. */
11769 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11770 {
11771 loc = *loc2p;
11772
11773 if (!is_breakpoint (loc->owner)
11774 || pspace_num != loc->pspace->num)
11775 continue;
11776
11777 /* Flag the location appropriately. We use a different state to
11778 let everyone know that we already updated the set of locations
11779 with addr bl->address and program space bl->pspace. This is so
11780 we don't have to keep calling these functions just to mark locations
11781 that have already been marked. */
11782 loc->condition_changed = condition_updated;
11783
11784 /* Free the agent expression bytecode as well. We will compute
11785 it later on. */
11786 loc->cond_bytecode.reset ();
11787 }
11788 }
11789
11790 /* Called whether new breakpoints are created, or existing breakpoints
11791 deleted, to update the global location list and recompute which
11792 locations are duplicate of which.
11793
11794 The INSERT_MODE flag determines whether locations may not, may, or
11795 shall be inserted now. See 'enum ugll_insert_mode' for more
11796 info. */
11797
11798 static void
11799 update_global_location_list (enum ugll_insert_mode insert_mode)
11800 {
11801 /* Last breakpoint location address that was marked for update. */
11802 CORE_ADDR last_addr = 0;
11803 /* Last breakpoint location program space that was marked for update. */
11804 int last_pspace_num = -1;
11805
11806 /* Used in the duplicates detection below. When iterating over all
11807 bp_locations, points to the first bp_location of a given address.
11808 Breakpoints and watchpoints of different types are never
11809 duplicates of each other. Keep one pointer for each type of
11810 breakpoint/watchpoint, so we only need to loop over all locations
11811 once. */
11812 struct bp_location *bp_loc_first; /* breakpoint */
11813 struct bp_location *wp_loc_first; /* hardware watchpoint */
11814 struct bp_location *awp_loc_first; /* access watchpoint */
11815 struct bp_location *rwp_loc_first; /* read watchpoint */
11816
11817 /* Saved former bp_locations array which we compare against the newly
11818 built bp_locations from the current state of ALL_BREAKPOINTS. */
11819 std::vector<bp_location *> old_locations = std::move (bp_locations);
11820 bp_locations.clear ();
11821
11822 for (breakpoint *b : all_breakpoints ())
11823 for (bp_location *loc : b->locations ())
11824 bp_locations.push_back (loc);
11825
11826 /* See if we need to "upgrade" a software breakpoint to a hardware
11827 breakpoint. Do this before deciding whether locations are
11828 duplicates. Also do this before sorting because sorting order
11829 depends on location type. */
11830 for (bp_location *loc : bp_locations)
11831 if (!loc->inserted && should_be_inserted (loc))
11832 handle_automatic_hardware_breakpoints (loc);
11833
11834 std::sort (bp_locations.begin (), bp_locations.end (),
11835 bp_location_is_less_than);
11836
11837 bp_locations_target_extensions_update ();
11838
11839 /* Identify bp_location instances that are no longer present in the
11840 new list, and therefore should be freed. Note that it's not
11841 necessary that those locations should be removed from inferior --
11842 if there's another location at the same address (previously
11843 marked as duplicate), we don't need to remove/insert the
11844 location.
11845
11846 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11847 and former bp_location array state respectively. */
11848
11849 size_t loc_i = 0;
11850 for (bp_location *old_loc : old_locations)
11851 {
11852 /* Tells if 'old_loc' is found among the new locations. If
11853 not, we have to free it. */
11854 int found_object = 0;
11855 /* Tells if the location should remain inserted in the target. */
11856 int keep_in_target = 0;
11857 int removed = 0;
11858
11859 /* Skip LOCP entries which will definitely never be needed.
11860 Stop either at or being the one matching OLD_LOC. */
11861 while (loc_i < bp_locations.size ()
11862 && bp_locations[loc_i]->address < old_loc->address)
11863 loc_i++;
11864
11865 for (size_t loc2_i = loc_i;
11866 (loc2_i < bp_locations.size ()
11867 && bp_locations[loc2_i]->address == old_loc->address);
11868 loc2_i++)
11869 {
11870 /* Check if this is a new/duplicated location or a duplicated
11871 location that had its condition modified. If so, we want to send
11872 its condition to the target if evaluation of conditions is taking
11873 place there. */
11874 if (bp_locations[loc2_i]->condition_changed == condition_modified
11875 && (last_addr != old_loc->address
11876 || last_pspace_num != old_loc->pspace->num))
11877 {
11878 force_breakpoint_reinsertion (bp_locations[loc2_i]);
11879 last_pspace_num = old_loc->pspace->num;
11880 }
11881
11882 if (bp_locations[loc2_i] == old_loc)
11883 found_object = 1;
11884 }
11885
11886 /* We have already handled this address, update it so that we don't
11887 have to go through updates again. */
11888 last_addr = old_loc->address;
11889
11890 /* Target-side condition evaluation: Handle deleted locations. */
11891 if (!found_object)
11892 force_breakpoint_reinsertion (old_loc);
11893
11894 /* If this location is no longer present, and inserted, look if
11895 there's maybe a new location at the same address. If so,
11896 mark that one inserted, and don't remove this one. This is
11897 needed so that we don't have a time window where a breakpoint
11898 at certain location is not inserted. */
11899
11900 if (old_loc->inserted)
11901 {
11902 /* If the location is inserted now, we might have to remove
11903 it. */
11904
11905 if (found_object && should_be_inserted (old_loc))
11906 {
11907 /* The location is still present in the location list,
11908 and still should be inserted. Don't do anything. */
11909 keep_in_target = 1;
11910 }
11911 else
11912 {
11913 /* This location still exists, but it won't be kept in the
11914 target since it may have been disabled. We proceed to
11915 remove its target-side condition. */
11916
11917 /* The location is either no longer present, or got
11918 disabled. See if there's another location at the
11919 same address, in which case we don't need to remove
11920 this one from the target. */
11921
11922 /* OLD_LOC comes from existing struct breakpoint. */
11923 if (bl_address_is_meaningful (old_loc))
11924 {
11925 for (size_t loc2_i = loc_i;
11926 (loc2_i < bp_locations.size ()
11927 && bp_locations[loc2_i]->address == old_loc->address);
11928 loc2_i++)
11929 {
11930 bp_location *loc2 = bp_locations[loc2_i];
11931
11932 if (loc2 == old_loc)
11933 continue;
11934
11935 if (breakpoint_locations_match (loc2, old_loc))
11936 {
11937 /* Read watchpoint locations are switched to
11938 access watchpoints, if the former are not
11939 supported, but the latter are. */
11940 if (is_hardware_watchpoint (old_loc->owner))
11941 {
11942 gdb_assert (is_hardware_watchpoint (loc2->owner));
11943 loc2->watchpoint_type = old_loc->watchpoint_type;
11944 }
11945
11946 /* loc2 is a duplicated location. We need to check
11947 if it should be inserted in case it will be
11948 unduplicated. */
11949 if (unduplicated_should_be_inserted (loc2))
11950 {
11951 swap_insertion (old_loc, loc2);
11952 keep_in_target = 1;
11953 break;
11954 }
11955 }
11956 }
11957 }
11958 }
11959
11960 if (!keep_in_target)
11961 {
11962 if (remove_breakpoint (old_loc))
11963 {
11964 /* This is just about all we can do. We could keep
11965 this location on the global list, and try to
11966 remove it next time, but there's no particular
11967 reason why we will succeed next time.
11968
11969 Note that at this point, old_loc->owner is still
11970 valid, as delete_breakpoint frees the breakpoint
11971 only after calling us. */
11972 printf_filtered (_("warning: Error removing "
11973 "breakpoint %d\n"),
11974 old_loc->owner->number);
11975 }
11976 removed = 1;
11977 }
11978 }
11979
11980 if (!found_object)
11981 {
11982 if (removed && target_is_non_stop_p ()
11983 && need_moribund_for_location_type (old_loc))
11984 {
11985 /* This location was removed from the target. In
11986 non-stop mode, a race condition is possible where
11987 we've removed a breakpoint, but stop events for that
11988 breakpoint are already queued and will arrive later.
11989 We apply an heuristic to be able to distinguish such
11990 SIGTRAPs from other random SIGTRAPs: we keep this
11991 breakpoint location for a bit, and will retire it
11992 after we see some number of events. The theory here
11993 is that reporting of events should, "on the average",
11994 be fair, so after a while we'll see events from all
11995 threads that have anything of interest, and no longer
11996 need to keep this breakpoint location around. We
11997 don't hold locations forever so to reduce chances of
11998 mistaking a non-breakpoint SIGTRAP for a breakpoint
11999 SIGTRAP.
12000
12001 The heuristic failing can be disastrous on
12002 decr_pc_after_break targets.
12003
12004 On decr_pc_after_break targets, like e.g., x86-linux,
12005 if we fail to recognize a late breakpoint SIGTRAP,
12006 because events_till_retirement has reached 0 too
12007 soon, we'll fail to do the PC adjustment, and report
12008 a random SIGTRAP to the user. When the user resumes
12009 the inferior, it will most likely immediately crash
12010 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12011 corrupted, because of being resumed e.g., in the
12012 middle of a multi-byte instruction, or skipped a
12013 one-byte instruction. This was actually seen happen
12014 on native x86-linux, and should be less rare on
12015 targets that do not support new thread events, like
12016 remote, due to the heuristic depending on
12017 thread_count.
12018
12019 Mistaking a random SIGTRAP for a breakpoint trap
12020 causes similar symptoms (PC adjustment applied when
12021 it shouldn't), but then again, playing with SIGTRAPs
12022 behind the debugger's back is asking for trouble.
12023
12024 Since hardware watchpoint traps are always
12025 distinguishable from other traps, so we don't need to
12026 apply keep hardware watchpoint moribund locations
12027 around. We simply always ignore hardware watchpoint
12028 traps we can no longer explain. */
12029
12030 process_stratum_target *proc_target = nullptr;
12031 for (inferior *inf : all_inferiors ())
12032 if (inf->pspace == old_loc->pspace)
12033 {
12034 proc_target = inf->process_target ();
12035 break;
12036 }
12037 if (proc_target != nullptr)
12038 old_loc->events_till_retirement
12039 = 3 * (thread_count (proc_target) + 1);
12040 else
12041 old_loc->events_till_retirement = 1;
12042 old_loc->owner = NULL;
12043
12044 moribund_locations.push_back (old_loc);
12045 }
12046 else
12047 {
12048 old_loc->owner = NULL;
12049 decref_bp_location (&old_loc);
12050 }
12051 }
12052 }
12053
12054 /* Rescan breakpoints at the same address and section, marking the
12055 first one as "first" and any others as "duplicates". This is so
12056 that the bpt instruction is only inserted once. If we have a
12057 permanent breakpoint at the same place as BPT, make that one the
12058 official one, and the rest as duplicates. Permanent breakpoints
12059 are sorted first for the same address.
12060
12061 Do the same for hardware watchpoints, but also considering the
12062 watchpoint's type (regular/access/read) and length. */
12063
12064 bp_loc_first = NULL;
12065 wp_loc_first = NULL;
12066 awp_loc_first = NULL;
12067 rwp_loc_first = NULL;
12068
12069 for (bp_location *loc : all_bp_locations ())
12070 {
12071 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12072 non-NULL. */
12073 struct bp_location **loc_first_p;
12074 breakpoint *b = loc->owner;
12075
12076 if (!unduplicated_should_be_inserted (loc)
12077 || !bl_address_is_meaningful (loc)
12078 /* Don't detect duplicate for tracepoint locations because they are
12079 never duplicated. See the comments in field `duplicate' of
12080 `struct bp_location'. */
12081 || is_tracepoint (b))
12082 {
12083 /* Clear the condition modification flag. */
12084 loc->condition_changed = condition_unchanged;
12085 continue;
12086 }
12087
12088 if (b->type == bp_hardware_watchpoint)
12089 loc_first_p = &wp_loc_first;
12090 else if (b->type == bp_read_watchpoint)
12091 loc_first_p = &rwp_loc_first;
12092 else if (b->type == bp_access_watchpoint)
12093 loc_first_p = &awp_loc_first;
12094 else
12095 loc_first_p = &bp_loc_first;
12096
12097 if (*loc_first_p == NULL
12098 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12099 || !breakpoint_locations_match (loc, *loc_first_p))
12100 {
12101 *loc_first_p = loc;
12102 loc->duplicate = 0;
12103
12104 if (is_breakpoint (loc->owner) && loc->condition_changed)
12105 {
12106 loc->needs_update = 1;
12107 /* Clear the condition modification flag. */
12108 loc->condition_changed = condition_unchanged;
12109 }
12110 continue;
12111 }
12112
12113
12114 /* This and the above ensure the invariant that the first location
12115 is not duplicated, and is the inserted one.
12116 All following are marked as duplicated, and are not inserted. */
12117 if (loc->inserted)
12118 swap_insertion (loc, *loc_first_p);
12119 loc->duplicate = 1;
12120
12121 /* Clear the condition modification flag. */
12122 loc->condition_changed = condition_unchanged;
12123 }
12124
12125 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12126 {
12127 if (insert_mode != UGLL_DONT_INSERT)
12128 insert_breakpoint_locations ();
12129 else
12130 {
12131 /* Even though the caller told us to not insert new
12132 locations, we may still need to update conditions on the
12133 target's side of breakpoints that were already inserted
12134 if the target is evaluating breakpoint conditions. We
12135 only update conditions for locations that are marked
12136 "needs_update". */
12137 update_inserted_breakpoint_locations ();
12138 }
12139 }
12140
12141 if (insert_mode != UGLL_DONT_INSERT)
12142 download_tracepoint_locations ();
12143 }
12144
12145 void
12146 breakpoint_retire_moribund (void)
12147 {
12148 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12149 {
12150 struct bp_location *loc = moribund_locations[ix];
12151 if (--(loc->events_till_retirement) == 0)
12152 {
12153 decref_bp_location (&loc);
12154 unordered_remove (moribund_locations, ix);
12155 --ix;
12156 }
12157 }
12158 }
12159
12160 static void
12161 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12162 {
12163
12164 try
12165 {
12166 update_global_location_list (insert_mode);
12167 }
12168 catch (const gdb_exception_error &e)
12169 {
12170 }
12171 }
12172
12173 /* Clear BKP from a BPS. */
12174
12175 static void
12176 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12177 {
12178 bpstat bs;
12179
12180 for (bs = bps; bs; bs = bs->next)
12181 if (bs->breakpoint_at == bpt)
12182 {
12183 bs->breakpoint_at = NULL;
12184 bs->old_val = NULL;
12185 /* bs->commands will be freed later. */
12186 }
12187 }
12188
12189 /* Callback for iterate_over_threads. */
12190 static int
12191 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12192 {
12193 struct breakpoint *bpt = (struct breakpoint *) data;
12194
12195 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12196 return 0;
12197 }
12198
12199 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12200 callbacks. */
12201
12202 static void
12203 say_where (struct breakpoint *b)
12204 {
12205 struct value_print_options opts;
12206
12207 get_user_print_options (&opts);
12208
12209 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12210 single string. */
12211 if (b->loc == NULL)
12212 {
12213 /* For pending locations, the output differs slightly based
12214 on b->extra_string. If this is non-NULL, it contains either
12215 a condition or dprintf arguments. */
12216 if (b->extra_string == NULL)
12217 {
12218 printf_filtered (_(" (%s) pending."),
12219 event_location_to_string (b->location.get ()));
12220 }
12221 else if (b->type == bp_dprintf)
12222 {
12223 printf_filtered (_(" (%s,%s) pending."),
12224 event_location_to_string (b->location.get ()),
12225 b->extra_string);
12226 }
12227 else
12228 {
12229 printf_filtered (_(" (%s %s) pending."),
12230 event_location_to_string (b->location.get ()),
12231 b->extra_string);
12232 }
12233 }
12234 else
12235 {
12236 if (opts.addressprint || b->loc->symtab == NULL)
12237 printf_filtered (" at %ps",
12238 styled_string (address_style.style (),
12239 paddress (b->loc->gdbarch,
12240 b->loc->address)));
12241 if (b->loc->symtab != NULL)
12242 {
12243 /* If there is a single location, we can print the location
12244 more nicely. */
12245 if (b->loc->next == NULL)
12246 {
12247 const char *filename
12248 = symtab_to_filename_for_display (b->loc->symtab);
12249 printf_filtered (": file %ps, line %d.",
12250 styled_string (file_name_style.style (),
12251 filename),
12252 b->loc->line_number);
12253 }
12254 else
12255 /* This is not ideal, but each location may have a
12256 different file name, and this at least reflects the
12257 real situation somewhat. */
12258 printf_filtered (": %s.",
12259 event_location_to_string (b->location.get ()));
12260 }
12261
12262 if (b->loc->next)
12263 {
12264 struct bp_location *loc = b->loc;
12265 int n = 0;
12266 for (; loc; loc = loc->next)
12267 ++n;
12268 printf_filtered (" (%d locations)", n);
12269 }
12270 }
12271 }
12272
12273 bp_location::~bp_location ()
12274 {
12275 xfree (function_name);
12276 }
12277
12278 /* Destructor for the breakpoint base class. */
12279
12280 breakpoint::~breakpoint ()
12281 {
12282 xfree (this->cond_string);
12283 xfree (this->extra_string);
12284 }
12285
12286 /* See breakpoint.h. */
12287
12288 bp_locations_range breakpoint::locations ()
12289 {
12290 return bp_locations_range (this->loc);
12291 }
12292
12293 static struct bp_location *
12294 base_breakpoint_allocate_location (struct breakpoint *self)
12295 {
12296 return new bp_location (self);
12297 }
12298
12299 static void
12300 base_breakpoint_re_set (struct breakpoint *b)
12301 {
12302 /* Nothing to re-set. */
12303 }
12304
12305 #define internal_error_pure_virtual_called() \
12306 gdb_assert_not_reached ("pure virtual function called")
12307
12308 static int
12309 base_breakpoint_insert_location (struct bp_location *bl)
12310 {
12311 internal_error_pure_virtual_called ();
12312 }
12313
12314 static int
12315 base_breakpoint_remove_location (struct bp_location *bl,
12316 enum remove_bp_reason reason)
12317 {
12318 internal_error_pure_virtual_called ();
12319 }
12320
12321 static int
12322 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12323 const address_space *aspace,
12324 CORE_ADDR bp_addr,
12325 const struct target_waitstatus *ws)
12326 {
12327 internal_error_pure_virtual_called ();
12328 }
12329
12330 static void
12331 base_breakpoint_check_status (bpstat bs)
12332 {
12333 /* Always stop. */
12334 }
12335
12336 /* A "works_in_software_mode" breakpoint_ops method that just internal
12337 errors. */
12338
12339 static int
12340 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12341 {
12342 internal_error_pure_virtual_called ();
12343 }
12344
12345 /* A "resources_needed" breakpoint_ops method that just internal
12346 errors. */
12347
12348 static int
12349 base_breakpoint_resources_needed (const struct bp_location *bl)
12350 {
12351 internal_error_pure_virtual_called ();
12352 }
12353
12354 static enum print_stop_action
12355 base_breakpoint_print_it (bpstat bs)
12356 {
12357 internal_error_pure_virtual_called ();
12358 }
12359
12360 static void
12361 base_breakpoint_print_one_detail (const struct breakpoint *self,
12362 struct ui_out *uiout)
12363 {
12364 /* nothing */
12365 }
12366
12367 static void
12368 base_breakpoint_print_mention (struct breakpoint *b)
12369 {
12370 internal_error_pure_virtual_called ();
12371 }
12372
12373 static void
12374 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12375 {
12376 internal_error_pure_virtual_called ();
12377 }
12378
12379 static void
12380 base_breakpoint_create_sals_from_location
12381 (struct event_location *location,
12382 struct linespec_result *canonical,
12383 enum bptype type_wanted)
12384 {
12385 internal_error_pure_virtual_called ();
12386 }
12387
12388 static void
12389 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12390 struct linespec_result *c,
12391 gdb::unique_xmalloc_ptr<char> cond_string,
12392 gdb::unique_xmalloc_ptr<char> extra_string,
12393 enum bptype type_wanted,
12394 enum bpdisp disposition,
12395 int thread,
12396 int task, int ignore_count,
12397 const struct breakpoint_ops *o,
12398 int from_tty, int enabled,
12399 int internal, unsigned flags)
12400 {
12401 internal_error_pure_virtual_called ();
12402 }
12403
12404 static std::vector<symtab_and_line>
12405 base_breakpoint_decode_location (struct breakpoint *b,
12406 struct event_location *location,
12407 struct program_space *search_pspace)
12408 {
12409 internal_error_pure_virtual_called ();
12410 }
12411
12412 /* The default 'explains_signal' method. */
12413
12414 static int
12415 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12416 {
12417 return 1;
12418 }
12419
12420 /* The default "after_condition_true" method. */
12421
12422 static void
12423 base_breakpoint_after_condition_true (struct bpstats *bs)
12424 {
12425 /* Nothing to do. */
12426 }
12427
12428 struct breakpoint_ops base_breakpoint_ops =
12429 {
12430 base_breakpoint_allocate_location,
12431 base_breakpoint_re_set,
12432 base_breakpoint_insert_location,
12433 base_breakpoint_remove_location,
12434 base_breakpoint_breakpoint_hit,
12435 base_breakpoint_check_status,
12436 base_breakpoint_resources_needed,
12437 base_breakpoint_works_in_software_mode,
12438 base_breakpoint_print_it,
12439 NULL,
12440 base_breakpoint_print_one_detail,
12441 base_breakpoint_print_mention,
12442 base_breakpoint_print_recreate,
12443 base_breakpoint_create_sals_from_location,
12444 base_breakpoint_create_breakpoints_sal,
12445 base_breakpoint_decode_location,
12446 base_breakpoint_explains_signal,
12447 base_breakpoint_after_condition_true,
12448 };
12449
12450 /* Default breakpoint_ops methods. */
12451
12452 static void
12453 bkpt_re_set (struct breakpoint *b)
12454 {
12455 /* FIXME: is this still reachable? */
12456 if (breakpoint_event_location_empty_p (b))
12457 {
12458 /* Anything without a location can't be re-set. */
12459 delete_breakpoint (b);
12460 return;
12461 }
12462
12463 breakpoint_re_set_default (b);
12464 }
12465
12466 static int
12467 bkpt_insert_location (struct bp_location *bl)
12468 {
12469 CORE_ADDR addr = bl->target_info.reqstd_address;
12470
12471 bl->target_info.kind = breakpoint_kind (bl, &addr);
12472 bl->target_info.placed_address = addr;
12473
12474 if (bl->loc_type == bp_loc_hardware_breakpoint)
12475 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12476 else
12477 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12478 }
12479
12480 static int
12481 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12482 {
12483 if (bl->loc_type == bp_loc_hardware_breakpoint)
12484 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12485 else
12486 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12487 }
12488
12489 static int
12490 bkpt_breakpoint_hit (const struct bp_location *bl,
12491 const address_space *aspace, CORE_ADDR bp_addr,
12492 const struct target_waitstatus *ws)
12493 {
12494 if (ws->kind != TARGET_WAITKIND_STOPPED
12495 || ws->value.sig != GDB_SIGNAL_TRAP)
12496 return 0;
12497
12498 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12499 aspace, bp_addr))
12500 return 0;
12501
12502 if (overlay_debugging /* unmapped overlay section */
12503 && section_is_overlay (bl->section)
12504 && !section_is_mapped (bl->section))
12505 return 0;
12506
12507 return 1;
12508 }
12509
12510 static int
12511 dprintf_breakpoint_hit (const struct bp_location *bl,
12512 const address_space *aspace, CORE_ADDR bp_addr,
12513 const struct target_waitstatus *ws)
12514 {
12515 if (dprintf_style == dprintf_style_agent
12516 && target_can_run_breakpoint_commands ())
12517 {
12518 /* An agent-style dprintf never causes a stop. If we see a trap
12519 for this address it must be for a breakpoint that happens to
12520 be set at the same address. */
12521 return 0;
12522 }
12523
12524 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12525 }
12526
12527 static int
12528 bkpt_resources_needed (const struct bp_location *bl)
12529 {
12530 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12531
12532 return 1;
12533 }
12534
12535 static enum print_stop_action
12536 bkpt_print_it (bpstat bs)
12537 {
12538 struct breakpoint *b;
12539 const struct bp_location *bl;
12540 int bp_temp;
12541 struct ui_out *uiout = current_uiout;
12542
12543 gdb_assert (bs->bp_location_at != NULL);
12544
12545 bl = bs->bp_location_at.get ();
12546 b = bs->breakpoint_at;
12547
12548 bp_temp = b->disposition == disp_del;
12549 if (bl->address != bl->requested_address)
12550 breakpoint_adjustment_warning (bl->requested_address,
12551 bl->address,
12552 b->number, 1);
12553 annotate_breakpoint (b->number);
12554 maybe_print_thread_hit_breakpoint (uiout);
12555
12556 if (uiout->is_mi_like_p ())
12557 {
12558 uiout->field_string ("reason",
12559 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12560 uiout->field_string ("disp", bpdisp_text (b->disposition));
12561 }
12562 if (bp_temp)
12563 uiout->message ("Temporary breakpoint %pF, ",
12564 signed_field ("bkptno", b->number));
12565 else
12566 uiout->message ("Breakpoint %pF, ",
12567 signed_field ("bkptno", b->number));
12568
12569 return PRINT_SRC_AND_LOC;
12570 }
12571
12572 static void
12573 bkpt_print_mention (struct breakpoint *b)
12574 {
12575 if (current_uiout->is_mi_like_p ())
12576 return;
12577
12578 switch (b->type)
12579 {
12580 case bp_breakpoint:
12581 case bp_gnu_ifunc_resolver:
12582 if (b->disposition == disp_del)
12583 printf_filtered (_("Temporary breakpoint"));
12584 else
12585 printf_filtered (_("Breakpoint"));
12586 printf_filtered (_(" %d"), b->number);
12587 if (b->type == bp_gnu_ifunc_resolver)
12588 printf_filtered (_(" at gnu-indirect-function resolver"));
12589 break;
12590 case bp_hardware_breakpoint:
12591 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12592 break;
12593 case bp_dprintf:
12594 printf_filtered (_("Dprintf %d"), b->number);
12595 break;
12596 }
12597
12598 say_where (b);
12599 }
12600
12601 static void
12602 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12603 {
12604 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12605 fprintf_unfiltered (fp, "tbreak");
12606 else if (tp->type == bp_breakpoint)
12607 fprintf_unfiltered (fp, "break");
12608 else if (tp->type == bp_hardware_breakpoint
12609 && tp->disposition == disp_del)
12610 fprintf_unfiltered (fp, "thbreak");
12611 else if (tp->type == bp_hardware_breakpoint)
12612 fprintf_unfiltered (fp, "hbreak");
12613 else
12614 internal_error (__FILE__, __LINE__,
12615 _("unhandled breakpoint type %d"), (int) tp->type);
12616
12617 fprintf_unfiltered (fp, " %s",
12618 event_location_to_string (tp->location.get ()));
12619
12620 /* Print out extra_string if this breakpoint is pending. It might
12621 contain, for example, conditions that were set by the user. */
12622 if (tp->loc == NULL && tp->extra_string != NULL)
12623 fprintf_unfiltered (fp, " %s", tp->extra_string);
12624
12625 print_recreate_thread (tp, fp);
12626 }
12627
12628 static void
12629 bkpt_create_sals_from_location (struct event_location *location,
12630 struct linespec_result *canonical,
12631 enum bptype type_wanted)
12632 {
12633 create_sals_from_location_default (location, canonical, type_wanted);
12634 }
12635
12636 static void
12637 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12638 struct linespec_result *canonical,
12639 gdb::unique_xmalloc_ptr<char> cond_string,
12640 gdb::unique_xmalloc_ptr<char> extra_string,
12641 enum bptype type_wanted,
12642 enum bpdisp disposition,
12643 int thread,
12644 int task, int ignore_count,
12645 const struct breakpoint_ops *ops,
12646 int from_tty, int enabled,
12647 int internal, unsigned flags)
12648 {
12649 create_breakpoints_sal_default (gdbarch, canonical,
12650 std::move (cond_string),
12651 std::move (extra_string),
12652 type_wanted,
12653 disposition, thread, task,
12654 ignore_count, ops, from_tty,
12655 enabled, internal, flags);
12656 }
12657
12658 static std::vector<symtab_and_line>
12659 bkpt_decode_location (struct breakpoint *b,
12660 struct event_location *location,
12661 struct program_space *search_pspace)
12662 {
12663 return decode_location_default (b, location, search_pspace);
12664 }
12665
12666 /* Virtual table for internal breakpoints. */
12667
12668 static void
12669 internal_bkpt_re_set (struct breakpoint *b)
12670 {
12671 switch (b->type)
12672 {
12673 /* Delete overlay event and longjmp master breakpoints; they
12674 will be reset later by breakpoint_re_set. */
12675 case bp_overlay_event:
12676 case bp_longjmp_master:
12677 case bp_std_terminate_master:
12678 case bp_exception_master:
12679 delete_breakpoint (b);
12680 break;
12681
12682 /* This breakpoint is special, it's set up when the inferior
12683 starts and we really don't want to touch it. */
12684 case bp_shlib_event:
12685
12686 /* Like bp_shlib_event, this breakpoint type is special. Once
12687 it is set up, we do not want to touch it. */
12688 case bp_thread_event:
12689 break;
12690 }
12691 }
12692
12693 static void
12694 internal_bkpt_check_status (bpstat bs)
12695 {
12696 if (bs->breakpoint_at->type == bp_shlib_event)
12697 {
12698 /* If requested, stop when the dynamic linker notifies GDB of
12699 events. This allows the user to get control and place
12700 breakpoints in initializer routines for dynamically loaded
12701 objects (among other things). */
12702 bs->stop = stop_on_solib_events;
12703 bs->print = stop_on_solib_events;
12704 }
12705 else
12706 bs->stop = 0;
12707 }
12708
12709 static enum print_stop_action
12710 internal_bkpt_print_it (bpstat bs)
12711 {
12712 struct breakpoint *b;
12713
12714 b = bs->breakpoint_at;
12715
12716 switch (b->type)
12717 {
12718 case bp_shlib_event:
12719 /* Did we stop because the user set the stop_on_solib_events
12720 variable? (If so, we report this as a generic, "Stopped due
12721 to shlib event" message.) */
12722 print_solib_event (0);
12723 break;
12724
12725 case bp_thread_event:
12726 /* Not sure how we will get here.
12727 GDB should not stop for these breakpoints. */
12728 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12729 break;
12730
12731 case bp_overlay_event:
12732 /* By analogy with the thread event, GDB should not stop for these. */
12733 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12734 break;
12735
12736 case bp_longjmp_master:
12737 /* These should never be enabled. */
12738 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12739 break;
12740
12741 case bp_std_terminate_master:
12742 /* These should never be enabled. */
12743 printf_filtered (_("std::terminate Master Breakpoint: "
12744 "gdb should not stop!\n"));
12745 break;
12746
12747 case bp_exception_master:
12748 /* These should never be enabled. */
12749 printf_filtered (_("Exception Master Breakpoint: "
12750 "gdb should not stop!\n"));
12751 break;
12752 }
12753
12754 return PRINT_NOTHING;
12755 }
12756
12757 static void
12758 internal_bkpt_print_mention (struct breakpoint *b)
12759 {
12760 /* Nothing to mention. These breakpoints are internal. */
12761 }
12762
12763 /* Virtual table for momentary breakpoints */
12764
12765 static void
12766 momentary_bkpt_re_set (struct breakpoint *b)
12767 {
12768 /* Keep temporary breakpoints, which can be encountered when we step
12769 over a dlopen call and solib_add is resetting the breakpoints.
12770 Otherwise these should have been blown away via the cleanup chain
12771 or by breakpoint_init_inferior when we rerun the executable. */
12772 }
12773
12774 static void
12775 momentary_bkpt_check_status (bpstat bs)
12776 {
12777 /* Nothing. The point of these breakpoints is causing a stop. */
12778 }
12779
12780 static enum print_stop_action
12781 momentary_bkpt_print_it (bpstat bs)
12782 {
12783 return PRINT_UNKNOWN;
12784 }
12785
12786 static void
12787 momentary_bkpt_print_mention (struct breakpoint *b)
12788 {
12789 /* Nothing to mention. These breakpoints are internal. */
12790 }
12791
12792 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12793
12794 It gets cleared already on the removal of the first one of such placed
12795 breakpoints. This is OK as they get all removed altogether. */
12796
12797 longjmp_breakpoint::~longjmp_breakpoint ()
12798 {
12799 thread_info *tp = find_thread_global_id (this->thread);
12800
12801 if (tp != NULL)
12802 tp->initiating_frame = null_frame_id;
12803 }
12804
12805 /* Specific methods for probe breakpoints. */
12806
12807 static int
12808 bkpt_probe_insert_location (struct bp_location *bl)
12809 {
12810 int v = bkpt_insert_location (bl);
12811
12812 if (v == 0)
12813 {
12814 /* The insertion was successful, now let's set the probe's semaphore
12815 if needed. */
12816 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
12817 }
12818
12819 return v;
12820 }
12821
12822 static int
12823 bkpt_probe_remove_location (struct bp_location *bl,
12824 enum remove_bp_reason reason)
12825 {
12826 /* Let's clear the semaphore before removing the location. */
12827 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
12828
12829 return bkpt_remove_location (bl, reason);
12830 }
12831
12832 static void
12833 bkpt_probe_create_sals_from_location (struct event_location *location,
12834 struct linespec_result *canonical,
12835 enum bptype type_wanted)
12836 {
12837 struct linespec_sals lsal;
12838
12839 lsal.sals = parse_probes (location, NULL, canonical);
12840 lsal.canonical
12841 = xstrdup (event_location_to_string (canonical->location.get ()));
12842 canonical->lsals.push_back (std::move (lsal));
12843 }
12844
12845 static std::vector<symtab_and_line>
12846 bkpt_probe_decode_location (struct breakpoint *b,
12847 struct event_location *location,
12848 struct program_space *search_pspace)
12849 {
12850 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12851 if (sals.empty ())
12852 error (_("probe not found"));
12853 return sals;
12854 }
12855
12856 /* The breakpoint_ops structure to be used in tracepoints. */
12857
12858 static void
12859 tracepoint_re_set (struct breakpoint *b)
12860 {
12861 breakpoint_re_set_default (b);
12862 }
12863
12864 static int
12865 tracepoint_breakpoint_hit (const struct bp_location *bl,
12866 const address_space *aspace, CORE_ADDR bp_addr,
12867 const struct target_waitstatus *ws)
12868 {
12869 /* By definition, the inferior does not report stops at
12870 tracepoints. */
12871 return 0;
12872 }
12873
12874 static void
12875 tracepoint_print_one_detail (const struct breakpoint *self,
12876 struct ui_out *uiout)
12877 {
12878 struct tracepoint *tp = (struct tracepoint *) self;
12879 if (!tp->static_trace_marker_id.empty ())
12880 {
12881 gdb_assert (self->type == bp_static_tracepoint);
12882
12883 uiout->message ("\tmarker id is %pF\n",
12884 string_field ("static-tracepoint-marker-string-id",
12885 tp->static_trace_marker_id.c_str ()));
12886 }
12887 }
12888
12889 static void
12890 tracepoint_print_mention (struct breakpoint *b)
12891 {
12892 if (current_uiout->is_mi_like_p ())
12893 return;
12894
12895 switch (b->type)
12896 {
12897 case bp_tracepoint:
12898 printf_filtered (_("Tracepoint"));
12899 printf_filtered (_(" %d"), b->number);
12900 break;
12901 case bp_fast_tracepoint:
12902 printf_filtered (_("Fast tracepoint"));
12903 printf_filtered (_(" %d"), b->number);
12904 break;
12905 case bp_static_tracepoint:
12906 printf_filtered (_("Static tracepoint"));
12907 printf_filtered (_(" %d"), b->number);
12908 break;
12909 default:
12910 internal_error (__FILE__, __LINE__,
12911 _("unhandled tracepoint type %d"), (int) b->type);
12912 }
12913
12914 say_where (b);
12915 }
12916
12917 static void
12918 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
12919 {
12920 struct tracepoint *tp = (struct tracepoint *) self;
12921
12922 if (self->type == bp_fast_tracepoint)
12923 fprintf_unfiltered (fp, "ftrace");
12924 else if (self->type == bp_static_tracepoint)
12925 fprintf_unfiltered (fp, "strace");
12926 else if (self->type == bp_tracepoint)
12927 fprintf_unfiltered (fp, "trace");
12928 else
12929 internal_error (__FILE__, __LINE__,
12930 _("unhandled tracepoint type %d"), (int) self->type);
12931
12932 fprintf_unfiltered (fp, " %s",
12933 event_location_to_string (self->location.get ()));
12934 print_recreate_thread (self, fp);
12935
12936 if (tp->pass_count)
12937 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
12938 }
12939
12940 static void
12941 tracepoint_create_sals_from_location (struct event_location *location,
12942 struct linespec_result *canonical,
12943 enum bptype type_wanted)
12944 {
12945 create_sals_from_location_default (location, canonical, type_wanted);
12946 }
12947
12948 static void
12949 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12950 struct linespec_result *canonical,
12951 gdb::unique_xmalloc_ptr<char> cond_string,
12952 gdb::unique_xmalloc_ptr<char> extra_string,
12953 enum bptype type_wanted,
12954 enum bpdisp disposition,
12955 int thread,
12956 int task, int ignore_count,
12957 const struct breakpoint_ops *ops,
12958 int from_tty, int enabled,
12959 int internal, unsigned flags)
12960 {
12961 create_breakpoints_sal_default (gdbarch, canonical,
12962 std::move (cond_string),
12963 std::move (extra_string),
12964 type_wanted,
12965 disposition, thread, task,
12966 ignore_count, ops, from_tty,
12967 enabled, internal, flags);
12968 }
12969
12970 static std::vector<symtab_and_line>
12971 tracepoint_decode_location (struct breakpoint *b,
12972 struct event_location *location,
12973 struct program_space *search_pspace)
12974 {
12975 return decode_location_default (b, location, search_pspace);
12976 }
12977
12978 struct breakpoint_ops tracepoint_breakpoint_ops;
12979
12980 /* Virtual table for tracepoints on static probes. */
12981
12982 static void
12983 tracepoint_probe_create_sals_from_location
12984 (struct event_location *location,
12985 struct linespec_result *canonical,
12986 enum bptype type_wanted)
12987 {
12988 /* We use the same method for breakpoint on probes. */
12989 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
12990 }
12991
12992 static std::vector<symtab_and_line>
12993 tracepoint_probe_decode_location (struct breakpoint *b,
12994 struct event_location *location,
12995 struct program_space *search_pspace)
12996 {
12997 /* We use the same method for breakpoint on probes. */
12998 return bkpt_probe_decode_location (b, location, search_pspace);
12999 }
13000
13001 /* Dprintf breakpoint_ops methods. */
13002
13003 static void
13004 dprintf_re_set (struct breakpoint *b)
13005 {
13006 breakpoint_re_set_default (b);
13007
13008 /* extra_string should never be non-NULL for dprintf. */
13009 gdb_assert (b->extra_string != NULL);
13010
13011 /* 1 - connect to target 1, that can run breakpoint commands.
13012 2 - create a dprintf, which resolves fine.
13013 3 - disconnect from target 1
13014 4 - connect to target 2, that can NOT run breakpoint commands.
13015
13016 After steps #3/#4, you'll want the dprintf command list to
13017 be updated, because target 1 and 2 may well return different
13018 answers for target_can_run_breakpoint_commands().
13019 Given absence of finer grained resetting, we get to do
13020 it all the time. */
13021 if (b->extra_string != NULL)
13022 update_dprintf_command_list (b);
13023 }
13024
13025 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13026
13027 static void
13028 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13029 {
13030 fprintf_unfiltered (fp, "dprintf %s,%s",
13031 event_location_to_string (tp->location.get ()),
13032 tp->extra_string);
13033 print_recreate_thread (tp, fp);
13034 }
13035
13036 /* Implement the "after_condition_true" breakpoint_ops method for
13037 dprintf.
13038
13039 dprintf's are implemented with regular commands in their command
13040 list, but we run the commands here instead of before presenting the
13041 stop to the user, as dprintf's don't actually cause a stop. This
13042 also makes it so that the commands of multiple dprintfs at the same
13043 address are all handled. */
13044
13045 static void
13046 dprintf_after_condition_true (struct bpstats *bs)
13047 {
13048 struct bpstats tmp_bs;
13049 struct bpstats *tmp_bs_p = &tmp_bs;
13050
13051 /* dprintf's never cause a stop. This wasn't set in the
13052 check_status hook instead because that would make the dprintf's
13053 condition not be evaluated. */
13054 bs->stop = 0;
13055
13056 /* Run the command list here. Take ownership of it instead of
13057 copying. We never want these commands to run later in
13058 bpstat_do_actions, if a breakpoint that causes a stop happens to
13059 be set at same address as this dprintf, or even if running the
13060 commands here throws. */
13061 tmp_bs.commands = bs->commands;
13062 bs->commands = NULL;
13063
13064 bpstat_do_actions_1 (&tmp_bs_p);
13065
13066 /* 'tmp_bs.commands' will usually be NULL by now, but
13067 bpstat_do_actions_1 may return early without processing the whole
13068 list. */
13069 }
13070
13071 /* The breakpoint_ops structure to be used on static tracepoints with
13072 markers (`-m'). */
13073
13074 static void
13075 strace_marker_create_sals_from_location (struct event_location *location,
13076 struct linespec_result *canonical,
13077 enum bptype type_wanted)
13078 {
13079 struct linespec_sals lsal;
13080 const char *arg_start, *arg;
13081
13082 arg = arg_start = get_linespec_location (location)->spec_string;
13083 lsal.sals = decode_static_tracepoint_spec (&arg);
13084
13085 std::string str (arg_start, arg - arg_start);
13086 const char *ptr = str.c_str ();
13087 canonical->location
13088 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
13089
13090 lsal.canonical
13091 = xstrdup (event_location_to_string (canonical->location.get ()));
13092 canonical->lsals.push_back (std::move (lsal));
13093 }
13094
13095 static void
13096 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13097 struct linespec_result *canonical,
13098 gdb::unique_xmalloc_ptr<char> cond_string,
13099 gdb::unique_xmalloc_ptr<char> extra_string,
13100 enum bptype type_wanted,
13101 enum bpdisp disposition,
13102 int thread,
13103 int task, int ignore_count,
13104 const struct breakpoint_ops *ops,
13105 int from_tty, int enabled,
13106 int internal, unsigned flags)
13107 {
13108 const linespec_sals &lsal = canonical->lsals[0];
13109
13110 /* If the user is creating a static tracepoint by marker id
13111 (strace -m MARKER_ID), then store the sals index, so that
13112 breakpoint_re_set can try to match up which of the newly
13113 found markers corresponds to this one, and, don't try to
13114 expand multiple locations for each sal, given than SALS
13115 already should contain all sals for MARKER_ID. */
13116
13117 for (size_t i = 0; i < lsal.sals.size (); i++)
13118 {
13119 event_location_up location
13120 = copy_event_location (canonical->location.get ());
13121
13122 std::unique_ptr<tracepoint> tp (new tracepoint ());
13123 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
13124 std::move (location), NULL,
13125 std::move (cond_string),
13126 std::move (extra_string),
13127 type_wanted, disposition,
13128 thread, task, ignore_count, ops,
13129 from_tty, enabled, internal, flags,
13130 canonical->special_display);
13131 /* Given that its possible to have multiple markers with
13132 the same string id, if the user is creating a static
13133 tracepoint by marker id ("strace -m MARKER_ID"), then
13134 store the sals index, so that breakpoint_re_set can
13135 try to match up which of the newly found markers
13136 corresponds to this one */
13137 tp->static_trace_marker_id_idx = i;
13138
13139 install_breakpoint (internal, std::move (tp), 0);
13140 }
13141 }
13142
13143 static std::vector<symtab_and_line>
13144 strace_marker_decode_location (struct breakpoint *b,
13145 struct event_location *location,
13146 struct program_space *search_pspace)
13147 {
13148 struct tracepoint *tp = (struct tracepoint *) b;
13149 const char *s = get_linespec_location (location)->spec_string;
13150
13151 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13152 if (sals.size () > tp->static_trace_marker_id_idx)
13153 {
13154 sals[0] = sals[tp->static_trace_marker_id_idx];
13155 sals.resize (1);
13156 return sals;
13157 }
13158 else
13159 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
13160 }
13161
13162 static struct breakpoint_ops strace_marker_breakpoint_ops;
13163
13164 static int
13165 strace_marker_p (struct breakpoint *b)
13166 {
13167 return b->ops == &strace_marker_breakpoint_ops;
13168 }
13169
13170 /* Delete a breakpoint and clean up all traces of it in the data
13171 structures. */
13172
13173 void
13174 delete_breakpoint (struct breakpoint *bpt)
13175 {
13176 gdb_assert (bpt != NULL);
13177
13178 /* Has this bp already been deleted? This can happen because
13179 multiple lists can hold pointers to bp's. bpstat lists are
13180 especial culprits.
13181
13182 One example of this happening is a watchpoint's scope bp. When
13183 the scope bp triggers, we notice that the watchpoint is out of
13184 scope, and delete it. We also delete its scope bp. But the
13185 scope bp is marked "auto-deleting", and is already on a bpstat.
13186 That bpstat is then checked for auto-deleting bp's, which are
13187 deleted.
13188
13189 A real solution to this problem might involve reference counts in
13190 bp's, and/or giving them pointers back to their referencing
13191 bpstat's, and teaching delete_breakpoint to only free a bp's
13192 storage when no more references were extent. A cheaper bandaid
13193 was chosen. */
13194 if (bpt->type == bp_none)
13195 return;
13196
13197 /* At least avoid this stale reference until the reference counting
13198 of breakpoints gets resolved. */
13199 if (bpt->related_breakpoint != bpt)
13200 {
13201 struct breakpoint *related;
13202 struct watchpoint *w;
13203
13204 if (bpt->type == bp_watchpoint_scope)
13205 w = (struct watchpoint *) bpt->related_breakpoint;
13206 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13207 w = (struct watchpoint *) bpt;
13208 else
13209 w = NULL;
13210 if (w != NULL)
13211 watchpoint_del_at_next_stop (w);
13212
13213 /* Unlink bpt from the bpt->related_breakpoint ring. */
13214 for (related = bpt; related->related_breakpoint != bpt;
13215 related = related->related_breakpoint);
13216 related->related_breakpoint = bpt->related_breakpoint;
13217 bpt->related_breakpoint = bpt;
13218 }
13219
13220 /* watch_command_1 creates a watchpoint but only sets its number if
13221 update_watchpoint succeeds in creating its bp_locations. If there's
13222 a problem in that process, we'll be asked to delete the half-created
13223 watchpoint. In that case, don't announce the deletion. */
13224 if (bpt->number)
13225 gdb::observers::breakpoint_deleted.notify (bpt);
13226
13227 if (breakpoint_chain == bpt)
13228 breakpoint_chain = bpt->next;
13229
13230 for (breakpoint *b : all_breakpoints ())
13231 if (b->next == bpt)
13232 {
13233 b->next = bpt->next;
13234 break;
13235 }
13236
13237 /* Be sure no bpstat's are pointing at the breakpoint after it's
13238 been freed. */
13239 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13240 in all threads for now. Note that we cannot just remove bpstats
13241 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13242 commands are associated with the bpstat; if we remove it here,
13243 then the later call to bpstat_do_actions (&stop_bpstat); in
13244 event-top.c won't do anything, and temporary breakpoints with
13245 commands won't work. */
13246
13247 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13248
13249 /* Now that breakpoint is removed from breakpoint list, update the
13250 global location list. This will remove locations that used to
13251 belong to this breakpoint. Do this before freeing the breakpoint
13252 itself, since remove_breakpoint looks at location's owner. It
13253 might be better design to have location completely
13254 self-contained, but it's not the case now. */
13255 update_global_location_list (UGLL_DONT_INSERT);
13256
13257 /* On the chance that someone will soon try again to delete this
13258 same bp, we mark it as deleted before freeing its storage. */
13259 bpt->type = bp_none;
13260 delete bpt;
13261 }
13262
13263 /* Iterator function to call a user-provided callback function once
13264 for each of B and its related breakpoints. */
13265
13266 static void
13267 iterate_over_related_breakpoints (struct breakpoint *b,
13268 gdb::function_view<void (breakpoint *)> function)
13269 {
13270 struct breakpoint *related;
13271
13272 related = b;
13273 do
13274 {
13275 struct breakpoint *next;
13276
13277 /* FUNCTION may delete RELATED. */
13278 next = related->related_breakpoint;
13279
13280 if (next == related)
13281 {
13282 /* RELATED is the last ring entry. */
13283 function (related);
13284
13285 /* FUNCTION may have deleted it, so we'd never reach back to
13286 B. There's nothing left to do anyway, so just break
13287 out. */
13288 break;
13289 }
13290 else
13291 function (related);
13292
13293 related = next;
13294 }
13295 while (related != b);
13296 }
13297
13298 static void
13299 delete_command (const char *arg, int from_tty)
13300 {
13301 dont_repeat ();
13302
13303 if (arg == 0)
13304 {
13305 int breaks_to_delete = 0;
13306
13307 /* Delete all breakpoints if no argument. Do not delete
13308 internal breakpoints, these have to be deleted with an
13309 explicit breakpoint number argument. */
13310 for (breakpoint *b : all_breakpoints ())
13311 if (user_breakpoint_p (b))
13312 {
13313 breaks_to_delete = 1;
13314 break;
13315 }
13316
13317 /* Ask user only if there are some breakpoints to delete. */
13318 if (!from_tty
13319 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13320 for (breakpoint *b : all_breakpoints_safe ())
13321 if (user_breakpoint_p (b))
13322 delete_breakpoint (b);
13323 }
13324 else
13325 map_breakpoint_numbers
13326 (arg, [&] (breakpoint *br)
13327 {
13328 iterate_over_related_breakpoints (br, delete_breakpoint);
13329 });
13330 }
13331
13332 /* Return true if all locations of B bound to PSPACE are pending. If
13333 PSPACE is NULL, all locations of all program spaces are
13334 considered. */
13335
13336 static int
13337 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13338 {
13339 for (bp_location *loc : b->locations ())
13340 if ((pspace == NULL
13341 || loc->pspace == pspace)
13342 && !loc->shlib_disabled
13343 && !loc->pspace->executing_startup)
13344 return 0;
13345 return 1;
13346 }
13347
13348 /* Subroutine of update_breakpoint_locations to simplify it.
13349 Return non-zero if multiple fns in list LOC have the same name.
13350 Null names are ignored. */
13351
13352 static int
13353 ambiguous_names_p (struct bp_location *loc)
13354 {
13355 struct bp_location *l;
13356 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
13357 xcalloc, xfree));
13358
13359 for (l = loc; l != NULL; l = l->next)
13360 {
13361 const char **slot;
13362 const char *name = l->function_name;
13363
13364 /* Allow for some names to be NULL, ignore them. */
13365 if (name == NULL)
13366 continue;
13367
13368 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
13369 INSERT);
13370 /* NOTE: We can assume slot != NULL here because xcalloc never
13371 returns NULL. */
13372 if (*slot != NULL)
13373 return 1;
13374 *slot = name;
13375 }
13376
13377 return 0;
13378 }
13379
13380 /* When symbols change, it probably means the sources changed as well,
13381 and it might mean the static tracepoint markers are no longer at
13382 the same address or line numbers they used to be at last we
13383 checked. Losing your static tracepoints whenever you rebuild is
13384 undesirable. This function tries to resync/rematch gdb static
13385 tracepoints with the markers on the target, for static tracepoints
13386 that have not been set by marker id. Static tracepoint that have
13387 been set by marker id are reset by marker id in breakpoint_re_set.
13388 The heuristic is:
13389
13390 1) For a tracepoint set at a specific address, look for a marker at
13391 the old PC. If one is found there, assume to be the same marker.
13392 If the name / string id of the marker found is different from the
13393 previous known name, assume that means the user renamed the marker
13394 in the sources, and output a warning.
13395
13396 2) For a tracepoint set at a given line number, look for a marker
13397 at the new address of the old line number. If one is found there,
13398 assume to be the same marker. If the name / string id of the
13399 marker found is different from the previous known name, assume that
13400 means the user renamed the marker in the sources, and output a
13401 warning.
13402
13403 3) If a marker is no longer found at the same address or line, it
13404 may mean the marker no longer exists. But it may also just mean
13405 the code changed a bit. Maybe the user added a few lines of code
13406 that made the marker move up or down (in line number terms). Ask
13407 the target for info about the marker with the string id as we knew
13408 it. If found, update line number and address in the matching
13409 static tracepoint. This will get confused if there's more than one
13410 marker with the same ID (possible in UST, although unadvised
13411 precisely because it confuses tools). */
13412
13413 static struct symtab_and_line
13414 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13415 {
13416 struct tracepoint *tp = (struct tracepoint *) b;
13417 struct static_tracepoint_marker marker;
13418 CORE_ADDR pc;
13419
13420 pc = sal.pc;
13421 if (sal.line)
13422 find_line_pc (sal.symtab, sal.line, &pc);
13423
13424 if (target_static_tracepoint_marker_at (pc, &marker))
13425 {
13426 if (tp->static_trace_marker_id != marker.str_id)
13427 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13428 b->number, tp->static_trace_marker_id.c_str (),
13429 marker.str_id.c_str ());
13430
13431 tp->static_trace_marker_id = std::move (marker.str_id);
13432
13433 return sal;
13434 }
13435
13436 /* Old marker wasn't found on target at lineno. Try looking it up
13437 by string ID. */
13438 if (!sal.explicit_pc
13439 && sal.line != 0
13440 && sal.symtab != NULL
13441 && !tp->static_trace_marker_id.empty ())
13442 {
13443 std::vector<static_tracepoint_marker> markers
13444 = target_static_tracepoint_markers_by_strid
13445 (tp->static_trace_marker_id.c_str ());
13446
13447 if (!markers.empty ())
13448 {
13449 struct symbol *sym;
13450 struct static_tracepoint_marker *tpmarker;
13451 struct ui_out *uiout = current_uiout;
13452 struct explicit_location explicit_loc;
13453
13454 tpmarker = &markers[0];
13455
13456 tp->static_trace_marker_id = std::move (tpmarker->str_id);
13457
13458 warning (_("marker for static tracepoint %d (%s) not "
13459 "found at previous line number"),
13460 b->number, tp->static_trace_marker_id.c_str ());
13461
13462 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
13463 sym = find_pc_sect_function (tpmarker->address, NULL);
13464 uiout->text ("Now in ");
13465 if (sym)
13466 {
13467 uiout->field_string ("func", sym->print_name (),
13468 function_name_style.style ());
13469 uiout->text (" at ");
13470 }
13471 uiout->field_string ("file",
13472 symtab_to_filename_for_display (sal2.symtab),
13473 file_name_style.style ());
13474 uiout->text (":");
13475
13476 if (uiout->is_mi_like_p ())
13477 {
13478 const char *fullname = symtab_to_fullname (sal2.symtab);
13479
13480 uiout->field_string ("fullname", fullname);
13481 }
13482
13483 uiout->field_signed ("line", sal2.line);
13484 uiout->text ("\n");
13485
13486 b->loc->line_number = sal2.line;
13487 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13488
13489 b->location.reset (NULL);
13490 initialize_explicit_location (&explicit_loc);
13491 explicit_loc.source_filename
13492 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13493 explicit_loc.line_offset.offset = b->loc->line_number;
13494 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13495 b->location = new_explicit_location (&explicit_loc);
13496
13497 /* Might be nice to check if function changed, and warn if
13498 so. */
13499 }
13500 }
13501 return sal;
13502 }
13503
13504 /* Returns 1 iff locations A and B are sufficiently same that
13505 we don't need to report breakpoint as changed. */
13506
13507 static int
13508 locations_are_equal (struct bp_location *a, struct bp_location *b)
13509 {
13510 while (a && b)
13511 {
13512 if (a->address != b->address)
13513 return 0;
13514
13515 if (a->shlib_disabled != b->shlib_disabled)
13516 return 0;
13517
13518 if (a->enabled != b->enabled)
13519 return 0;
13520
13521 if (a->disabled_by_cond != b->disabled_by_cond)
13522 return 0;
13523
13524 a = a->next;
13525 b = b->next;
13526 }
13527
13528 if ((a == NULL) != (b == NULL))
13529 return 0;
13530
13531 return 1;
13532 }
13533
13534 /* Split all locations of B that are bound to PSPACE out of B's
13535 location list to a separate list and return that list's head. If
13536 PSPACE is NULL, hoist out all locations of B. */
13537
13538 static struct bp_location *
13539 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13540 {
13541 struct bp_location head;
13542 struct bp_location *i = b->loc;
13543 struct bp_location **i_link = &b->loc;
13544 struct bp_location *hoisted = &head;
13545
13546 if (pspace == NULL)
13547 {
13548 i = b->loc;
13549 b->loc = NULL;
13550 return i;
13551 }
13552
13553 head.next = NULL;
13554
13555 while (i != NULL)
13556 {
13557 if (i->pspace == pspace)
13558 {
13559 *i_link = i->next;
13560 i->next = NULL;
13561 hoisted->next = i;
13562 hoisted = i;
13563 }
13564 else
13565 i_link = &i->next;
13566 i = *i_link;
13567 }
13568
13569 return head.next;
13570 }
13571
13572 /* Create new breakpoint locations for B (a hardware or software
13573 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13574 zero, then B is a ranged breakpoint. Only recreates locations for
13575 FILTER_PSPACE. Locations of other program spaces are left
13576 untouched. */
13577
13578 void
13579 update_breakpoint_locations (struct breakpoint *b,
13580 struct program_space *filter_pspace,
13581 gdb::array_view<const symtab_and_line> sals,
13582 gdb::array_view<const symtab_and_line> sals_end)
13583 {
13584 struct bp_location *existing_locations;
13585
13586 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
13587 {
13588 /* Ranged breakpoints have only one start location and one end
13589 location. */
13590 b->enable_state = bp_disabled;
13591 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13592 "multiple locations found\n"),
13593 b->number);
13594 return;
13595 }
13596
13597 /* If there's no new locations, and all existing locations are
13598 pending, don't do anything. This optimizes the common case where
13599 all locations are in the same shared library, that was unloaded.
13600 We'd like to retain the location, so that when the library is
13601 loaded again, we don't loose the enabled/disabled status of the
13602 individual locations. */
13603 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
13604 return;
13605
13606 existing_locations = hoist_existing_locations (b, filter_pspace);
13607
13608 for (const auto &sal : sals)
13609 {
13610 struct bp_location *new_loc;
13611
13612 switch_to_program_space_and_thread (sal.pspace);
13613
13614 new_loc = add_location_to_breakpoint (b, &sal);
13615
13616 /* Reparse conditions, they might contain references to the
13617 old symtab. */
13618 if (b->cond_string != NULL)
13619 {
13620 const char *s;
13621
13622 s = b->cond_string;
13623 try
13624 {
13625 new_loc->cond = parse_exp_1 (&s, sal.pc,
13626 block_for_pc (sal.pc),
13627 0);
13628 }
13629 catch (const gdb_exception_error &e)
13630 {
13631 new_loc->disabled_by_cond = true;
13632 }
13633 }
13634
13635 if (!sals_end.empty ())
13636 {
13637 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13638
13639 new_loc->length = end - sals[0].pc + 1;
13640 }
13641 }
13642
13643 /* If possible, carry over 'disable' status from existing
13644 breakpoints. */
13645 {
13646 struct bp_location *e = existing_locations;
13647 /* If there are multiple breakpoints with the same function name,
13648 e.g. for inline functions, comparing function names won't work.
13649 Instead compare pc addresses; this is just a heuristic as things
13650 may have moved, but in practice it gives the correct answer
13651 often enough until a better solution is found. */
13652 int have_ambiguous_names = ambiguous_names_p (b->loc);
13653
13654 for (; e; e = e->next)
13655 {
13656 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
13657 {
13658 if (have_ambiguous_names)
13659 {
13660 for (bp_location *l : b->locations ())
13661 {
13662 /* Ignore software vs hardware location type at
13663 this point, because with "set breakpoint
13664 auto-hw", after a re-set, locations that were
13665 hardware can end up as software, or vice versa.
13666 As mentioned above, this is an heuristic and in
13667 practice should give the correct answer often
13668 enough. */
13669 if (breakpoint_locations_match (e, l, true))
13670 {
13671 l->enabled = e->enabled;
13672 l->disabled_by_cond = e->disabled_by_cond;
13673 break;
13674 }
13675 }
13676 }
13677 else
13678 {
13679 for (bp_location *l : b->locations ())
13680 if (l->function_name
13681 && strcmp (e->function_name, l->function_name) == 0)
13682 {
13683 l->enabled = e->enabled;
13684 l->disabled_by_cond = e->disabled_by_cond;
13685 break;
13686 }
13687 }
13688 }
13689 }
13690 }
13691
13692 if (!locations_are_equal (existing_locations, b->loc))
13693 gdb::observers::breakpoint_modified.notify (b);
13694 }
13695
13696 /* Find the SaL locations corresponding to the given LOCATION.
13697 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13698
13699 static std::vector<symtab_and_line>
13700 location_to_sals (struct breakpoint *b, struct event_location *location,
13701 struct program_space *search_pspace, int *found)
13702 {
13703 struct gdb_exception exception;
13704
13705 gdb_assert (b->ops != NULL);
13706
13707 std::vector<symtab_and_line> sals;
13708
13709 try
13710 {
13711 sals = b->ops->decode_location (b, location, search_pspace);
13712 }
13713 catch (gdb_exception_error &e)
13714 {
13715 int not_found_and_ok = 0;
13716
13717 /* For pending breakpoints, it's expected that parsing will
13718 fail until the right shared library is loaded. User has
13719 already told to create pending breakpoints and don't need
13720 extra messages. If breakpoint is in bp_shlib_disabled
13721 state, then user already saw the message about that
13722 breakpoint being disabled, and don't want to see more
13723 errors. */
13724 if (e.error == NOT_FOUND_ERROR
13725 && (b->condition_not_parsed
13726 || (b->loc != NULL
13727 && search_pspace != NULL
13728 && b->loc->pspace != search_pspace)
13729 || (b->loc && b->loc->shlib_disabled)
13730 || (b->loc && b->loc->pspace->executing_startup)
13731 || b->enable_state == bp_disabled))
13732 not_found_and_ok = 1;
13733
13734 if (!not_found_and_ok)
13735 {
13736 /* We surely don't want to warn about the same breakpoint
13737 10 times. One solution, implemented here, is disable
13738 the breakpoint on error. Another solution would be to
13739 have separate 'warning emitted' flag. Since this
13740 happens only when a binary has changed, I don't know
13741 which approach is better. */
13742 b->enable_state = bp_disabled;
13743 throw;
13744 }
13745
13746 exception = std::move (e);
13747 }
13748
13749 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13750 {
13751 for (auto &sal : sals)
13752 resolve_sal_pc (&sal);
13753 if (b->condition_not_parsed && b->extra_string != NULL)
13754 {
13755 char *cond_string, *extra_string;
13756 int thread, task;
13757
13758 find_condition_and_thread_for_sals (sals, b->extra_string,
13759 &cond_string, &thread,
13760 &task, &extra_string);
13761 gdb_assert (b->cond_string == NULL);
13762 if (cond_string)
13763 b->cond_string = cond_string;
13764 b->thread = thread;
13765 b->task = task;
13766 if (extra_string)
13767 {
13768 xfree (b->extra_string);
13769 b->extra_string = extra_string;
13770 }
13771 b->condition_not_parsed = 0;
13772 }
13773
13774 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13775 sals[0] = update_static_tracepoint (b, sals[0]);
13776
13777 *found = 1;
13778 }
13779 else
13780 *found = 0;
13781
13782 return sals;
13783 }
13784
13785 /* The default re_set method, for typical hardware or software
13786 breakpoints. Reevaluate the breakpoint and recreate its
13787 locations. */
13788
13789 static void
13790 breakpoint_re_set_default (struct breakpoint *b)
13791 {
13792 struct program_space *filter_pspace = current_program_space;
13793 std::vector<symtab_and_line> expanded, expanded_end;
13794
13795 int found;
13796 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13797 filter_pspace, &found);
13798 if (found)
13799 expanded = std::move (sals);
13800
13801 if (b->location_range_end != NULL)
13802 {
13803 std::vector<symtab_and_line> sals_end
13804 = location_to_sals (b, b->location_range_end.get (),
13805 filter_pspace, &found);
13806 if (found)
13807 expanded_end = std::move (sals_end);
13808 }
13809
13810 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
13811 }
13812
13813 /* Default method for creating SALs from an address string. It basically
13814 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13815
13816 static void
13817 create_sals_from_location_default (struct event_location *location,
13818 struct linespec_result *canonical,
13819 enum bptype type_wanted)
13820 {
13821 parse_breakpoint_sals (location, canonical);
13822 }
13823
13824 /* Call create_breakpoints_sal for the given arguments. This is the default
13825 function for the `create_breakpoints_sal' method of
13826 breakpoint_ops. */
13827
13828 static void
13829 create_breakpoints_sal_default (struct gdbarch *gdbarch,
13830 struct linespec_result *canonical,
13831 gdb::unique_xmalloc_ptr<char> cond_string,
13832 gdb::unique_xmalloc_ptr<char> extra_string,
13833 enum bptype type_wanted,
13834 enum bpdisp disposition,
13835 int thread,
13836 int task, int ignore_count,
13837 const struct breakpoint_ops *ops,
13838 int from_tty, int enabled,
13839 int internal, unsigned flags)
13840 {
13841 create_breakpoints_sal (gdbarch, canonical,
13842 std::move (cond_string),
13843 std::move (extra_string),
13844 type_wanted, disposition,
13845 thread, task, ignore_count, ops, from_tty,
13846 enabled, internal, flags);
13847 }
13848
13849 /* Decode the line represented by S by calling decode_line_full. This is the
13850 default function for the `decode_location' method of breakpoint_ops. */
13851
13852 static std::vector<symtab_and_line>
13853 decode_location_default (struct breakpoint *b,
13854 struct event_location *location,
13855 struct program_space *search_pspace)
13856 {
13857 struct linespec_result canonical;
13858
13859 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
13860 NULL, 0, &canonical, multiple_symbols_all,
13861 b->filter.get ());
13862
13863 /* We should get 0 or 1 resulting SALs. */
13864 gdb_assert (canonical.lsals.size () < 2);
13865
13866 if (!canonical.lsals.empty ())
13867 {
13868 const linespec_sals &lsal = canonical.lsals[0];
13869 return std::move (lsal.sals);
13870 }
13871 return {};
13872 }
13873
13874 /* Reset a breakpoint. */
13875
13876 static void
13877 breakpoint_re_set_one (breakpoint *b)
13878 {
13879 input_radix = b->input_radix;
13880 set_language (b->language);
13881
13882 b->ops->re_set (b);
13883 }
13884
13885 /* Re-set breakpoint locations for the current program space.
13886 Locations bound to other program spaces are left untouched. */
13887
13888 void
13889 breakpoint_re_set (void)
13890 {
13891 {
13892 scoped_restore_current_language save_language;
13893 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
13894 scoped_restore_current_pspace_and_thread restore_pspace_thread;
13895
13896 /* breakpoint_re_set_one sets the current_language to the language
13897 of the breakpoint it is resetting (see prepare_re_set_context)
13898 before re-evaluating the breakpoint's location. This change can
13899 unfortunately get undone by accident if the language_mode is set
13900 to auto, and we either switch frames, or more likely in this context,
13901 we select the current frame.
13902
13903 We prevent this by temporarily turning the language_mode to
13904 language_mode_manual. We restore it once all breakpoints
13905 have been reset. */
13906 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13907 language_mode = language_mode_manual;
13908
13909 /* Note: we must not try to insert locations until after all
13910 breakpoints have been re-set. Otherwise, e.g., when re-setting
13911 breakpoint 1, we'd insert the locations of breakpoint 2, which
13912 hadn't been re-set yet, and thus may have stale locations. */
13913
13914 for (breakpoint *b : all_breakpoints_safe ())
13915 {
13916 try
13917 {
13918 breakpoint_re_set_one (b);
13919 }
13920 catch (const gdb_exception &ex)
13921 {
13922 exception_fprintf (gdb_stderr, ex,
13923 "Error in re-setting breakpoint %d: ",
13924 b->number);
13925 }
13926 }
13927
13928 jit_breakpoint_re_set ();
13929 }
13930
13931 create_overlay_event_breakpoint ();
13932 create_longjmp_master_breakpoint ();
13933 create_std_terminate_master_breakpoint ();
13934 create_exception_master_breakpoint ();
13935
13936 /* Now we can insert. */
13937 update_global_location_list (UGLL_MAY_INSERT);
13938 }
13939 \f
13940 /* Reset the thread number of this breakpoint:
13941
13942 - If the breakpoint is for all threads, leave it as-is.
13943 - Else, reset it to the current thread for inferior_ptid. */
13944 void
13945 breakpoint_re_set_thread (struct breakpoint *b)
13946 {
13947 if (b->thread != -1)
13948 {
13949 b->thread = inferior_thread ()->global_num;
13950
13951 /* We're being called after following a fork. The new fork is
13952 selected as current, and unless this was a vfork will have a
13953 different program space from the original thread. Reset that
13954 as well. */
13955 b->loc->pspace = current_program_space;
13956 }
13957 }
13958
13959 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13960 If from_tty is nonzero, it prints a message to that effect,
13961 which ends with a period (no newline). */
13962
13963 void
13964 set_ignore_count (int bptnum, int count, int from_tty)
13965 {
13966 if (count < 0)
13967 count = 0;
13968
13969 for (breakpoint *b : all_breakpoints ())
13970 if (b->number == bptnum)
13971 {
13972 if (is_tracepoint (b))
13973 {
13974 if (from_tty && count != 0)
13975 printf_filtered (_("Ignore count ignored for tracepoint %d."),
13976 bptnum);
13977 return;
13978 }
13979
13980 b->ignore_count = count;
13981 if (from_tty)
13982 {
13983 if (count == 0)
13984 printf_filtered (_("Will stop next time "
13985 "breakpoint %d is reached."),
13986 bptnum);
13987 else if (count == 1)
13988 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
13989 bptnum);
13990 else
13991 printf_filtered (_("Will ignore next %d "
13992 "crossings of breakpoint %d."),
13993 count, bptnum);
13994 }
13995 gdb::observers::breakpoint_modified.notify (b);
13996 return;
13997 }
13998
13999 error (_("No breakpoint number %d."), bptnum);
14000 }
14001
14002 /* Command to set ignore-count of breakpoint N to COUNT. */
14003
14004 static void
14005 ignore_command (const char *args, int from_tty)
14006 {
14007 const char *p = args;
14008 int num;
14009
14010 if (p == 0)
14011 error_no_arg (_("a breakpoint number"));
14012
14013 num = get_number (&p);
14014 if (num == 0)
14015 error (_("bad breakpoint number: '%s'"), args);
14016 if (*p == 0)
14017 error (_("Second argument (specified ignore-count) is missing."));
14018
14019 set_ignore_count (num,
14020 longest_to_int (value_as_long (parse_and_eval (p))),
14021 from_tty);
14022 if (from_tty)
14023 printf_filtered ("\n");
14024 }
14025 \f
14026
14027 /* Call FUNCTION on each of the breakpoints with numbers in the range
14028 defined by BP_NUM_RANGE (an inclusive range). */
14029
14030 static void
14031 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
14032 gdb::function_view<void (breakpoint *)> function)
14033 {
14034 if (bp_num_range.first == 0)
14035 {
14036 warning (_("bad breakpoint number at or near '%d'"),
14037 bp_num_range.first);
14038 }
14039 else
14040 {
14041 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
14042 {
14043 bool match = false;
14044
14045 for (breakpoint *b : all_breakpoints_safe ())
14046 if (b->number == i)
14047 {
14048 match = true;
14049 function (b);
14050 break;
14051 }
14052 if (!match)
14053 printf_unfiltered (_("No breakpoint number %d.\n"), i);
14054 }
14055 }
14056 }
14057
14058 /* Call FUNCTION on each of the breakpoints whose numbers are given in
14059 ARGS. */
14060
14061 static void
14062 map_breakpoint_numbers (const char *args,
14063 gdb::function_view<void (breakpoint *)> function)
14064 {
14065 if (args == NULL || *args == '\0')
14066 error_no_arg (_("one or more breakpoint numbers"));
14067
14068 number_or_range_parser parser (args);
14069
14070 while (!parser.finished ())
14071 {
14072 int num = parser.get_number ();
14073 map_breakpoint_number_range (std::make_pair (num, num), function);
14074 }
14075 }
14076
14077 /* Return the breakpoint location structure corresponding to the
14078 BP_NUM and LOC_NUM values. */
14079
14080 static struct bp_location *
14081 find_location_by_number (int bp_num, int loc_num)
14082 {
14083 breakpoint *b = get_breakpoint (bp_num);
14084
14085 if (!b || b->number != bp_num)
14086 error (_("Bad breakpoint number '%d'"), bp_num);
14087
14088 if (loc_num == 0)
14089 error (_("Bad breakpoint location number '%d'"), loc_num);
14090
14091 int n = 0;
14092 for (bp_location *loc : b->locations ())
14093 if (++n == loc_num)
14094 return loc;
14095
14096 error (_("Bad breakpoint location number '%d'"), loc_num);
14097 }
14098
14099 /* Modes of operation for extract_bp_num. */
14100 enum class extract_bp_kind
14101 {
14102 /* Extracting a breakpoint number. */
14103 bp,
14104
14105 /* Extracting a location number. */
14106 loc,
14107 };
14108
14109 /* Extract a breakpoint or location number (as determined by KIND)
14110 from the string starting at START. TRAILER is a character which
14111 can be found after the number. If you don't want a trailer, use
14112 '\0'. If END_OUT is not NULL, it is set to point after the parsed
14113 string. This always returns a positive integer. */
14114
14115 static int
14116 extract_bp_num (extract_bp_kind kind, const char *start,
14117 int trailer, const char **end_out = NULL)
14118 {
14119 const char *end = start;
14120 int num = get_number_trailer (&end, trailer);
14121 if (num < 0)
14122 error (kind == extract_bp_kind::bp
14123 ? _("Negative breakpoint number '%.*s'")
14124 : _("Negative breakpoint location number '%.*s'"),
14125 int (end - start), start);
14126 if (num == 0)
14127 error (kind == extract_bp_kind::bp
14128 ? _("Bad breakpoint number '%.*s'")
14129 : _("Bad breakpoint location number '%.*s'"),
14130 int (end - start), start);
14131
14132 if (end_out != NULL)
14133 *end_out = end;
14134 return num;
14135 }
14136
14137 /* Extract a breakpoint or location range (as determined by KIND) in
14138 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14139 representing the (inclusive) range. The returned pair's elements
14140 are always positive integers. */
14141
14142 static std::pair<int, int>
14143 extract_bp_or_bp_range (extract_bp_kind kind,
14144 const std::string &arg,
14145 std::string::size_type arg_offset)
14146 {
14147 std::pair<int, int> range;
14148 const char *bp_loc = &arg[arg_offset];
14149 std::string::size_type dash = arg.find ('-', arg_offset);
14150 if (dash != std::string::npos)
14151 {
14152 /* bp_loc is a range (x-z). */
14153 if (arg.length () == dash + 1)
14154 error (kind == extract_bp_kind::bp
14155 ? _("Bad breakpoint number at or near: '%s'")
14156 : _("Bad breakpoint location number at or near: '%s'"),
14157 bp_loc);
14158
14159 const char *end;
14160 const char *start_first = bp_loc;
14161 const char *start_second = &arg[dash + 1];
14162 range.first = extract_bp_num (kind, start_first, '-');
14163 range.second = extract_bp_num (kind, start_second, '\0', &end);
14164
14165 if (range.first > range.second)
14166 error (kind == extract_bp_kind::bp
14167 ? _("Inverted breakpoint range at '%.*s'")
14168 : _("Inverted breakpoint location range at '%.*s'"),
14169 int (end - start_first), start_first);
14170 }
14171 else
14172 {
14173 /* bp_loc is a single value. */
14174 range.first = extract_bp_num (kind, bp_loc, '\0');
14175 range.second = range.first;
14176 }
14177 return range;
14178 }
14179
14180 /* Extract the breakpoint/location range specified by ARG. Returns
14181 the breakpoint range in BP_NUM_RANGE, and the location range in
14182 BP_LOC_RANGE.
14183
14184 ARG may be in any of the following forms:
14185
14186 x where 'x' is a breakpoint number.
14187 x-y where 'x' and 'y' specify a breakpoint numbers range.
14188 x.y where 'x' is a breakpoint number and 'y' a location number.
14189 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14190 location number range.
14191 */
14192
14193 static void
14194 extract_bp_number_and_location (const std::string &arg,
14195 std::pair<int, int> &bp_num_range,
14196 std::pair<int, int> &bp_loc_range)
14197 {
14198 std::string::size_type dot = arg.find ('.');
14199
14200 if (dot != std::string::npos)
14201 {
14202 /* Handle 'x.y' and 'x.y-z' cases. */
14203
14204 if (arg.length () == dot + 1 || dot == 0)
14205 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
14206
14207 bp_num_range.first
14208 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14209 bp_num_range.second = bp_num_range.first;
14210
14211 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14212 arg, dot + 1);
14213 }
14214 else
14215 {
14216 /* Handle x and x-y cases. */
14217
14218 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
14219 bp_loc_range.first = 0;
14220 bp_loc_range.second = 0;
14221 }
14222 }
14223
14224 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14225 specifies whether to enable or disable. */
14226
14227 static void
14228 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14229 {
14230 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14231 if (loc != NULL)
14232 {
14233 if (loc->disabled_by_cond && enable)
14234 error (_("Breakpoint %d's condition is invalid at location %d, "
14235 "cannot enable."), bp_num, loc_num);
14236
14237 if (loc->enabled != enable)
14238 {
14239 loc->enabled = enable;
14240 mark_breakpoint_location_modified (loc);
14241 }
14242 if (target_supports_enable_disable_tracepoint ()
14243 && current_trace_status ()->running && loc->owner
14244 && is_tracepoint (loc->owner))
14245 target_disable_tracepoint (loc);
14246 }
14247 update_global_location_list (UGLL_DONT_INSERT);
14248
14249 gdb::observers::breakpoint_modified.notify (loc->owner);
14250 }
14251
14252 /* Enable or disable a range of breakpoint locations. BP_NUM is the
14253 number of the breakpoint, and BP_LOC_RANGE specifies the
14254 (inclusive) range of location numbers of that breakpoint to
14255 enable/disable. ENABLE specifies whether to enable or disable the
14256 location. */
14257
14258 static void
14259 enable_disable_breakpoint_location_range (int bp_num,
14260 std::pair<int, int> &bp_loc_range,
14261 bool enable)
14262 {
14263 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14264 enable_disable_bp_num_loc (bp_num, i, enable);
14265 }
14266
14267 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14268 If from_tty is nonzero, it prints a message to that effect,
14269 which ends with a period (no newline). */
14270
14271 void
14272 disable_breakpoint (struct breakpoint *bpt)
14273 {
14274 /* Never disable a watchpoint scope breakpoint; we want to
14275 hit them when we leave scope so we can delete both the
14276 watchpoint and its scope breakpoint at that time. */
14277 if (bpt->type == bp_watchpoint_scope)
14278 return;
14279
14280 bpt->enable_state = bp_disabled;
14281
14282 /* Mark breakpoint locations modified. */
14283 mark_breakpoint_modified (bpt);
14284
14285 if (target_supports_enable_disable_tracepoint ()
14286 && current_trace_status ()->running && is_tracepoint (bpt))
14287 {
14288 for (bp_location *location : bpt->locations ())
14289 target_disable_tracepoint (location);
14290 }
14291
14292 update_global_location_list (UGLL_DONT_INSERT);
14293
14294 gdb::observers::breakpoint_modified.notify (bpt);
14295 }
14296
14297 /* Enable or disable the breakpoint(s) or breakpoint location(s)
14298 specified in ARGS. ARGS may be in any of the formats handled by
14299 extract_bp_number_and_location. ENABLE specifies whether to enable
14300 or disable the breakpoints/locations. */
14301
14302 static void
14303 enable_disable_command (const char *args, int from_tty, bool enable)
14304 {
14305 if (args == 0)
14306 {
14307 for (breakpoint *bpt : all_breakpoints ())
14308 if (user_breakpoint_p (bpt))
14309 {
14310 if (enable)
14311 enable_breakpoint (bpt);
14312 else
14313 disable_breakpoint (bpt);
14314 }
14315 }
14316 else
14317 {
14318 std::string num = extract_arg (&args);
14319
14320 while (!num.empty ())
14321 {
14322 std::pair<int, int> bp_num_range, bp_loc_range;
14323
14324 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14325
14326 if (bp_loc_range.first == bp_loc_range.second
14327 && bp_loc_range.first == 0)
14328 {
14329 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14330 map_breakpoint_number_range (bp_num_range,
14331 enable
14332 ? enable_breakpoint
14333 : disable_breakpoint);
14334 }
14335 else
14336 {
14337 /* Handle breakpoint ids with formats 'x.y' or
14338 'x.y-z'. */
14339 enable_disable_breakpoint_location_range
14340 (bp_num_range.first, bp_loc_range, enable);
14341 }
14342 num = extract_arg (&args);
14343 }
14344 }
14345 }
14346
14347 /* The disable command disables the specified breakpoints/locations
14348 (or all defined breakpoints) so they're no longer effective in
14349 stopping the inferior. ARGS may be in any of the forms defined in
14350 extract_bp_number_and_location. */
14351
14352 static void
14353 disable_command (const char *args, int from_tty)
14354 {
14355 enable_disable_command (args, from_tty, false);
14356 }
14357
14358 static void
14359 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14360 int count)
14361 {
14362 int target_resources_ok;
14363
14364 if (bpt->type == bp_hardware_breakpoint)
14365 {
14366 int i;
14367 i = hw_breakpoint_used_count ();
14368 target_resources_ok =
14369 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14370 i + 1, 0);
14371 if (target_resources_ok == 0)
14372 error (_("No hardware breakpoint support in the target."));
14373 else if (target_resources_ok < 0)
14374 error (_("Hardware breakpoints used exceeds limit."));
14375 }
14376
14377 if (is_watchpoint (bpt))
14378 {
14379 /* Initialize it just to avoid a GCC false warning. */
14380 enum enable_state orig_enable_state = bp_disabled;
14381
14382 try
14383 {
14384 struct watchpoint *w = (struct watchpoint *) bpt;
14385
14386 orig_enable_state = bpt->enable_state;
14387 bpt->enable_state = bp_enabled;
14388 update_watchpoint (w, 1 /* reparse */);
14389 }
14390 catch (const gdb_exception &e)
14391 {
14392 bpt->enable_state = orig_enable_state;
14393 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14394 bpt->number);
14395 return;
14396 }
14397 }
14398
14399 bpt->enable_state = bp_enabled;
14400
14401 /* Mark breakpoint locations modified. */
14402 mark_breakpoint_modified (bpt);
14403
14404 if (target_supports_enable_disable_tracepoint ()
14405 && current_trace_status ()->running && is_tracepoint (bpt))
14406 {
14407 for (bp_location *location : bpt->locations ())
14408 target_enable_tracepoint (location);
14409 }
14410
14411 bpt->disposition = disposition;
14412 bpt->enable_count = count;
14413 update_global_location_list (UGLL_MAY_INSERT);
14414
14415 gdb::observers::breakpoint_modified.notify (bpt);
14416 }
14417
14418
14419 void
14420 enable_breakpoint (struct breakpoint *bpt)
14421 {
14422 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14423 }
14424
14425 /* The enable command enables the specified breakpoints/locations (or
14426 all defined breakpoints) so they once again become (or continue to
14427 be) effective in stopping the inferior. ARGS may be in any of the
14428 forms defined in extract_bp_number_and_location. */
14429
14430 static void
14431 enable_command (const char *args, int from_tty)
14432 {
14433 enable_disable_command (args, from_tty, true);
14434 }
14435
14436 static void
14437 enable_once_command (const char *args, int from_tty)
14438 {
14439 map_breakpoint_numbers
14440 (args, [&] (breakpoint *b)
14441 {
14442 iterate_over_related_breakpoints
14443 (b, [&] (breakpoint *bpt)
14444 {
14445 enable_breakpoint_disp (bpt, disp_disable, 1);
14446 });
14447 });
14448 }
14449
14450 static void
14451 enable_count_command (const char *args, int from_tty)
14452 {
14453 int count;
14454
14455 if (args == NULL)
14456 error_no_arg (_("hit count"));
14457
14458 count = get_number (&args);
14459
14460 map_breakpoint_numbers
14461 (args, [&] (breakpoint *b)
14462 {
14463 iterate_over_related_breakpoints
14464 (b, [&] (breakpoint *bpt)
14465 {
14466 enable_breakpoint_disp (bpt, disp_disable, count);
14467 });
14468 });
14469 }
14470
14471 static void
14472 enable_delete_command (const char *args, int from_tty)
14473 {
14474 map_breakpoint_numbers
14475 (args, [&] (breakpoint *b)
14476 {
14477 iterate_over_related_breakpoints
14478 (b, [&] (breakpoint *bpt)
14479 {
14480 enable_breakpoint_disp (bpt, disp_del, 1);
14481 });
14482 });
14483 }
14484 \f
14485 /* Invalidate last known value of any hardware watchpoint if
14486 the memory which that value represents has been written to by
14487 GDB itself. */
14488
14489 static void
14490 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14491 CORE_ADDR addr, ssize_t len,
14492 const bfd_byte *data)
14493 {
14494 for (breakpoint *bp : all_breakpoints ())
14495 if (bp->enable_state == bp_enabled
14496 && bp->type == bp_hardware_watchpoint)
14497 {
14498 struct watchpoint *wp = (struct watchpoint *) bp;
14499
14500 if (wp->val_valid && wp->val != nullptr)
14501 {
14502 for (bp_location *loc : bp->locations ())
14503 if (loc->loc_type == bp_loc_hardware_watchpoint
14504 && loc->address + loc->length > addr
14505 && addr + len > loc->address)
14506 {
14507 wp->val = NULL;
14508 wp->val_valid = false;
14509 }
14510 }
14511 }
14512 }
14513
14514 /* Create and insert a breakpoint for software single step. */
14515
14516 void
14517 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14518 const address_space *aspace,
14519 CORE_ADDR next_pc)
14520 {
14521 struct thread_info *tp = inferior_thread ();
14522 struct symtab_and_line sal;
14523 CORE_ADDR pc = next_pc;
14524
14525 if (tp->control.single_step_breakpoints == NULL)
14526 {
14527 tp->control.single_step_breakpoints
14528 = new_single_step_breakpoint (tp->global_num, gdbarch);
14529 }
14530
14531 sal = find_pc_line (pc, 0);
14532 sal.pc = pc;
14533 sal.section = find_pc_overlay (pc);
14534 sal.explicit_pc = 1;
14535 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14536
14537 update_global_location_list (UGLL_INSERT);
14538 }
14539
14540 /* Insert single step breakpoints according to the current state. */
14541
14542 int
14543 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14544 {
14545 struct regcache *regcache = get_current_regcache ();
14546 std::vector<CORE_ADDR> next_pcs;
14547
14548 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14549
14550 if (!next_pcs.empty ())
14551 {
14552 struct frame_info *frame = get_current_frame ();
14553 const address_space *aspace = get_frame_address_space (frame);
14554
14555 for (CORE_ADDR pc : next_pcs)
14556 insert_single_step_breakpoint (gdbarch, aspace, pc);
14557
14558 return 1;
14559 }
14560 else
14561 return 0;
14562 }
14563
14564 /* See breakpoint.h. */
14565
14566 int
14567 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14568 const address_space *aspace,
14569 CORE_ADDR pc)
14570 {
14571 for (bp_location *loc : bp->locations ())
14572 if (loc->inserted
14573 && breakpoint_location_address_match (loc, aspace, pc))
14574 return 1;
14575
14576 return 0;
14577 }
14578
14579 /* Check whether a software single-step breakpoint is inserted at
14580 PC. */
14581
14582 int
14583 single_step_breakpoint_inserted_here_p (const address_space *aspace,
14584 CORE_ADDR pc)
14585 {
14586 for (breakpoint *bpt : all_breakpoints ())
14587 {
14588 if (bpt->type == bp_single_step
14589 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14590 return 1;
14591 }
14592 return 0;
14593 }
14594
14595 /* Tracepoint-specific operations. */
14596
14597 /* Set tracepoint count to NUM. */
14598 static void
14599 set_tracepoint_count (int num)
14600 {
14601 tracepoint_count = num;
14602 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14603 }
14604
14605 static void
14606 trace_command (const char *arg, int from_tty)
14607 {
14608 event_location_up location = string_to_event_location (&arg,
14609 current_language);
14610 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
14611 (location.get (), true /* is_tracepoint */);
14612
14613 create_breakpoint (get_current_arch (),
14614 location.get (),
14615 NULL, 0, arg, false, 1 /* parse arg */,
14616 0 /* tempflag */,
14617 bp_tracepoint /* type_wanted */,
14618 0 /* Ignore count */,
14619 pending_break_support,
14620 ops,
14621 from_tty,
14622 1 /* enabled */,
14623 0 /* internal */, 0);
14624 }
14625
14626 static void
14627 ftrace_command (const char *arg, int from_tty)
14628 {
14629 event_location_up location = string_to_event_location (&arg,
14630 current_language);
14631 create_breakpoint (get_current_arch (),
14632 location.get (),
14633 NULL, 0, arg, false, 1 /* parse arg */,
14634 0 /* tempflag */,
14635 bp_fast_tracepoint /* type_wanted */,
14636 0 /* Ignore count */,
14637 pending_break_support,
14638 &tracepoint_breakpoint_ops,
14639 from_tty,
14640 1 /* enabled */,
14641 0 /* internal */, 0);
14642 }
14643
14644 /* strace command implementation. Creates a static tracepoint. */
14645
14646 static void
14647 strace_command (const char *arg, int from_tty)
14648 {
14649 struct breakpoint_ops *ops;
14650 event_location_up location;
14651
14652 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14653 or with a normal static tracepoint. */
14654 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14655 {
14656 ops = &strace_marker_breakpoint_ops;
14657 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
14658 }
14659 else
14660 {
14661 ops = &tracepoint_breakpoint_ops;
14662 location = string_to_event_location (&arg, current_language);
14663 }
14664
14665 create_breakpoint (get_current_arch (),
14666 location.get (),
14667 NULL, 0, arg, false, 1 /* parse arg */,
14668 0 /* tempflag */,
14669 bp_static_tracepoint /* type_wanted */,
14670 0 /* Ignore count */,
14671 pending_break_support,
14672 ops,
14673 from_tty,
14674 1 /* enabled */,
14675 0 /* internal */, 0);
14676 }
14677
14678 /* Set up a fake reader function that gets command lines from a linked
14679 list that was acquired during tracepoint uploading. */
14680
14681 static struct uploaded_tp *this_utp;
14682 static int next_cmd;
14683
14684 static char *
14685 read_uploaded_action (void)
14686 {
14687 char *rslt = nullptr;
14688
14689 if (next_cmd < this_utp->cmd_strings.size ())
14690 {
14691 rslt = this_utp->cmd_strings[next_cmd].get ();
14692 next_cmd++;
14693 }
14694
14695 return rslt;
14696 }
14697
14698 /* Given information about a tracepoint as recorded on a target (which
14699 can be either a live system or a trace file), attempt to create an
14700 equivalent GDB tracepoint. This is not a reliable process, since
14701 the target does not necessarily have all the information used when
14702 the tracepoint was originally defined. */
14703
14704 struct tracepoint *
14705 create_tracepoint_from_upload (struct uploaded_tp *utp)
14706 {
14707 const char *addr_str;
14708 char small_buf[100];
14709 struct tracepoint *tp;
14710
14711 if (utp->at_string)
14712 addr_str = utp->at_string.get ();
14713 else
14714 {
14715 /* In the absence of a source location, fall back to raw
14716 address. Since there is no way to confirm that the address
14717 means the same thing as when the trace was started, warn the
14718 user. */
14719 warning (_("Uploaded tracepoint %d has no "
14720 "source location, using raw address"),
14721 utp->number);
14722 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14723 addr_str = small_buf;
14724 }
14725
14726 /* There's not much we can do with a sequence of bytecodes. */
14727 if (utp->cond && !utp->cond_string)
14728 warning (_("Uploaded tracepoint %d condition "
14729 "has no source form, ignoring it"),
14730 utp->number);
14731
14732 event_location_up location = string_to_event_location (&addr_str,
14733 current_language);
14734 if (!create_breakpoint (get_current_arch (),
14735 location.get (),
14736 utp->cond_string.get (), -1, addr_str,
14737 false /* force_condition */,
14738 0 /* parse cond/thread */,
14739 0 /* tempflag */,
14740 utp->type /* type_wanted */,
14741 0 /* Ignore count */,
14742 pending_break_support,
14743 &tracepoint_breakpoint_ops,
14744 0 /* from_tty */,
14745 utp->enabled /* enabled */,
14746 0 /* internal */,
14747 CREATE_BREAKPOINT_FLAGS_INSERTED))
14748 return NULL;
14749
14750 /* Get the tracepoint we just created. */
14751 tp = get_tracepoint (tracepoint_count);
14752 gdb_assert (tp != NULL);
14753
14754 if (utp->pass > 0)
14755 {
14756 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14757 tp->number);
14758
14759 trace_pass_command (small_buf, 0);
14760 }
14761
14762 /* If we have uploaded versions of the original commands, set up a
14763 special-purpose "reader" function and call the usual command line
14764 reader, then pass the result to the breakpoint command-setting
14765 function. */
14766 if (!utp->cmd_strings.empty ())
14767 {
14768 counted_command_line cmd_list;
14769
14770 this_utp = utp;
14771 next_cmd = 0;
14772
14773 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
14774
14775 breakpoint_set_commands (tp, std::move (cmd_list));
14776 }
14777 else if (!utp->actions.empty ()
14778 || !utp->step_actions.empty ())
14779 warning (_("Uploaded tracepoint %d actions "
14780 "have no source form, ignoring them"),
14781 utp->number);
14782
14783 /* Copy any status information that might be available. */
14784 tp->hit_count = utp->hit_count;
14785 tp->traceframe_usage = utp->traceframe_usage;
14786
14787 return tp;
14788 }
14789
14790 /* Print information on tracepoint number TPNUM_EXP, or all if
14791 omitted. */
14792
14793 static void
14794 info_tracepoints_command (const char *args, int from_tty)
14795 {
14796 struct ui_out *uiout = current_uiout;
14797 int num_printed;
14798
14799 num_printed = breakpoint_1 (args, false, is_tracepoint);
14800
14801 if (num_printed == 0)
14802 {
14803 if (args == NULL || *args == '\0')
14804 uiout->message ("No tracepoints.\n");
14805 else
14806 uiout->message ("No tracepoint matching '%s'.\n", args);
14807 }
14808
14809 default_collect_info ();
14810 }
14811
14812 /* The 'enable trace' command enables tracepoints.
14813 Not supported by all targets. */
14814 static void
14815 enable_trace_command (const char *args, int from_tty)
14816 {
14817 enable_command (args, from_tty);
14818 }
14819
14820 /* The 'disable trace' command disables tracepoints.
14821 Not supported by all targets. */
14822 static void
14823 disable_trace_command (const char *args, int from_tty)
14824 {
14825 disable_command (args, from_tty);
14826 }
14827
14828 /* Remove a tracepoint (or all if no argument). */
14829 static void
14830 delete_trace_command (const char *arg, int from_tty)
14831 {
14832 dont_repeat ();
14833
14834 if (arg == 0)
14835 {
14836 int breaks_to_delete = 0;
14837
14838 /* Delete all breakpoints if no argument.
14839 Do not delete internal or call-dummy breakpoints, these
14840 have to be deleted with an explicit breakpoint number
14841 argument. */
14842 for (breakpoint *tp : all_tracepoints ())
14843 if (is_tracepoint (tp) && user_breakpoint_p (tp))
14844 {
14845 breaks_to_delete = 1;
14846 break;
14847 }
14848
14849 /* Ask user only if there are some breakpoints to delete. */
14850 if (!from_tty
14851 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14852 {
14853 for (breakpoint *b : all_breakpoints_safe ())
14854 if (is_tracepoint (b) && user_breakpoint_p (b))
14855 delete_breakpoint (b);
14856 }
14857 }
14858 else
14859 map_breakpoint_numbers
14860 (arg, [&] (breakpoint *br)
14861 {
14862 iterate_over_related_breakpoints (br, delete_breakpoint);
14863 });
14864 }
14865
14866 /* Helper function for trace_pass_command. */
14867
14868 static void
14869 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14870 {
14871 tp->pass_count = count;
14872 gdb::observers::breakpoint_modified.notify (tp);
14873 if (from_tty)
14874 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
14875 tp->number, count);
14876 }
14877
14878 /* Set passcount for tracepoint.
14879
14880 First command argument is passcount, second is tracepoint number.
14881 If tracepoint number omitted, apply to most recently defined.
14882 Also accepts special argument "all". */
14883
14884 static void
14885 trace_pass_command (const char *args, int from_tty)
14886 {
14887 struct tracepoint *t1;
14888 ULONGEST count;
14889
14890 if (args == 0 || *args == 0)
14891 error (_("passcount command requires an "
14892 "argument (count + optional TP num)"));
14893
14894 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
14895
14896 args = skip_spaces (args);
14897 if (*args && strncasecmp (args, "all", 3) == 0)
14898 {
14899 args += 3; /* Skip special argument "all". */
14900 if (*args)
14901 error (_("Junk at end of arguments."));
14902
14903 for (breakpoint *b : all_tracepoints ())
14904 {
14905 t1 = (struct tracepoint *) b;
14906 trace_pass_set_count (t1, count, from_tty);
14907 }
14908 }
14909 else if (*args == '\0')
14910 {
14911 t1 = get_tracepoint_by_number (&args, NULL);
14912 if (t1)
14913 trace_pass_set_count (t1, count, from_tty);
14914 }
14915 else
14916 {
14917 number_or_range_parser parser (args);
14918 while (!parser.finished ())
14919 {
14920 t1 = get_tracepoint_by_number (&args, &parser);
14921 if (t1)
14922 trace_pass_set_count (t1, count, from_tty);
14923 }
14924 }
14925 }
14926
14927 struct tracepoint *
14928 get_tracepoint (int num)
14929 {
14930 for (breakpoint *t : all_tracepoints ())
14931 if (t->number == num)
14932 return (struct tracepoint *) t;
14933
14934 return NULL;
14935 }
14936
14937 /* Find the tracepoint with the given target-side number (which may be
14938 different from the tracepoint number after disconnecting and
14939 reconnecting). */
14940
14941 struct tracepoint *
14942 get_tracepoint_by_number_on_target (int num)
14943 {
14944 for (breakpoint *b : all_tracepoints ())
14945 {
14946 struct tracepoint *t = (struct tracepoint *) b;
14947
14948 if (t->number_on_target == num)
14949 return t;
14950 }
14951
14952 return NULL;
14953 }
14954
14955 /* Utility: parse a tracepoint number and look it up in the list.
14956 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
14957 If the argument is missing, the most recent tracepoint
14958 (tracepoint_count) is returned. */
14959
14960 struct tracepoint *
14961 get_tracepoint_by_number (const char **arg,
14962 number_or_range_parser *parser)
14963 {
14964 int tpnum;
14965 const char *instring = arg == NULL ? NULL : *arg;
14966
14967 if (parser != NULL)
14968 {
14969 gdb_assert (!parser->finished ());
14970 tpnum = parser->get_number ();
14971 }
14972 else if (arg == NULL || *arg == NULL || ! **arg)
14973 tpnum = tracepoint_count;
14974 else
14975 tpnum = get_number (arg);
14976
14977 if (tpnum <= 0)
14978 {
14979 if (instring && *instring)
14980 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
14981 instring);
14982 else
14983 printf_filtered (_("No previous tracepoint\n"));
14984 return NULL;
14985 }
14986
14987 for (breakpoint *t : all_tracepoints ())
14988 if (t->number == tpnum)
14989 {
14990 return (struct tracepoint *) t;
14991 }
14992
14993 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
14994 return NULL;
14995 }
14996
14997 void
14998 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
14999 {
15000 if (b->thread != -1)
15001 fprintf_unfiltered (fp, " thread %d", b->thread);
15002
15003 if (b->task != 0)
15004 fprintf_unfiltered (fp, " task %d", b->task);
15005
15006 fprintf_unfiltered (fp, "\n");
15007 }
15008
15009 /* Save information on user settable breakpoints (watchpoints, etc) to
15010 a new script file named FILENAME. If FILTER is non-NULL, call it
15011 on each breakpoint and only include the ones for which it returns
15012 true. */
15013
15014 static void
15015 save_breakpoints (const char *filename, int from_tty,
15016 bool (*filter) (const struct breakpoint *))
15017 {
15018 int any = 0;
15019 int extra_trace_bits = 0;
15020
15021 if (filename == 0 || *filename == 0)
15022 error (_("Argument required (file name in which to save)"));
15023
15024 /* See if we have anything to save. */
15025 for (breakpoint *tp : all_breakpoints ())
15026 {
15027 /* Skip internal and momentary breakpoints. */
15028 if (!user_breakpoint_p (tp))
15029 continue;
15030
15031 /* If we have a filter, only save the breakpoints it accepts. */
15032 if (filter && !filter (tp))
15033 continue;
15034
15035 any = 1;
15036
15037 if (is_tracepoint (tp))
15038 {
15039 extra_trace_bits = 1;
15040
15041 /* We can stop searching. */
15042 break;
15043 }
15044 }
15045
15046 if (!any)
15047 {
15048 warning (_("Nothing to save."));
15049 return;
15050 }
15051
15052 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
15053
15054 stdio_file fp;
15055
15056 if (!fp.open (expanded_filename.get (), "w"))
15057 error (_("Unable to open file '%s' for saving (%s)"),
15058 expanded_filename.get (), safe_strerror (errno));
15059
15060 if (extra_trace_bits)
15061 save_trace_state_variables (&fp);
15062
15063 for (breakpoint *tp : all_breakpoints ())
15064 {
15065 /* Skip internal and momentary breakpoints. */
15066 if (!user_breakpoint_p (tp))
15067 continue;
15068
15069 /* If we have a filter, only save the breakpoints it accepts. */
15070 if (filter && !filter (tp))
15071 continue;
15072
15073 tp->ops->print_recreate (tp, &fp);
15074
15075 /* Note, we can't rely on tp->number for anything, as we can't
15076 assume the recreated breakpoint numbers will match. Use $bpnum
15077 instead. */
15078
15079 if (tp->cond_string)
15080 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15081
15082 if (tp->ignore_count)
15083 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15084
15085 if (tp->type != bp_dprintf && tp->commands)
15086 {
15087 fp.puts (" commands\n");
15088
15089 current_uiout->redirect (&fp);
15090 try
15091 {
15092 print_command_lines (current_uiout, tp->commands.get (), 2);
15093 }
15094 catch (const gdb_exception &ex)
15095 {
15096 current_uiout->redirect (NULL);
15097 throw;
15098 }
15099
15100 current_uiout->redirect (NULL);
15101 fp.puts (" end\n");
15102 }
15103
15104 if (tp->enable_state == bp_disabled)
15105 fp.puts ("disable $bpnum\n");
15106
15107 /* If this is a multi-location breakpoint, check if the locations
15108 should be individually disabled. Watchpoint locations are
15109 special, and not user visible. */
15110 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15111 {
15112 int n = 1;
15113
15114 for (bp_location *loc : tp->locations ())
15115 {
15116 if (!loc->enabled)
15117 fp.printf ("disable $bpnum.%d\n", n);
15118
15119 n++;
15120 }
15121 }
15122 }
15123
15124 if (extra_trace_bits && *default_collect)
15125 fp.printf ("set default-collect %s\n", default_collect);
15126
15127 if (from_tty)
15128 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
15129 }
15130
15131 /* The `save breakpoints' command. */
15132
15133 static void
15134 save_breakpoints_command (const char *args, int from_tty)
15135 {
15136 save_breakpoints (args, from_tty, NULL);
15137 }
15138
15139 /* The `save tracepoints' command. */
15140
15141 static void
15142 save_tracepoints_command (const char *args, int from_tty)
15143 {
15144 save_breakpoints (args, from_tty, is_tracepoint);
15145 }
15146
15147 \f
15148 /* This help string is used to consolidate all the help string for specifying
15149 locations used by several commands. */
15150
15151 #define LOCATION_HELP_STRING \
15152 "Linespecs are colon-separated lists of location parameters, such as\n\
15153 source filename, function name, label name, and line number.\n\
15154 Example: To specify the start of a label named \"the_top\" in the\n\
15155 function \"fact\" in the file \"factorial.c\", use\n\
15156 \"factorial.c:fact:the_top\".\n\
15157 \n\
15158 Address locations begin with \"*\" and specify an exact address in the\n\
15159 program. Example: To specify the fourth byte past the start function\n\
15160 \"main\", use \"*main + 4\".\n\
15161 \n\
15162 Explicit locations are similar to linespecs but use an option/argument\n\
15163 syntax to specify location parameters.\n\
15164 Example: To specify the start of the label named \"the_top\" in the\n\
15165 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15166 -function fact -label the_top\".\n\
15167 \n\
15168 By default, a specified function is matched against the program's\n\
15169 functions in all scopes. For C++, this means in all namespaces and\n\
15170 classes. For Ada, this means in all packages. E.g., in C++,\n\
15171 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15172 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
15173 specified name as a complete fully-qualified name instead."
15174
15175 /* This help string is used for the break, hbreak, tbreak and thbreak
15176 commands. It is defined as a macro to prevent duplication.
15177 COMMAND should be a string constant containing the name of the
15178 command. */
15179
15180 #define BREAK_ARGS_HELP(command) \
15181 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
15182 \t[-force-condition] [if CONDITION]\n\
15183 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15184 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15185 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15186 `-probe-dtrace' (for a DTrace probe).\n\
15187 LOCATION may be a linespec, address, or explicit location as described\n\
15188 below.\n\
15189 \n\
15190 With no LOCATION, uses current execution address of the selected\n\
15191 stack frame. This is useful for breaking on return to a stack frame.\n\
15192 \n\
15193 THREADNUM is the number from \"info threads\".\n\
15194 CONDITION is a boolean expression.\n\
15195 \n\
15196 With the \"-force-condition\" flag, the condition is defined even when\n\
15197 it is invalid for all current locations.\n\
15198 \n" LOCATION_HELP_STRING "\n\n\
15199 Multiple breakpoints at one place are permitted, and useful if their\n\
15200 conditions are different.\n\
15201 \n\
15202 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15203
15204 /* List of subcommands for "catch". */
15205 static struct cmd_list_element *catch_cmdlist;
15206
15207 /* List of subcommands for "tcatch". */
15208 static struct cmd_list_element *tcatch_cmdlist;
15209
15210 void
15211 add_catch_command (const char *name, const char *docstring,
15212 cmd_const_sfunc_ftype *sfunc,
15213 completer_ftype *completer,
15214 void *user_data_catch,
15215 void *user_data_tcatch)
15216 {
15217 struct cmd_list_element *command;
15218
15219 command = add_cmd (name, class_breakpoint, docstring,
15220 &catch_cmdlist);
15221 set_cmd_sfunc (command, sfunc);
15222 set_cmd_context (command, user_data_catch);
15223 set_cmd_completer (command, completer);
15224
15225 command = add_cmd (name, class_breakpoint, docstring,
15226 &tcatch_cmdlist);
15227 set_cmd_sfunc (command, sfunc);
15228 set_cmd_context (command, user_data_tcatch);
15229 set_cmd_completer (command, completer);
15230 }
15231
15232 struct breakpoint *
15233 iterate_over_breakpoints (gdb::function_view<bool (breakpoint *)> callback)
15234 {
15235 for (breakpoint *b : all_breakpoints_safe ())
15236 if (callback (b))
15237 return b;
15238
15239 return NULL;
15240 }
15241
15242 /* Zero if any of the breakpoint's locations could be a location where
15243 functions have been inlined, nonzero otherwise. */
15244
15245 static int
15246 is_non_inline_function (struct breakpoint *b)
15247 {
15248 /* The shared library event breakpoint is set on the address of a
15249 non-inline function. */
15250 if (b->type == bp_shlib_event)
15251 return 1;
15252
15253 return 0;
15254 }
15255
15256 /* Nonzero if the specified PC cannot be a location where functions
15257 have been inlined. */
15258
15259 int
15260 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
15261 const struct target_waitstatus *ws)
15262 {
15263 for (breakpoint *b : all_breakpoints ())
15264 {
15265 if (!is_non_inline_function (b))
15266 continue;
15267
15268 for (bp_location *bl : b->locations ())
15269 {
15270 if (!bl->shlib_disabled
15271 && bpstat_check_location (bl, aspace, pc, ws))
15272 return 1;
15273 }
15274 }
15275
15276 return 0;
15277 }
15278
15279 /* Remove any references to OBJFILE which is going to be freed. */
15280
15281 void
15282 breakpoint_free_objfile (struct objfile *objfile)
15283 {
15284 for (bp_location *loc : all_bp_locations ())
15285 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15286 loc->symtab = NULL;
15287 }
15288
15289 void
15290 initialize_breakpoint_ops (void)
15291 {
15292 static int initialized = 0;
15293
15294 struct breakpoint_ops *ops;
15295
15296 if (initialized)
15297 return;
15298 initialized = 1;
15299
15300 /* The breakpoint_ops structure to be inherit by all kinds of
15301 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15302 internal and momentary breakpoints, etc.). */
15303 ops = &bkpt_base_breakpoint_ops;
15304 *ops = base_breakpoint_ops;
15305 ops->re_set = bkpt_re_set;
15306 ops->insert_location = bkpt_insert_location;
15307 ops->remove_location = bkpt_remove_location;
15308 ops->breakpoint_hit = bkpt_breakpoint_hit;
15309 ops->create_sals_from_location = bkpt_create_sals_from_location;
15310 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15311 ops->decode_location = bkpt_decode_location;
15312
15313 /* The breakpoint_ops structure to be used in regular breakpoints. */
15314 ops = &bkpt_breakpoint_ops;
15315 *ops = bkpt_base_breakpoint_ops;
15316 ops->re_set = bkpt_re_set;
15317 ops->resources_needed = bkpt_resources_needed;
15318 ops->print_it = bkpt_print_it;
15319 ops->print_mention = bkpt_print_mention;
15320 ops->print_recreate = bkpt_print_recreate;
15321
15322 /* Ranged breakpoints. */
15323 ops = &ranged_breakpoint_ops;
15324 *ops = bkpt_breakpoint_ops;
15325 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15326 ops->resources_needed = resources_needed_ranged_breakpoint;
15327 ops->print_it = print_it_ranged_breakpoint;
15328 ops->print_one = print_one_ranged_breakpoint;
15329 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15330 ops->print_mention = print_mention_ranged_breakpoint;
15331 ops->print_recreate = print_recreate_ranged_breakpoint;
15332
15333 /* Internal breakpoints. */
15334 ops = &internal_breakpoint_ops;
15335 *ops = bkpt_base_breakpoint_ops;
15336 ops->re_set = internal_bkpt_re_set;
15337 ops->check_status = internal_bkpt_check_status;
15338 ops->print_it = internal_bkpt_print_it;
15339 ops->print_mention = internal_bkpt_print_mention;
15340
15341 /* Momentary breakpoints. */
15342 ops = &momentary_breakpoint_ops;
15343 *ops = bkpt_base_breakpoint_ops;
15344 ops->re_set = momentary_bkpt_re_set;
15345 ops->check_status = momentary_bkpt_check_status;
15346 ops->print_it = momentary_bkpt_print_it;
15347 ops->print_mention = momentary_bkpt_print_mention;
15348
15349 /* Probe breakpoints. */
15350 ops = &bkpt_probe_breakpoint_ops;
15351 *ops = bkpt_breakpoint_ops;
15352 ops->insert_location = bkpt_probe_insert_location;
15353 ops->remove_location = bkpt_probe_remove_location;
15354 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15355 ops->decode_location = bkpt_probe_decode_location;
15356
15357 /* Watchpoints. */
15358 ops = &watchpoint_breakpoint_ops;
15359 *ops = base_breakpoint_ops;
15360 ops->re_set = re_set_watchpoint;
15361 ops->insert_location = insert_watchpoint;
15362 ops->remove_location = remove_watchpoint;
15363 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15364 ops->check_status = check_status_watchpoint;
15365 ops->resources_needed = resources_needed_watchpoint;
15366 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15367 ops->print_it = print_it_watchpoint;
15368 ops->print_mention = print_mention_watchpoint;
15369 ops->print_recreate = print_recreate_watchpoint;
15370 ops->explains_signal = explains_signal_watchpoint;
15371
15372 /* Masked watchpoints. */
15373 ops = &masked_watchpoint_breakpoint_ops;
15374 *ops = watchpoint_breakpoint_ops;
15375 ops->insert_location = insert_masked_watchpoint;
15376 ops->remove_location = remove_masked_watchpoint;
15377 ops->resources_needed = resources_needed_masked_watchpoint;
15378 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15379 ops->print_it = print_it_masked_watchpoint;
15380 ops->print_one_detail = print_one_detail_masked_watchpoint;
15381 ops->print_mention = print_mention_masked_watchpoint;
15382 ops->print_recreate = print_recreate_masked_watchpoint;
15383
15384 /* Tracepoints. */
15385 ops = &tracepoint_breakpoint_ops;
15386 *ops = base_breakpoint_ops;
15387 ops->re_set = tracepoint_re_set;
15388 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15389 ops->print_one_detail = tracepoint_print_one_detail;
15390 ops->print_mention = tracepoint_print_mention;
15391 ops->print_recreate = tracepoint_print_recreate;
15392 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15393 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15394 ops->decode_location = tracepoint_decode_location;
15395
15396 /* Probe tracepoints. */
15397 ops = &tracepoint_probe_breakpoint_ops;
15398 *ops = tracepoint_breakpoint_ops;
15399 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15400 ops->decode_location = tracepoint_probe_decode_location;
15401
15402 /* Static tracepoints with marker (`-m'). */
15403 ops = &strace_marker_breakpoint_ops;
15404 *ops = tracepoint_breakpoint_ops;
15405 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15406 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15407 ops->decode_location = strace_marker_decode_location;
15408
15409 /* Fork catchpoints. */
15410 ops = &catch_fork_breakpoint_ops;
15411 *ops = base_breakpoint_ops;
15412 ops->insert_location = insert_catch_fork;
15413 ops->remove_location = remove_catch_fork;
15414 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15415 ops->print_it = print_it_catch_fork;
15416 ops->print_one = print_one_catch_fork;
15417 ops->print_mention = print_mention_catch_fork;
15418 ops->print_recreate = print_recreate_catch_fork;
15419
15420 /* Vfork catchpoints. */
15421 ops = &catch_vfork_breakpoint_ops;
15422 *ops = base_breakpoint_ops;
15423 ops->insert_location = insert_catch_vfork;
15424 ops->remove_location = remove_catch_vfork;
15425 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15426 ops->print_it = print_it_catch_vfork;
15427 ops->print_one = print_one_catch_vfork;
15428 ops->print_mention = print_mention_catch_vfork;
15429 ops->print_recreate = print_recreate_catch_vfork;
15430
15431 /* Exec catchpoints. */
15432 ops = &catch_exec_breakpoint_ops;
15433 *ops = base_breakpoint_ops;
15434 ops->insert_location = insert_catch_exec;
15435 ops->remove_location = remove_catch_exec;
15436 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15437 ops->print_it = print_it_catch_exec;
15438 ops->print_one = print_one_catch_exec;
15439 ops->print_mention = print_mention_catch_exec;
15440 ops->print_recreate = print_recreate_catch_exec;
15441
15442 /* Solib-related catchpoints. */
15443 ops = &catch_solib_breakpoint_ops;
15444 *ops = base_breakpoint_ops;
15445 ops->insert_location = insert_catch_solib;
15446 ops->remove_location = remove_catch_solib;
15447 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15448 ops->check_status = check_status_catch_solib;
15449 ops->print_it = print_it_catch_solib;
15450 ops->print_one = print_one_catch_solib;
15451 ops->print_mention = print_mention_catch_solib;
15452 ops->print_recreate = print_recreate_catch_solib;
15453
15454 ops = &dprintf_breakpoint_ops;
15455 *ops = bkpt_base_breakpoint_ops;
15456 ops->re_set = dprintf_re_set;
15457 ops->resources_needed = bkpt_resources_needed;
15458 ops->print_it = bkpt_print_it;
15459 ops->print_mention = bkpt_print_mention;
15460 ops->print_recreate = dprintf_print_recreate;
15461 ops->after_condition_true = dprintf_after_condition_true;
15462 ops->breakpoint_hit = dprintf_breakpoint_hit;
15463 }
15464
15465 /* Chain containing all defined "enable breakpoint" subcommands. */
15466
15467 static struct cmd_list_element *enablebreaklist = NULL;
15468
15469 /* See breakpoint.h. */
15470
15471 cmd_list_element *commands_cmd_element = nullptr;
15472
15473 void _initialize_breakpoint ();
15474 void
15475 _initialize_breakpoint ()
15476 {
15477 struct cmd_list_element *c;
15478
15479 initialize_breakpoint_ops ();
15480
15481 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
15482 "breakpoint");
15483 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
15484 "breakpoint");
15485 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
15486 "breakpoint");
15487
15488 breakpoint_chain = 0;
15489 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15490 before a breakpoint is set. */
15491 breakpoint_count = 0;
15492
15493 tracepoint_count = 0;
15494
15495 add_com ("ignore", class_breakpoint, ignore_command, _("\
15496 Set ignore-count of breakpoint number N to COUNT.\n\
15497 Usage is `ignore N COUNT'."));
15498
15499 commands_cmd_element = add_com ("commands", class_breakpoint,
15500 commands_command, _("\
15501 Set commands to be executed when the given breakpoints are hit.\n\
15502 Give a space-separated breakpoint list as argument after \"commands\".\n\
15503 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15504 (e.g. `5-7').\n\
15505 With no argument, the targeted breakpoint is the last one set.\n\
15506 The commands themselves follow starting on the next line.\n\
15507 Type a line containing \"end\" to indicate the end of them.\n\
15508 Give \"silent\" as the first line to make the breakpoint silent;\n\
15509 then no output is printed when it is hit, except what the commands print."));
15510
15511 const auto cc_opts = make_condition_command_options_def_group (nullptr);
15512 static std::string condition_command_help
15513 = gdb::option::build_help (_("\
15514 Specify breakpoint number N to break only if COND is true.\n\
15515 Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
15516 is an expression to be evaluated whenever breakpoint N is reached.\n\
15517 \n\
15518 Options:\n\
15519 %OPTIONS%"), cc_opts);
15520
15521 c = add_com ("condition", class_breakpoint, condition_command,
15522 condition_command_help.c_str ());
15523 set_cmd_completer_handle_brkchars (c, condition_completer);
15524
15525 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15526 Set a temporary breakpoint.\n\
15527 Like \"break\" except the breakpoint is only temporary,\n\
15528 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15529 by using \"enable delete\" on the breakpoint number.\n\
15530 \n"
15531 BREAK_ARGS_HELP ("tbreak")));
15532 set_cmd_completer (c, location_completer);
15533
15534 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15535 Set a hardware assisted breakpoint.\n\
15536 Like \"break\" except the breakpoint requires hardware support,\n\
15537 some target hardware may not have this support.\n\
15538 \n"
15539 BREAK_ARGS_HELP ("hbreak")));
15540 set_cmd_completer (c, location_completer);
15541
15542 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15543 Set a temporary hardware assisted breakpoint.\n\
15544 Like \"hbreak\" except the breakpoint is only temporary,\n\
15545 so it will be deleted when hit.\n\
15546 \n"
15547 BREAK_ARGS_HELP ("thbreak")));
15548 set_cmd_completer (c, location_completer);
15549
15550 cmd_list_element *enable_cmd
15551 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15552 Enable all or some breakpoints.\n\
15553 Usage: enable [BREAKPOINTNUM]...\n\
15554 Give breakpoint numbers (separated by spaces) as arguments.\n\
15555 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15556 This is used to cancel the effect of the \"disable\" command.\n\
15557 With a subcommand you can enable temporarily."),
15558 &enablelist, 1, &cmdlist);
15559
15560 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
15561
15562 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15563 Enable all or some breakpoints.\n\
15564 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
15565 Give breakpoint numbers (separated by spaces) as arguments.\n\
15566 This is used to cancel the effect of the \"disable\" command.\n\
15567 May be abbreviated to simply \"enable\"."),
15568 &enablebreaklist, 1, &enablelist);
15569
15570 add_cmd ("once", no_class, enable_once_command, _("\
15571 Enable some breakpoints for one hit.\n\
15572 Usage: enable breakpoints once BREAKPOINTNUM...\n\
15573 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15574 &enablebreaklist);
15575
15576 add_cmd ("delete", no_class, enable_delete_command, _("\
15577 Enable some breakpoints and delete when hit.\n\
15578 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
15579 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15580 &enablebreaklist);
15581
15582 add_cmd ("count", no_class, enable_count_command, _("\
15583 Enable some breakpoints for COUNT hits.\n\
15584 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
15585 If a breakpoint is hit while enabled in this fashion,\n\
15586 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15587 &enablebreaklist);
15588
15589 add_cmd ("delete", no_class, enable_delete_command, _("\
15590 Enable some breakpoints and delete when hit.\n\
15591 Usage: enable delete BREAKPOINTNUM...\n\
15592 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15593 &enablelist);
15594
15595 add_cmd ("once", no_class, enable_once_command, _("\
15596 Enable some breakpoints for one hit.\n\
15597 Usage: enable once BREAKPOINTNUM...\n\
15598 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15599 &enablelist);
15600
15601 add_cmd ("count", no_class, enable_count_command, _("\
15602 Enable some breakpoints for COUNT hits.\n\
15603 Usage: enable count COUNT BREAKPOINTNUM...\n\
15604 If a breakpoint is hit while enabled in this fashion,\n\
15605 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15606 &enablelist);
15607
15608 cmd_list_element *disable_cmd
15609 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15610 Disable all or some breakpoints.\n\
15611 Usage: disable [BREAKPOINTNUM]...\n\
15612 Arguments are breakpoint numbers with spaces in between.\n\
15613 To disable all breakpoints, give no argument.\n\
15614 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15615 &disablelist, 1, &cmdlist);
15616 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
15617 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
15618
15619 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
15620 Disable all or some breakpoints.\n\
15621 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
15622 Arguments are breakpoint numbers with spaces in between.\n\
15623 To disable all breakpoints, give no argument.\n\
15624 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15625 This command may be abbreviated \"disable\"."),
15626 &disablelist);
15627
15628 cmd_list_element *delete_cmd
15629 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15630 Delete all or some breakpoints.\n\
15631 Usage: delete [BREAKPOINTNUM]...\n\
15632 Arguments are breakpoint numbers with spaces in between.\n\
15633 To delete all breakpoints, give no argument.\n\
15634 \n\
15635 Also a prefix command for deletion of other GDB objects."),
15636 &deletelist, 1, &cmdlist);
15637 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
15638 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
15639
15640 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
15641 Delete all or some breakpoints or auto-display expressions.\n\
15642 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
15643 Arguments are breakpoint numbers with spaces in between.\n\
15644 To delete all breakpoints, give no argument.\n\
15645 This command may be abbreviated \"delete\"."),
15646 &deletelist);
15647
15648 cmd_list_element *clear_cmd
15649 = add_com ("clear", class_breakpoint, clear_command, _("\
15650 Clear breakpoint at specified location.\n\
15651 Argument may be a linespec, explicit, or address location as described below.\n\
15652 \n\
15653 With no argument, clears all breakpoints in the line that the selected frame\n\
15654 is executing in.\n"
15655 "\n" LOCATION_HELP_STRING "\n\n\
15656 See also the \"delete\" command which clears breakpoints by number."));
15657 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
15658
15659 cmd_list_element *break_cmd
15660 = add_com ("break", class_breakpoint, break_command, _("\
15661 Set breakpoint at specified location.\n"
15662 BREAK_ARGS_HELP ("break")));
15663 set_cmd_completer (break_cmd, location_completer);
15664
15665 add_com_alias ("b", break_cmd, class_run, 1);
15666 add_com_alias ("br", break_cmd, class_run, 1);
15667 add_com_alias ("bre", break_cmd, class_run, 1);
15668 add_com_alias ("brea", break_cmd, class_run, 1);
15669
15670 if (dbx_commands)
15671 {
15672 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15673 Break in function/address or break at a line in the current file."),
15674 &stoplist, 1, &cmdlist);
15675 add_cmd ("in", class_breakpoint, stopin_command,
15676 _("Break in function or address."), &stoplist);
15677 add_cmd ("at", class_breakpoint, stopat_command,
15678 _("Break at a line in the current file."), &stoplist);
15679 add_com ("status", class_info, info_breakpoints_command, _("\
15680 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15681 The \"Type\" column indicates one of:\n\
15682 \tbreakpoint - normal breakpoint\n\
15683 \twatchpoint - watchpoint\n\
15684 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15685 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15686 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15687 address and file/line number respectively.\n\
15688 \n\
15689 Convenience variable \"$_\" and default examine address for \"x\"\n\
15690 are set to the address of the last breakpoint listed unless the command\n\
15691 is prefixed with \"server \".\n\n\
15692 Convenience variable \"$bpnum\" contains the number of the last\n\
15693 breakpoint set."));
15694 }
15695
15696 cmd_list_element *info_breakpoints_cmd
15697 = add_info ("breakpoints", info_breakpoints_command, _("\
15698 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15699 The \"Type\" column indicates one of:\n\
15700 \tbreakpoint - normal breakpoint\n\
15701 \twatchpoint - watchpoint\n\
15702 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15703 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15704 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15705 address and file/line number respectively.\n\
15706 \n\
15707 Convenience variable \"$_\" and default examine address for \"x\"\n\
15708 are set to the address of the last breakpoint listed unless the command\n\
15709 is prefixed with \"server \".\n\n\
15710 Convenience variable \"$bpnum\" contains the number of the last\n\
15711 breakpoint set."));
15712
15713 add_info_alias ("b", info_breakpoints_cmd, 1);
15714
15715 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15716 Status of all breakpoints, or breakpoint number NUMBER.\n\
15717 The \"Type\" column indicates one of:\n\
15718 \tbreakpoint - normal breakpoint\n\
15719 \twatchpoint - watchpoint\n\
15720 \tlongjmp - internal breakpoint used to step through longjmp()\n\
15721 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15722 \tuntil - internal breakpoint used by the \"until\" command\n\
15723 \tfinish - internal breakpoint used by the \"finish\" command\n\
15724 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15725 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15726 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15727 address and file/line number respectively.\n\
15728 \n\
15729 Convenience variable \"$_\" and default examine address for \"x\"\n\
15730 are set to the address of the last breakpoint listed unless the command\n\
15731 is prefixed with \"server \".\n\n\
15732 Convenience variable \"$bpnum\" contains the number of the last\n\
15733 breakpoint set."),
15734 &maintenanceinfolist);
15735
15736 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
15737 Set catchpoints to catch events."),
15738 &catch_cmdlist,
15739 0/*allow-unknown*/, &cmdlist);
15740
15741 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
15742 Set temporary catchpoints to catch events."),
15743 &tcatch_cmdlist,
15744 0/*allow-unknown*/, &cmdlist);
15745
15746 add_catch_command ("fork", _("Catch calls to fork."),
15747 catch_fork_command_1,
15748 NULL,
15749 (void *) (uintptr_t) catch_fork_permanent,
15750 (void *) (uintptr_t) catch_fork_temporary);
15751 add_catch_command ("vfork", _("Catch calls to vfork."),
15752 catch_fork_command_1,
15753 NULL,
15754 (void *) (uintptr_t) catch_vfork_permanent,
15755 (void *) (uintptr_t) catch_vfork_temporary);
15756 add_catch_command ("exec", _("Catch calls to exec."),
15757 catch_exec_command_1,
15758 NULL,
15759 CATCH_PERMANENT,
15760 CATCH_TEMPORARY);
15761 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15762 Usage: catch load [REGEX]\n\
15763 If REGEX is given, only stop for libraries matching the regular expression."),
15764 catch_load_command_1,
15765 NULL,
15766 CATCH_PERMANENT,
15767 CATCH_TEMPORARY);
15768 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15769 Usage: catch unload [REGEX]\n\
15770 If REGEX is given, only stop for libraries matching the regular expression."),
15771 catch_unload_command_1,
15772 NULL,
15773 CATCH_PERMANENT,
15774 CATCH_TEMPORARY);
15775
15776 const auto opts = make_watch_options_def_group (nullptr);
15777
15778 static const std::string watch_help = gdb::option::build_help (_("\
15779 Set a watchpoint for EXPRESSION.\n\
15780 Usage: watch [-location] EXPRESSION\n\
15781 \n\
15782 Options:\n\
15783 %OPTIONS%\n\
15784 \n\
15785 A watchpoint stops execution of your program whenever the value of\n\
15786 an expression changes."), opts);
15787 c = add_com ("watch", class_breakpoint, watch_command,
15788 watch_help.c_str ());
15789 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15790
15791 static const std::string rwatch_help = gdb::option::build_help (_("\
15792 Set a read watchpoint for EXPRESSION.\n\
15793 Usage: rwatch [-location] EXPRESSION\n\
15794 \n\
15795 Options:\n\
15796 %OPTIONS%\n\
15797 \n\
15798 A read watchpoint stops execution of your program whenever the value of\n\
15799 an expression is read."), opts);
15800 c = add_com ("rwatch", class_breakpoint, rwatch_command,
15801 rwatch_help.c_str ());
15802 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15803
15804 static const std::string awatch_help = gdb::option::build_help (_("\
15805 Set an access watchpoint for EXPRESSION.\n\
15806 Usage: awatch [-location] EXPRESSION\n\
15807 \n\
15808 Options:\n\
15809 %OPTIONS%\n\
15810 \n\
15811 An access watchpoint stops execution of your program whenever the value\n\
15812 of an expression is either read or written."), opts);
15813 c = add_com ("awatch", class_breakpoint, awatch_command,
15814 awatch_help.c_str ());
15815 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15816
15817 add_info ("watchpoints", info_watchpoints_command, _("\
15818 Status of specified watchpoints (all watchpoints if no argument)."));
15819
15820 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15821 respond to changes - contrary to the description. */
15822 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15823 &can_use_hw_watchpoints, _("\
15824 Set debugger's willingness to use watchpoint hardware."), _("\
15825 Show debugger's willingness to use watchpoint hardware."), _("\
15826 If zero, gdb will not use hardware for new watchpoints, even if\n\
15827 such is available. (However, any hardware watchpoints that were\n\
15828 created before setting this to nonzero, will continue to use watchpoint\n\
15829 hardware.)"),
15830 NULL,
15831 show_can_use_hw_watchpoints,
15832 &setlist, &showlist);
15833
15834 can_use_hw_watchpoints = 1;
15835
15836 /* Tracepoint manipulation commands. */
15837
15838 cmd_list_element *trace_cmd
15839 = add_com ("trace", class_breakpoint, trace_command, _("\
15840 Set a tracepoint at specified location.\n\
15841 \n"
15842 BREAK_ARGS_HELP ("trace") "\n\
15843 Do \"help tracepoints\" for info on other tracepoint commands."));
15844 set_cmd_completer (trace_cmd, location_completer);
15845
15846 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
15847 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
15848 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
15849 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
15850
15851 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
15852 Set a fast tracepoint at specified location.\n\
15853 \n"
15854 BREAK_ARGS_HELP ("ftrace") "\n\
15855 Do \"help tracepoints\" for info on other tracepoint commands."));
15856 set_cmd_completer (c, location_completer);
15857
15858 c = add_com ("strace", class_breakpoint, strace_command, _("\
15859 Set a static tracepoint at location or marker.\n\
15860 \n\
15861 strace [LOCATION] [if CONDITION]\n\
15862 LOCATION may be a linespec, explicit, or address location (described below) \n\
15863 or -m MARKER_ID.\n\n\
15864 If a marker id is specified, probe the marker with that name. With\n\
15865 no LOCATION, uses current execution address of the selected stack frame.\n\
15866 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15867 This collects arbitrary user data passed in the probe point call to the\n\
15868 tracing library. You can inspect it when analyzing the trace buffer,\n\
15869 by printing the $_sdata variable like any other convenience variable.\n\
15870 \n\
15871 CONDITION is a boolean expression.\n\
15872 \n" LOCATION_HELP_STRING "\n\n\
15873 Multiple tracepoints at one place are permitted, and useful if their\n\
15874 conditions are different.\n\
15875 \n\
15876 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15877 Do \"help tracepoints\" for info on other tracepoint commands."));
15878 set_cmd_completer (c, location_completer);
15879
15880 cmd_list_element *info_tracepoints_cmd
15881 = add_info ("tracepoints", info_tracepoints_command, _("\
15882 Status of specified tracepoints (all tracepoints if no argument).\n\
15883 Convenience variable \"$tpnum\" contains the number of the\n\
15884 last tracepoint set."));
15885
15886 add_info_alias ("tp", info_tracepoints_cmd, 1);
15887
15888 cmd_list_element *delete_tracepoints_cmd
15889 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15890 Delete specified tracepoints.\n\
15891 Arguments are tracepoint numbers, separated by spaces.\n\
15892 No argument means delete all tracepoints."),
15893 &deletelist);
15894 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
15895
15896 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15897 Disable specified tracepoints.\n\
15898 Arguments are tracepoint numbers, separated by spaces.\n\
15899 No argument means disable all tracepoints."),
15900 &disablelist);
15901 deprecate_cmd (c, "disable");
15902
15903 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15904 Enable specified tracepoints.\n\
15905 Arguments are tracepoint numbers, separated by spaces.\n\
15906 No argument means enable all tracepoints."),
15907 &enablelist);
15908 deprecate_cmd (c, "enable");
15909
15910 add_com ("passcount", class_trace, trace_pass_command, _("\
15911 Set the passcount for a tracepoint.\n\
15912 The trace will end when the tracepoint has been passed 'count' times.\n\
15913 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15914 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15915
15916 add_basic_prefix_cmd ("save", class_breakpoint,
15917 _("Save breakpoint definitions as a script."),
15918 &save_cmdlist,
15919 0/*allow-unknown*/, &cmdlist);
15920
15921 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15922 Save current breakpoint definitions as a script.\n\
15923 This includes all types of breakpoints (breakpoints, watchpoints,\n\
15924 catchpoints, tracepoints). Use the 'source' command in another debug\n\
15925 session to restore them."),
15926 &save_cmdlist);
15927 set_cmd_completer (c, filename_completer);
15928
15929 cmd_list_element *save_tracepoints_cmd
15930 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
15931 Save current tracepoint definitions as a script.\n\
15932 Use the 'source' command in another debug session to restore them."),
15933 &save_cmdlist);
15934 set_cmd_completer (save_tracepoints_cmd, filename_completer);
15935
15936 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
15937 deprecate_cmd (c, "save tracepoints");
15938
15939 add_basic_prefix_cmd ("breakpoint", class_maintenance, _("\
15940 Breakpoint specific settings.\n\
15941 Configure various breakpoint-specific variables such as\n\
15942 pending breakpoint behavior."),
15943 &breakpoint_set_cmdlist,
15944 0/*allow-unknown*/, &setlist);
15945 add_show_prefix_cmd ("breakpoint", class_maintenance, _("\
15946 Breakpoint specific settings.\n\
15947 Configure various breakpoint-specific variables such as\n\
15948 pending breakpoint behavior."),
15949 &breakpoint_show_cmdlist,
15950 0/*allow-unknown*/, &showlist);
15951
15952 add_setshow_auto_boolean_cmd ("pending", no_class,
15953 &pending_break_support, _("\
15954 Set debugger's behavior regarding pending breakpoints."), _("\
15955 Show debugger's behavior regarding pending breakpoints."), _("\
15956 If on, an unrecognized breakpoint location will cause gdb to create a\n\
15957 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15958 an error. If auto, an unrecognized breakpoint location results in a\n\
15959 user-query to see if a pending breakpoint should be created."),
15960 NULL,
15961 show_pending_break_support,
15962 &breakpoint_set_cmdlist,
15963 &breakpoint_show_cmdlist);
15964
15965 pending_break_support = AUTO_BOOLEAN_AUTO;
15966
15967 add_setshow_boolean_cmd ("auto-hw", no_class,
15968 &automatic_hardware_breakpoints, _("\
15969 Set automatic usage of hardware breakpoints."), _("\
15970 Show automatic usage of hardware breakpoints."), _("\
15971 If set, the debugger will automatically use hardware breakpoints for\n\
15972 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15973 a warning will be emitted for such breakpoints."),
15974 NULL,
15975 show_automatic_hardware_breakpoints,
15976 &breakpoint_set_cmdlist,
15977 &breakpoint_show_cmdlist);
15978
15979 add_setshow_boolean_cmd ("always-inserted", class_support,
15980 &always_inserted_mode, _("\
15981 Set mode for inserting breakpoints."), _("\
15982 Show mode for inserting breakpoints."), _("\
15983 When this mode is on, breakpoints are inserted immediately as soon as\n\
15984 they're created, kept inserted even when execution stops, and removed\n\
15985 only when the user deletes them. When this mode is off (the default),\n\
15986 breakpoints are inserted only when execution continues, and removed\n\
15987 when execution stops."),
15988 NULL,
15989 &show_always_inserted_mode,
15990 &breakpoint_set_cmdlist,
15991 &breakpoint_show_cmdlist);
15992
15993 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15994 condition_evaluation_enums,
15995 &condition_evaluation_mode_1, _("\
15996 Set mode of breakpoint condition evaluation."), _("\
15997 Show mode of breakpoint condition evaluation."), _("\
15998 When this is set to \"host\", breakpoint conditions will be\n\
15999 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16000 breakpoint conditions will be downloaded to the target (if the target\n\
16001 supports such feature) and conditions will be evaluated on the target's side.\n\
16002 If this is set to \"auto\" (default), this will be automatically set to\n\
16003 \"target\" if it supports condition evaluation, otherwise it will\n\
16004 be set to \"host\"."),
16005 &set_condition_evaluation_mode,
16006 &show_condition_evaluation_mode,
16007 &breakpoint_set_cmdlist,
16008 &breakpoint_show_cmdlist);
16009
16010 add_com ("break-range", class_breakpoint, break_range_command, _("\
16011 Set a breakpoint for an address range.\n\
16012 break-range START-LOCATION, END-LOCATION\n\
16013 where START-LOCATION and END-LOCATION can be one of the following:\n\
16014 LINENUM, for that line in the current file,\n\
16015 FILE:LINENUM, for that line in that file,\n\
16016 +OFFSET, for that number of lines after the current line\n\
16017 or the start of the range\n\
16018 FUNCTION, for the first line in that function,\n\
16019 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16020 *ADDRESS, for the instruction at that address.\n\
16021 \n\
16022 The breakpoint will stop execution of the inferior whenever it executes\n\
16023 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16024 range (including START-LOCATION and END-LOCATION)."));
16025
16026 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16027 Set a dynamic printf at specified location.\n\
16028 dprintf location,format string,arg1,arg2,...\n\
16029 location may be a linespec, explicit, or address location.\n"
16030 "\n" LOCATION_HELP_STRING));
16031 set_cmd_completer (c, location_completer);
16032
16033 add_setshow_enum_cmd ("dprintf-style", class_support,
16034 dprintf_style_enums, &dprintf_style, _("\
16035 Set the style of usage for dynamic printf."), _("\
16036 Show the style of usage for dynamic printf."), _("\
16037 This setting chooses how GDB will do a dynamic printf.\n\
16038 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16039 console, as with the \"printf\" command.\n\
16040 If the value is \"call\", the print is done by calling a function in your\n\
16041 program; by default printf(), but you can choose a different function or\n\
16042 output stream by setting dprintf-function and dprintf-channel."),
16043 update_dprintf_commands, NULL,
16044 &setlist, &showlist);
16045
16046 dprintf_function = xstrdup ("printf");
16047 add_setshow_string_cmd ("dprintf-function", class_support,
16048 &dprintf_function, _("\
16049 Set the function to use for dynamic printf."), _("\
16050 Show the function to use for dynamic printf."), NULL,
16051 update_dprintf_commands, NULL,
16052 &setlist, &showlist);
16053
16054 dprintf_channel = xstrdup ("");
16055 add_setshow_string_cmd ("dprintf-channel", class_support,
16056 &dprintf_channel, _("\
16057 Set the channel to use for dynamic printf."), _("\
16058 Show the channel to use for dynamic printf."), NULL,
16059 update_dprintf_commands, NULL,
16060 &setlist, &showlist);
16061
16062 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16063 &disconnected_dprintf, _("\
16064 Set whether dprintf continues after GDB disconnects."), _("\
16065 Show whether dprintf continues after GDB disconnects."), _("\
16066 Use this to let dprintf commands continue to hit and produce output\n\
16067 even if GDB disconnects or detaches from the target."),
16068 NULL,
16069 NULL,
16070 &setlist, &showlist);
16071
16072 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16073 Target agent only formatted printing, like the C \"printf\" function.\n\
16074 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
16075 This supports most C printf format specifications, like %s, %d, etc.\n\
16076 This is useful for formatted output in user-defined commands."));
16077
16078 automatic_hardware_breakpoints = true;
16079
16080 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
16081 "breakpoint");
16082 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
16083 "breakpoint");
16084 }