gdb: add target_ops::supports_displaced_step
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2020 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 (const 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, const 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 free_bp_location (struct bp_location *loc);
176 static void incref_bp_location (struct bp_location *loc);
177 static void decref_bp_location (struct bp_location **loc);
178
179 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
180
181 /* update_global_location_list's modes of operation wrt to whether to
182 insert locations now. */
183 enum ugll_insert_mode
184 {
185 /* Don't insert any breakpoint locations into the inferior, only
186 remove already-inserted locations that no longer should be
187 inserted. Functions that delete a breakpoint or breakpoints
188 should specify this mode, so that deleting a breakpoint doesn't
189 have the side effect of inserting the locations of other
190 breakpoints that are marked not-inserted, but should_be_inserted
191 returns true on them.
192
193 This behavior is useful is situations close to tear-down -- e.g.,
194 after an exec, while the target still has execution, but
195 breakpoint shadows of the previous executable image should *NOT*
196 be restored to the new image; or before detaching, where the
197 target still has execution and wants to delete breakpoints from
198 GDB's lists, and all breakpoints had already been removed from
199 the inferior. */
200 UGLL_DONT_INSERT,
201
202 /* May insert breakpoints iff breakpoints_should_be_inserted_now
203 claims breakpoints should be inserted now. */
204 UGLL_MAY_INSERT,
205
206 /* Insert locations now, irrespective of
207 breakpoints_should_be_inserted_now. E.g., say all threads are
208 stopped right now, and the user did "continue". We need to
209 insert breakpoints _before_ resuming the target, but
210 UGLL_MAY_INSERT wouldn't insert them, because
211 breakpoints_should_be_inserted_now returns false at that point,
212 as no thread is running yet. */
213 UGLL_INSERT
214 };
215
216 static void update_global_location_list (enum ugll_insert_mode);
217
218 static void update_global_location_list_nothrow (enum ugll_insert_mode);
219
220 static void insert_breakpoint_locations (void);
221
222 static void trace_pass_command (const char *, int);
223
224 static void set_tracepoint_count (int num);
225
226 static bool is_masked_watchpoint (const struct breakpoint *b);
227
228 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
229
230 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
231 otherwise. */
232
233 static int strace_marker_p (struct breakpoint *b);
234
235 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
236 that are implemented on top of software or hardware breakpoints
237 (user breakpoints, internal and momentary breakpoints, etc.). */
238 static struct breakpoint_ops bkpt_base_breakpoint_ops;
239
240 /* Internal breakpoints class type. */
241 static struct breakpoint_ops internal_breakpoint_ops;
242
243 /* Momentary breakpoints class type. */
244 static struct breakpoint_ops momentary_breakpoint_ops;
245
246 /* The breakpoint_ops structure to be used in regular user created
247 breakpoints. */
248 struct breakpoint_ops bkpt_breakpoint_ops;
249
250 /* Breakpoints set on probes. */
251 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
252
253 /* Tracepoints set on probes. */
254 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
255
256 /* Dynamic printf class type. */
257 struct breakpoint_ops dprintf_breakpoint_ops;
258
259 /* The style in which to perform a dynamic printf. This is a user
260 option because different output options have different tradeoffs;
261 if GDB does the printing, there is better error handling if there
262 is a problem with any of the arguments, but using an inferior
263 function lets you have special-purpose printers and sending of
264 output to the same place as compiled-in print functions. */
265
266 static const char dprintf_style_gdb[] = "gdb";
267 static const char dprintf_style_call[] = "call";
268 static const char dprintf_style_agent[] = "agent";
269 static const char *const dprintf_style_enums[] = {
270 dprintf_style_gdb,
271 dprintf_style_call,
272 dprintf_style_agent,
273 NULL
274 };
275 static const char *dprintf_style = dprintf_style_gdb;
276
277 /* The function to use for dynamic printf if the preferred style is to
278 call into the inferior. The value is simply a string that is
279 copied into the command, so it can be anything that GDB can
280 evaluate to a callable address, not necessarily a function name. */
281
282 static char *dprintf_function;
283
284 /* The channel to use for dynamic printf if the preferred style is to
285 call into the inferior; if a nonempty string, it will be passed to
286 the call as the first argument, with the format string as the
287 second. As with the dprintf function, this can be anything that
288 GDB knows how to evaluate, so in addition to common choices like
289 "stderr", this could be an app-specific expression like
290 "mystreams[curlogger]". */
291
292 static char *dprintf_channel;
293
294 /* True if dprintf commands should continue to operate even if GDB
295 has disconnected. */
296 static bool disconnected_dprintf = true;
297
298 struct command_line *
299 breakpoint_commands (struct breakpoint *b)
300 {
301 return b->commands ? b->commands.get () : NULL;
302 }
303
304 /* Flag indicating that a command has proceeded the inferior past the
305 current breakpoint. */
306
307 static bool breakpoint_proceeded;
308
309 const char *
310 bpdisp_text (enum bpdisp disp)
311 {
312 /* NOTE: the following values are a part of MI protocol and
313 represent values of 'disp' field returned when inferior stops at
314 a breakpoint. */
315 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
316
317 return bpdisps[(int) disp];
318 }
319
320 /* Prototypes for exported functions. */
321 /* If FALSE, gdb will not use hardware support for watchpoints, even
322 if such is available. */
323 static int can_use_hw_watchpoints;
324
325 static void
326 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
327 struct cmd_list_element *c,
328 const char *value)
329 {
330 fprintf_filtered (file,
331 _("Debugger's willingness to use "
332 "watchpoint hardware is %s.\n"),
333 value);
334 }
335
336 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
337 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
338 for unrecognized breakpoint locations.
339 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
340 static enum auto_boolean pending_break_support;
341 static void
342 show_pending_break_support (struct ui_file *file, int from_tty,
343 struct cmd_list_element *c,
344 const char *value)
345 {
346 fprintf_filtered (file,
347 _("Debugger's behavior regarding "
348 "pending breakpoints is %s.\n"),
349 value);
350 }
351
352 /* If true, gdb will automatically use hardware breakpoints for breakpoints
353 set with "break" but falling in read-only memory.
354 If false, gdb will warn about such breakpoints, but won't automatically
355 use hardware breakpoints. */
356 static bool automatic_hardware_breakpoints;
357 static void
358 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
359 struct cmd_list_element *c,
360 const char *value)
361 {
362 fprintf_filtered (file,
363 _("Automatic usage of hardware breakpoints is %s.\n"),
364 value);
365 }
366
367 /* If on, GDB keeps breakpoints inserted even if the inferior is
368 stopped, and immediately inserts any new breakpoints as soon as
369 they're created. If off (default), GDB keeps breakpoints off of
370 the target as long as possible. That is, it delays inserting
371 breakpoints until the next resume, and removes them again when the
372 target fully stops. This is a bit safer in case GDB crashes while
373 processing user input. */
374 static bool always_inserted_mode = false;
375
376 static void
377 show_always_inserted_mode (struct ui_file *file, int from_tty,
378 struct cmd_list_element *c, const char *value)
379 {
380 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
381 value);
382 }
383
384 /* See breakpoint.h. */
385
386 int
387 breakpoints_should_be_inserted_now (void)
388 {
389 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
390 {
391 /* If breakpoints are global, they should be inserted even if no
392 thread under gdb's control is running, or even if there are
393 no threads under GDB's control yet. */
394 return 1;
395 }
396 else
397 {
398 if (always_inserted_mode)
399 {
400 /* The user wants breakpoints inserted even if all threads
401 are stopped. */
402 return 1;
403 }
404
405 for (inferior *inf : all_inferiors ())
406 if (inf->has_execution ()
407 && threads_are_executing (inf->process_target ()))
408 return 1;
409
410 /* Don't remove breakpoints yet if, even though all threads are
411 stopped, we still have events to process. */
412 for (thread_info *tp : all_non_exited_threads ())
413 if (tp->resumed
414 && tp->suspend.waitstatus_pending_p)
415 return 1;
416 }
417 return 0;
418 }
419
420 static const char condition_evaluation_both[] = "host or target";
421
422 /* Modes for breakpoint condition evaluation. */
423 static const char condition_evaluation_auto[] = "auto";
424 static const char condition_evaluation_host[] = "host";
425 static const char condition_evaluation_target[] = "target";
426 static const char *const condition_evaluation_enums[] = {
427 condition_evaluation_auto,
428 condition_evaluation_host,
429 condition_evaluation_target,
430 NULL
431 };
432
433 /* Global that holds the current mode for breakpoint condition evaluation. */
434 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
435
436 /* Global that we use to display information to the user (gets its value from
437 condition_evaluation_mode_1. */
438 static const char *condition_evaluation_mode = condition_evaluation_auto;
439
440 /* Translate a condition evaluation mode MODE into either "host"
441 or "target". This is used mostly to translate from "auto" to the
442 real setting that is being used. It returns the translated
443 evaluation mode. */
444
445 static const char *
446 translate_condition_evaluation_mode (const char *mode)
447 {
448 if (mode == condition_evaluation_auto)
449 {
450 if (target_supports_evaluation_of_breakpoint_conditions ())
451 return condition_evaluation_target;
452 else
453 return condition_evaluation_host;
454 }
455 else
456 return mode;
457 }
458
459 /* Discovers what condition_evaluation_auto translates to. */
460
461 static const char *
462 breakpoint_condition_evaluation_mode (void)
463 {
464 return translate_condition_evaluation_mode (condition_evaluation_mode);
465 }
466
467 /* Return true if GDB should evaluate breakpoint conditions or false
468 otherwise. */
469
470 static int
471 gdb_evaluates_breakpoint_condition_p (void)
472 {
473 const char *mode = breakpoint_condition_evaluation_mode ();
474
475 return (mode == condition_evaluation_host);
476 }
477
478 /* Are we executing breakpoint commands? */
479 static int executing_breakpoint_commands;
480
481 /* Are overlay event breakpoints enabled? */
482 static int overlay_events_enabled;
483
484 /* See description in breakpoint.h. */
485 bool target_exact_watchpoints = false;
486
487 /* Walk the following statement or block through all breakpoints.
488 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
489 current breakpoint. */
490
491 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
492
493 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
494 for (B = breakpoint_chain; \
495 B ? (TMP=B->next, 1): 0; \
496 B = TMP)
497
498 /* Similar iterator for the low-level breakpoints. SAFE variant is
499 not provided so update_global_location_list must not be called
500 while executing the block of ALL_BP_LOCATIONS. */
501
502 #define ALL_BP_LOCATIONS(B,BP_TMP) \
503 for (BP_TMP = bp_locations; \
504 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
505 BP_TMP++)
506
507 /* Iterates through locations with address ADDRESS for the currently selected
508 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
509 to where the loop should start from.
510 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
511 appropriate location to start with. */
512
513 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
514 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
515 BP_LOCP_TMP = BP_LOCP_START; \
516 BP_LOCP_START \
517 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
518 && (*BP_LOCP_TMP)->address == ADDRESS); \
519 BP_LOCP_TMP++)
520
521 /* Iterator for tracepoints only. */
522
523 #define ALL_TRACEPOINTS(B) \
524 for (B = breakpoint_chain; B; B = B->next) \
525 if (is_tracepoint (B))
526
527 /* Chains of all breakpoints defined. */
528
529 static struct breakpoint *breakpoint_chain;
530
531 /* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
532
533 static struct bp_location **bp_locations;
534
535 /* Number of elements of BP_LOCATIONS. */
536
537 static unsigned bp_locations_count;
538
539 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
540 ADDRESS for the current elements of BP_LOCATIONS which get a valid
541 result from bp_location_has_shadow. You can use it for roughly
542 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
543 an address you need to read. */
544
545 static CORE_ADDR bp_locations_placed_address_before_address_max;
546
547 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
548 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
549 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
550 You can use it for roughly limiting the subrange of BP_LOCATIONS to
551 scan for shadow bytes for an address you need to read. */
552
553 static CORE_ADDR bp_locations_shadow_len_after_address_max;
554
555 /* The locations that no longer correspond to any breakpoint, unlinked
556 from the bp_locations array, but for which a hit may still be
557 reported by a target. */
558 static std::vector<bp_location *> moribund_locations;
559
560 /* Number of last breakpoint made. */
561
562 static int breakpoint_count;
563
564 /* The value of `breakpoint_count' before the last command that
565 created breakpoints. If the last (break-like) command created more
566 than one breakpoint, then the difference between BREAKPOINT_COUNT
567 and PREV_BREAKPOINT_COUNT is more than one. */
568 static int prev_breakpoint_count;
569
570 /* Number of last tracepoint made. */
571
572 static int tracepoint_count;
573
574 static struct cmd_list_element *breakpoint_set_cmdlist;
575 static struct cmd_list_element *breakpoint_show_cmdlist;
576 struct cmd_list_element *save_cmdlist;
577
578 /* See declaration at breakpoint.h. */
579
580 struct breakpoint *
581 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
582 void *user_data)
583 {
584 struct breakpoint *b = NULL;
585
586 ALL_BREAKPOINTS (b)
587 {
588 if (func (b, user_data) != 0)
589 break;
590 }
591
592 return b;
593 }
594
595 /* Return whether a breakpoint is an active enabled breakpoint. */
596 static int
597 breakpoint_enabled (struct breakpoint *b)
598 {
599 return (b->enable_state == bp_enabled);
600 }
601
602 /* Set breakpoint count to NUM. */
603
604 static void
605 set_breakpoint_count (int num)
606 {
607 prev_breakpoint_count = breakpoint_count;
608 breakpoint_count = num;
609 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
610 }
611
612 /* Used by `start_rbreak_breakpoints' below, to record the current
613 breakpoint count before "rbreak" creates any breakpoint. */
614 static int rbreak_start_breakpoint_count;
615
616 /* Called at the start an "rbreak" command to record the first
617 breakpoint made. */
618
619 scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
620 {
621 rbreak_start_breakpoint_count = breakpoint_count;
622 }
623
624 /* Called at the end of an "rbreak" command to record the last
625 breakpoint made. */
626
627 scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
628 {
629 prev_breakpoint_count = rbreak_start_breakpoint_count;
630 }
631
632 /* Used in run_command to zero the hit count when a new run starts. */
633
634 void
635 clear_breakpoint_hit_counts (void)
636 {
637 struct breakpoint *b;
638
639 ALL_BREAKPOINTS (b)
640 b->hit_count = 0;
641 }
642
643 \f
644 /* Return the breakpoint with the specified number, or NULL
645 if the number does not refer to an existing breakpoint. */
646
647 struct breakpoint *
648 get_breakpoint (int num)
649 {
650 struct breakpoint *b;
651
652 ALL_BREAKPOINTS (b)
653 if (b->number == num)
654 return b;
655
656 return NULL;
657 }
658
659 \f
660
661 /* Mark locations as "conditions have changed" in case the target supports
662 evaluating conditions on its side. */
663
664 static void
665 mark_breakpoint_modified (struct breakpoint *b)
666 {
667 struct bp_location *loc;
668
669 /* This is only meaningful if the target is
670 evaluating conditions and if the user has
671 opted for condition evaluation on the target's
672 side. */
673 if (gdb_evaluates_breakpoint_condition_p ()
674 || !target_supports_evaluation_of_breakpoint_conditions ())
675 return;
676
677 if (!is_breakpoint (b))
678 return;
679
680 for (loc = b->loc; loc; loc = loc->next)
681 loc->condition_changed = condition_modified;
682 }
683
684 /* Mark location as "conditions have changed" in case the target supports
685 evaluating conditions on its side. */
686
687 static void
688 mark_breakpoint_location_modified (struct bp_location *loc)
689 {
690 /* This is only meaningful if the target is
691 evaluating conditions and if the user has
692 opted for condition evaluation on the target's
693 side. */
694 if (gdb_evaluates_breakpoint_condition_p ()
695 || !target_supports_evaluation_of_breakpoint_conditions ())
696
697 return;
698
699 if (!is_breakpoint (loc->owner))
700 return;
701
702 loc->condition_changed = condition_modified;
703 }
704
705 /* Sets the condition-evaluation mode using the static global
706 condition_evaluation_mode. */
707
708 static void
709 set_condition_evaluation_mode (const char *args, int from_tty,
710 struct cmd_list_element *c)
711 {
712 const char *old_mode, *new_mode;
713
714 if ((condition_evaluation_mode_1 == condition_evaluation_target)
715 && !target_supports_evaluation_of_breakpoint_conditions ())
716 {
717 condition_evaluation_mode_1 = condition_evaluation_mode;
718 warning (_("Target does not support breakpoint condition evaluation.\n"
719 "Using host evaluation mode instead."));
720 return;
721 }
722
723 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
724 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
725
726 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
727 settings was "auto". */
728 condition_evaluation_mode = condition_evaluation_mode_1;
729
730 /* Only update the mode if the user picked a different one. */
731 if (new_mode != old_mode)
732 {
733 struct bp_location *loc, **loc_tmp;
734 /* If the user switched to a different evaluation mode, we
735 need to synch the changes with the target as follows:
736
737 "host" -> "target": Send all (valid) conditions to the target.
738 "target" -> "host": Remove all the conditions from the target.
739 */
740
741 if (new_mode == condition_evaluation_target)
742 {
743 /* Mark everything modified and synch conditions with the
744 target. */
745 ALL_BP_LOCATIONS (loc, loc_tmp)
746 mark_breakpoint_location_modified (loc);
747 }
748 else
749 {
750 /* Manually mark non-duplicate locations to synch conditions
751 with the target. We do this to remove all the conditions the
752 target knows about. */
753 ALL_BP_LOCATIONS (loc, loc_tmp)
754 if (is_breakpoint (loc->owner) && loc->inserted)
755 loc->needs_update = 1;
756 }
757
758 /* Do the update. */
759 update_global_location_list (UGLL_MAY_INSERT);
760 }
761
762 return;
763 }
764
765 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
766 what "auto" is translating to. */
767
768 static void
769 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
770 struct cmd_list_element *c, const char *value)
771 {
772 if (condition_evaluation_mode == condition_evaluation_auto)
773 fprintf_filtered (file,
774 _("Breakpoint condition evaluation "
775 "mode is %s (currently %s).\n"),
776 value,
777 breakpoint_condition_evaluation_mode ());
778 else
779 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
780 value);
781 }
782
783 /* A comparison function for bp_location AP and BP that is used by
784 bsearch. This comparison function only cares about addresses, unlike
785 the more general bp_location_is_less_than function. */
786
787 static int
788 bp_locations_compare_addrs (const void *ap, const void *bp)
789 {
790 const struct bp_location *a = *(const struct bp_location **) ap;
791 const struct bp_location *b = *(const struct bp_location **) bp;
792
793 if (a->address == b->address)
794 return 0;
795 else
796 return ((a->address > b->address) - (a->address < b->address));
797 }
798
799 /* Helper function to skip all bp_locations with addresses
800 less than ADDRESS. It returns the first bp_location that
801 is greater than or equal to ADDRESS. If none is found, just
802 return NULL. */
803
804 static struct bp_location **
805 get_first_locp_gte_addr (CORE_ADDR address)
806 {
807 struct bp_location dummy_loc;
808 struct bp_location *dummy_locp = &dummy_loc;
809 struct bp_location **locp_found = NULL;
810
811 /* Initialize the dummy location's address field. */
812 dummy_loc.address = address;
813
814 /* Find a close match to the first location at ADDRESS. */
815 locp_found = ((struct bp_location **)
816 bsearch (&dummy_locp, bp_locations, bp_locations_count,
817 sizeof (struct bp_location **),
818 bp_locations_compare_addrs));
819
820 /* Nothing was found, nothing left to do. */
821 if (locp_found == NULL)
822 return NULL;
823
824 /* We may have found a location that is at ADDRESS but is not the first in the
825 location's list. Go backwards (if possible) and locate the first one. */
826 while ((locp_found - 1) >= bp_locations
827 && (*(locp_found - 1))->address == address)
828 locp_found--;
829
830 return locp_found;
831 }
832
833 void
834 set_breakpoint_condition (struct breakpoint *b, const char *exp,
835 int from_tty)
836 {
837 xfree (b->cond_string);
838 b->cond_string = NULL;
839
840 if (is_watchpoint (b))
841 {
842 struct watchpoint *w = (struct watchpoint *) b;
843
844 w->cond_exp.reset ();
845 }
846 else
847 {
848 struct bp_location *loc;
849
850 for (loc = b->loc; loc; loc = loc->next)
851 {
852 loc->cond.reset ();
853
854 /* No need to free the condition agent expression
855 bytecode (if we have one). We will handle this
856 when we go through update_global_location_list. */
857 }
858 }
859
860 if (*exp == 0)
861 {
862 if (from_tty)
863 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
864 }
865 else
866 {
867 const char *arg = exp;
868
869 /* I don't know if it matters whether this is the string the user
870 typed in or the decompiled expression. */
871 b->cond_string = xstrdup (arg);
872 b->condition_not_parsed = 0;
873
874 if (is_watchpoint (b))
875 {
876 struct watchpoint *w = (struct watchpoint *) b;
877
878 innermost_block_tracker tracker;
879 arg = exp;
880 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
881 if (*arg)
882 error (_("Junk at end of expression"));
883 w->cond_exp_valid_block = tracker.block ();
884 }
885 else
886 {
887 struct bp_location *loc;
888
889 for (loc = b->loc; loc; loc = loc->next)
890 {
891 arg = exp;
892 loc->cond =
893 parse_exp_1 (&arg, loc->address,
894 block_for_pc (loc->address), 0);
895 if (*arg)
896 error (_("Junk at end of expression"));
897 }
898 }
899 }
900 mark_breakpoint_modified (b);
901
902 gdb::observers::breakpoint_modified.notify (b);
903 }
904
905 /* Completion for the "condition" command. */
906
907 static void
908 condition_completer (struct cmd_list_element *cmd,
909 completion_tracker &tracker,
910 const char *text, const char *word)
911 {
912 const char *space;
913
914 text = skip_spaces (text);
915 space = skip_to_space (text);
916 if (*space == '\0')
917 {
918 int len;
919 struct breakpoint *b;
920
921 if (text[0] == '$')
922 {
923 /* We don't support completion of history indices. */
924 if (!isdigit (text[1]))
925 complete_internalvar (tracker, &text[1]);
926 return;
927 }
928
929 /* We're completing the breakpoint number. */
930 len = strlen (text);
931
932 ALL_BREAKPOINTS (b)
933 {
934 char number[50];
935
936 xsnprintf (number, sizeof (number), "%d", b->number);
937
938 if (strncmp (number, text, len) == 0)
939 tracker.add_completion (make_unique_xstrdup (number));
940 }
941
942 return;
943 }
944
945 /* We're completing the expression part. */
946 text = skip_spaces (space);
947 expression_completer (cmd, tracker, text, word);
948 }
949
950 /* condition N EXP -- set break condition of breakpoint N to EXP. */
951
952 static void
953 condition_command (const char *arg, int from_tty)
954 {
955 struct breakpoint *b;
956 const char *p;
957 int bnum;
958
959 if (arg == 0)
960 error_no_arg (_("breakpoint number"));
961
962 p = arg;
963 bnum = get_number (&p);
964 if (bnum == 0)
965 error (_("Bad breakpoint argument: '%s'"), arg);
966
967 ALL_BREAKPOINTS (b)
968 if (b->number == bnum)
969 {
970 /* Check if this breakpoint has a "stop" method implemented in an
971 extension language. This method and conditions entered into GDB
972 from the CLI are mutually exclusive. */
973 const struct extension_language_defn *extlang
974 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
975
976 if (extlang != NULL)
977 {
978 error (_("Only one stop condition allowed. There is currently"
979 " a %s stop condition defined for this breakpoint."),
980 ext_lang_capitalized_name (extlang));
981 }
982 set_breakpoint_condition (b, p, from_tty);
983
984 if (is_breakpoint (b))
985 update_global_location_list (UGLL_MAY_INSERT);
986
987 return;
988 }
989
990 error (_("No breakpoint number %d."), bnum);
991 }
992
993 /* Check that COMMAND do not contain commands that are suitable
994 only for tracepoints and not suitable for ordinary breakpoints.
995 Throw if any such commands is found. */
996
997 static void
998 check_no_tracepoint_commands (struct command_line *commands)
999 {
1000 struct command_line *c;
1001
1002 for (c = commands; c; c = c->next)
1003 {
1004 if (c->control_type == while_stepping_control)
1005 error (_("The 'while-stepping' command can "
1006 "only be used for tracepoints"));
1007
1008 check_no_tracepoint_commands (c->body_list_0.get ());
1009 check_no_tracepoint_commands (c->body_list_1.get ());
1010
1011 /* Not that command parsing removes leading whitespace and comment
1012 lines and also empty lines. So, we only need to check for
1013 command directly. */
1014 if (strstr (c->line, "collect ") == c->line)
1015 error (_("The 'collect' command can only be used for tracepoints"));
1016
1017 if (strstr (c->line, "teval ") == c->line)
1018 error (_("The 'teval' command can only be used for tracepoints"));
1019 }
1020 }
1021
1022 struct longjmp_breakpoint : public breakpoint
1023 {
1024 ~longjmp_breakpoint () override;
1025 };
1026
1027 /* Encapsulate tests for different types of tracepoints. */
1028
1029 static bool
1030 is_tracepoint_type (bptype type)
1031 {
1032 return (type == bp_tracepoint
1033 || type == bp_fast_tracepoint
1034 || type == bp_static_tracepoint);
1035 }
1036
1037 static bool
1038 is_longjmp_type (bptype type)
1039 {
1040 return type == bp_longjmp || type == bp_exception;
1041 }
1042
1043 /* See breakpoint.h. */
1044
1045 bool
1046 is_tracepoint (const struct breakpoint *b)
1047 {
1048 return is_tracepoint_type (b->type);
1049 }
1050
1051 /* Factory function to create an appropriate instance of breakpoint given
1052 TYPE. */
1053
1054 static std::unique_ptr<breakpoint>
1055 new_breakpoint_from_type (bptype type)
1056 {
1057 breakpoint *b;
1058
1059 if (is_tracepoint_type (type))
1060 b = new tracepoint ();
1061 else if (is_longjmp_type (type))
1062 b = new longjmp_breakpoint ();
1063 else
1064 b = new breakpoint ();
1065
1066 return std::unique_ptr<breakpoint> (b);
1067 }
1068
1069 /* A helper function that validates that COMMANDS are valid for a
1070 breakpoint. This function will throw an exception if a problem is
1071 found. */
1072
1073 static void
1074 validate_commands_for_breakpoint (struct breakpoint *b,
1075 struct command_line *commands)
1076 {
1077 if (is_tracepoint (b))
1078 {
1079 struct tracepoint *t = (struct tracepoint *) b;
1080 struct command_line *c;
1081 struct command_line *while_stepping = 0;
1082
1083 /* Reset the while-stepping step count. The previous commands
1084 might have included a while-stepping action, while the new
1085 ones might not. */
1086 t->step_count = 0;
1087
1088 /* We need to verify that each top-level element of commands is
1089 valid for tracepoints, that there's at most one
1090 while-stepping element, and that the while-stepping's body
1091 has valid tracing commands excluding nested while-stepping.
1092 We also need to validate the tracepoint action line in the
1093 context of the tracepoint --- validate_actionline actually
1094 has side effects, like setting the tracepoint's
1095 while-stepping STEP_COUNT, in addition to checking if the
1096 collect/teval actions parse and make sense in the
1097 tracepoint's context. */
1098 for (c = commands; c; c = c->next)
1099 {
1100 if (c->control_type == while_stepping_control)
1101 {
1102 if (b->type == bp_fast_tracepoint)
1103 error (_("The 'while-stepping' command "
1104 "cannot be used for fast tracepoint"));
1105 else if (b->type == bp_static_tracepoint)
1106 error (_("The 'while-stepping' command "
1107 "cannot be used for static tracepoint"));
1108
1109 if (while_stepping)
1110 error (_("The 'while-stepping' command "
1111 "can be used only once"));
1112 else
1113 while_stepping = c;
1114 }
1115
1116 validate_actionline (c->line, b);
1117 }
1118 if (while_stepping)
1119 {
1120 struct command_line *c2;
1121
1122 gdb_assert (while_stepping->body_list_1 == nullptr);
1123 c2 = while_stepping->body_list_0.get ();
1124 for (; c2; c2 = c2->next)
1125 {
1126 if (c2->control_type == while_stepping_control)
1127 error (_("The 'while-stepping' command cannot be nested"));
1128 }
1129 }
1130 }
1131 else
1132 {
1133 check_no_tracepoint_commands (commands);
1134 }
1135 }
1136
1137 /* Return a vector of all the static tracepoints set at ADDR. The
1138 caller is responsible for releasing the vector. */
1139
1140 std::vector<breakpoint *>
1141 static_tracepoints_here (CORE_ADDR addr)
1142 {
1143 struct breakpoint *b;
1144 std::vector<breakpoint *> found;
1145 struct bp_location *loc;
1146
1147 ALL_BREAKPOINTS (b)
1148 if (b->type == bp_static_tracepoint)
1149 {
1150 for (loc = b->loc; loc; loc = loc->next)
1151 if (loc->address == addr)
1152 found.push_back (b);
1153 }
1154
1155 return found;
1156 }
1157
1158 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1159 validate that only allowed commands are included. */
1160
1161 void
1162 breakpoint_set_commands (struct breakpoint *b,
1163 counted_command_line &&commands)
1164 {
1165 validate_commands_for_breakpoint (b, commands.get ());
1166
1167 b->commands = std::move (commands);
1168 gdb::observers::breakpoint_modified.notify (b);
1169 }
1170
1171 /* Set the internal `silent' flag on the breakpoint. Note that this
1172 is not the same as the "silent" that may appear in the breakpoint's
1173 commands. */
1174
1175 void
1176 breakpoint_set_silent (struct breakpoint *b, int silent)
1177 {
1178 int old_silent = b->silent;
1179
1180 b->silent = silent;
1181 if (old_silent != silent)
1182 gdb::observers::breakpoint_modified.notify (b);
1183 }
1184
1185 /* Set the thread for this breakpoint. If THREAD is -1, make the
1186 breakpoint work for any thread. */
1187
1188 void
1189 breakpoint_set_thread (struct breakpoint *b, int thread)
1190 {
1191 int old_thread = b->thread;
1192
1193 b->thread = thread;
1194 if (old_thread != thread)
1195 gdb::observers::breakpoint_modified.notify (b);
1196 }
1197
1198 /* Set the task for this breakpoint. If TASK is 0, make the
1199 breakpoint work for any task. */
1200
1201 void
1202 breakpoint_set_task (struct breakpoint *b, int task)
1203 {
1204 int old_task = b->task;
1205
1206 b->task = task;
1207 if (old_task != task)
1208 gdb::observers::breakpoint_modified.notify (b);
1209 }
1210
1211 static void
1212 commands_command_1 (const char *arg, int from_tty,
1213 struct command_line *control)
1214 {
1215 counted_command_line cmd;
1216 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1217 NULL after the call to read_command_lines if the user provides an empty
1218 list of command by just typing "end". */
1219 bool cmd_read = false;
1220
1221 std::string new_arg;
1222
1223 if (arg == NULL || !*arg)
1224 {
1225 if (breakpoint_count - prev_breakpoint_count > 1)
1226 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1227 breakpoint_count);
1228 else if (breakpoint_count > 0)
1229 new_arg = string_printf ("%d", breakpoint_count);
1230 arg = new_arg.c_str ();
1231 }
1232
1233 map_breakpoint_numbers
1234 (arg, [&] (breakpoint *b)
1235 {
1236 if (!cmd_read)
1237 {
1238 gdb_assert (cmd == NULL);
1239 if (control != NULL)
1240 cmd = control->body_list_0;
1241 else
1242 {
1243 std::string str
1244 = string_printf (_("Type commands for breakpoint(s) "
1245 "%s, one per line."),
1246 arg);
1247
1248 auto do_validate = [=] (const char *line)
1249 {
1250 validate_actionline (line, b);
1251 };
1252 gdb::function_view<void (const char *)> validator;
1253 if (is_tracepoint (b))
1254 validator = do_validate;
1255
1256 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1257 }
1258 cmd_read = true;
1259 }
1260
1261 /* If a breakpoint was on the list more than once, we don't need to
1262 do anything. */
1263 if (b->commands != cmd)
1264 {
1265 validate_commands_for_breakpoint (b, cmd.get ());
1266 b->commands = cmd;
1267 gdb::observers::breakpoint_modified.notify (b);
1268 }
1269 });
1270 }
1271
1272 static void
1273 commands_command (const char *arg, int from_tty)
1274 {
1275 commands_command_1 (arg, from_tty, NULL);
1276 }
1277
1278 /* Like commands_command, but instead of reading the commands from
1279 input stream, takes them from an already parsed command structure.
1280
1281 This is used by cli-script.c to DTRT with breakpoint commands
1282 that are part of if and while bodies. */
1283 enum command_control_type
1284 commands_from_control_command (const char *arg, struct command_line *cmd)
1285 {
1286 commands_command_1 (arg, 0, cmd);
1287 return simple_control;
1288 }
1289
1290 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1291
1292 static int
1293 bp_location_has_shadow (struct bp_location *bl)
1294 {
1295 if (bl->loc_type != bp_loc_software_breakpoint)
1296 return 0;
1297 if (!bl->inserted)
1298 return 0;
1299 if (bl->target_info.shadow_len == 0)
1300 /* BL isn't valid, or doesn't shadow memory. */
1301 return 0;
1302 return 1;
1303 }
1304
1305 /* Update BUF, which is LEN bytes read from the target address
1306 MEMADDR, by replacing a memory breakpoint with its shadowed
1307 contents.
1308
1309 If READBUF is not NULL, this buffer must not overlap with the of
1310 the breakpoint location's shadow_contents buffer. Otherwise, a
1311 failed assertion internal error will be raised. */
1312
1313 static void
1314 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1315 const gdb_byte *writebuf_org,
1316 ULONGEST memaddr, LONGEST len,
1317 struct bp_target_info *target_info,
1318 struct gdbarch *gdbarch)
1319 {
1320 /* Now do full processing of the found relevant range of elements. */
1321 CORE_ADDR bp_addr = 0;
1322 int bp_size = 0;
1323 int bptoffset = 0;
1324
1325 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1326 current_program_space->aspace, 0))
1327 {
1328 /* The breakpoint is inserted in a different address space. */
1329 return;
1330 }
1331
1332 /* Addresses and length of the part of the breakpoint that
1333 we need to copy. */
1334 bp_addr = target_info->placed_address;
1335 bp_size = target_info->shadow_len;
1336
1337 if (bp_addr + bp_size <= memaddr)
1338 {
1339 /* The breakpoint is entirely before the chunk of memory we are
1340 reading. */
1341 return;
1342 }
1343
1344 if (bp_addr >= memaddr + len)
1345 {
1346 /* The breakpoint is entirely after the chunk of memory we are
1347 reading. */
1348 return;
1349 }
1350
1351 /* Offset within shadow_contents. */
1352 if (bp_addr < memaddr)
1353 {
1354 /* Only copy the second part of the breakpoint. */
1355 bp_size -= memaddr - bp_addr;
1356 bptoffset = memaddr - bp_addr;
1357 bp_addr = memaddr;
1358 }
1359
1360 if (bp_addr + bp_size > memaddr + len)
1361 {
1362 /* Only copy the first part of the breakpoint. */
1363 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1364 }
1365
1366 if (readbuf != NULL)
1367 {
1368 /* Verify that the readbuf buffer does not overlap with the
1369 shadow_contents buffer. */
1370 gdb_assert (target_info->shadow_contents >= readbuf + len
1371 || readbuf >= (target_info->shadow_contents
1372 + target_info->shadow_len));
1373
1374 /* Update the read buffer with this inserted breakpoint's
1375 shadow. */
1376 memcpy (readbuf + bp_addr - memaddr,
1377 target_info->shadow_contents + bptoffset, bp_size);
1378 }
1379 else
1380 {
1381 const unsigned char *bp;
1382 CORE_ADDR addr = target_info->reqstd_address;
1383 int placed_size;
1384
1385 /* Update the shadow with what we want to write to memory. */
1386 memcpy (target_info->shadow_contents + bptoffset,
1387 writebuf_org + bp_addr - memaddr, bp_size);
1388
1389 /* Determine appropriate breakpoint contents and size for this
1390 address. */
1391 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1392
1393 /* Update the final write buffer with this inserted
1394 breakpoint's INSN. */
1395 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1396 }
1397 }
1398
1399 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1400 by replacing any memory breakpoints with their shadowed contents.
1401
1402 If READBUF is not NULL, this buffer must not overlap with any of
1403 the breakpoint location's shadow_contents buffers. Otherwise,
1404 a failed assertion internal error will be raised.
1405
1406 The range of shadowed area by each bp_location is:
1407 bl->address - bp_locations_placed_address_before_address_max
1408 up to bl->address + bp_locations_shadow_len_after_address_max
1409 The range we were requested to resolve shadows for is:
1410 memaddr ... memaddr + len
1411 Thus the safe cutoff boundaries for performance optimization are
1412 memaddr + len <= (bl->address
1413 - bp_locations_placed_address_before_address_max)
1414 and:
1415 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1416
1417 void
1418 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1419 const gdb_byte *writebuf_org,
1420 ULONGEST memaddr, LONGEST len)
1421 {
1422 /* Left boundary, right boundary and median element of our binary
1423 search. */
1424 unsigned bc_l, bc_r, bc;
1425
1426 /* Find BC_L which is a leftmost element which may affect BUF
1427 content. It is safe to report lower value but a failure to
1428 report higher one. */
1429
1430 bc_l = 0;
1431 bc_r = bp_locations_count;
1432 while (bc_l + 1 < bc_r)
1433 {
1434 struct bp_location *bl;
1435
1436 bc = (bc_l + bc_r) / 2;
1437 bl = bp_locations[bc];
1438
1439 /* Check first BL->ADDRESS will not overflow due to the added
1440 constant. Then advance the left boundary only if we are sure
1441 the BC element can in no way affect the BUF content (MEMADDR
1442 to MEMADDR + LEN range).
1443
1444 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1445 offset so that we cannot miss a breakpoint with its shadow
1446 range tail still reaching MEMADDR. */
1447
1448 if ((bl->address + bp_locations_shadow_len_after_address_max
1449 >= bl->address)
1450 && (bl->address + bp_locations_shadow_len_after_address_max
1451 <= memaddr))
1452 bc_l = bc;
1453 else
1454 bc_r = bc;
1455 }
1456
1457 /* Due to the binary search above, we need to make sure we pick the
1458 first location that's at BC_L's address. E.g., if there are
1459 multiple locations at the same address, BC_L may end up pointing
1460 at a duplicate location, and miss the "master"/"inserted"
1461 location. Say, given locations L1, L2 and L3 at addresses A and
1462 B:
1463
1464 L1@A, L2@A, L3@B, ...
1465
1466 BC_L could end up pointing at location L2, while the "master"
1467 location could be L1. Since the `loc->inserted' flag is only set
1468 on "master" locations, we'd forget to restore the shadow of L1
1469 and L2. */
1470 while (bc_l > 0
1471 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1472 bc_l--;
1473
1474 /* Now do full processing of the found relevant range of elements. */
1475
1476 for (bc = bc_l; bc < bp_locations_count; bc++)
1477 {
1478 struct bp_location *bl = bp_locations[bc];
1479
1480 /* bp_location array has BL->OWNER always non-NULL. */
1481 if (bl->owner->type == bp_none)
1482 warning (_("reading through apparently deleted breakpoint #%d?"),
1483 bl->owner->number);
1484
1485 /* Performance optimization: any further element can no longer affect BUF
1486 content. */
1487
1488 if (bl->address >= bp_locations_placed_address_before_address_max
1489 && memaddr + len <= (bl->address
1490 - bp_locations_placed_address_before_address_max))
1491 break;
1492
1493 if (!bp_location_has_shadow (bl))
1494 continue;
1495
1496 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1497 memaddr, len, &bl->target_info, bl->gdbarch);
1498 }
1499 }
1500
1501 /* See breakpoint.h. */
1502
1503 bool
1504 is_breakpoint (const struct breakpoint *bpt)
1505 {
1506 return (bpt->type == bp_breakpoint
1507 || bpt->type == bp_hardware_breakpoint
1508 || bpt->type == bp_dprintf);
1509 }
1510
1511 /* Return true if BPT is of any hardware watchpoint kind. */
1512
1513 static bool
1514 is_hardware_watchpoint (const struct breakpoint *bpt)
1515 {
1516 return (bpt->type == bp_hardware_watchpoint
1517 || bpt->type == bp_read_watchpoint
1518 || bpt->type == bp_access_watchpoint);
1519 }
1520
1521 /* See breakpoint.h. */
1522
1523 bool
1524 is_watchpoint (const struct breakpoint *bpt)
1525 {
1526 return (is_hardware_watchpoint (bpt)
1527 || bpt->type == bp_watchpoint);
1528 }
1529
1530 /* Returns true if the current thread and its running state are safe
1531 to evaluate or update watchpoint B. Watchpoints on local
1532 expressions need to be evaluated in the context of the thread that
1533 was current when the watchpoint was created, and, that thread needs
1534 to be stopped to be able to select the correct frame context.
1535 Watchpoints on global expressions can be evaluated on any thread,
1536 and in any state. It is presently left to the target allowing
1537 memory accesses when threads are running. */
1538
1539 static int
1540 watchpoint_in_thread_scope (struct watchpoint *b)
1541 {
1542 return (b->pspace == current_program_space
1543 && (b->watchpoint_thread == null_ptid
1544 || (inferior_ptid == b->watchpoint_thread
1545 && !inferior_thread ()->executing)));
1546 }
1547
1548 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1549 associated bp_watchpoint_scope breakpoint. */
1550
1551 static void
1552 watchpoint_del_at_next_stop (struct watchpoint *w)
1553 {
1554 if (w->related_breakpoint != w)
1555 {
1556 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1557 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1558 w->related_breakpoint->disposition = disp_del_at_next_stop;
1559 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1560 w->related_breakpoint = w;
1561 }
1562 w->disposition = disp_del_at_next_stop;
1563 }
1564
1565 /* Extract a bitfield value from value VAL using the bit parameters contained in
1566 watchpoint W. */
1567
1568 static struct value *
1569 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1570 {
1571 struct value *bit_val;
1572
1573 if (val == NULL)
1574 return NULL;
1575
1576 bit_val = allocate_value (value_type (val));
1577
1578 unpack_value_bitfield (bit_val,
1579 w->val_bitpos,
1580 w->val_bitsize,
1581 value_contents_for_printing (val),
1582 value_offset (val),
1583 val);
1584
1585 return bit_val;
1586 }
1587
1588 /* Allocate a dummy location and add it to B, which must be a software
1589 watchpoint. This is required because even if a software watchpoint
1590 is not watching any memory, bpstat_stop_status requires a location
1591 to be able to report stops. */
1592
1593 static void
1594 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1595 struct program_space *pspace)
1596 {
1597 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1598
1599 b->loc = allocate_bp_location (b);
1600 b->loc->pspace = pspace;
1601 b->loc->address = -1;
1602 b->loc->length = -1;
1603 }
1604
1605 /* Returns true if B is a software watchpoint that is not watching any
1606 memory (e.g., "watch $pc"). */
1607
1608 static bool
1609 is_no_memory_software_watchpoint (struct breakpoint *b)
1610 {
1611 return (b->type == bp_watchpoint
1612 && b->loc != NULL
1613 && b->loc->next == NULL
1614 && b->loc->address == -1
1615 && b->loc->length == -1);
1616 }
1617
1618 /* Assuming that B is a watchpoint:
1619 - Reparse watchpoint expression, if REPARSE is non-zero
1620 - Evaluate expression and store the result in B->val
1621 - Evaluate the condition if there is one, and store the result
1622 in b->loc->cond.
1623 - Update the list of values that must be watched in B->loc.
1624
1625 If the watchpoint disposition is disp_del_at_next_stop, then do
1626 nothing. If this is local watchpoint that is out of scope, delete
1627 it.
1628
1629 Even with `set breakpoint always-inserted on' the watchpoints are
1630 removed + inserted on each stop here. Normal breakpoints must
1631 never be removed because they might be missed by a running thread
1632 when debugging in non-stop mode. On the other hand, hardware
1633 watchpoints (is_hardware_watchpoint; processed here) are specific
1634 to each LWP since they are stored in each LWP's hardware debug
1635 registers. Therefore, such LWP must be stopped first in order to
1636 be able to modify its hardware watchpoints.
1637
1638 Hardware watchpoints must be reset exactly once after being
1639 presented to the user. It cannot be done sooner, because it would
1640 reset the data used to present the watchpoint hit to the user. And
1641 it must not be done later because it could display the same single
1642 watchpoint hit during multiple GDB stops. Note that the latter is
1643 relevant only to the hardware watchpoint types bp_read_watchpoint
1644 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1645 not user-visible - its hit is suppressed if the memory content has
1646 not changed.
1647
1648 The following constraints influence the location where we can reset
1649 hardware watchpoints:
1650
1651 * target_stopped_by_watchpoint and target_stopped_data_address are
1652 called several times when GDB stops.
1653
1654 [linux]
1655 * Multiple hardware watchpoints can be hit at the same time,
1656 causing GDB to stop. GDB only presents one hardware watchpoint
1657 hit at a time as the reason for stopping, and all the other hits
1658 are presented later, one after the other, each time the user
1659 requests the execution to be resumed. Execution is not resumed
1660 for the threads still having pending hit event stored in
1661 LWP_INFO->STATUS. While the watchpoint is already removed from
1662 the inferior on the first stop the thread hit event is kept being
1663 reported from its cached value by linux_nat_stopped_data_address
1664 until the real thread resume happens after the watchpoint gets
1665 presented and thus its LWP_INFO->STATUS gets reset.
1666
1667 Therefore the hardware watchpoint hit can get safely reset on the
1668 watchpoint removal from inferior. */
1669
1670 static void
1671 update_watchpoint (struct watchpoint *b, int reparse)
1672 {
1673 int within_current_scope;
1674 struct frame_id saved_frame_id;
1675 int frame_saved;
1676
1677 /* If this is a local watchpoint, we only want to check if the
1678 watchpoint frame is in scope if the current thread is the thread
1679 that was used to create the watchpoint. */
1680 if (!watchpoint_in_thread_scope (b))
1681 return;
1682
1683 if (b->disposition == disp_del_at_next_stop)
1684 return;
1685
1686 frame_saved = 0;
1687
1688 /* Determine if the watchpoint is within scope. */
1689 if (b->exp_valid_block == NULL)
1690 within_current_scope = 1;
1691 else
1692 {
1693 struct frame_info *fi = get_current_frame ();
1694 struct gdbarch *frame_arch = get_frame_arch (fi);
1695 CORE_ADDR frame_pc = get_frame_pc (fi);
1696
1697 /* If we're at a point where the stack has been destroyed
1698 (e.g. in a function epilogue), unwinding may not work
1699 properly. Do not attempt to recreate locations at this
1700 point. See similar comments in watchpoint_check. */
1701 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1702 return;
1703
1704 /* Save the current frame's ID so we can restore it after
1705 evaluating the watchpoint expression on its own frame. */
1706 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1707 took a frame parameter, so that we didn't have to change the
1708 selected frame. */
1709 frame_saved = 1;
1710 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1711
1712 fi = frame_find_by_id (b->watchpoint_frame);
1713 within_current_scope = (fi != NULL);
1714 if (within_current_scope)
1715 select_frame (fi);
1716 }
1717
1718 /* We don't free locations. They are stored in the bp_location array
1719 and update_global_location_list will eventually delete them and
1720 remove breakpoints if needed. */
1721 b->loc = NULL;
1722
1723 if (within_current_scope && reparse)
1724 {
1725 const char *s;
1726
1727 b->exp.reset ();
1728 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1729 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1730 /* If the meaning of expression itself changed, the old value is
1731 no longer relevant. We don't want to report a watchpoint hit
1732 to the user when the old value and the new value may actually
1733 be completely different objects. */
1734 b->val = NULL;
1735 b->val_valid = false;
1736
1737 /* Note that unlike with breakpoints, the watchpoint's condition
1738 expression is stored in the breakpoint object, not in the
1739 locations (re)created below. */
1740 if (b->cond_string != NULL)
1741 {
1742 b->cond_exp.reset ();
1743
1744 s = b->cond_string;
1745 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1746 }
1747 }
1748
1749 /* If we failed to parse the expression, for example because
1750 it refers to a global variable in a not-yet-loaded shared library,
1751 don't try to insert watchpoint. We don't automatically delete
1752 such watchpoint, though, since failure to parse expression
1753 is different from out-of-scope watchpoint. */
1754 if (!target_has_execution)
1755 {
1756 /* Without execution, memory can't change. No use to try and
1757 set watchpoint locations. The watchpoint will be reset when
1758 the target gains execution, through breakpoint_re_set. */
1759 if (!can_use_hw_watchpoints)
1760 {
1761 if (b->ops->works_in_software_mode (b))
1762 b->type = bp_watchpoint;
1763 else
1764 error (_("Can't set read/access watchpoint when "
1765 "hardware watchpoints are disabled."));
1766 }
1767 }
1768 else if (within_current_scope && b->exp)
1769 {
1770 int pc = 0;
1771 std::vector<value_ref_ptr> val_chain;
1772 struct value *v, *result;
1773 struct program_space *frame_pspace;
1774
1775 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
1776
1777 /* Avoid setting b->val if it's already set. The meaning of
1778 b->val is 'the last value' user saw, and we should update
1779 it only if we reported that last value to user. As it
1780 happens, the code that reports it updates b->val directly.
1781 We don't keep track of the memory value for masked
1782 watchpoints. */
1783 if (!b->val_valid && !is_masked_watchpoint (b))
1784 {
1785 if (b->val_bitsize != 0)
1786 v = extract_bitfield_from_watchpoint_value (b, v);
1787 b->val = release_value (v);
1788 b->val_valid = true;
1789 }
1790
1791 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1792
1793 /* Look at each value on the value chain. */
1794 gdb_assert (!val_chain.empty ());
1795 for (const value_ref_ptr &iter : val_chain)
1796 {
1797 v = iter.get ();
1798
1799 /* If it's a memory location, and GDB actually needed
1800 its contents to evaluate the expression, then we
1801 must watch it. If the first value returned is
1802 still lazy, that means an error occurred reading it;
1803 watch it anyway in case it becomes readable. */
1804 if (VALUE_LVAL (v) == lval_memory
1805 && (v == val_chain[0] || ! value_lazy (v)))
1806 {
1807 struct type *vtype = check_typedef (value_type (v));
1808
1809 /* We only watch structs and arrays if user asked
1810 for it explicitly, never if they just happen to
1811 appear in the middle of some value chain. */
1812 if (v == result
1813 || (vtype->code () != TYPE_CODE_STRUCT
1814 && vtype->code () != TYPE_CODE_ARRAY))
1815 {
1816 CORE_ADDR addr;
1817 enum target_hw_bp_type type;
1818 struct bp_location *loc, **tmp;
1819 int bitpos = 0, bitsize = 0;
1820
1821 if (value_bitsize (v) != 0)
1822 {
1823 /* Extract the bit parameters out from the bitfield
1824 sub-expression. */
1825 bitpos = value_bitpos (v);
1826 bitsize = value_bitsize (v);
1827 }
1828 else if (v == result && b->val_bitsize != 0)
1829 {
1830 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1831 lvalue whose bit parameters are saved in the fields
1832 VAL_BITPOS and VAL_BITSIZE. */
1833 bitpos = b->val_bitpos;
1834 bitsize = b->val_bitsize;
1835 }
1836
1837 addr = value_address (v);
1838 if (bitsize != 0)
1839 {
1840 /* Skip the bytes that don't contain the bitfield. */
1841 addr += bitpos / 8;
1842 }
1843
1844 type = hw_write;
1845 if (b->type == bp_read_watchpoint)
1846 type = hw_read;
1847 else if (b->type == bp_access_watchpoint)
1848 type = hw_access;
1849
1850 loc = allocate_bp_location (b);
1851 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1852 ;
1853 *tmp = loc;
1854 loc->gdbarch = get_type_arch (value_type (v));
1855
1856 loc->pspace = frame_pspace;
1857 loc->address = address_significant (loc->gdbarch, addr);
1858
1859 if (bitsize != 0)
1860 {
1861 /* Just cover the bytes that make up the bitfield. */
1862 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1863 }
1864 else
1865 loc->length = TYPE_LENGTH (value_type (v));
1866
1867 loc->watchpoint_type = type;
1868 }
1869 }
1870 }
1871
1872 /* Change the type of breakpoint between hardware assisted or
1873 an ordinary watchpoint depending on the hardware support
1874 and free hardware slots. REPARSE is set when the inferior
1875 is started. */
1876 if (reparse)
1877 {
1878 int reg_cnt;
1879 enum bp_loc_type loc_type;
1880 struct bp_location *bl;
1881
1882 reg_cnt = can_use_hardware_watchpoint (val_chain);
1883
1884 if (reg_cnt)
1885 {
1886 int i, target_resources_ok, other_type_used;
1887 enum bptype type;
1888
1889 /* Use an exact watchpoint when there's only one memory region to be
1890 watched, and only one debug register is needed to watch it. */
1891 b->exact = target_exact_watchpoints && reg_cnt == 1;
1892
1893 /* We need to determine how many resources are already
1894 used for all other hardware watchpoints plus this one
1895 to see if we still have enough resources to also fit
1896 this watchpoint in as well. */
1897
1898 /* If this is a software watchpoint, we try to turn it
1899 to a hardware one -- count resources as if B was of
1900 hardware watchpoint type. */
1901 type = b->type;
1902 if (type == bp_watchpoint)
1903 type = bp_hardware_watchpoint;
1904
1905 /* This watchpoint may or may not have been placed on
1906 the list yet at this point (it won't be in the list
1907 if we're trying to create it for the first time,
1908 through watch_command), so always account for it
1909 manually. */
1910
1911 /* Count resources used by all watchpoints except B. */
1912 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
1913
1914 /* Add in the resources needed for B. */
1915 i += hw_watchpoint_use_count (b);
1916
1917 target_resources_ok
1918 = target_can_use_hardware_watchpoint (type, i, other_type_used);
1919 if (target_resources_ok <= 0)
1920 {
1921 int sw_mode = b->ops->works_in_software_mode (b);
1922
1923 if (target_resources_ok == 0 && !sw_mode)
1924 error (_("Target does not support this type of "
1925 "hardware watchpoint."));
1926 else if (target_resources_ok < 0 && !sw_mode)
1927 error (_("There are not enough available hardware "
1928 "resources for this watchpoint."));
1929
1930 /* Downgrade to software watchpoint. */
1931 b->type = bp_watchpoint;
1932 }
1933 else
1934 {
1935 /* If this was a software watchpoint, we've just
1936 found we have enough resources to turn it to a
1937 hardware watchpoint. Otherwise, this is a
1938 nop. */
1939 b->type = type;
1940 }
1941 }
1942 else if (!b->ops->works_in_software_mode (b))
1943 {
1944 if (!can_use_hw_watchpoints)
1945 error (_("Can't set read/access watchpoint when "
1946 "hardware watchpoints are disabled."));
1947 else
1948 error (_("Expression cannot be implemented with "
1949 "read/access watchpoint."));
1950 }
1951 else
1952 b->type = bp_watchpoint;
1953
1954 loc_type = (b->type == bp_watchpoint? bp_loc_other
1955 : bp_loc_hardware_watchpoint);
1956 for (bl = b->loc; bl; bl = bl->next)
1957 bl->loc_type = loc_type;
1958 }
1959
1960 /* If a software watchpoint is not watching any memory, then the
1961 above left it without any location set up. But,
1962 bpstat_stop_status requires a location to be able to report
1963 stops, so make sure there's at least a dummy one. */
1964 if (b->type == bp_watchpoint && b->loc == NULL)
1965 software_watchpoint_add_no_memory_location (b, frame_pspace);
1966 }
1967 else if (!within_current_scope)
1968 {
1969 printf_filtered (_("\
1970 Watchpoint %d deleted because the program has left the block\n\
1971 in which its expression is valid.\n"),
1972 b->number);
1973 watchpoint_del_at_next_stop (b);
1974 }
1975
1976 /* Restore the selected frame. */
1977 if (frame_saved)
1978 select_frame (frame_find_by_id (saved_frame_id));
1979 }
1980
1981
1982 /* Returns 1 iff breakpoint location should be
1983 inserted in the inferior. We don't differentiate the type of BL's owner
1984 (breakpoint vs. tracepoint), although insert_location in tracepoint's
1985 breakpoint_ops is not defined, because in insert_bp_location,
1986 tracepoint's insert_location will not be called. */
1987 static int
1988 should_be_inserted (struct bp_location *bl)
1989 {
1990 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
1991 return 0;
1992
1993 if (bl->owner->disposition == disp_del_at_next_stop)
1994 return 0;
1995
1996 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
1997 return 0;
1998
1999 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2000 return 0;
2001
2002 /* This is set for example, when we're attached to the parent of a
2003 vfork, and have detached from the child. The child is running
2004 free, and we expect it to do an exec or exit, at which point the
2005 OS makes the parent schedulable again (and the target reports
2006 that the vfork is done). Until the child is done with the shared
2007 memory region, do not insert breakpoints in the parent, otherwise
2008 the child could still trip on the parent's breakpoints. Since
2009 the parent is blocked anyway, it won't miss any breakpoint. */
2010 if (bl->pspace->breakpoints_not_allowed)
2011 return 0;
2012
2013 /* Don't insert a breakpoint if we're trying to step past its
2014 location, except if the breakpoint is a single-step breakpoint,
2015 and the breakpoint's thread is the thread which is stepping past
2016 a breakpoint. */
2017 if ((bl->loc_type == bp_loc_software_breakpoint
2018 || bl->loc_type == bp_loc_hardware_breakpoint)
2019 && stepping_past_instruction_at (bl->pspace->aspace,
2020 bl->address)
2021 /* The single-step breakpoint may be inserted at the location
2022 we're trying to step if the instruction branches to itself.
2023 However, the instruction won't be executed at all and it may
2024 break the semantics of the instruction, for example, the
2025 instruction is a conditional branch or updates some flags.
2026 We can't fix it unless GDB is able to emulate the instruction
2027 or switch to displaced stepping. */
2028 && !(bl->owner->type == bp_single_step
2029 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2030 {
2031 if (debug_infrun)
2032 {
2033 fprintf_unfiltered (gdb_stdlog,
2034 "infrun: skipping breakpoint: "
2035 "stepping past insn at: %s\n",
2036 paddress (bl->gdbarch, bl->address));
2037 }
2038 return 0;
2039 }
2040
2041 /* Don't insert watchpoints if we're trying to step past the
2042 instruction that triggered one. */
2043 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2044 && stepping_past_nonsteppable_watchpoint ())
2045 {
2046 if (debug_infrun)
2047 {
2048 fprintf_unfiltered (gdb_stdlog,
2049 "infrun: stepping past non-steppable watchpoint. "
2050 "skipping watchpoint at %s:%d\n",
2051 paddress (bl->gdbarch, bl->address),
2052 bl->length);
2053 }
2054 return 0;
2055 }
2056
2057 return 1;
2058 }
2059
2060 /* Same as should_be_inserted but does the check assuming
2061 that the location is not duplicated. */
2062
2063 static int
2064 unduplicated_should_be_inserted (struct bp_location *bl)
2065 {
2066 int result;
2067 const int save_duplicate = bl->duplicate;
2068
2069 bl->duplicate = 0;
2070 result = should_be_inserted (bl);
2071 bl->duplicate = save_duplicate;
2072 return result;
2073 }
2074
2075 /* Parses a conditional described by an expression COND into an
2076 agent expression bytecode suitable for evaluation
2077 by the bytecode interpreter. Return NULL if there was
2078 any error during parsing. */
2079
2080 static agent_expr_up
2081 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2082 {
2083 if (cond == NULL)
2084 return NULL;
2085
2086 agent_expr_up aexpr;
2087
2088 /* We don't want to stop processing, so catch any errors
2089 that may show up. */
2090 try
2091 {
2092 aexpr = gen_eval_for_expr (scope, cond);
2093 }
2094
2095 catch (const gdb_exception_error &ex)
2096 {
2097 /* If we got here, it means the condition could not be parsed to a valid
2098 bytecode expression and thus can't be evaluated on the target's side.
2099 It's no use iterating through the conditions. */
2100 }
2101
2102 /* We have a valid agent expression. */
2103 return aexpr;
2104 }
2105
2106 /* Based on location BL, create a list of breakpoint conditions to be
2107 passed on to the target. If we have duplicated locations with different
2108 conditions, we will add such conditions to the list. The idea is that the
2109 target will evaluate the list of conditions and will only notify GDB when
2110 one of them is true. */
2111
2112 static void
2113 build_target_condition_list (struct bp_location *bl)
2114 {
2115 struct bp_location **locp = NULL, **loc2p;
2116 int null_condition_or_parse_error = 0;
2117 int modified = bl->needs_update;
2118 struct bp_location *loc;
2119
2120 /* Release conditions left over from a previous insert. */
2121 bl->target_info.conditions.clear ();
2122
2123 /* This is only meaningful if the target is
2124 evaluating conditions and if the user has
2125 opted for condition evaluation on the target's
2126 side. */
2127 if (gdb_evaluates_breakpoint_condition_p ()
2128 || !target_supports_evaluation_of_breakpoint_conditions ())
2129 return;
2130
2131 /* Do a first pass to check for locations with no assigned
2132 conditions or conditions that fail to parse to a valid agent
2133 expression bytecode. If any of these happen, then it's no use to
2134 send conditions to the target since this location will always
2135 trigger and generate a response back to GDB. Note we consider
2136 all locations at the same address irrespective of type, i.e.,
2137 even if the locations aren't considered duplicates (e.g.,
2138 software breakpoint and hardware breakpoint at the same
2139 address). */
2140 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2141 {
2142 loc = (*loc2p);
2143 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2144 {
2145 if (modified)
2146 {
2147 /* Re-parse the conditions since something changed. In that
2148 case we already freed the condition bytecodes (see
2149 force_breakpoint_reinsertion). We just
2150 need to parse the condition to bytecodes again. */
2151 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2152 loc->cond.get ());
2153 }
2154
2155 /* If we have a NULL bytecode expression, it means something
2156 went wrong or we have a null condition expression. */
2157 if (!loc->cond_bytecode)
2158 {
2159 null_condition_or_parse_error = 1;
2160 break;
2161 }
2162 }
2163 }
2164
2165 /* If any of these happened, it means we will have to evaluate the conditions
2166 for the location's address on gdb's side. It is no use keeping bytecodes
2167 for all the other duplicate locations, thus we free all of them here.
2168
2169 This is so we have a finer control over which locations' conditions are
2170 being evaluated by GDB or the remote stub. */
2171 if (null_condition_or_parse_error)
2172 {
2173 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2174 {
2175 loc = (*loc2p);
2176 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2177 {
2178 /* Only go as far as the first NULL bytecode is
2179 located. */
2180 if (!loc->cond_bytecode)
2181 return;
2182
2183 loc->cond_bytecode.reset ();
2184 }
2185 }
2186 }
2187
2188 /* No NULL conditions or failed bytecode generation. Build a
2189 condition list for this location's address. If we have software
2190 and hardware locations at the same address, they aren't
2191 considered duplicates, but we still marge all the conditions
2192 anyway, as it's simpler, and doesn't really make a practical
2193 difference. */
2194 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2195 {
2196 loc = (*loc2p);
2197 if (loc->cond
2198 && is_breakpoint (loc->owner)
2199 && loc->pspace->num == bl->pspace->num
2200 && loc->owner->enable_state == bp_enabled
2201 && loc->enabled)
2202 {
2203 /* Add the condition to the vector. This will be used later
2204 to send the conditions to the target. */
2205 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2206 }
2207 }
2208
2209 return;
2210 }
2211
2212 /* Parses a command described by string CMD into an agent expression
2213 bytecode suitable for evaluation by the bytecode interpreter.
2214 Return NULL if there was any error during parsing. */
2215
2216 static agent_expr_up
2217 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2218 {
2219 const char *cmdrest;
2220 const char *format_start, *format_end;
2221 struct gdbarch *gdbarch = get_current_arch ();
2222
2223 if (cmd == NULL)
2224 return NULL;
2225
2226 cmdrest = cmd;
2227
2228 if (*cmdrest == ',')
2229 ++cmdrest;
2230 cmdrest = skip_spaces (cmdrest);
2231
2232 if (*cmdrest++ != '"')
2233 error (_("No format string following the location"));
2234
2235 format_start = cmdrest;
2236
2237 format_pieces fpieces (&cmdrest);
2238
2239 format_end = cmdrest;
2240
2241 if (*cmdrest++ != '"')
2242 error (_("Bad format string, non-terminated '\"'."));
2243
2244 cmdrest = skip_spaces (cmdrest);
2245
2246 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2247 error (_("Invalid argument syntax"));
2248
2249 if (*cmdrest == ',')
2250 cmdrest++;
2251 cmdrest = skip_spaces (cmdrest);
2252
2253 /* For each argument, make an expression. */
2254
2255 std::vector<struct expression *> argvec;
2256 while (*cmdrest != '\0')
2257 {
2258 const char *cmd1;
2259
2260 cmd1 = cmdrest;
2261 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2262 argvec.push_back (expr.release ());
2263 cmdrest = cmd1;
2264 if (*cmdrest == ',')
2265 ++cmdrest;
2266 }
2267
2268 agent_expr_up aexpr;
2269
2270 /* We don't want to stop processing, so catch any errors
2271 that may show up. */
2272 try
2273 {
2274 aexpr = gen_printf (scope, gdbarch, 0, 0,
2275 format_start, format_end - format_start,
2276 argvec.size (), argvec.data ());
2277 }
2278 catch (const gdb_exception_error &ex)
2279 {
2280 /* If we got here, it means the command could not be parsed to a valid
2281 bytecode expression and thus can't be evaluated on the target's side.
2282 It's no use iterating through the other commands. */
2283 }
2284
2285 /* We have a valid agent expression, return it. */
2286 return aexpr;
2287 }
2288
2289 /* Based on location BL, create a list of breakpoint commands to be
2290 passed on to the target. If we have duplicated locations with
2291 different commands, we will add any such to the list. */
2292
2293 static void
2294 build_target_command_list (struct bp_location *bl)
2295 {
2296 struct bp_location **locp = NULL, **loc2p;
2297 int null_command_or_parse_error = 0;
2298 int modified = bl->needs_update;
2299 struct bp_location *loc;
2300
2301 /* Clear commands left over from a previous insert. */
2302 bl->target_info.tcommands.clear ();
2303
2304 if (!target_can_run_breakpoint_commands ())
2305 return;
2306
2307 /* For now, limit to agent-style dprintf breakpoints. */
2308 if (dprintf_style != dprintf_style_agent)
2309 return;
2310
2311 /* For now, if we have any location at the same address that isn't a
2312 dprintf, don't install the target-side commands, as that would
2313 make the breakpoint not be reported to the core, and we'd lose
2314 control. */
2315 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2316 {
2317 loc = (*loc2p);
2318 if (is_breakpoint (loc->owner)
2319 && loc->pspace->num == bl->pspace->num
2320 && loc->owner->type != bp_dprintf)
2321 return;
2322 }
2323
2324 /* Do a first pass to check for locations with no assigned
2325 conditions or conditions that fail to parse to a valid agent expression
2326 bytecode. If any of these happen, then it's no use to send conditions
2327 to the target since this location will always trigger and generate a
2328 response back to GDB. */
2329 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2330 {
2331 loc = (*loc2p);
2332 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2333 {
2334 if (modified)
2335 {
2336 /* Re-parse the commands since something changed. In that
2337 case we already freed the command bytecodes (see
2338 force_breakpoint_reinsertion). We just
2339 need to parse the command to bytecodes again. */
2340 loc->cmd_bytecode
2341 = parse_cmd_to_aexpr (bl->address,
2342 loc->owner->extra_string);
2343 }
2344
2345 /* If we have a NULL bytecode expression, it means something
2346 went wrong or we have a null command expression. */
2347 if (!loc->cmd_bytecode)
2348 {
2349 null_command_or_parse_error = 1;
2350 break;
2351 }
2352 }
2353 }
2354
2355 /* If anything failed, then we're not doing target-side commands,
2356 and so clean up. */
2357 if (null_command_or_parse_error)
2358 {
2359 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2360 {
2361 loc = (*loc2p);
2362 if (is_breakpoint (loc->owner)
2363 && loc->pspace->num == bl->pspace->num)
2364 {
2365 /* Only go as far as the first NULL bytecode is
2366 located. */
2367 if (loc->cmd_bytecode == NULL)
2368 return;
2369
2370 loc->cmd_bytecode.reset ();
2371 }
2372 }
2373 }
2374
2375 /* No NULL commands or failed bytecode generation. Build a command
2376 list for all duplicate locations at this location's address.
2377 Note that here we must care for whether the breakpoint location
2378 types are considered duplicates, otherwise, say, if we have a
2379 software and hardware location at the same address, the target
2380 could end up running the commands twice. For the moment, we only
2381 support targets-side commands with dprintf, but it doesn't hurt
2382 to be pedantically correct in case that changes. */
2383 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2384 {
2385 loc = (*loc2p);
2386 if (breakpoint_locations_match (bl, loc)
2387 && loc->owner->extra_string
2388 && is_breakpoint (loc->owner)
2389 && loc->pspace->num == bl->pspace->num
2390 && loc->owner->enable_state == bp_enabled
2391 && loc->enabled)
2392 {
2393 /* Add the command to the vector. This will be used later
2394 to send the commands to the target. */
2395 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2396 }
2397 }
2398
2399 bl->target_info.persist = 0;
2400 /* Maybe flag this location as persistent. */
2401 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2402 bl->target_info.persist = 1;
2403 }
2404
2405 /* Return the kind of breakpoint on address *ADDR. Get the kind
2406 of breakpoint according to ADDR except single-step breakpoint.
2407 Get the kind of single-step breakpoint according to the current
2408 registers state. */
2409
2410 static int
2411 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2412 {
2413 if (bl->owner->type == bp_single_step)
2414 {
2415 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2416 struct regcache *regcache;
2417
2418 regcache = get_thread_regcache (thr);
2419
2420 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2421 regcache, addr);
2422 }
2423 else
2424 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2425 }
2426
2427 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2428 location. Any error messages are printed to TMP_ERROR_STREAM; and
2429 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2430 Returns 0 for success, 1 if the bp_location type is not supported or
2431 -1 for failure.
2432
2433 NOTE drow/2003-09-09: This routine could be broken down to an
2434 object-style method for each breakpoint or catchpoint type. */
2435 static int
2436 insert_bp_location (struct bp_location *bl,
2437 struct ui_file *tmp_error_stream,
2438 int *disabled_breaks,
2439 int *hw_breakpoint_error,
2440 int *hw_bp_error_explained_already)
2441 {
2442 gdb_exception bp_excpt;
2443
2444 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2445 return 0;
2446
2447 /* Note we don't initialize bl->target_info, as that wipes out
2448 the breakpoint location's shadow_contents if the breakpoint
2449 is still inserted at that location. This in turn breaks
2450 target_read_memory which depends on these buffers when
2451 a memory read is requested at the breakpoint location:
2452 Once the target_info has been wiped, we fail to see that
2453 we have a breakpoint inserted at that address and thus
2454 read the breakpoint instead of returning the data saved in
2455 the breakpoint location's shadow contents. */
2456 bl->target_info.reqstd_address = bl->address;
2457 bl->target_info.placed_address_space = bl->pspace->aspace;
2458 bl->target_info.length = bl->length;
2459
2460 /* When working with target-side conditions, we must pass all the conditions
2461 for the same breakpoint address down to the target since GDB will not
2462 insert those locations. With a list of breakpoint conditions, the target
2463 can decide when to stop and notify GDB. */
2464
2465 if (is_breakpoint (bl->owner))
2466 {
2467 build_target_condition_list (bl);
2468 build_target_command_list (bl);
2469 /* Reset the modification marker. */
2470 bl->needs_update = 0;
2471 }
2472
2473 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2474 set at a read-only address, then a breakpoint location will have
2475 been changed to hardware breakpoint before we get here. If it is
2476 "off" however, error out before actually trying to insert the
2477 breakpoint, with a nicer error message. */
2478 if (bl->loc_type == bp_loc_software_breakpoint
2479 && !automatic_hardware_breakpoints)
2480 {
2481 mem_region *mr = lookup_mem_region (bl->address);
2482
2483 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2484 {
2485 fprintf_unfiltered (tmp_error_stream,
2486 _("Cannot insert breakpoint %d.\n"
2487 "Cannot set software breakpoint "
2488 "at read-only address %s\n"),
2489 bl->owner->number,
2490 paddress (bl->gdbarch, bl->address));
2491 return 1;
2492 }
2493 }
2494
2495 if (bl->loc_type == bp_loc_software_breakpoint
2496 || bl->loc_type == bp_loc_hardware_breakpoint)
2497 {
2498 /* First check to see if we have to handle an overlay. */
2499 if (overlay_debugging == ovly_off
2500 || bl->section == NULL
2501 || !(section_is_overlay (bl->section)))
2502 {
2503 /* No overlay handling: just set the breakpoint. */
2504 try
2505 {
2506 int val;
2507
2508 val = bl->owner->ops->insert_location (bl);
2509 if (val)
2510 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2511 }
2512 catch (gdb_exception &e)
2513 {
2514 bp_excpt = std::move (e);
2515 }
2516 }
2517 else
2518 {
2519 /* This breakpoint is in an overlay section.
2520 Shall we set a breakpoint at the LMA? */
2521 if (!overlay_events_enabled)
2522 {
2523 /* Yes -- overlay event support is not active,
2524 so we must try to set a breakpoint at the LMA.
2525 This will not work for a hardware breakpoint. */
2526 if (bl->loc_type == bp_loc_hardware_breakpoint)
2527 warning (_("hardware breakpoint %d not supported in overlay!"),
2528 bl->owner->number);
2529 else
2530 {
2531 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2532 bl->section);
2533 /* Set a software (trap) breakpoint at the LMA. */
2534 bl->overlay_target_info = bl->target_info;
2535 bl->overlay_target_info.reqstd_address = addr;
2536
2537 /* No overlay handling: just set the breakpoint. */
2538 try
2539 {
2540 int val;
2541
2542 bl->overlay_target_info.kind
2543 = breakpoint_kind (bl, &addr);
2544 bl->overlay_target_info.placed_address = addr;
2545 val = target_insert_breakpoint (bl->gdbarch,
2546 &bl->overlay_target_info);
2547 if (val)
2548 bp_excpt
2549 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2550 }
2551 catch (gdb_exception &e)
2552 {
2553 bp_excpt = std::move (e);
2554 }
2555
2556 if (bp_excpt.reason != 0)
2557 fprintf_unfiltered (tmp_error_stream,
2558 "Overlay breakpoint %d "
2559 "failed: in ROM?\n",
2560 bl->owner->number);
2561 }
2562 }
2563 /* Shall we set a breakpoint at the VMA? */
2564 if (section_is_mapped (bl->section))
2565 {
2566 /* Yes. This overlay section is mapped into memory. */
2567 try
2568 {
2569 int val;
2570
2571 val = bl->owner->ops->insert_location (bl);
2572 if (val)
2573 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2574 }
2575 catch (gdb_exception &e)
2576 {
2577 bp_excpt = std::move (e);
2578 }
2579 }
2580 else
2581 {
2582 /* No. This breakpoint will not be inserted.
2583 No error, but do not mark the bp as 'inserted'. */
2584 return 0;
2585 }
2586 }
2587
2588 if (bp_excpt.reason != 0)
2589 {
2590 /* Can't set the breakpoint. */
2591
2592 /* In some cases, we might not be able to insert a
2593 breakpoint in a shared library that has already been
2594 removed, but we have not yet processed the shlib unload
2595 event. Unfortunately, some targets that implement
2596 breakpoint insertion themselves can't tell why the
2597 breakpoint insertion failed (e.g., the remote target
2598 doesn't define error codes), so we must treat generic
2599 errors as memory errors. */
2600 if (bp_excpt.reason == RETURN_ERROR
2601 && (bp_excpt.error == GENERIC_ERROR
2602 || bp_excpt.error == MEMORY_ERROR)
2603 && bl->loc_type == bp_loc_software_breakpoint
2604 && (solib_name_from_address (bl->pspace, bl->address)
2605 || shared_objfile_contains_address_p (bl->pspace,
2606 bl->address)))
2607 {
2608 /* See also: disable_breakpoints_in_shlibs. */
2609 bl->shlib_disabled = 1;
2610 gdb::observers::breakpoint_modified.notify (bl->owner);
2611 if (!*disabled_breaks)
2612 {
2613 fprintf_unfiltered (tmp_error_stream,
2614 "Cannot insert breakpoint %d.\n",
2615 bl->owner->number);
2616 fprintf_unfiltered (tmp_error_stream,
2617 "Temporarily disabling shared "
2618 "library breakpoints:\n");
2619 }
2620 *disabled_breaks = 1;
2621 fprintf_unfiltered (tmp_error_stream,
2622 "breakpoint #%d\n", bl->owner->number);
2623 return 0;
2624 }
2625 else
2626 {
2627 if (bl->loc_type == bp_loc_hardware_breakpoint)
2628 {
2629 *hw_breakpoint_error = 1;
2630 *hw_bp_error_explained_already = bp_excpt.message != NULL;
2631 fprintf_unfiltered (tmp_error_stream,
2632 "Cannot insert hardware breakpoint %d%s",
2633 bl->owner->number,
2634 bp_excpt.message ? ":" : ".\n");
2635 if (bp_excpt.message != NULL)
2636 fprintf_unfiltered (tmp_error_stream, "%s.\n",
2637 bp_excpt.what ());
2638 }
2639 else
2640 {
2641 if (bp_excpt.message == NULL)
2642 {
2643 std::string message
2644 = memory_error_message (TARGET_XFER_E_IO,
2645 bl->gdbarch, bl->address);
2646
2647 fprintf_unfiltered (tmp_error_stream,
2648 "Cannot insert breakpoint %d.\n"
2649 "%s\n",
2650 bl->owner->number, message.c_str ());
2651 }
2652 else
2653 {
2654 fprintf_unfiltered (tmp_error_stream,
2655 "Cannot insert breakpoint %d: %s\n",
2656 bl->owner->number,
2657 bp_excpt.what ());
2658 }
2659 }
2660 return 1;
2661
2662 }
2663 }
2664 else
2665 bl->inserted = 1;
2666
2667 return 0;
2668 }
2669
2670 else if (bl->loc_type == bp_loc_hardware_watchpoint
2671 /* NOTE drow/2003-09-08: This state only exists for removing
2672 watchpoints. It's not clear that it's necessary... */
2673 && bl->owner->disposition != disp_del_at_next_stop)
2674 {
2675 int val;
2676
2677 gdb_assert (bl->owner->ops != NULL
2678 && bl->owner->ops->insert_location != NULL);
2679
2680 val = bl->owner->ops->insert_location (bl);
2681
2682 /* If trying to set a read-watchpoint, and it turns out it's not
2683 supported, try emulating one with an access watchpoint. */
2684 if (val == 1 && bl->watchpoint_type == hw_read)
2685 {
2686 struct bp_location *loc, **loc_temp;
2687
2688 /* But don't try to insert it, if there's already another
2689 hw_access location that would be considered a duplicate
2690 of this one. */
2691 ALL_BP_LOCATIONS (loc, loc_temp)
2692 if (loc != bl
2693 && loc->watchpoint_type == hw_access
2694 && watchpoint_locations_match (bl, loc))
2695 {
2696 bl->duplicate = 1;
2697 bl->inserted = 1;
2698 bl->target_info = loc->target_info;
2699 bl->watchpoint_type = hw_access;
2700 val = 0;
2701 break;
2702 }
2703
2704 if (val == 1)
2705 {
2706 bl->watchpoint_type = hw_access;
2707 val = bl->owner->ops->insert_location (bl);
2708
2709 if (val)
2710 /* Back to the original value. */
2711 bl->watchpoint_type = hw_read;
2712 }
2713 }
2714
2715 bl->inserted = (val == 0);
2716 }
2717
2718 else if (bl->owner->type == bp_catchpoint)
2719 {
2720 int val;
2721
2722 gdb_assert (bl->owner->ops != NULL
2723 && bl->owner->ops->insert_location != NULL);
2724
2725 val = bl->owner->ops->insert_location (bl);
2726 if (val)
2727 {
2728 bl->owner->enable_state = bp_disabled;
2729
2730 if (val == 1)
2731 warning (_("\
2732 Error inserting catchpoint %d: Your system does not support this type\n\
2733 of catchpoint."), bl->owner->number);
2734 else
2735 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2736 }
2737
2738 bl->inserted = (val == 0);
2739
2740 /* We've already printed an error message if there was a problem
2741 inserting this catchpoint, and we've disabled the catchpoint,
2742 so just return success. */
2743 return 0;
2744 }
2745
2746 return 0;
2747 }
2748
2749 /* This function is called when program space PSPACE is about to be
2750 deleted. It takes care of updating breakpoints to not reference
2751 PSPACE anymore. */
2752
2753 void
2754 breakpoint_program_space_exit (struct program_space *pspace)
2755 {
2756 struct breakpoint *b, *b_temp;
2757 struct bp_location *loc, **loc_temp;
2758
2759 /* Remove any breakpoint that was set through this program space. */
2760 ALL_BREAKPOINTS_SAFE (b, b_temp)
2761 {
2762 if (b->pspace == pspace)
2763 delete_breakpoint (b);
2764 }
2765
2766 /* Breakpoints set through other program spaces could have locations
2767 bound to PSPACE as well. Remove those. */
2768 ALL_BP_LOCATIONS (loc, loc_temp)
2769 {
2770 struct bp_location *tmp;
2771
2772 if (loc->pspace == pspace)
2773 {
2774 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2775 if (loc->owner->loc == loc)
2776 loc->owner->loc = loc->next;
2777 else
2778 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2779 if (tmp->next == loc)
2780 {
2781 tmp->next = loc->next;
2782 break;
2783 }
2784 }
2785 }
2786
2787 /* Now update the global location list to permanently delete the
2788 removed locations above. */
2789 update_global_location_list (UGLL_DONT_INSERT);
2790 }
2791
2792 /* Make sure all breakpoints are inserted in inferior.
2793 Throws exception on any error.
2794 A breakpoint that is already inserted won't be inserted
2795 again, so calling this function twice is safe. */
2796 void
2797 insert_breakpoints (void)
2798 {
2799 struct breakpoint *bpt;
2800
2801 ALL_BREAKPOINTS (bpt)
2802 if (is_hardware_watchpoint (bpt))
2803 {
2804 struct watchpoint *w = (struct watchpoint *) bpt;
2805
2806 update_watchpoint (w, 0 /* don't reparse. */);
2807 }
2808
2809 /* Updating watchpoints creates new locations, so update the global
2810 location list. Explicitly tell ugll to insert locations and
2811 ignore breakpoints_always_inserted_mode. Also,
2812 update_global_location_list tries to "upgrade" software
2813 breakpoints to hardware breakpoints to handle "set breakpoint
2814 auto-hw", so we need to call it even if we don't have new
2815 locations. */
2816 update_global_location_list (UGLL_INSERT);
2817 }
2818
2819 /* Invoke CALLBACK for each of bp_location. */
2820
2821 void
2822 iterate_over_bp_locations (walk_bp_location_callback callback)
2823 {
2824 struct bp_location *loc, **loc_tmp;
2825
2826 ALL_BP_LOCATIONS (loc, loc_tmp)
2827 {
2828 callback (loc, NULL);
2829 }
2830 }
2831
2832 /* This is used when we need to synch breakpoint conditions between GDB and the
2833 target. It is the case with deleting and disabling of breakpoints when using
2834 always-inserted mode. */
2835
2836 static void
2837 update_inserted_breakpoint_locations (void)
2838 {
2839 struct bp_location *bl, **blp_tmp;
2840 int error_flag = 0;
2841 int val = 0;
2842 int disabled_breaks = 0;
2843 int hw_breakpoint_error = 0;
2844 int hw_bp_details_reported = 0;
2845
2846 string_file tmp_error_stream;
2847
2848 /* Explicitly mark the warning -- this will only be printed if
2849 there was an error. */
2850 tmp_error_stream.puts ("Warning:\n");
2851
2852 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2853
2854 ALL_BP_LOCATIONS (bl, blp_tmp)
2855 {
2856 /* We only want to update software breakpoints and hardware
2857 breakpoints. */
2858 if (!is_breakpoint (bl->owner))
2859 continue;
2860
2861 /* We only want to update locations that are already inserted
2862 and need updating. This is to avoid unwanted insertion during
2863 deletion of breakpoints. */
2864 if (!bl->inserted || !bl->needs_update)
2865 continue;
2866
2867 switch_to_program_space_and_thread (bl->pspace);
2868
2869 /* For targets that support global breakpoints, there's no need
2870 to select an inferior to insert breakpoint to. In fact, even
2871 if we aren't attached to any process yet, we should still
2872 insert breakpoints. */
2873 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2874 && (inferior_ptid == null_ptid || !target_has_execution))
2875 continue;
2876
2877 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
2878 &hw_breakpoint_error, &hw_bp_details_reported);
2879 if (val)
2880 error_flag = val;
2881 }
2882
2883 if (error_flag)
2884 {
2885 target_terminal::ours_for_output ();
2886 error_stream (tmp_error_stream);
2887 }
2888 }
2889
2890 /* Used when starting or continuing the program. */
2891
2892 static void
2893 insert_breakpoint_locations (void)
2894 {
2895 struct breakpoint *bpt;
2896 struct bp_location *bl, **blp_tmp;
2897 int error_flag = 0;
2898 int val = 0;
2899 int disabled_breaks = 0;
2900 int hw_breakpoint_error = 0;
2901 int hw_bp_error_explained_already = 0;
2902
2903 string_file tmp_error_stream;
2904
2905 /* Explicitly mark the warning -- this will only be printed if
2906 there was an error. */
2907 tmp_error_stream.puts ("Warning:\n");
2908
2909 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2910
2911 ALL_BP_LOCATIONS (bl, blp_tmp)
2912 {
2913 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2914 continue;
2915
2916 /* There is no point inserting thread-specific breakpoints if
2917 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2918 has BL->OWNER always non-NULL. */
2919 if (bl->owner->thread != -1
2920 && !valid_global_thread_id (bl->owner->thread))
2921 continue;
2922
2923 switch_to_program_space_and_thread (bl->pspace);
2924
2925 /* For targets that support global breakpoints, there's no need
2926 to select an inferior to insert breakpoint to. In fact, even
2927 if we aren't attached to any process yet, we should still
2928 insert breakpoints. */
2929 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2930 && (inferior_ptid == null_ptid || !target_has_execution))
2931 continue;
2932
2933 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
2934 &hw_breakpoint_error, &hw_bp_error_explained_already);
2935 if (val)
2936 error_flag = val;
2937 }
2938
2939 /* If we failed to insert all locations of a watchpoint, remove
2940 them, as half-inserted watchpoint is of limited use. */
2941 ALL_BREAKPOINTS (bpt)
2942 {
2943 int some_failed = 0;
2944 struct bp_location *loc;
2945
2946 if (!is_hardware_watchpoint (bpt))
2947 continue;
2948
2949 if (!breakpoint_enabled (bpt))
2950 continue;
2951
2952 if (bpt->disposition == disp_del_at_next_stop)
2953 continue;
2954
2955 for (loc = bpt->loc; loc; loc = loc->next)
2956 if (!loc->inserted && should_be_inserted (loc))
2957 {
2958 some_failed = 1;
2959 break;
2960 }
2961 if (some_failed)
2962 {
2963 for (loc = bpt->loc; loc; loc = loc->next)
2964 if (loc->inserted)
2965 remove_breakpoint (loc);
2966
2967 hw_breakpoint_error = 1;
2968 tmp_error_stream.printf ("Could not insert "
2969 "hardware watchpoint %d.\n",
2970 bpt->number);
2971 error_flag = -1;
2972 }
2973 }
2974
2975 if (error_flag)
2976 {
2977 /* If a hardware breakpoint or watchpoint was inserted, add a
2978 message about possibly exhausted resources. */
2979 if (hw_breakpoint_error && !hw_bp_error_explained_already)
2980 {
2981 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
2982 You may have requested too many hardware breakpoints/watchpoints.\n");
2983 }
2984 target_terminal::ours_for_output ();
2985 error_stream (tmp_error_stream);
2986 }
2987 }
2988
2989 /* Used when the program stops.
2990 Returns zero if successful, or non-zero if there was a problem
2991 removing a breakpoint location. */
2992
2993 int
2994 remove_breakpoints (void)
2995 {
2996 struct bp_location *bl, **blp_tmp;
2997 int val = 0;
2998
2999 ALL_BP_LOCATIONS (bl, blp_tmp)
3000 {
3001 if (bl->inserted && !is_tracepoint (bl->owner))
3002 val |= remove_breakpoint (bl);
3003 }
3004 return val;
3005 }
3006
3007 /* When a thread exits, remove breakpoints that are related to
3008 that thread. */
3009
3010 static void
3011 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3012 {
3013 struct breakpoint *b, *b_tmp;
3014
3015 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3016 {
3017 if (b->thread == tp->global_num && user_breakpoint_p (b))
3018 {
3019 b->disposition = disp_del_at_next_stop;
3020
3021 printf_filtered (_("\
3022 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3023 b->number, print_thread_id (tp));
3024
3025 /* Hide it from the user. */
3026 b->number = 0;
3027 }
3028 }
3029 }
3030
3031 /* See breakpoint.h. */
3032
3033 void
3034 remove_breakpoints_inf (inferior *inf)
3035 {
3036 struct bp_location *bl, **blp_tmp;
3037 int val;
3038
3039 ALL_BP_LOCATIONS (bl, blp_tmp)
3040 {
3041 if (bl->pspace != inf->pspace)
3042 continue;
3043
3044 if (bl->inserted && !bl->target_info.persist)
3045 {
3046 val = remove_breakpoint (bl);
3047 if (val != 0)
3048 return;
3049 }
3050 }
3051 }
3052
3053 static int internal_breakpoint_number = -1;
3054
3055 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3056 If INTERNAL is non-zero, the breakpoint number will be populated
3057 from internal_breakpoint_number and that variable decremented.
3058 Otherwise the breakpoint number will be populated from
3059 breakpoint_count and that value incremented. Internal breakpoints
3060 do not set the internal var bpnum. */
3061 static void
3062 set_breakpoint_number (int internal, struct breakpoint *b)
3063 {
3064 if (internal)
3065 b->number = internal_breakpoint_number--;
3066 else
3067 {
3068 set_breakpoint_count (breakpoint_count + 1);
3069 b->number = breakpoint_count;
3070 }
3071 }
3072
3073 static struct breakpoint *
3074 create_internal_breakpoint (struct gdbarch *gdbarch,
3075 CORE_ADDR address, enum bptype type,
3076 const struct breakpoint_ops *ops)
3077 {
3078 symtab_and_line sal;
3079 sal.pc = address;
3080 sal.section = find_pc_overlay (sal.pc);
3081 sal.pspace = current_program_space;
3082
3083 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
3084 b->number = internal_breakpoint_number--;
3085 b->disposition = disp_donttouch;
3086
3087 return b;
3088 }
3089
3090 static const char *const longjmp_names[] =
3091 {
3092 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3093 };
3094 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3095
3096 /* Per-objfile data private to breakpoint.c. */
3097 struct breakpoint_objfile_data
3098 {
3099 /* Minimal symbol for "_ovly_debug_event" (if any). */
3100 struct bound_minimal_symbol overlay_msym {};
3101
3102 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3103 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
3104
3105 /* True if we have looked for longjmp probes. */
3106 int longjmp_searched = 0;
3107
3108 /* SystemTap probe points for longjmp (if any). These are non-owning
3109 references. */
3110 std::vector<probe *> longjmp_probes;
3111
3112 /* Minimal symbol for "std::terminate()" (if any). */
3113 struct bound_minimal_symbol terminate_msym {};
3114
3115 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3116 struct bound_minimal_symbol exception_msym {};
3117
3118 /* True if we have looked for exception probes. */
3119 int exception_searched = 0;
3120
3121 /* SystemTap probe points for unwinding (if any). These are non-owning
3122 references. */
3123 std::vector<probe *> exception_probes;
3124 };
3125
3126 static const struct objfile_key<breakpoint_objfile_data>
3127 breakpoint_objfile_key;
3128
3129 /* Minimal symbol not found sentinel. */
3130 static struct minimal_symbol msym_not_found;
3131
3132 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3133
3134 static int
3135 msym_not_found_p (const struct minimal_symbol *msym)
3136 {
3137 return msym == &msym_not_found;
3138 }
3139
3140 /* Return per-objfile data needed by breakpoint.c.
3141 Allocate the data if necessary. */
3142
3143 static struct breakpoint_objfile_data *
3144 get_breakpoint_objfile_data (struct objfile *objfile)
3145 {
3146 struct breakpoint_objfile_data *bp_objfile_data;
3147
3148 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3149 if (bp_objfile_data == NULL)
3150 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3151 return bp_objfile_data;
3152 }
3153
3154 static void
3155 create_overlay_event_breakpoint (void)
3156 {
3157 const char *const func_name = "_ovly_debug_event";
3158
3159 for (objfile *objfile : current_program_space->objfiles ())
3160 {
3161 struct breakpoint *b;
3162 struct breakpoint_objfile_data *bp_objfile_data;
3163 CORE_ADDR addr;
3164 struct explicit_location explicit_loc;
3165
3166 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3167
3168 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3169 continue;
3170
3171 if (bp_objfile_data->overlay_msym.minsym == NULL)
3172 {
3173 struct bound_minimal_symbol m;
3174
3175 m = lookup_minimal_symbol_text (func_name, objfile);
3176 if (m.minsym == NULL)
3177 {
3178 /* Avoid future lookups in this objfile. */
3179 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3180 continue;
3181 }
3182 bp_objfile_data->overlay_msym = m;
3183 }
3184
3185 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3186 b = create_internal_breakpoint (objfile->arch (), addr,
3187 bp_overlay_event,
3188 &internal_breakpoint_ops);
3189 initialize_explicit_location (&explicit_loc);
3190 explicit_loc.function_name = ASTRDUP (func_name);
3191 b->location = new_explicit_location (&explicit_loc);
3192
3193 if (overlay_debugging == ovly_auto)
3194 {
3195 b->enable_state = bp_enabled;
3196 overlay_events_enabled = 1;
3197 }
3198 else
3199 {
3200 b->enable_state = bp_disabled;
3201 overlay_events_enabled = 0;
3202 }
3203 }
3204 }
3205
3206 static void
3207 create_longjmp_master_breakpoint (void)
3208 {
3209 scoped_restore_current_program_space restore_pspace;
3210
3211 for (struct program_space *pspace : program_spaces)
3212 {
3213 set_current_program_space (pspace);
3214
3215 for (objfile *objfile : current_program_space->objfiles ())
3216 {
3217 int i;
3218 struct gdbarch *gdbarch;
3219 struct breakpoint_objfile_data *bp_objfile_data;
3220
3221 gdbarch = objfile->arch ();
3222
3223 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3224
3225 if (!bp_objfile_data->longjmp_searched)
3226 {
3227 std::vector<probe *> ret
3228 = find_probes_in_objfile (objfile, "libc", "longjmp");
3229
3230 if (!ret.empty ())
3231 {
3232 /* We are only interested in checking one element. */
3233 probe *p = ret[0];
3234
3235 if (!p->can_evaluate_arguments ())
3236 {
3237 /* We cannot use the probe interface here,
3238 because it does not know how to evaluate
3239 arguments. */
3240 ret.clear ();
3241 }
3242 }
3243 bp_objfile_data->longjmp_probes = ret;
3244 bp_objfile_data->longjmp_searched = 1;
3245 }
3246
3247 if (!bp_objfile_data->longjmp_probes.empty ())
3248 {
3249 for (probe *p : bp_objfile_data->longjmp_probes)
3250 {
3251 struct breakpoint *b;
3252
3253 b = create_internal_breakpoint (gdbarch,
3254 p->get_relocated_address (objfile),
3255 bp_longjmp_master,
3256 &internal_breakpoint_ops);
3257 b->location = new_probe_location ("-probe-stap libc:longjmp");
3258 b->enable_state = bp_disabled;
3259 }
3260
3261 continue;
3262 }
3263
3264 if (!gdbarch_get_longjmp_target_p (gdbarch))
3265 continue;
3266
3267 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3268 {
3269 struct breakpoint *b;
3270 const char *func_name;
3271 CORE_ADDR addr;
3272 struct explicit_location explicit_loc;
3273
3274 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3275 continue;
3276
3277 func_name = longjmp_names[i];
3278 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3279 {
3280 struct bound_minimal_symbol m;
3281
3282 m = lookup_minimal_symbol_text (func_name, objfile);
3283 if (m.minsym == NULL)
3284 {
3285 /* Prevent future lookups in this objfile. */
3286 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3287 continue;
3288 }
3289 bp_objfile_data->longjmp_msym[i] = m;
3290 }
3291
3292 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3293 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3294 &internal_breakpoint_ops);
3295 initialize_explicit_location (&explicit_loc);
3296 explicit_loc.function_name = ASTRDUP (func_name);
3297 b->location = new_explicit_location (&explicit_loc);
3298 b->enable_state = bp_disabled;
3299 }
3300 }
3301 }
3302 }
3303
3304 /* Create a master std::terminate breakpoint. */
3305 static void
3306 create_std_terminate_master_breakpoint (void)
3307 {
3308 const char *const func_name = "std::terminate()";
3309
3310 scoped_restore_current_program_space restore_pspace;
3311
3312 for (struct program_space *pspace : program_spaces)
3313 {
3314 CORE_ADDR addr;
3315
3316 set_current_program_space (pspace);
3317
3318 for (objfile *objfile : current_program_space->objfiles ())
3319 {
3320 struct breakpoint *b;
3321 struct breakpoint_objfile_data *bp_objfile_data;
3322 struct explicit_location explicit_loc;
3323
3324 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3325
3326 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3327 continue;
3328
3329 if (bp_objfile_data->terminate_msym.minsym == NULL)
3330 {
3331 struct bound_minimal_symbol m;
3332
3333 m = lookup_minimal_symbol (func_name, NULL, objfile);
3334 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3335 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3336 {
3337 /* Prevent future lookups in this objfile. */
3338 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3339 continue;
3340 }
3341 bp_objfile_data->terminate_msym = m;
3342 }
3343
3344 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3345 b = create_internal_breakpoint (objfile->arch (), addr,
3346 bp_std_terminate_master,
3347 &internal_breakpoint_ops);
3348 initialize_explicit_location (&explicit_loc);
3349 explicit_loc.function_name = ASTRDUP (func_name);
3350 b->location = new_explicit_location (&explicit_loc);
3351 b->enable_state = bp_disabled;
3352 }
3353 }
3354 }
3355
3356 /* Install a master breakpoint on the unwinder's debug hook. */
3357
3358 static void
3359 create_exception_master_breakpoint (void)
3360 {
3361 const char *const func_name = "_Unwind_DebugHook";
3362
3363 for (objfile *objfile : current_program_space->objfiles ())
3364 {
3365 struct breakpoint *b;
3366 struct gdbarch *gdbarch;
3367 struct breakpoint_objfile_data *bp_objfile_data;
3368 CORE_ADDR addr;
3369 struct explicit_location explicit_loc;
3370
3371 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3372
3373 /* We prefer the SystemTap probe point if it exists. */
3374 if (!bp_objfile_data->exception_searched)
3375 {
3376 std::vector<probe *> ret
3377 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3378
3379 if (!ret.empty ())
3380 {
3381 /* We are only interested in checking one element. */
3382 probe *p = ret[0];
3383
3384 if (!p->can_evaluate_arguments ())
3385 {
3386 /* We cannot use the probe interface here, because it does
3387 not know how to evaluate arguments. */
3388 ret.clear ();
3389 }
3390 }
3391 bp_objfile_data->exception_probes = ret;
3392 bp_objfile_data->exception_searched = 1;
3393 }
3394
3395 if (!bp_objfile_data->exception_probes.empty ())
3396 {
3397 gdbarch = objfile->arch ();
3398
3399 for (probe *p : bp_objfile_data->exception_probes)
3400 {
3401 b = create_internal_breakpoint (gdbarch,
3402 p->get_relocated_address (objfile),
3403 bp_exception_master,
3404 &internal_breakpoint_ops);
3405 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3406 b->enable_state = bp_disabled;
3407 }
3408
3409 continue;
3410 }
3411
3412 /* Otherwise, try the hook function. */
3413
3414 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3415 continue;
3416
3417 gdbarch = objfile->arch ();
3418
3419 if (bp_objfile_data->exception_msym.minsym == NULL)
3420 {
3421 struct bound_minimal_symbol debug_hook;
3422
3423 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3424 if (debug_hook.minsym == NULL)
3425 {
3426 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3427 continue;
3428 }
3429
3430 bp_objfile_data->exception_msym = debug_hook;
3431 }
3432
3433 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3434 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3435 current_top_target ());
3436 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3437 &internal_breakpoint_ops);
3438 initialize_explicit_location (&explicit_loc);
3439 explicit_loc.function_name = ASTRDUP (func_name);
3440 b->location = new_explicit_location (&explicit_loc);
3441 b->enable_state = bp_disabled;
3442 }
3443 }
3444
3445 /* Does B have a location spec? */
3446
3447 static int
3448 breakpoint_event_location_empty_p (const struct breakpoint *b)
3449 {
3450 return b->location != NULL && event_location_empty_p (b->location.get ());
3451 }
3452
3453 void
3454 update_breakpoints_after_exec (void)
3455 {
3456 struct breakpoint *b, *b_tmp;
3457 struct bp_location *bploc, **bplocp_tmp;
3458
3459 /* We're about to delete breakpoints from GDB's lists. If the
3460 INSERTED flag is true, GDB will try to lift the breakpoints by
3461 writing the breakpoints' "shadow contents" back into memory. The
3462 "shadow contents" are NOT valid after an exec, so GDB should not
3463 do that. Instead, the target is responsible from marking
3464 breakpoints out as soon as it detects an exec. We don't do that
3465 here instead, because there may be other attempts to delete
3466 breakpoints after detecting an exec and before reaching here. */
3467 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3468 if (bploc->pspace == current_program_space)
3469 gdb_assert (!bploc->inserted);
3470
3471 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3472 {
3473 if (b->pspace != current_program_space)
3474 continue;
3475
3476 /* Solib breakpoints must be explicitly reset after an exec(). */
3477 if (b->type == bp_shlib_event)
3478 {
3479 delete_breakpoint (b);
3480 continue;
3481 }
3482
3483 /* JIT breakpoints must be explicitly reset after an exec(). */
3484 if (b->type == bp_jit_event)
3485 {
3486 delete_breakpoint (b);
3487 continue;
3488 }
3489
3490 /* Thread event breakpoints must be set anew after an exec(),
3491 as must overlay event and longjmp master breakpoints. */
3492 if (b->type == bp_thread_event || b->type == bp_overlay_event
3493 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3494 || b->type == bp_exception_master)
3495 {
3496 delete_breakpoint (b);
3497 continue;
3498 }
3499
3500 /* Step-resume breakpoints are meaningless after an exec(). */
3501 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3502 {
3503 delete_breakpoint (b);
3504 continue;
3505 }
3506
3507 /* Just like single-step breakpoints. */
3508 if (b->type == bp_single_step)
3509 {
3510 delete_breakpoint (b);
3511 continue;
3512 }
3513
3514 /* Longjmp and longjmp-resume breakpoints are also meaningless
3515 after an exec. */
3516 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3517 || b->type == bp_longjmp_call_dummy
3518 || b->type == bp_exception || b->type == bp_exception_resume)
3519 {
3520 delete_breakpoint (b);
3521 continue;
3522 }
3523
3524 if (b->type == bp_catchpoint)
3525 {
3526 /* For now, none of the bp_catchpoint breakpoints need to
3527 do anything at this point. In the future, if some of
3528 the catchpoints need to something, we will need to add
3529 a new method, and call this method from here. */
3530 continue;
3531 }
3532
3533 /* bp_finish is a special case. The only way we ought to be able
3534 to see one of these when an exec() has happened, is if the user
3535 caught a vfork, and then said "finish". Ordinarily a finish just
3536 carries them to the call-site of the current callee, by setting
3537 a temporary bp there and resuming. But in this case, the finish
3538 will carry them entirely through the vfork & exec.
3539
3540 We don't want to allow a bp_finish to remain inserted now. But
3541 we can't safely delete it, 'cause finish_command has a handle to
3542 the bp on a bpstat, and will later want to delete it. There's a
3543 chance (and I've seen it happen) that if we delete the bp_finish
3544 here, that its storage will get reused by the time finish_command
3545 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3546 We really must allow finish_command to delete a bp_finish.
3547
3548 In the absence of a general solution for the "how do we know
3549 it's safe to delete something others may have handles to?"
3550 problem, what we'll do here is just uninsert the bp_finish, and
3551 let finish_command delete it.
3552
3553 (We know the bp_finish is "doomed" in the sense that it's
3554 momentary, and will be deleted as soon as finish_command sees
3555 the inferior stopped. So it doesn't matter that the bp's
3556 address is probably bogus in the new a.out, unlike e.g., the
3557 solib breakpoints.) */
3558
3559 if (b->type == bp_finish)
3560 {
3561 continue;
3562 }
3563
3564 /* Without a symbolic address, we have little hope of the
3565 pre-exec() address meaning the same thing in the post-exec()
3566 a.out. */
3567 if (breakpoint_event_location_empty_p (b))
3568 {
3569 delete_breakpoint (b);
3570 continue;
3571 }
3572 }
3573 }
3574
3575 int
3576 detach_breakpoints (ptid_t ptid)
3577 {
3578 struct bp_location *bl, **blp_tmp;
3579 int val = 0;
3580 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3581 struct inferior *inf = current_inferior ();
3582
3583 if (ptid.pid () == inferior_ptid.pid ())
3584 error (_("Cannot detach breakpoints of inferior_ptid"));
3585
3586 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3587 inferior_ptid = ptid;
3588 ALL_BP_LOCATIONS (bl, blp_tmp)
3589 {
3590 if (bl->pspace != inf->pspace)
3591 continue;
3592
3593 /* This function must physically remove breakpoints locations
3594 from the specified ptid, without modifying the breakpoint
3595 package's state. Locations of type bp_loc_other are only
3596 maintained at GDB side. So, there is no need to remove
3597 these bp_loc_other locations. Moreover, removing these
3598 would modify the breakpoint package's state. */
3599 if (bl->loc_type == bp_loc_other)
3600 continue;
3601
3602 if (bl->inserted)
3603 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3604 }
3605
3606 return val;
3607 }
3608
3609 /* Remove the breakpoint location BL from the current address space.
3610 Note that this is used to detach breakpoints from a child fork.
3611 When we get here, the child isn't in the inferior list, and neither
3612 do we have objects to represent its address space --- we should
3613 *not* look at bl->pspace->aspace here. */
3614
3615 static int
3616 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3617 {
3618 int val;
3619
3620 /* BL is never in moribund_locations by our callers. */
3621 gdb_assert (bl->owner != NULL);
3622
3623 /* The type of none suggests that owner is actually deleted.
3624 This should not ever happen. */
3625 gdb_assert (bl->owner->type != bp_none);
3626
3627 if (bl->loc_type == bp_loc_software_breakpoint
3628 || bl->loc_type == bp_loc_hardware_breakpoint)
3629 {
3630 /* "Normal" instruction breakpoint: either the standard
3631 trap-instruction bp (bp_breakpoint), or a
3632 bp_hardware_breakpoint. */
3633
3634 /* First check to see if we have to handle an overlay. */
3635 if (overlay_debugging == ovly_off
3636 || bl->section == NULL
3637 || !(section_is_overlay (bl->section)))
3638 {
3639 /* No overlay handling: just remove the breakpoint. */
3640
3641 /* If we're trying to uninsert a memory breakpoint that we
3642 know is set in a dynamic object that is marked
3643 shlib_disabled, then either the dynamic object was
3644 removed with "remove-symbol-file" or with
3645 "nosharedlibrary". In the former case, we don't know
3646 whether another dynamic object might have loaded over the
3647 breakpoint's address -- the user might well let us know
3648 about it next with add-symbol-file (the whole point of
3649 add-symbol-file is letting the user manually maintain a
3650 list of dynamically loaded objects). If we have the
3651 breakpoint's shadow memory, that is, this is a software
3652 breakpoint managed by GDB, check whether the breakpoint
3653 is still inserted in memory, to avoid overwriting wrong
3654 code with stale saved shadow contents. Note that HW
3655 breakpoints don't have shadow memory, as they're
3656 implemented using a mechanism that is not dependent on
3657 being able to modify the target's memory, and as such
3658 they should always be removed. */
3659 if (bl->shlib_disabled
3660 && bl->target_info.shadow_len != 0
3661 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3662 val = 0;
3663 else
3664 val = bl->owner->ops->remove_location (bl, reason);
3665 }
3666 else
3667 {
3668 /* This breakpoint is in an overlay section.
3669 Did we set a breakpoint at the LMA? */
3670 if (!overlay_events_enabled)
3671 {
3672 /* Yes -- overlay event support is not active, so we
3673 should have set a breakpoint at the LMA. Remove it.
3674 */
3675 /* Ignore any failures: if the LMA is in ROM, we will
3676 have already warned when we failed to insert it. */
3677 if (bl->loc_type == bp_loc_hardware_breakpoint)
3678 target_remove_hw_breakpoint (bl->gdbarch,
3679 &bl->overlay_target_info);
3680 else
3681 target_remove_breakpoint (bl->gdbarch,
3682 &bl->overlay_target_info,
3683 reason);
3684 }
3685 /* Did we set a breakpoint at the VMA?
3686 If so, we will have marked the breakpoint 'inserted'. */
3687 if (bl->inserted)
3688 {
3689 /* Yes -- remove it. Previously we did not bother to
3690 remove the breakpoint if the section had been
3691 unmapped, but let's not rely on that being safe. We
3692 don't know what the overlay manager might do. */
3693
3694 /* However, we should remove *software* breakpoints only
3695 if the section is still mapped, or else we overwrite
3696 wrong code with the saved shadow contents. */
3697 if (bl->loc_type == bp_loc_hardware_breakpoint
3698 || section_is_mapped (bl->section))
3699 val = bl->owner->ops->remove_location (bl, reason);
3700 else
3701 val = 0;
3702 }
3703 else
3704 {
3705 /* No -- not inserted, so no need to remove. No error. */
3706 val = 0;
3707 }
3708 }
3709
3710 /* In some cases, we might not be able to remove a breakpoint in
3711 a shared library that has already been removed, but we have
3712 not yet processed the shlib unload event. Similarly for an
3713 unloaded add-symbol-file object - the user might not yet have
3714 had the chance to remove-symbol-file it. shlib_disabled will
3715 be set if the library/object has already been removed, but
3716 the breakpoint hasn't been uninserted yet, e.g., after
3717 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3718 always-inserted mode. */
3719 if (val
3720 && (bl->loc_type == bp_loc_software_breakpoint
3721 && (bl->shlib_disabled
3722 || solib_name_from_address (bl->pspace, bl->address)
3723 || shared_objfile_contains_address_p (bl->pspace,
3724 bl->address))))
3725 val = 0;
3726
3727 if (val)
3728 return val;
3729 bl->inserted = (reason == DETACH_BREAKPOINT);
3730 }
3731 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3732 {
3733 gdb_assert (bl->owner->ops != NULL
3734 && bl->owner->ops->remove_location != NULL);
3735
3736 bl->inserted = (reason == DETACH_BREAKPOINT);
3737 bl->owner->ops->remove_location (bl, reason);
3738
3739 /* Failure to remove any of the hardware watchpoints comes here. */
3740 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3741 warning (_("Could not remove hardware watchpoint %d."),
3742 bl->owner->number);
3743 }
3744 else if (bl->owner->type == bp_catchpoint
3745 && breakpoint_enabled (bl->owner)
3746 && !bl->duplicate)
3747 {
3748 gdb_assert (bl->owner->ops != NULL
3749 && bl->owner->ops->remove_location != NULL);
3750
3751 val = bl->owner->ops->remove_location (bl, reason);
3752 if (val)
3753 return val;
3754
3755 bl->inserted = (reason == DETACH_BREAKPOINT);
3756 }
3757
3758 return 0;
3759 }
3760
3761 static int
3762 remove_breakpoint (struct bp_location *bl)
3763 {
3764 /* BL is never in moribund_locations by our callers. */
3765 gdb_assert (bl->owner != NULL);
3766
3767 /* The type of none suggests that owner is actually deleted.
3768 This should not ever happen. */
3769 gdb_assert (bl->owner->type != bp_none);
3770
3771 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3772
3773 switch_to_program_space_and_thread (bl->pspace);
3774
3775 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
3776 }
3777
3778 /* Clear the "inserted" flag in all breakpoints. */
3779
3780 void
3781 mark_breakpoints_out (void)
3782 {
3783 struct bp_location *bl, **blp_tmp;
3784
3785 ALL_BP_LOCATIONS (bl, blp_tmp)
3786 if (bl->pspace == current_program_space)
3787 bl->inserted = 0;
3788 }
3789
3790 /* Clear the "inserted" flag in all breakpoints and delete any
3791 breakpoints which should go away between runs of the program.
3792
3793 Plus other such housekeeping that has to be done for breakpoints
3794 between runs.
3795
3796 Note: this function gets called at the end of a run (by
3797 generic_mourn_inferior) and when a run begins (by
3798 init_wait_for_inferior). */
3799
3800
3801
3802 void
3803 breakpoint_init_inferior (enum inf_context context)
3804 {
3805 struct breakpoint *b, *b_tmp;
3806 struct program_space *pspace = current_program_space;
3807
3808 /* If breakpoint locations are shared across processes, then there's
3809 nothing to do. */
3810 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
3811 return;
3812
3813 mark_breakpoints_out ();
3814
3815 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3816 {
3817 if (b->loc && b->loc->pspace != pspace)
3818 continue;
3819
3820 switch (b->type)
3821 {
3822 case bp_call_dummy:
3823 case bp_longjmp_call_dummy:
3824
3825 /* If the call dummy breakpoint is at the entry point it will
3826 cause problems when the inferior is rerun, so we better get
3827 rid of it. */
3828
3829 case bp_watchpoint_scope:
3830
3831 /* Also get rid of scope breakpoints. */
3832
3833 case bp_shlib_event:
3834
3835 /* Also remove solib event breakpoints. Their addresses may
3836 have changed since the last time we ran the program.
3837 Actually we may now be debugging against different target;
3838 and so the solib backend that installed this breakpoint may
3839 not be used in by the target. E.g.,
3840
3841 (gdb) file prog-linux
3842 (gdb) run # native linux target
3843 ...
3844 (gdb) kill
3845 (gdb) file prog-win.exe
3846 (gdb) tar rem :9999 # remote Windows gdbserver.
3847 */
3848
3849 case bp_step_resume:
3850
3851 /* Also remove step-resume breakpoints. */
3852
3853 case bp_single_step:
3854
3855 /* Also remove single-step breakpoints. */
3856
3857 delete_breakpoint (b);
3858 break;
3859
3860 case bp_watchpoint:
3861 case bp_hardware_watchpoint:
3862 case bp_read_watchpoint:
3863 case bp_access_watchpoint:
3864 {
3865 struct watchpoint *w = (struct watchpoint *) b;
3866
3867 /* Likewise for watchpoints on local expressions. */
3868 if (w->exp_valid_block != NULL)
3869 delete_breakpoint (b);
3870 else
3871 {
3872 /* Get rid of existing locations, which are no longer
3873 valid. New ones will be created in
3874 update_watchpoint, when the inferior is restarted.
3875 The next update_global_location_list call will
3876 garbage collect them. */
3877 b->loc = NULL;
3878
3879 if (context == inf_starting)
3880 {
3881 /* Reset val field to force reread of starting value in
3882 insert_breakpoints. */
3883 w->val.reset (nullptr);
3884 w->val_valid = false;
3885 }
3886 }
3887 }
3888 break;
3889 default:
3890 break;
3891 }
3892 }
3893
3894 /* Get rid of the moribund locations. */
3895 for (bp_location *bl : moribund_locations)
3896 decref_bp_location (&bl);
3897 moribund_locations.clear ();
3898 }
3899
3900 /* These functions concern about actual breakpoints inserted in the
3901 target --- to e.g. check if we need to do decr_pc adjustment or if
3902 we need to hop over the bkpt --- so we check for address space
3903 match, not program space. */
3904
3905 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3906 exists at PC. It returns ordinary_breakpoint_here if it's an
3907 ordinary breakpoint, or permanent_breakpoint_here if it's a
3908 permanent breakpoint.
3909 - When continuing from a location with an ordinary breakpoint, we
3910 actually single step once before calling insert_breakpoints.
3911 - When continuing from a location with a permanent breakpoint, we
3912 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3913 the target, to advance the PC past the breakpoint. */
3914
3915 enum breakpoint_here
3916 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
3917 {
3918 struct bp_location *bl, **blp_tmp;
3919 int any_breakpoint_here = 0;
3920
3921 ALL_BP_LOCATIONS (bl, blp_tmp)
3922 {
3923 if (bl->loc_type != bp_loc_software_breakpoint
3924 && bl->loc_type != bp_loc_hardware_breakpoint)
3925 continue;
3926
3927 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
3928 if ((breakpoint_enabled (bl->owner)
3929 || bl->permanent)
3930 && breakpoint_location_address_match (bl, aspace, pc))
3931 {
3932 if (overlay_debugging
3933 && section_is_overlay (bl->section)
3934 && !section_is_mapped (bl->section))
3935 continue; /* unmapped overlay -- can't be a match */
3936 else if (bl->permanent)
3937 return permanent_breakpoint_here;
3938 else
3939 any_breakpoint_here = 1;
3940 }
3941 }
3942
3943 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
3944 }
3945
3946 /* See breakpoint.h. */
3947
3948 int
3949 breakpoint_in_range_p (const address_space *aspace,
3950 CORE_ADDR addr, ULONGEST len)
3951 {
3952 struct bp_location *bl, **blp_tmp;
3953
3954 ALL_BP_LOCATIONS (bl, blp_tmp)
3955 {
3956 if (bl->loc_type != bp_loc_software_breakpoint
3957 && bl->loc_type != bp_loc_hardware_breakpoint)
3958 continue;
3959
3960 if ((breakpoint_enabled (bl->owner)
3961 || bl->permanent)
3962 && breakpoint_location_address_range_overlap (bl, aspace,
3963 addr, len))
3964 {
3965 if (overlay_debugging
3966 && section_is_overlay (bl->section)
3967 && !section_is_mapped (bl->section))
3968 {
3969 /* Unmapped overlay -- can't be a match. */
3970 continue;
3971 }
3972
3973 return 1;
3974 }
3975 }
3976
3977 return 0;
3978 }
3979
3980 /* Return true if there's a moribund breakpoint at PC. */
3981
3982 int
3983 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
3984 {
3985 for (bp_location *loc : moribund_locations)
3986 if (breakpoint_location_address_match (loc, aspace, pc))
3987 return 1;
3988
3989 return 0;
3990 }
3991
3992 /* Returns non-zero iff BL is inserted at PC, in address space
3993 ASPACE. */
3994
3995 static int
3996 bp_location_inserted_here_p (struct bp_location *bl,
3997 const address_space *aspace, CORE_ADDR pc)
3998 {
3999 if (bl->inserted
4000 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4001 aspace, pc))
4002 {
4003 if (overlay_debugging
4004 && section_is_overlay (bl->section)
4005 && !section_is_mapped (bl->section))
4006 return 0; /* unmapped overlay -- can't be a match */
4007 else
4008 return 1;
4009 }
4010 return 0;
4011 }
4012
4013 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4014
4015 int
4016 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4017 {
4018 struct bp_location **blp, **blp_tmp = NULL;
4019
4020 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4021 {
4022 struct bp_location *bl = *blp;
4023
4024 if (bl->loc_type != bp_loc_software_breakpoint
4025 && bl->loc_type != bp_loc_hardware_breakpoint)
4026 continue;
4027
4028 if (bp_location_inserted_here_p (bl, aspace, pc))
4029 return 1;
4030 }
4031 return 0;
4032 }
4033
4034 /* This function returns non-zero iff there is a software breakpoint
4035 inserted at PC. */
4036
4037 int
4038 software_breakpoint_inserted_here_p (const address_space *aspace,
4039 CORE_ADDR pc)
4040 {
4041 struct bp_location **blp, **blp_tmp = NULL;
4042
4043 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4044 {
4045 struct bp_location *bl = *blp;
4046
4047 if (bl->loc_type != bp_loc_software_breakpoint)
4048 continue;
4049
4050 if (bp_location_inserted_here_p (bl, aspace, pc))
4051 return 1;
4052 }
4053
4054 return 0;
4055 }
4056
4057 /* See breakpoint.h. */
4058
4059 int
4060 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4061 CORE_ADDR pc)
4062 {
4063 struct bp_location **blp, **blp_tmp = NULL;
4064
4065 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4066 {
4067 struct bp_location *bl = *blp;
4068
4069 if (bl->loc_type != bp_loc_hardware_breakpoint)
4070 continue;
4071
4072 if (bp_location_inserted_here_p (bl, aspace, pc))
4073 return 1;
4074 }
4075
4076 return 0;
4077 }
4078
4079 int
4080 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4081 CORE_ADDR addr, ULONGEST len)
4082 {
4083 struct breakpoint *bpt;
4084
4085 ALL_BREAKPOINTS (bpt)
4086 {
4087 struct bp_location *loc;
4088
4089 if (bpt->type != bp_hardware_watchpoint
4090 && bpt->type != bp_access_watchpoint)
4091 continue;
4092
4093 if (!breakpoint_enabled (bpt))
4094 continue;
4095
4096 for (loc = bpt->loc; loc; loc = loc->next)
4097 if (loc->pspace->aspace == aspace && loc->inserted)
4098 {
4099 CORE_ADDR l, h;
4100
4101 /* Check for intersection. */
4102 l = std::max<CORE_ADDR> (loc->address, addr);
4103 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4104 if (l < h)
4105 return 1;
4106 }
4107 }
4108 return 0;
4109 }
4110
4111 /* See breakpoint.h. */
4112
4113 bool
4114 is_catchpoint (struct breakpoint *b)
4115 {
4116 return (b->type == bp_catchpoint);
4117 }
4118
4119 /* Frees any storage that is part of a bpstat. Does not walk the
4120 'next' chain. */
4121
4122 bpstats::~bpstats ()
4123 {
4124 if (bp_location_at != NULL)
4125 decref_bp_location (&bp_location_at);
4126 }
4127
4128 /* Clear a bpstat so that it says we are not at any breakpoint.
4129 Also free any storage that is part of a bpstat. */
4130
4131 void
4132 bpstat_clear (bpstat *bsp)
4133 {
4134 bpstat p;
4135 bpstat q;
4136
4137 if (bsp == 0)
4138 return;
4139 p = *bsp;
4140 while (p != NULL)
4141 {
4142 q = p->next;
4143 delete p;
4144 p = q;
4145 }
4146 *bsp = NULL;
4147 }
4148
4149 bpstats::bpstats (const bpstats &other)
4150 : next (NULL),
4151 bp_location_at (other.bp_location_at),
4152 breakpoint_at (other.breakpoint_at),
4153 commands (other.commands),
4154 print (other.print),
4155 stop (other.stop),
4156 print_it (other.print_it)
4157 {
4158 if (other.old_val != NULL)
4159 old_val = release_value (value_copy (other.old_val.get ()));
4160 incref_bp_location (bp_location_at);
4161 }
4162
4163 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4164 is part of the bpstat is copied as well. */
4165
4166 bpstat
4167 bpstat_copy (bpstat bs)
4168 {
4169 bpstat p = NULL;
4170 bpstat tmp;
4171 bpstat retval = NULL;
4172
4173 if (bs == NULL)
4174 return bs;
4175
4176 for (; bs != NULL; bs = bs->next)
4177 {
4178 tmp = new bpstats (*bs);
4179
4180 if (p == NULL)
4181 /* This is the first thing in the chain. */
4182 retval = tmp;
4183 else
4184 p->next = tmp;
4185 p = tmp;
4186 }
4187 p->next = NULL;
4188 return retval;
4189 }
4190
4191 /* Find the bpstat associated with this breakpoint. */
4192
4193 bpstat
4194 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4195 {
4196 if (bsp == NULL)
4197 return NULL;
4198
4199 for (; bsp != NULL; bsp = bsp->next)
4200 {
4201 if (bsp->breakpoint_at == breakpoint)
4202 return bsp;
4203 }
4204 return NULL;
4205 }
4206
4207 /* See breakpoint.h. */
4208
4209 bool
4210 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4211 {
4212 for (; bsp != NULL; bsp = bsp->next)
4213 {
4214 if (bsp->breakpoint_at == NULL)
4215 {
4216 /* A moribund location can never explain a signal other than
4217 GDB_SIGNAL_TRAP. */
4218 if (sig == GDB_SIGNAL_TRAP)
4219 return true;
4220 }
4221 else
4222 {
4223 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4224 sig))
4225 return true;
4226 }
4227 }
4228
4229 return false;
4230 }
4231
4232 /* Put in *NUM the breakpoint number of the first breakpoint we are
4233 stopped at. *BSP upon return is a bpstat which points to the
4234 remaining breakpoints stopped at (but which is not guaranteed to be
4235 good for anything but further calls to bpstat_num).
4236
4237 Return 0 if passed a bpstat which does not indicate any breakpoints.
4238 Return -1 if stopped at a breakpoint that has been deleted since
4239 we set it.
4240 Return 1 otherwise. */
4241
4242 int
4243 bpstat_num (bpstat *bsp, int *num)
4244 {
4245 struct breakpoint *b;
4246
4247 if ((*bsp) == NULL)
4248 return 0; /* No more breakpoint values */
4249
4250 /* We assume we'll never have several bpstats that correspond to a
4251 single breakpoint -- otherwise, this function might return the
4252 same number more than once and this will look ugly. */
4253 b = (*bsp)->breakpoint_at;
4254 *bsp = (*bsp)->next;
4255 if (b == NULL)
4256 return -1; /* breakpoint that's been deleted since */
4257
4258 *num = b->number; /* We have its number */
4259 return 1;
4260 }
4261
4262 /* See breakpoint.h. */
4263
4264 void
4265 bpstat_clear_actions (void)
4266 {
4267 bpstat bs;
4268
4269 if (inferior_ptid == null_ptid)
4270 return;
4271
4272 thread_info *tp = inferior_thread ();
4273 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4274 {
4275 bs->commands = NULL;
4276 bs->old_val.reset (nullptr);
4277 }
4278 }
4279
4280 /* Called when a command is about to proceed the inferior. */
4281
4282 static void
4283 breakpoint_about_to_proceed (void)
4284 {
4285 if (inferior_ptid != null_ptid)
4286 {
4287 struct thread_info *tp = inferior_thread ();
4288
4289 /* Allow inferior function calls in breakpoint commands to not
4290 interrupt the command list. When the call finishes
4291 successfully, the inferior will be standing at the same
4292 breakpoint as if nothing happened. */
4293 if (tp->control.in_infcall)
4294 return;
4295 }
4296
4297 breakpoint_proceeded = 1;
4298 }
4299
4300 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4301 or its equivalent. */
4302
4303 static int
4304 command_line_is_silent (struct command_line *cmd)
4305 {
4306 return cmd && (strcmp ("silent", cmd->line) == 0);
4307 }
4308
4309 /* Execute all the commands associated with all the breakpoints at
4310 this location. Any of these commands could cause the process to
4311 proceed beyond this point, etc. We look out for such changes by
4312 checking the global "breakpoint_proceeded" after each command.
4313
4314 Returns true if a breakpoint command resumed the inferior. In that
4315 case, it is the caller's responsibility to recall it again with the
4316 bpstat of the current thread. */
4317
4318 static int
4319 bpstat_do_actions_1 (bpstat *bsp)
4320 {
4321 bpstat bs;
4322 int again = 0;
4323
4324 /* Avoid endless recursion if a `source' command is contained
4325 in bs->commands. */
4326 if (executing_breakpoint_commands)
4327 return 0;
4328
4329 scoped_restore save_executing
4330 = make_scoped_restore (&executing_breakpoint_commands, 1);
4331
4332 scoped_restore preventer = prevent_dont_repeat ();
4333
4334 /* This pointer will iterate over the list of bpstat's. */
4335 bs = *bsp;
4336
4337 breakpoint_proceeded = 0;
4338 for (; bs != NULL; bs = bs->next)
4339 {
4340 struct command_line *cmd = NULL;
4341
4342 /* Take ownership of the BSP's command tree, if it has one.
4343
4344 The command tree could legitimately contain commands like
4345 'step' and 'next', which call clear_proceed_status, which
4346 frees stop_bpstat's command tree. To make sure this doesn't
4347 free the tree we're executing out from under us, we need to
4348 take ownership of the tree ourselves. Since a given bpstat's
4349 commands are only executed once, we don't need to copy it; we
4350 can clear the pointer in the bpstat, and make sure we free
4351 the tree when we're done. */
4352 counted_command_line ccmd = bs->commands;
4353 bs->commands = NULL;
4354 if (ccmd != NULL)
4355 cmd = ccmd.get ();
4356 if (command_line_is_silent (cmd))
4357 {
4358 /* The action has been already done by bpstat_stop_status. */
4359 cmd = cmd->next;
4360 }
4361
4362 while (cmd != NULL)
4363 {
4364 execute_control_command (cmd);
4365
4366 if (breakpoint_proceeded)
4367 break;
4368 else
4369 cmd = cmd->next;
4370 }
4371
4372 if (breakpoint_proceeded)
4373 {
4374 if (current_ui->async)
4375 /* If we are in async mode, then the target might be still
4376 running, not stopped at any breakpoint, so nothing for
4377 us to do here -- just return to the event loop. */
4378 ;
4379 else
4380 /* In sync mode, when execute_control_command returns
4381 we're already standing on the next breakpoint.
4382 Breakpoint commands for that stop were not run, since
4383 execute_command does not run breakpoint commands --
4384 only command_line_handler does, but that one is not
4385 involved in execution of breakpoint commands. So, we
4386 can now execute breakpoint commands. It should be
4387 noted that making execute_command do bpstat actions is
4388 not an option -- in this case we'll have recursive
4389 invocation of bpstat for each breakpoint with a
4390 command, and can easily blow up GDB stack. Instead, we
4391 return true, which will trigger the caller to recall us
4392 with the new stop_bpstat. */
4393 again = 1;
4394 break;
4395 }
4396 }
4397 return again;
4398 }
4399
4400 /* Helper for bpstat_do_actions. Get the current thread, if there's
4401 one, is alive and has execution. Return NULL otherwise. */
4402
4403 static thread_info *
4404 get_bpstat_thread ()
4405 {
4406 if (inferior_ptid == null_ptid || !target_has_execution)
4407 return NULL;
4408
4409 thread_info *tp = inferior_thread ();
4410 if (tp->state == THREAD_EXITED || tp->executing)
4411 return NULL;
4412 return tp;
4413 }
4414
4415 void
4416 bpstat_do_actions (void)
4417 {
4418 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4419 thread_info *tp;
4420
4421 /* Do any commands attached to breakpoint we are stopped at. */
4422 while ((tp = get_bpstat_thread ()) != NULL)
4423 {
4424 /* Since in sync mode, bpstat_do_actions may resume the
4425 inferior, and only return when it is stopped at the next
4426 breakpoint, we keep doing breakpoint actions until it returns
4427 false to indicate the inferior was not resumed. */
4428 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4429 break;
4430 }
4431
4432 cleanup_if_error.release ();
4433 }
4434
4435 /* Print out the (old or new) value associated with a watchpoint. */
4436
4437 static void
4438 watchpoint_value_print (struct value *val, struct ui_file *stream)
4439 {
4440 if (val == NULL)
4441 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4442 else
4443 {
4444 struct value_print_options opts;
4445 get_user_print_options (&opts);
4446 value_print (val, stream, &opts);
4447 }
4448 }
4449
4450 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4451 debugging multiple threads. */
4452
4453 void
4454 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4455 {
4456 if (uiout->is_mi_like_p ())
4457 return;
4458
4459 uiout->text ("\n");
4460
4461 if (show_thread_that_caused_stop ())
4462 {
4463 const char *name;
4464 struct thread_info *thr = inferior_thread ();
4465
4466 uiout->text ("Thread ");
4467 uiout->field_string ("thread-id", print_thread_id (thr));
4468
4469 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4470 if (name != NULL)
4471 {
4472 uiout->text (" \"");
4473 uiout->field_string ("name", name);
4474 uiout->text ("\"");
4475 }
4476
4477 uiout->text (" hit ");
4478 }
4479 }
4480
4481 /* Generic routine for printing messages indicating why we
4482 stopped. The behavior of this function depends on the value
4483 'print_it' in the bpstat structure. Under some circumstances we
4484 may decide not to print anything here and delegate the task to
4485 normal_stop(). */
4486
4487 static enum print_stop_action
4488 print_bp_stop_message (bpstat bs)
4489 {
4490 switch (bs->print_it)
4491 {
4492 case print_it_noop:
4493 /* Nothing should be printed for this bpstat entry. */
4494 return PRINT_UNKNOWN;
4495 break;
4496
4497 case print_it_done:
4498 /* We still want to print the frame, but we already printed the
4499 relevant messages. */
4500 return PRINT_SRC_AND_LOC;
4501 break;
4502
4503 case print_it_normal:
4504 {
4505 struct breakpoint *b = bs->breakpoint_at;
4506
4507 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4508 which has since been deleted. */
4509 if (b == NULL)
4510 return PRINT_UNKNOWN;
4511
4512 /* Normal case. Call the breakpoint's print_it method. */
4513 return b->ops->print_it (bs);
4514 }
4515 break;
4516
4517 default:
4518 internal_error (__FILE__, __LINE__,
4519 _("print_bp_stop_message: unrecognized enum value"));
4520 break;
4521 }
4522 }
4523
4524 /* A helper function that prints a shared library stopped event. */
4525
4526 static void
4527 print_solib_event (int is_catchpoint)
4528 {
4529 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4530 bool any_added = !current_program_space->added_solibs.empty ();
4531
4532 if (!is_catchpoint)
4533 {
4534 if (any_added || any_deleted)
4535 current_uiout->text (_("Stopped due to shared library event:\n"));
4536 else
4537 current_uiout->text (_("Stopped due to shared library event (no "
4538 "libraries added or removed)\n"));
4539 }
4540
4541 if (current_uiout->is_mi_like_p ())
4542 current_uiout->field_string ("reason",
4543 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4544
4545 if (any_deleted)
4546 {
4547 current_uiout->text (_(" Inferior unloaded "));
4548 ui_out_emit_list list_emitter (current_uiout, "removed");
4549 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4550 {
4551 const std::string &name = current_program_space->deleted_solibs[ix];
4552
4553 if (ix > 0)
4554 current_uiout->text (" ");
4555 current_uiout->field_string ("library", name);
4556 current_uiout->text ("\n");
4557 }
4558 }
4559
4560 if (any_added)
4561 {
4562 current_uiout->text (_(" Inferior loaded "));
4563 ui_out_emit_list list_emitter (current_uiout, "added");
4564 bool first = true;
4565 for (so_list *iter : current_program_space->added_solibs)
4566 {
4567 if (!first)
4568 current_uiout->text (" ");
4569 first = false;
4570 current_uiout->field_string ("library", iter->so_name);
4571 current_uiout->text ("\n");
4572 }
4573 }
4574 }
4575
4576 /* Print a message indicating what happened. This is called from
4577 normal_stop(). The input to this routine is the head of the bpstat
4578 list - a list of the eventpoints that caused this stop. KIND is
4579 the target_waitkind for the stopping event. This
4580 routine calls the generic print routine for printing a message
4581 about reasons for stopping. This will print (for example) the
4582 "Breakpoint n," part of the output. The return value of this
4583 routine is one of:
4584
4585 PRINT_UNKNOWN: Means we printed nothing.
4586 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4587 code to print the location. An example is
4588 "Breakpoint 1, " which should be followed by
4589 the location.
4590 PRINT_SRC_ONLY: Means we printed something, but there is no need
4591 to also print the location part of the message.
4592 An example is the catch/throw messages, which
4593 don't require a location appended to the end.
4594 PRINT_NOTHING: We have done some printing and we don't need any
4595 further info to be printed. */
4596
4597 enum print_stop_action
4598 bpstat_print (bpstat bs, int kind)
4599 {
4600 enum print_stop_action val;
4601
4602 /* Maybe another breakpoint in the chain caused us to stop.
4603 (Currently all watchpoints go on the bpstat whether hit or not.
4604 That probably could (should) be changed, provided care is taken
4605 with respect to bpstat_explains_signal). */
4606 for (; bs; bs = bs->next)
4607 {
4608 val = print_bp_stop_message (bs);
4609 if (val == PRINT_SRC_ONLY
4610 || val == PRINT_SRC_AND_LOC
4611 || val == PRINT_NOTHING)
4612 return val;
4613 }
4614
4615 /* If we had hit a shared library event breakpoint,
4616 print_bp_stop_message would print out this message. If we hit an
4617 OS-level shared library event, do the same thing. */
4618 if (kind == TARGET_WAITKIND_LOADED)
4619 {
4620 print_solib_event (0);
4621 return PRINT_NOTHING;
4622 }
4623
4624 /* We reached the end of the chain, or we got a null BS to start
4625 with and nothing was printed. */
4626 return PRINT_UNKNOWN;
4627 }
4628
4629 /* Evaluate the boolean expression EXP and return the result. */
4630
4631 static bool
4632 breakpoint_cond_eval (expression *exp)
4633 {
4634 struct value *mark = value_mark ();
4635 bool res = value_true (evaluate_expression (exp));
4636
4637 value_free_to_mark (mark);
4638 return res;
4639 }
4640
4641 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4642
4643 bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4644 : next (NULL),
4645 bp_location_at (bl),
4646 breakpoint_at (bl->owner),
4647 commands (NULL),
4648 print (0),
4649 stop (0),
4650 print_it (print_it_normal)
4651 {
4652 incref_bp_location (bl);
4653 **bs_link_pointer = this;
4654 *bs_link_pointer = &next;
4655 }
4656
4657 bpstats::bpstats ()
4658 : next (NULL),
4659 bp_location_at (NULL),
4660 breakpoint_at (NULL),
4661 commands (NULL),
4662 print (0),
4663 stop (0),
4664 print_it (print_it_normal)
4665 {
4666 }
4667 \f
4668 /* The target has stopped with waitstatus WS. Check if any hardware
4669 watchpoints have triggered, according to the target. */
4670
4671 int
4672 watchpoints_triggered (struct target_waitstatus *ws)
4673 {
4674 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
4675 CORE_ADDR addr;
4676 struct breakpoint *b;
4677
4678 if (!stopped_by_watchpoint)
4679 {
4680 /* We were not stopped by a watchpoint. Mark all watchpoints
4681 as not triggered. */
4682 ALL_BREAKPOINTS (b)
4683 if (is_hardware_watchpoint (b))
4684 {
4685 struct watchpoint *w = (struct watchpoint *) b;
4686
4687 w->watchpoint_triggered = watch_triggered_no;
4688 }
4689
4690 return 0;
4691 }
4692
4693 if (!target_stopped_data_address (current_top_target (), &addr))
4694 {
4695 /* We were stopped by a watchpoint, but we don't know where.
4696 Mark all watchpoints as unknown. */
4697 ALL_BREAKPOINTS (b)
4698 if (is_hardware_watchpoint (b))
4699 {
4700 struct watchpoint *w = (struct watchpoint *) b;
4701
4702 w->watchpoint_triggered = watch_triggered_unknown;
4703 }
4704
4705 return 1;
4706 }
4707
4708 /* The target could report the data address. Mark watchpoints
4709 affected by this data address as triggered, and all others as not
4710 triggered. */
4711
4712 ALL_BREAKPOINTS (b)
4713 if (is_hardware_watchpoint (b))
4714 {
4715 struct watchpoint *w = (struct watchpoint *) b;
4716 struct bp_location *loc;
4717
4718 w->watchpoint_triggered = watch_triggered_no;
4719 for (loc = b->loc; loc; loc = loc->next)
4720 {
4721 if (is_masked_watchpoint (b))
4722 {
4723 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4724 CORE_ADDR start = loc->address & w->hw_wp_mask;
4725
4726 if (newaddr == start)
4727 {
4728 w->watchpoint_triggered = watch_triggered_yes;
4729 break;
4730 }
4731 }
4732 /* Exact match not required. Within range is sufficient. */
4733 else if (target_watchpoint_addr_within_range (current_top_target (),
4734 addr, loc->address,
4735 loc->length))
4736 {
4737 w->watchpoint_triggered = watch_triggered_yes;
4738 break;
4739 }
4740 }
4741 }
4742
4743 return 1;
4744 }
4745
4746 /* Possible return values for watchpoint_check. */
4747 enum wp_check_result
4748 {
4749 /* The watchpoint has been deleted. */
4750 WP_DELETED = 1,
4751
4752 /* The value has changed. */
4753 WP_VALUE_CHANGED = 2,
4754
4755 /* The value has not changed. */
4756 WP_VALUE_NOT_CHANGED = 3,
4757
4758 /* Ignore this watchpoint, no matter if the value changed or not. */
4759 WP_IGNORE = 4,
4760 };
4761
4762 #define BP_TEMPFLAG 1
4763 #define BP_HARDWAREFLAG 2
4764
4765 /* Evaluate watchpoint condition expression and check if its value
4766 changed. */
4767
4768 static wp_check_result
4769 watchpoint_check (bpstat bs)
4770 {
4771 struct watchpoint *b;
4772 struct frame_info *fr;
4773 int within_current_scope;
4774
4775 /* BS is built from an existing struct breakpoint. */
4776 gdb_assert (bs->breakpoint_at != NULL);
4777 b = (struct watchpoint *) bs->breakpoint_at;
4778
4779 /* If this is a local watchpoint, we only want to check if the
4780 watchpoint frame is in scope if the current thread is the thread
4781 that was used to create the watchpoint. */
4782 if (!watchpoint_in_thread_scope (b))
4783 return WP_IGNORE;
4784
4785 if (b->exp_valid_block == NULL)
4786 within_current_scope = 1;
4787 else
4788 {
4789 struct frame_info *frame = get_current_frame ();
4790 struct gdbarch *frame_arch = get_frame_arch (frame);
4791 CORE_ADDR frame_pc = get_frame_pc (frame);
4792
4793 /* stack_frame_destroyed_p() returns a non-zero value if we're
4794 still in the function but the stack frame has already been
4795 invalidated. Since we can't rely on the values of local
4796 variables after the stack has been destroyed, we are treating
4797 the watchpoint in that state as `not changed' without further
4798 checking. Don't mark watchpoints as changed if the current
4799 frame is in an epilogue - even if they are in some other
4800 frame, our view of the stack is likely to be wrong and
4801 frame_find_by_id could error out. */
4802 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
4803 return WP_IGNORE;
4804
4805 fr = frame_find_by_id (b->watchpoint_frame);
4806 within_current_scope = (fr != NULL);
4807
4808 /* If we've gotten confused in the unwinder, we might have
4809 returned a frame that can't describe this variable. */
4810 if (within_current_scope)
4811 {
4812 struct symbol *function;
4813
4814 function = get_frame_function (fr);
4815 if (function == NULL
4816 || !contained_in (b->exp_valid_block,
4817 SYMBOL_BLOCK_VALUE (function)))
4818 within_current_scope = 0;
4819 }
4820
4821 if (within_current_scope)
4822 /* If we end up stopping, the current frame will get selected
4823 in normal_stop. So this call to select_frame won't affect
4824 the user. */
4825 select_frame (fr);
4826 }
4827
4828 if (within_current_scope)
4829 {
4830 /* We use value_{,free_to_}mark because it could be a *long*
4831 time before we return to the command level and call
4832 free_all_values. We can't call free_all_values because we
4833 might be in the middle of evaluating a function call. */
4834
4835 int pc = 0;
4836 struct value *mark;
4837 struct value *new_val;
4838
4839 if (is_masked_watchpoint (b))
4840 /* Since we don't know the exact trigger address (from
4841 stopped_data_address), just tell the user we've triggered
4842 a mask watchpoint. */
4843 return WP_VALUE_CHANGED;
4844
4845 mark = value_mark ();
4846 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
4847
4848 if (b->val_bitsize != 0)
4849 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4850
4851 /* We use value_equal_contents instead of value_equal because
4852 the latter coerces an array to a pointer, thus comparing just
4853 the address of the array instead of its contents. This is
4854 not what we want. */
4855 if ((b->val != NULL) != (new_val != NULL)
4856 || (b->val != NULL && !value_equal_contents (b->val.get (),
4857 new_val)))
4858 {
4859 bs->old_val = b->val;
4860 b->val = release_value (new_val);
4861 b->val_valid = true;
4862 if (new_val != NULL)
4863 value_free_to_mark (mark);
4864 return WP_VALUE_CHANGED;
4865 }
4866 else
4867 {
4868 /* Nothing changed. */
4869 value_free_to_mark (mark);
4870 return WP_VALUE_NOT_CHANGED;
4871 }
4872 }
4873 else
4874 {
4875 /* This seems like the only logical thing to do because
4876 if we temporarily ignored the watchpoint, then when
4877 we reenter the block in which it is valid it contains
4878 garbage (in the case of a function, it may have two
4879 garbage values, one before and one after the prologue).
4880 So we can't even detect the first assignment to it and
4881 watch after that (since the garbage may or may not equal
4882 the first value assigned). */
4883 /* We print all the stop information in
4884 breakpoint_ops->print_it, but in this case, by the time we
4885 call breakpoint_ops->print_it this bp will be deleted
4886 already. So we have no choice but print the information
4887 here. */
4888
4889 SWITCH_THRU_ALL_UIS ()
4890 {
4891 struct ui_out *uiout = current_uiout;
4892
4893 if (uiout->is_mi_like_p ())
4894 uiout->field_string
4895 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
4896 uiout->message ("\nWatchpoint %pF deleted because the program has "
4897 "left the block in\n"
4898 "which its expression is valid.\n",
4899 signed_field ("wpnum", b->number));
4900 }
4901
4902 /* Make sure the watchpoint's commands aren't executed. */
4903 b->commands = NULL;
4904 watchpoint_del_at_next_stop (b);
4905
4906 return WP_DELETED;
4907 }
4908 }
4909
4910 /* Return true if it looks like target has stopped due to hitting
4911 breakpoint location BL. This function does not check if we should
4912 stop, only if BL explains the stop. */
4913
4914 static int
4915 bpstat_check_location (const struct bp_location *bl,
4916 const address_space *aspace, CORE_ADDR bp_addr,
4917 const struct target_waitstatus *ws)
4918 {
4919 struct breakpoint *b = bl->owner;
4920
4921 /* BL is from an existing breakpoint. */
4922 gdb_assert (b != NULL);
4923
4924 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
4925 }
4926
4927 /* Determine if the watched values have actually changed, and we
4928 should stop. If not, set BS->stop to 0. */
4929
4930 static void
4931 bpstat_check_watchpoint (bpstat bs)
4932 {
4933 const struct bp_location *bl;
4934 struct watchpoint *b;
4935
4936 /* BS is built for existing struct breakpoint. */
4937 bl = bs->bp_location_at;
4938 gdb_assert (bl != NULL);
4939 b = (struct watchpoint *) bs->breakpoint_at;
4940 gdb_assert (b != NULL);
4941
4942 {
4943 int must_check_value = 0;
4944
4945 if (b->type == bp_watchpoint)
4946 /* For a software watchpoint, we must always check the
4947 watched value. */
4948 must_check_value = 1;
4949 else if (b->watchpoint_triggered == watch_triggered_yes)
4950 /* We have a hardware watchpoint (read, write, or access)
4951 and the target earlier reported an address watched by
4952 this watchpoint. */
4953 must_check_value = 1;
4954 else if (b->watchpoint_triggered == watch_triggered_unknown
4955 && b->type == bp_hardware_watchpoint)
4956 /* We were stopped by a hardware watchpoint, but the target could
4957 not report the data address. We must check the watchpoint's
4958 value. Access and read watchpoints are out of luck; without
4959 a data address, we can't figure it out. */
4960 must_check_value = 1;
4961
4962 if (must_check_value)
4963 {
4964 wp_check_result e;
4965
4966 try
4967 {
4968 e = watchpoint_check (bs);
4969 }
4970 catch (const gdb_exception &ex)
4971 {
4972 exception_fprintf (gdb_stderr, ex,
4973 "Error evaluating expression "
4974 "for watchpoint %d\n",
4975 b->number);
4976
4977 SWITCH_THRU_ALL_UIS ()
4978 {
4979 printf_filtered (_("Watchpoint %d deleted.\n"),
4980 b->number);
4981 }
4982 watchpoint_del_at_next_stop (b);
4983 e = WP_DELETED;
4984 }
4985
4986 switch (e)
4987 {
4988 case WP_DELETED:
4989 /* We've already printed what needs to be printed. */
4990 bs->print_it = print_it_done;
4991 /* Stop. */
4992 break;
4993 case WP_IGNORE:
4994 bs->print_it = print_it_noop;
4995 bs->stop = 0;
4996 break;
4997 case WP_VALUE_CHANGED:
4998 if (b->type == bp_read_watchpoint)
4999 {
5000 /* There are two cases to consider here:
5001
5002 1. We're watching the triggered memory for reads.
5003 In that case, trust the target, and always report
5004 the watchpoint hit to the user. Even though
5005 reads don't cause value changes, the value may
5006 have changed since the last time it was read, and
5007 since we're not trapping writes, we will not see
5008 those, and as such we should ignore our notion of
5009 old value.
5010
5011 2. We're watching the triggered memory for both
5012 reads and writes. There are two ways this may
5013 happen:
5014
5015 2.1. This is a target that can't break on data
5016 reads only, but can break on accesses (reads or
5017 writes), such as e.g., x86. We detect this case
5018 at the time we try to insert read watchpoints.
5019
5020 2.2. Otherwise, the target supports read
5021 watchpoints, but, the user set an access or write
5022 watchpoint watching the same memory as this read
5023 watchpoint.
5024
5025 If we're watching memory writes as well as reads,
5026 ignore watchpoint hits when we find that the
5027 value hasn't changed, as reads don't cause
5028 changes. This still gives false positives when
5029 the program writes the same value to memory as
5030 what there was already in memory (we will confuse
5031 it for a read), but it's much better than
5032 nothing. */
5033
5034 int other_write_watchpoint = 0;
5035
5036 if (bl->watchpoint_type == hw_read)
5037 {
5038 struct breakpoint *other_b;
5039
5040 ALL_BREAKPOINTS (other_b)
5041 if (other_b->type == bp_hardware_watchpoint
5042 || other_b->type == bp_access_watchpoint)
5043 {
5044 struct watchpoint *other_w =
5045 (struct watchpoint *) other_b;
5046
5047 if (other_w->watchpoint_triggered
5048 == watch_triggered_yes)
5049 {
5050 other_write_watchpoint = 1;
5051 break;
5052 }
5053 }
5054 }
5055
5056 if (other_write_watchpoint
5057 || bl->watchpoint_type == hw_access)
5058 {
5059 /* We're watching the same memory for writes,
5060 and the value changed since the last time we
5061 updated it, so this trap must be for a write.
5062 Ignore it. */
5063 bs->print_it = print_it_noop;
5064 bs->stop = 0;
5065 }
5066 }
5067 break;
5068 case WP_VALUE_NOT_CHANGED:
5069 if (b->type == bp_hardware_watchpoint
5070 || b->type == bp_watchpoint)
5071 {
5072 /* Don't stop: write watchpoints shouldn't fire if
5073 the value hasn't changed. */
5074 bs->print_it = print_it_noop;
5075 bs->stop = 0;
5076 }
5077 /* Stop. */
5078 break;
5079 default:
5080 /* Can't happen. */
5081 break;
5082 }
5083 }
5084 else /* must_check_value == 0 */
5085 {
5086 /* This is a case where some watchpoint(s) triggered, but
5087 not at the address of this watchpoint, or else no
5088 watchpoint triggered after all. So don't print
5089 anything for this watchpoint. */
5090 bs->print_it = print_it_noop;
5091 bs->stop = 0;
5092 }
5093 }
5094 }
5095
5096 /* For breakpoints that are currently marked as telling gdb to stop,
5097 check conditions (condition proper, frame, thread and ignore count)
5098 of breakpoint referred to by BS. If we should not stop for this
5099 breakpoint, set BS->stop to 0. */
5100
5101 static void
5102 bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
5103 {
5104 const struct bp_location *bl;
5105 struct breakpoint *b;
5106 /* Assume stop. */
5107 bool condition_result = true;
5108 struct expression *cond;
5109
5110 gdb_assert (bs->stop);
5111
5112 /* BS is built for existing struct breakpoint. */
5113 bl = bs->bp_location_at;
5114 gdb_assert (bl != NULL);
5115 b = bs->breakpoint_at;
5116 gdb_assert (b != NULL);
5117
5118 /* Even if the target evaluated the condition on its end and notified GDB, we
5119 need to do so again since GDB does not know if we stopped due to a
5120 breakpoint or a single step breakpoint. */
5121
5122 if (frame_id_p (b->frame_id)
5123 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5124 {
5125 bs->stop = 0;
5126 return;
5127 }
5128
5129 /* If this is a thread/task-specific breakpoint, don't waste cpu
5130 evaluating the condition if this isn't the specified
5131 thread/task. */
5132 if ((b->thread != -1 && b->thread != thread->global_num)
5133 || (b->task != 0 && b->task != ada_get_task_number (thread)))
5134 {
5135 bs->stop = 0;
5136 return;
5137 }
5138
5139 /* Evaluate extension language breakpoints that have a "stop" method
5140 implemented. */
5141 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5142
5143 if (is_watchpoint (b))
5144 {
5145 struct watchpoint *w = (struct watchpoint *) b;
5146
5147 cond = w->cond_exp.get ();
5148 }
5149 else
5150 cond = bl->cond.get ();
5151
5152 if (cond && b->disposition != disp_del_at_next_stop)
5153 {
5154 int within_current_scope = 1;
5155 struct watchpoint * w;
5156
5157 /* We use value_mark and value_free_to_mark because it could
5158 be a long time before we return to the command level and
5159 call free_all_values. We can't call free_all_values
5160 because we might be in the middle of evaluating a
5161 function call. */
5162 struct value *mark = value_mark ();
5163
5164 if (is_watchpoint (b))
5165 w = (struct watchpoint *) b;
5166 else
5167 w = NULL;
5168
5169 /* Need to select the frame, with all that implies so that
5170 the conditions will have the right context. Because we
5171 use the frame, we will not see an inlined function's
5172 variables when we arrive at a breakpoint at the start
5173 of the inlined function; the current frame will be the
5174 call site. */
5175 if (w == NULL || w->cond_exp_valid_block == NULL)
5176 select_frame (get_current_frame ());
5177 else
5178 {
5179 struct frame_info *frame;
5180
5181 /* For local watchpoint expressions, which particular
5182 instance of a local is being watched matters, so we
5183 keep track of the frame to evaluate the expression
5184 in. To evaluate the condition however, it doesn't
5185 really matter which instantiation of the function
5186 where the condition makes sense triggers the
5187 watchpoint. This allows an expression like "watch
5188 global if q > 10" set in `func', catch writes to
5189 global on all threads that call `func', or catch
5190 writes on all recursive calls of `func' by a single
5191 thread. We simply always evaluate the condition in
5192 the innermost frame that's executing where it makes
5193 sense to evaluate the condition. It seems
5194 intuitive. */
5195 frame = block_innermost_frame (w->cond_exp_valid_block);
5196 if (frame != NULL)
5197 select_frame (frame);
5198 else
5199 within_current_scope = 0;
5200 }
5201 if (within_current_scope)
5202 {
5203 try
5204 {
5205 condition_result = breakpoint_cond_eval (cond);
5206 }
5207 catch (const gdb_exception &ex)
5208 {
5209 exception_fprintf (gdb_stderr, ex,
5210 "Error in testing breakpoint condition:\n");
5211 }
5212 }
5213 else
5214 {
5215 warning (_("Watchpoint condition cannot be tested "
5216 "in the current scope"));
5217 /* If we failed to set the right context for this
5218 watchpoint, unconditionally report it. */
5219 }
5220 /* FIXME-someday, should give breakpoint #. */
5221 value_free_to_mark (mark);
5222 }
5223
5224 if (cond && !condition_result)
5225 {
5226 bs->stop = 0;
5227 }
5228 else if (b->ignore_count > 0)
5229 {
5230 b->ignore_count--;
5231 bs->stop = 0;
5232 /* Increase the hit count even though we don't stop. */
5233 ++(b->hit_count);
5234 gdb::observers::breakpoint_modified.notify (b);
5235 }
5236 }
5237
5238 /* Returns true if we need to track moribund locations of LOC's type
5239 on the current target. */
5240
5241 static int
5242 need_moribund_for_location_type (struct bp_location *loc)
5243 {
5244 return ((loc->loc_type == bp_loc_software_breakpoint
5245 && !target_supports_stopped_by_sw_breakpoint ())
5246 || (loc->loc_type == bp_loc_hardware_breakpoint
5247 && !target_supports_stopped_by_hw_breakpoint ()));
5248 }
5249
5250 /* See breakpoint.h. */
5251
5252 bpstat
5253 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5254 const struct target_waitstatus *ws)
5255 {
5256 struct breakpoint *b;
5257 bpstat bs_head = NULL, *bs_link = &bs_head;
5258
5259 ALL_BREAKPOINTS (b)
5260 {
5261 if (!breakpoint_enabled (b))
5262 continue;
5263
5264 for (bp_location *bl = b->loc; bl != NULL; bl = bl->next)
5265 {
5266 /* For hardware watchpoints, we look only at the first
5267 location. The watchpoint_check function will work on the
5268 entire expression, not the individual locations. For
5269 read watchpoints, the watchpoints_triggered function has
5270 checked all locations already. */
5271 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5272 break;
5273
5274 if (!bl->enabled || bl->shlib_disabled)
5275 continue;
5276
5277 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5278 continue;
5279
5280 /* Come here if it's a watchpoint, or if the break address
5281 matches. */
5282
5283 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5284 explain stop. */
5285
5286 /* Assume we stop. Should we find a watchpoint that is not
5287 actually triggered, or if the condition of the breakpoint
5288 evaluates as false, we'll reset 'stop' to 0. */
5289 bs->stop = 1;
5290 bs->print = 1;
5291
5292 /* If this is a scope breakpoint, mark the associated
5293 watchpoint as triggered so that we will handle the
5294 out-of-scope event. We'll get to the watchpoint next
5295 iteration. */
5296 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5297 {
5298 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5299
5300 w->watchpoint_triggered = watch_triggered_yes;
5301 }
5302 }
5303 }
5304
5305 /* Check if a moribund breakpoint explains the stop. */
5306 if (!target_supports_stopped_by_sw_breakpoint ()
5307 || !target_supports_stopped_by_hw_breakpoint ())
5308 {
5309 for (bp_location *loc : moribund_locations)
5310 {
5311 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5312 && need_moribund_for_location_type (loc))
5313 {
5314 bpstat bs = new bpstats (loc, &bs_link);
5315 /* For hits of moribund locations, we should just proceed. */
5316 bs->stop = 0;
5317 bs->print = 0;
5318 bs->print_it = print_it_noop;
5319 }
5320 }
5321 }
5322
5323 return bs_head;
5324 }
5325
5326 /* See breakpoint.h. */
5327
5328 bpstat
5329 bpstat_stop_status (const address_space *aspace,
5330 CORE_ADDR bp_addr, thread_info *thread,
5331 const struct target_waitstatus *ws,
5332 bpstat stop_chain)
5333 {
5334 struct breakpoint *b = NULL;
5335 /* First item of allocated bpstat's. */
5336 bpstat bs_head = stop_chain;
5337 bpstat bs;
5338 int need_remove_insert;
5339 int removed_any;
5340
5341 /* First, build the bpstat chain with locations that explain a
5342 target stop, while being careful to not set the target running,
5343 as that may invalidate locations (in particular watchpoint
5344 locations are recreated). Resuming will happen here with
5345 breakpoint conditions or watchpoint expressions that include
5346 inferior function calls. */
5347 if (bs_head == NULL)
5348 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5349
5350 /* A bit of special processing for shlib breakpoints. We need to
5351 process solib loading here, so that the lists of loaded and
5352 unloaded libraries are correct before we handle "catch load" and
5353 "catch unload". */
5354 for (bs = bs_head; bs != NULL; bs = bs->next)
5355 {
5356 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5357 {
5358 handle_solib_event ();
5359 break;
5360 }
5361 }
5362
5363 /* Now go through the locations that caused the target to stop, and
5364 check whether we're interested in reporting this stop to higher
5365 layers, or whether we should resume the target transparently. */
5366
5367 removed_any = 0;
5368
5369 for (bs = bs_head; bs != NULL; bs = bs->next)
5370 {
5371 if (!bs->stop)
5372 continue;
5373
5374 b = bs->breakpoint_at;
5375 b->ops->check_status (bs);
5376 if (bs->stop)
5377 {
5378 bpstat_check_breakpoint_conditions (bs, thread);
5379
5380 if (bs->stop)
5381 {
5382 ++(b->hit_count);
5383 gdb::observers::breakpoint_modified.notify (b);
5384
5385 /* We will stop here. */
5386 if (b->disposition == disp_disable)
5387 {
5388 --(b->enable_count);
5389 if (b->enable_count <= 0)
5390 b->enable_state = bp_disabled;
5391 removed_any = 1;
5392 }
5393 if (b->silent)
5394 bs->print = 0;
5395 bs->commands = b->commands;
5396 if (command_line_is_silent (bs->commands
5397 ? bs->commands.get () : NULL))
5398 bs->print = 0;
5399
5400 b->ops->after_condition_true (bs);
5401 }
5402
5403 }
5404
5405 /* Print nothing for this entry if we don't stop or don't
5406 print. */
5407 if (!bs->stop || !bs->print)
5408 bs->print_it = print_it_noop;
5409 }
5410
5411 /* If we aren't stopping, the value of some hardware watchpoint may
5412 not have changed, but the intermediate memory locations we are
5413 watching may have. Don't bother if we're stopping; this will get
5414 done later. */
5415 need_remove_insert = 0;
5416 if (! bpstat_causes_stop (bs_head))
5417 for (bs = bs_head; bs != NULL; bs = bs->next)
5418 if (!bs->stop
5419 && bs->breakpoint_at
5420 && is_hardware_watchpoint (bs->breakpoint_at))
5421 {
5422 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5423
5424 update_watchpoint (w, 0 /* don't reparse. */);
5425 need_remove_insert = 1;
5426 }
5427
5428 if (need_remove_insert)
5429 update_global_location_list (UGLL_MAY_INSERT);
5430 else if (removed_any)
5431 update_global_location_list (UGLL_DONT_INSERT);
5432
5433 return bs_head;
5434 }
5435
5436 static void
5437 handle_jit_event (void)
5438 {
5439 struct frame_info *frame;
5440 struct gdbarch *gdbarch;
5441
5442 if (debug_infrun)
5443 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5444
5445 /* Switch terminal for any messages produced by
5446 breakpoint_re_set. */
5447 target_terminal::ours_for_output ();
5448
5449 frame = get_current_frame ();
5450 gdbarch = get_frame_arch (frame);
5451
5452 jit_event_handler (gdbarch);
5453
5454 target_terminal::inferior ();
5455 }
5456
5457 /* Prepare WHAT final decision for infrun. */
5458
5459 /* Decide what infrun needs to do with this bpstat. */
5460
5461 struct bpstat_what
5462 bpstat_what (bpstat bs_head)
5463 {
5464 struct bpstat_what retval;
5465 bpstat bs;
5466
5467 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5468 retval.call_dummy = STOP_NONE;
5469 retval.is_longjmp = false;
5470
5471 for (bs = bs_head; bs != NULL; bs = bs->next)
5472 {
5473 /* Extract this BS's action. After processing each BS, we check
5474 if its action overrides all we've seem so far. */
5475 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5476 enum bptype bptype;
5477
5478 if (bs->breakpoint_at == NULL)
5479 {
5480 /* I suspect this can happen if it was a momentary
5481 breakpoint which has since been deleted. */
5482 bptype = bp_none;
5483 }
5484 else
5485 bptype = bs->breakpoint_at->type;
5486
5487 switch (bptype)
5488 {
5489 case bp_none:
5490 break;
5491 case bp_breakpoint:
5492 case bp_hardware_breakpoint:
5493 case bp_single_step:
5494 case bp_until:
5495 case bp_finish:
5496 case bp_shlib_event:
5497 if (bs->stop)
5498 {
5499 if (bs->print)
5500 this_action = BPSTAT_WHAT_STOP_NOISY;
5501 else
5502 this_action = BPSTAT_WHAT_STOP_SILENT;
5503 }
5504 else
5505 this_action = BPSTAT_WHAT_SINGLE;
5506 break;
5507 case bp_watchpoint:
5508 case bp_hardware_watchpoint:
5509 case bp_read_watchpoint:
5510 case bp_access_watchpoint:
5511 if (bs->stop)
5512 {
5513 if (bs->print)
5514 this_action = BPSTAT_WHAT_STOP_NOISY;
5515 else
5516 this_action = BPSTAT_WHAT_STOP_SILENT;
5517 }
5518 else
5519 {
5520 /* There was a watchpoint, but we're not stopping.
5521 This requires no further action. */
5522 }
5523 break;
5524 case bp_longjmp:
5525 case bp_longjmp_call_dummy:
5526 case bp_exception:
5527 if (bs->stop)
5528 {
5529 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5530 retval.is_longjmp = bptype != bp_exception;
5531 }
5532 else
5533 this_action = BPSTAT_WHAT_SINGLE;
5534 break;
5535 case bp_longjmp_resume:
5536 case bp_exception_resume:
5537 if (bs->stop)
5538 {
5539 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5540 retval.is_longjmp = bptype == bp_longjmp_resume;
5541 }
5542 else
5543 this_action = BPSTAT_WHAT_SINGLE;
5544 break;
5545 case bp_step_resume:
5546 if (bs->stop)
5547 this_action = BPSTAT_WHAT_STEP_RESUME;
5548 else
5549 {
5550 /* It is for the wrong frame. */
5551 this_action = BPSTAT_WHAT_SINGLE;
5552 }
5553 break;
5554 case bp_hp_step_resume:
5555 if (bs->stop)
5556 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5557 else
5558 {
5559 /* It is for the wrong frame. */
5560 this_action = BPSTAT_WHAT_SINGLE;
5561 }
5562 break;
5563 case bp_watchpoint_scope:
5564 case bp_thread_event:
5565 case bp_overlay_event:
5566 case bp_longjmp_master:
5567 case bp_std_terminate_master:
5568 case bp_exception_master:
5569 this_action = BPSTAT_WHAT_SINGLE;
5570 break;
5571 case bp_catchpoint:
5572 if (bs->stop)
5573 {
5574 if (bs->print)
5575 this_action = BPSTAT_WHAT_STOP_NOISY;
5576 else
5577 this_action = BPSTAT_WHAT_STOP_SILENT;
5578 }
5579 else
5580 {
5581 /* Some catchpoints are implemented with breakpoints.
5582 For those, we need to step over the breakpoint. */
5583 if (bs->bp_location_at->loc_type != bp_loc_other)
5584 this_action = BPSTAT_WHAT_SINGLE;
5585 }
5586 break;
5587 case bp_jit_event:
5588 this_action = BPSTAT_WHAT_SINGLE;
5589 break;
5590 case bp_call_dummy:
5591 /* Make sure the action is stop (silent or noisy),
5592 so infrun.c pops the dummy frame. */
5593 retval.call_dummy = STOP_STACK_DUMMY;
5594 this_action = BPSTAT_WHAT_STOP_SILENT;
5595 break;
5596 case bp_std_terminate:
5597 /* Make sure the action is stop (silent or noisy),
5598 so infrun.c pops the dummy frame. */
5599 retval.call_dummy = STOP_STD_TERMINATE;
5600 this_action = BPSTAT_WHAT_STOP_SILENT;
5601 break;
5602 case bp_tracepoint:
5603 case bp_fast_tracepoint:
5604 case bp_static_tracepoint:
5605 /* Tracepoint hits should not be reported back to GDB, and
5606 if one got through somehow, it should have been filtered
5607 out already. */
5608 internal_error (__FILE__, __LINE__,
5609 _("bpstat_what: tracepoint encountered"));
5610 break;
5611 case bp_gnu_ifunc_resolver:
5612 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5613 this_action = BPSTAT_WHAT_SINGLE;
5614 break;
5615 case bp_gnu_ifunc_resolver_return:
5616 /* The breakpoint will be removed, execution will restart from the
5617 PC of the former breakpoint. */
5618 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5619 break;
5620
5621 case bp_dprintf:
5622 if (bs->stop)
5623 this_action = BPSTAT_WHAT_STOP_SILENT;
5624 else
5625 this_action = BPSTAT_WHAT_SINGLE;
5626 break;
5627
5628 default:
5629 internal_error (__FILE__, __LINE__,
5630 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5631 }
5632
5633 retval.main_action = std::max (retval.main_action, this_action);
5634 }
5635
5636 return retval;
5637 }
5638
5639 void
5640 bpstat_run_callbacks (bpstat bs_head)
5641 {
5642 bpstat bs;
5643
5644 for (bs = bs_head; bs != NULL; bs = bs->next)
5645 {
5646 struct breakpoint *b = bs->breakpoint_at;
5647
5648 if (b == NULL)
5649 continue;
5650 switch (b->type)
5651 {
5652 case bp_jit_event:
5653 handle_jit_event ();
5654 break;
5655 case bp_gnu_ifunc_resolver:
5656 gnu_ifunc_resolver_stop (b);
5657 break;
5658 case bp_gnu_ifunc_resolver_return:
5659 gnu_ifunc_resolver_return_stop (b);
5660 break;
5661 }
5662 }
5663 }
5664
5665 /* See breakpoint.h. */
5666
5667 bool
5668 bpstat_should_step ()
5669 {
5670 struct breakpoint *b;
5671
5672 ALL_BREAKPOINTS (b)
5673 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5674 return true;
5675 return false;
5676 }
5677
5678 /* See breakpoint.h. */
5679
5680 bool
5681 bpstat_causes_stop (bpstat bs)
5682 {
5683 for (; bs != NULL; bs = bs->next)
5684 if (bs->stop)
5685 return true;
5686
5687 return false;
5688 }
5689
5690 \f
5691
5692 /* Compute a string of spaces suitable to indent the next line
5693 so it starts at the position corresponding to the table column
5694 named COL_NAME in the currently active table of UIOUT. */
5695
5696 static char *
5697 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5698 {
5699 static char wrap_indent[80];
5700 int i, total_width, width, align;
5701 const char *text;
5702
5703 total_width = 0;
5704 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5705 {
5706 if (strcmp (text, col_name) == 0)
5707 {
5708 gdb_assert (total_width < sizeof wrap_indent);
5709 memset (wrap_indent, ' ', total_width);
5710 wrap_indent[total_width] = 0;
5711
5712 return wrap_indent;
5713 }
5714
5715 total_width += width + 1;
5716 }
5717
5718 return NULL;
5719 }
5720
5721 /* Determine if the locations of this breakpoint will have their conditions
5722 evaluated by the target, host or a mix of both. Returns the following:
5723
5724 "host": Host evals condition.
5725 "host or target": Host or Target evals condition.
5726 "target": Target evals condition.
5727 */
5728
5729 static const char *
5730 bp_condition_evaluator (struct breakpoint *b)
5731 {
5732 struct bp_location *bl;
5733 char host_evals = 0;
5734 char target_evals = 0;
5735
5736 if (!b)
5737 return NULL;
5738
5739 if (!is_breakpoint (b))
5740 return NULL;
5741
5742 if (gdb_evaluates_breakpoint_condition_p ()
5743 || !target_supports_evaluation_of_breakpoint_conditions ())
5744 return condition_evaluation_host;
5745
5746 for (bl = b->loc; bl; bl = bl->next)
5747 {
5748 if (bl->cond_bytecode)
5749 target_evals++;
5750 else
5751 host_evals++;
5752 }
5753
5754 if (host_evals && target_evals)
5755 return condition_evaluation_both;
5756 else if (target_evals)
5757 return condition_evaluation_target;
5758 else
5759 return condition_evaluation_host;
5760 }
5761
5762 /* Determine the breakpoint location's condition evaluator. This is
5763 similar to bp_condition_evaluator, but for locations. */
5764
5765 static const char *
5766 bp_location_condition_evaluator (struct bp_location *bl)
5767 {
5768 if (bl && !is_breakpoint (bl->owner))
5769 return NULL;
5770
5771 if (gdb_evaluates_breakpoint_condition_p ()
5772 || !target_supports_evaluation_of_breakpoint_conditions ())
5773 return condition_evaluation_host;
5774
5775 if (bl && bl->cond_bytecode)
5776 return condition_evaluation_target;
5777 else
5778 return condition_evaluation_host;
5779 }
5780
5781 /* Print the LOC location out of the list of B->LOC locations. */
5782
5783 static void
5784 print_breakpoint_location (struct breakpoint *b,
5785 struct bp_location *loc)
5786 {
5787 struct ui_out *uiout = current_uiout;
5788
5789 scoped_restore_current_program_space restore_pspace;
5790
5791 if (loc != NULL && loc->shlib_disabled)
5792 loc = NULL;
5793
5794 if (loc != NULL)
5795 set_current_program_space (loc->pspace);
5796
5797 if (b->display_canonical)
5798 uiout->field_string ("what", event_location_to_string (b->location.get ()));
5799 else if (loc && loc->symtab)
5800 {
5801 const struct symbol *sym = loc->symbol;
5802
5803 if (sym)
5804 {
5805 uiout->text ("in ");
5806 uiout->field_string ("func", sym->print_name (),
5807 function_name_style.style ());
5808 uiout->text (" ");
5809 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5810 uiout->text ("at ");
5811 }
5812 uiout->field_string ("file",
5813 symtab_to_filename_for_display (loc->symtab),
5814 file_name_style.style ());
5815 uiout->text (":");
5816
5817 if (uiout->is_mi_like_p ())
5818 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
5819
5820 uiout->field_signed ("line", loc->line_number);
5821 }
5822 else if (loc)
5823 {
5824 string_file stb;
5825
5826 print_address_symbolic (loc->gdbarch, loc->address, &stb,
5827 demangle, "");
5828 uiout->field_stream ("at", stb);
5829 }
5830 else
5831 {
5832 uiout->field_string ("pending",
5833 event_location_to_string (b->location.get ()));
5834 /* If extra_string is available, it could be holding a condition
5835 or dprintf arguments. In either case, make sure it is printed,
5836 too, but only for non-MI streams. */
5837 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
5838 {
5839 if (b->type == bp_dprintf)
5840 uiout->text (",");
5841 else
5842 uiout->text (" ");
5843 uiout->text (b->extra_string);
5844 }
5845 }
5846
5847 if (loc && is_breakpoint (b)
5848 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5849 && bp_condition_evaluator (b) == condition_evaluation_both)
5850 {
5851 uiout->text (" (");
5852 uiout->field_string ("evaluated-by",
5853 bp_location_condition_evaluator (loc));
5854 uiout->text (")");
5855 }
5856 }
5857
5858 static const char *
5859 bptype_string (enum bptype type)
5860 {
5861 struct ep_type_description
5862 {
5863 enum bptype type;
5864 const char *description;
5865 };
5866 static struct ep_type_description bptypes[] =
5867 {
5868 {bp_none, "?deleted?"},
5869 {bp_breakpoint, "breakpoint"},
5870 {bp_hardware_breakpoint, "hw breakpoint"},
5871 {bp_single_step, "sw single-step"},
5872 {bp_until, "until"},
5873 {bp_finish, "finish"},
5874 {bp_watchpoint, "watchpoint"},
5875 {bp_hardware_watchpoint, "hw watchpoint"},
5876 {bp_read_watchpoint, "read watchpoint"},
5877 {bp_access_watchpoint, "acc watchpoint"},
5878 {bp_longjmp, "longjmp"},
5879 {bp_longjmp_resume, "longjmp resume"},
5880 {bp_longjmp_call_dummy, "longjmp for call dummy"},
5881 {bp_exception, "exception"},
5882 {bp_exception_resume, "exception resume"},
5883 {bp_step_resume, "step resume"},
5884 {bp_hp_step_resume, "high-priority step resume"},
5885 {bp_watchpoint_scope, "watchpoint scope"},
5886 {bp_call_dummy, "call dummy"},
5887 {bp_std_terminate, "std::terminate"},
5888 {bp_shlib_event, "shlib events"},
5889 {bp_thread_event, "thread events"},
5890 {bp_overlay_event, "overlay events"},
5891 {bp_longjmp_master, "longjmp master"},
5892 {bp_std_terminate_master, "std::terminate master"},
5893 {bp_exception_master, "exception master"},
5894 {bp_catchpoint, "catchpoint"},
5895 {bp_tracepoint, "tracepoint"},
5896 {bp_fast_tracepoint, "fast tracepoint"},
5897 {bp_static_tracepoint, "static tracepoint"},
5898 {bp_dprintf, "dprintf"},
5899 {bp_jit_event, "jit events"},
5900 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5901 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
5902 };
5903
5904 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5905 || ((int) type != bptypes[(int) type].type))
5906 internal_error (__FILE__, __LINE__,
5907 _("bptypes table does not describe type #%d."),
5908 (int) type);
5909
5910 return bptypes[(int) type].description;
5911 }
5912
5913 /* For MI, output a field named 'thread-groups' with a list as the value.
5914 For CLI, prefix the list with the string 'inf'. */
5915
5916 static void
5917 output_thread_groups (struct ui_out *uiout,
5918 const char *field_name,
5919 const std::vector<int> &inf_nums,
5920 int mi_only)
5921 {
5922 int is_mi = uiout->is_mi_like_p ();
5923
5924 /* For backward compatibility, don't display inferiors in CLI unless
5925 there are several. Always display them for MI. */
5926 if (!is_mi && mi_only)
5927 return;
5928
5929 ui_out_emit_list list_emitter (uiout, field_name);
5930
5931 for (size_t i = 0; i < inf_nums.size (); i++)
5932 {
5933 if (is_mi)
5934 {
5935 char mi_group[10];
5936
5937 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
5938 uiout->field_string (NULL, mi_group);
5939 }
5940 else
5941 {
5942 if (i == 0)
5943 uiout->text (" inf ");
5944 else
5945 uiout->text (", ");
5946
5947 uiout->text (plongest (inf_nums[i]));
5948 }
5949 }
5950 }
5951
5952 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
5953 instead of going via breakpoint_ops::print_one. This makes "maint
5954 info breakpoints" show the software breakpoint locations of
5955 catchpoints, which are considered internal implementation
5956 detail. */
5957
5958 static void
5959 print_one_breakpoint_location (struct breakpoint *b,
5960 struct bp_location *loc,
5961 int loc_number,
5962 struct bp_location **last_loc,
5963 int allflag, bool raw_loc)
5964 {
5965 struct command_line *l;
5966 static char bpenables[] = "nynny";
5967
5968 struct ui_out *uiout = current_uiout;
5969 int header_of_multiple = 0;
5970 int part_of_multiple = (loc != NULL);
5971 struct value_print_options opts;
5972
5973 get_user_print_options (&opts);
5974
5975 gdb_assert (!loc || loc_number != 0);
5976 /* See comment in print_one_breakpoint concerning treatment of
5977 breakpoints with single disabled location. */
5978 if (loc == NULL
5979 && (b->loc != NULL
5980 && (b->loc->next != NULL || !b->loc->enabled)))
5981 header_of_multiple = 1;
5982 if (loc == NULL)
5983 loc = b->loc;
5984
5985 annotate_record ();
5986
5987 /* 1 */
5988 annotate_field (0);
5989 if (part_of_multiple)
5990 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
5991 else
5992 uiout->field_signed ("number", b->number);
5993
5994 /* 2 */
5995 annotate_field (1);
5996 if (part_of_multiple)
5997 uiout->field_skip ("type");
5998 else
5999 uiout->field_string ("type", bptype_string (b->type));
6000
6001 /* 3 */
6002 annotate_field (2);
6003 if (part_of_multiple)
6004 uiout->field_skip ("disp");
6005 else
6006 uiout->field_string ("disp", bpdisp_text (b->disposition));
6007
6008 /* 4 */
6009 annotate_field (3);
6010 if (part_of_multiple)
6011 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
6012 else
6013 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6014
6015 /* 5 and 6 */
6016 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
6017 b->ops->print_one (b, last_loc);
6018 else
6019 {
6020 if (is_watchpoint (b))
6021 {
6022 struct watchpoint *w = (struct watchpoint *) b;
6023
6024 /* Field 4, the address, is omitted (which makes the columns
6025 not line up too nicely with the headers, but the effect
6026 is relatively readable). */
6027 if (opts.addressprint)
6028 uiout->field_skip ("addr");
6029 annotate_field (5);
6030 uiout->field_string ("what", w->exp_string);
6031 }
6032 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6033 || is_ada_exception_catchpoint (b))
6034 {
6035 if (opts.addressprint)
6036 {
6037 annotate_field (4);
6038 if (header_of_multiple)
6039 uiout->field_string ("addr", "<MULTIPLE>",
6040 metadata_style.style ());
6041 else if (b->loc == NULL || loc->shlib_disabled)
6042 uiout->field_string ("addr", "<PENDING>",
6043 metadata_style.style ());
6044 else
6045 uiout->field_core_addr ("addr",
6046 loc->gdbarch, loc->address);
6047 }
6048 annotate_field (5);
6049 if (!header_of_multiple)
6050 print_breakpoint_location (b, loc);
6051 if (b->loc)
6052 *last_loc = b->loc;
6053 }
6054 }
6055
6056 if (loc != NULL && !header_of_multiple)
6057 {
6058 std::vector<int> inf_nums;
6059 int mi_only = 1;
6060
6061 for (inferior *inf : all_inferiors ())
6062 {
6063 if (inf->pspace == loc->pspace)
6064 inf_nums.push_back (inf->num);
6065 }
6066
6067 /* For backward compatibility, don't display inferiors in CLI unless
6068 there are several. Always display for MI. */
6069 if (allflag
6070 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6071 && (program_spaces.size () > 1
6072 || number_of_inferiors () > 1)
6073 /* LOC is for existing B, it cannot be in
6074 moribund_locations and thus having NULL OWNER. */
6075 && loc->owner->type != bp_catchpoint))
6076 mi_only = 0;
6077 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6078 }
6079
6080 if (!part_of_multiple)
6081 {
6082 if (b->thread != -1)
6083 {
6084 /* FIXME: This seems to be redundant and lost here; see the
6085 "stop only in" line a little further down. */
6086 uiout->text (" thread ");
6087 uiout->field_signed ("thread", b->thread);
6088 }
6089 else if (b->task != 0)
6090 {
6091 uiout->text (" task ");
6092 uiout->field_signed ("task", b->task);
6093 }
6094 }
6095
6096 uiout->text ("\n");
6097
6098 if (!part_of_multiple)
6099 b->ops->print_one_detail (b, uiout);
6100
6101 if (part_of_multiple && frame_id_p (b->frame_id))
6102 {
6103 annotate_field (6);
6104 uiout->text ("\tstop only in stack frame at ");
6105 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6106 the frame ID. */
6107 uiout->field_core_addr ("frame",
6108 b->gdbarch, b->frame_id.stack_addr);
6109 uiout->text ("\n");
6110 }
6111
6112 if (!part_of_multiple && b->cond_string)
6113 {
6114 annotate_field (7);
6115 if (is_tracepoint (b))
6116 uiout->text ("\ttrace only if ");
6117 else
6118 uiout->text ("\tstop only if ");
6119 uiout->field_string ("cond", b->cond_string);
6120
6121 /* Print whether the target is doing the breakpoint's condition
6122 evaluation. If GDB is doing the evaluation, don't print anything. */
6123 if (is_breakpoint (b)
6124 && breakpoint_condition_evaluation_mode ()
6125 == condition_evaluation_target)
6126 {
6127 uiout->message (" (%pF evals)",
6128 string_field ("evaluated-by",
6129 bp_condition_evaluator (b)));
6130 }
6131 uiout->text ("\n");
6132 }
6133
6134 if (!part_of_multiple && b->thread != -1)
6135 {
6136 /* FIXME should make an annotation for this. */
6137 uiout->text ("\tstop only in thread ");
6138 if (uiout->is_mi_like_p ())
6139 uiout->field_signed ("thread", b->thread);
6140 else
6141 {
6142 struct thread_info *thr = find_thread_global_id (b->thread);
6143
6144 uiout->field_string ("thread", print_thread_id (thr));
6145 }
6146 uiout->text ("\n");
6147 }
6148
6149 if (!part_of_multiple)
6150 {
6151 if (b->hit_count)
6152 {
6153 /* FIXME should make an annotation for this. */
6154 if (is_catchpoint (b))
6155 uiout->text ("\tcatchpoint");
6156 else if (is_tracepoint (b))
6157 uiout->text ("\ttracepoint");
6158 else
6159 uiout->text ("\tbreakpoint");
6160 uiout->text (" already hit ");
6161 uiout->field_signed ("times", b->hit_count);
6162 if (b->hit_count == 1)
6163 uiout->text (" time\n");
6164 else
6165 uiout->text (" times\n");
6166 }
6167 else
6168 {
6169 /* Output the count also if it is zero, but only if this is mi. */
6170 if (uiout->is_mi_like_p ())
6171 uiout->field_signed ("times", b->hit_count);
6172 }
6173 }
6174
6175 if (!part_of_multiple && b->ignore_count)
6176 {
6177 annotate_field (8);
6178 uiout->message ("\tignore next %pF hits\n",
6179 signed_field ("ignore", b->ignore_count));
6180 }
6181
6182 /* Note that an enable count of 1 corresponds to "enable once"
6183 behavior, which is reported by the combination of enablement and
6184 disposition, so we don't need to mention it here. */
6185 if (!part_of_multiple && b->enable_count > 1)
6186 {
6187 annotate_field (8);
6188 uiout->text ("\tdisable after ");
6189 /* Tweak the wording to clarify that ignore and enable counts
6190 are distinct, and have additive effect. */
6191 if (b->ignore_count)
6192 uiout->text ("additional ");
6193 else
6194 uiout->text ("next ");
6195 uiout->field_signed ("enable", b->enable_count);
6196 uiout->text (" hits\n");
6197 }
6198
6199 if (!part_of_multiple && is_tracepoint (b))
6200 {
6201 struct tracepoint *tp = (struct tracepoint *) b;
6202
6203 if (tp->traceframe_usage)
6204 {
6205 uiout->text ("\ttrace buffer usage ");
6206 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6207 uiout->text (" bytes\n");
6208 }
6209 }
6210
6211 l = b->commands ? b->commands.get () : NULL;
6212 if (!part_of_multiple && l)
6213 {
6214 annotate_field (9);
6215 ui_out_emit_tuple tuple_emitter (uiout, "script");
6216 print_command_lines (uiout, l, 4);
6217 }
6218
6219 if (is_tracepoint (b))
6220 {
6221 struct tracepoint *t = (struct tracepoint *) b;
6222
6223 if (!part_of_multiple && t->pass_count)
6224 {
6225 annotate_field (10);
6226 uiout->text ("\tpass count ");
6227 uiout->field_signed ("pass", t->pass_count);
6228 uiout->text (" \n");
6229 }
6230
6231 /* Don't display it when tracepoint or tracepoint location is
6232 pending. */
6233 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6234 {
6235 annotate_field (11);
6236
6237 if (uiout->is_mi_like_p ())
6238 uiout->field_string ("installed",
6239 loc->inserted ? "y" : "n");
6240 else
6241 {
6242 if (loc->inserted)
6243 uiout->text ("\t");
6244 else
6245 uiout->text ("\tnot ");
6246 uiout->text ("installed on target\n");
6247 }
6248 }
6249 }
6250
6251 if (uiout->is_mi_like_p () && !part_of_multiple)
6252 {
6253 if (is_watchpoint (b))
6254 {
6255 struct watchpoint *w = (struct watchpoint *) b;
6256
6257 uiout->field_string ("original-location", w->exp_string);
6258 }
6259 else if (b->location != NULL
6260 && event_location_to_string (b->location.get ()) != NULL)
6261 uiout->field_string ("original-location",
6262 event_location_to_string (b->location.get ()));
6263 }
6264 }
6265
6266 /* See breakpoint.h. */
6267
6268 bool fix_multi_location_breakpoint_output_globally = false;
6269
6270 static void
6271 print_one_breakpoint (struct breakpoint *b,
6272 struct bp_location **last_loc,
6273 int allflag)
6274 {
6275 struct ui_out *uiout = current_uiout;
6276 bool use_fixed_output
6277 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6278 || fix_multi_location_breakpoint_output_globally);
6279
6280 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6281 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
6282
6283 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6284 are outside. */
6285 if (!use_fixed_output)
6286 bkpt_tuple_emitter.reset ();
6287
6288 /* If this breakpoint has custom print function,
6289 it's already printed. Otherwise, print individual
6290 locations, if any. */
6291 if (b->ops == NULL
6292 || b->ops->print_one == NULL
6293 || allflag)
6294 {
6295 /* If breakpoint has a single location that is disabled, we
6296 print it as if it had several locations, since otherwise it's
6297 hard to represent "breakpoint enabled, location disabled"
6298 situation.
6299
6300 Note that while hardware watchpoints have several locations
6301 internally, that's not a property exposed to users.
6302
6303 Likewise, while catchpoints may be implemented with
6304 breakpoints (e.g., catch throw), that's not a property
6305 exposed to users. We do however display the internal
6306 breakpoint locations with "maint info breakpoints". */
6307 if (!is_hardware_watchpoint (b)
6308 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6309 || is_ada_exception_catchpoint (b))
6310 && (allflag
6311 || (b->loc && (b->loc->next || !b->loc->enabled))))
6312 {
6313 gdb::optional<ui_out_emit_list> locations_list;
6314
6315 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6316 MI record. For later versions, place breakpoint locations in a
6317 list. */
6318 if (uiout->is_mi_like_p () && use_fixed_output)
6319 locations_list.emplace (uiout, "locations");
6320
6321 int n = 1;
6322 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next, ++n)
6323 {
6324 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6325 print_one_breakpoint_location (b, loc, n, last_loc,
6326 allflag, allflag);
6327 }
6328 }
6329 }
6330 }
6331
6332 static int
6333 breakpoint_address_bits (struct breakpoint *b)
6334 {
6335 int print_address_bits = 0;
6336 struct bp_location *loc;
6337
6338 /* Software watchpoints that aren't watching memory don't have an
6339 address to print. */
6340 if (is_no_memory_software_watchpoint (b))
6341 return 0;
6342
6343 for (loc = b->loc; loc; loc = loc->next)
6344 {
6345 int addr_bit;
6346
6347 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6348 if (addr_bit > print_address_bits)
6349 print_address_bits = addr_bit;
6350 }
6351
6352 return print_address_bits;
6353 }
6354
6355 /* See breakpoint.h. */
6356
6357 void
6358 print_breakpoint (breakpoint *b)
6359 {
6360 struct bp_location *dummy_loc = NULL;
6361 print_one_breakpoint (b, &dummy_loc, 0);
6362 }
6363
6364 /* Return true if this breakpoint was set by the user, false if it is
6365 internal or momentary. */
6366
6367 int
6368 user_breakpoint_p (struct breakpoint *b)
6369 {
6370 return b->number > 0;
6371 }
6372
6373 /* See breakpoint.h. */
6374
6375 int
6376 pending_breakpoint_p (struct breakpoint *b)
6377 {
6378 return b->loc == NULL;
6379 }
6380
6381 /* Print information on breakpoints (including watchpoints and tracepoints).
6382
6383 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6384 understood by number_or_range_parser. Only breakpoints included in this
6385 list are then printed.
6386
6387 If SHOW_INTERNAL is true, print internal breakpoints.
6388
6389 If FILTER is non-NULL, call it on each breakpoint and only include the
6390 ones for which it returns true.
6391
6392 Return the total number of breakpoints listed. */
6393
6394 static int
6395 breakpoint_1 (const char *bp_num_list, bool show_internal,
6396 bool (*filter) (const struct breakpoint *))
6397 {
6398 struct breakpoint *b;
6399 struct bp_location *last_loc = NULL;
6400 int nr_printable_breakpoints;
6401 struct value_print_options opts;
6402 int print_address_bits = 0;
6403 int print_type_col_width = 14;
6404 struct ui_out *uiout = current_uiout;
6405
6406 get_user_print_options (&opts);
6407
6408 /* Compute the number of rows in the table, as well as the size
6409 required for address fields. */
6410 nr_printable_breakpoints = 0;
6411 ALL_BREAKPOINTS (b)
6412 {
6413 /* If we have a filter, only list the breakpoints it accepts. */
6414 if (filter && !filter (b))
6415 continue;
6416
6417 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6418 accept. Skip the others. */
6419 if (bp_num_list != NULL && *bp_num_list != '\0')
6420 {
6421 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6422 continue;
6423 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6424 continue;
6425 }
6426
6427 if (show_internal || user_breakpoint_p (b))
6428 {
6429 int addr_bit, type_len;
6430
6431 addr_bit = breakpoint_address_bits (b);
6432 if (addr_bit > print_address_bits)
6433 print_address_bits = addr_bit;
6434
6435 type_len = strlen (bptype_string (b->type));
6436 if (type_len > print_type_col_width)
6437 print_type_col_width = type_len;
6438
6439 nr_printable_breakpoints++;
6440 }
6441 }
6442
6443 {
6444 ui_out_emit_table table_emitter (uiout,
6445 opts.addressprint ? 6 : 5,
6446 nr_printable_breakpoints,
6447 "BreakpointTable");
6448
6449 if (nr_printable_breakpoints > 0)
6450 annotate_breakpoints_headers ();
6451 if (nr_printable_breakpoints > 0)
6452 annotate_field (0);
6453 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6454 if (nr_printable_breakpoints > 0)
6455 annotate_field (1);
6456 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6457 if (nr_printable_breakpoints > 0)
6458 annotate_field (2);
6459 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6460 if (nr_printable_breakpoints > 0)
6461 annotate_field (3);
6462 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6463 if (opts.addressprint)
6464 {
6465 if (nr_printable_breakpoints > 0)
6466 annotate_field (4);
6467 if (print_address_bits <= 32)
6468 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6469 else
6470 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6471 }
6472 if (nr_printable_breakpoints > 0)
6473 annotate_field (5);
6474 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6475 uiout->table_body ();
6476 if (nr_printable_breakpoints > 0)
6477 annotate_breakpoints_table ();
6478
6479 ALL_BREAKPOINTS (b)
6480 {
6481 QUIT;
6482 /* If we have a filter, only list the breakpoints it accepts. */
6483 if (filter && !filter (b))
6484 continue;
6485
6486 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6487 accept. Skip the others. */
6488
6489 if (bp_num_list != NULL && *bp_num_list != '\0')
6490 {
6491 if (show_internal) /* maintenance info breakpoint */
6492 {
6493 if (parse_and_eval_long (bp_num_list) != b->number)
6494 continue;
6495 }
6496 else /* all others */
6497 {
6498 if (!number_is_in_list (bp_num_list, b->number))
6499 continue;
6500 }
6501 }
6502 /* We only print out user settable breakpoints unless the
6503 show_internal is set. */
6504 if (show_internal || user_breakpoint_p (b))
6505 print_one_breakpoint (b, &last_loc, show_internal);
6506 }
6507 }
6508
6509 if (nr_printable_breakpoints == 0)
6510 {
6511 /* If there's a filter, let the caller decide how to report
6512 empty list. */
6513 if (!filter)
6514 {
6515 if (bp_num_list == NULL || *bp_num_list == '\0')
6516 uiout->message ("No breakpoints or watchpoints.\n");
6517 else
6518 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6519 bp_num_list);
6520 }
6521 }
6522 else
6523 {
6524 if (last_loc && !server_command)
6525 set_next_address (last_loc->gdbarch, last_loc->address);
6526 }
6527
6528 /* FIXME? Should this be moved up so that it is only called when
6529 there have been breakpoints? */
6530 annotate_breakpoints_table_end ();
6531
6532 return nr_printable_breakpoints;
6533 }
6534
6535 /* Display the value of default-collect in a way that is generally
6536 compatible with the breakpoint list. */
6537
6538 static void
6539 default_collect_info (void)
6540 {
6541 struct ui_out *uiout = current_uiout;
6542
6543 /* If it has no value (which is frequently the case), say nothing; a
6544 message like "No default-collect." gets in user's face when it's
6545 not wanted. */
6546 if (!*default_collect)
6547 return;
6548
6549 /* The following phrase lines up nicely with per-tracepoint collect
6550 actions. */
6551 uiout->text ("default collect ");
6552 uiout->field_string ("default-collect", default_collect);
6553 uiout->text (" \n");
6554 }
6555
6556 static void
6557 info_breakpoints_command (const char *args, int from_tty)
6558 {
6559 breakpoint_1 (args, false, NULL);
6560
6561 default_collect_info ();
6562 }
6563
6564 static void
6565 info_watchpoints_command (const char *args, int from_tty)
6566 {
6567 int num_printed = breakpoint_1 (args, false, is_watchpoint);
6568 struct ui_out *uiout = current_uiout;
6569
6570 if (num_printed == 0)
6571 {
6572 if (args == NULL || *args == '\0')
6573 uiout->message ("No watchpoints.\n");
6574 else
6575 uiout->message ("No watchpoint matching '%s'.\n", args);
6576 }
6577 }
6578
6579 static void
6580 maintenance_info_breakpoints (const char *args, int from_tty)
6581 {
6582 breakpoint_1 (args, true, NULL);
6583
6584 default_collect_info ();
6585 }
6586
6587 static int
6588 breakpoint_has_pc (struct breakpoint *b,
6589 struct program_space *pspace,
6590 CORE_ADDR pc, struct obj_section *section)
6591 {
6592 struct bp_location *bl = b->loc;
6593
6594 for (; bl; bl = bl->next)
6595 {
6596 if (bl->pspace == pspace
6597 && bl->address == pc
6598 && (!overlay_debugging || bl->section == section))
6599 return 1;
6600 }
6601 return 0;
6602 }
6603
6604 /* Print a message describing any user-breakpoints set at PC. This
6605 concerns with logical breakpoints, so we match program spaces, not
6606 address spaces. */
6607
6608 static void
6609 describe_other_breakpoints (struct gdbarch *gdbarch,
6610 struct program_space *pspace, CORE_ADDR pc,
6611 struct obj_section *section, int thread)
6612 {
6613 int others = 0;
6614 struct breakpoint *b;
6615
6616 ALL_BREAKPOINTS (b)
6617 others += (user_breakpoint_p (b)
6618 && breakpoint_has_pc (b, pspace, pc, section));
6619 if (others > 0)
6620 {
6621 if (others == 1)
6622 printf_filtered (_("Note: breakpoint "));
6623 else /* if (others == ???) */
6624 printf_filtered (_("Note: breakpoints "));
6625 ALL_BREAKPOINTS (b)
6626 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6627 {
6628 others--;
6629 printf_filtered ("%d", b->number);
6630 if (b->thread == -1 && thread != -1)
6631 printf_filtered (" (all threads)");
6632 else if (b->thread != -1)
6633 printf_filtered (" (thread %d)", b->thread);
6634 printf_filtered ("%s%s ",
6635 ((b->enable_state == bp_disabled
6636 || b->enable_state == bp_call_disabled)
6637 ? " (disabled)"
6638 : ""),
6639 (others > 1) ? ","
6640 : ((others == 1) ? " and" : ""));
6641 }
6642 current_uiout->message (_("also set at pc %ps.\n"),
6643 styled_string (address_style.style (),
6644 paddress (gdbarch, pc)));
6645 }
6646 }
6647 \f
6648
6649 /* Return true iff it is meaningful to use the address member of LOC.
6650 For some breakpoint types, the locations' address members are
6651 irrelevant and it makes no sense to attempt to compare them to
6652 other addresses (or use them for any other purpose either).
6653
6654 More specifically, software watchpoints and catchpoints that are
6655 not backed by breakpoints always have a zero valued location
6656 address and we don't want to mark breakpoints of any of these types
6657 to be a duplicate of an actual breakpoint location at address
6658 zero. */
6659
6660 static bool
6661 bl_address_is_meaningful (bp_location *loc)
6662 {
6663 return loc->loc_type != bp_loc_other;
6664 }
6665
6666 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6667 true if LOC1 and LOC2 represent the same watchpoint location. */
6668
6669 static int
6670 watchpoint_locations_match (struct bp_location *loc1,
6671 struct bp_location *loc2)
6672 {
6673 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6674 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6675
6676 /* Both of them must exist. */
6677 gdb_assert (w1 != NULL);
6678 gdb_assert (w2 != NULL);
6679
6680 /* If the target can evaluate the condition expression in hardware,
6681 then we we need to insert both watchpoints even if they are at
6682 the same place. Otherwise the watchpoint will only trigger when
6683 the condition of whichever watchpoint was inserted evaluates to
6684 true, not giving a chance for GDB to check the condition of the
6685 other watchpoint. */
6686 if ((w1->cond_exp
6687 && target_can_accel_watchpoint_condition (loc1->address,
6688 loc1->length,
6689 loc1->watchpoint_type,
6690 w1->cond_exp.get ()))
6691 || (w2->cond_exp
6692 && target_can_accel_watchpoint_condition (loc2->address,
6693 loc2->length,
6694 loc2->watchpoint_type,
6695 w2->cond_exp.get ())))
6696 return 0;
6697
6698 /* Note that this checks the owner's type, not the location's. In
6699 case the target does not support read watchpoints, but does
6700 support access watchpoints, we'll have bp_read_watchpoint
6701 watchpoints with hw_access locations. Those should be considered
6702 duplicates of hw_read locations. The hw_read locations will
6703 become hw_access locations later. */
6704 return (loc1->owner->type == loc2->owner->type
6705 && loc1->pspace->aspace == loc2->pspace->aspace
6706 && loc1->address == loc2->address
6707 && loc1->length == loc2->length);
6708 }
6709
6710 /* See breakpoint.h. */
6711
6712 int
6713 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6714 const address_space *aspace2, CORE_ADDR addr2)
6715 {
6716 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6717 || aspace1 == aspace2)
6718 && addr1 == addr2);
6719 }
6720
6721 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6722 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6723 matches ASPACE2. On targets that have global breakpoints, the address
6724 space doesn't really matter. */
6725
6726 static int
6727 breakpoint_address_match_range (const address_space *aspace1,
6728 CORE_ADDR addr1,
6729 int len1, const address_space *aspace2,
6730 CORE_ADDR addr2)
6731 {
6732 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6733 || aspace1 == aspace2)
6734 && addr2 >= addr1 && addr2 < addr1 + len1);
6735 }
6736
6737 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6738 a ranged breakpoint. In most targets, a match happens only if ASPACE
6739 matches the breakpoint's address space. On targets that have global
6740 breakpoints, the address space doesn't really matter. */
6741
6742 static int
6743 breakpoint_location_address_match (struct bp_location *bl,
6744 const address_space *aspace,
6745 CORE_ADDR addr)
6746 {
6747 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6748 aspace, addr)
6749 || (bl->length
6750 && breakpoint_address_match_range (bl->pspace->aspace,
6751 bl->address, bl->length,
6752 aspace, addr)));
6753 }
6754
6755 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6756 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6757 match happens only if ASPACE matches the breakpoint's address
6758 space. On targets that have global breakpoints, the address space
6759 doesn't really matter. */
6760
6761 static int
6762 breakpoint_location_address_range_overlap (struct bp_location *bl,
6763 const address_space *aspace,
6764 CORE_ADDR addr, int len)
6765 {
6766 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6767 || bl->pspace->aspace == aspace)
6768 {
6769 int bl_len = bl->length != 0 ? bl->length : 1;
6770
6771 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6772 return 1;
6773 }
6774 return 0;
6775 }
6776
6777 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6778 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6779 true, otherwise returns false. */
6780
6781 static int
6782 tracepoint_locations_match (struct bp_location *loc1,
6783 struct bp_location *loc2)
6784 {
6785 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6786 /* Since tracepoint locations are never duplicated with others', tracepoint
6787 locations at the same address of different tracepoints are regarded as
6788 different locations. */
6789 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6790 else
6791 return 0;
6792 }
6793
6794 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6795 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
6796 the same location. If SW_HW_BPS_MATCH is true, then software
6797 breakpoint locations and hardware breakpoint locations match,
6798 otherwise they don't. */
6799
6800 static int
6801 breakpoint_locations_match (struct bp_location *loc1,
6802 struct bp_location *loc2,
6803 bool sw_hw_bps_match)
6804 {
6805 int hw_point1, hw_point2;
6806
6807 /* Both of them must not be in moribund_locations. */
6808 gdb_assert (loc1->owner != NULL);
6809 gdb_assert (loc2->owner != NULL);
6810
6811 hw_point1 = is_hardware_watchpoint (loc1->owner);
6812 hw_point2 = is_hardware_watchpoint (loc2->owner);
6813
6814 if (hw_point1 != hw_point2)
6815 return 0;
6816 else if (hw_point1)
6817 return watchpoint_locations_match (loc1, loc2);
6818 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6819 return tracepoint_locations_match (loc1, loc2);
6820 else
6821 /* We compare bp_location.length in order to cover ranged
6822 breakpoints. Keep this in sync with
6823 bp_location_is_less_than. */
6824 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6825 loc2->pspace->aspace, loc2->address)
6826 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
6827 && loc1->length == loc2->length);
6828 }
6829
6830 static void
6831 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6832 int bnum, int have_bnum)
6833 {
6834 /* The longest string possibly returned by hex_string_custom
6835 is 50 chars. These must be at least that big for safety. */
6836 char astr1[64];
6837 char astr2[64];
6838
6839 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6840 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
6841 if (have_bnum)
6842 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
6843 bnum, astr1, astr2);
6844 else
6845 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
6846 }
6847
6848 /* Adjust a breakpoint's address to account for architectural
6849 constraints on breakpoint placement. Return the adjusted address.
6850 Note: Very few targets require this kind of adjustment. For most
6851 targets, this function is simply the identity function. */
6852
6853 static CORE_ADDR
6854 adjust_breakpoint_address (struct gdbarch *gdbarch,
6855 CORE_ADDR bpaddr, enum bptype bptype)
6856 {
6857 if (bptype == bp_watchpoint
6858 || bptype == bp_hardware_watchpoint
6859 || bptype == bp_read_watchpoint
6860 || bptype == bp_access_watchpoint
6861 || bptype == bp_catchpoint)
6862 {
6863 /* Watchpoints and the various bp_catch_* eventpoints should not
6864 have their addresses modified. */
6865 return bpaddr;
6866 }
6867 else if (bptype == bp_single_step)
6868 {
6869 /* Single-step breakpoints should not have their addresses
6870 modified. If there's any architectural constrain that
6871 applies to this address, then it should have already been
6872 taken into account when the breakpoint was created in the
6873 first place. If we didn't do this, stepping through e.g.,
6874 Thumb-2 IT blocks would break. */
6875 return bpaddr;
6876 }
6877 else
6878 {
6879 CORE_ADDR adjusted_bpaddr = bpaddr;
6880
6881 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
6882 {
6883 /* Some targets have architectural constraints on the placement
6884 of breakpoint instructions. Obtain the adjusted address. */
6885 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
6886 }
6887
6888 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
6889
6890 /* An adjusted breakpoint address can significantly alter
6891 a user's expectations. Print a warning if an adjustment
6892 is required. */
6893 if (adjusted_bpaddr != bpaddr)
6894 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6895
6896 return adjusted_bpaddr;
6897 }
6898 }
6899
6900 static bp_loc_type
6901 bp_location_from_bp_type (bptype type)
6902 {
6903 switch (type)
6904 {
6905 case bp_breakpoint:
6906 case bp_single_step:
6907 case bp_until:
6908 case bp_finish:
6909 case bp_longjmp:
6910 case bp_longjmp_resume:
6911 case bp_longjmp_call_dummy:
6912 case bp_exception:
6913 case bp_exception_resume:
6914 case bp_step_resume:
6915 case bp_hp_step_resume:
6916 case bp_watchpoint_scope:
6917 case bp_call_dummy:
6918 case bp_std_terminate:
6919 case bp_shlib_event:
6920 case bp_thread_event:
6921 case bp_overlay_event:
6922 case bp_jit_event:
6923 case bp_longjmp_master:
6924 case bp_std_terminate_master:
6925 case bp_exception_master:
6926 case bp_gnu_ifunc_resolver:
6927 case bp_gnu_ifunc_resolver_return:
6928 case bp_dprintf:
6929 return bp_loc_software_breakpoint;
6930 case bp_hardware_breakpoint:
6931 return bp_loc_hardware_breakpoint;
6932 case bp_hardware_watchpoint:
6933 case bp_read_watchpoint:
6934 case bp_access_watchpoint:
6935 return bp_loc_hardware_watchpoint;
6936 case bp_watchpoint:
6937 case bp_catchpoint:
6938 case bp_tracepoint:
6939 case bp_fast_tracepoint:
6940 case bp_static_tracepoint:
6941 return bp_loc_other;
6942 default:
6943 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
6944 }
6945 }
6946
6947 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
6948 {
6949 this->owner = owner;
6950 this->cond_bytecode = NULL;
6951 this->shlib_disabled = 0;
6952 this->enabled = 1;
6953
6954 this->loc_type = type;
6955
6956 if (this->loc_type == bp_loc_software_breakpoint
6957 || this->loc_type == bp_loc_hardware_breakpoint)
6958 mark_breakpoint_location_modified (this);
6959
6960 this->refc = 1;
6961 }
6962
6963 bp_location::bp_location (breakpoint *owner)
6964 : bp_location::bp_location (owner,
6965 bp_location_from_bp_type (owner->type))
6966 {
6967 }
6968
6969 /* Allocate a struct bp_location. */
6970
6971 static struct bp_location *
6972 allocate_bp_location (struct breakpoint *bpt)
6973 {
6974 return bpt->ops->allocate_location (bpt);
6975 }
6976
6977 static void
6978 free_bp_location (struct bp_location *loc)
6979 {
6980 delete loc;
6981 }
6982
6983 /* Increment reference count. */
6984
6985 static void
6986 incref_bp_location (struct bp_location *bl)
6987 {
6988 ++bl->refc;
6989 }
6990
6991 /* Decrement reference count. If the reference count reaches 0,
6992 destroy the bp_location. Sets *BLP to NULL. */
6993
6994 static void
6995 decref_bp_location (struct bp_location **blp)
6996 {
6997 gdb_assert ((*blp)->refc > 0);
6998
6999 if (--(*blp)->refc == 0)
7000 free_bp_location (*blp);
7001 *blp = NULL;
7002 }
7003
7004 /* Add breakpoint B at the end of the global breakpoint chain. */
7005
7006 static breakpoint *
7007 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7008 {
7009 struct breakpoint *b1;
7010 struct breakpoint *result = b.get ();
7011
7012 /* Add this breakpoint to the end of the chain so that a list of
7013 breakpoints will come out in order of increasing numbers. */
7014
7015 b1 = breakpoint_chain;
7016 if (b1 == 0)
7017 breakpoint_chain = b.release ();
7018 else
7019 {
7020 while (b1->next)
7021 b1 = b1->next;
7022 b1->next = b.release ();
7023 }
7024
7025 return result;
7026 }
7027
7028 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7029
7030 static void
7031 init_raw_breakpoint_without_location (struct breakpoint *b,
7032 struct gdbarch *gdbarch,
7033 enum bptype bptype,
7034 const struct breakpoint_ops *ops)
7035 {
7036 gdb_assert (ops != NULL);
7037
7038 b->ops = ops;
7039 b->type = bptype;
7040 b->gdbarch = gdbarch;
7041 b->language = current_language->la_language;
7042 b->input_radix = input_radix;
7043 b->related_breakpoint = b;
7044 }
7045
7046 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7047 that has type BPTYPE and has no locations as yet. */
7048
7049 static struct breakpoint *
7050 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7051 enum bptype bptype,
7052 const struct breakpoint_ops *ops)
7053 {
7054 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7055
7056 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7057 return add_to_breakpoint_chain (std::move (b));
7058 }
7059
7060 /* Initialize loc->function_name. */
7061
7062 static void
7063 set_breakpoint_location_function (struct bp_location *loc)
7064 {
7065 gdb_assert (loc->owner != NULL);
7066
7067 if (loc->owner->type == bp_breakpoint
7068 || loc->owner->type == bp_hardware_breakpoint
7069 || is_tracepoint (loc->owner))
7070 {
7071 const char *function_name;
7072
7073 if (loc->msymbol != NULL
7074 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
7075 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
7076 {
7077 struct breakpoint *b = loc->owner;
7078
7079 function_name = loc->msymbol->linkage_name ();
7080
7081 if (b->type == bp_breakpoint && b->loc == loc
7082 && loc->next == NULL && b->related_breakpoint == b)
7083 {
7084 /* Create only the whole new breakpoint of this type but do not
7085 mess more complicated breakpoints with multiple locations. */
7086 b->type = bp_gnu_ifunc_resolver;
7087 /* Remember the resolver's address for use by the return
7088 breakpoint. */
7089 loc->related_address = loc->address;
7090 }
7091 }
7092 else
7093 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7094
7095 if (function_name)
7096 loc->function_name = xstrdup (function_name);
7097 }
7098 }
7099
7100 /* Attempt to determine architecture of location identified by SAL. */
7101 struct gdbarch *
7102 get_sal_arch (struct symtab_and_line sal)
7103 {
7104 if (sal.section)
7105 return sal.section->objfile->arch ();
7106 if (sal.symtab)
7107 return SYMTAB_OBJFILE (sal.symtab)->arch ();
7108
7109 return NULL;
7110 }
7111
7112 /* Low level routine for partially initializing a breakpoint of type
7113 BPTYPE. The newly created breakpoint's address, section, source
7114 file name, and line number are provided by SAL.
7115
7116 It is expected that the caller will complete the initialization of
7117 the newly created breakpoint struct as well as output any status
7118 information regarding the creation of a new breakpoint. */
7119
7120 static void
7121 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7122 struct symtab_and_line sal, enum bptype bptype,
7123 const struct breakpoint_ops *ops)
7124 {
7125 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7126
7127 add_location_to_breakpoint (b, &sal);
7128
7129 if (bptype != bp_catchpoint)
7130 gdb_assert (sal.pspace != NULL);
7131
7132 /* Store the program space that was used to set the breakpoint,
7133 except for ordinary breakpoints, which are independent of the
7134 program space. */
7135 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7136 b->pspace = sal.pspace;
7137 }
7138
7139 /* set_raw_breakpoint is a low level routine for allocating and
7140 partially initializing a breakpoint of type BPTYPE. The newly
7141 created breakpoint's address, section, source file name, and line
7142 number are provided by SAL. The newly created and partially
7143 initialized breakpoint is added to the breakpoint chain and
7144 is also returned as the value of this function.
7145
7146 It is expected that the caller will complete the initialization of
7147 the newly created breakpoint struct as well as output any status
7148 information regarding the creation of a new breakpoint. In
7149 particular, set_raw_breakpoint does NOT set the breakpoint
7150 number! Care should be taken to not allow an error to occur
7151 prior to completing the initialization of the breakpoint. If this
7152 should happen, a bogus breakpoint will be left on the chain. */
7153
7154 struct breakpoint *
7155 set_raw_breakpoint (struct gdbarch *gdbarch,
7156 struct symtab_and_line sal, enum bptype bptype,
7157 const struct breakpoint_ops *ops)
7158 {
7159 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7160
7161 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7162 return add_to_breakpoint_chain (std::move (b));
7163 }
7164
7165 /* Call this routine when stepping and nexting to enable a breakpoint
7166 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7167 initiated the operation. */
7168
7169 void
7170 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7171 {
7172 struct breakpoint *b, *b_tmp;
7173 int thread = tp->global_num;
7174
7175 /* To avoid having to rescan all objfile symbols at every step,
7176 we maintain a list of continually-inserted but always disabled
7177 longjmp "master" breakpoints. Here, we simply create momentary
7178 clones of those and enable them for the requested thread. */
7179 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7180 if (b->pspace == current_program_space
7181 && (b->type == bp_longjmp_master
7182 || b->type == bp_exception_master))
7183 {
7184 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7185 struct breakpoint *clone;
7186
7187 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7188 after their removal. */
7189 clone = momentary_breakpoint_from_master (b, type,
7190 &momentary_breakpoint_ops, 1);
7191 clone->thread = thread;
7192 }
7193
7194 tp->initiating_frame = frame;
7195 }
7196
7197 /* Delete all longjmp breakpoints from THREAD. */
7198 void
7199 delete_longjmp_breakpoint (int thread)
7200 {
7201 struct breakpoint *b, *b_tmp;
7202
7203 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7204 if (b->type == bp_longjmp || b->type == bp_exception)
7205 {
7206 if (b->thread == thread)
7207 delete_breakpoint (b);
7208 }
7209 }
7210
7211 void
7212 delete_longjmp_breakpoint_at_next_stop (int thread)
7213 {
7214 struct breakpoint *b, *b_tmp;
7215
7216 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7217 if (b->type == bp_longjmp || b->type == bp_exception)
7218 {
7219 if (b->thread == thread)
7220 b->disposition = disp_del_at_next_stop;
7221 }
7222 }
7223
7224 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7225 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7226 pointer to any of them. Return NULL if this system cannot place longjmp
7227 breakpoints. */
7228
7229 struct breakpoint *
7230 set_longjmp_breakpoint_for_call_dummy (void)
7231 {
7232 struct breakpoint *b, *retval = NULL;
7233
7234 ALL_BREAKPOINTS (b)
7235 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7236 {
7237 struct breakpoint *new_b;
7238
7239 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7240 &momentary_breakpoint_ops,
7241 1);
7242 new_b->thread = inferior_thread ()->global_num;
7243
7244 /* Link NEW_B into the chain of RETVAL breakpoints. */
7245
7246 gdb_assert (new_b->related_breakpoint == new_b);
7247 if (retval == NULL)
7248 retval = new_b;
7249 new_b->related_breakpoint = retval;
7250 while (retval->related_breakpoint != new_b->related_breakpoint)
7251 retval = retval->related_breakpoint;
7252 retval->related_breakpoint = new_b;
7253 }
7254
7255 return retval;
7256 }
7257
7258 /* Verify all existing dummy frames and their associated breakpoints for
7259 TP. Remove those which can no longer be found in the current frame
7260 stack.
7261
7262 You should call this function only at places where it is safe to currently
7263 unwind the whole stack. Failed stack unwind would discard live dummy
7264 frames. */
7265
7266 void
7267 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7268 {
7269 struct breakpoint *b, *b_tmp;
7270
7271 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7272 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7273 {
7274 struct breakpoint *dummy_b = b->related_breakpoint;
7275
7276 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7277 dummy_b = dummy_b->related_breakpoint;
7278 if (dummy_b->type != bp_call_dummy
7279 || frame_find_by_id (dummy_b->frame_id) != NULL)
7280 continue;
7281
7282 dummy_frame_discard (dummy_b->frame_id, tp);
7283
7284 while (b->related_breakpoint != b)
7285 {
7286 if (b_tmp == b->related_breakpoint)
7287 b_tmp = b->related_breakpoint->next;
7288 delete_breakpoint (b->related_breakpoint);
7289 }
7290 delete_breakpoint (b);
7291 }
7292 }
7293
7294 void
7295 enable_overlay_breakpoints (void)
7296 {
7297 struct breakpoint *b;
7298
7299 ALL_BREAKPOINTS (b)
7300 if (b->type == bp_overlay_event)
7301 {
7302 b->enable_state = bp_enabled;
7303 update_global_location_list (UGLL_MAY_INSERT);
7304 overlay_events_enabled = 1;
7305 }
7306 }
7307
7308 void
7309 disable_overlay_breakpoints (void)
7310 {
7311 struct breakpoint *b;
7312
7313 ALL_BREAKPOINTS (b)
7314 if (b->type == bp_overlay_event)
7315 {
7316 b->enable_state = bp_disabled;
7317 update_global_location_list (UGLL_DONT_INSERT);
7318 overlay_events_enabled = 0;
7319 }
7320 }
7321
7322 /* Set an active std::terminate breakpoint for each std::terminate
7323 master breakpoint. */
7324 void
7325 set_std_terminate_breakpoint (void)
7326 {
7327 struct breakpoint *b, *b_tmp;
7328
7329 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7330 if (b->pspace == current_program_space
7331 && b->type == bp_std_terminate_master)
7332 {
7333 momentary_breakpoint_from_master (b, bp_std_terminate,
7334 &momentary_breakpoint_ops, 1);
7335 }
7336 }
7337
7338 /* Delete all the std::terminate breakpoints. */
7339 void
7340 delete_std_terminate_breakpoint (void)
7341 {
7342 struct breakpoint *b, *b_tmp;
7343
7344 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7345 if (b->type == bp_std_terminate)
7346 delete_breakpoint (b);
7347 }
7348
7349 struct breakpoint *
7350 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7351 {
7352 struct breakpoint *b;
7353
7354 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7355 &internal_breakpoint_ops);
7356
7357 b->enable_state = bp_enabled;
7358 /* location has to be used or breakpoint_re_set will delete me. */
7359 b->location = new_address_location (b->loc->address, NULL, 0);
7360
7361 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7362
7363 return b;
7364 }
7365
7366 struct lang_and_radix
7367 {
7368 enum language lang;
7369 int radix;
7370 };
7371
7372 /* Create a breakpoint for JIT code registration and unregistration. */
7373
7374 struct breakpoint *
7375 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7376 {
7377 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7378 &internal_breakpoint_ops);
7379 }
7380
7381 /* Remove JIT code registration and unregistration breakpoint(s). */
7382
7383 void
7384 remove_jit_event_breakpoints (void)
7385 {
7386 struct breakpoint *b, *b_tmp;
7387
7388 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7389 if (b->type == bp_jit_event
7390 && b->loc->pspace == current_program_space)
7391 delete_breakpoint (b);
7392 }
7393
7394 void
7395 remove_solib_event_breakpoints (void)
7396 {
7397 struct breakpoint *b, *b_tmp;
7398
7399 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7400 if (b->type == bp_shlib_event
7401 && b->loc->pspace == current_program_space)
7402 delete_breakpoint (b);
7403 }
7404
7405 /* See breakpoint.h. */
7406
7407 void
7408 remove_solib_event_breakpoints_at_next_stop (void)
7409 {
7410 struct breakpoint *b, *b_tmp;
7411
7412 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7413 if (b->type == bp_shlib_event
7414 && b->loc->pspace == current_program_space)
7415 b->disposition = disp_del_at_next_stop;
7416 }
7417
7418 /* Helper for create_solib_event_breakpoint /
7419 create_and_insert_solib_event_breakpoint. Allows specifying which
7420 INSERT_MODE to pass through to update_global_location_list. */
7421
7422 static struct breakpoint *
7423 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7424 enum ugll_insert_mode insert_mode)
7425 {
7426 struct breakpoint *b;
7427
7428 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7429 &internal_breakpoint_ops);
7430 update_global_location_list_nothrow (insert_mode);
7431 return b;
7432 }
7433
7434 struct breakpoint *
7435 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7436 {
7437 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7438 }
7439
7440 /* See breakpoint.h. */
7441
7442 struct breakpoint *
7443 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7444 {
7445 struct breakpoint *b;
7446
7447 /* Explicitly tell update_global_location_list to insert
7448 locations. */
7449 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7450 if (!b->loc->inserted)
7451 {
7452 delete_breakpoint (b);
7453 return NULL;
7454 }
7455 return b;
7456 }
7457
7458 /* Disable any breakpoints that are on code in shared libraries. Only
7459 apply to enabled breakpoints, disabled ones can just stay disabled. */
7460
7461 void
7462 disable_breakpoints_in_shlibs (void)
7463 {
7464 struct bp_location *loc, **locp_tmp;
7465
7466 ALL_BP_LOCATIONS (loc, locp_tmp)
7467 {
7468 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7469 struct breakpoint *b = loc->owner;
7470
7471 /* We apply the check to all breakpoints, including disabled for
7472 those with loc->duplicate set. This is so that when breakpoint
7473 becomes enabled, or the duplicate is removed, gdb will try to
7474 insert all breakpoints. If we don't set shlib_disabled here,
7475 we'll try to insert those breakpoints and fail. */
7476 if (((b->type == bp_breakpoint)
7477 || (b->type == bp_jit_event)
7478 || (b->type == bp_hardware_breakpoint)
7479 || (is_tracepoint (b)))
7480 && loc->pspace == current_program_space
7481 && !loc->shlib_disabled
7482 && solib_name_from_address (loc->pspace, loc->address)
7483 )
7484 {
7485 loc->shlib_disabled = 1;
7486 }
7487 }
7488 }
7489
7490 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7491 notification of unloaded_shlib. Only apply to enabled breakpoints,
7492 disabled ones can just stay disabled. */
7493
7494 static void
7495 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7496 {
7497 struct bp_location *loc, **locp_tmp;
7498 int disabled_shlib_breaks = 0;
7499
7500 ALL_BP_LOCATIONS (loc, locp_tmp)
7501 {
7502 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7503 struct breakpoint *b = loc->owner;
7504
7505 if (solib->pspace == loc->pspace
7506 && !loc->shlib_disabled
7507 && (((b->type == bp_breakpoint
7508 || b->type == bp_jit_event
7509 || b->type == bp_hardware_breakpoint)
7510 && (loc->loc_type == bp_loc_hardware_breakpoint
7511 || loc->loc_type == bp_loc_software_breakpoint))
7512 || is_tracepoint (b))
7513 && solib_contains_address_p (solib, loc->address))
7514 {
7515 loc->shlib_disabled = 1;
7516 /* At this point, we cannot rely on remove_breakpoint
7517 succeeding so we must mark the breakpoint as not inserted
7518 to prevent future errors occurring in remove_breakpoints. */
7519 loc->inserted = 0;
7520
7521 /* This may cause duplicate notifications for the same breakpoint. */
7522 gdb::observers::breakpoint_modified.notify (b);
7523
7524 if (!disabled_shlib_breaks)
7525 {
7526 target_terminal::ours_for_output ();
7527 warning (_("Temporarily disabling breakpoints "
7528 "for unloaded shared library \"%s\""),
7529 solib->so_name);
7530 }
7531 disabled_shlib_breaks = 1;
7532 }
7533 }
7534 }
7535
7536 /* Disable any breakpoints and tracepoints in OBJFILE upon
7537 notification of free_objfile. Only apply to enabled breakpoints,
7538 disabled ones can just stay disabled. */
7539
7540 static void
7541 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7542 {
7543 struct breakpoint *b;
7544
7545 if (objfile == NULL)
7546 return;
7547
7548 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7549 managed by the user with add-symbol-file/remove-symbol-file.
7550 Similarly to how breakpoints in shared libraries are handled in
7551 response to "nosharedlibrary", mark breakpoints in such modules
7552 shlib_disabled so they end up uninserted on the next global
7553 location list update. Shared libraries not loaded by the user
7554 aren't handled here -- they're already handled in
7555 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7556 solib_unloaded observer. We skip objfiles that are not
7557 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7558 main objfile). */
7559 if ((objfile->flags & OBJF_SHARED) == 0
7560 || (objfile->flags & OBJF_USERLOADED) == 0)
7561 return;
7562
7563 ALL_BREAKPOINTS (b)
7564 {
7565 struct bp_location *loc;
7566 int bp_modified = 0;
7567
7568 if (!is_breakpoint (b) && !is_tracepoint (b))
7569 continue;
7570
7571 for (loc = b->loc; loc != NULL; loc = loc->next)
7572 {
7573 CORE_ADDR loc_addr = loc->address;
7574
7575 if (loc->loc_type != bp_loc_hardware_breakpoint
7576 && loc->loc_type != bp_loc_software_breakpoint)
7577 continue;
7578
7579 if (loc->shlib_disabled != 0)
7580 continue;
7581
7582 if (objfile->pspace != loc->pspace)
7583 continue;
7584
7585 if (loc->loc_type != bp_loc_hardware_breakpoint
7586 && loc->loc_type != bp_loc_software_breakpoint)
7587 continue;
7588
7589 if (is_addr_in_objfile (loc_addr, objfile))
7590 {
7591 loc->shlib_disabled = 1;
7592 /* At this point, we don't know whether the object was
7593 unmapped from the inferior or not, so leave the
7594 inserted flag alone. We'll handle failure to
7595 uninsert quietly, in case the object was indeed
7596 unmapped. */
7597
7598 mark_breakpoint_location_modified (loc);
7599
7600 bp_modified = 1;
7601 }
7602 }
7603
7604 if (bp_modified)
7605 gdb::observers::breakpoint_modified.notify (b);
7606 }
7607 }
7608
7609 /* FORK & VFORK catchpoints. */
7610
7611 /* An instance of this type is used to represent a fork or vfork
7612 catchpoint. A breakpoint is really of this type iff its ops pointer points
7613 to CATCH_FORK_BREAKPOINT_OPS. */
7614
7615 struct fork_catchpoint : public breakpoint
7616 {
7617 /* Process id of a child process whose forking triggered this
7618 catchpoint. This field is only valid immediately after this
7619 catchpoint has triggered. */
7620 ptid_t forked_inferior_pid;
7621 };
7622
7623 /* Implement the "insert" breakpoint_ops method for fork
7624 catchpoints. */
7625
7626 static int
7627 insert_catch_fork (struct bp_location *bl)
7628 {
7629 return target_insert_fork_catchpoint (inferior_ptid.pid ());
7630 }
7631
7632 /* Implement the "remove" breakpoint_ops method for fork
7633 catchpoints. */
7634
7635 static int
7636 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
7637 {
7638 return target_remove_fork_catchpoint (inferior_ptid.pid ());
7639 }
7640
7641 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7642 catchpoints. */
7643
7644 static int
7645 breakpoint_hit_catch_fork (const struct bp_location *bl,
7646 const address_space *aspace, CORE_ADDR bp_addr,
7647 const struct target_waitstatus *ws)
7648 {
7649 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7650
7651 if (ws->kind != TARGET_WAITKIND_FORKED)
7652 return 0;
7653
7654 c->forked_inferior_pid = ws->value.related_pid;
7655 return 1;
7656 }
7657
7658 /* Implement the "print_it" breakpoint_ops method for fork
7659 catchpoints. */
7660
7661 static enum print_stop_action
7662 print_it_catch_fork (bpstat bs)
7663 {
7664 struct ui_out *uiout = current_uiout;
7665 struct breakpoint *b = bs->breakpoint_at;
7666 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7667
7668 annotate_catchpoint (b->number);
7669 maybe_print_thread_hit_breakpoint (uiout);
7670 if (b->disposition == disp_del)
7671 uiout->text ("Temporary catchpoint ");
7672 else
7673 uiout->text ("Catchpoint ");
7674 if (uiout->is_mi_like_p ())
7675 {
7676 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7677 uiout->field_string ("disp", bpdisp_text (b->disposition));
7678 }
7679 uiout->field_signed ("bkptno", b->number);
7680 uiout->text (" (forked process ");
7681 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7682 uiout->text ("), ");
7683 return PRINT_SRC_AND_LOC;
7684 }
7685
7686 /* Implement the "print_one" breakpoint_ops method for fork
7687 catchpoints. */
7688
7689 static void
7690 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7691 {
7692 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7693 struct value_print_options opts;
7694 struct ui_out *uiout = current_uiout;
7695
7696 get_user_print_options (&opts);
7697
7698 /* Field 4, the address, is omitted (which makes the columns not
7699 line up too nicely with the headers, but the effect is relatively
7700 readable). */
7701 if (opts.addressprint)
7702 uiout->field_skip ("addr");
7703 annotate_field (5);
7704 uiout->text ("fork");
7705 if (c->forked_inferior_pid != null_ptid)
7706 {
7707 uiout->text (", process ");
7708 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7709 uiout->spaces (1);
7710 }
7711
7712 if (uiout->is_mi_like_p ())
7713 uiout->field_string ("catch-type", "fork");
7714 }
7715
7716 /* Implement the "print_mention" breakpoint_ops method for fork
7717 catchpoints. */
7718
7719 static void
7720 print_mention_catch_fork (struct breakpoint *b)
7721 {
7722 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7723 }
7724
7725 /* Implement the "print_recreate" breakpoint_ops method for fork
7726 catchpoints. */
7727
7728 static void
7729 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7730 {
7731 fprintf_unfiltered (fp, "catch fork");
7732 print_recreate_thread (b, fp);
7733 }
7734
7735 /* The breakpoint_ops structure to be used in fork catchpoints. */
7736
7737 static struct breakpoint_ops catch_fork_breakpoint_ops;
7738
7739 /* Implement the "insert" breakpoint_ops method for vfork
7740 catchpoints. */
7741
7742 static int
7743 insert_catch_vfork (struct bp_location *bl)
7744 {
7745 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
7746 }
7747
7748 /* Implement the "remove" breakpoint_ops method for vfork
7749 catchpoints. */
7750
7751 static int
7752 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
7753 {
7754 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
7755 }
7756
7757 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7758 catchpoints. */
7759
7760 static int
7761 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7762 const address_space *aspace, CORE_ADDR bp_addr,
7763 const struct target_waitstatus *ws)
7764 {
7765 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7766
7767 if (ws->kind != TARGET_WAITKIND_VFORKED)
7768 return 0;
7769
7770 c->forked_inferior_pid = ws->value.related_pid;
7771 return 1;
7772 }
7773
7774 /* Implement the "print_it" breakpoint_ops method for vfork
7775 catchpoints. */
7776
7777 static enum print_stop_action
7778 print_it_catch_vfork (bpstat bs)
7779 {
7780 struct ui_out *uiout = current_uiout;
7781 struct breakpoint *b = bs->breakpoint_at;
7782 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7783
7784 annotate_catchpoint (b->number);
7785 maybe_print_thread_hit_breakpoint (uiout);
7786 if (b->disposition == disp_del)
7787 uiout->text ("Temporary catchpoint ");
7788 else
7789 uiout->text ("Catchpoint ");
7790 if (uiout->is_mi_like_p ())
7791 {
7792 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7793 uiout->field_string ("disp", bpdisp_text (b->disposition));
7794 }
7795 uiout->field_signed ("bkptno", b->number);
7796 uiout->text (" (vforked process ");
7797 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7798 uiout->text ("), ");
7799 return PRINT_SRC_AND_LOC;
7800 }
7801
7802 /* Implement the "print_one" breakpoint_ops method for vfork
7803 catchpoints. */
7804
7805 static void
7806 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7807 {
7808 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7809 struct value_print_options opts;
7810 struct ui_out *uiout = current_uiout;
7811
7812 get_user_print_options (&opts);
7813 /* Field 4, the address, is omitted (which makes the columns not
7814 line up too nicely with the headers, but the effect is relatively
7815 readable). */
7816 if (opts.addressprint)
7817 uiout->field_skip ("addr");
7818 annotate_field (5);
7819 uiout->text ("vfork");
7820 if (c->forked_inferior_pid != null_ptid)
7821 {
7822 uiout->text (", process ");
7823 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7824 uiout->spaces (1);
7825 }
7826
7827 if (uiout->is_mi_like_p ())
7828 uiout->field_string ("catch-type", "vfork");
7829 }
7830
7831 /* Implement the "print_mention" breakpoint_ops method for vfork
7832 catchpoints. */
7833
7834 static void
7835 print_mention_catch_vfork (struct breakpoint *b)
7836 {
7837 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7838 }
7839
7840 /* Implement the "print_recreate" breakpoint_ops method for vfork
7841 catchpoints. */
7842
7843 static void
7844 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7845 {
7846 fprintf_unfiltered (fp, "catch vfork");
7847 print_recreate_thread (b, fp);
7848 }
7849
7850 /* The breakpoint_ops structure to be used in vfork catchpoints. */
7851
7852 static struct breakpoint_ops catch_vfork_breakpoint_ops;
7853
7854 /* An instance of this type is used to represent an solib catchpoint.
7855 A breakpoint is really of this type iff its ops pointer points to
7856 CATCH_SOLIB_BREAKPOINT_OPS. */
7857
7858 struct solib_catchpoint : public breakpoint
7859 {
7860 ~solib_catchpoint () override;
7861
7862 /* True for "catch load", false for "catch unload". */
7863 unsigned char is_load;
7864
7865 /* Regular expression to match, if any. COMPILED is only valid when
7866 REGEX is non-NULL. */
7867 char *regex;
7868 std::unique_ptr<compiled_regex> compiled;
7869 };
7870
7871 solib_catchpoint::~solib_catchpoint ()
7872 {
7873 xfree (this->regex);
7874 }
7875
7876 static int
7877 insert_catch_solib (struct bp_location *ignore)
7878 {
7879 return 0;
7880 }
7881
7882 static int
7883 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
7884 {
7885 return 0;
7886 }
7887
7888 static int
7889 breakpoint_hit_catch_solib (const struct bp_location *bl,
7890 const address_space *aspace,
7891 CORE_ADDR bp_addr,
7892 const struct target_waitstatus *ws)
7893 {
7894 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7895 struct breakpoint *other;
7896
7897 if (ws->kind == TARGET_WAITKIND_LOADED)
7898 return 1;
7899
7900 ALL_BREAKPOINTS (other)
7901 {
7902 struct bp_location *other_bl;
7903
7904 if (other == bl->owner)
7905 continue;
7906
7907 if (other->type != bp_shlib_event)
7908 continue;
7909
7910 if (self->pspace != NULL && other->pspace != self->pspace)
7911 continue;
7912
7913 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
7914 {
7915 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7916 return 1;
7917 }
7918 }
7919
7920 return 0;
7921 }
7922
7923 static void
7924 check_status_catch_solib (struct bpstats *bs)
7925 {
7926 struct solib_catchpoint *self
7927 = (struct solib_catchpoint *) bs->breakpoint_at;
7928
7929 if (self->is_load)
7930 {
7931 for (so_list *iter : current_program_space->added_solibs)
7932 {
7933 if (!self->regex
7934 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
7935 return;
7936 }
7937 }
7938 else
7939 {
7940 for (const std::string &iter : current_program_space->deleted_solibs)
7941 {
7942 if (!self->regex
7943 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
7944 return;
7945 }
7946 }
7947
7948 bs->stop = 0;
7949 bs->print_it = print_it_noop;
7950 }
7951
7952 static enum print_stop_action
7953 print_it_catch_solib (bpstat bs)
7954 {
7955 struct breakpoint *b = bs->breakpoint_at;
7956 struct ui_out *uiout = current_uiout;
7957
7958 annotate_catchpoint (b->number);
7959 maybe_print_thread_hit_breakpoint (uiout);
7960 if (b->disposition == disp_del)
7961 uiout->text ("Temporary catchpoint ");
7962 else
7963 uiout->text ("Catchpoint ");
7964 uiout->field_signed ("bkptno", b->number);
7965 uiout->text ("\n");
7966 if (uiout->is_mi_like_p ())
7967 uiout->field_string ("disp", bpdisp_text (b->disposition));
7968 print_solib_event (1);
7969 return PRINT_SRC_AND_LOC;
7970 }
7971
7972 static void
7973 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
7974 {
7975 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7976 struct value_print_options opts;
7977 struct ui_out *uiout = current_uiout;
7978
7979 get_user_print_options (&opts);
7980 /* Field 4, the address, is omitted (which makes the columns not
7981 line up too nicely with the headers, but the effect is relatively
7982 readable). */
7983 if (opts.addressprint)
7984 {
7985 annotate_field (4);
7986 uiout->field_skip ("addr");
7987 }
7988
7989 std::string msg;
7990 annotate_field (5);
7991 if (self->is_load)
7992 {
7993 if (self->regex)
7994 msg = string_printf (_("load of library matching %s"), self->regex);
7995 else
7996 msg = _("load of library");
7997 }
7998 else
7999 {
8000 if (self->regex)
8001 msg = string_printf (_("unload of library matching %s"), self->regex);
8002 else
8003 msg = _("unload of library");
8004 }
8005 uiout->field_string ("what", msg);
8006
8007 if (uiout->is_mi_like_p ())
8008 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8009 }
8010
8011 static void
8012 print_mention_catch_solib (struct breakpoint *b)
8013 {
8014 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8015
8016 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8017 self->is_load ? "load" : "unload");
8018 }
8019
8020 static void
8021 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8022 {
8023 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8024
8025 fprintf_unfiltered (fp, "%s %s",
8026 b->disposition == disp_del ? "tcatch" : "catch",
8027 self->is_load ? "load" : "unload");
8028 if (self->regex)
8029 fprintf_unfiltered (fp, " %s", self->regex);
8030 fprintf_unfiltered (fp, "\n");
8031 }
8032
8033 static struct breakpoint_ops catch_solib_breakpoint_ops;
8034
8035 /* Shared helper function (MI and CLI) for creating and installing
8036 a shared object event catchpoint. If IS_LOAD is non-zero then
8037 the events to be caught are load events, otherwise they are
8038 unload events. If IS_TEMP is non-zero the catchpoint is a
8039 temporary one. If ENABLED is non-zero the catchpoint is
8040 created in an enabled state. */
8041
8042 void
8043 add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
8044 {
8045 struct gdbarch *gdbarch = get_current_arch ();
8046
8047 if (!arg)
8048 arg = "";
8049 arg = skip_spaces (arg);
8050
8051 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
8052
8053 if (*arg != '\0')
8054 {
8055 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8056 _("Invalid regexp")));
8057 c->regex = xstrdup (arg);
8058 }
8059
8060 c->is_load = is_load;
8061 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
8062 &catch_solib_breakpoint_ops);
8063
8064 c->enable_state = enabled ? bp_enabled : bp_disabled;
8065
8066 install_breakpoint (0, std::move (c), 1);
8067 }
8068
8069 /* A helper function that does all the work for "catch load" and
8070 "catch unload". */
8071
8072 static void
8073 catch_load_or_unload (const char *arg, int from_tty, int is_load,
8074 struct cmd_list_element *command)
8075 {
8076 int tempflag;
8077 const int enabled = 1;
8078
8079 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8080
8081 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8082 }
8083
8084 static void
8085 catch_load_command_1 (const char *arg, int from_tty,
8086 struct cmd_list_element *command)
8087 {
8088 catch_load_or_unload (arg, from_tty, 1, command);
8089 }
8090
8091 static void
8092 catch_unload_command_1 (const char *arg, int from_tty,
8093 struct cmd_list_element *command)
8094 {
8095 catch_load_or_unload (arg, from_tty, 0, command);
8096 }
8097
8098 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8099 is non-zero, then make the breakpoint temporary. If COND_STRING is
8100 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8101 the breakpoint_ops structure associated to the catchpoint. */
8102
8103 void
8104 init_catchpoint (struct breakpoint *b,
8105 struct gdbarch *gdbarch, int tempflag,
8106 const char *cond_string,
8107 const struct breakpoint_ops *ops)
8108 {
8109 symtab_and_line sal;
8110 sal.pspace = current_program_space;
8111
8112 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8113
8114 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8115 b->disposition = tempflag ? disp_del : disp_donttouch;
8116 }
8117
8118 void
8119 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8120 {
8121 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8122 set_breakpoint_number (internal, b);
8123 if (is_tracepoint (b))
8124 set_tracepoint_count (breakpoint_count);
8125 if (!internal)
8126 mention (b);
8127 gdb::observers::breakpoint_created.notify (b);
8128
8129 if (update_gll)
8130 update_global_location_list (UGLL_MAY_INSERT);
8131 }
8132
8133 static void
8134 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8135 int tempflag, const char *cond_string,
8136 const struct breakpoint_ops *ops)
8137 {
8138 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
8139
8140 init_catchpoint (c.get (), gdbarch, tempflag, cond_string, ops);
8141
8142 c->forked_inferior_pid = null_ptid;
8143
8144 install_breakpoint (0, std::move (c), 1);
8145 }
8146
8147 /* Exec catchpoints. */
8148
8149 /* An instance of this type is used to represent an exec catchpoint.
8150 A breakpoint is really of this type iff its ops pointer points to
8151 CATCH_EXEC_BREAKPOINT_OPS. */
8152
8153 struct exec_catchpoint : public breakpoint
8154 {
8155 ~exec_catchpoint () override;
8156
8157 /* Filename of a program whose exec triggered this catchpoint.
8158 This field is only valid immediately after this catchpoint has
8159 triggered. */
8160 char *exec_pathname;
8161 };
8162
8163 /* Exec catchpoint destructor. */
8164
8165 exec_catchpoint::~exec_catchpoint ()
8166 {
8167 xfree (this->exec_pathname);
8168 }
8169
8170 static int
8171 insert_catch_exec (struct bp_location *bl)
8172 {
8173 return target_insert_exec_catchpoint (inferior_ptid.pid ());
8174 }
8175
8176 static int
8177 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8178 {
8179 return target_remove_exec_catchpoint (inferior_ptid.pid ());
8180 }
8181
8182 static int
8183 breakpoint_hit_catch_exec (const struct bp_location *bl,
8184 const address_space *aspace, CORE_ADDR bp_addr,
8185 const struct target_waitstatus *ws)
8186 {
8187 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8188
8189 if (ws->kind != TARGET_WAITKIND_EXECD)
8190 return 0;
8191
8192 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8193 return 1;
8194 }
8195
8196 static enum print_stop_action
8197 print_it_catch_exec (bpstat bs)
8198 {
8199 struct ui_out *uiout = current_uiout;
8200 struct breakpoint *b = bs->breakpoint_at;
8201 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8202
8203 annotate_catchpoint (b->number);
8204 maybe_print_thread_hit_breakpoint (uiout);
8205 if (b->disposition == disp_del)
8206 uiout->text ("Temporary catchpoint ");
8207 else
8208 uiout->text ("Catchpoint ");
8209 if (uiout->is_mi_like_p ())
8210 {
8211 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8212 uiout->field_string ("disp", bpdisp_text (b->disposition));
8213 }
8214 uiout->field_signed ("bkptno", b->number);
8215 uiout->text (" (exec'd ");
8216 uiout->field_string ("new-exec", c->exec_pathname);
8217 uiout->text ("), ");
8218
8219 return PRINT_SRC_AND_LOC;
8220 }
8221
8222 static void
8223 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8224 {
8225 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8226 struct value_print_options opts;
8227 struct ui_out *uiout = current_uiout;
8228
8229 get_user_print_options (&opts);
8230
8231 /* Field 4, the address, is omitted (which makes the columns
8232 not line up too nicely with the headers, but the effect
8233 is relatively readable). */
8234 if (opts.addressprint)
8235 uiout->field_skip ("addr");
8236 annotate_field (5);
8237 uiout->text ("exec");
8238 if (c->exec_pathname != NULL)
8239 {
8240 uiout->text (", program \"");
8241 uiout->field_string ("what", c->exec_pathname);
8242 uiout->text ("\" ");
8243 }
8244
8245 if (uiout->is_mi_like_p ())
8246 uiout->field_string ("catch-type", "exec");
8247 }
8248
8249 static void
8250 print_mention_catch_exec (struct breakpoint *b)
8251 {
8252 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8253 }
8254
8255 /* Implement the "print_recreate" breakpoint_ops method for exec
8256 catchpoints. */
8257
8258 static void
8259 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8260 {
8261 fprintf_unfiltered (fp, "catch exec");
8262 print_recreate_thread (b, fp);
8263 }
8264
8265 static struct breakpoint_ops catch_exec_breakpoint_ops;
8266
8267 static int
8268 hw_breakpoint_used_count (void)
8269 {
8270 int i = 0;
8271 struct breakpoint *b;
8272 struct bp_location *bl;
8273
8274 ALL_BREAKPOINTS (b)
8275 {
8276 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8277 for (bl = b->loc; bl; bl = bl->next)
8278 {
8279 /* Special types of hardware breakpoints may use more than
8280 one register. */
8281 i += b->ops->resources_needed (bl);
8282 }
8283 }
8284
8285 return i;
8286 }
8287
8288 /* Returns the resources B would use if it were a hardware
8289 watchpoint. */
8290
8291 static int
8292 hw_watchpoint_use_count (struct breakpoint *b)
8293 {
8294 int i = 0;
8295 struct bp_location *bl;
8296
8297 if (!breakpoint_enabled (b))
8298 return 0;
8299
8300 for (bl = b->loc; bl; bl = bl->next)
8301 {
8302 /* Special types of hardware watchpoints may use more than
8303 one register. */
8304 i += b->ops->resources_needed (bl);
8305 }
8306
8307 return i;
8308 }
8309
8310 /* Returns the sum the used resources of all hardware watchpoints of
8311 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8312 the sum of the used resources of all hardware watchpoints of other
8313 types _not_ TYPE. */
8314
8315 static int
8316 hw_watchpoint_used_count_others (struct breakpoint *except,
8317 enum bptype type, int *other_type_used)
8318 {
8319 int i = 0;
8320 struct breakpoint *b;
8321
8322 *other_type_used = 0;
8323 ALL_BREAKPOINTS (b)
8324 {
8325 if (b == except)
8326 continue;
8327 if (!breakpoint_enabled (b))
8328 continue;
8329
8330 if (b->type == type)
8331 i += hw_watchpoint_use_count (b);
8332 else if (is_hardware_watchpoint (b))
8333 *other_type_used = 1;
8334 }
8335
8336 return i;
8337 }
8338
8339 void
8340 disable_watchpoints_before_interactive_call_start (void)
8341 {
8342 struct breakpoint *b;
8343
8344 ALL_BREAKPOINTS (b)
8345 {
8346 if (is_watchpoint (b) && breakpoint_enabled (b))
8347 {
8348 b->enable_state = bp_call_disabled;
8349 update_global_location_list (UGLL_DONT_INSERT);
8350 }
8351 }
8352 }
8353
8354 void
8355 enable_watchpoints_after_interactive_call_stop (void)
8356 {
8357 struct breakpoint *b;
8358
8359 ALL_BREAKPOINTS (b)
8360 {
8361 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8362 {
8363 b->enable_state = bp_enabled;
8364 update_global_location_list (UGLL_MAY_INSERT);
8365 }
8366 }
8367 }
8368
8369 void
8370 disable_breakpoints_before_startup (void)
8371 {
8372 current_program_space->executing_startup = 1;
8373 update_global_location_list (UGLL_DONT_INSERT);
8374 }
8375
8376 void
8377 enable_breakpoints_after_startup (void)
8378 {
8379 current_program_space->executing_startup = 0;
8380 breakpoint_re_set ();
8381 }
8382
8383 /* Create a new single-step breakpoint for thread THREAD, with no
8384 locations. */
8385
8386 static struct breakpoint *
8387 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8388 {
8389 std::unique_ptr<breakpoint> b (new breakpoint ());
8390
8391 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
8392 &momentary_breakpoint_ops);
8393
8394 b->disposition = disp_donttouch;
8395 b->frame_id = null_frame_id;
8396
8397 b->thread = thread;
8398 gdb_assert (b->thread != 0);
8399
8400 return add_to_breakpoint_chain (std::move (b));
8401 }
8402
8403 /* Set a momentary breakpoint of type TYPE at address specified by
8404 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8405 frame. */
8406
8407 breakpoint_up
8408 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8409 struct frame_id frame_id, enum bptype type)
8410 {
8411 struct breakpoint *b;
8412
8413 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8414 tail-called one. */
8415 gdb_assert (!frame_id_artificial_p (frame_id));
8416
8417 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8418 b->enable_state = bp_enabled;
8419 b->disposition = disp_donttouch;
8420 b->frame_id = frame_id;
8421
8422 b->thread = inferior_thread ()->global_num;
8423
8424 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8425
8426 return breakpoint_up (b);
8427 }
8428
8429 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8430 The new breakpoint will have type TYPE, use OPS as its
8431 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8432
8433 static struct breakpoint *
8434 momentary_breakpoint_from_master (struct breakpoint *orig,
8435 enum bptype type,
8436 const struct breakpoint_ops *ops,
8437 int loc_enabled)
8438 {
8439 struct breakpoint *copy;
8440
8441 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8442 copy->loc = allocate_bp_location (copy);
8443 set_breakpoint_location_function (copy->loc);
8444
8445 copy->loc->gdbarch = orig->loc->gdbarch;
8446 copy->loc->requested_address = orig->loc->requested_address;
8447 copy->loc->address = orig->loc->address;
8448 copy->loc->section = orig->loc->section;
8449 copy->loc->pspace = orig->loc->pspace;
8450 copy->loc->probe = orig->loc->probe;
8451 copy->loc->line_number = orig->loc->line_number;
8452 copy->loc->symtab = orig->loc->symtab;
8453 copy->loc->enabled = loc_enabled;
8454 copy->frame_id = orig->frame_id;
8455 copy->thread = orig->thread;
8456 copy->pspace = orig->pspace;
8457
8458 copy->enable_state = bp_enabled;
8459 copy->disposition = disp_donttouch;
8460 copy->number = internal_breakpoint_number--;
8461
8462 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8463 return copy;
8464 }
8465
8466 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8467 ORIG is NULL. */
8468
8469 struct breakpoint *
8470 clone_momentary_breakpoint (struct breakpoint *orig)
8471 {
8472 /* If there's nothing to clone, then return nothing. */
8473 if (orig == NULL)
8474 return NULL;
8475
8476 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8477 }
8478
8479 breakpoint_up
8480 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8481 enum bptype type)
8482 {
8483 struct symtab_and_line sal;
8484
8485 sal = find_pc_line (pc, 0);
8486 sal.pc = pc;
8487 sal.section = find_pc_overlay (pc);
8488 sal.explicit_pc = 1;
8489
8490 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8491 }
8492 \f
8493
8494 /* Tell the user we have just set a breakpoint B. */
8495
8496 static void
8497 mention (struct breakpoint *b)
8498 {
8499 b->ops->print_mention (b);
8500 current_uiout->text ("\n");
8501 }
8502 \f
8503
8504 static bool bp_loc_is_permanent (struct bp_location *loc);
8505
8506 /* Handle "set breakpoint auto-hw on".
8507
8508 If the explicitly specified breakpoint type is not hardware
8509 breakpoint, check the memory map to see whether the breakpoint
8510 address is in read-only memory.
8511
8512 - location type is not hardware breakpoint, memory is read-only.
8513 We change the type of the location to hardware breakpoint.
8514
8515 - location type is hardware breakpoint, memory is read-write. This
8516 means we've previously made the location hardware one, but then the
8517 memory map changed, so we undo.
8518 */
8519
8520 static void
8521 handle_automatic_hardware_breakpoints (bp_location *bl)
8522 {
8523 if (automatic_hardware_breakpoints
8524 && bl->owner->type != bp_hardware_breakpoint
8525 && (bl->loc_type == bp_loc_software_breakpoint
8526 || bl->loc_type == bp_loc_hardware_breakpoint))
8527 {
8528 /* When breakpoints are removed, remove_breakpoints will use
8529 location types we've just set here, the only possible problem
8530 is that memory map has changed during running program, but
8531 it's not going to work anyway with current gdb. */
8532 mem_region *mr = lookup_mem_region (bl->address);
8533
8534 if (mr != nullptr)
8535 {
8536 enum bp_loc_type new_type;
8537
8538 if (mr->attrib.mode != MEM_RW)
8539 new_type = bp_loc_hardware_breakpoint;
8540 else
8541 new_type = bp_loc_software_breakpoint;
8542
8543 if (new_type != bl->loc_type)
8544 {
8545 static bool said = false;
8546
8547 bl->loc_type = new_type;
8548 if (!said)
8549 {
8550 fprintf_filtered (gdb_stdout,
8551 _("Note: automatically using "
8552 "hardware breakpoints for "
8553 "read-only addresses.\n"));
8554 said = true;
8555 }
8556 }
8557 }
8558 }
8559 }
8560
8561 static struct bp_location *
8562 add_location_to_breakpoint (struct breakpoint *b,
8563 const struct symtab_and_line *sal)
8564 {
8565 struct bp_location *loc, **tmp;
8566 CORE_ADDR adjusted_address;
8567 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8568
8569 if (loc_gdbarch == NULL)
8570 loc_gdbarch = b->gdbarch;
8571
8572 /* Adjust the breakpoint's address prior to allocating a location.
8573 Once we call allocate_bp_location(), that mostly uninitialized
8574 location will be placed on the location chain. Adjustment of the
8575 breakpoint may cause target_read_memory() to be called and we do
8576 not want its scan of the location chain to find a breakpoint and
8577 location that's only been partially initialized. */
8578 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8579 sal->pc, b->type);
8580
8581 /* Sort the locations by their ADDRESS. */
8582 loc = allocate_bp_location (b);
8583 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8584 tmp = &((*tmp)->next))
8585 ;
8586 loc->next = *tmp;
8587 *tmp = loc;
8588
8589 loc->requested_address = sal->pc;
8590 loc->address = adjusted_address;
8591 loc->pspace = sal->pspace;
8592 loc->probe.prob = sal->prob;
8593 loc->probe.objfile = sal->objfile;
8594 gdb_assert (loc->pspace != NULL);
8595 loc->section = sal->section;
8596 loc->gdbarch = loc_gdbarch;
8597 loc->line_number = sal->line;
8598 loc->symtab = sal->symtab;
8599 loc->symbol = sal->symbol;
8600 loc->msymbol = sal->msymbol;
8601 loc->objfile = sal->objfile;
8602
8603 set_breakpoint_location_function (loc);
8604
8605 /* While by definition, permanent breakpoints are already present in the
8606 code, we don't mark the location as inserted. Normally one would expect
8607 that GDB could rely on that breakpoint instruction to stop the program,
8608 thus removing the need to insert its own breakpoint, except that executing
8609 the breakpoint instruction can kill the target instead of reporting a
8610 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8611 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8612 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8613 breakpoint be inserted normally results in QEMU knowing about the GDB
8614 breakpoint, and thus trap before the breakpoint instruction is executed.
8615 (If GDB later needs to continue execution past the permanent breakpoint,
8616 it manually increments the PC, thus avoiding executing the breakpoint
8617 instruction.) */
8618 if (bp_loc_is_permanent (loc))
8619 loc->permanent = 1;
8620
8621 return loc;
8622 }
8623 \f
8624
8625 /* Return true if LOC is pointing to a permanent breakpoint,
8626 return false otherwise. */
8627
8628 static bool
8629 bp_loc_is_permanent (struct bp_location *loc)
8630 {
8631 gdb_assert (loc != NULL);
8632
8633 /* If we have a non-breakpoint-backed catchpoint or a software
8634 watchpoint, just return 0. We should not attempt to read from
8635 the addresses the locations of these breakpoint types point to.
8636 gdbarch_program_breakpoint_here_p, below, will attempt to read
8637 memory. */
8638 if (!bl_address_is_meaningful (loc))
8639 return false;
8640
8641 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8642 switch_to_program_space_and_thread (loc->pspace);
8643 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
8644 }
8645
8646 /* Build a command list for the dprintf corresponding to the current
8647 settings of the dprintf style options. */
8648
8649 static void
8650 update_dprintf_command_list (struct breakpoint *b)
8651 {
8652 char *dprintf_args = b->extra_string;
8653 char *printf_line = NULL;
8654
8655 if (!dprintf_args)
8656 return;
8657
8658 dprintf_args = skip_spaces (dprintf_args);
8659
8660 /* Allow a comma, as it may have terminated a location, but don't
8661 insist on it. */
8662 if (*dprintf_args == ',')
8663 ++dprintf_args;
8664 dprintf_args = skip_spaces (dprintf_args);
8665
8666 if (*dprintf_args != '"')
8667 error (_("Bad format string, missing '\"'."));
8668
8669 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8670 printf_line = xstrprintf ("printf %s", dprintf_args);
8671 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8672 {
8673 if (!dprintf_function)
8674 error (_("No function supplied for dprintf call"));
8675
8676 if (dprintf_channel && strlen (dprintf_channel) > 0)
8677 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8678 dprintf_function,
8679 dprintf_channel,
8680 dprintf_args);
8681 else
8682 printf_line = xstrprintf ("call (void) %s (%s)",
8683 dprintf_function,
8684 dprintf_args);
8685 }
8686 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8687 {
8688 if (target_can_run_breakpoint_commands ())
8689 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8690 else
8691 {
8692 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8693 printf_line = xstrprintf ("printf %s", dprintf_args);
8694 }
8695 }
8696 else
8697 internal_error (__FILE__, __LINE__,
8698 _("Invalid dprintf style."));
8699
8700 gdb_assert (printf_line != NULL);
8701
8702 /* Manufacture a printf sequence. */
8703 struct command_line *printf_cmd_line
8704 = new struct command_line (simple_control, printf_line);
8705 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8706 command_lines_deleter ()));
8707 }
8708
8709 /* Update all dprintf commands, making their command lists reflect
8710 current style settings. */
8711
8712 static void
8713 update_dprintf_commands (const char *args, int from_tty,
8714 struct cmd_list_element *c)
8715 {
8716 struct breakpoint *b;
8717
8718 ALL_BREAKPOINTS (b)
8719 {
8720 if (b->type == bp_dprintf)
8721 update_dprintf_command_list (b);
8722 }
8723 }
8724
8725 /* Create a breakpoint with SAL as location. Use LOCATION
8726 as a description of the location, and COND_STRING
8727 as condition expression. If LOCATION is NULL then create an
8728 "address location" from the address in the SAL. */
8729
8730 static void
8731 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8732 gdb::array_view<const symtab_and_line> sals,
8733 event_location_up &&location,
8734 gdb::unique_xmalloc_ptr<char> filter,
8735 gdb::unique_xmalloc_ptr<char> cond_string,
8736 gdb::unique_xmalloc_ptr<char> extra_string,
8737 enum bptype type, enum bpdisp disposition,
8738 int thread, int task, int ignore_count,
8739 const struct breakpoint_ops *ops, int from_tty,
8740 int enabled, int internal, unsigned flags,
8741 int display_canonical)
8742 {
8743 int i;
8744
8745 if (type == bp_hardware_breakpoint)
8746 {
8747 int target_resources_ok;
8748
8749 i = hw_breakpoint_used_count ();
8750 target_resources_ok =
8751 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8752 i + 1, 0);
8753 if (target_resources_ok == 0)
8754 error (_("No hardware breakpoint support in the target."));
8755 else if (target_resources_ok < 0)
8756 error (_("Hardware breakpoints used exceeds limit."));
8757 }
8758
8759 gdb_assert (!sals.empty ());
8760
8761 for (const auto &sal : sals)
8762 {
8763 struct bp_location *loc;
8764
8765 if (from_tty)
8766 {
8767 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8768 if (!loc_gdbarch)
8769 loc_gdbarch = gdbarch;
8770
8771 describe_other_breakpoints (loc_gdbarch,
8772 sal.pspace, sal.pc, sal.section, thread);
8773 }
8774
8775 if (&sal == &sals[0])
8776 {
8777 init_raw_breakpoint (b, gdbarch, sal, type, ops);
8778 b->thread = thread;
8779 b->task = task;
8780
8781 b->cond_string = cond_string.release ();
8782 b->extra_string = extra_string.release ();
8783 b->ignore_count = ignore_count;
8784 b->enable_state = enabled ? bp_enabled : bp_disabled;
8785 b->disposition = disposition;
8786
8787 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8788 b->loc->inserted = 1;
8789
8790 if (type == bp_static_tracepoint)
8791 {
8792 struct tracepoint *t = (struct tracepoint *) b;
8793 struct static_tracepoint_marker marker;
8794
8795 if (strace_marker_p (b))
8796 {
8797 /* We already know the marker exists, otherwise, we
8798 wouldn't see a sal for it. */
8799 const char *p
8800 = &event_location_to_string (b->location.get ())[3];
8801 const char *endp;
8802
8803 p = skip_spaces (p);
8804
8805 endp = skip_to_space (p);
8806
8807 t->static_trace_marker_id.assign (p, endp - p);
8808
8809 printf_filtered (_("Probed static tracepoint "
8810 "marker \"%s\"\n"),
8811 t->static_trace_marker_id.c_str ());
8812 }
8813 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8814 {
8815 t->static_trace_marker_id = std::move (marker.str_id);
8816
8817 printf_filtered (_("Probed static tracepoint "
8818 "marker \"%s\"\n"),
8819 t->static_trace_marker_id.c_str ());
8820 }
8821 else
8822 warning (_("Couldn't determine the static "
8823 "tracepoint marker to probe"));
8824 }
8825
8826 loc = b->loc;
8827 }
8828 else
8829 {
8830 loc = add_location_to_breakpoint (b, &sal);
8831 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8832 loc->inserted = 1;
8833 }
8834
8835 if (b->cond_string)
8836 {
8837 const char *arg = b->cond_string;
8838
8839 loc->cond = parse_exp_1 (&arg, loc->address,
8840 block_for_pc (loc->address), 0);
8841 if (*arg)
8842 error (_("Garbage '%s' follows condition"), arg);
8843 }
8844
8845 /* Dynamic printf requires and uses additional arguments on the
8846 command line, otherwise it's an error. */
8847 if (type == bp_dprintf)
8848 {
8849 if (b->extra_string)
8850 update_dprintf_command_list (b);
8851 else
8852 error (_("Format string required"));
8853 }
8854 else if (b->extra_string)
8855 error (_("Garbage '%s' at end of command"), b->extra_string);
8856 }
8857
8858 b->display_canonical = display_canonical;
8859 if (location != NULL)
8860 b->location = std::move (location);
8861 else
8862 b->location = new_address_location (b->loc->address, NULL, 0);
8863 b->filter = std::move (filter);
8864 }
8865
8866 static void
8867 create_breakpoint_sal (struct gdbarch *gdbarch,
8868 gdb::array_view<const symtab_and_line> sals,
8869 event_location_up &&location,
8870 gdb::unique_xmalloc_ptr<char> filter,
8871 gdb::unique_xmalloc_ptr<char> cond_string,
8872 gdb::unique_xmalloc_ptr<char> extra_string,
8873 enum bptype type, enum bpdisp disposition,
8874 int thread, int task, int ignore_count,
8875 const struct breakpoint_ops *ops, int from_tty,
8876 int enabled, int internal, unsigned flags,
8877 int display_canonical)
8878 {
8879 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
8880
8881 init_breakpoint_sal (b.get (), gdbarch,
8882 sals, std::move (location),
8883 std::move (filter),
8884 std::move (cond_string),
8885 std::move (extra_string),
8886 type, disposition,
8887 thread, task, ignore_count,
8888 ops, from_tty,
8889 enabled, internal, flags,
8890 display_canonical);
8891
8892 install_breakpoint (internal, std::move (b), 0);
8893 }
8894
8895 /* Add SALS.nelts breakpoints to the breakpoint table. For each
8896 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8897 value. COND_STRING, if not NULL, specified the condition to be
8898 used for all breakpoints. Essentially the only case where
8899 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8900 function. In that case, it's still not possible to specify
8901 separate conditions for different overloaded functions, so
8902 we take just a single condition string.
8903
8904 NOTE: If the function succeeds, the caller is expected to cleanup
8905 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
8906 array contents). If the function fails (error() is called), the
8907 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
8908 COND and SALS arrays and each of those arrays contents. */
8909
8910 static void
8911 create_breakpoints_sal (struct gdbarch *gdbarch,
8912 struct linespec_result *canonical,
8913 gdb::unique_xmalloc_ptr<char> cond_string,
8914 gdb::unique_xmalloc_ptr<char> extra_string,
8915 enum bptype type, enum bpdisp disposition,
8916 int thread, int task, int ignore_count,
8917 const struct breakpoint_ops *ops, int from_tty,
8918 int enabled, int internal, unsigned flags)
8919 {
8920 if (canonical->pre_expanded)
8921 gdb_assert (canonical->lsals.size () == 1);
8922
8923 for (const auto &lsal : canonical->lsals)
8924 {
8925 /* Note that 'location' can be NULL in the case of a plain
8926 'break', without arguments. */
8927 event_location_up location
8928 = (canonical->location != NULL
8929 ? copy_event_location (canonical->location.get ()) : NULL);
8930 gdb::unique_xmalloc_ptr<char> filter_string
8931 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
8932
8933 create_breakpoint_sal (gdbarch, lsal.sals,
8934 std::move (location),
8935 std::move (filter_string),
8936 std::move (cond_string),
8937 std::move (extra_string),
8938 type, disposition,
8939 thread, task, ignore_count, ops,
8940 from_tty, enabled, internal, flags,
8941 canonical->special_display);
8942 }
8943 }
8944
8945 /* Parse LOCATION which is assumed to be a SAL specification possibly
8946 followed by conditionals. On return, SALS contains an array of SAL
8947 addresses found. LOCATION points to the end of the SAL (for
8948 linespec locations).
8949
8950 The array and the line spec strings are allocated on the heap, it is
8951 the caller's responsibility to free them. */
8952
8953 static void
8954 parse_breakpoint_sals (const struct event_location *location,
8955 struct linespec_result *canonical)
8956 {
8957 struct symtab_and_line cursal;
8958
8959 if (event_location_type (location) == LINESPEC_LOCATION)
8960 {
8961 const char *spec = get_linespec_location (location)->spec_string;
8962
8963 if (spec == NULL)
8964 {
8965 /* The last displayed codepoint, if it's valid, is our default
8966 breakpoint address. */
8967 if (last_displayed_sal_is_valid ())
8968 {
8969 /* Set sal's pspace, pc, symtab, and line to the values
8970 corresponding to the last call to print_frame_info.
8971 Be sure to reinitialize LINE with NOTCURRENT == 0
8972 as the breakpoint line number is inappropriate otherwise.
8973 find_pc_line would adjust PC, re-set it back. */
8974 symtab_and_line sal = get_last_displayed_sal ();
8975 CORE_ADDR pc = sal.pc;
8976
8977 sal = find_pc_line (pc, 0);
8978
8979 /* "break" without arguments is equivalent to "break *PC"
8980 where PC is the last displayed codepoint's address. So
8981 make sure to set sal.explicit_pc to prevent GDB from
8982 trying to expand the list of sals to include all other
8983 instances with the same symtab and line. */
8984 sal.pc = pc;
8985 sal.explicit_pc = 1;
8986
8987 struct linespec_sals lsal;
8988 lsal.sals = {sal};
8989 lsal.canonical = NULL;
8990
8991 canonical->lsals.push_back (std::move (lsal));
8992 return;
8993 }
8994 else
8995 error (_("No default breakpoint address now."));
8996 }
8997 }
8998
8999 /* Force almost all breakpoints to be in terms of the
9000 current_source_symtab (which is decode_line_1's default).
9001 This should produce the results we want almost all of the
9002 time while leaving default_breakpoint_* alone.
9003
9004 ObjC: However, don't match an Objective-C method name which
9005 may have a '+' or '-' succeeded by a '['. */
9006 cursal = get_current_source_symtab_and_line ();
9007 if (last_displayed_sal_is_valid ())
9008 {
9009 const char *spec = NULL;
9010
9011 if (event_location_type (location) == LINESPEC_LOCATION)
9012 spec = get_linespec_location (location)->spec_string;
9013
9014 if (!cursal.symtab
9015 || (spec != NULL
9016 && strchr ("+-", spec[0]) != NULL
9017 && spec[1] != '['))
9018 {
9019 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9020 get_last_displayed_symtab (),
9021 get_last_displayed_line (),
9022 canonical, NULL, NULL);
9023 return;
9024 }
9025 }
9026
9027 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9028 cursal.symtab, cursal.line, canonical, NULL, NULL);
9029 }
9030
9031
9032 /* Convert each SAL into a real PC. Verify that the PC can be
9033 inserted as a breakpoint. If it can't throw an error. */
9034
9035 static void
9036 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
9037 {
9038 for (auto &sal : sals)
9039 resolve_sal_pc (&sal);
9040 }
9041
9042 /* Fast tracepoints may have restrictions on valid locations. For
9043 instance, a fast tracepoint using a jump instead of a trap will
9044 likely have to overwrite more bytes than a trap would, and so can
9045 only be placed where the instruction is longer than the jump, or a
9046 multi-instruction sequence does not have a jump into the middle of
9047 it, etc. */
9048
9049 static void
9050 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9051 gdb::array_view<const symtab_and_line> sals)
9052 {
9053 for (const auto &sal : sals)
9054 {
9055 struct gdbarch *sarch;
9056
9057 sarch = get_sal_arch (sal);
9058 /* We fall back to GDBARCH if there is no architecture
9059 associated with SAL. */
9060 if (sarch == NULL)
9061 sarch = gdbarch;
9062 std::string msg;
9063 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
9064 error (_("May not have a fast tracepoint at %s%s"),
9065 paddress (sarch, sal.pc), msg.c_str ());
9066 }
9067 }
9068
9069 /* Given TOK, a string specification of condition and thread, as
9070 accepted by the 'break' command, extract the condition
9071 string and thread number and set *COND_STRING and *THREAD.
9072 PC identifies the context at which the condition should be parsed.
9073 If no condition is found, *COND_STRING is set to NULL.
9074 If no thread is found, *THREAD is set to -1. */
9075
9076 static void
9077 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9078 char **cond_string, int *thread, int *task,
9079 char **rest)
9080 {
9081 *cond_string = NULL;
9082 *thread = -1;
9083 *task = 0;
9084 *rest = NULL;
9085
9086 while (tok && *tok)
9087 {
9088 const char *end_tok;
9089 int toklen;
9090 const char *cond_start = NULL;
9091 const char *cond_end = NULL;
9092
9093 tok = skip_spaces (tok);
9094
9095 if ((*tok == '"' || *tok == ',') && rest)
9096 {
9097 *rest = savestring (tok, strlen (tok));
9098 return;
9099 }
9100
9101 end_tok = skip_to_space (tok);
9102
9103 toklen = end_tok - tok;
9104
9105 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9106 {
9107 tok = cond_start = end_tok + 1;
9108 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9109 cond_end = tok;
9110 *cond_string = savestring (cond_start, cond_end - cond_start);
9111 }
9112 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9113 {
9114 const char *tmptok;
9115 struct thread_info *thr;
9116
9117 tok = end_tok + 1;
9118 thr = parse_thread_id (tok, &tmptok);
9119 if (tok == tmptok)
9120 error (_("Junk after thread keyword."));
9121 *thread = thr->global_num;
9122 tok = tmptok;
9123 }
9124 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9125 {
9126 char *tmptok;
9127
9128 tok = end_tok + 1;
9129 *task = strtol (tok, &tmptok, 0);
9130 if (tok == tmptok)
9131 error (_("Junk after task keyword."));
9132 if (!valid_task_id (*task))
9133 error (_("Unknown task %d."), *task);
9134 tok = tmptok;
9135 }
9136 else if (rest)
9137 {
9138 *rest = savestring (tok, strlen (tok));
9139 return;
9140 }
9141 else
9142 error (_("Junk at end of arguments."));
9143 }
9144 }
9145
9146 /* Decode a static tracepoint marker spec. */
9147
9148 static std::vector<symtab_and_line>
9149 decode_static_tracepoint_spec (const char **arg_p)
9150 {
9151 const char *p = &(*arg_p)[3];
9152 const char *endp;
9153
9154 p = skip_spaces (p);
9155
9156 endp = skip_to_space (p);
9157
9158 std::string marker_str (p, endp - p);
9159
9160 std::vector<static_tracepoint_marker> markers
9161 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9162 if (markers.empty ())
9163 error (_("No known static tracepoint marker named %s"),
9164 marker_str.c_str ());
9165
9166 std::vector<symtab_and_line> sals;
9167 sals.reserve (markers.size ());
9168
9169 for (const static_tracepoint_marker &marker : markers)
9170 {
9171 symtab_and_line sal = find_pc_line (marker.address, 0);
9172 sal.pc = marker.address;
9173 sals.push_back (sal);
9174 }
9175
9176 *arg_p = endp;
9177 return sals;
9178 }
9179
9180 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9181 according to IS_TRACEPOINT. */
9182
9183 static const struct breakpoint_ops *
9184 breakpoint_ops_for_event_location_type (enum event_location_type location_type,
9185 bool is_tracepoint)
9186 {
9187 if (is_tracepoint)
9188 {
9189 if (location_type == PROBE_LOCATION)
9190 return &tracepoint_probe_breakpoint_ops;
9191 else
9192 return &tracepoint_breakpoint_ops;
9193 }
9194 else
9195 {
9196 if (location_type == PROBE_LOCATION)
9197 return &bkpt_probe_breakpoint_ops;
9198 else
9199 return &bkpt_breakpoint_ops;
9200 }
9201 }
9202
9203 /* See breakpoint.h. */
9204
9205 const struct breakpoint_ops *
9206 breakpoint_ops_for_event_location (const struct event_location *location,
9207 bool is_tracepoint)
9208 {
9209 if (location != nullptr)
9210 return breakpoint_ops_for_event_location_type
9211 (event_location_type (location), is_tracepoint);
9212 return is_tracepoint ? &tracepoint_breakpoint_ops : &bkpt_breakpoint_ops;
9213 }
9214
9215 /* See breakpoint.h. */
9216
9217 int
9218 create_breakpoint (struct gdbarch *gdbarch,
9219 const struct event_location *location,
9220 const char *cond_string,
9221 int thread, const char *extra_string,
9222 int parse_extra,
9223 int tempflag, enum bptype type_wanted,
9224 int ignore_count,
9225 enum auto_boolean pending_break_support,
9226 const struct breakpoint_ops *ops,
9227 int from_tty, int enabled, int internal,
9228 unsigned flags)
9229 {
9230 struct linespec_result canonical;
9231 int pending = 0;
9232 int task = 0;
9233 int prev_bkpt_count = breakpoint_count;
9234
9235 gdb_assert (ops != NULL);
9236
9237 /* If extra_string isn't useful, set it to NULL. */
9238 if (extra_string != NULL && *extra_string == '\0')
9239 extra_string = NULL;
9240
9241 try
9242 {
9243 ops->create_sals_from_location (location, &canonical, type_wanted);
9244 }
9245 catch (const gdb_exception_error &e)
9246 {
9247 /* If caller is interested in rc value from parse, set
9248 value. */
9249 if (e.error == NOT_FOUND_ERROR)
9250 {
9251 /* If pending breakpoint support is turned off, throw
9252 error. */
9253
9254 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9255 throw;
9256
9257 exception_print (gdb_stderr, e);
9258
9259 /* If pending breakpoint support is auto query and the user
9260 selects no, then simply return the error code. */
9261 if (pending_break_support == AUTO_BOOLEAN_AUTO
9262 && !nquery (_("Make %s pending on future shared library load? "),
9263 bptype_string (type_wanted)))
9264 return 0;
9265
9266 /* At this point, either the user was queried about setting
9267 a pending breakpoint and selected yes, or pending
9268 breakpoint behavior is on and thus a pending breakpoint
9269 is defaulted on behalf of the user. */
9270 pending = 1;
9271 }
9272 else
9273 throw;
9274 }
9275
9276 if (!pending && canonical.lsals.empty ())
9277 return 0;
9278
9279 /* Resolve all line numbers to PC's and verify that the addresses
9280 are ok for the target. */
9281 if (!pending)
9282 {
9283 for (auto &lsal : canonical.lsals)
9284 breakpoint_sals_to_pc (lsal.sals);
9285 }
9286
9287 /* Fast tracepoints may have additional restrictions on location. */
9288 if (!pending && type_wanted == bp_fast_tracepoint)
9289 {
9290 for (const auto &lsal : canonical.lsals)
9291 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9292 }
9293
9294 /* Verify that condition can be parsed, before setting any
9295 breakpoints. Allocate a separate condition expression for each
9296 breakpoint. */
9297 if (!pending)
9298 {
9299 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9300 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9301
9302 if (parse_extra)
9303 {
9304 char *rest;
9305 char *cond;
9306
9307 const linespec_sals &lsal = canonical.lsals[0];
9308
9309 /* Here we only parse 'arg' to separate condition
9310 from thread number, so parsing in context of first
9311 sal is OK. When setting the breakpoint we'll
9312 re-parse it in context of each sal. */
9313
9314 find_condition_and_thread (extra_string, lsal.sals[0].pc,
9315 &cond, &thread, &task, &rest);
9316 cond_string_copy.reset (cond);
9317 extra_string_copy.reset (rest);
9318 }
9319 else
9320 {
9321 if (type_wanted != bp_dprintf
9322 && extra_string != NULL && *extra_string != '\0')
9323 error (_("Garbage '%s' at end of location"), extra_string);
9324
9325 /* Create a private copy of condition string. */
9326 if (cond_string)
9327 cond_string_copy.reset (xstrdup (cond_string));
9328 /* Create a private copy of any extra string. */
9329 if (extra_string)
9330 extra_string_copy.reset (xstrdup (extra_string));
9331 }
9332
9333 ops->create_breakpoints_sal (gdbarch, &canonical,
9334 std::move (cond_string_copy),
9335 std::move (extra_string_copy),
9336 type_wanted,
9337 tempflag ? disp_del : disp_donttouch,
9338 thread, task, ignore_count, ops,
9339 from_tty, enabled, internal, flags);
9340 }
9341 else
9342 {
9343 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9344
9345 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9346 b->location = copy_event_location (location);
9347
9348 if (parse_extra)
9349 b->cond_string = NULL;
9350 else
9351 {
9352 /* Create a private copy of condition string. */
9353 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
9354 b->thread = thread;
9355 }
9356
9357 /* Create a private copy of any extra string. */
9358 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
9359 b->ignore_count = ignore_count;
9360 b->disposition = tempflag ? disp_del : disp_donttouch;
9361 b->condition_not_parsed = 1;
9362 b->enable_state = enabled ? bp_enabled : bp_disabled;
9363 if ((type_wanted != bp_breakpoint
9364 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9365 b->pspace = current_program_space;
9366
9367 install_breakpoint (internal, std::move (b), 0);
9368 }
9369
9370 if (canonical.lsals.size () > 1)
9371 {
9372 warning (_("Multiple breakpoints were set.\nUse the "
9373 "\"delete\" command to delete unwanted breakpoints."));
9374 prev_breakpoint_count = prev_bkpt_count;
9375 }
9376
9377 update_global_location_list (UGLL_MAY_INSERT);
9378
9379 return 1;
9380 }
9381
9382 /* Set a breakpoint.
9383 ARG is a string describing breakpoint address,
9384 condition, and thread.
9385 FLAG specifies if a breakpoint is hardware on,
9386 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9387 and BP_TEMPFLAG. */
9388
9389 static void
9390 break_command_1 (const char *arg, int flag, int from_tty)
9391 {
9392 int tempflag = flag & BP_TEMPFLAG;
9393 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9394 ? bp_hardware_breakpoint
9395 : bp_breakpoint);
9396
9397 event_location_up location = string_to_event_location (&arg, current_language);
9398 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9399 (location.get (), false /* is_tracepoint */);
9400
9401 create_breakpoint (get_current_arch (),
9402 location.get (),
9403 NULL, 0, arg, 1 /* parse arg */,
9404 tempflag, type_wanted,
9405 0 /* Ignore count */,
9406 pending_break_support,
9407 ops,
9408 from_tty,
9409 1 /* enabled */,
9410 0 /* internal */,
9411 0);
9412 }
9413
9414 /* Helper function for break_command_1 and disassemble_command. */
9415
9416 void
9417 resolve_sal_pc (struct symtab_and_line *sal)
9418 {
9419 CORE_ADDR pc;
9420
9421 if (sal->pc == 0 && sal->symtab != NULL)
9422 {
9423 if (!find_line_pc (sal->symtab, sal->line, &pc))
9424 error (_("No line %d in file \"%s\"."),
9425 sal->line, symtab_to_filename_for_display (sal->symtab));
9426 sal->pc = pc;
9427
9428 /* If this SAL corresponds to a breakpoint inserted using a line
9429 number, then skip the function prologue if necessary. */
9430 if (sal->explicit_line)
9431 skip_prologue_sal (sal);
9432 }
9433
9434 if (sal->section == 0 && sal->symtab != NULL)
9435 {
9436 const struct blockvector *bv;
9437 const struct block *b;
9438 struct symbol *sym;
9439
9440 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9441 SYMTAB_COMPUNIT (sal->symtab));
9442 if (bv != NULL)
9443 {
9444 sym = block_linkage_function (b);
9445 if (sym != NULL)
9446 {
9447 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9448 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9449 sym);
9450 }
9451 else
9452 {
9453 /* It really is worthwhile to have the section, so we'll
9454 just have to look harder. This case can be executed
9455 if we have line numbers but no functions (as can
9456 happen in assembly source). */
9457
9458 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9459 switch_to_program_space_and_thread (sal->pspace);
9460
9461 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9462 if (msym.minsym)
9463 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9464 }
9465 }
9466 }
9467 }
9468
9469 void
9470 break_command (const char *arg, int from_tty)
9471 {
9472 break_command_1 (arg, 0, from_tty);
9473 }
9474
9475 void
9476 tbreak_command (const char *arg, int from_tty)
9477 {
9478 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9479 }
9480
9481 static void
9482 hbreak_command (const char *arg, int from_tty)
9483 {
9484 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9485 }
9486
9487 static void
9488 thbreak_command (const char *arg, int from_tty)
9489 {
9490 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9491 }
9492
9493 static void
9494 stop_command (const char *arg, int from_tty)
9495 {
9496 printf_filtered (_("Specify the type of breakpoint to set.\n\
9497 Usage: stop in <function | address>\n\
9498 stop at <line>\n"));
9499 }
9500
9501 static void
9502 stopin_command (const char *arg, int from_tty)
9503 {
9504 int badInput = 0;
9505
9506 if (arg == NULL)
9507 badInput = 1;
9508 else if (*arg != '*')
9509 {
9510 const char *argptr = arg;
9511 int hasColon = 0;
9512
9513 /* Look for a ':'. If this is a line number specification, then
9514 say it is bad, otherwise, it should be an address or
9515 function/method name. */
9516 while (*argptr && !hasColon)
9517 {
9518 hasColon = (*argptr == ':');
9519 argptr++;
9520 }
9521
9522 if (hasColon)
9523 badInput = (*argptr != ':'); /* Not a class::method */
9524 else
9525 badInput = isdigit (*arg); /* a simple line number */
9526 }
9527
9528 if (badInput)
9529 printf_filtered (_("Usage: stop in <function | address>\n"));
9530 else
9531 break_command_1 (arg, 0, from_tty);
9532 }
9533
9534 static void
9535 stopat_command (const char *arg, int from_tty)
9536 {
9537 int badInput = 0;
9538
9539 if (arg == NULL || *arg == '*') /* no line number */
9540 badInput = 1;
9541 else
9542 {
9543 const char *argptr = arg;
9544 int hasColon = 0;
9545
9546 /* Look for a ':'. If there is a '::' then get out, otherwise
9547 it is probably a line number. */
9548 while (*argptr && !hasColon)
9549 {
9550 hasColon = (*argptr == ':');
9551 argptr++;
9552 }
9553
9554 if (hasColon)
9555 badInput = (*argptr == ':'); /* we have class::method */
9556 else
9557 badInput = !isdigit (*arg); /* not a line number */
9558 }
9559
9560 if (badInput)
9561 printf_filtered (_("Usage: stop at LINE\n"));
9562 else
9563 break_command_1 (arg, 0, from_tty);
9564 }
9565
9566 /* The dynamic printf command is mostly like a regular breakpoint, but
9567 with a prewired command list consisting of a single output command,
9568 built from extra arguments supplied on the dprintf command
9569 line. */
9570
9571 static void
9572 dprintf_command (const char *arg, int from_tty)
9573 {
9574 event_location_up location = string_to_event_location (&arg, current_language);
9575
9576 /* If non-NULL, ARG should have been advanced past the location;
9577 the next character must be ','. */
9578 if (arg != NULL)
9579 {
9580 if (arg[0] != ',' || arg[1] == '\0')
9581 error (_("Format string required"));
9582 else
9583 {
9584 /* Skip the comma. */
9585 ++arg;
9586 }
9587 }
9588
9589 create_breakpoint (get_current_arch (),
9590 location.get (),
9591 NULL, 0, arg, 1 /* parse arg */,
9592 0, bp_dprintf,
9593 0 /* Ignore count */,
9594 pending_break_support,
9595 &dprintf_breakpoint_ops,
9596 from_tty,
9597 1 /* enabled */,
9598 0 /* internal */,
9599 0);
9600 }
9601
9602 static void
9603 agent_printf_command (const char *arg, int from_tty)
9604 {
9605 error (_("May only run agent-printf on the target"));
9606 }
9607
9608 /* Implement the "breakpoint_hit" breakpoint_ops method for
9609 ranged breakpoints. */
9610
9611 static int
9612 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9613 const address_space *aspace,
9614 CORE_ADDR bp_addr,
9615 const struct target_waitstatus *ws)
9616 {
9617 if (ws->kind != TARGET_WAITKIND_STOPPED
9618 || ws->value.sig != GDB_SIGNAL_TRAP)
9619 return 0;
9620
9621 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9622 bl->length, aspace, bp_addr);
9623 }
9624
9625 /* Implement the "resources_needed" breakpoint_ops method for
9626 ranged breakpoints. */
9627
9628 static int
9629 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9630 {
9631 return target_ranged_break_num_registers ();
9632 }
9633
9634 /* Implement the "print_it" breakpoint_ops method for
9635 ranged breakpoints. */
9636
9637 static enum print_stop_action
9638 print_it_ranged_breakpoint (bpstat bs)
9639 {
9640 struct breakpoint *b = bs->breakpoint_at;
9641 struct bp_location *bl = b->loc;
9642 struct ui_out *uiout = current_uiout;
9643
9644 gdb_assert (b->type == bp_hardware_breakpoint);
9645
9646 /* Ranged breakpoints have only one location. */
9647 gdb_assert (bl && bl->next == NULL);
9648
9649 annotate_breakpoint (b->number);
9650
9651 maybe_print_thread_hit_breakpoint (uiout);
9652
9653 if (b->disposition == disp_del)
9654 uiout->text ("Temporary ranged breakpoint ");
9655 else
9656 uiout->text ("Ranged breakpoint ");
9657 if (uiout->is_mi_like_p ())
9658 {
9659 uiout->field_string ("reason",
9660 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9661 uiout->field_string ("disp", bpdisp_text (b->disposition));
9662 }
9663 uiout->field_signed ("bkptno", b->number);
9664 uiout->text (", ");
9665
9666 return PRINT_SRC_AND_LOC;
9667 }
9668
9669 /* Implement the "print_one" breakpoint_ops method for
9670 ranged breakpoints. */
9671
9672 static void
9673 print_one_ranged_breakpoint (struct breakpoint *b,
9674 struct bp_location **last_loc)
9675 {
9676 struct bp_location *bl = b->loc;
9677 struct value_print_options opts;
9678 struct ui_out *uiout = current_uiout;
9679
9680 /* Ranged breakpoints have only one location. */
9681 gdb_assert (bl && bl->next == NULL);
9682
9683 get_user_print_options (&opts);
9684
9685 if (opts.addressprint)
9686 /* We don't print the address range here, it will be printed later
9687 by print_one_detail_ranged_breakpoint. */
9688 uiout->field_skip ("addr");
9689 annotate_field (5);
9690 print_breakpoint_location (b, bl);
9691 *last_loc = bl;
9692 }
9693
9694 /* Implement the "print_one_detail" breakpoint_ops method for
9695 ranged breakpoints. */
9696
9697 static void
9698 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9699 struct ui_out *uiout)
9700 {
9701 CORE_ADDR address_start, address_end;
9702 struct bp_location *bl = b->loc;
9703 string_file stb;
9704
9705 gdb_assert (bl);
9706
9707 address_start = bl->address;
9708 address_end = address_start + bl->length - 1;
9709
9710 uiout->text ("\taddress range: ");
9711 stb.printf ("[%s, %s]",
9712 print_core_address (bl->gdbarch, address_start),
9713 print_core_address (bl->gdbarch, address_end));
9714 uiout->field_stream ("addr", stb);
9715 uiout->text ("\n");
9716 }
9717
9718 /* Implement the "print_mention" breakpoint_ops method for
9719 ranged breakpoints. */
9720
9721 static void
9722 print_mention_ranged_breakpoint (struct breakpoint *b)
9723 {
9724 struct bp_location *bl = b->loc;
9725 struct ui_out *uiout = current_uiout;
9726
9727 gdb_assert (bl);
9728 gdb_assert (b->type == bp_hardware_breakpoint);
9729
9730 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9731 b->number, paddress (bl->gdbarch, bl->address),
9732 paddress (bl->gdbarch, bl->address + bl->length - 1));
9733 }
9734
9735 /* Implement the "print_recreate" breakpoint_ops method for
9736 ranged breakpoints. */
9737
9738 static void
9739 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9740 {
9741 fprintf_unfiltered (fp, "break-range %s, %s",
9742 event_location_to_string (b->location.get ()),
9743 event_location_to_string (b->location_range_end.get ()));
9744 print_recreate_thread (b, fp);
9745 }
9746
9747 /* The breakpoint_ops structure to be used in ranged breakpoints. */
9748
9749 static struct breakpoint_ops ranged_breakpoint_ops;
9750
9751 /* Find the address where the end of the breakpoint range should be
9752 placed, given the SAL of the end of the range. This is so that if
9753 the user provides a line number, the end of the range is set to the
9754 last instruction of the given line. */
9755
9756 static CORE_ADDR
9757 find_breakpoint_range_end (struct symtab_and_line sal)
9758 {
9759 CORE_ADDR end;
9760
9761 /* If the user provided a PC value, use it. Otherwise,
9762 find the address of the end of the given location. */
9763 if (sal.explicit_pc)
9764 end = sal.pc;
9765 else
9766 {
9767 int ret;
9768 CORE_ADDR start;
9769
9770 ret = find_line_pc_range (sal, &start, &end);
9771 if (!ret)
9772 error (_("Could not find location of the end of the range."));
9773
9774 /* find_line_pc_range returns the start of the next line. */
9775 end--;
9776 }
9777
9778 return end;
9779 }
9780
9781 /* Implement the "break-range" CLI command. */
9782
9783 static void
9784 break_range_command (const char *arg, int from_tty)
9785 {
9786 const char *arg_start;
9787 struct linespec_result canonical_start, canonical_end;
9788 int bp_count, can_use_bp, length;
9789 CORE_ADDR end;
9790 struct breakpoint *b;
9791
9792 /* We don't support software ranged breakpoints. */
9793 if (target_ranged_break_num_registers () < 0)
9794 error (_("This target does not support hardware ranged breakpoints."));
9795
9796 bp_count = hw_breakpoint_used_count ();
9797 bp_count += target_ranged_break_num_registers ();
9798 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9799 bp_count, 0);
9800 if (can_use_bp < 0)
9801 error (_("Hardware breakpoints used exceeds limit."));
9802
9803 arg = skip_spaces (arg);
9804 if (arg == NULL || arg[0] == '\0')
9805 error(_("No address range specified."));
9806
9807 arg_start = arg;
9808 event_location_up start_location = string_to_event_location (&arg,
9809 current_language);
9810 parse_breakpoint_sals (start_location.get (), &canonical_start);
9811
9812 if (arg[0] != ',')
9813 error (_("Too few arguments."));
9814 else if (canonical_start.lsals.empty ())
9815 error (_("Could not find location of the beginning of the range."));
9816
9817 const linespec_sals &lsal_start = canonical_start.lsals[0];
9818
9819 if (canonical_start.lsals.size () > 1
9820 || lsal_start.sals.size () != 1)
9821 error (_("Cannot create a ranged breakpoint with multiple locations."));
9822
9823 const symtab_and_line &sal_start = lsal_start.sals[0];
9824 std::string addr_string_start (arg_start, arg - arg_start);
9825
9826 arg++; /* Skip the comma. */
9827 arg = skip_spaces (arg);
9828
9829 /* Parse the end location. */
9830
9831 arg_start = arg;
9832
9833 /* We call decode_line_full directly here instead of using
9834 parse_breakpoint_sals because we need to specify the start location's
9835 symtab and line as the default symtab and line for the end of the
9836 range. This makes it possible to have ranges like "foo.c:27, +14",
9837 where +14 means 14 lines from the start location. */
9838 event_location_up end_location = string_to_event_location (&arg,
9839 current_language);
9840 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
9841 sal_start.symtab, sal_start.line,
9842 &canonical_end, NULL, NULL);
9843
9844 if (canonical_end.lsals.empty ())
9845 error (_("Could not find location of the end of the range."));
9846
9847 const linespec_sals &lsal_end = canonical_end.lsals[0];
9848 if (canonical_end.lsals.size () > 1
9849 || lsal_end.sals.size () != 1)
9850 error (_("Cannot create a ranged breakpoint with multiple locations."));
9851
9852 const symtab_and_line &sal_end = lsal_end.sals[0];
9853
9854 end = find_breakpoint_range_end (sal_end);
9855 if (sal_start.pc > end)
9856 error (_("Invalid address range, end precedes start."));
9857
9858 length = end - sal_start.pc + 1;
9859 if (length < 0)
9860 /* Length overflowed. */
9861 error (_("Address range too large."));
9862 else if (length == 1)
9863 {
9864 /* This range is simple enough to be handled by
9865 the `hbreak' command. */
9866 hbreak_command (&addr_string_start[0], 1);
9867
9868 return;
9869 }
9870
9871 /* Now set up the breakpoint. */
9872 b = set_raw_breakpoint (get_current_arch (), sal_start,
9873 bp_hardware_breakpoint, &ranged_breakpoint_ops);
9874 set_breakpoint_count (breakpoint_count + 1);
9875 b->number = breakpoint_count;
9876 b->disposition = disp_donttouch;
9877 b->location = std::move (start_location);
9878 b->location_range_end = std::move (end_location);
9879 b->loc->length = length;
9880
9881 mention (b);
9882 gdb::observers::breakpoint_created.notify (b);
9883 update_global_location_list (UGLL_MAY_INSERT);
9884 }
9885
9886 /* Return non-zero if EXP is verified as constant. Returned zero
9887 means EXP is variable. Also the constant detection may fail for
9888 some constant expressions and in such case still falsely return
9889 zero. */
9890
9891 static int
9892 watchpoint_exp_is_const (const struct expression *exp)
9893 {
9894 int i = exp->nelts;
9895
9896 while (i > 0)
9897 {
9898 int oplenp, argsp;
9899
9900 /* We are only interested in the descriptor of each element. */
9901 operator_length (exp, i, &oplenp, &argsp);
9902 i -= oplenp;
9903
9904 switch (exp->elts[i].opcode)
9905 {
9906 case BINOP_ADD:
9907 case BINOP_SUB:
9908 case BINOP_MUL:
9909 case BINOP_DIV:
9910 case BINOP_REM:
9911 case BINOP_MOD:
9912 case BINOP_LSH:
9913 case BINOP_RSH:
9914 case BINOP_LOGICAL_AND:
9915 case BINOP_LOGICAL_OR:
9916 case BINOP_BITWISE_AND:
9917 case BINOP_BITWISE_IOR:
9918 case BINOP_BITWISE_XOR:
9919 case BINOP_EQUAL:
9920 case BINOP_NOTEQUAL:
9921 case BINOP_LESS:
9922 case BINOP_GTR:
9923 case BINOP_LEQ:
9924 case BINOP_GEQ:
9925 case BINOP_REPEAT:
9926 case BINOP_COMMA:
9927 case BINOP_EXP:
9928 case BINOP_MIN:
9929 case BINOP_MAX:
9930 case BINOP_INTDIV:
9931 case BINOP_CONCAT:
9932 case TERNOP_COND:
9933 case TERNOP_SLICE:
9934
9935 case OP_LONG:
9936 case OP_FLOAT:
9937 case OP_LAST:
9938 case OP_COMPLEX:
9939 case OP_STRING:
9940 case OP_ARRAY:
9941 case OP_TYPE:
9942 case OP_TYPEOF:
9943 case OP_DECLTYPE:
9944 case OP_TYPEID:
9945 case OP_NAME:
9946 case OP_OBJC_NSSTRING:
9947
9948 case UNOP_NEG:
9949 case UNOP_LOGICAL_NOT:
9950 case UNOP_COMPLEMENT:
9951 case UNOP_ADDR:
9952 case UNOP_HIGH:
9953 case UNOP_CAST:
9954
9955 case UNOP_CAST_TYPE:
9956 case UNOP_REINTERPRET_CAST:
9957 case UNOP_DYNAMIC_CAST:
9958 /* Unary, binary and ternary operators: We have to check
9959 their operands. If they are constant, then so is the
9960 result of that operation. For instance, if A and B are
9961 determined to be constants, then so is "A + B".
9962
9963 UNOP_IND is one exception to the rule above, because the
9964 value of *ADDR is not necessarily a constant, even when
9965 ADDR is. */
9966 break;
9967
9968 case OP_VAR_VALUE:
9969 /* Check whether the associated symbol is a constant.
9970
9971 We use SYMBOL_CLASS rather than TYPE_CONST because it's
9972 possible that a buggy compiler could mark a variable as
9973 constant even when it is not, and TYPE_CONST would return
9974 true in this case, while SYMBOL_CLASS wouldn't.
9975
9976 We also have to check for function symbols because they
9977 are always constant. */
9978 {
9979 struct symbol *s = exp->elts[i + 2].symbol;
9980
9981 if (SYMBOL_CLASS (s) != LOC_BLOCK
9982 && SYMBOL_CLASS (s) != LOC_CONST
9983 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
9984 return 0;
9985 break;
9986 }
9987
9988 /* The default action is to return 0 because we are using
9989 the optimistic approach here: If we don't know something,
9990 then it is not a constant. */
9991 default:
9992 return 0;
9993 }
9994 }
9995
9996 return 1;
9997 }
9998
9999 /* Watchpoint destructor. */
10000
10001 watchpoint::~watchpoint ()
10002 {
10003 xfree (this->exp_string);
10004 xfree (this->exp_string_reparse);
10005 }
10006
10007 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10008
10009 static void
10010 re_set_watchpoint (struct breakpoint *b)
10011 {
10012 struct watchpoint *w = (struct watchpoint *) b;
10013
10014 /* Watchpoint can be either on expression using entirely global
10015 variables, or it can be on local variables.
10016
10017 Watchpoints of the first kind are never auto-deleted, and even
10018 persist across program restarts. Since they can use variables
10019 from shared libraries, we need to reparse expression as libraries
10020 are loaded and unloaded.
10021
10022 Watchpoints on local variables can also change meaning as result
10023 of solib event. For example, if a watchpoint uses both a local
10024 and a global variables in expression, it's a local watchpoint,
10025 but unloading of a shared library will make the expression
10026 invalid. This is not a very common use case, but we still
10027 re-evaluate expression, to avoid surprises to the user.
10028
10029 Note that for local watchpoints, we re-evaluate it only if
10030 watchpoints frame id is still valid. If it's not, it means the
10031 watchpoint is out of scope and will be deleted soon. In fact,
10032 I'm not sure we'll ever be called in this case.
10033
10034 If a local watchpoint's frame id is still valid, then
10035 w->exp_valid_block is likewise valid, and we can safely use it.
10036
10037 Don't do anything about disabled watchpoints, since they will be
10038 reevaluated again when enabled. */
10039 update_watchpoint (w, 1 /* reparse */);
10040 }
10041
10042 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10043
10044 static int
10045 insert_watchpoint (struct bp_location *bl)
10046 {
10047 struct watchpoint *w = (struct watchpoint *) bl->owner;
10048 int length = w->exact ? 1 : bl->length;
10049
10050 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10051 w->cond_exp.get ());
10052 }
10053
10054 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10055
10056 static int
10057 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10058 {
10059 struct watchpoint *w = (struct watchpoint *) bl->owner;
10060 int length = w->exact ? 1 : bl->length;
10061
10062 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10063 w->cond_exp.get ());
10064 }
10065
10066 static int
10067 breakpoint_hit_watchpoint (const struct bp_location *bl,
10068 const address_space *aspace, CORE_ADDR bp_addr,
10069 const struct target_waitstatus *ws)
10070 {
10071 struct breakpoint *b = bl->owner;
10072 struct watchpoint *w = (struct watchpoint *) b;
10073
10074 /* Continuable hardware watchpoints are treated as non-existent if the
10075 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10076 some data address). Otherwise gdb won't stop on a break instruction
10077 in the code (not from a breakpoint) when a hardware watchpoint has
10078 been defined. Also skip watchpoints which we know did not trigger
10079 (did not match the data address). */
10080 if (is_hardware_watchpoint (b)
10081 && w->watchpoint_triggered == watch_triggered_no)
10082 return 0;
10083
10084 return 1;
10085 }
10086
10087 static void
10088 check_status_watchpoint (bpstat bs)
10089 {
10090 gdb_assert (is_watchpoint (bs->breakpoint_at));
10091
10092 bpstat_check_watchpoint (bs);
10093 }
10094
10095 /* Implement the "resources_needed" breakpoint_ops method for
10096 hardware watchpoints. */
10097
10098 static int
10099 resources_needed_watchpoint (const struct bp_location *bl)
10100 {
10101 struct watchpoint *w = (struct watchpoint *) bl->owner;
10102 int length = w->exact? 1 : bl->length;
10103
10104 return target_region_ok_for_hw_watchpoint (bl->address, length);
10105 }
10106
10107 /* Implement the "works_in_software_mode" breakpoint_ops method for
10108 hardware watchpoints. */
10109
10110 static int
10111 works_in_software_mode_watchpoint (const struct breakpoint *b)
10112 {
10113 /* Read and access watchpoints only work with hardware support. */
10114 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10115 }
10116
10117 static enum print_stop_action
10118 print_it_watchpoint (bpstat bs)
10119 {
10120 struct breakpoint *b;
10121 enum print_stop_action result;
10122 struct watchpoint *w;
10123 struct ui_out *uiout = current_uiout;
10124
10125 gdb_assert (bs->bp_location_at != NULL);
10126
10127 b = bs->breakpoint_at;
10128 w = (struct watchpoint *) b;
10129
10130 annotate_watchpoint (b->number);
10131 maybe_print_thread_hit_breakpoint (uiout);
10132
10133 string_file stb;
10134
10135 gdb::optional<ui_out_emit_tuple> tuple_emitter;
10136 switch (b->type)
10137 {
10138 case bp_watchpoint:
10139 case bp_hardware_watchpoint:
10140 if (uiout->is_mi_like_p ())
10141 uiout->field_string
10142 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10143 mention (b);
10144 tuple_emitter.emplace (uiout, "value");
10145 uiout->text ("\nOld value = ");
10146 watchpoint_value_print (bs->old_val.get (), &stb);
10147 uiout->field_stream ("old", stb);
10148 uiout->text ("\nNew value = ");
10149 watchpoint_value_print (w->val.get (), &stb);
10150 uiout->field_stream ("new", stb);
10151 uiout->text ("\n");
10152 /* More than one watchpoint may have been triggered. */
10153 result = PRINT_UNKNOWN;
10154 break;
10155
10156 case bp_read_watchpoint:
10157 if (uiout->is_mi_like_p ())
10158 uiout->field_string
10159 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10160 mention (b);
10161 tuple_emitter.emplace (uiout, "value");
10162 uiout->text ("\nValue = ");
10163 watchpoint_value_print (w->val.get (), &stb);
10164 uiout->field_stream ("value", stb);
10165 uiout->text ("\n");
10166 result = PRINT_UNKNOWN;
10167 break;
10168
10169 case bp_access_watchpoint:
10170 if (bs->old_val != NULL)
10171 {
10172 if (uiout->is_mi_like_p ())
10173 uiout->field_string
10174 ("reason",
10175 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10176 mention (b);
10177 tuple_emitter.emplace (uiout, "value");
10178 uiout->text ("\nOld value = ");
10179 watchpoint_value_print (bs->old_val.get (), &stb);
10180 uiout->field_stream ("old", stb);
10181 uiout->text ("\nNew value = ");
10182 }
10183 else
10184 {
10185 mention (b);
10186 if (uiout->is_mi_like_p ())
10187 uiout->field_string
10188 ("reason",
10189 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10190 tuple_emitter.emplace (uiout, "value");
10191 uiout->text ("\nValue = ");
10192 }
10193 watchpoint_value_print (w->val.get (), &stb);
10194 uiout->field_stream ("new", stb);
10195 uiout->text ("\n");
10196 result = PRINT_UNKNOWN;
10197 break;
10198 default:
10199 result = PRINT_UNKNOWN;
10200 }
10201
10202 return result;
10203 }
10204
10205 /* Implement the "print_mention" breakpoint_ops method for hardware
10206 watchpoints. */
10207
10208 static void
10209 print_mention_watchpoint (struct breakpoint *b)
10210 {
10211 struct watchpoint *w = (struct watchpoint *) b;
10212 struct ui_out *uiout = current_uiout;
10213 const char *tuple_name;
10214
10215 switch (b->type)
10216 {
10217 case bp_watchpoint:
10218 uiout->text ("Watchpoint ");
10219 tuple_name = "wpt";
10220 break;
10221 case bp_hardware_watchpoint:
10222 uiout->text ("Hardware watchpoint ");
10223 tuple_name = "wpt";
10224 break;
10225 case bp_read_watchpoint:
10226 uiout->text ("Hardware read watchpoint ");
10227 tuple_name = "hw-rwpt";
10228 break;
10229 case bp_access_watchpoint:
10230 uiout->text ("Hardware access (read/write) watchpoint ");
10231 tuple_name = "hw-awpt";
10232 break;
10233 default:
10234 internal_error (__FILE__, __LINE__,
10235 _("Invalid hardware watchpoint type."));
10236 }
10237
10238 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10239 uiout->field_signed ("number", b->number);
10240 uiout->text (": ");
10241 uiout->field_string ("exp", w->exp_string);
10242 }
10243
10244 /* Implement the "print_recreate" breakpoint_ops method for
10245 watchpoints. */
10246
10247 static void
10248 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10249 {
10250 struct watchpoint *w = (struct watchpoint *) b;
10251
10252 switch (b->type)
10253 {
10254 case bp_watchpoint:
10255 case bp_hardware_watchpoint:
10256 fprintf_unfiltered (fp, "watch");
10257 break;
10258 case bp_read_watchpoint:
10259 fprintf_unfiltered (fp, "rwatch");
10260 break;
10261 case bp_access_watchpoint:
10262 fprintf_unfiltered (fp, "awatch");
10263 break;
10264 default:
10265 internal_error (__FILE__, __LINE__,
10266 _("Invalid watchpoint type."));
10267 }
10268
10269 fprintf_unfiltered (fp, " %s", w->exp_string);
10270 print_recreate_thread (b, fp);
10271 }
10272
10273 /* Implement the "explains_signal" breakpoint_ops method for
10274 watchpoints. */
10275
10276 static int
10277 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10278 {
10279 /* A software watchpoint cannot cause a signal other than
10280 GDB_SIGNAL_TRAP. */
10281 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10282 return 0;
10283
10284 return 1;
10285 }
10286
10287 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10288
10289 static struct breakpoint_ops watchpoint_breakpoint_ops;
10290
10291 /* Implement the "insert" breakpoint_ops method for
10292 masked hardware watchpoints. */
10293
10294 static int
10295 insert_masked_watchpoint (struct bp_location *bl)
10296 {
10297 struct watchpoint *w = (struct watchpoint *) bl->owner;
10298
10299 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10300 bl->watchpoint_type);
10301 }
10302
10303 /* Implement the "remove" breakpoint_ops method for
10304 masked hardware watchpoints. */
10305
10306 static int
10307 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10308 {
10309 struct watchpoint *w = (struct watchpoint *) bl->owner;
10310
10311 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10312 bl->watchpoint_type);
10313 }
10314
10315 /* Implement the "resources_needed" breakpoint_ops method for
10316 masked hardware watchpoints. */
10317
10318 static int
10319 resources_needed_masked_watchpoint (const struct bp_location *bl)
10320 {
10321 struct watchpoint *w = (struct watchpoint *) bl->owner;
10322
10323 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10324 }
10325
10326 /* Implement the "works_in_software_mode" breakpoint_ops method for
10327 masked hardware watchpoints. */
10328
10329 static int
10330 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10331 {
10332 return 0;
10333 }
10334
10335 /* Implement the "print_it" breakpoint_ops method for
10336 masked hardware watchpoints. */
10337
10338 static enum print_stop_action
10339 print_it_masked_watchpoint (bpstat bs)
10340 {
10341 struct breakpoint *b = bs->breakpoint_at;
10342 struct ui_out *uiout = current_uiout;
10343
10344 /* Masked watchpoints have only one location. */
10345 gdb_assert (b->loc && b->loc->next == NULL);
10346
10347 annotate_watchpoint (b->number);
10348 maybe_print_thread_hit_breakpoint (uiout);
10349
10350 switch (b->type)
10351 {
10352 case bp_hardware_watchpoint:
10353 if (uiout->is_mi_like_p ())
10354 uiout->field_string
10355 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10356 break;
10357
10358 case bp_read_watchpoint:
10359 if (uiout->is_mi_like_p ())
10360 uiout->field_string
10361 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10362 break;
10363
10364 case bp_access_watchpoint:
10365 if (uiout->is_mi_like_p ())
10366 uiout->field_string
10367 ("reason",
10368 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10369 break;
10370 default:
10371 internal_error (__FILE__, __LINE__,
10372 _("Invalid hardware watchpoint type."));
10373 }
10374
10375 mention (b);
10376 uiout->text (_("\n\
10377 Check the underlying instruction at PC for the memory\n\
10378 address and value which triggered this watchpoint.\n"));
10379 uiout->text ("\n");
10380
10381 /* More than one watchpoint may have been triggered. */
10382 return PRINT_UNKNOWN;
10383 }
10384
10385 /* Implement the "print_one_detail" breakpoint_ops method for
10386 masked hardware watchpoints. */
10387
10388 static void
10389 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10390 struct ui_out *uiout)
10391 {
10392 struct watchpoint *w = (struct watchpoint *) b;
10393
10394 /* Masked watchpoints have only one location. */
10395 gdb_assert (b->loc && b->loc->next == NULL);
10396
10397 uiout->text ("\tmask ");
10398 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10399 uiout->text ("\n");
10400 }
10401
10402 /* Implement the "print_mention" breakpoint_ops method for
10403 masked hardware watchpoints. */
10404
10405 static void
10406 print_mention_masked_watchpoint (struct breakpoint *b)
10407 {
10408 struct watchpoint *w = (struct watchpoint *) b;
10409 struct ui_out *uiout = current_uiout;
10410 const char *tuple_name;
10411
10412 switch (b->type)
10413 {
10414 case bp_hardware_watchpoint:
10415 uiout->text ("Masked hardware watchpoint ");
10416 tuple_name = "wpt";
10417 break;
10418 case bp_read_watchpoint:
10419 uiout->text ("Masked hardware read watchpoint ");
10420 tuple_name = "hw-rwpt";
10421 break;
10422 case bp_access_watchpoint:
10423 uiout->text ("Masked hardware access (read/write) watchpoint ");
10424 tuple_name = "hw-awpt";
10425 break;
10426 default:
10427 internal_error (__FILE__, __LINE__,
10428 _("Invalid hardware watchpoint type."));
10429 }
10430
10431 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10432 uiout->field_signed ("number", b->number);
10433 uiout->text (": ");
10434 uiout->field_string ("exp", w->exp_string);
10435 }
10436
10437 /* Implement the "print_recreate" breakpoint_ops method for
10438 masked hardware watchpoints. */
10439
10440 static void
10441 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10442 {
10443 struct watchpoint *w = (struct watchpoint *) b;
10444
10445 switch (b->type)
10446 {
10447 case bp_hardware_watchpoint:
10448 fprintf_unfiltered (fp, "watch");
10449 break;
10450 case bp_read_watchpoint:
10451 fprintf_unfiltered (fp, "rwatch");
10452 break;
10453 case bp_access_watchpoint:
10454 fprintf_unfiltered (fp, "awatch");
10455 break;
10456 default:
10457 internal_error (__FILE__, __LINE__,
10458 _("Invalid hardware watchpoint type."));
10459 }
10460
10461 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string,
10462 phex (w->hw_wp_mask, sizeof (CORE_ADDR)));
10463 print_recreate_thread (b, fp);
10464 }
10465
10466 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10467
10468 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10469
10470 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10471
10472 static bool
10473 is_masked_watchpoint (const struct breakpoint *b)
10474 {
10475 return b->ops == &masked_watchpoint_breakpoint_ops;
10476 }
10477
10478 /* accessflag: hw_write: watch write,
10479 hw_read: watch read,
10480 hw_access: watch access (read or write) */
10481 static void
10482 watch_command_1 (const char *arg, int accessflag, int from_tty,
10483 int just_location, int internal)
10484 {
10485 struct breakpoint *scope_breakpoint = NULL;
10486 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10487 struct value *result;
10488 int saved_bitpos = 0, saved_bitsize = 0;
10489 const char *exp_start = NULL;
10490 const char *exp_end = NULL;
10491 const char *tok, *end_tok;
10492 int toklen = -1;
10493 const char *cond_start = NULL;
10494 const char *cond_end = NULL;
10495 enum bptype bp_type;
10496 int thread = -1;
10497 int pc = 0;
10498 /* Flag to indicate whether we are going to use masks for
10499 the hardware watchpoint. */
10500 int use_mask = 0;
10501 CORE_ADDR mask = 0;
10502
10503 /* Make sure that we actually have parameters to parse. */
10504 if (arg != NULL && arg[0] != '\0')
10505 {
10506 const char *value_start;
10507
10508 exp_end = arg + strlen (arg);
10509
10510 /* Look for "parameter value" pairs at the end
10511 of the arguments string. */
10512 for (tok = exp_end - 1; tok > arg; tok--)
10513 {
10514 /* Skip whitespace at the end of the argument list. */
10515 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10516 tok--;
10517
10518 /* Find the beginning of the last token.
10519 This is the value of the parameter. */
10520 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10521 tok--;
10522 value_start = tok + 1;
10523
10524 /* Skip whitespace. */
10525 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10526 tok--;
10527
10528 end_tok = tok;
10529
10530 /* Find the beginning of the second to last token.
10531 This is the parameter itself. */
10532 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10533 tok--;
10534 tok++;
10535 toklen = end_tok - tok + 1;
10536
10537 if (toklen == 6 && startswith (tok, "thread"))
10538 {
10539 struct thread_info *thr;
10540 /* At this point we've found a "thread" token, which means
10541 the user is trying to set a watchpoint that triggers
10542 only in a specific thread. */
10543 const char *endp;
10544
10545 if (thread != -1)
10546 error(_("You can specify only one thread."));
10547
10548 /* Extract the thread ID from the next token. */
10549 thr = parse_thread_id (value_start, &endp);
10550
10551 /* Check if the user provided a valid thread ID. */
10552 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10553 invalid_thread_id_error (value_start);
10554
10555 thread = thr->global_num;
10556 }
10557 else if (toklen == 4 && startswith (tok, "mask"))
10558 {
10559 /* We've found a "mask" token, which means the user wants to
10560 create a hardware watchpoint that is going to have the mask
10561 facility. */
10562 struct value *mask_value, *mark;
10563
10564 if (use_mask)
10565 error(_("You can specify only one mask."));
10566
10567 use_mask = just_location = 1;
10568
10569 mark = value_mark ();
10570 mask_value = parse_to_comma_and_eval (&value_start);
10571 mask = value_as_address (mask_value);
10572 value_free_to_mark (mark);
10573 }
10574 else
10575 /* We didn't recognize what we found. We should stop here. */
10576 break;
10577
10578 /* Truncate the string and get rid of the "parameter value" pair before
10579 the arguments string is parsed by the parse_exp_1 function. */
10580 exp_end = tok;
10581 }
10582 }
10583 else
10584 exp_end = arg;
10585
10586 /* Parse the rest of the arguments. From here on out, everything
10587 is in terms of a newly allocated string instead of the original
10588 ARG. */
10589 std::string expression (arg, exp_end - arg);
10590 exp_start = arg = expression.c_str ();
10591 innermost_block_tracker tracker;
10592 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10593 exp_end = arg;
10594 /* Remove trailing whitespace from the expression before saving it.
10595 This makes the eventual display of the expression string a bit
10596 prettier. */
10597 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10598 --exp_end;
10599
10600 /* Checking if the expression is not constant. */
10601 if (watchpoint_exp_is_const (exp.get ()))
10602 {
10603 int len;
10604
10605 len = exp_end - exp_start;
10606 while (len > 0 && isspace (exp_start[len - 1]))
10607 len--;
10608 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10609 }
10610
10611 exp_valid_block = tracker.block ();
10612 struct value *mark = value_mark ();
10613 struct value *val_as_value = nullptr;
10614 fetch_subexp_value (exp.get (), &pc, &val_as_value, &result, NULL,
10615 just_location);
10616
10617 if (val_as_value != NULL && just_location)
10618 {
10619 saved_bitpos = value_bitpos (val_as_value);
10620 saved_bitsize = value_bitsize (val_as_value);
10621 }
10622
10623 value_ref_ptr val;
10624 if (just_location)
10625 {
10626 int ret;
10627
10628 exp_valid_block = NULL;
10629 val = release_value (value_addr (result));
10630 value_free_to_mark (mark);
10631
10632 if (use_mask)
10633 {
10634 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10635 mask);
10636 if (ret == -1)
10637 error (_("This target does not support masked watchpoints."));
10638 else if (ret == -2)
10639 error (_("Invalid mask or memory region."));
10640 }
10641 }
10642 else if (val_as_value != NULL)
10643 val = release_value (val_as_value);
10644
10645 tok = skip_spaces (arg);
10646 end_tok = skip_to_space (tok);
10647
10648 toklen = end_tok - tok;
10649 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10650 {
10651 tok = cond_start = end_tok + 1;
10652 innermost_block_tracker if_tracker;
10653 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10654
10655 /* The watchpoint expression may not be local, but the condition
10656 may still be. E.g.: `watch global if local > 0'. */
10657 cond_exp_valid_block = if_tracker.block ();
10658
10659 cond_end = tok;
10660 }
10661 if (*tok)
10662 error (_("Junk at end of command."));
10663
10664 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10665
10666 /* Save this because create_internal_breakpoint below invalidates
10667 'wp_frame'. */
10668 frame_id watchpoint_frame = get_frame_id (wp_frame);
10669
10670 /* If the expression is "local", then set up a "watchpoint scope"
10671 breakpoint at the point where we've left the scope of the watchpoint
10672 expression. Create the scope breakpoint before the watchpoint, so
10673 that we will encounter it first in bpstat_stop_status. */
10674 if (exp_valid_block != NULL && wp_frame != NULL)
10675 {
10676 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10677
10678 if (frame_id_p (caller_frame_id))
10679 {
10680 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10681 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10682
10683 scope_breakpoint
10684 = create_internal_breakpoint (caller_arch, caller_pc,
10685 bp_watchpoint_scope,
10686 &momentary_breakpoint_ops);
10687
10688 /* create_internal_breakpoint could invalidate WP_FRAME. */
10689 wp_frame = NULL;
10690
10691 scope_breakpoint->enable_state = bp_enabled;
10692
10693 /* Automatically delete the breakpoint when it hits. */
10694 scope_breakpoint->disposition = disp_del;
10695
10696 /* Only break in the proper frame (help with recursion). */
10697 scope_breakpoint->frame_id = caller_frame_id;
10698
10699 /* Set the address at which we will stop. */
10700 scope_breakpoint->loc->gdbarch = caller_arch;
10701 scope_breakpoint->loc->requested_address = caller_pc;
10702 scope_breakpoint->loc->address
10703 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10704 scope_breakpoint->loc->requested_address,
10705 scope_breakpoint->type);
10706 }
10707 }
10708
10709 /* Now set up the breakpoint. We create all watchpoints as hardware
10710 watchpoints here even if hardware watchpoints are turned off, a call
10711 to update_watchpoint later in this function will cause the type to
10712 drop back to bp_watchpoint (software watchpoint) if required. */
10713
10714 if (accessflag == hw_read)
10715 bp_type = bp_read_watchpoint;
10716 else if (accessflag == hw_access)
10717 bp_type = bp_access_watchpoint;
10718 else
10719 bp_type = bp_hardware_watchpoint;
10720
10721 std::unique_ptr<watchpoint> w (new watchpoint ());
10722
10723 if (use_mask)
10724 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10725 &masked_watchpoint_breakpoint_ops);
10726 else
10727 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10728 &watchpoint_breakpoint_ops);
10729 w->thread = thread;
10730 w->disposition = disp_donttouch;
10731 w->pspace = current_program_space;
10732 w->exp = std::move (exp);
10733 w->exp_valid_block = exp_valid_block;
10734 w->cond_exp_valid_block = cond_exp_valid_block;
10735 if (just_location)
10736 {
10737 struct type *t = value_type (val.get ());
10738 CORE_ADDR addr = value_as_address (val.get ());
10739
10740 w->exp_string_reparse
10741 = current_language->la_watch_location_expression (t, addr).release ();
10742
10743 w->exp_string = xstrprintf ("-location %.*s",
10744 (int) (exp_end - exp_start), exp_start);
10745 }
10746 else
10747 w->exp_string = savestring (exp_start, exp_end - exp_start);
10748
10749 if (use_mask)
10750 {
10751 w->hw_wp_mask = mask;
10752 }
10753 else
10754 {
10755 w->val = val;
10756 w->val_bitpos = saved_bitpos;
10757 w->val_bitsize = saved_bitsize;
10758 w->val_valid = true;
10759 }
10760
10761 if (cond_start)
10762 w->cond_string = savestring (cond_start, cond_end - cond_start);
10763 else
10764 w->cond_string = 0;
10765
10766 if (frame_id_p (watchpoint_frame))
10767 {
10768 w->watchpoint_frame = watchpoint_frame;
10769 w->watchpoint_thread = inferior_ptid;
10770 }
10771 else
10772 {
10773 w->watchpoint_frame = null_frame_id;
10774 w->watchpoint_thread = null_ptid;
10775 }
10776
10777 if (scope_breakpoint != NULL)
10778 {
10779 /* The scope breakpoint is related to the watchpoint. We will
10780 need to act on them together. */
10781 w->related_breakpoint = scope_breakpoint;
10782 scope_breakpoint->related_breakpoint = w.get ();
10783 }
10784
10785 if (!just_location)
10786 value_free_to_mark (mark);
10787
10788 /* Finally update the new watchpoint. This creates the locations
10789 that should be inserted. */
10790 update_watchpoint (w.get (), 1);
10791
10792 install_breakpoint (internal, std::move (w), 1);
10793 }
10794
10795 /* Return count of debug registers needed to watch the given expression.
10796 If the watchpoint cannot be handled in hardware return zero. */
10797
10798 static int
10799 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10800 {
10801 int found_memory_cnt = 0;
10802
10803 /* Did the user specifically forbid us to use hardware watchpoints? */
10804 if (!can_use_hw_watchpoints)
10805 return 0;
10806
10807 gdb_assert (!vals.empty ());
10808 struct value *head = vals[0].get ();
10809
10810 /* Make sure that the value of the expression depends only upon
10811 memory contents, and values computed from them within GDB. If we
10812 find any register references or function calls, we can't use a
10813 hardware watchpoint.
10814
10815 The idea here is that evaluating an expression generates a series
10816 of values, one holding the value of every subexpression. (The
10817 expression a*b+c has five subexpressions: a, b, a*b, c, and
10818 a*b+c.) GDB's values hold almost enough information to establish
10819 the criteria given above --- they identify memory lvalues,
10820 register lvalues, computed values, etcetera. So we can evaluate
10821 the expression, and then scan the chain of values that leaves
10822 behind to decide whether we can detect any possible change to the
10823 expression's final value using only hardware watchpoints.
10824
10825 However, I don't think that the values returned by inferior
10826 function calls are special in any way. So this function may not
10827 notice that an expression involving an inferior function call
10828 can't be watched with hardware watchpoints. FIXME. */
10829 for (const value_ref_ptr &iter : vals)
10830 {
10831 struct value *v = iter.get ();
10832
10833 if (VALUE_LVAL (v) == lval_memory)
10834 {
10835 if (v != head && value_lazy (v))
10836 /* A lazy memory lvalue in the chain is one that GDB never
10837 needed to fetch; we either just used its address (e.g.,
10838 `a' in `a.b') or we never needed it at all (e.g., `a'
10839 in `a,b'). This doesn't apply to HEAD; if that is
10840 lazy then it was not readable, but watch it anyway. */
10841 ;
10842 else
10843 {
10844 /* Ahh, memory we actually used! Check if we can cover
10845 it with hardware watchpoints. */
10846 struct type *vtype = check_typedef (value_type (v));
10847
10848 /* We only watch structs and arrays if user asked for it
10849 explicitly, never if they just happen to appear in a
10850 middle of some value chain. */
10851 if (v == head
10852 || (vtype->code () != TYPE_CODE_STRUCT
10853 && vtype->code () != TYPE_CODE_ARRAY))
10854 {
10855 CORE_ADDR vaddr = value_address (v);
10856 int len;
10857 int num_regs;
10858
10859 len = (target_exact_watchpoints
10860 && is_scalar_type_recursive (vtype))?
10861 1 : TYPE_LENGTH (value_type (v));
10862
10863 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10864 if (!num_regs)
10865 return 0;
10866 else
10867 found_memory_cnt += num_regs;
10868 }
10869 }
10870 }
10871 else if (VALUE_LVAL (v) != not_lval
10872 && deprecated_value_modifiable (v) == 0)
10873 return 0; /* These are values from the history (e.g., $1). */
10874 else if (VALUE_LVAL (v) == lval_register)
10875 return 0; /* Cannot watch a register with a HW watchpoint. */
10876 }
10877
10878 /* The expression itself looks suitable for using a hardware
10879 watchpoint, but give the target machine a chance to reject it. */
10880 return found_memory_cnt;
10881 }
10882
10883 void
10884 watch_command_wrapper (const char *arg, int from_tty, int internal)
10885 {
10886 watch_command_1 (arg, hw_write, from_tty, 0, internal);
10887 }
10888
10889 /* A helper function that looks for the "-location" argument and then
10890 calls watch_command_1. */
10891
10892 static void
10893 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
10894 {
10895 int just_location = 0;
10896
10897 if (arg
10898 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
10899 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
10900 just_location = 1;
10901
10902 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
10903 }
10904
10905 static void
10906 watch_command (const char *arg, int from_tty)
10907 {
10908 watch_maybe_just_location (arg, hw_write, from_tty);
10909 }
10910
10911 void
10912 rwatch_command_wrapper (const char *arg, int from_tty, int internal)
10913 {
10914 watch_command_1 (arg, hw_read, from_tty, 0, internal);
10915 }
10916
10917 static void
10918 rwatch_command (const char *arg, int from_tty)
10919 {
10920 watch_maybe_just_location (arg, hw_read, from_tty);
10921 }
10922
10923 void
10924 awatch_command_wrapper (const char *arg, int from_tty, int internal)
10925 {
10926 watch_command_1 (arg, hw_access, from_tty, 0, internal);
10927 }
10928
10929 static void
10930 awatch_command (const char *arg, int from_tty)
10931 {
10932 watch_maybe_just_location (arg, hw_access, from_tty);
10933 }
10934 \f
10935
10936 /* Data for the FSM that manages the until(location)/advance commands
10937 in infcmd.c. Here because it uses the mechanisms of
10938 breakpoints. */
10939
10940 struct until_break_fsm : public thread_fsm
10941 {
10942 /* The thread that was current when the command was executed. */
10943 int thread;
10944
10945 /* The breakpoint set at the destination location. */
10946 breakpoint_up location_breakpoint;
10947
10948 /* Breakpoint set at the return address in the caller frame. May be
10949 NULL. */
10950 breakpoint_up caller_breakpoint;
10951
10952 until_break_fsm (struct interp *cmd_interp, int thread,
10953 breakpoint_up &&location_breakpoint,
10954 breakpoint_up &&caller_breakpoint)
10955 : thread_fsm (cmd_interp),
10956 thread (thread),
10957 location_breakpoint (std::move (location_breakpoint)),
10958 caller_breakpoint (std::move (caller_breakpoint))
10959 {
10960 }
10961
10962 void clean_up (struct thread_info *thread) override;
10963 bool should_stop (struct thread_info *thread) override;
10964 enum async_reply_reason do_async_reply_reason () override;
10965 };
10966
10967 /* Implementation of the 'should_stop' FSM method for the
10968 until(location)/advance commands. */
10969
10970 bool
10971 until_break_fsm::should_stop (struct thread_info *tp)
10972 {
10973 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
10974 location_breakpoint.get ()) != NULL
10975 || (caller_breakpoint != NULL
10976 && bpstat_find_breakpoint (tp->control.stop_bpstat,
10977 caller_breakpoint.get ()) != NULL))
10978 set_finished ();
10979
10980 return true;
10981 }
10982
10983 /* Implementation of the 'clean_up' FSM method for the
10984 until(location)/advance commands. */
10985
10986 void
10987 until_break_fsm::clean_up (struct thread_info *)
10988 {
10989 /* Clean up our temporary breakpoints. */
10990 location_breakpoint.reset ();
10991 caller_breakpoint.reset ();
10992 delete_longjmp_breakpoint (thread);
10993 }
10994
10995 /* Implementation of the 'async_reply_reason' FSM method for the
10996 until(location)/advance commands. */
10997
10998 enum async_reply_reason
10999 until_break_fsm::do_async_reply_reason ()
11000 {
11001 return EXEC_ASYNC_LOCATION_REACHED;
11002 }
11003
11004 void
11005 until_break_command (const char *arg, int from_tty, int anywhere)
11006 {
11007 struct frame_info *frame;
11008 struct gdbarch *frame_gdbarch;
11009 struct frame_id stack_frame_id;
11010 struct frame_id caller_frame_id;
11011 int thread;
11012 struct thread_info *tp;
11013
11014 clear_proceed_status (0);
11015
11016 /* Set a breakpoint where the user wants it and at return from
11017 this function. */
11018
11019 event_location_up location = string_to_event_location (&arg, current_language);
11020
11021 std::vector<symtab_and_line> sals
11022 = (last_displayed_sal_is_valid ()
11023 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11024 get_last_displayed_symtab (),
11025 get_last_displayed_line ())
11026 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11027 NULL, NULL, 0));
11028
11029 if (sals.size () != 1)
11030 error (_("Couldn't get information on specified line."));
11031
11032 symtab_and_line &sal = sals[0];
11033
11034 if (*arg)
11035 error (_("Junk at end of arguments."));
11036
11037 resolve_sal_pc (&sal);
11038
11039 tp = inferior_thread ();
11040 thread = tp->global_num;
11041
11042 /* Note linespec handling above invalidates the frame chain.
11043 Installing a breakpoint also invalidates the frame chain (as it
11044 may need to switch threads), so do any frame handling before
11045 that. */
11046
11047 frame = get_selected_frame (NULL);
11048 frame_gdbarch = get_frame_arch (frame);
11049 stack_frame_id = get_stack_frame_id (frame);
11050 caller_frame_id = frame_unwind_caller_id (frame);
11051
11052 /* Keep within the current frame, or in frames called by the current
11053 one. */
11054
11055 breakpoint_up caller_breakpoint;
11056
11057 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11058
11059 if (frame_id_p (caller_frame_id))
11060 {
11061 struct symtab_and_line sal2;
11062 struct gdbarch *caller_gdbarch;
11063
11064 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11065 sal2.pc = frame_unwind_caller_pc (frame);
11066 caller_gdbarch = frame_unwind_caller_arch (frame);
11067 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11068 sal2,
11069 caller_frame_id,
11070 bp_until);
11071
11072 set_longjmp_breakpoint (tp, caller_frame_id);
11073 lj_deleter.emplace (thread);
11074 }
11075
11076 /* set_momentary_breakpoint could invalidate FRAME. */
11077 frame = NULL;
11078
11079 breakpoint_up location_breakpoint;
11080 if (anywhere)
11081 /* If the user told us to continue until a specified location,
11082 we don't specify a frame at which we need to stop. */
11083 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11084 null_frame_id, bp_until);
11085 else
11086 /* Otherwise, specify the selected frame, because we want to stop
11087 only at the very same frame. */
11088 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11089 stack_frame_id, bp_until);
11090
11091 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
11092 std::move (location_breakpoint),
11093 std::move (caller_breakpoint));
11094
11095 if (lj_deleter)
11096 lj_deleter->release ();
11097
11098 proceed (-1, GDB_SIGNAL_DEFAULT);
11099 }
11100
11101 /* This function attempts to parse an optional "if <cond>" clause
11102 from the arg string. If one is not found, it returns NULL.
11103
11104 Else, it returns a pointer to the condition string. (It does not
11105 attempt to evaluate the string against a particular block.) And,
11106 it updates arg to point to the first character following the parsed
11107 if clause in the arg string. */
11108
11109 const char *
11110 ep_parse_optional_if_clause (const char **arg)
11111 {
11112 const char *cond_string;
11113
11114 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11115 return NULL;
11116
11117 /* Skip the "if" keyword. */
11118 (*arg) += 2;
11119
11120 /* Skip any extra leading whitespace, and record the start of the
11121 condition string. */
11122 *arg = skip_spaces (*arg);
11123 cond_string = *arg;
11124
11125 /* Assume that the condition occupies the remainder of the arg
11126 string. */
11127 (*arg) += strlen (cond_string);
11128
11129 return cond_string;
11130 }
11131
11132 /* Commands to deal with catching events, such as signals, exceptions,
11133 process start/exit, etc. */
11134
11135 typedef enum
11136 {
11137 catch_fork_temporary, catch_vfork_temporary,
11138 catch_fork_permanent, catch_vfork_permanent
11139 }
11140 catch_fork_kind;
11141
11142 static void
11143 catch_fork_command_1 (const char *arg, int from_tty,
11144 struct cmd_list_element *command)
11145 {
11146 struct gdbarch *gdbarch = get_current_arch ();
11147 const char *cond_string = NULL;
11148 catch_fork_kind fork_kind;
11149 int tempflag;
11150
11151 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11152 tempflag = (fork_kind == catch_fork_temporary
11153 || fork_kind == catch_vfork_temporary);
11154
11155 if (!arg)
11156 arg = "";
11157 arg = skip_spaces (arg);
11158
11159 /* The allowed syntax is:
11160 catch [v]fork
11161 catch [v]fork if <cond>
11162
11163 First, check if there's an if clause. */
11164 cond_string = ep_parse_optional_if_clause (&arg);
11165
11166 if ((*arg != '\0') && !isspace (*arg))
11167 error (_("Junk at end of arguments."));
11168
11169 /* If this target supports it, create a fork or vfork catchpoint
11170 and enable reporting of such events. */
11171 switch (fork_kind)
11172 {
11173 case catch_fork_temporary:
11174 case catch_fork_permanent:
11175 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11176 &catch_fork_breakpoint_ops);
11177 break;
11178 case catch_vfork_temporary:
11179 case catch_vfork_permanent:
11180 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11181 &catch_vfork_breakpoint_ops);
11182 break;
11183 default:
11184 error (_("unsupported or unknown fork kind; cannot catch it"));
11185 break;
11186 }
11187 }
11188
11189 static void
11190 catch_exec_command_1 (const char *arg, int from_tty,
11191 struct cmd_list_element *command)
11192 {
11193 struct gdbarch *gdbarch = get_current_arch ();
11194 int tempflag;
11195 const char *cond_string = NULL;
11196
11197 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11198
11199 if (!arg)
11200 arg = "";
11201 arg = skip_spaces (arg);
11202
11203 /* The allowed syntax is:
11204 catch exec
11205 catch exec if <cond>
11206
11207 First, check if there's an if clause. */
11208 cond_string = ep_parse_optional_if_clause (&arg);
11209
11210 if ((*arg != '\0') && !isspace (*arg))
11211 error (_("Junk at end of arguments."));
11212
11213 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11214 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
11215 &catch_exec_breakpoint_ops);
11216 c->exec_pathname = NULL;
11217
11218 install_breakpoint (0, std::move (c), 1);
11219 }
11220
11221 void
11222 init_ada_exception_breakpoint (struct breakpoint *b,
11223 struct gdbarch *gdbarch,
11224 struct symtab_and_line sal,
11225 const char *addr_string,
11226 const struct breakpoint_ops *ops,
11227 int tempflag,
11228 int enabled,
11229 int from_tty)
11230 {
11231 if (from_tty)
11232 {
11233 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11234 if (!loc_gdbarch)
11235 loc_gdbarch = gdbarch;
11236
11237 describe_other_breakpoints (loc_gdbarch,
11238 sal.pspace, sal.pc, sal.section, -1);
11239 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11240 version for exception catchpoints, because two catchpoints
11241 used for different exception names will use the same address.
11242 In this case, a "breakpoint ... also set at..." warning is
11243 unproductive. Besides, the warning phrasing is also a bit
11244 inappropriate, we should use the word catchpoint, and tell
11245 the user what type of catchpoint it is. The above is good
11246 enough for now, though. */
11247 }
11248
11249 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
11250
11251 b->enable_state = enabled ? bp_enabled : bp_disabled;
11252 b->disposition = tempflag ? disp_del : disp_donttouch;
11253 b->location = string_to_event_location (&addr_string,
11254 language_def (language_ada));
11255 b->language = language_ada;
11256 }
11257
11258 \f
11259
11260 /* Compare two breakpoints and return a strcmp-like result. */
11261
11262 static int
11263 compare_breakpoints (const breakpoint *a, const breakpoint *b)
11264 {
11265 uintptr_t ua = (uintptr_t) a;
11266 uintptr_t ub = (uintptr_t) b;
11267
11268 if (a->number < b->number)
11269 return -1;
11270 else if (a->number > b->number)
11271 return 1;
11272
11273 /* Now sort by address, in case we see, e..g, two breakpoints with
11274 the number 0. */
11275 if (ua < ub)
11276 return -1;
11277 return ua > ub ? 1 : 0;
11278 }
11279
11280 /* Delete breakpoints by address or line. */
11281
11282 static void
11283 clear_command (const char *arg, int from_tty)
11284 {
11285 struct breakpoint *b;
11286 int default_match;
11287
11288 std::vector<symtab_and_line> decoded_sals;
11289 symtab_and_line last_sal;
11290 gdb::array_view<symtab_and_line> sals;
11291 if (arg)
11292 {
11293 decoded_sals
11294 = decode_line_with_current_source (arg,
11295 (DECODE_LINE_FUNFIRSTLINE
11296 | DECODE_LINE_LIST_MODE));
11297 default_match = 0;
11298 sals = decoded_sals;
11299 }
11300 else
11301 {
11302 /* Set sal's line, symtab, pc, and pspace to the values
11303 corresponding to the last call to print_frame_info. If the
11304 codepoint is not valid, this will set all the fields to 0. */
11305 last_sal = get_last_displayed_sal ();
11306 if (last_sal.symtab == 0)
11307 error (_("No source file specified."));
11308
11309 default_match = 1;
11310 sals = last_sal;
11311 }
11312
11313 /* We don't call resolve_sal_pc here. That's not as bad as it
11314 seems, because all existing breakpoints typically have both
11315 file/line and pc set. So, if clear is given file/line, we can
11316 match this to existing breakpoint without obtaining pc at all.
11317
11318 We only support clearing given the address explicitly
11319 present in breakpoint table. Say, we've set breakpoint
11320 at file:line. There were several PC values for that file:line,
11321 due to optimization, all in one block.
11322
11323 We've picked one PC value. If "clear" is issued with another
11324 PC corresponding to the same file:line, the breakpoint won't
11325 be cleared. We probably can still clear the breakpoint, but
11326 since the other PC value is never presented to user, user
11327 can only find it by guessing, and it does not seem important
11328 to support that. */
11329
11330 /* For each line spec given, delete bps which correspond to it. Do
11331 it in two passes, solely to preserve the current behavior that
11332 from_tty is forced true if we delete more than one
11333 breakpoint. */
11334
11335 std::vector<struct breakpoint *> found;
11336 for (const auto &sal : sals)
11337 {
11338 const char *sal_fullname;
11339
11340 /* If exact pc given, clear bpts at that pc.
11341 If line given (pc == 0), clear all bpts on specified line.
11342 If defaulting, clear all bpts on default line
11343 or at default pc.
11344
11345 defaulting sal.pc != 0 tests to do
11346
11347 0 1 pc
11348 1 1 pc _and_ line
11349 0 0 line
11350 1 0 <can't happen> */
11351
11352 sal_fullname = (sal.symtab == NULL
11353 ? NULL : symtab_to_fullname (sal.symtab));
11354
11355 /* Find all matching breakpoints and add them to 'found'. */
11356 ALL_BREAKPOINTS (b)
11357 {
11358 int match = 0;
11359 /* Are we going to delete b? */
11360 if (b->type != bp_none && !is_watchpoint (b))
11361 {
11362 struct bp_location *loc = b->loc;
11363 for (; loc; loc = loc->next)
11364 {
11365 /* If the user specified file:line, don't allow a PC
11366 match. This matches historical gdb behavior. */
11367 int pc_match = (!sal.explicit_line
11368 && sal.pc
11369 && (loc->pspace == sal.pspace)
11370 && (loc->address == sal.pc)
11371 && (!section_is_overlay (loc->section)
11372 || loc->section == sal.section));
11373 int line_match = 0;
11374
11375 if ((default_match || sal.explicit_line)
11376 && loc->symtab != NULL
11377 && sal_fullname != NULL
11378 && sal.pspace == loc->pspace
11379 && loc->line_number == sal.line
11380 && filename_cmp (symtab_to_fullname (loc->symtab),
11381 sal_fullname) == 0)
11382 line_match = 1;
11383
11384 if (pc_match || line_match)
11385 {
11386 match = 1;
11387 break;
11388 }
11389 }
11390 }
11391
11392 if (match)
11393 found.push_back (b);
11394 }
11395 }
11396
11397 /* Now go thru the 'found' chain and delete them. */
11398 if (found.empty ())
11399 {
11400 if (arg)
11401 error (_("No breakpoint at %s."), arg);
11402 else
11403 error (_("No breakpoint at this line."));
11404 }
11405
11406 /* Remove duplicates from the vec. */
11407 std::sort (found.begin (), found.end (),
11408 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11409 {
11410 return compare_breakpoints (bp_a, bp_b) < 0;
11411 });
11412 found.erase (std::unique (found.begin (), found.end (),
11413 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11414 {
11415 return compare_breakpoints (bp_a, bp_b) == 0;
11416 }),
11417 found.end ());
11418
11419 if (found.size () > 1)
11420 from_tty = 1; /* Always report if deleted more than one. */
11421 if (from_tty)
11422 {
11423 if (found.size () == 1)
11424 printf_unfiltered (_("Deleted breakpoint "));
11425 else
11426 printf_unfiltered (_("Deleted breakpoints "));
11427 }
11428
11429 for (breakpoint *iter : found)
11430 {
11431 if (from_tty)
11432 printf_unfiltered ("%d ", iter->number);
11433 delete_breakpoint (iter);
11434 }
11435 if (from_tty)
11436 putchar_unfiltered ('\n');
11437 }
11438 \f
11439 /* Delete breakpoint in BS if they are `delete' breakpoints and
11440 all breakpoints that are marked for deletion, whether hit or not.
11441 This is called after any breakpoint is hit, or after errors. */
11442
11443 void
11444 breakpoint_auto_delete (bpstat bs)
11445 {
11446 struct breakpoint *b, *b_tmp;
11447
11448 for (; bs; bs = bs->next)
11449 if (bs->breakpoint_at
11450 && bs->breakpoint_at->disposition == disp_del
11451 && bs->stop)
11452 delete_breakpoint (bs->breakpoint_at);
11453
11454 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11455 {
11456 if (b->disposition == disp_del_at_next_stop)
11457 delete_breakpoint (b);
11458 }
11459 }
11460
11461 /* A comparison function for bp_location AP and BP being interfaced to
11462 std::sort. Sort elements primarily by their ADDRESS (no matter what
11463 bl_address_is_meaningful says), secondarily by ordering first
11464 permanent elements and terciarily just ensuring the array is sorted
11465 stable way despite std::sort being an unstable algorithm. */
11466
11467 static int
11468 bp_location_is_less_than (const bp_location *a, const bp_location *b)
11469 {
11470 if (a->address != b->address)
11471 return a->address < b->address;
11472
11473 /* Sort locations at the same address by their pspace number, keeping
11474 locations of the same inferior (in a multi-inferior environment)
11475 grouped. */
11476
11477 if (a->pspace->num != b->pspace->num)
11478 return a->pspace->num < b->pspace->num;
11479
11480 /* Sort permanent breakpoints first. */
11481 if (a->permanent != b->permanent)
11482 return a->permanent > b->permanent;
11483
11484 /* Sort by type in order to make duplicate determination easier.
11485 See update_global_location_list. This is kept in sync with
11486 breakpoint_locations_match. */
11487 if (a->loc_type < b->loc_type)
11488 return true;
11489
11490 /* Likewise, for range-breakpoints, sort by length. */
11491 if (a->loc_type == bp_loc_hardware_breakpoint
11492 && b->loc_type == bp_loc_hardware_breakpoint
11493 && a->length < b->length)
11494 return true;
11495
11496 /* Make the internal GDB representation stable across GDB runs
11497 where A and B memory inside GDB can differ. Breakpoint locations of
11498 the same type at the same address can be sorted in arbitrary order. */
11499
11500 if (a->owner->number != b->owner->number)
11501 return a->owner->number < b->owner->number;
11502
11503 return a < b;
11504 }
11505
11506 /* Set bp_locations_placed_address_before_address_max and
11507 bp_locations_shadow_len_after_address_max according to the current
11508 content of the bp_locations array. */
11509
11510 static void
11511 bp_locations_target_extensions_update (void)
11512 {
11513 struct bp_location *bl, **blp_tmp;
11514
11515 bp_locations_placed_address_before_address_max = 0;
11516 bp_locations_shadow_len_after_address_max = 0;
11517
11518 ALL_BP_LOCATIONS (bl, blp_tmp)
11519 {
11520 CORE_ADDR start, end, addr;
11521
11522 if (!bp_location_has_shadow (bl))
11523 continue;
11524
11525 start = bl->target_info.placed_address;
11526 end = start + bl->target_info.shadow_len;
11527
11528 gdb_assert (bl->address >= start);
11529 addr = bl->address - start;
11530 if (addr > bp_locations_placed_address_before_address_max)
11531 bp_locations_placed_address_before_address_max = addr;
11532
11533 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11534
11535 gdb_assert (bl->address < end);
11536 addr = end - bl->address;
11537 if (addr > bp_locations_shadow_len_after_address_max)
11538 bp_locations_shadow_len_after_address_max = addr;
11539 }
11540 }
11541
11542 /* Download tracepoint locations if they haven't been. */
11543
11544 static void
11545 download_tracepoint_locations (void)
11546 {
11547 struct breakpoint *b;
11548 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11549
11550 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11551
11552 ALL_TRACEPOINTS (b)
11553 {
11554 struct bp_location *bl;
11555 struct tracepoint *t;
11556 int bp_location_downloaded = 0;
11557
11558 if ((b->type == bp_fast_tracepoint
11559 ? !may_insert_fast_tracepoints
11560 : !may_insert_tracepoints))
11561 continue;
11562
11563 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11564 {
11565 if (target_can_download_tracepoint ())
11566 can_download_tracepoint = TRIBOOL_TRUE;
11567 else
11568 can_download_tracepoint = TRIBOOL_FALSE;
11569 }
11570
11571 if (can_download_tracepoint == TRIBOOL_FALSE)
11572 break;
11573
11574 for (bl = b->loc; bl; bl = bl->next)
11575 {
11576 /* In tracepoint, locations are _never_ duplicated, so
11577 should_be_inserted is equivalent to
11578 unduplicated_should_be_inserted. */
11579 if (!should_be_inserted (bl) || bl->inserted)
11580 continue;
11581
11582 switch_to_program_space_and_thread (bl->pspace);
11583
11584 target_download_tracepoint (bl);
11585
11586 bl->inserted = 1;
11587 bp_location_downloaded = 1;
11588 }
11589 t = (struct tracepoint *) b;
11590 t->number_on_target = b->number;
11591 if (bp_location_downloaded)
11592 gdb::observers::breakpoint_modified.notify (b);
11593 }
11594 }
11595
11596 /* Swap the insertion/duplication state between two locations. */
11597
11598 static void
11599 swap_insertion (struct bp_location *left, struct bp_location *right)
11600 {
11601 const int left_inserted = left->inserted;
11602 const int left_duplicate = left->duplicate;
11603 const int left_needs_update = left->needs_update;
11604 const struct bp_target_info left_target_info = left->target_info;
11605
11606 /* Locations of tracepoints can never be duplicated. */
11607 if (is_tracepoint (left->owner))
11608 gdb_assert (!left->duplicate);
11609 if (is_tracepoint (right->owner))
11610 gdb_assert (!right->duplicate);
11611
11612 left->inserted = right->inserted;
11613 left->duplicate = right->duplicate;
11614 left->needs_update = right->needs_update;
11615 left->target_info = right->target_info;
11616 right->inserted = left_inserted;
11617 right->duplicate = left_duplicate;
11618 right->needs_update = left_needs_update;
11619 right->target_info = left_target_info;
11620 }
11621
11622 /* Force the re-insertion of the locations at ADDRESS. This is called
11623 once a new/deleted/modified duplicate location is found and we are evaluating
11624 conditions on the target's side. Such conditions need to be updated on
11625 the target. */
11626
11627 static void
11628 force_breakpoint_reinsertion (struct bp_location *bl)
11629 {
11630 struct bp_location **locp = NULL, **loc2p;
11631 struct bp_location *loc;
11632 CORE_ADDR address = 0;
11633 int pspace_num;
11634
11635 address = bl->address;
11636 pspace_num = bl->pspace->num;
11637
11638 /* This is only meaningful if the target is
11639 evaluating conditions and if the user has
11640 opted for condition evaluation on the target's
11641 side. */
11642 if (gdb_evaluates_breakpoint_condition_p ()
11643 || !target_supports_evaluation_of_breakpoint_conditions ())
11644 return;
11645
11646 /* Flag all breakpoint locations with this address and
11647 the same program space as the location
11648 as "its condition has changed". We need to
11649 update the conditions on the target's side. */
11650 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11651 {
11652 loc = *loc2p;
11653
11654 if (!is_breakpoint (loc->owner)
11655 || pspace_num != loc->pspace->num)
11656 continue;
11657
11658 /* Flag the location appropriately. We use a different state to
11659 let everyone know that we already updated the set of locations
11660 with addr bl->address and program space bl->pspace. This is so
11661 we don't have to keep calling these functions just to mark locations
11662 that have already been marked. */
11663 loc->condition_changed = condition_updated;
11664
11665 /* Free the agent expression bytecode as well. We will compute
11666 it later on. */
11667 loc->cond_bytecode.reset ();
11668 }
11669 }
11670
11671 /* Called whether new breakpoints are created, or existing breakpoints
11672 deleted, to update the global location list and recompute which
11673 locations are duplicate of which.
11674
11675 The INSERT_MODE flag determines whether locations may not, may, or
11676 shall be inserted now. See 'enum ugll_insert_mode' for more
11677 info. */
11678
11679 static void
11680 update_global_location_list (enum ugll_insert_mode insert_mode)
11681 {
11682 struct breakpoint *b;
11683 struct bp_location **locp, *loc;
11684 /* Last breakpoint location address that was marked for update. */
11685 CORE_ADDR last_addr = 0;
11686 /* Last breakpoint location program space that was marked for update. */
11687 int last_pspace_num = -1;
11688
11689 /* Used in the duplicates detection below. When iterating over all
11690 bp_locations, points to the first bp_location of a given address.
11691 Breakpoints and watchpoints of different types are never
11692 duplicates of each other. Keep one pointer for each type of
11693 breakpoint/watchpoint, so we only need to loop over all locations
11694 once. */
11695 struct bp_location *bp_loc_first; /* breakpoint */
11696 struct bp_location *wp_loc_first; /* hardware watchpoint */
11697 struct bp_location *awp_loc_first; /* access watchpoint */
11698 struct bp_location *rwp_loc_first; /* read watchpoint */
11699
11700 /* Saved former bp_locations array which we compare against the newly
11701 built bp_locations from the current state of ALL_BREAKPOINTS. */
11702 struct bp_location **old_locp;
11703 unsigned old_locations_count;
11704 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
11705
11706 old_locations_count = bp_locations_count;
11707 bp_locations = NULL;
11708 bp_locations_count = 0;
11709
11710 ALL_BREAKPOINTS (b)
11711 for (loc = b->loc; loc; loc = loc->next)
11712 bp_locations_count++;
11713
11714 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11715 locp = bp_locations;
11716 ALL_BREAKPOINTS (b)
11717 for (loc = b->loc; loc; loc = loc->next)
11718 *locp++ = loc;
11719
11720 /* See if we need to "upgrade" a software breakpoint to a hardware
11721 breakpoint. Do this before deciding whether locations are
11722 duplicates. Also do this before sorting because sorting order
11723 depends on location type. */
11724 for (locp = bp_locations;
11725 locp < bp_locations + bp_locations_count;
11726 locp++)
11727 {
11728 loc = *locp;
11729 if (!loc->inserted && should_be_inserted (loc))
11730 handle_automatic_hardware_breakpoints (loc);
11731 }
11732
11733 std::sort (bp_locations, bp_locations + bp_locations_count,
11734 bp_location_is_less_than);
11735
11736 bp_locations_target_extensions_update ();
11737
11738 /* Identify bp_location instances that are no longer present in the
11739 new list, and therefore should be freed. Note that it's not
11740 necessary that those locations should be removed from inferior --
11741 if there's another location at the same address (previously
11742 marked as duplicate), we don't need to remove/insert the
11743 location.
11744
11745 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11746 and former bp_location array state respectively. */
11747
11748 locp = bp_locations;
11749 for (old_locp = old_locations.get ();
11750 old_locp < old_locations.get () + old_locations_count;
11751 old_locp++)
11752 {
11753 struct bp_location *old_loc = *old_locp;
11754 struct bp_location **loc2p;
11755
11756 /* Tells if 'old_loc' is found among the new locations. If
11757 not, we have to free it. */
11758 int found_object = 0;
11759 /* Tells if the location should remain inserted in the target. */
11760 int keep_in_target = 0;
11761 int removed = 0;
11762
11763 /* Skip LOCP entries which will definitely never be needed.
11764 Stop either at or being the one matching OLD_LOC. */
11765 while (locp < bp_locations + bp_locations_count
11766 && (*locp)->address < old_loc->address)
11767 locp++;
11768
11769 for (loc2p = locp;
11770 (loc2p < bp_locations + bp_locations_count
11771 && (*loc2p)->address == old_loc->address);
11772 loc2p++)
11773 {
11774 /* Check if this is a new/duplicated location or a duplicated
11775 location that had its condition modified. If so, we want to send
11776 its condition to the target if evaluation of conditions is taking
11777 place there. */
11778 if ((*loc2p)->condition_changed == condition_modified
11779 && (last_addr != old_loc->address
11780 || last_pspace_num != old_loc->pspace->num))
11781 {
11782 force_breakpoint_reinsertion (*loc2p);
11783 last_pspace_num = old_loc->pspace->num;
11784 }
11785
11786 if (*loc2p == old_loc)
11787 found_object = 1;
11788 }
11789
11790 /* We have already handled this address, update it so that we don't
11791 have to go through updates again. */
11792 last_addr = old_loc->address;
11793
11794 /* Target-side condition evaluation: Handle deleted locations. */
11795 if (!found_object)
11796 force_breakpoint_reinsertion (old_loc);
11797
11798 /* If this location is no longer present, and inserted, look if
11799 there's maybe a new location at the same address. If so,
11800 mark that one inserted, and don't remove this one. This is
11801 needed so that we don't have a time window where a breakpoint
11802 at certain location is not inserted. */
11803
11804 if (old_loc->inserted)
11805 {
11806 /* If the location is inserted now, we might have to remove
11807 it. */
11808
11809 if (found_object && should_be_inserted (old_loc))
11810 {
11811 /* The location is still present in the location list,
11812 and still should be inserted. Don't do anything. */
11813 keep_in_target = 1;
11814 }
11815 else
11816 {
11817 /* This location still exists, but it won't be kept in the
11818 target since it may have been disabled. We proceed to
11819 remove its target-side condition. */
11820
11821 /* The location is either no longer present, or got
11822 disabled. See if there's another location at the
11823 same address, in which case we don't need to remove
11824 this one from the target. */
11825
11826 /* OLD_LOC comes from existing struct breakpoint. */
11827 if (bl_address_is_meaningful (old_loc))
11828 {
11829 for (loc2p = locp;
11830 (loc2p < bp_locations + bp_locations_count
11831 && (*loc2p)->address == old_loc->address);
11832 loc2p++)
11833 {
11834 struct bp_location *loc2 = *loc2p;
11835
11836 if (loc2 == old_loc)
11837 continue;
11838
11839 if (breakpoint_locations_match (loc2, old_loc))
11840 {
11841 /* Read watchpoint locations are switched to
11842 access watchpoints, if the former are not
11843 supported, but the latter are. */
11844 if (is_hardware_watchpoint (old_loc->owner))
11845 {
11846 gdb_assert (is_hardware_watchpoint (loc2->owner));
11847 loc2->watchpoint_type = old_loc->watchpoint_type;
11848 }
11849
11850 /* loc2 is a duplicated location. We need to check
11851 if it should be inserted in case it will be
11852 unduplicated. */
11853 if (unduplicated_should_be_inserted (loc2))
11854 {
11855 swap_insertion (old_loc, loc2);
11856 keep_in_target = 1;
11857 break;
11858 }
11859 }
11860 }
11861 }
11862 }
11863
11864 if (!keep_in_target)
11865 {
11866 if (remove_breakpoint (old_loc))
11867 {
11868 /* This is just about all we can do. We could keep
11869 this location on the global list, and try to
11870 remove it next time, but there's no particular
11871 reason why we will succeed next time.
11872
11873 Note that at this point, old_loc->owner is still
11874 valid, as delete_breakpoint frees the breakpoint
11875 only after calling us. */
11876 printf_filtered (_("warning: Error removing "
11877 "breakpoint %d\n"),
11878 old_loc->owner->number);
11879 }
11880 removed = 1;
11881 }
11882 }
11883
11884 if (!found_object)
11885 {
11886 if (removed && target_is_non_stop_p ()
11887 && need_moribund_for_location_type (old_loc))
11888 {
11889 /* This location was removed from the target. In
11890 non-stop mode, a race condition is possible where
11891 we've removed a breakpoint, but stop events for that
11892 breakpoint are already queued and will arrive later.
11893 We apply an heuristic to be able to distinguish such
11894 SIGTRAPs from other random SIGTRAPs: we keep this
11895 breakpoint location for a bit, and will retire it
11896 after we see some number of events. The theory here
11897 is that reporting of events should, "on the average",
11898 be fair, so after a while we'll see events from all
11899 threads that have anything of interest, and no longer
11900 need to keep this breakpoint location around. We
11901 don't hold locations forever so to reduce chances of
11902 mistaking a non-breakpoint SIGTRAP for a breakpoint
11903 SIGTRAP.
11904
11905 The heuristic failing can be disastrous on
11906 decr_pc_after_break targets.
11907
11908 On decr_pc_after_break targets, like e.g., x86-linux,
11909 if we fail to recognize a late breakpoint SIGTRAP,
11910 because events_till_retirement has reached 0 too
11911 soon, we'll fail to do the PC adjustment, and report
11912 a random SIGTRAP to the user. When the user resumes
11913 the inferior, it will most likely immediately crash
11914 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
11915 corrupted, because of being resumed e.g., in the
11916 middle of a multi-byte instruction, or skipped a
11917 one-byte instruction. This was actually seen happen
11918 on native x86-linux, and should be less rare on
11919 targets that do not support new thread events, like
11920 remote, due to the heuristic depending on
11921 thread_count.
11922
11923 Mistaking a random SIGTRAP for a breakpoint trap
11924 causes similar symptoms (PC adjustment applied when
11925 it shouldn't), but then again, playing with SIGTRAPs
11926 behind the debugger's back is asking for trouble.
11927
11928 Since hardware watchpoint traps are always
11929 distinguishable from other traps, so we don't need to
11930 apply keep hardware watchpoint moribund locations
11931 around. We simply always ignore hardware watchpoint
11932 traps we can no longer explain. */
11933
11934 process_stratum_target *proc_target = nullptr;
11935 for (inferior *inf : all_inferiors ())
11936 if (inf->pspace == old_loc->pspace)
11937 {
11938 proc_target = inf->process_target ();
11939 break;
11940 }
11941 if (proc_target != nullptr)
11942 old_loc->events_till_retirement
11943 = 3 * (thread_count (proc_target) + 1);
11944 else
11945 old_loc->events_till_retirement = 1;
11946 old_loc->owner = NULL;
11947
11948 moribund_locations.push_back (old_loc);
11949 }
11950 else
11951 {
11952 old_loc->owner = NULL;
11953 decref_bp_location (&old_loc);
11954 }
11955 }
11956 }
11957
11958 /* Rescan breakpoints at the same address and section, marking the
11959 first one as "first" and any others as "duplicates". This is so
11960 that the bpt instruction is only inserted once. If we have a
11961 permanent breakpoint at the same place as BPT, make that one the
11962 official one, and the rest as duplicates. Permanent breakpoints
11963 are sorted first for the same address.
11964
11965 Do the same for hardware watchpoints, but also considering the
11966 watchpoint's type (regular/access/read) and length. */
11967
11968 bp_loc_first = NULL;
11969 wp_loc_first = NULL;
11970 awp_loc_first = NULL;
11971 rwp_loc_first = NULL;
11972 ALL_BP_LOCATIONS (loc, locp)
11973 {
11974 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11975 non-NULL. */
11976 struct bp_location **loc_first_p;
11977 b = loc->owner;
11978
11979 if (!unduplicated_should_be_inserted (loc)
11980 || !bl_address_is_meaningful (loc)
11981 /* Don't detect duplicate for tracepoint locations because they are
11982 never duplicated. See the comments in field `duplicate' of
11983 `struct bp_location'. */
11984 || is_tracepoint (b))
11985 {
11986 /* Clear the condition modification flag. */
11987 loc->condition_changed = condition_unchanged;
11988 continue;
11989 }
11990
11991 if (b->type == bp_hardware_watchpoint)
11992 loc_first_p = &wp_loc_first;
11993 else if (b->type == bp_read_watchpoint)
11994 loc_first_p = &rwp_loc_first;
11995 else if (b->type == bp_access_watchpoint)
11996 loc_first_p = &awp_loc_first;
11997 else
11998 loc_first_p = &bp_loc_first;
11999
12000 if (*loc_first_p == NULL
12001 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12002 || !breakpoint_locations_match (loc, *loc_first_p))
12003 {
12004 *loc_first_p = loc;
12005 loc->duplicate = 0;
12006
12007 if (is_breakpoint (loc->owner) && loc->condition_changed)
12008 {
12009 loc->needs_update = 1;
12010 /* Clear the condition modification flag. */
12011 loc->condition_changed = condition_unchanged;
12012 }
12013 continue;
12014 }
12015
12016
12017 /* This and the above ensure the invariant that the first location
12018 is not duplicated, and is the inserted one.
12019 All following are marked as duplicated, and are not inserted. */
12020 if (loc->inserted)
12021 swap_insertion (loc, *loc_first_p);
12022 loc->duplicate = 1;
12023
12024 /* Clear the condition modification flag. */
12025 loc->condition_changed = condition_unchanged;
12026 }
12027
12028 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12029 {
12030 if (insert_mode != UGLL_DONT_INSERT)
12031 insert_breakpoint_locations ();
12032 else
12033 {
12034 /* Even though the caller told us to not insert new
12035 locations, we may still need to update conditions on the
12036 target's side of breakpoints that were already inserted
12037 if the target is evaluating breakpoint conditions. We
12038 only update conditions for locations that are marked
12039 "needs_update". */
12040 update_inserted_breakpoint_locations ();
12041 }
12042 }
12043
12044 if (insert_mode != UGLL_DONT_INSERT)
12045 download_tracepoint_locations ();
12046 }
12047
12048 void
12049 breakpoint_retire_moribund (void)
12050 {
12051 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12052 {
12053 struct bp_location *loc = moribund_locations[ix];
12054 if (--(loc->events_till_retirement) == 0)
12055 {
12056 decref_bp_location (&loc);
12057 unordered_remove (moribund_locations, ix);
12058 --ix;
12059 }
12060 }
12061 }
12062
12063 static void
12064 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12065 {
12066
12067 try
12068 {
12069 update_global_location_list (insert_mode);
12070 }
12071 catch (const gdb_exception_error &e)
12072 {
12073 }
12074 }
12075
12076 /* Clear BKP from a BPS. */
12077
12078 static void
12079 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12080 {
12081 bpstat bs;
12082
12083 for (bs = bps; bs; bs = bs->next)
12084 if (bs->breakpoint_at == bpt)
12085 {
12086 bs->breakpoint_at = NULL;
12087 bs->old_val = NULL;
12088 /* bs->commands will be freed later. */
12089 }
12090 }
12091
12092 /* Callback for iterate_over_threads. */
12093 static int
12094 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12095 {
12096 struct breakpoint *bpt = (struct breakpoint *) data;
12097
12098 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12099 return 0;
12100 }
12101
12102 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12103 callbacks. */
12104
12105 static void
12106 say_where (struct breakpoint *b)
12107 {
12108 struct value_print_options opts;
12109
12110 get_user_print_options (&opts);
12111
12112 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12113 single string. */
12114 if (b->loc == NULL)
12115 {
12116 /* For pending locations, the output differs slightly based
12117 on b->extra_string. If this is non-NULL, it contains either
12118 a condition or dprintf arguments. */
12119 if (b->extra_string == NULL)
12120 {
12121 printf_filtered (_(" (%s) pending."),
12122 event_location_to_string (b->location.get ()));
12123 }
12124 else if (b->type == bp_dprintf)
12125 {
12126 printf_filtered (_(" (%s,%s) pending."),
12127 event_location_to_string (b->location.get ()),
12128 b->extra_string);
12129 }
12130 else
12131 {
12132 printf_filtered (_(" (%s %s) pending."),
12133 event_location_to_string (b->location.get ()),
12134 b->extra_string);
12135 }
12136 }
12137 else
12138 {
12139 if (opts.addressprint || b->loc->symtab == NULL)
12140 printf_filtered (" at %ps",
12141 styled_string (address_style.style (),
12142 paddress (b->loc->gdbarch,
12143 b->loc->address)));
12144 if (b->loc->symtab != NULL)
12145 {
12146 /* If there is a single location, we can print the location
12147 more nicely. */
12148 if (b->loc->next == NULL)
12149 {
12150 const char *filename
12151 = symtab_to_filename_for_display (b->loc->symtab);
12152 printf_filtered (": file %ps, line %d.",
12153 styled_string (file_name_style.style (),
12154 filename),
12155 b->loc->line_number);
12156 }
12157 else
12158 /* This is not ideal, but each location may have a
12159 different file name, and this at least reflects the
12160 real situation somewhat. */
12161 printf_filtered (": %s.",
12162 event_location_to_string (b->location.get ()));
12163 }
12164
12165 if (b->loc->next)
12166 {
12167 struct bp_location *loc = b->loc;
12168 int n = 0;
12169 for (; loc; loc = loc->next)
12170 ++n;
12171 printf_filtered (" (%d locations)", n);
12172 }
12173 }
12174 }
12175
12176 bp_location::~bp_location ()
12177 {
12178 xfree (function_name);
12179 }
12180
12181 /* Destructor for the breakpoint base class. */
12182
12183 breakpoint::~breakpoint ()
12184 {
12185 xfree (this->cond_string);
12186 xfree (this->extra_string);
12187 }
12188
12189 static struct bp_location *
12190 base_breakpoint_allocate_location (struct breakpoint *self)
12191 {
12192 return new bp_location (self);
12193 }
12194
12195 static void
12196 base_breakpoint_re_set (struct breakpoint *b)
12197 {
12198 /* Nothing to re-set. */
12199 }
12200
12201 #define internal_error_pure_virtual_called() \
12202 gdb_assert_not_reached ("pure virtual function called")
12203
12204 static int
12205 base_breakpoint_insert_location (struct bp_location *bl)
12206 {
12207 internal_error_pure_virtual_called ();
12208 }
12209
12210 static int
12211 base_breakpoint_remove_location (struct bp_location *bl,
12212 enum remove_bp_reason reason)
12213 {
12214 internal_error_pure_virtual_called ();
12215 }
12216
12217 static int
12218 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12219 const address_space *aspace,
12220 CORE_ADDR bp_addr,
12221 const struct target_waitstatus *ws)
12222 {
12223 internal_error_pure_virtual_called ();
12224 }
12225
12226 static void
12227 base_breakpoint_check_status (bpstat bs)
12228 {
12229 /* Always stop. */
12230 }
12231
12232 /* A "works_in_software_mode" breakpoint_ops method that just internal
12233 errors. */
12234
12235 static int
12236 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12237 {
12238 internal_error_pure_virtual_called ();
12239 }
12240
12241 /* A "resources_needed" breakpoint_ops method that just internal
12242 errors. */
12243
12244 static int
12245 base_breakpoint_resources_needed (const struct bp_location *bl)
12246 {
12247 internal_error_pure_virtual_called ();
12248 }
12249
12250 static enum print_stop_action
12251 base_breakpoint_print_it (bpstat bs)
12252 {
12253 internal_error_pure_virtual_called ();
12254 }
12255
12256 static void
12257 base_breakpoint_print_one_detail (const struct breakpoint *self,
12258 struct ui_out *uiout)
12259 {
12260 /* nothing */
12261 }
12262
12263 static void
12264 base_breakpoint_print_mention (struct breakpoint *b)
12265 {
12266 internal_error_pure_virtual_called ();
12267 }
12268
12269 static void
12270 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12271 {
12272 internal_error_pure_virtual_called ();
12273 }
12274
12275 static void
12276 base_breakpoint_create_sals_from_location
12277 (const struct event_location *location,
12278 struct linespec_result *canonical,
12279 enum bptype type_wanted)
12280 {
12281 internal_error_pure_virtual_called ();
12282 }
12283
12284 static void
12285 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12286 struct linespec_result *c,
12287 gdb::unique_xmalloc_ptr<char> cond_string,
12288 gdb::unique_xmalloc_ptr<char> extra_string,
12289 enum bptype type_wanted,
12290 enum bpdisp disposition,
12291 int thread,
12292 int task, int ignore_count,
12293 const struct breakpoint_ops *o,
12294 int from_tty, int enabled,
12295 int internal, unsigned flags)
12296 {
12297 internal_error_pure_virtual_called ();
12298 }
12299
12300 static std::vector<symtab_and_line>
12301 base_breakpoint_decode_location (struct breakpoint *b,
12302 const struct event_location *location,
12303 struct program_space *search_pspace)
12304 {
12305 internal_error_pure_virtual_called ();
12306 }
12307
12308 /* The default 'explains_signal' method. */
12309
12310 static int
12311 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12312 {
12313 return 1;
12314 }
12315
12316 /* The default "after_condition_true" method. */
12317
12318 static void
12319 base_breakpoint_after_condition_true (struct bpstats *bs)
12320 {
12321 /* Nothing to do. */
12322 }
12323
12324 struct breakpoint_ops base_breakpoint_ops =
12325 {
12326 base_breakpoint_allocate_location,
12327 base_breakpoint_re_set,
12328 base_breakpoint_insert_location,
12329 base_breakpoint_remove_location,
12330 base_breakpoint_breakpoint_hit,
12331 base_breakpoint_check_status,
12332 base_breakpoint_resources_needed,
12333 base_breakpoint_works_in_software_mode,
12334 base_breakpoint_print_it,
12335 NULL,
12336 base_breakpoint_print_one_detail,
12337 base_breakpoint_print_mention,
12338 base_breakpoint_print_recreate,
12339 base_breakpoint_create_sals_from_location,
12340 base_breakpoint_create_breakpoints_sal,
12341 base_breakpoint_decode_location,
12342 base_breakpoint_explains_signal,
12343 base_breakpoint_after_condition_true,
12344 };
12345
12346 /* Default breakpoint_ops methods. */
12347
12348 static void
12349 bkpt_re_set (struct breakpoint *b)
12350 {
12351 /* FIXME: is this still reachable? */
12352 if (breakpoint_event_location_empty_p (b))
12353 {
12354 /* Anything without a location can't be re-set. */
12355 delete_breakpoint (b);
12356 return;
12357 }
12358
12359 breakpoint_re_set_default (b);
12360 }
12361
12362 static int
12363 bkpt_insert_location (struct bp_location *bl)
12364 {
12365 CORE_ADDR addr = bl->target_info.reqstd_address;
12366
12367 bl->target_info.kind = breakpoint_kind (bl, &addr);
12368 bl->target_info.placed_address = addr;
12369
12370 if (bl->loc_type == bp_loc_hardware_breakpoint)
12371 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12372 else
12373 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12374 }
12375
12376 static int
12377 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12378 {
12379 if (bl->loc_type == bp_loc_hardware_breakpoint)
12380 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12381 else
12382 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12383 }
12384
12385 static int
12386 bkpt_breakpoint_hit (const struct bp_location *bl,
12387 const address_space *aspace, CORE_ADDR bp_addr,
12388 const struct target_waitstatus *ws)
12389 {
12390 if (ws->kind != TARGET_WAITKIND_STOPPED
12391 || ws->value.sig != GDB_SIGNAL_TRAP)
12392 return 0;
12393
12394 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12395 aspace, bp_addr))
12396 return 0;
12397
12398 if (overlay_debugging /* unmapped overlay section */
12399 && section_is_overlay (bl->section)
12400 && !section_is_mapped (bl->section))
12401 return 0;
12402
12403 return 1;
12404 }
12405
12406 static int
12407 dprintf_breakpoint_hit (const struct bp_location *bl,
12408 const address_space *aspace, CORE_ADDR bp_addr,
12409 const struct target_waitstatus *ws)
12410 {
12411 if (dprintf_style == dprintf_style_agent
12412 && target_can_run_breakpoint_commands ())
12413 {
12414 /* An agent-style dprintf never causes a stop. If we see a trap
12415 for this address it must be for a breakpoint that happens to
12416 be set at the same address. */
12417 return 0;
12418 }
12419
12420 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12421 }
12422
12423 static int
12424 bkpt_resources_needed (const struct bp_location *bl)
12425 {
12426 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12427
12428 return 1;
12429 }
12430
12431 static enum print_stop_action
12432 bkpt_print_it (bpstat bs)
12433 {
12434 struct breakpoint *b;
12435 const struct bp_location *bl;
12436 int bp_temp;
12437 struct ui_out *uiout = current_uiout;
12438
12439 gdb_assert (bs->bp_location_at != NULL);
12440
12441 bl = bs->bp_location_at;
12442 b = bs->breakpoint_at;
12443
12444 bp_temp = b->disposition == disp_del;
12445 if (bl->address != bl->requested_address)
12446 breakpoint_adjustment_warning (bl->requested_address,
12447 bl->address,
12448 b->number, 1);
12449 annotate_breakpoint (b->number);
12450 maybe_print_thread_hit_breakpoint (uiout);
12451
12452 if (uiout->is_mi_like_p ())
12453 {
12454 uiout->field_string ("reason",
12455 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12456 uiout->field_string ("disp", bpdisp_text (b->disposition));
12457 }
12458 if (bp_temp)
12459 uiout->message ("Temporary breakpoint %pF, ",
12460 signed_field ("bkptno", b->number));
12461 else
12462 uiout->message ("Breakpoint %pF, ",
12463 signed_field ("bkptno", b->number));
12464
12465 return PRINT_SRC_AND_LOC;
12466 }
12467
12468 static void
12469 bkpt_print_mention (struct breakpoint *b)
12470 {
12471 if (current_uiout->is_mi_like_p ())
12472 return;
12473
12474 switch (b->type)
12475 {
12476 case bp_breakpoint:
12477 case bp_gnu_ifunc_resolver:
12478 if (b->disposition == disp_del)
12479 printf_filtered (_("Temporary breakpoint"));
12480 else
12481 printf_filtered (_("Breakpoint"));
12482 printf_filtered (_(" %d"), b->number);
12483 if (b->type == bp_gnu_ifunc_resolver)
12484 printf_filtered (_(" at gnu-indirect-function resolver"));
12485 break;
12486 case bp_hardware_breakpoint:
12487 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12488 break;
12489 case bp_dprintf:
12490 printf_filtered (_("Dprintf %d"), b->number);
12491 break;
12492 }
12493
12494 say_where (b);
12495 }
12496
12497 static void
12498 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12499 {
12500 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12501 fprintf_unfiltered (fp, "tbreak");
12502 else if (tp->type == bp_breakpoint)
12503 fprintf_unfiltered (fp, "break");
12504 else if (tp->type == bp_hardware_breakpoint
12505 && tp->disposition == disp_del)
12506 fprintf_unfiltered (fp, "thbreak");
12507 else if (tp->type == bp_hardware_breakpoint)
12508 fprintf_unfiltered (fp, "hbreak");
12509 else
12510 internal_error (__FILE__, __LINE__,
12511 _("unhandled breakpoint type %d"), (int) tp->type);
12512
12513 fprintf_unfiltered (fp, " %s",
12514 event_location_to_string (tp->location.get ()));
12515
12516 /* Print out extra_string if this breakpoint is pending. It might
12517 contain, for example, conditions that were set by the user. */
12518 if (tp->loc == NULL && tp->extra_string != NULL)
12519 fprintf_unfiltered (fp, " %s", tp->extra_string);
12520
12521 print_recreate_thread (tp, fp);
12522 }
12523
12524 static void
12525 bkpt_create_sals_from_location (const struct event_location *location,
12526 struct linespec_result *canonical,
12527 enum bptype type_wanted)
12528 {
12529 create_sals_from_location_default (location, canonical, type_wanted);
12530 }
12531
12532 static void
12533 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12534 struct linespec_result *canonical,
12535 gdb::unique_xmalloc_ptr<char> cond_string,
12536 gdb::unique_xmalloc_ptr<char> extra_string,
12537 enum bptype type_wanted,
12538 enum bpdisp disposition,
12539 int thread,
12540 int task, int ignore_count,
12541 const struct breakpoint_ops *ops,
12542 int from_tty, int enabled,
12543 int internal, unsigned flags)
12544 {
12545 create_breakpoints_sal_default (gdbarch, canonical,
12546 std::move (cond_string),
12547 std::move (extra_string),
12548 type_wanted,
12549 disposition, thread, task,
12550 ignore_count, ops, from_tty,
12551 enabled, internal, flags);
12552 }
12553
12554 static std::vector<symtab_and_line>
12555 bkpt_decode_location (struct breakpoint *b,
12556 const struct event_location *location,
12557 struct program_space *search_pspace)
12558 {
12559 return decode_location_default (b, location, search_pspace);
12560 }
12561
12562 /* Virtual table for internal breakpoints. */
12563
12564 static void
12565 internal_bkpt_re_set (struct breakpoint *b)
12566 {
12567 switch (b->type)
12568 {
12569 /* Delete overlay event and longjmp master breakpoints; they
12570 will be reset later by breakpoint_re_set. */
12571 case bp_overlay_event:
12572 case bp_longjmp_master:
12573 case bp_std_terminate_master:
12574 case bp_exception_master:
12575 delete_breakpoint (b);
12576 break;
12577
12578 /* This breakpoint is special, it's set up when the inferior
12579 starts and we really don't want to touch it. */
12580 case bp_shlib_event:
12581
12582 /* Like bp_shlib_event, this breakpoint type is special. Once
12583 it is set up, we do not want to touch it. */
12584 case bp_thread_event:
12585 break;
12586 }
12587 }
12588
12589 static void
12590 internal_bkpt_check_status (bpstat bs)
12591 {
12592 if (bs->breakpoint_at->type == bp_shlib_event)
12593 {
12594 /* If requested, stop when the dynamic linker notifies GDB of
12595 events. This allows the user to get control and place
12596 breakpoints in initializer routines for dynamically loaded
12597 objects (among other things). */
12598 bs->stop = stop_on_solib_events;
12599 bs->print = stop_on_solib_events;
12600 }
12601 else
12602 bs->stop = 0;
12603 }
12604
12605 static enum print_stop_action
12606 internal_bkpt_print_it (bpstat bs)
12607 {
12608 struct breakpoint *b;
12609
12610 b = bs->breakpoint_at;
12611
12612 switch (b->type)
12613 {
12614 case bp_shlib_event:
12615 /* Did we stop because the user set the stop_on_solib_events
12616 variable? (If so, we report this as a generic, "Stopped due
12617 to shlib event" message.) */
12618 print_solib_event (0);
12619 break;
12620
12621 case bp_thread_event:
12622 /* Not sure how we will get here.
12623 GDB should not stop for these breakpoints. */
12624 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12625 break;
12626
12627 case bp_overlay_event:
12628 /* By analogy with the thread event, GDB should not stop for these. */
12629 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12630 break;
12631
12632 case bp_longjmp_master:
12633 /* These should never be enabled. */
12634 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12635 break;
12636
12637 case bp_std_terminate_master:
12638 /* These should never be enabled. */
12639 printf_filtered (_("std::terminate Master Breakpoint: "
12640 "gdb should not stop!\n"));
12641 break;
12642
12643 case bp_exception_master:
12644 /* These should never be enabled. */
12645 printf_filtered (_("Exception Master Breakpoint: "
12646 "gdb should not stop!\n"));
12647 break;
12648 }
12649
12650 return PRINT_NOTHING;
12651 }
12652
12653 static void
12654 internal_bkpt_print_mention (struct breakpoint *b)
12655 {
12656 /* Nothing to mention. These breakpoints are internal. */
12657 }
12658
12659 /* Virtual table for momentary breakpoints */
12660
12661 static void
12662 momentary_bkpt_re_set (struct breakpoint *b)
12663 {
12664 /* Keep temporary breakpoints, which can be encountered when we step
12665 over a dlopen call and solib_add is resetting the breakpoints.
12666 Otherwise these should have been blown away via the cleanup chain
12667 or by breakpoint_init_inferior when we rerun the executable. */
12668 }
12669
12670 static void
12671 momentary_bkpt_check_status (bpstat bs)
12672 {
12673 /* Nothing. The point of these breakpoints is causing a stop. */
12674 }
12675
12676 static enum print_stop_action
12677 momentary_bkpt_print_it (bpstat bs)
12678 {
12679 return PRINT_UNKNOWN;
12680 }
12681
12682 static void
12683 momentary_bkpt_print_mention (struct breakpoint *b)
12684 {
12685 /* Nothing to mention. These breakpoints are internal. */
12686 }
12687
12688 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12689
12690 It gets cleared already on the removal of the first one of such placed
12691 breakpoints. This is OK as they get all removed altogether. */
12692
12693 longjmp_breakpoint::~longjmp_breakpoint ()
12694 {
12695 thread_info *tp = find_thread_global_id (this->thread);
12696
12697 if (tp != NULL)
12698 tp->initiating_frame = null_frame_id;
12699 }
12700
12701 /* Specific methods for probe breakpoints. */
12702
12703 static int
12704 bkpt_probe_insert_location (struct bp_location *bl)
12705 {
12706 int v = bkpt_insert_location (bl);
12707
12708 if (v == 0)
12709 {
12710 /* The insertion was successful, now let's set the probe's semaphore
12711 if needed. */
12712 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
12713 }
12714
12715 return v;
12716 }
12717
12718 static int
12719 bkpt_probe_remove_location (struct bp_location *bl,
12720 enum remove_bp_reason reason)
12721 {
12722 /* Let's clear the semaphore before removing the location. */
12723 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
12724
12725 return bkpt_remove_location (bl, reason);
12726 }
12727
12728 static void
12729 bkpt_probe_create_sals_from_location (const struct event_location *location,
12730 struct linespec_result *canonical,
12731 enum bptype type_wanted)
12732 {
12733 struct linespec_sals lsal;
12734
12735 lsal.sals = parse_probes (location, NULL, canonical);
12736 lsal.canonical
12737 = xstrdup (event_location_to_string (canonical->location.get ()));
12738 canonical->lsals.push_back (std::move (lsal));
12739 }
12740
12741 static std::vector<symtab_and_line>
12742 bkpt_probe_decode_location (struct breakpoint *b,
12743 const struct event_location *location,
12744 struct program_space *search_pspace)
12745 {
12746 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12747 if (sals.empty ())
12748 error (_("probe not found"));
12749 return sals;
12750 }
12751
12752 /* The breakpoint_ops structure to be used in tracepoints. */
12753
12754 static void
12755 tracepoint_re_set (struct breakpoint *b)
12756 {
12757 breakpoint_re_set_default (b);
12758 }
12759
12760 static int
12761 tracepoint_breakpoint_hit (const struct bp_location *bl,
12762 const address_space *aspace, CORE_ADDR bp_addr,
12763 const struct target_waitstatus *ws)
12764 {
12765 /* By definition, the inferior does not report stops at
12766 tracepoints. */
12767 return 0;
12768 }
12769
12770 static void
12771 tracepoint_print_one_detail (const struct breakpoint *self,
12772 struct ui_out *uiout)
12773 {
12774 struct tracepoint *tp = (struct tracepoint *) self;
12775 if (!tp->static_trace_marker_id.empty ())
12776 {
12777 gdb_assert (self->type == bp_static_tracepoint);
12778
12779 uiout->message ("\tmarker id is %pF\n",
12780 string_field ("static-tracepoint-marker-string-id",
12781 tp->static_trace_marker_id.c_str ()));
12782 }
12783 }
12784
12785 static void
12786 tracepoint_print_mention (struct breakpoint *b)
12787 {
12788 if (current_uiout->is_mi_like_p ())
12789 return;
12790
12791 switch (b->type)
12792 {
12793 case bp_tracepoint:
12794 printf_filtered (_("Tracepoint"));
12795 printf_filtered (_(" %d"), b->number);
12796 break;
12797 case bp_fast_tracepoint:
12798 printf_filtered (_("Fast tracepoint"));
12799 printf_filtered (_(" %d"), b->number);
12800 break;
12801 case bp_static_tracepoint:
12802 printf_filtered (_("Static tracepoint"));
12803 printf_filtered (_(" %d"), b->number);
12804 break;
12805 default:
12806 internal_error (__FILE__, __LINE__,
12807 _("unhandled tracepoint type %d"), (int) b->type);
12808 }
12809
12810 say_where (b);
12811 }
12812
12813 static void
12814 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
12815 {
12816 struct tracepoint *tp = (struct tracepoint *) self;
12817
12818 if (self->type == bp_fast_tracepoint)
12819 fprintf_unfiltered (fp, "ftrace");
12820 else if (self->type == bp_static_tracepoint)
12821 fprintf_unfiltered (fp, "strace");
12822 else if (self->type == bp_tracepoint)
12823 fprintf_unfiltered (fp, "trace");
12824 else
12825 internal_error (__FILE__, __LINE__,
12826 _("unhandled tracepoint type %d"), (int) self->type);
12827
12828 fprintf_unfiltered (fp, " %s",
12829 event_location_to_string (self->location.get ()));
12830 print_recreate_thread (self, fp);
12831
12832 if (tp->pass_count)
12833 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
12834 }
12835
12836 static void
12837 tracepoint_create_sals_from_location (const struct event_location *location,
12838 struct linespec_result *canonical,
12839 enum bptype type_wanted)
12840 {
12841 create_sals_from_location_default (location, canonical, type_wanted);
12842 }
12843
12844 static void
12845 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12846 struct linespec_result *canonical,
12847 gdb::unique_xmalloc_ptr<char> cond_string,
12848 gdb::unique_xmalloc_ptr<char> extra_string,
12849 enum bptype type_wanted,
12850 enum bpdisp disposition,
12851 int thread,
12852 int task, int ignore_count,
12853 const struct breakpoint_ops *ops,
12854 int from_tty, int enabled,
12855 int internal, unsigned flags)
12856 {
12857 create_breakpoints_sal_default (gdbarch, canonical,
12858 std::move (cond_string),
12859 std::move (extra_string),
12860 type_wanted,
12861 disposition, thread, task,
12862 ignore_count, ops, from_tty,
12863 enabled, internal, flags);
12864 }
12865
12866 static std::vector<symtab_and_line>
12867 tracepoint_decode_location (struct breakpoint *b,
12868 const struct event_location *location,
12869 struct program_space *search_pspace)
12870 {
12871 return decode_location_default (b, location, search_pspace);
12872 }
12873
12874 struct breakpoint_ops tracepoint_breakpoint_ops;
12875
12876 /* Virtual table for tracepoints on static probes. */
12877
12878 static void
12879 tracepoint_probe_create_sals_from_location
12880 (const struct event_location *location,
12881 struct linespec_result *canonical,
12882 enum bptype type_wanted)
12883 {
12884 /* We use the same method for breakpoint on probes. */
12885 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
12886 }
12887
12888 static std::vector<symtab_and_line>
12889 tracepoint_probe_decode_location (struct breakpoint *b,
12890 const struct event_location *location,
12891 struct program_space *search_pspace)
12892 {
12893 /* We use the same method for breakpoint on probes. */
12894 return bkpt_probe_decode_location (b, location, search_pspace);
12895 }
12896
12897 /* Dprintf breakpoint_ops methods. */
12898
12899 static void
12900 dprintf_re_set (struct breakpoint *b)
12901 {
12902 breakpoint_re_set_default (b);
12903
12904 /* extra_string should never be non-NULL for dprintf. */
12905 gdb_assert (b->extra_string != NULL);
12906
12907 /* 1 - connect to target 1, that can run breakpoint commands.
12908 2 - create a dprintf, which resolves fine.
12909 3 - disconnect from target 1
12910 4 - connect to target 2, that can NOT run breakpoint commands.
12911
12912 After steps #3/#4, you'll want the dprintf command list to
12913 be updated, because target 1 and 2 may well return different
12914 answers for target_can_run_breakpoint_commands().
12915 Given absence of finer grained resetting, we get to do
12916 it all the time. */
12917 if (b->extra_string != NULL)
12918 update_dprintf_command_list (b);
12919 }
12920
12921 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
12922
12923 static void
12924 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12925 {
12926 fprintf_unfiltered (fp, "dprintf %s,%s",
12927 event_location_to_string (tp->location.get ()),
12928 tp->extra_string);
12929 print_recreate_thread (tp, fp);
12930 }
12931
12932 /* Implement the "after_condition_true" breakpoint_ops method for
12933 dprintf.
12934
12935 dprintf's are implemented with regular commands in their command
12936 list, but we run the commands here instead of before presenting the
12937 stop to the user, as dprintf's don't actually cause a stop. This
12938 also makes it so that the commands of multiple dprintfs at the same
12939 address are all handled. */
12940
12941 static void
12942 dprintf_after_condition_true (struct bpstats *bs)
12943 {
12944 struct bpstats tmp_bs;
12945 struct bpstats *tmp_bs_p = &tmp_bs;
12946
12947 /* dprintf's never cause a stop. This wasn't set in the
12948 check_status hook instead because that would make the dprintf's
12949 condition not be evaluated. */
12950 bs->stop = 0;
12951
12952 /* Run the command list here. Take ownership of it instead of
12953 copying. We never want these commands to run later in
12954 bpstat_do_actions, if a breakpoint that causes a stop happens to
12955 be set at same address as this dprintf, or even if running the
12956 commands here throws. */
12957 tmp_bs.commands = bs->commands;
12958 bs->commands = NULL;
12959
12960 bpstat_do_actions_1 (&tmp_bs_p);
12961
12962 /* 'tmp_bs.commands' will usually be NULL by now, but
12963 bpstat_do_actions_1 may return early without processing the whole
12964 list. */
12965 }
12966
12967 /* The breakpoint_ops structure to be used on static tracepoints with
12968 markers (`-m'). */
12969
12970 static void
12971 strace_marker_create_sals_from_location (const struct event_location *location,
12972 struct linespec_result *canonical,
12973 enum bptype type_wanted)
12974 {
12975 struct linespec_sals lsal;
12976 const char *arg_start, *arg;
12977
12978 arg = arg_start = get_linespec_location (location)->spec_string;
12979 lsal.sals = decode_static_tracepoint_spec (&arg);
12980
12981 std::string str (arg_start, arg - arg_start);
12982 const char *ptr = str.c_str ();
12983 canonical->location
12984 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
12985
12986 lsal.canonical
12987 = xstrdup (event_location_to_string (canonical->location.get ()));
12988 canonical->lsals.push_back (std::move (lsal));
12989 }
12990
12991 static void
12992 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12993 struct linespec_result *canonical,
12994 gdb::unique_xmalloc_ptr<char> cond_string,
12995 gdb::unique_xmalloc_ptr<char> extra_string,
12996 enum bptype type_wanted,
12997 enum bpdisp disposition,
12998 int thread,
12999 int task, int ignore_count,
13000 const struct breakpoint_ops *ops,
13001 int from_tty, int enabled,
13002 int internal, unsigned flags)
13003 {
13004 const linespec_sals &lsal = canonical->lsals[0];
13005
13006 /* If the user is creating a static tracepoint by marker id
13007 (strace -m MARKER_ID), then store the sals index, so that
13008 breakpoint_re_set can try to match up which of the newly
13009 found markers corresponds to this one, and, don't try to
13010 expand multiple locations for each sal, given than SALS
13011 already should contain all sals for MARKER_ID. */
13012
13013 for (size_t i = 0; i < lsal.sals.size (); i++)
13014 {
13015 event_location_up location
13016 = copy_event_location (canonical->location.get ());
13017
13018 std::unique_ptr<tracepoint> tp (new tracepoint ());
13019 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
13020 std::move (location), NULL,
13021 std::move (cond_string),
13022 std::move (extra_string),
13023 type_wanted, disposition,
13024 thread, task, ignore_count, ops,
13025 from_tty, enabled, internal, flags,
13026 canonical->special_display);
13027 /* Given that its possible to have multiple markers with
13028 the same string id, if the user is creating a static
13029 tracepoint by marker id ("strace -m MARKER_ID"), then
13030 store the sals index, so that breakpoint_re_set can
13031 try to match up which of the newly found markers
13032 corresponds to this one */
13033 tp->static_trace_marker_id_idx = i;
13034
13035 install_breakpoint (internal, std::move (tp), 0);
13036 }
13037 }
13038
13039 static std::vector<symtab_and_line>
13040 strace_marker_decode_location (struct breakpoint *b,
13041 const struct event_location *location,
13042 struct program_space *search_pspace)
13043 {
13044 struct tracepoint *tp = (struct tracepoint *) b;
13045 const char *s = get_linespec_location (location)->spec_string;
13046
13047 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13048 if (sals.size () > tp->static_trace_marker_id_idx)
13049 {
13050 sals[0] = sals[tp->static_trace_marker_id_idx];
13051 sals.resize (1);
13052 return sals;
13053 }
13054 else
13055 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
13056 }
13057
13058 static struct breakpoint_ops strace_marker_breakpoint_ops;
13059
13060 static int
13061 strace_marker_p (struct breakpoint *b)
13062 {
13063 return b->ops == &strace_marker_breakpoint_ops;
13064 }
13065
13066 /* Delete a breakpoint and clean up all traces of it in the data
13067 structures. */
13068
13069 void
13070 delete_breakpoint (struct breakpoint *bpt)
13071 {
13072 struct breakpoint *b;
13073
13074 gdb_assert (bpt != NULL);
13075
13076 /* Has this bp already been deleted? This can happen because
13077 multiple lists can hold pointers to bp's. bpstat lists are
13078 especial culprits.
13079
13080 One example of this happening is a watchpoint's scope bp. When
13081 the scope bp triggers, we notice that the watchpoint is out of
13082 scope, and delete it. We also delete its scope bp. But the
13083 scope bp is marked "auto-deleting", and is already on a bpstat.
13084 That bpstat is then checked for auto-deleting bp's, which are
13085 deleted.
13086
13087 A real solution to this problem might involve reference counts in
13088 bp's, and/or giving them pointers back to their referencing
13089 bpstat's, and teaching delete_breakpoint to only free a bp's
13090 storage when no more references were extent. A cheaper bandaid
13091 was chosen. */
13092 if (bpt->type == bp_none)
13093 return;
13094
13095 /* At least avoid this stale reference until the reference counting
13096 of breakpoints gets resolved. */
13097 if (bpt->related_breakpoint != bpt)
13098 {
13099 struct breakpoint *related;
13100 struct watchpoint *w;
13101
13102 if (bpt->type == bp_watchpoint_scope)
13103 w = (struct watchpoint *) bpt->related_breakpoint;
13104 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13105 w = (struct watchpoint *) bpt;
13106 else
13107 w = NULL;
13108 if (w != NULL)
13109 watchpoint_del_at_next_stop (w);
13110
13111 /* Unlink bpt from the bpt->related_breakpoint ring. */
13112 for (related = bpt; related->related_breakpoint != bpt;
13113 related = related->related_breakpoint);
13114 related->related_breakpoint = bpt->related_breakpoint;
13115 bpt->related_breakpoint = bpt;
13116 }
13117
13118 /* watch_command_1 creates a watchpoint but only sets its number if
13119 update_watchpoint succeeds in creating its bp_locations. If there's
13120 a problem in that process, we'll be asked to delete the half-created
13121 watchpoint. In that case, don't announce the deletion. */
13122 if (bpt->number)
13123 gdb::observers::breakpoint_deleted.notify (bpt);
13124
13125 if (breakpoint_chain == bpt)
13126 breakpoint_chain = bpt->next;
13127
13128 ALL_BREAKPOINTS (b)
13129 if (b->next == bpt)
13130 {
13131 b->next = bpt->next;
13132 break;
13133 }
13134
13135 /* Be sure no bpstat's are pointing at the breakpoint after it's
13136 been freed. */
13137 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13138 in all threads for now. Note that we cannot just remove bpstats
13139 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13140 commands are associated with the bpstat; if we remove it here,
13141 then the later call to bpstat_do_actions (&stop_bpstat); in
13142 event-top.c won't do anything, and temporary breakpoints with
13143 commands won't work. */
13144
13145 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13146
13147 /* Now that breakpoint is removed from breakpoint list, update the
13148 global location list. This will remove locations that used to
13149 belong to this breakpoint. Do this before freeing the breakpoint
13150 itself, since remove_breakpoint looks at location's owner. It
13151 might be better design to have location completely
13152 self-contained, but it's not the case now. */
13153 update_global_location_list (UGLL_DONT_INSERT);
13154
13155 /* On the chance that someone will soon try again to delete this
13156 same bp, we mark it as deleted before freeing its storage. */
13157 bpt->type = bp_none;
13158 delete bpt;
13159 }
13160
13161 /* Iterator function to call a user-provided callback function once
13162 for each of B and its related breakpoints. */
13163
13164 static void
13165 iterate_over_related_breakpoints (struct breakpoint *b,
13166 gdb::function_view<void (breakpoint *)> function)
13167 {
13168 struct breakpoint *related;
13169
13170 related = b;
13171 do
13172 {
13173 struct breakpoint *next;
13174
13175 /* FUNCTION may delete RELATED. */
13176 next = related->related_breakpoint;
13177
13178 if (next == related)
13179 {
13180 /* RELATED is the last ring entry. */
13181 function (related);
13182
13183 /* FUNCTION may have deleted it, so we'd never reach back to
13184 B. There's nothing left to do anyway, so just break
13185 out. */
13186 break;
13187 }
13188 else
13189 function (related);
13190
13191 related = next;
13192 }
13193 while (related != b);
13194 }
13195
13196 static void
13197 delete_command (const char *arg, int from_tty)
13198 {
13199 struct breakpoint *b, *b_tmp;
13200
13201 dont_repeat ();
13202
13203 if (arg == 0)
13204 {
13205 int breaks_to_delete = 0;
13206
13207 /* Delete all breakpoints if no argument. Do not delete
13208 internal breakpoints, these have to be deleted with an
13209 explicit breakpoint number argument. */
13210 ALL_BREAKPOINTS (b)
13211 if (user_breakpoint_p (b))
13212 {
13213 breaks_to_delete = 1;
13214 break;
13215 }
13216
13217 /* Ask user only if there are some breakpoints to delete. */
13218 if (!from_tty
13219 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13220 {
13221 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13222 if (user_breakpoint_p (b))
13223 delete_breakpoint (b);
13224 }
13225 }
13226 else
13227 map_breakpoint_numbers
13228 (arg, [&] (breakpoint *br)
13229 {
13230 iterate_over_related_breakpoints (br, delete_breakpoint);
13231 });
13232 }
13233
13234 /* Return true if all locations of B bound to PSPACE are pending. If
13235 PSPACE is NULL, all locations of all program spaces are
13236 considered. */
13237
13238 static int
13239 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13240 {
13241 struct bp_location *loc;
13242
13243 for (loc = b->loc; loc != NULL; loc = loc->next)
13244 if ((pspace == NULL
13245 || loc->pspace == pspace)
13246 && !loc->shlib_disabled
13247 && !loc->pspace->executing_startup)
13248 return 0;
13249 return 1;
13250 }
13251
13252 /* Subroutine of update_breakpoint_locations to simplify it.
13253 Return non-zero if multiple fns in list LOC have the same name.
13254 Null names are ignored. */
13255
13256 static int
13257 ambiguous_names_p (struct bp_location *loc)
13258 {
13259 struct bp_location *l;
13260 htab_t htab = htab_create_alloc (13, htab_hash_string, streq_hash, NULL,
13261 xcalloc, xfree);
13262
13263 for (l = loc; l != NULL; l = l->next)
13264 {
13265 const char **slot;
13266 const char *name = l->function_name;
13267
13268 /* Allow for some names to be NULL, ignore them. */
13269 if (name == NULL)
13270 continue;
13271
13272 slot = (const char **) htab_find_slot (htab, (const void *) name,
13273 INSERT);
13274 /* NOTE: We can assume slot != NULL here because xcalloc never
13275 returns NULL. */
13276 if (*slot != NULL)
13277 {
13278 htab_delete (htab);
13279 return 1;
13280 }
13281 *slot = name;
13282 }
13283
13284 htab_delete (htab);
13285 return 0;
13286 }
13287
13288 /* When symbols change, it probably means the sources changed as well,
13289 and it might mean the static tracepoint markers are no longer at
13290 the same address or line numbers they used to be at last we
13291 checked. Losing your static tracepoints whenever you rebuild is
13292 undesirable. This function tries to resync/rematch gdb static
13293 tracepoints with the markers on the target, for static tracepoints
13294 that have not been set by marker id. Static tracepoint that have
13295 been set by marker id are reset by marker id in breakpoint_re_set.
13296 The heuristic is:
13297
13298 1) For a tracepoint set at a specific address, look for a marker at
13299 the old PC. If one is found there, assume to be the same marker.
13300 If the name / string id of the marker found is different from the
13301 previous known name, assume that means the user renamed the marker
13302 in the sources, and output a warning.
13303
13304 2) For a tracepoint set at a given line number, look for a marker
13305 at the new address of the old line number. If one is found there,
13306 assume to be the same marker. If the name / string id of the
13307 marker found is different from the previous known name, assume that
13308 means the user renamed the marker in the sources, and output a
13309 warning.
13310
13311 3) If a marker is no longer found at the same address or line, it
13312 may mean the marker no longer exists. But it may also just mean
13313 the code changed a bit. Maybe the user added a few lines of code
13314 that made the marker move up or down (in line number terms). Ask
13315 the target for info about the marker with the string id as we knew
13316 it. If found, update line number and address in the matching
13317 static tracepoint. This will get confused if there's more than one
13318 marker with the same ID (possible in UST, although unadvised
13319 precisely because it confuses tools). */
13320
13321 static struct symtab_and_line
13322 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13323 {
13324 struct tracepoint *tp = (struct tracepoint *) b;
13325 struct static_tracepoint_marker marker;
13326 CORE_ADDR pc;
13327
13328 pc = sal.pc;
13329 if (sal.line)
13330 find_line_pc (sal.symtab, sal.line, &pc);
13331
13332 if (target_static_tracepoint_marker_at (pc, &marker))
13333 {
13334 if (tp->static_trace_marker_id != marker.str_id)
13335 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13336 b->number, tp->static_trace_marker_id.c_str (),
13337 marker.str_id.c_str ());
13338
13339 tp->static_trace_marker_id = std::move (marker.str_id);
13340
13341 return sal;
13342 }
13343
13344 /* Old marker wasn't found on target at lineno. Try looking it up
13345 by string ID. */
13346 if (!sal.explicit_pc
13347 && sal.line != 0
13348 && sal.symtab != NULL
13349 && !tp->static_trace_marker_id.empty ())
13350 {
13351 std::vector<static_tracepoint_marker> markers
13352 = target_static_tracepoint_markers_by_strid
13353 (tp->static_trace_marker_id.c_str ());
13354
13355 if (!markers.empty ())
13356 {
13357 struct symbol *sym;
13358 struct static_tracepoint_marker *tpmarker;
13359 struct ui_out *uiout = current_uiout;
13360 struct explicit_location explicit_loc;
13361
13362 tpmarker = &markers[0];
13363
13364 tp->static_trace_marker_id = std::move (tpmarker->str_id);
13365
13366 warning (_("marker for static tracepoint %d (%s) not "
13367 "found at previous line number"),
13368 b->number, tp->static_trace_marker_id.c_str ());
13369
13370 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
13371 sym = find_pc_sect_function (tpmarker->address, NULL);
13372 uiout->text ("Now in ");
13373 if (sym)
13374 {
13375 uiout->field_string ("func", sym->print_name (),
13376 function_name_style.style ());
13377 uiout->text (" at ");
13378 }
13379 uiout->field_string ("file",
13380 symtab_to_filename_for_display (sal2.symtab),
13381 file_name_style.style ());
13382 uiout->text (":");
13383
13384 if (uiout->is_mi_like_p ())
13385 {
13386 const char *fullname = symtab_to_fullname (sal2.symtab);
13387
13388 uiout->field_string ("fullname", fullname);
13389 }
13390
13391 uiout->field_signed ("line", sal2.line);
13392 uiout->text ("\n");
13393
13394 b->loc->line_number = sal2.line;
13395 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13396
13397 b->location.reset (NULL);
13398 initialize_explicit_location (&explicit_loc);
13399 explicit_loc.source_filename
13400 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13401 explicit_loc.line_offset.offset = b->loc->line_number;
13402 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13403 b->location = new_explicit_location (&explicit_loc);
13404
13405 /* Might be nice to check if function changed, and warn if
13406 so. */
13407 }
13408 }
13409 return sal;
13410 }
13411
13412 /* Returns 1 iff locations A and B are sufficiently same that
13413 we don't need to report breakpoint as changed. */
13414
13415 static int
13416 locations_are_equal (struct bp_location *a, struct bp_location *b)
13417 {
13418 while (a && b)
13419 {
13420 if (a->address != b->address)
13421 return 0;
13422
13423 if (a->shlib_disabled != b->shlib_disabled)
13424 return 0;
13425
13426 if (a->enabled != b->enabled)
13427 return 0;
13428
13429 a = a->next;
13430 b = b->next;
13431 }
13432
13433 if ((a == NULL) != (b == NULL))
13434 return 0;
13435
13436 return 1;
13437 }
13438
13439 /* Split all locations of B that are bound to PSPACE out of B's
13440 location list to a separate list and return that list's head. If
13441 PSPACE is NULL, hoist out all locations of B. */
13442
13443 static struct bp_location *
13444 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13445 {
13446 struct bp_location head;
13447 struct bp_location *i = b->loc;
13448 struct bp_location **i_link = &b->loc;
13449 struct bp_location *hoisted = &head;
13450
13451 if (pspace == NULL)
13452 {
13453 i = b->loc;
13454 b->loc = NULL;
13455 return i;
13456 }
13457
13458 head.next = NULL;
13459
13460 while (i != NULL)
13461 {
13462 if (i->pspace == pspace)
13463 {
13464 *i_link = i->next;
13465 i->next = NULL;
13466 hoisted->next = i;
13467 hoisted = i;
13468 }
13469 else
13470 i_link = &i->next;
13471 i = *i_link;
13472 }
13473
13474 return head.next;
13475 }
13476
13477 /* Create new breakpoint locations for B (a hardware or software
13478 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13479 zero, then B is a ranged breakpoint. Only recreates locations for
13480 FILTER_PSPACE. Locations of other program spaces are left
13481 untouched. */
13482
13483 void
13484 update_breakpoint_locations (struct breakpoint *b,
13485 struct program_space *filter_pspace,
13486 gdb::array_view<const symtab_and_line> sals,
13487 gdb::array_view<const symtab_and_line> sals_end)
13488 {
13489 struct bp_location *existing_locations;
13490
13491 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
13492 {
13493 /* Ranged breakpoints have only one start location and one end
13494 location. */
13495 b->enable_state = bp_disabled;
13496 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13497 "multiple locations found\n"),
13498 b->number);
13499 return;
13500 }
13501
13502 /* If there's no new locations, and all existing locations are
13503 pending, don't do anything. This optimizes the common case where
13504 all locations are in the same shared library, that was unloaded.
13505 We'd like to retain the location, so that when the library is
13506 loaded again, we don't loose the enabled/disabled status of the
13507 individual locations. */
13508 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
13509 return;
13510
13511 existing_locations = hoist_existing_locations (b, filter_pspace);
13512
13513 for (const auto &sal : sals)
13514 {
13515 struct bp_location *new_loc;
13516
13517 switch_to_program_space_and_thread (sal.pspace);
13518
13519 new_loc = add_location_to_breakpoint (b, &sal);
13520
13521 /* Reparse conditions, they might contain references to the
13522 old symtab. */
13523 if (b->cond_string != NULL)
13524 {
13525 const char *s;
13526
13527 s = b->cond_string;
13528 try
13529 {
13530 new_loc->cond = parse_exp_1 (&s, sal.pc,
13531 block_for_pc (sal.pc),
13532 0);
13533 }
13534 catch (const gdb_exception_error &e)
13535 {
13536 warning (_("failed to reevaluate condition "
13537 "for breakpoint %d: %s"),
13538 b->number, e.what ());
13539 new_loc->enabled = 0;
13540 }
13541 }
13542
13543 if (!sals_end.empty ())
13544 {
13545 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13546
13547 new_loc->length = end - sals[0].pc + 1;
13548 }
13549 }
13550
13551 /* If possible, carry over 'disable' status from existing
13552 breakpoints. */
13553 {
13554 struct bp_location *e = existing_locations;
13555 /* If there are multiple breakpoints with the same function name,
13556 e.g. for inline functions, comparing function names won't work.
13557 Instead compare pc addresses; this is just a heuristic as things
13558 may have moved, but in practice it gives the correct answer
13559 often enough until a better solution is found. */
13560 int have_ambiguous_names = ambiguous_names_p (b->loc);
13561
13562 for (; e; e = e->next)
13563 {
13564 if (!e->enabled && e->function_name)
13565 {
13566 struct bp_location *l = b->loc;
13567 if (have_ambiguous_names)
13568 {
13569 for (; l; l = l->next)
13570 {
13571 /* Ignore software vs hardware location type at
13572 this point, because with "set breakpoint
13573 auto-hw", after a re-set, locations that were
13574 hardware can end up as software, or vice versa.
13575 As mentioned above, this is an heuristic and in
13576 practice should give the correct answer often
13577 enough. */
13578 if (breakpoint_locations_match (e, l, true))
13579 {
13580 l->enabled = 0;
13581 break;
13582 }
13583 }
13584 }
13585 else
13586 {
13587 for (; l; l = l->next)
13588 if (l->function_name
13589 && strcmp (e->function_name, l->function_name) == 0)
13590 {
13591 l->enabled = 0;
13592 break;
13593 }
13594 }
13595 }
13596 }
13597 }
13598
13599 if (!locations_are_equal (existing_locations, b->loc))
13600 gdb::observers::breakpoint_modified.notify (b);
13601 }
13602
13603 /* Find the SaL locations corresponding to the given LOCATION.
13604 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13605
13606 static std::vector<symtab_and_line>
13607 location_to_sals (struct breakpoint *b, struct event_location *location,
13608 struct program_space *search_pspace, int *found)
13609 {
13610 struct gdb_exception exception;
13611
13612 gdb_assert (b->ops != NULL);
13613
13614 std::vector<symtab_and_line> sals;
13615
13616 try
13617 {
13618 sals = b->ops->decode_location (b, location, search_pspace);
13619 }
13620 catch (gdb_exception_error &e)
13621 {
13622 int not_found_and_ok = 0;
13623
13624 /* For pending breakpoints, it's expected that parsing will
13625 fail until the right shared library is loaded. User has
13626 already told to create pending breakpoints and don't need
13627 extra messages. If breakpoint is in bp_shlib_disabled
13628 state, then user already saw the message about that
13629 breakpoint being disabled, and don't want to see more
13630 errors. */
13631 if (e.error == NOT_FOUND_ERROR
13632 && (b->condition_not_parsed
13633 || (b->loc != NULL
13634 && search_pspace != NULL
13635 && b->loc->pspace != search_pspace)
13636 || (b->loc && b->loc->shlib_disabled)
13637 || (b->loc && b->loc->pspace->executing_startup)
13638 || b->enable_state == bp_disabled))
13639 not_found_and_ok = 1;
13640
13641 if (!not_found_and_ok)
13642 {
13643 /* We surely don't want to warn about the same breakpoint
13644 10 times. One solution, implemented here, is disable
13645 the breakpoint on error. Another solution would be to
13646 have separate 'warning emitted' flag. Since this
13647 happens only when a binary has changed, I don't know
13648 which approach is better. */
13649 b->enable_state = bp_disabled;
13650 throw;
13651 }
13652
13653 exception = std::move (e);
13654 }
13655
13656 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13657 {
13658 for (auto &sal : sals)
13659 resolve_sal_pc (&sal);
13660 if (b->condition_not_parsed && b->extra_string != NULL)
13661 {
13662 char *cond_string, *extra_string;
13663 int thread, task;
13664
13665 find_condition_and_thread (b->extra_string, sals[0].pc,
13666 &cond_string, &thread, &task,
13667 &extra_string);
13668 gdb_assert (b->cond_string == NULL);
13669 if (cond_string)
13670 b->cond_string = cond_string;
13671 b->thread = thread;
13672 b->task = task;
13673 if (extra_string)
13674 {
13675 xfree (b->extra_string);
13676 b->extra_string = extra_string;
13677 }
13678 b->condition_not_parsed = 0;
13679 }
13680
13681 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13682 sals[0] = update_static_tracepoint (b, sals[0]);
13683
13684 *found = 1;
13685 }
13686 else
13687 *found = 0;
13688
13689 return sals;
13690 }
13691
13692 /* The default re_set method, for typical hardware or software
13693 breakpoints. Reevaluate the breakpoint and recreate its
13694 locations. */
13695
13696 static void
13697 breakpoint_re_set_default (struct breakpoint *b)
13698 {
13699 struct program_space *filter_pspace = current_program_space;
13700 std::vector<symtab_and_line> expanded, expanded_end;
13701
13702 int found;
13703 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13704 filter_pspace, &found);
13705 if (found)
13706 expanded = std::move (sals);
13707
13708 if (b->location_range_end != NULL)
13709 {
13710 std::vector<symtab_and_line> sals_end
13711 = location_to_sals (b, b->location_range_end.get (),
13712 filter_pspace, &found);
13713 if (found)
13714 expanded_end = std::move (sals_end);
13715 }
13716
13717 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
13718 }
13719
13720 /* Default method for creating SALs from an address string. It basically
13721 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13722
13723 static void
13724 create_sals_from_location_default (const struct event_location *location,
13725 struct linespec_result *canonical,
13726 enum bptype type_wanted)
13727 {
13728 parse_breakpoint_sals (location, canonical);
13729 }
13730
13731 /* Call create_breakpoints_sal for the given arguments. This is the default
13732 function for the `create_breakpoints_sal' method of
13733 breakpoint_ops. */
13734
13735 static void
13736 create_breakpoints_sal_default (struct gdbarch *gdbarch,
13737 struct linespec_result *canonical,
13738 gdb::unique_xmalloc_ptr<char> cond_string,
13739 gdb::unique_xmalloc_ptr<char> extra_string,
13740 enum bptype type_wanted,
13741 enum bpdisp disposition,
13742 int thread,
13743 int task, int ignore_count,
13744 const struct breakpoint_ops *ops,
13745 int from_tty, int enabled,
13746 int internal, unsigned flags)
13747 {
13748 create_breakpoints_sal (gdbarch, canonical,
13749 std::move (cond_string),
13750 std::move (extra_string),
13751 type_wanted, disposition,
13752 thread, task, ignore_count, ops, from_tty,
13753 enabled, internal, flags);
13754 }
13755
13756 /* Decode the line represented by S by calling decode_line_full. This is the
13757 default function for the `decode_location' method of breakpoint_ops. */
13758
13759 static std::vector<symtab_and_line>
13760 decode_location_default (struct breakpoint *b,
13761 const struct event_location *location,
13762 struct program_space *search_pspace)
13763 {
13764 struct linespec_result canonical;
13765
13766 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
13767 NULL, 0, &canonical, multiple_symbols_all,
13768 b->filter.get ());
13769
13770 /* We should get 0 or 1 resulting SALs. */
13771 gdb_assert (canonical.lsals.size () < 2);
13772
13773 if (!canonical.lsals.empty ())
13774 {
13775 const linespec_sals &lsal = canonical.lsals[0];
13776 return std::move (lsal.sals);
13777 }
13778 return {};
13779 }
13780
13781 /* Reset a breakpoint. */
13782
13783 static void
13784 breakpoint_re_set_one (breakpoint *b)
13785 {
13786 input_radix = b->input_radix;
13787 set_language (b->language);
13788
13789 b->ops->re_set (b);
13790 }
13791
13792 /* Re-set breakpoint locations for the current program space.
13793 Locations bound to other program spaces are left untouched. */
13794
13795 void
13796 breakpoint_re_set (void)
13797 {
13798 struct breakpoint *b, *b_tmp;
13799
13800 {
13801 scoped_restore_current_language save_language;
13802 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
13803 scoped_restore_current_pspace_and_thread restore_pspace_thread;
13804
13805 /* breakpoint_re_set_one sets the current_language to the language
13806 of the breakpoint it is resetting (see prepare_re_set_context)
13807 before re-evaluating the breakpoint's location. This change can
13808 unfortunately get undone by accident if the language_mode is set
13809 to auto, and we either switch frames, or more likely in this context,
13810 we select the current frame.
13811
13812 We prevent this by temporarily turning the language_mode to
13813 language_mode_manual. We restore it once all breakpoints
13814 have been reset. */
13815 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13816 language_mode = language_mode_manual;
13817
13818 /* Note: we must not try to insert locations until after all
13819 breakpoints have been re-set. Otherwise, e.g., when re-setting
13820 breakpoint 1, we'd insert the locations of breakpoint 2, which
13821 hadn't been re-set yet, and thus may have stale locations. */
13822
13823 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13824 {
13825 try
13826 {
13827 breakpoint_re_set_one (b);
13828 }
13829 catch (const gdb_exception &ex)
13830 {
13831 exception_fprintf (gdb_stderr, ex,
13832 "Error in re-setting breakpoint %d: ",
13833 b->number);
13834 }
13835 }
13836
13837 jit_breakpoint_re_set ();
13838 }
13839
13840 create_overlay_event_breakpoint ();
13841 create_longjmp_master_breakpoint ();
13842 create_std_terminate_master_breakpoint ();
13843 create_exception_master_breakpoint ();
13844
13845 /* Now we can insert. */
13846 update_global_location_list (UGLL_MAY_INSERT);
13847 }
13848 \f
13849 /* Reset the thread number of this breakpoint:
13850
13851 - If the breakpoint is for all threads, leave it as-is.
13852 - Else, reset it to the current thread for inferior_ptid. */
13853 void
13854 breakpoint_re_set_thread (struct breakpoint *b)
13855 {
13856 if (b->thread != -1)
13857 {
13858 b->thread = inferior_thread ()->global_num;
13859
13860 /* We're being called after following a fork. The new fork is
13861 selected as current, and unless this was a vfork will have a
13862 different program space from the original thread. Reset that
13863 as well. */
13864 b->loc->pspace = current_program_space;
13865 }
13866 }
13867
13868 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13869 If from_tty is nonzero, it prints a message to that effect,
13870 which ends with a period (no newline). */
13871
13872 void
13873 set_ignore_count (int bptnum, int count, int from_tty)
13874 {
13875 struct breakpoint *b;
13876
13877 if (count < 0)
13878 count = 0;
13879
13880 ALL_BREAKPOINTS (b)
13881 if (b->number == bptnum)
13882 {
13883 if (is_tracepoint (b))
13884 {
13885 if (from_tty && count != 0)
13886 printf_filtered (_("Ignore count ignored for tracepoint %d."),
13887 bptnum);
13888 return;
13889 }
13890
13891 b->ignore_count = count;
13892 if (from_tty)
13893 {
13894 if (count == 0)
13895 printf_filtered (_("Will stop next time "
13896 "breakpoint %d is reached."),
13897 bptnum);
13898 else if (count == 1)
13899 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
13900 bptnum);
13901 else
13902 printf_filtered (_("Will ignore next %d "
13903 "crossings of breakpoint %d."),
13904 count, bptnum);
13905 }
13906 gdb::observers::breakpoint_modified.notify (b);
13907 return;
13908 }
13909
13910 error (_("No breakpoint number %d."), bptnum);
13911 }
13912
13913 /* Command to set ignore-count of breakpoint N to COUNT. */
13914
13915 static void
13916 ignore_command (const char *args, int from_tty)
13917 {
13918 const char *p = args;
13919 int num;
13920
13921 if (p == 0)
13922 error_no_arg (_("a breakpoint number"));
13923
13924 num = get_number (&p);
13925 if (num == 0)
13926 error (_("bad breakpoint number: '%s'"), args);
13927 if (*p == 0)
13928 error (_("Second argument (specified ignore-count) is missing."));
13929
13930 set_ignore_count (num,
13931 longest_to_int (value_as_long (parse_and_eval (p))),
13932 from_tty);
13933 if (from_tty)
13934 printf_filtered ("\n");
13935 }
13936 \f
13937
13938 /* Call FUNCTION on each of the breakpoints with numbers in the range
13939 defined by BP_NUM_RANGE (an inclusive range). */
13940
13941 static void
13942 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13943 gdb::function_view<void (breakpoint *)> function)
13944 {
13945 if (bp_num_range.first == 0)
13946 {
13947 warning (_("bad breakpoint number at or near '%d'"),
13948 bp_num_range.first);
13949 }
13950 else
13951 {
13952 struct breakpoint *b, *tmp;
13953
13954 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
13955 {
13956 bool match = false;
13957
13958 ALL_BREAKPOINTS_SAFE (b, tmp)
13959 if (b->number == i)
13960 {
13961 match = true;
13962 function (b);
13963 break;
13964 }
13965 if (!match)
13966 printf_unfiltered (_("No breakpoint number %d.\n"), i);
13967 }
13968 }
13969 }
13970
13971 /* Call FUNCTION on each of the breakpoints whose numbers are given in
13972 ARGS. */
13973
13974 static void
13975 map_breakpoint_numbers (const char *args,
13976 gdb::function_view<void (breakpoint *)> function)
13977 {
13978 if (args == NULL || *args == '\0')
13979 error_no_arg (_("one or more breakpoint numbers"));
13980
13981 number_or_range_parser parser (args);
13982
13983 while (!parser.finished ())
13984 {
13985 int num = parser.get_number ();
13986 map_breakpoint_number_range (std::make_pair (num, num), function);
13987 }
13988 }
13989
13990 /* Return the breakpoint location structure corresponding to the
13991 BP_NUM and LOC_NUM values. */
13992
13993 static struct bp_location *
13994 find_location_by_number (int bp_num, int loc_num)
13995 {
13996 struct breakpoint *b;
13997
13998 ALL_BREAKPOINTS (b)
13999 if (b->number == bp_num)
14000 {
14001 break;
14002 }
14003
14004 if (!b || b->number != bp_num)
14005 error (_("Bad breakpoint number '%d'"), bp_num);
14006
14007 if (loc_num == 0)
14008 error (_("Bad breakpoint location number '%d'"), loc_num);
14009
14010 int n = 0;
14011 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
14012 if (++n == loc_num)
14013 return loc;
14014
14015 error (_("Bad breakpoint location number '%d'"), loc_num);
14016 }
14017
14018 /* Modes of operation for extract_bp_num. */
14019 enum class extract_bp_kind
14020 {
14021 /* Extracting a breakpoint number. */
14022 bp,
14023
14024 /* Extracting a location number. */
14025 loc,
14026 };
14027
14028 /* Extract a breakpoint or location number (as determined by KIND)
14029 from the string starting at START. TRAILER is a character which
14030 can be found after the number. If you don't want a trailer, use
14031 '\0'. If END_OUT is not NULL, it is set to point after the parsed
14032 string. This always returns a positive integer. */
14033
14034 static int
14035 extract_bp_num (extract_bp_kind kind, const char *start,
14036 int trailer, const char **end_out = NULL)
14037 {
14038 const char *end = start;
14039 int num = get_number_trailer (&end, trailer);
14040 if (num < 0)
14041 error (kind == extract_bp_kind::bp
14042 ? _("Negative breakpoint number '%.*s'")
14043 : _("Negative breakpoint location number '%.*s'"),
14044 int (end - start), start);
14045 if (num == 0)
14046 error (kind == extract_bp_kind::bp
14047 ? _("Bad breakpoint number '%.*s'")
14048 : _("Bad breakpoint location number '%.*s'"),
14049 int (end - start), start);
14050
14051 if (end_out != NULL)
14052 *end_out = end;
14053 return num;
14054 }
14055
14056 /* Extract a breakpoint or location range (as determined by KIND) in
14057 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14058 representing the (inclusive) range. The returned pair's elements
14059 are always positive integers. */
14060
14061 static std::pair<int, int>
14062 extract_bp_or_bp_range (extract_bp_kind kind,
14063 const std::string &arg,
14064 std::string::size_type arg_offset)
14065 {
14066 std::pair<int, int> range;
14067 const char *bp_loc = &arg[arg_offset];
14068 std::string::size_type dash = arg.find ('-', arg_offset);
14069 if (dash != std::string::npos)
14070 {
14071 /* bp_loc is a range (x-z). */
14072 if (arg.length () == dash + 1)
14073 error (kind == extract_bp_kind::bp
14074 ? _("Bad breakpoint number at or near: '%s'")
14075 : _("Bad breakpoint location number at or near: '%s'"),
14076 bp_loc);
14077
14078 const char *end;
14079 const char *start_first = bp_loc;
14080 const char *start_second = &arg[dash + 1];
14081 range.first = extract_bp_num (kind, start_first, '-');
14082 range.second = extract_bp_num (kind, start_second, '\0', &end);
14083
14084 if (range.first > range.second)
14085 error (kind == extract_bp_kind::bp
14086 ? _("Inverted breakpoint range at '%.*s'")
14087 : _("Inverted breakpoint location range at '%.*s'"),
14088 int (end - start_first), start_first);
14089 }
14090 else
14091 {
14092 /* bp_loc is a single value. */
14093 range.first = extract_bp_num (kind, bp_loc, '\0');
14094 range.second = range.first;
14095 }
14096 return range;
14097 }
14098
14099 /* Extract the breakpoint/location range specified by ARG. Returns
14100 the breakpoint range in BP_NUM_RANGE, and the location range in
14101 BP_LOC_RANGE.
14102
14103 ARG may be in any of the following forms:
14104
14105 x where 'x' is a breakpoint number.
14106 x-y where 'x' and 'y' specify a breakpoint numbers range.
14107 x.y where 'x' is a breakpoint number and 'y' a location number.
14108 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14109 location number range.
14110 */
14111
14112 static void
14113 extract_bp_number_and_location (const std::string &arg,
14114 std::pair<int, int> &bp_num_range,
14115 std::pair<int, int> &bp_loc_range)
14116 {
14117 std::string::size_type dot = arg.find ('.');
14118
14119 if (dot != std::string::npos)
14120 {
14121 /* Handle 'x.y' and 'x.y-z' cases. */
14122
14123 if (arg.length () == dot + 1 || dot == 0)
14124 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
14125
14126 bp_num_range.first
14127 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14128 bp_num_range.second = bp_num_range.first;
14129
14130 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14131 arg, dot + 1);
14132 }
14133 else
14134 {
14135 /* Handle x and x-y cases. */
14136
14137 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
14138 bp_loc_range.first = 0;
14139 bp_loc_range.second = 0;
14140 }
14141 }
14142
14143 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14144 specifies whether to enable or disable. */
14145
14146 static void
14147 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14148 {
14149 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14150 if (loc != NULL)
14151 {
14152 if (loc->enabled != enable)
14153 {
14154 loc->enabled = enable;
14155 mark_breakpoint_location_modified (loc);
14156 }
14157 if (target_supports_enable_disable_tracepoint ()
14158 && current_trace_status ()->running && loc->owner
14159 && is_tracepoint (loc->owner))
14160 target_disable_tracepoint (loc);
14161 }
14162 update_global_location_list (UGLL_DONT_INSERT);
14163
14164 gdb::observers::breakpoint_modified.notify (loc->owner);
14165 }
14166
14167 /* Enable or disable a range of breakpoint locations. BP_NUM is the
14168 number of the breakpoint, and BP_LOC_RANGE specifies the
14169 (inclusive) range of location numbers of that breakpoint to
14170 enable/disable. ENABLE specifies whether to enable or disable the
14171 location. */
14172
14173 static void
14174 enable_disable_breakpoint_location_range (int bp_num,
14175 std::pair<int, int> &bp_loc_range,
14176 bool enable)
14177 {
14178 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14179 enable_disable_bp_num_loc (bp_num, i, enable);
14180 }
14181
14182 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14183 If from_tty is nonzero, it prints a message to that effect,
14184 which ends with a period (no newline). */
14185
14186 void
14187 disable_breakpoint (struct breakpoint *bpt)
14188 {
14189 /* Never disable a watchpoint scope breakpoint; we want to
14190 hit them when we leave scope so we can delete both the
14191 watchpoint and its scope breakpoint at that time. */
14192 if (bpt->type == bp_watchpoint_scope)
14193 return;
14194
14195 bpt->enable_state = bp_disabled;
14196
14197 /* Mark breakpoint locations modified. */
14198 mark_breakpoint_modified (bpt);
14199
14200 if (target_supports_enable_disable_tracepoint ()
14201 && current_trace_status ()->running && is_tracepoint (bpt))
14202 {
14203 struct bp_location *location;
14204
14205 for (location = bpt->loc; location; location = location->next)
14206 target_disable_tracepoint (location);
14207 }
14208
14209 update_global_location_list (UGLL_DONT_INSERT);
14210
14211 gdb::observers::breakpoint_modified.notify (bpt);
14212 }
14213
14214 /* Enable or disable the breakpoint(s) or breakpoint location(s)
14215 specified in ARGS. ARGS may be in any of the formats handled by
14216 extract_bp_number_and_location. ENABLE specifies whether to enable
14217 or disable the breakpoints/locations. */
14218
14219 static void
14220 enable_disable_command (const char *args, int from_tty, bool enable)
14221 {
14222 if (args == 0)
14223 {
14224 struct breakpoint *bpt;
14225
14226 ALL_BREAKPOINTS (bpt)
14227 if (user_breakpoint_p (bpt))
14228 {
14229 if (enable)
14230 enable_breakpoint (bpt);
14231 else
14232 disable_breakpoint (bpt);
14233 }
14234 }
14235 else
14236 {
14237 std::string num = extract_arg (&args);
14238
14239 while (!num.empty ())
14240 {
14241 std::pair<int, int> bp_num_range, bp_loc_range;
14242
14243 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14244
14245 if (bp_loc_range.first == bp_loc_range.second
14246 && bp_loc_range.first == 0)
14247 {
14248 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14249 map_breakpoint_number_range (bp_num_range,
14250 enable
14251 ? enable_breakpoint
14252 : disable_breakpoint);
14253 }
14254 else
14255 {
14256 /* Handle breakpoint ids with formats 'x.y' or
14257 'x.y-z'. */
14258 enable_disable_breakpoint_location_range
14259 (bp_num_range.first, bp_loc_range, enable);
14260 }
14261 num = extract_arg (&args);
14262 }
14263 }
14264 }
14265
14266 /* The disable command disables the specified breakpoints/locations
14267 (or all defined breakpoints) so they're no longer effective in
14268 stopping the inferior. ARGS may be in any of the forms defined in
14269 extract_bp_number_and_location. */
14270
14271 static void
14272 disable_command (const char *args, int from_tty)
14273 {
14274 enable_disable_command (args, from_tty, false);
14275 }
14276
14277 static void
14278 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14279 int count)
14280 {
14281 int target_resources_ok;
14282
14283 if (bpt->type == bp_hardware_breakpoint)
14284 {
14285 int i;
14286 i = hw_breakpoint_used_count ();
14287 target_resources_ok =
14288 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14289 i + 1, 0);
14290 if (target_resources_ok == 0)
14291 error (_("No hardware breakpoint support in the target."));
14292 else if (target_resources_ok < 0)
14293 error (_("Hardware breakpoints used exceeds limit."));
14294 }
14295
14296 if (is_watchpoint (bpt))
14297 {
14298 /* Initialize it just to avoid a GCC false warning. */
14299 enum enable_state orig_enable_state = bp_disabled;
14300
14301 try
14302 {
14303 struct watchpoint *w = (struct watchpoint *) bpt;
14304
14305 orig_enable_state = bpt->enable_state;
14306 bpt->enable_state = bp_enabled;
14307 update_watchpoint (w, 1 /* reparse */);
14308 }
14309 catch (const gdb_exception &e)
14310 {
14311 bpt->enable_state = orig_enable_state;
14312 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14313 bpt->number);
14314 return;
14315 }
14316 }
14317
14318 bpt->enable_state = bp_enabled;
14319
14320 /* Mark breakpoint locations modified. */
14321 mark_breakpoint_modified (bpt);
14322
14323 if (target_supports_enable_disable_tracepoint ()
14324 && current_trace_status ()->running && is_tracepoint (bpt))
14325 {
14326 struct bp_location *location;
14327
14328 for (location = bpt->loc; location; location = location->next)
14329 target_enable_tracepoint (location);
14330 }
14331
14332 bpt->disposition = disposition;
14333 bpt->enable_count = count;
14334 update_global_location_list (UGLL_MAY_INSERT);
14335
14336 gdb::observers::breakpoint_modified.notify (bpt);
14337 }
14338
14339
14340 void
14341 enable_breakpoint (struct breakpoint *bpt)
14342 {
14343 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14344 }
14345
14346 /* The enable command enables the specified breakpoints/locations (or
14347 all defined breakpoints) so they once again become (or continue to
14348 be) effective in stopping the inferior. ARGS may be in any of the
14349 forms defined in extract_bp_number_and_location. */
14350
14351 static void
14352 enable_command (const char *args, int from_tty)
14353 {
14354 enable_disable_command (args, from_tty, true);
14355 }
14356
14357 static void
14358 enable_once_command (const char *args, int from_tty)
14359 {
14360 map_breakpoint_numbers
14361 (args, [&] (breakpoint *b)
14362 {
14363 iterate_over_related_breakpoints
14364 (b, [&] (breakpoint *bpt)
14365 {
14366 enable_breakpoint_disp (bpt, disp_disable, 1);
14367 });
14368 });
14369 }
14370
14371 static void
14372 enable_count_command (const char *args, int from_tty)
14373 {
14374 int count;
14375
14376 if (args == NULL)
14377 error_no_arg (_("hit count"));
14378
14379 count = get_number (&args);
14380
14381 map_breakpoint_numbers
14382 (args, [&] (breakpoint *b)
14383 {
14384 iterate_over_related_breakpoints
14385 (b, [&] (breakpoint *bpt)
14386 {
14387 enable_breakpoint_disp (bpt, disp_disable, count);
14388 });
14389 });
14390 }
14391
14392 static void
14393 enable_delete_command (const char *args, int from_tty)
14394 {
14395 map_breakpoint_numbers
14396 (args, [&] (breakpoint *b)
14397 {
14398 iterate_over_related_breakpoints
14399 (b, [&] (breakpoint *bpt)
14400 {
14401 enable_breakpoint_disp (bpt, disp_del, 1);
14402 });
14403 });
14404 }
14405 \f
14406 /* Invalidate last known value of any hardware watchpoint if
14407 the memory which that value represents has been written to by
14408 GDB itself. */
14409
14410 static void
14411 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14412 CORE_ADDR addr, ssize_t len,
14413 const bfd_byte *data)
14414 {
14415 struct breakpoint *bp;
14416
14417 ALL_BREAKPOINTS (bp)
14418 if (bp->enable_state == bp_enabled
14419 && bp->type == bp_hardware_watchpoint)
14420 {
14421 struct watchpoint *wp = (struct watchpoint *) bp;
14422
14423 if (wp->val_valid && wp->val != nullptr)
14424 {
14425 struct bp_location *loc;
14426
14427 for (loc = bp->loc; loc != NULL; loc = loc->next)
14428 if (loc->loc_type == bp_loc_hardware_watchpoint
14429 && loc->address + loc->length > addr
14430 && addr + len > loc->address)
14431 {
14432 wp->val = NULL;
14433 wp->val_valid = false;
14434 }
14435 }
14436 }
14437 }
14438
14439 /* Create and insert a breakpoint for software single step. */
14440
14441 void
14442 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14443 const address_space *aspace,
14444 CORE_ADDR next_pc)
14445 {
14446 struct thread_info *tp = inferior_thread ();
14447 struct symtab_and_line sal;
14448 CORE_ADDR pc = next_pc;
14449
14450 if (tp->control.single_step_breakpoints == NULL)
14451 {
14452 tp->control.single_step_breakpoints
14453 = new_single_step_breakpoint (tp->global_num, gdbarch);
14454 }
14455
14456 sal = find_pc_line (pc, 0);
14457 sal.pc = pc;
14458 sal.section = find_pc_overlay (pc);
14459 sal.explicit_pc = 1;
14460 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14461
14462 update_global_location_list (UGLL_INSERT);
14463 }
14464
14465 /* Insert single step breakpoints according to the current state. */
14466
14467 int
14468 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14469 {
14470 struct regcache *regcache = get_current_regcache ();
14471 std::vector<CORE_ADDR> next_pcs;
14472
14473 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14474
14475 if (!next_pcs.empty ())
14476 {
14477 struct frame_info *frame = get_current_frame ();
14478 const address_space *aspace = get_frame_address_space (frame);
14479
14480 for (CORE_ADDR pc : next_pcs)
14481 insert_single_step_breakpoint (gdbarch, aspace, pc);
14482
14483 return 1;
14484 }
14485 else
14486 return 0;
14487 }
14488
14489 /* See breakpoint.h. */
14490
14491 int
14492 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14493 const address_space *aspace,
14494 CORE_ADDR pc)
14495 {
14496 struct bp_location *loc;
14497
14498 for (loc = bp->loc; loc != NULL; loc = loc->next)
14499 if (loc->inserted
14500 && breakpoint_location_address_match (loc, aspace, pc))
14501 return 1;
14502
14503 return 0;
14504 }
14505
14506 /* Check whether a software single-step breakpoint is inserted at
14507 PC. */
14508
14509 int
14510 single_step_breakpoint_inserted_here_p (const address_space *aspace,
14511 CORE_ADDR pc)
14512 {
14513 struct breakpoint *bpt;
14514
14515 ALL_BREAKPOINTS (bpt)
14516 {
14517 if (bpt->type == bp_single_step
14518 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14519 return 1;
14520 }
14521 return 0;
14522 }
14523
14524 /* Tracepoint-specific operations. */
14525
14526 /* Set tracepoint count to NUM. */
14527 static void
14528 set_tracepoint_count (int num)
14529 {
14530 tracepoint_count = num;
14531 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14532 }
14533
14534 static void
14535 trace_command (const char *arg, int from_tty)
14536 {
14537 event_location_up location = string_to_event_location (&arg,
14538 current_language);
14539 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
14540 (location.get (), true /* is_tracepoint */);
14541
14542 create_breakpoint (get_current_arch (),
14543 location.get (),
14544 NULL, 0, arg, 1 /* parse arg */,
14545 0 /* tempflag */,
14546 bp_tracepoint /* type_wanted */,
14547 0 /* Ignore count */,
14548 pending_break_support,
14549 ops,
14550 from_tty,
14551 1 /* enabled */,
14552 0 /* internal */, 0);
14553 }
14554
14555 static void
14556 ftrace_command (const char *arg, int from_tty)
14557 {
14558 event_location_up location = string_to_event_location (&arg,
14559 current_language);
14560 create_breakpoint (get_current_arch (),
14561 location.get (),
14562 NULL, 0, arg, 1 /* parse arg */,
14563 0 /* tempflag */,
14564 bp_fast_tracepoint /* type_wanted */,
14565 0 /* Ignore count */,
14566 pending_break_support,
14567 &tracepoint_breakpoint_ops,
14568 from_tty,
14569 1 /* enabled */,
14570 0 /* internal */, 0);
14571 }
14572
14573 /* strace command implementation. Creates a static tracepoint. */
14574
14575 static void
14576 strace_command (const char *arg, int from_tty)
14577 {
14578 struct breakpoint_ops *ops;
14579 event_location_up location;
14580
14581 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14582 or with a normal static tracepoint. */
14583 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14584 {
14585 ops = &strace_marker_breakpoint_ops;
14586 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
14587 }
14588 else
14589 {
14590 ops = &tracepoint_breakpoint_ops;
14591 location = string_to_event_location (&arg, current_language);
14592 }
14593
14594 create_breakpoint (get_current_arch (),
14595 location.get (),
14596 NULL, 0, arg, 1 /* parse arg */,
14597 0 /* tempflag */,
14598 bp_static_tracepoint /* type_wanted */,
14599 0 /* Ignore count */,
14600 pending_break_support,
14601 ops,
14602 from_tty,
14603 1 /* enabled */,
14604 0 /* internal */, 0);
14605 }
14606
14607 /* Set up a fake reader function that gets command lines from a linked
14608 list that was acquired during tracepoint uploading. */
14609
14610 static struct uploaded_tp *this_utp;
14611 static int next_cmd;
14612
14613 static char *
14614 read_uploaded_action (void)
14615 {
14616 char *rslt = nullptr;
14617
14618 if (next_cmd < this_utp->cmd_strings.size ())
14619 {
14620 rslt = this_utp->cmd_strings[next_cmd].get ();
14621 next_cmd++;
14622 }
14623
14624 return rslt;
14625 }
14626
14627 /* Given information about a tracepoint as recorded on a target (which
14628 can be either a live system or a trace file), attempt to create an
14629 equivalent GDB tracepoint. This is not a reliable process, since
14630 the target does not necessarily have all the information used when
14631 the tracepoint was originally defined. */
14632
14633 struct tracepoint *
14634 create_tracepoint_from_upload (struct uploaded_tp *utp)
14635 {
14636 const char *addr_str;
14637 char small_buf[100];
14638 struct tracepoint *tp;
14639
14640 if (utp->at_string)
14641 addr_str = utp->at_string.get ();
14642 else
14643 {
14644 /* In the absence of a source location, fall back to raw
14645 address. Since there is no way to confirm that the address
14646 means the same thing as when the trace was started, warn the
14647 user. */
14648 warning (_("Uploaded tracepoint %d has no "
14649 "source location, using raw address"),
14650 utp->number);
14651 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14652 addr_str = small_buf;
14653 }
14654
14655 /* There's not much we can do with a sequence of bytecodes. */
14656 if (utp->cond && !utp->cond_string)
14657 warning (_("Uploaded tracepoint %d condition "
14658 "has no source form, ignoring it"),
14659 utp->number);
14660
14661 event_location_up location = string_to_event_location (&addr_str,
14662 current_language);
14663 if (!create_breakpoint (get_current_arch (),
14664 location.get (),
14665 utp->cond_string.get (), -1, addr_str,
14666 0 /* parse cond/thread */,
14667 0 /* tempflag */,
14668 utp->type /* type_wanted */,
14669 0 /* Ignore count */,
14670 pending_break_support,
14671 &tracepoint_breakpoint_ops,
14672 0 /* from_tty */,
14673 utp->enabled /* enabled */,
14674 0 /* internal */,
14675 CREATE_BREAKPOINT_FLAGS_INSERTED))
14676 return NULL;
14677
14678 /* Get the tracepoint we just created. */
14679 tp = get_tracepoint (tracepoint_count);
14680 gdb_assert (tp != NULL);
14681
14682 if (utp->pass > 0)
14683 {
14684 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14685 tp->number);
14686
14687 trace_pass_command (small_buf, 0);
14688 }
14689
14690 /* If we have uploaded versions of the original commands, set up a
14691 special-purpose "reader" function and call the usual command line
14692 reader, then pass the result to the breakpoint command-setting
14693 function. */
14694 if (!utp->cmd_strings.empty ())
14695 {
14696 counted_command_line cmd_list;
14697
14698 this_utp = utp;
14699 next_cmd = 0;
14700
14701 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
14702
14703 breakpoint_set_commands (tp, std::move (cmd_list));
14704 }
14705 else if (!utp->actions.empty ()
14706 || !utp->step_actions.empty ())
14707 warning (_("Uploaded tracepoint %d actions "
14708 "have no source form, ignoring them"),
14709 utp->number);
14710
14711 /* Copy any status information that might be available. */
14712 tp->hit_count = utp->hit_count;
14713 tp->traceframe_usage = utp->traceframe_usage;
14714
14715 return tp;
14716 }
14717
14718 /* Print information on tracepoint number TPNUM_EXP, or all if
14719 omitted. */
14720
14721 static void
14722 info_tracepoints_command (const char *args, int from_tty)
14723 {
14724 struct ui_out *uiout = current_uiout;
14725 int num_printed;
14726
14727 num_printed = breakpoint_1 (args, false, is_tracepoint);
14728
14729 if (num_printed == 0)
14730 {
14731 if (args == NULL || *args == '\0')
14732 uiout->message ("No tracepoints.\n");
14733 else
14734 uiout->message ("No tracepoint matching '%s'.\n", args);
14735 }
14736
14737 default_collect_info ();
14738 }
14739
14740 /* The 'enable trace' command enables tracepoints.
14741 Not supported by all targets. */
14742 static void
14743 enable_trace_command (const char *args, int from_tty)
14744 {
14745 enable_command (args, from_tty);
14746 }
14747
14748 /* The 'disable trace' command disables tracepoints.
14749 Not supported by all targets. */
14750 static void
14751 disable_trace_command (const char *args, int from_tty)
14752 {
14753 disable_command (args, from_tty);
14754 }
14755
14756 /* Remove a tracepoint (or all if no argument). */
14757 static void
14758 delete_trace_command (const char *arg, int from_tty)
14759 {
14760 struct breakpoint *b, *b_tmp;
14761
14762 dont_repeat ();
14763
14764 if (arg == 0)
14765 {
14766 int breaks_to_delete = 0;
14767
14768 /* Delete all breakpoints if no argument.
14769 Do not delete internal or call-dummy breakpoints, these
14770 have to be deleted with an explicit breakpoint number
14771 argument. */
14772 ALL_TRACEPOINTS (b)
14773 if (is_tracepoint (b) && user_breakpoint_p (b))
14774 {
14775 breaks_to_delete = 1;
14776 break;
14777 }
14778
14779 /* Ask user only if there are some breakpoints to delete. */
14780 if (!from_tty
14781 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14782 {
14783 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14784 if (is_tracepoint (b) && user_breakpoint_p (b))
14785 delete_breakpoint (b);
14786 }
14787 }
14788 else
14789 map_breakpoint_numbers
14790 (arg, [&] (breakpoint *br)
14791 {
14792 iterate_over_related_breakpoints (br, delete_breakpoint);
14793 });
14794 }
14795
14796 /* Helper function for trace_pass_command. */
14797
14798 static void
14799 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14800 {
14801 tp->pass_count = count;
14802 gdb::observers::breakpoint_modified.notify (tp);
14803 if (from_tty)
14804 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
14805 tp->number, count);
14806 }
14807
14808 /* Set passcount for tracepoint.
14809
14810 First command argument is passcount, second is tracepoint number.
14811 If tracepoint number omitted, apply to most recently defined.
14812 Also accepts special argument "all". */
14813
14814 static void
14815 trace_pass_command (const char *args, int from_tty)
14816 {
14817 struct tracepoint *t1;
14818 ULONGEST count;
14819
14820 if (args == 0 || *args == 0)
14821 error (_("passcount command requires an "
14822 "argument (count + optional TP num)"));
14823
14824 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
14825
14826 args = skip_spaces (args);
14827 if (*args && strncasecmp (args, "all", 3) == 0)
14828 {
14829 struct breakpoint *b;
14830
14831 args += 3; /* Skip special argument "all". */
14832 if (*args)
14833 error (_("Junk at end of arguments."));
14834
14835 ALL_TRACEPOINTS (b)
14836 {
14837 t1 = (struct tracepoint *) b;
14838 trace_pass_set_count (t1, count, from_tty);
14839 }
14840 }
14841 else if (*args == '\0')
14842 {
14843 t1 = get_tracepoint_by_number (&args, NULL);
14844 if (t1)
14845 trace_pass_set_count (t1, count, from_tty);
14846 }
14847 else
14848 {
14849 number_or_range_parser parser (args);
14850 while (!parser.finished ())
14851 {
14852 t1 = get_tracepoint_by_number (&args, &parser);
14853 if (t1)
14854 trace_pass_set_count (t1, count, from_tty);
14855 }
14856 }
14857 }
14858
14859 struct tracepoint *
14860 get_tracepoint (int num)
14861 {
14862 struct breakpoint *t;
14863
14864 ALL_TRACEPOINTS (t)
14865 if (t->number == num)
14866 return (struct tracepoint *) t;
14867
14868 return NULL;
14869 }
14870
14871 /* Find the tracepoint with the given target-side number (which may be
14872 different from the tracepoint number after disconnecting and
14873 reconnecting). */
14874
14875 struct tracepoint *
14876 get_tracepoint_by_number_on_target (int num)
14877 {
14878 struct breakpoint *b;
14879
14880 ALL_TRACEPOINTS (b)
14881 {
14882 struct tracepoint *t = (struct tracepoint *) b;
14883
14884 if (t->number_on_target == num)
14885 return t;
14886 }
14887
14888 return NULL;
14889 }
14890
14891 /* Utility: parse a tracepoint number and look it up in the list.
14892 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
14893 If the argument is missing, the most recent tracepoint
14894 (tracepoint_count) is returned. */
14895
14896 struct tracepoint *
14897 get_tracepoint_by_number (const char **arg,
14898 number_or_range_parser *parser)
14899 {
14900 struct breakpoint *t;
14901 int tpnum;
14902 const char *instring = arg == NULL ? NULL : *arg;
14903
14904 if (parser != NULL)
14905 {
14906 gdb_assert (!parser->finished ());
14907 tpnum = parser->get_number ();
14908 }
14909 else if (arg == NULL || *arg == NULL || ! **arg)
14910 tpnum = tracepoint_count;
14911 else
14912 tpnum = get_number (arg);
14913
14914 if (tpnum <= 0)
14915 {
14916 if (instring && *instring)
14917 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
14918 instring);
14919 else
14920 printf_filtered (_("No previous tracepoint\n"));
14921 return NULL;
14922 }
14923
14924 ALL_TRACEPOINTS (t)
14925 if (t->number == tpnum)
14926 {
14927 return (struct tracepoint *) t;
14928 }
14929
14930 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
14931 return NULL;
14932 }
14933
14934 void
14935 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
14936 {
14937 if (b->thread != -1)
14938 fprintf_unfiltered (fp, " thread %d", b->thread);
14939
14940 if (b->task != 0)
14941 fprintf_unfiltered (fp, " task %d", b->task);
14942
14943 fprintf_unfiltered (fp, "\n");
14944 }
14945
14946 /* Save information on user settable breakpoints (watchpoints, etc) to
14947 a new script file named FILENAME. If FILTER is non-NULL, call it
14948 on each breakpoint and only include the ones for which it returns
14949 true. */
14950
14951 static void
14952 save_breakpoints (const char *filename, int from_tty,
14953 bool (*filter) (const struct breakpoint *))
14954 {
14955 struct breakpoint *tp;
14956 int any = 0;
14957 int extra_trace_bits = 0;
14958
14959 if (filename == 0 || *filename == 0)
14960 error (_("Argument required (file name in which to save)"));
14961
14962 /* See if we have anything to save. */
14963 ALL_BREAKPOINTS (tp)
14964 {
14965 /* Skip internal and momentary breakpoints. */
14966 if (!user_breakpoint_p (tp))
14967 continue;
14968
14969 /* If we have a filter, only save the breakpoints it accepts. */
14970 if (filter && !filter (tp))
14971 continue;
14972
14973 any = 1;
14974
14975 if (is_tracepoint (tp))
14976 {
14977 extra_trace_bits = 1;
14978
14979 /* We can stop searching. */
14980 break;
14981 }
14982 }
14983
14984 if (!any)
14985 {
14986 warning (_("Nothing to save."));
14987 return;
14988 }
14989
14990 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
14991
14992 stdio_file fp;
14993
14994 if (!fp.open (expanded_filename.get (), "w"))
14995 error (_("Unable to open file '%s' for saving (%s)"),
14996 expanded_filename.get (), safe_strerror (errno));
14997
14998 if (extra_trace_bits)
14999 save_trace_state_variables (&fp);
15000
15001 ALL_BREAKPOINTS (tp)
15002 {
15003 /* Skip internal and momentary breakpoints. */
15004 if (!user_breakpoint_p (tp))
15005 continue;
15006
15007 /* If we have a filter, only save the breakpoints it accepts. */
15008 if (filter && !filter (tp))
15009 continue;
15010
15011 tp->ops->print_recreate (tp, &fp);
15012
15013 /* Note, we can't rely on tp->number for anything, as we can't
15014 assume the recreated breakpoint numbers will match. Use $bpnum
15015 instead. */
15016
15017 if (tp->cond_string)
15018 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15019
15020 if (tp->ignore_count)
15021 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15022
15023 if (tp->type != bp_dprintf && tp->commands)
15024 {
15025 fp.puts (" commands\n");
15026
15027 current_uiout->redirect (&fp);
15028 try
15029 {
15030 print_command_lines (current_uiout, tp->commands.get (), 2);
15031 }
15032 catch (const gdb_exception &ex)
15033 {
15034 current_uiout->redirect (NULL);
15035 throw;
15036 }
15037
15038 current_uiout->redirect (NULL);
15039 fp.puts (" end\n");
15040 }
15041
15042 if (tp->enable_state == bp_disabled)
15043 fp.puts ("disable $bpnum\n");
15044
15045 /* If this is a multi-location breakpoint, check if the locations
15046 should be individually disabled. Watchpoint locations are
15047 special, and not user visible. */
15048 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15049 {
15050 struct bp_location *loc;
15051 int n = 1;
15052
15053 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15054 if (!loc->enabled)
15055 fp.printf ("disable $bpnum.%d\n", n);
15056 }
15057 }
15058
15059 if (extra_trace_bits && *default_collect)
15060 fp.printf ("set default-collect %s\n", default_collect);
15061
15062 if (from_tty)
15063 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
15064 }
15065
15066 /* The `save breakpoints' command. */
15067
15068 static void
15069 save_breakpoints_command (const char *args, int from_tty)
15070 {
15071 save_breakpoints (args, from_tty, NULL);
15072 }
15073
15074 /* The `save tracepoints' command. */
15075
15076 static void
15077 save_tracepoints_command (const char *args, int from_tty)
15078 {
15079 save_breakpoints (args, from_tty, is_tracepoint);
15080 }
15081
15082 /* Create a vector of all tracepoints. */
15083
15084 std::vector<breakpoint *>
15085 all_tracepoints (void)
15086 {
15087 std::vector<breakpoint *> tp_vec;
15088 struct breakpoint *tp;
15089
15090 ALL_TRACEPOINTS (tp)
15091 {
15092 tp_vec.push_back (tp);
15093 }
15094
15095 return tp_vec;
15096 }
15097
15098 \f
15099 /* This help string is used to consolidate all the help string for specifying
15100 locations used by several commands. */
15101
15102 #define LOCATION_HELP_STRING \
15103 "Linespecs are colon-separated lists of location parameters, such as\n\
15104 source filename, function name, label name, and line number.\n\
15105 Example: To specify the start of a label named \"the_top\" in the\n\
15106 function \"fact\" in the file \"factorial.c\", use\n\
15107 \"factorial.c:fact:the_top\".\n\
15108 \n\
15109 Address locations begin with \"*\" and specify an exact address in the\n\
15110 program. Example: To specify the fourth byte past the start function\n\
15111 \"main\", use \"*main + 4\".\n\
15112 \n\
15113 Explicit locations are similar to linespecs but use an option/argument\n\
15114 syntax to specify location parameters.\n\
15115 Example: To specify the start of the label named \"the_top\" in the\n\
15116 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15117 -function fact -label the_top\".\n\
15118 \n\
15119 By default, a specified function is matched against the program's\n\
15120 functions in all scopes. For C++, this means in all namespaces and\n\
15121 classes. For Ada, this means in all packages. E.g., in C++,\n\
15122 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15123 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
15124 specified name as a complete fully-qualified name instead."
15125
15126 /* This help string is used for the break, hbreak, tbreak and thbreak
15127 commands. It is defined as a macro to prevent duplication.
15128 COMMAND should be a string constant containing the name of the
15129 command. */
15130
15131 #define BREAK_ARGS_HELP(command) \
15132 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15133 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15134 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15135 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15136 `-probe-dtrace' (for a DTrace probe).\n\
15137 LOCATION may be a linespec, address, or explicit location as described\n\
15138 below.\n\
15139 \n\
15140 With no LOCATION, uses current execution address of the selected\n\
15141 stack frame. This is useful for breaking on return to a stack frame.\n\
15142 \n\
15143 THREADNUM is the number from \"info threads\".\n\
15144 CONDITION is a boolean expression.\n\
15145 \n" LOCATION_HELP_STRING "\n\n\
15146 Multiple breakpoints at one place are permitted, and useful if their\n\
15147 conditions are different.\n\
15148 \n\
15149 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15150
15151 /* List of subcommands for "catch". */
15152 static struct cmd_list_element *catch_cmdlist;
15153
15154 /* List of subcommands for "tcatch". */
15155 static struct cmd_list_element *tcatch_cmdlist;
15156
15157 void
15158 add_catch_command (const char *name, const char *docstring,
15159 cmd_const_sfunc_ftype *sfunc,
15160 completer_ftype *completer,
15161 void *user_data_catch,
15162 void *user_data_tcatch)
15163 {
15164 struct cmd_list_element *command;
15165
15166 command = add_cmd (name, class_breakpoint, docstring,
15167 &catch_cmdlist);
15168 set_cmd_sfunc (command, sfunc);
15169 set_cmd_context (command, user_data_catch);
15170 set_cmd_completer (command, completer);
15171
15172 command = add_cmd (name, class_breakpoint, docstring,
15173 &tcatch_cmdlist);
15174 set_cmd_sfunc (command, sfunc);
15175 set_cmd_context (command, user_data_tcatch);
15176 set_cmd_completer (command, completer);
15177 }
15178
15179 struct breakpoint *
15180 iterate_over_breakpoints (gdb::function_view<bool (breakpoint *)> callback)
15181 {
15182 struct breakpoint *b, *b_tmp;
15183
15184 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15185 {
15186 if (callback (b))
15187 return b;
15188 }
15189
15190 return NULL;
15191 }
15192
15193 /* Zero if any of the breakpoint's locations could be a location where
15194 functions have been inlined, nonzero otherwise. */
15195
15196 static int
15197 is_non_inline_function (struct breakpoint *b)
15198 {
15199 /* The shared library event breakpoint is set on the address of a
15200 non-inline function. */
15201 if (b->type == bp_shlib_event)
15202 return 1;
15203
15204 return 0;
15205 }
15206
15207 /* Nonzero if the specified PC cannot be a location where functions
15208 have been inlined. */
15209
15210 int
15211 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
15212 const struct target_waitstatus *ws)
15213 {
15214 struct breakpoint *b;
15215 struct bp_location *bl;
15216
15217 ALL_BREAKPOINTS (b)
15218 {
15219 if (!is_non_inline_function (b))
15220 continue;
15221
15222 for (bl = b->loc; bl != NULL; bl = bl->next)
15223 {
15224 if (!bl->shlib_disabled
15225 && bpstat_check_location (bl, aspace, pc, ws))
15226 return 1;
15227 }
15228 }
15229
15230 return 0;
15231 }
15232
15233 /* Remove any references to OBJFILE which is going to be freed. */
15234
15235 void
15236 breakpoint_free_objfile (struct objfile *objfile)
15237 {
15238 struct bp_location **locp, *loc;
15239
15240 ALL_BP_LOCATIONS (loc, locp)
15241 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15242 loc->symtab = NULL;
15243 }
15244
15245 void
15246 initialize_breakpoint_ops (void)
15247 {
15248 static int initialized = 0;
15249
15250 struct breakpoint_ops *ops;
15251
15252 if (initialized)
15253 return;
15254 initialized = 1;
15255
15256 /* The breakpoint_ops structure to be inherit by all kinds of
15257 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15258 internal and momentary breakpoints, etc.). */
15259 ops = &bkpt_base_breakpoint_ops;
15260 *ops = base_breakpoint_ops;
15261 ops->re_set = bkpt_re_set;
15262 ops->insert_location = bkpt_insert_location;
15263 ops->remove_location = bkpt_remove_location;
15264 ops->breakpoint_hit = bkpt_breakpoint_hit;
15265 ops->create_sals_from_location = bkpt_create_sals_from_location;
15266 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15267 ops->decode_location = bkpt_decode_location;
15268
15269 /* The breakpoint_ops structure to be used in regular breakpoints. */
15270 ops = &bkpt_breakpoint_ops;
15271 *ops = bkpt_base_breakpoint_ops;
15272 ops->re_set = bkpt_re_set;
15273 ops->resources_needed = bkpt_resources_needed;
15274 ops->print_it = bkpt_print_it;
15275 ops->print_mention = bkpt_print_mention;
15276 ops->print_recreate = bkpt_print_recreate;
15277
15278 /* Ranged breakpoints. */
15279 ops = &ranged_breakpoint_ops;
15280 *ops = bkpt_breakpoint_ops;
15281 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15282 ops->resources_needed = resources_needed_ranged_breakpoint;
15283 ops->print_it = print_it_ranged_breakpoint;
15284 ops->print_one = print_one_ranged_breakpoint;
15285 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15286 ops->print_mention = print_mention_ranged_breakpoint;
15287 ops->print_recreate = print_recreate_ranged_breakpoint;
15288
15289 /* Internal breakpoints. */
15290 ops = &internal_breakpoint_ops;
15291 *ops = bkpt_base_breakpoint_ops;
15292 ops->re_set = internal_bkpt_re_set;
15293 ops->check_status = internal_bkpt_check_status;
15294 ops->print_it = internal_bkpt_print_it;
15295 ops->print_mention = internal_bkpt_print_mention;
15296
15297 /* Momentary breakpoints. */
15298 ops = &momentary_breakpoint_ops;
15299 *ops = bkpt_base_breakpoint_ops;
15300 ops->re_set = momentary_bkpt_re_set;
15301 ops->check_status = momentary_bkpt_check_status;
15302 ops->print_it = momentary_bkpt_print_it;
15303 ops->print_mention = momentary_bkpt_print_mention;
15304
15305 /* Probe breakpoints. */
15306 ops = &bkpt_probe_breakpoint_ops;
15307 *ops = bkpt_breakpoint_ops;
15308 ops->insert_location = bkpt_probe_insert_location;
15309 ops->remove_location = bkpt_probe_remove_location;
15310 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15311 ops->decode_location = bkpt_probe_decode_location;
15312
15313 /* Watchpoints. */
15314 ops = &watchpoint_breakpoint_ops;
15315 *ops = base_breakpoint_ops;
15316 ops->re_set = re_set_watchpoint;
15317 ops->insert_location = insert_watchpoint;
15318 ops->remove_location = remove_watchpoint;
15319 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15320 ops->check_status = check_status_watchpoint;
15321 ops->resources_needed = resources_needed_watchpoint;
15322 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15323 ops->print_it = print_it_watchpoint;
15324 ops->print_mention = print_mention_watchpoint;
15325 ops->print_recreate = print_recreate_watchpoint;
15326 ops->explains_signal = explains_signal_watchpoint;
15327
15328 /* Masked watchpoints. */
15329 ops = &masked_watchpoint_breakpoint_ops;
15330 *ops = watchpoint_breakpoint_ops;
15331 ops->insert_location = insert_masked_watchpoint;
15332 ops->remove_location = remove_masked_watchpoint;
15333 ops->resources_needed = resources_needed_masked_watchpoint;
15334 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15335 ops->print_it = print_it_masked_watchpoint;
15336 ops->print_one_detail = print_one_detail_masked_watchpoint;
15337 ops->print_mention = print_mention_masked_watchpoint;
15338 ops->print_recreate = print_recreate_masked_watchpoint;
15339
15340 /* Tracepoints. */
15341 ops = &tracepoint_breakpoint_ops;
15342 *ops = base_breakpoint_ops;
15343 ops->re_set = tracepoint_re_set;
15344 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15345 ops->print_one_detail = tracepoint_print_one_detail;
15346 ops->print_mention = tracepoint_print_mention;
15347 ops->print_recreate = tracepoint_print_recreate;
15348 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15349 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15350 ops->decode_location = tracepoint_decode_location;
15351
15352 /* Probe tracepoints. */
15353 ops = &tracepoint_probe_breakpoint_ops;
15354 *ops = tracepoint_breakpoint_ops;
15355 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15356 ops->decode_location = tracepoint_probe_decode_location;
15357
15358 /* Static tracepoints with marker (`-m'). */
15359 ops = &strace_marker_breakpoint_ops;
15360 *ops = tracepoint_breakpoint_ops;
15361 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15362 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15363 ops->decode_location = strace_marker_decode_location;
15364
15365 /* Fork catchpoints. */
15366 ops = &catch_fork_breakpoint_ops;
15367 *ops = base_breakpoint_ops;
15368 ops->insert_location = insert_catch_fork;
15369 ops->remove_location = remove_catch_fork;
15370 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15371 ops->print_it = print_it_catch_fork;
15372 ops->print_one = print_one_catch_fork;
15373 ops->print_mention = print_mention_catch_fork;
15374 ops->print_recreate = print_recreate_catch_fork;
15375
15376 /* Vfork catchpoints. */
15377 ops = &catch_vfork_breakpoint_ops;
15378 *ops = base_breakpoint_ops;
15379 ops->insert_location = insert_catch_vfork;
15380 ops->remove_location = remove_catch_vfork;
15381 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15382 ops->print_it = print_it_catch_vfork;
15383 ops->print_one = print_one_catch_vfork;
15384 ops->print_mention = print_mention_catch_vfork;
15385 ops->print_recreate = print_recreate_catch_vfork;
15386
15387 /* Exec catchpoints. */
15388 ops = &catch_exec_breakpoint_ops;
15389 *ops = base_breakpoint_ops;
15390 ops->insert_location = insert_catch_exec;
15391 ops->remove_location = remove_catch_exec;
15392 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15393 ops->print_it = print_it_catch_exec;
15394 ops->print_one = print_one_catch_exec;
15395 ops->print_mention = print_mention_catch_exec;
15396 ops->print_recreate = print_recreate_catch_exec;
15397
15398 /* Solib-related catchpoints. */
15399 ops = &catch_solib_breakpoint_ops;
15400 *ops = base_breakpoint_ops;
15401 ops->insert_location = insert_catch_solib;
15402 ops->remove_location = remove_catch_solib;
15403 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15404 ops->check_status = check_status_catch_solib;
15405 ops->print_it = print_it_catch_solib;
15406 ops->print_one = print_one_catch_solib;
15407 ops->print_mention = print_mention_catch_solib;
15408 ops->print_recreate = print_recreate_catch_solib;
15409
15410 ops = &dprintf_breakpoint_ops;
15411 *ops = bkpt_base_breakpoint_ops;
15412 ops->re_set = dprintf_re_set;
15413 ops->resources_needed = bkpt_resources_needed;
15414 ops->print_it = bkpt_print_it;
15415 ops->print_mention = bkpt_print_mention;
15416 ops->print_recreate = dprintf_print_recreate;
15417 ops->after_condition_true = dprintf_after_condition_true;
15418 ops->breakpoint_hit = dprintf_breakpoint_hit;
15419 }
15420
15421 /* Chain containing all defined "enable breakpoint" subcommands. */
15422
15423 static struct cmd_list_element *enablebreaklist = NULL;
15424
15425 /* See breakpoint.h. */
15426
15427 cmd_list_element *commands_cmd_element = nullptr;
15428
15429 void _initialize_breakpoint ();
15430 void
15431 _initialize_breakpoint ()
15432 {
15433 struct cmd_list_element *c;
15434
15435 initialize_breakpoint_ops ();
15436
15437 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib);
15438 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile);
15439 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change);
15440
15441 breakpoint_chain = 0;
15442 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15443 before a breakpoint is set. */
15444 breakpoint_count = 0;
15445
15446 tracepoint_count = 0;
15447
15448 add_com ("ignore", class_breakpoint, ignore_command, _("\
15449 Set ignore-count of breakpoint number N to COUNT.\n\
15450 Usage is `ignore N COUNT'."));
15451
15452 commands_cmd_element = add_com ("commands", class_breakpoint,
15453 commands_command, _("\
15454 Set commands to be executed when the given breakpoints are hit.\n\
15455 Give a space-separated breakpoint list as argument after \"commands\".\n\
15456 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15457 (e.g. `5-7').\n\
15458 With no argument, the targeted breakpoint is the last one set.\n\
15459 The commands themselves follow starting on the next line.\n\
15460 Type a line containing \"end\" to indicate the end of them.\n\
15461 Give \"silent\" as the first line to make the breakpoint silent;\n\
15462 then no output is printed when it is hit, except what the commands print."));
15463
15464 c = add_com ("condition", class_breakpoint, condition_command, _("\
15465 Specify breakpoint number N to break only if COND is true.\n\
15466 Usage is `condition N COND', where N is an integer and COND is an\n\
15467 expression to be evaluated whenever breakpoint N is reached."));
15468 set_cmd_completer (c, condition_completer);
15469
15470 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15471 Set a temporary breakpoint.\n\
15472 Like \"break\" except the breakpoint is only temporary,\n\
15473 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15474 by using \"enable delete\" on the breakpoint number.\n\
15475 \n"
15476 BREAK_ARGS_HELP ("tbreak")));
15477 set_cmd_completer (c, location_completer);
15478
15479 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15480 Set a hardware assisted breakpoint.\n\
15481 Like \"break\" except the breakpoint requires hardware support,\n\
15482 some target hardware may not have this support.\n\
15483 \n"
15484 BREAK_ARGS_HELP ("hbreak")));
15485 set_cmd_completer (c, location_completer);
15486
15487 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15488 Set a temporary hardware assisted breakpoint.\n\
15489 Like \"hbreak\" except the breakpoint is only temporary,\n\
15490 so it will be deleted when hit.\n\
15491 \n"
15492 BREAK_ARGS_HELP ("thbreak")));
15493 set_cmd_completer (c, location_completer);
15494
15495 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15496 Enable all or some breakpoints.\n\
15497 Usage: enable [BREAKPOINTNUM]...\n\
15498 Give breakpoint numbers (separated by spaces) as arguments.\n\
15499 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15500 This is used to cancel the effect of the \"disable\" command.\n\
15501 With a subcommand you can enable temporarily."),
15502 &enablelist, "enable ", 1, &cmdlist);
15503
15504 add_com_alias ("en", "enable", class_breakpoint, 1);
15505
15506 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15507 Enable all or some breakpoints.\n\
15508 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
15509 Give breakpoint numbers (separated by spaces) as arguments.\n\
15510 This is used to cancel the effect of the \"disable\" command.\n\
15511 May be abbreviated to simply \"enable\"."),
15512 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
15513
15514 add_cmd ("once", no_class, enable_once_command, _("\
15515 Enable some breakpoints for one hit.\n\
15516 Usage: enable breakpoints once BREAKPOINTNUM...\n\
15517 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15518 &enablebreaklist);
15519
15520 add_cmd ("delete", no_class, enable_delete_command, _("\
15521 Enable some breakpoints and delete when hit.\n\
15522 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
15523 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15524 &enablebreaklist);
15525
15526 add_cmd ("count", no_class, enable_count_command, _("\
15527 Enable some breakpoints for COUNT hits.\n\
15528 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
15529 If a breakpoint is hit while enabled in this fashion,\n\
15530 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15531 &enablebreaklist);
15532
15533 add_cmd ("delete", no_class, enable_delete_command, _("\
15534 Enable some breakpoints and delete when hit.\n\
15535 Usage: enable delete BREAKPOINTNUM...\n\
15536 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15537 &enablelist);
15538
15539 add_cmd ("once", no_class, enable_once_command, _("\
15540 Enable some breakpoints for one hit.\n\
15541 Usage: enable once BREAKPOINTNUM...\n\
15542 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15543 &enablelist);
15544
15545 add_cmd ("count", no_class, enable_count_command, _("\
15546 Enable some breakpoints for COUNT hits.\n\
15547 Usage: enable count COUNT BREAKPOINTNUM...\n\
15548 If a breakpoint is hit while enabled in this fashion,\n\
15549 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15550 &enablelist);
15551
15552 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15553 Disable all or some breakpoints.\n\
15554 Usage: disable [BREAKPOINTNUM]...\n\
15555 Arguments are breakpoint numbers with spaces in between.\n\
15556 To disable all breakpoints, give no argument.\n\
15557 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15558 &disablelist, "disable ", 1, &cmdlist);
15559 add_com_alias ("dis", "disable", class_breakpoint, 1);
15560 add_com_alias ("disa", "disable", class_breakpoint, 1);
15561
15562 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
15563 Disable all or some breakpoints.\n\
15564 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
15565 Arguments are breakpoint numbers with spaces in between.\n\
15566 To disable all breakpoints, give no argument.\n\
15567 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15568 This command may be abbreviated \"disable\"."),
15569 &disablelist);
15570
15571 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15572 Delete all or some breakpoints.\n\
15573 Usage: delete [BREAKPOINTNUM]...\n\
15574 Arguments are breakpoint numbers with spaces in between.\n\
15575 To delete all breakpoints, give no argument.\n\
15576 \n\
15577 Also a prefix command for deletion of other GDB objects."),
15578 &deletelist, "delete ", 1, &cmdlist);
15579 add_com_alias ("d", "delete", class_breakpoint, 1);
15580 add_com_alias ("del", "delete", class_breakpoint, 1);
15581
15582 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
15583 Delete all or some breakpoints or auto-display expressions.\n\
15584 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
15585 Arguments are breakpoint numbers with spaces in between.\n\
15586 To delete all breakpoints, give no argument.\n\
15587 This command may be abbreviated \"delete\"."),
15588 &deletelist);
15589
15590 add_com ("clear", class_breakpoint, clear_command, _("\
15591 Clear breakpoint at specified location.\n\
15592 Argument may be a linespec, explicit, or address location as described below.\n\
15593 \n\
15594 With no argument, clears all breakpoints in the line that the selected frame\n\
15595 is executing in.\n"
15596 "\n" LOCATION_HELP_STRING "\n\n\
15597 See also the \"delete\" command which clears breakpoints by number."));
15598 add_com_alias ("cl", "clear", class_breakpoint, 1);
15599
15600 c = add_com ("break", class_breakpoint, break_command, _("\
15601 Set breakpoint at specified location.\n"
15602 BREAK_ARGS_HELP ("break")));
15603 set_cmd_completer (c, location_completer);
15604
15605 add_com_alias ("b", "break", class_run, 1);
15606 add_com_alias ("br", "break", class_run, 1);
15607 add_com_alias ("bre", "break", class_run, 1);
15608 add_com_alias ("brea", "break", class_run, 1);
15609
15610 if (dbx_commands)
15611 {
15612 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15613 Break in function/address or break at a line in the current file."),
15614 &stoplist, "stop ", 1, &cmdlist);
15615 add_cmd ("in", class_breakpoint, stopin_command,
15616 _("Break in function or address."), &stoplist);
15617 add_cmd ("at", class_breakpoint, stopat_command,
15618 _("Break at a line in the current file."), &stoplist);
15619 add_com ("status", class_info, info_breakpoints_command, _("\
15620 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15621 The \"Type\" column indicates one of:\n\
15622 \tbreakpoint - normal breakpoint\n\
15623 \twatchpoint - watchpoint\n\
15624 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15625 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15626 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15627 address and file/line number respectively.\n\
15628 \n\
15629 Convenience variable \"$_\" and default examine address for \"x\"\n\
15630 are set to the address of the last breakpoint listed unless the command\n\
15631 is prefixed with \"server \".\n\n\
15632 Convenience variable \"$bpnum\" contains the number of the last\n\
15633 breakpoint set."));
15634 }
15635
15636 add_info ("breakpoints", info_breakpoints_command, _("\
15637 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15638 The \"Type\" column indicates one of:\n\
15639 \tbreakpoint - normal breakpoint\n\
15640 \twatchpoint - watchpoint\n\
15641 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15642 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15643 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15644 address and file/line number respectively.\n\
15645 \n\
15646 Convenience variable \"$_\" and default examine address for \"x\"\n\
15647 are set to the address of the last breakpoint listed unless the command\n\
15648 is prefixed with \"server \".\n\n\
15649 Convenience variable \"$bpnum\" contains the number of the last\n\
15650 breakpoint set."));
15651
15652 add_info_alias ("b", "breakpoints", 1);
15653
15654 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15655 Status of all breakpoints, or breakpoint number NUMBER.\n\
15656 The \"Type\" column indicates one of:\n\
15657 \tbreakpoint - normal breakpoint\n\
15658 \twatchpoint - watchpoint\n\
15659 \tlongjmp - internal breakpoint used to step through longjmp()\n\
15660 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15661 \tuntil - internal breakpoint used by the \"until\" command\n\
15662 \tfinish - internal breakpoint used by the \"finish\" command\n\
15663 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15664 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15665 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15666 address and file/line number respectively.\n\
15667 \n\
15668 Convenience variable \"$_\" and default examine address for \"x\"\n\
15669 are set to the address of the last breakpoint listed unless the command\n\
15670 is prefixed with \"server \".\n\n\
15671 Convenience variable \"$bpnum\" contains the number of the last\n\
15672 breakpoint set."),
15673 &maintenanceinfolist);
15674
15675 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
15676 Set catchpoints to catch events."),
15677 &catch_cmdlist, "catch ",
15678 0/*allow-unknown*/, &cmdlist);
15679
15680 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
15681 Set temporary catchpoints to catch events."),
15682 &tcatch_cmdlist, "tcatch ",
15683 0/*allow-unknown*/, &cmdlist);
15684
15685 add_catch_command ("fork", _("Catch calls to fork."),
15686 catch_fork_command_1,
15687 NULL,
15688 (void *) (uintptr_t) catch_fork_permanent,
15689 (void *) (uintptr_t) catch_fork_temporary);
15690 add_catch_command ("vfork", _("Catch calls to vfork."),
15691 catch_fork_command_1,
15692 NULL,
15693 (void *) (uintptr_t) catch_vfork_permanent,
15694 (void *) (uintptr_t) catch_vfork_temporary);
15695 add_catch_command ("exec", _("Catch calls to exec."),
15696 catch_exec_command_1,
15697 NULL,
15698 CATCH_PERMANENT,
15699 CATCH_TEMPORARY);
15700 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15701 Usage: catch load [REGEX]\n\
15702 If REGEX is given, only stop for libraries matching the regular expression."),
15703 catch_load_command_1,
15704 NULL,
15705 CATCH_PERMANENT,
15706 CATCH_TEMPORARY);
15707 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15708 Usage: catch unload [REGEX]\n\
15709 If REGEX is given, only stop for libraries matching the regular expression."),
15710 catch_unload_command_1,
15711 NULL,
15712 CATCH_PERMANENT,
15713 CATCH_TEMPORARY);
15714
15715 c = add_com ("watch", class_breakpoint, watch_command, _("\
15716 Set a watchpoint for an expression.\n\
15717 Usage: watch [-l|-location] EXPRESSION\n\
15718 A watchpoint stops execution of your program whenever the value of\n\
15719 an expression changes.\n\
15720 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15721 the memory to which it refers."));
15722 set_cmd_completer (c, expression_completer);
15723
15724 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15725 Set a read watchpoint for an expression.\n\
15726 Usage: rwatch [-l|-location] EXPRESSION\n\
15727 A watchpoint stops execution of your program whenever the value of\n\
15728 an expression is read.\n\
15729 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15730 the memory to which it refers."));
15731 set_cmd_completer (c, expression_completer);
15732
15733 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15734 Set a watchpoint for an expression.\n\
15735 Usage: awatch [-l|-location] EXPRESSION\n\
15736 A watchpoint stops execution of your program whenever the value of\n\
15737 an expression is either read or written.\n\
15738 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15739 the memory to which it refers."));
15740 set_cmd_completer (c, expression_completer);
15741
15742 add_info ("watchpoints", info_watchpoints_command, _("\
15743 Status of specified watchpoints (all watchpoints if no argument)."));
15744
15745 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15746 respond to changes - contrary to the description. */
15747 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15748 &can_use_hw_watchpoints, _("\
15749 Set debugger's willingness to use watchpoint hardware."), _("\
15750 Show debugger's willingness to use watchpoint hardware."), _("\
15751 If zero, gdb will not use hardware for new watchpoints, even if\n\
15752 such is available. (However, any hardware watchpoints that were\n\
15753 created before setting this to nonzero, will continue to use watchpoint\n\
15754 hardware.)"),
15755 NULL,
15756 show_can_use_hw_watchpoints,
15757 &setlist, &showlist);
15758
15759 can_use_hw_watchpoints = 1;
15760
15761 /* Tracepoint manipulation commands. */
15762
15763 c = add_com ("trace", class_breakpoint, trace_command, _("\
15764 Set a tracepoint at specified location.\n\
15765 \n"
15766 BREAK_ARGS_HELP ("trace") "\n\
15767 Do \"help tracepoints\" for info on other tracepoint commands."));
15768 set_cmd_completer (c, location_completer);
15769
15770 add_com_alias ("tp", "trace", class_breakpoint, 0);
15771 add_com_alias ("tr", "trace", class_breakpoint, 1);
15772 add_com_alias ("tra", "trace", class_breakpoint, 1);
15773 add_com_alias ("trac", "trace", class_breakpoint, 1);
15774
15775 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
15776 Set a fast tracepoint at specified location.\n\
15777 \n"
15778 BREAK_ARGS_HELP ("ftrace") "\n\
15779 Do \"help tracepoints\" for info on other tracepoint commands."));
15780 set_cmd_completer (c, location_completer);
15781
15782 c = add_com ("strace", class_breakpoint, strace_command, _("\
15783 Set a static tracepoint at location or marker.\n\
15784 \n\
15785 strace [LOCATION] [if CONDITION]\n\
15786 LOCATION may be a linespec, explicit, or address location (described below) \n\
15787 or -m MARKER_ID.\n\n\
15788 If a marker id is specified, probe the marker with that name. With\n\
15789 no LOCATION, uses current execution address of the selected stack frame.\n\
15790 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15791 This collects arbitrary user data passed in the probe point call to the\n\
15792 tracing library. You can inspect it when analyzing the trace buffer,\n\
15793 by printing the $_sdata variable like any other convenience variable.\n\
15794 \n\
15795 CONDITION is a boolean expression.\n\
15796 \n" LOCATION_HELP_STRING "\n\n\
15797 Multiple tracepoints at one place are permitted, and useful if their\n\
15798 conditions are different.\n\
15799 \n\
15800 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15801 Do \"help tracepoints\" for info on other tracepoint commands."));
15802 set_cmd_completer (c, location_completer);
15803
15804 add_info ("tracepoints", info_tracepoints_command, _("\
15805 Status of specified tracepoints (all tracepoints if no argument).\n\
15806 Convenience variable \"$tpnum\" contains the number of the\n\
15807 last tracepoint set."));
15808
15809 add_info_alias ("tp", "tracepoints", 1);
15810
15811 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15812 Delete specified tracepoints.\n\
15813 Arguments are tracepoint numbers, separated by spaces.\n\
15814 No argument means delete all tracepoints."),
15815 &deletelist);
15816 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
15817
15818 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15819 Disable specified tracepoints.\n\
15820 Arguments are tracepoint numbers, separated by spaces.\n\
15821 No argument means disable all tracepoints."),
15822 &disablelist);
15823 deprecate_cmd (c, "disable");
15824
15825 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15826 Enable specified tracepoints.\n\
15827 Arguments are tracepoint numbers, separated by spaces.\n\
15828 No argument means enable all tracepoints."),
15829 &enablelist);
15830 deprecate_cmd (c, "enable");
15831
15832 add_com ("passcount", class_trace, trace_pass_command, _("\
15833 Set the passcount for a tracepoint.\n\
15834 The trace will end when the tracepoint has been passed 'count' times.\n\
15835 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15836 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15837
15838 add_basic_prefix_cmd ("save", class_breakpoint,
15839 _("Save breakpoint definitions as a script."),
15840 &save_cmdlist, "save ",
15841 0/*allow-unknown*/, &cmdlist);
15842
15843 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15844 Save current breakpoint definitions as a script.\n\
15845 This includes all types of breakpoints (breakpoints, watchpoints,\n\
15846 catchpoints, tracepoints). Use the 'source' command in another debug\n\
15847 session to restore them."),
15848 &save_cmdlist);
15849 set_cmd_completer (c, filename_completer);
15850
15851 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
15852 Save current tracepoint definitions as a script.\n\
15853 Use the 'source' command in another debug session to restore them."),
15854 &save_cmdlist);
15855 set_cmd_completer (c, filename_completer);
15856
15857 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
15858 deprecate_cmd (c, "save tracepoints");
15859
15860 add_basic_prefix_cmd ("breakpoint", class_maintenance, _("\
15861 Breakpoint specific settings.\n\
15862 Configure various breakpoint-specific variables such as\n\
15863 pending breakpoint behavior."),
15864 &breakpoint_set_cmdlist, "set breakpoint ",
15865 0/*allow-unknown*/, &setlist);
15866 add_show_prefix_cmd ("breakpoint", class_maintenance, _("\
15867 Breakpoint specific settings.\n\
15868 Configure various breakpoint-specific variables such as\n\
15869 pending breakpoint behavior."),
15870 &breakpoint_show_cmdlist, "show breakpoint ",
15871 0/*allow-unknown*/, &showlist);
15872
15873 add_setshow_auto_boolean_cmd ("pending", no_class,
15874 &pending_break_support, _("\
15875 Set debugger's behavior regarding pending breakpoints."), _("\
15876 Show debugger's behavior regarding pending breakpoints."), _("\
15877 If on, an unrecognized breakpoint location will cause gdb to create a\n\
15878 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15879 an error. If auto, an unrecognized breakpoint location results in a\n\
15880 user-query to see if a pending breakpoint should be created."),
15881 NULL,
15882 show_pending_break_support,
15883 &breakpoint_set_cmdlist,
15884 &breakpoint_show_cmdlist);
15885
15886 pending_break_support = AUTO_BOOLEAN_AUTO;
15887
15888 add_setshow_boolean_cmd ("auto-hw", no_class,
15889 &automatic_hardware_breakpoints, _("\
15890 Set automatic usage of hardware breakpoints."), _("\
15891 Show automatic usage of hardware breakpoints."), _("\
15892 If set, the debugger will automatically use hardware breakpoints for\n\
15893 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15894 a warning will be emitted for such breakpoints."),
15895 NULL,
15896 show_automatic_hardware_breakpoints,
15897 &breakpoint_set_cmdlist,
15898 &breakpoint_show_cmdlist);
15899
15900 add_setshow_boolean_cmd ("always-inserted", class_support,
15901 &always_inserted_mode, _("\
15902 Set mode for inserting breakpoints."), _("\
15903 Show mode for inserting breakpoints."), _("\
15904 When this mode is on, breakpoints are inserted immediately as soon as\n\
15905 they're created, kept inserted even when execution stops, and removed\n\
15906 only when the user deletes them. When this mode is off (the default),\n\
15907 breakpoints are inserted only when execution continues, and removed\n\
15908 when execution stops."),
15909 NULL,
15910 &show_always_inserted_mode,
15911 &breakpoint_set_cmdlist,
15912 &breakpoint_show_cmdlist);
15913
15914 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15915 condition_evaluation_enums,
15916 &condition_evaluation_mode_1, _("\
15917 Set mode of breakpoint condition evaluation."), _("\
15918 Show mode of breakpoint condition evaluation."), _("\
15919 When this is set to \"host\", breakpoint conditions will be\n\
15920 evaluated on the host's side by GDB. When it is set to \"target\",\n\
15921 breakpoint conditions will be downloaded to the target (if the target\n\
15922 supports such feature) and conditions will be evaluated on the target's side.\n\
15923 If this is set to \"auto\" (default), this will be automatically set to\n\
15924 \"target\" if it supports condition evaluation, otherwise it will\n\
15925 be set to \"gdb\""),
15926 &set_condition_evaluation_mode,
15927 &show_condition_evaluation_mode,
15928 &breakpoint_set_cmdlist,
15929 &breakpoint_show_cmdlist);
15930
15931 add_com ("break-range", class_breakpoint, break_range_command, _("\
15932 Set a breakpoint for an address range.\n\
15933 break-range START-LOCATION, END-LOCATION\n\
15934 where START-LOCATION and END-LOCATION can be one of the following:\n\
15935 LINENUM, for that line in the current file,\n\
15936 FILE:LINENUM, for that line in that file,\n\
15937 +OFFSET, for that number of lines after the current line\n\
15938 or the start of the range\n\
15939 FUNCTION, for the first line in that function,\n\
15940 FILE:FUNCTION, to distinguish among like-named static functions.\n\
15941 *ADDRESS, for the instruction at that address.\n\
15942 \n\
15943 The breakpoint will stop execution of the inferior whenever it executes\n\
15944 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
15945 range (including START-LOCATION and END-LOCATION)."));
15946
15947 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
15948 Set a dynamic printf at specified location.\n\
15949 dprintf location,format string,arg1,arg2,...\n\
15950 location may be a linespec, explicit, or address location.\n"
15951 "\n" LOCATION_HELP_STRING));
15952 set_cmd_completer (c, location_completer);
15953
15954 add_setshow_enum_cmd ("dprintf-style", class_support,
15955 dprintf_style_enums, &dprintf_style, _("\
15956 Set the style of usage for dynamic printf."), _("\
15957 Show the style of usage for dynamic printf."), _("\
15958 This setting chooses how GDB will do a dynamic printf.\n\
15959 If the value is \"gdb\", then the printing is done by GDB to its own\n\
15960 console, as with the \"printf\" command.\n\
15961 If the value is \"call\", the print is done by calling a function in your\n\
15962 program; by default printf(), but you can choose a different function or\n\
15963 output stream by setting dprintf-function and dprintf-channel."),
15964 update_dprintf_commands, NULL,
15965 &setlist, &showlist);
15966
15967 dprintf_function = xstrdup ("printf");
15968 add_setshow_string_cmd ("dprintf-function", class_support,
15969 &dprintf_function, _("\
15970 Set the function to use for dynamic printf."), _("\
15971 Show the function to use for dynamic printf."), NULL,
15972 update_dprintf_commands, NULL,
15973 &setlist, &showlist);
15974
15975 dprintf_channel = xstrdup ("");
15976 add_setshow_string_cmd ("dprintf-channel", class_support,
15977 &dprintf_channel, _("\
15978 Set the channel to use for dynamic printf."), _("\
15979 Show the channel to use for dynamic printf."), NULL,
15980 update_dprintf_commands, NULL,
15981 &setlist, &showlist);
15982
15983 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
15984 &disconnected_dprintf, _("\
15985 Set whether dprintf continues after GDB disconnects."), _("\
15986 Show whether dprintf continues after GDB disconnects."), _("\
15987 Use this to let dprintf commands continue to hit and produce output\n\
15988 even if GDB disconnects or detaches from the target."),
15989 NULL,
15990 NULL,
15991 &setlist, &showlist);
15992
15993 add_com ("agent-printf", class_vars, agent_printf_command, _("\
15994 Target agent only formatted printing, like the C \"printf\" function.\n\
15995 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
15996 This supports most C printf format specifications, like %s, %d, etc.\n\
15997 This is useful for formatted output in user-defined commands."));
15998
15999 automatic_hardware_breakpoints = true;
16000
16001 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed);
16002 gdb::observers::thread_exit.attach (remove_threaded_breakpoints);
16003 }
This page took 0.40593 seconds and 4 git commands to generate.