[gdb] Fix internal-error in process_event_stop_test
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2021 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include <ctype.h>
23 #include "hashtab.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "breakpoint.h"
27 #include "tracepoint.h"
28 #include "gdbtypes.h"
29 #include "expression.h"
30 #include "gdbcore.h"
31 #include "gdbcmd.h"
32 #include "value.h"
33 #include "command.h"
34 #include "inferior.h"
35 #include "infrun.h"
36 #include "gdbthread.h"
37 #include "target.h"
38 #include "language.h"
39 #include "gdb-demangle.h"
40 #include "filenames.h"
41 #include "annotate.h"
42 #include "symfile.h"
43 #include "objfiles.h"
44 #include "source.h"
45 #include "linespec.h"
46 #include "completer.h"
47 #include "ui-out.h"
48 #include "cli/cli-script.h"
49 #include "block.h"
50 #include "solib.h"
51 #include "solist.h"
52 #include "observable.h"
53 #include "memattr.h"
54 #include "ada-lang.h"
55 #include "top.h"
56 #include "valprint.h"
57 #include "jit.h"
58 #include "parser-defs.h"
59 #include "gdb_regex.h"
60 #include "probe.h"
61 #include "cli/cli-utils.h"
62 #include "stack.h"
63 #include "ax-gdb.h"
64 #include "dummy-frame.h"
65 #include "interps.h"
66 #include "gdbsupport/format.h"
67 #include "thread-fsm.h"
68 #include "tid-parse.h"
69 #include "cli/cli-style.h"
70
71 /* readline include files */
72 #include "readline/tilde.h"
73
74 /* readline defines this. */
75 #undef savestring
76
77 #include "mi/mi-common.h"
78 #include "extension.h"
79 #include <algorithm>
80 #include "progspace-and-thread.h"
81 #include "gdbsupport/array-view.h"
82 #include "gdbsupport/gdb_optional.h"
83
84 /* Prototypes for local functions. */
85
86 static void map_breakpoint_numbers (const char *,
87 gdb::function_view<void (breakpoint *)>);
88
89 static void breakpoint_re_set_default (struct breakpoint *);
90
91 static void
92 create_sals_from_location_default (struct event_location *location,
93 struct linespec_result *canonical,
94 enum bptype type_wanted);
95
96 static void create_breakpoints_sal_default (struct gdbarch *,
97 struct linespec_result *,
98 gdb::unique_xmalloc_ptr<char>,
99 gdb::unique_xmalloc_ptr<char>,
100 enum bptype,
101 enum bpdisp, int, int,
102 int,
103 const struct breakpoint_ops *,
104 int, int, int, unsigned);
105
106 static std::vector<symtab_and_line> decode_location_default
107 (struct breakpoint *b, struct event_location *location,
108 struct program_space *search_pspace);
109
110 static int can_use_hardware_watchpoint
111 (const std::vector<value_ref_ptr> &vals);
112
113 static void mention (struct breakpoint *);
114
115 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
116 enum bptype,
117 const struct breakpoint_ops *);
118 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
119 const struct symtab_and_line *);
120
121 /* This function is used in gdbtk sources and thus can not be made
122 static. */
123 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
124 struct symtab_and_line,
125 enum bptype,
126 const struct breakpoint_ops *);
127
128 static struct breakpoint *
129 momentary_breakpoint_from_master (struct breakpoint *orig,
130 enum bptype type,
131 const struct breakpoint_ops *ops,
132 int loc_enabled);
133
134 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
135
136 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
137 CORE_ADDR bpaddr,
138 enum bptype bptype);
139
140 static void describe_other_breakpoints (struct gdbarch *,
141 struct program_space *, CORE_ADDR,
142 struct obj_section *, int);
143
144 static int watchpoint_locations_match (struct bp_location *loc1,
145 struct bp_location *loc2);
146
147 static int breakpoint_locations_match (struct bp_location *loc1,
148 struct bp_location *loc2,
149 bool sw_hw_bps_match = false);
150
151 static int breakpoint_location_address_match (struct bp_location *bl,
152 const struct address_space *aspace,
153 CORE_ADDR addr);
154
155 static int breakpoint_location_address_range_overlap (struct bp_location *,
156 const address_space *,
157 CORE_ADDR, int);
158
159 static int remove_breakpoint (struct bp_location *);
160 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
161
162 static enum print_stop_action print_bp_stop_message (bpstat bs);
163
164 static int hw_breakpoint_used_count (void);
165
166 static int hw_watchpoint_use_count (struct breakpoint *);
167
168 static int hw_watchpoint_used_count_others (struct breakpoint *except,
169 enum bptype type,
170 int *other_type_used);
171
172 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
173 int count);
174
175 static void decref_bp_location (struct bp_location **loc);
176
177 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
178
179 /* update_global_location_list's modes of operation wrt to whether to
180 insert locations now. */
181 enum ugll_insert_mode
182 {
183 /* Don't insert any breakpoint locations into the inferior, only
184 remove already-inserted locations that no longer should be
185 inserted. Functions that delete a breakpoint or breakpoints
186 should specify this mode, so that deleting a breakpoint doesn't
187 have the side effect of inserting the locations of other
188 breakpoints that are marked not-inserted, but should_be_inserted
189 returns true on them.
190
191 This behavior is useful is situations close to tear-down -- e.g.,
192 after an exec, while the target still has execution, but
193 breakpoint shadows of the previous executable image should *NOT*
194 be restored to the new image; or before detaching, where the
195 target still has execution and wants to delete breakpoints from
196 GDB's lists, and all breakpoints had already been removed from
197 the inferior. */
198 UGLL_DONT_INSERT,
199
200 /* May insert breakpoints iff breakpoints_should_be_inserted_now
201 claims breakpoints should be inserted now. */
202 UGLL_MAY_INSERT,
203
204 /* Insert locations now, irrespective of
205 breakpoints_should_be_inserted_now. E.g., say all threads are
206 stopped right now, and the user did "continue". We need to
207 insert breakpoints _before_ resuming the target, but
208 UGLL_MAY_INSERT wouldn't insert them, because
209 breakpoints_should_be_inserted_now returns false at that point,
210 as no thread is running yet. */
211 UGLL_INSERT
212 };
213
214 static void update_global_location_list (enum ugll_insert_mode);
215
216 static void update_global_location_list_nothrow (enum ugll_insert_mode);
217
218 static void insert_breakpoint_locations (void);
219
220 static void trace_pass_command (const char *, int);
221
222 static void set_tracepoint_count (int num);
223
224 static bool is_masked_watchpoint (const struct breakpoint *b);
225
226 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
227
228 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
229 otherwise. */
230
231 static int strace_marker_p (struct breakpoint *b);
232
233 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
234 that are implemented on top of software or hardware breakpoints
235 (user breakpoints, internal and momentary breakpoints, etc.). */
236 static struct breakpoint_ops bkpt_base_breakpoint_ops;
237
238 /* Internal breakpoints class type. */
239 static struct breakpoint_ops internal_breakpoint_ops;
240
241 /* Momentary breakpoints class type. */
242 static struct breakpoint_ops momentary_breakpoint_ops;
243
244 /* The breakpoint_ops structure to be used in regular user created
245 breakpoints. */
246 struct breakpoint_ops bkpt_breakpoint_ops;
247
248 /* Breakpoints set on probes. */
249 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
250
251 /* Tracepoints set on probes. */
252 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
253
254 /* Dynamic printf class type. */
255 struct breakpoint_ops dprintf_breakpoint_ops;
256
257 /* The style in which to perform a dynamic printf. This is a user
258 option because different output options have different tradeoffs;
259 if GDB does the printing, there is better error handling if there
260 is a problem with any of the arguments, but using an inferior
261 function lets you have special-purpose printers and sending of
262 output to the same place as compiled-in print functions. */
263
264 static const char dprintf_style_gdb[] = "gdb";
265 static const char dprintf_style_call[] = "call";
266 static const char dprintf_style_agent[] = "agent";
267 static const char *const dprintf_style_enums[] = {
268 dprintf_style_gdb,
269 dprintf_style_call,
270 dprintf_style_agent,
271 NULL
272 };
273 static const char *dprintf_style = dprintf_style_gdb;
274
275 /* The function to use for dynamic printf if the preferred style is to
276 call into the inferior. The value is simply a string that is
277 copied into the command, so it can be anything that GDB can
278 evaluate to a callable address, not necessarily a function name. */
279
280 static char *dprintf_function;
281
282 /* The channel to use for dynamic printf if the preferred style is to
283 call into the inferior; if a nonempty string, it will be passed to
284 the call as the first argument, with the format string as the
285 second. As with the dprintf function, this can be anything that
286 GDB knows how to evaluate, so in addition to common choices like
287 "stderr", this could be an app-specific expression like
288 "mystreams[curlogger]". */
289
290 static char *dprintf_channel;
291
292 /* True if dprintf commands should continue to operate even if GDB
293 has disconnected. */
294 static bool disconnected_dprintf = true;
295
296 struct command_line *
297 breakpoint_commands (struct breakpoint *b)
298 {
299 return b->commands ? b->commands.get () : NULL;
300 }
301
302 /* Flag indicating that a command has proceeded the inferior past the
303 current breakpoint. */
304
305 static bool breakpoint_proceeded;
306
307 const char *
308 bpdisp_text (enum bpdisp disp)
309 {
310 /* NOTE: the following values are a part of MI protocol and
311 represent values of 'disp' field returned when inferior stops at
312 a breakpoint. */
313 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
314
315 return bpdisps[(int) disp];
316 }
317
318 /* Prototypes for exported functions. */
319 /* If FALSE, gdb will not use hardware support for watchpoints, even
320 if such is available. */
321 static int can_use_hw_watchpoints;
322
323 static void
324 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
325 struct cmd_list_element *c,
326 const char *value)
327 {
328 fprintf_filtered (file,
329 _("Debugger's willingness to use "
330 "watchpoint hardware is %s.\n"),
331 value);
332 }
333
334 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
335 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
336 for unrecognized breakpoint locations.
337 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
338 static enum auto_boolean pending_break_support;
339 static void
340 show_pending_break_support (struct ui_file *file, int from_tty,
341 struct cmd_list_element *c,
342 const char *value)
343 {
344 fprintf_filtered (file,
345 _("Debugger's behavior regarding "
346 "pending breakpoints is %s.\n"),
347 value);
348 }
349
350 /* If true, gdb will automatically use hardware breakpoints for breakpoints
351 set with "break" but falling in read-only memory.
352 If false, gdb will warn about such breakpoints, but won't automatically
353 use hardware breakpoints. */
354 static bool automatic_hardware_breakpoints;
355 static void
356 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
357 struct cmd_list_element *c,
358 const char *value)
359 {
360 fprintf_filtered (file,
361 _("Automatic usage of hardware breakpoints is %s.\n"),
362 value);
363 }
364
365 /* If on, GDB keeps breakpoints inserted even if the inferior is
366 stopped, and immediately inserts any new breakpoints as soon as
367 they're created. If off (default), GDB keeps breakpoints off of
368 the target as long as possible. That is, it delays inserting
369 breakpoints until the next resume, and removes them again when the
370 target fully stops. This is a bit safer in case GDB crashes while
371 processing user input. */
372 static bool always_inserted_mode = false;
373
374 static void
375 show_always_inserted_mode (struct ui_file *file, int from_tty,
376 struct cmd_list_element *c, const char *value)
377 {
378 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
379 value);
380 }
381
382 /* See breakpoint.h. */
383
384 int
385 breakpoints_should_be_inserted_now (void)
386 {
387 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
388 {
389 /* If breakpoints are global, they should be inserted even if no
390 thread under gdb's control is running, or even if there are
391 no threads under GDB's control yet. */
392 return 1;
393 }
394 else
395 {
396 if (always_inserted_mode)
397 {
398 /* The user wants breakpoints inserted even if all threads
399 are stopped. */
400 return 1;
401 }
402
403 for (inferior *inf : all_inferiors ())
404 if (inf->has_execution ()
405 && threads_are_executing (inf->process_target ()))
406 return 1;
407
408 /* Don't remove breakpoints yet if, even though all threads are
409 stopped, we still have events to process. */
410 for (thread_info *tp : all_non_exited_threads ())
411 if (tp->resumed
412 && tp->suspend.waitstatus_pending_p)
413 return 1;
414 }
415 return 0;
416 }
417
418 static const char condition_evaluation_both[] = "host or target";
419
420 /* Modes for breakpoint condition evaluation. */
421 static const char condition_evaluation_auto[] = "auto";
422 static const char condition_evaluation_host[] = "host";
423 static const char condition_evaluation_target[] = "target";
424 static const char *const condition_evaluation_enums[] = {
425 condition_evaluation_auto,
426 condition_evaluation_host,
427 condition_evaluation_target,
428 NULL
429 };
430
431 /* Global that holds the current mode for breakpoint condition evaluation. */
432 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
433
434 /* Global that we use to display information to the user (gets its value from
435 condition_evaluation_mode_1. */
436 static const char *condition_evaluation_mode = condition_evaluation_auto;
437
438 /* Translate a condition evaluation mode MODE into either "host"
439 or "target". This is used mostly to translate from "auto" to the
440 real setting that is being used. It returns the translated
441 evaluation mode. */
442
443 static const char *
444 translate_condition_evaluation_mode (const char *mode)
445 {
446 if (mode == condition_evaluation_auto)
447 {
448 if (target_supports_evaluation_of_breakpoint_conditions ())
449 return condition_evaluation_target;
450 else
451 return condition_evaluation_host;
452 }
453 else
454 return mode;
455 }
456
457 /* Discovers what condition_evaluation_auto translates to. */
458
459 static const char *
460 breakpoint_condition_evaluation_mode (void)
461 {
462 return translate_condition_evaluation_mode (condition_evaluation_mode);
463 }
464
465 /* Return true if GDB should evaluate breakpoint conditions or false
466 otherwise. */
467
468 static int
469 gdb_evaluates_breakpoint_condition_p (void)
470 {
471 const char *mode = breakpoint_condition_evaluation_mode ();
472
473 return (mode == condition_evaluation_host);
474 }
475
476 /* Are we executing breakpoint commands? */
477 static int executing_breakpoint_commands;
478
479 /* Are overlay event breakpoints enabled? */
480 static int overlay_events_enabled;
481
482 /* See description in breakpoint.h. */
483 bool target_exact_watchpoints = false;
484
485 /* Walk the following statement or block through all breakpoints.
486 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
487 current breakpoint. */
488
489 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
490
491 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
492 for (B = breakpoint_chain; \
493 B ? (TMP=B->next, 1): 0; \
494 B = TMP)
495
496 /* Similar iterator for the low-level breakpoints. SAFE variant is
497 not provided so update_global_location_list must not be called
498 while executing the block of ALL_BP_LOCATIONS. */
499
500 #define ALL_BP_LOCATIONS(B,BP_TMP) \
501 for (BP_TMP = bp_locations; \
502 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
503 BP_TMP++)
504
505 /* Iterates through locations with address ADDRESS for the currently selected
506 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
507 to where the loop should start from.
508 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
509 appropriate location to start with. */
510
511 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
512 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
513 BP_LOCP_TMP = BP_LOCP_START; \
514 BP_LOCP_START \
515 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
516 && (*BP_LOCP_TMP)->address == ADDRESS); \
517 BP_LOCP_TMP++)
518
519 /* Iterator for tracepoints only. */
520
521 #define ALL_TRACEPOINTS(B) \
522 for (B = breakpoint_chain; B; B = B->next) \
523 if (is_tracepoint (B))
524
525 /* Chains of all breakpoints defined. */
526
527 static struct breakpoint *breakpoint_chain;
528
529 /* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
530
531 static struct bp_location **bp_locations;
532
533 /* Number of elements of BP_LOCATIONS. */
534
535 static unsigned bp_locations_count;
536
537 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
538 ADDRESS for the current elements of BP_LOCATIONS which get a valid
539 result from bp_location_has_shadow. You can use it for roughly
540 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
541 an address you need to read. */
542
543 static CORE_ADDR bp_locations_placed_address_before_address_max;
544
545 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
546 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
547 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
548 You can use it for roughly limiting the subrange of BP_LOCATIONS to
549 scan for shadow bytes for an address you need to read. */
550
551 static CORE_ADDR bp_locations_shadow_len_after_address_max;
552
553 /* The locations that no longer correspond to any breakpoint, unlinked
554 from the bp_locations array, but for which a hit may still be
555 reported by a target. */
556 static std::vector<bp_location *> moribund_locations;
557
558 /* Number of last breakpoint made. */
559
560 static int breakpoint_count;
561
562 /* The value of `breakpoint_count' before the last command that
563 created breakpoints. If the last (break-like) command created more
564 than one breakpoint, then the difference between BREAKPOINT_COUNT
565 and PREV_BREAKPOINT_COUNT is more than one. */
566 static int prev_breakpoint_count;
567
568 /* Number of last tracepoint made. */
569
570 static int tracepoint_count;
571
572 static struct cmd_list_element *breakpoint_set_cmdlist;
573 static struct cmd_list_element *breakpoint_show_cmdlist;
574 struct cmd_list_element *save_cmdlist;
575
576 /* See declaration at breakpoint.h. */
577
578 struct breakpoint *
579 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
580 void *user_data)
581 {
582 struct breakpoint *b = NULL;
583
584 ALL_BREAKPOINTS (b)
585 {
586 if (func (b, user_data) != 0)
587 break;
588 }
589
590 return b;
591 }
592
593 /* Return whether a breakpoint is an active enabled breakpoint. */
594 static int
595 breakpoint_enabled (struct breakpoint *b)
596 {
597 return (b->enable_state == bp_enabled);
598 }
599
600 /* Set breakpoint count to NUM. */
601
602 static void
603 set_breakpoint_count (int num)
604 {
605 prev_breakpoint_count = breakpoint_count;
606 breakpoint_count = num;
607 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
608 }
609
610 /* Used by `start_rbreak_breakpoints' below, to record the current
611 breakpoint count before "rbreak" creates any breakpoint. */
612 static int rbreak_start_breakpoint_count;
613
614 /* Called at the start an "rbreak" command to record the first
615 breakpoint made. */
616
617 scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
618 {
619 rbreak_start_breakpoint_count = breakpoint_count;
620 }
621
622 /* Called at the end of an "rbreak" command to record the last
623 breakpoint made. */
624
625 scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
626 {
627 prev_breakpoint_count = rbreak_start_breakpoint_count;
628 }
629
630 /* Used in run_command to zero the hit count when a new run starts. */
631
632 void
633 clear_breakpoint_hit_counts (void)
634 {
635 struct breakpoint *b;
636
637 ALL_BREAKPOINTS (b)
638 b->hit_count = 0;
639 }
640
641 \f
642 /* Return the breakpoint with the specified number, or NULL
643 if the number does not refer to an existing breakpoint. */
644
645 struct breakpoint *
646 get_breakpoint (int num)
647 {
648 struct breakpoint *b;
649
650 ALL_BREAKPOINTS (b)
651 if (b->number == num)
652 return b;
653
654 return NULL;
655 }
656
657 \f
658
659 /* Mark locations as "conditions have changed" in case the target supports
660 evaluating conditions on its side. */
661
662 static void
663 mark_breakpoint_modified (struct breakpoint *b)
664 {
665 struct bp_location *loc;
666
667 /* This is only meaningful if the target is
668 evaluating conditions and if the user has
669 opted for condition evaluation on the target's
670 side. */
671 if (gdb_evaluates_breakpoint_condition_p ()
672 || !target_supports_evaluation_of_breakpoint_conditions ())
673 return;
674
675 if (!is_breakpoint (b))
676 return;
677
678 for (loc = b->loc; loc; loc = loc->next)
679 loc->condition_changed = condition_modified;
680 }
681
682 /* Mark location as "conditions have changed" in case the target supports
683 evaluating conditions on its side. */
684
685 static void
686 mark_breakpoint_location_modified (struct bp_location *loc)
687 {
688 /* This is only meaningful if the target is
689 evaluating conditions and if the user has
690 opted for condition evaluation on the target's
691 side. */
692 if (gdb_evaluates_breakpoint_condition_p ()
693 || !target_supports_evaluation_of_breakpoint_conditions ())
694
695 return;
696
697 if (!is_breakpoint (loc->owner))
698 return;
699
700 loc->condition_changed = condition_modified;
701 }
702
703 /* Sets the condition-evaluation mode using the static global
704 condition_evaluation_mode. */
705
706 static void
707 set_condition_evaluation_mode (const char *args, int from_tty,
708 struct cmd_list_element *c)
709 {
710 const char *old_mode, *new_mode;
711
712 if ((condition_evaluation_mode_1 == condition_evaluation_target)
713 && !target_supports_evaluation_of_breakpoint_conditions ())
714 {
715 condition_evaluation_mode_1 = condition_evaluation_mode;
716 warning (_("Target does not support breakpoint condition evaluation.\n"
717 "Using host evaluation mode instead."));
718 return;
719 }
720
721 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
722 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
723
724 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
725 settings was "auto". */
726 condition_evaluation_mode = condition_evaluation_mode_1;
727
728 /* Only update the mode if the user picked a different one. */
729 if (new_mode != old_mode)
730 {
731 struct bp_location *loc, **loc_tmp;
732 /* If the user switched to a different evaluation mode, we
733 need to synch the changes with the target as follows:
734
735 "host" -> "target": Send all (valid) conditions to the target.
736 "target" -> "host": Remove all the conditions from the target.
737 */
738
739 if (new_mode == condition_evaluation_target)
740 {
741 /* Mark everything modified and synch conditions with the
742 target. */
743 ALL_BP_LOCATIONS (loc, loc_tmp)
744 mark_breakpoint_location_modified (loc);
745 }
746 else
747 {
748 /* Manually mark non-duplicate locations to synch conditions
749 with the target. We do this to remove all the conditions the
750 target knows about. */
751 ALL_BP_LOCATIONS (loc, loc_tmp)
752 if (is_breakpoint (loc->owner) && loc->inserted)
753 loc->needs_update = 1;
754 }
755
756 /* Do the update. */
757 update_global_location_list (UGLL_MAY_INSERT);
758 }
759
760 return;
761 }
762
763 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
764 what "auto" is translating to. */
765
766 static void
767 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
768 struct cmd_list_element *c, const char *value)
769 {
770 if (condition_evaluation_mode == condition_evaluation_auto)
771 fprintf_filtered (file,
772 _("Breakpoint condition evaluation "
773 "mode is %s (currently %s).\n"),
774 value,
775 breakpoint_condition_evaluation_mode ());
776 else
777 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
778 value);
779 }
780
781 /* A comparison function for bp_location AP and BP that is used by
782 bsearch. This comparison function only cares about addresses, unlike
783 the more general bp_location_is_less_than function. */
784
785 static int
786 bp_locations_compare_addrs (const void *ap, const void *bp)
787 {
788 const struct bp_location *a = *(const struct bp_location **) ap;
789 const struct bp_location *b = *(const struct bp_location **) bp;
790
791 if (a->address == b->address)
792 return 0;
793 else
794 return ((a->address > b->address) - (a->address < b->address));
795 }
796
797 /* Helper function to skip all bp_locations with addresses
798 less than ADDRESS. It returns the first bp_location that
799 is greater than or equal to ADDRESS. If none is found, just
800 return NULL. */
801
802 static struct bp_location **
803 get_first_locp_gte_addr (CORE_ADDR address)
804 {
805 struct bp_location dummy_loc;
806 struct bp_location *dummy_locp = &dummy_loc;
807 struct bp_location **locp_found = NULL;
808
809 /* Initialize the dummy location's address field. */
810 dummy_loc.address = address;
811
812 /* Find a close match to the first location at ADDRESS. */
813 locp_found = ((struct bp_location **)
814 bsearch (&dummy_locp, bp_locations, bp_locations_count,
815 sizeof (struct bp_location **),
816 bp_locations_compare_addrs));
817
818 /* Nothing was found, nothing left to do. */
819 if (locp_found == NULL)
820 return NULL;
821
822 /* We may have found a location that is at ADDRESS but is not the first in the
823 location's list. Go backwards (if possible) and locate the first one. */
824 while ((locp_found - 1) >= bp_locations
825 && (*(locp_found - 1))->address == address)
826 locp_found--;
827
828 return locp_found;
829 }
830
831 /* Parse COND_STRING in the context of LOC and set as the condition
832 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
833 the number of LOC within its owner. In case of parsing error, mark
834 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
835
836 static void
837 set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
838 int bp_num, int loc_num)
839 {
840 bool has_junk = false;
841 try
842 {
843 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
844 block_for_pc (loc->address), 0);
845 if (*cond_string != 0)
846 has_junk = true;
847 else
848 {
849 loc->cond = std::move (new_exp);
850 if (loc->disabled_by_cond && loc->enabled)
851 printf_filtered (_("Breakpoint %d's condition is now valid at "
852 "location %d, enabling.\n"),
853 bp_num, loc_num);
854
855 loc->disabled_by_cond = false;
856 }
857 }
858 catch (const gdb_exception_error &e)
859 {
860 if (loc->enabled)
861 {
862 /* Warn if a user-enabled location is now becoming disabled-by-cond.
863 BP_NUM is 0 if the breakpoint is being defined for the first
864 time using the "break ... if ..." command, and non-zero if
865 already defined. */
866 if (bp_num != 0)
867 warning (_("failed to validate condition at location %d.%d, "
868 "disabling:\n %s"), bp_num, loc_num, e.what ());
869 else
870 warning (_("failed to validate condition at location %d, "
871 "disabling:\n %s"), loc_num, e.what ());
872 }
873
874 loc->disabled_by_cond = true;
875 }
876
877 if (has_junk)
878 error (_("Garbage '%s' follows condition"), cond_string);
879 }
880
881 void
882 set_breakpoint_condition (struct breakpoint *b, const char *exp,
883 int from_tty, bool force)
884 {
885 if (*exp == 0)
886 {
887 xfree (b->cond_string);
888 b->cond_string = nullptr;
889
890 if (is_watchpoint (b))
891 static_cast<watchpoint *> (b)->cond_exp.reset ();
892 else
893 {
894 int loc_num = 1;
895 for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)
896 {
897 loc->cond.reset ();
898 if (loc->disabled_by_cond && loc->enabled)
899 printf_filtered (_("Breakpoint %d's condition is now valid at "
900 "location %d, enabling.\n"),
901 b->number, loc_num);
902 loc->disabled_by_cond = false;
903 loc_num++;
904
905 /* No need to free the condition agent expression
906 bytecode (if we have one). We will handle this
907 when we go through update_global_location_list. */
908 }
909 }
910
911 if (from_tty)
912 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
913 }
914 else
915 {
916 if (is_watchpoint (b))
917 {
918 innermost_block_tracker tracker;
919 const char *arg = exp;
920 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
921 if (*arg != 0)
922 error (_("Junk at end of expression"));
923 watchpoint *w = static_cast<watchpoint *> (b);
924 w->cond_exp = std::move (new_exp);
925 w->cond_exp_valid_block = tracker.block ();
926 }
927 else
928 {
929 /* Parse and set condition expressions. We make two passes.
930 In the first, we parse the condition string to see if it
931 is valid in at least one location. If so, the condition
932 would be accepted. So we go ahead and set the locations'
933 conditions. In case no valid case is found, we throw
934 the error and the condition string will be rejected.
935 This two-pass approach is taken to avoid setting the
936 state of locations in case of a reject. */
937 for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)
938 {
939 try
940 {
941 const char *arg = exp;
942 parse_exp_1 (&arg, loc->address,
943 block_for_pc (loc->address), 0);
944 if (*arg != 0)
945 error (_("Junk at end of expression"));
946 break;
947 }
948 catch (const gdb_exception_error &e)
949 {
950 /* Condition string is invalid. If this happens to
951 be the last loc, abandon (if not forced) or continue
952 (if forced). */
953 if (loc->next == nullptr && !force)
954 throw;
955 }
956 }
957
958 /* If we reach here, the condition is valid at some locations. */
959 int loc_num = 1;
960 for (bp_location *loc = b->loc; loc != nullptr;
961 loc = loc->next, loc_num++)
962 set_breakpoint_location_condition (exp, loc, b->number, loc_num);
963 }
964
965 /* We know that the new condition parsed successfully. The
966 condition string of the breakpoint can be safely updated. */
967 xfree (b->cond_string);
968 b->cond_string = xstrdup (exp);
969 b->condition_not_parsed = 0;
970 }
971 mark_breakpoint_modified (b);
972
973 gdb::observers::breakpoint_modified.notify (b);
974 }
975
976 /* The options for the "condition" command. */
977
978 struct condition_command_opts
979 {
980 /* For "-force". */
981 bool force_condition = false;
982 };
983
984 static const gdb::option::option_def condition_command_option_defs[] = {
985
986 gdb::option::flag_option_def<condition_command_opts> {
987 "force",
988 [] (condition_command_opts *opts) { return &opts->force_condition; },
989 N_("Set the condition even if it is invalid for all current locations."),
990 },
991
992 };
993
994 /* Create an option_def_group for the "condition" options, with
995 CC_OPTS as context. */
996
997 static inline gdb::option::option_def_group
998 make_condition_command_options_def_group (condition_command_opts *cc_opts)
999 {
1000 return {{condition_command_option_defs}, cc_opts};
1001 }
1002
1003 /* Completion for the "condition" command. */
1004
1005 static void
1006 condition_completer (struct cmd_list_element *cmd,
1007 completion_tracker &tracker,
1008 const char *text, const char * /*word*/)
1009 {
1010 bool has_no_arguments = (*text == '\0');
1011 condition_command_opts cc_opts;
1012 const auto group = make_condition_command_options_def_group (&cc_opts);
1013 if (gdb::option::complete_options
1014 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1015 return;
1016
1017 text = skip_spaces (text);
1018 const char *space = skip_to_space (text);
1019 if (*space == '\0')
1020 {
1021 int len;
1022 struct breakpoint *b;
1023
1024 if (text[0] == '$')
1025 {
1026 tracker.advance_custom_word_point_by (1);
1027 /* We don't support completion of history indices. */
1028 if (!isdigit (text[1]))
1029 complete_internalvar (tracker, &text[1]);
1030 return;
1031 }
1032
1033 /* Suggest the "-force" flag if no arguments are given. If
1034 arguments were passed, they either already include the flag,
1035 or we are beyond the point of suggesting it because it's
1036 positionally the first argument. */
1037 if (has_no_arguments)
1038 gdb::option::complete_on_all_options (tracker, group);
1039
1040 /* We're completing the breakpoint number. */
1041 len = strlen (text);
1042
1043 ALL_BREAKPOINTS (b)
1044 {
1045 char number[50];
1046
1047 xsnprintf (number, sizeof (number), "%d", b->number);
1048
1049 if (strncmp (number, text, len) == 0)
1050 tracker.add_completion (make_unique_xstrdup (number));
1051 }
1052
1053 return;
1054 }
1055
1056 /* We're completing the expression part. Skip the breakpoint num. */
1057 const char *exp_start = skip_spaces (space);
1058 tracker.advance_custom_word_point_by (exp_start - text);
1059 text = exp_start;
1060 const char *word = advance_to_expression_complete_word_point (tracker, text);
1061 expression_completer (cmd, tracker, text, word);
1062 }
1063
1064 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1065
1066 static void
1067 condition_command (const char *arg, int from_tty)
1068 {
1069 struct breakpoint *b;
1070 const char *p;
1071 int bnum;
1072
1073 if (arg == 0)
1074 error_no_arg (_("breakpoint number"));
1075
1076 p = arg;
1077
1078 /* Check if the "-force" flag was passed. */
1079 condition_command_opts cc_opts;
1080 const auto group = make_condition_command_options_def_group (&cc_opts);
1081 gdb::option::process_options
1082 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
1083
1084 bnum = get_number (&p);
1085 if (bnum == 0)
1086 error (_("Bad breakpoint argument: '%s'"), arg);
1087
1088 ALL_BREAKPOINTS (b)
1089 if (b->number == bnum)
1090 {
1091 /* Check if this breakpoint has a "stop" method implemented in an
1092 extension language. This method and conditions entered into GDB
1093 from the CLI are mutually exclusive. */
1094 const struct extension_language_defn *extlang
1095 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1096
1097 if (extlang != NULL)
1098 {
1099 error (_("Only one stop condition allowed. There is currently"
1100 " a %s stop condition defined for this breakpoint."),
1101 ext_lang_capitalized_name (extlang));
1102 }
1103 set_breakpoint_condition (b, p, from_tty, cc_opts.force_condition);
1104
1105 if (is_breakpoint (b))
1106 update_global_location_list (UGLL_MAY_INSERT);
1107
1108 return;
1109 }
1110
1111 error (_("No breakpoint number %d."), bnum);
1112 }
1113
1114 /* Check that COMMAND do not contain commands that are suitable
1115 only for tracepoints and not suitable for ordinary breakpoints.
1116 Throw if any such commands is found. */
1117
1118 static void
1119 check_no_tracepoint_commands (struct command_line *commands)
1120 {
1121 struct command_line *c;
1122
1123 for (c = commands; c; c = c->next)
1124 {
1125 if (c->control_type == while_stepping_control)
1126 error (_("The 'while-stepping' command can "
1127 "only be used for tracepoints"));
1128
1129 check_no_tracepoint_commands (c->body_list_0.get ());
1130 check_no_tracepoint_commands (c->body_list_1.get ());
1131
1132 /* Not that command parsing removes leading whitespace and comment
1133 lines and also empty lines. So, we only need to check for
1134 command directly. */
1135 if (strstr (c->line, "collect ") == c->line)
1136 error (_("The 'collect' command can only be used for tracepoints"));
1137
1138 if (strstr (c->line, "teval ") == c->line)
1139 error (_("The 'teval' command can only be used for tracepoints"));
1140 }
1141 }
1142
1143 struct longjmp_breakpoint : public breakpoint
1144 {
1145 ~longjmp_breakpoint () override;
1146 };
1147
1148 /* Encapsulate tests for different types of tracepoints. */
1149
1150 static bool
1151 is_tracepoint_type (bptype type)
1152 {
1153 return (type == bp_tracepoint
1154 || type == bp_fast_tracepoint
1155 || type == bp_static_tracepoint);
1156 }
1157
1158 static bool
1159 is_longjmp_type (bptype type)
1160 {
1161 return type == bp_longjmp || type == bp_exception;
1162 }
1163
1164 /* See breakpoint.h. */
1165
1166 bool
1167 is_tracepoint (const struct breakpoint *b)
1168 {
1169 return is_tracepoint_type (b->type);
1170 }
1171
1172 /* Factory function to create an appropriate instance of breakpoint given
1173 TYPE. */
1174
1175 static std::unique_ptr<breakpoint>
1176 new_breakpoint_from_type (bptype type)
1177 {
1178 breakpoint *b;
1179
1180 if (is_tracepoint_type (type))
1181 b = new tracepoint ();
1182 else if (is_longjmp_type (type))
1183 b = new longjmp_breakpoint ();
1184 else
1185 b = new breakpoint ();
1186
1187 return std::unique_ptr<breakpoint> (b);
1188 }
1189
1190 /* A helper function that validates that COMMANDS are valid for a
1191 breakpoint. This function will throw an exception if a problem is
1192 found. */
1193
1194 static void
1195 validate_commands_for_breakpoint (struct breakpoint *b,
1196 struct command_line *commands)
1197 {
1198 if (is_tracepoint (b))
1199 {
1200 struct tracepoint *t = (struct tracepoint *) b;
1201 struct command_line *c;
1202 struct command_line *while_stepping = 0;
1203
1204 /* Reset the while-stepping step count. The previous commands
1205 might have included a while-stepping action, while the new
1206 ones might not. */
1207 t->step_count = 0;
1208
1209 /* We need to verify that each top-level element of commands is
1210 valid for tracepoints, that there's at most one
1211 while-stepping element, and that the while-stepping's body
1212 has valid tracing commands excluding nested while-stepping.
1213 We also need to validate the tracepoint action line in the
1214 context of the tracepoint --- validate_actionline actually
1215 has side effects, like setting the tracepoint's
1216 while-stepping STEP_COUNT, in addition to checking if the
1217 collect/teval actions parse and make sense in the
1218 tracepoint's context. */
1219 for (c = commands; c; c = c->next)
1220 {
1221 if (c->control_type == while_stepping_control)
1222 {
1223 if (b->type == bp_fast_tracepoint)
1224 error (_("The 'while-stepping' command "
1225 "cannot be used for fast tracepoint"));
1226 else if (b->type == bp_static_tracepoint)
1227 error (_("The 'while-stepping' command "
1228 "cannot be used for static tracepoint"));
1229
1230 if (while_stepping)
1231 error (_("The 'while-stepping' command "
1232 "can be used only once"));
1233 else
1234 while_stepping = c;
1235 }
1236
1237 validate_actionline (c->line, b);
1238 }
1239 if (while_stepping)
1240 {
1241 struct command_line *c2;
1242
1243 gdb_assert (while_stepping->body_list_1 == nullptr);
1244 c2 = while_stepping->body_list_0.get ();
1245 for (; c2; c2 = c2->next)
1246 {
1247 if (c2->control_type == while_stepping_control)
1248 error (_("The 'while-stepping' command cannot be nested"));
1249 }
1250 }
1251 }
1252 else
1253 {
1254 check_no_tracepoint_commands (commands);
1255 }
1256 }
1257
1258 /* Return a vector of all the static tracepoints set at ADDR. The
1259 caller is responsible for releasing the vector. */
1260
1261 std::vector<breakpoint *>
1262 static_tracepoints_here (CORE_ADDR addr)
1263 {
1264 struct breakpoint *b;
1265 std::vector<breakpoint *> found;
1266 struct bp_location *loc;
1267
1268 ALL_BREAKPOINTS (b)
1269 if (b->type == bp_static_tracepoint)
1270 {
1271 for (loc = b->loc; loc; loc = loc->next)
1272 if (loc->address == addr)
1273 found.push_back (b);
1274 }
1275
1276 return found;
1277 }
1278
1279 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1280 validate that only allowed commands are included. */
1281
1282 void
1283 breakpoint_set_commands (struct breakpoint *b,
1284 counted_command_line &&commands)
1285 {
1286 validate_commands_for_breakpoint (b, commands.get ());
1287
1288 b->commands = std::move (commands);
1289 gdb::observers::breakpoint_modified.notify (b);
1290 }
1291
1292 /* Set the internal `silent' flag on the breakpoint. Note that this
1293 is not the same as the "silent" that may appear in the breakpoint's
1294 commands. */
1295
1296 void
1297 breakpoint_set_silent (struct breakpoint *b, int silent)
1298 {
1299 int old_silent = b->silent;
1300
1301 b->silent = silent;
1302 if (old_silent != silent)
1303 gdb::observers::breakpoint_modified.notify (b);
1304 }
1305
1306 /* Set the thread for this breakpoint. If THREAD is -1, make the
1307 breakpoint work for any thread. */
1308
1309 void
1310 breakpoint_set_thread (struct breakpoint *b, int thread)
1311 {
1312 int old_thread = b->thread;
1313
1314 b->thread = thread;
1315 if (old_thread != thread)
1316 gdb::observers::breakpoint_modified.notify (b);
1317 }
1318
1319 /* Set the task for this breakpoint. If TASK is 0, make the
1320 breakpoint work for any task. */
1321
1322 void
1323 breakpoint_set_task (struct breakpoint *b, int task)
1324 {
1325 int old_task = b->task;
1326
1327 b->task = task;
1328 if (old_task != task)
1329 gdb::observers::breakpoint_modified.notify (b);
1330 }
1331
1332 static void
1333 commands_command_1 (const char *arg, int from_tty,
1334 struct command_line *control)
1335 {
1336 counted_command_line cmd;
1337 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1338 NULL after the call to read_command_lines if the user provides an empty
1339 list of command by just typing "end". */
1340 bool cmd_read = false;
1341
1342 std::string new_arg;
1343
1344 if (arg == NULL || !*arg)
1345 {
1346 /* Argument not explicitly given. Synthesize it. */
1347 if (breakpoint_count - prev_breakpoint_count > 1)
1348 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1349 breakpoint_count);
1350 else if (breakpoint_count > 0)
1351 new_arg = string_printf ("%d", breakpoint_count);
1352 }
1353 else
1354 {
1355 /* Create a copy of ARG. This is needed because the "commands"
1356 command may be coming from a script. In that case, the read
1357 line buffer is going to be overwritten in the lambda of
1358 'map_breakpoint_numbers' below when reading the next line
1359 before we are are done parsing the breakpoint numbers. */
1360 new_arg = arg;
1361 }
1362 arg = new_arg.c_str ();
1363
1364 map_breakpoint_numbers
1365 (arg, [&] (breakpoint *b)
1366 {
1367 if (!cmd_read)
1368 {
1369 gdb_assert (cmd == NULL);
1370 if (control != NULL)
1371 cmd = control->body_list_0;
1372 else
1373 {
1374 std::string str
1375 = string_printf (_("Type commands for breakpoint(s) "
1376 "%s, one per line."),
1377 arg);
1378
1379 auto do_validate = [=] (const char *line)
1380 {
1381 validate_actionline (line, b);
1382 };
1383 gdb::function_view<void (const char *)> validator;
1384 if (is_tracepoint (b))
1385 validator = do_validate;
1386
1387 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1388 }
1389 cmd_read = true;
1390 }
1391
1392 /* If a breakpoint was on the list more than once, we don't need to
1393 do anything. */
1394 if (b->commands != cmd)
1395 {
1396 validate_commands_for_breakpoint (b, cmd.get ());
1397 b->commands = cmd;
1398 gdb::observers::breakpoint_modified.notify (b);
1399 }
1400 });
1401 }
1402
1403 static void
1404 commands_command (const char *arg, int from_tty)
1405 {
1406 commands_command_1 (arg, from_tty, NULL);
1407 }
1408
1409 /* Like commands_command, but instead of reading the commands from
1410 input stream, takes them from an already parsed command structure.
1411
1412 This is used by cli-script.c to DTRT with breakpoint commands
1413 that are part of if and while bodies. */
1414 enum command_control_type
1415 commands_from_control_command (const char *arg, struct command_line *cmd)
1416 {
1417 commands_command_1 (arg, 0, cmd);
1418 return simple_control;
1419 }
1420
1421 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1422
1423 static int
1424 bp_location_has_shadow (struct bp_location *bl)
1425 {
1426 if (bl->loc_type != bp_loc_software_breakpoint)
1427 return 0;
1428 if (!bl->inserted)
1429 return 0;
1430 if (bl->target_info.shadow_len == 0)
1431 /* BL isn't valid, or doesn't shadow memory. */
1432 return 0;
1433 return 1;
1434 }
1435
1436 /* Update BUF, which is LEN bytes read from the target address
1437 MEMADDR, by replacing a memory breakpoint with its shadowed
1438 contents.
1439
1440 If READBUF is not NULL, this buffer must not overlap with the of
1441 the breakpoint location's shadow_contents buffer. Otherwise, a
1442 failed assertion internal error will be raised. */
1443
1444 static void
1445 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1446 const gdb_byte *writebuf_org,
1447 ULONGEST memaddr, LONGEST len,
1448 struct bp_target_info *target_info,
1449 struct gdbarch *gdbarch)
1450 {
1451 /* Now do full processing of the found relevant range of elements. */
1452 CORE_ADDR bp_addr = 0;
1453 int bp_size = 0;
1454 int bptoffset = 0;
1455
1456 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1457 current_program_space->aspace, 0))
1458 {
1459 /* The breakpoint is inserted in a different address space. */
1460 return;
1461 }
1462
1463 /* Addresses and length of the part of the breakpoint that
1464 we need to copy. */
1465 bp_addr = target_info->placed_address;
1466 bp_size = target_info->shadow_len;
1467
1468 if (bp_addr + bp_size <= memaddr)
1469 {
1470 /* The breakpoint is entirely before the chunk of memory we are
1471 reading. */
1472 return;
1473 }
1474
1475 if (bp_addr >= memaddr + len)
1476 {
1477 /* The breakpoint is entirely after the chunk of memory we are
1478 reading. */
1479 return;
1480 }
1481
1482 /* Offset within shadow_contents. */
1483 if (bp_addr < memaddr)
1484 {
1485 /* Only copy the second part of the breakpoint. */
1486 bp_size -= memaddr - bp_addr;
1487 bptoffset = memaddr - bp_addr;
1488 bp_addr = memaddr;
1489 }
1490
1491 if (bp_addr + bp_size > memaddr + len)
1492 {
1493 /* Only copy the first part of the breakpoint. */
1494 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1495 }
1496
1497 if (readbuf != NULL)
1498 {
1499 /* Verify that the readbuf buffer does not overlap with the
1500 shadow_contents buffer. */
1501 gdb_assert (target_info->shadow_contents >= readbuf + len
1502 || readbuf >= (target_info->shadow_contents
1503 + target_info->shadow_len));
1504
1505 /* Update the read buffer with this inserted breakpoint's
1506 shadow. */
1507 memcpy (readbuf + bp_addr - memaddr,
1508 target_info->shadow_contents + bptoffset, bp_size);
1509 }
1510 else
1511 {
1512 const unsigned char *bp;
1513 CORE_ADDR addr = target_info->reqstd_address;
1514 int placed_size;
1515
1516 /* Update the shadow with what we want to write to memory. */
1517 memcpy (target_info->shadow_contents + bptoffset,
1518 writebuf_org + bp_addr - memaddr, bp_size);
1519
1520 /* Determine appropriate breakpoint contents and size for this
1521 address. */
1522 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1523
1524 /* Update the final write buffer with this inserted
1525 breakpoint's INSN. */
1526 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1527 }
1528 }
1529
1530 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1531 by replacing any memory breakpoints with their shadowed contents.
1532
1533 If READBUF is not NULL, this buffer must not overlap with any of
1534 the breakpoint location's shadow_contents buffers. Otherwise,
1535 a failed assertion internal error will be raised.
1536
1537 The range of shadowed area by each bp_location is:
1538 bl->address - bp_locations_placed_address_before_address_max
1539 up to bl->address + bp_locations_shadow_len_after_address_max
1540 The range we were requested to resolve shadows for is:
1541 memaddr ... memaddr + len
1542 Thus the safe cutoff boundaries for performance optimization are
1543 memaddr + len <= (bl->address
1544 - bp_locations_placed_address_before_address_max)
1545 and:
1546 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1547
1548 void
1549 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1550 const gdb_byte *writebuf_org,
1551 ULONGEST memaddr, LONGEST len)
1552 {
1553 /* Left boundary, right boundary and median element of our binary
1554 search. */
1555 unsigned bc_l, bc_r, bc;
1556
1557 /* Find BC_L which is a leftmost element which may affect BUF
1558 content. It is safe to report lower value but a failure to
1559 report higher one. */
1560
1561 bc_l = 0;
1562 bc_r = bp_locations_count;
1563 while (bc_l + 1 < bc_r)
1564 {
1565 struct bp_location *bl;
1566
1567 bc = (bc_l + bc_r) / 2;
1568 bl = bp_locations[bc];
1569
1570 /* Check first BL->ADDRESS will not overflow due to the added
1571 constant. Then advance the left boundary only if we are sure
1572 the BC element can in no way affect the BUF content (MEMADDR
1573 to MEMADDR + LEN range).
1574
1575 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1576 offset so that we cannot miss a breakpoint with its shadow
1577 range tail still reaching MEMADDR. */
1578
1579 if ((bl->address + bp_locations_shadow_len_after_address_max
1580 >= bl->address)
1581 && (bl->address + bp_locations_shadow_len_after_address_max
1582 <= memaddr))
1583 bc_l = bc;
1584 else
1585 bc_r = bc;
1586 }
1587
1588 /* Due to the binary search above, we need to make sure we pick the
1589 first location that's at BC_L's address. E.g., if there are
1590 multiple locations at the same address, BC_L may end up pointing
1591 at a duplicate location, and miss the "master"/"inserted"
1592 location. Say, given locations L1, L2 and L3 at addresses A and
1593 B:
1594
1595 L1@A, L2@A, L3@B, ...
1596
1597 BC_L could end up pointing at location L2, while the "master"
1598 location could be L1. Since the `loc->inserted' flag is only set
1599 on "master" locations, we'd forget to restore the shadow of L1
1600 and L2. */
1601 while (bc_l > 0
1602 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1603 bc_l--;
1604
1605 /* Now do full processing of the found relevant range of elements. */
1606
1607 for (bc = bc_l; bc < bp_locations_count; bc++)
1608 {
1609 struct bp_location *bl = bp_locations[bc];
1610
1611 /* bp_location array has BL->OWNER always non-NULL. */
1612 if (bl->owner->type == bp_none)
1613 warning (_("reading through apparently deleted breakpoint #%d?"),
1614 bl->owner->number);
1615
1616 /* Performance optimization: any further element can no longer affect BUF
1617 content. */
1618
1619 if (bl->address >= bp_locations_placed_address_before_address_max
1620 && memaddr + len <= (bl->address
1621 - bp_locations_placed_address_before_address_max))
1622 break;
1623
1624 if (!bp_location_has_shadow (bl))
1625 continue;
1626
1627 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1628 memaddr, len, &bl->target_info, bl->gdbarch);
1629 }
1630 }
1631
1632 /* See breakpoint.h. */
1633
1634 bool
1635 is_breakpoint (const struct breakpoint *bpt)
1636 {
1637 return (bpt->type == bp_breakpoint
1638 || bpt->type == bp_hardware_breakpoint
1639 || bpt->type == bp_dprintf);
1640 }
1641
1642 /* Return true if BPT is of any hardware watchpoint kind. */
1643
1644 static bool
1645 is_hardware_watchpoint (const struct breakpoint *bpt)
1646 {
1647 return (bpt->type == bp_hardware_watchpoint
1648 || bpt->type == bp_read_watchpoint
1649 || bpt->type == bp_access_watchpoint);
1650 }
1651
1652 /* See breakpoint.h. */
1653
1654 bool
1655 is_watchpoint (const struct breakpoint *bpt)
1656 {
1657 return (is_hardware_watchpoint (bpt)
1658 || bpt->type == bp_watchpoint);
1659 }
1660
1661 /* Returns true if the current thread and its running state are safe
1662 to evaluate or update watchpoint B. Watchpoints on local
1663 expressions need to be evaluated in the context of the thread that
1664 was current when the watchpoint was created, and, that thread needs
1665 to be stopped to be able to select the correct frame context.
1666 Watchpoints on global expressions can be evaluated on any thread,
1667 and in any state. It is presently left to the target allowing
1668 memory accesses when threads are running. */
1669
1670 static int
1671 watchpoint_in_thread_scope (struct watchpoint *b)
1672 {
1673 return (b->pspace == current_program_space
1674 && (b->watchpoint_thread == null_ptid
1675 || (inferior_ptid == b->watchpoint_thread
1676 && !inferior_thread ()->executing)));
1677 }
1678
1679 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1680 associated bp_watchpoint_scope breakpoint. */
1681
1682 static void
1683 watchpoint_del_at_next_stop (struct watchpoint *w)
1684 {
1685 if (w->related_breakpoint != w)
1686 {
1687 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1688 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1689 w->related_breakpoint->disposition = disp_del_at_next_stop;
1690 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1691 w->related_breakpoint = w;
1692 }
1693 w->disposition = disp_del_at_next_stop;
1694 }
1695
1696 /* Extract a bitfield value from value VAL using the bit parameters contained in
1697 watchpoint W. */
1698
1699 static struct value *
1700 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1701 {
1702 struct value *bit_val;
1703
1704 if (val == NULL)
1705 return NULL;
1706
1707 bit_val = allocate_value (value_type (val));
1708
1709 unpack_value_bitfield (bit_val,
1710 w->val_bitpos,
1711 w->val_bitsize,
1712 value_contents_for_printing (val),
1713 value_offset (val),
1714 val);
1715
1716 return bit_val;
1717 }
1718
1719 /* Allocate a dummy location and add it to B, which must be a software
1720 watchpoint. This is required because even if a software watchpoint
1721 is not watching any memory, bpstat_stop_status requires a location
1722 to be able to report stops. */
1723
1724 static void
1725 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1726 struct program_space *pspace)
1727 {
1728 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1729
1730 b->loc = allocate_bp_location (b);
1731 b->loc->pspace = pspace;
1732 b->loc->address = -1;
1733 b->loc->length = -1;
1734 }
1735
1736 /* Returns true if B is a software watchpoint that is not watching any
1737 memory (e.g., "watch $pc"). */
1738
1739 static bool
1740 is_no_memory_software_watchpoint (struct breakpoint *b)
1741 {
1742 return (b->type == bp_watchpoint
1743 && b->loc != NULL
1744 && b->loc->next == NULL
1745 && b->loc->address == -1
1746 && b->loc->length == -1);
1747 }
1748
1749 /* Assuming that B is a watchpoint:
1750 - Reparse watchpoint expression, if REPARSE is non-zero
1751 - Evaluate expression and store the result in B->val
1752 - Evaluate the condition if there is one, and store the result
1753 in b->loc->cond.
1754 - Update the list of values that must be watched in B->loc.
1755
1756 If the watchpoint disposition is disp_del_at_next_stop, then do
1757 nothing. If this is local watchpoint that is out of scope, delete
1758 it.
1759
1760 Even with `set breakpoint always-inserted on' the watchpoints are
1761 removed + inserted on each stop here. Normal breakpoints must
1762 never be removed because they might be missed by a running thread
1763 when debugging in non-stop mode. On the other hand, hardware
1764 watchpoints (is_hardware_watchpoint; processed here) are specific
1765 to each LWP since they are stored in each LWP's hardware debug
1766 registers. Therefore, such LWP must be stopped first in order to
1767 be able to modify its hardware watchpoints.
1768
1769 Hardware watchpoints must be reset exactly once after being
1770 presented to the user. It cannot be done sooner, because it would
1771 reset the data used to present the watchpoint hit to the user. And
1772 it must not be done later because it could display the same single
1773 watchpoint hit during multiple GDB stops. Note that the latter is
1774 relevant only to the hardware watchpoint types bp_read_watchpoint
1775 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1776 not user-visible - its hit is suppressed if the memory content has
1777 not changed.
1778
1779 The following constraints influence the location where we can reset
1780 hardware watchpoints:
1781
1782 * target_stopped_by_watchpoint and target_stopped_data_address are
1783 called several times when GDB stops.
1784
1785 [linux]
1786 * Multiple hardware watchpoints can be hit at the same time,
1787 causing GDB to stop. GDB only presents one hardware watchpoint
1788 hit at a time as the reason for stopping, and all the other hits
1789 are presented later, one after the other, each time the user
1790 requests the execution to be resumed. Execution is not resumed
1791 for the threads still having pending hit event stored in
1792 LWP_INFO->STATUS. While the watchpoint is already removed from
1793 the inferior on the first stop the thread hit event is kept being
1794 reported from its cached value by linux_nat_stopped_data_address
1795 until the real thread resume happens after the watchpoint gets
1796 presented and thus its LWP_INFO->STATUS gets reset.
1797
1798 Therefore the hardware watchpoint hit can get safely reset on the
1799 watchpoint removal from inferior. */
1800
1801 static void
1802 update_watchpoint (struct watchpoint *b, int reparse)
1803 {
1804 int within_current_scope;
1805 struct frame_id saved_frame_id;
1806 int frame_saved;
1807
1808 /* If this is a local watchpoint, we only want to check if the
1809 watchpoint frame is in scope if the current thread is the thread
1810 that was used to create the watchpoint. */
1811 if (!watchpoint_in_thread_scope (b))
1812 return;
1813
1814 if (b->disposition == disp_del_at_next_stop)
1815 return;
1816
1817 frame_saved = 0;
1818
1819 /* Determine if the watchpoint is within scope. */
1820 if (b->exp_valid_block == NULL)
1821 within_current_scope = 1;
1822 else
1823 {
1824 struct frame_info *fi = get_current_frame ();
1825 struct gdbarch *frame_arch = get_frame_arch (fi);
1826 CORE_ADDR frame_pc = get_frame_pc (fi);
1827
1828 /* If we're at a point where the stack has been destroyed
1829 (e.g. in a function epilogue), unwinding may not work
1830 properly. Do not attempt to recreate locations at this
1831 point. See similar comments in watchpoint_check. */
1832 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1833 return;
1834
1835 /* Save the current frame's ID so we can restore it after
1836 evaluating the watchpoint expression on its own frame. */
1837 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1838 took a frame parameter, so that we didn't have to change the
1839 selected frame. */
1840 frame_saved = 1;
1841 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1842
1843 fi = frame_find_by_id (b->watchpoint_frame);
1844 within_current_scope = (fi != NULL);
1845 if (within_current_scope)
1846 select_frame (fi);
1847 }
1848
1849 /* We don't free locations. They are stored in the bp_location array
1850 and update_global_location_list will eventually delete them and
1851 remove breakpoints if needed. */
1852 b->loc = NULL;
1853
1854 if (within_current_scope && reparse)
1855 {
1856 const char *s;
1857
1858 b->exp.reset ();
1859 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1860 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1861 /* If the meaning of expression itself changed, the old value is
1862 no longer relevant. We don't want to report a watchpoint hit
1863 to the user when the old value and the new value may actually
1864 be completely different objects. */
1865 b->val = NULL;
1866 b->val_valid = false;
1867
1868 /* Note that unlike with breakpoints, the watchpoint's condition
1869 expression is stored in the breakpoint object, not in the
1870 locations (re)created below. */
1871 if (b->cond_string != NULL)
1872 {
1873 b->cond_exp.reset ();
1874
1875 s = b->cond_string;
1876 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1877 }
1878 }
1879
1880 /* If we failed to parse the expression, for example because
1881 it refers to a global variable in a not-yet-loaded shared library,
1882 don't try to insert watchpoint. We don't automatically delete
1883 such watchpoint, though, since failure to parse expression
1884 is different from out-of-scope watchpoint. */
1885 if (!target_has_execution ())
1886 {
1887 /* Without execution, memory can't change. No use to try and
1888 set watchpoint locations. The watchpoint will be reset when
1889 the target gains execution, through breakpoint_re_set. */
1890 if (!can_use_hw_watchpoints)
1891 {
1892 if (b->ops->works_in_software_mode (b))
1893 b->type = bp_watchpoint;
1894 else
1895 error (_("Can't set read/access watchpoint when "
1896 "hardware watchpoints are disabled."));
1897 }
1898 }
1899 else if (within_current_scope && b->exp)
1900 {
1901 int pc = 0;
1902 std::vector<value_ref_ptr> val_chain;
1903 struct value *v, *result;
1904 struct program_space *frame_pspace;
1905
1906 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, false);
1907
1908 /* Avoid setting b->val if it's already set. The meaning of
1909 b->val is 'the last value' user saw, and we should update
1910 it only if we reported that last value to user. As it
1911 happens, the code that reports it updates b->val directly.
1912 We don't keep track of the memory value for masked
1913 watchpoints. */
1914 if (!b->val_valid && !is_masked_watchpoint (b))
1915 {
1916 if (b->val_bitsize != 0)
1917 v = extract_bitfield_from_watchpoint_value (b, v);
1918 b->val = release_value (v);
1919 b->val_valid = true;
1920 }
1921
1922 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1923
1924 /* Look at each value on the value chain. */
1925 gdb_assert (!val_chain.empty ());
1926 for (const value_ref_ptr &iter : val_chain)
1927 {
1928 v = iter.get ();
1929
1930 /* If it's a memory location, and GDB actually needed
1931 its contents to evaluate the expression, then we
1932 must watch it. If the first value returned is
1933 still lazy, that means an error occurred reading it;
1934 watch it anyway in case it becomes readable. */
1935 if (VALUE_LVAL (v) == lval_memory
1936 && (v == val_chain[0] || ! value_lazy (v)))
1937 {
1938 struct type *vtype = check_typedef (value_type (v));
1939
1940 /* We only watch structs and arrays if user asked
1941 for it explicitly, never if they just happen to
1942 appear in the middle of some value chain. */
1943 if (v == result
1944 || (vtype->code () != TYPE_CODE_STRUCT
1945 && vtype->code () != TYPE_CODE_ARRAY))
1946 {
1947 CORE_ADDR addr;
1948 enum target_hw_bp_type type;
1949 struct bp_location *loc, **tmp;
1950 int bitpos = 0, bitsize = 0;
1951
1952 if (value_bitsize (v) != 0)
1953 {
1954 /* Extract the bit parameters out from the bitfield
1955 sub-expression. */
1956 bitpos = value_bitpos (v);
1957 bitsize = value_bitsize (v);
1958 }
1959 else if (v == result && b->val_bitsize != 0)
1960 {
1961 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1962 lvalue whose bit parameters are saved in the fields
1963 VAL_BITPOS and VAL_BITSIZE. */
1964 bitpos = b->val_bitpos;
1965 bitsize = b->val_bitsize;
1966 }
1967
1968 addr = value_address (v);
1969 if (bitsize != 0)
1970 {
1971 /* Skip the bytes that don't contain the bitfield. */
1972 addr += bitpos / 8;
1973 }
1974
1975 type = hw_write;
1976 if (b->type == bp_read_watchpoint)
1977 type = hw_read;
1978 else if (b->type == bp_access_watchpoint)
1979 type = hw_access;
1980
1981 loc = allocate_bp_location (b);
1982 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1983 ;
1984 *tmp = loc;
1985 loc->gdbarch = get_type_arch (value_type (v));
1986
1987 loc->pspace = frame_pspace;
1988 loc->address = address_significant (loc->gdbarch, addr);
1989
1990 if (bitsize != 0)
1991 {
1992 /* Just cover the bytes that make up the bitfield. */
1993 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1994 }
1995 else
1996 loc->length = TYPE_LENGTH (value_type (v));
1997
1998 loc->watchpoint_type = type;
1999 }
2000 }
2001 }
2002
2003 /* Change the type of breakpoint between hardware assisted or
2004 an ordinary watchpoint depending on the hardware support
2005 and free hardware slots. REPARSE is set when the inferior
2006 is started. */
2007 if (reparse)
2008 {
2009 int reg_cnt;
2010 enum bp_loc_type loc_type;
2011 struct bp_location *bl;
2012
2013 reg_cnt = can_use_hardware_watchpoint (val_chain);
2014
2015 if (reg_cnt)
2016 {
2017 int i, target_resources_ok, other_type_used;
2018 enum bptype type;
2019
2020 /* Use an exact watchpoint when there's only one memory region to be
2021 watched, and only one debug register is needed to watch it. */
2022 b->exact = target_exact_watchpoints && reg_cnt == 1;
2023
2024 /* We need to determine how many resources are already
2025 used for all other hardware watchpoints plus this one
2026 to see if we still have enough resources to also fit
2027 this watchpoint in as well. */
2028
2029 /* If this is a software watchpoint, we try to turn it
2030 to a hardware one -- count resources as if B was of
2031 hardware watchpoint type. */
2032 type = b->type;
2033 if (type == bp_watchpoint)
2034 type = bp_hardware_watchpoint;
2035
2036 /* This watchpoint may or may not have been placed on
2037 the list yet at this point (it won't be in the list
2038 if we're trying to create it for the first time,
2039 through watch_command), so always account for it
2040 manually. */
2041
2042 /* Count resources used by all watchpoints except B. */
2043 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
2044
2045 /* Add in the resources needed for B. */
2046 i += hw_watchpoint_use_count (b);
2047
2048 target_resources_ok
2049 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2050 if (target_resources_ok <= 0)
2051 {
2052 int sw_mode = b->ops->works_in_software_mode (b);
2053
2054 if (target_resources_ok == 0 && !sw_mode)
2055 error (_("Target does not support this type of "
2056 "hardware watchpoint."));
2057 else if (target_resources_ok < 0 && !sw_mode)
2058 error (_("There are not enough available hardware "
2059 "resources for this watchpoint."));
2060
2061 /* Downgrade to software watchpoint. */
2062 b->type = bp_watchpoint;
2063 }
2064 else
2065 {
2066 /* If this was a software watchpoint, we've just
2067 found we have enough resources to turn it to a
2068 hardware watchpoint. Otherwise, this is a
2069 nop. */
2070 b->type = type;
2071 }
2072 }
2073 else if (!b->ops->works_in_software_mode (b))
2074 {
2075 if (!can_use_hw_watchpoints)
2076 error (_("Can't set read/access watchpoint when "
2077 "hardware watchpoints are disabled."));
2078 else
2079 error (_("Expression cannot be implemented with "
2080 "read/access watchpoint."));
2081 }
2082 else
2083 b->type = bp_watchpoint;
2084
2085 loc_type = (b->type == bp_watchpoint? bp_loc_other
2086 : bp_loc_hardware_watchpoint);
2087 for (bl = b->loc; bl; bl = bl->next)
2088 bl->loc_type = loc_type;
2089 }
2090
2091 /* If a software watchpoint is not watching any memory, then the
2092 above left it without any location set up. But,
2093 bpstat_stop_status requires a location to be able to report
2094 stops, so make sure there's at least a dummy one. */
2095 if (b->type == bp_watchpoint && b->loc == NULL)
2096 software_watchpoint_add_no_memory_location (b, frame_pspace);
2097 }
2098 else if (!within_current_scope)
2099 {
2100 printf_filtered (_("\
2101 Watchpoint %d deleted because the program has left the block\n\
2102 in which its expression is valid.\n"),
2103 b->number);
2104 watchpoint_del_at_next_stop (b);
2105 }
2106
2107 /* Restore the selected frame. */
2108 if (frame_saved)
2109 select_frame (frame_find_by_id (saved_frame_id));
2110 }
2111
2112
2113 /* Returns 1 iff breakpoint location should be
2114 inserted in the inferior. We don't differentiate the type of BL's owner
2115 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2116 breakpoint_ops is not defined, because in insert_bp_location,
2117 tracepoint's insert_location will not be called. */
2118 static int
2119 should_be_inserted (struct bp_location *bl)
2120 {
2121 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2122 return 0;
2123
2124 if (bl->owner->disposition == disp_del_at_next_stop)
2125 return 0;
2126
2127 if (!bl->enabled || bl->disabled_by_cond
2128 || bl->shlib_disabled || bl->duplicate)
2129 return 0;
2130
2131 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2132 return 0;
2133
2134 /* This is set for example, when we're attached to the parent of a
2135 vfork, and have detached from the child. The child is running
2136 free, and we expect it to do an exec or exit, at which point the
2137 OS makes the parent schedulable again (and the target reports
2138 that the vfork is done). Until the child is done with the shared
2139 memory region, do not insert breakpoints in the parent, otherwise
2140 the child could still trip on the parent's breakpoints. Since
2141 the parent is blocked anyway, it won't miss any breakpoint. */
2142 if (bl->pspace->breakpoints_not_allowed)
2143 return 0;
2144
2145 /* Don't insert a breakpoint if we're trying to step past its
2146 location, except if the breakpoint is a single-step breakpoint,
2147 and the breakpoint's thread is the thread which is stepping past
2148 a breakpoint. */
2149 if ((bl->loc_type == bp_loc_software_breakpoint
2150 || bl->loc_type == bp_loc_hardware_breakpoint)
2151 && stepping_past_instruction_at (bl->pspace->aspace,
2152 bl->address)
2153 /* The single-step breakpoint may be inserted at the location
2154 we're trying to step if the instruction branches to itself.
2155 However, the instruction won't be executed at all and it may
2156 break the semantics of the instruction, for example, the
2157 instruction is a conditional branch or updates some flags.
2158 We can't fix it unless GDB is able to emulate the instruction
2159 or switch to displaced stepping. */
2160 && !(bl->owner->type == bp_single_step
2161 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2162 {
2163 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2164 paddress (bl->gdbarch, bl->address));
2165 return 0;
2166 }
2167
2168 /* Don't insert watchpoints if we're trying to step past the
2169 instruction that triggered one. */
2170 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2171 && stepping_past_nonsteppable_watchpoint ())
2172 {
2173 infrun_debug_printf ("stepping past non-steppable watchpoint. "
2174 "skipping watchpoint at %s:%d",
2175 paddress (bl->gdbarch, bl->address), bl->length);
2176 return 0;
2177 }
2178
2179 return 1;
2180 }
2181
2182 /* Same as should_be_inserted but does the check assuming
2183 that the location is not duplicated. */
2184
2185 static int
2186 unduplicated_should_be_inserted (struct bp_location *bl)
2187 {
2188 int result;
2189 const int save_duplicate = bl->duplicate;
2190
2191 bl->duplicate = 0;
2192 result = should_be_inserted (bl);
2193 bl->duplicate = save_duplicate;
2194 return result;
2195 }
2196
2197 /* Parses a conditional described by an expression COND into an
2198 agent expression bytecode suitable for evaluation
2199 by the bytecode interpreter. Return NULL if there was
2200 any error during parsing. */
2201
2202 static agent_expr_up
2203 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2204 {
2205 if (cond == NULL)
2206 return NULL;
2207
2208 agent_expr_up aexpr;
2209
2210 /* We don't want to stop processing, so catch any errors
2211 that may show up. */
2212 try
2213 {
2214 aexpr = gen_eval_for_expr (scope, cond);
2215 }
2216
2217 catch (const gdb_exception_error &ex)
2218 {
2219 /* If we got here, it means the condition could not be parsed to a valid
2220 bytecode expression and thus can't be evaluated on the target's side.
2221 It's no use iterating through the conditions. */
2222 }
2223
2224 /* We have a valid agent expression. */
2225 return aexpr;
2226 }
2227
2228 /* Based on location BL, create a list of breakpoint conditions to be
2229 passed on to the target. If we have duplicated locations with different
2230 conditions, we will add such conditions to the list. The idea is that the
2231 target will evaluate the list of conditions and will only notify GDB when
2232 one of them is true. */
2233
2234 static void
2235 build_target_condition_list (struct bp_location *bl)
2236 {
2237 struct bp_location **locp = NULL, **loc2p;
2238 int null_condition_or_parse_error = 0;
2239 int modified = bl->needs_update;
2240 struct bp_location *loc;
2241
2242 /* Release conditions left over from a previous insert. */
2243 bl->target_info.conditions.clear ();
2244
2245 /* This is only meaningful if the target is
2246 evaluating conditions and if the user has
2247 opted for condition evaluation on the target's
2248 side. */
2249 if (gdb_evaluates_breakpoint_condition_p ()
2250 || !target_supports_evaluation_of_breakpoint_conditions ())
2251 return;
2252
2253 /* Do a first pass to check for locations with no assigned
2254 conditions or conditions that fail to parse to a valid agent
2255 expression bytecode. If any of these happen, then it's no use to
2256 send conditions to the target since this location will always
2257 trigger and generate a response back to GDB. Note we consider
2258 all locations at the same address irrespective of type, i.e.,
2259 even if the locations aren't considered duplicates (e.g.,
2260 software breakpoint and hardware breakpoint at the same
2261 address). */
2262 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2263 {
2264 loc = (*loc2p);
2265 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2266 {
2267 if (modified)
2268 {
2269 /* Re-parse the conditions since something changed. In that
2270 case we already freed the condition bytecodes (see
2271 force_breakpoint_reinsertion). We just
2272 need to parse the condition to bytecodes again. */
2273 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2274 loc->cond.get ());
2275 }
2276
2277 /* If we have a NULL bytecode expression, it means something
2278 went wrong or we have a null condition expression. */
2279 if (!loc->cond_bytecode)
2280 {
2281 null_condition_or_parse_error = 1;
2282 break;
2283 }
2284 }
2285 }
2286
2287 /* If any of these happened, it means we will have to evaluate the conditions
2288 for the location's address on gdb's side. It is no use keeping bytecodes
2289 for all the other duplicate locations, thus we free all of them here.
2290
2291 This is so we have a finer control over which locations' conditions are
2292 being evaluated by GDB or the remote stub. */
2293 if (null_condition_or_parse_error)
2294 {
2295 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2296 {
2297 loc = (*loc2p);
2298 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2299 {
2300 /* Only go as far as the first NULL bytecode is
2301 located. */
2302 if (!loc->cond_bytecode)
2303 return;
2304
2305 loc->cond_bytecode.reset ();
2306 }
2307 }
2308 }
2309
2310 /* No NULL conditions or failed bytecode generation. Build a
2311 condition list for this location's address. If we have software
2312 and hardware locations at the same address, they aren't
2313 considered duplicates, but we still marge all the conditions
2314 anyway, as it's simpler, and doesn't really make a practical
2315 difference. */
2316 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2317 {
2318 loc = (*loc2p);
2319 if (loc->cond
2320 && is_breakpoint (loc->owner)
2321 && loc->pspace->num == bl->pspace->num
2322 && loc->owner->enable_state == bp_enabled
2323 && loc->enabled
2324 && !loc->disabled_by_cond)
2325 {
2326 /* Add the condition to the vector. This will be used later
2327 to send the conditions to the target. */
2328 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2329 }
2330 }
2331
2332 return;
2333 }
2334
2335 /* Parses a command described by string CMD into an agent expression
2336 bytecode suitable for evaluation by the bytecode interpreter.
2337 Return NULL if there was any error during parsing. */
2338
2339 static agent_expr_up
2340 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2341 {
2342 const char *cmdrest;
2343 const char *format_start, *format_end;
2344 struct gdbarch *gdbarch = get_current_arch ();
2345
2346 if (cmd == NULL)
2347 return NULL;
2348
2349 cmdrest = cmd;
2350
2351 if (*cmdrest == ',')
2352 ++cmdrest;
2353 cmdrest = skip_spaces (cmdrest);
2354
2355 if (*cmdrest++ != '"')
2356 error (_("No format string following the location"));
2357
2358 format_start = cmdrest;
2359
2360 format_pieces fpieces (&cmdrest);
2361
2362 format_end = cmdrest;
2363
2364 if (*cmdrest++ != '"')
2365 error (_("Bad format string, non-terminated '\"'."));
2366
2367 cmdrest = skip_spaces (cmdrest);
2368
2369 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2370 error (_("Invalid argument syntax"));
2371
2372 if (*cmdrest == ',')
2373 cmdrest++;
2374 cmdrest = skip_spaces (cmdrest);
2375
2376 /* For each argument, make an expression. */
2377
2378 std::vector<struct expression *> argvec;
2379 while (*cmdrest != '\0')
2380 {
2381 const char *cmd1;
2382
2383 cmd1 = cmdrest;
2384 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2385 argvec.push_back (expr.release ());
2386 cmdrest = cmd1;
2387 if (*cmdrest == ',')
2388 ++cmdrest;
2389 }
2390
2391 agent_expr_up aexpr;
2392
2393 /* We don't want to stop processing, so catch any errors
2394 that may show up. */
2395 try
2396 {
2397 aexpr = gen_printf (scope, gdbarch, 0, 0,
2398 format_start, format_end - format_start,
2399 argvec.size (), argvec.data ());
2400 }
2401 catch (const gdb_exception_error &ex)
2402 {
2403 /* If we got here, it means the command could not be parsed to a valid
2404 bytecode expression and thus can't be evaluated on the target's side.
2405 It's no use iterating through the other commands. */
2406 }
2407
2408 /* We have a valid agent expression, return it. */
2409 return aexpr;
2410 }
2411
2412 /* Based on location BL, create a list of breakpoint commands to be
2413 passed on to the target. If we have duplicated locations with
2414 different commands, we will add any such to the list. */
2415
2416 static void
2417 build_target_command_list (struct bp_location *bl)
2418 {
2419 struct bp_location **locp = NULL, **loc2p;
2420 int null_command_or_parse_error = 0;
2421 int modified = bl->needs_update;
2422 struct bp_location *loc;
2423
2424 /* Clear commands left over from a previous insert. */
2425 bl->target_info.tcommands.clear ();
2426
2427 if (!target_can_run_breakpoint_commands ())
2428 return;
2429
2430 /* For now, limit to agent-style dprintf breakpoints. */
2431 if (dprintf_style != dprintf_style_agent)
2432 return;
2433
2434 /* For now, if we have any location at the same address that isn't a
2435 dprintf, don't install the target-side commands, as that would
2436 make the breakpoint not be reported to the core, and we'd lose
2437 control. */
2438 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2439 {
2440 loc = (*loc2p);
2441 if (is_breakpoint (loc->owner)
2442 && loc->pspace->num == bl->pspace->num
2443 && loc->owner->type != bp_dprintf)
2444 return;
2445 }
2446
2447 /* Do a first pass to check for locations with no assigned
2448 conditions or conditions that fail to parse to a valid agent expression
2449 bytecode. If any of these happen, then it's no use to send conditions
2450 to the target since this location will always trigger and generate a
2451 response back to GDB. */
2452 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2453 {
2454 loc = (*loc2p);
2455 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2456 {
2457 if (modified)
2458 {
2459 /* Re-parse the commands since something changed. In that
2460 case we already freed the command bytecodes (see
2461 force_breakpoint_reinsertion). We just
2462 need to parse the command to bytecodes again. */
2463 loc->cmd_bytecode
2464 = parse_cmd_to_aexpr (bl->address,
2465 loc->owner->extra_string);
2466 }
2467
2468 /* If we have a NULL bytecode expression, it means something
2469 went wrong or we have a null command expression. */
2470 if (!loc->cmd_bytecode)
2471 {
2472 null_command_or_parse_error = 1;
2473 break;
2474 }
2475 }
2476 }
2477
2478 /* If anything failed, then we're not doing target-side commands,
2479 and so clean up. */
2480 if (null_command_or_parse_error)
2481 {
2482 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2483 {
2484 loc = (*loc2p);
2485 if (is_breakpoint (loc->owner)
2486 && loc->pspace->num == bl->pspace->num)
2487 {
2488 /* Only go as far as the first NULL bytecode is
2489 located. */
2490 if (loc->cmd_bytecode == NULL)
2491 return;
2492
2493 loc->cmd_bytecode.reset ();
2494 }
2495 }
2496 }
2497
2498 /* No NULL commands or failed bytecode generation. Build a command
2499 list for all duplicate locations at this location's address.
2500 Note that here we must care for whether the breakpoint location
2501 types are considered duplicates, otherwise, say, if we have a
2502 software and hardware location at the same address, the target
2503 could end up running the commands twice. For the moment, we only
2504 support targets-side commands with dprintf, but it doesn't hurt
2505 to be pedantically correct in case that changes. */
2506 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2507 {
2508 loc = (*loc2p);
2509 if (breakpoint_locations_match (bl, loc)
2510 && loc->owner->extra_string
2511 && is_breakpoint (loc->owner)
2512 && loc->pspace->num == bl->pspace->num
2513 && loc->owner->enable_state == bp_enabled
2514 && loc->enabled
2515 && !loc->disabled_by_cond)
2516 {
2517 /* Add the command to the vector. This will be used later
2518 to send the commands to the target. */
2519 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2520 }
2521 }
2522
2523 bl->target_info.persist = 0;
2524 /* Maybe flag this location as persistent. */
2525 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2526 bl->target_info.persist = 1;
2527 }
2528
2529 /* Return the kind of breakpoint on address *ADDR. Get the kind
2530 of breakpoint according to ADDR except single-step breakpoint.
2531 Get the kind of single-step breakpoint according to the current
2532 registers state. */
2533
2534 static int
2535 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2536 {
2537 if (bl->owner->type == bp_single_step)
2538 {
2539 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2540 struct regcache *regcache;
2541
2542 regcache = get_thread_regcache (thr);
2543
2544 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2545 regcache, addr);
2546 }
2547 else
2548 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2549 }
2550
2551 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2552 location. Any error messages are printed to TMP_ERROR_STREAM; and
2553 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2554 Returns 0 for success, 1 if the bp_location type is not supported or
2555 -1 for failure.
2556
2557 NOTE drow/2003-09-09: This routine could be broken down to an
2558 object-style method for each breakpoint or catchpoint type. */
2559 static int
2560 insert_bp_location (struct bp_location *bl,
2561 struct ui_file *tmp_error_stream,
2562 int *disabled_breaks,
2563 int *hw_breakpoint_error,
2564 int *hw_bp_error_explained_already)
2565 {
2566 gdb_exception bp_excpt;
2567
2568 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2569 return 0;
2570
2571 /* Note we don't initialize bl->target_info, as that wipes out
2572 the breakpoint location's shadow_contents if the breakpoint
2573 is still inserted at that location. This in turn breaks
2574 target_read_memory which depends on these buffers when
2575 a memory read is requested at the breakpoint location:
2576 Once the target_info has been wiped, we fail to see that
2577 we have a breakpoint inserted at that address and thus
2578 read the breakpoint instead of returning the data saved in
2579 the breakpoint location's shadow contents. */
2580 bl->target_info.reqstd_address = bl->address;
2581 bl->target_info.placed_address_space = bl->pspace->aspace;
2582 bl->target_info.length = bl->length;
2583
2584 /* When working with target-side conditions, we must pass all the conditions
2585 for the same breakpoint address down to the target since GDB will not
2586 insert those locations. With a list of breakpoint conditions, the target
2587 can decide when to stop and notify GDB. */
2588
2589 if (is_breakpoint (bl->owner))
2590 {
2591 build_target_condition_list (bl);
2592 build_target_command_list (bl);
2593 /* Reset the modification marker. */
2594 bl->needs_update = 0;
2595 }
2596
2597 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2598 set at a read-only address, then a breakpoint location will have
2599 been changed to hardware breakpoint before we get here. If it is
2600 "off" however, error out before actually trying to insert the
2601 breakpoint, with a nicer error message. */
2602 if (bl->loc_type == bp_loc_software_breakpoint
2603 && !automatic_hardware_breakpoints)
2604 {
2605 mem_region *mr = lookup_mem_region (bl->address);
2606
2607 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2608 {
2609 fprintf_unfiltered (tmp_error_stream,
2610 _("Cannot insert breakpoint %d.\n"
2611 "Cannot set software breakpoint "
2612 "at read-only address %s\n"),
2613 bl->owner->number,
2614 paddress (bl->gdbarch, bl->address));
2615 return 1;
2616 }
2617 }
2618
2619 if (bl->loc_type == bp_loc_software_breakpoint
2620 || bl->loc_type == bp_loc_hardware_breakpoint)
2621 {
2622 /* First check to see if we have to handle an overlay. */
2623 if (overlay_debugging == ovly_off
2624 || bl->section == NULL
2625 || !(section_is_overlay (bl->section)))
2626 {
2627 /* No overlay handling: just set the breakpoint. */
2628 try
2629 {
2630 int val;
2631
2632 val = bl->owner->ops->insert_location (bl);
2633 if (val)
2634 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2635 }
2636 catch (gdb_exception &e)
2637 {
2638 bp_excpt = std::move (e);
2639 }
2640 }
2641 else
2642 {
2643 /* This breakpoint is in an overlay section.
2644 Shall we set a breakpoint at the LMA? */
2645 if (!overlay_events_enabled)
2646 {
2647 /* Yes -- overlay event support is not active,
2648 so we must try to set a breakpoint at the LMA.
2649 This will not work for a hardware breakpoint. */
2650 if (bl->loc_type == bp_loc_hardware_breakpoint)
2651 warning (_("hardware breakpoint %d not supported in overlay!"),
2652 bl->owner->number);
2653 else
2654 {
2655 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2656 bl->section);
2657 /* Set a software (trap) breakpoint at the LMA. */
2658 bl->overlay_target_info = bl->target_info;
2659 bl->overlay_target_info.reqstd_address = addr;
2660
2661 /* No overlay handling: just set the breakpoint. */
2662 try
2663 {
2664 int val;
2665
2666 bl->overlay_target_info.kind
2667 = breakpoint_kind (bl, &addr);
2668 bl->overlay_target_info.placed_address = addr;
2669 val = target_insert_breakpoint (bl->gdbarch,
2670 &bl->overlay_target_info);
2671 if (val)
2672 bp_excpt
2673 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2674 }
2675 catch (gdb_exception &e)
2676 {
2677 bp_excpt = std::move (e);
2678 }
2679
2680 if (bp_excpt.reason != 0)
2681 fprintf_unfiltered (tmp_error_stream,
2682 "Overlay breakpoint %d "
2683 "failed: in ROM?\n",
2684 bl->owner->number);
2685 }
2686 }
2687 /* Shall we set a breakpoint at the VMA? */
2688 if (section_is_mapped (bl->section))
2689 {
2690 /* Yes. This overlay section is mapped into memory. */
2691 try
2692 {
2693 int val;
2694
2695 val = bl->owner->ops->insert_location (bl);
2696 if (val)
2697 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2698 }
2699 catch (gdb_exception &e)
2700 {
2701 bp_excpt = std::move (e);
2702 }
2703 }
2704 else
2705 {
2706 /* No. This breakpoint will not be inserted.
2707 No error, but do not mark the bp as 'inserted'. */
2708 return 0;
2709 }
2710 }
2711
2712 if (bp_excpt.reason != 0)
2713 {
2714 /* Can't set the breakpoint. */
2715
2716 /* In some cases, we might not be able to insert a
2717 breakpoint in a shared library that has already been
2718 removed, but we have not yet processed the shlib unload
2719 event. Unfortunately, some targets that implement
2720 breakpoint insertion themselves can't tell why the
2721 breakpoint insertion failed (e.g., the remote target
2722 doesn't define error codes), so we must treat generic
2723 errors as memory errors. */
2724 if (bp_excpt.reason == RETURN_ERROR
2725 && (bp_excpt.error == GENERIC_ERROR
2726 || bp_excpt.error == MEMORY_ERROR)
2727 && bl->loc_type == bp_loc_software_breakpoint
2728 && (solib_name_from_address (bl->pspace, bl->address)
2729 || shared_objfile_contains_address_p (bl->pspace,
2730 bl->address)))
2731 {
2732 /* See also: disable_breakpoints_in_shlibs. */
2733 bl->shlib_disabled = 1;
2734 gdb::observers::breakpoint_modified.notify (bl->owner);
2735 if (!*disabled_breaks)
2736 {
2737 fprintf_unfiltered (tmp_error_stream,
2738 "Cannot insert breakpoint %d.\n",
2739 bl->owner->number);
2740 fprintf_unfiltered (tmp_error_stream,
2741 "Temporarily disabling shared "
2742 "library breakpoints:\n");
2743 }
2744 *disabled_breaks = 1;
2745 fprintf_unfiltered (tmp_error_stream,
2746 "breakpoint #%d\n", bl->owner->number);
2747 return 0;
2748 }
2749 else
2750 {
2751 if (bl->loc_type == bp_loc_hardware_breakpoint)
2752 {
2753 *hw_breakpoint_error = 1;
2754 *hw_bp_error_explained_already = bp_excpt.message != NULL;
2755 fprintf_unfiltered (tmp_error_stream,
2756 "Cannot insert hardware breakpoint %d%s",
2757 bl->owner->number,
2758 bp_excpt.message ? ":" : ".\n");
2759 if (bp_excpt.message != NULL)
2760 fprintf_unfiltered (tmp_error_stream, "%s.\n",
2761 bp_excpt.what ());
2762 }
2763 else
2764 {
2765 if (bp_excpt.message == NULL)
2766 {
2767 std::string message
2768 = memory_error_message (TARGET_XFER_E_IO,
2769 bl->gdbarch, bl->address);
2770
2771 fprintf_unfiltered (tmp_error_stream,
2772 "Cannot insert breakpoint %d.\n"
2773 "%s\n",
2774 bl->owner->number, message.c_str ());
2775 }
2776 else
2777 {
2778 fprintf_unfiltered (tmp_error_stream,
2779 "Cannot insert breakpoint %d: %s\n",
2780 bl->owner->number,
2781 bp_excpt.what ());
2782 }
2783 }
2784 return 1;
2785
2786 }
2787 }
2788 else
2789 bl->inserted = 1;
2790
2791 return 0;
2792 }
2793
2794 else if (bl->loc_type == bp_loc_hardware_watchpoint
2795 /* NOTE drow/2003-09-08: This state only exists for removing
2796 watchpoints. It's not clear that it's necessary... */
2797 && bl->owner->disposition != disp_del_at_next_stop)
2798 {
2799 int val;
2800
2801 gdb_assert (bl->owner->ops != NULL
2802 && bl->owner->ops->insert_location != NULL);
2803
2804 val = bl->owner->ops->insert_location (bl);
2805
2806 /* If trying to set a read-watchpoint, and it turns out it's not
2807 supported, try emulating one with an access watchpoint. */
2808 if (val == 1 && bl->watchpoint_type == hw_read)
2809 {
2810 struct bp_location *loc, **loc_temp;
2811
2812 /* But don't try to insert it, if there's already another
2813 hw_access location that would be considered a duplicate
2814 of this one. */
2815 ALL_BP_LOCATIONS (loc, loc_temp)
2816 if (loc != bl
2817 && loc->watchpoint_type == hw_access
2818 && watchpoint_locations_match (bl, loc))
2819 {
2820 bl->duplicate = 1;
2821 bl->inserted = 1;
2822 bl->target_info = loc->target_info;
2823 bl->watchpoint_type = hw_access;
2824 val = 0;
2825 break;
2826 }
2827
2828 if (val == 1)
2829 {
2830 bl->watchpoint_type = hw_access;
2831 val = bl->owner->ops->insert_location (bl);
2832
2833 if (val)
2834 /* Back to the original value. */
2835 bl->watchpoint_type = hw_read;
2836 }
2837 }
2838
2839 bl->inserted = (val == 0);
2840 }
2841
2842 else if (bl->owner->type == bp_catchpoint)
2843 {
2844 int val;
2845
2846 gdb_assert (bl->owner->ops != NULL
2847 && bl->owner->ops->insert_location != NULL);
2848
2849 val = bl->owner->ops->insert_location (bl);
2850 if (val)
2851 {
2852 bl->owner->enable_state = bp_disabled;
2853
2854 if (val == 1)
2855 warning (_("\
2856 Error inserting catchpoint %d: Your system does not support this type\n\
2857 of catchpoint."), bl->owner->number);
2858 else
2859 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2860 }
2861
2862 bl->inserted = (val == 0);
2863
2864 /* We've already printed an error message if there was a problem
2865 inserting this catchpoint, and we've disabled the catchpoint,
2866 so just return success. */
2867 return 0;
2868 }
2869
2870 return 0;
2871 }
2872
2873 /* This function is called when program space PSPACE is about to be
2874 deleted. It takes care of updating breakpoints to not reference
2875 PSPACE anymore. */
2876
2877 void
2878 breakpoint_program_space_exit (struct program_space *pspace)
2879 {
2880 struct breakpoint *b, *b_temp;
2881 struct bp_location *loc, **loc_temp;
2882
2883 /* Remove any breakpoint that was set through this program space. */
2884 ALL_BREAKPOINTS_SAFE (b, b_temp)
2885 {
2886 if (b->pspace == pspace)
2887 delete_breakpoint (b);
2888 }
2889
2890 /* Breakpoints set through other program spaces could have locations
2891 bound to PSPACE as well. Remove those. */
2892 ALL_BP_LOCATIONS (loc, loc_temp)
2893 {
2894 struct bp_location *tmp;
2895
2896 if (loc->pspace == pspace)
2897 {
2898 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2899 if (loc->owner->loc == loc)
2900 loc->owner->loc = loc->next;
2901 else
2902 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2903 if (tmp->next == loc)
2904 {
2905 tmp->next = loc->next;
2906 break;
2907 }
2908 }
2909 }
2910
2911 /* Now update the global location list to permanently delete the
2912 removed locations above. */
2913 update_global_location_list (UGLL_DONT_INSERT);
2914 }
2915
2916 /* Make sure all breakpoints are inserted in inferior.
2917 Throws exception on any error.
2918 A breakpoint that is already inserted won't be inserted
2919 again, so calling this function twice is safe. */
2920 void
2921 insert_breakpoints (void)
2922 {
2923 struct breakpoint *bpt;
2924
2925 ALL_BREAKPOINTS (bpt)
2926 if (is_hardware_watchpoint (bpt))
2927 {
2928 struct watchpoint *w = (struct watchpoint *) bpt;
2929
2930 update_watchpoint (w, 0 /* don't reparse. */);
2931 }
2932
2933 /* Updating watchpoints creates new locations, so update the global
2934 location list. Explicitly tell ugll to insert locations and
2935 ignore breakpoints_always_inserted_mode. Also,
2936 update_global_location_list tries to "upgrade" software
2937 breakpoints to hardware breakpoints to handle "set breakpoint
2938 auto-hw", so we need to call it even if we don't have new
2939 locations. */
2940 update_global_location_list (UGLL_INSERT);
2941 }
2942
2943 /* Invoke CALLBACK for each of bp_location. */
2944
2945 void
2946 iterate_over_bp_locations (walk_bp_location_callback callback)
2947 {
2948 struct bp_location *loc, **loc_tmp;
2949
2950 ALL_BP_LOCATIONS (loc, loc_tmp)
2951 {
2952 callback (loc, NULL);
2953 }
2954 }
2955
2956 /* This is used when we need to synch breakpoint conditions between GDB and the
2957 target. It is the case with deleting and disabling of breakpoints when using
2958 always-inserted mode. */
2959
2960 static void
2961 update_inserted_breakpoint_locations (void)
2962 {
2963 struct bp_location *bl, **blp_tmp;
2964 int error_flag = 0;
2965 int val = 0;
2966 int disabled_breaks = 0;
2967 int hw_breakpoint_error = 0;
2968 int hw_bp_details_reported = 0;
2969
2970 string_file tmp_error_stream;
2971
2972 /* Explicitly mark the warning -- this will only be printed if
2973 there was an error. */
2974 tmp_error_stream.puts ("Warning:\n");
2975
2976 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2977
2978 ALL_BP_LOCATIONS (bl, blp_tmp)
2979 {
2980 /* We only want to update software breakpoints and hardware
2981 breakpoints. */
2982 if (!is_breakpoint (bl->owner))
2983 continue;
2984
2985 /* We only want to update locations that are already inserted
2986 and need updating. This is to avoid unwanted insertion during
2987 deletion of breakpoints. */
2988 if (!bl->inserted || !bl->needs_update)
2989 continue;
2990
2991 switch_to_program_space_and_thread (bl->pspace);
2992
2993 /* For targets that support global breakpoints, there's no need
2994 to select an inferior to insert breakpoint to. In fact, even
2995 if we aren't attached to any process yet, we should still
2996 insert breakpoints. */
2997 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2998 && (inferior_ptid == null_ptid || !target_has_execution ()))
2999 continue;
3000
3001 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3002 &hw_breakpoint_error, &hw_bp_details_reported);
3003 if (val)
3004 error_flag = val;
3005 }
3006
3007 if (error_flag)
3008 {
3009 target_terminal::ours_for_output ();
3010 error_stream (tmp_error_stream);
3011 }
3012 }
3013
3014 /* Used when starting or continuing the program. */
3015
3016 static void
3017 insert_breakpoint_locations (void)
3018 {
3019 struct breakpoint *bpt;
3020 struct bp_location *bl, **blp_tmp;
3021 int error_flag = 0;
3022 int val = 0;
3023 int disabled_breaks = 0;
3024 int hw_breakpoint_error = 0;
3025 int hw_bp_error_explained_already = 0;
3026
3027 string_file tmp_error_stream;
3028
3029 /* Explicitly mark the warning -- this will only be printed if
3030 there was an error. */
3031 tmp_error_stream.puts ("Warning:\n");
3032
3033 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3034
3035 ALL_BP_LOCATIONS (bl, blp_tmp)
3036 {
3037 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3038 continue;
3039
3040 /* There is no point inserting thread-specific breakpoints if
3041 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3042 has BL->OWNER always non-NULL. */
3043 if (bl->owner->thread != -1
3044 && !valid_global_thread_id (bl->owner->thread))
3045 continue;
3046
3047 switch_to_program_space_and_thread (bl->pspace);
3048
3049 /* For targets that support global breakpoints, there's no need
3050 to select an inferior to insert breakpoint to. In fact, even
3051 if we aren't attached to any process yet, we should still
3052 insert breakpoints. */
3053 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3054 && (inferior_ptid == null_ptid || !target_has_execution ()))
3055 continue;
3056
3057 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3058 &hw_breakpoint_error, &hw_bp_error_explained_already);
3059 if (val)
3060 error_flag = val;
3061 }
3062
3063 /* If we failed to insert all locations of a watchpoint, remove
3064 them, as half-inserted watchpoint is of limited use. */
3065 ALL_BREAKPOINTS (bpt)
3066 {
3067 int some_failed = 0;
3068 struct bp_location *loc;
3069
3070 if (!is_hardware_watchpoint (bpt))
3071 continue;
3072
3073 if (!breakpoint_enabled (bpt))
3074 continue;
3075
3076 if (bpt->disposition == disp_del_at_next_stop)
3077 continue;
3078
3079 for (loc = bpt->loc; loc; loc = loc->next)
3080 if (!loc->inserted && should_be_inserted (loc))
3081 {
3082 some_failed = 1;
3083 break;
3084 }
3085 if (some_failed)
3086 {
3087 for (loc = bpt->loc; loc; loc = loc->next)
3088 if (loc->inserted)
3089 remove_breakpoint (loc);
3090
3091 hw_breakpoint_error = 1;
3092 tmp_error_stream.printf ("Could not insert "
3093 "hardware watchpoint %d.\n",
3094 bpt->number);
3095 error_flag = -1;
3096 }
3097 }
3098
3099 if (error_flag)
3100 {
3101 /* If a hardware breakpoint or watchpoint was inserted, add a
3102 message about possibly exhausted resources. */
3103 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3104 {
3105 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3106 You may have requested too many hardware breakpoints/watchpoints.\n");
3107 }
3108 target_terminal::ours_for_output ();
3109 error_stream (tmp_error_stream);
3110 }
3111 }
3112
3113 /* Used when the program stops.
3114 Returns zero if successful, or non-zero if there was a problem
3115 removing a breakpoint location. */
3116
3117 int
3118 remove_breakpoints (void)
3119 {
3120 struct bp_location *bl, **blp_tmp;
3121 int val = 0;
3122
3123 ALL_BP_LOCATIONS (bl, blp_tmp)
3124 {
3125 if (bl->inserted && !is_tracepoint (bl->owner))
3126 val |= remove_breakpoint (bl);
3127 }
3128 return val;
3129 }
3130
3131 /* When a thread exits, remove breakpoints that are related to
3132 that thread. */
3133
3134 static void
3135 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3136 {
3137 struct breakpoint *b, *b_tmp;
3138
3139 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3140 {
3141 if (b->thread == tp->global_num && user_breakpoint_p (b))
3142 {
3143 b->disposition = disp_del_at_next_stop;
3144
3145 printf_filtered (_("\
3146 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3147 b->number, print_thread_id (tp));
3148
3149 /* Hide it from the user. */
3150 b->number = 0;
3151 }
3152 }
3153 }
3154
3155 /* See breakpoint.h. */
3156
3157 void
3158 remove_breakpoints_inf (inferior *inf)
3159 {
3160 struct bp_location *bl, **blp_tmp;
3161 int val;
3162
3163 ALL_BP_LOCATIONS (bl, blp_tmp)
3164 {
3165 if (bl->pspace != inf->pspace)
3166 continue;
3167
3168 if (bl->inserted && !bl->target_info.persist)
3169 {
3170 val = remove_breakpoint (bl);
3171 if (val != 0)
3172 return;
3173 }
3174 }
3175 }
3176
3177 static int internal_breakpoint_number = -1;
3178
3179 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3180 If INTERNAL is non-zero, the breakpoint number will be populated
3181 from internal_breakpoint_number and that variable decremented.
3182 Otherwise the breakpoint number will be populated from
3183 breakpoint_count and that value incremented. Internal breakpoints
3184 do not set the internal var bpnum. */
3185 static void
3186 set_breakpoint_number (int internal, struct breakpoint *b)
3187 {
3188 if (internal)
3189 b->number = internal_breakpoint_number--;
3190 else
3191 {
3192 set_breakpoint_count (breakpoint_count + 1);
3193 b->number = breakpoint_count;
3194 }
3195 }
3196
3197 static struct breakpoint *
3198 create_internal_breakpoint (struct gdbarch *gdbarch,
3199 CORE_ADDR address, enum bptype type,
3200 const struct breakpoint_ops *ops)
3201 {
3202 symtab_and_line sal;
3203 sal.pc = address;
3204 sal.section = find_pc_overlay (sal.pc);
3205 sal.pspace = current_program_space;
3206
3207 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
3208 b->number = internal_breakpoint_number--;
3209 b->disposition = disp_donttouch;
3210
3211 return b;
3212 }
3213
3214 static const char *const longjmp_names[] =
3215 {
3216 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3217 };
3218 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3219
3220 /* Per-objfile data private to breakpoint.c. */
3221 struct breakpoint_objfile_data
3222 {
3223 /* Minimal symbol for "_ovly_debug_event" (if any). */
3224 struct bound_minimal_symbol overlay_msym {};
3225
3226 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3227 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
3228
3229 /* True if we have looked for longjmp probes. */
3230 int longjmp_searched = 0;
3231
3232 /* SystemTap probe points for longjmp (if any). These are non-owning
3233 references. */
3234 std::vector<probe *> longjmp_probes;
3235
3236 /* Minimal symbol for "std::terminate()" (if any). */
3237 struct bound_minimal_symbol terminate_msym {};
3238
3239 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3240 struct bound_minimal_symbol exception_msym {};
3241
3242 /* True if we have looked for exception probes. */
3243 int exception_searched = 0;
3244
3245 /* SystemTap probe points for unwinding (if any). These are non-owning
3246 references. */
3247 std::vector<probe *> exception_probes;
3248 };
3249
3250 static const struct objfile_key<breakpoint_objfile_data>
3251 breakpoint_objfile_key;
3252
3253 /* Minimal symbol not found sentinel. */
3254 static struct minimal_symbol msym_not_found;
3255
3256 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3257
3258 static int
3259 msym_not_found_p (const struct minimal_symbol *msym)
3260 {
3261 return msym == &msym_not_found;
3262 }
3263
3264 /* Return per-objfile data needed by breakpoint.c.
3265 Allocate the data if necessary. */
3266
3267 static struct breakpoint_objfile_data *
3268 get_breakpoint_objfile_data (struct objfile *objfile)
3269 {
3270 struct breakpoint_objfile_data *bp_objfile_data;
3271
3272 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3273 if (bp_objfile_data == NULL)
3274 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3275 return bp_objfile_data;
3276 }
3277
3278 static void
3279 create_overlay_event_breakpoint (void)
3280 {
3281 const char *const func_name = "_ovly_debug_event";
3282
3283 for (objfile *objfile : current_program_space->objfiles ())
3284 {
3285 struct breakpoint *b;
3286 struct breakpoint_objfile_data *bp_objfile_data;
3287 CORE_ADDR addr;
3288 struct explicit_location explicit_loc;
3289
3290 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3291
3292 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3293 continue;
3294
3295 if (bp_objfile_data->overlay_msym.minsym == NULL)
3296 {
3297 struct bound_minimal_symbol m;
3298
3299 m = lookup_minimal_symbol_text (func_name, objfile);
3300 if (m.minsym == NULL)
3301 {
3302 /* Avoid future lookups in this objfile. */
3303 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3304 continue;
3305 }
3306 bp_objfile_data->overlay_msym = m;
3307 }
3308
3309 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3310 b = create_internal_breakpoint (objfile->arch (), addr,
3311 bp_overlay_event,
3312 &internal_breakpoint_ops);
3313 initialize_explicit_location (&explicit_loc);
3314 explicit_loc.function_name = ASTRDUP (func_name);
3315 b->location = new_explicit_location (&explicit_loc);
3316
3317 if (overlay_debugging == ovly_auto)
3318 {
3319 b->enable_state = bp_enabled;
3320 overlay_events_enabled = 1;
3321 }
3322 else
3323 {
3324 b->enable_state = bp_disabled;
3325 overlay_events_enabled = 0;
3326 }
3327 }
3328 }
3329
3330 static void
3331 create_longjmp_master_breakpoint (void)
3332 {
3333 scoped_restore_current_program_space restore_pspace;
3334
3335 for (struct program_space *pspace : program_spaces)
3336 {
3337 set_current_program_space (pspace);
3338
3339 for (objfile *objfile : current_program_space->objfiles ())
3340 {
3341 int i;
3342 struct gdbarch *gdbarch;
3343 struct breakpoint_objfile_data *bp_objfile_data;
3344
3345 gdbarch = objfile->arch ();
3346
3347 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3348
3349 if (!bp_objfile_data->longjmp_searched)
3350 {
3351 std::vector<probe *> ret
3352 = find_probes_in_objfile (objfile, "libc", "longjmp");
3353
3354 if (!ret.empty ())
3355 {
3356 /* We are only interested in checking one element. */
3357 probe *p = ret[0];
3358
3359 if (!p->can_evaluate_arguments ())
3360 {
3361 /* We cannot use the probe interface here,
3362 because it does not know how to evaluate
3363 arguments. */
3364 ret.clear ();
3365 }
3366 }
3367 bp_objfile_data->longjmp_probes = ret;
3368 bp_objfile_data->longjmp_searched = 1;
3369 }
3370
3371 if (!bp_objfile_data->longjmp_probes.empty ())
3372 {
3373 for (probe *p : bp_objfile_data->longjmp_probes)
3374 {
3375 struct breakpoint *b;
3376
3377 b = create_internal_breakpoint (gdbarch,
3378 p->get_relocated_address (objfile),
3379 bp_longjmp_master,
3380 &internal_breakpoint_ops);
3381 b->location = new_probe_location ("-probe-stap libc:longjmp");
3382 b->enable_state = bp_disabled;
3383 }
3384
3385 continue;
3386 }
3387
3388 if (!gdbarch_get_longjmp_target_p (gdbarch))
3389 continue;
3390
3391 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3392 {
3393 struct breakpoint *b;
3394 const char *func_name;
3395 CORE_ADDR addr;
3396 struct explicit_location explicit_loc;
3397
3398 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3399 continue;
3400
3401 func_name = longjmp_names[i];
3402 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3403 {
3404 struct bound_minimal_symbol m;
3405
3406 m = lookup_minimal_symbol_text (func_name, objfile);
3407 if (m.minsym == NULL)
3408 {
3409 /* Prevent future lookups in this objfile. */
3410 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3411 continue;
3412 }
3413 bp_objfile_data->longjmp_msym[i] = m;
3414 }
3415
3416 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3417 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3418 &internal_breakpoint_ops);
3419 initialize_explicit_location (&explicit_loc);
3420 explicit_loc.function_name = ASTRDUP (func_name);
3421 b->location = new_explicit_location (&explicit_loc);
3422 b->enable_state = bp_disabled;
3423 }
3424 }
3425 }
3426 }
3427
3428 /* Create a master std::terminate breakpoint. */
3429 static void
3430 create_std_terminate_master_breakpoint (void)
3431 {
3432 const char *const func_name = "std::terminate()";
3433
3434 scoped_restore_current_program_space restore_pspace;
3435
3436 for (struct program_space *pspace : program_spaces)
3437 {
3438 CORE_ADDR addr;
3439
3440 set_current_program_space (pspace);
3441
3442 for (objfile *objfile : current_program_space->objfiles ())
3443 {
3444 struct breakpoint *b;
3445 struct breakpoint_objfile_data *bp_objfile_data;
3446 struct explicit_location explicit_loc;
3447
3448 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3449
3450 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3451 continue;
3452
3453 if (bp_objfile_data->terminate_msym.minsym == NULL)
3454 {
3455 struct bound_minimal_symbol m;
3456
3457 m = lookup_minimal_symbol (func_name, NULL, objfile);
3458 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3459 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3460 {
3461 /* Prevent future lookups in this objfile. */
3462 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3463 continue;
3464 }
3465 bp_objfile_data->terminate_msym = m;
3466 }
3467
3468 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3469 b = create_internal_breakpoint (objfile->arch (), addr,
3470 bp_std_terminate_master,
3471 &internal_breakpoint_ops);
3472 initialize_explicit_location (&explicit_loc);
3473 explicit_loc.function_name = ASTRDUP (func_name);
3474 b->location = new_explicit_location (&explicit_loc);
3475 b->enable_state = bp_disabled;
3476 }
3477 }
3478 }
3479
3480 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3481 probe. Return true if a breakpoint was installed. */
3482
3483 static bool
3484 create_exception_master_breakpoint_probe (objfile *objfile)
3485 {
3486 struct breakpoint *b;
3487 struct gdbarch *gdbarch;
3488 struct breakpoint_objfile_data *bp_objfile_data;
3489
3490 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3491
3492 /* We prefer the SystemTap probe point if it exists. */
3493 if (!bp_objfile_data->exception_searched)
3494 {
3495 std::vector<probe *> ret
3496 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3497
3498 if (!ret.empty ())
3499 {
3500 /* We are only interested in checking one element. */
3501 probe *p = ret[0];
3502
3503 if (!p->can_evaluate_arguments ())
3504 {
3505 /* We cannot use the probe interface here, because it does
3506 not know how to evaluate arguments. */
3507 ret.clear ();
3508 }
3509 }
3510 bp_objfile_data->exception_probes = ret;
3511 bp_objfile_data->exception_searched = 1;
3512 }
3513
3514 if (bp_objfile_data->exception_probes.empty ())
3515 return false;
3516
3517 gdbarch = objfile->arch ();
3518
3519 for (probe *p : bp_objfile_data->exception_probes)
3520 {
3521 b = create_internal_breakpoint (gdbarch,
3522 p->get_relocated_address (objfile),
3523 bp_exception_master,
3524 &internal_breakpoint_ops);
3525 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3526 b->enable_state = bp_disabled;
3527 }
3528
3529 return true;
3530 }
3531
3532 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3533 _Unwind_DebugHook. Return true if a breakpoint was installed. */
3534
3535 static bool
3536 create_exception_master_breakpoint_hook (objfile *objfile)
3537 {
3538 const char *const func_name = "_Unwind_DebugHook";
3539 struct breakpoint *b;
3540 struct gdbarch *gdbarch;
3541 struct breakpoint_objfile_data *bp_objfile_data;
3542 CORE_ADDR addr;
3543 struct explicit_location explicit_loc;
3544
3545 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3546
3547 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3548 return false;
3549
3550 gdbarch = objfile->arch ();
3551
3552 if (bp_objfile_data->exception_msym.minsym == NULL)
3553 {
3554 struct bound_minimal_symbol debug_hook;
3555
3556 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3557 if (debug_hook.minsym == NULL)
3558 {
3559 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3560 return false;
3561 }
3562
3563 bp_objfile_data->exception_msym = debug_hook;
3564 }
3565
3566 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3567 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3568 current_top_target ());
3569 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3570 &internal_breakpoint_ops);
3571 initialize_explicit_location (&explicit_loc);
3572 explicit_loc.function_name = ASTRDUP (func_name);
3573 b->location = new_explicit_location (&explicit_loc);
3574 b->enable_state = bp_disabled;
3575
3576 return true;
3577 }
3578
3579 /* Install a master breakpoint on the unwinder's debug hook. */
3580
3581 static void
3582 create_exception_master_breakpoint (void)
3583 {
3584 for (objfile *obj : current_program_space->objfiles ())
3585 {
3586 /* Skip separate debug object. */
3587 if (obj->separate_debug_objfile_backlink)
3588 continue;
3589
3590 /* Try a probe kind breakpoint. */
3591 if (create_exception_master_breakpoint_probe (obj))
3592 continue;
3593
3594 /* Iterate over separate debug objects and try an _Unwind_DebugHook
3595 kind breakpoint. */
3596 for (objfile *sepdebug = obj->separate_debug_objfile;
3597 sepdebug != nullptr; sepdebug = sepdebug->separate_debug_objfile)
3598 if (create_exception_master_breakpoint_hook (sepdebug))
3599 break;
3600 }
3601 }
3602
3603 /* Does B have a location spec? */
3604
3605 static int
3606 breakpoint_event_location_empty_p (const struct breakpoint *b)
3607 {
3608 return b->location != NULL && event_location_empty_p (b->location.get ());
3609 }
3610
3611 void
3612 update_breakpoints_after_exec (void)
3613 {
3614 struct breakpoint *b, *b_tmp;
3615 struct bp_location *bploc, **bplocp_tmp;
3616
3617 /* We're about to delete breakpoints from GDB's lists. If the
3618 INSERTED flag is true, GDB will try to lift the breakpoints by
3619 writing the breakpoints' "shadow contents" back into memory. The
3620 "shadow contents" are NOT valid after an exec, so GDB should not
3621 do that. Instead, the target is responsible from marking
3622 breakpoints out as soon as it detects an exec. We don't do that
3623 here instead, because there may be other attempts to delete
3624 breakpoints after detecting an exec and before reaching here. */
3625 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3626 if (bploc->pspace == current_program_space)
3627 gdb_assert (!bploc->inserted);
3628
3629 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3630 {
3631 if (b->pspace != current_program_space)
3632 continue;
3633
3634 /* Solib breakpoints must be explicitly reset after an exec(). */
3635 if (b->type == bp_shlib_event)
3636 {
3637 delete_breakpoint (b);
3638 continue;
3639 }
3640
3641 /* JIT breakpoints must be explicitly reset after an exec(). */
3642 if (b->type == bp_jit_event)
3643 {
3644 delete_breakpoint (b);
3645 continue;
3646 }
3647
3648 /* Thread event breakpoints must be set anew after an exec(),
3649 as must overlay event and longjmp master breakpoints. */
3650 if (b->type == bp_thread_event || b->type == bp_overlay_event
3651 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3652 || b->type == bp_exception_master)
3653 {
3654 delete_breakpoint (b);
3655 continue;
3656 }
3657
3658 /* Step-resume breakpoints are meaningless after an exec(). */
3659 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3660 {
3661 delete_breakpoint (b);
3662 continue;
3663 }
3664
3665 /* Just like single-step breakpoints. */
3666 if (b->type == bp_single_step)
3667 {
3668 delete_breakpoint (b);
3669 continue;
3670 }
3671
3672 /* Longjmp and longjmp-resume breakpoints are also meaningless
3673 after an exec. */
3674 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3675 || b->type == bp_longjmp_call_dummy
3676 || b->type == bp_exception || b->type == bp_exception_resume)
3677 {
3678 delete_breakpoint (b);
3679 continue;
3680 }
3681
3682 if (b->type == bp_catchpoint)
3683 {
3684 /* For now, none of the bp_catchpoint breakpoints need to
3685 do anything at this point. In the future, if some of
3686 the catchpoints need to something, we will need to add
3687 a new method, and call this method from here. */
3688 continue;
3689 }
3690
3691 /* bp_finish is a special case. The only way we ought to be able
3692 to see one of these when an exec() has happened, is if the user
3693 caught a vfork, and then said "finish". Ordinarily a finish just
3694 carries them to the call-site of the current callee, by setting
3695 a temporary bp there and resuming. But in this case, the finish
3696 will carry them entirely through the vfork & exec.
3697
3698 We don't want to allow a bp_finish to remain inserted now. But
3699 we can't safely delete it, 'cause finish_command has a handle to
3700 the bp on a bpstat, and will later want to delete it. There's a
3701 chance (and I've seen it happen) that if we delete the bp_finish
3702 here, that its storage will get reused by the time finish_command
3703 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3704 We really must allow finish_command to delete a bp_finish.
3705
3706 In the absence of a general solution for the "how do we know
3707 it's safe to delete something others may have handles to?"
3708 problem, what we'll do here is just uninsert the bp_finish, and
3709 let finish_command delete it.
3710
3711 (We know the bp_finish is "doomed" in the sense that it's
3712 momentary, and will be deleted as soon as finish_command sees
3713 the inferior stopped. So it doesn't matter that the bp's
3714 address is probably bogus in the new a.out, unlike e.g., the
3715 solib breakpoints.) */
3716
3717 if (b->type == bp_finish)
3718 {
3719 continue;
3720 }
3721
3722 /* Without a symbolic address, we have little hope of the
3723 pre-exec() address meaning the same thing in the post-exec()
3724 a.out. */
3725 if (breakpoint_event_location_empty_p (b))
3726 {
3727 delete_breakpoint (b);
3728 continue;
3729 }
3730 }
3731 }
3732
3733 int
3734 detach_breakpoints (ptid_t ptid)
3735 {
3736 struct bp_location *bl, **blp_tmp;
3737 int val = 0;
3738 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3739 struct inferior *inf = current_inferior ();
3740
3741 if (ptid.pid () == inferior_ptid.pid ())
3742 error (_("Cannot detach breakpoints of inferior_ptid"));
3743
3744 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3745 inferior_ptid = ptid;
3746 ALL_BP_LOCATIONS (bl, blp_tmp)
3747 {
3748 if (bl->pspace != inf->pspace)
3749 continue;
3750
3751 /* This function must physically remove breakpoints locations
3752 from the specified ptid, without modifying the breakpoint
3753 package's state. Locations of type bp_loc_other are only
3754 maintained at GDB side. So, there is no need to remove
3755 these bp_loc_other locations. Moreover, removing these
3756 would modify the breakpoint package's state. */
3757 if (bl->loc_type == bp_loc_other)
3758 continue;
3759
3760 if (bl->inserted)
3761 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3762 }
3763
3764 return val;
3765 }
3766
3767 /* Remove the breakpoint location BL from the current address space.
3768 Note that this is used to detach breakpoints from a child fork.
3769 When we get here, the child isn't in the inferior list, and neither
3770 do we have objects to represent its address space --- we should
3771 *not* look at bl->pspace->aspace here. */
3772
3773 static int
3774 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3775 {
3776 int val;
3777
3778 /* BL is never in moribund_locations by our callers. */
3779 gdb_assert (bl->owner != NULL);
3780
3781 /* The type of none suggests that owner is actually deleted.
3782 This should not ever happen. */
3783 gdb_assert (bl->owner->type != bp_none);
3784
3785 if (bl->loc_type == bp_loc_software_breakpoint
3786 || bl->loc_type == bp_loc_hardware_breakpoint)
3787 {
3788 /* "Normal" instruction breakpoint: either the standard
3789 trap-instruction bp (bp_breakpoint), or a
3790 bp_hardware_breakpoint. */
3791
3792 /* First check to see if we have to handle an overlay. */
3793 if (overlay_debugging == ovly_off
3794 || bl->section == NULL
3795 || !(section_is_overlay (bl->section)))
3796 {
3797 /* No overlay handling: just remove the breakpoint. */
3798
3799 /* If we're trying to uninsert a memory breakpoint that we
3800 know is set in a dynamic object that is marked
3801 shlib_disabled, then either the dynamic object was
3802 removed with "remove-symbol-file" or with
3803 "nosharedlibrary". In the former case, we don't know
3804 whether another dynamic object might have loaded over the
3805 breakpoint's address -- the user might well let us know
3806 about it next with add-symbol-file (the whole point of
3807 add-symbol-file is letting the user manually maintain a
3808 list of dynamically loaded objects). If we have the
3809 breakpoint's shadow memory, that is, this is a software
3810 breakpoint managed by GDB, check whether the breakpoint
3811 is still inserted in memory, to avoid overwriting wrong
3812 code with stale saved shadow contents. Note that HW
3813 breakpoints don't have shadow memory, as they're
3814 implemented using a mechanism that is not dependent on
3815 being able to modify the target's memory, and as such
3816 they should always be removed. */
3817 if (bl->shlib_disabled
3818 && bl->target_info.shadow_len != 0
3819 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3820 val = 0;
3821 else
3822 val = bl->owner->ops->remove_location (bl, reason);
3823 }
3824 else
3825 {
3826 /* This breakpoint is in an overlay section.
3827 Did we set a breakpoint at the LMA? */
3828 if (!overlay_events_enabled)
3829 {
3830 /* Yes -- overlay event support is not active, so we
3831 should have set a breakpoint at the LMA. Remove it.
3832 */
3833 /* Ignore any failures: if the LMA is in ROM, we will
3834 have already warned when we failed to insert it. */
3835 if (bl->loc_type == bp_loc_hardware_breakpoint)
3836 target_remove_hw_breakpoint (bl->gdbarch,
3837 &bl->overlay_target_info);
3838 else
3839 target_remove_breakpoint (bl->gdbarch,
3840 &bl->overlay_target_info,
3841 reason);
3842 }
3843 /* Did we set a breakpoint at the VMA?
3844 If so, we will have marked the breakpoint 'inserted'. */
3845 if (bl->inserted)
3846 {
3847 /* Yes -- remove it. Previously we did not bother to
3848 remove the breakpoint if the section had been
3849 unmapped, but let's not rely on that being safe. We
3850 don't know what the overlay manager might do. */
3851
3852 /* However, we should remove *software* breakpoints only
3853 if the section is still mapped, or else we overwrite
3854 wrong code with the saved shadow contents. */
3855 if (bl->loc_type == bp_loc_hardware_breakpoint
3856 || section_is_mapped (bl->section))
3857 val = bl->owner->ops->remove_location (bl, reason);
3858 else
3859 val = 0;
3860 }
3861 else
3862 {
3863 /* No -- not inserted, so no need to remove. No error. */
3864 val = 0;
3865 }
3866 }
3867
3868 /* In some cases, we might not be able to remove a breakpoint in
3869 a shared library that has already been removed, but we have
3870 not yet processed the shlib unload event. Similarly for an
3871 unloaded add-symbol-file object - the user might not yet have
3872 had the chance to remove-symbol-file it. shlib_disabled will
3873 be set if the library/object has already been removed, but
3874 the breakpoint hasn't been uninserted yet, e.g., after
3875 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3876 always-inserted mode. */
3877 if (val
3878 && (bl->loc_type == bp_loc_software_breakpoint
3879 && (bl->shlib_disabled
3880 || solib_name_from_address (bl->pspace, bl->address)
3881 || shared_objfile_contains_address_p (bl->pspace,
3882 bl->address))))
3883 val = 0;
3884
3885 if (val)
3886 return val;
3887 bl->inserted = (reason == DETACH_BREAKPOINT);
3888 }
3889 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3890 {
3891 gdb_assert (bl->owner->ops != NULL
3892 && bl->owner->ops->remove_location != NULL);
3893
3894 bl->inserted = (reason == DETACH_BREAKPOINT);
3895 bl->owner->ops->remove_location (bl, reason);
3896
3897 /* Failure to remove any of the hardware watchpoints comes here. */
3898 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3899 warning (_("Could not remove hardware watchpoint %d."),
3900 bl->owner->number);
3901 }
3902 else if (bl->owner->type == bp_catchpoint
3903 && breakpoint_enabled (bl->owner)
3904 && !bl->duplicate)
3905 {
3906 gdb_assert (bl->owner->ops != NULL
3907 && bl->owner->ops->remove_location != NULL);
3908
3909 val = bl->owner->ops->remove_location (bl, reason);
3910 if (val)
3911 return val;
3912
3913 bl->inserted = (reason == DETACH_BREAKPOINT);
3914 }
3915
3916 return 0;
3917 }
3918
3919 static int
3920 remove_breakpoint (struct bp_location *bl)
3921 {
3922 /* BL is never in moribund_locations by our callers. */
3923 gdb_assert (bl->owner != NULL);
3924
3925 /* The type of none suggests that owner is actually deleted.
3926 This should not ever happen. */
3927 gdb_assert (bl->owner->type != bp_none);
3928
3929 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3930
3931 switch_to_program_space_and_thread (bl->pspace);
3932
3933 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
3934 }
3935
3936 /* Clear the "inserted" flag in all breakpoints. */
3937
3938 void
3939 mark_breakpoints_out (void)
3940 {
3941 struct bp_location *bl, **blp_tmp;
3942
3943 ALL_BP_LOCATIONS (bl, blp_tmp)
3944 if (bl->pspace == current_program_space)
3945 bl->inserted = 0;
3946 }
3947
3948 /* Clear the "inserted" flag in all breakpoints and delete any
3949 breakpoints which should go away between runs of the program.
3950
3951 Plus other such housekeeping that has to be done for breakpoints
3952 between runs.
3953
3954 Note: this function gets called at the end of a run (by
3955 generic_mourn_inferior) and when a run begins (by
3956 init_wait_for_inferior). */
3957
3958
3959
3960 void
3961 breakpoint_init_inferior (enum inf_context context)
3962 {
3963 struct breakpoint *b, *b_tmp;
3964 struct program_space *pspace = current_program_space;
3965
3966 /* If breakpoint locations are shared across processes, then there's
3967 nothing to do. */
3968 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
3969 return;
3970
3971 mark_breakpoints_out ();
3972
3973 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3974 {
3975 if (b->loc && b->loc->pspace != pspace)
3976 continue;
3977
3978 switch (b->type)
3979 {
3980 case bp_call_dummy:
3981 case bp_longjmp_call_dummy:
3982
3983 /* If the call dummy breakpoint is at the entry point it will
3984 cause problems when the inferior is rerun, so we better get
3985 rid of it. */
3986
3987 case bp_watchpoint_scope:
3988
3989 /* Also get rid of scope breakpoints. */
3990
3991 case bp_shlib_event:
3992
3993 /* Also remove solib event breakpoints. Their addresses may
3994 have changed since the last time we ran the program.
3995 Actually we may now be debugging against different target;
3996 and so the solib backend that installed this breakpoint may
3997 not be used in by the target. E.g.,
3998
3999 (gdb) file prog-linux
4000 (gdb) run # native linux target
4001 ...
4002 (gdb) kill
4003 (gdb) file prog-win.exe
4004 (gdb) tar rem :9999 # remote Windows gdbserver.
4005 */
4006
4007 case bp_step_resume:
4008
4009 /* Also remove step-resume breakpoints. */
4010
4011 case bp_single_step:
4012
4013 /* Also remove single-step breakpoints. */
4014
4015 delete_breakpoint (b);
4016 break;
4017
4018 case bp_watchpoint:
4019 case bp_hardware_watchpoint:
4020 case bp_read_watchpoint:
4021 case bp_access_watchpoint:
4022 {
4023 struct watchpoint *w = (struct watchpoint *) b;
4024
4025 /* Likewise for watchpoints on local expressions. */
4026 if (w->exp_valid_block != NULL)
4027 delete_breakpoint (b);
4028 else
4029 {
4030 /* Get rid of existing locations, which are no longer
4031 valid. New ones will be created in
4032 update_watchpoint, when the inferior is restarted.
4033 The next update_global_location_list call will
4034 garbage collect them. */
4035 b->loc = NULL;
4036
4037 if (context == inf_starting)
4038 {
4039 /* Reset val field to force reread of starting value in
4040 insert_breakpoints. */
4041 w->val.reset (nullptr);
4042 w->val_valid = false;
4043 }
4044 }
4045 }
4046 break;
4047 default:
4048 break;
4049 }
4050 }
4051
4052 /* Get rid of the moribund locations. */
4053 for (bp_location *bl : moribund_locations)
4054 decref_bp_location (&bl);
4055 moribund_locations.clear ();
4056 }
4057
4058 /* These functions concern about actual breakpoints inserted in the
4059 target --- to e.g. check if we need to do decr_pc adjustment or if
4060 we need to hop over the bkpt --- so we check for address space
4061 match, not program space. */
4062
4063 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4064 exists at PC. It returns ordinary_breakpoint_here if it's an
4065 ordinary breakpoint, or permanent_breakpoint_here if it's a
4066 permanent breakpoint.
4067 - When continuing from a location with an ordinary breakpoint, we
4068 actually single step once before calling insert_breakpoints.
4069 - When continuing from a location with a permanent breakpoint, we
4070 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4071 the target, to advance the PC past the breakpoint. */
4072
4073 enum breakpoint_here
4074 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4075 {
4076 struct bp_location *bl, **blp_tmp;
4077 int any_breakpoint_here = 0;
4078
4079 ALL_BP_LOCATIONS (bl, blp_tmp)
4080 {
4081 if (bl->loc_type != bp_loc_software_breakpoint
4082 && bl->loc_type != bp_loc_hardware_breakpoint)
4083 continue;
4084
4085 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4086 if ((breakpoint_enabled (bl->owner)
4087 || bl->permanent)
4088 && breakpoint_location_address_match (bl, aspace, pc))
4089 {
4090 if (overlay_debugging
4091 && section_is_overlay (bl->section)
4092 && !section_is_mapped (bl->section))
4093 continue; /* unmapped overlay -- can't be a match */
4094 else if (bl->permanent)
4095 return permanent_breakpoint_here;
4096 else
4097 any_breakpoint_here = 1;
4098 }
4099 }
4100
4101 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4102 }
4103
4104 /* See breakpoint.h. */
4105
4106 int
4107 breakpoint_in_range_p (const address_space *aspace,
4108 CORE_ADDR addr, ULONGEST len)
4109 {
4110 struct bp_location *bl, **blp_tmp;
4111
4112 ALL_BP_LOCATIONS (bl, blp_tmp)
4113 {
4114 if (bl->loc_type != bp_loc_software_breakpoint
4115 && bl->loc_type != bp_loc_hardware_breakpoint)
4116 continue;
4117
4118 if ((breakpoint_enabled (bl->owner)
4119 || bl->permanent)
4120 && breakpoint_location_address_range_overlap (bl, aspace,
4121 addr, len))
4122 {
4123 if (overlay_debugging
4124 && section_is_overlay (bl->section)
4125 && !section_is_mapped (bl->section))
4126 {
4127 /* Unmapped overlay -- can't be a match. */
4128 continue;
4129 }
4130
4131 return 1;
4132 }
4133 }
4134
4135 return 0;
4136 }
4137
4138 /* Return true if there's a moribund breakpoint at PC. */
4139
4140 int
4141 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4142 {
4143 for (bp_location *loc : moribund_locations)
4144 if (breakpoint_location_address_match (loc, aspace, pc))
4145 return 1;
4146
4147 return 0;
4148 }
4149
4150 /* Returns non-zero iff BL is inserted at PC, in address space
4151 ASPACE. */
4152
4153 static int
4154 bp_location_inserted_here_p (struct bp_location *bl,
4155 const address_space *aspace, CORE_ADDR pc)
4156 {
4157 if (bl->inserted
4158 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4159 aspace, pc))
4160 {
4161 if (overlay_debugging
4162 && section_is_overlay (bl->section)
4163 && !section_is_mapped (bl->section))
4164 return 0; /* unmapped overlay -- can't be a match */
4165 else
4166 return 1;
4167 }
4168 return 0;
4169 }
4170
4171 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4172
4173 int
4174 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4175 {
4176 struct bp_location **blp, **blp_tmp = NULL;
4177
4178 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4179 {
4180 struct bp_location *bl = *blp;
4181
4182 if (bl->loc_type != bp_loc_software_breakpoint
4183 && bl->loc_type != bp_loc_hardware_breakpoint)
4184 continue;
4185
4186 if (bp_location_inserted_here_p (bl, aspace, pc))
4187 return 1;
4188 }
4189 return 0;
4190 }
4191
4192 /* This function returns non-zero iff there is a software breakpoint
4193 inserted at PC. */
4194
4195 int
4196 software_breakpoint_inserted_here_p (const address_space *aspace,
4197 CORE_ADDR pc)
4198 {
4199 struct bp_location **blp, **blp_tmp = NULL;
4200
4201 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4202 {
4203 struct bp_location *bl = *blp;
4204
4205 if (bl->loc_type != bp_loc_software_breakpoint)
4206 continue;
4207
4208 if (bp_location_inserted_here_p (bl, aspace, pc))
4209 return 1;
4210 }
4211
4212 return 0;
4213 }
4214
4215 /* See breakpoint.h. */
4216
4217 int
4218 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4219 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_hardware_breakpoint)
4228 continue;
4229
4230 if (bp_location_inserted_here_p (bl, aspace, pc))
4231 return 1;
4232 }
4233
4234 return 0;
4235 }
4236
4237 int
4238 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4239 CORE_ADDR addr, ULONGEST len)
4240 {
4241 struct breakpoint *bpt;
4242
4243 ALL_BREAKPOINTS (bpt)
4244 {
4245 struct bp_location *loc;
4246
4247 if (bpt->type != bp_hardware_watchpoint
4248 && bpt->type != bp_access_watchpoint)
4249 continue;
4250
4251 if (!breakpoint_enabled (bpt))
4252 continue;
4253
4254 for (loc = bpt->loc; loc; loc = loc->next)
4255 if (loc->pspace->aspace == aspace && loc->inserted)
4256 {
4257 CORE_ADDR l, h;
4258
4259 /* Check for intersection. */
4260 l = std::max<CORE_ADDR> (loc->address, addr);
4261 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4262 if (l < h)
4263 return 1;
4264 }
4265 }
4266 return 0;
4267 }
4268
4269 /* See breakpoint.h. */
4270
4271 bool
4272 is_catchpoint (struct breakpoint *b)
4273 {
4274 return (b->type == bp_catchpoint);
4275 }
4276
4277 /* Clear a bpstat so that it says we are not at any breakpoint.
4278 Also free any storage that is part of a bpstat. */
4279
4280 void
4281 bpstat_clear (bpstat *bsp)
4282 {
4283 bpstat p;
4284 bpstat q;
4285
4286 if (bsp == 0)
4287 return;
4288 p = *bsp;
4289 while (p != NULL)
4290 {
4291 q = p->next;
4292 delete p;
4293 p = q;
4294 }
4295 *bsp = NULL;
4296 }
4297
4298 bpstats::bpstats (const bpstats &other)
4299 : next (NULL),
4300 bp_location_at (other.bp_location_at),
4301 breakpoint_at (other.breakpoint_at),
4302 commands (other.commands),
4303 print (other.print),
4304 stop (other.stop),
4305 print_it (other.print_it)
4306 {
4307 if (other.old_val != NULL)
4308 old_val = release_value (value_copy (other.old_val.get ()));
4309 }
4310
4311 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4312 is part of the bpstat is copied as well. */
4313
4314 bpstat
4315 bpstat_copy (bpstat bs)
4316 {
4317 bpstat p = NULL;
4318 bpstat tmp;
4319 bpstat retval = NULL;
4320
4321 if (bs == NULL)
4322 return bs;
4323
4324 for (; bs != NULL; bs = bs->next)
4325 {
4326 tmp = new bpstats (*bs);
4327
4328 if (p == NULL)
4329 /* This is the first thing in the chain. */
4330 retval = tmp;
4331 else
4332 p->next = tmp;
4333 p = tmp;
4334 }
4335 p->next = NULL;
4336 return retval;
4337 }
4338
4339 /* Find the bpstat associated with this breakpoint. */
4340
4341 bpstat
4342 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4343 {
4344 if (bsp == NULL)
4345 return NULL;
4346
4347 for (; bsp != NULL; bsp = bsp->next)
4348 {
4349 if (bsp->breakpoint_at == breakpoint)
4350 return bsp;
4351 }
4352 return NULL;
4353 }
4354
4355 /* See breakpoint.h. */
4356
4357 bool
4358 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4359 {
4360 for (; bsp != NULL; bsp = bsp->next)
4361 {
4362 if (bsp->breakpoint_at == NULL)
4363 {
4364 /* A moribund location can never explain a signal other than
4365 GDB_SIGNAL_TRAP. */
4366 if (sig == GDB_SIGNAL_TRAP)
4367 return true;
4368 }
4369 else
4370 {
4371 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4372 sig))
4373 return true;
4374 }
4375 }
4376
4377 return false;
4378 }
4379
4380 /* Put in *NUM the breakpoint number of the first breakpoint we are
4381 stopped at. *BSP upon return is a bpstat which points to the
4382 remaining breakpoints stopped at (but which is not guaranteed to be
4383 good for anything but further calls to bpstat_num).
4384
4385 Return 0 if passed a bpstat which does not indicate any breakpoints.
4386 Return -1 if stopped at a breakpoint that has been deleted since
4387 we set it.
4388 Return 1 otherwise. */
4389
4390 int
4391 bpstat_num (bpstat *bsp, int *num)
4392 {
4393 struct breakpoint *b;
4394
4395 if ((*bsp) == NULL)
4396 return 0; /* No more breakpoint values */
4397
4398 /* We assume we'll never have several bpstats that correspond to a
4399 single breakpoint -- otherwise, this function might return the
4400 same number more than once and this will look ugly. */
4401 b = (*bsp)->breakpoint_at;
4402 *bsp = (*bsp)->next;
4403 if (b == NULL)
4404 return -1; /* breakpoint that's been deleted since */
4405
4406 *num = b->number; /* We have its number */
4407 return 1;
4408 }
4409
4410 /* See breakpoint.h. */
4411
4412 void
4413 bpstat_clear_actions (void)
4414 {
4415 bpstat bs;
4416
4417 if (inferior_ptid == null_ptid)
4418 return;
4419
4420 thread_info *tp = inferior_thread ();
4421 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4422 {
4423 bs->commands = NULL;
4424 bs->old_val.reset (nullptr);
4425 }
4426 }
4427
4428 /* Called when a command is about to proceed the inferior. */
4429
4430 static void
4431 breakpoint_about_to_proceed (void)
4432 {
4433 if (inferior_ptid != null_ptid)
4434 {
4435 struct thread_info *tp = inferior_thread ();
4436
4437 /* Allow inferior function calls in breakpoint commands to not
4438 interrupt the command list. When the call finishes
4439 successfully, the inferior will be standing at the same
4440 breakpoint as if nothing happened. */
4441 if (tp->control.in_infcall)
4442 return;
4443 }
4444
4445 breakpoint_proceeded = 1;
4446 }
4447
4448 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4449 or its equivalent. */
4450
4451 static int
4452 command_line_is_silent (struct command_line *cmd)
4453 {
4454 return cmd && (strcmp ("silent", cmd->line) == 0);
4455 }
4456
4457 /* Execute all the commands associated with all the breakpoints at
4458 this location. Any of these commands could cause the process to
4459 proceed beyond this point, etc. We look out for such changes by
4460 checking the global "breakpoint_proceeded" after each command.
4461
4462 Returns true if a breakpoint command resumed the inferior. In that
4463 case, it is the caller's responsibility to recall it again with the
4464 bpstat of the current thread. */
4465
4466 static int
4467 bpstat_do_actions_1 (bpstat *bsp)
4468 {
4469 bpstat bs;
4470 int again = 0;
4471
4472 /* Avoid endless recursion if a `source' command is contained
4473 in bs->commands. */
4474 if (executing_breakpoint_commands)
4475 return 0;
4476
4477 scoped_restore save_executing
4478 = make_scoped_restore (&executing_breakpoint_commands, 1);
4479
4480 scoped_restore preventer = prevent_dont_repeat ();
4481
4482 /* This pointer will iterate over the list of bpstat's. */
4483 bs = *bsp;
4484
4485 breakpoint_proceeded = 0;
4486 for (; bs != NULL; bs = bs->next)
4487 {
4488 struct command_line *cmd = NULL;
4489
4490 /* Take ownership of the BSP's command tree, if it has one.
4491
4492 The command tree could legitimately contain commands like
4493 'step' and 'next', which call clear_proceed_status, which
4494 frees stop_bpstat's command tree. To make sure this doesn't
4495 free the tree we're executing out from under us, we need to
4496 take ownership of the tree ourselves. Since a given bpstat's
4497 commands are only executed once, we don't need to copy it; we
4498 can clear the pointer in the bpstat, and make sure we free
4499 the tree when we're done. */
4500 counted_command_line ccmd = bs->commands;
4501 bs->commands = NULL;
4502 if (ccmd != NULL)
4503 cmd = ccmd.get ();
4504 if (command_line_is_silent (cmd))
4505 {
4506 /* The action has been already done by bpstat_stop_status. */
4507 cmd = cmd->next;
4508 }
4509
4510 while (cmd != NULL)
4511 {
4512 execute_control_command (cmd);
4513
4514 if (breakpoint_proceeded)
4515 break;
4516 else
4517 cmd = cmd->next;
4518 }
4519
4520 if (breakpoint_proceeded)
4521 {
4522 if (current_ui->async)
4523 /* If we are in async mode, then the target might be still
4524 running, not stopped at any breakpoint, so nothing for
4525 us to do here -- just return to the event loop. */
4526 ;
4527 else
4528 /* In sync mode, when execute_control_command returns
4529 we're already standing on the next breakpoint.
4530 Breakpoint commands for that stop were not run, since
4531 execute_command does not run breakpoint commands --
4532 only command_line_handler does, but that one is not
4533 involved in execution of breakpoint commands. So, we
4534 can now execute breakpoint commands. It should be
4535 noted that making execute_command do bpstat actions is
4536 not an option -- in this case we'll have recursive
4537 invocation of bpstat for each breakpoint with a
4538 command, and can easily blow up GDB stack. Instead, we
4539 return true, which will trigger the caller to recall us
4540 with the new stop_bpstat. */
4541 again = 1;
4542 break;
4543 }
4544 }
4545 return again;
4546 }
4547
4548 /* Helper for bpstat_do_actions. Get the current thread, if there's
4549 one, is alive and has execution. Return NULL otherwise. */
4550
4551 static thread_info *
4552 get_bpstat_thread ()
4553 {
4554 if (inferior_ptid == null_ptid || !target_has_execution ())
4555 return NULL;
4556
4557 thread_info *tp = inferior_thread ();
4558 if (tp->state == THREAD_EXITED || tp->executing)
4559 return NULL;
4560 return tp;
4561 }
4562
4563 void
4564 bpstat_do_actions (void)
4565 {
4566 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4567 thread_info *tp;
4568
4569 /* Do any commands attached to breakpoint we are stopped at. */
4570 while ((tp = get_bpstat_thread ()) != NULL)
4571 {
4572 /* Since in sync mode, bpstat_do_actions may resume the
4573 inferior, and only return when it is stopped at the next
4574 breakpoint, we keep doing breakpoint actions until it returns
4575 false to indicate the inferior was not resumed. */
4576 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4577 break;
4578 }
4579
4580 cleanup_if_error.release ();
4581 }
4582
4583 /* Print out the (old or new) value associated with a watchpoint. */
4584
4585 static void
4586 watchpoint_value_print (struct value *val, struct ui_file *stream)
4587 {
4588 if (val == NULL)
4589 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4590 else
4591 {
4592 struct value_print_options opts;
4593 get_user_print_options (&opts);
4594 value_print (val, stream, &opts);
4595 }
4596 }
4597
4598 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4599 debugging multiple threads. */
4600
4601 void
4602 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4603 {
4604 if (uiout->is_mi_like_p ())
4605 return;
4606
4607 uiout->text ("\n");
4608
4609 if (show_thread_that_caused_stop ())
4610 {
4611 const char *name;
4612 struct thread_info *thr = inferior_thread ();
4613
4614 uiout->text ("Thread ");
4615 uiout->field_string ("thread-id", print_thread_id (thr));
4616
4617 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4618 if (name != NULL)
4619 {
4620 uiout->text (" \"");
4621 uiout->field_string ("name", name);
4622 uiout->text ("\"");
4623 }
4624
4625 uiout->text (" hit ");
4626 }
4627 }
4628
4629 /* Generic routine for printing messages indicating why we
4630 stopped. The behavior of this function depends on the value
4631 'print_it' in the bpstat structure. Under some circumstances we
4632 may decide not to print anything here and delegate the task to
4633 normal_stop(). */
4634
4635 static enum print_stop_action
4636 print_bp_stop_message (bpstat bs)
4637 {
4638 switch (bs->print_it)
4639 {
4640 case print_it_noop:
4641 /* Nothing should be printed for this bpstat entry. */
4642 return PRINT_UNKNOWN;
4643 break;
4644
4645 case print_it_done:
4646 /* We still want to print the frame, but we already printed the
4647 relevant messages. */
4648 return PRINT_SRC_AND_LOC;
4649 break;
4650
4651 case print_it_normal:
4652 {
4653 struct breakpoint *b = bs->breakpoint_at;
4654
4655 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4656 which has since been deleted. */
4657 if (b == NULL)
4658 return PRINT_UNKNOWN;
4659
4660 /* Normal case. Call the breakpoint's print_it method. */
4661 return b->ops->print_it (bs);
4662 }
4663 break;
4664
4665 default:
4666 internal_error (__FILE__, __LINE__,
4667 _("print_bp_stop_message: unrecognized enum value"));
4668 break;
4669 }
4670 }
4671
4672 /* A helper function that prints a shared library stopped event. */
4673
4674 static void
4675 print_solib_event (int is_catchpoint)
4676 {
4677 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4678 bool any_added = !current_program_space->added_solibs.empty ();
4679
4680 if (!is_catchpoint)
4681 {
4682 if (any_added || any_deleted)
4683 current_uiout->text (_("Stopped due to shared library event:\n"));
4684 else
4685 current_uiout->text (_("Stopped due to shared library event (no "
4686 "libraries added or removed)\n"));
4687 }
4688
4689 if (current_uiout->is_mi_like_p ())
4690 current_uiout->field_string ("reason",
4691 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4692
4693 if (any_deleted)
4694 {
4695 current_uiout->text (_(" Inferior unloaded "));
4696 ui_out_emit_list list_emitter (current_uiout, "removed");
4697 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4698 {
4699 const std::string &name = current_program_space->deleted_solibs[ix];
4700
4701 if (ix > 0)
4702 current_uiout->text (" ");
4703 current_uiout->field_string ("library", name);
4704 current_uiout->text ("\n");
4705 }
4706 }
4707
4708 if (any_added)
4709 {
4710 current_uiout->text (_(" Inferior loaded "));
4711 ui_out_emit_list list_emitter (current_uiout, "added");
4712 bool first = true;
4713 for (so_list *iter : current_program_space->added_solibs)
4714 {
4715 if (!first)
4716 current_uiout->text (" ");
4717 first = false;
4718 current_uiout->field_string ("library", iter->so_name);
4719 current_uiout->text ("\n");
4720 }
4721 }
4722 }
4723
4724 /* Print a message indicating what happened. This is called from
4725 normal_stop(). The input to this routine is the head of the bpstat
4726 list - a list of the eventpoints that caused this stop. KIND is
4727 the target_waitkind for the stopping event. This
4728 routine calls the generic print routine for printing a message
4729 about reasons for stopping. This will print (for example) the
4730 "Breakpoint n," part of the output. The return value of this
4731 routine is one of:
4732
4733 PRINT_UNKNOWN: Means we printed nothing.
4734 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4735 code to print the location. An example is
4736 "Breakpoint 1, " which should be followed by
4737 the location.
4738 PRINT_SRC_ONLY: Means we printed something, but there is no need
4739 to also print the location part of the message.
4740 An example is the catch/throw messages, which
4741 don't require a location appended to the end.
4742 PRINT_NOTHING: We have done some printing and we don't need any
4743 further info to be printed. */
4744
4745 enum print_stop_action
4746 bpstat_print (bpstat bs, int kind)
4747 {
4748 enum print_stop_action val;
4749
4750 /* Maybe another breakpoint in the chain caused us to stop.
4751 (Currently all watchpoints go on the bpstat whether hit or not.
4752 That probably could (should) be changed, provided care is taken
4753 with respect to bpstat_explains_signal). */
4754 for (; bs; bs = bs->next)
4755 {
4756 val = print_bp_stop_message (bs);
4757 if (val == PRINT_SRC_ONLY
4758 || val == PRINT_SRC_AND_LOC
4759 || val == PRINT_NOTHING)
4760 return val;
4761 }
4762
4763 /* If we had hit a shared library event breakpoint,
4764 print_bp_stop_message would print out this message. If we hit an
4765 OS-level shared library event, do the same thing. */
4766 if (kind == TARGET_WAITKIND_LOADED)
4767 {
4768 print_solib_event (0);
4769 return PRINT_NOTHING;
4770 }
4771
4772 /* We reached the end of the chain, or we got a null BS to start
4773 with and nothing was printed. */
4774 return PRINT_UNKNOWN;
4775 }
4776
4777 /* Evaluate the boolean expression EXP and return the result. */
4778
4779 static bool
4780 breakpoint_cond_eval (expression *exp)
4781 {
4782 struct value *mark = value_mark ();
4783 bool res = value_true (evaluate_expression (exp));
4784
4785 value_free_to_mark (mark);
4786 return res;
4787 }
4788
4789 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4790
4791 bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4792 : next (NULL),
4793 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
4794 breakpoint_at (bl->owner),
4795 commands (NULL),
4796 print (0),
4797 stop (0),
4798 print_it (print_it_normal)
4799 {
4800 **bs_link_pointer = this;
4801 *bs_link_pointer = &next;
4802 }
4803
4804 bpstats::bpstats ()
4805 : next (NULL),
4806 breakpoint_at (NULL),
4807 commands (NULL),
4808 print (0),
4809 stop (0),
4810 print_it (print_it_normal)
4811 {
4812 }
4813 \f
4814 /* The target has stopped with waitstatus WS. Check if any hardware
4815 watchpoints have triggered, according to the target. */
4816
4817 int
4818 watchpoints_triggered (struct target_waitstatus *ws)
4819 {
4820 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
4821 CORE_ADDR addr;
4822 struct breakpoint *b;
4823
4824 if (!stopped_by_watchpoint)
4825 {
4826 /* We were not stopped by a watchpoint. Mark all watchpoints
4827 as not triggered. */
4828 ALL_BREAKPOINTS (b)
4829 if (is_hardware_watchpoint (b))
4830 {
4831 struct watchpoint *w = (struct watchpoint *) b;
4832
4833 w->watchpoint_triggered = watch_triggered_no;
4834 }
4835
4836 return 0;
4837 }
4838
4839 if (!target_stopped_data_address (current_top_target (), &addr))
4840 {
4841 /* We were stopped by a watchpoint, but we don't know where.
4842 Mark all watchpoints as unknown. */
4843 ALL_BREAKPOINTS (b)
4844 if (is_hardware_watchpoint (b))
4845 {
4846 struct watchpoint *w = (struct watchpoint *) b;
4847
4848 w->watchpoint_triggered = watch_triggered_unknown;
4849 }
4850
4851 return 1;
4852 }
4853
4854 /* The target could report the data address. Mark watchpoints
4855 affected by this data address as triggered, and all others as not
4856 triggered. */
4857
4858 ALL_BREAKPOINTS (b)
4859 if (is_hardware_watchpoint (b))
4860 {
4861 struct watchpoint *w = (struct watchpoint *) b;
4862 struct bp_location *loc;
4863
4864 w->watchpoint_triggered = watch_triggered_no;
4865 for (loc = b->loc; loc; loc = loc->next)
4866 {
4867 if (is_masked_watchpoint (b))
4868 {
4869 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4870 CORE_ADDR start = loc->address & w->hw_wp_mask;
4871
4872 if (newaddr == start)
4873 {
4874 w->watchpoint_triggered = watch_triggered_yes;
4875 break;
4876 }
4877 }
4878 /* Exact match not required. Within range is sufficient. */
4879 else if (target_watchpoint_addr_within_range (current_top_target (),
4880 addr, loc->address,
4881 loc->length))
4882 {
4883 w->watchpoint_triggered = watch_triggered_yes;
4884 break;
4885 }
4886 }
4887 }
4888
4889 return 1;
4890 }
4891
4892 /* Possible return values for watchpoint_check. */
4893 enum wp_check_result
4894 {
4895 /* The watchpoint has been deleted. */
4896 WP_DELETED = 1,
4897
4898 /* The value has changed. */
4899 WP_VALUE_CHANGED = 2,
4900
4901 /* The value has not changed. */
4902 WP_VALUE_NOT_CHANGED = 3,
4903
4904 /* Ignore this watchpoint, no matter if the value changed or not. */
4905 WP_IGNORE = 4,
4906 };
4907
4908 #define BP_TEMPFLAG 1
4909 #define BP_HARDWAREFLAG 2
4910
4911 /* Evaluate watchpoint condition expression and check if its value
4912 changed. */
4913
4914 static wp_check_result
4915 watchpoint_check (bpstat bs)
4916 {
4917 struct watchpoint *b;
4918 struct frame_info *fr;
4919 int within_current_scope;
4920
4921 /* BS is built from an existing struct breakpoint. */
4922 gdb_assert (bs->breakpoint_at != NULL);
4923 b = (struct watchpoint *) bs->breakpoint_at;
4924
4925 /* If this is a local watchpoint, we only want to check if the
4926 watchpoint frame is in scope if the current thread is the thread
4927 that was used to create the watchpoint. */
4928 if (!watchpoint_in_thread_scope (b))
4929 return WP_IGNORE;
4930
4931 if (b->exp_valid_block == NULL)
4932 within_current_scope = 1;
4933 else
4934 {
4935 struct frame_info *frame = get_current_frame ();
4936 struct gdbarch *frame_arch = get_frame_arch (frame);
4937 CORE_ADDR frame_pc = get_frame_pc (frame);
4938
4939 /* stack_frame_destroyed_p() returns a non-zero value if we're
4940 still in the function but the stack frame has already been
4941 invalidated. Since we can't rely on the values of local
4942 variables after the stack has been destroyed, we are treating
4943 the watchpoint in that state as `not changed' without further
4944 checking. Don't mark watchpoints as changed if the current
4945 frame is in an epilogue - even if they are in some other
4946 frame, our view of the stack is likely to be wrong and
4947 frame_find_by_id could error out. */
4948 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
4949 return WP_IGNORE;
4950
4951 fr = frame_find_by_id (b->watchpoint_frame);
4952 within_current_scope = (fr != NULL);
4953
4954 /* If we've gotten confused in the unwinder, we might have
4955 returned a frame that can't describe this variable. */
4956 if (within_current_scope)
4957 {
4958 struct symbol *function;
4959
4960 function = get_frame_function (fr);
4961 if (function == NULL
4962 || !contained_in (b->exp_valid_block,
4963 SYMBOL_BLOCK_VALUE (function)))
4964 within_current_scope = 0;
4965 }
4966
4967 if (within_current_scope)
4968 /* If we end up stopping, the current frame will get selected
4969 in normal_stop. So this call to select_frame won't affect
4970 the user. */
4971 select_frame (fr);
4972 }
4973
4974 if (within_current_scope)
4975 {
4976 /* We use value_{,free_to_}mark because it could be a *long*
4977 time before we return to the command level and call
4978 free_all_values. We can't call free_all_values because we
4979 might be in the middle of evaluating a function call. */
4980
4981 int pc = 0;
4982 struct value *mark;
4983 struct value *new_val;
4984
4985 if (is_masked_watchpoint (b))
4986 /* Since we don't know the exact trigger address (from
4987 stopped_data_address), just tell the user we've triggered
4988 a mask watchpoint. */
4989 return WP_VALUE_CHANGED;
4990
4991 mark = value_mark ();
4992 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, false);
4993
4994 if (b->val_bitsize != 0)
4995 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4996
4997 /* We use value_equal_contents instead of value_equal because
4998 the latter coerces an array to a pointer, thus comparing just
4999 the address of the array instead of its contents. This is
5000 not what we want. */
5001 if ((b->val != NULL) != (new_val != NULL)
5002 || (b->val != NULL && !value_equal_contents (b->val.get (),
5003 new_val)))
5004 {
5005 bs->old_val = b->val;
5006 b->val = release_value (new_val);
5007 b->val_valid = true;
5008 if (new_val != NULL)
5009 value_free_to_mark (mark);
5010 return WP_VALUE_CHANGED;
5011 }
5012 else
5013 {
5014 /* Nothing changed. */
5015 value_free_to_mark (mark);
5016 return WP_VALUE_NOT_CHANGED;
5017 }
5018 }
5019 else
5020 {
5021 /* This seems like the only logical thing to do because
5022 if we temporarily ignored the watchpoint, then when
5023 we reenter the block in which it is valid it contains
5024 garbage (in the case of a function, it may have two
5025 garbage values, one before and one after the prologue).
5026 So we can't even detect the first assignment to it and
5027 watch after that (since the garbage may or may not equal
5028 the first value assigned). */
5029 /* We print all the stop information in
5030 breakpoint_ops->print_it, but in this case, by the time we
5031 call breakpoint_ops->print_it this bp will be deleted
5032 already. So we have no choice but print the information
5033 here. */
5034
5035 SWITCH_THRU_ALL_UIS ()
5036 {
5037 struct ui_out *uiout = current_uiout;
5038
5039 if (uiout->is_mi_like_p ())
5040 uiout->field_string
5041 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5042 uiout->message ("\nWatchpoint %pF deleted because the program has "
5043 "left the block in\n"
5044 "which its expression is valid.\n",
5045 signed_field ("wpnum", b->number));
5046 }
5047
5048 /* Make sure the watchpoint's commands aren't executed. */
5049 b->commands = NULL;
5050 watchpoint_del_at_next_stop (b);
5051
5052 return WP_DELETED;
5053 }
5054 }
5055
5056 /* Return true if it looks like target has stopped due to hitting
5057 breakpoint location BL. This function does not check if we should
5058 stop, only if BL explains the stop. */
5059
5060 static int
5061 bpstat_check_location (const struct bp_location *bl,
5062 const address_space *aspace, CORE_ADDR bp_addr,
5063 const struct target_waitstatus *ws)
5064 {
5065 struct breakpoint *b = bl->owner;
5066
5067 /* BL is from an existing breakpoint. */
5068 gdb_assert (b != NULL);
5069
5070 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5071 }
5072
5073 /* Determine if the watched values have actually changed, and we
5074 should stop. If not, set BS->stop to 0. */
5075
5076 static void
5077 bpstat_check_watchpoint (bpstat bs)
5078 {
5079 const struct bp_location *bl;
5080 struct watchpoint *b;
5081
5082 /* BS is built for existing struct breakpoint. */
5083 bl = bs->bp_location_at.get ();
5084 gdb_assert (bl != NULL);
5085 b = (struct watchpoint *) bs->breakpoint_at;
5086 gdb_assert (b != NULL);
5087
5088 {
5089 int must_check_value = 0;
5090
5091 if (b->type == bp_watchpoint)
5092 /* For a software watchpoint, we must always check the
5093 watched value. */
5094 must_check_value = 1;
5095 else if (b->watchpoint_triggered == watch_triggered_yes)
5096 /* We have a hardware watchpoint (read, write, or access)
5097 and the target earlier reported an address watched by
5098 this watchpoint. */
5099 must_check_value = 1;
5100 else if (b->watchpoint_triggered == watch_triggered_unknown
5101 && b->type == bp_hardware_watchpoint)
5102 /* We were stopped by a hardware watchpoint, but the target could
5103 not report the data address. We must check the watchpoint's
5104 value. Access and read watchpoints are out of luck; without
5105 a data address, we can't figure it out. */
5106 must_check_value = 1;
5107
5108 if (must_check_value)
5109 {
5110 wp_check_result e;
5111
5112 try
5113 {
5114 e = watchpoint_check (bs);
5115 }
5116 catch (const gdb_exception &ex)
5117 {
5118 exception_fprintf (gdb_stderr, ex,
5119 "Error evaluating expression "
5120 "for watchpoint %d\n",
5121 b->number);
5122
5123 SWITCH_THRU_ALL_UIS ()
5124 {
5125 printf_filtered (_("Watchpoint %d deleted.\n"),
5126 b->number);
5127 }
5128 watchpoint_del_at_next_stop (b);
5129 e = WP_DELETED;
5130 }
5131
5132 switch (e)
5133 {
5134 case WP_DELETED:
5135 /* We've already printed what needs to be printed. */
5136 bs->print_it = print_it_done;
5137 /* Stop. */
5138 break;
5139 case WP_IGNORE:
5140 bs->print_it = print_it_noop;
5141 bs->stop = 0;
5142 break;
5143 case WP_VALUE_CHANGED:
5144 if (b->type == bp_read_watchpoint)
5145 {
5146 /* There are two cases to consider here:
5147
5148 1. We're watching the triggered memory for reads.
5149 In that case, trust the target, and always report
5150 the watchpoint hit to the user. Even though
5151 reads don't cause value changes, the value may
5152 have changed since the last time it was read, and
5153 since we're not trapping writes, we will not see
5154 those, and as such we should ignore our notion of
5155 old value.
5156
5157 2. We're watching the triggered memory for both
5158 reads and writes. There are two ways this may
5159 happen:
5160
5161 2.1. This is a target that can't break on data
5162 reads only, but can break on accesses (reads or
5163 writes), such as e.g., x86. We detect this case
5164 at the time we try to insert read watchpoints.
5165
5166 2.2. Otherwise, the target supports read
5167 watchpoints, but, the user set an access or write
5168 watchpoint watching the same memory as this read
5169 watchpoint.
5170
5171 If we're watching memory writes as well as reads,
5172 ignore watchpoint hits when we find that the
5173 value hasn't changed, as reads don't cause
5174 changes. This still gives false positives when
5175 the program writes the same value to memory as
5176 what there was already in memory (we will confuse
5177 it for a read), but it's much better than
5178 nothing. */
5179
5180 int other_write_watchpoint = 0;
5181
5182 if (bl->watchpoint_type == hw_read)
5183 {
5184 struct breakpoint *other_b;
5185
5186 ALL_BREAKPOINTS (other_b)
5187 if (other_b->type == bp_hardware_watchpoint
5188 || other_b->type == bp_access_watchpoint)
5189 {
5190 struct watchpoint *other_w =
5191 (struct watchpoint *) other_b;
5192
5193 if (other_w->watchpoint_triggered
5194 == watch_triggered_yes)
5195 {
5196 other_write_watchpoint = 1;
5197 break;
5198 }
5199 }
5200 }
5201
5202 if (other_write_watchpoint
5203 || bl->watchpoint_type == hw_access)
5204 {
5205 /* We're watching the same memory for writes,
5206 and the value changed since the last time we
5207 updated it, so this trap must be for a write.
5208 Ignore it. */
5209 bs->print_it = print_it_noop;
5210 bs->stop = 0;
5211 }
5212 }
5213 break;
5214 case WP_VALUE_NOT_CHANGED:
5215 if (b->type == bp_hardware_watchpoint
5216 || b->type == bp_watchpoint)
5217 {
5218 /* Don't stop: write watchpoints shouldn't fire if
5219 the value hasn't changed. */
5220 bs->print_it = print_it_noop;
5221 bs->stop = 0;
5222 }
5223 /* Stop. */
5224 break;
5225 default:
5226 /* Can't happen. */
5227 break;
5228 }
5229 }
5230 else /* must_check_value == 0 */
5231 {
5232 /* This is a case where some watchpoint(s) triggered, but
5233 not at the address of this watchpoint, or else no
5234 watchpoint triggered after all. So don't print
5235 anything for this watchpoint. */
5236 bs->print_it = print_it_noop;
5237 bs->stop = 0;
5238 }
5239 }
5240 }
5241
5242 /* For breakpoints that are currently marked as telling gdb to stop,
5243 check conditions (condition proper, frame, thread and ignore count)
5244 of breakpoint referred to by BS. If we should not stop for this
5245 breakpoint, set BS->stop to 0. */
5246
5247 static void
5248 bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
5249 {
5250 const struct bp_location *bl;
5251 struct breakpoint *b;
5252 /* Assume stop. */
5253 bool condition_result = true;
5254 struct expression *cond;
5255
5256 gdb_assert (bs->stop);
5257
5258 /* BS is built for existing struct breakpoint. */
5259 bl = bs->bp_location_at.get ();
5260 gdb_assert (bl != NULL);
5261 b = bs->breakpoint_at;
5262 gdb_assert (b != NULL);
5263
5264 /* Even if the target evaluated the condition on its end and notified GDB, we
5265 need to do so again since GDB does not know if we stopped due to a
5266 breakpoint or a single step breakpoint. */
5267
5268 if (frame_id_p (b->frame_id)
5269 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5270 {
5271 bs->stop = 0;
5272 return;
5273 }
5274
5275 /* If this is a thread/task-specific breakpoint, don't waste cpu
5276 evaluating the condition if this isn't the specified
5277 thread/task. */
5278 if ((b->thread != -1 && b->thread != thread->global_num)
5279 || (b->task != 0 && b->task != ada_get_task_number (thread)))
5280 {
5281 bs->stop = 0;
5282 return;
5283 }
5284
5285 /* Evaluate extension language breakpoints that have a "stop" method
5286 implemented. */
5287 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5288
5289 if (is_watchpoint (b))
5290 {
5291 struct watchpoint *w = (struct watchpoint *) b;
5292
5293 cond = w->cond_exp.get ();
5294 }
5295 else
5296 cond = bl->cond.get ();
5297
5298 if (cond && b->disposition != disp_del_at_next_stop)
5299 {
5300 int within_current_scope = 1;
5301 struct watchpoint * w;
5302
5303 /* We use value_mark and value_free_to_mark because it could
5304 be a long time before we return to the command level and
5305 call free_all_values. We can't call free_all_values
5306 because we might be in the middle of evaluating a
5307 function call. */
5308 struct value *mark = value_mark ();
5309
5310 if (is_watchpoint (b))
5311 w = (struct watchpoint *) b;
5312 else
5313 w = NULL;
5314
5315 /* Need to select the frame, with all that implies so that
5316 the conditions will have the right context. Because we
5317 use the frame, we will not see an inlined function's
5318 variables when we arrive at a breakpoint at the start
5319 of the inlined function; the current frame will be the
5320 call site. */
5321 if (w == NULL || w->cond_exp_valid_block == NULL)
5322 select_frame (get_current_frame ());
5323 else
5324 {
5325 struct frame_info *frame;
5326
5327 /* For local watchpoint expressions, which particular
5328 instance of a local is being watched matters, so we
5329 keep track of the frame to evaluate the expression
5330 in. To evaluate the condition however, it doesn't
5331 really matter which instantiation of the function
5332 where the condition makes sense triggers the
5333 watchpoint. This allows an expression like "watch
5334 global if q > 10" set in `func', catch writes to
5335 global on all threads that call `func', or catch
5336 writes on all recursive calls of `func' by a single
5337 thread. We simply always evaluate the condition in
5338 the innermost frame that's executing where it makes
5339 sense to evaluate the condition. It seems
5340 intuitive. */
5341 frame = block_innermost_frame (w->cond_exp_valid_block);
5342 if (frame != NULL)
5343 select_frame (frame);
5344 else
5345 within_current_scope = 0;
5346 }
5347 if (within_current_scope)
5348 {
5349 try
5350 {
5351 condition_result = breakpoint_cond_eval (cond);
5352 }
5353 catch (const gdb_exception &ex)
5354 {
5355 exception_fprintf (gdb_stderr, ex,
5356 "Error in testing breakpoint condition:\n");
5357 }
5358 }
5359 else
5360 {
5361 warning (_("Watchpoint condition cannot be tested "
5362 "in the current scope"));
5363 /* If we failed to set the right context for this
5364 watchpoint, unconditionally report it. */
5365 }
5366 /* FIXME-someday, should give breakpoint #. */
5367 value_free_to_mark (mark);
5368 }
5369
5370 if (cond && !condition_result)
5371 {
5372 bs->stop = 0;
5373 }
5374 else if (b->ignore_count > 0)
5375 {
5376 b->ignore_count--;
5377 bs->stop = 0;
5378 /* Increase the hit count even though we don't stop. */
5379 ++(b->hit_count);
5380 gdb::observers::breakpoint_modified.notify (b);
5381 }
5382 }
5383
5384 /* Returns true if we need to track moribund locations of LOC's type
5385 on the current target. */
5386
5387 static int
5388 need_moribund_for_location_type (struct bp_location *loc)
5389 {
5390 return ((loc->loc_type == bp_loc_software_breakpoint
5391 && !target_supports_stopped_by_sw_breakpoint ())
5392 || (loc->loc_type == bp_loc_hardware_breakpoint
5393 && !target_supports_stopped_by_hw_breakpoint ()));
5394 }
5395
5396 /* See breakpoint.h. */
5397
5398 bpstat
5399 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5400 const struct target_waitstatus *ws)
5401 {
5402 struct breakpoint *b;
5403 bpstat bs_head = NULL, *bs_link = &bs_head;
5404
5405 ALL_BREAKPOINTS (b)
5406 {
5407 if (!breakpoint_enabled (b))
5408 continue;
5409
5410 for (bp_location *bl = b->loc; bl != NULL; bl = bl->next)
5411 {
5412 /* For hardware watchpoints, we look only at the first
5413 location. The watchpoint_check function will work on the
5414 entire expression, not the individual locations. For
5415 read watchpoints, the watchpoints_triggered function has
5416 checked all locations already. */
5417 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5418 break;
5419
5420 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
5421 continue;
5422
5423 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5424 continue;
5425
5426 /* Come here if it's a watchpoint, or if the break address
5427 matches. */
5428
5429 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5430 explain stop. */
5431
5432 /* Assume we stop. Should we find a watchpoint that is not
5433 actually triggered, or if the condition of the breakpoint
5434 evaluates as false, we'll reset 'stop' to 0. */
5435 bs->stop = 1;
5436 bs->print = 1;
5437
5438 /* If this is a scope breakpoint, mark the associated
5439 watchpoint as triggered so that we will handle the
5440 out-of-scope event. We'll get to the watchpoint next
5441 iteration. */
5442 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5443 {
5444 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5445
5446 w->watchpoint_triggered = watch_triggered_yes;
5447 }
5448 }
5449 }
5450
5451 /* Check if a moribund breakpoint explains the stop. */
5452 if (!target_supports_stopped_by_sw_breakpoint ()
5453 || !target_supports_stopped_by_hw_breakpoint ())
5454 {
5455 for (bp_location *loc : moribund_locations)
5456 {
5457 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5458 && need_moribund_for_location_type (loc))
5459 {
5460 bpstat bs = new bpstats (loc, &bs_link);
5461 /* For hits of moribund locations, we should just proceed. */
5462 bs->stop = 0;
5463 bs->print = 0;
5464 bs->print_it = print_it_noop;
5465 }
5466 }
5467 }
5468
5469 return bs_head;
5470 }
5471
5472 /* See breakpoint.h. */
5473
5474 bpstat
5475 bpstat_stop_status (const address_space *aspace,
5476 CORE_ADDR bp_addr, thread_info *thread,
5477 const struct target_waitstatus *ws,
5478 bpstat stop_chain)
5479 {
5480 struct breakpoint *b = NULL;
5481 /* First item of allocated bpstat's. */
5482 bpstat bs_head = stop_chain;
5483 bpstat bs;
5484 int need_remove_insert;
5485 int removed_any;
5486
5487 /* First, build the bpstat chain with locations that explain a
5488 target stop, while being careful to not set the target running,
5489 as that may invalidate locations (in particular watchpoint
5490 locations are recreated). Resuming will happen here with
5491 breakpoint conditions or watchpoint expressions that include
5492 inferior function calls. */
5493 if (bs_head == NULL)
5494 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5495
5496 /* A bit of special processing for shlib breakpoints. We need to
5497 process solib loading here, so that the lists of loaded and
5498 unloaded libraries are correct before we handle "catch load" and
5499 "catch unload". */
5500 for (bs = bs_head; bs != NULL; bs = bs->next)
5501 {
5502 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5503 {
5504 handle_solib_event ();
5505 break;
5506 }
5507 }
5508
5509 /* Now go through the locations that caused the target to stop, and
5510 check whether we're interested in reporting this stop to higher
5511 layers, or whether we should resume the target transparently. */
5512
5513 removed_any = 0;
5514
5515 for (bs = bs_head; bs != NULL; bs = bs->next)
5516 {
5517 if (!bs->stop)
5518 continue;
5519
5520 b = bs->breakpoint_at;
5521 b->ops->check_status (bs);
5522 if (bs->stop)
5523 {
5524 bpstat_check_breakpoint_conditions (bs, thread);
5525
5526 if (bs->stop)
5527 {
5528 ++(b->hit_count);
5529 gdb::observers::breakpoint_modified.notify (b);
5530
5531 /* We will stop here. */
5532 if (b->disposition == disp_disable)
5533 {
5534 --(b->enable_count);
5535 if (b->enable_count <= 0)
5536 b->enable_state = bp_disabled;
5537 removed_any = 1;
5538 }
5539 if (b->silent)
5540 bs->print = 0;
5541 bs->commands = b->commands;
5542 if (command_line_is_silent (bs->commands
5543 ? bs->commands.get () : NULL))
5544 bs->print = 0;
5545
5546 b->ops->after_condition_true (bs);
5547 }
5548
5549 }
5550
5551 /* Print nothing for this entry if we don't stop or don't
5552 print. */
5553 if (!bs->stop || !bs->print)
5554 bs->print_it = print_it_noop;
5555 }
5556
5557 /* If we aren't stopping, the value of some hardware watchpoint may
5558 not have changed, but the intermediate memory locations we are
5559 watching may have. Don't bother if we're stopping; this will get
5560 done later. */
5561 need_remove_insert = 0;
5562 if (! bpstat_causes_stop (bs_head))
5563 for (bs = bs_head; bs != NULL; bs = bs->next)
5564 if (!bs->stop
5565 && bs->breakpoint_at
5566 && is_hardware_watchpoint (bs->breakpoint_at))
5567 {
5568 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5569
5570 update_watchpoint (w, 0 /* don't reparse. */);
5571 need_remove_insert = 1;
5572 }
5573
5574 if (need_remove_insert)
5575 update_global_location_list (UGLL_MAY_INSERT);
5576 else if (removed_any)
5577 update_global_location_list (UGLL_DONT_INSERT);
5578
5579 return bs_head;
5580 }
5581
5582 static void
5583 handle_jit_event (CORE_ADDR address)
5584 {
5585 struct gdbarch *gdbarch;
5586
5587 infrun_debug_printf ("handling bp_jit_event");
5588
5589 /* Switch terminal for any messages produced by
5590 breakpoint_re_set. */
5591 target_terminal::ours_for_output ();
5592
5593 gdbarch = get_frame_arch (get_current_frame ());
5594 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5595 thus it is expected that its objectfile can be found through
5596 minimal symbol lookup. If it doesn't work (and assert fails), it
5597 most likely means that `jit_breakpoint_re_set` was changes and this
5598 function needs to be updated too. */
5599 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5600 gdb_assert (jit_bp_sym.objfile != nullptr);
5601 jit_event_handler (gdbarch, jit_bp_sym.objfile);
5602
5603 target_terminal::inferior ();
5604 }
5605
5606 /* Prepare WHAT final decision for infrun. */
5607
5608 /* Decide what infrun needs to do with this bpstat. */
5609
5610 struct bpstat_what
5611 bpstat_what (bpstat bs_head)
5612 {
5613 struct bpstat_what retval;
5614 bpstat bs;
5615
5616 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5617 retval.call_dummy = STOP_NONE;
5618 retval.is_longjmp = false;
5619
5620 for (bs = bs_head; bs != NULL; bs = bs->next)
5621 {
5622 /* Extract this BS's action. After processing each BS, we check
5623 if its action overrides all we've seem so far. */
5624 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5625 enum bptype bptype;
5626
5627 if (bs->breakpoint_at == NULL)
5628 {
5629 /* I suspect this can happen if it was a momentary
5630 breakpoint which has since been deleted. */
5631 bptype = bp_none;
5632 }
5633 else
5634 bptype = bs->breakpoint_at->type;
5635
5636 switch (bptype)
5637 {
5638 case bp_none:
5639 break;
5640 case bp_breakpoint:
5641 case bp_hardware_breakpoint:
5642 case bp_single_step:
5643 case bp_until:
5644 case bp_finish:
5645 case bp_shlib_event:
5646 if (bs->stop)
5647 {
5648 if (bs->print)
5649 this_action = BPSTAT_WHAT_STOP_NOISY;
5650 else
5651 this_action = BPSTAT_WHAT_STOP_SILENT;
5652 }
5653 else
5654 this_action = BPSTAT_WHAT_SINGLE;
5655 break;
5656 case bp_watchpoint:
5657 case bp_hardware_watchpoint:
5658 case bp_read_watchpoint:
5659 case bp_access_watchpoint:
5660 if (bs->stop)
5661 {
5662 if (bs->print)
5663 this_action = BPSTAT_WHAT_STOP_NOISY;
5664 else
5665 this_action = BPSTAT_WHAT_STOP_SILENT;
5666 }
5667 else
5668 {
5669 /* There was a watchpoint, but we're not stopping.
5670 This requires no further action. */
5671 }
5672 break;
5673 case bp_longjmp:
5674 case bp_longjmp_call_dummy:
5675 case bp_exception:
5676 if (bs->stop)
5677 {
5678 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5679 retval.is_longjmp = bptype != bp_exception;
5680 }
5681 else
5682 this_action = BPSTAT_WHAT_SINGLE;
5683 break;
5684 case bp_longjmp_resume:
5685 case bp_exception_resume:
5686 if (bs->stop)
5687 {
5688 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5689 retval.is_longjmp = bptype == bp_longjmp_resume;
5690 }
5691 else
5692 this_action = BPSTAT_WHAT_SINGLE;
5693 break;
5694 case bp_step_resume:
5695 if (bs->stop)
5696 this_action = BPSTAT_WHAT_STEP_RESUME;
5697 else
5698 {
5699 /* It is for the wrong frame. */
5700 this_action = BPSTAT_WHAT_SINGLE;
5701 }
5702 break;
5703 case bp_hp_step_resume:
5704 if (bs->stop)
5705 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5706 else
5707 {
5708 /* It is for the wrong frame. */
5709 this_action = BPSTAT_WHAT_SINGLE;
5710 }
5711 break;
5712 case bp_watchpoint_scope:
5713 case bp_thread_event:
5714 case bp_overlay_event:
5715 case bp_longjmp_master:
5716 case bp_std_terminate_master:
5717 case bp_exception_master:
5718 this_action = BPSTAT_WHAT_SINGLE;
5719 break;
5720 case bp_catchpoint:
5721 if (bs->stop)
5722 {
5723 if (bs->print)
5724 this_action = BPSTAT_WHAT_STOP_NOISY;
5725 else
5726 this_action = BPSTAT_WHAT_STOP_SILENT;
5727 }
5728 else
5729 {
5730 /* Some catchpoints are implemented with breakpoints.
5731 For those, we need to step over the breakpoint. */
5732 if (bs->bp_location_at->loc_type != bp_loc_other)
5733 this_action = BPSTAT_WHAT_SINGLE;
5734 }
5735 break;
5736 case bp_jit_event:
5737 this_action = BPSTAT_WHAT_SINGLE;
5738 break;
5739 case bp_call_dummy:
5740 /* Make sure the action is stop (silent or noisy),
5741 so infrun.c pops the dummy frame. */
5742 retval.call_dummy = STOP_STACK_DUMMY;
5743 this_action = BPSTAT_WHAT_STOP_SILENT;
5744 break;
5745 case bp_std_terminate:
5746 /* Make sure the action is stop (silent or noisy),
5747 so infrun.c pops the dummy frame. */
5748 retval.call_dummy = STOP_STD_TERMINATE;
5749 this_action = BPSTAT_WHAT_STOP_SILENT;
5750 break;
5751 case bp_tracepoint:
5752 case bp_fast_tracepoint:
5753 case bp_static_tracepoint:
5754 /* Tracepoint hits should not be reported back to GDB, and
5755 if one got through somehow, it should have been filtered
5756 out already. */
5757 internal_error (__FILE__, __LINE__,
5758 _("bpstat_what: tracepoint encountered"));
5759 break;
5760 case bp_gnu_ifunc_resolver:
5761 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5762 this_action = BPSTAT_WHAT_SINGLE;
5763 break;
5764 case bp_gnu_ifunc_resolver_return:
5765 /* The breakpoint will be removed, execution will restart from the
5766 PC of the former breakpoint. */
5767 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5768 break;
5769
5770 case bp_dprintf:
5771 if (bs->stop)
5772 this_action = BPSTAT_WHAT_STOP_SILENT;
5773 else
5774 this_action = BPSTAT_WHAT_SINGLE;
5775 break;
5776
5777 default:
5778 internal_error (__FILE__, __LINE__,
5779 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5780 }
5781
5782 retval.main_action = std::max (retval.main_action, this_action);
5783 }
5784
5785 return retval;
5786 }
5787
5788 void
5789 bpstat_run_callbacks (bpstat bs_head)
5790 {
5791 bpstat bs;
5792
5793 for (bs = bs_head; bs != NULL; bs = bs->next)
5794 {
5795 struct breakpoint *b = bs->breakpoint_at;
5796
5797 if (b == NULL)
5798 continue;
5799 switch (b->type)
5800 {
5801 case bp_jit_event:
5802 handle_jit_event (bs->bp_location_at->address);
5803 break;
5804 case bp_gnu_ifunc_resolver:
5805 gnu_ifunc_resolver_stop (b);
5806 break;
5807 case bp_gnu_ifunc_resolver_return:
5808 gnu_ifunc_resolver_return_stop (b);
5809 break;
5810 }
5811 }
5812 }
5813
5814 /* See breakpoint.h. */
5815
5816 bool
5817 bpstat_should_step ()
5818 {
5819 struct breakpoint *b;
5820
5821 ALL_BREAKPOINTS (b)
5822 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5823 return true;
5824 return false;
5825 }
5826
5827 /* See breakpoint.h. */
5828
5829 bool
5830 bpstat_causes_stop (bpstat bs)
5831 {
5832 for (; bs != NULL; bs = bs->next)
5833 if (bs->stop)
5834 return true;
5835
5836 return false;
5837 }
5838
5839 \f
5840
5841 /* Compute a string of spaces suitable to indent the next line
5842 so it starts at the position corresponding to the table column
5843 named COL_NAME in the currently active table of UIOUT. */
5844
5845 static char *
5846 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5847 {
5848 static char wrap_indent[80];
5849 int i, total_width, width, align;
5850 const char *text;
5851
5852 total_width = 0;
5853 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5854 {
5855 if (strcmp (text, col_name) == 0)
5856 {
5857 gdb_assert (total_width < sizeof wrap_indent);
5858 memset (wrap_indent, ' ', total_width);
5859 wrap_indent[total_width] = 0;
5860
5861 return wrap_indent;
5862 }
5863
5864 total_width += width + 1;
5865 }
5866
5867 return NULL;
5868 }
5869
5870 /* Determine if the locations of this breakpoint will have their conditions
5871 evaluated by the target, host or a mix of both. Returns the following:
5872
5873 "host": Host evals condition.
5874 "host or target": Host or Target evals condition.
5875 "target": Target evals condition.
5876 */
5877
5878 static const char *
5879 bp_condition_evaluator (struct breakpoint *b)
5880 {
5881 struct bp_location *bl;
5882 char host_evals = 0;
5883 char target_evals = 0;
5884
5885 if (!b)
5886 return NULL;
5887
5888 if (!is_breakpoint (b))
5889 return NULL;
5890
5891 if (gdb_evaluates_breakpoint_condition_p ()
5892 || !target_supports_evaluation_of_breakpoint_conditions ())
5893 return condition_evaluation_host;
5894
5895 for (bl = b->loc; bl; bl = bl->next)
5896 {
5897 if (bl->cond_bytecode)
5898 target_evals++;
5899 else
5900 host_evals++;
5901 }
5902
5903 if (host_evals && target_evals)
5904 return condition_evaluation_both;
5905 else if (target_evals)
5906 return condition_evaluation_target;
5907 else
5908 return condition_evaluation_host;
5909 }
5910
5911 /* Determine the breakpoint location's condition evaluator. This is
5912 similar to bp_condition_evaluator, but for locations. */
5913
5914 static const char *
5915 bp_location_condition_evaluator (struct bp_location *bl)
5916 {
5917 if (bl && !is_breakpoint (bl->owner))
5918 return NULL;
5919
5920 if (gdb_evaluates_breakpoint_condition_p ()
5921 || !target_supports_evaluation_of_breakpoint_conditions ())
5922 return condition_evaluation_host;
5923
5924 if (bl && bl->cond_bytecode)
5925 return condition_evaluation_target;
5926 else
5927 return condition_evaluation_host;
5928 }
5929
5930 /* Print the LOC location out of the list of B->LOC locations. */
5931
5932 static void
5933 print_breakpoint_location (struct breakpoint *b,
5934 struct bp_location *loc)
5935 {
5936 struct ui_out *uiout = current_uiout;
5937
5938 scoped_restore_current_program_space restore_pspace;
5939
5940 if (loc != NULL && loc->shlib_disabled)
5941 loc = NULL;
5942
5943 if (loc != NULL)
5944 set_current_program_space (loc->pspace);
5945
5946 if (b->display_canonical)
5947 uiout->field_string ("what", event_location_to_string (b->location.get ()));
5948 else if (loc && loc->symtab)
5949 {
5950 const struct symbol *sym = loc->symbol;
5951
5952 if (sym)
5953 {
5954 uiout->text ("in ");
5955 uiout->field_string ("func", sym->print_name (),
5956 function_name_style.style ());
5957 uiout->text (" ");
5958 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5959 uiout->text ("at ");
5960 }
5961 uiout->field_string ("file",
5962 symtab_to_filename_for_display (loc->symtab),
5963 file_name_style.style ());
5964 uiout->text (":");
5965
5966 if (uiout->is_mi_like_p ())
5967 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
5968
5969 uiout->field_signed ("line", loc->line_number);
5970 }
5971 else if (loc)
5972 {
5973 string_file stb;
5974
5975 print_address_symbolic (loc->gdbarch, loc->address, &stb,
5976 demangle, "");
5977 uiout->field_stream ("at", stb);
5978 }
5979 else
5980 {
5981 uiout->field_string ("pending",
5982 event_location_to_string (b->location.get ()));
5983 /* If extra_string is available, it could be holding a condition
5984 or dprintf arguments. In either case, make sure it is printed,
5985 too, but only for non-MI streams. */
5986 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
5987 {
5988 if (b->type == bp_dprintf)
5989 uiout->text (",");
5990 else
5991 uiout->text (" ");
5992 uiout->text (b->extra_string);
5993 }
5994 }
5995
5996 if (loc && is_breakpoint (b)
5997 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5998 && bp_condition_evaluator (b) == condition_evaluation_both)
5999 {
6000 uiout->text (" (");
6001 uiout->field_string ("evaluated-by",
6002 bp_location_condition_evaluator (loc));
6003 uiout->text (")");
6004 }
6005 }
6006
6007 static const char *
6008 bptype_string (enum bptype type)
6009 {
6010 struct ep_type_description
6011 {
6012 enum bptype type;
6013 const char *description;
6014 };
6015 static struct ep_type_description bptypes[] =
6016 {
6017 {bp_none, "?deleted?"},
6018 {bp_breakpoint, "breakpoint"},
6019 {bp_hardware_breakpoint, "hw breakpoint"},
6020 {bp_single_step, "sw single-step"},
6021 {bp_until, "until"},
6022 {bp_finish, "finish"},
6023 {bp_watchpoint, "watchpoint"},
6024 {bp_hardware_watchpoint, "hw watchpoint"},
6025 {bp_read_watchpoint, "read watchpoint"},
6026 {bp_access_watchpoint, "acc watchpoint"},
6027 {bp_longjmp, "longjmp"},
6028 {bp_longjmp_resume, "longjmp resume"},
6029 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6030 {bp_exception, "exception"},
6031 {bp_exception_resume, "exception resume"},
6032 {bp_step_resume, "step resume"},
6033 {bp_hp_step_resume, "high-priority step resume"},
6034 {bp_watchpoint_scope, "watchpoint scope"},
6035 {bp_call_dummy, "call dummy"},
6036 {bp_std_terminate, "std::terminate"},
6037 {bp_shlib_event, "shlib events"},
6038 {bp_thread_event, "thread events"},
6039 {bp_overlay_event, "overlay events"},
6040 {bp_longjmp_master, "longjmp master"},
6041 {bp_std_terminate_master, "std::terminate master"},
6042 {bp_exception_master, "exception master"},
6043 {bp_catchpoint, "catchpoint"},
6044 {bp_tracepoint, "tracepoint"},
6045 {bp_fast_tracepoint, "fast tracepoint"},
6046 {bp_static_tracepoint, "static tracepoint"},
6047 {bp_dprintf, "dprintf"},
6048 {bp_jit_event, "jit events"},
6049 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6050 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6051 };
6052
6053 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6054 || ((int) type != bptypes[(int) type].type))
6055 internal_error (__FILE__, __LINE__,
6056 _("bptypes table does not describe type #%d."),
6057 (int) type);
6058
6059 return bptypes[(int) type].description;
6060 }
6061
6062 /* For MI, output a field named 'thread-groups' with a list as the value.
6063 For CLI, prefix the list with the string 'inf'. */
6064
6065 static void
6066 output_thread_groups (struct ui_out *uiout,
6067 const char *field_name,
6068 const std::vector<int> &inf_nums,
6069 int mi_only)
6070 {
6071 int is_mi = uiout->is_mi_like_p ();
6072
6073 /* For backward compatibility, don't display inferiors in CLI unless
6074 there are several. Always display them for MI. */
6075 if (!is_mi && mi_only)
6076 return;
6077
6078 ui_out_emit_list list_emitter (uiout, field_name);
6079
6080 for (size_t i = 0; i < inf_nums.size (); i++)
6081 {
6082 if (is_mi)
6083 {
6084 char mi_group[10];
6085
6086 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
6087 uiout->field_string (NULL, mi_group);
6088 }
6089 else
6090 {
6091 if (i == 0)
6092 uiout->text (" inf ");
6093 else
6094 uiout->text (", ");
6095
6096 uiout->text (plongest (inf_nums[i]));
6097 }
6098 }
6099 }
6100
6101 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6102 instead of going via breakpoint_ops::print_one. This makes "maint
6103 info breakpoints" show the software breakpoint locations of
6104 catchpoints, which are considered internal implementation
6105 detail. */
6106
6107 static void
6108 print_one_breakpoint_location (struct breakpoint *b,
6109 struct bp_location *loc,
6110 int loc_number,
6111 struct bp_location **last_loc,
6112 int allflag, bool raw_loc)
6113 {
6114 struct command_line *l;
6115 static char bpenables[] = "nynny";
6116
6117 struct ui_out *uiout = current_uiout;
6118 int header_of_multiple = 0;
6119 int part_of_multiple = (loc != NULL);
6120 struct value_print_options opts;
6121
6122 get_user_print_options (&opts);
6123
6124 gdb_assert (!loc || loc_number != 0);
6125 /* See comment in print_one_breakpoint concerning treatment of
6126 breakpoints with single disabled location. */
6127 if (loc == NULL
6128 && (b->loc != NULL
6129 && (b->loc->next != NULL
6130 || !b->loc->enabled || b->loc->disabled_by_cond)))
6131 header_of_multiple = 1;
6132 if (loc == NULL)
6133 loc = b->loc;
6134
6135 annotate_record ();
6136
6137 /* 1 */
6138 annotate_field (0);
6139 if (part_of_multiple)
6140 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6141 else
6142 uiout->field_signed ("number", b->number);
6143
6144 /* 2 */
6145 annotate_field (1);
6146 if (part_of_multiple)
6147 uiout->field_skip ("type");
6148 else
6149 uiout->field_string ("type", bptype_string (b->type));
6150
6151 /* 3 */
6152 annotate_field (2);
6153 if (part_of_multiple)
6154 uiout->field_skip ("disp");
6155 else
6156 uiout->field_string ("disp", bpdisp_text (b->disposition));
6157
6158 /* 4 */
6159 annotate_field (3);
6160 if (part_of_multiple)
6161 uiout->field_string ("enabled", (loc->disabled_by_cond ? "N*"
6162 : (loc->enabled ? "y" : "n")));
6163 else
6164 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6165
6166 /* 5 and 6 */
6167 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
6168 b->ops->print_one (b, last_loc);
6169 else
6170 {
6171 if (is_watchpoint (b))
6172 {
6173 struct watchpoint *w = (struct watchpoint *) b;
6174
6175 /* Field 4, the address, is omitted (which makes the columns
6176 not line up too nicely with the headers, but the effect
6177 is relatively readable). */
6178 if (opts.addressprint)
6179 uiout->field_skip ("addr");
6180 annotate_field (5);
6181 uiout->field_string ("what", w->exp_string);
6182 }
6183 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6184 || is_ada_exception_catchpoint (b))
6185 {
6186 if (opts.addressprint)
6187 {
6188 annotate_field (4);
6189 if (header_of_multiple)
6190 uiout->field_string ("addr", "<MULTIPLE>",
6191 metadata_style.style ());
6192 else if (b->loc == NULL || loc->shlib_disabled)
6193 uiout->field_string ("addr", "<PENDING>",
6194 metadata_style.style ());
6195 else
6196 uiout->field_core_addr ("addr",
6197 loc->gdbarch, loc->address);
6198 }
6199 annotate_field (5);
6200 if (!header_of_multiple)
6201 print_breakpoint_location (b, loc);
6202 if (b->loc)
6203 *last_loc = b->loc;
6204 }
6205 }
6206
6207 if (loc != NULL && !header_of_multiple)
6208 {
6209 std::vector<int> inf_nums;
6210 int mi_only = 1;
6211
6212 for (inferior *inf : all_inferiors ())
6213 {
6214 if (inf->pspace == loc->pspace)
6215 inf_nums.push_back (inf->num);
6216 }
6217
6218 /* For backward compatibility, don't display inferiors in CLI unless
6219 there are several. Always display for MI. */
6220 if (allflag
6221 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6222 && (program_spaces.size () > 1
6223 || number_of_inferiors () > 1)
6224 /* LOC is for existing B, it cannot be in
6225 moribund_locations and thus having NULL OWNER. */
6226 && loc->owner->type != bp_catchpoint))
6227 mi_only = 0;
6228 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6229 }
6230
6231 if (!part_of_multiple)
6232 {
6233 if (b->thread != -1)
6234 {
6235 /* FIXME: This seems to be redundant and lost here; see the
6236 "stop only in" line a little further down. */
6237 uiout->text (" thread ");
6238 uiout->field_signed ("thread", b->thread);
6239 }
6240 else if (b->task != 0)
6241 {
6242 uiout->text (" task ");
6243 uiout->field_signed ("task", b->task);
6244 }
6245 }
6246
6247 uiout->text ("\n");
6248
6249 if (!part_of_multiple)
6250 b->ops->print_one_detail (b, uiout);
6251
6252 if (part_of_multiple && frame_id_p (b->frame_id))
6253 {
6254 annotate_field (6);
6255 uiout->text ("\tstop only in stack frame at ");
6256 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6257 the frame ID. */
6258 uiout->field_core_addr ("frame",
6259 b->gdbarch, b->frame_id.stack_addr);
6260 uiout->text ("\n");
6261 }
6262
6263 if (!part_of_multiple && b->cond_string)
6264 {
6265 annotate_field (7);
6266 if (is_tracepoint (b))
6267 uiout->text ("\ttrace only if ");
6268 else
6269 uiout->text ("\tstop only if ");
6270 uiout->field_string ("cond", b->cond_string);
6271
6272 /* Print whether the target is doing the breakpoint's condition
6273 evaluation. If GDB is doing the evaluation, don't print anything. */
6274 if (is_breakpoint (b)
6275 && breakpoint_condition_evaluation_mode ()
6276 == condition_evaluation_target)
6277 {
6278 uiout->message (" (%pF evals)",
6279 string_field ("evaluated-by",
6280 bp_condition_evaluator (b)));
6281 }
6282 uiout->text ("\n");
6283 }
6284
6285 if (!part_of_multiple && b->thread != -1)
6286 {
6287 /* FIXME should make an annotation for this. */
6288 uiout->text ("\tstop only in thread ");
6289 if (uiout->is_mi_like_p ())
6290 uiout->field_signed ("thread", b->thread);
6291 else
6292 {
6293 struct thread_info *thr = find_thread_global_id (b->thread);
6294
6295 uiout->field_string ("thread", print_thread_id (thr));
6296 }
6297 uiout->text ("\n");
6298 }
6299
6300 if (!part_of_multiple)
6301 {
6302 if (b->hit_count)
6303 {
6304 /* FIXME should make an annotation for this. */
6305 if (is_catchpoint (b))
6306 uiout->text ("\tcatchpoint");
6307 else if (is_tracepoint (b))
6308 uiout->text ("\ttracepoint");
6309 else
6310 uiout->text ("\tbreakpoint");
6311 uiout->text (" already hit ");
6312 uiout->field_signed ("times", b->hit_count);
6313 if (b->hit_count == 1)
6314 uiout->text (" time\n");
6315 else
6316 uiout->text (" times\n");
6317 }
6318 else
6319 {
6320 /* Output the count also if it is zero, but only if this is mi. */
6321 if (uiout->is_mi_like_p ())
6322 uiout->field_signed ("times", b->hit_count);
6323 }
6324 }
6325
6326 if (!part_of_multiple && b->ignore_count)
6327 {
6328 annotate_field (8);
6329 uiout->message ("\tignore next %pF hits\n",
6330 signed_field ("ignore", b->ignore_count));
6331 }
6332
6333 /* Note that an enable count of 1 corresponds to "enable once"
6334 behavior, which is reported by the combination of enablement and
6335 disposition, so we don't need to mention it here. */
6336 if (!part_of_multiple && b->enable_count > 1)
6337 {
6338 annotate_field (8);
6339 uiout->text ("\tdisable after ");
6340 /* Tweak the wording to clarify that ignore and enable counts
6341 are distinct, and have additive effect. */
6342 if (b->ignore_count)
6343 uiout->text ("additional ");
6344 else
6345 uiout->text ("next ");
6346 uiout->field_signed ("enable", b->enable_count);
6347 uiout->text (" hits\n");
6348 }
6349
6350 if (!part_of_multiple && is_tracepoint (b))
6351 {
6352 struct tracepoint *tp = (struct tracepoint *) b;
6353
6354 if (tp->traceframe_usage)
6355 {
6356 uiout->text ("\ttrace buffer usage ");
6357 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6358 uiout->text (" bytes\n");
6359 }
6360 }
6361
6362 l = b->commands ? b->commands.get () : NULL;
6363 if (!part_of_multiple && l)
6364 {
6365 annotate_field (9);
6366 ui_out_emit_tuple tuple_emitter (uiout, "script");
6367 print_command_lines (uiout, l, 4);
6368 }
6369
6370 if (is_tracepoint (b))
6371 {
6372 struct tracepoint *t = (struct tracepoint *) b;
6373
6374 if (!part_of_multiple && t->pass_count)
6375 {
6376 annotate_field (10);
6377 uiout->text ("\tpass count ");
6378 uiout->field_signed ("pass", t->pass_count);
6379 uiout->text (" \n");
6380 }
6381
6382 /* Don't display it when tracepoint or tracepoint location is
6383 pending. */
6384 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6385 {
6386 annotate_field (11);
6387
6388 if (uiout->is_mi_like_p ())
6389 uiout->field_string ("installed",
6390 loc->inserted ? "y" : "n");
6391 else
6392 {
6393 if (loc->inserted)
6394 uiout->text ("\t");
6395 else
6396 uiout->text ("\tnot ");
6397 uiout->text ("installed on target\n");
6398 }
6399 }
6400 }
6401
6402 if (uiout->is_mi_like_p () && !part_of_multiple)
6403 {
6404 if (is_watchpoint (b))
6405 {
6406 struct watchpoint *w = (struct watchpoint *) b;
6407
6408 uiout->field_string ("original-location", w->exp_string);
6409 }
6410 else if (b->location != NULL
6411 && event_location_to_string (b->location.get ()) != NULL)
6412 uiout->field_string ("original-location",
6413 event_location_to_string (b->location.get ()));
6414 }
6415 }
6416
6417 /* See breakpoint.h. */
6418
6419 bool fix_multi_location_breakpoint_output_globally = false;
6420
6421 static void
6422 print_one_breakpoint (struct breakpoint *b,
6423 struct bp_location **last_loc,
6424 int allflag)
6425 {
6426 struct ui_out *uiout = current_uiout;
6427 bool use_fixed_output
6428 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6429 || fix_multi_location_breakpoint_output_globally);
6430
6431 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6432 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
6433
6434 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6435 are outside. */
6436 if (!use_fixed_output)
6437 bkpt_tuple_emitter.reset ();
6438
6439 /* If this breakpoint has custom print function,
6440 it's already printed. Otherwise, print individual
6441 locations, if any. */
6442 if (b->ops == NULL
6443 || b->ops->print_one == NULL
6444 || allflag)
6445 {
6446 /* If breakpoint has a single location that is disabled, we
6447 print it as if it had several locations, since otherwise it's
6448 hard to represent "breakpoint enabled, location disabled"
6449 situation.
6450
6451 Note that while hardware watchpoints have several locations
6452 internally, that's not a property exposed to users.
6453
6454 Likewise, while catchpoints may be implemented with
6455 breakpoints (e.g., catch throw), that's not a property
6456 exposed to users. We do however display the internal
6457 breakpoint locations with "maint info breakpoints". */
6458 if (!is_hardware_watchpoint (b)
6459 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6460 || is_ada_exception_catchpoint (b))
6461 && (allflag
6462 || (b->loc && (b->loc->next
6463 || !b->loc->enabled
6464 || b->loc->disabled_by_cond))))
6465 {
6466 gdb::optional<ui_out_emit_list> locations_list;
6467
6468 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6469 MI record. For later versions, place breakpoint locations in a
6470 list. */
6471 if (uiout->is_mi_like_p () && use_fixed_output)
6472 locations_list.emplace (uiout, "locations");
6473
6474 int n = 1;
6475 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next, ++n)
6476 {
6477 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6478 print_one_breakpoint_location (b, loc, n, last_loc,
6479 allflag, allflag);
6480 }
6481 }
6482 }
6483 }
6484
6485 static int
6486 breakpoint_address_bits (struct breakpoint *b)
6487 {
6488 int print_address_bits = 0;
6489 struct bp_location *loc;
6490
6491 /* Software watchpoints that aren't watching memory don't have an
6492 address to print. */
6493 if (is_no_memory_software_watchpoint (b))
6494 return 0;
6495
6496 for (loc = b->loc; loc; loc = loc->next)
6497 {
6498 int addr_bit;
6499
6500 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6501 if (addr_bit > print_address_bits)
6502 print_address_bits = addr_bit;
6503 }
6504
6505 return print_address_bits;
6506 }
6507
6508 /* See breakpoint.h. */
6509
6510 void
6511 print_breakpoint (breakpoint *b)
6512 {
6513 struct bp_location *dummy_loc = NULL;
6514 print_one_breakpoint (b, &dummy_loc, 0);
6515 }
6516
6517 /* Return true if this breakpoint was set by the user, false if it is
6518 internal or momentary. */
6519
6520 int
6521 user_breakpoint_p (struct breakpoint *b)
6522 {
6523 return b->number > 0;
6524 }
6525
6526 /* See breakpoint.h. */
6527
6528 int
6529 pending_breakpoint_p (struct breakpoint *b)
6530 {
6531 return b->loc == NULL;
6532 }
6533
6534 /* Print information on breakpoints (including watchpoints and tracepoints).
6535
6536 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6537 understood by number_or_range_parser. Only breakpoints included in this
6538 list are then printed.
6539
6540 If SHOW_INTERNAL is true, print internal breakpoints.
6541
6542 If FILTER is non-NULL, call it on each breakpoint and only include the
6543 ones for which it returns true.
6544
6545 Return the total number of breakpoints listed. */
6546
6547 static int
6548 breakpoint_1 (const char *bp_num_list, bool show_internal,
6549 bool (*filter) (const struct breakpoint *))
6550 {
6551 struct breakpoint *b;
6552 struct bp_location *last_loc = NULL;
6553 int nr_printable_breakpoints;
6554 struct value_print_options opts;
6555 int print_address_bits = 0;
6556 int print_type_col_width = 14;
6557 struct ui_out *uiout = current_uiout;
6558 bool has_disabled_by_cond_location = false;
6559
6560 get_user_print_options (&opts);
6561
6562 /* Compute the number of rows in the table, as well as the size
6563 required for address fields. */
6564 nr_printable_breakpoints = 0;
6565 ALL_BREAKPOINTS (b)
6566 {
6567 /* If we have a filter, only list the breakpoints it accepts. */
6568 if (filter && !filter (b))
6569 continue;
6570
6571 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6572 accept. Skip the others. */
6573 if (bp_num_list != NULL && *bp_num_list != '\0')
6574 {
6575 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6576 continue;
6577 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6578 continue;
6579 }
6580
6581 if (show_internal || user_breakpoint_p (b))
6582 {
6583 int addr_bit, type_len;
6584
6585 addr_bit = breakpoint_address_bits (b);
6586 if (addr_bit > print_address_bits)
6587 print_address_bits = addr_bit;
6588
6589 type_len = strlen (bptype_string (b->type));
6590 if (type_len > print_type_col_width)
6591 print_type_col_width = type_len;
6592
6593 nr_printable_breakpoints++;
6594 }
6595 }
6596
6597 {
6598 ui_out_emit_table table_emitter (uiout,
6599 opts.addressprint ? 6 : 5,
6600 nr_printable_breakpoints,
6601 "BreakpointTable");
6602
6603 if (nr_printable_breakpoints > 0)
6604 annotate_breakpoints_headers ();
6605 if (nr_printable_breakpoints > 0)
6606 annotate_field (0);
6607 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6608 if (nr_printable_breakpoints > 0)
6609 annotate_field (1);
6610 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6611 if (nr_printable_breakpoints > 0)
6612 annotate_field (2);
6613 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6614 if (nr_printable_breakpoints > 0)
6615 annotate_field (3);
6616 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6617 if (opts.addressprint)
6618 {
6619 if (nr_printable_breakpoints > 0)
6620 annotate_field (4);
6621 if (print_address_bits <= 32)
6622 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6623 else
6624 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6625 }
6626 if (nr_printable_breakpoints > 0)
6627 annotate_field (5);
6628 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6629 uiout->table_body ();
6630 if (nr_printable_breakpoints > 0)
6631 annotate_breakpoints_table ();
6632
6633 ALL_BREAKPOINTS (b)
6634 {
6635 QUIT;
6636 /* If we have a filter, only list the breakpoints it accepts. */
6637 if (filter && !filter (b))
6638 continue;
6639
6640 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6641 accept. Skip the others. */
6642
6643 if (bp_num_list != NULL && *bp_num_list != '\0')
6644 {
6645 if (show_internal) /* maintenance info breakpoint */
6646 {
6647 if (parse_and_eval_long (bp_num_list) != b->number)
6648 continue;
6649 }
6650 else /* all others */
6651 {
6652 if (!number_is_in_list (bp_num_list, b->number))
6653 continue;
6654 }
6655 }
6656 /* We only print out user settable breakpoints unless the
6657 show_internal is set. */
6658 if (show_internal || user_breakpoint_p (b))
6659 {
6660 print_one_breakpoint (b, &last_loc, show_internal);
6661 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
6662 if (loc->disabled_by_cond)
6663 has_disabled_by_cond_location = true;
6664 }
6665 }
6666 }
6667
6668 if (nr_printable_breakpoints == 0)
6669 {
6670 /* If there's a filter, let the caller decide how to report
6671 empty list. */
6672 if (!filter)
6673 {
6674 if (bp_num_list == NULL || *bp_num_list == '\0')
6675 uiout->message ("No breakpoints or watchpoints.\n");
6676 else
6677 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6678 bp_num_list);
6679 }
6680 }
6681 else
6682 {
6683 if (last_loc && !server_command)
6684 set_next_address (last_loc->gdbarch, last_loc->address);
6685
6686 if (has_disabled_by_cond_location)
6687 uiout->message (_("(*): Breakpoint condition is invalid at this "
6688 "location.\n"));
6689 }
6690
6691 /* FIXME? Should this be moved up so that it is only called when
6692 there have been breakpoints? */
6693 annotate_breakpoints_table_end ();
6694
6695 return nr_printable_breakpoints;
6696 }
6697
6698 /* Display the value of default-collect in a way that is generally
6699 compatible with the breakpoint list. */
6700
6701 static void
6702 default_collect_info (void)
6703 {
6704 struct ui_out *uiout = current_uiout;
6705
6706 /* If it has no value (which is frequently the case), say nothing; a
6707 message like "No default-collect." gets in user's face when it's
6708 not wanted. */
6709 if (!*default_collect)
6710 return;
6711
6712 /* The following phrase lines up nicely with per-tracepoint collect
6713 actions. */
6714 uiout->text ("default collect ");
6715 uiout->field_string ("default-collect", default_collect);
6716 uiout->text (" \n");
6717 }
6718
6719 static void
6720 info_breakpoints_command (const char *args, int from_tty)
6721 {
6722 breakpoint_1 (args, false, NULL);
6723
6724 default_collect_info ();
6725 }
6726
6727 static void
6728 info_watchpoints_command (const char *args, int from_tty)
6729 {
6730 int num_printed = breakpoint_1 (args, false, is_watchpoint);
6731 struct ui_out *uiout = current_uiout;
6732
6733 if (num_printed == 0)
6734 {
6735 if (args == NULL || *args == '\0')
6736 uiout->message ("No watchpoints.\n");
6737 else
6738 uiout->message ("No watchpoint matching '%s'.\n", args);
6739 }
6740 }
6741
6742 static void
6743 maintenance_info_breakpoints (const char *args, int from_tty)
6744 {
6745 breakpoint_1 (args, true, NULL);
6746
6747 default_collect_info ();
6748 }
6749
6750 static int
6751 breakpoint_has_pc (struct breakpoint *b,
6752 struct program_space *pspace,
6753 CORE_ADDR pc, struct obj_section *section)
6754 {
6755 struct bp_location *bl = b->loc;
6756
6757 for (; bl; bl = bl->next)
6758 {
6759 if (bl->pspace == pspace
6760 && bl->address == pc
6761 && (!overlay_debugging || bl->section == section))
6762 return 1;
6763 }
6764 return 0;
6765 }
6766
6767 /* Print a message describing any user-breakpoints set at PC. This
6768 concerns with logical breakpoints, so we match program spaces, not
6769 address spaces. */
6770
6771 static void
6772 describe_other_breakpoints (struct gdbarch *gdbarch,
6773 struct program_space *pspace, CORE_ADDR pc,
6774 struct obj_section *section, int thread)
6775 {
6776 int others = 0;
6777 struct breakpoint *b;
6778
6779 ALL_BREAKPOINTS (b)
6780 others += (user_breakpoint_p (b)
6781 && breakpoint_has_pc (b, pspace, pc, section));
6782 if (others > 0)
6783 {
6784 if (others == 1)
6785 printf_filtered (_("Note: breakpoint "));
6786 else /* if (others == ???) */
6787 printf_filtered (_("Note: breakpoints "));
6788 ALL_BREAKPOINTS (b)
6789 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6790 {
6791 others--;
6792 printf_filtered ("%d", b->number);
6793 if (b->thread == -1 && thread != -1)
6794 printf_filtered (" (all threads)");
6795 else if (b->thread != -1)
6796 printf_filtered (" (thread %d)", b->thread);
6797 printf_filtered ("%s%s ",
6798 ((b->enable_state == bp_disabled
6799 || b->enable_state == bp_call_disabled)
6800 ? " (disabled)"
6801 : ""),
6802 (others > 1) ? ","
6803 : ((others == 1) ? " and" : ""));
6804 }
6805 current_uiout->message (_("also set at pc %ps.\n"),
6806 styled_string (address_style.style (),
6807 paddress (gdbarch, pc)));
6808 }
6809 }
6810 \f
6811
6812 /* Return true iff it is meaningful to use the address member of LOC.
6813 For some breakpoint types, the locations' address members are
6814 irrelevant and it makes no sense to attempt to compare them to
6815 other addresses (or use them for any other purpose either).
6816
6817 More specifically, software watchpoints and catchpoints that are
6818 not backed by breakpoints always have a zero valued location
6819 address and we don't want to mark breakpoints of any of these types
6820 to be a duplicate of an actual breakpoint location at address
6821 zero. */
6822
6823 static bool
6824 bl_address_is_meaningful (bp_location *loc)
6825 {
6826 return loc->loc_type != bp_loc_other;
6827 }
6828
6829 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6830 true if LOC1 and LOC2 represent the same watchpoint location. */
6831
6832 static int
6833 watchpoint_locations_match (struct bp_location *loc1,
6834 struct bp_location *loc2)
6835 {
6836 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6837 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6838
6839 /* Both of them must exist. */
6840 gdb_assert (w1 != NULL);
6841 gdb_assert (w2 != NULL);
6842
6843 /* If the target can evaluate the condition expression in hardware,
6844 then we we need to insert both watchpoints even if they are at
6845 the same place. Otherwise the watchpoint will only trigger when
6846 the condition of whichever watchpoint was inserted evaluates to
6847 true, not giving a chance for GDB to check the condition of the
6848 other watchpoint. */
6849 if ((w1->cond_exp
6850 && target_can_accel_watchpoint_condition (loc1->address,
6851 loc1->length,
6852 loc1->watchpoint_type,
6853 w1->cond_exp.get ()))
6854 || (w2->cond_exp
6855 && target_can_accel_watchpoint_condition (loc2->address,
6856 loc2->length,
6857 loc2->watchpoint_type,
6858 w2->cond_exp.get ())))
6859 return 0;
6860
6861 /* Note that this checks the owner's type, not the location's. In
6862 case the target does not support read watchpoints, but does
6863 support access watchpoints, we'll have bp_read_watchpoint
6864 watchpoints with hw_access locations. Those should be considered
6865 duplicates of hw_read locations. The hw_read locations will
6866 become hw_access locations later. */
6867 return (loc1->owner->type == loc2->owner->type
6868 && loc1->pspace->aspace == loc2->pspace->aspace
6869 && loc1->address == loc2->address
6870 && loc1->length == loc2->length);
6871 }
6872
6873 /* See breakpoint.h. */
6874
6875 int
6876 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6877 const address_space *aspace2, CORE_ADDR addr2)
6878 {
6879 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6880 || aspace1 == aspace2)
6881 && addr1 == addr2);
6882 }
6883
6884 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6885 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6886 matches ASPACE2. On targets that have global breakpoints, the address
6887 space doesn't really matter. */
6888
6889 static int
6890 breakpoint_address_match_range (const address_space *aspace1,
6891 CORE_ADDR addr1,
6892 int len1, const address_space *aspace2,
6893 CORE_ADDR addr2)
6894 {
6895 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6896 || aspace1 == aspace2)
6897 && addr2 >= addr1 && addr2 < addr1 + len1);
6898 }
6899
6900 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6901 a ranged breakpoint. In most targets, a match happens only if ASPACE
6902 matches the breakpoint's address space. On targets that have global
6903 breakpoints, the address space doesn't really matter. */
6904
6905 static int
6906 breakpoint_location_address_match (struct bp_location *bl,
6907 const address_space *aspace,
6908 CORE_ADDR addr)
6909 {
6910 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6911 aspace, addr)
6912 || (bl->length
6913 && breakpoint_address_match_range (bl->pspace->aspace,
6914 bl->address, bl->length,
6915 aspace, addr)));
6916 }
6917
6918 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6919 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6920 match happens only if ASPACE matches the breakpoint's address
6921 space. On targets that have global breakpoints, the address space
6922 doesn't really matter. */
6923
6924 static int
6925 breakpoint_location_address_range_overlap (struct bp_location *bl,
6926 const address_space *aspace,
6927 CORE_ADDR addr, int len)
6928 {
6929 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6930 || bl->pspace->aspace == aspace)
6931 {
6932 int bl_len = bl->length != 0 ? bl->length : 1;
6933
6934 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6935 return 1;
6936 }
6937 return 0;
6938 }
6939
6940 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6941 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6942 true, otherwise returns false. */
6943
6944 static int
6945 tracepoint_locations_match (struct bp_location *loc1,
6946 struct bp_location *loc2)
6947 {
6948 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6949 /* Since tracepoint locations are never duplicated with others', tracepoint
6950 locations at the same address of different tracepoints are regarded as
6951 different locations. */
6952 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6953 else
6954 return 0;
6955 }
6956
6957 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6958 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
6959 the same location. If SW_HW_BPS_MATCH is true, then software
6960 breakpoint locations and hardware breakpoint locations match,
6961 otherwise they don't. */
6962
6963 static int
6964 breakpoint_locations_match (struct bp_location *loc1,
6965 struct bp_location *loc2,
6966 bool sw_hw_bps_match)
6967 {
6968 int hw_point1, hw_point2;
6969
6970 /* Both of them must not be in moribund_locations. */
6971 gdb_assert (loc1->owner != NULL);
6972 gdb_assert (loc2->owner != NULL);
6973
6974 hw_point1 = is_hardware_watchpoint (loc1->owner);
6975 hw_point2 = is_hardware_watchpoint (loc2->owner);
6976
6977 if (hw_point1 != hw_point2)
6978 return 0;
6979 else if (hw_point1)
6980 return watchpoint_locations_match (loc1, loc2);
6981 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6982 return tracepoint_locations_match (loc1, loc2);
6983 else
6984 /* We compare bp_location.length in order to cover ranged
6985 breakpoints. Keep this in sync with
6986 bp_location_is_less_than. */
6987 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6988 loc2->pspace->aspace, loc2->address)
6989 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
6990 && loc1->length == loc2->length);
6991 }
6992
6993 static void
6994 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6995 int bnum, int have_bnum)
6996 {
6997 /* The longest string possibly returned by hex_string_custom
6998 is 50 chars. These must be at least that big for safety. */
6999 char astr1[64];
7000 char astr2[64];
7001
7002 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7003 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7004 if (have_bnum)
7005 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7006 bnum, astr1, astr2);
7007 else
7008 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7009 }
7010
7011 /* Adjust a breakpoint's address to account for architectural
7012 constraints on breakpoint placement. Return the adjusted address.
7013 Note: Very few targets require this kind of adjustment. For most
7014 targets, this function is simply the identity function. */
7015
7016 static CORE_ADDR
7017 adjust_breakpoint_address (struct gdbarch *gdbarch,
7018 CORE_ADDR bpaddr, enum bptype bptype)
7019 {
7020 if (bptype == bp_watchpoint
7021 || bptype == bp_hardware_watchpoint
7022 || bptype == bp_read_watchpoint
7023 || bptype == bp_access_watchpoint
7024 || bptype == bp_catchpoint)
7025 {
7026 /* Watchpoints and the various bp_catch_* eventpoints should not
7027 have their addresses modified. */
7028 return bpaddr;
7029 }
7030 else if (bptype == bp_single_step)
7031 {
7032 /* Single-step breakpoints should not have their addresses
7033 modified. If there's any architectural constrain that
7034 applies to this address, then it should have already been
7035 taken into account when the breakpoint was created in the
7036 first place. If we didn't do this, stepping through e.g.,
7037 Thumb-2 IT blocks would break. */
7038 return bpaddr;
7039 }
7040 else
7041 {
7042 CORE_ADDR adjusted_bpaddr = bpaddr;
7043
7044 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7045 {
7046 /* Some targets have architectural constraints on the placement
7047 of breakpoint instructions. Obtain the adjusted address. */
7048 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7049 }
7050
7051 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
7052
7053 /* An adjusted breakpoint address can significantly alter
7054 a user's expectations. Print a warning if an adjustment
7055 is required. */
7056 if (adjusted_bpaddr != bpaddr)
7057 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7058
7059 return adjusted_bpaddr;
7060 }
7061 }
7062
7063 static bp_loc_type
7064 bp_location_from_bp_type (bptype type)
7065 {
7066 switch (type)
7067 {
7068 case bp_breakpoint:
7069 case bp_single_step:
7070 case bp_until:
7071 case bp_finish:
7072 case bp_longjmp:
7073 case bp_longjmp_resume:
7074 case bp_longjmp_call_dummy:
7075 case bp_exception:
7076 case bp_exception_resume:
7077 case bp_step_resume:
7078 case bp_hp_step_resume:
7079 case bp_watchpoint_scope:
7080 case bp_call_dummy:
7081 case bp_std_terminate:
7082 case bp_shlib_event:
7083 case bp_thread_event:
7084 case bp_overlay_event:
7085 case bp_jit_event:
7086 case bp_longjmp_master:
7087 case bp_std_terminate_master:
7088 case bp_exception_master:
7089 case bp_gnu_ifunc_resolver:
7090 case bp_gnu_ifunc_resolver_return:
7091 case bp_dprintf:
7092 return bp_loc_software_breakpoint;
7093 case bp_hardware_breakpoint:
7094 return bp_loc_hardware_breakpoint;
7095 case bp_hardware_watchpoint:
7096 case bp_read_watchpoint:
7097 case bp_access_watchpoint:
7098 return bp_loc_hardware_watchpoint;
7099 case bp_watchpoint:
7100 case bp_catchpoint:
7101 case bp_tracepoint:
7102 case bp_fast_tracepoint:
7103 case bp_static_tracepoint:
7104 return bp_loc_other;
7105 default:
7106 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7107 }
7108 }
7109
7110 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7111 {
7112 this->owner = owner;
7113 this->cond_bytecode = NULL;
7114 this->shlib_disabled = 0;
7115 this->enabled = 1;
7116 this->disabled_by_cond = false;
7117
7118 this->loc_type = type;
7119
7120 if (this->loc_type == bp_loc_software_breakpoint
7121 || this->loc_type == bp_loc_hardware_breakpoint)
7122 mark_breakpoint_location_modified (this);
7123
7124 incref ();
7125 }
7126
7127 bp_location::bp_location (breakpoint *owner)
7128 : bp_location::bp_location (owner,
7129 bp_location_from_bp_type (owner->type))
7130 {
7131 }
7132
7133 /* Allocate a struct bp_location. */
7134
7135 static struct bp_location *
7136 allocate_bp_location (struct breakpoint *bpt)
7137 {
7138 return bpt->ops->allocate_location (bpt);
7139 }
7140
7141 /* Decrement reference count. If the reference count reaches 0,
7142 destroy the bp_location. Sets *BLP to NULL. */
7143
7144 static void
7145 decref_bp_location (struct bp_location **blp)
7146 {
7147 bp_location_ref_policy::decref (*blp);
7148 *blp = NULL;
7149 }
7150
7151 /* Add breakpoint B at the end of the global breakpoint chain. */
7152
7153 static breakpoint *
7154 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7155 {
7156 struct breakpoint *b1;
7157 struct breakpoint *result = b.get ();
7158
7159 /* Add this breakpoint to the end of the chain so that a list of
7160 breakpoints will come out in order of increasing numbers. */
7161
7162 b1 = breakpoint_chain;
7163 if (b1 == 0)
7164 breakpoint_chain = b.release ();
7165 else
7166 {
7167 while (b1->next)
7168 b1 = b1->next;
7169 b1->next = b.release ();
7170 }
7171
7172 return result;
7173 }
7174
7175 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7176
7177 static void
7178 init_raw_breakpoint_without_location (struct breakpoint *b,
7179 struct gdbarch *gdbarch,
7180 enum bptype bptype,
7181 const struct breakpoint_ops *ops)
7182 {
7183 gdb_assert (ops != NULL);
7184
7185 b->ops = ops;
7186 b->type = bptype;
7187 b->gdbarch = gdbarch;
7188 b->language = current_language->la_language;
7189 b->input_radix = input_radix;
7190 b->related_breakpoint = b;
7191 }
7192
7193 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7194 that has type BPTYPE and has no locations as yet. */
7195
7196 static struct breakpoint *
7197 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7198 enum bptype bptype,
7199 const struct breakpoint_ops *ops)
7200 {
7201 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7202
7203 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7204 return add_to_breakpoint_chain (std::move (b));
7205 }
7206
7207 /* Initialize loc->function_name. */
7208
7209 static void
7210 set_breakpoint_location_function (struct bp_location *loc)
7211 {
7212 gdb_assert (loc->owner != NULL);
7213
7214 if (loc->owner->type == bp_breakpoint
7215 || loc->owner->type == bp_hardware_breakpoint
7216 || is_tracepoint (loc->owner))
7217 {
7218 const char *function_name;
7219
7220 if (loc->msymbol != NULL
7221 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
7222 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
7223 {
7224 struct breakpoint *b = loc->owner;
7225
7226 function_name = loc->msymbol->linkage_name ();
7227
7228 if (b->type == bp_breakpoint && b->loc == loc
7229 && loc->next == NULL && b->related_breakpoint == b)
7230 {
7231 /* Create only the whole new breakpoint of this type but do not
7232 mess more complicated breakpoints with multiple locations. */
7233 b->type = bp_gnu_ifunc_resolver;
7234 /* Remember the resolver's address for use by the return
7235 breakpoint. */
7236 loc->related_address = loc->address;
7237 }
7238 }
7239 else
7240 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7241
7242 if (function_name)
7243 loc->function_name = xstrdup (function_name);
7244 }
7245 }
7246
7247 /* Attempt to determine architecture of location identified by SAL. */
7248 struct gdbarch *
7249 get_sal_arch (struct symtab_and_line sal)
7250 {
7251 if (sal.section)
7252 return sal.section->objfile->arch ();
7253 if (sal.symtab)
7254 return SYMTAB_OBJFILE (sal.symtab)->arch ();
7255
7256 return NULL;
7257 }
7258
7259 /* Low level routine for partially initializing a breakpoint of type
7260 BPTYPE. The newly created breakpoint's address, section, source
7261 file name, and line number are provided by SAL.
7262
7263 It is expected that the caller will complete the initialization of
7264 the newly created breakpoint struct as well as output any status
7265 information regarding the creation of a new breakpoint. */
7266
7267 static void
7268 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7269 struct symtab_and_line sal, enum bptype bptype,
7270 const struct breakpoint_ops *ops)
7271 {
7272 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7273
7274 add_location_to_breakpoint (b, &sal);
7275
7276 if (bptype != bp_catchpoint)
7277 gdb_assert (sal.pspace != NULL);
7278
7279 /* Store the program space that was used to set the breakpoint,
7280 except for ordinary breakpoints, which are independent of the
7281 program space. */
7282 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7283 b->pspace = sal.pspace;
7284 }
7285
7286 /* set_raw_breakpoint is a low level routine for allocating and
7287 partially initializing a breakpoint of type BPTYPE. The newly
7288 created breakpoint's address, section, source file name, and line
7289 number are provided by SAL. The newly created and partially
7290 initialized breakpoint is added to the breakpoint chain and
7291 is also returned as the value of this function.
7292
7293 It is expected that the caller will complete the initialization of
7294 the newly created breakpoint struct as well as output any status
7295 information regarding the creation of a new breakpoint. In
7296 particular, set_raw_breakpoint does NOT set the breakpoint
7297 number! Care should be taken to not allow an error to occur
7298 prior to completing the initialization of the breakpoint. If this
7299 should happen, a bogus breakpoint will be left on the chain. */
7300
7301 struct breakpoint *
7302 set_raw_breakpoint (struct gdbarch *gdbarch,
7303 struct symtab_and_line sal, enum bptype bptype,
7304 const struct breakpoint_ops *ops)
7305 {
7306 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7307
7308 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7309 return add_to_breakpoint_chain (std::move (b));
7310 }
7311
7312 /* Call this routine when stepping and nexting to enable a breakpoint
7313 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7314 initiated the operation. */
7315
7316 void
7317 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7318 {
7319 struct breakpoint *b, *b_tmp;
7320 int thread = tp->global_num;
7321
7322 /* To avoid having to rescan all objfile symbols at every step,
7323 we maintain a list of continually-inserted but always disabled
7324 longjmp "master" breakpoints. Here, we simply create momentary
7325 clones of those and enable them for the requested thread. */
7326 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7327 if (b->pspace == current_program_space
7328 && (b->type == bp_longjmp_master
7329 || b->type == bp_exception_master))
7330 {
7331 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7332 struct breakpoint *clone;
7333
7334 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7335 after their removal. */
7336 clone = momentary_breakpoint_from_master (b, type,
7337 &momentary_breakpoint_ops, 1);
7338 clone->thread = thread;
7339 }
7340
7341 tp->initiating_frame = frame;
7342 }
7343
7344 /* Delete all longjmp breakpoints from THREAD. */
7345 void
7346 delete_longjmp_breakpoint (int thread)
7347 {
7348 struct breakpoint *b, *b_tmp;
7349
7350 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7351 if (b->type == bp_longjmp || b->type == bp_exception)
7352 {
7353 if (b->thread == thread)
7354 delete_breakpoint (b);
7355 }
7356 }
7357
7358 void
7359 delete_longjmp_breakpoint_at_next_stop (int thread)
7360 {
7361 struct breakpoint *b, *b_tmp;
7362
7363 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7364 if (b->type == bp_longjmp || b->type == bp_exception)
7365 {
7366 if (b->thread == thread)
7367 b->disposition = disp_del_at_next_stop;
7368 }
7369 }
7370
7371 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7372 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7373 pointer to any of them. Return NULL if this system cannot place longjmp
7374 breakpoints. */
7375
7376 struct breakpoint *
7377 set_longjmp_breakpoint_for_call_dummy (void)
7378 {
7379 struct breakpoint *b, *retval = NULL;
7380
7381 ALL_BREAKPOINTS (b)
7382 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7383 {
7384 struct breakpoint *new_b;
7385
7386 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7387 &momentary_breakpoint_ops,
7388 1);
7389 new_b->thread = inferior_thread ()->global_num;
7390
7391 /* Link NEW_B into the chain of RETVAL breakpoints. */
7392
7393 gdb_assert (new_b->related_breakpoint == new_b);
7394 if (retval == NULL)
7395 retval = new_b;
7396 new_b->related_breakpoint = retval;
7397 while (retval->related_breakpoint != new_b->related_breakpoint)
7398 retval = retval->related_breakpoint;
7399 retval->related_breakpoint = new_b;
7400 }
7401
7402 return retval;
7403 }
7404
7405 /* Verify all existing dummy frames and their associated breakpoints for
7406 TP. Remove those which can no longer be found in the current frame
7407 stack.
7408
7409 You should call this function only at places where it is safe to currently
7410 unwind the whole stack. Failed stack unwind would discard live dummy
7411 frames. */
7412
7413 void
7414 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7415 {
7416 struct breakpoint *b, *b_tmp;
7417
7418 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7419 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7420 {
7421 struct breakpoint *dummy_b = b->related_breakpoint;
7422
7423 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7424 dummy_b = dummy_b->related_breakpoint;
7425 if (dummy_b->type != bp_call_dummy
7426 || frame_find_by_id (dummy_b->frame_id) != NULL)
7427 continue;
7428
7429 dummy_frame_discard (dummy_b->frame_id, tp);
7430
7431 while (b->related_breakpoint != b)
7432 {
7433 if (b_tmp == b->related_breakpoint)
7434 b_tmp = b->related_breakpoint->next;
7435 delete_breakpoint (b->related_breakpoint);
7436 }
7437 delete_breakpoint (b);
7438 }
7439 }
7440
7441 void
7442 enable_overlay_breakpoints (void)
7443 {
7444 struct breakpoint *b;
7445
7446 ALL_BREAKPOINTS (b)
7447 if (b->type == bp_overlay_event)
7448 {
7449 b->enable_state = bp_enabled;
7450 update_global_location_list (UGLL_MAY_INSERT);
7451 overlay_events_enabled = 1;
7452 }
7453 }
7454
7455 void
7456 disable_overlay_breakpoints (void)
7457 {
7458 struct breakpoint *b;
7459
7460 ALL_BREAKPOINTS (b)
7461 if (b->type == bp_overlay_event)
7462 {
7463 b->enable_state = bp_disabled;
7464 update_global_location_list (UGLL_DONT_INSERT);
7465 overlay_events_enabled = 0;
7466 }
7467 }
7468
7469 /* Set an active std::terminate breakpoint for each std::terminate
7470 master breakpoint. */
7471 void
7472 set_std_terminate_breakpoint (void)
7473 {
7474 struct breakpoint *b, *b_tmp;
7475
7476 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7477 if (b->pspace == current_program_space
7478 && b->type == bp_std_terminate_master)
7479 {
7480 momentary_breakpoint_from_master (b, bp_std_terminate,
7481 &momentary_breakpoint_ops, 1);
7482 }
7483 }
7484
7485 /* Delete all the std::terminate breakpoints. */
7486 void
7487 delete_std_terminate_breakpoint (void)
7488 {
7489 struct breakpoint *b, *b_tmp;
7490
7491 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7492 if (b->type == bp_std_terminate)
7493 delete_breakpoint (b);
7494 }
7495
7496 struct breakpoint *
7497 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7498 {
7499 struct breakpoint *b;
7500
7501 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7502 &internal_breakpoint_ops);
7503
7504 b->enable_state = bp_enabled;
7505 /* location has to be used or breakpoint_re_set will delete me. */
7506 b->location = new_address_location (b->loc->address, NULL, 0);
7507
7508 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7509
7510 return b;
7511 }
7512
7513 struct lang_and_radix
7514 {
7515 enum language lang;
7516 int radix;
7517 };
7518
7519 /* Create a breakpoint for JIT code registration and unregistration. */
7520
7521 struct breakpoint *
7522 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7523 {
7524 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7525 &internal_breakpoint_ops);
7526 }
7527
7528 /* Remove JIT code registration and unregistration breakpoint(s). */
7529
7530 void
7531 remove_jit_event_breakpoints (void)
7532 {
7533 struct breakpoint *b, *b_tmp;
7534
7535 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7536 if (b->type == bp_jit_event
7537 && b->loc->pspace == current_program_space)
7538 delete_breakpoint (b);
7539 }
7540
7541 void
7542 remove_solib_event_breakpoints (void)
7543 {
7544 struct breakpoint *b, *b_tmp;
7545
7546 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7547 if (b->type == bp_shlib_event
7548 && b->loc->pspace == current_program_space)
7549 delete_breakpoint (b);
7550 }
7551
7552 /* See breakpoint.h. */
7553
7554 void
7555 remove_solib_event_breakpoints_at_next_stop (void)
7556 {
7557 struct breakpoint *b, *b_tmp;
7558
7559 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7560 if (b->type == bp_shlib_event
7561 && b->loc->pspace == current_program_space)
7562 b->disposition = disp_del_at_next_stop;
7563 }
7564
7565 /* Helper for create_solib_event_breakpoint /
7566 create_and_insert_solib_event_breakpoint. Allows specifying which
7567 INSERT_MODE to pass through to update_global_location_list. */
7568
7569 static struct breakpoint *
7570 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7571 enum ugll_insert_mode insert_mode)
7572 {
7573 struct breakpoint *b;
7574
7575 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7576 &internal_breakpoint_ops);
7577 update_global_location_list_nothrow (insert_mode);
7578 return b;
7579 }
7580
7581 struct breakpoint *
7582 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7583 {
7584 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7585 }
7586
7587 /* See breakpoint.h. */
7588
7589 struct breakpoint *
7590 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7591 {
7592 struct breakpoint *b;
7593
7594 /* Explicitly tell update_global_location_list to insert
7595 locations. */
7596 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7597 if (!b->loc->inserted)
7598 {
7599 delete_breakpoint (b);
7600 return NULL;
7601 }
7602 return b;
7603 }
7604
7605 /* Disable any breakpoints that are on code in shared libraries. Only
7606 apply to enabled breakpoints, disabled ones can just stay disabled. */
7607
7608 void
7609 disable_breakpoints_in_shlibs (void)
7610 {
7611 struct bp_location *loc, **locp_tmp;
7612
7613 ALL_BP_LOCATIONS (loc, locp_tmp)
7614 {
7615 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7616 struct breakpoint *b = loc->owner;
7617
7618 /* We apply the check to all breakpoints, including disabled for
7619 those with loc->duplicate set. This is so that when breakpoint
7620 becomes enabled, or the duplicate is removed, gdb will try to
7621 insert all breakpoints. If we don't set shlib_disabled here,
7622 we'll try to insert those breakpoints and fail. */
7623 if (((b->type == bp_breakpoint)
7624 || (b->type == bp_jit_event)
7625 || (b->type == bp_hardware_breakpoint)
7626 || (is_tracepoint (b)))
7627 && loc->pspace == current_program_space
7628 && !loc->shlib_disabled
7629 && solib_name_from_address (loc->pspace, loc->address)
7630 )
7631 {
7632 loc->shlib_disabled = 1;
7633 }
7634 }
7635 }
7636
7637 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7638 notification of unloaded_shlib. Only apply to enabled breakpoints,
7639 disabled ones can just stay disabled. */
7640
7641 static void
7642 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7643 {
7644 struct bp_location *loc, **locp_tmp;
7645 int disabled_shlib_breaks = 0;
7646
7647 ALL_BP_LOCATIONS (loc, locp_tmp)
7648 {
7649 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7650 struct breakpoint *b = loc->owner;
7651
7652 if (solib->pspace == loc->pspace
7653 && !loc->shlib_disabled
7654 && (((b->type == bp_breakpoint
7655 || b->type == bp_jit_event
7656 || b->type == bp_hardware_breakpoint)
7657 && (loc->loc_type == bp_loc_hardware_breakpoint
7658 || loc->loc_type == bp_loc_software_breakpoint))
7659 || is_tracepoint (b))
7660 && solib_contains_address_p (solib, loc->address))
7661 {
7662 loc->shlib_disabled = 1;
7663 /* At this point, we cannot rely on remove_breakpoint
7664 succeeding so we must mark the breakpoint as not inserted
7665 to prevent future errors occurring in remove_breakpoints. */
7666 loc->inserted = 0;
7667
7668 /* This may cause duplicate notifications for the same breakpoint. */
7669 gdb::observers::breakpoint_modified.notify (b);
7670
7671 if (!disabled_shlib_breaks)
7672 {
7673 target_terminal::ours_for_output ();
7674 warning (_("Temporarily disabling breakpoints "
7675 "for unloaded shared library \"%s\""),
7676 solib->so_name);
7677 }
7678 disabled_shlib_breaks = 1;
7679 }
7680 }
7681 }
7682
7683 /* Disable any breakpoints and tracepoints in OBJFILE upon
7684 notification of free_objfile. Only apply to enabled breakpoints,
7685 disabled ones can just stay disabled. */
7686
7687 static void
7688 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7689 {
7690 struct breakpoint *b;
7691
7692 if (objfile == NULL)
7693 return;
7694
7695 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7696 managed by the user with add-symbol-file/remove-symbol-file.
7697 Similarly to how breakpoints in shared libraries are handled in
7698 response to "nosharedlibrary", mark breakpoints in such modules
7699 shlib_disabled so they end up uninserted on the next global
7700 location list update. Shared libraries not loaded by the user
7701 aren't handled here -- they're already handled in
7702 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7703 solib_unloaded observer. We skip objfiles that are not
7704 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7705 main objfile). */
7706 if ((objfile->flags & OBJF_SHARED) == 0
7707 || (objfile->flags & OBJF_USERLOADED) == 0)
7708 return;
7709
7710 ALL_BREAKPOINTS (b)
7711 {
7712 struct bp_location *loc;
7713 int bp_modified = 0;
7714
7715 if (!is_breakpoint (b) && !is_tracepoint (b))
7716 continue;
7717
7718 for (loc = b->loc; loc != NULL; loc = loc->next)
7719 {
7720 CORE_ADDR loc_addr = loc->address;
7721
7722 if (loc->loc_type != bp_loc_hardware_breakpoint
7723 && loc->loc_type != bp_loc_software_breakpoint)
7724 continue;
7725
7726 if (loc->shlib_disabled != 0)
7727 continue;
7728
7729 if (objfile->pspace != loc->pspace)
7730 continue;
7731
7732 if (loc->loc_type != bp_loc_hardware_breakpoint
7733 && loc->loc_type != bp_loc_software_breakpoint)
7734 continue;
7735
7736 if (is_addr_in_objfile (loc_addr, objfile))
7737 {
7738 loc->shlib_disabled = 1;
7739 /* At this point, we don't know whether the object was
7740 unmapped from the inferior or not, so leave the
7741 inserted flag alone. We'll handle failure to
7742 uninsert quietly, in case the object was indeed
7743 unmapped. */
7744
7745 mark_breakpoint_location_modified (loc);
7746
7747 bp_modified = 1;
7748 }
7749 }
7750
7751 if (bp_modified)
7752 gdb::observers::breakpoint_modified.notify (b);
7753 }
7754 }
7755
7756 /* FORK & VFORK catchpoints. */
7757
7758 /* An instance of this type is used to represent a fork or vfork
7759 catchpoint. A breakpoint is really of this type iff its ops pointer points
7760 to CATCH_FORK_BREAKPOINT_OPS. */
7761
7762 struct fork_catchpoint : public breakpoint
7763 {
7764 /* Process id of a child process whose forking triggered this
7765 catchpoint. This field is only valid immediately after this
7766 catchpoint has triggered. */
7767 ptid_t forked_inferior_pid;
7768 };
7769
7770 /* Implement the "insert" breakpoint_ops method for fork
7771 catchpoints. */
7772
7773 static int
7774 insert_catch_fork (struct bp_location *bl)
7775 {
7776 return target_insert_fork_catchpoint (inferior_ptid.pid ());
7777 }
7778
7779 /* Implement the "remove" breakpoint_ops method for fork
7780 catchpoints. */
7781
7782 static int
7783 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
7784 {
7785 return target_remove_fork_catchpoint (inferior_ptid.pid ());
7786 }
7787
7788 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7789 catchpoints. */
7790
7791 static int
7792 breakpoint_hit_catch_fork (const struct bp_location *bl,
7793 const address_space *aspace, CORE_ADDR bp_addr,
7794 const struct target_waitstatus *ws)
7795 {
7796 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7797
7798 if (ws->kind != TARGET_WAITKIND_FORKED)
7799 return 0;
7800
7801 c->forked_inferior_pid = ws->value.related_pid;
7802 return 1;
7803 }
7804
7805 /* Implement the "print_it" breakpoint_ops method for fork
7806 catchpoints. */
7807
7808 static enum print_stop_action
7809 print_it_catch_fork (bpstat bs)
7810 {
7811 struct ui_out *uiout = current_uiout;
7812 struct breakpoint *b = bs->breakpoint_at;
7813 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7814
7815 annotate_catchpoint (b->number);
7816 maybe_print_thread_hit_breakpoint (uiout);
7817 if (b->disposition == disp_del)
7818 uiout->text ("Temporary catchpoint ");
7819 else
7820 uiout->text ("Catchpoint ");
7821 if (uiout->is_mi_like_p ())
7822 {
7823 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7824 uiout->field_string ("disp", bpdisp_text (b->disposition));
7825 }
7826 uiout->field_signed ("bkptno", b->number);
7827 uiout->text (" (forked process ");
7828 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7829 uiout->text ("), ");
7830 return PRINT_SRC_AND_LOC;
7831 }
7832
7833 /* Implement the "print_one" breakpoint_ops method for fork
7834 catchpoints. */
7835
7836 static void
7837 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7838 {
7839 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7840 struct value_print_options opts;
7841 struct ui_out *uiout = current_uiout;
7842
7843 get_user_print_options (&opts);
7844
7845 /* Field 4, the address, is omitted (which makes the columns not
7846 line up too nicely with the headers, but the effect is relatively
7847 readable). */
7848 if (opts.addressprint)
7849 uiout->field_skip ("addr");
7850 annotate_field (5);
7851 uiout->text ("fork");
7852 if (c->forked_inferior_pid != null_ptid)
7853 {
7854 uiout->text (", process ");
7855 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7856 uiout->spaces (1);
7857 }
7858
7859 if (uiout->is_mi_like_p ())
7860 uiout->field_string ("catch-type", "fork");
7861 }
7862
7863 /* Implement the "print_mention" breakpoint_ops method for fork
7864 catchpoints. */
7865
7866 static void
7867 print_mention_catch_fork (struct breakpoint *b)
7868 {
7869 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7870 }
7871
7872 /* Implement the "print_recreate" breakpoint_ops method for fork
7873 catchpoints. */
7874
7875 static void
7876 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7877 {
7878 fprintf_unfiltered (fp, "catch fork");
7879 print_recreate_thread (b, fp);
7880 }
7881
7882 /* The breakpoint_ops structure to be used in fork catchpoints. */
7883
7884 static struct breakpoint_ops catch_fork_breakpoint_ops;
7885
7886 /* Implement the "insert" breakpoint_ops method for vfork
7887 catchpoints. */
7888
7889 static int
7890 insert_catch_vfork (struct bp_location *bl)
7891 {
7892 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
7893 }
7894
7895 /* Implement the "remove" breakpoint_ops method for vfork
7896 catchpoints. */
7897
7898 static int
7899 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
7900 {
7901 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
7902 }
7903
7904 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7905 catchpoints. */
7906
7907 static int
7908 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7909 const address_space *aspace, CORE_ADDR bp_addr,
7910 const struct target_waitstatus *ws)
7911 {
7912 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7913
7914 if (ws->kind != TARGET_WAITKIND_VFORKED)
7915 return 0;
7916
7917 c->forked_inferior_pid = ws->value.related_pid;
7918 return 1;
7919 }
7920
7921 /* Implement the "print_it" breakpoint_ops method for vfork
7922 catchpoints. */
7923
7924 static enum print_stop_action
7925 print_it_catch_vfork (bpstat bs)
7926 {
7927 struct ui_out *uiout = current_uiout;
7928 struct breakpoint *b = bs->breakpoint_at;
7929 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7930
7931 annotate_catchpoint (b->number);
7932 maybe_print_thread_hit_breakpoint (uiout);
7933 if (b->disposition == disp_del)
7934 uiout->text ("Temporary catchpoint ");
7935 else
7936 uiout->text ("Catchpoint ");
7937 if (uiout->is_mi_like_p ())
7938 {
7939 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7940 uiout->field_string ("disp", bpdisp_text (b->disposition));
7941 }
7942 uiout->field_signed ("bkptno", b->number);
7943 uiout->text (" (vforked process ");
7944 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7945 uiout->text ("), ");
7946 return PRINT_SRC_AND_LOC;
7947 }
7948
7949 /* Implement the "print_one" breakpoint_ops method for vfork
7950 catchpoints. */
7951
7952 static void
7953 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7954 {
7955 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7956 struct value_print_options opts;
7957 struct ui_out *uiout = current_uiout;
7958
7959 get_user_print_options (&opts);
7960 /* Field 4, the address, is omitted (which makes the columns not
7961 line up too nicely with the headers, but the effect is relatively
7962 readable). */
7963 if (opts.addressprint)
7964 uiout->field_skip ("addr");
7965 annotate_field (5);
7966 uiout->text ("vfork");
7967 if (c->forked_inferior_pid != null_ptid)
7968 {
7969 uiout->text (", process ");
7970 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7971 uiout->spaces (1);
7972 }
7973
7974 if (uiout->is_mi_like_p ())
7975 uiout->field_string ("catch-type", "vfork");
7976 }
7977
7978 /* Implement the "print_mention" breakpoint_ops method for vfork
7979 catchpoints. */
7980
7981 static void
7982 print_mention_catch_vfork (struct breakpoint *b)
7983 {
7984 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7985 }
7986
7987 /* Implement the "print_recreate" breakpoint_ops method for vfork
7988 catchpoints. */
7989
7990 static void
7991 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7992 {
7993 fprintf_unfiltered (fp, "catch vfork");
7994 print_recreate_thread (b, fp);
7995 }
7996
7997 /* The breakpoint_ops structure to be used in vfork catchpoints. */
7998
7999 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8000
8001 /* An instance of this type is used to represent an solib catchpoint.
8002 A breakpoint is really of this type iff its ops pointer points to
8003 CATCH_SOLIB_BREAKPOINT_OPS. */
8004
8005 struct solib_catchpoint : public breakpoint
8006 {
8007 ~solib_catchpoint () override;
8008
8009 /* True for "catch load", false for "catch unload". */
8010 bool is_load;
8011
8012 /* Regular expression to match, if any. COMPILED is only valid when
8013 REGEX is non-NULL. */
8014 char *regex;
8015 std::unique_ptr<compiled_regex> compiled;
8016 };
8017
8018 solib_catchpoint::~solib_catchpoint ()
8019 {
8020 xfree (this->regex);
8021 }
8022
8023 static int
8024 insert_catch_solib (struct bp_location *ignore)
8025 {
8026 return 0;
8027 }
8028
8029 static int
8030 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
8031 {
8032 return 0;
8033 }
8034
8035 static int
8036 breakpoint_hit_catch_solib (const struct bp_location *bl,
8037 const address_space *aspace,
8038 CORE_ADDR bp_addr,
8039 const struct target_waitstatus *ws)
8040 {
8041 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8042 struct breakpoint *other;
8043
8044 if (ws->kind == TARGET_WAITKIND_LOADED)
8045 return 1;
8046
8047 ALL_BREAKPOINTS (other)
8048 {
8049 struct bp_location *other_bl;
8050
8051 if (other == bl->owner)
8052 continue;
8053
8054 if (other->type != bp_shlib_event)
8055 continue;
8056
8057 if (self->pspace != NULL && other->pspace != self->pspace)
8058 continue;
8059
8060 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8061 {
8062 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8063 return 1;
8064 }
8065 }
8066
8067 return 0;
8068 }
8069
8070 static void
8071 check_status_catch_solib (struct bpstats *bs)
8072 {
8073 struct solib_catchpoint *self
8074 = (struct solib_catchpoint *) bs->breakpoint_at;
8075
8076 if (self->is_load)
8077 {
8078 for (so_list *iter : current_program_space->added_solibs)
8079 {
8080 if (!self->regex
8081 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
8082 return;
8083 }
8084 }
8085 else
8086 {
8087 for (const std::string &iter : current_program_space->deleted_solibs)
8088 {
8089 if (!self->regex
8090 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
8091 return;
8092 }
8093 }
8094
8095 bs->stop = 0;
8096 bs->print_it = print_it_noop;
8097 }
8098
8099 static enum print_stop_action
8100 print_it_catch_solib (bpstat bs)
8101 {
8102 struct breakpoint *b = bs->breakpoint_at;
8103 struct ui_out *uiout = current_uiout;
8104
8105 annotate_catchpoint (b->number);
8106 maybe_print_thread_hit_breakpoint (uiout);
8107 if (b->disposition == disp_del)
8108 uiout->text ("Temporary catchpoint ");
8109 else
8110 uiout->text ("Catchpoint ");
8111 uiout->field_signed ("bkptno", b->number);
8112 uiout->text ("\n");
8113 if (uiout->is_mi_like_p ())
8114 uiout->field_string ("disp", bpdisp_text (b->disposition));
8115 print_solib_event (1);
8116 return PRINT_SRC_AND_LOC;
8117 }
8118
8119 static void
8120 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8121 {
8122 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8123 struct value_print_options opts;
8124 struct ui_out *uiout = current_uiout;
8125
8126 get_user_print_options (&opts);
8127 /* Field 4, the address, is omitted (which makes the columns not
8128 line up too nicely with the headers, but the effect is relatively
8129 readable). */
8130 if (opts.addressprint)
8131 {
8132 annotate_field (4);
8133 uiout->field_skip ("addr");
8134 }
8135
8136 std::string msg;
8137 annotate_field (5);
8138 if (self->is_load)
8139 {
8140 if (self->regex)
8141 msg = string_printf (_("load of library matching %s"), self->regex);
8142 else
8143 msg = _("load of library");
8144 }
8145 else
8146 {
8147 if (self->regex)
8148 msg = string_printf (_("unload of library matching %s"), self->regex);
8149 else
8150 msg = _("unload of library");
8151 }
8152 uiout->field_string ("what", msg);
8153
8154 if (uiout->is_mi_like_p ())
8155 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8156 }
8157
8158 static void
8159 print_mention_catch_solib (struct breakpoint *b)
8160 {
8161 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8162
8163 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8164 self->is_load ? "load" : "unload");
8165 }
8166
8167 static void
8168 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8169 {
8170 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8171
8172 fprintf_unfiltered (fp, "%s %s",
8173 b->disposition == disp_del ? "tcatch" : "catch",
8174 self->is_load ? "load" : "unload");
8175 if (self->regex)
8176 fprintf_unfiltered (fp, " %s", self->regex);
8177 fprintf_unfiltered (fp, "\n");
8178 }
8179
8180 static struct breakpoint_ops catch_solib_breakpoint_ops;
8181
8182 /* See breakpoint.h. */
8183
8184 void
8185 add_solib_catchpoint (const char *arg, bool is_load, bool is_temp, bool enabled)
8186 {
8187 struct gdbarch *gdbarch = get_current_arch ();
8188
8189 if (!arg)
8190 arg = "";
8191 arg = skip_spaces (arg);
8192
8193 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
8194
8195 if (*arg != '\0')
8196 {
8197 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8198 _("Invalid regexp")));
8199 c->regex = xstrdup (arg);
8200 }
8201
8202 c->is_load = is_load;
8203 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
8204 &catch_solib_breakpoint_ops);
8205
8206 c->enable_state = enabled ? bp_enabled : bp_disabled;
8207
8208 install_breakpoint (0, std::move (c), 1);
8209 }
8210
8211 /* A helper function that does all the work for "catch load" and
8212 "catch unload". */
8213
8214 static void
8215 catch_load_or_unload (const char *arg, int from_tty, int is_load,
8216 struct cmd_list_element *command)
8217 {
8218 const int enabled = 1;
8219 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
8220
8221 add_solib_catchpoint (arg, is_load, temp, enabled);
8222 }
8223
8224 static void
8225 catch_load_command_1 (const char *arg, int from_tty,
8226 struct cmd_list_element *command)
8227 {
8228 catch_load_or_unload (arg, from_tty, 1, command);
8229 }
8230
8231 static void
8232 catch_unload_command_1 (const char *arg, int from_tty,
8233 struct cmd_list_element *command)
8234 {
8235 catch_load_or_unload (arg, from_tty, 0, command);
8236 }
8237
8238 /* See breakpoint.h. */
8239
8240 void
8241 init_catchpoint (struct breakpoint *b,
8242 struct gdbarch *gdbarch, bool temp,
8243 const char *cond_string,
8244 const struct breakpoint_ops *ops)
8245 {
8246 symtab_and_line sal;
8247 sal.pspace = current_program_space;
8248
8249 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8250
8251 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8252 b->disposition = temp ? disp_del : disp_donttouch;
8253 }
8254
8255 void
8256 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8257 {
8258 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8259 set_breakpoint_number (internal, b);
8260 if (is_tracepoint (b))
8261 set_tracepoint_count (breakpoint_count);
8262 if (!internal)
8263 mention (b);
8264 gdb::observers::breakpoint_created.notify (b);
8265
8266 if (update_gll)
8267 update_global_location_list (UGLL_MAY_INSERT);
8268 }
8269
8270 static void
8271 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8272 bool temp, const char *cond_string,
8273 const struct breakpoint_ops *ops)
8274 {
8275 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
8276
8277 init_catchpoint (c.get (), gdbarch, temp, cond_string, ops);
8278
8279 c->forked_inferior_pid = null_ptid;
8280
8281 install_breakpoint (0, std::move (c), 1);
8282 }
8283
8284 /* Exec catchpoints. */
8285
8286 /* An instance of this type is used to represent an exec catchpoint.
8287 A breakpoint is really of this type iff its ops pointer points to
8288 CATCH_EXEC_BREAKPOINT_OPS. */
8289
8290 struct exec_catchpoint : public breakpoint
8291 {
8292 ~exec_catchpoint () override;
8293
8294 /* Filename of a program whose exec triggered this catchpoint.
8295 This field is only valid immediately after this catchpoint has
8296 triggered. */
8297 char *exec_pathname;
8298 };
8299
8300 /* Exec catchpoint destructor. */
8301
8302 exec_catchpoint::~exec_catchpoint ()
8303 {
8304 xfree (this->exec_pathname);
8305 }
8306
8307 static int
8308 insert_catch_exec (struct bp_location *bl)
8309 {
8310 return target_insert_exec_catchpoint (inferior_ptid.pid ());
8311 }
8312
8313 static int
8314 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8315 {
8316 return target_remove_exec_catchpoint (inferior_ptid.pid ());
8317 }
8318
8319 static int
8320 breakpoint_hit_catch_exec (const struct bp_location *bl,
8321 const address_space *aspace, CORE_ADDR bp_addr,
8322 const struct target_waitstatus *ws)
8323 {
8324 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8325
8326 if (ws->kind != TARGET_WAITKIND_EXECD)
8327 return 0;
8328
8329 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8330 return 1;
8331 }
8332
8333 static enum print_stop_action
8334 print_it_catch_exec (bpstat bs)
8335 {
8336 struct ui_out *uiout = current_uiout;
8337 struct breakpoint *b = bs->breakpoint_at;
8338 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8339
8340 annotate_catchpoint (b->number);
8341 maybe_print_thread_hit_breakpoint (uiout);
8342 if (b->disposition == disp_del)
8343 uiout->text ("Temporary catchpoint ");
8344 else
8345 uiout->text ("Catchpoint ");
8346 if (uiout->is_mi_like_p ())
8347 {
8348 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8349 uiout->field_string ("disp", bpdisp_text (b->disposition));
8350 }
8351 uiout->field_signed ("bkptno", b->number);
8352 uiout->text (" (exec'd ");
8353 uiout->field_string ("new-exec", c->exec_pathname);
8354 uiout->text ("), ");
8355
8356 return PRINT_SRC_AND_LOC;
8357 }
8358
8359 static void
8360 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8361 {
8362 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8363 struct value_print_options opts;
8364 struct ui_out *uiout = current_uiout;
8365
8366 get_user_print_options (&opts);
8367
8368 /* Field 4, the address, is omitted (which makes the columns
8369 not line up too nicely with the headers, but the effect
8370 is relatively readable). */
8371 if (opts.addressprint)
8372 uiout->field_skip ("addr");
8373 annotate_field (5);
8374 uiout->text ("exec");
8375 if (c->exec_pathname != NULL)
8376 {
8377 uiout->text (", program \"");
8378 uiout->field_string ("what", c->exec_pathname);
8379 uiout->text ("\" ");
8380 }
8381
8382 if (uiout->is_mi_like_p ())
8383 uiout->field_string ("catch-type", "exec");
8384 }
8385
8386 static void
8387 print_mention_catch_exec (struct breakpoint *b)
8388 {
8389 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8390 }
8391
8392 /* Implement the "print_recreate" breakpoint_ops method for exec
8393 catchpoints. */
8394
8395 static void
8396 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8397 {
8398 fprintf_unfiltered (fp, "catch exec");
8399 print_recreate_thread (b, fp);
8400 }
8401
8402 static struct breakpoint_ops catch_exec_breakpoint_ops;
8403
8404 static int
8405 hw_breakpoint_used_count (void)
8406 {
8407 int i = 0;
8408 struct breakpoint *b;
8409 struct bp_location *bl;
8410
8411 ALL_BREAKPOINTS (b)
8412 {
8413 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8414 for (bl = b->loc; bl; bl = bl->next)
8415 {
8416 /* Special types of hardware breakpoints may use more than
8417 one register. */
8418 i += b->ops->resources_needed (bl);
8419 }
8420 }
8421
8422 return i;
8423 }
8424
8425 /* Returns the resources B would use if it were a hardware
8426 watchpoint. */
8427
8428 static int
8429 hw_watchpoint_use_count (struct breakpoint *b)
8430 {
8431 int i = 0;
8432 struct bp_location *bl;
8433
8434 if (!breakpoint_enabled (b))
8435 return 0;
8436
8437 for (bl = b->loc; bl; bl = bl->next)
8438 {
8439 /* Special types of hardware watchpoints may use more than
8440 one register. */
8441 i += b->ops->resources_needed (bl);
8442 }
8443
8444 return i;
8445 }
8446
8447 /* Returns the sum the used resources of all hardware watchpoints of
8448 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8449 the sum of the used resources of all hardware watchpoints of other
8450 types _not_ TYPE. */
8451
8452 static int
8453 hw_watchpoint_used_count_others (struct breakpoint *except,
8454 enum bptype type, int *other_type_used)
8455 {
8456 int i = 0;
8457 struct breakpoint *b;
8458
8459 *other_type_used = 0;
8460 ALL_BREAKPOINTS (b)
8461 {
8462 if (b == except)
8463 continue;
8464 if (!breakpoint_enabled (b))
8465 continue;
8466
8467 if (b->type == type)
8468 i += hw_watchpoint_use_count (b);
8469 else if (is_hardware_watchpoint (b))
8470 *other_type_used = 1;
8471 }
8472
8473 return i;
8474 }
8475
8476 void
8477 disable_watchpoints_before_interactive_call_start (void)
8478 {
8479 struct breakpoint *b;
8480
8481 ALL_BREAKPOINTS (b)
8482 {
8483 if (is_watchpoint (b) && breakpoint_enabled (b))
8484 {
8485 b->enable_state = bp_call_disabled;
8486 update_global_location_list (UGLL_DONT_INSERT);
8487 }
8488 }
8489 }
8490
8491 void
8492 enable_watchpoints_after_interactive_call_stop (void)
8493 {
8494 struct breakpoint *b;
8495
8496 ALL_BREAKPOINTS (b)
8497 {
8498 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8499 {
8500 b->enable_state = bp_enabled;
8501 update_global_location_list (UGLL_MAY_INSERT);
8502 }
8503 }
8504 }
8505
8506 void
8507 disable_breakpoints_before_startup (void)
8508 {
8509 current_program_space->executing_startup = 1;
8510 update_global_location_list (UGLL_DONT_INSERT);
8511 }
8512
8513 void
8514 enable_breakpoints_after_startup (void)
8515 {
8516 current_program_space->executing_startup = 0;
8517 breakpoint_re_set ();
8518 }
8519
8520 /* Create a new single-step breakpoint for thread THREAD, with no
8521 locations. */
8522
8523 static struct breakpoint *
8524 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8525 {
8526 std::unique_ptr<breakpoint> b (new breakpoint ());
8527
8528 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
8529 &momentary_breakpoint_ops);
8530
8531 b->disposition = disp_donttouch;
8532 b->frame_id = null_frame_id;
8533
8534 b->thread = thread;
8535 gdb_assert (b->thread != 0);
8536
8537 return add_to_breakpoint_chain (std::move (b));
8538 }
8539
8540 /* Set a momentary breakpoint of type TYPE at address specified by
8541 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8542 frame. */
8543
8544 breakpoint_up
8545 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8546 struct frame_id frame_id, enum bptype type)
8547 {
8548 struct breakpoint *b;
8549
8550 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8551 tail-called one. */
8552 gdb_assert (!frame_id_artificial_p (frame_id));
8553
8554 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8555 b->enable_state = bp_enabled;
8556 b->disposition = disp_donttouch;
8557 b->frame_id = frame_id;
8558
8559 b->thread = inferior_thread ()->global_num;
8560
8561 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8562
8563 return breakpoint_up (b);
8564 }
8565
8566 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8567 The new breakpoint will have type TYPE, use OPS as its
8568 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8569
8570 static struct breakpoint *
8571 momentary_breakpoint_from_master (struct breakpoint *orig,
8572 enum bptype type,
8573 const struct breakpoint_ops *ops,
8574 int loc_enabled)
8575 {
8576 struct breakpoint *copy;
8577
8578 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8579 copy->loc = allocate_bp_location (copy);
8580 set_breakpoint_location_function (copy->loc);
8581
8582 copy->loc->gdbarch = orig->loc->gdbarch;
8583 copy->loc->requested_address = orig->loc->requested_address;
8584 copy->loc->address = orig->loc->address;
8585 copy->loc->section = orig->loc->section;
8586 copy->loc->pspace = orig->loc->pspace;
8587 copy->loc->probe = orig->loc->probe;
8588 copy->loc->line_number = orig->loc->line_number;
8589 copy->loc->symtab = orig->loc->symtab;
8590 copy->loc->enabled = loc_enabled;
8591 copy->frame_id = orig->frame_id;
8592 copy->thread = orig->thread;
8593 copy->pspace = orig->pspace;
8594
8595 copy->enable_state = bp_enabled;
8596 copy->disposition = disp_donttouch;
8597 copy->number = internal_breakpoint_number--;
8598
8599 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8600 return copy;
8601 }
8602
8603 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8604 ORIG is NULL. */
8605
8606 struct breakpoint *
8607 clone_momentary_breakpoint (struct breakpoint *orig)
8608 {
8609 /* If there's nothing to clone, then return nothing. */
8610 if (orig == NULL)
8611 return NULL;
8612
8613 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8614 }
8615
8616 breakpoint_up
8617 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8618 enum bptype type)
8619 {
8620 struct symtab_and_line sal;
8621
8622 sal = find_pc_line (pc, 0);
8623 sal.pc = pc;
8624 sal.section = find_pc_overlay (pc);
8625 sal.explicit_pc = 1;
8626
8627 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8628 }
8629 \f
8630
8631 /* Tell the user we have just set a breakpoint B. */
8632
8633 static void
8634 mention (struct breakpoint *b)
8635 {
8636 b->ops->print_mention (b);
8637 current_uiout->text ("\n");
8638 }
8639 \f
8640
8641 static bool bp_loc_is_permanent (struct bp_location *loc);
8642
8643 /* Handle "set breakpoint auto-hw on".
8644
8645 If the explicitly specified breakpoint type is not hardware
8646 breakpoint, check the memory map to see whether the breakpoint
8647 address is in read-only memory.
8648
8649 - location type is not hardware breakpoint, memory is read-only.
8650 We change the type of the location to hardware breakpoint.
8651
8652 - location type is hardware breakpoint, memory is read-write. This
8653 means we've previously made the location hardware one, but then the
8654 memory map changed, so we undo.
8655 */
8656
8657 static void
8658 handle_automatic_hardware_breakpoints (bp_location *bl)
8659 {
8660 if (automatic_hardware_breakpoints
8661 && bl->owner->type != bp_hardware_breakpoint
8662 && (bl->loc_type == bp_loc_software_breakpoint
8663 || bl->loc_type == bp_loc_hardware_breakpoint))
8664 {
8665 /* When breakpoints are removed, remove_breakpoints will use
8666 location types we've just set here, the only possible problem
8667 is that memory map has changed during running program, but
8668 it's not going to work anyway with current gdb. */
8669 mem_region *mr = lookup_mem_region (bl->address);
8670
8671 if (mr != nullptr)
8672 {
8673 enum bp_loc_type new_type;
8674
8675 if (mr->attrib.mode != MEM_RW)
8676 new_type = bp_loc_hardware_breakpoint;
8677 else
8678 new_type = bp_loc_software_breakpoint;
8679
8680 if (new_type != bl->loc_type)
8681 {
8682 static bool said = false;
8683
8684 bl->loc_type = new_type;
8685 if (!said)
8686 {
8687 fprintf_filtered (gdb_stdout,
8688 _("Note: automatically using "
8689 "hardware breakpoints for "
8690 "read-only addresses.\n"));
8691 said = true;
8692 }
8693 }
8694 }
8695 }
8696 }
8697
8698 static struct bp_location *
8699 add_location_to_breakpoint (struct breakpoint *b,
8700 const struct symtab_and_line *sal)
8701 {
8702 struct bp_location *loc, **tmp;
8703 CORE_ADDR adjusted_address;
8704 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8705
8706 if (loc_gdbarch == NULL)
8707 loc_gdbarch = b->gdbarch;
8708
8709 /* Adjust the breakpoint's address prior to allocating a location.
8710 Once we call allocate_bp_location(), that mostly uninitialized
8711 location will be placed on the location chain. Adjustment of the
8712 breakpoint may cause target_read_memory() to be called and we do
8713 not want its scan of the location chain to find a breakpoint and
8714 location that's only been partially initialized. */
8715 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8716 sal->pc, b->type);
8717
8718 /* Sort the locations by their ADDRESS. */
8719 loc = allocate_bp_location (b);
8720 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8721 tmp = &((*tmp)->next))
8722 ;
8723 loc->next = *tmp;
8724 *tmp = loc;
8725
8726 loc->requested_address = sal->pc;
8727 loc->address = adjusted_address;
8728 loc->pspace = sal->pspace;
8729 loc->probe.prob = sal->prob;
8730 loc->probe.objfile = sal->objfile;
8731 gdb_assert (loc->pspace != NULL);
8732 loc->section = sal->section;
8733 loc->gdbarch = loc_gdbarch;
8734 loc->line_number = sal->line;
8735 loc->symtab = sal->symtab;
8736 loc->symbol = sal->symbol;
8737 loc->msymbol = sal->msymbol;
8738 loc->objfile = sal->objfile;
8739
8740 set_breakpoint_location_function (loc);
8741
8742 /* While by definition, permanent breakpoints are already present in the
8743 code, we don't mark the location as inserted. Normally one would expect
8744 that GDB could rely on that breakpoint instruction to stop the program,
8745 thus removing the need to insert its own breakpoint, except that executing
8746 the breakpoint instruction can kill the target instead of reporting a
8747 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8748 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8749 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8750 breakpoint be inserted normally results in QEMU knowing about the GDB
8751 breakpoint, and thus trap before the breakpoint instruction is executed.
8752 (If GDB later needs to continue execution past the permanent breakpoint,
8753 it manually increments the PC, thus avoiding executing the breakpoint
8754 instruction.) */
8755 if (bp_loc_is_permanent (loc))
8756 loc->permanent = 1;
8757
8758 return loc;
8759 }
8760 \f
8761
8762 /* Return true if LOC is pointing to a permanent breakpoint,
8763 return false otherwise. */
8764
8765 static bool
8766 bp_loc_is_permanent (struct bp_location *loc)
8767 {
8768 gdb_assert (loc != NULL);
8769
8770 /* If we have a non-breakpoint-backed catchpoint or a software
8771 watchpoint, just return 0. We should not attempt to read from
8772 the addresses the locations of these breakpoint types point to.
8773 gdbarch_program_breakpoint_here_p, below, will attempt to read
8774 memory. */
8775 if (!bl_address_is_meaningful (loc))
8776 return false;
8777
8778 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8779 switch_to_program_space_and_thread (loc->pspace);
8780 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
8781 }
8782
8783 /* Build a command list for the dprintf corresponding to the current
8784 settings of the dprintf style options. */
8785
8786 static void
8787 update_dprintf_command_list (struct breakpoint *b)
8788 {
8789 char *dprintf_args = b->extra_string;
8790 char *printf_line = NULL;
8791
8792 if (!dprintf_args)
8793 return;
8794
8795 dprintf_args = skip_spaces (dprintf_args);
8796
8797 /* Allow a comma, as it may have terminated a location, but don't
8798 insist on it. */
8799 if (*dprintf_args == ',')
8800 ++dprintf_args;
8801 dprintf_args = skip_spaces (dprintf_args);
8802
8803 if (*dprintf_args != '"')
8804 error (_("Bad format string, missing '\"'."));
8805
8806 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8807 printf_line = xstrprintf ("printf %s", dprintf_args);
8808 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8809 {
8810 if (!dprintf_function)
8811 error (_("No function supplied for dprintf call"));
8812
8813 if (dprintf_channel && strlen (dprintf_channel) > 0)
8814 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8815 dprintf_function,
8816 dprintf_channel,
8817 dprintf_args);
8818 else
8819 printf_line = xstrprintf ("call (void) %s (%s)",
8820 dprintf_function,
8821 dprintf_args);
8822 }
8823 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8824 {
8825 if (target_can_run_breakpoint_commands ())
8826 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8827 else
8828 {
8829 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8830 printf_line = xstrprintf ("printf %s", dprintf_args);
8831 }
8832 }
8833 else
8834 internal_error (__FILE__, __LINE__,
8835 _("Invalid dprintf style."));
8836
8837 gdb_assert (printf_line != NULL);
8838
8839 /* Manufacture a printf sequence. */
8840 struct command_line *printf_cmd_line
8841 = new struct command_line (simple_control, printf_line);
8842 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8843 command_lines_deleter ()));
8844 }
8845
8846 /* Update all dprintf commands, making their command lists reflect
8847 current style settings. */
8848
8849 static void
8850 update_dprintf_commands (const char *args, int from_tty,
8851 struct cmd_list_element *c)
8852 {
8853 struct breakpoint *b;
8854
8855 ALL_BREAKPOINTS (b)
8856 {
8857 if (b->type == bp_dprintf)
8858 update_dprintf_command_list (b);
8859 }
8860 }
8861
8862 /* Create a breakpoint with SAL as location. Use LOCATION
8863 as a description of the location, and COND_STRING
8864 as condition expression. If LOCATION is NULL then create an
8865 "address location" from the address in the SAL. */
8866
8867 static void
8868 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8869 gdb::array_view<const symtab_and_line> sals,
8870 event_location_up &&location,
8871 gdb::unique_xmalloc_ptr<char> filter,
8872 gdb::unique_xmalloc_ptr<char> cond_string,
8873 gdb::unique_xmalloc_ptr<char> extra_string,
8874 enum bptype type, enum bpdisp disposition,
8875 int thread, int task, int ignore_count,
8876 const struct breakpoint_ops *ops, int from_tty,
8877 int enabled, int internal, unsigned flags,
8878 int display_canonical)
8879 {
8880 int i;
8881
8882 if (type == bp_hardware_breakpoint)
8883 {
8884 int target_resources_ok;
8885
8886 i = hw_breakpoint_used_count ();
8887 target_resources_ok =
8888 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8889 i + 1, 0);
8890 if (target_resources_ok == 0)
8891 error (_("No hardware breakpoint support in the target."));
8892 else if (target_resources_ok < 0)
8893 error (_("Hardware breakpoints used exceeds limit."));
8894 }
8895
8896 gdb_assert (!sals.empty ());
8897
8898 for (const auto &sal : sals)
8899 {
8900 struct bp_location *loc;
8901
8902 if (from_tty)
8903 {
8904 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8905 if (!loc_gdbarch)
8906 loc_gdbarch = gdbarch;
8907
8908 describe_other_breakpoints (loc_gdbarch,
8909 sal.pspace, sal.pc, sal.section, thread);
8910 }
8911
8912 if (&sal == &sals[0])
8913 {
8914 init_raw_breakpoint (b, gdbarch, sal, type, ops);
8915 b->thread = thread;
8916 b->task = task;
8917
8918 b->cond_string = cond_string.release ();
8919 b->extra_string = extra_string.release ();
8920 b->ignore_count = ignore_count;
8921 b->enable_state = enabled ? bp_enabled : bp_disabled;
8922 b->disposition = disposition;
8923
8924 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8925 b->loc->inserted = 1;
8926
8927 if (type == bp_static_tracepoint)
8928 {
8929 struct tracepoint *t = (struct tracepoint *) b;
8930 struct static_tracepoint_marker marker;
8931
8932 if (strace_marker_p (b))
8933 {
8934 /* We already know the marker exists, otherwise, we
8935 wouldn't see a sal for it. */
8936 const char *p
8937 = &event_location_to_string (b->location.get ())[3];
8938 const char *endp;
8939
8940 p = skip_spaces (p);
8941
8942 endp = skip_to_space (p);
8943
8944 t->static_trace_marker_id.assign (p, endp - p);
8945
8946 printf_filtered (_("Probed static tracepoint "
8947 "marker \"%s\"\n"),
8948 t->static_trace_marker_id.c_str ());
8949 }
8950 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8951 {
8952 t->static_trace_marker_id = std::move (marker.str_id);
8953
8954 printf_filtered (_("Probed static tracepoint "
8955 "marker \"%s\"\n"),
8956 t->static_trace_marker_id.c_str ());
8957 }
8958 else
8959 warning (_("Couldn't determine the static "
8960 "tracepoint marker to probe"));
8961 }
8962
8963 loc = b->loc;
8964 }
8965 else
8966 {
8967 loc = add_location_to_breakpoint (b, &sal);
8968 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8969 loc->inserted = 1;
8970 }
8971
8972 /* Do not set breakpoint locations conditions yet. As locations
8973 are inserted, they get sorted based on their addresses. Let
8974 the list stabilize to have reliable location numbers. */
8975
8976 /* Dynamic printf requires and uses additional arguments on the
8977 command line, otherwise it's an error. */
8978 if (type == bp_dprintf)
8979 {
8980 if (b->extra_string)
8981 update_dprintf_command_list (b);
8982 else
8983 error (_("Format string required"));
8984 }
8985 else if (b->extra_string)
8986 error (_("Garbage '%s' at end of command"), b->extra_string);
8987 }
8988
8989
8990 /* The order of the locations is now stable. Set the location
8991 condition using the location's number. */
8992 int loc_num = 1;
8993 for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)
8994 {
8995 if (b->cond_string != nullptr)
8996 set_breakpoint_location_condition (b->cond_string, loc, b->number,
8997 loc_num);
8998
8999 ++loc_num;
9000 }
9001
9002 b->display_canonical = display_canonical;
9003 if (location != NULL)
9004 b->location = std::move (location);
9005 else
9006 b->location = new_address_location (b->loc->address, NULL, 0);
9007 b->filter = std::move (filter);
9008 }
9009
9010 static void
9011 create_breakpoint_sal (struct gdbarch *gdbarch,
9012 gdb::array_view<const symtab_and_line> sals,
9013 event_location_up &&location,
9014 gdb::unique_xmalloc_ptr<char> filter,
9015 gdb::unique_xmalloc_ptr<char> cond_string,
9016 gdb::unique_xmalloc_ptr<char> extra_string,
9017 enum bptype type, enum bpdisp disposition,
9018 int thread, int task, int ignore_count,
9019 const struct breakpoint_ops *ops, int from_tty,
9020 int enabled, int internal, unsigned flags,
9021 int display_canonical)
9022 {
9023 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
9024
9025 init_breakpoint_sal (b.get (), gdbarch,
9026 sals, std::move (location),
9027 std::move (filter),
9028 std::move (cond_string),
9029 std::move (extra_string),
9030 type, disposition,
9031 thread, task, ignore_count,
9032 ops, from_tty,
9033 enabled, internal, flags,
9034 display_canonical);
9035
9036 install_breakpoint (internal, std::move (b), 0);
9037 }
9038
9039 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9040 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9041 value. COND_STRING, if not NULL, specified the condition to be
9042 used for all breakpoints. Essentially the only case where
9043 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9044 function. In that case, it's still not possible to specify
9045 separate conditions for different overloaded functions, so
9046 we take just a single condition string.
9047
9048 NOTE: If the function succeeds, the caller is expected to cleanup
9049 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9050 array contents). If the function fails (error() is called), the
9051 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9052 COND and SALS arrays and each of those arrays contents. */
9053
9054 static void
9055 create_breakpoints_sal (struct gdbarch *gdbarch,
9056 struct linespec_result *canonical,
9057 gdb::unique_xmalloc_ptr<char> cond_string,
9058 gdb::unique_xmalloc_ptr<char> extra_string,
9059 enum bptype type, enum bpdisp disposition,
9060 int thread, int task, int ignore_count,
9061 const struct breakpoint_ops *ops, int from_tty,
9062 int enabled, int internal, unsigned flags)
9063 {
9064 if (canonical->pre_expanded)
9065 gdb_assert (canonical->lsals.size () == 1);
9066
9067 for (const auto &lsal : canonical->lsals)
9068 {
9069 /* Note that 'location' can be NULL in the case of a plain
9070 'break', without arguments. */
9071 event_location_up location
9072 = (canonical->location != NULL
9073 ? copy_event_location (canonical->location.get ()) : NULL);
9074 gdb::unique_xmalloc_ptr<char> filter_string
9075 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
9076
9077 create_breakpoint_sal (gdbarch, lsal.sals,
9078 std::move (location),
9079 std::move (filter_string),
9080 std::move (cond_string),
9081 std::move (extra_string),
9082 type, disposition,
9083 thread, task, ignore_count, ops,
9084 from_tty, enabled, internal, flags,
9085 canonical->special_display);
9086 }
9087 }
9088
9089 /* Parse LOCATION which is assumed to be a SAL specification possibly
9090 followed by conditionals. On return, SALS contains an array of SAL
9091 addresses found. LOCATION points to the end of the SAL (for
9092 linespec locations).
9093
9094 The array and the line spec strings are allocated on the heap, it is
9095 the caller's responsibility to free them. */
9096
9097 static void
9098 parse_breakpoint_sals (struct event_location *location,
9099 struct linespec_result *canonical)
9100 {
9101 struct symtab_and_line cursal;
9102
9103 if (event_location_type (location) == LINESPEC_LOCATION)
9104 {
9105 const char *spec = get_linespec_location (location)->spec_string;
9106
9107 if (spec == NULL)
9108 {
9109 /* The last displayed codepoint, if it's valid, is our default
9110 breakpoint address. */
9111 if (last_displayed_sal_is_valid ())
9112 {
9113 /* Set sal's pspace, pc, symtab, and line to the values
9114 corresponding to the last call to print_frame_info.
9115 Be sure to reinitialize LINE with NOTCURRENT == 0
9116 as the breakpoint line number is inappropriate otherwise.
9117 find_pc_line would adjust PC, re-set it back. */
9118 symtab_and_line sal = get_last_displayed_sal ();
9119 CORE_ADDR pc = sal.pc;
9120
9121 sal = find_pc_line (pc, 0);
9122
9123 /* "break" without arguments is equivalent to "break *PC"
9124 where PC is the last displayed codepoint's address. So
9125 make sure to set sal.explicit_pc to prevent GDB from
9126 trying to expand the list of sals to include all other
9127 instances with the same symtab and line. */
9128 sal.pc = pc;
9129 sal.explicit_pc = 1;
9130
9131 struct linespec_sals lsal;
9132 lsal.sals = {sal};
9133 lsal.canonical = NULL;
9134
9135 canonical->lsals.push_back (std::move (lsal));
9136 return;
9137 }
9138 else
9139 error (_("No default breakpoint address now."));
9140 }
9141 }
9142
9143 /* Force almost all breakpoints to be in terms of the
9144 current_source_symtab (which is decode_line_1's default).
9145 This should produce the results we want almost all of the
9146 time while leaving default_breakpoint_* alone.
9147
9148 ObjC: However, don't match an Objective-C method name which
9149 may have a '+' or '-' succeeded by a '['. */
9150 cursal = get_current_source_symtab_and_line ();
9151 if (last_displayed_sal_is_valid ())
9152 {
9153 const char *spec = NULL;
9154
9155 if (event_location_type (location) == LINESPEC_LOCATION)
9156 spec = get_linespec_location (location)->spec_string;
9157
9158 if (!cursal.symtab
9159 || (spec != NULL
9160 && strchr ("+-", spec[0]) != NULL
9161 && spec[1] != '['))
9162 {
9163 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9164 get_last_displayed_symtab (),
9165 get_last_displayed_line (),
9166 canonical, NULL, NULL);
9167 return;
9168 }
9169 }
9170
9171 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9172 cursal.symtab, cursal.line, canonical, NULL, NULL);
9173 }
9174
9175
9176 /* Convert each SAL into a real PC. Verify that the PC can be
9177 inserted as a breakpoint. If it can't throw an error. */
9178
9179 static void
9180 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
9181 {
9182 for (auto &sal : sals)
9183 resolve_sal_pc (&sal);
9184 }
9185
9186 /* Fast tracepoints may have restrictions on valid locations. For
9187 instance, a fast tracepoint using a jump instead of a trap will
9188 likely have to overwrite more bytes than a trap would, and so can
9189 only be placed where the instruction is longer than the jump, or a
9190 multi-instruction sequence does not have a jump into the middle of
9191 it, etc. */
9192
9193 static void
9194 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9195 gdb::array_view<const symtab_and_line> sals)
9196 {
9197 for (const auto &sal : sals)
9198 {
9199 struct gdbarch *sarch;
9200
9201 sarch = get_sal_arch (sal);
9202 /* We fall back to GDBARCH if there is no architecture
9203 associated with SAL. */
9204 if (sarch == NULL)
9205 sarch = gdbarch;
9206 std::string msg;
9207 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
9208 error (_("May not have a fast tracepoint at %s%s"),
9209 paddress (sarch, sal.pc), msg.c_str ());
9210 }
9211 }
9212
9213 /* Given TOK, a string specification of condition and thread, as
9214 accepted by the 'break' command, extract the condition
9215 string and thread number and set *COND_STRING and *THREAD.
9216 PC identifies the context at which the condition should be parsed.
9217 If no condition is found, *COND_STRING is set to NULL.
9218 If no thread is found, *THREAD is set to -1. */
9219
9220 static void
9221 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9222 char **cond_string, int *thread, int *task,
9223 char **rest)
9224 {
9225 *cond_string = NULL;
9226 *thread = -1;
9227 *task = 0;
9228 *rest = NULL;
9229 bool force = false;
9230
9231 while (tok && *tok)
9232 {
9233 const char *end_tok;
9234 int toklen;
9235 const char *cond_start = NULL;
9236 const char *cond_end = NULL;
9237
9238 tok = skip_spaces (tok);
9239
9240 if ((*tok == '"' || *tok == ',') && rest)
9241 {
9242 *rest = savestring (tok, strlen (tok));
9243 return;
9244 }
9245
9246 end_tok = skip_to_space (tok);
9247
9248 toklen = end_tok - tok;
9249
9250 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9251 {
9252 tok = cond_start = end_tok + 1;
9253 try
9254 {
9255 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9256 }
9257 catch (const gdb_exception_error &)
9258 {
9259 if (!force)
9260 throw;
9261 else
9262 tok = tok + strlen (tok);
9263 }
9264 cond_end = tok;
9265 *cond_string = savestring (cond_start, cond_end - cond_start);
9266 }
9267 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
9268 {
9269 tok = tok + toklen;
9270 force = true;
9271 }
9272 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9273 {
9274 const char *tmptok;
9275 struct thread_info *thr;
9276
9277 tok = end_tok + 1;
9278 thr = parse_thread_id (tok, &tmptok);
9279 if (tok == tmptok)
9280 error (_("Junk after thread keyword."));
9281 *thread = thr->global_num;
9282 tok = tmptok;
9283 }
9284 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9285 {
9286 char *tmptok;
9287
9288 tok = end_tok + 1;
9289 *task = strtol (tok, &tmptok, 0);
9290 if (tok == tmptok)
9291 error (_("Junk after task keyword."));
9292 if (!valid_task_id (*task))
9293 error (_("Unknown task %d."), *task);
9294 tok = tmptok;
9295 }
9296 else if (rest)
9297 {
9298 *rest = savestring (tok, strlen (tok));
9299 return;
9300 }
9301 else
9302 error (_("Junk at end of arguments."));
9303 }
9304 }
9305
9306 /* Call 'find_condition_and_thread' for each sal in SALS until a parse
9307 succeeds. The parsed values are written to COND_STRING, THREAD,
9308 TASK, and REST. See the comment of 'find_condition_and_thread'
9309 for the description of these parameters and INPUT. */
9310
9311 static void
9312 find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
9313 const char *input, char **cond_string,
9314 int *thread, int *task, char **rest)
9315 {
9316 int num_failures = 0;
9317 for (auto &sal : sals)
9318 {
9319 char *cond = nullptr;
9320 int thread_id = 0;
9321 int task_id = 0;
9322 char *remaining = nullptr;
9323
9324 /* Here we want to parse 'arg' to separate condition from thread
9325 number. But because parsing happens in a context and the
9326 contexts of sals might be different, try each until there is
9327 success. Finding one successful parse is sufficient for our
9328 goal. When setting the breakpoint we'll re-parse the
9329 condition in the context of each sal. */
9330 try
9331 {
9332 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
9333 &task_id, &remaining);
9334 *cond_string = cond;
9335 *thread = thread_id;
9336 *task = task_id;
9337 *rest = remaining;
9338 break;
9339 }
9340 catch (const gdb_exception_error &e)
9341 {
9342 num_failures++;
9343 /* If no sal remains, do not continue. */
9344 if (num_failures == sals.size ())
9345 throw;
9346 }
9347 }
9348 }
9349
9350 /* Decode a static tracepoint marker spec. */
9351
9352 static std::vector<symtab_and_line>
9353 decode_static_tracepoint_spec (const char **arg_p)
9354 {
9355 const char *p = &(*arg_p)[3];
9356 const char *endp;
9357
9358 p = skip_spaces (p);
9359
9360 endp = skip_to_space (p);
9361
9362 std::string marker_str (p, endp - p);
9363
9364 std::vector<static_tracepoint_marker> markers
9365 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9366 if (markers.empty ())
9367 error (_("No known static tracepoint marker named %s"),
9368 marker_str.c_str ());
9369
9370 std::vector<symtab_and_line> sals;
9371 sals.reserve (markers.size ());
9372
9373 for (const static_tracepoint_marker &marker : markers)
9374 {
9375 symtab_and_line sal = find_pc_line (marker.address, 0);
9376 sal.pc = marker.address;
9377 sals.push_back (sal);
9378 }
9379
9380 *arg_p = endp;
9381 return sals;
9382 }
9383
9384 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9385 according to IS_TRACEPOINT. */
9386
9387 static const struct breakpoint_ops *
9388 breakpoint_ops_for_event_location_type (enum event_location_type location_type,
9389 bool is_tracepoint)
9390 {
9391 if (is_tracepoint)
9392 {
9393 if (location_type == PROBE_LOCATION)
9394 return &tracepoint_probe_breakpoint_ops;
9395 else
9396 return &tracepoint_breakpoint_ops;
9397 }
9398 else
9399 {
9400 if (location_type == PROBE_LOCATION)
9401 return &bkpt_probe_breakpoint_ops;
9402 else
9403 return &bkpt_breakpoint_ops;
9404 }
9405 }
9406
9407 /* See breakpoint.h. */
9408
9409 const struct breakpoint_ops *
9410 breakpoint_ops_for_event_location (const struct event_location *location,
9411 bool is_tracepoint)
9412 {
9413 if (location != nullptr)
9414 return breakpoint_ops_for_event_location_type
9415 (event_location_type (location), is_tracepoint);
9416 return is_tracepoint ? &tracepoint_breakpoint_ops : &bkpt_breakpoint_ops;
9417 }
9418
9419 /* See breakpoint.h. */
9420
9421 int
9422 create_breakpoint (struct gdbarch *gdbarch,
9423 struct event_location *location,
9424 const char *cond_string,
9425 int thread, const char *extra_string,
9426 int parse_extra,
9427 int tempflag, enum bptype type_wanted,
9428 int ignore_count,
9429 enum auto_boolean pending_break_support,
9430 const struct breakpoint_ops *ops,
9431 int from_tty, int enabled, int internal,
9432 unsigned flags)
9433 {
9434 struct linespec_result canonical;
9435 int pending = 0;
9436 int task = 0;
9437 int prev_bkpt_count = breakpoint_count;
9438
9439 gdb_assert (ops != NULL);
9440
9441 /* If extra_string isn't useful, set it to NULL. */
9442 if (extra_string != NULL && *extra_string == '\0')
9443 extra_string = NULL;
9444
9445 try
9446 {
9447 ops->create_sals_from_location (location, &canonical, type_wanted);
9448 }
9449 catch (const gdb_exception_error &e)
9450 {
9451 /* If caller is interested in rc value from parse, set
9452 value. */
9453 if (e.error == NOT_FOUND_ERROR)
9454 {
9455 /* If pending breakpoint support is turned off, throw
9456 error. */
9457
9458 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9459 throw;
9460
9461 exception_print (gdb_stderr, e);
9462
9463 /* If pending breakpoint support is auto query and the user
9464 selects no, then simply return the error code. */
9465 if (pending_break_support == AUTO_BOOLEAN_AUTO
9466 && !nquery (_("Make %s pending on future shared library load? "),
9467 bptype_string (type_wanted)))
9468 return 0;
9469
9470 /* At this point, either the user was queried about setting
9471 a pending breakpoint and selected yes, or pending
9472 breakpoint behavior is on and thus a pending breakpoint
9473 is defaulted on behalf of the user. */
9474 pending = 1;
9475 }
9476 else
9477 throw;
9478 }
9479
9480 if (!pending && canonical.lsals.empty ())
9481 return 0;
9482
9483 /* Resolve all line numbers to PC's and verify that the addresses
9484 are ok for the target. */
9485 if (!pending)
9486 {
9487 for (auto &lsal : canonical.lsals)
9488 breakpoint_sals_to_pc (lsal.sals);
9489 }
9490
9491 /* Fast tracepoints may have additional restrictions on location. */
9492 if (!pending && type_wanted == bp_fast_tracepoint)
9493 {
9494 for (const auto &lsal : canonical.lsals)
9495 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9496 }
9497
9498 /* Verify that condition can be parsed, before setting any
9499 breakpoints. Allocate a separate condition expression for each
9500 breakpoint. */
9501 if (!pending)
9502 {
9503 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9504 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9505
9506 if (parse_extra)
9507 {
9508 char *rest;
9509 char *cond;
9510
9511 const linespec_sals &lsal = canonical.lsals[0];
9512
9513 find_condition_and_thread_for_sals (lsal.sals, extra_string,
9514 &cond, &thread, &task, &rest);
9515 cond_string_copy.reset (cond);
9516 extra_string_copy.reset (rest);
9517 }
9518 else
9519 {
9520 if (type_wanted != bp_dprintf
9521 && extra_string != NULL && *extra_string != '\0')
9522 error (_("Garbage '%s' at end of location"), extra_string);
9523
9524 /* Create a private copy of condition string. */
9525 if (cond_string)
9526 cond_string_copy.reset (xstrdup (cond_string));
9527 /* Create a private copy of any extra string. */
9528 if (extra_string)
9529 extra_string_copy.reset (xstrdup (extra_string));
9530 }
9531
9532 ops->create_breakpoints_sal (gdbarch, &canonical,
9533 std::move (cond_string_copy),
9534 std::move (extra_string_copy),
9535 type_wanted,
9536 tempflag ? disp_del : disp_donttouch,
9537 thread, task, ignore_count, ops,
9538 from_tty, enabled, internal, flags);
9539 }
9540 else
9541 {
9542 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9543
9544 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9545 b->location = copy_event_location (location);
9546
9547 if (parse_extra)
9548 b->cond_string = NULL;
9549 else
9550 {
9551 /* Create a private copy of condition string. */
9552 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
9553 b->thread = thread;
9554 }
9555
9556 /* Create a private copy of any extra string. */
9557 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
9558 b->ignore_count = ignore_count;
9559 b->disposition = tempflag ? disp_del : disp_donttouch;
9560 b->condition_not_parsed = 1;
9561 b->enable_state = enabled ? bp_enabled : bp_disabled;
9562 if ((type_wanted != bp_breakpoint
9563 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9564 b->pspace = current_program_space;
9565
9566 install_breakpoint (internal, std::move (b), 0);
9567 }
9568
9569 if (canonical.lsals.size () > 1)
9570 {
9571 warning (_("Multiple breakpoints were set.\nUse the "
9572 "\"delete\" command to delete unwanted breakpoints."));
9573 prev_breakpoint_count = prev_bkpt_count;
9574 }
9575
9576 update_global_location_list (UGLL_MAY_INSERT);
9577
9578 return 1;
9579 }
9580
9581 /* Set a breakpoint.
9582 ARG is a string describing breakpoint address,
9583 condition, and thread.
9584 FLAG specifies if a breakpoint is hardware on,
9585 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9586 and BP_TEMPFLAG. */
9587
9588 static void
9589 break_command_1 (const char *arg, int flag, int from_tty)
9590 {
9591 int tempflag = flag & BP_TEMPFLAG;
9592 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9593 ? bp_hardware_breakpoint
9594 : bp_breakpoint);
9595
9596 event_location_up location = string_to_event_location (&arg, current_language);
9597 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9598 (location.get (), false /* is_tracepoint */);
9599
9600 create_breakpoint (get_current_arch (),
9601 location.get (),
9602 NULL, 0, arg, 1 /* parse arg */,
9603 tempflag, type_wanted,
9604 0 /* Ignore count */,
9605 pending_break_support,
9606 ops,
9607 from_tty,
9608 1 /* enabled */,
9609 0 /* internal */,
9610 0);
9611 }
9612
9613 /* Helper function for break_command_1 and disassemble_command. */
9614
9615 void
9616 resolve_sal_pc (struct symtab_and_line *sal)
9617 {
9618 CORE_ADDR pc;
9619
9620 if (sal->pc == 0 && sal->symtab != NULL)
9621 {
9622 if (!find_line_pc (sal->symtab, sal->line, &pc))
9623 error (_("No line %d in file \"%s\"."),
9624 sal->line, symtab_to_filename_for_display (sal->symtab));
9625 sal->pc = pc;
9626
9627 /* If this SAL corresponds to a breakpoint inserted using a line
9628 number, then skip the function prologue if necessary. */
9629 if (sal->explicit_line)
9630 skip_prologue_sal (sal);
9631 }
9632
9633 if (sal->section == 0 && sal->symtab != NULL)
9634 {
9635 const struct blockvector *bv;
9636 const struct block *b;
9637 struct symbol *sym;
9638
9639 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9640 SYMTAB_COMPUNIT (sal->symtab));
9641 if (bv != NULL)
9642 {
9643 sym = block_linkage_function (b);
9644 if (sym != NULL)
9645 {
9646 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9647 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9648 sym);
9649 }
9650 else
9651 {
9652 /* It really is worthwhile to have the section, so we'll
9653 just have to look harder. This case can be executed
9654 if we have line numbers but no functions (as can
9655 happen in assembly source). */
9656
9657 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9658 switch_to_program_space_and_thread (sal->pspace);
9659
9660 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9661 if (msym.minsym)
9662 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9663 }
9664 }
9665 }
9666 }
9667
9668 void
9669 break_command (const char *arg, int from_tty)
9670 {
9671 break_command_1 (arg, 0, from_tty);
9672 }
9673
9674 void
9675 tbreak_command (const char *arg, int from_tty)
9676 {
9677 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9678 }
9679
9680 static void
9681 hbreak_command (const char *arg, int from_tty)
9682 {
9683 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9684 }
9685
9686 static void
9687 thbreak_command (const char *arg, int from_tty)
9688 {
9689 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9690 }
9691
9692 static void
9693 stop_command (const char *arg, int from_tty)
9694 {
9695 printf_filtered (_("Specify the type of breakpoint to set.\n\
9696 Usage: stop in <function | address>\n\
9697 stop at <line>\n"));
9698 }
9699
9700 static void
9701 stopin_command (const char *arg, int from_tty)
9702 {
9703 int badInput = 0;
9704
9705 if (arg == NULL)
9706 badInput = 1;
9707 else if (*arg != '*')
9708 {
9709 const char *argptr = arg;
9710 int hasColon = 0;
9711
9712 /* Look for a ':'. If this is a line number specification, then
9713 say it is bad, otherwise, it should be an address or
9714 function/method name. */
9715 while (*argptr && !hasColon)
9716 {
9717 hasColon = (*argptr == ':');
9718 argptr++;
9719 }
9720
9721 if (hasColon)
9722 badInput = (*argptr != ':'); /* Not a class::method */
9723 else
9724 badInput = isdigit (*arg); /* a simple line number */
9725 }
9726
9727 if (badInput)
9728 printf_filtered (_("Usage: stop in <function | address>\n"));
9729 else
9730 break_command_1 (arg, 0, from_tty);
9731 }
9732
9733 static void
9734 stopat_command (const char *arg, int from_tty)
9735 {
9736 int badInput = 0;
9737
9738 if (arg == NULL || *arg == '*') /* no line number */
9739 badInput = 1;
9740 else
9741 {
9742 const char *argptr = arg;
9743 int hasColon = 0;
9744
9745 /* Look for a ':'. If there is a '::' then get out, otherwise
9746 it is probably a line number. */
9747 while (*argptr && !hasColon)
9748 {
9749 hasColon = (*argptr == ':');
9750 argptr++;
9751 }
9752
9753 if (hasColon)
9754 badInput = (*argptr == ':'); /* we have class::method */
9755 else
9756 badInput = !isdigit (*arg); /* not a line number */
9757 }
9758
9759 if (badInput)
9760 printf_filtered (_("Usage: stop at LINE\n"));
9761 else
9762 break_command_1 (arg, 0, from_tty);
9763 }
9764
9765 /* The dynamic printf command is mostly like a regular breakpoint, but
9766 with a prewired command list consisting of a single output command,
9767 built from extra arguments supplied on the dprintf command
9768 line. */
9769
9770 static void
9771 dprintf_command (const char *arg, int from_tty)
9772 {
9773 event_location_up location = string_to_event_location (&arg, current_language);
9774
9775 /* If non-NULL, ARG should have been advanced past the location;
9776 the next character must be ','. */
9777 if (arg != NULL)
9778 {
9779 if (arg[0] != ',' || arg[1] == '\0')
9780 error (_("Format string required"));
9781 else
9782 {
9783 /* Skip the comma. */
9784 ++arg;
9785 }
9786 }
9787
9788 create_breakpoint (get_current_arch (),
9789 location.get (),
9790 NULL, 0, arg, 1 /* parse arg */,
9791 0, bp_dprintf,
9792 0 /* Ignore count */,
9793 pending_break_support,
9794 &dprintf_breakpoint_ops,
9795 from_tty,
9796 1 /* enabled */,
9797 0 /* internal */,
9798 0);
9799 }
9800
9801 static void
9802 agent_printf_command (const char *arg, int from_tty)
9803 {
9804 error (_("May only run agent-printf on the target"));
9805 }
9806
9807 /* Implement the "breakpoint_hit" breakpoint_ops method for
9808 ranged breakpoints. */
9809
9810 static int
9811 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9812 const address_space *aspace,
9813 CORE_ADDR bp_addr,
9814 const struct target_waitstatus *ws)
9815 {
9816 if (ws->kind != TARGET_WAITKIND_STOPPED
9817 || ws->value.sig != GDB_SIGNAL_TRAP)
9818 return 0;
9819
9820 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9821 bl->length, aspace, bp_addr);
9822 }
9823
9824 /* Implement the "resources_needed" breakpoint_ops method for
9825 ranged breakpoints. */
9826
9827 static int
9828 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9829 {
9830 return target_ranged_break_num_registers ();
9831 }
9832
9833 /* Implement the "print_it" breakpoint_ops method for
9834 ranged breakpoints. */
9835
9836 static enum print_stop_action
9837 print_it_ranged_breakpoint (bpstat bs)
9838 {
9839 struct breakpoint *b = bs->breakpoint_at;
9840 struct bp_location *bl = b->loc;
9841 struct ui_out *uiout = current_uiout;
9842
9843 gdb_assert (b->type == bp_hardware_breakpoint);
9844
9845 /* Ranged breakpoints have only one location. */
9846 gdb_assert (bl && bl->next == NULL);
9847
9848 annotate_breakpoint (b->number);
9849
9850 maybe_print_thread_hit_breakpoint (uiout);
9851
9852 if (b->disposition == disp_del)
9853 uiout->text ("Temporary ranged breakpoint ");
9854 else
9855 uiout->text ("Ranged breakpoint ");
9856 if (uiout->is_mi_like_p ())
9857 {
9858 uiout->field_string ("reason",
9859 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9860 uiout->field_string ("disp", bpdisp_text (b->disposition));
9861 }
9862 uiout->field_signed ("bkptno", b->number);
9863 uiout->text (", ");
9864
9865 return PRINT_SRC_AND_LOC;
9866 }
9867
9868 /* Implement the "print_one" breakpoint_ops method for
9869 ranged breakpoints. */
9870
9871 static void
9872 print_one_ranged_breakpoint (struct breakpoint *b,
9873 struct bp_location **last_loc)
9874 {
9875 struct bp_location *bl = b->loc;
9876 struct value_print_options opts;
9877 struct ui_out *uiout = current_uiout;
9878
9879 /* Ranged breakpoints have only one location. */
9880 gdb_assert (bl && bl->next == NULL);
9881
9882 get_user_print_options (&opts);
9883
9884 if (opts.addressprint)
9885 /* We don't print the address range here, it will be printed later
9886 by print_one_detail_ranged_breakpoint. */
9887 uiout->field_skip ("addr");
9888 annotate_field (5);
9889 print_breakpoint_location (b, bl);
9890 *last_loc = bl;
9891 }
9892
9893 /* Implement the "print_one_detail" breakpoint_ops method for
9894 ranged breakpoints. */
9895
9896 static void
9897 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9898 struct ui_out *uiout)
9899 {
9900 CORE_ADDR address_start, address_end;
9901 struct bp_location *bl = b->loc;
9902 string_file stb;
9903
9904 gdb_assert (bl);
9905
9906 address_start = bl->address;
9907 address_end = address_start + bl->length - 1;
9908
9909 uiout->text ("\taddress range: ");
9910 stb.printf ("[%s, %s]",
9911 print_core_address (bl->gdbarch, address_start),
9912 print_core_address (bl->gdbarch, address_end));
9913 uiout->field_stream ("addr", stb);
9914 uiout->text ("\n");
9915 }
9916
9917 /* Implement the "print_mention" breakpoint_ops method for
9918 ranged breakpoints. */
9919
9920 static void
9921 print_mention_ranged_breakpoint (struct breakpoint *b)
9922 {
9923 struct bp_location *bl = b->loc;
9924 struct ui_out *uiout = current_uiout;
9925
9926 gdb_assert (bl);
9927 gdb_assert (b->type == bp_hardware_breakpoint);
9928
9929 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9930 b->number, paddress (bl->gdbarch, bl->address),
9931 paddress (bl->gdbarch, bl->address + bl->length - 1));
9932 }
9933
9934 /* Implement the "print_recreate" breakpoint_ops method for
9935 ranged breakpoints. */
9936
9937 static void
9938 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9939 {
9940 fprintf_unfiltered (fp, "break-range %s, %s",
9941 event_location_to_string (b->location.get ()),
9942 event_location_to_string (b->location_range_end.get ()));
9943 print_recreate_thread (b, fp);
9944 }
9945
9946 /* The breakpoint_ops structure to be used in ranged breakpoints. */
9947
9948 static struct breakpoint_ops ranged_breakpoint_ops;
9949
9950 /* Find the address where the end of the breakpoint range should be
9951 placed, given the SAL of the end of the range. This is so that if
9952 the user provides a line number, the end of the range is set to the
9953 last instruction of the given line. */
9954
9955 static CORE_ADDR
9956 find_breakpoint_range_end (struct symtab_and_line sal)
9957 {
9958 CORE_ADDR end;
9959
9960 /* If the user provided a PC value, use it. Otherwise,
9961 find the address of the end of the given location. */
9962 if (sal.explicit_pc)
9963 end = sal.pc;
9964 else
9965 {
9966 int ret;
9967 CORE_ADDR start;
9968
9969 ret = find_line_pc_range (sal, &start, &end);
9970 if (!ret)
9971 error (_("Could not find location of the end of the range."));
9972
9973 /* find_line_pc_range returns the start of the next line. */
9974 end--;
9975 }
9976
9977 return end;
9978 }
9979
9980 /* Implement the "break-range" CLI command. */
9981
9982 static void
9983 break_range_command (const char *arg, int from_tty)
9984 {
9985 const char *arg_start;
9986 struct linespec_result canonical_start, canonical_end;
9987 int bp_count, can_use_bp, length;
9988 CORE_ADDR end;
9989 struct breakpoint *b;
9990
9991 /* We don't support software ranged breakpoints. */
9992 if (target_ranged_break_num_registers () < 0)
9993 error (_("This target does not support hardware ranged breakpoints."));
9994
9995 bp_count = hw_breakpoint_used_count ();
9996 bp_count += target_ranged_break_num_registers ();
9997 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9998 bp_count, 0);
9999 if (can_use_bp < 0)
10000 error (_("Hardware breakpoints used exceeds limit."));
10001
10002 arg = skip_spaces (arg);
10003 if (arg == NULL || arg[0] == '\0')
10004 error(_("No address range specified."));
10005
10006 arg_start = arg;
10007 event_location_up start_location = string_to_event_location (&arg,
10008 current_language);
10009 parse_breakpoint_sals (start_location.get (), &canonical_start);
10010
10011 if (arg[0] != ',')
10012 error (_("Too few arguments."));
10013 else if (canonical_start.lsals.empty ())
10014 error (_("Could not find location of the beginning of the range."));
10015
10016 const linespec_sals &lsal_start = canonical_start.lsals[0];
10017
10018 if (canonical_start.lsals.size () > 1
10019 || lsal_start.sals.size () != 1)
10020 error (_("Cannot create a ranged breakpoint with multiple locations."));
10021
10022 const symtab_and_line &sal_start = lsal_start.sals[0];
10023 std::string addr_string_start (arg_start, arg - arg_start);
10024
10025 arg++; /* Skip the comma. */
10026 arg = skip_spaces (arg);
10027
10028 /* Parse the end location. */
10029
10030 arg_start = arg;
10031
10032 /* We call decode_line_full directly here instead of using
10033 parse_breakpoint_sals because we need to specify the start location's
10034 symtab and line as the default symtab and line for the end of the
10035 range. This makes it possible to have ranges like "foo.c:27, +14",
10036 where +14 means 14 lines from the start location. */
10037 event_location_up end_location = string_to_event_location (&arg,
10038 current_language);
10039 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10040 sal_start.symtab, sal_start.line,
10041 &canonical_end, NULL, NULL);
10042
10043 if (canonical_end.lsals.empty ())
10044 error (_("Could not find location of the end of the range."));
10045
10046 const linespec_sals &lsal_end = canonical_end.lsals[0];
10047 if (canonical_end.lsals.size () > 1
10048 || lsal_end.sals.size () != 1)
10049 error (_("Cannot create a ranged breakpoint with multiple locations."));
10050
10051 const symtab_and_line &sal_end = lsal_end.sals[0];
10052
10053 end = find_breakpoint_range_end (sal_end);
10054 if (sal_start.pc > end)
10055 error (_("Invalid address range, end precedes start."));
10056
10057 length = end - sal_start.pc + 1;
10058 if (length < 0)
10059 /* Length overflowed. */
10060 error (_("Address range too large."));
10061 else if (length == 1)
10062 {
10063 /* This range is simple enough to be handled by
10064 the `hbreak' command. */
10065 hbreak_command (&addr_string_start[0], 1);
10066
10067 return;
10068 }
10069
10070 /* Now set up the breakpoint. */
10071 b = set_raw_breakpoint (get_current_arch (), sal_start,
10072 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10073 set_breakpoint_count (breakpoint_count + 1);
10074 b->number = breakpoint_count;
10075 b->disposition = disp_donttouch;
10076 b->location = std::move (start_location);
10077 b->location_range_end = std::move (end_location);
10078 b->loc->length = length;
10079
10080 mention (b);
10081 gdb::observers::breakpoint_created.notify (b);
10082 update_global_location_list (UGLL_MAY_INSERT);
10083 }
10084
10085 /* Return non-zero if EXP is verified as constant. Returned zero
10086 means EXP is variable. Also the constant detection may fail for
10087 some constant expressions and in such case still falsely return
10088 zero. */
10089
10090 static bool
10091 watchpoint_exp_is_const (const struct expression *exp)
10092 {
10093 int i = exp->nelts;
10094
10095 while (i > 0)
10096 {
10097 int oplenp, argsp;
10098
10099 /* We are only interested in the descriptor of each element. */
10100 operator_length (exp, i, &oplenp, &argsp);
10101 i -= oplenp;
10102
10103 switch (exp->elts[i].opcode)
10104 {
10105 case BINOP_ADD:
10106 case BINOP_SUB:
10107 case BINOP_MUL:
10108 case BINOP_DIV:
10109 case BINOP_REM:
10110 case BINOP_MOD:
10111 case BINOP_LSH:
10112 case BINOP_RSH:
10113 case BINOP_LOGICAL_AND:
10114 case BINOP_LOGICAL_OR:
10115 case BINOP_BITWISE_AND:
10116 case BINOP_BITWISE_IOR:
10117 case BINOP_BITWISE_XOR:
10118 case BINOP_EQUAL:
10119 case BINOP_NOTEQUAL:
10120 case BINOP_LESS:
10121 case BINOP_GTR:
10122 case BINOP_LEQ:
10123 case BINOP_GEQ:
10124 case BINOP_REPEAT:
10125 case BINOP_COMMA:
10126 case BINOP_EXP:
10127 case BINOP_MIN:
10128 case BINOP_MAX:
10129 case BINOP_INTDIV:
10130 case BINOP_CONCAT:
10131 case TERNOP_COND:
10132 case TERNOP_SLICE:
10133
10134 case OP_LONG:
10135 case OP_FLOAT:
10136 case OP_LAST:
10137 case OP_COMPLEX:
10138 case OP_STRING:
10139 case OP_ARRAY:
10140 case OP_TYPE:
10141 case OP_TYPEOF:
10142 case OP_DECLTYPE:
10143 case OP_TYPEID:
10144 case OP_NAME:
10145 case OP_OBJC_NSSTRING:
10146
10147 case UNOP_NEG:
10148 case UNOP_LOGICAL_NOT:
10149 case UNOP_COMPLEMENT:
10150 case UNOP_ADDR:
10151 case UNOP_HIGH:
10152 case UNOP_CAST:
10153
10154 case UNOP_CAST_TYPE:
10155 case UNOP_REINTERPRET_CAST:
10156 case UNOP_DYNAMIC_CAST:
10157 /* Unary, binary and ternary operators: We have to check
10158 their operands. If they are constant, then so is the
10159 result of that operation. For instance, if A and B are
10160 determined to be constants, then so is "A + B".
10161
10162 UNOP_IND is one exception to the rule above, because the
10163 value of *ADDR is not necessarily a constant, even when
10164 ADDR is. */
10165 break;
10166
10167 case OP_VAR_VALUE:
10168 /* Check whether the associated symbol is a constant.
10169
10170 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10171 possible that a buggy compiler could mark a variable as
10172 constant even when it is not, and TYPE_CONST would return
10173 true in this case, while SYMBOL_CLASS wouldn't.
10174
10175 We also have to check for function symbols because they
10176 are always constant. */
10177 {
10178 struct symbol *s = exp->elts[i + 2].symbol;
10179
10180 if (SYMBOL_CLASS (s) != LOC_BLOCK
10181 && SYMBOL_CLASS (s) != LOC_CONST
10182 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10183 return false;
10184 break;
10185 }
10186
10187 /* The default action is to return 0 because we are using
10188 the optimistic approach here: If we don't know something,
10189 then it is not a constant. */
10190 default:
10191 return false;
10192 }
10193 }
10194
10195 return true;
10196 }
10197
10198 /* Watchpoint destructor. */
10199
10200 watchpoint::~watchpoint ()
10201 {
10202 xfree (this->exp_string);
10203 xfree (this->exp_string_reparse);
10204 }
10205
10206 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10207
10208 static void
10209 re_set_watchpoint (struct breakpoint *b)
10210 {
10211 struct watchpoint *w = (struct watchpoint *) b;
10212
10213 /* Watchpoint can be either on expression using entirely global
10214 variables, or it can be on local variables.
10215
10216 Watchpoints of the first kind are never auto-deleted, and even
10217 persist across program restarts. Since they can use variables
10218 from shared libraries, we need to reparse expression as libraries
10219 are loaded and unloaded.
10220
10221 Watchpoints on local variables can also change meaning as result
10222 of solib event. For example, if a watchpoint uses both a local
10223 and a global variables in expression, it's a local watchpoint,
10224 but unloading of a shared library will make the expression
10225 invalid. This is not a very common use case, but we still
10226 re-evaluate expression, to avoid surprises to the user.
10227
10228 Note that for local watchpoints, we re-evaluate it only if
10229 watchpoints frame id is still valid. If it's not, it means the
10230 watchpoint is out of scope and will be deleted soon. In fact,
10231 I'm not sure we'll ever be called in this case.
10232
10233 If a local watchpoint's frame id is still valid, then
10234 w->exp_valid_block is likewise valid, and we can safely use it.
10235
10236 Don't do anything about disabled watchpoints, since they will be
10237 reevaluated again when enabled. */
10238 update_watchpoint (w, 1 /* reparse */);
10239 }
10240
10241 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10242
10243 static int
10244 insert_watchpoint (struct bp_location *bl)
10245 {
10246 struct watchpoint *w = (struct watchpoint *) bl->owner;
10247 int length = w->exact ? 1 : bl->length;
10248
10249 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10250 w->cond_exp.get ());
10251 }
10252
10253 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10254
10255 static int
10256 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10257 {
10258 struct watchpoint *w = (struct watchpoint *) bl->owner;
10259 int length = w->exact ? 1 : bl->length;
10260
10261 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10262 w->cond_exp.get ());
10263 }
10264
10265 static int
10266 breakpoint_hit_watchpoint (const struct bp_location *bl,
10267 const address_space *aspace, CORE_ADDR bp_addr,
10268 const struct target_waitstatus *ws)
10269 {
10270 struct breakpoint *b = bl->owner;
10271 struct watchpoint *w = (struct watchpoint *) b;
10272
10273 /* Continuable hardware watchpoints are treated as non-existent if the
10274 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10275 some data address). Otherwise gdb won't stop on a break instruction
10276 in the code (not from a breakpoint) when a hardware watchpoint has
10277 been defined. Also skip watchpoints which we know did not trigger
10278 (did not match the data address). */
10279 if (is_hardware_watchpoint (b)
10280 && w->watchpoint_triggered == watch_triggered_no)
10281 return 0;
10282
10283 return 1;
10284 }
10285
10286 static void
10287 check_status_watchpoint (bpstat bs)
10288 {
10289 gdb_assert (is_watchpoint (bs->breakpoint_at));
10290
10291 bpstat_check_watchpoint (bs);
10292 }
10293
10294 /* Implement the "resources_needed" breakpoint_ops method for
10295 hardware watchpoints. */
10296
10297 static int
10298 resources_needed_watchpoint (const struct bp_location *bl)
10299 {
10300 struct watchpoint *w = (struct watchpoint *) bl->owner;
10301 int length = w->exact? 1 : bl->length;
10302
10303 return target_region_ok_for_hw_watchpoint (bl->address, length);
10304 }
10305
10306 /* Implement the "works_in_software_mode" breakpoint_ops method for
10307 hardware watchpoints. */
10308
10309 static int
10310 works_in_software_mode_watchpoint (const struct breakpoint *b)
10311 {
10312 /* Read and access watchpoints only work with hardware support. */
10313 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10314 }
10315
10316 static enum print_stop_action
10317 print_it_watchpoint (bpstat bs)
10318 {
10319 struct breakpoint *b;
10320 enum print_stop_action result;
10321 struct watchpoint *w;
10322 struct ui_out *uiout = current_uiout;
10323
10324 gdb_assert (bs->bp_location_at != NULL);
10325
10326 b = bs->breakpoint_at;
10327 w = (struct watchpoint *) b;
10328
10329 annotate_watchpoint (b->number);
10330 maybe_print_thread_hit_breakpoint (uiout);
10331
10332 string_file stb;
10333
10334 gdb::optional<ui_out_emit_tuple> tuple_emitter;
10335 switch (b->type)
10336 {
10337 case bp_watchpoint:
10338 case bp_hardware_watchpoint:
10339 if (uiout->is_mi_like_p ())
10340 uiout->field_string
10341 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10342 mention (b);
10343 tuple_emitter.emplace (uiout, "value");
10344 uiout->text ("\nOld value = ");
10345 watchpoint_value_print (bs->old_val.get (), &stb);
10346 uiout->field_stream ("old", stb);
10347 uiout->text ("\nNew value = ");
10348 watchpoint_value_print (w->val.get (), &stb);
10349 uiout->field_stream ("new", stb);
10350 uiout->text ("\n");
10351 /* More than one watchpoint may have been triggered. */
10352 result = PRINT_UNKNOWN;
10353 break;
10354
10355 case bp_read_watchpoint:
10356 if (uiout->is_mi_like_p ())
10357 uiout->field_string
10358 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10359 mention (b);
10360 tuple_emitter.emplace (uiout, "value");
10361 uiout->text ("\nValue = ");
10362 watchpoint_value_print (w->val.get (), &stb);
10363 uiout->field_stream ("value", stb);
10364 uiout->text ("\n");
10365 result = PRINT_UNKNOWN;
10366 break;
10367
10368 case bp_access_watchpoint:
10369 if (bs->old_val != NULL)
10370 {
10371 if (uiout->is_mi_like_p ())
10372 uiout->field_string
10373 ("reason",
10374 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10375 mention (b);
10376 tuple_emitter.emplace (uiout, "value");
10377 uiout->text ("\nOld value = ");
10378 watchpoint_value_print (bs->old_val.get (), &stb);
10379 uiout->field_stream ("old", stb);
10380 uiout->text ("\nNew value = ");
10381 }
10382 else
10383 {
10384 mention (b);
10385 if (uiout->is_mi_like_p ())
10386 uiout->field_string
10387 ("reason",
10388 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10389 tuple_emitter.emplace (uiout, "value");
10390 uiout->text ("\nValue = ");
10391 }
10392 watchpoint_value_print (w->val.get (), &stb);
10393 uiout->field_stream ("new", stb);
10394 uiout->text ("\n");
10395 result = PRINT_UNKNOWN;
10396 break;
10397 default:
10398 result = PRINT_UNKNOWN;
10399 }
10400
10401 return result;
10402 }
10403
10404 /* Implement the "print_mention" breakpoint_ops method for hardware
10405 watchpoints. */
10406
10407 static void
10408 print_mention_watchpoint (struct breakpoint *b)
10409 {
10410 struct watchpoint *w = (struct watchpoint *) b;
10411 struct ui_out *uiout = current_uiout;
10412 const char *tuple_name;
10413
10414 switch (b->type)
10415 {
10416 case bp_watchpoint:
10417 uiout->text ("Watchpoint ");
10418 tuple_name = "wpt";
10419 break;
10420 case bp_hardware_watchpoint:
10421 uiout->text ("Hardware watchpoint ");
10422 tuple_name = "wpt";
10423 break;
10424 case bp_read_watchpoint:
10425 uiout->text ("Hardware read watchpoint ");
10426 tuple_name = "hw-rwpt";
10427 break;
10428 case bp_access_watchpoint:
10429 uiout->text ("Hardware access (read/write) watchpoint ");
10430 tuple_name = "hw-awpt";
10431 break;
10432 default:
10433 internal_error (__FILE__, __LINE__,
10434 _("Invalid hardware watchpoint type."));
10435 }
10436
10437 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10438 uiout->field_signed ("number", b->number);
10439 uiout->text (": ");
10440 uiout->field_string ("exp", w->exp_string);
10441 }
10442
10443 /* Implement the "print_recreate" breakpoint_ops method for
10444 watchpoints. */
10445
10446 static void
10447 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10448 {
10449 struct watchpoint *w = (struct watchpoint *) b;
10450
10451 switch (b->type)
10452 {
10453 case bp_watchpoint:
10454 case bp_hardware_watchpoint:
10455 fprintf_unfiltered (fp, "watch");
10456 break;
10457 case bp_read_watchpoint:
10458 fprintf_unfiltered (fp, "rwatch");
10459 break;
10460 case bp_access_watchpoint:
10461 fprintf_unfiltered (fp, "awatch");
10462 break;
10463 default:
10464 internal_error (__FILE__, __LINE__,
10465 _("Invalid watchpoint type."));
10466 }
10467
10468 fprintf_unfiltered (fp, " %s", w->exp_string);
10469 print_recreate_thread (b, fp);
10470 }
10471
10472 /* Implement the "explains_signal" breakpoint_ops method for
10473 watchpoints. */
10474
10475 static int
10476 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10477 {
10478 /* A software watchpoint cannot cause a signal other than
10479 GDB_SIGNAL_TRAP. */
10480 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10481 return 0;
10482
10483 return 1;
10484 }
10485
10486 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10487
10488 static struct breakpoint_ops watchpoint_breakpoint_ops;
10489
10490 /* Implement the "insert" breakpoint_ops method for
10491 masked hardware watchpoints. */
10492
10493 static int
10494 insert_masked_watchpoint (struct bp_location *bl)
10495 {
10496 struct watchpoint *w = (struct watchpoint *) bl->owner;
10497
10498 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10499 bl->watchpoint_type);
10500 }
10501
10502 /* Implement the "remove" breakpoint_ops method for
10503 masked hardware watchpoints. */
10504
10505 static int
10506 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10507 {
10508 struct watchpoint *w = (struct watchpoint *) bl->owner;
10509
10510 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10511 bl->watchpoint_type);
10512 }
10513
10514 /* Implement the "resources_needed" breakpoint_ops method for
10515 masked hardware watchpoints. */
10516
10517 static int
10518 resources_needed_masked_watchpoint (const struct bp_location *bl)
10519 {
10520 struct watchpoint *w = (struct watchpoint *) bl->owner;
10521
10522 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10523 }
10524
10525 /* Implement the "works_in_software_mode" breakpoint_ops method for
10526 masked hardware watchpoints. */
10527
10528 static int
10529 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10530 {
10531 return 0;
10532 }
10533
10534 /* Implement the "print_it" breakpoint_ops method for
10535 masked hardware watchpoints. */
10536
10537 static enum print_stop_action
10538 print_it_masked_watchpoint (bpstat bs)
10539 {
10540 struct breakpoint *b = bs->breakpoint_at;
10541 struct ui_out *uiout = current_uiout;
10542
10543 /* Masked watchpoints have only one location. */
10544 gdb_assert (b->loc && b->loc->next == NULL);
10545
10546 annotate_watchpoint (b->number);
10547 maybe_print_thread_hit_breakpoint (uiout);
10548
10549 switch (b->type)
10550 {
10551 case bp_hardware_watchpoint:
10552 if (uiout->is_mi_like_p ())
10553 uiout->field_string
10554 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10555 break;
10556
10557 case bp_read_watchpoint:
10558 if (uiout->is_mi_like_p ())
10559 uiout->field_string
10560 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10561 break;
10562
10563 case bp_access_watchpoint:
10564 if (uiout->is_mi_like_p ())
10565 uiout->field_string
10566 ("reason",
10567 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10568 break;
10569 default:
10570 internal_error (__FILE__, __LINE__,
10571 _("Invalid hardware watchpoint type."));
10572 }
10573
10574 mention (b);
10575 uiout->text (_("\n\
10576 Check the underlying instruction at PC for the memory\n\
10577 address and value which triggered this watchpoint.\n"));
10578 uiout->text ("\n");
10579
10580 /* More than one watchpoint may have been triggered. */
10581 return PRINT_UNKNOWN;
10582 }
10583
10584 /* Implement the "print_one_detail" breakpoint_ops method for
10585 masked hardware watchpoints. */
10586
10587 static void
10588 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10589 struct ui_out *uiout)
10590 {
10591 struct watchpoint *w = (struct watchpoint *) b;
10592
10593 /* Masked watchpoints have only one location. */
10594 gdb_assert (b->loc && b->loc->next == NULL);
10595
10596 uiout->text ("\tmask ");
10597 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10598 uiout->text ("\n");
10599 }
10600
10601 /* Implement the "print_mention" breakpoint_ops method for
10602 masked hardware watchpoints. */
10603
10604 static void
10605 print_mention_masked_watchpoint (struct breakpoint *b)
10606 {
10607 struct watchpoint *w = (struct watchpoint *) b;
10608 struct ui_out *uiout = current_uiout;
10609 const char *tuple_name;
10610
10611 switch (b->type)
10612 {
10613 case bp_hardware_watchpoint:
10614 uiout->text ("Masked hardware watchpoint ");
10615 tuple_name = "wpt";
10616 break;
10617 case bp_read_watchpoint:
10618 uiout->text ("Masked hardware read watchpoint ");
10619 tuple_name = "hw-rwpt";
10620 break;
10621 case bp_access_watchpoint:
10622 uiout->text ("Masked hardware access (read/write) watchpoint ");
10623 tuple_name = "hw-awpt";
10624 break;
10625 default:
10626 internal_error (__FILE__, __LINE__,
10627 _("Invalid hardware watchpoint type."));
10628 }
10629
10630 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10631 uiout->field_signed ("number", b->number);
10632 uiout->text (": ");
10633 uiout->field_string ("exp", w->exp_string);
10634 }
10635
10636 /* Implement the "print_recreate" breakpoint_ops method for
10637 masked hardware watchpoints. */
10638
10639 static void
10640 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10641 {
10642 struct watchpoint *w = (struct watchpoint *) b;
10643
10644 switch (b->type)
10645 {
10646 case bp_hardware_watchpoint:
10647 fprintf_unfiltered (fp, "watch");
10648 break;
10649 case bp_read_watchpoint:
10650 fprintf_unfiltered (fp, "rwatch");
10651 break;
10652 case bp_access_watchpoint:
10653 fprintf_unfiltered (fp, "awatch");
10654 break;
10655 default:
10656 internal_error (__FILE__, __LINE__,
10657 _("Invalid hardware watchpoint type."));
10658 }
10659
10660 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string,
10661 phex (w->hw_wp_mask, sizeof (CORE_ADDR)));
10662 print_recreate_thread (b, fp);
10663 }
10664
10665 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10666
10667 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10668
10669 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10670
10671 static bool
10672 is_masked_watchpoint (const struct breakpoint *b)
10673 {
10674 return b->ops == &masked_watchpoint_breakpoint_ops;
10675 }
10676
10677 /* accessflag: hw_write: watch write,
10678 hw_read: watch read,
10679 hw_access: watch access (read or write) */
10680 static void
10681 watch_command_1 (const char *arg, int accessflag, int from_tty,
10682 bool just_location, bool internal)
10683 {
10684 struct breakpoint *scope_breakpoint = NULL;
10685 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10686 struct value *result;
10687 int saved_bitpos = 0, saved_bitsize = 0;
10688 const char *exp_start = NULL;
10689 const char *exp_end = NULL;
10690 const char *tok, *end_tok;
10691 int toklen = -1;
10692 const char *cond_start = NULL;
10693 const char *cond_end = NULL;
10694 enum bptype bp_type;
10695 int thread = -1;
10696 int pc = 0;
10697 /* Flag to indicate whether we are going to use masks for
10698 the hardware watchpoint. */
10699 bool use_mask = false;
10700 CORE_ADDR mask = 0;
10701
10702 /* Make sure that we actually have parameters to parse. */
10703 if (arg != NULL && arg[0] != '\0')
10704 {
10705 const char *value_start;
10706
10707 exp_end = arg + strlen (arg);
10708
10709 /* Look for "parameter value" pairs at the end
10710 of the arguments string. */
10711 for (tok = exp_end - 1; tok > arg; tok--)
10712 {
10713 /* Skip whitespace at the end of the argument list. */
10714 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10715 tok--;
10716
10717 /* Find the beginning of the last token.
10718 This is the value of the parameter. */
10719 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10720 tok--;
10721 value_start = tok + 1;
10722
10723 /* Skip whitespace. */
10724 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10725 tok--;
10726
10727 end_tok = tok;
10728
10729 /* Find the beginning of the second to last token.
10730 This is the parameter itself. */
10731 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10732 tok--;
10733 tok++;
10734 toklen = end_tok - tok + 1;
10735
10736 if (toklen == 6 && startswith (tok, "thread"))
10737 {
10738 struct thread_info *thr;
10739 /* At this point we've found a "thread" token, which means
10740 the user is trying to set a watchpoint that triggers
10741 only in a specific thread. */
10742 const char *endp;
10743
10744 if (thread != -1)
10745 error(_("You can specify only one thread."));
10746
10747 /* Extract the thread ID from the next token. */
10748 thr = parse_thread_id (value_start, &endp);
10749
10750 /* Check if the user provided a valid thread ID. */
10751 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10752 invalid_thread_id_error (value_start);
10753
10754 thread = thr->global_num;
10755 }
10756 else if (toklen == 4 && startswith (tok, "mask"))
10757 {
10758 /* We've found a "mask" token, which means the user wants to
10759 create a hardware watchpoint that is going to have the mask
10760 facility. */
10761 struct value *mask_value, *mark;
10762
10763 if (use_mask)
10764 error(_("You can specify only one mask."));
10765
10766 use_mask = just_location = true;
10767
10768 mark = value_mark ();
10769 mask_value = parse_to_comma_and_eval (&value_start);
10770 mask = value_as_address (mask_value);
10771 value_free_to_mark (mark);
10772 }
10773 else
10774 /* We didn't recognize what we found. We should stop here. */
10775 break;
10776
10777 /* Truncate the string and get rid of the "parameter value" pair before
10778 the arguments string is parsed by the parse_exp_1 function. */
10779 exp_end = tok;
10780 }
10781 }
10782 else
10783 exp_end = arg;
10784
10785 /* Parse the rest of the arguments. From here on out, everything
10786 is in terms of a newly allocated string instead of the original
10787 ARG. */
10788 std::string expression (arg, exp_end - arg);
10789 exp_start = arg = expression.c_str ();
10790 innermost_block_tracker tracker;
10791 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10792 exp_end = arg;
10793 /* Remove trailing whitespace from the expression before saving it.
10794 This makes the eventual display of the expression string a bit
10795 prettier. */
10796 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10797 --exp_end;
10798
10799 /* Checking if the expression is not constant. */
10800 if (watchpoint_exp_is_const (exp.get ()))
10801 {
10802 int len;
10803
10804 len = exp_end - exp_start;
10805 while (len > 0 && isspace (exp_start[len - 1]))
10806 len--;
10807 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10808 }
10809
10810 exp_valid_block = tracker.block ();
10811 struct value *mark = value_mark ();
10812 struct value *val_as_value = nullptr;
10813 fetch_subexp_value (exp.get (), &pc, &val_as_value, &result, NULL,
10814 just_location);
10815
10816 if (val_as_value != NULL && just_location)
10817 {
10818 saved_bitpos = value_bitpos (val_as_value);
10819 saved_bitsize = value_bitsize (val_as_value);
10820 }
10821
10822 value_ref_ptr val;
10823 if (just_location)
10824 {
10825 int ret;
10826
10827 exp_valid_block = NULL;
10828 val = release_value (value_addr (result));
10829 value_free_to_mark (mark);
10830
10831 if (use_mask)
10832 {
10833 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10834 mask);
10835 if (ret == -1)
10836 error (_("This target does not support masked watchpoints."));
10837 else if (ret == -2)
10838 error (_("Invalid mask or memory region."));
10839 }
10840 }
10841 else if (val_as_value != NULL)
10842 val = release_value (val_as_value);
10843
10844 tok = skip_spaces (arg);
10845 end_tok = skip_to_space (tok);
10846
10847 toklen = end_tok - tok;
10848 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10849 {
10850 tok = cond_start = end_tok + 1;
10851 innermost_block_tracker if_tracker;
10852 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10853
10854 /* The watchpoint expression may not be local, but the condition
10855 may still be. E.g.: `watch global if local > 0'. */
10856 cond_exp_valid_block = if_tracker.block ();
10857
10858 cond_end = tok;
10859 }
10860 if (*tok)
10861 error (_("Junk at end of command."));
10862
10863 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10864
10865 /* Save this because create_internal_breakpoint below invalidates
10866 'wp_frame'. */
10867 frame_id watchpoint_frame = get_frame_id (wp_frame);
10868
10869 /* If the expression is "local", then set up a "watchpoint scope"
10870 breakpoint at the point where we've left the scope of the watchpoint
10871 expression. Create the scope breakpoint before the watchpoint, so
10872 that we will encounter it first in bpstat_stop_status. */
10873 if (exp_valid_block != NULL && wp_frame != NULL)
10874 {
10875 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10876
10877 if (frame_id_p (caller_frame_id))
10878 {
10879 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10880 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10881
10882 scope_breakpoint
10883 = create_internal_breakpoint (caller_arch, caller_pc,
10884 bp_watchpoint_scope,
10885 &momentary_breakpoint_ops);
10886
10887 /* create_internal_breakpoint could invalidate WP_FRAME. */
10888 wp_frame = NULL;
10889
10890 scope_breakpoint->enable_state = bp_enabled;
10891
10892 /* Automatically delete the breakpoint when it hits. */
10893 scope_breakpoint->disposition = disp_del;
10894
10895 /* Only break in the proper frame (help with recursion). */
10896 scope_breakpoint->frame_id = caller_frame_id;
10897
10898 /* Set the address at which we will stop. */
10899 scope_breakpoint->loc->gdbarch = caller_arch;
10900 scope_breakpoint->loc->requested_address = caller_pc;
10901 scope_breakpoint->loc->address
10902 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10903 scope_breakpoint->loc->requested_address,
10904 scope_breakpoint->type);
10905 }
10906 }
10907
10908 /* Now set up the breakpoint. We create all watchpoints as hardware
10909 watchpoints here even if hardware watchpoints are turned off, a call
10910 to update_watchpoint later in this function will cause the type to
10911 drop back to bp_watchpoint (software watchpoint) if required. */
10912
10913 if (accessflag == hw_read)
10914 bp_type = bp_read_watchpoint;
10915 else if (accessflag == hw_access)
10916 bp_type = bp_access_watchpoint;
10917 else
10918 bp_type = bp_hardware_watchpoint;
10919
10920 std::unique_ptr<watchpoint> w (new watchpoint ());
10921
10922 if (use_mask)
10923 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10924 &masked_watchpoint_breakpoint_ops);
10925 else
10926 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10927 &watchpoint_breakpoint_ops);
10928 w->thread = thread;
10929 w->disposition = disp_donttouch;
10930 w->pspace = current_program_space;
10931 w->exp = std::move (exp);
10932 w->exp_valid_block = exp_valid_block;
10933 w->cond_exp_valid_block = cond_exp_valid_block;
10934 if (just_location)
10935 {
10936 struct type *t = value_type (val.get ());
10937 CORE_ADDR addr = value_as_address (val.get ());
10938
10939 w->exp_string_reparse
10940 = current_language->watch_location_expression (t, addr).release ();
10941
10942 w->exp_string = xstrprintf ("-location %.*s",
10943 (int) (exp_end - exp_start), exp_start);
10944 }
10945 else
10946 w->exp_string = savestring (exp_start, exp_end - exp_start);
10947
10948 if (use_mask)
10949 {
10950 w->hw_wp_mask = mask;
10951 }
10952 else
10953 {
10954 w->val = val;
10955 w->val_bitpos = saved_bitpos;
10956 w->val_bitsize = saved_bitsize;
10957 w->val_valid = true;
10958 }
10959
10960 if (cond_start)
10961 w->cond_string = savestring (cond_start, cond_end - cond_start);
10962 else
10963 w->cond_string = 0;
10964
10965 if (frame_id_p (watchpoint_frame))
10966 {
10967 w->watchpoint_frame = watchpoint_frame;
10968 w->watchpoint_thread = inferior_ptid;
10969 }
10970 else
10971 {
10972 w->watchpoint_frame = null_frame_id;
10973 w->watchpoint_thread = null_ptid;
10974 }
10975
10976 if (scope_breakpoint != NULL)
10977 {
10978 /* The scope breakpoint is related to the watchpoint. We will
10979 need to act on them together. */
10980 w->related_breakpoint = scope_breakpoint;
10981 scope_breakpoint->related_breakpoint = w.get ();
10982 }
10983
10984 if (!just_location)
10985 value_free_to_mark (mark);
10986
10987 /* Finally update the new watchpoint. This creates the locations
10988 that should be inserted. */
10989 update_watchpoint (w.get (), 1);
10990
10991 install_breakpoint (internal, std::move (w), 1);
10992 }
10993
10994 /* Return count of debug registers needed to watch the given expression.
10995 If the watchpoint cannot be handled in hardware return zero. */
10996
10997 static int
10998 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10999 {
11000 int found_memory_cnt = 0;
11001
11002 /* Did the user specifically forbid us to use hardware watchpoints? */
11003 if (!can_use_hw_watchpoints)
11004 return 0;
11005
11006 gdb_assert (!vals.empty ());
11007 struct value *head = vals[0].get ();
11008
11009 /* Make sure that the value of the expression depends only upon
11010 memory contents, and values computed from them within GDB. If we
11011 find any register references or function calls, we can't use a
11012 hardware watchpoint.
11013
11014 The idea here is that evaluating an expression generates a series
11015 of values, one holding the value of every subexpression. (The
11016 expression a*b+c has five subexpressions: a, b, a*b, c, and
11017 a*b+c.) GDB's values hold almost enough information to establish
11018 the criteria given above --- they identify memory lvalues,
11019 register lvalues, computed values, etcetera. So we can evaluate
11020 the expression, and then scan the chain of values that leaves
11021 behind to decide whether we can detect any possible change to the
11022 expression's final value using only hardware watchpoints.
11023
11024 However, I don't think that the values returned by inferior
11025 function calls are special in any way. So this function may not
11026 notice that an expression involving an inferior function call
11027 can't be watched with hardware watchpoints. FIXME. */
11028 for (const value_ref_ptr &iter : vals)
11029 {
11030 struct value *v = iter.get ();
11031
11032 if (VALUE_LVAL (v) == lval_memory)
11033 {
11034 if (v != head && value_lazy (v))
11035 /* A lazy memory lvalue in the chain is one that GDB never
11036 needed to fetch; we either just used its address (e.g.,
11037 `a' in `a.b') or we never needed it at all (e.g., `a'
11038 in `a,b'). This doesn't apply to HEAD; if that is
11039 lazy then it was not readable, but watch it anyway. */
11040 ;
11041 else
11042 {
11043 /* Ahh, memory we actually used! Check if we can cover
11044 it with hardware watchpoints. */
11045 struct type *vtype = check_typedef (value_type (v));
11046
11047 /* We only watch structs and arrays if user asked for it
11048 explicitly, never if they just happen to appear in a
11049 middle of some value chain. */
11050 if (v == head
11051 || (vtype->code () != TYPE_CODE_STRUCT
11052 && vtype->code () != TYPE_CODE_ARRAY))
11053 {
11054 CORE_ADDR vaddr = value_address (v);
11055 int len;
11056 int num_regs;
11057
11058 len = (target_exact_watchpoints
11059 && is_scalar_type_recursive (vtype))?
11060 1 : TYPE_LENGTH (value_type (v));
11061
11062 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11063 if (!num_regs)
11064 return 0;
11065 else
11066 found_memory_cnt += num_regs;
11067 }
11068 }
11069 }
11070 else if (VALUE_LVAL (v) != not_lval
11071 && deprecated_value_modifiable (v) == 0)
11072 return 0; /* These are values from the history (e.g., $1). */
11073 else if (VALUE_LVAL (v) == lval_register)
11074 return 0; /* Cannot watch a register with a HW watchpoint. */
11075 }
11076
11077 /* The expression itself looks suitable for using a hardware
11078 watchpoint, but give the target machine a chance to reject it. */
11079 return found_memory_cnt;
11080 }
11081
11082 void
11083 watch_command_wrapper (const char *arg, int from_tty, bool internal)
11084 {
11085 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11086 }
11087
11088 /* Options for the watch, awatch, and rwatch commands. */
11089
11090 struct watch_options
11091 {
11092 /* For -location. */
11093 bool location = false;
11094 };
11095
11096 /* Definitions of options for the "watch", "awatch", and "rwatch" commands.
11097
11098 Historically GDB always accepted both '-location' and '-l' flags for
11099 these commands (both flags being synonyms). When converting to the
11100 newer option scheme only '-location' is added here. That's fine (for
11101 backward compatibility) as any non-ambiguous prefix of a flag will be
11102 accepted, so '-l', '-loc', are now all accepted.
11103
11104 What this means is that, if in the future, we add any new flag here
11105 that starts with '-l' then this will break backward compatibility, so
11106 please, don't do that! */
11107
11108 static const gdb::option::option_def watch_option_defs[] = {
11109 gdb::option::flag_option_def<watch_options> {
11110 "location",
11111 [] (watch_options *opt) { return &opt->location; },
11112 N_("\
11113 This evaluates EXPRESSION and watches the memory to which is refers.\n\
11114 -l can be used as a short form of -location."),
11115 },
11116 };
11117
11118 /* Returns the option group used by 'watch', 'awatch', and 'rwatch'
11119 commands. */
11120
11121 static gdb::option::option_def_group
11122 make_watch_options_def_group (watch_options *opts)
11123 {
11124 return {{watch_option_defs}, opts};
11125 }
11126
11127 /* A helper function that looks for the "-location" argument and then
11128 calls watch_command_1. */
11129
11130 static void
11131 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
11132 {
11133 watch_options opts;
11134 auto grp = make_watch_options_def_group (&opts);
11135 gdb::option::process_options
11136 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
11137 if (arg != nullptr && *arg == '\0')
11138 arg = nullptr;
11139
11140 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
11141 }
11142
11143 /* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
11144 static void
11145 watch_command_completer (struct cmd_list_element *ignore,
11146 completion_tracker &tracker,
11147 const char *text, const char * /*word*/)
11148 {
11149 const auto group = make_watch_options_def_group (nullptr);
11150 if (gdb::option::complete_options
11151 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
11152 return;
11153
11154 const char *word = advance_to_expression_complete_word_point (tracker, text);
11155 expression_completer (ignore, tracker, text, word);
11156 }
11157
11158 static void
11159 watch_command (const char *arg, int from_tty)
11160 {
11161 watch_maybe_just_location (arg, hw_write, from_tty);
11162 }
11163
11164 void
11165 rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
11166 {
11167 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11168 }
11169
11170 static void
11171 rwatch_command (const char *arg, int from_tty)
11172 {
11173 watch_maybe_just_location (arg, hw_read, from_tty);
11174 }
11175
11176 void
11177 awatch_command_wrapper (const char *arg, int from_tty, bool internal)
11178 {
11179 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11180 }
11181
11182 static void
11183 awatch_command (const char *arg, int from_tty)
11184 {
11185 watch_maybe_just_location (arg, hw_access, from_tty);
11186 }
11187 \f
11188
11189 /* Data for the FSM that manages the until(location)/advance commands
11190 in infcmd.c. Here because it uses the mechanisms of
11191 breakpoints. */
11192
11193 struct until_break_fsm : public thread_fsm
11194 {
11195 /* The thread that was current when the command was executed. */
11196 int thread;
11197
11198 /* The breakpoint set at the return address in the caller frame,
11199 plus breakpoints at all the destination locations. */
11200 std::vector<breakpoint_up> breakpoints;
11201
11202 until_break_fsm (struct interp *cmd_interp, int thread,
11203 std::vector<breakpoint_up> &&breakpoints)
11204 : thread_fsm (cmd_interp),
11205 thread (thread),
11206 breakpoints (std::move (breakpoints))
11207 {
11208 }
11209
11210 void clean_up (struct thread_info *thread) override;
11211 bool should_stop (struct thread_info *thread) override;
11212 enum async_reply_reason do_async_reply_reason () override;
11213 };
11214
11215 /* Implementation of the 'should_stop' FSM method for the
11216 until(location)/advance commands. */
11217
11218 bool
11219 until_break_fsm::should_stop (struct thread_info *tp)
11220 {
11221 for (const breakpoint_up &bp : breakpoints)
11222 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11223 bp.get ()) != NULL)
11224 {
11225 set_finished ();
11226 break;
11227 }
11228
11229 return true;
11230 }
11231
11232 /* Implementation of the 'clean_up' FSM method for the
11233 until(location)/advance commands. */
11234
11235 void
11236 until_break_fsm::clean_up (struct thread_info *)
11237 {
11238 /* Clean up our temporary breakpoints. */
11239 breakpoints.clear ();
11240 delete_longjmp_breakpoint (thread);
11241 }
11242
11243 /* Implementation of the 'async_reply_reason' FSM method for the
11244 until(location)/advance commands. */
11245
11246 enum async_reply_reason
11247 until_break_fsm::do_async_reply_reason ()
11248 {
11249 return EXEC_ASYNC_LOCATION_REACHED;
11250 }
11251
11252 void
11253 until_break_command (const char *arg, int from_tty, int anywhere)
11254 {
11255 struct frame_info *frame;
11256 struct gdbarch *frame_gdbarch;
11257 struct frame_id stack_frame_id;
11258 struct frame_id caller_frame_id;
11259 int thread;
11260 struct thread_info *tp;
11261
11262 clear_proceed_status (0);
11263
11264 /* Set a breakpoint where the user wants it and at return from
11265 this function. */
11266
11267 event_location_up location = string_to_event_location (&arg, current_language);
11268
11269 std::vector<symtab_and_line> sals
11270 = (last_displayed_sal_is_valid ()
11271 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11272 get_last_displayed_symtab (),
11273 get_last_displayed_line ())
11274 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11275 NULL, NULL, 0));
11276
11277 if (sals.empty ())
11278 error (_("Couldn't get information on specified line."));
11279
11280 if (*arg)
11281 error (_("Junk at end of arguments."));
11282
11283 tp = inferior_thread ();
11284 thread = tp->global_num;
11285
11286 /* Note linespec handling above invalidates the frame chain.
11287 Installing a breakpoint also invalidates the frame chain (as it
11288 may need to switch threads), so do any frame handling before
11289 that. */
11290
11291 frame = get_selected_frame (NULL);
11292 frame_gdbarch = get_frame_arch (frame);
11293 stack_frame_id = get_stack_frame_id (frame);
11294 caller_frame_id = frame_unwind_caller_id (frame);
11295
11296 /* Keep within the current frame, or in frames called by the current
11297 one. */
11298
11299 std::vector<breakpoint_up> breakpoints;
11300
11301 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11302
11303 if (frame_id_p (caller_frame_id))
11304 {
11305 struct symtab_and_line sal2;
11306 struct gdbarch *caller_gdbarch;
11307
11308 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11309 sal2.pc = frame_unwind_caller_pc (frame);
11310 caller_gdbarch = frame_unwind_caller_arch (frame);
11311
11312 breakpoint_up caller_breakpoint
11313 = set_momentary_breakpoint (caller_gdbarch, sal2,
11314 caller_frame_id, bp_until);
11315 breakpoints.emplace_back (std::move (caller_breakpoint));
11316
11317 set_longjmp_breakpoint (tp, caller_frame_id);
11318 lj_deleter.emplace (thread);
11319 }
11320
11321 /* set_momentary_breakpoint could invalidate FRAME. */
11322 frame = NULL;
11323
11324 /* If the user told us to continue until a specified location, we
11325 don't specify a frame at which we need to stop. Otherwise,
11326 specify the selected frame, because we want to stop only at the
11327 very same frame. */
11328 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
11329
11330 for (symtab_and_line &sal : sals)
11331 {
11332 resolve_sal_pc (&sal);
11333
11334 breakpoint_up location_breakpoint
11335 = set_momentary_breakpoint (frame_gdbarch, sal,
11336 stop_frame_id, bp_until);
11337 breakpoints.emplace_back (std::move (location_breakpoint));
11338 }
11339
11340 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
11341 std::move (breakpoints));
11342
11343 if (lj_deleter)
11344 lj_deleter->release ();
11345
11346 proceed (-1, GDB_SIGNAL_DEFAULT);
11347 }
11348
11349 /* This function attempts to parse an optional "if <cond>" clause
11350 from the arg string. If one is not found, it returns NULL.
11351
11352 Else, it returns a pointer to the condition string. (It does not
11353 attempt to evaluate the string against a particular block.) And,
11354 it updates arg to point to the first character following the parsed
11355 if clause in the arg string. */
11356
11357 const char *
11358 ep_parse_optional_if_clause (const char **arg)
11359 {
11360 const char *cond_string;
11361
11362 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11363 return NULL;
11364
11365 /* Skip the "if" keyword. */
11366 (*arg) += 2;
11367
11368 /* Skip any extra leading whitespace, and record the start of the
11369 condition string. */
11370 *arg = skip_spaces (*arg);
11371 cond_string = *arg;
11372
11373 /* Assume that the condition occupies the remainder of the arg
11374 string. */
11375 (*arg) += strlen (cond_string);
11376
11377 return cond_string;
11378 }
11379
11380 /* Commands to deal with catching events, such as signals, exceptions,
11381 process start/exit, etc. */
11382
11383 typedef enum
11384 {
11385 catch_fork_temporary, catch_vfork_temporary,
11386 catch_fork_permanent, catch_vfork_permanent
11387 }
11388 catch_fork_kind;
11389
11390 static void
11391 catch_fork_command_1 (const char *arg, int from_tty,
11392 struct cmd_list_element *command)
11393 {
11394 struct gdbarch *gdbarch = get_current_arch ();
11395 const char *cond_string = NULL;
11396 catch_fork_kind fork_kind;
11397
11398 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11399 bool temp = (fork_kind == catch_fork_temporary
11400 || fork_kind == catch_vfork_temporary);
11401
11402 if (!arg)
11403 arg = "";
11404 arg = skip_spaces (arg);
11405
11406 /* The allowed syntax is:
11407 catch [v]fork
11408 catch [v]fork if <cond>
11409
11410 First, check if there's an if clause. */
11411 cond_string = ep_parse_optional_if_clause (&arg);
11412
11413 if ((*arg != '\0') && !isspace (*arg))
11414 error (_("Junk at end of arguments."));
11415
11416 /* If this target supports it, create a fork or vfork catchpoint
11417 and enable reporting of such events. */
11418 switch (fork_kind)
11419 {
11420 case catch_fork_temporary:
11421 case catch_fork_permanent:
11422 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
11423 &catch_fork_breakpoint_ops);
11424 break;
11425 case catch_vfork_temporary:
11426 case catch_vfork_permanent:
11427 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
11428 &catch_vfork_breakpoint_ops);
11429 break;
11430 default:
11431 error (_("unsupported or unknown fork kind; cannot catch it"));
11432 break;
11433 }
11434 }
11435
11436 static void
11437 catch_exec_command_1 (const char *arg, int from_tty,
11438 struct cmd_list_element *command)
11439 {
11440 struct gdbarch *gdbarch = get_current_arch ();
11441 const char *cond_string = NULL;
11442 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
11443
11444 if (!arg)
11445 arg = "";
11446 arg = skip_spaces (arg);
11447
11448 /* The allowed syntax is:
11449 catch exec
11450 catch exec if <cond>
11451
11452 First, check if there's an if clause. */
11453 cond_string = ep_parse_optional_if_clause (&arg);
11454
11455 if ((*arg != '\0') && !isspace (*arg))
11456 error (_("Junk at end of arguments."));
11457
11458 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11459 init_catchpoint (c.get (), gdbarch, temp, cond_string,
11460 &catch_exec_breakpoint_ops);
11461 c->exec_pathname = NULL;
11462
11463 install_breakpoint (0, std::move (c), 1);
11464 }
11465
11466 void
11467 init_ada_exception_breakpoint (struct breakpoint *b,
11468 struct gdbarch *gdbarch,
11469 struct symtab_and_line sal,
11470 const char *addr_string,
11471 const struct breakpoint_ops *ops,
11472 int tempflag,
11473 int enabled,
11474 int from_tty)
11475 {
11476 if (from_tty)
11477 {
11478 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11479 if (!loc_gdbarch)
11480 loc_gdbarch = gdbarch;
11481
11482 describe_other_breakpoints (loc_gdbarch,
11483 sal.pspace, sal.pc, sal.section, -1);
11484 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11485 version for exception catchpoints, because two catchpoints
11486 used for different exception names will use the same address.
11487 In this case, a "breakpoint ... also set at..." warning is
11488 unproductive. Besides, the warning phrasing is also a bit
11489 inappropriate, we should use the word catchpoint, and tell
11490 the user what type of catchpoint it is. The above is good
11491 enough for now, though. */
11492 }
11493
11494 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
11495
11496 b->enable_state = enabled ? bp_enabled : bp_disabled;
11497 b->disposition = tempflag ? disp_del : disp_donttouch;
11498 b->location = string_to_event_location (&addr_string,
11499 language_def (language_ada));
11500 b->language = language_ada;
11501 }
11502
11503 \f
11504
11505 /* Compare two breakpoints and return a strcmp-like result. */
11506
11507 static int
11508 compare_breakpoints (const breakpoint *a, const breakpoint *b)
11509 {
11510 uintptr_t ua = (uintptr_t) a;
11511 uintptr_t ub = (uintptr_t) b;
11512
11513 if (a->number < b->number)
11514 return -1;
11515 else if (a->number > b->number)
11516 return 1;
11517
11518 /* Now sort by address, in case we see, e..g, two breakpoints with
11519 the number 0. */
11520 if (ua < ub)
11521 return -1;
11522 return ua > ub ? 1 : 0;
11523 }
11524
11525 /* Delete breakpoints by address or line. */
11526
11527 static void
11528 clear_command (const char *arg, int from_tty)
11529 {
11530 struct breakpoint *b;
11531 int default_match;
11532
11533 std::vector<symtab_and_line> decoded_sals;
11534 symtab_and_line last_sal;
11535 gdb::array_view<symtab_and_line> sals;
11536 if (arg)
11537 {
11538 decoded_sals
11539 = decode_line_with_current_source (arg,
11540 (DECODE_LINE_FUNFIRSTLINE
11541 | DECODE_LINE_LIST_MODE));
11542 default_match = 0;
11543 sals = decoded_sals;
11544 }
11545 else
11546 {
11547 /* Set sal's line, symtab, pc, and pspace to the values
11548 corresponding to the last call to print_frame_info. If the
11549 codepoint is not valid, this will set all the fields to 0. */
11550 last_sal = get_last_displayed_sal ();
11551 if (last_sal.symtab == 0)
11552 error (_("No source file specified."));
11553
11554 default_match = 1;
11555 sals = last_sal;
11556 }
11557
11558 /* We don't call resolve_sal_pc here. That's not as bad as it
11559 seems, because all existing breakpoints typically have both
11560 file/line and pc set. So, if clear is given file/line, we can
11561 match this to existing breakpoint without obtaining pc at all.
11562
11563 We only support clearing given the address explicitly
11564 present in breakpoint table. Say, we've set breakpoint
11565 at file:line. There were several PC values for that file:line,
11566 due to optimization, all in one block.
11567
11568 We've picked one PC value. If "clear" is issued with another
11569 PC corresponding to the same file:line, the breakpoint won't
11570 be cleared. We probably can still clear the breakpoint, but
11571 since the other PC value is never presented to user, user
11572 can only find it by guessing, and it does not seem important
11573 to support that. */
11574
11575 /* For each line spec given, delete bps which correspond to it. Do
11576 it in two passes, solely to preserve the current behavior that
11577 from_tty is forced true if we delete more than one
11578 breakpoint. */
11579
11580 std::vector<struct breakpoint *> found;
11581 for (const auto &sal : sals)
11582 {
11583 const char *sal_fullname;
11584
11585 /* If exact pc given, clear bpts at that pc.
11586 If line given (pc == 0), clear all bpts on specified line.
11587 If defaulting, clear all bpts on default line
11588 or at default pc.
11589
11590 defaulting sal.pc != 0 tests to do
11591
11592 0 1 pc
11593 1 1 pc _and_ line
11594 0 0 line
11595 1 0 <can't happen> */
11596
11597 sal_fullname = (sal.symtab == NULL
11598 ? NULL : symtab_to_fullname (sal.symtab));
11599
11600 /* Find all matching breakpoints and add them to 'found'. */
11601 ALL_BREAKPOINTS (b)
11602 {
11603 int match = 0;
11604 /* Are we going to delete b? */
11605 if (b->type != bp_none && !is_watchpoint (b))
11606 {
11607 struct bp_location *loc = b->loc;
11608 for (; loc; loc = loc->next)
11609 {
11610 /* If the user specified file:line, don't allow a PC
11611 match. This matches historical gdb behavior. */
11612 int pc_match = (!sal.explicit_line
11613 && sal.pc
11614 && (loc->pspace == sal.pspace)
11615 && (loc->address == sal.pc)
11616 && (!section_is_overlay (loc->section)
11617 || loc->section == sal.section));
11618 int line_match = 0;
11619
11620 if ((default_match || sal.explicit_line)
11621 && loc->symtab != NULL
11622 && sal_fullname != NULL
11623 && sal.pspace == loc->pspace
11624 && loc->line_number == sal.line
11625 && filename_cmp (symtab_to_fullname (loc->symtab),
11626 sal_fullname) == 0)
11627 line_match = 1;
11628
11629 if (pc_match || line_match)
11630 {
11631 match = 1;
11632 break;
11633 }
11634 }
11635 }
11636
11637 if (match)
11638 found.push_back (b);
11639 }
11640 }
11641
11642 /* Now go thru the 'found' chain and delete them. */
11643 if (found.empty ())
11644 {
11645 if (arg)
11646 error (_("No breakpoint at %s."), arg);
11647 else
11648 error (_("No breakpoint at this line."));
11649 }
11650
11651 /* Remove duplicates from the vec. */
11652 std::sort (found.begin (), found.end (),
11653 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11654 {
11655 return compare_breakpoints (bp_a, bp_b) < 0;
11656 });
11657 found.erase (std::unique (found.begin (), found.end (),
11658 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11659 {
11660 return compare_breakpoints (bp_a, bp_b) == 0;
11661 }),
11662 found.end ());
11663
11664 if (found.size () > 1)
11665 from_tty = 1; /* Always report if deleted more than one. */
11666 if (from_tty)
11667 {
11668 if (found.size () == 1)
11669 printf_unfiltered (_("Deleted breakpoint "));
11670 else
11671 printf_unfiltered (_("Deleted breakpoints "));
11672 }
11673
11674 for (breakpoint *iter : found)
11675 {
11676 if (from_tty)
11677 printf_unfiltered ("%d ", iter->number);
11678 delete_breakpoint (iter);
11679 }
11680 if (from_tty)
11681 putchar_unfiltered ('\n');
11682 }
11683 \f
11684 /* Delete breakpoint in BS if they are `delete' breakpoints and
11685 all breakpoints that are marked for deletion, whether hit or not.
11686 This is called after any breakpoint is hit, or after errors. */
11687
11688 void
11689 breakpoint_auto_delete (bpstat bs)
11690 {
11691 struct breakpoint *b, *b_tmp;
11692
11693 for (; bs; bs = bs->next)
11694 if (bs->breakpoint_at
11695 && bs->breakpoint_at->disposition == disp_del
11696 && bs->stop)
11697 delete_breakpoint (bs->breakpoint_at);
11698
11699 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11700 {
11701 if (b->disposition == disp_del_at_next_stop)
11702 delete_breakpoint (b);
11703 }
11704 }
11705
11706 /* A comparison function for bp_location AP and BP being interfaced to
11707 std::sort. Sort elements primarily by their ADDRESS (no matter what
11708 bl_address_is_meaningful says), secondarily by ordering first
11709 permanent elements and terciarily just ensuring the array is sorted
11710 stable way despite std::sort being an unstable algorithm. */
11711
11712 static int
11713 bp_location_is_less_than (const bp_location *a, const bp_location *b)
11714 {
11715 if (a->address != b->address)
11716 return a->address < b->address;
11717
11718 /* Sort locations at the same address by their pspace number, keeping
11719 locations of the same inferior (in a multi-inferior environment)
11720 grouped. */
11721
11722 if (a->pspace->num != b->pspace->num)
11723 return a->pspace->num < b->pspace->num;
11724
11725 /* Sort permanent breakpoints first. */
11726 if (a->permanent != b->permanent)
11727 return a->permanent > b->permanent;
11728
11729 /* Sort by type in order to make duplicate determination easier.
11730 See update_global_location_list. This is kept in sync with
11731 breakpoint_locations_match. */
11732 if (a->loc_type < b->loc_type)
11733 return true;
11734
11735 /* Likewise, for range-breakpoints, sort by length. */
11736 if (a->loc_type == bp_loc_hardware_breakpoint
11737 && b->loc_type == bp_loc_hardware_breakpoint
11738 && a->length < b->length)
11739 return true;
11740
11741 /* Make the internal GDB representation stable across GDB runs
11742 where A and B memory inside GDB can differ. Breakpoint locations of
11743 the same type at the same address can be sorted in arbitrary order. */
11744
11745 if (a->owner->number != b->owner->number)
11746 return a->owner->number < b->owner->number;
11747
11748 return a < b;
11749 }
11750
11751 /* Set bp_locations_placed_address_before_address_max and
11752 bp_locations_shadow_len_after_address_max according to the current
11753 content of the bp_locations array. */
11754
11755 static void
11756 bp_locations_target_extensions_update (void)
11757 {
11758 struct bp_location *bl, **blp_tmp;
11759
11760 bp_locations_placed_address_before_address_max = 0;
11761 bp_locations_shadow_len_after_address_max = 0;
11762
11763 ALL_BP_LOCATIONS (bl, blp_tmp)
11764 {
11765 CORE_ADDR start, end, addr;
11766
11767 if (!bp_location_has_shadow (bl))
11768 continue;
11769
11770 start = bl->target_info.placed_address;
11771 end = start + bl->target_info.shadow_len;
11772
11773 gdb_assert (bl->address >= start);
11774 addr = bl->address - start;
11775 if (addr > bp_locations_placed_address_before_address_max)
11776 bp_locations_placed_address_before_address_max = addr;
11777
11778 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11779
11780 gdb_assert (bl->address < end);
11781 addr = end - bl->address;
11782 if (addr > bp_locations_shadow_len_after_address_max)
11783 bp_locations_shadow_len_after_address_max = addr;
11784 }
11785 }
11786
11787 /* Download tracepoint locations if they haven't been. */
11788
11789 static void
11790 download_tracepoint_locations (void)
11791 {
11792 struct breakpoint *b;
11793 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11794
11795 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11796
11797 ALL_TRACEPOINTS (b)
11798 {
11799 struct bp_location *bl;
11800 struct tracepoint *t;
11801 int bp_location_downloaded = 0;
11802
11803 if ((b->type == bp_fast_tracepoint
11804 ? !may_insert_fast_tracepoints
11805 : !may_insert_tracepoints))
11806 continue;
11807
11808 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11809 {
11810 if (target_can_download_tracepoint ())
11811 can_download_tracepoint = TRIBOOL_TRUE;
11812 else
11813 can_download_tracepoint = TRIBOOL_FALSE;
11814 }
11815
11816 if (can_download_tracepoint == TRIBOOL_FALSE)
11817 break;
11818
11819 for (bl = b->loc; bl; bl = bl->next)
11820 {
11821 /* In tracepoint, locations are _never_ duplicated, so
11822 should_be_inserted is equivalent to
11823 unduplicated_should_be_inserted. */
11824 if (!should_be_inserted (bl) || bl->inserted)
11825 continue;
11826
11827 switch_to_program_space_and_thread (bl->pspace);
11828
11829 target_download_tracepoint (bl);
11830
11831 bl->inserted = 1;
11832 bp_location_downloaded = 1;
11833 }
11834 t = (struct tracepoint *) b;
11835 t->number_on_target = b->number;
11836 if (bp_location_downloaded)
11837 gdb::observers::breakpoint_modified.notify (b);
11838 }
11839 }
11840
11841 /* Swap the insertion/duplication state between two locations. */
11842
11843 static void
11844 swap_insertion (struct bp_location *left, struct bp_location *right)
11845 {
11846 const int left_inserted = left->inserted;
11847 const int left_duplicate = left->duplicate;
11848 const int left_needs_update = left->needs_update;
11849 const struct bp_target_info left_target_info = left->target_info;
11850
11851 /* Locations of tracepoints can never be duplicated. */
11852 if (is_tracepoint (left->owner))
11853 gdb_assert (!left->duplicate);
11854 if (is_tracepoint (right->owner))
11855 gdb_assert (!right->duplicate);
11856
11857 left->inserted = right->inserted;
11858 left->duplicate = right->duplicate;
11859 left->needs_update = right->needs_update;
11860 left->target_info = right->target_info;
11861 right->inserted = left_inserted;
11862 right->duplicate = left_duplicate;
11863 right->needs_update = left_needs_update;
11864 right->target_info = left_target_info;
11865 }
11866
11867 /* Force the re-insertion of the locations at ADDRESS. This is called
11868 once a new/deleted/modified duplicate location is found and we are evaluating
11869 conditions on the target's side. Such conditions need to be updated on
11870 the target. */
11871
11872 static void
11873 force_breakpoint_reinsertion (struct bp_location *bl)
11874 {
11875 struct bp_location **locp = NULL, **loc2p;
11876 struct bp_location *loc;
11877 CORE_ADDR address = 0;
11878 int pspace_num;
11879
11880 address = bl->address;
11881 pspace_num = bl->pspace->num;
11882
11883 /* This is only meaningful if the target is
11884 evaluating conditions and if the user has
11885 opted for condition evaluation on the target's
11886 side. */
11887 if (gdb_evaluates_breakpoint_condition_p ()
11888 || !target_supports_evaluation_of_breakpoint_conditions ())
11889 return;
11890
11891 /* Flag all breakpoint locations with this address and
11892 the same program space as the location
11893 as "its condition has changed". We need to
11894 update the conditions on the target's side. */
11895 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11896 {
11897 loc = *loc2p;
11898
11899 if (!is_breakpoint (loc->owner)
11900 || pspace_num != loc->pspace->num)
11901 continue;
11902
11903 /* Flag the location appropriately. We use a different state to
11904 let everyone know that we already updated the set of locations
11905 with addr bl->address and program space bl->pspace. This is so
11906 we don't have to keep calling these functions just to mark locations
11907 that have already been marked. */
11908 loc->condition_changed = condition_updated;
11909
11910 /* Free the agent expression bytecode as well. We will compute
11911 it later on. */
11912 loc->cond_bytecode.reset ();
11913 }
11914 }
11915
11916 /* Called whether new breakpoints are created, or existing breakpoints
11917 deleted, to update the global location list and recompute which
11918 locations are duplicate of which.
11919
11920 The INSERT_MODE flag determines whether locations may not, may, or
11921 shall be inserted now. See 'enum ugll_insert_mode' for more
11922 info. */
11923
11924 static void
11925 update_global_location_list (enum ugll_insert_mode insert_mode)
11926 {
11927 struct breakpoint *b;
11928 struct bp_location **locp, *loc;
11929 /* Last breakpoint location address that was marked for update. */
11930 CORE_ADDR last_addr = 0;
11931 /* Last breakpoint location program space that was marked for update. */
11932 int last_pspace_num = -1;
11933
11934 /* Used in the duplicates detection below. When iterating over all
11935 bp_locations, points to the first bp_location of a given address.
11936 Breakpoints and watchpoints of different types are never
11937 duplicates of each other. Keep one pointer for each type of
11938 breakpoint/watchpoint, so we only need to loop over all locations
11939 once. */
11940 struct bp_location *bp_loc_first; /* breakpoint */
11941 struct bp_location *wp_loc_first; /* hardware watchpoint */
11942 struct bp_location *awp_loc_first; /* access watchpoint */
11943 struct bp_location *rwp_loc_first; /* read watchpoint */
11944
11945 /* Saved former bp_locations array which we compare against the newly
11946 built bp_locations from the current state of ALL_BREAKPOINTS. */
11947 struct bp_location **old_locp;
11948 unsigned old_locations_count;
11949 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
11950
11951 old_locations_count = bp_locations_count;
11952 bp_locations = NULL;
11953 bp_locations_count = 0;
11954
11955 ALL_BREAKPOINTS (b)
11956 for (loc = b->loc; loc; loc = loc->next)
11957 bp_locations_count++;
11958
11959 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11960 locp = bp_locations;
11961 ALL_BREAKPOINTS (b)
11962 for (loc = b->loc; loc; loc = loc->next)
11963 *locp++ = loc;
11964
11965 /* See if we need to "upgrade" a software breakpoint to a hardware
11966 breakpoint. Do this before deciding whether locations are
11967 duplicates. Also do this before sorting because sorting order
11968 depends on location type. */
11969 for (locp = bp_locations;
11970 locp < bp_locations + bp_locations_count;
11971 locp++)
11972 {
11973 loc = *locp;
11974 if (!loc->inserted && should_be_inserted (loc))
11975 handle_automatic_hardware_breakpoints (loc);
11976 }
11977
11978 std::sort (bp_locations, bp_locations + bp_locations_count,
11979 bp_location_is_less_than);
11980
11981 bp_locations_target_extensions_update ();
11982
11983 /* Identify bp_location instances that are no longer present in the
11984 new list, and therefore should be freed. Note that it's not
11985 necessary that those locations should be removed from inferior --
11986 if there's another location at the same address (previously
11987 marked as duplicate), we don't need to remove/insert the
11988 location.
11989
11990 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11991 and former bp_location array state respectively. */
11992
11993 locp = bp_locations;
11994 for (old_locp = old_locations.get ();
11995 old_locp < old_locations.get () + old_locations_count;
11996 old_locp++)
11997 {
11998 struct bp_location *old_loc = *old_locp;
11999 struct bp_location **loc2p;
12000
12001 /* Tells if 'old_loc' is found among the new locations. If
12002 not, we have to free it. */
12003 int found_object = 0;
12004 /* Tells if the location should remain inserted in the target. */
12005 int keep_in_target = 0;
12006 int removed = 0;
12007
12008 /* Skip LOCP entries which will definitely never be needed.
12009 Stop either at or being the one matching OLD_LOC. */
12010 while (locp < bp_locations + bp_locations_count
12011 && (*locp)->address < old_loc->address)
12012 locp++;
12013
12014 for (loc2p = locp;
12015 (loc2p < bp_locations + bp_locations_count
12016 && (*loc2p)->address == old_loc->address);
12017 loc2p++)
12018 {
12019 /* Check if this is a new/duplicated location or a duplicated
12020 location that had its condition modified. If so, we want to send
12021 its condition to the target if evaluation of conditions is taking
12022 place there. */
12023 if ((*loc2p)->condition_changed == condition_modified
12024 && (last_addr != old_loc->address
12025 || last_pspace_num != old_loc->pspace->num))
12026 {
12027 force_breakpoint_reinsertion (*loc2p);
12028 last_pspace_num = old_loc->pspace->num;
12029 }
12030
12031 if (*loc2p == old_loc)
12032 found_object = 1;
12033 }
12034
12035 /* We have already handled this address, update it so that we don't
12036 have to go through updates again. */
12037 last_addr = old_loc->address;
12038
12039 /* Target-side condition evaluation: Handle deleted locations. */
12040 if (!found_object)
12041 force_breakpoint_reinsertion (old_loc);
12042
12043 /* If this location is no longer present, and inserted, look if
12044 there's maybe a new location at the same address. If so,
12045 mark that one inserted, and don't remove this one. This is
12046 needed so that we don't have a time window where a breakpoint
12047 at certain location is not inserted. */
12048
12049 if (old_loc->inserted)
12050 {
12051 /* If the location is inserted now, we might have to remove
12052 it. */
12053
12054 if (found_object && should_be_inserted (old_loc))
12055 {
12056 /* The location is still present in the location list,
12057 and still should be inserted. Don't do anything. */
12058 keep_in_target = 1;
12059 }
12060 else
12061 {
12062 /* This location still exists, but it won't be kept in the
12063 target since it may have been disabled. We proceed to
12064 remove its target-side condition. */
12065
12066 /* The location is either no longer present, or got
12067 disabled. See if there's another location at the
12068 same address, in which case we don't need to remove
12069 this one from the target. */
12070
12071 /* OLD_LOC comes from existing struct breakpoint. */
12072 if (bl_address_is_meaningful (old_loc))
12073 {
12074 for (loc2p = locp;
12075 (loc2p < bp_locations + bp_locations_count
12076 && (*loc2p)->address == old_loc->address);
12077 loc2p++)
12078 {
12079 struct bp_location *loc2 = *loc2p;
12080
12081 if (loc2 == old_loc)
12082 continue;
12083
12084 if (breakpoint_locations_match (loc2, old_loc))
12085 {
12086 /* Read watchpoint locations are switched to
12087 access watchpoints, if the former are not
12088 supported, but the latter are. */
12089 if (is_hardware_watchpoint (old_loc->owner))
12090 {
12091 gdb_assert (is_hardware_watchpoint (loc2->owner));
12092 loc2->watchpoint_type = old_loc->watchpoint_type;
12093 }
12094
12095 /* loc2 is a duplicated location. We need to check
12096 if it should be inserted in case it will be
12097 unduplicated. */
12098 if (unduplicated_should_be_inserted (loc2))
12099 {
12100 swap_insertion (old_loc, loc2);
12101 keep_in_target = 1;
12102 break;
12103 }
12104 }
12105 }
12106 }
12107 }
12108
12109 if (!keep_in_target)
12110 {
12111 if (remove_breakpoint (old_loc))
12112 {
12113 /* This is just about all we can do. We could keep
12114 this location on the global list, and try to
12115 remove it next time, but there's no particular
12116 reason why we will succeed next time.
12117
12118 Note that at this point, old_loc->owner is still
12119 valid, as delete_breakpoint frees the breakpoint
12120 only after calling us. */
12121 printf_filtered (_("warning: Error removing "
12122 "breakpoint %d\n"),
12123 old_loc->owner->number);
12124 }
12125 removed = 1;
12126 }
12127 }
12128
12129 if (!found_object)
12130 {
12131 if (removed && target_is_non_stop_p ()
12132 && need_moribund_for_location_type (old_loc))
12133 {
12134 /* This location was removed from the target. In
12135 non-stop mode, a race condition is possible where
12136 we've removed a breakpoint, but stop events for that
12137 breakpoint are already queued and will arrive later.
12138 We apply an heuristic to be able to distinguish such
12139 SIGTRAPs from other random SIGTRAPs: we keep this
12140 breakpoint location for a bit, and will retire it
12141 after we see some number of events. The theory here
12142 is that reporting of events should, "on the average",
12143 be fair, so after a while we'll see events from all
12144 threads that have anything of interest, and no longer
12145 need to keep this breakpoint location around. We
12146 don't hold locations forever so to reduce chances of
12147 mistaking a non-breakpoint SIGTRAP for a breakpoint
12148 SIGTRAP.
12149
12150 The heuristic failing can be disastrous on
12151 decr_pc_after_break targets.
12152
12153 On decr_pc_after_break targets, like e.g., x86-linux,
12154 if we fail to recognize a late breakpoint SIGTRAP,
12155 because events_till_retirement has reached 0 too
12156 soon, we'll fail to do the PC adjustment, and report
12157 a random SIGTRAP to the user. When the user resumes
12158 the inferior, it will most likely immediately crash
12159 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12160 corrupted, because of being resumed e.g., in the
12161 middle of a multi-byte instruction, or skipped a
12162 one-byte instruction. This was actually seen happen
12163 on native x86-linux, and should be less rare on
12164 targets that do not support new thread events, like
12165 remote, due to the heuristic depending on
12166 thread_count.
12167
12168 Mistaking a random SIGTRAP for a breakpoint trap
12169 causes similar symptoms (PC adjustment applied when
12170 it shouldn't), but then again, playing with SIGTRAPs
12171 behind the debugger's back is asking for trouble.
12172
12173 Since hardware watchpoint traps are always
12174 distinguishable from other traps, so we don't need to
12175 apply keep hardware watchpoint moribund locations
12176 around. We simply always ignore hardware watchpoint
12177 traps we can no longer explain. */
12178
12179 process_stratum_target *proc_target = nullptr;
12180 for (inferior *inf : all_inferiors ())
12181 if (inf->pspace == old_loc->pspace)
12182 {
12183 proc_target = inf->process_target ();
12184 break;
12185 }
12186 if (proc_target != nullptr)
12187 old_loc->events_till_retirement
12188 = 3 * (thread_count (proc_target) + 1);
12189 else
12190 old_loc->events_till_retirement = 1;
12191 old_loc->owner = NULL;
12192
12193 moribund_locations.push_back (old_loc);
12194 }
12195 else
12196 {
12197 old_loc->owner = NULL;
12198 decref_bp_location (&old_loc);
12199 }
12200 }
12201 }
12202
12203 /* Rescan breakpoints at the same address and section, marking the
12204 first one as "first" and any others as "duplicates". This is so
12205 that the bpt instruction is only inserted once. If we have a
12206 permanent breakpoint at the same place as BPT, make that one the
12207 official one, and the rest as duplicates. Permanent breakpoints
12208 are sorted first for the same address.
12209
12210 Do the same for hardware watchpoints, but also considering the
12211 watchpoint's type (regular/access/read) and length. */
12212
12213 bp_loc_first = NULL;
12214 wp_loc_first = NULL;
12215 awp_loc_first = NULL;
12216 rwp_loc_first = NULL;
12217 ALL_BP_LOCATIONS (loc, locp)
12218 {
12219 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12220 non-NULL. */
12221 struct bp_location **loc_first_p;
12222 b = loc->owner;
12223
12224 if (!unduplicated_should_be_inserted (loc)
12225 || !bl_address_is_meaningful (loc)
12226 /* Don't detect duplicate for tracepoint locations because they are
12227 never duplicated. See the comments in field `duplicate' of
12228 `struct bp_location'. */
12229 || is_tracepoint (b))
12230 {
12231 /* Clear the condition modification flag. */
12232 loc->condition_changed = condition_unchanged;
12233 continue;
12234 }
12235
12236 if (b->type == bp_hardware_watchpoint)
12237 loc_first_p = &wp_loc_first;
12238 else if (b->type == bp_read_watchpoint)
12239 loc_first_p = &rwp_loc_first;
12240 else if (b->type == bp_access_watchpoint)
12241 loc_first_p = &awp_loc_first;
12242 else
12243 loc_first_p = &bp_loc_first;
12244
12245 if (*loc_first_p == NULL
12246 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12247 || !breakpoint_locations_match (loc, *loc_first_p))
12248 {
12249 *loc_first_p = loc;
12250 loc->duplicate = 0;
12251
12252 if (is_breakpoint (loc->owner) && loc->condition_changed)
12253 {
12254 loc->needs_update = 1;
12255 /* Clear the condition modification flag. */
12256 loc->condition_changed = condition_unchanged;
12257 }
12258 continue;
12259 }
12260
12261
12262 /* This and the above ensure the invariant that the first location
12263 is not duplicated, and is the inserted one.
12264 All following are marked as duplicated, and are not inserted. */
12265 if (loc->inserted)
12266 swap_insertion (loc, *loc_first_p);
12267 loc->duplicate = 1;
12268
12269 /* Clear the condition modification flag. */
12270 loc->condition_changed = condition_unchanged;
12271 }
12272
12273 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12274 {
12275 if (insert_mode != UGLL_DONT_INSERT)
12276 insert_breakpoint_locations ();
12277 else
12278 {
12279 /* Even though the caller told us to not insert new
12280 locations, we may still need to update conditions on the
12281 target's side of breakpoints that were already inserted
12282 if the target is evaluating breakpoint conditions. We
12283 only update conditions for locations that are marked
12284 "needs_update". */
12285 update_inserted_breakpoint_locations ();
12286 }
12287 }
12288
12289 if (insert_mode != UGLL_DONT_INSERT)
12290 download_tracepoint_locations ();
12291 }
12292
12293 void
12294 breakpoint_retire_moribund (void)
12295 {
12296 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12297 {
12298 struct bp_location *loc = moribund_locations[ix];
12299 if (--(loc->events_till_retirement) == 0)
12300 {
12301 decref_bp_location (&loc);
12302 unordered_remove (moribund_locations, ix);
12303 --ix;
12304 }
12305 }
12306 }
12307
12308 static void
12309 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12310 {
12311
12312 try
12313 {
12314 update_global_location_list (insert_mode);
12315 }
12316 catch (const gdb_exception_error &e)
12317 {
12318 }
12319 }
12320
12321 /* Clear BKP from a BPS. */
12322
12323 static void
12324 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12325 {
12326 bpstat bs;
12327
12328 for (bs = bps; bs; bs = bs->next)
12329 if (bs->breakpoint_at == bpt)
12330 {
12331 bs->breakpoint_at = NULL;
12332 bs->old_val = NULL;
12333 /* bs->commands will be freed later. */
12334 }
12335 }
12336
12337 /* Callback for iterate_over_threads. */
12338 static int
12339 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12340 {
12341 struct breakpoint *bpt = (struct breakpoint *) data;
12342
12343 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12344 return 0;
12345 }
12346
12347 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12348 callbacks. */
12349
12350 static void
12351 say_where (struct breakpoint *b)
12352 {
12353 struct value_print_options opts;
12354
12355 get_user_print_options (&opts);
12356
12357 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12358 single string. */
12359 if (b->loc == NULL)
12360 {
12361 /* For pending locations, the output differs slightly based
12362 on b->extra_string. If this is non-NULL, it contains either
12363 a condition or dprintf arguments. */
12364 if (b->extra_string == NULL)
12365 {
12366 printf_filtered (_(" (%s) pending."),
12367 event_location_to_string (b->location.get ()));
12368 }
12369 else if (b->type == bp_dprintf)
12370 {
12371 printf_filtered (_(" (%s,%s) pending."),
12372 event_location_to_string (b->location.get ()),
12373 b->extra_string);
12374 }
12375 else
12376 {
12377 printf_filtered (_(" (%s %s) pending."),
12378 event_location_to_string (b->location.get ()),
12379 b->extra_string);
12380 }
12381 }
12382 else
12383 {
12384 if (opts.addressprint || b->loc->symtab == NULL)
12385 printf_filtered (" at %ps",
12386 styled_string (address_style.style (),
12387 paddress (b->loc->gdbarch,
12388 b->loc->address)));
12389 if (b->loc->symtab != NULL)
12390 {
12391 /* If there is a single location, we can print the location
12392 more nicely. */
12393 if (b->loc->next == NULL)
12394 {
12395 const char *filename
12396 = symtab_to_filename_for_display (b->loc->symtab);
12397 printf_filtered (": file %ps, line %d.",
12398 styled_string (file_name_style.style (),
12399 filename),
12400 b->loc->line_number);
12401 }
12402 else
12403 /* This is not ideal, but each location may have a
12404 different file name, and this at least reflects the
12405 real situation somewhat. */
12406 printf_filtered (": %s.",
12407 event_location_to_string (b->location.get ()));
12408 }
12409
12410 if (b->loc->next)
12411 {
12412 struct bp_location *loc = b->loc;
12413 int n = 0;
12414 for (; loc; loc = loc->next)
12415 ++n;
12416 printf_filtered (" (%d locations)", n);
12417 }
12418 }
12419 }
12420
12421 bp_location::~bp_location ()
12422 {
12423 xfree (function_name);
12424 }
12425
12426 /* Destructor for the breakpoint base class. */
12427
12428 breakpoint::~breakpoint ()
12429 {
12430 xfree (this->cond_string);
12431 xfree (this->extra_string);
12432 }
12433
12434 static struct bp_location *
12435 base_breakpoint_allocate_location (struct breakpoint *self)
12436 {
12437 return new bp_location (self);
12438 }
12439
12440 static void
12441 base_breakpoint_re_set (struct breakpoint *b)
12442 {
12443 /* Nothing to re-set. */
12444 }
12445
12446 #define internal_error_pure_virtual_called() \
12447 gdb_assert_not_reached ("pure virtual function called")
12448
12449 static int
12450 base_breakpoint_insert_location (struct bp_location *bl)
12451 {
12452 internal_error_pure_virtual_called ();
12453 }
12454
12455 static int
12456 base_breakpoint_remove_location (struct bp_location *bl,
12457 enum remove_bp_reason reason)
12458 {
12459 internal_error_pure_virtual_called ();
12460 }
12461
12462 static int
12463 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12464 const address_space *aspace,
12465 CORE_ADDR bp_addr,
12466 const struct target_waitstatus *ws)
12467 {
12468 internal_error_pure_virtual_called ();
12469 }
12470
12471 static void
12472 base_breakpoint_check_status (bpstat bs)
12473 {
12474 /* Always stop. */
12475 }
12476
12477 /* A "works_in_software_mode" breakpoint_ops method that just internal
12478 errors. */
12479
12480 static int
12481 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12482 {
12483 internal_error_pure_virtual_called ();
12484 }
12485
12486 /* A "resources_needed" breakpoint_ops method that just internal
12487 errors. */
12488
12489 static int
12490 base_breakpoint_resources_needed (const struct bp_location *bl)
12491 {
12492 internal_error_pure_virtual_called ();
12493 }
12494
12495 static enum print_stop_action
12496 base_breakpoint_print_it (bpstat bs)
12497 {
12498 internal_error_pure_virtual_called ();
12499 }
12500
12501 static void
12502 base_breakpoint_print_one_detail (const struct breakpoint *self,
12503 struct ui_out *uiout)
12504 {
12505 /* nothing */
12506 }
12507
12508 static void
12509 base_breakpoint_print_mention (struct breakpoint *b)
12510 {
12511 internal_error_pure_virtual_called ();
12512 }
12513
12514 static void
12515 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12516 {
12517 internal_error_pure_virtual_called ();
12518 }
12519
12520 static void
12521 base_breakpoint_create_sals_from_location
12522 (struct event_location *location,
12523 struct linespec_result *canonical,
12524 enum bptype type_wanted)
12525 {
12526 internal_error_pure_virtual_called ();
12527 }
12528
12529 static void
12530 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12531 struct linespec_result *c,
12532 gdb::unique_xmalloc_ptr<char> cond_string,
12533 gdb::unique_xmalloc_ptr<char> extra_string,
12534 enum bptype type_wanted,
12535 enum bpdisp disposition,
12536 int thread,
12537 int task, int ignore_count,
12538 const struct breakpoint_ops *o,
12539 int from_tty, int enabled,
12540 int internal, unsigned flags)
12541 {
12542 internal_error_pure_virtual_called ();
12543 }
12544
12545 static std::vector<symtab_and_line>
12546 base_breakpoint_decode_location (struct breakpoint *b,
12547 struct event_location *location,
12548 struct program_space *search_pspace)
12549 {
12550 internal_error_pure_virtual_called ();
12551 }
12552
12553 /* The default 'explains_signal' method. */
12554
12555 static int
12556 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12557 {
12558 return 1;
12559 }
12560
12561 /* The default "after_condition_true" method. */
12562
12563 static void
12564 base_breakpoint_after_condition_true (struct bpstats *bs)
12565 {
12566 /* Nothing to do. */
12567 }
12568
12569 struct breakpoint_ops base_breakpoint_ops =
12570 {
12571 base_breakpoint_allocate_location,
12572 base_breakpoint_re_set,
12573 base_breakpoint_insert_location,
12574 base_breakpoint_remove_location,
12575 base_breakpoint_breakpoint_hit,
12576 base_breakpoint_check_status,
12577 base_breakpoint_resources_needed,
12578 base_breakpoint_works_in_software_mode,
12579 base_breakpoint_print_it,
12580 NULL,
12581 base_breakpoint_print_one_detail,
12582 base_breakpoint_print_mention,
12583 base_breakpoint_print_recreate,
12584 base_breakpoint_create_sals_from_location,
12585 base_breakpoint_create_breakpoints_sal,
12586 base_breakpoint_decode_location,
12587 base_breakpoint_explains_signal,
12588 base_breakpoint_after_condition_true,
12589 };
12590
12591 /* Default breakpoint_ops methods. */
12592
12593 static void
12594 bkpt_re_set (struct breakpoint *b)
12595 {
12596 /* FIXME: is this still reachable? */
12597 if (breakpoint_event_location_empty_p (b))
12598 {
12599 /* Anything without a location can't be re-set. */
12600 delete_breakpoint (b);
12601 return;
12602 }
12603
12604 breakpoint_re_set_default (b);
12605 }
12606
12607 static int
12608 bkpt_insert_location (struct bp_location *bl)
12609 {
12610 CORE_ADDR addr = bl->target_info.reqstd_address;
12611
12612 bl->target_info.kind = breakpoint_kind (bl, &addr);
12613 bl->target_info.placed_address = addr;
12614
12615 if (bl->loc_type == bp_loc_hardware_breakpoint)
12616 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12617 else
12618 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12619 }
12620
12621 static int
12622 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12623 {
12624 if (bl->loc_type == bp_loc_hardware_breakpoint)
12625 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12626 else
12627 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12628 }
12629
12630 static int
12631 bkpt_breakpoint_hit (const struct bp_location *bl,
12632 const address_space *aspace, CORE_ADDR bp_addr,
12633 const struct target_waitstatus *ws)
12634 {
12635 if (ws->kind != TARGET_WAITKIND_STOPPED
12636 || ws->value.sig != GDB_SIGNAL_TRAP)
12637 return 0;
12638
12639 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12640 aspace, bp_addr))
12641 return 0;
12642
12643 if (overlay_debugging /* unmapped overlay section */
12644 && section_is_overlay (bl->section)
12645 && !section_is_mapped (bl->section))
12646 return 0;
12647
12648 return 1;
12649 }
12650
12651 static int
12652 dprintf_breakpoint_hit (const struct bp_location *bl,
12653 const address_space *aspace, CORE_ADDR bp_addr,
12654 const struct target_waitstatus *ws)
12655 {
12656 if (dprintf_style == dprintf_style_agent
12657 && target_can_run_breakpoint_commands ())
12658 {
12659 /* An agent-style dprintf never causes a stop. If we see a trap
12660 for this address it must be for a breakpoint that happens to
12661 be set at the same address. */
12662 return 0;
12663 }
12664
12665 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12666 }
12667
12668 static int
12669 bkpt_resources_needed (const struct bp_location *bl)
12670 {
12671 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12672
12673 return 1;
12674 }
12675
12676 static enum print_stop_action
12677 bkpt_print_it (bpstat bs)
12678 {
12679 struct breakpoint *b;
12680 const struct bp_location *bl;
12681 int bp_temp;
12682 struct ui_out *uiout = current_uiout;
12683
12684 gdb_assert (bs->bp_location_at != NULL);
12685
12686 bl = bs->bp_location_at.get ();
12687 b = bs->breakpoint_at;
12688
12689 bp_temp = b->disposition == disp_del;
12690 if (bl->address != bl->requested_address)
12691 breakpoint_adjustment_warning (bl->requested_address,
12692 bl->address,
12693 b->number, 1);
12694 annotate_breakpoint (b->number);
12695 maybe_print_thread_hit_breakpoint (uiout);
12696
12697 if (uiout->is_mi_like_p ())
12698 {
12699 uiout->field_string ("reason",
12700 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12701 uiout->field_string ("disp", bpdisp_text (b->disposition));
12702 }
12703 if (bp_temp)
12704 uiout->message ("Temporary breakpoint %pF, ",
12705 signed_field ("bkptno", b->number));
12706 else
12707 uiout->message ("Breakpoint %pF, ",
12708 signed_field ("bkptno", b->number));
12709
12710 return PRINT_SRC_AND_LOC;
12711 }
12712
12713 static void
12714 bkpt_print_mention (struct breakpoint *b)
12715 {
12716 if (current_uiout->is_mi_like_p ())
12717 return;
12718
12719 switch (b->type)
12720 {
12721 case bp_breakpoint:
12722 case bp_gnu_ifunc_resolver:
12723 if (b->disposition == disp_del)
12724 printf_filtered (_("Temporary breakpoint"));
12725 else
12726 printf_filtered (_("Breakpoint"));
12727 printf_filtered (_(" %d"), b->number);
12728 if (b->type == bp_gnu_ifunc_resolver)
12729 printf_filtered (_(" at gnu-indirect-function resolver"));
12730 break;
12731 case bp_hardware_breakpoint:
12732 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12733 break;
12734 case bp_dprintf:
12735 printf_filtered (_("Dprintf %d"), b->number);
12736 break;
12737 }
12738
12739 say_where (b);
12740 }
12741
12742 static void
12743 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12744 {
12745 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12746 fprintf_unfiltered (fp, "tbreak");
12747 else if (tp->type == bp_breakpoint)
12748 fprintf_unfiltered (fp, "break");
12749 else if (tp->type == bp_hardware_breakpoint
12750 && tp->disposition == disp_del)
12751 fprintf_unfiltered (fp, "thbreak");
12752 else if (tp->type == bp_hardware_breakpoint)
12753 fprintf_unfiltered (fp, "hbreak");
12754 else
12755 internal_error (__FILE__, __LINE__,
12756 _("unhandled breakpoint type %d"), (int) tp->type);
12757
12758 fprintf_unfiltered (fp, " %s",
12759 event_location_to_string (tp->location.get ()));
12760
12761 /* Print out extra_string if this breakpoint is pending. It might
12762 contain, for example, conditions that were set by the user. */
12763 if (tp->loc == NULL && tp->extra_string != NULL)
12764 fprintf_unfiltered (fp, " %s", tp->extra_string);
12765
12766 print_recreate_thread (tp, fp);
12767 }
12768
12769 static void
12770 bkpt_create_sals_from_location (struct event_location *location,
12771 struct linespec_result *canonical,
12772 enum bptype type_wanted)
12773 {
12774 create_sals_from_location_default (location, canonical, type_wanted);
12775 }
12776
12777 static void
12778 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12779 struct linespec_result *canonical,
12780 gdb::unique_xmalloc_ptr<char> cond_string,
12781 gdb::unique_xmalloc_ptr<char> extra_string,
12782 enum bptype type_wanted,
12783 enum bpdisp disposition,
12784 int thread,
12785 int task, int ignore_count,
12786 const struct breakpoint_ops *ops,
12787 int from_tty, int enabled,
12788 int internal, unsigned flags)
12789 {
12790 create_breakpoints_sal_default (gdbarch, canonical,
12791 std::move (cond_string),
12792 std::move (extra_string),
12793 type_wanted,
12794 disposition, thread, task,
12795 ignore_count, ops, from_tty,
12796 enabled, internal, flags);
12797 }
12798
12799 static std::vector<symtab_and_line>
12800 bkpt_decode_location (struct breakpoint *b,
12801 struct event_location *location,
12802 struct program_space *search_pspace)
12803 {
12804 return decode_location_default (b, location, search_pspace);
12805 }
12806
12807 /* Virtual table for internal breakpoints. */
12808
12809 static void
12810 internal_bkpt_re_set (struct breakpoint *b)
12811 {
12812 switch (b->type)
12813 {
12814 /* Delete overlay event and longjmp master breakpoints; they
12815 will be reset later by breakpoint_re_set. */
12816 case bp_overlay_event:
12817 case bp_longjmp_master:
12818 case bp_std_terminate_master:
12819 case bp_exception_master:
12820 delete_breakpoint (b);
12821 break;
12822
12823 /* This breakpoint is special, it's set up when the inferior
12824 starts and we really don't want to touch it. */
12825 case bp_shlib_event:
12826
12827 /* Like bp_shlib_event, this breakpoint type is special. Once
12828 it is set up, we do not want to touch it. */
12829 case bp_thread_event:
12830 break;
12831 }
12832 }
12833
12834 static void
12835 internal_bkpt_check_status (bpstat bs)
12836 {
12837 if (bs->breakpoint_at->type == bp_shlib_event)
12838 {
12839 /* If requested, stop when the dynamic linker notifies GDB of
12840 events. This allows the user to get control and place
12841 breakpoints in initializer routines for dynamically loaded
12842 objects (among other things). */
12843 bs->stop = stop_on_solib_events;
12844 bs->print = stop_on_solib_events;
12845 }
12846 else
12847 bs->stop = 0;
12848 }
12849
12850 static enum print_stop_action
12851 internal_bkpt_print_it (bpstat bs)
12852 {
12853 struct breakpoint *b;
12854
12855 b = bs->breakpoint_at;
12856
12857 switch (b->type)
12858 {
12859 case bp_shlib_event:
12860 /* Did we stop because the user set the stop_on_solib_events
12861 variable? (If so, we report this as a generic, "Stopped due
12862 to shlib event" message.) */
12863 print_solib_event (0);
12864 break;
12865
12866 case bp_thread_event:
12867 /* Not sure how we will get here.
12868 GDB should not stop for these breakpoints. */
12869 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12870 break;
12871
12872 case bp_overlay_event:
12873 /* By analogy with the thread event, GDB should not stop for these. */
12874 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12875 break;
12876
12877 case bp_longjmp_master:
12878 /* These should never be enabled. */
12879 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12880 break;
12881
12882 case bp_std_terminate_master:
12883 /* These should never be enabled. */
12884 printf_filtered (_("std::terminate Master Breakpoint: "
12885 "gdb should not stop!\n"));
12886 break;
12887
12888 case bp_exception_master:
12889 /* These should never be enabled. */
12890 printf_filtered (_("Exception Master Breakpoint: "
12891 "gdb should not stop!\n"));
12892 break;
12893 }
12894
12895 return PRINT_NOTHING;
12896 }
12897
12898 static void
12899 internal_bkpt_print_mention (struct breakpoint *b)
12900 {
12901 /* Nothing to mention. These breakpoints are internal. */
12902 }
12903
12904 /* Virtual table for momentary breakpoints */
12905
12906 static void
12907 momentary_bkpt_re_set (struct breakpoint *b)
12908 {
12909 /* Keep temporary breakpoints, which can be encountered when we step
12910 over a dlopen call and solib_add is resetting the breakpoints.
12911 Otherwise these should have been blown away via the cleanup chain
12912 or by breakpoint_init_inferior when we rerun the executable. */
12913 }
12914
12915 static void
12916 momentary_bkpt_check_status (bpstat bs)
12917 {
12918 /* Nothing. The point of these breakpoints is causing a stop. */
12919 }
12920
12921 static enum print_stop_action
12922 momentary_bkpt_print_it (bpstat bs)
12923 {
12924 return PRINT_UNKNOWN;
12925 }
12926
12927 static void
12928 momentary_bkpt_print_mention (struct breakpoint *b)
12929 {
12930 /* Nothing to mention. These breakpoints are internal. */
12931 }
12932
12933 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12934
12935 It gets cleared already on the removal of the first one of such placed
12936 breakpoints. This is OK as they get all removed altogether. */
12937
12938 longjmp_breakpoint::~longjmp_breakpoint ()
12939 {
12940 thread_info *tp = find_thread_global_id (this->thread);
12941
12942 if (tp != NULL)
12943 tp->initiating_frame = null_frame_id;
12944 }
12945
12946 /* Specific methods for probe breakpoints. */
12947
12948 static int
12949 bkpt_probe_insert_location (struct bp_location *bl)
12950 {
12951 int v = bkpt_insert_location (bl);
12952
12953 if (v == 0)
12954 {
12955 /* The insertion was successful, now let's set the probe's semaphore
12956 if needed. */
12957 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
12958 }
12959
12960 return v;
12961 }
12962
12963 static int
12964 bkpt_probe_remove_location (struct bp_location *bl,
12965 enum remove_bp_reason reason)
12966 {
12967 /* Let's clear the semaphore before removing the location. */
12968 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
12969
12970 return bkpt_remove_location (bl, reason);
12971 }
12972
12973 static void
12974 bkpt_probe_create_sals_from_location (struct event_location *location,
12975 struct linespec_result *canonical,
12976 enum bptype type_wanted)
12977 {
12978 struct linespec_sals lsal;
12979
12980 lsal.sals = parse_probes (location, NULL, canonical);
12981 lsal.canonical
12982 = xstrdup (event_location_to_string (canonical->location.get ()));
12983 canonical->lsals.push_back (std::move (lsal));
12984 }
12985
12986 static std::vector<symtab_and_line>
12987 bkpt_probe_decode_location (struct breakpoint *b,
12988 struct event_location *location,
12989 struct program_space *search_pspace)
12990 {
12991 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12992 if (sals.empty ())
12993 error (_("probe not found"));
12994 return sals;
12995 }
12996
12997 /* The breakpoint_ops structure to be used in tracepoints. */
12998
12999 static void
13000 tracepoint_re_set (struct breakpoint *b)
13001 {
13002 breakpoint_re_set_default (b);
13003 }
13004
13005 static int
13006 tracepoint_breakpoint_hit (const struct bp_location *bl,
13007 const address_space *aspace, CORE_ADDR bp_addr,
13008 const struct target_waitstatus *ws)
13009 {
13010 /* By definition, the inferior does not report stops at
13011 tracepoints. */
13012 return 0;
13013 }
13014
13015 static void
13016 tracepoint_print_one_detail (const struct breakpoint *self,
13017 struct ui_out *uiout)
13018 {
13019 struct tracepoint *tp = (struct tracepoint *) self;
13020 if (!tp->static_trace_marker_id.empty ())
13021 {
13022 gdb_assert (self->type == bp_static_tracepoint);
13023
13024 uiout->message ("\tmarker id is %pF\n",
13025 string_field ("static-tracepoint-marker-string-id",
13026 tp->static_trace_marker_id.c_str ()));
13027 }
13028 }
13029
13030 static void
13031 tracepoint_print_mention (struct breakpoint *b)
13032 {
13033 if (current_uiout->is_mi_like_p ())
13034 return;
13035
13036 switch (b->type)
13037 {
13038 case bp_tracepoint:
13039 printf_filtered (_("Tracepoint"));
13040 printf_filtered (_(" %d"), b->number);
13041 break;
13042 case bp_fast_tracepoint:
13043 printf_filtered (_("Fast tracepoint"));
13044 printf_filtered (_(" %d"), b->number);
13045 break;
13046 case bp_static_tracepoint:
13047 printf_filtered (_("Static tracepoint"));
13048 printf_filtered (_(" %d"), b->number);
13049 break;
13050 default:
13051 internal_error (__FILE__, __LINE__,
13052 _("unhandled tracepoint type %d"), (int) b->type);
13053 }
13054
13055 say_where (b);
13056 }
13057
13058 static void
13059 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13060 {
13061 struct tracepoint *tp = (struct tracepoint *) self;
13062
13063 if (self->type == bp_fast_tracepoint)
13064 fprintf_unfiltered (fp, "ftrace");
13065 else if (self->type == bp_static_tracepoint)
13066 fprintf_unfiltered (fp, "strace");
13067 else if (self->type == bp_tracepoint)
13068 fprintf_unfiltered (fp, "trace");
13069 else
13070 internal_error (__FILE__, __LINE__,
13071 _("unhandled tracepoint type %d"), (int) self->type);
13072
13073 fprintf_unfiltered (fp, " %s",
13074 event_location_to_string (self->location.get ()));
13075 print_recreate_thread (self, fp);
13076
13077 if (tp->pass_count)
13078 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13079 }
13080
13081 static void
13082 tracepoint_create_sals_from_location (struct event_location *location,
13083 struct linespec_result *canonical,
13084 enum bptype type_wanted)
13085 {
13086 create_sals_from_location_default (location, canonical, type_wanted);
13087 }
13088
13089 static void
13090 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13091 struct linespec_result *canonical,
13092 gdb::unique_xmalloc_ptr<char> cond_string,
13093 gdb::unique_xmalloc_ptr<char> extra_string,
13094 enum bptype type_wanted,
13095 enum bpdisp disposition,
13096 int thread,
13097 int task, int ignore_count,
13098 const struct breakpoint_ops *ops,
13099 int from_tty, int enabled,
13100 int internal, unsigned flags)
13101 {
13102 create_breakpoints_sal_default (gdbarch, canonical,
13103 std::move (cond_string),
13104 std::move (extra_string),
13105 type_wanted,
13106 disposition, thread, task,
13107 ignore_count, ops, from_tty,
13108 enabled, internal, flags);
13109 }
13110
13111 static std::vector<symtab_and_line>
13112 tracepoint_decode_location (struct breakpoint *b,
13113 struct event_location *location,
13114 struct program_space *search_pspace)
13115 {
13116 return decode_location_default (b, location, search_pspace);
13117 }
13118
13119 struct breakpoint_ops tracepoint_breakpoint_ops;
13120
13121 /* Virtual table for tracepoints on static probes. */
13122
13123 static void
13124 tracepoint_probe_create_sals_from_location
13125 (struct event_location *location,
13126 struct linespec_result *canonical,
13127 enum bptype type_wanted)
13128 {
13129 /* We use the same method for breakpoint on probes. */
13130 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13131 }
13132
13133 static std::vector<symtab_and_line>
13134 tracepoint_probe_decode_location (struct breakpoint *b,
13135 struct event_location *location,
13136 struct program_space *search_pspace)
13137 {
13138 /* We use the same method for breakpoint on probes. */
13139 return bkpt_probe_decode_location (b, location, search_pspace);
13140 }
13141
13142 /* Dprintf breakpoint_ops methods. */
13143
13144 static void
13145 dprintf_re_set (struct breakpoint *b)
13146 {
13147 breakpoint_re_set_default (b);
13148
13149 /* extra_string should never be non-NULL for dprintf. */
13150 gdb_assert (b->extra_string != NULL);
13151
13152 /* 1 - connect to target 1, that can run breakpoint commands.
13153 2 - create a dprintf, which resolves fine.
13154 3 - disconnect from target 1
13155 4 - connect to target 2, that can NOT run breakpoint commands.
13156
13157 After steps #3/#4, you'll want the dprintf command list to
13158 be updated, because target 1 and 2 may well return different
13159 answers for target_can_run_breakpoint_commands().
13160 Given absence of finer grained resetting, we get to do
13161 it all the time. */
13162 if (b->extra_string != NULL)
13163 update_dprintf_command_list (b);
13164 }
13165
13166 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13167
13168 static void
13169 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13170 {
13171 fprintf_unfiltered (fp, "dprintf %s,%s",
13172 event_location_to_string (tp->location.get ()),
13173 tp->extra_string);
13174 print_recreate_thread (tp, fp);
13175 }
13176
13177 /* Implement the "after_condition_true" breakpoint_ops method for
13178 dprintf.
13179
13180 dprintf's are implemented with regular commands in their command
13181 list, but we run the commands here instead of before presenting the
13182 stop to the user, as dprintf's don't actually cause a stop. This
13183 also makes it so that the commands of multiple dprintfs at the same
13184 address are all handled. */
13185
13186 static void
13187 dprintf_after_condition_true (struct bpstats *bs)
13188 {
13189 struct bpstats tmp_bs;
13190 struct bpstats *tmp_bs_p = &tmp_bs;
13191
13192 /* dprintf's never cause a stop. This wasn't set in the
13193 check_status hook instead because that would make the dprintf's
13194 condition not be evaluated. */
13195 bs->stop = 0;
13196
13197 /* Run the command list here. Take ownership of it instead of
13198 copying. We never want these commands to run later in
13199 bpstat_do_actions, if a breakpoint that causes a stop happens to
13200 be set at same address as this dprintf, or even if running the
13201 commands here throws. */
13202 tmp_bs.commands = bs->commands;
13203 bs->commands = NULL;
13204
13205 bpstat_do_actions_1 (&tmp_bs_p);
13206
13207 /* 'tmp_bs.commands' will usually be NULL by now, but
13208 bpstat_do_actions_1 may return early without processing the whole
13209 list. */
13210 }
13211
13212 /* The breakpoint_ops structure to be used on static tracepoints with
13213 markers (`-m'). */
13214
13215 static void
13216 strace_marker_create_sals_from_location (struct event_location *location,
13217 struct linespec_result *canonical,
13218 enum bptype type_wanted)
13219 {
13220 struct linespec_sals lsal;
13221 const char *arg_start, *arg;
13222
13223 arg = arg_start = get_linespec_location (location)->spec_string;
13224 lsal.sals = decode_static_tracepoint_spec (&arg);
13225
13226 std::string str (arg_start, arg - arg_start);
13227 const char *ptr = str.c_str ();
13228 canonical->location
13229 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
13230
13231 lsal.canonical
13232 = xstrdup (event_location_to_string (canonical->location.get ()));
13233 canonical->lsals.push_back (std::move (lsal));
13234 }
13235
13236 static void
13237 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13238 struct linespec_result *canonical,
13239 gdb::unique_xmalloc_ptr<char> cond_string,
13240 gdb::unique_xmalloc_ptr<char> extra_string,
13241 enum bptype type_wanted,
13242 enum bpdisp disposition,
13243 int thread,
13244 int task, int ignore_count,
13245 const struct breakpoint_ops *ops,
13246 int from_tty, int enabled,
13247 int internal, unsigned flags)
13248 {
13249 const linespec_sals &lsal = canonical->lsals[0];
13250
13251 /* If the user is creating a static tracepoint by marker id
13252 (strace -m MARKER_ID), then store the sals index, so that
13253 breakpoint_re_set can try to match up which of the newly
13254 found markers corresponds to this one, and, don't try to
13255 expand multiple locations for each sal, given than SALS
13256 already should contain all sals for MARKER_ID. */
13257
13258 for (size_t i = 0; i < lsal.sals.size (); i++)
13259 {
13260 event_location_up location
13261 = copy_event_location (canonical->location.get ());
13262
13263 std::unique_ptr<tracepoint> tp (new tracepoint ());
13264 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
13265 std::move (location), NULL,
13266 std::move (cond_string),
13267 std::move (extra_string),
13268 type_wanted, disposition,
13269 thread, task, ignore_count, ops,
13270 from_tty, enabled, internal, flags,
13271 canonical->special_display);
13272 /* Given that its possible to have multiple markers with
13273 the same string id, if the user is creating a static
13274 tracepoint by marker id ("strace -m MARKER_ID"), then
13275 store the sals index, so that breakpoint_re_set can
13276 try to match up which of the newly found markers
13277 corresponds to this one */
13278 tp->static_trace_marker_id_idx = i;
13279
13280 install_breakpoint (internal, std::move (tp), 0);
13281 }
13282 }
13283
13284 static std::vector<symtab_and_line>
13285 strace_marker_decode_location (struct breakpoint *b,
13286 struct event_location *location,
13287 struct program_space *search_pspace)
13288 {
13289 struct tracepoint *tp = (struct tracepoint *) b;
13290 const char *s = get_linespec_location (location)->spec_string;
13291
13292 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13293 if (sals.size () > tp->static_trace_marker_id_idx)
13294 {
13295 sals[0] = sals[tp->static_trace_marker_id_idx];
13296 sals.resize (1);
13297 return sals;
13298 }
13299 else
13300 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
13301 }
13302
13303 static struct breakpoint_ops strace_marker_breakpoint_ops;
13304
13305 static int
13306 strace_marker_p (struct breakpoint *b)
13307 {
13308 return b->ops == &strace_marker_breakpoint_ops;
13309 }
13310
13311 /* Delete a breakpoint and clean up all traces of it in the data
13312 structures. */
13313
13314 void
13315 delete_breakpoint (struct breakpoint *bpt)
13316 {
13317 struct breakpoint *b;
13318
13319 gdb_assert (bpt != NULL);
13320
13321 /* Has this bp already been deleted? This can happen because
13322 multiple lists can hold pointers to bp's. bpstat lists are
13323 especial culprits.
13324
13325 One example of this happening is a watchpoint's scope bp. When
13326 the scope bp triggers, we notice that the watchpoint is out of
13327 scope, and delete it. We also delete its scope bp. But the
13328 scope bp is marked "auto-deleting", and is already on a bpstat.
13329 That bpstat is then checked for auto-deleting bp's, which are
13330 deleted.
13331
13332 A real solution to this problem might involve reference counts in
13333 bp's, and/or giving them pointers back to their referencing
13334 bpstat's, and teaching delete_breakpoint to only free a bp's
13335 storage when no more references were extent. A cheaper bandaid
13336 was chosen. */
13337 if (bpt->type == bp_none)
13338 return;
13339
13340 /* At least avoid this stale reference until the reference counting
13341 of breakpoints gets resolved. */
13342 if (bpt->related_breakpoint != bpt)
13343 {
13344 struct breakpoint *related;
13345 struct watchpoint *w;
13346
13347 if (bpt->type == bp_watchpoint_scope)
13348 w = (struct watchpoint *) bpt->related_breakpoint;
13349 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13350 w = (struct watchpoint *) bpt;
13351 else
13352 w = NULL;
13353 if (w != NULL)
13354 watchpoint_del_at_next_stop (w);
13355
13356 /* Unlink bpt from the bpt->related_breakpoint ring. */
13357 for (related = bpt; related->related_breakpoint != bpt;
13358 related = related->related_breakpoint);
13359 related->related_breakpoint = bpt->related_breakpoint;
13360 bpt->related_breakpoint = bpt;
13361 }
13362
13363 /* watch_command_1 creates a watchpoint but only sets its number if
13364 update_watchpoint succeeds in creating its bp_locations. If there's
13365 a problem in that process, we'll be asked to delete the half-created
13366 watchpoint. In that case, don't announce the deletion. */
13367 if (bpt->number)
13368 gdb::observers::breakpoint_deleted.notify (bpt);
13369
13370 if (breakpoint_chain == bpt)
13371 breakpoint_chain = bpt->next;
13372
13373 ALL_BREAKPOINTS (b)
13374 if (b->next == bpt)
13375 {
13376 b->next = bpt->next;
13377 break;
13378 }
13379
13380 /* Be sure no bpstat's are pointing at the breakpoint after it's
13381 been freed. */
13382 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13383 in all threads for now. Note that we cannot just remove bpstats
13384 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13385 commands are associated with the bpstat; if we remove it here,
13386 then the later call to bpstat_do_actions (&stop_bpstat); in
13387 event-top.c won't do anything, and temporary breakpoints with
13388 commands won't work. */
13389
13390 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13391
13392 /* Now that breakpoint is removed from breakpoint list, update the
13393 global location list. This will remove locations that used to
13394 belong to this breakpoint. Do this before freeing the breakpoint
13395 itself, since remove_breakpoint looks at location's owner. It
13396 might be better design to have location completely
13397 self-contained, but it's not the case now. */
13398 update_global_location_list (UGLL_DONT_INSERT);
13399
13400 /* On the chance that someone will soon try again to delete this
13401 same bp, we mark it as deleted before freeing its storage. */
13402 bpt->type = bp_none;
13403 delete bpt;
13404 }
13405
13406 /* Iterator function to call a user-provided callback function once
13407 for each of B and its related breakpoints. */
13408
13409 static void
13410 iterate_over_related_breakpoints (struct breakpoint *b,
13411 gdb::function_view<void (breakpoint *)> function)
13412 {
13413 struct breakpoint *related;
13414
13415 related = b;
13416 do
13417 {
13418 struct breakpoint *next;
13419
13420 /* FUNCTION may delete RELATED. */
13421 next = related->related_breakpoint;
13422
13423 if (next == related)
13424 {
13425 /* RELATED is the last ring entry. */
13426 function (related);
13427
13428 /* FUNCTION may have deleted it, so we'd never reach back to
13429 B. There's nothing left to do anyway, so just break
13430 out. */
13431 break;
13432 }
13433 else
13434 function (related);
13435
13436 related = next;
13437 }
13438 while (related != b);
13439 }
13440
13441 static void
13442 delete_command (const char *arg, int from_tty)
13443 {
13444 struct breakpoint *b, *b_tmp;
13445
13446 dont_repeat ();
13447
13448 if (arg == 0)
13449 {
13450 int breaks_to_delete = 0;
13451
13452 /* Delete all breakpoints if no argument. Do not delete
13453 internal breakpoints, these have to be deleted with an
13454 explicit breakpoint number argument. */
13455 ALL_BREAKPOINTS (b)
13456 if (user_breakpoint_p (b))
13457 {
13458 breaks_to_delete = 1;
13459 break;
13460 }
13461
13462 /* Ask user only if there are some breakpoints to delete. */
13463 if (!from_tty
13464 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13465 {
13466 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13467 if (user_breakpoint_p (b))
13468 delete_breakpoint (b);
13469 }
13470 }
13471 else
13472 map_breakpoint_numbers
13473 (arg, [&] (breakpoint *br)
13474 {
13475 iterate_over_related_breakpoints (br, delete_breakpoint);
13476 });
13477 }
13478
13479 /* Return true if all locations of B bound to PSPACE are pending. If
13480 PSPACE is NULL, all locations of all program spaces are
13481 considered. */
13482
13483 static int
13484 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13485 {
13486 struct bp_location *loc;
13487
13488 for (loc = b->loc; loc != NULL; loc = loc->next)
13489 if ((pspace == NULL
13490 || loc->pspace == pspace)
13491 && !loc->shlib_disabled
13492 && !loc->pspace->executing_startup)
13493 return 0;
13494 return 1;
13495 }
13496
13497 /* Subroutine of update_breakpoint_locations to simplify it.
13498 Return non-zero if multiple fns in list LOC have the same name.
13499 Null names are ignored. */
13500
13501 static int
13502 ambiguous_names_p (struct bp_location *loc)
13503 {
13504 struct bp_location *l;
13505 htab_up htab (htab_create_alloc (13, htab_hash_string, streq_hash, NULL,
13506 xcalloc, xfree));
13507
13508 for (l = loc; l != NULL; l = l->next)
13509 {
13510 const char **slot;
13511 const char *name = l->function_name;
13512
13513 /* Allow for some names to be NULL, ignore them. */
13514 if (name == NULL)
13515 continue;
13516
13517 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
13518 INSERT);
13519 /* NOTE: We can assume slot != NULL here because xcalloc never
13520 returns NULL. */
13521 if (*slot != NULL)
13522 return 1;
13523 *slot = name;
13524 }
13525
13526 return 0;
13527 }
13528
13529 /* When symbols change, it probably means the sources changed as well,
13530 and it might mean the static tracepoint markers are no longer at
13531 the same address or line numbers they used to be at last we
13532 checked. Losing your static tracepoints whenever you rebuild is
13533 undesirable. This function tries to resync/rematch gdb static
13534 tracepoints with the markers on the target, for static tracepoints
13535 that have not been set by marker id. Static tracepoint that have
13536 been set by marker id are reset by marker id in breakpoint_re_set.
13537 The heuristic is:
13538
13539 1) For a tracepoint set at a specific address, look for a marker at
13540 the old PC. If one is found there, assume to be the same marker.
13541 If the name / string id of the marker found is different from the
13542 previous known name, assume that means the user renamed the marker
13543 in the sources, and output a warning.
13544
13545 2) For a tracepoint set at a given line number, look for a marker
13546 at the new address of the old line number. If one is found there,
13547 assume to be the same marker. If the name / string id of the
13548 marker found is different from the previous known name, assume that
13549 means the user renamed the marker in the sources, and output a
13550 warning.
13551
13552 3) If a marker is no longer found at the same address or line, it
13553 may mean the marker no longer exists. But it may also just mean
13554 the code changed a bit. Maybe the user added a few lines of code
13555 that made the marker move up or down (in line number terms). Ask
13556 the target for info about the marker with the string id as we knew
13557 it. If found, update line number and address in the matching
13558 static tracepoint. This will get confused if there's more than one
13559 marker with the same ID (possible in UST, although unadvised
13560 precisely because it confuses tools). */
13561
13562 static struct symtab_and_line
13563 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13564 {
13565 struct tracepoint *tp = (struct tracepoint *) b;
13566 struct static_tracepoint_marker marker;
13567 CORE_ADDR pc;
13568
13569 pc = sal.pc;
13570 if (sal.line)
13571 find_line_pc (sal.symtab, sal.line, &pc);
13572
13573 if (target_static_tracepoint_marker_at (pc, &marker))
13574 {
13575 if (tp->static_trace_marker_id != marker.str_id)
13576 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13577 b->number, tp->static_trace_marker_id.c_str (),
13578 marker.str_id.c_str ());
13579
13580 tp->static_trace_marker_id = std::move (marker.str_id);
13581
13582 return sal;
13583 }
13584
13585 /* Old marker wasn't found on target at lineno. Try looking it up
13586 by string ID. */
13587 if (!sal.explicit_pc
13588 && sal.line != 0
13589 && sal.symtab != NULL
13590 && !tp->static_trace_marker_id.empty ())
13591 {
13592 std::vector<static_tracepoint_marker> markers
13593 = target_static_tracepoint_markers_by_strid
13594 (tp->static_trace_marker_id.c_str ());
13595
13596 if (!markers.empty ())
13597 {
13598 struct symbol *sym;
13599 struct static_tracepoint_marker *tpmarker;
13600 struct ui_out *uiout = current_uiout;
13601 struct explicit_location explicit_loc;
13602
13603 tpmarker = &markers[0];
13604
13605 tp->static_trace_marker_id = std::move (tpmarker->str_id);
13606
13607 warning (_("marker for static tracepoint %d (%s) not "
13608 "found at previous line number"),
13609 b->number, tp->static_trace_marker_id.c_str ());
13610
13611 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
13612 sym = find_pc_sect_function (tpmarker->address, NULL);
13613 uiout->text ("Now in ");
13614 if (sym)
13615 {
13616 uiout->field_string ("func", sym->print_name (),
13617 function_name_style.style ());
13618 uiout->text (" at ");
13619 }
13620 uiout->field_string ("file",
13621 symtab_to_filename_for_display (sal2.symtab),
13622 file_name_style.style ());
13623 uiout->text (":");
13624
13625 if (uiout->is_mi_like_p ())
13626 {
13627 const char *fullname = symtab_to_fullname (sal2.symtab);
13628
13629 uiout->field_string ("fullname", fullname);
13630 }
13631
13632 uiout->field_signed ("line", sal2.line);
13633 uiout->text ("\n");
13634
13635 b->loc->line_number = sal2.line;
13636 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13637
13638 b->location.reset (NULL);
13639 initialize_explicit_location (&explicit_loc);
13640 explicit_loc.source_filename
13641 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13642 explicit_loc.line_offset.offset = b->loc->line_number;
13643 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13644 b->location = new_explicit_location (&explicit_loc);
13645
13646 /* Might be nice to check if function changed, and warn if
13647 so. */
13648 }
13649 }
13650 return sal;
13651 }
13652
13653 /* Returns 1 iff locations A and B are sufficiently same that
13654 we don't need to report breakpoint as changed. */
13655
13656 static int
13657 locations_are_equal (struct bp_location *a, struct bp_location *b)
13658 {
13659 while (a && b)
13660 {
13661 if (a->address != b->address)
13662 return 0;
13663
13664 if (a->shlib_disabled != b->shlib_disabled)
13665 return 0;
13666
13667 if (a->enabled != b->enabled)
13668 return 0;
13669
13670 if (a->disabled_by_cond != b->disabled_by_cond)
13671 return 0;
13672
13673 a = a->next;
13674 b = b->next;
13675 }
13676
13677 if ((a == NULL) != (b == NULL))
13678 return 0;
13679
13680 return 1;
13681 }
13682
13683 /* Split all locations of B that are bound to PSPACE out of B's
13684 location list to a separate list and return that list's head. If
13685 PSPACE is NULL, hoist out all locations of B. */
13686
13687 static struct bp_location *
13688 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13689 {
13690 struct bp_location head;
13691 struct bp_location *i = b->loc;
13692 struct bp_location **i_link = &b->loc;
13693 struct bp_location *hoisted = &head;
13694
13695 if (pspace == NULL)
13696 {
13697 i = b->loc;
13698 b->loc = NULL;
13699 return i;
13700 }
13701
13702 head.next = NULL;
13703
13704 while (i != NULL)
13705 {
13706 if (i->pspace == pspace)
13707 {
13708 *i_link = i->next;
13709 i->next = NULL;
13710 hoisted->next = i;
13711 hoisted = i;
13712 }
13713 else
13714 i_link = &i->next;
13715 i = *i_link;
13716 }
13717
13718 return head.next;
13719 }
13720
13721 /* Create new breakpoint locations for B (a hardware or software
13722 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13723 zero, then B is a ranged breakpoint. Only recreates locations for
13724 FILTER_PSPACE. Locations of other program spaces are left
13725 untouched. */
13726
13727 void
13728 update_breakpoint_locations (struct breakpoint *b,
13729 struct program_space *filter_pspace,
13730 gdb::array_view<const symtab_and_line> sals,
13731 gdb::array_view<const symtab_and_line> sals_end)
13732 {
13733 struct bp_location *existing_locations;
13734
13735 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
13736 {
13737 /* Ranged breakpoints have only one start location and one end
13738 location. */
13739 b->enable_state = bp_disabled;
13740 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13741 "multiple locations found\n"),
13742 b->number);
13743 return;
13744 }
13745
13746 /* If there's no new locations, and all existing locations are
13747 pending, don't do anything. This optimizes the common case where
13748 all locations are in the same shared library, that was unloaded.
13749 We'd like to retain the location, so that when the library is
13750 loaded again, we don't loose the enabled/disabled status of the
13751 individual locations. */
13752 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
13753 return;
13754
13755 existing_locations = hoist_existing_locations (b, filter_pspace);
13756
13757 for (const auto &sal : sals)
13758 {
13759 struct bp_location *new_loc;
13760
13761 switch_to_program_space_and_thread (sal.pspace);
13762
13763 new_loc = add_location_to_breakpoint (b, &sal);
13764
13765 /* Reparse conditions, they might contain references to the
13766 old symtab. */
13767 if (b->cond_string != NULL)
13768 {
13769 const char *s;
13770
13771 s = b->cond_string;
13772 try
13773 {
13774 new_loc->cond = parse_exp_1 (&s, sal.pc,
13775 block_for_pc (sal.pc),
13776 0);
13777 }
13778 catch (const gdb_exception_error &e)
13779 {
13780 new_loc->disabled_by_cond = true;
13781 }
13782 }
13783
13784 if (!sals_end.empty ())
13785 {
13786 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13787
13788 new_loc->length = end - sals[0].pc + 1;
13789 }
13790 }
13791
13792 /* If possible, carry over 'disable' status from existing
13793 breakpoints. */
13794 {
13795 struct bp_location *e = existing_locations;
13796 /* If there are multiple breakpoints with the same function name,
13797 e.g. for inline functions, comparing function names won't work.
13798 Instead compare pc addresses; this is just a heuristic as things
13799 may have moved, but in practice it gives the correct answer
13800 often enough until a better solution is found. */
13801 int have_ambiguous_names = ambiguous_names_p (b->loc);
13802
13803 for (; e; e = e->next)
13804 {
13805 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
13806 {
13807 struct bp_location *l = b->loc;
13808 if (have_ambiguous_names)
13809 {
13810 for (; l; l = l->next)
13811 {
13812 /* Ignore software vs hardware location type at
13813 this point, because with "set breakpoint
13814 auto-hw", after a re-set, locations that were
13815 hardware can end up as software, or vice versa.
13816 As mentioned above, this is an heuristic and in
13817 practice should give the correct answer often
13818 enough. */
13819 if (breakpoint_locations_match (e, l, true))
13820 {
13821 l->enabled = e->enabled;
13822 l->disabled_by_cond = e->disabled_by_cond;
13823 break;
13824 }
13825 }
13826 }
13827 else
13828 {
13829 for (; l; l = l->next)
13830 if (l->function_name
13831 && strcmp (e->function_name, l->function_name) == 0)
13832 {
13833 l->enabled = e->enabled;
13834 l->disabled_by_cond = e->disabled_by_cond;
13835 break;
13836 }
13837 }
13838 }
13839 }
13840 }
13841
13842 if (!locations_are_equal (existing_locations, b->loc))
13843 gdb::observers::breakpoint_modified.notify (b);
13844 }
13845
13846 /* Find the SaL locations corresponding to the given LOCATION.
13847 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13848
13849 static std::vector<symtab_and_line>
13850 location_to_sals (struct breakpoint *b, struct event_location *location,
13851 struct program_space *search_pspace, int *found)
13852 {
13853 struct gdb_exception exception;
13854
13855 gdb_assert (b->ops != NULL);
13856
13857 std::vector<symtab_and_line> sals;
13858
13859 try
13860 {
13861 sals = b->ops->decode_location (b, location, search_pspace);
13862 }
13863 catch (gdb_exception_error &e)
13864 {
13865 int not_found_and_ok = 0;
13866
13867 /* For pending breakpoints, it's expected that parsing will
13868 fail until the right shared library is loaded. User has
13869 already told to create pending breakpoints and don't need
13870 extra messages. If breakpoint is in bp_shlib_disabled
13871 state, then user already saw the message about that
13872 breakpoint being disabled, and don't want to see more
13873 errors. */
13874 if (e.error == NOT_FOUND_ERROR
13875 && (b->condition_not_parsed
13876 || (b->loc != NULL
13877 && search_pspace != NULL
13878 && b->loc->pspace != search_pspace)
13879 || (b->loc && b->loc->shlib_disabled)
13880 || (b->loc && b->loc->pspace->executing_startup)
13881 || b->enable_state == bp_disabled))
13882 not_found_and_ok = 1;
13883
13884 if (!not_found_and_ok)
13885 {
13886 /* We surely don't want to warn about the same breakpoint
13887 10 times. One solution, implemented here, is disable
13888 the breakpoint on error. Another solution would be to
13889 have separate 'warning emitted' flag. Since this
13890 happens only when a binary has changed, I don't know
13891 which approach is better. */
13892 b->enable_state = bp_disabled;
13893 throw;
13894 }
13895
13896 exception = std::move (e);
13897 }
13898
13899 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13900 {
13901 for (auto &sal : sals)
13902 resolve_sal_pc (&sal);
13903 if (b->condition_not_parsed && b->extra_string != NULL)
13904 {
13905 char *cond_string, *extra_string;
13906 int thread, task;
13907
13908 find_condition_and_thread_for_sals (sals, b->extra_string,
13909 &cond_string, &thread,
13910 &task, &extra_string);
13911 gdb_assert (b->cond_string == NULL);
13912 if (cond_string)
13913 b->cond_string = cond_string;
13914 b->thread = thread;
13915 b->task = task;
13916 if (extra_string)
13917 {
13918 xfree (b->extra_string);
13919 b->extra_string = extra_string;
13920 }
13921 b->condition_not_parsed = 0;
13922 }
13923
13924 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13925 sals[0] = update_static_tracepoint (b, sals[0]);
13926
13927 *found = 1;
13928 }
13929 else
13930 *found = 0;
13931
13932 return sals;
13933 }
13934
13935 /* The default re_set method, for typical hardware or software
13936 breakpoints. Reevaluate the breakpoint and recreate its
13937 locations. */
13938
13939 static void
13940 breakpoint_re_set_default (struct breakpoint *b)
13941 {
13942 struct program_space *filter_pspace = current_program_space;
13943 std::vector<symtab_and_line> expanded, expanded_end;
13944
13945 int found;
13946 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13947 filter_pspace, &found);
13948 if (found)
13949 expanded = std::move (sals);
13950
13951 if (b->location_range_end != NULL)
13952 {
13953 std::vector<symtab_and_line> sals_end
13954 = location_to_sals (b, b->location_range_end.get (),
13955 filter_pspace, &found);
13956 if (found)
13957 expanded_end = std::move (sals_end);
13958 }
13959
13960 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
13961 }
13962
13963 /* Default method for creating SALs from an address string. It basically
13964 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13965
13966 static void
13967 create_sals_from_location_default (struct event_location *location,
13968 struct linespec_result *canonical,
13969 enum bptype type_wanted)
13970 {
13971 parse_breakpoint_sals (location, canonical);
13972 }
13973
13974 /* Call create_breakpoints_sal for the given arguments. This is the default
13975 function for the `create_breakpoints_sal' method of
13976 breakpoint_ops. */
13977
13978 static void
13979 create_breakpoints_sal_default (struct gdbarch *gdbarch,
13980 struct linespec_result *canonical,
13981 gdb::unique_xmalloc_ptr<char> cond_string,
13982 gdb::unique_xmalloc_ptr<char> extra_string,
13983 enum bptype type_wanted,
13984 enum bpdisp disposition,
13985 int thread,
13986 int task, int ignore_count,
13987 const struct breakpoint_ops *ops,
13988 int from_tty, int enabled,
13989 int internal, unsigned flags)
13990 {
13991 create_breakpoints_sal (gdbarch, canonical,
13992 std::move (cond_string),
13993 std::move (extra_string),
13994 type_wanted, disposition,
13995 thread, task, ignore_count, ops, from_tty,
13996 enabled, internal, flags);
13997 }
13998
13999 /* Decode the line represented by S by calling decode_line_full. This is the
14000 default function for the `decode_location' method of breakpoint_ops. */
14001
14002 static std::vector<symtab_and_line>
14003 decode_location_default (struct breakpoint *b,
14004 struct event_location *location,
14005 struct program_space *search_pspace)
14006 {
14007 struct linespec_result canonical;
14008
14009 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
14010 NULL, 0, &canonical, multiple_symbols_all,
14011 b->filter.get ());
14012
14013 /* We should get 0 or 1 resulting SALs. */
14014 gdb_assert (canonical.lsals.size () < 2);
14015
14016 if (!canonical.lsals.empty ())
14017 {
14018 const linespec_sals &lsal = canonical.lsals[0];
14019 return std::move (lsal.sals);
14020 }
14021 return {};
14022 }
14023
14024 /* Reset a breakpoint. */
14025
14026 static void
14027 breakpoint_re_set_one (breakpoint *b)
14028 {
14029 input_radix = b->input_radix;
14030 set_language (b->language);
14031
14032 b->ops->re_set (b);
14033 }
14034
14035 /* Re-set breakpoint locations for the current program space.
14036 Locations bound to other program spaces are left untouched. */
14037
14038 void
14039 breakpoint_re_set (void)
14040 {
14041 struct breakpoint *b, *b_tmp;
14042
14043 {
14044 scoped_restore_current_language save_language;
14045 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
14046 scoped_restore_current_pspace_and_thread restore_pspace_thread;
14047
14048 /* breakpoint_re_set_one sets the current_language to the language
14049 of the breakpoint it is resetting (see prepare_re_set_context)
14050 before re-evaluating the breakpoint's location. This change can
14051 unfortunately get undone by accident if the language_mode is set
14052 to auto, and we either switch frames, or more likely in this context,
14053 we select the current frame.
14054
14055 We prevent this by temporarily turning the language_mode to
14056 language_mode_manual. We restore it once all breakpoints
14057 have been reset. */
14058 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
14059 language_mode = language_mode_manual;
14060
14061 /* Note: we must not try to insert locations until after all
14062 breakpoints have been re-set. Otherwise, e.g., when re-setting
14063 breakpoint 1, we'd insert the locations of breakpoint 2, which
14064 hadn't been re-set yet, and thus may have stale locations. */
14065
14066 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14067 {
14068 try
14069 {
14070 breakpoint_re_set_one (b);
14071 }
14072 catch (const gdb_exception &ex)
14073 {
14074 exception_fprintf (gdb_stderr, ex,
14075 "Error in re-setting breakpoint %d: ",
14076 b->number);
14077 }
14078 }
14079
14080 jit_breakpoint_re_set ();
14081 }
14082
14083 create_overlay_event_breakpoint ();
14084 create_longjmp_master_breakpoint ();
14085 create_std_terminate_master_breakpoint ();
14086 create_exception_master_breakpoint ();
14087
14088 /* Now we can insert. */
14089 update_global_location_list (UGLL_MAY_INSERT);
14090 }
14091 \f
14092 /* Reset the thread number of this breakpoint:
14093
14094 - If the breakpoint is for all threads, leave it as-is.
14095 - Else, reset it to the current thread for inferior_ptid. */
14096 void
14097 breakpoint_re_set_thread (struct breakpoint *b)
14098 {
14099 if (b->thread != -1)
14100 {
14101 b->thread = inferior_thread ()->global_num;
14102
14103 /* We're being called after following a fork. The new fork is
14104 selected as current, and unless this was a vfork will have a
14105 different program space from the original thread. Reset that
14106 as well. */
14107 b->loc->pspace = current_program_space;
14108 }
14109 }
14110
14111 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14112 If from_tty is nonzero, it prints a message to that effect,
14113 which ends with a period (no newline). */
14114
14115 void
14116 set_ignore_count (int bptnum, int count, int from_tty)
14117 {
14118 struct breakpoint *b;
14119
14120 if (count < 0)
14121 count = 0;
14122
14123 ALL_BREAKPOINTS (b)
14124 if (b->number == bptnum)
14125 {
14126 if (is_tracepoint (b))
14127 {
14128 if (from_tty && count != 0)
14129 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14130 bptnum);
14131 return;
14132 }
14133
14134 b->ignore_count = count;
14135 if (from_tty)
14136 {
14137 if (count == 0)
14138 printf_filtered (_("Will stop next time "
14139 "breakpoint %d is reached."),
14140 bptnum);
14141 else if (count == 1)
14142 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14143 bptnum);
14144 else
14145 printf_filtered (_("Will ignore next %d "
14146 "crossings of breakpoint %d."),
14147 count, bptnum);
14148 }
14149 gdb::observers::breakpoint_modified.notify (b);
14150 return;
14151 }
14152
14153 error (_("No breakpoint number %d."), bptnum);
14154 }
14155
14156 /* Command to set ignore-count of breakpoint N to COUNT. */
14157
14158 static void
14159 ignore_command (const char *args, int from_tty)
14160 {
14161 const char *p = args;
14162 int num;
14163
14164 if (p == 0)
14165 error_no_arg (_("a breakpoint number"));
14166
14167 num = get_number (&p);
14168 if (num == 0)
14169 error (_("bad breakpoint number: '%s'"), args);
14170 if (*p == 0)
14171 error (_("Second argument (specified ignore-count) is missing."));
14172
14173 set_ignore_count (num,
14174 longest_to_int (value_as_long (parse_and_eval (p))),
14175 from_tty);
14176 if (from_tty)
14177 printf_filtered ("\n");
14178 }
14179 \f
14180
14181 /* Call FUNCTION on each of the breakpoints with numbers in the range
14182 defined by BP_NUM_RANGE (an inclusive range). */
14183
14184 static void
14185 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
14186 gdb::function_view<void (breakpoint *)> function)
14187 {
14188 if (bp_num_range.first == 0)
14189 {
14190 warning (_("bad breakpoint number at or near '%d'"),
14191 bp_num_range.first);
14192 }
14193 else
14194 {
14195 struct breakpoint *b, *tmp;
14196
14197 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
14198 {
14199 bool match = false;
14200
14201 ALL_BREAKPOINTS_SAFE (b, tmp)
14202 if (b->number == i)
14203 {
14204 match = true;
14205 function (b);
14206 break;
14207 }
14208 if (!match)
14209 printf_unfiltered (_("No breakpoint number %d.\n"), i);
14210 }
14211 }
14212 }
14213
14214 /* Call FUNCTION on each of the breakpoints whose numbers are given in
14215 ARGS. */
14216
14217 static void
14218 map_breakpoint_numbers (const char *args,
14219 gdb::function_view<void (breakpoint *)> function)
14220 {
14221 if (args == NULL || *args == '\0')
14222 error_no_arg (_("one or more breakpoint numbers"));
14223
14224 number_or_range_parser parser (args);
14225
14226 while (!parser.finished ())
14227 {
14228 int num = parser.get_number ();
14229 map_breakpoint_number_range (std::make_pair (num, num), function);
14230 }
14231 }
14232
14233 /* Return the breakpoint location structure corresponding to the
14234 BP_NUM and LOC_NUM values. */
14235
14236 static struct bp_location *
14237 find_location_by_number (int bp_num, int loc_num)
14238 {
14239 struct breakpoint *b;
14240
14241 ALL_BREAKPOINTS (b)
14242 if (b->number == bp_num)
14243 {
14244 break;
14245 }
14246
14247 if (!b || b->number != bp_num)
14248 error (_("Bad breakpoint number '%d'"), bp_num);
14249
14250 if (loc_num == 0)
14251 error (_("Bad breakpoint location number '%d'"), loc_num);
14252
14253 int n = 0;
14254 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
14255 if (++n == loc_num)
14256 return loc;
14257
14258 error (_("Bad breakpoint location number '%d'"), loc_num);
14259 }
14260
14261 /* Modes of operation for extract_bp_num. */
14262 enum class extract_bp_kind
14263 {
14264 /* Extracting a breakpoint number. */
14265 bp,
14266
14267 /* Extracting a location number. */
14268 loc,
14269 };
14270
14271 /* Extract a breakpoint or location number (as determined by KIND)
14272 from the string starting at START. TRAILER is a character which
14273 can be found after the number. If you don't want a trailer, use
14274 '\0'. If END_OUT is not NULL, it is set to point after the parsed
14275 string. This always returns a positive integer. */
14276
14277 static int
14278 extract_bp_num (extract_bp_kind kind, const char *start,
14279 int trailer, const char **end_out = NULL)
14280 {
14281 const char *end = start;
14282 int num = get_number_trailer (&end, trailer);
14283 if (num < 0)
14284 error (kind == extract_bp_kind::bp
14285 ? _("Negative breakpoint number '%.*s'")
14286 : _("Negative breakpoint location number '%.*s'"),
14287 int (end - start), start);
14288 if (num == 0)
14289 error (kind == extract_bp_kind::bp
14290 ? _("Bad breakpoint number '%.*s'")
14291 : _("Bad breakpoint location number '%.*s'"),
14292 int (end - start), start);
14293
14294 if (end_out != NULL)
14295 *end_out = end;
14296 return num;
14297 }
14298
14299 /* Extract a breakpoint or location range (as determined by KIND) in
14300 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14301 representing the (inclusive) range. The returned pair's elements
14302 are always positive integers. */
14303
14304 static std::pair<int, int>
14305 extract_bp_or_bp_range (extract_bp_kind kind,
14306 const std::string &arg,
14307 std::string::size_type arg_offset)
14308 {
14309 std::pair<int, int> range;
14310 const char *bp_loc = &arg[arg_offset];
14311 std::string::size_type dash = arg.find ('-', arg_offset);
14312 if (dash != std::string::npos)
14313 {
14314 /* bp_loc is a range (x-z). */
14315 if (arg.length () == dash + 1)
14316 error (kind == extract_bp_kind::bp
14317 ? _("Bad breakpoint number at or near: '%s'")
14318 : _("Bad breakpoint location number at or near: '%s'"),
14319 bp_loc);
14320
14321 const char *end;
14322 const char *start_first = bp_loc;
14323 const char *start_second = &arg[dash + 1];
14324 range.first = extract_bp_num (kind, start_first, '-');
14325 range.second = extract_bp_num (kind, start_second, '\0', &end);
14326
14327 if (range.first > range.second)
14328 error (kind == extract_bp_kind::bp
14329 ? _("Inverted breakpoint range at '%.*s'")
14330 : _("Inverted breakpoint location range at '%.*s'"),
14331 int (end - start_first), start_first);
14332 }
14333 else
14334 {
14335 /* bp_loc is a single value. */
14336 range.first = extract_bp_num (kind, bp_loc, '\0');
14337 range.second = range.first;
14338 }
14339 return range;
14340 }
14341
14342 /* Extract the breakpoint/location range specified by ARG. Returns
14343 the breakpoint range in BP_NUM_RANGE, and the location range in
14344 BP_LOC_RANGE.
14345
14346 ARG may be in any of the following forms:
14347
14348 x where 'x' is a breakpoint number.
14349 x-y where 'x' and 'y' specify a breakpoint numbers range.
14350 x.y where 'x' is a breakpoint number and 'y' a location number.
14351 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14352 location number range.
14353 */
14354
14355 static void
14356 extract_bp_number_and_location (const std::string &arg,
14357 std::pair<int, int> &bp_num_range,
14358 std::pair<int, int> &bp_loc_range)
14359 {
14360 std::string::size_type dot = arg.find ('.');
14361
14362 if (dot != std::string::npos)
14363 {
14364 /* Handle 'x.y' and 'x.y-z' cases. */
14365
14366 if (arg.length () == dot + 1 || dot == 0)
14367 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
14368
14369 bp_num_range.first
14370 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14371 bp_num_range.second = bp_num_range.first;
14372
14373 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14374 arg, dot + 1);
14375 }
14376 else
14377 {
14378 /* Handle x and x-y cases. */
14379
14380 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
14381 bp_loc_range.first = 0;
14382 bp_loc_range.second = 0;
14383 }
14384 }
14385
14386 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14387 specifies whether to enable or disable. */
14388
14389 static void
14390 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14391 {
14392 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14393 if (loc != NULL)
14394 {
14395 if (loc->disabled_by_cond && enable)
14396 error (_("Breakpoint %d's condition is invalid at location %d, "
14397 "cannot enable."), bp_num, loc_num);
14398
14399 if (loc->enabled != enable)
14400 {
14401 loc->enabled = enable;
14402 mark_breakpoint_location_modified (loc);
14403 }
14404 if (target_supports_enable_disable_tracepoint ()
14405 && current_trace_status ()->running && loc->owner
14406 && is_tracepoint (loc->owner))
14407 target_disable_tracepoint (loc);
14408 }
14409 update_global_location_list (UGLL_DONT_INSERT);
14410
14411 gdb::observers::breakpoint_modified.notify (loc->owner);
14412 }
14413
14414 /* Enable or disable a range of breakpoint locations. BP_NUM is the
14415 number of the breakpoint, and BP_LOC_RANGE specifies the
14416 (inclusive) range of location numbers of that breakpoint to
14417 enable/disable. ENABLE specifies whether to enable or disable the
14418 location. */
14419
14420 static void
14421 enable_disable_breakpoint_location_range (int bp_num,
14422 std::pair<int, int> &bp_loc_range,
14423 bool enable)
14424 {
14425 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14426 enable_disable_bp_num_loc (bp_num, i, enable);
14427 }
14428
14429 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14430 If from_tty is nonzero, it prints a message to that effect,
14431 which ends with a period (no newline). */
14432
14433 void
14434 disable_breakpoint (struct breakpoint *bpt)
14435 {
14436 /* Never disable a watchpoint scope breakpoint; we want to
14437 hit them when we leave scope so we can delete both the
14438 watchpoint and its scope breakpoint at that time. */
14439 if (bpt->type == bp_watchpoint_scope)
14440 return;
14441
14442 bpt->enable_state = bp_disabled;
14443
14444 /* Mark breakpoint locations modified. */
14445 mark_breakpoint_modified (bpt);
14446
14447 if (target_supports_enable_disable_tracepoint ()
14448 && current_trace_status ()->running && is_tracepoint (bpt))
14449 {
14450 struct bp_location *location;
14451
14452 for (location = bpt->loc; location; location = location->next)
14453 target_disable_tracepoint (location);
14454 }
14455
14456 update_global_location_list (UGLL_DONT_INSERT);
14457
14458 gdb::observers::breakpoint_modified.notify (bpt);
14459 }
14460
14461 /* Enable or disable the breakpoint(s) or breakpoint location(s)
14462 specified in ARGS. ARGS may be in any of the formats handled by
14463 extract_bp_number_and_location. ENABLE specifies whether to enable
14464 or disable the breakpoints/locations. */
14465
14466 static void
14467 enable_disable_command (const char *args, int from_tty, bool enable)
14468 {
14469 if (args == 0)
14470 {
14471 struct breakpoint *bpt;
14472
14473 ALL_BREAKPOINTS (bpt)
14474 if (user_breakpoint_p (bpt))
14475 {
14476 if (enable)
14477 enable_breakpoint (bpt);
14478 else
14479 disable_breakpoint (bpt);
14480 }
14481 }
14482 else
14483 {
14484 std::string num = extract_arg (&args);
14485
14486 while (!num.empty ())
14487 {
14488 std::pair<int, int> bp_num_range, bp_loc_range;
14489
14490 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14491
14492 if (bp_loc_range.first == bp_loc_range.second
14493 && bp_loc_range.first == 0)
14494 {
14495 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14496 map_breakpoint_number_range (bp_num_range,
14497 enable
14498 ? enable_breakpoint
14499 : disable_breakpoint);
14500 }
14501 else
14502 {
14503 /* Handle breakpoint ids with formats 'x.y' or
14504 'x.y-z'. */
14505 enable_disable_breakpoint_location_range
14506 (bp_num_range.first, bp_loc_range, enable);
14507 }
14508 num = extract_arg (&args);
14509 }
14510 }
14511 }
14512
14513 /* The disable command disables the specified breakpoints/locations
14514 (or all defined breakpoints) so they're no longer effective in
14515 stopping the inferior. ARGS may be in any of the forms defined in
14516 extract_bp_number_and_location. */
14517
14518 static void
14519 disable_command (const char *args, int from_tty)
14520 {
14521 enable_disable_command (args, from_tty, false);
14522 }
14523
14524 static void
14525 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14526 int count)
14527 {
14528 int target_resources_ok;
14529
14530 if (bpt->type == bp_hardware_breakpoint)
14531 {
14532 int i;
14533 i = hw_breakpoint_used_count ();
14534 target_resources_ok =
14535 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14536 i + 1, 0);
14537 if (target_resources_ok == 0)
14538 error (_("No hardware breakpoint support in the target."));
14539 else if (target_resources_ok < 0)
14540 error (_("Hardware breakpoints used exceeds limit."));
14541 }
14542
14543 if (is_watchpoint (bpt))
14544 {
14545 /* Initialize it just to avoid a GCC false warning. */
14546 enum enable_state orig_enable_state = bp_disabled;
14547
14548 try
14549 {
14550 struct watchpoint *w = (struct watchpoint *) bpt;
14551
14552 orig_enable_state = bpt->enable_state;
14553 bpt->enable_state = bp_enabled;
14554 update_watchpoint (w, 1 /* reparse */);
14555 }
14556 catch (const gdb_exception &e)
14557 {
14558 bpt->enable_state = orig_enable_state;
14559 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14560 bpt->number);
14561 return;
14562 }
14563 }
14564
14565 bpt->enable_state = bp_enabled;
14566
14567 /* Mark breakpoint locations modified. */
14568 mark_breakpoint_modified (bpt);
14569
14570 if (target_supports_enable_disable_tracepoint ()
14571 && current_trace_status ()->running && is_tracepoint (bpt))
14572 {
14573 struct bp_location *location;
14574
14575 for (location = bpt->loc; location; location = location->next)
14576 target_enable_tracepoint (location);
14577 }
14578
14579 bpt->disposition = disposition;
14580 bpt->enable_count = count;
14581 update_global_location_list (UGLL_MAY_INSERT);
14582
14583 gdb::observers::breakpoint_modified.notify (bpt);
14584 }
14585
14586
14587 void
14588 enable_breakpoint (struct breakpoint *bpt)
14589 {
14590 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14591 }
14592
14593 /* The enable command enables the specified breakpoints/locations (or
14594 all defined breakpoints) so they once again become (or continue to
14595 be) effective in stopping the inferior. ARGS may be in any of the
14596 forms defined in extract_bp_number_and_location. */
14597
14598 static void
14599 enable_command (const char *args, int from_tty)
14600 {
14601 enable_disable_command (args, from_tty, true);
14602 }
14603
14604 static void
14605 enable_once_command (const char *args, int from_tty)
14606 {
14607 map_breakpoint_numbers
14608 (args, [&] (breakpoint *b)
14609 {
14610 iterate_over_related_breakpoints
14611 (b, [&] (breakpoint *bpt)
14612 {
14613 enable_breakpoint_disp (bpt, disp_disable, 1);
14614 });
14615 });
14616 }
14617
14618 static void
14619 enable_count_command (const char *args, int from_tty)
14620 {
14621 int count;
14622
14623 if (args == NULL)
14624 error_no_arg (_("hit count"));
14625
14626 count = get_number (&args);
14627
14628 map_breakpoint_numbers
14629 (args, [&] (breakpoint *b)
14630 {
14631 iterate_over_related_breakpoints
14632 (b, [&] (breakpoint *bpt)
14633 {
14634 enable_breakpoint_disp (bpt, disp_disable, count);
14635 });
14636 });
14637 }
14638
14639 static void
14640 enable_delete_command (const char *args, int from_tty)
14641 {
14642 map_breakpoint_numbers
14643 (args, [&] (breakpoint *b)
14644 {
14645 iterate_over_related_breakpoints
14646 (b, [&] (breakpoint *bpt)
14647 {
14648 enable_breakpoint_disp (bpt, disp_del, 1);
14649 });
14650 });
14651 }
14652 \f
14653 /* Invalidate last known value of any hardware watchpoint if
14654 the memory which that value represents has been written to by
14655 GDB itself. */
14656
14657 static void
14658 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14659 CORE_ADDR addr, ssize_t len,
14660 const bfd_byte *data)
14661 {
14662 struct breakpoint *bp;
14663
14664 ALL_BREAKPOINTS (bp)
14665 if (bp->enable_state == bp_enabled
14666 && bp->type == bp_hardware_watchpoint)
14667 {
14668 struct watchpoint *wp = (struct watchpoint *) bp;
14669
14670 if (wp->val_valid && wp->val != nullptr)
14671 {
14672 struct bp_location *loc;
14673
14674 for (loc = bp->loc; loc != NULL; loc = loc->next)
14675 if (loc->loc_type == bp_loc_hardware_watchpoint
14676 && loc->address + loc->length > addr
14677 && addr + len > loc->address)
14678 {
14679 wp->val = NULL;
14680 wp->val_valid = false;
14681 }
14682 }
14683 }
14684 }
14685
14686 /* Create and insert a breakpoint for software single step. */
14687
14688 void
14689 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14690 const address_space *aspace,
14691 CORE_ADDR next_pc)
14692 {
14693 struct thread_info *tp = inferior_thread ();
14694 struct symtab_and_line sal;
14695 CORE_ADDR pc = next_pc;
14696
14697 if (tp->control.single_step_breakpoints == NULL)
14698 {
14699 tp->control.single_step_breakpoints
14700 = new_single_step_breakpoint (tp->global_num, gdbarch);
14701 }
14702
14703 sal = find_pc_line (pc, 0);
14704 sal.pc = pc;
14705 sal.section = find_pc_overlay (pc);
14706 sal.explicit_pc = 1;
14707 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14708
14709 update_global_location_list (UGLL_INSERT);
14710 }
14711
14712 /* Insert single step breakpoints according to the current state. */
14713
14714 int
14715 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14716 {
14717 struct regcache *regcache = get_current_regcache ();
14718 std::vector<CORE_ADDR> next_pcs;
14719
14720 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14721
14722 if (!next_pcs.empty ())
14723 {
14724 struct frame_info *frame = get_current_frame ();
14725 const address_space *aspace = get_frame_address_space (frame);
14726
14727 for (CORE_ADDR pc : next_pcs)
14728 insert_single_step_breakpoint (gdbarch, aspace, pc);
14729
14730 return 1;
14731 }
14732 else
14733 return 0;
14734 }
14735
14736 /* See breakpoint.h. */
14737
14738 int
14739 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14740 const address_space *aspace,
14741 CORE_ADDR pc)
14742 {
14743 struct bp_location *loc;
14744
14745 for (loc = bp->loc; loc != NULL; loc = loc->next)
14746 if (loc->inserted
14747 && breakpoint_location_address_match (loc, aspace, pc))
14748 return 1;
14749
14750 return 0;
14751 }
14752
14753 /* Check whether a software single-step breakpoint is inserted at
14754 PC. */
14755
14756 int
14757 single_step_breakpoint_inserted_here_p (const address_space *aspace,
14758 CORE_ADDR pc)
14759 {
14760 struct breakpoint *bpt;
14761
14762 ALL_BREAKPOINTS (bpt)
14763 {
14764 if (bpt->type == bp_single_step
14765 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14766 return 1;
14767 }
14768 return 0;
14769 }
14770
14771 /* Tracepoint-specific operations. */
14772
14773 /* Set tracepoint count to NUM. */
14774 static void
14775 set_tracepoint_count (int num)
14776 {
14777 tracepoint_count = num;
14778 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14779 }
14780
14781 static void
14782 trace_command (const char *arg, int from_tty)
14783 {
14784 event_location_up location = string_to_event_location (&arg,
14785 current_language);
14786 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
14787 (location.get (), true /* is_tracepoint */);
14788
14789 create_breakpoint (get_current_arch (),
14790 location.get (),
14791 NULL, 0, arg, 1 /* parse arg */,
14792 0 /* tempflag */,
14793 bp_tracepoint /* type_wanted */,
14794 0 /* Ignore count */,
14795 pending_break_support,
14796 ops,
14797 from_tty,
14798 1 /* enabled */,
14799 0 /* internal */, 0);
14800 }
14801
14802 static void
14803 ftrace_command (const char *arg, int from_tty)
14804 {
14805 event_location_up location = string_to_event_location (&arg,
14806 current_language);
14807 create_breakpoint (get_current_arch (),
14808 location.get (),
14809 NULL, 0, arg, 1 /* parse arg */,
14810 0 /* tempflag */,
14811 bp_fast_tracepoint /* type_wanted */,
14812 0 /* Ignore count */,
14813 pending_break_support,
14814 &tracepoint_breakpoint_ops,
14815 from_tty,
14816 1 /* enabled */,
14817 0 /* internal */, 0);
14818 }
14819
14820 /* strace command implementation. Creates a static tracepoint. */
14821
14822 static void
14823 strace_command (const char *arg, int from_tty)
14824 {
14825 struct breakpoint_ops *ops;
14826 event_location_up location;
14827
14828 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14829 or with a normal static tracepoint. */
14830 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14831 {
14832 ops = &strace_marker_breakpoint_ops;
14833 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
14834 }
14835 else
14836 {
14837 ops = &tracepoint_breakpoint_ops;
14838 location = string_to_event_location (&arg, current_language);
14839 }
14840
14841 create_breakpoint (get_current_arch (),
14842 location.get (),
14843 NULL, 0, arg, 1 /* parse arg */,
14844 0 /* tempflag */,
14845 bp_static_tracepoint /* type_wanted */,
14846 0 /* Ignore count */,
14847 pending_break_support,
14848 ops,
14849 from_tty,
14850 1 /* enabled */,
14851 0 /* internal */, 0);
14852 }
14853
14854 /* Set up a fake reader function that gets command lines from a linked
14855 list that was acquired during tracepoint uploading. */
14856
14857 static struct uploaded_tp *this_utp;
14858 static int next_cmd;
14859
14860 static char *
14861 read_uploaded_action (void)
14862 {
14863 char *rslt = nullptr;
14864
14865 if (next_cmd < this_utp->cmd_strings.size ())
14866 {
14867 rslt = this_utp->cmd_strings[next_cmd].get ();
14868 next_cmd++;
14869 }
14870
14871 return rslt;
14872 }
14873
14874 /* Given information about a tracepoint as recorded on a target (which
14875 can be either a live system or a trace file), attempt to create an
14876 equivalent GDB tracepoint. This is not a reliable process, since
14877 the target does not necessarily have all the information used when
14878 the tracepoint was originally defined. */
14879
14880 struct tracepoint *
14881 create_tracepoint_from_upload (struct uploaded_tp *utp)
14882 {
14883 const char *addr_str;
14884 char small_buf[100];
14885 struct tracepoint *tp;
14886
14887 if (utp->at_string)
14888 addr_str = utp->at_string.get ();
14889 else
14890 {
14891 /* In the absence of a source location, fall back to raw
14892 address. Since there is no way to confirm that the address
14893 means the same thing as when the trace was started, warn the
14894 user. */
14895 warning (_("Uploaded tracepoint %d has no "
14896 "source location, using raw address"),
14897 utp->number);
14898 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14899 addr_str = small_buf;
14900 }
14901
14902 /* There's not much we can do with a sequence of bytecodes. */
14903 if (utp->cond && !utp->cond_string)
14904 warning (_("Uploaded tracepoint %d condition "
14905 "has no source form, ignoring it"),
14906 utp->number);
14907
14908 event_location_up location = string_to_event_location (&addr_str,
14909 current_language);
14910 if (!create_breakpoint (get_current_arch (),
14911 location.get (),
14912 utp->cond_string.get (), -1, addr_str,
14913 0 /* parse cond/thread */,
14914 0 /* tempflag */,
14915 utp->type /* type_wanted */,
14916 0 /* Ignore count */,
14917 pending_break_support,
14918 &tracepoint_breakpoint_ops,
14919 0 /* from_tty */,
14920 utp->enabled /* enabled */,
14921 0 /* internal */,
14922 CREATE_BREAKPOINT_FLAGS_INSERTED))
14923 return NULL;
14924
14925 /* Get the tracepoint we just created. */
14926 tp = get_tracepoint (tracepoint_count);
14927 gdb_assert (tp != NULL);
14928
14929 if (utp->pass > 0)
14930 {
14931 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14932 tp->number);
14933
14934 trace_pass_command (small_buf, 0);
14935 }
14936
14937 /* If we have uploaded versions of the original commands, set up a
14938 special-purpose "reader" function and call the usual command line
14939 reader, then pass the result to the breakpoint command-setting
14940 function. */
14941 if (!utp->cmd_strings.empty ())
14942 {
14943 counted_command_line cmd_list;
14944
14945 this_utp = utp;
14946 next_cmd = 0;
14947
14948 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
14949
14950 breakpoint_set_commands (tp, std::move (cmd_list));
14951 }
14952 else if (!utp->actions.empty ()
14953 || !utp->step_actions.empty ())
14954 warning (_("Uploaded tracepoint %d actions "
14955 "have no source form, ignoring them"),
14956 utp->number);
14957
14958 /* Copy any status information that might be available. */
14959 tp->hit_count = utp->hit_count;
14960 tp->traceframe_usage = utp->traceframe_usage;
14961
14962 return tp;
14963 }
14964
14965 /* Print information on tracepoint number TPNUM_EXP, or all if
14966 omitted. */
14967
14968 static void
14969 info_tracepoints_command (const char *args, int from_tty)
14970 {
14971 struct ui_out *uiout = current_uiout;
14972 int num_printed;
14973
14974 num_printed = breakpoint_1 (args, false, is_tracepoint);
14975
14976 if (num_printed == 0)
14977 {
14978 if (args == NULL || *args == '\0')
14979 uiout->message ("No tracepoints.\n");
14980 else
14981 uiout->message ("No tracepoint matching '%s'.\n", args);
14982 }
14983
14984 default_collect_info ();
14985 }
14986
14987 /* The 'enable trace' command enables tracepoints.
14988 Not supported by all targets. */
14989 static void
14990 enable_trace_command (const char *args, int from_tty)
14991 {
14992 enable_command (args, from_tty);
14993 }
14994
14995 /* The 'disable trace' command disables tracepoints.
14996 Not supported by all targets. */
14997 static void
14998 disable_trace_command (const char *args, int from_tty)
14999 {
15000 disable_command (args, from_tty);
15001 }
15002
15003 /* Remove a tracepoint (or all if no argument). */
15004 static void
15005 delete_trace_command (const char *arg, int from_tty)
15006 {
15007 struct breakpoint *b, *b_tmp;
15008
15009 dont_repeat ();
15010
15011 if (arg == 0)
15012 {
15013 int breaks_to_delete = 0;
15014
15015 /* Delete all breakpoints if no argument.
15016 Do not delete internal or call-dummy breakpoints, these
15017 have to be deleted with an explicit breakpoint number
15018 argument. */
15019 ALL_TRACEPOINTS (b)
15020 if (is_tracepoint (b) && user_breakpoint_p (b))
15021 {
15022 breaks_to_delete = 1;
15023 break;
15024 }
15025
15026 /* Ask user only if there are some breakpoints to delete. */
15027 if (!from_tty
15028 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15029 {
15030 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15031 if (is_tracepoint (b) && user_breakpoint_p (b))
15032 delete_breakpoint (b);
15033 }
15034 }
15035 else
15036 map_breakpoint_numbers
15037 (arg, [&] (breakpoint *br)
15038 {
15039 iterate_over_related_breakpoints (br, delete_breakpoint);
15040 });
15041 }
15042
15043 /* Helper function for trace_pass_command. */
15044
15045 static void
15046 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15047 {
15048 tp->pass_count = count;
15049 gdb::observers::breakpoint_modified.notify (tp);
15050 if (from_tty)
15051 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15052 tp->number, count);
15053 }
15054
15055 /* Set passcount for tracepoint.
15056
15057 First command argument is passcount, second is tracepoint number.
15058 If tracepoint number omitted, apply to most recently defined.
15059 Also accepts special argument "all". */
15060
15061 static void
15062 trace_pass_command (const char *args, int from_tty)
15063 {
15064 struct tracepoint *t1;
15065 ULONGEST count;
15066
15067 if (args == 0 || *args == 0)
15068 error (_("passcount command requires an "
15069 "argument (count + optional TP num)"));
15070
15071 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
15072
15073 args = skip_spaces (args);
15074 if (*args && strncasecmp (args, "all", 3) == 0)
15075 {
15076 struct breakpoint *b;
15077
15078 args += 3; /* Skip special argument "all". */
15079 if (*args)
15080 error (_("Junk at end of arguments."));
15081
15082 ALL_TRACEPOINTS (b)
15083 {
15084 t1 = (struct tracepoint *) b;
15085 trace_pass_set_count (t1, count, from_tty);
15086 }
15087 }
15088 else if (*args == '\0')
15089 {
15090 t1 = get_tracepoint_by_number (&args, NULL);
15091 if (t1)
15092 trace_pass_set_count (t1, count, from_tty);
15093 }
15094 else
15095 {
15096 number_or_range_parser parser (args);
15097 while (!parser.finished ())
15098 {
15099 t1 = get_tracepoint_by_number (&args, &parser);
15100 if (t1)
15101 trace_pass_set_count (t1, count, from_tty);
15102 }
15103 }
15104 }
15105
15106 struct tracepoint *
15107 get_tracepoint (int num)
15108 {
15109 struct breakpoint *t;
15110
15111 ALL_TRACEPOINTS (t)
15112 if (t->number == num)
15113 return (struct tracepoint *) t;
15114
15115 return NULL;
15116 }
15117
15118 /* Find the tracepoint with the given target-side number (which may be
15119 different from the tracepoint number after disconnecting and
15120 reconnecting). */
15121
15122 struct tracepoint *
15123 get_tracepoint_by_number_on_target (int num)
15124 {
15125 struct breakpoint *b;
15126
15127 ALL_TRACEPOINTS (b)
15128 {
15129 struct tracepoint *t = (struct tracepoint *) b;
15130
15131 if (t->number_on_target == num)
15132 return t;
15133 }
15134
15135 return NULL;
15136 }
15137
15138 /* Utility: parse a tracepoint number and look it up in the list.
15139 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15140 If the argument is missing, the most recent tracepoint
15141 (tracepoint_count) is returned. */
15142
15143 struct tracepoint *
15144 get_tracepoint_by_number (const char **arg,
15145 number_or_range_parser *parser)
15146 {
15147 struct breakpoint *t;
15148 int tpnum;
15149 const char *instring = arg == NULL ? NULL : *arg;
15150
15151 if (parser != NULL)
15152 {
15153 gdb_assert (!parser->finished ());
15154 tpnum = parser->get_number ();
15155 }
15156 else if (arg == NULL || *arg == NULL || ! **arg)
15157 tpnum = tracepoint_count;
15158 else
15159 tpnum = get_number (arg);
15160
15161 if (tpnum <= 0)
15162 {
15163 if (instring && *instring)
15164 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15165 instring);
15166 else
15167 printf_filtered (_("No previous tracepoint\n"));
15168 return NULL;
15169 }
15170
15171 ALL_TRACEPOINTS (t)
15172 if (t->number == tpnum)
15173 {
15174 return (struct tracepoint *) t;
15175 }
15176
15177 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15178 return NULL;
15179 }
15180
15181 void
15182 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15183 {
15184 if (b->thread != -1)
15185 fprintf_unfiltered (fp, " thread %d", b->thread);
15186
15187 if (b->task != 0)
15188 fprintf_unfiltered (fp, " task %d", b->task);
15189
15190 fprintf_unfiltered (fp, "\n");
15191 }
15192
15193 /* Save information on user settable breakpoints (watchpoints, etc) to
15194 a new script file named FILENAME. If FILTER is non-NULL, call it
15195 on each breakpoint and only include the ones for which it returns
15196 true. */
15197
15198 static void
15199 save_breakpoints (const char *filename, int from_tty,
15200 bool (*filter) (const struct breakpoint *))
15201 {
15202 struct breakpoint *tp;
15203 int any = 0;
15204 int extra_trace_bits = 0;
15205
15206 if (filename == 0 || *filename == 0)
15207 error (_("Argument required (file name in which to save)"));
15208
15209 /* See if we have anything to save. */
15210 ALL_BREAKPOINTS (tp)
15211 {
15212 /* Skip internal and momentary breakpoints. */
15213 if (!user_breakpoint_p (tp))
15214 continue;
15215
15216 /* If we have a filter, only save the breakpoints it accepts. */
15217 if (filter && !filter (tp))
15218 continue;
15219
15220 any = 1;
15221
15222 if (is_tracepoint (tp))
15223 {
15224 extra_trace_bits = 1;
15225
15226 /* We can stop searching. */
15227 break;
15228 }
15229 }
15230
15231 if (!any)
15232 {
15233 warning (_("Nothing to save."));
15234 return;
15235 }
15236
15237 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
15238
15239 stdio_file fp;
15240
15241 if (!fp.open (expanded_filename.get (), "w"))
15242 error (_("Unable to open file '%s' for saving (%s)"),
15243 expanded_filename.get (), safe_strerror (errno));
15244
15245 if (extra_trace_bits)
15246 save_trace_state_variables (&fp);
15247
15248 ALL_BREAKPOINTS (tp)
15249 {
15250 /* Skip internal and momentary breakpoints. */
15251 if (!user_breakpoint_p (tp))
15252 continue;
15253
15254 /* If we have a filter, only save the breakpoints it accepts. */
15255 if (filter && !filter (tp))
15256 continue;
15257
15258 tp->ops->print_recreate (tp, &fp);
15259
15260 /* Note, we can't rely on tp->number for anything, as we can't
15261 assume the recreated breakpoint numbers will match. Use $bpnum
15262 instead. */
15263
15264 if (tp->cond_string)
15265 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15266
15267 if (tp->ignore_count)
15268 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15269
15270 if (tp->type != bp_dprintf && tp->commands)
15271 {
15272 fp.puts (" commands\n");
15273
15274 current_uiout->redirect (&fp);
15275 try
15276 {
15277 print_command_lines (current_uiout, tp->commands.get (), 2);
15278 }
15279 catch (const gdb_exception &ex)
15280 {
15281 current_uiout->redirect (NULL);
15282 throw;
15283 }
15284
15285 current_uiout->redirect (NULL);
15286 fp.puts (" end\n");
15287 }
15288
15289 if (tp->enable_state == bp_disabled)
15290 fp.puts ("disable $bpnum\n");
15291
15292 /* If this is a multi-location breakpoint, check if the locations
15293 should be individually disabled. Watchpoint locations are
15294 special, and not user visible. */
15295 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15296 {
15297 struct bp_location *loc;
15298 int n = 1;
15299
15300 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15301 if (!loc->enabled)
15302 fp.printf ("disable $bpnum.%d\n", n);
15303 }
15304 }
15305
15306 if (extra_trace_bits && *default_collect)
15307 fp.printf ("set default-collect %s\n", default_collect);
15308
15309 if (from_tty)
15310 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
15311 }
15312
15313 /* The `save breakpoints' command. */
15314
15315 static void
15316 save_breakpoints_command (const char *args, int from_tty)
15317 {
15318 save_breakpoints (args, from_tty, NULL);
15319 }
15320
15321 /* The `save tracepoints' command. */
15322
15323 static void
15324 save_tracepoints_command (const char *args, int from_tty)
15325 {
15326 save_breakpoints (args, from_tty, is_tracepoint);
15327 }
15328
15329 /* Create a vector of all tracepoints. */
15330
15331 std::vector<breakpoint *>
15332 all_tracepoints (void)
15333 {
15334 std::vector<breakpoint *> tp_vec;
15335 struct breakpoint *tp;
15336
15337 ALL_TRACEPOINTS (tp)
15338 {
15339 tp_vec.push_back (tp);
15340 }
15341
15342 return tp_vec;
15343 }
15344
15345 \f
15346 /* This help string is used to consolidate all the help string for specifying
15347 locations used by several commands. */
15348
15349 #define LOCATION_HELP_STRING \
15350 "Linespecs are colon-separated lists of location parameters, such as\n\
15351 source filename, function name, label name, and line number.\n\
15352 Example: To specify the start of a label named \"the_top\" in the\n\
15353 function \"fact\" in the file \"factorial.c\", use\n\
15354 \"factorial.c:fact:the_top\".\n\
15355 \n\
15356 Address locations begin with \"*\" and specify an exact address in the\n\
15357 program. Example: To specify the fourth byte past the start function\n\
15358 \"main\", use \"*main + 4\".\n\
15359 \n\
15360 Explicit locations are similar to linespecs but use an option/argument\n\
15361 syntax to specify location parameters.\n\
15362 Example: To specify the start of the label named \"the_top\" in the\n\
15363 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15364 -function fact -label the_top\".\n\
15365 \n\
15366 By default, a specified function is matched against the program's\n\
15367 functions in all scopes. For C++, this means in all namespaces and\n\
15368 classes. For Ada, this means in all packages. E.g., in C++,\n\
15369 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15370 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
15371 specified name as a complete fully-qualified name instead."
15372
15373 /* This help string is used for the break, hbreak, tbreak and thbreak
15374 commands. It is defined as a macro to prevent duplication.
15375 COMMAND should be a string constant containing the name of the
15376 command. */
15377
15378 #define BREAK_ARGS_HELP(command) \
15379 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
15380 \t[-force-condition] [if CONDITION]\n\
15381 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15382 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15383 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15384 `-probe-dtrace' (for a DTrace probe).\n\
15385 LOCATION may be a linespec, address, or explicit location as described\n\
15386 below.\n\
15387 \n\
15388 With no LOCATION, uses current execution address of the selected\n\
15389 stack frame. This is useful for breaking on return to a stack frame.\n\
15390 \n\
15391 THREADNUM is the number from \"info threads\".\n\
15392 CONDITION is a boolean expression.\n\
15393 \n\
15394 With the \"-force-condition\" flag, the condition is defined even when\n\
15395 it is invalid for all current locations.\n\
15396 \n" LOCATION_HELP_STRING "\n\n\
15397 Multiple breakpoints at one place are permitted, and useful if their\n\
15398 conditions are different.\n\
15399 \n\
15400 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15401
15402 /* List of subcommands for "catch". */
15403 static struct cmd_list_element *catch_cmdlist;
15404
15405 /* List of subcommands for "tcatch". */
15406 static struct cmd_list_element *tcatch_cmdlist;
15407
15408 void
15409 add_catch_command (const char *name, const char *docstring,
15410 cmd_const_sfunc_ftype *sfunc,
15411 completer_ftype *completer,
15412 void *user_data_catch,
15413 void *user_data_tcatch)
15414 {
15415 struct cmd_list_element *command;
15416
15417 command = add_cmd (name, class_breakpoint, docstring,
15418 &catch_cmdlist);
15419 set_cmd_sfunc (command, sfunc);
15420 set_cmd_context (command, user_data_catch);
15421 set_cmd_completer (command, completer);
15422
15423 command = add_cmd (name, class_breakpoint, docstring,
15424 &tcatch_cmdlist);
15425 set_cmd_sfunc (command, sfunc);
15426 set_cmd_context (command, user_data_tcatch);
15427 set_cmd_completer (command, completer);
15428 }
15429
15430 struct breakpoint *
15431 iterate_over_breakpoints (gdb::function_view<bool (breakpoint *)> callback)
15432 {
15433 struct breakpoint *b, *b_tmp;
15434
15435 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15436 {
15437 if (callback (b))
15438 return b;
15439 }
15440
15441 return NULL;
15442 }
15443
15444 /* Zero if any of the breakpoint's locations could be a location where
15445 functions have been inlined, nonzero otherwise. */
15446
15447 static int
15448 is_non_inline_function (struct breakpoint *b)
15449 {
15450 /* The shared library event breakpoint is set on the address of a
15451 non-inline function. */
15452 if (b->type == bp_shlib_event)
15453 return 1;
15454
15455 return 0;
15456 }
15457
15458 /* Nonzero if the specified PC cannot be a location where functions
15459 have been inlined. */
15460
15461 int
15462 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
15463 const struct target_waitstatus *ws)
15464 {
15465 struct breakpoint *b;
15466 struct bp_location *bl;
15467
15468 ALL_BREAKPOINTS (b)
15469 {
15470 if (!is_non_inline_function (b))
15471 continue;
15472
15473 for (bl = b->loc; bl != NULL; bl = bl->next)
15474 {
15475 if (!bl->shlib_disabled
15476 && bpstat_check_location (bl, aspace, pc, ws))
15477 return 1;
15478 }
15479 }
15480
15481 return 0;
15482 }
15483
15484 /* Remove any references to OBJFILE which is going to be freed. */
15485
15486 void
15487 breakpoint_free_objfile (struct objfile *objfile)
15488 {
15489 struct bp_location **locp, *loc;
15490
15491 ALL_BP_LOCATIONS (loc, locp)
15492 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15493 loc->symtab = NULL;
15494 }
15495
15496 void
15497 initialize_breakpoint_ops (void)
15498 {
15499 static int initialized = 0;
15500
15501 struct breakpoint_ops *ops;
15502
15503 if (initialized)
15504 return;
15505 initialized = 1;
15506
15507 /* The breakpoint_ops structure to be inherit by all kinds of
15508 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15509 internal and momentary breakpoints, etc.). */
15510 ops = &bkpt_base_breakpoint_ops;
15511 *ops = base_breakpoint_ops;
15512 ops->re_set = bkpt_re_set;
15513 ops->insert_location = bkpt_insert_location;
15514 ops->remove_location = bkpt_remove_location;
15515 ops->breakpoint_hit = bkpt_breakpoint_hit;
15516 ops->create_sals_from_location = bkpt_create_sals_from_location;
15517 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15518 ops->decode_location = bkpt_decode_location;
15519
15520 /* The breakpoint_ops structure to be used in regular breakpoints. */
15521 ops = &bkpt_breakpoint_ops;
15522 *ops = bkpt_base_breakpoint_ops;
15523 ops->re_set = bkpt_re_set;
15524 ops->resources_needed = bkpt_resources_needed;
15525 ops->print_it = bkpt_print_it;
15526 ops->print_mention = bkpt_print_mention;
15527 ops->print_recreate = bkpt_print_recreate;
15528
15529 /* Ranged breakpoints. */
15530 ops = &ranged_breakpoint_ops;
15531 *ops = bkpt_breakpoint_ops;
15532 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15533 ops->resources_needed = resources_needed_ranged_breakpoint;
15534 ops->print_it = print_it_ranged_breakpoint;
15535 ops->print_one = print_one_ranged_breakpoint;
15536 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15537 ops->print_mention = print_mention_ranged_breakpoint;
15538 ops->print_recreate = print_recreate_ranged_breakpoint;
15539
15540 /* Internal breakpoints. */
15541 ops = &internal_breakpoint_ops;
15542 *ops = bkpt_base_breakpoint_ops;
15543 ops->re_set = internal_bkpt_re_set;
15544 ops->check_status = internal_bkpt_check_status;
15545 ops->print_it = internal_bkpt_print_it;
15546 ops->print_mention = internal_bkpt_print_mention;
15547
15548 /* Momentary breakpoints. */
15549 ops = &momentary_breakpoint_ops;
15550 *ops = bkpt_base_breakpoint_ops;
15551 ops->re_set = momentary_bkpt_re_set;
15552 ops->check_status = momentary_bkpt_check_status;
15553 ops->print_it = momentary_bkpt_print_it;
15554 ops->print_mention = momentary_bkpt_print_mention;
15555
15556 /* Probe breakpoints. */
15557 ops = &bkpt_probe_breakpoint_ops;
15558 *ops = bkpt_breakpoint_ops;
15559 ops->insert_location = bkpt_probe_insert_location;
15560 ops->remove_location = bkpt_probe_remove_location;
15561 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15562 ops->decode_location = bkpt_probe_decode_location;
15563
15564 /* Watchpoints. */
15565 ops = &watchpoint_breakpoint_ops;
15566 *ops = base_breakpoint_ops;
15567 ops->re_set = re_set_watchpoint;
15568 ops->insert_location = insert_watchpoint;
15569 ops->remove_location = remove_watchpoint;
15570 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15571 ops->check_status = check_status_watchpoint;
15572 ops->resources_needed = resources_needed_watchpoint;
15573 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15574 ops->print_it = print_it_watchpoint;
15575 ops->print_mention = print_mention_watchpoint;
15576 ops->print_recreate = print_recreate_watchpoint;
15577 ops->explains_signal = explains_signal_watchpoint;
15578
15579 /* Masked watchpoints. */
15580 ops = &masked_watchpoint_breakpoint_ops;
15581 *ops = watchpoint_breakpoint_ops;
15582 ops->insert_location = insert_masked_watchpoint;
15583 ops->remove_location = remove_masked_watchpoint;
15584 ops->resources_needed = resources_needed_masked_watchpoint;
15585 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15586 ops->print_it = print_it_masked_watchpoint;
15587 ops->print_one_detail = print_one_detail_masked_watchpoint;
15588 ops->print_mention = print_mention_masked_watchpoint;
15589 ops->print_recreate = print_recreate_masked_watchpoint;
15590
15591 /* Tracepoints. */
15592 ops = &tracepoint_breakpoint_ops;
15593 *ops = base_breakpoint_ops;
15594 ops->re_set = tracepoint_re_set;
15595 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15596 ops->print_one_detail = tracepoint_print_one_detail;
15597 ops->print_mention = tracepoint_print_mention;
15598 ops->print_recreate = tracepoint_print_recreate;
15599 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15600 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15601 ops->decode_location = tracepoint_decode_location;
15602
15603 /* Probe tracepoints. */
15604 ops = &tracepoint_probe_breakpoint_ops;
15605 *ops = tracepoint_breakpoint_ops;
15606 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15607 ops->decode_location = tracepoint_probe_decode_location;
15608
15609 /* Static tracepoints with marker (`-m'). */
15610 ops = &strace_marker_breakpoint_ops;
15611 *ops = tracepoint_breakpoint_ops;
15612 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15613 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15614 ops->decode_location = strace_marker_decode_location;
15615
15616 /* Fork catchpoints. */
15617 ops = &catch_fork_breakpoint_ops;
15618 *ops = base_breakpoint_ops;
15619 ops->insert_location = insert_catch_fork;
15620 ops->remove_location = remove_catch_fork;
15621 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15622 ops->print_it = print_it_catch_fork;
15623 ops->print_one = print_one_catch_fork;
15624 ops->print_mention = print_mention_catch_fork;
15625 ops->print_recreate = print_recreate_catch_fork;
15626
15627 /* Vfork catchpoints. */
15628 ops = &catch_vfork_breakpoint_ops;
15629 *ops = base_breakpoint_ops;
15630 ops->insert_location = insert_catch_vfork;
15631 ops->remove_location = remove_catch_vfork;
15632 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15633 ops->print_it = print_it_catch_vfork;
15634 ops->print_one = print_one_catch_vfork;
15635 ops->print_mention = print_mention_catch_vfork;
15636 ops->print_recreate = print_recreate_catch_vfork;
15637
15638 /* Exec catchpoints. */
15639 ops = &catch_exec_breakpoint_ops;
15640 *ops = base_breakpoint_ops;
15641 ops->insert_location = insert_catch_exec;
15642 ops->remove_location = remove_catch_exec;
15643 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15644 ops->print_it = print_it_catch_exec;
15645 ops->print_one = print_one_catch_exec;
15646 ops->print_mention = print_mention_catch_exec;
15647 ops->print_recreate = print_recreate_catch_exec;
15648
15649 /* Solib-related catchpoints. */
15650 ops = &catch_solib_breakpoint_ops;
15651 *ops = base_breakpoint_ops;
15652 ops->insert_location = insert_catch_solib;
15653 ops->remove_location = remove_catch_solib;
15654 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15655 ops->check_status = check_status_catch_solib;
15656 ops->print_it = print_it_catch_solib;
15657 ops->print_one = print_one_catch_solib;
15658 ops->print_mention = print_mention_catch_solib;
15659 ops->print_recreate = print_recreate_catch_solib;
15660
15661 ops = &dprintf_breakpoint_ops;
15662 *ops = bkpt_base_breakpoint_ops;
15663 ops->re_set = dprintf_re_set;
15664 ops->resources_needed = bkpt_resources_needed;
15665 ops->print_it = bkpt_print_it;
15666 ops->print_mention = bkpt_print_mention;
15667 ops->print_recreate = dprintf_print_recreate;
15668 ops->after_condition_true = dprintf_after_condition_true;
15669 ops->breakpoint_hit = dprintf_breakpoint_hit;
15670 }
15671
15672 /* Chain containing all defined "enable breakpoint" subcommands. */
15673
15674 static struct cmd_list_element *enablebreaklist = NULL;
15675
15676 /* See breakpoint.h. */
15677
15678 cmd_list_element *commands_cmd_element = nullptr;
15679
15680 void _initialize_breakpoint ();
15681 void
15682 _initialize_breakpoint ()
15683 {
15684 struct cmd_list_element *c;
15685
15686 initialize_breakpoint_ops ();
15687
15688 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib);
15689 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile);
15690 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change);
15691
15692 breakpoint_chain = 0;
15693 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15694 before a breakpoint is set. */
15695 breakpoint_count = 0;
15696
15697 tracepoint_count = 0;
15698
15699 add_com ("ignore", class_breakpoint, ignore_command, _("\
15700 Set ignore-count of breakpoint number N to COUNT.\n\
15701 Usage is `ignore N COUNT'."));
15702
15703 commands_cmd_element = add_com ("commands", class_breakpoint,
15704 commands_command, _("\
15705 Set commands to be executed when the given breakpoints are hit.\n\
15706 Give a space-separated breakpoint list as argument after \"commands\".\n\
15707 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15708 (e.g. `5-7').\n\
15709 With no argument, the targeted breakpoint is the last one set.\n\
15710 The commands themselves follow starting on the next line.\n\
15711 Type a line containing \"end\" to indicate the end of them.\n\
15712 Give \"silent\" as the first line to make the breakpoint silent;\n\
15713 then no output is printed when it is hit, except what the commands print."));
15714
15715 const auto cc_opts = make_condition_command_options_def_group (nullptr);
15716 static std::string condition_command_help
15717 = gdb::option::build_help (_("\
15718 Specify breakpoint number N to break only if COND is true.\n\
15719 Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
15720 is an expression to be evaluated whenever breakpoint N is reached.\n\
15721 \n\
15722 Options:\n\
15723 %OPTIONS%"), cc_opts);
15724
15725 c = add_com ("condition", class_breakpoint, condition_command,
15726 condition_command_help.c_str ());
15727 set_cmd_completer_handle_brkchars (c, condition_completer);
15728
15729 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15730 Set a temporary breakpoint.\n\
15731 Like \"break\" except the breakpoint is only temporary,\n\
15732 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15733 by using \"enable delete\" on the breakpoint number.\n\
15734 \n"
15735 BREAK_ARGS_HELP ("tbreak")));
15736 set_cmd_completer (c, location_completer);
15737
15738 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15739 Set a hardware assisted breakpoint.\n\
15740 Like \"break\" except the breakpoint requires hardware support,\n\
15741 some target hardware may not have this support.\n\
15742 \n"
15743 BREAK_ARGS_HELP ("hbreak")));
15744 set_cmd_completer (c, location_completer);
15745
15746 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15747 Set a temporary hardware assisted breakpoint.\n\
15748 Like \"hbreak\" except the breakpoint is only temporary,\n\
15749 so it will be deleted when hit.\n\
15750 \n"
15751 BREAK_ARGS_HELP ("thbreak")));
15752 set_cmd_completer (c, location_completer);
15753
15754 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15755 Enable all or some breakpoints.\n\
15756 Usage: enable [BREAKPOINTNUM]...\n\
15757 Give breakpoint numbers (separated by spaces) as arguments.\n\
15758 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15759 This is used to cancel the effect of the \"disable\" command.\n\
15760 With a subcommand you can enable temporarily."),
15761 &enablelist, "enable ", 1, &cmdlist);
15762
15763 add_com_alias ("en", "enable", class_breakpoint, 1);
15764
15765 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15766 Enable all or some breakpoints.\n\
15767 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
15768 Give breakpoint numbers (separated by spaces) as arguments.\n\
15769 This is used to cancel the effect of the \"disable\" command.\n\
15770 May be abbreviated to simply \"enable\"."),
15771 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
15772
15773 add_cmd ("once", no_class, enable_once_command, _("\
15774 Enable some breakpoints for one hit.\n\
15775 Usage: enable breakpoints once BREAKPOINTNUM...\n\
15776 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15777 &enablebreaklist);
15778
15779 add_cmd ("delete", no_class, enable_delete_command, _("\
15780 Enable some breakpoints and delete when hit.\n\
15781 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
15782 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15783 &enablebreaklist);
15784
15785 add_cmd ("count", no_class, enable_count_command, _("\
15786 Enable some breakpoints for COUNT hits.\n\
15787 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
15788 If a breakpoint is hit while enabled in this fashion,\n\
15789 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15790 &enablebreaklist);
15791
15792 add_cmd ("delete", no_class, enable_delete_command, _("\
15793 Enable some breakpoints and delete when hit.\n\
15794 Usage: enable delete BREAKPOINTNUM...\n\
15795 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15796 &enablelist);
15797
15798 add_cmd ("once", no_class, enable_once_command, _("\
15799 Enable some breakpoints for one hit.\n\
15800 Usage: enable once BREAKPOINTNUM...\n\
15801 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15802 &enablelist);
15803
15804 add_cmd ("count", no_class, enable_count_command, _("\
15805 Enable some breakpoints for COUNT hits.\n\
15806 Usage: enable count COUNT BREAKPOINTNUM...\n\
15807 If a breakpoint is hit while enabled in this fashion,\n\
15808 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15809 &enablelist);
15810
15811 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15812 Disable all or some breakpoints.\n\
15813 Usage: disable [BREAKPOINTNUM]...\n\
15814 Arguments are breakpoint numbers with spaces in between.\n\
15815 To disable all breakpoints, give no argument.\n\
15816 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15817 &disablelist, "disable ", 1, &cmdlist);
15818 add_com_alias ("dis", "disable", class_breakpoint, 1);
15819 add_com_alias ("disa", "disable", class_breakpoint, 1);
15820
15821 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
15822 Disable all or some breakpoints.\n\
15823 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
15824 Arguments are breakpoint numbers with spaces in between.\n\
15825 To disable all breakpoints, give no argument.\n\
15826 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15827 This command may be abbreviated \"disable\"."),
15828 &disablelist);
15829
15830 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15831 Delete all or some breakpoints.\n\
15832 Usage: delete [BREAKPOINTNUM]...\n\
15833 Arguments are breakpoint numbers with spaces in between.\n\
15834 To delete all breakpoints, give no argument.\n\
15835 \n\
15836 Also a prefix command for deletion of other GDB objects."),
15837 &deletelist, "delete ", 1, &cmdlist);
15838 add_com_alias ("d", "delete", class_breakpoint, 1);
15839 add_com_alias ("del", "delete", class_breakpoint, 1);
15840
15841 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
15842 Delete all or some breakpoints or auto-display expressions.\n\
15843 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
15844 Arguments are breakpoint numbers with spaces in between.\n\
15845 To delete all breakpoints, give no argument.\n\
15846 This command may be abbreviated \"delete\"."),
15847 &deletelist);
15848
15849 add_com ("clear", class_breakpoint, clear_command, _("\
15850 Clear breakpoint at specified location.\n\
15851 Argument may be a linespec, explicit, or address location as described below.\n\
15852 \n\
15853 With no argument, clears all breakpoints in the line that the selected frame\n\
15854 is executing in.\n"
15855 "\n" LOCATION_HELP_STRING "\n\n\
15856 See also the \"delete\" command which clears breakpoints by number."));
15857 add_com_alias ("cl", "clear", class_breakpoint, 1);
15858
15859 c = add_com ("break", class_breakpoint, break_command, _("\
15860 Set breakpoint at specified location.\n"
15861 BREAK_ARGS_HELP ("break")));
15862 set_cmd_completer (c, location_completer);
15863
15864 add_com_alias ("b", "break", class_run, 1);
15865 add_com_alias ("br", "break", class_run, 1);
15866 add_com_alias ("bre", "break", class_run, 1);
15867 add_com_alias ("brea", "break", class_run, 1);
15868
15869 if (dbx_commands)
15870 {
15871 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15872 Break in function/address or break at a line in the current file."),
15873 &stoplist, "stop ", 1, &cmdlist);
15874 add_cmd ("in", class_breakpoint, stopin_command,
15875 _("Break in function or address."), &stoplist);
15876 add_cmd ("at", class_breakpoint, stopat_command,
15877 _("Break at a line in the current file."), &stoplist);
15878 add_com ("status", class_info, info_breakpoints_command, _("\
15879 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15880 The \"Type\" column indicates one of:\n\
15881 \tbreakpoint - normal breakpoint\n\
15882 \twatchpoint - watchpoint\n\
15883 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15884 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15885 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15886 address and file/line number respectively.\n\
15887 \n\
15888 Convenience variable \"$_\" and default examine address for \"x\"\n\
15889 are set to the address of the last breakpoint listed unless the command\n\
15890 is prefixed with \"server \".\n\n\
15891 Convenience variable \"$bpnum\" contains the number of the last\n\
15892 breakpoint set."));
15893 }
15894
15895 add_info ("breakpoints", info_breakpoints_command, _("\
15896 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15897 The \"Type\" column indicates one of:\n\
15898 \tbreakpoint - normal breakpoint\n\
15899 \twatchpoint - watchpoint\n\
15900 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15901 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15902 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15903 address and file/line number respectively.\n\
15904 \n\
15905 Convenience variable \"$_\" and default examine address for \"x\"\n\
15906 are set to the address of the last breakpoint listed unless the command\n\
15907 is prefixed with \"server \".\n\n\
15908 Convenience variable \"$bpnum\" contains the number of the last\n\
15909 breakpoint set."));
15910
15911 add_info_alias ("b", "breakpoints", 1);
15912
15913 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15914 Status of all breakpoints, or breakpoint number NUMBER.\n\
15915 The \"Type\" column indicates one of:\n\
15916 \tbreakpoint - normal breakpoint\n\
15917 \twatchpoint - watchpoint\n\
15918 \tlongjmp - internal breakpoint used to step through longjmp()\n\
15919 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15920 \tuntil - internal breakpoint used by the \"until\" command\n\
15921 \tfinish - internal breakpoint used by the \"finish\" command\n\
15922 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15923 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15924 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15925 address and file/line number respectively.\n\
15926 \n\
15927 Convenience variable \"$_\" and default examine address for \"x\"\n\
15928 are set to the address of the last breakpoint listed unless the command\n\
15929 is prefixed with \"server \".\n\n\
15930 Convenience variable \"$bpnum\" contains the number of the last\n\
15931 breakpoint set."),
15932 &maintenanceinfolist);
15933
15934 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
15935 Set catchpoints to catch events."),
15936 &catch_cmdlist, "catch ",
15937 0/*allow-unknown*/, &cmdlist);
15938
15939 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
15940 Set temporary catchpoints to catch events."),
15941 &tcatch_cmdlist, "tcatch ",
15942 0/*allow-unknown*/, &cmdlist);
15943
15944 add_catch_command ("fork", _("Catch calls to fork."),
15945 catch_fork_command_1,
15946 NULL,
15947 (void *) (uintptr_t) catch_fork_permanent,
15948 (void *) (uintptr_t) catch_fork_temporary);
15949 add_catch_command ("vfork", _("Catch calls to vfork."),
15950 catch_fork_command_1,
15951 NULL,
15952 (void *) (uintptr_t) catch_vfork_permanent,
15953 (void *) (uintptr_t) catch_vfork_temporary);
15954 add_catch_command ("exec", _("Catch calls to exec."),
15955 catch_exec_command_1,
15956 NULL,
15957 CATCH_PERMANENT,
15958 CATCH_TEMPORARY);
15959 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15960 Usage: catch load [REGEX]\n\
15961 If REGEX is given, only stop for libraries matching the regular expression."),
15962 catch_load_command_1,
15963 NULL,
15964 CATCH_PERMANENT,
15965 CATCH_TEMPORARY);
15966 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15967 Usage: catch unload [REGEX]\n\
15968 If REGEX is given, only stop for libraries matching the regular expression."),
15969 catch_unload_command_1,
15970 NULL,
15971 CATCH_PERMANENT,
15972 CATCH_TEMPORARY);
15973
15974 const auto opts = make_watch_options_def_group (nullptr);
15975
15976 static const std::string watch_help = gdb::option::build_help (_("\
15977 Set a watchpoint for EXPRESSION.\n\
15978 Usage: watch [-location] EXPRESSION\n\
15979 \n\
15980 Options:\n\
15981 %OPTIONS%\n\
15982 \n\
15983 A watchpoint stops execution of your program whenever the value of\n\
15984 an expression changes."), opts);
15985 c = add_com ("watch", class_breakpoint, watch_command,
15986 watch_help.c_str ());
15987 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15988
15989 static const std::string rwatch_help = gdb::option::build_help (_("\
15990 Set a read watchpoint for EXPRESSION.\n\
15991 Usage: rwatch [-location] EXPRESSION\n\
15992 \n\
15993 Options:\n\
15994 %OPTIONS%\n\
15995 \n\
15996 A read watchpoint stops execution of your program whenever the value of\n\
15997 an expression is read."), opts);
15998 c = add_com ("rwatch", class_breakpoint, rwatch_command,
15999 rwatch_help.c_str ());
16000 set_cmd_completer_handle_brkchars (c, watch_command_completer);
16001
16002 static const std::string awatch_help = gdb::option::build_help (_("\
16003 Set an access watchpoint for EXPRESSION.\n\
16004 Usage: awatch [-location] EXPRESSION\n\
16005 \n\
16006 Options:\n\
16007 %OPTIONS%\n\
16008 \n\
16009 An access watchpoint stops execution of your program whenever the value\n\
16010 of an expression is either read or written."), opts);
16011 c = add_com ("awatch", class_breakpoint, awatch_command,
16012 awatch_help.c_str ());
16013 set_cmd_completer_handle_brkchars (c, watch_command_completer);
16014
16015 add_info ("watchpoints", info_watchpoints_command, _("\
16016 Status of specified watchpoints (all watchpoints if no argument)."));
16017
16018 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16019 respond to changes - contrary to the description. */
16020 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16021 &can_use_hw_watchpoints, _("\
16022 Set debugger's willingness to use watchpoint hardware."), _("\
16023 Show debugger's willingness to use watchpoint hardware."), _("\
16024 If zero, gdb will not use hardware for new watchpoints, even if\n\
16025 such is available. (However, any hardware watchpoints that were\n\
16026 created before setting this to nonzero, will continue to use watchpoint\n\
16027 hardware.)"),
16028 NULL,
16029 show_can_use_hw_watchpoints,
16030 &setlist, &showlist);
16031
16032 can_use_hw_watchpoints = 1;
16033
16034 /* Tracepoint manipulation commands. */
16035
16036 c = add_com ("trace", class_breakpoint, trace_command, _("\
16037 Set a tracepoint at specified location.\n\
16038 \n"
16039 BREAK_ARGS_HELP ("trace") "\n\
16040 Do \"help tracepoints\" for info on other tracepoint commands."));
16041 set_cmd_completer (c, location_completer);
16042
16043 add_com_alias ("tp", "trace", class_breakpoint, 0);
16044 add_com_alias ("tr", "trace", class_breakpoint, 1);
16045 add_com_alias ("tra", "trace", class_breakpoint, 1);
16046 add_com_alias ("trac", "trace", class_breakpoint, 1);
16047
16048 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16049 Set a fast tracepoint at specified location.\n\
16050 \n"
16051 BREAK_ARGS_HELP ("ftrace") "\n\
16052 Do \"help tracepoints\" for info on other tracepoint commands."));
16053 set_cmd_completer (c, location_completer);
16054
16055 c = add_com ("strace", class_breakpoint, strace_command, _("\
16056 Set a static tracepoint at location or marker.\n\
16057 \n\
16058 strace [LOCATION] [if CONDITION]\n\
16059 LOCATION may be a linespec, explicit, or address location (described below) \n\
16060 or -m MARKER_ID.\n\n\
16061 If a marker id is specified, probe the marker with that name. With\n\
16062 no LOCATION, uses current execution address of the selected stack frame.\n\
16063 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16064 This collects arbitrary user data passed in the probe point call to the\n\
16065 tracing library. You can inspect it when analyzing the trace buffer,\n\
16066 by printing the $_sdata variable like any other convenience variable.\n\
16067 \n\
16068 CONDITION is a boolean expression.\n\
16069 \n" LOCATION_HELP_STRING "\n\n\
16070 Multiple tracepoints at one place are permitted, and useful if their\n\
16071 conditions are different.\n\
16072 \n\
16073 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16074 Do \"help tracepoints\" for info on other tracepoint commands."));
16075 set_cmd_completer (c, location_completer);
16076
16077 add_info ("tracepoints", info_tracepoints_command, _("\
16078 Status of specified tracepoints (all tracepoints if no argument).\n\
16079 Convenience variable \"$tpnum\" contains the number of the\n\
16080 last tracepoint set."));
16081
16082 add_info_alias ("tp", "tracepoints", 1);
16083
16084 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16085 Delete specified tracepoints.\n\
16086 Arguments are tracepoint numbers, separated by spaces.\n\
16087 No argument means delete all tracepoints."),
16088 &deletelist);
16089 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16090
16091 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16092 Disable specified tracepoints.\n\
16093 Arguments are tracepoint numbers, separated by spaces.\n\
16094 No argument means disable all tracepoints."),
16095 &disablelist);
16096 deprecate_cmd (c, "disable");
16097
16098 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16099 Enable specified tracepoints.\n\
16100 Arguments are tracepoint numbers, separated by spaces.\n\
16101 No argument means enable all tracepoints."),
16102 &enablelist);
16103 deprecate_cmd (c, "enable");
16104
16105 add_com ("passcount", class_trace, trace_pass_command, _("\
16106 Set the passcount for a tracepoint.\n\
16107 The trace will end when the tracepoint has been passed 'count' times.\n\
16108 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16109 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16110
16111 add_basic_prefix_cmd ("save", class_breakpoint,
16112 _("Save breakpoint definitions as a script."),
16113 &save_cmdlist, "save ",
16114 0/*allow-unknown*/, &cmdlist);
16115
16116 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16117 Save current breakpoint definitions as a script.\n\
16118 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16119 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16120 session to restore them."),
16121 &save_cmdlist);
16122 set_cmd_completer (c, filename_completer);
16123
16124 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16125 Save current tracepoint definitions as a script.\n\
16126 Use the 'source' command in another debug session to restore them."),
16127 &save_cmdlist);
16128 set_cmd_completer (c, filename_completer);
16129
16130 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16131 deprecate_cmd (c, "save tracepoints");
16132
16133 add_basic_prefix_cmd ("breakpoint", class_maintenance, _("\
16134 Breakpoint specific settings.\n\
16135 Configure various breakpoint-specific variables such as\n\
16136 pending breakpoint behavior."),
16137 &breakpoint_set_cmdlist, "set breakpoint ",
16138 0/*allow-unknown*/, &setlist);
16139 add_show_prefix_cmd ("breakpoint", class_maintenance, _("\
16140 Breakpoint specific settings.\n\
16141 Configure various breakpoint-specific variables such as\n\
16142 pending breakpoint behavior."),
16143 &breakpoint_show_cmdlist, "show breakpoint ",
16144 0/*allow-unknown*/, &showlist);
16145
16146 add_setshow_auto_boolean_cmd ("pending", no_class,
16147 &pending_break_support, _("\
16148 Set debugger's behavior regarding pending breakpoints."), _("\
16149 Show debugger's behavior regarding pending breakpoints."), _("\
16150 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16151 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16152 an error. If auto, an unrecognized breakpoint location results in a\n\
16153 user-query to see if a pending breakpoint should be created."),
16154 NULL,
16155 show_pending_break_support,
16156 &breakpoint_set_cmdlist,
16157 &breakpoint_show_cmdlist);
16158
16159 pending_break_support = AUTO_BOOLEAN_AUTO;
16160
16161 add_setshow_boolean_cmd ("auto-hw", no_class,
16162 &automatic_hardware_breakpoints, _("\
16163 Set automatic usage of hardware breakpoints."), _("\
16164 Show automatic usage of hardware breakpoints."), _("\
16165 If set, the debugger will automatically use hardware breakpoints for\n\
16166 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16167 a warning will be emitted for such breakpoints."),
16168 NULL,
16169 show_automatic_hardware_breakpoints,
16170 &breakpoint_set_cmdlist,
16171 &breakpoint_show_cmdlist);
16172
16173 add_setshow_boolean_cmd ("always-inserted", class_support,
16174 &always_inserted_mode, _("\
16175 Set mode for inserting breakpoints."), _("\
16176 Show mode for inserting breakpoints."), _("\
16177 When this mode is on, breakpoints are inserted immediately as soon as\n\
16178 they're created, kept inserted even when execution stops, and removed\n\
16179 only when the user deletes them. When this mode is off (the default),\n\
16180 breakpoints are inserted only when execution continues, and removed\n\
16181 when execution stops."),
16182 NULL,
16183 &show_always_inserted_mode,
16184 &breakpoint_set_cmdlist,
16185 &breakpoint_show_cmdlist);
16186
16187 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16188 condition_evaluation_enums,
16189 &condition_evaluation_mode_1, _("\
16190 Set mode of breakpoint condition evaluation."), _("\
16191 Show mode of breakpoint condition evaluation."), _("\
16192 When this is set to \"host\", breakpoint conditions will be\n\
16193 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16194 breakpoint conditions will be downloaded to the target (if the target\n\
16195 supports such feature) and conditions will be evaluated on the target's side.\n\
16196 If this is set to \"auto\" (default), this will be automatically set to\n\
16197 \"target\" if it supports condition evaluation, otherwise it will\n\
16198 be set to \"host\"."),
16199 &set_condition_evaluation_mode,
16200 &show_condition_evaluation_mode,
16201 &breakpoint_set_cmdlist,
16202 &breakpoint_show_cmdlist);
16203
16204 add_com ("break-range", class_breakpoint, break_range_command, _("\
16205 Set a breakpoint for an address range.\n\
16206 break-range START-LOCATION, END-LOCATION\n\
16207 where START-LOCATION and END-LOCATION can be one of the following:\n\
16208 LINENUM, for that line in the current file,\n\
16209 FILE:LINENUM, for that line in that file,\n\
16210 +OFFSET, for that number of lines after the current line\n\
16211 or the start of the range\n\
16212 FUNCTION, for the first line in that function,\n\
16213 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16214 *ADDRESS, for the instruction at that address.\n\
16215 \n\
16216 The breakpoint will stop execution of the inferior whenever it executes\n\
16217 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16218 range (including START-LOCATION and END-LOCATION)."));
16219
16220 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16221 Set a dynamic printf at specified location.\n\
16222 dprintf location,format string,arg1,arg2,...\n\
16223 location may be a linespec, explicit, or address location.\n"
16224 "\n" LOCATION_HELP_STRING));
16225 set_cmd_completer (c, location_completer);
16226
16227 add_setshow_enum_cmd ("dprintf-style", class_support,
16228 dprintf_style_enums, &dprintf_style, _("\
16229 Set the style of usage for dynamic printf."), _("\
16230 Show the style of usage for dynamic printf."), _("\
16231 This setting chooses how GDB will do a dynamic printf.\n\
16232 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16233 console, as with the \"printf\" command.\n\
16234 If the value is \"call\", the print is done by calling a function in your\n\
16235 program; by default printf(), but you can choose a different function or\n\
16236 output stream by setting dprintf-function and dprintf-channel."),
16237 update_dprintf_commands, NULL,
16238 &setlist, &showlist);
16239
16240 dprintf_function = xstrdup ("printf");
16241 add_setshow_string_cmd ("dprintf-function", class_support,
16242 &dprintf_function, _("\
16243 Set the function to use for dynamic printf."), _("\
16244 Show the function to use for dynamic printf."), NULL,
16245 update_dprintf_commands, NULL,
16246 &setlist, &showlist);
16247
16248 dprintf_channel = xstrdup ("");
16249 add_setshow_string_cmd ("dprintf-channel", class_support,
16250 &dprintf_channel, _("\
16251 Set the channel to use for dynamic printf."), _("\
16252 Show the channel to use for dynamic printf."), NULL,
16253 update_dprintf_commands, NULL,
16254 &setlist, &showlist);
16255
16256 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16257 &disconnected_dprintf, _("\
16258 Set whether dprintf continues after GDB disconnects."), _("\
16259 Show whether dprintf continues after GDB disconnects."), _("\
16260 Use this to let dprintf commands continue to hit and produce output\n\
16261 even if GDB disconnects or detaches from the target."),
16262 NULL,
16263 NULL,
16264 &setlist, &showlist);
16265
16266 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16267 Target agent only formatted printing, like the C \"printf\" function.\n\
16268 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
16269 This supports most C printf format specifications, like %s, %d, etc.\n\
16270 This is useful for formatted output in user-defined commands."));
16271
16272 automatic_hardware_breakpoints = true;
16273
16274 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed);
16275 gdb::observers::thread_exit.attach (remove_threaded_breakpoints);
16276 }
This page took 0.338102 seconds and 5 git commands to generate.