[gdb/symtab] Fix element type modification in read_array_type
[binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2021 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include <ctype.h>
23 #include "hashtab.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "breakpoint.h"
27 #include "tracepoint.h"
28 #include "gdbtypes.h"
29 #include "expression.h"
30 #include "gdbcore.h"
31 #include "gdbcmd.h"
32 #include "value.h"
33 #include "command.h"
34 #include "inferior.h"
35 #include "infrun.h"
36 #include "gdbthread.h"
37 #include "target.h"
38 #include "language.h"
39 #include "gdb-demangle.h"
40 #include "filenames.h"
41 #include "annotate.h"
42 #include "symfile.h"
43 #include "objfiles.h"
44 #include "source.h"
45 #include "linespec.h"
46 #include "completer.h"
47 #include "ui-out.h"
48 #include "cli/cli-script.h"
49 #include "block.h"
50 #include "solib.h"
51 #include "solist.h"
52 #include "observable.h"
53 #include "memattr.h"
54 #include "ada-lang.h"
55 #include "top.h"
56 #include "valprint.h"
57 #include "jit.h"
58 #include "parser-defs.h"
59 #include "gdb_regex.h"
60 #include "probe.h"
61 #include "cli/cli-utils.h"
62 #include "stack.h"
63 #include "ax-gdb.h"
64 #include "dummy-frame.h"
65 #include "interps.h"
66 #include "gdbsupport/format.h"
67 #include "thread-fsm.h"
68 #include "tid-parse.h"
69 #include "cli/cli-style.h"
70
71 /* readline include files */
72 #include "readline/tilde.h"
73
74 /* readline defines this. */
75 #undef savestring
76
77 #include "mi/mi-common.h"
78 #include "extension.h"
79 #include <algorithm>
80 #include "progspace-and-thread.h"
81 #include "gdbsupport/array-view.h"
82 #include "gdbsupport/gdb_optional.h"
83
84 /* Prototypes for local functions. */
85
86 static void map_breakpoint_numbers (const char *,
87 gdb::function_view<void (breakpoint *)>);
88
89 static void breakpoint_re_set_default (struct breakpoint *);
90
91 static void
92 create_sals_from_location_default (struct event_location *location,
93 struct linespec_result *canonical,
94 enum bptype type_wanted);
95
96 static void create_breakpoints_sal_default (struct gdbarch *,
97 struct linespec_result *,
98 gdb::unique_xmalloc_ptr<char>,
99 gdb::unique_xmalloc_ptr<char>,
100 enum bptype,
101 enum bpdisp, int, int,
102 int,
103 const struct breakpoint_ops *,
104 int, int, int, unsigned);
105
106 static std::vector<symtab_and_line> decode_location_default
107 (struct breakpoint *b, struct event_location *location,
108 struct program_space *search_pspace);
109
110 static int can_use_hardware_watchpoint
111 (const std::vector<value_ref_ptr> &vals);
112
113 static void mention (struct breakpoint *);
114
115 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
116 enum bptype,
117 const struct breakpoint_ops *);
118 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
119 const struct symtab_and_line *);
120
121 /* This function is used in gdbtk sources and thus can not be made
122 static. */
123 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
124 struct symtab_and_line,
125 enum bptype,
126 const struct breakpoint_ops *);
127
128 static struct breakpoint *
129 momentary_breakpoint_from_master (struct breakpoint *orig,
130 enum bptype type,
131 const struct breakpoint_ops *ops,
132 int loc_enabled);
133
134 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
135
136 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
137 CORE_ADDR bpaddr,
138 enum bptype bptype);
139
140 static void describe_other_breakpoints (struct gdbarch *,
141 struct program_space *, CORE_ADDR,
142 struct obj_section *, int);
143
144 static int watchpoint_locations_match (struct bp_location *loc1,
145 struct bp_location *loc2);
146
147 static int breakpoint_locations_match (struct bp_location *loc1,
148 struct bp_location *loc2,
149 bool sw_hw_bps_match = false);
150
151 static int breakpoint_location_address_match (struct bp_location *bl,
152 const struct address_space *aspace,
153 CORE_ADDR addr);
154
155 static int breakpoint_location_address_range_overlap (struct bp_location *,
156 const address_space *,
157 CORE_ADDR, int);
158
159 static int remove_breakpoint (struct bp_location *);
160 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
161
162 static enum print_stop_action print_bp_stop_message (bpstat bs);
163
164 static int hw_breakpoint_used_count (void);
165
166 static int hw_watchpoint_use_count (struct breakpoint *);
167
168 static int hw_watchpoint_used_count_others (struct breakpoint *except,
169 enum bptype type,
170 int *other_type_used);
171
172 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
173 int count);
174
175 static void decref_bp_location (struct bp_location **loc);
176
177 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
178
179 /* update_global_location_list's modes of operation wrt to whether to
180 insert locations now. */
181 enum ugll_insert_mode
182 {
183 /* Don't insert any breakpoint locations into the inferior, only
184 remove already-inserted locations that no longer should be
185 inserted. Functions that delete a breakpoint or breakpoints
186 should specify this mode, so that deleting a breakpoint doesn't
187 have the side effect of inserting the locations of other
188 breakpoints that are marked not-inserted, but should_be_inserted
189 returns true on them.
190
191 This behavior is useful is situations close to tear-down -- e.g.,
192 after an exec, while the target still has execution, but
193 breakpoint shadows of the previous executable image should *NOT*
194 be restored to the new image; or before detaching, where the
195 target still has execution and wants to delete breakpoints from
196 GDB's lists, and all breakpoints had already been removed from
197 the inferior. */
198 UGLL_DONT_INSERT,
199
200 /* May insert breakpoints iff breakpoints_should_be_inserted_now
201 claims breakpoints should be inserted now. */
202 UGLL_MAY_INSERT,
203
204 /* Insert locations now, irrespective of
205 breakpoints_should_be_inserted_now. E.g., say all threads are
206 stopped right now, and the user did "continue". We need to
207 insert breakpoints _before_ resuming the target, but
208 UGLL_MAY_INSERT wouldn't insert them, because
209 breakpoints_should_be_inserted_now returns false at that point,
210 as no thread is running yet. */
211 UGLL_INSERT
212 };
213
214 static void update_global_location_list (enum ugll_insert_mode);
215
216 static void update_global_location_list_nothrow (enum ugll_insert_mode);
217
218 static void insert_breakpoint_locations (void);
219
220 static void trace_pass_command (const char *, int);
221
222 static void set_tracepoint_count (int num);
223
224 static bool is_masked_watchpoint (const struct breakpoint *b);
225
226 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
227
228 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
229 otherwise. */
230
231 static int strace_marker_p (struct breakpoint *b);
232
233 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
234 that are implemented on top of software or hardware breakpoints
235 (user breakpoints, internal and momentary breakpoints, etc.). */
236 static struct breakpoint_ops bkpt_base_breakpoint_ops;
237
238 /* Internal breakpoints class type. */
239 static struct breakpoint_ops internal_breakpoint_ops;
240
241 /* Momentary breakpoints class type. */
242 static struct breakpoint_ops momentary_breakpoint_ops;
243
244 /* The breakpoint_ops structure to be used in regular user created
245 breakpoints. */
246 struct breakpoint_ops bkpt_breakpoint_ops;
247
248 /* Breakpoints set on probes. */
249 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
250
251 /* Tracepoints set on probes. */
252 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
253
254 /* Dynamic printf class type. */
255 struct breakpoint_ops dprintf_breakpoint_ops;
256
257 /* The style in which to perform a dynamic printf. This is a user
258 option because different output options have different tradeoffs;
259 if GDB does the printing, there is better error handling if there
260 is a problem with any of the arguments, but using an inferior
261 function lets you have special-purpose printers and sending of
262 output to the same place as compiled-in print functions. */
263
264 static const char dprintf_style_gdb[] = "gdb";
265 static const char dprintf_style_call[] = "call";
266 static const char dprintf_style_agent[] = "agent";
267 static const char *const dprintf_style_enums[] = {
268 dprintf_style_gdb,
269 dprintf_style_call,
270 dprintf_style_agent,
271 NULL
272 };
273 static const char *dprintf_style = dprintf_style_gdb;
274
275 /* The function to use for dynamic printf if the preferred style is to
276 call into the inferior. The value is simply a string that is
277 copied into the command, so it can be anything that GDB can
278 evaluate to a callable address, not necessarily a function name. */
279
280 static char *dprintf_function;
281
282 /* The channel to use for dynamic printf if the preferred style is to
283 call into the inferior; if a nonempty string, it will be passed to
284 the call as the first argument, with the format string as the
285 second. As with the dprintf function, this can be anything that
286 GDB knows how to evaluate, so in addition to common choices like
287 "stderr", this could be an app-specific expression like
288 "mystreams[curlogger]". */
289
290 static char *dprintf_channel;
291
292 /* True if dprintf commands should continue to operate even if GDB
293 has disconnected. */
294 static bool disconnected_dprintf = true;
295
296 struct command_line *
297 breakpoint_commands (struct breakpoint *b)
298 {
299 return b->commands ? b->commands.get () : NULL;
300 }
301
302 /* Flag indicating that a command has proceeded the inferior past the
303 current breakpoint. */
304
305 static bool breakpoint_proceeded;
306
307 const char *
308 bpdisp_text (enum bpdisp disp)
309 {
310 /* NOTE: the following values are a part of MI protocol and
311 represent values of 'disp' field returned when inferior stops at
312 a breakpoint. */
313 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
314
315 return bpdisps[(int) disp];
316 }
317
318 /* Prototypes for exported functions. */
319 /* If FALSE, gdb will not use hardware support for watchpoints, even
320 if such is available. */
321 static int can_use_hw_watchpoints;
322
323 static void
324 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
325 struct cmd_list_element *c,
326 const char *value)
327 {
328 fprintf_filtered (file,
329 _("Debugger's willingness to use "
330 "watchpoint hardware is %s.\n"),
331 value);
332 }
333
334 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
335 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
336 for unrecognized breakpoint locations.
337 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
338 static enum auto_boolean pending_break_support;
339 static void
340 show_pending_break_support (struct ui_file *file, int from_tty,
341 struct cmd_list_element *c,
342 const char *value)
343 {
344 fprintf_filtered (file,
345 _("Debugger's behavior regarding "
346 "pending breakpoints is %s.\n"),
347 value);
348 }
349
350 /* If true, gdb will automatically use hardware breakpoints for breakpoints
351 set with "break" but falling in read-only memory.
352 If false, gdb will warn about such breakpoints, but won't automatically
353 use hardware breakpoints. */
354 static bool automatic_hardware_breakpoints;
355 static void
356 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
357 struct cmd_list_element *c,
358 const char *value)
359 {
360 fprintf_filtered (file,
361 _("Automatic usage of hardware breakpoints is %s.\n"),
362 value);
363 }
364
365 /* If on, GDB keeps breakpoints inserted even if the inferior is
366 stopped, and immediately inserts any new breakpoints as soon as
367 they're created. If off (default), GDB keeps breakpoints off of
368 the target as long as possible. That is, it delays inserting
369 breakpoints until the next resume, and removes them again when the
370 target fully stops. This is a bit safer in case GDB crashes while
371 processing user input. */
372 static bool always_inserted_mode = false;
373
374 static void
375 show_always_inserted_mode (struct ui_file *file, int from_tty,
376 struct cmd_list_element *c, const char *value)
377 {
378 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
379 value);
380 }
381
382 /* See breakpoint.h. */
383
384 int
385 breakpoints_should_be_inserted_now (void)
386 {
387 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
388 {
389 /* If breakpoints are global, they should be inserted even if no
390 thread under gdb's control is running, or even if there are
391 no threads under GDB's control yet. */
392 return 1;
393 }
394 else
395 {
396 if (always_inserted_mode)
397 {
398 /* The user wants breakpoints inserted even if all threads
399 are stopped. */
400 return 1;
401 }
402
403 for (inferior *inf : all_inferiors ())
404 if (inf->has_execution ()
405 && threads_are_executing (inf->process_target ()))
406 return 1;
407
408 /* Don't remove breakpoints yet if, even though all threads are
409 stopped, we still have events to process. */
410 for (thread_info *tp : all_non_exited_threads ())
411 if (tp->resumed
412 && tp->suspend.waitstatus_pending_p)
413 return 1;
414 }
415 return 0;
416 }
417
418 static const char condition_evaluation_both[] = "host or target";
419
420 /* Modes for breakpoint condition evaluation. */
421 static const char condition_evaluation_auto[] = "auto";
422 static const char condition_evaluation_host[] = "host";
423 static const char condition_evaluation_target[] = "target";
424 static const char *const condition_evaluation_enums[] = {
425 condition_evaluation_auto,
426 condition_evaluation_host,
427 condition_evaluation_target,
428 NULL
429 };
430
431 /* Global that holds the current mode for breakpoint condition evaluation. */
432 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
433
434 /* Global that we use to display information to the user (gets its value from
435 condition_evaluation_mode_1. */
436 static const char *condition_evaluation_mode = condition_evaluation_auto;
437
438 /* Translate a condition evaluation mode MODE into either "host"
439 or "target". This is used mostly to translate from "auto" to the
440 real setting that is being used. It returns the translated
441 evaluation mode. */
442
443 static const char *
444 translate_condition_evaluation_mode (const char *mode)
445 {
446 if (mode == condition_evaluation_auto)
447 {
448 if (target_supports_evaluation_of_breakpoint_conditions ())
449 return condition_evaluation_target;
450 else
451 return condition_evaluation_host;
452 }
453 else
454 return mode;
455 }
456
457 /* Discovers what condition_evaluation_auto translates to. */
458
459 static const char *
460 breakpoint_condition_evaluation_mode (void)
461 {
462 return translate_condition_evaluation_mode (condition_evaluation_mode);
463 }
464
465 /* Return true if GDB should evaluate breakpoint conditions or false
466 otherwise. */
467
468 static int
469 gdb_evaluates_breakpoint_condition_p (void)
470 {
471 const char *mode = breakpoint_condition_evaluation_mode ();
472
473 return (mode == condition_evaluation_host);
474 }
475
476 /* Are we executing breakpoint commands? */
477 static int executing_breakpoint_commands;
478
479 /* Are overlay event breakpoints enabled? */
480 static int overlay_events_enabled;
481
482 /* See description in breakpoint.h. */
483 bool target_exact_watchpoints = false;
484
485 /* Walk the following statement or block through all breakpoints.
486 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
487 current breakpoint. */
488
489 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
490
491 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
492 for (B = breakpoint_chain; \
493 B ? (TMP=B->next, 1): 0; \
494 B = TMP)
495
496 /* Similar iterator for the low-level breakpoints. SAFE variant is
497 not provided so update_global_location_list must not be called
498 while executing the block of ALL_BP_LOCATIONS. */
499
500 #define ALL_BP_LOCATIONS(B,BP_TMP) \
501 for (BP_TMP = bp_locations; \
502 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
503 BP_TMP++)
504
505 /* Iterates through locations with address ADDRESS for the currently selected
506 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
507 to where the loop should start from.
508 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
509 appropriate location to start with. */
510
511 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
512 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
513 BP_LOCP_TMP = BP_LOCP_START; \
514 BP_LOCP_START \
515 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
516 && (*BP_LOCP_TMP)->address == ADDRESS); \
517 BP_LOCP_TMP++)
518
519 /* Iterator for tracepoints only. */
520
521 #define ALL_TRACEPOINTS(B) \
522 for (B = breakpoint_chain; B; B = B->next) \
523 if (is_tracepoint (B))
524
525 /* Chains of all breakpoints defined. */
526
527 static struct breakpoint *breakpoint_chain;
528
529 /* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
530
531 static struct bp_location **bp_locations;
532
533 /* Number of elements of BP_LOCATIONS. */
534
535 static unsigned bp_locations_count;
536
537 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
538 ADDRESS for the current elements of BP_LOCATIONS which get a valid
539 result from bp_location_has_shadow. You can use it for roughly
540 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
541 an address you need to read. */
542
543 static CORE_ADDR bp_locations_placed_address_before_address_max;
544
545 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
546 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
547 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
548 You can use it for roughly limiting the subrange of BP_LOCATIONS to
549 scan for shadow bytes for an address you need to read. */
550
551 static CORE_ADDR bp_locations_shadow_len_after_address_max;
552
553 /* The locations that no longer correspond to any breakpoint, unlinked
554 from the bp_locations array, but for which a hit may still be
555 reported by a target. */
556 static std::vector<bp_location *> moribund_locations;
557
558 /* Number of last breakpoint made. */
559
560 static int breakpoint_count;
561
562 /* The value of `breakpoint_count' before the last command that
563 created breakpoints. If the last (break-like) command created more
564 than one breakpoint, then the difference between BREAKPOINT_COUNT
565 and PREV_BREAKPOINT_COUNT is more than one. */
566 static int prev_breakpoint_count;
567
568 /* Number of last tracepoint made. */
569
570 static int tracepoint_count;
571
572 static struct cmd_list_element *breakpoint_set_cmdlist;
573 static struct cmd_list_element *breakpoint_show_cmdlist;
574 struct cmd_list_element *save_cmdlist;
575
576 /* See declaration at breakpoint.h. */
577
578 struct breakpoint *
579 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
580 void *user_data)
581 {
582 struct breakpoint *b = NULL;
583
584 ALL_BREAKPOINTS (b)
585 {
586 if (func (b, user_data) != 0)
587 break;
588 }
589
590 return b;
591 }
592
593 /* Return whether a breakpoint is an active enabled breakpoint. */
594 static int
595 breakpoint_enabled (struct breakpoint *b)
596 {
597 return (b->enable_state == bp_enabled);
598 }
599
600 /* Set breakpoint count to NUM. */
601
602 static void
603 set_breakpoint_count (int num)
604 {
605 prev_breakpoint_count = breakpoint_count;
606 breakpoint_count = num;
607 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
608 }
609
610 /* Used by `start_rbreak_breakpoints' below, to record the current
611 breakpoint count before "rbreak" creates any breakpoint. */
612 static int rbreak_start_breakpoint_count;
613
614 /* Called at the start an "rbreak" command to record the first
615 breakpoint made. */
616
617 scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
618 {
619 rbreak_start_breakpoint_count = breakpoint_count;
620 }
621
622 /* Called at the end of an "rbreak" command to record the last
623 breakpoint made. */
624
625 scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
626 {
627 prev_breakpoint_count = rbreak_start_breakpoint_count;
628 }
629
630 /* Used in run_command to zero the hit count when a new run starts. */
631
632 void
633 clear_breakpoint_hit_counts (void)
634 {
635 struct breakpoint *b;
636
637 ALL_BREAKPOINTS (b)
638 b->hit_count = 0;
639 }
640
641 \f
642 /* Return the breakpoint with the specified number, or NULL
643 if the number does not refer to an existing breakpoint. */
644
645 struct breakpoint *
646 get_breakpoint (int num)
647 {
648 struct breakpoint *b;
649
650 ALL_BREAKPOINTS (b)
651 if (b->number == num)
652 return b;
653
654 return NULL;
655 }
656
657 \f
658
659 /* Mark locations as "conditions have changed" in case the target supports
660 evaluating conditions on its side. */
661
662 static void
663 mark_breakpoint_modified (struct breakpoint *b)
664 {
665 struct bp_location *loc;
666
667 /* This is only meaningful if the target is
668 evaluating conditions and if the user has
669 opted for condition evaluation on the target's
670 side. */
671 if (gdb_evaluates_breakpoint_condition_p ()
672 || !target_supports_evaluation_of_breakpoint_conditions ())
673 return;
674
675 if (!is_breakpoint (b))
676 return;
677
678 for (loc = b->loc; loc; loc = loc->next)
679 loc->condition_changed = condition_modified;
680 }
681
682 /* Mark location as "conditions have changed" in case the target supports
683 evaluating conditions on its side. */
684
685 static void
686 mark_breakpoint_location_modified (struct bp_location *loc)
687 {
688 /* This is only meaningful if the target is
689 evaluating conditions and if the user has
690 opted for condition evaluation on the target's
691 side. */
692 if (gdb_evaluates_breakpoint_condition_p ()
693 || !target_supports_evaluation_of_breakpoint_conditions ())
694
695 return;
696
697 if (!is_breakpoint (loc->owner))
698 return;
699
700 loc->condition_changed = condition_modified;
701 }
702
703 /* Sets the condition-evaluation mode using the static global
704 condition_evaluation_mode. */
705
706 static void
707 set_condition_evaluation_mode (const char *args, int from_tty,
708 struct cmd_list_element *c)
709 {
710 const char *old_mode, *new_mode;
711
712 if ((condition_evaluation_mode_1 == condition_evaluation_target)
713 && !target_supports_evaluation_of_breakpoint_conditions ())
714 {
715 condition_evaluation_mode_1 = condition_evaluation_mode;
716 warning (_("Target does not support breakpoint condition evaluation.\n"
717 "Using host evaluation mode instead."));
718 return;
719 }
720
721 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
722 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
723
724 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
725 settings was "auto". */
726 condition_evaluation_mode = condition_evaluation_mode_1;
727
728 /* Only update the mode if the user picked a different one. */
729 if (new_mode != old_mode)
730 {
731 struct bp_location *loc, **loc_tmp;
732 /* If the user switched to a different evaluation mode, we
733 need to synch the changes with the target as follows:
734
735 "host" -> "target": Send all (valid) conditions to the target.
736 "target" -> "host": Remove all the conditions from the target.
737 */
738
739 if (new_mode == condition_evaluation_target)
740 {
741 /* Mark everything modified and synch conditions with the
742 target. */
743 ALL_BP_LOCATIONS (loc, loc_tmp)
744 mark_breakpoint_location_modified (loc);
745 }
746 else
747 {
748 /* Manually mark non-duplicate locations to synch conditions
749 with the target. We do this to remove all the conditions the
750 target knows about. */
751 ALL_BP_LOCATIONS (loc, loc_tmp)
752 if (is_breakpoint (loc->owner) && loc->inserted)
753 loc->needs_update = 1;
754 }
755
756 /* Do the update. */
757 update_global_location_list (UGLL_MAY_INSERT);
758 }
759
760 return;
761 }
762
763 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
764 what "auto" is translating to. */
765
766 static void
767 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
768 struct cmd_list_element *c, const char *value)
769 {
770 if (condition_evaluation_mode == condition_evaluation_auto)
771 fprintf_filtered (file,
772 _("Breakpoint condition evaluation "
773 "mode is %s (currently %s).\n"),
774 value,
775 breakpoint_condition_evaluation_mode ());
776 else
777 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
778 value);
779 }
780
781 /* A comparison function for bp_location AP and BP that is used by
782 bsearch. This comparison function only cares about addresses, unlike
783 the more general bp_location_is_less_than function. */
784
785 static int
786 bp_locations_compare_addrs (const void *ap, const void *bp)
787 {
788 const struct bp_location *a = *(const struct bp_location **) ap;
789 const struct bp_location *b = *(const struct bp_location **) bp;
790
791 if (a->address == b->address)
792 return 0;
793 else
794 return ((a->address > b->address) - (a->address < b->address));
795 }
796
797 /* Helper function to skip all bp_locations with addresses
798 less than ADDRESS. It returns the first bp_location that
799 is greater than or equal to ADDRESS. If none is found, just
800 return NULL. */
801
802 static struct bp_location **
803 get_first_locp_gte_addr (CORE_ADDR address)
804 {
805 struct bp_location dummy_loc;
806 struct bp_location *dummy_locp = &dummy_loc;
807 struct bp_location **locp_found = NULL;
808
809 /* Initialize the dummy location's address field. */
810 dummy_loc.address = address;
811
812 /* Find a close match to the first location at ADDRESS. */
813 locp_found = ((struct bp_location **)
814 bsearch (&dummy_locp, bp_locations, bp_locations_count,
815 sizeof (struct bp_location **),
816 bp_locations_compare_addrs));
817
818 /* Nothing was found, nothing left to do. */
819 if (locp_found == NULL)
820 return NULL;
821
822 /* We may have found a location that is at ADDRESS but is not the first in the
823 location's list. Go backwards (if possible) and locate the first one. */
824 while ((locp_found - 1) >= bp_locations
825 && (*(locp_found - 1))->address == address)
826 locp_found--;
827
828 return locp_found;
829 }
830
831 /* Parse COND_STRING in the context of LOC and set as the condition
832 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
833 the number of LOC within its owner. In case of parsing error, mark
834 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
835
836 static void
837 set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
838 int bp_num, int loc_num)
839 {
840 bool has_junk = false;
841 try
842 {
843 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
844 block_for_pc (loc->address), 0);
845 if (*cond_string != 0)
846 has_junk = true;
847 else
848 {
849 loc->cond = std::move (new_exp);
850 if (loc->disabled_by_cond && loc->enabled)
851 printf_filtered (_("Breakpoint %d's condition is now valid at "
852 "location %d, enabling.\n"),
853 bp_num, loc_num);
854
855 loc->disabled_by_cond = false;
856 }
857 }
858 catch (const gdb_exception_error &e)
859 {
860 if (loc->enabled)
861 {
862 /* Warn if a user-enabled location is now becoming disabled-by-cond.
863 BP_NUM is 0 if the breakpoint is being defined for the first
864 time using the "break ... if ..." command, and non-zero if
865 already defined. */
866 if (bp_num != 0)
867 warning (_("failed to validate condition at location %d.%d, "
868 "disabling:\n %s"), bp_num, loc_num, e.what ());
869 else
870 warning (_("failed to validate condition at location %d, "
871 "disabling:\n %s"), loc_num, e.what ());
872 }
873
874 loc->disabled_by_cond = true;
875 }
876
877 if (has_junk)
878 error (_("Garbage '%s' follows condition"), cond_string);
879 }
880
881 void
882 set_breakpoint_condition (struct breakpoint *b, const char *exp,
883 int from_tty, bool force)
884 {
885 if (*exp == 0)
886 {
887 xfree (b->cond_string);
888 b->cond_string = nullptr;
889
890 if (is_watchpoint (b))
891 static_cast<watchpoint *> (b)->cond_exp.reset ();
892 else
893 {
894 int loc_num = 1;
895 for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)
896 {
897 loc->cond.reset ();
898 if (loc->disabled_by_cond && loc->enabled)
899 printf_filtered (_("Breakpoint %d's condition is now valid at "
900 "location %d, enabling.\n"),
901 b->number, loc_num);
902 loc->disabled_by_cond = false;
903 loc_num++;
904
905 /* No need to free the condition agent expression
906 bytecode (if we have one). We will handle this
907 when we go through update_global_location_list. */
908 }
909 }
910
911 if (from_tty)
912 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
913 }
914 else
915 {
916 if (is_watchpoint (b))
917 {
918 innermost_block_tracker tracker;
919 const char *arg = exp;
920 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
921 if (*arg != 0)
922 error (_("Junk at end of expression"));
923 watchpoint *w = static_cast<watchpoint *> (b);
924 w->cond_exp = std::move (new_exp);
925 w->cond_exp_valid_block = tracker.block ();
926 }
927 else
928 {
929 /* Parse and set condition expressions. We make two passes.
930 In the first, we parse the condition string to see if it
931 is valid in at least one location. If so, the condition
932 would be accepted. So we go ahead and set the locations'
933 conditions. In case no valid case is found, we throw
934 the error and the condition string will be rejected.
935 This two-pass approach is taken to avoid setting the
936 state of locations in case of a reject. */
937 for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)
938 {
939 try
940 {
941 const char *arg = exp;
942 parse_exp_1 (&arg, loc->address,
943 block_for_pc (loc->address), 0);
944 if (*arg != 0)
945 error (_("Junk at end of expression"));
946 break;
947 }
948 catch (const gdb_exception_error &e)
949 {
950 /* Condition string is invalid. If this happens to
951 be the last loc, abandon (if not forced) or continue
952 (if forced). */
953 if (loc->next == nullptr && !force)
954 throw;
955 }
956 }
957
958 /* If we reach here, the condition is valid at some locations. */
959 int loc_num = 1;
960 for (bp_location *loc = b->loc; loc != nullptr;
961 loc = loc->next, loc_num++)
962 set_breakpoint_location_condition (exp, loc, b->number, loc_num);
963 }
964
965 /* We know that the new condition parsed successfully. The
966 condition string of the breakpoint can be safely updated. */
967 xfree (b->cond_string);
968 b->cond_string = xstrdup (exp);
969 b->condition_not_parsed = 0;
970 }
971 mark_breakpoint_modified (b);
972
973 gdb::observers::breakpoint_modified.notify (b);
974 }
975
976 /* The options for the "condition" command. */
977
978 struct condition_command_opts
979 {
980 /* For "-force". */
981 bool force_condition = false;
982 };
983
984 static const gdb::option::option_def condition_command_option_defs[] = {
985
986 gdb::option::flag_option_def<condition_command_opts> {
987 "force",
988 [] (condition_command_opts *opts) { return &opts->force_condition; },
989 N_("Set the condition even if it is invalid for all current locations."),
990 },
991
992 };
993
994 /* Create an option_def_group for the "condition" options, with
995 CC_OPTS as context. */
996
997 static inline gdb::option::option_def_group
998 make_condition_command_options_def_group (condition_command_opts *cc_opts)
999 {
1000 return {{condition_command_option_defs}, cc_opts};
1001 }
1002
1003 /* Completion for the "condition" command. */
1004
1005 static void
1006 condition_completer (struct cmd_list_element *cmd,
1007 completion_tracker &tracker,
1008 const char *text, const char * /*word*/)
1009 {
1010 bool has_no_arguments = (*text == '\0');
1011 condition_command_opts cc_opts;
1012 const auto group = make_condition_command_options_def_group (&cc_opts);
1013 if (gdb::option::complete_options
1014 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1015 return;
1016
1017 text = skip_spaces (text);
1018 const char *space = skip_to_space (text);
1019 if (*space == '\0')
1020 {
1021 int len;
1022 struct breakpoint *b;
1023
1024 if (text[0] == '$')
1025 {
1026 tracker.advance_custom_word_point_by (1);
1027 /* We don't support completion of history indices. */
1028 if (!isdigit (text[1]))
1029 complete_internalvar (tracker, &text[1]);
1030 return;
1031 }
1032
1033 /* Suggest the "-force" flag if no arguments are given. If
1034 arguments were passed, they either already include the flag,
1035 or we are beyond the point of suggesting it because it's
1036 positionally the first argument. */
1037 if (has_no_arguments)
1038 gdb::option::complete_on_all_options (tracker, group);
1039
1040 /* We're completing the breakpoint number. */
1041 len = strlen (text);
1042
1043 ALL_BREAKPOINTS (b)
1044 {
1045 char number[50];
1046
1047 xsnprintf (number, sizeof (number), "%d", b->number);
1048
1049 if (strncmp (number, text, len) == 0)
1050 tracker.add_completion (make_unique_xstrdup (number));
1051 }
1052
1053 return;
1054 }
1055
1056 /* We're completing the expression part. Skip the breakpoint num. */
1057 const char *exp_start = skip_spaces (space);
1058 tracker.advance_custom_word_point_by (exp_start - text);
1059 text = exp_start;
1060 const char *word = advance_to_expression_complete_word_point (tracker, text);
1061 expression_completer (cmd, tracker, text, word);
1062 }
1063
1064 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1065
1066 static void
1067 condition_command (const char *arg, int from_tty)
1068 {
1069 struct breakpoint *b;
1070 const char *p;
1071 int bnum;
1072
1073 if (arg == 0)
1074 error_no_arg (_("breakpoint number"));
1075
1076 p = arg;
1077
1078 /* Check if the "-force" flag was passed. */
1079 condition_command_opts cc_opts;
1080 const auto group = make_condition_command_options_def_group (&cc_opts);
1081 gdb::option::process_options
1082 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
1083
1084 bnum = get_number (&p);
1085 if (bnum == 0)
1086 error (_("Bad breakpoint argument: '%s'"), arg);
1087
1088 ALL_BREAKPOINTS (b)
1089 if (b->number == bnum)
1090 {
1091 /* Check if this breakpoint has a "stop" method implemented in an
1092 extension language. This method and conditions entered into GDB
1093 from the CLI are mutually exclusive. */
1094 const struct extension_language_defn *extlang
1095 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1096
1097 if (extlang != NULL)
1098 {
1099 error (_("Only one stop condition allowed. There is currently"
1100 " a %s stop condition defined for this breakpoint."),
1101 ext_lang_capitalized_name (extlang));
1102 }
1103 set_breakpoint_condition (b, p, from_tty, cc_opts.force_condition);
1104
1105 if (is_breakpoint (b))
1106 update_global_location_list (UGLL_MAY_INSERT);
1107
1108 return;
1109 }
1110
1111 error (_("No breakpoint number %d."), bnum);
1112 }
1113
1114 /* Check that COMMAND do not contain commands that are suitable
1115 only for tracepoints and not suitable for ordinary breakpoints.
1116 Throw if any such commands is found. */
1117
1118 static void
1119 check_no_tracepoint_commands (struct command_line *commands)
1120 {
1121 struct command_line *c;
1122
1123 for (c = commands; c; c = c->next)
1124 {
1125 if (c->control_type == while_stepping_control)
1126 error (_("The 'while-stepping' command can "
1127 "only be used for tracepoints"));
1128
1129 check_no_tracepoint_commands (c->body_list_0.get ());
1130 check_no_tracepoint_commands (c->body_list_1.get ());
1131
1132 /* Not that command parsing removes leading whitespace and comment
1133 lines and also empty lines. So, we only need to check for
1134 command directly. */
1135 if (strstr (c->line, "collect ") == c->line)
1136 error (_("The 'collect' command can only be used for tracepoints"));
1137
1138 if (strstr (c->line, "teval ") == c->line)
1139 error (_("The 'teval' command can only be used for tracepoints"));
1140 }
1141 }
1142
1143 struct longjmp_breakpoint : public breakpoint
1144 {
1145 ~longjmp_breakpoint () override;
1146 };
1147
1148 /* Encapsulate tests for different types of tracepoints. */
1149
1150 static bool
1151 is_tracepoint_type (bptype type)
1152 {
1153 return (type == bp_tracepoint
1154 || type == bp_fast_tracepoint
1155 || type == bp_static_tracepoint);
1156 }
1157
1158 static bool
1159 is_longjmp_type (bptype type)
1160 {
1161 return type == bp_longjmp || type == bp_exception;
1162 }
1163
1164 /* See breakpoint.h. */
1165
1166 bool
1167 is_tracepoint (const struct breakpoint *b)
1168 {
1169 return is_tracepoint_type (b->type);
1170 }
1171
1172 /* Factory function to create an appropriate instance of breakpoint given
1173 TYPE. */
1174
1175 static std::unique_ptr<breakpoint>
1176 new_breakpoint_from_type (bptype type)
1177 {
1178 breakpoint *b;
1179
1180 if (is_tracepoint_type (type))
1181 b = new tracepoint ();
1182 else if (is_longjmp_type (type))
1183 b = new longjmp_breakpoint ();
1184 else
1185 b = new breakpoint ();
1186
1187 return std::unique_ptr<breakpoint> (b);
1188 }
1189
1190 /* A helper function that validates that COMMANDS are valid for a
1191 breakpoint. This function will throw an exception if a problem is
1192 found. */
1193
1194 static void
1195 validate_commands_for_breakpoint (struct breakpoint *b,
1196 struct command_line *commands)
1197 {
1198 if (is_tracepoint (b))
1199 {
1200 struct tracepoint *t = (struct tracepoint *) b;
1201 struct command_line *c;
1202 struct command_line *while_stepping = 0;
1203
1204 /* Reset the while-stepping step count. The previous commands
1205 might have included a while-stepping action, while the new
1206 ones might not. */
1207 t->step_count = 0;
1208
1209 /* We need to verify that each top-level element of commands is
1210 valid for tracepoints, that there's at most one
1211 while-stepping element, and that the while-stepping's body
1212 has valid tracing commands excluding nested while-stepping.
1213 We also need to validate the tracepoint action line in the
1214 context of the tracepoint --- validate_actionline actually
1215 has side effects, like setting the tracepoint's
1216 while-stepping STEP_COUNT, in addition to checking if the
1217 collect/teval actions parse and make sense in the
1218 tracepoint's context. */
1219 for (c = commands; c; c = c->next)
1220 {
1221 if (c->control_type == while_stepping_control)
1222 {
1223 if (b->type == bp_fast_tracepoint)
1224 error (_("The 'while-stepping' command "
1225 "cannot be used for fast tracepoint"));
1226 else if (b->type == bp_static_tracepoint)
1227 error (_("The 'while-stepping' command "
1228 "cannot be used for static tracepoint"));
1229
1230 if (while_stepping)
1231 error (_("The 'while-stepping' command "
1232 "can be used only once"));
1233 else
1234 while_stepping = c;
1235 }
1236
1237 validate_actionline (c->line, b);
1238 }
1239 if (while_stepping)
1240 {
1241 struct command_line *c2;
1242
1243 gdb_assert (while_stepping->body_list_1 == nullptr);
1244 c2 = while_stepping->body_list_0.get ();
1245 for (; c2; c2 = c2->next)
1246 {
1247 if (c2->control_type == while_stepping_control)
1248 error (_("The 'while-stepping' command cannot be nested"));
1249 }
1250 }
1251 }
1252 else
1253 {
1254 check_no_tracepoint_commands (commands);
1255 }
1256 }
1257
1258 /* Return a vector of all the static tracepoints set at ADDR. The
1259 caller is responsible for releasing the vector. */
1260
1261 std::vector<breakpoint *>
1262 static_tracepoints_here (CORE_ADDR addr)
1263 {
1264 struct breakpoint *b;
1265 std::vector<breakpoint *> found;
1266 struct bp_location *loc;
1267
1268 ALL_BREAKPOINTS (b)
1269 if (b->type == bp_static_tracepoint)
1270 {
1271 for (loc = b->loc; loc; loc = loc->next)
1272 if (loc->address == addr)
1273 found.push_back (b);
1274 }
1275
1276 return found;
1277 }
1278
1279 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1280 validate that only allowed commands are included. */
1281
1282 void
1283 breakpoint_set_commands (struct breakpoint *b,
1284 counted_command_line &&commands)
1285 {
1286 validate_commands_for_breakpoint (b, commands.get ());
1287
1288 b->commands = std::move (commands);
1289 gdb::observers::breakpoint_modified.notify (b);
1290 }
1291
1292 /* Set the internal `silent' flag on the breakpoint. Note that this
1293 is not the same as the "silent" that may appear in the breakpoint's
1294 commands. */
1295
1296 void
1297 breakpoint_set_silent (struct breakpoint *b, int silent)
1298 {
1299 int old_silent = b->silent;
1300
1301 b->silent = silent;
1302 if (old_silent != silent)
1303 gdb::observers::breakpoint_modified.notify (b);
1304 }
1305
1306 /* Set the thread for this breakpoint. If THREAD is -1, make the
1307 breakpoint work for any thread. */
1308
1309 void
1310 breakpoint_set_thread (struct breakpoint *b, int thread)
1311 {
1312 int old_thread = b->thread;
1313
1314 b->thread = thread;
1315 if (old_thread != thread)
1316 gdb::observers::breakpoint_modified.notify (b);
1317 }
1318
1319 /* Set the task for this breakpoint. If TASK is 0, make the
1320 breakpoint work for any task. */
1321
1322 void
1323 breakpoint_set_task (struct breakpoint *b, int task)
1324 {
1325 int old_task = b->task;
1326
1327 b->task = task;
1328 if (old_task != task)
1329 gdb::observers::breakpoint_modified.notify (b);
1330 }
1331
1332 static void
1333 commands_command_1 (const char *arg, int from_tty,
1334 struct command_line *control)
1335 {
1336 counted_command_line cmd;
1337 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1338 NULL after the call to read_command_lines if the user provides an empty
1339 list of command by just typing "end". */
1340 bool cmd_read = false;
1341
1342 std::string new_arg;
1343
1344 if (arg == NULL || !*arg)
1345 {
1346 /* Argument not explicitly given. Synthesize it. */
1347 if (breakpoint_count - prev_breakpoint_count > 1)
1348 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1349 breakpoint_count);
1350 else if (breakpoint_count > 0)
1351 new_arg = string_printf ("%d", breakpoint_count);
1352 }
1353 else
1354 {
1355 /* Create a copy of ARG. This is needed because the "commands"
1356 command may be coming from a script. In that case, the read
1357 line buffer is going to be overwritten in the lambda of
1358 'map_breakpoint_numbers' below when reading the next line
1359 before we are are done parsing the breakpoint numbers. */
1360 new_arg = arg;
1361 }
1362 arg = new_arg.c_str ();
1363
1364 map_breakpoint_numbers
1365 (arg, [&] (breakpoint *b)
1366 {
1367 if (!cmd_read)
1368 {
1369 gdb_assert (cmd == NULL);
1370 if (control != NULL)
1371 cmd = control->body_list_0;
1372 else
1373 {
1374 std::string str
1375 = string_printf (_("Type commands for breakpoint(s) "
1376 "%s, one per line."),
1377 arg);
1378
1379 auto do_validate = [=] (const char *line)
1380 {
1381 validate_actionline (line, b);
1382 };
1383 gdb::function_view<void (const char *)> validator;
1384 if (is_tracepoint (b))
1385 validator = do_validate;
1386
1387 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1388 }
1389 cmd_read = true;
1390 }
1391
1392 /* If a breakpoint was on the list more than once, we don't need to
1393 do anything. */
1394 if (b->commands != cmd)
1395 {
1396 validate_commands_for_breakpoint (b, cmd.get ());
1397 b->commands = cmd;
1398 gdb::observers::breakpoint_modified.notify (b);
1399 }
1400 });
1401 }
1402
1403 static void
1404 commands_command (const char *arg, int from_tty)
1405 {
1406 commands_command_1 (arg, from_tty, NULL);
1407 }
1408
1409 /* Like commands_command, but instead of reading the commands from
1410 input stream, takes them from an already parsed command structure.
1411
1412 This is used by cli-script.c to DTRT with breakpoint commands
1413 that are part of if and while bodies. */
1414 enum command_control_type
1415 commands_from_control_command (const char *arg, struct command_line *cmd)
1416 {
1417 commands_command_1 (arg, 0, cmd);
1418 return simple_control;
1419 }
1420
1421 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1422
1423 static int
1424 bp_location_has_shadow (struct bp_location *bl)
1425 {
1426 if (bl->loc_type != bp_loc_software_breakpoint)
1427 return 0;
1428 if (!bl->inserted)
1429 return 0;
1430 if (bl->target_info.shadow_len == 0)
1431 /* BL isn't valid, or doesn't shadow memory. */
1432 return 0;
1433 return 1;
1434 }
1435
1436 /* Update BUF, which is LEN bytes read from the target address
1437 MEMADDR, by replacing a memory breakpoint with its shadowed
1438 contents.
1439
1440 If READBUF is not NULL, this buffer must not overlap with the of
1441 the breakpoint location's shadow_contents buffer. Otherwise, a
1442 failed assertion internal error will be raised. */
1443
1444 static void
1445 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1446 const gdb_byte *writebuf_org,
1447 ULONGEST memaddr, LONGEST len,
1448 struct bp_target_info *target_info,
1449 struct gdbarch *gdbarch)
1450 {
1451 /* Now do full processing of the found relevant range of elements. */
1452 CORE_ADDR bp_addr = 0;
1453 int bp_size = 0;
1454 int bptoffset = 0;
1455
1456 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1457 current_program_space->aspace, 0))
1458 {
1459 /* The breakpoint is inserted in a different address space. */
1460 return;
1461 }
1462
1463 /* Addresses and length of the part of the breakpoint that
1464 we need to copy. */
1465 bp_addr = target_info->placed_address;
1466 bp_size = target_info->shadow_len;
1467
1468 if (bp_addr + bp_size <= memaddr)
1469 {
1470 /* The breakpoint is entirely before the chunk of memory we are
1471 reading. */
1472 return;
1473 }
1474
1475 if (bp_addr >= memaddr + len)
1476 {
1477 /* The breakpoint is entirely after the chunk of memory we are
1478 reading. */
1479 return;
1480 }
1481
1482 /* Offset within shadow_contents. */
1483 if (bp_addr < memaddr)
1484 {
1485 /* Only copy the second part of the breakpoint. */
1486 bp_size -= memaddr - bp_addr;
1487 bptoffset = memaddr - bp_addr;
1488 bp_addr = memaddr;
1489 }
1490
1491 if (bp_addr + bp_size > memaddr + len)
1492 {
1493 /* Only copy the first part of the breakpoint. */
1494 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1495 }
1496
1497 if (readbuf != NULL)
1498 {
1499 /* Verify that the readbuf buffer does not overlap with the
1500 shadow_contents buffer. */
1501 gdb_assert (target_info->shadow_contents >= readbuf + len
1502 || readbuf >= (target_info->shadow_contents
1503 + target_info->shadow_len));
1504
1505 /* Update the read buffer with this inserted breakpoint's
1506 shadow. */
1507 memcpy (readbuf + bp_addr - memaddr,
1508 target_info->shadow_contents + bptoffset, bp_size);
1509 }
1510 else
1511 {
1512 const unsigned char *bp;
1513 CORE_ADDR addr = target_info->reqstd_address;
1514 int placed_size;
1515
1516 /* Update the shadow with what we want to write to memory. */
1517 memcpy (target_info->shadow_contents + bptoffset,
1518 writebuf_org + bp_addr - memaddr, bp_size);
1519
1520 /* Determine appropriate breakpoint contents and size for this
1521 address. */
1522 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1523
1524 /* Update the final write buffer with this inserted
1525 breakpoint's INSN. */
1526 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1527 }
1528 }
1529
1530 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1531 by replacing any memory breakpoints with their shadowed contents.
1532
1533 If READBUF is not NULL, this buffer must not overlap with any of
1534 the breakpoint location's shadow_contents buffers. Otherwise,
1535 a failed assertion internal error will be raised.
1536
1537 The range of shadowed area by each bp_location is:
1538 bl->address - bp_locations_placed_address_before_address_max
1539 up to bl->address + bp_locations_shadow_len_after_address_max
1540 The range we were requested to resolve shadows for is:
1541 memaddr ... memaddr + len
1542 Thus the safe cutoff boundaries for performance optimization are
1543 memaddr + len <= (bl->address
1544 - bp_locations_placed_address_before_address_max)
1545 and:
1546 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1547
1548 void
1549 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1550 const gdb_byte *writebuf_org,
1551 ULONGEST memaddr, LONGEST len)
1552 {
1553 /* Left boundary, right boundary and median element of our binary
1554 search. */
1555 unsigned bc_l, bc_r, bc;
1556
1557 /* Find BC_L which is a leftmost element which may affect BUF
1558 content. It is safe to report lower value but a failure to
1559 report higher one. */
1560
1561 bc_l = 0;
1562 bc_r = bp_locations_count;
1563 while (bc_l + 1 < bc_r)
1564 {
1565 struct bp_location *bl;
1566
1567 bc = (bc_l + bc_r) / 2;
1568 bl = bp_locations[bc];
1569
1570 /* Check first BL->ADDRESS will not overflow due to the added
1571 constant. Then advance the left boundary only if we are sure
1572 the BC element can in no way affect the BUF content (MEMADDR
1573 to MEMADDR + LEN range).
1574
1575 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1576 offset so that we cannot miss a breakpoint with its shadow
1577 range tail still reaching MEMADDR. */
1578
1579 if ((bl->address + bp_locations_shadow_len_after_address_max
1580 >= bl->address)
1581 && (bl->address + bp_locations_shadow_len_after_address_max
1582 <= memaddr))
1583 bc_l = bc;
1584 else
1585 bc_r = bc;
1586 }
1587
1588 /* Due to the binary search above, we need to make sure we pick the
1589 first location that's at BC_L's address. E.g., if there are
1590 multiple locations at the same address, BC_L may end up pointing
1591 at a duplicate location, and miss the "master"/"inserted"
1592 location. Say, given locations L1, L2 and L3 at addresses A and
1593 B:
1594
1595 L1@A, L2@A, L3@B, ...
1596
1597 BC_L could end up pointing at location L2, while the "master"
1598 location could be L1. Since the `loc->inserted' flag is only set
1599 on "master" locations, we'd forget to restore the shadow of L1
1600 and L2. */
1601 while (bc_l > 0
1602 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1603 bc_l--;
1604
1605 /* Now do full processing of the found relevant range of elements. */
1606
1607 for (bc = bc_l; bc < bp_locations_count; bc++)
1608 {
1609 struct bp_location *bl = bp_locations[bc];
1610
1611 /* bp_location array has BL->OWNER always non-NULL. */
1612 if (bl->owner->type == bp_none)
1613 warning (_("reading through apparently deleted breakpoint #%d?"),
1614 bl->owner->number);
1615
1616 /* Performance optimization: any further element can no longer affect BUF
1617 content. */
1618
1619 if (bl->address >= bp_locations_placed_address_before_address_max
1620 && memaddr + len <= (bl->address
1621 - bp_locations_placed_address_before_address_max))
1622 break;
1623
1624 if (!bp_location_has_shadow (bl))
1625 continue;
1626
1627 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1628 memaddr, len, &bl->target_info, bl->gdbarch);
1629 }
1630 }
1631
1632 /* See breakpoint.h. */
1633
1634 bool
1635 is_breakpoint (const struct breakpoint *bpt)
1636 {
1637 return (bpt->type == bp_breakpoint
1638 || bpt->type == bp_hardware_breakpoint
1639 || bpt->type == bp_dprintf);
1640 }
1641
1642 /* Return true if BPT is of any hardware watchpoint kind. */
1643
1644 static bool
1645 is_hardware_watchpoint (const struct breakpoint *bpt)
1646 {
1647 return (bpt->type == bp_hardware_watchpoint
1648 || bpt->type == bp_read_watchpoint
1649 || bpt->type == bp_access_watchpoint);
1650 }
1651
1652 /* See breakpoint.h. */
1653
1654 bool
1655 is_watchpoint (const struct breakpoint *bpt)
1656 {
1657 return (is_hardware_watchpoint (bpt)
1658 || bpt->type == bp_watchpoint);
1659 }
1660
1661 /* Returns true if the current thread and its running state are safe
1662 to evaluate or update watchpoint B. Watchpoints on local
1663 expressions need to be evaluated in the context of the thread that
1664 was current when the watchpoint was created, and, that thread needs
1665 to be stopped to be able to select the correct frame context.
1666 Watchpoints on global expressions can be evaluated on any thread,
1667 and in any state. It is presently left to the target allowing
1668 memory accesses when threads are running. */
1669
1670 static int
1671 watchpoint_in_thread_scope (struct watchpoint *b)
1672 {
1673 return (b->pspace == current_program_space
1674 && (b->watchpoint_thread == null_ptid
1675 || (inferior_ptid == b->watchpoint_thread
1676 && !inferior_thread ()->executing)));
1677 }
1678
1679 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1680 associated bp_watchpoint_scope breakpoint. */
1681
1682 static void
1683 watchpoint_del_at_next_stop (struct watchpoint *w)
1684 {
1685 if (w->related_breakpoint != w)
1686 {
1687 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1688 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1689 w->related_breakpoint->disposition = disp_del_at_next_stop;
1690 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1691 w->related_breakpoint = w;
1692 }
1693 w->disposition = disp_del_at_next_stop;
1694 }
1695
1696 /* Extract a bitfield value from value VAL using the bit parameters contained in
1697 watchpoint W. */
1698
1699 static struct value *
1700 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1701 {
1702 struct value *bit_val;
1703
1704 if (val == NULL)
1705 return NULL;
1706
1707 bit_val = allocate_value (value_type (val));
1708
1709 unpack_value_bitfield (bit_val,
1710 w->val_bitpos,
1711 w->val_bitsize,
1712 value_contents_for_printing (val),
1713 value_offset (val),
1714 val);
1715
1716 return bit_val;
1717 }
1718
1719 /* Allocate a dummy location and add it to B, which must be a software
1720 watchpoint. This is required because even if a software watchpoint
1721 is not watching any memory, bpstat_stop_status requires a location
1722 to be able to report stops. */
1723
1724 static void
1725 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1726 struct program_space *pspace)
1727 {
1728 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1729
1730 b->loc = allocate_bp_location (b);
1731 b->loc->pspace = pspace;
1732 b->loc->address = -1;
1733 b->loc->length = -1;
1734 }
1735
1736 /* Returns true if B is a software watchpoint that is not watching any
1737 memory (e.g., "watch $pc"). */
1738
1739 static bool
1740 is_no_memory_software_watchpoint (struct breakpoint *b)
1741 {
1742 return (b->type == bp_watchpoint
1743 && b->loc != NULL
1744 && b->loc->next == NULL
1745 && b->loc->address == -1
1746 && b->loc->length == -1);
1747 }
1748
1749 /* Assuming that B is a watchpoint:
1750 - Reparse watchpoint expression, if REPARSE is non-zero
1751 - Evaluate expression and store the result in B->val
1752 - Evaluate the condition if there is one, and store the result
1753 in b->loc->cond.
1754 - Update the list of values that must be watched in B->loc.
1755
1756 If the watchpoint disposition is disp_del_at_next_stop, then do
1757 nothing. If this is local watchpoint that is out of scope, delete
1758 it.
1759
1760 Even with `set breakpoint always-inserted on' the watchpoints are
1761 removed + inserted on each stop here. Normal breakpoints must
1762 never be removed because they might be missed by a running thread
1763 when debugging in non-stop mode. On the other hand, hardware
1764 watchpoints (is_hardware_watchpoint; processed here) are specific
1765 to each LWP since they are stored in each LWP's hardware debug
1766 registers. Therefore, such LWP must be stopped first in order to
1767 be able to modify its hardware watchpoints.
1768
1769 Hardware watchpoints must be reset exactly once after being
1770 presented to the user. It cannot be done sooner, because it would
1771 reset the data used to present the watchpoint hit to the user. And
1772 it must not be done later because it could display the same single
1773 watchpoint hit during multiple GDB stops. Note that the latter is
1774 relevant only to the hardware watchpoint types bp_read_watchpoint
1775 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1776 not user-visible - its hit is suppressed if the memory content has
1777 not changed.
1778
1779 The following constraints influence the location where we can reset
1780 hardware watchpoints:
1781
1782 * target_stopped_by_watchpoint and target_stopped_data_address are
1783 called several times when GDB stops.
1784
1785 [linux]
1786 * Multiple hardware watchpoints can be hit at the same time,
1787 causing GDB to stop. GDB only presents one hardware watchpoint
1788 hit at a time as the reason for stopping, and all the other hits
1789 are presented later, one after the other, each time the user
1790 requests the execution to be resumed. Execution is not resumed
1791 for the threads still having pending hit event stored in
1792 LWP_INFO->STATUS. While the watchpoint is already removed from
1793 the inferior on the first stop the thread hit event is kept being
1794 reported from its cached value by linux_nat_stopped_data_address
1795 until the real thread resume happens after the watchpoint gets
1796 presented and thus its LWP_INFO->STATUS gets reset.
1797
1798 Therefore the hardware watchpoint hit can get safely reset on the
1799 watchpoint removal from inferior. */
1800
1801 static void
1802 update_watchpoint (struct watchpoint *b, int reparse)
1803 {
1804 int within_current_scope;
1805 struct frame_id saved_frame_id;
1806 int frame_saved;
1807
1808 /* If this is a local watchpoint, we only want to check if the
1809 watchpoint frame is in scope if the current thread is the thread
1810 that was used to create the watchpoint. */
1811 if (!watchpoint_in_thread_scope (b))
1812 return;
1813
1814 if (b->disposition == disp_del_at_next_stop)
1815 return;
1816
1817 frame_saved = 0;
1818
1819 /* Determine if the watchpoint is within scope. */
1820 if (b->exp_valid_block == NULL)
1821 within_current_scope = 1;
1822 else
1823 {
1824 struct frame_info *fi = get_current_frame ();
1825 struct gdbarch *frame_arch = get_frame_arch (fi);
1826 CORE_ADDR frame_pc = get_frame_pc (fi);
1827
1828 /* If we're at a point where the stack has been destroyed
1829 (e.g. in a function epilogue), unwinding may not work
1830 properly. Do not attempt to recreate locations at this
1831 point. See similar comments in watchpoint_check. */
1832 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1833 return;
1834
1835 /* Save the current frame's ID so we can restore it after
1836 evaluating the watchpoint expression on its own frame. */
1837 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1838 took a frame parameter, so that we didn't have to change the
1839 selected frame. */
1840 frame_saved = 1;
1841 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1842
1843 fi = frame_find_by_id (b->watchpoint_frame);
1844 within_current_scope = (fi != NULL);
1845 if (within_current_scope)
1846 select_frame (fi);
1847 }
1848
1849 /* We don't free locations. They are stored in the bp_location array
1850 and update_global_location_list will eventually delete them and
1851 remove breakpoints if needed. */
1852 b->loc = NULL;
1853
1854 if (within_current_scope && reparse)
1855 {
1856 const char *s;
1857
1858 b->exp.reset ();
1859 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1860 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1861 /* If the meaning of expression itself changed, the old value is
1862 no longer relevant. We don't want to report a watchpoint hit
1863 to the user when the old value and the new value may actually
1864 be completely different objects. */
1865 b->val = NULL;
1866 b->val_valid = false;
1867
1868 /* Note that unlike with breakpoints, the watchpoint's condition
1869 expression is stored in the breakpoint object, not in the
1870 locations (re)created below. */
1871 if (b->cond_string != NULL)
1872 {
1873 b->cond_exp.reset ();
1874
1875 s = b->cond_string;
1876 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1877 }
1878 }
1879
1880 /* If we failed to parse the expression, for example because
1881 it refers to a global variable in a not-yet-loaded shared library,
1882 don't try to insert watchpoint. We don't automatically delete
1883 such watchpoint, though, since failure to parse expression
1884 is different from out-of-scope watchpoint. */
1885 if (!target_has_execution ())
1886 {
1887 /* Without execution, memory can't change. No use to try and
1888 set watchpoint locations. The watchpoint will be reset when
1889 the target gains execution, through breakpoint_re_set. */
1890 if (!can_use_hw_watchpoints)
1891 {
1892 if (b->ops->works_in_software_mode (b))
1893 b->type = bp_watchpoint;
1894 else
1895 error (_("Can't set read/access watchpoint when "
1896 "hardware watchpoints are disabled."));
1897 }
1898 }
1899 else if (within_current_scope && b->exp)
1900 {
1901 int pc = 0;
1902 std::vector<value_ref_ptr> val_chain;
1903 struct value *v, *result;
1904 struct program_space *frame_pspace;
1905
1906 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, false);
1907
1908 /* Avoid setting b->val if it's already set. The meaning of
1909 b->val is 'the last value' user saw, and we should update
1910 it only if we reported that last value to user. As it
1911 happens, the code that reports it updates b->val directly.
1912 We don't keep track of the memory value for masked
1913 watchpoints. */
1914 if (!b->val_valid && !is_masked_watchpoint (b))
1915 {
1916 if (b->val_bitsize != 0)
1917 v = extract_bitfield_from_watchpoint_value (b, v);
1918 b->val = release_value (v);
1919 b->val_valid = true;
1920 }
1921
1922 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1923
1924 /* Look at each value on the value chain. */
1925 gdb_assert (!val_chain.empty ());
1926 for (const value_ref_ptr &iter : val_chain)
1927 {
1928 v = iter.get ();
1929
1930 /* If it's a memory location, and GDB actually needed
1931 its contents to evaluate the expression, then we
1932 must watch it. If the first value returned is
1933 still lazy, that means an error occurred reading it;
1934 watch it anyway in case it becomes readable. */
1935 if (VALUE_LVAL (v) == lval_memory
1936 && (v == val_chain[0] || ! value_lazy (v)))
1937 {
1938 struct type *vtype = check_typedef (value_type (v));
1939
1940 /* We only watch structs and arrays if user asked
1941 for it explicitly, never if they just happen to
1942 appear in the middle of some value chain. */
1943 if (v == result
1944 || (vtype->code () != TYPE_CODE_STRUCT
1945 && vtype->code () != TYPE_CODE_ARRAY))
1946 {
1947 CORE_ADDR addr;
1948 enum target_hw_bp_type type;
1949 struct bp_location *loc, **tmp;
1950 int bitpos = 0, bitsize = 0;
1951
1952 if (value_bitsize (v) != 0)
1953 {
1954 /* Extract the bit parameters out from the bitfield
1955 sub-expression. */
1956 bitpos = value_bitpos (v);
1957 bitsize = value_bitsize (v);
1958 }
1959 else if (v == result && b->val_bitsize != 0)
1960 {
1961 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1962 lvalue whose bit parameters are saved in the fields
1963 VAL_BITPOS and VAL_BITSIZE. */
1964 bitpos = b->val_bitpos;
1965 bitsize = b->val_bitsize;
1966 }
1967
1968 addr = value_address (v);
1969 if (bitsize != 0)
1970 {
1971 /* Skip the bytes that don't contain the bitfield. */
1972 addr += bitpos / 8;
1973 }
1974
1975 type = hw_write;
1976 if (b->type == bp_read_watchpoint)
1977 type = hw_read;
1978 else if (b->type == bp_access_watchpoint)
1979 type = hw_access;
1980
1981 loc = allocate_bp_location (b);
1982 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1983 ;
1984 *tmp = loc;
1985 loc->gdbarch = value_type (v)->arch ();
1986
1987 loc->pspace = frame_pspace;
1988 loc->address = address_significant (loc->gdbarch, addr);
1989
1990 if (bitsize != 0)
1991 {
1992 /* Just cover the bytes that make up the bitfield. */
1993 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1994 }
1995 else
1996 loc->length = TYPE_LENGTH (value_type (v));
1997
1998 loc->watchpoint_type = type;
1999 }
2000 }
2001 }
2002
2003 /* Change the type of breakpoint between hardware assisted or
2004 an ordinary watchpoint depending on the hardware support
2005 and free hardware slots. REPARSE is set when the inferior
2006 is started. */
2007 if (reparse)
2008 {
2009 int reg_cnt;
2010 enum bp_loc_type loc_type;
2011 struct bp_location *bl;
2012
2013 reg_cnt = can_use_hardware_watchpoint (val_chain);
2014
2015 if (reg_cnt)
2016 {
2017 int i, target_resources_ok, other_type_used;
2018 enum bptype type;
2019
2020 /* Use an exact watchpoint when there's only one memory region to be
2021 watched, and only one debug register is needed to watch it. */
2022 b->exact = target_exact_watchpoints && reg_cnt == 1;
2023
2024 /* We need to determine how many resources are already
2025 used for all other hardware watchpoints plus this one
2026 to see if we still have enough resources to also fit
2027 this watchpoint in as well. */
2028
2029 /* If this is a software watchpoint, we try to turn it
2030 to a hardware one -- count resources as if B was of
2031 hardware watchpoint type. */
2032 type = b->type;
2033 if (type == bp_watchpoint)
2034 type = bp_hardware_watchpoint;
2035
2036 /* This watchpoint may or may not have been placed on
2037 the list yet at this point (it won't be in the list
2038 if we're trying to create it for the first time,
2039 through watch_command), so always account for it
2040 manually. */
2041
2042 /* Count resources used by all watchpoints except B. */
2043 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
2044
2045 /* Add in the resources needed for B. */
2046 i += hw_watchpoint_use_count (b);
2047
2048 target_resources_ok
2049 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2050 if (target_resources_ok <= 0)
2051 {
2052 int sw_mode = b->ops->works_in_software_mode (b);
2053
2054 if (target_resources_ok == 0 && !sw_mode)
2055 error (_("Target does not support this type of "
2056 "hardware watchpoint."));
2057 else if (target_resources_ok < 0 && !sw_mode)
2058 error (_("There are not enough available hardware "
2059 "resources for this watchpoint."));
2060
2061 /* Downgrade to software watchpoint. */
2062 b->type = bp_watchpoint;
2063 }
2064 else
2065 {
2066 /* If this was a software watchpoint, we've just
2067 found we have enough resources to turn it to a
2068 hardware watchpoint. Otherwise, this is a
2069 nop. */
2070 b->type = type;
2071 }
2072 }
2073 else if (!b->ops->works_in_software_mode (b))
2074 {
2075 if (!can_use_hw_watchpoints)
2076 error (_("Can't set read/access watchpoint when "
2077 "hardware watchpoints are disabled."));
2078 else
2079 error (_("Expression cannot be implemented with "
2080 "read/access watchpoint."));
2081 }
2082 else
2083 b->type = bp_watchpoint;
2084
2085 loc_type = (b->type == bp_watchpoint? bp_loc_other
2086 : bp_loc_hardware_watchpoint);
2087 for (bl = b->loc; bl; bl = bl->next)
2088 bl->loc_type = loc_type;
2089 }
2090
2091 /* If a software watchpoint is not watching any memory, then the
2092 above left it without any location set up. But,
2093 bpstat_stop_status requires a location to be able to report
2094 stops, so make sure there's at least a dummy one. */
2095 if (b->type == bp_watchpoint && b->loc == NULL)
2096 software_watchpoint_add_no_memory_location (b, frame_pspace);
2097 }
2098 else if (!within_current_scope)
2099 {
2100 printf_filtered (_("\
2101 Watchpoint %d deleted because the program has left the block\n\
2102 in which its expression is valid.\n"),
2103 b->number);
2104 watchpoint_del_at_next_stop (b);
2105 }
2106
2107 /* Restore the selected frame. */
2108 if (frame_saved)
2109 select_frame (frame_find_by_id (saved_frame_id));
2110 }
2111
2112
2113 /* Returns 1 iff breakpoint location should be
2114 inserted in the inferior. We don't differentiate the type of BL's owner
2115 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2116 breakpoint_ops is not defined, because in insert_bp_location,
2117 tracepoint's insert_location will not be called. */
2118 static int
2119 should_be_inserted (struct bp_location *bl)
2120 {
2121 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2122 return 0;
2123
2124 if (bl->owner->disposition == disp_del_at_next_stop)
2125 return 0;
2126
2127 if (!bl->enabled || bl->disabled_by_cond
2128 || bl->shlib_disabled || bl->duplicate)
2129 return 0;
2130
2131 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2132 return 0;
2133
2134 /* This is set for example, when we're attached to the parent of a
2135 vfork, and have detached from the child. The child is running
2136 free, and we expect it to do an exec or exit, at which point the
2137 OS makes the parent schedulable again (and the target reports
2138 that the vfork is done). Until the child is done with the shared
2139 memory region, do not insert breakpoints in the parent, otherwise
2140 the child could still trip on the parent's breakpoints. Since
2141 the parent is blocked anyway, it won't miss any breakpoint. */
2142 if (bl->pspace->breakpoints_not_allowed)
2143 return 0;
2144
2145 /* Don't insert a breakpoint if we're trying to step past its
2146 location, except if the breakpoint is a single-step breakpoint,
2147 and the breakpoint's thread is the thread which is stepping past
2148 a breakpoint. */
2149 if ((bl->loc_type == bp_loc_software_breakpoint
2150 || bl->loc_type == bp_loc_hardware_breakpoint)
2151 && stepping_past_instruction_at (bl->pspace->aspace,
2152 bl->address)
2153 /* The single-step breakpoint may be inserted at the location
2154 we're trying to step if the instruction branches to itself.
2155 However, the instruction won't be executed at all and it may
2156 break the semantics of the instruction, for example, the
2157 instruction is a conditional branch or updates some flags.
2158 We can't fix it unless GDB is able to emulate the instruction
2159 or switch to displaced stepping. */
2160 && !(bl->owner->type == bp_single_step
2161 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2162 {
2163 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2164 paddress (bl->gdbarch, bl->address));
2165 return 0;
2166 }
2167
2168 /* Don't insert watchpoints if we're trying to step past the
2169 instruction that triggered one. */
2170 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2171 && stepping_past_nonsteppable_watchpoint ())
2172 {
2173 infrun_debug_printf ("stepping past non-steppable watchpoint. "
2174 "skipping watchpoint at %s:%d",
2175 paddress (bl->gdbarch, bl->address), bl->length);
2176 return 0;
2177 }
2178
2179 return 1;
2180 }
2181
2182 /* Same as should_be_inserted but does the check assuming
2183 that the location is not duplicated. */
2184
2185 static int
2186 unduplicated_should_be_inserted (struct bp_location *bl)
2187 {
2188 int result;
2189 const int save_duplicate = bl->duplicate;
2190
2191 bl->duplicate = 0;
2192 result = should_be_inserted (bl);
2193 bl->duplicate = save_duplicate;
2194 return result;
2195 }
2196
2197 /* Parses a conditional described by an expression COND into an
2198 agent expression bytecode suitable for evaluation
2199 by the bytecode interpreter. Return NULL if there was
2200 any error during parsing. */
2201
2202 static agent_expr_up
2203 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2204 {
2205 if (cond == NULL)
2206 return NULL;
2207
2208 agent_expr_up aexpr;
2209
2210 /* We don't want to stop processing, so catch any errors
2211 that may show up. */
2212 try
2213 {
2214 aexpr = gen_eval_for_expr (scope, cond);
2215 }
2216
2217 catch (const gdb_exception_error &ex)
2218 {
2219 /* If we got here, it means the condition could not be parsed to a valid
2220 bytecode expression and thus can't be evaluated on the target's side.
2221 It's no use iterating through the conditions. */
2222 }
2223
2224 /* We have a valid agent expression. */
2225 return aexpr;
2226 }
2227
2228 /* Based on location BL, create a list of breakpoint conditions to be
2229 passed on to the target. If we have duplicated locations with different
2230 conditions, we will add such conditions to the list. The idea is that the
2231 target will evaluate the list of conditions and will only notify GDB when
2232 one of them is true. */
2233
2234 static void
2235 build_target_condition_list (struct bp_location *bl)
2236 {
2237 struct bp_location **locp = NULL, **loc2p;
2238 int null_condition_or_parse_error = 0;
2239 int modified = bl->needs_update;
2240 struct bp_location *loc;
2241
2242 /* Release conditions left over from a previous insert. */
2243 bl->target_info.conditions.clear ();
2244
2245 /* This is only meaningful if the target is
2246 evaluating conditions and if the user has
2247 opted for condition evaluation on the target's
2248 side. */
2249 if (gdb_evaluates_breakpoint_condition_p ()
2250 || !target_supports_evaluation_of_breakpoint_conditions ())
2251 return;
2252
2253 /* Do a first pass to check for locations with no assigned
2254 conditions or conditions that fail to parse to a valid agent
2255 expression bytecode. If any of these happen, then it's no use to
2256 send conditions to the target since this location will always
2257 trigger and generate a response back to GDB. Note we consider
2258 all locations at the same address irrespective of type, i.e.,
2259 even if the locations aren't considered duplicates (e.g.,
2260 software breakpoint and hardware breakpoint at the same
2261 address). */
2262 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2263 {
2264 loc = (*loc2p);
2265 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2266 {
2267 if (modified)
2268 {
2269 /* Re-parse the conditions since something changed. In that
2270 case we already freed the condition bytecodes (see
2271 force_breakpoint_reinsertion). We just
2272 need to parse the condition to bytecodes again. */
2273 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2274 loc->cond.get ());
2275 }
2276
2277 /* If we have a NULL bytecode expression, it means something
2278 went wrong or we have a null condition expression. */
2279 if (!loc->cond_bytecode)
2280 {
2281 null_condition_or_parse_error = 1;
2282 break;
2283 }
2284 }
2285 }
2286
2287 /* If any of these happened, it means we will have to evaluate the conditions
2288 for the location's address on gdb's side. It is no use keeping bytecodes
2289 for all the other duplicate locations, thus we free all of them here.
2290
2291 This is so we have a finer control over which locations' conditions are
2292 being evaluated by GDB or the remote stub. */
2293 if (null_condition_or_parse_error)
2294 {
2295 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2296 {
2297 loc = (*loc2p);
2298 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2299 {
2300 /* Only go as far as the first NULL bytecode is
2301 located. */
2302 if (!loc->cond_bytecode)
2303 return;
2304
2305 loc->cond_bytecode.reset ();
2306 }
2307 }
2308 }
2309
2310 /* No NULL conditions or failed bytecode generation. Build a
2311 condition list for this location's address. If we have software
2312 and hardware locations at the same address, they aren't
2313 considered duplicates, but we still marge all the conditions
2314 anyway, as it's simpler, and doesn't really make a practical
2315 difference. */
2316 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2317 {
2318 loc = (*loc2p);
2319 if (loc->cond
2320 && is_breakpoint (loc->owner)
2321 && loc->pspace->num == bl->pspace->num
2322 && loc->owner->enable_state == bp_enabled
2323 && loc->enabled
2324 && !loc->disabled_by_cond)
2325 {
2326 /* Add the condition to the vector. This will be used later
2327 to send the conditions to the target. */
2328 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2329 }
2330 }
2331
2332 return;
2333 }
2334
2335 /* Parses a command described by string CMD into an agent expression
2336 bytecode suitable for evaluation by the bytecode interpreter.
2337 Return NULL if there was any error during parsing. */
2338
2339 static agent_expr_up
2340 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2341 {
2342 const char *cmdrest;
2343 const char *format_start, *format_end;
2344 struct gdbarch *gdbarch = get_current_arch ();
2345
2346 if (cmd == NULL)
2347 return NULL;
2348
2349 cmdrest = cmd;
2350
2351 if (*cmdrest == ',')
2352 ++cmdrest;
2353 cmdrest = skip_spaces (cmdrest);
2354
2355 if (*cmdrest++ != '"')
2356 error (_("No format string following the location"));
2357
2358 format_start = cmdrest;
2359
2360 format_pieces fpieces (&cmdrest);
2361
2362 format_end = cmdrest;
2363
2364 if (*cmdrest++ != '"')
2365 error (_("Bad format string, non-terminated '\"'."));
2366
2367 cmdrest = skip_spaces (cmdrest);
2368
2369 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2370 error (_("Invalid argument syntax"));
2371
2372 if (*cmdrest == ',')
2373 cmdrest++;
2374 cmdrest = skip_spaces (cmdrest);
2375
2376 /* For each argument, make an expression. */
2377
2378 std::vector<struct expression *> argvec;
2379 while (*cmdrest != '\0')
2380 {
2381 const char *cmd1;
2382
2383 cmd1 = cmdrest;
2384 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2385 argvec.push_back (expr.release ());
2386 cmdrest = cmd1;
2387 if (*cmdrest == ',')
2388 ++cmdrest;
2389 }
2390
2391 agent_expr_up aexpr;
2392
2393 /* We don't want to stop processing, so catch any errors
2394 that may show up. */
2395 try
2396 {
2397 aexpr = gen_printf (scope, gdbarch, 0, 0,
2398 format_start, format_end - format_start,
2399 argvec.size (), argvec.data ());
2400 }
2401 catch (const gdb_exception_error &ex)
2402 {
2403 /* If we got here, it means the command could not be parsed to a valid
2404 bytecode expression and thus can't be evaluated on the target's side.
2405 It's no use iterating through the other commands. */
2406 }
2407
2408 /* We have a valid agent expression, return it. */
2409 return aexpr;
2410 }
2411
2412 /* Based on location BL, create a list of breakpoint commands to be
2413 passed on to the target. If we have duplicated locations with
2414 different commands, we will add any such to the list. */
2415
2416 static void
2417 build_target_command_list (struct bp_location *bl)
2418 {
2419 struct bp_location **locp = NULL, **loc2p;
2420 int null_command_or_parse_error = 0;
2421 int modified = bl->needs_update;
2422 struct bp_location *loc;
2423
2424 /* Clear commands left over from a previous insert. */
2425 bl->target_info.tcommands.clear ();
2426
2427 if (!target_can_run_breakpoint_commands ())
2428 return;
2429
2430 /* For now, limit to agent-style dprintf breakpoints. */
2431 if (dprintf_style != dprintf_style_agent)
2432 return;
2433
2434 /* For now, if we have any location at the same address that isn't a
2435 dprintf, don't install the target-side commands, as that would
2436 make the breakpoint not be reported to the core, and we'd lose
2437 control. */
2438 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2439 {
2440 loc = (*loc2p);
2441 if (is_breakpoint (loc->owner)
2442 && loc->pspace->num == bl->pspace->num
2443 && loc->owner->type != bp_dprintf)
2444 return;
2445 }
2446
2447 /* Do a first pass to check for locations with no assigned
2448 conditions or conditions that fail to parse to a valid agent expression
2449 bytecode. If any of these happen, then it's no use to send conditions
2450 to the target since this location will always trigger and generate a
2451 response back to GDB. */
2452 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2453 {
2454 loc = (*loc2p);
2455 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2456 {
2457 if (modified)
2458 {
2459 /* Re-parse the commands since something changed. In that
2460 case we already freed the command bytecodes (see
2461 force_breakpoint_reinsertion). We just
2462 need to parse the command to bytecodes again. */
2463 loc->cmd_bytecode
2464 = parse_cmd_to_aexpr (bl->address,
2465 loc->owner->extra_string);
2466 }
2467
2468 /* If we have a NULL bytecode expression, it means something
2469 went wrong or we have a null command expression. */
2470 if (!loc->cmd_bytecode)
2471 {
2472 null_command_or_parse_error = 1;
2473 break;
2474 }
2475 }
2476 }
2477
2478 /* If anything failed, then we're not doing target-side commands,
2479 and so clean up. */
2480 if (null_command_or_parse_error)
2481 {
2482 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2483 {
2484 loc = (*loc2p);
2485 if (is_breakpoint (loc->owner)
2486 && loc->pspace->num == bl->pspace->num)
2487 {
2488 /* Only go as far as the first NULL bytecode is
2489 located. */
2490 if (loc->cmd_bytecode == NULL)
2491 return;
2492
2493 loc->cmd_bytecode.reset ();
2494 }
2495 }
2496 }
2497
2498 /* No NULL commands or failed bytecode generation. Build a command
2499 list for all duplicate locations at this location's address.
2500 Note that here we must care for whether the breakpoint location
2501 types are considered duplicates, otherwise, say, if we have a
2502 software and hardware location at the same address, the target
2503 could end up running the commands twice. For the moment, we only
2504 support targets-side commands with dprintf, but it doesn't hurt
2505 to be pedantically correct in case that changes. */
2506 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2507 {
2508 loc = (*loc2p);
2509 if (breakpoint_locations_match (bl, loc)
2510 && loc->owner->extra_string
2511 && is_breakpoint (loc->owner)
2512 && loc->pspace->num == bl->pspace->num
2513 && loc->owner->enable_state == bp_enabled
2514 && loc->enabled
2515 && !loc->disabled_by_cond)
2516 {
2517 /* Add the command to the vector. This will be used later
2518 to send the commands to the target. */
2519 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2520 }
2521 }
2522
2523 bl->target_info.persist = 0;
2524 /* Maybe flag this location as persistent. */
2525 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2526 bl->target_info.persist = 1;
2527 }
2528
2529 /* Return the kind of breakpoint on address *ADDR. Get the kind
2530 of breakpoint according to ADDR except single-step breakpoint.
2531 Get the kind of single-step breakpoint according to the current
2532 registers state. */
2533
2534 static int
2535 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2536 {
2537 if (bl->owner->type == bp_single_step)
2538 {
2539 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2540 struct regcache *regcache;
2541
2542 regcache = get_thread_regcache (thr);
2543
2544 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2545 regcache, addr);
2546 }
2547 else
2548 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2549 }
2550
2551 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2552 location. Any error messages are printed to TMP_ERROR_STREAM; and
2553 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2554 Returns 0 for success, 1 if the bp_location type is not supported or
2555 -1 for failure.
2556
2557 NOTE drow/2003-09-09: This routine could be broken down to an
2558 object-style method for each breakpoint or catchpoint type. */
2559 static int
2560 insert_bp_location (struct bp_location *bl,
2561 struct ui_file *tmp_error_stream,
2562 int *disabled_breaks,
2563 int *hw_breakpoint_error,
2564 int *hw_bp_error_explained_already)
2565 {
2566 gdb_exception bp_excpt;
2567
2568 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2569 return 0;
2570
2571 /* Note we don't initialize bl->target_info, as that wipes out
2572 the breakpoint location's shadow_contents if the breakpoint
2573 is still inserted at that location. This in turn breaks
2574 target_read_memory which depends on these buffers when
2575 a memory read is requested at the breakpoint location:
2576 Once the target_info has been wiped, we fail to see that
2577 we have a breakpoint inserted at that address and thus
2578 read the breakpoint instead of returning the data saved in
2579 the breakpoint location's shadow contents. */
2580 bl->target_info.reqstd_address = bl->address;
2581 bl->target_info.placed_address_space = bl->pspace->aspace;
2582 bl->target_info.length = bl->length;
2583
2584 /* When working with target-side conditions, we must pass all the conditions
2585 for the same breakpoint address down to the target since GDB will not
2586 insert those locations. With a list of breakpoint conditions, the target
2587 can decide when to stop and notify GDB. */
2588
2589 if (is_breakpoint (bl->owner))
2590 {
2591 build_target_condition_list (bl);
2592 build_target_command_list (bl);
2593 /* Reset the modification marker. */
2594 bl->needs_update = 0;
2595 }
2596
2597 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2598 set at a read-only address, then a breakpoint location will have
2599 been changed to hardware breakpoint before we get here. If it is
2600 "off" however, error out before actually trying to insert the
2601 breakpoint, with a nicer error message. */
2602 if (bl->loc_type == bp_loc_software_breakpoint
2603 && !automatic_hardware_breakpoints)
2604 {
2605 mem_region *mr = lookup_mem_region (bl->address);
2606
2607 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2608 {
2609 fprintf_unfiltered (tmp_error_stream,
2610 _("Cannot insert breakpoint %d.\n"
2611 "Cannot set software breakpoint "
2612 "at read-only address %s\n"),
2613 bl->owner->number,
2614 paddress (bl->gdbarch, bl->address));
2615 return 1;
2616 }
2617 }
2618
2619 if (bl->loc_type == bp_loc_software_breakpoint
2620 || bl->loc_type == bp_loc_hardware_breakpoint)
2621 {
2622 /* First check to see if we have to handle an overlay. */
2623 if (overlay_debugging == ovly_off
2624 || bl->section == NULL
2625 || !(section_is_overlay (bl->section)))
2626 {
2627 /* No overlay handling: just set the breakpoint. */
2628 try
2629 {
2630 int val;
2631
2632 val = bl->owner->ops->insert_location (bl);
2633 if (val)
2634 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2635 }
2636 catch (gdb_exception &e)
2637 {
2638 bp_excpt = std::move (e);
2639 }
2640 }
2641 else
2642 {
2643 /* This breakpoint is in an overlay section.
2644 Shall we set a breakpoint at the LMA? */
2645 if (!overlay_events_enabled)
2646 {
2647 /* Yes -- overlay event support is not active,
2648 so we must try to set a breakpoint at the LMA.
2649 This will not work for a hardware breakpoint. */
2650 if (bl->loc_type == bp_loc_hardware_breakpoint)
2651 warning (_("hardware breakpoint %d not supported in overlay!"),
2652 bl->owner->number);
2653 else
2654 {
2655 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2656 bl->section);
2657 /* Set a software (trap) breakpoint at the LMA. */
2658 bl->overlay_target_info = bl->target_info;
2659 bl->overlay_target_info.reqstd_address = addr;
2660
2661 /* No overlay handling: just set the breakpoint. */
2662 try
2663 {
2664 int val;
2665
2666 bl->overlay_target_info.kind
2667 = breakpoint_kind (bl, &addr);
2668 bl->overlay_target_info.placed_address = addr;
2669 val = target_insert_breakpoint (bl->gdbarch,
2670 &bl->overlay_target_info);
2671 if (val)
2672 bp_excpt
2673 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2674 }
2675 catch (gdb_exception &e)
2676 {
2677 bp_excpt = std::move (e);
2678 }
2679
2680 if (bp_excpt.reason != 0)
2681 fprintf_unfiltered (tmp_error_stream,
2682 "Overlay breakpoint %d "
2683 "failed: in ROM?\n",
2684 bl->owner->number);
2685 }
2686 }
2687 /* Shall we set a breakpoint at the VMA? */
2688 if (section_is_mapped (bl->section))
2689 {
2690 /* Yes. This overlay section is mapped into memory. */
2691 try
2692 {
2693 int val;
2694
2695 val = bl->owner->ops->insert_location (bl);
2696 if (val)
2697 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2698 }
2699 catch (gdb_exception &e)
2700 {
2701 bp_excpt = std::move (e);
2702 }
2703 }
2704 else
2705 {
2706 /* No. This breakpoint will not be inserted.
2707 No error, but do not mark the bp as 'inserted'. */
2708 return 0;
2709 }
2710 }
2711
2712 if (bp_excpt.reason != 0)
2713 {
2714 /* Can't set the breakpoint. */
2715
2716 /* In some cases, we might not be able to insert a
2717 breakpoint in a shared library that has already been
2718 removed, but we have not yet processed the shlib unload
2719 event. Unfortunately, some targets that implement
2720 breakpoint insertion themselves can't tell why the
2721 breakpoint insertion failed (e.g., the remote target
2722 doesn't define error codes), so we must treat generic
2723 errors as memory errors. */
2724 if (bp_excpt.reason == RETURN_ERROR
2725 && (bp_excpt.error == GENERIC_ERROR
2726 || bp_excpt.error == MEMORY_ERROR)
2727 && bl->loc_type == bp_loc_software_breakpoint
2728 && (solib_name_from_address (bl->pspace, bl->address)
2729 || shared_objfile_contains_address_p (bl->pspace,
2730 bl->address)))
2731 {
2732 /* See also: disable_breakpoints_in_shlibs. */
2733 bl->shlib_disabled = 1;
2734 gdb::observers::breakpoint_modified.notify (bl->owner);
2735 if (!*disabled_breaks)
2736 {
2737 fprintf_unfiltered (tmp_error_stream,
2738 "Cannot insert breakpoint %d.\n",
2739 bl->owner->number);
2740 fprintf_unfiltered (tmp_error_stream,
2741 "Temporarily disabling shared "
2742 "library breakpoints:\n");
2743 }
2744 *disabled_breaks = 1;
2745 fprintf_unfiltered (tmp_error_stream,
2746 "breakpoint #%d\n", bl->owner->number);
2747 return 0;
2748 }
2749 else
2750 {
2751 if (bl->loc_type == bp_loc_hardware_breakpoint)
2752 {
2753 *hw_breakpoint_error = 1;
2754 *hw_bp_error_explained_already = bp_excpt.message != NULL;
2755 fprintf_unfiltered (tmp_error_stream,
2756 "Cannot insert hardware breakpoint %d%s",
2757 bl->owner->number,
2758 bp_excpt.message ? ":" : ".\n");
2759 if (bp_excpt.message != NULL)
2760 fprintf_unfiltered (tmp_error_stream, "%s.\n",
2761 bp_excpt.what ());
2762 }
2763 else
2764 {
2765 if (bp_excpt.message == NULL)
2766 {
2767 std::string message
2768 = memory_error_message (TARGET_XFER_E_IO,
2769 bl->gdbarch, bl->address);
2770
2771 fprintf_unfiltered (tmp_error_stream,
2772 "Cannot insert breakpoint %d.\n"
2773 "%s\n",
2774 bl->owner->number, message.c_str ());
2775 }
2776 else
2777 {
2778 fprintf_unfiltered (tmp_error_stream,
2779 "Cannot insert breakpoint %d: %s\n",
2780 bl->owner->number,
2781 bp_excpt.what ());
2782 }
2783 }
2784 return 1;
2785
2786 }
2787 }
2788 else
2789 bl->inserted = 1;
2790
2791 return 0;
2792 }
2793
2794 else if (bl->loc_type == bp_loc_hardware_watchpoint
2795 /* NOTE drow/2003-09-08: This state only exists for removing
2796 watchpoints. It's not clear that it's necessary... */
2797 && bl->owner->disposition != disp_del_at_next_stop)
2798 {
2799 int val;
2800
2801 gdb_assert (bl->owner->ops != NULL
2802 && bl->owner->ops->insert_location != NULL);
2803
2804 val = bl->owner->ops->insert_location (bl);
2805
2806 /* If trying to set a read-watchpoint, and it turns out it's not
2807 supported, try emulating one with an access watchpoint. */
2808 if (val == 1 && bl->watchpoint_type == hw_read)
2809 {
2810 struct bp_location *loc, **loc_temp;
2811
2812 /* But don't try to insert it, if there's already another
2813 hw_access location that would be considered a duplicate
2814 of this one. */
2815 ALL_BP_LOCATIONS (loc, loc_temp)
2816 if (loc != bl
2817 && loc->watchpoint_type == hw_access
2818 && watchpoint_locations_match (bl, loc))
2819 {
2820 bl->duplicate = 1;
2821 bl->inserted = 1;
2822 bl->target_info = loc->target_info;
2823 bl->watchpoint_type = hw_access;
2824 val = 0;
2825 break;
2826 }
2827
2828 if (val == 1)
2829 {
2830 bl->watchpoint_type = hw_access;
2831 val = bl->owner->ops->insert_location (bl);
2832
2833 if (val)
2834 /* Back to the original value. */
2835 bl->watchpoint_type = hw_read;
2836 }
2837 }
2838
2839 bl->inserted = (val == 0);
2840 }
2841
2842 else if (bl->owner->type == bp_catchpoint)
2843 {
2844 int val;
2845
2846 gdb_assert (bl->owner->ops != NULL
2847 && bl->owner->ops->insert_location != NULL);
2848
2849 val = bl->owner->ops->insert_location (bl);
2850 if (val)
2851 {
2852 bl->owner->enable_state = bp_disabled;
2853
2854 if (val == 1)
2855 warning (_("\
2856 Error inserting catchpoint %d: Your system does not support this type\n\
2857 of catchpoint."), bl->owner->number);
2858 else
2859 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2860 }
2861
2862 bl->inserted = (val == 0);
2863
2864 /* We've already printed an error message if there was a problem
2865 inserting this catchpoint, and we've disabled the catchpoint,
2866 so just return success. */
2867 return 0;
2868 }
2869
2870 return 0;
2871 }
2872
2873 /* This function is called when program space PSPACE is about to be
2874 deleted. It takes care of updating breakpoints to not reference
2875 PSPACE anymore. */
2876
2877 void
2878 breakpoint_program_space_exit (struct program_space *pspace)
2879 {
2880 struct breakpoint *b, *b_temp;
2881 struct bp_location *loc, **loc_temp;
2882
2883 /* Remove any breakpoint that was set through this program space. */
2884 ALL_BREAKPOINTS_SAFE (b, b_temp)
2885 {
2886 if (b->pspace == pspace)
2887 delete_breakpoint (b);
2888 }
2889
2890 /* Breakpoints set through other program spaces could have locations
2891 bound to PSPACE as well. Remove those. */
2892 ALL_BP_LOCATIONS (loc, loc_temp)
2893 {
2894 struct bp_location *tmp;
2895
2896 if (loc->pspace == pspace)
2897 {
2898 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2899 if (loc->owner->loc == loc)
2900 loc->owner->loc = loc->next;
2901 else
2902 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2903 if (tmp->next == loc)
2904 {
2905 tmp->next = loc->next;
2906 break;
2907 }
2908 }
2909 }
2910
2911 /* Now update the global location list to permanently delete the
2912 removed locations above. */
2913 update_global_location_list (UGLL_DONT_INSERT);
2914 }
2915
2916 /* Make sure all breakpoints are inserted in inferior.
2917 Throws exception on any error.
2918 A breakpoint that is already inserted won't be inserted
2919 again, so calling this function twice is safe. */
2920 void
2921 insert_breakpoints (void)
2922 {
2923 struct breakpoint *bpt;
2924
2925 ALL_BREAKPOINTS (bpt)
2926 if (is_hardware_watchpoint (bpt))
2927 {
2928 struct watchpoint *w = (struct watchpoint *) bpt;
2929
2930 update_watchpoint (w, 0 /* don't reparse. */);
2931 }
2932
2933 /* Updating watchpoints creates new locations, so update the global
2934 location list. Explicitly tell ugll to insert locations and
2935 ignore breakpoints_always_inserted_mode. Also,
2936 update_global_location_list tries to "upgrade" software
2937 breakpoints to hardware breakpoints to handle "set breakpoint
2938 auto-hw", so we need to call it even if we don't have new
2939 locations. */
2940 update_global_location_list (UGLL_INSERT);
2941 }
2942
2943 /* Invoke CALLBACK for each of bp_location. */
2944
2945 void
2946 iterate_over_bp_locations (walk_bp_location_callback callback)
2947 {
2948 struct bp_location *loc, **loc_tmp;
2949
2950 ALL_BP_LOCATIONS (loc, loc_tmp)
2951 {
2952 callback (loc, NULL);
2953 }
2954 }
2955
2956 /* This is used when we need to synch breakpoint conditions between GDB and the
2957 target. It is the case with deleting and disabling of breakpoints when using
2958 always-inserted mode. */
2959
2960 static void
2961 update_inserted_breakpoint_locations (void)
2962 {
2963 struct bp_location *bl, **blp_tmp;
2964 int error_flag = 0;
2965 int val = 0;
2966 int disabled_breaks = 0;
2967 int hw_breakpoint_error = 0;
2968 int hw_bp_details_reported = 0;
2969
2970 string_file tmp_error_stream;
2971
2972 /* Explicitly mark the warning -- this will only be printed if
2973 there was an error. */
2974 tmp_error_stream.puts ("Warning:\n");
2975
2976 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2977
2978 ALL_BP_LOCATIONS (bl, blp_tmp)
2979 {
2980 /* We only want to update software breakpoints and hardware
2981 breakpoints. */
2982 if (!is_breakpoint (bl->owner))
2983 continue;
2984
2985 /* We only want to update locations that are already inserted
2986 and need updating. This is to avoid unwanted insertion during
2987 deletion of breakpoints. */
2988 if (!bl->inserted || !bl->needs_update)
2989 continue;
2990
2991 switch_to_program_space_and_thread (bl->pspace);
2992
2993 /* For targets that support global breakpoints, there's no need
2994 to select an inferior to insert breakpoint to. In fact, even
2995 if we aren't attached to any process yet, we should still
2996 insert breakpoints. */
2997 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2998 && (inferior_ptid == null_ptid || !target_has_execution ()))
2999 continue;
3000
3001 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3002 &hw_breakpoint_error, &hw_bp_details_reported);
3003 if (val)
3004 error_flag = val;
3005 }
3006
3007 if (error_flag)
3008 {
3009 target_terminal::ours_for_output ();
3010 error_stream (tmp_error_stream);
3011 }
3012 }
3013
3014 /* Used when starting or continuing the program. */
3015
3016 static void
3017 insert_breakpoint_locations (void)
3018 {
3019 struct breakpoint *bpt;
3020 struct bp_location *bl, **blp_tmp;
3021 int error_flag = 0;
3022 int val = 0;
3023 int disabled_breaks = 0;
3024 int hw_breakpoint_error = 0;
3025 int hw_bp_error_explained_already = 0;
3026
3027 string_file tmp_error_stream;
3028
3029 /* Explicitly mark the warning -- this will only be printed if
3030 there was an error. */
3031 tmp_error_stream.puts ("Warning:\n");
3032
3033 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3034
3035 ALL_BP_LOCATIONS (bl, blp_tmp)
3036 {
3037 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3038 continue;
3039
3040 /* There is no point inserting thread-specific breakpoints if
3041 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3042 has BL->OWNER always non-NULL. */
3043 if (bl->owner->thread != -1
3044 && !valid_global_thread_id (bl->owner->thread))
3045 continue;
3046
3047 switch_to_program_space_and_thread (bl->pspace);
3048
3049 /* For targets that support global breakpoints, there's no need
3050 to select an inferior to insert breakpoint to. In fact, even
3051 if we aren't attached to any process yet, we should still
3052 insert breakpoints. */
3053 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3054 && (inferior_ptid == null_ptid || !target_has_execution ()))
3055 continue;
3056
3057 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3058 &hw_breakpoint_error, &hw_bp_error_explained_already);
3059 if (val)
3060 error_flag = val;
3061 }
3062
3063 /* If we failed to insert all locations of a watchpoint, remove
3064 them, as half-inserted watchpoint is of limited use. */
3065 ALL_BREAKPOINTS (bpt)
3066 {
3067 int some_failed = 0;
3068 struct bp_location *loc;
3069
3070 if (!is_hardware_watchpoint (bpt))
3071 continue;
3072
3073 if (!breakpoint_enabled (bpt))
3074 continue;
3075
3076 if (bpt->disposition == disp_del_at_next_stop)
3077 continue;
3078
3079 for (loc = bpt->loc; loc; loc = loc->next)
3080 if (!loc->inserted && should_be_inserted (loc))
3081 {
3082 some_failed = 1;
3083 break;
3084 }
3085 if (some_failed)
3086 {
3087 for (loc = bpt->loc; loc; loc = loc->next)
3088 if (loc->inserted)
3089 remove_breakpoint (loc);
3090
3091 hw_breakpoint_error = 1;
3092 tmp_error_stream.printf ("Could not insert "
3093 "hardware watchpoint %d.\n",
3094 bpt->number);
3095 error_flag = -1;
3096 }
3097 }
3098
3099 if (error_flag)
3100 {
3101 /* If a hardware breakpoint or watchpoint was inserted, add a
3102 message about possibly exhausted resources. */
3103 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3104 {
3105 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3106 You may have requested too many hardware breakpoints/watchpoints.\n");
3107 }
3108 target_terminal::ours_for_output ();
3109 error_stream (tmp_error_stream);
3110 }
3111 }
3112
3113 /* Used when the program stops.
3114 Returns zero if successful, or non-zero if there was a problem
3115 removing a breakpoint location. */
3116
3117 int
3118 remove_breakpoints (void)
3119 {
3120 struct bp_location *bl, **blp_tmp;
3121 int val = 0;
3122
3123 ALL_BP_LOCATIONS (bl, blp_tmp)
3124 {
3125 if (bl->inserted && !is_tracepoint (bl->owner))
3126 val |= remove_breakpoint (bl);
3127 }
3128 return val;
3129 }
3130
3131 /* When a thread exits, remove breakpoints that are related to
3132 that thread. */
3133
3134 static void
3135 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3136 {
3137 struct breakpoint *b, *b_tmp;
3138
3139 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3140 {
3141 if (b->thread == tp->global_num && user_breakpoint_p (b))
3142 {
3143 b->disposition = disp_del_at_next_stop;
3144
3145 printf_filtered (_("\
3146 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3147 b->number, print_thread_id (tp));
3148
3149 /* Hide it from the user. */
3150 b->number = 0;
3151 }
3152 }
3153 }
3154
3155 /* See breakpoint.h. */
3156
3157 void
3158 remove_breakpoints_inf (inferior *inf)
3159 {
3160 struct bp_location *bl, **blp_tmp;
3161 int val;
3162
3163 ALL_BP_LOCATIONS (bl, blp_tmp)
3164 {
3165 if (bl->pspace != inf->pspace)
3166 continue;
3167
3168 if (bl->inserted && !bl->target_info.persist)
3169 {
3170 val = remove_breakpoint (bl);
3171 if (val != 0)
3172 return;
3173 }
3174 }
3175 }
3176
3177 static int internal_breakpoint_number = -1;
3178
3179 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3180 If INTERNAL is non-zero, the breakpoint number will be populated
3181 from internal_breakpoint_number and that variable decremented.
3182 Otherwise the breakpoint number will be populated from
3183 breakpoint_count and that value incremented. Internal breakpoints
3184 do not set the internal var bpnum. */
3185 static void
3186 set_breakpoint_number (int internal, struct breakpoint *b)
3187 {
3188 if (internal)
3189 b->number = internal_breakpoint_number--;
3190 else
3191 {
3192 set_breakpoint_count (breakpoint_count + 1);
3193 b->number = breakpoint_count;
3194 }
3195 }
3196
3197 static struct breakpoint *
3198 create_internal_breakpoint (struct gdbarch *gdbarch,
3199 CORE_ADDR address, enum bptype type,
3200 const struct breakpoint_ops *ops)
3201 {
3202 symtab_and_line sal;
3203 sal.pc = address;
3204 sal.section = find_pc_overlay (sal.pc);
3205 sal.pspace = current_program_space;
3206
3207 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
3208 b->number = internal_breakpoint_number--;
3209 b->disposition = disp_donttouch;
3210
3211 return b;
3212 }
3213
3214 static const char *const longjmp_names[] =
3215 {
3216 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3217 };
3218 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3219
3220 /* Per-objfile data private to breakpoint.c. */
3221 struct breakpoint_objfile_data
3222 {
3223 /* Minimal symbol for "_ovly_debug_event" (if any). */
3224 struct bound_minimal_symbol overlay_msym {};
3225
3226 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3227 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
3228
3229 /* True if we have looked for longjmp probes. */
3230 int longjmp_searched = 0;
3231
3232 /* SystemTap probe points for longjmp (if any). These are non-owning
3233 references. */
3234 std::vector<probe *> longjmp_probes;
3235
3236 /* Minimal symbol for "std::terminate()" (if any). */
3237 struct bound_minimal_symbol terminate_msym {};
3238
3239 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3240 struct bound_minimal_symbol exception_msym {};
3241
3242 /* True if we have looked for exception probes. */
3243 int exception_searched = 0;
3244
3245 /* SystemTap probe points for unwinding (if any). These are non-owning
3246 references. */
3247 std::vector<probe *> exception_probes;
3248 };
3249
3250 static const struct objfile_key<breakpoint_objfile_data>
3251 breakpoint_objfile_key;
3252
3253 /* Minimal symbol not found sentinel. */
3254 static struct minimal_symbol msym_not_found;
3255
3256 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3257
3258 static int
3259 msym_not_found_p (const struct minimal_symbol *msym)
3260 {
3261 return msym == &msym_not_found;
3262 }
3263
3264 /* Return per-objfile data needed by breakpoint.c.
3265 Allocate the data if necessary. */
3266
3267 static struct breakpoint_objfile_data *
3268 get_breakpoint_objfile_data (struct objfile *objfile)
3269 {
3270 struct breakpoint_objfile_data *bp_objfile_data;
3271
3272 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3273 if (bp_objfile_data == NULL)
3274 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3275 return bp_objfile_data;
3276 }
3277
3278 static void
3279 create_overlay_event_breakpoint (void)
3280 {
3281 const char *const func_name = "_ovly_debug_event";
3282
3283 for (objfile *objfile : current_program_space->objfiles ())
3284 {
3285 struct breakpoint *b;
3286 struct breakpoint_objfile_data *bp_objfile_data;
3287 CORE_ADDR addr;
3288 struct explicit_location explicit_loc;
3289
3290 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3291
3292 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3293 continue;
3294
3295 if (bp_objfile_data->overlay_msym.minsym == NULL)
3296 {
3297 struct bound_minimal_symbol m;
3298
3299 m = lookup_minimal_symbol_text (func_name, objfile);
3300 if (m.minsym == NULL)
3301 {
3302 /* Avoid future lookups in this objfile. */
3303 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3304 continue;
3305 }
3306 bp_objfile_data->overlay_msym = m;
3307 }
3308
3309 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3310 b = create_internal_breakpoint (objfile->arch (), addr,
3311 bp_overlay_event,
3312 &internal_breakpoint_ops);
3313 initialize_explicit_location (&explicit_loc);
3314 explicit_loc.function_name = ASTRDUP (func_name);
3315 b->location = new_explicit_location (&explicit_loc);
3316
3317 if (overlay_debugging == ovly_auto)
3318 {
3319 b->enable_state = bp_enabled;
3320 overlay_events_enabled = 1;
3321 }
3322 else
3323 {
3324 b->enable_state = bp_disabled;
3325 overlay_events_enabled = 0;
3326 }
3327 }
3328 }
3329
3330 /* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3331 true if a breakpoint was installed. */
3332
3333 static bool
3334 create_longjmp_master_breakpoint_probe (objfile *objfile)
3335 {
3336 struct gdbarch *gdbarch = objfile->arch ();
3337 struct breakpoint_objfile_data *bp_objfile_data
3338 = get_breakpoint_objfile_data (objfile);
3339
3340 if (!bp_objfile_data->longjmp_searched)
3341 {
3342 std::vector<probe *> ret
3343 = find_probes_in_objfile (objfile, "libc", "longjmp");
3344
3345 if (!ret.empty ())
3346 {
3347 /* We are only interested in checking one element. */
3348 probe *p = ret[0];
3349
3350 if (!p->can_evaluate_arguments ())
3351 {
3352 /* We cannot use the probe interface here,
3353 because it does not know how to evaluate
3354 arguments. */
3355 ret.clear ();
3356 }
3357 }
3358 bp_objfile_data->longjmp_probes = ret;
3359 bp_objfile_data->longjmp_searched = 1;
3360 }
3361
3362 if (bp_objfile_data->longjmp_probes.empty ())
3363 return false;
3364
3365 for (probe *p : bp_objfile_data->longjmp_probes)
3366 {
3367 struct breakpoint *b;
3368
3369 b = create_internal_breakpoint (gdbarch,
3370 p->get_relocated_address (objfile),
3371 bp_longjmp_master,
3372 &internal_breakpoint_ops);
3373 b->location = new_probe_location ("-probe-stap libc:longjmp");
3374 b->enable_state = bp_disabled;
3375 }
3376
3377 return true;
3378 }
3379
3380 /* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3381 Return true if at least one breakpoint was installed. */
3382
3383 static bool
3384 create_longjmp_master_breakpoint_names (objfile *objfile)
3385 {
3386 struct gdbarch *gdbarch = objfile->arch ();
3387 if (!gdbarch_get_longjmp_target_p (gdbarch))
3388 return false;
3389
3390 struct breakpoint_objfile_data *bp_objfile_data
3391 = get_breakpoint_objfile_data (objfile);
3392 unsigned int installed_bp = 0;
3393
3394 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3395 {
3396 struct breakpoint *b;
3397 const char *func_name;
3398 CORE_ADDR addr;
3399 struct explicit_location explicit_loc;
3400
3401 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3402 continue;
3403
3404 func_name = longjmp_names[i];
3405 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3406 {
3407 struct bound_minimal_symbol m;
3408
3409 m = lookup_minimal_symbol_text (func_name, objfile);
3410 if (m.minsym == NULL)
3411 {
3412 /* Prevent future lookups in this objfile. */
3413 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3414 continue;
3415 }
3416 bp_objfile_data->longjmp_msym[i] = m;
3417 }
3418
3419 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3420 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3421 &internal_breakpoint_ops);
3422 initialize_explicit_location (&explicit_loc);
3423 explicit_loc.function_name = ASTRDUP (func_name);
3424 b->location = new_explicit_location (&explicit_loc);
3425 b->enable_state = bp_disabled;
3426 installed_bp++;
3427 }
3428
3429 return installed_bp > 0;
3430 }
3431
3432 /* Create a master longjmp breakpoint. */
3433
3434 static void
3435 create_longjmp_master_breakpoint (void)
3436 {
3437 scoped_restore_current_program_space restore_pspace;
3438
3439 for (struct program_space *pspace : program_spaces)
3440 {
3441 set_current_program_space (pspace);
3442
3443 for (objfile *obj : current_program_space->objfiles ())
3444 {
3445 /* Skip separate debug object, it's handled in the loop below. */
3446 if (obj->separate_debug_objfile_backlink != nullptr)
3447 continue;
3448
3449 /* Try a probe kind breakpoint on main objfile. */
3450 if (create_longjmp_master_breakpoint_probe (obj))
3451 continue;
3452
3453 /* Try longjmp_names kind breakpoints on main and separate_debug
3454 objfiles. */
3455 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3456 if (create_longjmp_master_breakpoint_names (debug_objfile))
3457 break;
3458 }
3459 }
3460 }
3461
3462 /* Create a master std::terminate breakpoint. */
3463 static void
3464 create_std_terminate_master_breakpoint (void)
3465 {
3466 const char *const func_name = "std::terminate()";
3467
3468 scoped_restore_current_program_space restore_pspace;
3469
3470 for (struct program_space *pspace : program_spaces)
3471 {
3472 CORE_ADDR addr;
3473
3474 set_current_program_space (pspace);
3475
3476 for (objfile *objfile : current_program_space->objfiles ())
3477 {
3478 struct breakpoint *b;
3479 struct breakpoint_objfile_data *bp_objfile_data;
3480 struct explicit_location explicit_loc;
3481
3482 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3483
3484 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3485 continue;
3486
3487 if (bp_objfile_data->terminate_msym.minsym == NULL)
3488 {
3489 struct bound_minimal_symbol m;
3490
3491 m = lookup_minimal_symbol (func_name, NULL, objfile);
3492 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3493 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3494 {
3495 /* Prevent future lookups in this objfile. */
3496 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3497 continue;
3498 }
3499 bp_objfile_data->terminate_msym = m;
3500 }
3501
3502 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3503 b = create_internal_breakpoint (objfile->arch (), addr,
3504 bp_std_terminate_master,
3505 &internal_breakpoint_ops);
3506 initialize_explicit_location (&explicit_loc);
3507 explicit_loc.function_name = ASTRDUP (func_name);
3508 b->location = new_explicit_location (&explicit_loc);
3509 b->enable_state = bp_disabled;
3510 }
3511 }
3512 }
3513
3514 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3515 probe. Return true if a breakpoint was installed. */
3516
3517 static bool
3518 create_exception_master_breakpoint_probe (objfile *objfile)
3519 {
3520 struct breakpoint *b;
3521 struct gdbarch *gdbarch;
3522 struct breakpoint_objfile_data *bp_objfile_data;
3523
3524 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3525
3526 /* We prefer the SystemTap probe point if it exists. */
3527 if (!bp_objfile_data->exception_searched)
3528 {
3529 std::vector<probe *> ret
3530 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3531
3532 if (!ret.empty ())
3533 {
3534 /* We are only interested in checking one element. */
3535 probe *p = ret[0];
3536
3537 if (!p->can_evaluate_arguments ())
3538 {
3539 /* We cannot use the probe interface here, because it does
3540 not know how to evaluate arguments. */
3541 ret.clear ();
3542 }
3543 }
3544 bp_objfile_data->exception_probes = ret;
3545 bp_objfile_data->exception_searched = 1;
3546 }
3547
3548 if (bp_objfile_data->exception_probes.empty ())
3549 return false;
3550
3551 gdbarch = objfile->arch ();
3552
3553 for (probe *p : bp_objfile_data->exception_probes)
3554 {
3555 b = create_internal_breakpoint (gdbarch,
3556 p->get_relocated_address (objfile),
3557 bp_exception_master,
3558 &internal_breakpoint_ops);
3559 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3560 b->enable_state = bp_disabled;
3561 }
3562
3563 return true;
3564 }
3565
3566 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3567 _Unwind_DebugHook. Return true if a breakpoint was installed. */
3568
3569 static bool
3570 create_exception_master_breakpoint_hook (objfile *objfile)
3571 {
3572 const char *const func_name = "_Unwind_DebugHook";
3573 struct breakpoint *b;
3574 struct gdbarch *gdbarch;
3575 struct breakpoint_objfile_data *bp_objfile_data;
3576 CORE_ADDR addr;
3577 struct explicit_location explicit_loc;
3578
3579 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3580
3581 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3582 return false;
3583
3584 gdbarch = objfile->arch ();
3585
3586 if (bp_objfile_data->exception_msym.minsym == NULL)
3587 {
3588 struct bound_minimal_symbol debug_hook;
3589
3590 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3591 if (debug_hook.minsym == NULL)
3592 {
3593 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3594 return false;
3595 }
3596
3597 bp_objfile_data->exception_msym = debug_hook;
3598 }
3599
3600 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3601 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3602 current_top_target ());
3603 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3604 &internal_breakpoint_ops);
3605 initialize_explicit_location (&explicit_loc);
3606 explicit_loc.function_name = ASTRDUP (func_name);
3607 b->location = new_explicit_location (&explicit_loc);
3608 b->enable_state = bp_disabled;
3609
3610 return true;
3611 }
3612
3613 /* Install a master breakpoint on the unwinder's debug hook. */
3614
3615 static void
3616 create_exception_master_breakpoint (void)
3617 {
3618 for (objfile *obj : current_program_space->objfiles ())
3619 {
3620 /* Skip separate debug object. */
3621 if (obj->separate_debug_objfile_backlink)
3622 continue;
3623
3624 /* Try a probe kind breakpoint. */
3625 if (create_exception_master_breakpoint_probe (obj))
3626 continue;
3627
3628 /* Iterate over main and separate debug objects and try an
3629 _Unwind_DebugHook kind breakpoint. */
3630 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3631 if (create_exception_master_breakpoint_hook (debug_objfile))
3632 break;
3633 }
3634 }
3635
3636 /* Does B have a location spec? */
3637
3638 static int
3639 breakpoint_event_location_empty_p (const struct breakpoint *b)
3640 {
3641 return b->location != NULL && event_location_empty_p (b->location.get ());
3642 }
3643
3644 void
3645 update_breakpoints_after_exec (void)
3646 {
3647 struct breakpoint *b, *b_tmp;
3648 struct bp_location *bploc, **bplocp_tmp;
3649
3650 /* We're about to delete breakpoints from GDB's lists. If the
3651 INSERTED flag is true, GDB will try to lift the breakpoints by
3652 writing the breakpoints' "shadow contents" back into memory. The
3653 "shadow contents" are NOT valid after an exec, so GDB should not
3654 do that. Instead, the target is responsible from marking
3655 breakpoints out as soon as it detects an exec. We don't do that
3656 here instead, because there may be other attempts to delete
3657 breakpoints after detecting an exec and before reaching here. */
3658 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3659 if (bploc->pspace == current_program_space)
3660 gdb_assert (!bploc->inserted);
3661
3662 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3663 {
3664 if (b->pspace != current_program_space)
3665 continue;
3666
3667 /* Solib breakpoints must be explicitly reset after an exec(). */
3668 if (b->type == bp_shlib_event)
3669 {
3670 delete_breakpoint (b);
3671 continue;
3672 }
3673
3674 /* JIT breakpoints must be explicitly reset after an exec(). */
3675 if (b->type == bp_jit_event)
3676 {
3677 delete_breakpoint (b);
3678 continue;
3679 }
3680
3681 /* Thread event breakpoints must be set anew after an exec(),
3682 as must overlay event and longjmp master breakpoints. */
3683 if (b->type == bp_thread_event || b->type == bp_overlay_event
3684 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3685 || b->type == bp_exception_master)
3686 {
3687 delete_breakpoint (b);
3688 continue;
3689 }
3690
3691 /* Step-resume breakpoints are meaningless after an exec(). */
3692 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3693 {
3694 delete_breakpoint (b);
3695 continue;
3696 }
3697
3698 /* Just like single-step breakpoints. */
3699 if (b->type == bp_single_step)
3700 {
3701 delete_breakpoint (b);
3702 continue;
3703 }
3704
3705 /* Longjmp and longjmp-resume breakpoints are also meaningless
3706 after an exec. */
3707 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3708 || b->type == bp_longjmp_call_dummy
3709 || b->type == bp_exception || b->type == bp_exception_resume)
3710 {
3711 delete_breakpoint (b);
3712 continue;
3713 }
3714
3715 if (b->type == bp_catchpoint)
3716 {
3717 /* For now, none of the bp_catchpoint breakpoints need to
3718 do anything at this point. In the future, if some of
3719 the catchpoints need to something, we will need to add
3720 a new method, and call this method from here. */
3721 continue;
3722 }
3723
3724 /* bp_finish is a special case. The only way we ought to be able
3725 to see one of these when an exec() has happened, is if the user
3726 caught a vfork, and then said "finish". Ordinarily a finish just
3727 carries them to the call-site of the current callee, by setting
3728 a temporary bp there and resuming. But in this case, the finish
3729 will carry them entirely through the vfork & exec.
3730
3731 We don't want to allow a bp_finish to remain inserted now. But
3732 we can't safely delete it, 'cause finish_command has a handle to
3733 the bp on a bpstat, and will later want to delete it. There's a
3734 chance (and I've seen it happen) that if we delete the bp_finish
3735 here, that its storage will get reused by the time finish_command
3736 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3737 We really must allow finish_command to delete a bp_finish.
3738
3739 In the absence of a general solution for the "how do we know
3740 it's safe to delete something others may have handles to?"
3741 problem, what we'll do here is just uninsert the bp_finish, and
3742 let finish_command delete it.
3743
3744 (We know the bp_finish is "doomed" in the sense that it's
3745 momentary, and will be deleted as soon as finish_command sees
3746 the inferior stopped. So it doesn't matter that the bp's
3747 address is probably bogus in the new a.out, unlike e.g., the
3748 solib breakpoints.) */
3749
3750 if (b->type == bp_finish)
3751 {
3752 continue;
3753 }
3754
3755 /* Without a symbolic address, we have little hope of the
3756 pre-exec() address meaning the same thing in the post-exec()
3757 a.out. */
3758 if (breakpoint_event_location_empty_p (b))
3759 {
3760 delete_breakpoint (b);
3761 continue;
3762 }
3763 }
3764 }
3765
3766 int
3767 detach_breakpoints (ptid_t ptid)
3768 {
3769 struct bp_location *bl, **blp_tmp;
3770 int val = 0;
3771 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3772 struct inferior *inf = current_inferior ();
3773
3774 if (ptid.pid () == inferior_ptid.pid ())
3775 error (_("Cannot detach breakpoints of inferior_ptid"));
3776
3777 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3778 inferior_ptid = ptid;
3779 ALL_BP_LOCATIONS (bl, blp_tmp)
3780 {
3781 if (bl->pspace != inf->pspace)
3782 continue;
3783
3784 /* This function must physically remove breakpoints locations
3785 from the specified ptid, without modifying the breakpoint
3786 package's state. Locations of type bp_loc_other are only
3787 maintained at GDB side. So, there is no need to remove
3788 these bp_loc_other locations. Moreover, removing these
3789 would modify the breakpoint package's state. */
3790 if (bl->loc_type == bp_loc_other)
3791 continue;
3792
3793 if (bl->inserted)
3794 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3795 }
3796
3797 return val;
3798 }
3799
3800 /* Remove the breakpoint location BL from the current address space.
3801 Note that this is used to detach breakpoints from a child fork.
3802 When we get here, the child isn't in the inferior list, and neither
3803 do we have objects to represent its address space --- we should
3804 *not* look at bl->pspace->aspace here. */
3805
3806 static int
3807 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3808 {
3809 int val;
3810
3811 /* BL is never in moribund_locations by our callers. */
3812 gdb_assert (bl->owner != NULL);
3813
3814 /* The type of none suggests that owner is actually deleted.
3815 This should not ever happen. */
3816 gdb_assert (bl->owner->type != bp_none);
3817
3818 if (bl->loc_type == bp_loc_software_breakpoint
3819 || bl->loc_type == bp_loc_hardware_breakpoint)
3820 {
3821 /* "Normal" instruction breakpoint: either the standard
3822 trap-instruction bp (bp_breakpoint), or a
3823 bp_hardware_breakpoint. */
3824
3825 /* First check to see if we have to handle an overlay. */
3826 if (overlay_debugging == ovly_off
3827 || bl->section == NULL
3828 || !(section_is_overlay (bl->section)))
3829 {
3830 /* No overlay handling: just remove the breakpoint. */
3831
3832 /* If we're trying to uninsert a memory breakpoint that we
3833 know is set in a dynamic object that is marked
3834 shlib_disabled, then either the dynamic object was
3835 removed with "remove-symbol-file" or with
3836 "nosharedlibrary". In the former case, we don't know
3837 whether another dynamic object might have loaded over the
3838 breakpoint's address -- the user might well let us know
3839 about it next with add-symbol-file (the whole point of
3840 add-symbol-file is letting the user manually maintain a
3841 list of dynamically loaded objects). If we have the
3842 breakpoint's shadow memory, that is, this is a software
3843 breakpoint managed by GDB, check whether the breakpoint
3844 is still inserted in memory, to avoid overwriting wrong
3845 code with stale saved shadow contents. Note that HW
3846 breakpoints don't have shadow memory, as they're
3847 implemented using a mechanism that is not dependent on
3848 being able to modify the target's memory, and as such
3849 they should always be removed. */
3850 if (bl->shlib_disabled
3851 && bl->target_info.shadow_len != 0
3852 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3853 val = 0;
3854 else
3855 val = bl->owner->ops->remove_location (bl, reason);
3856 }
3857 else
3858 {
3859 /* This breakpoint is in an overlay section.
3860 Did we set a breakpoint at the LMA? */
3861 if (!overlay_events_enabled)
3862 {
3863 /* Yes -- overlay event support is not active, so we
3864 should have set a breakpoint at the LMA. Remove it.
3865 */
3866 /* Ignore any failures: if the LMA is in ROM, we will
3867 have already warned when we failed to insert it. */
3868 if (bl->loc_type == bp_loc_hardware_breakpoint)
3869 target_remove_hw_breakpoint (bl->gdbarch,
3870 &bl->overlay_target_info);
3871 else
3872 target_remove_breakpoint (bl->gdbarch,
3873 &bl->overlay_target_info,
3874 reason);
3875 }
3876 /* Did we set a breakpoint at the VMA?
3877 If so, we will have marked the breakpoint 'inserted'. */
3878 if (bl->inserted)
3879 {
3880 /* Yes -- remove it. Previously we did not bother to
3881 remove the breakpoint if the section had been
3882 unmapped, but let's not rely on that being safe. We
3883 don't know what the overlay manager might do. */
3884
3885 /* However, we should remove *software* breakpoints only
3886 if the section is still mapped, or else we overwrite
3887 wrong code with the saved shadow contents. */
3888 if (bl->loc_type == bp_loc_hardware_breakpoint
3889 || section_is_mapped (bl->section))
3890 val = bl->owner->ops->remove_location (bl, reason);
3891 else
3892 val = 0;
3893 }
3894 else
3895 {
3896 /* No -- not inserted, so no need to remove. No error. */
3897 val = 0;
3898 }
3899 }
3900
3901 /* In some cases, we might not be able to remove a breakpoint in
3902 a shared library that has already been removed, but we have
3903 not yet processed the shlib unload event. Similarly for an
3904 unloaded add-symbol-file object - the user might not yet have
3905 had the chance to remove-symbol-file it. shlib_disabled will
3906 be set if the library/object has already been removed, but
3907 the breakpoint hasn't been uninserted yet, e.g., after
3908 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3909 always-inserted mode. */
3910 if (val
3911 && (bl->loc_type == bp_loc_software_breakpoint
3912 && (bl->shlib_disabled
3913 || solib_name_from_address (bl->pspace, bl->address)
3914 || shared_objfile_contains_address_p (bl->pspace,
3915 bl->address))))
3916 val = 0;
3917
3918 if (val)
3919 return val;
3920 bl->inserted = (reason == DETACH_BREAKPOINT);
3921 }
3922 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3923 {
3924 gdb_assert (bl->owner->ops != NULL
3925 && bl->owner->ops->remove_location != NULL);
3926
3927 bl->inserted = (reason == DETACH_BREAKPOINT);
3928 bl->owner->ops->remove_location (bl, reason);
3929
3930 /* Failure to remove any of the hardware watchpoints comes here. */
3931 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3932 warning (_("Could not remove hardware watchpoint %d."),
3933 bl->owner->number);
3934 }
3935 else if (bl->owner->type == bp_catchpoint
3936 && breakpoint_enabled (bl->owner)
3937 && !bl->duplicate)
3938 {
3939 gdb_assert (bl->owner->ops != NULL
3940 && bl->owner->ops->remove_location != NULL);
3941
3942 val = bl->owner->ops->remove_location (bl, reason);
3943 if (val)
3944 return val;
3945
3946 bl->inserted = (reason == DETACH_BREAKPOINT);
3947 }
3948
3949 return 0;
3950 }
3951
3952 static int
3953 remove_breakpoint (struct bp_location *bl)
3954 {
3955 /* BL is never in moribund_locations by our callers. */
3956 gdb_assert (bl->owner != NULL);
3957
3958 /* The type of none suggests that owner is actually deleted.
3959 This should not ever happen. */
3960 gdb_assert (bl->owner->type != bp_none);
3961
3962 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3963
3964 switch_to_program_space_and_thread (bl->pspace);
3965
3966 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
3967 }
3968
3969 /* Clear the "inserted" flag in all breakpoints. */
3970
3971 void
3972 mark_breakpoints_out (void)
3973 {
3974 struct bp_location *bl, **blp_tmp;
3975
3976 ALL_BP_LOCATIONS (bl, blp_tmp)
3977 if (bl->pspace == current_program_space)
3978 bl->inserted = 0;
3979 }
3980
3981 /* Clear the "inserted" flag in all breakpoints and delete any
3982 breakpoints which should go away between runs of the program.
3983
3984 Plus other such housekeeping that has to be done for breakpoints
3985 between runs.
3986
3987 Note: this function gets called at the end of a run (by
3988 generic_mourn_inferior) and when a run begins (by
3989 init_wait_for_inferior). */
3990
3991
3992
3993 void
3994 breakpoint_init_inferior (enum inf_context context)
3995 {
3996 struct breakpoint *b, *b_tmp;
3997 struct program_space *pspace = current_program_space;
3998
3999 /* If breakpoint locations are shared across processes, then there's
4000 nothing to do. */
4001 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4002 return;
4003
4004 mark_breakpoints_out ();
4005
4006 ALL_BREAKPOINTS_SAFE (b, b_tmp)
4007 {
4008 if (b->loc && b->loc->pspace != pspace)
4009 continue;
4010
4011 switch (b->type)
4012 {
4013 case bp_call_dummy:
4014 case bp_longjmp_call_dummy:
4015
4016 /* If the call dummy breakpoint is at the entry point it will
4017 cause problems when the inferior is rerun, so we better get
4018 rid of it. */
4019
4020 case bp_watchpoint_scope:
4021
4022 /* Also get rid of scope breakpoints. */
4023
4024 case bp_shlib_event:
4025
4026 /* Also remove solib event breakpoints. Their addresses may
4027 have changed since the last time we ran the program.
4028 Actually we may now be debugging against different target;
4029 and so the solib backend that installed this breakpoint may
4030 not be used in by the target. E.g.,
4031
4032 (gdb) file prog-linux
4033 (gdb) run # native linux target
4034 ...
4035 (gdb) kill
4036 (gdb) file prog-win.exe
4037 (gdb) tar rem :9999 # remote Windows gdbserver.
4038 */
4039
4040 case bp_step_resume:
4041
4042 /* Also remove step-resume breakpoints. */
4043
4044 case bp_single_step:
4045
4046 /* Also remove single-step breakpoints. */
4047
4048 delete_breakpoint (b);
4049 break;
4050
4051 case bp_watchpoint:
4052 case bp_hardware_watchpoint:
4053 case bp_read_watchpoint:
4054 case bp_access_watchpoint:
4055 {
4056 struct watchpoint *w = (struct watchpoint *) b;
4057
4058 /* Likewise for watchpoints on local expressions. */
4059 if (w->exp_valid_block != NULL)
4060 delete_breakpoint (b);
4061 else
4062 {
4063 /* Get rid of existing locations, which are no longer
4064 valid. New ones will be created in
4065 update_watchpoint, when the inferior is restarted.
4066 The next update_global_location_list call will
4067 garbage collect them. */
4068 b->loc = NULL;
4069
4070 if (context == inf_starting)
4071 {
4072 /* Reset val field to force reread of starting value in
4073 insert_breakpoints. */
4074 w->val.reset (nullptr);
4075 w->val_valid = false;
4076 }
4077 }
4078 }
4079 break;
4080 default:
4081 break;
4082 }
4083 }
4084
4085 /* Get rid of the moribund locations. */
4086 for (bp_location *bl : moribund_locations)
4087 decref_bp_location (&bl);
4088 moribund_locations.clear ();
4089 }
4090
4091 /* These functions concern about actual breakpoints inserted in the
4092 target --- to e.g. check if we need to do decr_pc adjustment or if
4093 we need to hop over the bkpt --- so we check for address space
4094 match, not program space. */
4095
4096 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4097 exists at PC. It returns ordinary_breakpoint_here if it's an
4098 ordinary breakpoint, or permanent_breakpoint_here if it's a
4099 permanent breakpoint.
4100 - When continuing from a location with an ordinary breakpoint, we
4101 actually single step once before calling insert_breakpoints.
4102 - When continuing from a location with a permanent breakpoint, we
4103 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4104 the target, to advance the PC past the breakpoint. */
4105
4106 enum breakpoint_here
4107 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4108 {
4109 struct bp_location *bl, **blp_tmp;
4110 int any_breakpoint_here = 0;
4111
4112 ALL_BP_LOCATIONS (bl, blp_tmp)
4113 {
4114 if (bl->loc_type != bp_loc_software_breakpoint
4115 && bl->loc_type != bp_loc_hardware_breakpoint)
4116 continue;
4117
4118 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4119 if ((breakpoint_enabled (bl->owner)
4120 || bl->permanent)
4121 && breakpoint_location_address_match (bl, aspace, pc))
4122 {
4123 if (overlay_debugging
4124 && section_is_overlay (bl->section)
4125 && !section_is_mapped (bl->section))
4126 continue; /* unmapped overlay -- can't be a match */
4127 else if (bl->permanent)
4128 return permanent_breakpoint_here;
4129 else
4130 any_breakpoint_here = 1;
4131 }
4132 }
4133
4134 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4135 }
4136
4137 /* See breakpoint.h. */
4138
4139 int
4140 breakpoint_in_range_p (const address_space *aspace,
4141 CORE_ADDR addr, ULONGEST len)
4142 {
4143 struct bp_location *bl, **blp_tmp;
4144
4145 ALL_BP_LOCATIONS (bl, blp_tmp)
4146 {
4147 if (bl->loc_type != bp_loc_software_breakpoint
4148 && bl->loc_type != bp_loc_hardware_breakpoint)
4149 continue;
4150
4151 if ((breakpoint_enabled (bl->owner)
4152 || bl->permanent)
4153 && breakpoint_location_address_range_overlap (bl, aspace,
4154 addr, len))
4155 {
4156 if (overlay_debugging
4157 && section_is_overlay (bl->section)
4158 && !section_is_mapped (bl->section))
4159 {
4160 /* Unmapped overlay -- can't be a match. */
4161 continue;
4162 }
4163
4164 return 1;
4165 }
4166 }
4167
4168 return 0;
4169 }
4170
4171 /* Return true if there's a moribund breakpoint at PC. */
4172
4173 int
4174 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4175 {
4176 for (bp_location *loc : moribund_locations)
4177 if (breakpoint_location_address_match (loc, aspace, pc))
4178 return 1;
4179
4180 return 0;
4181 }
4182
4183 /* Returns non-zero iff BL is inserted at PC, in address space
4184 ASPACE. */
4185
4186 static int
4187 bp_location_inserted_here_p (struct bp_location *bl,
4188 const address_space *aspace, CORE_ADDR pc)
4189 {
4190 if (bl->inserted
4191 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4192 aspace, pc))
4193 {
4194 if (overlay_debugging
4195 && section_is_overlay (bl->section)
4196 && !section_is_mapped (bl->section))
4197 return 0; /* unmapped overlay -- can't be a match */
4198 else
4199 return 1;
4200 }
4201 return 0;
4202 }
4203
4204 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4205
4206 int
4207 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4208 {
4209 struct bp_location **blp, **blp_tmp = NULL;
4210
4211 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4212 {
4213 struct bp_location *bl = *blp;
4214
4215 if (bl->loc_type != bp_loc_software_breakpoint
4216 && bl->loc_type != bp_loc_hardware_breakpoint)
4217 continue;
4218
4219 if (bp_location_inserted_here_p (bl, aspace, pc))
4220 return 1;
4221 }
4222 return 0;
4223 }
4224
4225 /* This function returns non-zero iff there is a software breakpoint
4226 inserted at PC. */
4227
4228 int
4229 software_breakpoint_inserted_here_p (const address_space *aspace,
4230 CORE_ADDR pc)
4231 {
4232 struct bp_location **blp, **blp_tmp = NULL;
4233
4234 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4235 {
4236 struct bp_location *bl = *blp;
4237
4238 if (bl->loc_type != bp_loc_software_breakpoint)
4239 continue;
4240
4241 if (bp_location_inserted_here_p (bl, aspace, pc))
4242 return 1;
4243 }
4244
4245 return 0;
4246 }
4247
4248 /* See breakpoint.h. */
4249
4250 int
4251 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4252 CORE_ADDR pc)
4253 {
4254 struct bp_location **blp, **blp_tmp = NULL;
4255
4256 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4257 {
4258 struct bp_location *bl = *blp;
4259
4260 if (bl->loc_type != bp_loc_hardware_breakpoint)
4261 continue;
4262
4263 if (bp_location_inserted_here_p (bl, aspace, pc))
4264 return 1;
4265 }
4266
4267 return 0;
4268 }
4269
4270 int
4271 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4272 CORE_ADDR addr, ULONGEST len)
4273 {
4274 struct breakpoint *bpt;
4275
4276 ALL_BREAKPOINTS (bpt)
4277 {
4278 struct bp_location *loc;
4279
4280 if (bpt->type != bp_hardware_watchpoint
4281 && bpt->type != bp_access_watchpoint)
4282 continue;
4283
4284 if (!breakpoint_enabled (bpt))
4285 continue;
4286
4287 for (loc = bpt->loc; loc; loc = loc->next)
4288 if (loc->pspace->aspace == aspace && loc->inserted)
4289 {
4290 CORE_ADDR l, h;
4291
4292 /* Check for intersection. */
4293 l = std::max<CORE_ADDR> (loc->address, addr);
4294 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4295 if (l < h)
4296 return 1;
4297 }
4298 }
4299 return 0;
4300 }
4301
4302 /* See breakpoint.h. */
4303
4304 bool
4305 is_catchpoint (struct breakpoint *b)
4306 {
4307 return (b->type == bp_catchpoint);
4308 }
4309
4310 /* Clear a bpstat so that it says we are not at any breakpoint.
4311 Also free any storage that is part of a bpstat. */
4312
4313 void
4314 bpstat_clear (bpstat *bsp)
4315 {
4316 bpstat p;
4317 bpstat q;
4318
4319 if (bsp == 0)
4320 return;
4321 p = *bsp;
4322 while (p != NULL)
4323 {
4324 q = p->next;
4325 delete p;
4326 p = q;
4327 }
4328 *bsp = NULL;
4329 }
4330
4331 bpstats::bpstats (const bpstats &other)
4332 : next (NULL),
4333 bp_location_at (other.bp_location_at),
4334 breakpoint_at (other.breakpoint_at),
4335 commands (other.commands),
4336 print (other.print),
4337 stop (other.stop),
4338 print_it (other.print_it)
4339 {
4340 if (other.old_val != NULL)
4341 old_val = release_value (value_copy (other.old_val.get ()));
4342 }
4343
4344 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4345 is part of the bpstat is copied as well. */
4346
4347 bpstat
4348 bpstat_copy (bpstat bs)
4349 {
4350 bpstat p = NULL;
4351 bpstat tmp;
4352 bpstat retval = NULL;
4353
4354 if (bs == NULL)
4355 return bs;
4356
4357 for (; bs != NULL; bs = bs->next)
4358 {
4359 tmp = new bpstats (*bs);
4360
4361 if (p == NULL)
4362 /* This is the first thing in the chain. */
4363 retval = tmp;
4364 else
4365 p->next = tmp;
4366 p = tmp;
4367 }
4368 p->next = NULL;
4369 return retval;
4370 }
4371
4372 /* Find the bpstat associated with this breakpoint. */
4373
4374 bpstat
4375 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4376 {
4377 if (bsp == NULL)
4378 return NULL;
4379
4380 for (; bsp != NULL; bsp = bsp->next)
4381 {
4382 if (bsp->breakpoint_at == breakpoint)
4383 return bsp;
4384 }
4385 return NULL;
4386 }
4387
4388 /* See breakpoint.h. */
4389
4390 bool
4391 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4392 {
4393 for (; bsp != NULL; bsp = bsp->next)
4394 {
4395 if (bsp->breakpoint_at == NULL)
4396 {
4397 /* A moribund location can never explain a signal other than
4398 GDB_SIGNAL_TRAP. */
4399 if (sig == GDB_SIGNAL_TRAP)
4400 return true;
4401 }
4402 else
4403 {
4404 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4405 sig))
4406 return true;
4407 }
4408 }
4409
4410 return false;
4411 }
4412
4413 /* Put in *NUM the breakpoint number of the first breakpoint we are
4414 stopped at. *BSP upon return is a bpstat which points to the
4415 remaining breakpoints stopped at (but which is not guaranteed to be
4416 good for anything but further calls to bpstat_num).
4417
4418 Return 0 if passed a bpstat which does not indicate any breakpoints.
4419 Return -1 if stopped at a breakpoint that has been deleted since
4420 we set it.
4421 Return 1 otherwise. */
4422
4423 int
4424 bpstat_num (bpstat *bsp, int *num)
4425 {
4426 struct breakpoint *b;
4427
4428 if ((*bsp) == NULL)
4429 return 0; /* No more breakpoint values */
4430
4431 /* We assume we'll never have several bpstats that correspond to a
4432 single breakpoint -- otherwise, this function might return the
4433 same number more than once and this will look ugly. */
4434 b = (*bsp)->breakpoint_at;
4435 *bsp = (*bsp)->next;
4436 if (b == NULL)
4437 return -1; /* breakpoint that's been deleted since */
4438
4439 *num = b->number; /* We have its number */
4440 return 1;
4441 }
4442
4443 /* See breakpoint.h. */
4444
4445 void
4446 bpstat_clear_actions (void)
4447 {
4448 bpstat bs;
4449
4450 if (inferior_ptid == null_ptid)
4451 return;
4452
4453 thread_info *tp = inferior_thread ();
4454 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4455 {
4456 bs->commands = NULL;
4457 bs->old_val.reset (nullptr);
4458 }
4459 }
4460
4461 /* Called when a command is about to proceed the inferior. */
4462
4463 static void
4464 breakpoint_about_to_proceed (void)
4465 {
4466 if (inferior_ptid != null_ptid)
4467 {
4468 struct thread_info *tp = inferior_thread ();
4469
4470 /* Allow inferior function calls in breakpoint commands to not
4471 interrupt the command list. When the call finishes
4472 successfully, the inferior will be standing at the same
4473 breakpoint as if nothing happened. */
4474 if (tp->control.in_infcall)
4475 return;
4476 }
4477
4478 breakpoint_proceeded = 1;
4479 }
4480
4481 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4482 or its equivalent. */
4483
4484 static int
4485 command_line_is_silent (struct command_line *cmd)
4486 {
4487 return cmd && (strcmp ("silent", cmd->line) == 0);
4488 }
4489
4490 /* Execute all the commands associated with all the breakpoints at
4491 this location. Any of these commands could cause the process to
4492 proceed beyond this point, etc. We look out for such changes by
4493 checking the global "breakpoint_proceeded" after each command.
4494
4495 Returns true if a breakpoint command resumed the inferior. In that
4496 case, it is the caller's responsibility to recall it again with the
4497 bpstat of the current thread. */
4498
4499 static int
4500 bpstat_do_actions_1 (bpstat *bsp)
4501 {
4502 bpstat bs;
4503 int again = 0;
4504
4505 /* Avoid endless recursion if a `source' command is contained
4506 in bs->commands. */
4507 if (executing_breakpoint_commands)
4508 return 0;
4509
4510 scoped_restore save_executing
4511 = make_scoped_restore (&executing_breakpoint_commands, 1);
4512
4513 scoped_restore preventer = prevent_dont_repeat ();
4514
4515 /* This pointer will iterate over the list of bpstat's. */
4516 bs = *bsp;
4517
4518 breakpoint_proceeded = 0;
4519 for (; bs != NULL; bs = bs->next)
4520 {
4521 struct command_line *cmd = NULL;
4522
4523 /* Take ownership of the BSP's command tree, if it has one.
4524
4525 The command tree could legitimately contain commands like
4526 'step' and 'next', which call clear_proceed_status, which
4527 frees stop_bpstat's command tree. To make sure this doesn't
4528 free the tree we're executing out from under us, we need to
4529 take ownership of the tree ourselves. Since a given bpstat's
4530 commands are only executed once, we don't need to copy it; we
4531 can clear the pointer in the bpstat, and make sure we free
4532 the tree when we're done. */
4533 counted_command_line ccmd = bs->commands;
4534 bs->commands = NULL;
4535 if (ccmd != NULL)
4536 cmd = ccmd.get ();
4537 if (command_line_is_silent (cmd))
4538 {
4539 /* The action has been already done by bpstat_stop_status. */
4540 cmd = cmd->next;
4541 }
4542
4543 while (cmd != NULL)
4544 {
4545 execute_control_command (cmd);
4546
4547 if (breakpoint_proceeded)
4548 break;
4549 else
4550 cmd = cmd->next;
4551 }
4552
4553 if (breakpoint_proceeded)
4554 {
4555 if (current_ui->async)
4556 /* If we are in async mode, then the target might be still
4557 running, not stopped at any breakpoint, so nothing for
4558 us to do here -- just return to the event loop. */
4559 ;
4560 else
4561 /* In sync mode, when execute_control_command returns
4562 we're already standing on the next breakpoint.
4563 Breakpoint commands for that stop were not run, since
4564 execute_command does not run breakpoint commands --
4565 only command_line_handler does, but that one is not
4566 involved in execution of breakpoint commands. So, we
4567 can now execute breakpoint commands. It should be
4568 noted that making execute_command do bpstat actions is
4569 not an option -- in this case we'll have recursive
4570 invocation of bpstat for each breakpoint with a
4571 command, and can easily blow up GDB stack. Instead, we
4572 return true, which will trigger the caller to recall us
4573 with the new stop_bpstat. */
4574 again = 1;
4575 break;
4576 }
4577 }
4578 return again;
4579 }
4580
4581 /* Helper for bpstat_do_actions. Get the current thread, if there's
4582 one, is alive and has execution. Return NULL otherwise. */
4583
4584 static thread_info *
4585 get_bpstat_thread ()
4586 {
4587 if (inferior_ptid == null_ptid || !target_has_execution ())
4588 return NULL;
4589
4590 thread_info *tp = inferior_thread ();
4591 if (tp->state == THREAD_EXITED || tp->executing)
4592 return NULL;
4593 return tp;
4594 }
4595
4596 void
4597 bpstat_do_actions (void)
4598 {
4599 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4600 thread_info *tp;
4601
4602 /* Do any commands attached to breakpoint we are stopped at. */
4603 while ((tp = get_bpstat_thread ()) != NULL)
4604 {
4605 /* Since in sync mode, bpstat_do_actions may resume the
4606 inferior, and only return when it is stopped at the next
4607 breakpoint, we keep doing breakpoint actions until it returns
4608 false to indicate the inferior was not resumed. */
4609 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4610 break;
4611 }
4612
4613 cleanup_if_error.release ();
4614 }
4615
4616 /* Print out the (old or new) value associated with a watchpoint. */
4617
4618 static void
4619 watchpoint_value_print (struct value *val, struct ui_file *stream)
4620 {
4621 if (val == NULL)
4622 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4623 else
4624 {
4625 struct value_print_options opts;
4626 get_user_print_options (&opts);
4627 value_print (val, stream, &opts);
4628 }
4629 }
4630
4631 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4632 debugging multiple threads. */
4633
4634 void
4635 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4636 {
4637 if (uiout->is_mi_like_p ())
4638 return;
4639
4640 uiout->text ("\n");
4641
4642 if (show_thread_that_caused_stop ())
4643 {
4644 const char *name;
4645 struct thread_info *thr = inferior_thread ();
4646
4647 uiout->text ("Thread ");
4648 uiout->field_string ("thread-id", print_thread_id (thr));
4649
4650 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4651 if (name != NULL)
4652 {
4653 uiout->text (" \"");
4654 uiout->field_string ("name", name);
4655 uiout->text ("\"");
4656 }
4657
4658 uiout->text (" hit ");
4659 }
4660 }
4661
4662 /* Generic routine for printing messages indicating why we
4663 stopped. The behavior of this function depends on the value
4664 'print_it' in the bpstat structure. Under some circumstances we
4665 may decide not to print anything here and delegate the task to
4666 normal_stop(). */
4667
4668 static enum print_stop_action
4669 print_bp_stop_message (bpstat bs)
4670 {
4671 switch (bs->print_it)
4672 {
4673 case print_it_noop:
4674 /* Nothing should be printed for this bpstat entry. */
4675 return PRINT_UNKNOWN;
4676 break;
4677
4678 case print_it_done:
4679 /* We still want to print the frame, but we already printed the
4680 relevant messages. */
4681 return PRINT_SRC_AND_LOC;
4682 break;
4683
4684 case print_it_normal:
4685 {
4686 struct breakpoint *b = bs->breakpoint_at;
4687
4688 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4689 which has since been deleted. */
4690 if (b == NULL)
4691 return PRINT_UNKNOWN;
4692
4693 /* Normal case. Call the breakpoint's print_it method. */
4694 return b->ops->print_it (bs);
4695 }
4696 break;
4697
4698 default:
4699 internal_error (__FILE__, __LINE__,
4700 _("print_bp_stop_message: unrecognized enum value"));
4701 break;
4702 }
4703 }
4704
4705 /* A helper function that prints a shared library stopped event. */
4706
4707 static void
4708 print_solib_event (int is_catchpoint)
4709 {
4710 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4711 bool any_added = !current_program_space->added_solibs.empty ();
4712
4713 if (!is_catchpoint)
4714 {
4715 if (any_added || any_deleted)
4716 current_uiout->text (_("Stopped due to shared library event:\n"));
4717 else
4718 current_uiout->text (_("Stopped due to shared library event (no "
4719 "libraries added or removed)\n"));
4720 }
4721
4722 if (current_uiout->is_mi_like_p ())
4723 current_uiout->field_string ("reason",
4724 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4725
4726 if (any_deleted)
4727 {
4728 current_uiout->text (_(" Inferior unloaded "));
4729 ui_out_emit_list list_emitter (current_uiout, "removed");
4730 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4731 {
4732 const std::string &name = current_program_space->deleted_solibs[ix];
4733
4734 if (ix > 0)
4735 current_uiout->text (" ");
4736 current_uiout->field_string ("library", name);
4737 current_uiout->text ("\n");
4738 }
4739 }
4740
4741 if (any_added)
4742 {
4743 current_uiout->text (_(" Inferior loaded "));
4744 ui_out_emit_list list_emitter (current_uiout, "added");
4745 bool first = true;
4746 for (so_list *iter : current_program_space->added_solibs)
4747 {
4748 if (!first)
4749 current_uiout->text (" ");
4750 first = false;
4751 current_uiout->field_string ("library", iter->so_name);
4752 current_uiout->text ("\n");
4753 }
4754 }
4755 }
4756
4757 /* Print a message indicating what happened. This is called from
4758 normal_stop(). The input to this routine is the head of the bpstat
4759 list - a list of the eventpoints that caused this stop. KIND is
4760 the target_waitkind for the stopping event. This
4761 routine calls the generic print routine for printing a message
4762 about reasons for stopping. This will print (for example) the
4763 "Breakpoint n," part of the output. The return value of this
4764 routine is one of:
4765
4766 PRINT_UNKNOWN: Means we printed nothing.
4767 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4768 code to print the location. An example is
4769 "Breakpoint 1, " which should be followed by
4770 the location.
4771 PRINT_SRC_ONLY: Means we printed something, but there is no need
4772 to also print the location part of the message.
4773 An example is the catch/throw messages, which
4774 don't require a location appended to the end.
4775 PRINT_NOTHING: We have done some printing and we don't need any
4776 further info to be printed. */
4777
4778 enum print_stop_action
4779 bpstat_print (bpstat bs, int kind)
4780 {
4781 enum print_stop_action val;
4782
4783 /* Maybe another breakpoint in the chain caused us to stop.
4784 (Currently all watchpoints go on the bpstat whether hit or not.
4785 That probably could (should) be changed, provided care is taken
4786 with respect to bpstat_explains_signal). */
4787 for (; bs; bs = bs->next)
4788 {
4789 val = print_bp_stop_message (bs);
4790 if (val == PRINT_SRC_ONLY
4791 || val == PRINT_SRC_AND_LOC
4792 || val == PRINT_NOTHING)
4793 return val;
4794 }
4795
4796 /* If we had hit a shared library event breakpoint,
4797 print_bp_stop_message would print out this message. If we hit an
4798 OS-level shared library event, do the same thing. */
4799 if (kind == TARGET_WAITKIND_LOADED)
4800 {
4801 print_solib_event (0);
4802 return PRINT_NOTHING;
4803 }
4804
4805 /* We reached the end of the chain, or we got a null BS to start
4806 with and nothing was printed. */
4807 return PRINT_UNKNOWN;
4808 }
4809
4810 /* Evaluate the boolean expression EXP and return the result. */
4811
4812 static bool
4813 breakpoint_cond_eval (expression *exp)
4814 {
4815 struct value *mark = value_mark ();
4816 bool res = value_true (evaluate_expression (exp));
4817
4818 value_free_to_mark (mark);
4819 return res;
4820 }
4821
4822 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4823
4824 bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4825 : next (NULL),
4826 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
4827 breakpoint_at (bl->owner),
4828 commands (NULL),
4829 print (0),
4830 stop (0),
4831 print_it (print_it_normal)
4832 {
4833 **bs_link_pointer = this;
4834 *bs_link_pointer = &next;
4835 }
4836
4837 bpstats::bpstats ()
4838 : next (NULL),
4839 breakpoint_at (NULL),
4840 commands (NULL),
4841 print (0),
4842 stop (0),
4843 print_it (print_it_normal)
4844 {
4845 }
4846 \f
4847 /* The target has stopped with waitstatus WS. Check if any hardware
4848 watchpoints have triggered, according to the target. */
4849
4850 int
4851 watchpoints_triggered (struct target_waitstatus *ws)
4852 {
4853 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
4854 CORE_ADDR addr;
4855 struct breakpoint *b;
4856
4857 if (!stopped_by_watchpoint)
4858 {
4859 /* We were not stopped by a watchpoint. Mark all watchpoints
4860 as not triggered. */
4861 ALL_BREAKPOINTS (b)
4862 if (is_hardware_watchpoint (b))
4863 {
4864 struct watchpoint *w = (struct watchpoint *) b;
4865
4866 w->watchpoint_triggered = watch_triggered_no;
4867 }
4868
4869 return 0;
4870 }
4871
4872 if (!target_stopped_data_address (current_top_target (), &addr))
4873 {
4874 /* We were stopped by a watchpoint, but we don't know where.
4875 Mark all watchpoints as unknown. */
4876 ALL_BREAKPOINTS (b)
4877 if (is_hardware_watchpoint (b))
4878 {
4879 struct watchpoint *w = (struct watchpoint *) b;
4880
4881 w->watchpoint_triggered = watch_triggered_unknown;
4882 }
4883
4884 return 1;
4885 }
4886
4887 /* The target could report the data address. Mark watchpoints
4888 affected by this data address as triggered, and all others as not
4889 triggered. */
4890
4891 ALL_BREAKPOINTS (b)
4892 if (is_hardware_watchpoint (b))
4893 {
4894 struct watchpoint *w = (struct watchpoint *) b;
4895 struct bp_location *loc;
4896
4897 w->watchpoint_triggered = watch_triggered_no;
4898 for (loc = b->loc; loc; loc = loc->next)
4899 {
4900 if (is_masked_watchpoint (b))
4901 {
4902 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4903 CORE_ADDR start = loc->address & w->hw_wp_mask;
4904
4905 if (newaddr == start)
4906 {
4907 w->watchpoint_triggered = watch_triggered_yes;
4908 break;
4909 }
4910 }
4911 /* Exact match not required. Within range is sufficient. */
4912 else if (target_watchpoint_addr_within_range (current_top_target (),
4913 addr, loc->address,
4914 loc->length))
4915 {
4916 w->watchpoint_triggered = watch_triggered_yes;
4917 break;
4918 }
4919 }
4920 }
4921
4922 return 1;
4923 }
4924
4925 /* Possible return values for watchpoint_check. */
4926 enum wp_check_result
4927 {
4928 /* The watchpoint has been deleted. */
4929 WP_DELETED = 1,
4930
4931 /* The value has changed. */
4932 WP_VALUE_CHANGED = 2,
4933
4934 /* The value has not changed. */
4935 WP_VALUE_NOT_CHANGED = 3,
4936
4937 /* Ignore this watchpoint, no matter if the value changed or not. */
4938 WP_IGNORE = 4,
4939 };
4940
4941 #define BP_TEMPFLAG 1
4942 #define BP_HARDWAREFLAG 2
4943
4944 /* Evaluate watchpoint condition expression and check if its value
4945 changed. */
4946
4947 static wp_check_result
4948 watchpoint_check (bpstat bs)
4949 {
4950 struct watchpoint *b;
4951 struct frame_info *fr;
4952 int within_current_scope;
4953
4954 /* BS is built from an existing struct breakpoint. */
4955 gdb_assert (bs->breakpoint_at != NULL);
4956 b = (struct watchpoint *) bs->breakpoint_at;
4957
4958 /* If this is a local watchpoint, we only want to check if the
4959 watchpoint frame is in scope if the current thread is the thread
4960 that was used to create the watchpoint. */
4961 if (!watchpoint_in_thread_scope (b))
4962 return WP_IGNORE;
4963
4964 if (b->exp_valid_block == NULL)
4965 within_current_scope = 1;
4966 else
4967 {
4968 struct frame_info *frame = get_current_frame ();
4969 struct gdbarch *frame_arch = get_frame_arch (frame);
4970 CORE_ADDR frame_pc = get_frame_pc (frame);
4971
4972 /* stack_frame_destroyed_p() returns a non-zero value if we're
4973 still in the function but the stack frame has already been
4974 invalidated. Since we can't rely on the values of local
4975 variables after the stack has been destroyed, we are treating
4976 the watchpoint in that state as `not changed' without further
4977 checking. Don't mark watchpoints as changed if the current
4978 frame is in an epilogue - even if they are in some other
4979 frame, our view of the stack is likely to be wrong and
4980 frame_find_by_id could error out. */
4981 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
4982 return WP_IGNORE;
4983
4984 fr = frame_find_by_id (b->watchpoint_frame);
4985 within_current_scope = (fr != NULL);
4986
4987 /* If we've gotten confused in the unwinder, we might have
4988 returned a frame that can't describe this variable. */
4989 if (within_current_scope)
4990 {
4991 struct symbol *function;
4992
4993 function = get_frame_function (fr);
4994 if (function == NULL
4995 || !contained_in (b->exp_valid_block,
4996 SYMBOL_BLOCK_VALUE (function)))
4997 within_current_scope = 0;
4998 }
4999
5000 if (within_current_scope)
5001 /* If we end up stopping, the current frame will get selected
5002 in normal_stop. So this call to select_frame won't affect
5003 the user. */
5004 select_frame (fr);
5005 }
5006
5007 if (within_current_scope)
5008 {
5009 /* We use value_{,free_to_}mark because it could be a *long*
5010 time before we return to the command level and call
5011 free_all_values. We can't call free_all_values because we
5012 might be in the middle of evaluating a function call. */
5013
5014 int pc = 0;
5015 struct value *mark;
5016 struct value *new_val;
5017
5018 if (is_masked_watchpoint (b))
5019 /* Since we don't know the exact trigger address (from
5020 stopped_data_address), just tell the user we've triggered
5021 a mask watchpoint. */
5022 return WP_VALUE_CHANGED;
5023
5024 mark = value_mark ();
5025 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, false);
5026
5027 if (b->val_bitsize != 0)
5028 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5029
5030 /* We use value_equal_contents instead of value_equal because
5031 the latter coerces an array to a pointer, thus comparing just
5032 the address of the array instead of its contents. This is
5033 not what we want. */
5034 if ((b->val != NULL) != (new_val != NULL)
5035 || (b->val != NULL && !value_equal_contents (b->val.get (),
5036 new_val)))
5037 {
5038 bs->old_val = b->val;
5039 b->val = release_value (new_val);
5040 b->val_valid = true;
5041 if (new_val != NULL)
5042 value_free_to_mark (mark);
5043 return WP_VALUE_CHANGED;
5044 }
5045 else
5046 {
5047 /* Nothing changed. */
5048 value_free_to_mark (mark);
5049 return WP_VALUE_NOT_CHANGED;
5050 }
5051 }
5052 else
5053 {
5054 /* This seems like the only logical thing to do because
5055 if we temporarily ignored the watchpoint, then when
5056 we reenter the block in which it is valid it contains
5057 garbage (in the case of a function, it may have two
5058 garbage values, one before and one after the prologue).
5059 So we can't even detect the first assignment to it and
5060 watch after that (since the garbage may or may not equal
5061 the first value assigned). */
5062 /* We print all the stop information in
5063 breakpoint_ops->print_it, but in this case, by the time we
5064 call breakpoint_ops->print_it this bp will be deleted
5065 already. So we have no choice but print the information
5066 here. */
5067
5068 SWITCH_THRU_ALL_UIS ()
5069 {
5070 struct ui_out *uiout = current_uiout;
5071
5072 if (uiout->is_mi_like_p ())
5073 uiout->field_string
5074 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5075 uiout->message ("\nWatchpoint %pF deleted because the program has "
5076 "left the block in\n"
5077 "which its expression is valid.\n",
5078 signed_field ("wpnum", b->number));
5079 }
5080
5081 /* Make sure the watchpoint's commands aren't executed. */
5082 b->commands = NULL;
5083 watchpoint_del_at_next_stop (b);
5084
5085 return WP_DELETED;
5086 }
5087 }
5088
5089 /* Return true if it looks like target has stopped due to hitting
5090 breakpoint location BL. This function does not check if we should
5091 stop, only if BL explains the stop. */
5092
5093 static int
5094 bpstat_check_location (const struct bp_location *bl,
5095 const address_space *aspace, CORE_ADDR bp_addr,
5096 const struct target_waitstatus *ws)
5097 {
5098 struct breakpoint *b = bl->owner;
5099
5100 /* BL is from an existing breakpoint. */
5101 gdb_assert (b != NULL);
5102
5103 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5104 }
5105
5106 /* Determine if the watched values have actually changed, and we
5107 should stop. If not, set BS->stop to 0. */
5108
5109 static void
5110 bpstat_check_watchpoint (bpstat bs)
5111 {
5112 const struct bp_location *bl;
5113 struct watchpoint *b;
5114
5115 /* BS is built for existing struct breakpoint. */
5116 bl = bs->bp_location_at.get ();
5117 gdb_assert (bl != NULL);
5118 b = (struct watchpoint *) bs->breakpoint_at;
5119 gdb_assert (b != NULL);
5120
5121 {
5122 int must_check_value = 0;
5123
5124 if (b->type == bp_watchpoint)
5125 /* For a software watchpoint, we must always check the
5126 watched value. */
5127 must_check_value = 1;
5128 else if (b->watchpoint_triggered == watch_triggered_yes)
5129 /* We have a hardware watchpoint (read, write, or access)
5130 and the target earlier reported an address watched by
5131 this watchpoint. */
5132 must_check_value = 1;
5133 else if (b->watchpoint_triggered == watch_triggered_unknown
5134 && b->type == bp_hardware_watchpoint)
5135 /* We were stopped by a hardware watchpoint, but the target could
5136 not report the data address. We must check the watchpoint's
5137 value. Access and read watchpoints are out of luck; without
5138 a data address, we can't figure it out. */
5139 must_check_value = 1;
5140
5141 if (must_check_value)
5142 {
5143 wp_check_result e;
5144
5145 try
5146 {
5147 e = watchpoint_check (bs);
5148 }
5149 catch (const gdb_exception &ex)
5150 {
5151 exception_fprintf (gdb_stderr, ex,
5152 "Error evaluating expression "
5153 "for watchpoint %d\n",
5154 b->number);
5155
5156 SWITCH_THRU_ALL_UIS ()
5157 {
5158 printf_filtered (_("Watchpoint %d deleted.\n"),
5159 b->number);
5160 }
5161 watchpoint_del_at_next_stop (b);
5162 e = WP_DELETED;
5163 }
5164
5165 switch (e)
5166 {
5167 case WP_DELETED:
5168 /* We've already printed what needs to be printed. */
5169 bs->print_it = print_it_done;
5170 /* Stop. */
5171 break;
5172 case WP_IGNORE:
5173 bs->print_it = print_it_noop;
5174 bs->stop = 0;
5175 break;
5176 case WP_VALUE_CHANGED:
5177 if (b->type == bp_read_watchpoint)
5178 {
5179 /* There are two cases to consider here:
5180
5181 1. We're watching the triggered memory for reads.
5182 In that case, trust the target, and always report
5183 the watchpoint hit to the user. Even though
5184 reads don't cause value changes, the value may
5185 have changed since the last time it was read, and
5186 since we're not trapping writes, we will not see
5187 those, and as such we should ignore our notion of
5188 old value.
5189
5190 2. We're watching the triggered memory for both
5191 reads and writes. There are two ways this may
5192 happen:
5193
5194 2.1. This is a target that can't break on data
5195 reads only, but can break on accesses (reads or
5196 writes), such as e.g., x86. We detect this case
5197 at the time we try to insert read watchpoints.
5198
5199 2.2. Otherwise, the target supports read
5200 watchpoints, but, the user set an access or write
5201 watchpoint watching the same memory as this read
5202 watchpoint.
5203
5204 If we're watching memory writes as well as reads,
5205 ignore watchpoint hits when we find that the
5206 value hasn't changed, as reads don't cause
5207 changes. This still gives false positives when
5208 the program writes the same value to memory as
5209 what there was already in memory (we will confuse
5210 it for a read), but it's much better than
5211 nothing. */
5212
5213 int other_write_watchpoint = 0;
5214
5215 if (bl->watchpoint_type == hw_read)
5216 {
5217 struct breakpoint *other_b;
5218
5219 ALL_BREAKPOINTS (other_b)
5220 if (other_b->type == bp_hardware_watchpoint
5221 || other_b->type == bp_access_watchpoint)
5222 {
5223 struct watchpoint *other_w =
5224 (struct watchpoint *) other_b;
5225
5226 if (other_w->watchpoint_triggered
5227 == watch_triggered_yes)
5228 {
5229 other_write_watchpoint = 1;
5230 break;
5231 }
5232 }
5233 }
5234
5235 if (other_write_watchpoint
5236 || bl->watchpoint_type == hw_access)
5237 {
5238 /* We're watching the same memory for writes,
5239 and the value changed since the last time we
5240 updated it, so this trap must be for a write.
5241 Ignore it. */
5242 bs->print_it = print_it_noop;
5243 bs->stop = 0;
5244 }
5245 }
5246 break;
5247 case WP_VALUE_NOT_CHANGED:
5248 if (b->type == bp_hardware_watchpoint
5249 || b->type == bp_watchpoint)
5250 {
5251 /* Don't stop: write watchpoints shouldn't fire if
5252 the value hasn't changed. */
5253 bs->print_it = print_it_noop;
5254 bs->stop = 0;
5255 }
5256 /* Stop. */
5257 break;
5258 default:
5259 /* Can't happen. */
5260 break;
5261 }
5262 }
5263 else /* must_check_value == 0 */
5264 {
5265 /* This is a case where some watchpoint(s) triggered, but
5266 not at the address of this watchpoint, or else no
5267 watchpoint triggered after all. So don't print
5268 anything for this watchpoint. */
5269 bs->print_it = print_it_noop;
5270 bs->stop = 0;
5271 }
5272 }
5273 }
5274
5275 /* For breakpoints that are currently marked as telling gdb to stop,
5276 check conditions (condition proper, frame, thread and ignore count)
5277 of breakpoint referred to by BS. If we should not stop for this
5278 breakpoint, set BS->stop to 0. */
5279
5280 static void
5281 bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
5282 {
5283 const struct bp_location *bl;
5284 struct breakpoint *b;
5285 /* Assume stop. */
5286 bool condition_result = true;
5287 struct expression *cond;
5288
5289 gdb_assert (bs->stop);
5290
5291 /* BS is built for existing struct breakpoint. */
5292 bl = bs->bp_location_at.get ();
5293 gdb_assert (bl != NULL);
5294 b = bs->breakpoint_at;
5295 gdb_assert (b != NULL);
5296
5297 /* Even if the target evaluated the condition on its end and notified GDB, we
5298 need to do so again since GDB does not know if we stopped due to a
5299 breakpoint or a single step breakpoint. */
5300
5301 if (frame_id_p (b->frame_id)
5302 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5303 {
5304 bs->stop = 0;
5305 return;
5306 }
5307
5308 /* If this is a thread/task-specific breakpoint, don't waste cpu
5309 evaluating the condition if this isn't the specified
5310 thread/task. */
5311 if ((b->thread != -1 && b->thread != thread->global_num)
5312 || (b->task != 0 && b->task != ada_get_task_number (thread)))
5313 {
5314 bs->stop = 0;
5315 return;
5316 }
5317
5318 /* Evaluate extension language breakpoints that have a "stop" method
5319 implemented. */
5320 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5321
5322 if (is_watchpoint (b))
5323 {
5324 struct watchpoint *w = (struct watchpoint *) b;
5325
5326 cond = w->cond_exp.get ();
5327 }
5328 else
5329 cond = bl->cond.get ();
5330
5331 if (cond && b->disposition != disp_del_at_next_stop)
5332 {
5333 int within_current_scope = 1;
5334 struct watchpoint * w;
5335
5336 /* We use value_mark and value_free_to_mark because it could
5337 be a long time before we return to the command level and
5338 call free_all_values. We can't call free_all_values
5339 because we might be in the middle of evaluating a
5340 function call. */
5341 struct value *mark = value_mark ();
5342
5343 if (is_watchpoint (b))
5344 w = (struct watchpoint *) b;
5345 else
5346 w = NULL;
5347
5348 /* Need to select the frame, with all that implies so that
5349 the conditions will have the right context. Because we
5350 use the frame, we will not see an inlined function's
5351 variables when we arrive at a breakpoint at the start
5352 of the inlined function; the current frame will be the
5353 call site. */
5354 if (w == NULL || w->cond_exp_valid_block == NULL)
5355 select_frame (get_current_frame ());
5356 else
5357 {
5358 struct frame_info *frame;
5359
5360 /* For local watchpoint expressions, which particular
5361 instance of a local is being watched matters, so we
5362 keep track of the frame to evaluate the expression
5363 in. To evaluate the condition however, it doesn't
5364 really matter which instantiation of the function
5365 where the condition makes sense triggers the
5366 watchpoint. This allows an expression like "watch
5367 global if q > 10" set in `func', catch writes to
5368 global on all threads that call `func', or catch
5369 writes on all recursive calls of `func' by a single
5370 thread. We simply always evaluate the condition in
5371 the innermost frame that's executing where it makes
5372 sense to evaluate the condition. It seems
5373 intuitive. */
5374 frame = block_innermost_frame (w->cond_exp_valid_block);
5375 if (frame != NULL)
5376 select_frame (frame);
5377 else
5378 within_current_scope = 0;
5379 }
5380 if (within_current_scope)
5381 {
5382 try
5383 {
5384 condition_result = breakpoint_cond_eval (cond);
5385 }
5386 catch (const gdb_exception &ex)
5387 {
5388 exception_fprintf (gdb_stderr, ex,
5389 "Error in testing breakpoint condition:\n");
5390 }
5391 }
5392 else
5393 {
5394 warning (_("Watchpoint condition cannot be tested "
5395 "in the current scope"));
5396 /* If we failed to set the right context for this
5397 watchpoint, unconditionally report it. */
5398 }
5399 /* FIXME-someday, should give breakpoint #. */
5400 value_free_to_mark (mark);
5401 }
5402
5403 if (cond && !condition_result)
5404 {
5405 bs->stop = 0;
5406 }
5407 else if (b->ignore_count > 0)
5408 {
5409 b->ignore_count--;
5410 bs->stop = 0;
5411 /* Increase the hit count even though we don't stop. */
5412 ++(b->hit_count);
5413 gdb::observers::breakpoint_modified.notify (b);
5414 }
5415 }
5416
5417 /* Returns true if we need to track moribund locations of LOC's type
5418 on the current target. */
5419
5420 static int
5421 need_moribund_for_location_type (struct bp_location *loc)
5422 {
5423 return ((loc->loc_type == bp_loc_software_breakpoint
5424 && !target_supports_stopped_by_sw_breakpoint ())
5425 || (loc->loc_type == bp_loc_hardware_breakpoint
5426 && !target_supports_stopped_by_hw_breakpoint ()));
5427 }
5428
5429 /* See breakpoint.h. */
5430
5431 bpstat
5432 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5433 const struct target_waitstatus *ws)
5434 {
5435 struct breakpoint *b;
5436 bpstat bs_head = NULL, *bs_link = &bs_head;
5437
5438 ALL_BREAKPOINTS (b)
5439 {
5440 if (!breakpoint_enabled (b))
5441 continue;
5442
5443 for (bp_location *bl = b->loc; bl != NULL; bl = bl->next)
5444 {
5445 /* For hardware watchpoints, we look only at the first
5446 location. The watchpoint_check function will work on the
5447 entire expression, not the individual locations. For
5448 read watchpoints, the watchpoints_triggered function has
5449 checked all locations already. */
5450 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5451 break;
5452
5453 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
5454 continue;
5455
5456 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5457 continue;
5458
5459 /* Come here if it's a watchpoint, or if the break address
5460 matches. */
5461
5462 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5463 explain stop. */
5464
5465 /* Assume we stop. Should we find a watchpoint that is not
5466 actually triggered, or if the condition of the breakpoint
5467 evaluates as false, we'll reset 'stop' to 0. */
5468 bs->stop = 1;
5469 bs->print = 1;
5470
5471 /* If this is a scope breakpoint, mark the associated
5472 watchpoint as triggered so that we will handle the
5473 out-of-scope event. We'll get to the watchpoint next
5474 iteration. */
5475 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5476 {
5477 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5478
5479 w->watchpoint_triggered = watch_triggered_yes;
5480 }
5481 }
5482 }
5483
5484 /* Check if a moribund breakpoint explains the stop. */
5485 if (!target_supports_stopped_by_sw_breakpoint ()
5486 || !target_supports_stopped_by_hw_breakpoint ())
5487 {
5488 for (bp_location *loc : moribund_locations)
5489 {
5490 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5491 && need_moribund_for_location_type (loc))
5492 {
5493 bpstat bs = new bpstats (loc, &bs_link);
5494 /* For hits of moribund locations, we should just proceed. */
5495 bs->stop = 0;
5496 bs->print = 0;
5497 bs->print_it = print_it_noop;
5498 }
5499 }
5500 }
5501
5502 return bs_head;
5503 }
5504
5505 /* See breakpoint.h. */
5506
5507 bpstat
5508 bpstat_stop_status (const address_space *aspace,
5509 CORE_ADDR bp_addr, thread_info *thread,
5510 const struct target_waitstatus *ws,
5511 bpstat stop_chain)
5512 {
5513 struct breakpoint *b = NULL;
5514 /* First item of allocated bpstat's. */
5515 bpstat bs_head = stop_chain;
5516 bpstat bs;
5517 int need_remove_insert;
5518 int removed_any;
5519
5520 /* First, build the bpstat chain with locations that explain a
5521 target stop, while being careful to not set the target running,
5522 as that may invalidate locations (in particular watchpoint
5523 locations are recreated). Resuming will happen here with
5524 breakpoint conditions or watchpoint expressions that include
5525 inferior function calls. */
5526 if (bs_head == NULL)
5527 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5528
5529 /* A bit of special processing for shlib breakpoints. We need to
5530 process solib loading here, so that the lists of loaded and
5531 unloaded libraries are correct before we handle "catch load" and
5532 "catch unload". */
5533 for (bs = bs_head; bs != NULL; bs = bs->next)
5534 {
5535 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5536 {
5537 handle_solib_event ();
5538 break;
5539 }
5540 }
5541
5542 /* Now go through the locations that caused the target to stop, and
5543 check whether we're interested in reporting this stop to higher
5544 layers, or whether we should resume the target transparently. */
5545
5546 removed_any = 0;
5547
5548 for (bs = bs_head; bs != NULL; bs = bs->next)
5549 {
5550 if (!bs->stop)
5551 continue;
5552
5553 b = bs->breakpoint_at;
5554 b->ops->check_status (bs);
5555 if (bs->stop)
5556 {
5557 bpstat_check_breakpoint_conditions (bs, thread);
5558
5559 if (bs->stop)
5560 {
5561 ++(b->hit_count);
5562 gdb::observers::breakpoint_modified.notify (b);
5563
5564 /* We will stop here. */
5565 if (b->disposition == disp_disable)
5566 {
5567 --(b->enable_count);
5568 if (b->enable_count <= 0)
5569 b->enable_state = bp_disabled;
5570 removed_any = 1;
5571 }
5572 if (b->silent)
5573 bs->print = 0;
5574 bs->commands = b->commands;
5575 if (command_line_is_silent (bs->commands
5576 ? bs->commands.get () : NULL))
5577 bs->print = 0;
5578
5579 b->ops->after_condition_true (bs);
5580 }
5581
5582 }
5583
5584 /* Print nothing for this entry if we don't stop or don't
5585 print. */
5586 if (!bs->stop || !bs->print)
5587 bs->print_it = print_it_noop;
5588 }
5589
5590 /* If we aren't stopping, the value of some hardware watchpoint may
5591 not have changed, but the intermediate memory locations we are
5592 watching may have. Don't bother if we're stopping; this will get
5593 done later. */
5594 need_remove_insert = 0;
5595 if (! bpstat_causes_stop (bs_head))
5596 for (bs = bs_head; bs != NULL; bs = bs->next)
5597 if (!bs->stop
5598 && bs->breakpoint_at
5599 && is_hardware_watchpoint (bs->breakpoint_at))
5600 {
5601 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5602
5603 update_watchpoint (w, 0 /* don't reparse. */);
5604 need_remove_insert = 1;
5605 }
5606
5607 if (need_remove_insert)
5608 update_global_location_list (UGLL_MAY_INSERT);
5609 else if (removed_any)
5610 update_global_location_list (UGLL_DONT_INSERT);
5611
5612 return bs_head;
5613 }
5614
5615 static void
5616 handle_jit_event (CORE_ADDR address)
5617 {
5618 struct gdbarch *gdbarch;
5619
5620 infrun_debug_printf ("handling bp_jit_event");
5621
5622 /* Switch terminal for any messages produced by
5623 breakpoint_re_set. */
5624 target_terminal::ours_for_output ();
5625
5626 gdbarch = get_frame_arch (get_current_frame ());
5627 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5628 thus it is expected that its objectfile can be found through
5629 minimal symbol lookup. If it doesn't work (and assert fails), it
5630 most likely means that `jit_breakpoint_re_set` was changes and this
5631 function needs to be updated too. */
5632 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5633 gdb_assert (jit_bp_sym.objfile != nullptr);
5634 jit_event_handler (gdbarch, jit_bp_sym.objfile);
5635
5636 target_terminal::inferior ();
5637 }
5638
5639 /* Prepare WHAT final decision for infrun. */
5640
5641 /* Decide what infrun needs to do with this bpstat. */
5642
5643 struct bpstat_what
5644 bpstat_what (bpstat bs_head)
5645 {
5646 struct bpstat_what retval;
5647 bpstat bs;
5648
5649 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5650 retval.call_dummy = STOP_NONE;
5651 retval.is_longjmp = false;
5652
5653 for (bs = bs_head; bs != NULL; bs = bs->next)
5654 {
5655 /* Extract this BS's action. After processing each BS, we check
5656 if its action overrides all we've seem so far. */
5657 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5658 enum bptype bptype;
5659
5660 if (bs->breakpoint_at == NULL)
5661 {
5662 /* I suspect this can happen if it was a momentary
5663 breakpoint which has since been deleted. */
5664 bptype = bp_none;
5665 }
5666 else
5667 bptype = bs->breakpoint_at->type;
5668
5669 switch (bptype)
5670 {
5671 case bp_none:
5672 break;
5673 case bp_breakpoint:
5674 case bp_hardware_breakpoint:
5675 case bp_single_step:
5676 case bp_until:
5677 case bp_finish:
5678 case bp_shlib_event:
5679 if (bs->stop)
5680 {
5681 if (bs->print)
5682 this_action = BPSTAT_WHAT_STOP_NOISY;
5683 else
5684 this_action = BPSTAT_WHAT_STOP_SILENT;
5685 }
5686 else
5687 this_action = BPSTAT_WHAT_SINGLE;
5688 break;
5689 case bp_watchpoint:
5690 case bp_hardware_watchpoint:
5691 case bp_read_watchpoint:
5692 case bp_access_watchpoint:
5693 if (bs->stop)
5694 {
5695 if (bs->print)
5696 this_action = BPSTAT_WHAT_STOP_NOISY;
5697 else
5698 this_action = BPSTAT_WHAT_STOP_SILENT;
5699 }
5700 else
5701 {
5702 /* There was a watchpoint, but we're not stopping.
5703 This requires no further action. */
5704 }
5705 break;
5706 case bp_longjmp:
5707 case bp_longjmp_call_dummy:
5708 case bp_exception:
5709 if (bs->stop)
5710 {
5711 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5712 retval.is_longjmp = bptype != bp_exception;
5713 }
5714 else
5715 this_action = BPSTAT_WHAT_SINGLE;
5716 break;
5717 case bp_longjmp_resume:
5718 case bp_exception_resume:
5719 if (bs->stop)
5720 {
5721 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5722 retval.is_longjmp = bptype == bp_longjmp_resume;
5723 }
5724 else
5725 this_action = BPSTAT_WHAT_SINGLE;
5726 break;
5727 case bp_step_resume:
5728 if (bs->stop)
5729 this_action = BPSTAT_WHAT_STEP_RESUME;
5730 else
5731 {
5732 /* It is for the wrong frame. */
5733 this_action = BPSTAT_WHAT_SINGLE;
5734 }
5735 break;
5736 case bp_hp_step_resume:
5737 if (bs->stop)
5738 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5739 else
5740 {
5741 /* It is for the wrong frame. */
5742 this_action = BPSTAT_WHAT_SINGLE;
5743 }
5744 break;
5745 case bp_watchpoint_scope:
5746 case bp_thread_event:
5747 case bp_overlay_event:
5748 case bp_longjmp_master:
5749 case bp_std_terminate_master:
5750 case bp_exception_master:
5751 this_action = BPSTAT_WHAT_SINGLE;
5752 break;
5753 case bp_catchpoint:
5754 if (bs->stop)
5755 {
5756 if (bs->print)
5757 this_action = BPSTAT_WHAT_STOP_NOISY;
5758 else
5759 this_action = BPSTAT_WHAT_STOP_SILENT;
5760 }
5761 else
5762 {
5763 /* Some catchpoints are implemented with breakpoints.
5764 For those, we need to step over the breakpoint. */
5765 if (bs->bp_location_at->loc_type != bp_loc_other)
5766 this_action = BPSTAT_WHAT_SINGLE;
5767 }
5768 break;
5769 case bp_jit_event:
5770 this_action = BPSTAT_WHAT_SINGLE;
5771 break;
5772 case bp_call_dummy:
5773 /* Make sure the action is stop (silent or noisy),
5774 so infrun.c pops the dummy frame. */
5775 retval.call_dummy = STOP_STACK_DUMMY;
5776 this_action = BPSTAT_WHAT_STOP_SILENT;
5777 break;
5778 case bp_std_terminate:
5779 /* Make sure the action is stop (silent or noisy),
5780 so infrun.c pops the dummy frame. */
5781 retval.call_dummy = STOP_STD_TERMINATE;
5782 this_action = BPSTAT_WHAT_STOP_SILENT;
5783 break;
5784 case bp_tracepoint:
5785 case bp_fast_tracepoint:
5786 case bp_static_tracepoint:
5787 /* Tracepoint hits should not be reported back to GDB, and
5788 if one got through somehow, it should have been filtered
5789 out already. */
5790 internal_error (__FILE__, __LINE__,
5791 _("bpstat_what: tracepoint encountered"));
5792 break;
5793 case bp_gnu_ifunc_resolver:
5794 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5795 this_action = BPSTAT_WHAT_SINGLE;
5796 break;
5797 case bp_gnu_ifunc_resolver_return:
5798 /* The breakpoint will be removed, execution will restart from the
5799 PC of the former breakpoint. */
5800 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5801 break;
5802
5803 case bp_dprintf:
5804 if (bs->stop)
5805 this_action = BPSTAT_WHAT_STOP_SILENT;
5806 else
5807 this_action = BPSTAT_WHAT_SINGLE;
5808 break;
5809
5810 default:
5811 internal_error (__FILE__, __LINE__,
5812 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5813 }
5814
5815 retval.main_action = std::max (retval.main_action, this_action);
5816 }
5817
5818 return retval;
5819 }
5820
5821 void
5822 bpstat_run_callbacks (bpstat bs_head)
5823 {
5824 bpstat bs;
5825
5826 for (bs = bs_head; bs != NULL; bs = bs->next)
5827 {
5828 struct breakpoint *b = bs->breakpoint_at;
5829
5830 if (b == NULL)
5831 continue;
5832 switch (b->type)
5833 {
5834 case bp_jit_event:
5835 handle_jit_event (bs->bp_location_at->address);
5836 break;
5837 case bp_gnu_ifunc_resolver:
5838 gnu_ifunc_resolver_stop (b);
5839 break;
5840 case bp_gnu_ifunc_resolver_return:
5841 gnu_ifunc_resolver_return_stop (b);
5842 break;
5843 }
5844 }
5845 }
5846
5847 /* See breakpoint.h. */
5848
5849 bool
5850 bpstat_should_step ()
5851 {
5852 struct breakpoint *b;
5853
5854 ALL_BREAKPOINTS (b)
5855 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5856 return true;
5857 return false;
5858 }
5859
5860 /* See breakpoint.h. */
5861
5862 bool
5863 bpstat_causes_stop (bpstat bs)
5864 {
5865 for (; bs != NULL; bs = bs->next)
5866 if (bs->stop)
5867 return true;
5868
5869 return false;
5870 }
5871
5872 \f
5873
5874 /* Compute a string of spaces suitable to indent the next line
5875 so it starts at the position corresponding to the table column
5876 named COL_NAME in the currently active table of UIOUT. */
5877
5878 static char *
5879 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5880 {
5881 static char wrap_indent[80];
5882 int i, total_width, width, align;
5883 const char *text;
5884
5885 total_width = 0;
5886 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5887 {
5888 if (strcmp (text, col_name) == 0)
5889 {
5890 gdb_assert (total_width < sizeof wrap_indent);
5891 memset (wrap_indent, ' ', total_width);
5892 wrap_indent[total_width] = 0;
5893
5894 return wrap_indent;
5895 }
5896
5897 total_width += width + 1;
5898 }
5899
5900 return NULL;
5901 }
5902
5903 /* Determine if the locations of this breakpoint will have their conditions
5904 evaluated by the target, host or a mix of both. Returns the following:
5905
5906 "host": Host evals condition.
5907 "host or target": Host or Target evals condition.
5908 "target": Target evals condition.
5909 */
5910
5911 static const char *
5912 bp_condition_evaluator (struct breakpoint *b)
5913 {
5914 struct bp_location *bl;
5915 char host_evals = 0;
5916 char target_evals = 0;
5917
5918 if (!b)
5919 return NULL;
5920
5921 if (!is_breakpoint (b))
5922 return NULL;
5923
5924 if (gdb_evaluates_breakpoint_condition_p ()
5925 || !target_supports_evaluation_of_breakpoint_conditions ())
5926 return condition_evaluation_host;
5927
5928 for (bl = b->loc; bl; bl = bl->next)
5929 {
5930 if (bl->cond_bytecode)
5931 target_evals++;
5932 else
5933 host_evals++;
5934 }
5935
5936 if (host_evals && target_evals)
5937 return condition_evaluation_both;
5938 else if (target_evals)
5939 return condition_evaluation_target;
5940 else
5941 return condition_evaluation_host;
5942 }
5943
5944 /* Determine the breakpoint location's condition evaluator. This is
5945 similar to bp_condition_evaluator, but for locations. */
5946
5947 static const char *
5948 bp_location_condition_evaluator (struct bp_location *bl)
5949 {
5950 if (bl && !is_breakpoint (bl->owner))
5951 return NULL;
5952
5953 if (gdb_evaluates_breakpoint_condition_p ()
5954 || !target_supports_evaluation_of_breakpoint_conditions ())
5955 return condition_evaluation_host;
5956
5957 if (bl && bl->cond_bytecode)
5958 return condition_evaluation_target;
5959 else
5960 return condition_evaluation_host;
5961 }
5962
5963 /* Print the LOC location out of the list of B->LOC locations. */
5964
5965 static void
5966 print_breakpoint_location (struct breakpoint *b,
5967 struct bp_location *loc)
5968 {
5969 struct ui_out *uiout = current_uiout;
5970
5971 scoped_restore_current_program_space restore_pspace;
5972
5973 if (loc != NULL && loc->shlib_disabled)
5974 loc = NULL;
5975
5976 if (loc != NULL)
5977 set_current_program_space (loc->pspace);
5978
5979 if (b->display_canonical)
5980 uiout->field_string ("what", event_location_to_string (b->location.get ()));
5981 else if (loc && loc->symtab)
5982 {
5983 const struct symbol *sym = loc->symbol;
5984
5985 if (sym)
5986 {
5987 uiout->text ("in ");
5988 uiout->field_string ("func", sym->print_name (),
5989 function_name_style.style ());
5990 uiout->text (" ");
5991 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5992 uiout->text ("at ");
5993 }
5994 uiout->field_string ("file",
5995 symtab_to_filename_for_display (loc->symtab),
5996 file_name_style.style ());
5997 uiout->text (":");
5998
5999 if (uiout->is_mi_like_p ())
6000 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
6001
6002 uiout->field_signed ("line", loc->line_number);
6003 }
6004 else if (loc)
6005 {
6006 string_file stb;
6007
6008 print_address_symbolic (loc->gdbarch, loc->address, &stb,
6009 demangle, "");
6010 uiout->field_stream ("at", stb);
6011 }
6012 else
6013 {
6014 uiout->field_string ("pending",
6015 event_location_to_string (b->location.get ()));
6016 /* If extra_string is available, it could be holding a condition
6017 or dprintf arguments. In either case, make sure it is printed,
6018 too, but only for non-MI streams. */
6019 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
6020 {
6021 if (b->type == bp_dprintf)
6022 uiout->text (",");
6023 else
6024 uiout->text (" ");
6025 uiout->text (b->extra_string);
6026 }
6027 }
6028
6029 if (loc && is_breakpoint (b)
6030 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6031 && bp_condition_evaluator (b) == condition_evaluation_both)
6032 {
6033 uiout->text (" (");
6034 uiout->field_string ("evaluated-by",
6035 bp_location_condition_evaluator (loc));
6036 uiout->text (")");
6037 }
6038 }
6039
6040 static const char *
6041 bptype_string (enum bptype type)
6042 {
6043 struct ep_type_description
6044 {
6045 enum bptype type;
6046 const char *description;
6047 };
6048 static struct ep_type_description bptypes[] =
6049 {
6050 {bp_none, "?deleted?"},
6051 {bp_breakpoint, "breakpoint"},
6052 {bp_hardware_breakpoint, "hw breakpoint"},
6053 {bp_single_step, "sw single-step"},
6054 {bp_until, "until"},
6055 {bp_finish, "finish"},
6056 {bp_watchpoint, "watchpoint"},
6057 {bp_hardware_watchpoint, "hw watchpoint"},
6058 {bp_read_watchpoint, "read watchpoint"},
6059 {bp_access_watchpoint, "acc watchpoint"},
6060 {bp_longjmp, "longjmp"},
6061 {bp_longjmp_resume, "longjmp resume"},
6062 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6063 {bp_exception, "exception"},
6064 {bp_exception_resume, "exception resume"},
6065 {bp_step_resume, "step resume"},
6066 {bp_hp_step_resume, "high-priority step resume"},
6067 {bp_watchpoint_scope, "watchpoint scope"},
6068 {bp_call_dummy, "call dummy"},
6069 {bp_std_terminate, "std::terminate"},
6070 {bp_shlib_event, "shlib events"},
6071 {bp_thread_event, "thread events"},
6072 {bp_overlay_event, "overlay events"},
6073 {bp_longjmp_master, "longjmp master"},
6074 {bp_std_terminate_master, "std::terminate master"},
6075 {bp_exception_master, "exception master"},
6076 {bp_catchpoint, "catchpoint"},
6077 {bp_tracepoint, "tracepoint"},
6078 {bp_fast_tracepoint, "fast tracepoint"},
6079 {bp_static_tracepoint, "static tracepoint"},
6080 {bp_dprintf, "dprintf"},
6081 {bp_jit_event, "jit events"},
6082 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6083 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6084 };
6085
6086 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6087 || ((int) type != bptypes[(int) type].type))
6088 internal_error (__FILE__, __LINE__,
6089 _("bptypes table does not describe type #%d."),
6090 (int) type);
6091
6092 return bptypes[(int) type].description;
6093 }
6094
6095 /* For MI, output a field named 'thread-groups' with a list as the value.
6096 For CLI, prefix the list with the string 'inf'. */
6097
6098 static void
6099 output_thread_groups (struct ui_out *uiout,
6100 const char *field_name,
6101 const std::vector<int> &inf_nums,
6102 int mi_only)
6103 {
6104 int is_mi = uiout->is_mi_like_p ();
6105
6106 /* For backward compatibility, don't display inferiors in CLI unless
6107 there are several. Always display them for MI. */
6108 if (!is_mi && mi_only)
6109 return;
6110
6111 ui_out_emit_list list_emitter (uiout, field_name);
6112
6113 for (size_t i = 0; i < inf_nums.size (); i++)
6114 {
6115 if (is_mi)
6116 {
6117 char mi_group[10];
6118
6119 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
6120 uiout->field_string (NULL, mi_group);
6121 }
6122 else
6123 {
6124 if (i == 0)
6125 uiout->text (" inf ");
6126 else
6127 uiout->text (", ");
6128
6129 uiout->text (plongest (inf_nums[i]));
6130 }
6131 }
6132 }
6133
6134 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6135 instead of going via breakpoint_ops::print_one. This makes "maint
6136 info breakpoints" show the software breakpoint locations of
6137 catchpoints, which are considered internal implementation
6138 detail. */
6139
6140 static void
6141 print_one_breakpoint_location (struct breakpoint *b,
6142 struct bp_location *loc,
6143 int loc_number,
6144 struct bp_location **last_loc,
6145 int allflag, bool raw_loc)
6146 {
6147 struct command_line *l;
6148 static char bpenables[] = "nynny";
6149
6150 struct ui_out *uiout = current_uiout;
6151 int header_of_multiple = 0;
6152 int part_of_multiple = (loc != NULL);
6153 struct value_print_options opts;
6154
6155 get_user_print_options (&opts);
6156
6157 gdb_assert (!loc || loc_number != 0);
6158 /* See comment in print_one_breakpoint concerning treatment of
6159 breakpoints with single disabled location. */
6160 if (loc == NULL
6161 && (b->loc != NULL
6162 && (b->loc->next != NULL
6163 || !b->loc->enabled || b->loc->disabled_by_cond)))
6164 header_of_multiple = 1;
6165 if (loc == NULL)
6166 loc = b->loc;
6167
6168 annotate_record ();
6169
6170 /* 1 */
6171 annotate_field (0);
6172 if (part_of_multiple)
6173 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6174 else
6175 uiout->field_signed ("number", b->number);
6176
6177 /* 2 */
6178 annotate_field (1);
6179 if (part_of_multiple)
6180 uiout->field_skip ("type");
6181 else
6182 uiout->field_string ("type", bptype_string (b->type));
6183
6184 /* 3 */
6185 annotate_field (2);
6186 if (part_of_multiple)
6187 uiout->field_skip ("disp");
6188 else
6189 uiout->field_string ("disp", bpdisp_text (b->disposition));
6190
6191 /* 4 */
6192 annotate_field (3);
6193 if (part_of_multiple)
6194 uiout->field_string ("enabled", (loc->disabled_by_cond ? "N*"
6195 : (loc->enabled ? "y" : "n")));
6196 else
6197 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6198
6199 /* 5 and 6 */
6200 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
6201 b->ops->print_one (b, last_loc);
6202 else
6203 {
6204 if (is_watchpoint (b))
6205 {
6206 struct watchpoint *w = (struct watchpoint *) b;
6207
6208 /* Field 4, the address, is omitted (which makes the columns
6209 not line up too nicely with the headers, but the effect
6210 is relatively readable). */
6211 if (opts.addressprint)
6212 uiout->field_skip ("addr");
6213 annotate_field (5);
6214 uiout->field_string ("what", w->exp_string);
6215 }
6216 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6217 || is_ada_exception_catchpoint (b))
6218 {
6219 if (opts.addressprint)
6220 {
6221 annotate_field (4);
6222 if (header_of_multiple)
6223 uiout->field_string ("addr", "<MULTIPLE>",
6224 metadata_style.style ());
6225 else if (b->loc == NULL || loc->shlib_disabled)
6226 uiout->field_string ("addr", "<PENDING>",
6227 metadata_style.style ());
6228 else
6229 uiout->field_core_addr ("addr",
6230 loc->gdbarch, loc->address);
6231 }
6232 annotate_field (5);
6233 if (!header_of_multiple)
6234 print_breakpoint_location (b, loc);
6235 if (b->loc)
6236 *last_loc = b->loc;
6237 }
6238 }
6239
6240 if (loc != NULL && !header_of_multiple)
6241 {
6242 std::vector<int> inf_nums;
6243 int mi_only = 1;
6244
6245 for (inferior *inf : all_inferiors ())
6246 {
6247 if (inf->pspace == loc->pspace)
6248 inf_nums.push_back (inf->num);
6249 }
6250
6251 /* For backward compatibility, don't display inferiors in CLI unless
6252 there are several. Always display for MI. */
6253 if (allflag
6254 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6255 && (program_spaces.size () > 1
6256 || number_of_inferiors () > 1)
6257 /* LOC is for existing B, it cannot be in
6258 moribund_locations and thus having NULL OWNER. */
6259 && loc->owner->type != bp_catchpoint))
6260 mi_only = 0;
6261 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6262 }
6263
6264 if (!part_of_multiple)
6265 {
6266 if (b->thread != -1)
6267 {
6268 /* FIXME: This seems to be redundant and lost here; see the
6269 "stop only in" line a little further down. */
6270 uiout->text (" thread ");
6271 uiout->field_signed ("thread", b->thread);
6272 }
6273 else if (b->task != 0)
6274 {
6275 uiout->text (" task ");
6276 uiout->field_signed ("task", b->task);
6277 }
6278 }
6279
6280 uiout->text ("\n");
6281
6282 if (!part_of_multiple)
6283 b->ops->print_one_detail (b, uiout);
6284
6285 if (part_of_multiple && frame_id_p (b->frame_id))
6286 {
6287 annotate_field (6);
6288 uiout->text ("\tstop only in stack frame at ");
6289 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6290 the frame ID. */
6291 uiout->field_core_addr ("frame",
6292 b->gdbarch, b->frame_id.stack_addr);
6293 uiout->text ("\n");
6294 }
6295
6296 if (!part_of_multiple && b->cond_string)
6297 {
6298 annotate_field (7);
6299 if (is_tracepoint (b))
6300 uiout->text ("\ttrace only if ");
6301 else
6302 uiout->text ("\tstop only if ");
6303 uiout->field_string ("cond", b->cond_string);
6304
6305 /* Print whether the target is doing the breakpoint's condition
6306 evaluation. If GDB is doing the evaluation, don't print anything. */
6307 if (is_breakpoint (b)
6308 && breakpoint_condition_evaluation_mode ()
6309 == condition_evaluation_target)
6310 {
6311 uiout->message (" (%pF evals)",
6312 string_field ("evaluated-by",
6313 bp_condition_evaluator (b)));
6314 }
6315 uiout->text ("\n");
6316 }
6317
6318 if (!part_of_multiple && b->thread != -1)
6319 {
6320 /* FIXME should make an annotation for this. */
6321 uiout->text ("\tstop only in thread ");
6322 if (uiout->is_mi_like_p ())
6323 uiout->field_signed ("thread", b->thread);
6324 else
6325 {
6326 struct thread_info *thr = find_thread_global_id (b->thread);
6327
6328 uiout->field_string ("thread", print_thread_id (thr));
6329 }
6330 uiout->text ("\n");
6331 }
6332
6333 if (!part_of_multiple)
6334 {
6335 if (b->hit_count)
6336 {
6337 /* FIXME should make an annotation for this. */
6338 if (is_catchpoint (b))
6339 uiout->text ("\tcatchpoint");
6340 else if (is_tracepoint (b))
6341 uiout->text ("\ttracepoint");
6342 else
6343 uiout->text ("\tbreakpoint");
6344 uiout->text (" already hit ");
6345 uiout->field_signed ("times", b->hit_count);
6346 if (b->hit_count == 1)
6347 uiout->text (" time\n");
6348 else
6349 uiout->text (" times\n");
6350 }
6351 else
6352 {
6353 /* Output the count also if it is zero, but only if this is mi. */
6354 if (uiout->is_mi_like_p ())
6355 uiout->field_signed ("times", b->hit_count);
6356 }
6357 }
6358
6359 if (!part_of_multiple && b->ignore_count)
6360 {
6361 annotate_field (8);
6362 uiout->message ("\tignore next %pF hits\n",
6363 signed_field ("ignore", b->ignore_count));
6364 }
6365
6366 /* Note that an enable count of 1 corresponds to "enable once"
6367 behavior, which is reported by the combination of enablement and
6368 disposition, so we don't need to mention it here. */
6369 if (!part_of_multiple && b->enable_count > 1)
6370 {
6371 annotate_field (8);
6372 uiout->text ("\tdisable after ");
6373 /* Tweak the wording to clarify that ignore and enable counts
6374 are distinct, and have additive effect. */
6375 if (b->ignore_count)
6376 uiout->text ("additional ");
6377 else
6378 uiout->text ("next ");
6379 uiout->field_signed ("enable", b->enable_count);
6380 uiout->text (" hits\n");
6381 }
6382
6383 if (!part_of_multiple && is_tracepoint (b))
6384 {
6385 struct tracepoint *tp = (struct tracepoint *) b;
6386
6387 if (tp->traceframe_usage)
6388 {
6389 uiout->text ("\ttrace buffer usage ");
6390 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6391 uiout->text (" bytes\n");
6392 }
6393 }
6394
6395 l = b->commands ? b->commands.get () : NULL;
6396 if (!part_of_multiple && l)
6397 {
6398 annotate_field (9);
6399 ui_out_emit_tuple tuple_emitter (uiout, "script");
6400 print_command_lines (uiout, l, 4);
6401 }
6402
6403 if (is_tracepoint (b))
6404 {
6405 struct tracepoint *t = (struct tracepoint *) b;
6406
6407 if (!part_of_multiple && t->pass_count)
6408 {
6409 annotate_field (10);
6410 uiout->text ("\tpass count ");
6411 uiout->field_signed ("pass", t->pass_count);
6412 uiout->text (" \n");
6413 }
6414
6415 /* Don't display it when tracepoint or tracepoint location is
6416 pending. */
6417 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6418 {
6419 annotate_field (11);
6420
6421 if (uiout->is_mi_like_p ())
6422 uiout->field_string ("installed",
6423 loc->inserted ? "y" : "n");
6424 else
6425 {
6426 if (loc->inserted)
6427 uiout->text ("\t");
6428 else
6429 uiout->text ("\tnot ");
6430 uiout->text ("installed on target\n");
6431 }
6432 }
6433 }
6434
6435 if (uiout->is_mi_like_p () && !part_of_multiple)
6436 {
6437 if (is_watchpoint (b))
6438 {
6439 struct watchpoint *w = (struct watchpoint *) b;
6440
6441 uiout->field_string ("original-location", w->exp_string);
6442 }
6443 else if (b->location != NULL
6444 && event_location_to_string (b->location.get ()) != NULL)
6445 uiout->field_string ("original-location",
6446 event_location_to_string (b->location.get ()));
6447 }
6448 }
6449
6450 /* See breakpoint.h. */
6451
6452 bool fix_multi_location_breakpoint_output_globally = false;
6453
6454 static void
6455 print_one_breakpoint (struct breakpoint *b,
6456 struct bp_location **last_loc,
6457 int allflag)
6458 {
6459 struct ui_out *uiout = current_uiout;
6460 bool use_fixed_output
6461 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6462 || fix_multi_location_breakpoint_output_globally);
6463
6464 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6465 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
6466
6467 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6468 are outside. */
6469 if (!use_fixed_output)
6470 bkpt_tuple_emitter.reset ();
6471
6472 /* If this breakpoint has custom print function,
6473 it's already printed. Otherwise, print individual
6474 locations, if any. */
6475 if (b->ops == NULL
6476 || b->ops->print_one == NULL
6477 || allflag)
6478 {
6479 /* If breakpoint has a single location that is disabled, we
6480 print it as if it had several locations, since otherwise it's
6481 hard to represent "breakpoint enabled, location disabled"
6482 situation.
6483
6484 Note that while hardware watchpoints have several locations
6485 internally, that's not a property exposed to users.
6486
6487 Likewise, while catchpoints may be implemented with
6488 breakpoints (e.g., catch throw), that's not a property
6489 exposed to users. We do however display the internal
6490 breakpoint locations with "maint info breakpoints". */
6491 if (!is_hardware_watchpoint (b)
6492 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6493 || is_ada_exception_catchpoint (b))
6494 && (allflag
6495 || (b->loc && (b->loc->next
6496 || !b->loc->enabled
6497 || b->loc->disabled_by_cond))))
6498 {
6499 gdb::optional<ui_out_emit_list> locations_list;
6500
6501 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6502 MI record. For later versions, place breakpoint locations in a
6503 list. */
6504 if (uiout->is_mi_like_p () && use_fixed_output)
6505 locations_list.emplace (uiout, "locations");
6506
6507 int n = 1;
6508 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next, ++n)
6509 {
6510 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6511 print_one_breakpoint_location (b, loc, n, last_loc,
6512 allflag, allflag);
6513 }
6514 }
6515 }
6516 }
6517
6518 static int
6519 breakpoint_address_bits (struct breakpoint *b)
6520 {
6521 int print_address_bits = 0;
6522 struct bp_location *loc;
6523
6524 /* Software watchpoints that aren't watching memory don't have an
6525 address to print. */
6526 if (is_no_memory_software_watchpoint (b))
6527 return 0;
6528
6529 for (loc = b->loc; loc; loc = loc->next)
6530 {
6531 int addr_bit;
6532
6533 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6534 if (addr_bit > print_address_bits)
6535 print_address_bits = addr_bit;
6536 }
6537
6538 return print_address_bits;
6539 }
6540
6541 /* See breakpoint.h. */
6542
6543 void
6544 print_breakpoint (breakpoint *b)
6545 {
6546 struct bp_location *dummy_loc = NULL;
6547 print_one_breakpoint (b, &dummy_loc, 0);
6548 }
6549
6550 /* Return true if this breakpoint was set by the user, false if it is
6551 internal or momentary. */
6552
6553 int
6554 user_breakpoint_p (struct breakpoint *b)
6555 {
6556 return b->number > 0;
6557 }
6558
6559 /* See breakpoint.h. */
6560
6561 int
6562 pending_breakpoint_p (struct breakpoint *b)
6563 {
6564 return b->loc == NULL;
6565 }
6566
6567 /* Print information on breakpoints (including watchpoints and tracepoints).
6568
6569 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6570 understood by number_or_range_parser. Only breakpoints included in this
6571 list are then printed.
6572
6573 If SHOW_INTERNAL is true, print internal breakpoints.
6574
6575 If FILTER is non-NULL, call it on each breakpoint and only include the
6576 ones for which it returns true.
6577
6578 Return the total number of breakpoints listed. */
6579
6580 static int
6581 breakpoint_1 (const char *bp_num_list, bool show_internal,
6582 bool (*filter) (const struct breakpoint *))
6583 {
6584 struct breakpoint *b;
6585 struct bp_location *last_loc = NULL;
6586 int nr_printable_breakpoints;
6587 struct value_print_options opts;
6588 int print_address_bits = 0;
6589 int print_type_col_width = 14;
6590 struct ui_out *uiout = current_uiout;
6591 bool has_disabled_by_cond_location = false;
6592
6593 get_user_print_options (&opts);
6594
6595 /* Compute the number of rows in the table, as well as the size
6596 required for address fields. */
6597 nr_printable_breakpoints = 0;
6598 ALL_BREAKPOINTS (b)
6599 {
6600 /* If we have a filter, only list the breakpoints it accepts. */
6601 if (filter && !filter (b))
6602 continue;
6603
6604 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6605 accept. Skip the others. */
6606 if (bp_num_list != NULL && *bp_num_list != '\0')
6607 {
6608 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6609 continue;
6610 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6611 continue;
6612 }
6613
6614 if (show_internal || user_breakpoint_p (b))
6615 {
6616 int addr_bit, type_len;
6617
6618 addr_bit = breakpoint_address_bits (b);
6619 if (addr_bit > print_address_bits)
6620 print_address_bits = addr_bit;
6621
6622 type_len = strlen (bptype_string (b->type));
6623 if (type_len > print_type_col_width)
6624 print_type_col_width = type_len;
6625
6626 nr_printable_breakpoints++;
6627 }
6628 }
6629
6630 {
6631 ui_out_emit_table table_emitter (uiout,
6632 opts.addressprint ? 6 : 5,
6633 nr_printable_breakpoints,
6634 "BreakpointTable");
6635
6636 if (nr_printable_breakpoints > 0)
6637 annotate_breakpoints_headers ();
6638 if (nr_printable_breakpoints > 0)
6639 annotate_field (0);
6640 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6641 if (nr_printable_breakpoints > 0)
6642 annotate_field (1);
6643 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6644 if (nr_printable_breakpoints > 0)
6645 annotate_field (2);
6646 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6647 if (nr_printable_breakpoints > 0)
6648 annotate_field (3);
6649 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6650 if (opts.addressprint)
6651 {
6652 if (nr_printable_breakpoints > 0)
6653 annotate_field (4);
6654 if (print_address_bits <= 32)
6655 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6656 else
6657 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6658 }
6659 if (nr_printable_breakpoints > 0)
6660 annotate_field (5);
6661 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6662 uiout->table_body ();
6663 if (nr_printable_breakpoints > 0)
6664 annotate_breakpoints_table ();
6665
6666 ALL_BREAKPOINTS (b)
6667 {
6668 QUIT;
6669 /* If we have a filter, only list the breakpoints it accepts. */
6670 if (filter && !filter (b))
6671 continue;
6672
6673 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6674 accept. Skip the others. */
6675
6676 if (bp_num_list != NULL && *bp_num_list != '\0')
6677 {
6678 if (show_internal) /* maintenance info breakpoint */
6679 {
6680 if (parse_and_eval_long (bp_num_list) != b->number)
6681 continue;
6682 }
6683 else /* all others */
6684 {
6685 if (!number_is_in_list (bp_num_list, b->number))
6686 continue;
6687 }
6688 }
6689 /* We only print out user settable breakpoints unless the
6690 show_internal is set. */
6691 if (show_internal || user_breakpoint_p (b))
6692 {
6693 print_one_breakpoint (b, &last_loc, show_internal);
6694 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
6695 if (loc->disabled_by_cond)
6696 has_disabled_by_cond_location = true;
6697 }
6698 }
6699 }
6700
6701 if (nr_printable_breakpoints == 0)
6702 {
6703 /* If there's a filter, let the caller decide how to report
6704 empty list. */
6705 if (!filter)
6706 {
6707 if (bp_num_list == NULL || *bp_num_list == '\0')
6708 uiout->message ("No breakpoints or watchpoints.\n");
6709 else
6710 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6711 bp_num_list);
6712 }
6713 }
6714 else
6715 {
6716 if (last_loc && !server_command)
6717 set_next_address (last_loc->gdbarch, last_loc->address);
6718
6719 if (has_disabled_by_cond_location)
6720 uiout->message (_("(*): Breakpoint condition is invalid at this "
6721 "location.\n"));
6722 }
6723
6724 /* FIXME? Should this be moved up so that it is only called when
6725 there have been breakpoints? */
6726 annotate_breakpoints_table_end ();
6727
6728 return nr_printable_breakpoints;
6729 }
6730
6731 /* Display the value of default-collect in a way that is generally
6732 compatible with the breakpoint list. */
6733
6734 static void
6735 default_collect_info (void)
6736 {
6737 struct ui_out *uiout = current_uiout;
6738
6739 /* If it has no value (which is frequently the case), say nothing; a
6740 message like "No default-collect." gets in user's face when it's
6741 not wanted. */
6742 if (!*default_collect)
6743 return;
6744
6745 /* The following phrase lines up nicely with per-tracepoint collect
6746 actions. */
6747 uiout->text ("default collect ");
6748 uiout->field_string ("default-collect", default_collect);
6749 uiout->text (" \n");
6750 }
6751
6752 static void
6753 info_breakpoints_command (const char *args, int from_tty)
6754 {
6755 breakpoint_1 (args, false, NULL);
6756
6757 default_collect_info ();
6758 }
6759
6760 static void
6761 info_watchpoints_command (const char *args, int from_tty)
6762 {
6763 int num_printed = breakpoint_1 (args, false, is_watchpoint);
6764 struct ui_out *uiout = current_uiout;
6765
6766 if (num_printed == 0)
6767 {
6768 if (args == NULL || *args == '\0')
6769 uiout->message ("No watchpoints.\n");
6770 else
6771 uiout->message ("No watchpoint matching '%s'.\n", args);
6772 }
6773 }
6774
6775 static void
6776 maintenance_info_breakpoints (const char *args, int from_tty)
6777 {
6778 breakpoint_1 (args, true, NULL);
6779
6780 default_collect_info ();
6781 }
6782
6783 static int
6784 breakpoint_has_pc (struct breakpoint *b,
6785 struct program_space *pspace,
6786 CORE_ADDR pc, struct obj_section *section)
6787 {
6788 struct bp_location *bl = b->loc;
6789
6790 for (; bl; bl = bl->next)
6791 {
6792 if (bl->pspace == pspace
6793 && bl->address == pc
6794 && (!overlay_debugging || bl->section == section))
6795 return 1;
6796 }
6797 return 0;
6798 }
6799
6800 /* Print a message describing any user-breakpoints set at PC. This
6801 concerns with logical breakpoints, so we match program spaces, not
6802 address spaces. */
6803
6804 static void
6805 describe_other_breakpoints (struct gdbarch *gdbarch,
6806 struct program_space *pspace, CORE_ADDR pc,
6807 struct obj_section *section, int thread)
6808 {
6809 int others = 0;
6810 struct breakpoint *b;
6811
6812 ALL_BREAKPOINTS (b)
6813 others += (user_breakpoint_p (b)
6814 && breakpoint_has_pc (b, pspace, pc, section));
6815 if (others > 0)
6816 {
6817 if (others == 1)
6818 printf_filtered (_("Note: breakpoint "));
6819 else /* if (others == ???) */
6820 printf_filtered (_("Note: breakpoints "));
6821 ALL_BREAKPOINTS (b)
6822 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6823 {
6824 others--;
6825 printf_filtered ("%d", b->number);
6826 if (b->thread == -1 && thread != -1)
6827 printf_filtered (" (all threads)");
6828 else if (b->thread != -1)
6829 printf_filtered (" (thread %d)", b->thread);
6830 printf_filtered ("%s%s ",
6831 ((b->enable_state == bp_disabled
6832 || b->enable_state == bp_call_disabled)
6833 ? " (disabled)"
6834 : ""),
6835 (others > 1) ? ","
6836 : ((others == 1) ? " and" : ""));
6837 }
6838 current_uiout->message (_("also set at pc %ps.\n"),
6839 styled_string (address_style.style (),
6840 paddress (gdbarch, pc)));
6841 }
6842 }
6843 \f
6844
6845 /* Return true iff it is meaningful to use the address member of LOC.
6846 For some breakpoint types, the locations' address members are
6847 irrelevant and it makes no sense to attempt to compare them to
6848 other addresses (or use them for any other purpose either).
6849
6850 More specifically, software watchpoints and catchpoints that are
6851 not backed by breakpoints always have a zero valued location
6852 address and we don't want to mark breakpoints of any of these types
6853 to be a duplicate of an actual breakpoint location at address
6854 zero. */
6855
6856 static bool
6857 bl_address_is_meaningful (bp_location *loc)
6858 {
6859 return loc->loc_type != bp_loc_other;
6860 }
6861
6862 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6863 true if LOC1 and LOC2 represent the same watchpoint location. */
6864
6865 static int
6866 watchpoint_locations_match (struct bp_location *loc1,
6867 struct bp_location *loc2)
6868 {
6869 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6870 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6871
6872 /* Both of them must exist. */
6873 gdb_assert (w1 != NULL);
6874 gdb_assert (w2 != NULL);
6875
6876 /* If the target can evaluate the condition expression in hardware,
6877 then we we need to insert both watchpoints even if they are at
6878 the same place. Otherwise the watchpoint will only trigger when
6879 the condition of whichever watchpoint was inserted evaluates to
6880 true, not giving a chance for GDB to check the condition of the
6881 other watchpoint. */
6882 if ((w1->cond_exp
6883 && target_can_accel_watchpoint_condition (loc1->address,
6884 loc1->length,
6885 loc1->watchpoint_type,
6886 w1->cond_exp.get ()))
6887 || (w2->cond_exp
6888 && target_can_accel_watchpoint_condition (loc2->address,
6889 loc2->length,
6890 loc2->watchpoint_type,
6891 w2->cond_exp.get ())))
6892 return 0;
6893
6894 /* Note that this checks the owner's type, not the location's. In
6895 case the target does not support read watchpoints, but does
6896 support access watchpoints, we'll have bp_read_watchpoint
6897 watchpoints with hw_access locations. Those should be considered
6898 duplicates of hw_read locations. The hw_read locations will
6899 become hw_access locations later. */
6900 return (loc1->owner->type == loc2->owner->type
6901 && loc1->pspace->aspace == loc2->pspace->aspace
6902 && loc1->address == loc2->address
6903 && loc1->length == loc2->length);
6904 }
6905
6906 /* See breakpoint.h. */
6907
6908 int
6909 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6910 const address_space *aspace2, CORE_ADDR addr2)
6911 {
6912 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6913 || aspace1 == aspace2)
6914 && addr1 == addr2);
6915 }
6916
6917 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6918 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6919 matches ASPACE2. On targets that have global breakpoints, the address
6920 space doesn't really matter. */
6921
6922 static int
6923 breakpoint_address_match_range (const address_space *aspace1,
6924 CORE_ADDR addr1,
6925 int len1, const address_space *aspace2,
6926 CORE_ADDR addr2)
6927 {
6928 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6929 || aspace1 == aspace2)
6930 && addr2 >= addr1 && addr2 < addr1 + len1);
6931 }
6932
6933 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6934 a ranged breakpoint. In most targets, a match happens only if ASPACE
6935 matches the breakpoint's address space. On targets that have global
6936 breakpoints, the address space doesn't really matter. */
6937
6938 static int
6939 breakpoint_location_address_match (struct bp_location *bl,
6940 const address_space *aspace,
6941 CORE_ADDR addr)
6942 {
6943 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6944 aspace, addr)
6945 || (bl->length
6946 && breakpoint_address_match_range (bl->pspace->aspace,
6947 bl->address, bl->length,
6948 aspace, addr)));
6949 }
6950
6951 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6952 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6953 match happens only if ASPACE matches the breakpoint's address
6954 space. On targets that have global breakpoints, the address space
6955 doesn't really matter. */
6956
6957 static int
6958 breakpoint_location_address_range_overlap (struct bp_location *bl,
6959 const address_space *aspace,
6960 CORE_ADDR addr, int len)
6961 {
6962 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6963 || bl->pspace->aspace == aspace)
6964 {
6965 int bl_len = bl->length != 0 ? bl->length : 1;
6966
6967 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6968 return 1;
6969 }
6970 return 0;
6971 }
6972
6973 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6974 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6975 true, otherwise returns false. */
6976
6977 static int
6978 tracepoint_locations_match (struct bp_location *loc1,
6979 struct bp_location *loc2)
6980 {
6981 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6982 /* Since tracepoint locations are never duplicated with others', tracepoint
6983 locations at the same address of different tracepoints are regarded as
6984 different locations. */
6985 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6986 else
6987 return 0;
6988 }
6989
6990 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6991 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
6992 the same location. If SW_HW_BPS_MATCH is true, then software
6993 breakpoint locations and hardware breakpoint locations match,
6994 otherwise they don't. */
6995
6996 static int
6997 breakpoint_locations_match (struct bp_location *loc1,
6998 struct bp_location *loc2,
6999 bool sw_hw_bps_match)
7000 {
7001 int hw_point1, hw_point2;
7002
7003 /* Both of them must not be in moribund_locations. */
7004 gdb_assert (loc1->owner != NULL);
7005 gdb_assert (loc2->owner != NULL);
7006
7007 hw_point1 = is_hardware_watchpoint (loc1->owner);
7008 hw_point2 = is_hardware_watchpoint (loc2->owner);
7009
7010 if (hw_point1 != hw_point2)
7011 return 0;
7012 else if (hw_point1)
7013 return watchpoint_locations_match (loc1, loc2);
7014 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7015 return tracepoint_locations_match (loc1, loc2);
7016 else
7017 /* We compare bp_location.length in order to cover ranged
7018 breakpoints. Keep this in sync with
7019 bp_location_is_less_than. */
7020 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7021 loc2->pspace->aspace, loc2->address)
7022 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
7023 && loc1->length == loc2->length);
7024 }
7025
7026 static void
7027 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7028 int bnum, int have_bnum)
7029 {
7030 /* The longest string possibly returned by hex_string_custom
7031 is 50 chars. These must be at least that big for safety. */
7032 char astr1[64];
7033 char astr2[64];
7034
7035 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7036 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7037 if (have_bnum)
7038 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7039 bnum, astr1, astr2);
7040 else
7041 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7042 }
7043
7044 /* Adjust a breakpoint's address to account for architectural
7045 constraints on breakpoint placement. Return the adjusted address.
7046 Note: Very few targets require this kind of adjustment. For most
7047 targets, this function is simply the identity function. */
7048
7049 static CORE_ADDR
7050 adjust_breakpoint_address (struct gdbarch *gdbarch,
7051 CORE_ADDR bpaddr, enum bptype bptype)
7052 {
7053 if (bptype == bp_watchpoint
7054 || bptype == bp_hardware_watchpoint
7055 || bptype == bp_read_watchpoint
7056 || bptype == bp_access_watchpoint
7057 || bptype == bp_catchpoint)
7058 {
7059 /* Watchpoints and the various bp_catch_* eventpoints should not
7060 have their addresses modified. */
7061 return bpaddr;
7062 }
7063 else if (bptype == bp_single_step)
7064 {
7065 /* Single-step breakpoints should not have their addresses
7066 modified. If there's any architectural constrain that
7067 applies to this address, then it should have already been
7068 taken into account when the breakpoint was created in the
7069 first place. If we didn't do this, stepping through e.g.,
7070 Thumb-2 IT blocks would break. */
7071 return bpaddr;
7072 }
7073 else
7074 {
7075 CORE_ADDR adjusted_bpaddr = bpaddr;
7076
7077 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7078 {
7079 /* Some targets have architectural constraints on the placement
7080 of breakpoint instructions. Obtain the adjusted address. */
7081 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7082 }
7083
7084 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
7085
7086 /* An adjusted breakpoint address can significantly alter
7087 a user's expectations. Print a warning if an adjustment
7088 is required. */
7089 if (adjusted_bpaddr != bpaddr)
7090 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7091
7092 return adjusted_bpaddr;
7093 }
7094 }
7095
7096 static bp_loc_type
7097 bp_location_from_bp_type (bptype type)
7098 {
7099 switch (type)
7100 {
7101 case bp_breakpoint:
7102 case bp_single_step:
7103 case bp_until:
7104 case bp_finish:
7105 case bp_longjmp:
7106 case bp_longjmp_resume:
7107 case bp_longjmp_call_dummy:
7108 case bp_exception:
7109 case bp_exception_resume:
7110 case bp_step_resume:
7111 case bp_hp_step_resume:
7112 case bp_watchpoint_scope:
7113 case bp_call_dummy:
7114 case bp_std_terminate:
7115 case bp_shlib_event:
7116 case bp_thread_event:
7117 case bp_overlay_event:
7118 case bp_jit_event:
7119 case bp_longjmp_master:
7120 case bp_std_terminate_master:
7121 case bp_exception_master:
7122 case bp_gnu_ifunc_resolver:
7123 case bp_gnu_ifunc_resolver_return:
7124 case bp_dprintf:
7125 return bp_loc_software_breakpoint;
7126 case bp_hardware_breakpoint:
7127 return bp_loc_hardware_breakpoint;
7128 case bp_hardware_watchpoint:
7129 case bp_read_watchpoint:
7130 case bp_access_watchpoint:
7131 return bp_loc_hardware_watchpoint;
7132 case bp_watchpoint:
7133 case bp_catchpoint:
7134 case bp_tracepoint:
7135 case bp_fast_tracepoint:
7136 case bp_static_tracepoint:
7137 return bp_loc_other;
7138 default:
7139 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7140 }
7141 }
7142
7143 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7144 {
7145 this->owner = owner;
7146 this->cond_bytecode = NULL;
7147 this->shlib_disabled = 0;
7148 this->enabled = 1;
7149 this->disabled_by_cond = false;
7150
7151 this->loc_type = type;
7152
7153 if (this->loc_type == bp_loc_software_breakpoint
7154 || this->loc_type == bp_loc_hardware_breakpoint)
7155 mark_breakpoint_location_modified (this);
7156
7157 incref ();
7158 }
7159
7160 bp_location::bp_location (breakpoint *owner)
7161 : bp_location::bp_location (owner,
7162 bp_location_from_bp_type (owner->type))
7163 {
7164 }
7165
7166 /* Allocate a struct bp_location. */
7167
7168 static struct bp_location *
7169 allocate_bp_location (struct breakpoint *bpt)
7170 {
7171 return bpt->ops->allocate_location (bpt);
7172 }
7173
7174 /* Decrement reference count. If the reference count reaches 0,
7175 destroy the bp_location. Sets *BLP to NULL. */
7176
7177 static void
7178 decref_bp_location (struct bp_location **blp)
7179 {
7180 bp_location_ref_policy::decref (*blp);
7181 *blp = NULL;
7182 }
7183
7184 /* Add breakpoint B at the end of the global breakpoint chain. */
7185
7186 static breakpoint *
7187 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7188 {
7189 struct breakpoint *b1;
7190 struct breakpoint *result = b.get ();
7191
7192 /* Add this breakpoint to the end of the chain so that a list of
7193 breakpoints will come out in order of increasing numbers. */
7194
7195 b1 = breakpoint_chain;
7196 if (b1 == 0)
7197 breakpoint_chain = b.release ();
7198 else
7199 {
7200 while (b1->next)
7201 b1 = b1->next;
7202 b1->next = b.release ();
7203 }
7204
7205 return result;
7206 }
7207
7208 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7209
7210 static void
7211 init_raw_breakpoint_without_location (struct breakpoint *b,
7212 struct gdbarch *gdbarch,
7213 enum bptype bptype,
7214 const struct breakpoint_ops *ops)
7215 {
7216 gdb_assert (ops != NULL);
7217
7218 b->ops = ops;
7219 b->type = bptype;
7220 b->gdbarch = gdbarch;
7221 b->language = current_language->la_language;
7222 b->input_radix = input_radix;
7223 b->related_breakpoint = b;
7224 }
7225
7226 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7227 that has type BPTYPE and has no locations as yet. */
7228
7229 static struct breakpoint *
7230 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7231 enum bptype bptype,
7232 const struct breakpoint_ops *ops)
7233 {
7234 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7235
7236 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7237 return add_to_breakpoint_chain (std::move (b));
7238 }
7239
7240 /* Initialize loc->function_name. */
7241
7242 static void
7243 set_breakpoint_location_function (struct bp_location *loc)
7244 {
7245 gdb_assert (loc->owner != NULL);
7246
7247 if (loc->owner->type == bp_breakpoint
7248 || loc->owner->type == bp_hardware_breakpoint
7249 || is_tracepoint (loc->owner))
7250 {
7251 const char *function_name;
7252
7253 if (loc->msymbol != NULL
7254 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
7255 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
7256 {
7257 struct breakpoint *b = loc->owner;
7258
7259 function_name = loc->msymbol->linkage_name ();
7260
7261 if (b->type == bp_breakpoint && b->loc == loc
7262 && loc->next == NULL && b->related_breakpoint == b)
7263 {
7264 /* Create only the whole new breakpoint of this type but do not
7265 mess more complicated breakpoints with multiple locations. */
7266 b->type = bp_gnu_ifunc_resolver;
7267 /* Remember the resolver's address for use by the return
7268 breakpoint. */
7269 loc->related_address = loc->address;
7270 }
7271 }
7272 else
7273 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7274
7275 if (function_name)
7276 loc->function_name = xstrdup (function_name);
7277 }
7278 }
7279
7280 /* Attempt to determine architecture of location identified by SAL. */
7281 struct gdbarch *
7282 get_sal_arch (struct symtab_and_line sal)
7283 {
7284 if (sal.section)
7285 return sal.section->objfile->arch ();
7286 if (sal.symtab)
7287 return SYMTAB_OBJFILE (sal.symtab)->arch ();
7288
7289 return NULL;
7290 }
7291
7292 /* Low level routine for partially initializing a breakpoint of type
7293 BPTYPE. The newly created breakpoint's address, section, source
7294 file name, and line number are provided by SAL.
7295
7296 It is expected that the caller will complete the initialization of
7297 the newly created breakpoint struct as well as output any status
7298 information regarding the creation of a new breakpoint. */
7299
7300 static void
7301 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7302 struct symtab_and_line sal, enum bptype bptype,
7303 const struct breakpoint_ops *ops)
7304 {
7305 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7306
7307 add_location_to_breakpoint (b, &sal);
7308
7309 if (bptype != bp_catchpoint)
7310 gdb_assert (sal.pspace != NULL);
7311
7312 /* Store the program space that was used to set the breakpoint,
7313 except for ordinary breakpoints, which are independent of the
7314 program space. */
7315 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7316 b->pspace = sal.pspace;
7317 }
7318
7319 /* set_raw_breakpoint is a low level routine for allocating and
7320 partially initializing a breakpoint of type BPTYPE. The newly
7321 created breakpoint's address, section, source file name, and line
7322 number are provided by SAL. The newly created and partially
7323 initialized breakpoint is added to the breakpoint chain and
7324 is also returned as the value of this function.
7325
7326 It is expected that the caller will complete the initialization of
7327 the newly created breakpoint struct as well as output any status
7328 information regarding the creation of a new breakpoint. In
7329 particular, set_raw_breakpoint does NOT set the breakpoint
7330 number! Care should be taken to not allow an error to occur
7331 prior to completing the initialization of the breakpoint. If this
7332 should happen, a bogus breakpoint will be left on the chain. */
7333
7334 struct breakpoint *
7335 set_raw_breakpoint (struct gdbarch *gdbarch,
7336 struct symtab_and_line sal, enum bptype bptype,
7337 const struct breakpoint_ops *ops)
7338 {
7339 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7340
7341 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7342 return add_to_breakpoint_chain (std::move (b));
7343 }
7344
7345 /* Call this routine when stepping and nexting to enable a breakpoint
7346 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7347 initiated the operation. */
7348
7349 void
7350 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7351 {
7352 struct breakpoint *b, *b_tmp;
7353 int thread = tp->global_num;
7354
7355 /* To avoid having to rescan all objfile symbols at every step,
7356 we maintain a list of continually-inserted but always disabled
7357 longjmp "master" breakpoints. Here, we simply create momentary
7358 clones of those and enable them for the requested thread. */
7359 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7360 if (b->pspace == current_program_space
7361 && (b->type == bp_longjmp_master
7362 || b->type == bp_exception_master))
7363 {
7364 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7365 struct breakpoint *clone;
7366
7367 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7368 after their removal. */
7369 clone = momentary_breakpoint_from_master (b, type,
7370 &momentary_breakpoint_ops, 1);
7371 clone->thread = thread;
7372 }
7373
7374 tp->initiating_frame = frame;
7375 }
7376
7377 /* Delete all longjmp breakpoints from THREAD. */
7378 void
7379 delete_longjmp_breakpoint (int thread)
7380 {
7381 struct breakpoint *b, *b_tmp;
7382
7383 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7384 if (b->type == bp_longjmp || b->type == bp_exception)
7385 {
7386 if (b->thread == thread)
7387 delete_breakpoint (b);
7388 }
7389 }
7390
7391 void
7392 delete_longjmp_breakpoint_at_next_stop (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 b->disposition = disp_del_at_next_stop;
7401 }
7402 }
7403
7404 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7405 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7406 pointer to any of them. Return NULL if this system cannot place longjmp
7407 breakpoints. */
7408
7409 struct breakpoint *
7410 set_longjmp_breakpoint_for_call_dummy (void)
7411 {
7412 struct breakpoint *b, *retval = NULL;
7413
7414 ALL_BREAKPOINTS (b)
7415 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7416 {
7417 struct breakpoint *new_b;
7418
7419 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7420 &momentary_breakpoint_ops,
7421 1);
7422 new_b->thread = inferior_thread ()->global_num;
7423
7424 /* Link NEW_B into the chain of RETVAL breakpoints. */
7425
7426 gdb_assert (new_b->related_breakpoint == new_b);
7427 if (retval == NULL)
7428 retval = new_b;
7429 new_b->related_breakpoint = retval;
7430 while (retval->related_breakpoint != new_b->related_breakpoint)
7431 retval = retval->related_breakpoint;
7432 retval->related_breakpoint = new_b;
7433 }
7434
7435 return retval;
7436 }
7437
7438 /* Verify all existing dummy frames and their associated breakpoints for
7439 TP. Remove those which can no longer be found in the current frame
7440 stack.
7441
7442 You should call this function only at places where it is safe to currently
7443 unwind the whole stack. Failed stack unwind would discard live dummy
7444 frames. */
7445
7446 void
7447 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7448 {
7449 struct breakpoint *b, *b_tmp;
7450
7451 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7452 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7453 {
7454 struct breakpoint *dummy_b = b->related_breakpoint;
7455
7456 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7457 dummy_b = dummy_b->related_breakpoint;
7458 if (dummy_b->type != bp_call_dummy
7459 || frame_find_by_id (dummy_b->frame_id) != NULL)
7460 continue;
7461
7462 dummy_frame_discard (dummy_b->frame_id, tp);
7463
7464 while (b->related_breakpoint != b)
7465 {
7466 if (b_tmp == b->related_breakpoint)
7467 b_tmp = b->related_breakpoint->next;
7468 delete_breakpoint (b->related_breakpoint);
7469 }
7470 delete_breakpoint (b);
7471 }
7472 }
7473
7474 void
7475 enable_overlay_breakpoints (void)
7476 {
7477 struct breakpoint *b;
7478
7479 ALL_BREAKPOINTS (b)
7480 if (b->type == bp_overlay_event)
7481 {
7482 b->enable_state = bp_enabled;
7483 update_global_location_list (UGLL_MAY_INSERT);
7484 overlay_events_enabled = 1;
7485 }
7486 }
7487
7488 void
7489 disable_overlay_breakpoints (void)
7490 {
7491 struct breakpoint *b;
7492
7493 ALL_BREAKPOINTS (b)
7494 if (b->type == bp_overlay_event)
7495 {
7496 b->enable_state = bp_disabled;
7497 update_global_location_list (UGLL_DONT_INSERT);
7498 overlay_events_enabled = 0;
7499 }
7500 }
7501
7502 /* Set an active std::terminate breakpoint for each std::terminate
7503 master breakpoint. */
7504 void
7505 set_std_terminate_breakpoint (void)
7506 {
7507 struct breakpoint *b, *b_tmp;
7508
7509 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7510 if (b->pspace == current_program_space
7511 && b->type == bp_std_terminate_master)
7512 {
7513 momentary_breakpoint_from_master (b, bp_std_terminate,
7514 &momentary_breakpoint_ops, 1);
7515 }
7516 }
7517
7518 /* Delete all the std::terminate breakpoints. */
7519 void
7520 delete_std_terminate_breakpoint (void)
7521 {
7522 struct breakpoint *b, *b_tmp;
7523
7524 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7525 if (b->type == bp_std_terminate)
7526 delete_breakpoint (b);
7527 }
7528
7529 struct breakpoint *
7530 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7531 {
7532 struct breakpoint *b;
7533
7534 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7535 &internal_breakpoint_ops);
7536
7537 b->enable_state = bp_enabled;
7538 /* location has to be used or breakpoint_re_set will delete me. */
7539 b->location = new_address_location (b->loc->address, NULL, 0);
7540
7541 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7542
7543 return b;
7544 }
7545
7546 struct lang_and_radix
7547 {
7548 enum language lang;
7549 int radix;
7550 };
7551
7552 /* Create a breakpoint for JIT code registration and unregistration. */
7553
7554 struct breakpoint *
7555 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7556 {
7557 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7558 &internal_breakpoint_ops);
7559 }
7560
7561 /* Remove JIT code registration and unregistration breakpoint(s). */
7562
7563 void
7564 remove_jit_event_breakpoints (void)
7565 {
7566 struct breakpoint *b, *b_tmp;
7567
7568 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7569 if (b->type == bp_jit_event
7570 && b->loc->pspace == current_program_space)
7571 delete_breakpoint (b);
7572 }
7573
7574 void
7575 remove_solib_event_breakpoints (void)
7576 {
7577 struct breakpoint *b, *b_tmp;
7578
7579 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7580 if (b->type == bp_shlib_event
7581 && b->loc->pspace == current_program_space)
7582 delete_breakpoint (b);
7583 }
7584
7585 /* See breakpoint.h. */
7586
7587 void
7588 remove_solib_event_breakpoints_at_next_stop (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 b->disposition = disp_del_at_next_stop;
7596 }
7597
7598 /* Helper for create_solib_event_breakpoint /
7599 create_and_insert_solib_event_breakpoint. Allows specifying which
7600 INSERT_MODE to pass through to update_global_location_list. */
7601
7602 static struct breakpoint *
7603 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7604 enum ugll_insert_mode insert_mode)
7605 {
7606 struct breakpoint *b;
7607
7608 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7609 &internal_breakpoint_ops);
7610 update_global_location_list_nothrow (insert_mode);
7611 return b;
7612 }
7613
7614 struct breakpoint *
7615 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7616 {
7617 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7618 }
7619
7620 /* See breakpoint.h. */
7621
7622 struct breakpoint *
7623 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7624 {
7625 struct breakpoint *b;
7626
7627 /* Explicitly tell update_global_location_list to insert
7628 locations. */
7629 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7630 if (!b->loc->inserted)
7631 {
7632 delete_breakpoint (b);
7633 return NULL;
7634 }
7635 return b;
7636 }
7637
7638 /* Disable any breakpoints that are on code in shared libraries. Only
7639 apply to enabled breakpoints, disabled ones can just stay disabled. */
7640
7641 void
7642 disable_breakpoints_in_shlibs (void)
7643 {
7644 struct bp_location *loc, **locp_tmp;
7645
7646 ALL_BP_LOCATIONS (loc, locp_tmp)
7647 {
7648 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7649 struct breakpoint *b = loc->owner;
7650
7651 /* We apply the check to all breakpoints, including disabled for
7652 those with loc->duplicate set. This is so that when breakpoint
7653 becomes enabled, or the duplicate is removed, gdb will try to
7654 insert all breakpoints. If we don't set shlib_disabled here,
7655 we'll try to insert those breakpoints and fail. */
7656 if (((b->type == bp_breakpoint)
7657 || (b->type == bp_jit_event)
7658 || (b->type == bp_hardware_breakpoint)
7659 || (is_tracepoint (b)))
7660 && loc->pspace == current_program_space
7661 && !loc->shlib_disabled
7662 && solib_name_from_address (loc->pspace, loc->address)
7663 )
7664 {
7665 loc->shlib_disabled = 1;
7666 }
7667 }
7668 }
7669
7670 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7671 notification of unloaded_shlib. Only apply to enabled breakpoints,
7672 disabled ones can just stay disabled. */
7673
7674 static void
7675 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7676 {
7677 struct bp_location *loc, **locp_tmp;
7678 int disabled_shlib_breaks = 0;
7679
7680 ALL_BP_LOCATIONS (loc, locp_tmp)
7681 {
7682 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7683 struct breakpoint *b = loc->owner;
7684
7685 if (solib->pspace == loc->pspace
7686 && !loc->shlib_disabled
7687 && (((b->type == bp_breakpoint
7688 || b->type == bp_jit_event
7689 || b->type == bp_hardware_breakpoint)
7690 && (loc->loc_type == bp_loc_hardware_breakpoint
7691 || loc->loc_type == bp_loc_software_breakpoint))
7692 || is_tracepoint (b))
7693 && solib_contains_address_p (solib, loc->address))
7694 {
7695 loc->shlib_disabled = 1;
7696 /* At this point, we cannot rely on remove_breakpoint
7697 succeeding so we must mark the breakpoint as not inserted
7698 to prevent future errors occurring in remove_breakpoints. */
7699 loc->inserted = 0;
7700
7701 /* This may cause duplicate notifications for the same breakpoint. */
7702 gdb::observers::breakpoint_modified.notify (b);
7703
7704 if (!disabled_shlib_breaks)
7705 {
7706 target_terminal::ours_for_output ();
7707 warning (_("Temporarily disabling breakpoints "
7708 "for unloaded shared library \"%s\""),
7709 solib->so_name);
7710 }
7711 disabled_shlib_breaks = 1;
7712 }
7713 }
7714 }
7715
7716 /* Disable any breakpoints and tracepoints in OBJFILE upon
7717 notification of free_objfile. Only apply to enabled breakpoints,
7718 disabled ones can just stay disabled. */
7719
7720 static void
7721 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7722 {
7723 struct breakpoint *b;
7724
7725 if (objfile == NULL)
7726 return;
7727
7728 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7729 managed by the user with add-symbol-file/remove-symbol-file.
7730 Similarly to how breakpoints in shared libraries are handled in
7731 response to "nosharedlibrary", mark breakpoints in such modules
7732 shlib_disabled so they end up uninserted on the next global
7733 location list update. Shared libraries not loaded by the user
7734 aren't handled here -- they're already handled in
7735 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7736 solib_unloaded observer. We skip objfiles that are not
7737 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7738 main objfile). */
7739 if ((objfile->flags & OBJF_SHARED) == 0
7740 || (objfile->flags & OBJF_USERLOADED) == 0)
7741 return;
7742
7743 ALL_BREAKPOINTS (b)
7744 {
7745 struct bp_location *loc;
7746 int bp_modified = 0;
7747
7748 if (!is_breakpoint (b) && !is_tracepoint (b))
7749 continue;
7750
7751 for (loc = b->loc; loc != NULL; loc = loc->next)
7752 {
7753 CORE_ADDR loc_addr = loc->address;
7754
7755 if (loc->loc_type != bp_loc_hardware_breakpoint
7756 && loc->loc_type != bp_loc_software_breakpoint)
7757 continue;
7758
7759 if (loc->shlib_disabled != 0)
7760 continue;
7761
7762 if (objfile->pspace != loc->pspace)
7763 continue;
7764
7765 if (loc->loc_type != bp_loc_hardware_breakpoint
7766 && loc->loc_type != bp_loc_software_breakpoint)
7767 continue;
7768
7769 if (is_addr_in_objfile (loc_addr, objfile))
7770 {
7771 loc->shlib_disabled = 1;
7772 /* At this point, we don't know whether the object was
7773 unmapped from the inferior or not, so leave the
7774 inserted flag alone. We'll handle failure to
7775 uninsert quietly, in case the object was indeed
7776 unmapped. */
7777
7778 mark_breakpoint_location_modified (loc);
7779
7780 bp_modified = 1;
7781 }
7782 }
7783
7784 if (bp_modified)
7785 gdb::observers::breakpoint_modified.notify (b);
7786 }
7787 }
7788
7789 /* FORK & VFORK catchpoints. */
7790
7791 /* An instance of this type is used to represent a fork or vfork
7792 catchpoint. A breakpoint is really of this type iff its ops pointer points
7793 to CATCH_FORK_BREAKPOINT_OPS. */
7794
7795 struct fork_catchpoint : public breakpoint
7796 {
7797 /* Process id of a child process whose forking triggered this
7798 catchpoint. This field is only valid immediately after this
7799 catchpoint has triggered. */
7800 ptid_t forked_inferior_pid;
7801 };
7802
7803 /* Implement the "insert" breakpoint_ops method for fork
7804 catchpoints. */
7805
7806 static int
7807 insert_catch_fork (struct bp_location *bl)
7808 {
7809 return target_insert_fork_catchpoint (inferior_ptid.pid ());
7810 }
7811
7812 /* Implement the "remove" breakpoint_ops method for fork
7813 catchpoints. */
7814
7815 static int
7816 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
7817 {
7818 return target_remove_fork_catchpoint (inferior_ptid.pid ());
7819 }
7820
7821 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7822 catchpoints. */
7823
7824 static int
7825 breakpoint_hit_catch_fork (const struct bp_location *bl,
7826 const address_space *aspace, CORE_ADDR bp_addr,
7827 const struct target_waitstatus *ws)
7828 {
7829 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7830
7831 if (ws->kind != TARGET_WAITKIND_FORKED)
7832 return 0;
7833
7834 c->forked_inferior_pid = ws->value.related_pid;
7835 return 1;
7836 }
7837
7838 /* Implement the "print_it" breakpoint_ops method for fork
7839 catchpoints. */
7840
7841 static enum print_stop_action
7842 print_it_catch_fork (bpstat bs)
7843 {
7844 struct ui_out *uiout = current_uiout;
7845 struct breakpoint *b = bs->breakpoint_at;
7846 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7847
7848 annotate_catchpoint (b->number);
7849 maybe_print_thread_hit_breakpoint (uiout);
7850 if (b->disposition == disp_del)
7851 uiout->text ("Temporary catchpoint ");
7852 else
7853 uiout->text ("Catchpoint ");
7854 if (uiout->is_mi_like_p ())
7855 {
7856 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7857 uiout->field_string ("disp", bpdisp_text (b->disposition));
7858 }
7859 uiout->field_signed ("bkptno", b->number);
7860 uiout->text (" (forked process ");
7861 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7862 uiout->text ("), ");
7863 return PRINT_SRC_AND_LOC;
7864 }
7865
7866 /* Implement the "print_one" breakpoint_ops method for fork
7867 catchpoints. */
7868
7869 static void
7870 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7871 {
7872 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7873 struct value_print_options opts;
7874 struct ui_out *uiout = current_uiout;
7875
7876 get_user_print_options (&opts);
7877
7878 /* Field 4, the address, is omitted (which makes the columns not
7879 line up too nicely with the headers, but the effect is relatively
7880 readable). */
7881 if (opts.addressprint)
7882 uiout->field_skip ("addr");
7883 annotate_field (5);
7884 uiout->text ("fork");
7885 if (c->forked_inferior_pid != null_ptid)
7886 {
7887 uiout->text (", process ");
7888 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7889 uiout->spaces (1);
7890 }
7891
7892 if (uiout->is_mi_like_p ())
7893 uiout->field_string ("catch-type", "fork");
7894 }
7895
7896 /* Implement the "print_mention" breakpoint_ops method for fork
7897 catchpoints. */
7898
7899 static void
7900 print_mention_catch_fork (struct breakpoint *b)
7901 {
7902 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7903 }
7904
7905 /* Implement the "print_recreate" breakpoint_ops method for fork
7906 catchpoints. */
7907
7908 static void
7909 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7910 {
7911 fprintf_unfiltered (fp, "catch fork");
7912 print_recreate_thread (b, fp);
7913 }
7914
7915 /* The breakpoint_ops structure to be used in fork catchpoints. */
7916
7917 static struct breakpoint_ops catch_fork_breakpoint_ops;
7918
7919 /* Implement the "insert" breakpoint_ops method for vfork
7920 catchpoints. */
7921
7922 static int
7923 insert_catch_vfork (struct bp_location *bl)
7924 {
7925 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
7926 }
7927
7928 /* Implement the "remove" breakpoint_ops method for vfork
7929 catchpoints. */
7930
7931 static int
7932 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
7933 {
7934 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
7935 }
7936
7937 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7938 catchpoints. */
7939
7940 static int
7941 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7942 const address_space *aspace, CORE_ADDR bp_addr,
7943 const struct target_waitstatus *ws)
7944 {
7945 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7946
7947 if (ws->kind != TARGET_WAITKIND_VFORKED)
7948 return 0;
7949
7950 c->forked_inferior_pid = ws->value.related_pid;
7951 return 1;
7952 }
7953
7954 /* Implement the "print_it" breakpoint_ops method for vfork
7955 catchpoints. */
7956
7957 static enum print_stop_action
7958 print_it_catch_vfork (bpstat bs)
7959 {
7960 struct ui_out *uiout = current_uiout;
7961 struct breakpoint *b = bs->breakpoint_at;
7962 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7963
7964 annotate_catchpoint (b->number);
7965 maybe_print_thread_hit_breakpoint (uiout);
7966 if (b->disposition == disp_del)
7967 uiout->text ("Temporary catchpoint ");
7968 else
7969 uiout->text ("Catchpoint ");
7970 if (uiout->is_mi_like_p ())
7971 {
7972 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7973 uiout->field_string ("disp", bpdisp_text (b->disposition));
7974 }
7975 uiout->field_signed ("bkptno", b->number);
7976 uiout->text (" (vforked process ");
7977 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7978 uiout->text ("), ");
7979 return PRINT_SRC_AND_LOC;
7980 }
7981
7982 /* Implement the "print_one" breakpoint_ops method for vfork
7983 catchpoints. */
7984
7985 static void
7986 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7987 {
7988 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7989 struct value_print_options opts;
7990 struct ui_out *uiout = current_uiout;
7991
7992 get_user_print_options (&opts);
7993 /* Field 4, the address, is omitted (which makes the columns not
7994 line up too nicely with the headers, but the effect is relatively
7995 readable). */
7996 if (opts.addressprint)
7997 uiout->field_skip ("addr");
7998 annotate_field (5);
7999 uiout->text ("vfork");
8000 if (c->forked_inferior_pid != null_ptid)
8001 {
8002 uiout->text (", process ");
8003 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
8004 uiout->spaces (1);
8005 }
8006
8007 if (uiout->is_mi_like_p ())
8008 uiout->field_string ("catch-type", "vfork");
8009 }
8010
8011 /* Implement the "print_mention" breakpoint_ops method for vfork
8012 catchpoints. */
8013
8014 static void
8015 print_mention_catch_vfork (struct breakpoint *b)
8016 {
8017 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8018 }
8019
8020 /* Implement the "print_recreate" breakpoint_ops method for vfork
8021 catchpoints. */
8022
8023 static void
8024 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8025 {
8026 fprintf_unfiltered (fp, "catch vfork");
8027 print_recreate_thread (b, fp);
8028 }
8029
8030 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8031
8032 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8033
8034 /* An instance of this type is used to represent an solib catchpoint.
8035 A breakpoint is really of this type iff its ops pointer points to
8036 CATCH_SOLIB_BREAKPOINT_OPS. */
8037
8038 struct solib_catchpoint : public breakpoint
8039 {
8040 ~solib_catchpoint () override;
8041
8042 /* True for "catch load", false for "catch unload". */
8043 bool is_load;
8044
8045 /* Regular expression to match, if any. COMPILED is only valid when
8046 REGEX is non-NULL. */
8047 char *regex;
8048 std::unique_ptr<compiled_regex> compiled;
8049 };
8050
8051 solib_catchpoint::~solib_catchpoint ()
8052 {
8053 xfree (this->regex);
8054 }
8055
8056 static int
8057 insert_catch_solib (struct bp_location *ignore)
8058 {
8059 return 0;
8060 }
8061
8062 static int
8063 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
8064 {
8065 return 0;
8066 }
8067
8068 static int
8069 breakpoint_hit_catch_solib (const struct bp_location *bl,
8070 const address_space *aspace,
8071 CORE_ADDR bp_addr,
8072 const struct target_waitstatus *ws)
8073 {
8074 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8075 struct breakpoint *other;
8076
8077 if (ws->kind == TARGET_WAITKIND_LOADED)
8078 return 1;
8079
8080 ALL_BREAKPOINTS (other)
8081 {
8082 struct bp_location *other_bl;
8083
8084 if (other == bl->owner)
8085 continue;
8086
8087 if (other->type != bp_shlib_event)
8088 continue;
8089
8090 if (self->pspace != NULL && other->pspace != self->pspace)
8091 continue;
8092
8093 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8094 {
8095 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8096 return 1;
8097 }
8098 }
8099
8100 return 0;
8101 }
8102
8103 static void
8104 check_status_catch_solib (struct bpstats *bs)
8105 {
8106 struct solib_catchpoint *self
8107 = (struct solib_catchpoint *) bs->breakpoint_at;
8108
8109 if (self->is_load)
8110 {
8111 for (so_list *iter : current_program_space->added_solibs)
8112 {
8113 if (!self->regex
8114 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
8115 return;
8116 }
8117 }
8118 else
8119 {
8120 for (const std::string &iter : current_program_space->deleted_solibs)
8121 {
8122 if (!self->regex
8123 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
8124 return;
8125 }
8126 }
8127
8128 bs->stop = 0;
8129 bs->print_it = print_it_noop;
8130 }
8131
8132 static enum print_stop_action
8133 print_it_catch_solib (bpstat bs)
8134 {
8135 struct breakpoint *b = bs->breakpoint_at;
8136 struct ui_out *uiout = current_uiout;
8137
8138 annotate_catchpoint (b->number);
8139 maybe_print_thread_hit_breakpoint (uiout);
8140 if (b->disposition == disp_del)
8141 uiout->text ("Temporary catchpoint ");
8142 else
8143 uiout->text ("Catchpoint ");
8144 uiout->field_signed ("bkptno", b->number);
8145 uiout->text ("\n");
8146 if (uiout->is_mi_like_p ())
8147 uiout->field_string ("disp", bpdisp_text (b->disposition));
8148 print_solib_event (1);
8149 return PRINT_SRC_AND_LOC;
8150 }
8151
8152 static void
8153 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8154 {
8155 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8156 struct value_print_options opts;
8157 struct ui_out *uiout = current_uiout;
8158
8159 get_user_print_options (&opts);
8160 /* Field 4, the address, is omitted (which makes the columns not
8161 line up too nicely with the headers, but the effect is relatively
8162 readable). */
8163 if (opts.addressprint)
8164 {
8165 annotate_field (4);
8166 uiout->field_skip ("addr");
8167 }
8168
8169 std::string msg;
8170 annotate_field (5);
8171 if (self->is_load)
8172 {
8173 if (self->regex)
8174 msg = string_printf (_("load of library matching %s"), self->regex);
8175 else
8176 msg = _("load of library");
8177 }
8178 else
8179 {
8180 if (self->regex)
8181 msg = string_printf (_("unload of library matching %s"), self->regex);
8182 else
8183 msg = _("unload of library");
8184 }
8185 uiout->field_string ("what", msg);
8186
8187 if (uiout->is_mi_like_p ())
8188 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8189 }
8190
8191 static void
8192 print_mention_catch_solib (struct breakpoint *b)
8193 {
8194 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8195
8196 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8197 self->is_load ? "load" : "unload");
8198 }
8199
8200 static void
8201 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8202 {
8203 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8204
8205 fprintf_unfiltered (fp, "%s %s",
8206 b->disposition == disp_del ? "tcatch" : "catch",
8207 self->is_load ? "load" : "unload");
8208 if (self->regex)
8209 fprintf_unfiltered (fp, " %s", self->regex);
8210 fprintf_unfiltered (fp, "\n");
8211 }
8212
8213 static struct breakpoint_ops catch_solib_breakpoint_ops;
8214
8215 /* See breakpoint.h. */
8216
8217 void
8218 add_solib_catchpoint (const char *arg, bool is_load, bool is_temp, bool enabled)
8219 {
8220 struct gdbarch *gdbarch = get_current_arch ();
8221
8222 if (!arg)
8223 arg = "";
8224 arg = skip_spaces (arg);
8225
8226 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
8227
8228 if (*arg != '\0')
8229 {
8230 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8231 _("Invalid regexp")));
8232 c->regex = xstrdup (arg);
8233 }
8234
8235 c->is_load = is_load;
8236 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
8237 &catch_solib_breakpoint_ops);
8238
8239 c->enable_state = enabled ? bp_enabled : bp_disabled;
8240
8241 install_breakpoint (0, std::move (c), 1);
8242 }
8243
8244 /* A helper function that does all the work for "catch load" and
8245 "catch unload". */
8246
8247 static void
8248 catch_load_or_unload (const char *arg, int from_tty, int is_load,
8249 struct cmd_list_element *command)
8250 {
8251 const int enabled = 1;
8252 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
8253
8254 add_solib_catchpoint (arg, is_load, temp, enabled);
8255 }
8256
8257 static void
8258 catch_load_command_1 (const char *arg, int from_tty,
8259 struct cmd_list_element *command)
8260 {
8261 catch_load_or_unload (arg, from_tty, 1, command);
8262 }
8263
8264 static void
8265 catch_unload_command_1 (const char *arg, int from_tty,
8266 struct cmd_list_element *command)
8267 {
8268 catch_load_or_unload (arg, from_tty, 0, command);
8269 }
8270
8271 /* See breakpoint.h. */
8272
8273 void
8274 init_catchpoint (struct breakpoint *b,
8275 struct gdbarch *gdbarch, bool temp,
8276 const char *cond_string,
8277 const struct breakpoint_ops *ops)
8278 {
8279 symtab_and_line sal;
8280 sal.pspace = current_program_space;
8281
8282 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8283
8284 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8285 b->disposition = temp ? disp_del : disp_donttouch;
8286 }
8287
8288 void
8289 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8290 {
8291 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8292 set_breakpoint_number (internal, b);
8293 if (is_tracepoint (b))
8294 set_tracepoint_count (breakpoint_count);
8295 if (!internal)
8296 mention (b);
8297 gdb::observers::breakpoint_created.notify (b);
8298
8299 if (update_gll)
8300 update_global_location_list (UGLL_MAY_INSERT);
8301 }
8302
8303 static void
8304 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8305 bool temp, const char *cond_string,
8306 const struct breakpoint_ops *ops)
8307 {
8308 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
8309
8310 init_catchpoint (c.get (), gdbarch, temp, cond_string, ops);
8311
8312 c->forked_inferior_pid = null_ptid;
8313
8314 install_breakpoint (0, std::move (c), 1);
8315 }
8316
8317 /* Exec catchpoints. */
8318
8319 /* An instance of this type is used to represent an exec catchpoint.
8320 A breakpoint is really of this type iff its ops pointer points to
8321 CATCH_EXEC_BREAKPOINT_OPS. */
8322
8323 struct exec_catchpoint : public breakpoint
8324 {
8325 ~exec_catchpoint () override;
8326
8327 /* Filename of a program whose exec triggered this catchpoint.
8328 This field is only valid immediately after this catchpoint has
8329 triggered. */
8330 char *exec_pathname;
8331 };
8332
8333 /* Exec catchpoint destructor. */
8334
8335 exec_catchpoint::~exec_catchpoint ()
8336 {
8337 xfree (this->exec_pathname);
8338 }
8339
8340 static int
8341 insert_catch_exec (struct bp_location *bl)
8342 {
8343 return target_insert_exec_catchpoint (inferior_ptid.pid ());
8344 }
8345
8346 static int
8347 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8348 {
8349 return target_remove_exec_catchpoint (inferior_ptid.pid ());
8350 }
8351
8352 static int
8353 breakpoint_hit_catch_exec (const struct bp_location *bl,
8354 const address_space *aspace, CORE_ADDR bp_addr,
8355 const struct target_waitstatus *ws)
8356 {
8357 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8358
8359 if (ws->kind != TARGET_WAITKIND_EXECD)
8360 return 0;
8361
8362 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8363 return 1;
8364 }
8365
8366 static enum print_stop_action
8367 print_it_catch_exec (bpstat bs)
8368 {
8369 struct ui_out *uiout = current_uiout;
8370 struct breakpoint *b = bs->breakpoint_at;
8371 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8372
8373 annotate_catchpoint (b->number);
8374 maybe_print_thread_hit_breakpoint (uiout);
8375 if (b->disposition == disp_del)
8376 uiout->text ("Temporary catchpoint ");
8377 else
8378 uiout->text ("Catchpoint ");
8379 if (uiout->is_mi_like_p ())
8380 {
8381 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8382 uiout->field_string ("disp", bpdisp_text (b->disposition));
8383 }
8384 uiout->field_signed ("bkptno", b->number);
8385 uiout->text (" (exec'd ");
8386 uiout->field_string ("new-exec", c->exec_pathname);
8387 uiout->text ("), ");
8388
8389 return PRINT_SRC_AND_LOC;
8390 }
8391
8392 static void
8393 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8394 {
8395 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8396 struct value_print_options opts;
8397 struct ui_out *uiout = current_uiout;
8398
8399 get_user_print_options (&opts);
8400
8401 /* Field 4, the address, is omitted (which makes the columns
8402 not line up too nicely with the headers, but the effect
8403 is relatively readable). */
8404 if (opts.addressprint)
8405 uiout->field_skip ("addr");
8406 annotate_field (5);
8407 uiout->text ("exec");
8408 if (c->exec_pathname != NULL)
8409 {
8410 uiout->text (", program \"");
8411 uiout->field_string ("what", c->exec_pathname);
8412 uiout->text ("\" ");
8413 }
8414
8415 if (uiout->is_mi_like_p ())
8416 uiout->field_string ("catch-type", "exec");
8417 }
8418
8419 static void
8420 print_mention_catch_exec (struct breakpoint *b)
8421 {
8422 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8423 }
8424
8425 /* Implement the "print_recreate" breakpoint_ops method for exec
8426 catchpoints. */
8427
8428 static void
8429 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8430 {
8431 fprintf_unfiltered (fp, "catch exec");
8432 print_recreate_thread (b, fp);
8433 }
8434
8435 static struct breakpoint_ops catch_exec_breakpoint_ops;
8436
8437 static int
8438 hw_breakpoint_used_count (void)
8439 {
8440 int i = 0;
8441 struct breakpoint *b;
8442 struct bp_location *bl;
8443
8444 ALL_BREAKPOINTS (b)
8445 {
8446 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8447 for (bl = b->loc; bl; bl = bl->next)
8448 {
8449 /* Special types of hardware breakpoints may use more than
8450 one register. */
8451 i += b->ops->resources_needed (bl);
8452 }
8453 }
8454
8455 return i;
8456 }
8457
8458 /* Returns the resources B would use if it were a hardware
8459 watchpoint. */
8460
8461 static int
8462 hw_watchpoint_use_count (struct breakpoint *b)
8463 {
8464 int i = 0;
8465 struct bp_location *bl;
8466
8467 if (!breakpoint_enabled (b))
8468 return 0;
8469
8470 for (bl = b->loc; bl; bl = bl->next)
8471 {
8472 /* Special types of hardware watchpoints may use more than
8473 one register. */
8474 i += b->ops->resources_needed (bl);
8475 }
8476
8477 return i;
8478 }
8479
8480 /* Returns the sum the used resources of all hardware watchpoints of
8481 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8482 the sum of the used resources of all hardware watchpoints of other
8483 types _not_ TYPE. */
8484
8485 static int
8486 hw_watchpoint_used_count_others (struct breakpoint *except,
8487 enum bptype type, int *other_type_used)
8488 {
8489 int i = 0;
8490 struct breakpoint *b;
8491
8492 *other_type_used = 0;
8493 ALL_BREAKPOINTS (b)
8494 {
8495 if (b == except)
8496 continue;
8497 if (!breakpoint_enabled (b))
8498 continue;
8499
8500 if (b->type == type)
8501 i += hw_watchpoint_use_count (b);
8502 else if (is_hardware_watchpoint (b))
8503 *other_type_used = 1;
8504 }
8505
8506 return i;
8507 }
8508
8509 void
8510 disable_watchpoints_before_interactive_call_start (void)
8511 {
8512 struct breakpoint *b;
8513
8514 ALL_BREAKPOINTS (b)
8515 {
8516 if (is_watchpoint (b) && breakpoint_enabled (b))
8517 {
8518 b->enable_state = bp_call_disabled;
8519 update_global_location_list (UGLL_DONT_INSERT);
8520 }
8521 }
8522 }
8523
8524 void
8525 enable_watchpoints_after_interactive_call_stop (void)
8526 {
8527 struct breakpoint *b;
8528
8529 ALL_BREAKPOINTS (b)
8530 {
8531 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8532 {
8533 b->enable_state = bp_enabled;
8534 update_global_location_list (UGLL_MAY_INSERT);
8535 }
8536 }
8537 }
8538
8539 void
8540 disable_breakpoints_before_startup (void)
8541 {
8542 current_program_space->executing_startup = 1;
8543 update_global_location_list (UGLL_DONT_INSERT);
8544 }
8545
8546 void
8547 enable_breakpoints_after_startup (void)
8548 {
8549 current_program_space->executing_startup = 0;
8550 breakpoint_re_set ();
8551 }
8552
8553 /* Create a new single-step breakpoint for thread THREAD, with no
8554 locations. */
8555
8556 static struct breakpoint *
8557 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8558 {
8559 std::unique_ptr<breakpoint> b (new breakpoint ());
8560
8561 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
8562 &momentary_breakpoint_ops);
8563
8564 b->disposition = disp_donttouch;
8565 b->frame_id = null_frame_id;
8566
8567 b->thread = thread;
8568 gdb_assert (b->thread != 0);
8569
8570 return add_to_breakpoint_chain (std::move (b));
8571 }
8572
8573 /* Set a momentary breakpoint of type TYPE at address specified by
8574 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8575 frame. */
8576
8577 breakpoint_up
8578 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8579 struct frame_id frame_id, enum bptype type)
8580 {
8581 struct breakpoint *b;
8582
8583 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8584 tail-called one. */
8585 gdb_assert (!frame_id_artificial_p (frame_id));
8586
8587 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8588 b->enable_state = bp_enabled;
8589 b->disposition = disp_donttouch;
8590 b->frame_id = frame_id;
8591
8592 b->thread = inferior_thread ()->global_num;
8593
8594 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8595
8596 return breakpoint_up (b);
8597 }
8598
8599 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8600 The new breakpoint will have type TYPE, use OPS as its
8601 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8602
8603 static struct breakpoint *
8604 momentary_breakpoint_from_master (struct breakpoint *orig,
8605 enum bptype type,
8606 const struct breakpoint_ops *ops,
8607 int loc_enabled)
8608 {
8609 struct breakpoint *copy;
8610
8611 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8612 copy->loc = allocate_bp_location (copy);
8613 set_breakpoint_location_function (copy->loc);
8614
8615 copy->loc->gdbarch = orig->loc->gdbarch;
8616 copy->loc->requested_address = orig->loc->requested_address;
8617 copy->loc->address = orig->loc->address;
8618 copy->loc->section = orig->loc->section;
8619 copy->loc->pspace = orig->loc->pspace;
8620 copy->loc->probe = orig->loc->probe;
8621 copy->loc->line_number = orig->loc->line_number;
8622 copy->loc->symtab = orig->loc->symtab;
8623 copy->loc->enabled = loc_enabled;
8624 copy->frame_id = orig->frame_id;
8625 copy->thread = orig->thread;
8626 copy->pspace = orig->pspace;
8627
8628 copy->enable_state = bp_enabled;
8629 copy->disposition = disp_donttouch;
8630 copy->number = internal_breakpoint_number--;
8631
8632 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8633 return copy;
8634 }
8635
8636 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8637 ORIG is NULL. */
8638
8639 struct breakpoint *
8640 clone_momentary_breakpoint (struct breakpoint *orig)
8641 {
8642 /* If there's nothing to clone, then return nothing. */
8643 if (orig == NULL)
8644 return NULL;
8645
8646 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8647 }
8648
8649 breakpoint_up
8650 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8651 enum bptype type)
8652 {
8653 struct symtab_and_line sal;
8654
8655 sal = find_pc_line (pc, 0);
8656 sal.pc = pc;
8657 sal.section = find_pc_overlay (pc);
8658 sal.explicit_pc = 1;
8659
8660 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8661 }
8662 \f
8663
8664 /* Tell the user we have just set a breakpoint B. */
8665
8666 static void
8667 mention (struct breakpoint *b)
8668 {
8669 b->ops->print_mention (b);
8670 current_uiout->text ("\n");
8671 }
8672 \f
8673
8674 static bool bp_loc_is_permanent (struct bp_location *loc);
8675
8676 /* Handle "set breakpoint auto-hw on".
8677
8678 If the explicitly specified breakpoint type is not hardware
8679 breakpoint, check the memory map to see whether the breakpoint
8680 address is in read-only memory.
8681
8682 - location type is not hardware breakpoint, memory is read-only.
8683 We change the type of the location to hardware breakpoint.
8684
8685 - location type is hardware breakpoint, memory is read-write. This
8686 means we've previously made the location hardware one, but then the
8687 memory map changed, so we undo.
8688 */
8689
8690 static void
8691 handle_automatic_hardware_breakpoints (bp_location *bl)
8692 {
8693 if (automatic_hardware_breakpoints
8694 && bl->owner->type != bp_hardware_breakpoint
8695 && (bl->loc_type == bp_loc_software_breakpoint
8696 || bl->loc_type == bp_loc_hardware_breakpoint))
8697 {
8698 /* When breakpoints are removed, remove_breakpoints will use
8699 location types we've just set here, the only possible problem
8700 is that memory map has changed during running program, but
8701 it's not going to work anyway with current gdb. */
8702 mem_region *mr = lookup_mem_region (bl->address);
8703
8704 if (mr != nullptr)
8705 {
8706 enum bp_loc_type new_type;
8707
8708 if (mr->attrib.mode != MEM_RW)
8709 new_type = bp_loc_hardware_breakpoint;
8710 else
8711 new_type = bp_loc_software_breakpoint;
8712
8713 if (new_type != bl->loc_type)
8714 {
8715 static bool said = false;
8716
8717 bl->loc_type = new_type;
8718 if (!said)
8719 {
8720 fprintf_filtered (gdb_stdout,
8721 _("Note: automatically using "
8722 "hardware breakpoints for "
8723 "read-only addresses.\n"));
8724 said = true;
8725 }
8726 }
8727 }
8728 }
8729 }
8730
8731 static struct bp_location *
8732 add_location_to_breakpoint (struct breakpoint *b,
8733 const struct symtab_and_line *sal)
8734 {
8735 struct bp_location *loc, **tmp;
8736 CORE_ADDR adjusted_address;
8737 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8738
8739 if (loc_gdbarch == NULL)
8740 loc_gdbarch = b->gdbarch;
8741
8742 /* Adjust the breakpoint's address prior to allocating a location.
8743 Once we call allocate_bp_location(), that mostly uninitialized
8744 location will be placed on the location chain. Adjustment of the
8745 breakpoint may cause target_read_memory() to be called and we do
8746 not want its scan of the location chain to find a breakpoint and
8747 location that's only been partially initialized. */
8748 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8749 sal->pc, b->type);
8750
8751 /* Sort the locations by their ADDRESS. */
8752 loc = allocate_bp_location (b);
8753 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8754 tmp = &((*tmp)->next))
8755 ;
8756 loc->next = *tmp;
8757 *tmp = loc;
8758
8759 loc->requested_address = sal->pc;
8760 loc->address = adjusted_address;
8761 loc->pspace = sal->pspace;
8762 loc->probe.prob = sal->prob;
8763 loc->probe.objfile = sal->objfile;
8764 gdb_assert (loc->pspace != NULL);
8765 loc->section = sal->section;
8766 loc->gdbarch = loc_gdbarch;
8767 loc->line_number = sal->line;
8768 loc->symtab = sal->symtab;
8769 loc->symbol = sal->symbol;
8770 loc->msymbol = sal->msymbol;
8771 loc->objfile = sal->objfile;
8772
8773 set_breakpoint_location_function (loc);
8774
8775 /* While by definition, permanent breakpoints are already present in the
8776 code, we don't mark the location as inserted. Normally one would expect
8777 that GDB could rely on that breakpoint instruction to stop the program,
8778 thus removing the need to insert its own breakpoint, except that executing
8779 the breakpoint instruction can kill the target instead of reporting a
8780 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8781 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8782 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8783 breakpoint be inserted normally results in QEMU knowing about the GDB
8784 breakpoint, and thus trap before the breakpoint instruction is executed.
8785 (If GDB later needs to continue execution past the permanent breakpoint,
8786 it manually increments the PC, thus avoiding executing the breakpoint
8787 instruction.) */
8788 if (bp_loc_is_permanent (loc))
8789 loc->permanent = 1;
8790
8791 return loc;
8792 }
8793 \f
8794
8795 /* Return true if LOC is pointing to a permanent breakpoint,
8796 return false otherwise. */
8797
8798 static bool
8799 bp_loc_is_permanent (struct bp_location *loc)
8800 {
8801 gdb_assert (loc != NULL);
8802
8803 /* If we have a non-breakpoint-backed catchpoint or a software
8804 watchpoint, just return 0. We should not attempt to read from
8805 the addresses the locations of these breakpoint types point to.
8806 gdbarch_program_breakpoint_here_p, below, will attempt to read
8807 memory. */
8808 if (!bl_address_is_meaningful (loc))
8809 return false;
8810
8811 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8812 switch_to_program_space_and_thread (loc->pspace);
8813 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
8814 }
8815
8816 /* Build a command list for the dprintf corresponding to the current
8817 settings of the dprintf style options. */
8818
8819 static void
8820 update_dprintf_command_list (struct breakpoint *b)
8821 {
8822 char *dprintf_args = b->extra_string;
8823 char *printf_line = NULL;
8824
8825 if (!dprintf_args)
8826 return;
8827
8828 dprintf_args = skip_spaces (dprintf_args);
8829
8830 /* Allow a comma, as it may have terminated a location, but don't
8831 insist on it. */
8832 if (*dprintf_args == ',')
8833 ++dprintf_args;
8834 dprintf_args = skip_spaces (dprintf_args);
8835
8836 if (*dprintf_args != '"')
8837 error (_("Bad format string, missing '\"'."));
8838
8839 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8840 printf_line = xstrprintf ("printf %s", dprintf_args);
8841 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8842 {
8843 if (!dprintf_function)
8844 error (_("No function supplied for dprintf call"));
8845
8846 if (dprintf_channel && strlen (dprintf_channel) > 0)
8847 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8848 dprintf_function,
8849 dprintf_channel,
8850 dprintf_args);
8851 else
8852 printf_line = xstrprintf ("call (void) %s (%s)",
8853 dprintf_function,
8854 dprintf_args);
8855 }
8856 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8857 {
8858 if (target_can_run_breakpoint_commands ())
8859 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8860 else
8861 {
8862 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8863 printf_line = xstrprintf ("printf %s", dprintf_args);
8864 }
8865 }
8866 else
8867 internal_error (__FILE__, __LINE__,
8868 _("Invalid dprintf style."));
8869
8870 gdb_assert (printf_line != NULL);
8871
8872 /* Manufacture a printf sequence. */
8873 struct command_line *printf_cmd_line
8874 = new struct command_line (simple_control, printf_line);
8875 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8876 command_lines_deleter ()));
8877 }
8878
8879 /* Update all dprintf commands, making their command lists reflect
8880 current style settings. */
8881
8882 static void
8883 update_dprintf_commands (const char *args, int from_tty,
8884 struct cmd_list_element *c)
8885 {
8886 struct breakpoint *b;
8887
8888 ALL_BREAKPOINTS (b)
8889 {
8890 if (b->type == bp_dprintf)
8891 update_dprintf_command_list (b);
8892 }
8893 }
8894
8895 /* Create a breakpoint with SAL as location. Use LOCATION
8896 as a description of the location, and COND_STRING
8897 as condition expression. If LOCATION is NULL then create an
8898 "address location" from the address in the SAL. */
8899
8900 static void
8901 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8902 gdb::array_view<const symtab_and_line> sals,
8903 event_location_up &&location,
8904 gdb::unique_xmalloc_ptr<char> filter,
8905 gdb::unique_xmalloc_ptr<char> cond_string,
8906 gdb::unique_xmalloc_ptr<char> extra_string,
8907 enum bptype type, enum bpdisp disposition,
8908 int thread, int task, int ignore_count,
8909 const struct breakpoint_ops *ops, int from_tty,
8910 int enabled, int internal, unsigned flags,
8911 int display_canonical)
8912 {
8913 int i;
8914
8915 if (type == bp_hardware_breakpoint)
8916 {
8917 int target_resources_ok;
8918
8919 i = hw_breakpoint_used_count ();
8920 target_resources_ok =
8921 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8922 i + 1, 0);
8923 if (target_resources_ok == 0)
8924 error (_("No hardware breakpoint support in the target."));
8925 else if (target_resources_ok < 0)
8926 error (_("Hardware breakpoints used exceeds limit."));
8927 }
8928
8929 gdb_assert (!sals.empty ());
8930
8931 for (const auto &sal : sals)
8932 {
8933 struct bp_location *loc;
8934
8935 if (from_tty)
8936 {
8937 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8938 if (!loc_gdbarch)
8939 loc_gdbarch = gdbarch;
8940
8941 describe_other_breakpoints (loc_gdbarch,
8942 sal.pspace, sal.pc, sal.section, thread);
8943 }
8944
8945 if (&sal == &sals[0])
8946 {
8947 init_raw_breakpoint (b, gdbarch, sal, type, ops);
8948 b->thread = thread;
8949 b->task = task;
8950
8951 b->cond_string = cond_string.release ();
8952 b->extra_string = extra_string.release ();
8953 b->ignore_count = ignore_count;
8954 b->enable_state = enabled ? bp_enabled : bp_disabled;
8955 b->disposition = disposition;
8956
8957 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8958 b->loc->inserted = 1;
8959
8960 if (type == bp_static_tracepoint)
8961 {
8962 struct tracepoint *t = (struct tracepoint *) b;
8963 struct static_tracepoint_marker marker;
8964
8965 if (strace_marker_p (b))
8966 {
8967 /* We already know the marker exists, otherwise, we
8968 wouldn't see a sal for it. */
8969 const char *p
8970 = &event_location_to_string (b->location.get ())[3];
8971 const char *endp;
8972
8973 p = skip_spaces (p);
8974
8975 endp = skip_to_space (p);
8976
8977 t->static_trace_marker_id.assign (p, endp - p);
8978
8979 printf_filtered (_("Probed static tracepoint "
8980 "marker \"%s\"\n"),
8981 t->static_trace_marker_id.c_str ());
8982 }
8983 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8984 {
8985 t->static_trace_marker_id = std::move (marker.str_id);
8986
8987 printf_filtered (_("Probed static tracepoint "
8988 "marker \"%s\"\n"),
8989 t->static_trace_marker_id.c_str ());
8990 }
8991 else
8992 warning (_("Couldn't determine the static "
8993 "tracepoint marker to probe"));
8994 }
8995
8996 loc = b->loc;
8997 }
8998 else
8999 {
9000 loc = add_location_to_breakpoint (b, &sal);
9001 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9002 loc->inserted = 1;
9003 }
9004
9005 /* Do not set breakpoint locations conditions yet. As locations
9006 are inserted, they get sorted based on their addresses. Let
9007 the list stabilize to have reliable location numbers. */
9008
9009 /* Dynamic printf requires and uses additional arguments on the
9010 command line, otherwise it's an error. */
9011 if (type == bp_dprintf)
9012 {
9013 if (b->extra_string)
9014 update_dprintf_command_list (b);
9015 else
9016 error (_("Format string required"));
9017 }
9018 else if (b->extra_string)
9019 error (_("Garbage '%s' at end of command"), b->extra_string);
9020 }
9021
9022
9023 /* The order of the locations is now stable. Set the location
9024 condition using the location's number. */
9025 int loc_num = 1;
9026 for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)
9027 {
9028 if (b->cond_string != nullptr)
9029 set_breakpoint_location_condition (b->cond_string, loc, b->number,
9030 loc_num);
9031
9032 ++loc_num;
9033 }
9034
9035 b->display_canonical = display_canonical;
9036 if (location != NULL)
9037 b->location = std::move (location);
9038 else
9039 b->location = new_address_location (b->loc->address, NULL, 0);
9040 b->filter = std::move (filter);
9041 }
9042
9043 static void
9044 create_breakpoint_sal (struct gdbarch *gdbarch,
9045 gdb::array_view<const symtab_and_line> sals,
9046 event_location_up &&location,
9047 gdb::unique_xmalloc_ptr<char> filter,
9048 gdb::unique_xmalloc_ptr<char> cond_string,
9049 gdb::unique_xmalloc_ptr<char> extra_string,
9050 enum bptype type, enum bpdisp disposition,
9051 int thread, int task, int ignore_count,
9052 const struct breakpoint_ops *ops, int from_tty,
9053 int enabled, int internal, unsigned flags,
9054 int display_canonical)
9055 {
9056 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
9057
9058 init_breakpoint_sal (b.get (), gdbarch,
9059 sals, std::move (location),
9060 std::move (filter),
9061 std::move (cond_string),
9062 std::move (extra_string),
9063 type, disposition,
9064 thread, task, ignore_count,
9065 ops, from_tty,
9066 enabled, internal, flags,
9067 display_canonical);
9068
9069 install_breakpoint (internal, std::move (b), 0);
9070 }
9071
9072 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9073 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9074 value. COND_STRING, if not NULL, specified the condition to be
9075 used for all breakpoints. Essentially the only case where
9076 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9077 function. In that case, it's still not possible to specify
9078 separate conditions for different overloaded functions, so
9079 we take just a single condition string.
9080
9081 NOTE: If the function succeeds, the caller is expected to cleanup
9082 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9083 array contents). If the function fails (error() is called), the
9084 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9085 COND and SALS arrays and each of those arrays contents. */
9086
9087 static void
9088 create_breakpoints_sal (struct gdbarch *gdbarch,
9089 struct linespec_result *canonical,
9090 gdb::unique_xmalloc_ptr<char> cond_string,
9091 gdb::unique_xmalloc_ptr<char> extra_string,
9092 enum bptype type, enum bpdisp disposition,
9093 int thread, int task, int ignore_count,
9094 const struct breakpoint_ops *ops, int from_tty,
9095 int enabled, int internal, unsigned flags)
9096 {
9097 if (canonical->pre_expanded)
9098 gdb_assert (canonical->lsals.size () == 1);
9099
9100 for (const auto &lsal : canonical->lsals)
9101 {
9102 /* Note that 'location' can be NULL in the case of a plain
9103 'break', without arguments. */
9104 event_location_up location
9105 = (canonical->location != NULL
9106 ? copy_event_location (canonical->location.get ()) : NULL);
9107 gdb::unique_xmalloc_ptr<char> filter_string
9108 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
9109
9110 create_breakpoint_sal (gdbarch, lsal.sals,
9111 std::move (location),
9112 std::move (filter_string),
9113 std::move (cond_string),
9114 std::move (extra_string),
9115 type, disposition,
9116 thread, task, ignore_count, ops,
9117 from_tty, enabled, internal, flags,
9118 canonical->special_display);
9119 }
9120 }
9121
9122 /* Parse LOCATION which is assumed to be a SAL specification possibly
9123 followed by conditionals. On return, SALS contains an array of SAL
9124 addresses found. LOCATION points to the end of the SAL (for
9125 linespec locations).
9126
9127 The array and the line spec strings are allocated on the heap, it is
9128 the caller's responsibility to free them. */
9129
9130 static void
9131 parse_breakpoint_sals (struct event_location *location,
9132 struct linespec_result *canonical)
9133 {
9134 struct symtab_and_line cursal;
9135
9136 if (event_location_type (location) == LINESPEC_LOCATION)
9137 {
9138 const char *spec = get_linespec_location (location)->spec_string;
9139
9140 if (spec == NULL)
9141 {
9142 /* The last displayed codepoint, if it's valid, is our default
9143 breakpoint address. */
9144 if (last_displayed_sal_is_valid ())
9145 {
9146 /* Set sal's pspace, pc, symtab, and line to the values
9147 corresponding to the last call to print_frame_info.
9148 Be sure to reinitialize LINE with NOTCURRENT == 0
9149 as the breakpoint line number is inappropriate otherwise.
9150 find_pc_line would adjust PC, re-set it back. */
9151 symtab_and_line sal = get_last_displayed_sal ();
9152 CORE_ADDR pc = sal.pc;
9153
9154 sal = find_pc_line (pc, 0);
9155
9156 /* "break" without arguments is equivalent to "break *PC"
9157 where PC is the last displayed codepoint's address. So
9158 make sure to set sal.explicit_pc to prevent GDB from
9159 trying to expand the list of sals to include all other
9160 instances with the same symtab and line. */
9161 sal.pc = pc;
9162 sal.explicit_pc = 1;
9163
9164 struct linespec_sals lsal;
9165 lsal.sals = {sal};
9166 lsal.canonical = NULL;
9167
9168 canonical->lsals.push_back (std::move (lsal));
9169 return;
9170 }
9171 else
9172 error (_("No default breakpoint address now."));
9173 }
9174 }
9175
9176 /* Force almost all breakpoints to be in terms of the
9177 current_source_symtab (which is decode_line_1's default).
9178 This should produce the results we want almost all of the
9179 time while leaving default_breakpoint_* alone.
9180
9181 ObjC: However, don't match an Objective-C method name which
9182 may have a '+' or '-' succeeded by a '['. */
9183 cursal = get_current_source_symtab_and_line ();
9184 if (last_displayed_sal_is_valid ())
9185 {
9186 const char *spec = NULL;
9187
9188 if (event_location_type (location) == LINESPEC_LOCATION)
9189 spec = get_linespec_location (location)->spec_string;
9190
9191 if (!cursal.symtab
9192 || (spec != NULL
9193 && strchr ("+-", spec[0]) != NULL
9194 && spec[1] != '['))
9195 {
9196 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9197 get_last_displayed_symtab (),
9198 get_last_displayed_line (),
9199 canonical, NULL, NULL);
9200 return;
9201 }
9202 }
9203
9204 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9205 cursal.symtab, cursal.line, canonical, NULL, NULL);
9206 }
9207
9208
9209 /* Convert each SAL into a real PC. Verify that the PC can be
9210 inserted as a breakpoint. If it can't throw an error. */
9211
9212 static void
9213 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
9214 {
9215 for (auto &sal : sals)
9216 resolve_sal_pc (&sal);
9217 }
9218
9219 /* Fast tracepoints may have restrictions on valid locations. For
9220 instance, a fast tracepoint using a jump instead of a trap will
9221 likely have to overwrite more bytes than a trap would, and so can
9222 only be placed where the instruction is longer than the jump, or a
9223 multi-instruction sequence does not have a jump into the middle of
9224 it, etc. */
9225
9226 static void
9227 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9228 gdb::array_view<const symtab_and_line> sals)
9229 {
9230 for (const auto &sal : sals)
9231 {
9232 struct gdbarch *sarch;
9233
9234 sarch = get_sal_arch (sal);
9235 /* We fall back to GDBARCH if there is no architecture
9236 associated with SAL. */
9237 if (sarch == NULL)
9238 sarch = gdbarch;
9239 std::string msg;
9240 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
9241 error (_("May not have a fast tracepoint at %s%s"),
9242 paddress (sarch, sal.pc), msg.c_str ());
9243 }
9244 }
9245
9246 /* Given TOK, a string specification of condition and thread, as
9247 accepted by the 'break' command, extract the condition
9248 string and thread number and set *COND_STRING and *THREAD.
9249 PC identifies the context at which the condition should be parsed.
9250 If no condition is found, *COND_STRING is set to NULL.
9251 If no thread is found, *THREAD is set to -1. */
9252
9253 static void
9254 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9255 char **cond_string, int *thread, int *task,
9256 char **rest)
9257 {
9258 *cond_string = NULL;
9259 *thread = -1;
9260 *task = 0;
9261 *rest = NULL;
9262 bool force = false;
9263
9264 while (tok && *tok)
9265 {
9266 const char *end_tok;
9267 int toklen;
9268 const char *cond_start = NULL;
9269 const char *cond_end = NULL;
9270
9271 tok = skip_spaces (tok);
9272
9273 if ((*tok == '"' || *tok == ',') && rest)
9274 {
9275 *rest = savestring (tok, strlen (tok));
9276 return;
9277 }
9278
9279 end_tok = skip_to_space (tok);
9280
9281 toklen = end_tok - tok;
9282
9283 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9284 {
9285 tok = cond_start = end_tok + 1;
9286 try
9287 {
9288 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9289 }
9290 catch (const gdb_exception_error &)
9291 {
9292 if (!force)
9293 throw;
9294 else
9295 tok = tok + strlen (tok);
9296 }
9297 cond_end = tok;
9298 *cond_string = savestring (cond_start, cond_end - cond_start);
9299 }
9300 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
9301 {
9302 tok = tok + toklen;
9303 force = true;
9304 }
9305 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9306 {
9307 const char *tmptok;
9308 struct thread_info *thr;
9309
9310 tok = end_tok + 1;
9311 thr = parse_thread_id (tok, &tmptok);
9312 if (tok == tmptok)
9313 error (_("Junk after thread keyword."));
9314 *thread = thr->global_num;
9315 tok = tmptok;
9316 }
9317 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9318 {
9319 char *tmptok;
9320
9321 tok = end_tok + 1;
9322 *task = strtol (tok, &tmptok, 0);
9323 if (tok == tmptok)
9324 error (_("Junk after task keyword."));
9325 if (!valid_task_id (*task))
9326 error (_("Unknown task %d."), *task);
9327 tok = tmptok;
9328 }
9329 else if (rest)
9330 {
9331 *rest = savestring (tok, strlen (tok));
9332 return;
9333 }
9334 else
9335 error (_("Junk at end of arguments."));
9336 }
9337 }
9338
9339 /* Call 'find_condition_and_thread' for each sal in SALS until a parse
9340 succeeds. The parsed values are written to COND_STRING, THREAD,
9341 TASK, and REST. See the comment of 'find_condition_and_thread'
9342 for the description of these parameters and INPUT. */
9343
9344 static void
9345 find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
9346 const char *input, char **cond_string,
9347 int *thread, int *task, char **rest)
9348 {
9349 int num_failures = 0;
9350 for (auto &sal : sals)
9351 {
9352 char *cond = nullptr;
9353 int thread_id = 0;
9354 int task_id = 0;
9355 char *remaining = nullptr;
9356
9357 /* Here we want to parse 'arg' to separate condition from thread
9358 number. But because parsing happens in a context and the
9359 contexts of sals might be different, try each until there is
9360 success. Finding one successful parse is sufficient for our
9361 goal. When setting the breakpoint we'll re-parse the
9362 condition in the context of each sal. */
9363 try
9364 {
9365 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
9366 &task_id, &remaining);
9367 *cond_string = cond;
9368 *thread = thread_id;
9369 *task = task_id;
9370 *rest = remaining;
9371 break;
9372 }
9373 catch (const gdb_exception_error &e)
9374 {
9375 num_failures++;
9376 /* If no sal remains, do not continue. */
9377 if (num_failures == sals.size ())
9378 throw;
9379 }
9380 }
9381 }
9382
9383 /* Decode a static tracepoint marker spec. */
9384
9385 static std::vector<symtab_and_line>
9386 decode_static_tracepoint_spec (const char **arg_p)
9387 {
9388 const char *p = &(*arg_p)[3];
9389 const char *endp;
9390
9391 p = skip_spaces (p);
9392
9393 endp = skip_to_space (p);
9394
9395 std::string marker_str (p, endp - p);
9396
9397 std::vector<static_tracepoint_marker> markers
9398 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9399 if (markers.empty ())
9400 error (_("No known static tracepoint marker named %s"),
9401 marker_str.c_str ());
9402
9403 std::vector<symtab_and_line> sals;
9404 sals.reserve (markers.size ());
9405
9406 for (const static_tracepoint_marker &marker : markers)
9407 {
9408 symtab_and_line sal = find_pc_line (marker.address, 0);
9409 sal.pc = marker.address;
9410 sals.push_back (sal);
9411 }
9412
9413 *arg_p = endp;
9414 return sals;
9415 }
9416
9417 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9418 according to IS_TRACEPOINT. */
9419
9420 static const struct breakpoint_ops *
9421 breakpoint_ops_for_event_location_type (enum event_location_type location_type,
9422 bool is_tracepoint)
9423 {
9424 if (is_tracepoint)
9425 {
9426 if (location_type == PROBE_LOCATION)
9427 return &tracepoint_probe_breakpoint_ops;
9428 else
9429 return &tracepoint_breakpoint_ops;
9430 }
9431 else
9432 {
9433 if (location_type == PROBE_LOCATION)
9434 return &bkpt_probe_breakpoint_ops;
9435 else
9436 return &bkpt_breakpoint_ops;
9437 }
9438 }
9439
9440 /* See breakpoint.h. */
9441
9442 const struct breakpoint_ops *
9443 breakpoint_ops_for_event_location (const struct event_location *location,
9444 bool is_tracepoint)
9445 {
9446 if (location != nullptr)
9447 return breakpoint_ops_for_event_location_type
9448 (event_location_type (location), is_tracepoint);
9449 return is_tracepoint ? &tracepoint_breakpoint_ops : &bkpt_breakpoint_ops;
9450 }
9451
9452 /* See breakpoint.h. */
9453
9454 int
9455 create_breakpoint (struct gdbarch *gdbarch,
9456 struct event_location *location,
9457 const char *cond_string,
9458 int thread, const char *extra_string,
9459 int parse_extra,
9460 int tempflag, enum bptype type_wanted,
9461 int ignore_count,
9462 enum auto_boolean pending_break_support,
9463 const struct breakpoint_ops *ops,
9464 int from_tty, int enabled, int internal,
9465 unsigned flags)
9466 {
9467 struct linespec_result canonical;
9468 int pending = 0;
9469 int task = 0;
9470 int prev_bkpt_count = breakpoint_count;
9471
9472 gdb_assert (ops != NULL);
9473
9474 /* If extra_string isn't useful, set it to NULL. */
9475 if (extra_string != NULL && *extra_string == '\0')
9476 extra_string = NULL;
9477
9478 try
9479 {
9480 ops->create_sals_from_location (location, &canonical, type_wanted);
9481 }
9482 catch (const gdb_exception_error &e)
9483 {
9484 /* If caller is interested in rc value from parse, set
9485 value. */
9486 if (e.error == NOT_FOUND_ERROR)
9487 {
9488 /* If pending breakpoint support is turned off, throw
9489 error. */
9490
9491 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9492 throw;
9493
9494 exception_print (gdb_stderr, e);
9495
9496 /* If pending breakpoint support is auto query and the user
9497 selects no, then simply return the error code. */
9498 if (pending_break_support == AUTO_BOOLEAN_AUTO
9499 && !nquery (_("Make %s pending on future shared library load? "),
9500 bptype_string (type_wanted)))
9501 return 0;
9502
9503 /* At this point, either the user was queried about setting
9504 a pending breakpoint and selected yes, or pending
9505 breakpoint behavior is on and thus a pending breakpoint
9506 is defaulted on behalf of the user. */
9507 pending = 1;
9508 }
9509 else
9510 throw;
9511 }
9512
9513 if (!pending && canonical.lsals.empty ())
9514 return 0;
9515
9516 /* Resolve all line numbers to PC's and verify that the addresses
9517 are ok for the target. */
9518 if (!pending)
9519 {
9520 for (auto &lsal : canonical.lsals)
9521 breakpoint_sals_to_pc (lsal.sals);
9522 }
9523
9524 /* Fast tracepoints may have additional restrictions on location. */
9525 if (!pending && type_wanted == bp_fast_tracepoint)
9526 {
9527 for (const auto &lsal : canonical.lsals)
9528 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9529 }
9530
9531 /* Verify that condition can be parsed, before setting any
9532 breakpoints. Allocate a separate condition expression for each
9533 breakpoint. */
9534 if (!pending)
9535 {
9536 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9537 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9538
9539 if (parse_extra)
9540 {
9541 char *rest;
9542 char *cond;
9543
9544 const linespec_sals &lsal = canonical.lsals[0];
9545
9546 find_condition_and_thread_for_sals (lsal.sals, extra_string,
9547 &cond, &thread, &task, &rest);
9548 cond_string_copy.reset (cond);
9549 extra_string_copy.reset (rest);
9550 }
9551 else
9552 {
9553 if (type_wanted != bp_dprintf
9554 && extra_string != NULL && *extra_string != '\0')
9555 error (_("Garbage '%s' at end of location"), extra_string);
9556
9557 /* Create a private copy of condition string. */
9558 if (cond_string)
9559 cond_string_copy.reset (xstrdup (cond_string));
9560 /* Create a private copy of any extra string. */
9561 if (extra_string)
9562 extra_string_copy.reset (xstrdup (extra_string));
9563 }
9564
9565 ops->create_breakpoints_sal (gdbarch, &canonical,
9566 std::move (cond_string_copy),
9567 std::move (extra_string_copy),
9568 type_wanted,
9569 tempflag ? disp_del : disp_donttouch,
9570 thread, task, ignore_count, ops,
9571 from_tty, enabled, internal, flags);
9572 }
9573 else
9574 {
9575 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9576
9577 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9578 b->location = copy_event_location (location);
9579
9580 if (parse_extra)
9581 b->cond_string = NULL;
9582 else
9583 {
9584 /* Create a private copy of condition string. */
9585 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
9586 b->thread = thread;
9587 }
9588
9589 /* Create a private copy of any extra string. */
9590 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
9591 b->ignore_count = ignore_count;
9592 b->disposition = tempflag ? disp_del : disp_donttouch;
9593 b->condition_not_parsed = 1;
9594 b->enable_state = enabled ? bp_enabled : bp_disabled;
9595 if ((type_wanted != bp_breakpoint
9596 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9597 b->pspace = current_program_space;
9598
9599 install_breakpoint (internal, std::move (b), 0);
9600 }
9601
9602 if (canonical.lsals.size () > 1)
9603 {
9604 warning (_("Multiple breakpoints were set.\nUse the "
9605 "\"delete\" command to delete unwanted breakpoints."));
9606 prev_breakpoint_count = prev_bkpt_count;
9607 }
9608
9609 update_global_location_list (UGLL_MAY_INSERT);
9610
9611 return 1;
9612 }
9613
9614 /* Set a breakpoint.
9615 ARG is a string describing breakpoint address,
9616 condition, and thread.
9617 FLAG specifies if a breakpoint is hardware on,
9618 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9619 and BP_TEMPFLAG. */
9620
9621 static void
9622 break_command_1 (const char *arg, int flag, int from_tty)
9623 {
9624 int tempflag = flag & BP_TEMPFLAG;
9625 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9626 ? bp_hardware_breakpoint
9627 : bp_breakpoint);
9628
9629 event_location_up location = string_to_event_location (&arg, current_language);
9630 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9631 (location.get (), false /* is_tracepoint */);
9632
9633 create_breakpoint (get_current_arch (),
9634 location.get (),
9635 NULL, 0, arg, 1 /* parse arg */,
9636 tempflag, type_wanted,
9637 0 /* Ignore count */,
9638 pending_break_support,
9639 ops,
9640 from_tty,
9641 1 /* enabled */,
9642 0 /* internal */,
9643 0);
9644 }
9645
9646 /* Helper function for break_command_1 and disassemble_command. */
9647
9648 void
9649 resolve_sal_pc (struct symtab_and_line *sal)
9650 {
9651 CORE_ADDR pc;
9652
9653 if (sal->pc == 0 && sal->symtab != NULL)
9654 {
9655 if (!find_line_pc (sal->symtab, sal->line, &pc))
9656 error (_("No line %d in file \"%s\"."),
9657 sal->line, symtab_to_filename_for_display (sal->symtab));
9658 sal->pc = pc;
9659
9660 /* If this SAL corresponds to a breakpoint inserted using a line
9661 number, then skip the function prologue if necessary. */
9662 if (sal->explicit_line)
9663 skip_prologue_sal (sal);
9664 }
9665
9666 if (sal->section == 0 && sal->symtab != NULL)
9667 {
9668 const struct blockvector *bv;
9669 const struct block *b;
9670 struct symbol *sym;
9671
9672 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9673 SYMTAB_COMPUNIT (sal->symtab));
9674 if (bv != NULL)
9675 {
9676 sym = block_linkage_function (b);
9677 if (sym != NULL)
9678 {
9679 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9680 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9681 sym);
9682 }
9683 else
9684 {
9685 /* It really is worthwhile to have the section, so we'll
9686 just have to look harder. This case can be executed
9687 if we have line numbers but no functions (as can
9688 happen in assembly source). */
9689
9690 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9691 switch_to_program_space_and_thread (sal->pspace);
9692
9693 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9694 if (msym.minsym)
9695 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9696 }
9697 }
9698 }
9699 }
9700
9701 void
9702 break_command (const char *arg, int from_tty)
9703 {
9704 break_command_1 (arg, 0, from_tty);
9705 }
9706
9707 void
9708 tbreak_command (const char *arg, int from_tty)
9709 {
9710 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9711 }
9712
9713 static void
9714 hbreak_command (const char *arg, int from_tty)
9715 {
9716 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9717 }
9718
9719 static void
9720 thbreak_command (const char *arg, int from_tty)
9721 {
9722 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9723 }
9724
9725 static void
9726 stop_command (const char *arg, int from_tty)
9727 {
9728 printf_filtered (_("Specify the type of breakpoint to set.\n\
9729 Usage: stop in <function | address>\n\
9730 stop at <line>\n"));
9731 }
9732
9733 static void
9734 stopin_command (const char *arg, int from_tty)
9735 {
9736 int badInput = 0;
9737
9738 if (arg == NULL)
9739 badInput = 1;
9740 else if (*arg != '*')
9741 {
9742 const char *argptr = arg;
9743 int hasColon = 0;
9744
9745 /* Look for a ':'. If this is a line number specification, then
9746 say it is bad, otherwise, it should be an address or
9747 function/method name. */
9748 while (*argptr && !hasColon)
9749 {
9750 hasColon = (*argptr == ':');
9751 argptr++;
9752 }
9753
9754 if (hasColon)
9755 badInput = (*argptr != ':'); /* Not a class::method */
9756 else
9757 badInput = isdigit (*arg); /* a simple line number */
9758 }
9759
9760 if (badInput)
9761 printf_filtered (_("Usage: stop in <function | address>\n"));
9762 else
9763 break_command_1 (arg, 0, from_tty);
9764 }
9765
9766 static void
9767 stopat_command (const char *arg, int from_tty)
9768 {
9769 int badInput = 0;
9770
9771 if (arg == NULL || *arg == '*') /* no line number */
9772 badInput = 1;
9773 else
9774 {
9775 const char *argptr = arg;
9776 int hasColon = 0;
9777
9778 /* Look for a ':'. If there is a '::' then get out, otherwise
9779 it is probably a line number. */
9780 while (*argptr && !hasColon)
9781 {
9782 hasColon = (*argptr == ':');
9783 argptr++;
9784 }
9785
9786 if (hasColon)
9787 badInput = (*argptr == ':'); /* we have class::method */
9788 else
9789 badInput = !isdigit (*arg); /* not a line number */
9790 }
9791
9792 if (badInput)
9793 printf_filtered (_("Usage: stop at LINE\n"));
9794 else
9795 break_command_1 (arg, 0, from_tty);
9796 }
9797
9798 /* The dynamic printf command is mostly like a regular breakpoint, but
9799 with a prewired command list consisting of a single output command,
9800 built from extra arguments supplied on the dprintf command
9801 line. */
9802
9803 static void
9804 dprintf_command (const char *arg, int from_tty)
9805 {
9806 event_location_up location = string_to_event_location (&arg, current_language);
9807
9808 /* If non-NULL, ARG should have been advanced past the location;
9809 the next character must be ','. */
9810 if (arg != NULL)
9811 {
9812 if (arg[0] != ',' || arg[1] == '\0')
9813 error (_("Format string required"));
9814 else
9815 {
9816 /* Skip the comma. */
9817 ++arg;
9818 }
9819 }
9820
9821 create_breakpoint (get_current_arch (),
9822 location.get (),
9823 NULL, 0, arg, 1 /* parse arg */,
9824 0, bp_dprintf,
9825 0 /* Ignore count */,
9826 pending_break_support,
9827 &dprintf_breakpoint_ops,
9828 from_tty,
9829 1 /* enabled */,
9830 0 /* internal */,
9831 0);
9832 }
9833
9834 static void
9835 agent_printf_command (const char *arg, int from_tty)
9836 {
9837 error (_("May only run agent-printf on the target"));
9838 }
9839
9840 /* Implement the "breakpoint_hit" breakpoint_ops method for
9841 ranged breakpoints. */
9842
9843 static int
9844 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9845 const address_space *aspace,
9846 CORE_ADDR bp_addr,
9847 const struct target_waitstatus *ws)
9848 {
9849 if (ws->kind != TARGET_WAITKIND_STOPPED
9850 || ws->value.sig != GDB_SIGNAL_TRAP)
9851 return 0;
9852
9853 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9854 bl->length, aspace, bp_addr);
9855 }
9856
9857 /* Implement the "resources_needed" breakpoint_ops method for
9858 ranged breakpoints. */
9859
9860 static int
9861 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9862 {
9863 return target_ranged_break_num_registers ();
9864 }
9865
9866 /* Implement the "print_it" breakpoint_ops method for
9867 ranged breakpoints. */
9868
9869 static enum print_stop_action
9870 print_it_ranged_breakpoint (bpstat bs)
9871 {
9872 struct breakpoint *b = bs->breakpoint_at;
9873 struct bp_location *bl = b->loc;
9874 struct ui_out *uiout = current_uiout;
9875
9876 gdb_assert (b->type == bp_hardware_breakpoint);
9877
9878 /* Ranged breakpoints have only one location. */
9879 gdb_assert (bl && bl->next == NULL);
9880
9881 annotate_breakpoint (b->number);
9882
9883 maybe_print_thread_hit_breakpoint (uiout);
9884
9885 if (b->disposition == disp_del)
9886 uiout->text ("Temporary ranged breakpoint ");
9887 else
9888 uiout->text ("Ranged breakpoint ");
9889 if (uiout->is_mi_like_p ())
9890 {
9891 uiout->field_string ("reason",
9892 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9893 uiout->field_string ("disp", bpdisp_text (b->disposition));
9894 }
9895 uiout->field_signed ("bkptno", b->number);
9896 uiout->text (", ");
9897
9898 return PRINT_SRC_AND_LOC;
9899 }
9900
9901 /* Implement the "print_one" breakpoint_ops method for
9902 ranged breakpoints. */
9903
9904 static void
9905 print_one_ranged_breakpoint (struct breakpoint *b,
9906 struct bp_location **last_loc)
9907 {
9908 struct bp_location *bl = b->loc;
9909 struct value_print_options opts;
9910 struct ui_out *uiout = current_uiout;
9911
9912 /* Ranged breakpoints have only one location. */
9913 gdb_assert (bl && bl->next == NULL);
9914
9915 get_user_print_options (&opts);
9916
9917 if (opts.addressprint)
9918 /* We don't print the address range here, it will be printed later
9919 by print_one_detail_ranged_breakpoint. */
9920 uiout->field_skip ("addr");
9921 annotate_field (5);
9922 print_breakpoint_location (b, bl);
9923 *last_loc = bl;
9924 }
9925
9926 /* Implement the "print_one_detail" breakpoint_ops method for
9927 ranged breakpoints. */
9928
9929 static void
9930 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9931 struct ui_out *uiout)
9932 {
9933 CORE_ADDR address_start, address_end;
9934 struct bp_location *bl = b->loc;
9935 string_file stb;
9936
9937 gdb_assert (bl);
9938
9939 address_start = bl->address;
9940 address_end = address_start + bl->length - 1;
9941
9942 uiout->text ("\taddress range: ");
9943 stb.printf ("[%s, %s]",
9944 print_core_address (bl->gdbarch, address_start),
9945 print_core_address (bl->gdbarch, address_end));
9946 uiout->field_stream ("addr", stb);
9947 uiout->text ("\n");
9948 }
9949
9950 /* Implement the "print_mention" breakpoint_ops method for
9951 ranged breakpoints. */
9952
9953 static void
9954 print_mention_ranged_breakpoint (struct breakpoint *b)
9955 {
9956 struct bp_location *bl = b->loc;
9957 struct ui_out *uiout = current_uiout;
9958
9959 gdb_assert (bl);
9960 gdb_assert (b->type == bp_hardware_breakpoint);
9961
9962 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9963 b->number, paddress (bl->gdbarch, bl->address),
9964 paddress (bl->gdbarch, bl->address + bl->length - 1));
9965 }
9966
9967 /* Implement the "print_recreate" breakpoint_ops method for
9968 ranged breakpoints. */
9969
9970 static void
9971 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9972 {
9973 fprintf_unfiltered (fp, "break-range %s, %s",
9974 event_location_to_string (b->location.get ()),
9975 event_location_to_string (b->location_range_end.get ()));
9976 print_recreate_thread (b, fp);
9977 }
9978
9979 /* The breakpoint_ops structure to be used in ranged breakpoints. */
9980
9981 static struct breakpoint_ops ranged_breakpoint_ops;
9982
9983 /* Find the address where the end of the breakpoint range should be
9984 placed, given the SAL of the end of the range. This is so that if
9985 the user provides a line number, the end of the range is set to the
9986 last instruction of the given line. */
9987
9988 static CORE_ADDR
9989 find_breakpoint_range_end (struct symtab_and_line sal)
9990 {
9991 CORE_ADDR end;
9992
9993 /* If the user provided a PC value, use it. Otherwise,
9994 find the address of the end of the given location. */
9995 if (sal.explicit_pc)
9996 end = sal.pc;
9997 else
9998 {
9999 int ret;
10000 CORE_ADDR start;
10001
10002 ret = find_line_pc_range (sal, &start, &end);
10003 if (!ret)
10004 error (_("Could not find location of the end of the range."));
10005
10006 /* find_line_pc_range returns the start of the next line. */
10007 end--;
10008 }
10009
10010 return end;
10011 }
10012
10013 /* Implement the "break-range" CLI command. */
10014
10015 static void
10016 break_range_command (const char *arg, int from_tty)
10017 {
10018 const char *arg_start;
10019 struct linespec_result canonical_start, canonical_end;
10020 int bp_count, can_use_bp, length;
10021 CORE_ADDR end;
10022 struct breakpoint *b;
10023
10024 /* We don't support software ranged breakpoints. */
10025 if (target_ranged_break_num_registers () < 0)
10026 error (_("This target does not support hardware ranged breakpoints."));
10027
10028 bp_count = hw_breakpoint_used_count ();
10029 bp_count += target_ranged_break_num_registers ();
10030 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10031 bp_count, 0);
10032 if (can_use_bp < 0)
10033 error (_("Hardware breakpoints used exceeds limit."));
10034
10035 arg = skip_spaces (arg);
10036 if (arg == NULL || arg[0] == '\0')
10037 error(_("No address range specified."));
10038
10039 arg_start = arg;
10040 event_location_up start_location = string_to_event_location (&arg,
10041 current_language);
10042 parse_breakpoint_sals (start_location.get (), &canonical_start);
10043
10044 if (arg[0] != ',')
10045 error (_("Too few arguments."));
10046 else if (canonical_start.lsals.empty ())
10047 error (_("Could not find location of the beginning of the range."));
10048
10049 const linespec_sals &lsal_start = canonical_start.lsals[0];
10050
10051 if (canonical_start.lsals.size () > 1
10052 || lsal_start.sals.size () != 1)
10053 error (_("Cannot create a ranged breakpoint with multiple locations."));
10054
10055 const symtab_and_line &sal_start = lsal_start.sals[0];
10056 std::string addr_string_start (arg_start, arg - arg_start);
10057
10058 arg++; /* Skip the comma. */
10059 arg = skip_spaces (arg);
10060
10061 /* Parse the end location. */
10062
10063 arg_start = arg;
10064
10065 /* We call decode_line_full directly here instead of using
10066 parse_breakpoint_sals because we need to specify the start location's
10067 symtab and line as the default symtab and line for the end of the
10068 range. This makes it possible to have ranges like "foo.c:27, +14",
10069 where +14 means 14 lines from the start location. */
10070 event_location_up end_location = string_to_event_location (&arg,
10071 current_language);
10072 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10073 sal_start.symtab, sal_start.line,
10074 &canonical_end, NULL, NULL);
10075
10076 if (canonical_end.lsals.empty ())
10077 error (_("Could not find location of the end of the range."));
10078
10079 const linespec_sals &lsal_end = canonical_end.lsals[0];
10080 if (canonical_end.lsals.size () > 1
10081 || lsal_end.sals.size () != 1)
10082 error (_("Cannot create a ranged breakpoint with multiple locations."));
10083
10084 const symtab_and_line &sal_end = lsal_end.sals[0];
10085
10086 end = find_breakpoint_range_end (sal_end);
10087 if (sal_start.pc > end)
10088 error (_("Invalid address range, end precedes start."));
10089
10090 length = end - sal_start.pc + 1;
10091 if (length < 0)
10092 /* Length overflowed. */
10093 error (_("Address range too large."));
10094 else if (length == 1)
10095 {
10096 /* This range is simple enough to be handled by
10097 the `hbreak' command. */
10098 hbreak_command (&addr_string_start[0], 1);
10099
10100 return;
10101 }
10102
10103 /* Now set up the breakpoint. */
10104 b = set_raw_breakpoint (get_current_arch (), sal_start,
10105 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10106 set_breakpoint_count (breakpoint_count + 1);
10107 b->number = breakpoint_count;
10108 b->disposition = disp_donttouch;
10109 b->location = std::move (start_location);
10110 b->location_range_end = std::move (end_location);
10111 b->loc->length = length;
10112
10113 mention (b);
10114 gdb::observers::breakpoint_created.notify (b);
10115 update_global_location_list (UGLL_MAY_INSERT);
10116 }
10117
10118 /* Return non-zero if EXP is verified as constant. Returned zero
10119 means EXP is variable. Also the constant detection may fail for
10120 some constant expressions and in such case still falsely return
10121 zero. */
10122
10123 static bool
10124 watchpoint_exp_is_const (const struct expression *exp)
10125 {
10126 int i = exp->nelts;
10127
10128 while (i > 0)
10129 {
10130 int oplenp, argsp;
10131
10132 /* We are only interested in the descriptor of each element. */
10133 operator_length (exp, i, &oplenp, &argsp);
10134 i -= oplenp;
10135
10136 switch (exp->elts[i].opcode)
10137 {
10138 case BINOP_ADD:
10139 case BINOP_SUB:
10140 case BINOP_MUL:
10141 case BINOP_DIV:
10142 case BINOP_REM:
10143 case BINOP_MOD:
10144 case BINOP_LSH:
10145 case BINOP_RSH:
10146 case BINOP_LOGICAL_AND:
10147 case BINOP_LOGICAL_OR:
10148 case BINOP_BITWISE_AND:
10149 case BINOP_BITWISE_IOR:
10150 case BINOP_BITWISE_XOR:
10151 case BINOP_EQUAL:
10152 case BINOP_NOTEQUAL:
10153 case BINOP_LESS:
10154 case BINOP_GTR:
10155 case BINOP_LEQ:
10156 case BINOP_GEQ:
10157 case BINOP_REPEAT:
10158 case BINOP_COMMA:
10159 case BINOP_EXP:
10160 case BINOP_MIN:
10161 case BINOP_MAX:
10162 case BINOP_INTDIV:
10163 case BINOP_CONCAT:
10164 case TERNOP_COND:
10165 case TERNOP_SLICE:
10166
10167 case OP_LONG:
10168 case OP_FLOAT:
10169 case OP_LAST:
10170 case OP_COMPLEX:
10171 case OP_STRING:
10172 case OP_ARRAY:
10173 case OP_TYPE:
10174 case OP_TYPEOF:
10175 case OP_DECLTYPE:
10176 case OP_TYPEID:
10177 case OP_NAME:
10178 case OP_OBJC_NSSTRING:
10179
10180 case UNOP_NEG:
10181 case UNOP_LOGICAL_NOT:
10182 case UNOP_COMPLEMENT:
10183 case UNOP_ADDR:
10184 case UNOP_HIGH:
10185 case UNOP_CAST:
10186
10187 case UNOP_CAST_TYPE:
10188 case UNOP_REINTERPRET_CAST:
10189 case UNOP_DYNAMIC_CAST:
10190 /* Unary, binary and ternary operators: We have to check
10191 their operands. If they are constant, then so is the
10192 result of that operation. For instance, if A and B are
10193 determined to be constants, then so is "A + B".
10194
10195 UNOP_IND is one exception to the rule above, because the
10196 value of *ADDR is not necessarily a constant, even when
10197 ADDR is. */
10198 break;
10199
10200 case OP_VAR_VALUE:
10201 /* Check whether the associated symbol is a constant.
10202
10203 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10204 possible that a buggy compiler could mark a variable as
10205 constant even when it is not, and TYPE_CONST would return
10206 true in this case, while SYMBOL_CLASS wouldn't.
10207
10208 We also have to check for function symbols because they
10209 are always constant. */
10210 {
10211 struct symbol *s = exp->elts[i + 2].symbol;
10212
10213 if (SYMBOL_CLASS (s) != LOC_BLOCK
10214 && SYMBOL_CLASS (s) != LOC_CONST
10215 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10216 return false;
10217 break;
10218 }
10219
10220 /* The default action is to return 0 because we are using
10221 the optimistic approach here: If we don't know something,
10222 then it is not a constant. */
10223 default:
10224 return false;
10225 }
10226 }
10227
10228 return true;
10229 }
10230
10231 /* Watchpoint destructor. */
10232
10233 watchpoint::~watchpoint ()
10234 {
10235 xfree (this->exp_string);
10236 xfree (this->exp_string_reparse);
10237 }
10238
10239 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10240
10241 static void
10242 re_set_watchpoint (struct breakpoint *b)
10243 {
10244 struct watchpoint *w = (struct watchpoint *) b;
10245
10246 /* Watchpoint can be either on expression using entirely global
10247 variables, or it can be on local variables.
10248
10249 Watchpoints of the first kind are never auto-deleted, and even
10250 persist across program restarts. Since they can use variables
10251 from shared libraries, we need to reparse expression as libraries
10252 are loaded and unloaded.
10253
10254 Watchpoints on local variables can also change meaning as result
10255 of solib event. For example, if a watchpoint uses both a local
10256 and a global variables in expression, it's a local watchpoint,
10257 but unloading of a shared library will make the expression
10258 invalid. This is not a very common use case, but we still
10259 re-evaluate expression, to avoid surprises to the user.
10260
10261 Note that for local watchpoints, we re-evaluate it only if
10262 watchpoints frame id is still valid. If it's not, it means the
10263 watchpoint is out of scope and will be deleted soon. In fact,
10264 I'm not sure we'll ever be called in this case.
10265
10266 If a local watchpoint's frame id is still valid, then
10267 w->exp_valid_block is likewise valid, and we can safely use it.
10268
10269 Don't do anything about disabled watchpoints, since they will be
10270 reevaluated again when enabled. */
10271 update_watchpoint (w, 1 /* reparse */);
10272 }
10273
10274 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10275
10276 static int
10277 insert_watchpoint (struct bp_location *bl)
10278 {
10279 struct watchpoint *w = (struct watchpoint *) bl->owner;
10280 int length = w->exact ? 1 : bl->length;
10281
10282 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10283 w->cond_exp.get ());
10284 }
10285
10286 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10287
10288 static int
10289 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10290 {
10291 struct watchpoint *w = (struct watchpoint *) bl->owner;
10292 int length = w->exact ? 1 : bl->length;
10293
10294 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10295 w->cond_exp.get ());
10296 }
10297
10298 static int
10299 breakpoint_hit_watchpoint (const struct bp_location *bl,
10300 const address_space *aspace, CORE_ADDR bp_addr,
10301 const struct target_waitstatus *ws)
10302 {
10303 struct breakpoint *b = bl->owner;
10304 struct watchpoint *w = (struct watchpoint *) b;
10305
10306 /* Continuable hardware watchpoints are treated as non-existent if the
10307 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10308 some data address). Otherwise gdb won't stop on a break instruction
10309 in the code (not from a breakpoint) when a hardware watchpoint has
10310 been defined. Also skip watchpoints which we know did not trigger
10311 (did not match the data address). */
10312 if (is_hardware_watchpoint (b)
10313 && w->watchpoint_triggered == watch_triggered_no)
10314 return 0;
10315
10316 return 1;
10317 }
10318
10319 static void
10320 check_status_watchpoint (bpstat bs)
10321 {
10322 gdb_assert (is_watchpoint (bs->breakpoint_at));
10323
10324 bpstat_check_watchpoint (bs);
10325 }
10326
10327 /* Implement the "resources_needed" breakpoint_ops method for
10328 hardware watchpoints. */
10329
10330 static int
10331 resources_needed_watchpoint (const struct bp_location *bl)
10332 {
10333 struct watchpoint *w = (struct watchpoint *) bl->owner;
10334 int length = w->exact? 1 : bl->length;
10335
10336 return target_region_ok_for_hw_watchpoint (bl->address, length);
10337 }
10338
10339 /* Implement the "works_in_software_mode" breakpoint_ops method for
10340 hardware watchpoints. */
10341
10342 static int
10343 works_in_software_mode_watchpoint (const struct breakpoint *b)
10344 {
10345 /* Read and access watchpoints only work with hardware support. */
10346 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10347 }
10348
10349 static enum print_stop_action
10350 print_it_watchpoint (bpstat bs)
10351 {
10352 struct breakpoint *b;
10353 enum print_stop_action result;
10354 struct watchpoint *w;
10355 struct ui_out *uiout = current_uiout;
10356
10357 gdb_assert (bs->bp_location_at != NULL);
10358
10359 b = bs->breakpoint_at;
10360 w = (struct watchpoint *) b;
10361
10362 annotate_watchpoint (b->number);
10363 maybe_print_thread_hit_breakpoint (uiout);
10364
10365 string_file stb;
10366
10367 gdb::optional<ui_out_emit_tuple> tuple_emitter;
10368 switch (b->type)
10369 {
10370 case bp_watchpoint:
10371 case bp_hardware_watchpoint:
10372 if (uiout->is_mi_like_p ())
10373 uiout->field_string
10374 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10375 mention (b);
10376 tuple_emitter.emplace (uiout, "value");
10377 uiout->text ("\nOld value = ");
10378 watchpoint_value_print (bs->old_val.get (), &stb);
10379 uiout->field_stream ("old", stb);
10380 uiout->text ("\nNew value = ");
10381 watchpoint_value_print (w->val.get (), &stb);
10382 uiout->field_stream ("new", stb);
10383 uiout->text ("\n");
10384 /* More than one watchpoint may have been triggered. */
10385 result = PRINT_UNKNOWN;
10386 break;
10387
10388 case bp_read_watchpoint:
10389 if (uiout->is_mi_like_p ())
10390 uiout->field_string
10391 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10392 mention (b);
10393 tuple_emitter.emplace (uiout, "value");
10394 uiout->text ("\nValue = ");
10395 watchpoint_value_print (w->val.get (), &stb);
10396 uiout->field_stream ("value", stb);
10397 uiout->text ("\n");
10398 result = PRINT_UNKNOWN;
10399 break;
10400
10401 case bp_access_watchpoint:
10402 if (bs->old_val != NULL)
10403 {
10404 if (uiout->is_mi_like_p ())
10405 uiout->field_string
10406 ("reason",
10407 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10408 mention (b);
10409 tuple_emitter.emplace (uiout, "value");
10410 uiout->text ("\nOld value = ");
10411 watchpoint_value_print (bs->old_val.get (), &stb);
10412 uiout->field_stream ("old", stb);
10413 uiout->text ("\nNew value = ");
10414 }
10415 else
10416 {
10417 mention (b);
10418 if (uiout->is_mi_like_p ())
10419 uiout->field_string
10420 ("reason",
10421 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10422 tuple_emitter.emplace (uiout, "value");
10423 uiout->text ("\nValue = ");
10424 }
10425 watchpoint_value_print (w->val.get (), &stb);
10426 uiout->field_stream ("new", stb);
10427 uiout->text ("\n");
10428 result = PRINT_UNKNOWN;
10429 break;
10430 default:
10431 result = PRINT_UNKNOWN;
10432 }
10433
10434 return result;
10435 }
10436
10437 /* Implement the "print_mention" breakpoint_ops method for hardware
10438 watchpoints. */
10439
10440 static void
10441 print_mention_watchpoint (struct breakpoint *b)
10442 {
10443 struct watchpoint *w = (struct watchpoint *) b;
10444 struct ui_out *uiout = current_uiout;
10445 const char *tuple_name;
10446
10447 switch (b->type)
10448 {
10449 case bp_watchpoint:
10450 uiout->text ("Watchpoint ");
10451 tuple_name = "wpt";
10452 break;
10453 case bp_hardware_watchpoint:
10454 uiout->text ("Hardware watchpoint ");
10455 tuple_name = "wpt";
10456 break;
10457 case bp_read_watchpoint:
10458 uiout->text ("Hardware read watchpoint ");
10459 tuple_name = "hw-rwpt";
10460 break;
10461 case bp_access_watchpoint:
10462 uiout->text ("Hardware access (read/write) watchpoint ");
10463 tuple_name = "hw-awpt";
10464 break;
10465 default:
10466 internal_error (__FILE__, __LINE__,
10467 _("Invalid hardware watchpoint type."));
10468 }
10469
10470 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10471 uiout->field_signed ("number", b->number);
10472 uiout->text (": ");
10473 uiout->field_string ("exp", w->exp_string);
10474 }
10475
10476 /* Implement the "print_recreate" breakpoint_ops method for
10477 watchpoints. */
10478
10479 static void
10480 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10481 {
10482 struct watchpoint *w = (struct watchpoint *) b;
10483
10484 switch (b->type)
10485 {
10486 case bp_watchpoint:
10487 case bp_hardware_watchpoint:
10488 fprintf_unfiltered (fp, "watch");
10489 break;
10490 case bp_read_watchpoint:
10491 fprintf_unfiltered (fp, "rwatch");
10492 break;
10493 case bp_access_watchpoint:
10494 fprintf_unfiltered (fp, "awatch");
10495 break;
10496 default:
10497 internal_error (__FILE__, __LINE__,
10498 _("Invalid watchpoint type."));
10499 }
10500
10501 fprintf_unfiltered (fp, " %s", w->exp_string);
10502 print_recreate_thread (b, fp);
10503 }
10504
10505 /* Implement the "explains_signal" breakpoint_ops method for
10506 watchpoints. */
10507
10508 static int
10509 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10510 {
10511 /* A software watchpoint cannot cause a signal other than
10512 GDB_SIGNAL_TRAP. */
10513 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10514 return 0;
10515
10516 return 1;
10517 }
10518
10519 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10520
10521 static struct breakpoint_ops watchpoint_breakpoint_ops;
10522
10523 /* Implement the "insert" breakpoint_ops method for
10524 masked hardware watchpoints. */
10525
10526 static int
10527 insert_masked_watchpoint (struct bp_location *bl)
10528 {
10529 struct watchpoint *w = (struct watchpoint *) bl->owner;
10530
10531 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10532 bl->watchpoint_type);
10533 }
10534
10535 /* Implement the "remove" breakpoint_ops method for
10536 masked hardware watchpoints. */
10537
10538 static int
10539 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10540 {
10541 struct watchpoint *w = (struct watchpoint *) bl->owner;
10542
10543 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10544 bl->watchpoint_type);
10545 }
10546
10547 /* Implement the "resources_needed" breakpoint_ops method for
10548 masked hardware watchpoints. */
10549
10550 static int
10551 resources_needed_masked_watchpoint (const struct bp_location *bl)
10552 {
10553 struct watchpoint *w = (struct watchpoint *) bl->owner;
10554
10555 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10556 }
10557
10558 /* Implement the "works_in_software_mode" breakpoint_ops method for
10559 masked hardware watchpoints. */
10560
10561 static int
10562 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10563 {
10564 return 0;
10565 }
10566
10567 /* Implement the "print_it" breakpoint_ops method for
10568 masked hardware watchpoints. */
10569
10570 static enum print_stop_action
10571 print_it_masked_watchpoint (bpstat bs)
10572 {
10573 struct breakpoint *b = bs->breakpoint_at;
10574 struct ui_out *uiout = current_uiout;
10575
10576 /* Masked watchpoints have only one location. */
10577 gdb_assert (b->loc && b->loc->next == NULL);
10578
10579 annotate_watchpoint (b->number);
10580 maybe_print_thread_hit_breakpoint (uiout);
10581
10582 switch (b->type)
10583 {
10584 case bp_hardware_watchpoint:
10585 if (uiout->is_mi_like_p ())
10586 uiout->field_string
10587 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10588 break;
10589
10590 case bp_read_watchpoint:
10591 if (uiout->is_mi_like_p ())
10592 uiout->field_string
10593 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10594 break;
10595
10596 case bp_access_watchpoint:
10597 if (uiout->is_mi_like_p ())
10598 uiout->field_string
10599 ("reason",
10600 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10601 break;
10602 default:
10603 internal_error (__FILE__, __LINE__,
10604 _("Invalid hardware watchpoint type."));
10605 }
10606
10607 mention (b);
10608 uiout->text (_("\n\
10609 Check the underlying instruction at PC for the memory\n\
10610 address and value which triggered this watchpoint.\n"));
10611 uiout->text ("\n");
10612
10613 /* More than one watchpoint may have been triggered. */
10614 return PRINT_UNKNOWN;
10615 }
10616
10617 /* Implement the "print_one_detail" breakpoint_ops method for
10618 masked hardware watchpoints. */
10619
10620 static void
10621 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10622 struct ui_out *uiout)
10623 {
10624 struct watchpoint *w = (struct watchpoint *) b;
10625
10626 /* Masked watchpoints have only one location. */
10627 gdb_assert (b->loc && b->loc->next == NULL);
10628
10629 uiout->text ("\tmask ");
10630 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10631 uiout->text ("\n");
10632 }
10633
10634 /* Implement the "print_mention" breakpoint_ops method for
10635 masked hardware watchpoints. */
10636
10637 static void
10638 print_mention_masked_watchpoint (struct breakpoint *b)
10639 {
10640 struct watchpoint *w = (struct watchpoint *) b;
10641 struct ui_out *uiout = current_uiout;
10642 const char *tuple_name;
10643
10644 switch (b->type)
10645 {
10646 case bp_hardware_watchpoint:
10647 uiout->text ("Masked hardware watchpoint ");
10648 tuple_name = "wpt";
10649 break;
10650 case bp_read_watchpoint:
10651 uiout->text ("Masked hardware read watchpoint ");
10652 tuple_name = "hw-rwpt";
10653 break;
10654 case bp_access_watchpoint:
10655 uiout->text ("Masked hardware access (read/write) watchpoint ");
10656 tuple_name = "hw-awpt";
10657 break;
10658 default:
10659 internal_error (__FILE__, __LINE__,
10660 _("Invalid hardware watchpoint type."));
10661 }
10662
10663 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10664 uiout->field_signed ("number", b->number);
10665 uiout->text (": ");
10666 uiout->field_string ("exp", w->exp_string);
10667 }
10668
10669 /* Implement the "print_recreate" breakpoint_ops method for
10670 masked hardware watchpoints. */
10671
10672 static void
10673 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10674 {
10675 struct watchpoint *w = (struct watchpoint *) b;
10676
10677 switch (b->type)
10678 {
10679 case bp_hardware_watchpoint:
10680 fprintf_unfiltered (fp, "watch");
10681 break;
10682 case bp_read_watchpoint:
10683 fprintf_unfiltered (fp, "rwatch");
10684 break;
10685 case bp_access_watchpoint:
10686 fprintf_unfiltered (fp, "awatch");
10687 break;
10688 default:
10689 internal_error (__FILE__, __LINE__,
10690 _("Invalid hardware watchpoint type."));
10691 }
10692
10693 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string,
10694 phex (w->hw_wp_mask, sizeof (CORE_ADDR)));
10695 print_recreate_thread (b, fp);
10696 }
10697
10698 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10699
10700 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10701
10702 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10703
10704 static bool
10705 is_masked_watchpoint (const struct breakpoint *b)
10706 {
10707 return b->ops == &masked_watchpoint_breakpoint_ops;
10708 }
10709
10710 /* accessflag: hw_write: watch write,
10711 hw_read: watch read,
10712 hw_access: watch access (read or write) */
10713 static void
10714 watch_command_1 (const char *arg, int accessflag, int from_tty,
10715 bool just_location, bool internal)
10716 {
10717 struct breakpoint *scope_breakpoint = NULL;
10718 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10719 struct value *result;
10720 int saved_bitpos = 0, saved_bitsize = 0;
10721 const char *exp_start = NULL;
10722 const char *exp_end = NULL;
10723 const char *tok, *end_tok;
10724 int toklen = -1;
10725 const char *cond_start = NULL;
10726 const char *cond_end = NULL;
10727 enum bptype bp_type;
10728 int thread = -1;
10729 int pc = 0;
10730 /* Flag to indicate whether we are going to use masks for
10731 the hardware watchpoint. */
10732 bool use_mask = false;
10733 CORE_ADDR mask = 0;
10734
10735 /* Make sure that we actually have parameters to parse. */
10736 if (arg != NULL && arg[0] != '\0')
10737 {
10738 const char *value_start;
10739
10740 exp_end = arg + strlen (arg);
10741
10742 /* Look for "parameter value" pairs at the end
10743 of the arguments string. */
10744 for (tok = exp_end - 1; tok > arg; tok--)
10745 {
10746 /* Skip whitespace at the end of the argument list. */
10747 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10748 tok--;
10749
10750 /* Find the beginning of the last token.
10751 This is the value of the parameter. */
10752 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10753 tok--;
10754 value_start = tok + 1;
10755
10756 /* Skip whitespace. */
10757 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10758 tok--;
10759
10760 end_tok = tok;
10761
10762 /* Find the beginning of the second to last token.
10763 This is the parameter itself. */
10764 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10765 tok--;
10766 tok++;
10767 toklen = end_tok - tok + 1;
10768
10769 if (toklen == 6 && startswith (tok, "thread"))
10770 {
10771 struct thread_info *thr;
10772 /* At this point we've found a "thread" token, which means
10773 the user is trying to set a watchpoint that triggers
10774 only in a specific thread. */
10775 const char *endp;
10776
10777 if (thread != -1)
10778 error(_("You can specify only one thread."));
10779
10780 /* Extract the thread ID from the next token. */
10781 thr = parse_thread_id (value_start, &endp);
10782
10783 /* Check if the user provided a valid thread ID. */
10784 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10785 invalid_thread_id_error (value_start);
10786
10787 thread = thr->global_num;
10788 }
10789 else if (toklen == 4 && startswith (tok, "mask"))
10790 {
10791 /* We've found a "mask" token, which means the user wants to
10792 create a hardware watchpoint that is going to have the mask
10793 facility. */
10794 struct value *mask_value, *mark;
10795
10796 if (use_mask)
10797 error(_("You can specify only one mask."));
10798
10799 use_mask = just_location = true;
10800
10801 mark = value_mark ();
10802 mask_value = parse_to_comma_and_eval (&value_start);
10803 mask = value_as_address (mask_value);
10804 value_free_to_mark (mark);
10805 }
10806 else
10807 /* We didn't recognize what we found. We should stop here. */
10808 break;
10809
10810 /* Truncate the string and get rid of the "parameter value" pair before
10811 the arguments string is parsed by the parse_exp_1 function. */
10812 exp_end = tok;
10813 }
10814 }
10815 else
10816 exp_end = arg;
10817
10818 /* Parse the rest of the arguments. From here on out, everything
10819 is in terms of a newly allocated string instead of the original
10820 ARG. */
10821 std::string expression (arg, exp_end - arg);
10822 exp_start = arg = expression.c_str ();
10823 innermost_block_tracker tracker;
10824 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10825 exp_end = arg;
10826 /* Remove trailing whitespace from the expression before saving it.
10827 This makes the eventual display of the expression string a bit
10828 prettier. */
10829 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10830 --exp_end;
10831
10832 /* Checking if the expression is not constant. */
10833 if (watchpoint_exp_is_const (exp.get ()))
10834 {
10835 int len;
10836
10837 len = exp_end - exp_start;
10838 while (len > 0 && isspace (exp_start[len - 1]))
10839 len--;
10840 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10841 }
10842
10843 exp_valid_block = tracker.block ();
10844 struct value *mark = value_mark ();
10845 struct value *val_as_value = nullptr;
10846 fetch_subexp_value (exp.get (), &pc, &val_as_value, &result, NULL,
10847 just_location);
10848
10849 if (val_as_value != NULL && just_location)
10850 {
10851 saved_bitpos = value_bitpos (val_as_value);
10852 saved_bitsize = value_bitsize (val_as_value);
10853 }
10854
10855 value_ref_ptr val;
10856 if (just_location)
10857 {
10858 int ret;
10859
10860 exp_valid_block = NULL;
10861 val = release_value (value_addr (result));
10862 value_free_to_mark (mark);
10863
10864 if (use_mask)
10865 {
10866 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10867 mask);
10868 if (ret == -1)
10869 error (_("This target does not support masked watchpoints."));
10870 else if (ret == -2)
10871 error (_("Invalid mask or memory region."));
10872 }
10873 }
10874 else if (val_as_value != NULL)
10875 val = release_value (val_as_value);
10876
10877 tok = skip_spaces (arg);
10878 end_tok = skip_to_space (tok);
10879
10880 toklen = end_tok - tok;
10881 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10882 {
10883 tok = cond_start = end_tok + 1;
10884 innermost_block_tracker if_tracker;
10885 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10886
10887 /* The watchpoint expression may not be local, but the condition
10888 may still be. E.g.: `watch global if local > 0'. */
10889 cond_exp_valid_block = if_tracker.block ();
10890
10891 cond_end = tok;
10892 }
10893 if (*tok)
10894 error (_("Junk at end of command."));
10895
10896 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10897
10898 /* Save this because create_internal_breakpoint below invalidates
10899 'wp_frame'. */
10900 frame_id watchpoint_frame = get_frame_id (wp_frame);
10901
10902 /* If the expression is "local", then set up a "watchpoint scope"
10903 breakpoint at the point where we've left the scope of the watchpoint
10904 expression. Create the scope breakpoint before the watchpoint, so
10905 that we will encounter it first in bpstat_stop_status. */
10906 if (exp_valid_block != NULL && wp_frame != NULL)
10907 {
10908 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10909
10910 if (frame_id_p (caller_frame_id))
10911 {
10912 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10913 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10914
10915 scope_breakpoint
10916 = create_internal_breakpoint (caller_arch, caller_pc,
10917 bp_watchpoint_scope,
10918 &momentary_breakpoint_ops);
10919
10920 /* create_internal_breakpoint could invalidate WP_FRAME. */
10921 wp_frame = NULL;
10922
10923 scope_breakpoint->enable_state = bp_enabled;
10924
10925 /* Automatically delete the breakpoint when it hits. */
10926 scope_breakpoint->disposition = disp_del;
10927
10928 /* Only break in the proper frame (help with recursion). */
10929 scope_breakpoint->frame_id = caller_frame_id;
10930
10931 /* Set the address at which we will stop. */
10932 scope_breakpoint->loc->gdbarch = caller_arch;
10933 scope_breakpoint->loc->requested_address = caller_pc;
10934 scope_breakpoint->loc->address
10935 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10936 scope_breakpoint->loc->requested_address,
10937 scope_breakpoint->type);
10938 }
10939 }
10940
10941 /* Now set up the breakpoint. We create all watchpoints as hardware
10942 watchpoints here even if hardware watchpoints are turned off, a call
10943 to update_watchpoint later in this function will cause the type to
10944 drop back to bp_watchpoint (software watchpoint) if required. */
10945
10946 if (accessflag == hw_read)
10947 bp_type = bp_read_watchpoint;
10948 else if (accessflag == hw_access)
10949 bp_type = bp_access_watchpoint;
10950 else
10951 bp_type = bp_hardware_watchpoint;
10952
10953 std::unique_ptr<watchpoint> w (new watchpoint ());
10954
10955 if (use_mask)
10956 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10957 &masked_watchpoint_breakpoint_ops);
10958 else
10959 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10960 &watchpoint_breakpoint_ops);
10961 w->thread = thread;
10962 w->disposition = disp_donttouch;
10963 w->pspace = current_program_space;
10964 w->exp = std::move (exp);
10965 w->exp_valid_block = exp_valid_block;
10966 w->cond_exp_valid_block = cond_exp_valid_block;
10967 if (just_location)
10968 {
10969 struct type *t = value_type (val.get ());
10970 CORE_ADDR addr = value_as_address (val.get ());
10971
10972 w->exp_string_reparse
10973 = current_language->watch_location_expression (t, addr).release ();
10974
10975 w->exp_string = xstrprintf ("-location %.*s",
10976 (int) (exp_end - exp_start), exp_start);
10977 }
10978 else
10979 w->exp_string = savestring (exp_start, exp_end - exp_start);
10980
10981 if (use_mask)
10982 {
10983 w->hw_wp_mask = mask;
10984 }
10985 else
10986 {
10987 w->val = val;
10988 w->val_bitpos = saved_bitpos;
10989 w->val_bitsize = saved_bitsize;
10990 w->val_valid = true;
10991 }
10992
10993 if (cond_start)
10994 w->cond_string = savestring (cond_start, cond_end - cond_start);
10995 else
10996 w->cond_string = 0;
10997
10998 if (frame_id_p (watchpoint_frame))
10999 {
11000 w->watchpoint_frame = watchpoint_frame;
11001 w->watchpoint_thread = inferior_ptid;
11002 }
11003 else
11004 {
11005 w->watchpoint_frame = null_frame_id;
11006 w->watchpoint_thread = null_ptid;
11007 }
11008
11009 if (scope_breakpoint != NULL)
11010 {
11011 /* The scope breakpoint is related to the watchpoint. We will
11012 need to act on them together. */
11013 w->related_breakpoint = scope_breakpoint;
11014 scope_breakpoint->related_breakpoint = w.get ();
11015 }
11016
11017 if (!just_location)
11018 value_free_to_mark (mark);
11019
11020 /* Finally update the new watchpoint. This creates the locations
11021 that should be inserted. */
11022 update_watchpoint (w.get (), 1);
11023
11024 install_breakpoint (internal, std::move (w), 1);
11025 }
11026
11027 /* Return count of debug registers needed to watch the given expression.
11028 If the watchpoint cannot be handled in hardware return zero. */
11029
11030 static int
11031 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
11032 {
11033 int found_memory_cnt = 0;
11034
11035 /* Did the user specifically forbid us to use hardware watchpoints? */
11036 if (!can_use_hw_watchpoints)
11037 return 0;
11038
11039 gdb_assert (!vals.empty ());
11040 struct value *head = vals[0].get ();
11041
11042 /* Make sure that the value of the expression depends only upon
11043 memory contents, and values computed from them within GDB. If we
11044 find any register references or function calls, we can't use a
11045 hardware watchpoint.
11046
11047 The idea here is that evaluating an expression generates a series
11048 of values, one holding the value of every subexpression. (The
11049 expression a*b+c has five subexpressions: a, b, a*b, c, and
11050 a*b+c.) GDB's values hold almost enough information to establish
11051 the criteria given above --- they identify memory lvalues,
11052 register lvalues, computed values, etcetera. So we can evaluate
11053 the expression, and then scan the chain of values that leaves
11054 behind to decide whether we can detect any possible change to the
11055 expression's final value using only hardware watchpoints.
11056
11057 However, I don't think that the values returned by inferior
11058 function calls are special in any way. So this function may not
11059 notice that an expression involving an inferior function call
11060 can't be watched with hardware watchpoints. FIXME. */
11061 for (const value_ref_ptr &iter : vals)
11062 {
11063 struct value *v = iter.get ();
11064
11065 if (VALUE_LVAL (v) == lval_memory)
11066 {
11067 if (v != head && value_lazy (v))
11068 /* A lazy memory lvalue in the chain is one that GDB never
11069 needed to fetch; we either just used its address (e.g.,
11070 `a' in `a.b') or we never needed it at all (e.g., `a'
11071 in `a,b'). This doesn't apply to HEAD; if that is
11072 lazy then it was not readable, but watch it anyway. */
11073 ;
11074 else
11075 {
11076 /* Ahh, memory we actually used! Check if we can cover
11077 it with hardware watchpoints. */
11078 struct type *vtype = check_typedef (value_type (v));
11079
11080 /* We only watch structs and arrays if user asked for it
11081 explicitly, never if they just happen to appear in a
11082 middle of some value chain. */
11083 if (v == head
11084 || (vtype->code () != TYPE_CODE_STRUCT
11085 && vtype->code () != TYPE_CODE_ARRAY))
11086 {
11087 CORE_ADDR vaddr = value_address (v);
11088 int len;
11089 int num_regs;
11090
11091 len = (target_exact_watchpoints
11092 && is_scalar_type_recursive (vtype))?
11093 1 : TYPE_LENGTH (value_type (v));
11094
11095 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11096 if (!num_regs)
11097 return 0;
11098 else
11099 found_memory_cnt += num_regs;
11100 }
11101 }
11102 }
11103 else if (VALUE_LVAL (v) != not_lval
11104 && deprecated_value_modifiable (v) == 0)
11105 return 0; /* These are values from the history (e.g., $1). */
11106 else if (VALUE_LVAL (v) == lval_register)
11107 return 0; /* Cannot watch a register with a HW watchpoint. */
11108 }
11109
11110 /* The expression itself looks suitable for using a hardware
11111 watchpoint, but give the target machine a chance to reject it. */
11112 return found_memory_cnt;
11113 }
11114
11115 void
11116 watch_command_wrapper (const char *arg, int from_tty, bool internal)
11117 {
11118 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11119 }
11120
11121 /* Options for the watch, awatch, and rwatch commands. */
11122
11123 struct watch_options
11124 {
11125 /* For -location. */
11126 bool location = false;
11127 };
11128
11129 /* Definitions of options for the "watch", "awatch", and "rwatch" commands.
11130
11131 Historically GDB always accepted both '-location' and '-l' flags for
11132 these commands (both flags being synonyms). When converting to the
11133 newer option scheme only '-location' is added here. That's fine (for
11134 backward compatibility) as any non-ambiguous prefix of a flag will be
11135 accepted, so '-l', '-loc', are now all accepted.
11136
11137 What this means is that, if in the future, we add any new flag here
11138 that starts with '-l' then this will break backward compatibility, so
11139 please, don't do that! */
11140
11141 static const gdb::option::option_def watch_option_defs[] = {
11142 gdb::option::flag_option_def<watch_options> {
11143 "location",
11144 [] (watch_options *opt) { return &opt->location; },
11145 N_("\
11146 This evaluates EXPRESSION and watches the memory to which is refers.\n\
11147 -l can be used as a short form of -location."),
11148 },
11149 };
11150
11151 /* Returns the option group used by 'watch', 'awatch', and 'rwatch'
11152 commands. */
11153
11154 static gdb::option::option_def_group
11155 make_watch_options_def_group (watch_options *opts)
11156 {
11157 return {{watch_option_defs}, opts};
11158 }
11159
11160 /* A helper function that looks for the "-location" argument and then
11161 calls watch_command_1. */
11162
11163 static void
11164 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
11165 {
11166 watch_options opts;
11167 auto grp = make_watch_options_def_group (&opts);
11168 gdb::option::process_options
11169 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
11170 if (arg != nullptr && *arg == '\0')
11171 arg = nullptr;
11172
11173 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
11174 }
11175
11176 /* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
11177 static void
11178 watch_command_completer (struct cmd_list_element *ignore,
11179 completion_tracker &tracker,
11180 const char *text, const char * /*word*/)
11181 {
11182 const auto group = make_watch_options_def_group (nullptr);
11183 if (gdb::option::complete_options
11184 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
11185 return;
11186
11187 const char *word = advance_to_expression_complete_word_point (tracker, text);
11188 expression_completer (ignore, tracker, text, word);
11189 }
11190
11191 static void
11192 watch_command (const char *arg, int from_tty)
11193 {
11194 watch_maybe_just_location (arg, hw_write, from_tty);
11195 }
11196
11197 void
11198 rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
11199 {
11200 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11201 }
11202
11203 static void
11204 rwatch_command (const char *arg, int from_tty)
11205 {
11206 watch_maybe_just_location (arg, hw_read, from_tty);
11207 }
11208
11209 void
11210 awatch_command_wrapper (const char *arg, int from_tty, bool internal)
11211 {
11212 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11213 }
11214
11215 static void
11216 awatch_command (const char *arg, int from_tty)
11217 {
11218 watch_maybe_just_location (arg, hw_access, from_tty);
11219 }
11220 \f
11221
11222 /* Data for the FSM that manages the until(location)/advance commands
11223 in infcmd.c. Here because it uses the mechanisms of
11224 breakpoints. */
11225
11226 struct until_break_fsm : public thread_fsm
11227 {
11228 /* The thread that was current when the command was executed. */
11229 int thread;
11230
11231 /* The breakpoint set at the return address in the caller frame,
11232 plus breakpoints at all the destination locations. */
11233 std::vector<breakpoint_up> breakpoints;
11234
11235 until_break_fsm (struct interp *cmd_interp, int thread,
11236 std::vector<breakpoint_up> &&breakpoints)
11237 : thread_fsm (cmd_interp),
11238 thread (thread),
11239 breakpoints (std::move (breakpoints))
11240 {
11241 }
11242
11243 void clean_up (struct thread_info *thread) override;
11244 bool should_stop (struct thread_info *thread) override;
11245 enum async_reply_reason do_async_reply_reason () override;
11246 };
11247
11248 /* Implementation of the 'should_stop' FSM method for the
11249 until(location)/advance commands. */
11250
11251 bool
11252 until_break_fsm::should_stop (struct thread_info *tp)
11253 {
11254 for (const breakpoint_up &bp : breakpoints)
11255 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11256 bp.get ()) != NULL)
11257 {
11258 set_finished ();
11259 break;
11260 }
11261
11262 return true;
11263 }
11264
11265 /* Implementation of the 'clean_up' FSM method for the
11266 until(location)/advance commands. */
11267
11268 void
11269 until_break_fsm::clean_up (struct thread_info *)
11270 {
11271 /* Clean up our temporary breakpoints. */
11272 breakpoints.clear ();
11273 delete_longjmp_breakpoint (thread);
11274 }
11275
11276 /* Implementation of the 'async_reply_reason' FSM method for the
11277 until(location)/advance commands. */
11278
11279 enum async_reply_reason
11280 until_break_fsm::do_async_reply_reason ()
11281 {
11282 return EXEC_ASYNC_LOCATION_REACHED;
11283 }
11284
11285 void
11286 until_break_command (const char *arg, int from_tty, int anywhere)
11287 {
11288 struct frame_info *frame;
11289 struct gdbarch *frame_gdbarch;
11290 struct frame_id stack_frame_id;
11291 struct frame_id caller_frame_id;
11292 int thread;
11293 struct thread_info *tp;
11294
11295 clear_proceed_status (0);
11296
11297 /* Set a breakpoint where the user wants it and at return from
11298 this function. */
11299
11300 event_location_up location = string_to_event_location (&arg, current_language);
11301
11302 std::vector<symtab_and_line> sals
11303 = (last_displayed_sal_is_valid ()
11304 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11305 get_last_displayed_symtab (),
11306 get_last_displayed_line ())
11307 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11308 NULL, NULL, 0));
11309
11310 if (sals.empty ())
11311 error (_("Couldn't get information on specified line."));
11312
11313 if (*arg)
11314 error (_("Junk at end of arguments."));
11315
11316 tp = inferior_thread ();
11317 thread = tp->global_num;
11318
11319 /* Note linespec handling above invalidates the frame chain.
11320 Installing a breakpoint also invalidates the frame chain (as it
11321 may need to switch threads), so do any frame handling before
11322 that. */
11323
11324 frame = get_selected_frame (NULL);
11325 frame_gdbarch = get_frame_arch (frame);
11326 stack_frame_id = get_stack_frame_id (frame);
11327 caller_frame_id = frame_unwind_caller_id (frame);
11328
11329 /* Keep within the current frame, or in frames called by the current
11330 one. */
11331
11332 std::vector<breakpoint_up> breakpoints;
11333
11334 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11335
11336 if (frame_id_p (caller_frame_id))
11337 {
11338 struct symtab_and_line sal2;
11339 struct gdbarch *caller_gdbarch;
11340
11341 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11342 sal2.pc = frame_unwind_caller_pc (frame);
11343 caller_gdbarch = frame_unwind_caller_arch (frame);
11344
11345 breakpoint_up caller_breakpoint
11346 = set_momentary_breakpoint (caller_gdbarch, sal2,
11347 caller_frame_id, bp_until);
11348 breakpoints.emplace_back (std::move (caller_breakpoint));
11349
11350 set_longjmp_breakpoint (tp, caller_frame_id);
11351 lj_deleter.emplace (thread);
11352 }
11353
11354 /* set_momentary_breakpoint could invalidate FRAME. */
11355 frame = NULL;
11356
11357 /* If the user told us to continue until a specified location, we
11358 don't specify a frame at which we need to stop. Otherwise,
11359 specify the selected frame, because we want to stop only at the
11360 very same frame. */
11361 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
11362
11363 for (symtab_and_line &sal : sals)
11364 {
11365 resolve_sal_pc (&sal);
11366
11367 breakpoint_up location_breakpoint
11368 = set_momentary_breakpoint (frame_gdbarch, sal,
11369 stop_frame_id, bp_until);
11370 breakpoints.emplace_back (std::move (location_breakpoint));
11371 }
11372
11373 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
11374 std::move (breakpoints));
11375
11376 if (lj_deleter)
11377 lj_deleter->release ();
11378
11379 proceed (-1, GDB_SIGNAL_DEFAULT);
11380 }
11381
11382 /* This function attempts to parse an optional "if <cond>" clause
11383 from the arg string. If one is not found, it returns NULL.
11384
11385 Else, it returns a pointer to the condition string. (It does not
11386 attempt to evaluate the string against a particular block.) And,
11387 it updates arg to point to the first character following the parsed
11388 if clause in the arg string. */
11389
11390 const char *
11391 ep_parse_optional_if_clause (const char **arg)
11392 {
11393 const char *cond_string;
11394
11395 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11396 return NULL;
11397
11398 /* Skip the "if" keyword. */
11399 (*arg) += 2;
11400
11401 /* Skip any extra leading whitespace, and record the start of the
11402 condition string. */
11403 *arg = skip_spaces (*arg);
11404 cond_string = *arg;
11405
11406 /* Assume that the condition occupies the remainder of the arg
11407 string. */
11408 (*arg) += strlen (cond_string);
11409
11410 return cond_string;
11411 }
11412
11413 /* Commands to deal with catching events, such as signals, exceptions,
11414 process start/exit, etc. */
11415
11416 typedef enum
11417 {
11418 catch_fork_temporary, catch_vfork_temporary,
11419 catch_fork_permanent, catch_vfork_permanent
11420 }
11421 catch_fork_kind;
11422
11423 static void
11424 catch_fork_command_1 (const char *arg, int from_tty,
11425 struct cmd_list_element *command)
11426 {
11427 struct gdbarch *gdbarch = get_current_arch ();
11428 const char *cond_string = NULL;
11429 catch_fork_kind fork_kind;
11430
11431 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11432 bool temp = (fork_kind == catch_fork_temporary
11433 || fork_kind == catch_vfork_temporary);
11434
11435 if (!arg)
11436 arg = "";
11437 arg = skip_spaces (arg);
11438
11439 /* The allowed syntax is:
11440 catch [v]fork
11441 catch [v]fork if <cond>
11442
11443 First, check if there's an if clause. */
11444 cond_string = ep_parse_optional_if_clause (&arg);
11445
11446 if ((*arg != '\0') && !isspace (*arg))
11447 error (_("Junk at end of arguments."));
11448
11449 /* If this target supports it, create a fork or vfork catchpoint
11450 and enable reporting of such events. */
11451 switch (fork_kind)
11452 {
11453 case catch_fork_temporary:
11454 case catch_fork_permanent:
11455 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
11456 &catch_fork_breakpoint_ops);
11457 break;
11458 case catch_vfork_temporary:
11459 case catch_vfork_permanent:
11460 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
11461 &catch_vfork_breakpoint_ops);
11462 break;
11463 default:
11464 error (_("unsupported or unknown fork kind; cannot catch it"));
11465 break;
11466 }
11467 }
11468
11469 static void
11470 catch_exec_command_1 (const char *arg, int from_tty,
11471 struct cmd_list_element *command)
11472 {
11473 struct gdbarch *gdbarch = get_current_arch ();
11474 const char *cond_string = NULL;
11475 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
11476
11477 if (!arg)
11478 arg = "";
11479 arg = skip_spaces (arg);
11480
11481 /* The allowed syntax is:
11482 catch exec
11483 catch exec if <cond>
11484
11485 First, check if there's an if clause. */
11486 cond_string = ep_parse_optional_if_clause (&arg);
11487
11488 if ((*arg != '\0') && !isspace (*arg))
11489 error (_("Junk at end of arguments."));
11490
11491 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11492 init_catchpoint (c.get (), gdbarch, temp, cond_string,
11493 &catch_exec_breakpoint_ops);
11494 c->exec_pathname = NULL;
11495
11496 install_breakpoint (0, std::move (c), 1);
11497 }
11498
11499 void
11500 init_ada_exception_breakpoint (struct breakpoint *b,
11501 struct gdbarch *gdbarch,
11502 struct symtab_and_line sal,
11503 const char *addr_string,
11504 const struct breakpoint_ops *ops,
11505 int tempflag,
11506 int enabled,
11507 int from_tty)
11508 {
11509 if (from_tty)
11510 {
11511 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11512 if (!loc_gdbarch)
11513 loc_gdbarch = gdbarch;
11514
11515 describe_other_breakpoints (loc_gdbarch,
11516 sal.pspace, sal.pc, sal.section, -1);
11517 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11518 version for exception catchpoints, because two catchpoints
11519 used for different exception names will use the same address.
11520 In this case, a "breakpoint ... also set at..." warning is
11521 unproductive. Besides, the warning phrasing is also a bit
11522 inappropriate, we should use the word catchpoint, and tell
11523 the user what type of catchpoint it is. The above is good
11524 enough for now, though. */
11525 }
11526
11527 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
11528
11529 b->enable_state = enabled ? bp_enabled : bp_disabled;
11530 b->disposition = tempflag ? disp_del : disp_donttouch;
11531 b->location = string_to_event_location (&addr_string,
11532 language_def (language_ada));
11533 b->language = language_ada;
11534 }
11535
11536 \f
11537
11538 /* Compare two breakpoints and return a strcmp-like result. */
11539
11540 static int
11541 compare_breakpoints (const breakpoint *a, const breakpoint *b)
11542 {
11543 uintptr_t ua = (uintptr_t) a;
11544 uintptr_t ub = (uintptr_t) b;
11545
11546 if (a->number < b->number)
11547 return -1;
11548 else if (a->number > b->number)
11549 return 1;
11550
11551 /* Now sort by address, in case we see, e..g, two breakpoints with
11552 the number 0. */
11553 if (ua < ub)
11554 return -1;
11555 return ua > ub ? 1 : 0;
11556 }
11557
11558 /* Delete breakpoints by address or line. */
11559
11560 static void
11561 clear_command (const char *arg, int from_tty)
11562 {
11563 struct breakpoint *b;
11564 int default_match;
11565
11566 std::vector<symtab_and_line> decoded_sals;
11567 symtab_and_line last_sal;
11568 gdb::array_view<symtab_and_line> sals;
11569 if (arg)
11570 {
11571 decoded_sals
11572 = decode_line_with_current_source (arg,
11573 (DECODE_LINE_FUNFIRSTLINE
11574 | DECODE_LINE_LIST_MODE));
11575 default_match = 0;
11576 sals = decoded_sals;
11577 }
11578 else
11579 {
11580 /* Set sal's line, symtab, pc, and pspace to the values
11581 corresponding to the last call to print_frame_info. If the
11582 codepoint is not valid, this will set all the fields to 0. */
11583 last_sal = get_last_displayed_sal ();
11584 if (last_sal.symtab == 0)
11585 error (_("No source file specified."));
11586
11587 default_match = 1;
11588 sals = last_sal;
11589 }
11590
11591 /* We don't call resolve_sal_pc here. That's not as bad as it
11592 seems, because all existing breakpoints typically have both
11593 file/line and pc set. So, if clear is given file/line, we can
11594 match this to existing breakpoint without obtaining pc at all.
11595
11596 We only support clearing given the address explicitly
11597 present in breakpoint table. Say, we've set breakpoint
11598 at file:line. There were several PC values for that file:line,
11599 due to optimization, all in one block.
11600
11601 We've picked one PC value. If "clear" is issued with another
11602 PC corresponding to the same file:line, the breakpoint won't
11603 be cleared. We probably can still clear the breakpoint, but
11604 since the other PC value is never presented to user, user
11605 can only find it by guessing, and it does not seem important
11606 to support that. */
11607
11608 /* For each line spec given, delete bps which correspond to it. Do
11609 it in two passes, solely to preserve the current behavior that
11610 from_tty is forced true if we delete more than one
11611 breakpoint. */
11612
11613 std::vector<struct breakpoint *> found;
11614 for (const auto &sal : sals)
11615 {
11616 const char *sal_fullname;
11617
11618 /* If exact pc given, clear bpts at that pc.
11619 If line given (pc == 0), clear all bpts on specified line.
11620 If defaulting, clear all bpts on default line
11621 or at default pc.
11622
11623 defaulting sal.pc != 0 tests to do
11624
11625 0 1 pc
11626 1 1 pc _and_ line
11627 0 0 line
11628 1 0 <can't happen> */
11629
11630 sal_fullname = (sal.symtab == NULL
11631 ? NULL : symtab_to_fullname (sal.symtab));
11632
11633 /* Find all matching breakpoints and add them to 'found'. */
11634 ALL_BREAKPOINTS (b)
11635 {
11636 int match = 0;
11637 /* Are we going to delete b? */
11638 if (b->type != bp_none && !is_watchpoint (b))
11639 {
11640 struct bp_location *loc = b->loc;
11641 for (; loc; loc = loc->next)
11642 {
11643 /* If the user specified file:line, don't allow a PC
11644 match. This matches historical gdb behavior. */
11645 int pc_match = (!sal.explicit_line
11646 && sal.pc
11647 && (loc->pspace == sal.pspace)
11648 && (loc->address == sal.pc)
11649 && (!section_is_overlay (loc->section)
11650 || loc->section == sal.section));
11651 int line_match = 0;
11652
11653 if ((default_match || sal.explicit_line)
11654 && loc->symtab != NULL
11655 && sal_fullname != NULL
11656 && sal.pspace == loc->pspace
11657 && loc->line_number == sal.line
11658 && filename_cmp (symtab_to_fullname (loc->symtab),
11659 sal_fullname) == 0)
11660 line_match = 1;
11661
11662 if (pc_match || line_match)
11663 {
11664 match = 1;
11665 break;
11666 }
11667 }
11668 }
11669
11670 if (match)
11671 found.push_back (b);
11672 }
11673 }
11674
11675 /* Now go thru the 'found' chain and delete them. */
11676 if (found.empty ())
11677 {
11678 if (arg)
11679 error (_("No breakpoint at %s."), arg);
11680 else
11681 error (_("No breakpoint at this line."));
11682 }
11683
11684 /* Remove duplicates from the vec. */
11685 std::sort (found.begin (), found.end (),
11686 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11687 {
11688 return compare_breakpoints (bp_a, bp_b) < 0;
11689 });
11690 found.erase (std::unique (found.begin (), found.end (),
11691 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11692 {
11693 return compare_breakpoints (bp_a, bp_b) == 0;
11694 }),
11695 found.end ());
11696
11697 if (found.size () > 1)
11698 from_tty = 1; /* Always report if deleted more than one. */
11699 if (from_tty)
11700 {
11701 if (found.size () == 1)
11702 printf_unfiltered (_("Deleted breakpoint "));
11703 else
11704 printf_unfiltered (_("Deleted breakpoints "));
11705 }
11706
11707 for (breakpoint *iter : found)
11708 {
11709 if (from_tty)
11710 printf_unfiltered ("%d ", iter->number);
11711 delete_breakpoint (iter);
11712 }
11713 if (from_tty)
11714 putchar_unfiltered ('\n');
11715 }
11716 \f
11717 /* Delete breakpoint in BS if they are `delete' breakpoints and
11718 all breakpoints that are marked for deletion, whether hit or not.
11719 This is called after any breakpoint is hit, or after errors. */
11720
11721 void
11722 breakpoint_auto_delete (bpstat bs)
11723 {
11724 struct breakpoint *b, *b_tmp;
11725
11726 for (; bs; bs = bs->next)
11727 if (bs->breakpoint_at
11728 && bs->breakpoint_at->disposition == disp_del
11729 && bs->stop)
11730 delete_breakpoint (bs->breakpoint_at);
11731
11732 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11733 {
11734 if (b->disposition == disp_del_at_next_stop)
11735 delete_breakpoint (b);
11736 }
11737 }
11738
11739 /* A comparison function for bp_location AP and BP being interfaced to
11740 std::sort. Sort elements primarily by their ADDRESS (no matter what
11741 bl_address_is_meaningful says), secondarily by ordering first
11742 permanent elements and terciarily just ensuring the array is sorted
11743 stable way despite std::sort being an unstable algorithm. */
11744
11745 static int
11746 bp_location_is_less_than (const bp_location *a, const bp_location *b)
11747 {
11748 if (a->address != b->address)
11749 return a->address < b->address;
11750
11751 /* Sort locations at the same address by their pspace number, keeping
11752 locations of the same inferior (in a multi-inferior environment)
11753 grouped. */
11754
11755 if (a->pspace->num != b->pspace->num)
11756 return a->pspace->num < b->pspace->num;
11757
11758 /* Sort permanent breakpoints first. */
11759 if (a->permanent != b->permanent)
11760 return a->permanent > b->permanent;
11761
11762 /* Sort by type in order to make duplicate determination easier.
11763 See update_global_location_list. This is kept in sync with
11764 breakpoint_locations_match. */
11765 if (a->loc_type < b->loc_type)
11766 return true;
11767
11768 /* Likewise, for range-breakpoints, sort by length. */
11769 if (a->loc_type == bp_loc_hardware_breakpoint
11770 && b->loc_type == bp_loc_hardware_breakpoint
11771 && a->length < b->length)
11772 return true;
11773
11774 /* Make the internal GDB representation stable across GDB runs
11775 where A and B memory inside GDB can differ. Breakpoint locations of
11776 the same type at the same address can be sorted in arbitrary order. */
11777
11778 if (a->owner->number != b->owner->number)
11779 return a->owner->number < b->owner->number;
11780
11781 return a < b;
11782 }
11783
11784 /* Set bp_locations_placed_address_before_address_max and
11785 bp_locations_shadow_len_after_address_max according to the current
11786 content of the bp_locations array. */
11787
11788 static void
11789 bp_locations_target_extensions_update (void)
11790 {
11791 struct bp_location *bl, **blp_tmp;
11792
11793 bp_locations_placed_address_before_address_max = 0;
11794 bp_locations_shadow_len_after_address_max = 0;
11795
11796 ALL_BP_LOCATIONS (bl, blp_tmp)
11797 {
11798 CORE_ADDR start, end, addr;
11799
11800 if (!bp_location_has_shadow (bl))
11801 continue;
11802
11803 start = bl->target_info.placed_address;
11804 end = start + bl->target_info.shadow_len;
11805
11806 gdb_assert (bl->address >= start);
11807 addr = bl->address - start;
11808 if (addr > bp_locations_placed_address_before_address_max)
11809 bp_locations_placed_address_before_address_max = addr;
11810
11811 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11812
11813 gdb_assert (bl->address < end);
11814 addr = end - bl->address;
11815 if (addr > bp_locations_shadow_len_after_address_max)
11816 bp_locations_shadow_len_after_address_max = addr;
11817 }
11818 }
11819
11820 /* Download tracepoint locations if they haven't been. */
11821
11822 static void
11823 download_tracepoint_locations (void)
11824 {
11825 struct breakpoint *b;
11826 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11827
11828 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11829
11830 ALL_TRACEPOINTS (b)
11831 {
11832 struct bp_location *bl;
11833 struct tracepoint *t;
11834 int bp_location_downloaded = 0;
11835
11836 if ((b->type == bp_fast_tracepoint
11837 ? !may_insert_fast_tracepoints
11838 : !may_insert_tracepoints))
11839 continue;
11840
11841 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11842 {
11843 if (target_can_download_tracepoint ())
11844 can_download_tracepoint = TRIBOOL_TRUE;
11845 else
11846 can_download_tracepoint = TRIBOOL_FALSE;
11847 }
11848
11849 if (can_download_tracepoint == TRIBOOL_FALSE)
11850 break;
11851
11852 for (bl = b->loc; bl; bl = bl->next)
11853 {
11854 /* In tracepoint, locations are _never_ duplicated, so
11855 should_be_inserted is equivalent to
11856 unduplicated_should_be_inserted. */
11857 if (!should_be_inserted (bl) || bl->inserted)
11858 continue;
11859
11860 switch_to_program_space_and_thread (bl->pspace);
11861
11862 target_download_tracepoint (bl);
11863
11864 bl->inserted = 1;
11865 bp_location_downloaded = 1;
11866 }
11867 t = (struct tracepoint *) b;
11868 t->number_on_target = b->number;
11869 if (bp_location_downloaded)
11870 gdb::observers::breakpoint_modified.notify (b);
11871 }
11872 }
11873
11874 /* Swap the insertion/duplication state between two locations. */
11875
11876 static void
11877 swap_insertion (struct bp_location *left, struct bp_location *right)
11878 {
11879 const int left_inserted = left->inserted;
11880 const int left_duplicate = left->duplicate;
11881 const int left_needs_update = left->needs_update;
11882 const struct bp_target_info left_target_info = left->target_info;
11883
11884 /* Locations of tracepoints can never be duplicated. */
11885 if (is_tracepoint (left->owner))
11886 gdb_assert (!left->duplicate);
11887 if (is_tracepoint (right->owner))
11888 gdb_assert (!right->duplicate);
11889
11890 left->inserted = right->inserted;
11891 left->duplicate = right->duplicate;
11892 left->needs_update = right->needs_update;
11893 left->target_info = right->target_info;
11894 right->inserted = left_inserted;
11895 right->duplicate = left_duplicate;
11896 right->needs_update = left_needs_update;
11897 right->target_info = left_target_info;
11898 }
11899
11900 /* Force the re-insertion of the locations at ADDRESS. This is called
11901 once a new/deleted/modified duplicate location is found and we are evaluating
11902 conditions on the target's side. Such conditions need to be updated on
11903 the target. */
11904
11905 static void
11906 force_breakpoint_reinsertion (struct bp_location *bl)
11907 {
11908 struct bp_location **locp = NULL, **loc2p;
11909 struct bp_location *loc;
11910 CORE_ADDR address = 0;
11911 int pspace_num;
11912
11913 address = bl->address;
11914 pspace_num = bl->pspace->num;
11915
11916 /* This is only meaningful if the target is
11917 evaluating conditions and if the user has
11918 opted for condition evaluation on the target's
11919 side. */
11920 if (gdb_evaluates_breakpoint_condition_p ()
11921 || !target_supports_evaluation_of_breakpoint_conditions ())
11922 return;
11923
11924 /* Flag all breakpoint locations with this address and
11925 the same program space as the location
11926 as "its condition has changed". We need to
11927 update the conditions on the target's side. */
11928 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11929 {
11930 loc = *loc2p;
11931
11932 if (!is_breakpoint (loc->owner)
11933 || pspace_num != loc->pspace->num)
11934 continue;
11935
11936 /* Flag the location appropriately. We use a different state to
11937 let everyone know that we already updated the set of locations
11938 with addr bl->address and program space bl->pspace. This is so
11939 we don't have to keep calling these functions just to mark locations
11940 that have already been marked. */
11941 loc->condition_changed = condition_updated;
11942
11943 /* Free the agent expression bytecode as well. We will compute
11944 it later on. */
11945 loc->cond_bytecode.reset ();
11946 }
11947 }
11948
11949 /* Called whether new breakpoints are created, or existing breakpoints
11950 deleted, to update the global location list and recompute which
11951 locations are duplicate of which.
11952
11953 The INSERT_MODE flag determines whether locations may not, may, or
11954 shall be inserted now. See 'enum ugll_insert_mode' for more
11955 info. */
11956
11957 static void
11958 update_global_location_list (enum ugll_insert_mode insert_mode)
11959 {
11960 struct breakpoint *b;
11961 struct bp_location **locp, *loc;
11962 /* Last breakpoint location address that was marked for update. */
11963 CORE_ADDR last_addr = 0;
11964 /* Last breakpoint location program space that was marked for update. */
11965 int last_pspace_num = -1;
11966
11967 /* Used in the duplicates detection below. When iterating over all
11968 bp_locations, points to the first bp_location of a given address.
11969 Breakpoints and watchpoints of different types are never
11970 duplicates of each other. Keep one pointer for each type of
11971 breakpoint/watchpoint, so we only need to loop over all locations
11972 once. */
11973 struct bp_location *bp_loc_first; /* breakpoint */
11974 struct bp_location *wp_loc_first; /* hardware watchpoint */
11975 struct bp_location *awp_loc_first; /* access watchpoint */
11976 struct bp_location *rwp_loc_first; /* read watchpoint */
11977
11978 /* Saved former bp_locations array which we compare against the newly
11979 built bp_locations from the current state of ALL_BREAKPOINTS. */
11980 struct bp_location **old_locp;
11981 unsigned old_locations_count;
11982 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
11983
11984 old_locations_count = bp_locations_count;
11985 bp_locations = NULL;
11986 bp_locations_count = 0;
11987
11988 ALL_BREAKPOINTS (b)
11989 for (loc = b->loc; loc; loc = loc->next)
11990 bp_locations_count++;
11991
11992 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11993 locp = bp_locations;
11994 ALL_BREAKPOINTS (b)
11995 for (loc = b->loc; loc; loc = loc->next)
11996 *locp++ = loc;
11997
11998 /* See if we need to "upgrade" a software breakpoint to a hardware
11999 breakpoint. Do this before deciding whether locations are
12000 duplicates. Also do this before sorting because sorting order
12001 depends on location type. */
12002 for (locp = bp_locations;
12003 locp < bp_locations + bp_locations_count;
12004 locp++)
12005 {
12006 loc = *locp;
12007 if (!loc->inserted && should_be_inserted (loc))
12008 handle_automatic_hardware_breakpoints (loc);
12009 }
12010
12011 std::sort (bp_locations, bp_locations + bp_locations_count,
12012 bp_location_is_less_than);
12013
12014 bp_locations_target_extensions_update ();
12015
12016 /* Identify bp_location instances that are no longer present in the
12017 new list, and therefore should be freed. Note that it's not
12018 necessary that those locations should be removed from inferior --
12019 if there's another location at the same address (previously
12020 marked as duplicate), we don't need to remove/insert the
12021 location.
12022
12023 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12024 and former bp_location array state respectively. */
12025
12026 locp = bp_locations;
12027 for (old_locp = old_locations.get ();
12028 old_locp < old_locations.get () + old_locations_count;
12029 old_locp++)
12030 {
12031 struct bp_location *old_loc = *old_locp;
12032 struct bp_location **loc2p;
12033
12034 /* Tells if 'old_loc' is found among the new locations. If
12035 not, we have to free it. */
12036 int found_object = 0;
12037 /* Tells if the location should remain inserted in the target. */
12038 int keep_in_target = 0;
12039 int removed = 0;
12040
12041 /* Skip LOCP entries which will definitely never be needed.
12042 Stop either at or being the one matching OLD_LOC. */
12043 while (locp < bp_locations + bp_locations_count
12044 && (*locp)->address < old_loc->address)
12045 locp++;
12046
12047 for (loc2p = locp;
12048 (loc2p < bp_locations + bp_locations_count
12049 && (*loc2p)->address == old_loc->address);
12050 loc2p++)
12051 {
12052 /* Check if this is a new/duplicated location or a duplicated
12053 location that had its condition modified. If so, we want to send
12054 its condition to the target if evaluation of conditions is taking
12055 place there. */
12056 if ((*loc2p)->condition_changed == condition_modified
12057 && (last_addr != old_loc->address
12058 || last_pspace_num != old_loc->pspace->num))
12059 {
12060 force_breakpoint_reinsertion (*loc2p);
12061 last_pspace_num = old_loc->pspace->num;
12062 }
12063
12064 if (*loc2p == old_loc)
12065 found_object = 1;
12066 }
12067
12068 /* We have already handled this address, update it so that we don't
12069 have to go through updates again. */
12070 last_addr = old_loc->address;
12071
12072 /* Target-side condition evaluation: Handle deleted locations. */
12073 if (!found_object)
12074 force_breakpoint_reinsertion (old_loc);
12075
12076 /* If this location is no longer present, and inserted, look if
12077 there's maybe a new location at the same address. If so,
12078 mark that one inserted, and don't remove this one. This is
12079 needed so that we don't have a time window where a breakpoint
12080 at certain location is not inserted. */
12081
12082 if (old_loc->inserted)
12083 {
12084 /* If the location is inserted now, we might have to remove
12085 it. */
12086
12087 if (found_object && should_be_inserted (old_loc))
12088 {
12089 /* The location is still present in the location list,
12090 and still should be inserted. Don't do anything. */
12091 keep_in_target = 1;
12092 }
12093 else
12094 {
12095 /* This location still exists, but it won't be kept in the
12096 target since it may have been disabled. We proceed to
12097 remove its target-side condition. */
12098
12099 /* The location is either no longer present, or got
12100 disabled. See if there's another location at the
12101 same address, in which case we don't need to remove
12102 this one from the target. */
12103
12104 /* OLD_LOC comes from existing struct breakpoint. */
12105 if (bl_address_is_meaningful (old_loc))
12106 {
12107 for (loc2p = locp;
12108 (loc2p < bp_locations + bp_locations_count
12109 && (*loc2p)->address == old_loc->address);
12110 loc2p++)
12111 {
12112 struct bp_location *loc2 = *loc2p;
12113
12114 if (loc2 == old_loc)
12115 continue;
12116
12117 if (breakpoint_locations_match (loc2, old_loc))
12118 {
12119 /* Read watchpoint locations are switched to
12120 access watchpoints, if the former are not
12121 supported, but the latter are. */
12122 if (is_hardware_watchpoint (old_loc->owner))
12123 {
12124 gdb_assert (is_hardware_watchpoint (loc2->owner));
12125 loc2->watchpoint_type = old_loc->watchpoint_type;
12126 }
12127
12128 /* loc2 is a duplicated location. We need to check
12129 if it should be inserted in case it will be
12130 unduplicated. */
12131 if (unduplicated_should_be_inserted (loc2))
12132 {
12133 swap_insertion (old_loc, loc2);
12134 keep_in_target = 1;
12135 break;
12136 }
12137 }
12138 }
12139 }
12140 }
12141
12142 if (!keep_in_target)
12143 {
12144 if (remove_breakpoint (old_loc))
12145 {
12146 /* This is just about all we can do. We could keep
12147 this location on the global list, and try to
12148 remove it next time, but there's no particular
12149 reason why we will succeed next time.
12150
12151 Note that at this point, old_loc->owner is still
12152 valid, as delete_breakpoint frees the breakpoint
12153 only after calling us. */
12154 printf_filtered (_("warning: Error removing "
12155 "breakpoint %d\n"),
12156 old_loc->owner->number);
12157 }
12158 removed = 1;
12159 }
12160 }
12161
12162 if (!found_object)
12163 {
12164 if (removed && target_is_non_stop_p ()
12165 && need_moribund_for_location_type (old_loc))
12166 {
12167 /* This location was removed from the target. In
12168 non-stop mode, a race condition is possible where
12169 we've removed a breakpoint, but stop events for that
12170 breakpoint are already queued and will arrive later.
12171 We apply an heuristic to be able to distinguish such
12172 SIGTRAPs from other random SIGTRAPs: we keep this
12173 breakpoint location for a bit, and will retire it
12174 after we see some number of events. The theory here
12175 is that reporting of events should, "on the average",
12176 be fair, so after a while we'll see events from all
12177 threads that have anything of interest, and no longer
12178 need to keep this breakpoint location around. We
12179 don't hold locations forever so to reduce chances of
12180 mistaking a non-breakpoint SIGTRAP for a breakpoint
12181 SIGTRAP.
12182
12183 The heuristic failing can be disastrous on
12184 decr_pc_after_break targets.
12185
12186 On decr_pc_after_break targets, like e.g., x86-linux,
12187 if we fail to recognize a late breakpoint SIGTRAP,
12188 because events_till_retirement has reached 0 too
12189 soon, we'll fail to do the PC adjustment, and report
12190 a random SIGTRAP to the user. When the user resumes
12191 the inferior, it will most likely immediately crash
12192 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12193 corrupted, because of being resumed e.g., in the
12194 middle of a multi-byte instruction, or skipped a
12195 one-byte instruction. This was actually seen happen
12196 on native x86-linux, and should be less rare on
12197 targets that do not support new thread events, like
12198 remote, due to the heuristic depending on
12199 thread_count.
12200
12201 Mistaking a random SIGTRAP for a breakpoint trap
12202 causes similar symptoms (PC adjustment applied when
12203 it shouldn't), but then again, playing with SIGTRAPs
12204 behind the debugger's back is asking for trouble.
12205
12206 Since hardware watchpoint traps are always
12207 distinguishable from other traps, so we don't need to
12208 apply keep hardware watchpoint moribund locations
12209 around. We simply always ignore hardware watchpoint
12210 traps we can no longer explain. */
12211
12212 process_stratum_target *proc_target = nullptr;
12213 for (inferior *inf : all_inferiors ())
12214 if (inf->pspace == old_loc->pspace)
12215 {
12216 proc_target = inf->process_target ();
12217 break;
12218 }
12219 if (proc_target != nullptr)
12220 old_loc->events_till_retirement
12221 = 3 * (thread_count (proc_target) + 1);
12222 else
12223 old_loc->events_till_retirement = 1;
12224 old_loc->owner = NULL;
12225
12226 moribund_locations.push_back (old_loc);
12227 }
12228 else
12229 {
12230 old_loc->owner = NULL;
12231 decref_bp_location (&old_loc);
12232 }
12233 }
12234 }
12235
12236 /* Rescan breakpoints at the same address and section, marking the
12237 first one as "first" and any others as "duplicates". This is so
12238 that the bpt instruction is only inserted once. If we have a
12239 permanent breakpoint at the same place as BPT, make that one the
12240 official one, and the rest as duplicates. Permanent breakpoints
12241 are sorted first for the same address.
12242
12243 Do the same for hardware watchpoints, but also considering the
12244 watchpoint's type (regular/access/read) and length. */
12245
12246 bp_loc_first = NULL;
12247 wp_loc_first = NULL;
12248 awp_loc_first = NULL;
12249 rwp_loc_first = NULL;
12250 ALL_BP_LOCATIONS (loc, locp)
12251 {
12252 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12253 non-NULL. */
12254 struct bp_location **loc_first_p;
12255 b = loc->owner;
12256
12257 if (!unduplicated_should_be_inserted (loc)
12258 || !bl_address_is_meaningful (loc)
12259 /* Don't detect duplicate for tracepoint locations because they are
12260 never duplicated. See the comments in field `duplicate' of
12261 `struct bp_location'. */
12262 || is_tracepoint (b))
12263 {
12264 /* Clear the condition modification flag. */
12265 loc->condition_changed = condition_unchanged;
12266 continue;
12267 }
12268
12269 if (b->type == bp_hardware_watchpoint)
12270 loc_first_p = &wp_loc_first;
12271 else if (b->type == bp_read_watchpoint)
12272 loc_first_p = &rwp_loc_first;
12273 else if (b->type == bp_access_watchpoint)
12274 loc_first_p = &awp_loc_first;
12275 else
12276 loc_first_p = &bp_loc_first;
12277
12278 if (*loc_first_p == NULL
12279 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12280 || !breakpoint_locations_match (loc, *loc_first_p))
12281 {
12282 *loc_first_p = loc;
12283 loc->duplicate = 0;
12284
12285 if (is_breakpoint (loc->owner) && loc->condition_changed)
12286 {
12287 loc->needs_update = 1;
12288 /* Clear the condition modification flag. */
12289 loc->condition_changed = condition_unchanged;
12290 }
12291 continue;
12292 }
12293
12294
12295 /* This and the above ensure the invariant that the first location
12296 is not duplicated, and is the inserted one.
12297 All following are marked as duplicated, and are not inserted. */
12298 if (loc->inserted)
12299 swap_insertion (loc, *loc_first_p);
12300 loc->duplicate = 1;
12301
12302 /* Clear the condition modification flag. */
12303 loc->condition_changed = condition_unchanged;
12304 }
12305
12306 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12307 {
12308 if (insert_mode != UGLL_DONT_INSERT)
12309 insert_breakpoint_locations ();
12310 else
12311 {
12312 /* Even though the caller told us to not insert new
12313 locations, we may still need to update conditions on the
12314 target's side of breakpoints that were already inserted
12315 if the target is evaluating breakpoint conditions. We
12316 only update conditions for locations that are marked
12317 "needs_update". */
12318 update_inserted_breakpoint_locations ();
12319 }
12320 }
12321
12322 if (insert_mode != UGLL_DONT_INSERT)
12323 download_tracepoint_locations ();
12324 }
12325
12326 void
12327 breakpoint_retire_moribund (void)
12328 {
12329 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12330 {
12331 struct bp_location *loc = moribund_locations[ix];
12332 if (--(loc->events_till_retirement) == 0)
12333 {
12334 decref_bp_location (&loc);
12335 unordered_remove (moribund_locations, ix);
12336 --ix;
12337 }
12338 }
12339 }
12340
12341 static void
12342 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12343 {
12344
12345 try
12346 {
12347 update_global_location_list (insert_mode);
12348 }
12349 catch (const gdb_exception_error &e)
12350 {
12351 }
12352 }
12353
12354 /* Clear BKP from a BPS. */
12355
12356 static void
12357 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12358 {
12359 bpstat bs;
12360
12361 for (bs = bps; bs; bs = bs->next)
12362 if (bs->breakpoint_at == bpt)
12363 {
12364 bs->breakpoint_at = NULL;
12365 bs->old_val = NULL;
12366 /* bs->commands will be freed later. */
12367 }
12368 }
12369
12370 /* Callback for iterate_over_threads. */
12371 static int
12372 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12373 {
12374 struct breakpoint *bpt = (struct breakpoint *) data;
12375
12376 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12377 return 0;
12378 }
12379
12380 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12381 callbacks. */
12382
12383 static void
12384 say_where (struct breakpoint *b)
12385 {
12386 struct value_print_options opts;
12387
12388 get_user_print_options (&opts);
12389
12390 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12391 single string. */
12392 if (b->loc == NULL)
12393 {
12394 /* For pending locations, the output differs slightly based
12395 on b->extra_string. If this is non-NULL, it contains either
12396 a condition or dprintf arguments. */
12397 if (b->extra_string == NULL)
12398 {
12399 printf_filtered (_(" (%s) pending."),
12400 event_location_to_string (b->location.get ()));
12401 }
12402 else if (b->type == bp_dprintf)
12403 {
12404 printf_filtered (_(" (%s,%s) pending."),
12405 event_location_to_string (b->location.get ()),
12406 b->extra_string);
12407 }
12408 else
12409 {
12410 printf_filtered (_(" (%s %s) pending."),
12411 event_location_to_string (b->location.get ()),
12412 b->extra_string);
12413 }
12414 }
12415 else
12416 {
12417 if (opts.addressprint || b->loc->symtab == NULL)
12418 printf_filtered (" at %ps",
12419 styled_string (address_style.style (),
12420 paddress (b->loc->gdbarch,
12421 b->loc->address)));
12422 if (b->loc->symtab != NULL)
12423 {
12424 /* If there is a single location, we can print the location
12425 more nicely. */
12426 if (b->loc->next == NULL)
12427 {
12428 const char *filename
12429 = symtab_to_filename_for_display (b->loc->symtab);
12430 printf_filtered (": file %ps, line %d.",
12431 styled_string (file_name_style.style (),
12432 filename),
12433 b->loc->line_number);
12434 }
12435 else
12436 /* This is not ideal, but each location may have a
12437 different file name, and this at least reflects the
12438 real situation somewhat. */
12439 printf_filtered (": %s.",
12440 event_location_to_string (b->location.get ()));
12441 }
12442
12443 if (b->loc->next)
12444 {
12445 struct bp_location *loc = b->loc;
12446 int n = 0;
12447 for (; loc; loc = loc->next)
12448 ++n;
12449 printf_filtered (" (%d locations)", n);
12450 }
12451 }
12452 }
12453
12454 bp_location::~bp_location ()
12455 {
12456 xfree (function_name);
12457 }
12458
12459 /* Destructor for the breakpoint base class. */
12460
12461 breakpoint::~breakpoint ()
12462 {
12463 xfree (this->cond_string);
12464 xfree (this->extra_string);
12465 }
12466
12467 static struct bp_location *
12468 base_breakpoint_allocate_location (struct breakpoint *self)
12469 {
12470 return new bp_location (self);
12471 }
12472
12473 static void
12474 base_breakpoint_re_set (struct breakpoint *b)
12475 {
12476 /* Nothing to re-set. */
12477 }
12478
12479 #define internal_error_pure_virtual_called() \
12480 gdb_assert_not_reached ("pure virtual function called")
12481
12482 static int
12483 base_breakpoint_insert_location (struct bp_location *bl)
12484 {
12485 internal_error_pure_virtual_called ();
12486 }
12487
12488 static int
12489 base_breakpoint_remove_location (struct bp_location *bl,
12490 enum remove_bp_reason reason)
12491 {
12492 internal_error_pure_virtual_called ();
12493 }
12494
12495 static int
12496 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12497 const address_space *aspace,
12498 CORE_ADDR bp_addr,
12499 const struct target_waitstatus *ws)
12500 {
12501 internal_error_pure_virtual_called ();
12502 }
12503
12504 static void
12505 base_breakpoint_check_status (bpstat bs)
12506 {
12507 /* Always stop. */
12508 }
12509
12510 /* A "works_in_software_mode" breakpoint_ops method that just internal
12511 errors. */
12512
12513 static int
12514 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12515 {
12516 internal_error_pure_virtual_called ();
12517 }
12518
12519 /* A "resources_needed" breakpoint_ops method that just internal
12520 errors. */
12521
12522 static int
12523 base_breakpoint_resources_needed (const struct bp_location *bl)
12524 {
12525 internal_error_pure_virtual_called ();
12526 }
12527
12528 static enum print_stop_action
12529 base_breakpoint_print_it (bpstat bs)
12530 {
12531 internal_error_pure_virtual_called ();
12532 }
12533
12534 static void
12535 base_breakpoint_print_one_detail (const struct breakpoint *self,
12536 struct ui_out *uiout)
12537 {
12538 /* nothing */
12539 }
12540
12541 static void
12542 base_breakpoint_print_mention (struct breakpoint *b)
12543 {
12544 internal_error_pure_virtual_called ();
12545 }
12546
12547 static void
12548 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12549 {
12550 internal_error_pure_virtual_called ();
12551 }
12552
12553 static void
12554 base_breakpoint_create_sals_from_location
12555 (struct event_location *location,
12556 struct linespec_result *canonical,
12557 enum bptype type_wanted)
12558 {
12559 internal_error_pure_virtual_called ();
12560 }
12561
12562 static void
12563 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12564 struct linespec_result *c,
12565 gdb::unique_xmalloc_ptr<char> cond_string,
12566 gdb::unique_xmalloc_ptr<char> extra_string,
12567 enum bptype type_wanted,
12568 enum bpdisp disposition,
12569 int thread,
12570 int task, int ignore_count,
12571 const struct breakpoint_ops *o,
12572 int from_tty, int enabled,
12573 int internal, unsigned flags)
12574 {
12575 internal_error_pure_virtual_called ();
12576 }
12577
12578 static std::vector<symtab_and_line>
12579 base_breakpoint_decode_location (struct breakpoint *b,
12580 struct event_location *location,
12581 struct program_space *search_pspace)
12582 {
12583 internal_error_pure_virtual_called ();
12584 }
12585
12586 /* The default 'explains_signal' method. */
12587
12588 static int
12589 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12590 {
12591 return 1;
12592 }
12593
12594 /* The default "after_condition_true" method. */
12595
12596 static void
12597 base_breakpoint_after_condition_true (struct bpstats *bs)
12598 {
12599 /* Nothing to do. */
12600 }
12601
12602 struct breakpoint_ops base_breakpoint_ops =
12603 {
12604 base_breakpoint_allocate_location,
12605 base_breakpoint_re_set,
12606 base_breakpoint_insert_location,
12607 base_breakpoint_remove_location,
12608 base_breakpoint_breakpoint_hit,
12609 base_breakpoint_check_status,
12610 base_breakpoint_resources_needed,
12611 base_breakpoint_works_in_software_mode,
12612 base_breakpoint_print_it,
12613 NULL,
12614 base_breakpoint_print_one_detail,
12615 base_breakpoint_print_mention,
12616 base_breakpoint_print_recreate,
12617 base_breakpoint_create_sals_from_location,
12618 base_breakpoint_create_breakpoints_sal,
12619 base_breakpoint_decode_location,
12620 base_breakpoint_explains_signal,
12621 base_breakpoint_after_condition_true,
12622 };
12623
12624 /* Default breakpoint_ops methods. */
12625
12626 static void
12627 bkpt_re_set (struct breakpoint *b)
12628 {
12629 /* FIXME: is this still reachable? */
12630 if (breakpoint_event_location_empty_p (b))
12631 {
12632 /* Anything without a location can't be re-set. */
12633 delete_breakpoint (b);
12634 return;
12635 }
12636
12637 breakpoint_re_set_default (b);
12638 }
12639
12640 static int
12641 bkpt_insert_location (struct bp_location *bl)
12642 {
12643 CORE_ADDR addr = bl->target_info.reqstd_address;
12644
12645 bl->target_info.kind = breakpoint_kind (bl, &addr);
12646 bl->target_info.placed_address = addr;
12647
12648 if (bl->loc_type == bp_loc_hardware_breakpoint)
12649 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12650 else
12651 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12652 }
12653
12654 static int
12655 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12656 {
12657 if (bl->loc_type == bp_loc_hardware_breakpoint)
12658 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12659 else
12660 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12661 }
12662
12663 static int
12664 bkpt_breakpoint_hit (const struct bp_location *bl,
12665 const address_space *aspace, CORE_ADDR bp_addr,
12666 const struct target_waitstatus *ws)
12667 {
12668 if (ws->kind != TARGET_WAITKIND_STOPPED
12669 || ws->value.sig != GDB_SIGNAL_TRAP)
12670 return 0;
12671
12672 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12673 aspace, bp_addr))
12674 return 0;
12675
12676 if (overlay_debugging /* unmapped overlay section */
12677 && section_is_overlay (bl->section)
12678 && !section_is_mapped (bl->section))
12679 return 0;
12680
12681 return 1;
12682 }
12683
12684 static int
12685 dprintf_breakpoint_hit (const struct bp_location *bl,
12686 const address_space *aspace, CORE_ADDR bp_addr,
12687 const struct target_waitstatus *ws)
12688 {
12689 if (dprintf_style == dprintf_style_agent
12690 && target_can_run_breakpoint_commands ())
12691 {
12692 /* An agent-style dprintf never causes a stop. If we see a trap
12693 for this address it must be for a breakpoint that happens to
12694 be set at the same address. */
12695 return 0;
12696 }
12697
12698 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12699 }
12700
12701 static int
12702 bkpt_resources_needed (const struct bp_location *bl)
12703 {
12704 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12705
12706 return 1;
12707 }
12708
12709 static enum print_stop_action
12710 bkpt_print_it (bpstat bs)
12711 {
12712 struct breakpoint *b;
12713 const struct bp_location *bl;
12714 int bp_temp;
12715 struct ui_out *uiout = current_uiout;
12716
12717 gdb_assert (bs->bp_location_at != NULL);
12718
12719 bl = bs->bp_location_at.get ();
12720 b = bs->breakpoint_at;
12721
12722 bp_temp = b->disposition == disp_del;
12723 if (bl->address != bl->requested_address)
12724 breakpoint_adjustment_warning (bl->requested_address,
12725 bl->address,
12726 b->number, 1);
12727 annotate_breakpoint (b->number);
12728 maybe_print_thread_hit_breakpoint (uiout);
12729
12730 if (uiout->is_mi_like_p ())
12731 {
12732 uiout->field_string ("reason",
12733 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12734 uiout->field_string ("disp", bpdisp_text (b->disposition));
12735 }
12736 if (bp_temp)
12737 uiout->message ("Temporary breakpoint %pF, ",
12738 signed_field ("bkptno", b->number));
12739 else
12740 uiout->message ("Breakpoint %pF, ",
12741 signed_field ("bkptno", b->number));
12742
12743 return PRINT_SRC_AND_LOC;
12744 }
12745
12746 static void
12747 bkpt_print_mention (struct breakpoint *b)
12748 {
12749 if (current_uiout->is_mi_like_p ())
12750 return;
12751
12752 switch (b->type)
12753 {
12754 case bp_breakpoint:
12755 case bp_gnu_ifunc_resolver:
12756 if (b->disposition == disp_del)
12757 printf_filtered (_("Temporary breakpoint"));
12758 else
12759 printf_filtered (_("Breakpoint"));
12760 printf_filtered (_(" %d"), b->number);
12761 if (b->type == bp_gnu_ifunc_resolver)
12762 printf_filtered (_(" at gnu-indirect-function resolver"));
12763 break;
12764 case bp_hardware_breakpoint:
12765 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12766 break;
12767 case bp_dprintf:
12768 printf_filtered (_("Dprintf %d"), b->number);
12769 break;
12770 }
12771
12772 say_where (b);
12773 }
12774
12775 static void
12776 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12777 {
12778 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12779 fprintf_unfiltered (fp, "tbreak");
12780 else if (tp->type == bp_breakpoint)
12781 fprintf_unfiltered (fp, "break");
12782 else if (tp->type == bp_hardware_breakpoint
12783 && tp->disposition == disp_del)
12784 fprintf_unfiltered (fp, "thbreak");
12785 else if (tp->type == bp_hardware_breakpoint)
12786 fprintf_unfiltered (fp, "hbreak");
12787 else
12788 internal_error (__FILE__, __LINE__,
12789 _("unhandled breakpoint type %d"), (int) tp->type);
12790
12791 fprintf_unfiltered (fp, " %s",
12792 event_location_to_string (tp->location.get ()));
12793
12794 /* Print out extra_string if this breakpoint is pending. It might
12795 contain, for example, conditions that were set by the user. */
12796 if (tp->loc == NULL && tp->extra_string != NULL)
12797 fprintf_unfiltered (fp, " %s", tp->extra_string);
12798
12799 print_recreate_thread (tp, fp);
12800 }
12801
12802 static void
12803 bkpt_create_sals_from_location (struct event_location *location,
12804 struct linespec_result *canonical,
12805 enum bptype type_wanted)
12806 {
12807 create_sals_from_location_default (location, canonical, type_wanted);
12808 }
12809
12810 static void
12811 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12812 struct linespec_result *canonical,
12813 gdb::unique_xmalloc_ptr<char> cond_string,
12814 gdb::unique_xmalloc_ptr<char> extra_string,
12815 enum bptype type_wanted,
12816 enum bpdisp disposition,
12817 int thread,
12818 int task, int ignore_count,
12819 const struct breakpoint_ops *ops,
12820 int from_tty, int enabled,
12821 int internal, unsigned flags)
12822 {
12823 create_breakpoints_sal_default (gdbarch, canonical,
12824 std::move (cond_string),
12825 std::move (extra_string),
12826 type_wanted,
12827 disposition, thread, task,
12828 ignore_count, ops, from_tty,
12829 enabled, internal, flags);
12830 }
12831
12832 static std::vector<symtab_and_line>
12833 bkpt_decode_location (struct breakpoint *b,
12834 struct event_location *location,
12835 struct program_space *search_pspace)
12836 {
12837 return decode_location_default (b, location, search_pspace);
12838 }
12839
12840 /* Virtual table for internal breakpoints. */
12841
12842 static void
12843 internal_bkpt_re_set (struct breakpoint *b)
12844 {
12845 switch (b->type)
12846 {
12847 /* Delete overlay event and longjmp master breakpoints; they
12848 will be reset later by breakpoint_re_set. */
12849 case bp_overlay_event:
12850 case bp_longjmp_master:
12851 case bp_std_terminate_master:
12852 case bp_exception_master:
12853 delete_breakpoint (b);
12854 break;
12855
12856 /* This breakpoint is special, it's set up when the inferior
12857 starts and we really don't want to touch it. */
12858 case bp_shlib_event:
12859
12860 /* Like bp_shlib_event, this breakpoint type is special. Once
12861 it is set up, we do not want to touch it. */
12862 case bp_thread_event:
12863 break;
12864 }
12865 }
12866
12867 static void
12868 internal_bkpt_check_status (bpstat bs)
12869 {
12870 if (bs->breakpoint_at->type == bp_shlib_event)
12871 {
12872 /* If requested, stop when the dynamic linker notifies GDB of
12873 events. This allows the user to get control and place
12874 breakpoints in initializer routines for dynamically loaded
12875 objects (among other things). */
12876 bs->stop = stop_on_solib_events;
12877 bs->print = stop_on_solib_events;
12878 }
12879 else
12880 bs->stop = 0;
12881 }
12882
12883 static enum print_stop_action
12884 internal_bkpt_print_it (bpstat bs)
12885 {
12886 struct breakpoint *b;
12887
12888 b = bs->breakpoint_at;
12889
12890 switch (b->type)
12891 {
12892 case bp_shlib_event:
12893 /* Did we stop because the user set the stop_on_solib_events
12894 variable? (If so, we report this as a generic, "Stopped due
12895 to shlib event" message.) */
12896 print_solib_event (0);
12897 break;
12898
12899 case bp_thread_event:
12900 /* Not sure how we will get here.
12901 GDB should not stop for these breakpoints. */
12902 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12903 break;
12904
12905 case bp_overlay_event:
12906 /* By analogy with the thread event, GDB should not stop for these. */
12907 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12908 break;
12909
12910 case bp_longjmp_master:
12911 /* These should never be enabled. */
12912 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12913 break;
12914
12915 case bp_std_terminate_master:
12916 /* These should never be enabled. */
12917 printf_filtered (_("std::terminate Master Breakpoint: "
12918 "gdb should not stop!\n"));
12919 break;
12920
12921 case bp_exception_master:
12922 /* These should never be enabled. */
12923 printf_filtered (_("Exception Master Breakpoint: "
12924 "gdb should not stop!\n"));
12925 break;
12926 }
12927
12928 return PRINT_NOTHING;
12929 }
12930
12931 static void
12932 internal_bkpt_print_mention (struct breakpoint *b)
12933 {
12934 /* Nothing to mention. These breakpoints are internal. */
12935 }
12936
12937 /* Virtual table for momentary breakpoints */
12938
12939 static void
12940 momentary_bkpt_re_set (struct breakpoint *b)
12941 {
12942 /* Keep temporary breakpoints, which can be encountered when we step
12943 over a dlopen call and solib_add is resetting the breakpoints.
12944 Otherwise these should have been blown away via the cleanup chain
12945 or by breakpoint_init_inferior when we rerun the executable. */
12946 }
12947
12948 static void
12949 momentary_bkpt_check_status (bpstat bs)
12950 {
12951 /* Nothing. The point of these breakpoints is causing a stop. */
12952 }
12953
12954 static enum print_stop_action
12955 momentary_bkpt_print_it (bpstat bs)
12956 {
12957 return PRINT_UNKNOWN;
12958 }
12959
12960 static void
12961 momentary_bkpt_print_mention (struct breakpoint *b)
12962 {
12963 /* Nothing to mention. These breakpoints are internal. */
12964 }
12965
12966 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12967
12968 It gets cleared already on the removal of the first one of such placed
12969 breakpoints. This is OK as they get all removed altogether. */
12970
12971 longjmp_breakpoint::~longjmp_breakpoint ()
12972 {
12973 thread_info *tp = find_thread_global_id (this->thread);
12974
12975 if (tp != NULL)
12976 tp->initiating_frame = null_frame_id;
12977 }
12978
12979 /* Specific methods for probe breakpoints. */
12980
12981 static int
12982 bkpt_probe_insert_location (struct bp_location *bl)
12983 {
12984 int v = bkpt_insert_location (bl);
12985
12986 if (v == 0)
12987 {
12988 /* The insertion was successful, now let's set the probe's semaphore
12989 if needed. */
12990 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
12991 }
12992
12993 return v;
12994 }
12995
12996 static int
12997 bkpt_probe_remove_location (struct bp_location *bl,
12998 enum remove_bp_reason reason)
12999 {
13000 /* Let's clear the semaphore before removing the location. */
13001 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
13002
13003 return bkpt_remove_location (bl, reason);
13004 }
13005
13006 static void
13007 bkpt_probe_create_sals_from_location (struct event_location *location,
13008 struct linespec_result *canonical,
13009 enum bptype type_wanted)
13010 {
13011 struct linespec_sals lsal;
13012
13013 lsal.sals = parse_probes (location, NULL, canonical);
13014 lsal.canonical
13015 = xstrdup (event_location_to_string (canonical->location.get ()));
13016 canonical->lsals.push_back (std::move (lsal));
13017 }
13018
13019 static std::vector<symtab_and_line>
13020 bkpt_probe_decode_location (struct breakpoint *b,
13021 struct event_location *location,
13022 struct program_space *search_pspace)
13023 {
13024 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
13025 if (sals.empty ())
13026 error (_("probe not found"));
13027 return sals;
13028 }
13029
13030 /* The breakpoint_ops structure to be used in tracepoints. */
13031
13032 static void
13033 tracepoint_re_set (struct breakpoint *b)
13034 {
13035 breakpoint_re_set_default (b);
13036 }
13037
13038 static int
13039 tracepoint_breakpoint_hit (const struct bp_location *bl,
13040 const address_space *aspace, CORE_ADDR bp_addr,
13041 const struct target_waitstatus *ws)
13042 {
13043 /* By definition, the inferior does not report stops at
13044 tracepoints. */
13045 return 0;
13046 }
13047
13048 static void
13049 tracepoint_print_one_detail (const struct breakpoint *self,
13050 struct ui_out *uiout)
13051 {
13052 struct tracepoint *tp = (struct tracepoint *) self;
13053 if (!tp->static_trace_marker_id.empty ())
13054 {
13055 gdb_assert (self->type == bp_static_tracepoint);
13056
13057 uiout->message ("\tmarker id is %pF\n",
13058 string_field ("static-tracepoint-marker-string-id",
13059 tp->static_trace_marker_id.c_str ()));
13060 }
13061 }
13062
13063 static void
13064 tracepoint_print_mention (struct breakpoint *b)
13065 {
13066 if (current_uiout->is_mi_like_p ())
13067 return;
13068
13069 switch (b->type)
13070 {
13071 case bp_tracepoint:
13072 printf_filtered (_("Tracepoint"));
13073 printf_filtered (_(" %d"), b->number);
13074 break;
13075 case bp_fast_tracepoint:
13076 printf_filtered (_("Fast tracepoint"));
13077 printf_filtered (_(" %d"), b->number);
13078 break;
13079 case bp_static_tracepoint:
13080 printf_filtered (_("Static tracepoint"));
13081 printf_filtered (_(" %d"), b->number);
13082 break;
13083 default:
13084 internal_error (__FILE__, __LINE__,
13085 _("unhandled tracepoint type %d"), (int) b->type);
13086 }
13087
13088 say_where (b);
13089 }
13090
13091 static void
13092 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13093 {
13094 struct tracepoint *tp = (struct tracepoint *) self;
13095
13096 if (self->type == bp_fast_tracepoint)
13097 fprintf_unfiltered (fp, "ftrace");
13098 else if (self->type == bp_static_tracepoint)
13099 fprintf_unfiltered (fp, "strace");
13100 else if (self->type == bp_tracepoint)
13101 fprintf_unfiltered (fp, "trace");
13102 else
13103 internal_error (__FILE__, __LINE__,
13104 _("unhandled tracepoint type %d"), (int) self->type);
13105
13106 fprintf_unfiltered (fp, " %s",
13107 event_location_to_string (self->location.get ()));
13108 print_recreate_thread (self, fp);
13109
13110 if (tp->pass_count)
13111 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13112 }
13113
13114 static void
13115 tracepoint_create_sals_from_location (struct event_location *location,
13116 struct linespec_result *canonical,
13117 enum bptype type_wanted)
13118 {
13119 create_sals_from_location_default (location, canonical, type_wanted);
13120 }
13121
13122 static void
13123 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13124 struct linespec_result *canonical,
13125 gdb::unique_xmalloc_ptr<char> cond_string,
13126 gdb::unique_xmalloc_ptr<char> extra_string,
13127 enum bptype type_wanted,
13128 enum bpdisp disposition,
13129 int thread,
13130 int task, int ignore_count,
13131 const struct breakpoint_ops *ops,
13132 int from_tty, int enabled,
13133 int internal, unsigned flags)
13134 {
13135 create_breakpoints_sal_default (gdbarch, canonical,
13136 std::move (cond_string),
13137 std::move (extra_string),
13138 type_wanted,
13139 disposition, thread, task,
13140 ignore_count, ops, from_tty,
13141 enabled, internal, flags);
13142 }
13143
13144 static std::vector<symtab_and_line>
13145 tracepoint_decode_location (struct breakpoint *b,
13146 struct event_location *location,
13147 struct program_space *search_pspace)
13148 {
13149 return decode_location_default (b, location, search_pspace);
13150 }
13151
13152 struct breakpoint_ops tracepoint_breakpoint_ops;
13153
13154 /* Virtual table for tracepoints on static probes. */
13155
13156 static void
13157 tracepoint_probe_create_sals_from_location
13158 (struct event_location *location,
13159 struct linespec_result *canonical,
13160 enum bptype type_wanted)
13161 {
13162 /* We use the same method for breakpoint on probes. */
13163 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13164 }
13165
13166 static std::vector<symtab_and_line>
13167 tracepoint_probe_decode_location (struct breakpoint *b,
13168 struct event_location *location,
13169 struct program_space *search_pspace)
13170 {
13171 /* We use the same method for breakpoint on probes. */
13172 return bkpt_probe_decode_location (b, location, search_pspace);
13173 }
13174
13175 /* Dprintf breakpoint_ops methods. */
13176
13177 static void
13178 dprintf_re_set (struct breakpoint *b)
13179 {
13180 breakpoint_re_set_default (b);
13181
13182 /* extra_string should never be non-NULL for dprintf. */
13183 gdb_assert (b->extra_string != NULL);
13184
13185 /* 1 - connect to target 1, that can run breakpoint commands.
13186 2 - create a dprintf, which resolves fine.
13187 3 - disconnect from target 1
13188 4 - connect to target 2, that can NOT run breakpoint commands.
13189
13190 After steps #3/#4, you'll want the dprintf command list to
13191 be updated, because target 1 and 2 may well return different
13192 answers for target_can_run_breakpoint_commands().
13193 Given absence of finer grained resetting, we get to do
13194 it all the time. */
13195 if (b->extra_string != NULL)
13196 update_dprintf_command_list (b);
13197 }
13198
13199 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13200
13201 static void
13202 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13203 {
13204 fprintf_unfiltered (fp, "dprintf %s,%s",
13205 event_location_to_string (tp->location.get ()),
13206 tp->extra_string);
13207 print_recreate_thread (tp, fp);
13208 }
13209
13210 /* Implement the "after_condition_true" breakpoint_ops method for
13211 dprintf.
13212
13213 dprintf's are implemented with regular commands in their command
13214 list, but we run the commands here instead of before presenting the
13215 stop to the user, as dprintf's don't actually cause a stop. This
13216 also makes it so that the commands of multiple dprintfs at the same
13217 address are all handled. */
13218
13219 static void
13220 dprintf_after_condition_true (struct bpstats *bs)
13221 {
13222 struct bpstats tmp_bs;
13223 struct bpstats *tmp_bs_p = &tmp_bs;
13224
13225 /* dprintf's never cause a stop. This wasn't set in the
13226 check_status hook instead because that would make the dprintf's
13227 condition not be evaluated. */
13228 bs->stop = 0;
13229
13230 /* Run the command list here. Take ownership of it instead of
13231 copying. We never want these commands to run later in
13232 bpstat_do_actions, if a breakpoint that causes a stop happens to
13233 be set at same address as this dprintf, or even if running the
13234 commands here throws. */
13235 tmp_bs.commands = bs->commands;
13236 bs->commands = NULL;
13237
13238 bpstat_do_actions_1 (&tmp_bs_p);
13239
13240 /* 'tmp_bs.commands' will usually be NULL by now, but
13241 bpstat_do_actions_1 may return early without processing the whole
13242 list. */
13243 }
13244
13245 /* The breakpoint_ops structure to be used on static tracepoints with
13246 markers (`-m'). */
13247
13248 static void
13249 strace_marker_create_sals_from_location (struct event_location *location,
13250 struct linespec_result *canonical,
13251 enum bptype type_wanted)
13252 {
13253 struct linespec_sals lsal;
13254 const char *arg_start, *arg;
13255
13256 arg = arg_start = get_linespec_location (location)->spec_string;
13257 lsal.sals = decode_static_tracepoint_spec (&arg);
13258
13259 std::string str (arg_start, arg - arg_start);
13260 const char *ptr = str.c_str ();
13261 canonical->location
13262 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
13263
13264 lsal.canonical
13265 = xstrdup (event_location_to_string (canonical->location.get ()));
13266 canonical->lsals.push_back (std::move (lsal));
13267 }
13268
13269 static void
13270 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13271 struct linespec_result *canonical,
13272 gdb::unique_xmalloc_ptr<char> cond_string,
13273 gdb::unique_xmalloc_ptr<char> extra_string,
13274 enum bptype type_wanted,
13275 enum bpdisp disposition,
13276 int thread,
13277 int task, int ignore_count,
13278 const struct breakpoint_ops *ops,
13279 int from_tty, int enabled,
13280 int internal, unsigned flags)
13281 {
13282 const linespec_sals &lsal = canonical->lsals[0];
13283
13284 /* If the user is creating a static tracepoint by marker id
13285 (strace -m MARKER_ID), then store the sals index, so that
13286 breakpoint_re_set can try to match up which of the newly
13287 found markers corresponds to this one, and, don't try to
13288 expand multiple locations for each sal, given than SALS
13289 already should contain all sals for MARKER_ID. */
13290
13291 for (size_t i = 0; i < lsal.sals.size (); i++)
13292 {
13293 event_location_up location
13294 = copy_event_location (canonical->location.get ());
13295
13296 std::unique_ptr<tracepoint> tp (new tracepoint ());
13297 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
13298 std::move (location), NULL,
13299 std::move (cond_string),
13300 std::move (extra_string),
13301 type_wanted, disposition,
13302 thread, task, ignore_count, ops,
13303 from_tty, enabled, internal, flags,
13304 canonical->special_display);
13305 /* Given that its possible to have multiple markers with
13306 the same string id, if the user is creating a static
13307 tracepoint by marker id ("strace -m MARKER_ID"), then
13308 store the sals index, so that breakpoint_re_set can
13309 try to match up which of the newly found markers
13310 corresponds to this one */
13311 tp->static_trace_marker_id_idx = i;
13312
13313 install_breakpoint (internal, std::move (tp), 0);
13314 }
13315 }
13316
13317 static std::vector<symtab_and_line>
13318 strace_marker_decode_location (struct breakpoint *b,
13319 struct event_location *location,
13320 struct program_space *search_pspace)
13321 {
13322 struct tracepoint *tp = (struct tracepoint *) b;
13323 const char *s = get_linespec_location (location)->spec_string;
13324
13325 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13326 if (sals.size () > tp->static_trace_marker_id_idx)
13327 {
13328 sals[0] = sals[tp->static_trace_marker_id_idx];
13329 sals.resize (1);
13330 return sals;
13331 }
13332 else
13333 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
13334 }
13335
13336 static struct breakpoint_ops strace_marker_breakpoint_ops;
13337
13338 static int
13339 strace_marker_p (struct breakpoint *b)
13340 {
13341 return b->ops == &strace_marker_breakpoint_ops;
13342 }
13343
13344 /* Delete a breakpoint and clean up all traces of it in the data
13345 structures. */
13346
13347 void
13348 delete_breakpoint (struct breakpoint *bpt)
13349 {
13350 struct breakpoint *b;
13351
13352 gdb_assert (bpt != NULL);
13353
13354 /* Has this bp already been deleted? This can happen because
13355 multiple lists can hold pointers to bp's. bpstat lists are
13356 especial culprits.
13357
13358 One example of this happening is a watchpoint's scope bp. When
13359 the scope bp triggers, we notice that the watchpoint is out of
13360 scope, and delete it. We also delete its scope bp. But the
13361 scope bp is marked "auto-deleting", and is already on a bpstat.
13362 That bpstat is then checked for auto-deleting bp's, which are
13363 deleted.
13364
13365 A real solution to this problem might involve reference counts in
13366 bp's, and/or giving them pointers back to their referencing
13367 bpstat's, and teaching delete_breakpoint to only free a bp's
13368 storage when no more references were extent. A cheaper bandaid
13369 was chosen. */
13370 if (bpt->type == bp_none)
13371 return;
13372
13373 /* At least avoid this stale reference until the reference counting
13374 of breakpoints gets resolved. */
13375 if (bpt->related_breakpoint != bpt)
13376 {
13377 struct breakpoint *related;
13378 struct watchpoint *w;
13379
13380 if (bpt->type == bp_watchpoint_scope)
13381 w = (struct watchpoint *) bpt->related_breakpoint;
13382 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13383 w = (struct watchpoint *) bpt;
13384 else
13385 w = NULL;
13386 if (w != NULL)
13387 watchpoint_del_at_next_stop (w);
13388
13389 /* Unlink bpt from the bpt->related_breakpoint ring. */
13390 for (related = bpt; related->related_breakpoint != bpt;
13391 related = related->related_breakpoint);
13392 related->related_breakpoint = bpt->related_breakpoint;
13393 bpt->related_breakpoint = bpt;
13394 }
13395
13396 /* watch_command_1 creates a watchpoint but only sets its number if
13397 update_watchpoint succeeds in creating its bp_locations. If there's
13398 a problem in that process, we'll be asked to delete the half-created
13399 watchpoint. In that case, don't announce the deletion. */
13400 if (bpt->number)
13401 gdb::observers::breakpoint_deleted.notify (bpt);
13402
13403 if (breakpoint_chain == bpt)
13404 breakpoint_chain = bpt->next;
13405
13406 ALL_BREAKPOINTS (b)
13407 if (b->next == bpt)
13408 {
13409 b->next = bpt->next;
13410 break;
13411 }
13412
13413 /* Be sure no bpstat's are pointing at the breakpoint after it's
13414 been freed. */
13415 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13416 in all threads for now. Note that we cannot just remove bpstats
13417 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13418 commands are associated with the bpstat; if we remove it here,
13419 then the later call to bpstat_do_actions (&stop_bpstat); in
13420 event-top.c won't do anything, and temporary breakpoints with
13421 commands won't work. */
13422
13423 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13424
13425 /* Now that breakpoint is removed from breakpoint list, update the
13426 global location list. This will remove locations that used to
13427 belong to this breakpoint. Do this before freeing the breakpoint
13428 itself, since remove_breakpoint looks at location's owner. It
13429 might be better design to have location completely
13430 self-contained, but it's not the case now. */
13431 update_global_location_list (UGLL_DONT_INSERT);
13432
13433 /* On the chance that someone will soon try again to delete this
13434 same bp, we mark it as deleted before freeing its storage. */
13435 bpt->type = bp_none;
13436 delete bpt;
13437 }
13438
13439 /* Iterator function to call a user-provided callback function once
13440 for each of B and its related breakpoints. */
13441
13442 static void
13443 iterate_over_related_breakpoints (struct breakpoint *b,
13444 gdb::function_view<void (breakpoint *)> function)
13445 {
13446 struct breakpoint *related;
13447
13448 related = b;
13449 do
13450 {
13451 struct breakpoint *next;
13452
13453 /* FUNCTION may delete RELATED. */
13454 next = related->related_breakpoint;
13455
13456 if (next == related)
13457 {
13458 /* RELATED is the last ring entry. */
13459 function (related);
13460
13461 /* FUNCTION may have deleted it, so we'd never reach back to
13462 B. There's nothing left to do anyway, so just break
13463 out. */
13464 break;
13465 }
13466 else
13467 function (related);
13468
13469 related = next;
13470 }
13471 while (related != b);
13472 }
13473
13474 static void
13475 delete_command (const char *arg, int from_tty)
13476 {
13477 struct breakpoint *b, *b_tmp;
13478
13479 dont_repeat ();
13480
13481 if (arg == 0)
13482 {
13483 int breaks_to_delete = 0;
13484
13485 /* Delete all breakpoints if no argument. Do not delete
13486 internal breakpoints, these have to be deleted with an
13487 explicit breakpoint number argument. */
13488 ALL_BREAKPOINTS (b)
13489 if (user_breakpoint_p (b))
13490 {
13491 breaks_to_delete = 1;
13492 break;
13493 }
13494
13495 /* Ask user only if there are some breakpoints to delete. */
13496 if (!from_tty
13497 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13498 {
13499 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13500 if (user_breakpoint_p (b))
13501 delete_breakpoint (b);
13502 }
13503 }
13504 else
13505 map_breakpoint_numbers
13506 (arg, [&] (breakpoint *br)
13507 {
13508 iterate_over_related_breakpoints (br, delete_breakpoint);
13509 });
13510 }
13511
13512 /* Return true if all locations of B bound to PSPACE are pending. If
13513 PSPACE is NULL, all locations of all program spaces are
13514 considered. */
13515
13516 static int
13517 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13518 {
13519 struct bp_location *loc;
13520
13521 for (loc = b->loc; loc != NULL; loc = loc->next)
13522 if ((pspace == NULL
13523 || loc->pspace == pspace)
13524 && !loc->shlib_disabled
13525 && !loc->pspace->executing_startup)
13526 return 0;
13527 return 1;
13528 }
13529
13530 /* Subroutine of update_breakpoint_locations to simplify it.
13531 Return non-zero if multiple fns in list LOC have the same name.
13532 Null names are ignored. */
13533
13534 static int
13535 ambiguous_names_p (struct bp_location *loc)
13536 {
13537 struct bp_location *l;
13538 htab_up htab (htab_create_alloc (13, htab_hash_string, streq_hash, NULL,
13539 xcalloc, xfree));
13540
13541 for (l = loc; l != NULL; l = l->next)
13542 {
13543 const char **slot;
13544 const char *name = l->function_name;
13545
13546 /* Allow for some names to be NULL, ignore them. */
13547 if (name == NULL)
13548 continue;
13549
13550 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
13551 INSERT);
13552 /* NOTE: We can assume slot != NULL here because xcalloc never
13553 returns NULL. */
13554 if (*slot != NULL)
13555 return 1;
13556 *slot = name;
13557 }
13558
13559 return 0;
13560 }
13561
13562 /* When symbols change, it probably means the sources changed as well,
13563 and it might mean the static tracepoint markers are no longer at
13564 the same address or line numbers they used to be at last we
13565 checked. Losing your static tracepoints whenever you rebuild is
13566 undesirable. This function tries to resync/rematch gdb static
13567 tracepoints with the markers on the target, for static tracepoints
13568 that have not been set by marker id. Static tracepoint that have
13569 been set by marker id are reset by marker id in breakpoint_re_set.
13570 The heuristic is:
13571
13572 1) For a tracepoint set at a specific address, look for a marker at
13573 the old PC. If one is found there, assume to be the same marker.
13574 If the name / string id of the marker found is different from the
13575 previous known name, assume that means the user renamed the marker
13576 in the sources, and output a warning.
13577
13578 2) For a tracepoint set at a given line number, look for a marker
13579 at the new address of the old line number. If one is found there,
13580 assume to be the same marker. If the name / string id of the
13581 marker found is different from the previous known name, assume that
13582 means the user renamed the marker in the sources, and output a
13583 warning.
13584
13585 3) If a marker is no longer found at the same address or line, it
13586 may mean the marker no longer exists. But it may also just mean
13587 the code changed a bit. Maybe the user added a few lines of code
13588 that made the marker move up or down (in line number terms). Ask
13589 the target for info about the marker with the string id as we knew
13590 it. If found, update line number and address in the matching
13591 static tracepoint. This will get confused if there's more than one
13592 marker with the same ID (possible in UST, although unadvised
13593 precisely because it confuses tools). */
13594
13595 static struct symtab_and_line
13596 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13597 {
13598 struct tracepoint *tp = (struct tracepoint *) b;
13599 struct static_tracepoint_marker marker;
13600 CORE_ADDR pc;
13601
13602 pc = sal.pc;
13603 if (sal.line)
13604 find_line_pc (sal.symtab, sal.line, &pc);
13605
13606 if (target_static_tracepoint_marker_at (pc, &marker))
13607 {
13608 if (tp->static_trace_marker_id != marker.str_id)
13609 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13610 b->number, tp->static_trace_marker_id.c_str (),
13611 marker.str_id.c_str ());
13612
13613 tp->static_trace_marker_id = std::move (marker.str_id);
13614
13615 return sal;
13616 }
13617
13618 /* Old marker wasn't found on target at lineno. Try looking it up
13619 by string ID. */
13620 if (!sal.explicit_pc
13621 && sal.line != 0
13622 && sal.symtab != NULL
13623 && !tp->static_trace_marker_id.empty ())
13624 {
13625 std::vector<static_tracepoint_marker> markers
13626 = target_static_tracepoint_markers_by_strid
13627 (tp->static_trace_marker_id.c_str ());
13628
13629 if (!markers.empty ())
13630 {
13631 struct symbol *sym;
13632 struct static_tracepoint_marker *tpmarker;
13633 struct ui_out *uiout = current_uiout;
13634 struct explicit_location explicit_loc;
13635
13636 tpmarker = &markers[0];
13637
13638 tp->static_trace_marker_id = std::move (tpmarker->str_id);
13639
13640 warning (_("marker for static tracepoint %d (%s) not "
13641 "found at previous line number"),
13642 b->number, tp->static_trace_marker_id.c_str ());
13643
13644 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
13645 sym = find_pc_sect_function (tpmarker->address, NULL);
13646 uiout->text ("Now in ");
13647 if (sym)
13648 {
13649 uiout->field_string ("func", sym->print_name (),
13650 function_name_style.style ());
13651 uiout->text (" at ");
13652 }
13653 uiout->field_string ("file",
13654 symtab_to_filename_for_display (sal2.symtab),
13655 file_name_style.style ());
13656 uiout->text (":");
13657
13658 if (uiout->is_mi_like_p ())
13659 {
13660 const char *fullname = symtab_to_fullname (sal2.symtab);
13661
13662 uiout->field_string ("fullname", fullname);
13663 }
13664
13665 uiout->field_signed ("line", sal2.line);
13666 uiout->text ("\n");
13667
13668 b->loc->line_number = sal2.line;
13669 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13670
13671 b->location.reset (NULL);
13672 initialize_explicit_location (&explicit_loc);
13673 explicit_loc.source_filename
13674 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13675 explicit_loc.line_offset.offset = b->loc->line_number;
13676 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13677 b->location = new_explicit_location (&explicit_loc);
13678
13679 /* Might be nice to check if function changed, and warn if
13680 so. */
13681 }
13682 }
13683 return sal;
13684 }
13685
13686 /* Returns 1 iff locations A and B are sufficiently same that
13687 we don't need to report breakpoint as changed. */
13688
13689 static int
13690 locations_are_equal (struct bp_location *a, struct bp_location *b)
13691 {
13692 while (a && b)
13693 {
13694 if (a->address != b->address)
13695 return 0;
13696
13697 if (a->shlib_disabled != b->shlib_disabled)
13698 return 0;
13699
13700 if (a->enabled != b->enabled)
13701 return 0;
13702
13703 if (a->disabled_by_cond != b->disabled_by_cond)
13704 return 0;
13705
13706 a = a->next;
13707 b = b->next;
13708 }
13709
13710 if ((a == NULL) != (b == NULL))
13711 return 0;
13712
13713 return 1;
13714 }
13715
13716 /* Split all locations of B that are bound to PSPACE out of B's
13717 location list to a separate list and return that list's head. If
13718 PSPACE is NULL, hoist out all locations of B. */
13719
13720 static struct bp_location *
13721 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13722 {
13723 struct bp_location head;
13724 struct bp_location *i = b->loc;
13725 struct bp_location **i_link = &b->loc;
13726 struct bp_location *hoisted = &head;
13727
13728 if (pspace == NULL)
13729 {
13730 i = b->loc;
13731 b->loc = NULL;
13732 return i;
13733 }
13734
13735 head.next = NULL;
13736
13737 while (i != NULL)
13738 {
13739 if (i->pspace == pspace)
13740 {
13741 *i_link = i->next;
13742 i->next = NULL;
13743 hoisted->next = i;
13744 hoisted = i;
13745 }
13746 else
13747 i_link = &i->next;
13748 i = *i_link;
13749 }
13750
13751 return head.next;
13752 }
13753
13754 /* Create new breakpoint locations for B (a hardware or software
13755 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13756 zero, then B is a ranged breakpoint. Only recreates locations for
13757 FILTER_PSPACE. Locations of other program spaces are left
13758 untouched. */
13759
13760 void
13761 update_breakpoint_locations (struct breakpoint *b,
13762 struct program_space *filter_pspace,
13763 gdb::array_view<const symtab_and_line> sals,
13764 gdb::array_view<const symtab_and_line> sals_end)
13765 {
13766 struct bp_location *existing_locations;
13767
13768 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
13769 {
13770 /* Ranged breakpoints have only one start location and one end
13771 location. */
13772 b->enable_state = bp_disabled;
13773 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13774 "multiple locations found\n"),
13775 b->number);
13776 return;
13777 }
13778
13779 /* If there's no new locations, and all existing locations are
13780 pending, don't do anything. This optimizes the common case where
13781 all locations are in the same shared library, that was unloaded.
13782 We'd like to retain the location, so that when the library is
13783 loaded again, we don't loose the enabled/disabled status of the
13784 individual locations. */
13785 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
13786 return;
13787
13788 existing_locations = hoist_existing_locations (b, filter_pspace);
13789
13790 for (const auto &sal : sals)
13791 {
13792 struct bp_location *new_loc;
13793
13794 switch_to_program_space_and_thread (sal.pspace);
13795
13796 new_loc = add_location_to_breakpoint (b, &sal);
13797
13798 /* Reparse conditions, they might contain references to the
13799 old symtab. */
13800 if (b->cond_string != NULL)
13801 {
13802 const char *s;
13803
13804 s = b->cond_string;
13805 try
13806 {
13807 new_loc->cond = parse_exp_1 (&s, sal.pc,
13808 block_for_pc (sal.pc),
13809 0);
13810 }
13811 catch (const gdb_exception_error &e)
13812 {
13813 new_loc->disabled_by_cond = true;
13814 }
13815 }
13816
13817 if (!sals_end.empty ())
13818 {
13819 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13820
13821 new_loc->length = end - sals[0].pc + 1;
13822 }
13823 }
13824
13825 /* If possible, carry over 'disable' status from existing
13826 breakpoints. */
13827 {
13828 struct bp_location *e = existing_locations;
13829 /* If there are multiple breakpoints with the same function name,
13830 e.g. for inline functions, comparing function names won't work.
13831 Instead compare pc addresses; this is just a heuristic as things
13832 may have moved, but in practice it gives the correct answer
13833 often enough until a better solution is found. */
13834 int have_ambiguous_names = ambiguous_names_p (b->loc);
13835
13836 for (; e; e = e->next)
13837 {
13838 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
13839 {
13840 struct bp_location *l = b->loc;
13841 if (have_ambiguous_names)
13842 {
13843 for (; l; l = l->next)
13844 {
13845 /* Ignore software vs hardware location type at
13846 this point, because with "set breakpoint
13847 auto-hw", after a re-set, locations that were
13848 hardware can end up as software, or vice versa.
13849 As mentioned above, this is an heuristic and in
13850 practice should give the correct answer often
13851 enough. */
13852 if (breakpoint_locations_match (e, l, true))
13853 {
13854 l->enabled = e->enabled;
13855 l->disabled_by_cond = e->disabled_by_cond;
13856 break;
13857 }
13858 }
13859 }
13860 else
13861 {
13862 for (; l; l = l->next)
13863 if (l->function_name
13864 && strcmp (e->function_name, l->function_name) == 0)
13865 {
13866 l->enabled = e->enabled;
13867 l->disabled_by_cond = e->disabled_by_cond;
13868 break;
13869 }
13870 }
13871 }
13872 }
13873 }
13874
13875 if (!locations_are_equal (existing_locations, b->loc))
13876 gdb::observers::breakpoint_modified.notify (b);
13877 }
13878
13879 /* Find the SaL locations corresponding to the given LOCATION.
13880 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13881
13882 static std::vector<symtab_and_line>
13883 location_to_sals (struct breakpoint *b, struct event_location *location,
13884 struct program_space *search_pspace, int *found)
13885 {
13886 struct gdb_exception exception;
13887
13888 gdb_assert (b->ops != NULL);
13889
13890 std::vector<symtab_and_line> sals;
13891
13892 try
13893 {
13894 sals = b->ops->decode_location (b, location, search_pspace);
13895 }
13896 catch (gdb_exception_error &e)
13897 {
13898 int not_found_and_ok = 0;
13899
13900 /* For pending breakpoints, it's expected that parsing will
13901 fail until the right shared library is loaded. User has
13902 already told to create pending breakpoints and don't need
13903 extra messages. If breakpoint is in bp_shlib_disabled
13904 state, then user already saw the message about that
13905 breakpoint being disabled, and don't want to see more
13906 errors. */
13907 if (e.error == NOT_FOUND_ERROR
13908 && (b->condition_not_parsed
13909 || (b->loc != NULL
13910 && search_pspace != NULL
13911 && b->loc->pspace != search_pspace)
13912 || (b->loc && b->loc->shlib_disabled)
13913 || (b->loc && b->loc->pspace->executing_startup)
13914 || b->enable_state == bp_disabled))
13915 not_found_and_ok = 1;
13916
13917 if (!not_found_and_ok)
13918 {
13919 /* We surely don't want to warn about the same breakpoint
13920 10 times. One solution, implemented here, is disable
13921 the breakpoint on error. Another solution would be to
13922 have separate 'warning emitted' flag. Since this
13923 happens only when a binary has changed, I don't know
13924 which approach is better. */
13925 b->enable_state = bp_disabled;
13926 throw;
13927 }
13928
13929 exception = std::move (e);
13930 }
13931
13932 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13933 {
13934 for (auto &sal : sals)
13935 resolve_sal_pc (&sal);
13936 if (b->condition_not_parsed && b->extra_string != NULL)
13937 {
13938 char *cond_string, *extra_string;
13939 int thread, task;
13940
13941 find_condition_and_thread_for_sals (sals, b->extra_string,
13942 &cond_string, &thread,
13943 &task, &extra_string);
13944 gdb_assert (b->cond_string == NULL);
13945 if (cond_string)
13946 b->cond_string = cond_string;
13947 b->thread = thread;
13948 b->task = task;
13949 if (extra_string)
13950 {
13951 xfree (b->extra_string);
13952 b->extra_string = extra_string;
13953 }
13954 b->condition_not_parsed = 0;
13955 }
13956
13957 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13958 sals[0] = update_static_tracepoint (b, sals[0]);
13959
13960 *found = 1;
13961 }
13962 else
13963 *found = 0;
13964
13965 return sals;
13966 }
13967
13968 /* The default re_set method, for typical hardware or software
13969 breakpoints. Reevaluate the breakpoint and recreate its
13970 locations. */
13971
13972 static void
13973 breakpoint_re_set_default (struct breakpoint *b)
13974 {
13975 struct program_space *filter_pspace = current_program_space;
13976 std::vector<symtab_and_line> expanded, expanded_end;
13977
13978 int found;
13979 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13980 filter_pspace, &found);
13981 if (found)
13982 expanded = std::move (sals);
13983
13984 if (b->location_range_end != NULL)
13985 {
13986 std::vector<symtab_and_line> sals_end
13987 = location_to_sals (b, b->location_range_end.get (),
13988 filter_pspace, &found);
13989 if (found)
13990 expanded_end = std::move (sals_end);
13991 }
13992
13993 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
13994 }
13995
13996 /* Default method for creating SALs from an address string. It basically
13997 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13998
13999 static void
14000 create_sals_from_location_default (struct event_location *location,
14001 struct linespec_result *canonical,
14002 enum bptype type_wanted)
14003 {
14004 parse_breakpoint_sals (location, canonical);
14005 }
14006
14007 /* Call create_breakpoints_sal for the given arguments. This is the default
14008 function for the `create_breakpoints_sal' method of
14009 breakpoint_ops. */
14010
14011 static void
14012 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14013 struct linespec_result *canonical,
14014 gdb::unique_xmalloc_ptr<char> cond_string,
14015 gdb::unique_xmalloc_ptr<char> extra_string,
14016 enum bptype type_wanted,
14017 enum bpdisp disposition,
14018 int thread,
14019 int task, int ignore_count,
14020 const struct breakpoint_ops *ops,
14021 int from_tty, int enabled,
14022 int internal, unsigned flags)
14023 {
14024 create_breakpoints_sal (gdbarch, canonical,
14025 std::move (cond_string),
14026 std::move (extra_string),
14027 type_wanted, disposition,
14028 thread, task, ignore_count, ops, from_tty,
14029 enabled, internal, flags);
14030 }
14031
14032 /* Decode the line represented by S by calling decode_line_full. This is the
14033 default function for the `decode_location' method of breakpoint_ops. */
14034
14035 static std::vector<symtab_and_line>
14036 decode_location_default (struct breakpoint *b,
14037 struct event_location *location,
14038 struct program_space *search_pspace)
14039 {
14040 struct linespec_result canonical;
14041
14042 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
14043 NULL, 0, &canonical, multiple_symbols_all,
14044 b->filter.get ());
14045
14046 /* We should get 0 or 1 resulting SALs. */
14047 gdb_assert (canonical.lsals.size () < 2);
14048
14049 if (!canonical.lsals.empty ())
14050 {
14051 const linespec_sals &lsal = canonical.lsals[0];
14052 return std::move (lsal.sals);
14053 }
14054 return {};
14055 }
14056
14057 /* Reset a breakpoint. */
14058
14059 static void
14060 breakpoint_re_set_one (breakpoint *b)
14061 {
14062 input_radix = b->input_radix;
14063 set_language (b->language);
14064
14065 b->ops->re_set (b);
14066 }
14067
14068 /* Re-set breakpoint locations for the current program space.
14069 Locations bound to other program spaces are left untouched. */
14070
14071 void
14072 breakpoint_re_set (void)
14073 {
14074 struct breakpoint *b, *b_tmp;
14075
14076 {
14077 scoped_restore_current_language save_language;
14078 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
14079 scoped_restore_current_pspace_and_thread restore_pspace_thread;
14080
14081 /* breakpoint_re_set_one sets the current_language to the language
14082 of the breakpoint it is resetting (see prepare_re_set_context)
14083 before re-evaluating the breakpoint's location. This change can
14084 unfortunately get undone by accident if the language_mode is set
14085 to auto, and we either switch frames, or more likely in this context,
14086 we select the current frame.
14087
14088 We prevent this by temporarily turning the language_mode to
14089 language_mode_manual. We restore it once all breakpoints
14090 have been reset. */
14091 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
14092 language_mode = language_mode_manual;
14093
14094 /* Note: we must not try to insert locations until after all
14095 breakpoints have been re-set. Otherwise, e.g., when re-setting
14096 breakpoint 1, we'd insert the locations of breakpoint 2, which
14097 hadn't been re-set yet, and thus may have stale locations. */
14098
14099 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14100 {
14101 try
14102 {
14103 breakpoint_re_set_one (b);
14104 }
14105 catch (const gdb_exception &ex)
14106 {
14107 exception_fprintf (gdb_stderr, ex,
14108 "Error in re-setting breakpoint %d: ",
14109 b->number);
14110 }
14111 }
14112
14113 jit_breakpoint_re_set ();
14114 }
14115
14116 create_overlay_event_breakpoint ();
14117 create_longjmp_master_breakpoint ();
14118 create_std_terminate_master_breakpoint ();
14119 create_exception_master_breakpoint ();
14120
14121 /* Now we can insert. */
14122 update_global_location_list (UGLL_MAY_INSERT);
14123 }
14124 \f
14125 /* Reset the thread number of this breakpoint:
14126
14127 - If the breakpoint is for all threads, leave it as-is.
14128 - Else, reset it to the current thread for inferior_ptid. */
14129 void
14130 breakpoint_re_set_thread (struct breakpoint *b)
14131 {
14132 if (b->thread != -1)
14133 {
14134 b->thread = inferior_thread ()->global_num;
14135
14136 /* We're being called after following a fork. The new fork is
14137 selected as current, and unless this was a vfork will have a
14138 different program space from the original thread. Reset that
14139 as well. */
14140 b->loc->pspace = current_program_space;
14141 }
14142 }
14143
14144 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14145 If from_tty is nonzero, it prints a message to that effect,
14146 which ends with a period (no newline). */
14147
14148 void
14149 set_ignore_count (int bptnum, int count, int from_tty)
14150 {
14151 struct breakpoint *b;
14152
14153 if (count < 0)
14154 count = 0;
14155
14156 ALL_BREAKPOINTS (b)
14157 if (b->number == bptnum)
14158 {
14159 if (is_tracepoint (b))
14160 {
14161 if (from_tty && count != 0)
14162 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14163 bptnum);
14164 return;
14165 }
14166
14167 b->ignore_count = count;
14168 if (from_tty)
14169 {
14170 if (count == 0)
14171 printf_filtered (_("Will stop next time "
14172 "breakpoint %d is reached."),
14173 bptnum);
14174 else if (count == 1)
14175 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14176 bptnum);
14177 else
14178 printf_filtered (_("Will ignore next %d "
14179 "crossings of breakpoint %d."),
14180 count, bptnum);
14181 }
14182 gdb::observers::breakpoint_modified.notify (b);
14183 return;
14184 }
14185
14186 error (_("No breakpoint number %d."), bptnum);
14187 }
14188
14189 /* Command to set ignore-count of breakpoint N to COUNT. */
14190
14191 static void
14192 ignore_command (const char *args, int from_tty)
14193 {
14194 const char *p = args;
14195 int num;
14196
14197 if (p == 0)
14198 error_no_arg (_("a breakpoint number"));
14199
14200 num = get_number (&p);
14201 if (num == 0)
14202 error (_("bad breakpoint number: '%s'"), args);
14203 if (*p == 0)
14204 error (_("Second argument (specified ignore-count) is missing."));
14205
14206 set_ignore_count (num,
14207 longest_to_int (value_as_long (parse_and_eval (p))),
14208 from_tty);
14209 if (from_tty)
14210 printf_filtered ("\n");
14211 }
14212 \f
14213
14214 /* Call FUNCTION on each of the breakpoints with numbers in the range
14215 defined by BP_NUM_RANGE (an inclusive range). */
14216
14217 static void
14218 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
14219 gdb::function_view<void (breakpoint *)> function)
14220 {
14221 if (bp_num_range.first == 0)
14222 {
14223 warning (_("bad breakpoint number at or near '%d'"),
14224 bp_num_range.first);
14225 }
14226 else
14227 {
14228 struct breakpoint *b, *tmp;
14229
14230 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
14231 {
14232 bool match = false;
14233
14234 ALL_BREAKPOINTS_SAFE (b, tmp)
14235 if (b->number == i)
14236 {
14237 match = true;
14238 function (b);
14239 break;
14240 }
14241 if (!match)
14242 printf_unfiltered (_("No breakpoint number %d.\n"), i);
14243 }
14244 }
14245 }
14246
14247 /* Call FUNCTION on each of the breakpoints whose numbers are given in
14248 ARGS. */
14249
14250 static void
14251 map_breakpoint_numbers (const char *args,
14252 gdb::function_view<void (breakpoint *)> function)
14253 {
14254 if (args == NULL || *args == '\0')
14255 error_no_arg (_("one or more breakpoint numbers"));
14256
14257 number_or_range_parser parser (args);
14258
14259 while (!parser.finished ())
14260 {
14261 int num = parser.get_number ();
14262 map_breakpoint_number_range (std::make_pair (num, num), function);
14263 }
14264 }
14265
14266 /* Return the breakpoint location structure corresponding to the
14267 BP_NUM and LOC_NUM values. */
14268
14269 static struct bp_location *
14270 find_location_by_number (int bp_num, int loc_num)
14271 {
14272 struct breakpoint *b;
14273
14274 ALL_BREAKPOINTS (b)
14275 if (b->number == bp_num)
14276 {
14277 break;
14278 }
14279
14280 if (!b || b->number != bp_num)
14281 error (_("Bad breakpoint number '%d'"), bp_num);
14282
14283 if (loc_num == 0)
14284 error (_("Bad breakpoint location number '%d'"), loc_num);
14285
14286 int n = 0;
14287 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
14288 if (++n == loc_num)
14289 return loc;
14290
14291 error (_("Bad breakpoint location number '%d'"), loc_num);
14292 }
14293
14294 /* Modes of operation for extract_bp_num. */
14295 enum class extract_bp_kind
14296 {
14297 /* Extracting a breakpoint number. */
14298 bp,
14299
14300 /* Extracting a location number. */
14301 loc,
14302 };
14303
14304 /* Extract a breakpoint or location number (as determined by KIND)
14305 from the string starting at START. TRAILER is a character which
14306 can be found after the number. If you don't want a trailer, use
14307 '\0'. If END_OUT is not NULL, it is set to point after the parsed
14308 string. This always returns a positive integer. */
14309
14310 static int
14311 extract_bp_num (extract_bp_kind kind, const char *start,
14312 int trailer, const char **end_out = NULL)
14313 {
14314 const char *end = start;
14315 int num = get_number_trailer (&end, trailer);
14316 if (num < 0)
14317 error (kind == extract_bp_kind::bp
14318 ? _("Negative breakpoint number '%.*s'")
14319 : _("Negative breakpoint location number '%.*s'"),
14320 int (end - start), start);
14321 if (num == 0)
14322 error (kind == extract_bp_kind::bp
14323 ? _("Bad breakpoint number '%.*s'")
14324 : _("Bad breakpoint location number '%.*s'"),
14325 int (end - start), start);
14326
14327 if (end_out != NULL)
14328 *end_out = end;
14329 return num;
14330 }
14331
14332 /* Extract a breakpoint or location range (as determined by KIND) in
14333 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14334 representing the (inclusive) range. The returned pair's elements
14335 are always positive integers. */
14336
14337 static std::pair<int, int>
14338 extract_bp_or_bp_range (extract_bp_kind kind,
14339 const std::string &arg,
14340 std::string::size_type arg_offset)
14341 {
14342 std::pair<int, int> range;
14343 const char *bp_loc = &arg[arg_offset];
14344 std::string::size_type dash = arg.find ('-', arg_offset);
14345 if (dash != std::string::npos)
14346 {
14347 /* bp_loc is a range (x-z). */
14348 if (arg.length () == dash + 1)
14349 error (kind == extract_bp_kind::bp
14350 ? _("Bad breakpoint number at or near: '%s'")
14351 : _("Bad breakpoint location number at or near: '%s'"),
14352 bp_loc);
14353
14354 const char *end;
14355 const char *start_first = bp_loc;
14356 const char *start_second = &arg[dash + 1];
14357 range.first = extract_bp_num (kind, start_first, '-');
14358 range.second = extract_bp_num (kind, start_second, '\0', &end);
14359
14360 if (range.first > range.second)
14361 error (kind == extract_bp_kind::bp
14362 ? _("Inverted breakpoint range at '%.*s'")
14363 : _("Inverted breakpoint location range at '%.*s'"),
14364 int (end - start_first), start_first);
14365 }
14366 else
14367 {
14368 /* bp_loc is a single value. */
14369 range.first = extract_bp_num (kind, bp_loc, '\0');
14370 range.second = range.first;
14371 }
14372 return range;
14373 }
14374
14375 /* Extract the breakpoint/location range specified by ARG. Returns
14376 the breakpoint range in BP_NUM_RANGE, and the location range in
14377 BP_LOC_RANGE.
14378
14379 ARG may be in any of the following forms:
14380
14381 x where 'x' is a breakpoint number.
14382 x-y where 'x' and 'y' specify a breakpoint numbers range.
14383 x.y where 'x' is a breakpoint number and 'y' a location number.
14384 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14385 location number range.
14386 */
14387
14388 static void
14389 extract_bp_number_and_location (const std::string &arg,
14390 std::pair<int, int> &bp_num_range,
14391 std::pair<int, int> &bp_loc_range)
14392 {
14393 std::string::size_type dot = arg.find ('.');
14394
14395 if (dot != std::string::npos)
14396 {
14397 /* Handle 'x.y' and 'x.y-z' cases. */
14398
14399 if (arg.length () == dot + 1 || dot == 0)
14400 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
14401
14402 bp_num_range.first
14403 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14404 bp_num_range.second = bp_num_range.first;
14405
14406 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14407 arg, dot + 1);
14408 }
14409 else
14410 {
14411 /* Handle x and x-y cases. */
14412
14413 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
14414 bp_loc_range.first = 0;
14415 bp_loc_range.second = 0;
14416 }
14417 }
14418
14419 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14420 specifies whether to enable or disable. */
14421
14422 static void
14423 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14424 {
14425 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14426 if (loc != NULL)
14427 {
14428 if (loc->disabled_by_cond && enable)
14429 error (_("Breakpoint %d's condition is invalid at location %d, "
14430 "cannot enable."), bp_num, loc_num);
14431
14432 if (loc->enabled != enable)
14433 {
14434 loc->enabled = enable;
14435 mark_breakpoint_location_modified (loc);
14436 }
14437 if (target_supports_enable_disable_tracepoint ()
14438 && current_trace_status ()->running && loc->owner
14439 && is_tracepoint (loc->owner))
14440 target_disable_tracepoint (loc);
14441 }
14442 update_global_location_list (UGLL_DONT_INSERT);
14443
14444 gdb::observers::breakpoint_modified.notify (loc->owner);
14445 }
14446
14447 /* Enable or disable a range of breakpoint locations. BP_NUM is the
14448 number of the breakpoint, and BP_LOC_RANGE specifies the
14449 (inclusive) range of location numbers of that breakpoint to
14450 enable/disable. ENABLE specifies whether to enable or disable the
14451 location. */
14452
14453 static void
14454 enable_disable_breakpoint_location_range (int bp_num,
14455 std::pair<int, int> &bp_loc_range,
14456 bool enable)
14457 {
14458 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14459 enable_disable_bp_num_loc (bp_num, i, enable);
14460 }
14461
14462 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14463 If from_tty is nonzero, it prints a message to that effect,
14464 which ends with a period (no newline). */
14465
14466 void
14467 disable_breakpoint (struct breakpoint *bpt)
14468 {
14469 /* Never disable a watchpoint scope breakpoint; we want to
14470 hit them when we leave scope so we can delete both the
14471 watchpoint and its scope breakpoint at that time. */
14472 if (bpt->type == bp_watchpoint_scope)
14473 return;
14474
14475 bpt->enable_state = bp_disabled;
14476
14477 /* Mark breakpoint locations modified. */
14478 mark_breakpoint_modified (bpt);
14479
14480 if (target_supports_enable_disable_tracepoint ()
14481 && current_trace_status ()->running && is_tracepoint (bpt))
14482 {
14483 struct bp_location *location;
14484
14485 for (location = bpt->loc; location; location = location->next)
14486 target_disable_tracepoint (location);
14487 }
14488
14489 update_global_location_list (UGLL_DONT_INSERT);
14490
14491 gdb::observers::breakpoint_modified.notify (bpt);
14492 }
14493
14494 /* Enable or disable the breakpoint(s) or breakpoint location(s)
14495 specified in ARGS. ARGS may be in any of the formats handled by
14496 extract_bp_number_and_location. ENABLE specifies whether to enable
14497 or disable the breakpoints/locations. */
14498
14499 static void
14500 enable_disable_command (const char *args, int from_tty, bool enable)
14501 {
14502 if (args == 0)
14503 {
14504 struct breakpoint *bpt;
14505
14506 ALL_BREAKPOINTS (bpt)
14507 if (user_breakpoint_p (bpt))
14508 {
14509 if (enable)
14510 enable_breakpoint (bpt);
14511 else
14512 disable_breakpoint (bpt);
14513 }
14514 }
14515 else
14516 {
14517 std::string num = extract_arg (&args);
14518
14519 while (!num.empty ())
14520 {
14521 std::pair<int, int> bp_num_range, bp_loc_range;
14522
14523 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14524
14525 if (bp_loc_range.first == bp_loc_range.second
14526 && bp_loc_range.first == 0)
14527 {
14528 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14529 map_breakpoint_number_range (bp_num_range,
14530 enable
14531 ? enable_breakpoint
14532 : disable_breakpoint);
14533 }
14534 else
14535 {
14536 /* Handle breakpoint ids with formats 'x.y' or
14537 'x.y-z'. */
14538 enable_disable_breakpoint_location_range
14539 (bp_num_range.first, bp_loc_range, enable);
14540 }
14541 num = extract_arg (&args);
14542 }
14543 }
14544 }
14545
14546 /* The disable command disables the specified breakpoints/locations
14547 (or all defined breakpoints) so they're no longer effective in
14548 stopping the inferior. ARGS may be in any of the forms defined in
14549 extract_bp_number_and_location. */
14550
14551 static void
14552 disable_command (const char *args, int from_tty)
14553 {
14554 enable_disable_command (args, from_tty, false);
14555 }
14556
14557 static void
14558 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14559 int count)
14560 {
14561 int target_resources_ok;
14562
14563 if (bpt->type == bp_hardware_breakpoint)
14564 {
14565 int i;
14566 i = hw_breakpoint_used_count ();
14567 target_resources_ok =
14568 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14569 i + 1, 0);
14570 if (target_resources_ok == 0)
14571 error (_("No hardware breakpoint support in the target."));
14572 else if (target_resources_ok < 0)
14573 error (_("Hardware breakpoints used exceeds limit."));
14574 }
14575
14576 if (is_watchpoint (bpt))
14577 {
14578 /* Initialize it just to avoid a GCC false warning. */
14579 enum enable_state orig_enable_state = bp_disabled;
14580
14581 try
14582 {
14583 struct watchpoint *w = (struct watchpoint *) bpt;
14584
14585 orig_enable_state = bpt->enable_state;
14586 bpt->enable_state = bp_enabled;
14587 update_watchpoint (w, 1 /* reparse */);
14588 }
14589 catch (const gdb_exception &e)
14590 {
14591 bpt->enable_state = orig_enable_state;
14592 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14593 bpt->number);
14594 return;
14595 }
14596 }
14597
14598 bpt->enable_state = bp_enabled;
14599
14600 /* Mark breakpoint locations modified. */
14601 mark_breakpoint_modified (bpt);
14602
14603 if (target_supports_enable_disable_tracepoint ()
14604 && current_trace_status ()->running && is_tracepoint (bpt))
14605 {
14606 struct bp_location *location;
14607
14608 for (location = bpt->loc; location; location = location->next)
14609 target_enable_tracepoint (location);
14610 }
14611
14612 bpt->disposition = disposition;
14613 bpt->enable_count = count;
14614 update_global_location_list (UGLL_MAY_INSERT);
14615
14616 gdb::observers::breakpoint_modified.notify (bpt);
14617 }
14618
14619
14620 void
14621 enable_breakpoint (struct breakpoint *bpt)
14622 {
14623 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14624 }
14625
14626 /* The enable command enables the specified breakpoints/locations (or
14627 all defined breakpoints) so they once again become (or continue to
14628 be) effective in stopping the inferior. ARGS may be in any of the
14629 forms defined in extract_bp_number_and_location. */
14630
14631 static void
14632 enable_command (const char *args, int from_tty)
14633 {
14634 enable_disable_command (args, from_tty, true);
14635 }
14636
14637 static void
14638 enable_once_command (const char *args, int from_tty)
14639 {
14640 map_breakpoint_numbers
14641 (args, [&] (breakpoint *b)
14642 {
14643 iterate_over_related_breakpoints
14644 (b, [&] (breakpoint *bpt)
14645 {
14646 enable_breakpoint_disp (bpt, disp_disable, 1);
14647 });
14648 });
14649 }
14650
14651 static void
14652 enable_count_command (const char *args, int from_tty)
14653 {
14654 int count;
14655
14656 if (args == NULL)
14657 error_no_arg (_("hit count"));
14658
14659 count = get_number (&args);
14660
14661 map_breakpoint_numbers
14662 (args, [&] (breakpoint *b)
14663 {
14664 iterate_over_related_breakpoints
14665 (b, [&] (breakpoint *bpt)
14666 {
14667 enable_breakpoint_disp (bpt, disp_disable, count);
14668 });
14669 });
14670 }
14671
14672 static void
14673 enable_delete_command (const char *args, int from_tty)
14674 {
14675 map_breakpoint_numbers
14676 (args, [&] (breakpoint *b)
14677 {
14678 iterate_over_related_breakpoints
14679 (b, [&] (breakpoint *bpt)
14680 {
14681 enable_breakpoint_disp (bpt, disp_del, 1);
14682 });
14683 });
14684 }
14685 \f
14686 /* Invalidate last known value of any hardware watchpoint if
14687 the memory which that value represents has been written to by
14688 GDB itself. */
14689
14690 static void
14691 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14692 CORE_ADDR addr, ssize_t len,
14693 const bfd_byte *data)
14694 {
14695 struct breakpoint *bp;
14696
14697 ALL_BREAKPOINTS (bp)
14698 if (bp->enable_state == bp_enabled
14699 && bp->type == bp_hardware_watchpoint)
14700 {
14701 struct watchpoint *wp = (struct watchpoint *) bp;
14702
14703 if (wp->val_valid && wp->val != nullptr)
14704 {
14705 struct bp_location *loc;
14706
14707 for (loc = bp->loc; loc != NULL; loc = loc->next)
14708 if (loc->loc_type == bp_loc_hardware_watchpoint
14709 && loc->address + loc->length > addr
14710 && addr + len > loc->address)
14711 {
14712 wp->val = NULL;
14713 wp->val_valid = false;
14714 }
14715 }
14716 }
14717 }
14718
14719 /* Create and insert a breakpoint for software single step. */
14720
14721 void
14722 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14723 const address_space *aspace,
14724 CORE_ADDR next_pc)
14725 {
14726 struct thread_info *tp = inferior_thread ();
14727 struct symtab_and_line sal;
14728 CORE_ADDR pc = next_pc;
14729
14730 if (tp->control.single_step_breakpoints == NULL)
14731 {
14732 tp->control.single_step_breakpoints
14733 = new_single_step_breakpoint (tp->global_num, gdbarch);
14734 }
14735
14736 sal = find_pc_line (pc, 0);
14737 sal.pc = pc;
14738 sal.section = find_pc_overlay (pc);
14739 sal.explicit_pc = 1;
14740 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14741
14742 update_global_location_list (UGLL_INSERT);
14743 }
14744
14745 /* Insert single step breakpoints according to the current state. */
14746
14747 int
14748 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14749 {
14750 struct regcache *regcache = get_current_regcache ();
14751 std::vector<CORE_ADDR> next_pcs;
14752
14753 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14754
14755 if (!next_pcs.empty ())
14756 {
14757 struct frame_info *frame = get_current_frame ();
14758 const address_space *aspace = get_frame_address_space (frame);
14759
14760 for (CORE_ADDR pc : next_pcs)
14761 insert_single_step_breakpoint (gdbarch, aspace, pc);
14762
14763 return 1;
14764 }
14765 else
14766 return 0;
14767 }
14768
14769 /* See breakpoint.h. */
14770
14771 int
14772 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14773 const address_space *aspace,
14774 CORE_ADDR pc)
14775 {
14776 struct bp_location *loc;
14777
14778 for (loc = bp->loc; loc != NULL; loc = loc->next)
14779 if (loc->inserted
14780 && breakpoint_location_address_match (loc, aspace, pc))
14781 return 1;
14782
14783 return 0;
14784 }
14785
14786 /* Check whether a software single-step breakpoint is inserted at
14787 PC. */
14788
14789 int
14790 single_step_breakpoint_inserted_here_p (const address_space *aspace,
14791 CORE_ADDR pc)
14792 {
14793 struct breakpoint *bpt;
14794
14795 ALL_BREAKPOINTS (bpt)
14796 {
14797 if (bpt->type == bp_single_step
14798 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14799 return 1;
14800 }
14801 return 0;
14802 }
14803
14804 /* Tracepoint-specific operations. */
14805
14806 /* Set tracepoint count to NUM. */
14807 static void
14808 set_tracepoint_count (int num)
14809 {
14810 tracepoint_count = num;
14811 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14812 }
14813
14814 static void
14815 trace_command (const char *arg, int from_tty)
14816 {
14817 event_location_up location = string_to_event_location (&arg,
14818 current_language);
14819 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
14820 (location.get (), true /* is_tracepoint */);
14821
14822 create_breakpoint (get_current_arch (),
14823 location.get (),
14824 NULL, 0, arg, 1 /* parse arg */,
14825 0 /* tempflag */,
14826 bp_tracepoint /* type_wanted */,
14827 0 /* Ignore count */,
14828 pending_break_support,
14829 ops,
14830 from_tty,
14831 1 /* enabled */,
14832 0 /* internal */, 0);
14833 }
14834
14835 static void
14836 ftrace_command (const char *arg, int from_tty)
14837 {
14838 event_location_up location = string_to_event_location (&arg,
14839 current_language);
14840 create_breakpoint (get_current_arch (),
14841 location.get (),
14842 NULL, 0, arg, 1 /* parse arg */,
14843 0 /* tempflag */,
14844 bp_fast_tracepoint /* type_wanted */,
14845 0 /* Ignore count */,
14846 pending_break_support,
14847 &tracepoint_breakpoint_ops,
14848 from_tty,
14849 1 /* enabled */,
14850 0 /* internal */, 0);
14851 }
14852
14853 /* strace command implementation. Creates a static tracepoint. */
14854
14855 static void
14856 strace_command (const char *arg, int from_tty)
14857 {
14858 struct breakpoint_ops *ops;
14859 event_location_up location;
14860
14861 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14862 or with a normal static tracepoint. */
14863 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14864 {
14865 ops = &strace_marker_breakpoint_ops;
14866 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
14867 }
14868 else
14869 {
14870 ops = &tracepoint_breakpoint_ops;
14871 location = string_to_event_location (&arg, current_language);
14872 }
14873
14874 create_breakpoint (get_current_arch (),
14875 location.get (),
14876 NULL, 0, arg, 1 /* parse arg */,
14877 0 /* tempflag */,
14878 bp_static_tracepoint /* type_wanted */,
14879 0 /* Ignore count */,
14880 pending_break_support,
14881 ops,
14882 from_tty,
14883 1 /* enabled */,
14884 0 /* internal */, 0);
14885 }
14886
14887 /* Set up a fake reader function that gets command lines from a linked
14888 list that was acquired during tracepoint uploading. */
14889
14890 static struct uploaded_tp *this_utp;
14891 static int next_cmd;
14892
14893 static char *
14894 read_uploaded_action (void)
14895 {
14896 char *rslt = nullptr;
14897
14898 if (next_cmd < this_utp->cmd_strings.size ())
14899 {
14900 rslt = this_utp->cmd_strings[next_cmd].get ();
14901 next_cmd++;
14902 }
14903
14904 return rslt;
14905 }
14906
14907 /* Given information about a tracepoint as recorded on a target (which
14908 can be either a live system or a trace file), attempt to create an
14909 equivalent GDB tracepoint. This is not a reliable process, since
14910 the target does not necessarily have all the information used when
14911 the tracepoint was originally defined. */
14912
14913 struct tracepoint *
14914 create_tracepoint_from_upload (struct uploaded_tp *utp)
14915 {
14916 const char *addr_str;
14917 char small_buf[100];
14918 struct tracepoint *tp;
14919
14920 if (utp->at_string)
14921 addr_str = utp->at_string.get ();
14922 else
14923 {
14924 /* In the absence of a source location, fall back to raw
14925 address. Since there is no way to confirm that the address
14926 means the same thing as when the trace was started, warn the
14927 user. */
14928 warning (_("Uploaded tracepoint %d has no "
14929 "source location, using raw address"),
14930 utp->number);
14931 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14932 addr_str = small_buf;
14933 }
14934
14935 /* There's not much we can do with a sequence of bytecodes. */
14936 if (utp->cond && !utp->cond_string)
14937 warning (_("Uploaded tracepoint %d condition "
14938 "has no source form, ignoring it"),
14939 utp->number);
14940
14941 event_location_up location = string_to_event_location (&addr_str,
14942 current_language);
14943 if (!create_breakpoint (get_current_arch (),
14944 location.get (),
14945 utp->cond_string.get (), -1, addr_str,
14946 0 /* parse cond/thread */,
14947 0 /* tempflag */,
14948 utp->type /* type_wanted */,
14949 0 /* Ignore count */,
14950 pending_break_support,
14951 &tracepoint_breakpoint_ops,
14952 0 /* from_tty */,
14953 utp->enabled /* enabled */,
14954 0 /* internal */,
14955 CREATE_BREAKPOINT_FLAGS_INSERTED))
14956 return NULL;
14957
14958 /* Get the tracepoint we just created. */
14959 tp = get_tracepoint (tracepoint_count);
14960 gdb_assert (tp != NULL);
14961
14962 if (utp->pass > 0)
14963 {
14964 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14965 tp->number);
14966
14967 trace_pass_command (small_buf, 0);
14968 }
14969
14970 /* If we have uploaded versions of the original commands, set up a
14971 special-purpose "reader" function and call the usual command line
14972 reader, then pass the result to the breakpoint command-setting
14973 function. */
14974 if (!utp->cmd_strings.empty ())
14975 {
14976 counted_command_line cmd_list;
14977
14978 this_utp = utp;
14979 next_cmd = 0;
14980
14981 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
14982
14983 breakpoint_set_commands (tp, std::move (cmd_list));
14984 }
14985 else if (!utp->actions.empty ()
14986 || !utp->step_actions.empty ())
14987 warning (_("Uploaded tracepoint %d actions "
14988 "have no source form, ignoring them"),
14989 utp->number);
14990
14991 /* Copy any status information that might be available. */
14992 tp->hit_count = utp->hit_count;
14993 tp->traceframe_usage = utp->traceframe_usage;
14994
14995 return tp;
14996 }
14997
14998 /* Print information on tracepoint number TPNUM_EXP, or all if
14999 omitted. */
15000
15001 static void
15002 info_tracepoints_command (const char *args, int from_tty)
15003 {
15004 struct ui_out *uiout = current_uiout;
15005 int num_printed;
15006
15007 num_printed = breakpoint_1 (args, false, is_tracepoint);
15008
15009 if (num_printed == 0)
15010 {
15011 if (args == NULL || *args == '\0')
15012 uiout->message ("No tracepoints.\n");
15013 else
15014 uiout->message ("No tracepoint matching '%s'.\n", args);
15015 }
15016
15017 default_collect_info ();
15018 }
15019
15020 /* The 'enable trace' command enables tracepoints.
15021 Not supported by all targets. */
15022 static void
15023 enable_trace_command (const char *args, int from_tty)
15024 {
15025 enable_command (args, from_tty);
15026 }
15027
15028 /* The 'disable trace' command disables tracepoints.
15029 Not supported by all targets. */
15030 static void
15031 disable_trace_command (const char *args, int from_tty)
15032 {
15033 disable_command (args, from_tty);
15034 }
15035
15036 /* Remove a tracepoint (or all if no argument). */
15037 static void
15038 delete_trace_command (const char *arg, int from_tty)
15039 {
15040 struct breakpoint *b, *b_tmp;
15041
15042 dont_repeat ();
15043
15044 if (arg == 0)
15045 {
15046 int breaks_to_delete = 0;
15047
15048 /* Delete all breakpoints if no argument.
15049 Do not delete internal or call-dummy breakpoints, these
15050 have to be deleted with an explicit breakpoint number
15051 argument. */
15052 ALL_TRACEPOINTS (b)
15053 if (is_tracepoint (b) && user_breakpoint_p (b))
15054 {
15055 breaks_to_delete = 1;
15056 break;
15057 }
15058
15059 /* Ask user only if there are some breakpoints to delete. */
15060 if (!from_tty
15061 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15062 {
15063 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15064 if (is_tracepoint (b) && user_breakpoint_p (b))
15065 delete_breakpoint (b);
15066 }
15067 }
15068 else
15069 map_breakpoint_numbers
15070 (arg, [&] (breakpoint *br)
15071 {
15072 iterate_over_related_breakpoints (br, delete_breakpoint);
15073 });
15074 }
15075
15076 /* Helper function for trace_pass_command. */
15077
15078 static void
15079 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15080 {
15081 tp->pass_count = count;
15082 gdb::observers::breakpoint_modified.notify (tp);
15083 if (from_tty)
15084 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15085 tp->number, count);
15086 }
15087
15088 /* Set passcount for tracepoint.
15089
15090 First command argument is passcount, second is tracepoint number.
15091 If tracepoint number omitted, apply to most recently defined.
15092 Also accepts special argument "all". */
15093
15094 static void
15095 trace_pass_command (const char *args, int from_tty)
15096 {
15097 struct tracepoint *t1;
15098 ULONGEST count;
15099
15100 if (args == 0 || *args == 0)
15101 error (_("passcount command requires an "
15102 "argument (count + optional TP num)"));
15103
15104 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
15105
15106 args = skip_spaces (args);
15107 if (*args && strncasecmp (args, "all", 3) == 0)
15108 {
15109 struct breakpoint *b;
15110
15111 args += 3; /* Skip special argument "all". */
15112 if (*args)
15113 error (_("Junk at end of arguments."));
15114
15115 ALL_TRACEPOINTS (b)
15116 {
15117 t1 = (struct tracepoint *) b;
15118 trace_pass_set_count (t1, count, from_tty);
15119 }
15120 }
15121 else if (*args == '\0')
15122 {
15123 t1 = get_tracepoint_by_number (&args, NULL);
15124 if (t1)
15125 trace_pass_set_count (t1, count, from_tty);
15126 }
15127 else
15128 {
15129 number_or_range_parser parser (args);
15130 while (!parser.finished ())
15131 {
15132 t1 = get_tracepoint_by_number (&args, &parser);
15133 if (t1)
15134 trace_pass_set_count (t1, count, from_tty);
15135 }
15136 }
15137 }
15138
15139 struct tracepoint *
15140 get_tracepoint (int num)
15141 {
15142 struct breakpoint *t;
15143
15144 ALL_TRACEPOINTS (t)
15145 if (t->number == num)
15146 return (struct tracepoint *) t;
15147
15148 return NULL;
15149 }
15150
15151 /* Find the tracepoint with the given target-side number (which may be
15152 different from the tracepoint number after disconnecting and
15153 reconnecting). */
15154
15155 struct tracepoint *
15156 get_tracepoint_by_number_on_target (int num)
15157 {
15158 struct breakpoint *b;
15159
15160 ALL_TRACEPOINTS (b)
15161 {
15162 struct tracepoint *t = (struct tracepoint *) b;
15163
15164 if (t->number_on_target == num)
15165 return t;
15166 }
15167
15168 return NULL;
15169 }
15170
15171 /* Utility: parse a tracepoint number and look it up in the list.
15172 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15173 If the argument is missing, the most recent tracepoint
15174 (tracepoint_count) is returned. */
15175
15176 struct tracepoint *
15177 get_tracepoint_by_number (const char **arg,
15178 number_or_range_parser *parser)
15179 {
15180 struct breakpoint *t;
15181 int tpnum;
15182 const char *instring = arg == NULL ? NULL : *arg;
15183
15184 if (parser != NULL)
15185 {
15186 gdb_assert (!parser->finished ());
15187 tpnum = parser->get_number ();
15188 }
15189 else if (arg == NULL || *arg == NULL || ! **arg)
15190 tpnum = tracepoint_count;
15191 else
15192 tpnum = get_number (arg);
15193
15194 if (tpnum <= 0)
15195 {
15196 if (instring && *instring)
15197 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15198 instring);
15199 else
15200 printf_filtered (_("No previous tracepoint\n"));
15201 return NULL;
15202 }
15203
15204 ALL_TRACEPOINTS (t)
15205 if (t->number == tpnum)
15206 {
15207 return (struct tracepoint *) t;
15208 }
15209
15210 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15211 return NULL;
15212 }
15213
15214 void
15215 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15216 {
15217 if (b->thread != -1)
15218 fprintf_unfiltered (fp, " thread %d", b->thread);
15219
15220 if (b->task != 0)
15221 fprintf_unfiltered (fp, " task %d", b->task);
15222
15223 fprintf_unfiltered (fp, "\n");
15224 }
15225
15226 /* Save information on user settable breakpoints (watchpoints, etc) to
15227 a new script file named FILENAME. If FILTER is non-NULL, call it
15228 on each breakpoint and only include the ones for which it returns
15229 true. */
15230
15231 static void
15232 save_breakpoints (const char *filename, int from_tty,
15233 bool (*filter) (const struct breakpoint *))
15234 {
15235 struct breakpoint *tp;
15236 int any = 0;
15237 int extra_trace_bits = 0;
15238
15239 if (filename == 0 || *filename == 0)
15240 error (_("Argument required (file name in which to save)"));
15241
15242 /* See if we have anything to save. */
15243 ALL_BREAKPOINTS (tp)
15244 {
15245 /* Skip internal and momentary breakpoints. */
15246 if (!user_breakpoint_p (tp))
15247 continue;
15248
15249 /* If we have a filter, only save the breakpoints it accepts. */
15250 if (filter && !filter (tp))
15251 continue;
15252
15253 any = 1;
15254
15255 if (is_tracepoint (tp))
15256 {
15257 extra_trace_bits = 1;
15258
15259 /* We can stop searching. */
15260 break;
15261 }
15262 }
15263
15264 if (!any)
15265 {
15266 warning (_("Nothing to save."));
15267 return;
15268 }
15269
15270 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
15271
15272 stdio_file fp;
15273
15274 if (!fp.open (expanded_filename.get (), "w"))
15275 error (_("Unable to open file '%s' for saving (%s)"),
15276 expanded_filename.get (), safe_strerror (errno));
15277
15278 if (extra_trace_bits)
15279 save_trace_state_variables (&fp);
15280
15281 ALL_BREAKPOINTS (tp)
15282 {
15283 /* Skip internal and momentary breakpoints. */
15284 if (!user_breakpoint_p (tp))
15285 continue;
15286
15287 /* If we have a filter, only save the breakpoints it accepts. */
15288 if (filter && !filter (tp))
15289 continue;
15290
15291 tp->ops->print_recreate (tp, &fp);
15292
15293 /* Note, we can't rely on tp->number for anything, as we can't
15294 assume the recreated breakpoint numbers will match. Use $bpnum
15295 instead. */
15296
15297 if (tp->cond_string)
15298 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15299
15300 if (tp->ignore_count)
15301 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15302
15303 if (tp->type != bp_dprintf && tp->commands)
15304 {
15305 fp.puts (" commands\n");
15306
15307 current_uiout->redirect (&fp);
15308 try
15309 {
15310 print_command_lines (current_uiout, tp->commands.get (), 2);
15311 }
15312 catch (const gdb_exception &ex)
15313 {
15314 current_uiout->redirect (NULL);
15315 throw;
15316 }
15317
15318 current_uiout->redirect (NULL);
15319 fp.puts (" end\n");
15320 }
15321
15322 if (tp->enable_state == bp_disabled)
15323 fp.puts ("disable $bpnum\n");
15324
15325 /* If this is a multi-location breakpoint, check if the locations
15326 should be individually disabled. Watchpoint locations are
15327 special, and not user visible. */
15328 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15329 {
15330 struct bp_location *loc;
15331 int n = 1;
15332
15333 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15334 if (!loc->enabled)
15335 fp.printf ("disable $bpnum.%d\n", n);
15336 }
15337 }
15338
15339 if (extra_trace_bits && *default_collect)
15340 fp.printf ("set default-collect %s\n", default_collect);
15341
15342 if (from_tty)
15343 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
15344 }
15345
15346 /* The `save breakpoints' command. */
15347
15348 static void
15349 save_breakpoints_command (const char *args, int from_tty)
15350 {
15351 save_breakpoints (args, from_tty, NULL);
15352 }
15353
15354 /* The `save tracepoints' command. */
15355
15356 static void
15357 save_tracepoints_command (const char *args, int from_tty)
15358 {
15359 save_breakpoints (args, from_tty, is_tracepoint);
15360 }
15361
15362 /* Create a vector of all tracepoints. */
15363
15364 std::vector<breakpoint *>
15365 all_tracepoints (void)
15366 {
15367 std::vector<breakpoint *> tp_vec;
15368 struct breakpoint *tp;
15369
15370 ALL_TRACEPOINTS (tp)
15371 {
15372 tp_vec.push_back (tp);
15373 }
15374
15375 return tp_vec;
15376 }
15377
15378 \f
15379 /* This help string is used to consolidate all the help string for specifying
15380 locations used by several commands. */
15381
15382 #define LOCATION_HELP_STRING \
15383 "Linespecs are colon-separated lists of location parameters, such as\n\
15384 source filename, function name, label name, and line number.\n\
15385 Example: To specify the start of a label named \"the_top\" in the\n\
15386 function \"fact\" in the file \"factorial.c\", use\n\
15387 \"factorial.c:fact:the_top\".\n\
15388 \n\
15389 Address locations begin with \"*\" and specify an exact address in the\n\
15390 program. Example: To specify the fourth byte past the start function\n\
15391 \"main\", use \"*main + 4\".\n\
15392 \n\
15393 Explicit locations are similar to linespecs but use an option/argument\n\
15394 syntax to specify location parameters.\n\
15395 Example: To specify the start of the label named \"the_top\" in the\n\
15396 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15397 -function fact -label the_top\".\n\
15398 \n\
15399 By default, a specified function is matched against the program's\n\
15400 functions in all scopes. For C++, this means in all namespaces and\n\
15401 classes. For Ada, this means in all packages. E.g., in C++,\n\
15402 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15403 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
15404 specified name as a complete fully-qualified name instead."
15405
15406 /* This help string is used for the break, hbreak, tbreak and thbreak
15407 commands. It is defined as a macro to prevent duplication.
15408 COMMAND should be a string constant containing the name of the
15409 command. */
15410
15411 #define BREAK_ARGS_HELP(command) \
15412 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
15413 \t[-force-condition] [if CONDITION]\n\
15414 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15415 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15416 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15417 `-probe-dtrace' (for a DTrace probe).\n\
15418 LOCATION may be a linespec, address, or explicit location as described\n\
15419 below.\n\
15420 \n\
15421 With no LOCATION, uses current execution address of the selected\n\
15422 stack frame. This is useful for breaking on return to a stack frame.\n\
15423 \n\
15424 THREADNUM is the number from \"info threads\".\n\
15425 CONDITION is a boolean expression.\n\
15426 \n\
15427 With the \"-force-condition\" flag, the condition is defined even when\n\
15428 it is invalid for all current locations.\n\
15429 \n" LOCATION_HELP_STRING "\n\n\
15430 Multiple breakpoints at one place are permitted, and useful if their\n\
15431 conditions are different.\n\
15432 \n\
15433 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15434
15435 /* List of subcommands for "catch". */
15436 static struct cmd_list_element *catch_cmdlist;
15437
15438 /* List of subcommands for "tcatch". */
15439 static struct cmd_list_element *tcatch_cmdlist;
15440
15441 void
15442 add_catch_command (const char *name, const char *docstring,
15443 cmd_const_sfunc_ftype *sfunc,
15444 completer_ftype *completer,
15445 void *user_data_catch,
15446 void *user_data_tcatch)
15447 {
15448 struct cmd_list_element *command;
15449
15450 command = add_cmd (name, class_breakpoint, docstring,
15451 &catch_cmdlist);
15452 set_cmd_sfunc (command, sfunc);
15453 set_cmd_context (command, user_data_catch);
15454 set_cmd_completer (command, completer);
15455
15456 command = add_cmd (name, class_breakpoint, docstring,
15457 &tcatch_cmdlist);
15458 set_cmd_sfunc (command, sfunc);
15459 set_cmd_context (command, user_data_tcatch);
15460 set_cmd_completer (command, completer);
15461 }
15462
15463 struct breakpoint *
15464 iterate_over_breakpoints (gdb::function_view<bool (breakpoint *)> callback)
15465 {
15466 struct breakpoint *b, *b_tmp;
15467
15468 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15469 {
15470 if (callback (b))
15471 return b;
15472 }
15473
15474 return NULL;
15475 }
15476
15477 /* Zero if any of the breakpoint's locations could be a location where
15478 functions have been inlined, nonzero otherwise. */
15479
15480 static int
15481 is_non_inline_function (struct breakpoint *b)
15482 {
15483 /* The shared library event breakpoint is set on the address of a
15484 non-inline function. */
15485 if (b->type == bp_shlib_event)
15486 return 1;
15487
15488 return 0;
15489 }
15490
15491 /* Nonzero if the specified PC cannot be a location where functions
15492 have been inlined. */
15493
15494 int
15495 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
15496 const struct target_waitstatus *ws)
15497 {
15498 struct breakpoint *b;
15499 struct bp_location *bl;
15500
15501 ALL_BREAKPOINTS (b)
15502 {
15503 if (!is_non_inline_function (b))
15504 continue;
15505
15506 for (bl = b->loc; bl != NULL; bl = bl->next)
15507 {
15508 if (!bl->shlib_disabled
15509 && bpstat_check_location (bl, aspace, pc, ws))
15510 return 1;
15511 }
15512 }
15513
15514 return 0;
15515 }
15516
15517 /* Remove any references to OBJFILE which is going to be freed. */
15518
15519 void
15520 breakpoint_free_objfile (struct objfile *objfile)
15521 {
15522 struct bp_location **locp, *loc;
15523
15524 ALL_BP_LOCATIONS (loc, locp)
15525 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15526 loc->symtab = NULL;
15527 }
15528
15529 void
15530 initialize_breakpoint_ops (void)
15531 {
15532 static int initialized = 0;
15533
15534 struct breakpoint_ops *ops;
15535
15536 if (initialized)
15537 return;
15538 initialized = 1;
15539
15540 /* The breakpoint_ops structure to be inherit by all kinds of
15541 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15542 internal and momentary breakpoints, etc.). */
15543 ops = &bkpt_base_breakpoint_ops;
15544 *ops = base_breakpoint_ops;
15545 ops->re_set = bkpt_re_set;
15546 ops->insert_location = bkpt_insert_location;
15547 ops->remove_location = bkpt_remove_location;
15548 ops->breakpoint_hit = bkpt_breakpoint_hit;
15549 ops->create_sals_from_location = bkpt_create_sals_from_location;
15550 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15551 ops->decode_location = bkpt_decode_location;
15552
15553 /* The breakpoint_ops structure to be used in regular breakpoints. */
15554 ops = &bkpt_breakpoint_ops;
15555 *ops = bkpt_base_breakpoint_ops;
15556 ops->re_set = bkpt_re_set;
15557 ops->resources_needed = bkpt_resources_needed;
15558 ops->print_it = bkpt_print_it;
15559 ops->print_mention = bkpt_print_mention;
15560 ops->print_recreate = bkpt_print_recreate;
15561
15562 /* Ranged breakpoints. */
15563 ops = &ranged_breakpoint_ops;
15564 *ops = bkpt_breakpoint_ops;
15565 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15566 ops->resources_needed = resources_needed_ranged_breakpoint;
15567 ops->print_it = print_it_ranged_breakpoint;
15568 ops->print_one = print_one_ranged_breakpoint;
15569 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15570 ops->print_mention = print_mention_ranged_breakpoint;
15571 ops->print_recreate = print_recreate_ranged_breakpoint;
15572
15573 /* Internal breakpoints. */
15574 ops = &internal_breakpoint_ops;
15575 *ops = bkpt_base_breakpoint_ops;
15576 ops->re_set = internal_bkpt_re_set;
15577 ops->check_status = internal_bkpt_check_status;
15578 ops->print_it = internal_bkpt_print_it;
15579 ops->print_mention = internal_bkpt_print_mention;
15580
15581 /* Momentary breakpoints. */
15582 ops = &momentary_breakpoint_ops;
15583 *ops = bkpt_base_breakpoint_ops;
15584 ops->re_set = momentary_bkpt_re_set;
15585 ops->check_status = momentary_bkpt_check_status;
15586 ops->print_it = momentary_bkpt_print_it;
15587 ops->print_mention = momentary_bkpt_print_mention;
15588
15589 /* Probe breakpoints. */
15590 ops = &bkpt_probe_breakpoint_ops;
15591 *ops = bkpt_breakpoint_ops;
15592 ops->insert_location = bkpt_probe_insert_location;
15593 ops->remove_location = bkpt_probe_remove_location;
15594 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15595 ops->decode_location = bkpt_probe_decode_location;
15596
15597 /* Watchpoints. */
15598 ops = &watchpoint_breakpoint_ops;
15599 *ops = base_breakpoint_ops;
15600 ops->re_set = re_set_watchpoint;
15601 ops->insert_location = insert_watchpoint;
15602 ops->remove_location = remove_watchpoint;
15603 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15604 ops->check_status = check_status_watchpoint;
15605 ops->resources_needed = resources_needed_watchpoint;
15606 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15607 ops->print_it = print_it_watchpoint;
15608 ops->print_mention = print_mention_watchpoint;
15609 ops->print_recreate = print_recreate_watchpoint;
15610 ops->explains_signal = explains_signal_watchpoint;
15611
15612 /* Masked watchpoints. */
15613 ops = &masked_watchpoint_breakpoint_ops;
15614 *ops = watchpoint_breakpoint_ops;
15615 ops->insert_location = insert_masked_watchpoint;
15616 ops->remove_location = remove_masked_watchpoint;
15617 ops->resources_needed = resources_needed_masked_watchpoint;
15618 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15619 ops->print_it = print_it_masked_watchpoint;
15620 ops->print_one_detail = print_one_detail_masked_watchpoint;
15621 ops->print_mention = print_mention_masked_watchpoint;
15622 ops->print_recreate = print_recreate_masked_watchpoint;
15623
15624 /* Tracepoints. */
15625 ops = &tracepoint_breakpoint_ops;
15626 *ops = base_breakpoint_ops;
15627 ops->re_set = tracepoint_re_set;
15628 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15629 ops->print_one_detail = tracepoint_print_one_detail;
15630 ops->print_mention = tracepoint_print_mention;
15631 ops->print_recreate = tracepoint_print_recreate;
15632 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15633 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15634 ops->decode_location = tracepoint_decode_location;
15635
15636 /* Probe tracepoints. */
15637 ops = &tracepoint_probe_breakpoint_ops;
15638 *ops = tracepoint_breakpoint_ops;
15639 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15640 ops->decode_location = tracepoint_probe_decode_location;
15641
15642 /* Static tracepoints with marker (`-m'). */
15643 ops = &strace_marker_breakpoint_ops;
15644 *ops = tracepoint_breakpoint_ops;
15645 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15646 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15647 ops->decode_location = strace_marker_decode_location;
15648
15649 /* Fork catchpoints. */
15650 ops = &catch_fork_breakpoint_ops;
15651 *ops = base_breakpoint_ops;
15652 ops->insert_location = insert_catch_fork;
15653 ops->remove_location = remove_catch_fork;
15654 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15655 ops->print_it = print_it_catch_fork;
15656 ops->print_one = print_one_catch_fork;
15657 ops->print_mention = print_mention_catch_fork;
15658 ops->print_recreate = print_recreate_catch_fork;
15659
15660 /* Vfork catchpoints. */
15661 ops = &catch_vfork_breakpoint_ops;
15662 *ops = base_breakpoint_ops;
15663 ops->insert_location = insert_catch_vfork;
15664 ops->remove_location = remove_catch_vfork;
15665 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15666 ops->print_it = print_it_catch_vfork;
15667 ops->print_one = print_one_catch_vfork;
15668 ops->print_mention = print_mention_catch_vfork;
15669 ops->print_recreate = print_recreate_catch_vfork;
15670
15671 /* Exec catchpoints. */
15672 ops = &catch_exec_breakpoint_ops;
15673 *ops = base_breakpoint_ops;
15674 ops->insert_location = insert_catch_exec;
15675 ops->remove_location = remove_catch_exec;
15676 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15677 ops->print_it = print_it_catch_exec;
15678 ops->print_one = print_one_catch_exec;
15679 ops->print_mention = print_mention_catch_exec;
15680 ops->print_recreate = print_recreate_catch_exec;
15681
15682 /* Solib-related catchpoints. */
15683 ops = &catch_solib_breakpoint_ops;
15684 *ops = base_breakpoint_ops;
15685 ops->insert_location = insert_catch_solib;
15686 ops->remove_location = remove_catch_solib;
15687 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15688 ops->check_status = check_status_catch_solib;
15689 ops->print_it = print_it_catch_solib;
15690 ops->print_one = print_one_catch_solib;
15691 ops->print_mention = print_mention_catch_solib;
15692 ops->print_recreate = print_recreate_catch_solib;
15693
15694 ops = &dprintf_breakpoint_ops;
15695 *ops = bkpt_base_breakpoint_ops;
15696 ops->re_set = dprintf_re_set;
15697 ops->resources_needed = bkpt_resources_needed;
15698 ops->print_it = bkpt_print_it;
15699 ops->print_mention = bkpt_print_mention;
15700 ops->print_recreate = dprintf_print_recreate;
15701 ops->after_condition_true = dprintf_after_condition_true;
15702 ops->breakpoint_hit = dprintf_breakpoint_hit;
15703 }
15704
15705 /* Chain containing all defined "enable breakpoint" subcommands. */
15706
15707 static struct cmd_list_element *enablebreaklist = NULL;
15708
15709 /* See breakpoint.h. */
15710
15711 cmd_list_element *commands_cmd_element = nullptr;
15712
15713 void _initialize_breakpoint ();
15714 void
15715 _initialize_breakpoint ()
15716 {
15717 struct cmd_list_element *c;
15718
15719 initialize_breakpoint_ops ();
15720
15721 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib);
15722 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile);
15723 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change);
15724
15725 breakpoint_chain = 0;
15726 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15727 before a breakpoint is set. */
15728 breakpoint_count = 0;
15729
15730 tracepoint_count = 0;
15731
15732 add_com ("ignore", class_breakpoint, ignore_command, _("\
15733 Set ignore-count of breakpoint number N to COUNT.\n\
15734 Usage is `ignore N COUNT'."));
15735
15736 commands_cmd_element = add_com ("commands", class_breakpoint,
15737 commands_command, _("\
15738 Set commands to be executed when the given breakpoints are hit.\n\
15739 Give a space-separated breakpoint list as argument after \"commands\".\n\
15740 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15741 (e.g. `5-7').\n\
15742 With no argument, the targeted breakpoint is the last one set.\n\
15743 The commands themselves follow starting on the next line.\n\
15744 Type a line containing \"end\" to indicate the end of them.\n\
15745 Give \"silent\" as the first line to make the breakpoint silent;\n\
15746 then no output is printed when it is hit, except what the commands print."));
15747
15748 const auto cc_opts = make_condition_command_options_def_group (nullptr);
15749 static std::string condition_command_help
15750 = gdb::option::build_help (_("\
15751 Specify breakpoint number N to break only if COND is true.\n\
15752 Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
15753 is an expression to be evaluated whenever breakpoint N is reached.\n\
15754 \n\
15755 Options:\n\
15756 %OPTIONS%"), cc_opts);
15757
15758 c = add_com ("condition", class_breakpoint, condition_command,
15759 condition_command_help.c_str ());
15760 set_cmd_completer_handle_brkchars (c, condition_completer);
15761
15762 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15763 Set a temporary breakpoint.\n\
15764 Like \"break\" except the breakpoint is only temporary,\n\
15765 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15766 by using \"enable delete\" on the breakpoint number.\n\
15767 \n"
15768 BREAK_ARGS_HELP ("tbreak")));
15769 set_cmd_completer (c, location_completer);
15770
15771 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15772 Set a hardware assisted breakpoint.\n\
15773 Like \"break\" except the breakpoint requires hardware support,\n\
15774 some target hardware may not have this support.\n\
15775 \n"
15776 BREAK_ARGS_HELP ("hbreak")));
15777 set_cmd_completer (c, location_completer);
15778
15779 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15780 Set a temporary hardware assisted breakpoint.\n\
15781 Like \"hbreak\" except the breakpoint is only temporary,\n\
15782 so it will be deleted when hit.\n\
15783 \n"
15784 BREAK_ARGS_HELP ("thbreak")));
15785 set_cmd_completer (c, location_completer);
15786
15787 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15788 Enable all or some breakpoints.\n\
15789 Usage: enable [BREAKPOINTNUM]...\n\
15790 Give breakpoint numbers (separated by spaces) as arguments.\n\
15791 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15792 This is used to cancel the effect of the \"disable\" command.\n\
15793 With a subcommand you can enable temporarily."),
15794 &enablelist, "enable ", 1, &cmdlist);
15795
15796 add_com_alias ("en", "enable", class_breakpoint, 1);
15797
15798 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15799 Enable all or some breakpoints.\n\
15800 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
15801 Give breakpoint numbers (separated by spaces) as arguments.\n\
15802 This is used to cancel the effect of the \"disable\" command.\n\
15803 May be abbreviated to simply \"enable\"."),
15804 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
15805
15806 add_cmd ("once", no_class, enable_once_command, _("\
15807 Enable some breakpoints for one hit.\n\
15808 Usage: enable breakpoints once BREAKPOINTNUM...\n\
15809 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15810 &enablebreaklist);
15811
15812 add_cmd ("delete", no_class, enable_delete_command, _("\
15813 Enable some breakpoints and delete when hit.\n\
15814 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
15815 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15816 &enablebreaklist);
15817
15818 add_cmd ("count", no_class, enable_count_command, _("\
15819 Enable some breakpoints for COUNT hits.\n\
15820 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
15821 If a breakpoint is hit while enabled in this fashion,\n\
15822 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15823 &enablebreaklist);
15824
15825 add_cmd ("delete", no_class, enable_delete_command, _("\
15826 Enable some breakpoints and delete when hit.\n\
15827 Usage: enable delete BREAKPOINTNUM...\n\
15828 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15829 &enablelist);
15830
15831 add_cmd ("once", no_class, enable_once_command, _("\
15832 Enable some breakpoints for one hit.\n\
15833 Usage: enable once BREAKPOINTNUM...\n\
15834 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15835 &enablelist);
15836
15837 add_cmd ("count", no_class, enable_count_command, _("\
15838 Enable some breakpoints for COUNT hits.\n\
15839 Usage: enable count COUNT BREAKPOINTNUM...\n\
15840 If a breakpoint is hit while enabled in this fashion,\n\
15841 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15842 &enablelist);
15843
15844 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15845 Disable all or some breakpoints.\n\
15846 Usage: disable [BREAKPOINTNUM]...\n\
15847 Arguments are breakpoint numbers with spaces in between.\n\
15848 To disable all breakpoints, give no argument.\n\
15849 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15850 &disablelist, "disable ", 1, &cmdlist);
15851 add_com_alias ("dis", "disable", class_breakpoint, 1);
15852 add_com_alias ("disa", "disable", class_breakpoint, 1);
15853
15854 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
15855 Disable all or some breakpoints.\n\
15856 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
15857 Arguments are breakpoint numbers with spaces in between.\n\
15858 To disable all breakpoints, give no argument.\n\
15859 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15860 This command may be abbreviated \"disable\"."),
15861 &disablelist);
15862
15863 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15864 Delete all or some breakpoints.\n\
15865 Usage: delete [BREAKPOINTNUM]...\n\
15866 Arguments are breakpoint numbers with spaces in between.\n\
15867 To delete all breakpoints, give no argument.\n\
15868 \n\
15869 Also a prefix command for deletion of other GDB objects."),
15870 &deletelist, "delete ", 1, &cmdlist);
15871 add_com_alias ("d", "delete", class_breakpoint, 1);
15872 add_com_alias ("del", "delete", class_breakpoint, 1);
15873
15874 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
15875 Delete all or some breakpoints or auto-display expressions.\n\
15876 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
15877 Arguments are breakpoint numbers with spaces in between.\n\
15878 To delete all breakpoints, give no argument.\n\
15879 This command may be abbreviated \"delete\"."),
15880 &deletelist);
15881
15882 add_com ("clear", class_breakpoint, clear_command, _("\
15883 Clear breakpoint at specified location.\n\
15884 Argument may be a linespec, explicit, or address location as described below.\n\
15885 \n\
15886 With no argument, clears all breakpoints in the line that the selected frame\n\
15887 is executing in.\n"
15888 "\n" LOCATION_HELP_STRING "\n\n\
15889 See also the \"delete\" command which clears breakpoints by number."));
15890 add_com_alias ("cl", "clear", class_breakpoint, 1);
15891
15892 c = add_com ("break", class_breakpoint, break_command, _("\
15893 Set breakpoint at specified location.\n"
15894 BREAK_ARGS_HELP ("break")));
15895 set_cmd_completer (c, location_completer);
15896
15897 add_com_alias ("b", "break", class_run, 1);
15898 add_com_alias ("br", "break", class_run, 1);
15899 add_com_alias ("bre", "break", class_run, 1);
15900 add_com_alias ("brea", "break", class_run, 1);
15901
15902 if (dbx_commands)
15903 {
15904 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15905 Break in function/address or break at a line in the current file."),
15906 &stoplist, "stop ", 1, &cmdlist);
15907 add_cmd ("in", class_breakpoint, stopin_command,
15908 _("Break in function or address."), &stoplist);
15909 add_cmd ("at", class_breakpoint, stopat_command,
15910 _("Break at a line in the current file."), &stoplist);
15911 add_com ("status", class_info, info_breakpoints_command, _("\
15912 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15913 The \"Type\" column indicates one of:\n\
15914 \tbreakpoint - normal breakpoint\n\
15915 \twatchpoint - watchpoint\n\
15916 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15917 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15918 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15919 address and file/line number respectively.\n\
15920 \n\
15921 Convenience variable \"$_\" and default examine address for \"x\"\n\
15922 are set to the address of the last breakpoint listed unless the command\n\
15923 is prefixed with \"server \".\n\n\
15924 Convenience variable \"$bpnum\" contains the number of the last\n\
15925 breakpoint set."));
15926 }
15927
15928 add_info ("breakpoints", info_breakpoints_command, _("\
15929 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15930 The \"Type\" column indicates one of:\n\
15931 \tbreakpoint - normal breakpoint\n\
15932 \twatchpoint - watchpoint\n\
15933 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15934 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15935 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15936 address and file/line number respectively.\n\
15937 \n\
15938 Convenience variable \"$_\" and default examine address for \"x\"\n\
15939 are set to the address of the last breakpoint listed unless the command\n\
15940 is prefixed with \"server \".\n\n\
15941 Convenience variable \"$bpnum\" contains the number of the last\n\
15942 breakpoint set."));
15943
15944 add_info_alias ("b", "breakpoints", 1);
15945
15946 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15947 Status of all breakpoints, or breakpoint number NUMBER.\n\
15948 The \"Type\" column indicates one of:\n\
15949 \tbreakpoint - normal breakpoint\n\
15950 \twatchpoint - watchpoint\n\
15951 \tlongjmp - internal breakpoint used to step through longjmp()\n\
15952 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15953 \tuntil - internal breakpoint used by the \"until\" command\n\
15954 \tfinish - internal breakpoint used by the \"finish\" command\n\
15955 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15956 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15957 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15958 address and file/line number respectively.\n\
15959 \n\
15960 Convenience variable \"$_\" and default examine address for \"x\"\n\
15961 are set to the address of the last breakpoint listed unless the command\n\
15962 is prefixed with \"server \".\n\n\
15963 Convenience variable \"$bpnum\" contains the number of the last\n\
15964 breakpoint set."),
15965 &maintenanceinfolist);
15966
15967 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
15968 Set catchpoints to catch events."),
15969 &catch_cmdlist, "catch ",
15970 0/*allow-unknown*/, &cmdlist);
15971
15972 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
15973 Set temporary catchpoints to catch events."),
15974 &tcatch_cmdlist, "tcatch ",
15975 0/*allow-unknown*/, &cmdlist);
15976
15977 add_catch_command ("fork", _("Catch calls to fork."),
15978 catch_fork_command_1,
15979 NULL,
15980 (void *) (uintptr_t) catch_fork_permanent,
15981 (void *) (uintptr_t) catch_fork_temporary);
15982 add_catch_command ("vfork", _("Catch calls to vfork."),
15983 catch_fork_command_1,
15984 NULL,
15985 (void *) (uintptr_t) catch_vfork_permanent,
15986 (void *) (uintptr_t) catch_vfork_temporary);
15987 add_catch_command ("exec", _("Catch calls to exec."),
15988 catch_exec_command_1,
15989 NULL,
15990 CATCH_PERMANENT,
15991 CATCH_TEMPORARY);
15992 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15993 Usage: catch load [REGEX]\n\
15994 If REGEX is given, only stop for libraries matching the regular expression."),
15995 catch_load_command_1,
15996 NULL,
15997 CATCH_PERMANENT,
15998 CATCH_TEMPORARY);
15999 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16000 Usage: catch unload [REGEX]\n\
16001 If REGEX is given, only stop for libraries matching the regular expression."),
16002 catch_unload_command_1,
16003 NULL,
16004 CATCH_PERMANENT,
16005 CATCH_TEMPORARY);
16006
16007 const auto opts = make_watch_options_def_group (nullptr);
16008
16009 static const std::string watch_help = gdb::option::build_help (_("\
16010 Set a watchpoint for EXPRESSION.\n\
16011 Usage: watch [-location] EXPRESSION\n\
16012 \n\
16013 Options:\n\
16014 %OPTIONS%\n\
16015 \n\
16016 A watchpoint stops execution of your program whenever the value of\n\
16017 an expression changes."), opts);
16018 c = add_com ("watch", class_breakpoint, watch_command,
16019 watch_help.c_str ());
16020 set_cmd_completer_handle_brkchars (c, watch_command_completer);
16021
16022 static const std::string rwatch_help = gdb::option::build_help (_("\
16023 Set a read watchpoint for EXPRESSION.\n\
16024 Usage: rwatch [-location] EXPRESSION\n\
16025 \n\
16026 Options:\n\
16027 %OPTIONS%\n\
16028 \n\
16029 A read watchpoint stops execution of your program whenever the value of\n\
16030 an expression is read."), opts);
16031 c = add_com ("rwatch", class_breakpoint, rwatch_command,
16032 rwatch_help.c_str ());
16033 set_cmd_completer_handle_brkchars (c, watch_command_completer);
16034
16035 static const std::string awatch_help = gdb::option::build_help (_("\
16036 Set an access watchpoint for EXPRESSION.\n\
16037 Usage: awatch [-location] EXPRESSION\n\
16038 \n\
16039 Options:\n\
16040 %OPTIONS%\n\
16041 \n\
16042 An access watchpoint stops execution of your program whenever the value\n\
16043 of an expression is either read or written."), opts);
16044 c = add_com ("awatch", class_breakpoint, awatch_command,
16045 awatch_help.c_str ());
16046 set_cmd_completer_handle_brkchars (c, watch_command_completer);
16047
16048 add_info ("watchpoints", info_watchpoints_command, _("\
16049 Status of specified watchpoints (all watchpoints if no argument)."));
16050
16051 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16052 respond to changes - contrary to the description. */
16053 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16054 &can_use_hw_watchpoints, _("\
16055 Set debugger's willingness to use watchpoint hardware."), _("\
16056 Show debugger's willingness to use watchpoint hardware."), _("\
16057 If zero, gdb will not use hardware for new watchpoints, even if\n\
16058 such is available. (However, any hardware watchpoints that were\n\
16059 created before setting this to nonzero, will continue to use watchpoint\n\
16060 hardware.)"),
16061 NULL,
16062 show_can_use_hw_watchpoints,
16063 &setlist, &showlist);
16064
16065 can_use_hw_watchpoints = 1;
16066
16067 /* Tracepoint manipulation commands. */
16068
16069 c = add_com ("trace", class_breakpoint, trace_command, _("\
16070 Set a tracepoint at specified location.\n\
16071 \n"
16072 BREAK_ARGS_HELP ("trace") "\n\
16073 Do \"help tracepoints\" for info on other tracepoint commands."));
16074 set_cmd_completer (c, location_completer);
16075
16076 add_com_alias ("tp", "trace", class_breakpoint, 0);
16077 add_com_alias ("tr", "trace", class_breakpoint, 1);
16078 add_com_alias ("tra", "trace", class_breakpoint, 1);
16079 add_com_alias ("trac", "trace", class_breakpoint, 1);
16080
16081 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16082 Set a fast tracepoint at specified location.\n\
16083 \n"
16084 BREAK_ARGS_HELP ("ftrace") "\n\
16085 Do \"help tracepoints\" for info on other tracepoint commands."));
16086 set_cmd_completer (c, location_completer);
16087
16088 c = add_com ("strace", class_breakpoint, strace_command, _("\
16089 Set a static tracepoint at location or marker.\n\
16090 \n\
16091 strace [LOCATION] [if CONDITION]\n\
16092 LOCATION may be a linespec, explicit, or address location (described below) \n\
16093 or -m MARKER_ID.\n\n\
16094 If a marker id is specified, probe the marker with that name. With\n\
16095 no LOCATION, uses current execution address of the selected stack frame.\n\
16096 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16097 This collects arbitrary user data passed in the probe point call to the\n\
16098 tracing library. You can inspect it when analyzing the trace buffer,\n\
16099 by printing the $_sdata variable like any other convenience variable.\n\
16100 \n\
16101 CONDITION is a boolean expression.\n\
16102 \n" LOCATION_HELP_STRING "\n\n\
16103 Multiple tracepoints at one place are permitted, and useful if their\n\
16104 conditions are different.\n\
16105 \n\
16106 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16107 Do \"help tracepoints\" for info on other tracepoint commands."));
16108 set_cmd_completer (c, location_completer);
16109
16110 add_info ("tracepoints", info_tracepoints_command, _("\
16111 Status of specified tracepoints (all tracepoints if no argument).\n\
16112 Convenience variable \"$tpnum\" contains the number of the\n\
16113 last tracepoint set."));
16114
16115 add_info_alias ("tp", "tracepoints", 1);
16116
16117 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16118 Delete specified tracepoints.\n\
16119 Arguments are tracepoint numbers, separated by spaces.\n\
16120 No argument means delete all tracepoints."),
16121 &deletelist);
16122 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16123
16124 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16125 Disable specified tracepoints.\n\
16126 Arguments are tracepoint numbers, separated by spaces.\n\
16127 No argument means disable all tracepoints."),
16128 &disablelist);
16129 deprecate_cmd (c, "disable");
16130
16131 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16132 Enable specified tracepoints.\n\
16133 Arguments are tracepoint numbers, separated by spaces.\n\
16134 No argument means enable all tracepoints."),
16135 &enablelist);
16136 deprecate_cmd (c, "enable");
16137
16138 add_com ("passcount", class_trace, trace_pass_command, _("\
16139 Set the passcount for a tracepoint.\n\
16140 The trace will end when the tracepoint has been passed 'count' times.\n\
16141 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16142 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16143
16144 add_basic_prefix_cmd ("save", class_breakpoint,
16145 _("Save breakpoint definitions as a script."),
16146 &save_cmdlist, "save ",
16147 0/*allow-unknown*/, &cmdlist);
16148
16149 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16150 Save current breakpoint definitions as a script.\n\
16151 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16152 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16153 session to restore them."),
16154 &save_cmdlist);
16155 set_cmd_completer (c, filename_completer);
16156
16157 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16158 Save current tracepoint definitions as a script.\n\
16159 Use the 'source' command in another debug session to restore them."),
16160 &save_cmdlist);
16161 set_cmd_completer (c, filename_completer);
16162
16163 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16164 deprecate_cmd (c, "save tracepoints");
16165
16166 add_basic_prefix_cmd ("breakpoint", class_maintenance, _("\
16167 Breakpoint specific settings.\n\
16168 Configure various breakpoint-specific variables such as\n\
16169 pending breakpoint behavior."),
16170 &breakpoint_set_cmdlist, "set breakpoint ",
16171 0/*allow-unknown*/, &setlist);
16172 add_show_prefix_cmd ("breakpoint", class_maintenance, _("\
16173 Breakpoint specific settings.\n\
16174 Configure various breakpoint-specific variables such as\n\
16175 pending breakpoint behavior."),
16176 &breakpoint_show_cmdlist, "show breakpoint ",
16177 0/*allow-unknown*/, &showlist);
16178
16179 add_setshow_auto_boolean_cmd ("pending", no_class,
16180 &pending_break_support, _("\
16181 Set debugger's behavior regarding pending breakpoints."), _("\
16182 Show debugger's behavior regarding pending breakpoints."), _("\
16183 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16184 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16185 an error. If auto, an unrecognized breakpoint location results in a\n\
16186 user-query to see if a pending breakpoint should be created."),
16187 NULL,
16188 show_pending_break_support,
16189 &breakpoint_set_cmdlist,
16190 &breakpoint_show_cmdlist);
16191
16192 pending_break_support = AUTO_BOOLEAN_AUTO;
16193
16194 add_setshow_boolean_cmd ("auto-hw", no_class,
16195 &automatic_hardware_breakpoints, _("\
16196 Set automatic usage of hardware breakpoints."), _("\
16197 Show automatic usage of hardware breakpoints."), _("\
16198 If set, the debugger will automatically use hardware breakpoints for\n\
16199 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16200 a warning will be emitted for such breakpoints."),
16201 NULL,
16202 show_automatic_hardware_breakpoints,
16203 &breakpoint_set_cmdlist,
16204 &breakpoint_show_cmdlist);
16205
16206 add_setshow_boolean_cmd ("always-inserted", class_support,
16207 &always_inserted_mode, _("\
16208 Set mode for inserting breakpoints."), _("\
16209 Show mode for inserting breakpoints."), _("\
16210 When this mode is on, breakpoints are inserted immediately as soon as\n\
16211 they're created, kept inserted even when execution stops, and removed\n\
16212 only when the user deletes them. When this mode is off (the default),\n\
16213 breakpoints are inserted only when execution continues, and removed\n\
16214 when execution stops."),
16215 NULL,
16216 &show_always_inserted_mode,
16217 &breakpoint_set_cmdlist,
16218 &breakpoint_show_cmdlist);
16219
16220 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16221 condition_evaluation_enums,
16222 &condition_evaluation_mode_1, _("\
16223 Set mode of breakpoint condition evaluation."), _("\
16224 Show mode of breakpoint condition evaluation."), _("\
16225 When this is set to \"host\", breakpoint conditions will be\n\
16226 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16227 breakpoint conditions will be downloaded to the target (if the target\n\
16228 supports such feature) and conditions will be evaluated on the target's side.\n\
16229 If this is set to \"auto\" (default), this will be automatically set to\n\
16230 \"target\" if it supports condition evaluation, otherwise it will\n\
16231 be set to \"host\"."),
16232 &set_condition_evaluation_mode,
16233 &show_condition_evaluation_mode,
16234 &breakpoint_set_cmdlist,
16235 &breakpoint_show_cmdlist);
16236
16237 add_com ("break-range", class_breakpoint, break_range_command, _("\
16238 Set a breakpoint for an address range.\n\
16239 break-range START-LOCATION, END-LOCATION\n\
16240 where START-LOCATION and END-LOCATION can be one of the following:\n\
16241 LINENUM, for that line in the current file,\n\
16242 FILE:LINENUM, for that line in that file,\n\
16243 +OFFSET, for that number of lines after the current line\n\
16244 or the start of the range\n\
16245 FUNCTION, for the first line in that function,\n\
16246 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16247 *ADDRESS, for the instruction at that address.\n\
16248 \n\
16249 The breakpoint will stop execution of the inferior whenever it executes\n\
16250 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16251 range (including START-LOCATION and END-LOCATION)."));
16252
16253 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16254 Set a dynamic printf at specified location.\n\
16255 dprintf location,format string,arg1,arg2,...\n\
16256 location may be a linespec, explicit, or address location.\n"
16257 "\n" LOCATION_HELP_STRING));
16258 set_cmd_completer (c, location_completer);
16259
16260 add_setshow_enum_cmd ("dprintf-style", class_support,
16261 dprintf_style_enums, &dprintf_style, _("\
16262 Set the style of usage for dynamic printf."), _("\
16263 Show the style of usage for dynamic printf."), _("\
16264 This setting chooses how GDB will do a dynamic printf.\n\
16265 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16266 console, as with the \"printf\" command.\n\
16267 If the value is \"call\", the print is done by calling a function in your\n\
16268 program; by default printf(), but you can choose a different function or\n\
16269 output stream by setting dprintf-function and dprintf-channel."),
16270 update_dprintf_commands, NULL,
16271 &setlist, &showlist);
16272
16273 dprintf_function = xstrdup ("printf");
16274 add_setshow_string_cmd ("dprintf-function", class_support,
16275 &dprintf_function, _("\
16276 Set the function to use for dynamic printf."), _("\
16277 Show the function to use for dynamic printf."), NULL,
16278 update_dprintf_commands, NULL,
16279 &setlist, &showlist);
16280
16281 dprintf_channel = xstrdup ("");
16282 add_setshow_string_cmd ("dprintf-channel", class_support,
16283 &dprintf_channel, _("\
16284 Set the channel to use for dynamic printf."), _("\
16285 Show the channel to use for dynamic printf."), NULL,
16286 update_dprintf_commands, NULL,
16287 &setlist, &showlist);
16288
16289 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16290 &disconnected_dprintf, _("\
16291 Set whether dprintf continues after GDB disconnects."), _("\
16292 Show whether dprintf continues after GDB disconnects."), _("\
16293 Use this to let dprintf commands continue to hit and produce output\n\
16294 even if GDB disconnects or detaches from the target."),
16295 NULL,
16296 NULL,
16297 &setlist, &showlist);
16298
16299 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16300 Target agent only formatted printing, like the C \"printf\" function.\n\
16301 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
16302 This supports most C printf format specifications, like %s, %d, etc.\n\
16303 This is useful for formatted output in user-defined commands."));
16304
16305 automatic_hardware_breakpoints = true;
16306
16307 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed);
16308 gdb::observers::thread_exit.attach (remove_threaded_breakpoints);
16309 }