gdb: add breakpoint::locations method
[binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2021 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include <ctype.h>
23 #include "hashtab.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "breakpoint.h"
27 #include "tracepoint.h"
28 #include "gdbtypes.h"
29 #include "expression.h"
30 #include "gdbcore.h"
31 #include "gdbcmd.h"
32 #include "value.h"
33 #include "command.h"
34 #include "inferior.h"
35 #include "infrun.h"
36 #include "gdbthread.h"
37 #include "target.h"
38 #include "language.h"
39 #include "gdb-demangle.h"
40 #include "filenames.h"
41 #include "annotate.h"
42 #include "symfile.h"
43 #include "objfiles.h"
44 #include "source.h"
45 #include "linespec.h"
46 #include "completer.h"
47 #include "ui-out.h"
48 #include "cli/cli-script.h"
49 #include "block.h"
50 #include "solib.h"
51 #include "solist.h"
52 #include "observable.h"
53 #include "memattr.h"
54 #include "ada-lang.h"
55 #include "top.h"
56 #include "valprint.h"
57 #include "jit.h"
58 #include "parser-defs.h"
59 #include "gdb_regex.h"
60 #include "probe.h"
61 #include "cli/cli-utils.h"
62 #include "stack.h"
63 #include "ax-gdb.h"
64 #include "dummy-frame.h"
65 #include "interps.h"
66 #include "gdbsupport/format.h"
67 #include "thread-fsm.h"
68 #include "tid-parse.h"
69 #include "cli/cli-style.h"
70
71 /* readline include files */
72 #include "readline/tilde.h"
73
74 /* readline defines this. */
75 #undef savestring
76
77 #include "mi/mi-common.h"
78 #include "extension.h"
79 #include <algorithm>
80 #include "progspace-and-thread.h"
81 #include "gdbsupport/array-view.h"
82 #include "gdbsupport/gdb_optional.h"
83
84 /* Prototypes for local functions. */
85
86 static void map_breakpoint_numbers (const char *,
87 gdb::function_view<void (breakpoint *)>);
88
89 static void breakpoint_re_set_default (struct breakpoint *);
90
91 static void
92 create_sals_from_location_default (struct event_location *location,
93 struct linespec_result *canonical,
94 enum bptype type_wanted);
95
96 static void create_breakpoints_sal_default (struct gdbarch *,
97 struct linespec_result *,
98 gdb::unique_xmalloc_ptr<char>,
99 gdb::unique_xmalloc_ptr<char>,
100 enum bptype,
101 enum bpdisp, int, int,
102 int,
103 const struct breakpoint_ops *,
104 int, int, int, unsigned);
105
106 static std::vector<symtab_and_line> decode_location_default
107 (struct breakpoint *b, struct event_location *location,
108 struct program_space *search_pspace);
109
110 static int can_use_hardware_watchpoint
111 (const std::vector<value_ref_ptr> &vals);
112
113 static void mention (struct breakpoint *);
114
115 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
116 enum bptype,
117 const struct breakpoint_ops *);
118 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
119 const struct symtab_and_line *);
120
121 /* This function is used in gdbtk sources and thus can not be made
122 static. */
123 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
124 struct symtab_and_line,
125 enum bptype,
126 const struct breakpoint_ops *);
127
128 static struct breakpoint *
129 momentary_breakpoint_from_master (struct breakpoint *orig,
130 enum bptype type,
131 const struct breakpoint_ops *ops,
132 int loc_enabled);
133
134 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
135
136 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
137 CORE_ADDR bpaddr,
138 enum bptype bptype);
139
140 static void describe_other_breakpoints (struct gdbarch *,
141 struct program_space *, CORE_ADDR,
142 struct obj_section *, int);
143
144 static int watchpoint_locations_match (struct bp_location *loc1,
145 struct bp_location *loc2);
146
147 static int breakpoint_locations_match (struct bp_location *loc1,
148 struct bp_location *loc2,
149 bool sw_hw_bps_match = false);
150
151 static int breakpoint_location_address_match (struct bp_location *bl,
152 const struct address_space *aspace,
153 CORE_ADDR addr);
154
155 static int breakpoint_location_address_range_overlap (struct bp_location *,
156 const address_space *,
157 CORE_ADDR, int);
158
159 static int remove_breakpoint (struct bp_location *);
160 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
161
162 static enum print_stop_action print_bp_stop_message (bpstat bs);
163
164 static int hw_breakpoint_used_count (void);
165
166 static int hw_watchpoint_use_count (struct breakpoint *);
167
168 static int hw_watchpoint_used_count_others (struct breakpoint *except,
169 enum bptype type,
170 int *other_type_used);
171
172 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
173 int count);
174
175 static void decref_bp_location (struct bp_location **loc);
176
177 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
178
179 /* update_global_location_list's modes of operation wrt to whether to
180 insert locations now. */
181 enum ugll_insert_mode
182 {
183 /* Don't insert any breakpoint locations into the inferior, only
184 remove already-inserted locations that no longer should be
185 inserted. Functions that delete a breakpoint or breakpoints
186 should specify this mode, so that deleting a breakpoint doesn't
187 have the side effect of inserting the locations of other
188 breakpoints that are marked not-inserted, but should_be_inserted
189 returns true on them.
190
191 This behavior is useful is situations close to tear-down -- e.g.,
192 after an exec, while the target still has execution, but
193 breakpoint shadows of the previous executable image should *NOT*
194 be restored to the new image; or before detaching, where the
195 target still has execution and wants to delete breakpoints from
196 GDB's lists, and all breakpoints had already been removed from
197 the inferior. */
198 UGLL_DONT_INSERT,
199
200 /* May insert breakpoints iff breakpoints_should_be_inserted_now
201 claims breakpoints should be inserted now. */
202 UGLL_MAY_INSERT,
203
204 /* Insert locations now, irrespective of
205 breakpoints_should_be_inserted_now. E.g., say all threads are
206 stopped right now, and the user did "continue". We need to
207 insert breakpoints _before_ resuming the target, but
208 UGLL_MAY_INSERT wouldn't insert them, because
209 breakpoints_should_be_inserted_now returns false at that point,
210 as no thread is running yet. */
211 UGLL_INSERT
212 };
213
214 static void update_global_location_list (enum ugll_insert_mode);
215
216 static void update_global_location_list_nothrow (enum ugll_insert_mode);
217
218 static void insert_breakpoint_locations (void);
219
220 static void trace_pass_command (const char *, int);
221
222 static void set_tracepoint_count (int num);
223
224 static bool is_masked_watchpoint (const struct breakpoint *b);
225
226 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
227
228 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
229 otherwise. */
230
231 static int strace_marker_p (struct breakpoint *b);
232
233 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
234 that are implemented on top of software or hardware breakpoints
235 (user breakpoints, internal and momentary breakpoints, etc.). */
236 static struct breakpoint_ops bkpt_base_breakpoint_ops;
237
238 /* Internal breakpoints class type. */
239 static struct breakpoint_ops internal_breakpoint_ops;
240
241 /* Momentary breakpoints class type. */
242 static struct breakpoint_ops momentary_breakpoint_ops;
243
244 /* The breakpoint_ops structure to be used in regular user created
245 breakpoints. */
246 struct breakpoint_ops bkpt_breakpoint_ops;
247
248 /* Breakpoints set on probes. */
249 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
250
251 /* Tracepoints set on probes. */
252 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
253
254 /* Dynamic printf class type. */
255 struct breakpoint_ops dprintf_breakpoint_ops;
256
257 /* The style in which to perform a dynamic printf. This is a user
258 option because different output options have different tradeoffs;
259 if GDB does the printing, there is better error handling if there
260 is a problem with any of the arguments, but using an inferior
261 function lets you have special-purpose printers and sending of
262 output to the same place as compiled-in print functions. */
263
264 static const char dprintf_style_gdb[] = "gdb";
265 static const char dprintf_style_call[] = "call";
266 static const char dprintf_style_agent[] = "agent";
267 static const char *const dprintf_style_enums[] = {
268 dprintf_style_gdb,
269 dprintf_style_call,
270 dprintf_style_agent,
271 NULL
272 };
273 static const char *dprintf_style = dprintf_style_gdb;
274
275 /* The function to use for dynamic printf if the preferred style is to
276 call into the inferior. The value is simply a string that is
277 copied into the command, so it can be anything that GDB can
278 evaluate to a callable address, not necessarily a function name. */
279
280 static char *dprintf_function;
281
282 /* The channel to use for dynamic printf if the preferred style is to
283 call into the inferior; if a nonempty string, it will be passed to
284 the call as the first argument, with the format string as the
285 second. As with the dprintf function, this can be anything that
286 GDB knows how to evaluate, so in addition to common choices like
287 "stderr", this could be an app-specific expression like
288 "mystreams[curlogger]". */
289
290 static char *dprintf_channel;
291
292 /* True if dprintf commands should continue to operate even if GDB
293 has disconnected. */
294 static bool disconnected_dprintf = true;
295
296 struct command_line *
297 breakpoint_commands (struct breakpoint *b)
298 {
299 return b->commands ? b->commands.get () : NULL;
300 }
301
302 /* Flag indicating that a command has proceeded the inferior past the
303 current breakpoint. */
304
305 static bool breakpoint_proceeded;
306
307 const char *
308 bpdisp_text (enum bpdisp disp)
309 {
310 /* NOTE: the following values are a part of MI protocol and
311 represent values of 'disp' field returned when inferior stops at
312 a breakpoint. */
313 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
314
315 return bpdisps[(int) disp];
316 }
317
318 /* Prototypes for exported functions. */
319 /* If FALSE, gdb will not use hardware support for watchpoints, even
320 if such is available. */
321 static int can_use_hw_watchpoints;
322
323 static void
324 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
325 struct cmd_list_element *c,
326 const char *value)
327 {
328 fprintf_filtered (file,
329 _("Debugger's willingness to use "
330 "watchpoint hardware is %s.\n"),
331 value);
332 }
333
334 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
335 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
336 for unrecognized breakpoint locations.
337 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
338 static enum auto_boolean pending_break_support;
339 static void
340 show_pending_break_support (struct ui_file *file, int from_tty,
341 struct cmd_list_element *c,
342 const char *value)
343 {
344 fprintf_filtered (file,
345 _("Debugger's behavior regarding "
346 "pending breakpoints is %s.\n"),
347 value);
348 }
349
350 /* If true, gdb will automatically use hardware breakpoints for breakpoints
351 set with "break" but falling in read-only memory.
352 If false, gdb will warn about such breakpoints, but won't automatically
353 use hardware breakpoints. */
354 static bool automatic_hardware_breakpoints;
355 static void
356 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
357 struct cmd_list_element *c,
358 const char *value)
359 {
360 fprintf_filtered (file,
361 _("Automatic usage of hardware breakpoints is %s.\n"),
362 value);
363 }
364
365 /* If on, GDB keeps breakpoints inserted even if the inferior is
366 stopped, and immediately inserts any new breakpoints as soon as
367 they're created. If off (default), GDB keeps breakpoints off of
368 the target as long as possible. That is, it delays inserting
369 breakpoints until the next resume, and removes them again when the
370 target fully stops. This is a bit safer in case GDB crashes while
371 processing user input. */
372 static bool always_inserted_mode = false;
373
374 static void
375 show_always_inserted_mode (struct ui_file *file, int from_tty,
376 struct cmd_list_element *c, const char *value)
377 {
378 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
379 value);
380 }
381
382 /* See breakpoint.h. */
383
384 int
385 breakpoints_should_be_inserted_now (void)
386 {
387 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
388 {
389 /* If breakpoints are global, they should be inserted even if no
390 thread under gdb's control is running, or even if there are
391 no threads under GDB's control yet. */
392 return 1;
393 }
394 else
395 {
396 if (always_inserted_mode)
397 {
398 /* The user wants breakpoints inserted even if all threads
399 are stopped. */
400 return 1;
401 }
402
403 for (inferior *inf : all_inferiors ())
404 if (inf->has_execution ()
405 && threads_are_executing (inf->process_target ()))
406 return 1;
407
408 /* Don't remove breakpoints yet if, even though all threads are
409 stopped, we still have events to process. */
410 for (thread_info *tp : all_non_exited_threads ())
411 if (tp->resumed
412 && tp->suspend.waitstatus_pending_p)
413 return 1;
414 }
415 return 0;
416 }
417
418 static const char condition_evaluation_both[] = "host or target";
419
420 /* Modes for breakpoint condition evaluation. */
421 static const char condition_evaluation_auto[] = "auto";
422 static const char condition_evaluation_host[] = "host";
423 static const char condition_evaluation_target[] = "target";
424 static const char *const condition_evaluation_enums[] = {
425 condition_evaluation_auto,
426 condition_evaluation_host,
427 condition_evaluation_target,
428 NULL
429 };
430
431 /* Global that holds the current mode for breakpoint condition evaluation. */
432 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
433
434 /* Global that we use to display information to the user (gets its value from
435 condition_evaluation_mode_1. */
436 static const char *condition_evaluation_mode = condition_evaluation_auto;
437
438 /* Translate a condition evaluation mode MODE into either "host"
439 or "target". This is used mostly to translate from "auto" to the
440 real setting that is being used. It returns the translated
441 evaluation mode. */
442
443 static const char *
444 translate_condition_evaluation_mode (const char *mode)
445 {
446 if (mode == condition_evaluation_auto)
447 {
448 if (target_supports_evaluation_of_breakpoint_conditions ())
449 return condition_evaluation_target;
450 else
451 return condition_evaluation_host;
452 }
453 else
454 return mode;
455 }
456
457 /* Discovers what condition_evaluation_auto translates to. */
458
459 static const char *
460 breakpoint_condition_evaluation_mode (void)
461 {
462 return translate_condition_evaluation_mode (condition_evaluation_mode);
463 }
464
465 /* Return true if GDB should evaluate breakpoint conditions or false
466 otherwise. */
467
468 static int
469 gdb_evaluates_breakpoint_condition_p (void)
470 {
471 const char *mode = breakpoint_condition_evaluation_mode ();
472
473 return (mode == condition_evaluation_host);
474 }
475
476 /* Are we executing breakpoint commands? */
477 static int executing_breakpoint_commands;
478
479 /* Are overlay event breakpoints enabled? */
480 static int overlay_events_enabled;
481
482 /* See description in breakpoint.h. */
483 bool target_exact_watchpoints = false;
484
485 /* Walk the following statement or block through all breakpoints.
486 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
487 current breakpoint. */
488
489 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
490 for (B = breakpoint_chain; \
491 B ? (TMP=B->next, 1): 0; \
492 B = TMP)
493
494 /* Similar iterator for the low-level breakpoints. SAFE variant is
495 not provided so update_global_location_list must not be called
496 while executing the block of ALL_BP_LOCATIONS. */
497
498 #define ALL_BP_LOCATIONS(B,BP_TMP) \
499 for (BP_TMP = bp_locations; \
500 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
501 BP_TMP++)
502
503 /* Iterates through locations with address ADDRESS for the currently selected
504 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
505 to where the loop should start from.
506 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
507 appropriate location to start with. */
508
509 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
510 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
511 BP_LOCP_TMP = BP_LOCP_START; \
512 BP_LOCP_START \
513 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
514 && (*BP_LOCP_TMP)->address == ADDRESS); \
515 BP_LOCP_TMP++)
516
517 /* Chains of all breakpoints defined. */
518
519 static struct breakpoint *breakpoint_chain;
520
521 /* Breakpoint linked list range. */
522
523 using breakpoint_range = next_adapter<breakpoint, breakpoint_iterator>;
524
525 /* Return a range to iterate over all breakpoints. */
526
527 static breakpoint_range
528 all_breakpoints ()
529 {
530 return breakpoint_range (breakpoint_chain);
531 }
532
533 /* Breakpoint linked list range, safe against deletion of the current
534 breakpoint while iterating. */
535
536 using breakpoint_safe_range = basic_safe_range<breakpoint_range>;
537
538 /* Return a range to iterate over all breakpoints. This range is safe against
539 deletion of the current breakpoint while iterating. */
540
541 static breakpoint_safe_range
542 all_breakpoints_safe ()
543 {
544 return breakpoint_safe_range (all_breakpoints ());
545 }
546
547 /* See breakpoint.h. */
548
549 tracepoint_range
550 all_tracepoints ()
551 {
552 return tracepoint_range (breakpoint_chain);
553 }
554
555 /* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
556
557 static struct bp_location **bp_locations;
558
559 /* Number of elements of BP_LOCATIONS. */
560
561 static unsigned bp_locations_count;
562
563 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
564 ADDRESS for the current elements of BP_LOCATIONS which get a valid
565 result from bp_location_has_shadow. You can use it for roughly
566 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
567 an address you need to read. */
568
569 static CORE_ADDR bp_locations_placed_address_before_address_max;
570
571 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
572 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
573 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
574 You can use it for roughly limiting the subrange of BP_LOCATIONS to
575 scan for shadow bytes for an address you need to read. */
576
577 static CORE_ADDR bp_locations_shadow_len_after_address_max;
578
579 /* The locations that no longer correspond to any breakpoint, unlinked
580 from the bp_locations array, but for which a hit may still be
581 reported by a target. */
582 static std::vector<bp_location *> moribund_locations;
583
584 /* Number of last breakpoint made. */
585
586 static int breakpoint_count;
587
588 /* The value of `breakpoint_count' before the last command that
589 created breakpoints. If the last (break-like) command created more
590 than one breakpoint, then the difference between BREAKPOINT_COUNT
591 and PREV_BREAKPOINT_COUNT is more than one. */
592 static int prev_breakpoint_count;
593
594 /* Number of last tracepoint made. */
595
596 static int tracepoint_count;
597
598 static struct cmd_list_element *breakpoint_set_cmdlist;
599 static struct cmd_list_element *breakpoint_show_cmdlist;
600 struct cmd_list_element *save_cmdlist;
601
602 /* See declaration at breakpoint.h. */
603
604 struct breakpoint *
605 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
606 void *user_data)
607 {
608 for (breakpoint *b : all_breakpoints ())
609 if (func (b, user_data) != 0)
610 return b;
611
612 return nullptr;
613 }
614
615 /* Return whether a breakpoint is an active enabled breakpoint. */
616 static int
617 breakpoint_enabled (struct breakpoint *b)
618 {
619 return (b->enable_state == bp_enabled);
620 }
621
622 /* Set breakpoint count to NUM. */
623
624 static void
625 set_breakpoint_count (int num)
626 {
627 prev_breakpoint_count = breakpoint_count;
628 breakpoint_count = num;
629 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
630 }
631
632 /* Used by `start_rbreak_breakpoints' below, to record the current
633 breakpoint count before "rbreak" creates any breakpoint. */
634 static int rbreak_start_breakpoint_count;
635
636 /* Called at the start an "rbreak" command to record the first
637 breakpoint made. */
638
639 scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
640 {
641 rbreak_start_breakpoint_count = breakpoint_count;
642 }
643
644 /* Called at the end of an "rbreak" command to record the last
645 breakpoint made. */
646
647 scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
648 {
649 prev_breakpoint_count = rbreak_start_breakpoint_count;
650 }
651
652 /* Used in run_command to zero the hit count when a new run starts. */
653
654 void
655 clear_breakpoint_hit_counts (void)
656 {
657 for (breakpoint *b : all_breakpoints ())
658 b->hit_count = 0;
659 }
660
661 \f
662 /* Return the breakpoint with the specified number, or NULL
663 if the number does not refer to an existing breakpoint. */
664
665 struct breakpoint *
666 get_breakpoint (int num)
667 {
668 for (breakpoint *b : all_breakpoints ())
669 if (b->number == num)
670 return b;
671
672 return nullptr;
673 }
674
675 \f
676
677 /* Mark locations as "conditions have changed" in case the target supports
678 evaluating conditions on its side. */
679
680 static void
681 mark_breakpoint_modified (struct breakpoint *b)
682 {
683 /* This is only meaningful if the target is
684 evaluating conditions and if the user has
685 opted for condition evaluation on the target's
686 side. */
687 if (gdb_evaluates_breakpoint_condition_p ()
688 || !target_supports_evaluation_of_breakpoint_conditions ())
689 return;
690
691 if (!is_breakpoint (b))
692 return;
693
694 for (bp_location *loc : b->locations ())
695 loc->condition_changed = condition_modified;
696 }
697
698 /* Mark location as "conditions have changed" in case the target supports
699 evaluating conditions on its side. */
700
701 static void
702 mark_breakpoint_location_modified (struct bp_location *loc)
703 {
704 /* This is only meaningful if the target is
705 evaluating conditions and if the user has
706 opted for condition evaluation on the target's
707 side. */
708 if (gdb_evaluates_breakpoint_condition_p ()
709 || !target_supports_evaluation_of_breakpoint_conditions ())
710
711 return;
712
713 if (!is_breakpoint (loc->owner))
714 return;
715
716 loc->condition_changed = condition_modified;
717 }
718
719 /* Sets the condition-evaluation mode using the static global
720 condition_evaluation_mode. */
721
722 static void
723 set_condition_evaluation_mode (const char *args, int from_tty,
724 struct cmd_list_element *c)
725 {
726 const char *old_mode, *new_mode;
727
728 if ((condition_evaluation_mode_1 == condition_evaluation_target)
729 && !target_supports_evaluation_of_breakpoint_conditions ())
730 {
731 condition_evaluation_mode_1 = condition_evaluation_mode;
732 warning (_("Target does not support breakpoint condition evaluation.\n"
733 "Using host evaluation mode instead."));
734 return;
735 }
736
737 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
738 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
739
740 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
741 settings was "auto". */
742 condition_evaluation_mode = condition_evaluation_mode_1;
743
744 /* Only update the mode if the user picked a different one. */
745 if (new_mode != old_mode)
746 {
747 struct bp_location *loc, **loc_tmp;
748 /* If the user switched to a different evaluation mode, we
749 need to synch the changes with the target as follows:
750
751 "host" -> "target": Send all (valid) conditions to the target.
752 "target" -> "host": Remove all the conditions from the target.
753 */
754
755 if (new_mode == condition_evaluation_target)
756 {
757 /* Mark everything modified and synch conditions with the
758 target. */
759 ALL_BP_LOCATIONS (loc, loc_tmp)
760 mark_breakpoint_location_modified (loc);
761 }
762 else
763 {
764 /* Manually mark non-duplicate locations to synch conditions
765 with the target. We do this to remove all the conditions the
766 target knows about. */
767 ALL_BP_LOCATIONS (loc, loc_tmp)
768 if (is_breakpoint (loc->owner) && loc->inserted)
769 loc->needs_update = 1;
770 }
771
772 /* Do the update. */
773 update_global_location_list (UGLL_MAY_INSERT);
774 }
775
776 return;
777 }
778
779 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
780 what "auto" is translating to. */
781
782 static void
783 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
784 struct cmd_list_element *c, const char *value)
785 {
786 if (condition_evaluation_mode == condition_evaluation_auto)
787 fprintf_filtered (file,
788 _("Breakpoint condition evaluation "
789 "mode is %s (currently %s).\n"),
790 value,
791 breakpoint_condition_evaluation_mode ());
792 else
793 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
794 value);
795 }
796
797 /* A comparison function for bp_location AP and BP that is used by
798 bsearch. This comparison function only cares about addresses, unlike
799 the more general bp_location_is_less_than function. */
800
801 static int
802 bp_locations_compare_addrs (const void *ap, const void *bp)
803 {
804 const struct bp_location *a = *(const struct bp_location **) ap;
805 const struct bp_location *b = *(const struct bp_location **) bp;
806
807 if (a->address == b->address)
808 return 0;
809 else
810 return ((a->address > b->address) - (a->address < b->address));
811 }
812
813 /* Helper function to skip all bp_locations with addresses
814 less than ADDRESS. It returns the first bp_location that
815 is greater than or equal to ADDRESS. If none is found, just
816 return NULL. */
817
818 static struct bp_location **
819 get_first_locp_gte_addr (CORE_ADDR address)
820 {
821 struct bp_location dummy_loc;
822 struct bp_location *dummy_locp = &dummy_loc;
823 struct bp_location **locp_found = NULL;
824
825 /* Initialize the dummy location's address field. */
826 dummy_loc.address = address;
827
828 /* Find a close match to the first location at ADDRESS. */
829 locp_found = ((struct bp_location **)
830 bsearch (&dummy_locp, bp_locations, bp_locations_count,
831 sizeof (struct bp_location **),
832 bp_locations_compare_addrs));
833
834 /* Nothing was found, nothing left to do. */
835 if (locp_found == NULL)
836 return NULL;
837
838 /* We may have found a location that is at ADDRESS but is not the first in the
839 location's list. Go backwards (if possible) and locate the first one. */
840 while ((locp_found - 1) >= bp_locations
841 && (*(locp_found - 1))->address == address)
842 locp_found--;
843
844 return locp_found;
845 }
846
847 /* Parse COND_STRING in the context of LOC and set as the condition
848 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
849 the number of LOC within its owner. In case of parsing error, mark
850 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
851
852 static void
853 set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
854 int bp_num, int loc_num)
855 {
856 bool has_junk = false;
857 try
858 {
859 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
860 block_for_pc (loc->address), 0);
861 if (*cond_string != 0)
862 has_junk = true;
863 else
864 {
865 loc->cond = std::move (new_exp);
866 if (loc->disabled_by_cond && loc->enabled)
867 printf_filtered (_("Breakpoint %d's condition is now valid at "
868 "location %d, enabling.\n"),
869 bp_num, loc_num);
870
871 loc->disabled_by_cond = false;
872 }
873 }
874 catch (const gdb_exception_error &e)
875 {
876 if (loc->enabled)
877 {
878 /* Warn if a user-enabled location is now becoming disabled-by-cond.
879 BP_NUM is 0 if the breakpoint is being defined for the first
880 time using the "break ... if ..." command, and non-zero if
881 already defined. */
882 if (bp_num != 0)
883 warning (_("failed to validate condition at location %d.%d, "
884 "disabling:\n %s"), bp_num, loc_num, e.what ());
885 else
886 warning (_("failed to validate condition at location %d, "
887 "disabling:\n %s"), loc_num, e.what ());
888 }
889
890 loc->disabled_by_cond = true;
891 }
892
893 if (has_junk)
894 error (_("Garbage '%s' follows condition"), cond_string);
895 }
896
897 void
898 set_breakpoint_condition (struct breakpoint *b, const char *exp,
899 int from_tty, bool force)
900 {
901 if (*exp == 0)
902 {
903 xfree (b->cond_string);
904 b->cond_string = nullptr;
905
906 if (is_watchpoint (b))
907 static_cast<watchpoint *> (b)->cond_exp.reset ();
908 else
909 {
910 int loc_num = 1;
911 for (bp_location *loc : b->locations ())
912 {
913 loc->cond.reset ();
914 if (loc->disabled_by_cond && loc->enabled)
915 printf_filtered (_("Breakpoint %d's condition is now valid at "
916 "location %d, enabling.\n"),
917 b->number, loc_num);
918 loc->disabled_by_cond = false;
919 loc_num++;
920
921 /* No need to free the condition agent expression
922 bytecode (if we have one). We will handle this
923 when we go through update_global_location_list. */
924 }
925 }
926
927 if (from_tty)
928 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
929 }
930 else
931 {
932 if (is_watchpoint (b))
933 {
934 innermost_block_tracker tracker;
935 const char *arg = exp;
936 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
937 if (*arg != 0)
938 error (_("Junk at end of expression"));
939 watchpoint *w = static_cast<watchpoint *> (b);
940 w->cond_exp = std::move (new_exp);
941 w->cond_exp_valid_block = tracker.block ();
942 }
943 else
944 {
945 /* Parse and set condition expressions. We make two passes.
946 In the first, we parse the condition string to see if it
947 is valid in at least one location. If so, the condition
948 would be accepted. So we go ahead and set the locations'
949 conditions. In case no valid case is found, we throw
950 the error and the condition string will be rejected.
951 This two-pass approach is taken to avoid setting the
952 state of locations in case of a reject. */
953 for (bp_location *loc : b->locations ())
954 {
955 try
956 {
957 const char *arg = exp;
958 parse_exp_1 (&arg, loc->address,
959 block_for_pc (loc->address), 0);
960 if (*arg != 0)
961 error (_("Junk at end of expression"));
962 break;
963 }
964 catch (const gdb_exception_error &e)
965 {
966 /* Condition string is invalid. If this happens to
967 be the last loc, abandon (if not forced) or continue
968 (if forced). */
969 if (loc->next == nullptr && !force)
970 throw;
971 }
972 }
973
974 /* If we reach here, the condition is valid at some locations. */
975 int loc_num = 1;
976 for (bp_location *loc : b->locations ())
977 {
978 set_breakpoint_location_condition (exp, loc, b->number, loc_num);
979 loc_num++;
980 }
981 }
982
983 /* We know that the new condition parsed successfully. The
984 condition string of the breakpoint can be safely updated. */
985 xfree (b->cond_string);
986 b->cond_string = xstrdup (exp);
987 b->condition_not_parsed = 0;
988 }
989 mark_breakpoint_modified (b);
990
991 gdb::observers::breakpoint_modified.notify (b);
992 }
993
994 /* See breakpoint.h. */
995
996 void
997 set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
998 bool force)
999 {
1000 for (breakpoint *b : all_breakpoints ())
1001 if (b->number == bpnum)
1002 {
1003 /* Check if this breakpoint has a "stop" method implemented in an
1004 extension language. This method and conditions entered into GDB
1005 from the CLI are mutually exclusive. */
1006 const struct extension_language_defn *extlang
1007 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1008
1009 if (extlang != NULL)
1010 {
1011 error (_("Only one stop condition allowed. There is currently"
1012 " a %s stop condition defined for this breakpoint."),
1013 ext_lang_capitalized_name (extlang));
1014 }
1015 set_breakpoint_condition (b, exp, from_tty, force);
1016
1017 if (is_breakpoint (b))
1018 update_global_location_list (UGLL_MAY_INSERT);
1019
1020 return;
1021 }
1022
1023 error (_("No breakpoint number %d."), bpnum);
1024 }
1025
1026 /* The options for the "condition" command. */
1027
1028 struct condition_command_opts
1029 {
1030 /* For "-force". */
1031 bool force_condition = false;
1032 };
1033
1034 static const gdb::option::option_def condition_command_option_defs[] = {
1035
1036 gdb::option::flag_option_def<condition_command_opts> {
1037 "force",
1038 [] (condition_command_opts *opts) { return &opts->force_condition; },
1039 N_("Set the condition even if it is invalid for all current locations."),
1040 },
1041
1042 };
1043
1044 /* Create an option_def_group for the "condition" options, with
1045 CC_OPTS as context. */
1046
1047 static inline gdb::option::option_def_group
1048 make_condition_command_options_def_group (condition_command_opts *cc_opts)
1049 {
1050 return {{condition_command_option_defs}, cc_opts};
1051 }
1052
1053 /* Completion for the "condition" command. */
1054
1055 static void
1056 condition_completer (struct cmd_list_element *cmd,
1057 completion_tracker &tracker,
1058 const char *text, const char * /*word*/)
1059 {
1060 bool has_no_arguments = (*text == '\0');
1061 condition_command_opts cc_opts;
1062 const auto group = make_condition_command_options_def_group (&cc_opts);
1063 if (gdb::option::complete_options
1064 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1065 return;
1066
1067 text = skip_spaces (text);
1068 const char *space = skip_to_space (text);
1069 if (*space == '\0')
1070 {
1071 int len;
1072
1073 if (text[0] == '$')
1074 {
1075 tracker.advance_custom_word_point_by (1);
1076 /* We don't support completion of history indices. */
1077 if (!isdigit (text[1]))
1078 complete_internalvar (tracker, &text[1]);
1079 return;
1080 }
1081
1082 /* Suggest the "-force" flag if no arguments are given. If
1083 arguments were passed, they either already include the flag,
1084 or we are beyond the point of suggesting it because it's
1085 positionally the first argument. */
1086 if (has_no_arguments)
1087 gdb::option::complete_on_all_options (tracker, group);
1088
1089 /* We're completing the breakpoint number. */
1090 len = strlen (text);
1091
1092 for (breakpoint *b : all_breakpoints ())
1093 {
1094 char number[50];
1095
1096 xsnprintf (number, sizeof (number), "%d", b->number);
1097
1098 if (strncmp (number, text, len) == 0)
1099 tracker.add_completion (make_unique_xstrdup (number));
1100 }
1101
1102 return;
1103 }
1104
1105 /* We're completing the expression part. Skip the breakpoint num. */
1106 const char *exp_start = skip_spaces (space);
1107 tracker.advance_custom_word_point_by (exp_start - text);
1108 text = exp_start;
1109 const char *word = advance_to_expression_complete_word_point (tracker, text);
1110 expression_completer (cmd, tracker, text, word);
1111 }
1112
1113 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1114
1115 static void
1116 condition_command (const char *arg, int from_tty)
1117 {
1118 const char *p;
1119 int bnum;
1120
1121 if (arg == 0)
1122 error_no_arg (_("breakpoint number"));
1123
1124 p = arg;
1125
1126 /* Check if the "-force" flag was passed. */
1127 condition_command_opts cc_opts;
1128 const auto group = make_condition_command_options_def_group (&cc_opts);
1129 gdb::option::process_options
1130 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
1131
1132 bnum = get_number (&p);
1133 if (bnum == 0)
1134 error (_("Bad breakpoint argument: '%s'"), arg);
1135
1136 set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
1137 }
1138
1139 /* Check that COMMAND do not contain commands that are suitable
1140 only for tracepoints and not suitable for ordinary breakpoints.
1141 Throw if any such commands is found. */
1142
1143 static void
1144 check_no_tracepoint_commands (struct command_line *commands)
1145 {
1146 struct command_line *c;
1147
1148 for (c = commands; c; c = c->next)
1149 {
1150 if (c->control_type == while_stepping_control)
1151 error (_("The 'while-stepping' command can "
1152 "only be used for tracepoints"));
1153
1154 check_no_tracepoint_commands (c->body_list_0.get ());
1155 check_no_tracepoint_commands (c->body_list_1.get ());
1156
1157 /* Not that command parsing removes leading whitespace and comment
1158 lines and also empty lines. So, we only need to check for
1159 command directly. */
1160 if (strstr (c->line, "collect ") == c->line)
1161 error (_("The 'collect' command can only be used for tracepoints"));
1162
1163 if (strstr (c->line, "teval ") == c->line)
1164 error (_("The 'teval' command can only be used for tracepoints"));
1165 }
1166 }
1167
1168 struct longjmp_breakpoint : public breakpoint
1169 {
1170 ~longjmp_breakpoint () override;
1171 };
1172
1173 /* Encapsulate tests for different types of tracepoints. */
1174
1175 static bool
1176 is_tracepoint_type (bptype type)
1177 {
1178 return (type == bp_tracepoint
1179 || type == bp_fast_tracepoint
1180 || type == bp_static_tracepoint);
1181 }
1182
1183 static bool
1184 is_longjmp_type (bptype type)
1185 {
1186 return type == bp_longjmp || type == bp_exception;
1187 }
1188
1189 /* See breakpoint.h. */
1190
1191 bool
1192 is_tracepoint (const struct breakpoint *b)
1193 {
1194 return is_tracepoint_type (b->type);
1195 }
1196
1197 /* Factory function to create an appropriate instance of breakpoint given
1198 TYPE. */
1199
1200 static std::unique_ptr<breakpoint>
1201 new_breakpoint_from_type (bptype type)
1202 {
1203 breakpoint *b;
1204
1205 if (is_tracepoint_type (type))
1206 b = new tracepoint ();
1207 else if (is_longjmp_type (type))
1208 b = new longjmp_breakpoint ();
1209 else
1210 b = new breakpoint ();
1211
1212 return std::unique_ptr<breakpoint> (b);
1213 }
1214
1215 /* A helper function that validates that COMMANDS are valid for a
1216 breakpoint. This function will throw an exception if a problem is
1217 found. */
1218
1219 static void
1220 validate_commands_for_breakpoint (struct breakpoint *b,
1221 struct command_line *commands)
1222 {
1223 if (is_tracepoint (b))
1224 {
1225 struct tracepoint *t = (struct tracepoint *) b;
1226 struct command_line *c;
1227 struct command_line *while_stepping = 0;
1228
1229 /* Reset the while-stepping step count. The previous commands
1230 might have included a while-stepping action, while the new
1231 ones might not. */
1232 t->step_count = 0;
1233
1234 /* We need to verify that each top-level element of commands is
1235 valid for tracepoints, that there's at most one
1236 while-stepping element, and that the while-stepping's body
1237 has valid tracing commands excluding nested while-stepping.
1238 We also need to validate the tracepoint action line in the
1239 context of the tracepoint --- validate_actionline actually
1240 has side effects, like setting the tracepoint's
1241 while-stepping STEP_COUNT, in addition to checking if the
1242 collect/teval actions parse and make sense in the
1243 tracepoint's context. */
1244 for (c = commands; c; c = c->next)
1245 {
1246 if (c->control_type == while_stepping_control)
1247 {
1248 if (b->type == bp_fast_tracepoint)
1249 error (_("The 'while-stepping' command "
1250 "cannot be used for fast tracepoint"));
1251 else if (b->type == bp_static_tracepoint)
1252 error (_("The 'while-stepping' command "
1253 "cannot be used for static tracepoint"));
1254
1255 if (while_stepping)
1256 error (_("The 'while-stepping' command "
1257 "can be used only once"));
1258 else
1259 while_stepping = c;
1260 }
1261
1262 validate_actionline (c->line, b);
1263 }
1264 if (while_stepping)
1265 {
1266 struct command_line *c2;
1267
1268 gdb_assert (while_stepping->body_list_1 == nullptr);
1269 c2 = while_stepping->body_list_0.get ();
1270 for (; c2; c2 = c2->next)
1271 {
1272 if (c2->control_type == while_stepping_control)
1273 error (_("The 'while-stepping' command cannot be nested"));
1274 }
1275 }
1276 }
1277 else
1278 {
1279 check_no_tracepoint_commands (commands);
1280 }
1281 }
1282
1283 /* Return a vector of all the static tracepoints set at ADDR. The
1284 caller is responsible for releasing the vector. */
1285
1286 std::vector<breakpoint *>
1287 static_tracepoints_here (CORE_ADDR addr)
1288 {
1289 std::vector<breakpoint *> found;
1290
1291 for (breakpoint *b : all_breakpoints ())
1292 if (b->type == bp_static_tracepoint)
1293 {
1294 for (bp_location *loc : b->locations ())
1295 if (loc->address == addr)
1296 found.push_back (b);
1297 }
1298
1299 return found;
1300 }
1301
1302 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1303 validate that only allowed commands are included. */
1304
1305 void
1306 breakpoint_set_commands (struct breakpoint *b,
1307 counted_command_line &&commands)
1308 {
1309 validate_commands_for_breakpoint (b, commands.get ());
1310
1311 b->commands = std::move (commands);
1312 gdb::observers::breakpoint_modified.notify (b);
1313 }
1314
1315 /* Set the internal `silent' flag on the breakpoint. Note that this
1316 is not the same as the "silent" that may appear in the breakpoint's
1317 commands. */
1318
1319 void
1320 breakpoint_set_silent (struct breakpoint *b, int silent)
1321 {
1322 int old_silent = b->silent;
1323
1324 b->silent = silent;
1325 if (old_silent != silent)
1326 gdb::observers::breakpoint_modified.notify (b);
1327 }
1328
1329 /* Set the thread for this breakpoint. If THREAD is -1, make the
1330 breakpoint work for any thread. */
1331
1332 void
1333 breakpoint_set_thread (struct breakpoint *b, int thread)
1334 {
1335 int old_thread = b->thread;
1336
1337 b->thread = thread;
1338 if (old_thread != thread)
1339 gdb::observers::breakpoint_modified.notify (b);
1340 }
1341
1342 /* Set the task for this breakpoint. If TASK is 0, make the
1343 breakpoint work for any task. */
1344
1345 void
1346 breakpoint_set_task (struct breakpoint *b, int task)
1347 {
1348 int old_task = b->task;
1349
1350 b->task = task;
1351 if (old_task != task)
1352 gdb::observers::breakpoint_modified.notify (b);
1353 }
1354
1355 static void
1356 commands_command_1 (const char *arg, int from_tty,
1357 struct command_line *control)
1358 {
1359 counted_command_line cmd;
1360 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1361 NULL after the call to read_command_lines if the user provides an empty
1362 list of command by just typing "end". */
1363 bool cmd_read = false;
1364
1365 std::string new_arg;
1366
1367 if (arg == NULL || !*arg)
1368 {
1369 /* Argument not explicitly given. Synthesize it. */
1370 if (breakpoint_count - prev_breakpoint_count > 1)
1371 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1372 breakpoint_count);
1373 else if (breakpoint_count > 0)
1374 new_arg = string_printf ("%d", breakpoint_count);
1375 }
1376 else
1377 {
1378 /* Create a copy of ARG. This is needed because the "commands"
1379 command may be coming from a script. In that case, the read
1380 line buffer is going to be overwritten in the lambda of
1381 'map_breakpoint_numbers' below when reading the next line
1382 before we are are done parsing the breakpoint numbers. */
1383 new_arg = arg;
1384 }
1385 arg = new_arg.c_str ();
1386
1387 map_breakpoint_numbers
1388 (arg, [&] (breakpoint *b)
1389 {
1390 if (!cmd_read)
1391 {
1392 gdb_assert (cmd == NULL);
1393 if (control != NULL)
1394 cmd = control->body_list_0;
1395 else
1396 {
1397 std::string str
1398 = string_printf (_("Type commands for breakpoint(s) "
1399 "%s, one per line."),
1400 arg);
1401
1402 auto do_validate = [=] (const char *line)
1403 {
1404 validate_actionline (line, b);
1405 };
1406 gdb::function_view<void (const char *)> validator;
1407 if (is_tracepoint (b))
1408 validator = do_validate;
1409
1410 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1411 }
1412 cmd_read = true;
1413 }
1414
1415 /* If a breakpoint was on the list more than once, we don't need to
1416 do anything. */
1417 if (b->commands != cmd)
1418 {
1419 validate_commands_for_breakpoint (b, cmd.get ());
1420 b->commands = cmd;
1421 gdb::observers::breakpoint_modified.notify (b);
1422 }
1423 });
1424 }
1425
1426 static void
1427 commands_command (const char *arg, int from_tty)
1428 {
1429 commands_command_1 (arg, from_tty, NULL);
1430 }
1431
1432 /* Like commands_command, but instead of reading the commands from
1433 input stream, takes them from an already parsed command structure.
1434
1435 This is used by cli-script.c to DTRT with breakpoint commands
1436 that are part of if and while bodies. */
1437 enum command_control_type
1438 commands_from_control_command (const char *arg, struct command_line *cmd)
1439 {
1440 commands_command_1 (arg, 0, cmd);
1441 return simple_control;
1442 }
1443
1444 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1445
1446 static int
1447 bp_location_has_shadow (struct bp_location *bl)
1448 {
1449 if (bl->loc_type != bp_loc_software_breakpoint)
1450 return 0;
1451 if (!bl->inserted)
1452 return 0;
1453 if (bl->target_info.shadow_len == 0)
1454 /* BL isn't valid, or doesn't shadow memory. */
1455 return 0;
1456 return 1;
1457 }
1458
1459 /* Update BUF, which is LEN bytes read from the target address
1460 MEMADDR, by replacing a memory breakpoint with its shadowed
1461 contents.
1462
1463 If READBUF is not NULL, this buffer must not overlap with the of
1464 the breakpoint location's shadow_contents buffer. Otherwise, a
1465 failed assertion internal error will be raised. */
1466
1467 static void
1468 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1469 const gdb_byte *writebuf_org,
1470 ULONGEST memaddr, LONGEST len,
1471 struct bp_target_info *target_info,
1472 struct gdbarch *gdbarch)
1473 {
1474 /* Now do full processing of the found relevant range of elements. */
1475 CORE_ADDR bp_addr = 0;
1476 int bp_size = 0;
1477 int bptoffset = 0;
1478
1479 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1480 current_program_space->aspace, 0))
1481 {
1482 /* The breakpoint is inserted in a different address space. */
1483 return;
1484 }
1485
1486 /* Addresses and length of the part of the breakpoint that
1487 we need to copy. */
1488 bp_addr = target_info->placed_address;
1489 bp_size = target_info->shadow_len;
1490
1491 if (bp_addr + bp_size <= memaddr)
1492 {
1493 /* The breakpoint is entirely before the chunk of memory we are
1494 reading. */
1495 return;
1496 }
1497
1498 if (bp_addr >= memaddr + len)
1499 {
1500 /* The breakpoint is entirely after the chunk of memory we are
1501 reading. */
1502 return;
1503 }
1504
1505 /* Offset within shadow_contents. */
1506 if (bp_addr < memaddr)
1507 {
1508 /* Only copy the second part of the breakpoint. */
1509 bp_size -= memaddr - bp_addr;
1510 bptoffset = memaddr - bp_addr;
1511 bp_addr = memaddr;
1512 }
1513
1514 if (bp_addr + bp_size > memaddr + len)
1515 {
1516 /* Only copy the first part of the breakpoint. */
1517 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1518 }
1519
1520 if (readbuf != NULL)
1521 {
1522 /* Verify that the readbuf buffer does not overlap with the
1523 shadow_contents buffer. */
1524 gdb_assert (target_info->shadow_contents >= readbuf + len
1525 || readbuf >= (target_info->shadow_contents
1526 + target_info->shadow_len));
1527
1528 /* Update the read buffer with this inserted breakpoint's
1529 shadow. */
1530 memcpy (readbuf + bp_addr - memaddr,
1531 target_info->shadow_contents + bptoffset, bp_size);
1532 }
1533 else
1534 {
1535 const unsigned char *bp;
1536 CORE_ADDR addr = target_info->reqstd_address;
1537 int placed_size;
1538
1539 /* Update the shadow with what we want to write to memory. */
1540 memcpy (target_info->shadow_contents + bptoffset,
1541 writebuf_org + bp_addr - memaddr, bp_size);
1542
1543 /* Determine appropriate breakpoint contents and size for this
1544 address. */
1545 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1546
1547 /* Update the final write buffer with this inserted
1548 breakpoint's INSN. */
1549 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1550 }
1551 }
1552
1553 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1554 by replacing any memory breakpoints with their shadowed contents.
1555
1556 If READBUF is not NULL, this buffer must not overlap with any of
1557 the breakpoint location's shadow_contents buffers. Otherwise,
1558 a failed assertion internal error will be raised.
1559
1560 The range of shadowed area by each bp_location is:
1561 bl->address - bp_locations_placed_address_before_address_max
1562 up to bl->address + bp_locations_shadow_len_after_address_max
1563 The range we were requested to resolve shadows for is:
1564 memaddr ... memaddr + len
1565 Thus the safe cutoff boundaries for performance optimization are
1566 memaddr + len <= (bl->address
1567 - bp_locations_placed_address_before_address_max)
1568 and:
1569 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1570
1571 void
1572 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1573 const gdb_byte *writebuf_org,
1574 ULONGEST memaddr, LONGEST len)
1575 {
1576 /* Left boundary, right boundary and median element of our binary
1577 search. */
1578 unsigned bc_l, bc_r, bc;
1579
1580 /* Find BC_L which is a leftmost element which may affect BUF
1581 content. It is safe to report lower value but a failure to
1582 report higher one. */
1583
1584 bc_l = 0;
1585 bc_r = bp_locations_count;
1586 while (bc_l + 1 < bc_r)
1587 {
1588 struct bp_location *bl;
1589
1590 bc = (bc_l + bc_r) / 2;
1591 bl = bp_locations[bc];
1592
1593 /* Check first BL->ADDRESS will not overflow due to the added
1594 constant. Then advance the left boundary only if we are sure
1595 the BC element can in no way affect the BUF content (MEMADDR
1596 to MEMADDR + LEN range).
1597
1598 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1599 offset so that we cannot miss a breakpoint with its shadow
1600 range tail still reaching MEMADDR. */
1601
1602 if ((bl->address + bp_locations_shadow_len_after_address_max
1603 >= bl->address)
1604 && (bl->address + bp_locations_shadow_len_after_address_max
1605 <= memaddr))
1606 bc_l = bc;
1607 else
1608 bc_r = bc;
1609 }
1610
1611 /* Due to the binary search above, we need to make sure we pick the
1612 first location that's at BC_L's address. E.g., if there are
1613 multiple locations at the same address, BC_L may end up pointing
1614 at a duplicate location, and miss the "master"/"inserted"
1615 location. Say, given locations L1, L2 and L3 at addresses A and
1616 B:
1617
1618 L1@A, L2@A, L3@B, ...
1619
1620 BC_L could end up pointing at location L2, while the "master"
1621 location could be L1. Since the `loc->inserted' flag is only set
1622 on "master" locations, we'd forget to restore the shadow of L1
1623 and L2. */
1624 while (bc_l > 0
1625 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1626 bc_l--;
1627
1628 /* Now do full processing of the found relevant range of elements. */
1629
1630 for (bc = bc_l; bc < bp_locations_count; bc++)
1631 {
1632 struct bp_location *bl = bp_locations[bc];
1633
1634 /* bp_location array has BL->OWNER always non-NULL. */
1635 if (bl->owner->type == bp_none)
1636 warning (_("reading through apparently deleted breakpoint #%d?"),
1637 bl->owner->number);
1638
1639 /* Performance optimization: any further element can no longer affect BUF
1640 content. */
1641
1642 if (bl->address >= bp_locations_placed_address_before_address_max
1643 && memaddr + len <= (bl->address
1644 - bp_locations_placed_address_before_address_max))
1645 break;
1646
1647 if (!bp_location_has_shadow (bl))
1648 continue;
1649
1650 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1651 memaddr, len, &bl->target_info, bl->gdbarch);
1652 }
1653 }
1654
1655 /* See breakpoint.h. */
1656
1657 bool
1658 is_breakpoint (const struct breakpoint *bpt)
1659 {
1660 return (bpt->type == bp_breakpoint
1661 || bpt->type == bp_hardware_breakpoint
1662 || bpt->type == bp_dprintf);
1663 }
1664
1665 /* Return true if BPT is of any hardware watchpoint kind. */
1666
1667 static bool
1668 is_hardware_watchpoint (const struct breakpoint *bpt)
1669 {
1670 return (bpt->type == bp_hardware_watchpoint
1671 || bpt->type == bp_read_watchpoint
1672 || bpt->type == bp_access_watchpoint);
1673 }
1674
1675 /* See breakpoint.h. */
1676
1677 bool
1678 is_watchpoint (const struct breakpoint *bpt)
1679 {
1680 return (is_hardware_watchpoint (bpt)
1681 || bpt->type == bp_watchpoint);
1682 }
1683
1684 /* Returns true if the current thread and its running state are safe
1685 to evaluate or update watchpoint B. Watchpoints on local
1686 expressions need to be evaluated in the context of the thread that
1687 was current when the watchpoint was created, and, that thread needs
1688 to be stopped to be able to select the correct frame context.
1689 Watchpoints on global expressions can be evaluated on any thread,
1690 and in any state. It is presently left to the target allowing
1691 memory accesses when threads are running. */
1692
1693 static int
1694 watchpoint_in_thread_scope (struct watchpoint *b)
1695 {
1696 return (b->pspace == current_program_space
1697 && (b->watchpoint_thread == null_ptid
1698 || (inferior_ptid == b->watchpoint_thread
1699 && !inferior_thread ()->executing)));
1700 }
1701
1702 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1703 associated bp_watchpoint_scope breakpoint. */
1704
1705 static void
1706 watchpoint_del_at_next_stop (struct watchpoint *w)
1707 {
1708 if (w->related_breakpoint != w)
1709 {
1710 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1711 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1712 w->related_breakpoint->disposition = disp_del_at_next_stop;
1713 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1714 w->related_breakpoint = w;
1715 }
1716 w->disposition = disp_del_at_next_stop;
1717 }
1718
1719 /* Extract a bitfield value from value VAL using the bit parameters contained in
1720 watchpoint W. */
1721
1722 static struct value *
1723 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1724 {
1725 struct value *bit_val;
1726
1727 if (val == NULL)
1728 return NULL;
1729
1730 bit_val = allocate_value (value_type (val));
1731
1732 unpack_value_bitfield (bit_val,
1733 w->val_bitpos,
1734 w->val_bitsize,
1735 value_contents_for_printing (val),
1736 value_offset (val),
1737 val);
1738
1739 return bit_val;
1740 }
1741
1742 /* Allocate a dummy location and add it to B, which must be a software
1743 watchpoint. This is required because even if a software watchpoint
1744 is not watching any memory, bpstat_stop_status requires a location
1745 to be able to report stops. */
1746
1747 static void
1748 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1749 struct program_space *pspace)
1750 {
1751 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1752
1753 b->loc = allocate_bp_location (b);
1754 b->loc->pspace = pspace;
1755 b->loc->address = -1;
1756 b->loc->length = -1;
1757 }
1758
1759 /* Returns true if B is a software watchpoint that is not watching any
1760 memory (e.g., "watch $pc"). */
1761
1762 static bool
1763 is_no_memory_software_watchpoint (struct breakpoint *b)
1764 {
1765 return (b->type == bp_watchpoint
1766 && b->loc != NULL
1767 && b->loc->next == NULL
1768 && b->loc->address == -1
1769 && b->loc->length == -1);
1770 }
1771
1772 /* Assuming that B is a watchpoint:
1773 - Reparse watchpoint expression, if REPARSE is non-zero
1774 - Evaluate expression and store the result in B->val
1775 - Evaluate the condition if there is one, and store the result
1776 in b->loc->cond.
1777 - Update the list of values that must be watched in B->loc.
1778
1779 If the watchpoint disposition is disp_del_at_next_stop, then do
1780 nothing. If this is local watchpoint that is out of scope, delete
1781 it.
1782
1783 Even with `set breakpoint always-inserted on' the watchpoints are
1784 removed + inserted on each stop here. Normal breakpoints must
1785 never be removed because they might be missed by a running thread
1786 when debugging in non-stop mode. On the other hand, hardware
1787 watchpoints (is_hardware_watchpoint; processed here) are specific
1788 to each LWP since they are stored in each LWP's hardware debug
1789 registers. Therefore, such LWP must be stopped first in order to
1790 be able to modify its hardware watchpoints.
1791
1792 Hardware watchpoints must be reset exactly once after being
1793 presented to the user. It cannot be done sooner, because it would
1794 reset the data used to present the watchpoint hit to the user. And
1795 it must not be done later because it could display the same single
1796 watchpoint hit during multiple GDB stops. Note that the latter is
1797 relevant only to the hardware watchpoint types bp_read_watchpoint
1798 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1799 not user-visible - its hit is suppressed if the memory content has
1800 not changed.
1801
1802 The following constraints influence the location where we can reset
1803 hardware watchpoints:
1804
1805 * target_stopped_by_watchpoint and target_stopped_data_address are
1806 called several times when GDB stops.
1807
1808 [linux]
1809 * Multiple hardware watchpoints can be hit at the same time,
1810 causing GDB to stop. GDB only presents one hardware watchpoint
1811 hit at a time as the reason for stopping, and all the other hits
1812 are presented later, one after the other, each time the user
1813 requests the execution to be resumed. Execution is not resumed
1814 for the threads still having pending hit event stored in
1815 LWP_INFO->STATUS. While the watchpoint is already removed from
1816 the inferior on the first stop the thread hit event is kept being
1817 reported from its cached value by linux_nat_stopped_data_address
1818 until the real thread resume happens after the watchpoint gets
1819 presented and thus its LWP_INFO->STATUS gets reset.
1820
1821 Therefore the hardware watchpoint hit can get safely reset on the
1822 watchpoint removal from inferior. */
1823
1824 static void
1825 update_watchpoint (struct watchpoint *b, int reparse)
1826 {
1827 int within_current_scope;
1828 struct frame_id saved_frame_id;
1829 int frame_saved;
1830
1831 /* If this is a local watchpoint, we only want to check if the
1832 watchpoint frame is in scope if the current thread is the thread
1833 that was used to create the watchpoint. */
1834 if (!watchpoint_in_thread_scope (b))
1835 return;
1836
1837 if (b->disposition == disp_del_at_next_stop)
1838 return;
1839
1840 frame_saved = 0;
1841
1842 /* Determine if the watchpoint is within scope. */
1843 if (b->exp_valid_block == NULL)
1844 within_current_scope = 1;
1845 else
1846 {
1847 struct frame_info *fi = get_current_frame ();
1848 struct gdbarch *frame_arch = get_frame_arch (fi);
1849 CORE_ADDR frame_pc = get_frame_pc (fi);
1850
1851 /* If we're at a point where the stack has been destroyed
1852 (e.g. in a function epilogue), unwinding may not work
1853 properly. Do not attempt to recreate locations at this
1854 point. See similar comments in watchpoint_check. */
1855 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1856 return;
1857
1858 /* Save the current frame's ID so we can restore it after
1859 evaluating the watchpoint expression on its own frame. */
1860 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1861 took a frame parameter, so that we didn't have to change the
1862 selected frame. */
1863 frame_saved = 1;
1864 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1865
1866 fi = frame_find_by_id (b->watchpoint_frame);
1867 within_current_scope = (fi != NULL);
1868 if (within_current_scope)
1869 select_frame (fi);
1870 }
1871
1872 /* We don't free locations. They are stored in the bp_location array
1873 and update_global_location_list will eventually delete them and
1874 remove breakpoints if needed. */
1875 b->loc = NULL;
1876
1877 if (within_current_scope && reparse)
1878 {
1879 const char *s;
1880
1881 b->exp.reset ();
1882 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1883 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1884 /* If the meaning of expression itself changed, the old value is
1885 no longer relevant. We don't want to report a watchpoint hit
1886 to the user when the old value and the new value may actually
1887 be completely different objects. */
1888 b->val = NULL;
1889 b->val_valid = false;
1890
1891 /* Note that unlike with breakpoints, the watchpoint's condition
1892 expression is stored in the breakpoint object, not in the
1893 locations (re)created below. */
1894 if (b->cond_string != NULL)
1895 {
1896 b->cond_exp.reset ();
1897
1898 s = b->cond_string;
1899 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1900 }
1901 }
1902
1903 /* If we failed to parse the expression, for example because
1904 it refers to a global variable in a not-yet-loaded shared library,
1905 don't try to insert watchpoint. We don't automatically delete
1906 such watchpoint, though, since failure to parse expression
1907 is different from out-of-scope watchpoint. */
1908 if (!target_has_execution ())
1909 {
1910 /* Without execution, memory can't change. No use to try and
1911 set watchpoint locations. The watchpoint will be reset when
1912 the target gains execution, through breakpoint_re_set. */
1913 if (!can_use_hw_watchpoints)
1914 {
1915 if (b->ops->works_in_software_mode (b))
1916 b->type = bp_watchpoint;
1917 else
1918 error (_("Can't set read/access watchpoint when "
1919 "hardware watchpoints are disabled."));
1920 }
1921 }
1922 else if (within_current_scope && b->exp)
1923 {
1924 std::vector<value_ref_ptr> val_chain;
1925 struct value *v, *result;
1926 struct program_space *frame_pspace;
1927
1928 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
1929 &val_chain, false);
1930
1931 /* Avoid setting b->val if it's already set. The meaning of
1932 b->val is 'the last value' user saw, and we should update
1933 it only if we reported that last value to user. As it
1934 happens, the code that reports it updates b->val directly.
1935 We don't keep track of the memory value for masked
1936 watchpoints. */
1937 if (!b->val_valid && !is_masked_watchpoint (b))
1938 {
1939 if (b->val_bitsize != 0)
1940 v = extract_bitfield_from_watchpoint_value (b, v);
1941 b->val = release_value (v);
1942 b->val_valid = true;
1943 }
1944
1945 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1946
1947 /* Look at each value on the value chain. */
1948 gdb_assert (!val_chain.empty ());
1949 for (const value_ref_ptr &iter : val_chain)
1950 {
1951 v = iter.get ();
1952
1953 /* If it's a memory location, and GDB actually needed
1954 its contents to evaluate the expression, then we
1955 must watch it. If the first value returned is
1956 still lazy, that means an error occurred reading it;
1957 watch it anyway in case it becomes readable. */
1958 if (VALUE_LVAL (v) == lval_memory
1959 && (v == val_chain[0] || ! value_lazy (v)))
1960 {
1961 struct type *vtype = check_typedef (value_type (v));
1962
1963 /* We only watch structs and arrays if user asked
1964 for it explicitly, never if they just happen to
1965 appear in the middle of some value chain. */
1966 if (v == result
1967 || (vtype->code () != TYPE_CODE_STRUCT
1968 && vtype->code () != TYPE_CODE_ARRAY))
1969 {
1970 CORE_ADDR addr;
1971 enum target_hw_bp_type type;
1972 struct bp_location *loc, **tmp;
1973 int bitpos = 0, bitsize = 0;
1974
1975 if (value_bitsize (v) != 0)
1976 {
1977 /* Extract the bit parameters out from the bitfield
1978 sub-expression. */
1979 bitpos = value_bitpos (v);
1980 bitsize = value_bitsize (v);
1981 }
1982 else if (v == result && b->val_bitsize != 0)
1983 {
1984 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1985 lvalue whose bit parameters are saved in the fields
1986 VAL_BITPOS and VAL_BITSIZE. */
1987 bitpos = b->val_bitpos;
1988 bitsize = b->val_bitsize;
1989 }
1990
1991 addr = value_address (v);
1992 if (bitsize != 0)
1993 {
1994 /* Skip the bytes that don't contain the bitfield. */
1995 addr += bitpos / 8;
1996 }
1997
1998 type = hw_write;
1999 if (b->type == bp_read_watchpoint)
2000 type = hw_read;
2001 else if (b->type == bp_access_watchpoint)
2002 type = hw_access;
2003
2004 loc = allocate_bp_location (b);
2005 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
2006 ;
2007 *tmp = loc;
2008 loc->gdbarch = value_type (v)->arch ();
2009
2010 loc->pspace = frame_pspace;
2011 loc->address = address_significant (loc->gdbarch, addr);
2012
2013 if (bitsize != 0)
2014 {
2015 /* Just cover the bytes that make up the bitfield. */
2016 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2017 }
2018 else
2019 loc->length = TYPE_LENGTH (value_type (v));
2020
2021 loc->watchpoint_type = type;
2022 }
2023 }
2024 }
2025
2026 /* Change the type of breakpoint between hardware assisted or
2027 an ordinary watchpoint depending on the hardware support
2028 and free hardware slots. REPARSE is set when the inferior
2029 is started. */
2030 if (reparse)
2031 {
2032 int reg_cnt;
2033 enum bp_loc_type loc_type;
2034
2035 reg_cnt = can_use_hardware_watchpoint (val_chain);
2036
2037 if (reg_cnt)
2038 {
2039 int i, target_resources_ok, other_type_used;
2040 enum bptype type;
2041
2042 /* Use an exact watchpoint when there's only one memory region to be
2043 watched, and only one debug register is needed to watch it. */
2044 b->exact = target_exact_watchpoints && reg_cnt == 1;
2045
2046 /* We need to determine how many resources are already
2047 used for all other hardware watchpoints plus this one
2048 to see if we still have enough resources to also fit
2049 this watchpoint in as well. */
2050
2051 /* If this is a software watchpoint, we try to turn it
2052 to a hardware one -- count resources as if B was of
2053 hardware watchpoint type. */
2054 type = b->type;
2055 if (type == bp_watchpoint)
2056 type = bp_hardware_watchpoint;
2057
2058 /* This watchpoint may or may not have been placed on
2059 the list yet at this point (it won't be in the list
2060 if we're trying to create it for the first time,
2061 through watch_command), so always account for it
2062 manually. */
2063
2064 /* Count resources used by all watchpoints except B. */
2065 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
2066
2067 /* Add in the resources needed for B. */
2068 i += hw_watchpoint_use_count (b);
2069
2070 target_resources_ok
2071 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2072 if (target_resources_ok <= 0)
2073 {
2074 int sw_mode = b->ops->works_in_software_mode (b);
2075
2076 if (target_resources_ok == 0 && !sw_mode)
2077 error (_("Target does not support this type of "
2078 "hardware watchpoint."));
2079 else if (target_resources_ok < 0 && !sw_mode)
2080 error (_("There are not enough available hardware "
2081 "resources for this watchpoint."));
2082
2083 /* Downgrade to software watchpoint. */
2084 b->type = bp_watchpoint;
2085 }
2086 else
2087 {
2088 /* If this was a software watchpoint, we've just
2089 found we have enough resources to turn it to a
2090 hardware watchpoint. Otherwise, this is a
2091 nop. */
2092 b->type = type;
2093 }
2094 }
2095 else if (!b->ops->works_in_software_mode (b))
2096 {
2097 if (!can_use_hw_watchpoints)
2098 error (_("Can't set read/access watchpoint when "
2099 "hardware watchpoints are disabled."));
2100 else
2101 error (_("Expression cannot be implemented with "
2102 "read/access watchpoint."));
2103 }
2104 else
2105 b->type = bp_watchpoint;
2106
2107 loc_type = (b->type == bp_watchpoint? bp_loc_other
2108 : bp_loc_hardware_watchpoint);
2109 for (bp_location *bl : b->locations ())
2110 bl->loc_type = loc_type;
2111 }
2112
2113 /* If a software watchpoint is not watching any memory, then the
2114 above left it without any location set up. But,
2115 bpstat_stop_status requires a location to be able to report
2116 stops, so make sure there's at least a dummy one. */
2117 if (b->type == bp_watchpoint && b->loc == NULL)
2118 software_watchpoint_add_no_memory_location (b, frame_pspace);
2119 }
2120 else if (!within_current_scope)
2121 {
2122 printf_filtered (_("\
2123 Watchpoint %d deleted because the program has left the block\n\
2124 in which its expression is valid.\n"),
2125 b->number);
2126 watchpoint_del_at_next_stop (b);
2127 }
2128
2129 /* Restore the selected frame. */
2130 if (frame_saved)
2131 select_frame (frame_find_by_id (saved_frame_id));
2132 }
2133
2134
2135 /* Returns 1 iff breakpoint location should be
2136 inserted in the inferior. We don't differentiate the type of BL's owner
2137 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2138 breakpoint_ops is not defined, because in insert_bp_location,
2139 tracepoint's insert_location will not be called. */
2140 static int
2141 should_be_inserted (struct bp_location *bl)
2142 {
2143 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2144 return 0;
2145
2146 if (bl->owner->disposition == disp_del_at_next_stop)
2147 return 0;
2148
2149 if (!bl->enabled || bl->disabled_by_cond
2150 || bl->shlib_disabled || bl->duplicate)
2151 return 0;
2152
2153 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2154 return 0;
2155
2156 /* This is set for example, when we're attached to the parent of a
2157 vfork, and have detached from the child. The child is running
2158 free, and we expect it to do an exec or exit, at which point the
2159 OS makes the parent schedulable again (and the target reports
2160 that the vfork is done). Until the child is done with the shared
2161 memory region, do not insert breakpoints in the parent, otherwise
2162 the child could still trip on the parent's breakpoints. Since
2163 the parent is blocked anyway, it won't miss any breakpoint. */
2164 if (bl->pspace->breakpoints_not_allowed)
2165 return 0;
2166
2167 /* Don't insert a breakpoint if we're trying to step past its
2168 location, except if the breakpoint is a single-step breakpoint,
2169 and the breakpoint's thread is the thread which is stepping past
2170 a breakpoint. */
2171 if ((bl->loc_type == bp_loc_software_breakpoint
2172 || bl->loc_type == bp_loc_hardware_breakpoint)
2173 && stepping_past_instruction_at (bl->pspace->aspace,
2174 bl->address)
2175 /* The single-step breakpoint may be inserted at the location
2176 we're trying to step if the instruction branches to itself.
2177 However, the instruction won't be executed at all and it may
2178 break the semantics of the instruction, for example, the
2179 instruction is a conditional branch or updates some flags.
2180 We can't fix it unless GDB is able to emulate the instruction
2181 or switch to displaced stepping. */
2182 && !(bl->owner->type == bp_single_step
2183 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2184 {
2185 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2186 paddress (bl->gdbarch, bl->address));
2187 return 0;
2188 }
2189
2190 /* Don't insert watchpoints if we're trying to step past the
2191 instruction that triggered one. */
2192 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2193 && stepping_past_nonsteppable_watchpoint ())
2194 {
2195 infrun_debug_printf ("stepping past non-steppable watchpoint. "
2196 "skipping watchpoint at %s:%d",
2197 paddress (bl->gdbarch, bl->address), bl->length);
2198 return 0;
2199 }
2200
2201 return 1;
2202 }
2203
2204 /* Same as should_be_inserted but does the check assuming
2205 that the location is not duplicated. */
2206
2207 static int
2208 unduplicated_should_be_inserted (struct bp_location *bl)
2209 {
2210 int result;
2211 const int save_duplicate = bl->duplicate;
2212
2213 bl->duplicate = 0;
2214 result = should_be_inserted (bl);
2215 bl->duplicate = save_duplicate;
2216 return result;
2217 }
2218
2219 /* Parses a conditional described by an expression COND into an
2220 agent expression bytecode suitable for evaluation
2221 by the bytecode interpreter. Return NULL if there was
2222 any error during parsing. */
2223
2224 static agent_expr_up
2225 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2226 {
2227 if (cond == NULL)
2228 return NULL;
2229
2230 agent_expr_up aexpr;
2231
2232 /* We don't want to stop processing, so catch any errors
2233 that may show up. */
2234 try
2235 {
2236 aexpr = gen_eval_for_expr (scope, cond);
2237 }
2238
2239 catch (const gdb_exception_error &ex)
2240 {
2241 /* If we got here, it means the condition could not be parsed to a valid
2242 bytecode expression and thus can't be evaluated on the target's side.
2243 It's no use iterating through the conditions. */
2244 }
2245
2246 /* We have a valid agent expression. */
2247 return aexpr;
2248 }
2249
2250 /* Based on location BL, create a list of breakpoint conditions to be
2251 passed on to the target. If we have duplicated locations with different
2252 conditions, we will add such conditions to the list. The idea is that the
2253 target will evaluate the list of conditions and will only notify GDB when
2254 one of them is true. */
2255
2256 static void
2257 build_target_condition_list (struct bp_location *bl)
2258 {
2259 struct bp_location **locp = NULL, **loc2p;
2260 int null_condition_or_parse_error = 0;
2261 int modified = bl->needs_update;
2262 struct bp_location *loc;
2263
2264 /* Release conditions left over from a previous insert. */
2265 bl->target_info.conditions.clear ();
2266
2267 /* This is only meaningful if the target is
2268 evaluating conditions and if the user has
2269 opted for condition evaluation on the target's
2270 side. */
2271 if (gdb_evaluates_breakpoint_condition_p ()
2272 || !target_supports_evaluation_of_breakpoint_conditions ())
2273 return;
2274
2275 /* Do a first pass to check for locations with no assigned
2276 conditions or conditions that fail to parse to a valid agent
2277 expression bytecode. If any of these happen, then it's no use to
2278 send conditions to the target since this location will always
2279 trigger and generate a response back to GDB. Note we consider
2280 all locations at the same address irrespective of type, i.e.,
2281 even if the locations aren't considered duplicates (e.g.,
2282 software breakpoint and hardware breakpoint at the same
2283 address). */
2284 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2285 {
2286 loc = (*loc2p);
2287 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2288 {
2289 if (modified)
2290 {
2291 /* Re-parse the conditions since something changed. In that
2292 case we already freed the condition bytecodes (see
2293 force_breakpoint_reinsertion). We just
2294 need to parse the condition to bytecodes again. */
2295 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2296 loc->cond.get ());
2297 }
2298
2299 /* If we have a NULL bytecode expression, it means something
2300 went wrong or we have a null condition expression. */
2301 if (!loc->cond_bytecode)
2302 {
2303 null_condition_or_parse_error = 1;
2304 break;
2305 }
2306 }
2307 }
2308
2309 /* If any of these happened, it means we will have to evaluate the conditions
2310 for the location's address on gdb's side. It is no use keeping bytecodes
2311 for all the other duplicate locations, thus we free all of them here.
2312
2313 This is so we have a finer control over which locations' conditions are
2314 being evaluated by GDB or the remote stub. */
2315 if (null_condition_or_parse_error)
2316 {
2317 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2318 {
2319 loc = (*loc2p);
2320 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2321 {
2322 /* Only go as far as the first NULL bytecode is
2323 located. */
2324 if (!loc->cond_bytecode)
2325 return;
2326
2327 loc->cond_bytecode.reset ();
2328 }
2329 }
2330 }
2331
2332 /* No NULL conditions or failed bytecode generation. Build a
2333 condition list for this location's address. If we have software
2334 and hardware locations at the same address, they aren't
2335 considered duplicates, but we still marge all the conditions
2336 anyway, as it's simpler, and doesn't really make a practical
2337 difference. */
2338 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2339 {
2340 loc = (*loc2p);
2341 if (loc->cond
2342 && is_breakpoint (loc->owner)
2343 && loc->pspace->num == bl->pspace->num
2344 && loc->owner->enable_state == bp_enabled
2345 && loc->enabled
2346 && !loc->disabled_by_cond)
2347 {
2348 /* Add the condition to the vector. This will be used later
2349 to send the conditions to the target. */
2350 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2351 }
2352 }
2353
2354 return;
2355 }
2356
2357 /* Parses a command described by string CMD into an agent expression
2358 bytecode suitable for evaluation by the bytecode interpreter.
2359 Return NULL if there was any error during parsing. */
2360
2361 static agent_expr_up
2362 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2363 {
2364 const char *cmdrest;
2365 const char *format_start, *format_end;
2366 struct gdbarch *gdbarch = get_current_arch ();
2367
2368 if (cmd == NULL)
2369 return NULL;
2370
2371 cmdrest = cmd;
2372
2373 if (*cmdrest == ',')
2374 ++cmdrest;
2375 cmdrest = skip_spaces (cmdrest);
2376
2377 if (*cmdrest++ != '"')
2378 error (_("No format string following the location"));
2379
2380 format_start = cmdrest;
2381
2382 format_pieces fpieces (&cmdrest);
2383
2384 format_end = cmdrest;
2385
2386 if (*cmdrest++ != '"')
2387 error (_("Bad format string, non-terminated '\"'."));
2388
2389 cmdrest = skip_spaces (cmdrest);
2390
2391 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2392 error (_("Invalid argument syntax"));
2393
2394 if (*cmdrest == ',')
2395 cmdrest++;
2396 cmdrest = skip_spaces (cmdrest);
2397
2398 /* For each argument, make an expression. */
2399
2400 std::vector<struct expression *> argvec;
2401 while (*cmdrest != '\0')
2402 {
2403 const char *cmd1;
2404
2405 cmd1 = cmdrest;
2406 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2407 argvec.push_back (expr.release ());
2408 cmdrest = cmd1;
2409 if (*cmdrest == ',')
2410 ++cmdrest;
2411 }
2412
2413 agent_expr_up aexpr;
2414
2415 /* We don't want to stop processing, so catch any errors
2416 that may show up. */
2417 try
2418 {
2419 aexpr = gen_printf (scope, gdbarch, 0, 0,
2420 format_start, format_end - format_start,
2421 argvec.size (), argvec.data ());
2422 }
2423 catch (const gdb_exception_error &ex)
2424 {
2425 /* If we got here, it means the command could not be parsed to a valid
2426 bytecode expression and thus can't be evaluated on the target's side.
2427 It's no use iterating through the other commands. */
2428 }
2429
2430 /* We have a valid agent expression, return it. */
2431 return aexpr;
2432 }
2433
2434 /* Based on location BL, create a list of breakpoint commands to be
2435 passed on to the target. If we have duplicated locations with
2436 different commands, we will add any such to the list. */
2437
2438 static void
2439 build_target_command_list (struct bp_location *bl)
2440 {
2441 struct bp_location **locp = NULL, **loc2p;
2442 int null_command_or_parse_error = 0;
2443 int modified = bl->needs_update;
2444 struct bp_location *loc;
2445
2446 /* Clear commands left over from a previous insert. */
2447 bl->target_info.tcommands.clear ();
2448
2449 if (!target_can_run_breakpoint_commands ())
2450 return;
2451
2452 /* For now, limit to agent-style dprintf breakpoints. */
2453 if (dprintf_style != dprintf_style_agent)
2454 return;
2455
2456 /* For now, if we have any location at the same address that isn't a
2457 dprintf, don't install the target-side commands, as that would
2458 make the breakpoint not be reported to the core, and we'd lose
2459 control. */
2460 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2461 {
2462 loc = (*loc2p);
2463 if (is_breakpoint (loc->owner)
2464 && loc->pspace->num == bl->pspace->num
2465 && loc->owner->type != bp_dprintf)
2466 return;
2467 }
2468
2469 /* Do a first pass to check for locations with no assigned
2470 conditions or conditions that fail to parse to a valid agent expression
2471 bytecode. If any of these happen, then it's no use to send conditions
2472 to the target since this location will always trigger and generate a
2473 response back to GDB. */
2474 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2475 {
2476 loc = (*loc2p);
2477 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2478 {
2479 if (modified)
2480 {
2481 /* Re-parse the commands since something changed. In that
2482 case we already freed the command bytecodes (see
2483 force_breakpoint_reinsertion). We just
2484 need to parse the command to bytecodes again. */
2485 loc->cmd_bytecode
2486 = parse_cmd_to_aexpr (bl->address,
2487 loc->owner->extra_string);
2488 }
2489
2490 /* If we have a NULL bytecode expression, it means something
2491 went wrong or we have a null command expression. */
2492 if (!loc->cmd_bytecode)
2493 {
2494 null_command_or_parse_error = 1;
2495 break;
2496 }
2497 }
2498 }
2499
2500 /* If anything failed, then we're not doing target-side commands,
2501 and so clean up. */
2502 if (null_command_or_parse_error)
2503 {
2504 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2505 {
2506 loc = (*loc2p);
2507 if (is_breakpoint (loc->owner)
2508 && loc->pspace->num == bl->pspace->num)
2509 {
2510 /* Only go as far as the first NULL bytecode is
2511 located. */
2512 if (loc->cmd_bytecode == NULL)
2513 return;
2514
2515 loc->cmd_bytecode.reset ();
2516 }
2517 }
2518 }
2519
2520 /* No NULL commands or failed bytecode generation. Build a command
2521 list for all duplicate locations at this location's address.
2522 Note that here we must care for whether the breakpoint location
2523 types are considered duplicates, otherwise, say, if we have a
2524 software and hardware location at the same address, the target
2525 could end up running the commands twice. For the moment, we only
2526 support targets-side commands with dprintf, but it doesn't hurt
2527 to be pedantically correct in case that changes. */
2528 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2529 {
2530 loc = (*loc2p);
2531 if (breakpoint_locations_match (bl, loc)
2532 && loc->owner->extra_string
2533 && is_breakpoint (loc->owner)
2534 && loc->pspace->num == bl->pspace->num
2535 && loc->owner->enable_state == bp_enabled
2536 && loc->enabled
2537 && !loc->disabled_by_cond)
2538 {
2539 /* Add the command to the vector. This will be used later
2540 to send the commands to the target. */
2541 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2542 }
2543 }
2544
2545 bl->target_info.persist = 0;
2546 /* Maybe flag this location as persistent. */
2547 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2548 bl->target_info.persist = 1;
2549 }
2550
2551 /* Return the kind of breakpoint on address *ADDR. Get the kind
2552 of breakpoint according to ADDR except single-step breakpoint.
2553 Get the kind of single-step breakpoint according to the current
2554 registers state. */
2555
2556 static int
2557 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2558 {
2559 if (bl->owner->type == bp_single_step)
2560 {
2561 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2562 struct regcache *regcache;
2563
2564 regcache = get_thread_regcache (thr);
2565
2566 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2567 regcache, addr);
2568 }
2569 else
2570 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2571 }
2572
2573 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2574 location. Any error messages are printed to TMP_ERROR_STREAM; and
2575 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2576 Returns 0 for success, 1 if the bp_location type is not supported or
2577 -1 for failure.
2578
2579 NOTE drow/2003-09-09: This routine could be broken down to an
2580 object-style method for each breakpoint or catchpoint type. */
2581 static int
2582 insert_bp_location (struct bp_location *bl,
2583 struct ui_file *tmp_error_stream,
2584 int *disabled_breaks,
2585 int *hw_breakpoint_error,
2586 int *hw_bp_error_explained_already)
2587 {
2588 gdb_exception bp_excpt;
2589
2590 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2591 return 0;
2592
2593 /* Note we don't initialize bl->target_info, as that wipes out
2594 the breakpoint location's shadow_contents if the breakpoint
2595 is still inserted at that location. This in turn breaks
2596 target_read_memory which depends on these buffers when
2597 a memory read is requested at the breakpoint location:
2598 Once the target_info has been wiped, we fail to see that
2599 we have a breakpoint inserted at that address and thus
2600 read the breakpoint instead of returning the data saved in
2601 the breakpoint location's shadow contents. */
2602 bl->target_info.reqstd_address = bl->address;
2603 bl->target_info.placed_address_space = bl->pspace->aspace;
2604 bl->target_info.length = bl->length;
2605
2606 /* When working with target-side conditions, we must pass all the conditions
2607 for the same breakpoint address down to the target since GDB will not
2608 insert those locations. With a list of breakpoint conditions, the target
2609 can decide when to stop and notify GDB. */
2610
2611 if (is_breakpoint (bl->owner))
2612 {
2613 build_target_condition_list (bl);
2614 build_target_command_list (bl);
2615 /* Reset the modification marker. */
2616 bl->needs_update = 0;
2617 }
2618
2619 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2620 set at a read-only address, then a breakpoint location will have
2621 been changed to hardware breakpoint before we get here. If it is
2622 "off" however, error out before actually trying to insert the
2623 breakpoint, with a nicer error message. */
2624 if (bl->loc_type == bp_loc_software_breakpoint
2625 && !automatic_hardware_breakpoints)
2626 {
2627 mem_region *mr = lookup_mem_region (bl->address);
2628
2629 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2630 {
2631 fprintf_unfiltered (tmp_error_stream,
2632 _("Cannot insert breakpoint %d.\n"
2633 "Cannot set software breakpoint "
2634 "at read-only address %s\n"),
2635 bl->owner->number,
2636 paddress (bl->gdbarch, bl->address));
2637 return 1;
2638 }
2639 }
2640
2641 if (bl->loc_type == bp_loc_software_breakpoint
2642 || bl->loc_type == bp_loc_hardware_breakpoint)
2643 {
2644 /* First check to see if we have to handle an overlay. */
2645 if (overlay_debugging == ovly_off
2646 || bl->section == NULL
2647 || !(section_is_overlay (bl->section)))
2648 {
2649 /* No overlay handling: just set the breakpoint. */
2650 try
2651 {
2652 int val;
2653
2654 val = bl->owner->ops->insert_location (bl);
2655 if (val)
2656 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2657 }
2658 catch (gdb_exception &e)
2659 {
2660 bp_excpt = std::move (e);
2661 }
2662 }
2663 else
2664 {
2665 /* This breakpoint is in an overlay section.
2666 Shall we set a breakpoint at the LMA? */
2667 if (!overlay_events_enabled)
2668 {
2669 /* Yes -- overlay event support is not active,
2670 so we must try to set a breakpoint at the LMA.
2671 This will not work for a hardware breakpoint. */
2672 if (bl->loc_type == bp_loc_hardware_breakpoint)
2673 warning (_("hardware breakpoint %d not supported in overlay!"),
2674 bl->owner->number);
2675 else
2676 {
2677 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2678 bl->section);
2679 /* Set a software (trap) breakpoint at the LMA. */
2680 bl->overlay_target_info = bl->target_info;
2681 bl->overlay_target_info.reqstd_address = addr;
2682
2683 /* No overlay handling: just set the breakpoint. */
2684 try
2685 {
2686 int val;
2687
2688 bl->overlay_target_info.kind
2689 = breakpoint_kind (bl, &addr);
2690 bl->overlay_target_info.placed_address = addr;
2691 val = target_insert_breakpoint (bl->gdbarch,
2692 &bl->overlay_target_info);
2693 if (val)
2694 bp_excpt
2695 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2696 }
2697 catch (gdb_exception &e)
2698 {
2699 bp_excpt = std::move (e);
2700 }
2701
2702 if (bp_excpt.reason != 0)
2703 fprintf_unfiltered (tmp_error_stream,
2704 "Overlay breakpoint %d "
2705 "failed: in ROM?\n",
2706 bl->owner->number);
2707 }
2708 }
2709 /* Shall we set a breakpoint at the VMA? */
2710 if (section_is_mapped (bl->section))
2711 {
2712 /* Yes. This overlay section is mapped into memory. */
2713 try
2714 {
2715 int val;
2716
2717 val = bl->owner->ops->insert_location (bl);
2718 if (val)
2719 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2720 }
2721 catch (gdb_exception &e)
2722 {
2723 bp_excpt = std::move (e);
2724 }
2725 }
2726 else
2727 {
2728 /* No. This breakpoint will not be inserted.
2729 No error, but do not mark the bp as 'inserted'. */
2730 return 0;
2731 }
2732 }
2733
2734 if (bp_excpt.reason != 0)
2735 {
2736 /* Can't set the breakpoint. */
2737
2738 /* In some cases, we might not be able to insert a
2739 breakpoint in a shared library that has already been
2740 removed, but we have not yet processed the shlib unload
2741 event. Unfortunately, some targets that implement
2742 breakpoint insertion themselves can't tell why the
2743 breakpoint insertion failed (e.g., the remote target
2744 doesn't define error codes), so we must treat generic
2745 errors as memory errors. */
2746 if (bp_excpt.reason == RETURN_ERROR
2747 && (bp_excpt.error == GENERIC_ERROR
2748 || bp_excpt.error == MEMORY_ERROR)
2749 && bl->loc_type == bp_loc_software_breakpoint
2750 && (solib_name_from_address (bl->pspace, bl->address)
2751 || shared_objfile_contains_address_p (bl->pspace,
2752 bl->address)))
2753 {
2754 /* See also: disable_breakpoints_in_shlibs. */
2755 bl->shlib_disabled = 1;
2756 gdb::observers::breakpoint_modified.notify (bl->owner);
2757 if (!*disabled_breaks)
2758 {
2759 fprintf_unfiltered (tmp_error_stream,
2760 "Cannot insert breakpoint %d.\n",
2761 bl->owner->number);
2762 fprintf_unfiltered (tmp_error_stream,
2763 "Temporarily disabling shared "
2764 "library breakpoints:\n");
2765 }
2766 *disabled_breaks = 1;
2767 fprintf_unfiltered (tmp_error_stream,
2768 "breakpoint #%d\n", bl->owner->number);
2769 return 0;
2770 }
2771 else
2772 {
2773 if (bl->loc_type == bp_loc_hardware_breakpoint)
2774 {
2775 *hw_breakpoint_error = 1;
2776 *hw_bp_error_explained_already = bp_excpt.message != NULL;
2777 fprintf_unfiltered (tmp_error_stream,
2778 "Cannot insert hardware breakpoint %d%s",
2779 bl->owner->number,
2780 bp_excpt.message ? ":" : ".\n");
2781 if (bp_excpt.message != NULL)
2782 fprintf_unfiltered (tmp_error_stream, "%s.\n",
2783 bp_excpt.what ());
2784 }
2785 else
2786 {
2787 if (bp_excpt.message == NULL)
2788 {
2789 std::string message
2790 = memory_error_message (TARGET_XFER_E_IO,
2791 bl->gdbarch, bl->address);
2792
2793 fprintf_unfiltered (tmp_error_stream,
2794 "Cannot insert breakpoint %d.\n"
2795 "%s\n",
2796 bl->owner->number, message.c_str ());
2797 }
2798 else
2799 {
2800 fprintf_unfiltered (tmp_error_stream,
2801 "Cannot insert breakpoint %d: %s\n",
2802 bl->owner->number,
2803 bp_excpt.what ());
2804 }
2805 }
2806 return 1;
2807
2808 }
2809 }
2810 else
2811 bl->inserted = 1;
2812
2813 return 0;
2814 }
2815
2816 else if (bl->loc_type == bp_loc_hardware_watchpoint
2817 /* NOTE drow/2003-09-08: This state only exists for removing
2818 watchpoints. It's not clear that it's necessary... */
2819 && bl->owner->disposition != disp_del_at_next_stop)
2820 {
2821 int val;
2822
2823 gdb_assert (bl->owner->ops != NULL
2824 && bl->owner->ops->insert_location != NULL);
2825
2826 val = bl->owner->ops->insert_location (bl);
2827
2828 /* If trying to set a read-watchpoint, and it turns out it's not
2829 supported, try emulating one with an access watchpoint. */
2830 if (val == 1 && bl->watchpoint_type == hw_read)
2831 {
2832 struct bp_location *loc, **loc_temp;
2833
2834 /* But don't try to insert it, if there's already another
2835 hw_access location that would be considered a duplicate
2836 of this one. */
2837 ALL_BP_LOCATIONS (loc, loc_temp)
2838 if (loc != bl
2839 && loc->watchpoint_type == hw_access
2840 && watchpoint_locations_match (bl, loc))
2841 {
2842 bl->duplicate = 1;
2843 bl->inserted = 1;
2844 bl->target_info = loc->target_info;
2845 bl->watchpoint_type = hw_access;
2846 val = 0;
2847 break;
2848 }
2849
2850 if (val == 1)
2851 {
2852 bl->watchpoint_type = hw_access;
2853 val = bl->owner->ops->insert_location (bl);
2854
2855 if (val)
2856 /* Back to the original value. */
2857 bl->watchpoint_type = hw_read;
2858 }
2859 }
2860
2861 bl->inserted = (val == 0);
2862 }
2863
2864 else if (bl->owner->type == bp_catchpoint)
2865 {
2866 int val;
2867
2868 gdb_assert (bl->owner->ops != NULL
2869 && bl->owner->ops->insert_location != NULL);
2870
2871 val = bl->owner->ops->insert_location (bl);
2872 if (val)
2873 {
2874 bl->owner->enable_state = bp_disabled;
2875
2876 if (val == 1)
2877 warning (_("\
2878 Error inserting catchpoint %d: Your system does not support this type\n\
2879 of catchpoint."), bl->owner->number);
2880 else
2881 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2882 }
2883
2884 bl->inserted = (val == 0);
2885
2886 /* We've already printed an error message if there was a problem
2887 inserting this catchpoint, and we've disabled the catchpoint,
2888 so just return success. */
2889 return 0;
2890 }
2891
2892 return 0;
2893 }
2894
2895 /* This function is called when program space PSPACE is about to be
2896 deleted. It takes care of updating breakpoints to not reference
2897 PSPACE anymore. */
2898
2899 void
2900 breakpoint_program_space_exit (struct program_space *pspace)
2901 {
2902 struct bp_location *loc, **loc_temp;
2903
2904 /* Remove any breakpoint that was set through this program space. */
2905 for (breakpoint *b : all_breakpoints_safe ())
2906 if (b->pspace == pspace)
2907 delete_breakpoint (b);
2908
2909 /* Breakpoints set through other program spaces could have locations
2910 bound to PSPACE as well. Remove those. */
2911 ALL_BP_LOCATIONS (loc, loc_temp)
2912 {
2913 struct bp_location *tmp;
2914
2915 if (loc->pspace == pspace)
2916 {
2917 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2918 if (loc->owner->loc == loc)
2919 loc->owner->loc = loc->next;
2920 else
2921 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2922 if (tmp->next == loc)
2923 {
2924 tmp->next = loc->next;
2925 break;
2926 }
2927 }
2928 }
2929
2930 /* Now update the global location list to permanently delete the
2931 removed locations above. */
2932 update_global_location_list (UGLL_DONT_INSERT);
2933 }
2934
2935 /* Make sure all breakpoints are inserted in inferior.
2936 Throws exception on any error.
2937 A breakpoint that is already inserted won't be inserted
2938 again, so calling this function twice is safe. */
2939 void
2940 insert_breakpoints (void)
2941 {
2942 for (breakpoint *bpt : all_breakpoints ())
2943 if (is_hardware_watchpoint (bpt))
2944 {
2945 struct watchpoint *w = (struct watchpoint *) bpt;
2946
2947 update_watchpoint (w, 0 /* don't reparse. */);
2948 }
2949
2950 /* Updating watchpoints creates new locations, so update the global
2951 location list. Explicitly tell ugll to insert locations and
2952 ignore breakpoints_always_inserted_mode. Also,
2953 update_global_location_list tries to "upgrade" software
2954 breakpoints to hardware breakpoints to handle "set breakpoint
2955 auto-hw", so we need to call it even if we don't have new
2956 locations. */
2957 update_global_location_list (UGLL_INSERT);
2958 }
2959
2960 /* Invoke CALLBACK for each of bp_location. */
2961
2962 void
2963 iterate_over_bp_locations (gdb::function_view<void (bp_location *)> callback)
2964 {
2965 struct bp_location *loc, **loc_tmp;
2966
2967 ALL_BP_LOCATIONS (loc, loc_tmp)
2968 {
2969 callback (loc);
2970 }
2971 }
2972
2973 /* This is used when we need to synch breakpoint conditions between GDB and the
2974 target. It is the case with deleting and disabling of breakpoints when using
2975 always-inserted mode. */
2976
2977 static void
2978 update_inserted_breakpoint_locations (void)
2979 {
2980 struct bp_location *bl, **blp_tmp;
2981 int error_flag = 0;
2982 int val = 0;
2983 int disabled_breaks = 0;
2984 int hw_breakpoint_error = 0;
2985 int hw_bp_details_reported = 0;
2986
2987 string_file tmp_error_stream;
2988
2989 /* Explicitly mark the warning -- this will only be printed if
2990 there was an error. */
2991 tmp_error_stream.puts ("Warning:\n");
2992
2993 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2994
2995 ALL_BP_LOCATIONS (bl, blp_tmp)
2996 {
2997 /* We only want to update software breakpoints and hardware
2998 breakpoints. */
2999 if (!is_breakpoint (bl->owner))
3000 continue;
3001
3002 /* We only want to update locations that are already inserted
3003 and need updating. This is to avoid unwanted insertion during
3004 deletion of breakpoints. */
3005 if (!bl->inserted || !bl->needs_update)
3006 continue;
3007
3008 switch_to_program_space_and_thread (bl->pspace);
3009
3010 /* For targets that support global breakpoints, there's no need
3011 to select an inferior to insert breakpoint to. In fact, even
3012 if we aren't attached to any process yet, we should still
3013 insert breakpoints. */
3014 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3015 && (inferior_ptid == null_ptid || !target_has_execution ()))
3016 continue;
3017
3018 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3019 &hw_breakpoint_error, &hw_bp_details_reported);
3020 if (val)
3021 error_flag = val;
3022 }
3023
3024 if (error_flag)
3025 {
3026 target_terminal::ours_for_output ();
3027 error_stream (tmp_error_stream);
3028 }
3029 }
3030
3031 /* Used when starting or continuing the program. */
3032
3033 static void
3034 insert_breakpoint_locations (void)
3035 {
3036 struct bp_location *bl, **blp_tmp;
3037 int error_flag = 0;
3038 int val = 0;
3039 int disabled_breaks = 0;
3040 int hw_breakpoint_error = 0;
3041 int hw_bp_error_explained_already = 0;
3042
3043 string_file tmp_error_stream;
3044
3045 /* Explicitly mark the warning -- this will only be printed if
3046 there was an error. */
3047 tmp_error_stream.puts ("Warning:\n");
3048
3049 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3050
3051 ALL_BP_LOCATIONS (bl, blp_tmp)
3052 {
3053 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3054 continue;
3055
3056 /* There is no point inserting thread-specific breakpoints if
3057 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3058 has BL->OWNER always non-NULL. */
3059 if (bl->owner->thread != -1
3060 && !valid_global_thread_id (bl->owner->thread))
3061 continue;
3062
3063 switch_to_program_space_and_thread (bl->pspace);
3064
3065 /* For targets that support global breakpoints, there's no need
3066 to select an inferior to insert breakpoint to. In fact, even
3067 if we aren't attached to any process yet, we should still
3068 insert breakpoints. */
3069 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3070 && (inferior_ptid == null_ptid || !target_has_execution ()))
3071 continue;
3072
3073 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3074 &hw_breakpoint_error, &hw_bp_error_explained_already);
3075 if (val)
3076 error_flag = val;
3077 }
3078
3079 /* If we failed to insert all locations of a watchpoint, remove
3080 them, as half-inserted watchpoint is of limited use. */
3081 for (breakpoint *bpt : all_breakpoints ())
3082 {
3083 int some_failed = 0;
3084
3085 if (!is_hardware_watchpoint (bpt))
3086 continue;
3087
3088 if (!breakpoint_enabled (bpt))
3089 continue;
3090
3091 if (bpt->disposition == disp_del_at_next_stop)
3092 continue;
3093
3094 for (bp_location *loc : bpt->locations ())
3095 if (!loc->inserted && should_be_inserted (loc))
3096 {
3097 some_failed = 1;
3098 break;
3099 }
3100
3101 if (some_failed)
3102 {
3103 for (bp_location *loc : bpt->locations ())
3104 if (loc->inserted)
3105 remove_breakpoint (loc);
3106
3107 hw_breakpoint_error = 1;
3108 tmp_error_stream.printf ("Could not insert "
3109 "hardware watchpoint %d.\n",
3110 bpt->number);
3111 error_flag = -1;
3112 }
3113 }
3114
3115 if (error_flag)
3116 {
3117 /* If a hardware breakpoint or watchpoint was inserted, add a
3118 message about possibly exhausted resources. */
3119 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3120 {
3121 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3122 You may have requested too many hardware breakpoints/watchpoints.\n");
3123 }
3124 target_terminal::ours_for_output ();
3125 error_stream (tmp_error_stream);
3126 }
3127 }
3128
3129 /* Used when the program stops.
3130 Returns zero if successful, or non-zero if there was a problem
3131 removing a breakpoint location. */
3132
3133 int
3134 remove_breakpoints (void)
3135 {
3136 struct bp_location *bl, **blp_tmp;
3137 int val = 0;
3138
3139 ALL_BP_LOCATIONS (bl, blp_tmp)
3140 {
3141 if (bl->inserted && !is_tracepoint (bl->owner))
3142 val |= remove_breakpoint (bl);
3143 }
3144 return val;
3145 }
3146
3147 /* When a thread exits, remove breakpoints that are related to
3148 that thread. */
3149
3150 static void
3151 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3152 {
3153 for (breakpoint *b : all_breakpoints_safe ())
3154 {
3155 if (b->thread == tp->global_num && user_breakpoint_p (b))
3156 {
3157 b->disposition = disp_del_at_next_stop;
3158
3159 printf_filtered (_("\
3160 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3161 b->number, print_thread_id (tp));
3162
3163 /* Hide it from the user. */
3164 b->number = 0;
3165 }
3166 }
3167 }
3168
3169 /* See breakpoint.h. */
3170
3171 void
3172 remove_breakpoints_inf (inferior *inf)
3173 {
3174 struct bp_location *bl, **blp_tmp;
3175 int val;
3176
3177 ALL_BP_LOCATIONS (bl, blp_tmp)
3178 {
3179 if (bl->pspace != inf->pspace)
3180 continue;
3181
3182 if (bl->inserted && !bl->target_info.persist)
3183 {
3184 val = remove_breakpoint (bl);
3185 if (val != 0)
3186 return;
3187 }
3188 }
3189 }
3190
3191 static int internal_breakpoint_number = -1;
3192
3193 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3194 If INTERNAL is non-zero, the breakpoint number will be populated
3195 from internal_breakpoint_number and that variable decremented.
3196 Otherwise the breakpoint number will be populated from
3197 breakpoint_count and that value incremented. Internal breakpoints
3198 do not set the internal var bpnum. */
3199 static void
3200 set_breakpoint_number (int internal, struct breakpoint *b)
3201 {
3202 if (internal)
3203 b->number = internal_breakpoint_number--;
3204 else
3205 {
3206 set_breakpoint_count (breakpoint_count + 1);
3207 b->number = breakpoint_count;
3208 }
3209 }
3210
3211 static struct breakpoint *
3212 create_internal_breakpoint (struct gdbarch *gdbarch,
3213 CORE_ADDR address, enum bptype type,
3214 const struct breakpoint_ops *ops)
3215 {
3216 symtab_and_line sal;
3217 sal.pc = address;
3218 sal.section = find_pc_overlay (sal.pc);
3219 sal.pspace = current_program_space;
3220
3221 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
3222 b->number = internal_breakpoint_number--;
3223 b->disposition = disp_donttouch;
3224
3225 return b;
3226 }
3227
3228 static const char *const longjmp_names[] =
3229 {
3230 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3231 };
3232 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3233
3234 /* Per-objfile data private to breakpoint.c. */
3235 struct breakpoint_objfile_data
3236 {
3237 /* Minimal symbol for "_ovly_debug_event" (if any). */
3238 struct bound_minimal_symbol overlay_msym {};
3239
3240 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3241 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
3242
3243 /* True if we have looked for longjmp probes. */
3244 int longjmp_searched = 0;
3245
3246 /* SystemTap probe points for longjmp (if any). These are non-owning
3247 references. */
3248 std::vector<probe *> longjmp_probes;
3249
3250 /* Minimal symbol for "std::terminate()" (if any). */
3251 struct bound_minimal_symbol terminate_msym {};
3252
3253 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3254 struct bound_minimal_symbol exception_msym {};
3255
3256 /* True if we have looked for exception probes. */
3257 int exception_searched = 0;
3258
3259 /* SystemTap probe points for unwinding (if any). These are non-owning
3260 references. */
3261 std::vector<probe *> exception_probes;
3262 };
3263
3264 static const struct objfile_key<breakpoint_objfile_data>
3265 breakpoint_objfile_key;
3266
3267 /* Minimal symbol not found sentinel. */
3268 static struct minimal_symbol msym_not_found;
3269
3270 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3271
3272 static int
3273 msym_not_found_p (const struct minimal_symbol *msym)
3274 {
3275 return msym == &msym_not_found;
3276 }
3277
3278 /* Return per-objfile data needed by breakpoint.c.
3279 Allocate the data if necessary. */
3280
3281 static struct breakpoint_objfile_data *
3282 get_breakpoint_objfile_data (struct objfile *objfile)
3283 {
3284 struct breakpoint_objfile_data *bp_objfile_data;
3285
3286 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3287 if (bp_objfile_data == NULL)
3288 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3289 return bp_objfile_data;
3290 }
3291
3292 static void
3293 create_overlay_event_breakpoint (void)
3294 {
3295 const char *const func_name = "_ovly_debug_event";
3296
3297 for (objfile *objfile : current_program_space->objfiles ())
3298 {
3299 struct breakpoint *b;
3300 struct breakpoint_objfile_data *bp_objfile_data;
3301 CORE_ADDR addr;
3302 struct explicit_location explicit_loc;
3303
3304 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3305
3306 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3307 continue;
3308
3309 if (bp_objfile_data->overlay_msym.minsym == NULL)
3310 {
3311 struct bound_minimal_symbol m;
3312
3313 m = lookup_minimal_symbol_text (func_name, objfile);
3314 if (m.minsym == NULL)
3315 {
3316 /* Avoid future lookups in this objfile. */
3317 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3318 continue;
3319 }
3320 bp_objfile_data->overlay_msym = m;
3321 }
3322
3323 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3324 b = create_internal_breakpoint (objfile->arch (), addr,
3325 bp_overlay_event,
3326 &internal_breakpoint_ops);
3327 initialize_explicit_location (&explicit_loc);
3328 explicit_loc.function_name = ASTRDUP (func_name);
3329 b->location = new_explicit_location (&explicit_loc);
3330
3331 if (overlay_debugging == ovly_auto)
3332 {
3333 b->enable_state = bp_enabled;
3334 overlay_events_enabled = 1;
3335 }
3336 else
3337 {
3338 b->enable_state = bp_disabled;
3339 overlay_events_enabled = 0;
3340 }
3341 }
3342 }
3343
3344 /* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3345 true if a breakpoint was installed. */
3346
3347 static bool
3348 create_longjmp_master_breakpoint_probe (objfile *objfile)
3349 {
3350 struct gdbarch *gdbarch = objfile->arch ();
3351 struct breakpoint_objfile_data *bp_objfile_data
3352 = get_breakpoint_objfile_data (objfile);
3353
3354 if (!bp_objfile_data->longjmp_searched)
3355 {
3356 std::vector<probe *> ret
3357 = find_probes_in_objfile (objfile, "libc", "longjmp");
3358
3359 if (!ret.empty ())
3360 {
3361 /* We are only interested in checking one element. */
3362 probe *p = ret[0];
3363
3364 if (!p->can_evaluate_arguments ())
3365 {
3366 /* We cannot use the probe interface here,
3367 because it does not know how to evaluate
3368 arguments. */
3369 ret.clear ();
3370 }
3371 }
3372 bp_objfile_data->longjmp_probes = ret;
3373 bp_objfile_data->longjmp_searched = 1;
3374 }
3375
3376 if (bp_objfile_data->longjmp_probes.empty ())
3377 return false;
3378
3379 for (probe *p : bp_objfile_data->longjmp_probes)
3380 {
3381 struct breakpoint *b;
3382
3383 b = create_internal_breakpoint (gdbarch,
3384 p->get_relocated_address (objfile),
3385 bp_longjmp_master,
3386 &internal_breakpoint_ops);
3387 b->location = new_probe_location ("-probe-stap libc:longjmp");
3388 b->enable_state = bp_disabled;
3389 }
3390
3391 return true;
3392 }
3393
3394 /* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3395 Return true if at least one breakpoint was installed. */
3396
3397 static bool
3398 create_longjmp_master_breakpoint_names (objfile *objfile)
3399 {
3400 struct gdbarch *gdbarch = objfile->arch ();
3401 if (!gdbarch_get_longjmp_target_p (gdbarch))
3402 return false;
3403
3404 struct breakpoint_objfile_data *bp_objfile_data
3405 = get_breakpoint_objfile_data (objfile);
3406 unsigned int installed_bp = 0;
3407
3408 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3409 {
3410 struct breakpoint *b;
3411 const char *func_name;
3412 CORE_ADDR addr;
3413 struct explicit_location explicit_loc;
3414
3415 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3416 continue;
3417
3418 func_name = longjmp_names[i];
3419 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3420 {
3421 struct bound_minimal_symbol m;
3422
3423 m = lookup_minimal_symbol_text (func_name, objfile);
3424 if (m.minsym == NULL)
3425 {
3426 /* Prevent future lookups in this objfile. */
3427 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3428 continue;
3429 }
3430 bp_objfile_data->longjmp_msym[i] = m;
3431 }
3432
3433 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3434 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3435 &internal_breakpoint_ops);
3436 initialize_explicit_location (&explicit_loc);
3437 explicit_loc.function_name = ASTRDUP (func_name);
3438 b->location = new_explicit_location (&explicit_loc);
3439 b->enable_state = bp_disabled;
3440 installed_bp++;
3441 }
3442
3443 return installed_bp > 0;
3444 }
3445
3446 /* Create a master longjmp breakpoint. */
3447
3448 static void
3449 create_longjmp_master_breakpoint (void)
3450 {
3451 scoped_restore_current_program_space restore_pspace;
3452
3453 for (struct program_space *pspace : program_spaces)
3454 {
3455 set_current_program_space (pspace);
3456
3457 for (objfile *obj : current_program_space->objfiles ())
3458 {
3459 /* Skip separate debug object, it's handled in the loop below. */
3460 if (obj->separate_debug_objfile_backlink != nullptr)
3461 continue;
3462
3463 /* Try a probe kind breakpoint on main objfile. */
3464 if (create_longjmp_master_breakpoint_probe (obj))
3465 continue;
3466
3467 /* Try longjmp_names kind breakpoints on main and separate_debug
3468 objfiles. */
3469 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3470 if (create_longjmp_master_breakpoint_names (debug_objfile))
3471 break;
3472 }
3473 }
3474 }
3475
3476 /* Create a master std::terminate breakpoint. */
3477 static void
3478 create_std_terminate_master_breakpoint (void)
3479 {
3480 const char *const func_name = "std::terminate()";
3481
3482 scoped_restore_current_program_space restore_pspace;
3483
3484 for (struct program_space *pspace : program_spaces)
3485 {
3486 CORE_ADDR addr;
3487
3488 set_current_program_space (pspace);
3489
3490 for (objfile *objfile : current_program_space->objfiles ())
3491 {
3492 struct breakpoint *b;
3493 struct breakpoint_objfile_data *bp_objfile_data;
3494 struct explicit_location explicit_loc;
3495
3496 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3497
3498 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3499 continue;
3500
3501 if (bp_objfile_data->terminate_msym.minsym == NULL)
3502 {
3503 struct bound_minimal_symbol m;
3504
3505 m = lookup_minimal_symbol (func_name, NULL, objfile);
3506 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3507 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3508 {
3509 /* Prevent future lookups in this objfile. */
3510 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3511 continue;
3512 }
3513 bp_objfile_data->terminate_msym = m;
3514 }
3515
3516 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3517 b = create_internal_breakpoint (objfile->arch (), addr,
3518 bp_std_terminate_master,
3519 &internal_breakpoint_ops);
3520 initialize_explicit_location (&explicit_loc);
3521 explicit_loc.function_name = ASTRDUP (func_name);
3522 b->location = new_explicit_location (&explicit_loc);
3523 b->enable_state = bp_disabled;
3524 }
3525 }
3526 }
3527
3528 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3529 probe. Return true if a breakpoint was installed. */
3530
3531 static bool
3532 create_exception_master_breakpoint_probe (objfile *objfile)
3533 {
3534 struct breakpoint *b;
3535 struct gdbarch *gdbarch;
3536 struct breakpoint_objfile_data *bp_objfile_data;
3537
3538 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3539
3540 /* We prefer the SystemTap probe point if it exists. */
3541 if (!bp_objfile_data->exception_searched)
3542 {
3543 std::vector<probe *> ret
3544 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3545
3546 if (!ret.empty ())
3547 {
3548 /* We are only interested in checking one element. */
3549 probe *p = ret[0];
3550
3551 if (!p->can_evaluate_arguments ())
3552 {
3553 /* We cannot use the probe interface here, because it does
3554 not know how to evaluate arguments. */
3555 ret.clear ();
3556 }
3557 }
3558 bp_objfile_data->exception_probes = ret;
3559 bp_objfile_data->exception_searched = 1;
3560 }
3561
3562 if (bp_objfile_data->exception_probes.empty ())
3563 return false;
3564
3565 gdbarch = objfile->arch ();
3566
3567 for (probe *p : bp_objfile_data->exception_probes)
3568 {
3569 b = create_internal_breakpoint (gdbarch,
3570 p->get_relocated_address (objfile),
3571 bp_exception_master,
3572 &internal_breakpoint_ops);
3573 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3574 b->enable_state = bp_disabled;
3575 }
3576
3577 return true;
3578 }
3579
3580 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3581 _Unwind_DebugHook. Return true if a breakpoint was installed. */
3582
3583 static bool
3584 create_exception_master_breakpoint_hook (objfile *objfile)
3585 {
3586 const char *const func_name = "_Unwind_DebugHook";
3587 struct breakpoint *b;
3588 struct gdbarch *gdbarch;
3589 struct breakpoint_objfile_data *bp_objfile_data;
3590 CORE_ADDR addr;
3591 struct explicit_location explicit_loc;
3592
3593 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3594
3595 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3596 return false;
3597
3598 gdbarch = objfile->arch ();
3599
3600 if (bp_objfile_data->exception_msym.minsym == NULL)
3601 {
3602 struct bound_minimal_symbol debug_hook;
3603
3604 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3605 if (debug_hook.minsym == NULL)
3606 {
3607 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3608 return false;
3609 }
3610
3611 bp_objfile_data->exception_msym = debug_hook;
3612 }
3613
3614 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3615 addr = gdbarch_convert_from_func_ptr_addr
3616 (gdbarch, addr, current_inferior ()->top_target ());
3617 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3618 &internal_breakpoint_ops);
3619 initialize_explicit_location (&explicit_loc);
3620 explicit_loc.function_name = ASTRDUP (func_name);
3621 b->location = new_explicit_location (&explicit_loc);
3622 b->enable_state = bp_disabled;
3623
3624 return true;
3625 }
3626
3627 /* Install a master breakpoint on the unwinder's debug hook. */
3628
3629 static void
3630 create_exception_master_breakpoint (void)
3631 {
3632 for (objfile *obj : current_program_space->objfiles ())
3633 {
3634 /* Skip separate debug object. */
3635 if (obj->separate_debug_objfile_backlink)
3636 continue;
3637
3638 /* Try a probe kind breakpoint. */
3639 if (create_exception_master_breakpoint_probe (obj))
3640 continue;
3641
3642 /* Iterate over main and separate debug objects and try an
3643 _Unwind_DebugHook kind breakpoint. */
3644 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3645 if (create_exception_master_breakpoint_hook (debug_objfile))
3646 break;
3647 }
3648 }
3649
3650 /* Does B have a location spec? */
3651
3652 static int
3653 breakpoint_event_location_empty_p (const struct breakpoint *b)
3654 {
3655 return b->location != NULL && event_location_empty_p (b->location.get ());
3656 }
3657
3658 void
3659 update_breakpoints_after_exec (void)
3660 {
3661 struct bp_location *bploc, **bplocp_tmp;
3662
3663 /* We're about to delete breakpoints from GDB's lists. If the
3664 INSERTED flag is true, GDB will try to lift the breakpoints by
3665 writing the breakpoints' "shadow contents" back into memory. The
3666 "shadow contents" are NOT valid after an exec, so GDB should not
3667 do that. Instead, the target is responsible from marking
3668 breakpoints out as soon as it detects an exec. We don't do that
3669 here instead, because there may be other attempts to delete
3670 breakpoints after detecting an exec and before reaching here. */
3671 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3672 if (bploc->pspace == current_program_space)
3673 gdb_assert (!bploc->inserted);
3674
3675 for (breakpoint *b : all_breakpoints_safe ())
3676 {
3677 if (b->pspace != current_program_space)
3678 continue;
3679
3680 /* Solib breakpoints must be explicitly reset after an exec(). */
3681 if (b->type == bp_shlib_event)
3682 {
3683 delete_breakpoint (b);
3684 continue;
3685 }
3686
3687 /* JIT breakpoints must be explicitly reset after an exec(). */
3688 if (b->type == bp_jit_event)
3689 {
3690 delete_breakpoint (b);
3691 continue;
3692 }
3693
3694 /* Thread event breakpoints must be set anew after an exec(),
3695 as must overlay event and longjmp master breakpoints. */
3696 if (b->type == bp_thread_event || b->type == bp_overlay_event
3697 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3698 || b->type == bp_exception_master)
3699 {
3700 delete_breakpoint (b);
3701 continue;
3702 }
3703
3704 /* Step-resume breakpoints are meaningless after an exec(). */
3705 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3706 {
3707 delete_breakpoint (b);
3708 continue;
3709 }
3710
3711 /* Just like single-step breakpoints. */
3712 if (b->type == bp_single_step)
3713 {
3714 delete_breakpoint (b);
3715 continue;
3716 }
3717
3718 /* Longjmp and longjmp-resume breakpoints are also meaningless
3719 after an exec. */
3720 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3721 || b->type == bp_longjmp_call_dummy
3722 || b->type == bp_exception || b->type == bp_exception_resume)
3723 {
3724 delete_breakpoint (b);
3725 continue;
3726 }
3727
3728 if (b->type == bp_catchpoint)
3729 {
3730 /* For now, none of the bp_catchpoint breakpoints need to
3731 do anything at this point. In the future, if some of
3732 the catchpoints need to something, we will need to add
3733 a new method, and call this method from here. */
3734 continue;
3735 }
3736
3737 /* bp_finish is a special case. The only way we ought to be able
3738 to see one of these when an exec() has happened, is if the user
3739 caught a vfork, and then said "finish". Ordinarily a finish just
3740 carries them to the call-site of the current callee, by setting
3741 a temporary bp there and resuming. But in this case, the finish
3742 will carry them entirely through the vfork & exec.
3743
3744 We don't want to allow a bp_finish to remain inserted now. But
3745 we can't safely delete it, 'cause finish_command has a handle to
3746 the bp on a bpstat, and will later want to delete it. There's a
3747 chance (and I've seen it happen) that if we delete the bp_finish
3748 here, that its storage will get reused by the time finish_command
3749 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3750 We really must allow finish_command to delete a bp_finish.
3751
3752 In the absence of a general solution for the "how do we know
3753 it's safe to delete something others may have handles to?"
3754 problem, what we'll do here is just uninsert the bp_finish, and
3755 let finish_command delete it.
3756
3757 (We know the bp_finish is "doomed" in the sense that it's
3758 momentary, and will be deleted as soon as finish_command sees
3759 the inferior stopped. So it doesn't matter that the bp's
3760 address is probably bogus in the new a.out, unlike e.g., the
3761 solib breakpoints.) */
3762
3763 if (b->type == bp_finish)
3764 {
3765 continue;
3766 }
3767
3768 /* Without a symbolic address, we have little hope of the
3769 pre-exec() address meaning the same thing in the post-exec()
3770 a.out. */
3771 if (breakpoint_event_location_empty_p (b))
3772 {
3773 delete_breakpoint (b);
3774 continue;
3775 }
3776 }
3777 }
3778
3779 int
3780 detach_breakpoints (ptid_t ptid)
3781 {
3782 struct bp_location *bl, **blp_tmp;
3783 int val = 0;
3784 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3785 struct inferior *inf = current_inferior ();
3786
3787 if (ptid.pid () == inferior_ptid.pid ())
3788 error (_("Cannot detach breakpoints of inferior_ptid"));
3789
3790 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3791 inferior_ptid = ptid;
3792 ALL_BP_LOCATIONS (bl, blp_tmp)
3793 {
3794 if (bl->pspace != inf->pspace)
3795 continue;
3796
3797 /* This function must physically remove breakpoints locations
3798 from the specified ptid, without modifying the breakpoint
3799 package's state. Locations of type bp_loc_other are only
3800 maintained at GDB side. So, there is no need to remove
3801 these bp_loc_other locations. Moreover, removing these
3802 would modify the breakpoint package's state. */
3803 if (bl->loc_type == bp_loc_other)
3804 continue;
3805
3806 if (bl->inserted)
3807 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3808 }
3809
3810 return val;
3811 }
3812
3813 /* Remove the breakpoint location BL from the current address space.
3814 Note that this is used to detach breakpoints from a child fork.
3815 When we get here, the child isn't in the inferior list, and neither
3816 do we have objects to represent its address space --- we should
3817 *not* look at bl->pspace->aspace here. */
3818
3819 static int
3820 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3821 {
3822 int val;
3823
3824 /* BL is never in moribund_locations by our callers. */
3825 gdb_assert (bl->owner != NULL);
3826
3827 /* The type of none suggests that owner is actually deleted.
3828 This should not ever happen. */
3829 gdb_assert (bl->owner->type != bp_none);
3830
3831 if (bl->loc_type == bp_loc_software_breakpoint
3832 || bl->loc_type == bp_loc_hardware_breakpoint)
3833 {
3834 /* "Normal" instruction breakpoint: either the standard
3835 trap-instruction bp (bp_breakpoint), or a
3836 bp_hardware_breakpoint. */
3837
3838 /* First check to see if we have to handle an overlay. */
3839 if (overlay_debugging == ovly_off
3840 || bl->section == NULL
3841 || !(section_is_overlay (bl->section)))
3842 {
3843 /* No overlay handling: just remove the breakpoint. */
3844
3845 /* If we're trying to uninsert a memory breakpoint that we
3846 know is set in a dynamic object that is marked
3847 shlib_disabled, then either the dynamic object was
3848 removed with "remove-symbol-file" or with
3849 "nosharedlibrary". In the former case, we don't know
3850 whether another dynamic object might have loaded over the
3851 breakpoint's address -- the user might well let us know
3852 about it next with add-symbol-file (the whole point of
3853 add-symbol-file is letting the user manually maintain a
3854 list of dynamically loaded objects). If we have the
3855 breakpoint's shadow memory, that is, this is a software
3856 breakpoint managed by GDB, check whether the breakpoint
3857 is still inserted in memory, to avoid overwriting wrong
3858 code with stale saved shadow contents. Note that HW
3859 breakpoints don't have shadow memory, as they're
3860 implemented using a mechanism that is not dependent on
3861 being able to modify the target's memory, and as such
3862 they should always be removed. */
3863 if (bl->shlib_disabled
3864 && bl->target_info.shadow_len != 0
3865 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3866 val = 0;
3867 else
3868 val = bl->owner->ops->remove_location (bl, reason);
3869 }
3870 else
3871 {
3872 /* This breakpoint is in an overlay section.
3873 Did we set a breakpoint at the LMA? */
3874 if (!overlay_events_enabled)
3875 {
3876 /* Yes -- overlay event support is not active, so we
3877 should have set a breakpoint at the LMA. Remove it.
3878 */
3879 /* Ignore any failures: if the LMA is in ROM, we will
3880 have already warned when we failed to insert it. */
3881 if (bl->loc_type == bp_loc_hardware_breakpoint)
3882 target_remove_hw_breakpoint (bl->gdbarch,
3883 &bl->overlay_target_info);
3884 else
3885 target_remove_breakpoint (bl->gdbarch,
3886 &bl->overlay_target_info,
3887 reason);
3888 }
3889 /* Did we set a breakpoint at the VMA?
3890 If so, we will have marked the breakpoint 'inserted'. */
3891 if (bl->inserted)
3892 {
3893 /* Yes -- remove it. Previously we did not bother to
3894 remove the breakpoint if the section had been
3895 unmapped, but let's not rely on that being safe. We
3896 don't know what the overlay manager might do. */
3897
3898 /* However, we should remove *software* breakpoints only
3899 if the section is still mapped, or else we overwrite
3900 wrong code with the saved shadow contents. */
3901 if (bl->loc_type == bp_loc_hardware_breakpoint
3902 || section_is_mapped (bl->section))
3903 val = bl->owner->ops->remove_location (bl, reason);
3904 else
3905 val = 0;
3906 }
3907 else
3908 {
3909 /* No -- not inserted, so no need to remove. No error. */
3910 val = 0;
3911 }
3912 }
3913
3914 /* In some cases, we might not be able to remove a breakpoint in
3915 a shared library that has already been removed, but we have
3916 not yet processed the shlib unload event. Similarly for an
3917 unloaded add-symbol-file object - the user might not yet have
3918 had the chance to remove-symbol-file it. shlib_disabled will
3919 be set if the library/object has already been removed, but
3920 the breakpoint hasn't been uninserted yet, e.g., after
3921 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3922 always-inserted mode. */
3923 if (val
3924 && (bl->loc_type == bp_loc_software_breakpoint
3925 && (bl->shlib_disabled
3926 || solib_name_from_address (bl->pspace, bl->address)
3927 || shared_objfile_contains_address_p (bl->pspace,
3928 bl->address))))
3929 val = 0;
3930
3931 if (val)
3932 return val;
3933 bl->inserted = (reason == DETACH_BREAKPOINT);
3934 }
3935 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3936 {
3937 gdb_assert (bl->owner->ops != NULL
3938 && bl->owner->ops->remove_location != NULL);
3939
3940 bl->inserted = (reason == DETACH_BREAKPOINT);
3941 bl->owner->ops->remove_location (bl, reason);
3942
3943 /* Failure to remove any of the hardware watchpoints comes here. */
3944 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3945 warning (_("Could not remove hardware watchpoint %d."),
3946 bl->owner->number);
3947 }
3948 else if (bl->owner->type == bp_catchpoint
3949 && breakpoint_enabled (bl->owner)
3950 && !bl->duplicate)
3951 {
3952 gdb_assert (bl->owner->ops != NULL
3953 && bl->owner->ops->remove_location != NULL);
3954
3955 val = bl->owner->ops->remove_location (bl, reason);
3956 if (val)
3957 return val;
3958
3959 bl->inserted = (reason == DETACH_BREAKPOINT);
3960 }
3961
3962 return 0;
3963 }
3964
3965 static int
3966 remove_breakpoint (struct bp_location *bl)
3967 {
3968 /* BL is never in moribund_locations by our callers. */
3969 gdb_assert (bl->owner != NULL);
3970
3971 /* The type of none suggests that owner is actually deleted.
3972 This should not ever happen. */
3973 gdb_assert (bl->owner->type != bp_none);
3974
3975 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3976
3977 switch_to_program_space_and_thread (bl->pspace);
3978
3979 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
3980 }
3981
3982 /* Clear the "inserted" flag in all breakpoints. */
3983
3984 void
3985 mark_breakpoints_out (void)
3986 {
3987 struct bp_location *bl, **blp_tmp;
3988
3989 ALL_BP_LOCATIONS (bl, blp_tmp)
3990 if (bl->pspace == current_program_space)
3991 bl->inserted = 0;
3992 }
3993
3994 /* Clear the "inserted" flag in all breakpoints and delete any
3995 breakpoints which should go away between runs of the program.
3996
3997 Plus other such housekeeping that has to be done for breakpoints
3998 between runs.
3999
4000 Note: this function gets called at the end of a run (by
4001 generic_mourn_inferior) and when a run begins (by
4002 init_wait_for_inferior). */
4003
4004
4005
4006 void
4007 breakpoint_init_inferior (enum inf_context context)
4008 {
4009 struct program_space *pspace = current_program_space;
4010
4011 /* If breakpoint locations are shared across processes, then there's
4012 nothing to do. */
4013 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4014 return;
4015
4016 mark_breakpoints_out ();
4017
4018 for (breakpoint *b : all_breakpoints_safe ())
4019 {
4020 if (b->loc && b->loc->pspace != pspace)
4021 continue;
4022
4023 switch (b->type)
4024 {
4025 case bp_call_dummy:
4026 case bp_longjmp_call_dummy:
4027
4028 /* If the call dummy breakpoint is at the entry point it will
4029 cause problems when the inferior is rerun, so we better get
4030 rid of it. */
4031
4032 case bp_watchpoint_scope:
4033
4034 /* Also get rid of scope breakpoints. */
4035
4036 case bp_shlib_event:
4037
4038 /* Also remove solib event breakpoints. Their addresses may
4039 have changed since the last time we ran the program.
4040 Actually we may now be debugging against different target;
4041 and so the solib backend that installed this breakpoint may
4042 not be used in by the target. E.g.,
4043
4044 (gdb) file prog-linux
4045 (gdb) run # native linux target
4046 ...
4047 (gdb) kill
4048 (gdb) file prog-win.exe
4049 (gdb) tar rem :9999 # remote Windows gdbserver.
4050 */
4051
4052 case bp_step_resume:
4053
4054 /* Also remove step-resume breakpoints. */
4055
4056 case bp_single_step:
4057
4058 /* Also remove single-step breakpoints. */
4059
4060 delete_breakpoint (b);
4061 break;
4062
4063 case bp_watchpoint:
4064 case bp_hardware_watchpoint:
4065 case bp_read_watchpoint:
4066 case bp_access_watchpoint:
4067 {
4068 struct watchpoint *w = (struct watchpoint *) b;
4069
4070 /* Likewise for watchpoints on local expressions. */
4071 if (w->exp_valid_block != NULL)
4072 delete_breakpoint (b);
4073 else
4074 {
4075 /* Get rid of existing locations, which are no longer
4076 valid. New ones will be created in
4077 update_watchpoint, when the inferior is restarted.
4078 The next update_global_location_list call will
4079 garbage collect them. */
4080 b->loc = NULL;
4081
4082 if (context == inf_starting)
4083 {
4084 /* Reset val field to force reread of starting value in
4085 insert_breakpoints. */
4086 w->val.reset (nullptr);
4087 w->val_valid = false;
4088 }
4089 }
4090 }
4091 break;
4092 default:
4093 break;
4094 }
4095 }
4096
4097 /* Get rid of the moribund locations. */
4098 for (bp_location *bl : moribund_locations)
4099 decref_bp_location (&bl);
4100 moribund_locations.clear ();
4101 }
4102
4103 /* These functions concern about actual breakpoints inserted in the
4104 target --- to e.g. check if we need to do decr_pc adjustment or if
4105 we need to hop over the bkpt --- so we check for address space
4106 match, not program space. */
4107
4108 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4109 exists at PC. It returns ordinary_breakpoint_here if it's an
4110 ordinary breakpoint, or permanent_breakpoint_here if it's a
4111 permanent breakpoint.
4112 - When continuing from a location with an ordinary breakpoint, we
4113 actually single step once before calling insert_breakpoints.
4114 - When continuing from a location with a permanent breakpoint, we
4115 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4116 the target, to advance the PC past the breakpoint. */
4117
4118 enum breakpoint_here
4119 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4120 {
4121 struct bp_location *bl, **blp_tmp;
4122 int any_breakpoint_here = 0;
4123
4124 ALL_BP_LOCATIONS (bl, blp_tmp)
4125 {
4126 if (bl->loc_type != bp_loc_software_breakpoint
4127 && bl->loc_type != bp_loc_hardware_breakpoint)
4128 continue;
4129
4130 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4131 if ((breakpoint_enabled (bl->owner)
4132 || bl->permanent)
4133 && breakpoint_location_address_match (bl, aspace, pc))
4134 {
4135 if (overlay_debugging
4136 && section_is_overlay (bl->section)
4137 && !section_is_mapped (bl->section))
4138 continue; /* unmapped overlay -- can't be a match */
4139 else if (bl->permanent)
4140 return permanent_breakpoint_here;
4141 else
4142 any_breakpoint_here = 1;
4143 }
4144 }
4145
4146 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4147 }
4148
4149 /* See breakpoint.h. */
4150
4151 int
4152 breakpoint_in_range_p (const address_space *aspace,
4153 CORE_ADDR addr, ULONGEST len)
4154 {
4155 struct bp_location *bl, **blp_tmp;
4156
4157 ALL_BP_LOCATIONS (bl, blp_tmp)
4158 {
4159 if (bl->loc_type != bp_loc_software_breakpoint
4160 && bl->loc_type != bp_loc_hardware_breakpoint)
4161 continue;
4162
4163 if ((breakpoint_enabled (bl->owner)
4164 || bl->permanent)
4165 && breakpoint_location_address_range_overlap (bl, aspace,
4166 addr, len))
4167 {
4168 if (overlay_debugging
4169 && section_is_overlay (bl->section)
4170 && !section_is_mapped (bl->section))
4171 {
4172 /* Unmapped overlay -- can't be a match. */
4173 continue;
4174 }
4175
4176 return 1;
4177 }
4178 }
4179
4180 return 0;
4181 }
4182
4183 /* Return true if there's a moribund breakpoint at PC. */
4184
4185 int
4186 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4187 {
4188 for (bp_location *loc : moribund_locations)
4189 if (breakpoint_location_address_match (loc, aspace, pc))
4190 return 1;
4191
4192 return 0;
4193 }
4194
4195 /* Returns non-zero iff BL is inserted at PC, in address space
4196 ASPACE. */
4197
4198 static int
4199 bp_location_inserted_here_p (struct bp_location *bl,
4200 const address_space *aspace, CORE_ADDR pc)
4201 {
4202 if (bl->inserted
4203 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4204 aspace, pc))
4205 {
4206 if (overlay_debugging
4207 && section_is_overlay (bl->section)
4208 && !section_is_mapped (bl->section))
4209 return 0; /* unmapped overlay -- can't be a match */
4210 else
4211 return 1;
4212 }
4213 return 0;
4214 }
4215
4216 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4217
4218 int
4219 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4220 {
4221 struct bp_location **blp, **blp_tmp = NULL;
4222
4223 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4224 {
4225 struct bp_location *bl = *blp;
4226
4227 if (bl->loc_type != bp_loc_software_breakpoint
4228 && bl->loc_type != bp_loc_hardware_breakpoint)
4229 continue;
4230
4231 if (bp_location_inserted_here_p (bl, aspace, pc))
4232 return 1;
4233 }
4234 return 0;
4235 }
4236
4237 /* This function returns non-zero iff there is a software breakpoint
4238 inserted at PC. */
4239
4240 int
4241 software_breakpoint_inserted_here_p (const address_space *aspace,
4242 CORE_ADDR pc)
4243 {
4244 struct bp_location **blp, **blp_tmp = NULL;
4245
4246 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4247 {
4248 struct bp_location *bl = *blp;
4249
4250 if (bl->loc_type != bp_loc_software_breakpoint)
4251 continue;
4252
4253 if (bp_location_inserted_here_p (bl, aspace, pc))
4254 return 1;
4255 }
4256
4257 return 0;
4258 }
4259
4260 /* See breakpoint.h. */
4261
4262 int
4263 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4264 CORE_ADDR pc)
4265 {
4266 struct bp_location **blp, **blp_tmp = NULL;
4267
4268 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4269 {
4270 struct bp_location *bl = *blp;
4271
4272 if (bl->loc_type != bp_loc_hardware_breakpoint)
4273 continue;
4274
4275 if (bp_location_inserted_here_p (bl, aspace, pc))
4276 return 1;
4277 }
4278
4279 return 0;
4280 }
4281
4282 int
4283 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4284 CORE_ADDR addr, ULONGEST len)
4285 {
4286 for (breakpoint *bpt : all_breakpoints ())
4287 {
4288 if (bpt->type != bp_hardware_watchpoint
4289 && bpt->type != bp_access_watchpoint)
4290 continue;
4291
4292 if (!breakpoint_enabled (bpt))
4293 continue;
4294
4295 for (bp_location *loc : bpt->locations ())
4296 if (loc->pspace->aspace == aspace && loc->inserted)
4297 {
4298 CORE_ADDR l, h;
4299
4300 /* Check for intersection. */
4301 l = std::max<CORE_ADDR> (loc->address, addr);
4302 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4303 if (l < h)
4304 return 1;
4305 }
4306 }
4307 return 0;
4308 }
4309
4310 /* See breakpoint.h. */
4311
4312 bool
4313 is_catchpoint (struct breakpoint *b)
4314 {
4315 return (b->type == bp_catchpoint);
4316 }
4317
4318 /* Clear a bpstat so that it says we are not at any breakpoint.
4319 Also free any storage that is part of a bpstat. */
4320
4321 void
4322 bpstat_clear (bpstat *bsp)
4323 {
4324 bpstat p;
4325 bpstat q;
4326
4327 if (bsp == 0)
4328 return;
4329 p = *bsp;
4330 while (p != NULL)
4331 {
4332 q = p->next;
4333 delete p;
4334 p = q;
4335 }
4336 *bsp = NULL;
4337 }
4338
4339 bpstats::bpstats (const bpstats &other)
4340 : next (NULL),
4341 bp_location_at (other.bp_location_at),
4342 breakpoint_at (other.breakpoint_at),
4343 commands (other.commands),
4344 print (other.print),
4345 stop (other.stop),
4346 print_it (other.print_it)
4347 {
4348 if (other.old_val != NULL)
4349 old_val = release_value (value_copy (other.old_val.get ()));
4350 }
4351
4352 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4353 is part of the bpstat is copied as well. */
4354
4355 bpstat
4356 bpstat_copy (bpstat bs)
4357 {
4358 bpstat p = NULL;
4359 bpstat tmp;
4360 bpstat retval = NULL;
4361
4362 if (bs == NULL)
4363 return bs;
4364
4365 for (; bs != NULL; bs = bs->next)
4366 {
4367 tmp = new bpstats (*bs);
4368
4369 if (p == NULL)
4370 /* This is the first thing in the chain. */
4371 retval = tmp;
4372 else
4373 p->next = tmp;
4374 p = tmp;
4375 }
4376 p->next = NULL;
4377 return retval;
4378 }
4379
4380 /* Find the bpstat associated with this breakpoint. */
4381
4382 bpstat
4383 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4384 {
4385 if (bsp == NULL)
4386 return NULL;
4387
4388 for (; bsp != NULL; bsp = bsp->next)
4389 {
4390 if (bsp->breakpoint_at == breakpoint)
4391 return bsp;
4392 }
4393 return NULL;
4394 }
4395
4396 /* See breakpoint.h. */
4397
4398 bool
4399 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4400 {
4401 for (; bsp != NULL; bsp = bsp->next)
4402 {
4403 if (bsp->breakpoint_at == NULL)
4404 {
4405 /* A moribund location can never explain a signal other than
4406 GDB_SIGNAL_TRAP. */
4407 if (sig == GDB_SIGNAL_TRAP)
4408 return true;
4409 }
4410 else
4411 {
4412 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4413 sig))
4414 return true;
4415 }
4416 }
4417
4418 return false;
4419 }
4420
4421 /* Put in *NUM the breakpoint number of the first breakpoint we are
4422 stopped at. *BSP upon return is a bpstat which points to the
4423 remaining breakpoints stopped at (but which is not guaranteed to be
4424 good for anything but further calls to bpstat_num).
4425
4426 Return 0 if passed a bpstat which does not indicate any breakpoints.
4427 Return -1 if stopped at a breakpoint that has been deleted since
4428 we set it.
4429 Return 1 otherwise. */
4430
4431 int
4432 bpstat_num (bpstat *bsp, int *num)
4433 {
4434 struct breakpoint *b;
4435
4436 if ((*bsp) == NULL)
4437 return 0; /* No more breakpoint values */
4438
4439 /* We assume we'll never have several bpstats that correspond to a
4440 single breakpoint -- otherwise, this function might return the
4441 same number more than once and this will look ugly. */
4442 b = (*bsp)->breakpoint_at;
4443 *bsp = (*bsp)->next;
4444 if (b == NULL)
4445 return -1; /* breakpoint that's been deleted since */
4446
4447 *num = b->number; /* We have its number */
4448 return 1;
4449 }
4450
4451 /* See breakpoint.h. */
4452
4453 void
4454 bpstat_clear_actions (void)
4455 {
4456 bpstat bs;
4457
4458 if (inferior_ptid == null_ptid)
4459 return;
4460
4461 thread_info *tp = inferior_thread ();
4462 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4463 {
4464 bs->commands = NULL;
4465 bs->old_val.reset (nullptr);
4466 }
4467 }
4468
4469 /* Called when a command is about to proceed the inferior. */
4470
4471 static void
4472 breakpoint_about_to_proceed (void)
4473 {
4474 if (inferior_ptid != null_ptid)
4475 {
4476 struct thread_info *tp = inferior_thread ();
4477
4478 /* Allow inferior function calls in breakpoint commands to not
4479 interrupt the command list. When the call finishes
4480 successfully, the inferior will be standing at the same
4481 breakpoint as if nothing happened. */
4482 if (tp->control.in_infcall)
4483 return;
4484 }
4485
4486 breakpoint_proceeded = 1;
4487 }
4488
4489 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4490 or its equivalent. */
4491
4492 static int
4493 command_line_is_silent (struct command_line *cmd)
4494 {
4495 return cmd && (strcmp ("silent", cmd->line) == 0);
4496 }
4497
4498 /* Execute all the commands associated with all the breakpoints at
4499 this location. Any of these commands could cause the process to
4500 proceed beyond this point, etc. We look out for such changes by
4501 checking the global "breakpoint_proceeded" after each command.
4502
4503 Returns true if a breakpoint command resumed the inferior. In that
4504 case, it is the caller's responsibility to recall it again with the
4505 bpstat of the current thread. */
4506
4507 static int
4508 bpstat_do_actions_1 (bpstat *bsp)
4509 {
4510 bpstat bs;
4511 int again = 0;
4512
4513 /* Avoid endless recursion if a `source' command is contained
4514 in bs->commands. */
4515 if (executing_breakpoint_commands)
4516 return 0;
4517
4518 scoped_restore save_executing
4519 = make_scoped_restore (&executing_breakpoint_commands, 1);
4520
4521 scoped_restore preventer = prevent_dont_repeat ();
4522
4523 /* This pointer will iterate over the list of bpstat's. */
4524 bs = *bsp;
4525
4526 breakpoint_proceeded = 0;
4527 for (; bs != NULL; bs = bs->next)
4528 {
4529 struct command_line *cmd = NULL;
4530
4531 /* Take ownership of the BSP's command tree, if it has one.
4532
4533 The command tree could legitimately contain commands like
4534 'step' and 'next', which call clear_proceed_status, which
4535 frees stop_bpstat's command tree. To make sure this doesn't
4536 free the tree we're executing out from under us, we need to
4537 take ownership of the tree ourselves. Since a given bpstat's
4538 commands are only executed once, we don't need to copy it; we
4539 can clear the pointer in the bpstat, and make sure we free
4540 the tree when we're done. */
4541 counted_command_line ccmd = bs->commands;
4542 bs->commands = NULL;
4543 if (ccmd != NULL)
4544 cmd = ccmd.get ();
4545 if (command_line_is_silent (cmd))
4546 {
4547 /* The action has been already done by bpstat_stop_status. */
4548 cmd = cmd->next;
4549 }
4550
4551 while (cmd != NULL)
4552 {
4553 execute_control_command (cmd);
4554
4555 if (breakpoint_proceeded)
4556 break;
4557 else
4558 cmd = cmd->next;
4559 }
4560
4561 if (breakpoint_proceeded)
4562 {
4563 if (current_ui->async)
4564 /* If we are in async mode, then the target might be still
4565 running, not stopped at any breakpoint, so nothing for
4566 us to do here -- just return to the event loop. */
4567 ;
4568 else
4569 /* In sync mode, when execute_control_command returns
4570 we're already standing on the next breakpoint.
4571 Breakpoint commands for that stop were not run, since
4572 execute_command does not run breakpoint commands --
4573 only command_line_handler does, but that one is not
4574 involved in execution of breakpoint commands. So, we
4575 can now execute breakpoint commands. It should be
4576 noted that making execute_command do bpstat actions is
4577 not an option -- in this case we'll have recursive
4578 invocation of bpstat for each breakpoint with a
4579 command, and can easily blow up GDB stack. Instead, we
4580 return true, which will trigger the caller to recall us
4581 with the new stop_bpstat. */
4582 again = 1;
4583 break;
4584 }
4585 }
4586 return again;
4587 }
4588
4589 /* Helper for bpstat_do_actions. Get the current thread, if there's
4590 one, is alive and has execution. Return NULL otherwise. */
4591
4592 static thread_info *
4593 get_bpstat_thread ()
4594 {
4595 if (inferior_ptid == null_ptid || !target_has_execution ())
4596 return NULL;
4597
4598 thread_info *tp = inferior_thread ();
4599 if (tp->state == THREAD_EXITED || tp->executing)
4600 return NULL;
4601 return tp;
4602 }
4603
4604 void
4605 bpstat_do_actions (void)
4606 {
4607 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4608 thread_info *tp;
4609
4610 /* Do any commands attached to breakpoint we are stopped at. */
4611 while ((tp = get_bpstat_thread ()) != NULL)
4612 {
4613 /* Since in sync mode, bpstat_do_actions may resume the
4614 inferior, and only return when it is stopped at the next
4615 breakpoint, we keep doing breakpoint actions until it returns
4616 false to indicate the inferior was not resumed. */
4617 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4618 break;
4619 }
4620
4621 cleanup_if_error.release ();
4622 }
4623
4624 /* Print out the (old or new) value associated with a watchpoint. */
4625
4626 static void
4627 watchpoint_value_print (struct value *val, struct ui_file *stream)
4628 {
4629 if (val == NULL)
4630 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4631 else
4632 {
4633 struct value_print_options opts;
4634 get_user_print_options (&opts);
4635 value_print (val, stream, &opts);
4636 }
4637 }
4638
4639 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4640 debugging multiple threads. */
4641
4642 void
4643 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4644 {
4645 if (uiout->is_mi_like_p ())
4646 return;
4647
4648 uiout->text ("\n");
4649
4650 if (show_thread_that_caused_stop ())
4651 {
4652 const char *name;
4653 struct thread_info *thr = inferior_thread ();
4654
4655 uiout->text ("Thread ");
4656 uiout->field_string ("thread-id", print_thread_id (thr));
4657
4658 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4659 if (name != NULL)
4660 {
4661 uiout->text (" \"");
4662 uiout->field_string ("name", name);
4663 uiout->text ("\"");
4664 }
4665
4666 uiout->text (" hit ");
4667 }
4668 }
4669
4670 /* Generic routine for printing messages indicating why we
4671 stopped. The behavior of this function depends on the value
4672 'print_it' in the bpstat structure. Under some circumstances we
4673 may decide not to print anything here and delegate the task to
4674 normal_stop(). */
4675
4676 static enum print_stop_action
4677 print_bp_stop_message (bpstat bs)
4678 {
4679 switch (bs->print_it)
4680 {
4681 case print_it_noop:
4682 /* Nothing should be printed for this bpstat entry. */
4683 return PRINT_UNKNOWN;
4684 break;
4685
4686 case print_it_done:
4687 /* We still want to print the frame, but we already printed the
4688 relevant messages. */
4689 return PRINT_SRC_AND_LOC;
4690 break;
4691
4692 case print_it_normal:
4693 {
4694 struct breakpoint *b = bs->breakpoint_at;
4695
4696 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4697 which has since been deleted. */
4698 if (b == NULL)
4699 return PRINT_UNKNOWN;
4700
4701 /* Normal case. Call the breakpoint's print_it method. */
4702 return b->ops->print_it (bs);
4703 }
4704 break;
4705
4706 default:
4707 internal_error (__FILE__, __LINE__,
4708 _("print_bp_stop_message: unrecognized enum value"));
4709 break;
4710 }
4711 }
4712
4713 /* A helper function that prints a shared library stopped event. */
4714
4715 static void
4716 print_solib_event (int is_catchpoint)
4717 {
4718 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4719 bool any_added = !current_program_space->added_solibs.empty ();
4720
4721 if (!is_catchpoint)
4722 {
4723 if (any_added || any_deleted)
4724 current_uiout->text (_("Stopped due to shared library event:\n"));
4725 else
4726 current_uiout->text (_("Stopped due to shared library event (no "
4727 "libraries added or removed)\n"));
4728 }
4729
4730 if (current_uiout->is_mi_like_p ())
4731 current_uiout->field_string ("reason",
4732 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4733
4734 if (any_deleted)
4735 {
4736 current_uiout->text (_(" Inferior unloaded "));
4737 ui_out_emit_list list_emitter (current_uiout, "removed");
4738 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4739 {
4740 const std::string &name = current_program_space->deleted_solibs[ix];
4741
4742 if (ix > 0)
4743 current_uiout->text (" ");
4744 current_uiout->field_string ("library", name);
4745 current_uiout->text ("\n");
4746 }
4747 }
4748
4749 if (any_added)
4750 {
4751 current_uiout->text (_(" Inferior loaded "));
4752 ui_out_emit_list list_emitter (current_uiout, "added");
4753 bool first = true;
4754 for (so_list *iter : current_program_space->added_solibs)
4755 {
4756 if (!first)
4757 current_uiout->text (" ");
4758 first = false;
4759 current_uiout->field_string ("library", iter->so_name);
4760 current_uiout->text ("\n");
4761 }
4762 }
4763 }
4764
4765 /* Print a message indicating what happened. This is called from
4766 normal_stop(). The input to this routine is the head of the bpstat
4767 list - a list of the eventpoints that caused this stop. KIND is
4768 the target_waitkind for the stopping event. This
4769 routine calls the generic print routine for printing a message
4770 about reasons for stopping. This will print (for example) the
4771 "Breakpoint n," part of the output. The return value of this
4772 routine is one of:
4773
4774 PRINT_UNKNOWN: Means we printed nothing.
4775 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4776 code to print the location. An example is
4777 "Breakpoint 1, " which should be followed by
4778 the location.
4779 PRINT_SRC_ONLY: Means we printed something, but there is no need
4780 to also print the location part of the message.
4781 An example is the catch/throw messages, which
4782 don't require a location appended to the end.
4783 PRINT_NOTHING: We have done some printing and we don't need any
4784 further info to be printed. */
4785
4786 enum print_stop_action
4787 bpstat_print (bpstat bs, int kind)
4788 {
4789 enum print_stop_action val;
4790
4791 /* Maybe another breakpoint in the chain caused us to stop.
4792 (Currently all watchpoints go on the bpstat whether hit or not.
4793 That probably could (should) be changed, provided care is taken
4794 with respect to bpstat_explains_signal). */
4795 for (; bs; bs = bs->next)
4796 {
4797 val = print_bp_stop_message (bs);
4798 if (val == PRINT_SRC_ONLY
4799 || val == PRINT_SRC_AND_LOC
4800 || val == PRINT_NOTHING)
4801 return val;
4802 }
4803
4804 /* If we had hit a shared library event breakpoint,
4805 print_bp_stop_message would print out this message. If we hit an
4806 OS-level shared library event, do the same thing. */
4807 if (kind == TARGET_WAITKIND_LOADED)
4808 {
4809 print_solib_event (0);
4810 return PRINT_NOTHING;
4811 }
4812
4813 /* We reached the end of the chain, or we got a null BS to start
4814 with and nothing was printed. */
4815 return PRINT_UNKNOWN;
4816 }
4817
4818 /* Evaluate the boolean expression EXP and return the result. */
4819
4820 static bool
4821 breakpoint_cond_eval (expression *exp)
4822 {
4823 struct value *mark = value_mark ();
4824 bool res = value_true (evaluate_expression (exp));
4825
4826 value_free_to_mark (mark);
4827 return res;
4828 }
4829
4830 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4831
4832 bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4833 : next (NULL),
4834 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
4835 breakpoint_at (bl->owner),
4836 commands (NULL),
4837 print (0),
4838 stop (0),
4839 print_it (print_it_normal)
4840 {
4841 **bs_link_pointer = this;
4842 *bs_link_pointer = &next;
4843 }
4844
4845 bpstats::bpstats ()
4846 : next (NULL),
4847 breakpoint_at (NULL),
4848 commands (NULL),
4849 print (0),
4850 stop (0),
4851 print_it (print_it_normal)
4852 {
4853 }
4854 \f
4855 /* The target has stopped with waitstatus WS. Check if any hardware
4856 watchpoints have triggered, according to the target. */
4857
4858 int
4859 watchpoints_triggered (struct target_waitstatus *ws)
4860 {
4861 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
4862 CORE_ADDR addr;
4863
4864 if (!stopped_by_watchpoint)
4865 {
4866 /* We were not stopped by a watchpoint. Mark all watchpoints
4867 as not triggered. */
4868 for (breakpoint *b : all_breakpoints ())
4869 if (is_hardware_watchpoint (b))
4870 {
4871 struct watchpoint *w = (struct watchpoint *) b;
4872
4873 w->watchpoint_triggered = watch_triggered_no;
4874 }
4875
4876 return 0;
4877 }
4878
4879 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
4880 {
4881 /* We were stopped by a watchpoint, but we don't know where.
4882 Mark all watchpoints as unknown. */
4883 for (breakpoint *b : all_breakpoints ())
4884 if (is_hardware_watchpoint (b))
4885 {
4886 struct watchpoint *w = (struct watchpoint *) b;
4887
4888 w->watchpoint_triggered = watch_triggered_unknown;
4889 }
4890
4891 return 1;
4892 }
4893
4894 /* The target could report the data address. Mark watchpoints
4895 affected by this data address as triggered, and all others as not
4896 triggered. */
4897
4898 for (breakpoint *b : all_breakpoints ())
4899 if (is_hardware_watchpoint (b))
4900 {
4901 struct watchpoint *w = (struct watchpoint *) b;
4902
4903 w->watchpoint_triggered = watch_triggered_no;
4904 for (bp_location *loc : b->locations ())
4905 {
4906 if (is_masked_watchpoint (b))
4907 {
4908 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4909 CORE_ADDR start = loc->address & w->hw_wp_mask;
4910
4911 if (newaddr == start)
4912 {
4913 w->watchpoint_triggered = watch_triggered_yes;
4914 break;
4915 }
4916 }
4917 /* Exact match not required. Within range is sufficient. */
4918 else if (target_watchpoint_addr_within_range
4919 (current_inferior ()->top_target (), addr, loc->address,
4920 loc->length))
4921 {
4922 w->watchpoint_triggered = watch_triggered_yes;
4923 break;
4924 }
4925 }
4926 }
4927
4928 return 1;
4929 }
4930
4931 /* Possible return values for watchpoint_check. */
4932 enum wp_check_result
4933 {
4934 /* The watchpoint has been deleted. */
4935 WP_DELETED = 1,
4936
4937 /* The value has changed. */
4938 WP_VALUE_CHANGED = 2,
4939
4940 /* The value has not changed. */
4941 WP_VALUE_NOT_CHANGED = 3,
4942
4943 /* Ignore this watchpoint, no matter if the value changed or not. */
4944 WP_IGNORE = 4,
4945 };
4946
4947 #define BP_TEMPFLAG 1
4948 #define BP_HARDWAREFLAG 2
4949
4950 /* Evaluate watchpoint condition expression and check if its value
4951 changed. */
4952
4953 static wp_check_result
4954 watchpoint_check (bpstat bs)
4955 {
4956 struct watchpoint *b;
4957 struct frame_info *fr;
4958 int within_current_scope;
4959
4960 /* BS is built from an existing struct breakpoint. */
4961 gdb_assert (bs->breakpoint_at != NULL);
4962 b = (struct watchpoint *) bs->breakpoint_at;
4963
4964 /* If this is a local watchpoint, we only want to check if the
4965 watchpoint frame is in scope if the current thread is the thread
4966 that was used to create the watchpoint. */
4967 if (!watchpoint_in_thread_scope (b))
4968 return WP_IGNORE;
4969
4970 if (b->exp_valid_block == NULL)
4971 within_current_scope = 1;
4972 else
4973 {
4974 struct frame_info *frame = get_current_frame ();
4975 struct gdbarch *frame_arch = get_frame_arch (frame);
4976 CORE_ADDR frame_pc = get_frame_pc (frame);
4977
4978 /* stack_frame_destroyed_p() returns a non-zero value if we're
4979 still in the function but the stack frame has already been
4980 invalidated. Since we can't rely on the values of local
4981 variables after the stack has been destroyed, we are treating
4982 the watchpoint in that state as `not changed' without further
4983 checking. Don't mark watchpoints as changed if the current
4984 frame is in an epilogue - even if they are in some other
4985 frame, our view of the stack is likely to be wrong and
4986 frame_find_by_id could error out. */
4987 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
4988 return WP_IGNORE;
4989
4990 fr = frame_find_by_id (b->watchpoint_frame);
4991 within_current_scope = (fr != NULL);
4992
4993 /* If we've gotten confused in the unwinder, we might have
4994 returned a frame that can't describe this variable. */
4995 if (within_current_scope)
4996 {
4997 struct symbol *function;
4998
4999 function = get_frame_function (fr);
5000 if (function == NULL
5001 || !contained_in (b->exp_valid_block,
5002 SYMBOL_BLOCK_VALUE (function)))
5003 within_current_scope = 0;
5004 }
5005
5006 if (within_current_scope)
5007 /* If we end up stopping, the current frame will get selected
5008 in normal_stop. So this call to select_frame won't affect
5009 the user. */
5010 select_frame (fr);
5011 }
5012
5013 if (within_current_scope)
5014 {
5015 /* We use value_{,free_to_}mark because it could be a *long*
5016 time before we return to the command level and call
5017 free_all_values. We can't call free_all_values because we
5018 might be in the middle of evaluating a function call. */
5019
5020 struct value *mark;
5021 struct value *new_val;
5022
5023 if (is_masked_watchpoint (b))
5024 /* Since we don't know the exact trigger address (from
5025 stopped_data_address), just tell the user we've triggered
5026 a mask watchpoint. */
5027 return WP_VALUE_CHANGED;
5028
5029 mark = value_mark ();
5030 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
5031 NULL, NULL, false);
5032
5033 if (b->val_bitsize != 0)
5034 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5035
5036 /* We use value_equal_contents instead of value_equal because
5037 the latter coerces an array to a pointer, thus comparing just
5038 the address of the array instead of its contents. This is
5039 not what we want. */
5040 if ((b->val != NULL) != (new_val != NULL)
5041 || (b->val != NULL && !value_equal_contents (b->val.get (),
5042 new_val)))
5043 {
5044 bs->old_val = b->val;
5045 b->val = release_value (new_val);
5046 b->val_valid = true;
5047 if (new_val != NULL)
5048 value_free_to_mark (mark);
5049 return WP_VALUE_CHANGED;
5050 }
5051 else
5052 {
5053 /* Nothing changed. */
5054 value_free_to_mark (mark);
5055 return WP_VALUE_NOT_CHANGED;
5056 }
5057 }
5058 else
5059 {
5060 /* This seems like the only logical thing to do because
5061 if we temporarily ignored the watchpoint, then when
5062 we reenter the block in which it is valid it contains
5063 garbage (in the case of a function, it may have two
5064 garbage values, one before and one after the prologue).
5065 So we can't even detect the first assignment to it and
5066 watch after that (since the garbage may or may not equal
5067 the first value assigned). */
5068 /* We print all the stop information in
5069 breakpoint_ops->print_it, but in this case, by the time we
5070 call breakpoint_ops->print_it this bp will be deleted
5071 already. So we have no choice but print the information
5072 here. */
5073
5074 SWITCH_THRU_ALL_UIS ()
5075 {
5076 struct ui_out *uiout = current_uiout;
5077
5078 if (uiout->is_mi_like_p ())
5079 uiout->field_string
5080 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5081 uiout->message ("\nWatchpoint %pF deleted because the program has "
5082 "left the block in\n"
5083 "which its expression is valid.\n",
5084 signed_field ("wpnum", b->number));
5085 }
5086
5087 /* Make sure the watchpoint's commands aren't executed. */
5088 b->commands = NULL;
5089 watchpoint_del_at_next_stop (b);
5090
5091 return WP_DELETED;
5092 }
5093 }
5094
5095 /* Return true if it looks like target has stopped due to hitting
5096 breakpoint location BL. This function does not check if we should
5097 stop, only if BL explains the stop. */
5098
5099 static int
5100 bpstat_check_location (const struct bp_location *bl,
5101 const address_space *aspace, CORE_ADDR bp_addr,
5102 const struct target_waitstatus *ws)
5103 {
5104 struct breakpoint *b = bl->owner;
5105
5106 /* BL is from an existing breakpoint. */
5107 gdb_assert (b != NULL);
5108
5109 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5110 }
5111
5112 /* Determine if the watched values have actually changed, and we
5113 should stop. If not, set BS->stop to 0. */
5114
5115 static void
5116 bpstat_check_watchpoint (bpstat bs)
5117 {
5118 const struct bp_location *bl;
5119 struct watchpoint *b;
5120
5121 /* BS is built for existing struct breakpoint. */
5122 bl = bs->bp_location_at.get ();
5123 gdb_assert (bl != NULL);
5124 b = (struct watchpoint *) bs->breakpoint_at;
5125 gdb_assert (b != NULL);
5126
5127 {
5128 int must_check_value = 0;
5129
5130 if (b->type == bp_watchpoint)
5131 /* For a software watchpoint, we must always check the
5132 watched value. */
5133 must_check_value = 1;
5134 else if (b->watchpoint_triggered == watch_triggered_yes)
5135 /* We have a hardware watchpoint (read, write, or access)
5136 and the target earlier reported an address watched by
5137 this watchpoint. */
5138 must_check_value = 1;
5139 else if (b->watchpoint_triggered == watch_triggered_unknown
5140 && b->type == bp_hardware_watchpoint)
5141 /* We were stopped by a hardware watchpoint, but the target could
5142 not report the data address. We must check the watchpoint's
5143 value. Access and read watchpoints are out of luck; without
5144 a data address, we can't figure it out. */
5145 must_check_value = 1;
5146
5147 if (must_check_value)
5148 {
5149 wp_check_result e;
5150
5151 try
5152 {
5153 e = watchpoint_check (bs);
5154 }
5155 catch (const gdb_exception &ex)
5156 {
5157 exception_fprintf (gdb_stderr, ex,
5158 "Error evaluating expression "
5159 "for watchpoint %d\n",
5160 b->number);
5161
5162 SWITCH_THRU_ALL_UIS ()
5163 {
5164 printf_filtered (_("Watchpoint %d deleted.\n"),
5165 b->number);
5166 }
5167 watchpoint_del_at_next_stop (b);
5168 e = WP_DELETED;
5169 }
5170
5171 switch (e)
5172 {
5173 case WP_DELETED:
5174 /* We've already printed what needs to be printed. */
5175 bs->print_it = print_it_done;
5176 /* Stop. */
5177 break;
5178 case WP_IGNORE:
5179 bs->print_it = print_it_noop;
5180 bs->stop = 0;
5181 break;
5182 case WP_VALUE_CHANGED:
5183 if (b->type == bp_read_watchpoint)
5184 {
5185 /* There are two cases to consider here:
5186
5187 1. We're watching the triggered memory for reads.
5188 In that case, trust the target, and always report
5189 the watchpoint hit to the user. Even though
5190 reads don't cause value changes, the value may
5191 have changed since the last time it was read, and
5192 since we're not trapping writes, we will not see
5193 those, and as such we should ignore our notion of
5194 old value.
5195
5196 2. We're watching the triggered memory for both
5197 reads and writes. There are two ways this may
5198 happen:
5199
5200 2.1. This is a target that can't break on data
5201 reads only, but can break on accesses (reads or
5202 writes), such as e.g., x86. We detect this case
5203 at the time we try to insert read watchpoints.
5204
5205 2.2. Otherwise, the target supports read
5206 watchpoints, but, the user set an access or write
5207 watchpoint watching the same memory as this read
5208 watchpoint.
5209
5210 If we're watching memory writes as well as reads,
5211 ignore watchpoint hits when we find that the
5212 value hasn't changed, as reads don't cause
5213 changes. This still gives false positives when
5214 the program writes the same value to memory as
5215 what there was already in memory (we will confuse
5216 it for a read), but it's much better than
5217 nothing. */
5218
5219 int other_write_watchpoint = 0;
5220
5221 if (bl->watchpoint_type == hw_read)
5222 {
5223 for (breakpoint *other_b : all_breakpoints ())
5224 if (other_b->type == bp_hardware_watchpoint
5225 || other_b->type == bp_access_watchpoint)
5226 {
5227 struct watchpoint *other_w =
5228 (struct watchpoint *) other_b;
5229
5230 if (other_w->watchpoint_triggered
5231 == watch_triggered_yes)
5232 {
5233 other_write_watchpoint = 1;
5234 break;
5235 }
5236 }
5237 }
5238
5239 if (other_write_watchpoint
5240 || bl->watchpoint_type == hw_access)
5241 {
5242 /* We're watching the same memory for writes,
5243 and the value changed since the last time we
5244 updated it, so this trap must be for a write.
5245 Ignore it. */
5246 bs->print_it = print_it_noop;
5247 bs->stop = 0;
5248 }
5249 }
5250 break;
5251 case WP_VALUE_NOT_CHANGED:
5252 if (b->type == bp_hardware_watchpoint
5253 || b->type == bp_watchpoint)
5254 {
5255 /* Don't stop: write watchpoints shouldn't fire if
5256 the value hasn't changed. */
5257 bs->print_it = print_it_noop;
5258 bs->stop = 0;
5259 }
5260 /* Stop. */
5261 break;
5262 default:
5263 /* Can't happen. */
5264 break;
5265 }
5266 }
5267 else /* must_check_value == 0 */
5268 {
5269 /* This is a case where some watchpoint(s) triggered, but
5270 not at the address of this watchpoint, or else no
5271 watchpoint triggered after all. So don't print
5272 anything for this watchpoint. */
5273 bs->print_it = print_it_noop;
5274 bs->stop = 0;
5275 }
5276 }
5277 }
5278
5279 /* For breakpoints that are currently marked as telling gdb to stop,
5280 check conditions (condition proper, frame, thread and ignore count)
5281 of breakpoint referred to by BS. If we should not stop for this
5282 breakpoint, set BS->stop to 0. */
5283
5284 static void
5285 bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
5286 {
5287 const struct bp_location *bl;
5288 struct breakpoint *b;
5289 /* Assume stop. */
5290 bool condition_result = true;
5291 struct expression *cond;
5292
5293 gdb_assert (bs->stop);
5294
5295 /* BS is built for existing struct breakpoint. */
5296 bl = bs->bp_location_at.get ();
5297 gdb_assert (bl != NULL);
5298 b = bs->breakpoint_at;
5299 gdb_assert (b != NULL);
5300
5301 /* Even if the target evaluated the condition on its end and notified GDB, we
5302 need to do so again since GDB does not know if we stopped due to a
5303 breakpoint or a single step breakpoint. */
5304
5305 if (frame_id_p (b->frame_id)
5306 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5307 {
5308 bs->stop = 0;
5309 return;
5310 }
5311
5312 /* If this is a thread/task-specific breakpoint, don't waste cpu
5313 evaluating the condition if this isn't the specified
5314 thread/task. */
5315 if ((b->thread != -1 && b->thread != thread->global_num)
5316 || (b->task != 0 && b->task != ada_get_task_number (thread)))
5317 {
5318 bs->stop = 0;
5319 return;
5320 }
5321
5322 /* Evaluate extension language breakpoints that have a "stop" method
5323 implemented. */
5324 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5325
5326 if (is_watchpoint (b))
5327 {
5328 struct watchpoint *w = (struct watchpoint *) b;
5329
5330 cond = w->cond_exp.get ();
5331 }
5332 else
5333 cond = bl->cond.get ();
5334
5335 if (cond && b->disposition != disp_del_at_next_stop)
5336 {
5337 int within_current_scope = 1;
5338 struct watchpoint * w;
5339
5340 /* We use value_mark and value_free_to_mark because it could
5341 be a long time before we return to the command level and
5342 call free_all_values. We can't call free_all_values
5343 because we might be in the middle of evaluating a
5344 function call. */
5345 struct value *mark = value_mark ();
5346
5347 if (is_watchpoint (b))
5348 w = (struct watchpoint *) b;
5349 else
5350 w = NULL;
5351
5352 /* Need to select the frame, with all that implies so that
5353 the conditions will have the right context. Because we
5354 use the frame, we will not see an inlined function's
5355 variables when we arrive at a breakpoint at the start
5356 of the inlined function; the current frame will be the
5357 call site. */
5358 if (w == NULL || w->cond_exp_valid_block == NULL)
5359 select_frame (get_current_frame ());
5360 else
5361 {
5362 struct frame_info *frame;
5363
5364 /* For local watchpoint expressions, which particular
5365 instance of a local is being watched matters, so we
5366 keep track of the frame to evaluate the expression
5367 in. To evaluate the condition however, it doesn't
5368 really matter which instantiation of the function
5369 where the condition makes sense triggers the
5370 watchpoint. This allows an expression like "watch
5371 global if q > 10" set in `func', catch writes to
5372 global on all threads that call `func', or catch
5373 writes on all recursive calls of `func' by a single
5374 thread. We simply always evaluate the condition in
5375 the innermost frame that's executing where it makes
5376 sense to evaluate the condition. It seems
5377 intuitive. */
5378 frame = block_innermost_frame (w->cond_exp_valid_block);
5379 if (frame != NULL)
5380 select_frame (frame);
5381 else
5382 within_current_scope = 0;
5383 }
5384 if (within_current_scope)
5385 {
5386 try
5387 {
5388 condition_result = breakpoint_cond_eval (cond);
5389 }
5390 catch (const gdb_exception &ex)
5391 {
5392 exception_fprintf (gdb_stderr, ex,
5393 "Error in testing breakpoint condition:\n");
5394 }
5395 }
5396 else
5397 {
5398 warning (_("Watchpoint condition cannot be tested "
5399 "in the current scope"));
5400 /* If we failed to set the right context for this
5401 watchpoint, unconditionally report it. */
5402 }
5403 /* FIXME-someday, should give breakpoint #. */
5404 value_free_to_mark (mark);
5405 }
5406
5407 if (cond && !condition_result)
5408 {
5409 bs->stop = 0;
5410 }
5411 else if (b->ignore_count > 0)
5412 {
5413 b->ignore_count--;
5414 bs->stop = 0;
5415 /* Increase the hit count even though we don't stop. */
5416 ++(b->hit_count);
5417 gdb::observers::breakpoint_modified.notify (b);
5418 }
5419 }
5420
5421 /* Returns true if we need to track moribund locations of LOC's type
5422 on the current target. */
5423
5424 static int
5425 need_moribund_for_location_type (struct bp_location *loc)
5426 {
5427 return ((loc->loc_type == bp_loc_software_breakpoint
5428 && !target_supports_stopped_by_sw_breakpoint ())
5429 || (loc->loc_type == bp_loc_hardware_breakpoint
5430 && !target_supports_stopped_by_hw_breakpoint ()));
5431 }
5432
5433 /* See breakpoint.h. */
5434
5435 bpstat
5436 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5437 const struct target_waitstatus *ws)
5438 {
5439 bpstat bs_head = NULL, *bs_link = &bs_head;
5440
5441 for (breakpoint *b : all_breakpoints ())
5442 {
5443 if (!breakpoint_enabled (b))
5444 continue;
5445
5446 for (bp_location *bl : b->locations ())
5447 {
5448 /* For hardware watchpoints, we look only at the first
5449 location. The watchpoint_check function will work on the
5450 entire expression, not the individual locations. For
5451 read watchpoints, the watchpoints_triggered function has
5452 checked all locations already. */
5453 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5454 break;
5455
5456 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
5457 continue;
5458
5459 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5460 continue;
5461
5462 /* Come here if it's a watchpoint, or if the break address
5463 matches. */
5464
5465 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5466 explain stop. */
5467
5468 /* Assume we stop. Should we find a watchpoint that is not
5469 actually triggered, or if the condition of the breakpoint
5470 evaluates as false, we'll reset 'stop' to 0. */
5471 bs->stop = 1;
5472 bs->print = 1;
5473
5474 /* If this is a scope breakpoint, mark the associated
5475 watchpoint as triggered so that we will handle the
5476 out-of-scope event. We'll get to the watchpoint next
5477 iteration. */
5478 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5479 {
5480 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5481
5482 w->watchpoint_triggered = watch_triggered_yes;
5483 }
5484 }
5485 }
5486
5487 /* Check if a moribund breakpoint explains the stop. */
5488 if (!target_supports_stopped_by_sw_breakpoint ()
5489 || !target_supports_stopped_by_hw_breakpoint ())
5490 {
5491 for (bp_location *loc : moribund_locations)
5492 {
5493 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5494 && need_moribund_for_location_type (loc))
5495 {
5496 bpstat bs = new bpstats (loc, &bs_link);
5497 /* For hits of moribund locations, we should just proceed. */
5498 bs->stop = 0;
5499 bs->print = 0;
5500 bs->print_it = print_it_noop;
5501 }
5502 }
5503 }
5504
5505 return bs_head;
5506 }
5507
5508 /* See breakpoint.h. */
5509
5510 bpstat
5511 bpstat_stop_status (const address_space *aspace,
5512 CORE_ADDR bp_addr, thread_info *thread,
5513 const struct target_waitstatus *ws,
5514 bpstat stop_chain)
5515 {
5516 struct breakpoint *b = NULL;
5517 /* First item of allocated bpstat's. */
5518 bpstat bs_head = stop_chain;
5519 bpstat bs;
5520 int need_remove_insert;
5521 int removed_any;
5522
5523 /* First, build the bpstat chain with locations that explain a
5524 target stop, while being careful to not set the target running,
5525 as that may invalidate locations (in particular watchpoint
5526 locations are recreated). Resuming will happen here with
5527 breakpoint conditions or watchpoint expressions that include
5528 inferior function calls. */
5529 if (bs_head == NULL)
5530 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5531
5532 /* A bit of special processing for shlib breakpoints. We need to
5533 process solib loading here, so that the lists of loaded and
5534 unloaded libraries are correct before we handle "catch load" and
5535 "catch unload". */
5536 for (bs = bs_head; bs != NULL; bs = bs->next)
5537 {
5538 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5539 {
5540 handle_solib_event ();
5541 break;
5542 }
5543 }
5544
5545 /* Now go through the locations that caused the target to stop, and
5546 check whether we're interested in reporting this stop to higher
5547 layers, or whether we should resume the target transparently. */
5548
5549 removed_any = 0;
5550
5551 for (bs = bs_head; bs != NULL; bs = bs->next)
5552 {
5553 if (!bs->stop)
5554 continue;
5555
5556 b = bs->breakpoint_at;
5557 b->ops->check_status (bs);
5558 if (bs->stop)
5559 {
5560 bpstat_check_breakpoint_conditions (bs, thread);
5561
5562 if (bs->stop)
5563 {
5564 ++(b->hit_count);
5565 gdb::observers::breakpoint_modified.notify (b);
5566
5567 /* We will stop here. */
5568 if (b->disposition == disp_disable)
5569 {
5570 --(b->enable_count);
5571 if (b->enable_count <= 0)
5572 b->enable_state = bp_disabled;
5573 removed_any = 1;
5574 }
5575 if (b->silent)
5576 bs->print = 0;
5577 bs->commands = b->commands;
5578 if (command_line_is_silent (bs->commands
5579 ? bs->commands.get () : NULL))
5580 bs->print = 0;
5581
5582 b->ops->after_condition_true (bs);
5583 }
5584
5585 }
5586
5587 /* Print nothing for this entry if we don't stop or don't
5588 print. */
5589 if (!bs->stop || !bs->print)
5590 bs->print_it = print_it_noop;
5591 }
5592
5593 /* If we aren't stopping, the value of some hardware watchpoint may
5594 not have changed, but the intermediate memory locations we are
5595 watching may have. Don't bother if we're stopping; this will get
5596 done later. */
5597 need_remove_insert = 0;
5598 if (! bpstat_causes_stop (bs_head))
5599 for (bs = bs_head; bs != NULL; bs = bs->next)
5600 if (!bs->stop
5601 && bs->breakpoint_at
5602 && is_hardware_watchpoint (bs->breakpoint_at))
5603 {
5604 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5605
5606 update_watchpoint (w, 0 /* don't reparse. */);
5607 need_remove_insert = 1;
5608 }
5609
5610 if (need_remove_insert)
5611 update_global_location_list (UGLL_MAY_INSERT);
5612 else if (removed_any)
5613 update_global_location_list (UGLL_DONT_INSERT);
5614
5615 return bs_head;
5616 }
5617
5618 static void
5619 handle_jit_event (CORE_ADDR address)
5620 {
5621 struct gdbarch *gdbarch;
5622
5623 infrun_debug_printf ("handling bp_jit_event");
5624
5625 /* Switch terminal for any messages produced by
5626 breakpoint_re_set. */
5627 target_terminal::ours_for_output ();
5628
5629 gdbarch = get_frame_arch (get_current_frame ());
5630 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5631 thus it is expected that its objectfile can be found through
5632 minimal symbol lookup. If it doesn't work (and assert fails), it
5633 most likely means that `jit_breakpoint_re_set` was changes and this
5634 function needs to be updated too. */
5635 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5636 gdb_assert (jit_bp_sym.objfile != nullptr);
5637 jit_event_handler (gdbarch, jit_bp_sym.objfile);
5638
5639 target_terminal::inferior ();
5640 }
5641
5642 /* Prepare WHAT final decision for infrun. */
5643
5644 /* Decide what infrun needs to do with this bpstat. */
5645
5646 struct bpstat_what
5647 bpstat_what (bpstat bs_head)
5648 {
5649 struct bpstat_what retval;
5650 bpstat bs;
5651
5652 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5653 retval.call_dummy = STOP_NONE;
5654 retval.is_longjmp = false;
5655
5656 for (bs = bs_head; bs != NULL; bs = bs->next)
5657 {
5658 /* Extract this BS's action. After processing each BS, we check
5659 if its action overrides all we've seem so far. */
5660 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5661 enum bptype bptype;
5662
5663 if (bs->breakpoint_at == NULL)
5664 {
5665 /* I suspect this can happen if it was a momentary
5666 breakpoint which has since been deleted. */
5667 bptype = bp_none;
5668 }
5669 else
5670 bptype = bs->breakpoint_at->type;
5671
5672 switch (bptype)
5673 {
5674 case bp_none:
5675 break;
5676 case bp_breakpoint:
5677 case bp_hardware_breakpoint:
5678 case bp_single_step:
5679 case bp_until:
5680 case bp_finish:
5681 case bp_shlib_event:
5682 if (bs->stop)
5683 {
5684 if (bs->print)
5685 this_action = BPSTAT_WHAT_STOP_NOISY;
5686 else
5687 this_action = BPSTAT_WHAT_STOP_SILENT;
5688 }
5689 else
5690 this_action = BPSTAT_WHAT_SINGLE;
5691 break;
5692 case bp_watchpoint:
5693 case bp_hardware_watchpoint:
5694 case bp_read_watchpoint:
5695 case bp_access_watchpoint:
5696 if (bs->stop)
5697 {
5698 if (bs->print)
5699 this_action = BPSTAT_WHAT_STOP_NOISY;
5700 else
5701 this_action = BPSTAT_WHAT_STOP_SILENT;
5702 }
5703 else
5704 {
5705 /* There was a watchpoint, but we're not stopping.
5706 This requires no further action. */
5707 }
5708 break;
5709 case bp_longjmp:
5710 case bp_longjmp_call_dummy:
5711 case bp_exception:
5712 if (bs->stop)
5713 {
5714 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5715 retval.is_longjmp = bptype != bp_exception;
5716 }
5717 else
5718 this_action = BPSTAT_WHAT_SINGLE;
5719 break;
5720 case bp_longjmp_resume:
5721 case bp_exception_resume:
5722 if (bs->stop)
5723 {
5724 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5725 retval.is_longjmp = bptype == bp_longjmp_resume;
5726 }
5727 else
5728 this_action = BPSTAT_WHAT_SINGLE;
5729 break;
5730 case bp_step_resume:
5731 if (bs->stop)
5732 this_action = BPSTAT_WHAT_STEP_RESUME;
5733 else
5734 {
5735 /* It is for the wrong frame. */
5736 this_action = BPSTAT_WHAT_SINGLE;
5737 }
5738 break;
5739 case bp_hp_step_resume:
5740 if (bs->stop)
5741 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5742 else
5743 {
5744 /* It is for the wrong frame. */
5745 this_action = BPSTAT_WHAT_SINGLE;
5746 }
5747 break;
5748 case bp_watchpoint_scope:
5749 case bp_thread_event:
5750 case bp_overlay_event:
5751 case bp_longjmp_master:
5752 case bp_std_terminate_master:
5753 case bp_exception_master:
5754 this_action = BPSTAT_WHAT_SINGLE;
5755 break;
5756 case bp_catchpoint:
5757 if (bs->stop)
5758 {
5759 if (bs->print)
5760 this_action = BPSTAT_WHAT_STOP_NOISY;
5761 else
5762 this_action = BPSTAT_WHAT_STOP_SILENT;
5763 }
5764 else
5765 {
5766 /* Some catchpoints are implemented with breakpoints.
5767 For those, we need to step over the breakpoint. */
5768 if (bs->bp_location_at->loc_type != bp_loc_other)
5769 this_action = BPSTAT_WHAT_SINGLE;
5770 }
5771 break;
5772 case bp_jit_event:
5773 this_action = BPSTAT_WHAT_SINGLE;
5774 break;
5775 case bp_call_dummy:
5776 /* Make sure the action is stop (silent or noisy),
5777 so infrun.c pops the dummy frame. */
5778 retval.call_dummy = STOP_STACK_DUMMY;
5779 this_action = BPSTAT_WHAT_STOP_SILENT;
5780 break;
5781 case bp_std_terminate:
5782 /* Make sure the action is stop (silent or noisy),
5783 so infrun.c pops the dummy frame. */
5784 retval.call_dummy = STOP_STD_TERMINATE;
5785 this_action = BPSTAT_WHAT_STOP_SILENT;
5786 break;
5787 case bp_tracepoint:
5788 case bp_fast_tracepoint:
5789 case bp_static_tracepoint:
5790 /* Tracepoint hits should not be reported back to GDB, and
5791 if one got through somehow, it should have been filtered
5792 out already. */
5793 internal_error (__FILE__, __LINE__,
5794 _("bpstat_what: tracepoint encountered"));
5795 break;
5796 case bp_gnu_ifunc_resolver:
5797 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5798 this_action = BPSTAT_WHAT_SINGLE;
5799 break;
5800 case bp_gnu_ifunc_resolver_return:
5801 /* The breakpoint will be removed, execution will restart from the
5802 PC of the former breakpoint. */
5803 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5804 break;
5805
5806 case bp_dprintf:
5807 if (bs->stop)
5808 this_action = BPSTAT_WHAT_STOP_SILENT;
5809 else
5810 this_action = BPSTAT_WHAT_SINGLE;
5811 break;
5812
5813 default:
5814 internal_error (__FILE__, __LINE__,
5815 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5816 }
5817
5818 retval.main_action = std::max (retval.main_action, this_action);
5819 }
5820
5821 return retval;
5822 }
5823
5824 void
5825 bpstat_run_callbacks (bpstat bs_head)
5826 {
5827 bpstat bs;
5828
5829 for (bs = bs_head; bs != NULL; bs = bs->next)
5830 {
5831 struct breakpoint *b = bs->breakpoint_at;
5832
5833 if (b == NULL)
5834 continue;
5835 switch (b->type)
5836 {
5837 case bp_jit_event:
5838 handle_jit_event (bs->bp_location_at->address);
5839 break;
5840 case bp_gnu_ifunc_resolver:
5841 gnu_ifunc_resolver_stop (b);
5842 break;
5843 case bp_gnu_ifunc_resolver_return:
5844 gnu_ifunc_resolver_return_stop (b);
5845 break;
5846 }
5847 }
5848 }
5849
5850 /* See breakpoint.h. */
5851
5852 bool
5853 bpstat_should_step ()
5854 {
5855 for (breakpoint *b : all_breakpoints ())
5856 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5857 return true;
5858
5859 return false;
5860 }
5861
5862 /* See breakpoint.h. */
5863
5864 bool
5865 bpstat_causes_stop (bpstat bs)
5866 {
5867 for (; bs != NULL; bs = bs->next)
5868 if (bs->stop)
5869 return true;
5870
5871 return false;
5872 }
5873
5874 \f
5875
5876 /* Compute a string of spaces suitable to indent the next line
5877 so it starts at the position corresponding to the table column
5878 named COL_NAME in the currently active table of UIOUT. */
5879
5880 static char *
5881 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5882 {
5883 static char wrap_indent[80];
5884 int i, total_width, width, align;
5885 const char *text;
5886
5887 total_width = 0;
5888 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5889 {
5890 if (strcmp (text, col_name) == 0)
5891 {
5892 gdb_assert (total_width < sizeof wrap_indent);
5893 memset (wrap_indent, ' ', total_width);
5894 wrap_indent[total_width] = 0;
5895
5896 return wrap_indent;
5897 }
5898
5899 total_width += width + 1;
5900 }
5901
5902 return NULL;
5903 }
5904
5905 /* Determine if the locations of this breakpoint will have their conditions
5906 evaluated by the target, host or a mix of both. Returns the following:
5907
5908 "host": Host evals condition.
5909 "host or target": Host or Target evals condition.
5910 "target": Target evals condition.
5911 */
5912
5913 static const char *
5914 bp_condition_evaluator (struct breakpoint *b)
5915 {
5916 char host_evals = 0;
5917 char target_evals = 0;
5918
5919 if (!b)
5920 return NULL;
5921
5922 if (!is_breakpoint (b))
5923 return NULL;
5924
5925 if (gdb_evaluates_breakpoint_condition_p ()
5926 || !target_supports_evaluation_of_breakpoint_conditions ())
5927 return condition_evaluation_host;
5928
5929 for (bp_location *bl : b->locations ())
5930 {
5931 if (bl->cond_bytecode)
5932 target_evals++;
5933 else
5934 host_evals++;
5935 }
5936
5937 if (host_evals && target_evals)
5938 return condition_evaluation_both;
5939 else if (target_evals)
5940 return condition_evaluation_target;
5941 else
5942 return condition_evaluation_host;
5943 }
5944
5945 /* Determine the breakpoint location's condition evaluator. This is
5946 similar to bp_condition_evaluator, but for locations. */
5947
5948 static const char *
5949 bp_location_condition_evaluator (struct bp_location *bl)
5950 {
5951 if (bl && !is_breakpoint (bl->owner))
5952 return NULL;
5953
5954 if (gdb_evaluates_breakpoint_condition_p ()
5955 || !target_supports_evaluation_of_breakpoint_conditions ())
5956 return condition_evaluation_host;
5957
5958 if (bl && bl->cond_bytecode)
5959 return condition_evaluation_target;
5960 else
5961 return condition_evaluation_host;
5962 }
5963
5964 /* Print the LOC location out of the list of B->LOC locations. */
5965
5966 static void
5967 print_breakpoint_location (struct breakpoint *b,
5968 struct bp_location *loc)
5969 {
5970 struct ui_out *uiout = current_uiout;
5971
5972 scoped_restore_current_program_space restore_pspace;
5973
5974 if (loc != NULL && loc->shlib_disabled)
5975 loc = NULL;
5976
5977 if (loc != NULL)
5978 set_current_program_space (loc->pspace);
5979
5980 if (b->display_canonical)
5981 uiout->field_string ("what", event_location_to_string (b->location.get ()));
5982 else if (loc && loc->symtab)
5983 {
5984 const struct symbol *sym = loc->symbol;
5985
5986 if (sym)
5987 {
5988 uiout->text ("in ");
5989 uiout->field_string ("func", sym->print_name (),
5990 function_name_style.style ());
5991 uiout->text (" ");
5992 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5993 uiout->text ("at ");
5994 }
5995 uiout->field_string ("file",
5996 symtab_to_filename_for_display (loc->symtab),
5997 file_name_style.style ());
5998 uiout->text (":");
5999
6000 if (uiout->is_mi_like_p ())
6001 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
6002
6003 uiout->field_signed ("line", loc->line_number);
6004 }
6005 else if (loc)
6006 {
6007 string_file stb;
6008
6009 print_address_symbolic (loc->gdbarch, loc->address, &stb,
6010 demangle, "");
6011 uiout->field_stream ("at", stb);
6012 }
6013 else
6014 {
6015 uiout->field_string ("pending",
6016 event_location_to_string (b->location.get ()));
6017 /* If extra_string is available, it could be holding a condition
6018 or dprintf arguments. In either case, make sure it is printed,
6019 too, but only for non-MI streams. */
6020 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
6021 {
6022 if (b->type == bp_dprintf)
6023 uiout->text (",");
6024 else
6025 uiout->text (" ");
6026 uiout->text (b->extra_string);
6027 }
6028 }
6029
6030 if (loc && is_breakpoint (b)
6031 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6032 && bp_condition_evaluator (b) == condition_evaluation_both)
6033 {
6034 uiout->text (" (");
6035 uiout->field_string ("evaluated-by",
6036 bp_location_condition_evaluator (loc));
6037 uiout->text (")");
6038 }
6039 }
6040
6041 static const char *
6042 bptype_string (enum bptype type)
6043 {
6044 struct ep_type_description
6045 {
6046 enum bptype type;
6047 const char *description;
6048 };
6049 static struct ep_type_description bptypes[] =
6050 {
6051 {bp_none, "?deleted?"},
6052 {bp_breakpoint, "breakpoint"},
6053 {bp_hardware_breakpoint, "hw breakpoint"},
6054 {bp_single_step, "sw single-step"},
6055 {bp_until, "until"},
6056 {bp_finish, "finish"},
6057 {bp_watchpoint, "watchpoint"},
6058 {bp_hardware_watchpoint, "hw watchpoint"},
6059 {bp_read_watchpoint, "read watchpoint"},
6060 {bp_access_watchpoint, "acc watchpoint"},
6061 {bp_longjmp, "longjmp"},
6062 {bp_longjmp_resume, "longjmp resume"},
6063 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6064 {bp_exception, "exception"},
6065 {bp_exception_resume, "exception resume"},
6066 {bp_step_resume, "step resume"},
6067 {bp_hp_step_resume, "high-priority step resume"},
6068 {bp_watchpoint_scope, "watchpoint scope"},
6069 {bp_call_dummy, "call dummy"},
6070 {bp_std_terminate, "std::terminate"},
6071 {bp_shlib_event, "shlib events"},
6072 {bp_thread_event, "thread events"},
6073 {bp_overlay_event, "overlay events"},
6074 {bp_longjmp_master, "longjmp master"},
6075 {bp_std_terminate_master, "std::terminate master"},
6076 {bp_exception_master, "exception master"},
6077 {bp_catchpoint, "catchpoint"},
6078 {bp_tracepoint, "tracepoint"},
6079 {bp_fast_tracepoint, "fast tracepoint"},
6080 {bp_static_tracepoint, "static tracepoint"},
6081 {bp_dprintf, "dprintf"},
6082 {bp_jit_event, "jit events"},
6083 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6084 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6085 };
6086
6087 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6088 || ((int) type != bptypes[(int) type].type))
6089 internal_error (__FILE__, __LINE__,
6090 _("bptypes table does not describe type #%d."),
6091 (int) type);
6092
6093 return bptypes[(int) type].description;
6094 }
6095
6096 /* For MI, output a field named 'thread-groups' with a list as the value.
6097 For CLI, prefix the list with the string 'inf'. */
6098
6099 static void
6100 output_thread_groups (struct ui_out *uiout,
6101 const char *field_name,
6102 const std::vector<int> &inf_nums,
6103 int mi_only)
6104 {
6105 int is_mi = uiout->is_mi_like_p ();
6106
6107 /* For backward compatibility, don't display inferiors in CLI unless
6108 there are several. Always display them for MI. */
6109 if (!is_mi && mi_only)
6110 return;
6111
6112 ui_out_emit_list list_emitter (uiout, field_name);
6113
6114 for (size_t i = 0; i < inf_nums.size (); i++)
6115 {
6116 if (is_mi)
6117 {
6118 char mi_group[10];
6119
6120 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
6121 uiout->field_string (NULL, mi_group);
6122 }
6123 else
6124 {
6125 if (i == 0)
6126 uiout->text (" inf ");
6127 else
6128 uiout->text (", ");
6129
6130 uiout->text (plongest (inf_nums[i]));
6131 }
6132 }
6133 }
6134
6135 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6136 instead of going via breakpoint_ops::print_one. This makes "maint
6137 info breakpoints" show the software breakpoint locations of
6138 catchpoints, which are considered internal implementation
6139 detail. */
6140
6141 static void
6142 print_one_breakpoint_location (struct breakpoint *b,
6143 struct bp_location *loc,
6144 int loc_number,
6145 struct bp_location **last_loc,
6146 int allflag, bool raw_loc)
6147 {
6148 struct command_line *l;
6149 static char bpenables[] = "nynny";
6150
6151 struct ui_out *uiout = current_uiout;
6152 int header_of_multiple = 0;
6153 int part_of_multiple = (loc != NULL);
6154 struct value_print_options opts;
6155
6156 get_user_print_options (&opts);
6157
6158 gdb_assert (!loc || loc_number != 0);
6159 /* See comment in print_one_breakpoint concerning treatment of
6160 breakpoints with single disabled location. */
6161 if (loc == NULL
6162 && (b->loc != NULL
6163 && (b->loc->next != NULL
6164 || !b->loc->enabled || b->loc->disabled_by_cond)))
6165 header_of_multiple = 1;
6166 if (loc == NULL)
6167 loc = b->loc;
6168
6169 annotate_record ();
6170
6171 /* 1 */
6172 annotate_field (0);
6173 if (part_of_multiple)
6174 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6175 else
6176 uiout->field_signed ("number", b->number);
6177
6178 /* 2 */
6179 annotate_field (1);
6180 if (part_of_multiple)
6181 uiout->field_skip ("type");
6182 else
6183 uiout->field_string ("type", bptype_string (b->type));
6184
6185 /* 3 */
6186 annotate_field (2);
6187 if (part_of_multiple)
6188 uiout->field_skip ("disp");
6189 else
6190 uiout->field_string ("disp", bpdisp_text (b->disposition));
6191
6192 /* 4 */
6193 annotate_field (3);
6194 /* For locations that are disabled because of an invalid condition,
6195 display "N*" on CLI, where "*" refers to a footnote below the
6196 table. For MI, simply display a "N" without a footnote. */
6197 const char *N = (uiout->is_mi_like_p ()) ? "N" : "N*";
6198 if (part_of_multiple)
6199 uiout->field_string ("enabled", (loc->disabled_by_cond ? N
6200 : (loc->enabled ? "y" : "n")));
6201 else
6202 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6203
6204 /* 5 and 6 */
6205 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
6206 b->ops->print_one (b, last_loc);
6207 else
6208 {
6209 if (is_watchpoint (b))
6210 {
6211 struct watchpoint *w = (struct watchpoint *) b;
6212
6213 /* Field 4, the address, is omitted (which makes the columns
6214 not line up too nicely with the headers, but the effect
6215 is relatively readable). */
6216 if (opts.addressprint)
6217 uiout->field_skip ("addr");
6218 annotate_field (5);
6219 uiout->field_string ("what", w->exp_string);
6220 }
6221 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6222 || is_ada_exception_catchpoint (b))
6223 {
6224 if (opts.addressprint)
6225 {
6226 annotate_field (4);
6227 if (header_of_multiple)
6228 uiout->field_string ("addr", "<MULTIPLE>",
6229 metadata_style.style ());
6230 else if (b->loc == NULL || loc->shlib_disabled)
6231 uiout->field_string ("addr", "<PENDING>",
6232 metadata_style.style ());
6233 else
6234 uiout->field_core_addr ("addr",
6235 loc->gdbarch, loc->address);
6236 }
6237 annotate_field (5);
6238 if (!header_of_multiple)
6239 print_breakpoint_location (b, loc);
6240 if (b->loc)
6241 *last_loc = b->loc;
6242 }
6243 }
6244
6245 if (loc != NULL && !header_of_multiple)
6246 {
6247 std::vector<int> inf_nums;
6248 int mi_only = 1;
6249
6250 for (inferior *inf : all_inferiors ())
6251 {
6252 if (inf->pspace == loc->pspace)
6253 inf_nums.push_back (inf->num);
6254 }
6255
6256 /* For backward compatibility, don't display inferiors in CLI unless
6257 there are several. Always display for MI. */
6258 if (allflag
6259 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6260 && (program_spaces.size () > 1
6261 || number_of_inferiors () > 1)
6262 /* LOC is for existing B, it cannot be in
6263 moribund_locations and thus having NULL OWNER. */
6264 && loc->owner->type != bp_catchpoint))
6265 mi_only = 0;
6266 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6267 }
6268
6269 if (!part_of_multiple)
6270 {
6271 if (b->thread != -1)
6272 {
6273 /* FIXME: This seems to be redundant and lost here; see the
6274 "stop only in" line a little further down. */
6275 uiout->text (" thread ");
6276 uiout->field_signed ("thread", b->thread);
6277 }
6278 else if (b->task != 0)
6279 {
6280 uiout->text (" task ");
6281 uiout->field_signed ("task", b->task);
6282 }
6283 }
6284
6285 uiout->text ("\n");
6286
6287 if (!part_of_multiple)
6288 b->ops->print_one_detail (b, uiout);
6289
6290 if (part_of_multiple && frame_id_p (b->frame_id))
6291 {
6292 annotate_field (6);
6293 uiout->text ("\tstop only in stack frame at ");
6294 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6295 the frame ID. */
6296 uiout->field_core_addr ("frame",
6297 b->gdbarch, b->frame_id.stack_addr);
6298 uiout->text ("\n");
6299 }
6300
6301 if (!part_of_multiple && b->cond_string)
6302 {
6303 annotate_field (7);
6304 if (is_tracepoint (b))
6305 uiout->text ("\ttrace only if ");
6306 else
6307 uiout->text ("\tstop only if ");
6308 uiout->field_string ("cond", b->cond_string);
6309
6310 /* Print whether the target is doing the breakpoint's condition
6311 evaluation. If GDB is doing the evaluation, don't print anything. */
6312 if (is_breakpoint (b)
6313 && breakpoint_condition_evaluation_mode ()
6314 == condition_evaluation_target)
6315 {
6316 uiout->message (" (%pF evals)",
6317 string_field ("evaluated-by",
6318 bp_condition_evaluator (b)));
6319 }
6320 uiout->text ("\n");
6321 }
6322
6323 if (!part_of_multiple && b->thread != -1)
6324 {
6325 /* FIXME should make an annotation for this. */
6326 uiout->text ("\tstop only in thread ");
6327 if (uiout->is_mi_like_p ())
6328 uiout->field_signed ("thread", b->thread);
6329 else
6330 {
6331 struct thread_info *thr = find_thread_global_id (b->thread);
6332
6333 uiout->field_string ("thread", print_thread_id (thr));
6334 }
6335 uiout->text ("\n");
6336 }
6337
6338 if (!part_of_multiple)
6339 {
6340 if (b->hit_count)
6341 {
6342 /* FIXME should make an annotation for this. */
6343 if (is_catchpoint (b))
6344 uiout->text ("\tcatchpoint");
6345 else if (is_tracepoint (b))
6346 uiout->text ("\ttracepoint");
6347 else
6348 uiout->text ("\tbreakpoint");
6349 uiout->text (" already hit ");
6350 uiout->field_signed ("times", b->hit_count);
6351 if (b->hit_count == 1)
6352 uiout->text (" time\n");
6353 else
6354 uiout->text (" times\n");
6355 }
6356 else
6357 {
6358 /* Output the count also if it is zero, but only if this is mi. */
6359 if (uiout->is_mi_like_p ())
6360 uiout->field_signed ("times", b->hit_count);
6361 }
6362 }
6363
6364 if (!part_of_multiple && b->ignore_count)
6365 {
6366 annotate_field (8);
6367 uiout->message ("\tignore next %pF hits\n",
6368 signed_field ("ignore", b->ignore_count));
6369 }
6370
6371 /* Note that an enable count of 1 corresponds to "enable once"
6372 behavior, which is reported by the combination of enablement and
6373 disposition, so we don't need to mention it here. */
6374 if (!part_of_multiple && b->enable_count > 1)
6375 {
6376 annotate_field (8);
6377 uiout->text ("\tdisable after ");
6378 /* Tweak the wording to clarify that ignore and enable counts
6379 are distinct, and have additive effect. */
6380 if (b->ignore_count)
6381 uiout->text ("additional ");
6382 else
6383 uiout->text ("next ");
6384 uiout->field_signed ("enable", b->enable_count);
6385 uiout->text (" hits\n");
6386 }
6387
6388 if (!part_of_multiple && is_tracepoint (b))
6389 {
6390 struct tracepoint *tp = (struct tracepoint *) b;
6391
6392 if (tp->traceframe_usage)
6393 {
6394 uiout->text ("\ttrace buffer usage ");
6395 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6396 uiout->text (" bytes\n");
6397 }
6398 }
6399
6400 l = b->commands ? b->commands.get () : NULL;
6401 if (!part_of_multiple && l)
6402 {
6403 annotate_field (9);
6404 ui_out_emit_tuple tuple_emitter (uiout, "script");
6405 print_command_lines (uiout, l, 4);
6406 }
6407
6408 if (is_tracepoint (b))
6409 {
6410 struct tracepoint *t = (struct tracepoint *) b;
6411
6412 if (!part_of_multiple && t->pass_count)
6413 {
6414 annotate_field (10);
6415 uiout->text ("\tpass count ");
6416 uiout->field_signed ("pass", t->pass_count);
6417 uiout->text (" \n");
6418 }
6419
6420 /* Don't display it when tracepoint or tracepoint location is
6421 pending. */
6422 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6423 {
6424 annotate_field (11);
6425
6426 if (uiout->is_mi_like_p ())
6427 uiout->field_string ("installed",
6428 loc->inserted ? "y" : "n");
6429 else
6430 {
6431 if (loc->inserted)
6432 uiout->text ("\t");
6433 else
6434 uiout->text ("\tnot ");
6435 uiout->text ("installed on target\n");
6436 }
6437 }
6438 }
6439
6440 if (uiout->is_mi_like_p () && !part_of_multiple)
6441 {
6442 if (is_watchpoint (b))
6443 {
6444 struct watchpoint *w = (struct watchpoint *) b;
6445
6446 uiout->field_string ("original-location", w->exp_string);
6447 }
6448 else if (b->location != NULL
6449 && event_location_to_string (b->location.get ()) != NULL)
6450 uiout->field_string ("original-location",
6451 event_location_to_string (b->location.get ()));
6452 }
6453 }
6454
6455 /* See breakpoint.h. */
6456
6457 bool fix_multi_location_breakpoint_output_globally = false;
6458
6459 static void
6460 print_one_breakpoint (struct breakpoint *b,
6461 struct bp_location **last_loc,
6462 int allflag)
6463 {
6464 struct ui_out *uiout = current_uiout;
6465 bool use_fixed_output
6466 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6467 || fix_multi_location_breakpoint_output_globally);
6468
6469 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6470 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
6471
6472 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6473 are outside. */
6474 if (!use_fixed_output)
6475 bkpt_tuple_emitter.reset ();
6476
6477 /* If this breakpoint has custom print function,
6478 it's already printed. Otherwise, print individual
6479 locations, if any. */
6480 if (b->ops == NULL
6481 || b->ops->print_one == NULL
6482 || allflag)
6483 {
6484 /* If breakpoint has a single location that is disabled, we
6485 print it as if it had several locations, since otherwise it's
6486 hard to represent "breakpoint enabled, location disabled"
6487 situation.
6488
6489 Note that while hardware watchpoints have several locations
6490 internally, that's not a property exposed to users.
6491
6492 Likewise, while catchpoints may be implemented with
6493 breakpoints (e.g., catch throw), that's not a property
6494 exposed to users. We do however display the internal
6495 breakpoint locations with "maint info breakpoints". */
6496 if (!is_hardware_watchpoint (b)
6497 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6498 || is_ada_exception_catchpoint (b))
6499 && (allflag
6500 || (b->loc && (b->loc->next
6501 || !b->loc->enabled
6502 || b->loc->disabled_by_cond))))
6503 {
6504 gdb::optional<ui_out_emit_list> locations_list;
6505
6506 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6507 MI record. For later versions, place breakpoint locations in a
6508 list. */
6509 if (uiout->is_mi_like_p () && use_fixed_output)
6510 locations_list.emplace (uiout, "locations");
6511
6512 int n = 1;
6513 for (bp_location *loc : b->locations ())
6514 {
6515 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6516 print_one_breakpoint_location (b, loc, n, last_loc,
6517 allflag, allflag);
6518 n++;
6519 }
6520 }
6521 }
6522 }
6523
6524 static int
6525 breakpoint_address_bits (struct breakpoint *b)
6526 {
6527 int print_address_bits = 0;
6528
6529 /* Software watchpoints that aren't watching memory don't have an
6530 address to print. */
6531 if (is_no_memory_software_watchpoint (b))
6532 return 0;
6533
6534 for (bp_location *loc : b->locations ())
6535 {
6536 int addr_bit;
6537
6538 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6539 if (addr_bit > print_address_bits)
6540 print_address_bits = addr_bit;
6541 }
6542
6543 return print_address_bits;
6544 }
6545
6546 /* See breakpoint.h. */
6547
6548 void
6549 print_breakpoint (breakpoint *b)
6550 {
6551 struct bp_location *dummy_loc = NULL;
6552 print_one_breakpoint (b, &dummy_loc, 0);
6553 }
6554
6555 /* Return true if this breakpoint was set by the user, false if it is
6556 internal or momentary. */
6557
6558 int
6559 user_breakpoint_p (struct breakpoint *b)
6560 {
6561 return b->number > 0;
6562 }
6563
6564 /* See breakpoint.h. */
6565
6566 int
6567 pending_breakpoint_p (struct breakpoint *b)
6568 {
6569 return b->loc == NULL;
6570 }
6571
6572 /* Print information on breakpoints (including watchpoints and tracepoints).
6573
6574 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6575 understood by number_or_range_parser. Only breakpoints included in this
6576 list are then printed.
6577
6578 If SHOW_INTERNAL is true, print internal breakpoints.
6579
6580 If FILTER is non-NULL, call it on each breakpoint and only include the
6581 ones for which it returns true.
6582
6583 Return the total number of breakpoints listed. */
6584
6585 static int
6586 breakpoint_1 (const char *bp_num_list, bool show_internal,
6587 bool (*filter) (const struct breakpoint *))
6588 {
6589 struct bp_location *last_loc = NULL;
6590 int nr_printable_breakpoints;
6591 struct value_print_options opts;
6592 int print_address_bits = 0;
6593 int print_type_col_width = 14;
6594 struct ui_out *uiout = current_uiout;
6595 bool has_disabled_by_cond_location = false;
6596
6597 get_user_print_options (&opts);
6598
6599 /* Compute the number of rows in the table, as well as the size
6600 required for address fields. */
6601 nr_printable_breakpoints = 0;
6602 for (breakpoint *b : all_breakpoints ())
6603 {
6604 /* If we have a filter, only list the breakpoints it accepts. */
6605 if (filter && !filter (b))
6606 continue;
6607
6608 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6609 accept. Skip the others. */
6610 if (bp_num_list != NULL && *bp_num_list != '\0')
6611 {
6612 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6613 continue;
6614 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6615 continue;
6616 }
6617
6618 if (show_internal || user_breakpoint_p (b))
6619 {
6620 int addr_bit, type_len;
6621
6622 addr_bit = breakpoint_address_bits (b);
6623 if (addr_bit > print_address_bits)
6624 print_address_bits = addr_bit;
6625
6626 type_len = strlen (bptype_string (b->type));
6627 if (type_len > print_type_col_width)
6628 print_type_col_width = type_len;
6629
6630 nr_printable_breakpoints++;
6631 }
6632 }
6633
6634 {
6635 ui_out_emit_table table_emitter (uiout,
6636 opts.addressprint ? 6 : 5,
6637 nr_printable_breakpoints,
6638 "BreakpointTable");
6639
6640 if (nr_printable_breakpoints > 0)
6641 annotate_breakpoints_headers ();
6642 if (nr_printable_breakpoints > 0)
6643 annotate_field (0);
6644 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6645 if (nr_printable_breakpoints > 0)
6646 annotate_field (1);
6647 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6648 if (nr_printable_breakpoints > 0)
6649 annotate_field (2);
6650 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6651 if (nr_printable_breakpoints > 0)
6652 annotate_field (3);
6653 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6654 if (opts.addressprint)
6655 {
6656 if (nr_printable_breakpoints > 0)
6657 annotate_field (4);
6658 if (print_address_bits <= 32)
6659 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6660 else
6661 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6662 }
6663 if (nr_printable_breakpoints > 0)
6664 annotate_field (5);
6665 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6666 uiout->table_body ();
6667 if (nr_printable_breakpoints > 0)
6668 annotate_breakpoints_table ();
6669
6670 for (breakpoint *b : all_breakpoints ())
6671 {
6672 QUIT;
6673 /* If we have a filter, only list the breakpoints it accepts. */
6674 if (filter && !filter (b))
6675 continue;
6676
6677 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6678 accept. Skip the others. */
6679
6680 if (bp_num_list != NULL && *bp_num_list != '\0')
6681 {
6682 if (show_internal) /* maintenance info breakpoint */
6683 {
6684 if (parse_and_eval_long (bp_num_list) != b->number)
6685 continue;
6686 }
6687 else /* all others */
6688 {
6689 if (!number_is_in_list (bp_num_list, b->number))
6690 continue;
6691 }
6692 }
6693 /* We only print out user settable breakpoints unless the
6694 show_internal is set. */
6695 if (show_internal || user_breakpoint_p (b))
6696 {
6697 print_one_breakpoint (b, &last_loc, show_internal);
6698 for (bp_location *loc : b->locations ())
6699 if (loc->disabled_by_cond)
6700 has_disabled_by_cond_location = true;
6701 }
6702 }
6703 }
6704
6705 if (nr_printable_breakpoints == 0)
6706 {
6707 /* If there's a filter, let the caller decide how to report
6708 empty list. */
6709 if (!filter)
6710 {
6711 if (bp_num_list == NULL || *bp_num_list == '\0')
6712 uiout->message ("No breakpoints or watchpoints.\n");
6713 else
6714 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6715 bp_num_list);
6716 }
6717 }
6718 else
6719 {
6720 if (last_loc && !server_command)
6721 set_next_address (last_loc->gdbarch, last_loc->address);
6722
6723 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
6724 uiout->message (_("(*): Breakpoint condition is invalid at this "
6725 "location.\n"));
6726 }
6727
6728 /* FIXME? Should this be moved up so that it is only called when
6729 there have been breakpoints? */
6730 annotate_breakpoints_table_end ();
6731
6732 return nr_printable_breakpoints;
6733 }
6734
6735 /* Display the value of default-collect in a way that is generally
6736 compatible with the breakpoint list. */
6737
6738 static void
6739 default_collect_info (void)
6740 {
6741 struct ui_out *uiout = current_uiout;
6742
6743 /* If it has no value (which is frequently the case), say nothing; a
6744 message like "No default-collect." gets in user's face when it's
6745 not wanted. */
6746 if (!*default_collect)
6747 return;
6748
6749 /* The following phrase lines up nicely with per-tracepoint collect
6750 actions. */
6751 uiout->text ("default collect ");
6752 uiout->field_string ("default-collect", default_collect);
6753 uiout->text (" \n");
6754 }
6755
6756 static void
6757 info_breakpoints_command (const char *args, int from_tty)
6758 {
6759 breakpoint_1 (args, false, NULL);
6760
6761 default_collect_info ();
6762 }
6763
6764 static void
6765 info_watchpoints_command (const char *args, int from_tty)
6766 {
6767 int num_printed = breakpoint_1 (args, false, is_watchpoint);
6768 struct ui_out *uiout = current_uiout;
6769
6770 if (num_printed == 0)
6771 {
6772 if (args == NULL || *args == '\0')
6773 uiout->message ("No watchpoints.\n");
6774 else
6775 uiout->message ("No watchpoint matching '%s'.\n", args);
6776 }
6777 }
6778
6779 static void
6780 maintenance_info_breakpoints (const char *args, int from_tty)
6781 {
6782 breakpoint_1 (args, true, NULL);
6783
6784 default_collect_info ();
6785 }
6786
6787 static int
6788 breakpoint_has_pc (struct breakpoint *b,
6789 struct program_space *pspace,
6790 CORE_ADDR pc, struct obj_section *section)
6791 {
6792 for (bp_location *bl : b->locations ())
6793 {
6794 if (bl->pspace == pspace
6795 && bl->address == pc
6796 && (!overlay_debugging || bl->section == section))
6797 return 1;
6798 }
6799 return 0;
6800 }
6801
6802 /* Print a message describing any user-breakpoints set at PC. This
6803 concerns with logical breakpoints, so we match program spaces, not
6804 address spaces. */
6805
6806 static void
6807 describe_other_breakpoints (struct gdbarch *gdbarch,
6808 struct program_space *pspace, CORE_ADDR pc,
6809 struct obj_section *section, int thread)
6810 {
6811 int others = 0;
6812
6813 for (breakpoint *b : all_breakpoints ())
6814 others += (user_breakpoint_p (b)
6815 && breakpoint_has_pc (b, pspace, pc, section));
6816
6817 if (others > 0)
6818 {
6819 if (others == 1)
6820 printf_filtered (_("Note: breakpoint "));
6821 else /* if (others == ???) */
6822 printf_filtered (_("Note: breakpoints "));
6823 for (breakpoint *b : all_breakpoints ())
6824 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6825 {
6826 others--;
6827 printf_filtered ("%d", b->number);
6828 if (b->thread == -1 && thread != -1)
6829 printf_filtered (" (all threads)");
6830 else if (b->thread != -1)
6831 printf_filtered (" (thread %d)", b->thread);
6832 printf_filtered ("%s%s ",
6833 ((b->enable_state == bp_disabled
6834 || b->enable_state == bp_call_disabled)
6835 ? " (disabled)"
6836 : ""),
6837 (others > 1) ? ","
6838 : ((others == 1) ? " and" : ""));
6839 }
6840 current_uiout->message (_("also set at pc %ps.\n"),
6841 styled_string (address_style.style (),
6842 paddress (gdbarch, pc)));
6843 }
6844 }
6845 \f
6846
6847 /* Return true iff it is meaningful to use the address member of LOC.
6848 For some breakpoint types, the locations' address members are
6849 irrelevant and it makes no sense to attempt to compare them to
6850 other addresses (or use them for any other purpose either).
6851
6852 More specifically, software watchpoints and catchpoints that are
6853 not backed by breakpoints always have a zero valued location
6854 address and we don't want to mark breakpoints of any of these types
6855 to be a duplicate of an actual breakpoint location at address
6856 zero. */
6857
6858 static bool
6859 bl_address_is_meaningful (bp_location *loc)
6860 {
6861 return loc->loc_type != bp_loc_other;
6862 }
6863
6864 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6865 true if LOC1 and LOC2 represent the same watchpoint location. */
6866
6867 static int
6868 watchpoint_locations_match (struct bp_location *loc1,
6869 struct bp_location *loc2)
6870 {
6871 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6872 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6873
6874 /* Both of them must exist. */
6875 gdb_assert (w1 != NULL);
6876 gdb_assert (w2 != NULL);
6877
6878 /* If the target can evaluate the condition expression in hardware,
6879 then we we need to insert both watchpoints even if they are at
6880 the same place. Otherwise the watchpoint will only trigger when
6881 the condition of whichever watchpoint was inserted evaluates to
6882 true, not giving a chance for GDB to check the condition of the
6883 other watchpoint. */
6884 if ((w1->cond_exp
6885 && target_can_accel_watchpoint_condition (loc1->address,
6886 loc1->length,
6887 loc1->watchpoint_type,
6888 w1->cond_exp.get ()))
6889 || (w2->cond_exp
6890 && target_can_accel_watchpoint_condition (loc2->address,
6891 loc2->length,
6892 loc2->watchpoint_type,
6893 w2->cond_exp.get ())))
6894 return 0;
6895
6896 /* Note that this checks the owner's type, not the location's. In
6897 case the target does not support read watchpoints, but does
6898 support access watchpoints, we'll have bp_read_watchpoint
6899 watchpoints with hw_access locations. Those should be considered
6900 duplicates of hw_read locations. The hw_read locations will
6901 become hw_access locations later. */
6902 return (loc1->owner->type == loc2->owner->type
6903 && loc1->pspace->aspace == loc2->pspace->aspace
6904 && loc1->address == loc2->address
6905 && loc1->length == loc2->length);
6906 }
6907
6908 /* See breakpoint.h. */
6909
6910 int
6911 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6912 const address_space *aspace2, CORE_ADDR addr2)
6913 {
6914 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6915 || aspace1 == aspace2)
6916 && addr1 == addr2);
6917 }
6918
6919 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6920 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6921 matches ASPACE2. On targets that have global breakpoints, the address
6922 space doesn't really matter. */
6923
6924 static int
6925 breakpoint_address_match_range (const address_space *aspace1,
6926 CORE_ADDR addr1,
6927 int len1, const address_space *aspace2,
6928 CORE_ADDR addr2)
6929 {
6930 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6931 || aspace1 == aspace2)
6932 && addr2 >= addr1 && addr2 < addr1 + len1);
6933 }
6934
6935 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6936 a ranged breakpoint. In most targets, a match happens only if ASPACE
6937 matches the breakpoint's address space. On targets that have global
6938 breakpoints, the address space doesn't really matter. */
6939
6940 static int
6941 breakpoint_location_address_match (struct bp_location *bl,
6942 const address_space *aspace,
6943 CORE_ADDR addr)
6944 {
6945 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6946 aspace, addr)
6947 || (bl->length
6948 && breakpoint_address_match_range (bl->pspace->aspace,
6949 bl->address, bl->length,
6950 aspace, addr)));
6951 }
6952
6953 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6954 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6955 match happens only if ASPACE matches the breakpoint's address
6956 space. On targets that have global breakpoints, the address space
6957 doesn't really matter. */
6958
6959 static int
6960 breakpoint_location_address_range_overlap (struct bp_location *bl,
6961 const address_space *aspace,
6962 CORE_ADDR addr, int len)
6963 {
6964 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6965 || bl->pspace->aspace == aspace)
6966 {
6967 int bl_len = bl->length != 0 ? bl->length : 1;
6968
6969 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6970 return 1;
6971 }
6972 return 0;
6973 }
6974
6975 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6976 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6977 true, otherwise returns false. */
6978
6979 static int
6980 tracepoint_locations_match (struct bp_location *loc1,
6981 struct bp_location *loc2)
6982 {
6983 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6984 /* Since tracepoint locations are never duplicated with others', tracepoint
6985 locations at the same address of different tracepoints are regarded as
6986 different locations. */
6987 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6988 else
6989 return 0;
6990 }
6991
6992 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6993 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
6994 the same location. If SW_HW_BPS_MATCH is true, then software
6995 breakpoint locations and hardware breakpoint locations match,
6996 otherwise they don't. */
6997
6998 static int
6999 breakpoint_locations_match (struct bp_location *loc1,
7000 struct bp_location *loc2,
7001 bool sw_hw_bps_match)
7002 {
7003 int hw_point1, hw_point2;
7004
7005 /* Both of them must not be in moribund_locations. */
7006 gdb_assert (loc1->owner != NULL);
7007 gdb_assert (loc2->owner != NULL);
7008
7009 hw_point1 = is_hardware_watchpoint (loc1->owner);
7010 hw_point2 = is_hardware_watchpoint (loc2->owner);
7011
7012 if (hw_point1 != hw_point2)
7013 return 0;
7014 else if (hw_point1)
7015 return watchpoint_locations_match (loc1, loc2);
7016 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7017 return tracepoint_locations_match (loc1, loc2);
7018 else
7019 /* We compare bp_location.length in order to cover ranged
7020 breakpoints. Keep this in sync with
7021 bp_location_is_less_than. */
7022 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7023 loc2->pspace->aspace, loc2->address)
7024 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
7025 && loc1->length == loc2->length);
7026 }
7027
7028 static void
7029 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7030 int bnum, int have_bnum)
7031 {
7032 /* The longest string possibly returned by hex_string_custom
7033 is 50 chars. These must be at least that big for safety. */
7034 char astr1[64];
7035 char astr2[64];
7036
7037 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7038 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7039 if (have_bnum)
7040 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7041 bnum, astr1, astr2);
7042 else
7043 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7044 }
7045
7046 /* Adjust a breakpoint's address to account for architectural
7047 constraints on breakpoint placement. Return the adjusted address.
7048 Note: Very few targets require this kind of adjustment. For most
7049 targets, this function is simply the identity function. */
7050
7051 static CORE_ADDR
7052 adjust_breakpoint_address (struct gdbarch *gdbarch,
7053 CORE_ADDR bpaddr, enum bptype bptype)
7054 {
7055 if (bptype == bp_watchpoint
7056 || bptype == bp_hardware_watchpoint
7057 || bptype == bp_read_watchpoint
7058 || bptype == bp_access_watchpoint
7059 || bptype == bp_catchpoint)
7060 {
7061 /* Watchpoints and the various bp_catch_* eventpoints should not
7062 have their addresses modified. */
7063 return bpaddr;
7064 }
7065 else if (bptype == bp_single_step)
7066 {
7067 /* Single-step breakpoints should not have their addresses
7068 modified. If there's any architectural constrain that
7069 applies to this address, then it should have already been
7070 taken into account when the breakpoint was created in the
7071 first place. If we didn't do this, stepping through e.g.,
7072 Thumb-2 IT blocks would break. */
7073 return bpaddr;
7074 }
7075 else
7076 {
7077 CORE_ADDR adjusted_bpaddr = bpaddr;
7078
7079 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7080 {
7081 /* Some targets have architectural constraints on the placement
7082 of breakpoint instructions. Obtain the adjusted address. */
7083 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7084 }
7085
7086 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
7087
7088 /* An adjusted breakpoint address can significantly alter
7089 a user's expectations. Print a warning if an adjustment
7090 is required. */
7091 if (adjusted_bpaddr != bpaddr)
7092 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7093
7094 return adjusted_bpaddr;
7095 }
7096 }
7097
7098 static bp_loc_type
7099 bp_location_from_bp_type (bptype type)
7100 {
7101 switch (type)
7102 {
7103 case bp_breakpoint:
7104 case bp_single_step:
7105 case bp_until:
7106 case bp_finish:
7107 case bp_longjmp:
7108 case bp_longjmp_resume:
7109 case bp_longjmp_call_dummy:
7110 case bp_exception:
7111 case bp_exception_resume:
7112 case bp_step_resume:
7113 case bp_hp_step_resume:
7114 case bp_watchpoint_scope:
7115 case bp_call_dummy:
7116 case bp_std_terminate:
7117 case bp_shlib_event:
7118 case bp_thread_event:
7119 case bp_overlay_event:
7120 case bp_jit_event:
7121 case bp_longjmp_master:
7122 case bp_std_terminate_master:
7123 case bp_exception_master:
7124 case bp_gnu_ifunc_resolver:
7125 case bp_gnu_ifunc_resolver_return:
7126 case bp_dprintf:
7127 return bp_loc_software_breakpoint;
7128 case bp_hardware_breakpoint:
7129 return bp_loc_hardware_breakpoint;
7130 case bp_hardware_watchpoint:
7131 case bp_read_watchpoint:
7132 case bp_access_watchpoint:
7133 return bp_loc_hardware_watchpoint;
7134 case bp_watchpoint:
7135 case bp_catchpoint:
7136 case bp_tracepoint:
7137 case bp_fast_tracepoint:
7138 case bp_static_tracepoint:
7139 return bp_loc_other;
7140 default:
7141 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7142 }
7143 }
7144
7145 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7146 {
7147 this->owner = owner;
7148 this->cond_bytecode = NULL;
7149 this->shlib_disabled = 0;
7150 this->enabled = 1;
7151 this->disabled_by_cond = false;
7152
7153 this->loc_type = type;
7154
7155 if (this->loc_type == bp_loc_software_breakpoint
7156 || this->loc_type == bp_loc_hardware_breakpoint)
7157 mark_breakpoint_location_modified (this);
7158
7159 incref ();
7160 }
7161
7162 bp_location::bp_location (breakpoint *owner)
7163 : bp_location::bp_location (owner,
7164 bp_location_from_bp_type (owner->type))
7165 {
7166 }
7167
7168 /* Allocate a struct bp_location. */
7169
7170 static struct bp_location *
7171 allocate_bp_location (struct breakpoint *bpt)
7172 {
7173 return bpt->ops->allocate_location (bpt);
7174 }
7175
7176 /* Decrement reference count. If the reference count reaches 0,
7177 destroy the bp_location. Sets *BLP to NULL. */
7178
7179 static void
7180 decref_bp_location (struct bp_location **blp)
7181 {
7182 bp_location_ref_policy::decref (*blp);
7183 *blp = NULL;
7184 }
7185
7186 /* Add breakpoint B at the end of the global breakpoint chain. */
7187
7188 static breakpoint *
7189 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7190 {
7191 struct breakpoint *b1;
7192 struct breakpoint *result = b.get ();
7193
7194 /* Add this breakpoint to the end of the chain so that a list of
7195 breakpoints will come out in order of increasing numbers. */
7196
7197 b1 = breakpoint_chain;
7198 if (b1 == 0)
7199 breakpoint_chain = b.release ();
7200 else
7201 {
7202 while (b1->next)
7203 b1 = b1->next;
7204 b1->next = b.release ();
7205 }
7206
7207 return result;
7208 }
7209
7210 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7211
7212 static void
7213 init_raw_breakpoint_without_location (struct breakpoint *b,
7214 struct gdbarch *gdbarch,
7215 enum bptype bptype,
7216 const struct breakpoint_ops *ops)
7217 {
7218 gdb_assert (ops != NULL);
7219
7220 b->ops = ops;
7221 b->type = bptype;
7222 b->gdbarch = gdbarch;
7223 b->language = current_language->la_language;
7224 b->input_radix = input_radix;
7225 b->related_breakpoint = b;
7226 }
7227
7228 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7229 that has type BPTYPE and has no locations as yet. */
7230
7231 static struct breakpoint *
7232 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7233 enum bptype bptype,
7234 const struct breakpoint_ops *ops)
7235 {
7236 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7237
7238 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7239 return add_to_breakpoint_chain (std::move (b));
7240 }
7241
7242 /* Initialize loc->function_name. */
7243
7244 static void
7245 set_breakpoint_location_function (struct bp_location *loc)
7246 {
7247 gdb_assert (loc->owner != NULL);
7248
7249 if (loc->owner->type == bp_breakpoint
7250 || loc->owner->type == bp_hardware_breakpoint
7251 || is_tracepoint (loc->owner))
7252 {
7253 const char *function_name;
7254
7255 if (loc->msymbol != NULL
7256 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
7257 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
7258 {
7259 struct breakpoint *b = loc->owner;
7260
7261 function_name = loc->msymbol->linkage_name ();
7262
7263 if (b->type == bp_breakpoint && b->loc == loc
7264 && loc->next == NULL && b->related_breakpoint == b)
7265 {
7266 /* Create only the whole new breakpoint of this type but do not
7267 mess more complicated breakpoints with multiple locations. */
7268 b->type = bp_gnu_ifunc_resolver;
7269 /* Remember the resolver's address for use by the return
7270 breakpoint. */
7271 loc->related_address = loc->address;
7272 }
7273 }
7274 else
7275 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7276
7277 if (function_name)
7278 loc->function_name = xstrdup (function_name);
7279 }
7280 }
7281
7282 /* Attempt to determine architecture of location identified by SAL. */
7283 struct gdbarch *
7284 get_sal_arch (struct symtab_and_line sal)
7285 {
7286 if (sal.section)
7287 return sal.section->objfile->arch ();
7288 if (sal.symtab)
7289 return SYMTAB_OBJFILE (sal.symtab)->arch ();
7290
7291 return NULL;
7292 }
7293
7294 /* Low level routine for partially initializing a breakpoint of type
7295 BPTYPE. The newly created breakpoint's address, section, source
7296 file name, and line number are provided by SAL.
7297
7298 It is expected that the caller will complete the initialization of
7299 the newly created breakpoint struct as well as output any status
7300 information regarding the creation of a new breakpoint. */
7301
7302 static void
7303 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7304 struct symtab_and_line sal, enum bptype bptype,
7305 const struct breakpoint_ops *ops)
7306 {
7307 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7308
7309 add_location_to_breakpoint (b, &sal);
7310
7311 if (bptype != bp_catchpoint)
7312 gdb_assert (sal.pspace != NULL);
7313
7314 /* Store the program space that was used to set the breakpoint,
7315 except for ordinary breakpoints, which are independent of the
7316 program space. */
7317 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7318 b->pspace = sal.pspace;
7319 }
7320
7321 /* set_raw_breakpoint is a low level routine for allocating and
7322 partially initializing a breakpoint of type BPTYPE. The newly
7323 created breakpoint's address, section, source file name, and line
7324 number are provided by SAL. The newly created and partially
7325 initialized breakpoint is added to the breakpoint chain and
7326 is also returned as the value of this function.
7327
7328 It is expected that the caller will complete the initialization of
7329 the newly created breakpoint struct as well as output any status
7330 information regarding the creation of a new breakpoint. In
7331 particular, set_raw_breakpoint does NOT set the breakpoint
7332 number! Care should be taken to not allow an error to occur
7333 prior to completing the initialization of the breakpoint. If this
7334 should happen, a bogus breakpoint will be left on the chain. */
7335
7336 struct breakpoint *
7337 set_raw_breakpoint (struct gdbarch *gdbarch,
7338 struct symtab_and_line sal, enum bptype bptype,
7339 const struct breakpoint_ops *ops)
7340 {
7341 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7342
7343 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7344 return add_to_breakpoint_chain (std::move (b));
7345 }
7346
7347 /* Call this routine when stepping and nexting to enable a breakpoint
7348 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7349 initiated the operation. */
7350
7351 void
7352 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7353 {
7354 int thread = tp->global_num;
7355
7356 /* To avoid having to rescan all objfile symbols at every step,
7357 we maintain a list of continually-inserted but always disabled
7358 longjmp "master" breakpoints. Here, we simply create momentary
7359 clones of those and enable them for the requested thread. */
7360 for (breakpoint *b : all_breakpoints_safe ())
7361 if (b->pspace == current_program_space
7362 && (b->type == bp_longjmp_master
7363 || b->type == bp_exception_master))
7364 {
7365 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7366 struct breakpoint *clone;
7367
7368 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7369 after their removal. */
7370 clone = momentary_breakpoint_from_master (b, type,
7371 &momentary_breakpoint_ops, 1);
7372 clone->thread = thread;
7373 }
7374
7375 tp->initiating_frame = frame;
7376 }
7377
7378 /* Delete all longjmp breakpoints from THREAD. */
7379 void
7380 delete_longjmp_breakpoint (int thread)
7381 {
7382 for (breakpoint *b : all_breakpoints_safe ())
7383 if (b->type == bp_longjmp || b->type == bp_exception)
7384 {
7385 if (b->thread == thread)
7386 delete_breakpoint (b);
7387 }
7388 }
7389
7390 void
7391 delete_longjmp_breakpoint_at_next_stop (int thread)
7392 {
7393 for (breakpoint *b : all_breakpoints_safe ())
7394 if (b->type == bp_longjmp || b->type == bp_exception)
7395 {
7396 if (b->thread == thread)
7397 b->disposition = disp_del_at_next_stop;
7398 }
7399 }
7400
7401 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7402 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7403 pointer to any of them. Return NULL if this system cannot place longjmp
7404 breakpoints. */
7405
7406 struct breakpoint *
7407 set_longjmp_breakpoint_for_call_dummy (void)
7408 {
7409 breakpoint *retval = nullptr;
7410
7411 for (breakpoint *b : all_breakpoints ())
7412 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7413 {
7414 struct breakpoint *new_b;
7415
7416 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7417 &momentary_breakpoint_ops,
7418 1);
7419 new_b->thread = inferior_thread ()->global_num;
7420
7421 /* Link NEW_B into the chain of RETVAL breakpoints. */
7422
7423 gdb_assert (new_b->related_breakpoint == new_b);
7424 if (retval == NULL)
7425 retval = new_b;
7426 new_b->related_breakpoint = retval;
7427 while (retval->related_breakpoint != new_b->related_breakpoint)
7428 retval = retval->related_breakpoint;
7429 retval->related_breakpoint = new_b;
7430 }
7431
7432 return retval;
7433 }
7434
7435 /* Verify all existing dummy frames and their associated breakpoints for
7436 TP. Remove those which can no longer be found in the current frame
7437 stack.
7438
7439 You should call this function only at places where it is safe to currently
7440 unwind the whole stack. Failed stack unwind would discard live dummy
7441 frames. */
7442
7443 void
7444 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7445 {
7446 struct breakpoint *b, *b_tmp;
7447
7448 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7449 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7450 {
7451 struct breakpoint *dummy_b = b->related_breakpoint;
7452
7453 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7454 dummy_b = dummy_b->related_breakpoint;
7455 if (dummy_b->type != bp_call_dummy
7456 || frame_find_by_id (dummy_b->frame_id) != NULL)
7457 continue;
7458
7459 dummy_frame_discard (dummy_b->frame_id, tp);
7460
7461 while (b->related_breakpoint != b)
7462 {
7463 if (b_tmp == b->related_breakpoint)
7464 b_tmp = b->related_breakpoint->next;
7465 delete_breakpoint (b->related_breakpoint);
7466 }
7467 delete_breakpoint (b);
7468 }
7469 }
7470
7471 void
7472 enable_overlay_breakpoints (void)
7473 {
7474 for (breakpoint *b : all_breakpoints ())
7475 if (b->type == bp_overlay_event)
7476 {
7477 b->enable_state = bp_enabled;
7478 update_global_location_list (UGLL_MAY_INSERT);
7479 overlay_events_enabled = 1;
7480 }
7481 }
7482
7483 void
7484 disable_overlay_breakpoints (void)
7485 {
7486 for (breakpoint *b : all_breakpoints ())
7487 if (b->type == bp_overlay_event)
7488 {
7489 b->enable_state = bp_disabled;
7490 update_global_location_list (UGLL_DONT_INSERT);
7491 overlay_events_enabled = 0;
7492 }
7493 }
7494
7495 /* Set an active std::terminate breakpoint for each std::terminate
7496 master breakpoint. */
7497 void
7498 set_std_terminate_breakpoint (void)
7499 {
7500 for (breakpoint *b : all_breakpoints_safe ())
7501 if (b->pspace == current_program_space
7502 && b->type == bp_std_terminate_master)
7503 {
7504 momentary_breakpoint_from_master (b, bp_std_terminate,
7505 &momentary_breakpoint_ops, 1);
7506 }
7507 }
7508
7509 /* Delete all the std::terminate breakpoints. */
7510 void
7511 delete_std_terminate_breakpoint (void)
7512 {
7513 for (breakpoint *b : all_breakpoints_safe ())
7514 if (b->type == bp_std_terminate)
7515 delete_breakpoint (b);
7516 }
7517
7518 struct breakpoint *
7519 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7520 {
7521 struct breakpoint *b;
7522
7523 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7524 &internal_breakpoint_ops);
7525
7526 b->enable_state = bp_enabled;
7527 /* location has to be used or breakpoint_re_set will delete me. */
7528 b->location = new_address_location (b->loc->address, NULL, 0);
7529
7530 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7531
7532 return b;
7533 }
7534
7535 struct lang_and_radix
7536 {
7537 enum language lang;
7538 int radix;
7539 };
7540
7541 /* Create a breakpoint for JIT code registration and unregistration. */
7542
7543 struct breakpoint *
7544 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7545 {
7546 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7547 &internal_breakpoint_ops);
7548 }
7549
7550 /* Remove JIT code registration and unregistration breakpoint(s). */
7551
7552 void
7553 remove_jit_event_breakpoints (void)
7554 {
7555 for (breakpoint *b : all_breakpoints_safe ())
7556 if (b->type == bp_jit_event
7557 && b->loc->pspace == current_program_space)
7558 delete_breakpoint (b);
7559 }
7560
7561 void
7562 remove_solib_event_breakpoints (void)
7563 {
7564 for (breakpoint *b : all_breakpoints_safe ())
7565 if (b->type == bp_shlib_event
7566 && b->loc->pspace == current_program_space)
7567 delete_breakpoint (b);
7568 }
7569
7570 /* See breakpoint.h. */
7571
7572 void
7573 remove_solib_event_breakpoints_at_next_stop (void)
7574 {
7575 for (breakpoint *b : all_breakpoints_safe ())
7576 if (b->type == bp_shlib_event
7577 && b->loc->pspace == current_program_space)
7578 b->disposition = disp_del_at_next_stop;
7579 }
7580
7581 /* Helper for create_solib_event_breakpoint /
7582 create_and_insert_solib_event_breakpoint. Allows specifying which
7583 INSERT_MODE to pass through to update_global_location_list. */
7584
7585 static struct breakpoint *
7586 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7587 enum ugll_insert_mode insert_mode)
7588 {
7589 struct breakpoint *b;
7590
7591 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7592 &internal_breakpoint_ops);
7593 update_global_location_list_nothrow (insert_mode);
7594 return b;
7595 }
7596
7597 struct breakpoint *
7598 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7599 {
7600 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7601 }
7602
7603 /* See breakpoint.h. */
7604
7605 struct breakpoint *
7606 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7607 {
7608 struct breakpoint *b;
7609
7610 /* Explicitly tell update_global_location_list to insert
7611 locations. */
7612 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7613 if (!b->loc->inserted)
7614 {
7615 delete_breakpoint (b);
7616 return NULL;
7617 }
7618 return b;
7619 }
7620
7621 /* Disable any breakpoints that are on code in shared libraries. Only
7622 apply to enabled breakpoints, disabled ones can just stay disabled. */
7623
7624 void
7625 disable_breakpoints_in_shlibs (void)
7626 {
7627 struct bp_location *loc, **locp_tmp;
7628
7629 ALL_BP_LOCATIONS (loc, locp_tmp)
7630 {
7631 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7632 struct breakpoint *b = loc->owner;
7633
7634 /* We apply the check to all breakpoints, including disabled for
7635 those with loc->duplicate set. This is so that when breakpoint
7636 becomes enabled, or the duplicate is removed, gdb will try to
7637 insert all breakpoints. If we don't set shlib_disabled here,
7638 we'll try to insert those breakpoints and fail. */
7639 if (((b->type == bp_breakpoint)
7640 || (b->type == bp_jit_event)
7641 || (b->type == bp_hardware_breakpoint)
7642 || (is_tracepoint (b)))
7643 && loc->pspace == current_program_space
7644 && !loc->shlib_disabled
7645 && solib_name_from_address (loc->pspace, loc->address)
7646 )
7647 {
7648 loc->shlib_disabled = 1;
7649 }
7650 }
7651 }
7652
7653 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7654 notification of unloaded_shlib. Only apply to enabled breakpoints,
7655 disabled ones can just stay disabled. */
7656
7657 static void
7658 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7659 {
7660 struct bp_location *loc, **locp_tmp;
7661 int disabled_shlib_breaks = 0;
7662
7663 ALL_BP_LOCATIONS (loc, locp_tmp)
7664 {
7665 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7666 struct breakpoint *b = loc->owner;
7667
7668 if (solib->pspace == loc->pspace
7669 && !loc->shlib_disabled
7670 && (((b->type == bp_breakpoint
7671 || b->type == bp_jit_event
7672 || b->type == bp_hardware_breakpoint)
7673 && (loc->loc_type == bp_loc_hardware_breakpoint
7674 || loc->loc_type == bp_loc_software_breakpoint))
7675 || is_tracepoint (b))
7676 && solib_contains_address_p (solib, loc->address))
7677 {
7678 loc->shlib_disabled = 1;
7679 /* At this point, we cannot rely on remove_breakpoint
7680 succeeding so we must mark the breakpoint as not inserted
7681 to prevent future errors occurring in remove_breakpoints. */
7682 loc->inserted = 0;
7683
7684 /* This may cause duplicate notifications for the same breakpoint. */
7685 gdb::observers::breakpoint_modified.notify (b);
7686
7687 if (!disabled_shlib_breaks)
7688 {
7689 target_terminal::ours_for_output ();
7690 warning (_("Temporarily disabling breakpoints "
7691 "for unloaded shared library \"%s\""),
7692 solib->so_name);
7693 }
7694 disabled_shlib_breaks = 1;
7695 }
7696 }
7697 }
7698
7699 /* Disable any breakpoints and tracepoints in OBJFILE upon
7700 notification of free_objfile. Only apply to enabled breakpoints,
7701 disabled ones can just stay disabled. */
7702
7703 static void
7704 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7705 {
7706 if (objfile == NULL)
7707 return;
7708
7709 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7710 managed by the user with add-symbol-file/remove-symbol-file.
7711 Similarly to how breakpoints in shared libraries are handled in
7712 response to "nosharedlibrary", mark breakpoints in such modules
7713 shlib_disabled so they end up uninserted on the next global
7714 location list update. Shared libraries not loaded by the user
7715 aren't handled here -- they're already handled in
7716 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7717 solib_unloaded observer. We skip objfiles that are not
7718 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7719 main objfile). */
7720 if ((objfile->flags & OBJF_SHARED) == 0
7721 || (objfile->flags & OBJF_USERLOADED) == 0)
7722 return;
7723
7724 for (breakpoint *b : all_breakpoints ())
7725 {
7726 int bp_modified = 0;
7727
7728 if (!is_breakpoint (b) && !is_tracepoint (b))
7729 continue;
7730
7731 for (bp_location *loc : b->locations ())
7732 {
7733 CORE_ADDR loc_addr = loc->address;
7734
7735 if (loc->loc_type != bp_loc_hardware_breakpoint
7736 && loc->loc_type != bp_loc_software_breakpoint)
7737 continue;
7738
7739 if (loc->shlib_disabled != 0)
7740 continue;
7741
7742 if (objfile->pspace != loc->pspace)
7743 continue;
7744
7745 if (loc->loc_type != bp_loc_hardware_breakpoint
7746 && loc->loc_type != bp_loc_software_breakpoint)
7747 continue;
7748
7749 if (is_addr_in_objfile (loc_addr, objfile))
7750 {
7751 loc->shlib_disabled = 1;
7752 /* At this point, we don't know whether the object was
7753 unmapped from the inferior or not, so leave the
7754 inserted flag alone. We'll handle failure to
7755 uninsert quietly, in case the object was indeed
7756 unmapped. */
7757
7758 mark_breakpoint_location_modified (loc);
7759
7760 bp_modified = 1;
7761 }
7762 }
7763
7764 if (bp_modified)
7765 gdb::observers::breakpoint_modified.notify (b);
7766 }
7767 }
7768
7769 /* FORK & VFORK catchpoints. */
7770
7771 /* An instance of this type is used to represent a fork or vfork
7772 catchpoint. A breakpoint is really of this type iff its ops pointer points
7773 to CATCH_FORK_BREAKPOINT_OPS. */
7774
7775 struct fork_catchpoint : public breakpoint
7776 {
7777 /* Process id of a child process whose forking triggered this
7778 catchpoint. This field is only valid immediately after this
7779 catchpoint has triggered. */
7780 ptid_t forked_inferior_pid;
7781 };
7782
7783 /* Implement the "insert" breakpoint_ops method for fork
7784 catchpoints. */
7785
7786 static int
7787 insert_catch_fork (struct bp_location *bl)
7788 {
7789 return target_insert_fork_catchpoint (inferior_ptid.pid ());
7790 }
7791
7792 /* Implement the "remove" breakpoint_ops method for fork
7793 catchpoints. */
7794
7795 static int
7796 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
7797 {
7798 return target_remove_fork_catchpoint (inferior_ptid.pid ());
7799 }
7800
7801 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7802 catchpoints. */
7803
7804 static int
7805 breakpoint_hit_catch_fork (const struct bp_location *bl,
7806 const address_space *aspace, CORE_ADDR bp_addr,
7807 const struct target_waitstatus *ws)
7808 {
7809 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7810
7811 if (ws->kind != TARGET_WAITKIND_FORKED)
7812 return 0;
7813
7814 c->forked_inferior_pid = ws->value.related_pid;
7815 return 1;
7816 }
7817
7818 /* Implement the "print_it" breakpoint_ops method for fork
7819 catchpoints. */
7820
7821 static enum print_stop_action
7822 print_it_catch_fork (bpstat bs)
7823 {
7824 struct ui_out *uiout = current_uiout;
7825 struct breakpoint *b = bs->breakpoint_at;
7826 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7827
7828 annotate_catchpoint (b->number);
7829 maybe_print_thread_hit_breakpoint (uiout);
7830 if (b->disposition == disp_del)
7831 uiout->text ("Temporary catchpoint ");
7832 else
7833 uiout->text ("Catchpoint ");
7834 if (uiout->is_mi_like_p ())
7835 {
7836 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7837 uiout->field_string ("disp", bpdisp_text (b->disposition));
7838 }
7839 uiout->field_signed ("bkptno", b->number);
7840 uiout->text (" (forked process ");
7841 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7842 uiout->text ("), ");
7843 return PRINT_SRC_AND_LOC;
7844 }
7845
7846 /* Implement the "print_one" breakpoint_ops method for fork
7847 catchpoints. */
7848
7849 static void
7850 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7851 {
7852 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7853 struct value_print_options opts;
7854 struct ui_out *uiout = current_uiout;
7855
7856 get_user_print_options (&opts);
7857
7858 /* Field 4, the address, is omitted (which makes the columns not
7859 line up too nicely with the headers, but the effect is relatively
7860 readable). */
7861 if (opts.addressprint)
7862 uiout->field_skip ("addr");
7863 annotate_field (5);
7864 uiout->text ("fork");
7865 if (c->forked_inferior_pid != null_ptid)
7866 {
7867 uiout->text (", process ");
7868 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7869 uiout->spaces (1);
7870 }
7871
7872 if (uiout->is_mi_like_p ())
7873 uiout->field_string ("catch-type", "fork");
7874 }
7875
7876 /* Implement the "print_mention" breakpoint_ops method for fork
7877 catchpoints. */
7878
7879 static void
7880 print_mention_catch_fork (struct breakpoint *b)
7881 {
7882 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7883 }
7884
7885 /* Implement the "print_recreate" breakpoint_ops method for fork
7886 catchpoints. */
7887
7888 static void
7889 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7890 {
7891 fprintf_unfiltered (fp, "catch fork");
7892 print_recreate_thread (b, fp);
7893 }
7894
7895 /* The breakpoint_ops structure to be used in fork catchpoints. */
7896
7897 static struct breakpoint_ops catch_fork_breakpoint_ops;
7898
7899 /* Implement the "insert" breakpoint_ops method for vfork
7900 catchpoints. */
7901
7902 static int
7903 insert_catch_vfork (struct bp_location *bl)
7904 {
7905 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
7906 }
7907
7908 /* Implement the "remove" breakpoint_ops method for vfork
7909 catchpoints. */
7910
7911 static int
7912 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
7913 {
7914 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
7915 }
7916
7917 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7918 catchpoints. */
7919
7920 static int
7921 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7922 const address_space *aspace, CORE_ADDR bp_addr,
7923 const struct target_waitstatus *ws)
7924 {
7925 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7926
7927 if (ws->kind != TARGET_WAITKIND_VFORKED)
7928 return 0;
7929
7930 c->forked_inferior_pid = ws->value.related_pid;
7931 return 1;
7932 }
7933
7934 /* Implement the "print_it" breakpoint_ops method for vfork
7935 catchpoints. */
7936
7937 static enum print_stop_action
7938 print_it_catch_vfork (bpstat bs)
7939 {
7940 struct ui_out *uiout = current_uiout;
7941 struct breakpoint *b = bs->breakpoint_at;
7942 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7943
7944 annotate_catchpoint (b->number);
7945 maybe_print_thread_hit_breakpoint (uiout);
7946 if (b->disposition == disp_del)
7947 uiout->text ("Temporary catchpoint ");
7948 else
7949 uiout->text ("Catchpoint ");
7950 if (uiout->is_mi_like_p ())
7951 {
7952 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7953 uiout->field_string ("disp", bpdisp_text (b->disposition));
7954 }
7955 uiout->field_signed ("bkptno", b->number);
7956 uiout->text (" (vforked process ");
7957 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7958 uiout->text ("), ");
7959 return PRINT_SRC_AND_LOC;
7960 }
7961
7962 /* Implement the "print_one" breakpoint_ops method for vfork
7963 catchpoints. */
7964
7965 static void
7966 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7967 {
7968 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7969 struct value_print_options opts;
7970 struct ui_out *uiout = current_uiout;
7971
7972 get_user_print_options (&opts);
7973 /* Field 4, the address, is omitted (which makes the columns not
7974 line up too nicely with the headers, but the effect is relatively
7975 readable). */
7976 if (opts.addressprint)
7977 uiout->field_skip ("addr");
7978 annotate_field (5);
7979 uiout->text ("vfork");
7980 if (c->forked_inferior_pid != null_ptid)
7981 {
7982 uiout->text (", process ");
7983 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7984 uiout->spaces (1);
7985 }
7986
7987 if (uiout->is_mi_like_p ())
7988 uiout->field_string ("catch-type", "vfork");
7989 }
7990
7991 /* Implement the "print_mention" breakpoint_ops method for vfork
7992 catchpoints. */
7993
7994 static void
7995 print_mention_catch_vfork (struct breakpoint *b)
7996 {
7997 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7998 }
7999
8000 /* Implement the "print_recreate" breakpoint_ops method for vfork
8001 catchpoints. */
8002
8003 static void
8004 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8005 {
8006 fprintf_unfiltered (fp, "catch vfork");
8007 print_recreate_thread (b, fp);
8008 }
8009
8010 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8011
8012 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8013
8014 /* An instance of this type is used to represent an solib catchpoint.
8015 A breakpoint is really of this type iff its ops pointer points to
8016 CATCH_SOLIB_BREAKPOINT_OPS. */
8017
8018 struct solib_catchpoint : public breakpoint
8019 {
8020 ~solib_catchpoint () override;
8021
8022 /* True for "catch load", false for "catch unload". */
8023 bool is_load;
8024
8025 /* Regular expression to match, if any. COMPILED is only valid when
8026 REGEX is non-NULL. */
8027 char *regex;
8028 std::unique_ptr<compiled_regex> compiled;
8029 };
8030
8031 solib_catchpoint::~solib_catchpoint ()
8032 {
8033 xfree (this->regex);
8034 }
8035
8036 static int
8037 insert_catch_solib (struct bp_location *ignore)
8038 {
8039 return 0;
8040 }
8041
8042 static int
8043 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
8044 {
8045 return 0;
8046 }
8047
8048 static int
8049 breakpoint_hit_catch_solib (const struct bp_location *bl,
8050 const address_space *aspace,
8051 CORE_ADDR bp_addr,
8052 const struct target_waitstatus *ws)
8053 {
8054 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8055
8056 if (ws->kind == TARGET_WAITKIND_LOADED)
8057 return 1;
8058
8059 for (breakpoint *other : all_breakpoints ())
8060 {
8061 if (other == bl->owner)
8062 continue;
8063
8064 if (other->type != bp_shlib_event)
8065 continue;
8066
8067 if (self->pspace != NULL && other->pspace != self->pspace)
8068 continue;
8069
8070 for (bp_location *other_bl : other->locations ())
8071 {
8072 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8073 return 1;
8074 }
8075 }
8076
8077 return 0;
8078 }
8079
8080 static void
8081 check_status_catch_solib (struct bpstats *bs)
8082 {
8083 struct solib_catchpoint *self
8084 = (struct solib_catchpoint *) bs->breakpoint_at;
8085
8086 if (self->is_load)
8087 {
8088 for (so_list *iter : current_program_space->added_solibs)
8089 {
8090 if (!self->regex
8091 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
8092 return;
8093 }
8094 }
8095 else
8096 {
8097 for (const std::string &iter : current_program_space->deleted_solibs)
8098 {
8099 if (!self->regex
8100 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
8101 return;
8102 }
8103 }
8104
8105 bs->stop = 0;
8106 bs->print_it = print_it_noop;
8107 }
8108
8109 static enum print_stop_action
8110 print_it_catch_solib (bpstat bs)
8111 {
8112 struct breakpoint *b = bs->breakpoint_at;
8113 struct ui_out *uiout = current_uiout;
8114
8115 annotate_catchpoint (b->number);
8116 maybe_print_thread_hit_breakpoint (uiout);
8117 if (b->disposition == disp_del)
8118 uiout->text ("Temporary catchpoint ");
8119 else
8120 uiout->text ("Catchpoint ");
8121 uiout->field_signed ("bkptno", b->number);
8122 uiout->text ("\n");
8123 if (uiout->is_mi_like_p ())
8124 uiout->field_string ("disp", bpdisp_text (b->disposition));
8125 print_solib_event (1);
8126 return PRINT_SRC_AND_LOC;
8127 }
8128
8129 static void
8130 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8131 {
8132 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8133 struct value_print_options opts;
8134 struct ui_out *uiout = current_uiout;
8135
8136 get_user_print_options (&opts);
8137 /* Field 4, the address, is omitted (which makes the columns not
8138 line up too nicely with the headers, but the effect is relatively
8139 readable). */
8140 if (opts.addressprint)
8141 {
8142 annotate_field (4);
8143 uiout->field_skip ("addr");
8144 }
8145
8146 std::string msg;
8147 annotate_field (5);
8148 if (self->is_load)
8149 {
8150 if (self->regex)
8151 msg = string_printf (_("load of library matching %s"), self->regex);
8152 else
8153 msg = _("load of library");
8154 }
8155 else
8156 {
8157 if (self->regex)
8158 msg = string_printf (_("unload of library matching %s"), self->regex);
8159 else
8160 msg = _("unload of library");
8161 }
8162 uiout->field_string ("what", msg);
8163
8164 if (uiout->is_mi_like_p ())
8165 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8166 }
8167
8168 static void
8169 print_mention_catch_solib (struct breakpoint *b)
8170 {
8171 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8172
8173 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8174 self->is_load ? "load" : "unload");
8175 }
8176
8177 static void
8178 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8179 {
8180 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8181
8182 fprintf_unfiltered (fp, "%s %s",
8183 b->disposition == disp_del ? "tcatch" : "catch",
8184 self->is_load ? "load" : "unload");
8185 if (self->regex)
8186 fprintf_unfiltered (fp, " %s", self->regex);
8187 fprintf_unfiltered (fp, "\n");
8188 }
8189
8190 static struct breakpoint_ops catch_solib_breakpoint_ops;
8191
8192 /* See breakpoint.h. */
8193
8194 void
8195 add_solib_catchpoint (const char *arg, bool is_load, bool is_temp, bool enabled)
8196 {
8197 struct gdbarch *gdbarch = get_current_arch ();
8198
8199 if (!arg)
8200 arg = "";
8201 arg = skip_spaces (arg);
8202
8203 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
8204
8205 if (*arg != '\0')
8206 {
8207 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8208 _("Invalid regexp")));
8209 c->regex = xstrdup (arg);
8210 }
8211
8212 c->is_load = is_load;
8213 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
8214 &catch_solib_breakpoint_ops);
8215
8216 c->enable_state = enabled ? bp_enabled : bp_disabled;
8217
8218 install_breakpoint (0, std::move (c), 1);
8219 }
8220
8221 /* A helper function that does all the work for "catch load" and
8222 "catch unload". */
8223
8224 static void
8225 catch_load_or_unload (const char *arg, int from_tty, int is_load,
8226 struct cmd_list_element *command)
8227 {
8228 const int enabled = 1;
8229 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
8230
8231 add_solib_catchpoint (arg, is_load, temp, enabled);
8232 }
8233
8234 static void
8235 catch_load_command_1 (const char *arg, int from_tty,
8236 struct cmd_list_element *command)
8237 {
8238 catch_load_or_unload (arg, from_tty, 1, command);
8239 }
8240
8241 static void
8242 catch_unload_command_1 (const char *arg, int from_tty,
8243 struct cmd_list_element *command)
8244 {
8245 catch_load_or_unload (arg, from_tty, 0, command);
8246 }
8247
8248 /* See breakpoint.h. */
8249
8250 void
8251 init_catchpoint (struct breakpoint *b,
8252 struct gdbarch *gdbarch, bool temp,
8253 const char *cond_string,
8254 const struct breakpoint_ops *ops)
8255 {
8256 symtab_and_line sal;
8257 sal.pspace = current_program_space;
8258
8259 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8260
8261 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8262 b->disposition = temp ? disp_del : disp_donttouch;
8263 }
8264
8265 void
8266 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8267 {
8268 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8269 set_breakpoint_number (internal, b);
8270 if (is_tracepoint (b))
8271 set_tracepoint_count (breakpoint_count);
8272 if (!internal)
8273 mention (b);
8274 gdb::observers::breakpoint_created.notify (b);
8275
8276 if (update_gll)
8277 update_global_location_list (UGLL_MAY_INSERT);
8278 }
8279
8280 static void
8281 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8282 bool temp, const char *cond_string,
8283 const struct breakpoint_ops *ops)
8284 {
8285 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
8286
8287 init_catchpoint (c.get (), gdbarch, temp, cond_string, ops);
8288
8289 c->forked_inferior_pid = null_ptid;
8290
8291 install_breakpoint (0, std::move (c), 1);
8292 }
8293
8294 /* Exec catchpoints. */
8295
8296 /* An instance of this type is used to represent an exec catchpoint.
8297 A breakpoint is really of this type iff its ops pointer points to
8298 CATCH_EXEC_BREAKPOINT_OPS. */
8299
8300 struct exec_catchpoint : public breakpoint
8301 {
8302 ~exec_catchpoint () override;
8303
8304 /* Filename of a program whose exec triggered this catchpoint.
8305 This field is only valid immediately after this catchpoint has
8306 triggered. */
8307 char *exec_pathname;
8308 };
8309
8310 /* Exec catchpoint destructor. */
8311
8312 exec_catchpoint::~exec_catchpoint ()
8313 {
8314 xfree (this->exec_pathname);
8315 }
8316
8317 static int
8318 insert_catch_exec (struct bp_location *bl)
8319 {
8320 return target_insert_exec_catchpoint (inferior_ptid.pid ());
8321 }
8322
8323 static int
8324 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8325 {
8326 return target_remove_exec_catchpoint (inferior_ptid.pid ());
8327 }
8328
8329 static int
8330 breakpoint_hit_catch_exec (const struct bp_location *bl,
8331 const address_space *aspace, CORE_ADDR bp_addr,
8332 const struct target_waitstatus *ws)
8333 {
8334 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8335
8336 if (ws->kind != TARGET_WAITKIND_EXECD)
8337 return 0;
8338
8339 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8340 return 1;
8341 }
8342
8343 static enum print_stop_action
8344 print_it_catch_exec (bpstat bs)
8345 {
8346 struct ui_out *uiout = current_uiout;
8347 struct breakpoint *b = bs->breakpoint_at;
8348 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8349
8350 annotate_catchpoint (b->number);
8351 maybe_print_thread_hit_breakpoint (uiout);
8352 if (b->disposition == disp_del)
8353 uiout->text ("Temporary catchpoint ");
8354 else
8355 uiout->text ("Catchpoint ");
8356 if (uiout->is_mi_like_p ())
8357 {
8358 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8359 uiout->field_string ("disp", bpdisp_text (b->disposition));
8360 }
8361 uiout->field_signed ("bkptno", b->number);
8362 uiout->text (" (exec'd ");
8363 uiout->field_string ("new-exec", c->exec_pathname);
8364 uiout->text ("), ");
8365
8366 return PRINT_SRC_AND_LOC;
8367 }
8368
8369 static void
8370 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8371 {
8372 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8373 struct value_print_options opts;
8374 struct ui_out *uiout = current_uiout;
8375
8376 get_user_print_options (&opts);
8377
8378 /* Field 4, the address, is omitted (which makes the columns
8379 not line up too nicely with the headers, but the effect
8380 is relatively readable). */
8381 if (opts.addressprint)
8382 uiout->field_skip ("addr");
8383 annotate_field (5);
8384 uiout->text ("exec");
8385 if (c->exec_pathname != NULL)
8386 {
8387 uiout->text (", program \"");
8388 uiout->field_string ("what", c->exec_pathname);
8389 uiout->text ("\" ");
8390 }
8391
8392 if (uiout->is_mi_like_p ())
8393 uiout->field_string ("catch-type", "exec");
8394 }
8395
8396 static void
8397 print_mention_catch_exec (struct breakpoint *b)
8398 {
8399 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8400 }
8401
8402 /* Implement the "print_recreate" breakpoint_ops method for exec
8403 catchpoints. */
8404
8405 static void
8406 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8407 {
8408 fprintf_unfiltered (fp, "catch exec");
8409 print_recreate_thread (b, fp);
8410 }
8411
8412 static struct breakpoint_ops catch_exec_breakpoint_ops;
8413
8414 static int
8415 hw_breakpoint_used_count (void)
8416 {
8417 int i = 0;
8418
8419 for (breakpoint *b : all_breakpoints ())
8420 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8421 for (bp_location *bl : b->locations ())
8422 {
8423 /* Special types of hardware breakpoints may use more than
8424 one register. */
8425 i += b->ops->resources_needed (bl);
8426 }
8427
8428 return i;
8429 }
8430
8431 /* Returns the resources B would use if it were a hardware
8432 watchpoint. */
8433
8434 static int
8435 hw_watchpoint_use_count (struct breakpoint *b)
8436 {
8437 int i = 0;
8438
8439 if (!breakpoint_enabled (b))
8440 return 0;
8441
8442 for (bp_location *bl : b->locations ())
8443 {
8444 /* Special types of hardware watchpoints may use more than
8445 one register. */
8446 i += b->ops->resources_needed (bl);
8447 }
8448
8449 return i;
8450 }
8451
8452 /* Returns the sum the used resources of all hardware watchpoints of
8453 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8454 the sum of the used resources of all hardware watchpoints of other
8455 types _not_ TYPE. */
8456
8457 static int
8458 hw_watchpoint_used_count_others (struct breakpoint *except,
8459 enum bptype type, int *other_type_used)
8460 {
8461 int i = 0;
8462
8463 *other_type_used = 0;
8464 for (breakpoint *b : all_breakpoints ())
8465 {
8466 if (b == except)
8467 continue;
8468 if (!breakpoint_enabled (b))
8469 continue;
8470
8471 if (b->type == type)
8472 i += hw_watchpoint_use_count (b);
8473 else if (is_hardware_watchpoint (b))
8474 *other_type_used = 1;
8475 }
8476
8477 return i;
8478 }
8479
8480 void
8481 disable_watchpoints_before_interactive_call_start (void)
8482 {
8483 for (breakpoint *b : all_breakpoints ())
8484 if (is_watchpoint (b) && breakpoint_enabled (b))
8485 {
8486 b->enable_state = bp_call_disabled;
8487 update_global_location_list (UGLL_DONT_INSERT);
8488 }
8489 }
8490
8491 void
8492 enable_watchpoints_after_interactive_call_stop (void)
8493 {
8494 for (breakpoint *b : all_breakpoints ())
8495 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8496 {
8497 b->enable_state = bp_enabled;
8498 update_global_location_list (UGLL_MAY_INSERT);
8499 }
8500 }
8501
8502 void
8503 disable_breakpoints_before_startup (void)
8504 {
8505 current_program_space->executing_startup = 1;
8506 update_global_location_list (UGLL_DONT_INSERT);
8507 }
8508
8509 void
8510 enable_breakpoints_after_startup (void)
8511 {
8512 current_program_space->executing_startup = 0;
8513 breakpoint_re_set ();
8514 }
8515
8516 /* Create a new single-step breakpoint for thread THREAD, with no
8517 locations. */
8518
8519 static struct breakpoint *
8520 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8521 {
8522 std::unique_ptr<breakpoint> b (new breakpoint ());
8523
8524 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
8525 &momentary_breakpoint_ops);
8526
8527 b->disposition = disp_donttouch;
8528 b->frame_id = null_frame_id;
8529
8530 b->thread = thread;
8531 gdb_assert (b->thread != 0);
8532
8533 return add_to_breakpoint_chain (std::move (b));
8534 }
8535
8536 /* Set a momentary breakpoint of type TYPE at address specified by
8537 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8538 frame. */
8539
8540 breakpoint_up
8541 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8542 struct frame_id frame_id, enum bptype type)
8543 {
8544 struct breakpoint *b;
8545
8546 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8547 tail-called one. */
8548 gdb_assert (!frame_id_artificial_p (frame_id));
8549
8550 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8551 b->enable_state = bp_enabled;
8552 b->disposition = disp_donttouch;
8553 b->frame_id = frame_id;
8554
8555 b->thread = inferior_thread ()->global_num;
8556
8557 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8558
8559 return breakpoint_up (b);
8560 }
8561
8562 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8563 The new breakpoint will have type TYPE, use OPS as its
8564 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8565
8566 static struct breakpoint *
8567 momentary_breakpoint_from_master (struct breakpoint *orig,
8568 enum bptype type,
8569 const struct breakpoint_ops *ops,
8570 int loc_enabled)
8571 {
8572 struct breakpoint *copy;
8573
8574 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8575 copy->loc = allocate_bp_location (copy);
8576 set_breakpoint_location_function (copy->loc);
8577
8578 copy->loc->gdbarch = orig->loc->gdbarch;
8579 copy->loc->requested_address = orig->loc->requested_address;
8580 copy->loc->address = orig->loc->address;
8581 copy->loc->section = orig->loc->section;
8582 copy->loc->pspace = orig->loc->pspace;
8583 copy->loc->probe = orig->loc->probe;
8584 copy->loc->line_number = orig->loc->line_number;
8585 copy->loc->symtab = orig->loc->symtab;
8586 copy->loc->enabled = loc_enabled;
8587 copy->frame_id = orig->frame_id;
8588 copy->thread = orig->thread;
8589 copy->pspace = orig->pspace;
8590
8591 copy->enable_state = bp_enabled;
8592 copy->disposition = disp_donttouch;
8593 copy->number = internal_breakpoint_number--;
8594
8595 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8596 return copy;
8597 }
8598
8599 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8600 ORIG is NULL. */
8601
8602 struct breakpoint *
8603 clone_momentary_breakpoint (struct breakpoint *orig)
8604 {
8605 /* If there's nothing to clone, then return nothing. */
8606 if (orig == NULL)
8607 return NULL;
8608
8609 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8610 }
8611
8612 breakpoint_up
8613 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8614 enum bptype type)
8615 {
8616 struct symtab_and_line sal;
8617
8618 sal = find_pc_line (pc, 0);
8619 sal.pc = pc;
8620 sal.section = find_pc_overlay (pc);
8621 sal.explicit_pc = 1;
8622
8623 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8624 }
8625 \f
8626
8627 /* Tell the user we have just set a breakpoint B. */
8628
8629 static void
8630 mention (struct breakpoint *b)
8631 {
8632 b->ops->print_mention (b);
8633 current_uiout->text ("\n");
8634 }
8635 \f
8636
8637 static bool bp_loc_is_permanent (struct bp_location *loc);
8638
8639 /* Handle "set breakpoint auto-hw on".
8640
8641 If the explicitly specified breakpoint type is not hardware
8642 breakpoint, check the memory map to see whether the breakpoint
8643 address is in read-only memory.
8644
8645 - location type is not hardware breakpoint, memory is read-only.
8646 We change the type of the location to hardware breakpoint.
8647
8648 - location type is hardware breakpoint, memory is read-write. This
8649 means we've previously made the location hardware one, but then the
8650 memory map changed, so we undo.
8651 */
8652
8653 static void
8654 handle_automatic_hardware_breakpoints (bp_location *bl)
8655 {
8656 if (automatic_hardware_breakpoints
8657 && bl->owner->type != bp_hardware_breakpoint
8658 && (bl->loc_type == bp_loc_software_breakpoint
8659 || bl->loc_type == bp_loc_hardware_breakpoint))
8660 {
8661 /* When breakpoints are removed, remove_breakpoints will use
8662 location types we've just set here, the only possible problem
8663 is that memory map has changed during running program, but
8664 it's not going to work anyway with current gdb. */
8665 mem_region *mr = lookup_mem_region (bl->address);
8666
8667 if (mr != nullptr)
8668 {
8669 enum bp_loc_type new_type;
8670
8671 if (mr->attrib.mode != MEM_RW)
8672 new_type = bp_loc_hardware_breakpoint;
8673 else
8674 new_type = bp_loc_software_breakpoint;
8675
8676 if (new_type != bl->loc_type)
8677 {
8678 static bool said = false;
8679
8680 bl->loc_type = new_type;
8681 if (!said)
8682 {
8683 fprintf_filtered (gdb_stdout,
8684 _("Note: automatically using "
8685 "hardware breakpoints for "
8686 "read-only addresses.\n"));
8687 said = true;
8688 }
8689 }
8690 }
8691 }
8692 }
8693
8694 static struct bp_location *
8695 add_location_to_breakpoint (struct breakpoint *b,
8696 const struct symtab_and_line *sal)
8697 {
8698 struct bp_location *loc, **tmp;
8699 CORE_ADDR adjusted_address;
8700 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8701
8702 if (loc_gdbarch == NULL)
8703 loc_gdbarch = b->gdbarch;
8704
8705 /* Adjust the breakpoint's address prior to allocating a location.
8706 Once we call allocate_bp_location(), that mostly uninitialized
8707 location will be placed on the location chain. Adjustment of the
8708 breakpoint may cause target_read_memory() to be called and we do
8709 not want its scan of the location chain to find a breakpoint and
8710 location that's only been partially initialized. */
8711 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8712 sal->pc, b->type);
8713
8714 /* Sort the locations by their ADDRESS. */
8715 loc = allocate_bp_location (b);
8716 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8717 tmp = &((*tmp)->next))
8718 ;
8719 loc->next = *tmp;
8720 *tmp = loc;
8721
8722 loc->requested_address = sal->pc;
8723 loc->address = adjusted_address;
8724 loc->pspace = sal->pspace;
8725 loc->probe.prob = sal->prob;
8726 loc->probe.objfile = sal->objfile;
8727 gdb_assert (loc->pspace != NULL);
8728 loc->section = sal->section;
8729 loc->gdbarch = loc_gdbarch;
8730 loc->line_number = sal->line;
8731 loc->symtab = sal->symtab;
8732 loc->symbol = sal->symbol;
8733 loc->msymbol = sal->msymbol;
8734 loc->objfile = sal->objfile;
8735
8736 set_breakpoint_location_function (loc);
8737
8738 /* While by definition, permanent breakpoints are already present in the
8739 code, we don't mark the location as inserted. Normally one would expect
8740 that GDB could rely on that breakpoint instruction to stop the program,
8741 thus removing the need to insert its own breakpoint, except that executing
8742 the breakpoint instruction can kill the target instead of reporting a
8743 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8744 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8745 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8746 breakpoint be inserted normally results in QEMU knowing about the GDB
8747 breakpoint, and thus trap before the breakpoint instruction is executed.
8748 (If GDB later needs to continue execution past the permanent breakpoint,
8749 it manually increments the PC, thus avoiding executing the breakpoint
8750 instruction.) */
8751 if (bp_loc_is_permanent (loc))
8752 loc->permanent = 1;
8753
8754 return loc;
8755 }
8756 \f
8757
8758 /* Return true if LOC is pointing to a permanent breakpoint,
8759 return false otherwise. */
8760
8761 static bool
8762 bp_loc_is_permanent (struct bp_location *loc)
8763 {
8764 gdb_assert (loc != NULL);
8765
8766 /* If we have a non-breakpoint-backed catchpoint or a software
8767 watchpoint, just return 0. We should not attempt to read from
8768 the addresses the locations of these breakpoint types point to.
8769 gdbarch_program_breakpoint_here_p, below, will attempt to read
8770 memory. */
8771 if (!bl_address_is_meaningful (loc))
8772 return false;
8773
8774 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8775 switch_to_program_space_and_thread (loc->pspace);
8776 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
8777 }
8778
8779 /* Build a command list for the dprintf corresponding to the current
8780 settings of the dprintf style options. */
8781
8782 static void
8783 update_dprintf_command_list (struct breakpoint *b)
8784 {
8785 char *dprintf_args = b->extra_string;
8786 char *printf_line = NULL;
8787
8788 if (!dprintf_args)
8789 return;
8790
8791 dprintf_args = skip_spaces (dprintf_args);
8792
8793 /* Allow a comma, as it may have terminated a location, but don't
8794 insist on it. */
8795 if (*dprintf_args == ',')
8796 ++dprintf_args;
8797 dprintf_args = skip_spaces (dprintf_args);
8798
8799 if (*dprintf_args != '"')
8800 error (_("Bad format string, missing '\"'."));
8801
8802 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8803 printf_line = xstrprintf ("printf %s", dprintf_args);
8804 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8805 {
8806 if (!dprintf_function)
8807 error (_("No function supplied for dprintf call"));
8808
8809 if (dprintf_channel && strlen (dprintf_channel) > 0)
8810 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8811 dprintf_function,
8812 dprintf_channel,
8813 dprintf_args);
8814 else
8815 printf_line = xstrprintf ("call (void) %s (%s)",
8816 dprintf_function,
8817 dprintf_args);
8818 }
8819 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8820 {
8821 if (target_can_run_breakpoint_commands ())
8822 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8823 else
8824 {
8825 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8826 printf_line = xstrprintf ("printf %s", dprintf_args);
8827 }
8828 }
8829 else
8830 internal_error (__FILE__, __LINE__,
8831 _("Invalid dprintf style."));
8832
8833 gdb_assert (printf_line != NULL);
8834
8835 /* Manufacture a printf sequence. */
8836 struct command_line *printf_cmd_line
8837 = new struct command_line (simple_control, printf_line);
8838 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8839 command_lines_deleter ()));
8840 }
8841
8842 /* Update all dprintf commands, making their command lists reflect
8843 current style settings. */
8844
8845 static void
8846 update_dprintf_commands (const char *args, int from_tty,
8847 struct cmd_list_element *c)
8848 {
8849 for (breakpoint *b : all_breakpoints ())
8850 if (b->type == bp_dprintf)
8851 update_dprintf_command_list (b);
8852 }
8853
8854 /* Create a breakpoint with SAL as location. Use LOCATION
8855 as a description of the location, and COND_STRING
8856 as condition expression. If LOCATION is NULL then create an
8857 "address location" from the address in the SAL. */
8858
8859 static void
8860 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8861 gdb::array_view<const symtab_and_line> sals,
8862 event_location_up &&location,
8863 gdb::unique_xmalloc_ptr<char> filter,
8864 gdb::unique_xmalloc_ptr<char> cond_string,
8865 gdb::unique_xmalloc_ptr<char> extra_string,
8866 enum bptype type, enum bpdisp disposition,
8867 int thread, int task, int ignore_count,
8868 const struct breakpoint_ops *ops, int from_tty,
8869 int enabled, int internal, unsigned flags,
8870 int display_canonical)
8871 {
8872 int i;
8873
8874 if (type == bp_hardware_breakpoint)
8875 {
8876 int target_resources_ok;
8877
8878 i = hw_breakpoint_used_count ();
8879 target_resources_ok =
8880 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8881 i + 1, 0);
8882 if (target_resources_ok == 0)
8883 error (_("No hardware breakpoint support in the target."));
8884 else if (target_resources_ok < 0)
8885 error (_("Hardware breakpoints used exceeds limit."));
8886 }
8887
8888 gdb_assert (!sals.empty ());
8889
8890 for (const auto &sal : sals)
8891 {
8892 struct bp_location *loc;
8893
8894 if (from_tty)
8895 {
8896 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8897 if (!loc_gdbarch)
8898 loc_gdbarch = gdbarch;
8899
8900 describe_other_breakpoints (loc_gdbarch,
8901 sal.pspace, sal.pc, sal.section, thread);
8902 }
8903
8904 if (&sal == &sals[0])
8905 {
8906 init_raw_breakpoint (b, gdbarch, sal, type, ops);
8907 b->thread = thread;
8908 b->task = task;
8909
8910 b->cond_string = cond_string.release ();
8911 b->extra_string = extra_string.release ();
8912 b->ignore_count = ignore_count;
8913 b->enable_state = enabled ? bp_enabled : bp_disabled;
8914 b->disposition = disposition;
8915
8916 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8917 b->loc->inserted = 1;
8918
8919 if (type == bp_static_tracepoint)
8920 {
8921 struct tracepoint *t = (struct tracepoint *) b;
8922 struct static_tracepoint_marker marker;
8923
8924 if (strace_marker_p (b))
8925 {
8926 /* We already know the marker exists, otherwise, we
8927 wouldn't see a sal for it. */
8928 const char *p
8929 = &event_location_to_string (b->location.get ())[3];
8930 const char *endp;
8931
8932 p = skip_spaces (p);
8933
8934 endp = skip_to_space (p);
8935
8936 t->static_trace_marker_id.assign (p, endp - p);
8937
8938 printf_filtered (_("Probed static tracepoint "
8939 "marker \"%s\"\n"),
8940 t->static_trace_marker_id.c_str ());
8941 }
8942 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8943 {
8944 t->static_trace_marker_id = std::move (marker.str_id);
8945
8946 printf_filtered (_("Probed static tracepoint "
8947 "marker \"%s\"\n"),
8948 t->static_trace_marker_id.c_str ());
8949 }
8950 else
8951 warning (_("Couldn't determine the static "
8952 "tracepoint marker to probe"));
8953 }
8954
8955 loc = b->loc;
8956 }
8957 else
8958 {
8959 loc = add_location_to_breakpoint (b, &sal);
8960 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8961 loc->inserted = 1;
8962 }
8963
8964 /* Do not set breakpoint locations conditions yet. As locations
8965 are inserted, they get sorted based on their addresses. Let
8966 the list stabilize to have reliable location numbers. */
8967
8968 /* Dynamic printf requires and uses additional arguments on the
8969 command line, otherwise it's an error. */
8970 if (type == bp_dprintf)
8971 {
8972 if (b->extra_string)
8973 update_dprintf_command_list (b);
8974 else
8975 error (_("Format string required"));
8976 }
8977 else if (b->extra_string)
8978 error (_("Garbage '%s' at end of command"), b->extra_string);
8979 }
8980
8981
8982 /* The order of the locations is now stable. Set the location
8983 condition using the location's number. */
8984 int loc_num = 1;
8985 for (bp_location *loc : b->locations ())
8986 {
8987 if (b->cond_string != nullptr)
8988 set_breakpoint_location_condition (b->cond_string, loc, b->number,
8989 loc_num);
8990
8991 ++loc_num;
8992 }
8993
8994 b->display_canonical = display_canonical;
8995 if (location != NULL)
8996 b->location = std::move (location);
8997 else
8998 b->location = new_address_location (b->loc->address, NULL, 0);
8999 b->filter = std::move (filter);
9000 }
9001
9002 static void
9003 create_breakpoint_sal (struct gdbarch *gdbarch,
9004 gdb::array_view<const symtab_and_line> sals,
9005 event_location_up &&location,
9006 gdb::unique_xmalloc_ptr<char> filter,
9007 gdb::unique_xmalloc_ptr<char> cond_string,
9008 gdb::unique_xmalloc_ptr<char> extra_string,
9009 enum bptype type, enum bpdisp disposition,
9010 int thread, int task, int ignore_count,
9011 const struct breakpoint_ops *ops, int from_tty,
9012 int enabled, int internal, unsigned flags,
9013 int display_canonical)
9014 {
9015 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
9016
9017 init_breakpoint_sal (b.get (), gdbarch,
9018 sals, std::move (location),
9019 std::move (filter),
9020 std::move (cond_string),
9021 std::move (extra_string),
9022 type, disposition,
9023 thread, task, ignore_count,
9024 ops, from_tty,
9025 enabled, internal, flags,
9026 display_canonical);
9027
9028 install_breakpoint (internal, std::move (b), 0);
9029 }
9030
9031 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9032 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9033 value. COND_STRING, if not NULL, specified the condition to be
9034 used for all breakpoints. Essentially the only case where
9035 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9036 function. In that case, it's still not possible to specify
9037 separate conditions for different overloaded functions, so
9038 we take just a single condition string.
9039
9040 NOTE: If the function succeeds, the caller is expected to cleanup
9041 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9042 array contents). If the function fails (error() is called), the
9043 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9044 COND and SALS arrays and each of those arrays contents. */
9045
9046 static void
9047 create_breakpoints_sal (struct gdbarch *gdbarch,
9048 struct linespec_result *canonical,
9049 gdb::unique_xmalloc_ptr<char> cond_string,
9050 gdb::unique_xmalloc_ptr<char> extra_string,
9051 enum bptype type, enum bpdisp disposition,
9052 int thread, int task, int ignore_count,
9053 const struct breakpoint_ops *ops, int from_tty,
9054 int enabled, int internal, unsigned flags)
9055 {
9056 if (canonical->pre_expanded)
9057 gdb_assert (canonical->lsals.size () == 1);
9058
9059 for (const auto &lsal : canonical->lsals)
9060 {
9061 /* Note that 'location' can be NULL in the case of a plain
9062 'break', without arguments. */
9063 event_location_up location
9064 = (canonical->location != NULL
9065 ? copy_event_location (canonical->location.get ()) : NULL);
9066 gdb::unique_xmalloc_ptr<char> filter_string
9067 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
9068
9069 create_breakpoint_sal (gdbarch, lsal.sals,
9070 std::move (location),
9071 std::move (filter_string),
9072 std::move (cond_string),
9073 std::move (extra_string),
9074 type, disposition,
9075 thread, task, ignore_count, ops,
9076 from_tty, enabled, internal, flags,
9077 canonical->special_display);
9078 }
9079 }
9080
9081 /* Parse LOCATION which is assumed to be a SAL specification possibly
9082 followed by conditionals. On return, SALS contains an array of SAL
9083 addresses found. LOCATION points to the end of the SAL (for
9084 linespec locations).
9085
9086 The array and the line spec strings are allocated on the heap, it is
9087 the caller's responsibility to free them. */
9088
9089 static void
9090 parse_breakpoint_sals (struct event_location *location,
9091 struct linespec_result *canonical)
9092 {
9093 struct symtab_and_line cursal;
9094
9095 if (event_location_type (location) == LINESPEC_LOCATION)
9096 {
9097 const char *spec = get_linespec_location (location)->spec_string;
9098
9099 if (spec == NULL)
9100 {
9101 /* The last displayed codepoint, if it's valid, is our default
9102 breakpoint address. */
9103 if (last_displayed_sal_is_valid ())
9104 {
9105 /* Set sal's pspace, pc, symtab, and line to the values
9106 corresponding to the last call to print_frame_info.
9107 Be sure to reinitialize LINE with NOTCURRENT == 0
9108 as the breakpoint line number is inappropriate otherwise.
9109 find_pc_line would adjust PC, re-set it back. */
9110 symtab_and_line sal = get_last_displayed_sal ();
9111 CORE_ADDR pc = sal.pc;
9112
9113 sal = find_pc_line (pc, 0);
9114
9115 /* "break" without arguments is equivalent to "break *PC"
9116 where PC is the last displayed codepoint's address. So
9117 make sure to set sal.explicit_pc to prevent GDB from
9118 trying to expand the list of sals to include all other
9119 instances with the same symtab and line. */
9120 sal.pc = pc;
9121 sal.explicit_pc = 1;
9122
9123 struct linespec_sals lsal;
9124 lsal.sals = {sal};
9125 lsal.canonical = NULL;
9126
9127 canonical->lsals.push_back (std::move (lsal));
9128 return;
9129 }
9130 else
9131 error (_("No default breakpoint address now."));
9132 }
9133 }
9134
9135 /* Force almost all breakpoints to be in terms of the
9136 current_source_symtab (which is decode_line_1's default).
9137 This should produce the results we want almost all of the
9138 time while leaving default_breakpoint_* alone.
9139
9140 ObjC: However, don't match an Objective-C method name which
9141 may have a '+' or '-' succeeded by a '['. */
9142 cursal = get_current_source_symtab_and_line ();
9143 if (last_displayed_sal_is_valid ())
9144 {
9145 const char *spec = NULL;
9146
9147 if (event_location_type (location) == LINESPEC_LOCATION)
9148 spec = get_linespec_location (location)->spec_string;
9149
9150 if (!cursal.symtab
9151 || (spec != NULL
9152 && strchr ("+-", spec[0]) != NULL
9153 && spec[1] != '['))
9154 {
9155 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9156 get_last_displayed_symtab (),
9157 get_last_displayed_line (),
9158 canonical, NULL, NULL);
9159 return;
9160 }
9161 }
9162
9163 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9164 cursal.symtab, cursal.line, canonical, NULL, NULL);
9165 }
9166
9167
9168 /* Convert each SAL into a real PC. Verify that the PC can be
9169 inserted as a breakpoint. If it can't throw an error. */
9170
9171 static void
9172 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
9173 {
9174 for (auto &sal : sals)
9175 resolve_sal_pc (&sal);
9176 }
9177
9178 /* Fast tracepoints may have restrictions on valid locations. For
9179 instance, a fast tracepoint using a jump instead of a trap will
9180 likely have to overwrite more bytes than a trap would, and so can
9181 only be placed where the instruction is longer than the jump, or a
9182 multi-instruction sequence does not have a jump into the middle of
9183 it, etc. */
9184
9185 static void
9186 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9187 gdb::array_view<const symtab_and_line> sals)
9188 {
9189 for (const auto &sal : sals)
9190 {
9191 struct gdbarch *sarch;
9192
9193 sarch = get_sal_arch (sal);
9194 /* We fall back to GDBARCH if there is no architecture
9195 associated with SAL. */
9196 if (sarch == NULL)
9197 sarch = gdbarch;
9198 std::string msg;
9199 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
9200 error (_("May not have a fast tracepoint at %s%s"),
9201 paddress (sarch, sal.pc), msg.c_str ());
9202 }
9203 }
9204
9205 /* Given TOK, a string specification of condition and thread, as
9206 accepted by the 'break' command, extract the condition
9207 string and thread number and set *COND_STRING and *THREAD.
9208 PC identifies the context at which the condition should be parsed.
9209 If no condition is found, *COND_STRING is set to NULL.
9210 If no thread is found, *THREAD is set to -1. */
9211
9212 static void
9213 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9214 char **cond_string, int *thread, int *task,
9215 char **rest)
9216 {
9217 *cond_string = NULL;
9218 *thread = -1;
9219 *task = 0;
9220 *rest = NULL;
9221 bool force = false;
9222
9223 while (tok && *tok)
9224 {
9225 const char *end_tok;
9226 int toklen;
9227 const char *cond_start = NULL;
9228 const char *cond_end = NULL;
9229
9230 tok = skip_spaces (tok);
9231
9232 if ((*tok == '"' || *tok == ',') && rest)
9233 {
9234 *rest = savestring (tok, strlen (tok));
9235 return;
9236 }
9237
9238 end_tok = skip_to_space (tok);
9239
9240 toklen = end_tok - tok;
9241
9242 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9243 {
9244 tok = cond_start = end_tok + 1;
9245 try
9246 {
9247 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9248 }
9249 catch (const gdb_exception_error &)
9250 {
9251 if (!force)
9252 throw;
9253 else
9254 tok = tok + strlen (tok);
9255 }
9256 cond_end = tok;
9257 *cond_string = savestring (cond_start, cond_end - cond_start);
9258 }
9259 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
9260 {
9261 tok = tok + toklen;
9262 force = true;
9263 }
9264 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9265 {
9266 const char *tmptok;
9267 struct thread_info *thr;
9268
9269 tok = end_tok + 1;
9270 thr = parse_thread_id (tok, &tmptok);
9271 if (tok == tmptok)
9272 error (_("Junk after thread keyword."));
9273 *thread = thr->global_num;
9274 tok = tmptok;
9275 }
9276 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9277 {
9278 char *tmptok;
9279
9280 tok = end_tok + 1;
9281 *task = strtol (tok, &tmptok, 0);
9282 if (tok == tmptok)
9283 error (_("Junk after task keyword."));
9284 if (!valid_task_id (*task))
9285 error (_("Unknown task %d."), *task);
9286 tok = tmptok;
9287 }
9288 else if (rest)
9289 {
9290 *rest = savestring (tok, strlen (tok));
9291 return;
9292 }
9293 else
9294 error (_("Junk at end of arguments."));
9295 }
9296 }
9297
9298 /* Call 'find_condition_and_thread' for each sal in SALS until a parse
9299 succeeds. The parsed values are written to COND_STRING, THREAD,
9300 TASK, and REST. See the comment of 'find_condition_and_thread'
9301 for the description of these parameters and INPUT. */
9302
9303 static void
9304 find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
9305 const char *input, char **cond_string,
9306 int *thread, int *task, char **rest)
9307 {
9308 int num_failures = 0;
9309 for (auto &sal : sals)
9310 {
9311 char *cond = nullptr;
9312 int thread_id = 0;
9313 int task_id = 0;
9314 char *remaining = nullptr;
9315
9316 /* Here we want to parse 'arg' to separate condition from thread
9317 number. But because parsing happens in a context and the
9318 contexts of sals might be different, try each until there is
9319 success. Finding one successful parse is sufficient for our
9320 goal. When setting the breakpoint we'll re-parse the
9321 condition in the context of each sal. */
9322 try
9323 {
9324 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
9325 &task_id, &remaining);
9326 *cond_string = cond;
9327 *thread = thread_id;
9328 *task = task_id;
9329 *rest = remaining;
9330 break;
9331 }
9332 catch (const gdb_exception_error &e)
9333 {
9334 num_failures++;
9335 /* If no sal remains, do not continue. */
9336 if (num_failures == sals.size ())
9337 throw;
9338 }
9339 }
9340 }
9341
9342 /* Decode a static tracepoint marker spec. */
9343
9344 static std::vector<symtab_and_line>
9345 decode_static_tracepoint_spec (const char **arg_p)
9346 {
9347 const char *p = &(*arg_p)[3];
9348 const char *endp;
9349
9350 p = skip_spaces (p);
9351
9352 endp = skip_to_space (p);
9353
9354 std::string marker_str (p, endp - p);
9355
9356 std::vector<static_tracepoint_marker> markers
9357 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9358 if (markers.empty ())
9359 error (_("No known static tracepoint marker named %s"),
9360 marker_str.c_str ());
9361
9362 std::vector<symtab_and_line> sals;
9363 sals.reserve (markers.size ());
9364
9365 for (const static_tracepoint_marker &marker : markers)
9366 {
9367 symtab_and_line sal = find_pc_line (marker.address, 0);
9368 sal.pc = marker.address;
9369 sals.push_back (sal);
9370 }
9371
9372 *arg_p = endp;
9373 return sals;
9374 }
9375
9376 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9377 according to IS_TRACEPOINT. */
9378
9379 static const struct breakpoint_ops *
9380 breakpoint_ops_for_event_location_type (enum event_location_type location_type,
9381 bool is_tracepoint)
9382 {
9383 if (is_tracepoint)
9384 {
9385 if (location_type == PROBE_LOCATION)
9386 return &tracepoint_probe_breakpoint_ops;
9387 else
9388 return &tracepoint_breakpoint_ops;
9389 }
9390 else
9391 {
9392 if (location_type == PROBE_LOCATION)
9393 return &bkpt_probe_breakpoint_ops;
9394 else
9395 return &bkpt_breakpoint_ops;
9396 }
9397 }
9398
9399 /* See breakpoint.h. */
9400
9401 const struct breakpoint_ops *
9402 breakpoint_ops_for_event_location (const struct event_location *location,
9403 bool is_tracepoint)
9404 {
9405 if (location != nullptr)
9406 return breakpoint_ops_for_event_location_type
9407 (event_location_type (location), is_tracepoint);
9408 return is_tracepoint ? &tracepoint_breakpoint_ops : &bkpt_breakpoint_ops;
9409 }
9410
9411 /* See breakpoint.h. */
9412
9413 int
9414 create_breakpoint (struct gdbarch *gdbarch,
9415 struct event_location *location,
9416 const char *cond_string,
9417 int thread, const char *extra_string,
9418 bool force_condition, int parse_extra,
9419 int tempflag, enum bptype type_wanted,
9420 int ignore_count,
9421 enum auto_boolean pending_break_support,
9422 const struct breakpoint_ops *ops,
9423 int from_tty, int enabled, int internal,
9424 unsigned flags)
9425 {
9426 struct linespec_result canonical;
9427 int pending = 0;
9428 int task = 0;
9429 int prev_bkpt_count = breakpoint_count;
9430
9431 gdb_assert (ops != NULL);
9432
9433 /* If extra_string isn't useful, set it to NULL. */
9434 if (extra_string != NULL && *extra_string == '\0')
9435 extra_string = NULL;
9436
9437 try
9438 {
9439 ops->create_sals_from_location (location, &canonical, type_wanted);
9440 }
9441 catch (const gdb_exception_error &e)
9442 {
9443 /* If caller is interested in rc value from parse, set
9444 value. */
9445 if (e.error == NOT_FOUND_ERROR)
9446 {
9447 /* If pending breakpoint support is turned off, throw
9448 error. */
9449
9450 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9451 throw;
9452
9453 exception_print (gdb_stderr, e);
9454
9455 /* If pending breakpoint support is auto query and the user
9456 selects no, then simply return the error code. */
9457 if (pending_break_support == AUTO_BOOLEAN_AUTO
9458 && !nquery (_("Make %s pending on future shared library load? "),
9459 bptype_string (type_wanted)))
9460 return 0;
9461
9462 /* At this point, either the user was queried about setting
9463 a pending breakpoint and selected yes, or pending
9464 breakpoint behavior is on and thus a pending breakpoint
9465 is defaulted on behalf of the user. */
9466 pending = 1;
9467 }
9468 else
9469 throw;
9470 }
9471
9472 if (!pending && canonical.lsals.empty ())
9473 return 0;
9474
9475 /* Resolve all line numbers to PC's and verify that the addresses
9476 are ok for the target. */
9477 if (!pending)
9478 {
9479 for (auto &lsal : canonical.lsals)
9480 breakpoint_sals_to_pc (lsal.sals);
9481 }
9482
9483 /* Fast tracepoints may have additional restrictions on location. */
9484 if (!pending && type_wanted == bp_fast_tracepoint)
9485 {
9486 for (const auto &lsal : canonical.lsals)
9487 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9488 }
9489
9490 /* Verify that condition can be parsed, before setting any
9491 breakpoints. Allocate a separate condition expression for each
9492 breakpoint. */
9493 if (!pending)
9494 {
9495 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9496 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9497
9498 if (parse_extra)
9499 {
9500 char *rest;
9501 char *cond;
9502
9503 const linespec_sals &lsal = canonical.lsals[0];
9504
9505 find_condition_and_thread_for_sals (lsal.sals, extra_string,
9506 &cond, &thread, &task, &rest);
9507 cond_string_copy.reset (cond);
9508 extra_string_copy.reset (rest);
9509 }
9510 else
9511 {
9512 if (type_wanted != bp_dprintf
9513 && extra_string != NULL && *extra_string != '\0')
9514 error (_("Garbage '%s' at end of location"), extra_string);
9515
9516 /* Check the validity of the condition. We should error out
9517 if the condition is invalid at all of the locations and
9518 if it is not forced. In the PARSE_EXTRA case above, this
9519 check is done when parsing the EXTRA_STRING. */
9520 if (cond_string != nullptr && !force_condition)
9521 {
9522 int num_failures = 0;
9523 const linespec_sals &lsal = canonical.lsals[0];
9524 for (const auto &sal : lsal.sals)
9525 {
9526 const char *cond = cond_string;
9527 try
9528 {
9529 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
9530 /* One success is sufficient to keep going. */
9531 break;
9532 }
9533 catch (const gdb_exception_error &)
9534 {
9535 num_failures++;
9536 /* If this is the last sal, error out. */
9537 if (num_failures == lsal.sals.size ())
9538 throw;
9539 }
9540 }
9541 }
9542
9543 /* Create a private copy of condition string. */
9544 if (cond_string)
9545 cond_string_copy.reset (xstrdup (cond_string));
9546 /* Create a private copy of any extra string. */
9547 if (extra_string)
9548 extra_string_copy.reset (xstrdup (extra_string));
9549 }
9550
9551 ops->create_breakpoints_sal (gdbarch, &canonical,
9552 std::move (cond_string_copy),
9553 std::move (extra_string_copy),
9554 type_wanted,
9555 tempflag ? disp_del : disp_donttouch,
9556 thread, task, ignore_count, ops,
9557 from_tty, enabled, internal, flags);
9558 }
9559 else
9560 {
9561 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9562
9563 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9564 b->location = copy_event_location (location);
9565
9566 if (parse_extra)
9567 b->cond_string = NULL;
9568 else
9569 {
9570 /* Create a private copy of condition string. */
9571 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
9572 b->thread = thread;
9573 }
9574
9575 /* Create a private copy of any extra string. */
9576 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
9577 b->ignore_count = ignore_count;
9578 b->disposition = tempflag ? disp_del : disp_donttouch;
9579 b->condition_not_parsed = 1;
9580 b->enable_state = enabled ? bp_enabled : bp_disabled;
9581 if ((type_wanted != bp_breakpoint
9582 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9583 b->pspace = current_program_space;
9584
9585 install_breakpoint (internal, std::move (b), 0);
9586 }
9587
9588 if (canonical.lsals.size () > 1)
9589 {
9590 warning (_("Multiple breakpoints were set.\nUse the "
9591 "\"delete\" command to delete unwanted breakpoints."));
9592 prev_breakpoint_count = prev_bkpt_count;
9593 }
9594
9595 update_global_location_list (UGLL_MAY_INSERT);
9596
9597 return 1;
9598 }
9599
9600 /* Set a breakpoint.
9601 ARG is a string describing breakpoint address,
9602 condition, and thread.
9603 FLAG specifies if a breakpoint is hardware on,
9604 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9605 and BP_TEMPFLAG. */
9606
9607 static void
9608 break_command_1 (const char *arg, int flag, int from_tty)
9609 {
9610 int tempflag = flag & BP_TEMPFLAG;
9611 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9612 ? bp_hardware_breakpoint
9613 : bp_breakpoint);
9614
9615 event_location_up location = string_to_event_location (&arg, current_language);
9616 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9617 (location.get (), false /* is_tracepoint */);
9618
9619 create_breakpoint (get_current_arch (),
9620 location.get (),
9621 NULL, 0, arg, false, 1 /* parse arg */,
9622 tempflag, type_wanted,
9623 0 /* Ignore count */,
9624 pending_break_support,
9625 ops,
9626 from_tty,
9627 1 /* enabled */,
9628 0 /* internal */,
9629 0);
9630 }
9631
9632 /* Helper function for break_command_1 and disassemble_command. */
9633
9634 void
9635 resolve_sal_pc (struct symtab_and_line *sal)
9636 {
9637 CORE_ADDR pc;
9638
9639 if (sal->pc == 0 && sal->symtab != NULL)
9640 {
9641 if (!find_line_pc (sal->symtab, sal->line, &pc))
9642 error (_("No line %d in file \"%s\"."),
9643 sal->line, symtab_to_filename_for_display (sal->symtab));
9644 sal->pc = pc;
9645
9646 /* If this SAL corresponds to a breakpoint inserted using a line
9647 number, then skip the function prologue if necessary. */
9648 if (sal->explicit_line)
9649 skip_prologue_sal (sal);
9650 }
9651
9652 if (sal->section == 0 && sal->symtab != NULL)
9653 {
9654 const struct blockvector *bv;
9655 const struct block *b;
9656 struct symbol *sym;
9657
9658 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9659 SYMTAB_COMPUNIT (sal->symtab));
9660 if (bv != NULL)
9661 {
9662 sym = block_linkage_function (b);
9663 if (sym != NULL)
9664 {
9665 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9666 sal->section = sym->obj_section (SYMTAB_OBJFILE (sal->symtab));
9667 }
9668 else
9669 {
9670 /* It really is worthwhile to have the section, so we'll
9671 just have to look harder. This case can be executed
9672 if we have line numbers but no functions (as can
9673 happen in assembly source). */
9674
9675 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9676 switch_to_program_space_and_thread (sal->pspace);
9677
9678 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9679 if (msym.minsym)
9680 sal->section = msym.obj_section ();
9681 }
9682 }
9683 }
9684 }
9685
9686 void
9687 break_command (const char *arg, int from_tty)
9688 {
9689 break_command_1 (arg, 0, from_tty);
9690 }
9691
9692 void
9693 tbreak_command (const char *arg, int from_tty)
9694 {
9695 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9696 }
9697
9698 static void
9699 hbreak_command (const char *arg, int from_tty)
9700 {
9701 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9702 }
9703
9704 static void
9705 thbreak_command (const char *arg, int from_tty)
9706 {
9707 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9708 }
9709
9710 static void
9711 stop_command (const char *arg, int from_tty)
9712 {
9713 printf_filtered (_("Specify the type of breakpoint to set.\n\
9714 Usage: stop in <function | address>\n\
9715 stop at <line>\n"));
9716 }
9717
9718 static void
9719 stopin_command (const char *arg, int from_tty)
9720 {
9721 int badInput = 0;
9722
9723 if (arg == NULL)
9724 badInput = 1;
9725 else if (*arg != '*')
9726 {
9727 const char *argptr = arg;
9728 int hasColon = 0;
9729
9730 /* Look for a ':'. If this is a line number specification, then
9731 say it is bad, otherwise, it should be an address or
9732 function/method name. */
9733 while (*argptr && !hasColon)
9734 {
9735 hasColon = (*argptr == ':');
9736 argptr++;
9737 }
9738
9739 if (hasColon)
9740 badInput = (*argptr != ':'); /* Not a class::method */
9741 else
9742 badInput = isdigit (*arg); /* a simple line number */
9743 }
9744
9745 if (badInput)
9746 printf_filtered (_("Usage: stop in <function | address>\n"));
9747 else
9748 break_command_1 (arg, 0, from_tty);
9749 }
9750
9751 static void
9752 stopat_command (const char *arg, int from_tty)
9753 {
9754 int badInput = 0;
9755
9756 if (arg == NULL || *arg == '*') /* no line number */
9757 badInput = 1;
9758 else
9759 {
9760 const char *argptr = arg;
9761 int hasColon = 0;
9762
9763 /* Look for a ':'. If there is a '::' then get out, otherwise
9764 it is probably a line number. */
9765 while (*argptr && !hasColon)
9766 {
9767 hasColon = (*argptr == ':');
9768 argptr++;
9769 }
9770
9771 if (hasColon)
9772 badInput = (*argptr == ':'); /* we have class::method */
9773 else
9774 badInput = !isdigit (*arg); /* not a line number */
9775 }
9776
9777 if (badInput)
9778 printf_filtered (_("Usage: stop at LINE\n"));
9779 else
9780 break_command_1 (arg, 0, from_tty);
9781 }
9782
9783 /* The dynamic printf command is mostly like a regular breakpoint, but
9784 with a prewired command list consisting of a single output command,
9785 built from extra arguments supplied on the dprintf command
9786 line. */
9787
9788 static void
9789 dprintf_command (const char *arg, int from_tty)
9790 {
9791 event_location_up location = string_to_event_location (&arg, current_language);
9792
9793 /* If non-NULL, ARG should have been advanced past the location;
9794 the next character must be ','. */
9795 if (arg != NULL)
9796 {
9797 if (arg[0] != ',' || arg[1] == '\0')
9798 error (_("Format string required"));
9799 else
9800 {
9801 /* Skip the comma. */
9802 ++arg;
9803 }
9804 }
9805
9806 create_breakpoint (get_current_arch (),
9807 location.get (),
9808 NULL, 0, arg, false, 1 /* parse arg */,
9809 0, bp_dprintf,
9810 0 /* Ignore count */,
9811 pending_break_support,
9812 &dprintf_breakpoint_ops,
9813 from_tty,
9814 1 /* enabled */,
9815 0 /* internal */,
9816 0);
9817 }
9818
9819 static void
9820 agent_printf_command (const char *arg, int from_tty)
9821 {
9822 error (_("May only run agent-printf on the target"));
9823 }
9824
9825 /* Implement the "breakpoint_hit" breakpoint_ops method for
9826 ranged breakpoints. */
9827
9828 static int
9829 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9830 const address_space *aspace,
9831 CORE_ADDR bp_addr,
9832 const struct target_waitstatus *ws)
9833 {
9834 if (ws->kind != TARGET_WAITKIND_STOPPED
9835 || ws->value.sig != GDB_SIGNAL_TRAP)
9836 return 0;
9837
9838 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9839 bl->length, aspace, bp_addr);
9840 }
9841
9842 /* Implement the "resources_needed" breakpoint_ops method for
9843 ranged breakpoints. */
9844
9845 static int
9846 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9847 {
9848 return target_ranged_break_num_registers ();
9849 }
9850
9851 /* Implement the "print_it" breakpoint_ops method for
9852 ranged breakpoints. */
9853
9854 static enum print_stop_action
9855 print_it_ranged_breakpoint (bpstat bs)
9856 {
9857 struct breakpoint *b = bs->breakpoint_at;
9858 struct bp_location *bl = b->loc;
9859 struct ui_out *uiout = current_uiout;
9860
9861 gdb_assert (b->type == bp_hardware_breakpoint);
9862
9863 /* Ranged breakpoints have only one location. */
9864 gdb_assert (bl && bl->next == NULL);
9865
9866 annotate_breakpoint (b->number);
9867
9868 maybe_print_thread_hit_breakpoint (uiout);
9869
9870 if (b->disposition == disp_del)
9871 uiout->text ("Temporary ranged breakpoint ");
9872 else
9873 uiout->text ("Ranged breakpoint ");
9874 if (uiout->is_mi_like_p ())
9875 {
9876 uiout->field_string ("reason",
9877 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9878 uiout->field_string ("disp", bpdisp_text (b->disposition));
9879 }
9880 uiout->field_signed ("bkptno", b->number);
9881 uiout->text (", ");
9882
9883 return PRINT_SRC_AND_LOC;
9884 }
9885
9886 /* Implement the "print_one" breakpoint_ops method for
9887 ranged breakpoints. */
9888
9889 static void
9890 print_one_ranged_breakpoint (struct breakpoint *b,
9891 struct bp_location **last_loc)
9892 {
9893 struct bp_location *bl = b->loc;
9894 struct value_print_options opts;
9895 struct ui_out *uiout = current_uiout;
9896
9897 /* Ranged breakpoints have only one location. */
9898 gdb_assert (bl && bl->next == NULL);
9899
9900 get_user_print_options (&opts);
9901
9902 if (opts.addressprint)
9903 /* We don't print the address range here, it will be printed later
9904 by print_one_detail_ranged_breakpoint. */
9905 uiout->field_skip ("addr");
9906 annotate_field (5);
9907 print_breakpoint_location (b, bl);
9908 *last_loc = bl;
9909 }
9910
9911 /* Implement the "print_one_detail" breakpoint_ops method for
9912 ranged breakpoints. */
9913
9914 static void
9915 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9916 struct ui_out *uiout)
9917 {
9918 CORE_ADDR address_start, address_end;
9919 struct bp_location *bl = b->loc;
9920 string_file stb;
9921
9922 gdb_assert (bl);
9923
9924 address_start = bl->address;
9925 address_end = address_start + bl->length - 1;
9926
9927 uiout->text ("\taddress range: ");
9928 stb.printf ("[%s, %s]",
9929 print_core_address (bl->gdbarch, address_start),
9930 print_core_address (bl->gdbarch, address_end));
9931 uiout->field_stream ("addr", stb);
9932 uiout->text ("\n");
9933 }
9934
9935 /* Implement the "print_mention" breakpoint_ops method for
9936 ranged breakpoints. */
9937
9938 static void
9939 print_mention_ranged_breakpoint (struct breakpoint *b)
9940 {
9941 struct bp_location *bl = b->loc;
9942 struct ui_out *uiout = current_uiout;
9943
9944 gdb_assert (bl);
9945 gdb_assert (b->type == bp_hardware_breakpoint);
9946
9947 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9948 b->number, paddress (bl->gdbarch, bl->address),
9949 paddress (bl->gdbarch, bl->address + bl->length - 1));
9950 }
9951
9952 /* Implement the "print_recreate" breakpoint_ops method for
9953 ranged breakpoints. */
9954
9955 static void
9956 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9957 {
9958 fprintf_unfiltered (fp, "break-range %s, %s",
9959 event_location_to_string (b->location.get ()),
9960 event_location_to_string (b->location_range_end.get ()));
9961 print_recreate_thread (b, fp);
9962 }
9963
9964 /* The breakpoint_ops structure to be used in ranged breakpoints. */
9965
9966 static struct breakpoint_ops ranged_breakpoint_ops;
9967
9968 /* Find the address where the end of the breakpoint range should be
9969 placed, given the SAL of the end of the range. This is so that if
9970 the user provides a line number, the end of the range is set to the
9971 last instruction of the given line. */
9972
9973 static CORE_ADDR
9974 find_breakpoint_range_end (struct symtab_and_line sal)
9975 {
9976 CORE_ADDR end;
9977
9978 /* If the user provided a PC value, use it. Otherwise,
9979 find the address of the end of the given location. */
9980 if (sal.explicit_pc)
9981 end = sal.pc;
9982 else
9983 {
9984 int ret;
9985 CORE_ADDR start;
9986
9987 ret = find_line_pc_range (sal, &start, &end);
9988 if (!ret)
9989 error (_("Could not find location of the end of the range."));
9990
9991 /* find_line_pc_range returns the start of the next line. */
9992 end--;
9993 }
9994
9995 return end;
9996 }
9997
9998 /* Implement the "break-range" CLI command. */
9999
10000 static void
10001 break_range_command (const char *arg, int from_tty)
10002 {
10003 const char *arg_start;
10004 struct linespec_result canonical_start, canonical_end;
10005 int bp_count, can_use_bp, length;
10006 CORE_ADDR end;
10007 struct breakpoint *b;
10008
10009 /* We don't support software ranged breakpoints. */
10010 if (target_ranged_break_num_registers () < 0)
10011 error (_("This target does not support hardware ranged breakpoints."));
10012
10013 bp_count = hw_breakpoint_used_count ();
10014 bp_count += target_ranged_break_num_registers ();
10015 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10016 bp_count, 0);
10017 if (can_use_bp < 0)
10018 error (_("Hardware breakpoints used exceeds limit."));
10019
10020 arg = skip_spaces (arg);
10021 if (arg == NULL || arg[0] == '\0')
10022 error(_("No address range specified."));
10023
10024 arg_start = arg;
10025 event_location_up start_location = string_to_event_location (&arg,
10026 current_language);
10027 parse_breakpoint_sals (start_location.get (), &canonical_start);
10028
10029 if (arg[0] != ',')
10030 error (_("Too few arguments."));
10031 else if (canonical_start.lsals.empty ())
10032 error (_("Could not find location of the beginning of the range."));
10033
10034 const linespec_sals &lsal_start = canonical_start.lsals[0];
10035
10036 if (canonical_start.lsals.size () > 1
10037 || lsal_start.sals.size () != 1)
10038 error (_("Cannot create a ranged breakpoint with multiple locations."));
10039
10040 const symtab_and_line &sal_start = lsal_start.sals[0];
10041 std::string addr_string_start (arg_start, arg - arg_start);
10042
10043 arg++; /* Skip the comma. */
10044 arg = skip_spaces (arg);
10045
10046 /* Parse the end location. */
10047
10048 arg_start = arg;
10049
10050 /* We call decode_line_full directly here instead of using
10051 parse_breakpoint_sals because we need to specify the start location's
10052 symtab and line as the default symtab and line for the end of the
10053 range. This makes it possible to have ranges like "foo.c:27, +14",
10054 where +14 means 14 lines from the start location. */
10055 event_location_up end_location = string_to_event_location (&arg,
10056 current_language);
10057 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10058 sal_start.symtab, sal_start.line,
10059 &canonical_end, NULL, NULL);
10060
10061 if (canonical_end.lsals.empty ())
10062 error (_("Could not find location of the end of the range."));
10063
10064 const linespec_sals &lsal_end = canonical_end.lsals[0];
10065 if (canonical_end.lsals.size () > 1
10066 || lsal_end.sals.size () != 1)
10067 error (_("Cannot create a ranged breakpoint with multiple locations."));
10068
10069 const symtab_and_line &sal_end = lsal_end.sals[0];
10070
10071 end = find_breakpoint_range_end (sal_end);
10072 if (sal_start.pc > end)
10073 error (_("Invalid address range, end precedes start."));
10074
10075 length = end - sal_start.pc + 1;
10076 if (length < 0)
10077 /* Length overflowed. */
10078 error (_("Address range too large."));
10079 else if (length == 1)
10080 {
10081 /* This range is simple enough to be handled by
10082 the `hbreak' command. */
10083 hbreak_command (&addr_string_start[0], 1);
10084
10085 return;
10086 }
10087
10088 /* Now set up the breakpoint. */
10089 b = set_raw_breakpoint (get_current_arch (), sal_start,
10090 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10091 set_breakpoint_count (breakpoint_count + 1);
10092 b->number = breakpoint_count;
10093 b->disposition = disp_donttouch;
10094 b->location = std::move (start_location);
10095 b->location_range_end = std::move (end_location);
10096 b->loc->length = length;
10097
10098 mention (b);
10099 gdb::observers::breakpoint_created.notify (b);
10100 update_global_location_list (UGLL_MAY_INSERT);
10101 }
10102
10103 /* Return non-zero if EXP is verified as constant. Returned zero
10104 means EXP is variable. Also the constant detection may fail for
10105 some constant expressions and in such case still falsely return
10106 zero. */
10107
10108 static bool
10109 watchpoint_exp_is_const (const struct expression *exp)
10110 {
10111 return exp->op->constant_p ();
10112 }
10113
10114 /* Watchpoint destructor. */
10115
10116 watchpoint::~watchpoint ()
10117 {
10118 xfree (this->exp_string);
10119 xfree (this->exp_string_reparse);
10120 }
10121
10122 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10123
10124 static void
10125 re_set_watchpoint (struct breakpoint *b)
10126 {
10127 struct watchpoint *w = (struct watchpoint *) b;
10128
10129 /* Watchpoint can be either on expression using entirely global
10130 variables, or it can be on local variables.
10131
10132 Watchpoints of the first kind are never auto-deleted, and even
10133 persist across program restarts. Since they can use variables
10134 from shared libraries, we need to reparse expression as libraries
10135 are loaded and unloaded.
10136
10137 Watchpoints on local variables can also change meaning as result
10138 of solib event. For example, if a watchpoint uses both a local
10139 and a global variables in expression, it's a local watchpoint,
10140 but unloading of a shared library will make the expression
10141 invalid. This is not a very common use case, but we still
10142 re-evaluate expression, to avoid surprises to the user.
10143
10144 Note that for local watchpoints, we re-evaluate it only if
10145 watchpoints frame id is still valid. If it's not, it means the
10146 watchpoint is out of scope and will be deleted soon. In fact,
10147 I'm not sure we'll ever be called in this case.
10148
10149 If a local watchpoint's frame id is still valid, then
10150 w->exp_valid_block is likewise valid, and we can safely use it.
10151
10152 Don't do anything about disabled watchpoints, since they will be
10153 reevaluated again when enabled. */
10154 update_watchpoint (w, 1 /* reparse */);
10155 }
10156
10157 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10158
10159 static int
10160 insert_watchpoint (struct bp_location *bl)
10161 {
10162 struct watchpoint *w = (struct watchpoint *) bl->owner;
10163 int length = w->exact ? 1 : bl->length;
10164
10165 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10166 w->cond_exp.get ());
10167 }
10168
10169 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10170
10171 static int
10172 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10173 {
10174 struct watchpoint *w = (struct watchpoint *) bl->owner;
10175 int length = w->exact ? 1 : bl->length;
10176
10177 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10178 w->cond_exp.get ());
10179 }
10180
10181 static int
10182 breakpoint_hit_watchpoint (const struct bp_location *bl,
10183 const address_space *aspace, CORE_ADDR bp_addr,
10184 const struct target_waitstatus *ws)
10185 {
10186 struct breakpoint *b = bl->owner;
10187 struct watchpoint *w = (struct watchpoint *) b;
10188
10189 /* Continuable hardware watchpoints are treated as non-existent if the
10190 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10191 some data address). Otherwise gdb won't stop on a break instruction
10192 in the code (not from a breakpoint) when a hardware watchpoint has
10193 been defined. Also skip watchpoints which we know did not trigger
10194 (did not match the data address). */
10195 if (is_hardware_watchpoint (b)
10196 && w->watchpoint_triggered == watch_triggered_no)
10197 return 0;
10198
10199 return 1;
10200 }
10201
10202 static void
10203 check_status_watchpoint (bpstat bs)
10204 {
10205 gdb_assert (is_watchpoint (bs->breakpoint_at));
10206
10207 bpstat_check_watchpoint (bs);
10208 }
10209
10210 /* Implement the "resources_needed" breakpoint_ops method for
10211 hardware watchpoints. */
10212
10213 static int
10214 resources_needed_watchpoint (const struct bp_location *bl)
10215 {
10216 struct watchpoint *w = (struct watchpoint *) bl->owner;
10217 int length = w->exact? 1 : bl->length;
10218
10219 return target_region_ok_for_hw_watchpoint (bl->address, length);
10220 }
10221
10222 /* Implement the "works_in_software_mode" breakpoint_ops method for
10223 hardware watchpoints. */
10224
10225 static int
10226 works_in_software_mode_watchpoint (const struct breakpoint *b)
10227 {
10228 /* Read and access watchpoints only work with hardware support. */
10229 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10230 }
10231
10232 static enum print_stop_action
10233 print_it_watchpoint (bpstat bs)
10234 {
10235 struct breakpoint *b;
10236 enum print_stop_action result;
10237 struct watchpoint *w;
10238 struct ui_out *uiout = current_uiout;
10239
10240 gdb_assert (bs->bp_location_at != NULL);
10241
10242 b = bs->breakpoint_at;
10243 w = (struct watchpoint *) b;
10244
10245 annotate_watchpoint (b->number);
10246 maybe_print_thread_hit_breakpoint (uiout);
10247
10248 string_file stb;
10249
10250 gdb::optional<ui_out_emit_tuple> tuple_emitter;
10251 switch (b->type)
10252 {
10253 case bp_watchpoint:
10254 case bp_hardware_watchpoint:
10255 if (uiout->is_mi_like_p ())
10256 uiout->field_string
10257 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10258 mention (b);
10259 tuple_emitter.emplace (uiout, "value");
10260 uiout->text ("\nOld value = ");
10261 watchpoint_value_print (bs->old_val.get (), &stb);
10262 uiout->field_stream ("old", stb);
10263 uiout->text ("\nNew value = ");
10264 watchpoint_value_print (w->val.get (), &stb);
10265 uiout->field_stream ("new", stb);
10266 uiout->text ("\n");
10267 /* More than one watchpoint may have been triggered. */
10268 result = PRINT_UNKNOWN;
10269 break;
10270
10271 case bp_read_watchpoint:
10272 if (uiout->is_mi_like_p ())
10273 uiout->field_string
10274 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10275 mention (b);
10276 tuple_emitter.emplace (uiout, "value");
10277 uiout->text ("\nValue = ");
10278 watchpoint_value_print (w->val.get (), &stb);
10279 uiout->field_stream ("value", stb);
10280 uiout->text ("\n");
10281 result = PRINT_UNKNOWN;
10282 break;
10283
10284 case bp_access_watchpoint:
10285 if (bs->old_val != NULL)
10286 {
10287 if (uiout->is_mi_like_p ())
10288 uiout->field_string
10289 ("reason",
10290 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10291 mention (b);
10292 tuple_emitter.emplace (uiout, "value");
10293 uiout->text ("\nOld value = ");
10294 watchpoint_value_print (bs->old_val.get (), &stb);
10295 uiout->field_stream ("old", stb);
10296 uiout->text ("\nNew value = ");
10297 }
10298 else
10299 {
10300 mention (b);
10301 if (uiout->is_mi_like_p ())
10302 uiout->field_string
10303 ("reason",
10304 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10305 tuple_emitter.emplace (uiout, "value");
10306 uiout->text ("\nValue = ");
10307 }
10308 watchpoint_value_print (w->val.get (), &stb);
10309 uiout->field_stream ("new", stb);
10310 uiout->text ("\n");
10311 result = PRINT_UNKNOWN;
10312 break;
10313 default:
10314 result = PRINT_UNKNOWN;
10315 }
10316
10317 return result;
10318 }
10319
10320 /* Implement the "print_mention" breakpoint_ops method for hardware
10321 watchpoints. */
10322
10323 static void
10324 print_mention_watchpoint (struct breakpoint *b)
10325 {
10326 struct watchpoint *w = (struct watchpoint *) b;
10327 struct ui_out *uiout = current_uiout;
10328 const char *tuple_name;
10329
10330 switch (b->type)
10331 {
10332 case bp_watchpoint:
10333 uiout->text ("Watchpoint ");
10334 tuple_name = "wpt";
10335 break;
10336 case bp_hardware_watchpoint:
10337 uiout->text ("Hardware watchpoint ");
10338 tuple_name = "wpt";
10339 break;
10340 case bp_read_watchpoint:
10341 uiout->text ("Hardware read watchpoint ");
10342 tuple_name = "hw-rwpt";
10343 break;
10344 case bp_access_watchpoint:
10345 uiout->text ("Hardware access (read/write) watchpoint ");
10346 tuple_name = "hw-awpt";
10347 break;
10348 default:
10349 internal_error (__FILE__, __LINE__,
10350 _("Invalid hardware watchpoint type."));
10351 }
10352
10353 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10354 uiout->field_signed ("number", b->number);
10355 uiout->text (": ");
10356 uiout->field_string ("exp", w->exp_string);
10357 }
10358
10359 /* Implement the "print_recreate" breakpoint_ops method for
10360 watchpoints. */
10361
10362 static void
10363 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10364 {
10365 struct watchpoint *w = (struct watchpoint *) b;
10366
10367 switch (b->type)
10368 {
10369 case bp_watchpoint:
10370 case bp_hardware_watchpoint:
10371 fprintf_unfiltered (fp, "watch");
10372 break;
10373 case bp_read_watchpoint:
10374 fprintf_unfiltered (fp, "rwatch");
10375 break;
10376 case bp_access_watchpoint:
10377 fprintf_unfiltered (fp, "awatch");
10378 break;
10379 default:
10380 internal_error (__FILE__, __LINE__,
10381 _("Invalid watchpoint type."));
10382 }
10383
10384 fprintf_unfiltered (fp, " %s", w->exp_string);
10385 print_recreate_thread (b, fp);
10386 }
10387
10388 /* Implement the "explains_signal" breakpoint_ops method for
10389 watchpoints. */
10390
10391 static int
10392 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10393 {
10394 /* A software watchpoint cannot cause a signal other than
10395 GDB_SIGNAL_TRAP. */
10396 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10397 return 0;
10398
10399 return 1;
10400 }
10401
10402 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10403
10404 static struct breakpoint_ops watchpoint_breakpoint_ops;
10405
10406 /* Implement the "insert" breakpoint_ops method for
10407 masked hardware watchpoints. */
10408
10409 static int
10410 insert_masked_watchpoint (struct bp_location *bl)
10411 {
10412 struct watchpoint *w = (struct watchpoint *) bl->owner;
10413
10414 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10415 bl->watchpoint_type);
10416 }
10417
10418 /* Implement the "remove" breakpoint_ops method for
10419 masked hardware watchpoints. */
10420
10421 static int
10422 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10423 {
10424 struct watchpoint *w = (struct watchpoint *) bl->owner;
10425
10426 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10427 bl->watchpoint_type);
10428 }
10429
10430 /* Implement the "resources_needed" breakpoint_ops method for
10431 masked hardware watchpoints. */
10432
10433 static int
10434 resources_needed_masked_watchpoint (const struct bp_location *bl)
10435 {
10436 struct watchpoint *w = (struct watchpoint *) bl->owner;
10437
10438 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10439 }
10440
10441 /* Implement the "works_in_software_mode" breakpoint_ops method for
10442 masked hardware watchpoints. */
10443
10444 static int
10445 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10446 {
10447 return 0;
10448 }
10449
10450 /* Implement the "print_it" breakpoint_ops method for
10451 masked hardware watchpoints. */
10452
10453 static enum print_stop_action
10454 print_it_masked_watchpoint (bpstat bs)
10455 {
10456 struct breakpoint *b = bs->breakpoint_at;
10457 struct ui_out *uiout = current_uiout;
10458
10459 /* Masked watchpoints have only one location. */
10460 gdb_assert (b->loc && b->loc->next == NULL);
10461
10462 annotate_watchpoint (b->number);
10463 maybe_print_thread_hit_breakpoint (uiout);
10464
10465 switch (b->type)
10466 {
10467 case bp_hardware_watchpoint:
10468 if (uiout->is_mi_like_p ())
10469 uiout->field_string
10470 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10471 break;
10472
10473 case bp_read_watchpoint:
10474 if (uiout->is_mi_like_p ())
10475 uiout->field_string
10476 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10477 break;
10478
10479 case bp_access_watchpoint:
10480 if (uiout->is_mi_like_p ())
10481 uiout->field_string
10482 ("reason",
10483 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10484 break;
10485 default:
10486 internal_error (__FILE__, __LINE__,
10487 _("Invalid hardware watchpoint type."));
10488 }
10489
10490 mention (b);
10491 uiout->text (_("\n\
10492 Check the underlying instruction at PC for the memory\n\
10493 address and value which triggered this watchpoint.\n"));
10494 uiout->text ("\n");
10495
10496 /* More than one watchpoint may have been triggered. */
10497 return PRINT_UNKNOWN;
10498 }
10499
10500 /* Implement the "print_one_detail" breakpoint_ops method for
10501 masked hardware watchpoints. */
10502
10503 static void
10504 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10505 struct ui_out *uiout)
10506 {
10507 struct watchpoint *w = (struct watchpoint *) b;
10508
10509 /* Masked watchpoints have only one location. */
10510 gdb_assert (b->loc && b->loc->next == NULL);
10511
10512 uiout->text ("\tmask ");
10513 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10514 uiout->text ("\n");
10515 }
10516
10517 /* Implement the "print_mention" breakpoint_ops method for
10518 masked hardware watchpoints. */
10519
10520 static void
10521 print_mention_masked_watchpoint (struct breakpoint *b)
10522 {
10523 struct watchpoint *w = (struct watchpoint *) b;
10524 struct ui_out *uiout = current_uiout;
10525 const char *tuple_name;
10526
10527 switch (b->type)
10528 {
10529 case bp_hardware_watchpoint:
10530 uiout->text ("Masked hardware watchpoint ");
10531 tuple_name = "wpt";
10532 break;
10533 case bp_read_watchpoint:
10534 uiout->text ("Masked hardware read watchpoint ");
10535 tuple_name = "hw-rwpt";
10536 break;
10537 case bp_access_watchpoint:
10538 uiout->text ("Masked hardware access (read/write) watchpoint ");
10539 tuple_name = "hw-awpt";
10540 break;
10541 default:
10542 internal_error (__FILE__, __LINE__,
10543 _("Invalid hardware watchpoint type."));
10544 }
10545
10546 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10547 uiout->field_signed ("number", b->number);
10548 uiout->text (": ");
10549 uiout->field_string ("exp", w->exp_string);
10550 }
10551
10552 /* Implement the "print_recreate" breakpoint_ops method for
10553 masked hardware watchpoints. */
10554
10555 static void
10556 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10557 {
10558 struct watchpoint *w = (struct watchpoint *) b;
10559
10560 switch (b->type)
10561 {
10562 case bp_hardware_watchpoint:
10563 fprintf_unfiltered (fp, "watch");
10564 break;
10565 case bp_read_watchpoint:
10566 fprintf_unfiltered (fp, "rwatch");
10567 break;
10568 case bp_access_watchpoint:
10569 fprintf_unfiltered (fp, "awatch");
10570 break;
10571 default:
10572 internal_error (__FILE__, __LINE__,
10573 _("Invalid hardware watchpoint type."));
10574 }
10575
10576 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string,
10577 phex (w->hw_wp_mask, sizeof (CORE_ADDR)));
10578 print_recreate_thread (b, fp);
10579 }
10580
10581 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10582
10583 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10584
10585 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10586
10587 static bool
10588 is_masked_watchpoint (const struct breakpoint *b)
10589 {
10590 return b->ops == &masked_watchpoint_breakpoint_ops;
10591 }
10592
10593 /* accessflag: hw_write: watch write,
10594 hw_read: watch read,
10595 hw_access: watch access (read or write) */
10596 static void
10597 watch_command_1 (const char *arg, int accessflag, int from_tty,
10598 bool just_location, bool internal)
10599 {
10600 struct breakpoint *scope_breakpoint = NULL;
10601 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10602 struct value *result;
10603 int saved_bitpos = 0, saved_bitsize = 0;
10604 const char *exp_start = NULL;
10605 const char *exp_end = NULL;
10606 const char *tok, *end_tok;
10607 int toklen = -1;
10608 const char *cond_start = NULL;
10609 const char *cond_end = NULL;
10610 enum bptype bp_type;
10611 int thread = -1;
10612 /* Flag to indicate whether we are going to use masks for
10613 the hardware watchpoint. */
10614 bool use_mask = false;
10615 CORE_ADDR mask = 0;
10616
10617 /* Make sure that we actually have parameters to parse. */
10618 if (arg != NULL && arg[0] != '\0')
10619 {
10620 const char *value_start;
10621
10622 exp_end = arg + strlen (arg);
10623
10624 /* Look for "parameter value" pairs at the end
10625 of the arguments string. */
10626 for (tok = exp_end - 1; tok > arg; tok--)
10627 {
10628 /* Skip whitespace at the end of the argument list. */
10629 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10630 tok--;
10631
10632 /* Find the beginning of the last token.
10633 This is the value of the parameter. */
10634 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10635 tok--;
10636 value_start = tok + 1;
10637
10638 /* Skip whitespace. */
10639 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10640 tok--;
10641
10642 end_tok = tok;
10643
10644 /* Find the beginning of the second to last token.
10645 This is the parameter itself. */
10646 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10647 tok--;
10648 tok++;
10649 toklen = end_tok - tok + 1;
10650
10651 if (toklen == 6 && startswith (tok, "thread"))
10652 {
10653 struct thread_info *thr;
10654 /* At this point we've found a "thread" token, which means
10655 the user is trying to set a watchpoint that triggers
10656 only in a specific thread. */
10657 const char *endp;
10658
10659 if (thread != -1)
10660 error(_("You can specify only one thread."));
10661
10662 /* Extract the thread ID from the next token. */
10663 thr = parse_thread_id (value_start, &endp);
10664
10665 /* Check if the user provided a valid thread ID. */
10666 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10667 invalid_thread_id_error (value_start);
10668
10669 thread = thr->global_num;
10670 }
10671 else if (toklen == 4 && startswith (tok, "mask"))
10672 {
10673 /* We've found a "mask" token, which means the user wants to
10674 create a hardware watchpoint that is going to have the mask
10675 facility. */
10676 struct value *mask_value, *mark;
10677
10678 if (use_mask)
10679 error(_("You can specify only one mask."));
10680
10681 use_mask = just_location = true;
10682
10683 mark = value_mark ();
10684 mask_value = parse_to_comma_and_eval (&value_start);
10685 mask = value_as_address (mask_value);
10686 value_free_to_mark (mark);
10687 }
10688 else
10689 /* We didn't recognize what we found. We should stop here. */
10690 break;
10691
10692 /* Truncate the string and get rid of the "parameter value" pair before
10693 the arguments string is parsed by the parse_exp_1 function. */
10694 exp_end = tok;
10695 }
10696 }
10697 else
10698 exp_end = arg;
10699
10700 /* Parse the rest of the arguments. From here on out, everything
10701 is in terms of a newly allocated string instead of the original
10702 ARG. */
10703 std::string expression (arg, exp_end - arg);
10704 exp_start = arg = expression.c_str ();
10705 innermost_block_tracker tracker;
10706 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10707 exp_end = arg;
10708 /* Remove trailing whitespace from the expression before saving it.
10709 This makes the eventual display of the expression string a bit
10710 prettier. */
10711 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10712 --exp_end;
10713
10714 /* Checking if the expression is not constant. */
10715 if (watchpoint_exp_is_const (exp.get ()))
10716 {
10717 int len;
10718
10719 len = exp_end - exp_start;
10720 while (len > 0 && isspace (exp_start[len - 1]))
10721 len--;
10722 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10723 }
10724
10725 exp_valid_block = tracker.block ();
10726 struct value *mark = value_mark ();
10727 struct value *val_as_value = nullptr;
10728 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10729 just_location);
10730
10731 if (val_as_value != NULL && just_location)
10732 {
10733 saved_bitpos = value_bitpos (val_as_value);
10734 saved_bitsize = value_bitsize (val_as_value);
10735 }
10736
10737 value_ref_ptr val;
10738 if (just_location)
10739 {
10740 int ret;
10741
10742 exp_valid_block = NULL;
10743 val = release_value (value_addr (result));
10744 value_free_to_mark (mark);
10745
10746 if (use_mask)
10747 {
10748 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10749 mask);
10750 if (ret == -1)
10751 error (_("This target does not support masked watchpoints."));
10752 else if (ret == -2)
10753 error (_("Invalid mask or memory region."));
10754 }
10755 }
10756 else if (val_as_value != NULL)
10757 val = release_value (val_as_value);
10758
10759 tok = skip_spaces (arg);
10760 end_tok = skip_to_space (tok);
10761
10762 toklen = end_tok - tok;
10763 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10764 {
10765 tok = cond_start = end_tok + 1;
10766 innermost_block_tracker if_tracker;
10767 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10768
10769 /* The watchpoint expression may not be local, but the condition
10770 may still be. E.g.: `watch global if local > 0'. */
10771 cond_exp_valid_block = if_tracker.block ();
10772
10773 cond_end = tok;
10774 }
10775 if (*tok)
10776 error (_("Junk at end of command."));
10777
10778 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10779
10780 /* Save this because create_internal_breakpoint below invalidates
10781 'wp_frame'. */
10782 frame_id watchpoint_frame = get_frame_id (wp_frame);
10783
10784 /* If the expression is "local", then set up a "watchpoint scope"
10785 breakpoint at the point where we've left the scope of the watchpoint
10786 expression. Create the scope breakpoint before the watchpoint, so
10787 that we will encounter it first in bpstat_stop_status. */
10788 if (exp_valid_block != NULL && wp_frame != NULL)
10789 {
10790 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10791
10792 if (frame_id_p (caller_frame_id))
10793 {
10794 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10795 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10796
10797 scope_breakpoint
10798 = create_internal_breakpoint (caller_arch, caller_pc,
10799 bp_watchpoint_scope,
10800 &momentary_breakpoint_ops);
10801
10802 /* create_internal_breakpoint could invalidate WP_FRAME. */
10803 wp_frame = NULL;
10804
10805 scope_breakpoint->enable_state = bp_enabled;
10806
10807 /* Automatically delete the breakpoint when it hits. */
10808 scope_breakpoint->disposition = disp_del;
10809
10810 /* Only break in the proper frame (help with recursion). */
10811 scope_breakpoint->frame_id = caller_frame_id;
10812
10813 /* Set the address at which we will stop. */
10814 scope_breakpoint->loc->gdbarch = caller_arch;
10815 scope_breakpoint->loc->requested_address = caller_pc;
10816 scope_breakpoint->loc->address
10817 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10818 scope_breakpoint->loc->requested_address,
10819 scope_breakpoint->type);
10820 }
10821 }
10822
10823 /* Now set up the breakpoint. We create all watchpoints as hardware
10824 watchpoints here even if hardware watchpoints are turned off, a call
10825 to update_watchpoint later in this function will cause the type to
10826 drop back to bp_watchpoint (software watchpoint) if required. */
10827
10828 if (accessflag == hw_read)
10829 bp_type = bp_read_watchpoint;
10830 else if (accessflag == hw_access)
10831 bp_type = bp_access_watchpoint;
10832 else
10833 bp_type = bp_hardware_watchpoint;
10834
10835 std::unique_ptr<watchpoint> w (new watchpoint ());
10836
10837 if (use_mask)
10838 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10839 &masked_watchpoint_breakpoint_ops);
10840 else
10841 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10842 &watchpoint_breakpoint_ops);
10843 w->thread = thread;
10844 w->disposition = disp_donttouch;
10845 w->pspace = current_program_space;
10846 w->exp = std::move (exp);
10847 w->exp_valid_block = exp_valid_block;
10848 w->cond_exp_valid_block = cond_exp_valid_block;
10849 if (just_location)
10850 {
10851 struct type *t = value_type (val.get ());
10852 CORE_ADDR addr = value_as_address (val.get ());
10853
10854 w->exp_string_reparse
10855 = current_language->watch_location_expression (t, addr).release ();
10856
10857 w->exp_string = xstrprintf ("-location %.*s",
10858 (int) (exp_end - exp_start), exp_start);
10859 }
10860 else
10861 w->exp_string = savestring (exp_start, exp_end - exp_start);
10862
10863 if (use_mask)
10864 {
10865 w->hw_wp_mask = mask;
10866 }
10867 else
10868 {
10869 w->val = val;
10870 w->val_bitpos = saved_bitpos;
10871 w->val_bitsize = saved_bitsize;
10872 w->val_valid = true;
10873 }
10874
10875 if (cond_start)
10876 w->cond_string = savestring (cond_start, cond_end - cond_start);
10877 else
10878 w->cond_string = 0;
10879
10880 if (frame_id_p (watchpoint_frame))
10881 {
10882 w->watchpoint_frame = watchpoint_frame;
10883 w->watchpoint_thread = inferior_ptid;
10884 }
10885 else
10886 {
10887 w->watchpoint_frame = null_frame_id;
10888 w->watchpoint_thread = null_ptid;
10889 }
10890
10891 if (scope_breakpoint != NULL)
10892 {
10893 /* The scope breakpoint is related to the watchpoint. We will
10894 need to act on them together. */
10895 w->related_breakpoint = scope_breakpoint;
10896 scope_breakpoint->related_breakpoint = w.get ();
10897 }
10898
10899 if (!just_location)
10900 value_free_to_mark (mark);
10901
10902 /* Finally update the new watchpoint. This creates the locations
10903 that should be inserted. */
10904 update_watchpoint (w.get (), 1);
10905
10906 install_breakpoint (internal, std::move (w), 1);
10907 }
10908
10909 /* Return count of debug registers needed to watch the given expression.
10910 If the watchpoint cannot be handled in hardware return zero. */
10911
10912 static int
10913 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10914 {
10915 int found_memory_cnt = 0;
10916
10917 /* Did the user specifically forbid us to use hardware watchpoints? */
10918 if (!can_use_hw_watchpoints)
10919 return 0;
10920
10921 gdb_assert (!vals.empty ());
10922 struct value *head = vals[0].get ();
10923
10924 /* Make sure that the value of the expression depends only upon
10925 memory contents, and values computed from them within GDB. If we
10926 find any register references or function calls, we can't use a
10927 hardware watchpoint.
10928
10929 The idea here is that evaluating an expression generates a series
10930 of values, one holding the value of every subexpression. (The
10931 expression a*b+c has five subexpressions: a, b, a*b, c, and
10932 a*b+c.) GDB's values hold almost enough information to establish
10933 the criteria given above --- they identify memory lvalues,
10934 register lvalues, computed values, etcetera. So we can evaluate
10935 the expression, and then scan the chain of values that leaves
10936 behind to decide whether we can detect any possible change to the
10937 expression's final value using only hardware watchpoints.
10938
10939 However, I don't think that the values returned by inferior
10940 function calls are special in any way. So this function may not
10941 notice that an expression involving an inferior function call
10942 can't be watched with hardware watchpoints. FIXME. */
10943 for (const value_ref_ptr &iter : vals)
10944 {
10945 struct value *v = iter.get ();
10946
10947 if (VALUE_LVAL (v) == lval_memory)
10948 {
10949 if (v != head && value_lazy (v))
10950 /* A lazy memory lvalue in the chain is one that GDB never
10951 needed to fetch; we either just used its address (e.g.,
10952 `a' in `a.b') or we never needed it at all (e.g., `a'
10953 in `a,b'). This doesn't apply to HEAD; if that is
10954 lazy then it was not readable, but watch it anyway. */
10955 ;
10956 else
10957 {
10958 /* Ahh, memory we actually used! Check if we can cover
10959 it with hardware watchpoints. */
10960 struct type *vtype = check_typedef (value_type (v));
10961
10962 /* We only watch structs and arrays if user asked for it
10963 explicitly, never if they just happen to appear in a
10964 middle of some value chain. */
10965 if (v == head
10966 || (vtype->code () != TYPE_CODE_STRUCT
10967 && vtype->code () != TYPE_CODE_ARRAY))
10968 {
10969 CORE_ADDR vaddr = value_address (v);
10970 int len;
10971 int num_regs;
10972
10973 len = (target_exact_watchpoints
10974 && is_scalar_type_recursive (vtype))?
10975 1 : TYPE_LENGTH (value_type (v));
10976
10977 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10978 if (!num_regs)
10979 return 0;
10980 else
10981 found_memory_cnt += num_regs;
10982 }
10983 }
10984 }
10985 else if (VALUE_LVAL (v) != not_lval
10986 && deprecated_value_modifiable (v) == 0)
10987 return 0; /* These are values from the history (e.g., $1). */
10988 else if (VALUE_LVAL (v) == lval_register)
10989 return 0; /* Cannot watch a register with a HW watchpoint. */
10990 }
10991
10992 /* The expression itself looks suitable for using a hardware
10993 watchpoint, but give the target machine a chance to reject it. */
10994 return found_memory_cnt;
10995 }
10996
10997 void
10998 watch_command_wrapper (const char *arg, int from_tty, bool internal)
10999 {
11000 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11001 }
11002
11003 /* Options for the watch, awatch, and rwatch commands. */
11004
11005 struct watch_options
11006 {
11007 /* For -location. */
11008 bool location = false;
11009 };
11010
11011 /* Definitions of options for the "watch", "awatch", and "rwatch" commands.
11012
11013 Historically GDB always accepted both '-location' and '-l' flags for
11014 these commands (both flags being synonyms). When converting to the
11015 newer option scheme only '-location' is added here. That's fine (for
11016 backward compatibility) as any non-ambiguous prefix of a flag will be
11017 accepted, so '-l', '-loc', are now all accepted.
11018
11019 What this means is that, if in the future, we add any new flag here
11020 that starts with '-l' then this will break backward compatibility, so
11021 please, don't do that! */
11022
11023 static const gdb::option::option_def watch_option_defs[] = {
11024 gdb::option::flag_option_def<watch_options> {
11025 "location",
11026 [] (watch_options *opt) { return &opt->location; },
11027 N_("\
11028 This evaluates EXPRESSION and watches the memory to which is refers.\n\
11029 -l can be used as a short form of -location."),
11030 },
11031 };
11032
11033 /* Returns the option group used by 'watch', 'awatch', and 'rwatch'
11034 commands. */
11035
11036 static gdb::option::option_def_group
11037 make_watch_options_def_group (watch_options *opts)
11038 {
11039 return {{watch_option_defs}, opts};
11040 }
11041
11042 /* A helper function that looks for the "-location" argument and then
11043 calls watch_command_1. */
11044
11045 static void
11046 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
11047 {
11048 watch_options opts;
11049 auto grp = make_watch_options_def_group (&opts);
11050 gdb::option::process_options
11051 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
11052 if (arg != nullptr && *arg == '\0')
11053 arg = nullptr;
11054
11055 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
11056 }
11057
11058 /* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
11059 static void
11060 watch_command_completer (struct cmd_list_element *ignore,
11061 completion_tracker &tracker,
11062 const char *text, const char * /*word*/)
11063 {
11064 const auto group = make_watch_options_def_group (nullptr);
11065 if (gdb::option::complete_options
11066 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
11067 return;
11068
11069 const char *word = advance_to_expression_complete_word_point (tracker, text);
11070 expression_completer (ignore, tracker, text, word);
11071 }
11072
11073 static void
11074 watch_command (const char *arg, int from_tty)
11075 {
11076 watch_maybe_just_location (arg, hw_write, from_tty);
11077 }
11078
11079 void
11080 rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
11081 {
11082 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11083 }
11084
11085 static void
11086 rwatch_command (const char *arg, int from_tty)
11087 {
11088 watch_maybe_just_location (arg, hw_read, from_tty);
11089 }
11090
11091 void
11092 awatch_command_wrapper (const char *arg, int from_tty, bool internal)
11093 {
11094 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11095 }
11096
11097 static void
11098 awatch_command (const char *arg, int from_tty)
11099 {
11100 watch_maybe_just_location (arg, hw_access, from_tty);
11101 }
11102 \f
11103
11104 /* Data for the FSM that manages the until(location)/advance commands
11105 in infcmd.c. Here because it uses the mechanisms of
11106 breakpoints. */
11107
11108 struct until_break_fsm : public thread_fsm
11109 {
11110 /* The thread that was current when the command was executed. */
11111 int thread;
11112
11113 /* The breakpoint set at the return address in the caller frame,
11114 plus breakpoints at all the destination locations. */
11115 std::vector<breakpoint_up> breakpoints;
11116
11117 until_break_fsm (struct interp *cmd_interp, int thread,
11118 std::vector<breakpoint_up> &&breakpoints)
11119 : thread_fsm (cmd_interp),
11120 thread (thread),
11121 breakpoints (std::move (breakpoints))
11122 {
11123 }
11124
11125 void clean_up (struct thread_info *thread) override;
11126 bool should_stop (struct thread_info *thread) override;
11127 enum async_reply_reason do_async_reply_reason () override;
11128 };
11129
11130 /* Implementation of the 'should_stop' FSM method for the
11131 until(location)/advance commands. */
11132
11133 bool
11134 until_break_fsm::should_stop (struct thread_info *tp)
11135 {
11136 for (const breakpoint_up &bp : breakpoints)
11137 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11138 bp.get ()) != NULL)
11139 {
11140 set_finished ();
11141 break;
11142 }
11143
11144 return true;
11145 }
11146
11147 /* Implementation of the 'clean_up' FSM method for the
11148 until(location)/advance commands. */
11149
11150 void
11151 until_break_fsm::clean_up (struct thread_info *)
11152 {
11153 /* Clean up our temporary breakpoints. */
11154 breakpoints.clear ();
11155 delete_longjmp_breakpoint (thread);
11156 }
11157
11158 /* Implementation of the 'async_reply_reason' FSM method for the
11159 until(location)/advance commands. */
11160
11161 enum async_reply_reason
11162 until_break_fsm::do_async_reply_reason ()
11163 {
11164 return EXEC_ASYNC_LOCATION_REACHED;
11165 }
11166
11167 void
11168 until_break_command (const char *arg, int from_tty, int anywhere)
11169 {
11170 struct frame_info *frame;
11171 struct gdbarch *frame_gdbarch;
11172 struct frame_id stack_frame_id;
11173 struct frame_id caller_frame_id;
11174 int thread;
11175 struct thread_info *tp;
11176
11177 clear_proceed_status (0);
11178
11179 /* Set a breakpoint where the user wants it and at return from
11180 this function. */
11181
11182 event_location_up location = string_to_event_location (&arg, current_language);
11183
11184 std::vector<symtab_and_line> sals
11185 = (last_displayed_sal_is_valid ()
11186 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11187 get_last_displayed_symtab (),
11188 get_last_displayed_line ())
11189 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11190 NULL, NULL, 0));
11191
11192 if (sals.empty ())
11193 error (_("Couldn't get information on specified line."));
11194
11195 if (*arg)
11196 error (_("Junk at end of arguments."));
11197
11198 tp = inferior_thread ();
11199 thread = tp->global_num;
11200
11201 /* Note linespec handling above invalidates the frame chain.
11202 Installing a breakpoint also invalidates the frame chain (as it
11203 may need to switch threads), so do any frame handling before
11204 that. */
11205
11206 frame = get_selected_frame (NULL);
11207 frame_gdbarch = get_frame_arch (frame);
11208 stack_frame_id = get_stack_frame_id (frame);
11209 caller_frame_id = frame_unwind_caller_id (frame);
11210
11211 /* Keep within the current frame, or in frames called by the current
11212 one. */
11213
11214 std::vector<breakpoint_up> breakpoints;
11215
11216 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11217
11218 if (frame_id_p (caller_frame_id))
11219 {
11220 struct symtab_and_line sal2;
11221 struct gdbarch *caller_gdbarch;
11222
11223 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11224 sal2.pc = frame_unwind_caller_pc (frame);
11225 caller_gdbarch = frame_unwind_caller_arch (frame);
11226
11227 breakpoint_up caller_breakpoint
11228 = set_momentary_breakpoint (caller_gdbarch, sal2,
11229 caller_frame_id, bp_until);
11230 breakpoints.emplace_back (std::move (caller_breakpoint));
11231
11232 set_longjmp_breakpoint (tp, caller_frame_id);
11233 lj_deleter.emplace (thread);
11234 }
11235
11236 /* set_momentary_breakpoint could invalidate FRAME. */
11237 frame = NULL;
11238
11239 /* If the user told us to continue until a specified location, we
11240 don't specify a frame at which we need to stop. Otherwise,
11241 specify the selected frame, because we want to stop only at the
11242 very same frame. */
11243 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
11244
11245 for (symtab_and_line &sal : sals)
11246 {
11247 resolve_sal_pc (&sal);
11248
11249 breakpoint_up location_breakpoint
11250 = set_momentary_breakpoint (frame_gdbarch, sal,
11251 stop_frame_id, bp_until);
11252 breakpoints.emplace_back (std::move (location_breakpoint));
11253 }
11254
11255 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
11256 std::move (breakpoints));
11257
11258 if (lj_deleter)
11259 lj_deleter->release ();
11260
11261 proceed (-1, GDB_SIGNAL_DEFAULT);
11262 }
11263
11264 /* This function attempts to parse an optional "if <cond>" clause
11265 from the arg string. If one is not found, it returns NULL.
11266
11267 Else, it returns a pointer to the condition string. (It does not
11268 attempt to evaluate the string against a particular block.) And,
11269 it updates arg to point to the first character following the parsed
11270 if clause in the arg string. */
11271
11272 const char *
11273 ep_parse_optional_if_clause (const char **arg)
11274 {
11275 const char *cond_string;
11276
11277 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11278 return NULL;
11279
11280 /* Skip the "if" keyword. */
11281 (*arg) += 2;
11282
11283 /* Skip any extra leading whitespace, and record the start of the
11284 condition string. */
11285 *arg = skip_spaces (*arg);
11286 cond_string = *arg;
11287
11288 /* Assume that the condition occupies the remainder of the arg
11289 string. */
11290 (*arg) += strlen (cond_string);
11291
11292 return cond_string;
11293 }
11294
11295 /* Commands to deal with catching events, such as signals, exceptions,
11296 process start/exit, etc. */
11297
11298 typedef enum
11299 {
11300 catch_fork_temporary, catch_vfork_temporary,
11301 catch_fork_permanent, catch_vfork_permanent
11302 }
11303 catch_fork_kind;
11304
11305 static void
11306 catch_fork_command_1 (const char *arg, int from_tty,
11307 struct cmd_list_element *command)
11308 {
11309 struct gdbarch *gdbarch = get_current_arch ();
11310 const char *cond_string = NULL;
11311 catch_fork_kind fork_kind;
11312
11313 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11314 bool temp = (fork_kind == catch_fork_temporary
11315 || fork_kind == catch_vfork_temporary);
11316
11317 if (!arg)
11318 arg = "";
11319 arg = skip_spaces (arg);
11320
11321 /* The allowed syntax is:
11322 catch [v]fork
11323 catch [v]fork if <cond>
11324
11325 First, check if there's an if clause. */
11326 cond_string = ep_parse_optional_if_clause (&arg);
11327
11328 if ((*arg != '\0') && !isspace (*arg))
11329 error (_("Junk at end of arguments."));
11330
11331 /* If this target supports it, create a fork or vfork catchpoint
11332 and enable reporting of such events. */
11333 switch (fork_kind)
11334 {
11335 case catch_fork_temporary:
11336 case catch_fork_permanent:
11337 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
11338 &catch_fork_breakpoint_ops);
11339 break;
11340 case catch_vfork_temporary:
11341 case catch_vfork_permanent:
11342 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
11343 &catch_vfork_breakpoint_ops);
11344 break;
11345 default:
11346 error (_("unsupported or unknown fork kind; cannot catch it"));
11347 break;
11348 }
11349 }
11350
11351 static void
11352 catch_exec_command_1 (const char *arg, int from_tty,
11353 struct cmd_list_element *command)
11354 {
11355 struct gdbarch *gdbarch = get_current_arch ();
11356 const char *cond_string = NULL;
11357 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
11358
11359 if (!arg)
11360 arg = "";
11361 arg = skip_spaces (arg);
11362
11363 /* The allowed syntax is:
11364 catch exec
11365 catch exec if <cond>
11366
11367 First, check if there's an if clause. */
11368 cond_string = ep_parse_optional_if_clause (&arg);
11369
11370 if ((*arg != '\0') && !isspace (*arg))
11371 error (_("Junk at end of arguments."));
11372
11373 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11374 init_catchpoint (c.get (), gdbarch, temp, cond_string,
11375 &catch_exec_breakpoint_ops);
11376 c->exec_pathname = NULL;
11377
11378 install_breakpoint (0, std::move (c), 1);
11379 }
11380
11381 void
11382 init_ada_exception_breakpoint (struct breakpoint *b,
11383 struct gdbarch *gdbarch,
11384 struct symtab_and_line sal,
11385 const char *addr_string,
11386 const struct breakpoint_ops *ops,
11387 int tempflag,
11388 int enabled,
11389 int from_tty)
11390 {
11391 if (from_tty)
11392 {
11393 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11394 if (!loc_gdbarch)
11395 loc_gdbarch = gdbarch;
11396
11397 describe_other_breakpoints (loc_gdbarch,
11398 sal.pspace, sal.pc, sal.section, -1);
11399 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11400 version for exception catchpoints, because two catchpoints
11401 used for different exception names will use the same address.
11402 In this case, a "breakpoint ... also set at..." warning is
11403 unproductive. Besides, the warning phrasing is also a bit
11404 inappropriate, we should use the word catchpoint, and tell
11405 the user what type of catchpoint it is. The above is good
11406 enough for now, though. */
11407 }
11408
11409 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
11410
11411 b->enable_state = enabled ? bp_enabled : bp_disabled;
11412 b->disposition = tempflag ? disp_del : disp_donttouch;
11413 b->location = string_to_event_location (&addr_string,
11414 language_def (language_ada));
11415 b->language = language_ada;
11416 }
11417
11418 \f
11419
11420 /* Compare two breakpoints and return a strcmp-like result. */
11421
11422 static int
11423 compare_breakpoints (const breakpoint *a, const breakpoint *b)
11424 {
11425 uintptr_t ua = (uintptr_t) a;
11426 uintptr_t ub = (uintptr_t) b;
11427
11428 if (a->number < b->number)
11429 return -1;
11430 else if (a->number > b->number)
11431 return 1;
11432
11433 /* Now sort by address, in case we see, e..g, two breakpoints with
11434 the number 0. */
11435 if (ua < ub)
11436 return -1;
11437 return ua > ub ? 1 : 0;
11438 }
11439
11440 /* Delete breakpoints by address or line. */
11441
11442 static void
11443 clear_command (const char *arg, int from_tty)
11444 {
11445 int default_match;
11446
11447 std::vector<symtab_and_line> decoded_sals;
11448 symtab_and_line last_sal;
11449 gdb::array_view<symtab_and_line> sals;
11450 if (arg)
11451 {
11452 decoded_sals
11453 = decode_line_with_current_source (arg,
11454 (DECODE_LINE_FUNFIRSTLINE
11455 | DECODE_LINE_LIST_MODE));
11456 default_match = 0;
11457 sals = decoded_sals;
11458 }
11459 else
11460 {
11461 /* Set sal's line, symtab, pc, and pspace to the values
11462 corresponding to the last call to print_frame_info. If the
11463 codepoint is not valid, this will set all the fields to 0. */
11464 last_sal = get_last_displayed_sal ();
11465 if (last_sal.symtab == 0)
11466 error (_("No source file specified."));
11467
11468 default_match = 1;
11469 sals = last_sal;
11470 }
11471
11472 /* We don't call resolve_sal_pc here. That's not as bad as it
11473 seems, because all existing breakpoints typically have both
11474 file/line and pc set. So, if clear is given file/line, we can
11475 match this to existing breakpoint without obtaining pc at all.
11476
11477 We only support clearing given the address explicitly
11478 present in breakpoint table. Say, we've set breakpoint
11479 at file:line. There were several PC values for that file:line,
11480 due to optimization, all in one block.
11481
11482 We've picked one PC value. If "clear" is issued with another
11483 PC corresponding to the same file:line, the breakpoint won't
11484 be cleared. We probably can still clear the breakpoint, but
11485 since the other PC value is never presented to user, user
11486 can only find it by guessing, and it does not seem important
11487 to support that. */
11488
11489 /* For each line spec given, delete bps which correspond to it. Do
11490 it in two passes, solely to preserve the current behavior that
11491 from_tty is forced true if we delete more than one
11492 breakpoint. */
11493
11494 std::vector<struct breakpoint *> found;
11495 for (const auto &sal : sals)
11496 {
11497 const char *sal_fullname;
11498
11499 /* If exact pc given, clear bpts at that pc.
11500 If line given (pc == 0), clear all bpts on specified line.
11501 If defaulting, clear all bpts on default line
11502 or at default pc.
11503
11504 defaulting sal.pc != 0 tests to do
11505
11506 0 1 pc
11507 1 1 pc _and_ line
11508 0 0 line
11509 1 0 <can't happen> */
11510
11511 sal_fullname = (sal.symtab == NULL
11512 ? NULL : symtab_to_fullname (sal.symtab));
11513
11514 /* Find all matching breakpoints and add them to 'found'. */
11515 for (breakpoint *b : all_breakpoints ())
11516 {
11517 int match = 0;
11518 /* Are we going to delete b? */
11519 if (b->type != bp_none && !is_watchpoint (b))
11520 {
11521 for (bp_location *loc : b->locations ())
11522 {
11523 /* If the user specified file:line, don't allow a PC
11524 match. This matches historical gdb behavior. */
11525 int pc_match = (!sal.explicit_line
11526 && sal.pc
11527 && (loc->pspace == sal.pspace)
11528 && (loc->address == sal.pc)
11529 && (!section_is_overlay (loc->section)
11530 || loc->section == sal.section));
11531 int line_match = 0;
11532
11533 if ((default_match || sal.explicit_line)
11534 && loc->symtab != NULL
11535 && sal_fullname != NULL
11536 && sal.pspace == loc->pspace
11537 && loc->line_number == sal.line
11538 && filename_cmp (symtab_to_fullname (loc->symtab),
11539 sal_fullname) == 0)
11540 line_match = 1;
11541
11542 if (pc_match || line_match)
11543 {
11544 match = 1;
11545 break;
11546 }
11547 }
11548 }
11549
11550 if (match)
11551 found.push_back (b);
11552 }
11553 }
11554
11555 /* Now go thru the 'found' chain and delete them. */
11556 if (found.empty ())
11557 {
11558 if (arg)
11559 error (_("No breakpoint at %s."), arg);
11560 else
11561 error (_("No breakpoint at this line."));
11562 }
11563
11564 /* Remove duplicates from the vec. */
11565 std::sort (found.begin (), found.end (),
11566 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11567 {
11568 return compare_breakpoints (bp_a, bp_b) < 0;
11569 });
11570 found.erase (std::unique (found.begin (), found.end (),
11571 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11572 {
11573 return compare_breakpoints (bp_a, bp_b) == 0;
11574 }),
11575 found.end ());
11576
11577 if (found.size () > 1)
11578 from_tty = 1; /* Always report if deleted more than one. */
11579 if (from_tty)
11580 {
11581 if (found.size () == 1)
11582 printf_unfiltered (_("Deleted breakpoint "));
11583 else
11584 printf_unfiltered (_("Deleted breakpoints "));
11585 }
11586
11587 for (breakpoint *iter : found)
11588 {
11589 if (from_tty)
11590 printf_unfiltered ("%d ", iter->number);
11591 delete_breakpoint (iter);
11592 }
11593 if (from_tty)
11594 putchar_unfiltered ('\n');
11595 }
11596 \f
11597 /* Delete breakpoint in BS if they are `delete' breakpoints and
11598 all breakpoints that are marked for deletion, whether hit or not.
11599 This is called after any breakpoint is hit, or after errors. */
11600
11601 void
11602 breakpoint_auto_delete (bpstat bs)
11603 {
11604 for (; bs; bs = bs->next)
11605 if (bs->breakpoint_at
11606 && bs->breakpoint_at->disposition == disp_del
11607 && bs->stop)
11608 delete_breakpoint (bs->breakpoint_at);
11609
11610 for (breakpoint *b : all_breakpoints_safe ())
11611 if (b->disposition == disp_del_at_next_stop)
11612 delete_breakpoint (b);
11613 }
11614
11615 /* A comparison function for bp_location AP and BP being interfaced to
11616 std::sort. Sort elements primarily by their ADDRESS (no matter what
11617 bl_address_is_meaningful says), secondarily by ordering first
11618 permanent elements and terciarily just ensuring the array is sorted
11619 stable way despite std::sort being an unstable algorithm. */
11620
11621 static int
11622 bp_location_is_less_than (const bp_location *a, const bp_location *b)
11623 {
11624 if (a->address != b->address)
11625 return a->address < b->address;
11626
11627 /* Sort locations at the same address by their pspace number, keeping
11628 locations of the same inferior (in a multi-inferior environment)
11629 grouped. */
11630
11631 if (a->pspace->num != b->pspace->num)
11632 return a->pspace->num < b->pspace->num;
11633
11634 /* Sort permanent breakpoints first. */
11635 if (a->permanent != b->permanent)
11636 return a->permanent > b->permanent;
11637
11638 /* Sort by type in order to make duplicate determination easier.
11639 See update_global_location_list. This is kept in sync with
11640 breakpoint_locations_match. */
11641 if (a->loc_type < b->loc_type)
11642 return true;
11643
11644 /* Likewise, for range-breakpoints, sort by length. */
11645 if (a->loc_type == bp_loc_hardware_breakpoint
11646 && b->loc_type == bp_loc_hardware_breakpoint
11647 && a->length < b->length)
11648 return true;
11649
11650 /* Make the internal GDB representation stable across GDB runs
11651 where A and B memory inside GDB can differ. Breakpoint locations of
11652 the same type at the same address can be sorted in arbitrary order. */
11653
11654 if (a->owner->number != b->owner->number)
11655 return a->owner->number < b->owner->number;
11656
11657 return a < b;
11658 }
11659
11660 /* Set bp_locations_placed_address_before_address_max and
11661 bp_locations_shadow_len_after_address_max according to the current
11662 content of the bp_locations array. */
11663
11664 static void
11665 bp_locations_target_extensions_update (void)
11666 {
11667 struct bp_location *bl, **blp_tmp;
11668
11669 bp_locations_placed_address_before_address_max = 0;
11670 bp_locations_shadow_len_after_address_max = 0;
11671
11672 ALL_BP_LOCATIONS (bl, blp_tmp)
11673 {
11674 CORE_ADDR start, end, addr;
11675
11676 if (!bp_location_has_shadow (bl))
11677 continue;
11678
11679 start = bl->target_info.placed_address;
11680 end = start + bl->target_info.shadow_len;
11681
11682 gdb_assert (bl->address >= start);
11683 addr = bl->address - start;
11684 if (addr > bp_locations_placed_address_before_address_max)
11685 bp_locations_placed_address_before_address_max = addr;
11686
11687 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11688
11689 gdb_assert (bl->address < end);
11690 addr = end - bl->address;
11691 if (addr > bp_locations_shadow_len_after_address_max)
11692 bp_locations_shadow_len_after_address_max = addr;
11693 }
11694 }
11695
11696 /* Download tracepoint locations if they haven't been. */
11697
11698 static void
11699 download_tracepoint_locations (void)
11700 {
11701 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11702
11703 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11704
11705 for (breakpoint *b : all_tracepoints ())
11706 {
11707 struct tracepoint *t;
11708 int bp_location_downloaded = 0;
11709
11710 if ((b->type == bp_fast_tracepoint
11711 ? !may_insert_fast_tracepoints
11712 : !may_insert_tracepoints))
11713 continue;
11714
11715 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11716 {
11717 if (target_can_download_tracepoint ())
11718 can_download_tracepoint = TRIBOOL_TRUE;
11719 else
11720 can_download_tracepoint = TRIBOOL_FALSE;
11721 }
11722
11723 if (can_download_tracepoint == TRIBOOL_FALSE)
11724 break;
11725
11726 for (bp_location *bl : b->locations ())
11727 {
11728 /* In tracepoint, locations are _never_ duplicated, so
11729 should_be_inserted is equivalent to
11730 unduplicated_should_be_inserted. */
11731 if (!should_be_inserted (bl) || bl->inserted)
11732 continue;
11733
11734 switch_to_program_space_and_thread (bl->pspace);
11735
11736 target_download_tracepoint (bl);
11737
11738 bl->inserted = 1;
11739 bp_location_downloaded = 1;
11740 }
11741 t = (struct tracepoint *) b;
11742 t->number_on_target = b->number;
11743 if (bp_location_downloaded)
11744 gdb::observers::breakpoint_modified.notify (b);
11745 }
11746 }
11747
11748 /* Swap the insertion/duplication state between two locations. */
11749
11750 static void
11751 swap_insertion (struct bp_location *left, struct bp_location *right)
11752 {
11753 const int left_inserted = left->inserted;
11754 const int left_duplicate = left->duplicate;
11755 const int left_needs_update = left->needs_update;
11756 const struct bp_target_info left_target_info = left->target_info;
11757
11758 /* Locations of tracepoints can never be duplicated. */
11759 if (is_tracepoint (left->owner))
11760 gdb_assert (!left->duplicate);
11761 if (is_tracepoint (right->owner))
11762 gdb_assert (!right->duplicate);
11763
11764 left->inserted = right->inserted;
11765 left->duplicate = right->duplicate;
11766 left->needs_update = right->needs_update;
11767 left->target_info = right->target_info;
11768 right->inserted = left_inserted;
11769 right->duplicate = left_duplicate;
11770 right->needs_update = left_needs_update;
11771 right->target_info = left_target_info;
11772 }
11773
11774 /* Force the re-insertion of the locations at ADDRESS. This is called
11775 once a new/deleted/modified duplicate location is found and we are evaluating
11776 conditions on the target's side. Such conditions need to be updated on
11777 the target. */
11778
11779 static void
11780 force_breakpoint_reinsertion (struct bp_location *bl)
11781 {
11782 struct bp_location **locp = NULL, **loc2p;
11783 struct bp_location *loc;
11784 CORE_ADDR address = 0;
11785 int pspace_num;
11786
11787 address = bl->address;
11788 pspace_num = bl->pspace->num;
11789
11790 /* This is only meaningful if the target is
11791 evaluating conditions and if the user has
11792 opted for condition evaluation on the target's
11793 side. */
11794 if (gdb_evaluates_breakpoint_condition_p ()
11795 || !target_supports_evaluation_of_breakpoint_conditions ())
11796 return;
11797
11798 /* Flag all breakpoint locations with this address and
11799 the same program space as the location
11800 as "its condition has changed". We need to
11801 update the conditions on the target's side. */
11802 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11803 {
11804 loc = *loc2p;
11805
11806 if (!is_breakpoint (loc->owner)
11807 || pspace_num != loc->pspace->num)
11808 continue;
11809
11810 /* Flag the location appropriately. We use a different state to
11811 let everyone know that we already updated the set of locations
11812 with addr bl->address and program space bl->pspace. This is so
11813 we don't have to keep calling these functions just to mark locations
11814 that have already been marked. */
11815 loc->condition_changed = condition_updated;
11816
11817 /* Free the agent expression bytecode as well. We will compute
11818 it later on. */
11819 loc->cond_bytecode.reset ();
11820 }
11821 }
11822
11823 /* Called whether new breakpoints are created, or existing breakpoints
11824 deleted, to update the global location list and recompute which
11825 locations are duplicate of which.
11826
11827 The INSERT_MODE flag determines whether locations may not, may, or
11828 shall be inserted now. See 'enum ugll_insert_mode' for more
11829 info. */
11830
11831 static void
11832 update_global_location_list (enum ugll_insert_mode insert_mode)
11833 {
11834 struct bp_location **locp;
11835 /* Last breakpoint location address that was marked for update. */
11836 CORE_ADDR last_addr = 0;
11837 /* Last breakpoint location program space that was marked for update. */
11838 int last_pspace_num = -1;
11839
11840 /* Used in the duplicates detection below. When iterating over all
11841 bp_locations, points to the first bp_location of a given address.
11842 Breakpoints and watchpoints of different types are never
11843 duplicates of each other. Keep one pointer for each type of
11844 breakpoint/watchpoint, so we only need to loop over all locations
11845 once. */
11846 struct bp_location *bp_loc_first; /* breakpoint */
11847 struct bp_location *wp_loc_first; /* hardware watchpoint */
11848 struct bp_location *awp_loc_first; /* access watchpoint */
11849 struct bp_location *rwp_loc_first; /* read watchpoint */
11850
11851 /* Saved former bp_locations array which we compare against the newly
11852 built bp_locations from the current state of ALL_BREAKPOINTS. */
11853 struct bp_location **old_locp;
11854 unsigned old_locations_count;
11855 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
11856
11857 old_locations_count = bp_locations_count;
11858 bp_locations = NULL;
11859 bp_locations_count = 0;
11860
11861 for (breakpoint *b : all_breakpoints ())
11862 for (bp_location *loc ATTRIBUTE_UNUSED : b->locations ())
11863 bp_locations_count++;
11864
11865 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11866 locp = bp_locations;
11867 for (breakpoint *b : all_breakpoints ())
11868 for (bp_location *loc : b->locations ())
11869 *locp++ = loc;
11870
11871 /* See if we need to "upgrade" a software breakpoint to a hardware
11872 breakpoint. Do this before deciding whether locations are
11873 duplicates. Also do this before sorting because sorting order
11874 depends on location type. */
11875 for (locp = bp_locations;
11876 locp < bp_locations + bp_locations_count;
11877 locp++)
11878 {
11879 bp_location *loc = *locp;
11880 if (!loc->inserted && should_be_inserted (loc))
11881 handle_automatic_hardware_breakpoints (loc);
11882 }
11883
11884 std::sort (bp_locations, bp_locations + bp_locations_count,
11885 bp_location_is_less_than);
11886
11887 bp_locations_target_extensions_update ();
11888
11889 /* Identify bp_location instances that are no longer present in the
11890 new list, and therefore should be freed. Note that it's not
11891 necessary that those locations should be removed from inferior --
11892 if there's another location at the same address (previously
11893 marked as duplicate), we don't need to remove/insert the
11894 location.
11895
11896 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11897 and former bp_location array state respectively. */
11898
11899 locp = bp_locations;
11900 for (old_locp = old_locations.get ();
11901 old_locp < old_locations.get () + old_locations_count;
11902 old_locp++)
11903 {
11904 struct bp_location *old_loc = *old_locp;
11905 struct bp_location **loc2p;
11906
11907 /* Tells if 'old_loc' is found among the new locations. If
11908 not, we have to free it. */
11909 int found_object = 0;
11910 /* Tells if the location should remain inserted in the target. */
11911 int keep_in_target = 0;
11912 int removed = 0;
11913
11914 /* Skip LOCP entries which will definitely never be needed.
11915 Stop either at or being the one matching OLD_LOC. */
11916 while (locp < bp_locations + bp_locations_count
11917 && (*locp)->address < old_loc->address)
11918 locp++;
11919
11920 for (loc2p = locp;
11921 (loc2p < bp_locations + bp_locations_count
11922 && (*loc2p)->address == old_loc->address);
11923 loc2p++)
11924 {
11925 /* Check if this is a new/duplicated location or a duplicated
11926 location that had its condition modified. If so, we want to send
11927 its condition to the target if evaluation of conditions is taking
11928 place there. */
11929 if ((*loc2p)->condition_changed == condition_modified
11930 && (last_addr != old_loc->address
11931 || last_pspace_num != old_loc->pspace->num))
11932 {
11933 force_breakpoint_reinsertion (*loc2p);
11934 last_pspace_num = old_loc->pspace->num;
11935 }
11936
11937 if (*loc2p == old_loc)
11938 found_object = 1;
11939 }
11940
11941 /* We have already handled this address, update it so that we don't
11942 have to go through updates again. */
11943 last_addr = old_loc->address;
11944
11945 /* Target-side condition evaluation: Handle deleted locations. */
11946 if (!found_object)
11947 force_breakpoint_reinsertion (old_loc);
11948
11949 /* If this location is no longer present, and inserted, look if
11950 there's maybe a new location at the same address. If so,
11951 mark that one inserted, and don't remove this one. This is
11952 needed so that we don't have a time window where a breakpoint
11953 at certain location is not inserted. */
11954
11955 if (old_loc->inserted)
11956 {
11957 /* If the location is inserted now, we might have to remove
11958 it. */
11959
11960 if (found_object && should_be_inserted (old_loc))
11961 {
11962 /* The location is still present in the location list,
11963 and still should be inserted. Don't do anything. */
11964 keep_in_target = 1;
11965 }
11966 else
11967 {
11968 /* This location still exists, but it won't be kept in the
11969 target since it may have been disabled. We proceed to
11970 remove its target-side condition. */
11971
11972 /* The location is either no longer present, or got
11973 disabled. See if there's another location at the
11974 same address, in which case we don't need to remove
11975 this one from the target. */
11976
11977 /* OLD_LOC comes from existing struct breakpoint. */
11978 if (bl_address_is_meaningful (old_loc))
11979 {
11980 for (loc2p = locp;
11981 (loc2p < bp_locations + bp_locations_count
11982 && (*loc2p)->address == old_loc->address);
11983 loc2p++)
11984 {
11985 struct bp_location *loc2 = *loc2p;
11986
11987 if (loc2 == old_loc)
11988 continue;
11989
11990 if (breakpoint_locations_match (loc2, old_loc))
11991 {
11992 /* Read watchpoint locations are switched to
11993 access watchpoints, if the former are not
11994 supported, but the latter are. */
11995 if (is_hardware_watchpoint (old_loc->owner))
11996 {
11997 gdb_assert (is_hardware_watchpoint (loc2->owner));
11998 loc2->watchpoint_type = old_loc->watchpoint_type;
11999 }
12000
12001 /* loc2 is a duplicated location. We need to check
12002 if it should be inserted in case it will be
12003 unduplicated. */
12004 if (unduplicated_should_be_inserted (loc2))
12005 {
12006 swap_insertion (old_loc, loc2);
12007 keep_in_target = 1;
12008 break;
12009 }
12010 }
12011 }
12012 }
12013 }
12014
12015 if (!keep_in_target)
12016 {
12017 if (remove_breakpoint (old_loc))
12018 {
12019 /* This is just about all we can do. We could keep
12020 this location on the global list, and try to
12021 remove it next time, but there's no particular
12022 reason why we will succeed next time.
12023
12024 Note that at this point, old_loc->owner is still
12025 valid, as delete_breakpoint frees the breakpoint
12026 only after calling us. */
12027 printf_filtered (_("warning: Error removing "
12028 "breakpoint %d\n"),
12029 old_loc->owner->number);
12030 }
12031 removed = 1;
12032 }
12033 }
12034
12035 if (!found_object)
12036 {
12037 if (removed && target_is_non_stop_p ()
12038 && need_moribund_for_location_type (old_loc))
12039 {
12040 /* This location was removed from the target. In
12041 non-stop mode, a race condition is possible where
12042 we've removed a breakpoint, but stop events for that
12043 breakpoint are already queued and will arrive later.
12044 We apply an heuristic to be able to distinguish such
12045 SIGTRAPs from other random SIGTRAPs: we keep this
12046 breakpoint location for a bit, and will retire it
12047 after we see some number of events. The theory here
12048 is that reporting of events should, "on the average",
12049 be fair, so after a while we'll see events from all
12050 threads that have anything of interest, and no longer
12051 need to keep this breakpoint location around. We
12052 don't hold locations forever so to reduce chances of
12053 mistaking a non-breakpoint SIGTRAP for a breakpoint
12054 SIGTRAP.
12055
12056 The heuristic failing can be disastrous on
12057 decr_pc_after_break targets.
12058
12059 On decr_pc_after_break targets, like e.g., x86-linux,
12060 if we fail to recognize a late breakpoint SIGTRAP,
12061 because events_till_retirement has reached 0 too
12062 soon, we'll fail to do the PC adjustment, and report
12063 a random SIGTRAP to the user. When the user resumes
12064 the inferior, it will most likely immediately crash
12065 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12066 corrupted, because of being resumed e.g., in the
12067 middle of a multi-byte instruction, or skipped a
12068 one-byte instruction. This was actually seen happen
12069 on native x86-linux, and should be less rare on
12070 targets that do not support new thread events, like
12071 remote, due to the heuristic depending on
12072 thread_count.
12073
12074 Mistaking a random SIGTRAP for a breakpoint trap
12075 causes similar symptoms (PC adjustment applied when
12076 it shouldn't), but then again, playing with SIGTRAPs
12077 behind the debugger's back is asking for trouble.
12078
12079 Since hardware watchpoint traps are always
12080 distinguishable from other traps, so we don't need to
12081 apply keep hardware watchpoint moribund locations
12082 around. We simply always ignore hardware watchpoint
12083 traps we can no longer explain. */
12084
12085 process_stratum_target *proc_target = nullptr;
12086 for (inferior *inf : all_inferiors ())
12087 if (inf->pspace == old_loc->pspace)
12088 {
12089 proc_target = inf->process_target ();
12090 break;
12091 }
12092 if (proc_target != nullptr)
12093 old_loc->events_till_retirement
12094 = 3 * (thread_count (proc_target) + 1);
12095 else
12096 old_loc->events_till_retirement = 1;
12097 old_loc->owner = NULL;
12098
12099 moribund_locations.push_back (old_loc);
12100 }
12101 else
12102 {
12103 old_loc->owner = NULL;
12104 decref_bp_location (&old_loc);
12105 }
12106 }
12107 }
12108
12109 /* Rescan breakpoints at the same address and section, marking the
12110 first one as "first" and any others as "duplicates". This is so
12111 that the bpt instruction is only inserted once. If we have a
12112 permanent breakpoint at the same place as BPT, make that one the
12113 official one, and the rest as duplicates. Permanent breakpoints
12114 are sorted first for the same address.
12115
12116 Do the same for hardware watchpoints, but also considering the
12117 watchpoint's type (regular/access/read) and length. */
12118
12119 bp_loc_first = NULL;
12120 wp_loc_first = NULL;
12121 awp_loc_first = NULL;
12122 rwp_loc_first = NULL;
12123
12124 bp_location *loc;
12125 ALL_BP_LOCATIONS (loc, locp)
12126 {
12127 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12128 non-NULL. */
12129 struct bp_location **loc_first_p;
12130 breakpoint *b = loc->owner;
12131
12132 if (!unduplicated_should_be_inserted (loc)
12133 || !bl_address_is_meaningful (loc)
12134 /* Don't detect duplicate for tracepoint locations because they are
12135 never duplicated. See the comments in field `duplicate' of
12136 `struct bp_location'. */
12137 || is_tracepoint (b))
12138 {
12139 /* Clear the condition modification flag. */
12140 loc->condition_changed = condition_unchanged;
12141 continue;
12142 }
12143
12144 if (b->type == bp_hardware_watchpoint)
12145 loc_first_p = &wp_loc_first;
12146 else if (b->type == bp_read_watchpoint)
12147 loc_first_p = &rwp_loc_first;
12148 else if (b->type == bp_access_watchpoint)
12149 loc_first_p = &awp_loc_first;
12150 else
12151 loc_first_p = &bp_loc_first;
12152
12153 if (*loc_first_p == NULL
12154 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12155 || !breakpoint_locations_match (loc, *loc_first_p))
12156 {
12157 *loc_first_p = loc;
12158 loc->duplicate = 0;
12159
12160 if (is_breakpoint (loc->owner) && loc->condition_changed)
12161 {
12162 loc->needs_update = 1;
12163 /* Clear the condition modification flag. */
12164 loc->condition_changed = condition_unchanged;
12165 }
12166 continue;
12167 }
12168
12169
12170 /* This and the above ensure the invariant that the first location
12171 is not duplicated, and is the inserted one.
12172 All following are marked as duplicated, and are not inserted. */
12173 if (loc->inserted)
12174 swap_insertion (loc, *loc_first_p);
12175 loc->duplicate = 1;
12176
12177 /* Clear the condition modification flag. */
12178 loc->condition_changed = condition_unchanged;
12179 }
12180
12181 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12182 {
12183 if (insert_mode != UGLL_DONT_INSERT)
12184 insert_breakpoint_locations ();
12185 else
12186 {
12187 /* Even though the caller told us to not insert new
12188 locations, we may still need to update conditions on the
12189 target's side of breakpoints that were already inserted
12190 if the target is evaluating breakpoint conditions. We
12191 only update conditions for locations that are marked
12192 "needs_update". */
12193 update_inserted_breakpoint_locations ();
12194 }
12195 }
12196
12197 if (insert_mode != UGLL_DONT_INSERT)
12198 download_tracepoint_locations ();
12199 }
12200
12201 void
12202 breakpoint_retire_moribund (void)
12203 {
12204 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12205 {
12206 struct bp_location *loc = moribund_locations[ix];
12207 if (--(loc->events_till_retirement) == 0)
12208 {
12209 decref_bp_location (&loc);
12210 unordered_remove (moribund_locations, ix);
12211 --ix;
12212 }
12213 }
12214 }
12215
12216 static void
12217 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12218 {
12219
12220 try
12221 {
12222 update_global_location_list (insert_mode);
12223 }
12224 catch (const gdb_exception_error &e)
12225 {
12226 }
12227 }
12228
12229 /* Clear BKP from a BPS. */
12230
12231 static void
12232 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12233 {
12234 bpstat bs;
12235
12236 for (bs = bps; bs; bs = bs->next)
12237 if (bs->breakpoint_at == bpt)
12238 {
12239 bs->breakpoint_at = NULL;
12240 bs->old_val = NULL;
12241 /* bs->commands will be freed later. */
12242 }
12243 }
12244
12245 /* Callback for iterate_over_threads. */
12246 static int
12247 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12248 {
12249 struct breakpoint *bpt = (struct breakpoint *) data;
12250
12251 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12252 return 0;
12253 }
12254
12255 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12256 callbacks. */
12257
12258 static void
12259 say_where (struct breakpoint *b)
12260 {
12261 struct value_print_options opts;
12262
12263 get_user_print_options (&opts);
12264
12265 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12266 single string. */
12267 if (b->loc == NULL)
12268 {
12269 /* For pending locations, the output differs slightly based
12270 on b->extra_string. If this is non-NULL, it contains either
12271 a condition or dprintf arguments. */
12272 if (b->extra_string == NULL)
12273 {
12274 printf_filtered (_(" (%s) pending."),
12275 event_location_to_string (b->location.get ()));
12276 }
12277 else if (b->type == bp_dprintf)
12278 {
12279 printf_filtered (_(" (%s,%s) pending."),
12280 event_location_to_string (b->location.get ()),
12281 b->extra_string);
12282 }
12283 else
12284 {
12285 printf_filtered (_(" (%s %s) pending."),
12286 event_location_to_string (b->location.get ()),
12287 b->extra_string);
12288 }
12289 }
12290 else
12291 {
12292 if (opts.addressprint || b->loc->symtab == NULL)
12293 printf_filtered (" at %ps",
12294 styled_string (address_style.style (),
12295 paddress (b->loc->gdbarch,
12296 b->loc->address)));
12297 if (b->loc->symtab != NULL)
12298 {
12299 /* If there is a single location, we can print the location
12300 more nicely. */
12301 if (b->loc->next == NULL)
12302 {
12303 const char *filename
12304 = symtab_to_filename_for_display (b->loc->symtab);
12305 printf_filtered (": file %ps, line %d.",
12306 styled_string (file_name_style.style (),
12307 filename),
12308 b->loc->line_number);
12309 }
12310 else
12311 /* This is not ideal, but each location may have a
12312 different file name, and this at least reflects the
12313 real situation somewhat. */
12314 printf_filtered (": %s.",
12315 event_location_to_string (b->location.get ()));
12316 }
12317
12318 if (b->loc->next)
12319 {
12320 struct bp_location *loc = b->loc;
12321 int n = 0;
12322 for (; loc; loc = loc->next)
12323 ++n;
12324 printf_filtered (" (%d locations)", n);
12325 }
12326 }
12327 }
12328
12329 bp_location::~bp_location ()
12330 {
12331 xfree (function_name);
12332 }
12333
12334 /* Destructor for the breakpoint base class. */
12335
12336 breakpoint::~breakpoint ()
12337 {
12338 xfree (this->cond_string);
12339 xfree (this->extra_string);
12340 }
12341
12342 /* See breakpoint.h. */
12343
12344 bp_locations_range breakpoint::locations ()
12345 {
12346 return bp_locations_range (this->loc);
12347 }
12348
12349 static struct bp_location *
12350 base_breakpoint_allocate_location (struct breakpoint *self)
12351 {
12352 return new bp_location (self);
12353 }
12354
12355 static void
12356 base_breakpoint_re_set (struct breakpoint *b)
12357 {
12358 /* Nothing to re-set. */
12359 }
12360
12361 #define internal_error_pure_virtual_called() \
12362 gdb_assert_not_reached ("pure virtual function called")
12363
12364 static int
12365 base_breakpoint_insert_location (struct bp_location *bl)
12366 {
12367 internal_error_pure_virtual_called ();
12368 }
12369
12370 static int
12371 base_breakpoint_remove_location (struct bp_location *bl,
12372 enum remove_bp_reason reason)
12373 {
12374 internal_error_pure_virtual_called ();
12375 }
12376
12377 static int
12378 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12379 const address_space *aspace,
12380 CORE_ADDR bp_addr,
12381 const struct target_waitstatus *ws)
12382 {
12383 internal_error_pure_virtual_called ();
12384 }
12385
12386 static void
12387 base_breakpoint_check_status (bpstat bs)
12388 {
12389 /* Always stop. */
12390 }
12391
12392 /* A "works_in_software_mode" breakpoint_ops method that just internal
12393 errors. */
12394
12395 static int
12396 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12397 {
12398 internal_error_pure_virtual_called ();
12399 }
12400
12401 /* A "resources_needed" breakpoint_ops method that just internal
12402 errors. */
12403
12404 static int
12405 base_breakpoint_resources_needed (const struct bp_location *bl)
12406 {
12407 internal_error_pure_virtual_called ();
12408 }
12409
12410 static enum print_stop_action
12411 base_breakpoint_print_it (bpstat bs)
12412 {
12413 internal_error_pure_virtual_called ();
12414 }
12415
12416 static void
12417 base_breakpoint_print_one_detail (const struct breakpoint *self,
12418 struct ui_out *uiout)
12419 {
12420 /* nothing */
12421 }
12422
12423 static void
12424 base_breakpoint_print_mention (struct breakpoint *b)
12425 {
12426 internal_error_pure_virtual_called ();
12427 }
12428
12429 static void
12430 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12431 {
12432 internal_error_pure_virtual_called ();
12433 }
12434
12435 static void
12436 base_breakpoint_create_sals_from_location
12437 (struct event_location *location,
12438 struct linespec_result *canonical,
12439 enum bptype type_wanted)
12440 {
12441 internal_error_pure_virtual_called ();
12442 }
12443
12444 static void
12445 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12446 struct linespec_result *c,
12447 gdb::unique_xmalloc_ptr<char> cond_string,
12448 gdb::unique_xmalloc_ptr<char> extra_string,
12449 enum bptype type_wanted,
12450 enum bpdisp disposition,
12451 int thread,
12452 int task, int ignore_count,
12453 const struct breakpoint_ops *o,
12454 int from_tty, int enabled,
12455 int internal, unsigned flags)
12456 {
12457 internal_error_pure_virtual_called ();
12458 }
12459
12460 static std::vector<symtab_and_line>
12461 base_breakpoint_decode_location (struct breakpoint *b,
12462 struct event_location *location,
12463 struct program_space *search_pspace)
12464 {
12465 internal_error_pure_virtual_called ();
12466 }
12467
12468 /* The default 'explains_signal' method. */
12469
12470 static int
12471 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12472 {
12473 return 1;
12474 }
12475
12476 /* The default "after_condition_true" method. */
12477
12478 static void
12479 base_breakpoint_after_condition_true (struct bpstats *bs)
12480 {
12481 /* Nothing to do. */
12482 }
12483
12484 struct breakpoint_ops base_breakpoint_ops =
12485 {
12486 base_breakpoint_allocate_location,
12487 base_breakpoint_re_set,
12488 base_breakpoint_insert_location,
12489 base_breakpoint_remove_location,
12490 base_breakpoint_breakpoint_hit,
12491 base_breakpoint_check_status,
12492 base_breakpoint_resources_needed,
12493 base_breakpoint_works_in_software_mode,
12494 base_breakpoint_print_it,
12495 NULL,
12496 base_breakpoint_print_one_detail,
12497 base_breakpoint_print_mention,
12498 base_breakpoint_print_recreate,
12499 base_breakpoint_create_sals_from_location,
12500 base_breakpoint_create_breakpoints_sal,
12501 base_breakpoint_decode_location,
12502 base_breakpoint_explains_signal,
12503 base_breakpoint_after_condition_true,
12504 };
12505
12506 /* Default breakpoint_ops methods. */
12507
12508 static void
12509 bkpt_re_set (struct breakpoint *b)
12510 {
12511 /* FIXME: is this still reachable? */
12512 if (breakpoint_event_location_empty_p (b))
12513 {
12514 /* Anything without a location can't be re-set. */
12515 delete_breakpoint (b);
12516 return;
12517 }
12518
12519 breakpoint_re_set_default (b);
12520 }
12521
12522 static int
12523 bkpt_insert_location (struct bp_location *bl)
12524 {
12525 CORE_ADDR addr = bl->target_info.reqstd_address;
12526
12527 bl->target_info.kind = breakpoint_kind (bl, &addr);
12528 bl->target_info.placed_address = addr;
12529
12530 if (bl->loc_type == bp_loc_hardware_breakpoint)
12531 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12532 else
12533 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12534 }
12535
12536 static int
12537 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12538 {
12539 if (bl->loc_type == bp_loc_hardware_breakpoint)
12540 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12541 else
12542 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12543 }
12544
12545 static int
12546 bkpt_breakpoint_hit (const struct bp_location *bl,
12547 const address_space *aspace, CORE_ADDR bp_addr,
12548 const struct target_waitstatus *ws)
12549 {
12550 if (ws->kind != TARGET_WAITKIND_STOPPED
12551 || ws->value.sig != GDB_SIGNAL_TRAP)
12552 return 0;
12553
12554 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12555 aspace, bp_addr))
12556 return 0;
12557
12558 if (overlay_debugging /* unmapped overlay section */
12559 && section_is_overlay (bl->section)
12560 && !section_is_mapped (bl->section))
12561 return 0;
12562
12563 return 1;
12564 }
12565
12566 static int
12567 dprintf_breakpoint_hit (const struct bp_location *bl,
12568 const address_space *aspace, CORE_ADDR bp_addr,
12569 const struct target_waitstatus *ws)
12570 {
12571 if (dprintf_style == dprintf_style_agent
12572 && target_can_run_breakpoint_commands ())
12573 {
12574 /* An agent-style dprintf never causes a stop. If we see a trap
12575 for this address it must be for a breakpoint that happens to
12576 be set at the same address. */
12577 return 0;
12578 }
12579
12580 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12581 }
12582
12583 static int
12584 bkpt_resources_needed (const struct bp_location *bl)
12585 {
12586 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12587
12588 return 1;
12589 }
12590
12591 static enum print_stop_action
12592 bkpt_print_it (bpstat bs)
12593 {
12594 struct breakpoint *b;
12595 const struct bp_location *bl;
12596 int bp_temp;
12597 struct ui_out *uiout = current_uiout;
12598
12599 gdb_assert (bs->bp_location_at != NULL);
12600
12601 bl = bs->bp_location_at.get ();
12602 b = bs->breakpoint_at;
12603
12604 bp_temp = b->disposition == disp_del;
12605 if (bl->address != bl->requested_address)
12606 breakpoint_adjustment_warning (bl->requested_address,
12607 bl->address,
12608 b->number, 1);
12609 annotate_breakpoint (b->number);
12610 maybe_print_thread_hit_breakpoint (uiout);
12611
12612 if (uiout->is_mi_like_p ())
12613 {
12614 uiout->field_string ("reason",
12615 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12616 uiout->field_string ("disp", bpdisp_text (b->disposition));
12617 }
12618 if (bp_temp)
12619 uiout->message ("Temporary breakpoint %pF, ",
12620 signed_field ("bkptno", b->number));
12621 else
12622 uiout->message ("Breakpoint %pF, ",
12623 signed_field ("bkptno", b->number));
12624
12625 return PRINT_SRC_AND_LOC;
12626 }
12627
12628 static void
12629 bkpt_print_mention (struct breakpoint *b)
12630 {
12631 if (current_uiout->is_mi_like_p ())
12632 return;
12633
12634 switch (b->type)
12635 {
12636 case bp_breakpoint:
12637 case bp_gnu_ifunc_resolver:
12638 if (b->disposition == disp_del)
12639 printf_filtered (_("Temporary breakpoint"));
12640 else
12641 printf_filtered (_("Breakpoint"));
12642 printf_filtered (_(" %d"), b->number);
12643 if (b->type == bp_gnu_ifunc_resolver)
12644 printf_filtered (_(" at gnu-indirect-function resolver"));
12645 break;
12646 case bp_hardware_breakpoint:
12647 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12648 break;
12649 case bp_dprintf:
12650 printf_filtered (_("Dprintf %d"), b->number);
12651 break;
12652 }
12653
12654 say_where (b);
12655 }
12656
12657 static void
12658 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12659 {
12660 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12661 fprintf_unfiltered (fp, "tbreak");
12662 else if (tp->type == bp_breakpoint)
12663 fprintf_unfiltered (fp, "break");
12664 else if (tp->type == bp_hardware_breakpoint
12665 && tp->disposition == disp_del)
12666 fprintf_unfiltered (fp, "thbreak");
12667 else if (tp->type == bp_hardware_breakpoint)
12668 fprintf_unfiltered (fp, "hbreak");
12669 else
12670 internal_error (__FILE__, __LINE__,
12671 _("unhandled breakpoint type %d"), (int) tp->type);
12672
12673 fprintf_unfiltered (fp, " %s",
12674 event_location_to_string (tp->location.get ()));
12675
12676 /* Print out extra_string if this breakpoint is pending. It might
12677 contain, for example, conditions that were set by the user. */
12678 if (tp->loc == NULL && tp->extra_string != NULL)
12679 fprintf_unfiltered (fp, " %s", tp->extra_string);
12680
12681 print_recreate_thread (tp, fp);
12682 }
12683
12684 static void
12685 bkpt_create_sals_from_location (struct event_location *location,
12686 struct linespec_result *canonical,
12687 enum bptype type_wanted)
12688 {
12689 create_sals_from_location_default (location, canonical, type_wanted);
12690 }
12691
12692 static void
12693 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12694 struct linespec_result *canonical,
12695 gdb::unique_xmalloc_ptr<char> cond_string,
12696 gdb::unique_xmalloc_ptr<char> extra_string,
12697 enum bptype type_wanted,
12698 enum bpdisp disposition,
12699 int thread,
12700 int task, int ignore_count,
12701 const struct breakpoint_ops *ops,
12702 int from_tty, int enabled,
12703 int internal, unsigned flags)
12704 {
12705 create_breakpoints_sal_default (gdbarch, canonical,
12706 std::move (cond_string),
12707 std::move (extra_string),
12708 type_wanted,
12709 disposition, thread, task,
12710 ignore_count, ops, from_tty,
12711 enabled, internal, flags);
12712 }
12713
12714 static std::vector<symtab_and_line>
12715 bkpt_decode_location (struct breakpoint *b,
12716 struct event_location *location,
12717 struct program_space *search_pspace)
12718 {
12719 return decode_location_default (b, location, search_pspace);
12720 }
12721
12722 /* Virtual table for internal breakpoints. */
12723
12724 static void
12725 internal_bkpt_re_set (struct breakpoint *b)
12726 {
12727 switch (b->type)
12728 {
12729 /* Delete overlay event and longjmp master breakpoints; they
12730 will be reset later by breakpoint_re_set. */
12731 case bp_overlay_event:
12732 case bp_longjmp_master:
12733 case bp_std_terminate_master:
12734 case bp_exception_master:
12735 delete_breakpoint (b);
12736 break;
12737
12738 /* This breakpoint is special, it's set up when the inferior
12739 starts and we really don't want to touch it. */
12740 case bp_shlib_event:
12741
12742 /* Like bp_shlib_event, this breakpoint type is special. Once
12743 it is set up, we do not want to touch it. */
12744 case bp_thread_event:
12745 break;
12746 }
12747 }
12748
12749 static void
12750 internal_bkpt_check_status (bpstat bs)
12751 {
12752 if (bs->breakpoint_at->type == bp_shlib_event)
12753 {
12754 /* If requested, stop when the dynamic linker notifies GDB of
12755 events. This allows the user to get control and place
12756 breakpoints in initializer routines for dynamically loaded
12757 objects (among other things). */
12758 bs->stop = stop_on_solib_events;
12759 bs->print = stop_on_solib_events;
12760 }
12761 else
12762 bs->stop = 0;
12763 }
12764
12765 static enum print_stop_action
12766 internal_bkpt_print_it (bpstat bs)
12767 {
12768 struct breakpoint *b;
12769
12770 b = bs->breakpoint_at;
12771
12772 switch (b->type)
12773 {
12774 case bp_shlib_event:
12775 /* Did we stop because the user set the stop_on_solib_events
12776 variable? (If so, we report this as a generic, "Stopped due
12777 to shlib event" message.) */
12778 print_solib_event (0);
12779 break;
12780
12781 case bp_thread_event:
12782 /* Not sure how we will get here.
12783 GDB should not stop for these breakpoints. */
12784 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12785 break;
12786
12787 case bp_overlay_event:
12788 /* By analogy with the thread event, GDB should not stop for these. */
12789 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12790 break;
12791
12792 case bp_longjmp_master:
12793 /* These should never be enabled. */
12794 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12795 break;
12796
12797 case bp_std_terminate_master:
12798 /* These should never be enabled. */
12799 printf_filtered (_("std::terminate Master Breakpoint: "
12800 "gdb should not stop!\n"));
12801 break;
12802
12803 case bp_exception_master:
12804 /* These should never be enabled. */
12805 printf_filtered (_("Exception Master Breakpoint: "
12806 "gdb should not stop!\n"));
12807 break;
12808 }
12809
12810 return PRINT_NOTHING;
12811 }
12812
12813 static void
12814 internal_bkpt_print_mention (struct breakpoint *b)
12815 {
12816 /* Nothing to mention. These breakpoints are internal. */
12817 }
12818
12819 /* Virtual table for momentary breakpoints */
12820
12821 static void
12822 momentary_bkpt_re_set (struct breakpoint *b)
12823 {
12824 /* Keep temporary breakpoints, which can be encountered when we step
12825 over a dlopen call and solib_add is resetting the breakpoints.
12826 Otherwise these should have been blown away via the cleanup chain
12827 or by breakpoint_init_inferior when we rerun the executable. */
12828 }
12829
12830 static void
12831 momentary_bkpt_check_status (bpstat bs)
12832 {
12833 /* Nothing. The point of these breakpoints is causing a stop. */
12834 }
12835
12836 static enum print_stop_action
12837 momentary_bkpt_print_it (bpstat bs)
12838 {
12839 return PRINT_UNKNOWN;
12840 }
12841
12842 static void
12843 momentary_bkpt_print_mention (struct breakpoint *b)
12844 {
12845 /* Nothing to mention. These breakpoints are internal. */
12846 }
12847
12848 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12849
12850 It gets cleared already on the removal of the first one of such placed
12851 breakpoints. This is OK as they get all removed altogether. */
12852
12853 longjmp_breakpoint::~longjmp_breakpoint ()
12854 {
12855 thread_info *tp = find_thread_global_id (this->thread);
12856
12857 if (tp != NULL)
12858 tp->initiating_frame = null_frame_id;
12859 }
12860
12861 /* Specific methods for probe breakpoints. */
12862
12863 static int
12864 bkpt_probe_insert_location (struct bp_location *bl)
12865 {
12866 int v = bkpt_insert_location (bl);
12867
12868 if (v == 0)
12869 {
12870 /* The insertion was successful, now let's set the probe's semaphore
12871 if needed. */
12872 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
12873 }
12874
12875 return v;
12876 }
12877
12878 static int
12879 bkpt_probe_remove_location (struct bp_location *bl,
12880 enum remove_bp_reason reason)
12881 {
12882 /* Let's clear the semaphore before removing the location. */
12883 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
12884
12885 return bkpt_remove_location (bl, reason);
12886 }
12887
12888 static void
12889 bkpt_probe_create_sals_from_location (struct event_location *location,
12890 struct linespec_result *canonical,
12891 enum bptype type_wanted)
12892 {
12893 struct linespec_sals lsal;
12894
12895 lsal.sals = parse_probes (location, NULL, canonical);
12896 lsal.canonical
12897 = xstrdup (event_location_to_string (canonical->location.get ()));
12898 canonical->lsals.push_back (std::move (lsal));
12899 }
12900
12901 static std::vector<symtab_and_line>
12902 bkpt_probe_decode_location (struct breakpoint *b,
12903 struct event_location *location,
12904 struct program_space *search_pspace)
12905 {
12906 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12907 if (sals.empty ())
12908 error (_("probe not found"));
12909 return sals;
12910 }
12911
12912 /* The breakpoint_ops structure to be used in tracepoints. */
12913
12914 static void
12915 tracepoint_re_set (struct breakpoint *b)
12916 {
12917 breakpoint_re_set_default (b);
12918 }
12919
12920 static int
12921 tracepoint_breakpoint_hit (const struct bp_location *bl,
12922 const address_space *aspace, CORE_ADDR bp_addr,
12923 const struct target_waitstatus *ws)
12924 {
12925 /* By definition, the inferior does not report stops at
12926 tracepoints. */
12927 return 0;
12928 }
12929
12930 static void
12931 tracepoint_print_one_detail (const struct breakpoint *self,
12932 struct ui_out *uiout)
12933 {
12934 struct tracepoint *tp = (struct tracepoint *) self;
12935 if (!tp->static_trace_marker_id.empty ())
12936 {
12937 gdb_assert (self->type == bp_static_tracepoint);
12938
12939 uiout->message ("\tmarker id is %pF\n",
12940 string_field ("static-tracepoint-marker-string-id",
12941 tp->static_trace_marker_id.c_str ()));
12942 }
12943 }
12944
12945 static void
12946 tracepoint_print_mention (struct breakpoint *b)
12947 {
12948 if (current_uiout->is_mi_like_p ())
12949 return;
12950
12951 switch (b->type)
12952 {
12953 case bp_tracepoint:
12954 printf_filtered (_("Tracepoint"));
12955 printf_filtered (_(" %d"), b->number);
12956 break;
12957 case bp_fast_tracepoint:
12958 printf_filtered (_("Fast tracepoint"));
12959 printf_filtered (_(" %d"), b->number);
12960 break;
12961 case bp_static_tracepoint:
12962 printf_filtered (_("Static tracepoint"));
12963 printf_filtered (_(" %d"), b->number);
12964 break;
12965 default:
12966 internal_error (__FILE__, __LINE__,
12967 _("unhandled tracepoint type %d"), (int) b->type);
12968 }
12969
12970 say_where (b);
12971 }
12972
12973 static void
12974 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
12975 {
12976 struct tracepoint *tp = (struct tracepoint *) self;
12977
12978 if (self->type == bp_fast_tracepoint)
12979 fprintf_unfiltered (fp, "ftrace");
12980 else if (self->type == bp_static_tracepoint)
12981 fprintf_unfiltered (fp, "strace");
12982 else if (self->type == bp_tracepoint)
12983 fprintf_unfiltered (fp, "trace");
12984 else
12985 internal_error (__FILE__, __LINE__,
12986 _("unhandled tracepoint type %d"), (int) self->type);
12987
12988 fprintf_unfiltered (fp, " %s",
12989 event_location_to_string (self->location.get ()));
12990 print_recreate_thread (self, fp);
12991
12992 if (tp->pass_count)
12993 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
12994 }
12995
12996 static void
12997 tracepoint_create_sals_from_location (struct event_location *location,
12998 struct linespec_result *canonical,
12999 enum bptype type_wanted)
13000 {
13001 create_sals_from_location_default (location, canonical, type_wanted);
13002 }
13003
13004 static void
13005 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13006 struct linespec_result *canonical,
13007 gdb::unique_xmalloc_ptr<char> cond_string,
13008 gdb::unique_xmalloc_ptr<char> extra_string,
13009 enum bptype type_wanted,
13010 enum bpdisp disposition,
13011 int thread,
13012 int task, int ignore_count,
13013 const struct breakpoint_ops *ops,
13014 int from_tty, int enabled,
13015 int internal, unsigned flags)
13016 {
13017 create_breakpoints_sal_default (gdbarch, canonical,
13018 std::move (cond_string),
13019 std::move (extra_string),
13020 type_wanted,
13021 disposition, thread, task,
13022 ignore_count, ops, from_tty,
13023 enabled, internal, flags);
13024 }
13025
13026 static std::vector<symtab_and_line>
13027 tracepoint_decode_location (struct breakpoint *b,
13028 struct event_location *location,
13029 struct program_space *search_pspace)
13030 {
13031 return decode_location_default (b, location, search_pspace);
13032 }
13033
13034 struct breakpoint_ops tracepoint_breakpoint_ops;
13035
13036 /* Virtual table for tracepoints on static probes. */
13037
13038 static void
13039 tracepoint_probe_create_sals_from_location
13040 (struct event_location *location,
13041 struct linespec_result *canonical,
13042 enum bptype type_wanted)
13043 {
13044 /* We use the same method for breakpoint on probes. */
13045 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13046 }
13047
13048 static std::vector<symtab_and_line>
13049 tracepoint_probe_decode_location (struct breakpoint *b,
13050 struct event_location *location,
13051 struct program_space *search_pspace)
13052 {
13053 /* We use the same method for breakpoint on probes. */
13054 return bkpt_probe_decode_location (b, location, search_pspace);
13055 }
13056
13057 /* Dprintf breakpoint_ops methods. */
13058
13059 static void
13060 dprintf_re_set (struct breakpoint *b)
13061 {
13062 breakpoint_re_set_default (b);
13063
13064 /* extra_string should never be non-NULL for dprintf. */
13065 gdb_assert (b->extra_string != NULL);
13066
13067 /* 1 - connect to target 1, that can run breakpoint commands.
13068 2 - create a dprintf, which resolves fine.
13069 3 - disconnect from target 1
13070 4 - connect to target 2, that can NOT run breakpoint commands.
13071
13072 After steps #3/#4, you'll want the dprintf command list to
13073 be updated, because target 1 and 2 may well return different
13074 answers for target_can_run_breakpoint_commands().
13075 Given absence of finer grained resetting, we get to do
13076 it all the time. */
13077 if (b->extra_string != NULL)
13078 update_dprintf_command_list (b);
13079 }
13080
13081 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13082
13083 static void
13084 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13085 {
13086 fprintf_unfiltered (fp, "dprintf %s,%s",
13087 event_location_to_string (tp->location.get ()),
13088 tp->extra_string);
13089 print_recreate_thread (tp, fp);
13090 }
13091
13092 /* Implement the "after_condition_true" breakpoint_ops method for
13093 dprintf.
13094
13095 dprintf's are implemented with regular commands in their command
13096 list, but we run the commands here instead of before presenting the
13097 stop to the user, as dprintf's don't actually cause a stop. This
13098 also makes it so that the commands of multiple dprintfs at the same
13099 address are all handled. */
13100
13101 static void
13102 dprintf_after_condition_true (struct bpstats *bs)
13103 {
13104 struct bpstats tmp_bs;
13105 struct bpstats *tmp_bs_p = &tmp_bs;
13106
13107 /* dprintf's never cause a stop. This wasn't set in the
13108 check_status hook instead because that would make the dprintf's
13109 condition not be evaluated. */
13110 bs->stop = 0;
13111
13112 /* Run the command list here. Take ownership of it instead of
13113 copying. We never want these commands to run later in
13114 bpstat_do_actions, if a breakpoint that causes a stop happens to
13115 be set at same address as this dprintf, or even if running the
13116 commands here throws. */
13117 tmp_bs.commands = bs->commands;
13118 bs->commands = NULL;
13119
13120 bpstat_do_actions_1 (&tmp_bs_p);
13121
13122 /* 'tmp_bs.commands' will usually be NULL by now, but
13123 bpstat_do_actions_1 may return early without processing the whole
13124 list. */
13125 }
13126
13127 /* The breakpoint_ops structure to be used on static tracepoints with
13128 markers (`-m'). */
13129
13130 static void
13131 strace_marker_create_sals_from_location (struct event_location *location,
13132 struct linespec_result *canonical,
13133 enum bptype type_wanted)
13134 {
13135 struct linespec_sals lsal;
13136 const char *arg_start, *arg;
13137
13138 arg = arg_start = get_linespec_location (location)->spec_string;
13139 lsal.sals = decode_static_tracepoint_spec (&arg);
13140
13141 std::string str (arg_start, arg - arg_start);
13142 const char *ptr = str.c_str ();
13143 canonical->location
13144 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
13145
13146 lsal.canonical
13147 = xstrdup (event_location_to_string (canonical->location.get ()));
13148 canonical->lsals.push_back (std::move (lsal));
13149 }
13150
13151 static void
13152 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13153 struct linespec_result *canonical,
13154 gdb::unique_xmalloc_ptr<char> cond_string,
13155 gdb::unique_xmalloc_ptr<char> extra_string,
13156 enum bptype type_wanted,
13157 enum bpdisp disposition,
13158 int thread,
13159 int task, int ignore_count,
13160 const struct breakpoint_ops *ops,
13161 int from_tty, int enabled,
13162 int internal, unsigned flags)
13163 {
13164 const linespec_sals &lsal = canonical->lsals[0];
13165
13166 /* If the user is creating a static tracepoint by marker id
13167 (strace -m MARKER_ID), then store the sals index, so that
13168 breakpoint_re_set can try to match up which of the newly
13169 found markers corresponds to this one, and, don't try to
13170 expand multiple locations for each sal, given than SALS
13171 already should contain all sals for MARKER_ID. */
13172
13173 for (size_t i = 0; i < lsal.sals.size (); i++)
13174 {
13175 event_location_up location
13176 = copy_event_location (canonical->location.get ());
13177
13178 std::unique_ptr<tracepoint> tp (new tracepoint ());
13179 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
13180 std::move (location), NULL,
13181 std::move (cond_string),
13182 std::move (extra_string),
13183 type_wanted, disposition,
13184 thread, task, ignore_count, ops,
13185 from_tty, enabled, internal, flags,
13186 canonical->special_display);
13187 /* Given that its possible to have multiple markers with
13188 the same string id, if the user is creating a static
13189 tracepoint by marker id ("strace -m MARKER_ID"), then
13190 store the sals index, so that breakpoint_re_set can
13191 try to match up which of the newly found markers
13192 corresponds to this one */
13193 tp->static_trace_marker_id_idx = i;
13194
13195 install_breakpoint (internal, std::move (tp), 0);
13196 }
13197 }
13198
13199 static std::vector<symtab_and_line>
13200 strace_marker_decode_location (struct breakpoint *b,
13201 struct event_location *location,
13202 struct program_space *search_pspace)
13203 {
13204 struct tracepoint *tp = (struct tracepoint *) b;
13205 const char *s = get_linespec_location (location)->spec_string;
13206
13207 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13208 if (sals.size () > tp->static_trace_marker_id_idx)
13209 {
13210 sals[0] = sals[tp->static_trace_marker_id_idx];
13211 sals.resize (1);
13212 return sals;
13213 }
13214 else
13215 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
13216 }
13217
13218 static struct breakpoint_ops strace_marker_breakpoint_ops;
13219
13220 static int
13221 strace_marker_p (struct breakpoint *b)
13222 {
13223 return b->ops == &strace_marker_breakpoint_ops;
13224 }
13225
13226 /* Delete a breakpoint and clean up all traces of it in the data
13227 structures. */
13228
13229 void
13230 delete_breakpoint (struct breakpoint *bpt)
13231 {
13232 gdb_assert (bpt != NULL);
13233
13234 /* Has this bp already been deleted? This can happen because
13235 multiple lists can hold pointers to bp's. bpstat lists are
13236 especial culprits.
13237
13238 One example of this happening is a watchpoint's scope bp. When
13239 the scope bp triggers, we notice that the watchpoint is out of
13240 scope, and delete it. We also delete its scope bp. But the
13241 scope bp is marked "auto-deleting", and is already on a bpstat.
13242 That bpstat is then checked for auto-deleting bp's, which are
13243 deleted.
13244
13245 A real solution to this problem might involve reference counts in
13246 bp's, and/or giving them pointers back to their referencing
13247 bpstat's, and teaching delete_breakpoint to only free a bp's
13248 storage when no more references were extent. A cheaper bandaid
13249 was chosen. */
13250 if (bpt->type == bp_none)
13251 return;
13252
13253 /* At least avoid this stale reference until the reference counting
13254 of breakpoints gets resolved. */
13255 if (bpt->related_breakpoint != bpt)
13256 {
13257 struct breakpoint *related;
13258 struct watchpoint *w;
13259
13260 if (bpt->type == bp_watchpoint_scope)
13261 w = (struct watchpoint *) bpt->related_breakpoint;
13262 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13263 w = (struct watchpoint *) bpt;
13264 else
13265 w = NULL;
13266 if (w != NULL)
13267 watchpoint_del_at_next_stop (w);
13268
13269 /* Unlink bpt from the bpt->related_breakpoint ring. */
13270 for (related = bpt; related->related_breakpoint != bpt;
13271 related = related->related_breakpoint);
13272 related->related_breakpoint = bpt->related_breakpoint;
13273 bpt->related_breakpoint = bpt;
13274 }
13275
13276 /* watch_command_1 creates a watchpoint but only sets its number if
13277 update_watchpoint succeeds in creating its bp_locations. If there's
13278 a problem in that process, we'll be asked to delete the half-created
13279 watchpoint. In that case, don't announce the deletion. */
13280 if (bpt->number)
13281 gdb::observers::breakpoint_deleted.notify (bpt);
13282
13283 if (breakpoint_chain == bpt)
13284 breakpoint_chain = bpt->next;
13285
13286 for (breakpoint *b : all_breakpoints ())
13287 if (b->next == bpt)
13288 {
13289 b->next = bpt->next;
13290 break;
13291 }
13292
13293 /* Be sure no bpstat's are pointing at the breakpoint after it's
13294 been freed. */
13295 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13296 in all threads for now. Note that we cannot just remove bpstats
13297 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13298 commands are associated with the bpstat; if we remove it here,
13299 then the later call to bpstat_do_actions (&stop_bpstat); in
13300 event-top.c won't do anything, and temporary breakpoints with
13301 commands won't work. */
13302
13303 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13304
13305 /* Now that breakpoint is removed from breakpoint list, update the
13306 global location list. This will remove locations that used to
13307 belong to this breakpoint. Do this before freeing the breakpoint
13308 itself, since remove_breakpoint looks at location's owner. It
13309 might be better design to have location completely
13310 self-contained, but it's not the case now. */
13311 update_global_location_list (UGLL_DONT_INSERT);
13312
13313 /* On the chance that someone will soon try again to delete this
13314 same bp, we mark it as deleted before freeing its storage. */
13315 bpt->type = bp_none;
13316 delete bpt;
13317 }
13318
13319 /* Iterator function to call a user-provided callback function once
13320 for each of B and its related breakpoints. */
13321
13322 static void
13323 iterate_over_related_breakpoints (struct breakpoint *b,
13324 gdb::function_view<void (breakpoint *)> function)
13325 {
13326 struct breakpoint *related;
13327
13328 related = b;
13329 do
13330 {
13331 struct breakpoint *next;
13332
13333 /* FUNCTION may delete RELATED. */
13334 next = related->related_breakpoint;
13335
13336 if (next == related)
13337 {
13338 /* RELATED is the last ring entry. */
13339 function (related);
13340
13341 /* FUNCTION may have deleted it, so we'd never reach back to
13342 B. There's nothing left to do anyway, so just break
13343 out. */
13344 break;
13345 }
13346 else
13347 function (related);
13348
13349 related = next;
13350 }
13351 while (related != b);
13352 }
13353
13354 static void
13355 delete_command (const char *arg, int from_tty)
13356 {
13357 dont_repeat ();
13358
13359 if (arg == 0)
13360 {
13361 int breaks_to_delete = 0;
13362
13363 /* Delete all breakpoints if no argument. Do not delete
13364 internal breakpoints, these have to be deleted with an
13365 explicit breakpoint number argument. */
13366 for (breakpoint *b : all_breakpoints ())
13367 if (user_breakpoint_p (b))
13368 {
13369 breaks_to_delete = 1;
13370 break;
13371 }
13372
13373 /* Ask user only if there are some breakpoints to delete. */
13374 if (!from_tty
13375 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13376 for (breakpoint *b : all_breakpoints_safe ())
13377 if (user_breakpoint_p (b))
13378 delete_breakpoint (b);
13379 }
13380 else
13381 map_breakpoint_numbers
13382 (arg, [&] (breakpoint *br)
13383 {
13384 iterate_over_related_breakpoints (br, delete_breakpoint);
13385 });
13386 }
13387
13388 /* Return true if all locations of B bound to PSPACE are pending. If
13389 PSPACE is NULL, all locations of all program spaces are
13390 considered. */
13391
13392 static int
13393 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13394 {
13395 for (bp_location *loc : b->locations ())
13396 if ((pspace == NULL
13397 || loc->pspace == pspace)
13398 && !loc->shlib_disabled
13399 && !loc->pspace->executing_startup)
13400 return 0;
13401 return 1;
13402 }
13403
13404 /* Subroutine of update_breakpoint_locations to simplify it.
13405 Return non-zero if multiple fns in list LOC have the same name.
13406 Null names are ignored. */
13407
13408 static int
13409 ambiguous_names_p (struct bp_location *loc)
13410 {
13411 struct bp_location *l;
13412 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
13413 xcalloc, xfree));
13414
13415 for (l = loc; l != NULL; l = l->next)
13416 {
13417 const char **slot;
13418 const char *name = l->function_name;
13419
13420 /* Allow for some names to be NULL, ignore them. */
13421 if (name == NULL)
13422 continue;
13423
13424 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
13425 INSERT);
13426 /* NOTE: We can assume slot != NULL here because xcalloc never
13427 returns NULL. */
13428 if (*slot != NULL)
13429 return 1;
13430 *slot = name;
13431 }
13432
13433 return 0;
13434 }
13435
13436 /* When symbols change, it probably means the sources changed as well,
13437 and it might mean the static tracepoint markers are no longer at
13438 the same address or line numbers they used to be at last we
13439 checked. Losing your static tracepoints whenever you rebuild is
13440 undesirable. This function tries to resync/rematch gdb static
13441 tracepoints with the markers on the target, for static tracepoints
13442 that have not been set by marker id. Static tracepoint that have
13443 been set by marker id are reset by marker id in breakpoint_re_set.
13444 The heuristic is:
13445
13446 1) For a tracepoint set at a specific address, look for a marker at
13447 the old PC. If one is found there, assume to be the same marker.
13448 If the name / string id of the marker found is different from the
13449 previous known name, assume that means the user renamed the marker
13450 in the sources, and output a warning.
13451
13452 2) For a tracepoint set at a given line number, look for a marker
13453 at the new address of the old line number. If one is found there,
13454 assume to be the same marker. If the name / string id of the
13455 marker found is different from the previous known name, assume that
13456 means the user renamed the marker in the sources, and output a
13457 warning.
13458
13459 3) If a marker is no longer found at the same address or line, it
13460 may mean the marker no longer exists. But it may also just mean
13461 the code changed a bit. Maybe the user added a few lines of code
13462 that made the marker move up or down (in line number terms). Ask
13463 the target for info about the marker with the string id as we knew
13464 it. If found, update line number and address in the matching
13465 static tracepoint. This will get confused if there's more than one
13466 marker with the same ID (possible in UST, although unadvised
13467 precisely because it confuses tools). */
13468
13469 static struct symtab_and_line
13470 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13471 {
13472 struct tracepoint *tp = (struct tracepoint *) b;
13473 struct static_tracepoint_marker marker;
13474 CORE_ADDR pc;
13475
13476 pc = sal.pc;
13477 if (sal.line)
13478 find_line_pc (sal.symtab, sal.line, &pc);
13479
13480 if (target_static_tracepoint_marker_at (pc, &marker))
13481 {
13482 if (tp->static_trace_marker_id != marker.str_id)
13483 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13484 b->number, tp->static_trace_marker_id.c_str (),
13485 marker.str_id.c_str ());
13486
13487 tp->static_trace_marker_id = std::move (marker.str_id);
13488
13489 return sal;
13490 }
13491
13492 /* Old marker wasn't found on target at lineno. Try looking it up
13493 by string ID. */
13494 if (!sal.explicit_pc
13495 && sal.line != 0
13496 && sal.symtab != NULL
13497 && !tp->static_trace_marker_id.empty ())
13498 {
13499 std::vector<static_tracepoint_marker> markers
13500 = target_static_tracepoint_markers_by_strid
13501 (tp->static_trace_marker_id.c_str ());
13502
13503 if (!markers.empty ())
13504 {
13505 struct symbol *sym;
13506 struct static_tracepoint_marker *tpmarker;
13507 struct ui_out *uiout = current_uiout;
13508 struct explicit_location explicit_loc;
13509
13510 tpmarker = &markers[0];
13511
13512 tp->static_trace_marker_id = std::move (tpmarker->str_id);
13513
13514 warning (_("marker for static tracepoint %d (%s) not "
13515 "found at previous line number"),
13516 b->number, tp->static_trace_marker_id.c_str ());
13517
13518 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
13519 sym = find_pc_sect_function (tpmarker->address, NULL);
13520 uiout->text ("Now in ");
13521 if (sym)
13522 {
13523 uiout->field_string ("func", sym->print_name (),
13524 function_name_style.style ());
13525 uiout->text (" at ");
13526 }
13527 uiout->field_string ("file",
13528 symtab_to_filename_for_display (sal2.symtab),
13529 file_name_style.style ());
13530 uiout->text (":");
13531
13532 if (uiout->is_mi_like_p ())
13533 {
13534 const char *fullname = symtab_to_fullname (sal2.symtab);
13535
13536 uiout->field_string ("fullname", fullname);
13537 }
13538
13539 uiout->field_signed ("line", sal2.line);
13540 uiout->text ("\n");
13541
13542 b->loc->line_number = sal2.line;
13543 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13544
13545 b->location.reset (NULL);
13546 initialize_explicit_location (&explicit_loc);
13547 explicit_loc.source_filename
13548 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13549 explicit_loc.line_offset.offset = b->loc->line_number;
13550 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13551 b->location = new_explicit_location (&explicit_loc);
13552
13553 /* Might be nice to check if function changed, and warn if
13554 so. */
13555 }
13556 }
13557 return sal;
13558 }
13559
13560 /* Returns 1 iff locations A and B are sufficiently same that
13561 we don't need to report breakpoint as changed. */
13562
13563 static int
13564 locations_are_equal (struct bp_location *a, struct bp_location *b)
13565 {
13566 while (a && b)
13567 {
13568 if (a->address != b->address)
13569 return 0;
13570
13571 if (a->shlib_disabled != b->shlib_disabled)
13572 return 0;
13573
13574 if (a->enabled != b->enabled)
13575 return 0;
13576
13577 if (a->disabled_by_cond != b->disabled_by_cond)
13578 return 0;
13579
13580 a = a->next;
13581 b = b->next;
13582 }
13583
13584 if ((a == NULL) != (b == NULL))
13585 return 0;
13586
13587 return 1;
13588 }
13589
13590 /* Split all locations of B that are bound to PSPACE out of B's
13591 location list to a separate list and return that list's head. If
13592 PSPACE is NULL, hoist out all locations of B. */
13593
13594 static struct bp_location *
13595 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13596 {
13597 struct bp_location head;
13598 struct bp_location *i = b->loc;
13599 struct bp_location **i_link = &b->loc;
13600 struct bp_location *hoisted = &head;
13601
13602 if (pspace == NULL)
13603 {
13604 i = b->loc;
13605 b->loc = NULL;
13606 return i;
13607 }
13608
13609 head.next = NULL;
13610
13611 while (i != NULL)
13612 {
13613 if (i->pspace == pspace)
13614 {
13615 *i_link = i->next;
13616 i->next = NULL;
13617 hoisted->next = i;
13618 hoisted = i;
13619 }
13620 else
13621 i_link = &i->next;
13622 i = *i_link;
13623 }
13624
13625 return head.next;
13626 }
13627
13628 /* Create new breakpoint locations for B (a hardware or software
13629 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13630 zero, then B is a ranged breakpoint. Only recreates locations for
13631 FILTER_PSPACE. Locations of other program spaces are left
13632 untouched. */
13633
13634 void
13635 update_breakpoint_locations (struct breakpoint *b,
13636 struct program_space *filter_pspace,
13637 gdb::array_view<const symtab_and_line> sals,
13638 gdb::array_view<const symtab_and_line> sals_end)
13639 {
13640 struct bp_location *existing_locations;
13641
13642 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
13643 {
13644 /* Ranged breakpoints have only one start location and one end
13645 location. */
13646 b->enable_state = bp_disabled;
13647 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13648 "multiple locations found\n"),
13649 b->number);
13650 return;
13651 }
13652
13653 /* If there's no new locations, and all existing locations are
13654 pending, don't do anything. This optimizes the common case where
13655 all locations are in the same shared library, that was unloaded.
13656 We'd like to retain the location, so that when the library is
13657 loaded again, we don't loose the enabled/disabled status of the
13658 individual locations. */
13659 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
13660 return;
13661
13662 existing_locations = hoist_existing_locations (b, filter_pspace);
13663
13664 for (const auto &sal : sals)
13665 {
13666 struct bp_location *new_loc;
13667
13668 switch_to_program_space_and_thread (sal.pspace);
13669
13670 new_loc = add_location_to_breakpoint (b, &sal);
13671
13672 /* Reparse conditions, they might contain references to the
13673 old symtab. */
13674 if (b->cond_string != NULL)
13675 {
13676 const char *s;
13677
13678 s = b->cond_string;
13679 try
13680 {
13681 new_loc->cond = parse_exp_1 (&s, sal.pc,
13682 block_for_pc (sal.pc),
13683 0);
13684 }
13685 catch (const gdb_exception_error &e)
13686 {
13687 new_loc->disabled_by_cond = true;
13688 }
13689 }
13690
13691 if (!sals_end.empty ())
13692 {
13693 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13694
13695 new_loc->length = end - sals[0].pc + 1;
13696 }
13697 }
13698
13699 /* If possible, carry over 'disable' status from existing
13700 breakpoints. */
13701 {
13702 struct bp_location *e = existing_locations;
13703 /* If there are multiple breakpoints with the same function name,
13704 e.g. for inline functions, comparing function names won't work.
13705 Instead compare pc addresses; this is just a heuristic as things
13706 may have moved, but in practice it gives the correct answer
13707 often enough until a better solution is found. */
13708 int have_ambiguous_names = ambiguous_names_p (b->loc);
13709
13710 for (; e; e = e->next)
13711 {
13712 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
13713 {
13714 if (have_ambiguous_names)
13715 {
13716 for (bp_location *l : b->locations ())
13717 {
13718 /* Ignore software vs hardware location type at
13719 this point, because with "set breakpoint
13720 auto-hw", after a re-set, locations that were
13721 hardware can end up as software, or vice versa.
13722 As mentioned above, this is an heuristic and in
13723 practice should give the correct answer often
13724 enough. */
13725 if (breakpoint_locations_match (e, l, true))
13726 {
13727 l->enabled = e->enabled;
13728 l->disabled_by_cond = e->disabled_by_cond;
13729 break;
13730 }
13731 }
13732 }
13733 else
13734 {
13735 for (bp_location *l : b->locations ())
13736 if (l->function_name
13737 && strcmp (e->function_name, l->function_name) == 0)
13738 {
13739 l->enabled = e->enabled;
13740 l->disabled_by_cond = e->disabled_by_cond;
13741 break;
13742 }
13743 }
13744 }
13745 }
13746 }
13747
13748 if (!locations_are_equal (existing_locations, b->loc))
13749 gdb::observers::breakpoint_modified.notify (b);
13750 }
13751
13752 /* Find the SaL locations corresponding to the given LOCATION.
13753 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13754
13755 static std::vector<symtab_and_line>
13756 location_to_sals (struct breakpoint *b, struct event_location *location,
13757 struct program_space *search_pspace, int *found)
13758 {
13759 struct gdb_exception exception;
13760
13761 gdb_assert (b->ops != NULL);
13762
13763 std::vector<symtab_and_line> sals;
13764
13765 try
13766 {
13767 sals = b->ops->decode_location (b, location, search_pspace);
13768 }
13769 catch (gdb_exception_error &e)
13770 {
13771 int not_found_and_ok = 0;
13772
13773 /* For pending breakpoints, it's expected that parsing will
13774 fail until the right shared library is loaded. User has
13775 already told to create pending breakpoints and don't need
13776 extra messages. If breakpoint is in bp_shlib_disabled
13777 state, then user already saw the message about that
13778 breakpoint being disabled, and don't want to see more
13779 errors. */
13780 if (e.error == NOT_FOUND_ERROR
13781 && (b->condition_not_parsed
13782 || (b->loc != NULL
13783 && search_pspace != NULL
13784 && b->loc->pspace != search_pspace)
13785 || (b->loc && b->loc->shlib_disabled)
13786 || (b->loc && b->loc->pspace->executing_startup)
13787 || b->enable_state == bp_disabled))
13788 not_found_and_ok = 1;
13789
13790 if (!not_found_and_ok)
13791 {
13792 /* We surely don't want to warn about the same breakpoint
13793 10 times. One solution, implemented here, is disable
13794 the breakpoint on error. Another solution would be to
13795 have separate 'warning emitted' flag. Since this
13796 happens only when a binary has changed, I don't know
13797 which approach is better. */
13798 b->enable_state = bp_disabled;
13799 throw;
13800 }
13801
13802 exception = std::move (e);
13803 }
13804
13805 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13806 {
13807 for (auto &sal : sals)
13808 resolve_sal_pc (&sal);
13809 if (b->condition_not_parsed && b->extra_string != NULL)
13810 {
13811 char *cond_string, *extra_string;
13812 int thread, task;
13813
13814 find_condition_and_thread_for_sals (sals, b->extra_string,
13815 &cond_string, &thread,
13816 &task, &extra_string);
13817 gdb_assert (b->cond_string == NULL);
13818 if (cond_string)
13819 b->cond_string = cond_string;
13820 b->thread = thread;
13821 b->task = task;
13822 if (extra_string)
13823 {
13824 xfree (b->extra_string);
13825 b->extra_string = extra_string;
13826 }
13827 b->condition_not_parsed = 0;
13828 }
13829
13830 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13831 sals[0] = update_static_tracepoint (b, sals[0]);
13832
13833 *found = 1;
13834 }
13835 else
13836 *found = 0;
13837
13838 return sals;
13839 }
13840
13841 /* The default re_set method, for typical hardware or software
13842 breakpoints. Reevaluate the breakpoint and recreate its
13843 locations. */
13844
13845 static void
13846 breakpoint_re_set_default (struct breakpoint *b)
13847 {
13848 struct program_space *filter_pspace = current_program_space;
13849 std::vector<symtab_and_line> expanded, expanded_end;
13850
13851 int found;
13852 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13853 filter_pspace, &found);
13854 if (found)
13855 expanded = std::move (sals);
13856
13857 if (b->location_range_end != NULL)
13858 {
13859 std::vector<symtab_and_line> sals_end
13860 = location_to_sals (b, b->location_range_end.get (),
13861 filter_pspace, &found);
13862 if (found)
13863 expanded_end = std::move (sals_end);
13864 }
13865
13866 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
13867 }
13868
13869 /* Default method for creating SALs from an address string. It basically
13870 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13871
13872 static void
13873 create_sals_from_location_default (struct event_location *location,
13874 struct linespec_result *canonical,
13875 enum bptype type_wanted)
13876 {
13877 parse_breakpoint_sals (location, canonical);
13878 }
13879
13880 /* Call create_breakpoints_sal for the given arguments. This is the default
13881 function for the `create_breakpoints_sal' method of
13882 breakpoint_ops. */
13883
13884 static void
13885 create_breakpoints_sal_default (struct gdbarch *gdbarch,
13886 struct linespec_result *canonical,
13887 gdb::unique_xmalloc_ptr<char> cond_string,
13888 gdb::unique_xmalloc_ptr<char> extra_string,
13889 enum bptype type_wanted,
13890 enum bpdisp disposition,
13891 int thread,
13892 int task, int ignore_count,
13893 const struct breakpoint_ops *ops,
13894 int from_tty, int enabled,
13895 int internal, unsigned flags)
13896 {
13897 create_breakpoints_sal (gdbarch, canonical,
13898 std::move (cond_string),
13899 std::move (extra_string),
13900 type_wanted, disposition,
13901 thread, task, ignore_count, ops, from_tty,
13902 enabled, internal, flags);
13903 }
13904
13905 /* Decode the line represented by S by calling decode_line_full. This is the
13906 default function for the `decode_location' method of breakpoint_ops. */
13907
13908 static std::vector<symtab_and_line>
13909 decode_location_default (struct breakpoint *b,
13910 struct event_location *location,
13911 struct program_space *search_pspace)
13912 {
13913 struct linespec_result canonical;
13914
13915 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
13916 NULL, 0, &canonical, multiple_symbols_all,
13917 b->filter.get ());
13918
13919 /* We should get 0 or 1 resulting SALs. */
13920 gdb_assert (canonical.lsals.size () < 2);
13921
13922 if (!canonical.lsals.empty ())
13923 {
13924 const linespec_sals &lsal = canonical.lsals[0];
13925 return std::move (lsal.sals);
13926 }
13927 return {};
13928 }
13929
13930 /* Reset a breakpoint. */
13931
13932 static void
13933 breakpoint_re_set_one (breakpoint *b)
13934 {
13935 input_radix = b->input_radix;
13936 set_language (b->language);
13937
13938 b->ops->re_set (b);
13939 }
13940
13941 /* Re-set breakpoint locations for the current program space.
13942 Locations bound to other program spaces are left untouched. */
13943
13944 void
13945 breakpoint_re_set (void)
13946 {
13947 {
13948 scoped_restore_current_language save_language;
13949 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
13950 scoped_restore_current_pspace_and_thread restore_pspace_thread;
13951
13952 /* breakpoint_re_set_one sets the current_language to the language
13953 of the breakpoint it is resetting (see prepare_re_set_context)
13954 before re-evaluating the breakpoint's location. This change can
13955 unfortunately get undone by accident if the language_mode is set
13956 to auto, and we either switch frames, or more likely in this context,
13957 we select the current frame.
13958
13959 We prevent this by temporarily turning the language_mode to
13960 language_mode_manual. We restore it once all breakpoints
13961 have been reset. */
13962 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13963 language_mode = language_mode_manual;
13964
13965 /* Note: we must not try to insert locations until after all
13966 breakpoints have been re-set. Otherwise, e.g., when re-setting
13967 breakpoint 1, we'd insert the locations of breakpoint 2, which
13968 hadn't been re-set yet, and thus may have stale locations. */
13969
13970 for (breakpoint *b : all_breakpoints_safe ())
13971 {
13972 try
13973 {
13974 breakpoint_re_set_one (b);
13975 }
13976 catch (const gdb_exception &ex)
13977 {
13978 exception_fprintf (gdb_stderr, ex,
13979 "Error in re-setting breakpoint %d: ",
13980 b->number);
13981 }
13982 }
13983
13984 jit_breakpoint_re_set ();
13985 }
13986
13987 create_overlay_event_breakpoint ();
13988 create_longjmp_master_breakpoint ();
13989 create_std_terminate_master_breakpoint ();
13990 create_exception_master_breakpoint ();
13991
13992 /* Now we can insert. */
13993 update_global_location_list (UGLL_MAY_INSERT);
13994 }
13995 \f
13996 /* Reset the thread number of this breakpoint:
13997
13998 - If the breakpoint is for all threads, leave it as-is.
13999 - Else, reset it to the current thread for inferior_ptid. */
14000 void
14001 breakpoint_re_set_thread (struct breakpoint *b)
14002 {
14003 if (b->thread != -1)
14004 {
14005 b->thread = inferior_thread ()->global_num;
14006
14007 /* We're being called after following a fork. The new fork is
14008 selected as current, and unless this was a vfork will have a
14009 different program space from the original thread. Reset that
14010 as well. */
14011 b->loc->pspace = current_program_space;
14012 }
14013 }
14014
14015 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14016 If from_tty is nonzero, it prints a message to that effect,
14017 which ends with a period (no newline). */
14018
14019 void
14020 set_ignore_count (int bptnum, int count, int from_tty)
14021 {
14022 if (count < 0)
14023 count = 0;
14024
14025 for (breakpoint *b : all_breakpoints ())
14026 if (b->number == bptnum)
14027 {
14028 if (is_tracepoint (b))
14029 {
14030 if (from_tty && count != 0)
14031 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14032 bptnum);
14033 return;
14034 }
14035
14036 b->ignore_count = count;
14037 if (from_tty)
14038 {
14039 if (count == 0)
14040 printf_filtered (_("Will stop next time "
14041 "breakpoint %d is reached."),
14042 bptnum);
14043 else if (count == 1)
14044 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14045 bptnum);
14046 else
14047 printf_filtered (_("Will ignore next %d "
14048 "crossings of breakpoint %d."),
14049 count, bptnum);
14050 }
14051 gdb::observers::breakpoint_modified.notify (b);
14052 return;
14053 }
14054
14055 error (_("No breakpoint number %d."), bptnum);
14056 }
14057
14058 /* Command to set ignore-count of breakpoint N to COUNT. */
14059
14060 static void
14061 ignore_command (const char *args, int from_tty)
14062 {
14063 const char *p = args;
14064 int num;
14065
14066 if (p == 0)
14067 error_no_arg (_("a breakpoint number"));
14068
14069 num = get_number (&p);
14070 if (num == 0)
14071 error (_("bad breakpoint number: '%s'"), args);
14072 if (*p == 0)
14073 error (_("Second argument (specified ignore-count) is missing."));
14074
14075 set_ignore_count (num,
14076 longest_to_int (value_as_long (parse_and_eval (p))),
14077 from_tty);
14078 if (from_tty)
14079 printf_filtered ("\n");
14080 }
14081 \f
14082
14083 /* Call FUNCTION on each of the breakpoints with numbers in the range
14084 defined by BP_NUM_RANGE (an inclusive range). */
14085
14086 static void
14087 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
14088 gdb::function_view<void (breakpoint *)> function)
14089 {
14090 if (bp_num_range.first == 0)
14091 {
14092 warning (_("bad breakpoint number at or near '%d'"),
14093 bp_num_range.first);
14094 }
14095 else
14096 {
14097 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
14098 {
14099 bool match = false;
14100
14101 for (breakpoint *b : all_breakpoints_safe ())
14102 if (b->number == i)
14103 {
14104 match = true;
14105 function (b);
14106 break;
14107 }
14108 if (!match)
14109 printf_unfiltered (_("No breakpoint number %d.\n"), i);
14110 }
14111 }
14112 }
14113
14114 /* Call FUNCTION on each of the breakpoints whose numbers are given in
14115 ARGS. */
14116
14117 static void
14118 map_breakpoint_numbers (const char *args,
14119 gdb::function_view<void (breakpoint *)> function)
14120 {
14121 if (args == NULL || *args == '\0')
14122 error_no_arg (_("one or more breakpoint numbers"));
14123
14124 number_or_range_parser parser (args);
14125
14126 while (!parser.finished ())
14127 {
14128 int num = parser.get_number ();
14129 map_breakpoint_number_range (std::make_pair (num, num), function);
14130 }
14131 }
14132
14133 /* Return the breakpoint location structure corresponding to the
14134 BP_NUM and LOC_NUM values. */
14135
14136 static struct bp_location *
14137 find_location_by_number (int bp_num, int loc_num)
14138 {
14139 breakpoint *b = get_breakpoint (bp_num);
14140
14141 if (!b || b->number != bp_num)
14142 error (_("Bad breakpoint number '%d'"), bp_num);
14143
14144 if (loc_num == 0)
14145 error (_("Bad breakpoint location number '%d'"), loc_num);
14146
14147 int n = 0;
14148 for (bp_location *loc : b->locations ())
14149 if (++n == loc_num)
14150 return loc;
14151
14152 error (_("Bad breakpoint location number '%d'"), loc_num);
14153 }
14154
14155 /* Modes of operation for extract_bp_num. */
14156 enum class extract_bp_kind
14157 {
14158 /* Extracting a breakpoint number. */
14159 bp,
14160
14161 /* Extracting a location number. */
14162 loc,
14163 };
14164
14165 /* Extract a breakpoint or location number (as determined by KIND)
14166 from the string starting at START. TRAILER is a character which
14167 can be found after the number. If you don't want a trailer, use
14168 '\0'. If END_OUT is not NULL, it is set to point after the parsed
14169 string. This always returns a positive integer. */
14170
14171 static int
14172 extract_bp_num (extract_bp_kind kind, const char *start,
14173 int trailer, const char **end_out = NULL)
14174 {
14175 const char *end = start;
14176 int num = get_number_trailer (&end, trailer);
14177 if (num < 0)
14178 error (kind == extract_bp_kind::bp
14179 ? _("Negative breakpoint number '%.*s'")
14180 : _("Negative breakpoint location number '%.*s'"),
14181 int (end - start), start);
14182 if (num == 0)
14183 error (kind == extract_bp_kind::bp
14184 ? _("Bad breakpoint number '%.*s'")
14185 : _("Bad breakpoint location number '%.*s'"),
14186 int (end - start), start);
14187
14188 if (end_out != NULL)
14189 *end_out = end;
14190 return num;
14191 }
14192
14193 /* Extract a breakpoint or location range (as determined by KIND) in
14194 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14195 representing the (inclusive) range. The returned pair's elements
14196 are always positive integers. */
14197
14198 static std::pair<int, int>
14199 extract_bp_or_bp_range (extract_bp_kind kind,
14200 const std::string &arg,
14201 std::string::size_type arg_offset)
14202 {
14203 std::pair<int, int> range;
14204 const char *bp_loc = &arg[arg_offset];
14205 std::string::size_type dash = arg.find ('-', arg_offset);
14206 if (dash != std::string::npos)
14207 {
14208 /* bp_loc is a range (x-z). */
14209 if (arg.length () == dash + 1)
14210 error (kind == extract_bp_kind::bp
14211 ? _("Bad breakpoint number at or near: '%s'")
14212 : _("Bad breakpoint location number at or near: '%s'"),
14213 bp_loc);
14214
14215 const char *end;
14216 const char *start_first = bp_loc;
14217 const char *start_second = &arg[dash + 1];
14218 range.first = extract_bp_num (kind, start_first, '-');
14219 range.second = extract_bp_num (kind, start_second, '\0', &end);
14220
14221 if (range.first > range.second)
14222 error (kind == extract_bp_kind::bp
14223 ? _("Inverted breakpoint range at '%.*s'")
14224 : _("Inverted breakpoint location range at '%.*s'"),
14225 int (end - start_first), start_first);
14226 }
14227 else
14228 {
14229 /* bp_loc is a single value. */
14230 range.first = extract_bp_num (kind, bp_loc, '\0');
14231 range.second = range.first;
14232 }
14233 return range;
14234 }
14235
14236 /* Extract the breakpoint/location range specified by ARG. Returns
14237 the breakpoint range in BP_NUM_RANGE, and the location range in
14238 BP_LOC_RANGE.
14239
14240 ARG may be in any of the following forms:
14241
14242 x where 'x' is a breakpoint number.
14243 x-y where 'x' and 'y' specify a breakpoint numbers range.
14244 x.y where 'x' is a breakpoint number and 'y' a location number.
14245 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14246 location number range.
14247 */
14248
14249 static void
14250 extract_bp_number_and_location (const std::string &arg,
14251 std::pair<int, int> &bp_num_range,
14252 std::pair<int, int> &bp_loc_range)
14253 {
14254 std::string::size_type dot = arg.find ('.');
14255
14256 if (dot != std::string::npos)
14257 {
14258 /* Handle 'x.y' and 'x.y-z' cases. */
14259
14260 if (arg.length () == dot + 1 || dot == 0)
14261 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
14262
14263 bp_num_range.first
14264 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14265 bp_num_range.second = bp_num_range.first;
14266
14267 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14268 arg, dot + 1);
14269 }
14270 else
14271 {
14272 /* Handle x and x-y cases. */
14273
14274 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
14275 bp_loc_range.first = 0;
14276 bp_loc_range.second = 0;
14277 }
14278 }
14279
14280 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14281 specifies whether to enable or disable. */
14282
14283 static void
14284 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14285 {
14286 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14287 if (loc != NULL)
14288 {
14289 if (loc->disabled_by_cond && enable)
14290 error (_("Breakpoint %d's condition is invalid at location %d, "
14291 "cannot enable."), bp_num, loc_num);
14292
14293 if (loc->enabled != enable)
14294 {
14295 loc->enabled = enable;
14296 mark_breakpoint_location_modified (loc);
14297 }
14298 if (target_supports_enable_disable_tracepoint ()
14299 && current_trace_status ()->running && loc->owner
14300 && is_tracepoint (loc->owner))
14301 target_disable_tracepoint (loc);
14302 }
14303 update_global_location_list (UGLL_DONT_INSERT);
14304
14305 gdb::observers::breakpoint_modified.notify (loc->owner);
14306 }
14307
14308 /* Enable or disable a range of breakpoint locations. BP_NUM is the
14309 number of the breakpoint, and BP_LOC_RANGE specifies the
14310 (inclusive) range of location numbers of that breakpoint to
14311 enable/disable. ENABLE specifies whether to enable or disable the
14312 location. */
14313
14314 static void
14315 enable_disable_breakpoint_location_range (int bp_num,
14316 std::pair<int, int> &bp_loc_range,
14317 bool enable)
14318 {
14319 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14320 enable_disable_bp_num_loc (bp_num, i, enable);
14321 }
14322
14323 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14324 If from_tty is nonzero, it prints a message to that effect,
14325 which ends with a period (no newline). */
14326
14327 void
14328 disable_breakpoint (struct breakpoint *bpt)
14329 {
14330 /* Never disable a watchpoint scope breakpoint; we want to
14331 hit them when we leave scope so we can delete both the
14332 watchpoint and its scope breakpoint at that time. */
14333 if (bpt->type == bp_watchpoint_scope)
14334 return;
14335
14336 bpt->enable_state = bp_disabled;
14337
14338 /* Mark breakpoint locations modified. */
14339 mark_breakpoint_modified (bpt);
14340
14341 if (target_supports_enable_disable_tracepoint ()
14342 && current_trace_status ()->running && is_tracepoint (bpt))
14343 {
14344 for (bp_location *location : bpt->locations ())
14345 target_disable_tracepoint (location);
14346 }
14347
14348 update_global_location_list (UGLL_DONT_INSERT);
14349
14350 gdb::observers::breakpoint_modified.notify (bpt);
14351 }
14352
14353 /* Enable or disable the breakpoint(s) or breakpoint location(s)
14354 specified in ARGS. ARGS may be in any of the formats handled by
14355 extract_bp_number_and_location. ENABLE specifies whether to enable
14356 or disable the breakpoints/locations. */
14357
14358 static void
14359 enable_disable_command (const char *args, int from_tty, bool enable)
14360 {
14361 if (args == 0)
14362 {
14363 for (breakpoint *bpt : all_breakpoints ())
14364 if (user_breakpoint_p (bpt))
14365 {
14366 if (enable)
14367 enable_breakpoint (bpt);
14368 else
14369 disable_breakpoint (bpt);
14370 }
14371 }
14372 else
14373 {
14374 std::string num = extract_arg (&args);
14375
14376 while (!num.empty ())
14377 {
14378 std::pair<int, int> bp_num_range, bp_loc_range;
14379
14380 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14381
14382 if (bp_loc_range.first == bp_loc_range.second
14383 && bp_loc_range.first == 0)
14384 {
14385 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14386 map_breakpoint_number_range (bp_num_range,
14387 enable
14388 ? enable_breakpoint
14389 : disable_breakpoint);
14390 }
14391 else
14392 {
14393 /* Handle breakpoint ids with formats 'x.y' or
14394 'x.y-z'. */
14395 enable_disable_breakpoint_location_range
14396 (bp_num_range.first, bp_loc_range, enable);
14397 }
14398 num = extract_arg (&args);
14399 }
14400 }
14401 }
14402
14403 /* The disable command disables the specified breakpoints/locations
14404 (or all defined breakpoints) so they're no longer effective in
14405 stopping the inferior. ARGS may be in any of the forms defined in
14406 extract_bp_number_and_location. */
14407
14408 static void
14409 disable_command (const char *args, int from_tty)
14410 {
14411 enable_disable_command (args, from_tty, false);
14412 }
14413
14414 static void
14415 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14416 int count)
14417 {
14418 int target_resources_ok;
14419
14420 if (bpt->type == bp_hardware_breakpoint)
14421 {
14422 int i;
14423 i = hw_breakpoint_used_count ();
14424 target_resources_ok =
14425 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14426 i + 1, 0);
14427 if (target_resources_ok == 0)
14428 error (_("No hardware breakpoint support in the target."));
14429 else if (target_resources_ok < 0)
14430 error (_("Hardware breakpoints used exceeds limit."));
14431 }
14432
14433 if (is_watchpoint (bpt))
14434 {
14435 /* Initialize it just to avoid a GCC false warning. */
14436 enum enable_state orig_enable_state = bp_disabled;
14437
14438 try
14439 {
14440 struct watchpoint *w = (struct watchpoint *) bpt;
14441
14442 orig_enable_state = bpt->enable_state;
14443 bpt->enable_state = bp_enabled;
14444 update_watchpoint (w, 1 /* reparse */);
14445 }
14446 catch (const gdb_exception &e)
14447 {
14448 bpt->enable_state = orig_enable_state;
14449 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14450 bpt->number);
14451 return;
14452 }
14453 }
14454
14455 bpt->enable_state = bp_enabled;
14456
14457 /* Mark breakpoint locations modified. */
14458 mark_breakpoint_modified (bpt);
14459
14460 if (target_supports_enable_disable_tracepoint ()
14461 && current_trace_status ()->running && is_tracepoint (bpt))
14462 {
14463 for (bp_location *location : bpt->locations ())
14464 target_enable_tracepoint (location);
14465 }
14466
14467 bpt->disposition = disposition;
14468 bpt->enable_count = count;
14469 update_global_location_list (UGLL_MAY_INSERT);
14470
14471 gdb::observers::breakpoint_modified.notify (bpt);
14472 }
14473
14474
14475 void
14476 enable_breakpoint (struct breakpoint *bpt)
14477 {
14478 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14479 }
14480
14481 /* The enable command enables the specified breakpoints/locations (or
14482 all defined breakpoints) so they once again become (or continue to
14483 be) effective in stopping the inferior. ARGS may be in any of the
14484 forms defined in extract_bp_number_and_location. */
14485
14486 static void
14487 enable_command (const char *args, int from_tty)
14488 {
14489 enable_disable_command (args, from_tty, true);
14490 }
14491
14492 static void
14493 enable_once_command (const char *args, int from_tty)
14494 {
14495 map_breakpoint_numbers
14496 (args, [&] (breakpoint *b)
14497 {
14498 iterate_over_related_breakpoints
14499 (b, [&] (breakpoint *bpt)
14500 {
14501 enable_breakpoint_disp (bpt, disp_disable, 1);
14502 });
14503 });
14504 }
14505
14506 static void
14507 enable_count_command (const char *args, int from_tty)
14508 {
14509 int count;
14510
14511 if (args == NULL)
14512 error_no_arg (_("hit count"));
14513
14514 count = get_number (&args);
14515
14516 map_breakpoint_numbers
14517 (args, [&] (breakpoint *b)
14518 {
14519 iterate_over_related_breakpoints
14520 (b, [&] (breakpoint *bpt)
14521 {
14522 enable_breakpoint_disp (bpt, disp_disable, count);
14523 });
14524 });
14525 }
14526
14527 static void
14528 enable_delete_command (const char *args, int from_tty)
14529 {
14530 map_breakpoint_numbers
14531 (args, [&] (breakpoint *b)
14532 {
14533 iterate_over_related_breakpoints
14534 (b, [&] (breakpoint *bpt)
14535 {
14536 enable_breakpoint_disp (bpt, disp_del, 1);
14537 });
14538 });
14539 }
14540 \f
14541 /* Invalidate last known value of any hardware watchpoint if
14542 the memory which that value represents has been written to by
14543 GDB itself. */
14544
14545 static void
14546 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14547 CORE_ADDR addr, ssize_t len,
14548 const bfd_byte *data)
14549 {
14550 for (breakpoint *bp : all_breakpoints ())
14551 if (bp->enable_state == bp_enabled
14552 && bp->type == bp_hardware_watchpoint)
14553 {
14554 struct watchpoint *wp = (struct watchpoint *) bp;
14555
14556 if (wp->val_valid && wp->val != nullptr)
14557 {
14558 for (bp_location *loc : bp->locations ())
14559 if (loc->loc_type == bp_loc_hardware_watchpoint
14560 && loc->address + loc->length > addr
14561 && addr + len > loc->address)
14562 {
14563 wp->val = NULL;
14564 wp->val_valid = false;
14565 }
14566 }
14567 }
14568 }
14569
14570 /* Create and insert a breakpoint for software single step. */
14571
14572 void
14573 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14574 const address_space *aspace,
14575 CORE_ADDR next_pc)
14576 {
14577 struct thread_info *tp = inferior_thread ();
14578 struct symtab_and_line sal;
14579 CORE_ADDR pc = next_pc;
14580
14581 if (tp->control.single_step_breakpoints == NULL)
14582 {
14583 tp->control.single_step_breakpoints
14584 = new_single_step_breakpoint (tp->global_num, gdbarch);
14585 }
14586
14587 sal = find_pc_line (pc, 0);
14588 sal.pc = pc;
14589 sal.section = find_pc_overlay (pc);
14590 sal.explicit_pc = 1;
14591 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14592
14593 update_global_location_list (UGLL_INSERT);
14594 }
14595
14596 /* Insert single step breakpoints according to the current state. */
14597
14598 int
14599 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14600 {
14601 struct regcache *regcache = get_current_regcache ();
14602 std::vector<CORE_ADDR> next_pcs;
14603
14604 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14605
14606 if (!next_pcs.empty ())
14607 {
14608 struct frame_info *frame = get_current_frame ();
14609 const address_space *aspace = get_frame_address_space (frame);
14610
14611 for (CORE_ADDR pc : next_pcs)
14612 insert_single_step_breakpoint (gdbarch, aspace, pc);
14613
14614 return 1;
14615 }
14616 else
14617 return 0;
14618 }
14619
14620 /* See breakpoint.h. */
14621
14622 int
14623 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14624 const address_space *aspace,
14625 CORE_ADDR pc)
14626 {
14627 for (bp_location *loc : bp->locations ())
14628 if (loc->inserted
14629 && breakpoint_location_address_match (loc, aspace, pc))
14630 return 1;
14631
14632 return 0;
14633 }
14634
14635 /* Check whether a software single-step breakpoint is inserted at
14636 PC. */
14637
14638 int
14639 single_step_breakpoint_inserted_here_p (const address_space *aspace,
14640 CORE_ADDR pc)
14641 {
14642 for (breakpoint *bpt : all_breakpoints ())
14643 {
14644 if (bpt->type == bp_single_step
14645 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14646 return 1;
14647 }
14648 return 0;
14649 }
14650
14651 /* Tracepoint-specific operations. */
14652
14653 /* Set tracepoint count to NUM. */
14654 static void
14655 set_tracepoint_count (int num)
14656 {
14657 tracepoint_count = num;
14658 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14659 }
14660
14661 static void
14662 trace_command (const char *arg, int from_tty)
14663 {
14664 event_location_up location = string_to_event_location (&arg,
14665 current_language);
14666 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
14667 (location.get (), true /* is_tracepoint */);
14668
14669 create_breakpoint (get_current_arch (),
14670 location.get (),
14671 NULL, 0, arg, false, 1 /* parse arg */,
14672 0 /* tempflag */,
14673 bp_tracepoint /* type_wanted */,
14674 0 /* Ignore count */,
14675 pending_break_support,
14676 ops,
14677 from_tty,
14678 1 /* enabled */,
14679 0 /* internal */, 0);
14680 }
14681
14682 static void
14683 ftrace_command (const char *arg, int from_tty)
14684 {
14685 event_location_up location = string_to_event_location (&arg,
14686 current_language);
14687 create_breakpoint (get_current_arch (),
14688 location.get (),
14689 NULL, 0, arg, false, 1 /* parse arg */,
14690 0 /* tempflag */,
14691 bp_fast_tracepoint /* type_wanted */,
14692 0 /* Ignore count */,
14693 pending_break_support,
14694 &tracepoint_breakpoint_ops,
14695 from_tty,
14696 1 /* enabled */,
14697 0 /* internal */, 0);
14698 }
14699
14700 /* strace command implementation. Creates a static tracepoint. */
14701
14702 static void
14703 strace_command (const char *arg, int from_tty)
14704 {
14705 struct breakpoint_ops *ops;
14706 event_location_up location;
14707
14708 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14709 or with a normal static tracepoint. */
14710 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14711 {
14712 ops = &strace_marker_breakpoint_ops;
14713 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
14714 }
14715 else
14716 {
14717 ops = &tracepoint_breakpoint_ops;
14718 location = string_to_event_location (&arg, current_language);
14719 }
14720
14721 create_breakpoint (get_current_arch (),
14722 location.get (),
14723 NULL, 0, arg, false, 1 /* parse arg */,
14724 0 /* tempflag */,
14725 bp_static_tracepoint /* type_wanted */,
14726 0 /* Ignore count */,
14727 pending_break_support,
14728 ops,
14729 from_tty,
14730 1 /* enabled */,
14731 0 /* internal */, 0);
14732 }
14733
14734 /* Set up a fake reader function that gets command lines from a linked
14735 list that was acquired during tracepoint uploading. */
14736
14737 static struct uploaded_tp *this_utp;
14738 static int next_cmd;
14739
14740 static char *
14741 read_uploaded_action (void)
14742 {
14743 char *rslt = nullptr;
14744
14745 if (next_cmd < this_utp->cmd_strings.size ())
14746 {
14747 rslt = this_utp->cmd_strings[next_cmd].get ();
14748 next_cmd++;
14749 }
14750
14751 return rslt;
14752 }
14753
14754 /* Given information about a tracepoint as recorded on a target (which
14755 can be either a live system or a trace file), attempt to create an
14756 equivalent GDB tracepoint. This is not a reliable process, since
14757 the target does not necessarily have all the information used when
14758 the tracepoint was originally defined. */
14759
14760 struct tracepoint *
14761 create_tracepoint_from_upload (struct uploaded_tp *utp)
14762 {
14763 const char *addr_str;
14764 char small_buf[100];
14765 struct tracepoint *tp;
14766
14767 if (utp->at_string)
14768 addr_str = utp->at_string.get ();
14769 else
14770 {
14771 /* In the absence of a source location, fall back to raw
14772 address. Since there is no way to confirm that the address
14773 means the same thing as when the trace was started, warn the
14774 user. */
14775 warning (_("Uploaded tracepoint %d has no "
14776 "source location, using raw address"),
14777 utp->number);
14778 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14779 addr_str = small_buf;
14780 }
14781
14782 /* There's not much we can do with a sequence of bytecodes. */
14783 if (utp->cond && !utp->cond_string)
14784 warning (_("Uploaded tracepoint %d condition "
14785 "has no source form, ignoring it"),
14786 utp->number);
14787
14788 event_location_up location = string_to_event_location (&addr_str,
14789 current_language);
14790 if (!create_breakpoint (get_current_arch (),
14791 location.get (),
14792 utp->cond_string.get (), -1, addr_str,
14793 false /* force_condition */,
14794 0 /* parse cond/thread */,
14795 0 /* tempflag */,
14796 utp->type /* type_wanted */,
14797 0 /* Ignore count */,
14798 pending_break_support,
14799 &tracepoint_breakpoint_ops,
14800 0 /* from_tty */,
14801 utp->enabled /* enabled */,
14802 0 /* internal */,
14803 CREATE_BREAKPOINT_FLAGS_INSERTED))
14804 return NULL;
14805
14806 /* Get the tracepoint we just created. */
14807 tp = get_tracepoint (tracepoint_count);
14808 gdb_assert (tp != NULL);
14809
14810 if (utp->pass > 0)
14811 {
14812 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14813 tp->number);
14814
14815 trace_pass_command (small_buf, 0);
14816 }
14817
14818 /* If we have uploaded versions of the original commands, set up a
14819 special-purpose "reader" function and call the usual command line
14820 reader, then pass the result to the breakpoint command-setting
14821 function. */
14822 if (!utp->cmd_strings.empty ())
14823 {
14824 counted_command_line cmd_list;
14825
14826 this_utp = utp;
14827 next_cmd = 0;
14828
14829 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
14830
14831 breakpoint_set_commands (tp, std::move (cmd_list));
14832 }
14833 else if (!utp->actions.empty ()
14834 || !utp->step_actions.empty ())
14835 warning (_("Uploaded tracepoint %d actions "
14836 "have no source form, ignoring them"),
14837 utp->number);
14838
14839 /* Copy any status information that might be available. */
14840 tp->hit_count = utp->hit_count;
14841 tp->traceframe_usage = utp->traceframe_usage;
14842
14843 return tp;
14844 }
14845
14846 /* Print information on tracepoint number TPNUM_EXP, or all if
14847 omitted. */
14848
14849 static void
14850 info_tracepoints_command (const char *args, int from_tty)
14851 {
14852 struct ui_out *uiout = current_uiout;
14853 int num_printed;
14854
14855 num_printed = breakpoint_1 (args, false, is_tracepoint);
14856
14857 if (num_printed == 0)
14858 {
14859 if (args == NULL || *args == '\0')
14860 uiout->message ("No tracepoints.\n");
14861 else
14862 uiout->message ("No tracepoint matching '%s'.\n", args);
14863 }
14864
14865 default_collect_info ();
14866 }
14867
14868 /* The 'enable trace' command enables tracepoints.
14869 Not supported by all targets. */
14870 static void
14871 enable_trace_command (const char *args, int from_tty)
14872 {
14873 enable_command (args, from_tty);
14874 }
14875
14876 /* The 'disable trace' command disables tracepoints.
14877 Not supported by all targets. */
14878 static void
14879 disable_trace_command (const char *args, int from_tty)
14880 {
14881 disable_command (args, from_tty);
14882 }
14883
14884 /* Remove a tracepoint (or all if no argument). */
14885 static void
14886 delete_trace_command (const char *arg, int from_tty)
14887 {
14888 dont_repeat ();
14889
14890 if (arg == 0)
14891 {
14892 int breaks_to_delete = 0;
14893
14894 /* Delete all breakpoints if no argument.
14895 Do not delete internal or call-dummy breakpoints, these
14896 have to be deleted with an explicit breakpoint number
14897 argument. */
14898 for (breakpoint *tp : all_tracepoints ())
14899 if (is_tracepoint (tp) && user_breakpoint_p (tp))
14900 {
14901 breaks_to_delete = 1;
14902 break;
14903 }
14904
14905 /* Ask user only if there are some breakpoints to delete. */
14906 if (!from_tty
14907 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14908 {
14909 for (breakpoint *b : all_breakpoints_safe ())
14910 if (is_tracepoint (b) && user_breakpoint_p (b))
14911 delete_breakpoint (b);
14912 }
14913 }
14914 else
14915 map_breakpoint_numbers
14916 (arg, [&] (breakpoint *br)
14917 {
14918 iterate_over_related_breakpoints (br, delete_breakpoint);
14919 });
14920 }
14921
14922 /* Helper function for trace_pass_command. */
14923
14924 static void
14925 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14926 {
14927 tp->pass_count = count;
14928 gdb::observers::breakpoint_modified.notify (tp);
14929 if (from_tty)
14930 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
14931 tp->number, count);
14932 }
14933
14934 /* Set passcount for tracepoint.
14935
14936 First command argument is passcount, second is tracepoint number.
14937 If tracepoint number omitted, apply to most recently defined.
14938 Also accepts special argument "all". */
14939
14940 static void
14941 trace_pass_command (const char *args, int from_tty)
14942 {
14943 struct tracepoint *t1;
14944 ULONGEST count;
14945
14946 if (args == 0 || *args == 0)
14947 error (_("passcount command requires an "
14948 "argument (count + optional TP num)"));
14949
14950 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
14951
14952 args = skip_spaces (args);
14953 if (*args && strncasecmp (args, "all", 3) == 0)
14954 {
14955 args += 3; /* Skip special argument "all". */
14956 if (*args)
14957 error (_("Junk at end of arguments."));
14958
14959 for (breakpoint *b : all_tracepoints ())
14960 {
14961 t1 = (struct tracepoint *) b;
14962 trace_pass_set_count (t1, count, from_tty);
14963 }
14964 }
14965 else if (*args == '\0')
14966 {
14967 t1 = get_tracepoint_by_number (&args, NULL);
14968 if (t1)
14969 trace_pass_set_count (t1, count, from_tty);
14970 }
14971 else
14972 {
14973 number_or_range_parser parser (args);
14974 while (!parser.finished ())
14975 {
14976 t1 = get_tracepoint_by_number (&args, &parser);
14977 if (t1)
14978 trace_pass_set_count (t1, count, from_tty);
14979 }
14980 }
14981 }
14982
14983 struct tracepoint *
14984 get_tracepoint (int num)
14985 {
14986 for (breakpoint *t : all_tracepoints ())
14987 if (t->number == num)
14988 return (struct tracepoint *) t;
14989
14990 return NULL;
14991 }
14992
14993 /* Find the tracepoint with the given target-side number (which may be
14994 different from the tracepoint number after disconnecting and
14995 reconnecting). */
14996
14997 struct tracepoint *
14998 get_tracepoint_by_number_on_target (int num)
14999 {
15000 for (breakpoint *b : all_tracepoints ())
15001 {
15002 struct tracepoint *t = (struct tracepoint *) b;
15003
15004 if (t->number_on_target == num)
15005 return t;
15006 }
15007
15008 return NULL;
15009 }
15010
15011 /* Utility: parse a tracepoint number and look it up in the list.
15012 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15013 If the argument is missing, the most recent tracepoint
15014 (tracepoint_count) is returned. */
15015
15016 struct tracepoint *
15017 get_tracepoint_by_number (const char **arg,
15018 number_or_range_parser *parser)
15019 {
15020 int tpnum;
15021 const char *instring = arg == NULL ? NULL : *arg;
15022
15023 if (parser != NULL)
15024 {
15025 gdb_assert (!parser->finished ());
15026 tpnum = parser->get_number ();
15027 }
15028 else if (arg == NULL || *arg == NULL || ! **arg)
15029 tpnum = tracepoint_count;
15030 else
15031 tpnum = get_number (arg);
15032
15033 if (tpnum <= 0)
15034 {
15035 if (instring && *instring)
15036 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15037 instring);
15038 else
15039 printf_filtered (_("No previous tracepoint\n"));
15040 return NULL;
15041 }
15042
15043 for (breakpoint *t : all_tracepoints ())
15044 if (t->number == tpnum)
15045 {
15046 return (struct tracepoint *) t;
15047 }
15048
15049 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15050 return NULL;
15051 }
15052
15053 void
15054 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15055 {
15056 if (b->thread != -1)
15057 fprintf_unfiltered (fp, " thread %d", b->thread);
15058
15059 if (b->task != 0)
15060 fprintf_unfiltered (fp, " task %d", b->task);
15061
15062 fprintf_unfiltered (fp, "\n");
15063 }
15064
15065 /* Save information on user settable breakpoints (watchpoints, etc) to
15066 a new script file named FILENAME. If FILTER is non-NULL, call it
15067 on each breakpoint and only include the ones for which it returns
15068 true. */
15069
15070 static void
15071 save_breakpoints (const char *filename, int from_tty,
15072 bool (*filter) (const struct breakpoint *))
15073 {
15074 int any = 0;
15075 int extra_trace_bits = 0;
15076
15077 if (filename == 0 || *filename == 0)
15078 error (_("Argument required (file name in which to save)"));
15079
15080 /* See if we have anything to save. */
15081 for (breakpoint *tp : all_breakpoints ())
15082 {
15083 /* Skip internal and momentary breakpoints. */
15084 if (!user_breakpoint_p (tp))
15085 continue;
15086
15087 /* If we have a filter, only save the breakpoints it accepts. */
15088 if (filter && !filter (tp))
15089 continue;
15090
15091 any = 1;
15092
15093 if (is_tracepoint (tp))
15094 {
15095 extra_trace_bits = 1;
15096
15097 /* We can stop searching. */
15098 break;
15099 }
15100 }
15101
15102 if (!any)
15103 {
15104 warning (_("Nothing to save."));
15105 return;
15106 }
15107
15108 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
15109
15110 stdio_file fp;
15111
15112 if (!fp.open (expanded_filename.get (), "w"))
15113 error (_("Unable to open file '%s' for saving (%s)"),
15114 expanded_filename.get (), safe_strerror (errno));
15115
15116 if (extra_trace_bits)
15117 save_trace_state_variables (&fp);
15118
15119 for (breakpoint *tp : all_breakpoints ())
15120 {
15121 /* Skip internal and momentary breakpoints. */
15122 if (!user_breakpoint_p (tp))
15123 continue;
15124
15125 /* If we have a filter, only save the breakpoints it accepts. */
15126 if (filter && !filter (tp))
15127 continue;
15128
15129 tp->ops->print_recreate (tp, &fp);
15130
15131 /* Note, we can't rely on tp->number for anything, as we can't
15132 assume the recreated breakpoint numbers will match. Use $bpnum
15133 instead. */
15134
15135 if (tp->cond_string)
15136 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15137
15138 if (tp->ignore_count)
15139 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15140
15141 if (tp->type != bp_dprintf && tp->commands)
15142 {
15143 fp.puts (" commands\n");
15144
15145 current_uiout->redirect (&fp);
15146 try
15147 {
15148 print_command_lines (current_uiout, tp->commands.get (), 2);
15149 }
15150 catch (const gdb_exception &ex)
15151 {
15152 current_uiout->redirect (NULL);
15153 throw;
15154 }
15155
15156 current_uiout->redirect (NULL);
15157 fp.puts (" end\n");
15158 }
15159
15160 if (tp->enable_state == bp_disabled)
15161 fp.puts ("disable $bpnum\n");
15162
15163 /* If this is a multi-location breakpoint, check if the locations
15164 should be individually disabled. Watchpoint locations are
15165 special, and not user visible. */
15166 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15167 {
15168 int n = 1;
15169
15170 for (bp_location *loc : tp->locations ())
15171 {
15172 if (!loc->enabled)
15173 fp.printf ("disable $bpnum.%d\n", n);
15174
15175 n++;
15176 }
15177 }
15178 }
15179
15180 if (extra_trace_bits && *default_collect)
15181 fp.printf ("set default-collect %s\n", default_collect);
15182
15183 if (from_tty)
15184 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
15185 }
15186
15187 /* The `save breakpoints' command. */
15188
15189 static void
15190 save_breakpoints_command (const char *args, int from_tty)
15191 {
15192 save_breakpoints (args, from_tty, NULL);
15193 }
15194
15195 /* The `save tracepoints' command. */
15196
15197 static void
15198 save_tracepoints_command (const char *args, int from_tty)
15199 {
15200 save_breakpoints (args, from_tty, is_tracepoint);
15201 }
15202
15203 \f
15204 /* This help string is used to consolidate all the help string for specifying
15205 locations used by several commands. */
15206
15207 #define LOCATION_HELP_STRING \
15208 "Linespecs are colon-separated lists of location parameters, such as\n\
15209 source filename, function name, label name, and line number.\n\
15210 Example: To specify the start of a label named \"the_top\" in the\n\
15211 function \"fact\" in the file \"factorial.c\", use\n\
15212 \"factorial.c:fact:the_top\".\n\
15213 \n\
15214 Address locations begin with \"*\" and specify an exact address in the\n\
15215 program. Example: To specify the fourth byte past the start function\n\
15216 \"main\", use \"*main + 4\".\n\
15217 \n\
15218 Explicit locations are similar to linespecs but use an option/argument\n\
15219 syntax to specify location parameters.\n\
15220 Example: To specify the start of the label named \"the_top\" in the\n\
15221 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15222 -function fact -label the_top\".\n\
15223 \n\
15224 By default, a specified function is matched against the program's\n\
15225 functions in all scopes. For C++, this means in all namespaces and\n\
15226 classes. For Ada, this means in all packages. E.g., in C++,\n\
15227 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15228 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
15229 specified name as a complete fully-qualified name instead."
15230
15231 /* This help string is used for the break, hbreak, tbreak and thbreak
15232 commands. It is defined as a macro to prevent duplication.
15233 COMMAND should be a string constant containing the name of the
15234 command. */
15235
15236 #define BREAK_ARGS_HELP(command) \
15237 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
15238 \t[-force-condition] [if CONDITION]\n\
15239 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15240 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15241 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15242 `-probe-dtrace' (for a DTrace probe).\n\
15243 LOCATION may be a linespec, address, or explicit location as described\n\
15244 below.\n\
15245 \n\
15246 With no LOCATION, uses current execution address of the selected\n\
15247 stack frame. This is useful for breaking on return to a stack frame.\n\
15248 \n\
15249 THREADNUM is the number from \"info threads\".\n\
15250 CONDITION is a boolean expression.\n\
15251 \n\
15252 With the \"-force-condition\" flag, the condition is defined even when\n\
15253 it is invalid for all current locations.\n\
15254 \n" LOCATION_HELP_STRING "\n\n\
15255 Multiple breakpoints at one place are permitted, and useful if their\n\
15256 conditions are different.\n\
15257 \n\
15258 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15259
15260 /* List of subcommands for "catch". */
15261 static struct cmd_list_element *catch_cmdlist;
15262
15263 /* List of subcommands for "tcatch". */
15264 static struct cmd_list_element *tcatch_cmdlist;
15265
15266 void
15267 add_catch_command (const char *name, const char *docstring,
15268 cmd_const_sfunc_ftype *sfunc,
15269 completer_ftype *completer,
15270 void *user_data_catch,
15271 void *user_data_tcatch)
15272 {
15273 struct cmd_list_element *command;
15274
15275 command = add_cmd (name, class_breakpoint, docstring,
15276 &catch_cmdlist);
15277 set_cmd_sfunc (command, sfunc);
15278 set_cmd_context (command, user_data_catch);
15279 set_cmd_completer (command, completer);
15280
15281 command = add_cmd (name, class_breakpoint, docstring,
15282 &tcatch_cmdlist);
15283 set_cmd_sfunc (command, sfunc);
15284 set_cmd_context (command, user_data_tcatch);
15285 set_cmd_completer (command, completer);
15286 }
15287
15288 struct breakpoint *
15289 iterate_over_breakpoints (gdb::function_view<bool (breakpoint *)> callback)
15290 {
15291 for (breakpoint *b : all_breakpoints_safe ())
15292 if (callback (b))
15293 return b;
15294
15295 return NULL;
15296 }
15297
15298 /* Zero if any of the breakpoint's locations could be a location where
15299 functions have been inlined, nonzero otherwise. */
15300
15301 static int
15302 is_non_inline_function (struct breakpoint *b)
15303 {
15304 /* The shared library event breakpoint is set on the address of a
15305 non-inline function. */
15306 if (b->type == bp_shlib_event)
15307 return 1;
15308
15309 return 0;
15310 }
15311
15312 /* Nonzero if the specified PC cannot be a location where functions
15313 have been inlined. */
15314
15315 int
15316 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
15317 const struct target_waitstatus *ws)
15318 {
15319 for (breakpoint *b : all_breakpoints ())
15320 {
15321 if (!is_non_inline_function (b))
15322 continue;
15323
15324 for (bp_location *bl : b->locations ())
15325 {
15326 if (!bl->shlib_disabled
15327 && bpstat_check_location (bl, aspace, pc, ws))
15328 return 1;
15329 }
15330 }
15331
15332 return 0;
15333 }
15334
15335 /* Remove any references to OBJFILE which is going to be freed. */
15336
15337 void
15338 breakpoint_free_objfile (struct objfile *objfile)
15339 {
15340 struct bp_location **locp, *loc;
15341
15342 ALL_BP_LOCATIONS (loc, locp)
15343 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15344 loc->symtab = NULL;
15345 }
15346
15347 void
15348 initialize_breakpoint_ops (void)
15349 {
15350 static int initialized = 0;
15351
15352 struct breakpoint_ops *ops;
15353
15354 if (initialized)
15355 return;
15356 initialized = 1;
15357
15358 /* The breakpoint_ops structure to be inherit by all kinds of
15359 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15360 internal and momentary breakpoints, etc.). */
15361 ops = &bkpt_base_breakpoint_ops;
15362 *ops = base_breakpoint_ops;
15363 ops->re_set = bkpt_re_set;
15364 ops->insert_location = bkpt_insert_location;
15365 ops->remove_location = bkpt_remove_location;
15366 ops->breakpoint_hit = bkpt_breakpoint_hit;
15367 ops->create_sals_from_location = bkpt_create_sals_from_location;
15368 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15369 ops->decode_location = bkpt_decode_location;
15370
15371 /* The breakpoint_ops structure to be used in regular breakpoints. */
15372 ops = &bkpt_breakpoint_ops;
15373 *ops = bkpt_base_breakpoint_ops;
15374 ops->re_set = bkpt_re_set;
15375 ops->resources_needed = bkpt_resources_needed;
15376 ops->print_it = bkpt_print_it;
15377 ops->print_mention = bkpt_print_mention;
15378 ops->print_recreate = bkpt_print_recreate;
15379
15380 /* Ranged breakpoints. */
15381 ops = &ranged_breakpoint_ops;
15382 *ops = bkpt_breakpoint_ops;
15383 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15384 ops->resources_needed = resources_needed_ranged_breakpoint;
15385 ops->print_it = print_it_ranged_breakpoint;
15386 ops->print_one = print_one_ranged_breakpoint;
15387 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15388 ops->print_mention = print_mention_ranged_breakpoint;
15389 ops->print_recreate = print_recreate_ranged_breakpoint;
15390
15391 /* Internal breakpoints. */
15392 ops = &internal_breakpoint_ops;
15393 *ops = bkpt_base_breakpoint_ops;
15394 ops->re_set = internal_bkpt_re_set;
15395 ops->check_status = internal_bkpt_check_status;
15396 ops->print_it = internal_bkpt_print_it;
15397 ops->print_mention = internal_bkpt_print_mention;
15398
15399 /* Momentary breakpoints. */
15400 ops = &momentary_breakpoint_ops;
15401 *ops = bkpt_base_breakpoint_ops;
15402 ops->re_set = momentary_bkpt_re_set;
15403 ops->check_status = momentary_bkpt_check_status;
15404 ops->print_it = momentary_bkpt_print_it;
15405 ops->print_mention = momentary_bkpt_print_mention;
15406
15407 /* Probe breakpoints. */
15408 ops = &bkpt_probe_breakpoint_ops;
15409 *ops = bkpt_breakpoint_ops;
15410 ops->insert_location = bkpt_probe_insert_location;
15411 ops->remove_location = bkpt_probe_remove_location;
15412 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15413 ops->decode_location = bkpt_probe_decode_location;
15414
15415 /* Watchpoints. */
15416 ops = &watchpoint_breakpoint_ops;
15417 *ops = base_breakpoint_ops;
15418 ops->re_set = re_set_watchpoint;
15419 ops->insert_location = insert_watchpoint;
15420 ops->remove_location = remove_watchpoint;
15421 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15422 ops->check_status = check_status_watchpoint;
15423 ops->resources_needed = resources_needed_watchpoint;
15424 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15425 ops->print_it = print_it_watchpoint;
15426 ops->print_mention = print_mention_watchpoint;
15427 ops->print_recreate = print_recreate_watchpoint;
15428 ops->explains_signal = explains_signal_watchpoint;
15429
15430 /* Masked watchpoints. */
15431 ops = &masked_watchpoint_breakpoint_ops;
15432 *ops = watchpoint_breakpoint_ops;
15433 ops->insert_location = insert_masked_watchpoint;
15434 ops->remove_location = remove_masked_watchpoint;
15435 ops->resources_needed = resources_needed_masked_watchpoint;
15436 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15437 ops->print_it = print_it_masked_watchpoint;
15438 ops->print_one_detail = print_one_detail_masked_watchpoint;
15439 ops->print_mention = print_mention_masked_watchpoint;
15440 ops->print_recreate = print_recreate_masked_watchpoint;
15441
15442 /* Tracepoints. */
15443 ops = &tracepoint_breakpoint_ops;
15444 *ops = base_breakpoint_ops;
15445 ops->re_set = tracepoint_re_set;
15446 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15447 ops->print_one_detail = tracepoint_print_one_detail;
15448 ops->print_mention = tracepoint_print_mention;
15449 ops->print_recreate = tracepoint_print_recreate;
15450 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15451 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15452 ops->decode_location = tracepoint_decode_location;
15453
15454 /* Probe tracepoints. */
15455 ops = &tracepoint_probe_breakpoint_ops;
15456 *ops = tracepoint_breakpoint_ops;
15457 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15458 ops->decode_location = tracepoint_probe_decode_location;
15459
15460 /* Static tracepoints with marker (`-m'). */
15461 ops = &strace_marker_breakpoint_ops;
15462 *ops = tracepoint_breakpoint_ops;
15463 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15464 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15465 ops->decode_location = strace_marker_decode_location;
15466
15467 /* Fork catchpoints. */
15468 ops = &catch_fork_breakpoint_ops;
15469 *ops = base_breakpoint_ops;
15470 ops->insert_location = insert_catch_fork;
15471 ops->remove_location = remove_catch_fork;
15472 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15473 ops->print_it = print_it_catch_fork;
15474 ops->print_one = print_one_catch_fork;
15475 ops->print_mention = print_mention_catch_fork;
15476 ops->print_recreate = print_recreate_catch_fork;
15477
15478 /* Vfork catchpoints. */
15479 ops = &catch_vfork_breakpoint_ops;
15480 *ops = base_breakpoint_ops;
15481 ops->insert_location = insert_catch_vfork;
15482 ops->remove_location = remove_catch_vfork;
15483 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15484 ops->print_it = print_it_catch_vfork;
15485 ops->print_one = print_one_catch_vfork;
15486 ops->print_mention = print_mention_catch_vfork;
15487 ops->print_recreate = print_recreate_catch_vfork;
15488
15489 /* Exec catchpoints. */
15490 ops = &catch_exec_breakpoint_ops;
15491 *ops = base_breakpoint_ops;
15492 ops->insert_location = insert_catch_exec;
15493 ops->remove_location = remove_catch_exec;
15494 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15495 ops->print_it = print_it_catch_exec;
15496 ops->print_one = print_one_catch_exec;
15497 ops->print_mention = print_mention_catch_exec;
15498 ops->print_recreate = print_recreate_catch_exec;
15499
15500 /* Solib-related catchpoints. */
15501 ops = &catch_solib_breakpoint_ops;
15502 *ops = base_breakpoint_ops;
15503 ops->insert_location = insert_catch_solib;
15504 ops->remove_location = remove_catch_solib;
15505 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15506 ops->check_status = check_status_catch_solib;
15507 ops->print_it = print_it_catch_solib;
15508 ops->print_one = print_one_catch_solib;
15509 ops->print_mention = print_mention_catch_solib;
15510 ops->print_recreate = print_recreate_catch_solib;
15511
15512 ops = &dprintf_breakpoint_ops;
15513 *ops = bkpt_base_breakpoint_ops;
15514 ops->re_set = dprintf_re_set;
15515 ops->resources_needed = bkpt_resources_needed;
15516 ops->print_it = bkpt_print_it;
15517 ops->print_mention = bkpt_print_mention;
15518 ops->print_recreate = dprintf_print_recreate;
15519 ops->after_condition_true = dprintf_after_condition_true;
15520 ops->breakpoint_hit = dprintf_breakpoint_hit;
15521 }
15522
15523 /* Chain containing all defined "enable breakpoint" subcommands. */
15524
15525 static struct cmd_list_element *enablebreaklist = NULL;
15526
15527 /* See breakpoint.h. */
15528
15529 cmd_list_element *commands_cmd_element = nullptr;
15530
15531 void _initialize_breakpoint ();
15532 void
15533 _initialize_breakpoint ()
15534 {
15535 struct cmd_list_element *c;
15536
15537 initialize_breakpoint_ops ();
15538
15539 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
15540 "breakpoint");
15541 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
15542 "breakpoint");
15543 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
15544 "breakpoint");
15545
15546 breakpoint_chain = 0;
15547 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15548 before a breakpoint is set. */
15549 breakpoint_count = 0;
15550
15551 tracepoint_count = 0;
15552
15553 add_com ("ignore", class_breakpoint, ignore_command, _("\
15554 Set ignore-count of breakpoint number N to COUNT.\n\
15555 Usage is `ignore N COUNT'."));
15556
15557 commands_cmd_element = add_com ("commands", class_breakpoint,
15558 commands_command, _("\
15559 Set commands to be executed when the given breakpoints are hit.\n\
15560 Give a space-separated breakpoint list as argument after \"commands\".\n\
15561 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15562 (e.g. `5-7').\n\
15563 With no argument, the targeted breakpoint is the last one set.\n\
15564 The commands themselves follow starting on the next line.\n\
15565 Type a line containing \"end\" to indicate the end of them.\n\
15566 Give \"silent\" as the first line to make the breakpoint silent;\n\
15567 then no output is printed when it is hit, except what the commands print."));
15568
15569 const auto cc_opts = make_condition_command_options_def_group (nullptr);
15570 static std::string condition_command_help
15571 = gdb::option::build_help (_("\
15572 Specify breakpoint number N to break only if COND is true.\n\
15573 Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
15574 is an expression to be evaluated whenever breakpoint N is reached.\n\
15575 \n\
15576 Options:\n\
15577 %OPTIONS%"), cc_opts);
15578
15579 c = add_com ("condition", class_breakpoint, condition_command,
15580 condition_command_help.c_str ());
15581 set_cmd_completer_handle_brkchars (c, condition_completer);
15582
15583 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15584 Set a temporary breakpoint.\n\
15585 Like \"break\" except the breakpoint is only temporary,\n\
15586 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15587 by using \"enable delete\" on the breakpoint number.\n\
15588 \n"
15589 BREAK_ARGS_HELP ("tbreak")));
15590 set_cmd_completer (c, location_completer);
15591
15592 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15593 Set a hardware assisted breakpoint.\n\
15594 Like \"break\" except the breakpoint requires hardware support,\n\
15595 some target hardware may not have this support.\n\
15596 \n"
15597 BREAK_ARGS_HELP ("hbreak")));
15598 set_cmd_completer (c, location_completer);
15599
15600 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15601 Set a temporary hardware assisted breakpoint.\n\
15602 Like \"hbreak\" except the breakpoint is only temporary,\n\
15603 so it will be deleted when hit.\n\
15604 \n"
15605 BREAK_ARGS_HELP ("thbreak")));
15606 set_cmd_completer (c, location_completer);
15607
15608 cmd_list_element *enable_cmd
15609 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15610 Enable all or some breakpoints.\n\
15611 Usage: enable [BREAKPOINTNUM]...\n\
15612 Give breakpoint numbers (separated by spaces) as arguments.\n\
15613 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15614 This is used to cancel the effect of the \"disable\" command.\n\
15615 With a subcommand you can enable temporarily."),
15616 &enablelist, 1, &cmdlist);
15617
15618 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
15619
15620 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15621 Enable all or some breakpoints.\n\
15622 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
15623 Give breakpoint numbers (separated by spaces) as arguments.\n\
15624 This is used to cancel the effect of the \"disable\" command.\n\
15625 May be abbreviated to simply \"enable\"."),
15626 &enablebreaklist, 1, &enablelist);
15627
15628 add_cmd ("once", no_class, enable_once_command, _("\
15629 Enable some breakpoints for one hit.\n\
15630 Usage: enable breakpoints once BREAKPOINTNUM...\n\
15631 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15632 &enablebreaklist);
15633
15634 add_cmd ("delete", no_class, enable_delete_command, _("\
15635 Enable some breakpoints and delete when hit.\n\
15636 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
15637 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15638 &enablebreaklist);
15639
15640 add_cmd ("count", no_class, enable_count_command, _("\
15641 Enable some breakpoints for COUNT hits.\n\
15642 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
15643 If a breakpoint is hit while enabled in this fashion,\n\
15644 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15645 &enablebreaklist);
15646
15647 add_cmd ("delete", no_class, enable_delete_command, _("\
15648 Enable some breakpoints and delete when hit.\n\
15649 Usage: enable delete BREAKPOINTNUM...\n\
15650 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15651 &enablelist);
15652
15653 add_cmd ("once", no_class, enable_once_command, _("\
15654 Enable some breakpoints for one hit.\n\
15655 Usage: enable once BREAKPOINTNUM...\n\
15656 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15657 &enablelist);
15658
15659 add_cmd ("count", no_class, enable_count_command, _("\
15660 Enable some breakpoints for COUNT hits.\n\
15661 Usage: enable count COUNT BREAKPOINTNUM...\n\
15662 If a breakpoint is hit while enabled in this fashion,\n\
15663 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15664 &enablelist);
15665
15666 cmd_list_element *disable_cmd
15667 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15668 Disable all or some breakpoints.\n\
15669 Usage: disable [BREAKPOINTNUM]...\n\
15670 Arguments are breakpoint numbers with spaces in between.\n\
15671 To disable all breakpoints, give no argument.\n\
15672 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15673 &disablelist, 1, &cmdlist);
15674 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
15675 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
15676
15677 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
15678 Disable all or some breakpoints.\n\
15679 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
15680 Arguments are breakpoint numbers with spaces in between.\n\
15681 To disable all breakpoints, give no argument.\n\
15682 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15683 This command may be abbreviated \"disable\"."),
15684 &disablelist);
15685
15686 cmd_list_element *delete_cmd
15687 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15688 Delete all or some breakpoints.\n\
15689 Usage: delete [BREAKPOINTNUM]...\n\
15690 Arguments are breakpoint numbers with spaces in between.\n\
15691 To delete all breakpoints, give no argument.\n\
15692 \n\
15693 Also a prefix command for deletion of other GDB objects."),
15694 &deletelist, 1, &cmdlist);
15695 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
15696 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
15697
15698 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
15699 Delete all or some breakpoints or auto-display expressions.\n\
15700 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
15701 Arguments are breakpoint numbers with spaces in between.\n\
15702 To delete all breakpoints, give no argument.\n\
15703 This command may be abbreviated \"delete\"."),
15704 &deletelist);
15705
15706 cmd_list_element *clear_cmd
15707 = add_com ("clear", class_breakpoint, clear_command, _("\
15708 Clear breakpoint at specified location.\n\
15709 Argument may be a linespec, explicit, or address location as described below.\n\
15710 \n\
15711 With no argument, clears all breakpoints in the line that the selected frame\n\
15712 is executing in.\n"
15713 "\n" LOCATION_HELP_STRING "\n\n\
15714 See also the \"delete\" command which clears breakpoints by number."));
15715 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
15716
15717 cmd_list_element *break_cmd
15718 = add_com ("break", class_breakpoint, break_command, _("\
15719 Set breakpoint at specified location.\n"
15720 BREAK_ARGS_HELP ("break")));
15721 set_cmd_completer (break_cmd, location_completer);
15722
15723 add_com_alias ("b", break_cmd, class_run, 1);
15724 add_com_alias ("br", break_cmd, class_run, 1);
15725 add_com_alias ("bre", break_cmd, class_run, 1);
15726 add_com_alias ("brea", break_cmd, class_run, 1);
15727
15728 if (dbx_commands)
15729 {
15730 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15731 Break in function/address or break at a line in the current file."),
15732 &stoplist, 1, &cmdlist);
15733 add_cmd ("in", class_breakpoint, stopin_command,
15734 _("Break in function or address."), &stoplist);
15735 add_cmd ("at", class_breakpoint, stopat_command,
15736 _("Break at a line in the current file."), &stoplist);
15737 add_com ("status", class_info, info_breakpoints_command, _("\
15738 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15739 The \"Type\" column indicates one of:\n\
15740 \tbreakpoint - normal breakpoint\n\
15741 \twatchpoint - watchpoint\n\
15742 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15743 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15744 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15745 address and file/line number respectively.\n\
15746 \n\
15747 Convenience variable \"$_\" and default examine address for \"x\"\n\
15748 are set to the address of the last breakpoint listed unless the command\n\
15749 is prefixed with \"server \".\n\n\
15750 Convenience variable \"$bpnum\" contains the number of the last\n\
15751 breakpoint set."));
15752 }
15753
15754 cmd_list_element *info_breakpoints_cmd
15755 = add_info ("breakpoints", info_breakpoints_command, _("\
15756 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15757 The \"Type\" column indicates one of:\n\
15758 \tbreakpoint - normal breakpoint\n\
15759 \twatchpoint - watchpoint\n\
15760 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15761 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15762 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15763 address and file/line number respectively.\n\
15764 \n\
15765 Convenience variable \"$_\" and default examine address for \"x\"\n\
15766 are set to the address of the last breakpoint listed unless the command\n\
15767 is prefixed with \"server \".\n\n\
15768 Convenience variable \"$bpnum\" contains the number of the last\n\
15769 breakpoint set."));
15770
15771 add_info_alias ("b", info_breakpoints_cmd, 1);
15772
15773 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15774 Status of all breakpoints, or breakpoint number NUMBER.\n\
15775 The \"Type\" column indicates one of:\n\
15776 \tbreakpoint - normal breakpoint\n\
15777 \twatchpoint - watchpoint\n\
15778 \tlongjmp - internal breakpoint used to step through longjmp()\n\
15779 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15780 \tuntil - internal breakpoint used by the \"until\" command\n\
15781 \tfinish - internal breakpoint used by the \"finish\" command\n\
15782 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15783 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15784 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15785 address and file/line number respectively.\n\
15786 \n\
15787 Convenience variable \"$_\" and default examine address for \"x\"\n\
15788 are set to the address of the last breakpoint listed unless the command\n\
15789 is prefixed with \"server \".\n\n\
15790 Convenience variable \"$bpnum\" contains the number of the last\n\
15791 breakpoint set."),
15792 &maintenanceinfolist);
15793
15794 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
15795 Set catchpoints to catch events."),
15796 &catch_cmdlist,
15797 0/*allow-unknown*/, &cmdlist);
15798
15799 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
15800 Set temporary catchpoints to catch events."),
15801 &tcatch_cmdlist,
15802 0/*allow-unknown*/, &cmdlist);
15803
15804 add_catch_command ("fork", _("Catch calls to fork."),
15805 catch_fork_command_1,
15806 NULL,
15807 (void *) (uintptr_t) catch_fork_permanent,
15808 (void *) (uintptr_t) catch_fork_temporary);
15809 add_catch_command ("vfork", _("Catch calls to vfork."),
15810 catch_fork_command_1,
15811 NULL,
15812 (void *) (uintptr_t) catch_vfork_permanent,
15813 (void *) (uintptr_t) catch_vfork_temporary);
15814 add_catch_command ("exec", _("Catch calls to exec."),
15815 catch_exec_command_1,
15816 NULL,
15817 CATCH_PERMANENT,
15818 CATCH_TEMPORARY);
15819 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15820 Usage: catch load [REGEX]\n\
15821 If REGEX is given, only stop for libraries matching the regular expression."),
15822 catch_load_command_1,
15823 NULL,
15824 CATCH_PERMANENT,
15825 CATCH_TEMPORARY);
15826 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15827 Usage: catch unload [REGEX]\n\
15828 If REGEX is given, only stop for libraries matching the regular expression."),
15829 catch_unload_command_1,
15830 NULL,
15831 CATCH_PERMANENT,
15832 CATCH_TEMPORARY);
15833
15834 const auto opts = make_watch_options_def_group (nullptr);
15835
15836 static const std::string watch_help = gdb::option::build_help (_("\
15837 Set a watchpoint for EXPRESSION.\n\
15838 Usage: watch [-location] EXPRESSION\n\
15839 \n\
15840 Options:\n\
15841 %OPTIONS%\n\
15842 \n\
15843 A watchpoint stops execution of your program whenever the value of\n\
15844 an expression changes."), opts);
15845 c = add_com ("watch", class_breakpoint, watch_command,
15846 watch_help.c_str ());
15847 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15848
15849 static const std::string rwatch_help = gdb::option::build_help (_("\
15850 Set a read watchpoint for EXPRESSION.\n\
15851 Usage: rwatch [-location] EXPRESSION\n\
15852 \n\
15853 Options:\n\
15854 %OPTIONS%\n\
15855 \n\
15856 A read watchpoint stops execution of your program whenever the value of\n\
15857 an expression is read."), opts);
15858 c = add_com ("rwatch", class_breakpoint, rwatch_command,
15859 rwatch_help.c_str ());
15860 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15861
15862 static const std::string awatch_help = gdb::option::build_help (_("\
15863 Set an access watchpoint for EXPRESSION.\n\
15864 Usage: awatch [-location] EXPRESSION\n\
15865 \n\
15866 Options:\n\
15867 %OPTIONS%\n\
15868 \n\
15869 An access watchpoint stops execution of your program whenever the value\n\
15870 of an expression is either read or written."), opts);
15871 c = add_com ("awatch", class_breakpoint, awatch_command,
15872 awatch_help.c_str ());
15873 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15874
15875 add_info ("watchpoints", info_watchpoints_command, _("\
15876 Status of specified watchpoints (all watchpoints if no argument)."));
15877
15878 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15879 respond to changes - contrary to the description. */
15880 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15881 &can_use_hw_watchpoints, _("\
15882 Set debugger's willingness to use watchpoint hardware."), _("\
15883 Show debugger's willingness to use watchpoint hardware."), _("\
15884 If zero, gdb will not use hardware for new watchpoints, even if\n\
15885 such is available. (However, any hardware watchpoints that were\n\
15886 created before setting this to nonzero, will continue to use watchpoint\n\
15887 hardware.)"),
15888 NULL,
15889 show_can_use_hw_watchpoints,
15890 &setlist, &showlist);
15891
15892 can_use_hw_watchpoints = 1;
15893
15894 /* Tracepoint manipulation commands. */
15895
15896 cmd_list_element *trace_cmd
15897 = add_com ("trace", class_breakpoint, trace_command, _("\
15898 Set a tracepoint at specified location.\n\
15899 \n"
15900 BREAK_ARGS_HELP ("trace") "\n\
15901 Do \"help tracepoints\" for info on other tracepoint commands."));
15902 set_cmd_completer (trace_cmd, location_completer);
15903
15904 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
15905 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
15906 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
15907 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
15908
15909 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
15910 Set a fast tracepoint at specified location.\n\
15911 \n"
15912 BREAK_ARGS_HELP ("ftrace") "\n\
15913 Do \"help tracepoints\" for info on other tracepoint commands."));
15914 set_cmd_completer (c, location_completer);
15915
15916 c = add_com ("strace", class_breakpoint, strace_command, _("\
15917 Set a static tracepoint at location or marker.\n\
15918 \n\
15919 strace [LOCATION] [if CONDITION]\n\
15920 LOCATION may be a linespec, explicit, or address location (described below) \n\
15921 or -m MARKER_ID.\n\n\
15922 If a marker id is specified, probe the marker with that name. With\n\
15923 no LOCATION, uses current execution address of the selected stack frame.\n\
15924 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15925 This collects arbitrary user data passed in the probe point call to the\n\
15926 tracing library. You can inspect it when analyzing the trace buffer,\n\
15927 by printing the $_sdata variable like any other convenience variable.\n\
15928 \n\
15929 CONDITION is a boolean expression.\n\
15930 \n" LOCATION_HELP_STRING "\n\n\
15931 Multiple tracepoints at one place are permitted, and useful if their\n\
15932 conditions are different.\n\
15933 \n\
15934 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15935 Do \"help tracepoints\" for info on other tracepoint commands."));
15936 set_cmd_completer (c, location_completer);
15937
15938 cmd_list_element *info_tracepoints_cmd
15939 = add_info ("tracepoints", info_tracepoints_command, _("\
15940 Status of specified tracepoints (all tracepoints if no argument).\n\
15941 Convenience variable \"$tpnum\" contains the number of the\n\
15942 last tracepoint set."));
15943
15944 add_info_alias ("tp", info_tracepoints_cmd, 1);
15945
15946 cmd_list_element *delete_tracepoints_cmd
15947 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15948 Delete specified tracepoints.\n\
15949 Arguments are tracepoint numbers, separated by spaces.\n\
15950 No argument means delete all tracepoints."),
15951 &deletelist);
15952 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
15953
15954 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15955 Disable specified tracepoints.\n\
15956 Arguments are tracepoint numbers, separated by spaces.\n\
15957 No argument means disable all tracepoints."),
15958 &disablelist);
15959 deprecate_cmd (c, "disable");
15960
15961 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15962 Enable specified tracepoints.\n\
15963 Arguments are tracepoint numbers, separated by spaces.\n\
15964 No argument means enable all tracepoints."),
15965 &enablelist);
15966 deprecate_cmd (c, "enable");
15967
15968 add_com ("passcount", class_trace, trace_pass_command, _("\
15969 Set the passcount for a tracepoint.\n\
15970 The trace will end when the tracepoint has been passed 'count' times.\n\
15971 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15972 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15973
15974 add_basic_prefix_cmd ("save", class_breakpoint,
15975 _("Save breakpoint definitions as a script."),
15976 &save_cmdlist,
15977 0/*allow-unknown*/, &cmdlist);
15978
15979 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15980 Save current breakpoint definitions as a script.\n\
15981 This includes all types of breakpoints (breakpoints, watchpoints,\n\
15982 catchpoints, tracepoints). Use the 'source' command in another debug\n\
15983 session to restore them."),
15984 &save_cmdlist);
15985 set_cmd_completer (c, filename_completer);
15986
15987 cmd_list_element *save_tracepoints_cmd
15988 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
15989 Save current tracepoint definitions as a script.\n\
15990 Use the 'source' command in another debug session to restore them."),
15991 &save_cmdlist);
15992 set_cmd_completer (save_tracepoints_cmd, filename_completer);
15993
15994 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
15995 deprecate_cmd (c, "save tracepoints");
15996
15997 add_basic_prefix_cmd ("breakpoint", class_maintenance, _("\
15998 Breakpoint specific settings.\n\
15999 Configure various breakpoint-specific variables such as\n\
16000 pending breakpoint behavior."),
16001 &breakpoint_set_cmdlist,
16002 0/*allow-unknown*/, &setlist);
16003 add_show_prefix_cmd ("breakpoint", class_maintenance, _("\
16004 Breakpoint specific settings.\n\
16005 Configure various breakpoint-specific variables such as\n\
16006 pending breakpoint behavior."),
16007 &breakpoint_show_cmdlist,
16008 0/*allow-unknown*/, &showlist);
16009
16010 add_setshow_auto_boolean_cmd ("pending", no_class,
16011 &pending_break_support, _("\
16012 Set debugger's behavior regarding pending breakpoints."), _("\
16013 Show debugger's behavior regarding pending breakpoints."), _("\
16014 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16015 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16016 an error. If auto, an unrecognized breakpoint location results in a\n\
16017 user-query to see if a pending breakpoint should be created."),
16018 NULL,
16019 show_pending_break_support,
16020 &breakpoint_set_cmdlist,
16021 &breakpoint_show_cmdlist);
16022
16023 pending_break_support = AUTO_BOOLEAN_AUTO;
16024
16025 add_setshow_boolean_cmd ("auto-hw", no_class,
16026 &automatic_hardware_breakpoints, _("\
16027 Set automatic usage of hardware breakpoints."), _("\
16028 Show automatic usage of hardware breakpoints."), _("\
16029 If set, the debugger will automatically use hardware breakpoints for\n\
16030 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16031 a warning will be emitted for such breakpoints."),
16032 NULL,
16033 show_automatic_hardware_breakpoints,
16034 &breakpoint_set_cmdlist,
16035 &breakpoint_show_cmdlist);
16036
16037 add_setshow_boolean_cmd ("always-inserted", class_support,
16038 &always_inserted_mode, _("\
16039 Set mode for inserting breakpoints."), _("\
16040 Show mode for inserting breakpoints."), _("\
16041 When this mode is on, breakpoints are inserted immediately as soon as\n\
16042 they're created, kept inserted even when execution stops, and removed\n\
16043 only when the user deletes them. When this mode is off (the default),\n\
16044 breakpoints are inserted only when execution continues, and removed\n\
16045 when execution stops."),
16046 NULL,
16047 &show_always_inserted_mode,
16048 &breakpoint_set_cmdlist,
16049 &breakpoint_show_cmdlist);
16050
16051 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16052 condition_evaluation_enums,
16053 &condition_evaluation_mode_1, _("\
16054 Set mode of breakpoint condition evaluation."), _("\
16055 Show mode of breakpoint condition evaluation."), _("\
16056 When this is set to \"host\", breakpoint conditions will be\n\
16057 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16058 breakpoint conditions will be downloaded to the target (if the target\n\
16059 supports such feature) and conditions will be evaluated on the target's side.\n\
16060 If this is set to \"auto\" (default), this will be automatically set to\n\
16061 \"target\" if it supports condition evaluation, otherwise it will\n\
16062 be set to \"host\"."),
16063 &set_condition_evaluation_mode,
16064 &show_condition_evaluation_mode,
16065 &breakpoint_set_cmdlist,
16066 &breakpoint_show_cmdlist);
16067
16068 add_com ("break-range", class_breakpoint, break_range_command, _("\
16069 Set a breakpoint for an address range.\n\
16070 break-range START-LOCATION, END-LOCATION\n\
16071 where START-LOCATION and END-LOCATION can be one of the following:\n\
16072 LINENUM, for that line in the current file,\n\
16073 FILE:LINENUM, for that line in that file,\n\
16074 +OFFSET, for that number of lines after the current line\n\
16075 or the start of the range\n\
16076 FUNCTION, for the first line in that function,\n\
16077 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16078 *ADDRESS, for the instruction at that address.\n\
16079 \n\
16080 The breakpoint will stop execution of the inferior whenever it executes\n\
16081 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16082 range (including START-LOCATION and END-LOCATION)."));
16083
16084 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16085 Set a dynamic printf at specified location.\n\
16086 dprintf location,format string,arg1,arg2,...\n\
16087 location may be a linespec, explicit, or address location.\n"
16088 "\n" LOCATION_HELP_STRING));
16089 set_cmd_completer (c, location_completer);
16090
16091 add_setshow_enum_cmd ("dprintf-style", class_support,
16092 dprintf_style_enums, &dprintf_style, _("\
16093 Set the style of usage for dynamic printf."), _("\
16094 Show the style of usage for dynamic printf."), _("\
16095 This setting chooses how GDB will do a dynamic printf.\n\
16096 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16097 console, as with the \"printf\" command.\n\
16098 If the value is \"call\", the print is done by calling a function in your\n\
16099 program; by default printf(), but you can choose a different function or\n\
16100 output stream by setting dprintf-function and dprintf-channel."),
16101 update_dprintf_commands, NULL,
16102 &setlist, &showlist);
16103
16104 dprintf_function = xstrdup ("printf");
16105 add_setshow_string_cmd ("dprintf-function", class_support,
16106 &dprintf_function, _("\
16107 Set the function to use for dynamic printf."), _("\
16108 Show the function to use for dynamic printf."), NULL,
16109 update_dprintf_commands, NULL,
16110 &setlist, &showlist);
16111
16112 dprintf_channel = xstrdup ("");
16113 add_setshow_string_cmd ("dprintf-channel", class_support,
16114 &dprintf_channel, _("\
16115 Set the channel to use for dynamic printf."), _("\
16116 Show the channel to use for dynamic printf."), NULL,
16117 update_dprintf_commands, NULL,
16118 &setlist, &showlist);
16119
16120 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16121 &disconnected_dprintf, _("\
16122 Set whether dprintf continues after GDB disconnects."), _("\
16123 Show whether dprintf continues after GDB disconnects."), _("\
16124 Use this to let dprintf commands continue to hit and produce output\n\
16125 even if GDB disconnects or detaches from the target."),
16126 NULL,
16127 NULL,
16128 &setlist, &showlist);
16129
16130 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16131 Target agent only formatted printing, like the C \"printf\" function.\n\
16132 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
16133 This supports most C printf format specifications, like %s, %d, etc.\n\
16134 This is useful for formatted output in user-defined commands."));
16135
16136 automatic_hardware_breakpoints = true;
16137
16138 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
16139 "breakpoint");
16140 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
16141 "breakpoint");
16142 }