gdb: remove add_alias_cmd overload that accepts a string
[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 /* See breakpoint.h. */
977
978 void
979 set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
980 bool force)
981 {
982 struct breakpoint *b;
983 ALL_BREAKPOINTS (b)
984 if (b->number == bpnum)
985 {
986 /* Check if this breakpoint has a "stop" method implemented in an
987 extension language. This method and conditions entered into GDB
988 from the CLI are mutually exclusive. */
989 const struct extension_language_defn *extlang
990 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
991
992 if (extlang != NULL)
993 {
994 error (_("Only one stop condition allowed. There is currently"
995 " a %s stop condition defined for this breakpoint."),
996 ext_lang_capitalized_name (extlang));
997 }
998 set_breakpoint_condition (b, exp, from_tty, force);
999
1000 if (is_breakpoint (b))
1001 update_global_location_list (UGLL_MAY_INSERT);
1002
1003 return;
1004 }
1005
1006 error (_("No breakpoint number %d."), bpnum);
1007 }
1008
1009 /* The options for the "condition" command. */
1010
1011 struct condition_command_opts
1012 {
1013 /* For "-force". */
1014 bool force_condition = false;
1015 };
1016
1017 static const gdb::option::option_def condition_command_option_defs[] = {
1018
1019 gdb::option::flag_option_def<condition_command_opts> {
1020 "force",
1021 [] (condition_command_opts *opts) { return &opts->force_condition; },
1022 N_("Set the condition even if it is invalid for all current locations."),
1023 },
1024
1025 };
1026
1027 /* Create an option_def_group for the "condition" options, with
1028 CC_OPTS as context. */
1029
1030 static inline gdb::option::option_def_group
1031 make_condition_command_options_def_group (condition_command_opts *cc_opts)
1032 {
1033 return {{condition_command_option_defs}, cc_opts};
1034 }
1035
1036 /* Completion for the "condition" command. */
1037
1038 static void
1039 condition_completer (struct cmd_list_element *cmd,
1040 completion_tracker &tracker,
1041 const char *text, const char * /*word*/)
1042 {
1043 bool has_no_arguments = (*text == '\0');
1044 condition_command_opts cc_opts;
1045 const auto group = make_condition_command_options_def_group (&cc_opts);
1046 if (gdb::option::complete_options
1047 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1048 return;
1049
1050 text = skip_spaces (text);
1051 const char *space = skip_to_space (text);
1052 if (*space == '\0')
1053 {
1054 int len;
1055 struct breakpoint *b;
1056
1057 if (text[0] == '$')
1058 {
1059 tracker.advance_custom_word_point_by (1);
1060 /* We don't support completion of history indices. */
1061 if (!isdigit (text[1]))
1062 complete_internalvar (tracker, &text[1]);
1063 return;
1064 }
1065
1066 /* Suggest the "-force" flag if no arguments are given. If
1067 arguments were passed, they either already include the flag,
1068 or we are beyond the point of suggesting it because it's
1069 positionally the first argument. */
1070 if (has_no_arguments)
1071 gdb::option::complete_on_all_options (tracker, group);
1072
1073 /* We're completing the breakpoint number. */
1074 len = strlen (text);
1075
1076 ALL_BREAKPOINTS (b)
1077 {
1078 char number[50];
1079
1080 xsnprintf (number, sizeof (number), "%d", b->number);
1081
1082 if (strncmp (number, text, len) == 0)
1083 tracker.add_completion (make_unique_xstrdup (number));
1084 }
1085
1086 return;
1087 }
1088
1089 /* We're completing the expression part. Skip the breakpoint num. */
1090 const char *exp_start = skip_spaces (space);
1091 tracker.advance_custom_word_point_by (exp_start - text);
1092 text = exp_start;
1093 const char *word = advance_to_expression_complete_word_point (tracker, text);
1094 expression_completer (cmd, tracker, text, word);
1095 }
1096
1097 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1098
1099 static void
1100 condition_command (const char *arg, int from_tty)
1101 {
1102 const char *p;
1103 int bnum;
1104
1105 if (arg == 0)
1106 error_no_arg (_("breakpoint number"));
1107
1108 p = arg;
1109
1110 /* Check if the "-force" flag was passed. */
1111 condition_command_opts cc_opts;
1112 const auto group = make_condition_command_options_def_group (&cc_opts);
1113 gdb::option::process_options
1114 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
1115
1116 bnum = get_number (&p);
1117 if (bnum == 0)
1118 error (_("Bad breakpoint argument: '%s'"), arg);
1119
1120 set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
1121 }
1122
1123 /* Check that COMMAND do not contain commands that are suitable
1124 only for tracepoints and not suitable for ordinary breakpoints.
1125 Throw if any such commands is found. */
1126
1127 static void
1128 check_no_tracepoint_commands (struct command_line *commands)
1129 {
1130 struct command_line *c;
1131
1132 for (c = commands; c; c = c->next)
1133 {
1134 if (c->control_type == while_stepping_control)
1135 error (_("The 'while-stepping' command can "
1136 "only be used for tracepoints"));
1137
1138 check_no_tracepoint_commands (c->body_list_0.get ());
1139 check_no_tracepoint_commands (c->body_list_1.get ());
1140
1141 /* Not that command parsing removes leading whitespace and comment
1142 lines and also empty lines. So, we only need to check for
1143 command directly. */
1144 if (strstr (c->line, "collect ") == c->line)
1145 error (_("The 'collect' command can only be used for tracepoints"));
1146
1147 if (strstr (c->line, "teval ") == c->line)
1148 error (_("The 'teval' command can only be used for tracepoints"));
1149 }
1150 }
1151
1152 struct longjmp_breakpoint : public breakpoint
1153 {
1154 ~longjmp_breakpoint () override;
1155 };
1156
1157 /* Encapsulate tests for different types of tracepoints. */
1158
1159 static bool
1160 is_tracepoint_type (bptype type)
1161 {
1162 return (type == bp_tracepoint
1163 || type == bp_fast_tracepoint
1164 || type == bp_static_tracepoint);
1165 }
1166
1167 static bool
1168 is_longjmp_type (bptype type)
1169 {
1170 return type == bp_longjmp || type == bp_exception;
1171 }
1172
1173 /* See breakpoint.h. */
1174
1175 bool
1176 is_tracepoint (const struct breakpoint *b)
1177 {
1178 return is_tracepoint_type (b->type);
1179 }
1180
1181 /* Factory function to create an appropriate instance of breakpoint given
1182 TYPE. */
1183
1184 static std::unique_ptr<breakpoint>
1185 new_breakpoint_from_type (bptype type)
1186 {
1187 breakpoint *b;
1188
1189 if (is_tracepoint_type (type))
1190 b = new tracepoint ();
1191 else if (is_longjmp_type (type))
1192 b = new longjmp_breakpoint ();
1193 else
1194 b = new breakpoint ();
1195
1196 return std::unique_ptr<breakpoint> (b);
1197 }
1198
1199 /* A helper function that validates that COMMANDS are valid for a
1200 breakpoint. This function will throw an exception if a problem is
1201 found. */
1202
1203 static void
1204 validate_commands_for_breakpoint (struct breakpoint *b,
1205 struct command_line *commands)
1206 {
1207 if (is_tracepoint (b))
1208 {
1209 struct tracepoint *t = (struct tracepoint *) b;
1210 struct command_line *c;
1211 struct command_line *while_stepping = 0;
1212
1213 /* Reset the while-stepping step count. The previous commands
1214 might have included a while-stepping action, while the new
1215 ones might not. */
1216 t->step_count = 0;
1217
1218 /* We need to verify that each top-level element of commands is
1219 valid for tracepoints, that there's at most one
1220 while-stepping element, and that the while-stepping's body
1221 has valid tracing commands excluding nested while-stepping.
1222 We also need to validate the tracepoint action line in the
1223 context of the tracepoint --- validate_actionline actually
1224 has side effects, like setting the tracepoint's
1225 while-stepping STEP_COUNT, in addition to checking if the
1226 collect/teval actions parse and make sense in the
1227 tracepoint's context. */
1228 for (c = commands; c; c = c->next)
1229 {
1230 if (c->control_type == while_stepping_control)
1231 {
1232 if (b->type == bp_fast_tracepoint)
1233 error (_("The 'while-stepping' command "
1234 "cannot be used for fast tracepoint"));
1235 else if (b->type == bp_static_tracepoint)
1236 error (_("The 'while-stepping' command "
1237 "cannot be used for static tracepoint"));
1238
1239 if (while_stepping)
1240 error (_("The 'while-stepping' command "
1241 "can be used only once"));
1242 else
1243 while_stepping = c;
1244 }
1245
1246 validate_actionline (c->line, b);
1247 }
1248 if (while_stepping)
1249 {
1250 struct command_line *c2;
1251
1252 gdb_assert (while_stepping->body_list_1 == nullptr);
1253 c2 = while_stepping->body_list_0.get ();
1254 for (; c2; c2 = c2->next)
1255 {
1256 if (c2->control_type == while_stepping_control)
1257 error (_("The 'while-stepping' command cannot be nested"));
1258 }
1259 }
1260 }
1261 else
1262 {
1263 check_no_tracepoint_commands (commands);
1264 }
1265 }
1266
1267 /* Return a vector of all the static tracepoints set at ADDR. The
1268 caller is responsible for releasing the vector. */
1269
1270 std::vector<breakpoint *>
1271 static_tracepoints_here (CORE_ADDR addr)
1272 {
1273 struct breakpoint *b;
1274 std::vector<breakpoint *> found;
1275 struct bp_location *loc;
1276
1277 ALL_BREAKPOINTS (b)
1278 if (b->type == bp_static_tracepoint)
1279 {
1280 for (loc = b->loc; loc; loc = loc->next)
1281 if (loc->address == addr)
1282 found.push_back (b);
1283 }
1284
1285 return found;
1286 }
1287
1288 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1289 validate that only allowed commands are included. */
1290
1291 void
1292 breakpoint_set_commands (struct breakpoint *b,
1293 counted_command_line &&commands)
1294 {
1295 validate_commands_for_breakpoint (b, commands.get ());
1296
1297 b->commands = std::move (commands);
1298 gdb::observers::breakpoint_modified.notify (b);
1299 }
1300
1301 /* Set the internal `silent' flag on the breakpoint. Note that this
1302 is not the same as the "silent" that may appear in the breakpoint's
1303 commands. */
1304
1305 void
1306 breakpoint_set_silent (struct breakpoint *b, int silent)
1307 {
1308 int old_silent = b->silent;
1309
1310 b->silent = silent;
1311 if (old_silent != silent)
1312 gdb::observers::breakpoint_modified.notify (b);
1313 }
1314
1315 /* Set the thread for this breakpoint. If THREAD is -1, make the
1316 breakpoint work for any thread. */
1317
1318 void
1319 breakpoint_set_thread (struct breakpoint *b, int thread)
1320 {
1321 int old_thread = b->thread;
1322
1323 b->thread = thread;
1324 if (old_thread != thread)
1325 gdb::observers::breakpoint_modified.notify (b);
1326 }
1327
1328 /* Set the task for this breakpoint. If TASK is 0, make the
1329 breakpoint work for any task. */
1330
1331 void
1332 breakpoint_set_task (struct breakpoint *b, int task)
1333 {
1334 int old_task = b->task;
1335
1336 b->task = task;
1337 if (old_task != task)
1338 gdb::observers::breakpoint_modified.notify (b);
1339 }
1340
1341 static void
1342 commands_command_1 (const char *arg, int from_tty,
1343 struct command_line *control)
1344 {
1345 counted_command_line cmd;
1346 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1347 NULL after the call to read_command_lines if the user provides an empty
1348 list of command by just typing "end". */
1349 bool cmd_read = false;
1350
1351 std::string new_arg;
1352
1353 if (arg == NULL || !*arg)
1354 {
1355 /* Argument not explicitly given. Synthesize it. */
1356 if (breakpoint_count - prev_breakpoint_count > 1)
1357 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1358 breakpoint_count);
1359 else if (breakpoint_count > 0)
1360 new_arg = string_printf ("%d", breakpoint_count);
1361 }
1362 else
1363 {
1364 /* Create a copy of ARG. This is needed because the "commands"
1365 command may be coming from a script. In that case, the read
1366 line buffer is going to be overwritten in the lambda of
1367 'map_breakpoint_numbers' below when reading the next line
1368 before we are are done parsing the breakpoint numbers. */
1369 new_arg = arg;
1370 }
1371 arg = new_arg.c_str ();
1372
1373 map_breakpoint_numbers
1374 (arg, [&] (breakpoint *b)
1375 {
1376 if (!cmd_read)
1377 {
1378 gdb_assert (cmd == NULL);
1379 if (control != NULL)
1380 cmd = control->body_list_0;
1381 else
1382 {
1383 std::string str
1384 = string_printf (_("Type commands for breakpoint(s) "
1385 "%s, one per line."),
1386 arg);
1387
1388 auto do_validate = [=] (const char *line)
1389 {
1390 validate_actionline (line, b);
1391 };
1392 gdb::function_view<void (const char *)> validator;
1393 if (is_tracepoint (b))
1394 validator = do_validate;
1395
1396 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1397 }
1398 cmd_read = true;
1399 }
1400
1401 /* If a breakpoint was on the list more than once, we don't need to
1402 do anything. */
1403 if (b->commands != cmd)
1404 {
1405 validate_commands_for_breakpoint (b, cmd.get ());
1406 b->commands = cmd;
1407 gdb::observers::breakpoint_modified.notify (b);
1408 }
1409 });
1410 }
1411
1412 static void
1413 commands_command (const char *arg, int from_tty)
1414 {
1415 commands_command_1 (arg, from_tty, NULL);
1416 }
1417
1418 /* Like commands_command, but instead of reading the commands from
1419 input stream, takes them from an already parsed command structure.
1420
1421 This is used by cli-script.c to DTRT with breakpoint commands
1422 that are part of if and while bodies. */
1423 enum command_control_type
1424 commands_from_control_command (const char *arg, struct command_line *cmd)
1425 {
1426 commands_command_1 (arg, 0, cmd);
1427 return simple_control;
1428 }
1429
1430 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1431
1432 static int
1433 bp_location_has_shadow (struct bp_location *bl)
1434 {
1435 if (bl->loc_type != bp_loc_software_breakpoint)
1436 return 0;
1437 if (!bl->inserted)
1438 return 0;
1439 if (bl->target_info.shadow_len == 0)
1440 /* BL isn't valid, or doesn't shadow memory. */
1441 return 0;
1442 return 1;
1443 }
1444
1445 /* Update BUF, which is LEN bytes read from the target address
1446 MEMADDR, by replacing a memory breakpoint with its shadowed
1447 contents.
1448
1449 If READBUF is not NULL, this buffer must not overlap with the of
1450 the breakpoint location's shadow_contents buffer. Otherwise, a
1451 failed assertion internal error will be raised. */
1452
1453 static void
1454 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1455 const gdb_byte *writebuf_org,
1456 ULONGEST memaddr, LONGEST len,
1457 struct bp_target_info *target_info,
1458 struct gdbarch *gdbarch)
1459 {
1460 /* Now do full processing of the found relevant range of elements. */
1461 CORE_ADDR bp_addr = 0;
1462 int bp_size = 0;
1463 int bptoffset = 0;
1464
1465 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1466 current_program_space->aspace, 0))
1467 {
1468 /* The breakpoint is inserted in a different address space. */
1469 return;
1470 }
1471
1472 /* Addresses and length of the part of the breakpoint that
1473 we need to copy. */
1474 bp_addr = target_info->placed_address;
1475 bp_size = target_info->shadow_len;
1476
1477 if (bp_addr + bp_size <= memaddr)
1478 {
1479 /* The breakpoint is entirely before the chunk of memory we are
1480 reading. */
1481 return;
1482 }
1483
1484 if (bp_addr >= memaddr + len)
1485 {
1486 /* The breakpoint is entirely after the chunk of memory we are
1487 reading. */
1488 return;
1489 }
1490
1491 /* Offset within shadow_contents. */
1492 if (bp_addr < memaddr)
1493 {
1494 /* Only copy the second part of the breakpoint. */
1495 bp_size -= memaddr - bp_addr;
1496 bptoffset = memaddr - bp_addr;
1497 bp_addr = memaddr;
1498 }
1499
1500 if (bp_addr + bp_size > memaddr + len)
1501 {
1502 /* Only copy the first part of the breakpoint. */
1503 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1504 }
1505
1506 if (readbuf != NULL)
1507 {
1508 /* Verify that the readbuf buffer does not overlap with the
1509 shadow_contents buffer. */
1510 gdb_assert (target_info->shadow_contents >= readbuf + len
1511 || readbuf >= (target_info->shadow_contents
1512 + target_info->shadow_len));
1513
1514 /* Update the read buffer with this inserted breakpoint's
1515 shadow. */
1516 memcpy (readbuf + bp_addr - memaddr,
1517 target_info->shadow_contents + bptoffset, bp_size);
1518 }
1519 else
1520 {
1521 const unsigned char *bp;
1522 CORE_ADDR addr = target_info->reqstd_address;
1523 int placed_size;
1524
1525 /* Update the shadow with what we want to write to memory. */
1526 memcpy (target_info->shadow_contents + bptoffset,
1527 writebuf_org + bp_addr - memaddr, bp_size);
1528
1529 /* Determine appropriate breakpoint contents and size for this
1530 address. */
1531 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1532
1533 /* Update the final write buffer with this inserted
1534 breakpoint's INSN. */
1535 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1536 }
1537 }
1538
1539 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1540 by replacing any memory breakpoints with their shadowed contents.
1541
1542 If READBUF is not NULL, this buffer must not overlap with any of
1543 the breakpoint location's shadow_contents buffers. Otherwise,
1544 a failed assertion internal error will be raised.
1545
1546 The range of shadowed area by each bp_location is:
1547 bl->address - bp_locations_placed_address_before_address_max
1548 up to bl->address + bp_locations_shadow_len_after_address_max
1549 The range we were requested to resolve shadows for is:
1550 memaddr ... memaddr + len
1551 Thus the safe cutoff boundaries for performance optimization are
1552 memaddr + len <= (bl->address
1553 - bp_locations_placed_address_before_address_max)
1554 and:
1555 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1556
1557 void
1558 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1559 const gdb_byte *writebuf_org,
1560 ULONGEST memaddr, LONGEST len)
1561 {
1562 /* Left boundary, right boundary and median element of our binary
1563 search. */
1564 unsigned bc_l, bc_r, bc;
1565
1566 /* Find BC_L which is a leftmost element which may affect BUF
1567 content. It is safe to report lower value but a failure to
1568 report higher one. */
1569
1570 bc_l = 0;
1571 bc_r = bp_locations_count;
1572 while (bc_l + 1 < bc_r)
1573 {
1574 struct bp_location *bl;
1575
1576 bc = (bc_l + bc_r) / 2;
1577 bl = bp_locations[bc];
1578
1579 /* Check first BL->ADDRESS will not overflow due to the added
1580 constant. Then advance the left boundary only if we are sure
1581 the BC element can in no way affect the BUF content (MEMADDR
1582 to MEMADDR + LEN range).
1583
1584 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1585 offset so that we cannot miss a breakpoint with its shadow
1586 range tail still reaching MEMADDR. */
1587
1588 if ((bl->address + bp_locations_shadow_len_after_address_max
1589 >= bl->address)
1590 && (bl->address + bp_locations_shadow_len_after_address_max
1591 <= memaddr))
1592 bc_l = bc;
1593 else
1594 bc_r = bc;
1595 }
1596
1597 /* Due to the binary search above, we need to make sure we pick the
1598 first location that's at BC_L's address. E.g., if there are
1599 multiple locations at the same address, BC_L may end up pointing
1600 at a duplicate location, and miss the "master"/"inserted"
1601 location. Say, given locations L1, L2 and L3 at addresses A and
1602 B:
1603
1604 L1@A, L2@A, L3@B, ...
1605
1606 BC_L could end up pointing at location L2, while the "master"
1607 location could be L1. Since the `loc->inserted' flag is only set
1608 on "master" locations, we'd forget to restore the shadow of L1
1609 and L2. */
1610 while (bc_l > 0
1611 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1612 bc_l--;
1613
1614 /* Now do full processing of the found relevant range of elements. */
1615
1616 for (bc = bc_l; bc < bp_locations_count; bc++)
1617 {
1618 struct bp_location *bl = bp_locations[bc];
1619
1620 /* bp_location array has BL->OWNER always non-NULL. */
1621 if (bl->owner->type == bp_none)
1622 warning (_("reading through apparently deleted breakpoint #%d?"),
1623 bl->owner->number);
1624
1625 /* Performance optimization: any further element can no longer affect BUF
1626 content. */
1627
1628 if (bl->address >= bp_locations_placed_address_before_address_max
1629 && memaddr + len <= (bl->address
1630 - bp_locations_placed_address_before_address_max))
1631 break;
1632
1633 if (!bp_location_has_shadow (bl))
1634 continue;
1635
1636 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1637 memaddr, len, &bl->target_info, bl->gdbarch);
1638 }
1639 }
1640
1641 /* See breakpoint.h. */
1642
1643 bool
1644 is_breakpoint (const struct breakpoint *bpt)
1645 {
1646 return (bpt->type == bp_breakpoint
1647 || bpt->type == bp_hardware_breakpoint
1648 || bpt->type == bp_dprintf);
1649 }
1650
1651 /* Return true if BPT is of any hardware watchpoint kind. */
1652
1653 static bool
1654 is_hardware_watchpoint (const struct breakpoint *bpt)
1655 {
1656 return (bpt->type == bp_hardware_watchpoint
1657 || bpt->type == bp_read_watchpoint
1658 || bpt->type == bp_access_watchpoint);
1659 }
1660
1661 /* See breakpoint.h. */
1662
1663 bool
1664 is_watchpoint (const struct breakpoint *bpt)
1665 {
1666 return (is_hardware_watchpoint (bpt)
1667 || bpt->type == bp_watchpoint);
1668 }
1669
1670 /* Returns true if the current thread and its running state are safe
1671 to evaluate or update watchpoint B. Watchpoints on local
1672 expressions need to be evaluated in the context of the thread that
1673 was current when the watchpoint was created, and, that thread needs
1674 to be stopped to be able to select the correct frame context.
1675 Watchpoints on global expressions can be evaluated on any thread,
1676 and in any state. It is presently left to the target allowing
1677 memory accesses when threads are running. */
1678
1679 static int
1680 watchpoint_in_thread_scope (struct watchpoint *b)
1681 {
1682 return (b->pspace == current_program_space
1683 && (b->watchpoint_thread == null_ptid
1684 || (inferior_ptid == b->watchpoint_thread
1685 && !inferior_thread ()->executing)));
1686 }
1687
1688 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1689 associated bp_watchpoint_scope breakpoint. */
1690
1691 static void
1692 watchpoint_del_at_next_stop (struct watchpoint *w)
1693 {
1694 if (w->related_breakpoint != w)
1695 {
1696 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1697 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1698 w->related_breakpoint->disposition = disp_del_at_next_stop;
1699 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1700 w->related_breakpoint = w;
1701 }
1702 w->disposition = disp_del_at_next_stop;
1703 }
1704
1705 /* Extract a bitfield value from value VAL using the bit parameters contained in
1706 watchpoint W. */
1707
1708 static struct value *
1709 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1710 {
1711 struct value *bit_val;
1712
1713 if (val == NULL)
1714 return NULL;
1715
1716 bit_val = allocate_value (value_type (val));
1717
1718 unpack_value_bitfield (bit_val,
1719 w->val_bitpos,
1720 w->val_bitsize,
1721 value_contents_for_printing (val),
1722 value_offset (val),
1723 val);
1724
1725 return bit_val;
1726 }
1727
1728 /* Allocate a dummy location and add it to B, which must be a software
1729 watchpoint. This is required because even if a software watchpoint
1730 is not watching any memory, bpstat_stop_status requires a location
1731 to be able to report stops. */
1732
1733 static void
1734 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1735 struct program_space *pspace)
1736 {
1737 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1738
1739 b->loc = allocate_bp_location (b);
1740 b->loc->pspace = pspace;
1741 b->loc->address = -1;
1742 b->loc->length = -1;
1743 }
1744
1745 /* Returns true if B is a software watchpoint that is not watching any
1746 memory (e.g., "watch $pc"). */
1747
1748 static bool
1749 is_no_memory_software_watchpoint (struct breakpoint *b)
1750 {
1751 return (b->type == bp_watchpoint
1752 && b->loc != NULL
1753 && b->loc->next == NULL
1754 && b->loc->address == -1
1755 && b->loc->length == -1);
1756 }
1757
1758 /* Assuming that B is a watchpoint:
1759 - Reparse watchpoint expression, if REPARSE is non-zero
1760 - Evaluate expression and store the result in B->val
1761 - Evaluate the condition if there is one, and store the result
1762 in b->loc->cond.
1763 - Update the list of values that must be watched in B->loc.
1764
1765 If the watchpoint disposition is disp_del_at_next_stop, then do
1766 nothing. If this is local watchpoint that is out of scope, delete
1767 it.
1768
1769 Even with `set breakpoint always-inserted on' the watchpoints are
1770 removed + inserted on each stop here. Normal breakpoints must
1771 never be removed because they might be missed by a running thread
1772 when debugging in non-stop mode. On the other hand, hardware
1773 watchpoints (is_hardware_watchpoint; processed here) are specific
1774 to each LWP since they are stored in each LWP's hardware debug
1775 registers. Therefore, such LWP must be stopped first in order to
1776 be able to modify its hardware watchpoints.
1777
1778 Hardware watchpoints must be reset exactly once after being
1779 presented to the user. It cannot be done sooner, because it would
1780 reset the data used to present the watchpoint hit to the user. And
1781 it must not be done later because it could display the same single
1782 watchpoint hit during multiple GDB stops. Note that the latter is
1783 relevant only to the hardware watchpoint types bp_read_watchpoint
1784 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1785 not user-visible - its hit is suppressed if the memory content has
1786 not changed.
1787
1788 The following constraints influence the location where we can reset
1789 hardware watchpoints:
1790
1791 * target_stopped_by_watchpoint and target_stopped_data_address are
1792 called several times when GDB stops.
1793
1794 [linux]
1795 * Multiple hardware watchpoints can be hit at the same time,
1796 causing GDB to stop. GDB only presents one hardware watchpoint
1797 hit at a time as the reason for stopping, and all the other hits
1798 are presented later, one after the other, each time the user
1799 requests the execution to be resumed. Execution is not resumed
1800 for the threads still having pending hit event stored in
1801 LWP_INFO->STATUS. While the watchpoint is already removed from
1802 the inferior on the first stop the thread hit event is kept being
1803 reported from its cached value by linux_nat_stopped_data_address
1804 until the real thread resume happens after the watchpoint gets
1805 presented and thus its LWP_INFO->STATUS gets reset.
1806
1807 Therefore the hardware watchpoint hit can get safely reset on the
1808 watchpoint removal from inferior. */
1809
1810 static void
1811 update_watchpoint (struct watchpoint *b, int reparse)
1812 {
1813 int within_current_scope;
1814 struct frame_id saved_frame_id;
1815 int frame_saved;
1816
1817 /* If this is a local watchpoint, we only want to check if the
1818 watchpoint frame is in scope if the current thread is the thread
1819 that was used to create the watchpoint. */
1820 if (!watchpoint_in_thread_scope (b))
1821 return;
1822
1823 if (b->disposition == disp_del_at_next_stop)
1824 return;
1825
1826 frame_saved = 0;
1827
1828 /* Determine if the watchpoint is within scope. */
1829 if (b->exp_valid_block == NULL)
1830 within_current_scope = 1;
1831 else
1832 {
1833 struct frame_info *fi = get_current_frame ();
1834 struct gdbarch *frame_arch = get_frame_arch (fi);
1835 CORE_ADDR frame_pc = get_frame_pc (fi);
1836
1837 /* If we're at a point where the stack has been destroyed
1838 (e.g. in a function epilogue), unwinding may not work
1839 properly. Do not attempt to recreate locations at this
1840 point. See similar comments in watchpoint_check. */
1841 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1842 return;
1843
1844 /* Save the current frame's ID so we can restore it after
1845 evaluating the watchpoint expression on its own frame. */
1846 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1847 took a frame parameter, so that we didn't have to change the
1848 selected frame. */
1849 frame_saved = 1;
1850 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1851
1852 fi = frame_find_by_id (b->watchpoint_frame);
1853 within_current_scope = (fi != NULL);
1854 if (within_current_scope)
1855 select_frame (fi);
1856 }
1857
1858 /* We don't free locations. They are stored in the bp_location array
1859 and update_global_location_list will eventually delete them and
1860 remove breakpoints if needed. */
1861 b->loc = NULL;
1862
1863 if (within_current_scope && reparse)
1864 {
1865 const char *s;
1866
1867 b->exp.reset ();
1868 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1869 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1870 /* If the meaning of expression itself changed, the old value is
1871 no longer relevant. We don't want to report a watchpoint hit
1872 to the user when the old value and the new value may actually
1873 be completely different objects. */
1874 b->val = NULL;
1875 b->val_valid = false;
1876
1877 /* Note that unlike with breakpoints, the watchpoint's condition
1878 expression is stored in the breakpoint object, not in the
1879 locations (re)created below. */
1880 if (b->cond_string != NULL)
1881 {
1882 b->cond_exp.reset ();
1883
1884 s = b->cond_string;
1885 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1886 }
1887 }
1888
1889 /* If we failed to parse the expression, for example because
1890 it refers to a global variable in a not-yet-loaded shared library,
1891 don't try to insert watchpoint. We don't automatically delete
1892 such watchpoint, though, since failure to parse expression
1893 is different from out-of-scope watchpoint. */
1894 if (!target_has_execution ())
1895 {
1896 /* Without execution, memory can't change. No use to try and
1897 set watchpoint locations. The watchpoint will be reset when
1898 the target gains execution, through breakpoint_re_set. */
1899 if (!can_use_hw_watchpoints)
1900 {
1901 if (b->ops->works_in_software_mode (b))
1902 b->type = bp_watchpoint;
1903 else
1904 error (_("Can't set read/access watchpoint when "
1905 "hardware watchpoints are disabled."));
1906 }
1907 }
1908 else if (within_current_scope && b->exp)
1909 {
1910 std::vector<value_ref_ptr> val_chain;
1911 struct value *v, *result;
1912 struct program_space *frame_pspace;
1913
1914 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
1915 &val_chain, false);
1916
1917 /* Avoid setting b->val if it's already set. The meaning of
1918 b->val is 'the last value' user saw, and we should update
1919 it only if we reported that last value to user. As it
1920 happens, the code that reports it updates b->val directly.
1921 We don't keep track of the memory value for masked
1922 watchpoints. */
1923 if (!b->val_valid && !is_masked_watchpoint (b))
1924 {
1925 if (b->val_bitsize != 0)
1926 v = extract_bitfield_from_watchpoint_value (b, v);
1927 b->val = release_value (v);
1928 b->val_valid = true;
1929 }
1930
1931 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1932
1933 /* Look at each value on the value chain. */
1934 gdb_assert (!val_chain.empty ());
1935 for (const value_ref_ptr &iter : val_chain)
1936 {
1937 v = iter.get ();
1938
1939 /* If it's a memory location, and GDB actually needed
1940 its contents to evaluate the expression, then we
1941 must watch it. If the first value returned is
1942 still lazy, that means an error occurred reading it;
1943 watch it anyway in case it becomes readable. */
1944 if (VALUE_LVAL (v) == lval_memory
1945 && (v == val_chain[0] || ! value_lazy (v)))
1946 {
1947 struct type *vtype = check_typedef (value_type (v));
1948
1949 /* We only watch structs and arrays if user asked
1950 for it explicitly, never if they just happen to
1951 appear in the middle of some value chain. */
1952 if (v == result
1953 || (vtype->code () != TYPE_CODE_STRUCT
1954 && vtype->code () != TYPE_CODE_ARRAY))
1955 {
1956 CORE_ADDR addr;
1957 enum target_hw_bp_type type;
1958 struct bp_location *loc, **tmp;
1959 int bitpos = 0, bitsize = 0;
1960
1961 if (value_bitsize (v) != 0)
1962 {
1963 /* Extract the bit parameters out from the bitfield
1964 sub-expression. */
1965 bitpos = value_bitpos (v);
1966 bitsize = value_bitsize (v);
1967 }
1968 else if (v == result && b->val_bitsize != 0)
1969 {
1970 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1971 lvalue whose bit parameters are saved in the fields
1972 VAL_BITPOS and VAL_BITSIZE. */
1973 bitpos = b->val_bitpos;
1974 bitsize = b->val_bitsize;
1975 }
1976
1977 addr = value_address (v);
1978 if (bitsize != 0)
1979 {
1980 /* Skip the bytes that don't contain the bitfield. */
1981 addr += bitpos / 8;
1982 }
1983
1984 type = hw_write;
1985 if (b->type == bp_read_watchpoint)
1986 type = hw_read;
1987 else if (b->type == bp_access_watchpoint)
1988 type = hw_access;
1989
1990 loc = allocate_bp_location (b);
1991 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1992 ;
1993 *tmp = loc;
1994 loc->gdbarch = value_type (v)->arch ();
1995
1996 loc->pspace = frame_pspace;
1997 loc->address = address_significant (loc->gdbarch, addr);
1998
1999 if (bitsize != 0)
2000 {
2001 /* Just cover the bytes that make up the bitfield. */
2002 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2003 }
2004 else
2005 loc->length = TYPE_LENGTH (value_type (v));
2006
2007 loc->watchpoint_type = type;
2008 }
2009 }
2010 }
2011
2012 /* Change the type of breakpoint between hardware assisted or
2013 an ordinary watchpoint depending on the hardware support
2014 and free hardware slots. REPARSE is set when the inferior
2015 is started. */
2016 if (reparse)
2017 {
2018 int reg_cnt;
2019 enum bp_loc_type loc_type;
2020 struct bp_location *bl;
2021
2022 reg_cnt = can_use_hardware_watchpoint (val_chain);
2023
2024 if (reg_cnt)
2025 {
2026 int i, target_resources_ok, other_type_used;
2027 enum bptype type;
2028
2029 /* Use an exact watchpoint when there's only one memory region to be
2030 watched, and only one debug register is needed to watch it. */
2031 b->exact = target_exact_watchpoints && reg_cnt == 1;
2032
2033 /* We need to determine how many resources are already
2034 used for all other hardware watchpoints plus this one
2035 to see if we still have enough resources to also fit
2036 this watchpoint in as well. */
2037
2038 /* If this is a software watchpoint, we try to turn it
2039 to a hardware one -- count resources as if B was of
2040 hardware watchpoint type. */
2041 type = b->type;
2042 if (type == bp_watchpoint)
2043 type = bp_hardware_watchpoint;
2044
2045 /* This watchpoint may or may not have been placed on
2046 the list yet at this point (it won't be in the list
2047 if we're trying to create it for the first time,
2048 through watch_command), so always account for it
2049 manually. */
2050
2051 /* Count resources used by all watchpoints except B. */
2052 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
2053
2054 /* Add in the resources needed for B. */
2055 i += hw_watchpoint_use_count (b);
2056
2057 target_resources_ok
2058 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2059 if (target_resources_ok <= 0)
2060 {
2061 int sw_mode = b->ops->works_in_software_mode (b);
2062
2063 if (target_resources_ok == 0 && !sw_mode)
2064 error (_("Target does not support this type of "
2065 "hardware watchpoint."));
2066 else if (target_resources_ok < 0 && !sw_mode)
2067 error (_("There are not enough available hardware "
2068 "resources for this watchpoint."));
2069
2070 /* Downgrade to software watchpoint. */
2071 b->type = bp_watchpoint;
2072 }
2073 else
2074 {
2075 /* If this was a software watchpoint, we've just
2076 found we have enough resources to turn it to a
2077 hardware watchpoint. Otherwise, this is a
2078 nop. */
2079 b->type = type;
2080 }
2081 }
2082 else if (!b->ops->works_in_software_mode (b))
2083 {
2084 if (!can_use_hw_watchpoints)
2085 error (_("Can't set read/access watchpoint when "
2086 "hardware watchpoints are disabled."));
2087 else
2088 error (_("Expression cannot be implemented with "
2089 "read/access watchpoint."));
2090 }
2091 else
2092 b->type = bp_watchpoint;
2093
2094 loc_type = (b->type == bp_watchpoint? bp_loc_other
2095 : bp_loc_hardware_watchpoint);
2096 for (bl = b->loc; bl; bl = bl->next)
2097 bl->loc_type = loc_type;
2098 }
2099
2100 /* If a software watchpoint is not watching any memory, then the
2101 above left it without any location set up. But,
2102 bpstat_stop_status requires a location to be able to report
2103 stops, so make sure there's at least a dummy one. */
2104 if (b->type == bp_watchpoint && b->loc == NULL)
2105 software_watchpoint_add_no_memory_location (b, frame_pspace);
2106 }
2107 else if (!within_current_scope)
2108 {
2109 printf_filtered (_("\
2110 Watchpoint %d deleted because the program has left the block\n\
2111 in which its expression is valid.\n"),
2112 b->number);
2113 watchpoint_del_at_next_stop (b);
2114 }
2115
2116 /* Restore the selected frame. */
2117 if (frame_saved)
2118 select_frame (frame_find_by_id (saved_frame_id));
2119 }
2120
2121
2122 /* Returns 1 iff breakpoint location should be
2123 inserted in the inferior. We don't differentiate the type of BL's owner
2124 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2125 breakpoint_ops is not defined, because in insert_bp_location,
2126 tracepoint's insert_location will not be called. */
2127 static int
2128 should_be_inserted (struct bp_location *bl)
2129 {
2130 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2131 return 0;
2132
2133 if (bl->owner->disposition == disp_del_at_next_stop)
2134 return 0;
2135
2136 if (!bl->enabled || bl->disabled_by_cond
2137 || bl->shlib_disabled || bl->duplicate)
2138 return 0;
2139
2140 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2141 return 0;
2142
2143 /* This is set for example, when we're attached to the parent of a
2144 vfork, and have detached from the child. The child is running
2145 free, and we expect it to do an exec or exit, at which point the
2146 OS makes the parent schedulable again (and the target reports
2147 that the vfork is done). Until the child is done with the shared
2148 memory region, do not insert breakpoints in the parent, otherwise
2149 the child could still trip on the parent's breakpoints. Since
2150 the parent is blocked anyway, it won't miss any breakpoint. */
2151 if (bl->pspace->breakpoints_not_allowed)
2152 return 0;
2153
2154 /* Don't insert a breakpoint if we're trying to step past its
2155 location, except if the breakpoint is a single-step breakpoint,
2156 and the breakpoint's thread is the thread which is stepping past
2157 a breakpoint. */
2158 if ((bl->loc_type == bp_loc_software_breakpoint
2159 || bl->loc_type == bp_loc_hardware_breakpoint)
2160 && stepping_past_instruction_at (bl->pspace->aspace,
2161 bl->address)
2162 /* The single-step breakpoint may be inserted at the location
2163 we're trying to step if the instruction branches to itself.
2164 However, the instruction won't be executed at all and it may
2165 break the semantics of the instruction, for example, the
2166 instruction is a conditional branch or updates some flags.
2167 We can't fix it unless GDB is able to emulate the instruction
2168 or switch to displaced stepping. */
2169 && !(bl->owner->type == bp_single_step
2170 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2171 {
2172 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2173 paddress (bl->gdbarch, bl->address));
2174 return 0;
2175 }
2176
2177 /* Don't insert watchpoints if we're trying to step past the
2178 instruction that triggered one. */
2179 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2180 && stepping_past_nonsteppable_watchpoint ())
2181 {
2182 infrun_debug_printf ("stepping past non-steppable watchpoint. "
2183 "skipping watchpoint at %s:%d",
2184 paddress (bl->gdbarch, bl->address), bl->length);
2185 return 0;
2186 }
2187
2188 return 1;
2189 }
2190
2191 /* Same as should_be_inserted but does the check assuming
2192 that the location is not duplicated. */
2193
2194 static int
2195 unduplicated_should_be_inserted (struct bp_location *bl)
2196 {
2197 int result;
2198 const int save_duplicate = bl->duplicate;
2199
2200 bl->duplicate = 0;
2201 result = should_be_inserted (bl);
2202 bl->duplicate = save_duplicate;
2203 return result;
2204 }
2205
2206 /* Parses a conditional described by an expression COND into an
2207 agent expression bytecode suitable for evaluation
2208 by the bytecode interpreter. Return NULL if there was
2209 any error during parsing. */
2210
2211 static agent_expr_up
2212 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2213 {
2214 if (cond == NULL)
2215 return NULL;
2216
2217 agent_expr_up aexpr;
2218
2219 /* We don't want to stop processing, so catch any errors
2220 that may show up. */
2221 try
2222 {
2223 aexpr = gen_eval_for_expr (scope, cond);
2224 }
2225
2226 catch (const gdb_exception_error &ex)
2227 {
2228 /* If we got here, it means the condition could not be parsed to a valid
2229 bytecode expression and thus can't be evaluated on the target's side.
2230 It's no use iterating through the conditions. */
2231 }
2232
2233 /* We have a valid agent expression. */
2234 return aexpr;
2235 }
2236
2237 /* Based on location BL, create a list of breakpoint conditions to be
2238 passed on to the target. If we have duplicated locations with different
2239 conditions, we will add such conditions to the list. The idea is that the
2240 target will evaluate the list of conditions and will only notify GDB when
2241 one of them is true. */
2242
2243 static void
2244 build_target_condition_list (struct bp_location *bl)
2245 {
2246 struct bp_location **locp = NULL, **loc2p;
2247 int null_condition_or_parse_error = 0;
2248 int modified = bl->needs_update;
2249 struct bp_location *loc;
2250
2251 /* Release conditions left over from a previous insert. */
2252 bl->target_info.conditions.clear ();
2253
2254 /* This is only meaningful if the target is
2255 evaluating conditions and if the user has
2256 opted for condition evaluation on the target's
2257 side. */
2258 if (gdb_evaluates_breakpoint_condition_p ()
2259 || !target_supports_evaluation_of_breakpoint_conditions ())
2260 return;
2261
2262 /* Do a first pass to check for locations with no assigned
2263 conditions or conditions that fail to parse to a valid agent
2264 expression bytecode. If any of these happen, then it's no use to
2265 send conditions to the target since this location will always
2266 trigger and generate a response back to GDB. Note we consider
2267 all locations at the same address irrespective of type, i.e.,
2268 even if the locations aren't considered duplicates (e.g.,
2269 software breakpoint and hardware breakpoint at the same
2270 address). */
2271 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2272 {
2273 loc = (*loc2p);
2274 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2275 {
2276 if (modified)
2277 {
2278 /* Re-parse the conditions since something changed. In that
2279 case we already freed the condition bytecodes (see
2280 force_breakpoint_reinsertion). We just
2281 need to parse the condition to bytecodes again. */
2282 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2283 loc->cond.get ());
2284 }
2285
2286 /* If we have a NULL bytecode expression, it means something
2287 went wrong or we have a null condition expression. */
2288 if (!loc->cond_bytecode)
2289 {
2290 null_condition_or_parse_error = 1;
2291 break;
2292 }
2293 }
2294 }
2295
2296 /* If any of these happened, it means we will have to evaluate the conditions
2297 for the location's address on gdb's side. It is no use keeping bytecodes
2298 for all the other duplicate locations, thus we free all of them here.
2299
2300 This is so we have a finer control over which locations' conditions are
2301 being evaluated by GDB or the remote stub. */
2302 if (null_condition_or_parse_error)
2303 {
2304 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2305 {
2306 loc = (*loc2p);
2307 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2308 {
2309 /* Only go as far as the first NULL bytecode is
2310 located. */
2311 if (!loc->cond_bytecode)
2312 return;
2313
2314 loc->cond_bytecode.reset ();
2315 }
2316 }
2317 }
2318
2319 /* No NULL conditions or failed bytecode generation. Build a
2320 condition list for this location's address. If we have software
2321 and hardware locations at the same address, they aren't
2322 considered duplicates, but we still marge all the conditions
2323 anyway, as it's simpler, and doesn't really make a practical
2324 difference. */
2325 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2326 {
2327 loc = (*loc2p);
2328 if (loc->cond
2329 && is_breakpoint (loc->owner)
2330 && loc->pspace->num == bl->pspace->num
2331 && loc->owner->enable_state == bp_enabled
2332 && loc->enabled
2333 && !loc->disabled_by_cond)
2334 {
2335 /* Add the condition to the vector. This will be used later
2336 to send the conditions to the target. */
2337 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2338 }
2339 }
2340
2341 return;
2342 }
2343
2344 /* Parses a command described by string CMD into an agent expression
2345 bytecode suitable for evaluation by the bytecode interpreter.
2346 Return NULL if there was any error during parsing. */
2347
2348 static agent_expr_up
2349 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2350 {
2351 const char *cmdrest;
2352 const char *format_start, *format_end;
2353 struct gdbarch *gdbarch = get_current_arch ();
2354
2355 if (cmd == NULL)
2356 return NULL;
2357
2358 cmdrest = cmd;
2359
2360 if (*cmdrest == ',')
2361 ++cmdrest;
2362 cmdrest = skip_spaces (cmdrest);
2363
2364 if (*cmdrest++ != '"')
2365 error (_("No format string following the location"));
2366
2367 format_start = cmdrest;
2368
2369 format_pieces fpieces (&cmdrest);
2370
2371 format_end = cmdrest;
2372
2373 if (*cmdrest++ != '"')
2374 error (_("Bad format string, non-terminated '\"'."));
2375
2376 cmdrest = skip_spaces (cmdrest);
2377
2378 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2379 error (_("Invalid argument syntax"));
2380
2381 if (*cmdrest == ',')
2382 cmdrest++;
2383 cmdrest = skip_spaces (cmdrest);
2384
2385 /* For each argument, make an expression. */
2386
2387 std::vector<struct expression *> argvec;
2388 while (*cmdrest != '\0')
2389 {
2390 const char *cmd1;
2391
2392 cmd1 = cmdrest;
2393 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2394 argvec.push_back (expr.release ());
2395 cmdrest = cmd1;
2396 if (*cmdrest == ',')
2397 ++cmdrest;
2398 }
2399
2400 agent_expr_up aexpr;
2401
2402 /* We don't want to stop processing, so catch any errors
2403 that may show up. */
2404 try
2405 {
2406 aexpr = gen_printf (scope, gdbarch, 0, 0,
2407 format_start, format_end - format_start,
2408 argvec.size (), argvec.data ());
2409 }
2410 catch (const gdb_exception_error &ex)
2411 {
2412 /* If we got here, it means the command could not be parsed to a valid
2413 bytecode expression and thus can't be evaluated on the target's side.
2414 It's no use iterating through the other commands. */
2415 }
2416
2417 /* We have a valid agent expression, return it. */
2418 return aexpr;
2419 }
2420
2421 /* Based on location BL, create a list of breakpoint commands to be
2422 passed on to the target. If we have duplicated locations with
2423 different commands, we will add any such to the list. */
2424
2425 static void
2426 build_target_command_list (struct bp_location *bl)
2427 {
2428 struct bp_location **locp = NULL, **loc2p;
2429 int null_command_or_parse_error = 0;
2430 int modified = bl->needs_update;
2431 struct bp_location *loc;
2432
2433 /* Clear commands left over from a previous insert. */
2434 bl->target_info.tcommands.clear ();
2435
2436 if (!target_can_run_breakpoint_commands ())
2437 return;
2438
2439 /* For now, limit to agent-style dprintf breakpoints. */
2440 if (dprintf_style != dprintf_style_agent)
2441 return;
2442
2443 /* For now, if we have any location at the same address that isn't a
2444 dprintf, don't install the target-side commands, as that would
2445 make the breakpoint not be reported to the core, and we'd lose
2446 control. */
2447 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2448 {
2449 loc = (*loc2p);
2450 if (is_breakpoint (loc->owner)
2451 && loc->pspace->num == bl->pspace->num
2452 && loc->owner->type != bp_dprintf)
2453 return;
2454 }
2455
2456 /* Do a first pass to check for locations with no assigned
2457 conditions or conditions that fail to parse to a valid agent expression
2458 bytecode. If any of these happen, then it's no use to send conditions
2459 to the target since this location will always trigger and generate a
2460 response back to GDB. */
2461 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2462 {
2463 loc = (*loc2p);
2464 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2465 {
2466 if (modified)
2467 {
2468 /* Re-parse the commands since something changed. In that
2469 case we already freed the command bytecodes (see
2470 force_breakpoint_reinsertion). We just
2471 need to parse the command to bytecodes again. */
2472 loc->cmd_bytecode
2473 = parse_cmd_to_aexpr (bl->address,
2474 loc->owner->extra_string);
2475 }
2476
2477 /* If we have a NULL bytecode expression, it means something
2478 went wrong or we have a null command expression. */
2479 if (!loc->cmd_bytecode)
2480 {
2481 null_command_or_parse_error = 1;
2482 break;
2483 }
2484 }
2485 }
2486
2487 /* If anything failed, then we're not doing target-side commands,
2488 and so clean up. */
2489 if (null_command_or_parse_error)
2490 {
2491 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2492 {
2493 loc = (*loc2p);
2494 if (is_breakpoint (loc->owner)
2495 && loc->pspace->num == bl->pspace->num)
2496 {
2497 /* Only go as far as the first NULL bytecode is
2498 located. */
2499 if (loc->cmd_bytecode == NULL)
2500 return;
2501
2502 loc->cmd_bytecode.reset ();
2503 }
2504 }
2505 }
2506
2507 /* No NULL commands or failed bytecode generation. Build a command
2508 list for all duplicate locations at this location's address.
2509 Note that here we must care for whether the breakpoint location
2510 types are considered duplicates, otherwise, say, if we have a
2511 software and hardware location at the same address, the target
2512 could end up running the commands twice. For the moment, we only
2513 support targets-side commands with dprintf, but it doesn't hurt
2514 to be pedantically correct in case that changes. */
2515 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2516 {
2517 loc = (*loc2p);
2518 if (breakpoint_locations_match (bl, loc)
2519 && loc->owner->extra_string
2520 && is_breakpoint (loc->owner)
2521 && loc->pspace->num == bl->pspace->num
2522 && loc->owner->enable_state == bp_enabled
2523 && loc->enabled
2524 && !loc->disabled_by_cond)
2525 {
2526 /* Add the command to the vector. This will be used later
2527 to send the commands to the target. */
2528 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2529 }
2530 }
2531
2532 bl->target_info.persist = 0;
2533 /* Maybe flag this location as persistent. */
2534 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2535 bl->target_info.persist = 1;
2536 }
2537
2538 /* Return the kind of breakpoint on address *ADDR. Get the kind
2539 of breakpoint according to ADDR except single-step breakpoint.
2540 Get the kind of single-step breakpoint according to the current
2541 registers state. */
2542
2543 static int
2544 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2545 {
2546 if (bl->owner->type == bp_single_step)
2547 {
2548 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2549 struct regcache *regcache;
2550
2551 regcache = get_thread_regcache (thr);
2552
2553 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2554 regcache, addr);
2555 }
2556 else
2557 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2558 }
2559
2560 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2561 location. Any error messages are printed to TMP_ERROR_STREAM; and
2562 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2563 Returns 0 for success, 1 if the bp_location type is not supported or
2564 -1 for failure.
2565
2566 NOTE drow/2003-09-09: This routine could be broken down to an
2567 object-style method for each breakpoint or catchpoint type. */
2568 static int
2569 insert_bp_location (struct bp_location *bl,
2570 struct ui_file *tmp_error_stream,
2571 int *disabled_breaks,
2572 int *hw_breakpoint_error,
2573 int *hw_bp_error_explained_already)
2574 {
2575 gdb_exception bp_excpt;
2576
2577 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2578 return 0;
2579
2580 /* Note we don't initialize bl->target_info, as that wipes out
2581 the breakpoint location's shadow_contents if the breakpoint
2582 is still inserted at that location. This in turn breaks
2583 target_read_memory which depends on these buffers when
2584 a memory read is requested at the breakpoint location:
2585 Once the target_info has been wiped, we fail to see that
2586 we have a breakpoint inserted at that address and thus
2587 read the breakpoint instead of returning the data saved in
2588 the breakpoint location's shadow contents. */
2589 bl->target_info.reqstd_address = bl->address;
2590 bl->target_info.placed_address_space = bl->pspace->aspace;
2591 bl->target_info.length = bl->length;
2592
2593 /* When working with target-side conditions, we must pass all the conditions
2594 for the same breakpoint address down to the target since GDB will not
2595 insert those locations. With a list of breakpoint conditions, the target
2596 can decide when to stop and notify GDB. */
2597
2598 if (is_breakpoint (bl->owner))
2599 {
2600 build_target_condition_list (bl);
2601 build_target_command_list (bl);
2602 /* Reset the modification marker. */
2603 bl->needs_update = 0;
2604 }
2605
2606 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2607 set at a read-only address, then a breakpoint location will have
2608 been changed to hardware breakpoint before we get here. If it is
2609 "off" however, error out before actually trying to insert the
2610 breakpoint, with a nicer error message. */
2611 if (bl->loc_type == bp_loc_software_breakpoint
2612 && !automatic_hardware_breakpoints)
2613 {
2614 mem_region *mr = lookup_mem_region (bl->address);
2615
2616 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2617 {
2618 fprintf_unfiltered (tmp_error_stream,
2619 _("Cannot insert breakpoint %d.\n"
2620 "Cannot set software breakpoint "
2621 "at read-only address %s\n"),
2622 bl->owner->number,
2623 paddress (bl->gdbarch, bl->address));
2624 return 1;
2625 }
2626 }
2627
2628 if (bl->loc_type == bp_loc_software_breakpoint
2629 || bl->loc_type == bp_loc_hardware_breakpoint)
2630 {
2631 /* First check to see if we have to handle an overlay. */
2632 if (overlay_debugging == ovly_off
2633 || bl->section == NULL
2634 || !(section_is_overlay (bl->section)))
2635 {
2636 /* No overlay handling: just set the breakpoint. */
2637 try
2638 {
2639 int val;
2640
2641 val = bl->owner->ops->insert_location (bl);
2642 if (val)
2643 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2644 }
2645 catch (gdb_exception &e)
2646 {
2647 bp_excpt = std::move (e);
2648 }
2649 }
2650 else
2651 {
2652 /* This breakpoint is in an overlay section.
2653 Shall we set a breakpoint at the LMA? */
2654 if (!overlay_events_enabled)
2655 {
2656 /* Yes -- overlay event support is not active,
2657 so we must try to set a breakpoint at the LMA.
2658 This will not work for a hardware breakpoint. */
2659 if (bl->loc_type == bp_loc_hardware_breakpoint)
2660 warning (_("hardware breakpoint %d not supported in overlay!"),
2661 bl->owner->number);
2662 else
2663 {
2664 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2665 bl->section);
2666 /* Set a software (trap) breakpoint at the LMA. */
2667 bl->overlay_target_info = bl->target_info;
2668 bl->overlay_target_info.reqstd_address = addr;
2669
2670 /* No overlay handling: just set the breakpoint. */
2671 try
2672 {
2673 int val;
2674
2675 bl->overlay_target_info.kind
2676 = breakpoint_kind (bl, &addr);
2677 bl->overlay_target_info.placed_address = addr;
2678 val = target_insert_breakpoint (bl->gdbarch,
2679 &bl->overlay_target_info);
2680 if (val)
2681 bp_excpt
2682 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2683 }
2684 catch (gdb_exception &e)
2685 {
2686 bp_excpt = std::move (e);
2687 }
2688
2689 if (bp_excpt.reason != 0)
2690 fprintf_unfiltered (tmp_error_stream,
2691 "Overlay breakpoint %d "
2692 "failed: in ROM?\n",
2693 bl->owner->number);
2694 }
2695 }
2696 /* Shall we set a breakpoint at the VMA? */
2697 if (section_is_mapped (bl->section))
2698 {
2699 /* Yes. This overlay section is mapped into memory. */
2700 try
2701 {
2702 int val;
2703
2704 val = bl->owner->ops->insert_location (bl);
2705 if (val)
2706 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2707 }
2708 catch (gdb_exception &e)
2709 {
2710 bp_excpt = std::move (e);
2711 }
2712 }
2713 else
2714 {
2715 /* No. This breakpoint will not be inserted.
2716 No error, but do not mark the bp as 'inserted'. */
2717 return 0;
2718 }
2719 }
2720
2721 if (bp_excpt.reason != 0)
2722 {
2723 /* Can't set the breakpoint. */
2724
2725 /* In some cases, we might not be able to insert a
2726 breakpoint in a shared library that has already been
2727 removed, but we have not yet processed the shlib unload
2728 event. Unfortunately, some targets that implement
2729 breakpoint insertion themselves can't tell why the
2730 breakpoint insertion failed (e.g., the remote target
2731 doesn't define error codes), so we must treat generic
2732 errors as memory errors. */
2733 if (bp_excpt.reason == RETURN_ERROR
2734 && (bp_excpt.error == GENERIC_ERROR
2735 || bp_excpt.error == MEMORY_ERROR)
2736 && bl->loc_type == bp_loc_software_breakpoint
2737 && (solib_name_from_address (bl->pspace, bl->address)
2738 || shared_objfile_contains_address_p (bl->pspace,
2739 bl->address)))
2740 {
2741 /* See also: disable_breakpoints_in_shlibs. */
2742 bl->shlib_disabled = 1;
2743 gdb::observers::breakpoint_modified.notify (bl->owner);
2744 if (!*disabled_breaks)
2745 {
2746 fprintf_unfiltered (tmp_error_stream,
2747 "Cannot insert breakpoint %d.\n",
2748 bl->owner->number);
2749 fprintf_unfiltered (tmp_error_stream,
2750 "Temporarily disabling shared "
2751 "library breakpoints:\n");
2752 }
2753 *disabled_breaks = 1;
2754 fprintf_unfiltered (tmp_error_stream,
2755 "breakpoint #%d\n", bl->owner->number);
2756 return 0;
2757 }
2758 else
2759 {
2760 if (bl->loc_type == bp_loc_hardware_breakpoint)
2761 {
2762 *hw_breakpoint_error = 1;
2763 *hw_bp_error_explained_already = bp_excpt.message != NULL;
2764 fprintf_unfiltered (tmp_error_stream,
2765 "Cannot insert hardware breakpoint %d%s",
2766 bl->owner->number,
2767 bp_excpt.message ? ":" : ".\n");
2768 if (bp_excpt.message != NULL)
2769 fprintf_unfiltered (tmp_error_stream, "%s.\n",
2770 bp_excpt.what ());
2771 }
2772 else
2773 {
2774 if (bp_excpt.message == NULL)
2775 {
2776 std::string message
2777 = memory_error_message (TARGET_XFER_E_IO,
2778 bl->gdbarch, bl->address);
2779
2780 fprintf_unfiltered (tmp_error_stream,
2781 "Cannot insert breakpoint %d.\n"
2782 "%s\n",
2783 bl->owner->number, message.c_str ());
2784 }
2785 else
2786 {
2787 fprintf_unfiltered (tmp_error_stream,
2788 "Cannot insert breakpoint %d: %s\n",
2789 bl->owner->number,
2790 bp_excpt.what ());
2791 }
2792 }
2793 return 1;
2794
2795 }
2796 }
2797 else
2798 bl->inserted = 1;
2799
2800 return 0;
2801 }
2802
2803 else if (bl->loc_type == bp_loc_hardware_watchpoint
2804 /* NOTE drow/2003-09-08: This state only exists for removing
2805 watchpoints. It's not clear that it's necessary... */
2806 && bl->owner->disposition != disp_del_at_next_stop)
2807 {
2808 int val;
2809
2810 gdb_assert (bl->owner->ops != NULL
2811 && bl->owner->ops->insert_location != NULL);
2812
2813 val = bl->owner->ops->insert_location (bl);
2814
2815 /* If trying to set a read-watchpoint, and it turns out it's not
2816 supported, try emulating one with an access watchpoint. */
2817 if (val == 1 && bl->watchpoint_type == hw_read)
2818 {
2819 struct bp_location *loc, **loc_temp;
2820
2821 /* But don't try to insert it, if there's already another
2822 hw_access location that would be considered a duplicate
2823 of this one. */
2824 ALL_BP_LOCATIONS (loc, loc_temp)
2825 if (loc != bl
2826 && loc->watchpoint_type == hw_access
2827 && watchpoint_locations_match (bl, loc))
2828 {
2829 bl->duplicate = 1;
2830 bl->inserted = 1;
2831 bl->target_info = loc->target_info;
2832 bl->watchpoint_type = hw_access;
2833 val = 0;
2834 break;
2835 }
2836
2837 if (val == 1)
2838 {
2839 bl->watchpoint_type = hw_access;
2840 val = bl->owner->ops->insert_location (bl);
2841
2842 if (val)
2843 /* Back to the original value. */
2844 bl->watchpoint_type = hw_read;
2845 }
2846 }
2847
2848 bl->inserted = (val == 0);
2849 }
2850
2851 else if (bl->owner->type == bp_catchpoint)
2852 {
2853 int val;
2854
2855 gdb_assert (bl->owner->ops != NULL
2856 && bl->owner->ops->insert_location != NULL);
2857
2858 val = bl->owner->ops->insert_location (bl);
2859 if (val)
2860 {
2861 bl->owner->enable_state = bp_disabled;
2862
2863 if (val == 1)
2864 warning (_("\
2865 Error inserting catchpoint %d: Your system does not support this type\n\
2866 of catchpoint."), bl->owner->number);
2867 else
2868 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2869 }
2870
2871 bl->inserted = (val == 0);
2872
2873 /* We've already printed an error message if there was a problem
2874 inserting this catchpoint, and we've disabled the catchpoint,
2875 so just return success. */
2876 return 0;
2877 }
2878
2879 return 0;
2880 }
2881
2882 /* This function is called when program space PSPACE is about to be
2883 deleted. It takes care of updating breakpoints to not reference
2884 PSPACE anymore. */
2885
2886 void
2887 breakpoint_program_space_exit (struct program_space *pspace)
2888 {
2889 struct breakpoint *b, *b_temp;
2890 struct bp_location *loc, **loc_temp;
2891
2892 /* Remove any breakpoint that was set through this program space. */
2893 ALL_BREAKPOINTS_SAFE (b, b_temp)
2894 {
2895 if (b->pspace == pspace)
2896 delete_breakpoint (b);
2897 }
2898
2899 /* Breakpoints set through other program spaces could have locations
2900 bound to PSPACE as well. Remove those. */
2901 ALL_BP_LOCATIONS (loc, loc_temp)
2902 {
2903 struct bp_location *tmp;
2904
2905 if (loc->pspace == pspace)
2906 {
2907 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2908 if (loc->owner->loc == loc)
2909 loc->owner->loc = loc->next;
2910 else
2911 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2912 if (tmp->next == loc)
2913 {
2914 tmp->next = loc->next;
2915 break;
2916 }
2917 }
2918 }
2919
2920 /* Now update the global location list to permanently delete the
2921 removed locations above. */
2922 update_global_location_list (UGLL_DONT_INSERT);
2923 }
2924
2925 /* Make sure all breakpoints are inserted in inferior.
2926 Throws exception on any error.
2927 A breakpoint that is already inserted won't be inserted
2928 again, so calling this function twice is safe. */
2929 void
2930 insert_breakpoints (void)
2931 {
2932 struct breakpoint *bpt;
2933
2934 ALL_BREAKPOINTS (bpt)
2935 if (is_hardware_watchpoint (bpt))
2936 {
2937 struct watchpoint *w = (struct watchpoint *) bpt;
2938
2939 update_watchpoint (w, 0 /* don't reparse. */);
2940 }
2941
2942 /* Updating watchpoints creates new locations, so update the global
2943 location list. Explicitly tell ugll to insert locations and
2944 ignore breakpoints_always_inserted_mode. Also,
2945 update_global_location_list tries to "upgrade" software
2946 breakpoints to hardware breakpoints to handle "set breakpoint
2947 auto-hw", so we need to call it even if we don't have new
2948 locations. */
2949 update_global_location_list (UGLL_INSERT);
2950 }
2951
2952 /* Invoke CALLBACK for each of bp_location. */
2953
2954 void
2955 iterate_over_bp_locations (gdb::function_view<void (bp_location *)> callback)
2956 {
2957 struct bp_location *loc, **loc_tmp;
2958
2959 ALL_BP_LOCATIONS (loc, loc_tmp)
2960 {
2961 callback (loc);
2962 }
2963 }
2964
2965 /* This is used when we need to synch breakpoint conditions between GDB and the
2966 target. It is the case with deleting and disabling of breakpoints when using
2967 always-inserted mode. */
2968
2969 static void
2970 update_inserted_breakpoint_locations (void)
2971 {
2972 struct bp_location *bl, **blp_tmp;
2973 int error_flag = 0;
2974 int val = 0;
2975 int disabled_breaks = 0;
2976 int hw_breakpoint_error = 0;
2977 int hw_bp_details_reported = 0;
2978
2979 string_file tmp_error_stream;
2980
2981 /* Explicitly mark the warning -- this will only be printed if
2982 there was an error. */
2983 tmp_error_stream.puts ("Warning:\n");
2984
2985 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2986
2987 ALL_BP_LOCATIONS (bl, blp_tmp)
2988 {
2989 /* We only want to update software breakpoints and hardware
2990 breakpoints. */
2991 if (!is_breakpoint (bl->owner))
2992 continue;
2993
2994 /* We only want to update locations that are already inserted
2995 and need updating. This is to avoid unwanted insertion during
2996 deletion of breakpoints. */
2997 if (!bl->inserted || !bl->needs_update)
2998 continue;
2999
3000 switch_to_program_space_and_thread (bl->pspace);
3001
3002 /* For targets that support global breakpoints, there's no need
3003 to select an inferior to insert breakpoint to. In fact, even
3004 if we aren't attached to any process yet, we should still
3005 insert breakpoints. */
3006 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3007 && (inferior_ptid == null_ptid || !target_has_execution ()))
3008 continue;
3009
3010 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3011 &hw_breakpoint_error, &hw_bp_details_reported);
3012 if (val)
3013 error_flag = val;
3014 }
3015
3016 if (error_flag)
3017 {
3018 target_terminal::ours_for_output ();
3019 error_stream (tmp_error_stream);
3020 }
3021 }
3022
3023 /* Used when starting or continuing the program. */
3024
3025 static void
3026 insert_breakpoint_locations (void)
3027 {
3028 struct breakpoint *bpt;
3029 struct bp_location *bl, **blp_tmp;
3030 int error_flag = 0;
3031 int val = 0;
3032 int disabled_breaks = 0;
3033 int hw_breakpoint_error = 0;
3034 int hw_bp_error_explained_already = 0;
3035
3036 string_file tmp_error_stream;
3037
3038 /* Explicitly mark the warning -- this will only be printed if
3039 there was an error. */
3040 tmp_error_stream.puts ("Warning:\n");
3041
3042 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3043
3044 ALL_BP_LOCATIONS (bl, blp_tmp)
3045 {
3046 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3047 continue;
3048
3049 /* There is no point inserting thread-specific breakpoints if
3050 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3051 has BL->OWNER always non-NULL. */
3052 if (bl->owner->thread != -1
3053 && !valid_global_thread_id (bl->owner->thread))
3054 continue;
3055
3056 switch_to_program_space_and_thread (bl->pspace);
3057
3058 /* For targets that support global breakpoints, there's no need
3059 to select an inferior to insert breakpoint to. In fact, even
3060 if we aren't attached to any process yet, we should still
3061 insert breakpoints. */
3062 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3063 && (inferior_ptid == null_ptid || !target_has_execution ()))
3064 continue;
3065
3066 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3067 &hw_breakpoint_error, &hw_bp_error_explained_already);
3068 if (val)
3069 error_flag = val;
3070 }
3071
3072 /* If we failed to insert all locations of a watchpoint, remove
3073 them, as half-inserted watchpoint is of limited use. */
3074 ALL_BREAKPOINTS (bpt)
3075 {
3076 int some_failed = 0;
3077 struct bp_location *loc;
3078
3079 if (!is_hardware_watchpoint (bpt))
3080 continue;
3081
3082 if (!breakpoint_enabled (bpt))
3083 continue;
3084
3085 if (bpt->disposition == disp_del_at_next_stop)
3086 continue;
3087
3088 for (loc = bpt->loc; loc; loc = loc->next)
3089 if (!loc->inserted && should_be_inserted (loc))
3090 {
3091 some_failed = 1;
3092 break;
3093 }
3094 if (some_failed)
3095 {
3096 for (loc = bpt->loc; loc; loc = loc->next)
3097 if (loc->inserted)
3098 remove_breakpoint (loc);
3099
3100 hw_breakpoint_error = 1;
3101 tmp_error_stream.printf ("Could not insert "
3102 "hardware watchpoint %d.\n",
3103 bpt->number);
3104 error_flag = -1;
3105 }
3106 }
3107
3108 if (error_flag)
3109 {
3110 /* If a hardware breakpoint or watchpoint was inserted, add a
3111 message about possibly exhausted resources. */
3112 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3113 {
3114 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3115 You may have requested too many hardware breakpoints/watchpoints.\n");
3116 }
3117 target_terminal::ours_for_output ();
3118 error_stream (tmp_error_stream);
3119 }
3120 }
3121
3122 /* Used when the program stops.
3123 Returns zero if successful, or non-zero if there was a problem
3124 removing a breakpoint location. */
3125
3126 int
3127 remove_breakpoints (void)
3128 {
3129 struct bp_location *bl, **blp_tmp;
3130 int val = 0;
3131
3132 ALL_BP_LOCATIONS (bl, blp_tmp)
3133 {
3134 if (bl->inserted && !is_tracepoint (bl->owner))
3135 val |= remove_breakpoint (bl);
3136 }
3137 return val;
3138 }
3139
3140 /* When a thread exits, remove breakpoints that are related to
3141 that thread. */
3142
3143 static void
3144 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3145 {
3146 struct breakpoint *b, *b_tmp;
3147
3148 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3149 {
3150 if (b->thread == tp->global_num && user_breakpoint_p (b))
3151 {
3152 b->disposition = disp_del_at_next_stop;
3153
3154 printf_filtered (_("\
3155 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3156 b->number, print_thread_id (tp));
3157
3158 /* Hide it from the user. */
3159 b->number = 0;
3160 }
3161 }
3162 }
3163
3164 /* See breakpoint.h. */
3165
3166 void
3167 remove_breakpoints_inf (inferior *inf)
3168 {
3169 struct bp_location *bl, **blp_tmp;
3170 int val;
3171
3172 ALL_BP_LOCATIONS (bl, blp_tmp)
3173 {
3174 if (bl->pspace != inf->pspace)
3175 continue;
3176
3177 if (bl->inserted && !bl->target_info.persist)
3178 {
3179 val = remove_breakpoint (bl);
3180 if (val != 0)
3181 return;
3182 }
3183 }
3184 }
3185
3186 static int internal_breakpoint_number = -1;
3187
3188 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3189 If INTERNAL is non-zero, the breakpoint number will be populated
3190 from internal_breakpoint_number and that variable decremented.
3191 Otherwise the breakpoint number will be populated from
3192 breakpoint_count and that value incremented. Internal breakpoints
3193 do not set the internal var bpnum. */
3194 static void
3195 set_breakpoint_number (int internal, struct breakpoint *b)
3196 {
3197 if (internal)
3198 b->number = internal_breakpoint_number--;
3199 else
3200 {
3201 set_breakpoint_count (breakpoint_count + 1);
3202 b->number = breakpoint_count;
3203 }
3204 }
3205
3206 static struct breakpoint *
3207 create_internal_breakpoint (struct gdbarch *gdbarch,
3208 CORE_ADDR address, enum bptype type,
3209 const struct breakpoint_ops *ops)
3210 {
3211 symtab_and_line sal;
3212 sal.pc = address;
3213 sal.section = find_pc_overlay (sal.pc);
3214 sal.pspace = current_program_space;
3215
3216 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
3217 b->number = internal_breakpoint_number--;
3218 b->disposition = disp_donttouch;
3219
3220 return b;
3221 }
3222
3223 static const char *const longjmp_names[] =
3224 {
3225 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3226 };
3227 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3228
3229 /* Per-objfile data private to breakpoint.c. */
3230 struct breakpoint_objfile_data
3231 {
3232 /* Minimal symbol for "_ovly_debug_event" (if any). */
3233 struct bound_minimal_symbol overlay_msym {};
3234
3235 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3236 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
3237
3238 /* True if we have looked for longjmp probes. */
3239 int longjmp_searched = 0;
3240
3241 /* SystemTap probe points for longjmp (if any). These are non-owning
3242 references. */
3243 std::vector<probe *> longjmp_probes;
3244
3245 /* Minimal symbol for "std::terminate()" (if any). */
3246 struct bound_minimal_symbol terminate_msym {};
3247
3248 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3249 struct bound_minimal_symbol exception_msym {};
3250
3251 /* True if we have looked for exception probes. */
3252 int exception_searched = 0;
3253
3254 /* SystemTap probe points for unwinding (if any). These are non-owning
3255 references. */
3256 std::vector<probe *> exception_probes;
3257 };
3258
3259 static const struct objfile_key<breakpoint_objfile_data>
3260 breakpoint_objfile_key;
3261
3262 /* Minimal symbol not found sentinel. */
3263 static struct minimal_symbol msym_not_found;
3264
3265 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3266
3267 static int
3268 msym_not_found_p (const struct minimal_symbol *msym)
3269 {
3270 return msym == &msym_not_found;
3271 }
3272
3273 /* Return per-objfile data needed by breakpoint.c.
3274 Allocate the data if necessary. */
3275
3276 static struct breakpoint_objfile_data *
3277 get_breakpoint_objfile_data (struct objfile *objfile)
3278 {
3279 struct breakpoint_objfile_data *bp_objfile_data;
3280
3281 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3282 if (bp_objfile_data == NULL)
3283 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3284 return bp_objfile_data;
3285 }
3286
3287 static void
3288 create_overlay_event_breakpoint (void)
3289 {
3290 const char *const func_name = "_ovly_debug_event";
3291
3292 for (objfile *objfile : current_program_space->objfiles ())
3293 {
3294 struct breakpoint *b;
3295 struct breakpoint_objfile_data *bp_objfile_data;
3296 CORE_ADDR addr;
3297 struct explicit_location explicit_loc;
3298
3299 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3300
3301 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3302 continue;
3303
3304 if (bp_objfile_data->overlay_msym.minsym == NULL)
3305 {
3306 struct bound_minimal_symbol m;
3307
3308 m = lookup_minimal_symbol_text (func_name, objfile);
3309 if (m.minsym == NULL)
3310 {
3311 /* Avoid future lookups in this objfile. */
3312 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3313 continue;
3314 }
3315 bp_objfile_data->overlay_msym = m;
3316 }
3317
3318 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3319 b = create_internal_breakpoint (objfile->arch (), addr,
3320 bp_overlay_event,
3321 &internal_breakpoint_ops);
3322 initialize_explicit_location (&explicit_loc);
3323 explicit_loc.function_name = ASTRDUP (func_name);
3324 b->location = new_explicit_location (&explicit_loc);
3325
3326 if (overlay_debugging == ovly_auto)
3327 {
3328 b->enable_state = bp_enabled;
3329 overlay_events_enabled = 1;
3330 }
3331 else
3332 {
3333 b->enable_state = bp_disabled;
3334 overlay_events_enabled = 0;
3335 }
3336 }
3337 }
3338
3339 /* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3340 true if a breakpoint was installed. */
3341
3342 static bool
3343 create_longjmp_master_breakpoint_probe (objfile *objfile)
3344 {
3345 struct gdbarch *gdbarch = objfile->arch ();
3346 struct breakpoint_objfile_data *bp_objfile_data
3347 = get_breakpoint_objfile_data (objfile);
3348
3349 if (!bp_objfile_data->longjmp_searched)
3350 {
3351 std::vector<probe *> ret
3352 = find_probes_in_objfile (objfile, "libc", "longjmp");
3353
3354 if (!ret.empty ())
3355 {
3356 /* We are only interested in checking one element. */
3357 probe *p = ret[0];
3358
3359 if (!p->can_evaluate_arguments ())
3360 {
3361 /* We cannot use the probe interface here,
3362 because it does not know how to evaluate
3363 arguments. */
3364 ret.clear ();
3365 }
3366 }
3367 bp_objfile_data->longjmp_probes = ret;
3368 bp_objfile_data->longjmp_searched = 1;
3369 }
3370
3371 if (bp_objfile_data->longjmp_probes.empty ())
3372 return false;
3373
3374 for (probe *p : bp_objfile_data->longjmp_probes)
3375 {
3376 struct breakpoint *b;
3377
3378 b = create_internal_breakpoint (gdbarch,
3379 p->get_relocated_address (objfile),
3380 bp_longjmp_master,
3381 &internal_breakpoint_ops);
3382 b->location = new_probe_location ("-probe-stap libc:longjmp");
3383 b->enable_state = bp_disabled;
3384 }
3385
3386 return true;
3387 }
3388
3389 /* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3390 Return true if at least one breakpoint was installed. */
3391
3392 static bool
3393 create_longjmp_master_breakpoint_names (objfile *objfile)
3394 {
3395 struct gdbarch *gdbarch = objfile->arch ();
3396 if (!gdbarch_get_longjmp_target_p (gdbarch))
3397 return false;
3398
3399 struct breakpoint_objfile_data *bp_objfile_data
3400 = get_breakpoint_objfile_data (objfile);
3401 unsigned int installed_bp = 0;
3402
3403 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3404 {
3405 struct breakpoint *b;
3406 const char *func_name;
3407 CORE_ADDR addr;
3408 struct explicit_location explicit_loc;
3409
3410 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3411 continue;
3412
3413 func_name = longjmp_names[i];
3414 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3415 {
3416 struct bound_minimal_symbol m;
3417
3418 m = lookup_minimal_symbol_text (func_name, objfile);
3419 if (m.minsym == NULL)
3420 {
3421 /* Prevent future lookups in this objfile. */
3422 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3423 continue;
3424 }
3425 bp_objfile_data->longjmp_msym[i] = m;
3426 }
3427
3428 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3429 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3430 &internal_breakpoint_ops);
3431 initialize_explicit_location (&explicit_loc);
3432 explicit_loc.function_name = ASTRDUP (func_name);
3433 b->location = new_explicit_location (&explicit_loc);
3434 b->enable_state = bp_disabled;
3435 installed_bp++;
3436 }
3437
3438 return installed_bp > 0;
3439 }
3440
3441 /* Create a master longjmp breakpoint. */
3442
3443 static void
3444 create_longjmp_master_breakpoint (void)
3445 {
3446 scoped_restore_current_program_space restore_pspace;
3447
3448 for (struct program_space *pspace : program_spaces)
3449 {
3450 set_current_program_space (pspace);
3451
3452 for (objfile *obj : current_program_space->objfiles ())
3453 {
3454 /* Skip separate debug object, it's handled in the loop below. */
3455 if (obj->separate_debug_objfile_backlink != nullptr)
3456 continue;
3457
3458 /* Try a probe kind breakpoint on main objfile. */
3459 if (create_longjmp_master_breakpoint_probe (obj))
3460 continue;
3461
3462 /* Try longjmp_names kind breakpoints on main and separate_debug
3463 objfiles. */
3464 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3465 if (create_longjmp_master_breakpoint_names (debug_objfile))
3466 break;
3467 }
3468 }
3469 }
3470
3471 /* Create a master std::terminate breakpoint. */
3472 static void
3473 create_std_terminate_master_breakpoint (void)
3474 {
3475 const char *const func_name = "std::terminate()";
3476
3477 scoped_restore_current_program_space restore_pspace;
3478
3479 for (struct program_space *pspace : program_spaces)
3480 {
3481 CORE_ADDR addr;
3482
3483 set_current_program_space (pspace);
3484
3485 for (objfile *objfile : current_program_space->objfiles ())
3486 {
3487 struct breakpoint *b;
3488 struct breakpoint_objfile_data *bp_objfile_data;
3489 struct explicit_location explicit_loc;
3490
3491 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3492
3493 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3494 continue;
3495
3496 if (bp_objfile_data->terminate_msym.minsym == NULL)
3497 {
3498 struct bound_minimal_symbol m;
3499
3500 m = lookup_minimal_symbol (func_name, NULL, objfile);
3501 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3502 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3503 {
3504 /* Prevent future lookups in this objfile. */
3505 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3506 continue;
3507 }
3508 bp_objfile_data->terminate_msym = m;
3509 }
3510
3511 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3512 b = create_internal_breakpoint (objfile->arch (), addr,
3513 bp_std_terminate_master,
3514 &internal_breakpoint_ops);
3515 initialize_explicit_location (&explicit_loc);
3516 explicit_loc.function_name = ASTRDUP (func_name);
3517 b->location = new_explicit_location (&explicit_loc);
3518 b->enable_state = bp_disabled;
3519 }
3520 }
3521 }
3522
3523 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3524 probe. Return true if a breakpoint was installed. */
3525
3526 static bool
3527 create_exception_master_breakpoint_probe (objfile *objfile)
3528 {
3529 struct breakpoint *b;
3530 struct gdbarch *gdbarch;
3531 struct breakpoint_objfile_data *bp_objfile_data;
3532
3533 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3534
3535 /* We prefer the SystemTap probe point if it exists. */
3536 if (!bp_objfile_data->exception_searched)
3537 {
3538 std::vector<probe *> ret
3539 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3540
3541 if (!ret.empty ())
3542 {
3543 /* We are only interested in checking one element. */
3544 probe *p = ret[0];
3545
3546 if (!p->can_evaluate_arguments ())
3547 {
3548 /* We cannot use the probe interface here, because it does
3549 not know how to evaluate arguments. */
3550 ret.clear ();
3551 }
3552 }
3553 bp_objfile_data->exception_probes = ret;
3554 bp_objfile_data->exception_searched = 1;
3555 }
3556
3557 if (bp_objfile_data->exception_probes.empty ())
3558 return false;
3559
3560 gdbarch = objfile->arch ();
3561
3562 for (probe *p : bp_objfile_data->exception_probes)
3563 {
3564 b = create_internal_breakpoint (gdbarch,
3565 p->get_relocated_address (objfile),
3566 bp_exception_master,
3567 &internal_breakpoint_ops);
3568 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3569 b->enable_state = bp_disabled;
3570 }
3571
3572 return true;
3573 }
3574
3575 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3576 _Unwind_DebugHook. Return true if a breakpoint was installed. */
3577
3578 static bool
3579 create_exception_master_breakpoint_hook (objfile *objfile)
3580 {
3581 const char *const func_name = "_Unwind_DebugHook";
3582 struct breakpoint *b;
3583 struct gdbarch *gdbarch;
3584 struct breakpoint_objfile_data *bp_objfile_data;
3585 CORE_ADDR addr;
3586 struct explicit_location explicit_loc;
3587
3588 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3589
3590 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3591 return false;
3592
3593 gdbarch = objfile->arch ();
3594
3595 if (bp_objfile_data->exception_msym.minsym == NULL)
3596 {
3597 struct bound_minimal_symbol debug_hook;
3598
3599 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3600 if (debug_hook.minsym == NULL)
3601 {
3602 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3603 return false;
3604 }
3605
3606 bp_objfile_data->exception_msym = debug_hook;
3607 }
3608
3609 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3610 addr = gdbarch_convert_from_func_ptr_addr
3611 (gdbarch, addr, current_inferior ()->top_target ());
3612 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3613 &internal_breakpoint_ops);
3614 initialize_explicit_location (&explicit_loc);
3615 explicit_loc.function_name = ASTRDUP (func_name);
3616 b->location = new_explicit_location (&explicit_loc);
3617 b->enable_state = bp_disabled;
3618
3619 return true;
3620 }
3621
3622 /* Install a master breakpoint on the unwinder's debug hook. */
3623
3624 static void
3625 create_exception_master_breakpoint (void)
3626 {
3627 for (objfile *obj : current_program_space->objfiles ())
3628 {
3629 /* Skip separate debug object. */
3630 if (obj->separate_debug_objfile_backlink)
3631 continue;
3632
3633 /* Try a probe kind breakpoint. */
3634 if (create_exception_master_breakpoint_probe (obj))
3635 continue;
3636
3637 /* Iterate over main and separate debug objects and try an
3638 _Unwind_DebugHook kind breakpoint. */
3639 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3640 if (create_exception_master_breakpoint_hook (debug_objfile))
3641 break;
3642 }
3643 }
3644
3645 /* Does B have a location spec? */
3646
3647 static int
3648 breakpoint_event_location_empty_p (const struct breakpoint *b)
3649 {
3650 return b->location != NULL && event_location_empty_p (b->location.get ());
3651 }
3652
3653 void
3654 update_breakpoints_after_exec (void)
3655 {
3656 struct breakpoint *b, *b_tmp;
3657 struct bp_location *bploc, **bplocp_tmp;
3658
3659 /* We're about to delete breakpoints from GDB's lists. If the
3660 INSERTED flag is true, GDB will try to lift the breakpoints by
3661 writing the breakpoints' "shadow contents" back into memory. The
3662 "shadow contents" are NOT valid after an exec, so GDB should not
3663 do that. Instead, the target is responsible from marking
3664 breakpoints out as soon as it detects an exec. We don't do that
3665 here instead, because there may be other attempts to delete
3666 breakpoints after detecting an exec and before reaching here. */
3667 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3668 if (bploc->pspace == current_program_space)
3669 gdb_assert (!bploc->inserted);
3670
3671 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3672 {
3673 if (b->pspace != current_program_space)
3674 continue;
3675
3676 /* Solib breakpoints must be explicitly reset after an exec(). */
3677 if (b->type == bp_shlib_event)
3678 {
3679 delete_breakpoint (b);
3680 continue;
3681 }
3682
3683 /* JIT breakpoints must be explicitly reset after an exec(). */
3684 if (b->type == bp_jit_event)
3685 {
3686 delete_breakpoint (b);
3687 continue;
3688 }
3689
3690 /* Thread event breakpoints must be set anew after an exec(),
3691 as must overlay event and longjmp master breakpoints. */
3692 if (b->type == bp_thread_event || b->type == bp_overlay_event
3693 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3694 || b->type == bp_exception_master)
3695 {
3696 delete_breakpoint (b);
3697 continue;
3698 }
3699
3700 /* Step-resume breakpoints are meaningless after an exec(). */
3701 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3702 {
3703 delete_breakpoint (b);
3704 continue;
3705 }
3706
3707 /* Just like single-step breakpoints. */
3708 if (b->type == bp_single_step)
3709 {
3710 delete_breakpoint (b);
3711 continue;
3712 }
3713
3714 /* Longjmp and longjmp-resume breakpoints are also meaningless
3715 after an exec. */
3716 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3717 || b->type == bp_longjmp_call_dummy
3718 || b->type == bp_exception || b->type == bp_exception_resume)
3719 {
3720 delete_breakpoint (b);
3721 continue;
3722 }
3723
3724 if (b->type == bp_catchpoint)
3725 {
3726 /* For now, none of the bp_catchpoint breakpoints need to
3727 do anything at this point. In the future, if some of
3728 the catchpoints need to something, we will need to add
3729 a new method, and call this method from here. */
3730 continue;
3731 }
3732
3733 /* bp_finish is a special case. The only way we ought to be able
3734 to see one of these when an exec() has happened, is if the user
3735 caught a vfork, and then said "finish". Ordinarily a finish just
3736 carries them to the call-site of the current callee, by setting
3737 a temporary bp there and resuming. But in this case, the finish
3738 will carry them entirely through the vfork & exec.
3739
3740 We don't want to allow a bp_finish to remain inserted now. But
3741 we can't safely delete it, 'cause finish_command has a handle to
3742 the bp on a bpstat, and will later want to delete it. There's a
3743 chance (and I've seen it happen) that if we delete the bp_finish
3744 here, that its storage will get reused by the time finish_command
3745 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3746 We really must allow finish_command to delete a bp_finish.
3747
3748 In the absence of a general solution for the "how do we know
3749 it's safe to delete something others may have handles to?"
3750 problem, what we'll do here is just uninsert the bp_finish, and
3751 let finish_command delete it.
3752
3753 (We know the bp_finish is "doomed" in the sense that it's
3754 momentary, and will be deleted as soon as finish_command sees
3755 the inferior stopped. So it doesn't matter that the bp's
3756 address is probably bogus in the new a.out, unlike e.g., the
3757 solib breakpoints.) */
3758
3759 if (b->type == bp_finish)
3760 {
3761 continue;
3762 }
3763
3764 /* Without a symbolic address, we have little hope of the
3765 pre-exec() address meaning the same thing in the post-exec()
3766 a.out. */
3767 if (breakpoint_event_location_empty_p (b))
3768 {
3769 delete_breakpoint (b);
3770 continue;
3771 }
3772 }
3773 }
3774
3775 int
3776 detach_breakpoints (ptid_t ptid)
3777 {
3778 struct bp_location *bl, **blp_tmp;
3779 int val = 0;
3780 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3781 struct inferior *inf = current_inferior ();
3782
3783 if (ptid.pid () == inferior_ptid.pid ())
3784 error (_("Cannot detach breakpoints of inferior_ptid"));
3785
3786 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3787 inferior_ptid = ptid;
3788 ALL_BP_LOCATIONS (bl, blp_tmp)
3789 {
3790 if (bl->pspace != inf->pspace)
3791 continue;
3792
3793 /* This function must physically remove breakpoints locations
3794 from the specified ptid, without modifying the breakpoint
3795 package's state. Locations of type bp_loc_other are only
3796 maintained at GDB side. So, there is no need to remove
3797 these bp_loc_other locations. Moreover, removing these
3798 would modify the breakpoint package's state. */
3799 if (bl->loc_type == bp_loc_other)
3800 continue;
3801
3802 if (bl->inserted)
3803 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3804 }
3805
3806 return val;
3807 }
3808
3809 /* Remove the breakpoint location BL from the current address space.
3810 Note that this is used to detach breakpoints from a child fork.
3811 When we get here, the child isn't in the inferior list, and neither
3812 do we have objects to represent its address space --- we should
3813 *not* look at bl->pspace->aspace here. */
3814
3815 static int
3816 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3817 {
3818 int val;
3819
3820 /* BL is never in moribund_locations by our callers. */
3821 gdb_assert (bl->owner != NULL);
3822
3823 /* The type of none suggests that owner is actually deleted.
3824 This should not ever happen. */
3825 gdb_assert (bl->owner->type != bp_none);
3826
3827 if (bl->loc_type == bp_loc_software_breakpoint
3828 || bl->loc_type == bp_loc_hardware_breakpoint)
3829 {
3830 /* "Normal" instruction breakpoint: either the standard
3831 trap-instruction bp (bp_breakpoint), or a
3832 bp_hardware_breakpoint. */
3833
3834 /* First check to see if we have to handle an overlay. */
3835 if (overlay_debugging == ovly_off
3836 || bl->section == NULL
3837 || !(section_is_overlay (bl->section)))
3838 {
3839 /* No overlay handling: just remove the breakpoint. */
3840
3841 /* If we're trying to uninsert a memory breakpoint that we
3842 know is set in a dynamic object that is marked
3843 shlib_disabled, then either the dynamic object was
3844 removed with "remove-symbol-file" or with
3845 "nosharedlibrary". In the former case, we don't know
3846 whether another dynamic object might have loaded over the
3847 breakpoint's address -- the user might well let us know
3848 about it next with add-symbol-file (the whole point of
3849 add-symbol-file is letting the user manually maintain a
3850 list of dynamically loaded objects). If we have the
3851 breakpoint's shadow memory, that is, this is a software
3852 breakpoint managed by GDB, check whether the breakpoint
3853 is still inserted in memory, to avoid overwriting wrong
3854 code with stale saved shadow contents. Note that HW
3855 breakpoints don't have shadow memory, as they're
3856 implemented using a mechanism that is not dependent on
3857 being able to modify the target's memory, and as such
3858 they should always be removed. */
3859 if (bl->shlib_disabled
3860 && bl->target_info.shadow_len != 0
3861 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3862 val = 0;
3863 else
3864 val = bl->owner->ops->remove_location (bl, reason);
3865 }
3866 else
3867 {
3868 /* This breakpoint is in an overlay section.
3869 Did we set a breakpoint at the LMA? */
3870 if (!overlay_events_enabled)
3871 {
3872 /* Yes -- overlay event support is not active, so we
3873 should have set a breakpoint at the LMA. Remove it.
3874 */
3875 /* Ignore any failures: if the LMA is in ROM, we will
3876 have already warned when we failed to insert it. */
3877 if (bl->loc_type == bp_loc_hardware_breakpoint)
3878 target_remove_hw_breakpoint (bl->gdbarch,
3879 &bl->overlay_target_info);
3880 else
3881 target_remove_breakpoint (bl->gdbarch,
3882 &bl->overlay_target_info,
3883 reason);
3884 }
3885 /* Did we set a breakpoint at the VMA?
3886 If so, we will have marked the breakpoint 'inserted'. */
3887 if (bl->inserted)
3888 {
3889 /* Yes -- remove it. Previously we did not bother to
3890 remove the breakpoint if the section had been
3891 unmapped, but let's not rely on that being safe. We
3892 don't know what the overlay manager might do. */
3893
3894 /* However, we should remove *software* breakpoints only
3895 if the section is still mapped, or else we overwrite
3896 wrong code with the saved shadow contents. */
3897 if (bl->loc_type == bp_loc_hardware_breakpoint
3898 || section_is_mapped (bl->section))
3899 val = bl->owner->ops->remove_location (bl, reason);
3900 else
3901 val = 0;
3902 }
3903 else
3904 {
3905 /* No -- not inserted, so no need to remove. No error. */
3906 val = 0;
3907 }
3908 }
3909
3910 /* In some cases, we might not be able to remove a breakpoint in
3911 a shared library that has already been removed, but we have
3912 not yet processed the shlib unload event. Similarly for an
3913 unloaded add-symbol-file object - the user might not yet have
3914 had the chance to remove-symbol-file it. shlib_disabled will
3915 be set if the library/object has already been removed, but
3916 the breakpoint hasn't been uninserted yet, e.g., after
3917 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3918 always-inserted mode. */
3919 if (val
3920 && (bl->loc_type == bp_loc_software_breakpoint
3921 && (bl->shlib_disabled
3922 || solib_name_from_address (bl->pspace, bl->address)
3923 || shared_objfile_contains_address_p (bl->pspace,
3924 bl->address))))
3925 val = 0;
3926
3927 if (val)
3928 return val;
3929 bl->inserted = (reason == DETACH_BREAKPOINT);
3930 }
3931 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3932 {
3933 gdb_assert (bl->owner->ops != NULL
3934 && bl->owner->ops->remove_location != NULL);
3935
3936 bl->inserted = (reason == DETACH_BREAKPOINT);
3937 bl->owner->ops->remove_location (bl, reason);
3938
3939 /* Failure to remove any of the hardware watchpoints comes here. */
3940 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3941 warning (_("Could not remove hardware watchpoint %d."),
3942 bl->owner->number);
3943 }
3944 else if (bl->owner->type == bp_catchpoint
3945 && breakpoint_enabled (bl->owner)
3946 && !bl->duplicate)
3947 {
3948 gdb_assert (bl->owner->ops != NULL
3949 && bl->owner->ops->remove_location != NULL);
3950
3951 val = bl->owner->ops->remove_location (bl, reason);
3952 if (val)
3953 return val;
3954
3955 bl->inserted = (reason == DETACH_BREAKPOINT);
3956 }
3957
3958 return 0;
3959 }
3960
3961 static int
3962 remove_breakpoint (struct bp_location *bl)
3963 {
3964 /* BL is never in moribund_locations by our callers. */
3965 gdb_assert (bl->owner != NULL);
3966
3967 /* The type of none suggests that owner is actually deleted.
3968 This should not ever happen. */
3969 gdb_assert (bl->owner->type != bp_none);
3970
3971 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3972
3973 switch_to_program_space_and_thread (bl->pspace);
3974
3975 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
3976 }
3977
3978 /* Clear the "inserted" flag in all breakpoints. */
3979
3980 void
3981 mark_breakpoints_out (void)
3982 {
3983 struct bp_location *bl, **blp_tmp;
3984
3985 ALL_BP_LOCATIONS (bl, blp_tmp)
3986 if (bl->pspace == current_program_space)
3987 bl->inserted = 0;
3988 }
3989
3990 /* Clear the "inserted" flag in all breakpoints and delete any
3991 breakpoints which should go away between runs of the program.
3992
3993 Plus other such housekeeping that has to be done for breakpoints
3994 between runs.
3995
3996 Note: this function gets called at the end of a run (by
3997 generic_mourn_inferior) and when a run begins (by
3998 init_wait_for_inferior). */
3999
4000
4001
4002 void
4003 breakpoint_init_inferior (enum inf_context context)
4004 {
4005 struct breakpoint *b, *b_tmp;
4006 struct program_space *pspace = current_program_space;
4007
4008 /* If breakpoint locations are shared across processes, then there's
4009 nothing to do. */
4010 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4011 return;
4012
4013 mark_breakpoints_out ();
4014
4015 ALL_BREAKPOINTS_SAFE (b, b_tmp)
4016 {
4017 if (b->loc && b->loc->pspace != pspace)
4018 continue;
4019
4020 switch (b->type)
4021 {
4022 case bp_call_dummy:
4023 case bp_longjmp_call_dummy:
4024
4025 /* If the call dummy breakpoint is at the entry point it will
4026 cause problems when the inferior is rerun, so we better get
4027 rid of it. */
4028
4029 case bp_watchpoint_scope:
4030
4031 /* Also get rid of scope breakpoints. */
4032
4033 case bp_shlib_event:
4034
4035 /* Also remove solib event breakpoints. Their addresses may
4036 have changed since the last time we ran the program.
4037 Actually we may now be debugging against different target;
4038 and so the solib backend that installed this breakpoint may
4039 not be used in by the target. E.g.,
4040
4041 (gdb) file prog-linux
4042 (gdb) run # native linux target
4043 ...
4044 (gdb) kill
4045 (gdb) file prog-win.exe
4046 (gdb) tar rem :9999 # remote Windows gdbserver.
4047 */
4048
4049 case bp_step_resume:
4050
4051 /* Also remove step-resume breakpoints. */
4052
4053 case bp_single_step:
4054
4055 /* Also remove single-step breakpoints. */
4056
4057 delete_breakpoint (b);
4058 break;
4059
4060 case bp_watchpoint:
4061 case bp_hardware_watchpoint:
4062 case bp_read_watchpoint:
4063 case bp_access_watchpoint:
4064 {
4065 struct watchpoint *w = (struct watchpoint *) b;
4066
4067 /* Likewise for watchpoints on local expressions. */
4068 if (w->exp_valid_block != NULL)
4069 delete_breakpoint (b);
4070 else
4071 {
4072 /* Get rid of existing locations, which are no longer
4073 valid. New ones will be created in
4074 update_watchpoint, when the inferior is restarted.
4075 The next update_global_location_list call will
4076 garbage collect them. */
4077 b->loc = NULL;
4078
4079 if (context == inf_starting)
4080 {
4081 /* Reset val field to force reread of starting value in
4082 insert_breakpoints. */
4083 w->val.reset (nullptr);
4084 w->val_valid = false;
4085 }
4086 }
4087 }
4088 break;
4089 default:
4090 break;
4091 }
4092 }
4093
4094 /* Get rid of the moribund locations. */
4095 for (bp_location *bl : moribund_locations)
4096 decref_bp_location (&bl);
4097 moribund_locations.clear ();
4098 }
4099
4100 /* These functions concern about actual breakpoints inserted in the
4101 target --- to e.g. check if we need to do decr_pc adjustment or if
4102 we need to hop over the bkpt --- so we check for address space
4103 match, not program space. */
4104
4105 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4106 exists at PC. It returns ordinary_breakpoint_here if it's an
4107 ordinary breakpoint, or permanent_breakpoint_here if it's a
4108 permanent breakpoint.
4109 - When continuing from a location with an ordinary breakpoint, we
4110 actually single step once before calling insert_breakpoints.
4111 - When continuing from a location with a permanent breakpoint, we
4112 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4113 the target, to advance the PC past the breakpoint. */
4114
4115 enum breakpoint_here
4116 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4117 {
4118 struct bp_location *bl, **blp_tmp;
4119 int any_breakpoint_here = 0;
4120
4121 ALL_BP_LOCATIONS (bl, blp_tmp)
4122 {
4123 if (bl->loc_type != bp_loc_software_breakpoint
4124 && bl->loc_type != bp_loc_hardware_breakpoint)
4125 continue;
4126
4127 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4128 if ((breakpoint_enabled (bl->owner)
4129 || bl->permanent)
4130 && breakpoint_location_address_match (bl, aspace, pc))
4131 {
4132 if (overlay_debugging
4133 && section_is_overlay (bl->section)
4134 && !section_is_mapped (bl->section))
4135 continue; /* unmapped overlay -- can't be a match */
4136 else if (bl->permanent)
4137 return permanent_breakpoint_here;
4138 else
4139 any_breakpoint_here = 1;
4140 }
4141 }
4142
4143 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4144 }
4145
4146 /* See breakpoint.h. */
4147
4148 int
4149 breakpoint_in_range_p (const address_space *aspace,
4150 CORE_ADDR addr, ULONGEST len)
4151 {
4152 struct bp_location *bl, **blp_tmp;
4153
4154 ALL_BP_LOCATIONS (bl, blp_tmp)
4155 {
4156 if (bl->loc_type != bp_loc_software_breakpoint
4157 && bl->loc_type != bp_loc_hardware_breakpoint)
4158 continue;
4159
4160 if ((breakpoint_enabled (bl->owner)
4161 || bl->permanent)
4162 && breakpoint_location_address_range_overlap (bl, aspace,
4163 addr, len))
4164 {
4165 if (overlay_debugging
4166 && section_is_overlay (bl->section)
4167 && !section_is_mapped (bl->section))
4168 {
4169 /* Unmapped overlay -- can't be a match. */
4170 continue;
4171 }
4172
4173 return 1;
4174 }
4175 }
4176
4177 return 0;
4178 }
4179
4180 /* Return true if there's a moribund breakpoint at PC. */
4181
4182 int
4183 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4184 {
4185 for (bp_location *loc : moribund_locations)
4186 if (breakpoint_location_address_match (loc, aspace, pc))
4187 return 1;
4188
4189 return 0;
4190 }
4191
4192 /* Returns non-zero iff BL is inserted at PC, in address space
4193 ASPACE. */
4194
4195 static int
4196 bp_location_inserted_here_p (struct bp_location *bl,
4197 const address_space *aspace, CORE_ADDR pc)
4198 {
4199 if (bl->inserted
4200 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4201 aspace, pc))
4202 {
4203 if (overlay_debugging
4204 && section_is_overlay (bl->section)
4205 && !section_is_mapped (bl->section))
4206 return 0; /* unmapped overlay -- can't be a match */
4207 else
4208 return 1;
4209 }
4210 return 0;
4211 }
4212
4213 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4214
4215 int
4216 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4217 {
4218 struct bp_location **blp, **blp_tmp = NULL;
4219
4220 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4221 {
4222 struct bp_location *bl = *blp;
4223
4224 if (bl->loc_type != bp_loc_software_breakpoint
4225 && bl->loc_type != bp_loc_hardware_breakpoint)
4226 continue;
4227
4228 if (bp_location_inserted_here_p (bl, aspace, pc))
4229 return 1;
4230 }
4231 return 0;
4232 }
4233
4234 /* This function returns non-zero iff there is a software breakpoint
4235 inserted at PC. */
4236
4237 int
4238 software_breakpoint_inserted_here_p (const address_space *aspace,
4239 CORE_ADDR pc)
4240 {
4241 struct bp_location **blp, **blp_tmp = NULL;
4242
4243 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4244 {
4245 struct bp_location *bl = *blp;
4246
4247 if (bl->loc_type != bp_loc_software_breakpoint)
4248 continue;
4249
4250 if (bp_location_inserted_here_p (bl, aspace, pc))
4251 return 1;
4252 }
4253
4254 return 0;
4255 }
4256
4257 /* See breakpoint.h. */
4258
4259 int
4260 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4261 CORE_ADDR pc)
4262 {
4263 struct bp_location **blp, **blp_tmp = NULL;
4264
4265 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4266 {
4267 struct bp_location *bl = *blp;
4268
4269 if (bl->loc_type != bp_loc_hardware_breakpoint)
4270 continue;
4271
4272 if (bp_location_inserted_here_p (bl, aspace, pc))
4273 return 1;
4274 }
4275
4276 return 0;
4277 }
4278
4279 int
4280 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4281 CORE_ADDR addr, ULONGEST len)
4282 {
4283 struct breakpoint *bpt;
4284
4285 ALL_BREAKPOINTS (bpt)
4286 {
4287 struct bp_location *loc;
4288
4289 if (bpt->type != bp_hardware_watchpoint
4290 && bpt->type != bp_access_watchpoint)
4291 continue;
4292
4293 if (!breakpoint_enabled (bpt))
4294 continue;
4295
4296 for (loc = bpt->loc; loc; loc = loc->next)
4297 if (loc->pspace->aspace == aspace && loc->inserted)
4298 {
4299 CORE_ADDR l, h;
4300
4301 /* Check for intersection. */
4302 l = std::max<CORE_ADDR> (loc->address, addr);
4303 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4304 if (l < h)
4305 return 1;
4306 }
4307 }
4308 return 0;
4309 }
4310
4311 /* See breakpoint.h. */
4312
4313 bool
4314 is_catchpoint (struct breakpoint *b)
4315 {
4316 return (b->type == bp_catchpoint);
4317 }
4318
4319 /* Clear a bpstat so that it says we are not at any breakpoint.
4320 Also free any storage that is part of a bpstat. */
4321
4322 void
4323 bpstat_clear (bpstat *bsp)
4324 {
4325 bpstat p;
4326 bpstat q;
4327
4328 if (bsp == 0)
4329 return;
4330 p = *bsp;
4331 while (p != NULL)
4332 {
4333 q = p->next;
4334 delete p;
4335 p = q;
4336 }
4337 *bsp = NULL;
4338 }
4339
4340 bpstats::bpstats (const bpstats &other)
4341 : next (NULL),
4342 bp_location_at (other.bp_location_at),
4343 breakpoint_at (other.breakpoint_at),
4344 commands (other.commands),
4345 print (other.print),
4346 stop (other.stop),
4347 print_it (other.print_it)
4348 {
4349 if (other.old_val != NULL)
4350 old_val = release_value (value_copy (other.old_val.get ()));
4351 }
4352
4353 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4354 is part of the bpstat is copied as well. */
4355
4356 bpstat
4357 bpstat_copy (bpstat bs)
4358 {
4359 bpstat p = NULL;
4360 bpstat tmp;
4361 bpstat retval = NULL;
4362
4363 if (bs == NULL)
4364 return bs;
4365
4366 for (; bs != NULL; bs = bs->next)
4367 {
4368 tmp = new bpstats (*bs);
4369
4370 if (p == NULL)
4371 /* This is the first thing in the chain. */
4372 retval = tmp;
4373 else
4374 p->next = tmp;
4375 p = tmp;
4376 }
4377 p->next = NULL;
4378 return retval;
4379 }
4380
4381 /* Find the bpstat associated with this breakpoint. */
4382
4383 bpstat
4384 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4385 {
4386 if (bsp == NULL)
4387 return NULL;
4388
4389 for (; bsp != NULL; bsp = bsp->next)
4390 {
4391 if (bsp->breakpoint_at == breakpoint)
4392 return bsp;
4393 }
4394 return NULL;
4395 }
4396
4397 /* See breakpoint.h. */
4398
4399 bool
4400 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4401 {
4402 for (; bsp != NULL; bsp = bsp->next)
4403 {
4404 if (bsp->breakpoint_at == NULL)
4405 {
4406 /* A moribund location can never explain a signal other than
4407 GDB_SIGNAL_TRAP. */
4408 if (sig == GDB_SIGNAL_TRAP)
4409 return true;
4410 }
4411 else
4412 {
4413 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4414 sig))
4415 return true;
4416 }
4417 }
4418
4419 return false;
4420 }
4421
4422 /* Put in *NUM the breakpoint number of the first breakpoint we are
4423 stopped at. *BSP upon return is a bpstat which points to the
4424 remaining breakpoints stopped at (but which is not guaranteed to be
4425 good for anything but further calls to bpstat_num).
4426
4427 Return 0 if passed a bpstat which does not indicate any breakpoints.
4428 Return -1 if stopped at a breakpoint that has been deleted since
4429 we set it.
4430 Return 1 otherwise. */
4431
4432 int
4433 bpstat_num (bpstat *bsp, int *num)
4434 {
4435 struct breakpoint *b;
4436
4437 if ((*bsp) == NULL)
4438 return 0; /* No more breakpoint values */
4439
4440 /* We assume we'll never have several bpstats that correspond to a
4441 single breakpoint -- otherwise, this function might return the
4442 same number more than once and this will look ugly. */
4443 b = (*bsp)->breakpoint_at;
4444 *bsp = (*bsp)->next;
4445 if (b == NULL)
4446 return -1; /* breakpoint that's been deleted since */
4447
4448 *num = b->number; /* We have its number */
4449 return 1;
4450 }
4451
4452 /* See breakpoint.h. */
4453
4454 void
4455 bpstat_clear_actions (void)
4456 {
4457 bpstat bs;
4458
4459 if (inferior_ptid == null_ptid)
4460 return;
4461
4462 thread_info *tp = inferior_thread ();
4463 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4464 {
4465 bs->commands = NULL;
4466 bs->old_val.reset (nullptr);
4467 }
4468 }
4469
4470 /* Called when a command is about to proceed the inferior. */
4471
4472 static void
4473 breakpoint_about_to_proceed (void)
4474 {
4475 if (inferior_ptid != null_ptid)
4476 {
4477 struct thread_info *tp = inferior_thread ();
4478
4479 /* Allow inferior function calls in breakpoint commands to not
4480 interrupt the command list. When the call finishes
4481 successfully, the inferior will be standing at the same
4482 breakpoint as if nothing happened. */
4483 if (tp->control.in_infcall)
4484 return;
4485 }
4486
4487 breakpoint_proceeded = 1;
4488 }
4489
4490 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4491 or its equivalent. */
4492
4493 static int
4494 command_line_is_silent (struct command_line *cmd)
4495 {
4496 return cmd && (strcmp ("silent", cmd->line) == 0);
4497 }
4498
4499 /* Execute all the commands associated with all the breakpoints at
4500 this location. Any of these commands could cause the process to
4501 proceed beyond this point, etc. We look out for such changes by
4502 checking the global "breakpoint_proceeded" after each command.
4503
4504 Returns true if a breakpoint command resumed the inferior. In that
4505 case, it is the caller's responsibility to recall it again with the
4506 bpstat of the current thread. */
4507
4508 static int
4509 bpstat_do_actions_1 (bpstat *bsp)
4510 {
4511 bpstat bs;
4512 int again = 0;
4513
4514 /* Avoid endless recursion if a `source' command is contained
4515 in bs->commands. */
4516 if (executing_breakpoint_commands)
4517 return 0;
4518
4519 scoped_restore save_executing
4520 = make_scoped_restore (&executing_breakpoint_commands, 1);
4521
4522 scoped_restore preventer = prevent_dont_repeat ();
4523
4524 /* This pointer will iterate over the list of bpstat's. */
4525 bs = *bsp;
4526
4527 breakpoint_proceeded = 0;
4528 for (; bs != NULL; bs = bs->next)
4529 {
4530 struct command_line *cmd = NULL;
4531
4532 /* Take ownership of the BSP's command tree, if it has one.
4533
4534 The command tree could legitimately contain commands like
4535 'step' and 'next', which call clear_proceed_status, which
4536 frees stop_bpstat's command tree. To make sure this doesn't
4537 free the tree we're executing out from under us, we need to
4538 take ownership of the tree ourselves. Since a given bpstat's
4539 commands are only executed once, we don't need to copy it; we
4540 can clear the pointer in the bpstat, and make sure we free
4541 the tree when we're done. */
4542 counted_command_line ccmd = bs->commands;
4543 bs->commands = NULL;
4544 if (ccmd != NULL)
4545 cmd = ccmd.get ();
4546 if (command_line_is_silent (cmd))
4547 {
4548 /* The action has been already done by bpstat_stop_status. */
4549 cmd = cmd->next;
4550 }
4551
4552 while (cmd != NULL)
4553 {
4554 execute_control_command (cmd);
4555
4556 if (breakpoint_proceeded)
4557 break;
4558 else
4559 cmd = cmd->next;
4560 }
4561
4562 if (breakpoint_proceeded)
4563 {
4564 if (current_ui->async)
4565 /* If we are in async mode, then the target might be still
4566 running, not stopped at any breakpoint, so nothing for
4567 us to do here -- just return to the event loop. */
4568 ;
4569 else
4570 /* In sync mode, when execute_control_command returns
4571 we're already standing on the next breakpoint.
4572 Breakpoint commands for that stop were not run, since
4573 execute_command does not run breakpoint commands --
4574 only command_line_handler does, but that one is not
4575 involved in execution of breakpoint commands. So, we
4576 can now execute breakpoint commands. It should be
4577 noted that making execute_command do bpstat actions is
4578 not an option -- in this case we'll have recursive
4579 invocation of bpstat for each breakpoint with a
4580 command, and can easily blow up GDB stack. Instead, we
4581 return true, which will trigger the caller to recall us
4582 with the new stop_bpstat. */
4583 again = 1;
4584 break;
4585 }
4586 }
4587 return again;
4588 }
4589
4590 /* Helper for bpstat_do_actions. Get the current thread, if there's
4591 one, is alive and has execution. Return NULL otherwise. */
4592
4593 static thread_info *
4594 get_bpstat_thread ()
4595 {
4596 if (inferior_ptid == null_ptid || !target_has_execution ())
4597 return NULL;
4598
4599 thread_info *tp = inferior_thread ();
4600 if (tp->state == THREAD_EXITED || tp->executing)
4601 return NULL;
4602 return tp;
4603 }
4604
4605 void
4606 bpstat_do_actions (void)
4607 {
4608 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4609 thread_info *tp;
4610
4611 /* Do any commands attached to breakpoint we are stopped at. */
4612 while ((tp = get_bpstat_thread ()) != NULL)
4613 {
4614 /* Since in sync mode, bpstat_do_actions may resume the
4615 inferior, and only return when it is stopped at the next
4616 breakpoint, we keep doing breakpoint actions until it returns
4617 false to indicate the inferior was not resumed. */
4618 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4619 break;
4620 }
4621
4622 cleanup_if_error.release ();
4623 }
4624
4625 /* Print out the (old or new) value associated with a watchpoint. */
4626
4627 static void
4628 watchpoint_value_print (struct value *val, struct ui_file *stream)
4629 {
4630 if (val == NULL)
4631 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4632 else
4633 {
4634 struct value_print_options opts;
4635 get_user_print_options (&opts);
4636 value_print (val, stream, &opts);
4637 }
4638 }
4639
4640 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4641 debugging multiple threads. */
4642
4643 void
4644 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4645 {
4646 if (uiout->is_mi_like_p ())
4647 return;
4648
4649 uiout->text ("\n");
4650
4651 if (show_thread_that_caused_stop ())
4652 {
4653 const char *name;
4654 struct thread_info *thr = inferior_thread ();
4655
4656 uiout->text ("Thread ");
4657 uiout->field_string ("thread-id", print_thread_id (thr));
4658
4659 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4660 if (name != NULL)
4661 {
4662 uiout->text (" \"");
4663 uiout->field_string ("name", name);
4664 uiout->text ("\"");
4665 }
4666
4667 uiout->text (" hit ");
4668 }
4669 }
4670
4671 /* Generic routine for printing messages indicating why we
4672 stopped. The behavior of this function depends on the value
4673 'print_it' in the bpstat structure. Under some circumstances we
4674 may decide not to print anything here and delegate the task to
4675 normal_stop(). */
4676
4677 static enum print_stop_action
4678 print_bp_stop_message (bpstat bs)
4679 {
4680 switch (bs->print_it)
4681 {
4682 case print_it_noop:
4683 /* Nothing should be printed for this bpstat entry. */
4684 return PRINT_UNKNOWN;
4685 break;
4686
4687 case print_it_done:
4688 /* We still want to print the frame, but we already printed the
4689 relevant messages. */
4690 return PRINT_SRC_AND_LOC;
4691 break;
4692
4693 case print_it_normal:
4694 {
4695 struct breakpoint *b = bs->breakpoint_at;
4696
4697 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4698 which has since been deleted. */
4699 if (b == NULL)
4700 return PRINT_UNKNOWN;
4701
4702 /* Normal case. Call the breakpoint's print_it method. */
4703 return b->ops->print_it (bs);
4704 }
4705 break;
4706
4707 default:
4708 internal_error (__FILE__, __LINE__,
4709 _("print_bp_stop_message: unrecognized enum value"));
4710 break;
4711 }
4712 }
4713
4714 /* A helper function that prints a shared library stopped event. */
4715
4716 static void
4717 print_solib_event (int is_catchpoint)
4718 {
4719 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4720 bool any_added = !current_program_space->added_solibs.empty ();
4721
4722 if (!is_catchpoint)
4723 {
4724 if (any_added || any_deleted)
4725 current_uiout->text (_("Stopped due to shared library event:\n"));
4726 else
4727 current_uiout->text (_("Stopped due to shared library event (no "
4728 "libraries added or removed)\n"));
4729 }
4730
4731 if (current_uiout->is_mi_like_p ())
4732 current_uiout->field_string ("reason",
4733 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4734
4735 if (any_deleted)
4736 {
4737 current_uiout->text (_(" Inferior unloaded "));
4738 ui_out_emit_list list_emitter (current_uiout, "removed");
4739 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4740 {
4741 const std::string &name = current_program_space->deleted_solibs[ix];
4742
4743 if (ix > 0)
4744 current_uiout->text (" ");
4745 current_uiout->field_string ("library", name);
4746 current_uiout->text ("\n");
4747 }
4748 }
4749
4750 if (any_added)
4751 {
4752 current_uiout->text (_(" Inferior loaded "));
4753 ui_out_emit_list list_emitter (current_uiout, "added");
4754 bool first = true;
4755 for (so_list *iter : current_program_space->added_solibs)
4756 {
4757 if (!first)
4758 current_uiout->text (" ");
4759 first = false;
4760 current_uiout->field_string ("library", iter->so_name);
4761 current_uiout->text ("\n");
4762 }
4763 }
4764 }
4765
4766 /* Print a message indicating what happened. This is called from
4767 normal_stop(). The input to this routine is the head of the bpstat
4768 list - a list of the eventpoints that caused this stop. KIND is
4769 the target_waitkind for the stopping event. This
4770 routine calls the generic print routine for printing a message
4771 about reasons for stopping. This will print (for example) the
4772 "Breakpoint n," part of the output. The return value of this
4773 routine is one of:
4774
4775 PRINT_UNKNOWN: Means we printed nothing.
4776 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4777 code to print the location. An example is
4778 "Breakpoint 1, " which should be followed by
4779 the location.
4780 PRINT_SRC_ONLY: Means we printed something, but there is no need
4781 to also print the location part of the message.
4782 An example is the catch/throw messages, which
4783 don't require a location appended to the end.
4784 PRINT_NOTHING: We have done some printing and we don't need any
4785 further info to be printed. */
4786
4787 enum print_stop_action
4788 bpstat_print (bpstat bs, int kind)
4789 {
4790 enum print_stop_action val;
4791
4792 /* Maybe another breakpoint in the chain caused us to stop.
4793 (Currently all watchpoints go on the bpstat whether hit or not.
4794 That probably could (should) be changed, provided care is taken
4795 with respect to bpstat_explains_signal). */
4796 for (; bs; bs = bs->next)
4797 {
4798 val = print_bp_stop_message (bs);
4799 if (val == PRINT_SRC_ONLY
4800 || val == PRINT_SRC_AND_LOC
4801 || val == PRINT_NOTHING)
4802 return val;
4803 }
4804
4805 /* If we had hit a shared library event breakpoint,
4806 print_bp_stop_message would print out this message. If we hit an
4807 OS-level shared library event, do the same thing. */
4808 if (kind == TARGET_WAITKIND_LOADED)
4809 {
4810 print_solib_event (0);
4811 return PRINT_NOTHING;
4812 }
4813
4814 /* We reached the end of the chain, or we got a null BS to start
4815 with and nothing was printed. */
4816 return PRINT_UNKNOWN;
4817 }
4818
4819 /* Evaluate the boolean expression EXP and return the result. */
4820
4821 static bool
4822 breakpoint_cond_eval (expression *exp)
4823 {
4824 struct value *mark = value_mark ();
4825 bool res = value_true (evaluate_expression (exp));
4826
4827 value_free_to_mark (mark);
4828 return res;
4829 }
4830
4831 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4832
4833 bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4834 : next (NULL),
4835 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
4836 breakpoint_at (bl->owner),
4837 commands (NULL),
4838 print (0),
4839 stop (0),
4840 print_it (print_it_normal)
4841 {
4842 **bs_link_pointer = this;
4843 *bs_link_pointer = &next;
4844 }
4845
4846 bpstats::bpstats ()
4847 : next (NULL),
4848 breakpoint_at (NULL),
4849 commands (NULL),
4850 print (0),
4851 stop (0),
4852 print_it (print_it_normal)
4853 {
4854 }
4855 \f
4856 /* The target has stopped with waitstatus WS. Check if any hardware
4857 watchpoints have triggered, according to the target. */
4858
4859 int
4860 watchpoints_triggered (struct target_waitstatus *ws)
4861 {
4862 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
4863 CORE_ADDR addr;
4864 struct breakpoint *b;
4865
4866 if (!stopped_by_watchpoint)
4867 {
4868 /* We were not stopped by a watchpoint. Mark all watchpoints
4869 as not triggered. */
4870 ALL_BREAKPOINTS (b)
4871 if (is_hardware_watchpoint (b))
4872 {
4873 struct watchpoint *w = (struct watchpoint *) b;
4874
4875 w->watchpoint_triggered = watch_triggered_no;
4876 }
4877
4878 return 0;
4879 }
4880
4881 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
4882 {
4883 /* We were stopped by a watchpoint, but we don't know where.
4884 Mark all watchpoints as unknown. */
4885 ALL_BREAKPOINTS (b)
4886 if (is_hardware_watchpoint (b))
4887 {
4888 struct watchpoint *w = (struct watchpoint *) b;
4889
4890 w->watchpoint_triggered = watch_triggered_unknown;
4891 }
4892
4893 return 1;
4894 }
4895
4896 /* The target could report the data address. Mark watchpoints
4897 affected by this data address as triggered, and all others as not
4898 triggered. */
4899
4900 ALL_BREAKPOINTS (b)
4901 if (is_hardware_watchpoint (b))
4902 {
4903 struct watchpoint *w = (struct watchpoint *) b;
4904 struct bp_location *loc;
4905
4906 w->watchpoint_triggered = watch_triggered_no;
4907 for (loc = b->loc; loc; loc = loc->next)
4908 {
4909 if (is_masked_watchpoint (b))
4910 {
4911 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4912 CORE_ADDR start = loc->address & w->hw_wp_mask;
4913
4914 if (newaddr == start)
4915 {
4916 w->watchpoint_triggered = watch_triggered_yes;
4917 break;
4918 }
4919 }
4920 /* Exact match not required. Within range is sufficient. */
4921 else if (target_watchpoint_addr_within_range
4922 (current_inferior ()->top_target (), addr, loc->address,
4923 loc->length))
4924 {
4925 w->watchpoint_triggered = watch_triggered_yes;
4926 break;
4927 }
4928 }
4929 }
4930
4931 return 1;
4932 }
4933
4934 /* Possible return values for watchpoint_check. */
4935 enum wp_check_result
4936 {
4937 /* The watchpoint has been deleted. */
4938 WP_DELETED = 1,
4939
4940 /* The value has changed. */
4941 WP_VALUE_CHANGED = 2,
4942
4943 /* The value has not changed. */
4944 WP_VALUE_NOT_CHANGED = 3,
4945
4946 /* Ignore this watchpoint, no matter if the value changed or not. */
4947 WP_IGNORE = 4,
4948 };
4949
4950 #define BP_TEMPFLAG 1
4951 #define BP_HARDWAREFLAG 2
4952
4953 /* Evaluate watchpoint condition expression and check if its value
4954 changed. */
4955
4956 static wp_check_result
4957 watchpoint_check (bpstat bs)
4958 {
4959 struct watchpoint *b;
4960 struct frame_info *fr;
4961 int within_current_scope;
4962
4963 /* BS is built from an existing struct breakpoint. */
4964 gdb_assert (bs->breakpoint_at != NULL);
4965 b = (struct watchpoint *) bs->breakpoint_at;
4966
4967 /* If this is a local watchpoint, we only want to check if the
4968 watchpoint frame is in scope if the current thread is the thread
4969 that was used to create the watchpoint. */
4970 if (!watchpoint_in_thread_scope (b))
4971 return WP_IGNORE;
4972
4973 if (b->exp_valid_block == NULL)
4974 within_current_scope = 1;
4975 else
4976 {
4977 struct frame_info *frame = get_current_frame ();
4978 struct gdbarch *frame_arch = get_frame_arch (frame);
4979 CORE_ADDR frame_pc = get_frame_pc (frame);
4980
4981 /* stack_frame_destroyed_p() returns a non-zero value if we're
4982 still in the function but the stack frame has already been
4983 invalidated. Since we can't rely on the values of local
4984 variables after the stack has been destroyed, we are treating
4985 the watchpoint in that state as `not changed' without further
4986 checking. Don't mark watchpoints as changed if the current
4987 frame is in an epilogue - even if they are in some other
4988 frame, our view of the stack is likely to be wrong and
4989 frame_find_by_id could error out. */
4990 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
4991 return WP_IGNORE;
4992
4993 fr = frame_find_by_id (b->watchpoint_frame);
4994 within_current_scope = (fr != NULL);
4995
4996 /* If we've gotten confused in the unwinder, we might have
4997 returned a frame that can't describe this variable. */
4998 if (within_current_scope)
4999 {
5000 struct symbol *function;
5001
5002 function = get_frame_function (fr);
5003 if (function == NULL
5004 || !contained_in (b->exp_valid_block,
5005 SYMBOL_BLOCK_VALUE (function)))
5006 within_current_scope = 0;
5007 }
5008
5009 if (within_current_scope)
5010 /* If we end up stopping, the current frame will get selected
5011 in normal_stop. So this call to select_frame won't affect
5012 the user. */
5013 select_frame (fr);
5014 }
5015
5016 if (within_current_scope)
5017 {
5018 /* We use value_{,free_to_}mark because it could be a *long*
5019 time before we return to the command level and call
5020 free_all_values. We can't call free_all_values because we
5021 might be in the middle of evaluating a function call. */
5022
5023 struct value *mark;
5024 struct value *new_val;
5025
5026 if (is_masked_watchpoint (b))
5027 /* Since we don't know the exact trigger address (from
5028 stopped_data_address), just tell the user we've triggered
5029 a mask watchpoint. */
5030 return WP_VALUE_CHANGED;
5031
5032 mark = value_mark ();
5033 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
5034 NULL, NULL, false);
5035
5036 if (b->val_bitsize != 0)
5037 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5038
5039 /* We use value_equal_contents instead of value_equal because
5040 the latter coerces an array to a pointer, thus comparing just
5041 the address of the array instead of its contents. This is
5042 not what we want. */
5043 if ((b->val != NULL) != (new_val != NULL)
5044 || (b->val != NULL && !value_equal_contents (b->val.get (),
5045 new_val)))
5046 {
5047 bs->old_val = b->val;
5048 b->val = release_value (new_val);
5049 b->val_valid = true;
5050 if (new_val != NULL)
5051 value_free_to_mark (mark);
5052 return WP_VALUE_CHANGED;
5053 }
5054 else
5055 {
5056 /* Nothing changed. */
5057 value_free_to_mark (mark);
5058 return WP_VALUE_NOT_CHANGED;
5059 }
5060 }
5061 else
5062 {
5063 /* This seems like the only logical thing to do because
5064 if we temporarily ignored the watchpoint, then when
5065 we reenter the block in which it is valid it contains
5066 garbage (in the case of a function, it may have two
5067 garbage values, one before and one after the prologue).
5068 So we can't even detect the first assignment to it and
5069 watch after that (since the garbage may or may not equal
5070 the first value assigned). */
5071 /* We print all the stop information in
5072 breakpoint_ops->print_it, but in this case, by the time we
5073 call breakpoint_ops->print_it this bp will be deleted
5074 already. So we have no choice but print the information
5075 here. */
5076
5077 SWITCH_THRU_ALL_UIS ()
5078 {
5079 struct ui_out *uiout = current_uiout;
5080
5081 if (uiout->is_mi_like_p ())
5082 uiout->field_string
5083 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5084 uiout->message ("\nWatchpoint %pF deleted because the program has "
5085 "left the block in\n"
5086 "which its expression is valid.\n",
5087 signed_field ("wpnum", b->number));
5088 }
5089
5090 /* Make sure the watchpoint's commands aren't executed. */
5091 b->commands = NULL;
5092 watchpoint_del_at_next_stop (b);
5093
5094 return WP_DELETED;
5095 }
5096 }
5097
5098 /* Return true if it looks like target has stopped due to hitting
5099 breakpoint location BL. This function does not check if we should
5100 stop, only if BL explains the stop. */
5101
5102 static int
5103 bpstat_check_location (const struct bp_location *bl,
5104 const address_space *aspace, CORE_ADDR bp_addr,
5105 const struct target_waitstatus *ws)
5106 {
5107 struct breakpoint *b = bl->owner;
5108
5109 /* BL is from an existing breakpoint. */
5110 gdb_assert (b != NULL);
5111
5112 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5113 }
5114
5115 /* Determine if the watched values have actually changed, and we
5116 should stop. If not, set BS->stop to 0. */
5117
5118 static void
5119 bpstat_check_watchpoint (bpstat bs)
5120 {
5121 const struct bp_location *bl;
5122 struct watchpoint *b;
5123
5124 /* BS is built for existing struct breakpoint. */
5125 bl = bs->bp_location_at.get ();
5126 gdb_assert (bl != NULL);
5127 b = (struct watchpoint *) bs->breakpoint_at;
5128 gdb_assert (b != NULL);
5129
5130 {
5131 int must_check_value = 0;
5132
5133 if (b->type == bp_watchpoint)
5134 /* For a software watchpoint, we must always check the
5135 watched value. */
5136 must_check_value = 1;
5137 else if (b->watchpoint_triggered == watch_triggered_yes)
5138 /* We have a hardware watchpoint (read, write, or access)
5139 and the target earlier reported an address watched by
5140 this watchpoint. */
5141 must_check_value = 1;
5142 else if (b->watchpoint_triggered == watch_triggered_unknown
5143 && b->type == bp_hardware_watchpoint)
5144 /* We were stopped by a hardware watchpoint, but the target could
5145 not report the data address. We must check the watchpoint's
5146 value. Access and read watchpoints are out of luck; without
5147 a data address, we can't figure it out. */
5148 must_check_value = 1;
5149
5150 if (must_check_value)
5151 {
5152 wp_check_result e;
5153
5154 try
5155 {
5156 e = watchpoint_check (bs);
5157 }
5158 catch (const gdb_exception &ex)
5159 {
5160 exception_fprintf (gdb_stderr, ex,
5161 "Error evaluating expression "
5162 "for watchpoint %d\n",
5163 b->number);
5164
5165 SWITCH_THRU_ALL_UIS ()
5166 {
5167 printf_filtered (_("Watchpoint %d deleted.\n"),
5168 b->number);
5169 }
5170 watchpoint_del_at_next_stop (b);
5171 e = WP_DELETED;
5172 }
5173
5174 switch (e)
5175 {
5176 case WP_DELETED:
5177 /* We've already printed what needs to be printed. */
5178 bs->print_it = print_it_done;
5179 /* Stop. */
5180 break;
5181 case WP_IGNORE:
5182 bs->print_it = print_it_noop;
5183 bs->stop = 0;
5184 break;
5185 case WP_VALUE_CHANGED:
5186 if (b->type == bp_read_watchpoint)
5187 {
5188 /* There are two cases to consider here:
5189
5190 1. We're watching the triggered memory for reads.
5191 In that case, trust the target, and always report
5192 the watchpoint hit to the user. Even though
5193 reads don't cause value changes, the value may
5194 have changed since the last time it was read, and
5195 since we're not trapping writes, we will not see
5196 those, and as such we should ignore our notion of
5197 old value.
5198
5199 2. We're watching the triggered memory for both
5200 reads and writes. There are two ways this may
5201 happen:
5202
5203 2.1. This is a target that can't break on data
5204 reads only, but can break on accesses (reads or
5205 writes), such as e.g., x86. We detect this case
5206 at the time we try to insert read watchpoints.
5207
5208 2.2. Otherwise, the target supports read
5209 watchpoints, but, the user set an access or write
5210 watchpoint watching the same memory as this read
5211 watchpoint.
5212
5213 If we're watching memory writes as well as reads,
5214 ignore watchpoint hits when we find that the
5215 value hasn't changed, as reads don't cause
5216 changes. This still gives false positives when
5217 the program writes the same value to memory as
5218 what there was already in memory (we will confuse
5219 it for a read), but it's much better than
5220 nothing. */
5221
5222 int other_write_watchpoint = 0;
5223
5224 if (bl->watchpoint_type == hw_read)
5225 {
5226 struct breakpoint *other_b;
5227
5228 ALL_BREAKPOINTS (other_b)
5229 if (other_b->type == bp_hardware_watchpoint
5230 || other_b->type == bp_access_watchpoint)
5231 {
5232 struct watchpoint *other_w =
5233 (struct watchpoint *) other_b;
5234
5235 if (other_w->watchpoint_triggered
5236 == watch_triggered_yes)
5237 {
5238 other_write_watchpoint = 1;
5239 break;
5240 }
5241 }
5242 }
5243
5244 if (other_write_watchpoint
5245 || bl->watchpoint_type == hw_access)
5246 {
5247 /* We're watching the same memory for writes,
5248 and the value changed since the last time we
5249 updated it, so this trap must be for a write.
5250 Ignore it. */
5251 bs->print_it = print_it_noop;
5252 bs->stop = 0;
5253 }
5254 }
5255 break;
5256 case WP_VALUE_NOT_CHANGED:
5257 if (b->type == bp_hardware_watchpoint
5258 || b->type == bp_watchpoint)
5259 {
5260 /* Don't stop: write watchpoints shouldn't fire if
5261 the value hasn't changed. */
5262 bs->print_it = print_it_noop;
5263 bs->stop = 0;
5264 }
5265 /* Stop. */
5266 break;
5267 default:
5268 /* Can't happen. */
5269 break;
5270 }
5271 }
5272 else /* must_check_value == 0 */
5273 {
5274 /* This is a case where some watchpoint(s) triggered, but
5275 not at the address of this watchpoint, or else no
5276 watchpoint triggered after all. So don't print
5277 anything for this watchpoint. */
5278 bs->print_it = print_it_noop;
5279 bs->stop = 0;
5280 }
5281 }
5282 }
5283
5284 /* For breakpoints that are currently marked as telling gdb to stop,
5285 check conditions (condition proper, frame, thread and ignore count)
5286 of breakpoint referred to by BS. If we should not stop for this
5287 breakpoint, set BS->stop to 0. */
5288
5289 static void
5290 bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
5291 {
5292 const struct bp_location *bl;
5293 struct breakpoint *b;
5294 /* Assume stop. */
5295 bool condition_result = true;
5296 struct expression *cond;
5297
5298 gdb_assert (bs->stop);
5299
5300 /* BS is built for existing struct breakpoint. */
5301 bl = bs->bp_location_at.get ();
5302 gdb_assert (bl != NULL);
5303 b = bs->breakpoint_at;
5304 gdb_assert (b != NULL);
5305
5306 /* Even if the target evaluated the condition on its end and notified GDB, we
5307 need to do so again since GDB does not know if we stopped due to a
5308 breakpoint or a single step breakpoint. */
5309
5310 if (frame_id_p (b->frame_id)
5311 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5312 {
5313 bs->stop = 0;
5314 return;
5315 }
5316
5317 /* If this is a thread/task-specific breakpoint, don't waste cpu
5318 evaluating the condition if this isn't the specified
5319 thread/task. */
5320 if ((b->thread != -1 && b->thread != thread->global_num)
5321 || (b->task != 0 && b->task != ada_get_task_number (thread)))
5322 {
5323 bs->stop = 0;
5324 return;
5325 }
5326
5327 /* Evaluate extension language breakpoints that have a "stop" method
5328 implemented. */
5329 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5330
5331 if (is_watchpoint (b))
5332 {
5333 struct watchpoint *w = (struct watchpoint *) b;
5334
5335 cond = w->cond_exp.get ();
5336 }
5337 else
5338 cond = bl->cond.get ();
5339
5340 if (cond && b->disposition != disp_del_at_next_stop)
5341 {
5342 int within_current_scope = 1;
5343 struct watchpoint * w;
5344
5345 /* We use value_mark and value_free_to_mark because it could
5346 be a long time before we return to the command level and
5347 call free_all_values. We can't call free_all_values
5348 because we might be in the middle of evaluating a
5349 function call. */
5350 struct value *mark = value_mark ();
5351
5352 if (is_watchpoint (b))
5353 w = (struct watchpoint *) b;
5354 else
5355 w = NULL;
5356
5357 /* Need to select the frame, with all that implies so that
5358 the conditions will have the right context. Because we
5359 use the frame, we will not see an inlined function's
5360 variables when we arrive at a breakpoint at the start
5361 of the inlined function; the current frame will be the
5362 call site. */
5363 if (w == NULL || w->cond_exp_valid_block == NULL)
5364 select_frame (get_current_frame ());
5365 else
5366 {
5367 struct frame_info *frame;
5368
5369 /* For local watchpoint expressions, which particular
5370 instance of a local is being watched matters, so we
5371 keep track of the frame to evaluate the expression
5372 in. To evaluate the condition however, it doesn't
5373 really matter which instantiation of the function
5374 where the condition makes sense triggers the
5375 watchpoint. This allows an expression like "watch
5376 global if q > 10" set in `func', catch writes to
5377 global on all threads that call `func', or catch
5378 writes on all recursive calls of `func' by a single
5379 thread. We simply always evaluate the condition in
5380 the innermost frame that's executing where it makes
5381 sense to evaluate the condition. It seems
5382 intuitive. */
5383 frame = block_innermost_frame (w->cond_exp_valid_block);
5384 if (frame != NULL)
5385 select_frame (frame);
5386 else
5387 within_current_scope = 0;
5388 }
5389 if (within_current_scope)
5390 {
5391 try
5392 {
5393 condition_result = breakpoint_cond_eval (cond);
5394 }
5395 catch (const gdb_exception &ex)
5396 {
5397 exception_fprintf (gdb_stderr, ex,
5398 "Error in testing breakpoint condition:\n");
5399 }
5400 }
5401 else
5402 {
5403 warning (_("Watchpoint condition cannot be tested "
5404 "in the current scope"));
5405 /* If we failed to set the right context for this
5406 watchpoint, unconditionally report it. */
5407 }
5408 /* FIXME-someday, should give breakpoint #. */
5409 value_free_to_mark (mark);
5410 }
5411
5412 if (cond && !condition_result)
5413 {
5414 bs->stop = 0;
5415 }
5416 else if (b->ignore_count > 0)
5417 {
5418 b->ignore_count--;
5419 bs->stop = 0;
5420 /* Increase the hit count even though we don't stop. */
5421 ++(b->hit_count);
5422 gdb::observers::breakpoint_modified.notify (b);
5423 }
5424 }
5425
5426 /* Returns true if we need to track moribund locations of LOC's type
5427 on the current target. */
5428
5429 static int
5430 need_moribund_for_location_type (struct bp_location *loc)
5431 {
5432 return ((loc->loc_type == bp_loc_software_breakpoint
5433 && !target_supports_stopped_by_sw_breakpoint ())
5434 || (loc->loc_type == bp_loc_hardware_breakpoint
5435 && !target_supports_stopped_by_hw_breakpoint ()));
5436 }
5437
5438 /* See breakpoint.h. */
5439
5440 bpstat
5441 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5442 const struct target_waitstatus *ws)
5443 {
5444 struct breakpoint *b;
5445 bpstat bs_head = NULL, *bs_link = &bs_head;
5446
5447 ALL_BREAKPOINTS (b)
5448 {
5449 if (!breakpoint_enabled (b))
5450 continue;
5451
5452 for (bp_location *bl = b->loc; bl != NULL; bl = bl->next)
5453 {
5454 /* For hardware watchpoints, we look only at the first
5455 location. The watchpoint_check function will work on the
5456 entire expression, not the individual locations. For
5457 read watchpoints, the watchpoints_triggered function has
5458 checked all locations already. */
5459 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5460 break;
5461
5462 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
5463 continue;
5464
5465 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5466 continue;
5467
5468 /* Come here if it's a watchpoint, or if the break address
5469 matches. */
5470
5471 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5472 explain stop. */
5473
5474 /* Assume we stop. Should we find a watchpoint that is not
5475 actually triggered, or if the condition of the breakpoint
5476 evaluates as false, we'll reset 'stop' to 0. */
5477 bs->stop = 1;
5478 bs->print = 1;
5479
5480 /* If this is a scope breakpoint, mark the associated
5481 watchpoint as triggered so that we will handle the
5482 out-of-scope event. We'll get to the watchpoint next
5483 iteration. */
5484 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5485 {
5486 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5487
5488 w->watchpoint_triggered = watch_triggered_yes;
5489 }
5490 }
5491 }
5492
5493 /* Check if a moribund breakpoint explains the stop. */
5494 if (!target_supports_stopped_by_sw_breakpoint ()
5495 || !target_supports_stopped_by_hw_breakpoint ())
5496 {
5497 for (bp_location *loc : moribund_locations)
5498 {
5499 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5500 && need_moribund_for_location_type (loc))
5501 {
5502 bpstat bs = new bpstats (loc, &bs_link);
5503 /* For hits of moribund locations, we should just proceed. */
5504 bs->stop = 0;
5505 bs->print = 0;
5506 bs->print_it = print_it_noop;
5507 }
5508 }
5509 }
5510
5511 return bs_head;
5512 }
5513
5514 /* See breakpoint.h. */
5515
5516 bpstat
5517 bpstat_stop_status (const address_space *aspace,
5518 CORE_ADDR bp_addr, thread_info *thread,
5519 const struct target_waitstatus *ws,
5520 bpstat stop_chain)
5521 {
5522 struct breakpoint *b = NULL;
5523 /* First item of allocated bpstat's. */
5524 bpstat bs_head = stop_chain;
5525 bpstat bs;
5526 int need_remove_insert;
5527 int removed_any;
5528
5529 /* First, build the bpstat chain with locations that explain a
5530 target stop, while being careful to not set the target running,
5531 as that may invalidate locations (in particular watchpoint
5532 locations are recreated). Resuming will happen here with
5533 breakpoint conditions or watchpoint expressions that include
5534 inferior function calls. */
5535 if (bs_head == NULL)
5536 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5537
5538 /* A bit of special processing for shlib breakpoints. We need to
5539 process solib loading here, so that the lists of loaded and
5540 unloaded libraries are correct before we handle "catch load" and
5541 "catch unload". */
5542 for (bs = bs_head; bs != NULL; bs = bs->next)
5543 {
5544 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5545 {
5546 handle_solib_event ();
5547 break;
5548 }
5549 }
5550
5551 /* Now go through the locations that caused the target to stop, and
5552 check whether we're interested in reporting this stop to higher
5553 layers, or whether we should resume the target transparently. */
5554
5555 removed_any = 0;
5556
5557 for (bs = bs_head; bs != NULL; bs = bs->next)
5558 {
5559 if (!bs->stop)
5560 continue;
5561
5562 b = bs->breakpoint_at;
5563 b->ops->check_status (bs);
5564 if (bs->stop)
5565 {
5566 bpstat_check_breakpoint_conditions (bs, thread);
5567
5568 if (bs->stop)
5569 {
5570 ++(b->hit_count);
5571 gdb::observers::breakpoint_modified.notify (b);
5572
5573 /* We will stop here. */
5574 if (b->disposition == disp_disable)
5575 {
5576 --(b->enable_count);
5577 if (b->enable_count <= 0)
5578 b->enable_state = bp_disabled;
5579 removed_any = 1;
5580 }
5581 if (b->silent)
5582 bs->print = 0;
5583 bs->commands = b->commands;
5584 if (command_line_is_silent (bs->commands
5585 ? bs->commands.get () : NULL))
5586 bs->print = 0;
5587
5588 b->ops->after_condition_true (bs);
5589 }
5590
5591 }
5592
5593 /* Print nothing for this entry if we don't stop or don't
5594 print. */
5595 if (!bs->stop || !bs->print)
5596 bs->print_it = print_it_noop;
5597 }
5598
5599 /* If we aren't stopping, the value of some hardware watchpoint may
5600 not have changed, but the intermediate memory locations we are
5601 watching may have. Don't bother if we're stopping; this will get
5602 done later. */
5603 need_remove_insert = 0;
5604 if (! bpstat_causes_stop (bs_head))
5605 for (bs = bs_head; bs != NULL; bs = bs->next)
5606 if (!bs->stop
5607 && bs->breakpoint_at
5608 && is_hardware_watchpoint (bs->breakpoint_at))
5609 {
5610 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5611
5612 update_watchpoint (w, 0 /* don't reparse. */);
5613 need_remove_insert = 1;
5614 }
5615
5616 if (need_remove_insert)
5617 update_global_location_list (UGLL_MAY_INSERT);
5618 else if (removed_any)
5619 update_global_location_list (UGLL_DONT_INSERT);
5620
5621 return bs_head;
5622 }
5623
5624 static void
5625 handle_jit_event (CORE_ADDR address)
5626 {
5627 struct gdbarch *gdbarch;
5628
5629 infrun_debug_printf ("handling bp_jit_event");
5630
5631 /* Switch terminal for any messages produced by
5632 breakpoint_re_set. */
5633 target_terminal::ours_for_output ();
5634
5635 gdbarch = get_frame_arch (get_current_frame ());
5636 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5637 thus it is expected that its objectfile can be found through
5638 minimal symbol lookup. If it doesn't work (and assert fails), it
5639 most likely means that `jit_breakpoint_re_set` was changes and this
5640 function needs to be updated too. */
5641 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5642 gdb_assert (jit_bp_sym.objfile != nullptr);
5643 jit_event_handler (gdbarch, jit_bp_sym.objfile);
5644
5645 target_terminal::inferior ();
5646 }
5647
5648 /* Prepare WHAT final decision for infrun. */
5649
5650 /* Decide what infrun needs to do with this bpstat. */
5651
5652 struct bpstat_what
5653 bpstat_what (bpstat bs_head)
5654 {
5655 struct bpstat_what retval;
5656 bpstat bs;
5657
5658 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5659 retval.call_dummy = STOP_NONE;
5660 retval.is_longjmp = false;
5661
5662 for (bs = bs_head; bs != NULL; bs = bs->next)
5663 {
5664 /* Extract this BS's action. After processing each BS, we check
5665 if its action overrides all we've seem so far. */
5666 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5667 enum bptype bptype;
5668
5669 if (bs->breakpoint_at == NULL)
5670 {
5671 /* I suspect this can happen if it was a momentary
5672 breakpoint which has since been deleted. */
5673 bptype = bp_none;
5674 }
5675 else
5676 bptype = bs->breakpoint_at->type;
5677
5678 switch (bptype)
5679 {
5680 case bp_none:
5681 break;
5682 case bp_breakpoint:
5683 case bp_hardware_breakpoint:
5684 case bp_single_step:
5685 case bp_until:
5686 case bp_finish:
5687 case bp_shlib_event:
5688 if (bs->stop)
5689 {
5690 if (bs->print)
5691 this_action = BPSTAT_WHAT_STOP_NOISY;
5692 else
5693 this_action = BPSTAT_WHAT_STOP_SILENT;
5694 }
5695 else
5696 this_action = BPSTAT_WHAT_SINGLE;
5697 break;
5698 case bp_watchpoint:
5699 case bp_hardware_watchpoint:
5700 case bp_read_watchpoint:
5701 case bp_access_watchpoint:
5702 if (bs->stop)
5703 {
5704 if (bs->print)
5705 this_action = BPSTAT_WHAT_STOP_NOISY;
5706 else
5707 this_action = BPSTAT_WHAT_STOP_SILENT;
5708 }
5709 else
5710 {
5711 /* There was a watchpoint, but we're not stopping.
5712 This requires no further action. */
5713 }
5714 break;
5715 case bp_longjmp:
5716 case bp_longjmp_call_dummy:
5717 case bp_exception:
5718 if (bs->stop)
5719 {
5720 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5721 retval.is_longjmp = bptype != bp_exception;
5722 }
5723 else
5724 this_action = BPSTAT_WHAT_SINGLE;
5725 break;
5726 case bp_longjmp_resume:
5727 case bp_exception_resume:
5728 if (bs->stop)
5729 {
5730 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5731 retval.is_longjmp = bptype == bp_longjmp_resume;
5732 }
5733 else
5734 this_action = BPSTAT_WHAT_SINGLE;
5735 break;
5736 case bp_step_resume:
5737 if (bs->stop)
5738 this_action = BPSTAT_WHAT_STEP_RESUME;
5739 else
5740 {
5741 /* It is for the wrong frame. */
5742 this_action = BPSTAT_WHAT_SINGLE;
5743 }
5744 break;
5745 case bp_hp_step_resume:
5746 if (bs->stop)
5747 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5748 else
5749 {
5750 /* It is for the wrong frame. */
5751 this_action = BPSTAT_WHAT_SINGLE;
5752 }
5753 break;
5754 case bp_watchpoint_scope:
5755 case bp_thread_event:
5756 case bp_overlay_event:
5757 case bp_longjmp_master:
5758 case bp_std_terminate_master:
5759 case bp_exception_master:
5760 this_action = BPSTAT_WHAT_SINGLE;
5761 break;
5762 case bp_catchpoint:
5763 if (bs->stop)
5764 {
5765 if (bs->print)
5766 this_action = BPSTAT_WHAT_STOP_NOISY;
5767 else
5768 this_action = BPSTAT_WHAT_STOP_SILENT;
5769 }
5770 else
5771 {
5772 /* Some catchpoints are implemented with breakpoints.
5773 For those, we need to step over the breakpoint. */
5774 if (bs->bp_location_at->loc_type != bp_loc_other)
5775 this_action = BPSTAT_WHAT_SINGLE;
5776 }
5777 break;
5778 case bp_jit_event:
5779 this_action = BPSTAT_WHAT_SINGLE;
5780 break;
5781 case bp_call_dummy:
5782 /* Make sure the action is stop (silent or noisy),
5783 so infrun.c pops the dummy frame. */
5784 retval.call_dummy = STOP_STACK_DUMMY;
5785 this_action = BPSTAT_WHAT_STOP_SILENT;
5786 break;
5787 case bp_std_terminate:
5788 /* Make sure the action is stop (silent or noisy),
5789 so infrun.c pops the dummy frame. */
5790 retval.call_dummy = STOP_STD_TERMINATE;
5791 this_action = BPSTAT_WHAT_STOP_SILENT;
5792 break;
5793 case bp_tracepoint:
5794 case bp_fast_tracepoint:
5795 case bp_static_tracepoint:
5796 /* Tracepoint hits should not be reported back to GDB, and
5797 if one got through somehow, it should have been filtered
5798 out already. */
5799 internal_error (__FILE__, __LINE__,
5800 _("bpstat_what: tracepoint encountered"));
5801 break;
5802 case bp_gnu_ifunc_resolver:
5803 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5804 this_action = BPSTAT_WHAT_SINGLE;
5805 break;
5806 case bp_gnu_ifunc_resolver_return:
5807 /* The breakpoint will be removed, execution will restart from the
5808 PC of the former breakpoint. */
5809 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5810 break;
5811
5812 case bp_dprintf:
5813 if (bs->stop)
5814 this_action = BPSTAT_WHAT_STOP_SILENT;
5815 else
5816 this_action = BPSTAT_WHAT_SINGLE;
5817 break;
5818
5819 default:
5820 internal_error (__FILE__, __LINE__,
5821 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5822 }
5823
5824 retval.main_action = std::max (retval.main_action, this_action);
5825 }
5826
5827 return retval;
5828 }
5829
5830 void
5831 bpstat_run_callbacks (bpstat bs_head)
5832 {
5833 bpstat bs;
5834
5835 for (bs = bs_head; bs != NULL; bs = bs->next)
5836 {
5837 struct breakpoint *b = bs->breakpoint_at;
5838
5839 if (b == NULL)
5840 continue;
5841 switch (b->type)
5842 {
5843 case bp_jit_event:
5844 handle_jit_event (bs->bp_location_at->address);
5845 break;
5846 case bp_gnu_ifunc_resolver:
5847 gnu_ifunc_resolver_stop (b);
5848 break;
5849 case bp_gnu_ifunc_resolver_return:
5850 gnu_ifunc_resolver_return_stop (b);
5851 break;
5852 }
5853 }
5854 }
5855
5856 /* See breakpoint.h. */
5857
5858 bool
5859 bpstat_should_step ()
5860 {
5861 struct breakpoint *b;
5862
5863 ALL_BREAKPOINTS (b)
5864 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5865 return true;
5866 return false;
5867 }
5868
5869 /* See breakpoint.h. */
5870
5871 bool
5872 bpstat_causes_stop (bpstat bs)
5873 {
5874 for (; bs != NULL; bs = bs->next)
5875 if (bs->stop)
5876 return true;
5877
5878 return false;
5879 }
5880
5881 \f
5882
5883 /* Compute a string of spaces suitable to indent the next line
5884 so it starts at the position corresponding to the table column
5885 named COL_NAME in the currently active table of UIOUT. */
5886
5887 static char *
5888 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5889 {
5890 static char wrap_indent[80];
5891 int i, total_width, width, align;
5892 const char *text;
5893
5894 total_width = 0;
5895 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5896 {
5897 if (strcmp (text, col_name) == 0)
5898 {
5899 gdb_assert (total_width < sizeof wrap_indent);
5900 memset (wrap_indent, ' ', total_width);
5901 wrap_indent[total_width] = 0;
5902
5903 return wrap_indent;
5904 }
5905
5906 total_width += width + 1;
5907 }
5908
5909 return NULL;
5910 }
5911
5912 /* Determine if the locations of this breakpoint will have their conditions
5913 evaluated by the target, host or a mix of both. Returns the following:
5914
5915 "host": Host evals condition.
5916 "host or target": Host or Target evals condition.
5917 "target": Target evals condition.
5918 */
5919
5920 static const char *
5921 bp_condition_evaluator (struct breakpoint *b)
5922 {
5923 struct bp_location *bl;
5924 char host_evals = 0;
5925 char target_evals = 0;
5926
5927 if (!b)
5928 return NULL;
5929
5930 if (!is_breakpoint (b))
5931 return NULL;
5932
5933 if (gdb_evaluates_breakpoint_condition_p ()
5934 || !target_supports_evaluation_of_breakpoint_conditions ())
5935 return condition_evaluation_host;
5936
5937 for (bl = b->loc; bl; bl = bl->next)
5938 {
5939 if (bl->cond_bytecode)
5940 target_evals++;
5941 else
5942 host_evals++;
5943 }
5944
5945 if (host_evals && target_evals)
5946 return condition_evaluation_both;
5947 else if (target_evals)
5948 return condition_evaluation_target;
5949 else
5950 return condition_evaluation_host;
5951 }
5952
5953 /* Determine the breakpoint location's condition evaluator. This is
5954 similar to bp_condition_evaluator, but for locations. */
5955
5956 static const char *
5957 bp_location_condition_evaluator (struct bp_location *bl)
5958 {
5959 if (bl && !is_breakpoint (bl->owner))
5960 return NULL;
5961
5962 if (gdb_evaluates_breakpoint_condition_p ()
5963 || !target_supports_evaluation_of_breakpoint_conditions ())
5964 return condition_evaluation_host;
5965
5966 if (bl && bl->cond_bytecode)
5967 return condition_evaluation_target;
5968 else
5969 return condition_evaluation_host;
5970 }
5971
5972 /* Print the LOC location out of the list of B->LOC locations. */
5973
5974 static void
5975 print_breakpoint_location (struct breakpoint *b,
5976 struct bp_location *loc)
5977 {
5978 struct ui_out *uiout = current_uiout;
5979
5980 scoped_restore_current_program_space restore_pspace;
5981
5982 if (loc != NULL && loc->shlib_disabled)
5983 loc = NULL;
5984
5985 if (loc != NULL)
5986 set_current_program_space (loc->pspace);
5987
5988 if (b->display_canonical)
5989 uiout->field_string ("what", event_location_to_string (b->location.get ()));
5990 else if (loc && loc->symtab)
5991 {
5992 const struct symbol *sym = loc->symbol;
5993
5994 if (sym)
5995 {
5996 uiout->text ("in ");
5997 uiout->field_string ("func", sym->print_name (),
5998 function_name_style.style ());
5999 uiout->text (" ");
6000 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6001 uiout->text ("at ");
6002 }
6003 uiout->field_string ("file",
6004 symtab_to_filename_for_display (loc->symtab),
6005 file_name_style.style ());
6006 uiout->text (":");
6007
6008 if (uiout->is_mi_like_p ())
6009 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
6010
6011 uiout->field_signed ("line", loc->line_number);
6012 }
6013 else if (loc)
6014 {
6015 string_file stb;
6016
6017 print_address_symbolic (loc->gdbarch, loc->address, &stb,
6018 demangle, "");
6019 uiout->field_stream ("at", stb);
6020 }
6021 else
6022 {
6023 uiout->field_string ("pending",
6024 event_location_to_string (b->location.get ()));
6025 /* If extra_string is available, it could be holding a condition
6026 or dprintf arguments. In either case, make sure it is printed,
6027 too, but only for non-MI streams. */
6028 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
6029 {
6030 if (b->type == bp_dprintf)
6031 uiout->text (",");
6032 else
6033 uiout->text (" ");
6034 uiout->text (b->extra_string);
6035 }
6036 }
6037
6038 if (loc && is_breakpoint (b)
6039 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6040 && bp_condition_evaluator (b) == condition_evaluation_both)
6041 {
6042 uiout->text (" (");
6043 uiout->field_string ("evaluated-by",
6044 bp_location_condition_evaluator (loc));
6045 uiout->text (")");
6046 }
6047 }
6048
6049 static const char *
6050 bptype_string (enum bptype type)
6051 {
6052 struct ep_type_description
6053 {
6054 enum bptype type;
6055 const char *description;
6056 };
6057 static struct ep_type_description bptypes[] =
6058 {
6059 {bp_none, "?deleted?"},
6060 {bp_breakpoint, "breakpoint"},
6061 {bp_hardware_breakpoint, "hw breakpoint"},
6062 {bp_single_step, "sw single-step"},
6063 {bp_until, "until"},
6064 {bp_finish, "finish"},
6065 {bp_watchpoint, "watchpoint"},
6066 {bp_hardware_watchpoint, "hw watchpoint"},
6067 {bp_read_watchpoint, "read watchpoint"},
6068 {bp_access_watchpoint, "acc watchpoint"},
6069 {bp_longjmp, "longjmp"},
6070 {bp_longjmp_resume, "longjmp resume"},
6071 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6072 {bp_exception, "exception"},
6073 {bp_exception_resume, "exception resume"},
6074 {bp_step_resume, "step resume"},
6075 {bp_hp_step_resume, "high-priority step resume"},
6076 {bp_watchpoint_scope, "watchpoint scope"},
6077 {bp_call_dummy, "call dummy"},
6078 {bp_std_terminate, "std::terminate"},
6079 {bp_shlib_event, "shlib events"},
6080 {bp_thread_event, "thread events"},
6081 {bp_overlay_event, "overlay events"},
6082 {bp_longjmp_master, "longjmp master"},
6083 {bp_std_terminate_master, "std::terminate master"},
6084 {bp_exception_master, "exception master"},
6085 {bp_catchpoint, "catchpoint"},
6086 {bp_tracepoint, "tracepoint"},
6087 {bp_fast_tracepoint, "fast tracepoint"},
6088 {bp_static_tracepoint, "static tracepoint"},
6089 {bp_dprintf, "dprintf"},
6090 {bp_jit_event, "jit events"},
6091 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6092 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6093 };
6094
6095 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6096 || ((int) type != bptypes[(int) type].type))
6097 internal_error (__FILE__, __LINE__,
6098 _("bptypes table does not describe type #%d."),
6099 (int) type);
6100
6101 return bptypes[(int) type].description;
6102 }
6103
6104 /* For MI, output a field named 'thread-groups' with a list as the value.
6105 For CLI, prefix the list with the string 'inf'. */
6106
6107 static void
6108 output_thread_groups (struct ui_out *uiout,
6109 const char *field_name,
6110 const std::vector<int> &inf_nums,
6111 int mi_only)
6112 {
6113 int is_mi = uiout->is_mi_like_p ();
6114
6115 /* For backward compatibility, don't display inferiors in CLI unless
6116 there are several. Always display them for MI. */
6117 if (!is_mi && mi_only)
6118 return;
6119
6120 ui_out_emit_list list_emitter (uiout, field_name);
6121
6122 for (size_t i = 0; i < inf_nums.size (); i++)
6123 {
6124 if (is_mi)
6125 {
6126 char mi_group[10];
6127
6128 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
6129 uiout->field_string (NULL, mi_group);
6130 }
6131 else
6132 {
6133 if (i == 0)
6134 uiout->text (" inf ");
6135 else
6136 uiout->text (", ");
6137
6138 uiout->text (plongest (inf_nums[i]));
6139 }
6140 }
6141 }
6142
6143 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6144 instead of going via breakpoint_ops::print_one. This makes "maint
6145 info breakpoints" show the software breakpoint locations of
6146 catchpoints, which are considered internal implementation
6147 detail. */
6148
6149 static void
6150 print_one_breakpoint_location (struct breakpoint *b,
6151 struct bp_location *loc,
6152 int loc_number,
6153 struct bp_location **last_loc,
6154 int allflag, bool raw_loc)
6155 {
6156 struct command_line *l;
6157 static char bpenables[] = "nynny";
6158
6159 struct ui_out *uiout = current_uiout;
6160 int header_of_multiple = 0;
6161 int part_of_multiple = (loc != NULL);
6162 struct value_print_options opts;
6163
6164 get_user_print_options (&opts);
6165
6166 gdb_assert (!loc || loc_number != 0);
6167 /* See comment in print_one_breakpoint concerning treatment of
6168 breakpoints with single disabled location. */
6169 if (loc == NULL
6170 && (b->loc != NULL
6171 && (b->loc->next != NULL
6172 || !b->loc->enabled || b->loc->disabled_by_cond)))
6173 header_of_multiple = 1;
6174 if (loc == NULL)
6175 loc = b->loc;
6176
6177 annotate_record ();
6178
6179 /* 1 */
6180 annotate_field (0);
6181 if (part_of_multiple)
6182 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6183 else
6184 uiout->field_signed ("number", b->number);
6185
6186 /* 2 */
6187 annotate_field (1);
6188 if (part_of_multiple)
6189 uiout->field_skip ("type");
6190 else
6191 uiout->field_string ("type", bptype_string (b->type));
6192
6193 /* 3 */
6194 annotate_field (2);
6195 if (part_of_multiple)
6196 uiout->field_skip ("disp");
6197 else
6198 uiout->field_string ("disp", bpdisp_text (b->disposition));
6199
6200 /* 4 */
6201 annotate_field (3);
6202 /* For locations that are disabled because of an invalid condition,
6203 display "N*" on CLI, where "*" refers to a footnote below the
6204 table. For MI, simply display a "N" without a footnote. */
6205 const char *N = (uiout->is_mi_like_p ()) ? "N" : "N*";
6206 if (part_of_multiple)
6207 uiout->field_string ("enabled", (loc->disabled_by_cond ? N
6208 : (loc->enabled ? "y" : "n")));
6209 else
6210 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6211
6212 /* 5 and 6 */
6213 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
6214 b->ops->print_one (b, last_loc);
6215 else
6216 {
6217 if (is_watchpoint (b))
6218 {
6219 struct watchpoint *w = (struct watchpoint *) b;
6220
6221 /* Field 4, the address, is omitted (which makes the columns
6222 not line up too nicely with the headers, but the effect
6223 is relatively readable). */
6224 if (opts.addressprint)
6225 uiout->field_skip ("addr");
6226 annotate_field (5);
6227 uiout->field_string ("what", w->exp_string);
6228 }
6229 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6230 || is_ada_exception_catchpoint (b))
6231 {
6232 if (opts.addressprint)
6233 {
6234 annotate_field (4);
6235 if (header_of_multiple)
6236 uiout->field_string ("addr", "<MULTIPLE>",
6237 metadata_style.style ());
6238 else if (b->loc == NULL || loc->shlib_disabled)
6239 uiout->field_string ("addr", "<PENDING>",
6240 metadata_style.style ());
6241 else
6242 uiout->field_core_addr ("addr",
6243 loc->gdbarch, loc->address);
6244 }
6245 annotate_field (5);
6246 if (!header_of_multiple)
6247 print_breakpoint_location (b, loc);
6248 if (b->loc)
6249 *last_loc = b->loc;
6250 }
6251 }
6252
6253 if (loc != NULL && !header_of_multiple)
6254 {
6255 std::vector<int> inf_nums;
6256 int mi_only = 1;
6257
6258 for (inferior *inf : all_inferiors ())
6259 {
6260 if (inf->pspace == loc->pspace)
6261 inf_nums.push_back (inf->num);
6262 }
6263
6264 /* For backward compatibility, don't display inferiors in CLI unless
6265 there are several. Always display for MI. */
6266 if (allflag
6267 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6268 && (program_spaces.size () > 1
6269 || number_of_inferiors () > 1)
6270 /* LOC is for existing B, it cannot be in
6271 moribund_locations and thus having NULL OWNER. */
6272 && loc->owner->type != bp_catchpoint))
6273 mi_only = 0;
6274 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6275 }
6276
6277 if (!part_of_multiple)
6278 {
6279 if (b->thread != -1)
6280 {
6281 /* FIXME: This seems to be redundant and lost here; see the
6282 "stop only in" line a little further down. */
6283 uiout->text (" thread ");
6284 uiout->field_signed ("thread", b->thread);
6285 }
6286 else if (b->task != 0)
6287 {
6288 uiout->text (" task ");
6289 uiout->field_signed ("task", b->task);
6290 }
6291 }
6292
6293 uiout->text ("\n");
6294
6295 if (!part_of_multiple)
6296 b->ops->print_one_detail (b, uiout);
6297
6298 if (part_of_multiple && frame_id_p (b->frame_id))
6299 {
6300 annotate_field (6);
6301 uiout->text ("\tstop only in stack frame at ");
6302 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6303 the frame ID. */
6304 uiout->field_core_addr ("frame",
6305 b->gdbarch, b->frame_id.stack_addr);
6306 uiout->text ("\n");
6307 }
6308
6309 if (!part_of_multiple && b->cond_string)
6310 {
6311 annotate_field (7);
6312 if (is_tracepoint (b))
6313 uiout->text ("\ttrace only if ");
6314 else
6315 uiout->text ("\tstop only if ");
6316 uiout->field_string ("cond", b->cond_string);
6317
6318 /* Print whether the target is doing the breakpoint's condition
6319 evaluation. If GDB is doing the evaluation, don't print anything. */
6320 if (is_breakpoint (b)
6321 && breakpoint_condition_evaluation_mode ()
6322 == condition_evaluation_target)
6323 {
6324 uiout->message (" (%pF evals)",
6325 string_field ("evaluated-by",
6326 bp_condition_evaluator (b)));
6327 }
6328 uiout->text ("\n");
6329 }
6330
6331 if (!part_of_multiple && b->thread != -1)
6332 {
6333 /* FIXME should make an annotation for this. */
6334 uiout->text ("\tstop only in thread ");
6335 if (uiout->is_mi_like_p ())
6336 uiout->field_signed ("thread", b->thread);
6337 else
6338 {
6339 struct thread_info *thr = find_thread_global_id (b->thread);
6340
6341 uiout->field_string ("thread", print_thread_id (thr));
6342 }
6343 uiout->text ("\n");
6344 }
6345
6346 if (!part_of_multiple)
6347 {
6348 if (b->hit_count)
6349 {
6350 /* FIXME should make an annotation for this. */
6351 if (is_catchpoint (b))
6352 uiout->text ("\tcatchpoint");
6353 else if (is_tracepoint (b))
6354 uiout->text ("\ttracepoint");
6355 else
6356 uiout->text ("\tbreakpoint");
6357 uiout->text (" already hit ");
6358 uiout->field_signed ("times", b->hit_count);
6359 if (b->hit_count == 1)
6360 uiout->text (" time\n");
6361 else
6362 uiout->text (" times\n");
6363 }
6364 else
6365 {
6366 /* Output the count also if it is zero, but only if this is mi. */
6367 if (uiout->is_mi_like_p ())
6368 uiout->field_signed ("times", b->hit_count);
6369 }
6370 }
6371
6372 if (!part_of_multiple && b->ignore_count)
6373 {
6374 annotate_field (8);
6375 uiout->message ("\tignore next %pF hits\n",
6376 signed_field ("ignore", b->ignore_count));
6377 }
6378
6379 /* Note that an enable count of 1 corresponds to "enable once"
6380 behavior, which is reported by the combination of enablement and
6381 disposition, so we don't need to mention it here. */
6382 if (!part_of_multiple && b->enable_count > 1)
6383 {
6384 annotate_field (8);
6385 uiout->text ("\tdisable after ");
6386 /* Tweak the wording to clarify that ignore and enable counts
6387 are distinct, and have additive effect. */
6388 if (b->ignore_count)
6389 uiout->text ("additional ");
6390 else
6391 uiout->text ("next ");
6392 uiout->field_signed ("enable", b->enable_count);
6393 uiout->text (" hits\n");
6394 }
6395
6396 if (!part_of_multiple && is_tracepoint (b))
6397 {
6398 struct tracepoint *tp = (struct tracepoint *) b;
6399
6400 if (tp->traceframe_usage)
6401 {
6402 uiout->text ("\ttrace buffer usage ");
6403 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6404 uiout->text (" bytes\n");
6405 }
6406 }
6407
6408 l = b->commands ? b->commands.get () : NULL;
6409 if (!part_of_multiple && l)
6410 {
6411 annotate_field (9);
6412 ui_out_emit_tuple tuple_emitter (uiout, "script");
6413 print_command_lines (uiout, l, 4);
6414 }
6415
6416 if (is_tracepoint (b))
6417 {
6418 struct tracepoint *t = (struct tracepoint *) b;
6419
6420 if (!part_of_multiple && t->pass_count)
6421 {
6422 annotate_field (10);
6423 uiout->text ("\tpass count ");
6424 uiout->field_signed ("pass", t->pass_count);
6425 uiout->text (" \n");
6426 }
6427
6428 /* Don't display it when tracepoint or tracepoint location is
6429 pending. */
6430 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6431 {
6432 annotate_field (11);
6433
6434 if (uiout->is_mi_like_p ())
6435 uiout->field_string ("installed",
6436 loc->inserted ? "y" : "n");
6437 else
6438 {
6439 if (loc->inserted)
6440 uiout->text ("\t");
6441 else
6442 uiout->text ("\tnot ");
6443 uiout->text ("installed on target\n");
6444 }
6445 }
6446 }
6447
6448 if (uiout->is_mi_like_p () && !part_of_multiple)
6449 {
6450 if (is_watchpoint (b))
6451 {
6452 struct watchpoint *w = (struct watchpoint *) b;
6453
6454 uiout->field_string ("original-location", w->exp_string);
6455 }
6456 else if (b->location != NULL
6457 && event_location_to_string (b->location.get ()) != NULL)
6458 uiout->field_string ("original-location",
6459 event_location_to_string (b->location.get ()));
6460 }
6461 }
6462
6463 /* See breakpoint.h. */
6464
6465 bool fix_multi_location_breakpoint_output_globally = false;
6466
6467 static void
6468 print_one_breakpoint (struct breakpoint *b,
6469 struct bp_location **last_loc,
6470 int allflag)
6471 {
6472 struct ui_out *uiout = current_uiout;
6473 bool use_fixed_output
6474 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6475 || fix_multi_location_breakpoint_output_globally);
6476
6477 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6478 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
6479
6480 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6481 are outside. */
6482 if (!use_fixed_output)
6483 bkpt_tuple_emitter.reset ();
6484
6485 /* If this breakpoint has custom print function,
6486 it's already printed. Otherwise, print individual
6487 locations, if any. */
6488 if (b->ops == NULL
6489 || b->ops->print_one == NULL
6490 || allflag)
6491 {
6492 /* If breakpoint has a single location that is disabled, we
6493 print it as if it had several locations, since otherwise it's
6494 hard to represent "breakpoint enabled, location disabled"
6495 situation.
6496
6497 Note that while hardware watchpoints have several locations
6498 internally, that's not a property exposed to users.
6499
6500 Likewise, while catchpoints may be implemented with
6501 breakpoints (e.g., catch throw), that's not a property
6502 exposed to users. We do however display the internal
6503 breakpoint locations with "maint info breakpoints". */
6504 if (!is_hardware_watchpoint (b)
6505 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6506 || is_ada_exception_catchpoint (b))
6507 && (allflag
6508 || (b->loc && (b->loc->next
6509 || !b->loc->enabled
6510 || b->loc->disabled_by_cond))))
6511 {
6512 gdb::optional<ui_out_emit_list> locations_list;
6513
6514 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6515 MI record. For later versions, place breakpoint locations in a
6516 list. */
6517 if (uiout->is_mi_like_p () && use_fixed_output)
6518 locations_list.emplace (uiout, "locations");
6519
6520 int n = 1;
6521 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next, ++n)
6522 {
6523 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6524 print_one_breakpoint_location (b, loc, n, last_loc,
6525 allflag, allflag);
6526 }
6527 }
6528 }
6529 }
6530
6531 static int
6532 breakpoint_address_bits (struct breakpoint *b)
6533 {
6534 int print_address_bits = 0;
6535 struct bp_location *loc;
6536
6537 /* Software watchpoints that aren't watching memory don't have an
6538 address to print. */
6539 if (is_no_memory_software_watchpoint (b))
6540 return 0;
6541
6542 for (loc = b->loc; loc; loc = loc->next)
6543 {
6544 int addr_bit;
6545
6546 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6547 if (addr_bit > print_address_bits)
6548 print_address_bits = addr_bit;
6549 }
6550
6551 return print_address_bits;
6552 }
6553
6554 /* See breakpoint.h. */
6555
6556 void
6557 print_breakpoint (breakpoint *b)
6558 {
6559 struct bp_location *dummy_loc = NULL;
6560 print_one_breakpoint (b, &dummy_loc, 0);
6561 }
6562
6563 /* Return true if this breakpoint was set by the user, false if it is
6564 internal or momentary. */
6565
6566 int
6567 user_breakpoint_p (struct breakpoint *b)
6568 {
6569 return b->number > 0;
6570 }
6571
6572 /* See breakpoint.h. */
6573
6574 int
6575 pending_breakpoint_p (struct breakpoint *b)
6576 {
6577 return b->loc == NULL;
6578 }
6579
6580 /* Print information on breakpoints (including watchpoints and tracepoints).
6581
6582 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6583 understood by number_or_range_parser. Only breakpoints included in this
6584 list are then printed.
6585
6586 If SHOW_INTERNAL is true, print internal breakpoints.
6587
6588 If FILTER is non-NULL, call it on each breakpoint and only include the
6589 ones for which it returns true.
6590
6591 Return the total number of breakpoints listed. */
6592
6593 static int
6594 breakpoint_1 (const char *bp_num_list, bool show_internal,
6595 bool (*filter) (const struct breakpoint *))
6596 {
6597 struct breakpoint *b;
6598 struct bp_location *last_loc = NULL;
6599 int nr_printable_breakpoints;
6600 struct value_print_options opts;
6601 int print_address_bits = 0;
6602 int print_type_col_width = 14;
6603 struct ui_out *uiout = current_uiout;
6604 bool has_disabled_by_cond_location = false;
6605
6606 get_user_print_options (&opts);
6607
6608 /* Compute the number of rows in the table, as well as the size
6609 required for address fields. */
6610 nr_printable_breakpoints = 0;
6611 ALL_BREAKPOINTS (b)
6612 {
6613 /* If we have a filter, only list the breakpoints it accepts. */
6614 if (filter && !filter (b))
6615 continue;
6616
6617 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6618 accept. Skip the others. */
6619 if (bp_num_list != NULL && *bp_num_list != '\0')
6620 {
6621 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6622 continue;
6623 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6624 continue;
6625 }
6626
6627 if (show_internal || user_breakpoint_p (b))
6628 {
6629 int addr_bit, type_len;
6630
6631 addr_bit = breakpoint_address_bits (b);
6632 if (addr_bit > print_address_bits)
6633 print_address_bits = addr_bit;
6634
6635 type_len = strlen (bptype_string (b->type));
6636 if (type_len > print_type_col_width)
6637 print_type_col_width = type_len;
6638
6639 nr_printable_breakpoints++;
6640 }
6641 }
6642
6643 {
6644 ui_out_emit_table table_emitter (uiout,
6645 opts.addressprint ? 6 : 5,
6646 nr_printable_breakpoints,
6647 "BreakpointTable");
6648
6649 if (nr_printable_breakpoints > 0)
6650 annotate_breakpoints_headers ();
6651 if (nr_printable_breakpoints > 0)
6652 annotate_field (0);
6653 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6654 if (nr_printable_breakpoints > 0)
6655 annotate_field (1);
6656 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6657 if (nr_printable_breakpoints > 0)
6658 annotate_field (2);
6659 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6660 if (nr_printable_breakpoints > 0)
6661 annotate_field (3);
6662 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6663 if (opts.addressprint)
6664 {
6665 if (nr_printable_breakpoints > 0)
6666 annotate_field (4);
6667 if (print_address_bits <= 32)
6668 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6669 else
6670 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6671 }
6672 if (nr_printable_breakpoints > 0)
6673 annotate_field (5);
6674 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6675 uiout->table_body ();
6676 if (nr_printable_breakpoints > 0)
6677 annotate_breakpoints_table ();
6678
6679 ALL_BREAKPOINTS (b)
6680 {
6681 QUIT;
6682 /* If we have a filter, only list the breakpoints it accepts. */
6683 if (filter && !filter (b))
6684 continue;
6685
6686 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6687 accept. Skip the others. */
6688
6689 if (bp_num_list != NULL && *bp_num_list != '\0')
6690 {
6691 if (show_internal) /* maintenance info breakpoint */
6692 {
6693 if (parse_and_eval_long (bp_num_list) != b->number)
6694 continue;
6695 }
6696 else /* all others */
6697 {
6698 if (!number_is_in_list (bp_num_list, b->number))
6699 continue;
6700 }
6701 }
6702 /* We only print out user settable breakpoints unless the
6703 show_internal is set. */
6704 if (show_internal || user_breakpoint_p (b))
6705 {
6706 print_one_breakpoint (b, &last_loc, show_internal);
6707 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
6708 if (loc->disabled_by_cond)
6709 has_disabled_by_cond_location = true;
6710 }
6711 }
6712 }
6713
6714 if (nr_printable_breakpoints == 0)
6715 {
6716 /* If there's a filter, let the caller decide how to report
6717 empty list. */
6718 if (!filter)
6719 {
6720 if (bp_num_list == NULL || *bp_num_list == '\0')
6721 uiout->message ("No breakpoints or watchpoints.\n");
6722 else
6723 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6724 bp_num_list);
6725 }
6726 }
6727 else
6728 {
6729 if (last_loc && !server_command)
6730 set_next_address (last_loc->gdbarch, last_loc->address);
6731
6732 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
6733 uiout->message (_("(*): Breakpoint condition is invalid at this "
6734 "location.\n"));
6735 }
6736
6737 /* FIXME? Should this be moved up so that it is only called when
6738 there have been breakpoints? */
6739 annotate_breakpoints_table_end ();
6740
6741 return nr_printable_breakpoints;
6742 }
6743
6744 /* Display the value of default-collect in a way that is generally
6745 compatible with the breakpoint list. */
6746
6747 static void
6748 default_collect_info (void)
6749 {
6750 struct ui_out *uiout = current_uiout;
6751
6752 /* If it has no value (which is frequently the case), say nothing; a
6753 message like "No default-collect." gets in user's face when it's
6754 not wanted. */
6755 if (!*default_collect)
6756 return;
6757
6758 /* The following phrase lines up nicely with per-tracepoint collect
6759 actions. */
6760 uiout->text ("default collect ");
6761 uiout->field_string ("default-collect", default_collect);
6762 uiout->text (" \n");
6763 }
6764
6765 static void
6766 info_breakpoints_command (const char *args, int from_tty)
6767 {
6768 breakpoint_1 (args, false, NULL);
6769
6770 default_collect_info ();
6771 }
6772
6773 static void
6774 info_watchpoints_command (const char *args, int from_tty)
6775 {
6776 int num_printed = breakpoint_1 (args, false, is_watchpoint);
6777 struct ui_out *uiout = current_uiout;
6778
6779 if (num_printed == 0)
6780 {
6781 if (args == NULL || *args == '\0')
6782 uiout->message ("No watchpoints.\n");
6783 else
6784 uiout->message ("No watchpoint matching '%s'.\n", args);
6785 }
6786 }
6787
6788 static void
6789 maintenance_info_breakpoints (const char *args, int from_tty)
6790 {
6791 breakpoint_1 (args, true, NULL);
6792
6793 default_collect_info ();
6794 }
6795
6796 static int
6797 breakpoint_has_pc (struct breakpoint *b,
6798 struct program_space *pspace,
6799 CORE_ADDR pc, struct obj_section *section)
6800 {
6801 struct bp_location *bl = b->loc;
6802
6803 for (; bl; bl = bl->next)
6804 {
6805 if (bl->pspace == pspace
6806 && bl->address == pc
6807 && (!overlay_debugging || bl->section == section))
6808 return 1;
6809 }
6810 return 0;
6811 }
6812
6813 /* Print a message describing any user-breakpoints set at PC. This
6814 concerns with logical breakpoints, so we match program spaces, not
6815 address spaces. */
6816
6817 static void
6818 describe_other_breakpoints (struct gdbarch *gdbarch,
6819 struct program_space *pspace, CORE_ADDR pc,
6820 struct obj_section *section, int thread)
6821 {
6822 int others = 0;
6823 struct breakpoint *b;
6824
6825 ALL_BREAKPOINTS (b)
6826 others += (user_breakpoint_p (b)
6827 && breakpoint_has_pc (b, pspace, pc, section));
6828 if (others > 0)
6829 {
6830 if (others == 1)
6831 printf_filtered (_("Note: breakpoint "));
6832 else /* if (others == ???) */
6833 printf_filtered (_("Note: breakpoints "));
6834 ALL_BREAKPOINTS (b)
6835 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6836 {
6837 others--;
6838 printf_filtered ("%d", b->number);
6839 if (b->thread == -1 && thread != -1)
6840 printf_filtered (" (all threads)");
6841 else if (b->thread != -1)
6842 printf_filtered (" (thread %d)", b->thread);
6843 printf_filtered ("%s%s ",
6844 ((b->enable_state == bp_disabled
6845 || b->enable_state == bp_call_disabled)
6846 ? " (disabled)"
6847 : ""),
6848 (others > 1) ? ","
6849 : ((others == 1) ? " and" : ""));
6850 }
6851 current_uiout->message (_("also set at pc %ps.\n"),
6852 styled_string (address_style.style (),
6853 paddress (gdbarch, pc)));
6854 }
6855 }
6856 \f
6857
6858 /* Return true iff it is meaningful to use the address member of LOC.
6859 For some breakpoint types, the locations' address members are
6860 irrelevant and it makes no sense to attempt to compare them to
6861 other addresses (or use them for any other purpose either).
6862
6863 More specifically, software watchpoints and catchpoints that are
6864 not backed by breakpoints always have a zero valued location
6865 address and we don't want to mark breakpoints of any of these types
6866 to be a duplicate of an actual breakpoint location at address
6867 zero. */
6868
6869 static bool
6870 bl_address_is_meaningful (bp_location *loc)
6871 {
6872 return loc->loc_type != bp_loc_other;
6873 }
6874
6875 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6876 true if LOC1 and LOC2 represent the same watchpoint location. */
6877
6878 static int
6879 watchpoint_locations_match (struct bp_location *loc1,
6880 struct bp_location *loc2)
6881 {
6882 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6883 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6884
6885 /* Both of them must exist. */
6886 gdb_assert (w1 != NULL);
6887 gdb_assert (w2 != NULL);
6888
6889 /* If the target can evaluate the condition expression in hardware,
6890 then we we need to insert both watchpoints even if they are at
6891 the same place. Otherwise the watchpoint will only trigger when
6892 the condition of whichever watchpoint was inserted evaluates to
6893 true, not giving a chance for GDB to check the condition of the
6894 other watchpoint. */
6895 if ((w1->cond_exp
6896 && target_can_accel_watchpoint_condition (loc1->address,
6897 loc1->length,
6898 loc1->watchpoint_type,
6899 w1->cond_exp.get ()))
6900 || (w2->cond_exp
6901 && target_can_accel_watchpoint_condition (loc2->address,
6902 loc2->length,
6903 loc2->watchpoint_type,
6904 w2->cond_exp.get ())))
6905 return 0;
6906
6907 /* Note that this checks the owner's type, not the location's. In
6908 case the target does not support read watchpoints, but does
6909 support access watchpoints, we'll have bp_read_watchpoint
6910 watchpoints with hw_access locations. Those should be considered
6911 duplicates of hw_read locations. The hw_read locations will
6912 become hw_access locations later. */
6913 return (loc1->owner->type == loc2->owner->type
6914 && loc1->pspace->aspace == loc2->pspace->aspace
6915 && loc1->address == loc2->address
6916 && loc1->length == loc2->length);
6917 }
6918
6919 /* See breakpoint.h. */
6920
6921 int
6922 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6923 const address_space *aspace2, CORE_ADDR addr2)
6924 {
6925 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6926 || aspace1 == aspace2)
6927 && addr1 == addr2);
6928 }
6929
6930 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6931 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6932 matches ASPACE2. On targets that have global breakpoints, the address
6933 space doesn't really matter. */
6934
6935 static int
6936 breakpoint_address_match_range (const address_space *aspace1,
6937 CORE_ADDR addr1,
6938 int len1, const address_space *aspace2,
6939 CORE_ADDR addr2)
6940 {
6941 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6942 || aspace1 == aspace2)
6943 && addr2 >= addr1 && addr2 < addr1 + len1);
6944 }
6945
6946 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6947 a ranged breakpoint. In most targets, a match happens only if ASPACE
6948 matches the breakpoint's address space. On targets that have global
6949 breakpoints, the address space doesn't really matter. */
6950
6951 static int
6952 breakpoint_location_address_match (struct bp_location *bl,
6953 const address_space *aspace,
6954 CORE_ADDR addr)
6955 {
6956 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6957 aspace, addr)
6958 || (bl->length
6959 && breakpoint_address_match_range (bl->pspace->aspace,
6960 bl->address, bl->length,
6961 aspace, addr)));
6962 }
6963
6964 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6965 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6966 match happens only if ASPACE matches the breakpoint's address
6967 space. On targets that have global breakpoints, the address space
6968 doesn't really matter. */
6969
6970 static int
6971 breakpoint_location_address_range_overlap (struct bp_location *bl,
6972 const address_space *aspace,
6973 CORE_ADDR addr, int len)
6974 {
6975 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6976 || bl->pspace->aspace == aspace)
6977 {
6978 int bl_len = bl->length != 0 ? bl->length : 1;
6979
6980 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6981 return 1;
6982 }
6983 return 0;
6984 }
6985
6986 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6987 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6988 true, otherwise returns false. */
6989
6990 static int
6991 tracepoint_locations_match (struct bp_location *loc1,
6992 struct bp_location *loc2)
6993 {
6994 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6995 /* Since tracepoint locations are never duplicated with others', tracepoint
6996 locations at the same address of different tracepoints are regarded as
6997 different locations. */
6998 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6999 else
7000 return 0;
7001 }
7002
7003 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7004 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
7005 the same location. If SW_HW_BPS_MATCH is true, then software
7006 breakpoint locations and hardware breakpoint locations match,
7007 otherwise they don't. */
7008
7009 static int
7010 breakpoint_locations_match (struct bp_location *loc1,
7011 struct bp_location *loc2,
7012 bool sw_hw_bps_match)
7013 {
7014 int hw_point1, hw_point2;
7015
7016 /* Both of them must not be in moribund_locations. */
7017 gdb_assert (loc1->owner != NULL);
7018 gdb_assert (loc2->owner != NULL);
7019
7020 hw_point1 = is_hardware_watchpoint (loc1->owner);
7021 hw_point2 = is_hardware_watchpoint (loc2->owner);
7022
7023 if (hw_point1 != hw_point2)
7024 return 0;
7025 else if (hw_point1)
7026 return watchpoint_locations_match (loc1, loc2);
7027 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7028 return tracepoint_locations_match (loc1, loc2);
7029 else
7030 /* We compare bp_location.length in order to cover ranged
7031 breakpoints. Keep this in sync with
7032 bp_location_is_less_than. */
7033 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7034 loc2->pspace->aspace, loc2->address)
7035 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
7036 && loc1->length == loc2->length);
7037 }
7038
7039 static void
7040 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7041 int bnum, int have_bnum)
7042 {
7043 /* The longest string possibly returned by hex_string_custom
7044 is 50 chars. These must be at least that big for safety. */
7045 char astr1[64];
7046 char astr2[64];
7047
7048 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7049 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7050 if (have_bnum)
7051 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7052 bnum, astr1, astr2);
7053 else
7054 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7055 }
7056
7057 /* Adjust a breakpoint's address to account for architectural
7058 constraints on breakpoint placement. Return the adjusted address.
7059 Note: Very few targets require this kind of adjustment. For most
7060 targets, this function is simply the identity function. */
7061
7062 static CORE_ADDR
7063 adjust_breakpoint_address (struct gdbarch *gdbarch,
7064 CORE_ADDR bpaddr, enum bptype bptype)
7065 {
7066 if (bptype == bp_watchpoint
7067 || bptype == bp_hardware_watchpoint
7068 || bptype == bp_read_watchpoint
7069 || bptype == bp_access_watchpoint
7070 || bptype == bp_catchpoint)
7071 {
7072 /* Watchpoints and the various bp_catch_* eventpoints should not
7073 have their addresses modified. */
7074 return bpaddr;
7075 }
7076 else if (bptype == bp_single_step)
7077 {
7078 /* Single-step breakpoints should not have their addresses
7079 modified. If there's any architectural constrain that
7080 applies to this address, then it should have already been
7081 taken into account when the breakpoint was created in the
7082 first place. If we didn't do this, stepping through e.g.,
7083 Thumb-2 IT blocks would break. */
7084 return bpaddr;
7085 }
7086 else
7087 {
7088 CORE_ADDR adjusted_bpaddr = bpaddr;
7089
7090 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7091 {
7092 /* Some targets have architectural constraints on the placement
7093 of breakpoint instructions. Obtain the adjusted address. */
7094 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7095 }
7096
7097 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
7098
7099 /* An adjusted breakpoint address can significantly alter
7100 a user's expectations. Print a warning if an adjustment
7101 is required. */
7102 if (adjusted_bpaddr != bpaddr)
7103 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7104
7105 return adjusted_bpaddr;
7106 }
7107 }
7108
7109 static bp_loc_type
7110 bp_location_from_bp_type (bptype type)
7111 {
7112 switch (type)
7113 {
7114 case bp_breakpoint:
7115 case bp_single_step:
7116 case bp_until:
7117 case bp_finish:
7118 case bp_longjmp:
7119 case bp_longjmp_resume:
7120 case bp_longjmp_call_dummy:
7121 case bp_exception:
7122 case bp_exception_resume:
7123 case bp_step_resume:
7124 case bp_hp_step_resume:
7125 case bp_watchpoint_scope:
7126 case bp_call_dummy:
7127 case bp_std_terminate:
7128 case bp_shlib_event:
7129 case bp_thread_event:
7130 case bp_overlay_event:
7131 case bp_jit_event:
7132 case bp_longjmp_master:
7133 case bp_std_terminate_master:
7134 case bp_exception_master:
7135 case bp_gnu_ifunc_resolver:
7136 case bp_gnu_ifunc_resolver_return:
7137 case bp_dprintf:
7138 return bp_loc_software_breakpoint;
7139 case bp_hardware_breakpoint:
7140 return bp_loc_hardware_breakpoint;
7141 case bp_hardware_watchpoint:
7142 case bp_read_watchpoint:
7143 case bp_access_watchpoint:
7144 return bp_loc_hardware_watchpoint;
7145 case bp_watchpoint:
7146 case bp_catchpoint:
7147 case bp_tracepoint:
7148 case bp_fast_tracepoint:
7149 case bp_static_tracepoint:
7150 return bp_loc_other;
7151 default:
7152 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7153 }
7154 }
7155
7156 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7157 {
7158 this->owner = owner;
7159 this->cond_bytecode = NULL;
7160 this->shlib_disabled = 0;
7161 this->enabled = 1;
7162 this->disabled_by_cond = false;
7163
7164 this->loc_type = type;
7165
7166 if (this->loc_type == bp_loc_software_breakpoint
7167 || this->loc_type == bp_loc_hardware_breakpoint)
7168 mark_breakpoint_location_modified (this);
7169
7170 incref ();
7171 }
7172
7173 bp_location::bp_location (breakpoint *owner)
7174 : bp_location::bp_location (owner,
7175 bp_location_from_bp_type (owner->type))
7176 {
7177 }
7178
7179 /* Allocate a struct bp_location. */
7180
7181 static struct bp_location *
7182 allocate_bp_location (struct breakpoint *bpt)
7183 {
7184 return bpt->ops->allocate_location (bpt);
7185 }
7186
7187 /* Decrement reference count. If the reference count reaches 0,
7188 destroy the bp_location. Sets *BLP to NULL. */
7189
7190 static void
7191 decref_bp_location (struct bp_location **blp)
7192 {
7193 bp_location_ref_policy::decref (*blp);
7194 *blp = NULL;
7195 }
7196
7197 /* Add breakpoint B at the end of the global breakpoint chain. */
7198
7199 static breakpoint *
7200 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7201 {
7202 struct breakpoint *b1;
7203 struct breakpoint *result = b.get ();
7204
7205 /* Add this breakpoint to the end of the chain so that a list of
7206 breakpoints will come out in order of increasing numbers. */
7207
7208 b1 = breakpoint_chain;
7209 if (b1 == 0)
7210 breakpoint_chain = b.release ();
7211 else
7212 {
7213 while (b1->next)
7214 b1 = b1->next;
7215 b1->next = b.release ();
7216 }
7217
7218 return result;
7219 }
7220
7221 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7222
7223 static void
7224 init_raw_breakpoint_without_location (struct breakpoint *b,
7225 struct gdbarch *gdbarch,
7226 enum bptype bptype,
7227 const struct breakpoint_ops *ops)
7228 {
7229 gdb_assert (ops != NULL);
7230
7231 b->ops = ops;
7232 b->type = bptype;
7233 b->gdbarch = gdbarch;
7234 b->language = current_language->la_language;
7235 b->input_radix = input_radix;
7236 b->related_breakpoint = b;
7237 }
7238
7239 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7240 that has type BPTYPE and has no locations as yet. */
7241
7242 static struct breakpoint *
7243 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7244 enum bptype bptype,
7245 const struct breakpoint_ops *ops)
7246 {
7247 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7248
7249 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7250 return add_to_breakpoint_chain (std::move (b));
7251 }
7252
7253 /* Initialize loc->function_name. */
7254
7255 static void
7256 set_breakpoint_location_function (struct bp_location *loc)
7257 {
7258 gdb_assert (loc->owner != NULL);
7259
7260 if (loc->owner->type == bp_breakpoint
7261 || loc->owner->type == bp_hardware_breakpoint
7262 || is_tracepoint (loc->owner))
7263 {
7264 const char *function_name;
7265
7266 if (loc->msymbol != NULL
7267 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
7268 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
7269 {
7270 struct breakpoint *b = loc->owner;
7271
7272 function_name = loc->msymbol->linkage_name ();
7273
7274 if (b->type == bp_breakpoint && b->loc == loc
7275 && loc->next == NULL && b->related_breakpoint == b)
7276 {
7277 /* Create only the whole new breakpoint of this type but do not
7278 mess more complicated breakpoints with multiple locations. */
7279 b->type = bp_gnu_ifunc_resolver;
7280 /* Remember the resolver's address for use by the return
7281 breakpoint. */
7282 loc->related_address = loc->address;
7283 }
7284 }
7285 else
7286 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7287
7288 if (function_name)
7289 loc->function_name = xstrdup (function_name);
7290 }
7291 }
7292
7293 /* Attempt to determine architecture of location identified by SAL. */
7294 struct gdbarch *
7295 get_sal_arch (struct symtab_and_line sal)
7296 {
7297 if (sal.section)
7298 return sal.section->objfile->arch ();
7299 if (sal.symtab)
7300 return SYMTAB_OBJFILE (sal.symtab)->arch ();
7301
7302 return NULL;
7303 }
7304
7305 /* Low level routine for partially initializing a breakpoint of type
7306 BPTYPE. The newly created breakpoint's address, section, source
7307 file name, and line number are provided by SAL.
7308
7309 It is expected that the caller will complete the initialization of
7310 the newly created breakpoint struct as well as output any status
7311 information regarding the creation of a new breakpoint. */
7312
7313 static void
7314 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7315 struct symtab_and_line sal, enum bptype bptype,
7316 const struct breakpoint_ops *ops)
7317 {
7318 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7319
7320 add_location_to_breakpoint (b, &sal);
7321
7322 if (bptype != bp_catchpoint)
7323 gdb_assert (sal.pspace != NULL);
7324
7325 /* Store the program space that was used to set the breakpoint,
7326 except for ordinary breakpoints, which are independent of the
7327 program space. */
7328 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7329 b->pspace = sal.pspace;
7330 }
7331
7332 /* set_raw_breakpoint is a low level routine for allocating and
7333 partially initializing a breakpoint of type BPTYPE. The newly
7334 created breakpoint's address, section, source file name, and line
7335 number are provided by SAL. The newly created and partially
7336 initialized breakpoint is added to the breakpoint chain and
7337 is also returned as the value of this function.
7338
7339 It is expected that the caller will complete the initialization of
7340 the newly created breakpoint struct as well as output any status
7341 information regarding the creation of a new breakpoint. In
7342 particular, set_raw_breakpoint does NOT set the breakpoint
7343 number! Care should be taken to not allow an error to occur
7344 prior to completing the initialization of the breakpoint. If this
7345 should happen, a bogus breakpoint will be left on the chain. */
7346
7347 struct breakpoint *
7348 set_raw_breakpoint (struct gdbarch *gdbarch,
7349 struct symtab_and_line sal, enum bptype bptype,
7350 const struct breakpoint_ops *ops)
7351 {
7352 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7353
7354 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7355 return add_to_breakpoint_chain (std::move (b));
7356 }
7357
7358 /* Call this routine when stepping and nexting to enable a breakpoint
7359 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7360 initiated the operation. */
7361
7362 void
7363 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7364 {
7365 struct breakpoint *b, *b_tmp;
7366 int thread = tp->global_num;
7367
7368 /* To avoid having to rescan all objfile symbols at every step,
7369 we maintain a list of continually-inserted but always disabled
7370 longjmp "master" breakpoints. Here, we simply create momentary
7371 clones of those and enable them for the requested thread. */
7372 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7373 if (b->pspace == current_program_space
7374 && (b->type == bp_longjmp_master
7375 || b->type == bp_exception_master))
7376 {
7377 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7378 struct breakpoint *clone;
7379
7380 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7381 after their removal. */
7382 clone = momentary_breakpoint_from_master (b, type,
7383 &momentary_breakpoint_ops, 1);
7384 clone->thread = thread;
7385 }
7386
7387 tp->initiating_frame = frame;
7388 }
7389
7390 /* Delete all longjmp breakpoints from THREAD. */
7391 void
7392 delete_longjmp_breakpoint (int thread)
7393 {
7394 struct breakpoint *b, *b_tmp;
7395
7396 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7397 if (b->type == bp_longjmp || b->type == bp_exception)
7398 {
7399 if (b->thread == thread)
7400 delete_breakpoint (b);
7401 }
7402 }
7403
7404 void
7405 delete_longjmp_breakpoint_at_next_stop (int thread)
7406 {
7407 struct breakpoint *b, *b_tmp;
7408
7409 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7410 if (b->type == bp_longjmp || b->type == bp_exception)
7411 {
7412 if (b->thread == thread)
7413 b->disposition = disp_del_at_next_stop;
7414 }
7415 }
7416
7417 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7418 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7419 pointer to any of them. Return NULL if this system cannot place longjmp
7420 breakpoints. */
7421
7422 struct breakpoint *
7423 set_longjmp_breakpoint_for_call_dummy (void)
7424 {
7425 struct breakpoint *b, *retval = NULL;
7426
7427 ALL_BREAKPOINTS (b)
7428 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7429 {
7430 struct breakpoint *new_b;
7431
7432 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7433 &momentary_breakpoint_ops,
7434 1);
7435 new_b->thread = inferior_thread ()->global_num;
7436
7437 /* Link NEW_B into the chain of RETVAL breakpoints. */
7438
7439 gdb_assert (new_b->related_breakpoint == new_b);
7440 if (retval == NULL)
7441 retval = new_b;
7442 new_b->related_breakpoint = retval;
7443 while (retval->related_breakpoint != new_b->related_breakpoint)
7444 retval = retval->related_breakpoint;
7445 retval->related_breakpoint = new_b;
7446 }
7447
7448 return retval;
7449 }
7450
7451 /* Verify all existing dummy frames and their associated breakpoints for
7452 TP. Remove those which can no longer be found in the current frame
7453 stack.
7454
7455 You should call this function only at places where it is safe to currently
7456 unwind the whole stack. Failed stack unwind would discard live dummy
7457 frames. */
7458
7459 void
7460 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7461 {
7462 struct breakpoint *b, *b_tmp;
7463
7464 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7465 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7466 {
7467 struct breakpoint *dummy_b = b->related_breakpoint;
7468
7469 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7470 dummy_b = dummy_b->related_breakpoint;
7471 if (dummy_b->type != bp_call_dummy
7472 || frame_find_by_id (dummy_b->frame_id) != NULL)
7473 continue;
7474
7475 dummy_frame_discard (dummy_b->frame_id, tp);
7476
7477 while (b->related_breakpoint != b)
7478 {
7479 if (b_tmp == b->related_breakpoint)
7480 b_tmp = b->related_breakpoint->next;
7481 delete_breakpoint (b->related_breakpoint);
7482 }
7483 delete_breakpoint (b);
7484 }
7485 }
7486
7487 void
7488 enable_overlay_breakpoints (void)
7489 {
7490 struct breakpoint *b;
7491
7492 ALL_BREAKPOINTS (b)
7493 if (b->type == bp_overlay_event)
7494 {
7495 b->enable_state = bp_enabled;
7496 update_global_location_list (UGLL_MAY_INSERT);
7497 overlay_events_enabled = 1;
7498 }
7499 }
7500
7501 void
7502 disable_overlay_breakpoints (void)
7503 {
7504 struct breakpoint *b;
7505
7506 ALL_BREAKPOINTS (b)
7507 if (b->type == bp_overlay_event)
7508 {
7509 b->enable_state = bp_disabled;
7510 update_global_location_list (UGLL_DONT_INSERT);
7511 overlay_events_enabled = 0;
7512 }
7513 }
7514
7515 /* Set an active std::terminate breakpoint for each std::terminate
7516 master breakpoint. */
7517 void
7518 set_std_terminate_breakpoint (void)
7519 {
7520 struct breakpoint *b, *b_tmp;
7521
7522 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7523 if (b->pspace == current_program_space
7524 && b->type == bp_std_terminate_master)
7525 {
7526 momentary_breakpoint_from_master (b, bp_std_terminate,
7527 &momentary_breakpoint_ops, 1);
7528 }
7529 }
7530
7531 /* Delete all the std::terminate breakpoints. */
7532 void
7533 delete_std_terminate_breakpoint (void)
7534 {
7535 struct breakpoint *b, *b_tmp;
7536
7537 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7538 if (b->type == bp_std_terminate)
7539 delete_breakpoint (b);
7540 }
7541
7542 struct breakpoint *
7543 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7544 {
7545 struct breakpoint *b;
7546
7547 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7548 &internal_breakpoint_ops);
7549
7550 b->enable_state = bp_enabled;
7551 /* location has to be used or breakpoint_re_set will delete me. */
7552 b->location = new_address_location (b->loc->address, NULL, 0);
7553
7554 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7555
7556 return b;
7557 }
7558
7559 struct lang_and_radix
7560 {
7561 enum language lang;
7562 int radix;
7563 };
7564
7565 /* Create a breakpoint for JIT code registration and unregistration. */
7566
7567 struct breakpoint *
7568 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7569 {
7570 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7571 &internal_breakpoint_ops);
7572 }
7573
7574 /* Remove JIT code registration and unregistration breakpoint(s). */
7575
7576 void
7577 remove_jit_event_breakpoints (void)
7578 {
7579 struct breakpoint *b, *b_tmp;
7580
7581 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7582 if (b->type == bp_jit_event
7583 && b->loc->pspace == current_program_space)
7584 delete_breakpoint (b);
7585 }
7586
7587 void
7588 remove_solib_event_breakpoints (void)
7589 {
7590 struct breakpoint *b, *b_tmp;
7591
7592 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7593 if (b->type == bp_shlib_event
7594 && b->loc->pspace == current_program_space)
7595 delete_breakpoint (b);
7596 }
7597
7598 /* See breakpoint.h. */
7599
7600 void
7601 remove_solib_event_breakpoints_at_next_stop (void)
7602 {
7603 struct breakpoint *b, *b_tmp;
7604
7605 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7606 if (b->type == bp_shlib_event
7607 && b->loc->pspace == current_program_space)
7608 b->disposition = disp_del_at_next_stop;
7609 }
7610
7611 /* Helper for create_solib_event_breakpoint /
7612 create_and_insert_solib_event_breakpoint. Allows specifying which
7613 INSERT_MODE to pass through to update_global_location_list. */
7614
7615 static struct breakpoint *
7616 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7617 enum ugll_insert_mode insert_mode)
7618 {
7619 struct breakpoint *b;
7620
7621 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7622 &internal_breakpoint_ops);
7623 update_global_location_list_nothrow (insert_mode);
7624 return b;
7625 }
7626
7627 struct breakpoint *
7628 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7629 {
7630 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7631 }
7632
7633 /* See breakpoint.h. */
7634
7635 struct breakpoint *
7636 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7637 {
7638 struct breakpoint *b;
7639
7640 /* Explicitly tell update_global_location_list to insert
7641 locations. */
7642 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7643 if (!b->loc->inserted)
7644 {
7645 delete_breakpoint (b);
7646 return NULL;
7647 }
7648 return b;
7649 }
7650
7651 /* Disable any breakpoints that are on code in shared libraries. Only
7652 apply to enabled breakpoints, disabled ones can just stay disabled. */
7653
7654 void
7655 disable_breakpoints_in_shlibs (void)
7656 {
7657 struct bp_location *loc, **locp_tmp;
7658
7659 ALL_BP_LOCATIONS (loc, locp_tmp)
7660 {
7661 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7662 struct breakpoint *b = loc->owner;
7663
7664 /* We apply the check to all breakpoints, including disabled for
7665 those with loc->duplicate set. This is so that when breakpoint
7666 becomes enabled, or the duplicate is removed, gdb will try to
7667 insert all breakpoints. If we don't set shlib_disabled here,
7668 we'll try to insert those breakpoints and fail. */
7669 if (((b->type == bp_breakpoint)
7670 || (b->type == bp_jit_event)
7671 || (b->type == bp_hardware_breakpoint)
7672 || (is_tracepoint (b)))
7673 && loc->pspace == current_program_space
7674 && !loc->shlib_disabled
7675 && solib_name_from_address (loc->pspace, loc->address)
7676 )
7677 {
7678 loc->shlib_disabled = 1;
7679 }
7680 }
7681 }
7682
7683 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7684 notification of unloaded_shlib. Only apply to enabled breakpoints,
7685 disabled ones can just stay disabled. */
7686
7687 static void
7688 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7689 {
7690 struct bp_location *loc, **locp_tmp;
7691 int disabled_shlib_breaks = 0;
7692
7693 ALL_BP_LOCATIONS (loc, locp_tmp)
7694 {
7695 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7696 struct breakpoint *b = loc->owner;
7697
7698 if (solib->pspace == loc->pspace
7699 && !loc->shlib_disabled
7700 && (((b->type == bp_breakpoint
7701 || b->type == bp_jit_event
7702 || b->type == bp_hardware_breakpoint)
7703 && (loc->loc_type == bp_loc_hardware_breakpoint
7704 || loc->loc_type == bp_loc_software_breakpoint))
7705 || is_tracepoint (b))
7706 && solib_contains_address_p (solib, loc->address))
7707 {
7708 loc->shlib_disabled = 1;
7709 /* At this point, we cannot rely on remove_breakpoint
7710 succeeding so we must mark the breakpoint as not inserted
7711 to prevent future errors occurring in remove_breakpoints. */
7712 loc->inserted = 0;
7713
7714 /* This may cause duplicate notifications for the same breakpoint. */
7715 gdb::observers::breakpoint_modified.notify (b);
7716
7717 if (!disabled_shlib_breaks)
7718 {
7719 target_terminal::ours_for_output ();
7720 warning (_("Temporarily disabling breakpoints "
7721 "for unloaded shared library \"%s\""),
7722 solib->so_name);
7723 }
7724 disabled_shlib_breaks = 1;
7725 }
7726 }
7727 }
7728
7729 /* Disable any breakpoints and tracepoints in OBJFILE upon
7730 notification of free_objfile. Only apply to enabled breakpoints,
7731 disabled ones can just stay disabled. */
7732
7733 static void
7734 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7735 {
7736 struct breakpoint *b;
7737
7738 if (objfile == NULL)
7739 return;
7740
7741 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7742 managed by the user with add-symbol-file/remove-symbol-file.
7743 Similarly to how breakpoints in shared libraries are handled in
7744 response to "nosharedlibrary", mark breakpoints in such modules
7745 shlib_disabled so they end up uninserted on the next global
7746 location list update. Shared libraries not loaded by the user
7747 aren't handled here -- they're already handled in
7748 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7749 solib_unloaded observer. We skip objfiles that are not
7750 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7751 main objfile). */
7752 if ((objfile->flags & OBJF_SHARED) == 0
7753 || (objfile->flags & OBJF_USERLOADED) == 0)
7754 return;
7755
7756 ALL_BREAKPOINTS (b)
7757 {
7758 struct bp_location *loc;
7759 int bp_modified = 0;
7760
7761 if (!is_breakpoint (b) && !is_tracepoint (b))
7762 continue;
7763
7764 for (loc = b->loc; loc != NULL; loc = loc->next)
7765 {
7766 CORE_ADDR loc_addr = loc->address;
7767
7768 if (loc->loc_type != bp_loc_hardware_breakpoint
7769 && loc->loc_type != bp_loc_software_breakpoint)
7770 continue;
7771
7772 if (loc->shlib_disabled != 0)
7773 continue;
7774
7775 if (objfile->pspace != loc->pspace)
7776 continue;
7777
7778 if (loc->loc_type != bp_loc_hardware_breakpoint
7779 && loc->loc_type != bp_loc_software_breakpoint)
7780 continue;
7781
7782 if (is_addr_in_objfile (loc_addr, objfile))
7783 {
7784 loc->shlib_disabled = 1;
7785 /* At this point, we don't know whether the object was
7786 unmapped from the inferior or not, so leave the
7787 inserted flag alone. We'll handle failure to
7788 uninsert quietly, in case the object was indeed
7789 unmapped. */
7790
7791 mark_breakpoint_location_modified (loc);
7792
7793 bp_modified = 1;
7794 }
7795 }
7796
7797 if (bp_modified)
7798 gdb::observers::breakpoint_modified.notify (b);
7799 }
7800 }
7801
7802 /* FORK & VFORK catchpoints. */
7803
7804 /* An instance of this type is used to represent a fork or vfork
7805 catchpoint. A breakpoint is really of this type iff its ops pointer points
7806 to CATCH_FORK_BREAKPOINT_OPS. */
7807
7808 struct fork_catchpoint : public breakpoint
7809 {
7810 /* Process id of a child process whose forking triggered this
7811 catchpoint. This field is only valid immediately after this
7812 catchpoint has triggered. */
7813 ptid_t forked_inferior_pid;
7814 };
7815
7816 /* Implement the "insert" breakpoint_ops method for fork
7817 catchpoints. */
7818
7819 static int
7820 insert_catch_fork (struct bp_location *bl)
7821 {
7822 return target_insert_fork_catchpoint (inferior_ptid.pid ());
7823 }
7824
7825 /* Implement the "remove" breakpoint_ops method for fork
7826 catchpoints. */
7827
7828 static int
7829 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
7830 {
7831 return target_remove_fork_catchpoint (inferior_ptid.pid ());
7832 }
7833
7834 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7835 catchpoints. */
7836
7837 static int
7838 breakpoint_hit_catch_fork (const struct bp_location *bl,
7839 const address_space *aspace, CORE_ADDR bp_addr,
7840 const struct target_waitstatus *ws)
7841 {
7842 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7843
7844 if (ws->kind != TARGET_WAITKIND_FORKED)
7845 return 0;
7846
7847 c->forked_inferior_pid = ws->value.related_pid;
7848 return 1;
7849 }
7850
7851 /* Implement the "print_it" breakpoint_ops method for fork
7852 catchpoints. */
7853
7854 static enum print_stop_action
7855 print_it_catch_fork (bpstat bs)
7856 {
7857 struct ui_out *uiout = current_uiout;
7858 struct breakpoint *b = bs->breakpoint_at;
7859 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7860
7861 annotate_catchpoint (b->number);
7862 maybe_print_thread_hit_breakpoint (uiout);
7863 if (b->disposition == disp_del)
7864 uiout->text ("Temporary catchpoint ");
7865 else
7866 uiout->text ("Catchpoint ");
7867 if (uiout->is_mi_like_p ())
7868 {
7869 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7870 uiout->field_string ("disp", bpdisp_text (b->disposition));
7871 }
7872 uiout->field_signed ("bkptno", b->number);
7873 uiout->text (" (forked process ");
7874 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7875 uiout->text ("), ");
7876 return PRINT_SRC_AND_LOC;
7877 }
7878
7879 /* Implement the "print_one" breakpoint_ops method for fork
7880 catchpoints. */
7881
7882 static void
7883 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7884 {
7885 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7886 struct value_print_options opts;
7887 struct ui_out *uiout = current_uiout;
7888
7889 get_user_print_options (&opts);
7890
7891 /* Field 4, the address, is omitted (which makes the columns not
7892 line up too nicely with the headers, but the effect is relatively
7893 readable). */
7894 if (opts.addressprint)
7895 uiout->field_skip ("addr");
7896 annotate_field (5);
7897 uiout->text ("fork");
7898 if (c->forked_inferior_pid != null_ptid)
7899 {
7900 uiout->text (", process ");
7901 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7902 uiout->spaces (1);
7903 }
7904
7905 if (uiout->is_mi_like_p ())
7906 uiout->field_string ("catch-type", "fork");
7907 }
7908
7909 /* Implement the "print_mention" breakpoint_ops method for fork
7910 catchpoints. */
7911
7912 static void
7913 print_mention_catch_fork (struct breakpoint *b)
7914 {
7915 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7916 }
7917
7918 /* Implement the "print_recreate" breakpoint_ops method for fork
7919 catchpoints. */
7920
7921 static void
7922 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7923 {
7924 fprintf_unfiltered (fp, "catch fork");
7925 print_recreate_thread (b, fp);
7926 }
7927
7928 /* The breakpoint_ops structure to be used in fork catchpoints. */
7929
7930 static struct breakpoint_ops catch_fork_breakpoint_ops;
7931
7932 /* Implement the "insert" breakpoint_ops method for vfork
7933 catchpoints. */
7934
7935 static int
7936 insert_catch_vfork (struct bp_location *bl)
7937 {
7938 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
7939 }
7940
7941 /* Implement the "remove" breakpoint_ops method for vfork
7942 catchpoints. */
7943
7944 static int
7945 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
7946 {
7947 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
7948 }
7949
7950 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7951 catchpoints. */
7952
7953 static int
7954 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7955 const address_space *aspace, CORE_ADDR bp_addr,
7956 const struct target_waitstatus *ws)
7957 {
7958 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7959
7960 if (ws->kind != TARGET_WAITKIND_VFORKED)
7961 return 0;
7962
7963 c->forked_inferior_pid = ws->value.related_pid;
7964 return 1;
7965 }
7966
7967 /* Implement the "print_it" breakpoint_ops method for vfork
7968 catchpoints. */
7969
7970 static enum print_stop_action
7971 print_it_catch_vfork (bpstat bs)
7972 {
7973 struct ui_out *uiout = current_uiout;
7974 struct breakpoint *b = bs->breakpoint_at;
7975 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7976
7977 annotate_catchpoint (b->number);
7978 maybe_print_thread_hit_breakpoint (uiout);
7979 if (b->disposition == disp_del)
7980 uiout->text ("Temporary catchpoint ");
7981 else
7982 uiout->text ("Catchpoint ");
7983 if (uiout->is_mi_like_p ())
7984 {
7985 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7986 uiout->field_string ("disp", bpdisp_text (b->disposition));
7987 }
7988 uiout->field_signed ("bkptno", b->number);
7989 uiout->text (" (vforked process ");
7990 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7991 uiout->text ("), ");
7992 return PRINT_SRC_AND_LOC;
7993 }
7994
7995 /* Implement the "print_one" breakpoint_ops method for vfork
7996 catchpoints. */
7997
7998 static void
7999 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
8000 {
8001 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8002 struct value_print_options opts;
8003 struct ui_out *uiout = current_uiout;
8004
8005 get_user_print_options (&opts);
8006 /* Field 4, the address, is omitted (which makes the columns not
8007 line up too nicely with the headers, but the effect is relatively
8008 readable). */
8009 if (opts.addressprint)
8010 uiout->field_skip ("addr");
8011 annotate_field (5);
8012 uiout->text ("vfork");
8013 if (c->forked_inferior_pid != null_ptid)
8014 {
8015 uiout->text (", process ");
8016 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
8017 uiout->spaces (1);
8018 }
8019
8020 if (uiout->is_mi_like_p ())
8021 uiout->field_string ("catch-type", "vfork");
8022 }
8023
8024 /* Implement the "print_mention" breakpoint_ops method for vfork
8025 catchpoints. */
8026
8027 static void
8028 print_mention_catch_vfork (struct breakpoint *b)
8029 {
8030 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8031 }
8032
8033 /* Implement the "print_recreate" breakpoint_ops method for vfork
8034 catchpoints. */
8035
8036 static void
8037 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8038 {
8039 fprintf_unfiltered (fp, "catch vfork");
8040 print_recreate_thread (b, fp);
8041 }
8042
8043 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8044
8045 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8046
8047 /* An instance of this type is used to represent an solib catchpoint.
8048 A breakpoint is really of this type iff its ops pointer points to
8049 CATCH_SOLIB_BREAKPOINT_OPS. */
8050
8051 struct solib_catchpoint : public breakpoint
8052 {
8053 ~solib_catchpoint () override;
8054
8055 /* True for "catch load", false for "catch unload". */
8056 bool is_load;
8057
8058 /* Regular expression to match, if any. COMPILED is only valid when
8059 REGEX is non-NULL. */
8060 char *regex;
8061 std::unique_ptr<compiled_regex> compiled;
8062 };
8063
8064 solib_catchpoint::~solib_catchpoint ()
8065 {
8066 xfree (this->regex);
8067 }
8068
8069 static int
8070 insert_catch_solib (struct bp_location *ignore)
8071 {
8072 return 0;
8073 }
8074
8075 static int
8076 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
8077 {
8078 return 0;
8079 }
8080
8081 static int
8082 breakpoint_hit_catch_solib (const struct bp_location *bl,
8083 const address_space *aspace,
8084 CORE_ADDR bp_addr,
8085 const struct target_waitstatus *ws)
8086 {
8087 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8088 struct breakpoint *other;
8089
8090 if (ws->kind == TARGET_WAITKIND_LOADED)
8091 return 1;
8092
8093 ALL_BREAKPOINTS (other)
8094 {
8095 struct bp_location *other_bl;
8096
8097 if (other == bl->owner)
8098 continue;
8099
8100 if (other->type != bp_shlib_event)
8101 continue;
8102
8103 if (self->pspace != NULL && other->pspace != self->pspace)
8104 continue;
8105
8106 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8107 {
8108 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8109 return 1;
8110 }
8111 }
8112
8113 return 0;
8114 }
8115
8116 static void
8117 check_status_catch_solib (struct bpstats *bs)
8118 {
8119 struct solib_catchpoint *self
8120 = (struct solib_catchpoint *) bs->breakpoint_at;
8121
8122 if (self->is_load)
8123 {
8124 for (so_list *iter : current_program_space->added_solibs)
8125 {
8126 if (!self->regex
8127 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
8128 return;
8129 }
8130 }
8131 else
8132 {
8133 for (const std::string &iter : current_program_space->deleted_solibs)
8134 {
8135 if (!self->regex
8136 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
8137 return;
8138 }
8139 }
8140
8141 bs->stop = 0;
8142 bs->print_it = print_it_noop;
8143 }
8144
8145 static enum print_stop_action
8146 print_it_catch_solib (bpstat bs)
8147 {
8148 struct breakpoint *b = bs->breakpoint_at;
8149 struct ui_out *uiout = current_uiout;
8150
8151 annotate_catchpoint (b->number);
8152 maybe_print_thread_hit_breakpoint (uiout);
8153 if (b->disposition == disp_del)
8154 uiout->text ("Temporary catchpoint ");
8155 else
8156 uiout->text ("Catchpoint ");
8157 uiout->field_signed ("bkptno", b->number);
8158 uiout->text ("\n");
8159 if (uiout->is_mi_like_p ())
8160 uiout->field_string ("disp", bpdisp_text (b->disposition));
8161 print_solib_event (1);
8162 return PRINT_SRC_AND_LOC;
8163 }
8164
8165 static void
8166 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8167 {
8168 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8169 struct value_print_options opts;
8170 struct ui_out *uiout = current_uiout;
8171
8172 get_user_print_options (&opts);
8173 /* Field 4, the address, is omitted (which makes the columns not
8174 line up too nicely with the headers, but the effect is relatively
8175 readable). */
8176 if (opts.addressprint)
8177 {
8178 annotate_field (4);
8179 uiout->field_skip ("addr");
8180 }
8181
8182 std::string msg;
8183 annotate_field (5);
8184 if (self->is_load)
8185 {
8186 if (self->regex)
8187 msg = string_printf (_("load of library matching %s"), self->regex);
8188 else
8189 msg = _("load of library");
8190 }
8191 else
8192 {
8193 if (self->regex)
8194 msg = string_printf (_("unload of library matching %s"), self->regex);
8195 else
8196 msg = _("unload of library");
8197 }
8198 uiout->field_string ("what", msg);
8199
8200 if (uiout->is_mi_like_p ())
8201 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8202 }
8203
8204 static void
8205 print_mention_catch_solib (struct breakpoint *b)
8206 {
8207 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8208
8209 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8210 self->is_load ? "load" : "unload");
8211 }
8212
8213 static void
8214 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8215 {
8216 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8217
8218 fprintf_unfiltered (fp, "%s %s",
8219 b->disposition == disp_del ? "tcatch" : "catch",
8220 self->is_load ? "load" : "unload");
8221 if (self->regex)
8222 fprintf_unfiltered (fp, " %s", self->regex);
8223 fprintf_unfiltered (fp, "\n");
8224 }
8225
8226 static struct breakpoint_ops catch_solib_breakpoint_ops;
8227
8228 /* See breakpoint.h. */
8229
8230 void
8231 add_solib_catchpoint (const char *arg, bool is_load, bool is_temp, bool enabled)
8232 {
8233 struct gdbarch *gdbarch = get_current_arch ();
8234
8235 if (!arg)
8236 arg = "";
8237 arg = skip_spaces (arg);
8238
8239 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
8240
8241 if (*arg != '\0')
8242 {
8243 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8244 _("Invalid regexp")));
8245 c->regex = xstrdup (arg);
8246 }
8247
8248 c->is_load = is_load;
8249 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
8250 &catch_solib_breakpoint_ops);
8251
8252 c->enable_state = enabled ? bp_enabled : bp_disabled;
8253
8254 install_breakpoint (0, std::move (c), 1);
8255 }
8256
8257 /* A helper function that does all the work for "catch load" and
8258 "catch unload". */
8259
8260 static void
8261 catch_load_or_unload (const char *arg, int from_tty, int is_load,
8262 struct cmd_list_element *command)
8263 {
8264 const int enabled = 1;
8265 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
8266
8267 add_solib_catchpoint (arg, is_load, temp, enabled);
8268 }
8269
8270 static void
8271 catch_load_command_1 (const char *arg, int from_tty,
8272 struct cmd_list_element *command)
8273 {
8274 catch_load_or_unload (arg, from_tty, 1, command);
8275 }
8276
8277 static void
8278 catch_unload_command_1 (const char *arg, int from_tty,
8279 struct cmd_list_element *command)
8280 {
8281 catch_load_or_unload (arg, from_tty, 0, command);
8282 }
8283
8284 /* See breakpoint.h. */
8285
8286 void
8287 init_catchpoint (struct breakpoint *b,
8288 struct gdbarch *gdbarch, bool temp,
8289 const char *cond_string,
8290 const struct breakpoint_ops *ops)
8291 {
8292 symtab_and_line sal;
8293 sal.pspace = current_program_space;
8294
8295 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8296
8297 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8298 b->disposition = temp ? disp_del : disp_donttouch;
8299 }
8300
8301 void
8302 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8303 {
8304 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8305 set_breakpoint_number (internal, b);
8306 if (is_tracepoint (b))
8307 set_tracepoint_count (breakpoint_count);
8308 if (!internal)
8309 mention (b);
8310 gdb::observers::breakpoint_created.notify (b);
8311
8312 if (update_gll)
8313 update_global_location_list (UGLL_MAY_INSERT);
8314 }
8315
8316 static void
8317 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8318 bool temp, const char *cond_string,
8319 const struct breakpoint_ops *ops)
8320 {
8321 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
8322
8323 init_catchpoint (c.get (), gdbarch, temp, cond_string, ops);
8324
8325 c->forked_inferior_pid = null_ptid;
8326
8327 install_breakpoint (0, std::move (c), 1);
8328 }
8329
8330 /* Exec catchpoints. */
8331
8332 /* An instance of this type is used to represent an exec catchpoint.
8333 A breakpoint is really of this type iff its ops pointer points to
8334 CATCH_EXEC_BREAKPOINT_OPS. */
8335
8336 struct exec_catchpoint : public breakpoint
8337 {
8338 ~exec_catchpoint () override;
8339
8340 /* Filename of a program whose exec triggered this catchpoint.
8341 This field is only valid immediately after this catchpoint has
8342 triggered. */
8343 char *exec_pathname;
8344 };
8345
8346 /* Exec catchpoint destructor. */
8347
8348 exec_catchpoint::~exec_catchpoint ()
8349 {
8350 xfree (this->exec_pathname);
8351 }
8352
8353 static int
8354 insert_catch_exec (struct bp_location *bl)
8355 {
8356 return target_insert_exec_catchpoint (inferior_ptid.pid ());
8357 }
8358
8359 static int
8360 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8361 {
8362 return target_remove_exec_catchpoint (inferior_ptid.pid ());
8363 }
8364
8365 static int
8366 breakpoint_hit_catch_exec (const struct bp_location *bl,
8367 const address_space *aspace, CORE_ADDR bp_addr,
8368 const struct target_waitstatus *ws)
8369 {
8370 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8371
8372 if (ws->kind != TARGET_WAITKIND_EXECD)
8373 return 0;
8374
8375 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8376 return 1;
8377 }
8378
8379 static enum print_stop_action
8380 print_it_catch_exec (bpstat bs)
8381 {
8382 struct ui_out *uiout = current_uiout;
8383 struct breakpoint *b = bs->breakpoint_at;
8384 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8385
8386 annotate_catchpoint (b->number);
8387 maybe_print_thread_hit_breakpoint (uiout);
8388 if (b->disposition == disp_del)
8389 uiout->text ("Temporary catchpoint ");
8390 else
8391 uiout->text ("Catchpoint ");
8392 if (uiout->is_mi_like_p ())
8393 {
8394 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8395 uiout->field_string ("disp", bpdisp_text (b->disposition));
8396 }
8397 uiout->field_signed ("bkptno", b->number);
8398 uiout->text (" (exec'd ");
8399 uiout->field_string ("new-exec", c->exec_pathname);
8400 uiout->text ("), ");
8401
8402 return PRINT_SRC_AND_LOC;
8403 }
8404
8405 static void
8406 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8407 {
8408 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8409 struct value_print_options opts;
8410 struct ui_out *uiout = current_uiout;
8411
8412 get_user_print_options (&opts);
8413
8414 /* Field 4, the address, is omitted (which makes the columns
8415 not line up too nicely with the headers, but the effect
8416 is relatively readable). */
8417 if (opts.addressprint)
8418 uiout->field_skip ("addr");
8419 annotate_field (5);
8420 uiout->text ("exec");
8421 if (c->exec_pathname != NULL)
8422 {
8423 uiout->text (", program \"");
8424 uiout->field_string ("what", c->exec_pathname);
8425 uiout->text ("\" ");
8426 }
8427
8428 if (uiout->is_mi_like_p ())
8429 uiout->field_string ("catch-type", "exec");
8430 }
8431
8432 static void
8433 print_mention_catch_exec (struct breakpoint *b)
8434 {
8435 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8436 }
8437
8438 /* Implement the "print_recreate" breakpoint_ops method for exec
8439 catchpoints. */
8440
8441 static void
8442 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8443 {
8444 fprintf_unfiltered (fp, "catch exec");
8445 print_recreate_thread (b, fp);
8446 }
8447
8448 static struct breakpoint_ops catch_exec_breakpoint_ops;
8449
8450 static int
8451 hw_breakpoint_used_count (void)
8452 {
8453 int i = 0;
8454 struct breakpoint *b;
8455 struct bp_location *bl;
8456
8457 ALL_BREAKPOINTS (b)
8458 {
8459 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8460 for (bl = b->loc; bl; bl = bl->next)
8461 {
8462 /* Special types of hardware breakpoints may use more than
8463 one register. */
8464 i += b->ops->resources_needed (bl);
8465 }
8466 }
8467
8468 return i;
8469 }
8470
8471 /* Returns the resources B would use if it were a hardware
8472 watchpoint. */
8473
8474 static int
8475 hw_watchpoint_use_count (struct breakpoint *b)
8476 {
8477 int i = 0;
8478 struct bp_location *bl;
8479
8480 if (!breakpoint_enabled (b))
8481 return 0;
8482
8483 for (bl = b->loc; bl; bl = bl->next)
8484 {
8485 /* Special types of hardware watchpoints may use more than
8486 one register. */
8487 i += b->ops->resources_needed (bl);
8488 }
8489
8490 return i;
8491 }
8492
8493 /* Returns the sum the used resources of all hardware watchpoints of
8494 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8495 the sum of the used resources of all hardware watchpoints of other
8496 types _not_ TYPE. */
8497
8498 static int
8499 hw_watchpoint_used_count_others (struct breakpoint *except,
8500 enum bptype type, int *other_type_used)
8501 {
8502 int i = 0;
8503 struct breakpoint *b;
8504
8505 *other_type_used = 0;
8506 ALL_BREAKPOINTS (b)
8507 {
8508 if (b == except)
8509 continue;
8510 if (!breakpoint_enabled (b))
8511 continue;
8512
8513 if (b->type == type)
8514 i += hw_watchpoint_use_count (b);
8515 else if (is_hardware_watchpoint (b))
8516 *other_type_used = 1;
8517 }
8518
8519 return i;
8520 }
8521
8522 void
8523 disable_watchpoints_before_interactive_call_start (void)
8524 {
8525 struct breakpoint *b;
8526
8527 ALL_BREAKPOINTS (b)
8528 {
8529 if (is_watchpoint (b) && breakpoint_enabled (b))
8530 {
8531 b->enable_state = bp_call_disabled;
8532 update_global_location_list (UGLL_DONT_INSERT);
8533 }
8534 }
8535 }
8536
8537 void
8538 enable_watchpoints_after_interactive_call_stop (void)
8539 {
8540 struct breakpoint *b;
8541
8542 ALL_BREAKPOINTS (b)
8543 {
8544 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8545 {
8546 b->enable_state = bp_enabled;
8547 update_global_location_list (UGLL_MAY_INSERT);
8548 }
8549 }
8550 }
8551
8552 void
8553 disable_breakpoints_before_startup (void)
8554 {
8555 current_program_space->executing_startup = 1;
8556 update_global_location_list (UGLL_DONT_INSERT);
8557 }
8558
8559 void
8560 enable_breakpoints_after_startup (void)
8561 {
8562 current_program_space->executing_startup = 0;
8563 breakpoint_re_set ();
8564 }
8565
8566 /* Create a new single-step breakpoint for thread THREAD, with no
8567 locations. */
8568
8569 static struct breakpoint *
8570 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8571 {
8572 std::unique_ptr<breakpoint> b (new breakpoint ());
8573
8574 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
8575 &momentary_breakpoint_ops);
8576
8577 b->disposition = disp_donttouch;
8578 b->frame_id = null_frame_id;
8579
8580 b->thread = thread;
8581 gdb_assert (b->thread != 0);
8582
8583 return add_to_breakpoint_chain (std::move (b));
8584 }
8585
8586 /* Set a momentary breakpoint of type TYPE at address specified by
8587 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8588 frame. */
8589
8590 breakpoint_up
8591 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8592 struct frame_id frame_id, enum bptype type)
8593 {
8594 struct breakpoint *b;
8595
8596 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8597 tail-called one. */
8598 gdb_assert (!frame_id_artificial_p (frame_id));
8599
8600 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8601 b->enable_state = bp_enabled;
8602 b->disposition = disp_donttouch;
8603 b->frame_id = frame_id;
8604
8605 b->thread = inferior_thread ()->global_num;
8606
8607 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8608
8609 return breakpoint_up (b);
8610 }
8611
8612 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8613 The new breakpoint will have type TYPE, use OPS as its
8614 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8615
8616 static struct breakpoint *
8617 momentary_breakpoint_from_master (struct breakpoint *orig,
8618 enum bptype type,
8619 const struct breakpoint_ops *ops,
8620 int loc_enabled)
8621 {
8622 struct breakpoint *copy;
8623
8624 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8625 copy->loc = allocate_bp_location (copy);
8626 set_breakpoint_location_function (copy->loc);
8627
8628 copy->loc->gdbarch = orig->loc->gdbarch;
8629 copy->loc->requested_address = orig->loc->requested_address;
8630 copy->loc->address = orig->loc->address;
8631 copy->loc->section = orig->loc->section;
8632 copy->loc->pspace = orig->loc->pspace;
8633 copy->loc->probe = orig->loc->probe;
8634 copy->loc->line_number = orig->loc->line_number;
8635 copy->loc->symtab = orig->loc->symtab;
8636 copy->loc->enabled = loc_enabled;
8637 copy->frame_id = orig->frame_id;
8638 copy->thread = orig->thread;
8639 copy->pspace = orig->pspace;
8640
8641 copy->enable_state = bp_enabled;
8642 copy->disposition = disp_donttouch;
8643 copy->number = internal_breakpoint_number--;
8644
8645 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8646 return copy;
8647 }
8648
8649 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8650 ORIG is NULL. */
8651
8652 struct breakpoint *
8653 clone_momentary_breakpoint (struct breakpoint *orig)
8654 {
8655 /* If there's nothing to clone, then return nothing. */
8656 if (orig == NULL)
8657 return NULL;
8658
8659 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8660 }
8661
8662 breakpoint_up
8663 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8664 enum bptype type)
8665 {
8666 struct symtab_and_line sal;
8667
8668 sal = find_pc_line (pc, 0);
8669 sal.pc = pc;
8670 sal.section = find_pc_overlay (pc);
8671 sal.explicit_pc = 1;
8672
8673 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8674 }
8675 \f
8676
8677 /* Tell the user we have just set a breakpoint B. */
8678
8679 static void
8680 mention (struct breakpoint *b)
8681 {
8682 b->ops->print_mention (b);
8683 current_uiout->text ("\n");
8684 }
8685 \f
8686
8687 static bool bp_loc_is_permanent (struct bp_location *loc);
8688
8689 /* Handle "set breakpoint auto-hw on".
8690
8691 If the explicitly specified breakpoint type is not hardware
8692 breakpoint, check the memory map to see whether the breakpoint
8693 address is in read-only memory.
8694
8695 - location type is not hardware breakpoint, memory is read-only.
8696 We change the type of the location to hardware breakpoint.
8697
8698 - location type is hardware breakpoint, memory is read-write. This
8699 means we've previously made the location hardware one, but then the
8700 memory map changed, so we undo.
8701 */
8702
8703 static void
8704 handle_automatic_hardware_breakpoints (bp_location *bl)
8705 {
8706 if (automatic_hardware_breakpoints
8707 && bl->owner->type != bp_hardware_breakpoint
8708 && (bl->loc_type == bp_loc_software_breakpoint
8709 || bl->loc_type == bp_loc_hardware_breakpoint))
8710 {
8711 /* When breakpoints are removed, remove_breakpoints will use
8712 location types we've just set here, the only possible problem
8713 is that memory map has changed during running program, but
8714 it's not going to work anyway with current gdb. */
8715 mem_region *mr = lookup_mem_region (bl->address);
8716
8717 if (mr != nullptr)
8718 {
8719 enum bp_loc_type new_type;
8720
8721 if (mr->attrib.mode != MEM_RW)
8722 new_type = bp_loc_hardware_breakpoint;
8723 else
8724 new_type = bp_loc_software_breakpoint;
8725
8726 if (new_type != bl->loc_type)
8727 {
8728 static bool said = false;
8729
8730 bl->loc_type = new_type;
8731 if (!said)
8732 {
8733 fprintf_filtered (gdb_stdout,
8734 _("Note: automatically using "
8735 "hardware breakpoints for "
8736 "read-only addresses.\n"));
8737 said = true;
8738 }
8739 }
8740 }
8741 }
8742 }
8743
8744 static struct bp_location *
8745 add_location_to_breakpoint (struct breakpoint *b,
8746 const struct symtab_and_line *sal)
8747 {
8748 struct bp_location *loc, **tmp;
8749 CORE_ADDR adjusted_address;
8750 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8751
8752 if (loc_gdbarch == NULL)
8753 loc_gdbarch = b->gdbarch;
8754
8755 /* Adjust the breakpoint's address prior to allocating a location.
8756 Once we call allocate_bp_location(), that mostly uninitialized
8757 location will be placed on the location chain. Adjustment of the
8758 breakpoint may cause target_read_memory() to be called and we do
8759 not want its scan of the location chain to find a breakpoint and
8760 location that's only been partially initialized. */
8761 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8762 sal->pc, b->type);
8763
8764 /* Sort the locations by their ADDRESS. */
8765 loc = allocate_bp_location (b);
8766 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8767 tmp = &((*tmp)->next))
8768 ;
8769 loc->next = *tmp;
8770 *tmp = loc;
8771
8772 loc->requested_address = sal->pc;
8773 loc->address = adjusted_address;
8774 loc->pspace = sal->pspace;
8775 loc->probe.prob = sal->prob;
8776 loc->probe.objfile = sal->objfile;
8777 gdb_assert (loc->pspace != NULL);
8778 loc->section = sal->section;
8779 loc->gdbarch = loc_gdbarch;
8780 loc->line_number = sal->line;
8781 loc->symtab = sal->symtab;
8782 loc->symbol = sal->symbol;
8783 loc->msymbol = sal->msymbol;
8784 loc->objfile = sal->objfile;
8785
8786 set_breakpoint_location_function (loc);
8787
8788 /* While by definition, permanent breakpoints are already present in the
8789 code, we don't mark the location as inserted. Normally one would expect
8790 that GDB could rely on that breakpoint instruction to stop the program,
8791 thus removing the need to insert its own breakpoint, except that executing
8792 the breakpoint instruction can kill the target instead of reporting a
8793 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8794 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8795 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8796 breakpoint be inserted normally results in QEMU knowing about the GDB
8797 breakpoint, and thus trap before the breakpoint instruction is executed.
8798 (If GDB later needs to continue execution past the permanent breakpoint,
8799 it manually increments the PC, thus avoiding executing the breakpoint
8800 instruction.) */
8801 if (bp_loc_is_permanent (loc))
8802 loc->permanent = 1;
8803
8804 return loc;
8805 }
8806 \f
8807
8808 /* Return true if LOC is pointing to a permanent breakpoint,
8809 return false otherwise. */
8810
8811 static bool
8812 bp_loc_is_permanent (struct bp_location *loc)
8813 {
8814 gdb_assert (loc != NULL);
8815
8816 /* If we have a non-breakpoint-backed catchpoint or a software
8817 watchpoint, just return 0. We should not attempt to read from
8818 the addresses the locations of these breakpoint types point to.
8819 gdbarch_program_breakpoint_here_p, below, will attempt to read
8820 memory. */
8821 if (!bl_address_is_meaningful (loc))
8822 return false;
8823
8824 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8825 switch_to_program_space_and_thread (loc->pspace);
8826 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
8827 }
8828
8829 /* Build a command list for the dprintf corresponding to the current
8830 settings of the dprintf style options. */
8831
8832 static void
8833 update_dprintf_command_list (struct breakpoint *b)
8834 {
8835 char *dprintf_args = b->extra_string;
8836 char *printf_line = NULL;
8837
8838 if (!dprintf_args)
8839 return;
8840
8841 dprintf_args = skip_spaces (dprintf_args);
8842
8843 /* Allow a comma, as it may have terminated a location, but don't
8844 insist on it. */
8845 if (*dprintf_args == ',')
8846 ++dprintf_args;
8847 dprintf_args = skip_spaces (dprintf_args);
8848
8849 if (*dprintf_args != '"')
8850 error (_("Bad format string, missing '\"'."));
8851
8852 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8853 printf_line = xstrprintf ("printf %s", dprintf_args);
8854 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8855 {
8856 if (!dprintf_function)
8857 error (_("No function supplied for dprintf call"));
8858
8859 if (dprintf_channel && strlen (dprintf_channel) > 0)
8860 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8861 dprintf_function,
8862 dprintf_channel,
8863 dprintf_args);
8864 else
8865 printf_line = xstrprintf ("call (void) %s (%s)",
8866 dprintf_function,
8867 dprintf_args);
8868 }
8869 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8870 {
8871 if (target_can_run_breakpoint_commands ())
8872 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8873 else
8874 {
8875 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8876 printf_line = xstrprintf ("printf %s", dprintf_args);
8877 }
8878 }
8879 else
8880 internal_error (__FILE__, __LINE__,
8881 _("Invalid dprintf style."));
8882
8883 gdb_assert (printf_line != NULL);
8884
8885 /* Manufacture a printf sequence. */
8886 struct command_line *printf_cmd_line
8887 = new struct command_line (simple_control, printf_line);
8888 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8889 command_lines_deleter ()));
8890 }
8891
8892 /* Update all dprintf commands, making their command lists reflect
8893 current style settings. */
8894
8895 static void
8896 update_dprintf_commands (const char *args, int from_tty,
8897 struct cmd_list_element *c)
8898 {
8899 struct breakpoint *b;
8900
8901 ALL_BREAKPOINTS (b)
8902 {
8903 if (b->type == bp_dprintf)
8904 update_dprintf_command_list (b);
8905 }
8906 }
8907
8908 /* Create a breakpoint with SAL as location. Use LOCATION
8909 as a description of the location, and COND_STRING
8910 as condition expression. If LOCATION is NULL then create an
8911 "address location" from the address in the SAL. */
8912
8913 static void
8914 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8915 gdb::array_view<const symtab_and_line> sals,
8916 event_location_up &&location,
8917 gdb::unique_xmalloc_ptr<char> filter,
8918 gdb::unique_xmalloc_ptr<char> cond_string,
8919 gdb::unique_xmalloc_ptr<char> extra_string,
8920 enum bptype type, enum bpdisp disposition,
8921 int thread, int task, int ignore_count,
8922 const struct breakpoint_ops *ops, int from_tty,
8923 int enabled, int internal, unsigned flags,
8924 int display_canonical)
8925 {
8926 int i;
8927
8928 if (type == bp_hardware_breakpoint)
8929 {
8930 int target_resources_ok;
8931
8932 i = hw_breakpoint_used_count ();
8933 target_resources_ok =
8934 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8935 i + 1, 0);
8936 if (target_resources_ok == 0)
8937 error (_("No hardware breakpoint support in the target."));
8938 else if (target_resources_ok < 0)
8939 error (_("Hardware breakpoints used exceeds limit."));
8940 }
8941
8942 gdb_assert (!sals.empty ());
8943
8944 for (const auto &sal : sals)
8945 {
8946 struct bp_location *loc;
8947
8948 if (from_tty)
8949 {
8950 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8951 if (!loc_gdbarch)
8952 loc_gdbarch = gdbarch;
8953
8954 describe_other_breakpoints (loc_gdbarch,
8955 sal.pspace, sal.pc, sal.section, thread);
8956 }
8957
8958 if (&sal == &sals[0])
8959 {
8960 init_raw_breakpoint (b, gdbarch, sal, type, ops);
8961 b->thread = thread;
8962 b->task = task;
8963
8964 b->cond_string = cond_string.release ();
8965 b->extra_string = extra_string.release ();
8966 b->ignore_count = ignore_count;
8967 b->enable_state = enabled ? bp_enabled : bp_disabled;
8968 b->disposition = disposition;
8969
8970 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8971 b->loc->inserted = 1;
8972
8973 if (type == bp_static_tracepoint)
8974 {
8975 struct tracepoint *t = (struct tracepoint *) b;
8976 struct static_tracepoint_marker marker;
8977
8978 if (strace_marker_p (b))
8979 {
8980 /* We already know the marker exists, otherwise, we
8981 wouldn't see a sal for it. */
8982 const char *p
8983 = &event_location_to_string (b->location.get ())[3];
8984 const char *endp;
8985
8986 p = skip_spaces (p);
8987
8988 endp = skip_to_space (p);
8989
8990 t->static_trace_marker_id.assign (p, endp - p);
8991
8992 printf_filtered (_("Probed static tracepoint "
8993 "marker \"%s\"\n"),
8994 t->static_trace_marker_id.c_str ());
8995 }
8996 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8997 {
8998 t->static_trace_marker_id = std::move (marker.str_id);
8999
9000 printf_filtered (_("Probed static tracepoint "
9001 "marker \"%s\"\n"),
9002 t->static_trace_marker_id.c_str ());
9003 }
9004 else
9005 warning (_("Couldn't determine the static "
9006 "tracepoint marker to probe"));
9007 }
9008
9009 loc = b->loc;
9010 }
9011 else
9012 {
9013 loc = add_location_to_breakpoint (b, &sal);
9014 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9015 loc->inserted = 1;
9016 }
9017
9018 /* Do not set breakpoint locations conditions yet. As locations
9019 are inserted, they get sorted based on their addresses. Let
9020 the list stabilize to have reliable location numbers. */
9021
9022 /* Dynamic printf requires and uses additional arguments on the
9023 command line, otherwise it's an error. */
9024 if (type == bp_dprintf)
9025 {
9026 if (b->extra_string)
9027 update_dprintf_command_list (b);
9028 else
9029 error (_("Format string required"));
9030 }
9031 else if (b->extra_string)
9032 error (_("Garbage '%s' at end of command"), b->extra_string);
9033 }
9034
9035
9036 /* The order of the locations is now stable. Set the location
9037 condition using the location's number. */
9038 int loc_num = 1;
9039 for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)
9040 {
9041 if (b->cond_string != nullptr)
9042 set_breakpoint_location_condition (b->cond_string, loc, b->number,
9043 loc_num);
9044
9045 ++loc_num;
9046 }
9047
9048 b->display_canonical = display_canonical;
9049 if (location != NULL)
9050 b->location = std::move (location);
9051 else
9052 b->location = new_address_location (b->loc->address, NULL, 0);
9053 b->filter = std::move (filter);
9054 }
9055
9056 static void
9057 create_breakpoint_sal (struct gdbarch *gdbarch,
9058 gdb::array_view<const symtab_and_line> sals,
9059 event_location_up &&location,
9060 gdb::unique_xmalloc_ptr<char> filter,
9061 gdb::unique_xmalloc_ptr<char> cond_string,
9062 gdb::unique_xmalloc_ptr<char> extra_string,
9063 enum bptype type, enum bpdisp disposition,
9064 int thread, int task, int ignore_count,
9065 const struct breakpoint_ops *ops, int from_tty,
9066 int enabled, int internal, unsigned flags,
9067 int display_canonical)
9068 {
9069 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
9070
9071 init_breakpoint_sal (b.get (), gdbarch,
9072 sals, std::move (location),
9073 std::move (filter),
9074 std::move (cond_string),
9075 std::move (extra_string),
9076 type, disposition,
9077 thread, task, ignore_count,
9078 ops, from_tty,
9079 enabled, internal, flags,
9080 display_canonical);
9081
9082 install_breakpoint (internal, std::move (b), 0);
9083 }
9084
9085 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9086 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9087 value. COND_STRING, if not NULL, specified the condition to be
9088 used for all breakpoints. Essentially the only case where
9089 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9090 function. In that case, it's still not possible to specify
9091 separate conditions for different overloaded functions, so
9092 we take just a single condition string.
9093
9094 NOTE: If the function succeeds, the caller is expected to cleanup
9095 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9096 array contents). If the function fails (error() is called), the
9097 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9098 COND and SALS arrays and each of those arrays contents. */
9099
9100 static void
9101 create_breakpoints_sal (struct gdbarch *gdbarch,
9102 struct linespec_result *canonical,
9103 gdb::unique_xmalloc_ptr<char> cond_string,
9104 gdb::unique_xmalloc_ptr<char> extra_string,
9105 enum bptype type, enum bpdisp disposition,
9106 int thread, int task, int ignore_count,
9107 const struct breakpoint_ops *ops, int from_tty,
9108 int enabled, int internal, unsigned flags)
9109 {
9110 if (canonical->pre_expanded)
9111 gdb_assert (canonical->lsals.size () == 1);
9112
9113 for (const auto &lsal : canonical->lsals)
9114 {
9115 /* Note that 'location' can be NULL in the case of a plain
9116 'break', without arguments. */
9117 event_location_up location
9118 = (canonical->location != NULL
9119 ? copy_event_location (canonical->location.get ()) : NULL);
9120 gdb::unique_xmalloc_ptr<char> filter_string
9121 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
9122
9123 create_breakpoint_sal (gdbarch, lsal.sals,
9124 std::move (location),
9125 std::move (filter_string),
9126 std::move (cond_string),
9127 std::move (extra_string),
9128 type, disposition,
9129 thread, task, ignore_count, ops,
9130 from_tty, enabled, internal, flags,
9131 canonical->special_display);
9132 }
9133 }
9134
9135 /* Parse LOCATION which is assumed to be a SAL specification possibly
9136 followed by conditionals. On return, SALS contains an array of SAL
9137 addresses found. LOCATION points to the end of the SAL (for
9138 linespec locations).
9139
9140 The array and the line spec strings are allocated on the heap, it is
9141 the caller's responsibility to free them. */
9142
9143 static void
9144 parse_breakpoint_sals (struct event_location *location,
9145 struct linespec_result *canonical)
9146 {
9147 struct symtab_and_line cursal;
9148
9149 if (event_location_type (location) == LINESPEC_LOCATION)
9150 {
9151 const char *spec = get_linespec_location (location)->spec_string;
9152
9153 if (spec == NULL)
9154 {
9155 /* The last displayed codepoint, if it's valid, is our default
9156 breakpoint address. */
9157 if (last_displayed_sal_is_valid ())
9158 {
9159 /* Set sal's pspace, pc, symtab, and line to the values
9160 corresponding to the last call to print_frame_info.
9161 Be sure to reinitialize LINE with NOTCURRENT == 0
9162 as the breakpoint line number is inappropriate otherwise.
9163 find_pc_line would adjust PC, re-set it back. */
9164 symtab_and_line sal = get_last_displayed_sal ();
9165 CORE_ADDR pc = sal.pc;
9166
9167 sal = find_pc_line (pc, 0);
9168
9169 /* "break" without arguments is equivalent to "break *PC"
9170 where PC is the last displayed codepoint's address. So
9171 make sure to set sal.explicit_pc to prevent GDB from
9172 trying to expand the list of sals to include all other
9173 instances with the same symtab and line. */
9174 sal.pc = pc;
9175 sal.explicit_pc = 1;
9176
9177 struct linespec_sals lsal;
9178 lsal.sals = {sal};
9179 lsal.canonical = NULL;
9180
9181 canonical->lsals.push_back (std::move (lsal));
9182 return;
9183 }
9184 else
9185 error (_("No default breakpoint address now."));
9186 }
9187 }
9188
9189 /* Force almost all breakpoints to be in terms of the
9190 current_source_symtab (which is decode_line_1's default).
9191 This should produce the results we want almost all of the
9192 time while leaving default_breakpoint_* alone.
9193
9194 ObjC: However, don't match an Objective-C method name which
9195 may have a '+' or '-' succeeded by a '['. */
9196 cursal = get_current_source_symtab_and_line ();
9197 if (last_displayed_sal_is_valid ())
9198 {
9199 const char *spec = NULL;
9200
9201 if (event_location_type (location) == LINESPEC_LOCATION)
9202 spec = get_linespec_location (location)->spec_string;
9203
9204 if (!cursal.symtab
9205 || (spec != NULL
9206 && strchr ("+-", spec[0]) != NULL
9207 && spec[1] != '['))
9208 {
9209 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9210 get_last_displayed_symtab (),
9211 get_last_displayed_line (),
9212 canonical, NULL, NULL);
9213 return;
9214 }
9215 }
9216
9217 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9218 cursal.symtab, cursal.line, canonical, NULL, NULL);
9219 }
9220
9221
9222 /* Convert each SAL into a real PC. Verify that the PC can be
9223 inserted as a breakpoint. If it can't throw an error. */
9224
9225 static void
9226 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
9227 {
9228 for (auto &sal : sals)
9229 resolve_sal_pc (&sal);
9230 }
9231
9232 /* Fast tracepoints may have restrictions on valid locations. For
9233 instance, a fast tracepoint using a jump instead of a trap will
9234 likely have to overwrite more bytes than a trap would, and so can
9235 only be placed where the instruction is longer than the jump, or a
9236 multi-instruction sequence does not have a jump into the middle of
9237 it, etc. */
9238
9239 static void
9240 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9241 gdb::array_view<const symtab_and_line> sals)
9242 {
9243 for (const auto &sal : sals)
9244 {
9245 struct gdbarch *sarch;
9246
9247 sarch = get_sal_arch (sal);
9248 /* We fall back to GDBARCH if there is no architecture
9249 associated with SAL. */
9250 if (sarch == NULL)
9251 sarch = gdbarch;
9252 std::string msg;
9253 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
9254 error (_("May not have a fast tracepoint at %s%s"),
9255 paddress (sarch, sal.pc), msg.c_str ());
9256 }
9257 }
9258
9259 /* Given TOK, a string specification of condition and thread, as
9260 accepted by the 'break' command, extract the condition
9261 string and thread number and set *COND_STRING and *THREAD.
9262 PC identifies the context at which the condition should be parsed.
9263 If no condition is found, *COND_STRING is set to NULL.
9264 If no thread is found, *THREAD is set to -1. */
9265
9266 static void
9267 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9268 char **cond_string, int *thread, int *task,
9269 char **rest)
9270 {
9271 *cond_string = NULL;
9272 *thread = -1;
9273 *task = 0;
9274 *rest = NULL;
9275 bool force = false;
9276
9277 while (tok && *tok)
9278 {
9279 const char *end_tok;
9280 int toklen;
9281 const char *cond_start = NULL;
9282 const char *cond_end = NULL;
9283
9284 tok = skip_spaces (tok);
9285
9286 if ((*tok == '"' || *tok == ',') && rest)
9287 {
9288 *rest = savestring (tok, strlen (tok));
9289 return;
9290 }
9291
9292 end_tok = skip_to_space (tok);
9293
9294 toklen = end_tok - tok;
9295
9296 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9297 {
9298 tok = cond_start = end_tok + 1;
9299 try
9300 {
9301 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9302 }
9303 catch (const gdb_exception_error &)
9304 {
9305 if (!force)
9306 throw;
9307 else
9308 tok = tok + strlen (tok);
9309 }
9310 cond_end = tok;
9311 *cond_string = savestring (cond_start, cond_end - cond_start);
9312 }
9313 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
9314 {
9315 tok = tok + toklen;
9316 force = true;
9317 }
9318 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9319 {
9320 const char *tmptok;
9321 struct thread_info *thr;
9322
9323 tok = end_tok + 1;
9324 thr = parse_thread_id (tok, &tmptok);
9325 if (tok == tmptok)
9326 error (_("Junk after thread keyword."));
9327 *thread = thr->global_num;
9328 tok = tmptok;
9329 }
9330 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9331 {
9332 char *tmptok;
9333
9334 tok = end_tok + 1;
9335 *task = strtol (tok, &tmptok, 0);
9336 if (tok == tmptok)
9337 error (_("Junk after task keyword."));
9338 if (!valid_task_id (*task))
9339 error (_("Unknown task %d."), *task);
9340 tok = tmptok;
9341 }
9342 else if (rest)
9343 {
9344 *rest = savestring (tok, strlen (tok));
9345 return;
9346 }
9347 else
9348 error (_("Junk at end of arguments."));
9349 }
9350 }
9351
9352 /* Call 'find_condition_and_thread' for each sal in SALS until a parse
9353 succeeds. The parsed values are written to COND_STRING, THREAD,
9354 TASK, and REST. See the comment of 'find_condition_and_thread'
9355 for the description of these parameters and INPUT. */
9356
9357 static void
9358 find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
9359 const char *input, char **cond_string,
9360 int *thread, int *task, char **rest)
9361 {
9362 int num_failures = 0;
9363 for (auto &sal : sals)
9364 {
9365 char *cond = nullptr;
9366 int thread_id = 0;
9367 int task_id = 0;
9368 char *remaining = nullptr;
9369
9370 /* Here we want to parse 'arg' to separate condition from thread
9371 number. But because parsing happens in a context and the
9372 contexts of sals might be different, try each until there is
9373 success. Finding one successful parse is sufficient for our
9374 goal. When setting the breakpoint we'll re-parse the
9375 condition in the context of each sal. */
9376 try
9377 {
9378 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
9379 &task_id, &remaining);
9380 *cond_string = cond;
9381 *thread = thread_id;
9382 *task = task_id;
9383 *rest = remaining;
9384 break;
9385 }
9386 catch (const gdb_exception_error &e)
9387 {
9388 num_failures++;
9389 /* If no sal remains, do not continue. */
9390 if (num_failures == sals.size ())
9391 throw;
9392 }
9393 }
9394 }
9395
9396 /* Decode a static tracepoint marker spec. */
9397
9398 static std::vector<symtab_and_line>
9399 decode_static_tracepoint_spec (const char **arg_p)
9400 {
9401 const char *p = &(*arg_p)[3];
9402 const char *endp;
9403
9404 p = skip_spaces (p);
9405
9406 endp = skip_to_space (p);
9407
9408 std::string marker_str (p, endp - p);
9409
9410 std::vector<static_tracepoint_marker> markers
9411 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9412 if (markers.empty ())
9413 error (_("No known static tracepoint marker named %s"),
9414 marker_str.c_str ());
9415
9416 std::vector<symtab_and_line> sals;
9417 sals.reserve (markers.size ());
9418
9419 for (const static_tracepoint_marker &marker : markers)
9420 {
9421 symtab_and_line sal = find_pc_line (marker.address, 0);
9422 sal.pc = marker.address;
9423 sals.push_back (sal);
9424 }
9425
9426 *arg_p = endp;
9427 return sals;
9428 }
9429
9430 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9431 according to IS_TRACEPOINT. */
9432
9433 static const struct breakpoint_ops *
9434 breakpoint_ops_for_event_location_type (enum event_location_type location_type,
9435 bool is_tracepoint)
9436 {
9437 if (is_tracepoint)
9438 {
9439 if (location_type == PROBE_LOCATION)
9440 return &tracepoint_probe_breakpoint_ops;
9441 else
9442 return &tracepoint_breakpoint_ops;
9443 }
9444 else
9445 {
9446 if (location_type == PROBE_LOCATION)
9447 return &bkpt_probe_breakpoint_ops;
9448 else
9449 return &bkpt_breakpoint_ops;
9450 }
9451 }
9452
9453 /* See breakpoint.h. */
9454
9455 const struct breakpoint_ops *
9456 breakpoint_ops_for_event_location (const struct event_location *location,
9457 bool is_tracepoint)
9458 {
9459 if (location != nullptr)
9460 return breakpoint_ops_for_event_location_type
9461 (event_location_type (location), is_tracepoint);
9462 return is_tracepoint ? &tracepoint_breakpoint_ops : &bkpt_breakpoint_ops;
9463 }
9464
9465 /* See breakpoint.h. */
9466
9467 int
9468 create_breakpoint (struct gdbarch *gdbarch,
9469 struct event_location *location,
9470 const char *cond_string,
9471 int thread, const char *extra_string,
9472 bool force_condition, int parse_extra,
9473 int tempflag, enum bptype type_wanted,
9474 int ignore_count,
9475 enum auto_boolean pending_break_support,
9476 const struct breakpoint_ops *ops,
9477 int from_tty, int enabled, int internal,
9478 unsigned flags)
9479 {
9480 struct linespec_result canonical;
9481 int pending = 0;
9482 int task = 0;
9483 int prev_bkpt_count = breakpoint_count;
9484
9485 gdb_assert (ops != NULL);
9486
9487 /* If extra_string isn't useful, set it to NULL. */
9488 if (extra_string != NULL && *extra_string == '\0')
9489 extra_string = NULL;
9490
9491 try
9492 {
9493 ops->create_sals_from_location (location, &canonical, type_wanted);
9494 }
9495 catch (const gdb_exception_error &e)
9496 {
9497 /* If caller is interested in rc value from parse, set
9498 value. */
9499 if (e.error == NOT_FOUND_ERROR)
9500 {
9501 /* If pending breakpoint support is turned off, throw
9502 error. */
9503
9504 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9505 throw;
9506
9507 exception_print (gdb_stderr, e);
9508
9509 /* If pending breakpoint support is auto query and the user
9510 selects no, then simply return the error code. */
9511 if (pending_break_support == AUTO_BOOLEAN_AUTO
9512 && !nquery (_("Make %s pending on future shared library load? "),
9513 bptype_string (type_wanted)))
9514 return 0;
9515
9516 /* At this point, either the user was queried about setting
9517 a pending breakpoint and selected yes, or pending
9518 breakpoint behavior is on and thus a pending breakpoint
9519 is defaulted on behalf of the user. */
9520 pending = 1;
9521 }
9522 else
9523 throw;
9524 }
9525
9526 if (!pending && canonical.lsals.empty ())
9527 return 0;
9528
9529 /* Resolve all line numbers to PC's and verify that the addresses
9530 are ok for the target. */
9531 if (!pending)
9532 {
9533 for (auto &lsal : canonical.lsals)
9534 breakpoint_sals_to_pc (lsal.sals);
9535 }
9536
9537 /* Fast tracepoints may have additional restrictions on location. */
9538 if (!pending && type_wanted == bp_fast_tracepoint)
9539 {
9540 for (const auto &lsal : canonical.lsals)
9541 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9542 }
9543
9544 /* Verify that condition can be parsed, before setting any
9545 breakpoints. Allocate a separate condition expression for each
9546 breakpoint. */
9547 if (!pending)
9548 {
9549 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9550 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9551
9552 if (parse_extra)
9553 {
9554 char *rest;
9555 char *cond;
9556
9557 const linespec_sals &lsal = canonical.lsals[0];
9558
9559 find_condition_and_thread_for_sals (lsal.sals, extra_string,
9560 &cond, &thread, &task, &rest);
9561 cond_string_copy.reset (cond);
9562 extra_string_copy.reset (rest);
9563 }
9564 else
9565 {
9566 if (type_wanted != bp_dprintf
9567 && extra_string != NULL && *extra_string != '\0')
9568 error (_("Garbage '%s' at end of location"), extra_string);
9569
9570 /* Check the validity of the condition. We should error out
9571 if the condition is invalid at all of the locations and
9572 if it is not forced. In the PARSE_EXTRA case above, this
9573 check is done when parsing the EXTRA_STRING. */
9574 if (cond_string != nullptr && !force_condition)
9575 {
9576 int num_failures = 0;
9577 const linespec_sals &lsal = canonical.lsals[0];
9578 for (const auto &sal : lsal.sals)
9579 {
9580 const char *cond = cond_string;
9581 try
9582 {
9583 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
9584 /* One success is sufficient to keep going. */
9585 break;
9586 }
9587 catch (const gdb_exception_error &)
9588 {
9589 num_failures++;
9590 /* If this is the last sal, error out. */
9591 if (num_failures == lsal.sals.size ())
9592 throw;
9593 }
9594 }
9595 }
9596
9597 /* Create a private copy of condition string. */
9598 if (cond_string)
9599 cond_string_copy.reset (xstrdup (cond_string));
9600 /* Create a private copy of any extra string. */
9601 if (extra_string)
9602 extra_string_copy.reset (xstrdup (extra_string));
9603 }
9604
9605 ops->create_breakpoints_sal (gdbarch, &canonical,
9606 std::move (cond_string_copy),
9607 std::move (extra_string_copy),
9608 type_wanted,
9609 tempflag ? disp_del : disp_donttouch,
9610 thread, task, ignore_count, ops,
9611 from_tty, enabled, internal, flags);
9612 }
9613 else
9614 {
9615 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9616
9617 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9618 b->location = copy_event_location (location);
9619
9620 if (parse_extra)
9621 b->cond_string = NULL;
9622 else
9623 {
9624 /* Create a private copy of condition string. */
9625 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
9626 b->thread = thread;
9627 }
9628
9629 /* Create a private copy of any extra string. */
9630 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
9631 b->ignore_count = ignore_count;
9632 b->disposition = tempflag ? disp_del : disp_donttouch;
9633 b->condition_not_parsed = 1;
9634 b->enable_state = enabled ? bp_enabled : bp_disabled;
9635 if ((type_wanted != bp_breakpoint
9636 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9637 b->pspace = current_program_space;
9638
9639 install_breakpoint (internal, std::move (b), 0);
9640 }
9641
9642 if (canonical.lsals.size () > 1)
9643 {
9644 warning (_("Multiple breakpoints were set.\nUse the "
9645 "\"delete\" command to delete unwanted breakpoints."));
9646 prev_breakpoint_count = prev_bkpt_count;
9647 }
9648
9649 update_global_location_list (UGLL_MAY_INSERT);
9650
9651 return 1;
9652 }
9653
9654 /* Set a breakpoint.
9655 ARG is a string describing breakpoint address,
9656 condition, and thread.
9657 FLAG specifies if a breakpoint is hardware on,
9658 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9659 and BP_TEMPFLAG. */
9660
9661 static void
9662 break_command_1 (const char *arg, int flag, int from_tty)
9663 {
9664 int tempflag = flag & BP_TEMPFLAG;
9665 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9666 ? bp_hardware_breakpoint
9667 : bp_breakpoint);
9668
9669 event_location_up location = string_to_event_location (&arg, current_language);
9670 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9671 (location.get (), false /* is_tracepoint */);
9672
9673 create_breakpoint (get_current_arch (),
9674 location.get (),
9675 NULL, 0, arg, false, 1 /* parse arg */,
9676 tempflag, type_wanted,
9677 0 /* Ignore count */,
9678 pending_break_support,
9679 ops,
9680 from_tty,
9681 1 /* enabled */,
9682 0 /* internal */,
9683 0);
9684 }
9685
9686 /* Helper function for break_command_1 and disassemble_command. */
9687
9688 void
9689 resolve_sal_pc (struct symtab_and_line *sal)
9690 {
9691 CORE_ADDR pc;
9692
9693 if (sal->pc == 0 && sal->symtab != NULL)
9694 {
9695 if (!find_line_pc (sal->symtab, sal->line, &pc))
9696 error (_("No line %d in file \"%s\"."),
9697 sal->line, symtab_to_filename_for_display (sal->symtab));
9698 sal->pc = pc;
9699
9700 /* If this SAL corresponds to a breakpoint inserted using a line
9701 number, then skip the function prologue if necessary. */
9702 if (sal->explicit_line)
9703 skip_prologue_sal (sal);
9704 }
9705
9706 if (sal->section == 0 && sal->symtab != NULL)
9707 {
9708 const struct blockvector *bv;
9709 const struct block *b;
9710 struct symbol *sym;
9711
9712 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9713 SYMTAB_COMPUNIT (sal->symtab));
9714 if (bv != NULL)
9715 {
9716 sym = block_linkage_function (b);
9717 if (sym != NULL)
9718 {
9719 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9720 sal->section = sym->obj_section (SYMTAB_OBJFILE (sal->symtab));
9721 }
9722 else
9723 {
9724 /* It really is worthwhile to have the section, so we'll
9725 just have to look harder. This case can be executed
9726 if we have line numbers but no functions (as can
9727 happen in assembly source). */
9728
9729 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9730 switch_to_program_space_and_thread (sal->pspace);
9731
9732 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9733 if (msym.minsym)
9734 sal->section = msym.obj_section ();
9735 }
9736 }
9737 }
9738 }
9739
9740 void
9741 break_command (const char *arg, int from_tty)
9742 {
9743 break_command_1 (arg, 0, from_tty);
9744 }
9745
9746 void
9747 tbreak_command (const char *arg, int from_tty)
9748 {
9749 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9750 }
9751
9752 static void
9753 hbreak_command (const char *arg, int from_tty)
9754 {
9755 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9756 }
9757
9758 static void
9759 thbreak_command (const char *arg, int from_tty)
9760 {
9761 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9762 }
9763
9764 static void
9765 stop_command (const char *arg, int from_tty)
9766 {
9767 printf_filtered (_("Specify the type of breakpoint to set.\n\
9768 Usage: stop in <function | address>\n\
9769 stop at <line>\n"));
9770 }
9771
9772 static void
9773 stopin_command (const char *arg, int from_tty)
9774 {
9775 int badInput = 0;
9776
9777 if (arg == NULL)
9778 badInput = 1;
9779 else if (*arg != '*')
9780 {
9781 const char *argptr = arg;
9782 int hasColon = 0;
9783
9784 /* Look for a ':'. If this is a line number specification, then
9785 say it is bad, otherwise, it should be an address or
9786 function/method name. */
9787 while (*argptr && !hasColon)
9788 {
9789 hasColon = (*argptr == ':');
9790 argptr++;
9791 }
9792
9793 if (hasColon)
9794 badInput = (*argptr != ':'); /* Not a class::method */
9795 else
9796 badInput = isdigit (*arg); /* a simple line number */
9797 }
9798
9799 if (badInput)
9800 printf_filtered (_("Usage: stop in <function | address>\n"));
9801 else
9802 break_command_1 (arg, 0, from_tty);
9803 }
9804
9805 static void
9806 stopat_command (const char *arg, int from_tty)
9807 {
9808 int badInput = 0;
9809
9810 if (arg == NULL || *arg == '*') /* no line number */
9811 badInput = 1;
9812 else
9813 {
9814 const char *argptr = arg;
9815 int hasColon = 0;
9816
9817 /* Look for a ':'. If there is a '::' then get out, otherwise
9818 it is probably a line number. */
9819 while (*argptr && !hasColon)
9820 {
9821 hasColon = (*argptr == ':');
9822 argptr++;
9823 }
9824
9825 if (hasColon)
9826 badInput = (*argptr == ':'); /* we have class::method */
9827 else
9828 badInput = !isdigit (*arg); /* not a line number */
9829 }
9830
9831 if (badInput)
9832 printf_filtered (_("Usage: stop at LINE\n"));
9833 else
9834 break_command_1 (arg, 0, from_tty);
9835 }
9836
9837 /* The dynamic printf command is mostly like a regular breakpoint, but
9838 with a prewired command list consisting of a single output command,
9839 built from extra arguments supplied on the dprintf command
9840 line. */
9841
9842 static void
9843 dprintf_command (const char *arg, int from_tty)
9844 {
9845 event_location_up location = string_to_event_location (&arg, current_language);
9846
9847 /* If non-NULL, ARG should have been advanced past the location;
9848 the next character must be ','. */
9849 if (arg != NULL)
9850 {
9851 if (arg[0] != ',' || arg[1] == '\0')
9852 error (_("Format string required"));
9853 else
9854 {
9855 /* Skip the comma. */
9856 ++arg;
9857 }
9858 }
9859
9860 create_breakpoint (get_current_arch (),
9861 location.get (),
9862 NULL, 0, arg, false, 1 /* parse arg */,
9863 0, bp_dprintf,
9864 0 /* Ignore count */,
9865 pending_break_support,
9866 &dprintf_breakpoint_ops,
9867 from_tty,
9868 1 /* enabled */,
9869 0 /* internal */,
9870 0);
9871 }
9872
9873 static void
9874 agent_printf_command (const char *arg, int from_tty)
9875 {
9876 error (_("May only run agent-printf on the target"));
9877 }
9878
9879 /* Implement the "breakpoint_hit" breakpoint_ops method for
9880 ranged breakpoints. */
9881
9882 static int
9883 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9884 const address_space *aspace,
9885 CORE_ADDR bp_addr,
9886 const struct target_waitstatus *ws)
9887 {
9888 if (ws->kind != TARGET_WAITKIND_STOPPED
9889 || ws->value.sig != GDB_SIGNAL_TRAP)
9890 return 0;
9891
9892 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9893 bl->length, aspace, bp_addr);
9894 }
9895
9896 /* Implement the "resources_needed" breakpoint_ops method for
9897 ranged breakpoints. */
9898
9899 static int
9900 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9901 {
9902 return target_ranged_break_num_registers ();
9903 }
9904
9905 /* Implement the "print_it" breakpoint_ops method for
9906 ranged breakpoints. */
9907
9908 static enum print_stop_action
9909 print_it_ranged_breakpoint (bpstat bs)
9910 {
9911 struct breakpoint *b = bs->breakpoint_at;
9912 struct bp_location *bl = b->loc;
9913 struct ui_out *uiout = current_uiout;
9914
9915 gdb_assert (b->type == bp_hardware_breakpoint);
9916
9917 /* Ranged breakpoints have only one location. */
9918 gdb_assert (bl && bl->next == NULL);
9919
9920 annotate_breakpoint (b->number);
9921
9922 maybe_print_thread_hit_breakpoint (uiout);
9923
9924 if (b->disposition == disp_del)
9925 uiout->text ("Temporary ranged breakpoint ");
9926 else
9927 uiout->text ("Ranged breakpoint ");
9928 if (uiout->is_mi_like_p ())
9929 {
9930 uiout->field_string ("reason",
9931 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9932 uiout->field_string ("disp", bpdisp_text (b->disposition));
9933 }
9934 uiout->field_signed ("bkptno", b->number);
9935 uiout->text (", ");
9936
9937 return PRINT_SRC_AND_LOC;
9938 }
9939
9940 /* Implement the "print_one" breakpoint_ops method for
9941 ranged breakpoints. */
9942
9943 static void
9944 print_one_ranged_breakpoint (struct breakpoint *b,
9945 struct bp_location **last_loc)
9946 {
9947 struct bp_location *bl = b->loc;
9948 struct value_print_options opts;
9949 struct ui_out *uiout = current_uiout;
9950
9951 /* Ranged breakpoints have only one location. */
9952 gdb_assert (bl && bl->next == NULL);
9953
9954 get_user_print_options (&opts);
9955
9956 if (opts.addressprint)
9957 /* We don't print the address range here, it will be printed later
9958 by print_one_detail_ranged_breakpoint. */
9959 uiout->field_skip ("addr");
9960 annotate_field (5);
9961 print_breakpoint_location (b, bl);
9962 *last_loc = bl;
9963 }
9964
9965 /* Implement the "print_one_detail" breakpoint_ops method for
9966 ranged breakpoints. */
9967
9968 static void
9969 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9970 struct ui_out *uiout)
9971 {
9972 CORE_ADDR address_start, address_end;
9973 struct bp_location *bl = b->loc;
9974 string_file stb;
9975
9976 gdb_assert (bl);
9977
9978 address_start = bl->address;
9979 address_end = address_start + bl->length - 1;
9980
9981 uiout->text ("\taddress range: ");
9982 stb.printf ("[%s, %s]",
9983 print_core_address (bl->gdbarch, address_start),
9984 print_core_address (bl->gdbarch, address_end));
9985 uiout->field_stream ("addr", stb);
9986 uiout->text ("\n");
9987 }
9988
9989 /* Implement the "print_mention" breakpoint_ops method for
9990 ranged breakpoints. */
9991
9992 static void
9993 print_mention_ranged_breakpoint (struct breakpoint *b)
9994 {
9995 struct bp_location *bl = b->loc;
9996 struct ui_out *uiout = current_uiout;
9997
9998 gdb_assert (bl);
9999 gdb_assert (b->type == bp_hardware_breakpoint);
10000
10001 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10002 b->number, paddress (bl->gdbarch, bl->address),
10003 paddress (bl->gdbarch, bl->address + bl->length - 1));
10004 }
10005
10006 /* Implement the "print_recreate" breakpoint_ops method for
10007 ranged breakpoints. */
10008
10009 static void
10010 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10011 {
10012 fprintf_unfiltered (fp, "break-range %s, %s",
10013 event_location_to_string (b->location.get ()),
10014 event_location_to_string (b->location_range_end.get ()));
10015 print_recreate_thread (b, fp);
10016 }
10017
10018 /* The breakpoint_ops structure to be used in ranged breakpoints. */
10019
10020 static struct breakpoint_ops ranged_breakpoint_ops;
10021
10022 /* Find the address where the end of the breakpoint range should be
10023 placed, given the SAL of the end of the range. This is so that if
10024 the user provides a line number, the end of the range is set to the
10025 last instruction of the given line. */
10026
10027 static CORE_ADDR
10028 find_breakpoint_range_end (struct symtab_and_line sal)
10029 {
10030 CORE_ADDR end;
10031
10032 /* If the user provided a PC value, use it. Otherwise,
10033 find the address of the end of the given location. */
10034 if (sal.explicit_pc)
10035 end = sal.pc;
10036 else
10037 {
10038 int ret;
10039 CORE_ADDR start;
10040
10041 ret = find_line_pc_range (sal, &start, &end);
10042 if (!ret)
10043 error (_("Could not find location of the end of the range."));
10044
10045 /* find_line_pc_range returns the start of the next line. */
10046 end--;
10047 }
10048
10049 return end;
10050 }
10051
10052 /* Implement the "break-range" CLI command. */
10053
10054 static void
10055 break_range_command (const char *arg, int from_tty)
10056 {
10057 const char *arg_start;
10058 struct linespec_result canonical_start, canonical_end;
10059 int bp_count, can_use_bp, length;
10060 CORE_ADDR end;
10061 struct breakpoint *b;
10062
10063 /* We don't support software ranged breakpoints. */
10064 if (target_ranged_break_num_registers () < 0)
10065 error (_("This target does not support hardware ranged breakpoints."));
10066
10067 bp_count = hw_breakpoint_used_count ();
10068 bp_count += target_ranged_break_num_registers ();
10069 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10070 bp_count, 0);
10071 if (can_use_bp < 0)
10072 error (_("Hardware breakpoints used exceeds limit."));
10073
10074 arg = skip_spaces (arg);
10075 if (arg == NULL || arg[0] == '\0')
10076 error(_("No address range specified."));
10077
10078 arg_start = arg;
10079 event_location_up start_location = string_to_event_location (&arg,
10080 current_language);
10081 parse_breakpoint_sals (start_location.get (), &canonical_start);
10082
10083 if (arg[0] != ',')
10084 error (_("Too few arguments."));
10085 else if (canonical_start.lsals.empty ())
10086 error (_("Could not find location of the beginning of the range."));
10087
10088 const linespec_sals &lsal_start = canonical_start.lsals[0];
10089
10090 if (canonical_start.lsals.size () > 1
10091 || lsal_start.sals.size () != 1)
10092 error (_("Cannot create a ranged breakpoint with multiple locations."));
10093
10094 const symtab_and_line &sal_start = lsal_start.sals[0];
10095 std::string addr_string_start (arg_start, arg - arg_start);
10096
10097 arg++; /* Skip the comma. */
10098 arg = skip_spaces (arg);
10099
10100 /* Parse the end location. */
10101
10102 arg_start = arg;
10103
10104 /* We call decode_line_full directly here instead of using
10105 parse_breakpoint_sals because we need to specify the start location's
10106 symtab and line as the default symtab and line for the end of the
10107 range. This makes it possible to have ranges like "foo.c:27, +14",
10108 where +14 means 14 lines from the start location. */
10109 event_location_up end_location = string_to_event_location (&arg,
10110 current_language);
10111 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10112 sal_start.symtab, sal_start.line,
10113 &canonical_end, NULL, NULL);
10114
10115 if (canonical_end.lsals.empty ())
10116 error (_("Could not find location of the end of the range."));
10117
10118 const linespec_sals &lsal_end = canonical_end.lsals[0];
10119 if (canonical_end.lsals.size () > 1
10120 || lsal_end.sals.size () != 1)
10121 error (_("Cannot create a ranged breakpoint with multiple locations."));
10122
10123 const symtab_and_line &sal_end = lsal_end.sals[0];
10124
10125 end = find_breakpoint_range_end (sal_end);
10126 if (sal_start.pc > end)
10127 error (_("Invalid address range, end precedes start."));
10128
10129 length = end - sal_start.pc + 1;
10130 if (length < 0)
10131 /* Length overflowed. */
10132 error (_("Address range too large."));
10133 else if (length == 1)
10134 {
10135 /* This range is simple enough to be handled by
10136 the `hbreak' command. */
10137 hbreak_command (&addr_string_start[0], 1);
10138
10139 return;
10140 }
10141
10142 /* Now set up the breakpoint. */
10143 b = set_raw_breakpoint (get_current_arch (), sal_start,
10144 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10145 set_breakpoint_count (breakpoint_count + 1);
10146 b->number = breakpoint_count;
10147 b->disposition = disp_donttouch;
10148 b->location = std::move (start_location);
10149 b->location_range_end = std::move (end_location);
10150 b->loc->length = length;
10151
10152 mention (b);
10153 gdb::observers::breakpoint_created.notify (b);
10154 update_global_location_list (UGLL_MAY_INSERT);
10155 }
10156
10157 /* Return non-zero if EXP is verified as constant. Returned zero
10158 means EXP is variable. Also the constant detection may fail for
10159 some constant expressions and in such case still falsely return
10160 zero. */
10161
10162 static bool
10163 watchpoint_exp_is_const (const struct expression *exp)
10164 {
10165 return exp->op->constant_p ();
10166 }
10167
10168 /* Watchpoint destructor. */
10169
10170 watchpoint::~watchpoint ()
10171 {
10172 xfree (this->exp_string);
10173 xfree (this->exp_string_reparse);
10174 }
10175
10176 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10177
10178 static void
10179 re_set_watchpoint (struct breakpoint *b)
10180 {
10181 struct watchpoint *w = (struct watchpoint *) b;
10182
10183 /* Watchpoint can be either on expression using entirely global
10184 variables, or it can be on local variables.
10185
10186 Watchpoints of the first kind are never auto-deleted, and even
10187 persist across program restarts. Since they can use variables
10188 from shared libraries, we need to reparse expression as libraries
10189 are loaded and unloaded.
10190
10191 Watchpoints on local variables can also change meaning as result
10192 of solib event. For example, if a watchpoint uses both a local
10193 and a global variables in expression, it's a local watchpoint,
10194 but unloading of a shared library will make the expression
10195 invalid. This is not a very common use case, but we still
10196 re-evaluate expression, to avoid surprises to the user.
10197
10198 Note that for local watchpoints, we re-evaluate it only if
10199 watchpoints frame id is still valid. If it's not, it means the
10200 watchpoint is out of scope and will be deleted soon. In fact,
10201 I'm not sure we'll ever be called in this case.
10202
10203 If a local watchpoint's frame id is still valid, then
10204 w->exp_valid_block is likewise valid, and we can safely use it.
10205
10206 Don't do anything about disabled watchpoints, since they will be
10207 reevaluated again when enabled. */
10208 update_watchpoint (w, 1 /* reparse */);
10209 }
10210
10211 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10212
10213 static int
10214 insert_watchpoint (struct bp_location *bl)
10215 {
10216 struct watchpoint *w = (struct watchpoint *) bl->owner;
10217 int length = w->exact ? 1 : bl->length;
10218
10219 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10220 w->cond_exp.get ());
10221 }
10222
10223 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10224
10225 static int
10226 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10227 {
10228 struct watchpoint *w = (struct watchpoint *) bl->owner;
10229 int length = w->exact ? 1 : bl->length;
10230
10231 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10232 w->cond_exp.get ());
10233 }
10234
10235 static int
10236 breakpoint_hit_watchpoint (const struct bp_location *bl,
10237 const address_space *aspace, CORE_ADDR bp_addr,
10238 const struct target_waitstatus *ws)
10239 {
10240 struct breakpoint *b = bl->owner;
10241 struct watchpoint *w = (struct watchpoint *) b;
10242
10243 /* Continuable hardware watchpoints are treated as non-existent if the
10244 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10245 some data address). Otherwise gdb won't stop on a break instruction
10246 in the code (not from a breakpoint) when a hardware watchpoint has
10247 been defined. Also skip watchpoints which we know did not trigger
10248 (did not match the data address). */
10249 if (is_hardware_watchpoint (b)
10250 && w->watchpoint_triggered == watch_triggered_no)
10251 return 0;
10252
10253 return 1;
10254 }
10255
10256 static void
10257 check_status_watchpoint (bpstat bs)
10258 {
10259 gdb_assert (is_watchpoint (bs->breakpoint_at));
10260
10261 bpstat_check_watchpoint (bs);
10262 }
10263
10264 /* Implement the "resources_needed" breakpoint_ops method for
10265 hardware watchpoints. */
10266
10267 static int
10268 resources_needed_watchpoint (const struct bp_location *bl)
10269 {
10270 struct watchpoint *w = (struct watchpoint *) bl->owner;
10271 int length = w->exact? 1 : bl->length;
10272
10273 return target_region_ok_for_hw_watchpoint (bl->address, length);
10274 }
10275
10276 /* Implement the "works_in_software_mode" breakpoint_ops method for
10277 hardware watchpoints. */
10278
10279 static int
10280 works_in_software_mode_watchpoint (const struct breakpoint *b)
10281 {
10282 /* Read and access watchpoints only work with hardware support. */
10283 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10284 }
10285
10286 static enum print_stop_action
10287 print_it_watchpoint (bpstat bs)
10288 {
10289 struct breakpoint *b;
10290 enum print_stop_action result;
10291 struct watchpoint *w;
10292 struct ui_out *uiout = current_uiout;
10293
10294 gdb_assert (bs->bp_location_at != NULL);
10295
10296 b = bs->breakpoint_at;
10297 w = (struct watchpoint *) b;
10298
10299 annotate_watchpoint (b->number);
10300 maybe_print_thread_hit_breakpoint (uiout);
10301
10302 string_file stb;
10303
10304 gdb::optional<ui_out_emit_tuple> tuple_emitter;
10305 switch (b->type)
10306 {
10307 case bp_watchpoint:
10308 case bp_hardware_watchpoint:
10309 if (uiout->is_mi_like_p ())
10310 uiout->field_string
10311 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10312 mention (b);
10313 tuple_emitter.emplace (uiout, "value");
10314 uiout->text ("\nOld value = ");
10315 watchpoint_value_print (bs->old_val.get (), &stb);
10316 uiout->field_stream ("old", stb);
10317 uiout->text ("\nNew value = ");
10318 watchpoint_value_print (w->val.get (), &stb);
10319 uiout->field_stream ("new", stb);
10320 uiout->text ("\n");
10321 /* More than one watchpoint may have been triggered. */
10322 result = PRINT_UNKNOWN;
10323 break;
10324
10325 case bp_read_watchpoint:
10326 if (uiout->is_mi_like_p ())
10327 uiout->field_string
10328 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10329 mention (b);
10330 tuple_emitter.emplace (uiout, "value");
10331 uiout->text ("\nValue = ");
10332 watchpoint_value_print (w->val.get (), &stb);
10333 uiout->field_stream ("value", stb);
10334 uiout->text ("\n");
10335 result = PRINT_UNKNOWN;
10336 break;
10337
10338 case bp_access_watchpoint:
10339 if (bs->old_val != NULL)
10340 {
10341 if (uiout->is_mi_like_p ())
10342 uiout->field_string
10343 ("reason",
10344 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10345 mention (b);
10346 tuple_emitter.emplace (uiout, "value");
10347 uiout->text ("\nOld value = ");
10348 watchpoint_value_print (bs->old_val.get (), &stb);
10349 uiout->field_stream ("old", stb);
10350 uiout->text ("\nNew value = ");
10351 }
10352 else
10353 {
10354 mention (b);
10355 if (uiout->is_mi_like_p ())
10356 uiout->field_string
10357 ("reason",
10358 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10359 tuple_emitter.emplace (uiout, "value");
10360 uiout->text ("\nValue = ");
10361 }
10362 watchpoint_value_print (w->val.get (), &stb);
10363 uiout->field_stream ("new", stb);
10364 uiout->text ("\n");
10365 result = PRINT_UNKNOWN;
10366 break;
10367 default:
10368 result = PRINT_UNKNOWN;
10369 }
10370
10371 return result;
10372 }
10373
10374 /* Implement the "print_mention" breakpoint_ops method for hardware
10375 watchpoints. */
10376
10377 static void
10378 print_mention_watchpoint (struct breakpoint *b)
10379 {
10380 struct watchpoint *w = (struct watchpoint *) b;
10381 struct ui_out *uiout = current_uiout;
10382 const char *tuple_name;
10383
10384 switch (b->type)
10385 {
10386 case bp_watchpoint:
10387 uiout->text ("Watchpoint ");
10388 tuple_name = "wpt";
10389 break;
10390 case bp_hardware_watchpoint:
10391 uiout->text ("Hardware watchpoint ");
10392 tuple_name = "wpt";
10393 break;
10394 case bp_read_watchpoint:
10395 uiout->text ("Hardware read watchpoint ");
10396 tuple_name = "hw-rwpt";
10397 break;
10398 case bp_access_watchpoint:
10399 uiout->text ("Hardware access (read/write) watchpoint ");
10400 tuple_name = "hw-awpt";
10401 break;
10402 default:
10403 internal_error (__FILE__, __LINE__,
10404 _("Invalid hardware watchpoint type."));
10405 }
10406
10407 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10408 uiout->field_signed ("number", b->number);
10409 uiout->text (": ");
10410 uiout->field_string ("exp", w->exp_string);
10411 }
10412
10413 /* Implement the "print_recreate" breakpoint_ops method for
10414 watchpoints. */
10415
10416 static void
10417 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10418 {
10419 struct watchpoint *w = (struct watchpoint *) b;
10420
10421 switch (b->type)
10422 {
10423 case bp_watchpoint:
10424 case bp_hardware_watchpoint:
10425 fprintf_unfiltered (fp, "watch");
10426 break;
10427 case bp_read_watchpoint:
10428 fprintf_unfiltered (fp, "rwatch");
10429 break;
10430 case bp_access_watchpoint:
10431 fprintf_unfiltered (fp, "awatch");
10432 break;
10433 default:
10434 internal_error (__FILE__, __LINE__,
10435 _("Invalid watchpoint type."));
10436 }
10437
10438 fprintf_unfiltered (fp, " %s", w->exp_string);
10439 print_recreate_thread (b, fp);
10440 }
10441
10442 /* Implement the "explains_signal" breakpoint_ops method for
10443 watchpoints. */
10444
10445 static int
10446 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10447 {
10448 /* A software watchpoint cannot cause a signal other than
10449 GDB_SIGNAL_TRAP. */
10450 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10451 return 0;
10452
10453 return 1;
10454 }
10455
10456 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10457
10458 static struct breakpoint_ops watchpoint_breakpoint_ops;
10459
10460 /* Implement the "insert" breakpoint_ops method for
10461 masked hardware watchpoints. */
10462
10463 static int
10464 insert_masked_watchpoint (struct bp_location *bl)
10465 {
10466 struct watchpoint *w = (struct watchpoint *) bl->owner;
10467
10468 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10469 bl->watchpoint_type);
10470 }
10471
10472 /* Implement the "remove" breakpoint_ops method for
10473 masked hardware watchpoints. */
10474
10475 static int
10476 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10477 {
10478 struct watchpoint *w = (struct watchpoint *) bl->owner;
10479
10480 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10481 bl->watchpoint_type);
10482 }
10483
10484 /* Implement the "resources_needed" breakpoint_ops method for
10485 masked hardware watchpoints. */
10486
10487 static int
10488 resources_needed_masked_watchpoint (const struct bp_location *bl)
10489 {
10490 struct watchpoint *w = (struct watchpoint *) bl->owner;
10491
10492 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10493 }
10494
10495 /* Implement the "works_in_software_mode" breakpoint_ops method for
10496 masked hardware watchpoints. */
10497
10498 static int
10499 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10500 {
10501 return 0;
10502 }
10503
10504 /* Implement the "print_it" breakpoint_ops method for
10505 masked hardware watchpoints. */
10506
10507 static enum print_stop_action
10508 print_it_masked_watchpoint (bpstat bs)
10509 {
10510 struct breakpoint *b = bs->breakpoint_at;
10511 struct ui_out *uiout = current_uiout;
10512
10513 /* Masked watchpoints have only one location. */
10514 gdb_assert (b->loc && b->loc->next == NULL);
10515
10516 annotate_watchpoint (b->number);
10517 maybe_print_thread_hit_breakpoint (uiout);
10518
10519 switch (b->type)
10520 {
10521 case bp_hardware_watchpoint:
10522 if (uiout->is_mi_like_p ())
10523 uiout->field_string
10524 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10525 break;
10526
10527 case bp_read_watchpoint:
10528 if (uiout->is_mi_like_p ())
10529 uiout->field_string
10530 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10531 break;
10532
10533 case bp_access_watchpoint:
10534 if (uiout->is_mi_like_p ())
10535 uiout->field_string
10536 ("reason",
10537 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10538 break;
10539 default:
10540 internal_error (__FILE__, __LINE__,
10541 _("Invalid hardware watchpoint type."));
10542 }
10543
10544 mention (b);
10545 uiout->text (_("\n\
10546 Check the underlying instruction at PC for the memory\n\
10547 address and value which triggered this watchpoint.\n"));
10548 uiout->text ("\n");
10549
10550 /* More than one watchpoint may have been triggered. */
10551 return PRINT_UNKNOWN;
10552 }
10553
10554 /* Implement the "print_one_detail" breakpoint_ops method for
10555 masked hardware watchpoints. */
10556
10557 static void
10558 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10559 struct ui_out *uiout)
10560 {
10561 struct watchpoint *w = (struct watchpoint *) b;
10562
10563 /* Masked watchpoints have only one location. */
10564 gdb_assert (b->loc && b->loc->next == NULL);
10565
10566 uiout->text ("\tmask ");
10567 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10568 uiout->text ("\n");
10569 }
10570
10571 /* Implement the "print_mention" breakpoint_ops method for
10572 masked hardware watchpoints. */
10573
10574 static void
10575 print_mention_masked_watchpoint (struct breakpoint *b)
10576 {
10577 struct watchpoint *w = (struct watchpoint *) b;
10578 struct ui_out *uiout = current_uiout;
10579 const char *tuple_name;
10580
10581 switch (b->type)
10582 {
10583 case bp_hardware_watchpoint:
10584 uiout->text ("Masked hardware watchpoint ");
10585 tuple_name = "wpt";
10586 break;
10587 case bp_read_watchpoint:
10588 uiout->text ("Masked hardware read watchpoint ");
10589 tuple_name = "hw-rwpt";
10590 break;
10591 case bp_access_watchpoint:
10592 uiout->text ("Masked hardware access (read/write) watchpoint ");
10593 tuple_name = "hw-awpt";
10594 break;
10595 default:
10596 internal_error (__FILE__, __LINE__,
10597 _("Invalid hardware watchpoint type."));
10598 }
10599
10600 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10601 uiout->field_signed ("number", b->number);
10602 uiout->text (": ");
10603 uiout->field_string ("exp", w->exp_string);
10604 }
10605
10606 /* Implement the "print_recreate" breakpoint_ops method for
10607 masked hardware watchpoints. */
10608
10609 static void
10610 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10611 {
10612 struct watchpoint *w = (struct watchpoint *) b;
10613
10614 switch (b->type)
10615 {
10616 case bp_hardware_watchpoint:
10617 fprintf_unfiltered (fp, "watch");
10618 break;
10619 case bp_read_watchpoint:
10620 fprintf_unfiltered (fp, "rwatch");
10621 break;
10622 case bp_access_watchpoint:
10623 fprintf_unfiltered (fp, "awatch");
10624 break;
10625 default:
10626 internal_error (__FILE__, __LINE__,
10627 _("Invalid hardware watchpoint type."));
10628 }
10629
10630 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string,
10631 phex (w->hw_wp_mask, sizeof (CORE_ADDR)));
10632 print_recreate_thread (b, fp);
10633 }
10634
10635 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10636
10637 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10638
10639 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10640
10641 static bool
10642 is_masked_watchpoint (const struct breakpoint *b)
10643 {
10644 return b->ops == &masked_watchpoint_breakpoint_ops;
10645 }
10646
10647 /* accessflag: hw_write: watch write,
10648 hw_read: watch read,
10649 hw_access: watch access (read or write) */
10650 static void
10651 watch_command_1 (const char *arg, int accessflag, int from_tty,
10652 bool just_location, bool internal)
10653 {
10654 struct breakpoint *scope_breakpoint = NULL;
10655 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10656 struct value *result;
10657 int saved_bitpos = 0, saved_bitsize = 0;
10658 const char *exp_start = NULL;
10659 const char *exp_end = NULL;
10660 const char *tok, *end_tok;
10661 int toklen = -1;
10662 const char *cond_start = NULL;
10663 const char *cond_end = NULL;
10664 enum bptype bp_type;
10665 int thread = -1;
10666 /* Flag to indicate whether we are going to use masks for
10667 the hardware watchpoint. */
10668 bool use_mask = false;
10669 CORE_ADDR mask = 0;
10670
10671 /* Make sure that we actually have parameters to parse. */
10672 if (arg != NULL && arg[0] != '\0')
10673 {
10674 const char *value_start;
10675
10676 exp_end = arg + strlen (arg);
10677
10678 /* Look for "parameter value" pairs at the end
10679 of the arguments string. */
10680 for (tok = exp_end - 1; tok > arg; tok--)
10681 {
10682 /* Skip whitespace at the end of the argument list. */
10683 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10684 tok--;
10685
10686 /* Find the beginning of the last token.
10687 This is the value of the parameter. */
10688 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10689 tok--;
10690 value_start = tok + 1;
10691
10692 /* Skip whitespace. */
10693 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10694 tok--;
10695
10696 end_tok = tok;
10697
10698 /* Find the beginning of the second to last token.
10699 This is the parameter itself. */
10700 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10701 tok--;
10702 tok++;
10703 toklen = end_tok - tok + 1;
10704
10705 if (toklen == 6 && startswith (tok, "thread"))
10706 {
10707 struct thread_info *thr;
10708 /* At this point we've found a "thread" token, which means
10709 the user is trying to set a watchpoint that triggers
10710 only in a specific thread. */
10711 const char *endp;
10712
10713 if (thread != -1)
10714 error(_("You can specify only one thread."));
10715
10716 /* Extract the thread ID from the next token. */
10717 thr = parse_thread_id (value_start, &endp);
10718
10719 /* Check if the user provided a valid thread ID. */
10720 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10721 invalid_thread_id_error (value_start);
10722
10723 thread = thr->global_num;
10724 }
10725 else if (toklen == 4 && startswith (tok, "mask"))
10726 {
10727 /* We've found a "mask" token, which means the user wants to
10728 create a hardware watchpoint that is going to have the mask
10729 facility. */
10730 struct value *mask_value, *mark;
10731
10732 if (use_mask)
10733 error(_("You can specify only one mask."));
10734
10735 use_mask = just_location = true;
10736
10737 mark = value_mark ();
10738 mask_value = parse_to_comma_and_eval (&value_start);
10739 mask = value_as_address (mask_value);
10740 value_free_to_mark (mark);
10741 }
10742 else
10743 /* We didn't recognize what we found. We should stop here. */
10744 break;
10745
10746 /* Truncate the string and get rid of the "parameter value" pair before
10747 the arguments string is parsed by the parse_exp_1 function. */
10748 exp_end = tok;
10749 }
10750 }
10751 else
10752 exp_end = arg;
10753
10754 /* Parse the rest of the arguments. From here on out, everything
10755 is in terms of a newly allocated string instead of the original
10756 ARG. */
10757 std::string expression (arg, exp_end - arg);
10758 exp_start = arg = expression.c_str ();
10759 innermost_block_tracker tracker;
10760 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10761 exp_end = arg;
10762 /* Remove trailing whitespace from the expression before saving it.
10763 This makes the eventual display of the expression string a bit
10764 prettier. */
10765 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10766 --exp_end;
10767
10768 /* Checking if the expression is not constant. */
10769 if (watchpoint_exp_is_const (exp.get ()))
10770 {
10771 int len;
10772
10773 len = exp_end - exp_start;
10774 while (len > 0 && isspace (exp_start[len - 1]))
10775 len--;
10776 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10777 }
10778
10779 exp_valid_block = tracker.block ();
10780 struct value *mark = value_mark ();
10781 struct value *val_as_value = nullptr;
10782 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10783 just_location);
10784
10785 if (val_as_value != NULL && just_location)
10786 {
10787 saved_bitpos = value_bitpos (val_as_value);
10788 saved_bitsize = value_bitsize (val_as_value);
10789 }
10790
10791 value_ref_ptr val;
10792 if (just_location)
10793 {
10794 int ret;
10795
10796 exp_valid_block = NULL;
10797 val = release_value (value_addr (result));
10798 value_free_to_mark (mark);
10799
10800 if (use_mask)
10801 {
10802 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10803 mask);
10804 if (ret == -1)
10805 error (_("This target does not support masked watchpoints."));
10806 else if (ret == -2)
10807 error (_("Invalid mask or memory region."));
10808 }
10809 }
10810 else if (val_as_value != NULL)
10811 val = release_value (val_as_value);
10812
10813 tok = skip_spaces (arg);
10814 end_tok = skip_to_space (tok);
10815
10816 toklen = end_tok - tok;
10817 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10818 {
10819 tok = cond_start = end_tok + 1;
10820 innermost_block_tracker if_tracker;
10821 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10822
10823 /* The watchpoint expression may not be local, but the condition
10824 may still be. E.g.: `watch global if local > 0'. */
10825 cond_exp_valid_block = if_tracker.block ();
10826
10827 cond_end = tok;
10828 }
10829 if (*tok)
10830 error (_("Junk at end of command."));
10831
10832 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10833
10834 /* Save this because create_internal_breakpoint below invalidates
10835 'wp_frame'. */
10836 frame_id watchpoint_frame = get_frame_id (wp_frame);
10837
10838 /* If the expression is "local", then set up a "watchpoint scope"
10839 breakpoint at the point where we've left the scope of the watchpoint
10840 expression. Create the scope breakpoint before the watchpoint, so
10841 that we will encounter it first in bpstat_stop_status. */
10842 if (exp_valid_block != NULL && wp_frame != NULL)
10843 {
10844 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10845
10846 if (frame_id_p (caller_frame_id))
10847 {
10848 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10849 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10850
10851 scope_breakpoint
10852 = create_internal_breakpoint (caller_arch, caller_pc,
10853 bp_watchpoint_scope,
10854 &momentary_breakpoint_ops);
10855
10856 /* create_internal_breakpoint could invalidate WP_FRAME. */
10857 wp_frame = NULL;
10858
10859 scope_breakpoint->enable_state = bp_enabled;
10860
10861 /* Automatically delete the breakpoint when it hits. */
10862 scope_breakpoint->disposition = disp_del;
10863
10864 /* Only break in the proper frame (help with recursion). */
10865 scope_breakpoint->frame_id = caller_frame_id;
10866
10867 /* Set the address at which we will stop. */
10868 scope_breakpoint->loc->gdbarch = caller_arch;
10869 scope_breakpoint->loc->requested_address = caller_pc;
10870 scope_breakpoint->loc->address
10871 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10872 scope_breakpoint->loc->requested_address,
10873 scope_breakpoint->type);
10874 }
10875 }
10876
10877 /* Now set up the breakpoint. We create all watchpoints as hardware
10878 watchpoints here even if hardware watchpoints are turned off, a call
10879 to update_watchpoint later in this function will cause the type to
10880 drop back to bp_watchpoint (software watchpoint) if required. */
10881
10882 if (accessflag == hw_read)
10883 bp_type = bp_read_watchpoint;
10884 else if (accessflag == hw_access)
10885 bp_type = bp_access_watchpoint;
10886 else
10887 bp_type = bp_hardware_watchpoint;
10888
10889 std::unique_ptr<watchpoint> w (new watchpoint ());
10890
10891 if (use_mask)
10892 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10893 &masked_watchpoint_breakpoint_ops);
10894 else
10895 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10896 &watchpoint_breakpoint_ops);
10897 w->thread = thread;
10898 w->disposition = disp_donttouch;
10899 w->pspace = current_program_space;
10900 w->exp = std::move (exp);
10901 w->exp_valid_block = exp_valid_block;
10902 w->cond_exp_valid_block = cond_exp_valid_block;
10903 if (just_location)
10904 {
10905 struct type *t = value_type (val.get ());
10906 CORE_ADDR addr = value_as_address (val.get ());
10907
10908 w->exp_string_reparse
10909 = current_language->watch_location_expression (t, addr).release ();
10910
10911 w->exp_string = xstrprintf ("-location %.*s",
10912 (int) (exp_end - exp_start), exp_start);
10913 }
10914 else
10915 w->exp_string = savestring (exp_start, exp_end - exp_start);
10916
10917 if (use_mask)
10918 {
10919 w->hw_wp_mask = mask;
10920 }
10921 else
10922 {
10923 w->val = val;
10924 w->val_bitpos = saved_bitpos;
10925 w->val_bitsize = saved_bitsize;
10926 w->val_valid = true;
10927 }
10928
10929 if (cond_start)
10930 w->cond_string = savestring (cond_start, cond_end - cond_start);
10931 else
10932 w->cond_string = 0;
10933
10934 if (frame_id_p (watchpoint_frame))
10935 {
10936 w->watchpoint_frame = watchpoint_frame;
10937 w->watchpoint_thread = inferior_ptid;
10938 }
10939 else
10940 {
10941 w->watchpoint_frame = null_frame_id;
10942 w->watchpoint_thread = null_ptid;
10943 }
10944
10945 if (scope_breakpoint != NULL)
10946 {
10947 /* The scope breakpoint is related to the watchpoint. We will
10948 need to act on them together. */
10949 w->related_breakpoint = scope_breakpoint;
10950 scope_breakpoint->related_breakpoint = w.get ();
10951 }
10952
10953 if (!just_location)
10954 value_free_to_mark (mark);
10955
10956 /* Finally update the new watchpoint. This creates the locations
10957 that should be inserted. */
10958 update_watchpoint (w.get (), 1);
10959
10960 install_breakpoint (internal, std::move (w), 1);
10961 }
10962
10963 /* Return count of debug registers needed to watch the given expression.
10964 If the watchpoint cannot be handled in hardware return zero. */
10965
10966 static int
10967 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10968 {
10969 int found_memory_cnt = 0;
10970
10971 /* Did the user specifically forbid us to use hardware watchpoints? */
10972 if (!can_use_hw_watchpoints)
10973 return 0;
10974
10975 gdb_assert (!vals.empty ());
10976 struct value *head = vals[0].get ();
10977
10978 /* Make sure that the value of the expression depends only upon
10979 memory contents, and values computed from them within GDB. If we
10980 find any register references or function calls, we can't use a
10981 hardware watchpoint.
10982
10983 The idea here is that evaluating an expression generates a series
10984 of values, one holding the value of every subexpression. (The
10985 expression a*b+c has five subexpressions: a, b, a*b, c, and
10986 a*b+c.) GDB's values hold almost enough information to establish
10987 the criteria given above --- they identify memory lvalues,
10988 register lvalues, computed values, etcetera. So we can evaluate
10989 the expression, and then scan the chain of values that leaves
10990 behind to decide whether we can detect any possible change to the
10991 expression's final value using only hardware watchpoints.
10992
10993 However, I don't think that the values returned by inferior
10994 function calls are special in any way. So this function may not
10995 notice that an expression involving an inferior function call
10996 can't be watched with hardware watchpoints. FIXME. */
10997 for (const value_ref_ptr &iter : vals)
10998 {
10999 struct value *v = iter.get ();
11000
11001 if (VALUE_LVAL (v) == lval_memory)
11002 {
11003 if (v != head && value_lazy (v))
11004 /* A lazy memory lvalue in the chain is one that GDB never
11005 needed to fetch; we either just used its address (e.g.,
11006 `a' in `a.b') or we never needed it at all (e.g., `a'
11007 in `a,b'). This doesn't apply to HEAD; if that is
11008 lazy then it was not readable, but watch it anyway. */
11009 ;
11010 else
11011 {
11012 /* Ahh, memory we actually used! Check if we can cover
11013 it with hardware watchpoints. */
11014 struct type *vtype = check_typedef (value_type (v));
11015
11016 /* We only watch structs and arrays if user asked for it
11017 explicitly, never if they just happen to appear in a
11018 middle of some value chain. */
11019 if (v == head
11020 || (vtype->code () != TYPE_CODE_STRUCT
11021 && vtype->code () != TYPE_CODE_ARRAY))
11022 {
11023 CORE_ADDR vaddr = value_address (v);
11024 int len;
11025 int num_regs;
11026
11027 len = (target_exact_watchpoints
11028 && is_scalar_type_recursive (vtype))?
11029 1 : TYPE_LENGTH (value_type (v));
11030
11031 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11032 if (!num_regs)
11033 return 0;
11034 else
11035 found_memory_cnt += num_regs;
11036 }
11037 }
11038 }
11039 else if (VALUE_LVAL (v) != not_lval
11040 && deprecated_value_modifiable (v) == 0)
11041 return 0; /* These are values from the history (e.g., $1). */
11042 else if (VALUE_LVAL (v) == lval_register)
11043 return 0; /* Cannot watch a register with a HW watchpoint. */
11044 }
11045
11046 /* The expression itself looks suitable for using a hardware
11047 watchpoint, but give the target machine a chance to reject it. */
11048 return found_memory_cnt;
11049 }
11050
11051 void
11052 watch_command_wrapper (const char *arg, int from_tty, bool internal)
11053 {
11054 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11055 }
11056
11057 /* Options for the watch, awatch, and rwatch commands. */
11058
11059 struct watch_options
11060 {
11061 /* For -location. */
11062 bool location = false;
11063 };
11064
11065 /* Definitions of options for the "watch", "awatch", and "rwatch" commands.
11066
11067 Historically GDB always accepted both '-location' and '-l' flags for
11068 these commands (both flags being synonyms). When converting to the
11069 newer option scheme only '-location' is added here. That's fine (for
11070 backward compatibility) as any non-ambiguous prefix of a flag will be
11071 accepted, so '-l', '-loc', are now all accepted.
11072
11073 What this means is that, if in the future, we add any new flag here
11074 that starts with '-l' then this will break backward compatibility, so
11075 please, don't do that! */
11076
11077 static const gdb::option::option_def watch_option_defs[] = {
11078 gdb::option::flag_option_def<watch_options> {
11079 "location",
11080 [] (watch_options *opt) { return &opt->location; },
11081 N_("\
11082 This evaluates EXPRESSION and watches the memory to which is refers.\n\
11083 -l can be used as a short form of -location."),
11084 },
11085 };
11086
11087 /* Returns the option group used by 'watch', 'awatch', and 'rwatch'
11088 commands. */
11089
11090 static gdb::option::option_def_group
11091 make_watch_options_def_group (watch_options *opts)
11092 {
11093 return {{watch_option_defs}, opts};
11094 }
11095
11096 /* A helper function that looks for the "-location" argument and then
11097 calls watch_command_1. */
11098
11099 static void
11100 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
11101 {
11102 watch_options opts;
11103 auto grp = make_watch_options_def_group (&opts);
11104 gdb::option::process_options
11105 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
11106 if (arg != nullptr && *arg == '\0')
11107 arg = nullptr;
11108
11109 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
11110 }
11111
11112 /* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
11113 static void
11114 watch_command_completer (struct cmd_list_element *ignore,
11115 completion_tracker &tracker,
11116 const char *text, const char * /*word*/)
11117 {
11118 const auto group = make_watch_options_def_group (nullptr);
11119 if (gdb::option::complete_options
11120 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
11121 return;
11122
11123 const char *word = advance_to_expression_complete_word_point (tracker, text);
11124 expression_completer (ignore, tracker, text, word);
11125 }
11126
11127 static void
11128 watch_command (const char *arg, int from_tty)
11129 {
11130 watch_maybe_just_location (arg, hw_write, from_tty);
11131 }
11132
11133 void
11134 rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
11135 {
11136 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11137 }
11138
11139 static void
11140 rwatch_command (const char *arg, int from_tty)
11141 {
11142 watch_maybe_just_location (arg, hw_read, from_tty);
11143 }
11144
11145 void
11146 awatch_command_wrapper (const char *arg, int from_tty, bool internal)
11147 {
11148 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11149 }
11150
11151 static void
11152 awatch_command (const char *arg, int from_tty)
11153 {
11154 watch_maybe_just_location (arg, hw_access, from_tty);
11155 }
11156 \f
11157
11158 /* Data for the FSM that manages the until(location)/advance commands
11159 in infcmd.c. Here because it uses the mechanisms of
11160 breakpoints. */
11161
11162 struct until_break_fsm : public thread_fsm
11163 {
11164 /* The thread that was current when the command was executed. */
11165 int thread;
11166
11167 /* The breakpoint set at the return address in the caller frame,
11168 plus breakpoints at all the destination locations. */
11169 std::vector<breakpoint_up> breakpoints;
11170
11171 until_break_fsm (struct interp *cmd_interp, int thread,
11172 std::vector<breakpoint_up> &&breakpoints)
11173 : thread_fsm (cmd_interp),
11174 thread (thread),
11175 breakpoints (std::move (breakpoints))
11176 {
11177 }
11178
11179 void clean_up (struct thread_info *thread) override;
11180 bool should_stop (struct thread_info *thread) override;
11181 enum async_reply_reason do_async_reply_reason () override;
11182 };
11183
11184 /* Implementation of the 'should_stop' FSM method for the
11185 until(location)/advance commands. */
11186
11187 bool
11188 until_break_fsm::should_stop (struct thread_info *tp)
11189 {
11190 for (const breakpoint_up &bp : breakpoints)
11191 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11192 bp.get ()) != NULL)
11193 {
11194 set_finished ();
11195 break;
11196 }
11197
11198 return true;
11199 }
11200
11201 /* Implementation of the 'clean_up' FSM method for the
11202 until(location)/advance commands. */
11203
11204 void
11205 until_break_fsm::clean_up (struct thread_info *)
11206 {
11207 /* Clean up our temporary breakpoints. */
11208 breakpoints.clear ();
11209 delete_longjmp_breakpoint (thread);
11210 }
11211
11212 /* Implementation of the 'async_reply_reason' FSM method for the
11213 until(location)/advance commands. */
11214
11215 enum async_reply_reason
11216 until_break_fsm::do_async_reply_reason ()
11217 {
11218 return EXEC_ASYNC_LOCATION_REACHED;
11219 }
11220
11221 void
11222 until_break_command (const char *arg, int from_tty, int anywhere)
11223 {
11224 struct frame_info *frame;
11225 struct gdbarch *frame_gdbarch;
11226 struct frame_id stack_frame_id;
11227 struct frame_id caller_frame_id;
11228 int thread;
11229 struct thread_info *tp;
11230
11231 clear_proceed_status (0);
11232
11233 /* Set a breakpoint where the user wants it and at return from
11234 this function. */
11235
11236 event_location_up location = string_to_event_location (&arg, current_language);
11237
11238 std::vector<symtab_and_line> sals
11239 = (last_displayed_sal_is_valid ()
11240 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11241 get_last_displayed_symtab (),
11242 get_last_displayed_line ())
11243 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11244 NULL, NULL, 0));
11245
11246 if (sals.empty ())
11247 error (_("Couldn't get information on specified line."));
11248
11249 if (*arg)
11250 error (_("Junk at end of arguments."));
11251
11252 tp = inferior_thread ();
11253 thread = tp->global_num;
11254
11255 /* Note linespec handling above invalidates the frame chain.
11256 Installing a breakpoint also invalidates the frame chain (as it
11257 may need to switch threads), so do any frame handling before
11258 that. */
11259
11260 frame = get_selected_frame (NULL);
11261 frame_gdbarch = get_frame_arch (frame);
11262 stack_frame_id = get_stack_frame_id (frame);
11263 caller_frame_id = frame_unwind_caller_id (frame);
11264
11265 /* Keep within the current frame, or in frames called by the current
11266 one. */
11267
11268 std::vector<breakpoint_up> breakpoints;
11269
11270 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11271
11272 if (frame_id_p (caller_frame_id))
11273 {
11274 struct symtab_and_line sal2;
11275 struct gdbarch *caller_gdbarch;
11276
11277 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11278 sal2.pc = frame_unwind_caller_pc (frame);
11279 caller_gdbarch = frame_unwind_caller_arch (frame);
11280
11281 breakpoint_up caller_breakpoint
11282 = set_momentary_breakpoint (caller_gdbarch, sal2,
11283 caller_frame_id, bp_until);
11284 breakpoints.emplace_back (std::move (caller_breakpoint));
11285
11286 set_longjmp_breakpoint (tp, caller_frame_id);
11287 lj_deleter.emplace (thread);
11288 }
11289
11290 /* set_momentary_breakpoint could invalidate FRAME. */
11291 frame = NULL;
11292
11293 /* If the user told us to continue until a specified location, we
11294 don't specify a frame at which we need to stop. Otherwise,
11295 specify the selected frame, because we want to stop only at the
11296 very same frame. */
11297 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
11298
11299 for (symtab_and_line &sal : sals)
11300 {
11301 resolve_sal_pc (&sal);
11302
11303 breakpoint_up location_breakpoint
11304 = set_momentary_breakpoint (frame_gdbarch, sal,
11305 stop_frame_id, bp_until);
11306 breakpoints.emplace_back (std::move (location_breakpoint));
11307 }
11308
11309 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
11310 std::move (breakpoints));
11311
11312 if (lj_deleter)
11313 lj_deleter->release ();
11314
11315 proceed (-1, GDB_SIGNAL_DEFAULT);
11316 }
11317
11318 /* This function attempts to parse an optional "if <cond>" clause
11319 from the arg string. If one is not found, it returns NULL.
11320
11321 Else, it returns a pointer to the condition string. (It does not
11322 attempt to evaluate the string against a particular block.) And,
11323 it updates arg to point to the first character following the parsed
11324 if clause in the arg string. */
11325
11326 const char *
11327 ep_parse_optional_if_clause (const char **arg)
11328 {
11329 const char *cond_string;
11330
11331 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11332 return NULL;
11333
11334 /* Skip the "if" keyword. */
11335 (*arg) += 2;
11336
11337 /* Skip any extra leading whitespace, and record the start of the
11338 condition string. */
11339 *arg = skip_spaces (*arg);
11340 cond_string = *arg;
11341
11342 /* Assume that the condition occupies the remainder of the arg
11343 string. */
11344 (*arg) += strlen (cond_string);
11345
11346 return cond_string;
11347 }
11348
11349 /* Commands to deal with catching events, such as signals, exceptions,
11350 process start/exit, etc. */
11351
11352 typedef enum
11353 {
11354 catch_fork_temporary, catch_vfork_temporary,
11355 catch_fork_permanent, catch_vfork_permanent
11356 }
11357 catch_fork_kind;
11358
11359 static void
11360 catch_fork_command_1 (const char *arg, int from_tty,
11361 struct cmd_list_element *command)
11362 {
11363 struct gdbarch *gdbarch = get_current_arch ();
11364 const char *cond_string = NULL;
11365 catch_fork_kind fork_kind;
11366
11367 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11368 bool temp = (fork_kind == catch_fork_temporary
11369 || fork_kind == catch_vfork_temporary);
11370
11371 if (!arg)
11372 arg = "";
11373 arg = skip_spaces (arg);
11374
11375 /* The allowed syntax is:
11376 catch [v]fork
11377 catch [v]fork if <cond>
11378
11379 First, check if there's an if clause. */
11380 cond_string = ep_parse_optional_if_clause (&arg);
11381
11382 if ((*arg != '\0') && !isspace (*arg))
11383 error (_("Junk at end of arguments."));
11384
11385 /* If this target supports it, create a fork or vfork catchpoint
11386 and enable reporting of such events. */
11387 switch (fork_kind)
11388 {
11389 case catch_fork_temporary:
11390 case catch_fork_permanent:
11391 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
11392 &catch_fork_breakpoint_ops);
11393 break;
11394 case catch_vfork_temporary:
11395 case catch_vfork_permanent:
11396 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
11397 &catch_vfork_breakpoint_ops);
11398 break;
11399 default:
11400 error (_("unsupported or unknown fork kind; cannot catch it"));
11401 break;
11402 }
11403 }
11404
11405 static void
11406 catch_exec_command_1 (const char *arg, int from_tty,
11407 struct cmd_list_element *command)
11408 {
11409 struct gdbarch *gdbarch = get_current_arch ();
11410 const char *cond_string = NULL;
11411 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
11412
11413 if (!arg)
11414 arg = "";
11415 arg = skip_spaces (arg);
11416
11417 /* The allowed syntax is:
11418 catch exec
11419 catch exec if <cond>
11420
11421 First, check if there's an if clause. */
11422 cond_string = ep_parse_optional_if_clause (&arg);
11423
11424 if ((*arg != '\0') && !isspace (*arg))
11425 error (_("Junk at end of arguments."));
11426
11427 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11428 init_catchpoint (c.get (), gdbarch, temp, cond_string,
11429 &catch_exec_breakpoint_ops);
11430 c->exec_pathname = NULL;
11431
11432 install_breakpoint (0, std::move (c), 1);
11433 }
11434
11435 void
11436 init_ada_exception_breakpoint (struct breakpoint *b,
11437 struct gdbarch *gdbarch,
11438 struct symtab_and_line sal,
11439 const char *addr_string,
11440 const struct breakpoint_ops *ops,
11441 int tempflag,
11442 int enabled,
11443 int from_tty)
11444 {
11445 if (from_tty)
11446 {
11447 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11448 if (!loc_gdbarch)
11449 loc_gdbarch = gdbarch;
11450
11451 describe_other_breakpoints (loc_gdbarch,
11452 sal.pspace, sal.pc, sal.section, -1);
11453 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11454 version for exception catchpoints, because two catchpoints
11455 used for different exception names will use the same address.
11456 In this case, a "breakpoint ... also set at..." warning is
11457 unproductive. Besides, the warning phrasing is also a bit
11458 inappropriate, we should use the word catchpoint, and tell
11459 the user what type of catchpoint it is. The above is good
11460 enough for now, though. */
11461 }
11462
11463 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
11464
11465 b->enable_state = enabled ? bp_enabled : bp_disabled;
11466 b->disposition = tempflag ? disp_del : disp_donttouch;
11467 b->location = string_to_event_location (&addr_string,
11468 language_def (language_ada));
11469 b->language = language_ada;
11470 }
11471
11472 \f
11473
11474 /* Compare two breakpoints and return a strcmp-like result. */
11475
11476 static int
11477 compare_breakpoints (const breakpoint *a, const breakpoint *b)
11478 {
11479 uintptr_t ua = (uintptr_t) a;
11480 uintptr_t ub = (uintptr_t) b;
11481
11482 if (a->number < b->number)
11483 return -1;
11484 else if (a->number > b->number)
11485 return 1;
11486
11487 /* Now sort by address, in case we see, e..g, two breakpoints with
11488 the number 0. */
11489 if (ua < ub)
11490 return -1;
11491 return ua > ub ? 1 : 0;
11492 }
11493
11494 /* Delete breakpoints by address or line. */
11495
11496 static void
11497 clear_command (const char *arg, int from_tty)
11498 {
11499 struct breakpoint *b;
11500 int default_match;
11501
11502 std::vector<symtab_and_line> decoded_sals;
11503 symtab_and_line last_sal;
11504 gdb::array_view<symtab_and_line> sals;
11505 if (arg)
11506 {
11507 decoded_sals
11508 = decode_line_with_current_source (arg,
11509 (DECODE_LINE_FUNFIRSTLINE
11510 | DECODE_LINE_LIST_MODE));
11511 default_match = 0;
11512 sals = decoded_sals;
11513 }
11514 else
11515 {
11516 /* Set sal's line, symtab, pc, and pspace to the values
11517 corresponding to the last call to print_frame_info. If the
11518 codepoint is not valid, this will set all the fields to 0. */
11519 last_sal = get_last_displayed_sal ();
11520 if (last_sal.symtab == 0)
11521 error (_("No source file specified."));
11522
11523 default_match = 1;
11524 sals = last_sal;
11525 }
11526
11527 /* We don't call resolve_sal_pc here. That's not as bad as it
11528 seems, because all existing breakpoints typically have both
11529 file/line and pc set. So, if clear is given file/line, we can
11530 match this to existing breakpoint without obtaining pc at all.
11531
11532 We only support clearing given the address explicitly
11533 present in breakpoint table. Say, we've set breakpoint
11534 at file:line. There were several PC values for that file:line,
11535 due to optimization, all in one block.
11536
11537 We've picked one PC value. If "clear" is issued with another
11538 PC corresponding to the same file:line, the breakpoint won't
11539 be cleared. We probably can still clear the breakpoint, but
11540 since the other PC value is never presented to user, user
11541 can only find it by guessing, and it does not seem important
11542 to support that. */
11543
11544 /* For each line spec given, delete bps which correspond to it. Do
11545 it in two passes, solely to preserve the current behavior that
11546 from_tty is forced true if we delete more than one
11547 breakpoint. */
11548
11549 std::vector<struct breakpoint *> found;
11550 for (const auto &sal : sals)
11551 {
11552 const char *sal_fullname;
11553
11554 /* If exact pc given, clear bpts at that pc.
11555 If line given (pc == 0), clear all bpts on specified line.
11556 If defaulting, clear all bpts on default line
11557 or at default pc.
11558
11559 defaulting sal.pc != 0 tests to do
11560
11561 0 1 pc
11562 1 1 pc _and_ line
11563 0 0 line
11564 1 0 <can't happen> */
11565
11566 sal_fullname = (sal.symtab == NULL
11567 ? NULL : symtab_to_fullname (sal.symtab));
11568
11569 /* Find all matching breakpoints and add them to 'found'. */
11570 ALL_BREAKPOINTS (b)
11571 {
11572 int match = 0;
11573 /* Are we going to delete b? */
11574 if (b->type != bp_none && !is_watchpoint (b))
11575 {
11576 struct bp_location *loc = b->loc;
11577 for (; loc; loc = loc->next)
11578 {
11579 /* If the user specified file:line, don't allow a PC
11580 match. This matches historical gdb behavior. */
11581 int pc_match = (!sal.explicit_line
11582 && sal.pc
11583 && (loc->pspace == sal.pspace)
11584 && (loc->address == sal.pc)
11585 && (!section_is_overlay (loc->section)
11586 || loc->section == sal.section));
11587 int line_match = 0;
11588
11589 if ((default_match || sal.explicit_line)
11590 && loc->symtab != NULL
11591 && sal_fullname != NULL
11592 && sal.pspace == loc->pspace
11593 && loc->line_number == sal.line
11594 && filename_cmp (symtab_to_fullname (loc->symtab),
11595 sal_fullname) == 0)
11596 line_match = 1;
11597
11598 if (pc_match || line_match)
11599 {
11600 match = 1;
11601 break;
11602 }
11603 }
11604 }
11605
11606 if (match)
11607 found.push_back (b);
11608 }
11609 }
11610
11611 /* Now go thru the 'found' chain and delete them. */
11612 if (found.empty ())
11613 {
11614 if (arg)
11615 error (_("No breakpoint at %s."), arg);
11616 else
11617 error (_("No breakpoint at this line."));
11618 }
11619
11620 /* Remove duplicates from the vec. */
11621 std::sort (found.begin (), found.end (),
11622 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11623 {
11624 return compare_breakpoints (bp_a, bp_b) < 0;
11625 });
11626 found.erase (std::unique (found.begin (), found.end (),
11627 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11628 {
11629 return compare_breakpoints (bp_a, bp_b) == 0;
11630 }),
11631 found.end ());
11632
11633 if (found.size () > 1)
11634 from_tty = 1; /* Always report if deleted more than one. */
11635 if (from_tty)
11636 {
11637 if (found.size () == 1)
11638 printf_unfiltered (_("Deleted breakpoint "));
11639 else
11640 printf_unfiltered (_("Deleted breakpoints "));
11641 }
11642
11643 for (breakpoint *iter : found)
11644 {
11645 if (from_tty)
11646 printf_unfiltered ("%d ", iter->number);
11647 delete_breakpoint (iter);
11648 }
11649 if (from_tty)
11650 putchar_unfiltered ('\n');
11651 }
11652 \f
11653 /* Delete breakpoint in BS if they are `delete' breakpoints and
11654 all breakpoints that are marked for deletion, whether hit or not.
11655 This is called after any breakpoint is hit, or after errors. */
11656
11657 void
11658 breakpoint_auto_delete (bpstat bs)
11659 {
11660 struct breakpoint *b, *b_tmp;
11661
11662 for (; bs; bs = bs->next)
11663 if (bs->breakpoint_at
11664 && bs->breakpoint_at->disposition == disp_del
11665 && bs->stop)
11666 delete_breakpoint (bs->breakpoint_at);
11667
11668 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11669 {
11670 if (b->disposition == disp_del_at_next_stop)
11671 delete_breakpoint (b);
11672 }
11673 }
11674
11675 /* A comparison function for bp_location AP and BP being interfaced to
11676 std::sort. Sort elements primarily by their ADDRESS (no matter what
11677 bl_address_is_meaningful says), secondarily by ordering first
11678 permanent elements and terciarily just ensuring the array is sorted
11679 stable way despite std::sort being an unstable algorithm. */
11680
11681 static int
11682 bp_location_is_less_than (const bp_location *a, const bp_location *b)
11683 {
11684 if (a->address != b->address)
11685 return a->address < b->address;
11686
11687 /* Sort locations at the same address by their pspace number, keeping
11688 locations of the same inferior (in a multi-inferior environment)
11689 grouped. */
11690
11691 if (a->pspace->num != b->pspace->num)
11692 return a->pspace->num < b->pspace->num;
11693
11694 /* Sort permanent breakpoints first. */
11695 if (a->permanent != b->permanent)
11696 return a->permanent > b->permanent;
11697
11698 /* Sort by type in order to make duplicate determination easier.
11699 See update_global_location_list. This is kept in sync with
11700 breakpoint_locations_match. */
11701 if (a->loc_type < b->loc_type)
11702 return true;
11703
11704 /* Likewise, for range-breakpoints, sort by length. */
11705 if (a->loc_type == bp_loc_hardware_breakpoint
11706 && b->loc_type == bp_loc_hardware_breakpoint
11707 && a->length < b->length)
11708 return true;
11709
11710 /* Make the internal GDB representation stable across GDB runs
11711 where A and B memory inside GDB can differ. Breakpoint locations of
11712 the same type at the same address can be sorted in arbitrary order. */
11713
11714 if (a->owner->number != b->owner->number)
11715 return a->owner->number < b->owner->number;
11716
11717 return a < b;
11718 }
11719
11720 /* Set bp_locations_placed_address_before_address_max and
11721 bp_locations_shadow_len_after_address_max according to the current
11722 content of the bp_locations array. */
11723
11724 static void
11725 bp_locations_target_extensions_update (void)
11726 {
11727 struct bp_location *bl, **blp_tmp;
11728
11729 bp_locations_placed_address_before_address_max = 0;
11730 bp_locations_shadow_len_after_address_max = 0;
11731
11732 ALL_BP_LOCATIONS (bl, blp_tmp)
11733 {
11734 CORE_ADDR start, end, addr;
11735
11736 if (!bp_location_has_shadow (bl))
11737 continue;
11738
11739 start = bl->target_info.placed_address;
11740 end = start + bl->target_info.shadow_len;
11741
11742 gdb_assert (bl->address >= start);
11743 addr = bl->address - start;
11744 if (addr > bp_locations_placed_address_before_address_max)
11745 bp_locations_placed_address_before_address_max = addr;
11746
11747 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11748
11749 gdb_assert (bl->address < end);
11750 addr = end - bl->address;
11751 if (addr > bp_locations_shadow_len_after_address_max)
11752 bp_locations_shadow_len_after_address_max = addr;
11753 }
11754 }
11755
11756 /* Download tracepoint locations if they haven't been. */
11757
11758 static void
11759 download_tracepoint_locations (void)
11760 {
11761 struct breakpoint *b;
11762 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11763
11764 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11765
11766 ALL_TRACEPOINTS (b)
11767 {
11768 struct bp_location *bl;
11769 struct tracepoint *t;
11770 int bp_location_downloaded = 0;
11771
11772 if ((b->type == bp_fast_tracepoint
11773 ? !may_insert_fast_tracepoints
11774 : !may_insert_tracepoints))
11775 continue;
11776
11777 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11778 {
11779 if (target_can_download_tracepoint ())
11780 can_download_tracepoint = TRIBOOL_TRUE;
11781 else
11782 can_download_tracepoint = TRIBOOL_FALSE;
11783 }
11784
11785 if (can_download_tracepoint == TRIBOOL_FALSE)
11786 break;
11787
11788 for (bl = b->loc; bl; bl = bl->next)
11789 {
11790 /* In tracepoint, locations are _never_ duplicated, so
11791 should_be_inserted is equivalent to
11792 unduplicated_should_be_inserted. */
11793 if (!should_be_inserted (bl) || bl->inserted)
11794 continue;
11795
11796 switch_to_program_space_and_thread (bl->pspace);
11797
11798 target_download_tracepoint (bl);
11799
11800 bl->inserted = 1;
11801 bp_location_downloaded = 1;
11802 }
11803 t = (struct tracepoint *) b;
11804 t->number_on_target = b->number;
11805 if (bp_location_downloaded)
11806 gdb::observers::breakpoint_modified.notify (b);
11807 }
11808 }
11809
11810 /* Swap the insertion/duplication state between two locations. */
11811
11812 static void
11813 swap_insertion (struct bp_location *left, struct bp_location *right)
11814 {
11815 const int left_inserted = left->inserted;
11816 const int left_duplicate = left->duplicate;
11817 const int left_needs_update = left->needs_update;
11818 const struct bp_target_info left_target_info = left->target_info;
11819
11820 /* Locations of tracepoints can never be duplicated. */
11821 if (is_tracepoint (left->owner))
11822 gdb_assert (!left->duplicate);
11823 if (is_tracepoint (right->owner))
11824 gdb_assert (!right->duplicate);
11825
11826 left->inserted = right->inserted;
11827 left->duplicate = right->duplicate;
11828 left->needs_update = right->needs_update;
11829 left->target_info = right->target_info;
11830 right->inserted = left_inserted;
11831 right->duplicate = left_duplicate;
11832 right->needs_update = left_needs_update;
11833 right->target_info = left_target_info;
11834 }
11835
11836 /* Force the re-insertion of the locations at ADDRESS. This is called
11837 once a new/deleted/modified duplicate location is found and we are evaluating
11838 conditions on the target's side. Such conditions need to be updated on
11839 the target. */
11840
11841 static void
11842 force_breakpoint_reinsertion (struct bp_location *bl)
11843 {
11844 struct bp_location **locp = NULL, **loc2p;
11845 struct bp_location *loc;
11846 CORE_ADDR address = 0;
11847 int pspace_num;
11848
11849 address = bl->address;
11850 pspace_num = bl->pspace->num;
11851
11852 /* This is only meaningful if the target is
11853 evaluating conditions and if the user has
11854 opted for condition evaluation on the target's
11855 side. */
11856 if (gdb_evaluates_breakpoint_condition_p ()
11857 || !target_supports_evaluation_of_breakpoint_conditions ())
11858 return;
11859
11860 /* Flag all breakpoint locations with this address and
11861 the same program space as the location
11862 as "its condition has changed". We need to
11863 update the conditions on the target's side. */
11864 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11865 {
11866 loc = *loc2p;
11867
11868 if (!is_breakpoint (loc->owner)
11869 || pspace_num != loc->pspace->num)
11870 continue;
11871
11872 /* Flag the location appropriately. We use a different state to
11873 let everyone know that we already updated the set of locations
11874 with addr bl->address and program space bl->pspace. This is so
11875 we don't have to keep calling these functions just to mark locations
11876 that have already been marked. */
11877 loc->condition_changed = condition_updated;
11878
11879 /* Free the agent expression bytecode as well. We will compute
11880 it later on. */
11881 loc->cond_bytecode.reset ();
11882 }
11883 }
11884
11885 /* Called whether new breakpoints are created, or existing breakpoints
11886 deleted, to update the global location list and recompute which
11887 locations are duplicate of which.
11888
11889 The INSERT_MODE flag determines whether locations may not, may, or
11890 shall be inserted now. See 'enum ugll_insert_mode' for more
11891 info. */
11892
11893 static void
11894 update_global_location_list (enum ugll_insert_mode insert_mode)
11895 {
11896 struct breakpoint *b;
11897 struct bp_location **locp, *loc;
11898 /* Last breakpoint location address that was marked for update. */
11899 CORE_ADDR last_addr = 0;
11900 /* Last breakpoint location program space that was marked for update. */
11901 int last_pspace_num = -1;
11902
11903 /* Used in the duplicates detection below. When iterating over all
11904 bp_locations, points to the first bp_location of a given address.
11905 Breakpoints and watchpoints of different types are never
11906 duplicates of each other. Keep one pointer for each type of
11907 breakpoint/watchpoint, so we only need to loop over all locations
11908 once. */
11909 struct bp_location *bp_loc_first; /* breakpoint */
11910 struct bp_location *wp_loc_first; /* hardware watchpoint */
11911 struct bp_location *awp_loc_first; /* access watchpoint */
11912 struct bp_location *rwp_loc_first; /* read watchpoint */
11913
11914 /* Saved former bp_locations array which we compare against the newly
11915 built bp_locations from the current state of ALL_BREAKPOINTS. */
11916 struct bp_location **old_locp;
11917 unsigned old_locations_count;
11918 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
11919
11920 old_locations_count = bp_locations_count;
11921 bp_locations = NULL;
11922 bp_locations_count = 0;
11923
11924 ALL_BREAKPOINTS (b)
11925 for (loc = b->loc; loc; loc = loc->next)
11926 bp_locations_count++;
11927
11928 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11929 locp = bp_locations;
11930 ALL_BREAKPOINTS (b)
11931 for (loc = b->loc; loc; loc = loc->next)
11932 *locp++ = loc;
11933
11934 /* See if we need to "upgrade" a software breakpoint to a hardware
11935 breakpoint. Do this before deciding whether locations are
11936 duplicates. Also do this before sorting because sorting order
11937 depends on location type. */
11938 for (locp = bp_locations;
11939 locp < bp_locations + bp_locations_count;
11940 locp++)
11941 {
11942 loc = *locp;
11943 if (!loc->inserted && should_be_inserted (loc))
11944 handle_automatic_hardware_breakpoints (loc);
11945 }
11946
11947 std::sort (bp_locations, bp_locations + bp_locations_count,
11948 bp_location_is_less_than);
11949
11950 bp_locations_target_extensions_update ();
11951
11952 /* Identify bp_location instances that are no longer present in the
11953 new list, and therefore should be freed. Note that it's not
11954 necessary that those locations should be removed from inferior --
11955 if there's another location at the same address (previously
11956 marked as duplicate), we don't need to remove/insert the
11957 location.
11958
11959 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11960 and former bp_location array state respectively. */
11961
11962 locp = bp_locations;
11963 for (old_locp = old_locations.get ();
11964 old_locp < old_locations.get () + old_locations_count;
11965 old_locp++)
11966 {
11967 struct bp_location *old_loc = *old_locp;
11968 struct bp_location **loc2p;
11969
11970 /* Tells if 'old_loc' is found among the new locations. If
11971 not, we have to free it. */
11972 int found_object = 0;
11973 /* Tells if the location should remain inserted in the target. */
11974 int keep_in_target = 0;
11975 int removed = 0;
11976
11977 /* Skip LOCP entries which will definitely never be needed.
11978 Stop either at or being the one matching OLD_LOC. */
11979 while (locp < bp_locations + bp_locations_count
11980 && (*locp)->address < old_loc->address)
11981 locp++;
11982
11983 for (loc2p = locp;
11984 (loc2p < bp_locations + bp_locations_count
11985 && (*loc2p)->address == old_loc->address);
11986 loc2p++)
11987 {
11988 /* Check if this is a new/duplicated location or a duplicated
11989 location that had its condition modified. If so, we want to send
11990 its condition to the target if evaluation of conditions is taking
11991 place there. */
11992 if ((*loc2p)->condition_changed == condition_modified
11993 && (last_addr != old_loc->address
11994 || last_pspace_num != old_loc->pspace->num))
11995 {
11996 force_breakpoint_reinsertion (*loc2p);
11997 last_pspace_num = old_loc->pspace->num;
11998 }
11999
12000 if (*loc2p == old_loc)
12001 found_object = 1;
12002 }
12003
12004 /* We have already handled this address, update it so that we don't
12005 have to go through updates again. */
12006 last_addr = old_loc->address;
12007
12008 /* Target-side condition evaluation: Handle deleted locations. */
12009 if (!found_object)
12010 force_breakpoint_reinsertion (old_loc);
12011
12012 /* If this location is no longer present, and inserted, look if
12013 there's maybe a new location at the same address. If so,
12014 mark that one inserted, and don't remove this one. This is
12015 needed so that we don't have a time window where a breakpoint
12016 at certain location is not inserted. */
12017
12018 if (old_loc->inserted)
12019 {
12020 /* If the location is inserted now, we might have to remove
12021 it. */
12022
12023 if (found_object && should_be_inserted (old_loc))
12024 {
12025 /* The location is still present in the location list,
12026 and still should be inserted. Don't do anything. */
12027 keep_in_target = 1;
12028 }
12029 else
12030 {
12031 /* This location still exists, but it won't be kept in the
12032 target since it may have been disabled. We proceed to
12033 remove its target-side condition. */
12034
12035 /* The location is either no longer present, or got
12036 disabled. See if there's another location at the
12037 same address, in which case we don't need to remove
12038 this one from the target. */
12039
12040 /* OLD_LOC comes from existing struct breakpoint. */
12041 if (bl_address_is_meaningful (old_loc))
12042 {
12043 for (loc2p = locp;
12044 (loc2p < bp_locations + bp_locations_count
12045 && (*loc2p)->address == old_loc->address);
12046 loc2p++)
12047 {
12048 struct bp_location *loc2 = *loc2p;
12049
12050 if (loc2 == old_loc)
12051 continue;
12052
12053 if (breakpoint_locations_match (loc2, old_loc))
12054 {
12055 /* Read watchpoint locations are switched to
12056 access watchpoints, if the former are not
12057 supported, but the latter are. */
12058 if (is_hardware_watchpoint (old_loc->owner))
12059 {
12060 gdb_assert (is_hardware_watchpoint (loc2->owner));
12061 loc2->watchpoint_type = old_loc->watchpoint_type;
12062 }
12063
12064 /* loc2 is a duplicated location. We need to check
12065 if it should be inserted in case it will be
12066 unduplicated. */
12067 if (unduplicated_should_be_inserted (loc2))
12068 {
12069 swap_insertion (old_loc, loc2);
12070 keep_in_target = 1;
12071 break;
12072 }
12073 }
12074 }
12075 }
12076 }
12077
12078 if (!keep_in_target)
12079 {
12080 if (remove_breakpoint (old_loc))
12081 {
12082 /* This is just about all we can do. We could keep
12083 this location on the global list, and try to
12084 remove it next time, but there's no particular
12085 reason why we will succeed next time.
12086
12087 Note that at this point, old_loc->owner is still
12088 valid, as delete_breakpoint frees the breakpoint
12089 only after calling us. */
12090 printf_filtered (_("warning: Error removing "
12091 "breakpoint %d\n"),
12092 old_loc->owner->number);
12093 }
12094 removed = 1;
12095 }
12096 }
12097
12098 if (!found_object)
12099 {
12100 if (removed && target_is_non_stop_p ()
12101 && need_moribund_for_location_type (old_loc))
12102 {
12103 /* This location was removed from the target. In
12104 non-stop mode, a race condition is possible where
12105 we've removed a breakpoint, but stop events for that
12106 breakpoint are already queued and will arrive later.
12107 We apply an heuristic to be able to distinguish such
12108 SIGTRAPs from other random SIGTRAPs: we keep this
12109 breakpoint location for a bit, and will retire it
12110 after we see some number of events. The theory here
12111 is that reporting of events should, "on the average",
12112 be fair, so after a while we'll see events from all
12113 threads that have anything of interest, and no longer
12114 need to keep this breakpoint location around. We
12115 don't hold locations forever so to reduce chances of
12116 mistaking a non-breakpoint SIGTRAP for a breakpoint
12117 SIGTRAP.
12118
12119 The heuristic failing can be disastrous on
12120 decr_pc_after_break targets.
12121
12122 On decr_pc_after_break targets, like e.g., x86-linux,
12123 if we fail to recognize a late breakpoint SIGTRAP,
12124 because events_till_retirement has reached 0 too
12125 soon, we'll fail to do the PC adjustment, and report
12126 a random SIGTRAP to the user. When the user resumes
12127 the inferior, it will most likely immediately crash
12128 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12129 corrupted, because of being resumed e.g., in the
12130 middle of a multi-byte instruction, or skipped a
12131 one-byte instruction. This was actually seen happen
12132 on native x86-linux, and should be less rare on
12133 targets that do not support new thread events, like
12134 remote, due to the heuristic depending on
12135 thread_count.
12136
12137 Mistaking a random SIGTRAP for a breakpoint trap
12138 causes similar symptoms (PC adjustment applied when
12139 it shouldn't), but then again, playing with SIGTRAPs
12140 behind the debugger's back is asking for trouble.
12141
12142 Since hardware watchpoint traps are always
12143 distinguishable from other traps, so we don't need to
12144 apply keep hardware watchpoint moribund locations
12145 around. We simply always ignore hardware watchpoint
12146 traps we can no longer explain. */
12147
12148 process_stratum_target *proc_target = nullptr;
12149 for (inferior *inf : all_inferiors ())
12150 if (inf->pspace == old_loc->pspace)
12151 {
12152 proc_target = inf->process_target ();
12153 break;
12154 }
12155 if (proc_target != nullptr)
12156 old_loc->events_till_retirement
12157 = 3 * (thread_count (proc_target) + 1);
12158 else
12159 old_loc->events_till_retirement = 1;
12160 old_loc->owner = NULL;
12161
12162 moribund_locations.push_back (old_loc);
12163 }
12164 else
12165 {
12166 old_loc->owner = NULL;
12167 decref_bp_location (&old_loc);
12168 }
12169 }
12170 }
12171
12172 /* Rescan breakpoints at the same address and section, marking the
12173 first one as "first" and any others as "duplicates". This is so
12174 that the bpt instruction is only inserted once. If we have a
12175 permanent breakpoint at the same place as BPT, make that one the
12176 official one, and the rest as duplicates. Permanent breakpoints
12177 are sorted first for the same address.
12178
12179 Do the same for hardware watchpoints, but also considering the
12180 watchpoint's type (regular/access/read) and length. */
12181
12182 bp_loc_first = NULL;
12183 wp_loc_first = NULL;
12184 awp_loc_first = NULL;
12185 rwp_loc_first = NULL;
12186 ALL_BP_LOCATIONS (loc, locp)
12187 {
12188 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12189 non-NULL. */
12190 struct bp_location **loc_first_p;
12191 b = loc->owner;
12192
12193 if (!unduplicated_should_be_inserted (loc)
12194 || !bl_address_is_meaningful (loc)
12195 /* Don't detect duplicate for tracepoint locations because they are
12196 never duplicated. See the comments in field `duplicate' of
12197 `struct bp_location'. */
12198 || is_tracepoint (b))
12199 {
12200 /* Clear the condition modification flag. */
12201 loc->condition_changed = condition_unchanged;
12202 continue;
12203 }
12204
12205 if (b->type == bp_hardware_watchpoint)
12206 loc_first_p = &wp_loc_first;
12207 else if (b->type == bp_read_watchpoint)
12208 loc_first_p = &rwp_loc_first;
12209 else if (b->type == bp_access_watchpoint)
12210 loc_first_p = &awp_loc_first;
12211 else
12212 loc_first_p = &bp_loc_first;
12213
12214 if (*loc_first_p == NULL
12215 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12216 || !breakpoint_locations_match (loc, *loc_first_p))
12217 {
12218 *loc_first_p = loc;
12219 loc->duplicate = 0;
12220
12221 if (is_breakpoint (loc->owner) && loc->condition_changed)
12222 {
12223 loc->needs_update = 1;
12224 /* Clear the condition modification flag. */
12225 loc->condition_changed = condition_unchanged;
12226 }
12227 continue;
12228 }
12229
12230
12231 /* This and the above ensure the invariant that the first location
12232 is not duplicated, and is the inserted one.
12233 All following are marked as duplicated, and are not inserted. */
12234 if (loc->inserted)
12235 swap_insertion (loc, *loc_first_p);
12236 loc->duplicate = 1;
12237
12238 /* Clear the condition modification flag. */
12239 loc->condition_changed = condition_unchanged;
12240 }
12241
12242 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12243 {
12244 if (insert_mode != UGLL_DONT_INSERT)
12245 insert_breakpoint_locations ();
12246 else
12247 {
12248 /* Even though the caller told us to not insert new
12249 locations, we may still need to update conditions on the
12250 target's side of breakpoints that were already inserted
12251 if the target is evaluating breakpoint conditions. We
12252 only update conditions for locations that are marked
12253 "needs_update". */
12254 update_inserted_breakpoint_locations ();
12255 }
12256 }
12257
12258 if (insert_mode != UGLL_DONT_INSERT)
12259 download_tracepoint_locations ();
12260 }
12261
12262 void
12263 breakpoint_retire_moribund (void)
12264 {
12265 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12266 {
12267 struct bp_location *loc = moribund_locations[ix];
12268 if (--(loc->events_till_retirement) == 0)
12269 {
12270 decref_bp_location (&loc);
12271 unordered_remove (moribund_locations, ix);
12272 --ix;
12273 }
12274 }
12275 }
12276
12277 static void
12278 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12279 {
12280
12281 try
12282 {
12283 update_global_location_list (insert_mode);
12284 }
12285 catch (const gdb_exception_error &e)
12286 {
12287 }
12288 }
12289
12290 /* Clear BKP from a BPS. */
12291
12292 static void
12293 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12294 {
12295 bpstat bs;
12296
12297 for (bs = bps; bs; bs = bs->next)
12298 if (bs->breakpoint_at == bpt)
12299 {
12300 bs->breakpoint_at = NULL;
12301 bs->old_val = NULL;
12302 /* bs->commands will be freed later. */
12303 }
12304 }
12305
12306 /* Callback for iterate_over_threads. */
12307 static int
12308 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12309 {
12310 struct breakpoint *bpt = (struct breakpoint *) data;
12311
12312 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12313 return 0;
12314 }
12315
12316 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12317 callbacks. */
12318
12319 static void
12320 say_where (struct breakpoint *b)
12321 {
12322 struct value_print_options opts;
12323
12324 get_user_print_options (&opts);
12325
12326 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12327 single string. */
12328 if (b->loc == NULL)
12329 {
12330 /* For pending locations, the output differs slightly based
12331 on b->extra_string. If this is non-NULL, it contains either
12332 a condition or dprintf arguments. */
12333 if (b->extra_string == NULL)
12334 {
12335 printf_filtered (_(" (%s) pending."),
12336 event_location_to_string (b->location.get ()));
12337 }
12338 else if (b->type == bp_dprintf)
12339 {
12340 printf_filtered (_(" (%s,%s) pending."),
12341 event_location_to_string (b->location.get ()),
12342 b->extra_string);
12343 }
12344 else
12345 {
12346 printf_filtered (_(" (%s %s) pending."),
12347 event_location_to_string (b->location.get ()),
12348 b->extra_string);
12349 }
12350 }
12351 else
12352 {
12353 if (opts.addressprint || b->loc->symtab == NULL)
12354 printf_filtered (" at %ps",
12355 styled_string (address_style.style (),
12356 paddress (b->loc->gdbarch,
12357 b->loc->address)));
12358 if (b->loc->symtab != NULL)
12359 {
12360 /* If there is a single location, we can print the location
12361 more nicely. */
12362 if (b->loc->next == NULL)
12363 {
12364 const char *filename
12365 = symtab_to_filename_for_display (b->loc->symtab);
12366 printf_filtered (": file %ps, line %d.",
12367 styled_string (file_name_style.style (),
12368 filename),
12369 b->loc->line_number);
12370 }
12371 else
12372 /* This is not ideal, but each location may have a
12373 different file name, and this at least reflects the
12374 real situation somewhat. */
12375 printf_filtered (": %s.",
12376 event_location_to_string (b->location.get ()));
12377 }
12378
12379 if (b->loc->next)
12380 {
12381 struct bp_location *loc = b->loc;
12382 int n = 0;
12383 for (; loc; loc = loc->next)
12384 ++n;
12385 printf_filtered (" (%d locations)", n);
12386 }
12387 }
12388 }
12389
12390 bp_location::~bp_location ()
12391 {
12392 xfree (function_name);
12393 }
12394
12395 /* Destructor for the breakpoint base class. */
12396
12397 breakpoint::~breakpoint ()
12398 {
12399 xfree (this->cond_string);
12400 xfree (this->extra_string);
12401 }
12402
12403 static struct bp_location *
12404 base_breakpoint_allocate_location (struct breakpoint *self)
12405 {
12406 return new bp_location (self);
12407 }
12408
12409 static void
12410 base_breakpoint_re_set (struct breakpoint *b)
12411 {
12412 /* Nothing to re-set. */
12413 }
12414
12415 #define internal_error_pure_virtual_called() \
12416 gdb_assert_not_reached ("pure virtual function called")
12417
12418 static int
12419 base_breakpoint_insert_location (struct bp_location *bl)
12420 {
12421 internal_error_pure_virtual_called ();
12422 }
12423
12424 static int
12425 base_breakpoint_remove_location (struct bp_location *bl,
12426 enum remove_bp_reason reason)
12427 {
12428 internal_error_pure_virtual_called ();
12429 }
12430
12431 static int
12432 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12433 const address_space *aspace,
12434 CORE_ADDR bp_addr,
12435 const struct target_waitstatus *ws)
12436 {
12437 internal_error_pure_virtual_called ();
12438 }
12439
12440 static void
12441 base_breakpoint_check_status (bpstat bs)
12442 {
12443 /* Always stop. */
12444 }
12445
12446 /* A "works_in_software_mode" breakpoint_ops method that just internal
12447 errors. */
12448
12449 static int
12450 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12451 {
12452 internal_error_pure_virtual_called ();
12453 }
12454
12455 /* A "resources_needed" breakpoint_ops method that just internal
12456 errors. */
12457
12458 static int
12459 base_breakpoint_resources_needed (const struct bp_location *bl)
12460 {
12461 internal_error_pure_virtual_called ();
12462 }
12463
12464 static enum print_stop_action
12465 base_breakpoint_print_it (bpstat bs)
12466 {
12467 internal_error_pure_virtual_called ();
12468 }
12469
12470 static void
12471 base_breakpoint_print_one_detail (const struct breakpoint *self,
12472 struct ui_out *uiout)
12473 {
12474 /* nothing */
12475 }
12476
12477 static void
12478 base_breakpoint_print_mention (struct breakpoint *b)
12479 {
12480 internal_error_pure_virtual_called ();
12481 }
12482
12483 static void
12484 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12485 {
12486 internal_error_pure_virtual_called ();
12487 }
12488
12489 static void
12490 base_breakpoint_create_sals_from_location
12491 (struct event_location *location,
12492 struct linespec_result *canonical,
12493 enum bptype type_wanted)
12494 {
12495 internal_error_pure_virtual_called ();
12496 }
12497
12498 static void
12499 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12500 struct linespec_result *c,
12501 gdb::unique_xmalloc_ptr<char> cond_string,
12502 gdb::unique_xmalloc_ptr<char> extra_string,
12503 enum bptype type_wanted,
12504 enum bpdisp disposition,
12505 int thread,
12506 int task, int ignore_count,
12507 const struct breakpoint_ops *o,
12508 int from_tty, int enabled,
12509 int internal, unsigned flags)
12510 {
12511 internal_error_pure_virtual_called ();
12512 }
12513
12514 static std::vector<symtab_and_line>
12515 base_breakpoint_decode_location (struct breakpoint *b,
12516 struct event_location *location,
12517 struct program_space *search_pspace)
12518 {
12519 internal_error_pure_virtual_called ();
12520 }
12521
12522 /* The default 'explains_signal' method. */
12523
12524 static int
12525 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12526 {
12527 return 1;
12528 }
12529
12530 /* The default "after_condition_true" method. */
12531
12532 static void
12533 base_breakpoint_after_condition_true (struct bpstats *bs)
12534 {
12535 /* Nothing to do. */
12536 }
12537
12538 struct breakpoint_ops base_breakpoint_ops =
12539 {
12540 base_breakpoint_allocate_location,
12541 base_breakpoint_re_set,
12542 base_breakpoint_insert_location,
12543 base_breakpoint_remove_location,
12544 base_breakpoint_breakpoint_hit,
12545 base_breakpoint_check_status,
12546 base_breakpoint_resources_needed,
12547 base_breakpoint_works_in_software_mode,
12548 base_breakpoint_print_it,
12549 NULL,
12550 base_breakpoint_print_one_detail,
12551 base_breakpoint_print_mention,
12552 base_breakpoint_print_recreate,
12553 base_breakpoint_create_sals_from_location,
12554 base_breakpoint_create_breakpoints_sal,
12555 base_breakpoint_decode_location,
12556 base_breakpoint_explains_signal,
12557 base_breakpoint_after_condition_true,
12558 };
12559
12560 /* Default breakpoint_ops methods. */
12561
12562 static void
12563 bkpt_re_set (struct breakpoint *b)
12564 {
12565 /* FIXME: is this still reachable? */
12566 if (breakpoint_event_location_empty_p (b))
12567 {
12568 /* Anything without a location can't be re-set. */
12569 delete_breakpoint (b);
12570 return;
12571 }
12572
12573 breakpoint_re_set_default (b);
12574 }
12575
12576 static int
12577 bkpt_insert_location (struct bp_location *bl)
12578 {
12579 CORE_ADDR addr = bl->target_info.reqstd_address;
12580
12581 bl->target_info.kind = breakpoint_kind (bl, &addr);
12582 bl->target_info.placed_address = addr;
12583
12584 if (bl->loc_type == bp_loc_hardware_breakpoint)
12585 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12586 else
12587 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12588 }
12589
12590 static int
12591 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12592 {
12593 if (bl->loc_type == bp_loc_hardware_breakpoint)
12594 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12595 else
12596 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12597 }
12598
12599 static int
12600 bkpt_breakpoint_hit (const struct bp_location *bl,
12601 const address_space *aspace, CORE_ADDR bp_addr,
12602 const struct target_waitstatus *ws)
12603 {
12604 if (ws->kind != TARGET_WAITKIND_STOPPED
12605 || ws->value.sig != GDB_SIGNAL_TRAP)
12606 return 0;
12607
12608 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12609 aspace, bp_addr))
12610 return 0;
12611
12612 if (overlay_debugging /* unmapped overlay section */
12613 && section_is_overlay (bl->section)
12614 && !section_is_mapped (bl->section))
12615 return 0;
12616
12617 return 1;
12618 }
12619
12620 static int
12621 dprintf_breakpoint_hit (const struct bp_location *bl,
12622 const address_space *aspace, CORE_ADDR bp_addr,
12623 const struct target_waitstatus *ws)
12624 {
12625 if (dprintf_style == dprintf_style_agent
12626 && target_can_run_breakpoint_commands ())
12627 {
12628 /* An agent-style dprintf never causes a stop. If we see a trap
12629 for this address it must be for a breakpoint that happens to
12630 be set at the same address. */
12631 return 0;
12632 }
12633
12634 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12635 }
12636
12637 static int
12638 bkpt_resources_needed (const struct bp_location *bl)
12639 {
12640 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12641
12642 return 1;
12643 }
12644
12645 static enum print_stop_action
12646 bkpt_print_it (bpstat bs)
12647 {
12648 struct breakpoint *b;
12649 const struct bp_location *bl;
12650 int bp_temp;
12651 struct ui_out *uiout = current_uiout;
12652
12653 gdb_assert (bs->bp_location_at != NULL);
12654
12655 bl = bs->bp_location_at.get ();
12656 b = bs->breakpoint_at;
12657
12658 bp_temp = b->disposition == disp_del;
12659 if (bl->address != bl->requested_address)
12660 breakpoint_adjustment_warning (bl->requested_address,
12661 bl->address,
12662 b->number, 1);
12663 annotate_breakpoint (b->number);
12664 maybe_print_thread_hit_breakpoint (uiout);
12665
12666 if (uiout->is_mi_like_p ())
12667 {
12668 uiout->field_string ("reason",
12669 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12670 uiout->field_string ("disp", bpdisp_text (b->disposition));
12671 }
12672 if (bp_temp)
12673 uiout->message ("Temporary breakpoint %pF, ",
12674 signed_field ("bkptno", b->number));
12675 else
12676 uiout->message ("Breakpoint %pF, ",
12677 signed_field ("bkptno", b->number));
12678
12679 return PRINT_SRC_AND_LOC;
12680 }
12681
12682 static void
12683 bkpt_print_mention (struct breakpoint *b)
12684 {
12685 if (current_uiout->is_mi_like_p ())
12686 return;
12687
12688 switch (b->type)
12689 {
12690 case bp_breakpoint:
12691 case bp_gnu_ifunc_resolver:
12692 if (b->disposition == disp_del)
12693 printf_filtered (_("Temporary breakpoint"));
12694 else
12695 printf_filtered (_("Breakpoint"));
12696 printf_filtered (_(" %d"), b->number);
12697 if (b->type == bp_gnu_ifunc_resolver)
12698 printf_filtered (_(" at gnu-indirect-function resolver"));
12699 break;
12700 case bp_hardware_breakpoint:
12701 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12702 break;
12703 case bp_dprintf:
12704 printf_filtered (_("Dprintf %d"), b->number);
12705 break;
12706 }
12707
12708 say_where (b);
12709 }
12710
12711 static void
12712 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12713 {
12714 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12715 fprintf_unfiltered (fp, "tbreak");
12716 else if (tp->type == bp_breakpoint)
12717 fprintf_unfiltered (fp, "break");
12718 else if (tp->type == bp_hardware_breakpoint
12719 && tp->disposition == disp_del)
12720 fprintf_unfiltered (fp, "thbreak");
12721 else if (tp->type == bp_hardware_breakpoint)
12722 fprintf_unfiltered (fp, "hbreak");
12723 else
12724 internal_error (__FILE__, __LINE__,
12725 _("unhandled breakpoint type %d"), (int) tp->type);
12726
12727 fprintf_unfiltered (fp, " %s",
12728 event_location_to_string (tp->location.get ()));
12729
12730 /* Print out extra_string if this breakpoint is pending. It might
12731 contain, for example, conditions that were set by the user. */
12732 if (tp->loc == NULL && tp->extra_string != NULL)
12733 fprintf_unfiltered (fp, " %s", tp->extra_string);
12734
12735 print_recreate_thread (tp, fp);
12736 }
12737
12738 static void
12739 bkpt_create_sals_from_location (struct event_location *location,
12740 struct linespec_result *canonical,
12741 enum bptype type_wanted)
12742 {
12743 create_sals_from_location_default (location, canonical, type_wanted);
12744 }
12745
12746 static void
12747 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12748 struct linespec_result *canonical,
12749 gdb::unique_xmalloc_ptr<char> cond_string,
12750 gdb::unique_xmalloc_ptr<char> extra_string,
12751 enum bptype type_wanted,
12752 enum bpdisp disposition,
12753 int thread,
12754 int task, int ignore_count,
12755 const struct breakpoint_ops *ops,
12756 int from_tty, int enabled,
12757 int internal, unsigned flags)
12758 {
12759 create_breakpoints_sal_default (gdbarch, canonical,
12760 std::move (cond_string),
12761 std::move (extra_string),
12762 type_wanted,
12763 disposition, thread, task,
12764 ignore_count, ops, from_tty,
12765 enabled, internal, flags);
12766 }
12767
12768 static std::vector<symtab_and_line>
12769 bkpt_decode_location (struct breakpoint *b,
12770 struct event_location *location,
12771 struct program_space *search_pspace)
12772 {
12773 return decode_location_default (b, location, search_pspace);
12774 }
12775
12776 /* Virtual table for internal breakpoints. */
12777
12778 static void
12779 internal_bkpt_re_set (struct breakpoint *b)
12780 {
12781 switch (b->type)
12782 {
12783 /* Delete overlay event and longjmp master breakpoints; they
12784 will be reset later by breakpoint_re_set. */
12785 case bp_overlay_event:
12786 case bp_longjmp_master:
12787 case bp_std_terminate_master:
12788 case bp_exception_master:
12789 delete_breakpoint (b);
12790 break;
12791
12792 /* This breakpoint is special, it's set up when the inferior
12793 starts and we really don't want to touch it. */
12794 case bp_shlib_event:
12795
12796 /* Like bp_shlib_event, this breakpoint type is special. Once
12797 it is set up, we do not want to touch it. */
12798 case bp_thread_event:
12799 break;
12800 }
12801 }
12802
12803 static void
12804 internal_bkpt_check_status (bpstat bs)
12805 {
12806 if (bs->breakpoint_at->type == bp_shlib_event)
12807 {
12808 /* If requested, stop when the dynamic linker notifies GDB of
12809 events. This allows the user to get control and place
12810 breakpoints in initializer routines for dynamically loaded
12811 objects (among other things). */
12812 bs->stop = stop_on_solib_events;
12813 bs->print = stop_on_solib_events;
12814 }
12815 else
12816 bs->stop = 0;
12817 }
12818
12819 static enum print_stop_action
12820 internal_bkpt_print_it (bpstat bs)
12821 {
12822 struct breakpoint *b;
12823
12824 b = bs->breakpoint_at;
12825
12826 switch (b->type)
12827 {
12828 case bp_shlib_event:
12829 /* Did we stop because the user set the stop_on_solib_events
12830 variable? (If so, we report this as a generic, "Stopped due
12831 to shlib event" message.) */
12832 print_solib_event (0);
12833 break;
12834
12835 case bp_thread_event:
12836 /* Not sure how we will get here.
12837 GDB should not stop for these breakpoints. */
12838 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12839 break;
12840
12841 case bp_overlay_event:
12842 /* By analogy with the thread event, GDB should not stop for these. */
12843 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12844 break;
12845
12846 case bp_longjmp_master:
12847 /* These should never be enabled. */
12848 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12849 break;
12850
12851 case bp_std_terminate_master:
12852 /* These should never be enabled. */
12853 printf_filtered (_("std::terminate Master Breakpoint: "
12854 "gdb should not stop!\n"));
12855 break;
12856
12857 case bp_exception_master:
12858 /* These should never be enabled. */
12859 printf_filtered (_("Exception Master Breakpoint: "
12860 "gdb should not stop!\n"));
12861 break;
12862 }
12863
12864 return PRINT_NOTHING;
12865 }
12866
12867 static void
12868 internal_bkpt_print_mention (struct breakpoint *b)
12869 {
12870 /* Nothing to mention. These breakpoints are internal. */
12871 }
12872
12873 /* Virtual table for momentary breakpoints */
12874
12875 static void
12876 momentary_bkpt_re_set (struct breakpoint *b)
12877 {
12878 /* Keep temporary breakpoints, which can be encountered when we step
12879 over a dlopen call and solib_add is resetting the breakpoints.
12880 Otherwise these should have been blown away via the cleanup chain
12881 or by breakpoint_init_inferior when we rerun the executable. */
12882 }
12883
12884 static void
12885 momentary_bkpt_check_status (bpstat bs)
12886 {
12887 /* Nothing. The point of these breakpoints is causing a stop. */
12888 }
12889
12890 static enum print_stop_action
12891 momentary_bkpt_print_it (bpstat bs)
12892 {
12893 return PRINT_UNKNOWN;
12894 }
12895
12896 static void
12897 momentary_bkpt_print_mention (struct breakpoint *b)
12898 {
12899 /* Nothing to mention. These breakpoints are internal. */
12900 }
12901
12902 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12903
12904 It gets cleared already on the removal of the first one of such placed
12905 breakpoints. This is OK as they get all removed altogether. */
12906
12907 longjmp_breakpoint::~longjmp_breakpoint ()
12908 {
12909 thread_info *tp = find_thread_global_id (this->thread);
12910
12911 if (tp != NULL)
12912 tp->initiating_frame = null_frame_id;
12913 }
12914
12915 /* Specific methods for probe breakpoints. */
12916
12917 static int
12918 bkpt_probe_insert_location (struct bp_location *bl)
12919 {
12920 int v = bkpt_insert_location (bl);
12921
12922 if (v == 0)
12923 {
12924 /* The insertion was successful, now let's set the probe's semaphore
12925 if needed. */
12926 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
12927 }
12928
12929 return v;
12930 }
12931
12932 static int
12933 bkpt_probe_remove_location (struct bp_location *bl,
12934 enum remove_bp_reason reason)
12935 {
12936 /* Let's clear the semaphore before removing the location. */
12937 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
12938
12939 return bkpt_remove_location (bl, reason);
12940 }
12941
12942 static void
12943 bkpt_probe_create_sals_from_location (struct event_location *location,
12944 struct linespec_result *canonical,
12945 enum bptype type_wanted)
12946 {
12947 struct linespec_sals lsal;
12948
12949 lsal.sals = parse_probes (location, NULL, canonical);
12950 lsal.canonical
12951 = xstrdup (event_location_to_string (canonical->location.get ()));
12952 canonical->lsals.push_back (std::move (lsal));
12953 }
12954
12955 static std::vector<symtab_and_line>
12956 bkpt_probe_decode_location (struct breakpoint *b,
12957 struct event_location *location,
12958 struct program_space *search_pspace)
12959 {
12960 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12961 if (sals.empty ())
12962 error (_("probe not found"));
12963 return sals;
12964 }
12965
12966 /* The breakpoint_ops structure to be used in tracepoints. */
12967
12968 static void
12969 tracepoint_re_set (struct breakpoint *b)
12970 {
12971 breakpoint_re_set_default (b);
12972 }
12973
12974 static int
12975 tracepoint_breakpoint_hit (const struct bp_location *bl,
12976 const address_space *aspace, CORE_ADDR bp_addr,
12977 const struct target_waitstatus *ws)
12978 {
12979 /* By definition, the inferior does not report stops at
12980 tracepoints. */
12981 return 0;
12982 }
12983
12984 static void
12985 tracepoint_print_one_detail (const struct breakpoint *self,
12986 struct ui_out *uiout)
12987 {
12988 struct tracepoint *tp = (struct tracepoint *) self;
12989 if (!tp->static_trace_marker_id.empty ())
12990 {
12991 gdb_assert (self->type == bp_static_tracepoint);
12992
12993 uiout->message ("\tmarker id is %pF\n",
12994 string_field ("static-tracepoint-marker-string-id",
12995 tp->static_trace_marker_id.c_str ()));
12996 }
12997 }
12998
12999 static void
13000 tracepoint_print_mention (struct breakpoint *b)
13001 {
13002 if (current_uiout->is_mi_like_p ())
13003 return;
13004
13005 switch (b->type)
13006 {
13007 case bp_tracepoint:
13008 printf_filtered (_("Tracepoint"));
13009 printf_filtered (_(" %d"), b->number);
13010 break;
13011 case bp_fast_tracepoint:
13012 printf_filtered (_("Fast tracepoint"));
13013 printf_filtered (_(" %d"), b->number);
13014 break;
13015 case bp_static_tracepoint:
13016 printf_filtered (_("Static tracepoint"));
13017 printf_filtered (_(" %d"), b->number);
13018 break;
13019 default:
13020 internal_error (__FILE__, __LINE__,
13021 _("unhandled tracepoint type %d"), (int) b->type);
13022 }
13023
13024 say_where (b);
13025 }
13026
13027 static void
13028 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13029 {
13030 struct tracepoint *tp = (struct tracepoint *) self;
13031
13032 if (self->type == bp_fast_tracepoint)
13033 fprintf_unfiltered (fp, "ftrace");
13034 else if (self->type == bp_static_tracepoint)
13035 fprintf_unfiltered (fp, "strace");
13036 else if (self->type == bp_tracepoint)
13037 fprintf_unfiltered (fp, "trace");
13038 else
13039 internal_error (__FILE__, __LINE__,
13040 _("unhandled tracepoint type %d"), (int) self->type);
13041
13042 fprintf_unfiltered (fp, " %s",
13043 event_location_to_string (self->location.get ()));
13044 print_recreate_thread (self, fp);
13045
13046 if (tp->pass_count)
13047 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13048 }
13049
13050 static void
13051 tracepoint_create_sals_from_location (struct event_location *location,
13052 struct linespec_result *canonical,
13053 enum bptype type_wanted)
13054 {
13055 create_sals_from_location_default (location, canonical, type_wanted);
13056 }
13057
13058 static void
13059 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13060 struct linespec_result *canonical,
13061 gdb::unique_xmalloc_ptr<char> cond_string,
13062 gdb::unique_xmalloc_ptr<char> extra_string,
13063 enum bptype type_wanted,
13064 enum bpdisp disposition,
13065 int thread,
13066 int task, int ignore_count,
13067 const struct breakpoint_ops *ops,
13068 int from_tty, int enabled,
13069 int internal, unsigned flags)
13070 {
13071 create_breakpoints_sal_default (gdbarch, canonical,
13072 std::move (cond_string),
13073 std::move (extra_string),
13074 type_wanted,
13075 disposition, thread, task,
13076 ignore_count, ops, from_tty,
13077 enabled, internal, flags);
13078 }
13079
13080 static std::vector<symtab_and_line>
13081 tracepoint_decode_location (struct breakpoint *b,
13082 struct event_location *location,
13083 struct program_space *search_pspace)
13084 {
13085 return decode_location_default (b, location, search_pspace);
13086 }
13087
13088 struct breakpoint_ops tracepoint_breakpoint_ops;
13089
13090 /* Virtual table for tracepoints on static probes. */
13091
13092 static void
13093 tracepoint_probe_create_sals_from_location
13094 (struct event_location *location,
13095 struct linespec_result *canonical,
13096 enum bptype type_wanted)
13097 {
13098 /* We use the same method for breakpoint on probes. */
13099 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13100 }
13101
13102 static std::vector<symtab_and_line>
13103 tracepoint_probe_decode_location (struct breakpoint *b,
13104 struct event_location *location,
13105 struct program_space *search_pspace)
13106 {
13107 /* We use the same method for breakpoint on probes. */
13108 return bkpt_probe_decode_location (b, location, search_pspace);
13109 }
13110
13111 /* Dprintf breakpoint_ops methods. */
13112
13113 static void
13114 dprintf_re_set (struct breakpoint *b)
13115 {
13116 breakpoint_re_set_default (b);
13117
13118 /* extra_string should never be non-NULL for dprintf. */
13119 gdb_assert (b->extra_string != NULL);
13120
13121 /* 1 - connect to target 1, that can run breakpoint commands.
13122 2 - create a dprintf, which resolves fine.
13123 3 - disconnect from target 1
13124 4 - connect to target 2, that can NOT run breakpoint commands.
13125
13126 After steps #3/#4, you'll want the dprintf command list to
13127 be updated, because target 1 and 2 may well return different
13128 answers for target_can_run_breakpoint_commands().
13129 Given absence of finer grained resetting, we get to do
13130 it all the time. */
13131 if (b->extra_string != NULL)
13132 update_dprintf_command_list (b);
13133 }
13134
13135 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13136
13137 static void
13138 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13139 {
13140 fprintf_unfiltered (fp, "dprintf %s,%s",
13141 event_location_to_string (tp->location.get ()),
13142 tp->extra_string);
13143 print_recreate_thread (tp, fp);
13144 }
13145
13146 /* Implement the "after_condition_true" breakpoint_ops method for
13147 dprintf.
13148
13149 dprintf's are implemented with regular commands in their command
13150 list, but we run the commands here instead of before presenting the
13151 stop to the user, as dprintf's don't actually cause a stop. This
13152 also makes it so that the commands of multiple dprintfs at the same
13153 address are all handled. */
13154
13155 static void
13156 dprintf_after_condition_true (struct bpstats *bs)
13157 {
13158 struct bpstats tmp_bs;
13159 struct bpstats *tmp_bs_p = &tmp_bs;
13160
13161 /* dprintf's never cause a stop. This wasn't set in the
13162 check_status hook instead because that would make the dprintf's
13163 condition not be evaluated. */
13164 bs->stop = 0;
13165
13166 /* Run the command list here. Take ownership of it instead of
13167 copying. We never want these commands to run later in
13168 bpstat_do_actions, if a breakpoint that causes a stop happens to
13169 be set at same address as this dprintf, or even if running the
13170 commands here throws. */
13171 tmp_bs.commands = bs->commands;
13172 bs->commands = NULL;
13173
13174 bpstat_do_actions_1 (&tmp_bs_p);
13175
13176 /* 'tmp_bs.commands' will usually be NULL by now, but
13177 bpstat_do_actions_1 may return early without processing the whole
13178 list. */
13179 }
13180
13181 /* The breakpoint_ops structure to be used on static tracepoints with
13182 markers (`-m'). */
13183
13184 static void
13185 strace_marker_create_sals_from_location (struct event_location *location,
13186 struct linespec_result *canonical,
13187 enum bptype type_wanted)
13188 {
13189 struct linespec_sals lsal;
13190 const char *arg_start, *arg;
13191
13192 arg = arg_start = get_linespec_location (location)->spec_string;
13193 lsal.sals = decode_static_tracepoint_spec (&arg);
13194
13195 std::string str (arg_start, arg - arg_start);
13196 const char *ptr = str.c_str ();
13197 canonical->location
13198 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
13199
13200 lsal.canonical
13201 = xstrdup (event_location_to_string (canonical->location.get ()));
13202 canonical->lsals.push_back (std::move (lsal));
13203 }
13204
13205 static void
13206 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13207 struct linespec_result *canonical,
13208 gdb::unique_xmalloc_ptr<char> cond_string,
13209 gdb::unique_xmalloc_ptr<char> extra_string,
13210 enum bptype type_wanted,
13211 enum bpdisp disposition,
13212 int thread,
13213 int task, int ignore_count,
13214 const struct breakpoint_ops *ops,
13215 int from_tty, int enabled,
13216 int internal, unsigned flags)
13217 {
13218 const linespec_sals &lsal = canonical->lsals[0];
13219
13220 /* If the user is creating a static tracepoint by marker id
13221 (strace -m MARKER_ID), then store the sals index, so that
13222 breakpoint_re_set can try to match up which of the newly
13223 found markers corresponds to this one, and, don't try to
13224 expand multiple locations for each sal, given than SALS
13225 already should contain all sals for MARKER_ID. */
13226
13227 for (size_t i = 0; i < lsal.sals.size (); i++)
13228 {
13229 event_location_up location
13230 = copy_event_location (canonical->location.get ());
13231
13232 std::unique_ptr<tracepoint> tp (new tracepoint ());
13233 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
13234 std::move (location), NULL,
13235 std::move (cond_string),
13236 std::move (extra_string),
13237 type_wanted, disposition,
13238 thread, task, ignore_count, ops,
13239 from_tty, enabled, internal, flags,
13240 canonical->special_display);
13241 /* Given that its possible to have multiple markers with
13242 the same string id, if the user is creating a static
13243 tracepoint by marker id ("strace -m MARKER_ID"), then
13244 store the sals index, so that breakpoint_re_set can
13245 try to match up which of the newly found markers
13246 corresponds to this one */
13247 tp->static_trace_marker_id_idx = i;
13248
13249 install_breakpoint (internal, std::move (tp), 0);
13250 }
13251 }
13252
13253 static std::vector<symtab_and_line>
13254 strace_marker_decode_location (struct breakpoint *b,
13255 struct event_location *location,
13256 struct program_space *search_pspace)
13257 {
13258 struct tracepoint *tp = (struct tracepoint *) b;
13259 const char *s = get_linespec_location (location)->spec_string;
13260
13261 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13262 if (sals.size () > tp->static_trace_marker_id_idx)
13263 {
13264 sals[0] = sals[tp->static_trace_marker_id_idx];
13265 sals.resize (1);
13266 return sals;
13267 }
13268 else
13269 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
13270 }
13271
13272 static struct breakpoint_ops strace_marker_breakpoint_ops;
13273
13274 static int
13275 strace_marker_p (struct breakpoint *b)
13276 {
13277 return b->ops == &strace_marker_breakpoint_ops;
13278 }
13279
13280 /* Delete a breakpoint and clean up all traces of it in the data
13281 structures. */
13282
13283 void
13284 delete_breakpoint (struct breakpoint *bpt)
13285 {
13286 struct breakpoint *b;
13287
13288 gdb_assert (bpt != NULL);
13289
13290 /* Has this bp already been deleted? This can happen because
13291 multiple lists can hold pointers to bp's. bpstat lists are
13292 especial culprits.
13293
13294 One example of this happening is a watchpoint's scope bp. When
13295 the scope bp triggers, we notice that the watchpoint is out of
13296 scope, and delete it. We also delete its scope bp. But the
13297 scope bp is marked "auto-deleting", and is already on a bpstat.
13298 That bpstat is then checked for auto-deleting bp's, which are
13299 deleted.
13300
13301 A real solution to this problem might involve reference counts in
13302 bp's, and/or giving them pointers back to their referencing
13303 bpstat's, and teaching delete_breakpoint to only free a bp's
13304 storage when no more references were extent. A cheaper bandaid
13305 was chosen. */
13306 if (bpt->type == bp_none)
13307 return;
13308
13309 /* At least avoid this stale reference until the reference counting
13310 of breakpoints gets resolved. */
13311 if (bpt->related_breakpoint != bpt)
13312 {
13313 struct breakpoint *related;
13314 struct watchpoint *w;
13315
13316 if (bpt->type == bp_watchpoint_scope)
13317 w = (struct watchpoint *) bpt->related_breakpoint;
13318 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13319 w = (struct watchpoint *) bpt;
13320 else
13321 w = NULL;
13322 if (w != NULL)
13323 watchpoint_del_at_next_stop (w);
13324
13325 /* Unlink bpt from the bpt->related_breakpoint ring. */
13326 for (related = bpt; related->related_breakpoint != bpt;
13327 related = related->related_breakpoint);
13328 related->related_breakpoint = bpt->related_breakpoint;
13329 bpt->related_breakpoint = bpt;
13330 }
13331
13332 /* watch_command_1 creates a watchpoint but only sets its number if
13333 update_watchpoint succeeds in creating its bp_locations. If there's
13334 a problem in that process, we'll be asked to delete the half-created
13335 watchpoint. In that case, don't announce the deletion. */
13336 if (bpt->number)
13337 gdb::observers::breakpoint_deleted.notify (bpt);
13338
13339 if (breakpoint_chain == bpt)
13340 breakpoint_chain = bpt->next;
13341
13342 ALL_BREAKPOINTS (b)
13343 if (b->next == bpt)
13344 {
13345 b->next = bpt->next;
13346 break;
13347 }
13348
13349 /* Be sure no bpstat's are pointing at the breakpoint after it's
13350 been freed. */
13351 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13352 in all threads for now. Note that we cannot just remove bpstats
13353 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13354 commands are associated with the bpstat; if we remove it here,
13355 then the later call to bpstat_do_actions (&stop_bpstat); in
13356 event-top.c won't do anything, and temporary breakpoints with
13357 commands won't work. */
13358
13359 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13360
13361 /* Now that breakpoint is removed from breakpoint list, update the
13362 global location list. This will remove locations that used to
13363 belong to this breakpoint. Do this before freeing the breakpoint
13364 itself, since remove_breakpoint looks at location's owner. It
13365 might be better design to have location completely
13366 self-contained, but it's not the case now. */
13367 update_global_location_list (UGLL_DONT_INSERT);
13368
13369 /* On the chance that someone will soon try again to delete this
13370 same bp, we mark it as deleted before freeing its storage. */
13371 bpt->type = bp_none;
13372 delete bpt;
13373 }
13374
13375 /* Iterator function to call a user-provided callback function once
13376 for each of B and its related breakpoints. */
13377
13378 static void
13379 iterate_over_related_breakpoints (struct breakpoint *b,
13380 gdb::function_view<void (breakpoint *)> function)
13381 {
13382 struct breakpoint *related;
13383
13384 related = b;
13385 do
13386 {
13387 struct breakpoint *next;
13388
13389 /* FUNCTION may delete RELATED. */
13390 next = related->related_breakpoint;
13391
13392 if (next == related)
13393 {
13394 /* RELATED is the last ring entry. */
13395 function (related);
13396
13397 /* FUNCTION may have deleted it, so we'd never reach back to
13398 B. There's nothing left to do anyway, so just break
13399 out. */
13400 break;
13401 }
13402 else
13403 function (related);
13404
13405 related = next;
13406 }
13407 while (related != b);
13408 }
13409
13410 static void
13411 delete_command (const char *arg, int from_tty)
13412 {
13413 struct breakpoint *b, *b_tmp;
13414
13415 dont_repeat ();
13416
13417 if (arg == 0)
13418 {
13419 int breaks_to_delete = 0;
13420
13421 /* Delete all breakpoints if no argument. Do not delete
13422 internal breakpoints, these have to be deleted with an
13423 explicit breakpoint number argument. */
13424 ALL_BREAKPOINTS (b)
13425 if (user_breakpoint_p (b))
13426 {
13427 breaks_to_delete = 1;
13428 break;
13429 }
13430
13431 /* Ask user only if there are some breakpoints to delete. */
13432 if (!from_tty
13433 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13434 {
13435 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13436 if (user_breakpoint_p (b))
13437 delete_breakpoint (b);
13438 }
13439 }
13440 else
13441 map_breakpoint_numbers
13442 (arg, [&] (breakpoint *br)
13443 {
13444 iterate_over_related_breakpoints (br, delete_breakpoint);
13445 });
13446 }
13447
13448 /* Return true if all locations of B bound to PSPACE are pending. If
13449 PSPACE is NULL, all locations of all program spaces are
13450 considered. */
13451
13452 static int
13453 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13454 {
13455 struct bp_location *loc;
13456
13457 for (loc = b->loc; loc != NULL; loc = loc->next)
13458 if ((pspace == NULL
13459 || loc->pspace == pspace)
13460 && !loc->shlib_disabled
13461 && !loc->pspace->executing_startup)
13462 return 0;
13463 return 1;
13464 }
13465
13466 /* Subroutine of update_breakpoint_locations to simplify it.
13467 Return non-zero if multiple fns in list LOC have the same name.
13468 Null names are ignored. */
13469
13470 static int
13471 ambiguous_names_p (struct bp_location *loc)
13472 {
13473 struct bp_location *l;
13474 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
13475 xcalloc, xfree));
13476
13477 for (l = loc; l != NULL; l = l->next)
13478 {
13479 const char **slot;
13480 const char *name = l->function_name;
13481
13482 /* Allow for some names to be NULL, ignore them. */
13483 if (name == NULL)
13484 continue;
13485
13486 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
13487 INSERT);
13488 /* NOTE: We can assume slot != NULL here because xcalloc never
13489 returns NULL. */
13490 if (*slot != NULL)
13491 return 1;
13492 *slot = name;
13493 }
13494
13495 return 0;
13496 }
13497
13498 /* When symbols change, it probably means the sources changed as well,
13499 and it might mean the static tracepoint markers are no longer at
13500 the same address or line numbers they used to be at last we
13501 checked. Losing your static tracepoints whenever you rebuild is
13502 undesirable. This function tries to resync/rematch gdb static
13503 tracepoints with the markers on the target, for static tracepoints
13504 that have not been set by marker id. Static tracepoint that have
13505 been set by marker id are reset by marker id in breakpoint_re_set.
13506 The heuristic is:
13507
13508 1) For a tracepoint set at a specific address, look for a marker at
13509 the old PC. If one is found there, assume to be the same marker.
13510 If the name / string id of the marker found is different from the
13511 previous known name, assume that means the user renamed the marker
13512 in the sources, and output a warning.
13513
13514 2) For a tracepoint set at a given line number, look for a marker
13515 at the new address of the old line number. If one is found there,
13516 assume to be the same marker. If the name / string id of the
13517 marker found is different from the previous known name, assume that
13518 means the user renamed the marker in the sources, and output a
13519 warning.
13520
13521 3) If a marker is no longer found at the same address or line, it
13522 may mean the marker no longer exists. But it may also just mean
13523 the code changed a bit. Maybe the user added a few lines of code
13524 that made the marker move up or down (in line number terms). Ask
13525 the target for info about the marker with the string id as we knew
13526 it. If found, update line number and address in the matching
13527 static tracepoint. This will get confused if there's more than one
13528 marker with the same ID (possible in UST, although unadvised
13529 precisely because it confuses tools). */
13530
13531 static struct symtab_and_line
13532 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13533 {
13534 struct tracepoint *tp = (struct tracepoint *) b;
13535 struct static_tracepoint_marker marker;
13536 CORE_ADDR pc;
13537
13538 pc = sal.pc;
13539 if (sal.line)
13540 find_line_pc (sal.symtab, sal.line, &pc);
13541
13542 if (target_static_tracepoint_marker_at (pc, &marker))
13543 {
13544 if (tp->static_trace_marker_id != marker.str_id)
13545 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13546 b->number, tp->static_trace_marker_id.c_str (),
13547 marker.str_id.c_str ());
13548
13549 tp->static_trace_marker_id = std::move (marker.str_id);
13550
13551 return sal;
13552 }
13553
13554 /* Old marker wasn't found on target at lineno. Try looking it up
13555 by string ID. */
13556 if (!sal.explicit_pc
13557 && sal.line != 0
13558 && sal.symtab != NULL
13559 && !tp->static_trace_marker_id.empty ())
13560 {
13561 std::vector<static_tracepoint_marker> markers
13562 = target_static_tracepoint_markers_by_strid
13563 (tp->static_trace_marker_id.c_str ());
13564
13565 if (!markers.empty ())
13566 {
13567 struct symbol *sym;
13568 struct static_tracepoint_marker *tpmarker;
13569 struct ui_out *uiout = current_uiout;
13570 struct explicit_location explicit_loc;
13571
13572 tpmarker = &markers[0];
13573
13574 tp->static_trace_marker_id = std::move (tpmarker->str_id);
13575
13576 warning (_("marker for static tracepoint %d (%s) not "
13577 "found at previous line number"),
13578 b->number, tp->static_trace_marker_id.c_str ());
13579
13580 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
13581 sym = find_pc_sect_function (tpmarker->address, NULL);
13582 uiout->text ("Now in ");
13583 if (sym)
13584 {
13585 uiout->field_string ("func", sym->print_name (),
13586 function_name_style.style ());
13587 uiout->text (" at ");
13588 }
13589 uiout->field_string ("file",
13590 symtab_to_filename_for_display (sal2.symtab),
13591 file_name_style.style ());
13592 uiout->text (":");
13593
13594 if (uiout->is_mi_like_p ())
13595 {
13596 const char *fullname = symtab_to_fullname (sal2.symtab);
13597
13598 uiout->field_string ("fullname", fullname);
13599 }
13600
13601 uiout->field_signed ("line", sal2.line);
13602 uiout->text ("\n");
13603
13604 b->loc->line_number = sal2.line;
13605 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13606
13607 b->location.reset (NULL);
13608 initialize_explicit_location (&explicit_loc);
13609 explicit_loc.source_filename
13610 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13611 explicit_loc.line_offset.offset = b->loc->line_number;
13612 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13613 b->location = new_explicit_location (&explicit_loc);
13614
13615 /* Might be nice to check if function changed, and warn if
13616 so. */
13617 }
13618 }
13619 return sal;
13620 }
13621
13622 /* Returns 1 iff locations A and B are sufficiently same that
13623 we don't need to report breakpoint as changed. */
13624
13625 static int
13626 locations_are_equal (struct bp_location *a, struct bp_location *b)
13627 {
13628 while (a && b)
13629 {
13630 if (a->address != b->address)
13631 return 0;
13632
13633 if (a->shlib_disabled != b->shlib_disabled)
13634 return 0;
13635
13636 if (a->enabled != b->enabled)
13637 return 0;
13638
13639 if (a->disabled_by_cond != b->disabled_by_cond)
13640 return 0;
13641
13642 a = a->next;
13643 b = b->next;
13644 }
13645
13646 if ((a == NULL) != (b == NULL))
13647 return 0;
13648
13649 return 1;
13650 }
13651
13652 /* Split all locations of B that are bound to PSPACE out of B's
13653 location list to a separate list and return that list's head. If
13654 PSPACE is NULL, hoist out all locations of B. */
13655
13656 static struct bp_location *
13657 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13658 {
13659 struct bp_location head;
13660 struct bp_location *i = b->loc;
13661 struct bp_location **i_link = &b->loc;
13662 struct bp_location *hoisted = &head;
13663
13664 if (pspace == NULL)
13665 {
13666 i = b->loc;
13667 b->loc = NULL;
13668 return i;
13669 }
13670
13671 head.next = NULL;
13672
13673 while (i != NULL)
13674 {
13675 if (i->pspace == pspace)
13676 {
13677 *i_link = i->next;
13678 i->next = NULL;
13679 hoisted->next = i;
13680 hoisted = i;
13681 }
13682 else
13683 i_link = &i->next;
13684 i = *i_link;
13685 }
13686
13687 return head.next;
13688 }
13689
13690 /* Create new breakpoint locations for B (a hardware or software
13691 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13692 zero, then B is a ranged breakpoint. Only recreates locations for
13693 FILTER_PSPACE. Locations of other program spaces are left
13694 untouched. */
13695
13696 void
13697 update_breakpoint_locations (struct breakpoint *b,
13698 struct program_space *filter_pspace,
13699 gdb::array_view<const symtab_and_line> sals,
13700 gdb::array_view<const symtab_and_line> sals_end)
13701 {
13702 struct bp_location *existing_locations;
13703
13704 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
13705 {
13706 /* Ranged breakpoints have only one start location and one end
13707 location. */
13708 b->enable_state = bp_disabled;
13709 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13710 "multiple locations found\n"),
13711 b->number);
13712 return;
13713 }
13714
13715 /* If there's no new locations, and all existing locations are
13716 pending, don't do anything. This optimizes the common case where
13717 all locations are in the same shared library, that was unloaded.
13718 We'd like to retain the location, so that when the library is
13719 loaded again, we don't loose the enabled/disabled status of the
13720 individual locations. */
13721 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
13722 return;
13723
13724 existing_locations = hoist_existing_locations (b, filter_pspace);
13725
13726 for (const auto &sal : sals)
13727 {
13728 struct bp_location *new_loc;
13729
13730 switch_to_program_space_and_thread (sal.pspace);
13731
13732 new_loc = add_location_to_breakpoint (b, &sal);
13733
13734 /* Reparse conditions, they might contain references to the
13735 old symtab. */
13736 if (b->cond_string != NULL)
13737 {
13738 const char *s;
13739
13740 s = b->cond_string;
13741 try
13742 {
13743 new_loc->cond = parse_exp_1 (&s, sal.pc,
13744 block_for_pc (sal.pc),
13745 0);
13746 }
13747 catch (const gdb_exception_error &e)
13748 {
13749 new_loc->disabled_by_cond = true;
13750 }
13751 }
13752
13753 if (!sals_end.empty ())
13754 {
13755 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13756
13757 new_loc->length = end - sals[0].pc + 1;
13758 }
13759 }
13760
13761 /* If possible, carry over 'disable' status from existing
13762 breakpoints. */
13763 {
13764 struct bp_location *e = existing_locations;
13765 /* If there are multiple breakpoints with the same function name,
13766 e.g. for inline functions, comparing function names won't work.
13767 Instead compare pc addresses; this is just a heuristic as things
13768 may have moved, but in practice it gives the correct answer
13769 often enough until a better solution is found. */
13770 int have_ambiguous_names = ambiguous_names_p (b->loc);
13771
13772 for (; e; e = e->next)
13773 {
13774 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
13775 {
13776 struct bp_location *l = b->loc;
13777 if (have_ambiguous_names)
13778 {
13779 for (; l; l = l->next)
13780 {
13781 /* Ignore software vs hardware location type at
13782 this point, because with "set breakpoint
13783 auto-hw", after a re-set, locations that were
13784 hardware can end up as software, or vice versa.
13785 As mentioned above, this is an heuristic and in
13786 practice should give the correct answer often
13787 enough. */
13788 if (breakpoint_locations_match (e, l, true))
13789 {
13790 l->enabled = e->enabled;
13791 l->disabled_by_cond = e->disabled_by_cond;
13792 break;
13793 }
13794 }
13795 }
13796 else
13797 {
13798 for (; l; l = l->next)
13799 if (l->function_name
13800 && strcmp (e->function_name, l->function_name) == 0)
13801 {
13802 l->enabled = e->enabled;
13803 l->disabled_by_cond = e->disabled_by_cond;
13804 break;
13805 }
13806 }
13807 }
13808 }
13809 }
13810
13811 if (!locations_are_equal (existing_locations, b->loc))
13812 gdb::observers::breakpoint_modified.notify (b);
13813 }
13814
13815 /* Find the SaL locations corresponding to the given LOCATION.
13816 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13817
13818 static std::vector<symtab_and_line>
13819 location_to_sals (struct breakpoint *b, struct event_location *location,
13820 struct program_space *search_pspace, int *found)
13821 {
13822 struct gdb_exception exception;
13823
13824 gdb_assert (b->ops != NULL);
13825
13826 std::vector<symtab_and_line> sals;
13827
13828 try
13829 {
13830 sals = b->ops->decode_location (b, location, search_pspace);
13831 }
13832 catch (gdb_exception_error &e)
13833 {
13834 int not_found_and_ok = 0;
13835
13836 /* For pending breakpoints, it's expected that parsing will
13837 fail until the right shared library is loaded. User has
13838 already told to create pending breakpoints and don't need
13839 extra messages. If breakpoint is in bp_shlib_disabled
13840 state, then user already saw the message about that
13841 breakpoint being disabled, and don't want to see more
13842 errors. */
13843 if (e.error == NOT_FOUND_ERROR
13844 && (b->condition_not_parsed
13845 || (b->loc != NULL
13846 && search_pspace != NULL
13847 && b->loc->pspace != search_pspace)
13848 || (b->loc && b->loc->shlib_disabled)
13849 || (b->loc && b->loc->pspace->executing_startup)
13850 || b->enable_state == bp_disabled))
13851 not_found_and_ok = 1;
13852
13853 if (!not_found_and_ok)
13854 {
13855 /* We surely don't want to warn about the same breakpoint
13856 10 times. One solution, implemented here, is disable
13857 the breakpoint on error. Another solution would be to
13858 have separate 'warning emitted' flag. Since this
13859 happens only when a binary has changed, I don't know
13860 which approach is better. */
13861 b->enable_state = bp_disabled;
13862 throw;
13863 }
13864
13865 exception = std::move (e);
13866 }
13867
13868 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13869 {
13870 for (auto &sal : sals)
13871 resolve_sal_pc (&sal);
13872 if (b->condition_not_parsed && b->extra_string != NULL)
13873 {
13874 char *cond_string, *extra_string;
13875 int thread, task;
13876
13877 find_condition_and_thread_for_sals (sals, b->extra_string,
13878 &cond_string, &thread,
13879 &task, &extra_string);
13880 gdb_assert (b->cond_string == NULL);
13881 if (cond_string)
13882 b->cond_string = cond_string;
13883 b->thread = thread;
13884 b->task = task;
13885 if (extra_string)
13886 {
13887 xfree (b->extra_string);
13888 b->extra_string = extra_string;
13889 }
13890 b->condition_not_parsed = 0;
13891 }
13892
13893 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13894 sals[0] = update_static_tracepoint (b, sals[0]);
13895
13896 *found = 1;
13897 }
13898 else
13899 *found = 0;
13900
13901 return sals;
13902 }
13903
13904 /* The default re_set method, for typical hardware or software
13905 breakpoints. Reevaluate the breakpoint and recreate its
13906 locations. */
13907
13908 static void
13909 breakpoint_re_set_default (struct breakpoint *b)
13910 {
13911 struct program_space *filter_pspace = current_program_space;
13912 std::vector<symtab_and_line> expanded, expanded_end;
13913
13914 int found;
13915 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13916 filter_pspace, &found);
13917 if (found)
13918 expanded = std::move (sals);
13919
13920 if (b->location_range_end != NULL)
13921 {
13922 std::vector<symtab_and_line> sals_end
13923 = location_to_sals (b, b->location_range_end.get (),
13924 filter_pspace, &found);
13925 if (found)
13926 expanded_end = std::move (sals_end);
13927 }
13928
13929 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
13930 }
13931
13932 /* Default method for creating SALs from an address string. It basically
13933 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13934
13935 static void
13936 create_sals_from_location_default (struct event_location *location,
13937 struct linespec_result *canonical,
13938 enum bptype type_wanted)
13939 {
13940 parse_breakpoint_sals (location, canonical);
13941 }
13942
13943 /* Call create_breakpoints_sal for the given arguments. This is the default
13944 function for the `create_breakpoints_sal' method of
13945 breakpoint_ops. */
13946
13947 static void
13948 create_breakpoints_sal_default (struct gdbarch *gdbarch,
13949 struct linespec_result *canonical,
13950 gdb::unique_xmalloc_ptr<char> cond_string,
13951 gdb::unique_xmalloc_ptr<char> extra_string,
13952 enum bptype type_wanted,
13953 enum bpdisp disposition,
13954 int thread,
13955 int task, int ignore_count,
13956 const struct breakpoint_ops *ops,
13957 int from_tty, int enabled,
13958 int internal, unsigned flags)
13959 {
13960 create_breakpoints_sal (gdbarch, canonical,
13961 std::move (cond_string),
13962 std::move (extra_string),
13963 type_wanted, disposition,
13964 thread, task, ignore_count, ops, from_tty,
13965 enabled, internal, flags);
13966 }
13967
13968 /* Decode the line represented by S by calling decode_line_full. This is the
13969 default function for the `decode_location' method of breakpoint_ops. */
13970
13971 static std::vector<symtab_and_line>
13972 decode_location_default (struct breakpoint *b,
13973 struct event_location *location,
13974 struct program_space *search_pspace)
13975 {
13976 struct linespec_result canonical;
13977
13978 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
13979 NULL, 0, &canonical, multiple_symbols_all,
13980 b->filter.get ());
13981
13982 /* We should get 0 or 1 resulting SALs. */
13983 gdb_assert (canonical.lsals.size () < 2);
13984
13985 if (!canonical.lsals.empty ())
13986 {
13987 const linespec_sals &lsal = canonical.lsals[0];
13988 return std::move (lsal.sals);
13989 }
13990 return {};
13991 }
13992
13993 /* Reset a breakpoint. */
13994
13995 static void
13996 breakpoint_re_set_one (breakpoint *b)
13997 {
13998 input_radix = b->input_radix;
13999 set_language (b->language);
14000
14001 b->ops->re_set (b);
14002 }
14003
14004 /* Re-set breakpoint locations for the current program space.
14005 Locations bound to other program spaces are left untouched. */
14006
14007 void
14008 breakpoint_re_set (void)
14009 {
14010 struct breakpoint *b, *b_tmp;
14011
14012 {
14013 scoped_restore_current_language save_language;
14014 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
14015 scoped_restore_current_pspace_and_thread restore_pspace_thread;
14016
14017 /* breakpoint_re_set_one sets the current_language to the language
14018 of the breakpoint it is resetting (see prepare_re_set_context)
14019 before re-evaluating the breakpoint's location. This change can
14020 unfortunately get undone by accident if the language_mode is set
14021 to auto, and we either switch frames, or more likely in this context,
14022 we select the current frame.
14023
14024 We prevent this by temporarily turning the language_mode to
14025 language_mode_manual. We restore it once all breakpoints
14026 have been reset. */
14027 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
14028 language_mode = language_mode_manual;
14029
14030 /* Note: we must not try to insert locations until after all
14031 breakpoints have been re-set. Otherwise, e.g., when re-setting
14032 breakpoint 1, we'd insert the locations of breakpoint 2, which
14033 hadn't been re-set yet, and thus may have stale locations. */
14034
14035 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14036 {
14037 try
14038 {
14039 breakpoint_re_set_one (b);
14040 }
14041 catch (const gdb_exception &ex)
14042 {
14043 exception_fprintf (gdb_stderr, ex,
14044 "Error in re-setting breakpoint %d: ",
14045 b->number);
14046 }
14047 }
14048
14049 jit_breakpoint_re_set ();
14050 }
14051
14052 create_overlay_event_breakpoint ();
14053 create_longjmp_master_breakpoint ();
14054 create_std_terminate_master_breakpoint ();
14055 create_exception_master_breakpoint ();
14056
14057 /* Now we can insert. */
14058 update_global_location_list (UGLL_MAY_INSERT);
14059 }
14060 \f
14061 /* Reset the thread number of this breakpoint:
14062
14063 - If the breakpoint is for all threads, leave it as-is.
14064 - Else, reset it to the current thread for inferior_ptid. */
14065 void
14066 breakpoint_re_set_thread (struct breakpoint *b)
14067 {
14068 if (b->thread != -1)
14069 {
14070 b->thread = inferior_thread ()->global_num;
14071
14072 /* We're being called after following a fork. The new fork is
14073 selected as current, and unless this was a vfork will have a
14074 different program space from the original thread. Reset that
14075 as well. */
14076 b->loc->pspace = current_program_space;
14077 }
14078 }
14079
14080 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14081 If from_tty is nonzero, it prints a message to that effect,
14082 which ends with a period (no newline). */
14083
14084 void
14085 set_ignore_count (int bptnum, int count, int from_tty)
14086 {
14087 struct breakpoint *b;
14088
14089 if (count < 0)
14090 count = 0;
14091
14092 ALL_BREAKPOINTS (b)
14093 if (b->number == bptnum)
14094 {
14095 if (is_tracepoint (b))
14096 {
14097 if (from_tty && count != 0)
14098 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14099 bptnum);
14100 return;
14101 }
14102
14103 b->ignore_count = count;
14104 if (from_tty)
14105 {
14106 if (count == 0)
14107 printf_filtered (_("Will stop next time "
14108 "breakpoint %d is reached."),
14109 bptnum);
14110 else if (count == 1)
14111 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14112 bptnum);
14113 else
14114 printf_filtered (_("Will ignore next %d "
14115 "crossings of breakpoint %d."),
14116 count, bptnum);
14117 }
14118 gdb::observers::breakpoint_modified.notify (b);
14119 return;
14120 }
14121
14122 error (_("No breakpoint number %d."), bptnum);
14123 }
14124
14125 /* Command to set ignore-count of breakpoint N to COUNT. */
14126
14127 static void
14128 ignore_command (const char *args, int from_tty)
14129 {
14130 const char *p = args;
14131 int num;
14132
14133 if (p == 0)
14134 error_no_arg (_("a breakpoint number"));
14135
14136 num = get_number (&p);
14137 if (num == 0)
14138 error (_("bad breakpoint number: '%s'"), args);
14139 if (*p == 0)
14140 error (_("Second argument (specified ignore-count) is missing."));
14141
14142 set_ignore_count (num,
14143 longest_to_int (value_as_long (parse_and_eval (p))),
14144 from_tty);
14145 if (from_tty)
14146 printf_filtered ("\n");
14147 }
14148 \f
14149
14150 /* Call FUNCTION on each of the breakpoints with numbers in the range
14151 defined by BP_NUM_RANGE (an inclusive range). */
14152
14153 static void
14154 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
14155 gdb::function_view<void (breakpoint *)> function)
14156 {
14157 if (bp_num_range.first == 0)
14158 {
14159 warning (_("bad breakpoint number at or near '%d'"),
14160 bp_num_range.first);
14161 }
14162 else
14163 {
14164 struct breakpoint *b, *tmp;
14165
14166 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
14167 {
14168 bool match = false;
14169
14170 ALL_BREAKPOINTS_SAFE (b, tmp)
14171 if (b->number == i)
14172 {
14173 match = true;
14174 function (b);
14175 break;
14176 }
14177 if (!match)
14178 printf_unfiltered (_("No breakpoint number %d.\n"), i);
14179 }
14180 }
14181 }
14182
14183 /* Call FUNCTION on each of the breakpoints whose numbers are given in
14184 ARGS. */
14185
14186 static void
14187 map_breakpoint_numbers (const char *args,
14188 gdb::function_view<void (breakpoint *)> function)
14189 {
14190 if (args == NULL || *args == '\0')
14191 error_no_arg (_("one or more breakpoint numbers"));
14192
14193 number_or_range_parser parser (args);
14194
14195 while (!parser.finished ())
14196 {
14197 int num = parser.get_number ();
14198 map_breakpoint_number_range (std::make_pair (num, num), function);
14199 }
14200 }
14201
14202 /* Return the breakpoint location structure corresponding to the
14203 BP_NUM and LOC_NUM values. */
14204
14205 static struct bp_location *
14206 find_location_by_number (int bp_num, int loc_num)
14207 {
14208 struct breakpoint *b;
14209
14210 ALL_BREAKPOINTS (b)
14211 if (b->number == bp_num)
14212 {
14213 break;
14214 }
14215
14216 if (!b || b->number != bp_num)
14217 error (_("Bad breakpoint number '%d'"), bp_num);
14218
14219 if (loc_num == 0)
14220 error (_("Bad breakpoint location number '%d'"), loc_num);
14221
14222 int n = 0;
14223 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
14224 if (++n == loc_num)
14225 return loc;
14226
14227 error (_("Bad breakpoint location number '%d'"), loc_num);
14228 }
14229
14230 /* Modes of operation for extract_bp_num. */
14231 enum class extract_bp_kind
14232 {
14233 /* Extracting a breakpoint number. */
14234 bp,
14235
14236 /* Extracting a location number. */
14237 loc,
14238 };
14239
14240 /* Extract a breakpoint or location number (as determined by KIND)
14241 from the string starting at START. TRAILER is a character which
14242 can be found after the number. If you don't want a trailer, use
14243 '\0'. If END_OUT is not NULL, it is set to point after the parsed
14244 string. This always returns a positive integer. */
14245
14246 static int
14247 extract_bp_num (extract_bp_kind kind, const char *start,
14248 int trailer, const char **end_out = NULL)
14249 {
14250 const char *end = start;
14251 int num = get_number_trailer (&end, trailer);
14252 if (num < 0)
14253 error (kind == extract_bp_kind::bp
14254 ? _("Negative breakpoint number '%.*s'")
14255 : _("Negative breakpoint location number '%.*s'"),
14256 int (end - start), start);
14257 if (num == 0)
14258 error (kind == extract_bp_kind::bp
14259 ? _("Bad breakpoint number '%.*s'")
14260 : _("Bad breakpoint location number '%.*s'"),
14261 int (end - start), start);
14262
14263 if (end_out != NULL)
14264 *end_out = end;
14265 return num;
14266 }
14267
14268 /* Extract a breakpoint or location range (as determined by KIND) in
14269 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14270 representing the (inclusive) range. The returned pair's elements
14271 are always positive integers. */
14272
14273 static std::pair<int, int>
14274 extract_bp_or_bp_range (extract_bp_kind kind,
14275 const std::string &arg,
14276 std::string::size_type arg_offset)
14277 {
14278 std::pair<int, int> range;
14279 const char *bp_loc = &arg[arg_offset];
14280 std::string::size_type dash = arg.find ('-', arg_offset);
14281 if (dash != std::string::npos)
14282 {
14283 /* bp_loc is a range (x-z). */
14284 if (arg.length () == dash + 1)
14285 error (kind == extract_bp_kind::bp
14286 ? _("Bad breakpoint number at or near: '%s'")
14287 : _("Bad breakpoint location number at or near: '%s'"),
14288 bp_loc);
14289
14290 const char *end;
14291 const char *start_first = bp_loc;
14292 const char *start_second = &arg[dash + 1];
14293 range.first = extract_bp_num (kind, start_first, '-');
14294 range.second = extract_bp_num (kind, start_second, '\0', &end);
14295
14296 if (range.first > range.second)
14297 error (kind == extract_bp_kind::bp
14298 ? _("Inverted breakpoint range at '%.*s'")
14299 : _("Inverted breakpoint location range at '%.*s'"),
14300 int (end - start_first), start_first);
14301 }
14302 else
14303 {
14304 /* bp_loc is a single value. */
14305 range.first = extract_bp_num (kind, bp_loc, '\0');
14306 range.second = range.first;
14307 }
14308 return range;
14309 }
14310
14311 /* Extract the breakpoint/location range specified by ARG. Returns
14312 the breakpoint range in BP_NUM_RANGE, and the location range in
14313 BP_LOC_RANGE.
14314
14315 ARG may be in any of the following forms:
14316
14317 x where 'x' is a breakpoint number.
14318 x-y where 'x' and 'y' specify a breakpoint numbers range.
14319 x.y where 'x' is a breakpoint number and 'y' a location number.
14320 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14321 location number range.
14322 */
14323
14324 static void
14325 extract_bp_number_and_location (const std::string &arg,
14326 std::pair<int, int> &bp_num_range,
14327 std::pair<int, int> &bp_loc_range)
14328 {
14329 std::string::size_type dot = arg.find ('.');
14330
14331 if (dot != std::string::npos)
14332 {
14333 /* Handle 'x.y' and 'x.y-z' cases. */
14334
14335 if (arg.length () == dot + 1 || dot == 0)
14336 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
14337
14338 bp_num_range.first
14339 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14340 bp_num_range.second = bp_num_range.first;
14341
14342 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14343 arg, dot + 1);
14344 }
14345 else
14346 {
14347 /* Handle x and x-y cases. */
14348
14349 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
14350 bp_loc_range.first = 0;
14351 bp_loc_range.second = 0;
14352 }
14353 }
14354
14355 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14356 specifies whether to enable or disable. */
14357
14358 static void
14359 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14360 {
14361 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14362 if (loc != NULL)
14363 {
14364 if (loc->disabled_by_cond && enable)
14365 error (_("Breakpoint %d's condition is invalid at location %d, "
14366 "cannot enable."), bp_num, loc_num);
14367
14368 if (loc->enabled != enable)
14369 {
14370 loc->enabled = enable;
14371 mark_breakpoint_location_modified (loc);
14372 }
14373 if (target_supports_enable_disable_tracepoint ()
14374 && current_trace_status ()->running && loc->owner
14375 && is_tracepoint (loc->owner))
14376 target_disable_tracepoint (loc);
14377 }
14378 update_global_location_list (UGLL_DONT_INSERT);
14379
14380 gdb::observers::breakpoint_modified.notify (loc->owner);
14381 }
14382
14383 /* Enable or disable a range of breakpoint locations. BP_NUM is the
14384 number of the breakpoint, and BP_LOC_RANGE specifies the
14385 (inclusive) range of location numbers of that breakpoint to
14386 enable/disable. ENABLE specifies whether to enable or disable the
14387 location. */
14388
14389 static void
14390 enable_disable_breakpoint_location_range (int bp_num,
14391 std::pair<int, int> &bp_loc_range,
14392 bool enable)
14393 {
14394 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14395 enable_disable_bp_num_loc (bp_num, i, enable);
14396 }
14397
14398 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14399 If from_tty is nonzero, it prints a message to that effect,
14400 which ends with a period (no newline). */
14401
14402 void
14403 disable_breakpoint (struct breakpoint *bpt)
14404 {
14405 /* Never disable a watchpoint scope breakpoint; we want to
14406 hit them when we leave scope so we can delete both the
14407 watchpoint and its scope breakpoint at that time. */
14408 if (bpt->type == bp_watchpoint_scope)
14409 return;
14410
14411 bpt->enable_state = bp_disabled;
14412
14413 /* Mark breakpoint locations modified. */
14414 mark_breakpoint_modified (bpt);
14415
14416 if (target_supports_enable_disable_tracepoint ()
14417 && current_trace_status ()->running && is_tracepoint (bpt))
14418 {
14419 struct bp_location *location;
14420
14421 for (location = bpt->loc; location; location = location->next)
14422 target_disable_tracepoint (location);
14423 }
14424
14425 update_global_location_list (UGLL_DONT_INSERT);
14426
14427 gdb::observers::breakpoint_modified.notify (bpt);
14428 }
14429
14430 /* Enable or disable the breakpoint(s) or breakpoint location(s)
14431 specified in ARGS. ARGS may be in any of the formats handled by
14432 extract_bp_number_and_location. ENABLE specifies whether to enable
14433 or disable the breakpoints/locations. */
14434
14435 static void
14436 enable_disable_command (const char *args, int from_tty, bool enable)
14437 {
14438 if (args == 0)
14439 {
14440 struct breakpoint *bpt;
14441
14442 ALL_BREAKPOINTS (bpt)
14443 if (user_breakpoint_p (bpt))
14444 {
14445 if (enable)
14446 enable_breakpoint (bpt);
14447 else
14448 disable_breakpoint (bpt);
14449 }
14450 }
14451 else
14452 {
14453 std::string num = extract_arg (&args);
14454
14455 while (!num.empty ())
14456 {
14457 std::pair<int, int> bp_num_range, bp_loc_range;
14458
14459 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14460
14461 if (bp_loc_range.first == bp_loc_range.second
14462 && bp_loc_range.first == 0)
14463 {
14464 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14465 map_breakpoint_number_range (bp_num_range,
14466 enable
14467 ? enable_breakpoint
14468 : disable_breakpoint);
14469 }
14470 else
14471 {
14472 /* Handle breakpoint ids with formats 'x.y' or
14473 'x.y-z'. */
14474 enable_disable_breakpoint_location_range
14475 (bp_num_range.first, bp_loc_range, enable);
14476 }
14477 num = extract_arg (&args);
14478 }
14479 }
14480 }
14481
14482 /* The disable command disables the specified breakpoints/locations
14483 (or all defined breakpoints) so they're no longer effective in
14484 stopping the inferior. ARGS may be in any of the forms defined in
14485 extract_bp_number_and_location. */
14486
14487 static void
14488 disable_command (const char *args, int from_tty)
14489 {
14490 enable_disable_command (args, from_tty, false);
14491 }
14492
14493 static void
14494 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14495 int count)
14496 {
14497 int target_resources_ok;
14498
14499 if (bpt->type == bp_hardware_breakpoint)
14500 {
14501 int i;
14502 i = hw_breakpoint_used_count ();
14503 target_resources_ok =
14504 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14505 i + 1, 0);
14506 if (target_resources_ok == 0)
14507 error (_("No hardware breakpoint support in the target."));
14508 else if (target_resources_ok < 0)
14509 error (_("Hardware breakpoints used exceeds limit."));
14510 }
14511
14512 if (is_watchpoint (bpt))
14513 {
14514 /* Initialize it just to avoid a GCC false warning. */
14515 enum enable_state orig_enable_state = bp_disabled;
14516
14517 try
14518 {
14519 struct watchpoint *w = (struct watchpoint *) bpt;
14520
14521 orig_enable_state = bpt->enable_state;
14522 bpt->enable_state = bp_enabled;
14523 update_watchpoint (w, 1 /* reparse */);
14524 }
14525 catch (const gdb_exception &e)
14526 {
14527 bpt->enable_state = orig_enable_state;
14528 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14529 bpt->number);
14530 return;
14531 }
14532 }
14533
14534 bpt->enable_state = bp_enabled;
14535
14536 /* Mark breakpoint locations modified. */
14537 mark_breakpoint_modified (bpt);
14538
14539 if (target_supports_enable_disable_tracepoint ()
14540 && current_trace_status ()->running && is_tracepoint (bpt))
14541 {
14542 struct bp_location *location;
14543
14544 for (location = bpt->loc; location; location = location->next)
14545 target_enable_tracepoint (location);
14546 }
14547
14548 bpt->disposition = disposition;
14549 bpt->enable_count = count;
14550 update_global_location_list (UGLL_MAY_INSERT);
14551
14552 gdb::observers::breakpoint_modified.notify (bpt);
14553 }
14554
14555
14556 void
14557 enable_breakpoint (struct breakpoint *bpt)
14558 {
14559 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14560 }
14561
14562 /* The enable command enables the specified breakpoints/locations (or
14563 all defined breakpoints) so they once again become (or continue to
14564 be) effective in stopping the inferior. ARGS may be in any of the
14565 forms defined in extract_bp_number_and_location. */
14566
14567 static void
14568 enable_command (const char *args, int from_tty)
14569 {
14570 enable_disable_command (args, from_tty, true);
14571 }
14572
14573 static void
14574 enable_once_command (const char *args, int from_tty)
14575 {
14576 map_breakpoint_numbers
14577 (args, [&] (breakpoint *b)
14578 {
14579 iterate_over_related_breakpoints
14580 (b, [&] (breakpoint *bpt)
14581 {
14582 enable_breakpoint_disp (bpt, disp_disable, 1);
14583 });
14584 });
14585 }
14586
14587 static void
14588 enable_count_command (const char *args, int from_tty)
14589 {
14590 int count;
14591
14592 if (args == NULL)
14593 error_no_arg (_("hit count"));
14594
14595 count = get_number (&args);
14596
14597 map_breakpoint_numbers
14598 (args, [&] (breakpoint *b)
14599 {
14600 iterate_over_related_breakpoints
14601 (b, [&] (breakpoint *bpt)
14602 {
14603 enable_breakpoint_disp (bpt, disp_disable, count);
14604 });
14605 });
14606 }
14607
14608 static void
14609 enable_delete_command (const char *args, int from_tty)
14610 {
14611 map_breakpoint_numbers
14612 (args, [&] (breakpoint *b)
14613 {
14614 iterate_over_related_breakpoints
14615 (b, [&] (breakpoint *bpt)
14616 {
14617 enable_breakpoint_disp (bpt, disp_del, 1);
14618 });
14619 });
14620 }
14621 \f
14622 /* Invalidate last known value of any hardware watchpoint if
14623 the memory which that value represents has been written to by
14624 GDB itself. */
14625
14626 static void
14627 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14628 CORE_ADDR addr, ssize_t len,
14629 const bfd_byte *data)
14630 {
14631 struct breakpoint *bp;
14632
14633 ALL_BREAKPOINTS (bp)
14634 if (bp->enable_state == bp_enabled
14635 && bp->type == bp_hardware_watchpoint)
14636 {
14637 struct watchpoint *wp = (struct watchpoint *) bp;
14638
14639 if (wp->val_valid && wp->val != nullptr)
14640 {
14641 struct bp_location *loc;
14642
14643 for (loc = bp->loc; loc != NULL; loc = loc->next)
14644 if (loc->loc_type == bp_loc_hardware_watchpoint
14645 && loc->address + loc->length > addr
14646 && addr + len > loc->address)
14647 {
14648 wp->val = NULL;
14649 wp->val_valid = false;
14650 }
14651 }
14652 }
14653 }
14654
14655 /* Create and insert a breakpoint for software single step. */
14656
14657 void
14658 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14659 const address_space *aspace,
14660 CORE_ADDR next_pc)
14661 {
14662 struct thread_info *tp = inferior_thread ();
14663 struct symtab_and_line sal;
14664 CORE_ADDR pc = next_pc;
14665
14666 if (tp->control.single_step_breakpoints == NULL)
14667 {
14668 tp->control.single_step_breakpoints
14669 = new_single_step_breakpoint (tp->global_num, gdbarch);
14670 }
14671
14672 sal = find_pc_line (pc, 0);
14673 sal.pc = pc;
14674 sal.section = find_pc_overlay (pc);
14675 sal.explicit_pc = 1;
14676 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14677
14678 update_global_location_list (UGLL_INSERT);
14679 }
14680
14681 /* Insert single step breakpoints according to the current state. */
14682
14683 int
14684 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14685 {
14686 struct regcache *regcache = get_current_regcache ();
14687 std::vector<CORE_ADDR> next_pcs;
14688
14689 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14690
14691 if (!next_pcs.empty ())
14692 {
14693 struct frame_info *frame = get_current_frame ();
14694 const address_space *aspace = get_frame_address_space (frame);
14695
14696 for (CORE_ADDR pc : next_pcs)
14697 insert_single_step_breakpoint (gdbarch, aspace, pc);
14698
14699 return 1;
14700 }
14701 else
14702 return 0;
14703 }
14704
14705 /* See breakpoint.h. */
14706
14707 int
14708 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14709 const address_space *aspace,
14710 CORE_ADDR pc)
14711 {
14712 struct bp_location *loc;
14713
14714 for (loc = bp->loc; loc != NULL; loc = loc->next)
14715 if (loc->inserted
14716 && breakpoint_location_address_match (loc, aspace, pc))
14717 return 1;
14718
14719 return 0;
14720 }
14721
14722 /* Check whether a software single-step breakpoint is inserted at
14723 PC. */
14724
14725 int
14726 single_step_breakpoint_inserted_here_p (const address_space *aspace,
14727 CORE_ADDR pc)
14728 {
14729 struct breakpoint *bpt;
14730
14731 ALL_BREAKPOINTS (bpt)
14732 {
14733 if (bpt->type == bp_single_step
14734 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14735 return 1;
14736 }
14737 return 0;
14738 }
14739
14740 /* Tracepoint-specific operations. */
14741
14742 /* Set tracepoint count to NUM. */
14743 static void
14744 set_tracepoint_count (int num)
14745 {
14746 tracepoint_count = num;
14747 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14748 }
14749
14750 static void
14751 trace_command (const char *arg, int from_tty)
14752 {
14753 event_location_up location = string_to_event_location (&arg,
14754 current_language);
14755 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
14756 (location.get (), true /* is_tracepoint */);
14757
14758 create_breakpoint (get_current_arch (),
14759 location.get (),
14760 NULL, 0, arg, false, 1 /* parse arg */,
14761 0 /* tempflag */,
14762 bp_tracepoint /* type_wanted */,
14763 0 /* Ignore count */,
14764 pending_break_support,
14765 ops,
14766 from_tty,
14767 1 /* enabled */,
14768 0 /* internal */, 0);
14769 }
14770
14771 static void
14772 ftrace_command (const char *arg, int from_tty)
14773 {
14774 event_location_up location = string_to_event_location (&arg,
14775 current_language);
14776 create_breakpoint (get_current_arch (),
14777 location.get (),
14778 NULL, 0, arg, false, 1 /* parse arg */,
14779 0 /* tempflag */,
14780 bp_fast_tracepoint /* type_wanted */,
14781 0 /* Ignore count */,
14782 pending_break_support,
14783 &tracepoint_breakpoint_ops,
14784 from_tty,
14785 1 /* enabled */,
14786 0 /* internal */, 0);
14787 }
14788
14789 /* strace command implementation. Creates a static tracepoint. */
14790
14791 static void
14792 strace_command (const char *arg, int from_tty)
14793 {
14794 struct breakpoint_ops *ops;
14795 event_location_up location;
14796
14797 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14798 or with a normal static tracepoint. */
14799 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14800 {
14801 ops = &strace_marker_breakpoint_ops;
14802 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
14803 }
14804 else
14805 {
14806 ops = &tracepoint_breakpoint_ops;
14807 location = string_to_event_location (&arg, current_language);
14808 }
14809
14810 create_breakpoint (get_current_arch (),
14811 location.get (),
14812 NULL, 0, arg, false, 1 /* parse arg */,
14813 0 /* tempflag */,
14814 bp_static_tracepoint /* type_wanted */,
14815 0 /* Ignore count */,
14816 pending_break_support,
14817 ops,
14818 from_tty,
14819 1 /* enabled */,
14820 0 /* internal */, 0);
14821 }
14822
14823 /* Set up a fake reader function that gets command lines from a linked
14824 list that was acquired during tracepoint uploading. */
14825
14826 static struct uploaded_tp *this_utp;
14827 static int next_cmd;
14828
14829 static char *
14830 read_uploaded_action (void)
14831 {
14832 char *rslt = nullptr;
14833
14834 if (next_cmd < this_utp->cmd_strings.size ())
14835 {
14836 rslt = this_utp->cmd_strings[next_cmd].get ();
14837 next_cmd++;
14838 }
14839
14840 return rslt;
14841 }
14842
14843 /* Given information about a tracepoint as recorded on a target (which
14844 can be either a live system or a trace file), attempt to create an
14845 equivalent GDB tracepoint. This is not a reliable process, since
14846 the target does not necessarily have all the information used when
14847 the tracepoint was originally defined. */
14848
14849 struct tracepoint *
14850 create_tracepoint_from_upload (struct uploaded_tp *utp)
14851 {
14852 const char *addr_str;
14853 char small_buf[100];
14854 struct tracepoint *tp;
14855
14856 if (utp->at_string)
14857 addr_str = utp->at_string.get ();
14858 else
14859 {
14860 /* In the absence of a source location, fall back to raw
14861 address. Since there is no way to confirm that the address
14862 means the same thing as when the trace was started, warn the
14863 user. */
14864 warning (_("Uploaded tracepoint %d has no "
14865 "source location, using raw address"),
14866 utp->number);
14867 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14868 addr_str = small_buf;
14869 }
14870
14871 /* There's not much we can do with a sequence of bytecodes. */
14872 if (utp->cond && !utp->cond_string)
14873 warning (_("Uploaded tracepoint %d condition "
14874 "has no source form, ignoring it"),
14875 utp->number);
14876
14877 event_location_up location = string_to_event_location (&addr_str,
14878 current_language);
14879 if (!create_breakpoint (get_current_arch (),
14880 location.get (),
14881 utp->cond_string.get (), -1, addr_str,
14882 false /* force_condition */,
14883 0 /* parse cond/thread */,
14884 0 /* tempflag */,
14885 utp->type /* type_wanted */,
14886 0 /* Ignore count */,
14887 pending_break_support,
14888 &tracepoint_breakpoint_ops,
14889 0 /* from_tty */,
14890 utp->enabled /* enabled */,
14891 0 /* internal */,
14892 CREATE_BREAKPOINT_FLAGS_INSERTED))
14893 return NULL;
14894
14895 /* Get the tracepoint we just created. */
14896 tp = get_tracepoint (tracepoint_count);
14897 gdb_assert (tp != NULL);
14898
14899 if (utp->pass > 0)
14900 {
14901 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14902 tp->number);
14903
14904 trace_pass_command (small_buf, 0);
14905 }
14906
14907 /* If we have uploaded versions of the original commands, set up a
14908 special-purpose "reader" function and call the usual command line
14909 reader, then pass the result to the breakpoint command-setting
14910 function. */
14911 if (!utp->cmd_strings.empty ())
14912 {
14913 counted_command_line cmd_list;
14914
14915 this_utp = utp;
14916 next_cmd = 0;
14917
14918 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
14919
14920 breakpoint_set_commands (tp, std::move (cmd_list));
14921 }
14922 else if (!utp->actions.empty ()
14923 || !utp->step_actions.empty ())
14924 warning (_("Uploaded tracepoint %d actions "
14925 "have no source form, ignoring them"),
14926 utp->number);
14927
14928 /* Copy any status information that might be available. */
14929 tp->hit_count = utp->hit_count;
14930 tp->traceframe_usage = utp->traceframe_usage;
14931
14932 return tp;
14933 }
14934
14935 /* Print information on tracepoint number TPNUM_EXP, or all if
14936 omitted. */
14937
14938 static void
14939 info_tracepoints_command (const char *args, int from_tty)
14940 {
14941 struct ui_out *uiout = current_uiout;
14942 int num_printed;
14943
14944 num_printed = breakpoint_1 (args, false, is_tracepoint);
14945
14946 if (num_printed == 0)
14947 {
14948 if (args == NULL || *args == '\0')
14949 uiout->message ("No tracepoints.\n");
14950 else
14951 uiout->message ("No tracepoint matching '%s'.\n", args);
14952 }
14953
14954 default_collect_info ();
14955 }
14956
14957 /* The 'enable trace' command enables tracepoints.
14958 Not supported by all targets. */
14959 static void
14960 enable_trace_command (const char *args, int from_tty)
14961 {
14962 enable_command (args, from_tty);
14963 }
14964
14965 /* The 'disable trace' command disables tracepoints.
14966 Not supported by all targets. */
14967 static void
14968 disable_trace_command (const char *args, int from_tty)
14969 {
14970 disable_command (args, from_tty);
14971 }
14972
14973 /* Remove a tracepoint (or all if no argument). */
14974 static void
14975 delete_trace_command (const char *arg, int from_tty)
14976 {
14977 struct breakpoint *b, *b_tmp;
14978
14979 dont_repeat ();
14980
14981 if (arg == 0)
14982 {
14983 int breaks_to_delete = 0;
14984
14985 /* Delete all breakpoints if no argument.
14986 Do not delete internal or call-dummy breakpoints, these
14987 have to be deleted with an explicit breakpoint number
14988 argument. */
14989 ALL_TRACEPOINTS (b)
14990 if (is_tracepoint (b) && user_breakpoint_p (b))
14991 {
14992 breaks_to_delete = 1;
14993 break;
14994 }
14995
14996 /* Ask user only if there are some breakpoints to delete. */
14997 if (!from_tty
14998 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14999 {
15000 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15001 if (is_tracepoint (b) && user_breakpoint_p (b))
15002 delete_breakpoint (b);
15003 }
15004 }
15005 else
15006 map_breakpoint_numbers
15007 (arg, [&] (breakpoint *br)
15008 {
15009 iterate_over_related_breakpoints (br, delete_breakpoint);
15010 });
15011 }
15012
15013 /* Helper function for trace_pass_command. */
15014
15015 static void
15016 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15017 {
15018 tp->pass_count = count;
15019 gdb::observers::breakpoint_modified.notify (tp);
15020 if (from_tty)
15021 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15022 tp->number, count);
15023 }
15024
15025 /* Set passcount for tracepoint.
15026
15027 First command argument is passcount, second is tracepoint number.
15028 If tracepoint number omitted, apply to most recently defined.
15029 Also accepts special argument "all". */
15030
15031 static void
15032 trace_pass_command (const char *args, int from_tty)
15033 {
15034 struct tracepoint *t1;
15035 ULONGEST count;
15036
15037 if (args == 0 || *args == 0)
15038 error (_("passcount command requires an "
15039 "argument (count + optional TP num)"));
15040
15041 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
15042
15043 args = skip_spaces (args);
15044 if (*args && strncasecmp (args, "all", 3) == 0)
15045 {
15046 struct breakpoint *b;
15047
15048 args += 3; /* Skip special argument "all". */
15049 if (*args)
15050 error (_("Junk at end of arguments."));
15051
15052 ALL_TRACEPOINTS (b)
15053 {
15054 t1 = (struct tracepoint *) b;
15055 trace_pass_set_count (t1, count, from_tty);
15056 }
15057 }
15058 else if (*args == '\0')
15059 {
15060 t1 = get_tracepoint_by_number (&args, NULL);
15061 if (t1)
15062 trace_pass_set_count (t1, count, from_tty);
15063 }
15064 else
15065 {
15066 number_or_range_parser parser (args);
15067 while (!parser.finished ())
15068 {
15069 t1 = get_tracepoint_by_number (&args, &parser);
15070 if (t1)
15071 trace_pass_set_count (t1, count, from_tty);
15072 }
15073 }
15074 }
15075
15076 struct tracepoint *
15077 get_tracepoint (int num)
15078 {
15079 struct breakpoint *t;
15080
15081 ALL_TRACEPOINTS (t)
15082 if (t->number == num)
15083 return (struct tracepoint *) t;
15084
15085 return NULL;
15086 }
15087
15088 /* Find the tracepoint with the given target-side number (which may be
15089 different from the tracepoint number after disconnecting and
15090 reconnecting). */
15091
15092 struct tracepoint *
15093 get_tracepoint_by_number_on_target (int num)
15094 {
15095 struct breakpoint *b;
15096
15097 ALL_TRACEPOINTS (b)
15098 {
15099 struct tracepoint *t = (struct tracepoint *) b;
15100
15101 if (t->number_on_target == num)
15102 return t;
15103 }
15104
15105 return NULL;
15106 }
15107
15108 /* Utility: parse a tracepoint number and look it up in the list.
15109 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15110 If the argument is missing, the most recent tracepoint
15111 (tracepoint_count) is returned. */
15112
15113 struct tracepoint *
15114 get_tracepoint_by_number (const char **arg,
15115 number_or_range_parser *parser)
15116 {
15117 struct breakpoint *t;
15118 int tpnum;
15119 const char *instring = arg == NULL ? NULL : *arg;
15120
15121 if (parser != NULL)
15122 {
15123 gdb_assert (!parser->finished ());
15124 tpnum = parser->get_number ();
15125 }
15126 else if (arg == NULL || *arg == NULL || ! **arg)
15127 tpnum = tracepoint_count;
15128 else
15129 tpnum = get_number (arg);
15130
15131 if (tpnum <= 0)
15132 {
15133 if (instring && *instring)
15134 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15135 instring);
15136 else
15137 printf_filtered (_("No previous tracepoint\n"));
15138 return NULL;
15139 }
15140
15141 ALL_TRACEPOINTS (t)
15142 if (t->number == tpnum)
15143 {
15144 return (struct tracepoint *) t;
15145 }
15146
15147 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15148 return NULL;
15149 }
15150
15151 void
15152 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15153 {
15154 if (b->thread != -1)
15155 fprintf_unfiltered (fp, " thread %d", b->thread);
15156
15157 if (b->task != 0)
15158 fprintf_unfiltered (fp, " task %d", b->task);
15159
15160 fprintf_unfiltered (fp, "\n");
15161 }
15162
15163 /* Save information on user settable breakpoints (watchpoints, etc) to
15164 a new script file named FILENAME. If FILTER is non-NULL, call it
15165 on each breakpoint and only include the ones for which it returns
15166 true. */
15167
15168 static void
15169 save_breakpoints (const char *filename, int from_tty,
15170 bool (*filter) (const struct breakpoint *))
15171 {
15172 struct breakpoint *tp;
15173 int any = 0;
15174 int extra_trace_bits = 0;
15175
15176 if (filename == 0 || *filename == 0)
15177 error (_("Argument required (file name in which to save)"));
15178
15179 /* See if we have anything to save. */
15180 ALL_BREAKPOINTS (tp)
15181 {
15182 /* Skip internal and momentary breakpoints. */
15183 if (!user_breakpoint_p (tp))
15184 continue;
15185
15186 /* If we have a filter, only save the breakpoints it accepts. */
15187 if (filter && !filter (tp))
15188 continue;
15189
15190 any = 1;
15191
15192 if (is_tracepoint (tp))
15193 {
15194 extra_trace_bits = 1;
15195
15196 /* We can stop searching. */
15197 break;
15198 }
15199 }
15200
15201 if (!any)
15202 {
15203 warning (_("Nothing to save."));
15204 return;
15205 }
15206
15207 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
15208
15209 stdio_file fp;
15210
15211 if (!fp.open (expanded_filename.get (), "w"))
15212 error (_("Unable to open file '%s' for saving (%s)"),
15213 expanded_filename.get (), safe_strerror (errno));
15214
15215 if (extra_trace_bits)
15216 save_trace_state_variables (&fp);
15217
15218 ALL_BREAKPOINTS (tp)
15219 {
15220 /* Skip internal and momentary breakpoints. */
15221 if (!user_breakpoint_p (tp))
15222 continue;
15223
15224 /* If we have a filter, only save the breakpoints it accepts. */
15225 if (filter && !filter (tp))
15226 continue;
15227
15228 tp->ops->print_recreate (tp, &fp);
15229
15230 /* Note, we can't rely on tp->number for anything, as we can't
15231 assume the recreated breakpoint numbers will match. Use $bpnum
15232 instead. */
15233
15234 if (tp->cond_string)
15235 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15236
15237 if (tp->ignore_count)
15238 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15239
15240 if (tp->type != bp_dprintf && tp->commands)
15241 {
15242 fp.puts (" commands\n");
15243
15244 current_uiout->redirect (&fp);
15245 try
15246 {
15247 print_command_lines (current_uiout, tp->commands.get (), 2);
15248 }
15249 catch (const gdb_exception &ex)
15250 {
15251 current_uiout->redirect (NULL);
15252 throw;
15253 }
15254
15255 current_uiout->redirect (NULL);
15256 fp.puts (" end\n");
15257 }
15258
15259 if (tp->enable_state == bp_disabled)
15260 fp.puts ("disable $bpnum\n");
15261
15262 /* If this is a multi-location breakpoint, check if the locations
15263 should be individually disabled. Watchpoint locations are
15264 special, and not user visible. */
15265 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15266 {
15267 struct bp_location *loc;
15268 int n = 1;
15269
15270 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15271 if (!loc->enabled)
15272 fp.printf ("disable $bpnum.%d\n", n);
15273 }
15274 }
15275
15276 if (extra_trace_bits && *default_collect)
15277 fp.printf ("set default-collect %s\n", default_collect);
15278
15279 if (from_tty)
15280 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
15281 }
15282
15283 /* The `save breakpoints' command. */
15284
15285 static void
15286 save_breakpoints_command (const char *args, int from_tty)
15287 {
15288 save_breakpoints (args, from_tty, NULL);
15289 }
15290
15291 /* The `save tracepoints' command. */
15292
15293 static void
15294 save_tracepoints_command (const char *args, int from_tty)
15295 {
15296 save_breakpoints (args, from_tty, is_tracepoint);
15297 }
15298
15299 /* Create a vector of all tracepoints. */
15300
15301 std::vector<breakpoint *>
15302 all_tracepoints (void)
15303 {
15304 std::vector<breakpoint *> tp_vec;
15305 struct breakpoint *tp;
15306
15307 ALL_TRACEPOINTS (tp)
15308 {
15309 tp_vec.push_back (tp);
15310 }
15311
15312 return tp_vec;
15313 }
15314
15315 \f
15316 /* This help string is used to consolidate all the help string for specifying
15317 locations used by several commands. */
15318
15319 #define LOCATION_HELP_STRING \
15320 "Linespecs are colon-separated lists of location parameters, such as\n\
15321 source filename, function name, label name, and line number.\n\
15322 Example: To specify the start of a label named \"the_top\" in the\n\
15323 function \"fact\" in the file \"factorial.c\", use\n\
15324 \"factorial.c:fact:the_top\".\n\
15325 \n\
15326 Address locations begin with \"*\" and specify an exact address in the\n\
15327 program. Example: To specify the fourth byte past the start function\n\
15328 \"main\", use \"*main + 4\".\n\
15329 \n\
15330 Explicit locations are similar to linespecs but use an option/argument\n\
15331 syntax to specify location parameters.\n\
15332 Example: To specify the start of the label named \"the_top\" in the\n\
15333 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15334 -function fact -label the_top\".\n\
15335 \n\
15336 By default, a specified function is matched against the program's\n\
15337 functions in all scopes. For C++, this means in all namespaces and\n\
15338 classes. For Ada, this means in all packages. E.g., in C++,\n\
15339 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15340 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
15341 specified name as a complete fully-qualified name instead."
15342
15343 /* This help string is used for the break, hbreak, tbreak and thbreak
15344 commands. It is defined as a macro to prevent duplication.
15345 COMMAND should be a string constant containing the name of the
15346 command. */
15347
15348 #define BREAK_ARGS_HELP(command) \
15349 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
15350 \t[-force-condition] [if CONDITION]\n\
15351 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15352 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15353 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15354 `-probe-dtrace' (for a DTrace probe).\n\
15355 LOCATION may be a linespec, address, or explicit location as described\n\
15356 below.\n\
15357 \n\
15358 With no LOCATION, uses current execution address of the selected\n\
15359 stack frame. This is useful for breaking on return to a stack frame.\n\
15360 \n\
15361 THREADNUM is the number from \"info threads\".\n\
15362 CONDITION is a boolean expression.\n\
15363 \n\
15364 With the \"-force-condition\" flag, the condition is defined even when\n\
15365 it is invalid for all current locations.\n\
15366 \n" LOCATION_HELP_STRING "\n\n\
15367 Multiple breakpoints at one place are permitted, and useful if their\n\
15368 conditions are different.\n\
15369 \n\
15370 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15371
15372 /* List of subcommands for "catch". */
15373 static struct cmd_list_element *catch_cmdlist;
15374
15375 /* List of subcommands for "tcatch". */
15376 static struct cmd_list_element *tcatch_cmdlist;
15377
15378 void
15379 add_catch_command (const char *name, const char *docstring,
15380 cmd_const_sfunc_ftype *sfunc,
15381 completer_ftype *completer,
15382 void *user_data_catch,
15383 void *user_data_tcatch)
15384 {
15385 struct cmd_list_element *command;
15386
15387 command = add_cmd (name, class_breakpoint, docstring,
15388 &catch_cmdlist);
15389 set_cmd_sfunc (command, sfunc);
15390 set_cmd_context (command, user_data_catch);
15391 set_cmd_completer (command, completer);
15392
15393 command = add_cmd (name, class_breakpoint, docstring,
15394 &tcatch_cmdlist);
15395 set_cmd_sfunc (command, sfunc);
15396 set_cmd_context (command, user_data_tcatch);
15397 set_cmd_completer (command, completer);
15398 }
15399
15400 struct breakpoint *
15401 iterate_over_breakpoints (gdb::function_view<bool (breakpoint *)> callback)
15402 {
15403 struct breakpoint *b, *b_tmp;
15404
15405 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15406 {
15407 if (callback (b))
15408 return b;
15409 }
15410
15411 return NULL;
15412 }
15413
15414 /* Zero if any of the breakpoint's locations could be a location where
15415 functions have been inlined, nonzero otherwise. */
15416
15417 static int
15418 is_non_inline_function (struct breakpoint *b)
15419 {
15420 /* The shared library event breakpoint is set on the address of a
15421 non-inline function. */
15422 if (b->type == bp_shlib_event)
15423 return 1;
15424
15425 return 0;
15426 }
15427
15428 /* Nonzero if the specified PC cannot be a location where functions
15429 have been inlined. */
15430
15431 int
15432 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
15433 const struct target_waitstatus *ws)
15434 {
15435 struct breakpoint *b;
15436 struct bp_location *bl;
15437
15438 ALL_BREAKPOINTS (b)
15439 {
15440 if (!is_non_inline_function (b))
15441 continue;
15442
15443 for (bl = b->loc; bl != NULL; bl = bl->next)
15444 {
15445 if (!bl->shlib_disabled
15446 && bpstat_check_location (bl, aspace, pc, ws))
15447 return 1;
15448 }
15449 }
15450
15451 return 0;
15452 }
15453
15454 /* Remove any references to OBJFILE which is going to be freed. */
15455
15456 void
15457 breakpoint_free_objfile (struct objfile *objfile)
15458 {
15459 struct bp_location **locp, *loc;
15460
15461 ALL_BP_LOCATIONS (loc, locp)
15462 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15463 loc->symtab = NULL;
15464 }
15465
15466 void
15467 initialize_breakpoint_ops (void)
15468 {
15469 static int initialized = 0;
15470
15471 struct breakpoint_ops *ops;
15472
15473 if (initialized)
15474 return;
15475 initialized = 1;
15476
15477 /* The breakpoint_ops structure to be inherit by all kinds of
15478 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15479 internal and momentary breakpoints, etc.). */
15480 ops = &bkpt_base_breakpoint_ops;
15481 *ops = base_breakpoint_ops;
15482 ops->re_set = bkpt_re_set;
15483 ops->insert_location = bkpt_insert_location;
15484 ops->remove_location = bkpt_remove_location;
15485 ops->breakpoint_hit = bkpt_breakpoint_hit;
15486 ops->create_sals_from_location = bkpt_create_sals_from_location;
15487 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15488 ops->decode_location = bkpt_decode_location;
15489
15490 /* The breakpoint_ops structure to be used in regular breakpoints. */
15491 ops = &bkpt_breakpoint_ops;
15492 *ops = bkpt_base_breakpoint_ops;
15493 ops->re_set = bkpt_re_set;
15494 ops->resources_needed = bkpt_resources_needed;
15495 ops->print_it = bkpt_print_it;
15496 ops->print_mention = bkpt_print_mention;
15497 ops->print_recreate = bkpt_print_recreate;
15498
15499 /* Ranged breakpoints. */
15500 ops = &ranged_breakpoint_ops;
15501 *ops = bkpt_breakpoint_ops;
15502 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15503 ops->resources_needed = resources_needed_ranged_breakpoint;
15504 ops->print_it = print_it_ranged_breakpoint;
15505 ops->print_one = print_one_ranged_breakpoint;
15506 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15507 ops->print_mention = print_mention_ranged_breakpoint;
15508 ops->print_recreate = print_recreate_ranged_breakpoint;
15509
15510 /* Internal breakpoints. */
15511 ops = &internal_breakpoint_ops;
15512 *ops = bkpt_base_breakpoint_ops;
15513 ops->re_set = internal_bkpt_re_set;
15514 ops->check_status = internal_bkpt_check_status;
15515 ops->print_it = internal_bkpt_print_it;
15516 ops->print_mention = internal_bkpt_print_mention;
15517
15518 /* Momentary breakpoints. */
15519 ops = &momentary_breakpoint_ops;
15520 *ops = bkpt_base_breakpoint_ops;
15521 ops->re_set = momentary_bkpt_re_set;
15522 ops->check_status = momentary_bkpt_check_status;
15523 ops->print_it = momentary_bkpt_print_it;
15524 ops->print_mention = momentary_bkpt_print_mention;
15525
15526 /* Probe breakpoints. */
15527 ops = &bkpt_probe_breakpoint_ops;
15528 *ops = bkpt_breakpoint_ops;
15529 ops->insert_location = bkpt_probe_insert_location;
15530 ops->remove_location = bkpt_probe_remove_location;
15531 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15532 ops->decode_location = bkpt_probe_decode_location;
15533
15534 /* Watchpoints. */
15535 ops = &watchpoint_breakpoint_ops;
15536 *ops = base_breakpoint_ops;
15537 ops->re_set = re_set_watchpoint;
15538 ops->insert_location = insert_watchpoint;
15539 ops->remove_location = remove_watchpoint;
15540 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15541 ops->check_status = check_status_watchpoint;
15542 ops->resources_needed = resources_needed_watchpoint;
15543 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15544 ops->print_it = print_it_watchpoint;
15545 ops->print_mention = print_mention_watchpoint;
15546 ops->print_recreate = print_recreate_watchpoint;
15547 ops->explains_signal = explains_signal_watchpoint;
15548
15549 /* Masked watchpoints. */
15550 ops = &masked_watchpoint_breakpoint_ops;
15551 *ops = watchpoint_breakpoint_ops;
15552 ops->insert_location = insert_masked_watchpoint;
15553 ops->remove_location = remove_masked_watchpoint;
15554 ops->resources_needed = resources_needed_masked_watchpoint;
15555 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15556 ops->print_it = print_it_masked_watchpoint;
15557 ops->print_one_detail = print_one_detail_masked_watchpoint;
15558 ops->print_mention = print_mention_masked_watchpoint;
15559 ops->print_recreate = print_recreate_masked_watchpoint;
15560
15561 /* Tracepoints. */
15562 ops = &tracepoint_breakpoint_ops;
15563 *ops = base_breakpoint_ops;
15564 ops->re_set = tracepoint_re_set;
15565 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15566 ops->print_one_detail = tracepoint_print_one_detail;
15567 ops->print_mention = tracepoint_print_mention;
15568 ops->print_recreate = tracepoint_print_recreate;
15569 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15570 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15571 ops->decode_location = tracepoint_decode_location;
15572
15573 /* Probe tracepoints. */
15574 ops = &tracepoint_probe_breakpoint_ops;
15575 *ops = tracepoint_breakpoint_ops;
15576 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15577 ops->decode_location = tracepoint_probe_decode_location;
15578
15579 /* Static tracepoints with marker (`-m'). */
15580 ops = &strace_marker_breakpoint_ops;
15581 *ops = tracepoint_breakpoint_ops;
15582 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15583 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15584 ops->decode_location = strace_marker_decode_location;
15585
15586 /* Fork catchpoints. */
15587 ops = &catch_fork_breakpoint_ops;
15588 *ops = base_breakpoint_ops;
15589 ops->insert_location = insert_catch_fork;
15590 ops->remove_location = remove_catch_fork;
15591 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15592 ops->print_it = print_it_catch_fork;
15593 ops->print_one = print_one_catch_fork;
15594 ops->print_mention = print_mention_catch_fork;
15595 ops->print_recreate = print_recreate_catch_fork;
15596
15597 /* Vfork catchpoints. */
15598 ops = &catch_vfork_breakpoint_ops;
15599 *ops = base_breakpoint_ops;
15600 ops->insert_location = insert_catch_vfork;
15601 ops->remove_location = remove_catch_vfork;
15602 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15603 ops->print_it = print_it_catch_vfork;
15604 ops->print_one = print_one_catch_vfork;
15605 ops->print_mention = print_mention_catch_vfork;
15606 ops->print_recreate = print_recreate_catch_vfork;
15607
15608 /* Exec catchpoints. */
15609 ops = &catch_exec_breakpoint_ops;
15610 *ops = base_breakpoint_ops;
15611 ops->insert_location = insert_catch_exec;
15612 ops->remove_location = remove_catch_exec;
15613 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15614 ops->print_it = print_it_catch_exec;
15615 ops->print_one = print_one_catch_exec;
15616 ops->print_mention = print_mention_catch_exec;
15617 ops->print_recreate = print_recreate_catch_exec;
15618
15619 /* Solib-related catchpoints. */
15620 ops = &catch_solib_breakpoint_ops;
15621 *ops = base_breakpoint_ops;
15622 ops->insert_location = insert_catch_solib;
15623 ops->remove_location = remove_catch_solib;
15624 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15625 ops->check_status = check_status_catch_solib;
15626 ops->print_it = print_it_catch_solib;
15627 ops->print_one = print_one_catch_solib;
15628 ops->print_mention = print_mention_catch_solib;
15629 ops->print_recreate = print_recreate_catch_solib;
15630
15631 ops = &dprintf_breakpoint_ops;
15632 *ops = bkpt_base_breakpoint_ops;
15633 ops->re_set = dprintf_re_set;
15634 ops->resources_needed = bkpt_resources_needed;
15635 ops->print_it = bkpt_print_it;
15636 ops->print_mention = bkpt_print_mention;
15637 ops->print_recreate = dprintf_print_recreate;
15638 ops->after_condition_true = dprintf_after_condition_true;
15639 ops->breakpoint_hit = dprintf_breakpoint_hit;
15640 }
15641
15642 /* Chain containing all defined "enable breakpoint" subcommands. */
15643
15644 static struct cmd_list_element *enablebreaklist = NULL;
15645
15646 /* See breakpoint.h. */
15647
15648 cmd_list_element *commands_cmd_element = nullptr;
15649
15650 void _initialize_breakpoint ();
15651 void
15652 _initialize_breakpoint ()
15653 {
15654 struct cmd_list_element *c;
15655
15656 initialize_breakpoint_ops ();
15657
15658 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
15659 "breakpoint");
15660 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
15661 "breakpoint");
15662 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
15663 "breakpoint");
15664
15665 breakpoint_chain = 0;
15666 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15667 before a breakpoint is set. */
15668 breakpoint_count = 0;
15669
15670 tracepoint_count = 0;
15671
15672 add_com ("ignore", class_breakpoint, ignore_command, _("\
15673 Set ignore-count of breakpoint number N to COUNT.\n\
15674 Usage is `ignore N COUNT'."));
15675
15676 commands_cmd_element = add_com ("commands", class_breakpoint,
15677 commands_command, _("\
15678 Set commands to be executed when the given breakpoints are hit.\n\
15679 Give a space-separated breakpoint list as argument after \"commands\".\n\
15680 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15681 (e.g. `5-7').\n\
15682 With no argument, the targeted breakpoint is the last one set.\n\
15683 The commands themselves follow starting on the next line.\n\
15684 Type a line containing \"end\" to indicate the end of them.\n\
15685 Give \"silent\" as the first line to make the breakpoint silent;\n\
15686 then no output is printed when it is hit, except what the commands print."));
15687
15688 const auto cc_opts = make_condition_command_options_def_group (nullptr);
15689 static std::string condition_command_help
15690 = gdb::option::build_help (_("\
15691 Specify breakpoint number N to break only if COND is true.\n\
15692 Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
15693 is an expression to be evaluated whenever breakpoint N is reached.\n\
15694 \n\
15695 Options:\n\
15696 %OPTIONS%"), cc_opts);
15697
15698 c = add_com ("condition", class_breakpoint, condition_command,
15699 condition_command_help.c_str ());
15700 set_cmd_completer_handle_brkchars (c, condition_completer);
15701
15702 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15703 Set a temporary breakpoint.\n\
15704 Like \"break\" except the breakpoint is only temporary,\n\
15705 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15706 by using \"enable delete\" on the breakpoint number.\n\
15707 \n"
15708 BREAK_ARGS_HELP ("tbreak")));
15709 set_cmd_completer (c, location_completer);
15710
15711 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15712 Set a hardware assisted breakpoint.\n\
15713 Like \"break\" except the breakpoint requires hardware support,\n\
15714 some target hardware may not have this support.\n\
15715 \n"
15716 BREAK_ARGS_HELP ("hbreak")));
15717 set_cmd_completer (c, location_completer);
15718
15719 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15720 Set a temporary hardware assisted breakpoint.\n\
15721 Like \"hbreak\" except the breakpoint is only temporary,\n\
15722 so it will be deleted when hit.\n\
15723 \n"
15724 BREAK_ARGS_HELP ("thbreak")));
15725 set_cmd_completer (c, location_completer);
15726
15727 cmd_list_element *enable_cmd
15728 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15729 Enable all or some breakpoints.\n\
15730 Usage: enable [BREAKPOINTNUM]...\n\
15731 Give breakpoint numbers (separated by spaces) as arguments.\n\
15732 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15733 This is used to cancel the effect of the \"disable\" command.\n\
15734 With a subcommand you can enable temporarily."),
15735 &enablelist, 1, &cmdlist);
15736
15737 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
15738
15739 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15740 Enable all or some breakpoints.\n\
15741 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
15742 Give breakpoint numbers (separated by spaces) as arguments.\n\
15743 This is used to cancel the effect of the \"disable\" command.\n\
15744 May be abbreviated to simply \"enable\"."),
15745 &enablebreaklist, 1, &enablelist);
15746
15747 add_cmd ("once", no_class, enable_once_command, _("\
15748 Enable some breakpoints for one hit.\n\
15749 Usage: enable breakpoints once BREAKPOINTNUM...\n\
15750 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15751 &enablebreaklist);
15752
15753 add_cmd ("delete", no_class, enable_delete_command, _("\
15754 Enable some breakpoints and delete when hit.\n\
15755 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
15756 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15757 &enablebreaklist);
15758
15759 add_cmd ("count", no_class, enable_count_command, _("\
15760 Enable some breakpoints for COUNT hits.\n\
15761 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
15762 If a breakpoint is hit while enabled in this fashion,\n\
15763 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15764 &enablebreaklist);
15765
15766 add_cmd ("delete", no_class, enable_delete_command, _("\
15767 Enable some breakpoints and delete when hit.\n\
15768 Usage: enable delete BREAKPOINTNUM...\n\
15769 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15770 &enablelist);
15771
15772 add_cmd ("once", no_class, enable_once_command, _("\
15773 Enable some breakpoints for one hit.\n\
15774 Usage: enable once BREAKPOINTNUM...\n\
15775 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15776 &enablelist);
15777
15778 add_cmd ("count", no_class, enable_count_command, _("\
15779 Enable some breakpoints for COUNT hits.\n\
15780 Usage: enable count COUNT BREAKPOINTNUM...\n\
15781 If a breakpoint is hit while enabled in this fashion,\n\
15782 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15783 &enablelist);
15784
15785 cmd_list_element *disable_cmd
15786 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15787 Disable all or some breakpoints.\n\
15788 Usage: disable [BREAKPOINTNUM]...\n\
15789 Arguments are breakpoint numbers with spaces in between.\n\
15790 To disable all breakpoints, give no argument.\n\
15791 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15792 &disablelist, 1, &cmdlist);
15793 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
15794 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
15795
15796 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
15797 Disable all or some breakpoints.\n\
15798 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
15799 Arguments are breakpoint numbers with spaces in between.\n\
15800 To disable all breakpoints, give no argument.\n\
15801 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15802 This command may be abbreviated \"disable\"."),
15803 &disablelist);
15804
15805 cmd_list_element *delete_cmd
15806 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15807 Delete all or some breakpoints.\n\
15808 Usage: delete [BREAKPOINTNUM]...\n\
15809 Arguments are breakpoint numbers with spaces in between.\n\
15810 To delete all breakpoints, give no argument.\n\
15811 \n\
15812 Also a prefix command for deletion of other GDB objects."),
15813 &deletelist, 1, &cmdlist);
15814 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
15815 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
15816
15817 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
15818 Delete all or some breakpoints or auto-display expressions.\n\
15819 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
15820 Arguments are breakpoint numbers with spaces in between.\n\
15821 To delete all breakpoints, give no argument.\n\
15822 This command may be abbreviated \"delete\"."),
15823 &deletelist);
15824
15825 cmd_list_element *clear_cmd
15826 = add_com ("clear", class_breakpoint, clear_command, _("\
15827 Clear breakpoint at specified location.\n\
15828 Argument may be a linespec, explicit, or address location as described below.\n\
15829 \n\
15830 With no argument, clears all breakpoints in the line that the selected frame\n\
15831 is executing in.\n"
15832 "\n" LOCATION_HELP_STRING "\n\n\
15833 See also the \"delete\" command which clears breakpoints by number."));
15834 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
15835
15836 cmd_list_element *break_cmd
15837 = add_com ("break", class_breakpoint, break_command, _("\
15838 Set breakpoint at specified location.\n"
15839 BREAK_ARGS_HELP ("break")));
15840 set_cmd_completer (break_cmd, location_completer);
15841
15842 add_com_alias ("b", break_cmd, class_run, 1);
15843 add_com_alias ("br", break_cmd, class_run, 1);
15844 add_com_alias ("bre", break_cmd, class_run, 1);
15845 add_com_alias ("brea", break_cmd, class_run, 1);
15846
15847 if (dbx_commands)
15848 {
15849 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15850 Break in function/address or break at a line in the current file."),
15851 &stoplist, 1, &cmdlist);
15852 add_cmd ("in", class_breakpoint, stopin_command,
15853 _("Break in function or address."), &stoplist);
15854 add_cmd ("at", class_breakpoint, stopat_command,
15855 _("Break at a line in the current file."), &stoplist);
15856 add_com ("status", class_info, info_breakpoints_command, _("\
15857 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15858 The \"Type\" column indicates one of:\n\
15859 \tbreakpoint - normal breakpoint\n\
15860 \twatchpoint - watchpoint\n\
15861 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15862 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15863 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15864 address and file/line number respectively.\n\
15865 \n\
15866 Convenience variable \"$_\" and default examine address for \"x\"\n\
15867 are set to the address of the last breakpoint listed unless the command\n\
15868 is prefixed with \"server \".\n\n\
15869 Convenience variable \"$bpnum\" contains the number of the last\n\
15870 breakpoint set."));
15871 }
15872
15873 cmd_list_element *info_breakpoints_cmd
15874 = add_info ("breakpoints", info_breakpoints_command, _("\
15875 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15876 The \"Type\" column indicates one of:\n\
15877 \tbreakpoint - normal breakpoint\n\
15878 \twatchpoint - watchpoint\n\
15879 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15880 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15881 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15882 address and file/line number respectively.\n\
15883 \n\
15884 Convenience variable \"$_\" and default examine address for \"x\"\n\
15885 are set to the address of the last breakpoint listed unless the command\n\
15886 is prefixed with \"server \".\n\n\
15887 Convenience variable \"$bpnum\" contains the number of the last\n\
15888 breakpoint set."));
15889
15890 add_info_alias ("b", info_breakpoints_cmd, 1);
15891
15892 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15893 Status of all breakpoints, or breakpoint number NUMBER.\n\
15894 The \"Type\" column indicates one of:\n\
15895 \tbreakpoint - normal breakpoint\n\
15896 \twatchpoint - watchpoint\n\
15897 \tlongjmp - internal breakpoint used to step through longjmp()\n\
15898 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15899 \tuntil - internal breakpoint used by the \"until\" command\n\
15900 \tfinish - internal breakpoint used by the \"finish\" command\n\
15901 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15902 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15903 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15904 address and file/line number respectively.\n\
15905 \n\
15906 Convenience variable \"$_\" and default examine address for \"x\"\n\
15907 are set to the address of the last breakpoint listed unless the command\n\
15908 is prefixed with \"server \".\n\n\
15909 Convenience variable \"$bpnum\" contains the number of the last\n\
15910 breakpoint set."),
15911 &maintenanceinfolist);
15912
15913 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
15914 Set catchpoints to catch events."),
15915 &catch_cmdlist,
15916 0/*allow-unknown*/, &cmdlist);
15917
15918 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
15919 Set temporary catchpoints to catch events."),
15920 &tcatch_cmdlist,
15921 0/*allow-unknown*/, &cmdlist);
15922
15923 add_catch_command ("fork", _("Catch calls to fork."),
15924 catch_fork_command_1,
15925 NULL,
15926 (void *) (uintptr_t) catch_fork_permanent,
15927 (void *) (uintptr_t) catch_fork_temporary);
15928 add_catch_command ("vfork", _("Catch calls to vfork."),
15929 catch_fork_command_1,
15930 NULL,
15931 (void *) (uintptr_t) catch_vfork_permanent,
15932 (void *) (uintptr_t) catch_vfork_temporary);
15933 add_catch_command ("exec", _("Catch calls to exec."),
15934 catch_exec_command_1,
15935 NULL,
15936 CATCH_PERMANENT,
15937 CATCH_TEMPORARY);
15938 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15939 Usage: catch load [REGEX]\n\
15940 If REGEX is given, only stop for libraries matching the regular expression."),
15941 catch_load_command_1,
15942 NULL,
15943 CATCH_PERMANENT,
15944 CATCH_TEMPORARY);
15945 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15946 Usage: catch unload [REGEX]\n\
15947 If REGEX is given, only stop for libraries matching the regular expression."),
15948 catch_unload_command_1,
15949 NULL,
15950 CATCH_PERMANENT,
15951 CATCH_TEMPORARY);
15952
15953 const auto opts = make_watch_options_def_group (nullptr);
15954
15955 static const std::string watch_help = gdb::option::build_help (_("\
15956 Set a watchpoint for EXPRESSION.\n\
15957 Usage: watch [-location] EXPRESSION\n\
15958 \n\
15959 Options:\n\
15960 %OPTIONS%\n\
15961 \n\
15962 A watchpoint stops execution of your program whenever the value of\n\
15963 an expression changes."), opts);
15964 c = add_com ("watch", class_breakpoint, watch_command,
15965 watch_help.c_str ());
15966 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15967
15968 static const std::string rwatch_help = gdb::option::build_help (_("\
15969 Set a read watchpoint for EXPRESSION.\n\
15970 Usage: rwatch [-location] EXPRESSION\n\
15971 \n\
15972 Options:\n\
15973 %OPTIONS%\n\
15974 \n\
15975 A read watchpoint stops execution of your program whenever the value of\n\
15976 an expression is read."), opts);
15977 c = add_com ("rwatch", class_breakpoint, rwatch_command,
15978 rwatch_help.c_str ());
15979 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15980
15981 static const std::string awatch_help = gdb::option::build_help (_("\
15982 Set an access watchpoint for EXPRESSION.\n\
15983 Usage: awatch [-location] EXPRESSION\n\
15984 \n\
15985 Options:\n\
15986 %OPTIONS%\n\
15987 \n\
15988 An access watchpoint stops execution of your program whenever the value\n\
15989 of an expression is either read or written."), opts);
15990 c = add_com ("awatch", class_breakpoint, awatch_command,
15991 awatch_help.c_str ());
15992 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15993
15994 add_info ("watchpoints", info_watchpoints_command, _("\
15995 Status of specified watchpoints (all watchpoints if no argument)."));
15996
15997 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15998 respond to changes - contrary to the description. */
15999 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16000 &can_use_hw_watchpoints, _("\
16001 Set debugger's willingness to use watchpoint hardware."), _("\
16002 Show debugger's willingness to use watchpoint hardware."), _("\
16003 If zero, gdb will not use hardware for new watchpoints, even if\n\
16004 such is available. (However, any hardware watchpoints that were\n\
16005 created before setting this to nonzero, will continue to use watchpoint\n\
16006 hardware.)"),
16007 NULL,
16008 show_can_use_hw_watchpoints,
16009 &setlist, &showlist);
16010
16011 can_use_hw_watchpoints = 1;
16012
16013 /* Tracepoint manipulation commands. */
16014
16015 cmd_list_element *trace_cmd
16016 = add_com ("trace", class_breakpoint, trace_command, _("\
16017 Set a tracepoint at specified location.\n\
16018 \n"
16019 BREAK_ARGS_HELP ("trace") "\n\
16020 Do \"help tracepoints\" for info on other tracepoint commands."));
16021 set_cmd_completer (trace_cmd, location_completer);
16022
16023 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
16024 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
16025 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
16026 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
16027
16028 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16029 Set a fast tracepoint at specified location.\n\
16030 \n"
16031 BREAK_ARGS_HELP ("ftrace") "\n\
16032 Do \"help tracepoints\" for info on other tracepoint commands."));
16033 set_cmd_completer (c, location_completer);
16034
16035 c = add_com ("strace", class_breakpoint, strace_command, _("\
16036 Set a static tracepoint at location or marker.\n\
16037 \n\
16038 strace [LOCATION] [if CONDITION]\n\
16039 LOCATION may be a linespec, explicit, or address location (described below) \n\
16040 or -m MARKER_ID.\n\n\
16041 If a marker id is specified, probe the marker with that name. With\n\
16042 no LOCATION, uses current execution address of the selected stack frame.\n\
16043 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16044 This collects arbitrary user data passed in the probe point call to the\n\
16045 tracing library. You can inspect it when analyzing the trace buffer,\n\
16046 by printing the $_sdata variable like any other convenience variable.\n\
16047 \n\
16048 CONDITION is a boolean expression.\n\
16049 \n" LOCATION_HELP_STRING "\n\n\
16050 Multiple tracepoints at one place are permitted, and useful if their\n\
16051 conditions are different.\n\
16052 \n\
16053 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16054 Do \"help tracepoints\" for info on other tracepoint commands."));
16055 set_cmd_completer (c, location_completer);
16056
16057 cmd_list_element *info_tracepoints_cmd
16058 = add_info ("tracepoints", info_tracepoints_command, _("\
16059 Status of specified tracepoints (all tracepoints if no argument).\n\
16060 Convenience variable \"$tpnum\" contains the number of the\n\
16061 last tracepoint set."));
16062
16063 add_info_alias ("tp", info_tracepoints_cmd, 1);
16064
16065 cmd_list_element *delete_tracepoints_cmd
16066 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16067 Delete specified tracepoints.\n\
16068 Arguments are tracepoint numbers, separated by spaces.\n\
16069 No argument means delete all tracepoints."),
16070 &deletelist);
16071 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
16072
16073 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16074 Disable specified tracepoints.\n\
16075 Arguments are tracepoint numbers, separated by spaces.\n\
16076 No argument means disable all tracepoints."),
16077 &disablelist);
16078 deprecate_cmd (c, "disable");
16079
16080 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16081 Enable specified tracepoints.\n\
16082 Arguments are tracepoint numbers, separated by spaces.\n\
16083 No argument means enable all tracepoints."),
16084 &enablelist);
16085 deprecate_cmd (c, "enable");
16086
16087 add_com ("passcount", class_trace, trace_pass_command, _("\
16088 Set the passcount for a tracepoint.\n\
16089 The trace will end when the tracepoint has been passed 'count' times.\n\
16090 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16091 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16092
16093 add_basic_prefix_cmd ("save", class_breakpoint,
16094 _("Save breakpoint definitions as a script."),
16095 &save_cmdlist,
16096 0/*allow-unknown*/, &cmdlist);
16097
16098 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16099 Save current breakpoint definitions as a script.\n\
16100 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16101 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16102 session to restore them."),
16103 &save_cmdlist);
16104 set_cmd_completer (c, filename_completer);
16105
16106 cmd_list_element *save_tracepoints_cmd
16107 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16108 Save current tracepoint definitions as a script.\n\
16109 Use the 'source' command in another debug session to restore them."),
16110 &save_cmdlist);
16111 set_cmd_completer (save_tracepoints_cmd, filename_completer);
16112
16113 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
16114 deprecate_cmd (c, "save tracepoints");
16115
16116 add_basic_prefix_cmd ("breakpoint", class_maintenance, _("\
16117 Breakpoint specific settings.\n\
16118 Configure various breakpoint-specific variables such as\n\
16119 pending breakpoint behavior."),
16120 &breakpoint_set_cmdlist,
16121 0/*allow-unknown*/, &setlist);
16122 add_show_prefix_cmd ("breakpoint", class_maintenance, _("\
16123 Breakpoint specific settings.\n\
16124 Configure various breakpoint-specific variables such as\n\
16125 pending breakpoint behavior."),
16126 &breakpoint_show_cmdlist,
16127 0/*allow-unknown*/, &showlist);
16128
16129 add_setshow_auto_boolean_cmd ("pending", no_class,
16130 &pending_break_support, _("\
16131 Set debugger's behavior regarding pending breakpoints."), _("\
16132 Show debugger's behavior regarding pending breakpoints."), _("\
16133 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16134 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16135 an error. If auto, an unrecognized breakpoint location results in a\n\
16136 user-query to see if a pending breakpoint should be created."),
16137 NULL,
16138 show_pending_break_support,
16139 &breakpoint_set_cmdlist,
16140 &breakpoint_show_cmdlist);
16141
16142 pending_break_support = AUTO_BOOLEAN_AUTO;
16143
16144 add_setshow_boolean_cmd ("auto-hw", no_class,
16145 &automatic_hardware_breakpoints, _("\
16146 Set automatic usage of hardware breakpoints."), _("\
16147 Show automatic usage of hardware breakpoints."), _("\
16148 If set, the debugger will automatically use hardware breakpoints for\n\
16149 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16150 a warning will be emitted for such breakpoints."),
16151 NULL,
16152 show_automatic_hardware_breakpoints,
16153 &breakpoint_set_cmdlist,
16154 &breakpoint_show_cmdlist);
16155
16156 add_setshow_boolean_cmd ("always-inserted", class_support,
16157 &always_inserted_mode, _("\
16158 Set mode for inserting breakpoints."), _("\
16159 Show mode for inserting breakpoints."), _("\
16160 When this mode is on, breakpoints are inserted immediately as soon as\n\
16161 they're created, kept inserted even when execution stops, and removed\n\
16162 only when the user deletes them. When this mode is off (the default),\n\
16163 breakpoints are inserted only when execution continues, and removed\n\
16164 when execution stops."),
16165 NULL,
16166 &show_always_inserted_mode,
16167 &breakpoint_set_cmdlist,
16168 &breakpoint_show_cmdlist);
16169
16170 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16171 condition_evaluation_enums,
16172 &condition_evaluation_mode_1, _("\
16173 Set mode of breakpoint condition evaluation."), _("\
16174 Show mode of breakpoint condition evaluation."), _("\
16175 When this is set to \"host\", breakpoint conditions will be\n\
16176 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16177 breakpoint conditions will be downloaded to the target (if the target\n\
16178 supports such feature) and conditions will be evaluated on the target's side.\n\
16179 If this is set to \"auto\" (default), this will be automatically set to\n\
16180 \"target\" if it supports condition evaluation, otherwise it will\n\
16181 be set to \"host\"."),
16182 &set_condition_evaluation_mode,
16183 &show_condition_evaluation_mode,
16184 &breakpoint_set_cmdlist,
16185 &breakpoint_show_cmdlist);
16186
16187 add_com ("break-range", class_breakpoint, break_range_command, _("\
16188 Set a breakpoint for an address range.\n\
16189 break-range START-LOCATION, END-LOCATION\n\
16190 where START-LOCATION and END-LOCATION can be one of the following:\n\
16191 LINENUM, for that line in the current file,\n\
16192 FILE:LINENUM, for that line in that file,\n\
16193 +OFFSET, for that number of lines after the current line\n\
16194 or the start of the range\n\
16195 FUNCTION, for the first line in that function,\n\
16196 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16197 *ADDRESS, for the instruction at that address.\n\
16198 \n\
16199 The breakpoint will stop execution of the inferior whenever it executes\n\
16200 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16201 range (including START-LOCATION and END-LOCATION)."));
16202
16203 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16204 Set a dynamic printf at specified location.\n\
16205 dprintf location,format string,arg1,arg2,...\n\
16206 location may be a linespec, explicit, or address location.\n"
16207 "\n" LOCATION_HELP_STRING));
16208 set_cmd_completer (c, location_completer);
16209
16210 add_setshow_enum_cmd ("dprintf-style", class_support,
16211 dprintf_style_enums, &dprintf_style, _("\
16212 Set the style of usage for dynamic printf."), _("\
16213 Show the style of usage for dynamic printf."), _("\
16214 This setting chooses how GDB will do a dynamic printf.\n\
16215 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16216 console, as with the \"printf\" command.\n\
16217 If the value is \"call\", the print is done by calling a function in your\n\
16218 program; by default printf(), but you can choose a different function or\n\
16219 output stream by setting dprintf-function and dprintf-channel."),
16220 update_dprintf_commands, NULL,
16221 &setlist, &showlist);
16222
16223 dprintf_function = xstrdup ("printf");
16224 add_setshow_string_cmd ("dprintf-function", class_support,
16225 &dprintf_function, _("\
16226 Set the function to use for dynamic printf."), _("\
16227 Show the function to use for dynamic printf."), NULL,
16228 update_dprintf_commands, NULL,
16229 &setlist, &showlist);
16230
16231 dprintf_channel = xstrdup ("");
16232 add_setshow_string_cmd ("dprintf-channel", class_support,
16233 &dprintf_channel, _("\
16234 Set the channel to use for dynamic printf."), _("\
16235 Show the channel to use for dynamic printf."), NULL,
16236 update_dprintf_commands, NULL,
16237 &setlist, &showlist);
16238
16239 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16240 &disconnected_dprintf, _("\
16241 Set whether dprintf continues after GDB disconnects."), _("\
16242 Show whether dprintf continues after GDB disconnects."), _("\
16243 Use this to let dprintf commands continue to hit and produce output\n\
16244 even if GDB disconnects or detaches from the target."),
16245 NULL,
16246 NULL,
16247 &setlist, &showlist);
16248
16249 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16250 Target agent only formatted printing, like the C \"printf\" function.\n\
16251 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
16252 This supports most C printf format specifications, like %s, %d, etc.\n\
16253 This is useful for formatted output in user-defined commands."));
16254
16255 automatic_hardware_breakpoints = true;
16256
16257 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
16258 "breakpoint");
16259 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
16260 "breakpoint");
16261 }
This page took 0.547763 seconds and 5 git commands to generate.