Add an expr::operation_up to struct expression
[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, b->exp->op.get (), &v, &result,
1907 &val_chain, false);
1908
1909 /* Avoid setting b->val if it's already set. The meaning of
1910 b->val is 'the last value' user saw, and we should update
1911 it only if we reported that last value to user. As it
1912 happens, the code that reports it updates b->val directly.
1913 We don't keep track of the memory value for masked
1914 watchpoints. */
1915 if (!b->val_valid && !is_masked_watchpoint (b))
1916 {
1917 if (b->val_bitsize != 0)
1918 v = extract_bitfield_from_watchpoint_value (b, v);
1919 b->val = release_value (v);
1920 b->val_valid = true;
1921 }
1922
1923 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1924
1925 /* Look at each value on the value chain. */
1926 gdb_assert (!val_chain.empty ());
1927 for (const value_ref_ptr &iter : val_chain)
1928 {
1929 v = iter.get ();
1930
1931 /* If it's a memory location, and GDB actually needed
1932 its contents to evaluate the expression, then we
1933 must watch it. If the first value returned is
1934 still lazy, that means an error occurred reading it;
1935 watch it anyway in case it becomes readable. */
1936 if (VALUE_LVAL (v) == lval_memory
1937 && (v == val_chain[0] || ! value_lazy (v)))
1938 {
1939 struct type *vtype = check_typedef (value_type (v));
1940
1941 /* We only watch structs and arrays if user asked
1942 for it explicitly, never if they just happen to
1943 appear in the middle of some value chain. */
1944 if (v == result
1945 || (vtype->code () != TYPE_CODE_STRUCT
1946 && vtype->code () != TYPE_CODE_ARRAY))
1947 {
1948 CORE_ADDR addr;
1949 enum target_hw_bp_type type;
1950 struct bp_location *loc, **tmp;
1951 int bitpos = 0, bitsize = 0;
1952
1953 if (value_bitsize (v) != 0)
1954 {
1955 /* Extract the bit parameters out from the bitfield
1956 sub-expression. */
1957 bitpos = value_bitpos (v);
1958 bitsize = value_bitsize (v);
1959 }
1960 else if (v == result && b->val_bitsize != 0)
1961 {
1962 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1963 lvalue whose bit parameters are saved in the fields
1964 VAL_BITPOS and VAL_BITSIZE. */
1965 bitpos = b->val_bitpos;
1966 bitsize = b->val_bitsize;
1967 }
1968
1969 addr = value_address (v);
1970 if (bitsize != 0)
1971 {
1972 /* Skip the bytes that don't contain the bitfield. */
1973 addr += bitpos / 8;
1974 }
1975
1976 type = hw_write;
1977 if (b->type == bp_read_watchpoint)
1978 type = hw_read;
1979 else if (b->type == bp_access_watchpoint)
1980 type = hw_access;
1981
1982 loc = allocate_bp_location (b);
1983 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1984 ;
1985 *tmp = loc;
1986 loc->gdbarch = value_type (v)->arch ();
1987
1988 loc->pspace = frame_pspace;
1989 loc->address = address_significant (loc->gdbarch, addr);
1990
1991 if (bitsize != 0)
1992 {
1993 /* Just cover the bytes that make up the bitfield. */
1994 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1995 }
1996 else
1997 loc->length = TYPE_LENGTH (value_type (v));
1998
1999 loc->watchpoint_type = type;
2000 }
2001 }
2002 }
2003
2004 /* Change the type of breakpoint between hardware assisted or
2005 an ordinary watchpoint depending on the hardware support
2006 and free hardware slots. REPARSE is set when the inferior
2007 is started. */
2008 if (reparse)
2009 {
2010 int reg_cnt;
2011 enum bp_loc_type loc_type;
2012 struct bp_location *bl;
2013
2014 reg_cnt = can_use_hardware_watchpoint (val_chain);
2015
2016 if (reg_cnt)
2017 {
2018 int i, target_resources_ok, other_type_used;
2019 enum bptype type;
2020
2021 /* Use an exact watchpoint when there's only one memory region to be
2022 watched, and only one debug register is needed to watch it. */
2023 b->exact = target_exact_watchpoints && reg_cnt == 1;
2024
2025 /* We need to determine how many resources are already
2026 used for all other hardware watchpoints plus this one
2027 to see if we still have enough resources to also fit
2028 this watchpoint in as well. */
2029
2030 /* If this is a software watchpoint, we try to turn it
2031 to a hardware one -- count resources as if B was of
2032 hardware watchpoint type. */
2033 type = b->type;
2034 if (type == bp_watchpoint)
2035 type = bp_hardware_watchpoint;
2036
2037 /* This watchpoint may or may not have been placed on
2038 the list yet at this point (it won't be in the list
2039 if we're trying to create it for the first time,
2040 through watch_command), so always account for it
2041 manually. */
2042
2043 /* Count resources used by all watchpoints except B. */
2044 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
2045
2046 /* Add in the resources needed for B. */
2047 i += hw_watchpoint_use_count (b);
2048
2049 target_resources_ok
2050 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2051 if (target_resources_ok <= 0)
2052 {
2053 int sw_mode = b->ops->works_in_software_mode (b);
2054
2055 if (target_resources_ok == 0 && !sw_mode)
2056 error (_("Target does not support this type of "
2057 "hardware watchpoint."));
2058 else if (target_resources_ok < 0 && !sw_mode)
2059 error (_("There are not enough available hardware "
2060 "resources for this watchpoint."));
2061
2062 /* Downgrade to software watchpoint. */
2063 b->type = bp_watchpoint;
2064 }
2065 else
2066 {
2067 /* If this was a software watchpoint, we've just
2068 found we have enough resources to turn it to a
2069 hardware watchpoint. Otherwise, this is a
2070 nop. */
2071 b->type = type;
2072 }
2073 }
2074 else if (!b->ops->works_in_software_mode (b))
2075 {
2076 if (!can_use_hw_watchpoints)
2077 error (_("Can't set read/access watchpoint when "
2078 "hardware watchpoints are disabled."));
2079 else
2080 error (_("Expression cannot be implemented with "
2081 "read/access watchpoint."));
2082 }
2083 else
2084 b->type = bp_watchpoint;
2085
2086 loc_type = (b->type == bp_watchpoint? bp_loc_other
2087 : bp_loc_hardware_watchpoint);
2088 for (bl = b->loc; bl; bl = bl->next)
2089 bl->loc_type = loc_type;
2090 }
2091
2092 /* If a software watchpoint is not watching any memory, then the
2093 above left it without any location set up. But,
2094 bpstat_stop_status requires a location to be able to report
2095 stops, so make sure there's at least a dummy one. */
2096 if (b->type == bp_watchpoint && b->loc == NULL)
2097 software_watchpoint_add_no_memory_location (b, frame_pspace);
2098 }
2099 else if (!within_current_scope)
2100 {
2101 printf_filtered (_("\
2102 Watchpoint %d deleted because the program has left the block\n\
2103 in which its expression is valid.\n"),
2104 b->number);
2105 watchpoint_del_at_next_stop (b);
2106 }
2107
2108 /* Restore the selected frame. */
2109 if (frame_saved)
2110 select_frame (frame_find_by_id (saved_frame_id));
2111 }
2112
2113
2114 /* Returns 1 iff breakpoint location should be
2115 inserted in the inferior. We don't differentiate the type of BL's owner
2116 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2117 breakpoint_ops is not defined, because in insert_bp_location,
2118 tracepoint's insert_location will not be called. */
2119 static int
2120 should_be_inserted (struct bp_location *bl)
2121 {
2122 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2123 return 0;
2124
2125 if (bl->owner->disposition == disp_del_at_next_stop)
2126 return 0;
2127
2128 if (!bl->enabled || bl->disabled_by_cond
2129 || bl->shlib_disabled || bl->duplicate)
2130 return 0;
2131
2132 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2133 return 0;
2134
2135 /* This is set for example, when we're attached to the parent of a
2136 vfork, and have detached from the child. The child is running
2137 free, and we expect it to do an exec or exit, at which point the
2138 OS makes the parent schedulable again (and the target reports
2139 that the vfork is done). Until the child is done with the shared
2140 memory region, do not insert breakpoints in the parent, otherwise
2141 the child could still trip on the parent's breakpoints. Since
2142 the parent is blocked anyway, it won't miss any breakpoint. */
2143 if (bl->pspace->breakpoints_not_allowed)
2144 return 0;
2145
2146 /* Don't insert a breakpoint if we're trying to step past its
2147 location, except if the breakpoint is a single-step breakpoint,
2148 and the breakpoint's thread is the thread which is stepping past
2149 a breakpoint. */
2150 if ((bl->loc_type == bp_loc_software_breakpoint
2151 || bl->loc_type == bp_loc_hardware_breakpoint)
2152 && stepping_past_instruction_at (bl->pspace->aspace,
2153 bl->address)
2154 /* The single-step breakpoint may be inserted at the location
2155 we're trying to step if the instruction branches to itself.
2156 However, the instruction won't be executed at all and it may
2157 break the semantics of the instruction, for example, the
2158 instruction is a conditional branch or updates some flags.
2159 We can't fix it unless GDB is able to emulate the instruction
2160 or switch to displaced stepping. */
2161 && !(bl->owner->type == bp_single_step
2162 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2163 {
2164 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2165 paddress (bl->gdbarch, bl->address));
2166 return 0;
2167 }
2168
2169 /* Don't insert watchpoints if we're trying to step past the
2170 instruction that triggered one. */
2171 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2172 && stepping_past_nonsteppable_watchpoint ())
2173 {
2174 infrun_debug_printf ("stepping past non-steppable watchpoint. "
2175 "skipping watchpoint at %s:%d",
2176 paddress (bl->gdbarch, bl->address), bl->length);
2177 return 0;
2178 }
2179
2180 return 1;
2181 }
2182
2183 /* Same as should_be_inserted but does the check assuming
2184 that the location is not duplicated. */
2185
2186 static int
2187 unduplicated_should_be_inserted (struct bp_location *bl)
2188 {
2189 int result;
2190 const int save_duplicate = bl->duplicate;
2191
2192 bl->duplicate = 0;
2193 result = should_be_inserted (bl);
2194 bl->duplicate = save_duplicate;
2195 return result;
2196 }
2197
2198 /* Parses a conditional described by an expression COND into an
2199 agent expression bytecode suitable for evaluation
2200 by the bytecode interpreter. Return NULL if there was
2201 any error during parsing. */
2202
2203 static agent_expr_up
2204 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2205 {
2206 if (cond == NULL)
2207 return NULL;
2208
2209 agent_expr_up aexpr;
2210
2211 /* We don't want to stop processing, so catch any errors
2212 that may show up. */
2213 try
2214 {
2215 aexpr = gen_eval_for_expr (scope, cond);
2216 }
2217
2218 catch (const gdb_exception_error &ex)
2219 {
2220 /* If we got here, it means the condition could not be parsed to a valid
2221 bytecode expression and thus can't be evaluated on the target's side.
2222 It's no use iterating through the conditions. */
2223 }
2224
2225 /* We have a valid agent expression. */
2226 return aexpr;
2227 }
2228
2229 /* Based on location BL, create a list of breakpoint conditions to be
2230 passed on to the target. If we have duplicated locations with different
2231 conditions, we will add such conditions to the list. The idea is that the
2232 target will evaluate the list of conditions and will only notify GDB when
2233 one of them is true. */
2234
2235 static void
2236 build_target_condition_list (struct bp_location *bl)
2237 {
2238 struct bp_location **locp = NULL, **loc2p;
2239 int null_condition_or_parse_error = 0;
2240 int modified = bl->needs_update;
2241 struct bp_location *loc;
2242
2243 /* Release conditions left over from a previous insert. */
2244 bl->target_info.conditions.clear ();
2245
2246 /* This is only meaningful if the target is
2247 evaluating conditions and if the user has
2248 opted for condition evaluation on the target's
2249 side. */
2250 if (gdb_evaluates_breakpoint_condition_p ()
2251 || !target_supports_evaluation_of_breakpoint_conditions ())
2252 return;
2253
2254 /* Do a first pass to check for locations with no assigned
2255 conditions or conditions that fail to parse to a valid agent
2256 expression bytecode. If any of these happen, then it's no use to
2257 send conditions to the target since this location will always
2258 trigger and generate a response back to GDB. Note we consider
2259 all locations at the same address irrespective of type, i.e.,
2260 even if the locations aren't considered duplicates (e.g.,
2261 software breakpoint and hardware breakpoint at the same
2262 address). */
2263 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2264 {
2265 loc = (*loc2p);
2266 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2267 {
2268 if (modified)
2269 {
2270 /* Re-parse the conditions since something changed. In that
2271 case we already freed the condition bytecodes (see
2272 force_breakpoint_reinsertion). We just
2273 need to parse the condition to bytecodes again. */
2274 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2275 loc->cond.get ());
2276 }
2277
2278 /* If we have a NULL bytecode expression, it means something
2279 went wrong or we have a null condition expression. */
2280 if (!loc->cond_bytecode)
2281 {
2282 null_condition_or_parse_error = 1;
2283 break;
2284 }
2285 }
2286 }
2287
2288 /* If any of these happened, it means we will have to evaluate the conditions
2289 for the location's address on gdb's side. It is no use keeping bytecodes
2290 for all the other duplicate locations, thus we free all of them here.
2291
2292 This is so we have a finer control over which locations' conditions are
2293 being evaluated by GDB or the remote stub. */
2294 if (null_condition_or_parse_error)
2295 {
2296 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2297 {
2298 loc = (*loc2p);
2299 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2300 {
2301 /* Only go as far as the first NULL bytecode is
2302 located. */
2303 if (!loc->cond_bytecode)
2304 return;
2305
2306 loc->cond_bytecode.reset ();
2307 }
2308 }
2309 }
2310
2311 /* No NULL conditions or failed bytecode generation. Build a
2312 condition list for this location's address. If we have software
2313 and hardware locations at the same address, they aren't
2314 considered duplicates, but we still marge all the conditions
2315 anyway, as it's simpler, and doesn't really make a practical
2316 difference. */
2317 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2318 {
2319 loc = (*loc2p);
2320 if (loc->cond
2321 && is_breakpoint (loc->owner)
2322 && loc->pspace->num == bl->pspace->num
2323 && loc->owner->enable_state == bp_enabled
2324 && loc->enabled
2325 && !loc->disabled_by_cond)
2326 {
2327 /* Add the condition to the vector. This will be used later
2328 to send the conditions to the target. */
2329 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2330 }
2331 }
2332
2333 return;
2334 }
2335
2336 /* Parses a command described by string CMD into an agent expression
2337 bytecode suitable for evaluation by the bytecode interpreter.
2338 Return NULL if there was any error during parsing. */
2339
2340 static agent_expr_up
2341 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2342 {
2343 const char *cmdrest;
2344 const char *format_start, *format_end;
2345 struct gdbarch *gdbarch = get_current_arch ();
2346
2347 if (cmd == NULL)
2348 return NULL;
2349
2350 cmdrest = cmd;
2351
2352 if (*cmdrest == ',')
2353 ++cmdrest;
2354 cmdrest = skip_spaces (cmdrest);
2355
2356 if (*cmdrest++ != '"')
2357 error (_("No format string following the location"));
2358
2359 format_start = cmdrest;
2360
2361 format_pieces fpieces (&cmdrest);
2362
2363 format_end = cmdrest;
2364
2365 if (*cmdrest++ != '"')
2366 error (_("Bad format string, non-terminated '\"'."));
2367
2368 cmdrest = skip_spaces (cmdrest);
2369
2370 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2371 error (_("Invalid argument syntax"));
2372
2373 if (*cmdrest == ',')
2374 cmdrest++;
2375 cmdrest = skip_spaces (cmdrest);
2376
2377 /* For each argument, make an expression. */
2378
2379 std::vector<struct expression *> argvec;
2380 while (*cmdrest != '\0')
2381 {
2382 const char *cmd1;
2383
2384 cmd1 = cmdrest;
2385 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2386 argvec.push_back (expr.release ());
2387 cmdrest = cmd1;
2388 if (*cmdrest == ',')
2389 ++cmdrest;
2390 }
2391
2392 agent_expr_up aexpr;
2393
2394 /* We don't want to stop processing, so catch any errors
2395 that may show up. */
2396 try
2397 {
2398 aexpr = gen_printf (scope, gdbarch, 0, 0,
2399 format_start, format_end - format_start,
2400 argvec.size (), argvec.data ());
2401 }
2402 catch (const gdb_exception_error &ex)
2403 {
2404 /* If we got here, it means the command could not be parsed to a valid
2405 bytecode expression and thus can't be evaluated on the target's side.
2406 It's no use iterating through the other commands. */
2407 }
2408
2409 /* We have a valid agent expression, return it. */
2410 return aexpr;
2411 }
2412
2413 /* Based on location BL, create a list of breakpoint commands to be
2414 passed on to the target. If we have duplicated locations with
2415 different commands, we will add any such to the list. */
2416
2417 static void
2418 build_target_command_list (struct bp_location *bl)
2419 {
2420 struct bp_location **locp = NULL, **loc2p;
2421 int null_command_or_parse_error = 0;
2422 int modified = bl->needs_update;
2423 struct bp_location *loc;
2424
2425 /* Clear commands left over from a previous insert. */
2426 bl->target_info.tcommands.clear ();
2427
2428 if (!target_can_run_breakpoint_commands ())
2429 return;
2430
2431 /* For now, limit to agent-style dprintf breakpoints. */
2432 if (dprintf_style != dprintf_style_agent)
2433 return;
2434
2435 /* For now, if we have any location at the same address that isn't a
2436 dprintf, don't install the target-side commands, as that would
2437 make the breakpoint not be reported to the core, and we'd lose
2438 control. */
2439 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2440 {
2441 loc = (*loc2p);
2442 if (is_breakpoint (loc->owner)
2443 && loc->pspace->num == bl->pspace->num
2444 && loc->owner->type != bp_dprintf)
2445 return;
2446 }
2447
2448 /* Do a first pass to check for locations with no assigned
2449 conditions or conditions that fail to parse to a valid agent expression
2450 bytecode. If any of these happen, then it's no use to send conditions
2451 to the target since this location will always trigger and generate a
2452 response back to GDB. */
2453 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2454 {
2455 loc = (*loc2p);
2456 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2457 {
2458 if (modified)
2459 {
2460 /* Re-parse the commands since something changed. In that
2461 case we already freed the command bytecodes (see
2462 force_breakpoint_reinsertion). We just
2463 need to parse the command to bytecodes again. */
2464 loc->cmd_bytecode
2465 = parse_cmd_to_aexpr (bl->address,
2466 loc->owner->extra_string);
2467 }
2468
2469 /* If we have a NULL bytecode expression, it means something
2470 went wrong or we have a null command expression. */
2471 if (!loc->cmd_bytecode)
2472 {
2473 null_command_or_parse_error = 1;
2474 break;
2475 }
2476 }
2477 }
2478
2479 /* If anything failed, then we're not doing target-side commands,
2480 and so clean up. */
2481 if (null_command_or_parse_error)
2482 {
2483 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2484 {
2485 loc = (*loc2p);
2486 if (is_breakpoint (loc->owner)
2487 && loc->pspace->num == bl->pspace->num)
2488 {
2489 /* Only go as far as the first NULL bytecode is
2490 located. */
2491 if (loc->cmd_bytecode == NULL)
2492 return;
2493
2494 loc->cmd_bytecode.reset ();
2495 }
2496 }
2497 }
2498
2499 /* No NULL commands or failed bytecode generation. Build a command
2500 list for all duplicate locations at this location's address.
2501 Note that here we must care for whether the breakpoint location
2502 types are considered duplicates, otherwise, say, if we have a
2503 software and hardware location at the same address, the target
2504 could end up running the commands twice. For the moment, we only
2505 support targets-side commands with dprintf, but it doesn't hurt
2506 to be pedantically correct in case that changes. */
2507 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2508 {
2509 loc = (*loc2p);
2510 if (breakpoint_locations_match (bl, loc)
2511 && loc->owner->extra_string
2512 && is_breakpoint (loc->owner)
2513 && loc->pspace->num == bl->pspace->num
2514 && loc->owner->enable_state == bp_enabled
2515 && loc->enabled
2516 && !loc->disabled_by_cond)
2517 {
2518 /* Add the command to the vector. This will be used later
2519 to send the commands to the target. */
2520 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2521 }
2522 }
2523
2524 bl->target_info.persist = 0;
2525 /* Maybe flag this location as persistent. */
2526 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2527 bl->target_info.persist = 1;
2528 }
2529
2530 /* Return the kind of breakpoint on address *ADDR. Get the kind
2531 of breakpoint according to ADDR except single-step breakpoint.
2532 Get the kind of single-step breakpoint according to the current
2533 registers state. */
2534
2535 static int
2536 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2537 {
2538 if (bl->owner->type == bp_single_step)
2539 {
2540 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2541 struct regcache *regcache;
2542
2543 regcache = get_thread_regcache (thr);
2544
2545 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2546 regcache, addr);
2547 }
2548 else
2549 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2550 }
2551
2552 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2553 location. Any error messages are printed to TMP_ERROR_STREAM; and
2554 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2555 Returns 0 for success, 1 if the bp_location type is not supported or
2556 -1 for failure.
2557
2558 NOTE drow/2003-09-09: This routine could be broken down to an
2559 object-style method for each breakpoint or catchpoint type. */
2560 static int
2561 insert_bp_location (struct bp_location *bl,
2562 struct ui_file *tmp_error_stream,
2563 int *disabled_breaks,
2564 int *hw_breakpoint_error,
2565 int *hw_bp_error_explained_already)
2566 {
2567 gdb_exception bp_excpt;
2568
2569 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2570 return 0;
2571
2572 /* Note we don't initialize bl->target_info, as that wipes out
2573 the breakpoint location's shadow_contents if the breakpoint
2574 is still inserted at that location. This in turn breaks
2575 target_read_memory which depends on these buffers when
2576 a memory read is requested at the breakpoint location:
2577 Once the target_info has been wiped, we fail to see that
2578 we have a breakpoint inserted at that address and thus
2579 read the breakpoint instead of returning the data saved in
2580 the breakpoint location's shadow contents. */
2581 bl->target_info.reqstd_address = bl->address;
2582 bl->target_info.placed_address_space = bl->pspace->aspace;
2583 bl->target_info.length = bl->length;
2584
2585 /* When working with target-side conditions, we must pass all the conditions
2586 for the same breakpoint address down to the target since GDB will not
2587 insert those locations. With a list of breakpoint conditions, the target
2588 can decide when to stop and notify GDB. */
2589
2590 if (is_breakpoint (bl->owner))
2591 {
2592 build_target_condition_list (bl);
2593 build_target_command_list (bl);
2594 /* Reset the modification marker. */
2595 bl->needs_update = 0;
2596 }
2597
2598 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2599 set at a read-only address, then a breakpoint location will have
2600 been changed to hardware breakpoint before we get here. If it is
2601 "off" however, error out before actually trying to insert the
2602 breakpoint, with a nicer error message. */
2603 if (bl->loc_type == bp_loc_software_breakpoint
2604 && !automatic_hardware_breakpoints)
2605 {
2606 mem_region *mr = lookup_mem_region (bl->address);
2607
2608 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2609 {
2610 fprintf_unfiltered (tmp_error_stream,
2611 _("Cannot insert breakpoint %d.\n"
2612 "Cannot set software breakpoint "
2613 "at read-only address %s\n"),
2614 bl->owner->number,
2615 paddress (bl->gdbarch, bl->address));
2616 return 1;
2617 }
2618 }
2619
2620 if (bl->loc_type == bp_loc_software_breakpoint
2621 || bl->loc_type == bp_loc_hardware_breakpoint)
2622 {
2623 /* First check to see if we have to handle an overlay. */
2624 if (overlay_debugging == ovly_off
2625 || bl->section == NULL
2626 || !(section_is_overlay (bl->section)))
2627 {
2628 /* No overlay handling: just set the breakpoint. */
2629 try
2630 {
2631 int val;
2632
2633 val = bl->owner->ops->insert_location (bl);
2634 if (val)
2635 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2636 }
2637 catch (gdb_exception &e)
2638 {
2639 bp_excpt = std::move (e);
2640 }
2641 }
2642 else
2643 {
2644 /* This breakpoint is in an overlay section.
2645 Shall we set a breakpoint at the LMA? */
2646 if (!overlay_events_enabled)
2647 {
2648 /* Yes -- overlay event support is not active,
2649 so we must try to set a breakpoint at the LMA.
2650 This will not work for a hardware breakpoint. */
2651 if (bl->loc_type == bp_loc_hardware_breakpoint)
2652 warning (_("hardware breakpoint %d not supported in overlay!"),
2653 bl->owner->number);
2654 else
2655 {
2656 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2657 bl->section);
2658 /* Set a software (trap) breakpoint at the LMA. */
2659 bl->overlay_target_info = bl->target_info;
2660 bl->overlay_target_info.reqstd_address = addr;
2661
2662 /* No overlay handling: just set the breakpoint. */
2663 try
2664 {
2665 int val;
2666
2667 bl->overlay_target_info.kind
2668 = breakpoint_kind (bl, &addr);
2669 bl->overlay_target_info.placed_address = addr;
2670 val = target_insert_breakpoint (bl->gdbarch,
2671 &bl->overlay_target_info);
2672 if (val)
2673 bp_excpt
2674 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2675 }
2676 catch (gdb_exception &e)
2677 {
2678 bp_excpt = std::move (e);
2679 }
2680
2681 if (bp_excpt.reason != 0)
2682 fprintf_unfiltered (tmp_error_stream,
2683 "Overlay breakpoint %d "
2684 "failed: in ROM?\n",
2685 bl->owner->number);
2686 }
2687 }
2688 /* Shall we set a breakpoint at the VMA? */
2689 if (section_is_mapped (bl->section))
2690 {
2691 /* Yes. This overlay section is mapped into memory. */
2692 try
2693 {
2694 int val;
2695
2696 val = bl->owner->ops->insert_location (bl);
2697 if (val)
2698 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2699 }
2700 catch (gdb_exception &e)
2701 {
2702 bp_excpt = std::move (e);
2703 }
2704 }
2705 else
2706 {
2707 /* No. This breakpoint will not be inserted.
2708 No error, but do not mark the bp as 'inserted'. */
2709 return 0;
2710 }
2711 }
2712
2713 if (bp_excpt.reason != 0)
2714 {
2715 /* Can't set the breakpoint. */
2716
2717 /* In some cases, we might not be able to insert a
2718 breakpoint in a shared library that has already been
2719 removed, but we have not yet processed the shlib unload
2720 event. Unfortunately, some targets that implement
2721 breakpoint insertion themselves can't tell why the
2722 breakpoint insertion failed (e.g., the remote target
2723 doesn't define error codes), so we must treat generic
2724 errors as memory errors. */
2725 if (bp_excpt.reason == RETURN_ERROR
2726 && (bp_excpt.error == GENERIC_ERROR
2727 || bp_excpt.error == MEMORY_ERROR)
2728 && bl->loc_type == bp_loc_software_breakpoint
2729 && (solib_name_from_address (bl->pspace, bl->address)
2730 || shared_objfile_contains_address_p (bl->pspace,
2731 bl->address)))
2732 {
2733 /* See also: disable_breakpoints_in_shlibs. */
2734 bl->shlib_disabled = 1;
2735 gdb::observers::breakpoint_modified.notify (bl->owner);
2736 if (!*disabled_breaks)
2737 {
2738 fprintf_unfiltered (tmp_error_stream,
2739 "Cannot insert breakpoint %d.\n",
2740 bl->owner->number);
2741 fprintf_unfiltered (tmp_error_stream,
2742 "Temporarily disabling shared "
2743 "library breakpoints:\n");
2744 }
2745 *disabled_breaks = 1;
2746 fprintf_unfiltered (tmp_error_stream,
2747 "breakpoint #%d\n", bl->owner->number);
2748 return 0;
2749 }
2750 else
2751 {
2752 if (bl->loc_type == bp_loc_hardware_breakpoint)
2753 {
2754 *hw_breakpoint_error = 1;
2755 *hw_bp_error_explained_already = bp_excpt.message != NULL;
2756 fprintf_unfiltered (tmp_error_stream,
2757 "Cannot insert hardware breakpoint %d%s",
2758 bl->owner->number,
2759 bp_excpt.message ? ":" : ".\n");
2760 if (bp_excpt.message != NULL)
2761 fprintf_unfiltered (tmp_error_stream, "%s.\n",
2762 bp_excpt.what ());
2763 }
2764 else
2765 {
2766 if (bp_excpt.message == NULL)
2767 {
2768 std::string message
2769 = memory_error_message (TARGET_XFER_E_IO,
2770 bl->gdbarch, bl->address);
2771
2772 fprintf_unfiltered (tmp_error_stream,
2773 "Cannot insert breakpoint %d.\n"
2774 "%s\n",
2775 bl->owner->number, message.c_str ());
2776 }
2777 else
2778 {
2779 fprintf_unfiltered (tmp_error_stream,
2780 "Cannot insert breakpoint %d: %s\n",
2781 bl->owner->number,
2782 bp_excpt.what ());
2783 }
2784 }
2785 return 1;
2786
2787 }
2788 }
2789 else
2790 bl->inserted = 1;
2791
2792 return 0;
2793 }
2794
2795 else if (bl->loc_type == bp_loc_hardware_watchpoint
2796 /* NOTE drow/2003-09-08: This state only exists for removing
2797 watchpoints. It's not clear that it's necessary... */
2798 && bl->owner->disposition != disp_del_at_next_stop)
2799 {
2800 int val;
2801
2802 gdb_assert (bl->owner->ops != NULL
2803 && bl->owner->ops->insert_location != NULL);
2804
2805 val = bl->owner->ops->insert_location (bl);
2806
2807 /* If trying to set a read-watchpoint, and it turns out it's not
2808 supported, try emulating one with an access watchpoint. */
2809 if (val == 1 && bl->watchpoint_type == hw_read)
2810 {
2811 struct bp_location *loc, **loc_temp;
2812
2813 /* But don't try to insert it, if there's already another
2814 hw_access location that would be considered a duplicate
2815 of this one. */
2816 ALL_BP_LOCATIONS (loc, loc_temp)
2817 if (loc != bl
2818 && loc->watchpoint_type == hw_access
2819 && watchpoint_locations_match (bl, loc))
2820 {
2821 bl->duplicate = 1;
2822 bl->inserted = 1;
2823 bl->target_info = loc->target_info;
2824 bl->watchpoint_type = hw_access;
2825 val = 0;
2826 break;
2827 }
2828
2829 if (val == 1)
2830 {
2831 bl->watchpoint_type = hw_access;
2832 val = bl->owner->ops->insert_location (bl);
2833
2834 if (val)
2835 /* Back to the original value. */
2836 bl->watchpoint_type = hw_read;
2837 }
2838 }
2839
2840 bl->inserted = (val == 0);
2841 }
2842
2843 else if (bl->owner->type == bp_catchpoint)
2844 {
2845 int val;
2846
2847 gdb_assert (bl->owner->ops != NULL
2848 && bl->owner->ops->insert_location != NULL);
2849
2850 val = bl->owner->ops->insert_location (bl);
2851 if (val)
2852 {
2853 bl->owner->enable_state = bp_disabled;
2854
2855 if (val == 1)
2856 warning (_("\
2857 Error inserting catchpoint %d: Your system does not support this type\n\
2858 of catchpoint."), bl->owner->number);
2859 else
2860 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2861 }
2862
2863 bl->inserted = (val == 0);
2864
2865 /* We've already printed an error message if there was a problem
2866 inserting this catchpoint, and we've disabled the catchpoint,
2867 so just return success. */
2868 return 0;
2869 }
2870
2871 return 0;
2872 }
2873
2874 /* This function is called when program space PSPACE is about to be
2875 deleted. It takes care of updating breakpoints to not reference
2876 PSPACE anymore. */
2877
2878 void
2879 breakpoint_program_space_exit (struct program_space *pspace)
2880 {
2881 struct breakpoint *b, *b_temp;
2882 struct bp_location *loc, **loc_temp;
2883
2884 /* Remove any breakpoint that was set through this program space. */
2885 ALL_BREAKPOINTS_SAFE (b, b_temp)
2886 {
2887 if (b->pspace == pspace)
2888 delete_breakpoint (b);
2889 }
2890
2891 /* Breakpoints set through other program spaces could have locations
2892 bound to PSPACE as well. Remove those. */
2893 ALL_BP_LOCATIONS (loc, loc_temp)
2894 {
2895 struct bp_location *tmp;
2896
2897 if (loc->pspace == pspace)
2898 {
2899 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2900 if (loc->owner->loc == loc)
2901 loc->owner->loc = loc->next;
2902 else
2903 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2904 if (tmp->next == loc)
2905 {
2906 tmp->next = loc->next;
2907 break;
2908 }
2909 }
2910 }
2911
2912 /* Now update the global location list to permanently delete the
2913 removed locations above. */
2914 update_global_location_list (UGLL_DONT_INSERT);
2915 }
2916
2917 /* Make sure all breakpoints are inserted in inferior.
2918 Throws exception on any error.
2919 A breakpoint that is already inserted won't be inserted
2920 again, so calling this function twice is safe. */
2921 void
2922 insert_breakpoints (void)
2923 {
2924 struct breakpoint *bpt;
2925
2926 ALL_BREAKPOINTS (bpt)
2927 if (is_hardware_watchpoint (bpt))
2928 {
2929 struct watchpoint *w = (struct watchpoint *) bpt;
2930
2931 update_watchpoint (w, 0 /* don't reparse. */);
2932 }
2933
2934 /* Updating watchpoints creates new locations, so update the global
2935 location list. Explicitly tell ugll to insert locations and
2936 ignore breakpoints_always_inserted_mode. Also,
2937 update_global_location_list tries to "upgrade" software
2938 breakpoints to hardware breakpoints to handle "set breakpoint
2939 auto-hw", so we need to call it even if we don't have new
2940 locations. */
2941 update_global_location_list (UGLL_INSERT);
2942 }
2943
2944 /* Invoke CALLBACK for each of bp_location. */
2945
2946 void
2947 iterate_over_bp_locations (walk_bp_location_callback callback)
2948 {
2949 struct bp_location *loc, **loc_tmp;
2950
2951 ALL_BP_LOCATIONS (loc, loc_tmp)
2952 {
2953 callback (loc, NULL);
2954 }
2955 }
2956
2957 /* This is used when we need to synch breakpoint conditions between GDB and the
2958 target. It is the case with deleting and disabling of breakpoints when using
2959 always-inserted mode. */
2960
2961 static void
2962 update_inserted_breakpoint_locations (void)
2963 {
2964 struct bp_location *bl, **blp_tmp;
2965 int error_flag = 0;
2966 int val = 0;
2967 int disabled_breaks = 0;
2968 int hw_breakpoint_error = 0;
2969 int hw_bp_details_reported = 0;
2970
2971 string_file tmp_error_stream;
2972
2973 /* Explicitly mark the warning -- this will only be printed if
2974 there was an error. */
2975 tmp_error_stream.puts ("Warning:\n");
2976
2977 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2978
2979 ALL_BP_LOCATIONS (bl, blp_tmp)
2980 {
2981 /* We only want to update software breakpoints and hardware
2982 breakpoints. */
2983 if (!is_breakpoint (bl->owner))
2984 continue;
2985
2986 /* We only want to update locations that are already inserted
2987 and need updating. This is to avoid unwanted insertion during
2988 deletion of breakpoints. */
2989 if (!bl->inserted || !bl->needs_update)
2990 continue;
2991
2992 switch_to_program_space_and_thread (bl->pspace);
2993
2994 /* For targets that support global breakpoints, there's no need
2995 to select an inferior to insert breakpoint to. In fact, even
2996 if we aren't attached to any process yet, we should still
2997 insert breakpoints. */
2998 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2999 && (inferior_ptid == null_ptid || !target_has_execution ()))
3000 continue;
3001
3002 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3003 &hw_breakpoint_error, &hw_bp_details_reported);
3004 if (val)
3005 error_flag = val;
3006 }
3007
3008 if (error_flag)
3009 {
3010 target_terminal::ours_for_output ();
3011 error_stream (tmp_error_stream);
3012 }
3013 }
3014
3015 /* Used when starting or continuing the program. */
3016
3017 static void
3018 insert_breakpoint_locations (void)
3019 {
3020 struct breakpoint *bpt;
3021 struct bp_location *bl, **blp_tmp;
3022 int error_flag = 0;
3023 int val = 0;
3024 int disabled_breaks = 0;
3025 int hw_breakpoint_error = 0;
3026 int hw_bp_error_explained_already = 0;
3027
3028 string_file tmp_error_stream;
3029
3030 /* Explicitly mark the warning -- this will only be printed if
3031 there was an error. */
3032 tmp_error_stream.puts ("Warning:\n");
3033
3034 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3035
3036 ALL_BP_LOCATIONS (bl, blp_tmp)
3037 {
3038 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3039 continue;
3040
3041 /* There is no point inserting thread-specific breakpoints if
3042 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3043 has BL->OWNER always non-NULL. */
3044 if (bl->owner->thread != -1
3045 && !valid_global_thread_id (bl->owner->thread))
3046 continue;
3047
3048 switch_to_program_space_and_thread (bl->pspace);
3049
3050 /* For targets that support global breakpoints, there's no need
3051 to select an inferior to insert breakpoint to. In fact, even
3052 if we aren't attached to any process yet, we should still
3053 insert breakpoints. */
3054 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3055 && (inferior_ptid == null_ptid || !target_has_execution ()))
3056 continue;
3057
3058 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3059 &hw_breakpoint_error, &hw_bp_error_explained_already);
3060 if (val)
3061 error_flag = val;
3062 }
3063
3064 /* If we failed to insert all locations of a watchpoint, remove
3065 them, as half-inserted watchpoint is of limited use. */
3066 ALL_BREAKPOINTS (bpt)
3067 {
3068 int some_failed = 0;
3069 struct bp_location *loc;
3070
3071 if (!is_hardware_watchpoint (bpt))
3072 continue;
3073
3074 if (!breakpoint_enabled (bpt))
3075 continue;
3076
3077 if (bpt->disposition == disp_del_at_next_stop)
3078 continue;
3079
3080 for (loc = bpt->loc; loc; loc = loc->next)
3081 if (!loc->inserted && should_be_inserted (loc))
3082 {
3083 some_failed = 1;
3084 break;
3085 }
3086 if (some_failed)
3087 {
3088 for (loc = bpt->loc; loc; loc = loc->next)
3089 if (loc->inserted)
3090 remove_breakpoint (loc);
3091
3092 hw_breakpoint_error = 1;
3093 tmp_error_stream.printf ("Could not insert "
3094 "hardware watchpoint %d.\n",
3095 bpt->number);
3096 error_flag = -1;
3097 }
3098 }
3099
3100 if (error_flag)
3101 {
3102 /* If a hardware breakpoint or watchpoint was inserted, add a
3103 message about possibly exhausted resources. */
3104 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3105 {
3106 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3107 You may have requested too many hardware breakpoints/watchpoints.\n");
3108 }
3109 target_terminal::ours_for_output ();
3110 error_stream (tmp_error_stream);
3111 }
3112 }
3113
3114 /* Used when the program stops.
3115 Returns zero if successful, or non-zero if there was a problem
3116 removing a breakpoint location. */
3117
3118 int
3119 remove_breakpoints (void)
3120 {
3121 struct bp_location *bl, **blp_tmp;
3122 int val = 0;
3123
3124 ALL_BP_LOCATIONS (bl, blp_tmp)
3125 {
3126 if (bl->inserted && !is_tracepoint (bl->owner))
3127 val |= remove_breakpoint (bl);
3128 }
3129 return val;
3130 }
3131
3132 /* When a thread exits, remove breakpoints that are related to
3133 that thread. */
3134
3135 static void
3136 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3137 {
3138 struct breakpoint *b, *b_tmp;
3139
3140 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3141 {
3142 if (b->thread == tp->global_num && user_breakpoint_p (b))
3143 {
3144 b->disposition = disp_del_at_next_stop;
3145
3146 printf_filtered (_("\
3147 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3148 b->number, print_thread_id (tp));
3149
3150 /* Hide it from the user. */
3151 b->number = 0;
3152 }
3153 }
3154 }
3155
3156 /* See breakpoint.h. */
3157
3158 void
3159 remove_breakpoints_inf (inferior *inf)
3160 {
3161 struct bp_location *bl, **blp_tmp;
3162 int val;
3163
3164 ALL_BP_LOCATIONS (bl, blp_tmp)
3165 {
3166 if (bl->pspace != inf->pspace)
3167 continue;
3168
3169 if (bl->inserted && !bl->target_info.persist)
3170 {
3171 val = remove_breakpoint (bl);
3172 if (val != 0)
3173 return;
3174 }
3175 }
3176 }
3177
3178 static int internal_breakpoint_number = -1;
3179
3180 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3181 If INTERNAL is non-zero, the breakpoint number will be populated
3182 from internal_breakpoint_number and that variable decremented.
3183 Otherwise the breakpoint number will be populated from
3184 breakpoint_count and that value incremented. Internal breakpoints
3185 do not set the internal var bpnum. */
3186 static void
3187 set_breakpoint_number (int internal, struct breakpoint *b)
3188 {
3189 if (internal)
3190 b->number = internal_breakpoint_number--;
3191 else
3192 {
3193 set_breakpoint_count (breakpoint_count + 1);
3194 b->number = breakpoint_count;
3195 }
3196 }
3197
3198 static struct breakpoint *
3199 create_internal_breakpoint (struct gdbarch *gdbarch,
3200 CORE_ADDR address, enum bptype type,
3201 const struct breakpoint_ops *ops)
3202 {
3203 symtab_and_line sal;
3204 sal.pc = address;
3205 sal.section = find_pc_overlay (sal.pc);
3206 sal.pspace = current_program_space;
3207
3208 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
3209 b->number = internal_breakpoint_number--;
3210 b->disposition = disp_donttouch;
3211
3212 return b;
3213 }
3214
3215 static const char *const longjmp_names[] =
3216 {
3217 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3218 };
3219 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3220
3221 /* Per-objfile data private to breakpoint.c. */
3222 struct breakpoint_objfile_data
3223 {
3224 /* Minimal symbol for "_ovly_debug_event" (if any). */
3225 struct bound_minimal_symbol overlay_msym {};
3226
3227 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3228 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
3229
3230 /* True if we have looked for longjmp probes. */
3231 int longjmp_searched = 0;
3232
3233 /* SystemTap probe points for longjmp (if any). These are non-owning
3234 references. */
3235 std::vector<probe *> longjmp_probes;
3236
3237 /* Minimal symbol for "std::terminate()" (if any). */
3238 struct bound_minimal_symbol terminate_msym {};
3239
3240 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3241 struct bound_minimal_symbol exception_msym {};
3242
3243 /* True if we have looked for exception probes. */
3244 int exception_searched = 0;
3245
3246 /* SystemTap probe points for unwinding (if any). These are non-owning
3247 references. */
3248 std::vector<probe *> exception_probes;
3249 };
3250
3251 static const struct objfile_key<breakpoint_objfile_data>
3252 breakpoint_objfile_key;
3253
3254 /* Minimal symbol not found sentinel. */
3255 static struct minimal_symbol msym_not_found;
3256
3257 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3258
3259 static int
3260 msym_not_found_p (const struct minimal_symbol *msym)
3261 {
3262 return msym == &msym_not_found;
3263 }
3264
3265 /* Return per-objfile data needed by breakpoint.c.
3266 Allocate the data if necessary. */
3267
3268 static struct breakpoint_objfile_data *
3269 get_breakpoint_objfile_data (struct objfile *objfile)
3270 {
3271 struct breakpoint_objfile_data *bp_objfile_data;
3272
3273 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3274 if (bp_objfile_data == NULL)
3275 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3276 return bp_objfile_data;
3277 }
3278
3279 static void
3280 create_overlay_event_breakpoint (void)
3281 {
3282 const char *const func_name = "_ovly_debug_event";
3283
3284 for (objfile *objfile : current_program_space->objfiles ())
3285 {
3286 struct breakpoint *b;
3287 struct breakpoint_objfile_data *bp_objfile_data;
3288 CORE_ADDR addr;
3289 struct explicit_location explicit_loc;
3290
3291 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3292
3293 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3294 continue;
3295
3296 if (bp_objfile_data->overlay_msym.minsym == NULL)
3297 {
3298 struct bound_minimal_symbol m;
3299
3300 m = lookup_minimal_symbol_text (func_name, objfile);
3301 if (m.minsym == NULL)
3302 {
3303 /* Avoid future lookups in this objfile. */
3304 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3305 continue;
3306 }
3307 bp_objfile_data->overlay_msym = m;
3308 }
3309
3310 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3311 b = create_internal_breakpoint (objfile->arch (), addr,
3312 bp_overlay_event,
3313 &internal_breakpoint_ops);
3314 initialize_explicit_location (&explicit_loc);
3315 explicit_loc.function_name = ASTRDUP (func_name);
3316 b->location = new_explicit_location (&explicit_loc);
3317
3318 if (overlay_debugging == ovly_auto)
3319 {
3320 b->enable_state = bp_enabled;
3321 overlay_events_enabled = 1;
3322 }
3323 else
3324 {
3325 b->enable_state = bp_disabled;
3326 overlay_events_enabled = 0;
3327 }
3328 }
3329 }
3330
3331 /* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3332 true if a breakpoint was installed. */
3333
3334 static bool
3335 create_longjmp_master_breakpoint_probe (objfile *objfile)
3336 {
3337 struct gdbarch *gdbarch = objfile->arch ();
3338 struct breakpoint_objfile_data *bp_objfile_data
3339 = get_breakpoint_objfile_data (objfile);
3340
3341 if (!bp_objfile_data->longjmp_searched)
3342 {
3343 std::vector<probe *> ret
3344 = find_probes_in_objfile (objfile, "libc", "longjmp");
3345
3346 if (!ret.empty ())
3347 {
3348 /* We are only interested in checking one element. */
3349 probe *p = ret[0];
3350
3351 if (!p->can_evaluate_arguments ())
3352 {
3353 /* We cannot use the probe interface here,
3354 because it does not know how to evaluate
3355 arguments. */
3356 ret.clear ();
3357 }
3358 }
3359 bp_objfile_data->longjmp_probes = ret;
3360 bp_objfile_data->longjmp_searched = 1;
3361 }
3362
3363 if (bp_objfile_data->longjmp_probes.empty ())
3364 return false;
3365
3366 for (probe *p : bp_objfile_data->longjmp_probes)
3367 {
3368 struct breakpoint *b;
3369
3370 b = create_internal_breakpoint (gdbarch,
3371 p->get_relocated_address (objfile),
3372 bp_longjmp_master,
3373 &internal_breakpoint_ops);
3374 b->location = new_probe_location ("-probe-stap libc:longjmp");
3375 b->enable_state = bp_disabled;
3376 }
3377
3378 return true;
3379 }
3380
3381 /* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3382 Return true if at least one breakpoint was installed. */
3383
3384 static bool
3385 create_longjmp_master_breakpoint_names (objfile *objfile)
3386 {
3387 struct gdbarch *gdbarch = objfile->arch ();
3388 if (!gdbarch_get_longjmp_target_p (gdbarch))
3389 return false;
3390
3391 struct breakpoint_objfile_data *bp_objfile_data
3392 = get_breakpoint_objfile_data (objfile);
3393 unsigned int installed_bp = 0;
3394
3395 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3396 {
3397 struct breakpoint *b;
3398 const char *func_name;
3399 CORE_ADDR addr;
3400 struct explicit_location explicit_loc;
3401
3402 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3403 continue;
3404
3405 func_name = longjmp_names[i];
3406 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3407 {
3408 struct bound_minimal_symbol m;
3409
3410 m = lookup_minimal_symbol_text (func_name, objfile);
3411 if (m.minsym == NULL)
3412 {
3413 /* Prevent future lookups in this objfile. */
3414 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3415 continue;
3416 }
3417 bp_objfile_data->longjmp_msym[i] = m;
3418 }
3419
3420 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3421 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3422 &internal_breakpoint_ops);
3423 initialize_explicit_location (&explicit_loc);
3424 explicit_loc.function_name = ASTRDUP (func_name);
3425 b->location = new_explicit_location (&explicit_loc);
3426 b->enable_state = bp_disabled;
3427 installed_bp++;
3428 }
3429
3430 return installed_bp > 0;
3431 }
3432
3433 /* Create a master longjmp breakpoint. */
3434
3435 static void
3436 create_longjmp_master_breakpoint (void)
3437 {
3438 scoped_restore_current_program_space restore_pspace;
3439
3440 for (struct program_space *pspace : program_spaces)
3441 {
3442 set_current_program_space (pspace);
3443
3444 for (objfile *obj : current_program_space->objfiles ())
3445 {
3446 /* Skip separate debug object, it's handled in the loop below. */
3447 if (obj->separate_debug_objfile_backlink != nullptr)
3448 continue;
3449
3450 /* Try a probe kind breakpoint on main objfile. */
3451 if (create_longjmp_master_breakpoint_probe (obj))
3452 continue;
3453
3454 /* Try longjmp_names kind breakpoints on main and separate_debug
3455 objfiles. */
3456 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3457 if (create_longjmp_master_breakpoint_names (debug_objfile))
3458 break;
3459 }
3460 }
3461 }
3462
3463 /* Create a master std::terminate breakpoint. */
3464 static void
3465 create_std_terminate_master_breakpoint (void)
3466 {
3467 const char *const func_name = "std::terminate()";
3468
3469 scoped_restore_current_program_space restore_pspace;
3470
3471 for (struct program_space *pspace : program_spaces)
3472 {
3473 CORE_ADDR addr;
3474
3475 set_current_program_space (pspace);
3476
3477 for (objfile *objfile : current_program_space->objfiles ())
3478 {
3479 struct breakpoint *b;
3480 struct breakpoint_objfile_data *bp_objfile_data;
3481 struct explicit_location explicit_loc;
3482
3483 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3484
3485 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3486 continue;
3487
3488 if (bp_objfile_data->terminate_msym.minsym == NULL)
3489 {
3490 struct bound_minimal_symbol m;
3491
3492 m = lookup_minimal_symbol (func_name, NULL, objfile);
3493 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3494 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3495 {
3496 /* Prevent future lookups in this objfile. */
3497 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3498 continue;
3499 }
3500 bp_objfile_data->terminate_msym = m;
3501 }
3502
3503 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3504 b = create_internal_breakpoint (objfile->arch (), addr,
3505 bp_std_terminate_master,
3506 &internal_breakpoint_ops);
3507 initialize_explicit_location (&explicit_loc);
3508 explicit_loc.function_name = ASTRDUP (func_name);
3509 b->location = new_explicit_location (&explicit_loc);
3510 b->enable_state = bp_disabled;
3511 }
3512 }
3513 }
3514
3515 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3516 probe. Return true if a breakpoint was installed. */
3517
3518 static bool
3519 create_exception_master_breakpoint_probe (objfile *objfile)
3520 {
3521 struct breakpoint *b;
3522 struct gdbarch *gdbarch;
3523 struct breakpoint_objfile_data *bp_objfile_data;
3524
3525 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3526
3527 /* We prefer the SystemTap probe point if it exists. */
3528 if (!bp_objfile_data->exception_searched)
3529 {
3530 std::vector<probe *> ret
3531 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3532
3533 if (!ret.empty ())
3534 {
3535 /* We are only interested in checking one element. */
3536 probe *p = ret[0];
3537
3538 if (!p->can_evaluate_arguments ())
3539 {
3540 /* We cannot use the probe interface here, because it does
3541 not know how to evaluate arguments. */
3542 ret.clear ();
3543 }
3544 }
3545 bp_objfile_data->exception_probes = ret;
3546 bp_objfile_data->exception_searched = 1;
3547 }
3548
3549 if (bp_objfile_data->exception_probes.empty ())
3550 return false;
3551
3552 gdbarch = objfile->arch ();
3553
3554 for (probe *p : bp_objfile_data->exception_probes)
3555 {
3556 b = create_internal_breakpoint (gdbarch,
3557 p->get_relocated_address (objfile),
3558 bp_exception_master,
3559 &internal_breakpoint_ops);
3560 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3561 b->enable_state = bp_disabled;
3562 }
3563
3564 return true;
3565 }
3566
3567 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3568 _Unwind_DebugHook. Return true if a breakpoint was installed. */
3569
3570 static bool
3571 create_exception_master_breakpoint_hook (objfile *objfile)
3572 {
3573 const char *const func_name = "_Unwind_DebugHook";
3574 struct breakpoint *b;
3575 struct gdbarch *gdbarch;
3576 struct breakpoint_objfile_data *bp_objfile_data;
3577 CORE_ADDR addr;
3578 struct explicit_location explicit_loc;
3579
3580 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3581
3582 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3583 return false;
3584
3585 gdbarch = objfile->arch ();
3586
3587 if (bp_objfile_data->exception_msym.minsym == NULL)
3588 {
3589 struct bound_minimal_symbol debug_hook;
3590
3591 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3592 if (debug_hook.minsym == NULL)
3593 {
3594 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3595 return false;
3596 }
3597
3598 bp_objfile_data->exception_msym = debug_hook;
3599 }
3600
3601 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3602 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3603 current_top_target ());
3604 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3605 &internal_breakpoint_ops);
3606 initialize_explicit_location (&explicit_loc);
3607 explicit_loc.function_name = ASTRDUP (func_name);
3608 b->location = new_explicit_location (&explicit_loc);
3609 b->enable_state = bp_disabled;
3610
3611 return true;
3612 }
3613
3614 /* Install a master breakpoint on the unwinder's debug hook. */
3615
3616 static void
3617 create_exception_master_breakpoint (void)
3618 {
3619 for (objfile *obj : current_program_space->objfiles ())
3620 {
3621 /* Skip separate debug object. */
3622 if (obj->separate_debug_objfile_backlink)
3623 continue;
3624
3625 /* Try a probe kind breakpoint. */
3626 if (create_exception_master_breakpoint_probe (obj))
3627 continue;
3628
3629 /* Iterate over main and separate debug objects and try an
3630 _Unwind_DebugHook kind breakpoint. */
3631 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3632 if (create_exception_master_breakpoint_hook (debug_objfile))
3633 break;
3634 }
3635 }
3636
3637 /* Does B have a location spec? */
3638
3639 static int
3640 breakpoint_event_location_empty_p (const struct breakpoint *b)
3641 {
3642 return b->location != NULL && event_location_empty_p (b->location.get ());
3643 }
3644
3645 void
3646 update_breakpoints_after_exec (void)
3647 {
3648 struct breakpoint *b, *b_tmp;
3649 struct bp_location *bploc, **bplocp_tmp;
3650
3651 /* We're about to delete breakpoints from GDB's lists. If the
3652 INSERTED flag is true, GDB will try to lift the breakpoints by
3653 writing the breakpoints' "shadow contents" back into memory. The
3654 "shadow contents" are NOT valid after an exec, so GDB should not
3655 do that. Instead, the target is responsible from marking
3656 breakpoints out as soon as it detects an exec. We don't do that
3657 here instead, because there may be other attempts to delete
3658 breakpoints after detecting an exec and before reaching here. */
3659 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3660 if (bploc->pspace == current_program_space)
3661 gdb_assert (!bploc->inserted);
3662
3663 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3664 {
3665 if (b->pspace != current_program_space)
3666 continue;
3667
3668 /* Solib breakpoints must be explicitly reset after an exec(). */
3669 if (b->type == bp_shlib_event)
3670 {
3671 delete_breakpoint (b);
3672 continue;
3673 }
3674
3675 /* JIT breakpoints must be explicitly reset after an exec(). */
3676 if (b->type == bp_jit_event)
3677 {
3678 delete_breakpoint (b);
3679 continue;
3680 }
3681
3682 /* Thread event breakpoints must be set anew after an exec(),
3683 as must overlay event and longjmp master breakpoints. */
3684 if (b->type == bp_thread_event || b->type == bp_overlay_event
3685 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3686 || b->type == bp_exception_master)
3687 {
3688 delete_breakpoint (b);
3689 continue;
3690 }
3691
3692 /* Step-resume breakpoints are meaningless after an exec(). */
3693 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3694 {
3695 delete_breakpoint (b);
3696 continue;
3697 }
3698
3699 /* Just like single-step breakpoints. */
3700 if (b->type == bp_single_step)
3701 {
3702 delete_breakpoint (b);
3703 continue;
3704 }
3705
3706 /* Longjmp and longjmp-resume breakpoints are also meaningless
3707 after an exec. */
3708 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3709 || b->type == bp_longjmp_call_dummy
3710 || b->type == bp_exception || b->type == bp_exception_resume)
3711 {
3712 delete_breakpoint (b);
3713 continue;
3714 }
3715
3716 if (b->type == bp_catchpoint)
3717 {
3718 /* For now, none of the bp_catchpoint breakpoints need to
3719 do anything at this point. In the future, if some of
3720 the catchpoints need to something, we will need to add
3721 a new method, and call this method from here. */
3722 continue;
3723 }
3724
3725 /* bp_finish is a special case. The only way we ought to be able
3726 to see one of these when an exec() has happened, is if the user
3727 caught a vfork, and then said "finish". Ordinarily a finish just
3728 carries them to the call-site of the current callee, by setting
3729 a temporary bp there and resuming. But in this case, the finish
3730 will carry them entirely through the vfork & exec.
3731
3732 We don't want to allow a bp_finish to remain inserted now. But
3733 we can't safely delete it, 'cause finish_command has a handle to
3734 the bp on a bpstat, and will later want to delete it. There's a
3735 chance (and I've seen it happen) that if we delete the bp_finish
3736 here, that its storage will get reused by the time finish_command
3737 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3738 We really must allow finish_command to delete a bp_finish.
3739
3740 In the absence of a general solution for the "how do we know
3741 it's safe to delete something others may have handles to?"
3742 problem, what we'll do here is just uninsert the bp_finish, and
3743 let finish_command delete it.
3744
3745 (We know the bp_finish is "doomed" in the sense that it's
3746 momentary, and will be deleted as soon as finish_command sees
3747 the inferior stopped. So it doesn't matter that the bp's
3748 address is probably bogus in the new a.out, unlike e.g., the
3749 solib breakpoints.) */
3750
3751 if (b->type == bp_finish)
3752 {
3753 continue;
3754 }
3755
3756 /* Without a symbolic address, we have little hope of the
3757 pre-exec() address meaning the same thing in the post-exec()
3758 a.out. */
3759 if (breakpoint_event_location_empty_p (b))
3760 {
3761 delete_breakpoint (b);
3762 continue;
3763 }
3764 }
3765 }
3766
3767 int
3768 detach_breakpoints (ptid_t ptid)
3769 {
3770 struct bp_location *bl, **blp_tmp;
3771 int val = 0;
3772 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3773 struct inferior *inf = current_inferior ();
3774
3775 if (ptid.pid () == inferior_ptid.pid ())
3776 error (_("Cannot detach breakpoints of inferior_ptid"));
3777
3778 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3779 inferior_ptid = ptid;
3780 ALL_BP_LOCATIONS (bl, blp_tmp)
3781 {
3782 if (bl->pspace != inf->pspace)
3783 continue;
3784
3785 /* This function must physically remove breakpoints locations
3786 from the specified ptid, without modifying the breakpoint
3787 package's state. Locations of type bp_loc_other are only
3788 maintained at GDB side. So, there is no need to remove
3789 these bp_loc_other locations. Moreover, removing these
3790 would modify the breakpoint package's state. */
3791 if (bl->loc_type == bp_loc_other)
3792 continue;
3793
3794 if (bl->inserted)
3795 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3796 }
3797
3798 return val;
3799 }
3800
3801 /* Remove the breakpoint location BL from the current address space.
3802 Note that this is used to detach breakpoints from a child fork.
3803 When we get here, the child isn't in the inferior list, and neither
3804 do we have objects to represent its address space --- we should
3805 *not* look at bl->pspace->aspace here. */
3806
3807 static int
3808 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3809 {
3810 int val;
3811
3812 /* BL is never in moribund_locations by our callers. */
3813 gdb_assert (bl->owner != NULL);
3814
3815 /* The type of none suggests that owner is actually deleted.
3816 This should not ever happen. */
3817 gdb_assert (bl->owner->type != bp_none);
3818
3819 if (bl->loc_type == bp_loc_software_breakpoint
3820 || bl->loc_type == bp_loc_hardware_breakpoint)
3821 {
3822 /* "Normal" instruction breakpoint: either the standard
3823 trap-instruction bp (bp_breakpoint), or a
3824 bp_hardware_breakpoint. */
3825
3826 /* First check to see if we have to handle an overlay. */
3827 if (overlay_debugging == ovly_off
3828 || bl->section == NULL
3829 || !(section_is_overlay (bl->section)))
3830 {
3831 /* No overlay handling: just remove the breakpoint. */
3832
3833 /* If we're trying to uninsert a memory breakpoint that we
3834 know is set in a dynamic object that is marked
3835 shlib_disabled, then either the dynamic object was
3836 removed with "remove-symbol-file" or with
3837 "nosharedlibrary". In the former case, we don't know
3838 whether another dynamic object might have loaded over the
3839 breakpoint's address -- the user might well let us know
3840 about it next with add-symbol-file (the whole point of
3841 add-symbol-file is letting the user manually maintain a
3842 list of dynamically loaded objects). If we have the
3843 breakpoint's shadow memory, that is, this is a software
3844 breakpoint managed by GDB, check whether the breakpoint
3845 is still inserted in memory, to avoid overwriting wrong
3846 code with stale saved shadow contents. Note that HW
3847 breakpoints don't have shadow memory, as they're
3848 implemented using a mechanism that is not dependent on
3849 being able to modify the target's memory, and as such
3850 they should always be removed. */
3851 if (bl->shlib_disabled
3852 && bl->target_info.shadow_len != 0
3853 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3854 val = 0;
3855 else
3856 val = bl->owner->ops->remove_location (bl, reason);
3857 }
3858 else
3859 {
3860 /* This breakpoint is in an overlay section.
3861 Did we set a breakpoint at the LMA? */
3862 if (!overlay_events_enabled)
3863 {
3864 /* Yes -- overlay event support is not active, so we
3865 should have set a breakpoint at the LMA. Remove it.
3866 */
3867 /* Ignore any failures: if the LMA is in ROM, we will
3868 have already warned when we failed to insert it. */
3869 if (bl->loc_type == bp_loc_hardware_breakpoint)
3870 target_remove_hw_breakpoint (bl->gdbarch,
3871 &bl->overlay_target_info);
3872 else
3873 target_remove_breakpoint (bl->gdbarch,
3874 &bl->overlay_target_info,
3875 reason);
3876 }
3877 /* Did we set a breakpoint at the VMA?
3878 If so, we will have marked the breakpoint 'inserted'. */
3879 if (bl->inserted)
3880 {
3881 /* Yes -- remove it. Previously we did not bother to
3882 remove the breakpoint if the section had been
3883 unmapped, but let's not rely on that being safe. We
3884 don't know what the overlay manager might do. */
3885
3886 /* However, we should remove *software* breakpoints only
3887 if the section is still mapped, or else we overwrite
3888 wrong code with the saved shadow contents. */
3889 if (bl->loc_type == bp_loc_hardware_breakpoint
3890 || section_is_mapped (bl->section))
3891 val = bl->owner->ops->remove_location (bl, reason);
3892 else
3893 val = 0;
3894 }
3895 else
3896 {
3897 /* No -- not inserted, so no need to remove. No error. */
3898 val = 0;
3899 }
3900 }
3901
3902 /* In some cases, we might not be able to remove a breakpoint in
3903 a shared library that has already been removed, but we have
3904 not yet processed the shlib unload event. Similarly for an
3905 unloaded add-symbol-file object - the user might not yet have
3906 had the chance to remove-symbol-file it. shlib_disabled will
3907 be set if the library/object has already been removed, but
3908 the breakpoint hasn't been uninserted yet, e.g., after
3909 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3910 always-inserted mode. */
3911 if (val
3912 && (bl->loc_type == bp_loc_software_breakpoint
3913 && (bl->shlib_disabled
3914 || solib_name_from_address (bl->pspace, bl->address)
3915 || shared_objfile_contains_address_p (bl->pspace,
3916 bl->address))))
3917 val = 0;
3918
3919 if (val)
3920 return val;
3921 bl->inserted = (reason == DETACH_BREAKPOINT);
3922 }
3923 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3924 {
3925 gdb_assert (bl->owner->ops != NULL
3926 && bl->owner->ops->remove_location != NULL);
3927
3928 bl->inserted = (reason == DETACH_BREAKPOINT);
3929 bl->owner->ops->remove_location (bl, reason);
3930
3931 /* Failure to remove any of the hardware watchpoints comes here. */
3932 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3933 warning (_("Could not remove hardware watchpoint %d."),
3934 bl->owner->number);
3935 }
3936 else if (bl->owner->type == bp_catchpoint
3937 && breakpoint_enabled (bl->owner)
3938 && !bl->duplicate)
3939 {
3940 gdb_assert (bl->owner->ops != NULL
3941 && bl->owner->ops->remove_location != NULL);
3942
3943 val = bl->owner->ops->remove_location (bl, reason);
3944 if (val)
3945 return val;
3946
3947 bl->inserted = (reason == DETACH_BREAKPOINT);
3948 }
3949
3950 return 0;
3951 }
3952
3953 static int
3954 remove_breakpoint (struct bp_location *bl)
3955 {
3956 /* BL is never in moribund_locations by our callers. */
3957 gdb_assert (bl->owner != NULL);
3958
3959 /* The type of none suggests that owner is actually deleted.
3960 This should not ever happen. */
3961 gdb_assert (bl->owner->type != bp_none);
3962
3963 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3964
3965 switch_to_program_space_and_thread (bl->pspace);
3966
3967 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
3968 }
3969
3970 /* Clear the "inserted" flag in all breakpoints. */
3971
3972 void
3973 mark_breakpoints_out (void)
3974 {
3975 struct bp_location *bl, **blp_tmp;
3976
3977 ALL_BP_LOCATIONS (bl, blp_tmp)
3978 if (bl->pspace == current_program_space)
3979 bl->inserted = 0;
3980 }
3981
3982 /* Clear the "inserted" flag in all breakpoints and delete any
3983 breakpoints which should go away between runs of the program.
3984
3985 Plus other such housekeeping that has to be done for breakpoints
3986 between runs.
3987
3988 Note: this function gets called at the end of a run (by
3989 generic_mourn_inferior) and when a run begins (by
3990 init_wait_for_inferior). */
3991
3992
3993
3994 void
3995 breakpoint_init_inferior (enum inf_context context)
3996 {
3997 struct breakpoint *b, *b_tmp;
3998 struct program_space *pspace = current_program_space;
3999
4000 /* If breakpoint locations are shared across processes, then there's
4001 nothing to do. */
4002 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4003 return;
4004
4005 mark_breakpoints_out ();
4006
4007 ALL_BREAKPOINTS_SAFE (b, b_tmp)
4008 {
4009 if (b->loc && b->loc->pspace != pspace)
4010 continue;
4011
4012 switch (b->type)
4013 {
4014 case bp_call_dummy:
4015 case bp_longjmp_call_dummy:
4016
4017 /* If the call dummy breakpoint is at the entry point it will
4018 cause problems when the inferior is rerun, so we better get
4019 rid of it. */
4020
4021 case bp_watchpoint_scope:
4022
4023 /* Also get rid of scope breakpoints. */
4024
4025 case bp_shlib_event:
4026
4027 /* Also remove solib event breakpoints. Their addresses may
4028 have changed since the last time we ran the program.
4029 Actually we may now be debugging against different target;
4030 and so the solib backend that installed this breakpoint may
4031 not be used in by the target. E.g.,
4032
4033 (gdb) file prog-linux
4034 (gdb) run # native linux target
4035 ...
4036 (gdb) kill
4037 (gdb) file prog-win.exe
4038 (gdb) tar rem :9999 # remote Windows gdbserver.
4039 */
4040
4041 case bp_step_resume:
4042
4043 /* Also remove step-resume breakpoints. */
4044
4045 case bp_single_step:
4046
4047 /* Also remove single-step breakpoints. */
4048
4049 delete_breakpoint (b);
4050 break;
4051
4052 case bp_watchpoint:
4053 case bp_hardware_watchpoint:
4054 case bp_read_watchpoint:
4055 case bp_access_watchpoint:
4056 {
4057 struct watchpoint *w = (struct watchpoint *) b;
4058
4059 /* Likewise for watchpoints on local expressions. */
4060 if (w->exp_valid_block != NULL)
4061 delete_breakpoint (b);
4062 else
4063 {
4064 /* Get rid of existing locations, which are no longer
4065 valid. New ones will be created in
4066 update_watchpoint, when the inferior is restarted.
4067 The next update_global_location_list call will
4068 garbage collect them. */
4069 b->loc = NULL;
4070
4071 if (context == inf_starting)
4072 {
4073 /* Reset val field to force reread of starting value in
4074 insert_breakpoints. */
4075 w->val.reset (nullptr);
4076 w->val_valid = false;
4077 }
4078 }
4079 }
4080 break;
4081 default:
4082 break;
4083 }
4084 }
4085
4086 /* Get rid of the moribund locations. */
4087 for (bp_location *bl : moribund_locations)
4088 decref_bp_location (&bl);
4089 moribund_locations.clear ();
4090 }
4091
4092 /* These functions concern about actual breakpoints inserted in the
4093 target --- to e.g. check if we need to do decr_pc adjustment or if
4094 we need to hop over the bkpt --- so we check for address space
4095 match, not program space. */
4096
4097 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4098 exists at PC. It returns ordinary_breakpoint_here if it's an
4099 ordinary breakpoint, or permanent_breakpoint_here if it's a
4100 permanent breakpoint.
4101 - When continuing from a location with an ordinary breakpoint, we
4102 actually single step once before calling insert_breakpoints.
4103 - When continuing from a location with a permanent breakpoint, we
4104 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4105 the target, to advance the PC past the breakpoint. */
4106
4107 enum breakpoint_here
4108 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4109 {
4110 struct bp_location *bl, **blp_tmp;
4111 int any_breakpoint_here = 0;
4112
4113 ALL_BP_LOCATIONS (bl, blp_tmp)
4114 {
4115 if (bl->loc_type != bp_loc_software_breakpoint
4116 && bl->loc_type != bp_loc_hardware_breakpoint)
4117 continue;
4118
4119 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4120 if ((breakpoint_enabled (bl->owner)
4121 || bl->permanent)
4122 && breakpoint_location_address_match (bl, aspace, pc))
4123 {
4124 if (overlay_debugging
4125 && section_is_overlay (bl->section)
4126 && !section_is_mapped (bl->section))
4127 continue; /* unmapped overlay -- can't be a match */
4128 else if (bl->permanent)
4129 return permanent_breakpoint_here;
4130 else
4131 any_breakpoint_here = 1;
4132 }
4133 }
4134
4135 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4136 }
4137
4138 /* See breakpoint.h. */
4139
4140 int
4141 breakpoint_in_range_p (const address_space *aspace,
4142 CORE_ADDR addr, ULONGEST len)
4143 {
4144 struct bp_location *bl, **blp_tmp;
4145
4146 ALL_BP_LOCATIONS (bl, blp_tmp)
4147 {
4148 if (bl->loc_type != bp_loc_software_breakpoint
4149 && bl->loc_type != bp_loc_hardware_breakpoint)
4150 continue;
4151
4152 if ((breakpoint_enabled (bl->owner)
4153 || bl->permanent)
4154 && breakpoint_location_address_range_overlap (bl, aspace,
4155 addr, len))
4156 {
4157 if (overlay_debugging
4158 && section_is_overlay (bl->section)
4159 && !section_is_mapped (bl->section))
4160 {
4161 /* Unmapped overlay -- can't be a match. */
4162 continue;
4163 }
4164
4165 return 1;
4166 }
4167 }
4168
4169 return 0;
4170 }
4171
4172 /* Return true if there's a moribund breakpoint at PC. */
4173
4174 int
4175 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4176 {
4177 for (bp_location *loc : moribund_locations)
4178 if (breakpoint_location_address_match (loc, aspace, pc))
4179 return 1;
4180
4181 return 0;
4182 }
4183
4184 /* Returns non-zero iff BL is inserted at PC, in address space
4185 ASPACE. */
4186
4187 static int
4188 bp_location_inserted_here_p (struct bp_location *bl,
4189 const address_space *aspace, CORE_ADDR pc)
4190 {
4191 if (bl->inserted
4192 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4193 aspace, pc))
4194 {
4195 if (overlay_debugging
4196 && section_is_overlay (bl->section)
4197 && !section_is_mapped (bl->section))
4198 return 0; /* unmapped overlay -- can't be a match */
4199 else
4200 return 1;
4201 }
4202 return 0;
4203 }
4204
4205 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4206
4207 int
4208 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4209 {
4210 struct bp_location **blp, **blp_tmp = NULL;
4211
4212 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4213 {
4214 struct bp_location *bl = *blp;
4215
4216 if (bl->loc_type != bp_loc_software_breakpoint
4217 && bl->loc_type != bp_loc_hardware_breakpoint)
4218 continue;
4219
4220 if (bp_location_inserted_here_p (bl, aspace, pc))
4221 return 1;
4222 }
4223 return 0;
4224 }
4225
4226 /* This function returns non-zero iff there is a software breakpoint
4227 inserted at PC. */
4228
4229 int
4230 software_breakpoint_inserted_here_p (const address_space *aspace,
4231 CORE_ADDR pc)
4232 {
4233 struct bp_location **blp, **blp_tmp = NULL;
4234
4235 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4236 {
4237 struct bp_location *bl = *blp;
4238
4239 if (bl->loc_type != bp_loc_software_breakpoint)
4240 continue;
4241
4242 if (bp_location_inserted_here_p (bl, aspace, pc))
4243 return 1;
4244 }
4245
4246 return 0;
4247 }
4248
4249 /* See breakpoint.h. */
4250
4251 int
4252 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4253 CORE_ADDR pc)
4254 {
4255 struct bp_location **blp, **blp_tmp = NULL;
4256
4257 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4258 {
4259 struct bp_location *bl = *blp;
4260
4261 if (bl->loc_type != bp_loc_hardware_breakpoint)
4262 continue;
4263
4264 if (bp_location_inserted_here_p (bl, aspace, pc))
4265 return 1;
4266 }
4267
4268 return 0;
4269 }
4270
4271 int
4272 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4273 CORE_ADDR addr, ULONGEST len)
4274 {
4275 struct breakpoint *bpt;
4276
4277 ALL_BREAKPOINTS (bpt)
4278 {
4279 struct bp_location *loc;
4280
4281 if (bpt->type != bp_hardware_watchpoint
4282 && bpt->type != bp_access_watchpoint)
4283 continue;
4284
4285 if (!breakpoint_enabled (bpt))
4286 continue;
4287
4288 for (loc = bpt->loc; loc; loc = loc->next)
4289 if (loc->pspace->aspace == aspace && loc->inserted)
4290 {
4291 CORE_ADDR l, h;
4292
4293 /* Check for intersection. */
4294 l = std::max<CORE_ADDR> (loc->address, addr);
4295 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4296 if (l < h)
4297 return 1;
4298 }
4299 }
4300 return 0;
4301 }
4302
4303 /* See breakpoint.h. */
4304
4305 bool
4306 is_catchpoint (struct breakpoint *b)
4307 {
4308 return (b->type == bp_catchpoint);
4309 }
4310
4311 /* Clear a bpstat so that it says we are not at any breakpoint.
4312 Also free any storage that is part of a bpstat. */
4313
4314 void
4315 bpstat_clear (bpstat *bsp)
4316 {
4317 bpstat p;
4318 bpstat q;
4319
4320 if (bsp == 0)
4321 return;
4322 p = *bsp;
4323 while (p != NULL)
4324 {
4325 q = p->next;
4326 delete p;
4327 p = q;
4328 }
4329 *bsp = NULL;
4330 }
4331
4332 bpstats::bpstats (const bpstats &other)
4333 : next (NULL),
4334 bp_location_at (other.bp_location_at),
4335 breakpoint_at (other.breakpoint_at),
4336 commands (other.commands),
4337 print (other.print),
4338 stop (other.stop),
4339 print_it (other.print_it)
4340 {
4341 if (other.old_val != NULL)
4342 old_val = release_value (value_copy (other.old_val.get ()));
4343 }
4344
4345 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4346 is part of the bpstat is copied as well. */
4347
4348 bpstat
4349 bpstat_copy (bpstat bs)
4350 {
4351 bpstat p = NULL;
4352 bpstat tmp;
4353 bpstat retval = NULL;
4354
4355 if (bs == NULL)
4356 return bs;
4357
4358 for (; bs != NULL; bs = bs->next)
4359 {
4360 tmp = new bpstats (*bs);
4361
4362 if (p == NULL)
4363 /* This is the first thing in the chain. */
4364 retval = tmp;
4365 else
4366 p->next = tmp;
4367 p = tmp;
4368 }
4369 p->next = NULL;
4370 return retval;
4371 }
4372
4373 /* Find the bpstat associated with this breakpoint. */
4374
4375 bpstat
4376 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4377 {
4378 if (bsp == NULL)
4379 return NULL;
4380
4381 for (; bsp != NULL; bsp = bsp->next)
4382 {
4383 if (bsp->breakpoint_at == breakpoint)
4384 return bsp;
4385 }
4386 return NULL;
4387 }
4388
4389 /* See breakpoint.h. */
4390
4391 bool
4392 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4393 {
4394 for (; bsp != NULL; bsp = bsp->next)
4395 {
4396 if (bsp->breakpoint_at == NULL)
4397 {
4398 /* A moribund location can never explain a signal other than
4399 GDB_SIGNAL_TRAP. */
4400 if (sig == GDB_SIGNAL_TRAP)
4401 return true;
4402 }
4403 else
4404 {
4405 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4406 sig))
4407 return true;
4408 }
4409 }
4410
4411 return false;
4412 }
4413
4414 /* Put in *NUM the breakpoint number of the first breakpoint we are
4415 stopped at. *BSP upon return is a bpstat which points to the
4416 remaining breakpoints stopped at (but which is not guaranteed to be
4417 good for anything but further calls to bpstat_num).
4418
4419 Return 0 if passed a bpstat which does not indicate any breakpoints.
4420 Return -1 if stopped at a breakpoint that has been deleted since
4421 we set it.
4422 Return 1 otherwise. */
4423
4424 int
4425 bpstat_num (bpstat *bsp, int *num)
4426 {
4427 struct breakpoint *b;
4428
4429 if ((*bsp) == NULL)
4430 return 0; /* No more breakpoint values */
4431
4432 /* We assume we'll never have several bpstats that correspond to a
4433 single breakpoint -- otherwise, this function might return the
4434 same number more than once and this will look ugly. */
4435 b = (*bsp)->breakpoint_at;
4436 *bsp = (*bsp)->next;
4437 if (b == NULL)
4438 return -1; /* breakpoint that's been deleted since */
4439
4440 *num = b->number; /* We have its number */
4441 return 1;
4442 }
4443
4444 /* See breakpoint.h. */
4445
4446 void
4447 bpstat_clear_actions (void)
4448 {
4449 bpstat bs;
4450
4451 if (inferior_ptid == null_ptid)
4452 return;
4453
4454 thread_info *tp = inferior_thread ();
4455 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4456 {
4457 bs->commands = NULL;
4458 bs->old_val.reset (nullptr);
4459 }
4460 }
4461
4462 /* Called when a command is about to proceed the inferior. */
4463
4464 static void
4465 breakpoint_about_to_proceed (void)
4466 {
4467 if (inferior_ptid != null_ptid)
4468 {
4469 struct thread_info *tp = inferior_thread ();
4470
4471 /* Allow inferior function calls in breakpoint commands to not
4472 interrupt the command list. When the call finishes
4473 successfully, the inferior will be standing at the same
4474 breakpoint as if nothing happened. */
4475 if (tp->control.in_infcall)
4476 return;
4477 }
4478
4479 breakpoint_proceeded = 1;
4480 }
4481
4482 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4483 or its equivalent. */
4484
4485 static int
4486 command_line_is_silent (struct command_line *cmd)
4487 {
4488 return cmd && (strcmp ("silent", cmd->line) == 0);
4489 }
4490
4491 /* Execute all the commands associated with all the breakpoints at
4492 this location. Any of these commands could cause the process to
4493 proceed beyond this point, etc. We look out for such changes by
4494 checking the global "breakpoint_proceeded" after each command.
4495
4496 Returns true if a breakpoint command resumed the inferior. In that
4497 case, it is the caller's responsibility to recall it again with the
4498 bpstat of the current thread. */
4499
4500 static int
4501 bpstat_do_actions_1 (bpstat *bsp)
4502 {
4503 bpstat bs;
4504 int again = 0;
4505
4506 /* Avoid endless recursion if a `source' command is contained
4507 in bs->commands. */
4508 if (executing_breakpoint_commands)
4509 return 0;
4510
4511 scoped_restore save_executing
4512 = make_scoped_restore (&executing_breakpoint_commands, 1);
4513
4514 scoped_restore preventer = prevent_dont_repeat ();
4515
4516 /* This pointer will iterate over the list of bpstat's. */
4517 bs = *bsp;
4518
4519 breakpoint_proceeded = 0;
4520 for (; bs != NULL; bs = bs->next)
4521 {
4522 struct command_line *cmd = NULL;
4523
4524 /* Take ownership of the BSP's command tree, if it has one.
4525
4526 The command tree could legitimately contain commands like
4527 'step' and 'next', which call clear_proceed_status, which
4528 frees stop_bpstat's command tree. To make sure this doesn't
4529 free the tree we're executing out from under us, we need to
4530 take ownership of the tree ourselves. Since a given bpstat's
4531 commands are only executed once, we don't need to copy it; we
4532 can clear the pointer in the bpstat, and make sure we free
4533 the tree when we're done. */
4534 counted_command_line ccmd = bs->commands;
4535 bs->commands = NULL;
4536 if (ccmd != NULL)
4537 cmd = ccmd.get ();
4538 if (command_line_is_silent (cmd))
4539 {
4540 /* The action has been already done by bpstat_stop_status. */
4541 cmd = cmd->next;
4542 }
4543
4544 while (cmd != NULL)
4545 {
4546 execute_control_command (cmd);
4547
4548 if (breakpoint_proceeded)
4549 break;
4550 else
4551 cmd = cmd->next;
4552 }
4553
4554 if (breakpoint_proceeded)
4555 {
4556 if (current_ui->async)
4557 /* If we are in async mode, then the target might be still
4558 running, not stopped at any breakpoint, so nothing for
4559 us to do here -- just return to the event loop. */
4560 ;
4561 else
4562 /* In sync mode, when execute_control_command returns
4563 we're already standing on the next breakpoint.
4564 Breakpoint commands for that stop were not run, since
4565 execute_command does not run breakpoint commands --
4566 only command_line_handler does, but that one is not
4567 involved in execution of breakpoint commands. So, we
4568 can now execute breakpoint commands. It should be
4569 noted that making execute_command do bpstat actions is
4570 not an option -- in this case we'll have recursive
4571 invocation of bpstat for each breakpoint with a
4572 command, and can easily blow up GDB stack. Instead, we
4573 return true, which will trigger the caller to recall us
4574 with the new stop_bpstat. */
4575 again = 1;
4576 break;
4577 }
4578 }
4579 return again;
4580 }
4581
4582 /* Helper for bpstat_do_actions. Get the current thread, if there's
4583 one, is alive and has execution. Return NULL otherwise. */
4584
4585 static thread_info *
4586 get_bpstat_thread ()
4587 {
4588 if (inferior_ptid == null_ptid || !target_has_execution ())
4589 return NULL;
4590
4591 thread_info *tp = inferior_thread ();
4592 if (tp->state == THREAD_EXITED || tp->executing)
4593 return NULL;
4594 return tp;
4595 }
4596
4597 void
4598 bpstat_do_actions (void)
4599 {
4600 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4601 thread_info *tp;
4602
4603 /* Do any commands attached to breakpoint we are stopped at. */
4604 while ((tp = get_bpstat_thread ()) != NULL)
4605 {
4606 /* Since in sync mode, bpstat_do_actions may resume the
4607 inferior, and only return when it is stopped at the next
4608 breakpoint, we keep doing breakpoint actions until it returns
4609 false to indicate the inferior was not resumed. */
4610 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4611 break;
4612 }
4613
4614 cleanup_if_error.release ();
4615 }
4616
4617 /* Print out the (old or new) value associated with a watchpoint. */
4618
4619 static void
4620 watchpoint_value_print (struct value *val, struct ui_file *stream)
4621 {
4622 if (val == NULL)
4623 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4624 else
4625 {
4626 struct value_print_options opts;
4627 get_user_print_options (&opts);
4628 value_print (val, stream, &opts);
4629 }
4630 }
4631
4632 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4633 debugging multiple threads. */
4634
4635 void
4636 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4637 {
4638 if (uiout->is_mi_like_p ())
4639 return;
4640
4641 uiout->text ("\n");
4642
4643 if (show_thread_that_caused_stop ())
4644 {
4645 const char *name;
4646 struct thread_info *thr = inferior_thread ();
4647
4648 uiout->text ("Thread ");
4649 uiout->field_string ("thread-id", print_thread_id (thr));
4650
4651 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4652 if (name != NULL)
4653 {
4654 uiout->text (" \"");
4655 uiout->field_string ("name", name);
4656 uiout->text ("\"");
4657 }
4658
4659 uiout->text (" hit ");
4660 }
4661 }
4662
4663 /* Generic routine for printing messages indicating why we
4664 stopped. The behavior of this function depends on the value
4665 'print_it' in the bpstat structure. Under some circumstances we
4666 may decide not to print anything here and delegate the task to
4667 normal_stop(). */
4668
4669 static enum print_stop_action
4670 print_bp_stop_message (bpstat bs)
4671 {
4672 switch (bs->print_it)
4673 {
4674 case print_it_noop:
4675 /* Nothing should be printed for this bpstat entry. */
4676 return PRINT_UNKNOWN;
4677 break;
4678
4679 case print_it_done:
4680 /* We still want to print the frame, but we already printed the
4681 relevant messages. */
4682 return PRINT_SRC_AND_LOC;
4683 break;
4684
4685 case print_it_normal:
4686 {
4687 struct breakpoint *b = bs->breakpoint_at;
4688
4689 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4690 which has since been deleted. */
4691 if (b == NULL)
4692 return PRINT_UNKNOWN;
4693
4694 /* Normal case. Call the breakpoint's print_it method. */
4695 return b->ops->print_it (bs);
4696 }
4697 break;
4698
4699 default:
4700 internal_error (__FILE__, __LINE__,
4701 _("print_bp_stop_message: unrecognized enum value"));
4702 break;
4703 }
4704 }
4705
4706 /* A helper function that prints a shared library stopped event. */
4707
4708 static void
4709 print_solib_event (int is_catchpoint)
4710 {
4711 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4712 bool any_added = !current_program_space->added_solibs.empty ();
4713
4714 if (!is_catchpoint)
4715 {
4716 if (any_added || any_deleted)
4717 current_uiout->text (_("Stopped due to shared library event:\n"));
4718 else
4719 current_uiout->text (_("Stopped due to shared library event (no "
4720 "libraries added or removed)\n"));
4721 }
4722
4723 if (current_uiout->is_mi_like_p ())
4724 current_uiout->field_string ("reason",
4725 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4726
4727 if (any_deleted)
4728 {
4729 current_uiout->text (_(" Inferior unloaded "));
4730 ui_out_emit_list list_emitter (current_uiout, "removed");
4731 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4732 {
4733 const std::string &name = current_program_space->deleted_solibs[ix];
4734
4735 if (ix > 0)
4736 current_uiout->text (" ");
4737 current_uiout->field_string ("library", name);
4738 current_uiout->text ("\n");
4739 }
4740 }
4741
4742 if (any_added)
4743 {
4744 current_uiout->text (_(" Inferior loaded "));
4745 ui_out_emit_list list_emitter (current_uiout, "added");
4746 bool first = true;
4747 for (so_list *iter : current_program_space->added_solibs)
4748 {
4749 if (!first)
4750 current_uiout->text (" ");
4751 first = false;
4752 current_uiout->field_string ("library", iter->so_name);
4753 current_uiout->text ("\n");
4754 }
4755 }
4756 }
4757
4758 /* Print a message indicating what happened. This is called from
4759 normal_stop(). The input to this routine is the head of the bpstat
4760 list - a list of the eventpoints that caused this stop. KIND is
4761 the target_waitkind for the stopping event. This
4762 routine calls the generic print routine for printing a message
4763 about reasons for stopping. This will print (for example) the
4764 "Breakpoint n," part of the output. The return value of this
4765 routine is one of:
4766
4767 PRINT_UNKNOWN: Means we printed nothing.
4768 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4769 code to print the location. An example is
4770 "Breakpoint 1, " which should be followed by
4771 the location.
4772 PRINT_SRC_ONLY: Means we printed something, but there is no need
4773 to also print the location part of the message.
4774 An example is the catch/throw messages, which
4775 don't require a location appended to the end.
4776 PRINT_NOTHING: We have done some printing and we don't need any
4777 further info to be printed. */
4778
4779 enum print_stop_action
4780 bpstat_print (bpstat bs, int kind)
4781 {
4782 enum print_stop_action val;
4783
4784 /* Maybe another breakpoint in the chain caused us to stop.
4785 (Currently all watchpoints go on the bpstat whether hit or not.
4786 That probably could (should) be changed, provided care is taken
4787 with respect to bpstat_explains_signal). */
4788 for (; bs; bs = bs->next)
4789 {
4790 val = print_bp_stop_message (bs);
4791 if (val == PRINT_SRC_ONLY
4792 || val == PRINT_SRC_AND_LOC
4793 || val == PRINT_NOTHING)
4794 return val;
4795 }
4796
4797 /* If we had hit a shared library event breakpoint,
4798 print_bp_stop_message would print out this message. If we hit an
4799 OS-level shared library event, do the same thing. */
4800 if (kind == TARGET_WAITKIND_LOADED)
4801 {
4802 print_solib_event (0);
4803 return PRINT_NOTHING;
4804 }
4805
4806 /* We reached the end of the chain, or we got a null BS to start
4807 with and nothing was printed. */
4808 return PRINT_UNKNOWN;
4809 }
4810
4811 /* Evaluate the boolean expression EXP and return the result. */
4812
4813 static bool
4814 breakpoint_cond_eval (expression *exp)
4815 {
4816 struct value *mark = value_mark ();
4817 bool res = value_true (evaluate_expression (exp));
4818
4819 value_free_to_mark (mark);
4820 return res;
4821 }
4822
4823 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4824
4825 bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4826 : next (NULL),
4827 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
4828 breakpoint_at (bl->owner),
4829 commands (NULL),
4830 print (0),
4831 stop (0),
4832 print_it (print_it_normal)
4833 {
4834 **bs_link_pointer = this;
4835 *bs_link_pointer = &next;
4836 }
4837
4838 bpstats::bpstats ()
4839 : next (NULL),
4840 breakpoint_at (NULL),
4841 commands (NULL),
4842 print (0),
4843 stop (0),
4844 print_it (print_it_normal)
4845 {
4846 }
4847 \f
4848 /* The target has stopped with waitstatus WS. Check if any hardware
4849 watchpoints have triggered, according to the target. */
4850
4851 int
4852 watchpoints_triggered (struct target_waitstatus *ws)
4853 {
4854 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
4855 CORE_ADDR addr;
4856 struct breakpoint *b;
4857
4858 if (!stopped_by_watchpoint)
4859 {
4860 /* We were not stopped by a watchpoint. Mark all watchpoints
4861 as not triggered. */
4862 ALL_BREAKPOINTS (b)
4863 if (is_hardware_watchpoint (b))
4864 {
4865 struct watchpoint *w = (struct watchpoint *) b;
4866
4867 w->watchpoint_triggered = watch_triggered_no;
4868 }
4869
4870 return 0;
4871 }
4872
4873 if (!target_stopped_data_address (current_top_target (), &addr))
4874 {
4875 /* We were stopped by a watchpoint, but we don't know where.
4876 Mark all watchpoints as unknown. */
4877 ALL_BREAKPOINTS (b)
4878 if (is_hardware_watchpoint (b))
4879 {
4880 struct watchpoint *w = (struct watchpoint *) b;
4881
4882 w->watchpoint_triggered = watch_triggered_unknown;
4883 }
4884
4885 return 1;
4886 }
4887
4888 /* The target could report the data address. Mark watchpoints
4889 affected by this data address as triggered, and all others as not
4890 triggered. */
4891
4892 ALL_BREAKPOINTS (b)
4893 if (is_hardware_watchpoint (b))
4894 {
4895 struct watchpoint *w = (struct watchpoint *) b;
4896 struct bp_location *loc;
4897
4898 w->watchpoint_triggered = watch_triggered_no;
4899 for (loc = b->loc; loc; loc = loc->next)
4900 {
4901 if (is_masked_watchpoint (b))
4902 {
4903 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4904 CORE_ADDR start = loc->address & w->hw_wp_mask;
4905
4906 if (newaddr == start)
4907 {
4908 w->watchpoint_triggered = watch_triggered_yes;
4909 break;
4910 }
4911 }
4912 /* Exact match not required. Within range is sufficient. */
4913 else if (target_watchpoint_addr_within_range (current_top_target (),
4914 addr, loc->address,
4915 loc->length))
4916 {
4917 w->watchpoint_triggered = watch_triggered_yes;
4918 break;
4919 }
4920 }
4921 }
4922
4923 return 1;
4924 }
4925
4926 /* Possible return values for watchpoint_check. */
4927 enum wp_check_result
4928 {
4929 /* The watchpoint has been deleted. */
4930 WP_DELETED = 1,
4931
4932 /* The value has changed. */
4933 WP_VALUE_CHANGED = 2,
4934
4935 /* The value has not changed. */
4936 WP_VALUE_NOT_CHANGED = 3,
4937
4938 /* Ignore this watchpoint, no matter if the value changed or not. */
4939 WP_IGNORE = 4,
4940 };
4941
4942 #define BP_TEMPFLAG 1
4943 #define BP_HARDWAREFLAG 2
4944
4945 /* Evaluate watchpoint condition expression and check if its value
4946 changed. */
4947
4948 static wp_check_result
4949 watchpoint_check (bpstat bs)
4950 {
4951 struct watchpoint *b;
4952 struct frame_info *fr;
4953 int within_current_scope;
4954
4955 /* BS is built from an existing struct breakpoint. */
4956 gdb_assert (bs->breakpoint_at != NULL);
4957 b = (struct watchpoint *) bs->breakpoint_at;
4958
4959 /* If this is a local watchpoint, we only want to check if the
4960 watchpoint frame is in scope if the current thread is the thread
4961 that was used to create the watchpoint. */
4962 if (!watchpoint_in_thread_scope (b))
4963 return WP_IGNORE;
4964
4965 if (b->exp_valid_block == NULL)
4966 within_current_scope = 1;
4967 else
4968 {
4969 struct frame_info *frame = get_current_frame ();
4970 struct gdbarch *frame_arch = get_frame_arch (frame);
4971 CORE_ADDR frame_pc = get_frame_pc (frame);
4972
4973 /* stack_frame_destroyed_p() returns a non-zero value if we're
4974 still in the function but the stack frame has already been
4975 invalidated. Since we can't rely on the values of local
4976 variables after the stack has been destroyed, we are treating
4977 the watchpoint in that state as `not changed' without further
4978 checking. Don't mark watchpoints as changed if the current
4979 frame is in an epilogue - even if they are in some other
4980 frame, our view of the stack is likely to be wrong and
4981 frame_find_by_id could error out. */
4982 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
4983 return WP_IGNORE;
4984
4985 fr = frame_find_by_id (b->watchpoint_frame);
4986 within_current_scope = (fr != NULL);
4987
4988 /* If we've gotten confused in the unwinder, we might have
4989 returned a frame that can't describe this variable. */
4990 if (within_current_scope)
4991 {
4992 struct symbol *function;
4993
4994 function = get_frame_function (fr);
4995 if (function == NULL
4996 || !contained_in (b->exp_valid_block,
4997 SYMBOL_BLOCK_VALUE (function)))
4998 within_current_scope = 0;
4999 }
5000
5001 if (within_current_scope)
5002 /* If we end up stopping, the current frame will get selected
5003 in normal_stop. So this call to select_frame won't affect
5004 the user. */
5005 select_frame (fr);
5006 }
5007
5008 if (within_current_scope)
5009 {
5010 /* We use value_{,free_to_}mark because it could be a *long*
5011 time before we return to the command level and call
5012 free_all_values. We can't call free_all_values because we
5013 might be in the middle of evaluating a function call. */
5014
5015 int pc = 0;
5016 struct value *mark;
5017 struct value *new_val;
5018
5019 if (is_masked_watchpoint (b))
5020 /* Since we don't know the exact trigger address (from
5021 stopped_data_address), just tell the user we've triggered
5022 a mask watchpoint. */
5023 return WP_VALUE_CHANGED;
5024
5025 mark = value_mark ();
5026 fetch_subexp_value (b->exp.get (), &pc, b->exp->op.get (), &new_val,
5027 NULL, NULL, false);
5028
5029 if (b->val_bitsize != 0)
5030 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5031
5032 /* We use value_equal_contents instead of value_equal because
5033 the latter coerces an array to a pointer, thus comparing just
5034 the address of the array instead of its contents. This is
5035 not what we want. */
5036 if ((b->val != NULL) != (new_val != NULL)
5037 || (b->val != NULL && !value_equal_contents (b->val.get (),
5038 new_val)))
5039 {
5040 bs->old_val = b->val;
5041 b->val = release_value (new_val);
5042 b->val_valid = true;
5043 if (new_val != NULL)
5044 value_free_to_mark (mark);
5045 return WP_VALUE_CHANGED;
5046 }
5047 else
5048 {
5049 /* Nothing changed. */
5050 value_free_to_mark (mark);
5051 return WP_VALUE_NOT_CHANGED;
5052 }
5053 }
5054 else
5055 {
5056 /* This seems like the only logical thing to do because
5057 if we temporarily ignored the watchpoint, then when
5058 we reenter the block in which it is valid it contains
5059 garbage (in the case of a function, it may have two
5060 garbage values, one before and one after the prologue).
5061 So we can't even detect the first assignment to it and
5062 watch after that (since the garbage may or may not equal
5063 the first value assigned). */
5064 /* We print all the stop information in
5065 breakpoint_ops->print_it, but in this case, by the time we
5066 call breakpoint_ops->print_it this bp will be deleted
5067 already. So we have no choice but print the information
5068 here. */
5069
5070 SWITCH_THRU_ALL_UIS ()
5071 {
5072 struct ui_out *uiout = current_uiout;
5073
5074 if (uiout->is_mi_like_p ())
5075 uiout->field_string
5076 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5077 uiout->message ("\nWatchpoint %pF deleted because the program has "
5078 "left the block in\n"
5079 "which its expression is valid.\n",
5080 signed_field ("wpnum", b->number));
5081 }
5082
5083 /* Make sure the watchpoint's commands aren't executed. */
5084 b->commands = NULL;
5085 watchpoint_del_at_next_stop (b);
5086
5087 return WP_DELETED;
5088 }
5089 }
5090
5091 /* Return true if it looks like target has stopped due to hitting
5092 breakpoint location BL. This function does not check if we should
5093 stop, only if BL explains the stop. */
5094
5095 static int
5096 bpstat_check_location (const struct bp_location *bl,
5097 const address_space *aspace, CORE_ADDR bp_addr,
5098 const struct target_waitstatus *ws)
5099 {
5100 struct breakpoint *b = bl->owner;
5101
5102 /* BL is from an existing breakpoint. */
5103 gdb_assert (b != NULL);
5104
5105 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5106 }
5107
5108 /* Determine if the watched values have actually changed, and we
5109 should stop. If not, set BS->stop to 0. */
5110
5111 static void
5112 bpstat_check_watchpoint (bpstat bs)
5113 {
5114 const struct bp_location *bl;
5115 struct watchpoint *b;
5116
5117 /* BS is built for existing struct breakpoint. */
5118 bl = bs->bp_location_at.get ();
5119 gdb_assert (bl != NULL);
5120 b = (struct watchpoint *) bs->breakpoint_at;
5121 gdb_assert (b != NULL);
5122
5123 {
5124 int must_check_value = 0;
5125
5126 if (b->type == bp_watchpoint)
5127 /* For a software watchpoint, we must always check the
5128 watched value. */
5129 must_check_value = 1;
5130 else if (b->watchpoint_triggered == watch_triggered_yes)
5131 /* We have a hardware watchpoint (read, write, or access)
5132 and the target earlier reported an address watched by
5133 this watchpoint. */
5134 must_check_value = 1;
5135 else if (b->watchpoint_triggered == watch_triggered_unknown
5136 && b->type == bp_hardware_watchpoint)
5137 /* We were stopped by a hardware watchpoint, but the target could
5138 not report the data address. We must check the watchpoint's
5139 value. Access and read watchpoints are out of luck; without
5140 a data address, we can't figure it out. */
5141 must_check_value = 1;
5142
5143 if (must_check_value)
5144 {
5145 wp_check_result e;
5146
5147 try
5148 {
5149 e = watchpoint_check (bs);
5150 }
5151 catch (const gdb_exception &ex)
5152 {
5153 exception_fprintf (gdb_stderr, ex,
5154 "Error evaluating expression "
5155 "for watchpoint %d\n",
5156 b->number);
5157
5158 SWITCH_THRU_ALL_UIS ()
5159 {
5160 printf_filtered (_("Watchpoint %d deleted.\n"),
5161 b->number);
5162 }
5163 watchpoint_del_at_next_stop (b);
5164 e = WP_DELETED;
5165 }
5166
5167 switch (e)
5168 {
5169 case WP_DELETED:
5170 /* We've already printed what needs to be printed. */
5171 bs->print_it = print_it_done;
5172 /* Stop. */
5173 break;
5174 case WP_IGNORE:
5175 bs->print_it = print_it_noop;
5176 bs->stop = 0;
5177 break;
5178 case WP_VALUE_CHANGED:
5179 if (b->type == bp_read_watchpoint)
5180 {
5181 /* There are two cases to consider here:
5182
5183 1. We're watching the triggered memory for reads.
5184 In that case, trust the target, and always report
5185 the watchpoint hit to the user. Even though
5186 reads don't cause value changes, the value may
5187 have changed since the last time it was read, and
5188 since we're not trapping writes, we will not see
5189 those, and as such we should ignore our notion of
5190 old value.
5191
5192 2. We're watching the triggered memory for both
5193 reads and writes. There are two ways this may
5194 happen:
5195
5196 2.1. This is a target that can't break on data
5197 reads only, but can break on accesses (reads or
5198 writes), such as e.g., x86. We detect this case
5199 at the time we try to insert read watchpoints.
5200
5201 2.2. Otherwise, the target supports read
5202 watchpoints, but, the user set an access or write
5203 watchpoint watching the same memory as this read
5204 watchpoint.
5205
5206 If we're watching memory writes as well as reads,
5207 ignore watchpoint hits when we find that the
5208 value hasn't changed, as reads don't cause
5209 changes. This still gives false positives when
5210 the program writes the same value to memory as
5211 what there was already in memory (we will confuse
5212 it for a read), but it's much better than
5213 nothing. */
5214
5215 int other_write_watchpoint = 0;
5216
5217 if (bl->watchpoint_type == hw_read)
5218 {
5219 struct breakpoint *other_b;
5220
5221 ALL_BREAKPOINTS (other_b)
5222 if (other_b->type == bp_hardware_watchpoint
5223 || other_b->type == bp_access_watchpoint)
5224 {
5225 struct watchpoint *other_w =
5226 (struct watchpoint *) other_b;
5227
5228 if (other_w->watchpoint_triggered
5229 == watch_triggered_yes)
5230 {
5231 other_write_watchpoint = 1;
5232 break;
5233 }
5234 }
5235 }
5236
5237 if (other_write_watchpoint
5238 || bl->watchpoint_type == hw_access)
5239 {
5240 /* We're watching the same memory for writes,
5241 and the value changed since the last time we
5242 updated it, so this trap must be for a write.
5243 Ignore it. */
5244 bs->print_it = print_it_noop;
5245 bs->stop = 0;
5246 }
5247 }
5248 break;
5249 case WP_VALUE_NOT_CHANGED:
5250 if (b->type == bp_hardware_watchpoint
5251 || b->type == bp_watchpoint)
5252 {
5253 /* Don't stop: write watchpoints shouldn't fire if
5254 the value hasn't changed. */
5255 bs->print_it = print_it_noop;
5256 bs->stop = 0;
5257 }
5258 /* Stop. */
5259 break;
5260 default:
5261 /* Can't happen. */
5262 break;
5263 }
5264 }
5265 else /* must_check_value == 0 */
5266 {
5267 /* This is a case where some watchpoint(s) triggered, but
5268 not at the address of this watchpoint, or else no
5269 watchpoint triggered after all. So don't print
5270 anything for this watchpoint. */
5271 bs->print_it = print_it_noop;
5272 bs->stop = 0;
5273 }
5274 }
5275 }
5276
5277 /* For breakpoints that are currently marked as telling gdb to stop,
5278 check conditions (condition proper, frame, thread and ignore count)
5279 of breakpoint referred to by BS. If we should not stop for this
5280 breakpoint, set BS->stop to 0. */
5281
5282 static void
5283 bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
5284 {
5285 const struct bp_location *bl;
5286 struct breakpoint *b;
5287 /* Assume stop. */
5288 bool condition_result = true;
5289 struct expression *cond;
5290
5291 gdb_assert (bs->stop);
5292
5293 /* BS is built for existing struct breakpoint. */
5294 bl = bs->bp_location_at.get ();
5295 gdb_assert (bl != NULL);
5296 b = bs->breakpoint_at;
5297 gdb_assert (b != NULL);
5298
5299 /* Even if the target evaluated the condition on its end and notified GDB, we
5300 need to do so again since GDB does not know if we stopped due to a
5301 breakpoint or a single step breakpoint. */
5302
5303 if (frame_id_p (b->frame_id)
5304 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5305 {
5306 bs->stop = 0;
5307 return;
5308 }
5309
5310 /* If this is a thread/task-specific breakpoint, don't waste cpu
5311 evaluating the condition if this isn't the specified
5312 thread/task. */
5313 if ((b->thread != -1 && b->thread != thread->global_num)
5314 || (b->task != 0 && b->task != ada_get_task_number (thread)))
5315 {
5316 bs->stop = 0;
5317 return;
5318 }
5319
5320 /* Evaluate extension language breakpoints that have a "stop" method
5321 implemented. */
5322 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5323
5324 if (is_watchpoint (b))
5325 {
5326 struct watchpoint *w = (struct watchpoint *) b;
5327
5328 cond = w->cond_exp.get ();
5329 }
5330 else
5331 cond = bl->cond.get ();
5332
5333 if (cond && b->disposition != disp_del_at_next_stop)
5334 {
5335 int within_current_scope = 1;
5336 struct watchpoint * w;
5337
5338 /* We use value_mark and value_free_to_mark because it could
5339 be a long time before we return to the command level and
5340 call free_all_values. We can't call free_all_values
5341 because we might be in the middle of evaluating a
5342 function call. */
5343 struct value *mark = value_mark ();
5344
5345 if (is_watchpoint (b))
5346 w = (struct watchpoint *) b;
5347 else
5348 w = NULL;
5349
5350 /* Need to select the frame, with all that implies so that
5351 the conditions will have the right context. Because we
5352 use the frame, we will not see an inlined function's
5353 variables when we arrive at a breakpoint at the start
5354 of the inlined function; the current frame will be the
5355 call site. */
5356 if (w == NULL || w->cond_exp_valid_block == NULL)
5357 select_frame (get_current_frame ());
5358 else
5359 {
5360 struct frame_info *frame;
5361
5362 /* For local watchpoint expressions, which particular
5363 instance of a local is being watched matters, so we
5364 keep track of the frame to evaluate the expression
5365 in. To evaluate the condition however, it doesn't
5366 really matter which instantiation of the function
5367 where the condition makes sense triggers the
5368 watchpoint. This allows an expression like "watch
5369 global if q > 10" set in `func', catch writes to
5370 global on all threads that call `func', or catch
5371 writes on all recursive calls of `func' by a single
5372 thread. We simply always evaluate the condition in
5373 the innermost frame that's executing where it makes
5374 sense to evaluate the condition. It seems
5375 intuitive. */
5376 frame = block_innermost_frame (w->cond_exp_valid_block);
5377 if (frame != NULL)
5378 select_frame (frame);
5379 else
5380 within_current_scope = 0;
5381 }
5382 if (within_current_scope)
5383 {
5384 try
5385 {
5386 condition_result = breakpoint_cond_eval (cond);
5387 }
5388 catch (const gdb_exception &ex)
5389 {
5390 exception_fprintf (gdb_stderr, ex,
5391 "Error in testing breakpoint condition:\n");
5392 }
5393 }
5394 else
5395 {
5396 warning (_("Watchpoint condition cannot be tested "
5397 "in the current scope"));
5398 /* If we failed to set the right context for this
5399 watchpoint, unconditionally report it. */
5400 }
5401 /* FIXME-someday, should give breakpoint #. */
5402 value_free_to_mark (mark);
5403 }
5404
5405 if (cond && !condition_result)
5406 {
5407 bs->stop = 0;
5408 }
5409 else if (b->ignore_count > 0)
5410 {
5411 b->ignore_count--;
5412 bs->stop = 0;
5413 /* Increase the hit count even though we don't stop. */
5414 ++(b->hit_count);
5415 gdb::observers::breakpoint_modified.notify (b);
5416 }
5417 }
5418
5419 /* Returns true if we need to track moribund locations of LOC's type
5420 on the current target. */
5421
5422 static int
5423 need_moribund_for_location_type (struct bp_location *loc)
5424 {
5425 return ((loc->loc_type == bp_loc_software_breakpoint
5426 && !target_supports_stopped_by_sw_breakpoint ())
5427 || (loc->loc_type == bp_loc_hardware_breakpoint
5428 && !target_supports_stopped_by_hw_breakpoint ()));
5429 }
5430
5431 /* See breakpoint.h. */
5432
5433 bpstat
5434 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5435 const struct target_waitstatus *ws)
5436 {
5437 struct breakpoint *b;
5438 bpstat bs_head = NULL, *bs_link = &bs_head;
5439
5440 ALL_BREAKPOINTS (b)
5441 {
5442 if (!breakpoint_enabled (b))
5443 continue;
5444
5445 for (bp_location *bl = b->loc; bl != NULL; bl = bl->next)
5446 {
5447 /* For hardware watchpoints, we look only at the first
5448 location. The watchpoint_check function will work on the
5449 entire expression, not the individual locations. For
5450 read watchpoints, the watchpoints_triggered function has
5451 checked all locations already. */
5452 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5453 break;
5454
5455 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
5456 continue;
5457
5458 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5459 continue;
5460
5461 /* Come here if it's a watchpoint, or if the break address
5462 matches. */
5463
5464 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5465 explain stop. */
5466
5467 /* Assume we stop. Should we find a watchpoint that is not
5468 actually triggered, or if the condition of the breakpoint
5469 evaluates as false, we'll reset 'stop' to 0. */
5470 bs->stop = 1;
5471 bs->print = 1;
5472
5473 /* If this is a scope breakpoint, mark the associated
5474 watchpoint as triggered so that we will handle the
5475 out-of-scope event. We'll get to the watchpoint next
5476 iteration. */
5477 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5478 {
5479 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5480
5481 w->watchpoint_triggered = watch_triggered_yes;
5482 }
5483 }
5484 }
5485
5486 /* Check if a moribund breakpoint explains the stop. */
5487 if (!target_supports_stopped_by_sw_breakpoint ()
5488 || !target_supports_stopped_by_hw_breakpoint ())
5489 {
5490 for (bp_location *loc : moribund_locations)
5491 {
5492 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5493 && need_moribund_for_location_type (loc))
5494 {
5495 bpstat bs = new bpstats (loc, &bs_link);
5496 /* For hits of moribund locations, we should just proceed. */
5497 bs->stop = 0;
5498 bs->print = 0;
5499 bs->print_it = print_it_noop;
5500 }
5501 }
5502 }
5503
5504 return bs_head;
5505 }
5506
5507 /* See breakpoint.h. */
5508
5509 bpstat
5510 bpstat_stop_status (const address_space *aspace,
5511 CORE_ADDR bp_addr, thread_info *thread,
5512 const struct target_waitstatus *ws,
5513 bpstat stop_chain)
5514 {
5515 struct breakpoint *b = NULL;
5516 /* First item of allocated bpstat's. */
5517 bpstat bs_head = stop_chain;
5518 bpstat bs;
5519 int need_remove_insert;
5520 int removed_any;
5521
5522 /* First, build the bpstat chain with locations that explain a
5523 target stop, while being careful to not set the target running,
5524 as that may invalidate locations (in particular watchpoint
5525 locations are recreated). Resuming will happen here with
5526 breakpoint conditions or watchpoint expressions that include
5527 inferior function calls. */
5528 if (bs_head == NULL)
5529 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5530
5531 /* A bit of special processing for shlib breakpoints. We need to
5532 process solib loading here, so that the lists of loaded and
5533 unloaded libraries are correct before we handle "catch load" and
5534 "catch unload". */
5535 for (bs = bs_head; bs != NULL; bs = bs->next)
5536 {
5537 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5538 {
5539 handle_solib_event ();
5540 break;
5541 }
5542 }
5543
5544 /* Now go through the locations that caused the target to stop, and
5545 check whether we're interested in reporting this stop to higher
5546 layers, or whether we should resume the target transparently. */
5547
5548 removed_any = 0;
5549
5550 for (bs = bs_head; bs != NULL; bs = bs->next)
5551 {
5552 if (!bs->stop)
5553 continue;
5554
5555 b = bs->breakpoint_at;
5556 b->ops->check_status (bs);
5557 if (bs->stop)
5558 {
5559 bpstat_check_breakpoint_conditions (bs, thread);
5560
5561 if (bs->stop)
5562 {
5563 ++(b->hit_count);
5564 gdb::observers::breakpoint_modified.notify (b);
5565
5566 /* We will stop here. */
5567 if (b->disposition == disp_disable)
5568 {
5569 --(b->enable_count);
5570 if (b->enable_count <= 0)
5571 b->enable_state = bp_disabled;
5572 removed_any = 1;
5573 }
5574 if (b->silent)
5575 bs->print = 0;
5576 bs->commands = b->commands;
5577 if (command_line_is_silent (bs->commands
5578 ? bs->commands.get () : NULL))
5579 bs->print = 0;
5580
5581 b->ops->after_condition_true (bs);
5582 }
5583
5584 }
5585
5586 /* Print nothing for this entry if we don't stop or don't
5587 print. */
5588 if (!bs->stop || !bs->print)
5589 bs->print_it = print_it_noop;
5590 }
5591
5592 /* If we aren't stopping, the value of some hardware watchpoint may
5593 not have changed, but the intermediate memory locations we are
5594 watching may have. Don't bother if we're stopping; this will get
5595 done later. */
5596 need_remove_insert = 0;
5597 if (! bpstat_causes_stop (bs_head))
5598 for (bs = bs_head; bs != NULL; bs = bs->next)
5599 if (!bs->stop
5600 && bs->breakpoint_at
5601 && is_hardware_watchpoint (bs->breakpoint_at))
5602 {
5603 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5604
5605 update_watchpoint (w, 0 /* don't reparse. */);
5606 need_remove_insert = 1;
5607 }
5608
5609 if (need_remove_insert)
5610 update_global_location_list (UGLL_MAY_INSERT);
5611 else if (removed_any)
5612 update_global_location_list (UGLL_DONT_INSERT);
5613
5614 return bs_head;
5615 }
5616
5617 static void
5618 handle_jit_event (CORE_ADDR address)
5619 {
5620 struct gdbarch *gdbarch;
5621
5622 infrun_debug_printf ("handling bp_jit_event");
5623
5624 /* Switch terminal for any messages produced by
5625 breakpoint_re_set. */
5626 target_terminal::ours_for_output ();
5627
5628 gdbarch = get_frame_arch (get_current_frame ());
5629 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5630 thus it is expected that its objectfile can be found through
5631 minimal symbol lookup. If it doesn't work (and assert fails), it
5632 most likely means that `jit_breakpoint_re_set` was changes and this
5633 function needs to be updated too. */
5634 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5635 gdb_assert (jit_bp_sym.objfile != nullptr);
5636 jit_event_handler (gdbarch, jit_bp_sym.objfile);
5637
5638 target_terminal::inferior ();
5639 }
5640
5641 /* Prepare WHAT final decision for infrun. */
5642
5643 /* Decide what infrun needs to do with this bpstat. */
5644
5645 struct bpstat_what
5646 bpstat_what (bpstat bs_head)
5647 {
5648 struct bpstat_what retval;
5649 bpstat bs;
5650
5651 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5652 retval.call_dummy = STOP_NONE;
5653 retval.is_longjmp = false;
5654
5655 for (bs = bs_head; bs != NULL; bs = bs->next)
5656 {
5657 /* Extract this BS's action. After processing each BS, we check
5658 if its action overrides all we've seem so far. */
5659 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5660 enum bptype bptype;
5661
5662 if (bs->breakpoint_at == NULL)
5663 {
5664 /* I suspect this can happen if it was a momentary
5665 breakpoint which has since been deleted. */
5666 bptype = bp_none;
5667 }
5668 else
5669 bptype = bs->breakpoint_at->type;
5670
5671 switch (bptype)
5672 {
5673 case bp_none:
5674 break;
5675 case bp_breakpoint:
5676 case bp_hardware_breakpoint:
5677 case bp_single_step:
5678 case bp_until:
5679 case bp_finish:
5680 case bp_shlib_event:
5681 if (bs->stop)
5682 {
5683 if (bs->print)
5684 this_action = BPSTAT_WHAT_STOP_NOISY;
5685 else
5686 this_action = BPSTAT_WHAT_STOP_SILENT;
5687 }
5688 else
5689 this_action = BPSTAT_WHAT_SINGLE;
5690 break;
5691 case bp_watchpoint:
5692 case bp_hardware_watchpoint:
5693 case bp_read_watchpoint:
5694 case bp_access_watchpoint:
5695 if (bs->stop)
5696 {
5697 if (bs->print)
5698 this_action = BPSTAT_WHAT_STOP_NOISY;
5699 else
5700 this_action = BPSTAT_WHAT_STOP_SILENT;
5701 }
5702 else
5703 {
5704 /* There was a watchpoint, but we're not stopping.
5705 This requires no further action. */
5706 }
5707 break;
5708 case bp_longjmp:
5709 case bp_longjmp_call_dummy:
5710 case bp_exception:
5711 if (bs->stop)
5712 {
5713 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5714 retval.is_longjmp = bptype != bp_exception;
5715 }
5716 else
5717 this_action = BPSTAT_WHAT_SINGLE;
5718 break;
5719 case bp_longjmp_resume:
5720 case bp_exception_resume:
5721 if (bs->stop)
5722 {
5723 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5724 retval.is_longjmp = bptype == bp_longjmp_resume;
5725 }
5726 else
5727 this_action = BPSTAT_WHAT_SINGLE;
5728 break;
5729 case bp_step_resume:
5730 if (bs->stop)
5731 this_action = BPSTAT_WHAT_STEP_RESUME;
5732 else
5733 {
5734 /* It is for the wrong frame. */
5735 this_action = BPSTAT_WHAT_SINGLE;
5736 }
5737 break;
5738 case bp_hp_step_resume:
5739 if (bs->stop)
5740 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5741 else
5742 {
5743 /* It is for the wrong frame. */
5744 this_action = BPSTAT_WHAT_SINGLE;
5745 }
5746 break;
5747 case bp_watchpoint_scope:
5748 case bp_thread_event:
5749 case bp_overlay_event:
5750 case bp_longjmp_master:
5751 case bp_std_terminate_master:
5752 case bp_exception_master:
5753 this_action = BPSTAT_WHAT_SINGLE;
5754 break;
5755 case bp_catchpoint:
5756 if (bs->stop)
5757 {
5758 if (bs->print)
5759 this_action = BPSTAT_WHAT_STOP_NOISY;
5760 else
5761 this_action = BPSTAT_WHAT_STOP_SILENT;
5762 }
5763 else
5764 {
5765 /* Some catchpoints are implemented with breakpoints.
5766 For those, we need to step over the breakpoint. */
5767 if (bs->bp_location_at->loc_type != bp_loc_other)
5768 this_action = BPSTAT_WHAT_SINGLE;
5769 }
5770 break;
5771 case bp_jit_event:
5772 this_action = BPSTAT_WHAT_SINGLE;
5773 break;
5774 case bp_call_dummy:
5775 /* Make sure the action is stop (silent or noisy),
5776 so infrun.c pops the dummy frame. */
5777 retval.call_dummy = STOP_STACK_DUMMY;
5778 this_action = BPSTAT_WHAT_STOP_SILENT;
5779 break;
5780 case bp_std_terminate:
5781 /* Make sure the action is stop (silent or noisy),
5782 so infrun.c pops the dummy frame. */
5783 retval.call_dummy = STOP_STD_TERMINATE;
5784 this_action = BPSTAT_WHAT_STOP_SILENT;
5785 break;
5786 case bp_tracepoint:
5787 case bp_fast_tracepoint:
5788 case bp_static_tracepoint:
5789 /* Tracepoint hits should not be reported back to GDB, and
5790 if one got through somehow, it should have been filtered
5791 out already. */
5792 internal_error (__FILE__, __LINE__,
5793 _("bpstat_what: tracepoint encountered"));
5794 break;
5795 case bp_gnu_ifunc_resolver:
5796 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5797 this_action = BPSTAT_WHAT_SINGLE;
5798 break;
5799 case bp_gnu_ifunc_resolver_return:
5800 /* The breakpoint will be removed, execution will restart from the
5801 PC of the former breakpoint. */
5802 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5803 break;
5804
5805 case bp_dprintf:
5806 if (bs->stop)
5807 this_action = BPSTAT_WHAT_STOP_SILENT;
5808 else
5809 this_action = BPSTAT_WHAT_SINGLE;
5810 break;
5811
5812 default:
5813 internal_error (__FILE__, __LINE__,
5814 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5815 }
5816
5817 retval.main_action = std::max (retval.main_action, this_action);
5818 }
5819
5820 return retval;
5821 }
5822
5823 void
5824 bpstat_run_callbacks (bpstat bs_head)
5825 {
5826 bpstat bs;
5827
5828 for (bs = bs_head; bs != NULL; bs = bs->next)
5829 {
5830 struct breakpoint *b = bs->breakpoint_at;
5831
5832 if (b == NULL)
5833 continue;
5834 switch (b->type)
5835 {
5836 case bp_jit_event:
5837 handle_jit_event (bs->bp_location_at->address);
5838 break;
5839 case bp_gnu_ifunc_resolver:
5840 gnu_ifunc_resolver_stop (b);
5841 break;
5842 case bp_gnu_ifunc_resolver_return:
5843 gnu_ifunc_resolver_return_stop (b);
5844 break;
5845 }
5846 }
5847 }
5848
5849 /* See breakpoint.h. */
5850
5851 bool
5852 bpstat_should_step ()
5853 {
5854 struct breakpoint *b;
5855
5856 ALL_BREAKPOINTS (b)
5857 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5858 return true;
5859 return false;
5860 }
5861
5862 /* See breakpoint.h. */
5863
5864 bool
5865 bpstat_causes_stop (bpstat bs)
5866 {
5867 for (; bs != NULL; bs = bs->next)
5868 if (bs->stop)
5869 return true;
5870
5871 return false;
5872 }
5873
5874 \f
5875
5876 /* Compute a string of spaces suitable to indent the next line
5877 so it starts at the position corresponding to the table column
5878 named COL_NAME in the currently active table of UIOUT. */
5879
5880 static char *
5881 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5882 {
5883 static char wrap_indent[80];
5884 int i, total_width, width, align;
5885 const char *text;
5886
5887 total_width = 0;
5888 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5889 {
5890 if (strcmp (text, col_name) == 0)
5891 {
5892 gdb_assert (total_width < sizeof wrap_indent);
5893 memset (wrap_indent, ' ', total_width);
5894 wrap_indent[total_width] = 0;
5895
5896 return wrap_indent;
5897 }
5898
5899 total_width += width + 1;
5900 }
5901
5902 return NULL;
5903 }
5904
5905 /* Determine if the locations of this breakpoint will have their conditions
5906 evaluated by the target, host or a mix of both. Returns the following:
5907
5908 "host": Host evals condition.
5909 "host or target": Host or Target evals condition.
5910 "target": Target evals condition.
5911 */
5912
5913 static const char *
5914 bp_condition_evaluator (struct breakpoint *b)
5915 {
5916 struct bp_location *bl;
5917 char host_evals = 0;
5918 char target_evals = 0;
5919
5920 if (!b)
5921 return NULL;
5922
5923 if (!is_breakpoint (b))
5924 return NULL;
5925
5926 if (gdb_evaluates_breakpoint_condition_p ()
5927 || !target_supports_evaluation_of_breakpoint_conditions ())
5928 return condition_evaluation_host;
5929
5930 for (bl = b->loc; bl; bl = bl->next)
5931 {
5932 if (bl->cond_bytecode)
5933 target_evals++;
5934 else
5935 host_evals++;
5936 }
5937
5938 if (host_evals && target_evals)
5939 return condition_evaluation_both;
5940 else if (target_evals)
5941 return condition_evaluation_target;
5942 else
5943 return condition_evaluation_host;
5944 }
5945
5946 /* Determine the breakpoint location's condition evaluator. This is
5947 similar to bp_condition_evaluator, but for locations. */
5948
5949 static const char *
5950 bp_location_condition_evaluator (struct bp_location *bl)
5951 {
5952 if (bl && !is_breakpoint (bl->owner))
5953 return NULL;
5954
5955 if (gdb_evaluates_breakpoint_condition_p ()
5956 || !target_supports_evaluation_of_breakpoint_conditions ())
5957 return condition_evaluation_host;
5958
5959 if (bl && bl->cond_bytecode)
5960 return condition_evaluation_target;
5961 else
5962 return condition_evaluation_host;
5963 }
5964
5965 /* Print the LOC location out of the list of B->LOC locations. */
5966
5967 static void
5968 print_breakpoint_location (struct breakpoint *b,
5969 struct bp_location *loc)
5970 {
5971 struct ui_out *uiout = current_uiout;
5972
5973 scoped_restore_current_program_space restore_pspace;
5974
5975 if (loc != NULL && loc->shlib_disabled)
5976 loc = NULL;
5977
5978 if (loc != NULL)
5979 set_current_program_space (loc->pspace);
5980
5981 if (b->display_canonical)
5982 uiout->field_string ("what", event_location_to_string (b->location.get ()));
5983 else if (loc && loc->symtab)
5984 {
5985 const struct symbol *sym = loc->symbol;
5986
5987 if (sym)
5988 {
5989 uiout->text ("in ");
5990 uiout->field_string ("func", sym->print_name (),
5991 function_name_style.style ());
5992 uiout->text (" ");
5993 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5994 uiout->text ("at ");
5995 }
5996 uiout->field_string ("file",
5997 symtab_to_filename_for_display (loc->symtab),
5998 file_name_style.style ());
5999 uiout->text (":");
6000
6001 if (uiout->is_mi_like_p ())
6002 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
6003
6004 uiout->field_signed ("line", loc->line_number);
6005 }
6006 else if (loc)
6007 {
6008 string_file stb;
6009
6010 print_address_symbolic (loc->gdbarch, loc->address, &stb,
6011 demangle, "");
6012 uiout->field_stream ("at", stb);
6013 }
6014 else
6015 {
6016 uiout->field_string ("pending",
6017 event_location_to_string (b->location.get ()));
6018 /* If extra_string is available, it could be holding a condition
6019 or dprintf arguments. In either case, make sure it is printed,
6020 too, but only for non-MI streams. */
6021 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
6022 {
6023 if (b->type == bp_dprintf)
6024 uiout->text (",");
6025 else
6026 uiout->text (" ");
6027 uiout->text (b->extra_string);
6028 }
6029 }
6030
6031 if (loc && is_breakpoint (b)
6032 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6033 && bp_condition_evaluator (b) == condition_evaluation_both)
6034 {
6035 uiout->text (" (");
6036 uiout->field_string ("evaluated-by",
6037 bp_location_condition_evaluator (loc));
6038 uiout->text (")");
6039 }
6040 }
6041
6042 static const char *
6043 bptype_string (enum bptype type)
6044 {
6045 struct ep_type_description
6046 {
6047 enum bptype type;
6048 const char *description;
6049 };
6050 static struct ep_type_description bptypes[] =
6051 {
6052 {bp_none, "?deleted?"},
6053 {bp_breakpoint, "breakpoint"},
6054 {bp_hardware_breakpoint, "hw breakpoint"},
6055 {bp_single_step, "sw single-step"},
6056 {bp_until, "until"},
6057 {bp_finish, "finish"},
6058 {bp_watchpoint, "watchpoint"},
6059 {bp_hardware_watchpoint, "hw watchpoint"},
6060 {bp_read_watchpoint, "read watchpoint"},
6061 {bp_access_watchpoint, "acc watchpoint"},
6062 {bp_longjmp, "longjmp"},
6063 {bp_longjmp_resume, "longjmp resume"},
6064 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6065 {bp_exception, "exception"},
6066 {bp_exception_resume, "exception resume"},
6067 {bp_step_resume, "step resume"},
6068 {bp_hp_step_resume, "high-priority step resume"},
6069 {bp_watchpoint_scope, "watchpoint scope"},
6070 {bp_call_dummy, "call dummy"},
6071 {bp_std_terminate, "std::terminate"},
6072 {bp_shlib_event, "shlib events"},
6073 {bp_thread_event, "thread events"},
6074 {bp_overlay_event, "overlay events"},
6075 {bp_longjmp_master, "longjmp master"},
6076 {bp_std_terminate_master, "std::terminate master"},
6077 {bp_exception_master, "exception master"},
6078 {bp_catchpoint, "catchpoint"},
6079 {bp_tracepoint, "tracepoint"},
6080 {bp_fast_tracepoint, "fast tracepoint"},
6081 {bp_static_tracepoint, "static tracepoint"},
6082 {bp_dprintf, "dprintf"},
6083 {bp_jit_event, "jit events"},
6084 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6085 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6086 };
6087
6088 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6089 || ((int) type != bptypes[(int) type].type))
6090 internal_error (__FILE__, __LINE__,
6091 _("bptypes table does not describe type #%d."),
6092 (int) type);
6093
6094 return bptypes[(int) type].description;
6095 }
6096
6097 /* For MI, output a field named 'thread-groups' with a list as the value.
6098 For CLI, prefix the list with the string 'inf'. */
6099
6100 static void
6101 output_thread_groups (struct ui_out *uiout,
6102 const char *field_name,
6103 const std::vector<int> &inf_nums,
6104 int mi_only)
6105 {
6106 int is_mi = uiout->is_mi_like_p ();
6107
6108 /* For backward compatibility, don't display inferiors in CLI unless
6109 there are several. Always display them for MI. */
6110 if (!is_mi && mi_only)
6111 return;
6112
6113 ui_out_emit_list list_emitter (uiout, field_name);
6114
6115 for (size_t i = 0; i < inf_nums.size (); i++)
6116 {
6117 if (is_mi)
6118 {
6119 char mi_group[10];
6120
6121 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
6122 uiout->field_string (NULL, mi_group);
6123 }
6124 else
6125 {
6126 if (i == 0)
6127 uiout->text (" inf ");
6128 else
6129 uiout->text (", ");
6130
6131 uiout->text (plongest (inf_nums[i]));
6132 }
6133 }
6134 }
6135
6136 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6137 instead of going via breakpoint_ops::print_one. This makes "maint
6138 info breakpoints" show the software breakpoint locations of
6139 catchpoints, which are considered internal implementation
6140 detail. */
6141
6142 static void
6143 print_one_breakpoint_location (struct breakpoint *b,
6144 struct bp_location *loc,
6145 int loc_number,
6146 struct bp_location **last_loc,
6147 int allflag, bool raw_loc)
6148 {
6149 struct command_line *l;
6150 static char bpenables[] = "nynny";
6151
6152 struct ui_out *uiout = current_uiout;
6153 int header_of_multiple = 0;
6154 int part_of_multiple = (loc != NULL);
6155 struct value_print_options opts;
6156
6157 get_user_print_options (&opts);
6158
6159 gdb_assert (!loc || loc_number != 0);
6160 /* See comment in print_one_breakpoint concerning treatment of
6161 breakpoints with single disabled location. */
6162 if (loc == NULL
6163 && (b->loc != NULL
6164 && (b->loc->next != NULL
6165 || !b->loc->enabled || b->loc->disabled_by_cond)))
6166 header_of_multiple = 1;
6167 if (loc == NULL)
6168 loc = b->loc;
6169
6170 annotate_record ();
6171
6172 /* 1 */
6173 annotate_field (0);
6174 if (part_of_multiple)
6175 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6176 else
6177 uiout->field_signed ("number", b->number);
6178
6179 /* 2 */
6180 annotate_field (1);
6181 if (part_of_multiple)
6182 uiout->field_skip ("type");
6183 else
6184 uiout->field_string ("type", bptype_string (b->type));
6185
6186 /* 3 */
6187 annotate_field (2);
6188 if (part_of_multiple)
6189 uiout->field_skip ("disp");
6190 else
6191 uiout->field_string ("disp", bpdisp_text (b->disposition));
6192
6193 /* 4 */
6194 annotate_field (3);
6195 if (part_of_multiple)
6196 uiout->field_string ("enabled", (loc->disabled_by_cond ? "N*"
6197 : (loc->enabled ? "y" : "n")));
6198 else
6199 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6200
6201 /* 5 and 6 */
6202 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
6203 b->ops->print_one (b, last_loc);
6204 else
6205 {
6206 if (is_watchpoint (b))
6207 {
6208 struct watchpoint *w = (struct watchpoint *) b;
6209
6210 /* Field 4, the address, is omitted (which makes the columns
6211 not line up too nicely with the headers, but the effect
6212 is relatively readable). */
6213 if (opts.addressprint)
6214 uiout->field_skip ("addr");
6215 annotate_field (5);
6216 uiout->field_string ("what", w->exp_string);
6217 }
6218 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6219 || is_ada_exception_catchpoint (b))
6220 {
6221 if (opts.addressprint)
6222 {
6223 annotate_field (4);
6224 if (header_of_multiple)
6225 uiout->field_string ("addr", "<MULTIPLE>",
6226 metadata_style.style ());
6227 else if (b->loc == NULL || loc->shlib_disabled)
6228 uiout->field_string ("addr", "<PENDING>",
6229 metadata_style.style ());
6230 else
6231 uiout->field_core_addr ("addr",
6232 loc->gdbarch, loc->address);
6233 }
6234 annotate_field (5);
6235 if (!header_of_multiple)
6236 print_breakpoint_location (b, loc);
6237 if (b->loc)
6238 *last_loc = b->loc;
6239 }
6240 }
6241
6242 if (loc != NULL && !header_of_multiple)
6243 {
6244 std::vector<int> inf_nums;
6245 int mi_only = 1;
6246
6247 for (inferior *inf : all_inferiors ())
6248 {
6249 if (inf->pspace == loc->pspace)
6250 inf_nums.push_back (inf->num);
6251 }
6252
6253 /* For backward compatibility, don't display inferiors in CLI unless
6254 there are several. Always display for MI. */
6255 if (allflag
6256 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6257 && (program_spaces.size () > 1
6258 || number_of_inferiors () > 1)
6259 /* LOC is for existing B, it cannot be in
6260 moribund_locations and thus having NULL OWNER. */
6261 && loc->owner->type != bp_catchpoint))
6262 mi_only = 0;
6263 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6264 }
6265
6266 if (!part_of_multiple)
6267 {
6268 if (b->thread != -1)
6269 {
6270 /* FIXME: This seems to be redundant and lost here; see the
6271 "stop only in" line a little further down. */
6272 uiout->text (" thread ");
6273 uiout->field_signed ("thread", b->thread);
6274 }
6275 else if (b->task != 0)
6276 {
6277 uiout->text (" task ");
6278 uiout->field_signed ("task", b->task);
6279 }
6280 }
6281
6282 uiout->text ("\n");
6283
6284 if (!part_of_multiple)
6285 b->ops->print_one_detail (b, uiout);
6286
6287 if (part_of_multiple && frame_id_p (b->frame_id))
6288 {
6289 annotate_field (6);
6290 uiout->text ("\tstop only in stack frame at ");
6291 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6292 the frame ID. */
6293 uiout->field_core_addr ("frame",
6294 b->gdbarch, b->frame_id.stack_addr);
6295 uiout->text ("\n");
6296 }
6297
6298 if (!part_of_multiple && b->cond_string)
6299 {
6300 annotate_field (7);
6301 if (is_tracepoint (b))
6302 uiout->text ("\ttrace only if ");
6303 else
6304 uiout->text ("\tstop only if ");
6305 uiout->field_string ("cond", b->cond_string);
6306
6307 /* Print whether the target is doing the breakpoint's condition
6308 evaluation. If GDB is doing the evaluation, don't print anything. */
6309 if (is_breakpoint (b)
6310 && breakpoint_condition_evaluation_mode ()
6311 == condition_evaluation_target)
6312 {
6313 uiout->message (" (%pF evals)",
6314 string_field ("evaluated-by",
6315 bp_condition_evaluator (b)));
6316 }
6317 uiout->text ("\n");
6318 }
6319
6320 if (!part_of_multiple && b->thread != -1)
6321 {
6322 /* FIXME should make an annotation for this. */
6323 uiout->text ("\tstop only in thread ");
6324 if (uiout->is_mi_like_p ())
6325 uiout->field_signed ("thread", b->thread);
6326 else
6327 {
6328 struct thread_info *thr = find_thread_global_id (b->thread);
6329
6330 uiout->field_string ("thread", print_thread_id (thr));
6331 }
6332 uiout->text ("\n");
6333 }
6334
6335 if (!part_of_multiple)
6336 {
6337 if (b->hit_count)
6338 {
6339 /* FIXME should make an annotation for this. */
6340 if (is_catchpoint (b))
6341 uiout->text ("\tcatchpoint");
6342 else if (is_tracepoint (b))
6343 uiout->text ("\ttracepoint");
6344 else
6345 uiout->text ("\tbreakpoint");
6346 uiout->text (" already hit ");
6347 uiout->field_signed ("times", b->hit_count);
6348 if (b->hit_count == 1)
6349 uiout->text (" time\n");
6350 else
6351 uiout->text (" times\n");
6352 }
6353 else
6354 {
6355 /* Output the count also if it is zero, but only if this is mi. */
6356 if (uiout->is_mi_like_p ())
6357 uiout->field_signed ("times", b->hit_count);
6358 }
6359 }
6360
6361 if (!part_of_multiple && b->ignore_count)
6362 {
6363 annotate_field (8);
6364 uiout->message ("\tignore next %pF hits\n",
6365 signed_field ("ignore", b->ignore_count));
6366 }
6367
6368 /* Note that an enable count of 1 corresponds to "enable once"
6369 behavior, which is reported by the combination of enablement and
6370 disposition, so we don't need to mention it here. */
6371 if (!part_of_multiple && b->enable_count > 1)
6372 {
6373 annotate_field (8);
6374 uiout->text ("\tdisable after ");
6375 /* Tweak the wording to clarify that ignore and enable counts
6376 are distinct, and have additive effect. */
6377 if (b->ignore_count)
6378 uiout->text ("additional ");
6379 else
6380 uiout->text ("next ");
6381 uiout->field_signed ("enable", b->enable_count);
6382 uiout->text (" hits\n");
6383 }
6384
6385 if (!part_of_multiple && is_tracepoint (b))
6386 {
6387 struct tracepoint *tp = (struct tracepoint *) b;
6388
6389 if (tp->traceframe_usage)
6390 {
6391 uiout->text ("\ttrace buffer usage ");
6392 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6393 uiout->text (" bytes\n");
6394 }
6395 }
6396
6397 l = b->commands ? b->commands.get () : NULL;
6398 if (!part_of_multiple && l)
6399 {
6400 annotate_field (9);
6401 ui_out_emit_tuple tuple_emitter (uiout, "script");
6402 print_command_lines (uiout, l, 4);
6403 }
6404
6405 if (is_tracepoint (b))
6406 {
6407 struct tracepoint *t = (struct tracepoint *) b;
6408
6409 if (!part_of_multiple && t->pass_count)
6410 {
6411 annotate_field (10);
6412 uiout->text ("\tpass count ");
6413 uiout->field_signed ("pass", t->pass_count);
6414 uiout->text (" \n");
6415 }
6416
6417 /* Don't display it when tracepoint or tracepoint location is
6418 pending. */
6419 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6420 {
6421 annotate_field (11);
6422
6423 if (uiout->is_mi_like_p ())
6424 uiout->field_string ("installed",
6425 loc->inserted ? "y" : "n");
6426 else
6427 {
6428 if (loc->inserted)
6429 uiout->text ("\t");
6430 else
6431 uiout->text ("\tnot ");
6432 uiout->text ("installed on target\n");
6433 }
6434 }
6435 }
6436
6437 if (uiout->is_mi_like_p () && !part_of_multiple)
6438 {
6439 if (is_watchpoint (b))
6440 {
6441 struct watchpoint *w = (struct watchpoint *) b;
6442
6443 uiout->field_string ("original-location", w->exp_string);
6444 }
6445 else if (b->location != NULL
6446 && event_location_to_string (b->location.get ()) != NULL)
6447 uiout->field_string ("original-location",
6448 event_location_to_string (b->location.get ()));
6449 }
6450 }
6451
6452 /* See breakpoint.h. */
6453
6454 bool fix_multi_location_breakpoint_output_globally = false;
6455
6456 static void
6457 print_one_breakpoint (struct breakpoint *b,
6458 struct bp_location **last_loc,
6459 int allflag)
6460 {
6461 struct ui_out *uiout = current_uiout;
6462 bool use_fixed_output
6463 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6464 || fix_multi_location_breakpoint_output_globally);
6465
6466 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6467 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
6468
6469 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6470 are outside. */
6471 if (!use_fixed_output)
6472 bkpt_tuple_emitter.reset ();
6473
6474 /* If this breakpoint has custom print function,
6475 it's already printed. Otherwise, print individual
6476 locations, if any. */
6477 if (b->ops == NULL
6478 || b->ops->print_one == NULL
6479 || allflag)
6480 {
6481 /* If breakpoint has a single location that is disabled, we
6482 print it as if it had several locations, since otherwise it's
6483 hard to represent "breakpoint enabled, location disabled"
6484 situation.
6485
6486 Note that while hardware watchpoints have several locations
6487 internally, that's not a property exposed to users.
6488
6489 Likewise, while catchpoints may be implemented with
6490 breakpoints (e.g., catch throw), that's not a property
6491 exposed to users. We do however display the internal
6492 breakpoint locations with "maint info breakpoints". */
6493 if (!is_hardware_watchpoint (b)
6494 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6495 || is_ada_exception_catchpoint (b))
6496 && (allflag
6497 || (b->loc && (b->loc->next
6498 || !b->loc->enabled
6499 || b->loc->disabled_by_cond))))
6500 {
6501 gdb::optional<ui_out_emit_list> locations_list;
6502
6503 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6504 MI record. For later versions, place breakpoint locations in a
6505 list. */
6506 if (uiout->is_mi_like_p () && use_fixed_output)
6507 locations_list.emplace (uiout, "locations");
6508
6509 int n = 1;
6510 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next, ++n)
6511 {
6512 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6513 print_one_breakpoint_location (b, loc, n, last_loc,
6514 allflag, allflag);
6515 }
6516 }
6517 }
6518 }
6519
6520 static int
6521 breakpoint_address_bits (struct breakpoint *b)
6522 {
6523 int print_address_bits = 0;
6524 struct bp_location *loc;
6525
6526 /* Software watchpoints that aren't watching memory don't have an
6527 address to print. */
6528 if (is_no_memory_software_watchpoint (b))
6529 return 0;
6530
6531 for (loc = b->loc; loc; loc = loc->next)
6532 {
6533 int addr_bit;
6534
6535 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6536 if (addr_bit > print_address_bits)
6537 print_address_bits = addr_bit;
6538 }
6539
6540 return print_address_bits;
6541 }
6542
6543 /* See breakpoint.h. */
6544
6545 void
6546 print_breakpoint (breakpoint *b)
6547 {
6548 struct bp_location *dummy_loc = NULL;
6549 print_one_breakpoint (b, &dummy_loc, 0);
6550 }
6551
6552 /* Return true if this breakpoint was set by the user, false if it is
6553 internal or momentary. */
6554
6555 int
6556 user_breakpoint_p (struct breakpoint *b)
6557 {
6558 return b->number > 0;
6559 }
6560
6561 /* See breakpoint.h. */
6562
6563 int
6564 pending_breakpoint_p (struct breakpoint *b)
6565 {
6566 return b->loc == NULL;
6567 }
6568
6569 /* Print information on breakpoints (including watchpoints and tracepoints).
6570
6571 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6572 understood by number_or_range_parser. Only breakpoints included in this
6573 list are then printed.
6574
6575 If SHOW_INTERNAL is true, print internal breakpoints.
6576
6577 If FILTER is non-NULL, call it on each breakpoint and only include the
6578 ones for which it returns true.
6579
6580 Return the total number of breakpoints listed. */
6581
6582 static int
6583 breakpoint_1 (const char *bp_num_list, bool show_internal,
6584 bool (*filter) (const struct breakpoint *))
6585 {
6586 struct breakpoint *b;
6587 struct bp_location *last_loc = NULL;
6588 int nr_printable_breakpoints;
6589 struct value_print_options opts;
6590 int print_address_bits = 0;
6591 int print_type_col_width = 14;
6592 struct ui_out *uiout = current_uiout;
6593 bool has_disabled_by_cond_location = false;
6594
6595 get_user_print_options (&opts);
6596
6597 /* Compute the number of rows in the table, as well as the size
6598 required for address fields. */
6599 nr_printable_breakpoints = 0;
6600 ALL_BREAKPOINTS (b)
6601 {
6602 /* If we have a filter, only list the breakpoints it accepts. */
6603 if (filter && !filter (b))
6604 continue;
6605
6606 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6607 accept. Skip the others. */
6608 if (bp_num_list != NULL && *bp_num_list != '\0')
6609 {
6610 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6611 continue;
6612 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6613 continue;
6614 }
6615
6616 if (show_internal || user_breakpoint_p (b))
6617 {
6618 int addr_bit, type_len;
6619
6620 addr_bit = breakpoint_address_bits (b);
6621 if (addr_bit > print_address_bits)
6622 print_address_bits = addr_bit;
6623
6624 type_len = strlen (bptype_string (b->type));
6625 if (type_len > print_type_col_width)
6626 print_type_col_width = type_len;
6627
6628 nr_printable_breakpoints++;
6629 }
6630 }
6631
6632 {
6633 ui_out_emit_table table_emitter (uiout,
6634 opts.addressprint ? 6 : 5,
6635 nr_printable_breakpoints,
6636 "BreakpointTable");
6637
6638 if (nr_printable_breakpoints > 0)
6639 annotate_breakpoints_headers ();
6640 if (nr_printable_breakpoints > 0)
6641 annotate_field (0);
6642 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6643 if (nr_printable_breakpoints > 0)
6644 annotate_field (1);
6645 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6646 if (nr_printable_breakpoints > 0)
6647 annotate_field (2);
6648 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6649 if (nr_printable_breakpoints > 0)
6650 annotate_field (3);
6651 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6652 if (opts.addressprint)
6653 {
6654 if (nr_printable_breakpoints > 0)
6655 annotate_field (4);
6656 if (print_address_bits <= 32)
6657 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6658 else
6659 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6660 }
6661 if (nr_printable_breakpoints > 0)
6662 annotate_field (5);
6663 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6664 uiout->table_body ();
6665 if (nr_printable_breakpoints > 0)
6666 annotate_breakpoints_table ();
6667
6668 ALL_BREAKPOINTS (b)
6669 {
6670 QUIT;
6671 /* If we have a filter, only list the breakpoints it accepts. */
6672 if (filter && !filter (b))
6673 continue;
6674
6675 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6676 accept. Skip the others. */
6677
6678 if (bp_num_list != NULL && *bp_num_list != '\0')
6679 {
6680 if (show_internal) /* maintenance info breakpoint */
6681 {
6682 if (parse_and_eval_long (bp_num_list) != b->number)
6683 continue;
6684 }
6685 else /* all others */
6686 {
6687 if (!number_is_in_list (bp_num_list, b->number))
6688 continue;
6689 }
6690 }
6691 /* We only print out user settable breakpoints unless the
6692 show_internal is set. */
6693 if (show_internal || user_breakpoint_p (b))
6694 {
6695 print_one_breakpoint (b, &last_loc, show_internal);
6696 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
6697 if (loc->disabled_by_cond)
6698 has_disabled_by_cond_location = true;
6699 }
6700 }
6701 }
6702
6703 if (nr_printable_breakpoints == 0)
6704 {
6705 /* If there's a filter, let the caller decide how to report
6706 empty list. */
6707 if (!filter)
6708 {
6709 if (bp_num_list == NULL || *bp_num_list == '\0')
6710 uiout->message ("No breakpoints or watchpoints.\n");
6711 else
6712 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6713 bp_num_list);
6714 }
6715 }
6716 else
6717 {
6718 if (last_loc && !server_command)
6719 set_next_address (last_loc->gdbarch, last_loc->address);
6720
6721 if (has_disabled_by_cond_location)
6722 uiout->message (_("(*): Breakpoint condition is invalid at this "
6723 "location.\n"));
6724 }
6725
6726 /* FIXME? Should this be moved up so that it is only called when
6727 there have been breakpoints? */
6728 annotate_breakpoints_table_end ();
6729
6730 return nr_printable_breakpoints;
6731 }
6732
6733 /* Display the value of default-collect in a way that is generally
6734 compatible with the breakpoint list. */
6735
6736 static void
6737 default_collect_info (void)
6738 {
6739 struct ui_out *uiout = current_uiout;
6740
6741 /* If it has no value (which is frequently the case), say nothing; a
6742 message like "No default-collect." gets in user's face when it's
6743 not wanted. */
6744 if (!*default_collect)
6745 return;
6746
6747 /* The following phrase lines up nicely with per-tracepoint collect
6748 actions. */
6749 uiout->text ("default collect ");
6750 uiout->field_string ("default-collect", default_collect);
6751 uiout->text (" \n");
6752 }
6753
6754 static void
6755 info_breakpoints_command (const char *args, int from_tty)
6756 {
6757 breakpoint_1 (args, false, NULL);
6758
6759 default_collect_info ();
6760 }
6761
6762 static void
6763 info_watchpoints_command (const char *args, int from_tty)
6764 {
6765 int num_printed = breakpoint_1 (args, false, is_watchpoint);
6766 struct ui_out *uiout = current_uiout;
6767
6768 if (num_printed == 0)
6769 {
6770 if (args == NULL || *args == '\0')
6771 uiout->message ("No watchpoints.\n");
6772 else
6773 uiout->message ("No watchpoint matching '%s'.\n", args);
6774 }
6775 }
6776
6777 static void
6778 maintenance_info_breakpoints (const char *args, int from_tty)
6779 {
6780 breakpoint_1 (args, true, NULL);
6781
6782 default_collect_info ();
6783 }
6784
6785 static int
6786 breakpoint_has_pc (struct breakpoint *b,
6787 struct program_space *pspace,
6788 CORE_ADDR pc, struct obj_section *section)
6789 {
6790 struct bp_location *bl = b->loc;
6791
6792 for (; bl; bl = bl->next)
6793 {
6794 if (bl->pspace == pspace
6795 && bl->address == pc
6796 && (!overlay_debugging || bl->section == section))
6797 return 1;
6798 }
6799 return 0;
6800 }
6801
6802 /* Print a message describing any user-breakpoints set at PC. This
6803 concerns with logical breakpoints, so we match program spaces, not
6804 address spaces. */
6805
6806 static void
6807 describe_other_breakpoints (struct gdbarch *gdbarch,
6808 struct program_space *pspace, CORE_ADDR pc,
6809 struct obj_section *section, int thread)
6810 {
6811 int others = 0;
6812 struct breakpoint *b;
6813
6814 ALL_BREAKPOINTS (b)
6815 others += (user_breakpoint_p (b)
6816 && breakpoint_has_pc (b, pspace, pc, section));
6817 if (others > 0)
6818 {
6819 if (others == 1)
6820 printf_filtered (_("Note: breakpoint "));
6821 else /* if (others == ???) */
6822 printf_filtered (_("Note: breakpoints "));
6823 ALL_BREAKPOINTS (b)
6824 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6825 {
6826 others--;
6827 printf_filtered ("%d", b->number);
6828 if (b->thread == -1 && thread != -1)
6829 printf_filtered (" (all threads)");
6830 else if (b->thread != -1)
6831 printf_filtered (" (thread %d)", b->thread);
6832 printf_filtered ("%s%s ",
6833 ((b->enable_state == bp_disabled
6834 || b->enable_state == bp_call_disabled)
6835 ? " (disabled)"
6836 : ""),
6837 (others > 1) ? ","
6838 : ((others == 1) ? " and" : ""));
6839 }
6840 current_uiout->message (_("also set at pc %ps.\n"),
6841 styled_string (address_style.style (),
6842 paddress (gdbarch, pc)));
6843 }
6844 }
6845 \f
6846
6847 /* Return true iff it is meaningful to use the address member of LOC.
6848 For some breakpoint types, the locations' address members are
6849 irrelevant and it makes no sense to attempt to compare them to
6850 other addresses (or use them for any other purpose either).
6851
6852 More specifically, software watchpoints and catchpoints that are
6853 not backed by breakpoints always have a zero valued location
6854 address and we don't want to mark breakpoints of any of these types
6855 to be a duplicate of an actual breakpoint location at address
6856 zero. */
6857
6858 static bool
6859 bl_address_is_meaningful (bp_location *loc)
6860 {
6861 return loc->loc_type != bp_loc_other;
6862 }
6863
6864 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6865 true if LOC1 and LOC2 represent the same watchpoint location. */
6866
6867 static int
6868 watchpoint_locations_match (struct bp_location *loc1,
6869 struct bp_location *loc2)
6870 {
6871 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6872 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6873
6874 /* Both of them must exist. */
6875 gdb_assert (w1 != NULL);
6876 gdb_assert (w2 != NULL);
6877
6878 /* If the target can evaluate the condition expression in hardware,
6879 then we we need to insert both watchpoints even if they are at
6880 the same place. Otherwise the watchpoint will only trigger when
6881 the condition of whichever watchpoint was inserted evaluates to
6882 true, not giving a chance for GDB to check the condition of the
6883 other watchpoint. */
6884 if ((w1->cond_exp
6885 && target_can_accel_watchpoint_condition (loc1->address,
6886 loc1->length,
6887 loc1->watchpoint_type,
6888 w1->cond_exp.get ()))
6889 || (w2->cond_exp
6890 && target_can_accel_watchpoint_condition (loc2->address,
6891 loc2->length,
6892 loc2->watchpoint_type,
6893 w2->cond_exp.get ())))
6894 return 0;
6895
6896 /* Note that this checks the owner's type, not the location's. In
6897 case the target does not support read watchpoints, but does
6898 support access watchpoints, we'll have bp_read_watchpoint
6899 watchpoints with hw_access locations. Those should be considered
6900 duplicates of hw_read locations. The hw_read locations will
6901 become hw_access locations later. */
6902 return (loc1->owner->type == loc2->owner->type
6903 && loc1->pspace->aspace == loc2->pspace->aspace
6904 && loc1->address == loc2->address
6905 && loc1->length == loc2->length);
6906 }
6907
6908 /* See breakpoint.h. */
6909
6910 int
6911 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6912 const address_space *aspace2, CORE_ADDR addr2)
6913 {
6914 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6915 || aspace1 == aspace2)
6916 && addr1 == addr2);
6917 }
6918
6919 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6920 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6921 matches ASPACE2. On targets that have global breakpoints, the address
6922 space doesn't really matter. */
6923
6924 static int
6925 breakpoint_address_match_range (const address_space *aspace1,
6926 CORE_ADDR addr1,
6927 int len1, const address_space *aspace2,
6928 CORE_ADDR addr2)
6929 {
6930 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6931 || aspace1 == aspace2)
6932 && addr2 >= addr1 && addr2 < addr1 + len1);
6933 }
6934
6935 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6936 a ranged breakpoint. In most targets, a match happens only if ASPACE
6937 matches the breakpoint's address space. On targets that have global
6938 breakpoints, the address space doesn't really matter. */
6939
6940 static int
6941 breakpoint_location_address_match (struct bp_location *bl,
6942 const address_space *aspace,
6943 CORE_ADDR addr)
6944 {
6945 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6946 aspace, addr)
6947 || (bl->length
6948 && breakpoint_address_match_range (bl->pspace->aspace,
6949 bl->address, bl->length,
6950 aspace, addr)));
6951 }
6952
6953 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6954 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6955 match happens only if ASPACE matches the breakpoint's address
6956 space. On targets that have global breakpoints, the address space
6957 doesn't really matter. */
6958
6959 static int
6960 breakpoint_location_address_range_overlap (struct bp_location *bl,
6961 const address_space *aspace,
6962 CORE_ADDR addr, int len)
6963 {
6964 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6965 || bl->pspace->aspace == aspace)
6966 {
6967 int bl_len = bl->length != 0 ? bl->length : 1;
6968
6969 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6970 return 1;
6971 }
6972 return 0;
6973 }
6974
6975 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6976 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6977 true, otherwise returns false. */
6978
6979 static int
6980 tracepoint_locations_match (struct bp_location *loc1,
6981 struct bp_location *loc2)
6982 {
6983 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6984 /* Since tracepoint locations are never duplicated with others', tracepoint
6985 locations at the same address of different tracepoints are regarded as
6986 different locations. */
6987 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6988 else
6989 return 0;
6990 }
6991
6992 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6993 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
6994 the same location. If SW_HW_BPS_MATCH is true, then software
6995 breakpoint locations and hardware breakpoint locations match,
6996 otherwise they don't. */
6997
6998 static int
6999 breakpoint_locations_match (struct bp_location *loc1,
7000 struct bp_location *loc2,
7001 bool sw_hw_bps_match)
7002 {
7003 int hw_point1, hw_point2;
7004
7005 /* Both of them must not be in moribund_locations. */
7006 gdb_assert (loc1->owner != NULL);
7007 gdb_assert (loc2->owner != NULL);
7008
7009 hw_point1 = is_hardware_watchpoint (loc1->owner);
7010 hw_point2 = is_hardware_watchpoint (loc2->owner);
7011
7012 if (hw_point1 != hw_point2)
7013 return 0;
7014 else if (hw_point1)
7015 return watchpoint_locations_match (loc1, loc2);
7016 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7017 return tracepoint_locations_match (loc1, loc2);
7018 else
7019 /* We compare bp_location.length in order to cover ranged
7020 breakpoints. Keep this in sync with
7021 bp_location_is_less_than. */
7022 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7023 loc2->pspace->aspace, loc2->address)
7024 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
7025 && loc1->length == loc2->length);
7026 }
7027
7028 static void
7029 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7030 int bnum, int have_bnum)
7031 {
7032 /* The longest string possibly returned by hex_string_custom
7033 is 50 chars. These must be at least that big for safety. */
7034 char astr1[64];
7035 char astr2[64];
7036
7037 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7038 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7039 if (have_bnum)
7040 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7041 bnum, astr1, astr2);
7042 else
7043 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7044 }
7045
7046 /* Adjust a breakpoint's address to account for architectural
7047 constraints on breakpoint placement. Return the adjusted address.
7048 Note: Very few targets require this kind of adjustment. For most
7049 targets, this function is simply the identity function. */
7050
7051 static CORE_ADDR
7052 adjust_breakpoint_address (struct gdbarch *gdbarch,
7053 CORE_ADDR bpaddr, enum bptype bptype)
7054 {
7055 if (bptype == bp_watchpoint
7056 || bptype == bp_hardware_watchpoint
7057 || bptype == bp_read_watchpoint
7058 || bptype == bp_access_watchpoint
7059 || bptype == bp_catchpoint)
7060 {
7061 /* Watchpoints and the various bp_catch_* eventpoints should not
7062 have their addresses modified. */
7063 return bpaddr;
7064 }
7065 else if (bptype == bp_single_step)
7066 {
7067 /* Single-step breakpoints should not have their addresses
7068 modified. If there's any architectural constrain that
7069 applies to this address, then it should have already been
7070 taken into account when the breakpoint was created in the
7071 first place. If we didn't do this, stepping through e.g.,
7072 Thumb-2 IT blocks would break. */
7073 return bpaddr;
7074 }
7075 else
7076 {
7077 CORE_ADDR adjusted_bpaddr = bpaddr;
7078
7079 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7080 {
7081 /* Some targets have architectural constraints on the placement
7082 of breakpoint instructions. Obtain the adjusted address. */
7083 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7084 }
7085
7086 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
7087
7088 /* An adjusted breakpoint address can significantly alter
7089 a user's expectations. Print a warning if an adjustment
7090 is required. */
7091 if (adjusted_bpaddr != bpaddr)
7092 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7093
7094 return adjusted_bpaddr;
7095 }
7096 }
7097
7098 static bp_loc_type
7099 bp_location_from_bp_type (bptype type)
7100 {
7101 switch (type)
7102 {
7103 case bp_breakpoint:
7104 case bp_single_step:
7105 case bp_until:
7106 case bp_finish:
7107 case bp_longjmp:
7108 case bp_longjmp_resume:
7109 case bp_longjmp_call_dummy:
7110 case bp_exception:
7111 case bp_exception_resume:
7112 case bp_step_resume:
7113 case bp_hp_step_resume:
7114 case bp_watchpoint_scope:
7115 case bp_call_dummy:
7116 case bp_std_terminate:
7117 case bp_shlib_event:
7118 case bp_thread_event:
7119 case bp_overlay_event:
7120 case bp_jit_event:
7121 case bp_longjmp_master:
7122 case bp_std_terminate_master:
7123 case bp_exception_master:
7124 case bp_gnu_ifunc_resolver:
7125 case bp_gnu_ifunc_resolver_return:
7126 case bp_dprintf:
7127 return bp_loc_software_breakpoint;
7128 case bp_hardware_breakpoint:
7129 return bp_loc_hardware_breakpoint;
7130 case bp_hardware_watchpoint:
7131 case bp_read_watchpoint:
7132 case bp_access_watchpoint:
7133 return bp_loc_hardware_watchpoint;
7134 case bp_watchpoint:
7135 case bp_catchpoint:
7136 case bp_tracepoint:
7137 case bp_fast_tracepoint:
7138 case bp_static_tracepoint:
7139 return bp_loc_other;
7140 default:
7141 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7142 }
7143 }
7144
7145 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7146 {
7147 this->owner = owner;
7148 this->cond_bytecode = NULL;
7149 this->shlib_disabled = 0;
7150 this->enabled = 1;
7151 this->disabled_by_cond = false;
7152
7153 this->loc_type = type;
7154
7155 if (this->loc_type == bp_loc_software_breakpoint
7156 || this->loc_type == bp_loc_hardware_breakpoint)
7157 mark_breakpoint_location_modified (this);
7158
7159 incref ();
7160 }
7161
7162 bp_location::bp_location (breakpoint *owner)
7163 : bp_location::bp_location (owner,
7164 bp_location_from_bp_type (owner->type))
7165 {
7166 }
7167
7168 /* Allocate a struct bp_location. */
7169
7170 static struct bp_location *
7171 allocate_bp_location (struct breakpoint *bpt)
7172 {
7173 return bpt->ops->allocate_location (bpt);
7174 }
7175
7176 /* Decrement reference count. If the reference count reaches 0,
7177 destroy the bp_location. Sets *BLP to NULL. */
7178
7179 static void
7180 decref_bp_location (struct bp_location **blp)
7181 {
7182 bp_location_ref_policy::decref (*blp);
7183 *blp = NULL;
7184 }
7185
7186 /* Add breakpoint B at the end of the global breakpoint chain. */
7187
7188 static breakpoint *
7189 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7190 {
7191 struct breakpoint *b1;
7192 struct breakpoint *result = b.get ();
7193
7194 /* Add this breakpoint to the end of the chain so that a list of
7195 breakpoints will come out in order of increasing numbers. */
7196
7197 b1 = breakpoint_chain;
7198 if (b1 == 0)
7199 breakpoint_chain = b.release ();
7200 else
7201 {
7202 while (b1->next)
7203 b1 = b1->next;
7204 b1->next = b.release ();
7205 }
7206
7207 return result;
7208 }
7209
7210 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7211
7212 static void
7213 init_raw_breakpoint_without_location (struct breakpoint *b,
7214 struct gdbarch *gdbarch,
7215 enum bptype bptype,
7216 const struct breakpoint_ops *ops)
7217 {
7218 gdb_assert (ops != NULL);
7219
7220 b->ops = ops;
7221 b->type = bptype;
7222 b->gdbarch = gdbarch;
7223 b->language = current_language->la_language;
7224 b->input_radix = input_radix;
7225 b->related_breakpoint = b;
7226 }
7227
7228 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7229 that has type BPTYPE and has no locations as yet. */
7230
7231 static struct breakpoint *
7232 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7233 enum bptype bptype,
7234 const struct breakpoint_ops *ops)
7235 {
7236 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7237
7238 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7239 return add_to_breakpoint_chain (std::move (b));
7240 }
7241
7242 /* Initialize loc->function_name. */
7243
7244 static void
7245 set_breakpoint_location_function (struct bp_location *loc)
7246 {
7247 gdb_assert (loc->owner != NULL);
7248
7249 if (loc->owner->type == bp_breakpoint
7250 || loc->owner->type == bp_hardware_breakpoint
7251 || is_tracepoint (loc->owner))
7252 {
7253 const char *function_name;
7254
7255 if (loc->msymbol != NULL
7256 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
7257 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
7258 {
7259 struct breakpoint *b = loc->owner;
7260
7261 function_name = loc->msymbol->linkage_name ();
7262
7263 if (b->type == bp_breakpoint && b->loc == loc
7264 && loc->next == NULL && b->related_breakpoint == b)
7265 {
7266 /* Create only the whole new breakpoint of this type but do not
7267 mess more complicated breakpoints with multiple locations. */
7268 b->type = bp_gnu_ifunc_resolver;
7269 /* Remember the resolver's address for use by the return
7270 breakpoint. */
7271 loc->related_address = loc->address;
7272 }
7273 }
7274 else
7275 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7276
7277 if (function_name)
7278 loc->function_name = xstrdup (function_name);
7279 }
7280 }
7281
7282 /* Attempt to determine architecture of location identified by SAL. */
7283 struct gdbarch *
7284 get_sal_arch (struct symtab_and_line sal)
7285 {
7286 if (sal.section)
7287 return sal.section->objfile->arch ();
7288 if (sal.symtab)
7289 return SYMTAB_OBJFILE (sal.symtab)->arch ();
7290
7291 return NULL;
7292 }
7293
7294 /* Low level routine for partially initializing a breakpoint of type
7295 BPTYPE. The newly created breakpoint's address, section, source
7296 file name, and line number are provided by SAL.
7297
7298 It is expected that the caller will complete the initialization of
7299 the newly created breakpoint struct as well as output any status
7300 information regarding the creation of a new breakpoint. */
7301
7302 static void
7303 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7304 struct symtab_and_line sal, enum bptype bptype,
7305 const struct breakpoint_ops *ops)
7306 {
7307 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7308
7309 add_location_to_breakpoint (b, &sal);
7310
7311 if (bptype != bp_catchpoint)
7312 gdb_assert (sal.pspace != NULL);
7313
7314 /* Store the program space that was used to set the breakpoint,
7315 except for ordinary breakpoints, which are independent of the
7316 program space. */
7317 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7318 b->pspace = sal.pspace;
7319 }
7320
7321 /* set_raw_breakpoint is a low level routine for allocating and
7322 partially initializing a breakpoint of type BPTYPE. The newly
7323 created breakpoint's address, section, source file name, and line
7324 number are provided by SAL. The newly created and partially
7325 initialized breakpoint is added to the breakpoint chain and
7326 is also returned as the value of this function.
7327
7328 It is expected that the caller will complete the initialization of
7329 the newly created breakpoint struct as well as output any status
7330 information regarding the creation of a new breakpoint. In
7331 particular, set_raw_breakpoint does NOT set the breakpoint
7332 number! Care should be taken to not allow an error to occur
7333 prior to completing the initialization of the breakpoint. If this
7334 should happen, a bogus breakpoint will be left on the chain. */
7335
7336 struct breakpoint *
7337 set_raw_breakpoint (struct gdbarch *gdbarch,
7338 struct symtab_and_line sal, enum bptype bptype,
7339 const struct breakpoint_ops *ops)
7340 {
7341 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7342
7343 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7344 return add_to_breakpoint_chain (std::move (b));
7345 }
7346
7347 /* Call this routine when stepping and nexting to enable a breakpoint
7348 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7349 initiated the operation. */
7350
7351 void
7352 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7353 {
7354 struct breakpoint *b, *b_tmp;
7355 int thread = tp->global_num;
7356
7357 /* To avoid having to rescan all objfile symbols at every step,
7358 we maintain a list of continually-inserted but always disabled
7359 longjmp "master" breakpoints. Here, we simply create momentary
7360 clones of those and enable them for the requested thread. */
7361 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7362 if (b->pspace == current_program_space
7363 && (b->type == bp_longjmp_master
7364 || b->type == bp_exception_master))
7365 {
7366 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7367 struct breakpoint *clone;
7368
7369 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7370 after their removal. */
7371 clone = momentary_breakpoint_from_master (b, type,
7372 &momentary_breakpoint_ops, 1);
7373 clone->thread = thread;
7374 }
7375
7376 tp->initiating_frame = frame;
7377 }
7378
7379 /* Delete all longjmp breakpoints from THREAD. */
7380 void
7381 delete_longjmp_breakpoint (int thread)
7382 {
7383 struct breakpoint *b, *b_tmp;
7384
7385 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7386 if (b->type == bp_longjmp || b->type == bp_exception)
7387 {
7388 if (b->thread == thread)
7389 delete_breakpoint (b);
7390 }
7391 }
7392
7393 void
7394 delete_longjmp_breakpoint_at_next_stop (int thread)
7395 {
7396 struct breakpoint *b, *b_tmp;
7397
7398 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7399 if (b->type == bp_longjmp || b->type == bp_exception)
7400 {
7401 if (b->thread == thread)
7402 b->disposition = disp_del_at_next_stop;
7403 }
7404 }
7405
7406 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7407 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7408 pointer to any of them. Return NULL if this system cannot place longjmp
7409 breakpoints. */
7410
7411 struct breakpoint *
7412 set_longjmp_breakpoint_for_call_dummy (void)
7413 {
7414 struct breakpoint *b, *retval = NULL;
7415
7416 ALL_BREAKPOINTS (b)
7417 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7418 {
7419 struct breakpoint *new_b;
7420
7421 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7422 &momentary_breakpoint_ops,
7423 1);
7424 new_b->thread = inferior_thread ()->global_num;
7425
7426 /* Link NEW_B into the chain of RETVAL breakpoints. */
7427
7428 gdb_assert (new_b->related_breakpoint == new_b);
7429 if (retval == NULL)
7430 retval = new_b;
7431 new_b->related_breakpoint = retval;
7432 while (retval->related_breakpoint != new_b->related_breakpoint)
7433 retval = retval->related_breakpoint;
7434 retval->related_breakpoint = new_b;
7435 }
7436
7437 return retval;
7438 }
7439
7440 /* Verify all existing dummy frames and their associated breakpoints for
7441 TP. Remove those which can no longer be found in the current frame
7442 stack.
7443
7444 You should call this function only at places where it is safe to currently
7445 unwind the whole stack. Failed stack unwind would discard live dummy
7446 frames. */
7447
7448 void
7449 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7450 {
7451 struct breakpoint *b, *b_tmp;
7452
7453 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7454 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7455 {
7456 struct breakpoint *dummy_b = b->related_breakpoint;
7457
7458 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7459 dummy_b = dummy_b->related_breakpoint;
7460 if (dummy_b->type != bp_call_dummy
7461 || frame_find_by_id (dummy_b->frame_id) != NULL)
7462 continue;
7463
7464 dummy_frame_discard (dummy_b->frame_id, tp);
7465
7466 while (b->related_breakpoint != b)
7467 {
7468 if (b_tmp == b->related_breakpoint)
7469 b_tmp = b->related_breakpoint->next;
7470 delete_breakpoint (b->related_breakpoint);
7471 }
7472 delete_breakpoint (b);
7473 }
7474 }
7475
7476 void
7477 enable_overlay_breakpoints (void)
7478 {
7479 struct breakpoint *b;
7480
7481 ALL_BREAKPOINTS (b)
7482 if (b->type == bp_overlay_event)
7483 {
7484 b->enable_state = bp_enabled;
7485 update_global_location_list (UGLL_MAY_INSERT);
7486 overlay_events_enabled = 1;
7487 }
7488 }
7489
7490 void
7491 disable_overlay_breakpoints (void)
7492 {
7493 struct breakpoint *b;
7494
7495 ALL_BREAKPOINTS (b)
7496 if (b->type == bp_overlay_event)
7497 {
7498 b->enable_state = bp_disabled;
7499 update_global_location_list (UGLL_DONT_INSERT);
7500 overlay_events_enabled = 0;
7501 }
7502 }
7503
7504 /* Set an active std::terminate breakpoint for each std::terminate
7505 master breakpoint. */
7506 void
7507 set_std_terminate_breakpoint (void)
7508 {
7509 struct breakpoint *b, *b_tmp;
7510
7511 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7512 if (b->pspace == current_program_space
7513 && b->type == bp_std_terminate_master)
7514 {
7515 momentary_breakpoint_from_master (b, bp_std_terminate,
7516 &momentary_breakpoint_ops, 1);
7517 }
7518 }
7519
7520 /* Delete all the std::terminate breakpoints. */
7521 void
7522 delete_std_terminate_breakpoint (void)
7523 {
7524 struct breakpoint *b, *b_tmp;
7525
7526 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7527 if (b->type == bp_std_terminate)
7528 delete_breakpoint (b);
7529 }
7530
7531 struct breakpoint *
7532 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7533 {
7534 struct breakpoint *b;
7535
7536 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7537 &internal_breakpoint_ops);
7538
7539 b->enable_state = bp_enabled;
7540 /* location has to be used or breakpoint_re_set will delete me. */
7541 b->location = new_address_location (b->loc->address, NULL, 0);
7542
7543 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7544
7545 return b;
7546 }
7547
7548 struct lang_and_radix
7549 {
7550 enum language lang;
7551 int radix;
7552 };
7553
7554 /* Create a breakpoint for JIT code registration and unregistration. */
7555
7556 struct breakpoint *
7557 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7558 {
7559 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7560 &internal_breakpoint_ops);
7561 }
7562
7563 /* Remove JIT code registration and unregistration breakpoint(s). */
7564
7565 void
7566 remove_jit_event_breakpoints (void)
7567 {
7568 struct breakpoint *b, *b_tmp;
7569
7570 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7571 if (b->type == bp_jit_event
7572 && b->loc->pspace == current_program_space)
7573 delete_breakpoint (b);
7574 }
7575
7576 void
7577 remove_solib_event_breakpoints (void)
7578 {
7579 struct breakpoint *b, *b_tmp;
7580
7581 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7582 if (b->type == bp_shlib_event
7583 && b->loc->pspace == current_program_space)
7584 delete_breakpoint (b);
7585 }
7586
7587 /* See breakpoint.h. */
7588
7589 void
7590 remove_solib_event_breakpoints_at_next_stop (void)
7591 {
7592 struct breakpoint *b, *b_tmp;
7593
7594 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7595 if (b->type == bp_shlib_event
7596 && b->loc->pspace == current_program_space)
7597 b->disposition = disp_del_at_next_stop;
7598 }
7599
7600 /* Helper for create_solib_event_breakpoint /
7601 create_and_insert_solib_event_breakpoint. Allows specifying which
7602 INSERT_MODE to pass through to update_global_location_list. */
7603
7604 static struct breakpoint *
7605 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7606 enum ugll_insert_mode insert_mode)
7607 {
7608 struct breakpoint *b;
7609
7610 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7611 &internal_breakpoint_ops);
7612 update_global_location_list_nothrow (insert_mode);
7613 return b;
7614 }
7615
7616 struct breakpoint *
7617 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7618 {
7619 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7620 }
7621
7622 /* See breakpoint.h. */
7623
7624 struct breakpoint *
7625 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7626 {
7627 struct breakpoint *b;
7628
7629 /* Explicitly tell update_global_location_list to insert
7630 locations. */
7631 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7632 if (!b->loc->inserted)
7633 {
7634 delete_breakpoint (b);
7635 return NULL;
7636 }
7637 return b;
7638 }
7639
7640 /* Disable any breakpoints that are on code in shared libraries. Only
7641 apply to enabled breakpoints, disabled ones can just stay disabled. */
7642
7643 void
7644 disable_breakpoints_in_shlibs (void)
7645 {
7646 struct bp_location *loc, **locp_tmp;
7647
7648 ALL_BP_LOCATIONS (loc, locp_tmp)
7649 {
7650 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7651 struct breakpoint *b = loc->owner;
7652
7653 /* We apply the check to all breakpoints, including disabled for
7654 those with loc->duplicate set. This is so that when breakpoint
7655 becomes enabled, or the duplicate is removed, gdb will try to
7656 insert all breakpoints. If we don't set shlib_disabled here,
7657 we'll try to insert those breakpoints and fail. */
7658 if (((b->type == bp_breakpoint)
7659 || (b->type == bp_jit_event)
7660 || (b->type == bp_hardware_breakpoint)
7661 || (is_tracepoint (b)))
7662 && loc->pspace == current_program_space
7663 && !loc->shlib_disabled
7664 && solib_name_from_address (loc->pspace, loc->address)
7665 )
7666 {
7667 loc->shlib_disabled = 1;
7668 }
7669 }
7670 }
7671
7672 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7673 notification of unloaded_shlib. Only apply to enabled breakpoints,
7674 disabled ones can just stay disabled. */
7675
7676 static void
7677 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7678 {
7679 struct bp_location *loc, **locp_tmp;
7680 int disabled_shlib_breaks = 0;
7681
7682 ALL_BP_LOCATIONS (loc, locp_tmp)
7683 {
7684 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7685 struct breakpoint *b = loc->owner;
7686
7687 if (solib->pspace == loc->pspace
7688 && !loc->shlib_disabled
7689 && (((b->type == bp_breakpoint
7690 || b->type == bp_jit_event
7691 || b->type == bp_hardware_breakpoint)
7692 && (loc->loc_type == bp_loc_hardware_breakpoint
7693 || loc->loc_type == bp_loc_software_breakpoint))
7694 || is_tracepoint (b))
7695 && solib_contains_address_p (solib, loc->address))
7696 {
7697 loc->shlib_disabled = 1;
7698 /* At this point, we cannot rely on remove_breakpoint
7699 succeeding so we must mark the breakpoint as not inserted
7700 to prevent future errors occurring in remove_breakpoints. */
7701 loc->inserted = 0;
7702
7703 /* This may cause duplicate notifications for the same breakpoint. */
7704 gdb::observers::breakpoint_modified.notify (b);
7705
7706 if (!disabled_shlib_breaks)
7707 {
7708 target_terminal::ours_for_output ();
7709 warning (_("Temporarily disabling breakpoints "
7710 "for unloaded shared library \"%s\""),
7711 solib->so_name);
7712 }
7713 disabled_shlib_breaks = 1;
7714 }
7715 }
7716 }
7717
7718 /* Disable any breakpoints and tracepoints in OBJFILE upon
7719 notification of free_objfile. Only apply to enabled breakpoints,
7720 disabled ones can just stay disabled. */
7721
7722 static void
7723 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7724 {
7725 struct breakpoint *b;
7726
7727 if (objfile == NULL)
7728 return;
7729
7730 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7731 managed by the user with add-symbol-file/remove-symbol-file.
7732 Similarly to how breakpoints in shared libraries are handled in
7733 response to "nosharedlibrary", mark breakpoints in such modules
7734 shlib_disabled so they end up uninserted on the next global
7735 location list update. Shared libraries not loaded by the user
7736 aren't handled here -- they're already handled in
7737 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7738 solib_unloaded observer. We skip objfiles that are not
7739 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7740 main objfile). */
7741 if ((objfile->flags & OBJF_SHARED) == 0
7742 || (objfile->flags & OBJF_USERLOADED) == 0)
7743 return;
7744
7745 ALL_BREAKPOINTS (b)
7746 {
7747 struct bp_location *loc;
7748 int bp_modified = 0;
7749
7750 if (!is_breakpoint (b) && !is_tracepoint (b))
7751 continue;
7752
7753 for (loc = b->loc; loc != NULL; loc = loc->next)
7754 {
7755 CORE_ADDR loc_addr = loc->address;
7756
7757 if (loc->loc_type != bp_loc_hardware_breakpoint
7758 && loc->loc_type != bp_loc_software_breakpoint)
7759 continue;
7760
7761 if (loc->shlib_disabled != 0)
7762 continue;
7763
7764 if (objfile->pspace != loc->pspace)
7765 continue;
7766
7767 if (loc->loc_type != bp_loc_hardware_breakpoint
7768 && loc->loc_type != bp_loc_software_breakpoint)
7769 continue;
7770
7771 if (is_addr_in_objfile (loc_addr, objfile))
7772 {
7773 loc->shlib_disabled = 1;
7774 /* At this point, we don't know whether the object was
7775 unmapped from the inferior or not, so leave the
7776 inserted flag alone. We'll handle failure to
7777 uninsert quietly, in case the object was indeed
7778 unmapped. */
7779
7780 mark_breakpoint_location_modified (loc);
7781
7782 bp_modified = 1;
7783 }
7784 }
7785
7786 if (bp_modified)
7787 gdb::observers::breakpoint_modified.notify (b);
7788 }
7789 }
7790
7791 /* FORK & VFORK catchpoints. */
7792
7793 /* An instance of this type is used to represent a fork or vfork
7794 catchpoint. A breakpoint is really of this type iff its ops pointer points
7795 to CATCH_FORK_BREAKPOINT_OPS. */
7796
7797 struct fork_catchpoint : public breakpoint
7798 {
7799 /* Process id of a child process whose forking triggered this
7800 catchpoint. This field is only valid immediately after this
7801 catchpoint has triggered. */
7802 ptid_t forked_inferior_pid;
7803 };
7804
7805 /* Implement the "insert" breakpoint_ops method for fork
7806 catchpoints. */
7807
7808 static int
7809 insert_catch_fork (struct bp_location *bl)
7810 {
7811 return target_insert_fork_catchpoint (inferior_ptid.pid ());
7812 }
7813
7814 /* Implement the "remove" breakpoint_ops method for fork
7815 catchpoints. */
7816
7817 static int
7818 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
7819 {
7820 return target_remove_fork_catchpoint (inferior_ptid.pid ());
7821 }
7822
7823 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7824 catchpoints. */
7825
7826 static int
7827 breakpoint_hit_catch_fork (const struct bp_location *bl,
7828 const address_space *aspace, CORE_ADDR bp_addr,
7829 const struct target_waitstatus *ws)
7830 {
7831 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7832
7833 if (ws->kind != TARGET_WAITKIND_FORKED)
7834 return 0;
7835
7836 c->forked_inferior_pid = ws->value.related_pid;
7837 return 1;
7838 }
7839
7840 /* Implement the "print_it" breakpoint_ops method for fork
7841 catchpoints. */
7842
7843 static enum print_stop_action
7844 print_it_catch_fork (bpstat bs)
7845 {
7846 struct ui_out *uiout = current_uiout;
7847 struct breakpoint *b = bs->breakpoint_at;
7848 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7849
7850 annotate_catchpoint (b->number);
7851 maybe_print_thread_hit_breakpoint (uiout);
7852 if (b->disposition == disp_del)
7853 uiout->text ("Temporary catchpoint ");
7854 else
7855 uiout->text ("Catchpoint ");
7856 if (uiout->is_mi_like_p ())
7857 {
7858 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7859 uiout->field_string ("disp", bpdisp_text (b->disposition));
7860 }
7861 uiout->field_signed ("bkptno", b->number);
7862 uiout->text (" (forked process ");
7863 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7864 uiout->text ("), ");
7865 return PRINT_SRC_AND_LOC;
7866 }
7867
7868 /* Implement the "print_one" breakpoint_ops method for fork
7869 catchpoints. */
7870
7871 static void
7872 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7873 {
7874 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7875 struct value_print_options opts;
7876 struct ui_out *uiout = current_uiout;
7877
7878 get_user_print_options (&opts);
7879
7880 /* Field 4, the address, is omitted (which makes the columns not
7881 line up too nicely with the headers, but the effect is relatively
7882 readable). */
7883 if (opts.addressprint)
7884 uiout->field_skip ("addr");
7885 annotate_field (5);
7886 uiout->text ("fork");
7887 if (c->forked_inferior_pid != null_ptid)
7888 {
7889 uiout->text (", process ");
7890 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7891 uiout->spaces (1);
7892 }
7893
7894 if (uiout->is_mi_like_p ())
7895 uiout->field_string ("catch-type", "fork");
7896 }
7897
7898 /* Implement the "print_mention" breakpoint_ops method for fork
7899 catchpoints. */
7900
7901 static void
7902 print_mention_catch_fork (struct breakpoint *b)
7903 {
7904 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7905 }
7906
7907 /* Implement the "print_recreate" breakpoint_ops method for fork
7908 catchpoints. */
7909
7910 static void
7911 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7912 {
7913 fprintf_unfiltered (fp, "catch fork");
7914 print_recreate_thread (b, fp);
7915 }
7916
7917 /* The breakpoint_ops structure to be used in fork catchpoints. */
7918
7919 static struct breakpoint_ops catch_fork_breakpoint_ops;
7920
7921 /* Implement the "insert" breakpoint_ops method for vfork
7922 catchpoints. */
7923
7924 static int
7925 insert_catch_vfork (struct bp_location *bl)
7926 {
7927 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
7928 }
7929
7930 /* Implement the "remove" breakpoint_ops method for vfork
7931 catchpoints. */
7932
7933 static int
7934 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
7935 {
7936 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
7937 }
7938
7939 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7940 catchpoints. */
7941
7942 static int
7943 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7944 const address_space *aspace, CORE_ADDR bp_addr,
7945 const struct target_waitstatus *ws)
7946 {
7947 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7948
7949 if (ws->kind != TARGET_WAITKIND_VFORKED)
7950 return 0;
7951
7952 c->forked_inferior_pid = ws->value.related_pid;
7953 return 1;
7954 }
7955
7956 /* Implement the "print_it" breakpoint_ops method for vfork
7957 catchpoints. */
7958
7959 static enum print_stop_action
7960 print_it_catch_vfork (bpstat bs)
7961 {
7962 struct ui_out *uiout = current_uiout;
7963 struct breakpoint *b = bs->breakpoint_at;
7964 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7965
7966 annotate_catchpoint (b->number);
7967 maybe_print_thread_hit_breakpoint (uiout);
7968 if (b->disposition == disp_del)
7969 uiout->text ("Temporary catchpoint ");
7970 else
7971 uiout->text ("Catchpoint ");
7972 if (uiout->is_mi_like_p ())
7973 {
7974 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7975 uiout->field_string ("disp", bpdisp_text (b->disposition));
7976 }
7977 uiout->field_signed ("bkptno", b->number);
7978 uiout->text (" (vforked process ");
7979 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7980 uiout->text ("), ");
7981 return PRINT_SRC_AND_LOC;
7982 }
7983
7984 /* Implement the "print_one" breakpoint_ops method for vfork
7985 catchpoints. */
7986
7987 static void
7988 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7989 {
7990 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7991 struct value_print_options opts;
7992 struct ui_out *uiout = current_uiout;
7993
7994 get_user_print_options (&opts);
7995 /* Field 4, the address, is omitted (which makes the columns not
7996 line up too nicely with the headers, but the effect is relatively
7997 readable). */
7998 if (opts.addressprint)
7999 uiout->field_skip ("addr");
8000 annotate_field (5);
8001 uiout->text ("vfork");
8002 if (c->forked_inferior_pid != null_ptid)
8003 {
8004 uiout->text (", process ");
8005 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
8006 uiout->spaces (1);
8007 }
8008
8009 if (uiout->is_mi_like_p ())
8010 uiout->field_string ("catch-type", "vfork");
8011 }
8012
8013 /* Implement the "print_mention" breakpoint_ops method for vfork
8014 catchpoints. */
8015
8016 static void
8017 print_mention_catch_vfork (struct breakpoint *b)
8018 {
8019 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8020 }
8021
8022 /* Implement the "print_recreate" breakpoint_ops method for vfork
8023 catchpoints. */
8024
8025 static void
8026 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8027 {
8028 fprintf_unfiltered (fp, "catch vfork");
8029 print_recreate_thread (b, fp);
8030 }
8031
8032 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8033
8034 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8035
8036 /* An instance of this type is used to represent an solib catchpoint.
8037 A breakpoint is really of this type iff its ops pointer points to
8038 CATCH_SOLIB_BREAKPOINT_OPS. */
8039
8040 struct solib_catchpoint : public breakpoint
8041 {
8042 ~solib_catchpoint () override;
8043
8044 /* True for "catch load", false for "catch unload". */
8045 bool is_load;
8046
8047 /* Regular expression to match, if any. COMPILED is only valid when
8048 REGEX is non-NULL. */
8049 char *regex;
8050 std::unique_ptr<compiled_regex> compiled;
8051 };
8052
8053 solib_catchpoint::~solib_catchpoint ()
8054 {
8055 xfree (this->regex);
8056 }
8057
8058 static int
8059 insert_catch_solib (struct bp_location *ignore)
8060 {
8061 return 0;
8062 }
8063
8064 static int
8065 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
8066 {
8067 return 0;
8068 }
8069
8070 static int
8071 breakpoint_hit_catch_solib (const struct bp_location *bl,
8072 const address_space *aspace,
8073 CORE_ADDR bp_addr,
8074 const struct target_waitstatus *ws)
8075 {
8076 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8077 struct breakpoint *other;
8078
8079 if (ws->kind == TARGET_WAITKIND_LOADED)
8080 return 1;
8081
8082 ALL_BREAKPOINTS (other)
8083 {
8084 struct bp_location *other_bl;
8085
8086 if (other == bl->owner)
8087 continue;
8088
8089 if (other->type != bp_shlib_event)
8090 continue;
8091
8092 if (self->pspace != NULL && other->pspace != self->pspace)
8093 continue;
8094
8095 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8096 {
8097 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8098 return 1;
8099 }
8100 }
8101
8102 return 0;
8103 }
8104
8105 static void
8106 check_status_catch_solib (struct bpstats *bs)
8107 {
8108 struct solib_catchpoint *self
8109 = (struct solib_catchpoint *) bs->breakpoint_at;
8110
8111 if (self->is_load)
8112 {
8113 for (so_list *iter : current_program_space->added_solibs)
8114 {
8115 if (!self->regex
8116 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
8117 return;
8118 }
8119 }
8120 else
8121 {
8122 for (const std::string &iter : current_program_space->deleted_solibs)
8123 {
8124 if (!self->regex
8125 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
8126 return;
8127 }
8128 }
8129
8130 bs->stop = 0;
8131 bs->print_it = print_it_noop;
8132 }
8133
8134 static enum print_stop_action
8135 print_it_catch_solib (bpstat bs)
8136 {
8137 struct breakpoint *b = bs->breakpoint_at;
8138 struct ui_out *uiout = current_uiout;
8139
8140 annotate_catchpoint (b->number);
8141 maybe_print_thread_hit_breakpoint (uiout);
8142 if (b->disposition == disp_del)
8143 uiout->text ("Temporary catchpoint ");
8144 else
8145 uiout->text ("Catchpoint ");
8146 uiout->field_signed ("bkptno", b->number);
8147 uiout->text ("\n");
8148 if (uiout->is_mi_like_p ())
8149 uiout->field_string ("disp", bpdisp_text (b->disposition));
8150 print_solib_event (1);
8151 return PRINT_SRC_AND_LOC;
8152 }
8153
8154 static void
8155 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8156 {
8157 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8158 struct value_print_options opts;
8159 struct ui_out *uiout = current_uiout;
8160
8161 get_user_print_options (&opts);
8162 /* Field 4, the address, is omitted (which makes the columns not
8163 line up too nicely with the headers, but the effect is relatively
8164 readable). */
8165 if (opts.addressprint)
8166 {
8167 annotate_field (4);
8168 uiout->field_skip ("addr");
8169 }
8170
8171 std::string msg;
8172 annotate_field (5);
8173 if (self->is_load)
8174 {
8175 if (self->regex)
8176 msg = string_printf (_("load of library matching %s"), self->regex);
8177 else
8178 msg = _("load of library");
8179 }
8180 else
8181 {
8182 if (self->regex)
8183 msg = string_printf (_("unload of library matching %s"), self->regex);
8184 else
8185 msg = _("unload of library");
8186 }
8187 uiout->field_string ("what", msg);
8188
8189 if (uiout->is_mi_like_p ())
8190 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8191 }
8192
8193 static void
8194 print_mention_catch_solib (struct breakpoint *b)
8195 {
8196 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8197
8198 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8199 self->is_load ? "load" : "unload");
8200 }
8201
8202 static void
8203 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8204 {
8205 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8206
8207 fprintf_unfiltered (fp, "%s %s",
8208 b->disposition == disp_del ? "tcatch" : "catch",
8209 self->is_load ? "load" : "unload");
8210 if (self->regex)
8211 fprintf_unfiltered (fp, " %s", self->regex);
8212 fprintf_unfiltered (fp, "\n");
8213 }
8214
8215 static struct breakpoint_ops catch_solib_breakpoint_ops;
8216
8217 /* See breakpoint.h. */
8218
8219 void
8220 add_solib_catchpoint (const char *arg, bool is_load, bool is_temp, bool enabled)
8221 {
8222 struct gdbarch *gdbarch = get_current_arch ();
8223
8224 if (!arg)
8225 arg = "";
8226 arg = skip_spaces (arg);
8227
8228 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
8229
8230 if (*arg != '\0')
8231 {
8232 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8233 _("Invalid regexp")));
8234 c->regex = xstrdup (arg);
8235 }
8236
8237 c->is_load = is_load;
8238 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
8239 &catch_solib_breakpoint_ops);
8240
8241 c->enable_state = enabled ? bp_enabled : bp_disabled;
8242
8243 install_breakpoint (0, std::move (c), 1);
8244 }
8245
8246 /* A helper function that does all the work for "catch load" and
8247 "catch unload". */
8248
8249 static void
8250 catch_load_or_unload (const char *arg, int from_tty, int is_load,
8251 struct cmd_list_element *command)
8252 {
8253 const int enabled = 1;
8254 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
8255
8256 add_solib_catchpoint (arg, is_load, temp, enabled);
8257 }
8258
8259 static void
8260 catch_load_command_1 (const char *arg, int from_tty,
8261 struct cmd_list_element *command)
8262 {
8263 catch_load_or_unload (arg, from_tty, 1, command);
8264 }
8265
8266 static void
8267 catch_unload_command_1 (const char *arg, int from_tty,
8268 struct cmd_list_element *command)
8269 {
8270 catch_load_or_unload (arg, from_tty, 0, command);
8271 }
8272
8273 /* See breakpoint.h. */
8274
8275 void
8276 init_catchpoint (struct breakpoint *b,
8277 struct gdbarch *gdbarch, bool temp,
8278 const char *cond_string,
8279 const struct breakpoint_ops *ops)
8280 {
8281 symtab_and_line sal;
8282 sal.pspace = current_program_space;
8283
8284 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8285
8286 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8287 b->disposition = temp ? disp_del : disp_donttouch;
8288 }
8289
8290 void
8291 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8292 {
8293 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8294 set_breakpoint_number (internal, b);
8295 if (is_tracepoint (b))
8296 set_tracepoint_count (breakpoint_count);
8297 if (!internal)
8298 mention (b);
8299 gdb::observers::breakpoint_created.notify (b);
8300
8301 if (update_gll)
8302 update_global_location_list (UGLL_MAY_INSERT);
8303 }
8304
8305 static void
8306 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8307 bool temp, const char *cond_string,
8308 const struct breakpoint_ops *ops)
8309 {
8310 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
8311
8312 init_catchpoint (c.get (), gdbarch, temp, cond_string, ops);
8313
8314 c->forked_inferior_pid = null_ptid;
8315
8316 install_breakpoint (0, std::move (c), 1);
8317 }
8318
8319 /* Exec catchpoints. */
8320
8321 /* An instance of this type is used to represent an exec catchpoint.
8322 A breakpoint is really of this type iff its ops pointer points to
8323 CATCH_EXEC_BREAKPOINT_OPS. */
8324
8325 struct exec_catchpoint : public breakpoint
8326 {
8327 ~exec_catchpoint () override;
8328
8329 /* Filename of a program whose exec triggered this catchpoint.
8330 This field is only valid immediately after this catchpoint has
8331 triggered. */
8332 char *exec_pathname;
8333 };
8334
8335 /* Exec catchpoint destructor. */
8336
8337 exec_catchpoint::~exec_catchpoint ()
8338 {
8339 xfree (this->exec_pathname);
8340 }
8341
8342 static int
8343 insert_catch_exec (struct bp_location *bl)
8344 {
8345 return target_insert_exec_catchpoint (inferior_ptid.pid ());
8346 }
8347
8348 static int
8349 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8350 {
8351 return target_remove_exec_catchpoint (inferior_ptid.pid ());
8352 }
8353
8354 static int
8355 breakpoint_hit_catch_exec (const struct bp_location *bl,
8356 const address_space *aspace, CORE_ADDR bp_addr,
8357 const struct target_waitstatus *ws)
8358 {
8359 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8360
8361 if (ws->kind != TARGET_WAITKIND_EXECD)
8362 return 0;
8363
8364 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8365 return 1;
8366 }
8367
8368 static enum print_stop_action
8369 print_it_catch_exec (bpstat bs)
8370 {
8371 struct ui_out *uiout = current_uiout;
8372 struct breakpoint *b = bs->breakpoint_at;
8373 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8374
8375 annotate_catchpoint (b->number);
8376 maybe_print_thread_hit_breakpoint (uiout);
8377 if (b->disposition == disp_del)
8378 uiout->text ("Temporary catchpoint ");
8379 else
8380 uiout->text ("Catchpoint ");
8381 if (uiout->is_mi_like_p ())
8382 {
8383 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8384 uiout->field_string ("disp", bpdisp_text (b->disposition));
8385 }
8386 uiout->field_signed ("bkptno", b->number);
8387 uiout->text (" (exec'd ");
8388 uiout->field_string ("new-exec", c->exec_pathname);
8389 uiout->text ("), ");
8390
8391 return PRINT_SRC_AND_LOC;
8392 }
8393
8394 static void
8395 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8396 {
8397 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8398 struct value_print_options opts;
8399 struct ui_out *uiout = current_uiout;
8400
8401 get_user_print_options (&opts);
8402
8403 /* Field 4, the address, is omitted (which makes the columns
8404 not line up too nicely with the headers, but the effect
8405 is relatively readable). */
8406 if (opts.addressprint)
8407 uiout->field_skip ("addr");
8408 annotate_field (5);
8409 uiout->text ("exec");
8410 if (c->exec_pathname != NULL)
8411 {
8412 uiout->text (", program \"");
8413 uiout->field_string ("what", c->exec_pathname);
8414 uiout->text ("\" ");
8415 }
8416
8417 if (uiout->is_mi_like_p ())
8418 uiout->field_string ("catch-type", "exec");
8419 }
8420
8421 static void
8422 print_mention_catch_exec (struct breakpoint *b)
8423 {
8424 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8425 }
8426
8427 /* Implement the "print_recreate" breakpoint_ops method for exec
8428 catchpoints. */
8429
8430 static void
8431 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8432 {
8433 fprintf_unfiltered (fp, "catch exec");
8434 print_recreate_thread (b, fp);
8435 }
8436
8437 static struct breakpoint_ops catch_exec_breakpoint_ops;
8438
8439 static int
8440 hw_breakpoint_used_count (void)
8441 {
8442 int i = 0;
8443 struct breakpoint *b;
8444 struct bp_location *bl;
8445
8446 ALL_BREAKPOINTS (b)
8447 {
8448 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8449 for (bl = b->loc; bl; bl = bl->next)
8450 {
8451 /* Special types of hardware breakpoints may use more than
8452 one register. */
8453 i += b->ops->resources_needed (bl);
8454 }
8455 }
8456
8457 return i;
8458 }
8459
8460 /* Returns the resources B would use if it were a hardware
8461 watchpoint. */
8462
8463 static int
8464 hw_watchpoint_use_count (struct breakpoint *b)
8465 {
8466 int i = 0;
8467 struct bp_location *bl;
8468
8469 if (!breakpoint_enabled (b))
8470 return 0;
8471
8472 for (bl = b->loc; bl; bl = bl->next)
8473 {
8474 /* Special types of hardware watchpoints may use more than
8475 one register. */
8476 i += b->ops->resources_needed (bl);
8477 }
8478
8479 return i;
8480 }
8481
8482 /* Returns the sum the used resources of all hardware watchpoints of
8483 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8484 the sum of the used resources of all hardware watchpoints of other
8485 types _not_ TYPE. */
8486
8487 static int
8488 hw_watchpoint_used_count_others (struct breakpoint *except,
8489 enum bptype type, int *other_type_used)
8490 {
8491 int i = 0;
8492 struct breakpoint *b;
8493
8494 *other_type_used = 0;
8495 ALL_BREAKPOINTS (b)
8496 {
8497 if (b == except)
8498 continue;
8499 if (!breakpoint_enabled (b))
8500 continue;
8501
8502 if (b->type == type)
8503 i += hw_watchpoint_use_count (b);
8504 else if (is_hardware_watchpoint (b))
8505 *other_type_used = 1;
8506 }
8507
8508 return i;
8509 }
8510
8511 void
8512 disable_watchpoints_before_interactive_call_start (void)
8513 {
8514 struct breakpoint *b;
8515
8516 ALL_BREAKPOINTS (b)
8517 {
8518 if (is_watchpoint (b) && breakpoint_enabled (b))
8519 {
8520 b->enable_state = bp_call_disabled;
8521 update_global_location_list (UGLL_DONT_INSERT);
8522 }
8523 }
8524 }
8525
8526 void
8527 enable_watchpoints_after_interactive_call_stop (void)
8528 {
8529 struct breakpoint *b;
8530
8531 ALL_BREAKPOINTS (b)
8532 {
8533 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8534 {
8535 b->enable_state = bp_enabled;
8536 update_global_location_list (UGLL_MAY_INSERT);
8537 }
8538 }
8539 }
8540
8541 void
8542 disable_breakpoints_before_startup (void)
8543 {
8544 current_program_space->executing_startup = 1;
8545 update_global_location_list (UGLL_DONT_INSERT);
8546 }
8547
8548 void
8549 enable_breakpoints_after_startup (void)
8550 {
8551 current_program_space->executing_startup = 0;
8552 breakpoint_re_set ();
8553 }
8554
8555 /* Create a new single-step breakpoint for thread THREAD, with no
8556 locations. */
8557
8558 static struct breakpoint *
8559 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8560 {
8561 std::unique_ptr<breakpoint> b (new breakpoint ());
8562
8563 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
8564 &momentary_breakpoint_ops);
8565
8566 b->disposition = disp_donttouch;
8567 b->frame_id = null_frame_id;
8568
8569 b->thread = thread;
8570 gdb_assert (b->thread != 0);
8571
8572 return add_to_breakpoint_chain (std::move (b));
8573 }
8574
8575 /* Set a momentary breakpoint of type TYPE at address specified by
8576 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8577 frame. */
8578
8579 breakpoint_up
8580 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8581 struct frame_id frame_id, enum bptype type)
8582 {
8583 struct breakpoint *b;
8584
8585 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8586 tail-called one. */
8587 gdb_assert (!frame_id_artificial_p (frame_id));
8588
8589 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8590 b->enable_state = bp_enabled;
8591 b->disposition = disp_donttouch;
8592 b->frame_id = frame_id;
8593
8594 b->thread = inferior_thread ()->global_num;
8595
8596 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8597
8598 return breakpoint_up (b);
8599 }
8600
8601 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8602 The new breakpoint will have type TYPE, use OPS as its
8603 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8604
8605 static struct breakpoint *
8606 momentary_breakpoint_from_master (struct breakpoint *orig,
8607 enum bptype type,
8608 const struct breakpoint_ops *ops,
8609 int loc_enabled)
8610 {
8611 struct breakpoint *copy;
8612
8613 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8614 copy->loc = allocate_bp_location (copy);
8615 set_breakpoint_location_function (copy->loc);
8616
8617 copy->loc->gdbarch = orig->loc->gdbarch;
8618 copy->loc->requested_address = orig->loc->requested_address;
8619 copy->loc->address = orig->loc->address;
8620 copy->loc->section = orig->loc->section;
8621 copy->loc->pspace = orig->loc->pspace;
8622 copy->loc->probe = orig->loc->probe;
8623 copy->loc->line_number = orig->loc->line_number;
8624 copy->loc->symtab = orig->loc->symtab;
8625 copy->loc->enabled = loc_enabled;
8626 copy->frame_id = orig->frame_id;
8627 copy->thread = orig->thread;
8628 copy->pspace = orig->pspace;
8629
8630 copy->enable_state = bp_enabled;
8631 copy->disposition = disp_donttouch;
8632 copy->number = internal_breakpoint_number--;
8633
8634 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8635 return copy;
8636 }
8637
8638 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8639 ORIG is NULL. */
8640
8641 struct breakpoint *
8642 clone_momentary_breakpoint (struct breakpoint *orig)
8643 {
8644 /* If there's nothing to clone, then return nothing. */
8645 if (orig == NULL)
8646 return NULL;
8647
8648 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8649 }
8650
8651 breakpoint_up
8652 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8653 enum bptype type)
8654 {
8655 struct symtab_and_line sal;
8656
8657 sal = find_pc_line (pc, 0);
8658 sal.pc = pc;
8659 sal.section = find_pc_overlay (pc);
8660 sal.explicit_pc = 1;
8661
8662 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8663 }
8664 \f
8665
8666 /* Tell the user we have just set a breakpoint B. */
8667
8668 static void
8669 mention (struct breakpoint *b)
8670 {
8671 b->ops->print_mention (b);
8672 current_uiout->text ("\n");
8673 }
8674 \f
8675
8676 static bool bp_loc_is_permanent (struct bp_location *loc);
8677
8678 /* Handle "set breakpoint auto-hw on".
8679
8680 If the explicitly specified breakpoint type is not hardware
8681 breakpoint, check the memory map to see whether the breakpoint
8682 address is in read-only memory.
8683
8684 - location type is not hardware breakpoint, memory is read-only.
8685 We change the type of the location to hardware breakpoint.
8686
8687 - location type is hardware breakpoint, memory is read-write. This
8688 means we've previously made the location hardware one, but then the
8689 memory map changed, so we undo.
8690 */
8691
8692 static void
8693 handle_automatic_hardware_breakpoints (bp_location *bl)
8694 {
8695 if (automatic_hardware_breakpoints
8696 && bl->owner->type != bp_hardware_breakpoint
8697 && (bl->loc_type == bp_loc_software_breakpoint
8698 || bl->loc_type == bp_loc_hardware_breakpoint))
8699 {
8700 /* When breakpoints are removed, remove_breakpoints will use
8701 location types we've just set here, the only possible problem
8702 is that memory map has changed during running program, but
8703 it's not going to work anyway with current gdb. */
8704 mem_region *mr = lookup_mem_region (bl->address);
8705
8706 if (mr != nullptr)
8707 {
8708 enum bp_loc_type new_type;
8709
8710 if (mr->attrib.mode != MEM_RW)
8711 new_type = bp_loc_hardware_breakpoint;
8712 else
8713 new_type = bp_loc_software_breakpoint;
8714
8715 if (new_type != bl->loc_type)
8716 {
8717 static bool said = false;
8718
8719 bl->loc_type = new_type;
8720 if (!said)
8721 {
8722 fprintf_filtered (gdb_stdout,
8723 _("Note: automatically using "
8724 "hardware breakpoints for "
8725 "read-only addresses.\n"));
8726 said = true;
8727 }
8728 }
8729 }
8730 }
8731 }
8732
8733 static struct bp_location *
8734 add_location_to_breakpoint (struct breakpoint *b,
8735 const struct symtab_and_line *sal)
8736 {
8737 struct bp_location *loc, **tmp;
8738 CORE_ADDR adjusted_address;
8739 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8740
8741 if (loc_gdbarch == NULL)
8742 loc_gdbarch = b->gdbarch;
8743
8744 /* Adjust the breakpoint's address prior to allocating a location.
8745 Once we call allocate_bp_location(), that mostly uninitialized
8746 location will be placed on the location chain. Adjustment of the
8747 breakpoint may cause target_read_memory() to be called and we do
8748 not want its scan of the location chain to find a breakpoint and
8749 location that's only been partially initialized. */
8750 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8751 sal->pc, b->type);
8752
8753 /* Sort the locations by their ADDRESS. */
8754 loc = allocate_bp_location (b);
8755 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8756 tmp = &((*tmp)->next))
8757 ;
8758 loc->next = *tmp;
8759 *tmp = loc;
8760
8761 loc->requested_address = sal->pc;
8762 loc->address = adjusted_address;
8763 loc->pspace = sal->pspace;
8764 loc->probe.prob = sal->prob;
8765 loc->probe.objfile = sal->objfile;
8766 gdb_assert (loc->pspace != NULL);
8767 loc->section = sal->section;
8768 loc->gdbarch = loc_gdbarch;
8769 loc->line_number = sal->line;
8770 loc->symtab = sal->symtab;
8771 loc->symbol = sal->symbol;
8772 loc->msymbol = sal->msymbol;
8773 loc->objfile = sal->objfile;
8774
8775 set_breakpoint_location_function (loc);
8776
8777 /* While by definition, permanent breakpoints are already present in the
8778 code, we don't mark the location as inserted. Normally one would expect
8779 that GDB could rely on that breakpoint instruction to stop the program,
8780 thus removing the need to insert its own breakpoint, except that executing
8781 the breakpoint instruction can kill the target instead of reporting a
8782 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8783 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8784 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8785 breakpoint be inserted normally results in QEMU knowing about the GDB
8786 breakpoint, and thus trap before the breakpoint instruction is executed.
8787 (If GDB later needs to continue execution past the permanent breakpoint,
8788 it manually increments the PC, thus avoiding executing the breakpoint
8789 instruction.) */
8790 if (bp_loc_is_permanent (loc))
8791 loc->permanent = 1;
8792
8793 return loc;
8794 }
8795 \f
8796
8797 /* Return true if LOC is pointing to a permanent breakpoint,
8798 return false otherwise. */
8799
8800 static bool
8801 bp_loc_is_permanent (struct bp_location *loc)
8802 {
8803 gdb_assert (loc != NULL);
8804
8805 /* If we have a non-breakpoint-backed catchpoint or a software
8806 watchpoint, just return 0. We should not attempt to read from
8807 the addresses the locations of these breakpoint types point to.
8808 gdbarch_program_breakpoint_here_p, below, will attempt to read
8809 memory. */
8810 if (!bl_address_is_meaningful (loc))
8811 return false;
8812
8813 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8814 switch_to_program_space_and_thread (loc->pspace);
8815 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
8816 }
8817
8818 /* Build a command list for the dprintf corresponding to the current
8819 settings of the dprintf style options. */
8820
8821 static void
8822 update_dprintf_command_list (struct breakpoint *b)
8823 {
8824 char *dprintf_args = b->extra_string;
8825 char *printf_line = NULL;
8826
8827 if (!dprintf_args)
8828 return;
8829
8830 dprintf_args = skip_spaces (dprintf_args);
8831
8832 /* Allow a comma, as it may have terminated a location, but don't
8833 insist on it. */
8834 if (*dprintf_args == ',')
8835 ++dprintf_args;
8836 dprintf_args = skip_spaces (dprintf_args);
8837
8838 if (*dprintf_args != '"')
8839 error (_("Bad format string, missing '\"'."));
8840
8841 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8842 printf_line = xstrprintf ("printf %s", dprintf_args);
8843 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8844 {
8845 if (!dprintf_function)
8846 error (_("No function supplied for dprintf call"));
8847
8848 if (dprintf_channel && strlen (dprintf_channel) > 0)
8849 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8850 dprintf_function,
8851 dprintf_channel,
8852 dprintf_args);
8853 else
8854 printf_line = xstrprintf ("call (void) %s (%s)",
8855 dprintf_function,
8856 dprintf_args);
8857 }
8858 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8859 {
8860 if (target_can_run_breakpoint_commands ())
8861 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8862 else
8863 {
8864 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8865 printf_line = xstrprintf ("printf %s", dprintf_args);
8866 }
8867 }
8868 else
8869 internal_error (__FILE__, __LINE__,
8870 _("Invalid dprintf style."));
8871
8872 gdb_assert (printf_line != NULL);
8873
8874 /* Manufacture a printf sequence. */
8875 struct command_line *printf_cmd_line
8876 = new struct command_line (simple_control, printf_line);
8877 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8878 command_lines_deleter ()));
8879 }
8880
8881 /* Update all dprintf commands, making their command lists reflect
8882 current style settings. */
8883
8884 static void
8885 update_dprintf_commands (const char *args, int from_tty,
8886 struct cmd_list_element *c)
8887 {
8888 struct breakpoint *b;
8889
8890 ALL_BREAKPOINTS (b)
8891 {
8892 if (b->type == bp_dprintf)
8893 update_dprintf_command_list (b);
8894 }
8895 }
8896
8897 /* Create a breakpoint with SAL as location. Use LOCATION
8898 as a description of the location, and COND_STRING
8899 as condition expression. If LOCATION is NULL then create an
8900 "address location" from the address in the SAL. */
8901
8902 static void
8903 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8904 gdb::array_view<const symtab_and_line> sals,
8905 event_location_up &&location,
8906 gdb::unique_xmalloc_ptr<char> filter,
8907 gdb::unique_xmalloc_ptr<char> cond_string,
8908 gdb::unique_xmalloc_ptr<char> extra_string,
8909 enum bptype type, enum bpdisp disposition,
8910 int thread, int task, int ignore_count,
8911 const struct breakpoint_ops *ops, int from_tty,
8912 int enabled, int internal, unsigned flags,
8913 int display_canonical)
8914 {
8915 int i;
8916
8917 if (type == bp_hardware_breakpoint)
8918 {
8919 int target_resources_ok;
8920
8921 i = hw_breakpoint_used_count ();
8922 target_resources_ok =
8923 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8924 i + 1, 0);
8925 if (target_resources_ok == 0)
8926 error (_("No hardware breakpoint support in the target."));
8927 else if (target_resources_ok < 0)
8928 error (_("Hardware breakpoints used exceeds limit."));
8929 }
8930
8931 gdb_assert (!sals.empty ());
8932
8933 for (const auto &sal : sals)
8934 {
8935 struct bp_location *loc;
8936
8937 if (from_tty)
8938 {
8939 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8940 if (!loc_gdbarch)
8941 loc_gdbarch = gdbarch;
8942
8943 describe_other_breakpoints (loc_gdbarch,
8944 sal.pspace, sal.pc, sal.section, thread);
8945 }
8946
8947 if (&sal == &sals[0])
8948 {
8949 init_raw_breakpoint (b, gdbarch, sal, type, ops);
8950 b->thread = thread;
8951 b->task = task;
8952
8953 b->cond_string = cond_string.release ();
8954 b->extra_string = extra_string.release ();
8955 b->ignore_count = ignore_count;
8956 b->enable_state = enabled ? bp_enabled : bp_disabled;
8957 b->disposition = disposition;
8958
8959 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8960 b->loc->inserted = 1;
8961
8962 if (type == bp_static_tracepoint)
8963 {
8964 struct tracepoint *t = (struct tracepoint *) b;
8965 struct static_tracepoint_marker marker;
8966
8967 if (strace_marker_p (b))
8968 {
8969 /* We already know the marker exists, otherwise, we
8970 wouldn't see a sal for it. */
8971 const char *p
8972 = &event_location_to_string (b->location.get ())[3];
8973 const char *endp;
8974
8975 p = skip_spaces (p);
8976
8977 endp = skip_to_space (p);
8978
8979 t->static_trace_marker_id.assign (p, endp - p);
8980
8981 printf_filtered (_("Probed static tracepoint "
8982 "marker \"%s\"\n"),
8983 t->static_trace_marker_id.c_str ());
8984 }
8985 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8986 {
8987 t->static_trace_marker_id = std::move (marker.str_id);
8988
8989 printf_filtered (_("Probed static tracepoint "
8990 "marker \"%s\"\n"),
8991 t->static_trace_marker_id.c_str ());
8992 }
8993 else
8994 warning (_("Couldn't determine the static "
8995 "tracepoint marker to probe"));
8996 }
8997
8998 loc = b->loc;
8999 }
9000 else
9001 {
9002 loc = add_location_to_breakpoint (b, &sal);
9003 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9004 loc->inserted = 1;
9005 }
9006
9007 /* Do not set breakpoint locations conditions yet. As locations
9008 are inserted, they get sorted based on their addresses. Let
9009 the list stabilize to have reliable location numbers. */
9010
9011 /* Dynamic printf requires and uses additional arguments on the
9012 command line, otherwise it's an error. */
9013 if (type == bp_dprintf)
9014 {
9015 if (b->extra_string)
9016 update_dprintf_command_list (b);
9017 else
9018 error (_("Format string required"));
9019 }
9020 else if (b->extra_string)
9021 error (_("Garbage '%s' at end of command"), b->extra_string);
9022 }
9023
9024
9025 /* The order of the locations is now stable. Set the location
9026 condition using the location's number. */
9027 int loc_num = 1;
9028 for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)
9029 {
9030 if (b->cond_string != nullptr)
9031 set_breakpoint_location_condition (b->cond_string, loc, b->number,
9032 loc_num);
9033
9034 ++loc_num;
9035 }
9036
9037 b->display_canonical = display_canonical;
9038 if (location != NULL)
9039 b->location = std::move (location);
9040 else
9041 b->location = new_address_location (b->loc->address, NULL, 0);
9042 b->filter = std::move (filter);
9043 }
9044
9045 static void
9046 create_breakpoint_sal (struct gdbarch *gdbarch,
9047 gdb::array_view<const symtab_and_line> sals,
9048 event_location_up &&location,
9049 gdb::unique_xmalloc_ptr<char> filter,
9050 gdb::unique_xmalloc_ptr<char> cond_string,
9051 gdb::unique_xmalloc_ptr<char> extra_string,
9052 enum bptype type, enum bpdisp disposition,
9053 int thread, int task, int ignore_count,
9054 const struct breakpoint_ops *ops, int from_tty,
9055 int enabled, int internal, unsigned flags,
9056 int display_canonical)
9057 {
9058 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
9059
9060 init_breakpoint_sal (b.get (), gdbarch,
9061 sals, std::move (location),
9062 std::move (filter),
9063 std::move (cond_string),
9064 std::move (extra_string),
9065 type, disposition,
9066 thread, task, ignore_count,
9067 ops, from_tty,
9068 enabled, internal, flags,
9069 display_canonical);
9070
9071 install_breakpoint (internal, std::move (b), 0);
9072 }
9073
9074 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9075 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9076 value. COND_STRING, if not NULL, specified the condition to be
9077 used for all breakpoints. Essentially the only case where
9078 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9079 function. In that case, it's still not possible to specify
9080 separate conditions for different overloaded functions, so
9081 we take just a single condition string.
9082
9083 NOTE: If the function succeeds, the caller is expected to cleanup
9084 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9085 array contents). If the function fails (error() is called), the
9086 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9087 COND and SALS arrays and each of those arrays contents. */
9088
9089 static void
9090 create_breakpoints_sal (struct gdbarch *gdbarch,
9091 struct linespec_result *canonical,
9092 gdb::unique_xmalloc_ptr<char> cond_string,
9093 gdb::unique_xmalloc_ptr<char> extra_string,
9094 enum bptype type, enum bpdisp disposition,
9095 int thread, int task, int ignore_count,
9096 const struct breakpoint_ops *ops, int from_tty,
9097 int enabled, int internal, unsigned flags)
9098 {
9099 if (canonical->pre_expanded)
9100 gdb_assert (canonical->lsals.size () == 1);
9101
9102 for (const auto &lsal : canonical->lsals)
9103 {
9104 /* Note that 'location' can be NULL in the case of a plain
9105 'break', without arguments. */
9106 event_location_up location
9107 = (canonical->location != NULL
9108 ? copy_event_location (canonical->location.get ()) : NULL);
9109 gdb::unique_xmalloc_ptr<char> filter_string
9110 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
9111
9112 create_breakpoint_sal (gdbarch, lsal.sals,
9113 std::move (location),
9114 std::move (filter_string),
9115 std::move (cond_string),
9116 std::move (extra_string),
9117 type, disposition,
9118 thread, task, ignore_count, ops,
9119 from_tty, enabled, internal, flags,
9120 canonical->special_display);
9121 }
9122 }
9123
9124 /* Parse LOCATION which is assumed to be a SAL specification possibly
9125 followed by conditionals. On return, SALS contains an array of SAL
9126 addresses found. LOCATION points to the end of the SAL (for
9127 linespec locations).
9128
9129 The array and the line spec strings are allocated on the heap, it is
9130 the caller's responsibility to free them. */
9131
9132 static void
9133 parse_breakpoint_sals (struct event_location *location,
9134 struct linespec_result *canonical)
9135 {
9136 struct symtab_and_line cursal;
9137
9138 if (event_location_type (location) == LINESPEC_LOCATION)
9139 {
9140 const char *spec = get_linespec_location (location)->spec_string;
9141
9142 if (spec == NULL)
9143 {
9144 /* The last displayed codepoint, if it's valid, is our default
9145 breakpoint address. */
9146 if (last_displayed_sal_is_valid ())
9147 {
9148 /* Set sal's pspace, pc, symtab, and line to the values
9149 corresponding to the last call to print_frame_info.
9150 Be sure to reinitialize LINE with NOTCURRENT == 0
9151 as the breakpoint line number is inappropriate otherwise.
9152 find_pc_line would adjust PC, re-set it back. */
9153 symtab_and_line sal = get_last_displayed_sal ();
9154 CORE_ADDR pc = sal.pc;
9155
9156 sal = find_pc_line (pc, 0);
9157
9158 /* "break" without arguments is equivalent to "break *PC"
9159 where PC is the last displayed codepoint's address. So
9160 make sure to set sal.explicit_pc to prevent GDB from
9161 trying to expand the list of sals to include all other
9162 instances with the same symtab and line. */
9163 sal.pc = pc;
9164 sal.explicit_pc = 1;
9165
9166 struct linespec_sals lsal;
9167 lsal.sals = {sal};
9168 lsal.canonical = NULL;
9169
9170 canonical->lsals.push_back (std::move (lsal));
9171 return;
9172 }
9173 else
9174 error (_("No default breakpoint address now."));
9175 }
9176 }
9177
9178 /* Force almost all breakpoints to be in terms of the
9179 current_source_symtab (which is decode_line_1's default).
9180 This should produce the results we want almost all of the
9181 time while leaving default_breakpoint_* alone.
9182
9183 ObjC: However, don't match an Objective-C method name which
9184 may have a '+' or '-' succeeded by a '['. */
9185 cursal = get_current_source_symtab_and_line ();
9186 if (last_displayed_sal_is_valid ())
9187 {
9188 const char *spec = NULL;
9189
9190 if (event_location_type (location) == LINESPEC_LOCATION)
9191 spec = get_linespec_location (location)->spec_string;
9192
9193 if (!cursal.symtab
9194 || (spec != NULL
9195 && strchr ("+-", spec[0]) != NULL
9196 && spec[1] != '['))
9197 {
9198 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9199 get_last_displayed_symtab (),
9200 get_last_displayed_line (),
9201 canonical, NULL, NULL);
9202 return;
9203 }
9204 }
9205
9206 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9207 cursal.symtab, cursal.line, canonical, NULL, NULL);
9208 }
9209
9210
9211 /* Convert each SAL into a real PC. Verify that the PC can be
9212 inserted as a breakpoint. If it can't throw an error. */
9213
9214 static void
9215 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
9216 {
9217 for (auto &sal : sals)
9218 resolve_sal_pc (&sal);
9219 }
9220
9221 /* Fast tracepoints may have restrictions on valid locations. For
9222 instance, a fast tracepoint using a jump instead of a trap will
9223 likely have to overwrite more bytes than a trap would, and so can
9224 only be placed where the instruction is longer than the jump, or a
9225 multi-instruction sequence does not have a jump into the middle of
9226 it, etc. */
9227
9228 static void
9229 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9230 gdb::array_view<const symtab_and_line> sals)
9231 {
9232 for (const auto &sal : sals)
9233 {
9234 struct gdbarch *sarch;
9235
9236 sarch = get_sal_arch (sal);
9237 /* We fall back to GDBARCH if there is no architecture
9238 associated with SAL. */
9239 if (sarch == NULL)
9240 sarch = gdbarch;
9241 std::string msg;
9242 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
9243 error (_("May not have a fast tracepoint at %s%s"),
9244 paddress (sarch, sal.pc), msg.c_str ());
9245 }
9246 }
9247
9248 /* Given TOK, a string specification of condition and thread, as
9249 accepted by the 'break' command, extract the condition
9250 string and thread number and set *COND_STRING and *THREAD.
9251 PC identifies the context at which the condition should be parsed.
9252 If no condition is found, *COND_STRING is set to NULL.
9253 If no thread is found, *THREAD is set to -1. */
9254
9255 static void
9256 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9257 char **cond_string, int *thread, int *task,
9258 char **rest)
9259 {
9260 *cond_string = NULL;
9261 *thread = -1;
9262 *task = 0;
9263 *rest = NULL;
9264 bool force = false;
9265
9266 while (tok && *tok)
9267 {
9268 const char *end_tok;
9269 int toklen;
9270 const char *cond_start = NULL;
9271 const char *cond_end = NULL;
9272
9273 tok = skip_spaces (tok);
9274
9275 if ((*tok == '"' || *tok == ',') && rest)
9276 {
9277 *rest = savestring (tok, strlen (tok));
9278 return;
9279 }
9280
9281 end_tok = skip_to_space (tok);
9282
9283 toklen = end_tok - tok;
9284
9285 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9286 {
9287 tok = cond_start = end_tok + 1;
9288 try
9289 {
9290 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9291 }
9292 catch (const gdb_exception_error &)
9293 {
9294 if (!force)
9295 throw;
9296 else
9297 tok = tok + strlen (tok);
9298 }
9299 cond_end = tok;
9300 *cond_string = savestring (cond_start, cond_end - cond_start);
9301 }
9302 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
9303 {
9304 tok = tok + toklen;
9305 force = true;
9306 }
9307 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9308 {
9309 const char *tmptok;
9310 struct thread_info *thr;
9311
9312 tok = end_tok + 1;
9313 thr = parse_thread_id (tok, &tmptok);
9314 if (tok == tmptok)
9315 error (_("Junk after thread keyword."));
9316 *thread = thr->global_num;
9317 tok = tmptok;
9318 }
9319 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9320 {
9321 char *tmptok;
9322
9323 tok = end_tok + 1;
9324 *task = strtol (tok, &tmptok, 0);
9325 if (tok == tmptok)
9326 error (_("Junk after task keyword."));
9327 if (!valid_task_id (*task))
9328 error (_("Unknown task %d."), *task);
9329 tok = tmptok;
9330 }
9331 else if (rest)
9332 {
9333 *rest = savestring (tok, strlen (tok));
9334 return;
9335 }
9336 else
9337 error (_("Junk at end of arguments."));
9338 }
9339 }
9340
9341 /* Call 'find_condition_and_thread' for each sal in SALS until a parse
9342 succeeds. The parsed values are written to COND_STRING, THREAD,
9343 TASK, and REST. See the comment of 'find_condition_and_thread'
9344 for the description of these parameters and INPUT. */
9345
9346 static void
9347 find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
9348 const char *input, char **cond_string,
9349 int *thread, int *task, char **rest)
9350 {
9351 int num_failures = 0;
9352 for (auto &sal : sals)
9353 {
9354 char *cond = nullptr;
9355 int thread_id = 0;
9356 int task_id = 0;
9357 char *remaining = nullptr;
9358
9359 /* Here we want to parse 'arg' to separate condition from thread
9360 number. But because parsing happens in a context and the
9361 contexts of sals might be different, try each until there is
9362 success. Finding one successful parse is sufficient for our
9363 goal. When setting the breakpoint we'll re-parse the
9364 condition in the context of each sal. */
9365 try
9366 {
9367 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
9368 &task_id, &remaining);
9369 *cond_string = cond;
9370 *thread = thread_id;
9371 *task = task_id;
9372 *rest = remaining;
9373 break;
9374 }
9375 catch (const gdb_exception_error &e)
9376 {
9377 num_failures++;
9378 /* If no sal remains, do not continue. */
9379 if (num_failures == sals.size ())
9380 throw;
9381 }
9382 }
9383 }
9384
9385 /* Decode a static tracepoint marker spec. */
9386
9387 static std::vector<symtab_and_line>
9388 decode_static_tracepoint_spec (const char **arg_p)
9389 {
9390 const char *p = &(*arg_p)[3];
9391 const char *endp;
9392
9393 p = skip_spaces (p);
9394
9395 endp = skip_to_space (p);
9396
9397 std::string marker_str (p, endp - p);
9398
9399 std::vector<static_tracepoint_marker> markers
9400 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9401 if (markers.empty ())
9402 error (_("No known static tracepoint marker named %s"),
9403 marker_str.c_str ());
9404
9405 std::vector<symtab_and_line> sals;
9406 sals.reserve (markers.size ());
9407
9408 for (const static_tracepoint_marker &marker : markers)
9409 {
9410 symtab_and_line sal = find_pc_line (marker.address, 0);
9411 sal.pc = marker.address;
9412 sals.push_back (sal);
9413 }
9414
9415 *arg_p = endp;
9416 return sals;
9417 }
9418
9419 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9420 according to IS_TRACEPOINT. */
9421
9422 static const struct breakpoint_ops *
9423 breakpoint_ops_for_event_location_type (enum event_location_type location_type,
9424 bool is_tracepoint)
9425 {
9426 if (is_tracepoint)
9427 {
9428 if (location_type == PROBE_LOCATION)
9429 return &tracepoint_probe_breakpoint_ops;
9430 else
9431 return &tracepoint_breakpoint_ops;
9432 }
9433 else
9434 {
9435 if (location_type == PROBE_LOCATION)
9436 return &bkpt_probe_breakpoint_ops;
9437 else
9438 return &bkpt_breakpoint_ops;
9439 }
9440 }
9441
9442 /* See breakpoint.h. */
9443
9444 const struct breakpoint_ops *
9445 breakpoint_ops_for_event_location (const struct event_location *location,
9446 bool is_tracepoint)
9447 {
9448 if (location != nullptr)
9449 return breakpoint_ops_for_event_location_type
9450 (event_location_type (location), is_tracepoint);
9451 return is_tracepoint ? &tracepoint_breakpoint_ops : &bkpt_breakpoint_ops;
9452 }
9453
9454 /* See breakpoint.h. */
9455
9456 int
9457 create_breakpoint (struct gdbarch *gdbarch,
9458 struct event_location *location,
9459 const char *cond_string,
9460 int thread, const char *extra_string,
9461 int parse_extra,
9462 int tempflag, enum bptype type_wanted,
9463 int ignore_count,
9464 enum auto_boolean pending_break_support,
9465 const struct breakpoint_ops *ops,
9466 int from_tty, int enabled, int internal,
9467 unsigned flags)
9468 {
9469 struct linespec_result canonical;
9470 int pending = 0;
9471 int task = 0;
9472 int prev_bkpt_count = breakpoint_count;
9473
9474 gdb_assert (ops != NULL);
9475
9476 /* If extra_string isn't useful, set it to NULL. */
9477 if (extra_string != NULL && *extra_string == '\0')
9478 extra_string = NULL;
9479
9480 try
9481 {
9482 ops->create_sals_from_location (location, &canonical, type_wanted);
9483 }
9484 catch (const gdb_exception_error &e)
9485 {
9486 /* If caller is interested in rc value from parse, set
9487 value. */
9488 if (e.error == NOT_FOUND_ERROR)
9489 {
9490 /* If pending breakpoint support is turned off, throw
9491 error. */
9492
9493 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9494 throw;
9495
9496 exception_print (gdb_stderr, e);
9497
9498 /* If pending breakpoint support is auto query and the user
9499 selects no, then simply return the error code. */
9500 if (pending_break_support == AUTO_BOOLEAN_AUTO
9501 && !nquery (_("Make %s pending on future shared library load? "),
9502 bptype_string (type_wanted)))
9503 return 0;
9504
9505 /* At this point, either the user was queried about setting
9506 a pending breakpoint and selected yes, or pending
9507 breakpoint behavior is on and thus a pending breakpoint
9508 is defaulted on behalf of the user. */
9509 pending = 1;
9510 }
9511 else
9512 throw;
9513 }
9514
9515 if (!pending && canonical.lsals.empty ())
9516 return 0;
9517
9518 /* Resolve all line numbers to PC's and verify that the addresses
9519 are ok for the target. */
9520 if (!pending)
9521 {
9522 for (auto &lsal : canonical.lsals)
9523 breakpoint_sals_to_pc (lsal.sals);
9524 }
9525
9526 /* Fast tracepoints may have additional restrictions on location. */
9527 if (!pending && type_wanted == bp_fast_tracepoint)
9528 {
9529 for (const auto &lsal : canonical.lsals)
9530 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9531 }
9532
9533 /* Verify that condition can be parsed, before setting any
9534 breakpoints. Allocate a separate condition expression for each
9535 breakpoint. */
9536 if (!pending)
9537 {
9538 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9539 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9540
9541 if (parse_extra)
9542 {
9543 char *rest;
9544 char *cond;
9545
9546 const linespec_sals &lsal = canonical.lsals[0];
9547
9548 find_condition_and_thread_for_sals (lsal.sals, extra_string,
9549 &cond, &thread, &task, &rest);
9550 cond_string_copy.reset (cond);
9551 extra_string_copy.reset (rest);
9552 }
9553 else
9554 {
9555 if (type_wanted != bp_dprintf
9556 && extra_string != NULL && *extra_string != '\0')
9557 error (_("Garbage '%s' at end of location"), extra_string);
9558
9559 /* Create a private copy of condition string. */
9560 if (cond_string)
9561 cond_string_copy.reset (xstrdup (cond_string));
9562 /* Create a private copy of any extra string. */
9563 if (extra_string)
9564 extra_string_copy.reset (xstrdup (extra_string));
9565 }
9566
9567 ops->create_breakpoints_sal (gdbarch, &canonical,
9568 std::move (cond_string_copy),
9569 std::move (extra_string_copy),
9570 type_wanted,
9571 tempflag ? disp_del : disp_donttouch,
9572 thread, task, ignore_count, ops,
9573 from_tty, enabled, internal, flags);
9574 }
9575 else
9576 {
9577 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9578
9579 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9580 b->location = copy_event_location (location);
9581
9582 if (parse_extra)
9583 b->cond_string = NULL;
9584 else
9585 {
9586 /* Create a private copy of condition string. */
9587 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
9588 b->thread = thread;
9589 }
9590
9591 /* Create a private copy of any extra string. */
9592 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
9593 b->ignore_count = ignore_count;
9594 b->disposition = tempflag ? disp_del : disp_donttouch;
9595 b->condition_not_parsed = 1;
9596 b->enable_state = enabled ? bp_enabled : bp_disabled;
9597 if ((type_wanted != bp_breakpoint
9598 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9599 b->pspace = current_program_space;
9600
9601 install_breakpoint (internal, std::move (b), 0);
9602 }
9603
9604 if (canonical.lsals.size () > 1)
9605 {
9606 warning (_("Multiple breakpoints were set.\nUse the "
9607 "\"delete\" command to delete unwanted breakpoints."));
9608 prev_breakpoint_count = prev_bkpt_count;
9609 }
9610
9611 update_global_location_list (UGLL_MAY_INSERT);
9612
9613 return 1;
9614 }
9615
9616 /* Set a breakpoint.
9617 ARG is a string describing breakpoint address,
9618 condition, and thread.
9619 FLAG specifies if a breakpoint is hardware on,
9620 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9621 and BP_TEMPFLAG. */
9622
9623 static void
9624 break_command_1 (const char *arg, int flag, int from_tty)
9625 {
9626 int tempflag = flag & BP_TEMPFLAG;
9627 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9628 ? bp_hardware_breakpoint
9629 : bp_breakpoint);
9630
9631 event_location_up location = string_to_event_location (&arg, current_language);
9632 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9633 (location.get (), false /* is_tracepoint */);
9634
9635 create_breakpoint (get_current_arch (),
9636 location.get (),
9637 NULL, 0, arg, 1 /* parse arg */,
9638 tempflag, type_wanted,
9639 0 /* Ignore count */,
9640 pending_break_support,
9641 ops,
9642 from_tty,
9643 1 /* enabled */,
9644 0 /* internal */,
9645 0);
9646 }
9647
9648 /* Helper function for break_command_1 and disassemble_command. */
9649
9650 void
9651 resolve_sal_pc (struct symtab_and_line *sal)
9652 {
9653 CORE_ADDR pc;
9654
9655 if (sal->pc == 0 && sal->symtab != NULL)
9656 {
9657 if (!find_line_pc (sal->symtab, sal->line, &pc))
9658 error (_("No line %d in file \"%s\"."),
9659 sal->line, symtab_to_filename_for_display (sal->symtab));
9660 sal->pc = pc;
9661
9662 /* If this SAL corresponds to a breakpoint inserted using a line
9663 number, then skip the function prologue if necessary. */
9664 if (sal->explicit_line)
9665 skip_prologue_sal (sal);
9666 }
9667
9668 if (sal->section == 0 && sal->symtab != NULL)
9669 {
9670 const struct blockvector *bv;
9671 const struct block *b;
9672 struct symbol *sym;
9673
9674 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9675 SYMTAB_COMPUNIT (sal->symtab));
9676 if (bv != NULL)
9677 {
9678 sym = block_linkage_function (b);
9679 if (sym != NULL)
9680 {
9681 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9682 sal->section = sym->obj_section (SYMTAB_OBJFILE (sal->symtab));
9683 }
9684 else
9685 {
9686 /* It really is worthwhile to have the section, so we'll
9687 just have to look harder. This case can be executed
9688 if we have line numbers but no functions (as can
9689 happen in assembly source). */
9690
9691 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9692 switch_to_program_space_and_thread (sal->pspace);
9693
9694 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9695 if (msym.minsym)
9696 sal->section = msym.obj_section ();
9697 }
9698 }
9699 }
9700 }
9701
9702 void
9703 break_command (const char *arg, int from_tty)
9704 {
9705 break_command_1 (arg, 0, from_tty);
9706 }
9707
9708 void
9709 tbreak_command (const char *arg, int from_tty)
9710 {
9711 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9712 }
9713
9714 static void
9715 hbreak_command (const char *arg, int from_tty)
9716 {
9717 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9718 }
9719
9720 static void
9721 thbreak_command (const char *arg, int from_tty)
9722 {
9723 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9724 }
9725
9726 static void
9727 stop_command (const char *arg, int from_tty)
9728 {
9729 printf_filtered (_("Specify the type of breakpoint to set.\n\
9730 Usage: stop in <function | address>\n\
9731 stop at <line>\n"));
9732 }
9733
9734 static void
9735 stopin_command (const char *arg, int from_tty)
9736 {
9737 int badInput = 0;
9738
9739 if (arg == NULL)
9740 badInput = 1;
9741 else if (*arg != '*')
9742 {
9743 const char *argptr = arg;
9744 int hasColon = 0;
9745
9746 /* Look for a ':'. If this is a line number specification, then
9747 say it is bad, otherwise, it should be an address or
9748 function/method name. */
9749 while (*argptr && !hasColon)
9750 {
9751 hasColon = (*argptr == ':');
9752 argptr++;
9753 }
9754
9755 if (hasColon)
9756 badInput = (*argptr != ':'); /* Not a class::method */
9757 else
9758 badInput = isdigit (*arg); /* a simple line number */
9759 }
9760
9761 if (badInput)
9762 printf_filtered (_("Usage: stop in <function | address>\n"));
9763 else
9764 break_command_1 (arg, 0, from_tty);
9765 }
9766
9767 static void
9768 stopat_command (const char *arg, int from_tty)
9769 {
9770 int badInput = 0;
9771
9772 if (arg == NULL || *arg == '*') /* no line number */
9773 badInput = 1;
9774 else
9775 {
9776 const char *argptr = arg;
9777 int hasColon = 0;
9778
9779 /* Look for a ':'. If there is a '::' then get out, otherwise
9780 it is probably a line number. */
9781 while (*argptr && !hasColon)
9782 {
9783 hasColon = (*argptr == ':');
9784 argptr++;
9785 }
9786
9787 if (hasColon)
9788 badInput = (*argptr == ':'); /* we have class::method */
9789 else
9790 badInput = !isdigit (*arg); /* not a line number */
9791 }
9792
9793 if (badInput)
9794 printf_filtered (_("Usage: stop at LINE\n"));
9795 else
9796 break_command_1 (arg, 0, from_tty);
9797 }
9798
9799 /* The dynamic printf command is mostly like a regular breakpoint, but
9800 with a prewired command list consisting of a single output command,
9801 built from extra arguments supplied on the dprintf command
9802 line. */
9803
9804 static void
9805 dprintf_command (const char *arg, int from_tty)
9806 {
9807 event_location_up location = string_to_event_location (&arg, current_language);
9808
9809 /* If non-NULL, ARG should have been advanced past the location;
9810 the next character must be ','. */
9811 if (arg != NULL)
9812 {
9813 if (arg[0] != ',' || arg[1] == '\0')
9814 error (_("Format string required"));
9815 else
9816 {
9817 /* Skip the comma. */
9818 ++arg;
9819 }
9820 }
9821
9822 create_breakpoint (get_current_arch (),
9823 location.get (),
9824 NULL, 0, arg, 1 /* parse arg */,
9825 0, bp_dprintf,
9826 0 /* Ignore count */,
9827 pending_break_support,
9828 &dprintf_breakpoint_ops,
9829 from_tty,
9830 1 /* enabled */,
9831 0 /* internal */,
9832 0);
9833 }
9834
9835 static void
9836 agent_printf_command (const char *arg, int from_tty)
9837 {
9838 error (_("May only run agent-printf on the target"));
9839 }
9840
9841 /* Implement the "breakpoint_hit" breakpoint_ops method for
9842 ranged breakpoints. */
9843
9844 static int
9845 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9846 const address_space *aspace,
9847 CORE_ADDR bp_addr,
9848 const struct target_waitstatus *ws)
9849 {
9850 if (ws->kind != TARGET_WAITKIND_STOPPED
9851 || ws->value.sig != GDB_SIGNAL_TRAP)
9852 return 0;
9853
9854 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9855 bl->length, aspace, bp_addr);
9856 }
9857
9858 /* Implement the "resources_needed" breakpoint_ops method for
9859 ranged breakpoints. */
9860
9861 static int
9862 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9863 {
9864 return target_ranged_break_num_registers ();
9865 }
9866
9867 /* Implement the "print_it" breakpoint_ops method for
9868 ranged breakpoints. */
9869
9870 static enum print_stop_action
9871 print_it_ranged_breakpoint (bpstat bs)
9872 {
9873 struct breakpoint *b = bs->breakpoint_at;
9874 struct bp_location *bl = b->loc;
9875 struct ui_out *uiout = current_uiout;
9876
9877 gdb_assert (b->type == bp_hardware_breakpoint);
9878
9879 /* Ranged breakpoints have only one location. */
9880 gdb_assert (bl && bl->next == NULL);
9881
9882 annotate_breakpoint (b->number);
9883
9884 maybe_print_thread_hit_breakpoint (uiout);
9885
9886 if (b->disposition == disp_del)
9887 uiout->text ("Temporary ranged breakpoint ");
9888 else
9889 uiout->text ("Ranged breakpoint ");
9890 if (uiout->is_mi_like_p ())
9891 {
9892 uiout->field_string ("reason",
9893 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9894 uiout->field_string ("disp", bpdisp_text (b->disposition));
9895 }
9896 uiout->field_signed ("bkptno", b->number);
9897 uiout->text (", ");
9898
9899 return PRINT_SRC_AND_LOC;
9900 }
9901
9902 /* Implement the "print_one" breakpoint_ops method for
9903 ranged breakpoints. */
9904
9905 static void
9906 print_one_ranged_breakpoint (struct breakpoint *b,
9907 struct bp_location **last_loc)
9908 {
9909 struct bp_location *bl = b->loc;
9910 struct value_print_options opts;
9911 struct ui_out *uiout = current_uiout;
9912
9913 /* Ranged breakpoints have only one location. */
9914 gdb_assert (bl && bl->next == NULL);
9915
9916 get_user_print_options (&opts);
9917
9918 if (opts.addressprint)
9919 /* We don't print the address range here, it will be printed later
9920 by print_one_detail_ranged_breakpoint. */
9921 uiout->field_skip ("addr");
9922 annotate_field (5);
9923 print_breakpoint_location (b, bl);
9924 *last_loc = bl;
9925 }
9926
9927 /* Implement the "print_one_detail" breakpoint_ops method for
9928 ranged breakpoints. */
9929
9930 static void
9931 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9932 struct ui_out *uiout)
9933 {
9934 CORE_ADDR address_start, address_end;
9935 struct bp_location *bl = b->loc;
9936 string_file stb;
9937
9938 gdb_assert (bl);
9939
9940 address_start = bl->address;
9941 address_end = address_start + bl->length - 1;
9942
9943 uiout->text ("\taddress range: ");
9944 stb.printf ("[%s, %s]",
9945 print_core_address (bl->gdbarch, address_start),
9946 print_core_address (bl->gdbarch, address_end));
9947 uiout->field_stream ("addr", stb);
9948 uiout->text ("\n");
9949 }
9950
9951 /* Implement the "print_mention" breakpoint_ops method for
9952 ranged breakpoints. */
9953
9954 static void
9955 print_mention_ranged_breakpoint (struct breakpoint *b)
9956 {
9957 struct bp_location *bl = b->loc;
9958 struct ui_out *uiout = current_uiout;
9959
9960 gdb_assert (bl);
9961 gdb_assert (b->type == bp_hardware_breakpoint);
9962
9963 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9964 b->number, paddress (bl->gdbarch, bl->address),
9965 paddress (bl->gdbarch, bl->address + bl->length - 1));
9966 }
9967
9968 /* Implement the "print_recreate" breakpoint_ops method for
9969 ranged breakpoints. */
9970
9971 static void
9972 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9973 {
9974 fprintf_unfiltered (fp, "break-range %s, %s",
9975 event_location_to_string (b->location.get ()),
9976 event_location_to_string (b->location_range_end.get ()));
9977 print_recreate_thread (b, fp);
9978 }
9979
9980 /* The breakpoint_ops structure to be used in ranged breakpoints. */
9981
9982 static struct breakpoint_ops ranged_breakpoint_ops;
9983
9984 /* Find the address where the end of the breakpoint range should be
9985 placed, given the SAL of the end of the range. This is so that if
9986 the user provides a line number, the end of the range is set to the
9987 last instruction of the given line. */
9988
9989 static CORE_ADDR
9990 find_breakpoint_range_end (struct symtab_and_line sal)
9991 {
9992 CORE_ADDR end;
9993
9994 /* If the user provided a PC value, use it. Otherwise,
9995 find the address of the end of the given location. */
9996 if (sal.explicit_pc)
9997 end = sal.pc;
9998 else
9999 {
10000 int ret;
10001 CORE_ADDR start;
10002
10003 ret = find_line_pc_range (sal, &start, &end);
10004 if (!ret)
10005 error (_("Could not find location of the end of the range."));
10006
10007 /* find_line_pc_range returns the start of the next line. */
10008 end--;
10009 }
10010
10011 return end;
10012 }
10013
10014 /* Implement the "break-range" CLI command. */
10015
10016 static void
10017 break_range_command (const char *arg, int from_tty)
10018 {
10019 const char *arg_start;
10020 struct linespec_result canonical_start, canonical_end;
10021 int bp_count, can_use_bp, length;
10022 CORE_ADDR end;
10023 struct breakpoint *b;
10024
10025 /* We don't support software ranged breakpoints. */
10026 if (target_ranged_break_num_registers () < 0)
10027 error (_("This target does not support hardware ranged breakpoints."));
10028
10029 bp_count = hw_breakpoint_used_count ();
10030 bp_count += target_ranged_break_num_registers ();
10031 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10032 bp_count, 0);
10033 if (can_use_bp < 0)
10034 error (_("Hardware breakpoints used exceeds limit."));
10035
10036 arg = skip_spaces (arg);
10037 if (arg == NULL || arg[0] == '\0')
10038 error(_("No address range specified."));
10039
10040 arg_start = arg;
10041 event_location_up start_location = string_to_event_location (&arg,
10042 current_language);
10043 parse_breakpoint_sals (start_location.get (), &canonical_start);
10044
10045 if (arg[0] != ',')
10046 error (_("Too few arguments."));
10047 else if (canonical_start.lsals.empty ())
10048 error (_("Could not find location of the beginning of the range."));
10049
10050 const linespec_sals &lsal_start = canonical_start.lsals[0];
10051
10052 if (canonical_start.lsals.size () > 1
10053 || lsal_start.sals.size () != 1)
10054 error (_("Cannot create a ranged breakpoint with multiple locations."));
10055
10056 const symtab_and_line &sal_start = lsal_start.sals[0];
10057 std::string addr_string_start (arg_start, arg - arg_start);
10058
10059 arg++; /* Skip the comma. */
10060 arg = skip_spaces (arg);
10061
10062 /* Parse the end location. */
10063
10064 arg_start = arg;
10065
10066 /* We call decode_line_full directly here instead of using
10067 parse_breakpoint_sals because we need to specify the start location's
10068 symtab and line as the default symtab and line for the end of the
10069 range. This makes it possible to have ranges like "foo.c:27, +14",
10070 where +14 means 14 lines from the start location. */
10071 event_location_up end_location = string_to_event_location (&arg,
10072 current_language);
10073 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10074 sal_start.symtab, sal_start.line,
10075 &canonical_end, NULL, NULL);
10076
10077 if (canonical_end.lsals.empty ())
10078 error (_("Could not find location of the end of the range."));
10079
10080 const linespec_sals &lsal_end = canonical_end.lsals[0];
10081 if (canonical_end.lsals.size () > 1
10082 || lsal_end.sals.size () != 1)
10083 error (_("Cannot create a ranged breakpoint with multiple locations."));
10084
10085 const symtab_and_line &sal_end = lsal_end.sals[0];
10086
10087 end = find_breakpoint_range_end (sal_end);
10088 if (sal_start.pc > end)
10089 error (_("Invalid address range, end precedes start."));
10090
10091 length = end - sal_start.pc + 1;
10092 if (length < 0)
10093 /* Length overflowed. */
10094 error (_("Address range too large."));
10095 else if (length == 1)
10096 {
10097 /* This range is simple enough to be handled by
10098 the `hbreak' command. */
10099 hbreak_command (&addr_string_start[0], 1);
10100
10101 return;
10102 }
10103
10104 /* Now set up the breakpoint. */
10105 b = set_raw_breakpoint (get_current_arch (), sal_start,
10106 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10107 set_breakpoint_count (breakpoint_count + 1);
10108 b->number = breakpoint_count;
10109 b->disposition = disp_donttouch;
10110 b->location = std::move (start_location);
10111 b->location_range_end = std::move (end_location);
10112 b->loc->length = length;
10113
10114 mention (b);
10115 gdb::observers::breakpoint_created.notify (b);
10116 update_global_location_list (UGLL_MAY_INSERT);
10117 }
10118
10119 /* Return non-zero if EXP is verified as constant. Returned zero
10120 means EXP is variable. Also the constant detection may fail for
10121 some constant expressions and in such case still falsely return
10122 zero. */
10123
10124 static bool
10125 watchpoint_exp_is_const (const struct expression *exp)
10126 {
10127 if (exp->op != nullptr)
10128 return exp->op->constant_p ();
10129
10130 int i = exp->nelts;
10131
10132 while (i > 0)
10133 {
10134 int oplenp, argsp;
10135
10136 /* We are only interested in the descriptor of each element. */
10137 operator_length (exp, i, &oplenp, &argsp);
10138 i -= oplenp;
10139
10140 switch (exp->elts[i].opcode)
10141 {
10142 case BINOP_ADD:
10143 case BINOP_SUB:
10144 case BINOP_MUL:
10145 case BINOP_DIV:
10146 case BINOP_REM:
10147 case BINOP_MOD:
10148 case BINOP_LSH:
10149 case BINOP_RSH:
10150 case BINOP_LOGICAL_AND:
10151 case BINOP_LOGICAL_OR:
10152 case BINOP_BITWISE_AND:
10153 case BINOP_BITWISE_IOR:
10154 case BINOP_BITWISE_XOR:
10155 case BINOP_EQUAL:
10156 case BINOP_NOTEQUAL:
10157 case BINOP_LESS:
10158 case BINOP_GTR:
10159 case BINOP_LEQ:
10160 case BINOP_GEQ:
10161 case BINOP_REPEAT:
10162 case BINOP_COMMA:
10163 case BINOP_EXP:
10164 case BINOP_MIN:
10165 case BINOP_MAX:
10166 case BINOP_INTDIV:
10167 case BINOP_CONCAT:
10168 case TERNOP_COND:
10169 case TERNOP_SLICE:
10170
10171 case OP_LONG:
10172 case OP_FLOAT:
10173 case OP_LAST:
10174 case OP_COMPLEX:
10175 case OP_STRING:
10176 case OP_ARRAY:
10177 case OP_TYPE:
10178 case OP_TYPEOF:
10179 case OP_DECLTYPE:
10180 case OP_TYPEID:
10181 case OP_NAME:
10182 case OP_OBJC_NSSTRING:
10183
10184 case UNOP_NEG:
10185 case UNOP_LOGICAL_NOT:
10186 case UNOP_COMPLEMENT:
10187 case UNOP_ADDR:
10188 case UNOP_HIGH:
10189 case UNOP_CAST:
10190
10191 case UNOP_CAST_TYPE:
10192 case UNOP_REINTERPRET_CAST:
10193 case UNOP_DYNAMIC_CAST:
10194 /* Unary, binary and ternary operators: We have to check
10195 their operands. If they are constant, then so is the
10196 result of that operation. For instance, if A and B are
10197 determined to be constants, then so is "A + B".
10198
10199 UNOP_IND is one exception to the rule above, because the
10200 value of *ADDR is not necessarily a constant, even when
10201 ADDR is. */
10202 break;
10203
10204 case OP_VAR_VALUE:
10205 /* Check whether the associated symbol is a constant.
10206
10207 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10208 possible that a buggy compiler could mark a variable as
10209 constant even when it is not, and TYPE_CONST would return
10210 true in this case, while SYMBOL_CLASS wouldn't.
10211
10212 We also have to check for function symbols because they
10213 are always constant. */
10214 {
10215 struct symbol *s = exp->elts[i + 2].symbol;
10216
10217 if (SYMBOL_CLASS (s) != LOC_BLOCK
10218 && SYMBOL_CLASS (s) != LOC_CONST
10219 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10220 return false;
10221 break;
10222 }
10223
10224 /* The default action is to return 0 because we are using
10225 the optimistic approach here: If we don't know something,
10226 then it is not a constant. */
10227 default:
10228 return false;
10229 }
10230 }
10231
10232 return true;
10233 }
10234
10235 /* Watchpoint destructor. */
10236
10237 watchpoint::~watchpoint ()
10238 {
10239 xfree (this->exp_string);
10240 xfree (this->exp_string_reparse);
10241 }
10242
10243 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10244
10245 static void
10246 re_set_watchpoint (struct breakpoint *b)
10247 {
10248 struct watchpoint *w = (struct watchpoint *) b;
10249
10250 /* Watchpoint can be either on expression using entirely global
10251 variables, or it can be on local variables.
10252
10253 Watchpoints of the first kind are never auto-deleted, and even
10254 persist across program restarts. Since they can use variables
10255 from shared libraries, we need to reparse expression as libraries
10256 are loaded and unloaded.
10257
10258 Watchpoints on local variables can also change meaning as result
10259 of solib event. For example, if a watchpoint uses both a local
10260 and a global variables in expression, it's a local watchpoint,
10261 but unloading of a shared library will make the expression
10262 invalid. This is not a very common use case, but we still
10263 re-evaluate expression, to avoid surprises to the user.
10264
10265 Note that for local watchpoints, we re-evaluate it only if
10266 watchpoints frame id is still valid. If it's not, it means the
10267 watchpoint is out of scope and will be deleted soon. In fact,
10268 I'm not sure we'll ever be called in this case.
10269
10270 If a local watchpoint's frame id is still valid, then
10271 w->exp_valid_block is likewise valid, and we can safely use it.
10272
10273 Don't do anything about disabled watchpoints, since they will be
10274 reevaluated again when enabled. */
10275 update_watchpoint (w, 1 /* reparse */);
10276 }
10277
10278 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10279
10280 static int
10281 insert_watchpoint (struct bp_location *bl)
10282 {
10283 struct watchpoint *w = (struct watchpoint *) bl->owner;
10284 int length = w->exact ? 1 : bl->length;
10285
10286 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10287 w->cond_exp.get ());
10288 }
10289
10290 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10291
10292 static int
10293 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10294 {
10295 struct watchpoint *w = (struct watchpoint *) bl->owner;
10296 int length = w->exact ? 1 : bl->length;
10297
10298 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10299 w->cond_exp.get ());
10300 }
10301
10302 static int
10303 breakpoint_hit_watchpoint (const struct bp_location *bl,
10304 const address_space *aspace, CORE_ADDR bp_addr,
10305 const struct target_waitstatus *ws)
10306 {
10307 struct breakpoint *b = bl->owner;
10308 struct watchpoint *w = (struct watchpoint *) b;
10309
10310 /* Continuable hardware watchpoints are treated as non-existent if the
10311 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10312 some data address). Otherwise gdb won't stop on a break instruction
10313 in the code (not from a breakpoint) when a hardware watchpoint has
10314 been defined. Also skip watchpoints which we know did not trigger
10315 (did not match the data address). */
10316 if (is_hardware_watchpoint (b)
10317 && w->watchpoint_triggered == watch_triggered_no)
10318 return 0;
10319
10320 return 1;
10321 }
10322
10323 static void
10324 check_status_watchpoint (bpstat bs)
10325 {
10326 gdb_assert (is_watchpoint (bs->breakpoint_at));
10327
10328 bpstat_check_watchpoint (bs);
10329 }
10330
10331 /* Implement the "resources_needed" breakpoint_ops method for
10332 hardware watchpoints. */
10333
10334 static int
10335 resources_needed_watchpoint (const struct bp_location *bl)
10336 {
10337 struct watchpoint *w = (struct watchpoint *) bl->owner;
10338 int length = w->exact? 1 : bl->length;
10339
10340 return target_region_ok_for_hw_watchpoint (bl->address, length);
10341 }
10342
10343 /* Implement the "works_in_software_mode" breakpoint_ops method for
10344 hardware watchpoints. */
10345
10346 static int
10347 works_in_software_mode_watchpoint (const struct breakpoint *b)
10348 {
10349 /* Read and access watchpoints only work with hardware support. */
10350 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10351 }
10352
10353 static enum print_stop_action
10354 print_it_watchpoint (bpstat bs)
10355 {
10356 struct breakpoint *b;
10357 enum print_stop_action result;
10358 struct watchpoint *w;
10359 struct ui_out *uiout = current_uiout;
10360
10361 gdb_assert (bs->bp_location_at != NULL);
10362
10363 b = bs->breakpoint_at;
10364 w = (struct watchpoint *) b;
10365
10366 annotate_watchpoint (b->number);
10367 maybe_print_thread_hit_breakpoint (uiout);
10368
10369 string_file stb;
10370
10371 gdb::optional<ui_out_emit_tuple> tuple_emitter;
10372 switch (b->type)
10373 {
10374 case bp_watchpoint:
10375 case bp_hardware_watchpoint:
10376 if (uiout->is_mi_like_p ())
10377 uiout->field_string
10378 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10379 mention (b);
10380 tuple_emitter.emplace (uiout, "value");
10381 uiout->text ("\nOld value = ");
10382 watchpoint_value_print (bs->old_val.get (), &stb);
10383 uiout->field_stream ("old", stb);
10384 uiout->text ("\nNew value = ");
10385 watchpoint_value_print (w->val.get (), &stb);
10386 uiout->field_stream ("new", stb);
10387 uiout->text ("\n");
10388 /* More than one watchpoint may have been triggered. */
10389 result = PRINT_UNKNOWN;
10390 break;
10391
10392 case bp_read_watchpoint:
10393 if (uiout->is_mi_like_p ())
10394 uiout->field_string
10395 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10396 mention (b);
10397 tuple_emitter.emplace (uiout, "value");
10398 uiout->text ("\nValue = ");
10399 watchpoint_value_print (w->val.get (), &stb);
10400 uiout->field_stream ("value", stb);
10401 uiout->text ("\n");
10402 result = PRINT_UNKNOWN;
10403 break;
10404
10405 case bp_access_watchpoint:
10406 if (bs->old_val != NULL)
10407 {
10408 if (uiout->is_mi_like_p ())
10409 uiout->field_string
10410 ("reason",
10411 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10412 mention (b);
10413 tuple_emitter.emplace (uiout, "value");
10414 uiout->text ("\nOld value = ");
10415 watchpoint_value_print (bs->old_val.get (), &stb);
10416 uiout->field_stream ("old", stb);
10417 uiout->text ("\nNew value = ");
10418 }
10419 else
10420 {
10421 mention (b);
10422 if (uiout->is_mi_like_p ())
10423 uiout->field_string
10424 ("reason",
10425 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10426 tuple_emitter.emplace (uiout, "value");
10427 uiout->text ("\nValue = ");
10428 }
10429 watchpoint_value_print (w->val.get (), &stb);
10430 uiout->field_stream ("new", stb);
10431 uiout->text ("\n");
10432 result = PRINT_UNKNOWN;
10433 break;
10434 default:
10435 result = PRINT_UNKNOWN;
10436 }
10437
10438 return result;
10439 }
10440
10441 /* Implement the "print_mention" breakpoint_ops method for hardware
10442 watchpoints. */
10443
10444 static void
10445 print_mention_watchpoint (struct breakpoint *b)
10446 {
10447 struct watchpoint *w = (struct watchpoint *) b;
10448 struct ui_out *uiout = current_uiout;
10449 const char *tuple_name;
10450
10451 switch (b->type)
10452 {
10453 case bp_watchpoint:
10454 uiout->text ("Watchpoint ");
10455 tuple_name = "wpt";
10456 break;
10457 case bp_hardware_watchpoint:
10458 uiout->text ("Hardware watchpoint ");
10459 tuple_name = "wpt";
10460 break;
10461 case bp_read_watchpoint:
10462 uiout->text ("Hardware read watchpoint ");
10463 tuple_name = "hw-rwpt";
10464 break;
10465 case bp_access_watchpoint:
10466 uiout->text ("Hardware access (read/write) watchpoint ");
10467 tuple_name = "hw-awpt";
10468 break;
10469 default:
10470 internal_error (__FILE__, __LINE__,
10471 _("Invalid hardware watchpoint type."));
10472 }
10473
10474 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10475 uiout->field_signed ("number", b->number);
10476 uiout->text (": ");
10477 uiout->field_string ("exp", w->exp_string);
10478 }
10479
10480 /* Implement the "print_recreate" breakpoint_ops method for
10481 watchpoints. */
10482
10483 static void
10484 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10485 {
10486 struct watchpoint *w = (struct watchpoint *) b;
10487
10488 switch (b->type)
10489 {
10490 case bp_watchpoint:
10491 case bp_hardware_watchpoint:
10492 fprintf_unfiltered (fp, "watch");
10493 break;
10494 case bp_read_watchpoint:
10495 fprintf_unfiltered (fp, "rwatch");
10496 break;
10497 case bp_access_watchpoint:
10498 fprintf_unfiltered (fp, "awatch");
10499 break;
10500 default:
10501 internal_error (__FILE__, __LINE__,
10502 _("Invalid watchpoint type."));
10503 }
10504
10505 fprintf_unfiltered (fp, " %s", w->exp_string);
10506 print_recreate_thread (b, fp);
10507 }
10508
10509 /* Implement the "explains_signal" breakpoint_ops method for
10510 watchpoints. */
10511
10512 static int
10513 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10514 {
10515 /* A software watchpoint cannot cause a signal other than
10516 GDB_SIGNAL_TRAP. */
10517 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10518 return 0;
10519
10520 return 1;
10521 }
10522
10523 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10524
10525 static struct breakpoint_ops watchpoint_breakpoint_ops;
10526
10527 /* Implement the "insert" breakpoint_ops method for
10528 masked hardware watchpoints. */
10529
10530 static int
10531 insert_masked_watchpoint (struct bp_location *bl)
10532 {
10533 struct watchpoint *w = (struct watchpoint *) bl->owner;
10534
10535 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10536 bl->watchpoint_type);
10537 }
10538
10539 /* Implement the "remove" breakpoint_ops method for
10540 masked hardware watchpoints. */
10541
10542 static int
10543 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10544 {
10545 struct watchpoint *w = (struct watchpoint *) bl->owner;
10546
10547 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10548 bl->watchpoint_type);
10549 }
10550
10551 /* Implement the "resources_needed" breakpoint_ops method for
10552 masked hardware watchpoints. */
10553
10554 static int
10555 resources_needed_masked_watchpoint (const struct bp_location *bl)
10556 {
10557 struct watchpoint *w = (struct watchpoint *) bl->owner;
10558
10559 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10560 }
10561
10562 /* Implement the "works_in_software_mode" breakpoint_ops method for
10563 masked hardware watchpoints. */
10564
10565 static int
10566 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10567 {
10568 return 0;
10569 }
10570
10571 /* Implement the "print_it" breakpoint_ops method for
10572 masked hardware watchpoints. */
10573
10574 static enum print_stop_action
10575 print_it_masked_watchpoint (bpstat bs)
10576 {
10577 struct breakpoint *b = bs->breakpoint_at;
10578 struct ui_out *uiout = current_uiout;
10579
10580 /* Masked watchpoints have only one location. */
10581 gdb_assert (b->loc && b->loc->next == NULL);
10582
10583 annotate_watchpoint (b->number);
10584 maybe_print_thread_hit_breakpoint (uiout);
10585
10586 switch (b->type)
10587 {
10588 case bp_hardware_watchpoint:
10589 if (uiout->is_mi_like_p ())
10590 uiout->field_string
10591 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10592 break;
10593
10594 case bp_read_watchpoint:
10595 if (uiout->is_mi_like_p ())
10596 uiout->field_string
10597 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10598 break;
10599
10600 case bp_access_watchpoint:
10601 if (uiout->is_mi_like_p ())
10602 uiout->field_string
10603 ("reason",
10604 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10605 break;
10606 default:
10607 internal_error (__FILE__, __LINE__,
10608 _("Invalid hardware watchpoint type."));
10609 }
10610
10611 mention (b);
10612 uiout->text (_("\n\
10613 Check the underlying instruction at PC for the memory\n\
10614 address and value which triggered this watchpoint.\n"));
10615 uiout->text ("\n");
10616
10617 /* More than one watchpoint may have been triggered. */
10618 return PRINT_UNKNOWN;
10619 }
10620
10621 /* Implement the "print_one_detail" breakpoint_ops method for
10622 masked hardware watchpoints. */
10623
10624 static void
10625 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10626 struct ui_out *uiout)
10627 {
10628 struct watchpoint *w = (struct watchpoint *) b;
10629
10630 /* Masked watchpoints have only one location. */
10631 gdb_assert (b->loc && b->loc->next == NULL);
10632
10633 uiout->text ("\tmask ");
10634 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10635 uiout->text ("\n");
10636 }
10637
10638 /* Implement the "print_mention" breakpoint_ops method for
10639 masked hardware watchpoints. */
10640
10641 static void
10642 print_mention_masked_watchpoint (struct breakpoint *b)
10643 {
10644 struct watchpoint *w = (struct watchpoint *) b;
10645 struct ui_out *uiout = current_uiout;
10646 const char *tuple_name;
10647
10648 switch (b->type)
10649 {
10650 case bp_hardware_watchpoint:
10651 uiout->text ("Masked hardware watchpoint ");
10652 tuple_name = "wpt";
10653 break;
10654 case bp_read_watchpoint:
10655 uiout->text ("Masked hardware read watchpoint ");
10656 tuple_name = "hw-rwpt";
10657 break;
10658 case bp_access_watchpoint:
10659 uiout->text ("Masked hardware access (read/write) watchpoint ");
10660 tuple_name = "hw-awpt";
10661 break;
10662 default:
10663 internal_error (__FILE__, __LINE__,
10664 _("Invalid hardware watchpoint type."));
10665 }
10666
10667 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10668 uiout->field_signed ("number", b->number);
10669 uiout->text (": ");
10670 uiout->field_string ("exp", w->exp_string);
10671 }
10672
10673 /* Implement the "print_recreate" breakpoint_ops method for
10674 masked hardware watchpoints. */
10675
10676 static void
10677 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10678 {
10679 struct watchpoint *w = (struct watchpoint *) b;
10680
10681 switch (b->type)
10682 {
10683 case bp_hardware_watchpoint:
10684 fprintf_unfiltered (fp, "watch");
10685 break;
10686 case bp_read_watchpoint:
10687 fprintf_unfiltered (fp, "rwatch");
10688 break;
10689 case bp_access_watchpoint:
10690 fprintf_unfiltered (fp, "awatch");
10691 break;
10692 default:
10693 internal_error (__FILE__, __LINE__,
10694 _("Invalid hardware watchpoint type."));
10695 }
10696
10697 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string,
10698 phex (w->hw_wp_mask, sizeof (CORE_ADDR)));
10699 print_recreate_thread (b, fp);
10700 }
10701
10702 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10703
10704 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10705
10706 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10707
10708 static bool
10709 is_masked_watchpoint (const struct breakpoint *b)
10710 {
10711 return b->ops == &masked_watchpoint_breakpoint_ops;
10712 }
10713
10714 /* accessflag: hw_write: watch write,
10715 hw_read: watch read,
10716 hw_access: watch access (read or write) */
10717 static void
10718 watch_command_1 (const char *arg, int accessflag, int from_tty,
10719 bool just_location, bool internal)
10720 {
10721 struct breakpoint *scope_breakpoint = NULL;
10722 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10723 struct value *result;
10724 int saved_bitpos = 0, saved_bitsize = 0;
10725 const char *exp_start = NULL;
10726 const char *exp_end = NULL;
10727 const char *tok, *end_tok;
10728 int toklen = -1;
10729 const char *cond_start = NULL;
10730 const char *cond_end = NULL;
10731 enum bptype bp_type;
10732 int thread = -1;
10733 int pc = 0;
10734 /* Flag to indicate whether we are going to use masks for
10735 the hardware watchpoint. */
10736 bool use_mask = false;
10737 CORE_ADDR mask = 0;
10738
10739 /* Make sure that we actually have parameters to parse. */
10740 if (arg != NULL && arg[0] != '\0')
10741 {
10742 const char *value_start;
10743
10744 exp_end = arg + strlen (arg);
10745
10746 /* Look for "parameter value" pairs at the end
10747 of the arguments string. */
10748 for (tok = exp_end - 1; tok > arg; tok--)
10749 {
10750 /* Skip whitespace at the end of the argument list. */
10751 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10752 tok--;
10753
10754 /* Find the beginning of the last token.
10755 This is the value of the parameter. */
10756 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10757 tok--;
10758 value_start = tok + 1;
10759
10760 /* Skip whitespace. */
10761 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10762 tok--;
10763
10764 end_tok = tok;
10765
10766 /* Find the beginning of the second to last token.
10767 This is the parameter itself. */
10768 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10769 tok--;
10770 tok++;
10771 toklen = end_tok - tok + 1;
10772
10773 if (toklen == 6 && startswith (tok, "thread"))
10774 {
10775 struct thread_info *thr;
10776 /* At this point we've found a "thread" token, which means
10777 the user is trying to set a watchpoint that triggers
10778 only in a specific thread. */
10779 const char *endp;
10780
10781 if (thread != -1)
10782 error(_("You can specify only one thread."));
10783
10784 /* Extract the thread ID from the next token. */
10785 thr = parse_thread_id (value_start, &endp);
10786
10787 /* Check if the user provided a valid thread ID. */
10788 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10789 invalid_thread_id_error (value_start);
10790
10791 thread = thr->global_num;
10792 }
10793 else if (toklen == 4 && startswith (tok, "mask"))
10794 {
10795 /* We've found a "mask" token, which means the user wants to
10796 create a hardware watchpoint that is going to have the mask
10797 facility. */
10798 struct value *mask_value, *mark;
10799
10800 if (use_mask)
10801 error(_("You can specify only one mask."));
10802
10803 use_mask = just_location = true;
10804
10805 mark = value_mark ();
10806 mask_value = parse_to_comma_and_eval (&value_start);
10807 mask = value_as_address (mask_value);
10808 value_free_to_mark (mark);
10809 }
10810 else
10811 /* We didn't recognize what we found. We should stop here. */
10812 break;
10813
10814 /* Truncate the string and get rid of the "parameter value" pair before
10815 the arguments string is parsed by the parse_exp_1 function. */
10816 exp_end = tok;
10817 }
10818 }
10819 else
10820 exp_end = arg;
10821
10822 /* Parse the rest of the arguments. From here on out, everything
10823 is in terms of a newly allocated string instead of the original
10824 ARG. */
10825 std::string expression (arg, exp_end - arg);
10826 exp_start = arg = expression.c_str ();
10827 innermost_block_tracker tracker;
10828 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10829 exp_end = arg;
10830 /* Remove trailing whitespace from the expression before saving it.
10831 This makes the eventual display of the expression string a bit
10832 prettier. */
10833 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10834 --exp_end;
10835
10836 /* Checking if the expression is not constant. */
10837 if (watchpoint_exp_is_const (exp.get ()))
10838 {
10839 int len;
10840
10841 len = exp_end - exp_start;
10842 while (len > 0 && isspace (exp_start[len - 1]))
10843 len--;
10844 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10845 }
10846
10847 exp_valid_block = tracker.block ();
10848 struct value *mark = value_mark ();
10849 struct value *val_as_value = nullptr;
10850 fetch_subexp_value (exp.get (), &pc, exp->op.get (), &val_as_value, &result,
10851 NULL, just_location);
10852
10853 if (val_as_value != NULL && just_location)
10854 {
10855 saved_bitpos = value_bitpos (val_as_value);
10856 saved_bitsize = value_bitsize (val_as_value);
10857 }
10858
10859 value_ref_ptr val;
10860 if (just_location)
10861 {
10862 int ret;
10863
10864 exp_valid_block = NULL;
10865 val = release_value (value_addr (result));
10866 value_free_to_mark (mark);
10867
10868 if (use_mask)
10869 {
10870 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10871 mask);
10872 if (ret == -1)
10873 error (_("This target does not support masked watchpoints."));
10874 else if (ret == -2)
10875 error (_("Invalid mask or memory region."));
10876 }
10877 }
10878 else if (val_as_value != NULL)
10879 val = release_value (val_as_value);
10880
10881 tok = skip_spaces (arg);
10882 end_tok = skip_to_space (tok);
10883
10884 toklen = end_tok - tok;
10885 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10886 {
10887 tok = cond_start = end_tok + 1;
10888 innermost_block_tracker if_tracker;
10889 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10890
10891 /* The watchpoint expression may not be local, but the condition
10892 may still be. E.g.: `watch global if local > 0'. */
10893 cond_exp_valid_block = if_tracker.block ();
10894
10895 cond_end = tok;
10896 }
10897 if (*tok)
10898 error (_("Junk at end of command."));
10899
10900 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10901
10902 /* Save this because create_internal_breakpoint below invalidates
10903 'wp_frame'. */
10904 frame_id watchpoint_frame = get_frame_id (wp_frame);
10905
10906 /* If the expression is "local", then set up a "watchpoint scope"
10907 breakpoint at the point where we've left the scope of the watchpoint
10908 expression. Create the scope breakpoint before the watchpoint, so
10909 that we will encounter it first in bpstat_stop_status. */
10910 if (exp_valid_block != NULL && wp_frame != NULL)
10911 {
10912 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10913
10914 if (frame_id_p (caller_frame_id))
10915 {
10916 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10917 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10918
10919 scope_breakpoint
10920 = create_internal_breakpoint (caller_arch, caller_pc,
10921 bp_watchpoint_scope,
10922 &momentary_breakpoint_ops);
10923
10924 /* create_internal_breakpoint could invalidate WP_FRAME. */
10925 wp_frame = NULL;
10926
10927 scope_breakpoint->enable_state = bp_enabled;
10928
10929 /* Automatically delete the breakpoint when it hits. */
10930 scope_breakpoint->disposition = disp_del;
10931
10932 /* Only break in the proper frame (help with recursion). */
10933 scope_breakpoint->frame_id = caller_frame_id;
10934
10935 /* Set the address at which we will stop. */
10936 scope_breakpoint->loc->gdbarch = caller_arch;
10937 scope_breakpoint->loc->requested_address = caller_pc;
10938 scope_breakpoint->loc->address
10939 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10940 scope_breakpoint->loc->requested_address,
10941 scope_breakpoint->type);
10942 }
10943 }
10944
10945 /* Now set up the breakpoint. We create all watchpoints as hardware
10946 watchpoints here even if hardware watchpoints are turned off, a call
10947 to update_watchpoint later in this function will cause the type to
10948 drop back to bp_watchpoint (software watchpoint) if required. */
10949
10950 if (accessflag == hw_read)
10951 bp_type = bp_read_watchpoint;
10952 else if (accessflag == hw_access)
10953 bp_type = bp_access_watchpoint;
10954 else
10955 bp_type = bp_hardware_watchpoint;
10956
10957 std::unique_ptr<watchpoint> w (new watchpoint ());
10958
10959 if (use_mask)
10960 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10961 &masked_watchpoint_breakpoint_ops);
10962 else
10963 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10964 &watchpoint_breakpoint_ops);
10965 w->thread = thread;
10966 w->disposition = disp_donttouch;
10967 w->pspace = current_program_space;
10968 w->exp = std::move (exp);
10969 w->exp_valid_block = exp_valid_block;
10970 w->cond_exp_valid_block = cond_exp_valid_block;
10971 if (just_location)
10972 {
10973 struct type *t = value_type (val.get ());
10974 CORE_ADDR addr = value_as_address (val.get ());
10975
10976 w->exp_string_reparse
10977 = current_language->watch_location_expression (t, addr).release ();
10978
10979 w->exp_string = xstrprintf ("-location %.*s",
10980 (int) (exp_end - exp_start), exp_start);
10981 }
10982 else
10983 w->exp_string = savestring (exp_start, exp_end - exp_start);
10984
10985 if (use_mask)
10986 {
10987 w->hw_wp_mask = mask;
10988 }
10989 else
10990 {
10991 w->val = val;
10992 w->val_bitpos = saved_bitpos;
10993 w->val_bitsize = saved_bitsize;
10994 w->val_valid = true;
10995 }
10996
10997 if (cond_start)
10998 w->cond_string = savestring (cond_start, cond_end - cond_start);
10999 else
11000 w->cond_string = 0;
11001
11002 if (frame_id_p (watchpoint_frame))
11003 {
11004 w->watchpoint_frame = watchpoint_frame;
11005 w->watchpoint_thread = inferior_ptid;
11006 }
11007 else
11008 {
11009 w->watchpoint_frame = null_frame_id;
11010 w->watchpoint_thread = null_ptid;
11011 }
11012
11013 if (scope_breakpoint != NULL)
11014 {
11015 /* The scope breakpoint is related to the watchpoint. We will
11016 need to act on them together. */
11017 w->related_breakpoint = scope_breakpoint;
11018 scope_breakpoint->related_breakpoint = w.get ();
11019 }
11020
11021 if (!just_location)
11022 value_free_to_mark (mark);
11023
11024 /* Finally update the new watchpoint. This creates the locations
11025 that should be inserted. */
11026 update_watchpoint (w.get (), 1);
11027
11028 install_breakpoint (internal, std::move (w), 1);
11029 }
11030
11031 /* Return count of debug registers needed to watch the given expression.
11032 If the watchpoint cannot be handled in hardware return zero. */
11033
11034 static int
11035 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
11036 {
11037 int found_memory_cnt = 0;
11038
11039 /* Did the user specifically forbid us to use hardware watchpoints? */
11040 if (!can_use_hw_watchpoints)
11041 return 0;
11042
11043 gdb_assert (!vals.empty ());
11044 struct value *head = vals[0].get ();
11045
11046 /* Make sure that the value of the expression depends only upon
11047 memory contents, and values computed from them within GDB. If we
11048 find any register references or function calls, we can't use a
11049 hardware watchpoint.
11050
11051 The idea here is that evaluating an expression generates a series
11052 of values, one holding the value of every subexpression. (The
11053 expression a*b+c has five subexpressions: a, b, a*b, c, and
11054 a*b+c.) GDB's values hold almost enough information to establish
11055 the criteria given above --- they identify memory lvalues,
11056 register lvalues, computed values, etcetera. So we can evaluate
11057 the expression, and then scan the chain of values that leaves
11058 behind to decide whether we can detect any possible change to the
11059 expression's final value using only hardware watchpoints.
11060
11061 However, I don't think that the values returned by inferior
11062 function calls are special in any way. So this function may not
11063 notice that an expression involving an inferior function call
11064 can't be watched with hardware watchpoints. FIXME. */
11065 for (const value_ref_ptr &iter : vals)
11066 {
11067 struct value *v = iter.get ();
11068
11069 if (VALUE_LVAL (v) == lval_memory)
11070 {
11071 if (v != head && value_lazy (v))
11072 /* A lazy memory lvalue in the chain is one that GDB never
11073 needed to fetch; we either just used its address (e.g.,
11074 `a' in `a.b') or we never needed it at all (e.g., `a'
11075 in `a,b'). This doesn't apply to HEAD; if that is
11076 lazy then it was not readable, but watch it anyway. */
11077 ;
11078 else
11079 {
11080 /* Ahh, memory we actually used! Check if we can cover
11081 it with hardware watchpoints. */
11082 struct type *vtype = check_typedef (value_type (v));
11083
11084 /* We only watch structs and arrays if user asked for it
11085 explicitly, never if they just happen to appear in a
11086 middle of some value chain. */
11087 if (v == head
11088 || (vtype->code () != TYPE_CODE_STRUCT
11089 && vtype->code () != TYPE_CODE_ARRAY))
11090 {
11091 CORE_ADDR vaddr = value_address (v);
11092 int len;
11093 int num_regs;
11094
11095 len = (target_exact_watchpoints
11096 && is_scalar_type_recursive (vtype))?
11097 1 : TYPE_LENGTH (value_type (v));
11098
11099 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11100 if (!num_regs)
11101 return 0;
11102 else
11103 found_memory_cnt += num_regs;
11104 }
11105 }
11106 }
11107 else if (VALUE_LVAL (v) != not_lval
11108 && deprecated_value_modifiable (v) == 0)
11109 return 0; /* These are values from the history (e.g., $1). */
11110 else if (VALUE_LVAL (v) == lval_register)
11111 return 0; /* Cannot watch a register with a HW watchpoint. */
11112 }
11113
11114 /* The expression itself looks suitable for using a hardware
11115 watchpoint, but give the target machine a chance to reject it. */
11116 return found_memory_cnt;
11117 }
11118
11119 void
11120 watch_command_wrapper (const char *arg, int from_tty, bool internal)
11121 {
11122 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11123 }
11124
11125 /* Options for the watch, awatch, and rwatch commands. */
11126
11127 struct watch_options
11128 {
11129 /* For -location. */
11130 bool location = false;
11131 };
11132
11133 /* Definitions of options for the "watch", "awatch", and "rwatch" commands.
11134
11135 Historically GDB always accepted both '-location' and '-l' flags for
11136 these commands (both flags being synonyms). When converting to the
11137 newer option scheme only '-location' is added here. That's fine (for
11138 backward compatibility) as any non-ambiguous prefix of a flag will be
11139 accepted, so '-l', '-loc', are now all accepted.
11140
11141 What this means is that, if in the future, we add any new flag here
11142 that starts with '-l' then this will break backward compatibility, so
11143 please, don't do that! */
11144
11145 static const gdb::option::option_def watch_option_defs[] = {
11146 gdb::option::flag_option_def<watch_options> {
11147 "location",
11148 [] (watch_options *opt) { return &opt->location; },
11149 N_("\
11150 This evaluates EXPRESSION and watches the memory to which is refers.\n\
11151 -l can be used as a short form of -location."),
11152 },
11153 };
11154
11155 /* Returns the option group used by 'watch', 'awatch', and 'rwatch'
11156 commands. */
11157
11158 static gdb::option::option_def_group
11159 make_watch_options_def_group (watch_options *opts)
11160 {
11161 return {{watch_option_defs}, opts};
11162 }
11163
11164 /* A helper function that looks for the "-location" argument and then
11165 calls watch_command_1. */
11166
11167 static void
11168 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
11169 {
11170 watch_options opts;
11171 auto grp = make_watch_options_def_group (&opts);
11172 gdb::option::process_options
11173 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
11174 if (arg != nullptr && *arg == '\0')
11175 arg = nullptr;
11176
11177 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
11178 }
11179
11180 /* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
11181 static void
11182 watch_command_completer (struct cmd_list_element *ignore,
11183 completion_tracker &tracker,
11184 const char *text, const char * /*word*/)
11185 {
11186 const auto group = make_watch_options_def_group (nullptr);
11187 if (gdb::option::complete_options
11188 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
11189 return;
11190
11191 const char *word = advance_to_expression_complete_word_point (tracker, text);
11192 expression_completer (ignore, tracker, text, word);
11193 }
11194
11195 static void
11196 watch_command (const char *arg, int from_tty)
11197 {
11198 watch_maybe_just_location (arg, hw_write, from_tty);
11199 }
11200
11201 void
11202 rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
11203 {
11204 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11205 }
11206
11207 static void
11208 rwatch_command (const char *arg, int from_tty)
11209 {
11210 watch_maybe_just_location (arg, hw_read, from_tty);
11211 }
11212
11213 void
11214 awatch_command_wrapper (const char *arg, int from_tty, bool internal)
11215 {
11216 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11217 }
11218
11219 static void
11220 awatch_command (const char *arg, int from_tty)
11221 {
11222 watch_maybe_just_location (arg, hw_access, from_tty);
11223 }
11224 \f
11225
11226 /* Data for the FSM that manages the until(location)/advance commands
11227 in infcmd.c. Here because it uses the mechanisms of
11228 breakpoints. */
11229
11230 struct until_break_fsm : public thread_fsm
11231 {
11232 /* The thread that was current when the command was executed. */
11233 int thread;
11234
11235 /* The breakpoint set at the return address in the caller frame,
11236 plus breakpoints at all the destination locations. */
11237 std::vector<breakpoint_up> breakpoints;
11238
11239 until_break_fsm (struct interp *cmd_interp, int thread,
11240 std::vector<breakpoint_up> &&breakpoints)
11241 : thread_fsm (cmd_interp),
11242 thread (thread),
11243 breakpoints (std::move (breakpoints))
11244 {
11245 }
11246
11247 void clean_up (struct thread_info *thread) override;
11248 bool should_stop (struct thread_info *thread) override;
11249 enum async_reply_reason do_async_reply_reason () override;
11250 };
11251
11252 /* Implementation of the 'should_stop' FSM method for the
11253 until(location)/advance commands. */
11254
11255 bool
11256 until_break_fsm::should_stop (struct thread_info *tp)
11257 {
11258 for (const breakpoint_up &bp : breakpoints)
11259 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11260 bp.get ()) != NULL)
11261 {
11262 set_finished ();
11263 break;
11264 }
11265
11266 return true;
11267 }
11268
11269 /* Implementation of the 'clean_up' FSM method for the
11270 until(location)/advance commands. */
11271
11272 void
11273 until_break_fsm::clean_up (struct thread_info *)
11274 {
11275 /* Clean up our temporary breakpoints. */
11276 breakpoints.clear ();
11277 delete_longjmp_breakpoint (thread);
11278 }
11279
11280 /* Implementation of the 'async_reply_reason' FSM method for the
11281 until(location)/advance commands. */
11282
11283 enum async_reply_reason
11284 until_break_fsm::do_async_reply_reason ()
11285 {
11286 return EXEC_ASYNC_LOCATION_REACHED;
11287 }
11288
11289 void
11290 until_break_command (const char *arg, int from_tty, int anywhere)
11291 {
11292 struct frame_info *frame;
11293 struct gdbarch *frame_gdbarch;
11294 struct frame_id stack_frame_id;
11295 struct frame_id caller_frame_id;
11296 int thread;
11297 struct thread_info *tp;
11298
11299 clear_proceed_status (0);
11300
11301 /* Set a breakpoint where the user wants it and at return from
11302 this function. */
11303
11304 event_location_up location = string_to_event_location (&arg, current_language);
11305
11306 std::vector<symtab_and_line> sals
11307 = (last_displayed_sal_is_valid ()
11308 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11309 get_last_displayed_symtab (),
11310 get_last_displayed_line ())
11311 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11312 NULL, NULL, 0));
11313
11314 if (sals.empty ())
11315 error (_("Couldn't get information on specified line."));
11316
11317 if (*arg)
11318 error (_("Junk at end of arguments."));
11319
11320 tp = inferior_thread ();
11321 thread = tp->global_num;
11322
11323 /* Note linespec handling above invalidates the frame chain.
11324 Installing a breakpoint also invalidates the frame chain (as it
11325 may need to switch threads), so do any frame handling before
11326 that. */
11327
11328 frame = get_selected_frame (NULL);
11329 frame_gdbarch = get_frame_arch (frame);
11330 stack_frame_id = get_stack_frame_id (frame);
11331 caller_frame_id = frame_unwind_caller_id (frame);
11332
11333 /* Keep within the current frame, or in frames called by the current
11334 one. */
11335
11336 std::vector<breakpoint_up> breakpoints;
11337
11338 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11339
11340 if (frame_id_p (caller_frame_id))
11341 {
11342 struct symtab_and_line sal2;
11343 struct gdbarch *caller_gdbarch;
11344
11345 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11346 sal2.pc = frame_unwind_caller_pc (frame);
11347 caller_gdbarch = frame_unwind_caller_arch (frame);
11348
11349 breakpoint_up caller_breakpoint
11350 = set_momentary_breakpoint (caller_gdbarch, sal2,
11351 caller_frame_id, bp_until);
11352 breakpoints.emplace_back (std::move (caller_breakpoint));
11353
11354 set_longjmp_breakpoint (tp, caller_frame_id);
11355 lj_deleter.emplace (thread);
11356 }
11357
11358 /* set_momentary_breakpoint could invalidate FRAME. */
11359 frame = NULL;
11360
11361 /* If the user told us to continue until a specified location, we
11362 don't specify a frame at which we need to stop. Otherwise,
11363 specify the selected frame, because we want to stop only at the
11364 very same frame. */
11365 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
11366
11367 for (symtab_and_line &sal : sals)
11368 {
11369 resolve_sal_pc (&sal);
11370
11371 breakpoint_up location_breakpoint
11372 = set_momentary_breakpoint (frame_gdbarch, sal,
11373 stop_frame_id, bp_until);
11374 breakpoints.emplace_back (std::move (location_breakpoint));
11375 }
11376
11377 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
11378 std::move (breakpoints));
11379
11380 if (lj_deleter)
11381 lj_deleter->release ();
11382
11383 proceed (-1, GDB_SIGNAL_DEFAULT);
11384 }
11385
11386 /* This function attempts to parse an optional "if <cond>" clause
11387 from the arg string. If one is not found, it returns NULL.
11388
11389 Else, it returns a pointer to the condition string. (It does not
11390 attempt to evaluate the string against a particular block.) And,
11391 it updates arg to point to the first character following the parsed
11392 if clause in the arg string. */
11393
11394 const char *
11395 ep_parse_optional_if_clause (const char **arg)
11396 {
11397 const char *cond_string;
11398
11399 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11400 return NULL;
11401
11402 /* Skip the "if" keyword. */
11403 (*arg) += 2;
11404
11405 /* Skip any extra leading whitespace, and record the start of the
11406 condition string. */
11407 *arg = skip_spaces (*arg);
11408 cond_string = *arg;
11409
11410 /* Assume that the condition occupies the remainder of the arg
11411 string. */
11412 (*arg) += strlen (cond_string);
11413
11414 return cond_string;
11415 }
11416
11417 /* Commands to deal with catching events, such as signals, exceptions,
11418 process start/exit, etc. */
11419
11420 typedef enum
11421 {
11422 catch_fork_temporary, catch_vfork_temporary,
11423 catch_fork_permanent, catch_vfork_permanent
11424 }
11425 catch_fork_kind;
11426
11427 static void
11428 catch_fork_command_1 (const char *arg, int from_tty,
11429 struct cmd_list_element *command)
11430 {
11431 struct gdbarch *gdbarch = get_current_arch ();
11432 const char *cond_string = NULL;
11433 catch_fork_kind fork_kind;
11434
11435 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11436 bool temp = (fork_kind == catch_fork_temporary
11437 || fork_kind == catch_vfork_temporary);
11438
11439 if (!arg)
11440 arg = "";
11441 arg = skip_spaces (arg);
11442
11443 /* The allowed syntax is:
11444 catch [v]fork
11445 catch [v]fork if <cond>
11446
11447 First, check if there's an if clause. */
11448 cond_string = ep_parse_optional_if_clause (&arg);
11449
11450 if ((*arg != '\0') && !isspace (*arg))
11451 error (_("Junk at end of arguments."));
11452
11453 /* If this target supports it, create a fork or vfork catchpoint
11454 and enable reporting of such events. */
11455 switch (fork_kind)
11456 {
11457 case catch_fork_temporary:
11458 case catch_fork_permanent:
11459 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
11460 &catch_fork_breakpoint_ops);
11461 break;
11462 case catch_vfork_temporary:
11463 case catch_vfork_permanent:
11464 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
11465 &catch_vfork_breakpoint_ops);
11466 break;
11467 default:
11468 error (_("unsupported or unknown fork kind; cannot catch it"));
11469 break;
11470 }
11471 }
11472
11473 static void
11474 catch_exec_command_1 (const char *arg, int from_tty,
11475 struct cmd_list_element *command)
11476 {
11477 struct gdbarch *gdbarch = get_current_arch ();
11478 const char *cond_string = NULL;
11479 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
11480
11481 if (!arg)
11482 arg = "";
11483 arg = skip_spaces (arg);
11484
11485 /* The allowed syntax is:
11486 catch exec
11487 catch exec if <cond>
11488
11489 First, check if there's an if clause. */
11490 cond_string = ep_parse_optional_if_clause (&arg);
11491
11492 if ((*arg != '\0') && !isspace (*arg))
11493 error (_("Junk at end of arguments."));
11494
11495 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11496 init_catchpoint (c.get (), gdbarch, temp, cond_string,
11497 &catch_exec_breakpoint_ops);
11498 c->exec_pathname = NULL;
11499
11500 install_breakpoint (0, std::move (c), 1);
11501 }
11502
11503 void
11504 init_ada_exception_breakpoint (struct breakpoint *b,
11505 struct gdbarch *gdbarch,
11506 struct symtab_and_line sal,
11507 const char *addr_string,
11508 const struct breakpoint_ops *ops,
11509 int tempflag,
11510 int enabled,
11511 int from_tty)
11512 {
11513 if (from_tty)
11514 {
11515 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11516 if (!loc_gdbarch)
11517 loc_gdbarch = gdbarch;
11518
11519 describe_other_breakpoints (loc_gdbarch,
11520 sal.pspace, sal.pc, sal.section, -1);
11521 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11522 version for exception catchpoints, because two catchpoints
11523 used for different exception names will use the same address.
11524 In this case, a "breakpoint ... also set at..." warning is
11525 unproductive. Besides, the warning phrasing is also a bit
11526 inappropriate, we should use the word catchpoint, and tell
11527 the user what type of catchpoint it is. The above is good
11528 enough for now, though. */
11529 }
11530
11531 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
11532
11533 b->enable_state = enabled ? bp_enabled : bp_disabled;
11534 b->disposition = tempflag ? disp_del : disp_donttouch;
11535 b->location = string_to_event_location (&addr_string,
11536 language_def (language_ada));
11537 b->language = language_ada;
11538 }
11539
11540 \f
11541
11542 /* Compare two breakpoints and return a strcmp-like result. */
11543
11544 static int
11545 compare_breakpoints (const breakpoint *a, const breakpoint *b)
11546 {
11547 uintptr_t ua = (uintptr_t) a;
11548 uintptr_t ub = (uintptr_t) b;
11549
11550 if (a->number < b->number)
11551 return -1;
11552 else if (a->number > b->number)
11553 return 1;
11554
11555 /* Now sort by address, in case we see, e..g, two breakpoints with
11556 the number 0. */
11557 if (ua < ub)
11558 return -1;
11559 return ua > ub ? 1 : 0;
11560 }
11561
11562 /* Delete breakpoints by address or line. */
11563
11564 static void
11565 clear_command (const char *arg, int from_tty)
11566 {
11567 struct breakpoint *b;
11568 int default_match;
11569
11570 std::vector<symtab_and_line> decoded_sals;
11571 symtab_and_line last_sal;
11572 gdb::array_view<symtab_and_line> sals;
11573 if (arg)
11574 {
11575 decoded_sals
11576 = decode_line_with_current_source (arg,
11577 (DECODE_LINE_FUNFIRSTLINE
11578 | DECODE_LINE_LIST_MODE));
11579 default_match = 0;
11580 sals = decoded_sals;
11581 }
11582 else
11583 {
11584 /* Set sal's line, symtab, pc, and pspace to the values
11585 corresponding to the last call to print_frame_info. If the
11586 codepoint is not valid, this will set all the fields to 0. */
11587 last_sal = get_last_displayed_sal ();
11588 if (last_sal.symtab == 0)
11589 error (_("No source file specified."));
11590
11591 default_match = 1;
11592 sals = last_sal;
11593 }
11594
11595 /* We don't call resolve_sal_pc here. That's not as bad as it
11596 seems, because all existing breakpoints typically have both
11597 file/line and pc set. So, if clear is given file/line, we can
11598 match this to existing breakpoint without obtaining pc at all.
11599
11600 We only support clearing given the address explicitly
11601 present in breakpoint table. Say, we've set breakpoint
11602 at file:line. There were several PC values for that file:line,
11603 due to optimization, all in one block.
11604
11605 We've picked one PC value. If "clear" is issued with another
11606 PC corresponding to the same file:line, the breakpoint won't
11607 be cleared. We probably can still clear the breakpoint, but
11608 since the other PC value is never presented to user, user
11609 can only find it by guessing, and it does not seem important
11610 to support that. */
11611
11612 /* For each line spec given, delete bps which correspond to it. Do
11613 it in two passes, solely to preserve the current behavior that
11614 from_tty is forced true if we delete more than one
11615 breakpoint. */
11616
11617 std::vector<struct breakpoint *> found;
11618 for (const auto &sal : sals)
11619 {
11620 const char *sal_fullname;
11621
11622 /* If exact pc given, clear bpts at that pc.
11623 If line given (pc == 0), clear all bpts on specified line.
11624 If defaulting, clear all bpts on default line
11625 or at default pc.
11626
11627 defaulting sal.pc != 0 tests to do
11628
11629 0 1 pc
11630 1 1 pc _and_ line
11631 0 0 line
11632 1 0 <can't happen> */
11633
11634 sal_fullname = (sal.symtab == NULL
11635 ? NULL : symtab_to_fullname (sal.symtab));
11636
11637 /* Find all matching breakpoints and add them to 'found'. */
11638 ALL_BREAKPOINTS (b)
11639 {
11640 int match = 0;
11641 /* Are we going to delete b? */
11642 if (b->type != bp_none && !is_watchpoint (b))
11643 {
11644 struct bp_location *loc = b->loc;
11645 for (; loc; loc = loc->next)
11646 {
11647 /* If the user specified file:line, don't allow a PC
11648 match. This matches historical gdb behavior. */
11649 int pc_match = (!sal.explicit_line
11650 && sal.pc
11651 && (loc->pspace == sal.pspace)
11652 && (loc->address == sal.pc)
11653 && (!section_is_overlay (loc->section)
11654 || loc->section == sal.section));
11655 int line_match = 0;
11656
11657 if ((default_match || sal.explicit_line)
11658 && loc->symtab != NULL
11659 && sal_fullname != NULL
11660 && sal.pspace == loc->pspace
11661 && loc->line_number == sal.line
11662 && filename_cmp (symtab_to_fullname (loc->symtab),
11663 sal_fullname) == 0)
11664 line_match = 1;
11665
11666 if (pc_match || line_match)
11667 {
11668 match = 1;
11669 break;
11670 }
11671 }
11672 }
11673
11674 if (match)
11675 found.push_back (b);
11676 }
11677 }
11678
11679 /* Now go thru the 'found' chain and delete them. */
11680 if (found.empty ())
11681 {
11682 if (arg)
11683 error (_("No breakpoint at %s."), arg);
11684 else
11685 error (_("No breakpoint at this line."));
11686 }
11687
11688 /* Remove duplicates from the vec. */
11689 std::sort (found.begin (), found.end (),
11690 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11691 {
11692 return compare_breakpoints (bp_a, bp_b) < 0;
11693 });
11694 found.erase (std::unique (found.begin (), found.end (),
11695 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11696 {
11697 return compare_breakpoints (bp_a, bp_b) == 0;
11698 }),
11699 found.end ());
11700
11701 if (found.size () > 1)
11702 from_tty = 1; /* Always report if deleted more than one. */
11703 if (from_tty)
11704 {
11705 if (found.size () == 1)
11706 printf_unfiltered (_("Deleted breakpoint "));
11707 else
11708 printf_unfiltered (_("Deleted breakpoints "));
11709 }
11710
11711 for (breakpoint *iter : found)
11712 {
11713 if (from_tty)
11714 printf_unfiltered ("%d ", iter->number);
11715 delete_breakpoint (iter);
11716 }
11717 if (from_tty)
11718 putchar_unfiltered ('\n');
11719 }
11720 \f
11721 /* Delete breakpoint in BS if they are `delete' breakpoints and
11722 all breakpoints that are marked for deletion, whether hit or not.
11723 This is called after any breakpoint is hit, or after errors. */
11724
11725 void
11726 breakpoint_auto_delete (bpstat bs)
11727 {
11728 struct breakpoint *b, *b_tmp;
11729
11730 for (; bs; bs = bs->next)
11731 if (bs->breakpoint_at
11732 && bs->breakpoint_at->disposition == disp_del
11733 && bs->stop)
11734 delete_breakpoint (bs->breakpoint_at);
11735
11736 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11737 {
11738 if (b->disposition == disp_del_at_next_stop)
11739 delete_breakpoint (b);
11740 }
11741 }
11742
11743 /* A comparison function for bp_location AP and BP being interfaced to
11744 std::sort. Sort elements primarily by their ADDRESS (no matter what
11745 bl_address_is_meaningful says), secondarily by ordering first
11746 permanent elements and terciarily just ensuring the array is sorted
11747 stable way despite std::sort being an unstable algorithm. */
11748
11749 static int
11750 bp_location_is_less_than (const bp_location *a, const bp_location *b)
11751 {
11752 if (a->address != b->address)
11753 return a->address < b->address;
11754
11755 /* Sort locations at the same address by their pspace number, keeping
11756 locations of the same inferior (in a multi-inferior environment)
11757 grouped. */
11758
11759 if (a->pspace->num != b->pspace->num)
11760 return a->pspace->num < b->pspace->num;
11761
11762 /* Sort permanent breakpoints first. */
11763 if (a->permanent != b->permanent)
11764 return a->permanent > b->permanent;
11765
11766 /* Sort by type in order to make duplicate determination easier.
11767 See update_global_location_list. This is kept in sync with
11768 breakpoint_locations_match. */
11769 if (a->loc_type < b->loc_type)
11770 return true;
11771
11772 /* Likewise, for range-breakpoints, sort by length. */
11773 if (a->loc_type == bp_loc_hardware_breakpoint
11774 && b->loc_type == bp_loc_hardware_breakpoint
11775 && a->length < b->length)
11776 return true;
11777
11778 /* Make the internal GDB representation stable across GDB runs
11779 where A and B memory inside GDB can differ. Breakpoint locations of
11780 the same type at the same address can be sorted in arbitrary order. */
11781
11782 if (a->owner->number != b->owner->number)
11783 return a->owner->number < b->owner->number;
11784
11785 return a < b;
11786 }
11787
11788 /* Set bp_locations_placed_address_before_address_max and
11789 bp_locations_shadow_len_after_address_max according to the current
11790 content of the bp_locations array. */
11791
11792 static void
11793 bp_locations_target_extensions_update (void)
11794 {
11795 struct bp_location *bl, **blp_tmp;
11796
11797 bp_locations_placed_address_before_address_max = 0;
11798 bp_locations_shadow_len_after_address_max = 0;
11799
11800 ALL_BP_LOCATIONS (bl, blp_tmp)
11801 {
11802 CORE_ADDR start, end, addr;
11803
11804 if (!bp_location_has_shadow (bl))
11805 continue;
11806
11807 start = bl->target_info.placed_address;
11808 end = start + bl->target_info.shadow_len;
11809
11810 gdb_assert (bl->address >= start);
11811 addr = bl->address - start;
11812 if (addr > bp_locations_placed_address_before_address_max)
11813 bp_locations_placed_address_before_address_max = addr;
11814
11815 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11816
11817 gdb_assert (bl->address < end);
11818 addr = end - bl->address;
11819 if (addr > bp_locations_shadow_len_after_address_max)
11820 bp_locations_shadow_len_after_address_max = addr;
11821 }
11822 }
11823
11824 /* Download tracepoint locations if they haven't been. */
11825
11826 static void
11827 download_tracepoint_locations (void)
11828 {
11829 struct breakpoint *b;
11830 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11831
11832 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11833
11834 ALL_TRACEPOINTS (b)
11835 {
11836 struct bp_location *bl;
11837 struct tracepoint *t;
11838 int bp_location_downloaded = 0;
11839
11840 if ((b->type == bp_fast_tracepoint
11841 ? !may_insert_fast_tracepoints
11842 : !may_insert_tracepoints))
11843 continue;
11844
11845 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11846 {
11847 if (target_can_download_tracepoint ())
11848 can_download_tracepoint = TRIBOOL_TRUE;
11849 else
11850 can_download_tracepoint = TRIBOOL_FALSE;
11851 }
11852
11853 if (can_download_tracepoint == TRIBOOL_FALSE)
11854 break;
11855
11856 for (bl = b->loc; bl; bl = bl->next)
11857 {
11858 /* In tracepoint, locations are _never_ duplicated, so
11859 should_be_inserted is equivalent to
11860 unduplicated_should_be_inserted. */
11861 if (!should_be_inserted (bl) || bl->inserted)
11862 continue;
11863
11864 switch_to_program_space_and_thread (bl->pspace);
11865
11866 target_download_tracepoint (bl);
11867
11868 bl->inserted = 1;
11869 bp_location_downloaded = 1;
11870 }
11871 t = (struct tracepoint *) b;
11872 t->number_on_target = b->number;
11873 if (bp_location_downloaded)
11874 gdb::observers::breakpoint_modified.notify (b);
11875 }
11876 }
11877
11878 /* Swap the insertion/duplication state between two locations. */
11879
11880 static void
11881 swap_insertion (struct bp_location *left, struct bp_location *right)
11882 {
11883 const int left_inserted = left->inserted;
11884 const int left_duplicate = left->duplicate;
11885 const int left_needs_update = left->needs_update;
11886 const struct bp_target_info left_target_info = left->target_info;
11887
11888 /* Locations of tracepoints can never be duplicated. */
11889 if (is_tracepoint (left->owner))
11890 gdb_assert (!left->duplicate);
11891 if (is_tracepoint (right->owner))
11892 gdb_assert (!right->duplicate);
11893
11894 left->inserted = right->inserted;
11895 left->duplicate = right->duplicate;
11896 left->needs_update = right->needs_update;
11897 left->target_info = right->target_info;
11898 right->inserted = left_inserted;
11899 right->duplicate = left_duplicate;
11900 right->needs_update = left_needs_update;
11901 right->target_info = left_target_info;
11902 }
11903
11904 /* Force the re-insertion of the locations at ADDRESS. This is called
11905 once a new/deleted/modified duplicate location is found and we are evaluating
11906 conditions on the target's side. Such conditions need to be updated on
11907 the target. */
11908
11909 static void
11910 force_breakpoint_reinsertion (struct bp_location *bl)
11911 {
11912 struct bp_location **locp = NULL, **loc2p;
11913 struct bp_location *loc;
11914 CORE_ADDR address = 0;
11915 int pspace_num;
11916
11917 address = bl->address;
11918 pspace_num = bl->pspace->num;
11919
11920 /* This is only meaningful if the target is
11921 evaluating conditions and if the user has
11922 opted for condition evaluation on the target's
11923 side. */
11924 if (gdb_evaluates_breakpoint_condition_p ()
11925 || !target_supports_evaluation_of_breakpoint_conditions ())
11926 return;
11927
11928 /* Flag all breakpoint locations with this address and
11929 the same program space as the location
11930 as "its condition has changed". We need to
11931 update the conditions on the target's side. */
11932 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11933 {
11934 loc = *loc2p;
11935
11936 if (!is_breakpoint (loc->owner)
11937 || pspace_num != loc->pspace->num)
11938 continue;
11939
11940 /* Flag the location appropriately. We use a different state to
11941 let everyone know that we already updated the set of locations
11942 with addr bl->address and program space bl->pspace. This is so
11943 we don't have to keep calling these functions just to mark locations
11944 that have already been marked. */
11945 loc->condition_changed = condition_updated;
11946
11947 /* Free the agent expression bytecode as well. We will compute
11948 it later on. */
11949 loc->cond_bytecode.reset ();
11950 }
11951 }
11952
11953 /* Called whether new breakpoints are created, or existing breakpoints
11954 deleted, to update the global location list and recompute which
11955 locations are duplicate of which.
11956
11957 The INSERT_MODE flag determines whether locations may not, may, or
11958 shall be inserted now. See 'enum ugll_insert_mode' for more
11959 info. */
11960
11961 static void
11962 update_global_location_list (enum ugll_insert_mode insert_mode)
11963 {
11964 struct breakpoint *b;
11965 struct bp_location **locp, *loc;
11966 /* Last breakpoint location address that was marked for update. */
11967 CORE_ADDR last_addr = 0;
11968 /* Last breakpoint location program space that was marked for update. */
11969 int last_pspace_num = -1;
11970
11971 /* Used in the duplicates detection below. When iterating over all
11972 bp_locations, points to the first bp_location of a given address.
11973 Breakpoints and watchpoints of different types are never
11974 duplicates of each other. Keep one pointer for each type of
11975 breakpoint/watchpoint, so we only need to loop over all locations
11976 once. */
11977 struct bp_location *bp_loc_first; /* breakpoint */
11978 struct bp_location *wp_loc_first; /* hardware watchpoint */
11979 struct bp_location *awp_loc_first; /* access watchpoint */
11980 struct bp_location *rwp_loc_first; /* read watchpoint */
11981
11982 /* Saved former bp_locations array which we compare against the newly
11983 built bp_locations from the current state of ALL_BREAKPOINTS. */
11984 struct bp_location **old_locp;
11985 unsigned old_locations_count;
11986 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
11987
11988 old_locations_count = bp_locations_count;
11989 bp_locations = NULL;
11990 bp_locations_count = 0;
11991
11992 ALL_BREAKPOINTS (b)
11993 for (loc = b->loc; loc; loc = loc->next)
11994 bp_locations_count++;
11995
11996 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11997 locp = bp_locations;
11998 ALL_BREAKPOINTS (b)
11999 for (loc = b->loc; loc; loc = loc->next)
12000 *locp++ = loc;
12001
12002 /* See if we need to "upgrade" a software breakpoint to a hardware
12003 breakpoint. Do this before deciding whether locations are
12004 duplicates. Also do this before sorting because sorting order
12005 depends on location type. */
12006 for (locp = bp_locations;
12007 locp < bp_locations + bp_locations_count;
12008 locp++)
12009 {
12010 loc = *locp;
12011 if (!loc->inserted && should_be_inserted (loc))
12012 handle_automatic_hardware_breakpoints (loc);
12013 }
12014
12015 std::sort (bp_locations, bp_locations + bp_locations_count,
12016 bp_location_is_less_than);
12017
12018 bp_locations_target_extensions_update ();
12019
12020 /* Identify bp_location instances that are no longer present in the
12021 new list, and therefore should be freed. Note that it's not
12022 necessary that those locations should be removed from inferior --
12023 if there's another location at the same address (previously
12024 marked as duplicate), we don't need to remove/insert the
12025 location.
12026
12027 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12028 and former bp_location array state respectively. */
12029
12030 locp = bp_locations;
12031 for (old_locp = old_locations.get ();
12032 old_locp < old_locations.get () + old_locations_count;
12033 old_locp++)
12034 {
12035 struct bp_location *old_loc = *old_locp;
12036 struct bp_location **loc2p;
12037
12038 /* Tells if 'old_loc' is found among the new locations. If
12039 not, we have to free it. */
12040 int found_object = 0;
12041 /* Tells if the location should remain inserted in the target. */
12042 int keep_in_target = 0;
12043 int removed = 0;
12044
12045 /* Skip LOCP entries which will definitely never be needed.
12046 Stop either at or being the one matching OLD_LOC. */
12047 while (locp < bp_locations + bp_locations_count
12048 && (*locp)->address < old_loc->address)
12049 locp++;
12050
12051 for (loc2p = locp;
12052 (loc2p < bp_locations + bp_locations_count
12053 && (*loc2p)->address == old_loc->address);
12054 loc2p++)
12055 {
12056 /* Check if this is a new/duplicated location or a duplicated
12057 location that had its condition modified. If so, we want to send
12058 its condition to the target if evaluation of conditions is taking
12059 place there. */
12060 if ((*loc2p)->condition_changed == condition_modified
12061 && (last_addr != old_loc->address
12062 || last_pspace_num != old_loc->pspace->num))
12063 {
12064 force_breakpoint_reinsertion (*loc2p);
12065 last_pspace_num = old_loc->pspace->num;
12066 }
12067
12068 if (*loc2p == old_loc)
12069 found_object = 1;
12070 }
12071
12072 /* We have already handled this address, update it so that we don't
12073 have to go through updates again. */
12074 last_addr = old_loc->address;
12075
12076 /* Target-side condition evaluation: Handle deleted locations. */
12077 if (!found_object)
12078 force_breakpoint_reinsertion (old_loc);
12079
12080 /* If this location is no longer present, and inserted, look if
12081 there's maybe a new location at the same address. If so,
12082 mark that one inserted, and don't remove this one. This is
12083 needed so that we don't have a time window where a breakpoint
12084 at certain location is not inserted. */
12085
12086 if (old_loc->inserted)
12087 {
12088 /* If the location is inserted now, we might have to remove
12089 it. */
12090
12091 if (found_object && should_be_inserted (old_loc))
12092 {
12093 /* The location is still present in the location list,
12094 and still should be inserted. Don't do anything. */
12095 keep_in_target = 1;
12096 }
12097 else
12098 {
12099 /* This location still exists, but it won't be kept in the
12100 target since it may have been disabled. We proceed to
12101 remove its target-side condition. */
12102
12103 /* The location is either no longer present, or got
12104 disabled. See if there's another location at the
12105 same address, in which case we don't need to remove
12106 this one from the target. */
12107
12108 /* OLD_LOC comes from existing struct breakpoint. */
12109 if (bl_address_is_meaningful (old_loc))
12110 {
12111 for (loc2p = locp;
12112 (loc2p < bp_locations + bp_locations_count
12113 && (*loc2p)->address == old_loc->address);
12114 loc2p++)
12115 {
12116 struct bp_location *loc2 = *loc2p;
12117
12118 if (loc2 == old_loc)
12119 continue;
12120
12121 if (breakpoint_locations_match (loc2, old_loc))
12122 {
12123 /* Read watchpoint locations are switched to
12124 access watchpoints, if the former are not
12125 supported, but the latter are. */
12126 if (is_hardware_watchpoint (old_loc->owner))
12127 {
12128 gdb_assert (is_hardware_watchpoint (loc2->owner));
12129 loc2->watchpoint_type = old_loc->watchpoint_type;
12130 }
12131
12132 /* loc2 is a duplicated location. We need to check
12133 if it should be inserted in case it will be
12134 unduplicated. */
12135 if (unduplicated_should_be_inserted (loc2))
12136 {
12137 swap_insertion (old_loc, loc2);
12138 keep_in_target = 1;
12139 break;
12140 }
12141 }
12142 }
12143 }
12144 }
12145
12146 if (!keep_in_target)
12147 {
12148 if (remove_breakpoint (old_loc))
12149 {
12150 /* This is just about all we can do. We could keep
12151 this location on the global list, and try to
12152 remove it next time, but there's no particular
12153 reason why we will succeed next time.
12154
12155 Note that at this point, old_loc->owner is still
12156 valid, as delete_breakpoint frees the breakpoint
12157 only after calling us. */
12158 printf_filtered (_("warning: Error removing "
12159 "breakpoint %d\n"),
12160 old_loc->owner->number);
12161 }
12162 removed = 1;
12163 }
12164 }
12165
12166 if (!found_object)
12167 {
12168 if (removed && target_is_non_stop_p ()
12169 && need_moribund_for_location_type (old_loc))
12170 {
12171 /* This location was removed from the target. In
12172 non-stop mode, a race condition is possible where
12173 we've removed a breakpoint, but stop events for that
12174 breakpoint are already queued and will arrive later.
12175 We apply an heuristic to be able to distinguish such
12176 SIGTRAPs from other random SIGTRAPs: we keep this
12177 breakpoint location for a bit, and will retire it
12178 after we see some number of events. The theory here
12179 is that reporting of events should, "on the average",
12180 be fair, so after a while we'll see events from all
12181 threads that have anything of interest, and no longer
12182 need to keep this breakpoint location around. We
12183 don't hold locations forever so to reduce chances of
12184 mistaking a non-breakpoint SIGTRAP for a breakpoint
12185 SIGTRAP.
12186
12187 The heuristic failing can be disastrous on
12188 decr_pc_after_break targets.
12189
12190 On decr_pc_after_break targets, like e.g., x86-linux,
12191 if we fail to recognize a late breakpoint SIGTRAP,
12192 because events_till_retirement has reached 0 too
12193 soon, we'll fail to do the PC adjustment, and report
12194 a random SIGTRAP to the user. When the user resumes
12195 the inferior, it will most likely immediately crash
12196 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12197 corrupted, because of being resumed e.g., in the
12198 middle of a multi-byte instruction, or skipped a
12199 one-byte instruction. This was actually seen happen
12200 on native x86-linux, and should be less rare on
12201 targets that do not support new thread events, like
12202 remote, due to the heuristic depending on
12203 thread_count.
12204
12205 Mistaking a random SIGTRAP for a breakpoint trap
12206 causes similar symptoms (PC adjustment applied when
12207 it shouldn't), but then again, playing with SIGTRAPs
12208 behind the debugger's back is asking for trouble.
12209
12210 Since hardware watchpoint traps are always
12211 distinguishable from other traps, so we don't need to
12212 apply keep hardware watchpoint moribund locations
12213 around. We simply always ignore hardware watchpoint
12214 traps we can no longer explain. */
12215
12216 process_stratum_target *proc_target = nullptr;
12217 for (inferior *inf : all_inferiors ())
12218 if (inf->pspace == old_loc->pspace)
12219 {
12220 proc_target = inf->process_target ();
12221 break;
12222 }
12223 if (proc_target != nullptr)
12224 old_loc->events_till_retirement
12225 = 3 * (thread_count (proc_target) + 1);
12226 else
12227 old_loc->events_till_retirement = 1;
12228 old_loc->owner = NULL;
12229
12230 moribund_locations.push_back (old_loc);
12231 }
12232 else
12233 {
12234 old_loc->owner = NULL;
12235 decref_bp_location (&old_loc);
12236 }
12237 }
12238 }
12239
12240 /* Rescan breakpoints at the same address and section, marking the
12241 first one as "first" and any others as "duplicates". This is so
12242 that the bpt instruction is only inserted once. If we have a
12243 permanent breakpoint at the same place as BPT, make that one the
12244 official one, and the rest as duplicates. Permanent breakpoints
12245 are sorted first for the same address.
12246
12247 Do the same for hardware watchpoints, but also considering the
12248 watchpoint's type (regular/access/read) and length. */
12249
12250 bp_loc_first = NULL;
12251 wp_loc_first = NULL;
12252 awp_loc_first = NULL;
12253 rwp_loc_first = NULL;
12254 ALL_BP_LOCATIONS (loc, locp)
12255 {
12256 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12257 non-NULL. */
12258 struct bp_location **loc_first_p;
12259 b = loc->owner;
12260
12261 if (!unduplicated_should_be_inserted (loc)
12262 || !bl_address_is_meaningful (loc)
12263 /* Don't detect duplicate for tracepoint locations because they are
12264 never duplicated. See the comments in field `duplicate' of
12265 `struct bp_location'. */
12266 || is_tracepoint (b))
12267 {
12268 /* Clear the condition modification flag. */
12269 loc->condition_changed = condition_unchanged;
12270 continue;
12271 }
12272
12273 if (b->type == bp_hardware_watchpoint)
12274 loc_first_p = &wp_loc_first;
12275 else if (b->type == bp_read_watchpoint)
12276 loc_first_p = &rwp_loc_first;
12277 else if (b->type == bp_access_watchpoint)
12278 loc_first_p = &awp_loc_first;
12279 else
12280 loc_first_p = &bp_loc_first;
12281
12282 if (*loc_first_p == NULL
12283 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12284 || !breakpoint_locations_match (loc, *loc_first_p))
12285 {
12286 *loc_first_p = loc;
12287 loc->duplicate = 0;
12288
12289 if (is_breakpoint (loc->owner) && loc->condition_changed)
12290 {
12291 loc->needs_update = 1;
12292 /* Clear the condition modification flag. */
12293 loc->condition_changed = condition_unchanged;
12294 }
12295 continue;
12296 }
12297
12298
12299 /* This and the above ensure the invariant that the first location
12300 is not duplicated, and is the inserted one.
12301 All following are marked as duplicated, and are not inserted. */
12302 if (loc->inserted)
12303 swap_insertion (loc, *loc_first_p);
12304 loc->duplicate = 1;
12305
12306 /* Clear the condition modification flag. */
12307 loc->condition_changed = condition_unchanged;
12308 }
12309
12310 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12311 {
12312 if (insert_mode != UGLL_DONT_INSERT)
12313 insert_breakpoint_locations ();
12314 else
12315 {
12316 /* Even though the caller told us to not insert new
12317 locations, we may still need to update conditions on the
12318 target's side of breakpoints that were already inserted
12319 if the target is evaluating breakpoint conditions. We
12320 only update conditions for locations that are marked
12321 "needs_update". */
12322 update_inserted_breakpoint_locations ();
12323 }
12324 }
12325
12326 if (insert_mode != UGLL_DONT_INSERT)
12327 download_tracepoint_locations ();
12328 }
12329
12330 void
12331 breakpoint_retire_moribund (void)
12332 {
12333 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12334 {
12335 struct bp_location *loc = moribund_locations[ix];
12336 if (--(loc->events_till_retirement) == 0)
12337 {
12338 decref_bp_location (&loc);
12339 unordered_remove (moribund_locations, ix);
12340 --ix;
12341 }
12342 }
12343 }
12344
12345 static void
12346 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12347 {
12348
12349 try
12350 {
12351 update_global_location_list (insert_mode);
12352 }
12353 catch (const gdb_exception_error &e)
12354 {
12355 }
12356 }
12357
12358 /* Clear BKP from a BPS. */
12359
12360 static void
12361 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12362 {
12363 bpstat bs;
12364
12365 for (bs = bps; bs; bs = bs->next)
12366 if (bs->breakpoint_at == bpt)
12367 {
12368 bs->breakpoint_at = NULL;
12369 bs->old_val = NULL;
12370 /* bs->commands will be freed later. */
12371 }
12372 }
12373
12374 /* Callback for iterate_over_threads. */
12375 static int
12376 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12377 {
12378 struct breakpoint *bpt = (struct breakpoint *) data;
12379
12380 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12381 return 0;
12382 }
12383
12384 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12385 callbacks. */
12386
12387 static void
12388 say_where (struct breakpoint *b)
12389 {
12390 struct value_print_options opts;
12391
12392 get_user_print_options (&opts);
12393
12394 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12395 single string. */
12396 if (b->loc == NULL)
12397 {
12398 /* For pending locations, the output differs slightly based
12399 on b->extra_string. If this is non-NULL, it contains either
12400 a condition or dprintf arguments. */
12401 if (b->extra_string == NULL)
12402 {
12403 printf_filtered (_(" (%s) pending."),
12404 event_location_to_string (b->location.get ()));
12405 }
12406 else if (b->type == bp_dprintf)
12407 {
12408 printf_filtered (_(" (%s,%s) pending."),
12409 event_location_to_string (b->location.get ()),
12410 b->extra_string);
12411 }
12412 else
12413 {
12414 printf_filtered (_(" (%s %s) pending."),
12415 event_location_to_string (b->location.get ()),
12416 b->extra_string);
12417 }
12418 }
12419 else
12420 {
12421 if (opts.addressprint || b->loc->symtab == NULL)
12422 printf_filtered (" at %ps",
12423 styled_string (address_style.style (),
12424 paddress (b->loc->gdbarch,
12425 b->loc->address)));
12426 if (b->loc->symtab != NULL)
12427 {
12428 /* If there is a single location, we can print the location
12429 more nicely. */
12430 if (b->loc->next == NULL)
12431 {
12432 const char *filename
12433 = symtab_to_filename_for_display (b->loc->symtab);
12434 printf_filtered (": file %ps, line %d.",
12435 styled_string (file_name_style.style (),
12436 filename),
12437 b->loc->line_number);
12438 }
12439 else
12440 /* This is not ideal, but each location may have a
12441 different file name, and this at least reflects the
12442 real situation somewhat. */
12443 printf_filtered (": %s.",
12444 event_location_to_string (b->location.get ()));
12445 }
12446
12447 if (b->loc->next)
12448 {
12449 struct bp_location *loc = b->loc;
12450 int n = 0;
12451 for (; loc; loc = loc->next)
12452 ++n;
12453 printf_filtered (" (%d locations)", n);
12454 }
12455 }
12456 }
12457
12458 bp_location::~bp_location ()
12459 {
12460 xfree (function_name);
12461 }
12462
12463 /* Destructor for the breakpoint base class. */
12464
12465 breakpoint::~breakpoint ()
12466 {
12467 xfree (this->cond_string);
12468 xfree (this->extra_string);
12469 }
12470
12471 static struct bp_location *
12472 base_breakpoint_allocate_location (struct breakpoint *self)
12473 {
12474 return new bp_location (self);
12475 }
12476
12477 static void
12478 base_breakpoint_re_set (struct breakpoint *b)
12479 {
12480 /* Nothing to re-set. */
12481 }
12482
12483 #define internal_error_pure_virtual_called() \
12484 gdb_assert_not_reached ("pure virtual function called")
12485
12486 static int
12487 base_breakpoint_insert_location (struct bp_location *bl)
12488 {
12489 internal_error_pure_virtual_called ();
12490 }
12491
12492 static int
12493 base_breakpoint_remove_location (struct bp_location *bl,
12494 enum remove_bp_reason reason)
12495 {
12496 internal_error_pure_virtual_called ();
12497 }
12498
12499 static int
12500 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12501 const address_space *aspace,
12502 CORE_ADDR bp_addr,
12503 const struct target_waitstatus *ws)
12504 {
12505 internal_error_pure_virtual_called ();
12506 }
12507
12508 static void
12509 base_breakpoint_check_status (bpstat bs)
12510 {
12511 /* Always stop. */
12512 }
12513
12514 /* A "works_in_software_mode" breakpoint_ops method that just internal
12515 errors. */
12516
12517 static int
12518 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12519 {
12520 internal_error_pure_virtual_called ();
12521 }
12522
12523 /* A "resources_needed" breakpoint_ops method that just internal
12524 errors. */
12525
12526 static int
12527 base_breakpoint_resources_needed (const struct bp_location *bl)
12528 {
12529 internal_error_pure_virtual_called ();
12530 }
12531
12532 static enum print_stop_action
12533 base_breakpoint_print_it (bpstat bs)
12534 {
12535 internal_error_pure_virtual_called ();
12536 }
12537
12538 static void
12539 base_breakpoint_print_one_detail (const struct breakpoint *self,
12540 struct ui_out *uiout)
12541 {
12542 /* nothing */
12543 }
12544
12545 static void
12546 base_breakpoint_print_mention (struct breakpoint *b)
12547 {
12548 internal_error_pure_virtual_called ();
12549 }
12550
12551 static void
12552 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12553 {
12554 internal_error_pure_virtual_called ();
12555 }
12556
12557 static void
12558 base_breakpoint_create_sals_from_location
12559 (struct event_location *location,
12560 struct linespec_result *canonical,
12561 enum bptype type_wanted)
12562 {
12563 internal_error_pure_virtual_called ();
12564 }
12565
12566 static void
12567 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12568 struct linespec_result *c,
12569 gdb::unique_xmalloc_ptr<char> cond_string,
12570 gdb::unique_xmalloc_ptr<char> extra_string,
12571 enum bptype type_wanted,
12572 enum bpdisp disposition,
12573 int thread,
12574 int task, int ignore_count,
12575 const struct breakpoint_ops *o,
12576 int from_tty, int enabled,
12577 int internal, unsigned flags)
12578 {
12579 internal_error_pure_virtual_called ();
12580 }
12581
12582 static std::vector<symtab_and_line>
12583 base_breakpoint_decode_location (struct breakpoint *b,
12584 struct event_location *location,
12585 struct program_space *search_pspace)
12586 {
12587 internal_error_pure_virtual_called ();
12588 }
12589
12590 /* The default 'explains_signal' method. */
12591
12592 static int
12593 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12594 {
12595 return 1;
12596 }
12597
12598 /* The default "after_condition_true" method. */
12599
12600 static void
12601 base_breakpoint_after_condition_true (struct bpstats *bs)
12602 {
12603 /* Nothing to do. */
12604 }
12605
12606 struct breakpoint_ops base_breakpoint_ops =
12607 {
12608 base_breakpoint_allocate_location,
12609 base_breakpoint_re_set,
12610 base_breakpoint_insert_location,
12611 base_breakpoint_remove_location,
12612 base_breakpoint_breakpoint_hit,
12613 base_breakpoint_check_status,
12614 base_breakpoint_resources_needed,
12615 base_breakpoint_works_in_software_mode,
12616 base_breakpoint_print_it,
12617 NULL,
12618 base_breakpoint_print_one_detail,
12619 base_breakpoint_print_mention,
12620 base_breakpoint_print_recreate,
12621 base_breakpoint_create_sals_from_location,
12622 base_breakpoint_create_breakpoints_sal,
12623 base_breakpoint_decode_location,
12624 base_breakpoint_explains_signal,
12625 base_breakpoint_after_condition_true,
12626 };
12627
12628 /* Default breakpoint_ops methods. */
12629
12630 static void
12631 bkpt_re_set (struct breakpoint *b)
12632 {
12633 /* FIXME: is this still reachable? */
12634 if (breakpoint_event_location_empty_p (b))
12635 {
12636 /* Anything without a location can't be re-set. */
12637 delete_breakpoint (b);
12638 return;
12639 }
12640
12641 breakpoint_re_set_default (b);
12642 }
12643
12644 static int
12645 bkpt_insert_location (struct bp_location *bl)
12646 {
12647 CORE_ADDR addr = bl->target_info.reqstd_address;
12648
12649 bl->target_info.kind = breakpoint_kind (bl, &addr);
12650 bl->target_info.placed_address = addr;
12651
12652 if (bl->loc_type == bp_loc_hardware_breakpoint)
12653 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12654 else
12655 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12656 }
12657
12658 static int
12659 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12660 {
12661 if (bl->loc_type == bp_loc_hardware_breakpoint)
12662 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12663 else
12664 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12665 }
12666
12667 static int
12668 bkpt_breakpoint_hit (const struct bp_location *bl,
12669 const address_space *aspace, CORE_ADDR bp_addr,
12670 const struct target_waitstatus *ws)
12671 {
12672 if (ws->kind != TARGET_WAITKIND_STOPPED
12673 || ws->value.sig != GDB_SIGNAL_TRAP)
12674 return 0;
12675
12676 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12677 aspace, bp_addr))
12678 return 0;
12679
12680 if (overlay_debugging /* unmapped overlay section */
12681 && section_is_overlay (bl->section)
12682 && !section_is_mapped (bl->section))
12683 return 0;
12684
12685 return 1;
12686 }
12687
12688 static int
12689 dprintf_breakpoint_hit (const struct bp_location *bl,
12690 const address_space *aspace, CORE_ADDR bp_addr,
12691 const struct target_waitstatus *ws)
12692 {
12693 if (dprintf_style == dprintf_style_agent
12694 && target_can_run_breakpoint_commands ())
12695 {
12696 /* An agent-style dprintf never causes a stop. If we see a trap
12697 for this address it must be for a breakpoint that happens to
12698 be set at the same address. */
12699 return 0;
12700 }
12701
12702 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12703 }
12704
12705 static int
12706 bkpt_resources_needed (const struct bp_location *bl)
12707 {
12708 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12709
12710 return 1;
12711 }
12712
12713 static enum print_stop_action
12714 bkpt_print_it (bpstat bs)
12715 {
12716 struct breakpoint *b;
12717 const struct bp_location *bl;
12718 int bp_temp;
12719 struct ui_out *uiout = current_uiout;
12720
12721 gdb_assert (bs->bp_location_at != NULL);
12722
12723 bl = bs->bp_location_at.get ();
12724 b = bs->breakpoint_at;
12725
12726 bp_temp = b->disposition == disp_del;
12727 if (bl->address != bl->requested_address)
12728 breakpoint_adjustment_warning (bl->requested_address,
12729 bl->address,
12730 b->number, 1);
12731 annotate_breakpoint (b->number);
12732 maybe_print_thread_hit_breakpoint (uiout);
12733
12734 if (uiout->is_mi_like_p ())
12735 {
12736 uiout->field_string ("reason",
12737 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12738 uiout->field_string ("disp", bpdisp_text (b->disposition));
12739 }
12740 if (bp_temp)
12741 uiout->message ("Temporary breakpoint %pF, ",
12742 signed_field ("bkptno", b->number));
12743 else
12744 uiout->message ("Breakpoint %pF, ",
12745 signed_field ("bkptno", b->number));
12746
12747 return PRINT_SRC_AND_LOC;
12748 }
12749
12750 static void
12751 bkpt_print_mention (struct breakpoint *b)
12752 {
12753 if (current_uiout->is_mi_like_p ())
12754 return;
12755
12756 switch (b->type)
12757 {
12758 case bp_breakpoint:
12759 case bp_gnu_ifunc_resolver:
12760 if (b->disposition == disp_del)
12761 printf_filtered (_("Temporary breakpoint"));
12762 else
12763 printf_filtered (_("Breakpoint"));
12764 printf_filtered (_(" %d"), b->number);
12765 if (b->type == bp_gnu_ifunc_resolver)
12766 printf_filtered (_(" at gnu-indirect-function resolver"));
12767 break;
12768 case bp_hardware_breakpoint:
12769 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12770 break;
12771 case bp_dprintf:
12772 printf_filtered (_("Dprintf %d"), b->number);
12773 break;
12774 }
12775
12776 say_where (b);
12777 }
12778
12779 static void
12780 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12781 {
12782 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12783 fprintf_unfiltered (fp, "tbreak");
12784 else if (tp->type == bp_breakpoint)
12785 fprintf_unfiltered (fp, "break");
12786 else if (tp->type == bp_hardware_breakpoint
12787 && tp->disposition == disp_del)
12788 fprintf_unfiltered (fp, "thbreak");
12789 else if (tp->type == bp_hardware_breakpoint)
12790 fprintf_unfiltered (fp, "hbreak");
12791 else
12792 internal_error (__FILE__, __LINE__,
12793 _("unhandled breakpoint type %d"), (int) tp->type);
12794
12795 fprintf_unfiltered (fp, " %s",
12796 event_location_to_string (tp->location.get ()));
12797
12798 /* Print out extra_string if this breakpoint is pending. It might
12799 contain, for example, conditions that were set by the user. */
12800 if (tp->loc == NULL && tp->extra_string != NULL)
12801 fprintf_unfiltered (fp, " %s", tp->extra_string);
12802
12803 print_recreate_thread (tp, fp);
12804 }
12805
12806 static void
12807 bkpt_create_sals_from_location (struct event_location *location,
12808 struct linespec_result *canonical,
12809 enum bptype type_wanted)
12810 {
12811 create_sals_from_location_default (location, canonical, type_wanted);
12812 }
12813
12814 static void
12815 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12816 struct linespec_result *canonical,
12817 gdb::unique_xmalloc_ptr<char> cond_string,
12818 gdb::unique_xmalloc_ptr<char> extra_string,
12819 enum bptype type_wanted,
12820 enum bpdisp disposition,
12821 int thread,
12822 int task, int ignore_count,
12823 const struct breakpoint_ops *ops,
12824 int from_tty, int enabled,
12825 int internal, unsigned flags)
12826 {
12827 create_breakpoints_sal_default (gdbarch, canonical,
12828 std::move (cond_string),
12829 std::move (extra_string),
12830 type_wanted,
12831 disposition, thread, task,
12832 ignore_count, ops, from_tty,
12833 enabled, internal, flags);
12834 }
12835
12836 static std::vector<symtab_and_line>
12837 bkpt_decode_location (struct breakpoint *b,
12838 struct event_location *location,
12839 struct program_space *search_pspace)
12840 {
12841 return decode_location_default (b, location, search_pspace);
12842 }
12843
12844 /* Virtual table for internal breakpoints. */
12845
12846 static void
12847 internal_bkpt_re_set (struct breakpoint *b)
12848 {
12849 switch (b->type)
12850 {
12851 /* Delete overlay event and longjmp master breakpoints; they
12852 will be reset later by breakpoint_re_set. */
12853 case bp_overlay_event:
12854 case bp_longjmp_master:
12855 case bp_std_terminate_master:
12856 case bp_exception_master:
12857 delete_breakpoint (b);
12858 break;
12859
12860 /* This breakpoint is special, it's set up when the inferior
12861 starts and we really don't want to touch it. */
12862 case bp_shlib_event:
12863
12864 /* Like bp_shlib_event, this breakpoint type is special. Once
12865 it is set up, we do not want to touch it. */
12866 case bp_thread_event:
12867 break;
12868 }
12869 }
12870
12871 static void
12872 internal_bkpt_check_status (bpstat bs)
12873 {
12874 if (bs->breakpoint_at->type == bp_shlib_event)
12875 {
12876 /* If requested, stop when the dynamic linker notifies GDB of
12877 events. This allows the user to get control and place
12878 breakpoints in initializer routines for dynamically loaded
12879 objects (among other things). */
12880 bs->stop = stop_on_solib_events;
12881 bs->print = stop_on_solib_events;
12882 }
12883 else
12884 bs->stop = 0;
12885 }
12886
12887 static enum print_stop_action
12888 internal_bkpt_print_it (bpstat bs)
12889 {
12890 struct breakpoint *b;
12891
12892 b = bs->breakpoint_at;
12893
12894 switch (b->type)
12895 {
12896 case bp_shlib_event:
12897 /* Did we stop because the user set the stop_on_solib_events
12898 variable? (If so, we report this as a generic, "Stopped due
12899 to shlib event" message.) */
12900 print_solib_event (0);
12901 break;
12902
12903 case bp_thread_event:
12904 /* Not sure how we will get here.
12905 GDB should not stop for these breakpoints. */
12906 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12907 break;
12908
12909 case bp_overlay_event:
12910 /* By analogy with the thread event, GDB should not stop for these. */
12911 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12912 break;
12913
12914 case bp_longjmp_master:
12915 /* These should never be enabled. */
12916 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12917 break;
12918
12919 case bp_std_terminate_master:
12920 /* These should never be enabled. */
12921 printf_filtered (_("std::terminate Master Breakpoint: "
12922 "gdb should not stop!\n"));
12923 break;
12924
12925 case bp_exception_master:
12926 /* These should never be enabled. */
12927 printf_filtered (_("Exception Master Breakpoint: "
12928 "gdb should not stop!\n"));
12929 break;
12930 }
12931
12932 return PRINT_NOTHING;
12933 }
12934
12935 static void
12936 internal_bkpt_print_mention (struct breakpoint *b)
12937 {
12938 /* Nothing to mention. These breakpoints are internal. */
12939 }
12940
12941 /* Virtual table for momentary breakpoints */
12942
12943 static void
12944 momentary_bkpt_re_set (struct breakpoint *b)
12945 {
12946 /* Keep temporary breakpoints, which can be encountered when we step
12947 over a dlopen call and solib_add is resetting the breakpoints.
12948 Otherwise these should have been blown away via the cleanup chain
12949 or by breakpoint_init_inferior when we rerun the executable. */
12950 }
12951
12952 static void
12953 momentary_bkpt_check_status (bpstat bs)
12954 {
12955 /* Nothing. The point of these breakpoints is causing a stop. */
12956 }
12957
12958 static enum print_stop_action
12959 momentary_bkpt_print_it (bpstat bs)
12960 {
12961 return PRINT_UNKNOWN;
12962 }
12963
12964 static void
12965 momentary_bkpt_print_mention (struct breakpoint *b)
12966 {
12967 /* Nothing to mention. These breakpoints are internal. */
12968 }
12969
12970 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12971
12972 It gets cleared already on the removal of the first one of such placed
12973 breakpoints. This is OK as they get all removed altogether. */
12974
12975 longjmp_breakpoint::~longjmp_breakpoint ()
12976 {
12977 thread_info *tp = find_thread_global_id (this->thread);
12978
12979 if (tp != NULL)
12980 tp->initiating_frame = null_frame_id;
12981 }
12982
12983 /* Specific methods for probe breakpoints. */
12984
12985 static int
12986 bkpt_probe_insert_location (struct bp_location *bl)
12987 {
12988 int v = bkpt_insert_location (bl);
12989
12990 if (v == 0)
12991 {
12992 /* The insertion was successful, now let's set the probe's semaphore
12993 if needed. */
12994 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
12995 }
12996
12997 return v;
12998 }
12999
13000 static int
13001 bkpt_probe_remove_location (struct bp_location *bl,
13002 enum remove_bp_reason reason)
13003 {
13004 /* Let's clear the semaphore before removing the location. */
13005 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
13006
13007 return bkpt_remove_location (bl, reason);
13008 }
13009
13010 static void
13011 bkpt_probe_create_sals_from_location (struct event_location *location,
13012 struct linespec_result *canonical,
13013 enum bptype type_wanted)
13014 {
13015 struct linespec_sals lsal;
13016
13017 lsal.sals = parse_probes (location, NULL, canonical);
13018 lsal.canonical
13019 = xstrdup (event_location_to_string (canonical->location.get ()));
13020 canonical->lsals.push_back (std::move (lsal));
13021 }
13022
13023 static std::vector<symtab_and_line>
13024 bkpt_probe_decode_location (struct breakpoint *b,
13025 struct event_location *location,
13026 struct program_space *search_pspace)
13027 {
13028 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
13029 if (sals.empty ())
13030 error (_("probe not found"));
13031 return sals;
13032 }
13033
13034 /* The breakpoint_ops structure to be used in tracepoints. */
13035
13036 static void
13037 tracepoint_re_set (struct breakpoint *b)
13038 {
13039 breakpoint_re_set_default (b);
13040 }
13041
13042 static int
13043 tracepoint_breakpoint_hit (const struct bp_location *bl,
13044 const address_space *aspace, CORE_ADDR bp_addr,
13045 const struct target_waitstatus *ws)
13046 {
13047 /* By definition, the inferior does not report stops at
13048 tracepoints. */
13049 return 0;
13050 }
13051
13052 static void
13053 tracepoint_print_one_detail (const struct breakpoint *self,
13054 struct ui_out *uiout)
13055 {
13056 struct tracepoint *tp = (struct tracepoint *) self;
13057 if (!tp->static_trace_marker_id.empty ())
13058 {
13059 gdb_assert (self->type == bp_static_tracepoint);
13060
13061 uiout->message ("\tmarker id is %pF\n",
13062 string_field ("static-tracepoint-marker-string-id",
13063 tp->static_trace_marker_id.c_str ()));
13064 }
13065 }
13066
13067 static void
13068 tracepoint_print_mention (struct breakpoint *b)
13069 {
13070 if (current_uiout->is_mi_like_p ())
13071 return;
13072
13073 switch (b->type)
13074 {
13075 case bp_tracepoint:
13076 printf_filtered (_("Tracepoint"));
13077 printf_filtered (_(" %d"), b->number);
13078 break;
13079 case bp_fast_tracepoint:
13080 printf_filtered (_("Fast tracepoint"));
13081 printf_filtered (_(" %d"), b->number);
13082 break;
13083 case bp_static_tracepoint:
13084 printf_filtered (_("Static tracepoint"));
13085 printf_filtered (_(" %d"), b->number);
13086 break;
13087 default:
13088 internal_error (__FILE__, __LINE__,
13089 _("unhandled tracepoint type %d"), (int) b->type);
13090 }
13091
13092 say_where (b);
13093 }
13094
13095 static void
13096 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13097 {
13098 struct tracepoint *tp = (struct tracepoint *) self;
13099
13100 if (self->type == bp_fast_tracepoint)
13101 fprintf_unfiltered (fp, "ftrace");
13102 else if (self->type == bp_static_tracepoint)
13103 fprintf_unfiltered (fp, "strace");
13104 else if (self->type == bp_tracepoint)
13105 fprintf_unfiltered (fp, "trace");
13106 else
13107 internal_error (__FILE__, __LINE__,
13108 _("unhandled tracepoint type %d"), (int) self->type);
13109
13110 fprintf_unfiltered (fp, " %s",
13111 event_location_to_string (self->location.get ()));
13112 print_recreate_thread (self, fp);
13113
13114 if (tp->pass_count)
13115 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13116 }
13117
13118 static void
13119 tracepoint_create_sals_from_location (struct event_location *location,
13120 struct linespec_result *canonical,
13121 enum bptype type_wanted)
13122 {
13123 create_sals_from_location_default (location, canonical, type_wanted);
13124 }
13125
13126 static void
13127 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13128 struct linespec_result *canonical,
13129 gdb::unique_xmalloc_ptr<char> cond_string,
13130 gdb::unique_xmalloc_ptr<char> extra_string,
13131 enum bptype type_wanted,
13132 enum bpdisp disposition,
13133 int thread,
13134 int task, int ignore_count,
13135 const struct breakpoint_ops *ops,
13136 int from_tty, int enabled,
13137 int internal, unsigned flags)
13138 {
13139 create_breakpoints_sal_default (gdbarch, canonical,
13140 std::move (cond_string),
13141 std::move (extra_string),
13142 type_wanted,
13143 disposition, thread, task,
13144 ignore_count, ops, from_tty,
13145 enabled, internal, flags);
13146 }
13147
13148 static std::vector<symtab_and_line>
13149 tracepoint_decode_location (struct breakpoint *b,
13150 struct event_location *location,
13151 struct program_space *search_pspace)
13152 {
13153 return decode_location_default (b, location, search_pspace);
13154 }
13155
13156 struct breakpoint_ops tracepoint_breakpoint_ops;
13157
13158 /* Virtual table for tracepoints on static probes. */
13159
13160 static void
13161 tracepoint_probe_create_sals_from_location
13162 (struct event_location *location,
13163 struct linespec_result *canonical,
13164 enum bptype type_wanted)
13165 {
13166 /* We use the same method for breakpoint on probes. */
13167 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13168 }
13169
13170 static std::vector<symtab_and_line>
13171 tracepoint_probe_decode_location (struct breakpoint *b,
13172 struct event_location *location,
13173 struct program_space *search_pspace)
13174 {
13175 /* We use the same method for breakpoint on probes. */
13176 return bkpt_probe_decode_location (b, location, search_pspace);
13177 }
13178
13179 /* Dprintf breakpoint_ops methods. */
13180
13181 static void
13182 dprintf_re_set (struct breakpoint *b)
13183 {
13184 breakpoint_re_set_default (b);
13185
13186 /* extra_string should never be non-NULL for dprintf. */
13187 gdb_assert (b->extra_string != NULL);
13188
13189 /* 1 - connect to target 1, that can run breakpoint commands.
13190 2 - create a dprintf, which resolves fine.
13191 3 - disconnect from target 1
13192 4 - connect to target 2, that can NOT run breakpoint commands.
13193
13194 After steps #3/#4, you'll want the dprintf command list to
13195 be updated, because target 1 and 2 may well return different
13196 answers for target_can_run_breakpoint_commands().
13197 Given absence of finer grained resetting, we get to do
13198 it all the time. */
13199 if (b->extra_string != NULL)
13200 update_dprintf_command_list (b);
13201 }
13202
13203 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13204
13205 static void
13206 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13207 {
13208 fprintf_unfiltered (fp, "dprintf %s,%s",
13209 event_location_to_string (tp->location.get ()),
13210 tp->extra_string);
13211 print_recreate_thread (tp, fp);
13212 }
13213
13214 /* Implement the "after_condition_true" breakpoint_ops method for
13215 dprintf.
13216
13217 dprintf's are implemented with regular commands in their command
13218 list, but we run the commands here instead of before presenting the
13219 stop to the user, as dprintf's don't actually cause a stop. This
13220 also makes it so that the commands of multiple dprintfs at the same
13221 address are all handled. */
13222
13223 static void
13224 dprintf_after_condition_true (struct bpstats *bs)
13225 {
13226 struct bpstats tmp_bs;
13227 struct bpstats *tmp_bs_p = &tmp_bs;
13228
13229 /* dprintf's never cause a stop. This wasn't set in the
13230 check_status hook instead because that would make the dprintf's
13231 condition not be evaluated. */
13232 bs->stop = 0;
13233
13234 /* Run the command list here. Take ownership of it instead of
13235 copying. We never want these commands to run later in
13236 bpstat_do_actions, if a breakpoint that causes a stop happens to
13237 be set at same address as this dprintf, or even if running the
13238 commands here throws. */
13239 tmp_bs.commands = bs->commands;
13240 bs->commands = NULL;
13241
13242 bpstat_do_actions_1 (&tmp_bs_p);
13243
13244 /* 'tmp_bs.commands' will usually be NULL by now, but
13245 bpstat_do_actions_1 may return early without processing the whole
13246 list. */
13247 }
13248
13249 /* The breakpoint_ops structure to be used on static tracepoints with
13250 markers (`-m'). */
13251
13252 static void
13253 strace_marker_create_sals_from_location (struct event_location *location,
13254 struct linespec_result *canonical,
13255 enum bptype type_wanted)
13256 {
13257 struct linespec_sals lsal;
13258 const char *arg_start, *arg;
13259
13260 arg = arg_start = get_linespec_location (location)->spec_string;
13261 lsal.sals = decode_static_tracepoint_spec (&arg);
13262
13263 std::string str (arg_start, arg - arg_start);
13264 const char *ptr = str.c_str ();
13265 canonical->location
13266 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
13267
13268 lsal.canonical
13269 = xstrdup (event_location_to_string (canonical->location.get ()));
13270 canonical->lsals.push_back (std::move (lsal));
13271 }
13272
13273 static void
13274 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13275 struct linespec_result *canonical,
13276 gdb::unique_xmalloc_ptr<char> cond_string,
13277 gdb::unique_xmalloc_ptr<char> extra_string,
13278 enum bptype type_wanted,
13279 enum bpdisp disposition,
13280 int thread,
13281 int task, int ignore_count,
13282 const struct breakpoint_ops *ops,
13283 int from_tty, int enabled,
13284 int internal, unsigned flags)
13285 {
13286 const linespec_sals &lsal = canonical->lsals[0];
13287
13288 /* If the user is creating a static tracepoint by marker id
13289 (strace -m MARKER_ID), then store the sals index, so that
13290 breakpoint_re_set can try to match up which of the newly
13291 found markers corresponds to this one, and, don't try to
13292 expand multiple locations for each sal, given than SALS
13293 already should contain all sals for MARKER_ID. */
13294
13295 for (size_t i = 0; i < lsal.sals.size (); i++)
13296 {
13297 event_location_up location
13298 = copy_event_location (canonical->location.get ());
13299
13300 std::unique_ptr<tracepoint> tp (new tracepoint ());
13301 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
13302 std::move (location), NULL,
13303 std::move (cond_string),
13304 std::move (extra_string),
13305 type_wanted, disposition,
13306 thread, task, ignore_count, ops,
13307 from_tty, enabled, internal, flags,
13308 canonical->special_display);
13309 /* Given that its possible to have multiple markers with
13310 the same string id, if the user is creating a static
13311 tracepoint by marker id ("strace -m MARKER_ID"), then
13312 store the sals index, so that breakpoint_re_set can
13313 try to match up which of the newly found markers
13314 corresponds to this one */
13315 tp->static_trace_marker_id_idx = i;
13316
13317 install_breakpoint (internal, std::move (tp), 0);
13318 }
13319 }
13320
13321 static std::vector<symtab_and_line>
13322 strace_marker_decode_location (struct breakpoint *b,
13323 struct event_location *location,
13324 struct program_space *search_pspace)
13325 {
13326 struct tracepoint *tp = (struct tracepoint *) b;
13327 const char *s = get_linespec_location (location)->spec_string;
13328
13329 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13330 if (sals.size () > tp->static_trace_marker_id_idx)
13331 {
13332 sals[0] = sals[tp->static_trace_marker_id_idx];
13333 sals.resize (1);
13334 return sals;
13335 }
13336 else
13337 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
13338 }
13339
13340 static struct breakpoint_ops strace_marker_breakpoint_ops;
13341
13342 static int
13343 strace_marker_p (struct breakpoint *b)
13344 {
13345 return b->ops == &strace_marker_breakpoint_ops;
13346 }
13347
13348 /* Delete a breakpoint and clean up all traces of it in the data
13349 structures. */
13350
13351 void
13352 delete_breakpoint (struct breakpoint *bpt)
13353 {
13354 struct breakpoint *b;
13355
13356 gdb_assert (bpt != NULL);
13357
13358 /* Has this bp already been deleted? This can happen because
13359 multiple lists can hold pointers to bp's. bpstat lists are
13360 especial culprits.
13361
13362 One example of this happening is a watchpoint's scope bp. When
13363 the scope bp triggers, we notice that the watchpoint is out of
13364 scope, and delete it. We also delete its scope bp. But the
13365 scope bp is marked "auto-deleting", and is already on a bpstat.
13366 That bpstat is then checked for auto-deleting bp's, which are
13367 deleted.
13368
13369 A real solution to this problem might involve reference counts in
13370 bp's, and/or giving them pointers back to their referencing
13371 bpstat's, and teaching delete_breakpoint to only free a bp's
13372 storage when no more references were extent. A cheaper bandaid
13373 was chosen. */
13374 if (bpt->type == bp_none)
13375 return;
13376
13377 /* At least avoid this stale reference until the reference counting
13378 of breakpoints gets resolved. */
13379 if (bpt->related_breakpoint != bpt)
13380 {
13381 struct breakpoint *related;
13382 struct watchpoint *w;
13383
13384 if (bpt->type == bp_watchpoint_scope)
13385 w = (struct watchpoint *) bpt->related_breakpoint;
13386 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13387 w = (struct watchpoint *) bpt;
13388 else
13389 w = NULL;
13390 if (w != NULL)
13391 watchpoint_del_at_next_stop (w);
13392
13393 /* Unlink bpt from the bpt->related_breakpoint ring. */
13394 for (related = bpt; related->related_breakpoint != bpt;
13395 related = related->related_breakpoint);
13396 related->related_breakpoint = bpt->related_breakpoint;
13397 bpt->related_breakpoint = bpt;
13398 }
13399
13400 /* watch_command_1 creates a watchpoint but only sets its number if
13401 update_watchpoint succeeds in creating its bp_locations. If there's
13402 a problem in that process, we'll be asked to delete the half-created
13403 watchpoint. In that case, don't announce the deletion. */
13404 if (bpt->number)
13405 gdb::observers::breakpoint_deleted.notify (bpt);
13406
13407 if (breakpoint_chain == bpt)
13408 breakpoint_chain = bpt->next;
13409
13410 ALL_BREAKPOINTS (b)
13411 if (b->next == bpt)
13412 {
13413 b->next = bpt->next;
13414 break;
13415 }
13416
13417 /* Be sure no bpstat's are pointing at the breakpoint after it's
13418 been freed. */
13419 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13420 in all threads for now. Note that we cannot just remove bpstats
13421 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13422 commands are associated with the bpstat; if we remove it here,
13423 then the later call to bpstat_do_actions (&stop_bpstat); in
13424 event-top.c won't do anything, and temporary breakpoints with
13425 commands won't work. */
13426
13427 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13428
13429 /* Now that breakpoint is removed from breakpoint list, update the
13430 global location list. This will remove locations that used to
13431 belong to this breakpoint. Do this before freeing the breakpoint
13432 itself, since remove_breakpoint looks at location's owner. It
13433 might be better design to have location completely
13434 self-contained, but it's not the case now. */
13435 update_global_location_list (UGLL_DONT_INSERT);
13436
13437 /* On the chance that someone will soon try again to delete this
13438 same bp, we mark it as deleted before freeing its storage. */
13439 bpt->type = bp_none;
13440 delete bpt;
13441 }
13442
13443 /* Iterator function to call a user-provided callback function once
13444 for each of B and its related breakpoints. */
13445
13446 static void
13447 iterate_over_related_breakpoints (struct breakpoint *b,
13448 gdb::function_view<void (breakpoint *)> function)
13449 {
13450 struct breakpoint *related;
13451
13452 related = b;
13453 do
13454 {
13455 struct breakpoint *next;
13456
13457 /* FUNCTION may delete RELATED. */
13458 next = related->related_breakpoint;
13459
13460 if (next == related)
13461 {
13462 /* RELATED is the last ring entry. */
13463 function (related);
13464
13465 /* FUNCTION may have deleted it, so we'd never reach back to
13466 B. There's nothing left to do anyway, so just break
13467 out. */
13468 break;
13469 }
13470 else
13471 function (related);
13472
13473 related = next;
13474 }
13475 while (related != b);
13476 }
13477
13478 static void
13479 delete_command (const char *arg, int from_tty)
13480 {
13481 struct breakpoint *b, *b_tmp;
13482
13483 dont_repeat ();
13484
13485 if (arg == 0)
13486 {
13487 int breaks_to_delete = 0;
13488
13489 /* Delete all breakpoints if no argument. Do not delete
13490 internal breakpoints, these have to be deleted with an
13491 explicit breakpoint number argument. */
13492 ALL_BREAKPOINTS (b)
13493 if (user_breakpoint_p (b))
13494 {
13495 breaks_to_delete = 1;
13496 break;
13497 }
13498
13499 /* Ask user only if there are some breakpoints to delete. */
13500 if (!from_tty
13501 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13502 {
13503 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13504 if (user_breakpoint_p (b))
13505 delete_breakpoint (b);
13506 }
13507 }
13508 else
13509 map_breakpoint_numbers
13510 (arg, [&] (breakpoint *br)
13511 {
13512 iterate_over_related_breakpoints (br, delete_breakpoint);
13513 });
13514 }
13515
13516 /* Return true if all locations of B bound to PSPACE are pending. If
13517 PSPACE is NULL, all locations of all program spaces are
13518 considered. */
13519
13520 static int
13521 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13522 {
13523 struct bp_location *loc;
13524
13525 for (loc = b->loc; loc != NULL; loc = loc->next)
13526 if ((pspace == NULL
13527 || loc->pspace == pspace)
13528 && !loc->shlib_disabled
13529 && !loc->pspace->executing_startup)
13530 return 0;
13531 return 1;
13532 }
13533
13534 /* Subroutine of update_breakpoint_locations to simplify it.
13535 Return non-zero if multiple fns in list LOC have the same name.
13536 Null names are ignored. */
13537
13538 static int
13539 ambiguous_names_p (struct bp_location *loc)
13540 {
13541 struct bp_location *l;
13542 htab_up htab (htab_create_alloc (13, htab_hash_string, streq_hash, NULL,
13543 xcalloc, xfree));
13544
13545 for (l = loc; l != NULL; l = l->next)
13546 {
13547 const char **slot;
13548 const char *name = l->function_name;
13549
13550 /* Allow for some names to be NULL, ignore them. */
13551 if (name == NULL)
13552 continue;
13553
13554 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
13555 INSERT);
13556 /* NOTE: We can assume slot != NULL here because xcalloc never
13557 returns NULL. */
13558 if (*slot != NULL)
13559 return 1;
13560 *slot = name;
13561 }
13562
13563 return 0;
13564 }
13565
13566 /* When symbols change, it probably means the sources changed as well,
13567 and it might mean the static tracepoint markers are no longer at
13568 the same address or line numbers they used to be at last we
13569 checked. Losing your static tracepoints whenever you rebuild is
13570 undesirable. This function tries to resync/rematch gdb static
13571 tracepoints with the markers on the target, for static tracepoints
13572 that have not been set by marker id. Static tracepoint that have
13573 been set by marker id are reset by marker id in breakpoint_re_set.
13574 The heuristic is:
13575
13576 1) For a tracepoint set at a specific address, look for a marker at
13577 the old PC. If one is found there, assume to be the same marker.
13578 If the name / string id of the marker found is different from the
13579 previous known name, assume that means the user renamed the marker
13580 in the sources, and output a warning.
13581
13582 2) For a tracepoint set at a given line number, look for a marker
13583 at the new address of the old line number. If one is found there,
13584 assume to be the same marker. If the name / string id of the
13585 marker found is different from the previous known name, assume that
13586 means the user renamed the marker in the sources, and output a
13587 warning.
13588
13589 3) If a marker is no longer found at the same address or line, it
13590 may mean the marker no longer exists. But it may also just mean
13591 the code changed a bit. Maybe the user added a few lines of code
13592 that made the marker move up or down (in line number terms). Ask
13593 the target for info about the marker with the string id as we knew
13594 it. If found, update line number and address in the matching
13595 static tracepoint. This will get confused if there's more than one
13596 marker with the same ID (possible in UST, although unadvised
13597 precisely because it confuses tools). */
13598
13599 static struct symtab_and_line
13600 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13601 {
13602 struct tracepoint *tp = (struct tracepoint *) b;
13603 struct static_tracepoint_marker marker;
13604 CORE_ADDR pc;
13605
13606 pc = sal.pc;
13607 if (sal.line)
13608 find_line_pc (sal.symtab, sal.line, &pc);
13609
13610 if (target_static_tracepoint_marker_at (pc, &marker))
13611 {
13612 if (tp->static_trace_marker_id != marker.str_id)
13613 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13614 b->number, tp->static_trace_marker_id.c_str (),
13615 marker.str_id.c_str ());
13616
13617 tp->static_trace_marker_id = std::move (marker.str_id);
13618
13619 return sal;
13620 }
13621
13622 /* Old marker wasn't found on target at lineno. Try looking it up
13623 by string ID. */
13624 if (!sal.explicit_pc
13625 && sal.line != 0
13626 && sal.symtab != NULL
13627 && !tp->static_trace_marker_id.empty ())
13628 {
13629 std::vector<static_tracepoint_marker> markers
13630 = target_static_tracepoint_markers_by_strid
13631 (tp->static_trace_marker_id.c_str ());
13632
13633 if (!markers.empty ())
13634 {
13635 struct symbol *sym;
13636 struct static_tracepoint_marker *tpmarker;
13637 struct ui_out *uiout = current_uiout;
13638 struct explicit_location explicit_loc;
13639
13640 tpmarker = &markers[0];
13641
13642 tp->static_trace_marker_id = std::move (tpmarker->str_id);
13643
13644 warning (_("marker for static tracepoint %d (%s) not "
13645 "found at previous line number"),
13646 b->number, tp->static_trace_marker_id.c_str ());
13647
13648 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
13649 sym = find_pc_sect_function (tpmarker->address, NULL);
13650 uiout->text ("Now in ");
13651 if (sym)
13652 {
13653 uiout->field_string ("func", sym->print_name (),
13654 function_name_style.style ());
13655 uiout->text (" at ");
13656 }
13657 uiout->field_string ("file",
13658 symtab_to_filename_for_display (sal2.symtab),
13659 file_name_style.style ());
13660 uiout->text (":");
13661
13662 if (uiout->is_mi_like_p ())
13663 {
13664 const char *fullname = symtab_to_fullname (sal2.symtab);
13665
13666 uiout->field_string ("fullname", fullname);
13667 }
13668
13669 uiout->field_signed ("line", sal2.line);
13670 uiout->text ("\n");
13671
13672 b->loc->line_number = sal2.line;
13673 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13674
13675 b->location.reset (NULL);
13676 initialize_explicit_location (&explicit_loc);
13677 explicit_loc.source_filename
13678 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13679 explicit_loc.line_offset.offset = b->loc->line_number;
13680 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13681 b->location = new_explicit_location (&explicit_loc);
13682
13683 /* Might be nice to check if function changed, and warn if
13684 so. */
13685 }
13686 }
13687 return sal;
13688 }
13689
13690 /* Returns 1 iff locations A and B are sufficiently same that
13691 we don't need to report breakpoint as changed. */
13692
13693 static int
13694 locations_are_equal (struct bp_location *a, struct bp_location *b)
13695 {
13696 while (a && b)
13697 {
13698 if (a->address != b->address)
13699 return 0;
13700
13701 if (a->shlib_disabled != b->shlib_disabled)
13702 return 0;
13703
13704 if (a->enabled != b->enabled)
13705 return 0;
13706
13707 if (a->disabled_by_cond != b->disabled_by_cond)
13708 return 0;
13709
13710 a = a->next;
13711 b = b->next;
13712 }
13713
13714 if ((a == NULL) != (b == NULL))
13715 return 0;
13716
13717 return 1;
13718 }
13719
13720 /* Split all locations of B that are bound to PSPACE out of B's
13721 location list to a separate list and return that list's head. If
13722 PSPACE is NULL, hoist out all locations of B. */
13723
13724 static struct bp_location *
13725 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13726 {
13727 struct bp_location head;
13728 struct bp_location *i = b->loc;
13729 struct bp_location **i_link = &b->loc;
13730 struct bp_location *hoisted = &head;
13731
13732 if (pspace == NULL)
13733 {
13734 i = b->loc;
13735 b->loc = NULL;
13736 return i;
13737 }
13738
13739 head.next = NULL;
13740
13741 while (i != NULL)
13742 {
13743 if (i->pspace == pspace)
13744 {
13745 *i_link = i->next;
13746 i->next = NULL;
13747 hoisted->next = i;
13748 hoisted = i;
13749 }
13750 else
13751 i_link = &i->next;
13752 i = *i_link;
13753 }
13754
13755 return head.next;
13756 }
13757
13758 /* Create new breakpoint locations for B (a hardware or software
13759 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13760 zero, then B is a ranged breakpoint. Only recreates locations for
13761 FILTER_PSPACE. Locations of other program spaces are left
13762 untouched. */
13763
13764 void
13765 update_breakpoint_locations (struct breakpoint *b,
13766 struct program_space *filter_pspace,
13767 gdb::array_view<const symtab_and_line> sals,
13768 gdb::array_view<const symtab_and_line> sals_end)
13769 {
13770 struct bp_location *existing_locations;
13771
13772 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
13773 {
13774 /* Ranged breakpoints have only one start location and one end
13775 location. */
13776 b->enable_state = bp_disabled;
13777 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13778 "multiple locations found\n"),
13779 b->number);
13780 return;
13781 }
13782
13783 /* If there's no new locations, and all existing locations are
13784 pending, don't do anything. This optimizes the common case where
13785 all locations are in the same shared library, that was unloaded.
13786 We'd like to retain the location, so that when the library is
13787 loaded again, we don't loose the enabled/disabled status of the
13788 individual locations. */
13789 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
13790 return;
13791
13792 existing_locations = hoist_existing_locations (b, filter_pspace);
13793
13794 for (const auto &sal : sals)
13795 {
13796 struct bp_location *new_loc;
13797
13798 switch_to_program_space_and_thread (sal.pspace);
13799
13800 new_loc = add_location_to_breakpoint (b, &sal);
13801
13802 /* Reparse conditions, they might contain references to the
13803 old symtab. */
13804 if (b->cond_string != NULL)
13805 {
13806 const char *s;
13807
13808 s = b->cond_string;
13809 try
13810 {
13811 new_loc->cond = parse_exp_1 (&s, sal.pc,
13812 block_for_pc (sal.pc),
13813 0);
13814 }
13815 catch (const gdb_exception_error &e)
13816 {
13817 new_loc->disabled_by_cond = true;
13818 }
13819 }
13820
13821 if (!sals_end.empty ())
13822 {
13823 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13824
13825 new_loc->length = end - sals[0].pc + 1;
13826 }
13827 }
13828
13829 /* If possible, carry over 'disable' status from existing
13830 breakpoints. */
13831 {
13832 struct bp_location *e = existing_locations;
13833 /* If there are multiple breakpoints with the same function name,
13834 e.g. for inline functions, comparing function names won't work.
13835 Instead compare pc addresses; this is just a heuristic as things
13836 may have moved, but in practice it gives the correct answer
13837 often enough until a better solution is found. */
13838 int have_ambiguous_names = ambiguous_names_p (b->loc);
13839
13840 for (; e; e = e->next)
13841 {
13842 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
13843 {
13844 struct bp_location *l = b->loc;
13845 if (have_ambiguous_names)
13846 {
13847 for (; l; l = l->next)
13848 {
13849 /* Ignore software vs hardware location type at
13850 this point, because with "set breakpoint
13851 auto-hw", after a re-set, locations that were
13852 hardware can end up as software, or vice versa.
13853 As mentioned above, this is an heuristic and in
13854 practice should give the correct answer often
13855 enough. */
13856 if (breakpoint_locations_match (e, l, true))
13857 {
13858 l->enabled = e->enabled;
13859 l->disabled_by_cond = e->disabled_by_cond;
13860 break;
13861 }
13862 }
13863 }
13864 else
13865 {
13866 for (; l; l = l->next)
13867 if (l->function_name
13868 && strcmp (e->function_name, l->function_name) == 0)
13869 {
13870 l->enabled = e->enabled;
13871 l->disabled_by_cond = e->disabled_by_cond;
13872 break;
13873 }
13874 }
13875 }
13876 }
13877 }
13878
13879 if (!locations_are_equal (existing_locations, b->loc))
13880 gdb::observers::breakpoint_modified.notify (b);
13881 }
13882
13883 /* Find the SaL locations corresponding to the given LOCATION.
13884 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13885
13886 static std::vector<symtab_and_line>
13887 location_to_sals (struct breakpoint *b, struct event_location *location,
13888 struct program_space *search_pspace, int *found)
13889 {
13890 struct gdb_exception exception;
13891
13892 gdb_assert (b->ops != NULL);
13893
13894 std::vector<symtab_and_line> sals;
13895
13896 try
13897 {
13898 sals = b->ops->decode_location (b, location, search_pspace);
13899 }
13900 catch (gdb_exception_error &e)
13901 {
13902 int not_found_and_ok = 0;
13903
13904 /* For pending breakpoints, it's expected that parsing will
13905 fail until the right shared library is loaded. User has
13906 already told to create pending breakpoints and don't need
13907 extra messages. If breakpoint is in bp_shlib_disabled
13908 state, then user already saw the message about that
13909 breakpoint being disabled, and don't want to see more
13910 errors. */
13911 if (e.error == NOT_FOUND_ERROR
13912 && (b->condition_not_parsed
13913 || (b->loc != NULL
13914 && search_pspace != NULL
13915 && b->loc->pspace != search_pspace)
13916 || (b->loc && b->loc->shlib_disabled)
13917 || (b->loc && b->loc->pspace->executing_startup)
13918 || b->enable_state == bp_disabled))
13919 not_found_and_ok = 1;
13920
13921 if (!not_found_and_ok)
13922 {
13923 /* We surely don't want to warn about the same breakpoint
13924 10 times. One solution, implemented here, is disable
13925 the breakpoint on error. Another solution would be to
13926 have separate 'warning emitted' flag. Since this
13927 happens only when a binary has changed, I don't know
13928 which approach is better. */
13929 b->enable_state = bp_disabled;
13930 throw;
13931 }
13932
13933 exception = std::move (e);
13934 }
13935
13936 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13937 {
13938 for (auto &sal : sals)
13939 resolve_sal_pc (&sal);
13940 if (b->condition_not_parsed && b->extra_string != NULL)
13941 {
13942 char *cond_string, *extra_string;
13943 int thread, task;
13944
13945 find_condition_and_thread_for_sals (sals, b->extra_string,
13946 &cond_string, &thread,
13947 &task, &extra_string);
13948 gdb_assert (b->cond_string == NULL);
13949 if (cond_string)
13950 b->cond_string = cond_string;
13951 b->thread = thread;
13952 b->task = task;
13953 if (extra_string)
13954 {
13955 xfree (b->extra_string);
13956 b->extra_string = extra_string;
13957 }
13958 b->condition_not_parsed = 0;
13959 }
13960
13961 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13962 sals[0] = update_static_tracepoint (b, sals[0]);
13963
13964 *found = 1;
13965 }
13966 else
13967 *found = 0;
13968
13969 return sals;
13970 }
13971
13972 /* The default re_set method, for typical hardware or software
13973 breakpoints. Reevaluate the breakpoint and recreate its
13974 locations. */
13975
13976 static void
13977 breakpoint_re_set_default (struct breakpoint *b)
13978 {
13979 struct program_space *filter_pspace = current_program_space;
13980 std::vector<symtab_and_line> expanded, expanded_end;
13981
13982 int found;
13983 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13984 filter_pspace, &found);
13985 if (found)
13986 expanded = std::move (sals);
13987
13988 if (b->location_range_end != NULL)
13989 {
13990 std::vector<symtab_and_line> sals_end
13991 = location_to_sals (b, b->location_range_end.get (),
13992 filter_pspace, &found);
13993 if (found)
13994 expanded_end = std::move (sals_end);
13995 }
13996
13997 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
13998 }
13999
14000 /* Default method for creating SALs from an address string. It basically
14001 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14002
14003 static void
14004 create_sals_from_location_default (struct event_location *location,
14005 struct linespec_result *canonical,
14006 enum bptype type_wanted)
14007 {
14008 parse_breakpoint_sals (location, canonical);
14009 }
14010
14011 /* Call create_breakpoints_sal for the given arguments. This is the default
14012 function for the `create_breakpoints_sal' method of
14013 breakpoint_ops. */
14014
14015 static void
14016 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14017 struct linespec_result *canonical,
14018 gdb::unique_xmalloc_ptr<char> cond_string,
14019 gdb::unique_xmalloc_ptr<char> extra_string,
14020 enum bptype type_wanted,
14021 enum bpdisp disposition,
14022 int thread,
14023 int task, int ignore_count,
14024 const struct breakpoint_ops *ops,
14025 int from_tty, int enabled,
14026 int internal, unsigned flags)
14027 {
14028 create_breakpoints_sal (gdbarch, canonical,
14029 std::move (cond_string),
14030 std::move (extra_string),
14031 type_wanted, disposition,
14032 thread, task, ignore_count, ops, from_tty,
14033 enabled, internal, flags);
14034 }
14035
14036 /* Decode the line represented by S by calling decode_line_full. This is the
14037 default function for the `decode_location' method of breakpoint_ops. */
14038
14039 static std::vector<symtab_and_line>
14040 decode_location_default (struct breakpoint *b,
14041 struct event_location *location,
14042 struct program_space *search_pspace)
14043 {
14044 struct linespec_result canonical;
14045
14046 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
14047 NULL, 0, &canonical, multiple_symbols_all,
14048 b->filter.get ());
14049
14050 /* We should get 0 or 1 resulting SALs. */
14051 gdb_assert (canonical.lsals.size () < 2);
14052
14053 if (!canonical.lsals.empty ())
14054 {
14055 const linespec_sals &lsal = canonical.lsals[0];
14056 return std::move (lsal.sals);
14057 }
14058 return {};
14059 }
14060
14061 /* Reset a breakpoint. */
14062
14063 static void
14064 breakpoint_re_set_one (breakpoint *b)
14065 {
14066 input_radix = b->input_radix;
14067 set_language (b->language);
14068
14069 b->ops->re_set (b);
14070 }
14071
14072 /* Re-set breakpoint locations for the current program space.
14073 Locations bound to other program spaces are left untouched. */
14074
14075 void
14076 breakpoint_re_set (void)
14077 {
14078 struct breakpoint *b, *b_tmp;
14079
14080 {
14081 scoped_restore_current_language save_language;
14082 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
14083 scoped_restore_current_pspace_and_thread restore_pspace_thread;
14084
14085 /* breakpoint_re_set_one sets the current_language to the language
14086 of the breakpoint it is resetting (see prepare_re_set_context)
14087 before re-evaluating the breakpoint's location. This change can
14088 unfortunately get undone by accident if the language_mode is set
14089 to auto, and we either switch frames, or more likely in this context,
14090 we select the current frame.
14091
14092 We prevent this by temporarily turning the language_mode to
14093 language_mode_manual. We restore it once all breakpoints
14094 have been reset. */
14095 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
14096 language_mode = language_mode_manual;
14097
14098 /* Note: we must not try to insert locations until after all
14099 breakpoints have been re-set. Otherwise, e.g., when re-setting
14100 breakpoint 1, we'd insert the locations of breakpoint 2, which
14101 hadn't been re-set yet, and thus may have stale locations. */
14102
14103 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14104 {
14105 try
14106 {
14107 breakpoint_re_set_one (b);
14108 }
14109 catch (const gdb_exception &ex)
14110 {
14111 exception_fprintf (gdb_stderr, ex,
14112 "Error in re-setting breakpoint %d: ",
14113 b->number);
14114 }
14115 }
14116
14117 jit_breakpoint_re_set ();
14118 }
14119
14120 create_overlay_event_breakpoint ();
14121 create_longjmp_master_breakpoint ();
14122 create_std_terminate_master_breakpoint ();
14123 create_exception_master_breakpoint ();
14124
14125 /* Now we can insert. */
14126 update_global_location_list (UGLL_MAY_INSERT);
14127 }
14128 \f
14129 /* Reset the thread number of this breakpoint:
14130
14131 - If the breakpoint is for all threads, leave it as-is.
14132 - Else, reset it to the current thread for inferior_ptid. */
14133 void
14134 breakpoint_re_set_thread (struct breakpoint *b)
14135 {
14136 if (b->thread != -1)
14137 {
14138 b->thread = inferior_thread ()->global_num;
14139
14140 /* We're being called after following a fork. The new fork is
14141 selected as current, and unless this was a vfork will have a
14142 different program space from the original thread. Reset that
14143 as well. */
14144 b->loc->pspace = current_program_space;
14145 }
14146 }
14147
14148 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14149 If from_tty is nonzero, it prints a message to that effect,
14150 which ends with a period (no newline). */
14151
14152 void
14153 set_ignore_count (int bptnum, int count, int from_tty)
14154 {
14155 struct breakpoint *b;
14156
14157 if (count < 0)
14158 count = 0;
14159
14160 ALL_BREAKPOINTS (b)
14161 if (b->number == bptnum)
14162 {
14163 if (is_tracepoint (b))
14164 {
14165 if (from_tty && count != 0)
14166 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14167 bptnum);
14168 return;
14169 }
14170
14171 b->ignore_count = count;
14172 if (from_tty)
14173 {
14174 if (count == 0)
14175 printf_filtered (_("Will stop next time "
14176 "breakpoint %d is reached."),
14177 bptnum);
14178 else if (count == 1)
14179 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14180 bptnum);
14181 else
14182 printf_filtered (_("Will ignore next %d "
14183 "crossings of breakpoint %d."),
14184 count, bptnum);
14185 }
14186 gdb::observers::breakpoint_modified.notify (b);
14187 return;
14188 }
14189
14190 error (_("No breakpoint number %d."), bptnum);
14191 }
14192
14193 /* Command to set ignore-count of breakpoint N to COUNT. */
14194
14195 static void
14196 ignore_command (const char *args, int from_tty)
14197 {
14198 const char *p = args;
14199 int num;
14200
14201 if (p == 0)
14202 error_no_arg (_("a breakpoint number"));
14203
14204 num = get_number (&p);
14205 if (num == 0)
14206 error (_("bad breakpoint number: '%s'"), args);
14207 if (*p == 0)
14208 error (_("Second argument (specified ignore-count) is missing."));
14209
14210 set_ignore_count (num,
14211 longest_to_int (value_as_long (parse_and_eval (p))),
14212 from_tty);
14213 if (from_tty)
14214 printf_filtered ("\n");
14215 }
14216 \f
14217
14218 /* Call FUNCTION on each of the breakpoints with numbers in the range
14219 defined by BP_NUM_RANGE (an inclusive range). */
14220
14221 static void
14222 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
14223 gdb::function_view<void (breakpoint *)> function)
14224 {
14225 if (bp_num_range.first == 0)
14226 {
14227 warning (_("bad breakpoint number at or near '%d'"),
14228 bp_num_range.first);
14229 }
14230 else
14231 {
14232 struct breakpoint *b, *tmp;
14233
14234 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
14235 {
14236 bool match = false;
14237
14238 ALL_BREAKPOINTS_SAFE (b, tmp)
14239 if (b->number == i)
14240 {
14241 match = true;
14242 function (b);
14243 break;
14244 }
14245 if (!match)
14246 printf_unfiltered (_("No breakpoint number %d.\n"), i);
14247 }
14248 }
14249 }
14250
14251 /* Call FUNCTION on each of the breakpoints whose numbers are given in
14252 ARGS. */
14253
14254 static void
14255 map_breakpoint_numbers (const char *args,
14256 gdb::function_view<void (breakpoint *)> function)
14257 {
14258 if (args == NULL || *args == '\0')
14259 error_no_arg (_("one or more breakpoint numbers"));
14260
14261 number_or_range_parser parser (args);
14262
14263 while (!parser.finished ())
14264 {
14265 int num = parser.get_number ();
14266 map_breakpoint_number_range (std::make_pair (num, num), function);
14267 }
14268 }
14269
14270 /* Return the breakpoint location structure corresponding to the
14271 BP_NUM and LOC_NUM values. */
14272
14273 static struct bp_location *
14274 find_location_by_number (int bp_num, int loc_num)
14275 {
14276 struct breakpoint *b;
14277
14278 ALL_BREAKPOINTS (b)
14279 if (b->number == bp_num)
14280 {
14281 break;
14282 }
14283
14284 if (!b || b->number != bp_num)
14285 error (_("Bad breakpoint number '%d'"), bp_num);
14286
14287 if (loc_num == 0)
14288 error (_("Bad breakpoint location number '%d'"), loc_num);
14289
14290 int n = 0;
14291 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
14292 if (++n == loc_num)
14293 return loc;
14294
14295 error (_("Bad breakpoint location number '%d'"), loc_num);
14296 }
14297
14298 /* Modes of operation for extract_bp_num. */
14299 enum class extract_bp_kind
14300 {
14301 /* Extracting a breakpoint number. */
14302 bp,
14303
14304 /* Extracting a location number. */
14305 loc,
14306 };
14307
14308 /* Extract a breakpoint or location number (as determined by KIND)
14309 from the string starting at START. TRAILER is a character which
14310 can be found after the number. If you don't want a trailer, use
14311 '\0'. If END_OUT is not NULL, it is set to point after the parsed
14312 string. This always returns a positive integer. */
14313
14314 static int
14315 extract_bp_num (extract_bp_kind kind, const char *start,
14316 int trailer, const char **end_out = NULL)
14317 {
14318 const char *end = start;
14319 int num = get_number_trailer (&end, trailer);
14320 if (num < 0)
14321 error (kind == extract_bp_kind::bp
14322 ? _("Negative breakpoint number '%.*s'")
14323 : _("Negative breakpoint location number '%.*s'"),
14324 int (end - start), start);
14325 if (num == 0)
14326 error (kind == extract_bp_kind::bp
14327 ? _("Bad breakpoint number '%.*s'")
14328 : _("Bad breakpoint location number '%.*s'"),
14329 int (end - start), start);
14330
14331 if (end_out != NULL)
14332 *end_out = end;
14333 return num;
14334 }
14335
14336 /* Extract a breakpoint or location range (as determined by KIND) in
14337 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14338 representing the (inclusive) range. The returned pair's elements
14339 are always positive integers. */
14340
14341 static std::pair<int, int>
14342 extract_bp_or_bp_range (extract_bp_kind kind,
14343 const std::string &arg,
14344 std::string::size_type arg_offset)
14345 {
14346 std::pair<int, int> range;
14347 const char *bp_loc = &arg[arg_offset];
14348 std::string::size_type dash = arg.find ('-', arg_offset);
14349 if (dash != std::string::npos)
14350 {
14351 /* bp_loc is a range (x-z). */
14352 if (arg.length () == dash + 1)
14353 error (kind == extract_bp_kind::bp
14354 ? _("Bad breakpoint number at or near: '%s'")
14355 : _("Bad breakpoint location number at or near: '%s'"),
14356 bp_loc);
14357
14358 const char *end;
14359 const char *start_first = bp_loc;
14360 const char *start_second = &arg[dash + 1];
14361 range.first = extract_bp_num (kind, start_first, '-');
14362 range.second = extract_bp_num (kind, start_second, '\0', &end);
14363
14364 if (range.first > range.second)
14365 error (kind == extract_bp_kind::bp
14366 ? _("Inverted breakpoint range at '%.*s'")
14367 : _("Inverted breakpoint location range at '%.*s'"),
14368 int (end - start_first), start_first);
14369 }
14370 else
14371 {
14372 /* bp_loc is a single value. */
14373 range.first = extract_bp_num (kind, bp_loc, '\0');
14374 range.second = range.first;
14375 }
14376 return range;
14377 }
14378
14379 /* Extract the breakpoint/location range specified by ARG. Returns
14380 the breakpoint range in BP_NUM_RANGE, and the location range in
14381 BP_LOC_RANGE.
14382
14383 ARG may be in any of the following forms:
14384
14385 x where 'x' is a breakpoint number.
14386 x-y where 'x' and 'y' specify a breakpoint numbers range.
14387 x.y where 'x' is a breakpoint number and 'y' a location number.
14388 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14389 location number range.
14390 */
14391
14392 static void
14393 extract_bp_number_and_location (const std::string &arg,
14394 std::pair<int, int> &bp_num_range,
14395 std::pair<int, int> &bp_loc_range)
14396 {
14397 std::string::size_type dot = arg.find ('.');
14398
14399 if (dot != std::string::npos)
14400 {
14401 /* Handle 'x.y' and 'x.y-z' cases. */
14402
14403 if (arg.length () == dot + 1 || dot == 0)
14404 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
14405
14406 bp_num_range.first
14407 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14408 bp_num_range.second = bp_num_range.first;
14409
14410 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14411 arg, dot + 1);
14412 }
14413 else
14414 {
14415 /* Handle x and x-y cases. */
14416
14417 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
14418 bp_loc_range.first = 0;
14419 bp_loc_range.second = 0;
14420 }
14421 }
14422
14423 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14424 specifies whether to enable or disable. */
14425
14426 static void
14427 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14428 {
14429 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14430 if (loc != NULL)
14431 {
14432 if (loc->disabled_by_cond && enable)
14433 error (_("Breakpoint %d's condition is invalid at location %d, "
14434 "cannot enable."), bp_num, loc_num);
14435
14436 if (loc->enabled != enable)
14437 {
14438 loc->enabled = enable;
14439 mark_breakpoint_location_modified (loc);
14440 }
14441 if (target_supports_enable_disable_tracepoint ()
14442 && current_trace_status ()->running && loc->owner
14443 && is_tracepoint (loc->owner))
14444 target_disable_tracepoint (loc);
14445 }
14446 update_global_location_list (UGLL_DONT_INSERT);
14447
14448 gdb::observers::breakpoint_modified.notify (loc->owner);
14449 }
14450
14451 /* Enable or disable a range of breakpoint locations. BP_NUM is the
14452 number of the breakpoint, and BP_LOC_RANGE specifies the
14453 (inclusive) range of location numbers of that breakpoint to
14454 enable/disable. ENABLE specifies whether to enable or disable the
14455 location. */
14456
14457 static void
14458 enable_disable_breakpoint_location_range (int bp_num,
14459 std::pair<int, int> &bp_loc_range,
14460 bool enable)
14461 {
14462 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14463 enable_disable_bp_num_loc (bp_num, i, enable);
14464 }
14465
14466 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14467 If from_tty is nonzero, it prints a message to that effect,
14468 which ends with a period (no newline). */
14469
14470 void
14471 disable_breakpoint (struct breakpoint *bpt)
14472 {
14473 /* Never disable a watchpoint scope breakpoint; we want to
14474 hit them when we leave scope so we can delete both the
14475 watchpoint and its scope breakpoint at that time. */
14476 if (bpt->type == bp_watchpoint_scope)
14477 return;
14478
14479 bpt->enable_state = bp_disabled;
14480
14481 /* Mark breakpoint locations modified. */
14482 mark_breakpoint_modified (bpt);
14483
14484 if (target_supports_enable_disable_tracepoint ()
14485 && current_trace_status ()->running && is_tracepoint (bpt))
14486 {
14487 struct bp_location *location;
14488
14489 for (location = bpt->loc; location; location = location->next)
14490 target_disable_tracepoint (location);
14491 }
14492
14493 update_global_location_list (UGLL_DONT_INSERT);
14494
14495 gdb::observers::breakpoint_modified.notify (bpt);
14496 }
14497
14498 /* Enable or disable the breakpoint(s) or breakpoint location(s)
14499 specified in ARGS. ARGS may be in any of the formats handled by
14500 extract_bp_number_and_location. ENABLE specifies whether to enable
14501 or disable the breakpoints/locations. */
14502
14503 static void
14504 enable_disable_command (const char *args, int from_tty, bool enable)
14505 {
14506 if (args == 0)
14507 {
14508 struct breakpoint *bpt;
14509
14510 ALL_BREAKPOINTS (bpt)
14511 if (user_breakpoint_p (bpt))
14512 {
14513 if (enable)
14514 enable_breakpoint (bpt);
14515 else
14516 disable_breakpoint (bpt);
14517 }
14518 }
14519 else
14520 {
14521 std::string num = extract_arg (&args);
14522
14523 while (!num.empty ())
14524 {
14525 std::pair<int, int> bp_num_range, bp_loc_range;
14526
14527 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14528
14529 if (bp_loc_range.first == bp_loc_range.second
14530 && bp_loc_range.first == 0)
14531 {
14532 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14533 map_breakpoint_number_range (bp_num_range,
14534 enable
14535 ? enable_breakpoint
14536 : disable_breakpoint);
14537 }
14538 else
14539 {
14540 /* Handle breakpoint ids with formats 'x.y' or
14541 'x.y-z'. */
14542 enable_disable_breakpoint_location_range
14543 (bp_num_range.first, bp_loc_range, enable);
14544 }
14545 num = extract_arg (&args);
14546 }
14547 }
14548 }
14549
14550 /* The disable command disables the specified breakpoints/locations
14551 (or all defined breakpoints) so they're no longer effective in
14552 stopping the inferior. ARGS may be in any of the forms defined in
14553 extract_bp_number_and_location. */
14554
14555 static void
14556 disable_command (const char *args, int from_tty)
14557 {
14558 enable_disable_command (args, from_tty, false);
14559 }
14560
14561 static void
14562 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14563 int count)
14564 {
14565 int target_resources_ok;
14566
14567 if (bpt->type == bp_hardware_breakpoint)
14568 {
14569 int i;
14570 i = hw_breakpoint_used_count ();
14571 target_resources_ok =
14572 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14573 i + 1, 0);
14574 if (target_resources_ok == 0)
14575 error (_("No hardware breakpoint support in the target."));
14576 else if (target_resources_ok < 0)
14577 error (_("Hardware breakpoints used exceeds limit."));
14578 }
14579
14580 if (is_watchpoint (bpt))
14581 {
14582 /* Initialize it just to avoid a GCC false warning. */
14583 enum enable_state orig_enable_state = bp_disabled;
14584
14585 try
14586 {
14587 struct watchpoint *w = (struct watchpoint *) bpt;
14588
14589 orig_enable_state = bpt->enable_state;
14590 bpt->enable_state = bp_enabled;
14591 update_watchpoint (w, 1 /* reparse */);
14592 }
14593 catch (const gdb_exception &e)
14594 {
14595 bpt->enable_state = orig_enable_state;
14596 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14597 bpt->number);
14598 return;
14599 }
14600 }
14601
14602 bpt->enable_state = bp_enabled;
14603
14604 /* Mark breakpoint locations modified. */
14605 mark_breakpoint_modified (bpt);
14606
14607 if (target_supports_enable_disable_tracepoint ()
14608 && current_trace_status ()->running && is_tracepoint (bpt))
14609 {
14610 struct bp_location *location;
14611
14612 for (location = bpt->loc; location; location = location->next)
14613 target_enable_tracepoint (location);
14614 }
14615
14616 bpt->disposition = disposition;
14617 bpt->enable_count = count;
14618 update_global_location_list (UGLL_MAY_INSERT);
14619
14620 gdb::observers::breakpoint_modified.notify (bpt);
14621 }
14622
14623
14624 void
14625 enable_breakpoint (struct breakpoint *bpt)
14626 {
14627 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14628 }
14629
14630 /* The enable command enables the specified breakpoints/locations (or
14631 all defined breakpoints) so they once again become (or continue to
14632 be) effective in stopping the inferior. ARGS may be in any of the
14633 forms defined in extract_bp_number_and_location. */
14634
14635 static void
14636 enable_command (const char *args, int from_tty)
14637 {
14638 enable_disable_command (args, from_tty, true);
14639 }
14640
14641 static void
14642 enable_once_command (const char *args, int from_tty)
14643 {
14644 map_breakpoint_numbers
14645 (args, [&] (breakpoint *b)
14646 {
14647 iterate_over_related_breakpoints
14648 (b, [&] (breakpoint *bpt)
14649 {
14650 enable_breakpoint_disp (bpt, disp_disable, 1);
14651 });
14652 });
14653 }
14654
14655 static void
14656 enable_count_command (const char *args, int from_tty)
14657 {
14658 int count;
14659
14660 if (args == NULL)
14661 error_no_arg (_("hit count"));
14662
14663 count = get_number (&args);
14664
14665 map_breakpoint_numbers
14666 (args, [&] (breakpoint *b)
14667 {
14668 iterate_over_related_breakpoints
14669 (b, [&] (breakpoint *bpt)
14670 {
14671 enable_breakpoint_disp (bpt, disp_disable, count);
14672 });
14673 });
14674 }
14675
14676 static void
14677 enable_delete_command (const char *args, int from_tty)
14678 {
14679 map_breakpoint_numbers
14680 (args, [&] (breakpoint *b)
14681 {
14682 iterate_over_related_breakpoints
14683 (b, [&] (breakpoint *bpt)
14684 {
14685 enable_breakpoint_disp (bpt, disp_del, 1);
14686 });
14687 });
14688 }
14689 \f
14690 /* Invalidate last known value of any hardware watchpoint if
14691 the memory which that value represents has been written to by
14692 GDB itself. */
14693
14694 static void
14695 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14696 CORE_ADDR addr, ssize_t len,
14697 const bfd_byte *data)
14698 {
14699 struct breakpoint *bp;
14700
14701 ALL_BREAKPOINTS (bp)
14702 if (bp->enable_state == bp_enabled
14703 && bp->type == bp_hardware_watchpoint)
14704 {
14705 struct watchpoint *wp = (struct watchpoint *) bp;
14706
14707 if (wp->val_valid && wp->val != nullptr)
14708 {
14709 struct bp_location *loc;
14710
14711 for (loc = bp->loc; loc != NULL; loc = loc->next)
14712 if (loc->loc_type == bp_loc_hardware_watchpoint
14713 && loc->address + loc->length > addr
14714 && addr + len > loc->address)
14715 {
14716 wp->val = NULL;
14717 wp->val_valid = false;
14718 }
14719 }
14720 }
14721 }
14722
14723 /* Create and insert a breakpoint for software single step. */
14724
14725 void
14726 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14727 const address_space *aspace,
14728 CORE_ADDR next_pc)
14729 {
14730 struct thread_info *tp = inferior_thread ();
14731 struct symtab_and_line sal;
14732 CORE_ADDR pc = next_pc;
14733
14734 if (tp->control.single_step_breakpoints == NULL)
14735 {
14736 tp->control.single_step_breakpoints
14737 = new_single_step_breakpoint (tp->global_num, gdbarch);
14738 }
14739
14740 sal = find_pc_line (pc, 0);
14741 sal.pc = pc;
14742 sal.section = find_pc_overlay (pc);
14743 sal.explicit_pc = 1;
14744 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14745
14746 update_global_location_list (UGLL_INSERT);
14747 }
14748
14749 /* Insert single step breakpoints according to the current state. */
14750
14751 int
14752 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14753 {
14754 struct regcache *regcache = get_current_regcache ();
14755 std::vector<CORE_ADDR> next_pcs;
14756
14757 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14758
14759 if (!next_pcs.empty ())
14760 {
14761 struct frame_info *frame = get_current_frame ();
14762 const address_space *aspace = get_frame_address_space (frame);
14763
14764 for (CORE_ADDR pc : next_pcs)
14765 insert_single_step_breakpoint (gdbarch, aspace, pc);
14766
14767 return 1;
14768 }
14769 else
14770 return 0;
14771 }
14772
14773 /* See breakpoint.h. */
14774
14775 int
14776 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14777 const address_space *aspace,
14778 CORE_ADDR pc)
14779 {
14780 struct bp_location *loc;
14781
14782 for (loc = bp->loc; loc != NULL; loc = loc->next)
14783 if (loc->inserted
14784 && breakpoint_location_address_match (loc, aspace, pc))
14785 return 1;
14786
14787 return 0;
14788 }
14789
14790 /* Check whether a software single-step breakpoint is inserted at
14791 PC. */
14792
14793 int
14794 single_step_breakpoint_inserted_here_p (const address_space *aspace,
14795 CORE_ADDR pc)
14796 {
14797 struct breakpoint *bpt;
14798
14799 ALL_BREAKPOINTS (bpt)
14800 {
14801 if (bpt->type == bp_single_step
14802 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14803 return 1;
14804 }
14805 return 0;
14806 }
14807
14808 /* Tracepoint-specific operations. */
14809
14810 /* Set tracepoint count to NUM. */
14811 static void
14812 set_tracepoint_count (int num)
14813 {
14814 tracepoint_count = num;
14815 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14816 }
14817
14818 static void
14819 trace_command (const char *arg, int from_tty)
14820 {
14821 event_location_up location = string_to_event_location (&arg,
14822 current_language);
14823 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
14824 (location.get (), true /* is_tracepoint */);
14825
14826 create_breakpoint (get_current_arch (),
14827 location.get (),
14828 NULL, 0, arg, 1 /* parse arg */,
14829 0 /* tempflag */,
14830 bp_tracepoint /* type_wanted */,
14831 0 /* Ignore count */,
14832 pending_break_support,
14833 ops,
14834 from_tty,
14835 1 /* enabled */,
14836 0 /* internal */, 0);
14837 }
14838
14839 static void
14840 ftrace_command (const char *arg, int from_tty)
14841 {
14842 event_location_up location = string_to_event_location (&arg,
14843 current_language);
14844 create_breakpoint (get_current_arch (),
14845 location.get (),
14846 NULL, 0, arg, 1 /* parse arg */,
14847 0 /* tempflag */,
14848 bp_fast_tracepoint /* type_wanted */,
14849 0 /* Ignore count */,
14850 pending_break_support,
14851 &tracepoint_breakpoint_ops,
14852 from_tty,
14853 1 /* enabled */,
14854 0 /* internal */, 0);
14855 }
14856
14857 /* strace command implementation. Creates a static tracepoint. */
14858
14859 static void
14860 strace_command (const char *arg, int from_tty)
14861 {
14862 struct breakpoint_ops *ops;
14863 event_location_up location;
14864
14865 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14866 or with a normal static tracepoint. */
14867 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14868 {
14869 ops = &strace_marker_breakpoint_ops;
14870 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
14871 }
14872 else
14873 {
14874 ops = &tracepoint_breakpoint_ops;
14875 location = string_to_event_location (&arg, current_language);
14876 }
14877
14878 create_breakpoint (get_current_arch (),
14879 location.get (),
14880 NULL, 0, arg, 1 /* parse arg */,
14881 0 /* tempflag */,
14882 bp_static_tracepoint /* type_wanted */,
14883 0 /* Ignore count */,
14884 pending_break_support,
14885 ops,
14886 from_tty,
14887 1 /* enabled */,
14888 0 /* internal */, 0);
14889 }
14890
14891 /* Set up a fake reader function that gets command lines from a linked
14892 list that was acquired during tracepoint uploading. */
14893
14894 static struct uploaded_tp *this_utp;
14895 static int next_cmd;
14896
14897 static char *
14898 read_uploaded_action (void)
14899 {
14900 char *rslt = nullptr;
14901
14902 if (next_cmd < this_utp->cmd_strings.size ())
14903 {
14904 rslt = this_utp->cmd_strings[next_cmd].get ();
14905 next_cmd++;
14906 }
14907
14908 return rslt;
14909 }
14910
14911 /* Given information about a tracepoint as recorded on a target (which
14912 can be either a live system or a trace file), attempt to create an
14913 equivalent GDB tracepoint. This is not a reliable process, since
14914 the target does not necessarily have all the information used when
14915 the tracepoint was originally defined. */
14916
14917 struct tracepoint *
14918 create_tracepoint_from_upload (struct uploaded_tp *utp)
14919 {
14920 const char *addr_str;
14921 char small_buf[100];
14922 struct tracepoint *tp;
14923
14924 if (utp->at_string)
14925 addr_str = utp->at_string.get ();
14926 else
14927 {
14928 /* In the absence of a source location, fall back to raw
14929 address. Since there is no way to confirm that the address
14930 means the same thing as when the trace was started, warn the
14931 user. */
14932 warning (_("Uploaded tracepoint %d has no "
14933 "source location, using raw address"),
14934 utp->number);
14935 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14936 addr_str = small_buf;
14937 }
14938
14939 /* There's not much we can do with a sequence of bytecodes. */
14940 if (utp->cond && !utp->cond_string)
14941 warning (_("Uploaded tracepoint %d condition "
14942 "has no source form, ignoring it"),
14943 utp->number);
14944
14945 event_location_up location = string_to_event_location (&addr_str,
14946 current_language);
14947 if (!create_breakpoint (get_current_arch (),
14948 location.get (),
14949 utp->cond_string.get (), -1, addr_str,
14950 0 /* parse cond/thread */,
14951 0 /* tempflag */,
14952 utp->type /* type_wanted */,
14953 0 /* Ignore count */,
14954 pending_break_support,
14955 &tracepoint_breakpoint_ops,
14956 0 /* from_tty */,
14957 utp->enabled /* enabled */,
14958 0 /* internal */,
14959 CREATE_BREAKPOINT_FLAGS_INSERTED))
14960 return NULL;
14961
14962 /* Get the tracepoint we just created. */
14963 tp = get_tracepoint (tracepoint_count);
14964 gdb_assert (tp != NULL);
14965
14966 if (utp->pass > 0)
14967 {
14968 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14969 tp->number);
14970
14971 trace_pass_command (small_buf, 0);
14972 }
14973
14974 /* If we have uploaded versions of the original commands, set up a
14975 special-purpose "reader" function and call the usual command line
14976 reader, then pass the result to the breakpoint command-setting
14977 function. */
14978 if (!utp->cmd_strings.empty ())
14979 {
14980 counted_command_line cmd_list;
14981
14982 this_utp = utp;
14983 next_cmd = 0;
14984
14985 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
14986
14987 breakpoint_set_commands (tp, std::move (cmd_list));
14988 }
14989 else if (!utp->actions.empty ()
14990 || !utp->step_actions.empty ())
14991 warning (_("Uploaded tracepoint %d actions "
14992 "have no source form, ignoring them"),
14993 utp->number);
14994
14995 /* Copy any status information that might be available. */
14996 tp->hit_count = utp->hit_count;
14997 tp->traceframe_usage = utp->traceframe_usage;
14998
14999 return tp;
15000 }
15001
15002 /* Print information on tracepoint number TPNUM_EXP, or all if
15003 omitted. */
15004
15005 static void
15006 info_tracepoints_command (const char *args, int from_tty)
15007 {
15008 struct ui_out *uiout = current_uiout;
15009 int num_printed;
15010
15011 num_printed = breakpoint_1 (args, false, is_tracepoint);
15012
15013 if (num_printed == 0)
15014 {
15015 if (args == NULL || *args == '\0')
15016 uiout->message ("No tracepoints.\n");
15017 else
15018 uiout->message ("No tracepoint matching '%s'.\n", args);
15019 }
15020
15021 default_collect_info ();
15022 }
15023
15024 /* The 'enable trace' command enables tracepoints.
15025 Not supported by all targets. */
15026 static void
15027 enable_trace_command (const char *args, int from_tty)
15028 {
15029 enable_command (args, from_tty);
15030 }
15031
15032 /* The 'disable trace' command disables tracepoints.
15033 Not supported by all targets. */
15034 static void
15035 disable_trace_command (const char *args, int from_tty)
15036 {
15037 disable_command (args, from_tty);
15038 }
15039
15040 /* Remove a tracepoint (or all if no argument). */
15041 static void
15042 delete_trace_command (const char *arg, int from_tty)
15043 {
15044 struct breakpoint *b, *b_tmp;
15045
15046 dont_repeat ();
15047
15048 if (arg == 0)
15049 {
15050 int breaks_to_delete = 0;
15051
15052 /* Delete all breakpoints if no argument.
15053 Do not delete internal or call-dummy breakpoints, these
15054 have to be deleted with an explicit breakpoint number
15055 argument. */
15056 ALL_TRACEPOINTS (b)
15057 if (is_tracepoint (b) && user_breakpoint_p (b))
15058 {
15059 breaks_to_delete = 1;
15060 break;
15061 }
15062
15063 /* Ask user only if there are some breakpoints to delete. */
15064 if (!from_tty
15065 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15066 {
15067 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15068 if (is_tracepoint (b) && user_breakpoint_p (b))
15069 delete_breakpoint (b);
15070 }
15071 }
15072 else
15073 map_breakpoint_numbers
15074 (arg, [&] (breakpoint *br)
15075 {
15076 iterate_over_related_breakpoints (br, delete_breakpoint);
15077 });
15078 }
15079
15080 /* Helper function for trace_pass_command. */
15081
15082 static void
15083 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15084 {
15085 tp->pass_count = count;
15086 gdb::observers::breakpoint_modified.notify (tp);
15087 if (from_tty)
15088 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15089 tp->number, count);
15090 }
15091
15092 /* Set passcount for tracepoint.
15093
15094 First command argument is passcount, second is tracepoint number.
15095 If tracepoint number omitted, apply to most recently defined.
15096 Also accepts special argument "all". */
15097
15098 static void
15099 trace_pass_command (const char *args, int from_tty)
15100 {
15101 struct tracepoint *t1;
15102 ULONGEST count;
15103
15104 if (args == 0 || *args == 0)
15105 error (_("passcount command requires an "
15106 "argument (count + optional TP num)"));
15107
15108 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
15109
15110 args = skip_spaces (args);
15111 if (*args && strncasecmp (args, "all", 3) == 0)
15112 {
15113 struct breakpoint *b;
15114
15115 args += 3; /* Skip special argument "all". */
15116 if (*args)
15117 error (_("Junk at end of arguments."));
15118
15119 ALL_TRACEPOINTS (b)
15120 {
15121 t1 = (struct tracepoint *) b;
15122 trace_pass_set_count (t1, count, from_tty);
15123 }
15124 }
15125 else if (*args == '\0')
15126 {
15127 t1 = get_tracepoint_by_number (&args, NULL);
15128 if (t1)
15129 trace_pass_set_count (t1, count, from_tty);
15130 }
15131 else
15132 {
15133 number_or_range_parser parser (args);
15134 while (!parser.finished ())
15135 {
15136 t1 = get_tracepoint_by_number (&args, &parser);
15137 if (t1)
15138 trace_pass_set_count (t1, count, from_tty);
15139 }
15140 }
15141 }
15142
15143 struct tracepoint *
15144 get_tracepoint (int num)
15145 {
15146 struct breakpoint *t;
15147
15148 ALL_TRACEPOINTS (t)
15149 if (t->number == num)
15150 return (struct tracepoint *) t;
15151
15152 return NULL;
15153 }
15154
15155 /* Find the tracepoint with the given target-side number (which may be
15156 different from the tracepoint number after disconnecting and
15157 reconnecting). */
15158
15159 struct tracepoint *
15160 get_tracepoint_by_number_on_target (int num)
15161 {
15162 struct breakpoint *b;
15163
15164 ALL_TRACEPOINTS (b)
15165 {
15166 struct tracepoint *t = (struct tracepoint *) b;
15167
15168 if (t->number_on_target == num)
15169 return t;
15170 }
15171
15172 return NULL;
15173 }
15174
15175 /* Utility: parse a tracepoint number and look it up in the list.
15176 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15177 If the argument is missing, the most recent tracepoint
15178 (tracepoint_count) is returned. */
15179
15180 struct tracepoint *
15181 get_tracepoint_by_number (const char **arg,
15182 number_or_range_parser *parser)
15183 {
15184 struct breakpoint *t;
15185 int tpnum;
15186 const char *instring = arg == NULL ? NULL : *arg;
15187
15188 if (parser != NULL)
15189 {
15190 gdb_assert (!parser->finished ());
15191 tpnum = parser->get_number ();
15192 }
15193 else if (arg == NULL || *arg == NULL || ! **arg)
15194 tpnum = tracepoint_count;
15195 else
15196 tpnum = get_number (arg);
15197
15198 if (tpnum <= 0)
15199 {
15200 if (instring && *instring)
15201 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15202 instring);
15203 else
15204 printf_filtered (_("No previous tracepoint\n"));
15205 return NULL;
15206 }
15207
15208 ALL_TRACEPOINTS (t)
15209 if (t->number == tpnum)
15210 {
15211 return (struct tracepoint *) t;
15212 }
15213
15214 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15215 return NULL;
15216 }
15217
15218 void
15219 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15220 {
15221 if (b->thread != -1)
15222 fprintf_unfiltered (fp, " thread %d", b->thread);
15223
15224 if (b->task != 0)
15225 fprintf_unfiltered (fp, " task %d", b->task);
15226
15227 fprintf_unfiltered (fp, "\n");
15228 }
15229
15230 /* Save information on user settable breakpoints (watchpoints, etc) to
15231 a new script file named FILENAME. If FILTER is non-NULL, call it
15232 on each breakpoint and only include the ones for which it returns
15233 true. */
15234
15235 static void
15236 save_breakpoints (const char *filename, int from_tty,
15237 bool (*filter) (const struct breakpoint *))
15238 {
15239 struct breakpoint *tp;
15240 int any = 0;
15241 int extra_trace_bits = 0;
15242
15243 if (filename == 0 || *filename == 0)
15244 error (_("Argument required (file name in which to save)"));
15245
15246 /* See if we have anything to save. */
15247 ALL_BREAKPOINTS (tp)
15248 {
15249 /* Skip internal and momentary breakpoints. */
15250 if (!user_breakpoint_p (tp))
15251 continue;
15252
15253 /* If we have a filter, only save the breakpoints it accepts. */
15254 if (filter && !filter (tp))
15255 continue;
15256
15257 any = 1;
15258
15259 if (is_tracepoint (tp))
15260 {
15261 extra_trace_bits = 1;
15262
15263 /* We can stop searching. */
15264 break;
15265 }
15266 }
15267
15268 if (!any)
15269 {
15270 warning (_("Nothing to save."));
15271 return;
15272 }
15273
15274 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
15275
15276 stdio_file fp;
15277
15278 if (!fp.open (expanded_filename.get (), "w"))
15279 error (_("Unable to open file '%s' for saving (%s)"),
15280 expanded_filename.get (), safe_strerror (errno));
15281
15282 if (extra_trace_bits)
15283 save_trace_state_variables (&fp);
15284
15285 ALL_BREAKPOINTS (tp)
15286 {
15287 /* Skip internal and momentary breakpoints. */
15288 if (!user_breakpoint_p (tp))
15289 continue;
15290
15291 /* If we have a filter, only save the breakpoints it accepts. */
15292 if (filter && !filter (tp))
15293 continue;
15294
15295 tp->ops->print_recreate (tp, &fp);
15296
15297 /* Note, we can't rely on tp->number for anything, as we can't
15298 assume the recreated breakpoint numbers will match. Use $bpnum
15299 instead. */
15300
15301 if (tp->cond_string)
15302 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15303
15304 if (tp->ignore_count)
15305 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15306
15307 if (tp->type != bp_dprintf && tp->commands)
15308 {
15309 fp.puts (" commands\n");
15310
15311 current_uiout->redirect (&fp);
15312 try
15313 {
15314 print_command_lines (current_uiout, tp->commands.get (), 2);
15315 }
15316 catch (const gdb_exception &ex)
15317 {
15318 current_uiout->redirect (NULL);
15319 throw;
15320 }
15321
15322 current_uiout->redirect (NULL);
15323 fp.puts (" end\n");
15324 }
15325
15326 if (tp->enable_state == bp_disabled)
15327 fp.puts ("disable $bpnum\n");
15328
15329 /* If this is a multi-location breakpoint, check if the locations
15330 should be individually disabled. Watchpoint locations are
15331 special, and not user visible. */
15332 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15333 {
15334 struct bp_location *loc;
15335 int n = 1;
15336
15337 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15338 if (!loc->enabled)
15339 fp.printf ("disable $bpnum.%d\n", n);
15340 }
15341 }
15342
15343 if (extra_trace_bits && *default_collect)
15344 fp.printf ("set default-collect %s\n", default_collect);
15345
15346 if (from_tty)
15347 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
15348 }
15349
15350 /* The `save breakpoints' command. */
15351
15352 static void
15353 save_breakpoints_command (const char *args, int from_tty)
15354 {
15355 save_breakpoints (args, from_tty, NULL);
15356 }
15357
15358 /* The `save tracepoints' command. */
15359
15360 static void
15361 save_tracepoints_command (const char *args, int from_tty)
15362 {
15363 save_breakpoints (args, from_tty, is_tracepoint);
15364 }
15365
15366 /* Create a vector of all tracepoints. */
15367
15368 std::vector<breakpoint *>
15369 all_tracepoints (void)
15370 {
15371 std::vector<breakpoint *> tp_vec;
15372 struct breakpoint *tp;
15373
15374 ALL_TRACEPOINTS (tp)
15375 {
15376 tp_vec.push_back (tp);
15377 }
15378
15379 return tp_vec;
15380 }
15381
15382 \f
15383 /* This help string is used to consolidate all the help string for specifying
15384 locations used by several commands. */
15385
15386 #define LOCATION_HELP_STRING \
15387 "Linespecs are colon-separated lists of location parameters, such as\n\
15388 source filename, function name, label name, and line number.\n\
15389 Example: To specify the start of a label named \"the_top\" in the\n\
15390 function \"fact\" in the file \"factorial.c\", use\n\
15391 \"factorial.c:fact:the_top\".\n\
15392 \n\
15393 Address locations begin with \"*\" and specify an exact address in the\n\
15394 program. Example: To specify the fourth byte past the start function\n\
15395 \"main\", use \"*main + 4\".\n\
15396 \n\
15397 Explicit locations are similar to linespecs but use an option/argument\n\
15398 syntax to specify location parameters.\n\
15399 Example: To specify the start of the label named \"the_top\" in the\n\
15400 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15401 -function fact -label the_top\".\n\
15402 \n\
15403 By default, a specified function is matched against the program's\n\
15404 functions in all scopes. For C++, this means in all namespaces and\n\
15405 classes. For Ada, this means in all packages. E.g., in C++,\n\
15406 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15407 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
15408 specified name as a complete fully-qualified name instead."
15409
15410 /* This help string is used for the break, hbreak, tbreak and thbreak
15411 commands. It is defined as a macro to prevent duplication.
15412 COMMAND should be a string constant containing the name of the
15413 command. */
15414
15415 #define BREAK_ARGS_HELP(command) \
15416 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
15417 \t[-force-condition] [if CONDITION]\n\
15418 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15419 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15420 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15421 `-probe-dtrace' (for a DTrace probe).\n\
15422 LOCATION may be a linespec, address, or explicit location as described\n\
15423 below.\n\
15424 \n\
15425 With no LOCATION, uses current execution address of the selected\n\
15426 stack frame. This is useful for breaking on return to a stack frame.\n\
15427 \n\
15428 THREADNUM is the number from \"info threads\".\n\
15429 CONDITION is a boolean expression.\n\
15430 \n\
15431 With the \"-force-condition\" flag, the condition is defined even when\n\
15432 it is invalid for all current locations.\n\
15433 \n" LOCATION_HELP_STRING "\n\n\
15434 Multiple breakpoints at one place are permitted, and useful if their\n\
15435 conditions are different.\n\
15436 \n\
15437 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15438
15439 /* List of subcommands for "catch". */
15440 static struct cmd_list_element *catch_cmdlist;
15441
15442 /* List of subcommands for "tcatch". */
15443 static struct cmd_list_element *tcatch_cmdlist;
15444
15445 void
15446 add_catch_command (const char *name, const char *docstring,
15447 cmd_const_sfunc_ftype *sfunc,
15448 completer_ftype *completer,
15449 void *user_data_catch,
15450 void *user_data_tcatch)
15451 {
15452 struct cmd_list_element *command;
15453
15454 command = add_cmd (name, class_breakpoint, docstring,
15455 &catch_cmdlist);
15456 set_cmd_sfunc (command, sfunc);
15457 set_cmd_context (command, user_data_catch);
15458 set_cmd_completer (command, completer);
15459
15460 command = add_cmd (name, class_breakpoint, docstring,
15461 &tcatch_cmdlist);
15462 set_cmd_sfunc (command, sfunc);
15463 set_cmd_context (command, user_data_tcatch);
15464 set_cmd_completer (command, completer);
15465 }
15466
15467 struct breakpoint *
15468 iterate_over_breakpoints (gdb::function_view<bool (breakpoint *)> callback)
15469 {
15470 struct breakpoint *b, *b_tmp;
15471
15472 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15473 {
15474 if (callback (b))
15475 return b;
15476 }
15477
15478 return NULL;
15479 }
15480
15481 /* Zero if any of the breakpoint's locations could be a location where
15482 functions have been inlined, nonzero otherwise. */
15483
15484 static int
15485 is_non_inline_function (struct breakpoint *b)
15486 {
15487 /* The shared library event breakpoint is set on the address of a
15488 non-inline function. */
15489 if (b->type == bp_shlib_event)
15490 return 1;
15491
15492 return 0;
15493 }
15494
15495 /* Nonzero if the specified PC cannot be a location where functions
15496 have been inlined. */
15497
15498 int
15499 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
15500 const struct target_waitstatus *ws)
15501 {
15502 struct breakpoint *b;
15503 struct bp_location *bl;
15504
15505 ALL_BREAKPOINTS (b)
15506 {
15507 if (!is_non_inline_function (b))
15508 continue;
15509
15510 for (bl = b->loc; bl != NULL; bl = bl->next)
15511 {
15512 if (!bl->shlib_disabled
15513 && bpstat_check_location (bl, aspace, pc, ws))
15514 return 1;
15515 }
15516 }
15517
15518 return 0;
15519 }
15520
15521 /* Remove any references to OBJFILE which is going to be freed. */
15522
15523 void
15524 breakpoint_free_objfile (struct objfile *objfile)
15525 {
15526 struct bp_location **locp, *loc;
15527
15528 ALL_BP_LOCATIONS (loc, locp)
15529 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15530 loc->symtab = NULL;
15531 }
15532
15533 void
15534 initialize_breakpoint_ops (void)
15535 {
15536 static int initialized = 0;
15537
15538 struct breakpoint_ops *ops;
15539
15540 if (initialized)
15541 return;
15542 initialized = 1;
15543
15544 /* The breakpoint_ops structure to be inherit by all kinds of
15545 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15546 internal and momentary breakpoints, etc.). */
15547 ops = &bkpt_base_breakpoint_ops;
15548 *ops = base_breakpoint_ops;
15549 ops->re_set = bkpt_re_set;
15550 ops->insert_location = bkpt_insert_location;
15551 ops->remove_location = bkpt_remove_location;
15552 ops->breakpoint_hit = bkpt_breakpoint_hit;
15553 ops->create_sals_from_location = bkpt_create_sals_from_location;
15554 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15555 ops->decode_location = bkpt_decode_location;
15556
15557 /* The breakpoint_ops structure to be used in regular breakpoints. */
15558 ops = &bkpt_breakpoint_ops;
15559 *ops = bkpt_base_breakpoint_ops;
15560 ops->re_set = bkpt_re_set;
15561 ops->resources_needed = bkpt_resources_needed;
15562 ops->print_it = bkpt_print_it;
15563 ops->print_mention = bkpt_print_mention;
15564 ops->print_recreate = bkpt_print_recreate;
15565
15566 /* Ranged breakpoints. */
15567 ops = &ranged_breakpoint_ops;
15568 *ops = bkpt_breakpoint_ops;
15569 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15570 ops->resources_needed = resources_needed_ranged_breakpoint;
15571 ops->print_it = print_it_ranged_breakpoint;
15572 ops->print_one = print_one_ranged_breakpoint;
15573 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15574 ops->print_mention = print_mention_ranged_breakpoint;
15575 ops->print_recreate = print_recreate_ranged_breakpoint;
15576
15577 /* Internal breakpoints. */
15578 ops = &internal_breakpoint_ops;
15579 *ops = bkpt_base_breakpoint_ops;
15580 ops->re_set = internal_bkpt_re_set;
15581 ops->check_status = internal_bkpt_check_status;
15582 ops->print_it = internal_bkpt_print_it;
15583 ops->print_mention = internal_bkpt_print_mention;
15584
15585 /* Momentary breakpoints. */
15586 ops = &momentary_breakpoint_ops;
15587 *ops = bkpt_base_breakpoint_ops;
15588 ops->re_set = momentary_bkpt_re_set;
15589 ops->check_status = momentary_bkpt_check_status;
15590 ops->print_it = momentary_bkpt_print_it;
15591 ops->print_mention = momentary_bkpt_print_mention;
15592
15593 /* Probe breakpoints. */
15594 ops = &bkpt_probe_breakpoint_ops;
15595 *ops = bkpt_breakpoint_ops;
15596 ops->insert_location = bkpt_probe_insert_location;
15597 ops->remove_location = bkpt_probe_remove_location;
15598 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15599 ops->decode_location = bkpt_probe_decode_location;
15600
15601 /* Watchpoints. */
15602 ops = &watchpoint_breakpoint_ops;
15603 *ops = base_breakpoint_ops;
15604 ops->re_set = re_set_watchpoint;
15605 ops->insert_location = insert_watchpoint;
15606 ops->remove_location = remove_watchpoint;
15607 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15608 ops->check_status = check_status_watchpoint;
15609 ops->resources_needed = resources_needed_watchpoint;
15610 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15611 ops->print_it = print_it_watchpoint;
15612 ops->print_mention = print_mention_watchpoint;
15613 ops->print_recreate = print_recreate_watchpoint;
15614 ops->explains_signal = explains_signal_watchpoint;
15615
15616 /* Masked watchpoints. */
15617 ops = &masked_watchpoint_breakpoint_ops;
15618 *ops = watchpoint_breakpoint_ops;
15619 ops->insert_location = insert_masked_watchpoint;
15620 ops->remove_location = remove_masked_watchpoint;
15621 ops->resources_needed = resources_needed_masked_watchpoint;
15622 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15623 ops->print_it = print_it_masked_watchpoint;
15624 ops->print_one_detail = print_one_detail_masked_watchpoint;
15625 ops->print_mention = print_mention_masked_watchpoint;
15626 ops->print_recreate = print_recreate_masked_watchpoint;
15627
15628 /* Tracepoints. */
15629 ops = &tracepoint_breakpoint_ops;
15630 *ops = base_breakpoint_ops;
15631 ops->re_set = tracepoint_re_set;
15632 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15633 ops->print_one_detail = tracepoint_print_one_detail;
15634 ops->print_mention = tracepoint_print_mention;
15635 ops->print_recreate = tracepoint_print_recreate;
15636 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15637 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15638 ops->decode_location = tracepoint_decode_location;
15639
15640 /* Probe tracepoints. */
15641 ops = &tracepoint_probe_breakpoint_ops;
15642 *ops = tracepoint_breakpoint_ops;
15643 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15644 ops->decode_location = tracepoint_probe_decode_location;
15645
15646 /* Static tracepoints with marker (`-m'). */
15647 ops = &strace_marker_breakpoint_ops;
15648 *ops = tracepoint_breakpoint_ops;
15649 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15650 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15651 ops->decode_location = strace_marker_decode_location;
15652
15653 /* Fork catchpoints. */
15654 ops = &catch_fork_breakpoint_ops;
15655 *ops = base_breakpoint_ops;
15656 ops->insert_location = insert_catch_fork;
15657 ops->remove_location = remove_catch_fork;
15658 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15659 ops->print_it = print_it_catch_fork;
15660 ops->print_one = print_one_catch_fork;
15661 ops->print_mention = print_mention_catch_fork;
15662 ops->print_recreate = print_recreate_catch_fork;
15663
15664 /* Vfork catchpoints. */
15665 ops = &catch_vfork_breakpoint_ops;
15666 *ops = base_breakpoint_ops;
15667 ops->insert_location = insert_catch_vfork;
15668 ops->remove_location = remove_catch_vfork;
15669 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15670 ops->print_it = print_it_catch_vfork;
15671 ops->print_one = print_one_catch_vfork;
15672 ops->print_mention = print_mention_catch_vfork;
15673 ops->print_recreate = print_recreate_catch_vfork;
15674
15675 /* Exec catchpoints. */
15676 ops = &catch_exec_breakpoint_ops;
15677 *ops = base_breakpoint_ops;
15678 ops->insert_location = insert_catch_exec;
15679 ops->remove_location = remove_catch_exec;
15680 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15681 ops->print_it = print_it_catch_exec;
15682 ops->print_one = print_one_catch_exec;
15683 ops->print_mention = print_mention_catch_exec;
15684 ops->print_recreate = print_recreate_catch_exec;
15685
15686 /* Solib-related catchpoints. */
15687 ops = &catch_solib_breakpoint_ops;
15688 *ops = base_breakpoint_ops;
15689 ops->insert_location = insert_catch_solib;
15690 ops->remove_location = remove_catch_solib;
15691 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15692 ops->check_status = check_status_catch_solib;
15693 ops->print_it = print_it_catch_solib;
15694 ops->print_one = print_one_catch_solib;
15695 ops->print_mention = print_mention_catch_solib;
15696 ops->print_recreate = print_recreate_catch_solib;
15697
15698 ops = &dprintf_breakpoint_ops;
15699 *ops = bkpt_base_breakpoint_ops;
15700 ops->re_set = dprintf_re_set;
15701 ops->resources_needed = bkpt_resources_needed;
15702 ops->print_it = bkpt_print_it;
15703 ops->print_mention = bkpt_print_mention;
15704 ops->print_recreate = dprintf_print_recreate;
15705 ops->after_condition_true = dprintf_after_condition_true;
15706 ops->breakpoint_hit = dprintf_breakpoint_hit;
15707 }
15708
15709 /* Chain containing all defined "enable breakpoint" subcommands. */
15710
15711 static struct cmd_list_element *enablebreaklist = NULL;
15712
15713 /* See breakpoint.h. */
15714
15715 cmd_list_element *commands_cmd_element = nullptr;
15716
15717 void _initialize_breakpoint ();
15718 void
15719 _initialize_breakpoint ()
15720 {
15721 struct cmd_list_element *c;
15722
15723 initialize_breakpoint_ops ();
15724
15725 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib);
15726 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile);
15727 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change);
15728
15729 breakpoint_chain = 0;
15730 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15731 before a breakpoint is set. */
15732 breakpoint_count = 0;
15733
15734 tracepoint_count = 0;
15735
15736 add_com ("ignore", class_breakpoint, ignore_command, _("\
15737 Set ignore-count of breakpoint number N to COUNT.\n\
15738 Usage is `ignore N COUNT'."));
15739
15740 commands_cmd_element = add_com ("commands", class_breakpoint,
15741 commands_command, _("\
15742 Set commands to be executed when the given breakpoints are hit.\n\
15743 Give a space-separated breakpoint list as argument after \"commands\".\n\
15744 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15745 (e.g. `5-7').\n\
15746 With no argument, the targeted breakpoint is the last one set.\n\
15747 The commands themselves follow starting on the next line.\n\
15748 Type a line containing \"end\" to indicate the end of them.\n\
15749 Give \"silent\" as the first line to make the breakpoint silent;\n\
15750 then no output is printed when it is hit, except what the commands print."));
15751
15752 const auto cc_opts = make_condition_command_options_def_group (nullptr);
15753 static std::string condition_command_help
15754 = gdb::option::build_help (_("\
15755 Specify breakpoint number N to break only if COND is true.\n\
15756 Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
15757 is an expression to be evaluated whenever breakpoint N is reached.\n\
15758 \n\
15759 Options:\n\
15760 %OPTIONS%"), cc_opts);
15761
15762 c = add_com ("condition", class_breakpoint, condition_command,
15763 condition_command_help.c_str ());
15764 set_cmd_completer_handle_brkchars (c, condition_completer);
15765
15766 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15767 Set a temporary breakpoint.\n\
15768 Like \"break\" except the breakpoint is only temporary,\n\
15769 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15770 by using \"enable delete\" on the breakpoint number.\n\
15771 \n"
15772 BREAK_ARGS_HELP ("tbreak")));
15773 set_cmd_completer (c, location_completer);
15774
15775 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15776 Set a hardware assisted breakpoint.\n\
15777 Like \"break\" except the breakpoint requires hardware support,\n\
15778 some target hardware may not have this support.\n\
15779 \n"
15780 BREAK_ARGS_HELP ("hbreak")));
15781 set_cmd_completer (c, location_completer);
15782
15783 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15784 Set a temporary hardware assisted breakpoint.\n\
15785 Like \"hbreak\" except the breakpoint is only temporary,\n\
15786 so it will be deleted when hit.\n\
15787 \n"
15788 BREAK_ARGS_HELP ("thbreak")));
15789 set_cmd_completer (c, location_completer);
15790
15791 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15792 Enable all or some breakpoints.\n\
15793 Usage: enable [BREAKPOINTNUM]...\n\
15794 Give breakpoint numbers (separated by spaces) as arguments.\n\
15795 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15796 This is used to cancel the effect of the \"disable\" command.\n\
15797 With a subcommand you can enable temporarily."),
15798 &enablelist, "enable ", 1, &cmdlist);
15799
15800 add_com_alias ("en", "enable", class_breakpoint, 1);
15801
15802 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15803 Enable all or some breakpoints.\n\
15804 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
15805 Give breakpoint numbers (separated by spaces) as arguments.\n\
15806 This is used to cancel the effect of the \"disable\" command.\n\
15807 May be abbreviated to simply \"enable\"."),
15808 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
15809
15810 add_cmd ("once", no_class, enable_once_command, _("\
15811 Enable some breakpoints for one hit.\n\
15812 Usage: enable breakpoints once BREAKPOINTNUM...\n\
15813 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15814 &enablebreaklist);
15815
15816 add_cmd ("delete", no_class, enable_delete_command, _("\
15817 Enable some breakpoints and delete when hit.\n\
15818 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
15819 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15820 &enablebreaklist);
15821
15822 add_cmd ("count", no_class, enable_count_command, _("\
15823 Enable some breakpoints for COUNT hits.\n\
15824 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
15825 If a breakpoint is hit while enabled in this fashion,\n\
15826 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15827 &enablebreaklist);
15828
15829 add_cmd ("delete", no_class, enable_delete_command, _("\
15830 Enable some breakpoints and delete when hit.\n\
15831 Usage: enable delete BREAKPOINTNUM...\n\
15832 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15833 &enablelist);
15834
15835 add_cmd ("once", no_class, enable_once_command, _("\
15836 Enable some breakpoints for one hit.\n\
15837 Usage: enable once BREAKPOINTNUM...\n\
15838 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15839 &enablelist);
15840
15841 add_cmd ("count", no_class, enable_count_command, _("\
15842 Enable some breakpoints for COUNT hits.\n\
15843 Usage: enable count COUNT BREAKPOINTNUM...\n\
15844 If a breakpoint is hit while enabled in this fashion,\n\
15845 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15846 &enablelist);
15847
15848 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15849 Disable all or some breakpoints.\n\
15850 Usage: disable [BREAKPOINTNUM]...\n\
15851 Arguments are breakpoint numbers with spaces in between.\n\
15852 To disable all breakpoints, give no argument.\n\
15853 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15854 &disablelist, "disable ", 1, &cmdlist);
15855 add_com_alias ("dis", "disable", class_breakpoint, 1);
15856 add_com_alias ("disa", "disable", class_breakpoint, 1);
15857
15858 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
15859 Disable all or some breakpoints.\n\
15860 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
15861 Arguments are breakpoint numbers with spaces in between.\n\
15862 To disable all breakpoints, give no argument.\n\
15863 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15864 This command may be abbreviated \"disable\"."),
15865 &disablelist);
15866
15867 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15868 Delete all or some breakpoints.\n\
15869 Usage: delete [BREAKPOINTNUM]...\n\
15870 Arguments are breakpoint numbers with spaces in between.\n\
15871 To delete all breakpoints, give no argument.\n\
15872 \n\
15873 Also a prefix command for deletion of other GDB objects."),
15874 &deletelist, "delete ", 1, &cmdlist);
15875 add_com_alias ("d", "delete", class_breakpoint, 1);
15876 add_com_alias ("del", "delete", class_breakpoint, 1);
15877
15878 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
15879 Delete all or some breakpoints or auto-display expressions.\n\
15880 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
15881 Arguments are breakpoint numbers with spaces in between.\n\
15882 To delete all breakpoints, give no argument.\n\
15883 This command may be abbreviated \"delete\"."),
15884 &deletelist);
15885
15886 add_com ("clear", class_breakpoint, clear_command, _("\
15887 Clear breakpoint at specified location.\n\
15888 Argument may be a linespec, explicit, or address location as described below.\n\
15889 \n\
15890 With no argument, clears all breakpoints in the line that the selected frame\n\
15891 is executing in.\n"
15892 "\n" LOCATION_HELP_STRING "\n\n\
15893 See also the \"delete\" command which clears breakpoints by number."));
15894 add_com_alias ("cl", "clear", class_breakpoint, 1);
15895
15896 c = add_com ("break", class_breakpoint, break_command, _("\
15897 Set breakpoint at specified location.\n"
15898 BREAK_ARGS_HELP ("break")));
15899 set_cmd_completer (c, location_completer);
15900
15901 add_com_alias ("b", "break", class_run, 1);
15902 add_com_alias ("br", "break", class_run, 1);
15903 add_com_alias ("bre", "break", class_run, 1);
15904 add_com_alias ("brea", "break", class_run, 1);
15905
15906 if (dbx_commands)
15907 {
15908 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15909 Break in function/address or break at a line in the current file."),
15910 &stoplist, "stop ", 1, &cmdlist);
15911 add_cmd ("in", class_breakpoint, stopin_command,
15912 _("Break in function or address."), &stoplist);
15913 add_cmd ("at", class_breakpoint, stopat_command,
15914 _("Break at a line in the current file."), &stoplist);
15915 add_com ("status", class_info, info_breakpoints_command, _("\
15916 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15917 The \"Type\" column indicates one of:\n\
15918 \tbreakpoint - normal breakpoint\n\
15919 \twatchpoint - watchpoint\n\
15920 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15921 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15922 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15923 address and file/line number respectively.\n\
15924 \n\
15925 Convenience variable \"$_\" and default examine address for \"x\"\n\
15926 are set to the address of the last breakpoint listed unless the command\n\
15927 is prefixed with \"server \".\n\n\
15928 Convenience variable \"$bpnum\" contains the number of the last\n\
15929 breakpoint set."));
15930 }
15931
15932 add_info ("breakpoints", info_breakpoints_command, _("\
15933 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15934 The \"Type\" column indicates one of:\n\
15935 \tbreakpoint - normal breakpoint\n\
15936 \twatchpoint - watchpoint\n\
15937 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15938 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15939 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15940 address and file/line number respectively.\n\
15941 \n\
15942 Convenience variable \"$_\" and default examine address for \"x\"\n\
15943 are set to the address of the last breakpoint listed unless the command\n\
15944 is prefixed with \"server \".\n\n\
15945 Convenience variable \"$bpnum\" contains the number of the last\n\
15946 breakpoint set."));
15947
15948 add_info_alias ("b", "breakpoints", 1);
15949
15950 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15951 Status of all breakpoints, or breakpoint number NUMBER.\n\
15952 The \"Type\" column indicates one of:\n\
15953 \tbreakpoint - normal breakpoint\n\
15954 \twatchpoint - watchpoint\n\
15955 \tlongjmp - internal breakpoint used to step through longjmp()\n\
15956 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15957 \tuntil - internal breakpoint used by the \"until\" command\n\
15958 \tfinish - internal breakpoint used by the \"finish\" command\n\
15959 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15960 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15961 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15962 address and file/line number respectively.\n\
15963 \n\
15964 Convenience variable \"$_\" and default examine address for \"x\"\n\
15965 are set to the address of the last breakpoint listed unless the command\n\
15966 is prefixed with \"server \".\n\n\
15967 Convenience variable \"$bpnum\" contains the number of the last\n\
15968 breakpoint set."),
15969 &maintenanceinfolist);
15970
15971 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
15972 Set catchpoints to catch events."),
15973 &catch_cmdlist, "catch ",
15974 0/*allow-unknown*/, &cmdlist);
15975
15976 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
15977 Set temporary catchpoints to catch events."),
15978 &tcatch_cmdlist, "tcatch ",
15979 0/*allow-unknown*/, &cmdlist);
15980
15981 add_catch_command ("fork", _("Catch calls to fork."),
15982 catch_fork_command_1,
15983 NULL,
15984 (void *) (uintptr_t) catch_fork_permanent,
15985 (void *) (uintptr_t) catch_fork_temporary);
15986 add_catch_command ("vfork", _("Catch calls to vfork."),
15987 catch_fork_command_1,
15988 NULL,
15989 (void *) (uintptr_t) catch_vfork_permanent,
15990 (void *) (uintptr_t) catch_vfork_temporary);
15991 add_catch_command ("exec", _("Catch calls to exec."),
15992 catch_exec_command_1,
15993 NULL,
15994 CATCH_PERMANENT,
15995 CATCH_TEMPORARY);
15996 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15997 Usage: catch load [REGEX]\n\
15998 If REGEX is given, only stop for libraries matching the regular expression."),
15999 catch_load_command_1,
16000 NULL,
16001 CATCH_PERMANENT,
16002 CATCH_TEMPORARY);
16003 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16004 Usage: catch unload [REGEX]\n\
16005 If REGEX is given, only stop for libraries matching the regular expression."),
16006 catch_unload_command_1,
16007 NULL,
16008 CATCH_PERMANENT,
16009 CATCH_TEMPORARY);
16010
16011 const auto opts = make_watch_options_def_group (nullptr);
16012
16013 static const std::string watch_help = gdb::option::build_help (_("\
16014 Set a watchpoint for EXPRESSION.\n\
16015 Usage: watch [-location] EXPRESSION\n\
16016 \n\
16017 Options:\n\
16018 %OPTIONS%\n\
16019 \n\
16020 A watchpoint stops execution of your program whenever the value of\n\
16021 an expression changes."), opts);
16022 c = add_com ("watch", class_breakpoint, watch_command,
16023 watch_help.c_str ());
16024 set_cmd_completer_handle_brkchars (c, watch_command_completer);
16025
16026 static const std::string rwatch_help = gdb::option::build_help (_("\
16027 Set a read watchpoint for EXPRESSION.\n\
16028 Usage: rwatch [-location] EXPRESSION\n\
16029 \n\
16030 Options:\n\
16031 %OPTIONS%\n\
16032 \n\
16033 A read watchpoint stops execution of your program whenever the value of\n\
16034 an expression is read."), opts);
16035 c = add_com ("rwatch", class_breakpoint, rwatch_command,
16036 rwatch_help.c_str ());
16037 set_cmd_completer_handle_brkchars (c, watch_command_completer);
16038
16039 static const std::string awatch_help = gdb::option::build_help (_("\
16040 Set an access watchpoint for EXPRESSION.\n\
16041 Usage: awatch [-location] EXPRESSION\n\
16042 \n\
16043 Options:\n\
16044 %OPTIONS%\n\
16045 \n\
16046 An access watchpoint stops execution of your program whenever the value\n\
16047 of an expression is either read or written."), opts);
16048 c = add_com ("awatch", class_breakpoint, awatch_command,
16049 awatch_help.c_str ());
16050 set_cmd_completer_handle_brkchars (c, watch_command_completer);
16051
16052 add_info ("watchpoints", info_watchpoints_command, _("\
16053 Status of specified watchpoints (all watchpoints if no argument)."));
16054
16055 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16056 respond to changes - contrary to the description. */
16057 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16058 &can_use_hw_watchpoints, _("\
16059 Set debugger's willingness to use watchpoint hardware."), _("\
16060 Show debugger's willingness to use watchpoint hardware."), _("\
16061 If zero, gdb will not use hardware for new watchpoints, even if\n\
16062 such is available. (However, any hardware watchpoints that were\n\
16063 created before setting this to nonzero, will continue to use watchpoint\n\
16064 hardware.)"),
16065 NULL,
16066 show_can_use_hw_watchpoints,
16067 &setlist, &showlist);
16068
16069 can_use_hw_watchpoints = 1;
16070
16071 /* Tracepoint manipulation commands. */
16072
16073 c = add_com ("trace", class_breakpoint, trace_command, _("\
16074 Set a tracepoint at specified location.\n\
16075 \n"
16076 BREAK_ARGS_HELP ("trace") "\n\
16077 Do \"help tracepoints\" for info on other tracepoint commands."));
16078 set_cmd_completer (c, location_completer);
16079
16080 add_com_alias ("tp", "trace", class_breakpoint, 0);
16081 add_com_alias ("tr", "trace", class_breakpoint, 1);
16082 add_com_alias ("tra", "trace", class_breakpoint, 1);
16083 add_com_alias ("trac", "trace", class_breakpoint, 1);
16084
16085 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16086 Set a fast tracepoint at specified location.\n\
16087 \n"
16088 BREAK_ARGS_HELP ("ftrace") "\n\
16089 Do \"help tracepoints\" for info on other tracepoint commands."));
16090 set_cmd_completer (c, location_completer);
16091
16092 c = add_com ("strace", class_breakpoint, strace_command, _("\
16093 Set a static tracepoint at location or marker.\n\
16094 \n\
16095 strace [LOCATION] [if CONDITION]\n\
16096 LOCATION may be a linespec, explicit, or address location (described below) \n\
16097 or -m MARKER_ID.\n\n\
16098 If a marker id is specified, probe the marker with that name. With\n\
16099 no LOCATION, uses current execution address of the selected stack frame.\n\
16100 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16101 This collects arbitrary user data passed in the probe point call to the\n\
16102 tracing library. You can inspect it when analyzing the trace buffer,\n\
16103 by printing the $_sdata variable like any other convenience variable.\n\
16104 \n\
16105 CONDITION is a boolean expression.\n\
16106 \n" LOCATION_HELP_STRING "\n\n\
16107 Multiple tracepoints at one place are permitted, and useful if their\n\
16108 conditions are different.\n\
16109 \n\
16110 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16111 Do \"help tracepoints\" for info on other tracepoint commands."));
16112 set_cmd_completer (c, location_completer);
16113
16114 add_info ("tracepoints", info_tracepoints_command, _("\
16115 Status of specified tracepoints (all tracepoints if no argument).\n\
16116 Convenience variable \"$tpnum\" contains the number of the\n\
16117 last tracepoint set."));
16118
16119 add_info_alias ("tp", "tracepoints", 1);
16120
16121 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16122 Delete specified tracepoints.\n\
16123 Arguments are tracepoint numbers, separated by spaces.\n\
16124 No argument means delete all tracepoints."),
16125 &deletelist);
16126 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16127
16128 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16129 Disable specified tracepoints.\n\
16130 Arguments are tracepoint numbers, separated by spaces.\n\
16131 No argument means disable all tracepoints."),
16132 &disablelist);
16133 deprecate_cmd (c, "disable");
16134
16135 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16136 Enable specified tracepoints.\n\
16137 Arguments are tracepoint numbers, separated by spaces.\n\
16138 No argument means enable all tracepoints."),
16139 &enablelist);
16140 deprecate_cmd (c, "enable");
16141
16142 add_com ("passcount", class_trace, trace_pass_command, _("\
16143 Set the passcount for a tracepoint.\n\
16144 The trace will end when the tracepoint has been passed 'count' times.\n\
16145 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16146 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16147
16148 add_basic_prefix_cmd ("save", class_breakpoint,
16149 _("Save breakpoint definitions as a script."),
16150 &save_cmdlist, "save ",
16151 0/*allow-unknown*/, &cmdlist);
16152
16153 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16154 Save current breakpoint definitions as a script.\n\
16155 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16156 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16157 session to restore them."),
16158 &save_cmdlist);
16159 set_cmd_completer (c, filename_completer);
16160
16161 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16162 Save current tracepoint definitions as a script.\n\
16163 Use the 'source' command in another debug session to restore them."),
16164 &save_cmdlist);
16165 set_cmd_completer (c, filename_completer);
16166
16167 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16168 deprecate_cmd (c, "save tracepoints");
16169
16170 add_basic_prefix_cmd ("breakpoint", class_maintenance, _("\
16171 Breakpoint specific settings.\n\
16172 Configure various breakpoint-specific variables such as\n\
16173 pending breakpoint behavior."),
16174 &breakpoint_set_cmdlist, "set breakpoint ",
16175 0/*allow-unknown*/, &setlist);
16176 add_show_prefix_cmd ("breakpoint", class_maintenance, _("\
16177 Breakpoint specific settings.\n\
16178 Configure various breakpoint-specific variables such as\n\
16179 pending breakpoint behavior."),
16180 &breakpoint_show_cmdlist, "show breakpoint ",
16181 0/*allow-unknown*/, &showlist);
16182
16183 add_setshow_auto_boolean_cmd ("pending", no_class,
16184 &pending_break_support, _("\
16185 Set debugger's behavior regarding pending breakpoints."), _("\
16186 Show debugger's behavior regarding pending breakpoints."), _("\
16187 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16188 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16189 an error. If auto, an unrecognized breakpoint location results in a\n\
16190 user-query to see if a pending breakpoint should be created."),
16191 NULL,
16192 show_pending_break_support,
16193 &breakpoint_set_cmdlist,
16194 &breakpoint_show_cmdlist);
16195
16196 pending_break_support = AUTO_BOOLEAN_AUTO;
16197
16198 add_setshow_boolean_cmd ("auto-hw", no_class,
16199 &automatic_hardware_breakpoints, _("\
16200 Set automatic usage of hardware breakpoints."), _("\
16201 Show automatic usage of hardware breakpoints."), _("\
16202 If set, the debugger will automatically use hardware breakpoints for\n\
16203 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16204 a warning will be emitted for such breakpoints."),
16205 NULL,
16206 show_automatic_hardware_breakpoints,
16207 &breakpoint_set_cmdlist,
16208 &breakpoint_show_cmdlist);
16209
16210 add_setshow_boolean_cmd ("always-inserted", class_support,
16211 &always_inserted_mode, _("\
16212 Set mode for inserting breakpoints."), _("\
16213 Show mode for inserting breakpoints."), _("\
16214 When this mode is on, breakpoints are inserted immediately as soon as\n\
16215 they're created, kept inserted even when execution stops, and removed\n\
16216 only when the user deletes them. When this mode is off (the default),\n\
16217 breakpoints are inserted only when execution continues, and removed\n\
16218 when execution stops."),
16219 NULL,
16220 &show_always_inserted_mode,
16221 &breakpoint_set_cmdlist,
16222 &breakpoint_show_cmdlist);
16223
16224 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16225 condition_evaluation_enums,
16226 &condition_evaluation_mode_1, _("\
16227 Set mode of breakpoint condition evaluation."), _("\
16228 Show mode of breakpoint condition evaluation."), _("\
16229 When this is set to \"host\", breakpoint conditions will be\n\
16230 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16231 breakpoint conditions will be downloaded to the target (if the target\n\
16232 supports such feature) and conditions will be evaluated on the target's side.\n\
16233 If this is set to \"auto\" (default), this will be automatically set to\n\
16234 \"target\" if it supports condition evaluation, otherwise it will\n\
16235 be set to \"host\"."),
16236 &set_condition_evaluation_mode,
16237 &show_condition_evaluation_mode,
16238 &breakpoint_set_cmdlist,
16239 &breakpoint_show_cmdlist);
16240
16241 add_com ("break-range", class_breakpoint, break_range_command, _("\
16242 Set a breakpoint for an address range.\n\
16243 break-range START-LOCATION, END-LOCATION\n\
16244 where START-LOCATION and END-LOCATION can be one of the following:\n\
16245 LINENUM, for that line in the current file,\n\
16246 FILE:LINENUM, for that line in that file,\n\
16247 +OFFSET, for that number of lines after the current line\n\
16248 or the start of the range\n\
16249 FUNCTION, for the first line in that function,\n\
16250 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16251 *ADDRESS, for the instruction at that address.\n\
16252 \n\
16253 The breakpoint will stop execution of the inferior whenever it executes\n\
16254 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16255 range (including START-LOCATION and END-LOCATION)."));
16256
16257 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16258 Set a dynamic printf at specified location.\n\
16259 dprintf location,format string,arg1,arg2,...\n\
16260 location may be a linespec, explicit, or address location.\n"
16261 "\n" LOCATION_HELP_STRING));
16262 set_cmd_completer (c, location_completer);
16263
16264 add_setshow_enum_cmd ("dprintf-style", class_support,
16265 dprintf_style_enums, &dprintf_style, _("\
16266 Set the style of usage for dynamic printf."), _("\
16267 Show the style of usage for dynamic printf."), _("\
16268 This setting chooses how GDB will do a dynamic printf.\n\
16269 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16270 console, as with the \"printf\" command.\n\
16271 If the value is \"call\", the print is done by calling a function in your\n\
16272 program; by default printf(), but you can choose a different function or\n\
16273 output stream by setting dprintf-function and dprintf-channel."),
16274 update_dprintf_commands, NULL,
16275 &setlist, &showlist);
16276
16277 dprintf_function = xstrdup ("printf");
16278 add_setshow_string_cmd ("dprintf-function", class_support,
16279 &dprintf_function, _("\
16280 Set the function to use for dynamic printf."), _("\
16281 Show the function to use for dynamic printf."), NULL,
16282 update_dprintf_commands, NULL,
16283 &setlist, &showlist);
16284
16285 dprintf_channel = xstrdup ("");
16286 add_setshow_string_cmd ("dprintf-channel", class_support,
16287 &dprintf_channel, _("\
16288 Set the channel to use for dynamic printf."), _("\
16289 Show the channel to use for dynamic printf."), NULL,
16290 update_dprintf_commands, NULL,
16291 &setlist, &showlist);
16292
16293 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16294 &disconnected_dprintf, _("\
16295 Set whether dprintf continues after GDB disconnects."), _("\
16296 Show whether dprintf continues after GDB disconnects."), _("\
16297 Use this to let dprintf commands continue to hit and produce output\n\
16298 even if GDB disconnects or detaches from the target."),
16299 NULL,
16300 NULL,
16301 &setlist, &showlist);
16302
16303 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16304 Target agent only formatted printing, like the C \"printf\" function.\n\
16305 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
16306 This supports most C printf format specifications, like %s, %d, etc.\n\
16307 This is useful for formatted output in user-defined commands."));
16308
16309 automatic_hardware_breakpoints = true;
16310
16311 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed);
16312 gdb::observers::thread_exit.attach (remove_threaded_breakpoints);
16313 }