gdb: get jiter objfile from a bound minsym
[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 (struct event_location *location,
93 struct linespec_result *canonical,
94 enum bptype type_wanted);
95
96 static void create_breakpoints_sal_default (struct gdbarch *,
97 struct linespec_result *,
98 gdb::unique_xmalloc_ptr<char>,
99 gdb::unique_xmalloc_ptr<char>,
100 enum bptype,
101 enum bpdisp, int, int,
102 int,
103 const struct breakpoint_ops *,
104 int, int, int, unsigned);
105
106 static std::vector<symtab_and_line> decode_location_default
107 (struct breakpoint *b, struct event_location *location,
108 struct program_space *search_pspace);
109
110 static int can_use_hardware_watchpoint
111 (const std::vector<value_ref_ptr> &vals);
112
113 static void mention (struct breakpoint *);
114
115 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
116 enum bptype,
117 const struct breakpoint_ops *);
118 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
119 const struct symtab_and_line *);
120
121 /* This function is used in gdbtk sources and thus can not be made
122 static. */
123 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
124 struct symtab_and_line,
125 enum bptype,
126 const struct breakpoint_ops *);
127
128 static struct breakpoint *
129 momentary_breakpoint_from_master (struct breakpoint *orig,
130 enum bptype type,
131 const struct breakpoint_ops *ops,
132 int loc_enabled);
133
134 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
135
136 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
137 CORE_ADDR bpaddr,
138 enum bptype bptype);
139
140 static void describe_other_breakpoints (struct gdbarch *,
141 struct program_space *, CORE_ADDR,
142 struct obj_section *, int);
143
144 static int watchpoint_locations_match (struct bp_location *loc1,
145 struct bp_location *loc2);
146
147 static int breakpoint_locations_match (struct bp_location *loc1,
148 struct bp_location *loc2,
149 bool sw_hw_bps_match = false);
150
151 static int breakpoint_location_address_match (struct bp_location *bl,
152 const struct address_space *aspace,
153 CORE_ADDR addr);
154
155 static int breakpoint_location_address_range_overlap (struct bp_location *,
156 const address_space *,
157 CORE_ADDR, int);
158
159 static int remove_breakpoint (struct bp_location *);
160 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
161
162 static enum print_stop_action print_bp_stop_message (bpstat bs);
163
164 static int hw_breakpoint_used_count (void);
165
166 static int hw_watchpoint_use_count (struct breakpoint *);
167
168 static int hw_watchpoint_used_count_others (struct breakpoint *except,
169 enum bptype type,
170 int *other_type_used);
171
172 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
173 int count);
174
175 static void 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 if (*exp == 0)
838 {
839 xfree (b->cond_string);
840 b->cond_string = nullptr;
841
842 if (is_watchpoint (b))
843 static_cast<watchpoint *> (b)->cond_exp.reset ();
844 else
845 {
846 for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)
847 {
848 loc->cond.reset ();
849
850 /* No need to free the condition agent expression
851 bytecode (if we have one). We will handle this
852 when we go through update_global_location_list. */
853 }
854 }
855
856 if (from_tty)
857 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
858 }
859 else
860 {
861 if (is_watchpoint (b))
862 {
863 innermost_block_tracker tracker;
864 const char *arg = exp;
865 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
866 if (*arg != 0)
867 error (_("Junk at end of expression"));
868 watchpoint *w = static_cast<watchpoint *> (b);
869 w->cond_exp = std::move (new_exp);
870 w->cond_exp_valid_block = tracker.block ();
871 }
872 else
873 {
874 /* Parse and set condition expressions. We make two passes.
875 In the first, we parse the condition string to see if it
876 is valid in all locations. If so, the condition would be
877 accepted. So we go ahead and set the locations'
878 conditions. In case a failing case is found, we throw
879 the error and the condition string will be rejected.
880 This two-pass approach is taken to avoid setting the
881 state of locations in case of a reject. */
882 for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)
883 {
884 const char *arg = exp;
885 parse_exp_1 (&arg, loc->address,
886 block_for_pc (loc->address), 0);
887 if (*arg != 0)
888 error (_("Junk at end of expression"));
889 }
890
891 /* If we reach here, the condition is valid at all locations. */
892 for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)
893 {
894 const char *arg = exp;
895 loc->cond =
896 parse_exp_1 (&arg, loc->address,
897 block_for_pc (loc->address), 0);
898 }
899 }
900
901 /* We know that the new condition parsed successfully. The
902 condition string of the breakpoint can be safely updated. */
903 xfree (b->cond_string);
904 b->cond_string = xstrdup (exp);
905 b->condition_not_parsed = 0;
906 }
907 mark_breakpoint_modified (b);
908
909 gdb::observers::breakpoint_modified.notify (b);
910 }
911
912 /* Completion for the "condition" command. */
913
914 static void
915 condition_completer (struct cmd_list_element *cmd,
916 completion_tracker &tracker,
917 const char *text, const char *word)
918 {
919 const char *space;
920
921 text = skip_spaces (text);
922 space = skip_to_space (text);
923 if (*space == '\0')
924 {
925 int len;
926 struct breakpoint *b;
927
928 if (text[0] == '$')
929 {
930 /* We don't support completion of history indices. */
931 if (!isdigit (text[1]))
932 complete_internalvar (tracker, &text[1]);
933 return;
934 }
935
936 /* We're completing the breakpoint number. */
937 len = strlen (text);
938
939 ALL_BREAKPOINTS (b)
940 {
941 char number[50];
942
943 xsnprintf (number, sizeof (number), "%d", b->number);
944
945 if (strncmp (number, text, len) == 0)
946 tracker.add_completion (make_unique_xstrdup (number));
947 }
948
949 return;
950 }
951
952 /* We're completing the expression part. */
953 text = skip_spaces (space);
954 expression_completer (cmd, tracker, text, word);
955 }
956
957 /* condition N EXP -- set break condition of breakpoint N to EXP. */
958
959 static void
960 condition_command (const char *arg, int from_tty)
961 {
962 struct breakpoint *b;
963 const char *p;
964 int bnum;
965
966 if (arg == 0)
967 error_no_arg (_("breakpoint number"));
968
969 p = arg;
970 bnum = get_number (&p);
971 if (bnum == 0)
972 error (_("Bad breakpoint argument: '%s'"), arg);
973
974 ALL_BREAKPOINTS (b)
975 if (b->number == bnum)
976 {
977 /* Check if this breakpoint has a "stop" method implemented in an
978 extension language. This method and conditions entered into GDB
979 from the CLI are mutually exclusive. */
980 const struct extension_language_defn *extlang
981 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
982
983 if (extlang != NULL)
984 {
985 error (_("Only one stop condition allowed. There is currently"
986 " a %s stop condition defined for this breakpoint."),
987 ext_lang_capitalized_name (extlang));
988 }
989 set_breakpoint_condition (b, p, from_tty);
990
991 if (is_breakpoint (b))
992 update_global_location_list (UGLL_MAY_INSERT);
993
994 return;
995 }
996
997 error (_("No breakpoint number %d."), bnum);
998 }
999
1000 /* Check that COMMAND do not contain commands that are suitable
1001 only for tracepoints and not suitable for ordinary breakpoints.
1002 Throw if any such commands is found. */
1003
1004 static void
1005 check_no_tracepoint_commands (struct command_line *commands)
1006 {
1007 struct command_line *c;
1008
1009 for (c = commands; c; c = c->next)
1010 {
1011 if (c->control_type == while_stepping_control)
1012 error (_("The 'while-stepping' command can "
1013 "only be used for tracepoints"));
1014
1015 check_no_tracepoint_commands (c->body_list_0.get ());
1016 check_no_tracepoint_commands (c->body_list_1.get ());
1017
1018 /* Not that command parsing removes leading whitespace and comment
1019 lines and also empty lines. So, we only need to check for
1020 command directly. */
1021 if (strstr (c->line, "collect ") == c->line)
1022 error (_("The 'collect' command can only be used for tracepoints"));
1023
1024 if (strstr (c->line, "teval ") == c->line)
1025 error (_("The 'teval' command can only be used for tracepoints"));
1026 }
1027 }
1028
1029 struct longjmp_breakpoint : public breakpoint
1030 {
1031 ~longjmp_breakpoint () override;
1032 };
1033
1034 /* Encapsulate tests for different types of tracepoints. */
1035
1036 static bool
1037 is_tracepoint_type (bptype type)
1038 {
1039 return (type == bp_tracepoint
1040 || type == bp_fast_tracepoint
1041 || type == bp_static_tracepoint);
1042 }
1043
1044 static bool
1045 is_longjmp_type (bptype type)
1046 {
1047 return type == bp_longjmp || type == bp_exception;
1048 }
1049
1050 /* See breakpoint.h. */
1051
1052 bool
1053 is_tracepoint (const struct breakpoint *b)
1054 {
1055 return is_tracepoint_type (b->type);
1056 }
1057
1058 /* Factory function to create an appropriate instance of breakpoint given
1059 TYPE. */
1060
1061 static std::unique_ptr<breakpoint>
1062 new_breakpoint_from_type (bptype type)
1063 {
1064 breakpoint *b;
1065
1066 if (is_tracepoint_type (type))
1067 b = new tracepoint ();
1068 else if (is_longjmp_type (type))
1069 b = new longjmp_breakpoint ();
1070 else
1071 b = new breakpoint ();
1072
1073 return std::unique_ptr<breakpoint> (b);
1074 }
1075
1076 /* A helper function that validates that COMMANDS are valid for a
1077 breakpoint. This function will throw an exception if a problem is
1078 found. */
1079
1080 static void
1081 validate_commands_for_breakpoint (struct breakpoint *b,
1082 struct command_line *commands)
1083 {
1084 if (is_tracepoint (b))
1085 {
1086 struct tracepoint *t = (struct tracepoint *) b;
1087 struct command_line *c;
1088 struct command_line *while_stepping = 0;
1089
1090 /* Reset the while-stepping step count. The previous commands
1091 might have included a while-stepping action, while the new
1092 ones might not. */
1093 t->step_count = 0;
1094
1095 /* We need to verify that each top-level element of commands is
1096 valid for tracepoints, that there's at most one
1097 while-stepping element, and that the while-stepping's body
1098 has valid tracing commands excluding nested while-stepping.
1099 We also need to validate the tracepoint action line in the
1100 context of the tracepoint --- validate_actionline actually
1101 has side effects, like setting the tracepoint's
1102 while-stepping STEP_COUNT, in addition to checking if the
1103 collect/teval actions parse and make sense in the
1104 tracepoint's context. */
1105 for (c = commands; c; c = c->next)
1106 {
1107 if (c->control_type == while_stepping_control)
1108 {
1109 if (b->type == bp_fast_tracepoint)
1110 error (_("The 'while-stepping' command "
1111 "cannot be used for fast tracepoint"));
1112 else if (b->type == bp_static_tracepoint)
1113 error (_("The 'while-stepping' command "
1114 "cannot be used for static tracepoint"));
1115
1116 if (while_stepping)
1117 error (_("The 'while-stepping' command "
1118 "can be used only once"));
1119 else
1120 while_stepping = c;
1121 }
1122
1123 validate_actionline (c->line, b);
1124 }
1125 if (while_stepping)
1126 {
1127 struct command_line *c2;
1128
1129 gdb_assert (while_stepping->body_list_1 == nullptr);
1130 c2 = while_stepping->body_list_0.get ();
1131 for (; c2; c2 = c2->next)
1132 {
1133 if (c2->control_type == while_stepping_control)
1134 error (_("The 'while-stepping' command cannot be nested"));
1135 }
1136 }
1137 }
1138 else
1139 {
1140 check_no_tracepoint_commands (commands);
1141 }
1142 }
1143
1144 /* Return a vector of all the static tracepoints set at ADDR. The
1145 caller is responsible for releasing the vector. */
1146
1147 std::vector<breakpoint *>
1148 static_tracepoints_here (CORE_ADDR addr)
1149 {
1150 struct breakpoint *b;
1151 std::vector<breakpoint *> found;
1152 struct bp_location *loc;
1153
1154 ALL_BREAKPOINTS (b)
1155 if (b->type == bp_static_tracepoint)
1156 {
1157 for (loc = b->loc; loc; loc = loc->next)
1158 if (loc->address == addr)
1159 found.push_back (b);
1160 }
1161
1162 return found;
1163 }
1164
1165 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1166 validate that only allowed commands are included. */
1167
1168 void
1169 breakpoint_set_commands (struct breakpoint *b,
1170 counted_command_line &&commands)
1171 {
1172 validate_commands_for_breakpoint (b, commands.get ());
1173
1174 b->commands = std::move (commands);
1175 gdb::observers::breakpoint_modified.notify (b);
1176 }
1177
1178 /* Set the internal `silent' flag on the breakpoint. Note that this
1179 is not the same as the "silent" that may appear in the breakpoint's
1180 commands. */
1181
1182 void
1183 breakpoint_set_silent (struct breakpoint *b, int silent)
1184 {
1185 int old_silent = b->silent;
1186
1187 b->silent = silent;
1188 if (old_silent != silent)
1189 gdb::observers::breakpoint_modified.notify (b);
1190 }
1191
1192 /* Set the thread for this breakpoint. If THREAD is -1, make the
1193 breakpoint work for any thread. */
1194
1195 void
1196 breakpoint_set_thread (struct breakpoint *b, int thread)
1197 {
1198 int old_thread = b->thread;
1199
1200 b->thread = thread;
1201 if (old_thread != thread)
1202 gdb::observers::breakpoint_modified.notify (b);
1203 }
1204
1205 /* Set the task for this breakpoint. If TASK is 0, make the
1206 breakpoint work for any task. */
1207
1208 void
1209 breakpoint_set_task (struct breakpoint *b, int task)
1210 {
1211 int old_task = b->task;
1212
1213 b->task = task;
1214 if (old_task != task)
1215 gdb::observers::breakpoint_modified.notify (b);
1216 }
1217
1218 static void
1219 commands_command_1 (const char *arg, int from_tty,
1220 struct command_line *control)
1221 {
1222 counted_command_line cmd;
1223 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1224 NULL after the call to read_command_lines if the user provides an empty
1225 list of command by just typing "end". */
1226 bool cmd_read = false;
1227
1228 std::string new_arg;
1229
1230 if (arg == NULL || !*arg)
1231 {
1232 /* Argument not explicitly given. Synthesize it. */
1233 if (breakpoint_count - prev_breakpoint_count > 1)
1234 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1235 breakpoint_count);
1236 else if (breakpoint_count > 0)
1237 new_arg = string_printf ("%d", breakpoint_count);
1238 }
1239 else
1240 {
1241 /* Create a copy of ARG. This is needed because the "commands"
1242 command may be coming from a script. In that case, the read
1243 line buffer is going to be overwritten in the lambda of
1244 'map_breakpoint_numbers' below when reading the next line
1245 before we are are done parsing the breakpoint numbers. */
1246 new_arg = arg;
1247 }
1248 arg = new_arg.c_str ();
1249
1250 map_breakpoint_numbers
1251 (arg, [&] (breakpoint *b)
1252 {
1253 if (!cmd_read)
1254 {
1255 gdb_assert (cmd == NULL);
1256 if (control != NULL)
1257 cmd = control->body_list_0;
1258 else
1259 {
1260 std::string str
1261 = string_printf (_("Type commands for breakpoint(s) "
1262 "%s, one per line."),
1263 arg);
1264
1265 auto do_validate = [=] (const char *line)
1266 {
1267 validate_actionline (line, b);
1268 };
1269 gdb::function_view<void (const char *)> validator;
1270 if (is_tracepoint (b))
1271 validator = do_validate;
1272
1273 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1274 }
1275 cmd_read = true;
1276 }
1277
1278 /* If a breakpoint was on the list more than once, we don't need to
1279 do anything. */
1280 if (b->commands != cmd)
1281 {
1282 validate_commands_for_breakpoint (b, cmd.get ());
1283 b->commands = cmd;
1284 gdb::observers::breakpoint_modified.notify (b);
1285 }
1286 });
1287 }
1288
1289 static void
1290 commands_command (const char *arg, int from_tty)
1291 {
1292 commands_command_1 (arg, from_tty, NULL);
1293 }
1294
1295 /* Like commands_command, but instead of reading the commands from
1296 input stream, takes them from an already parsed command structure.
1297
1298 This is used by cli-script.c to DTRT with breakpoint commands
1299 that are part of if and while bodies. */
1300 enum command_control_type
1301 commands_from_control_command (const char *arg, struct command_line *cmd)
1302 {
1303 commands_command_1 (arg, 0, cmd);
1304 return simple_control;
1305 }
1306
1307 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1308
1309 static int
1310 bp_location_has_shadow (struct bp_location *bl)
1311 {
1312 if (bl->loc_type != bp_loc_software_breakpoint)
1313 return 0;
1314 if (!bl->inserted)
1315 return 0;
1316 if (bl->target_info.shadow_len == 0)
1317 /* BL isn't valid, or doesn't shadow memory. */
1318 return 0;
1319 return 1;
1320 }
1321
1322 /* Update BUF, which is LEN bytes read from the target address
1323 MEMADDR, by replacing a memory breakpoint with its shadowed
1324 contents.
1325
1326 If READBUF is not NULL, this buffer must not overlap with the of
1327 the breakpoint location's shadow_contents buffer. Otherwise, a
1328 failed assertion internal error will be raised. */
1329
1330 static void
1331 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1332 const gdb_byte *writebuf_org,
1333 ULONGEST memaddr, LONGEST len,
1334 struct bp_target_info *target_info,
1335 struct gdbarch *gdbarch)
1336 {
1337 /* Now do full processing of the found relevant range of elements. */
1338 CORE_ADDR bp_addr = 0;
1339 int bp_size = 0;
1340 int bptoffset = 0;
1341
1342 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1343 current_program_space->aspace, 0))
1344 {
1345 /* The breakpoint is inserted in a different address space. */
1346 return;
1347 }
1348
1349 /* Addresses and length of the part of the breakpoint that
1350 we need to copy. */
1351 bp_addr = target_info->placed_address;
1352 bp_size = target_info->shadow_len;
1353
1354 if (bp_addr + bp_size <= memaddr)
1355 {
1356 /* The breakpoint is entirely before the chunk of memory we are
1357 reading. */
1358 return;
1359 }
1360
1361 if (bp_addr >= memaddr + len)
1362 {
1363 /* The breakpoint is entirely after the chunk of memory we are
1364 reading. */
1365 return;
1366 }
1367
1368 /* Offset within shadow_contents. */
1369 if (bp_addr < memaddr)
1370 {
1371 /* Only copy the second part of the breakpoint. */
1372 bp_size -= memaddr - bp_addr;
1373 bptoffset = memaddr - bp_addr;
1374 bp_addr = memaddr;
1375 }
1376
1377 if (bp_addr + bp_size > memaddr + len)
1378 {
1379 /* Only copy the first part of the breakpoint. */
1380 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1381 }
1382
1383 if (readbuf != NULL)
1384 {
1385 /* Verify that the readbuf buffer does not overlap with the
1386 shadow_contents buffer. */
1387 gdb_assert (target_info->shadow_contents >= readbuf + len
1388 || readbuf >= (target_info->shadow_contents
1389 + target_info->shadow_len));
1390
1391 /* Update the read buffer with this inserted breakpoint's
1392 shadow. */
1393 memcpy (readbuf + bp_addr - memaddr,
1394 target_info->shadow_contents + bptoffset, bp_size);
1395 }
1396 else
1397 {
1398 const unsigned char *bp;
1399 CORE_ADDR addr = target_info->reqstd_address;
1400 int placed_size;
1401
1402 /* Update the shadow with what we want to write to memory. */
1403 memcpy (target_info->shadow_contents + bptoffset,
1404 writebuf_org + bp_addr - memaddr, bp_size);
1405
1406 /* Determine appropriate breakpoint contents and size for this
1407 address. */
1408 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1409
1410 /* Update the final write buffer with this inserted
1411 breakpoint's INSN. */
1412 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1413 }
1414 }
1415
1416 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1417 by replacing any memory breakpoints with their shadowed contents.
1418
1419 If READBUF is not NULL, this buffer must not overlap with any of
1420 the breakpoint location's shadow_contents buffers. Otherwise,
1421 a failed assertion internal error will be raised.
1422
1423 The range of shadowed area by each bp_location is:
1424 bl->address - bp_locations_placed_address_before_address_max
1425 up to bl->address + bp_locations_shadow_len_after_address_max
1426 The range we were requested to resolve shadows for is:
1427 memaddr ... memaddr + len
1428 Thus the safe cutoff boundaries for performance optimization are
1429 memaddr + len <= (bl->address
1430 - bp_locations_placed_address_before_address_max)
1431 and:
1432 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1433
1434 void
1435 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1436 const gdb_byte *writebuf_org,
1437 ULONGEST memaddr, LONGEST len)
1438 {
1439 /* Left boundary, right boundary and median element of our binary
1440 search. */
1441 unsigned bc_l, bc_r, bc;
1442
1443 /* Find BC_L which is a leftmost element which may affect BUF
1444 content. It is safe to report lower value but a failure to
1445 report higher one. */
1446
1447 bc_l = 0;
1448 bc_r = bp_locations_count;
1449 while (bc_l + 1 < bc_r)
1450 {
1451 struct bp_location *bl;
1452
1453 bc = (bc_l + bc_r) / 2;
1454 bl = bp_locations[bc];
1455
1456 /* Check first BL->ADDRESS will not overflow due to the added
1457 constant. Then advance the left boundary only if we are sure
1458 the BC element can in no way affect the BUF content (MEMADDR
1459 to MEMADDR + LEN range).
1460
1461 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1462 offset so that we cannot miss a breakpoint with its shadow
1463 range tail still reaching MEMADDR. */
1464
1465 if ((bl->address + bp_locations_shadow_len_after_address_max
1466 >= bl->address)
1467 && (bl->address + bp_locations_shadow_len_after_address_max
1468 <= memaddr))
1469 bc_l = bc;
1470 else
1471 bc_r = bc;
1472 }
1473
1474 /* Due to the binary search above, we need to make sure we pick the
1475 first location that's at BC_L's address. E.g., if there are
1476 multiple locations at the same address, BC_L may end up pointing
1477 at a duplicate location, and miss the "master"/"inserted"
1478 location. Say, given locations L1, L2 and L3 at addresses A and
1479 B:
1480
1481 L1@A, L2@A, L3@B, ...
1482
1483 BC_L could end up pointing at location L2, while the "master"
1484 location could be L1. Since the `loc->inserted' flag is only set
1485 on "master" locations, we'd forget to restore the shadow of L1
1486 and L2. */
1487 while (bc_l > 0
1488 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1489 bc_l--;
1490
1491 /* Now do full processing of the found relevant range of elements. */
1492
1493 for (bc = bc_l; bc < bp_locations_count; bc++)
1494 {
1495 struct bp_location *bl = bp_locations[bc];
1496
1497 /* bp_location array has BL->OWNER always non-NULL. */
1498 if (bl->owner->type == bp_none)
1499 warning (_("reading through apparently deleted breakpoint #%d?"),
1500 bl->owner->number);
1501
1502 /* Performance optimization: any further element can no longer affect BUF
1503 content. */
1504
1505 if (bl->address >= bp_locations_placed_address_before_address_max
1506 && memaddr + len <= (bl->address
1507 - bp_locations_placed_address_before_address_max))
1508 break;
1509
1510 if (!bp_location_has_shadow (bl))
1511 continue;
1512
1513 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1514 memaddr, len, &bl->target_info, bl->gdbarch);
1515 }
1516 }
1517
1518 /* See breakpoint.h. */
1519
1520 bool
1521 is_breakpoint (const struct breakpoint *bpt)
1522 {
1523 return (bpt->type == bp_breakpoint
1524 || bpt->type == bp_hardware_breakpoint
1525 || bpt->type == bp_dprintf);
1526 }
1527
1528 /* Return true if BPT is of any hardware watchpoint kind. */
1529
1530 static bool
1531 is_hardware_watchpoint (const struct breakpoint *bpt)
1532 {
1533 return (bpt->type == bp_hardware_watchpoint
1534 || bpt->type == bp_read_watchpoint
1535 || bpt->type == bp_access_watchpoint);
1536 }
1537
1538 /* See breakpoint.h. */
1539
1540 bool
1541 is_watchpoint (const struct breakpoint *bpt)
1542 {
1543 return (is_hardware_watchpoint (bpt)
1544 || bpt->type == bp_watchpoint);
1545 }
1546
1547 /* Returns true if the current thread and its running state are safe
1548 to evaluate or update watchpoint B. Watchpoints on local
1549 expressions need to be evaluated in the context of the thread that
1550 was current when the watchpoint was created, and, that thread needs
1551 to be stopped to be able to select the correct frame context.
1552 Watchpoints on global expressions can be evaluated on any thread,
1553 and in any state. It is presently left to the target allowing
1554 memory accesses when threads are running. */
1555
1556 static int
1557 watchpoint_in_thread_scope (struct watchpoint *b)
1558 {
1559 return (b->pspace == current_program_space
1560 && (b->watchpoint_thread == null_ptid
1561 || (inferior_ptid == b->watchpoint_thread
1562 && !inferior_thread ()->executing)));
1563 }
1564
1565 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1566 associated bp_watchpoint_scope breakpoint. */
1567
1568 static void
1569 watchpoint_del_at_next_stop (struct watchpoint *w)
1570 {
1571 if (w->related_breakpoint != w)
1572 {
1573 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1574 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1575 w->related_breakpoint->disposition = disp_del_at_next_stop;
1576 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1577 w->related_breakpoint = w;
1578 }
1579 w->disposition = disp_del_at_next_stop;
1580 }
1581
1582 /* Extract a bitfield value from value VAL using the bit parameters contained in
1583 watchpoint W. */
1584
1585 static struct value *
1586 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1587 {
1588 struct value *bit_val;
1589
1590 if (val == NULL)
1591 return NULL;
1592
1593 bit_val = allocate_value (value_type (val));
1594
1595 unpack_value_bitfield (bit_val,
1596 w->val_bitpos,
1597 w->val_bitsize,
1598 value_contents_for_printing (val),
1599 value_offset (val),
1600 val);
1601
1602 return bit_val;
1603 }
1604
1605 /* Allocate a dummy location and add it to B, which must be a software
1606 watchpoint. This is required because even if a software watchpoint
1607 is not watching any memory, bpstat_stop_status requires a location
1608 to be able to report stops. */
1609
1610 static void
1611 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1612 struct program_space *pspace)
1613 {
1614 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1615
1616 b->loc = allocate_bp_location (b);
1617 b->loc->pspace = pspace;
1618 b->loc->address = -1;
1619 b->loc->length = -1;
1620 }
1621
1622 /* Returns true if B is a software watchpoint that is not watching any
1623 memory (e.g., "watch $pc"). */
1624
1625 static bool
1626 is_no_memory_software_watchpoint (struct breakpoint *b)
1627 {
1628 return (b->type == bp_watchpoint
1629 && b->loc != NULL
1630 && b->loc->next == NULL
1631 && b->loc->address == -1
1632 && b->loc->length == -1);
1633 }
1634
1635 /* Assuming that B is a watchpoint:
1636 - Reparse watchpoint expression, if REPARSE is non-zero
1637 - Evaluate expression and store the result in B->val
1638 - Evaluate the condition if there is one, and store the result
1639 in b->loc->cond.
1640 - Update the list of values that must be watched in B->loc.
1641
1642 If the watchpoint disposition is disp_del_at_next_stop, then do
1643 nothing. If this is local watchpoint that is out of scope, delete
1644 it.
1645
1646 Even with `set breakpoint always-inserted on' the watchpoints are
1647 removed + inserted on each stop here. Normal breakpoints must
1648 never be removed because they might be missed by a running thread
1649 when debugging in non-stop mode. On the other hand, hardware
1650 watchpoints (is_hardware_watchpoint; processed here) are specific
1651 to each LWP since they are stored in each LWP's hardware debug
1652 registers. Therefore, such LWP must be stopped first in order to
1653 be able to modify its hardware watchpoints.
1654
1655 Hardware watchpoints must be reset exactly once after being
1656 presented to the user. It cannot be done sooner, because it would
1657 reset the data used to present the watchpoint hit to the user. And
1658 it must not be done later because it could display the same single
1659 watchpoint hit during multiple GDB stops. Note that the latter is
1660 relevant only to the hardware watchpoint types bp_read_watchpoint
1661 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1662 not user-visible - its hit is suppressed if the memory content has
1663 not changed.
1664
1665 The following constraints influence the location where we can reset
1666 hardware watchpoints:
1667
1668 * target_stopped_by_watchpoint and target_stopped_data_address are
1669 called several times when GDB stops.
1670
1671 [linux]
1672 * Multiple hardware watchpoints can be hit at the same time,
1673 causing GDB to stop. GDB only presents one hardware watchpoint
1674 hit at a time as the reason for stopping, and all the other hits
1675 are presented later, one after the other, each time the user
1676 requests the execution to be resumed. Execution is not resumed
1677 for the threads still having pending hit event stored in
1678 LWP_INFO->STATUS. While the watchpoint is already removed from
1679 the inferior on the first stop the thread hit event is kept being
1680 reported from its cached value by linux_nat_stopped_data_address
1681 until the real thread resume happens after the watchpoint gets
1682 presented and thus its LWP_INFO->STATUS gets reset.
1683
1684 Therefore the hardware watchpoint hit can get safely reset on the
1685 watchpoint removal from inferior. */
1686
1687 static void
1688 update_watchpoint (struct watchpoint *b, int reparse)
1689 {
1690 int within_current_scope;
1691 struct frame_id saved_frame_id;
1692 int frame_saved;
1693
1694 /* If this is a local watchpoint, we only want to check if the
1695 watchpoint frame is in scope if the current thread is the thread
1696 that was used to create the watchpoint. */
1697 if (!watchpoint_in_thread_scope (b))
1698 return;
1699
1700 if (b->disposition == disp_del_at_next_stop)
1701 return;
1702
1703 frame_saved = 0;
1704
1705 /* Determine if the watchpoint is within scope. */
1706 if (b->exp_valid_block == NULL)
1707 within_current_scope = 1;
1708 else
1709 {
1710 struct frame_info *fi = get_current_frame ();
1711 struct gdbarch *frame_arch = get_frame_arch (fi);
1712 CORE_ADDR frame_pc = get_frame_pc (fi);
1713
1714 /* If we're at a point where the stack has been destroyed
1715 (e.g. in a function epilogue), unwinding may not work
1716 properly. Do not attempt to recreate locations at this
1717 point. See similar comments in watchpoint_check. */
1718 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1719 return;
1720
1721 /* Save the current frame's ID so we can restore it after
1722 evaluating the watchpoint expression on its own frame. */
1723 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1724 took a frame parameter, so that we didn't have to change the
1725 selected frame. */
1726 frame_saved = 1;
1727 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1728
1729 fi = frame_find_by_id (b->watchpoint_frame);
1730 within_current_scope = (fi != NULL);
1731 if (within_current_scope)
1732 select_frame (fi);
1733 }
1734
1735 /* We don't free locations. They are stored in the bp_location array
1736 and update_global_location_list will eventually delete them and
1737 remove breakpoints if needed. */
1738 b->loc = NULL;
1739
1740 if (within_current_scope && reparse)
1741 {
1742 const char *s;
1743
1744 b->exp.reset ();
1745 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1746 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1747 /* If the meaning of expression itself changed, the old value is
1748 no longer relevant. We don't want to report a watchpoint hit
1749 to the user when the old value and the new value may actually
1750 be completely different objects. */
1751 b->val = NULL;
1752 b->val_valid = false;
1753
1754 /* Note that unlike with breakpoints, the watchpoint's condition
1755 expression is stored in the breakpoint object, not in the
1756 locations (re)created below. */
1757 if (b->cond_string != NULL)
1758 {
1759 b->cond_exp.reset ();
1760
1761 s = b->cond_string;
1762 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1763 }
1764 }
1765
1766 /* If we failed to parse the expression, for example because
1767 it refers to a global variable in a not-yet-loaded shared library,
1768 don't try to insert watchpoint. We don't automatically delete
1769 such watchpoint, though, since failure to parse expression
1770 is different from out-of-scope watchpoint. */
1771 if (!target_has_execution ())
1772 {
1773 /* Without execution, memory can't change. No use to try and
1774 set watchpoint locations. The watchpoint will be reset when
1775 the target gains execution, through breakpoint_re_set. */
1776 if (!can_use_hw_watchpoints)
1777 {
1778 if (b->ops->works_in_software_mode (b))
1779 b->type = bp_watchpoint;
1780 else
1781 error (_("Can't set read/access watchpoint when "
1782 "hardware watchpoints are disabled."));
1783 }
1784 }
1785 else if (within_current_scope && b->exp)
1786 {
1787 int pc = 0;
1788 std::vector<value_ref_ptr> val_chain;
1789 struct value *v, *result;
1790 struct program_space *frame_pspace;
1791
1792 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
1793
1794 /* Avoid setting b->val if it's already set. The meaning of
1795 b->val is 'the last value' user saw, and we should update
1796 it only if we reported that last value to user. As it
1797 happens, the code that reports it updates b->val directly.
1798 We don't keep track of the memory value for masked
1799 watchpoints. */
1800 if (!b->val_valid && !is_masked_watchpoint (b))
1801 {
1802 if (b->val_bitsize != 0)
1803 v = extract_bitfield_from_watchpoint_value (b, v);
1804 b->val = release_value (v);
1805 b->val_valid = true;
1806 }
1807
1808 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1809
1810 /* Look at each value on the value chain. */
1811 gdb_assert (!val_chain.empty ());
1812 for (const value_ref_ptr &iter : val_chain)
1813 {
1814 v = iter.get ();
1815
1816 /* If it's a memory location, and GDB actually needed
1817 its contents to evaluate the expression, then we
1818 must watch it. If the first value returned is
1819 still lazy, that means an error occurred reading it;
1820 watch it anyway in case it becomes readable. */
1821 if (VALUE_LVAL (v) == lval_memory
1822 && (v == val_chain[0] || ! value_lazy (v)))
1823 {
1824 struct type *vtype = check_typedef (value_type (v));
1825
1826 /* We only watch structs and arrays if user asked
1827 for it explicitly, never if they just happen to
1828 appear in the middle of some value chain. */
1829 if (v == result
1830 || (vtype->code () != TYPE_CODE_STRUCT
1831 && vtype->code () != TYPE_CODE_ARRAY))
1832 {
1833 CORE_ADDR addr;
1834 enum target_hw_bp_type type;
1835 struct bp_location *loc, **tmp;
1836 int bitpos = 0, bitsize = 0;
1837
1838 if (value_bitsize (v) != 0)
1839 {
1840 /* Extract the bit parameters out from the bitfield
1841 sub-expression. */
1842 bitpos = value_bitpos (v);
1843 bitsize = value_bitsize (v);
1844 }
1845 else if (v == result && b->val_bitsize != 0)
1846 {
1847 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1848 lvalue whose bit parameters are saved in the fields
1849 VAL_BITPOS and VAL_BITSIZE. */
1850 bitpos = b->val_bitpos;
1851 bitsize = b->val_bitsize;
1852 }
1853
1854 addr = value_address (v);
1855 if (bitsize != 0)
1856 {
1857 /* Skip the bytes that don't contain the bitfield. */
1858 addr += bitpos / 8;
1859 }
1860
1861 type = hw_write;
1862 if (b->type == bp_read_watchpoint)
1863 type = hw_read;
1864 else if (b->type == bp_access_watchpoint)
1865 type = hw_access;
1866
1867 loc = allocate_bp_location (b);
1868 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1869 ;
1870 *tmp = loc;
1871 loc->gdbarch = get_type_arch (value_type (v));
1872
1873 loc->pspace = frame_pspace;
1874 loc->address = address_significant (loc->gdbarch, addr);
1875
1876 if (bitsize != 0)
1877 {
1878 /* Just cover the bytes that make up the bitfield. */
1879 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1880 }
1881 else
1882 loc->length = TYPE_LENGTH (value_type (v));
1883
1884 loc->watchpoint_type = type;
1885 }
1886 }
1887 }
1888
1889 /* Change the type of breakpoint between hardware assisted or
1890 an ordinary watchpoint depending on the hardware support
1891 and free hardware slots. REPARSE is set when the inferior
1892 is started. */
1893 if (reparse)
1894 {
1895 int reg_cnt;
1896 enum bp_loc_type loc_type;
1897 struct bp_location *bl;
1898
1899 reg_cnt = can_use_hardware_watchpoint (val_chain);
1900
1901 if (reg_cnt)
1902 {
1903 int i, target_resources_ok, other_type_used;
1904 enum bptype type;
1905
1906 /* Use an exact watchpoint when there's only one memory region to be
1907 watched, and only one debug register is needed to watch it. */
1908 b->exact = target_exact_watchpoints && reg_cnt == 1;
1909
1910 /* We need to determine how many resources are already
1911 used for all other hardware watchpoints plus this one
1912 to see if we still have enough resources to also fit
1913 this watchpoint in as well. */
1914
1915 /* If this is a software watchpoint, we try to turn it
1916 to a hardware one -- count resources as if B was of
1917 hardware watchpoint type. */
1918 type = b->type;
1919 if (type == bp_watchpoint)
1920 type = bp_hardware_watchpoint;
1921
1922 /* This watchpoint may or may not have been placed on
1923 the list yet at this point (it won't be in the list
1924 if we're trying to create it for the first time,
1925 through watch_command), so always account for it
1926 manually. */
1927
1928 /* Count resources used by all watchpoints except B. */
1929 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
1930
1931 /* Add in the resources needed for B. */
1932 i += hw_watchpoint_use_count (b);
1933
1934 target_resources_ok
1935 = target_can_use_hardware_watchpoint (type, i, other_type_used);
1936 if (target_resources_ok <= 0)
1937 {
1938 int sw_mode = b->ops->works_in_software_mode (b);
1939
1940 if (target_resources_ok == 0 && !sw_mode)
1941 error (_("Target does not support this type of "
1942 "hardware watchpoint."));
1943 else if (target_resources_ok < 0 && !sw_mode)
1944 error (_("There are not enough available hardware "
1945 "resources for this watchpoint."));
1946
1947 /* Downgrade to software watchpoint. */
1948 b->type = bp_watchpoint;
1949 }
1950 else
1951 {
1952 /* If this was a software watchpoint, we've just
1953 found we have enough resources to turn it to a
1954 hardware watchpoint. Otherwise, this is a
1955 nop. */
1956 b->type = type;
1957 }
1958 }
1959 else if (!b->ops->works_in_software_mode (b))
1960 {
1961 if (!can_use_hw_watchpoints)
1962 error (_("Can't set read/access watchpoint when "
1963 "hardware watchpoints are disabled."));
1964 else
1965 error (_("Expression cannot be implemented with "
1966 "read/access watchpoint."));
1967 }
1968 else
1969 b->type = bp_watchpoint;
1970
1971 loc_type = (b->type == bp_watchpoint? bp_loc_other
1972 : bp_loc_hardware_watchpoint);
1973 for (bl = b->loc; bl; bl = bl->next)
1974 bl->loc_type = loc_type;
1975 }
1976
1977 /* If a software watchpoint is not watching any memory, then the
1978 above left it without any location set up. But,
1979 bpstat_stop_status requires a location to be able to report
1980 stops, so make sure there's at least a dummy one. */
1981 if (b->type == bp_watchpoint && b->loc == NULL)
1982 software_watchpoint_add_no_memory_location (b, frame_pspace);
1983 }
1984 else if (!within_current_scope)
1985 {
1986 printf_filtered (_("\
1987 Watchpoint %d deleted because the program has left the block\n\
1988 in which its expression is valid.\n"),
1989 b->number);
1990 watchpoint_del_at_next_stop (b);
1991 }
1992
1993 /* Restore the selected frame. */
1994 if (frame_saved)
1995 select_frame (frame_find_by_id (saved_frame_id));
1996 }
1997
1998
1999 /* Returns 1 iff breakpoint location should be
2000 inserted in the inferior. We don't differentiate the type of BL's owner
2001 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2002 breakpoint_ops is not defined, because in insert_bp_location,
2003 tracepoint's insert_location will not be called. */
2004 static int
2005 should_be_inserted (struct bp_location *bl)
2006 {
2007 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2008 return 0;
2009
2010 if (bl->owner->disposition == disp_del_at_next_stop)
2011 return 0;
2012
2013 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2014 return 0;
2015
2016 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2017 return 0;
2018
2019 /* This is set for example, when we're attached to the parent of a
2020 vfork, and have detached from the child. The child is running
2021 free, and we expect it to do an exec or exit, at which point the
2022 OS makes the parent schedulable again (and the target reports
2023 that the vfork is done). Until the child is done with the shared
2024 memory region, do not insert breakpoints in the parent, otherwise
2025 the child could still trip on the parent's breakpoints. Since
2026 the parent is blocked anyway, it won't miss any breakpoint. */
2027 if (bl->pspace->breakpoints_not_allowed)
2028 return 0;
2029
2030 /* Don't insert a breakpoint if we're trying to step past its
2031 location, except if the breakpoint is a single-step breakpoint,
2032 and the breakpoint's thread is the thread which is stepping past
2033 a breakpoint. */
2034 if ((bl->loc_type == bp_loc_software_breakpoint
2035 || bl->loc_type == bp_loc_hardware_breakpoint)
2036 && stepping_past_instruction_at (bl->pspace->aspace,
2037 bl->address)
2038 /* The single-step breakpoint may be inserted at the location
2039 we're trying to step if the instruction branches to itself.
2040 However, the instruction won't be executed at all and it may
2041 break the semantics of the instruction, for example, the
2042 instruction is a conditional branch or updates some flags.
2043 We can't fix it unless GDB is able to emulate the instruction
2044 or switch to displaced stepping. */
2045 && !(bl->owner->type == bp_single_step
2046 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2047 {
2048 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2049 paddress (bl->gdbarch, bl->address));
2050 return 0;
2051 }
2052
2053 /* Don't insert watchpoints if we're trying to step past the
2054 instruction that triggered one. */
2055 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2056 && stepping_past_nonsteppable_watchpoint ())
2057 {
2058 infrun_debug_printf ("stepping past non-steppable watchpoint. "
2059 "skipping watchpoint at %s:%d\n",
2060 paddress (bl->gdbarch, bl->address), bl->length);
2061 return 0;
2062 }
2063
2064 return 1;
2065 }
2066
2067 /* Same as should_be_inserted but does the check assuming
2068 that the location is not duplicated. */
2069
2070 static int
2071 unduplicated_should_be_inserted (struct bp_location *bl)
2072 {
2073 int result;
2074 const int save_duplicate = bl->duplicate;
2075
2076 bl->duplicate = 0;
2077 result = should_be_inserted (bl);
2078 bl->duplicate = save_duplicate;
2079 return result;
2080 }
2081
2082 /* Parses a conditional described by an expression COND into an
2083 agent expression bytecode suitable for evaluation
2084 by the bytecode interpreter. Return NULL if there was
2085 any error during parsing. */
2086
2087 static agent_expr_up
2088 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2089 {
2090 if (cond == NULL)
2091 return NULL;
2092
2093 agent_expr_up aexpr;
2094
2095 /* We don't want to stop processing, so catch any errors
2096 that may show up. */
2097 try
2098 {
2099 aexpr = gen_eval_for_expr (scope, cond);
2100 }
2101
2102 catch (const gdb_exception_error &ex)
2103 {
2104 /* If we got here, it means the condition could not be parsed to a valid
2105 bytecode expression and thus can't be evaluated on the target's side.
2106 It's no use iterating through the conditions. */
2107 }
2108
2109 /* We have a valid agent expression. */
2110 return aexpr;
2111 }
2112
2113 /* Based on location BL, create a list of breakpoint conditions to be
2114 passed on to the target. If we have duplicated locations with different
2115 conditions, we will add such conditions to the list. The idea is that the
2116 target will evaluate the list of conditions and will only notify GDB when
2117 one of them is true. */
2118
2119 static void
2120 build_target_condition_list (struct bp_location *bl)
2121 {
2122 struct bp_location **locp = NULL, **loc2p;
2123 int null_condition_or_parse_error = 0;
2124 int modified = bl->needs_update;
2125 struct bp_location *loc;
2126
2127 /* Release conditions left over from a previous insert. */
2128 bl->target_info.conditions.clear ();
2129
2130 /* This is only meaningful if the target is
2131 evaluating conditions and if the user has
2132 opted for condition evaluation on the target's
2133 side. */
2134 if (gdb_evaluates_breakpoint_condition_p ()
2135 || !target_supports_evaluation_of_breakpoint_conditions ())
2136 return;
2137
2138 /* Do a first pass to check for locations with no assigned
2139 conditions or conditions that fail to parse to a valid agent
2140 expression bytecode. If any of these happen, then it's no use to
2141 send conditions to the target since this location will always
2142 trigger and generate a response back to GDB. Note we consider
2143 all locations at the same address irrespective of type, i.e.,
2144 even if the locations aren't considered duplicates (e.g.,
2145 software breakpoint and hardware breakpoint at the same
2146 address). */
2147 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2148 {
2149 loc = (*loc2p);
2150 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2151 {
2152 if (modified)
2153 {
2154 /* Re-parse the conditions since something changed. In that
2155 case we already freed the condition bytecodes (see
2156 force_breakpoint_reinsertion). We just
2157 need to parse the condition to bytecodes again. */
2158 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2159 loc->cond.get ());
2160 }
2161
2162 /* If we have a NULL bytecode expression, it means something
2163 went wrong or we have a null condition expression. */
2164 if (!loc->cond_bytecode)
2165 {
2166 null_condition_or_parse_error = 1;
2167 break;
2168 }
2169 }
2170 }
2171
2172 /* If any of these happened, it means we will have to evaluate the conditions
2173 for the location's address on gdb's side. It is no use keeping bytecodes
2174 for all the other duplicate locations, thus we free all of them here.
2175
2176 This is so we have a finer control over which locations' conditions are
2177 being evaluated by GDB or the remote stub. */
2178 if (null_condition_or_parse_error)
2179 {
2180 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2181 {
2182 loc = (*loc2p);
2183 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2184 {
2185 /* Only go as far as the first NULL bytecode is
2186 located. */
2187 if (!loc->cond_bytecode)
2188 return;
2189
2190 loc->cond_bytecode.reset ();
2191 }
2192 }
2193 }
2194
2195 /* No NULL conditions or failed bytecode generation. Build a
2196 condition list for this location's address. If we have software
2197 and hardware locations at the same address, they aren't
2198 considered duplicates, but we still marge all the conditions
2199 anyway, as it's simpler, and doesn't really make a practical
2200 difference. */
2201 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2202 {
2203 loc = (*loc2p);
2204 if (loc->cond
2205 && is_breakpoint (loc->owner)
2206 && loc->pspace->num == bl->pspace->num
2207 && loc->owner->enable_state == bp_enabled
2208 && loc->enabled)
2209 {
2210 /* Add the condition to the vector. This will be used later
2211 to send the conditions to the target. */
2212 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2213 }
2214 }
2215
2216 return;
2217 }
2218
2219 /* Parses a command described by string CMD into an agent expression
2220 bytecode suitable for evaluation by the bytecode interpreter.
2221 Return NULL if there was any error during parsing. */
2222
2223 static agent_expr_up
2224 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2225 {
2226 const char *cmdrest;
2227 const char *format_start, *format_end;
2228 struct gdbarch *gdbarch = get_current_arch ();
2229
2230 if (cmd == NULL)
2231 return NULL;
2232
2233 cmdrest = cmd;
2234
2235 if (*cmdrest == ',')
2236 ++cmdrest;
2237 cmdrest = skip_spaces (cmdrest);
2238
2239 if (*cmdrest++ != '"')
2240 error (_("No format string following the location"));
2241
2242 format_start = cmdrest;
2243
2244 format_pieces fpieces (&cmdrest);
2245
2246 format_end = cmdrest;
2247
2248 if (*cmdrest++ != '"')
2249 error (_("Bad format string, non-terminated '\"'."));
2250
2251 cmdrest = skip_spaces (cmdrest);
2252
2253 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2254 error (_("Invalid argument syntax"));
2255
2256 if (*cmdrest == ',')
2257 cmdrest++;
2258 cmdrest = skip_spaces (cmdrest);
2259
2260 /* For each argument, make an expression. */
2261
2262 std::vector<struct expression *> argvec;
2263 while (*cmdrest != '\0')
2264 {
2265 const char *cmd1;
2266
2267 cmd1 = cmdrest;
2268 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2269 argvec.push_back (expr.release ());
2270 cmdrest = cmd1;
2271 if (*cmdrest == ',')
2272 ++cmdrest;
2273 }
2274
2275 agent_expr_up aexpr;
2276
2277 /* We don't want to stop processing, so catch any errors
2278 that may show up. */
2279 try
2280 {
2281 aexpr = gen_printf (scope, gdbarch, 0, 0,
2282 format_start, format_end - format_start,
2283 argvec.size (), argvec.data ());
2284 }
2285 catch (const gdb_exception_error &ex)
2286 {
2287 /* If we got here, it means the command could not be parsed to a valid
2288 bytecode expression and thus can't be evaluated on the target's side.
2289 It's no use iterating through the other commands. */
2290 }
2291
2292 /* We have a valid agent expression, return it. */
2293 return aexpr;
2294 }
2295
2296 /* Based on location BL, create a list of breakpoint commands to be
2297 passed on to the target. If we have duplicated locations with
2298 different commands, we will add any such to the list. */
2299
2300 static void
2301 build_target_command_list (struct bp_location *bl)
2302 {
2303 struct bp_location **locp = NULL, **loc2p;
2304 int null_command_or_parse_error = 0;
2305 int modified = bl->needs_update;
2306 struct bp_location *loc;
2307
2308 /* Clear commands left over from a previous insert. */
2309 bl->target_info.tcommands.clear ();
2310
2311 if (!target_can_run_breakpoint_commands ())
2312 return;
2313
2314 /* For now, limit to agent-style dprintf breakpoints. */
2315 if (dprintf_style != dprintf_style_agent)
2316 return;
2317
2318 /* For now, if we have any location at the same address that isn't a
2319 dprintf, don't install the target-side commands, as that would
2320 make the breakpoint not be reported to the core, and we'd lose
2321 control. */
2322 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2323 {
2324 loc = (*loc2p);
2325 if (is_breakpoint (loc->owner)
2326 && loc->pspace->num == bl->pspace->num
2327 && loc->owner->type != bp_dprintf)
2328 return;
2329 }
2330
2331 /* Do a first pass to check for locations with no assigned
2332 conditions or conditions that fail to parse to a valid agent expression
2333 bytecode. If any of these happen, then it's no use to send conditions
2334 to the target since this location will always trigger and generate a
2335 response back to GDB. */
2336 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2337 {
2338 loc = (*loc2p);
2339 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2340 {
2341 if (modified)
2342 {
2343 /* Re-parse the commands since something changed. In that
2344 case we already freed the command bytecodes (see
2345 force_breakpoint_reinsertion). We just
2346 need to parse the command to bytecodes again. */
2347 loc->cmd_bytecode
2348 = parse_cmd_to_aexpr (bl->address,
2349 loc->owner->extra_string);
2350 }
2351
2352 /* If we have a NULL bytecode expression, it means something
2353 went wrong or we have a null command expression. */
2354 if (!loc->cmd_bytecode)
2355 {
2356 null_command_or_parse_error = 1;
2357 break;
2358 }
2359 }
2360 }
2361
2362 /* If anything failed, then we're not doing target-side commands,
2363 and so clean up. */
2364 if (null_command_or_parse_error)
2365 {
2366 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2367 {
2368 loc = (*loc2p);
2369 if (is_breakpoint (loc->owner)
2370 && loc->pspace->num == bl->pspace->num)
2371 {
2372 /* Only go as far as the first NULL bytecode is
2373 located. */
2374 if (loc->cmd_bytecode == NULL)
2375 return;
2376
2377 loc->cmd_bytecode.reset ();
2378 }
2379 }
2380 }
2381
2382 /* No NULL commands or failed bytecode generation. Build a command
2383 list for all duplicate locations at this location's address.
2384 Note that here we must care for whether the breakpoint location
2385 types are considered duplicates, otherwise, say, if we have a
2386 software and hardware location at the same address, the target
2387 could end up running the commands twice. For the moment, we only
2388 support targets-side commands with dprintf, but it doesn't hurt
2389 to be pedantically correct in case that changes. */
2390 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2391 {
2392 loc = (*loc2p);
2393 if (breakpoint_locations_match (bl, loc)
2394 && loc->owner->extra_string
2395 && is_breakpoint (loc->owner)
2396 && loc->pspace->num == bl->pspace->num
2397 && loc->owner->enable_state == bp_enabled
2398 && loc->enabled)
2399 {
2400 /* Add the command to the vector. This will be used later
2401 to send the commands to the target. */
2402 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2403 }
2404 }
2405
2406 bl->target_info.persist = 0;
2407 /* Maybe flag this location as persistent. */
2408 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2409 bl->target_info.persist = 1;
2410 }
2411
2412 /* Return the kind of breakpoint on address *ADDR. Get the kind
2413 of breakpoint according to ADDR except single-step breakpoint.
2414 Get the kind of single-step breakpoint according to the current
2415 registers state. */
2416
2417 static int
2418 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2419 {
2420 if (bl->owner->type == bp_single_step)
2421 {
2422 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2423 struct regcache *regcache;
2424
2425 regcache = get_thread_regcache (thr);
2426
2427 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2428 regcache, addr);
2429 }
2430 else
2431 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2432 }
2433
2434 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2435 location. Any error messages are printed to TMP_ERROR_STREAM; and
2436 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2437 Returns 0 for success, 1 if the bp_location type is not supported or
2438 -1 for failure.
2439
2440 NOTE drow/2003-09-09: This routine could be broken down to an
2441 object-style method for each breakpoint or catchpoint type. */
2442 static int
2443 insert_bp_location (struct bp_location *bl,
2444 struct ui_file *tmp_error_stream,
2445 int *disabled_breaks,
2446 int *hw_breakpoint_error,
2447 int *hw_bp_error_explained_already)
2448 {
2449 gdb_exception bp_excpt;
2450
2451 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2452 return 0;
2453
2454 /* Note we don't initialize bl->target_info, as that wipes out
2455 the breakpoint location's shadow_contents if the breakpoint
2456 is still inserted at that location. This in turn breaks
2457 target_read_memory which depends on these buffers when
2458 a memory read is requested at the breakpoint location:
2459 Once the target_info has been wiped, we fail to see that
2460 we have a breakpoint inserted at that address and thus
2461 read the breakpoint instead of returning the data saved in
2462 the breakpoint location's shadow contents. */
2463 bl->target_info.reqstd_address = bl->address;
2464 bl->target_info.placed_address_space = bl->pspace->aspace;
2465 bl->target_info.length = bl->length;
2466
2467 /* When working with target-side conditions, we must pass all the conditions
2468 for the same breakpoint address down to the target since GDB will not
2469 insert those locations. With a list of breakpoint conditions, the target
2470 can decide when to stop and notify GDB. */
2471
2472 if (is_breakpoint (bl->owner))
2473 {
2474 build_target_condition_list (bl);
2475 build_target_command_list (bl);
2476 /* Reset the modification marker. */
2477 bl->needs_update = 0;
2478 }
2479
2480 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2481 set at a read-only address, then a breakpoint location will have
2482 been changed to hardware breakpoint before we get here. If it is
2483 "off" however, error out before actually trying to insert the
2484 breakpoint, with a nicer error message. */
2485 if (bl->loc_type == bp_loc_software_breakpoint
2486 && !automatic_hardware_breakpoints)
2487 {
2488 mem_region *mr = lookup_mem_region (bl->address);
2489
2490 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2491 {
2492 fprintf_unfiltered (tmp_error_stream,
2493 _("Cannot insert breakpoint %d.\n"
2494 "Cannot set software breakpoint "
2495 "at read-only address %s\n"),
2496 bl->owner->number,
2497 paddress (bl->gdbarch, bl->address));
2498 return 1;
2499 }
2500 }
2501
2502 if (bl->loc_type == bp_loc_software_breakpoint
2503 || bl->loc_type == bp_loc_hardware_breakpoint)
2504 {
2505 /* First check to see if we have to handle an overlay. */
2506 if (overlay_debugging == ovly_off
2507 || bl->section == NULL
2508 || !(section_is_overlay (bl->section)))
2509 {
2510 /* No overlay handling: just set the breakpoint. */
2511 try
2512 {
2513 int val;
2514
2515 val = bl->owner->ops->insert_location (bl);
2516 if (val)
2517 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2518 }
2519 catch (gdb_exception &e)
2520 {
2521 bp_excpt = std::move (e);
2522 }
2523 }
2524 else
2525 {
2526 /* This breakpoint is in an overlay section.
2527 Shall we set a breakpoint at the LMA? */
2528 if (!overlay_events_enabled)
2529 {
2530 /* Yes -- overlay event support is not active,
2531 so we must try to set a breakpoint at the LMA.
2532 This will not work for a hardware breakpoint. */
2533 if (bl->loc_type == bp_loc_hardware_breakpoint)
2534 warning (_("hardware breakpoint %d not supported in overlay!"),
2535 bl->owner->number);
2536 else
2537 {
2538 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2539 bl->section);
2540 /* Set a software (trap) breakpoint at the LMA. */
2541 bl->overlay_target_info = bl->target_info;
2542 bl->overlay_target_info.reqstd_address = addr;
2543
2544 /* No overlay handling: just set the breakpoint. */
2545 try
2546 {
2547 int val;
2548
2549 bl->overlay_target_info.kind
2550 = breakpoint_kind (bl, &addr);
2551 bl->overlay_target_info.placed_address = addr;
2552 val = target_insert_breakpoint (bl->gdbarch,
2553 &bl->overlay_target_info);
2554 if (val)
2555 bp_excpt
2556 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2557 }
2558 catch (gdb_exception &e)
2559 {
2560 bp_excpt = std::move (e);
2561 }
2562
2563 if (bp_excpt.reason != 0)
2564 fprintf_unfiltered (tmp_error_stream,
2565 "Overlay breakpoint %d "
2566 "failed: in ROM?\n",
2567 bl->owner->number);
2568 }
2569 }
2570 /* Shall we set a breakpoint at the VMA? */
2571 if (section_is_mapped (bl->section))
2572 {
2573 /* Yes. This overlay section is mapped into memory. */
2574 try
2575 {
2576 int val;
2577
2578 val = bl->owner->ops->insert_location (bl);
2579 if (val)
2580 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2581 }
2582 catch (gdb_exception &e)
2583 {
2584 bp_excpt = std::move (e);
2585 }
2586 }
2587 else
2588 {
2589 /* No. This breakpoint will not be inserted.
2590 No error, but do not mark the bp as 'inserted'. */
2591 return 0;
2592 }
2593 }
2594
2595 if (bp_excpt.reason != 0)
2596 {
2597 /* Can't set the breakpoint. */
2598
2599 /* In some cases, we might not be able to insert a
2600 breakpoint in a shared library that has already been
2601 removed, but we have not yet processed the shlib unload
2602 event. Unfortunately, some targets that implement
2603 breakpoint insertion themselves can't tell why the
2604 breakpoint insertion failed (e.g., the remote target
2605 doesn't define error codes), so we must treat generic
2606 errors as memory errors. */
2607 if (bp_excpt.reason == RETURN_ERROR
2608 && (bp_excpt.error == GENERIC_ERROR
2609 || bp_excpt.error == MEMORY_ERROR)
2610 && bl->loc_type == bp_loc_software_breakpoint
2611 && (solib_name_from_address (bl->pspace, bl->address)
2612 || shared_objfile_contains_address_p (bl->pspace,
2613 bl->address)))
2614 {
2615 /* See also: disable_breakpoints_in_shlibs. */
2616 bl->shlib_disabled = 1;
2617 gdb::observers::breakpoint_modified.notify (bl->owner);
2618 if (!*disabled_breaks)
2619 {
2620 fprintf_unfiltered (tmp_error_stream,
2621 "Cannot insert breakpoint %d.\n",
2622 bl->owner->number);
2623 fprintf_unfiltered (tmp_error_stream,
2624 "Temporarily disabling shared "
2625 "library breakpoints:\n");
2626 }
2627 *disabled_breaks = 1;
2628 fprintf_unfiltered (tmp_error_stream,
2629 "breakpoint #%d\n", bl->owner->number);
2630 return 0;
2631 }
2632 else
2633 {
2634 if (bl->loc_type == bp_loc_hardware_breakpoint)
2635 {
2636 *hw_breakpoint_error = 1;
2637 *hw_bp_error_explained_already = bp_excpt.message != NULL;
2638 fprintf_unfiltered (tmp_error_stream,
2639 "Cannot insert hardware breakpoint %d%s",
2640 bl->owner->number,
2641 bp_excpt.message ? ":" : ".\n");
2642 if (bp_excpt.message != NULL)
2643 fprintf_unfiltered (tmp_error_stream, "%s.\n",
2644 bp_excpt.what ());
2645 }
2646 else
2647 {
2648 if (bp_excpt.message == NULL)
2649 {
2650 std::string message
2651 = memory_error_message (TARGET_XFER_E_IO,
2652 bl->gdbarch, bl->address);
2653
2654 fprintf_unfiltered (tmp_error_stream,
2655 "Cannot insert breakpoint %d.\n"
2656 "%s\n",
2657 bl->owner->number, message.c_str ());
2658 }
2659 else
2660 {
2661 fprintf_unfiltered (tmp_error_stream,
2662 "Cannot insert breakpoint %d: %s\n",
2663 bl->owner->number,
2664 bp_excpt.what ());
2665 }
2666 }
2667 return 1;
2668
2669 }
2670 }
2671 else
2672 bl->inserted = 1;
2673
2674 return 0;
2675 }
2676
2677 else if (bl->loc_type == bp_loc_hardware_watchpoint
2678 /* NOTE drow/2003-09-08: This state only exists for removing
2679 watchpoints. It's not clear that it's necessary... */
2680 && bl->owner->disposition != disp_del_at_next_stop)
2681 {
2682 int val;
2683
2684 gdb_assert (bl->owner->ops != NULL
2685 && bl->owner->ops->insert_location != NULL);
2686
2687 val = bl->owner->ops->insert_location (bl);
2688
2689 /* If trying to set a read-watchpoint, and it turns out it's not
2690 supported, try emulating one with an access watchpoint. */
2691 if (val == 1 && bl->watchpoint_type == hw_read)
2692 {
2693 struct bp_location *loc, **loc_temp;
2694
2695 /* But don't try to insert it, if there's already another
2696 hw_access location that would be considered a duplicate
2697 of this one. */
2698 ALL_BP_LOCATIONS (loc, loc_temp)
2699 if (loc != bl
2700 && loc->watchpoint_type == hw_access
2701 && watchpoint_locations_match (bl, loc))
2702 {
2703 bl->duplicate = 1;
2704 bl->inserted = 1;
2705 bl->target_info = loc->target_info;
2706 bl->watchpoint_type = hw_access;
2707 val = 0;
2708 break;
2709 }
2710
2711 if (val == 1)
2712 {
2713 bl->watchpoint_type = hw_access;
2714 val = bl->owner->ops->insert_location (bl);
2715
2716 if (val)
2717 /* Back to the original value. */
2718 bl->watchpoint_type = hw_read;
2719 }
2720 }
2721
2722 bl->inserted = (val == 0);
2723 }
2724
2725 else if (bl->owner->type == bp_catchpoint)
2726 {
2727 int val;
2728
2729 gdb_assert (bl->owner->ops != NULL
2730 && bl->owner->ops->insert_location != NULL);
2731
2732 val = bl->owner->ops->insert_location (bl);
2733 if (val)
2734 {
2735 bl->owner->enable_state = bp_disabled;
2736
2737 if (val == 1)
2738 warning (_("\
2739 Error inserting catchpoint %d: Your system does not support this type\n\
2740 of catchpoint."), bl->owner->number);
2741 else
2742 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2743 }
2744
2745 bl->inserted = (val == 0);
2746
2747 /* We've already printed an error message if there was a problem
2748 inserting this catchpoint, and we've disabled the catchpoint,
2749 so just return success. */
2750 return 0;
2751 }
2752
2753 return 0;
2754 }
2755
2756 /* This function is called when program space PSPACE is about to be
2757 deleted. It takes care of updating breakpoints to not reference
2758 PSPACE anymore. */
2759
2760 void
2761 breakpoint_program_space_exit (struct program_space *pspace)
2762 {
2763 struct breakpoint *b, *b_temp;
2764 struct bp_location *loc, **loc_temp;
2765
2766 /* Remove any breakpoint that was set through this program space. */
2767 ALL_BREAKPOINTS_SAFE (b, b_temp)
2768 {
2769 if (b->pspace == pspace)
2770 delete_breakpoint (b);
2771 }
2772
2773 /* Breakpoints set through other program spaces could have locations
2774 bound to PSPACE as well. Remove those. */
2775 ALL_BP_LOCATIONS (loc, loc_temp)
2776 {
2777 struct bp_location *tmp;
2778
2779 if (loc->pspace == pspace)
2780 {
2781 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2782 if (loc->owner->loc == loc)
2783 loc->owner->loc = loc->next;
2784 else
2785 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2786 if (tmp->next == loc)
2787 {
2788 tmp->next = loc->next;
2789 break;
2790 }
2791 }
2792 }
2793
2794 /* Now update the global location list to permanently delete the
2795 removed locations above. */
2796 update_global_location_list (UGLL_DONT_INSERT);
2797 }
2798
2799 /* Make sure all breakpoints are inserted in inferior.
2800 Throws exception on any error.
2801 A breakpoint that is already inserted won't be inserted
2802 again, so calling this function twice is safe. */
2803 void
2804 insert_breakpoints (void)
2805 {
2806 struct breakpoint *bpt;
2807
2808 ALL_BREAKPOINTS (bpt)
2809 if (is_hardware_watchpoint (bpt))
2810 {
2811 struct watchpoint *w = (struct watchpoint *) bpt;
2812
2813 update_watchpoint (w, 0 /* don't reparse. */);
2814 }
2815
2816 /* Updating watchpoints creates new locations, so update the global
2817 location list. Explicitly tell ugll to insert locations and
2818 ignore breakpoints_always_inserted_mode. Also,
2819 update_global_location_list tries to "upgrade" software
2820 breakpoints to hardware breakpoints to handle "set breakpoint
2821 auto-hw", so we need to call it even if we don't have new
2822 locations. */
2823 update_global_location_list (UGLL_INSERT);
2824 }
2825
2826 /* Invoke CALLBACK for each of bp_location. */
2827
2828 void
2829 iterate_over_bp_locations (walk_bp_location_callback callback)
2830 {
2831 struct bp_location *loc, **loc_tmp;
2832
2833 ALL_BP_LOCATIONS (loc, loc_tmp)
2834 {
2835 callback (loc, NULL);
2836 }
2837 }
2838
2839 /* This is used when we need to synch breakpoint conditions between GDB and the
2840 target. It is the case with deleting and disabling of breakpoints when using
2841 always-inserted mode. */
2842
2843 static void
2844 update_inserted_breakpoint_locations (void)
2845 {
2846 struct bp_location *bl, **blp_tmp;
2847 int error_flag = 0;
2848 int val = 0;
2849 int disabled_breaks = 0;
2850 int hw_breakpoint_error = 0;
2851 int hw_bp_details_reported = 0;
2852
2853 string_file tmp_error_stream;
2854
2855 /* Explicitly mark the warning -- this will only be printed if
2856 there was an error. */
2857 tmp_error_stream.puts ("Warning:\n");
2858
2859 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2860
2861 ALL_BP_LOCATIONS (bl, blp_tmp)
2862 {
2863 /* We only want to update software breakpoints and hardware
2864 breakpoints. */
2865 if (!is_breakpoint (bl->owner))
2866 continue;
2867
2868 /* We only want to update locations that are already inserted
2869 and need updating. This is to avoid unwanted insertion during
2870 deletion of breakpoints. */
2871 if (!bl->inserted || !bl->needs_update)
2872 continue;
2873
2874 switch_to_program_space_and_thread (bl->pspace);
2875
2876 /* For targets that support global breakpoints, there's no need
2877 to select an inferior to insert breakpoint to. In fact, even
2878 if we aren't attached to any process yet, we should still
2879 insert breakpoints. */
2880 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2881 && (inferior_ptid == null_ptid || !target_has_execution ()))
2882 continue;
2883
2884 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
2885 &hw_breakpoint_error, &hw_bp_details_reported);
2886 if (val)
2887 error_flag = val;
2888 }
2889
2890 if (error_flag)
2891 {
2892 target_terminal::ours_for_output ();
2893 error_stream (tmp_error_stream);
2894 }
2895 }
2896
2897 /* Used when starting or continuing the program. */
2898
2899 static void
2900 insert_breakpoint_locations (void)
2901 {
2902 struct breakpoint *bpt;
2903 struct bp_location *bl, **blp_tmp;
2904 int error_flag = 0;
2905 int val = 0;
2906 int disabled_breaks = 0;
2907 int hw_breakpoint_error = 0;
2908 int hw_bp_error_explained_already = 0;
2909
2910 string_file tmp_error_stream;
2911
2912 /* Explicitly mark the warning -- this will only be printed if
2913 there was an error. */
2914 tmp_error_stream.puts ("Warning:\n");
2915
2916 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2917
2918 ALL_BP_LOCATIONS (bl, blp_tmp)
2919 {
2920 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2921 continue;
2922
2923 /* There is no point inserting thread-specific breakpoints if
2924 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2925 has BL->OWNER always non-NULL. */
2926 if (bl->owner->thread != -1
2927 && !valid_global_thread_id (bl->owner->thread))
2928 continue;
2929
2930 switch_to_program_space_and_thread (bl->pspace);
2931
2932 /* For targets that support global breakpoints, there's no need
2933 to select an inferior to insert breakpoint to. In fact, even
2934 if we aren't attached to any process yet, we should still
2935 insert breakpoints. */
2936 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2937 && (inferior_ptid == null_ptid || !target_has_execution ()))
2938 continue;
2939
2940 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
2941 &hw_breakpoint_error, &hw_bp_error_explained_already);
2942 if (val)
2943 error_flag = val;
2944 }
2945
2946 /* If we failed to insert all locations of a watchpoint, remove
2947 them, as half-inserted watchpoint is of limited use. */
2948 ALL_BREAKPOINTS (bpt)
2949 {
2950 int some_failed = 0;
2951 struct bp_location *loc;
2952
2953 if (!is_hardware_watchpoint (bpt))
2954 continue;
2955
2956 if (!breakpoint_enabled (bpt))
2957 continue;
2958
2959 if (bpt->disposition == disp_del_at_next_stop)
2960 continue;
2961
2962 for (loc = bpt->loc; loc; loc = loc->next)
2963 if (!loc->inserted && should_be_inserted (loc))
2964 {
2965 some_failed = 1;
2966 break;
2967 }
2968 if (some_failed)
2969 {
2970 for (loc = bpt->loc; loc; loc = loc->next)
2971 if (loc->inserted)
2972 remove_breakpoint (loc);
2973
2974 hw_breakpoint_error = 1;
2975 tmp_error_stream.printf ("Could not insert "
2976 "hardware watchpoint %d.\n",
2977 bpt->number);
2978 error_flag = -1;
2979 }
2980 }
2981
2982 if (error_flag)
2983 {
2984 /* If a hardware breakpoint or watchpoint was inserted, add a
2985 message about possibly exhausted resources. */
2986 if (hw_breakpoint_error && !hw_bp_error_explained_already)
2987 {
2988 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
2989 You may have requested too many hardware breakpoints/watchpoints.\n");
2990 }
2991 target_terminal::ours_for_output ();
2992 error_stream (tmp_error_stream);
2993 }
2994 }
2995
2996 /* Used when the program stops.
2997 Returns zero if successful, or non-zero if there was a problem
2998 removing a breakpoint location. */
2999
3000 int
3001 remove_breakpoints (void)
3002 {
3003 struct bp_location *bl, **blp_tmp;
3004 int val = 0;
3005
3006 ALL_BP_LOCATIONS (bl, blp_tmp)
3007 {
3008 if (bl->inserted && !is_tracepoint (bl->owner))
3009 val |= remove_breakpoint (bl);
3010 }
3011 return val;
3012 }
3013
3014 /* When a thread exits, remove breakpoints that are related to
3015 that thread. */
3016
3017 static void
3018 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3019 {
3020 struct breakpoint *b, *b_tmp;
3021
3022 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3023 {
3024 if (b->thread == tp->global_num && user_breakpoint_p (b))
3025 {
3026 b->disposition = disp_del_at_next_stop;
3027
3028 printf_filtered (_("\
3029 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3030 b->number, print_thread_id (tp));
3031
3032 /* Hide it from the user. */
3033 b->number = 0;
3034 }
3035 }
3036 }
3037
3038 /* See breakpoint.h. */
3039
3040 void
3041 remove_breakpoints_inf (inferior *inf)
3042 {
3043 struct bp_location *bl, **blp_tmp;
3044 int val;
3045
3046 ALL_BP_LOCATIONS (bl, blp_tmp)
3047 {
3048 if (bl->pspace != inf->pspace)
3049 continue;
3050
3051 if (bl->inserted && !bl->target_info.persist)
3052 {
3053 val = remove_breakpoint (bl);
3054 if (val != 0)
3055 return;
3056 }
3057 }
3058 }
3059
3060 static int internal_breakpoint_number = -1;
3061
3062 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3063 If INTERNAL is non-zero, the breakpoint number will be populated
3064 from internal_breakpoint_number and that variable decremented.
3065 Otherwise the breakpoint number will be populated from
3066 breakpoint_count and that value incremented. Internal breakpoints
3067 do not set the internal var bpnum. */
3068 static void
3069 set_breakpoint_number (int internal, struct breakpoint *b)
3070 {
3071 if (internal)
3072 b->number = internal_breakpoint_number--;
3073 else
3074 {
3075 set_breakpoint_count (breakpoint_count + 1);
3076 b->number = breakpoint_count;
3077 }
3078 }
3079
3080 static struct breakpoint *
3081 create_internal_breakpoint (struct gdbarch *gdbarch,
3082 CORE_ADDR address, enum bptype type,
3083 const struct breakpoint_ops *ops)
3084 {
3085 symtab_and_line sal;
3086 sal.pc = address;
3087 sal.section = find_pc_overlay (sal.pc);
3088 sal.pspace = current_program_space;
3089
3090 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
3091 b->number = internal_breakpoint_number--;
3092 b->disposition = disp_donttouch;
3093
3094 return b;
3095 }
3096
3097 static const char *const longjmp_names[] =
3098 {
3099 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3100 };
3101 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3102
3103 /* Per-objfile data private to breakpoint.c. */
3104 struct breakpoint_objfile_data
3105 {
3106 /* Minimal symbol for "_ovly_debug_event" (if any). */
3107 struct bound_minimal_symbol overlay_msym {};
3108
3109 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3110 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
3111
3112 /* True if we have looked for longjmp probes. */
3113 int longjmp_searched = 0;
3114
3115 /* SystemTap probe points for longjmp (if any). These are non-owning
3116 references. */
3117 std::vector<probe *> longjmp_probes;
3118
3119 /* Minimal symbol for "std::terminate()" (if any). */
3120 struct bound_minimal_symbol terminate_msym {};
3121
3122 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3123 struct bound_minimal_symbol exception_msym {};
3124
3125 /* True if we have looked for exception probes. */
3126 int exception_searched = 0;
3127
3128 /* SystemTap probe points for unwinding (if any). These are non-owning
3129 references. */
3130 std::vector<probe *> exception_probes;
3131 };
3132
3133 static const struct objfile_key<breakpoint_objfile_data>
3134 breakpoint_objfile_key;
3135
3136 /* Minimal symbol not found sentinel. */
3137 static struct minimal_symbol msym_not_found;
3138
3139 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3140
3141 static int
3142 msym_not_found_p (const struct minimal_symbol *msym)
3143 {
3144 return msym == &msym_not_found;
3145 }
3146
3147 /* Return per-objfile data needed by breakpoint.c.
3148 Allocate the data if necessary. */
3149
3150 static struct breakpoint_objfile_data *
3151 get_breakpoint_objfile_data (struct objfile *objfile)
3152 {
3153 struct breakpoint_objfile_data *bp_objfile_data;
3154
3155 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3156 if (bp_objfile_data == NULL)
3157 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3158 return bp_objfile_data;
3159 }
3160
3161 static void
3162 create_overlay_event_breakpoint (void)
3163 {
3164 const char *const func_name = "_ovly_debug_event";
3165
3166 for (objfile *objfile : current_program_space->objfiles ())
3167 {
3168 struct breakpoint *b;
3169 struct breakpoint_objfile_data *bp_objfile_data;
3170 CORE_ADDR addr;
3171 struct explicit_location explicit_loc;
3172
3173 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3174
3175 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3176 continue;
3177
3178 if (bp_objfile_data->overlay_msym.minsym == NULL)
3179 {
3180 struct bound_minimal_symbol m;
3181
3182 m = lookup_minimal_symbol_text (func_name, objfile);
3183 if (m.minsym == NULL)
3184 {
3185 /* Avoid future lookups in this objfile. */
3186 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3187 continue;
3188 }
3189 bp_objfile_data->overlay_msym = m;
3190 }
3191
3192 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3193 b = create_internal_breakpoint (objfile->arch (), addr,
3194 bp_overlay_event,
3195 &internal_breakpoint_ops);
3196 initialize_explicit_location (&explicit_loc);
3197 explicit_loc.function_name = ASTRDUP (func_name);
3198 b->location = new_explicit_location (&explicit_loc);
3199
3200 if (overlay_debugging == ovly_auto)
3201 {
3202 b->enable_state = bp_enabled;
3203 overlay_events_enabled = 1;
3204 }
3205 else
3206 {
3207 b->enable_state = bp_disabled;
3208 overlay_events_enabled = 0;
3209 }
3210 }
3211 }
3212
3213 static void
3214 create_longjmp_master_breakpoint (void)
3215 {
3216 scoped_restore_current_program_space restore_pspace;
3217
3218 for (struct program_space *pspace : program_spaces)
3219 {
3220 set_current_program_space (pspace);
3221
3222 for (objfile *objfile : current_program_space->objfiles ())
3223 {
3224 int i;
3225 struct gdbarch *gdbarch;
3226 struct breakpoint_objfile_data *bp_objfile_data;
3227
3228 gdbarch = objfile->arch ();
3229
3230 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3231
3232 if (!bp_objfile_data->longjmp_searched)
3233 {
3234 std::vector<probe *> ret
3235 = find_probes_in_objfile (objfile, "libc", "longjmp");
3236
3237 if (!ret.empty ())
3238 {
3239 /* We are only interested in checking one element. */
3240 probe *p = ret[0];
3241
3242 if (!p->can_evaluate_arguments ())
3243 {
3244 /* We cannot use the probe interface here,
3245 because it does not know how to evaluate
3246 arguments. */
3247 ret.clear ();
3248 }
3249 }
3250 bp_objfile_data->longjmp_probes = ret;
3251 bp_objfile_data->longjmp_searched = 1;
3252 }
3253
3254 if (!bp_objfile_data->longjmp_probes.empty ())
3255 {
3256 for (probe *p : bp_objfile_data->longjmp_probes)
3257 {
3258 struct breakpoint *b;
3259
3260 b = create_internal_breakpoint (gdbarch,
3261 p->get_relocated_address (objfile),
3262 bp_longjmp_master,
3263 &internal_breakpoint_ops);
3264 b->location = new_probe_location ("-probe-stap libc:longjmp");
3265 b->enable_state = bp_disabled;
3266 }
3267
3268 continue;
3269 }
3270
3271 if (!gdbarch_get_longjmp_target_p (gdbarch))
3272 continue;
3273
3274 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3275 {
3276 struct breakpoint *b;
3277 const char *func_name;
3278 CORE_ADDR addr;
3279 struct explicit_location explicit_loc;
3280
3281 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3282 continue;
3283
3284 func_name = longjmp_names[i];
3285 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3286 {
3287 struct bound_minimal_symbol m;
3288
3289 m = lookup_minimal_symbol_text (func_name, objfile);
3290 if (m.minsym == NULL)
3291 {
3292 /* Prevent future lookups in this objfile. */
3293 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3294 continue;
3295 }
3296 bp_objfile_data->longjmp_msym[i] = m;
3297 }
3298
3299 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3300 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3301 &internal_breakpoint_ops);
3302 initialize_explicit_location (&explicit_loc);
3303 explicit_loc.function_name = ASTRDUP (func_name);
3304 b->location = new_explicit_location (&explicit_loc);
3305 b->enable_state = bp_disabled;
3306 }
3307 }
3308 }
3309 }
3310
3311 /* Create a master std::terminate breakpoint. */
3312 static void
3313 create_std_terminate_master_breakpoint (void)
3314 {
3315 const char *const func_name = "std::terminate()";
3316
3317 scoped_restore_current_program_space restore_pspace;
3318
3319 for (struct program_space *pspace : program_spaces)
3320 {
3321 CORE_ADDR addr;
3322
3323 set_current_program_space (pspace);
3324
3325 for (objfile *objfile : current_program_space->objfiles ())
3326 {
3327 struct breakpoint *b;
3328 struct breakpoint_objfile_data *bp_objfile_data;
3329 struct explicit_location explicit_loc;
3330
3331 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3332
3333 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3334 continue;
3335
3336 if (bp_objfile_data->terminate_msym.minsym == NULL)
3337 {
3338 struct bound_minimal_symbol m;
3339
3340 m = lookup_minimal_symbol (func_name, NULL, objfile);
3341 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3342 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3343 {
3344 /* Prevent future lookups in this objfile. */
3345 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3346 continue;
3347 }
3348 bp_objfile_data->terminate_msym = m;
3349 }
3350
3351 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3352 b = create_internal_breakpoint (objfile->arch (), addr,
3353 bp_std_terminate_master,
3354 &internal_breakpoint_ops);
3355 initialize_explicit_location (&explicit_loc);
3356 explicit_loc.function_name = ASTRDUP (func_name);
3357 b->location = new_explicit_location (&explicit_loc);
3358 b->enable_state = bp_disabled;
3359 }
3360 }
3361 }
3362
3363 /* Install a master breakpoint on the unwinder's debug hook. */
3364
3365 static void
3366 create_exception_master_breakpoint (void)
3367 {
3368 const char *const func_name = "_Unwind_DebugHook";
3369
3370 for (objfile *objfile : current_program_space->objfiles ())
3371 {
3372 struct breakpoint *b;
3373 struct gdbarch *gdbarch;
3374 struct breakpoint_objfile_data *bp_objfile_data;
3375 CORE_ADDR addr;
3376 struct explicit_location explicit_loc;
3377
3378 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3379
3380 /* We prefer the SystemTap probe point if it exists. */
3381 if (!bp_objfile_data->exception_searched)
3382 {
3383 std::vector<probe *> ret
3384 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3385
3386 if (!ret.empty ())
3387 {
3388 /* We are only interested in checking one element. */
3389 probe *p = ret[0];
3390
3391 if (!p->can_evaluate_arguments ())
3392 {
3393 /* We cannot use the probe interface here, because it does
3394 not know how to evaluate arguments. */
3395 ret.clear ();
3396 }
3397 }
3398 bp_objfile_data->exception_probes = ret;
3399 bp_objfile_data->exception_searched = 1;
3400 }
3401
3402 if (!bp_objfile_data->exception_probes.empty ())
3403 {
3404 gdbarch = objfile->arch ();
3405
3406 for (probe *p : bp_objfile_data->exception_probes)
3407 {
3408 b = create_internal_breakpoint (gdbarch,
3409 p->get_relocated_address (objfile),
3410 bp_exception_master,
3411 &internal_breakpoint_ops);
3412 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3413 b->enable_state = bp_disabled;
3414 }
3415
3416 continue;
3417 }
3418
3419 /* Otherwise, try the hook function. */
3420
3421 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3422 continue;
3423
3424 gdbarch = objfile->arch ();
3425
3426 if (bp_objfile_data->exception_msym.minsym == NULL)
3427 {
3428 struct bound_minimal_symbol debug_hook;
3429
3430 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3431 if (debug_hook.minsym == NULL)
3432 {
3433 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3434 continue;
3435 }
3436
3437 bp_objfile_data->exception_msym = debug_hook;
3438 }
3439
3440 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3441 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3442 current_top_target ());
3443 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3444 &internal_breakpoint_ops);
3445 initialize_explicit_location (&explicit_loc);
3446 explicit_loc.function_name = ASTRDUP (func_name);
3447 b->location = new_explicit_location (&explicit_loc);
3448 b->enable_state = bp_disabled;
3449 }
3450 }
3451
3452 /* Does B have a location spec? */
3453
3454 static int
3455 breakpoint_event_location_empty_p (const struct breakpoint *b)
3456 {
3457 return b->location != NULL && event_location_empty_p (b->location.get ());
3458 }
3459
3460 void
3461 update_breakpoints_after_exec (void)
3462 {
3463 struct breakpoint *b, *b_tmp;
3464 struct bp_location *bploc, **bplocp_tmp;
3465
3466 /* We're about to delete breakpoints from GDB's lists. If the
3467 INSERTED flag is true, GDB will try to lift the breakpoints by
3468 writing the breakpoints' "shadow contents" back into memory. The
3469 "shadow contents" are NOT valid after an exec, so GDB should not
3470 do that. Instead, the target is responsible from marking
3471 breakpoints out as soon as it detects an exec. We don't do that
3472 here instead, because there may be other attempts to delete
3473 breakpoints after detecting an exec and before reaching here. */
3474 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3475 if (bploc->pspace == current_program_space)
3476 gdb_assert (!bploc->inserted);
3477
3478 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3479 {
3480 if (b->pspace != current_program_space)
3481 continue;
3482
3483 /* Solib breakpoints must be explicitly reset after an exec(). */
3484 if (b->type == bp_shlib_event)
3485 {
3486 delete_breakpoint (b);
3487 continue;
3488 }
3489
3490 /* JIT breakpoints must be explicitly reset after an exec(). */
3491 if (b->type == bp_jit_event)
3492 {
3493 delete_breakpoint (b);
3494 continue;
3495 }
3496
3497 /* Thread event breakpoints must be set anew after an exec(),
3498 as must overlay event and longjmp master breakpoints. */
3499 if (b->type == bp_thread_event || b->type == bp_overlay_event
3500 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3501 || b->type == bp_exception_master)
3502 {
3503 delete_breakpoint (b);
3504 continue;
3505 }
3506
3507 /* Step-resume breakpoints are meaningless after an exec(). */
3508 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3509 {
3510 delete_breakpoint (b);
3511 continue;
3512 }
3513
3514 /* Just like single-step breakpoints. */
3515 if (b->type == bp_single_step)
3516 {
3517 delete_breakpoint (b);
3518 continue;
3519 }
3520
3521 /* Longjmp and longjmp-resume breakpoints are also meaningless
3522 after an exec. */
3523 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3524 || b->type == bp_longjmp_call_dummy
3525 || b->type == bp_exception || b->type == bp_exception_resume)
3526 {
3527 delete_breakpoint (b);
3528 continue;
3529 }
3530
3531 if (b->type == bp_catchpoint)
3532 {
3533 /* For now, none of the bp_catchpoint breakpoints need to
3534 do anything at this point. In the future, if some of
3535 the catchpoints need to something, we will need to add
3536 a new method, and call this method from here. */
3537 continue;
3538 }
3539
3540 /* bp_finish is a special case. The only way we ought to be able
3541 to see one of these when an exec() has happened, is if the user
3542 caught a vfork, and then said "finish". Ordinarily a finish just
3543 carries them to the call-site of the current callee, by setting
3544 a temporary bp there and resuming. But in this case, the finish
3545 will carry them entirely through the vfork & exec.
3546
3547 We don't want to allow a bp_finish to remain inserted now. But
3548 we can't safely delete it, 'cause finish_command has a handle to
3549 the bp on a bpstat, and will later want to delete it. There's a
3550 chance (and I've seen it happen) that if we delete the bp_finish
3551 here, that its storage will get reused by the time finish_command
3552 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3553 We really must allow finish_command to delete a bp_finish.
3554
3555 In the absence of a general solution for the "how do we know
3556 it's safe to delete something others may have handles to?"
3557 problem, what we'll do here is just uninsert the bp_finish, and
3558 let finish_command delete it.
3559
3560 (We know the bp_finish is "doomed" in the sense that it's
3561 momentary, and will be deleted as soon as finish_command sees
3562 the inferior stopped. So it doesn't matter that the bp's
3563 address is probably bogus in the new a.out, unlike e.g., the
3564 solib breakpoints.) */
3565
3566 if (b->type == bp_finish)
3567 {
3568 continue;
3569 }
3570
3571 /* Without a symbolic address, we have little hope of the
3572 pre-exec() address meaning the same thing in the post-exec()
3573 a.out. */
3574 if (breakpoint_event_location_empty_p (b))
3575 {
3576 delete_breakpoint (b);
3577 continue;
3578 }
3579 }
3580 }
3581
3582 int
3583 detach_breakpoints (ptid_t ptid)
3584 {
3585 struct bp_location *bl, **blp_tmp;
3586 int val = 0;
3587 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3588 struct inferior *inf = current_inferior ();
3589
3590 if (ptid.pid () == inferior_ptid.pid ())
3591 error (_("Cannot detach breakpoints of inferior_ptid"));
3592
3593 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3594 inferior_ptid = ptid;
3595 ALL_BP_LOCATIONS (bl, blp_tmp)
3596 {
3597 if (bl->pspace != inf->pspace)
3598 continue;
3599
3600 /* This function must physically remove breakpoints locations
3601 from the specified ptid, without modifying the breakpoint
3602 package's state. Locations of type bp_loc_other are only
3603 maintained at GDB side. So, there is no need to remove
3604 these bp_loc_other locations. Moreover, removing these
3605 would modify the breakpoint package's state. */
3606 if (bl->loc_type == bp_loc_other)
3607 continue;
3608
3609 if (bl->inserted)
3610 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3611 }
3612
3613 return val;
3614 }
3615
3616 /* Remove the breakpoint location BL from the current address space.
3617 Note that this is used to detach breakpoints from a child fork.
3618 When we get here, the child isn't in the inferior list, and neither
3619 do we have objects to represent its address space --- we should
3620 *not* look at bl->pspace->aspace here. */
3621
3622 static int
3623 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3624 {
3625 int val;
3626
3627 /* BL is never in moribund_locations by our callers. */
3628 gdb_assert (bl->owner != NULL);
3629
3630 /* The type of none suggests that owner is actually deleted.
3631 This should not ever happen. */
3632 gdb_assert (bl->owner->type != bp_none);
3633
3634 if (bl->loc_type == bp_loc_software_breakpoint
3635 || bl->loc_type == bp_loc_hardware_breakpoint)
3636 {
3637 /* "Normal" instruction breakpoint: either the standard
3638 trap-instruction bp (bp_breakpoint), or a
3639 bp_hardware_breakpoint. */
3640
3641 /* First check to see if we have to handle an overlay. */
3642 if (overlay_debugging == ovly_off
3643 || bl->section == NULL
3644 || !(section_is_overlay (bl->section)))
3645 {
3646 /* No overlay handling: just remove the breakpoint. */
3647
3648 /* If we're trying to uninsert a memory breakpoint that we
3649 know is set in a dynamic object that is marked
3650 shlib_disabled, then either the dynamic object was
3651 removed with "remove-symbol-file" or with
3652 "nosharedlibrary". In the former case, we don't know
3653 whether another dynamic object might have loaded over the
3654 breakpoint's address -- the user might well let us know
3655 about it next with add-symbol-file (the whole point of
3656 add-symbol-file is letting the user manually maintain a
3657 list of dynamically loaded objects). If we have the
3658 breakpoint's shadow memory, that is, this is a software
3659 breakpoint managed by GDB, check whether the breakpoint
3660 is still inserted in memory, to avoid overwriting wrong
3661 code with stale saved shadow contents. Note that HW
3662 breakpoints don't have shadow memory, as they're
3663 implemented using a mechanism that is not dependent on
3664 being able to modify the target's memory, and as such
3665 they should always be removed. */
3666 if (bl->shlib_disabled
3667 && bl->target_info.shadow_len != 0
3668 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3669 val = 0;
3670 else
3671 val = bl->owner->ops->remove_location (bl, reason);
3672 }
3673 else
3674 {
3675 /* This breakpoint is in an overlay section.
3676 Did we set a breakpoint at the LMA? */
3677 if (!overlay_events_enabled)
3678 {
3679 /* Yes -- overlay event support is not active, so we
3680 should have set a breakpoint at the LMA. Remove it.
3681 */
3682 /* Ignore any failures: if the LMA is in ROM, we will
3683 have already warned when we failed to insert it. */
3684 if (bl->loc_type == bp_loc_hardware_breakpoint)
3685 target_remove_hw_breakpoint (bl->gdbarch,
3686 &bl->overlay_target_info);
3687 else
3688 target_remove_breakpoint (bl->gdbarch,
3689 &bl->overlay_target_info,
3690 reason);
3691 }
3692 /* Did we set a breakpoint at the VMA?
3693 If so, we will have marked the breakpoint 'inserted'. */
3694 if (bl->inserted)
3695 {
3696 /* Yes -- remove it. Previously we did not bother to
3697 remove the breakpoint if the section had been
3698 unmapped, but let's not rely on that being safe. We
3699 don't know what the overlay manager might do. */
3700
3701 /* However, we should remove *software* breakpoints only
3702 if the section is still mapped, or else we overwrite
3703 wrong code with the saved shadow contents. */
3704 if (bl->loc_type == bp_loc_hardware_breakpoint
3705 || section_is_mapped (bl->section))
3706 val = bl->owner->ops->remove_location (bl, reason);
3707 else
3708 val = 0;
3709 }
3710 else
3711 {
3712 /* No -- not inserted, so no need to remove. No error. */
3713 val = 0;
3714 }
3715 }
3716
3717 /* In some cases, we might not be able to remove a breakpoint in
3718 a shared library that has already been removed, but we have
3719 not yet processed the shlib unload event. Similarly for an
3720 unloaded add-symbol-file object - the user might not yet have
3721 had the chance to remove-symbol-file it. shlib_disabled will
3722 be set if the library/object has already been removed, but
3723 the breakpoint hasn't been uninserted yet, e.g., after
3724 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3725 always-inserted mode. */
3726 if (val
3727 && (bl->loc_type == bp_loc_software_breakpoint
3728 && (bl->shlib_disabled
3729 || solib_name_from_address (bl->pspace, bl->address)
3730 || shared_objfile_contains_address_p (bl->pspace,
3731 bl->address))))
3732 val = 0;
3733
3734 if (val)
3735 return val;
3736 bl->inserted = (reason == DETACH_BREAKPOINT);
3737 }
3738 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3739 {
3740 gdb_assert (bl->owner->ops != NULL
3741 && bl->owner->ops->remove_location != NULL);
3742
3743 bl->inserted = (reason == DETACH_BREAKPOINT);
3744 bl->owner->ops->remove_location (bl, reason);
3745
3746 /* Failure to remove any of the hardware watchpoints comes here. */
3747 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3748 warning (_("Could not remove hardware watchpoint %d."),
3749 bl->owner->number);
3750 }
3751 else if (bl->owner->type == bp_catchpoint
3752 && breakpoint_enabled (bl->owner)
3753 && !bl->duplicate)
3754 {
3755 gdb_assert (bl->owner->ops != NULL
3756 && bl->owner->ops->remove_location != NULL);
3757
3758 val = bl->owner->ops->remove_location (bl, reason);
3759 if (val)
3760 return val;
3761
3762 bl->inserted = (reason == DETACH_BREAKPOINT);
3763 }
3764
3765 return 0;
3766 }
3767
3768 static int
3769 remove_breakpoint (struct bp_location *bl)
3770 {
3771 /* BL is never in moribund_locations by our callers. */
3772 gdb_assert (bl->owner != NULL);
3773
3774 /* The type of none suggests that owner is actually deleted.
3775 This should not ever happen. */
3776 gdb_assert (bl->owner->type != bp_none);
3777
3778 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3779
3780 switch_to_program_space_and_thread (bl->pspace);
3781
3782 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
3783 }
3784
3785 /* Clear the "inserted" flag in all breakpoints. */
3786
3787 void
3788 mark_breakpoints_out (void)
3789 {
3790 struct bp_location *bl, **blp_tmp;
3791
3792 ALL_BP_LOCATIONS (bl, blp_tmp)
3793 if (bl->pspace == current_program_space)
3794 bl->inserted = 0;
3795 }
3796
3797 /* Clear the "inserted" flag in all breakpoints and delete any
3798 breakpoints which should go away between runs of the program.
3799
3800 Plus other such housekeeping that has to be done for breakpoints
3801 between runs.
3802
3803 Note: this function gets called at the end of a run (by
3804 generic_mourn_inferior) and when a run begins (by
3805 init_wait_for_inferior). */
3806
3807
3808
3809 void
3810 breakpoint_init_inferior (enum inf_context context)
3811 {
3812 struct breakpoint *b, *b_tmp;
3813 struct program_space *pspace = current_program_space;
3814
3815 /* If breakpoint locations are shared across processes, then there's
3816 nothing to do. */
3817 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
3818 return;
3819
3820 mark_breakpoints_out ();
3821
3822 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3823 {
3824 if (b->loc && b->loc->pspace != pspace)
3825 continue;
3826
3827 switch (b->type)
3828 {
3829 case bp_call_dummy:
3830 case bp_longjmp_call_dummy:
3831
3832 /* If the call dummy breakpoint is at the entry point it will
3833 cause problems when the inferior is rerun, so we better get
3834 rid of it. */
3835
3836 case bp_watchpoint_scope:
3837
3838 /* Also get rid of scope breakpoints. */
3839
3840 case bp_shlib_event:
3841
3842 /* Also remove solib event breakpoints. Their addresses may
3843 have changed since the last time we ran the program.
3844 Actually we may now be debugging against different target;
3845 and so the solib backend that installed this breakpoint may
3846 not be used in by the target. E.g.,
3847
3848 (gdb) file prog-linux
3849 (gdb) run # native linux target
3850 ...
3851 (gdb) kill
3852 (gdb) file prog-win.exe
3853 (gdb) tar rem :9999 # remote Windows gdbserver.
3854 */
3855
3856 case bp_step_resume:
3857
3858 /* Also remove step-resume breakpoints. */
3859
3860 case bp_single_step:
3861
3862 /* Also remove single-step breakpoints. */
3863
3864 delete_breakpoint (b);
3865 break;
3866
3867 case bp_watchpoint:
3868 case bp_hardware_watchpoint:
3869 case bp_read_watchpoint:
3870 case bp_access_watchpoint:
3871 {
3872 struct watchpoint *w = (struct watchpoint *) b;
3873
3874 /* Likewise for watchpoints on local expressions. */
3875 if (w->exp_valid_block != NULL)
3876 delete_breakpoint (b);
3877 else
3878 {
3879 /* Get rid of existing locations, which are no longer
3880 valid. New ones will be created in
3881 update_watchpoint, when the inferior is restarted.
3882 The next update_global_location_list call will
3883 garbage collect them. */
3884 b->loc = NULL;
3885
3886 if (context == inf_starting)
3887 {
3888 /* Reset val field to force reread of starting value in
3889 insert_breakpoints. */
3890 w->val.reset (nullptr);
3891 w->val_valid = false;
3892 }
3893 }
3894 }
3895 break;
3896 default:
3897 break;
3898 }
3899 }
3900
3901 /* Get rid of the moribund locations. */
3902 for (bp_location *bl : moribund_locations)
3903 decref_bp_location (&bl);
3904 moribund_locations.clear ();
3905 }
3906
3907 /* These functions concern about actual breakpoints inserted in the
3908 target --- to e.g. check if we need to do decr_pc adjustment or if
3909 we need to hop over the bkpt --- so we check for address space
3910 match, not program space. */
3911
3912 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3913 exists at PC. It returns ordinary_breakpoint_here if it's an
3914 ordinary breakpoint, or permanent_breakpoint_here if it's a
3915 permanent breakpoint.
3916 - When continuing from a location with an ordinary breakpoint, we
3917 actually single step once before calling insert_breakpoints.
3918 - When continuing from a location with a permanent breakpoint, we
3919 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3920 the target, to advance the PC past the breakpoint. */
3921
3922 enum breakpoint_here
3923 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
3924 {
3925 struct bp_location *bl, **blp_tmp;
3926 int any_breakpoint_here = 0;
3927
3928 ALL_BP_LOCATIONS (bl, blp_tmp)
3929 {
3930 if (bl->loc_type != bp_loc_software_breakpoint
3931 && bl->loc_type != bp_loc_hardware_breakpoint)
3932 continue;
3933
3934 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
3935 if ((breakpoint_enabled (bl->owner)
3936 || bl->permanent)
3937 && breakpoint_location_address_match (bl, aspace, pc))
3938 {
3939 if (overlay_debugging
3940 && section_is_overlay (bl->section)
3941 && !section_is_mapped (bl->section))
3942 continue; /* unmapped overlay -- can't be a match */
3943 else if (bl->permanent)
3944 return permanent_breakpoint_here;
3945 else
3946 any_breakpoint_here = 1;
3947 }
3948 }
3949
3950 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
3951 }
3952
3953 /* See breakpoint.h. */
3954
3955 int
3956 breakpoint_in_range_p (const address_space *aspace,
3957 CORE_ADDR addr, ULONGEST len)
3958 {
3959 struct bp_location *bl, **blp_tmp;
3960
3961 ALL_BP_LOCATIONS (bl, blp_tmp)
3962 {
3963 if (bl->loc_type != bp_loc_software_breakpoint
3964 && bl->loc_type != bp_loc_hardware_breakpoint)
3965 continue;
3966
3967 if ((breakpoint_enabled (bl->owner)
3968 || bl->permanent)
3969 && breakpoint_location_address_range_overlap (bl, aspace,
3970 addr, len))
3971 {
3972 if (overlay_debugging
3973 && section_is_overlay (bl->section)
3974 && !section_is_mapped (bl->section))
3975 {
3976 /* Unmapped overlay -- can't be a match. */
3977 continue;
3978 }
3979
3980 return 1;
3981 }
3982 }
3983
3984 return 0;
3985 }
3986
3987 /* Return true if there's a moribund breakpoint at PC. */
3988
3989 int
3990 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
3991 {
3992 for (bp_location *loc : moribund_locations)
3993 if (breakpoint_location_address_match (loc, aspace, pc))
3994 return 1;
3995
3996 return 0;
3997 }
3998
3999 /* Returns non-zero iff BL is inserted at PC, in address space
4000 ASPACE. */
4001
4002 static int
4003 bp_location_inserted_here_p (struct bp_location *bl,
4004 const address_space *aspace, CORE_ADDR pc)
4005 {
4006 if (bl->inserted
4007 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4008 aspace, pc))
4009 {
4010 if (overlay_debugging
4011 && section_is_overlay (bl->section)
4012 && !section_is_mapped (bl->section))
4013 return 0; /* unmapped overlay -- can't be a match */
4014 else
4015 return 1;
4016 }
4017 return 0;
4018 }
4019
4020 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4021
4022 int
4023 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4024 {
4025 struct bp_location **blp, **blp_tmp = NULL;
4026
4027 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4028 {
4029 struct bp_location *bl = *blp;
4030
4031 if (bl->loc_type != bp_loc_software_breakpoint
4032 && bl->loc_type != bp_loc_hardware_breakpoint)
4033 continue;
4034
4035 if (bp_location_inserted_here_p (bl, aspace, pc))
4036 return 1;
4037 }
4038 return 0;
4039 }
4040
4041 /* This function returns non-zero iff there is a software breakpoint
4042 inserted at PC. */
4043
4044 int
4045 software_breakpoint_inserted_here_p (const address_space *aspace,
4046 CORE_ADDR pc)
4047 {
4048 struct bp_location **blp, **blp_tmp = NULL;
4049
4050 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4051 {
4052 struct bp_location *bl = *blp;
4053
4054 if (bl->loc_type != bp_loc_software_breakpoint)
4055 continue;
4056
4057 if (bp_location_inserted_here_p (bl, aspace, pc))
4058 return 1;
4059 }
4060
4061 return 0;
4062 }
4063
4064 /* See breakpoint.h. */
4065
4066 int
4067 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4068 CORE_ADDR pc)
4069 {
4070 struct bp_location **blp, **blp_tmp = NULL;
4071
4072 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4073 {
4074 struct bp_location *bl = *blp;
4075
4076 if (bl->loc_type != bp_loc_hardware_breakpoint)
4077 continue;
4078
4079 if (bp_location_inserted_here_p (bl, aspace, pc))
4080 return 1;
4081 }
4082
4083 return 0;
4084 }
4085
4086 int
4087 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4088 CORE_ADDR addr, ULONGEST len)
4089 {
4090 struct breakpoint *bpt;
4091
4092 ALL_BREAKPOINTS (bpt)
4093 {
4094 struct bp_location *loc;
4095
4096 if (bpt->type != bp_hardware_watchpoint
4097 && bpt->type != bp_access_watchpoint)
4098 continue;
4099
4100 if (!breakpoint_enabled (bpt))
4101 continue;
4102
4103 for (loc = bpt->loc; loc; loc = loc->next)
4104 if (loc->pspace->aspace == aspace && loc->inserted)
4105 {
4106 CORE_ADDR l, h;
4107
4108 /* Check for intersection. */
4109 l = std::max<CORE_ADDR> (loc->address, addr);
4110 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4111 if (l < h)
4112 return 1;
4113 }
4114 }
4115 return 0;
4116 }
4117
4118 /* See breakpoint.h. */
4119
4120 bool
4121 is_catchpoint (struct breakpoint *b)
4122 {
4123 return (b->type == bp_catchpoint);
4124 }
4125
4126 /* Frees any storage that is part of a bpstat. Does not walk the
4127 'next' chain. */
4128
4129 bpstats::~bpstats ()
4130 {
4131 if (bp_location_at != NULL)
4132 decref_bp_location (&bp_location_at);
4133 }
4134
4135 /* Clear a bpstat so that it says we are not at any breakpoint.
4136 Also free any storage that is part of a bpstat. */
4137
4138 void
4139 bpstat_clear (bpstat *bsp)
4140 {
4141 bpstat p;
4142 bpstat q;
4143
4144 if (bsp == 0)
4145 return;
4146 p = *bsp;
4147 while (p != NULL)
4148 {
4149 q = p->next;
4150 delete p;
4151 p = q;
4152 }
4153 *bsp = NULL;
4154 }
4155
4156 bpstats::bpstats (const bpstats &other)
4157 : next (NULL),
4158 bp_location_at (other.bp_location_at),
4159 breakpoint_at (other.breakpoint_at),
4160 commands (other.commands),
4161 print (other.print),
4162 stop (other.stop),
4163 print_it (other.print_it)
4164 {
4165 if (other.old_val != NULL)
4166 old_val = release_value (value_copy (other.old_val.get ()));
4167 incref_bp_location (bp_location_at);
4168 }
4169
4170 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4171 is part of the bpstat is copied as well. */
4172
4173 bpstat
4174 bpstat_copy (bpstat bs)
4175 {
4176 bpstat p = NULL;
4177 bpstat tmp;
4178 bpstat retval = NULL;
4179
4180 if (bs == NULL)
4181 return bs;
4182
4183 for (; bs != NULL; bs = bs->next)
4184 {
4185 tmp = new bpstats (*bs);
4186
4187 if (p == NULL)
4188 /* This is the first thing in the chain. */
4189 retval = tmp;
4190 else
4191 p->next = tmp;
4192 p = tmp;
4193 }
4194 p->next = NULL;
4195 return retval;
4196 }
4197
4198 /* Find the bpstat associated with this breakpoint. */
4199
4200 bpstat
4201 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4202 {
4203 if (bsp == NULL)
4204 return NULL;
4205
4206 for (; bsp != NULL; bsp = bsp->next)
4207 {
4208 if (bsp->breakpoint_at == breakpoint)
4209 return bsp;
4210 }
4211 return NULL;
4212 }
4213
4214 /* See breakpoint.h. */
4215
4216 bool
4217 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4218 {
4219 for (; bsp != NULL; bsp = bsp->next)
4220 {
4221 if (bsp->breakpoint_at == NULL)
4222 {
4223 /* A moribund location can never explain a signal other than
4224 GDB_SIGNAL_TRAP. */
4225 if (sig == GDB_SIGNAL_TRAP)
4226 return true;
4227 }
4228 else
4229 {
4230 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4231 sig))
4232 return true;
4233 }
4234 }
4235
4236 return false;
4237 }
4238
4239 /* Put in *NUM the breakpoint number of the first breakpoint we are
4240 stopped at. *BSP upon return is a bpstat which points to the
4241 remaining breakpoints stopped at (but which is not guaranteed to be
4242 good for anything but further calls to bpstat_num).
4243
4244 Return 0 if passed a bpstat which does not indicate any breakpoints.
4245 Return -1 if stopped at a breakpoint that has been deleted since
4246 we set it.
4247 Return 1 otherwise. */
4248
4249 int
4250 bpstat_num (bpstat *bsp, int *num)
4251 {
4252 struct breakpoint *b;
4253
4254 if ((*bsp) == NULL)
4255 return 0; /* No more breakpoint values */
4256
4257 /* We assume we'll never have several bpstats that correspond to a
4258 single breakpoint -- otherwise, this function might return the
4259 same number more than once and this will look ugly. */
4260 b = (*bsp)->breakpoint_at;
4261 *bsp = (*bsp)->next;
4262 if (b == NULL)
4263 return -1; /* breakpoint that's been deleted since */
4264
4265 *num = b->number; /* We have its number */
4266 return 1;
4267 }
4268
4269 /* See breakpoint.h. */
4270
4271 void
4272 bpstat_clear_actions (void)
4273 {
4274 bpstat bs;
4275
4276 if (inferior_ptid == null_ptid)
4277 return;
4278
4279 thread_info *tp = inferior_thread ();
4280 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4281 {
4282 bs->commands = NULL;
4283 bs->old_val.reset (nullptr);
4284 }
4285 }
4286
4287 /* Called when a command is about to proceed the inferior. */
4288
4289 static void
4290 breakpoint_about_to_proceed (void)
4291 {
4292 if (inferior_ptid != null_ptid)
4293 {
4294 struct thread_info *tp = inferior_thread ();
4295
4296 /* Allow inferior function calls in breakpoint commands to not
4297 interrupt the command list. When the call finishes
4298 successfully, the inferior will be standing at the same
4299 breakpoint as if nothing happened. */
4300 if (tp->control.in_infcall)
4301 return;
4302 }
4303
4304 breakpoint_proceeded = 1;
4305 }
4306
4307 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4308 or its equivalent. */
4309
4310 static int
4311 command_line_is_silent (struct command_line *cmd)
4312 {
4313 return cmd && (strcmp ("silent", cmd->line) == 0);
4314 }
4315
4316 /* Execute all the commands associated with all the breakpoints at
4317 this location. Any of these commands could cause the process to
4318 proceed beyond this point, etc. We look out for such changes by
4319 checking the global "breakpoint_proceeded" after each command.
4320
4321 Returns true if a breakpoint command resumed the inferior. In that
4322 case, it is the caller's responsibility to recall it again with the
4323 bpstat of the current thread. */
4324
4325 static int
4326 bpstat_do_actions_1 (bpstat *bsp)
4327 {
4328 bpstat bs;
4329 int again = 0;
4330
4331 /* Avoid endless recursion if a `source' command is contained
4332 in bs->commands. */
4333 if (executing_breakpoint_commands)
4334 return 0;
4335
4336 scoped_restore save_executing
4337 = make_scoped_restore (&executing_breakpoint_commands, 1);
4338
4339 scoped_restore preventer = prevent_dont_repeat ();
4340
4341 /* This pointer will iterate over the list of bpstat's. */
4342 bs = *bsp;
4343
4344 breakpoint_proceeded = 0;
4345 for (; bs != NULL; bs = bs->next)
4346 {
4347 struct command_line *cmd = NULL;
4348
4349 /* Take ownership of the BSP's command tree, if it has one.
4350
4351 The command tree could legitimately contain commands like
4352 'step' and 'next', which call clear_proceed_status, which
4353 frees stop_bpstat's command tree. To make sure this doesn't
4354 free the tree we're executing out from under us, we need to
4355 take ownership of the tree ourselves. Since a given bpstat's
4356 commands are only executed once, we don't need to copy it; we
4357 can clear the pointer in the bpstat, and make sure we free
4358 the tree when we're done. */
4359 counted_command_line ccmd = bs->commands;
4360 bs->commands = NULL;
4361 if (ccmd != NULL)
4362 cmd = ccmd.get ();
4363 if (command_line_is_silent (cmd))
4364 {
4365 /* The action has been already done by bpstat_stop_status. */
4366 cmd = cmd->next;
4367 }
4368
4369 while (cmd != NULL)
4370 {
4371 execute_control_command (cmd);
4372
4373 if (breakpoint_proceeded)
4374 break;
4375 else
4376 cmd = cmd->next;
4377 }
4378
4379 if (breakpoint_proceeded)
4380 {
4381 if (current_ui->async)
4382 /* If we are in async mode, then the target might be still
4383 running, not stopped at any breakpoint, so nothing for
4384 us to do here -- just return to the event loop. */
4385 ;
4386 else
4387 /* In sync mode, when execute_control_command returns
4388 we're already standing on the next breakpoint.
4389 Breakpoint commands for that stop were not run, since
4390 execute_command does not run breakpoint commands --
4391 only command_line_handler does, but that one is not
4392 involved in execution of breakpoint commands. So, we
4393 can now execute breakpoint commands. It should be
4394 noted that making execute_command do bpstat actions is
4395 not an option -- in this case we'll have recursive
4396 invocation of bpstat for each breakpoint with a
4397 command, and can easily blow up GDB stack. Instead, we
4398 return true, which will trigger the caller to recall us
4399 with the new stop_bpstat. */
4400 again = 1;
4401 break;
4402 }
4403 }
4404 return again;
4405 }
4406
4407 /* Helper for bpstat_do_actions. Get the current thread, if there's
4408 one, is alive and has execution. Return NULL otherwise. */
4409
4410 static thread_info *
4411 get_bpstat_thread ()
4412 {
4413 if (inferior_ptid == null_ptid || !target_has_execution ())
4414 return NULL;
4415
4416 thread_info *tp = inferior_thread ();
4417 if (tp->state == THREAD_EXITED || tp->executing)
4418 return NULL;
4419 return tp;
4420 }
4421
4422 void
4423 bpstat_do_actions (void)
4424 {
4425 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4426 thread_info *tp;
4427
4428 /* Do any commands attached to breakpoint we are stopped at. */
4429 while ((tp = get_bpstat_thread ()) != NULL)
4430 {
4431 /* Since in sync mode, bpstat_do_actions may resume the
4432 inferior, and only return when it is stopped at the next
4433 breakpoint, we keep doing breakpoint actions until it returns
4434 false to indicate the inferior was not resumed. */
4435 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4436 break;
4437 }
4438
4439 cleanup_if_error.release ();
4440 }
4441
4442 /* Print out the (old or new) value associated with a watchpoint. */
4443
4444 static void
4445 watchpoint_value_print (struct value *val, struct ui_file *stream)
4446 {
4447 if (val == NULL)
4448 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4449 else
4450 {
4451 struct value_print_options opts;
4452 get_user_print_options (&opts);
4453 value_print (val, stream, &opts);
4454 }
4455 }
4456
4457 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4458 debugging multiple threads. */
4459
4460 void
4461 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4462 {
4463 if (uiout->is_mi_like_p ())
4464 return;
4465
4466 uiout->text ("\n");
4467
4468 if (show_thread_that_caused_stop ())
4469 {
4470 const char *name;
4471 struct thread_info *thr = inferior_thread ();
4472
4473 uiout->text ("Thread ");
4474 uiout->field_string ("thread-id", print_thread_id (thr));
4475
4476 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4477 if (name != NULL)
4478 {
4479 uiout->text (" \"");
4480 uiout->field_string ("name", name);
4481 uiout->text ("\"");
4482 }
4483
4484 uiout->text (" hit ");
4485 }
4486 }
4487
4488 /* Generic routine for printing messages indicating why we
4489 stopped. The behavior of this function depends on the value
4490 'print_it' in the bpstat structure. Under some circumstances we
4491 may decide not to print anything here and delegate the task to
4492 normal_stop(). */
4493
4494 static enum print_stop_action
4495 print_bp_stop_message (bpstat bs)
4496 {
4497 switch (bs->print_it)
4498 {
4499 case print_it_noop:
4500 /* Nothing should be printed for this bpstat entry. */
4501 return PRINT_UNKNOWN;
4502 break;
4503
4504 case print_it_done:
4505 /* We still want to print the frame, but we already printed the
4506 relevant messages. */
4507 return PRINT_SRC_AND_LOC;
4508 break;
4509
4510 case print_it_normal:
4511 {
4512 struct breakpoint *b = bs->breakpoint_at;
4513
4514 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4515 which has since been deleted. */
4516 if (b == NULL)
4517 return PRINT_UNKNOWN;
4518
4519 /* Normal case. Call the breakpoint's print_it method. */
4520 return b->ops->print_it (bs);
4521 }
4522 break;
4523
4524 default:
4525 internal_error (__FILE__, __LINE__,
4526 _("print_bp_stop_message: unrecognized enum value"));
4527 break;
4528 }
4529 }
4530
4531 /* A helper function that prints a shared library stopped event. */
4532
4533 static void
4534 print_solib_event (int is_catchpoint)
4535 {
4536 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4537 bool any_added = !current_program_space->added_solibs.empty ();
4538
4539 if (!is_catchpoint)
4540 {
4541 if (any_added || any_deleted)
4542 current_uiout->text (_("Stopped due to shared library event:\n"));
4543 else
4544 current_uiout->text (_("Stopped due to shared library event (no "
4545 "libraries added or removed)\n"));
4546 }
4547
4548 if (current_uiout->is_mi_like_p ())
4549 current_uiout->field_string ("reason",
4550 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4551
4552 if (any_deleted)
4553 {
4554 current_uiout->text (_(" Inferior unloaded "));
4555 ui_out_emit_list list_emitter (current_uiout, "removed");
4556 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4557 {
4558 const std::string &name = current_program_space->deleted_solibs[ix];
4559
4560 if (ix > 0)
4561 current_uiout->text (" ");
4562 current_uiout->field_string ("library", name);
4563 current_uiout->text ("\n");
4564 }
4565 }
4566
4567 if (any_added)
4568 {
4569 current_uiout->text (_(" Inferior loaded "));
4570 ui_out_emit_list list_emitter (current_uiout, "added");
4571 bool first = true;
4572 for (so_list *iter : current_program_space->added_solibs)
4573 {
4574 if (!first)
4575 current_uiout->text (" ");
4576 first = false;
4577 current_uiout->field_string ("library", iter->so_name);
4578 current_uiout->text ("\n");
4579 }
4580 }
4581 }
4582
4583 /* Print a message indicating what happened. This is called from
4584 normal_stop(). The input to this routine is the head of the bpstat
4585 list - a list of the eventpoints that caused this stop. KIND is
4586 the target_waitkind for the stopping event. This
4587 routine calls the generic print routine for printing a message
4588 about reasons for stopping. This will print (for example) the
4589 "Breakpoint n," part of the output. The return value of this
4590 routine is one of:
4591
4592 PRINT_UNKNOWN: Means we printed nothing.
4593 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4594 code to print the location. An example is
4595 "Breakpoint 1, " which should be followed by
4596 the location.
4597 PRINT_SRC_ONLY: Means we printed something, but there is no need
4598 to also print the location part of the message.
4599 An example is the catch/throw messages, which
4600 don't require a location appended to the end.
4601 PRINT_NOTHING: We have done some printing and we don't need any
4602 further info to be printed. */
4603
4604 enum print_stop_action
4605 bpstat_print (bpstat bs, int kind)
4606 {
4607 enum print_stop_action val;
4608
4609 /* Maybe another breakpoint in the chain caused us to stop.
4610 (Currently all watchpoints go on the bpstat whether hit or not.
4611 That probably could (should) be changed, provided care is taken
4612 with respect to bpstat_explains_signal). */
4613 for (; bs; bs = bs->next)
4614 {
4615 val = print_bp_stop_message (bs);
4616 if (val == PRINT_SRC_ONLY
4617 || val == PRINT_SRC_AND_LOC
4618 || val == PRINT_NOTHING)
4619 return val;
4620 }
4621
4622 /* If we had hit a shared library event breakpoint,
4623 print_bp_stop_message would print out this message. If we hit an
4624 OS-level shared library event, do the same thing. */
4625 if (kind == TARGET_WAITKIND_LOADED)
4626 {
4627 print_solib_event (0);
4628 return PRINT_NOTHING;
4629 }
4630
4631 /* We reached the end of the chain, or we got a null BS to start
4632 with and nothing was printed. */
4633 return PRINT_UNKNOWN;
4634 }
4635
4636 /* Evaluate the boolean expression EXP and return the result. */
4637
4638 static bool
4639 breakpoint_cond_eval (expression *exp)
4640 {
4641 struct value *mark = value_mark ();
4642 bool res = value_true (evaluate_expression (exp));
4643
4644 value_free_to_mark (mark);
4645 return res;
4646 }
4647
4648 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4649
4650 bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4651 : next (NULL),
4652 bp_location_at (bl),
4653 breakpoint_at (bl->owner),
4654 commands (NULL),
4655 print (0),
4656 stop (0),
4657 print_it (print_it_normal)
4658 {
4659 incref_bp_location (bl);
4660 **bs_link_pointer = this;
4661 *bs_link_pointer = &next;
4662 }
4663
4664 bpstats::bpstats ()
4665 : next (NULL),
4666 bp_location_at (NULL),
4667 breakpoint_at (NULL),
4668 commands (NULL),
4669 print (0),
4670 stop (0),
4671 print_it (print_it_normal)
4672 {
4673 }
4674 \f
4675 /* The target has stopped with waitstatus WS. Check if any hardware
4676 watchpoints have triggered, according to the target. */
4677
4678 int
4679 watchpoints_triggered (struct target_waitstatus *ws)
4680 {
4681 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
4682 CORE_ADDR addr;
4683 struct breakpoint *b;
4684
4685 if (!stopped_by_watchpoint)
4686 {
4687 /* We were not stopped by a watchpoint. Mark all watchpoints
4688 as not triggered. */
4689 ALL_BREAKPOINTS (b)
4690 if (is_hardware_watchpoint (b))
4691 {
4692 struct watchpoint *w = (struct watchpoint *) b;
4693
4694 w->watchpoint_triggered = watch_triggered_no;
4695 }
4696
4697 return 0;
4698 }
4699
4700 if (!target_stopped_data_address (current_top_target (), &addr))
4701 {
4702 /* We were stopped by a watchpoint, but we don't know where.
4703 Mark all watchpoints as unknown. */
4704 ALL_BREAKPOINTS (b)
4705 if (is_hardware_watchpoint (b))
4706 {
4707 struct watchpoint *w = (struct watchpoint *) b;
4708
4709 w->watchpoint_triggered = watch_triggered_unknown;
4710 }
4711
4712 return 1;
4713 }
4714
4715 /* The target could report the data address. Mark watchpoints
4716 affected by this data address as triggered, and all others as not
4717 triggered. */
4718
4719 ALL_BREAKPOINTS (b)
4720 if (is_hardware_watchpoint (b))
4721 {
4722 struct watchpoint *w = (struct watchpoint *) b;
4723 struct bp_location *loc;
4724
4725 w->watchpoint_triggered = watch_triggered_no;
4726 for (loc = b->loc; loc; loc = loc->next)
4727 {
4728 if (is_masked_watchpoint (b))
4729 {
4730 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4731 CORE_ADDR start = loc->address & w->hw_wp_mask;
4732
4733 if (newaddr == start)
4734 {
4735 w->watchpoint_triggered = watch_triggered_yes;
4736 break;
4737 }
4738 }
4739 /* Exact match not required. Within range is sufficient. */
4740 else if (target_watchpoint_addr_within_range (current_top_target (),
4741 addr, loc->address,
4742 loc->length))
4743 {
4744 w->watchpoint_triggered = watch_triggered_yes;
4745 break;
4746 }
4747 }
4748 }
4749
4750 return 1;
4751 }
4752
4753 /* Possible return values for watchpoint_check. */
4754 enum wp_check_result
4755 {
4756 /* The watchpoint has been deleted. */
4757 WP_DELETED = 1,
4758
4759 /* The value has changed. */
4760 WP_VALUE_CHANGED = 2,
4761
4762 /* The value has not changed. */
4763 WP_VALUE_NOT_CHANGED = 3,
4764
4765 /* Ignore this watchpoint, no matter if the value changed or not. */
4766 WP_IGNORE = 4,
4767 };
4768
4769 #define BP_TEMPFLAG 1
4770 #define BP_HARDWAREFLAG 2
4771
4772 /* Evaluate watchpoint condition expression and check if its value
4773 changed. */
4774
4775 static wp_check_result
4776 watchpoint_check (bpstat bs)
4777 {
4778 struct watchpoint *b;
4779 struct frame_info *fr;
4780 int within_current_scope;
4781
4782 /* BS is built from an existing struct breakpoint. */
4783 gdb_assert (bs->breakpoint_at != NULL);
4784 b = (struct watchpoint *) bs->breakpoint_at;
4785
4786 /* If this is a local watchpoint, we only want to check if the
4787 watchpoint frame is in scope if the current thread is the thread
4788 that was used to create the watchpoint. */
4789 if (!watchpoint_in_thread_scope (b))
4790 return WP_IGNORE;
4791
4792 if (b->exp_valid_block == NULL)
4793 within_current_scope = 1;
4794 else
4795 {
4796 struct frame_info *frame = get_current_frame ();
4797 struct gdbarch *frame_arch = get_frame_arch (frame);
4798 CORE_ADDR frame_pc = get_frame_pc (frame);
4799
4800 /* stack_frame_destroyed_p() returns a non-zero value if we're
4801 still in the function but the stack frame has already been
4802 invalidated. Since we can't rely on the values of local
4803 variables after the stack has been destroyed, we are treating
4804 the watchpoint in that state as `not changed' without further
4805 checking. Don't mark watchpoints as changed if the current
4806 frame is in an epilogue - even if they are in some other
4807 frame, our view of the stack is likely to be wrong and
4808 frame_find_by_id could error out. */
4809 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
4810 return WP_IGNORE;
4811
4812 fr = frame_find_by_id (b->watchpoint_frame);
4813 within_current_scope = (fr != NULL);
4814
4815 /* If we've gotten confused in the unwinder, we might have
4816 returned a frame that can't describe this variable. */
4817 if (within_current_scope)
4818 {
4819 struct symbol *function;
4820
4821 function = get_frame_function (fr);
4822 if (function == NULL
4823 || !contained_in (b->exp_valid_block,
4824 SYMBOL_BLOCK_VALUE (function)))
4825 within_current_scope = 0;
4826 }
4827
4828 if (within_current_scope)
4829 /* If we end up stopping, the current frame will get selected
4830 in normal_stop. So this call to select_frame won't affect
4831 the user. */
4832 select_frame (fr);
4833 }
4834
4835 if (within_current_scope)
4836 {
4837 /* We use value_{,free_to_}mark because it could be a *long*
4838 time before we return to the command level and call
4839 free_all_values. We can't call free_all_values because we
4840 might be in the middle of evaluating a function call. */
4841
4842 int pc = 0;
4843 struct value *mark;
4844 struct value *new_val;
4845
4846 if (is_masked_watchpoint (b))
4847 /* Since we don't know the exact trigger address (from
4848 stopped_data_address), just tell the user we've triggered
4849 a mask watchpoint. */
4850 return WP_VALUE_CHANGED;
4851
4852 mark = value_mark ();
4853 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
4854
4855 if (b->val_bitsize != 0)
4856 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4857
4858 /* We use value_equal_contents instead of value_equal because
4859 the latter coerces an array to a pointer, thus comparing just
4860 the address of the array instead of its contents. This is
4861 not what we want. */
4862 if ((b->val != NULL) != (new_val != NULL)
4863 || (b->val != NULL && !value_equal_contents (b->val.get (),
4864 new_val)))
4865 {
4866 bs->old_val = b->val;
4867 b->val = release_value (new_val);
4868 b->val_valid = true;
4869 if (new_val != NULL)
4870 value_free_to_mark (mark);
4871 return WP_VALUE_CHANGED;
4872 }
4873 else
4874 {
4875 /* Nothing changed. */
4876 value_free_to_mark (mark);
4877 return WP_VALUE_NOT_CHANGED;
4878 }
4879 }
4880 else
4881 {
4882 /* This seems like the only logical thing to do because
4883 if we temporarily ignored the watchpoint, then when
4884 we reenter the block in which it is valid it contains
4885 garbage (in the case of a function, it may have two
4886 garbage values, one before and one after the prologue).
4887 So we can't even detect the first assignment to it and
4888 watch after that (since the garbage may or may not equal
4889 the first value assigned). */
4890 /* We print all the stop information in
4891 breakpoint_ops->print_it, but in this case, by the time we
4892 call breakpoint_ops->print_it this bp will be deleted
4893 already. So we have no choice but print the information
4894 here. */
4895
4896 SWITCH_THRU_ALL_UIS ()
4897 {
4898 struct ui_out *uiout = current_uiout;
4899
4900 if (uiout->is_mi_like_p ())
4901 uiout->field_string
4902 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
4903 uiout->message ("\nWatchpoint %pF deleted because the program has "
4904 "left the block in\n"
4905 "which its expression is valid.\n",
4906 signed_field ("wpnum", b->number));
4907 }
4908
4909 /* Make sure the watchpoint's commands aren't executed. */
4910 b->commands = NULL;
4911 watchpoint_del_at_next_stop (b);
4912
4913 return WP_DELETED;
4914 }
4915 }
4916
4917 /* Return true if it looks like target has stopped due to hitting
4918 breakpoint location BL. This function does not check if we should
4919 stop, only if BL explains the stop. */
4920
4921 static int
4922 bpstat_check_location (const struct bp_location *bl,
4923 const address_space *aspace, CORE_ADDR bp_addr,
4924 const struct target_waitstatus *ws)
4925 {
4926 struct breakpoint *b = bl->owner;
4927
4928 /* BL is from an existing breakpoint. */
4929 gdb_assert (b != NULL);
4930
4931 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
4932 }
4933
4934 /* Determine if the watched values have actually changed, and we
4935 should stop. If not, set BS->stop to 0. */
4936
4937 static void
4938 bpstat_check_watchpoint (bpstat bs)
4939 {
4940 const struct bp_location *bl;
4941 struct watchpoint *b;
4942
4943 /* BS is built for existing struct breakpoint. */
4944 bl = bs->bp_location_at;
4945 gdb_assert (bl != NULL);
4946 b = (struct watchpoint *) bs->breakpoint_at;
4947 gdb_assert (b != NULL);
4948
4949 {
4950 int must_check_value = 0;
4951
4952 if (b->type == bp_watchpoint)
4953 /* For a software watchpoint, we must always check the
4954 watched value. */
4955 must_check_value = 1;
4956 else if (b->watchpoint_triggered == watch_triggered_yes)
4957 /* We have a hardware watchpoint (read, write, or access)
4958 and the target earlier reported an address watched by
4959 this watchpoint. */
4960 must_check_value = 1;
4961 else if (b->watchpoint_triggered == watch_triggered_unknown
4962 && b->type == bp_hardware_watchpoint)
4963 /* We were stopped by a hardware watchpoint, but the target could
4964 not report the data address. We must check the watchpoint's
4965 value. Access and read watchpoints are out of luck; without
4966 a data address, we can't figure it out. */
4967 must_check_value = 1;
4968
4969 if (must_check_value)
4970 {
4971 wp_check_result e;
4972
4973 try
4974 {
4975 e = watchpoint_check (bs);
4976 }
4977 catch (const gdb_exception &ex)
4978 {
4979 exception_fprintf (gdb_stderr, ex,
4980 "Error evaluating expression "
4981 "for watchpoint %d\n",
4982 b->number);
4983
4984 SWITCH_THRU_ALL_UIS ()
4985 {
4986 printf_filtered (_("Watchpoint %d deleted.\n"),
4987 b->number);
4988 }
4989 watchpoint_del_at_next_stop (b);
4990 e = WP_DELETED;
4991 }
4992
4993 switch (e)
4994 {
4995 case WP_DELETED:
4996 /* We've already printed what needs to be printed. */
4997 bs->print_it = print_it_done;
4998 /* Stop. */
4999 break;
5000 case WP_IGNORE:
5001 bs->print_it = print_it_noop;
5002 bs->stop = 0;
5003 break;
5004 case WP_VALUE_CHANGED:
5005 if (b->type == bp_read_watchpoint)
5006 {
5007 /* There are two cases to consider here:
5008
5009 1. We're watching the triggered memory for reads.
5010 In that case, trust the target, and always report
5011 the watchpoint hit to the user. Even though
5012 reads don't cause value changes, the value may
5013 have changed since the last time it was read, and
5014 since we're not trapping writes, we will not see
5015 those, and as such we should ignore our notion of
5016 old value.
5017
5018 2. We're watching the triggered memory for both
5019 reads and writes. There are two ways this may
5020 happen:
5021
5022 2.1. This is a target that can't break on data
5023 reads only, but can break on accesses (reads or
5024 writes), such as e.g., x86. We detect this case
5025 at the time we try to insert read watchpoints.
5026
5027 2.2. Otherwise, the target supports read
5028 watchpoints, but, the user set an access or write
5029 watchpoint watching the same memory as this read
5030 watchpoint.
5031
5032 If we're watching memory writes as well as reads,
5033 ignore watchpoint hits when we find that the
5034 value hasn't changed, as reads don't cause
5035 changes. This still gives false positives when
5036 the program writes the same value to memory as
5037 what there was already in memory (we will confuse
5038 it for a read), but it's much better than
5039 nothing. */
5040
5041 int other_write_watchpoint = 0;
5042
5043 if (bl->watchpoint_type == hw_read)
5044 {
5045 struct breakpoint *other_b;
5046
5047 ALL_BREAKPOINTS (other_b)
5048 if (other_b->type == bp_hardware_watchpoint
5049 || other_b->type == bp_access_watchpoint)
5050 {
5051 struct watchpoint *other_w =
5052 (struct watchpoint *) other_b;
5053
5054 if (other_w->watchpoint_triggered
5055 == watch_triggered_yes)
5056 {
5057 other_write_watchpoint = 1;
5058 break;
5059 }
5060 }
5061 }
5062
5063 if (other_write_watchpoint
5064 || bl->watchpoint_type == hw_access)
5065 {
5066 /* We're watching the same memory for writes,
5067 and the value changed since the last time we
5068 updated it, so this trap must be for a write.
5069 Ignore it. */
5070 bs->print_it = print_it_noop;
5071 bs->stop = 0;
5072 }
5073 }
5074 break;
5075 case WP_VALUE_NOT_CHANGED:
5076 if (b->type == bp_hardware_watchpoint
5077 || b->type == bp_watchpoint)
5078 {
5079 /* Don't stop: write watchpoints shouldn't fire if
5080 the value hasn't changed. */
5081 bs->print_it = print_it_noop;
5082 bs->stop = 0;
5083 }
5084 /* Stop. */
5085 break;
5086 default:
5087 /* Can't happen. */
5088 break;
5089 }
5090 }
5091 else /* must_check_value == 0 */
5092 {
5093 /* This is a case where some watchpoint(s) triggered, but
5094 not at the address of this watchpoint, or else no
5095 watchpoint triggered after all. So don't print
5096 anything for this watchpoint. */
5097 bs->print_it = print_it_noop;
5098 bs->stop = 0;
5099 }
5100 }
5101 }
5102
5103 /* For breakpoints that are currently marked as telling gdb to stop,
5104 check conditions (condition proper, frame, thread and ignore count)
5105 of breakpoint referred to by BS. If we should not stop for this
5106 breakpoint, set BS->stop to 0. */
5107
5108 static void
5109 bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
5110 {
5111 const struct bp_location *bl;
5112 struct breakpoint *b;
5113 /* Assume stop. */
5114 bool condition_result = true;
5115 struct expression *cond;
5116
5117 gdb_assert (bs->stop);
5118
5119 /* BS is built for existing struct breakpoint. */
5120 bl = bs->bp_location_at;
5121 gdb_assert (bl != NULL);
5122 b = bs->breakpoint_at;
5123 gdb_assert (b != NULL);
5124
5125 /* Even if the target evaluated the condition on its end and notified GDB, we
5126 need to do so again since GDB does not know if we stopped due to a
5127 breakpoint or a single step breakpoint. */
5128
5129 if (frame_id_p (b->frame_id)
5130 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5131 {
5132 bs->stop = 0;
5133 return;
5134 }
5135
5136 /* If this is a thread/task-specific breakpoint, don't waste cpu
5137 evaluating the condition if this isn't the specified
5138 thread/task. */
5139 if ((b->thread != -1 && b->thread != thread->global_num)
5140 || (b->task != 0 && b->task != ada_get_task_number (thread)))
5141 {
5142 bs->stop = 0;
5143 return;
5144 }
5145
5146 /* Evaluate extension language breakpoints that have a "stop" method
5147 implemented. */
5148 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5149
5150 if (is_watchpoint (b))
5151 {
5152 struct watchpoint *w = (struct watchpoint *) b;
5153
5154 cond = w->cond_exp.get ();
5155 }
5156 else
5157 cond = bl->cond.get ();
5158
5159 if (cond && b->disposition != disp_del_at_next_stop)
5160 {
5161 int within_current_scope = 1;
5162 struct watchpoint * w;
5163
5164 /* We use value_mark and value_free_to_mark because it could
5165 be a long time before we return to the command level and
5166 call free_all_values. We can't call free_all_values
5167 because we might be in the middle of evaluating a
5168 function call. */
5169 struct value *mark = value_mark ();
5170
5171 if (is_watchpoint (b))
5172 w = (struct watchpoint *) b;
5173 else
5174 w = NULL;
5175
5176 /* Need to select the frame, with all that implies so that
5177 the conditions will have the right context. Because we
5178 use the frame, we will not see an inlined function's
5179 variables when we arrive at a breakpoint at the start
5180 of the inlined function; the current frame will be the
5181 call site. */
5182 if (w == NULL || w->cond_exp_valid_block == NULL)
5183 select_frame (get_current_frame ());
5184 else
5185 {
5186 struct frame_info *frame;
5187
5188 /* For local watchpoint expressions, which particular
5189 instance of a local is being watched matters, so we
5190 keep track of the frame to evaluate the expression
5191 in. To evaluate the condition however, it doesn't
5192 really matter which instantiation of the function
5193 where the condition makes sense triggers the
5194 watchpoint. This allows an expression like "watch
5195 global if q > 10" set in `func', catch writes to
5196 global on all threads that call `func', or catch
5197 writes on all recursive calls of `func' by a single
5198 thread. We simply always evaluate the condition in
5199 the innermost frame that's executing where it makes
5200 sense to evaluate the condition. It seems
5201 intuitive. */
5202 frame = block_innermost_frame (w->cond_exp_valid_block);
5203 if (frame != NULL)
5204 select_frame (frame);
5205 else
5206 within_current_scope = 0;
5207 }
5208 if (within_current_scope)
5209 {
5210 try
5211 {
5212 condition_result = breakpoint_cond_eval (cond);
5213 }
5214 catch (const gdb_exception &ex)
5215 {
5216 exception_fprintf (gdb_stderr, ex,
5217 "Error in testing breakpoint condition:\n");
5218 }
5219 }
5220 else
5221 {
5222 warning (_("Watchpoint condition cannot be tested "
5223 "in the current scope"));
5224 /* If we failed to set the right context for this
5225 watchpoint, unconditionally report it. */
5226 }
5227 /* FIXME-someday, should give breakpoint #. */
5228 value_free_to_mark (mark);
5229 }
5230
5231 if (cond && !condition_result)
5232 {
5233 bs->stop = 0;
5234 }
5235 else if (b->ignore_count > 0)
5236 {
5237 b->ignore_count--;
5238 bs->stop = 0;
5239 /* Increase the hit count even though we don't stop. */
5240 ++(b->hit_count);
5241 gdb::observers::breakpoint_modified.notify (b);
5242 }
5243 }
5244
5245 /* Returns true if we need to track moribund locations of LOC's type
5246 on the current target. */
5247
5248 static int
5249 need_moribund_for_location_type (struct bp_location *loc)
5250 {
5251 return ((loc->loc_type == bp_loc_software_breakpoint
5252 && !target_supports_stopped_by_sw_breakpoint ())
5253 || (loc->loc_type == bp_loc_hardware_breakpoint
5254 && !target_supports_stopped_by_hw_breakpoint ()));
5255 }
5256
5257 /* See breakpoint.h. */
5258
5259 bpstat
5260 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5261 const struct target_waitstatus *ws)
5262 {
5263 struct breakpoint *b;
5264 bpstat bs_head = NULL, *bs_link = &bs_head;
5265
5266 ALL_BREAKPOINTS (b)
5267 {
5268 if (!breakpoint_enabled (b))
5269 continue;
5270
5271 for (bp_location *bl = b->loc; bl != NULL; bl = bl->next)
5272 {
5273 /* For hardware watchpoints, we look only at the first
5274 location. The watchpoint_check function will work on the
5275 entire expression, not the individual locations. For
5276 read watchpoints, the watchpoints_triggered function has
5277 checked all locations already. */
5278 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5279 break;
5280
5281 if (!bl->enabled || bl->shlib_disabled)
5282 continue;
5283
5284 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5285 continue;
5286
5287 /* Come here if it's a watchpoint, or if the break address
5288 matches. */
5289
5290 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5291 explain stop. */
5292
5293 /* Assume we stop. Should we find a watchpoint that is not
5294 actually triggered, or if the condition of the breakpoint
5295 evaluates as false, we'll reset 'stop' to 0. */
5296 bs->stop = 1;
5297 bs->print = 1;
5298
5299 /* If this is a scope breakpoint, mark the associated
5300 watchpoint as triggered so that we will handle the
5301 out-of-scope event. We'll get to the watchpoint next
5302 iteration. */
5303 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5304 {
5305 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5306
5307 w->watchpoint_triggered = watch_triggered_yes;
5308 }
5309 }
5310 }
5311
5312 /* Check if a moribund breakpoint explains the stop. */
5313 if (!target_supports_stopped_by_sw_breakpoint ()
5314 || !target_supports_stopped_by_hw_breakpoint ())
5315 {
5316 for (bp_location *loc : moribund_locations)
5317 {
5318 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5319 && need_moribund_for_location_type (loc))
5320 {
5321 bpstat bs = new bpstats (loc, &bs_link);
5322 /* For hits of moribund locations, we should just proceed. */
5323 bs->stop = 0;
5324 bs->print = 0;
5325 bs->print_it = print_it_noop;
5326 }
5327 }
5328 }
5329
5330 return bs_head;
5331 }
5332
5333 /* See breakpoint.h. */
5334
5335 bpstat
5336 bpstat_stop_status (const address_space *aspace,
5337 CORE_ADDR bp_addr, thread_info *thread,
5338 const struct target_waitstatus *ws,
5339 bpstat stop_chain)
5340 {
5341 struct breakpoint *b = NULL;
5342 /* First item of allocated bpstat's. */
5343 bpstat bs_head = stop_chain;
5344 bpstat bs;
5345 int need_remove_insert;
5346 int removed_any;
5347
5348 /* First, build the bpstat chain with locations that explain a
5349 target stop, while being careful to not set the target running,
5350 as that may invalidate locations (in particular watchpoint
5351 locations are recreated). Resuming will happen here with
5352 breakpoint conditions or watchpoint expressions that include
5353 inferior function calls. */
5354 if (bs_head == NULL)
5355 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5356
5357 /* A bit of special processing for shlib breakpoints. We need to
5358 process solib loading here, so that the lists of loaded and
5359 unloaded libraries are correct before we handle "catch load" and
5360 "catch unload". */
5361 for (bs = bs_head; bs != NULL; bs = bs->next)
5362 {
5363 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5364 {
5365 handle_solib_event ();
5366 break;
5367 }
5368 }
5369
5370 /* Now go through the locations that caused the target to stop, and
5371 check whether we're interested in reporting this stop to higher
5372 layers, or whether we should resume the target transparently. */
5373
5374 removed_any = 0;
5375
5376 for (bs = bs_head; bs != NULL; bs = bs->next)
5377 {
5378 if (!bs->stop)
5379 continue;
5380
5381 b = bs->breakpoint_at;
5382 b->ops->check_status (bs);
5383 if (bs->stop)
5384 {
5385 bpstat_check_breakpoint_conditions (bs, thread);
5386
5387 if (bs->stop)
5388 {
5389 ++(b->hit_count);
5390 gdb::observers::breakpoint_modified.notify (b);
5391
5392 /* We will stop here. */
5393 if (b->disposition == disp_disable)
5394 {
5395 --(b->enable_count);
5396 if (b->enable_count <= 0)
5397 b->enable_state = bp_disabled;
5398 removed_any = 1;
5399 }
5400 if (b->silent)
5401 bs->print = 0;
5402 bs->commands = b->commands;
5403 if (command_line_is_silent (bs->commands
5404 ? bs->commands.get () : NULL))
5405 bs->print = 0;
5406
5407 b->ops->after_condition_true (bs);
5408 }
5409
5410 }
5411
5412 /* Print nothing for this entry if we don't stop or don't
5413 print. */
5414 if (!bs->stop || !bs->print)
5415 bs->print_it = print_it_noop;
5416 }
5417
5418 /* If we aren't stopping, the value of some hardware watchpoint may
5419 not have changed, but the intermediate memory locations we are
5420 watching may have. Don't bother if we're stopping; this will get
5421 done later. */
5422 need_remove_insert = 0;
5423 if (! bpstat_causes_stop (bs_head))
5424 for (bs = bs_head; bs != NULL; bs = bs->next)
5425 if (!bs->stop
5426 && bs->breakpoint_at
5427 && is_hardware_watchpoint (bs->breakpoint_at))
5428 {
5429 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5430
5431 update_watchpoint (w, 0 /* don't reparse. */);
5432 need_remove_insert = 1;
5433 }
5434
5435 if (need_remove_insert)
5436 update_global_location_list (UGLL_MAY_INSERT);
5437 else if (removed_any)
5438 update_global_location_list (UGLL_DONT_INSERT);
5439
5440 return bs_head;
5441 }
5442
5443 static void
5444 handle_jit_event (CORE_ADDR address)
5445 {
5446 struct gdbarch *gdbarch;
5447
5448 infrun_debug_printf ("handling bp_jit_event");
5449
5450 /* Switch terminal for any messages produced by
5451 breakpoint_re_set. */
5452 target_terminal::ours_for_output ();
5453
5454 gdbarch = get_frame_arch (get_current_frame ());
5455 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5456 thus it is expected that its objectfile can be found through
5457 minimal symbol lookup. If it doesn't work (and assert fails), it
5458 most likely means that `jit_breakpoint_re_set` was changes and this
5459 function needs to be updated too. */
5460 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5461 gdb_assert (jit_bp_sym.objfile != nullptr);
5462 jit_event_handler (gdbarch, jit_bp_sym.objfile);
5463
5464 target_terminal::inferior ();
5465 }
5466
5467 /* Prepare WHAT final decision for infrun. */
5468
5469 /* Decide what infrun needs to do with this bpstat. */
5470
5471 struct bpstat_what
5472 bpstat_what (bpstat bs_head)
5473 {
5474 struct bpstat_what retval;
5475 bpstat bs;
5476
5477 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5478 retval.call_dummy = STOP_NONE;
5479 retval.is_longjmp = false;
5480
5481 for (bs = bs_head; bs != NULL; bs = bs->next)
5482 {
5483 /* Extract this BS's action. After processing each BS, we check
5484 if its action overrides all we've seem so far. */
5485 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5486 enum bptype bptype;
5487
5488 if (bs->breakpoint_at == NULL)
5489 {
5490 /* I suspect this can happen if it was a momentary
5491 breakpoint which has since been deleted. */
5492 bptype = bp_none;
5493 }
5494 else
5495 bptype = bs->breakpoint_at->type;
5496
5497 switch (bptype)
5498 {
5499 case bp_none:
5500 break;
5501 case bp_breakpoint:
5502 case bp_hardware_breakpoint:
5503 case bp_single_step:
5504 case bp_until:
5505 case bp_finish:
5506 case bp_shlib_event:
5507 if (bs->stop)
5508 {
5509 if (bs->print)
5510 this_action = BPSTAT_WHAT_STOP_NOISY;
5511 else
5512 this_action = BPSTAT_WHAT_STOP_SILENT;
5513 }
5514 else
5515 this_action = BPSTAT_WHAT_SINGLE;
5516 break;
5517 case bp_watchpoint:
5518 case bp_hardware_watchpoint:
5519 case bp_read_watchpoint:
5520 case bp_access_watchpoint:
5521 if (bs->stop)
5522 {
5523 if (bs->print)
5524 this_action = BPSTAT_WHAT_STOP_NOISY;
5525 else
5526 this_action = BPSTAT_WHAT_STOP_SILENT;
5527 }
5528 else
5529 {
5530 /* There was a watchpoint, but we're not stopping.
5531 This requires no further action. */
5532 }
5533 break;
5534 case bp_longjmp:
5535 case bp_longjmp_call_dummy:
5536 case bp_exception:
5537 if (bs->stop)
5538 {
5539 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5540 retval.is_longjmp = bptype != bp_exception;
5541 }
5542 else
5543 this_action = BPSTAT_WHAT_SINGLE;
5544 break;
5545 case bp_longjmp_resume:
5546 case bp_exception_resume:
5547 if (bs->stop)
5548 {
5549 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5550 retval.is_longjmp = bptype == bp_longjmp_resume;
5551 }
5552 else
5553 this_action = BPSTAT_WHAT_SINGLE;
5554 break;
5555 case bp_step_resume:
5556 if (bs->stop)
5557 this_action = BPSTAT_WHAT_STEP_RESUME;
5558 else
5559 {
5560 /* It is for the wrong frame. */
5561 this_action = BPSTAT_WHAT_SINGLE;
5562 }
5563 break;
5564 case bp_hp_step_resume:
5565 if (bs->stop)
5566 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5567 else
5568 {
5569 /* It is for the wrong frame. */
5570 this_action = BPSTAT_WHAT_SINGLE;
5571 }
5572 break;
5573 case bp_watchpoint_scope:
5574 case bp_thread_event:
5575 case bp_overlay_event:
5576 case bp_longjmp_master:
5577 case bp_std_terminate_master:
5578 case bp_exception_master:
5579 this_action = BPSTAT_WHAT_SINGLE;
5580 break;
5581 case bp_catchpoint:
5582 if (bs->stop)
5583 {
5584 if (bs->print)
5585 this_action = BPSTAT_WHAT_STOP_NOISY;
5586 else
5587 this_action = BPSTAT_WHAT_STOP_SILENT;
5588 }
5589 else
5590 {
5591 /* Some catchpoints are implemented with breakpoints.
5592 For those, we need to step over the breakpoint. */
5593 if (bs->bp_location_at->loc_type != bp_loc_other)
5594 this_action = BPSTAT_WHAT_SINGLE;
5595 }
5596 break;
5597 case bp_jit_event:
5598 this_action = BPSTAT_WHAT_SINGLE;
5599 break;
5600 case bp_call_dummy:
5601 /* Make sure the action is stop (silent or noisy),
5602 so infrun.c pops the dummy frame. */
5603 retval.call_dummy = STOP_STACK_DUMMY;
5604 this_action = BPSTAT_WHAT_STOP_SILENT;
5605 break;
5606 case bp_std_terminate:
5607 /* Make sure the action is stop (silent or noisy),
5608 so infrun.c pops the dummy frame. */
5609 retval.call_dummy = STOP_STD_TERMINATE;
5610 this_action = BPSTAT_WHAT_STOP_SILENT;
5611 break;
5612 case bp_tracepoint:
5613 case bp_fast_tracepoint:
5614 case bp_static_tracepoint:
5615 /* Tracepoint hits should not be reported back to GDB, and
5616 if one got through somehow, it should have been filtered
5617 out already. */
5618 internal_error (__FILE__, __LINE__,
5619 _("bpstat_what: tracepoint encountered"));
5620 break;
5621 case bp_gnu_ifunc_resolver:
5622 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5623 this_action = BPSTAT_WHAT_SINGLE;
5624 break;
5625 case bp_gnu_ifunc_resolver_return:
5626 /* The breakpoint will be removed, execution will restart from the
5627 PC of the former breakpoint. */
5628 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5629 break;
5630
5631 case bp_dprintf:
5632 if (bs->stop)
5633 this_action = BPSTAT_WHAT_STOP_SILENT;
5634 else
5635 this_action = BPSTAT_WHAT_SINGLE;
5636 break;
5637
5638 default:
5639 internal_error (__FILE__, __LINE__,
5640 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5641 }
5642
5643 retval.main_action = std::max (retval.main_action, this_action);
5644 }
5645
5646 return retval;
5647 }
5648
5649 void
5650 bpstat_run_callbacks (bpstat bs_head)
5651 {
5652 bpstat bs;
5653
5654 for (bs = bs_head; bs != NULL; bs = bs->next)
5655 {
5656 struct breakpoint *b = bs->breakpoint_at;
5657
5658 if (b == NULL)
5659 continue;
5660 switch (b->type)
5661 {
5662 case bp_jit_event:
5663 handle_jit_event (bs->bp_location_at->address);
5664 break;
5665 case bp_gnu_ifunc_resolver:
5666 gnu_ifunc_resolver_stop (b);
5667 break;
5668 case bp_gnu_ifunc_resolver_return:
5669 gnu_ifunc_resolver_return_stop (b);
5670 break;
5671 }
5672 }
5673 }
5674
5675 /* See breakpoint.h. */
5676
5677 bool
5678 bpstat_should_step ()
5679 {
5680 struct breakpoint *b;
5681
5682 ALL_BREAKPOINTS (b)
5683 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5684 return true;
5685 return false;
5686 }
5687
5688 /* See breakpoint.h. */
5689
5690 bool
5691 bpstat_causes_stop (bpstat bs)
5692 {
5693 for (; bs != NULL; bs = bs->next)
5694 if (bs->stop)
5695 return true;
5696
5697 return false;
5698 }
5699
5700 \f
5701
5702 /* Compute a string of spaces suitable to indent the next line
5703 so it starts at the position corresponding to the table column
5704 named COL_NAME in the currently active table of UIOUT. */
5705
5706 static char *
5707 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5708 {
5709 static char wrap_indent[80];
5710 int i, total_width, width, align;
5711 const char *text;
5712
5713 total_width = 0;
5714 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5715 {
5716 if (strcmp (text, col_name) == 0)
5717 {
5718 gdb_assert (total_width < sizeof wrap_indent);
5719 memset (wrap_indent, ' ', total_width);
5720 wrap_indent[total_width] = 0;
5721
5722 return wrap_indent;
5723 }
5724
5725 total_width += width + 1;
5726 }
5727
5728 return NULL;
5729 }
5730
5731 /* Determine if the locations of this breakpoint will have their conditions
5732 evaluated by the target, host or a mix of both. Returns the following:
5733
5734 "host": Host evals condition.
5735 "host or target": Host or Target evals condition.
5736 "target": Target evals condition.
5737 */
5738
5739 static const char *
5740 bp_condition_evaluator (struct breakpoint *b)
5741 {
5742 struct bp_location *bl;
5743 char host_evals = 0;
5744 char target_evals = 0;
5745
5746 if (!b)
5747 return NULL;
5748
5749 if (!is_breakpoint (b))
5750 return NULL;
5751
5752 if (gdb_evaluates_breakpoint_condition_p ()
5753 || !target_supports_evaluation_of_breakpoint_conditions ())
5754 return condition_evaluation_host;
5755
5756 for (bl = b->loc; bl; bl = bl->next)
5757 {
5758 if (bl->cond_bytecode)
5759 target_evals++;
5760 else
5761 host_evals++;
5762 }
5763
5764 if (host_evals && target_evals)
5765 return condition_evaluation_both;
5766 else if (target_evals)
5767 return condition_evaluation_target;
5768 else
5769 return condition_evaluation_host;
5770 }
5771
5772 /* Determine the breakpoint location's condition evaluator. This is
5773 similar to bp_condition_evaluator, but for locations. */
5774
5775 static const char *
5776 bp_location_condition_evaluator (struct bp_location *bl)
5777 {
5778 if (bl && !is_breakpoint (bl->owner))
5779 return NULL;
5780
5781 if (gdb_evaluates_breakpoint_condition_p ()
5782 || !target_supports_evaluation_of_breakpoint_conditions ())
5783 return condition_evaluation_host;
5784
5785 if (bl && bl->cond_bytecode)
5786 return condition_evaluation_target;
5787 else
5788 return condition_evaluation_host;
5789 }
5790
5791 /* Print the LOC location out of the list of B->LOC locations. */
5792
5793 static void
5794 print_breakpoint_location (struct breakpoint *b,
5795 struct bp_location *loc)
5796 {
5797 struct ui_out *uiout = current_uiout;
5798
5799 scoped_restore_current_program_space restore_pspace;
5800
5801 if (loc != NULL && loc->shlib_disabled)
5802 loc = NULL;
5803
5804 if (loc != NULL)
5805 set_current_program_space (loc->pspace);
5806
5807 if (b->display_canonical)
5808 uiout->field_string ("what", event_location_to_string (b->location.get ()));
5809 else if (loc && loc->symtab)
5810 {
5811 const struct symbol *sym = loc->symbol;
5812
5813 if (sym)
5814 {
5815 uiout->text ("in ");
5816 uiout->field_string ("func", sym->print_name (),
5817 function_name_style.style ());
5818 uiout->text (" ");
5819 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5820 uiout->text ("at ");
5821 }
5822 uiout->field_string ("file",
5823 symtab_to_filename_for_display (loc->symtab),
5824 file_name_style.style ());
5825 uiout->text (":");
5826
5827 if (uiout->is_mi_like_p ())
5828 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
5829
5830 uiout->field_signed ("line", loc->line_number);
5831 }
5832 else if (loc)
5833 {
5834 string_file stb;
5835
5836 print_address_symbolic (loc->gdbarch, loc->address, &stb,
5837 demangle, "");
5838 uiout->field_stream ("at", stb);
5839 }
5840 else
5841 {
5842 uiout->field_string ("pending",
5843 event_location_to_string (b->location.get ()));
5844 /* If extra_string is available, it could be holding a condition
5845 or dprintf arguments. In either case, make sure it is printed,
5846 too, but only for non-MI streams. */
5847 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
5848 {
5849 if (b->type == bp_dprintf)
5850 uiout->text (",");
5851 else
5852 uiout->text (" ");
5853 uiout->text (b->extra_string);
5854 }
5855 }
5856
5857 if (loc && is_breakpoint (b)
5858 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5859 && bp_condition_evaluator (b) == condition_evaluation_both)
5860 {
5861 uiout->text (" (");
5862 uiout->field_string ("evaluated-by",
5863 bp_location_condition_evaluator (loc));
5864 uiout->text (")");
5865 }
5866 }
5867
5868 static const char *
5869 bptype_string (enum bptype type)
5870 {
5871 struct ep_type_description
5872 {
5873 enum bptype type;
5874 const char *description;
5875 };
5876 static struct ep_type_description bptypes[] =
5877 {
5878 {bp_none, "?deleted?"},
5879 {bp_breakpoint, "breakpoint"},
5880 {bp_hardware_breakpoint, "hw breakpoint"},
5881 {bp_single_step, "sw single-step"},
5882 {bp_until, "until"},
5883 {bp_finish, "finish"},
5884 {bp_watchpoint, "watchpoint"},
5885 {bp_hardware_watchpoint, "hw watchpoint"},
5886 {bp_read_watchpoint, "read watchpoint"},
5887 {bp_access_watchpoint, "acc watchpoint"},
5888 {bp_longjmp, "longjmp"},
5889 {bp_longjmp_resume, "longjmp resume"},
5890 {bp_longjmp_call_dummy, "longjmp for call dummy"},
5891 {bp_exception, "exception"},
5892 {bp_exception_resume, "exception resume"},
5893 {bp_step_resume, "step resume"},
5894 {bp_hp_step_resume, "high-priority step resume"},
5895 {bp_watchpoint_scope, "watchpoint scope"},
5896 {bp_call_dummy, "call dummy"},
5897 {bp_std_terminate, "std::terminate"},
5898 {bp_shlib_event, "shlib events"},
5899 {bp_thread_event, "thread events"},
5900 {bp_overlay_event, "overlay events"},
5901 {bp_longjmp_master, "longjmp master"},
5902 {bp_std_terminate_master, "std::terminate master"},
5903 {bp_exception_master, "exception master"},
5904 {bp_catchpoint, "catchpoint"},
5905 {bp_tracepoint, "tracepoint"},
5906 {bp_fast_tracepoint, "fast tracepoint"},
5907 {bp_static_tracepoint, "static tracepoint"},
5908 {bp_dprintf, "dprintf"},
5909 {bp_jit_event, "jit events"},
5910 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5911 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
5912 };
5913
5914 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5915 || ((int) type != bptypes[(int) type].type))
5916 internal_error (__FILE__, __LINE__,
5917 _("bptypes table does not describe type #%d."),
5918 (int) type);
5919
5920 return bptypes[(int) type].description;
5921 }
5922
5923 /* For MI, output a field named 'thread-groups' with a list as the value.
5924 For CLI, prefix the list with the string 'inf'. */
5925
5926 static void
5927 output_thread_groups (struct ui_out *uiout,
5928 const char *field_name,
5929 const std::vector<int> &inf_nums,
5930 int mi_only)
5931 {
5932 int is_mi = uiout->is_mi_like_p ();
5933
5934 /* For backward compatibility, don't display inferiors in CLI unless
5935 there are several. Always display them for MI. */
5936 if (!is_mi && mi_only)
5937 return;
5938
5939 ui_out_emit_list list_emitter (uiout, field_name);
5940
5941 for (size_t i = 0; i < inf_nums.size (); i++)
5942 {
5943 if (is_mi)
5944 {
5945 char mi_group[10];
5946
5947 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
5948 uiout->field_string (NULL, mi_group);
5949 }
5950 else
5951 {
5952 if (i == 0)
5953 uiout->text (" inf ");
5954 else
5955 uiout->text (", ");
5956
5957 uiout->text (plongest (inf_nums[i]));
5958 }
5959 }
5960 }
5961
5962 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
5963 instead of going via breakpoint_ops::print_one. This makes "maint
5964 info breakpoints" show the software breakpoint locations of
5965 catchpoints, which are considered internal implementation
5966 detail. */
5967
5968 static void
5969 print_one_breakpoint_location (struct breakpoint *b,
5970 struct bp_location *loc,
5971 int loc_number,
5972 struct bp_location **last_loc,
5973 int allflag, bool raw_loc)
5974 {
5975 struct command_line *l;
5976 static char bpenables[] = "nynny";
5977
5978 struct ui_out *uiout = current_uiout;
5979 int header_of_multiple = 0;
5980 int part_of_multiple = (loc != NULL);
5981 struct value_print_options opts;
5982
5983 get_user_print_options (&opts);
5984
5985 gdb_assert (!loc || loc_number != 0);
5986 /* See comment in print_one_breakpoint concerning treatment of
5987 breakpoints with single disabled location. */
5988 if (loc == NULL
5989 && (b->loc != NULL
5990 && (b->loc->next != NULL || !b->loc->enabled)))
5991 header_of_multiple = 1;
5992 if (loc == NULL)
5993 loc = b->loc;
5994
5995 annotate_record ();
5996
5997 /* 1 */
5998 annotate_field (0);
5999 if (part_of_multiple)
6000 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6001 else
6002 uiout->field_signed ("number", b->number);
6003
6004 /* 2 */
6005 annotate_field (1);
6006 if (part_of_multiple)
6007 uiout->field_skip ("type");
6008 else
6009 uiout->field_string ("type", bptype_string (b->type));
6010
6011 /* 3 */
6012 annotate_field (2);
6013 if (part_of_multiple)
6014 uiout->field_skip ("disp");
6015 else
6016 uiout->field_string ("disp", bpdisp_text (b->disposition));
6017
6018 /* 4 */
6019 annotate_field (3);
6020 if (part_of_multiple)
6021 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
6022 else
6023 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6024
6025 /* 5 and 6 */
6026 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
6027 b->ops->print_one (b, last_loc);
6028 else
6029 {
6030 if (is_watchpoint (b))
6031 {
6032 struct watchpoint *w = (struct watchpoint *) b;
6033
6034 /* Field 4, the address, is omitted (which makes the columns
6035 not line up too nicely with the headers, but the effect
6036 is relatively readable). */
6037 if (opts.addressprint)
6038 uiout->field_skip ("addr");
6039 annotate_field (5);
6040 uiout->field_string ("what", w->exp_string);
6041 }
6042 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6043 || is_ada_exception_catchpoint (b))
6044 {
6045 if (opts.addressprint)
6046 {
6047 annotate_field (4);
6048 if (header_of_multiple)
6049 uiout->field_string ("addr", "<MULTIPLE>",
6050 metadata_style.style ());
6051 else if (b->loc == NULL || loc->shlib_disabled)
6052 uiout->field_string ("addr", "<PENDING>",
6053 metadata_style.style ());
6054 else
6055 uiout->field_core_addr ("addr",
6056 loc->gdbarch, loc->address);
6057 }
6058 annotate_field (5);
6059 if (!header_of_multiple)
6060 print_breakpoint_location (b, loc);
6061 if (b->loc)
6062 *last_loc = b->loc;
6063 }
6064 }
6065
6066 if (loc != NULL && !header_of_multiple)
6067 {
6068 std::vector<int> inf_nums;
6069 int mi_only = 1;
6070
6071 for (inferior *inf : all_inferiors ())
6072 {
6073 if (inf->pspace == loc->pspace)
6074 inf_nums.push_back (inf->num);
6075 }
6076
6077 /* For backward compatibility, don't display inferiors in CLI unless
6078 there are several. Always display for MI. */
6079 if (allflag
6080 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6081 && (program_spaces.size () > 1
6082 || number_of_inferiors () > 1)
6083 /* LOC is for existing B, it cannot be in
6084 moribund_locations and thus having NULL OWNER. */
6085 && loc->owner->type != bp_catchpoint))
6086 mi_only = 0;
6087 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6088 }
6089
6090 if (!part_of_multiple)
6091 {
6092 if (b->thread != -1)
6093 {
6094 /* FIXME: This seems to be redundant and lost here; see the
6095 "stop only in" line a little further down. */
6096 uiout->text (" thread ");
6097 uiout->field_signed ("thread", b->thread);
6098 }
6099 else if (b->task != 0)
6100 {
6101 uiout->text (" task ");
6102 uiout->field_signed ("task", b->task);
6103 }
6104 }
6105
6106 uiout->text ("\n");
6107
6108 if (!part_of_multiple)
6109 b->ops->print_one_detail (b, uiout);
6110
6111 if (part_of_multiple && frame_id_p (b->frame_id))
6112 {
6113 annotate_field (6);
6114 uiout->text ("\tstop only in stack frame at ");
6115 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6116 the frame ID. */
6117 uiout->field_core_addr ("frame",
6118 b->gdbarch, b->frame_id.stack_addr);
6119 uiout->text ("\n");
6120 }
6121
6122 if (!part_of_multiple && b->cond_string)
6123 {
6124 annotate_field (7);
6125 if (is_tracepoint (b))
6126 uiout->text ("\ttrace only if ");
6127 else
6128 uiout->text ("\tstop only if ");
6129 uiout->field_string ("cond", b->cond_string);
6130
6131 /* Print whether the target is doing the breakpoint's condition
6132 evaluation. If GDB is doing the evaluation, don't print anything. */
6133 if (is_breakpoint (b)
6134 && breakpoint_condition_evaluation_mode ()
6135 == condition_evaluation_target)
6136 {
6137 uiout->message (" (%pF evals)",
6138 string_field ("evaluated-by",
6139 bp_condition_evaluator (b)));
6140 }
6141 uiout->text ("\n");
6142 }
6143
6144 if (!part_of_multiple && b->thread != -1)
6145 {
6146 /* FIXME should make an annotation for this. */
6147 uiout->text ("\tstop only in thread ");
6148 if (uiout->is_mi_like_p ())
6149 uiout->field_signed ("thread", b->thread);
6150 else
6151 {
6152 struct thread_info *thr = find_thread_global_id (b->thread);
6153
6154 uiout->field_string ("thread", print_thread_id (thr));
6155 }
6156 uiout->text ("\n");
6157 }
6158
6159 if (!part_of_multiple)
6160 {
6161 if (b->hit_count)
6162 {
6163 /* FIXME should make an annotation for this. */
6164 if (is_catchpoint (b))
6165 uiout->text ("\tcatchpoint");
6166 else if (is_tracepoint (b))
6167 uiout->text ("\ttracepoint");
6168 else
6169 uiout->text ("\tbreakpoint");
6170 uiout->text (" already hit ");
6171 uiout->field_signed ("times", b->hit_count);
6172 if (b->hit_count == 1)
6173 uiout->text (" time\n");
6174 else
6175 uiout->text (" times\n");
6176 }
6177 else
6178 {
6179 /* Output the count also if it is zero, but only if this is mi. */
6180 if (uiout->is_mi_like_p ())
6181 uiout->field_signed ("times", b->hit_count);
6182 }
6183 }
6184
6185 if (!part_of_multiple && b->ignore_count)
6186 {
6187 annotate_field (8);
6188 uiout->message ("\tignore next %pF hits\n",
6189 signed_field ("ignore", b->ignore_count));
6190 }
6191
6192 /* Note that an enable count of 1 corresponds to "enable once"
6193 behavior, which is reported by the combination of enablement and
6194 disposition, so we don't need to mention it here. */
6195 if (!part_of_multiple && b->enable_count > 1)
6196 {
6197 annotate_field (8);
6198 uiout->text ("\tdisable after ");
6199 /* Tweak the wording to clarify that ignore and enable counts
6200 are distinct, and have additive effect. */
6201 if (b->ignore_count)
6202 uiout->text ("additional ");
6203 else
6204 uiout->text ("next ");
6205 uiout->field_signed ("enable", b->enable_count);
6206 uiout->text (" hits\n");
6207 }
6208
6209 if (!part_of_multiple && is_tracepoint (b))
6210 {
6211 struct tracepoint *tp = (struct tracepoint *) b;
6212
6213 if (tp->traceframe_usage)
6214 {
6215 uiout->text ("\ttrace buffer usage ");
6216 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6217 uiout->text (" bytes\n");
6218 }
6219 }
6220
6221 l = b->commands ? b->commands.get () : NULL;
6222 if (!part_of_multiple && l)
6223 {
6224 annotate_field (9);
6225 ui_out_emit_tuple tuple_emitter (uiout, "script");
6226 print_command_lines (uiout, l, 4);
6227 }
6228
6229 if (is_tracepoint (b))
6230 {
6231 struct tracepoint *t = (struct tracepoint *) b;
6232
6233 if (!part_of_multiple && t->pass_count)
6234 {
6235 annotate_field (10);
6236 uiout->text ("\tpass count ");
6237 uiout->field_signed ("pass", t->pass_count);
6238 uiout->text (" \n");
6239 }
6240
6241 /* Don't display it when tracepoint or tracepoint location is
6242 pending. */
6243 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6244 {
6245 annotate_field (11);
6246
6247 if (uiout->is_mi_like_p ())
6248 uiout->field_string ("installed",
6249 loc->inserted ? "y" : "n");
6250 else
6251 {
6252 if (loc->inserted)
6253 uiout->text ("\t");
6254 else
6255 uiout->text ("\tnot ");
6256 uiout->text ("installed on target\n");
6257 }
6258 }
6259 }
6260
6261 if (uiout->is_mi_like_p () && !part_of_multiple)
6262 {
6263 if (is_watchpoint (b))
6264 {
6265 struct watchpoint *w = (struct watchpoint *) b;
6266
6267 uiout->field_string ("original-location", w->exp_string);
6268 }
6269 else if (b->location != NULL
6270 && event_location_to_string (b->location.get ()) != NULL)
6271 uiout->field_string ("original-location",
6272 event_location_to_string (b->location.get ()));
6273 }
6274 }
6275
6276 /* See breakpoint.h. */
6277
6278 bool fix_multi_location_breakpoint_output_globally = false;
6279
6280 static void
6281 print_one_breakpoint (struct breakpoint *b,
6282 struct bp_location **last_loc,
6283 int allflag)
6284 {
6285 struct ui_out *uiout = current_uiout;
6286 bool use_fixed_output
6287 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6288 || fix_multi_location_breakpoint_output_globally);
6289
6290 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6291 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
6292
6293 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6294 are outside. */
6295 if (!use_fixed_output)
6296 bkpt_tuple_emitter.reset ();
6297
6298 /* If this breakpoint has custom print function,
6299 it's already printed. Otherwise, print individual
6300 locations, if any. */
6301 if (b->ops == NULL
6302 || b->ops->print_one == NULL
6303 || allflag)
6304 {
6305 /* If breakpoint has a single location that is disabled, we
6306 print it as if it had several locations, since otherwise it's
6307 hard to represent "breakpoint enabled, location disabled"
6308 situation.
6309
6310 Note that while hardware watchpoints have several locations
6311 internally, that's not a property exposed to users.
6312
6313 Likewise, while catchpoints may be implemented with
6314 breakpoints (e.g., catch throw), that's not a property
6315 exposed to users. We do however display the internal
6316 breakpoint locations with "maint info breakpoints". */
6317 if (!is_hardware_watchpoint (b)
6318 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6319 || is_ada_exception_catchpoint (b))
6320 && (allflag
6321 || (b->loc && (b->loc->next || !b->loc->enabled))))
6322 {
6323 gdb::optional<ui_out_emit_list> locations_list;
6324
6325 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6326 MI record. For later versions, place breakpoint locations in a
6327 list. */
6328 if (uiout->is_mi_like_p () && use_fixed_output)
6329 locations_list.emplace (uiout, "locations");
6330
6331 int n = 1;
6332 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next, ++n)
6333 {
6334 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6335 print_one_breakpoint_location (b, loc, n, last_loc,
6336 allflag, allflag);
6337 }
6338 }
6339 }
6340 }
6341
6342 static int
6343 breakpoint_address_bits (struct breakpoint *b)
6344 {
6345 int print_address_bits = 0;
6346 struct bp_location *loc;
6347
6348 /* Software watchpoints that aren't watching memory don't have an
6349 address to print. */
6350 if (is_no_memory_software_watchpoint (b))
6351 return 0;
6352
6353 for (loc = b->loc; loc; loc = loc->next)
6354 {
6355 int addr_bit;
6356
6357 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6358 if (addr_bit > print_address_bits)
6359 print_address_bits = addr_bit;
6360 }
6361
6362 return print_address_bits;
6363 }
6364
6365 /* See breakpoint.h. */
6366
6367 void
6368 print_breakpoint (breakpoint *b)
6369 {
6370 struct bp_location *dummy_loc = NULL;
6371 print_one_breakpoint (b, &dummy_loc, 0);
6372 }
6373
6374 /* Return true if this breakpoint was set by the user, false if it is
6375 internal or momentary. */
6376
6377 int
6378 user_breakpoint_p (struct breakpoint *b)
6379 {
6380 return b->number > 0;
6381 }
6382
6383 /* See breakpoint.h. */
6384
6385 int
6386 pending_breakpoint_p (struct breakpoint *b)
6387 {
6388 return b->loc == NULL;
6389 }
6390
6391 /* Print information on breakpoints (including watchpoints and tracepoints).
6392
6393 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6394 understood by number_or_range_parser. Only breakpoints included in this
6395 list are then printed.
6396
6397 If SHOW_INTERNAL is true, print internal breakpoints.
6398
6399 If FILTER is non-NULL, call it on each breakpoint and only include the
6400 ones for which it returns true.
6401
6402 Return the total number of breakpoints listed. */
6403
6404 static int
6405 breakpoint_1 (const char *bp_num_list, bool show_internal,
6406 bool (*filter) (const struct breakpoint *))
6407 {
6408 struct breakpoint *b;
6409 struct bp_location *last_loc = NULL;
6410 int nr_printable_breakpoints;
6411 struct value_print_options opts;
6412 int print_address_bits = 0;
6413 int print_type_col_width = 14;
6414 struct ui_out *uiout = current_uiout;
6415
6416 get_user_print_options (&opts);
6417
6418 /* Compute the number of rows in the table, as well as the size
6419 required for address fields. */
6420 nr_printable_breakpoints = 0;
6421 ALL_BREAKPOINTS (b)
6422 {
6423 /* If we have a filter, only list the breakpoints it accepts. */
6424 if (filter && !filter (b))
6425 continue;
6426
6427 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6428 accept. Skip the others. */
6429 if (bp_num_list != NULL && *bp_num_list != '\0')
6430 {
6431 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6432 continue;
6433 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6434 continue;
6435 }
6436
6437 if (show_internal || user_breakpoint_p (b))
6438 {
6439 int addr_bit, type_len;
6440
6441 addr_bit = breakpoint_address_bits (b);
6442 if (addr_bit > print_address_bits)
6443 print_address_bits = addr_bit;
6444
6445 type_len = strlen (bptype_string (b->type));
6446 if (type_len > print_type_col_width)
6447 print_type_col_width = type_len;
6448
6449 nr_printable_breakpoints++;
6450 }
6451 }
6452
6453 {
6454 ui_out_emit_table table_emitter (uiout,
6455 opts.addressprint ? 6 : 5,
6456 nr_printable_breakpoints,
6457 "BreakpointTable");
6458
6459 if (nr_printable_breakpoints > 0)
6460 annotate_breakpoints_headers ();
6461 if (nr_printable_breakpoints > 0)
6462 annotate_field (0);
6463 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6464 if (nr_printable_breakpoints > 0)
6465 annotate_field (1);
6466 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6467 if (nr_printable_breakpoints > 0)
6468 annotate_field (2);
6469 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6470 if (nr_printable_breakpoints > 0)
6471 annotate_field (3);
6472 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6473 if (opts.addressprint)
6474 {
6475 if (nr_printable_breakpoints > 0)
6476 annotate_field (4);
6477 if (print_address_bits <= 32)
6478 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6479 else
6480 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6481 }
6482 if (nr_printable_breakpoints > 0)
6483 annotate_field (5);
6484 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6485 uiout->table_body ();
6486 if (nr_printable_breakpoints > 0)
6487 annotate_breakpoints_table ();
6488
6489 ALL_BREAKPOINTS (b)
6490 {
6491 QUIT;
6492 /* If we have a filter, only list the breakpoints it accepts. */
6493 if (filter && !filter (b))
6494 continue;
6495
6496 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6497 accept. Skip the others. */
6498
6499 if (bp_num_list != NULL && *bp_num_list != '\0')
6500 {
6501 if (show_internal) /* maintenance info breakpoint */
6502 {
6503 if (parse_and_eval_long (bp_num_list) != b->number)
6504 continue;
6505 }
6506 else /* all others */
6507 {
6508 if (!number_is_in_list (bp_num_list, b->number))
6509 continue;
6510 }
6511 }
6512 /* We only print out user settable breakpoints unless the
6513 show_internal is set. */
6514 if (show_internal || user_breakpoint_p (b))
6515 print_one_breakpoint (b, &last_loc, show_internal);
6516 }
6517 }
6518
6519 if (nr_printable_breakpoints == 0)
6520 {
6521 /* If there's a filter, let the caller decide how to report
6522 empty list. */
6523 if (!filter)
6524 {
6525 if (bp_num_list == NULL || *bp_num_list == '\0')
6526 uiout->message ("No breakpoints or watchpoints.\n");
6527 else
6528 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6529 bp_num_list);
6530 }
6531 }
6532 else
6533 {
6534 if (last_loc && !server_command)
6535 set_next_address (last_loc->gdbarch, last_loc->address);
6536 }
6537
6538 /* FIXME? Should this be moved up so that it is only called when
6539 there have been breakpoints? */
6540 annotate_breakpoints_table_end ();
6541
6542 return nr_printable_breakpoints;
6543 }
6544
6545 /* Display the value of default-collect in a way that is generally
6546 compatible with the breakpoint list. */
6547
6548 static void
6549 default_collect_info (void)
6550 {
6551 struct ui_out *uiout = current_uiout;
6552
6553 /* If it has no value (which is frequently the case), say nothing; a
6554 message like "No default-collect." gets in user's face when it's
6555 not wanted. */
6556 if (!*default_collect)
6557 return;
6558
6559 /* The following phrase lines up nicely with per-tracepoint collect
6560 actions. */
6561 uiout->text ("default collect ");
6562 uiout->field_string ("default-collect", default_collect);
6563 uiout->text (" \n");
6564 }
6565
6566 static void
6567 info_breakpoints_command (const char *args, int from_tty)
6568 {
6569 breakpoint_1 (args, false, NULL);
6570
6571 default_collect_info ();
6572 }
6573
6574 static void
6575 info_watchpoints_command (const char *args, int from_tty)
6576 {
6577 int num_printed = breakpoint_1 (args, false, is_watchpoint);
6578 struct ui_out *uiout = current_uiout;
6579
6580 if (num_printed == 0)
6581 {
6582 if (args == NULL || *args == '\0')
6583 uiout->message ("No watchpoints.\n");
6584 else
6585 uiout->message ("No watchpoint matching '%s'.\n", args);
6586 }
6587 }
6588
6589 static void
6590 maintenance_info_breakpoints (const char *args, int from_tty)
6591 {
6592 breakpoint_1 (args, true, NULL);
6593
6594 default_collect_info ();
6595 }
6596
6597 static int
6598 breakpoint_has_pc (struct breakpoint *b,
6599 struct program_space *pspace,
6600 CORE_ADDR pc, struct obj_section *section)
6601 {
6602 struct bp_location *bl = b->loc;
6603
6604 for (; bl; bl = bl->next)
6605 {
6606 if (bl->pspace == pspace
6607 && bl->address == pc
6608 && (!overlay_debugging || bl->section == section))
6609 return 1;
6610 }
6611 return 0;
6612 }
6613
6614 /* Print a message describing any user-breakpoints set at PC. This
6615 concerns with logical breakpoints, so we match program spaces, not
6616 address spaces. */
6617
6618 static void
6619 describe_other_breakpoints (struct gdbarch *gdbarch,
6620 struct program_space *pspace, CORE_ADDR pc,
6621 struct obj_section *section, int thread)
6622 {
6623 int others = 0;
6624 struct breakpoint *b;
6625
6626 ALL_BREAKPOINTS (b)
6627 others += (user_breakpoint_p (b)
6628 && breakpoint_has_pc (b, pspace, pc, section));
6629 if (others > 0)
6630 {
6631 if (others == 1)
6632 printf_filtered (_("Note: breakpoint "));
6633 else /* if (others == ???) */
6634 printf_filtered (_("Note: breakpoints "));
6635 ALL_BREAKPOINTS (b)
6636 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6637 {
6638 others--;
6639 printf_filtered ("%d", b->number);
6640 if (b->thread == -1 && thread != -1)
6641 printf_filtered (" (all threads)");
6642 else if (b->thread != -1)
6643 printf_filtered (" (thread %d)", b->thread);
6644 printf_filtered ("%s%s ",
6645 ((b->enable_state == bp_disabled
6646 || b->enable_state == bp_call_disabled)
6647 ? " (disabled)"
6648 : ""),
6649 (others > 1) ? ","
6650 : ((others == 1) ? " and" : ""));
6651 }
6652 current_uiout->message (_("also set at pc %ps.\n"),
6653 styled_string (address_style.style (),
6654 paddress (gdbarch, pc)));
6655 }
6656 }
6657 \f
6658
6659 /* Return true iff it is meaningful to use the address member of LOC.
6660 For some breakpoint types, the locations' address members are
6661 irrelevant and it makes no sense to attempt to compare them to
6662 other addresses (or use them for any other purpose either).
6663
6664 More specifically, software watchpoints and catchpoints that are
6665 not backed by breakpoints always have a zero valued location
6666 address and we don't want to mark breakpoints of any of these types
6667 to be a duplicate of an actual breakpoint location at address
6668 zero. */
6669
6670 static bool
6671 bl_address_is_meaningful (bp_location *loc)
6672 {
6673 return loc->loc_type != bp_loc_other;
6674 }
6675
6676 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6677 true if LOC1 and LOC2 represent the same watchpoint location. */
6678
6679 static int
6680 watchpoint_locations_match (struct bp_location *loc1,
6681 struct bp_location *loc2)
6682 {
6683 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6684 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6685
6686 /* Both of them must exist. */
6687 gdb_assert (w1 != NULL);
6688 gdb_assert (w2 != NULL);
6689
6690 /* If the target can evaluate the condition expression in hardware,
6691 then we we need to insert both watchpoints even if they are at
6692 the same place. Otherwise the watchpoint will only trigger when
6693 the condition of whichever watchpoint was inserted evaluates to
6694 true, not giving a chance for GDB to check the condition of the
6695 other watchpoint. */
6696 if ((w1->cond_exp
6697 && target_can_accel_watchpoint_condition (loc1->address,
6698 loc1->length,
6699 loc1->watchpoint_type,
6700 w1->cond_exp.get ()))
6701 || (w2->cond_exp
6702 && target_can_accel_watchpoint_condition (loc2->address,
6703 loc2->length,
6704 loc2->watchpoint_type,
6705 w2->cond_exp.get ())))
6706 return 0;
6707
6708 /* Note that this checks the owner's type, not the location's. In
6709 case the target does not support read watchpoints, but does
6710 support access watchpoints, we'll have bp_read_watchpoint
6711 watchpoints with hw_access locations. Those should be considered
6712 duplicates of hw_read locations. The hw_read locations will
6713 become hw_access locations later. */
6714 return (loc1->owner->type == loc2->owner->type
6715 && loc1->pspace->aspace == loc2->pspace->aspace
6716 && loc1->address == loc2->address
6717 && loc1->length == loc2->length);
6718 }
6719
6720 /* See breakpoint.h. */
6721
6722 int
6723 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6724 const address_space *aspace2, CORE_ADDR addr2)
6725 {
6726 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6727 || aspace1 == aspace2)
6728 && addr1 == addr2);
6729 }
6730
6731 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6732 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6733 matches ASPACE2. On targets that have global breakpoints, the address
6734 space doesn't really matter. */
6735
6736 static int
6737 breakpoint_address_match_range (const address_space *aspace1,
6738 CORE_ADDR addr1,
6739 int len1, const address_space *aspace2,
6740 CORE_ADDR addr2)
6741 {
6742 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6743 || aspace1 == aspace2)
6744 && addr2 >= addr1 && addr2 < addr1 + len1);
6745 }
6746
6747 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6748 a ranged breakpoint. In most targets, a match happens only if ASPACE
6749 matches the breakpoint's address space. On targets that have global
6750 breakpoints, the address space doesn't really matter. */
6751
6752 static int
6753 breakpoint_location_address_match (struct bp_location *bl,
6754 const address_space *aspace,
6755 CORE_ADDR addr)
6756 {
6757 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6758 aspace, addr)
6759 || (bl->length
6760 && breakpoint_address_match_range (bl->pspace->aspace,
6761 bl->address, bl->length,
6762 aspace, addr)));
6763 }
6764
6765 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6766 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6767 match happens only if ASPACE matches the breakpoint's address
6768 space. On targets that have global breakpoints, the address space
6769 doesn't really matter. */
6770
6771 static int
6772 breakpoint_location_address_range_overlap (struct bp_location *bl,
6773 const address_space *aspace,
6774 CORE_ADDR addr, int len)
6775 {
6776 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6777 || bl->pspace->aspace == aspace)
6778 {
6779 int bl_len = bl->length != 0 ? bl->length : 1;
6780
6781 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6782 return 1;
6783 }
6784 return 0;
6785 }
6786
6787 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6788 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6789 true, otherwise returns false. */
6790
6791 static int
6792 tracepoint_locations_match (struct bp_location *loc1,
6793 struct bp_location *loc2)
6794 {
6795 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6796 /* Since tracepoint locations are never duplicated with others', tracepoint
6797 locations at the same address of different tracepoints are regarded as
6798 different locations. */
6799 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6800 else
6801 return 0;
6802 }
6803
6804 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6805 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
6806 the same location. If SW_HW_BPS_MATCH is true, then software
6807 breakpoint locations and hardware breakpoint locations match,
6808 otherwise they don't. */
6809
6810 static int
6811 breakpoint_locations_match (struct bp_location *loc1,
6812 struct bp_location *loc2,
6813 bool sw_hw_bps_match)
6814 {
6815 int hw_point1, hw_point2;
6816
6817 /* Both of them must not be in moribund_locations. */
6818 gdb_assert (loc1->owner != NULL);
6819 gdb_assert (loc2->owner != NULL);
6820
6821 hw_point1 = is_hardware_watchpoint (loc1->owner);
6822 hw_point2 = is_hardware_watchpoint (loc2->owner);
6823
6824 if (hw_point1 != hw_point2)
6825 return 0;
6826 else if (hw_point1)
6827 return watchpoint_locations_match (loc1, loc2);
6828 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6829 return tracepoint_locations_match (loc1, loc2);
6830 else
6831 /* We compare bp_location.length in order to cover ranged
6832 breakpoints. Keep this in sync with
6833 bp_location_is_less_than. */
6834 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6835 loc2->pspace->aspace, loc2->address)
6836 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
6837 && loc1->length == loc2->length);
6838 }
6839
6840 static void
6841 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6842 int bnum, int have_bnum)
6843 {
6844 /* The longest string possibly returned by hex_string_custom
6845 is 50 chars. These must be at least that big for safety. */
6846 char astr1[64];
6847 char astr2[64];
6848
6849 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6850 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
6851 if (have_bnum)
6852 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
6853 bnum, astr1, astr2);
6854 else
6855 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
6856 }
6857
6858 /* Adjust a breakpoint's address to account for architectural
6859 constraints on breakpoint placement. Return the adjusted address.
6860 Note: Very few targets require this kind of adjustment. For most
6861 targets, this function is simply the identity function. */
6862
6863 static CORE_ADDR
6864 adjust_breakpoint_address (struct gdbarch *gdbarch,
6865 CORE_ADDR bpaddr, enum bptype bptype)
6866 {
6867 if (bptype == bp_watchpoint
6868 || bptype == bp_hardware_watchpoint
6869 || bptype == bp_read_watchpoint
6870 || bptype == bp_access_watchpoint
6871 || bptype == bp_catchpoint)
6872 {
6873 /* Watchpoints and the various bp_catch_* eventpoints should not
6874 have their addresses modified. */
6875 return bpaddr;
6876 }
6877 else if (bptype == bp_single_step)
6878 {
6879 /* Single-step breakpoints should not have their addresses
6880 modified. If there's any architectural constrain that
6881 applies to this address, then it should have already been
6882 taken into account when the breakpoint was created in the
6883 first place. If we didn't do this, stepping through e.g.,
6884 Thumb-2 IT blocks would break. */
6885 return bpaddr;
6886 }
6887 else
6888 {
6889 CORE_ADDR adjusted_bpaddr = bpaddr;
6890
6891 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
6892 {
6893 /* Some targets have architectural constraints on the placement
6894 of breakpoint instructions. Obtain the adjusted address. */
6895 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
6896 }
6897
6898 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
6899
6900 /* An adjusted breakpoint address can significantly alter
6901 a user's expectations. Print a warning if an adjustment
6902 is required. */
6903 if (adjusted_bpaddr != bpaddr)
6904 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6905
6906 return adjusted_bpaddr;
6907 }
6908 }
6909
6910 static bp_loc_type
6911 bp_location_from_bp_type (bptype type)
6912 {
6913 switch (type)
6914 {
6915 case bp_breakpoint:
6916 case bp_single_step:
6917 case bp_until:
6918 case bp_finish:
6919 case bp_longjmp:
6920 case bp_longjmp_resume:
6921 case bp_longjmp_call_dummy:
6922 case bp_exception:
6923 case bp_exception_resume:
6924 case bp_step_resume:
6925 case bp_hp_step_resume:
6926 case bp_watchpoint_scope:
6927 case bp_call_dummy:
6928 case bp_std_terminate:
6929 case bp_shlib_event:
6930 case bp_thread_event:
6931 case bp_overlay_event:
6932 case bp_jit_event:
6933 case bp_longjmp_master:
6934 case bp_std_terminate_master:
6935 case bp_exception_master:
6936 case bp_gnu_ifunc_resolver:
6937 case bp_gnu_ifunc_resolver_return:
6938 case bp_dprintf:
6939 return bp_loc_software_breakpoint;
6940 case bp_hardware_breakpoint:
6941 return bp_loc_hardware_breakpoint;
6942 case bp_hardware_watchpoint:
6943 case bp_read_watchpoint:
6944 case bp_access_watchpoint:
6945 return bp_loc_hardware_watchpoint;
6946 case bp_watchpoint:
6947 case bp_catchpoint:
6948 case bp_tracepoint:
6949 case bp_fast_tracepoint:
6950 case bp_static_tracepoint:
6951 return bp_loc_other;
6952 default:
6953 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
6954 }
6955 }
6956
6957 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
6958 {
6959 this->owner = owner;
6960 this->cond_bytecode = NULL;
6961 this->shlib_disabled = 0;
6962 this->enabled = 1;
6963
6964 this->loc_type = type;
6965
6966 if (this->loc_type == bp_loc_software_breakpoint
6967 || this->loc_type == bp_loc_hardware_breakpoint)
6968 mark_breakpoint_location_modified (this);
6969
6970 this->refc = 1;
6971 }
6972
6973 bp_location::bp_location (breakpoint *owner)
6974 : bp_location::bp_location (owner,
6975 bp_location_from_bp_type (owner->type))
6976 {
6977 }
6978
6979 /* Allocate a struct bp_location. */
6980
6981 static struct bp_location *
6982 allocate_bp_location (struct breakpoint *bpt)
6983 {
6984 return bpt->ops->allocate_location (bpt);
6985 }
6986
6987 static void
6988 free_bp_location (struct bp_location *loc)
6989 {
6990 delete loc;
6991 }
6992
6993 /* Increment reference count. */
6994
6995 static void
6996 incref_bp_location (struct bp_location *bl)
6997 {
6998 ++bl->refc;
6999 }
7000
7001 /* Decrement reference count. If the reference count reaches 0,
7002 destroy the bp_location. Sets *BLP to NULL. */
7003
7004 static void
7005 decref_bp_location (struct bp_location **blp)
7006 {
7007 gdb_assert ((*blp)->refc > 0);
7008
7009 if (--(*blp)->refc == 0)
7010 free_bp_location (*blp);
7011 *blp = NULL;
7012 }
7013
7014 /* Add breakpoint B at the end of the global breakpoint chain. */
7015
7016 static breakpoint *
7017 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7018 {
7019 struct breakpoint *b1;
7020 struct breakpoint *result = b.get ();
7021
7022 /* Add this breakpoint to the end of the chain so that a list of
7023 breakpoints will come out in order of increasing numbers. */
7024
7025 b1 = breakpoint_chain;
7026 if (b1 == 0)
7027 breakpoint_chain = b.release ();
7028 else
7029 {
7030 while (b1->next)
7031 b1 = b1->next;
7032 b1->next = b.release ();
7033 }
7034
7035 return result;
7036 }
7037
7038 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7039
7040 static void
7041 init_raw_breakpoint_without_location (struct breakpoint *b,
7042 struct gdbarch *gdbarch,
7043 enum bptype bptype,
7044 const struct breakpoint_ops *ops)
7045 {
7046 gdb_assert (ops != NULL);
7047
7048 b->ops = ops;
7049 b->type = bptype;
7050 b->gdbarch = gdbarch;
7051 b->language = current_language->la_language;
7052 b->input_radix = input_radix;
7053 b->related_breakpoint = b;
7054 }
7055
7056 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7057 that has type BPTYPE and has no locations as yet. */
7058
7059 static struct breakpoint *
7060 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7061 enum bptype bptype,
7062 const struct breakpoint_ops *ops)
7063 {
7064 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7065
7066 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7067 return add_to_breakpoint_chain (std::move (b));
7068 }
7069
7070 /* Initialize loc->function_name. */
7071
7072 static void
7073 set_breakpoint_location_function (struct bp_location *loc)
7074 {
7075 gdb_assert (loc->owner != NULL);
7076
7077 if (loc->owner->type == bp_breakpoint
7078 || loc->owner->type == bp_hardware_breakpoint
7079 || is_tracepoint (loc->owner))
7080 {
7081 const char *function_name;
7082
7083 if (loc->msymbol != NULL
7084 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
7085 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
7086 {
7087 struct breakpoint *b = loc->owner;
7088
7089 function_name = loc->msymbol->linkage_name ();
7090
7091 if (b->type == bp_breakpoint && b->loc == loc
7092 && loc->next == NULL && b->related_breakpoint == b)
7093 {
7094 /* Create only the whole new breakpoint of this type but do not
7095 mess more complicated breakpoints with multiple locations. */
7096 b->type = bp_gnu_ifunc_resolver;
7097 /* Remember the resolver's address for use by the return
7098 breakpoint. */
7099 loc->related_address = loc->address;
7100 }
7101 }
7102 else
7103 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7104
7105 if (function_name)
7106 loc->function_name = xstrdup (function_name);
7107 }
7108 }
7109
7110 /* Attempt to determine architecture of location identified by SAL. */
7111 struct gdbarch *
7112 get_sal_arch (struct symtab_and_line sal)
7113 {
7114 if (sal.section)
7115 return sal.section->objfile->arch ();
7116 if (sal.symtab)
7117 return SYMTAB_OBJFILE (sal.symtab)->arch ();
7118
7119 return NULL;
7120 }
7121
7122 /* Low level routine for partially initializing a breakpoint of type
7123 BPTYPE. The newly created breakpoint's address, section, source
7124 file name, and line number are provided by SAL.
7125
7126 It is expected that the caller will complete the initialization of
7127 the newly created breakpoint struct as well as output any status
7128 information regarding the creation of a new breakpoint. */
7129
7130 static void
7131 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7132 struct symtab_and_line sal, enum bptype bptype,
7133 const struct breakpoint_ops *ops)
7134 {
7135 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7136
7137 add_location_to_breakpoint (b, &sal);
7138
7139 if (bptype != bp_catchpoint)
7140 gdb_assert (sal.pspace != NULL);
7141
7142 /* Store the program space that was used to set the breakpoint,
7143 except for ordinary breakpoints, which are independent of the
7144 program space. */
7145 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7146 b->pspace = sal.pspace;
7147 }
7148
7149 /* set_raw_breakpoint is a low level routine for allocating and
7150 partially initializing a breakpoint of type BPTYPE. The newly
7151 created breakpoint's address, section, source file name, and line
7152 number are provided by SAL. The newly created and partially
7153 initialized breakpoint is added to the breakpoint chain and
7154 is also returned as the value of this function.
7155
7156 It is expected that the caller will complete the initialization of
7157 the newly created breakpoint struct as well as output any status
7158 information regarding the creation of a new breakpoint. In
7159 particular, set_raw_breakpoint does NOT set the breakpoint
7160 number! Care should be taken to not allow an error to occur
7161 prior to completing the initialization of the breakpoint. If this
7162 should happen, a bogus breakpoint will be left on the chain. */
7163
7164 struct breakpoint *
7165 set_raw_breakpoint (struct gdbarch *gdbarch,
7166 struct symtab_and_line sal, enum bptype bptype,
7167 const struct breakpoint_ops *ops)
7168 {
7169 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7170
7171 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7172 return add_to_breakpoint_chain (std::move (b));
7173 }
7174
7175 /* Call this routine when stepping and nexting to enable a breakpoint
7176 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7177 initiated the operation. */
7178
7179 void
7180 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7181 {
7182 struct breakpoint *b, *b_tmp;
7183 int thread = tp->global_num;
7184
7185 /* To avoid having to rescan all objfile symbols at every step,
7186 we maintain a list of continually-inserted but always disabled
7187 longjmp "master" breakpoints. Here, we simply create momentary
7188 clones of those and enable them for the requested thread. */
7189 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7190 if (b->pspace == current_program_space
7191 && (b->type == bp_longjmp_master
7192 || b->type == bp_exception_master))
7193 {
7194 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7195 struct breakpoint *clone;
7196
7197 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7198 after their removal. */
7199 clone = momentary_breakpoint_from_master (b, type,
7200 &momentary_breakpoint_ops, 1);
7201 clone->thread = thread;
7202 }
7203
7204 tp->initiating_frame = frame;
7205 }
7206
7207 /* Delete all longjmp breakpoints from THREAD. */
7208 void
7209 delete_longjmp_breakpoint (int thread)
7210 {
7211 struct breakpoint *b, *b_tmp;
7212
7213 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7214 if (b->type == bp_longjmp || b->type == bp_exception)
7215 {
7216 if (b->thread == thread)
7217 delete_breakpoint (b);
7218 }
7219 }
7220
7221 void
7222 delete_longjmp_breakpoint_at_next_stop (int thread)
7223 {
7224 struct breakpoint *b, *b_tmp;
7225
7226 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7227 if (b->type == bp_longjmp || b->type == bp_exception)
7228 {
7229 if (b->thread == thread)
7230 b->disposition = disp_del_at_next_stop;
7231 }
7232 }
7233
7234 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7235 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7236 pointer to any of them. Return NULL if this system cannot place longjmp
7237 breakpoints. */
7238
7239 struct breakpoint *
7240 set_longjmp_breakpoint_for_call_dummy (void)
7241 {
7242 struct breakpoint *b, *retval = NULL;
7243
7244 ALL_BREAKPOINTS (b)
7245 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7246 {
7247 struct breakpoint *new_b;
7248
7249 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7250 &momentary_breakpoint_ops,
7251 1);
7252 new_b->thread = inferior_thread ()->global_num;
7253
7254 /* Link NEW_B into the chain of RETVAL breakpoints. */
7255
7256 gdb_assert (new_b->related_breakpoint == new_b);
7257 if (retval == NULL)
7258 retval = new_b;
7259 new_b->related_breakpoint = retval;
7260 while (retval->related_breakpoint != new_b->related_breakpoint)
7261 retval = retval->related_breakpoint;
7262 retval->related_breakpoint = new_b;
7263 }
7264
7265 return retval;
7266 }
7267
7268 /* Verify all existing dummy frames and their associated breakpoints for
7269 TP. Remove those which can no longer be found in the current frame
7270 stack.
7271
7272 You should call this function only at places where it is safe to currently
7273 unwind the whole stack. Failed stack unwind would discard live dummy
7274 frames. */
7275
7276 void
7277 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7278 {
7279 struct breakpoint *b, *b_tmp;
7280
7281 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7282 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7283 {
7284 struct breakpoint *dummy_b = b->related_breakpoint;
7285
7286 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7287 dummy_b = dummy_b->related_breakpoint;
7288 if (dummy_b->type != bp_call_dummy
7289 || frame_find_by_id (dummy_b->frame_id) != NULL)
7290 continue;
7291
7292 dummy_frame_discard (dummy_b->frame_id, tp);
7293
7294 while (b->related_breakpoint != b)
7295 {
7296 if (b_tmp == b->related_breakpoint)
7297 b_tmp = b->related_breakpoint->next;
7298 delete_breakpoint (b->related_breakpoint);
7299 }
7300 delete_breakpoint (b);
7301 }
7302 }
7303
7304 void
7305 enable_overlay_breakpoints (void)
7306 {
7307 struct breakpoint *b;
7308
7309 ALL_BREAKPOINTS (b)
7310 if (b->type == bp_overlay_event)
7311 {
7312 b->enable_state = bp_enabled;
7313 update_global_location_list (UGLL_MAY_INSERT);
7314 overlay_events_enabled = 1;
7315 }
7316 }
7317
7318 void
7319 disable_overlay_breakpoints (void)
7320 {
7321 struct breakpoint *b;
7322
7323 ALL_BREAKPOINTS (b)
7324 if (b->type == bp_overlay_event)
7325 {
7326 b->enable_state = bp_disabled;
7327 update_global_location_list (UGLL_DONT_INSERT);
7328 overlay_events_enabled = 0;
7329 }
7330 }
7331
7332 /* Set an active std::terminate breakpoint for each std::terminate
7333 master breakpoint. */
7334 void
7335 set_std_terminate_breakpoint (void)
7336 {
7337 struct breakpoint *b, *b_tmp;
7338
7339 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7340 if (b->pspace == current_program_space
7341 && b->type == bp_std_terminate_master)
7342 {
7343 momentary_breakpoint_from_master (b, bp_std_terminate,
7344 &momentary_breakpoint_ops, 1);
7345 }
7346 }
7347
7348 /* Delete all the std::terminate breakpoints. */
7349 void
7350 delete_std_terminate_breakpoint (void)
7351 {
7352 struct breakpoint *b, *b_tmp;
7353
7354 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7355 if (b->type == bp_std_terminate)
7356 delete_breakpoint (b);
7357 }
7358
7359 struct breakpoint *
7360 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7361 {
7362 struct breakpoint *b;
7363
7364 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7365 &internal_breakpoint_ops);
7366
7367 b->enable_state = bp_enabled;
7368 /* location has to be used or breakpoint_re_set will delete me. */
7369 b->location = new_address_location (b->loc->address, NULL, 0);
7370
7371 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7372
7373 return b;
7374 }
7375
7376 struct lang_and_radix
7377 {
7378 enum language lang;
7379 int radix;
7380 };
7381
7382 /* Create a breakpoint for JIT code registration and unregistration. */
7383
7384 struct breakpoint *
7385 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7386 {
7387 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7388 &internal_breakpoint_ops);
7389 }
7390
7391 /* Remove JIT code registration and unregistration breakpoint(s). */
7392
7393 void
7394 remove_jit_event_breakpoints (void)
7395 {
7396 struct breakpoint *b, *b_tmp;
7397
7398 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7399 if (b->type == bp_jit_event
7400 && b->loc->pspace == current_program_space)
7401 delete_breakpoint (b);
7402 }
7403
7404 void
7405 remove_solib_event_breakpoints (void)
7406 {
7407 struct breakpoint *b, *b_tmp;
7408
7409 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7410 if (b->type == bp_shlib_event
7411 && b->loc->pspace == current_program_space)
7412 delete_breakpoint (b);
7413 }
7414
7415 /* See breakpoint.h. */
7416
7417 void
7418 remove_solib_event_breakpoints_at_next_stop (void)
7419 {
7420 struct breakpoint *b, *b_tmp;
7421
7422 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7423 if (b->type == bp_shlib_event
7424 && b->loc->pspace == current_program_space)
7425 b->disposition = disp_del_at_next_stop;
7426 }
7427
7428 /* Helper for create_solib_event_breakpoint /
7429 create_and_insert_solib_event_breakpoint. Allows specifying which
7430 INSERT_MODE to pass through to update_global_location_list. */
7431
7432 static struct breakpoint *
7433 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7434 enum ugll_insert_mode insert_mode)
7435 {
7436 struct breakpoint *b;
7437
7438 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7439 &internal_breakpoint_ops);
7440 update_global_location_list_nothrow (insert_mode);
7441 return b;
7442 }
7443
7444 struct breakpoint *
7445 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7446 {
7447 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7448 }
7449
7450 /* See breakpoint.h. */
7451
7452 struct breakpoint *
7453 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7454 {
7455 struct breakpoint *b;
7456
7457 /* Explicitly tell update_global_location_list to insert
7458 locations. */
7459 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7460 if (!b->loc->inserted)
7461 {
7462 delete_breakpoint (b);
7463 return NULL;
7464 }
7465 return b;
7466 }
7467
7468 /* Disable any breakpoints that are on code in shared libraries. Only
7469 apply to enabled breakpoints, disabled ones can just stay disabled. */
7470
7471 void
7472 disable_breakpoints_in_shlibs (void)
7473 {
7474 struct bp_location *loc, **locp_tmp;
7475
7476 ALL_BP_LOCATIONS (loc, locp_tmp)
7477 {
7478 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7479 struct breakpoint *b = loc->owner;
7480
7481 /* We apply the check to all breakpoints, including disabled for
7482 those with loc->duplicate set. This is so that when breakpoint
7483 becomes enabled, or the duplicate is removed, gdb will try to
7484 insert all breakpoints. If we don't set shlib_disabled here,
7485 we'll try to insert those breakpoints and fail. */
7486 if (((b->type == bp_breakpoint)
7487 || (b->type == bp_jit_event)
7488 || (b->type == bp_hardware_breakpoint)
7489 || (is_tracepoint (b)))
7490 && loc->pspace == current_program_space
7491 && !loc->shlib_disabled
7492 && solib_name_from_address (loc->pspace, loc->address)
7493 )
7494 {
7495 loc->shlib_disabled = 1;
7496 }
7497 }
7498 }
7499
7500 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7501 notification of unloaded_shlib. Only apply to enabled breakpoints,
7502 disabled ones can just stay disabled. */
7503
7504 static void
7505 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7506 {
7507 struct bp_location *loc, **locp_tmp;
7508 int disabled_shlib_breaks = 0;
7509
7510 ALL_BP_LOCATIONS (loc, locp_tmp)
7511 {
7512 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7513 struct breakpoint *b = loc->owner;
7514
7515 if (solib->pspace == loc->pspace
7516 && !loc->shlib_disabled
7517 && (((b->type == bp_breakpoint
7518 || b->type == bp_jit_event
7519 || b->type == bp_hardware_breakpoint)
7520 && (loc->loc_type == bp_loc_hardware_breakpoint
7521 || loc->loc_type == bp_loc_software_breakpoint))
7522 || is_tracepoint (b))
7523 && solib_contains_address_p (solib, loc->address))
7524 {
7525 loc->shlib_disabled = 1;
7526 /* At this point, we cannot rely on remove_breakpoint
7527 succeeding so we must mark the breakpoint as not inserted
7528 to prevent future errors occurring in remove_breakpoints. */
7529 loc->inserted = 0;
7530
7531 /* This may cause duplicate notifications for the same breakpoint. */
7532 gdb::observers::breakpoint_modified.notify (b);
7533
7534 if (!disabled_shlib_breaks)
7535 {
7536 target_terminal::ours_for_output ();
7537 warning (_("Temporarily disabling breakpoints "
7538 "for unloaded shared library \"%s\""),
7539 solib->so_name);
7540 }
7541 disabled_shlib_breaks = 1;
7542 }
7543 }
7544 }
7545
7546 /* Disable any breakpoints and tracepoints in OBJFILE upon
7547 notification of free_objfile. Only apply to enabled breakpoints,
7548 disabled ones can just stay disabled. */
7549
7550 static void
7551 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7552 {
7553 struct breakpoint *b;
7554
7555 if (objfile == NULL)
7556 return;
7557
7558 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7559 managed by the user with add-symbol-file/remove-symbol-file.
7560 Similarly to how breakpoints in shared libraries are handled in
7561 response to "nosharedlibrary", mark breakpoints in such modules
7562 shlib_disabled so they end up uninserted on the next global
7563 location list update. Shared libraries not loaded by the user
7564 aren't handled here -- they're already handled in
7565 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7566 solib_unloaded observer. We skip objfiles that are not
7567 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7568 main objfile). */
7569 if ((objfile->flags & OBJF_SHARED) == 0
7570 || (objfile->flags & OBJF_USERLOADED) == 0)
7571 return;
7572
7573 ALL_BREAKPOINTS (b)
7574 {
7575 struct bp_location *loc;
7576 int bp_modified = 0;
7577
7578 if (!is_breakpoint (b) && !is_tracepoint (b))
7579 continue;
7580
7581 for (loc = b->loc; loc != NULL; loc = loc->next)
7582 {
7583 CORE_ADDR loc_addr = loc->address;
7584
7585 if (loc->loc_type != bp_loc_hardware_breakpoint
7586 && loc->loc_type != bp_loc_software_breakpoint)
7587 continue;
7588
7589 if (loc->shlib_disabled != 0)
7590 continue;
7591
7592 if (objfile->pspace != loc->pspace)
7593 continue;
7594
7595 if (loc->loc_type != bp_loc_hardware_breakpoint
7596 && loc->loc_type != bp_loc_software_breakpoint)
7597 continue;
7598
7599 if (is_addr_in_objfile (loc_addr, objfile))
7600 {
7601 loc->shlib_disabled = 1;
7602 /* At this point, we don't know whether the object was
7603 unmapped from the inferior or not, so leave the
7604 inserted flag alone. We'll handle failure to
7605 uninsert quietly, in case the object was indeed
7606 unmapped. */
7607
7608 mark_breakpoint_location_modified (loc);
7609
7610 bp_modified = 1;
7611 }
7612 }
7613
7614 if (bp_modified)
7615 gdb::observers::breakpoint_modified.notify (b);
7616 }
7617 }
7618
7619 /* FORK & VFORK catchpoints. */
7620
7621 /* An instance of this type is used to represent a fork or vfork
7622 catchpoint. A breakpoint is really of this type iff its ops pointer points
7623 to CATCH_FORK_BREAKPOINT_OPS. */
7624
7625 struct fork_catchpoint : public breakpoint
7626 {
7627 /* Process id of a child process whose forking triggered this
7628 catchpoint. This field is only valid immediately after this
7629 catchpoint has triggered. */
7630 ptid_t forked_inferior_pid;
7631 };
7632
7633 /* Implement the "insert" breakpoint_ops method for fork
7634 catchpoints. */
7635
7636 static int
7637 insert_catch_fork (struct bp_location *bl)
7638 {
7639 return target_insert_fork_catchpoint (inferior_ptid.pid ());
7640 }
7641
7642 /* Implement the "remove" breakpoint_ops method for fork
7643 catchpoints. */
7644
7645 static int
7646 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
7647 {
7648 return target_remove_fork_catchpoint (inferior_ptid.pid ());
7649 }
7650
7651 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7652 catchpoints. */
7653
7654 static int
7655 breakpoint_hit_catch_fork (const struct bp_location *bl,
7656 const address_space *aspace, CORE_ADDR bp_addr,
7657 const struct target_waitstatus *ws)
7658 {
7659 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7660
7661 if (ws->kind != TARGET_WAITKIND_FORKED)
7662 return 0;
7663
7664 c->forked_inferior_pid = ws->value.related_pid;
7665 return 1;
7666 }
7667
7668 /* Implement the "print_it" breakpoint_ops method for fork
7669 catchpoints. */
7670
7671 static enum print_stop_action
7672 print_it_catch_fork (bpstat bs)
7673 {
7674 struct ui_out *uiout = current_uiout;
7675 struct breakpoint *b = bs->breakpoint_at;
7676 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7677
7678 annotate_catchpoint (b->number);
7679 maybe_print_thread_hit_breakpoint (uiout);
7680 if (b->disposition == disp_del)
7681 uiout->text ("Temporary catchpoint ");
7682 else
7683 uiout->text ("Catchpoint ");
7684 if (uiout->is_mi_like_p ())
7685 {
7686 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7687 uiout->field_string ("disp", bpdisp_text (b->disposition));
7688 }
7689 uiout->field_signed ("bkptno", b->number);
7690 uiout->text (" (forked process ");
7691 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7692 uiout->text ("), ");
7693 return PRINT_SRC_AND_LOC;
7694 }
7695
7696 /* Implement the "print_one" breakpoint_ops method for fork
7697 catchpoints. */
7698
7699 static void
7700 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7701 {
7702 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7703 struct value_print_options opts;
7704 struct ui_out *uiout = current_uiout;
7705
7706 get_user_print_options (&opts);
7707
7708 /* Field 4, the address, is omitted (which makes the columns not
7709 line up too nicely with the headers, but the effect is relatively
7710 readable). */
7711 if (opts.addressprint)
7712 uiout->field_skip ("addr");
7713 annotate_field (5);
7714 uiout->text ("fork");
7715 if (c->forked_inferior_pid != null_ptid)
7716 {
7717 uiout->text (", process ");
7718 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7719 uiout->spaces (1);
7720 }
7721
7722 if (uiout->is_mi_like_p ())
7723 uiout->field_string ("catch-type", "fork");
7724 }
7725
7726 /* Implement the "print_mention" breakpoint_ops method for fork
7727 catchpoints. */
7728
7729 static void
7730 print_mention_catch_fork (struct breakpoint *b)
7731 {
7732 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7733 }
7734
7735 /* Implement the "print_recreate" breakpoint_ops method for fork
7736 catchpoints. */
7737
7738 static void
7739 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7740 {
7741 fprintf_unfiltered (fp, "catch fork");
7742 print_recreate_thread (b, fp);
7743 }
7744
7745 /* The breakpoint_ops structure to be used in fork catchpoints. */
7746
7747 static struct breakpoint_ops catch_fork_breakpoint_ops;
7748
7749 /* Implement the "insert" breakpoint_ops method for vfork
7750 catchpoints. */
7751
7752 static int
7753 insert_catch_vfork (struct bp_location *bl)
7754 {
7755 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
7756 }
7757
7758 /* Implement the "remove" breakpoint_ops method for vfork
7759 catchpoints. */
7760
7761 static int
7762 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
7763 {
7764 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
7765 }
7766
7767 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7768 catchpoints. */
7769
7770 static int
7771 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7772 const address_space *aspace, CORE_ADDR bp_addr,
7773 const struct target_waitstatus *ws)
7774 {
7775 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7776
7777 if (ws->kind != TARGET_WAITKIND_VFORKED)
7778 return 0;
7779
7780 c->forked_inferior_pid = ws->value.related_pid;
7781 return 1;
7782 }
7783
7784 /* Implement the "print_it" breakpoint_ops method for vfork
7785 catchpoints. */
7786
7787 static enum print_stop_action
7788 print_it_catch_vfork (bpstat bs)
7789 {
7790 struct ui_out *uiout = current_uiout;
7791 struct breakpoint *b = bs->breakpoint_at;
7792 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7793
7794 annotate_catchpoint (b->number);
7795 maybe_print_thread_hit_breakpoint (uiout);
7796 if (b->disposition == disp_del)
7797 uiout->text ("Temporary catchpoint ");
7798 else
7799 uiout->text ("Catchpoint ");
7800 if (uiout->is_mi_like_p ())
7801 {
7802 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7803 uiout->field_string ("disp", bpdisp_text (b->disposition));
7804 }
7805 uiout->field_signed ("bkptno", b->number);
7806 uiout->text (" (vforked process ");
7807 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7808 uiout->text ("), ");
7809 return PRINT_SRC_AND_LOC;
7810 }
7811
7812 /* Implement the "print_one" breakpoint_ops method for vfork
7813 catchpoints. */
7814
7815 static void
7816 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7817 {
7818 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7819 struct value_print_options opts;
7820 struct ui_out *uiout = current_uiout;
7821
7822 get_user_print_options (&opts);
7823 /* Field 4, the address, is omitted (which makes the columns not
7824 line up too nicely with the headers, but the effect is relatively
7825 readable). */
7826 if (opts.addressprint)
7827 uiout->field_skip ("addr");
7828 annotate_field (5);
7829 uiout->text ("vfork");
7830 if (c->forked_inferior_pid != null_ptid)
7831 {
7832 uiout->text (", process ");
7833 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7834 uiout->spaces (1);
7835 }
7836
7837 if (uiout->is_mi_like_p ())
7838 uiout->field_string ("catch-type", "vfork");
7839 }
7840
7841 /* Implement the "print_mention" breakpoint_ops method for vfork
7842 catchpoints. */
7843
7844 static void
7845 print_mention_catch_vfork (struct breakpoint *b)
7846 {
7847 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7848 }
7849
7850 /* Implement the "print_recreate" breakpoint_ops method for vfork
7851 catchpoints. */
7852
7853 static void
7854 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7855 {
7856 fprintf_unfiltered (fp, "catch vfork");
7857 print_recreate_thread (b, fp);
7858 }
7859
7860 /* The breakpoint_ops structure to be used in vfork catchpoints. */
7861
7862 static struct breakpoint_ops catch_vfork_breakpoint_ops;
7863
7864 /* An instance of this type is used to represent an solib catchpoint.
7865 A breakpoint is really of this type iff its ops pointer points to
7866 CATCH_SOLIB_BREAKPOINT_OPS. */
7867
7868 struct solib_catchpoint : public breakpoint
7869 {
7870 ~solib_catchpoint () override;
7871
7872 /* True for "catch load", false for "catch unload". */
7873 bool is_load;
7874
7875 /* Regular expression to match, if any. COMPILED is only valid when
7876 REGEX is non-NULL. */
7877 char *regex;
7878 std::unique_ptr<compiled_regex> compiled;
7879 };
7880
7881 solib_catchpoint::~solib_catchpoint ()
7882 {
7883 xfree (this->regex);
7884 }
7885
7886 static int
7887 insert_catch_solib (struct bp_location *ignore)
7888 {
7889 return 0;
7890 }
7891
7892 static int
7893 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
7894 {
7895 return 0;
7896 }
7897
7898 static int
7899 breakpoint_hit_catch_solib (const struct bp_location *bl,
7900 const address_space *aspace,
7901 CORE_ADDR bp_addr,
7902 const struct target_waitstatus *ws)
7903 {
7904 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7905 struct breakpoint *other;
7906
7907 if (ws->kind == TARGET_WAITKIND_LOADED)
7908 return 1;
7909
7910 ALL_BREAKPOINTS (other)
7911 {
7912 struct bp_location *other_bl;
7913
7914 if (other == bl->owner)
7915 continue;
7916
7917 if (other->type != bp_shlib_event)
7918 continue;
7919
7920 if (self->pspace != NULL && other->pspace != self->pspace)
7921 continue;
7922
7923 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
7924 {
7925 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7926 return 1;
7927 }
7928 }
7929
7930 return 0;
7931 }
7932
7933 static void
7934 check_status_catch_solib (struct bpstats *bs)
7935 {
7936 struct solib_catchpoint *self
7937 = (struct solib_catchpoint *) bs->breakpoint_at;
7938
7939 if (self->is_load)
7940 {
7941 for (so_list *iter : current_program_space->added_solibs)
7942 {
7943 if (!self->regex
7944 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
7945 return;
7946 }
7947 }
7948 else
7949 {
7950 for (const std::string &iter : current_program_space->deleted_solibs)
7951 {
7952 if (!self->regex
7953 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
7954 return;
7955 }
7956 }
7957
7958 bs->stop = 0;
7959 bs->print_it = print_it_noop;
7960 }
7961
7962 static enum print_stop_action
7963 print_it_catch_solib (bpstat bs)
7964 {
7965 struct breakpoint *b = bs->breakpoint_at;
7966 struct ui_out *uiout = current_uiout;
7967
7968 annotate_catchpoint (b->number);
7969 maybe_print_thread_hit_breakpoint (uiout);
7970 if (b->disposition == disp_del)
7971 uiout->text ("Temporary catchpoint ");
7972 else
7973 uiout->text ("Catchpoint ");
7974 uiout->field_signed ("bkptno", b->number);
7975 uiout->text ("\n");
7976 if (uiout->is_mi_like_p ())
7977 uiout->field_string ("disp", bpdisp_text (b->disposition));
7978 print_solib_event (1);
7979 return PRINT_SRC_AND_LOC;
7980 }
7981
7982 static void
7983 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
7984 {
7985 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7986 struct value_print_options opts;
7987 struct ui_out *uiout = current_uiout;
7988
7989 get_user_print_options (&opts);
7990 /* Field 4, the address, is omitted (which makes the columns not
7991 line up too nicely with the headers, but the effect is relatively
7992 readable). */
7993 if (opts.addressprint)
7994 {
7995 annotate_field (4);
7996 uiout->field_skip ("addr");
7997 }
7998
7999 std::string msg;
8000 annotate_field (5);
8001 if (self->is_load)
8002 {
8003 if (self->regex)
8004 msg = string_printf (_("load of library matching %s"), self->regex);
8005 else
8006 msg = _("load of library");
8007 }
8008 else
8009 {
8010 if (self->regex)
8011 msg = string_printf (_("unload of library matching %s"), self->regex);
8012 else
8013 msg = _("unload of library");
8014 }
8015 uiout->field_string ("what", msg);
8016
8017 if (uiout->is_mi_like_p ())
8018 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8019 }
8020
8021 static void
8022 print_mention_catch_solib (struct breakpoint *b)
8023 {
8024 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8025
8026 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8027 self->is_load ? "load" : "unload");
8028 }
8029
8030 static void
8031 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8032 {
8033 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8034
8035 fprintf_unfiltered (fp, "%s %s",
8036 b->disposition == disp_del ? "tcatch" : "catch",
8037 self->is_load ? "load" : "unload");
8038 if (self->regex)
8039 fprintf_unfiltered (fp, " %s", self->regex);
8040 fprintf_unfiltered (fp, "\n");
8041 }
8042
8043 static struct breakpoint_ops catch_solib_breakpoint_ops;
8044
8045 /* See breakpoint.h. */
8046
8047 void
8048 add_solib_catchpoint (const char *arg, bool is_load, bool is_temp, bool enabled)
8049 {
8050 struct gdbarch *gdbarch = get_current_arch ();
8051
8052 if (!arg)
8053 arg = "";
8054 arg = skip_spaces (arg);
8055
8056 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
8057
8058 if (*arg != '\0')
8059 {
8060 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8061 _("Invalid regexp")));
8062 c->regex = xstrdup (arg);
8063 }
8064
8065 c->is_load = is_load;
8066 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
8067 &catch_solib_breakpoint_ops);
8068
8069 c->enable_state = enabled ? bp_enabled : bp_disabled;
8070
8071 install_breakpoint (0, std::move (c), 1);
8072 }
8073
8074 /* A helper function that does all the work for "catch load" and
8075 "catch unload". */
8076
8077 static void
8078 catch_load_or_unload (const char *arg, int from_tty, int is_load,
8079 struct cmd_list_element *command)
8080 {
8081 const int enabled = 1;
8082 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
8083
8084 add_solib_catchpoint (arg, is_load, temp, enabled);
8085 }
8086
8087 static void
8088 catch_load_command_1 (const char *arg, int from_tty,
8089 struct cmd_list_element *command)
8090 {
8091 catch_load_or_unload (arg, from_tty, 1, command);
8092 }
8093
8094 static void
8095 catch_unload_command_1 (const char *arg, int from_tty,
8096 struct cmd_list_element *command)
8097 {
8098 catch_load_or_unload (arg, from_tty, 0, command);
8099 }
8100
8101 /* See breakpoint.h. */
8102
8103 void
8104 init_catchpoint (struct breakpoint *b,
8105 struct gdbarch *gdbarch, bool temp,
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 = temp ? 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 bool temp, 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, temp, 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 (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 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->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 return address in the caller frame,
10946 plus breakpoints at all the destination locations. */
10947 std::vector<breakpoint_up> breakpoints;
10948
10949 until_break_fsm (struct interp *cmd_interp, int thread,
10950 std::vector<breakpoint_up> &&breakpoints)
10951 : thread_fsm (cmd_interp),
10952 thread (thread),
10953 breakpoints (std::move (breakpoints))
10954 {
10955 }
10956
10957 void clean_up (struct thread_info *thread) override;
10958 bool should_stop (struct thread_info *thread) override;
10959 enum async_reply_reason do_async_reply_reason () override;
10960 };
10961
10962 /* Implementation of the 'should_stop' FSM method for the
10963 until(location)/advance commands. */
10964
10965 bool
10966 until_break_fsm::should_stop (struct thread_info *tp)
10967 {
10968 for (const breakpoint_up &bp : breakpoints)
10969 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
10970 bp.get ()) != NULL)
10971 {
10972 set_finished ();
10973 break;
10974 }
10975
10976 return true;
10977 }
10978
10979 /* Implementation of the 'clean_up' FSM method for the
10980 until(location)/advance commands. */
10981
10982 void
10983 until_break_fsm::clean_up (struct thread_info *)
10984 {
10985 /* Clean up our temporary breakpoints. */
10986 breakpoints.clear ();
10987 delete_longjmp_breakpoint (thread);
10988 }
10989
10990 /* Implementation of the 'async_reply_reason' FSM method for the
10991 until(location)/advance commands. */
10992
10993 enum async_reply_reason
10994 until_break_fsm::do_async_reply_reason ()
10995 {
10996 return EXEC_ASYNC_LOCATION_REACHED;
10997 }
10998
10999 void
11000 until_break_command (const char *arg, int from_tty, int anywhere)
11001 {
11002 struct frame_info *frame;
11003 struct gdbarch *frame_gdbarch;
11004 struct frame_id stack_frame_id;
11005 struct frame_id caller_frame_id;
11006 int thread;
11007 struct thread_info *tp;
11008
11009 clear_proceed_status (0);
11010
11011 /* Set a breakpoint where the user wants it and at return from
11012 this function. */
11013
11014 event_location_up location = string_to_event_location (&arg, current_language);
11015
11016 std::vector<symtab_and_line> sals
11017 = (last_displayed_sal_is_valid ()
11018 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11019 get_last_displayed_symtab (),
11020 get_last_displayed_line ())
11021 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11022 NULL, NULL, 0));
11023
11024 if (sals.empty ())
11025 error (_("Couldn't get information on specified line."));
11026
11027 if (*arg)
11028 error (_("Junk at end of arguments."));
11029
11030 tp = inferior_thread ();
11031 thread = tp->global_num;
11032
11033 /* Note linespec handling above invalidates the frame chain.
11034 Installing a breakpoint also invalidates the frame chain (as it
11035 may need to switch threads), so do any frame handling before
11036 that. */
11037
11038 frame = get_selected_frame (NULL);
11039 frame_gdbarch = get_frame_arch (frame);
11040 stack_frame_id = get_stack_frame_id (frame);
11041 caller_frame_id = frame_unwind_caller_id (frame);
11042
11043 /* Keep within the current frame, or in frames called by the current
11044 one. */
11045
11046 std::vector<breakpoint_up> breakpoints;
11047
11048 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11049
11050 if (frame_id_p (caller_frame_id))
11051 {
11052 struct symtab_and_line sal2;
11053 struct gdbarch *caller_gdbarch;
11054
11055 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11056 sal2.pc = frame_unwind_caller_pc (frame);
11057 caller_gdbarch = frame_unwind_caller_arch (frame);
11058
11059 breakpoint_up caller_breakpoint
11060 = set_momentary_breakpoint (caller_gdbarch, sal2,
11061 caller_frame_id, bp_until);
11062 breakpoints.emplace_back (std::move (caller_breakpoint));
11063
11064 set_longjmp_breakpoint (tp, caller_frame_id);
11065 lj_deleter.emplace (thread);
11066 }
11067
11068 /* set_momentary_breakpoint could invalidate FRAME. */
11069 frame = NULL;
11070
11071 /* If the user told us to continue until a specified location, we
11072 don't specify a frame at which we need to stop. Otherwise,
11073 specify the selected frame, because we want to stop only at the
11074 very same frame. */
11075 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
11076
11077 for (symtab_and_line &sal : sals)
11078 {
11079 resolve_sal_pc (&sal);
11080
11081 breakpoint_up location_breakpoint
11082 = set_momentary_breakpoint (frame_gdbarch, sal,
11083 stop_frame_id, bp_until);
11084 breakpoints.emplace_back (std::move (location_breakpoint));
11085 }
11086
11087 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
11088 std::move (breakpoints));
11089
11090 if (lj_deleter)
11091 lj_deleter->release ();
11092
11093 proceed (-1, GDB_SIGNAL_DEFAULT);
11094 }
11095
11096 /* This function attempts to parse an optional "if <cond>" clause
11097 from the arg string. If one is not found, it returns NULL.
11098
11099 Else, it returns a pointer to the condition string. (It does not
11100 attempt to evaluate the string against a particular block.) And,
11101 it updates arg to point to the first character following the parsed
11102 if clause in the arg string. */
11103
11104 const char *
11105 ep_parse_optional_if_clause (const char **arg)
11106 {
11107 const char *cond_string;
11108
11109 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11110 return NULL;
11111
11112 /* Skip the "if" keyword. */
11113 (*arg) += 2;
11114
11115 /* Skip any extra leading whitespace, and record the start of the
11116 condition string. */
11117 *arg = skip_spaces (*arg);
11118 cond_string = *arg;
11119
11120 /* Assume that the condition occupies the remainder of the arg
11121 string. */
11122 (*arg) += strlen (cond_string);
11123
11124 return cond_string;
11125 }
11126
11127 /* Commands to deal with catching events, such as signals, exceptions,
11128 process start/exit, etc. */
11129
11130 typedef enum
11131 {
11132 catch_fork_temporary, catch_vfork_temporary,
11133 catch_fork_permanent, catch_vfork_permanent
11134 }
11135 catch_fork_kind;
11136
11137 static void
11138 catch_fork_command_1 (const char *arg, int from_tty,
11139 struct cmd_list_element *command)
11140 {
11141 struct gdbarch *gdbarch = get_current_arch ();
11142 const char *cond_string = NULL;
11143 catch_fork_kind fork_kind;
11144
11145 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11146 bool temp = (fork_kind == catch_fork_temporary
11147 || fork_kind == catch_vfork_temporary);
11148
11149 if (!arg)
11150 arg = "";
11151 arg = skip_spaces (arg);
11152
11153 /* The allowed syntax is:
11154 catch [v]fork
11155 catch [v]fork if <cond>
11156
11157 First, check if there's an if clause. */
11158 cond_string = ep_parse_optional_if_clause (&arg);
11159
11160 if ((*arg != '\0') && !isspace (*arg))
11161 error (_("Junk at end of arguments."));
11162
11163 /* If this target supports it, create a fork or vfork catchpoint
11164 and enable reporting of such events. */
11165 switch (fork_kind)
11166 {
11167 case catch_fork_temporary:
11168 case catch_fork_permanent:
11169 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
11170 &catch_fork_breakpoint_ops);
11171 break;
11172 case catch_vfork_temporary:
11173 case catch_vfork_permanent:
11174 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
11175 &catch_vfork_breakpoint_ops);
11176 break;
11177 default:
11178 error (_("unsupported or unknown fork kind; cannot catch it"));
11179 break;
11180 }
11181 }
11182
11183 static void
11184 catch_exec_command_1 (const char *arg, int from_tty,
11185 struct cmd_list_element *command)
11186 {
11187 struct gdbarch *gdbarch = get_current_arch ();
11188 const char *cond_string = NULL;
11189 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
11190
11191 if (!arg)
11192 arg = "";
11193 arg = skip_spaces (arg);
11194
11195 /* The allowed syntax is:
11196 catch exec
11197 catch exec if <cond>
11198
11199 First, check if there's an if clause. */
11200 cond_string = ep_parse_optional_if_clause (&arg);
11201
11202 if ((*arg != '\0') && !isspace (*arg))
11203 error (_("Junk at end of arguments."));
11204
11205 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11206 init_catchpoint (c.get (), gdbarch, temp, cond_string,
11207 &catch_exec_breakpoint_ops);
11208 c->exec_pathname = NULL;
11209
11210 install_breakpoint (0, std::move (c), 1);
11211 }
11212
11213 void
11214 init_ada_exception_breakpoint (struct breakpoint *b,
11215 struct gdbarch *gdbarch,
11216 struct symtab_and_line sal,
11217 const char *addr_string,
11218 const struct breakpoint_ops *ops,
11219 int tempflag,
11220 int enabled,
11221 int from_tty)
11222 {
11223 if (from_tty)
11224 {
11225 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11226 if (!loc_gdbarch)
11227 loc_gdbarch = gdbarch;
11228
11229 describe_other_breakpoints (loc_gdbarch,
11230 sal.pspace, sal.pc, sal.section, -1);
11231 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11232 version for exception catchpoints, because two catchpoints
11233 used for different exception names will use the same address.
11234 In this case, a "breakpoint ... also set at..." warning is
11235 unproductive. Besides, the warning phrasing is also a bit
11236 inappropriate, we should use the word catchpoint, and tell
11237 the user what type of catchpoint it is. The above is good
11238 enough for now, though. */
11239 }
11240
11241 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
11242
11243 b->enable_state = enabled ? bp_enabled : bp_disabled;
11244 b->disposition = tempflag ? disp_del : disp_donttouch;
11245 b->location = string_to_event_location (&addr_string,
11246 language_def (language_ada));
11247 b->language = language_ada;
11248 }
11249
11250 \f
11251
11252 /* Compare two breakpoints and return a strcmp-like result. */
11253
11254 static int
11255 compare_breakpoints (const breakpoint *a, const breakpoint *b)
11256 {
11257 uintptr_t ua = (uintptr_t) a;
11258 uintptr_t ub = (uintptr_t) b;
11259
11260 if (a->number < b->number)
11261 return -1;
11262 else if (a->number > b->number)
11263 return 1;
11264
11265 /* Now sort by address, in case we see, e..g, two breakpoints with
11266 the number 0. */
11267 if (ua < ub)
11268 return -1;
11269 return ua > ub ? 1 : 0;
11270 }
11271
11272 /* Delete breakpoints by address or line. */
11273
11274 static void
11275 clear_command (const char *arg, int from_tty)
11276 {
11277 struct breakpoint *b;
11278 int default_match;
11279
11280 std::vector<symtab_and_line> decoded_sals;
11281 symtab_and_line last_sal;
11282 gdb::array_view<symtab_and_line> sals;
11283 if (arg)
11284 {
11285 decoded_sals
11286 = decode_line_with_current_source (arg,
11287 (DECODE_LINE_FUNFIRSTLINE
11288 | DECODE_LINE_LIST_MODE));
11289 default_match = 0;
11290 sals = decoded_sals;
11291 }
11292 else
11293 {
11294 /* Set sal's line, symtab, pc, and pspace to the values
11295 corresponding to the last call to print_frame_info. If the
11296 codepoint is not valid, this will set all the fields to 0. */
11297 last_sal = get_last_displayed_sal ();
11298 if (last_sal.symtab == 0)
11299 error (_("No source file specified."));
11300
11301 default_match = 1;
11302 sals = last_sal;
11303 }
11304
11305 /* We don't call resolve_sal_pc here. That's not as bad as it
11306 seems, because all existing breakpoints typically have both
11307 file/line and pc set. So, if clear is given file/line, we can
11308 match this to existing breakpoint without obtaining pc at all.
11309
11310 We only support clearing given the address explicitly
11311 present in breakpoint table. Say, we've set breakpoint
11312 at file:line. There were several PC values for that file:line,
11313 due to optimization, all in one block.
11314
11315 We've picked one PC value. If "clear" is issued with another
11316 PC corresponding to the same file:line, the breakpoint won't
11317 be cleared. We probably can still clear the breakpoint, but
11318 since the other PC value is never presented to user, user
11319 can only find it by guessing, and it does not seem important
11320 to support that. */
11321
11322 /* For each line spec given, delete bps which correspond to it. Do
11323 it in two passes, solely to preserve the current behavior that
11324 from_tty is forced true if we delete more than one
11325 breakpoint. */
11326
11327 std::vector<struct breakpoint *> found;
11328 for (const auto &sal : sals)
11329 {
11330 const char *sal_fullname;
11331
11332 /* If exact pc given, clear bpts at that pc.
11333 If line given (pc == 0), clear all bpts on specified line.
11334 If defaulting, clear all bpts on default line
11335 or at default pc.
11336
11337 defaulting sal.pc != 0 tests to do
11338
11339 0 1 pc
11340 1 1 pc _and_ line
11341 0 0 line
11342 1 0 <can't happen> */
11343
11344 sal_fullname = (sal.symtab == NULL
11345 ? NULL : symtab_to_fullname (sal.symtab));
11346
11347 /* Find all matching breakpoints and add them to 'found'. */
11348 ALL_BREAKPOINTS (b)
11349 {
11350 int match = 0;
11351 /* Are we going to delete b? */
11352 if (b->type != bp_none && !is_watchpoint (b))
11353 {
11354 struct bp_location *loc = b->loc;
11355 for (; loc; loc = loc->next)
11356 {
11357 /* If the user specified file:line, don't allow a PC
11358 match. This matches historical gdb behavior. */
11359 int pc_match = (!sal.explicit_line
11360 && sal.pc
11361 && (loc->pspace == sal.pspace)
11362 && (loc->address == sal.pc)
11363 && (!section_is_overlay (loc->section)
11364 || loc->section == sal.section));
11365 int line_match = 0;
11366
11367 if ((default_match || sal.explicit_line)
11368 && loc->symtab != NULL
11369 && sal_fullname != NULL
11370 && sal.pspace == loc->pspace
11371 && loc->line_number == sal.line
11372 && filename_cmp (symtab_to_fullname (loc->symtab),
11373 sal_fullname) == 0)
11374 line_match = 1;
11375
11376 if (pc_match || line_match)
11377 {
11378 match = 1;
11379 break;
11380 }
11381 }
11382 }
11383
11384 if (match)
11385 found.push_back (b);
11386 }
11387 }
11388
11389 /* Now go thru the 'found' chain and delete them. */
11390 if (found.empty ())
11391 {
11392 if (arg)
11393 error (_("No breakpoint at %s."), arg);
11394 else
11395 error (_("No breakpoint at this line."));
11396 }
11397
11398 /* Remove duplicates from the vec. */
11399 std::sort (found.begin (), found.end (),
11400 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11401 {
11402 return compare_breakpoints (bp_a, bp_b) < 0;
11403 });
11404 found.erase (std::unique (found.begin (), found.end (),
11405 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11406 {
11407 return compare_breakpoints (bp_a, bp_b) == 0;
11408 }),
11409 found.end ());
11410
11411 if (found.size () > 1)
11412 from_tty = 1; /* Always report if deleted more than one. */
11413 if (from_tty)
11414 {
11415 if (found.size () == 1)
11416 printf_unfiltered (_("Deleted breakpoint "));
11417 else
11418 printf_unfiltered (_("Deleted breakpoints "));
11419 }
11420
11421 for (breakpoint *iter : found)
11422 {
11423 if (from_tty)
11424 printf_unfiltered ("%d ", iter->number);
11425 delete_breakpoint (iter);
11426 }
11427 if (from_tty)
11428 putchar_unfiltered ('\n');
11429 }
11430 \f
11431 /* Delete breakpoint in BS if they are `delete' breakpoints and
11432 all breakpoints that are marked for deletion, whether hit or not.
11433 This is called after any breakpoint is hit, or after errors. */
11434
11435 void
11436 breakpoint_auto_delete (bpstat bs)
11437 {
11438 struct breakpoint *b, *b_tmp;
11439
11440 for (; bs; bs = bs->next)
11441 if (bs->breakpoint_at
11442 && bs->breakpoint_at->disposition == disp_del
11443 && bs->stop)
11444 delete_breakpoint (bs->breakpoint_at);
11445
11446 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11447 {
11448 if (b->disposition == disp_del_at_next_stop)
11449 delete_breakpoint (b);
11450 }
11451 }
11452
11453 /* A comparison function for bp_location AP and BP being interfaced to
11454 std::sort. Sort elements primarily by their ADDRESS (no matter what
11455 bl_address_is_meaningful says), secondarily by ordering first
11456 permanent elements and terciarily just ensuring the array is sorted
11457 stable way despite std::sort being an unstable algorithm. */
11458
11459 static int
11460 bp_location_is_less_than (const bp_location *a, const bp_location *b)
11461 {
11462 if (a->address != b->address)
11463 return a->address < b->address;
11464
11465 /* Sort locations at the same address by their pspace number, keeping
11466 locations of the same inferior (in a multi-inferior environment)
11467 grouped. */
11468
11469 if (a->pspace->num != b->pspace->num)
11470 return a->pspace->num < b->pspace->num;
11471
11472 /* Sort permanent breakpoints first. */
11473 if (a->permanent != b->permanent)
11474 return a->permanent > b->permanent;
11475
11476 /* Sort by type in order to make duplicate determination easier.
11477 See update_global_location_list. This is kept in sync with
11478 breakpoint_locations_match. */
11479 if (a->loc_type < b->loc_type)
11480 return true;
11481
11482 /* Likewise, for range-breakpoints, sort by length. */
11483 if (a->loc_type == bp_loc_hardware_breakpoint
11484 && b->loc_type == bp_loc_hardware_breakpoint
11485 && a->length < b->length)
11486 return true;
11487
11488 /* Make the internal GDB representation stable across GDB runs
11489 where A and B memory inside GDB can differ. Breakpoint locations of
11490 the same type at the same address can be sorted in arbitrary order. */
11491
11492 if (a->owner->number != b->owner->number)
11493 return a->owner->number < b->owner->number;
11494
11495 return a < b;
11496 }
11497
11498 /* Set bp_locations_placed_address_before_address_max and
11499 bp_locations_shadow_len_after_address_max according to the current
11500 content of the bp_locations array. */
11501
11502 static void
11503 bp_locations_target_extensions_update (void)
11504 {
11505 struct bp_location *bl, **blp_tmp;
11506
11507 bp_locations_placed_address_before_address_max = 0;
11508 bp_locations_shadow_len_after_address_max = 0;
11509
11510 ALL_BP_LOCATIONS (bl, blp_tmp)
11511 {
11512 CORE_ADDR start, end, addr;
11513
11514 if (!bp_location_has_shadow (bl))
11515 continue;
11516
11517 start = bl->target_info.placed_address;
11518 end = start + bl->target_info.shadow_len;
11519
11520 gdb_assert (bl->address >= start);
11521 addr = bl->address - start;
11522 if (addr > bp_locations_placed_address_before_address_max)
11523 bp_locations_placed_address_before_address_max = addr;
11524
11525 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11526
11527 gdb_assert (bl->address < end);
11528 addr = end - bl->address;
11529 if (addr > bp_locations_shadow_len_after_address_max)
11530 bp_locations_shadow_len_after_address_max = addr;
11531 }
11532 }
11533
11534 /* Download tracepoint locations if they haven't been. */
11535
11536 static void
11537 download_tracepoint_locations (void)
11538 {
11539 struct breakpoint *b;
11540 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11541
11542 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11543
11544 ALL_TRACEPOINTS (b)
11545 {
11546 struct bp_location *bl;
11547 struct tracepoint *t;
11548 int bp_location_downloaded = 0;
11549
11550 if ((b->type == bp_fast_tracepoint
11551 ? !may_insert_fast_tracepoints
11552 : !may_insert_tracepoints))
11553 continue;
11554
11555 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11556 {
11557 if (target_can_download_tracepoint ())
11558 can_download_tracepoint = TRIBOOL_TRUE;
11559 else
11560 can_download_tracepoint = TRIBOOL_FALSE;
11561 }
11562
11563 if (can_download_tracepoint == TRIBOOL_FALSE)
11564 break;
11565
11566 for (bl = b->loc; bl; bl = bl->next)
11567 {
11568 /* In tracepoint, locations are _never_ duplicated, so
11569 should_be_inserted is equivalent to
11570 unduplicated_should_be_inserted. */
11571 if (!should_be_inserted (bl) || bl->inserted)
11572 continue;
11573
11574 switch_to_program_space_and_thread (bl->pspace);
11575
11576 target_download_tracepoint (bl);
11577
11578 bl->inserted = 1;
11579 bp_location_downloaded = 1;
11580 }
11581 t = (struct tracepoint *) b;
11582 t->number_on_target = b->number;
11583 if (bp_location_downloaded)
11584 gdb::observers::breakpoint_modified.notify (b);
11585 }
11586 }
11587
11588 /* Swap the insertion/duplication state between two locations. */
11589
11590 static void
11591 swap_insertion (struct bp_location *left, struct bp_location *right)
11592 {
11593 const int left_inserted = left->inserted;
11594 const int left_duplicate = left->duplicate;
11595 const int left_needs_update = left->needs_update;
11596 const struct bp_target_info left_target_info = left->target_info;
11597
11598 /* Locations of tracepoints can never be duplicated. */
11599 if (is_tracepoint (left->owner))
11600 gdb_assert (!left->duplicate);
11601 if (is_tracepoint (right->owner))
11602 gdb_assert (!right->duplicate);
11603
11604 left->inserted = right->inserted;
11605 left->duplicate = right->duplicate;
11606 left->needs_update = right->needs_update;
11607 left->target_info = right->target_info;
11608 right->inserted = left_inserted;
11609 right->duplicate = left_duplicate;
11610 right->needs_update = left_needs_update;
11611 right->target_info = left_target_info;
11612 }
11613
11614 /* Force the re-insertion of the locations at ADDRESS. This is called
11615 once a new/deleted/modified duplicate location is found and we are evaluating
11616 conditions on the target's side. Such conditions need to be updated on
11617 the target. */
11618
11619 static void
11620 force_breakpoint_reinsertion (struct bp_location *bl)
11621 {
11622 struct bp_location **locp = NULL, **loc2p;
11623 struct bp_location *loc;
11624 CORE_ADDR address = 0;
11625 int pspace_num;
11626
11627 address = bl->address;
11628 pspace_num = bl->pspace->num;
11629
11630 /* This is only meaningful if the target is
11631 evaluating conditions and if the user has
11632 opted for condition evaluation on the target's
11633 side. */
11634 if (gdb_evaluates_breakpoint_condition_p ()
11635 || !target_supports_evaluation_of_breakpoint_conditions ())
11636 return;
11637
11638 /* Flag all breakpoint locations with this address and
11639 the same program space as the location
11640 as "its condition has changed". We need to
11641 update the conditions on the target's side. */
11642 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11643 {
11644 loc = *loc2p;
11645
11646 if (!is_breakpoint (loc->owner)
11647 || pspace_num != loc->pspace->num)
11648 continue;
11649
11650 /* Flag the location appropriately. We use a different state to
11651 let everyone know that we already updated the set of locations
11652 with addr bl->address and program space bl->pspace. This is so
11653 we don't have to keep calling these functions just to mark locations
11654 that have already been marked. */
11655 loc->condition_changed = condition_updated;
11656
11657 /* Free the agent expression bytecode as well. We will compute
11658 it later on. */
11659 loc->cond_bytecode.reset ();
11660 }
11661 }
11662
11663 /* Called whether new breakpoints are created, or existing breakpoints
11664 deleted, to update the global location list and recompute which
11665 locations are duplicate of which.
11666
11667 The INSERT_MODE flag determines whether locations may not, may, or
11668 shall be inserted now. See 'enum ugll_insert_mode' for more
11669 info. */
11670
11671 static void
11672 update_global_location_list (enum ugll_insert_mode insert_mode)
11673 {
11674 struct breakpoint *b;
11675 struct bp_location **locp, *loc;
11676 /* Last breakpoint location address that was marked for update. */
11677 CORE_ADDR last_addr = 0;
11678 /* Last breakpoint location program space that was marked for update. */
11679 int last_pspace_num = -1;
11680
11681 /* Used in the duplicates detection below. When iterating over all
11682 bp_locations, points to the first bp_location of a given address.
11683 Breakpoints and watchpoints of different types are never
11684 duplicates of each other. Keep one pointer for each type of
11685 breakpoint/watchpoint, so we only need to loop over all locations
11686 once. */
11687 struct bp_location *bp_loc_first; /* breakpoint */
11688 struct bp_location *wp_loc_first; /* hardware watchpoint */
11689 struct bp_location *awp_loc_first; /* access watchpoint */
11690 struct bp_location *rwp_loc_first; /* read watchpoint */
11691
11692 /* Saved former bp_locations array which we compare against the newly
11693 built bp_locations from the current state of ALL_BREAKPOINTS. */
11694 struct bp_location **old_locp;
11695 unsigned old_locations_count;
11696 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
11697
11698 old_locations_count = bp_locations_count;
11699 bp_locations = NULL;
11700 bp_locations_count = 0;
11701
11702 ALL_BREAKPOINTS (b)
11703 for (loc = b->loc; loc; loc = loc->next)
11704 bp_locations_count++;
11705
11706 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11707 locp = bp_locations;
11708 ALL_BREAKPOINTS (b)
11709 for (loc = b->loc; loc; loc = loc->next)
11710 *locp++ = loc;
11711
11712 /* See if we need to "upgrade" a software breakpoint to a hardware
11713 breakpoint. Do this before deciding whether locations are
11714 duplicates. Also do this before sorting because sorting order
11715 depends on location type. */
11716 for (locp = bp_locations;
11717 locp < bp_locations + bp_locations_count;
11718 locp++)
11719 {
11720 loc = *locp;
11721 if (!loc->inserted && should_be_inserted (loc))
11722 handle_automatic_hardware_breakpoints (loc);
11723 }
11724
11725 std::sort (bp_locations, bp_locations + bp_locations_count,
11726 bp_location_is_less_than);
11727
11728 bp_locations_target_extensions_update ();
11729
11730 /* Identify bp_location instances that are no longer present in the
11731 new list, and therefore should be freed. Note that it's not
11732 necessary that those locations should be removed from inferior --
11733 if there's another location at the same address (previously
11734 marked as duplicate), we don't need to remove/insert the
11735 location.
11736
11737 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11738 and former bp_location array state respectively. */
11739
11740 locp = bp_locations;
11741 for (old_locp = old_locations.get ();
11742 old_locp < old_locations.get () + old_locations_count;
11743 old_locp++)
11744 {
11745 struct bp_location *old_loc = *old_locp;
11746 struct bp_location **loc2p;
11747
11748 /* Tells if 'old_loc' is found among the new locations. If
11749 not, we have to free it. */
11750 int found_object = 0;
11751 /* Tells if the location should remain inserted in the target. */
11752 int keep_in_target = 0;
11753 int removed = 0;
11754
11755 /* Skip LOCP entries which will definitely never be needed.
11756 Stop either at or being the one matching OLD_LOC. */
11757 while (locp < bp_locations + bp_locations_count
11758 && (*locp)->address < old_loc->address)
11759 locp++;
11760
11761 for (loc2p = locp;
11762 (loc2p < bp_locations + bp_locations_count
11763 && (*loc2p)->address == old_loc->address);
11764 loc2p++)
11765 {
11766 /* Check if this is a new/duplicated location or a duplicated
11767 location that had its condition modified. If so, we want to send
11768 its condition to the target if evaluation of conditions is taking
11769 place there. */
11770 if ((*loc2p)->condition_changed == condition_modified
11771 && (last_addr != old_loc->address
11772 || last_pspace_num != old_loc->pspace->num))
11773 {
11774 force_breakpoint_reinsertion (*loc2p);
11775 last_pspace_num = old_loc->pspace->num;
11776 }
11777
11778 if (*loc2p == old_loc)
11779 found_object = 1;
11780 }
11781
11782 /* We have already handled this address, update it so that we don't
11783 have to go through updates again. */
11784 last_addr = old_loc->address;
11785
11786 /* Target-side condition evaluation: Handle deleted locations. */
11787 if (!found_object)
11788 force_breakpoint_reinsertion (old_loc);
11789
11790 /* If this location is no longer present, and inserted, look if
11791 there's maybe a new location at the same address. If so,
11792 mark that one inserted, and don't remove this one. This is
11793 needed so that we don't have a time window where a breakpoint
11794 at certain location is not inserted. */
11795
11796 if (old_loc->inserted)
11797 {
11798 /* If the location is inserted now, we might have to remove
11799 it. */
11800
11801 if (found_object && should_be_inserted (old_loc))
11802 {
11803 /* The location is still present in the location list,
11804 and still should be inserted. Don't do anything. */
11805 keep_in_target = 1;
11806 }
11807 else
11808 {
11809 /* This location still exists, but it won't be kept in the
11810 target since it may have been disabled. We proceed to
11811 remove its target-side condition. */
11812
11813 /* The location is either no longer present, or got
11814 disabled. See if there's another location at the
11815 same address, in which case we don't need to remove
11816 this one from the target. */
11817
11818 /* OLD_LOC comes from existing struct breakpoint. */
11819 if (bl_address_is_meaningful (old_loc))
11820 {
11821 for (loc2p = locp;
11822 (loc2p < bp_locations + bp_locations_count
11823 && (*loc2p)->address == old_loc->address);
11824 loc2p++)
11825 {
11826 struct bp_location *loc2 = *loc2p;
11827
11828 if (loc2 == old_loc)
11829 continue;
11830
11831 if (breakpoint_locations_match (loc2, old_loc))
11832 {
11833 /* Read watchpoint locations are switched to
11834 access watchpoints, if the former are not
11835 supported, but the latter are. */
11836 if (is_hardware_watchpoint (old_loc->owner))
11837 {
11838 gdb_assert (is_hardware_watchpoint (loc2->owner));
11839 loc2->watchpoint_type = old_loc->watchpoint_type;
11840 }
11841
11842 /* loc2 is a duplicated location. We need to check
11843 if it should be inserted in case it will be
11844 unduplicated. */
11845 if (unduplicated_should_be_inserted (loc2))
11846 {
11847 swap_insertion (old_loc, loc2);
11848 keep_in_target = 1;
11849 break;
11850 }
11851 }
11852 }
11853 }
11854 }
11855
11856 if (!keep_in_target)
11857 {
11858 if (remove_breakpoint (old_loc))
11859 {
11860 /* This is just about all we can do. We could keep
11861 this location on the global list, and try to
11862 remove it next time, but there's no particular
11863 reason why we will succeed next time.
11864
11865 Note that at this point, old_loc->owner is still
11866 valid, as delete_breakpoint frees the breakpoint
11867 only after calling us. */
11868 printf_filtered (_("warning: Error removing "
11869 "breakpoint %d\n"),
11870 old_loc->owner->number);
11871 }
11872 removed = 1;
11873 }
11874 }
11875
11876 if (!found_object)
11877 {
11878 if (removed && target_is_non_stop_p ()
11879 && need_moribund_for_location_type (old_loc))
11880 {
11881 /* This location was removed from the target. In
11882 non-stop mode, a race condition is possible where
11883 we've removed a breakpoint, but stop events for that
11884 breakpoint are already queued and will arrive later.
11885 We apply an heuristic to be able to distinguish such
11886 SIGTRAPs from other random SIGTRAPs: we keep this
11887 breakpoint location for a bit, and will retire it
11888 after we see some number of events. The theory here
11889 is that reporting of events should, "on the average",
11890 be fair, so after a while we'll see events from all
11891 threads that have anything of interest, and no longer
11892 need to keep this breakpoint location around. We
11893 don't hold locations forever so to reduce chances of
11894 mistaking a non-breakpoint SIGTRAP for a breakpoint
11895 SIGTRAP.
11896
11897 The heuristic failing can be disastrous on
11898 decr_pc_after_break targets.
11899
11900 On decr_pc_after_break targets, like e.g., x86-linux,
11901 if we fail to recognize a late breakpoint SIGTRAP,
11902 because events_till_retirement has reached 0 too
11903 soon, we'll fail to do the PC adjustment, and report
11904 a random SIGTRAP to the user. When the user resumes
11905 the inferior, it will most likely immediately crash
11906 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
11907 corrupted, because of being resumed e.g., in the
11908 middle of a multi-byte instruction, or skipped a
11909 one-byte instruction. This was actually seen happen
11910 on native x86-linux, and should be less rare on
11911 targets that do not support new thread events, like
11912 remote, due to the heuristic depending on
11913 thread_count.
11914
11915 Mistaking a random SIGTRAP for a breakpoint trap
11916 causes similar symptoms (PC adjustment applied when
11917 it shouldn't), but then again, playing with SIGTRAPs
11918 behind the debugger's back is asking for trouble.
11919
11920 Since hardware watchpoint traps are always
11921 distinguishable from other traps, so we don't need to
11922 apply keep hardware watchpoint moribund locations
11923 around. We simply always ignore hardware watchpoint
11924 traps we can no longer explain. */
11925
11926 process_stratum_target *proc_target = nullptr;
11927 for (inferior *inf : all_inferiors ())
11928 if (inf->pspace == old_loc->pspace)
11929 {
11930 proc_target = inf->process_target ();
11931 break;
11932 }
11933 if (proc_target != nullptr)
11934 old_loc->events_till_retirement
11935 = 3 * (thread_count (proc_target) + 1);
11936 else
11937 old_loc->events_till_retirement = 1;
11938 old_loc->owner = NULL;
11939
11940 moribund_locations.push_back (old_loc);
11941 }
11942 else
11943 {
11944 old_loc->owner = NULL;
11945 decref_bp_location (&old_loc);
11946 }
11947 }
11948 }
11949
11950 /* Rescan breakpoints at the same address and section, marking the
11951 first one as "first" and any others as "duplicates". This is so
11952 that the bpt instruction is only inserted once. If we have a
11953 permanent breakpoint at the same place as BPT, make that one the
11954 official one, and the rest as duplicates. Permanent breakpoints
11955 are sorted first for the same address.
11956
11957 Do the same for hardware watchpoints, but also considering the
11958 watchpoint's type (regular/access/read) and length. */
11959
11960 bp_loc_first = NULL;
11961 wp_loc_first = NULL;
11962 awp_loc_first = NULL;
11963 rwp_loc_first = NULL;
11964 ALL_BP_LOCATIONS (loc, locp)
11965 {
11966 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11967 non-NULL. */
11968 struct bp_location **loc_first_p;
11969 b = loc->owner;
11970
11971 if (!unduplicated_should_be_inserted (loc)
11972 || !bl_address_is_meaningful (loc)
11973 /* Don't detect duplicate for tracepoint locations because they are
11974 never duplicated. See the comments in field `duplicate' of
11975 `struct bp_location'. */
11976 || is_tracepoint (b))
11977 {
11978 /* Clear the condition modification flag. */
11979 loc->condition_changed = condition_unchanged;
11980 continue;
11981 }
11982
11983 if (b->type == bp_hardware_watchpoint)
11984 loc_first_p = &wp_loc_first;
11985 else if (b->type == bp_read_watchpoint)
11986 loc_first_p = &rwp_loc_first;
11987 else if (b->type == bp_access_watchpoint)
11988 loc_first_p = &awp_loc_first;
11989 else
11990 loc_first_p = &bp_loc_first;
11991
11992 if (*loc_first_p == NULL
11993 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11994 || !breakpoint_locations_match (loc, *loc_first_p))
11995 {
11996 *loc_first_p = loc;
11997 loc->duplicate = 0;
11998
11999 if (is_breakpoint (loc->owner) && loc->condition_changed)
12000 {
12001 loc->needs_update = 1;
12002 /* Clear the condition modification flag. */
12003 loc->condition_changed = condition_unchanged;
12004 }
12005 continue;
12006 }
12007
12008
12009 /* This and the above ensure the invariant that the first location
12010 is not duplicated, and is the inserted one.
12011 All following are marked as duplicated, and are not inserted. */
12012 if (loc->inserted)
12013 swap_insertion (loc, *loc_first_p);
12014 loc->duplicate = 1;
12015
12016 /* Clear the condition modification flag. */
12017 loc->condition_changed = condition_unchanged;
12018 }
12019
12020 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12021 {
12022 if (insert_mode != UGLL_DONT_INSERT)
12023 insert_breakpoint_locations ();
12024 else
12025 {
12026 /* Even though the caller told us to not insert new
12027 locations, we may still need to update conditions on the
12028 target's side of breakpoints that were already inserted
12029 if the target is evaluating breakpoint conditions. We
12030 only update conditions for locations that are marked
12031 "needs_update". */
12032 update_inserted_breakpoint_locations ();
12033 }
12034 }
12035
12036 if (insert_mode != UGLL_DONT_INSERT)
12037 download_tracepoint_locations ();
12038 }
12039
12040 void
12041 breakpoint_retire_moribund (void)
12042 {
12043 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12044 {
12045 struct bp_location *loc = moribund_locations[ix];
12046 if (--(loc->events_till_retirement) == 0)
12047 {
12048 decref_bp_location (&loc);
12049 unordered_remove (moribund_locations, ix);
12050 --ix;
12051 }
12052 }
12053 }
12054
12055 static void
12056 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12057 {
12058
12059 try
12060 {
12061 update_global_location_list (insert_mode);
12062 }
12063 catch (const gdb_exception_error &e)
12064 {
12065 }
12066 }
12067
12068 /* Clear BKP from a BPS. */
12069
12070 static void
12071 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12072 {
12073 bpstat bs;
12074
12075 for (bs = bps; bs; bs = bs->next)
12076 if (bs->breakpoint_at == bpt)
12077 {
12078 bs->breakpoint_at = NULL;
12079 bs->old_val = NULL;
12080 /* bs->commands will be freed later. */
12081 }
12082 }
12083
12084 /* Callback for iterate_over_threads. */
12085 static int
12086 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12087 {
12088 struct breakpoint *bpt = (struct breakpoint *) data;
12089
12090 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12091 return 0;
12092 }
12093
12094 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12095 callbacks. */
12096
12097 static void
12098 say_where (struct breakpoint *b)
12099 {
12100 struct value_print_options opts;
12101
12102 get_user_print_options (&opts);
12103
12104 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12105 single string. */
12106 if (b->loc == NULL)
12107 {
12108 /* For pending locations, the output differs slightly based
12109 on b->extra_string. If this is non-NULL, it contains either
12110 a condition or dprintf arguments. */
12111 if (b->extra_string == NULL)
12112 {
12113 printf_filtered (_(" (%s) pending."),
12114 event_location_to_string (b->location.get ()));
12115 }
12116 else if (b->type == bp_dprintf)
12117 {
12118 printf_filtered (_(" (%s,%s) pending."),
12119 event_location_to_string (b->location.get ()),
12120 b->extra_string);
12121 }
12122 else
12123 {
12124 printf_filtered (_(" (%s %s) pending."),
12125 event_location_to_string (b->location.get ()),
12126 b->extra_string);
12127 }
12128 }
12129 else
12130 {
12131 if (opts.addressprint || b->loc->symtab == NULL)
12132 printf_filtered (" at %ps",
12133 styled_string (address_style.style (),
12134 paddress (b->loc->gdbarch,
12135 b->loc->address)));
12136 if (b->loc->symtab != NULL)
12137 {
12138 /* If there is a single location, we can print the location
12139 more nicely. */
12140 if (b->loc->next == NULL)
12141 {
12142 const char *filename
12143 = symtab_to_filename_for_display (b->loc->symtab);
12144 printf_filtered (": file %ps, line %d.",
12145 styled_string (file_name_style.style (),
12146 filename),
12147 b->loc->line_number);
12148 }
12149 else
12150 /* This is not ideal, but each location may have a
12151 different file name, and this at least reflects the
12152 real situation somewhat. */
12153 printf_filtered (": %s.",
12154 event_location_to_string (b->location.get ()));
12155 }
12156
12157 if (b->loc->next)
12158 {
12159 struct bp_location *loc = b->loc;
12160 int n = 0;
12161 for (; loc; loc = loc->next)
12162 ++n;
12163 printf_filtered (" (%d locations)", n);
12164 }
12165 }
12166 }
12167
12168 bp_location::~bp_location ()
12169 {
12170 xfree (function_name);
12171 }
12172
12173 /* Destructor for the breakpoint base class. */
12174
12175 breakpoint::~breakpoint ()
12176 {
12177 xfree (this->cond_string);
12178 xfree (this->extra_string);
12179 }
12180
12181 static struct bp_location *
12182 base_breakpoint_allocate_location (struct breakpoint *self)
12183 {
12184 return new bp_location (self);
12185 }
12186
12187 static void
12188 base_breakpoint_re_set (struct breakpoint *b)
12189 {
12190 /* Nothing to re-set. */
12191 }
12192
12193 #define internal_error_pure_virtual_called() \
12194 gdb_assert_not_reached ("pure virtual function called")
12195
12196 static int
12197 base_breakpoint_insert_location (struct bp_location *bl)
12198 {
12199 internal_error_pure_virtual_called ();
12200 }
12201
12202 static int
12203 base_breakpoint_remove_location (struct bp_location *bl,
12204 enum remove_bp_reason reason)
12205 {
12206 internal_error_pure_virtual_called ();
12207 }
12208
12209 static int
12210 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12211 const address_space *aspace,
12212 CORE_ADDR bp_addr,
12213 const struct target_waitstatus *ws)
12214 {
12215 internal_error_pure_virtual_called ();
12216 }
12217
12218 static void
12219 base_breakpoint_check_status (bpstat bs)
12220 {
12221 /* Always stop. */
12222 }
12223
12224 /* A "works_in_software_mode" breakpoint_ops method that just internal
12225 errors. */
12226
12227 static int
12228 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12229 {
12230 internal_error_pure_virtual_called ();
12231 }
12232
12233 /* A "resources_needed" breakpoint_ops method that just internal
12234 errors. */
12235
12236 static int
12237 base_breakpoint_resources_needed (const struct bp_location *bl)
12238 {
12239 internal_error_pure_virtual_called ();
12240 }
12241
12242 static enum print_stop_action
12243 base_breakpoint_print_it (bpstat bs)
12244 {
12245 internal_error_pure_virtual_called ();
12246 }
12247
12248 static void
12249 base_breakpoint_print_one_detail (const struct breakpoint *self,
12250 struct ui_out *uiout)
12251 {
12252 /* nothing */
12253 }
12254
12255 static void
12256 base_breakpoint_print_mention (struct breakpoint *b)
12257 {
12258 internal_error_pure_virtual_called ();
12259 }
12260
12261 static void
12262 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12263 {
12264 internal_error_pure_virtual_called ();
12265 }
12266
12267 static void
12268 base_breakpoint_create_sals_from_location
12269 (struct event_location *location,
12270 struct linespec_result *canonical,
12271 enum bptype type_wanted)
12272 {
12273 internal_error_pure_virtual_called ();
12274 }
12275
12276 static void
12277 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12278 struct linespec_result *c,
12279 gdb::unique_xmalloc_ptr<char> cond_string,
12280 gdb::unique_xmalloc_ptr<char> extra_string,
12281 enum bptype type_wanted,
12282 enum bpdisp disposition,
12283 int thread,
12284 int task, int ignore_count,
12285 const struct breakpoint_ops *o,
12286 int from_tty, int enabled,
12287 int internal, unsigned flags)
12288 {
12289 internal_error_pure_virtual_called ();
12290 }
12291
12292 static std::vector<symtab_and_line>
12293 base_breakpoint_decode_location (struct breakpoint *b,
12294 struct event_location *location,
12295 struct program_space *search_pspace)
12296 {
12297 internal_error_pure_virtual_called ();
12298 }
12299
12300 /* The default 'explains_signal' method. */
12301
12302 static int
12303 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12304 {
12305 return 1;
12306 }
12307
12308 /* The default "after_condition_true" method. */
12309
12310 static void
12311 base_breakpoint_after_condition_true (struct bpstats *bs)
12312 {
12313 /* Nothing to do. */
12314 }
12315
12316 struct breakpoint_ops base_breakpoint_ops =
12317 {
12318 base_breakpoint_allocate_location,
12319 base_breakpoint_re_set,
12320 base_breakpoint_insert_location,
12321 base_breakpoint_remove_location,
12322 base_breakpoint_breakpoint_hit,
12323 base_breakpoint_check_status,
12324 base_breakpoint_resources_needed,
12325 base_breakpoint_works_in_software_mode,
12326 base_breakpoint_print_it,
12327 NULL,
12328 base_breakpoint_print_one_detail,
12329 base_breakpoint_print_mention,
12330 base_breakpoint_print_recreate,
12331 base_breakpoint_create_sals_from_location,
12332 base_breakpoint_create_breakpoints_sal,
12333 base_breakpoint_decode_location,
12334 base_breakpoint_explains_signal,
12335 base_breakpoint_after_condition_true,
12336 };
12337
12338 /* Default breakpoint_ops methods. */
12339
12340 static void
12341 bkpt_re_set (struct breakpoint *b)
12342 {
12343 /* FIXME: is this still reachable? */
12344 if (breakpoint_event_location_empty_p (b))
12345 {
12346 /* Anything without a location can't be re-set. */
12347 delete_breakpoint (b);
12348 return;
12349 }
12350
12351 breakpoint_re_set_default (b);
12352 }
12353
12354 static int
12355 bkpt_insert_location (struct bp_location *bl)
12356 {
12357 CORE_ADDR addr = bl->target_info.reqstd_address;
12358
12359 bl->target_info.kind = breakpoint_kind (bl, &addr);
12360 bl->target_info.placed_address = addr;
12361
12362 if (bl->loc_type == bp_loc_hardware_breakpoint)
12363 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12364 else
12365 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12366 }
12367
12368 static int
12369 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12370 {
12371 if (bl->loc_type == bp_loc_hardware_breakpoint)
12372 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12373 else
12374 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12375 }
12376
12377 static int
12378 bkpt_breakpoint_hit (const struct bp_location *bl,
12379 const address_space *aspace, CORE_ADDR bp_addr,
12380 const struct target_waitstatus *ws)
12381 {
12382 if (ws->kind != TARGET_WAITKIND_STOPPED
12383 || ws->value.sig != GDB_SIGNAL_TRAP)
12384 return 0;
12385
12386 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12387 aspace, bp_addr))
12388 return 0;
12389
12390 if (overlay_debugging /* unmapped overlay section */
12391 && section_is_overlay (bl->section)
12392 && !section_is_mapped (bl->section))
12393 return 0;
12394
12395 return 1;
12396 }
12397
12398 static int
12399 dprintf_breakpoint_hit (const struct bp_location *bl,
12400 const address_space *aspace, CORE_ADDR bp_addr,
12401 const struct target_waitstatus *ws)
12402 {
12403 if (dprintf_style == dprintf_style_agent
12404 && target_can_run_breakpoint_commands ())
12405 {
12406 /* An agent-style dprintf never causes a stop. If we see a trap
12407 for this address it must be for a breakpoint that happens to
12408 be set at the same address. */
12409 return 0;
12410 }
12411
12412 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12413 }
12414
12415 static int
12416 bkpt_resources_needed (const struct bp_location *bl)
12417 {
12418 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12419
12420 return 1;
12421 }
12422
12423 static enum print_stop_action
12424 bkpt_print_it (bpstat bs)
12425 {
12426 struct breakpoint *b;
12427 const struct bp_location *bl;
12428 int bp_temp;
12429 struct ui_out *uiout = current_uiout;
12430
12431 gdb_assert (bs->bp_location_at != NULL);
12432
12433 bl = bs->bp_location_at;
12434 b = bs->breakpoint_at;
12435
12436 bp_temp = b->disposition == disp_del;
12437 if (bl->address != bl->requested_address)
12438 breakpoint_adjustment_warning (bl->requested_address,
12439 bl->address,
12440 b->number, 1);
12441 annotate_breakpoint (b->number);
12442 maybe_print_thread_hit_breakpoint (uiout);
12443
12444 if (uiout->is_mi_like_p ())
12445 {
12446 uiout->field_string ("reason",
12447 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12448 uiout->field_string ("disp", bpdisp_text (b->disposition));
12449 }
12450 if (bp_temp)
12451 uiout->message ("Temporary breakpoint %pF, ",
12452 signed_field ("bkptno", b->number));
12453 else
12454 uiout->message ("Breakpoint %pF, ",
12455 signed_field ("bkptno", b->number));
12456
12457 return PRINT_SRC_AND_LOC;
12458 }
12459
12460 static void
12461 bkpt_print_mention (struct breakpoint *b)
12462 {
12463 if (current_uiout->is_mi_like_p ())
12464 return;
12465
12466 switch (b->type)
12467 {
12468 case bp_breakpoint:
12469 case bp_gnu_ifunc_resolver:
12470 if (b->disposition == disp_del)
12471 printf_filtered (_("Temporary breakpoint"));
12472 else
12473 printf_filtered (_("Breakpoint"));
12474 printf_filtered (_(" %d"), b->number);
12475 if (b->type == bp_gnu_ifunc_resolver)
12476 printf_filtered (_(" at gnu-indirect-function resolver"));
12477 break;
12478 case bp_hardware_breakpoint:
12479 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12480 break;
12481 case bp_dprintf:
12482 printf_filtered (_("Dprintf %d"), b->number);
12483 break;
12484 }
12485
12486 say_where (b);
12487 }
12488
12489 static void
12490 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12491 {
12492 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12493 fprintf_unfiltered (fp, "tbreak");
12494 else if (tp->type == bp_breakpoint)
12495 fprintf_unfiltered (fp, "break");
12496 else if (tp->type == bp_hardware_breakpoint
12497 && tp->disposition == disp_del)
12498 fprintf_unfiltered (fp, "thbreak");
12499 else if (tp->type == bp_hardware_breakpoint)
12500 fprintf_unfiltered (fp, "hbreak");
12501 else
12502 internal_error (__FILE__, __LINE__,
12503 _("unhandled breakpoint type %d"), (int) tp->type);
12504
12505 fprintf_unfiltered (fp, " %s",
12506 event_location_to_string (tp->location.get ()));
12507
12508 /* Print out extra_string if this breakpoint is pending. It might
12509 contain, for example, conditions that were set by the user. */
12510 if (tp->loc == NULL && tp->extra_string != NULL)
12511 fprintf_unfiltered (fp, " %s", tp->extra_string);
12512
12513 print_recreate_thread (tp, fp);
12514 }
12515
12516 static void
12517 bkpt_create_sals_from_location (struct event_location *location,
12518 struct linespec_result *canonical,
12519 enum bptype type_wanted)
12520 {
12521 create_sals_from_location_default (location, canonical, type_wanted);
12522 }
12523
12524 static void
12525 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12526 struct linespec_result *canonical,
12527 gdb::unique_xmalloc_ptr<char> cond_string,
12528 gdb::unique_xmalloc_ptr<char> extra_string,
12529 enum bptype type_wanted,
12530 enum bpdisp disposition,
12531 int thread,
12532 int task, int ignore_count,
12533 const struct breakpoint_ops *ops,
12534 int from_tty, int enabled,
12535 int internal, unsigned flags)
12536 {
12537 create_breakpoints_sal_default (gdbarch, canonical,
12538 std::move (cond_string),
12539 std::move (extra_string),
12540 type_wanted,
12541 disposition, thread, task,
12542 ignore_count, ops, from_tty,
12543 enabled, internal, flags);
12544 }
12545
12546 static std::vector<symtab_and_line>
12547 bkpt_decode_location (struct breakpoint *b,
12548 struct event_location *location,
12549 struct program_space *search_pspace)
12550 {
12551 return decode_location_default (b, location, search_pspace);
12552 }
12553
12554 /* Virtual table for internal breakpoints. */
12555
12556 static void
12557 internal_bkpt_re_set (struct breakpoint *b)
12558 {
12559 switch (b->type)
12560 {
12561 /* Delete overlay event and longjmp master breakpoints; they
12562 will be reset later by breakpoint_re_set. */
12563 case bp_overlay_event:
12564 case bp_longjmp_master:
12565 case bp_std_terminate_master:
12566 case bp_exception_master:
12567 delete_breakpoint (b);
12568 break;
12569
12570 /* This breakpoint is special, it's set up when the inferior
12571 starts and we really don't want to touch it. */
12572 case bp_shlib_event:
12573
12574 /* Like bp_shlib_event, this breakpoint type is special. Once
12575 it is set up, we do not want to touch it. */
12576 case bp_thread_event:
12577 break;
12578 }
12579 }
12580
12581 static void
12582 internal_bkpt_check_status (bpstat bs)
12583 {
12584 if (bs->breakpoint_at->type == bp_shlib_event)
12585 {
12586 /* If requested, stop when the dynamic linker notifies GDB of
12587 events. This allows the user to get control and place
12588 breakpoints in initializer routines for dynamically loaded
12589 objects (among other things). */
12590 bs->stop = stop_on_solib_events;
12591 bs->print = stop_on_solib_events;
12592 }
12593 else
12594 bs->stop = 0;
12595 }
12596
12597 static enum print_stop_action
12598 internal_bkpt_print_it (bpstat bs)
12599 {
12600 struct breakpoint *b;
12601
12602 b = bs->breakpoint_at;
12603
12604 switch (b->type)
12605 {
12606 case bp_shlib_event:
12607 /* Did we stop because the user set the stop_on_solib_events
12608 variable? (If so, we report this as a generic, "Stopped due
12609 to shlib event" message.) */
12610 print_solib_event (0);
12611 break;
12612
12613 case bp_thread_event:
12614 /* Not sure how we will get here.
12615 GDB should not stop for these breakpoints. */
12616 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12617 break;
12618
12619 case bp_overlay_event:
12620 /* By analogy with the thread event, GDB should not stop for these. */
12621 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12622 break;
12623
12624 case bp_longjmp_master:
12625 /* These should never be enabled. */
12626 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12627 break;
12628
12629 case bp_std_terminate_master:
12630 /* These should never be enabled. */
12631 printf_filtered (_("std::terminate Master Breakpoint: "
12632 "gdb should not stop!\n"));
12633 break;
12634
12635 case bp_exception_master:
12636 /* These should never be enabled. */
12637 printf_filtered (_("Exception Master Breakpoint: "
12638 "gdb should not stop!\n"));
12639 break;
12640 }
12641
12642 return PRINT_NOTHING;
12643 }
12644
12645 static void
12646 internal_bkpt_print_mention (struct breakpoint *b)
12647 {
12648 /* Nothing to mention. These breakpoints are internal. */
12649 }
12650
12651 /* Virtual table for momentary breakpoints */
12652
12653 static void
12654 momentary_bkpt_re_set (struct breakpoint *b)
12655 {
12656 /* Keep temporary breakpoints, which can be encountered when we step
12657 over a dlopen call and solib_add is resetting the breakpoints.
12658 Otherwise these should have been blown away via the cleanup chain
12659 or by breakpoint_init_inferior when we rerun the executable. */
12660 }
12661
12662 static void
12663 momentary_bkpt_check_status (bpstat bs)
12664 {
12665 /* Nothing. The point of these breakpoints is causing a stop. */
12666 }
12667
12668 static enum print_stop_action
12669 momentary_bkpt_print_it (bpstat bs)
12670 {
12671 return PRINT_UNKNOWN;
12672 }
12673
12674 static void
12675 momentary_bkpt_print_mention (struct breakpoint *b)
12676 {
12677 /* Nothing to mention. These breakpoints are internal. */
12678 }
12679
12680 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12681
12682 It gets cleared already on the removal of the first one of such placed
12683 breakpoints. This is OK as they get all removed altogether. */
12684
12685 longjmp_breakpoint::~longjmp_breakpoint ()
12686 {
12687 thread_info *tp = find_thread_global_id (this->thread);
12688
12689 if (tp != NULL)
12690 tp->initiating_frame = null_frame_id;
12691 }
12692
12693 /* Specific methods for probe breakpoints. */
12694
12695 static int
12696 bkpt_probe_insert_location (struct bp_location *bl)
12697 {
12698 int v = bkpt_insert_location (bl);
12699
12700 if (v == 0)
12701 {
12702 /* The insertion was successful, now let's set the probe's semaphore
12703 if needed. */
12704 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
12705 }
12706
12707 return v;
12708 }
12709
12710 static int
12711 bkpt_probe_remove_location (struct bp_location *bl,
12712 enum remove_bp_reason reason)
12713 {
12714 /* Let's clear the semaphore before removing the location. */
12715 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
12716
12717 return bkpt_remove_location (bl, reason);
12718 }
12719
12720 static void
12721 bkpt_probe_create_sals_from_location (struct event_location *location,
12722 struct linespec_result *canonical,
12723 enum bptype type_wanted)
12724 {
12725 struct linespec_sals lsal;
12726
12727 lsal.sals = parse_probes (location, NULL, canonical);
12728 lsal.canonical
12729 = xstrdup (event_location_to_string (canonical->location.get ()));
12730 canonical->lsals.push_back (std::move (lsal));
12731 }
12732
12733 static std::vector<symtab_and_line>
12734 bkpt_probe_decode_location (struct breakpoint *b,
12735 struct event_location *location,
12736 struct program_space *search_pspace)
12737 {
12738 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12739 if (sals.empty ())
12740 error (_("probe not found"));
12741 return sals;
12742 }
12743
12744 /* The breakpoint_ops structure to be used in tracepoints. */
12745
12746 static void
12747 tracepoint_re_set (struct breakpoint *b)
12748 {
12749 breakpoint_re_set_default (b);
12750 }
12751
12752 static int
12753 tracepoint_breakpoint_hit (const struct bp_location *bl,
12754 const address_space *aspace, CORE_ADDR bp_addr,
12755 const struct target_waitstatus *ws)
12756 {
12757 /* By definition, the inferior does not report stops at
12758 tracepoints. */
12759 return 0;
12760 }
12761
12762 static void
12763 tracepoint_print_one_detail (const struct breakpoint *self,
12764 struct ui_out *uiout)
12765 {
12766 struct tracepoint *tp = (struct tracepoint *) self;
12767 if (!tp->static_trace_marker_id.empty ())
12768 {
12769 gdb_assert (self->type == bp_static_tracepoint);
12770
12771 uiout->message ("\tmarker id is %pF\n",
12772 string_field ("static-tracepoint-marker-string-id",
12773 tp->static_trace_marker_id.c_str ()));
12774 }
12775 }
12776
12777 static void
12778 tracepoint_print_mention (struct breakpoint *b)
12779 {
12780 if (current_uiout->is_mi_like_p ())
12781 return;
12782
12783 switch (b->type)
12784 {
12785 case bp_tracepoint:
12786 printf_filtered (_("Tracepoint"));
12787 printf_filtered (_(" %d"), b->number);
12788 break;
12789 case bp_fast_tracepoint:
12790 printf_filtered (_("Fast tracepoint"));
12791 printf_filtered (_(" %d"), b->number);
12792 break;
12793 case bp_static_tracepoint:
12794 printf_filtered (_("Static tracepoint"));
12795 printf_filtered (_(" %d"), b->number);
12796 break;
12797 default:
12798 internal_error (__FILE__, __LINE__,
12799 _("unhandled tracepoint type %d"), (int) b->type);
12800 }
12801
12802 say_where (b);
12803 }
12804
12805 static void
12806 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
12807 {
12808 struct tracepoint *tp = (struct tracepoint *) self;
12809
12810 if (self->type == bp_fast_tracepoint)
12811 fprintf_unfiltered (fp, "ftrace");
12812 else if (self->type == bp_static_tracepoint)
12813 fprintf_unfiltered (fp, "strace");
12814 else if (self->type == bp_tracepoint)
12815 fprintf_unfiltered (fp, "trace");
12816 else
12817 internal_error (__FILE__, __LINE__,
12818 _("unhandled tracepoint type %d"), (int) self->type);
12819
12820 fprintf_unfiltered (fp, " %s",
12821 event_location_to_string (self->location.get ()));
12822 print_recreate_thread (self, fp);
12823
12824 if (tp->pass_count)
12825 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
12826 }
12827
12828 static void
12829 tracepoint_create_sals_from_location (struct event_location *location,
12830 struct linespec_result *canonical,
12831 enum bptype type_wanted)
12832 {
12833 create_sals_from_location_default (location, canonical, type_wanted);
12834 }
12835
12836 static void
12837 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12838 struct linespec_result *canonical,
12839 gdb::unique_xmalloc_ptr<char> cond_string,
12840 gdb::unique_xmalloc_ptr<char> extra_string,
12841 enum bptype type_wanted,
12842 enum bpdisp disposition,
12843 int thread,
12844 int task, int ignore_count,
12845 const struct breakpoint_ops *ops,
12846 int from_tty, int enabled,
12847 int internal, unsigned flags)
12848 {
12849 create_breakpoints_sal_default (gdbarch, canonical,
12850 std::move (cond_string),
12851 std::move (extra_string),
12852 type_wanted,
12853 disposition, thread, task,
12854 ignore_count, ops, from_tty,
12855 enabled, internal, flags);
12856 }
12857
12858 static std::vector<symtab_and_line>
12859 tracepoint_decode_location (struct breakpoint *b,
12860 struct event_location *location,
12861 struct program_space *search_pspace)
12862 {
12863 return decode_location_default (b, location, search_pspace);
12864 }
12865
12866 struct breakpoint_ops tracepoint_breakpoint_ops;
12867
12868 /* Virtual table for tracepoints on static probes. */
12869
12870 static void
12871 tracepoint_probe_create_sals_from_location
12872 (struct event_location *location,
12873 struct linespec_result *canonical,
12874 enum bptype type_wanted)
12875 {
12876 /* We use the same method for breakpoint on probes. */
12877 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
12878 }
12879
12880 static std::vector<symtab_and_line>
12881 tracepoint_probe_decode_location (struct breakpoint *b,
12882 struct event_location *location,
12883 struct program_space *search_pspace)
12884 {
12885 /* We use the same method for breakpoint on probes. */
12886 return bkpt_probe_decode_location (b, location, search_pspace);
12887 }
12888
12889 /* Dprintf breakpoint_ops methods. */
12890
12891 static void
12892 dprintf_re_set (struct breakpoint *b)
12893 {
12894 breakpoint_re_set_default (b);
12895
12896 /* extra_string should never be non-NULL for dprintf. */
12897 gdb_assert (b->extra_string != NULL);
12898
12899 /* 1 - connect to target 1, that can run breakpoint commands.
12900 2 - create a dprintf, which resolves fine.
12901 3 - disconnect from target 1
12902 4 - connect to target 2, that can NOT run breakpoint commands.
12903
12904 After steps #3/#4, you'll want the dprintf command list to
12905 be updated, because target 1 and 2 may well return different
12906 answers for target_can_run_breakpoint_commands().
12907 Given absence of finer grained resetting, we get to do
12908 it all the time. */
12909 if (b->extra_string != NULL)
12910 update_dprintf_command_list (b);
12911 }
12912
12913 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
12914
12915 static void
12916 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12917 {
12918 fprintf_unfiltered (fp, "dprintf %s,%s",
12919 event_location_to_string (tp->location.get ()),
12920 tp->extra_string);
12921 print_recreate_thread (tp, fp);
12922 }
12923
12924 /* Implement the "after_condition_true" breakpoint_ops method for
12925 dprintf.
12926
12927 dprintf's are implemented with regular commands in their command
12928 list, but we run the commands here instead of before presenting the
12929 stop to the user, as dprintf's don't actually cause a stop. This
12930 also makes it so that the commands of multiple dprintfs at the same
12931 address are all handled. */
12932
12933 static void
12934 dprintf_after_condition_true (struct bpstats *bs)
12935 {
12936 struct bpstats tmp_bs;
12937 struct bpstats *tmp_bs_p = &tmp_bs;
12938
12939 /* dprintf's never cause a stop. This wasn't set in the
12940 check_status hook instead because that would make the dprintf's
12941 condition not be evaluated. */
12942 bs->stop = 0;
12943
12944 /* Run the command list here. Take ownership of it instead of
12945 copying. We never want these commands to run later in
12946 bpstat_do_actions, if a breakpoint that causes a stop happens to
12947 be set at same address as this dprintf, or even if running the
12948 commands here throws. */
12949 tmp_bs.commands = bs->commands;
12950 bs->commands = NULL;
12951
12952 bpstat_do_actions_1 (&tmp_bs_p);
12953
12954 /* 'tmp_bs.commands' will usually be NULL by now, but
12955 bpstat_do_actions_1 may return early without processing the whole
12956 list. */
12957 }
12958
12959 /* The breakpoint_ops structure to be used on static tracepoints with
12960 markers (`-m'). */
12961
12962 static void
12963 strace_marker_create_sals_from_location (struct event_location *location,
12964 struct linespec_result *canonical,
12965 enum bptype type_wanted)
12966 {
12967 struct linespec_sals lsal;
12968 const char *arg_start, *arg;
12969
12970 arg = arg_start = get_linespec_location (location)->spec_string;
12971 lsal.sals = decode_static_tracepoint_spec (&arg);
12972
12973 std::string str (arg_start, arg - arg_start);
12974 const char *ptr = str.c_str ();
12975 canonical->location
12976 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
12977
12978 lsal.canonical
12979 = xstrdup (event_location_to_string (canonical->location.get ()));
12980 canonical->lsals.push_back (std::move (lsal));
12981 }
12982
12983 static void
12984 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12985 struct linespec_result *canonical,
12986 gdb::unique_xmalloc_ptr<char> cond_string,
12987 gdb::unique_xmalloc_ptr<char> extra_string,
12988 enum bptype type_wanted,
12989 enum bpdisp disposition,
12990 int thread,
12991 int task, int ignore_count,
12992 const struct breakpoint_ops *ops,
12993 int from_tty, int enabled,
12994 int internal, unsigned flags)
12995 {
12996 const linespec_sals &lsal = canonical->lsals[0];
12997
12998 /* If the user is creating a static tracepoint by marker id
12999 (strace -m MARKER_ID), then store the sals index, so that
13000 breakpoint_re_set can try to match up which of the newly
13001 found markers corresponds to this one, and, don't try to
13002 expand multiple locations for each sal, given than SALS
13003 already should contain all sals for MARKER_ID. */
13004
13005 for (size_t i = 0; i < lsal.sals.size (); i++)
13006 {
13007 event_location_up location
13008 = copy_event_location (canonical->location.get ());
13009
13010 std::unique_ptr<tracepoint> tp (new tracepoint ());
13011 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
13012 std::move (location), NULL,
13013 std::move (cond_string),
13014 std::move (extra_string),
13015 type_wanted, disposition,
13016 thread, task, ignore_count, ops,
13017 from_tty, enabled, internal, flags,
13018 canonical->special_display);
13019 /* Given that its possible to have multiple markers with
13020 the same string id, if the user is creating a static
13021 tracepoint by marker id ("strace -m MARKER_ID"), then
13022 store the sals index, so that breakpoint_re_set can
13023 try to match up which of the newly found markers
13024 corresponds to this one */
13025 tp->static_trace_marker_id_idx = i;
13026
13027 install_breakpoint (internal, std::move (tp), 0);
13028 }
13029 }
13030
13031 static std::vector<symtab_and_line>
13032 strace_marker_decode_location (struct breakpoint *b,
13033 struct event_location *location,
13034 struct program_space *search_pspace)
13035 {
13036 struct tracepoint *tp = (struct tracepoint *) b;
13037 const char *s = get_linespec_location (location)->spec_string;
13038
13039 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13040 if (sals.size () > tp->static_trace_marker_id_idx)
13041 {
13042 sals[0] = sals[tp->static_trace_marker_id_idx];
13043 sals.resize (1);
13044 return sals;
13045 }
13046 else
13047 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
13048 }
13049
13050 static struct breakpoint_ops strace_marker_breakpoint_ops;
13051
13052 static int
13053 strace_marker_p (struct breakpoint *b)
13054 {
13055 return b->ops == &strace_marker_breakpoint_ops;
13056 }
13057
13058 /* Delete a breakpoint and clean up all traces of it in the data
13059 structures. */
13060
13061 void
13062 delete_breakpoint (struct breakpoint *bpt)
13063 {
13064 struct breakpoint *b;
13065
13066 gdb_assert (bpt != NULL);
13067
13068 /* Has this bp already been deleted? This can happen because
13069 multiple lists can hold pointers to bp's. bpstat lists are
13070 especial culprits.
13071
13072 One example of this happening is a watchpoint's scope bp. When
13073 the scope bp triggers, we notice that the watchpoint is out of
13074 scope, and delete it. We also delete its scope bp. But the
13075 scope bp is marked "auto-deleting", and is already on a bpstat.
13076 That bpstat is then checked for auto-deleting bp's, which are
13077 deleted.
13078
13079 A real solution to this problem might involve reference counts in
13080 bp's, and/or giving them pointers back to their referencing
13081 bpstat's, and teaching delete_breakpoint to only free a bp's
13082 storage when no more references were extent. A cheaper bandaid
13083 was chosen. */
13084 if (bpt->type == bp_none)
13085 return;
13086
13087 /* At least avoid this stale reference until the reference counting
13088 of breakpoints gets resolved. */
13089 if (bpt->related_breakpoint != bpt)
13090 {
13091 struct breakpoint *related;
13092 struct watchpoint *w;
13093
13094 if (bpt->type == bp_watchpoint_scope)
13095 w = (struct watchpoint *) bpt->related_breakpoint;
13096 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13097 w = (struct watchpoint *) bpt;
13098 else
13099 w = NULL;
13100 if (w != NULL)
13101 watchpoint_del_at_next_stop (w);
13102
13103 /* Unlink bpt from the bpt->related_breakpoint ring. */
13104 for (related = bpt; related->related_breakpoint != bpt;
13105 related = related->related_breakpoint);
13106 related->related_breakpoint = bpt->related_breakpoint;
13107 bpt->related_breakpoint = bpt;
13108 }
13109
13110 /* watch_command_1 creates a watchpoint but only sets its number if
13111 update_watchpoint succeeds in creating its bp_locations. If there's
13112 a problem in that process, we'll be asked to delete the half-created
13113 watchpoint. In that case, don't announce the deletion. */
13114 if (bpt->number)
13115 gdb::observers::breakpoint_deleted.notify (bpt);
13116
13117 if (breakpoint_chain == bpt)
13118 breakpoint_chain = bpt->next;
13119
13120 ALL_BREAKPOINTS (b)
13121 if (b->next == bpt)
13122 {
13123 b->next = bpt->next;
13124 break;
13125 }
13126
13127 /* Be sure no bpstat's are pointing at the breakpoint after it's
13128 been freed. */
13129 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13130 in all threads for now. Note that we cannot just remove bpstats
13131 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13132 commands are associated with the bpstat; if we remove it here,
13133 then the later call to bpstat_do_actions (&stop_bpstat); in
13134 event-top.c won't do anything, and temporary breakpoints with
13135 commands won't work. */
13136
13137 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13138
13139 /* Now that breakpoint is removed from breakpoint list, update the
13140 global location list. This will remove locations that used to
13141 belong to this breakpoint. Do this before freeing the breakpoint
13142 itself, since remove_breakpoint looks at location's owner. It
13143 might be better design to have location completely
13144 self-contained, but it's not the case now. */
13145 update_global_location_list (UGLL_DONT_INSERT);
13146
13147 /* On the chance that someone will soon try again to delete this
13148 same bp, we mark it as deleted before freeing its storage. */
13149 bpt->type = bp_none;
13150 delete bpt;
13151 }
13152
13153 /* Iterator function to call a user-provided callback function once
13154 for each of B and its related breakpoints. */
13155
13156 static void
13157 iterate_over_related_breakpoints (struct breakpoint *b,
13158 gdb::function_view<void (breakpoint *)> function)
13159 {
13160 struct breakpoint *related;
13161
13162 related = b;
13163 do
13164 {
13165 struct breakpoint *next;
13166
13167 /* FUNCTION may delete RELATED. */
13168 next = related->related_breakpoint;
13169
13170 if (next == related)
13171 {
13172 /* RELATED is the last ring entry. */
13173 function (related);
13174
13175 /* FUNCTION may have deleted it, so we'd never reach back to
13176 B. There's nothing left to do anyway, so just break
13177 out. */
13178 break;
13179 }
13180 else
13181 function (related);
13182
13183 related = next;
13184 }
13185 while (related != b);
13186 }
13187
13188 static void
13189 delete_command (const char *arg, int from_tty)
13190 {
13191 struct breakpoint *b, *b_tmp;
13192
13193 dont_repeat ();
13194
13195 if (arg == 0)
13196 {
13197 int breaks_to_delete = 0;
13198
13199 /* Delete all breakpoints if no argument. Do not delete
13200 internal breakpoints, these have to be deleted with an
13201 explicit breakpoint number argument. */
13202 ALL_BREAKPOINTS (b)
13203 if (user_breakpoint_p (b))
13204 {
13205 breaks_to_delete = 1;
13206 break;
13207 }
13208
13209 /* Ask user only if there are some breakpoints to delete. */
13210 if (!from_tty
13211 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13212 {
13213 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13214 if (user_breakpoint_p (b))
13215 delete_breakpoint (b);
13216 }
13217 }
13218 else
13219 map_breakpoint_numbers
13220 (arg, [&] (breakpoint *br)
13221 {
13222 iterate_over_related_breakpoints (br, delete_breakpoint);
13223 });
13224 }
13225
13226 /* Return true if all locations of B bound to PSPACE are pending. If
13227 PSPACE is NULL, all locations of all program spaces are
13228 considered. */
13229
13230 static int
13231 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13232 {
13233 struct bp_location *loc;
13234
13235 for (loc = b->loc; loc != NULL; loc = loc->next)
13236 if ((pspace == NULL
13237 || loc->pspace == pspace)
13238 && !loc->shlib_disabled
13239 && !loc->pspace->executing_startup)
13240 return 0;
13241 return 1;
13242 }
13243
13244 /* Subroutine of update_breakpoint_locations to simplify it.
13245 Return non-zero if multiple fns in list LOC have the same name.
13246 Null names are ignored. */
13247
13248 static int
13249 ambiguous_names_p (struct bp_location *loc)
13250 {
13251 struct bp_location *l;
13252 htab_up htab (htab_create_alloc (13, htab_hash_string, streq_hash, NULL,
13253 xcalloc, xfree));
13254
13255 for (l = loc; l != NULL; l = l->next)
13256 {
13257 const char **slot;
13258 const char *name = l->function_name;
13259
13260 /* Allow for some names to be NULL, ignore them. */
13261 if (name == NULL)
13262 continue;
13263
13264 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
13265 INSERT);
13266 /* NOTE: We can assume slot != NULL here because xcalloc never
13267 returns NULL. */
13268 if (*slot != NULL)
13269 return 1;
13270 *slot = name;
13271 }
13272
13273 return 0;
13274 }
13275
13276 /* When symbols change, it probably means the sources changed as well,
13277 and it might mean the static tracepoint markers are no longer at
13278 the same address or line numbers they used to be at last we
13279 checked. Losing your static tracepoints whenever you rebuild is
13280 undesirable. This function tries to resync/rematch gdb static
13281 tracepoints with the markers on the target, for static tracepoints
13282 that have not been set by marker id. Static tracepoint that have
13283 been set by marker id are reset by marker id in breakpoint_re_set.
13284 The heuristic is:
13285
13286 1) For a tracepoint set at a specific address, look for a marker at
13287 the old PC. If one is found there, assume to be the same marker.
13288 If the name / string id of the marker found is different from the
13289 previous known name, assume that means the user renamed the marker
13290 in the sources, and output a warning.
13291
13292 2) For a tracepoint set at a given line number, look for a marker
13293 at the new address of the old line number. If one is found there,
13294 assume to be the same marker. If the name / string id of the
13295 marker found is different from the previous known name, assume that
13296 means the user renamed the marker in the sources, and output a
13297 warning.
13298
13299 3) If a marker is no longer found at the same address or line, it
13300 may mean the marker no longer exists. But it may also just mean
13301 the code changed a bit. Maybe the user added a few lines of code
13302 that made the marker move up or down (in line number terms). Ask
13303 the target for info about the marker with the string id as we knew
13304 it. If found, update line number and address in the matching
13305 static tracepoint. This will get confused if there's more than one
13306 marker with the same ID (possible in UST, although unadvised
13307 precisely because it confuses tools). */
13308
13309 static struct symtab_and_line
13310 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13311 {
13312 struct tracepoint *tp = (struct tracepoint *) b;
13313 struct static_tracepoint_marker marker;
13314 CORE_ADDR pc;
13315
13316 pc = sal.pc;
13317 if (sal.line)
13318 find_line_pc (sal.symtab, sal.line, &pc);
13319
13320 if (target_static_tracepoint_marker_at (pc, &marker))
13321 {
13322 if (tp->static_trace_marker_id != marker.str_id)
13323 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13324 b->number, tp->static_trace_marker_id.c_str (),
13325 marker.str_id.c_str ());
13326
13327 tp->static_trace_marker_id = std::move (marker.str_id);
13328
13329 return sal;
13330 }
13331
13332 /* Old marker wasn't found on target at lineno. Try looking it up
13333 by string ID. */
13334 if (!sal.explicit_pc
13335 && sal.line != 0
13336 && sal.symtab != NULL
13337 && !tp->static_trace_marker_id.empty ())
13338 {
13339 std::vector<static_tracepoint_marker> markers
13340 = target_static_tracepoint_markers_by_strid
13341 (tp->static_trace_marker_id.c_str ());
13342
13343 if (!markers.empty ())
13344 {
13345 struct symbol *sym;
13346 struct static_tracepoint_marker *tpmarker;
13347 struct ui_out *uiout = current_uiout;
13348 struct explicit_location explicit_loc;
13349
13350 tpmarker = &markers[0];
13351
13352 tp->static_trace_marker_id = std::move (tpmarker->str_id);
13353
13354 warning (_("marker for static tracepoint %d (%s) not "
13355 "found at previous line number"),
13356 b->number, tp->static_trace_marker_id.c_str ());
13357
13358 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
13359 sym = find_pc_sect_function (tpmarker->address, NULL);
13360 uiout->text ("Now in ");
13361 if (sym)
13362 {
13363 uiout->field_string ("func", sym->print_name (),
13364 function_name_style.style ());
13365 uiout->text (" at ");
13366 }
13367 uiout->field_string ("file",
13368 symtab_to_filename_for_display (sal2.symtab),
13369 file_name_style.style ());
13370 uiout->text (":");
13371
13372 if (uiout->is_mi_like_p ())
13373 {
13374 const char *fullname = symtab_to_fullname (sal2.symtab);
13375
13376 uiout->field_string ("fullname", fullname);
13377 }
13378
13379 uiout->field_signed ("line", sal2.line);
13380 uiout->text ("\n");
13381
13382 b->loc->line_number = sal2.line;
13383 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13384
13385 b->location.reset (NULL);
13386 initialize_explicit_location (&explicit_loc);
13387 explicit_loc.source_filename
13388 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13389 explicit_loc.line_offset.offset = b->loc->line_number;
13390 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13391 b->location = new_explicit_location (&explicit_loc);
13392
13393 /* Might be nice to check if function changed, and warn if
13394 so. */
13395 }
13396 }
13397 return sal;
13398 }
13399
13400 /* Returns 1 iff locations A and B are sufficiently same that
13401 we don't need to report breakpoint as changed. */
13402
13403 static int
13404 locations_are_equal (struct bp_location *a, struct bp_location *b)
13405 {
13406 while (a && b)
13407 {
13408 if (a->address != b->address)
13409 return 0;
13410
13411 if (a->shlib_disabled != b->shlib_disabled)
13412 return 0;
13413
13414 if (a->enabled != b->enabled)
13415 return 0;
13416
13417 a = a->next;
13418 b = b->next;
13419 }
13420
13421 if ((a == NULL) != (b == NULL))
13422 return 0;
13423
13424 return 1;
13425 }
13426
13427 /* Split all locations of B that are bound to PSPACE out of B's
13428 location list to a separate list and return that list's head. If
13429 PSPACE is NULL, hoist out all locations of B. */
13430
13431 static struct bp_location *
13432 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13433 {
13434 struct bp_location head;
13435 struct bp_location *i = b->loc;
13436 struct bp_location **i_link = &b->loc;
13437 struct bp_location *hoisted = &head;
13438
13439 if (pspace == NULL)
13440 {
13441 i = b->loc;
13442 b->loc = NULL;
13443 return i;
13444 }
13445
13446 head.next = NULL;
13447
13448 while (i != NULL)
13449 {
13450 if (i->pspace == pspace)
13451 {
13452 *i_link = i->next;
13453 i->next = NULL;
13454 hoisted->next = i;
13455 hoisted = i;
13456 }
13457 else
13458 i_link = &i->next;
13459 i = *i_link;
13460 }
13461
13462 return head.next;
13463 }
13464
13465 /* Create new breakpoint locations for B (a hardware or software
13466 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13467 zero, then B is a ranged breakpoint. Only recreates locations for
13468 FILTER_PSPACE. Locations of other program spaces are left
13469 untouched. */
13470
13471 void
13472 update_breakpoint_locations (struct breakpoint *b,
13473 struct program_space *filter_pspace,
13474 gdb::array_view<const symtab_and_line> sals,
13475 gdb::array_view<const symtab_and_line> sals_end)
13476 {
13477 struct bp_location *existing_locations;
13478
13479 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
13480 {
13481 /* Ranged breakpoints have only one start location and one end
13482 location. */
13483 b->enable_state = bp_disabled;
13484 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13485 "multiple locations found\n"),
13486 b->number);
13487 return;
13488 }
13489
13490 /* If there's no new locations, and all existing locations are
13491 pending, don't do anything. This optimizes the common case where
13492 all locations are in the same shared library, that was unloaded.
13493 We'd like to retain the location, so that when the library is
13494 loaded again, we don't loose the enabled/disabled status of the
13495 individual locations. */
13496 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
13497 return;
13498
13499 existing_locations = hoist_existing_locations (b, filter_pspace);
13500
13501 for (const auto &sal : sals)
13502 {
13503 struct bp_location *new_loc;
13504
13505 switch_to_program_space_and_thread (sal.pspace);
13506
13507 new_loc = add_location_to_breakpoint (b, &sal);
13508
13509 /* Reparse conditions, they might contain references to the
13510 old symtab. */
13511 if (b->cond_string != NULL)
13512 {
13513 const char *s;
13514
13515 s = b->cond_string;
13516 try
13517 {
13518 new_loc->cond = parse_exp_1 (&s, sal.pc,
13519 block_for_pc (sal.pc),
13520 0);
13521 }
13522 catch (const gdb_exception_error &e)
13523 {
13524 warning (_("failed to reevaluate condition "
13525 "for breakpoint %d: %s"),
13526 b->number, e.what ());
13527 new_loc->enabled = 0;
13528 }
13529 }
13530
13531 if (!sals_end.empty ())
13532 {
13533 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13534
13535 new_loc->length = end - sals[0].pc + 1;
13536 }
13537 }
13538
13539 /* If possible, carry over 'disable' status from existing
13540 breakpoints. */
13541 {
13542 struct bp_location *e = existing_locations;
13543 /* If there are multiple breakpoints with the same function name,
13544 e.g. for inline functions, comparing function names won't work.
13545 Instead compare pc addresses; this is just a heuristic as things
13546 may have moved, but in practice it gives the correct answer
13547 often enough until a better solution is found. */
13548 int have_ambiguous_names = ambiguous_names_p (b->loc);
13549
13550 for (; e; e = e->next)
13551 {
13552 if (!e->enabled && e->function_name)
13553 {
13554 struct bp_location *l = b->loc;
13555 if (have_ambiguous_names)
13556 {
13557 for (; l; l = l->next)
13558 {
13559 /* Ignore software vs hardware location type at
13560 this point, because with "set breakpoint
13561 auto-hw", after a re-set, locations that were
13562 hardware can end up as software, or vice versa.
13563 As mentioned above, this is an heuristic and in
13564 practice should give the correct answer often
13565 enough. */
13566 if (breakpoint_locations_match (e, l, true))
13567 {
13568 l->enabled = 0;
13569 break;
13570 }
13571 }
13572 }
13573 else
13574 {
13575 for (; l; l = l->next)
13576 if (l->function_name
13577 && strcmp (e->function_name, l->function_name) == 0)
13578 {
13579 l->enabled = 0;
13580 break;
13581 }
13582 }
13583 }
13584 }
13585 }
13586
13587 if (!locations_are_equal (existing_locations, b->loc))
13588 gdb::observers::breakpoint_modified.notify (b);
13589 }
13590
13591 /* Find the SaL locations corresponding to the given LOCATION.
13592 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13593
13594 static std::vector<symtab_and_line>
13595 location_to_sals (struct breakpoint *b, struct event_location *location,
13596 struct program_space *search_pspace, int *found)
13597 {
13598 struct gdb_exception exception;
13599
13600 gdb_assert (b->ops != NULL);
13601
13602 std::vector<symtab_and_line> sals;
13603
13604 try
13605 {
13606 sals = b->ops->decode_location (b, location, search_pspace);
13607 }
13608 catch (gdb_exception_error &e)
13609 {
13610 int not_found_and_ok = 0;
13611
13612 /* For pending breakpoints, it's expected that parsing will
13613 fail until the right shared library is loaded. User has
13614 already told to create pending breakpoints and don't need
13615 extra messages. If breakpoint is in bp_shlib_disabled
13616 state, then user already saw the message about that
13617 breakpoint being disabled, and don't want to see more
13618 errors. */
13619 if (e.error == NOT_FOUND_ERROR
13620 && (b->condition_not_parsed
13621 || (b->loc != NULL
13622 && search_pspace != NULL
13623 && b->loc->pspace != search_pspace)
13624 || (b->loc && b->loc->shlib_disabled)
13625 || (b->loc && b->loc->pspace->executing_startup)
13626 || b->enable_state == bp_disabled))
13627 not_found_and_ok = 1;
13628
13629 if (!not_found_and_ok)
13630 {
13631 /* We surely don't want to warn about the same breakpoint
13632 10 times. One solution, implemented here, is disable
13633 the breakpoint on error. Another solution would be to
13634 have separate 'warning emitted' flag. Since this
13635 happens only when a binary has changed, I don't know
13636 which approach is better. */
13637 b->enable_state = bp_disabled;
13638 throw;
13639 }
13640
13641 exception = std::move (e);
13642 }
13643
13644 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13645 {
13646 for (auto &sal : sals)
13647 resolve_sal_pc (&sal);
13648 if (b->condition_not_parsed && b->extra_string != NULL)
13649 {
13650 char *cond_string, *extra_string;
13651 int thread, task;
13652
13653 find_condition_and_thread (b->extra_string, sals[0].pc,
13654 &cond_string, &thread, &task,
13655 &extra_string);
13656 gdb_assert (b->cond_string == NULL);
13657 if (cond_string)
13658 b->cond_string = cond_string;
13659 b->thread = thread;
13660 b->task = task;
13661 if (extra_string)
13662 {
13663 xfree (b->extra_string);
13664 b->extra_string = extra_string;
13665 }
13666 b->condition_not_parsed = 0;
13667 }
13668
13669 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13670 sals[0] = update_static_tracepoint (b, sals[0]);
13671
13672 *found = 1;
13673 }
13674 else
13675 *found = 0;
13676
13677 return sals;
13678 }
13679
13680 /* The default re_set method, for typical hardware or software
13681 breakpoints. Reevaluate the breakpoint and recreate its
13682 locations. */
13683
13684 static void
13685 breakpoint_re_set_default (struct breakpoint *b)
13686 {
13687 struct program_space *filter_pspace = current_program_space;
13688 std::vector<symtab_and_line> expanded, expanded_end;
13689
13690 int found;
13691 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13692 filter_pspace, &found);
13693 if (found)
13694 expanded = std::move (sals);
13695
13696 if (b->location_range_end != NULL)
13697 {
13698 std::vector<symtab_and_line> sals_end
13699 = location_to_sals (b, b->location_range_end.get (),
13700 filter_pspace, &found);
13701 if (found)
13702 expanded_end = std::move (sals_end);
13703 }
13704
13705 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
13706 }
13707
13708 /* Default method for creating SALs from an address string. It basically
13709 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13710
13711 static void
13712 create_sals_from_location_default (struct event_location *location,
13713 struct linespec_result *canonical,
13714 enum bptype type_wanted)
13715 {
13716 parse_breakpoint_sals (location, canonical);
13717 }
13718
13719 /* Call create_breakpoints_sal for the given arguments. This is the default
13720 function for the `create_breakpoints_sal' method of
13721 breakpoint_ops. */
13722
13723 static void
13724 create_breakpoints_sal_default (struct gdbarch *gdbarch,
13725 struct linespec_result *canonical,
13726 gdb::unique_xmalloc_ptr<char> cond_string,
13727 gdb::unique_xmalloc_ptr<char> extra_string,
13728 enum bptype type_wanted,
13729 enum bpdisp disposition,
13730 int thread,
13731 int task, int ignore_count,
13732 const struct breakpoint_ops *ops,
13733 int from_tty, int enabled,
13734 int internal, unsigned flags)
13735 {
13736 create_breakpoints_sal (gdbarch, canonical,
13737 std::move (cond_string),
13738 std::move (extra_string),
13739 type_wanted, disposition,
13740 thread, task, ignore_count, ops, from_tty,
13741 enabled, internal, flags);
13742 }
13743
13744 /* Decode the line represented by S by calling decode_line_full. This is the
13745 default function for the `decode_location' method of breakpoint_ops. */
13746
13747 static std::vector<symtab_and_line>
13748 decode_location_default (struct breakpoint *b,
13749 struct event_location *location,
13750 struct program_space *search_pspace)
13751 {
13752 struct linespec_result canonical;
13753
13754 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
13755 NULL, 0, &canonical, multiple_symbols_all,
13756 b->filter.get ());
13757
13758 /* We should get 0 or 1 resulting SALs. */
13759 gdb_assert (canonical.lsals.size () < 2);
13760
13761 if (!canonical.lsals.empty ())
13762 {
13763 const linespec_sals &lsal = canonical.lsals[0];
13764 return std::move (lsal.sals);
13765 }
13766 return {};
13767 }
13768
13769 /* Reset a breakpoint. */
13770
13771 static void
13772 breakpoint_re_set_one (breakpoint *b)
13773 {
13774 input_radix = b->input_radix;
13775 set_language (b->language);
13776
13777 b->ops->re_set (b);
13778 }
13779
13780 /* Re-set breakpoint locations for the current program space.
13781 Locations bound to other program spaces are left untouched. */
13782
13783 void
13784 breakpoint_re_set (void)
13785 {
13786 struct breakpoint *b, *b_tmp;
13787
13788 {
13789 scoped_restore_current_language save_language;
13790 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
13791 scoped_restore_current_pspace_and_thread restore_pspace_thread;
13792
13793 /* breakpoint_re_set_one sets the current_language to the language
13794 of the breakpoint it is resetting (see prepare_re_set_context)
13795 before re-evaluating the breakpoint's location. This change can
13796 unfortunately get undone by accident if the language_mode is set
13797 to auto, and we either switch frames, or more likely in this context,
13798 we select the current frame.
13799
13800 We prevent this by temporarily turning the language_mode to
13801 language_mode_manual. We restore it once all breakpoints
13802 have been reset. */
13803 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13804 language_mode = language_mode_manual;
13805
13806 /* Note: we must not try to insert locations until after all
13807 breakpoints have been re-set. Otherwise, e.g., when re-setting
13808 breakpoint 1, we'd insert the locations of breakpoint 2, which
13809 hadn't been re-set yet, and thus may have stale locations. */
13810
13811 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13812 {
13813 try
13814 {
13815 breakpoint_re_set_one (b);
13816 }
13817 catch (const gdb_exception &ex)
13818 {
13819 exception_fprintf (gdb_stderr, ex,
13820 "Error in re-setting breakpoint %d: ",
13821 b->number);
13822 }
13823 }
13824
13825 jit_breakpoint_re_set ();
13826 }
13827
13828 create_overlay_event_breakpoint ();
13829 create_longjmp_master_breakpoint ();
13830 create_std_terminate_master_breakpoint ();
13831 create_exception_master_breakpoint ();
13832
13833 /* Now we can insert. */
13834 update_global_location_list (UGLL_MAY_INSERT);
13835 }
13836 \f
13837 /* Reset the thread number of this breakpoint:
13838
13839 - If the breakpoint is for all threads, leave it as-is.
13840 - Else, reset it to the current thread for inferior_ptid. */
13841 void
13842 breakpoint_re_set_thread (struct breakpoint *b)
13843 {
13844 if (b->thread != -1)
13845 {
13846 b->thread = inferior_thread ()->global_num;
13847
13848 /* We're being called after following a fork. The new fork is
13849 selected as current, and unless this was a vfork will have a
13850 different program space from the original thread. Reset that
13851 as well. */
13852 b->loc->pspace = current_program_space;
13853 }
13854 }
13855
13856 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13857 If from_tty is nonzero, it prints a message to that effect,
13858 which ends with a period (no newline). */
13859
13860 void
13861 set_ignore_count (int bptnum, int count, int from_tty)
13862 {
13863 struct breakpoint *b;
13864
13865 if (count < 0)
13866 count = 0;
13867
13868 ALL_BREAKPOINTS (b)
13869 if (b->number == bptnum)
13870 {
13871 if (is_tracepoint (b))
13872 {
13873 if (from_tty && count != 0)
13874 printf_filtered (_("Ignore count ignored for tracepoint %d."),
13875 bptnum);
13876 return;
13877 }
13878
13879 b->ignore_count = count;
13880 if (from_tty)
13881 {
13882 if (count == 0)
13883 printf_filtered (_("Will stop next time "
13884 "breakpoint %d is reached."),
13885 bptnum);
13886 else if (count == 1)
13887 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
13888 bptnum);
13889 else
13890 printf_filtered (_("Will ignore next %d "
13891 "crossings of breakpoint %d."),
13892 count, bptnum);
13893 }
13894 gdb::observers::breakpoint_modified.notify (b);
13895 return;
13896 }
13897
13898 error (_("No breakpoint number %d."), bptnum);
13899 }
13900
13901 /* Command to set ignore-count of breakpoint N to COUNT. */
13902
13903 static void
13904 ignore_command (const char *args, int from_tty)
13905 {
13906 const char *p = args;
13907 int num;
13908
13909 if (p == 0)
13910 error_no_arg (_("a breakpoint number"));
13911
13912 num = get_number (&p);
13913 if (num == 0)
13914 error (_("bad breakpoint number: '%s'"), args);
13915 if (*p == 0)
13916 error (_("Second argument (specified ignore-count) is missing."));
13917
13918 set_ignore_count (num,
13919 longest_to_int (value_as_long (parse_and_eval (p))),
13920 from_tty);
13921 if (from_tty)
13922 printf_filtered ("\n");
13923 }
13924 \f
13925
13926 /* Call FUNCTION on each of the breakpoints with numbers in the range
13927 defined by BP_NUM_RANGE (an inclusive range). */
13928
13929 static void
13930 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13931 gdb::function_view<void (breakpoint *)> function)
13932 {
13933 if (bp_num_range.first == 0)
13934 {
13935 warning (_("bad breakpoint number at or near '%d'"),
13936 bp_num_range.first);
13937 }
13938 else
13939 {
13940 struct breakpoint *b, *tmp;
13941
13942 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
13943 {
13944 bool match = false;
13945
13946 ALL_BREAKPOINTS_SAFE (b, tmp)
13947 if (b->number == i)
13948 {
13949 match = true;
13950 function (b);
13951 break;
13952 }
13953 if (!match)
13954 printf_unfiltered (_("No breakpoint number %d.\n"), i);
13955 }
13956 }
13957 }
13958
13959 /* Call FUNCTION on each of the breakpoints whose numbers are given in
13960 ARGS. */
13961
13962 static void
13963 map_breakpoint_numbers (const char *args,
13964 gdb::function_view<void (breakpoint *)> function)
13965 {
13966 if (args == NULL || *args == '\0')
13967 error_no_arg (_("one or more breakpoint numbers"));
13968
13969 number_or_range_parser parser (args);
13970
13971 while (!parser.finished ())
13972 {
13973 int num = parser.get_number ();
13974 map_breakpoint_number_range (std::make_pair (num, num), function);
13975 }
13976 }
13977
13978 /* Return the breakpoint location structure corresponding to the
13979 BP_NUM and LOC_NUM values. */
13980
13981 static struct bp_location *
13982 find_location_by_number (int bp_num, int loc_num)
13983 {
13984 struct breakpoint *b;
13985
13986 ALL_BREAKPOINTS (b)
13987 if (b->number == bp_num)
13988 {
13989 break;
13990 }
13991
13992 if (!b || b->number != bp_num)
13993 error (_("Bad breakpoint number '%d'"), bp_num);
13994
13995 if (loc_num == 0)
13996 error (_("Bad breakpoint location number '%d'"), loc_num);
13997
13998 int n = 0;
13999 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
14000 if (++n == loc_num)
14001 return loc;
14002
14003 error (_("Bad breakpoint location number '%d'"), loc_num);
14004 }
14005
14006 /* Modes of operation for extract_bp_num. */
14007 enum class extract_bp_kind
14008 {
14009 /* Extracting a breakpoint number. */
14010 bp,
14011
14012 /* Extracting a location number. */
14013 loc,
14014 };
14015
14016 /* Extract a breakpoint or location number (as determined by KIND)
14017 from the string starting at START. TRAILER is a character which
14018 can be found after the number. If you don't want a trailer, use
14019 '\0'. If END_OUT is not NULL, it is set to point after the parsed
14020 string. This always returns a positive integer. */
14021
14022 static int
14023 extract_bp_num (extract_bp_kind kind, const char *start,
14024 int trailer, const char **end_out = NULL)
14025 {
14026 const char *end = start;
14027 int num = get_number_trailer (&end, trailer);
14028 if (num < 0)
14029 error (kind == extract_bp_kind::bp
14030 ? _("Negative breakpoint number '%.*s'")
14031 : _("Negative breakpoint location number '%.*s'"),
14032 int (end - start), start);
14033 if (num == 0)
14034 error (kind == extract_bp_kind::bp
14035 ? _("Bad breakpoint number '%.*s'")
14036 : _("Bad breakpoint location number '%.*s'"),
14037 int (end - start), start);
14038
14039 if (end_out != NULL)
14040 *end_out = end;
14041 return num;
14042 }
14043
14044 /* Extract a breakpoint or location range (as determined by KIND) in
14045 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14046 representing the (inclusive) range. The returned pair's elements
14047 are always positive integers. */
14048
14049 static std::pair<int, int>
14050 extract_bp_or_bp_range (extract_bp_kind kind,
14051 const std::string &arg,
14052 std::string::size_type arg_offset)
14053 {
14054 std::pair<int, int> range;
14055 const char *bp_loc = &arg[arg_offset];
14056 std::string::size_type dash = arg.find ('-', arg_offset);
14057 if (dash != std::string::npos)
14058 {
14059 /* bp_loc is a range (x-z). */
14060 if (arg.length () == dash + 1)
14061 error (kind == extract_bp_kind::bp
14062 ? _("Bad breakpoint number at or near: '%s'")
14063 : _("Bad breakpoint location number at or near: '%s'"),
14064 bp_loc);
14065
14066 const char *end;
14067 const char *start_first = bp_loc;
14068 const char *start_second = &arg[dash + 1];
14069 range.first = extract_bp_num (kind, start_first, '-');
14070 range.second = extract_bp_num (kind, start_second, '\0', &end);
14071
14072 if (range.first > range.second)
14073 error (kind == extract_bp_kind::bp
14074 ? _("Inverted breakpoint range at '%.*s'")
14075 : _("Inverted breakpoint location range at '%.*s'"),
14076 int (end - start_first), start_first);
14077 }
14078 else
14079 {
14080 /* bp_loc is a single value. */
14081 range.first = extract_bp_num (kind, bp_loc, '\0');
14082 range.second = range.first;
14083 }
14084 return range;
14085 }
14086
14087 /* Extract the breakpoint/location range specified by ARG. Returns
14088 the breakpoint range in BP_NUM_RANGE, and the location range in
14089 BP_LOC_RANGE.
14090
14091 ARG may be in any of the following forms:
14092
14093 x where 'x' is a breakpoint number.
14094 x-y where 'x' and 'y' specify a breakpoint numbers range.
14095 x.y where 'x' is a breakpoint number and 'y' a location number.
14096 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14097 location number range.
14098 */
14099
14100 static void
14101 extract_bp_number_and_location (const std::string &arg,
14102 std::pair<int, int> &bp_num_range,
14103 std::pair<int, int> &bp_loc_range)
14104 {
14105 std::string::size_type dot = arg.find ('.');
14106
14107 if (dot != std::string::npos)
14108 {
14109 /* Handle 'x.y' and 'x.y-z' cases. */
14110
14111 if (arg.length () == dot + 1 || dot == 0)
14112 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
14113
14114 bp_num_range.first
14115 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14116 bp_num_range.second = bp_num_range.first;
14117
14118 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14119 arg, dot + 1);
14120 }
14121 else
14122 {
14123 /* Handle x and x-y cases. */
14124
14125 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
14126 bp_loc_range.first = 0;
14127 bp_loc_range.second = 0;
14128 }
14129 }
14130
14131 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14132 specifies whether to enable or disable. */
14133
14134 static void
14135 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14136 {
14137 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14138 if (loc != NULL)
14139 {
14140 if (loc->enabled != enable)
14141 {
14142 loc->enabled = enable;
14143 mark_breakpoint_location_modified (loc);
14144 }
14145 if (target_supports_enable_disable_tracepoint ()
14146 && current_trace_status ()->running && loc->owner
14147 && is_tracepoint (loc->owner))
14148 target_disable_tracepoint (loc);
14149 }
14150 update_global_location_list (UGLL_DONT_INSERT);
14151
14152 gdb::observers::breakpoint_modified.notify (loc->owner);
14153 }
14154
14155 /* Enable or disable a range of breakpoint locations. BP_NUM is the
14156 number of the breakpoint, and BP_LOC_RANGE specifies the
14157 (inclusive) range of location numbers of that breakpoint to
14158 enable/disable. ENABLE specifies whether to enable or disable the
14159 location. */
14160
14161 static void
14162 enable_disable_breakpoint_location_range (int bp_num,
14163 std::pair<int, int> &bp_loc_range,
14164 bool enable)
14165 {
14166 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14167 enable_disable_bp_num_loc (bp_num, i, enable);
14168 }
14169
14170 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14171 If from_tty is nonzero, it prints a message to that effect,
14172 which ends with a period (no newline). */
14173
14174 void
14175 disable_breakpoint (struct breakpoint *bpt)
14176 {
14177 /* Never disable a watchpoint scope breakpoint; we want to
14178 hit them when we leave scope so we can delete both the
14179 watchpoint and its scope breakpoint at that time. */
14180 if (bpt->type == bp_watchpoint_scope)
14181 return;
14182
14183 bpt->enable_state = bp_disabled;
14184
14185 /* Mark breakpoint locations modified. */
14186 mark_breakpoint_modified (bpt);
14187
14188 if (target_supports_enable_disable_tracepoint ()
14189 && current_trace_status ()->running && is_tracepoint (bpt))
14190 {
14191 struct bp_location *location;
14192
14193 for (location = bpt->loc; location; location = location->next)
14194 target_disable_tracepoint (location);
14195 }
14196
14197 update_global_location_list (UGLL_DONT_INSERT);
14198
14199 gdb::observers::breakpoint_modified.notify (bpt);
14200 }
14201
14202 /* Enable or disable the breakpoint(s) or breakpoint location(s)
14203 specified in ARGS. ARGS may be in any of the formats handled by
14204 extract_bp_number_and_location. ENABLE specifies whether to enable
14205 or disable the breakpoints/locations. */
14206
14207 static void
14208 enable_disable_command (const char *args, int from_tty, bool enable)
14209 {
14210 if (args == 0)
14211 {
14212 struct breakpoint *bpt;
14213
14214 ALL_BREAKPOINTS (bpt)
14215 if (user_breakpoint_p (bpt))
14216 {
14217 if (enable)
14218 enable_breakpoint (bpt);
14219 else
14220 disable_breakpoint (bpt);
14221 }
14222 }
14223 else
14224 {
14225 std::string num = extract_arg (&args);
14226
14227 while (!num.empty ())
14228 {
14229 std::pair<int, int> bp_num_range, bp_loc_range;
14230
14231 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14232
14233 if (bp_loc_range.first == bp_loc_range.second
14234 && bp_loc_range.first == 0)
14235 {
14236 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14237 map_breakpoint_number_range (bp_num_range,
14238 enable
14239 ? enable_breakpoint
14240 : disable_breakpoint);
14241 }
14242 else
14243 {
14244 /* Handle breakpoint ids with formats 'x.y' or
14245 'x.y-z'. */
14246 enable_disable_breakpoint_location_range
14247 (bp_num_range.first, bp_loc_range, enable);
14248 }
14249 num = extract_arg (&args);
14250 }
14251 }
14252 }
14253
14254 /* The disable command disables the specified breakpoints/locations
14255 (or all defined breakpoints) so they're no longer effective in
14256 stopping the inferior. ARGS may be in any of the forms defined in
14257 extract_bp_number_and_location. */
14258
14259 static void
14260 disable_command (const char *args, int from_tty)
14261 {
14262 enable_disable_command (args, from_tty, false);
14263 }
14264
14265 static void
14266 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14267 int count)
14268 {
14269 int target_resources_ok;
14270
14271 if (bpt->type == bp_hardware_breakpoint)
14272 {
14273 int i;
14274 i = hw_breakpoint_used_count ();
14275 target_resources_ok =
14276 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14277 i + 1, 0);
14278 if (target_resources_ok == 0)
14279 error (_("No hardware breakpoint support in the target."));
14280 else if (target_resources_ok < 0)
14281 error (_("Hardware breakpoints used exceeds limit."));
14282 }
14283
14284 if (is_watchpoint (bpt))
14285 {
14286 /* Initialize it just to avoid a GCC false warning. */
14287 enum enable_state orig_enable_state = bp_disabled;
14288
14289 try
14290 {
14291 struct watchpoint *w = (struct watchpoint *) bpt;
14292
14293 orig_enable_state = bpt->enable_state;
14294 bpt->enable_state = bp_enabled;
14295 update_watchpoint (w, 1 /* reparse */);
14296 }
14297 catch (const gdb_exception &e)
14298 {
14299 bpt->enable_state = orig_enable_state;
14300 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14301 bpt->number);
14302 return;
14303 }
14304 }
14305
14306 bpt->enable_state = bp_enabled;
14307
14308 /* Mark breakpoint locations modified. */
14309 mark_breakpoint_modified (bpt);
14310
14311 if (target_supports_enable_disable_tracepoint ()
14312 && current_trace_status ()->running && is_tracepoint (bpt))
14313 {
14314 struct bp_location *location;
14315
14316 for (location = bpt->loc; location; location = location->next)
14317 target_enable_tracepoint (location);
14318 }
14319
14320 bpt->disposition = disposition;
14321 bpt->enable_count = count;
14322 update_global_location_list (UGLL_MAY_INSERT);
14323
14324 gdb::observers::breakpoint_modified.notify (bpt);
14325 }
14326
14327
14328 void
14329 enable_breakpoint (struct breakpoint *bpt)
14330 {
14331 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14332 }
14333
14334 /* The enable command enables the specified breakpoints/locations (or
14335 all defined breakpoints) so they once again become (or continue to
14336 be) effective in stopping the inferior. ARGS may be in any of the
14337 forms defined in extract_bp_number_and_location. */
14338
14339 static void
14340 enable_command (const char *args, int from_tty)
14341 {
14342 enable_disable_command (args, from_tty, true);
14343 }
14344
14345 static void
14346 enable_once_command (const char *args, int from_tty)
14347 {
14348 map_breakpoint_numbers
14349 (args, [&] (breakpoint *b)
14350 {
14351 iterate_over_related_breakpoints
14352 (b, [&] (breakpoint *bpt)
14353 {
14354 enable_breakpoint_disp (bpt, disp_disable, 1);
14355 });
14356 });
14357 }
14358
14359 static void
14360 enable_count_command (const char *args, int from_tty)
14361 {
14362 int count;
14363
14364 if (args == NULL)
14365 error_no_arg (_("hit count"));
14366
14367 count = get_number (&args);
14368
14369 map_breakpoint_numbers
14370 (args, [&] (breakpoint *b)
14371 {
14372 iterate_over_related_breakpoints
14373 (b, [&] (breakpoint *bpt)
14374 {
14375 enable_breakpoint_disp (bpt, disp_disable, count);
14376 });
14377 });
14378 }
14379
14380 static void
14381 enable_delete_command (const char *args, int from_tty)
14382 {
14383 map_breakpoint_numbers
14384 (args, [&] (breakpoint *b)
14385 {
14386 iterate_over_related_breakpoints
14387 (b, [&] (breakpoint *bpt)
14388 {
14389 enable_breakpoint_disp (bpt, disp_del, 1);
14390 });
14391 });
14392 }
14393 \f
14394 /* Invalidate last known value of any hardware watchpoint if
14395 the memory which that value represents has been written to by
14396 GDB itself. */
14397
14398 static void
14399 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14400 CORE_ADDR addr, ssize_t len,
14401 const bfd_byte *data)
14402 {
14403 struct breakpoint *bp;
14404
14405 ALL_BREAKPOINTS (bp)
14406 if (bp->enable_state == bp_enabled
14407 && bp->type == bp_hardware_watchpoint)
14408 {
14409 struct watchpoint *wp = (struct watchpoint *) bp;
14410
14411 if (wp->val_valid && wp->val != nullptr)
14412 {
14413 struct bp_location *loc;
14414
14415 for (loc = bp->loc; loc != NULL; loc = loc->next)
14416 if (loc->loc_type == bp_loc_hardware_watchpoint
14417 && loc->address + loc->length > addr
14418 && addr + len > loc->address)
14419 {
14420 wp->val = NULL;
14421 wp->val_valid = false;
14422 }
14423 }
14424 }
14425 }
14426
14427 /* Create and insert a breakpoint for software single step. */
14428
14429 void
14430 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14431 const address_space *aspace,
14432 CORE_ADDR next_pc)
14433 {
14434 struct thread_info *tp = inferior_thread ();
14435 struct symtab_and_line sal;
14436 CORE_ADDR pc = next_pc;
14437
14438 if (tp->control.single_step_breakpoints == NULL)
14439 {
14440 tp->control.single_step_breakpoints
14441 = new_single_step_breakpoint (tp->global_num, gdbarch);
14442 }
14443
14444 sal = find_pc_line (pc, 0);
14445 sal.pc = pc;
14446 sal.section = find_pc_overlay (pc);
14447 sal.explicit_pc = 1;
14448 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14449
14450 update_global_location_list (UGLL_INSERT);
14451 }
14452
14453 /* Insert single step breakpoints according to the current state. */
14454
14455 int
14456 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14457 {
14458 struct regcache *regcache = get_current_regcache ();
14459 std::vector<CORE_ADDR> next_pcs;
14460
14461 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14462
14463 if (!next_pcs.empty ())
14464 {
14465 struct frame_info *frame = get_current_frame ();
14466 const address_space *aspace = get_frame_address_space (frame);
14467
14468 for (CORE_ADDR pc : next_pcs)
14469 insert_single_step_breakpoint (gdbarch, aspace, pc);
14470
14471 return 1;
14472 }
14473 else
14474 return 0;
14475 }
14476
14477 /* See breakpoint.h. */
14478
14479 int
14480 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14481 const address_space *aspace,
14482 CORE_ADDR pc)
14483 {
14484 struct bp_location *loc;
14485
14486 for (loc = bp->loc; loc != NULL; loc = loc->next)
14487 if (loc->inserted
14488 && breakpoint_location_address_match (loc, aspace, pc))
14489 return 1;
14490
14491 return 0;
14492 }
14493
14494 /* Check whether a software single-step breakpoint is inserted at
14495 PC. */
14496
14497 int
14498 single_step_breakpoint_inserted_here_p (const address_space *aspace,
14499 CORE_ADDR pc)
14500 {
14501 struct breakpoint *bpt;
14502
14503 ALL_BREAKPOINTS (bpt)
14504 {
14505 if (bpt->type == bp_single_step
14506 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14507 return 1;
14508 }
14509 return 0;
14510 }
14511
14512 /* Tracepoint-specific operations. */
14513
14514 /* Set tracepoint count to NUM. */
14515 static void
14516 set_tracepoint_count (int num)
14517 {
14518 tracepoint_count = num;
14519 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14520 }
14521
14522 static void
14523 trace_command (const char *arg, int from_tty)
14524 {
14525 event_location_up location = string_to_event_location (&arg,
14526 current_language);
14527 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
14528 (location.get (), true /* is_tracepoint */);
14529
14530 create_breakpoint (get_current_arch (),
14531 location.get (),
14532 NULL, 0, arg, 1 /* parse arg */,
14533 0 /* tempflag */,
14534 bp_tracepoint /* type_wanted */,
14535 0 /* Ignore count */,
14536 pending_break_support,
14537 ops,
14538 from_tty,
14539 1 /* enabled */,
14540 0 /* internal */, 0);
14541 }
14542
14543 static void
14544 ftrace_command (const char *arg, int from_tty)
14545 {
14546 event_location_up location = string_to_event_location (&arg,
14547 current_language);
14548 create_breakpoint (get_current_arch (),
14549 location.get (),
14550 NULL, 0, arg, 1 /* parse arg */,
14551 0 /* tempflag */,
14552 bp_fast_tracepoint /* type_wanted */,
14553 0 /* Ignore count */,
14554 pending_break_support,
14555 &tracepoint_breakpoint_ops,
14556 from_tty,
14557 1 /* enabled */,
14558 0 /* internal */, 0);
14559 }
14560
14561 /* strace command implementation. Creates a static tracepoint. */
14562
14563 static void
14564 strace_command (const char *arg, int from_tty)
14565 {
14566 struct breakpoint_ops *ops;
14567 event_location_up location;
14568
14569 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14570 or with a normal static tracepoint. */
14571 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14572 {
14573 ops = &strace_marker_breakpoint_ops;
14574 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
14575 }
14576 else
14577 {
14578 ops = &tracepoint_breakpoint_ops;
14579 location = string_to_event_location (&arg, current_language);
14580 }
14581
14582 create_breakpoint (get_current_arch (),
14583 location.get (),
14584 NULL, 0, arg, 1 /* parse arg */,
14585 0 /* tempflag */,
14586 bp_static_tracepoint /* type_wanted */,
14587 0 /* Ignore count */,
14588 pending_break_support,
14589 ops,
14590 from_tty,
14591 1 /* enabled */,
14592 0 /* internal */, 0);
14593 }
14594
14595 /* Set up a fake reader function that gets command lines from a linked
14596 list that was acquired during tracepoint uploading. */
14597
14598 static struct uploaded_tp *this_utp;
14599 static int next_cmd;
14600
14601 static char *
14602 read_uploaded_action (void)
14603 {
14604 char *rslt = nullptr;
14605
14606 if (next_cmd < this_utp->cmd_strings.size ())
14607 {
14608 rslt = this_utp->cmd_strings[next_cmd].get ();
14609 next_cmd++;
14610 }
14611
14612 return rslt;
14613 }
14614
14615 /* Given information about a tracepoint as recorded on a target (which
14616 can be either a live system or a trace file), attempt to create an
14617 equivalent GDB tracepoint. This is not a reliable process, since
14618 the target does not necessarily have all the information used when
14619 the tracepoint was originally defined. */
14620
14621 struct tracepoint *
14622 create_tracepoint_from_upload (struct uploaded_tp *utp)
14623 {
14624 const char *addr_str;
14625 char small_buf[100];
14626 struct tracepoint *tp;
14627
14628 if (utp->at_string)
14629 addr_str = utp->at_string.get ();
14630 else
14631 {
14632 /* In the absence of a source location, fall back to raw
14633 address. Since there is no way to confirm that the address
14634 means the same thing as when the trace was started, warn the
14635 user. */
14636 warning (_("Uploaded tracepoint %d has no "
14637 "source location, using raw address"),
14638 utp->number);
14639 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14640 addr_str = small_buf;
14641 }
14642
14643 /* There's not much we can do with a sequence of bytecodes. */
14644 if (utp->cond && !utp->cond_string)
14645 warning (_("Uploaded tracepoint %d condition "
14646 "has no source form, ignoring it"),
14647 utp->number);
14648
14649 event_location_up location = string_to_event_location (&addr_str,
14650 current_language);
14651 if (!create_breakpoint (get_current_arch (),
14652 location.get (),
14653 utp->cond_string.get (), -1, addr_str,
14654 0 /* parse cond/thread */,
14655 0 /* tempflag */,
14656 utp->type /* type_wanted */,
14657 0 /* Ignore count */,
14658 pending_break_support,
14659 &tracepoint_breakpoint_ops,
14660 0 /* from_tty */,
14661 utp->enabled /* enabled */,
14662 0 /* internal */,
14663 CREATE_BREAKPOINT_FLAGS_INSERTED))
14664 return NULL;
14665
14666 /* Get the tracepoint we just created. */
14667 tp = get_tracepoint (tracepoint_count);
14668 gdb_assert (tp != NULL);
14669
14670 if (utp->pass > 0)
14671 {
14672 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14673 tp->number);
14674
14675 trace_pass_command (small_buf, 0);
14676 }
14677
14678 /* If we have uploaded versions of the original commands, set up a
14679 special-purpose "reader" function and call the usual command line
14680 reader, then pass the result to the breakpoint command-setting
14681 function. */
14682 if (!utp->cmd_strings.empty ())
14683 {
14684 counted_command_line cmd_list;
14685
14686 this_utp = utp;
14687 next_cmd = 0;
14688
14689 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
14690
14691 breakpoint_set_commands (tp, std::move (cmd_list));
14692 }
14693 else if (!utp->actions.empty ()
14694 || !utp->step_actions.empty ())
14695 warning (_("Uploaded tracepoint %d actions "
14696 "have no source form, ignoring them"),
14697 utp->number);
14698
14699 /* Copy any status information that might be available. */
14700 tp->hit_count = utp->hit_count;
14701 tp->traceframe_usage = utp->traceframe_usage;
14702
14703 return tp;
14704 }
14705
14706 /* Print information on tracepoint number TPNUM_EXP, or all if
14707 omitted. */
14708
14709 static void
14710 info_tracepoints_command (const char *args, int from_tty)
14711 {
14712 struct ui_out *uiout = current_uiout;
14713 int num_printed;
14714
14715 num_printed = breakpoint_1 (args, false, is_tracepoint);
14716
14717 if (num_printed == 0)
14718 {
14719 if (args == NULL || *args == '\0')
14720 uiout->message ("No tracepoints.\n");
14721 else
14722 uiout->message ("No tracepoint matching '%s'.\n", args);
14723 }
14724
14725 default_collect_info ();
14726 }
14727
14728 /* The 'enable trace' command enables tracepoints.
14729 Not supported by all targets. */
14730 static void
14731 enable_trace_command (const char *args, int from_tty)
14732 {
14733 enable_command (args, from_tty);
14734 }
14735
14736 /* The 'disable trace' command disables tracepoints.
14737 Not supported by all targets. */
14738 static void
14739 disable_trace_command (const char *args, int from_tty)
14740 {
14741 disable_command (args, from_tty);
14742 }
14743
14744 /* Remove a tracepoint (or all if no argument). */
14745 static void
14746 delete_trace_command (const char *arg, int from_tty)
14747 {
14748 struct breakpoint *b, *b_tmp;
14749
14750 dont_repeat ();
14751
14752 if (arg == 0)
14753 {
14754 int breaks_to_delete = 0;
14755
14756 /* Delete all breakpoints if no argument.
14757 Do not delete internal or call-dummy breakpoints, these
14758 have to be deleted with an explicit breakpoint number
14759 argument. */
14760 ALL_TRACEPOINTS (b)
14761 if (is_tracepoint (b) && user_breakpoint_p (b))
14762 {
14763 breaks_to_delete = 1;
14764 break;
14765 }
14766
14767 /* Ask user only if there are some breakpoints to delete. */
14768 if (!from_tty
14769 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14770 {
14771 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14772 if (is_tracepoint (b) && user_breakpoint_p (b))
14773 delete_breakpoint (b);
14774 }
14775 }
14776 else
14777 map_breakpoint_numbers
14778 (arg, [&] (breakpoint *br)
14779 {
14780 iterate_over_related_breakpoints (br, delete_breakpoint);
14781 });
14782 }
14783
14784 /* Helper function for trace_pass_command. */
14785
14786 static void
14787 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14788 {
14789 tp->pass_count = count;
14790 gdb::observers::breakpoint_modified.notify (tp);
14791 if (from_tty)
14792 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
14793 tp->number, count);
14794 }
14795
14796 /* Set passcount for tracepoint.
14797
14798 First command argument is passcount, second is tracepoint number.
14799 If tracepoint number omitted, apply to most recently defined.
14800 Also accepts special argument "all". */
14801
14802 static void
14803 trace_pass_command (const char *args, int from_tty)
14804 {
14805 struct tracepoint *t1;
14806 ULONGEST count;
14807
14808 if (args == 0 || *args == 0)
14809 error (_("passcount command requires an "
14810 "argument (count + optional TP num)"));
14811
14812 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
14813
14814 args = skip_spaces (args);
14815 if (*args && strncasecmp (args, "all", 3) == 0)
14816 {
14817 struct breakpoint *b;
14818
14819 args += 3; /* Skip special argument "all". */
14820 if (*args)
14821 error (_("Junk at end of arguments."));
14822
14823 ALL_TRACEPOINTS (b)
14824 {
14825 t1 = (struct tracepoint *) b;
14826 trace_pass_set_count (t1, count, from_tty);
14827 }
14828 }
14829 else if (*args == '\0')
14830 {
14831 t1 = get_tracepoint_by_number (&args, NULL);
14832 if (t1)
14833 trace_pass_set_count (t1, count, from_tty);
14834 }
14835 else
14836 {
14837 number_or_range_parser parser (args);
14838 while (!parser.finished ())
14839 {
14840 t1 = get_tracepoint_by_number (&args, &parser);
14841 if (t1)
14842 trace_pass_set_count (t1, count, from_tty);
14843 }
14844 }
14845 }
14846
14847 struct tracepoint *
14848 get_tracepoint (int num)
14849 {
14850 struct breakpoint *t;
14851
14852 ALL_TRACEPOINTS (t)
14853 if (t->number == num)
14854 return (struct tracepoint *) t;
14855
14856 return NULL;
14857 }
14858
14859 /* Find the tracepoint with the given target-side number (which may be
14860 different from the tracepoint number after disconnecting and
14861 reconnecting). */
14862
14863 struct tracepoint *
14864 get_tracepoint_by_number_on_target (int num)
14865 {
14866 struct breakpoint *b;
14867
14868 ALL_TRACEPOINTS (b)
14869 {
14870 struct tracepoint *t = (struct tracepoint *) b;
14871
14872 if (t->number_on_target == num)
14873 return t;
14874 }
14875
14876 return NULL;
14877 }
14878
14879 /* Utility: parse a tracepoint number and look it up in the list.
14880 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
14881 If the argument is missing, the most recent tracepoint
14882 (tracepoint_count) is returned. */
14883
14884 struct tracepoint *
14885 get_tracepoint_by_number (const char **arg,
14886 number_or_range_parser *parser)
14887 {
14888 struct breakpoint *t;
14889 int tpnum;
14890 const char *instring = arg == NULL ? NULL : *arg;
14891
14892 if (parser != NULL)
14893 {
14894 gdb_assert (!parser->finished ());
14895 tpnum = parser->get_number ();
14896 }
14897 else if (arg == NULL || *arg == NULL || ! **arg)
14898 tpnum = tracepoint_count;
14899 else
14900 tpnum = get_number (arg);
14901
14902 if (tpnum <= 0)
14903 {
14904 if (instring && *instring)
14905 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
14906 instring);
14907 else
14908 printf_filtered (_("No previous tracepoint\n"));
14909 return NULL;
14910 }
14911
14912 ALL_TRACEPOINTS (t)
14913 if (t->number == tpnum)
14914 {
14915 return (struct tracepoint *) t;
14916 }
14917
14918 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
14919 return NULL;
14920 }
14921
14922 void
14923 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
14924 {
14925 if (b->thread != -1)
14926 fprintf_unfiltered (fp, " thread %d", b->thread);
14927
14928 if (b->task != 0)
14929 fprintf_unfiltered (fp, " task %d", b->task);
14930
14931 fprintf_unfiltered (fp, "\n");
14932 }
14933
14934 /* Save information on user settable breakpoints (watchpoints, etc) to
14935 a new script file named FILENAME. If FILTER is non-NULL, call it
14936 on each breakpoint and only include the ones for which it returns
14937 true. */
14938
14939 static void
14940 save_breakpoints (const char *filename, int from_tty,
14941 bool (*filter) (const struct breakpoint *))
14942 {
14943 struct breakpoint *tp;
14944 int any = 0;
14945 int extra_trace_bits = 0;
14946
14947 if (filename == 0 || *filename == 0)
14948 error (_("Argument required (file name in which to save)"));
14949
14950 /* See if we have anything to save. */
14951 ALL_BREAKPOINTS (tp)
14952 {
14953 /* Skip internal and momentary breakpoints. */
14954 if (!user_breakpoint_p (tp))
14955 continue;
14956
14957 /* If we have a filter, only save the breakpoints it accepts. */
14958 if (filter && !filter (tp))
14959 continue;
14960
14961 any = 1;
14962
14963 if (is_tracepoint (tp))
14964 {
14965 extra_trace_bits = 1;
14966
14967 /* We can stop searching. */
14968 break;
14969 }
14970 }
14971
14972 if (!any)
14973 {
14974 warning (_("Nothing to save."));
14975 return;
14976 }
14977
14978 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
14979
14980 stdio_file fp;
14981
14982 if (!fp.open (expanded_filename.get (), "w"))
14983 error (_("Unable to open file '%s' for saving (%s)"),
14984 expanded_filename.get (), safe_strerror (errno));
14985
14986 if (extra_trace_bits)
14987 save_trace_state_variables (&fp);
14988
14989 ALL_BREAKPOINTS (tp)
14990 {
14991 /* Skip internal and momentary breakpoints. */
14992 if (!user_breakpoint_p (tp))
14993 continue;
14994
14995 /* If we have a filter, only save the breakpoints it accepts. */
14996 if (filter && !filter (tp))
14997 continue;
14998
14999 tp->ops->print_recreate (tp, &fp);
15000
15001 /* Note, we can't rely on tp->number for anything, as we can't
15002 assume the recreated breakpoint numbers will match. Use $bpnum
15003 instead. */
15004
15005 if (tp->cond_string)
15006 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15007
15008 if (tp->ignore_count)
15009 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15010
15011 if (tp->type != bp_dprintf && tp->commands)
15012 {
15013 fp.puts (" commands\n");
15014
15015 current_uiout->redirect (&fp);
15016 try
15017 {
15018 print_command_lines (current_uiout, tp->commands.get (), 2);
15019 }
15020 catch (const gdb_exception &ex)
15021 {
15022 current_uiout->redirect (NULL);
15023 throw;
15024 }
15025
15026 current_uiout->redirect (NULL);
15027 fp.puts (" end\n");
15028 }
15029
15030 if (tp->enable_state == bp_disabled)
15031 fp.puts ("disable $bpnum\n");
15032
15033 /* If this is a multi-location breakpoint, check if the locations
15034 should be individually disabled. Watchpoint locations are
15035 special, and not user visible. */
15036 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15037 {
15038 struct bp_location *loc;
15039 int n = 1;
15040
15041 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15042 if (!loc->enabled)
15043 fp.printf ("disable $bpnum.%d\n", n);
15044 }
15045 }
15046
15047 if (extra_trace_bits && *default_collect)
15048 fp.printf ("set default-collect %s\n", default_collect);
15049
15050 if (from_tty)
15051 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
15052 }
15053
15054 /* The `save breakpoints' command. */
15055
15056 static void
15057 save_breakpoints_command (const char *args, int from_tty)
15058 {
15059 save_breakpoints (args, from_tty, NULL);
15060 }
15061
15062 /* The `save tracepoints' command. */
15063
15064 static void
15065 save_tracepoints_command (const char *args, int from_tty)
15066 {
15067 save_breakpoints (args, from_tty, is_tracepoint);
15068 }
15069
15070 /* Create a vector of all tracepoints. */
15071
15072 std::vector<breakpoint *>
15073 all_tracepoints (void)
15074 {
15075 std::vector<breakpoint *> tp_vec;
15076 struct breakpoint *tp;
15077
15078 ALL_TRACEPOINTS (tp)
15079 {
15080 tp_vec.push_back (tp);
15081 }
15082
15083 return tp_vec;
15084 }
15085
15086 \f
15087 /* This help string is used to consolidate all the help string for specifying
15088 locations used by several commands. */
15089
15090 #define LOCATION_HELP_STRING \
15091 "Linespecs are colon-separated lists of location parameters, such as\n\
15092 source filename, function name, label name, and line number.\n\
15093 Example: To specify the start of a label named \"the_top\" in the\n\
15094 function \"fact\" in the file \"factorial.c\", use\n\
15095 \"factorial.c:fact:the_top\".\n\
15096 \n\
15097 Address locations begin with \"*\" and specify an exact address in the\n\
15098 program. Example: To specify the fourth byte past the start function\n\
15099 \"main\", use \"*main + 4\".\n\
15100 \n\
15101 Explicit locations are similar to linespecs but use an option/argument\n\
15102 syntax to specify location parameters.\n\
15103 Example: To specify the start of the label named \"the_top\" in the\n\
15104 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15105 -function fact -label the_top\".\n\
15106 \n\
15107 By default, a specified function is matched against the program's\n\
15108 functions in all scopes. For C++, this means in all namespaces and\n\
15109 classes. For Ada, this means in all packages. E.g., in C++,\n\
15110 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15111 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
15112 specified name as a complete fully-qualified name instead."
15113
15114 /* This help string is used for the break, hbreak, tbreak and thbreak
15115 commands. It is defined as a macro to prevent duplication.
15116 COMMAND should be a string constant containing the name of the
15117 command. */
15118
15119 #define BREAK_ARGS_HELP(command) \
15120 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15121 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15122 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15123 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15124 `-probe-dtrace' (for a DTrace probe).\n\
15125 LOCATION may be a linespec, address, or explicit location as described\n\
15126 below.\n\
15127 \n\
15128 With no LOCATION, uses current execution address of the selected\n\
15129 stack frame. This is useful for breaking on return to a stack frame.\n\
15130 \n\
15131 THREADNUM is the number from \"info threads\".\n\
15132 CONDITION is a boolean expression.\n\
15133 \n" LOCATION_HELP_STRING "\n\n\
15134 Multiple breakpoints at one place are permitted, and useful if their\n\
15135 conditions are different.\n\
15136 \n\
15137 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15138
15139 /* List of subcommands for "catch". */
15140 static struct cmd_list_element *catch_cmdlist;
15141
15142 /* List of subcommands for "tcatch". */
15143 static struct cmd_list_element *tcatch_cmdlist;
15144
15145 void
15146 add_catch_command (const char *name, const char *docstring,
15147 cmd_const_sfunc_ftype *sfunc,
15148 completer_ftype *completer,
15149 void *user_data_catch,
15150 void *user_data_tcatch)
15151 {
15152 struct cmd_list_element *command;
15153
15154 command = add_cmd (name, class_breakpoint, docstring,
15155 &catch_cmdlist);
15156 set_cmd_sfunc (command, sfunc);
15157 set_cmd_context (command, user_data_catch);
15158 set_cmd_completer (command, completer);
15159
15160 command = add_cmd (name, class_breakpoint, docstring,
15161 &tcatch_cmdlist);
15162 set_cmd_sfunc (command, sfunc);
15163 set_cmd_context (command, user_data_tcatch);
15164 set_cmd_completer (command, completer);
15165 }
15166
15167 struct breakpoint *
15168 iterate_over_breakpoints (gdb::function_view<bool (breakpoint *)> callback)
15169 {
15170 struct breakpoint *b, *b_tmp;
15171
15172 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15173 {
15174 if (callback (b))
15175 return b;
15176 }
15177
15178 return NULL;
15179 }
15180
15181 /* Zero if any of the breakpoint's locations could be a location where
15182 functions have been inlined, nonzero otherwise. */
15183
15184 static int
15185 is_non_inline_function (struct breakpoint *b)
15186 {
15187 /* The shared library event breakpoint is set on the address of a
15188 non-inline function. */
15189 if (b->type == bp_shlib_event)
15190 return 1;
15191
15192 return 0;
15193 }
15194
15195 /* Nonzero if the specified PC cannot be a location where functions
15196 have been inlined. */
15197
15198 int
15199 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
15200 const struct target_waitstatus *ws)
15201 {
15202 struct breakpoint *b;
15203 struct bp_location *bl;
15204
15205 ALL_BREAKPOINTS (b)
15206 {
15207 if (!is_non_inline_function (b))
15208 continue;
15209
15210 for (bl = b->loc; bl != NULL; bl = bl->next)
15211 {
15212 if (!bl->shlib_disabled
15213 && bpstat_check_location (bl, aspace, pc, ws))
15214 return 1;
15215 }
15216 }
15217
15218 return 0;
15219 }
15220
15221 /* Remove any references to OBJFILE which is going to be freed. */
15222
15223 void
15224 breakpoint_free_objfile (struct objfile *objfile)
15225 {
15226 struct bp_location **locp, *loc;
15227
15228 ALL_BP_LOCATIONS (loc, locp)
15229 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15230 loc->symtab = NULL;
15231 }
15232
15233 void
15234 initialize_breakpoint_ops (void)
15235 {
15236 static int initialized = 0;
15237
15238 struct breakpoint_ops *ops;
15239
15240 if (initialized)
15241 return;
15242 initialized = 1;
15243
15244 /* The breakpoint_ops structure to be inherit by all kinds of
15245 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15246 internal and momentary breakpoints, etc.). */
15247 ops = &bkpt_base_breakpoint_ops;
15248 *ops = base_breakpoint_ops;
15249 ops->re_set = bkpt_re_set;
15250 ops->insert_location = bkpt_insert_location;
15251 ops->remove_location = bkpt_remove_location;
15252 ops->breakpoint_hit = bkpt_breakpoint_hit;
15253 ops->create_sals_from_location = bkpt_create_sals_from_location;
15254 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15255 ops->decode_location = bkpt_decode_location;
15256
15257 /* The breakpoint_ops structure to be used in regular breakpoints. */
15258 ops = &bkpt_breakpoint_ops;
15259 *ops = bkpt_base_breakpoint_ops;
15260 ops->re_set = bkpt_re_set;
15261 ops->resources_needed = bkpt_resources_needed;
15262 ops->print_it = bkpt_print_it;
15263 ops->print_mention = bkpt_print_mention;
15264 ops->print_recreate = bkpt_print_recreate;
15265
15266 /* Ranged breakpoints. */
15267 ops = &ranged_breakpoint_ops;
15268 *ops = bkpt_breakpoint_ops;
15269 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15270 ops->resources_needed = resources_needed_ranged_breakpoint;
15271 ops->print_it = print_it_ranged_breakpoint;
15272 ops->print_one = print_one_ranged_breakpoint;
15273 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15274 ops->print_mention = print_mention_ranged_breakpoint;
15275 ops->print_recreate = print_recreate_ranged_breakpoint;
15276
15277 /* Internal breakpoints. */
15278 ops = &internal_breakpoint_ops;
15279 *ops = bkpt_base_breakpoint_ops;
15280 ops->re_set = internal_bkpt_re_set;
15281 ops->check_status = internal_bkpt_check_status;
15282 ops->print_it = internal_bkpt_print_it;
15283 ops->print_mention = internal_bkpt_print_mention;
15284
15285 /* Momentary breakpoints. */
15286 ops = &momentary_breakpoint_ops;
15287 *ops = bkpt_base_breakpoint_ops;
15288 ops->re_set = momentary_bkpt_re_set;
15289 ops->check_status = momentary_bkpt_check_status;
15290 ops->print_it = momentary_bkpt_print_it;
15291 ops->print_mention = momentary_bkpt_print_mention;
15292
15293 /* Probe breakpoints. */
15294 ops = &bkpt_probe_breakpoint_ops;
15295 *ops = bkpt_breakpoint_ops;
15296 ops->insert_location = bkpt_probe_insert_location;
15297 ops->remove_location = bkpt_probe_remove_location;
15298 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15299 ops->decode_location = bkpt_probe_decode_location;
15300
15301 /* Watchpoints. */
15302 ops = &watchpoint_breakpoint_ops;
15303 *ops = base_breakpoint_ops;
15304 ops->re_set = re_set_watchpoint;
15305 ops->insert_location = insert_watchpoint;
15306 ops->remove_location = remove_watchpoint;
15307 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15308 ops->check_status = check_status_watchpoint;
15309 ops->resources_needed = resources_needed_watchpoint;
15310 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15311 ops->print_it = print_it_watchpoint;
15312 ops->print_mention = print_mention_watchpoint;
15313 ops->print_recreate = print_recreate_watchpoint;
15314 ops->explains_signal = explains_signal_watchpoint;
15315
15316 /* Masked watchpoints. */
15317 ops = &masked_watchpoint_breakpoint_ops;
15318 *ops = watchpoint_breakpoint_ops;
15319 ops->insert_location = insert_masked_watchpoint;
15320 ops->remove_location = remove_masked_watchpoint;
15321 ops->resources_needed = resources_needed_masked_watchpoint;
15322 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15323 ops->print_it = print_it_masked_watchpoint;
15324 ops->print_one_detail = print_one_detail_masked_watchpoint;
15325 ops->print_mention = print_mention_masked_watchpoint;
15326 ops->print_recreate = print_recreate_masked_watchpoint;
15327
15328 /* Tracepoints. */
15329 ops = &tracepoint_breakpoint_ops;
15330 *ops = base_breakpoint_ops;
15331 ops->re_set = tracepoint_re_set;
15332 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15333 ops->print_one_detail = tracepoint_print_one_detail;
15334 ops->print_mention = tracepoint_print_mention;
15335 ops->print_recreate = tracepoint_print_recreate;
15336 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15337 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15338 ops->decode_location = tracepoint_decode_location;
15339
15340 /* Probe tracepoints. */
15341 ops = &tracepoint_probe_breakpoint_ops;
15342 *ops = tracepoint_breakpoint_ops;
15343 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15344 ops->decode_location = tracepoint_probe_decode_location;
15345
15346 /* Static tracepoints with marker (`-m'). */
15347 ops = &strace_marker_breakpoint_ops;
15348 *ops = tracepoint_breakpoint_ops;
15349 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15350 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15351 ops->decode_location = strace_marker_decode_location;
15352
15353 /* Fork catchpoints. */
15354 ops = &catch_fork_breakpoint_ops;
15355 *ops = base_breakpoint_ops;
15356 ops->insert_location = insert_catch_fork;
15357 ops->remove_location = remove_catch_fork;
15358 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15359 ops->print_it = print_it_catch_fork;
15360 ops->print_one = print_one_catch_fork;
15361 ops->print_mention = print_mention_catch_fork;
15362 ops->print_recreate = print_recreate_catch_fork;
15363
15364 /* Vfork catchpoints. */
15365 ops = &catch_vfork_breakpoint_ops;
15366 *ops = base_breakpoint_ops;
15367 ops->insert_location = insert_catch_vfork;
15368 ops->remove_location = remove_catch_vfork;
15369 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15370 ops->print_it = print_it_catch_vfork;
15371 ops->print_one = print_one_catch_vfork;
15372 ops->print_mention = print_mention_catch_vfork;
15373 ops->print_recreate = print_recreate_catch_vfork;
15374
15375 /* Exec catchpoints. */
15376 ops = &catch_exec_breakpoint_ops;
15377 *ops = base_breakpoint_ops;
15378 ops->insert_location = insert_catch_exec;
15379 ops->remove_location = remove_catch_exec;
15380 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15381 ops->print_it = print_it_catch_exec;
15382 ops->print_one = print_one_catch_exec;
15383 ops->print_mention = print_mention_catch_exec;
15384 ops->print_recreate = print_recreate_catch_exec;
15385
15386 /* Solib-related catchpoints. */
15387 ops = &catch_solib_breakpoint_ops;
15388 *ops = base_breakpoint_ops;
15389 ops->insert_location = insert_catch_solib;
15390 ops->remove_location = remove_catch_solib;
15391 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15392 ops->check_status = check_status_catch_solib;
15393 ops->print_it = print_it_catch_solib;
15394 ops->print_one = print_one_catch_solib;
15395 ops->print_mention = print_mention_catch_solib;
15396 ops->print_recreate = print_recreate_catch_solib;
15397
15398 ops = &dprintf_breakpoint_ops;
15399 *ops = bkpt_base_breakpoint_ops;
15400 ops->re_set = dprintf_re_set;
15401 ops->resources_needed = bkpt_resources_needed;
15402 ops->print_it = bkpt_print_it;
15403 ops->print_mention = bkpt_print_mention;
15404 ops->print_recreate = dprintf_print_recreate;
15405 ops->after_condition_true = dprintf_after_condition_true;
15406 ops->breakpoint_hit = dprintf_breakpoint_hit;
15407 }
15408
15409 /* Chain containing all defined "enable breakpoint" subcommands. */
15410
15411 static struct cmd_list_element *enablebreaklist = NULL;
15412
15413 /* See breakpoint.h. */
15414
15415 cmd_list_element *commands_cmd_element = nullptr;
15416
15417 void _initialize_breakpoint ();
15418 void
15419 _initialize_breakpoint ()
15420 {
15421 struct cmd_list_element *c;
15422
15423 initialize_breakpoint_ops ();
15424
15425 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib);
15426 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile);
15427 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change);
15428
15429 breakpoint_chain = 0;
15430 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15431 before a breakpoint is set. */
15432 breakpoint_count = 0;
15433
15434 tracepoint_count = 0;
15435
15436 add_com ("ignore", class_breakpoint, ignore_command, _("\
15437 Set ignore-count of breakpoint number N to COUNT.\n\
15438 Usage is `ignore N COUNT'."));
15439
15440 commands_cmd_element = add_com ("commands", class_breakpoint,
15441 commands_command, _("\
15442 Set commands to be executed when the given breakpoints are hit.\n\
15443 Give a space-separated breakpoint list as argument after \"commands\".\n\
15444 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15445 (e.g. `5-7').\n\
15446 With no argument, the targeted breakpoint is the last one set.\n\
15447 The commands themselves follow starting on the next line.\n\
15448 Type a line containing \"end\" to indicate the end of them.\n\
15449 Give \"silent\" as the first line to make the breakpoint silent;\n\
15450 then no output is printed when it is hit, except what the commands print."));
15451
15452 c = add_com ("condition", class_breakpoint, condition_command, _("\
15453 Specify breakpoint number N to break only if COND is true.\n\
15454 Usage is `condition N COND', where N is an integer and COND is an\n\
15455 expression to be evaluated whenever breakpoint N is reached."));
15456 set_cmd_completer (c, condition_completer);
15457
15458 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15459 Set a temporary breakpoint.\n\
15460 Like \"break\" except the breakpoint is only temporary,\n\
15461 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15462 by using \"enable delete\" on the breakpoint number.\n\
15463 \n"
15464 BREAK_ARGS_HELP ("tbreak")));
15465 set_cmd_completer (c, location_completer);
15466
15467 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15468 Set a hardware assisted breakpoint.\n\
15469 Like \"break\" except the breakpoint requires hardware support,\n\
15470 some target hardware may not have this support.\n\
15471 \n"
15472 BREAK_ARGS_HELP ("hbreak")));
15473 set_cmd_completer (c, location_completer);
15474
15475 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15476 Set a temporary hardware assisted breakpoint.\n\
15477 Like \"hbreak\" except the breakpoint is only temporary,\n\
15478 so it will be deleted when hit.\n\
15479 \n"
15480 BREAK_ARGS_HELP ("thbreak")));
15481 set_cmd_completer (c, location_completer);
15482
15483 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15484 Enable all or some breakpoints.\n\
15485 Usage: enable [BREAKPOINTNUM]...\n\
15486 Give breakpoint numbers (separated by spaces) as arguments.\n\
15487 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15488 This is used to cancel the effect of the \"disable\" command.\n\
15489 With a subcommand you can enable temporarily."),
15490 &enablelist, "enable ", 1, &cmdlist);
15491
15492 add_com_alias ("en", "enable", class_breakpoint, 1);
15493
15494 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15495 Enable all or some breakpoints.\n\
15496 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
15497 Give breakpoint numbers (separated by spaces) as arguments.\n\
15498 This is used to cancel the effect of the \"disable\" command.\n\
15499 May be abbreviated to simply \"enable\"."),
15500 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
15501
15502 add_cmd ("once", no_class, enable_once_command, _("\
15503 Enable some breakpoints for one hit.\n\
15504 Usage: enable breakpoints once BREAKPOINTNUM...\n\
15505 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15506 &enablebreaklist);
15507
15508 add_cmd ("delete", no_class, enable_delete_command, _("\
15509 Enable some breakpoints and delete when hit.\n\
15510 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
15511 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15512 &enablebreaklist);
15513
15514 add_cmd ("count", no_class, enable_count_command, _("\
15515 Enable some breakpoints for COUNT hits.\n\
15516 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
15517 If a breakpoint is hit while enabled in this fashion,\n\
15518 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15519 &enablebreaklist);
15520
15521 add_cmd ("delete", no_class, enable_delete_command, _("\
15522 Enable some breakpoints and delete when hit.\n\
15523 Usage: enable delete BREAKPOINTNUM...\n\
15524 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15525 &enablelist);
15526
15527 add_cmd ("once", no_class, enable_once_command, _("\
15528 Enable some breakpoints for one hit.\n\
15529 Usage: enable once BREAKPOINTNUM...\n\
15530 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15531 &enablelist);
15532
15533 add_cmd ("count", no_class, enable_count_command, _("\
15534 Enable some breakpoints for COUNT hits.\n\
15535 Usage: enable count COUNT BREAKPOINTNUM...\n\
15536 If a breakpoint is hit while enabled in this fashion,\n\
15537 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15538 &enablelist);
15539
15540 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15541 Disable all or some breakpoints.\n\
15542 Usage: disable [BREAKPOINTNUM]...\n\
15543 Arguments are breakpoint numbers with spaces in between.\n\
15544 To disable all breakpoints, give no argument.\n\
15545 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15546 &disablelist, "disable ", 1, &cmdlist);
15547 add_com_alias ("dis", "disable", class_breakpoint, 1);
15548 add_com_alias ("disa", "disable", class_breakpoint, 1);
15549
15550 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
15551 Disable all or some breakpoints.\n\
15552 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
15553 Arguments are breakpoint numbers with spaces in between.\n\
15554 To disable all breakpoints, give no argument.\n\
15555 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15556 This command may be abbreviated \"disable\"."),
15557 &disablelist);
15558
15559 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15560 Delete all or some breakpoints.\n\
15561 Usage: delete [BREAKPOINTNUM]...\n\
15562 Arguments are breakpoint numbers with spaces in between.\n\
15563 To delete all breakpoints, give no argument.\n\
15564 \n\
15565 Also a prefix command for deletion of other GDB objects."),
15566 &deletelist, "delete ", 1, &cmdlist);
15567 add_com_alias ("d", "delete", class_breakpoint, 1);
15568 add_com_alias ("del", "delete", class_breakpoint, 1);
15569
15570 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
15571 Delete all or some breakpoints or auto-display expressions.\n\
15572 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
15573 Arguments are breakpoint numbers with spaces in between.\n\
15574 To delete all breakpoints, give no argument.\n\
15575 This command may be abbreviated \"delete\"."),
15576 &deletelist);
15577
15578 add_com ("clear", class_breakpoint, clear_command, _("\
15579 Clear breakpoint at specified location.\n\
15580 Argument may be a linespec, explicit, or address location as described below.\n\
15581 \n\
15582 With no argument, clears all breakpoints in the line that the selected frame\n\
15583 is executing in.\n"
15584 "\n" LOCATION_HELP_STRING "\n\n\
15585 See also the \"delete\" command which clears breakpoints by number."));
15586 add_com_alias ("cl", "clear", class_breakpoint, 1);
15587
15588 c = add_com ("break", class_breakpoint, break_command, _("\
15589 Set breakpoint at specified location.\n"
15590 BREAK_ARGS_HELP ("break")));
15591 set_cmd_completer (c, location_completer);
15592
15593 add_com_alias ("b", "break", class_run, 1);
15594 add_com_alias ("br", "break", class_run, 1);
15595 add_com_alias ("bre", "break", class_run, 1);
15596 add_com_alias ("brea", "break", class_run, 1);
15597
15598 if (dbx_commands)
15599 {
15600 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15601 Break in function/address or break at a line in the current file."),
15602 &stoplist, "stop ", 1, &cmdlist);
15603 add_cmd ("in", class_breakpoint, stopin_command,
15604 _("Break in function or address."), &stoplist);
15605 add_cmd ("at", class_breakpoint, stopat_command,
15606 _("Break at a line in the current file."), &stoplist);
15607 add_com ("status", class_info, info_breakpoints_command, _("\
15608 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15609 The \"Type\" column indicates one of:\n\
15610 \tbreakpoint - normal breakpoint\n\
15611 \twatchpoint - watchpoint\n\
15612 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15613 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15614 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15615 address and file/line number respectively.\n\
15616 \n\
15617 Convenience variable \"$_\" and default examine address for \"x\"\n\
15618 are set to the address of the last breakpoint listed unless the command\n\
15619 is prefixed with \"server \".\n\n\
15620 Convenience variable \"$bpnum\" contains the number of the last\n\
15621 breakpoint set."));
15622 }
15623
15624 add_info ("breakpoints", info_breakpoints_command, _("\
15625 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15626 The \"Type\" column indicates one of:\n\
15627 \tbreakpoint - normal breakpoint\n\
15628 \twatchpoint - watchpoint\n\
15629 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15630 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15631 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15632 address and file/line number respectively.\n\
15633 \n\
15634 Convenience variable \"$_\" and default examine address for \"x\"\n\
15635 are set to the address of the last breakpoint listed unless the command\n\
15636 is prefixed with \"server \".\n\n\
15637 Convenience variable \"$bpnum\" contains the number of the last\n\
15638 breakpoint set."));
15639
15640 add_info_alias ("b", "breakpoints", 1);
15641
15642 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15643 Status of all breakpoints, or breakpoint number NUMBER.\n\
15644 The \"Type\" column indicates one of:\n\
15645 \tbreakpoint - normal breakpoint\n\
15646 \twatchpoint - watchpoint\n\
15647 \tlongjmp - internal breakpoint used to step through longjmp()\n\
15648 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15649 \tuntil - internal breakpoint used by the \"until\" command\n\
15650 \tfinish - internal breakpoint used by the \"finish\" command\n\
15651 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15652 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15653 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15654 address and file/line number respectively.\n\
15655 \n\
15656 Convenience variable \"$_\" and default examine address for \"x\"\n\
15657 are set to the address of the last breakpoint listed unless the command\n\
15658 is prefixed with \"server \".\n\n\
15659 Convenience variable \"$bpnum\" contains the number of the last\n\
15660 breakpoint set."),
15661 &maintenanceinfolist);
15662
15663 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
15664 Set catchpoints to catch events."),
15665 &catch_cmdlist, "catch ",
15666 0/*allow-unknown*/, &cmdlist);
15667
15668 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
15669 Set temporary catchpoints to catch events."),
15670 &tcatch_cmdlist, "tcatch ",
15671 0/*allow-unknown*/, &cmdlist);
15672
15673 add_catch_command ("fork", _("Catch calls to fork."),
15674 catch_fork_command_1,
15675 NULL,
15676 (void *) (uintptr_t) catch_fork_permanent,
15677 (void *) (uintptr_t) catch_fork_temporary);
15678 add_catch_command ("vfork", _("Catch calls to vfork."),
15679 catch_fork_command_1,
15680 NULL,
15681 (void *) (uintptr_t) catch_vfork_permanent,
15682 (void *) (uintptr_t) catch_vfork_temporary);
15683 add_catch_command ("exec", _("Catch calls to exec."),
15684 catch_exec_command_1,
15685 NULL,
15686 CATCH_PERMANENT,
15687 CATCH_TEMPORARY);
15688 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15689 Usage: catch load [REGEX]\n\
15690 If REGEX is given, only stop for libraries matching the regular expression."),
15691 catch_load_command_1,
15692 NULL,
15693 CATCH_PERMANENT,
15694 CATCH_TEMPORARY);
15695 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15696 Usage: catch unload [REGEX]\n\
15697 If REGEX is given, only stop for libraries matching the regular expression."),
15698 catch_unload_command_1,
15699 NULL,
15700 CATCH_PERMANENT,
15701 CATCH_TEMPORARY);
15702
15703 c = add_com ("watch", class_breakpoint, watch_command, _("\
15704 Set a watchpoint for an expression.\n\
15705 Usage: watch [-l|-location] EXPRESSION\n\
15706 A watchpoint stops execution of your program whenever the value of\n\
15707 an expression changes.\n\
15708 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15709 the memory to which it refers."));
15710 set_cmd_completer (c, expression_completer);
15711
15712 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15713 Set a read watchpoint for an expression.\n\
15714 Usage: rwatch [-l|-location] EXPRESSION\n\
15715 A watchpoint stops execution of your program whenever the value of\n\
15716 an expression is read.\n\
15717 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15718 the memory to which it refers."));
15719 set_cmd_completer (c, expression_completer);
15720
15721 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15722 Set a watchpoint for an expression.\n\
15723 Usage: awatch [-l|-location] EXPRESSION\n\
15724 A watchpoint stops execution of your program whenever the value of\n\
15725 an expression is either read or written.\n\
15726 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15727 the memory to which it refers."));
15728 set_cmd_completer (c, expression_completer);
15729
15730 add_info ("watchpoints", info_watchpoints_command, _("\
15731 Status of specified watchpoints (all watchpoints if no argument)."));
15732
15733 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15734 respond to changes - contrary to the description. */
15735 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15736 &can_use_hw_watchpoints, _("\
15737 Set debugger's willingness to use watchpoint hardware."), _("\
15738 Show debugger's willingness to use watchpoint hardware."), _("\
15739 If zero, gdb will not use hardware for new watchpoints, even if\n\
15740 such is available. (However, any hardware watchpoints that were\n\
15741 created before setting this to nonzero, will continue to use watchpoint\n\
15742 hardware.)"),
15743 NULL,
15744 show_can_use_hw_watchpoints,
15745 &setlist, &showlist);
15746
15747 can_use_hw_watchpoints = 1;
15748
15749 /* Tracepoint manipulation commands. */
15750
15751 c = add_com ("trace", class_breakpoint, trace_command, _("\
15752 Set a tracepoint at specified location.\n\
15753 \n"
15754 BREAK_ARGS_HELP ("trace") "\n\
15755 Do \"help tracepoints\" for info on other tracepoint commands."));
15756 set_cmd_completer (c, location_completer);
15757
15758 add_com_alias ("tp", "trace", class_breakpoint, 0);
15759 add_com_alias ("tr", "trace", class_breakpoint, 1);
15760 add_com_alias ("tra", "trace", class_breakpoint, 1);
15761 add_com_alias ("trac", "trace", class_breakpoint, 1);
15762
15763 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
15764 Set a fast tracepoint at specified location.\n\
15765 \n"
15766 BREAK_ARGS_HELP ("ftrace") "\n\
15767 Do \"help tracepoints\" for info on other tracepoint commands."));
15768 set_cmd_completer (c, location_completer);
15769
15770 c = add_com ("strace", class_breakpoint, strace_command, _("\
15771 Set a static tracepoint at location or marker.\n\
15772 \n\
15773 strace [LOCATION] [if CONDITION]\n\
15774 LOCATION may be a linespec, explicit, or address location (described below) \n\
15775 or -m MARKER_ID.\n\n\
15776 If a marker id is specified, probe the marker with that name. With\n\
15777 no LOCATION, uses current execution address of the selected stack frame.\n\
15778 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15779 This collects arbitrary user data passed in the probe point call to the\n\
15780 tracing library. You can inspect it when analyzing the trace buffer,\n\
15781 by printing the $_sdata variable like any other convenience variable.\n\
15782 \n\
15783 CONDITION is a boolean expression.\n\
15784 \n" LOCATION_HELP_STRING "\n\n\
15785 Multiple tracepoints at one place are permitted, and useful if their\n\
15786 conditions are different.\n\
15787 \n\
15788 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15789 Do \"help tracepoints\" for info on other tracepoint commands."));
15790 set_cmd_completer (c, location_completer);
15791
15792 add_info ("tracepoints", info_tracepoints_command, _("\
15793 Status of specified tracepoints (all tracepoints if no argument).\n\
15794 Convenience variable \"$tpnum\" contains the number of the\n\
15795 last tracepoint set."));
15796
15797 add_info_alias ("tp", "tracepoints", 1);
15798
15799 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15800 Delete specified tracepoints.\n\
15801 Arguments are tracepoint numbers, separated by spaces.\n\
15802 No argument means delete all tracepoints."),
15803 &deletelist);
15804 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
15805
15806 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15807 Disable specified tracepoints.\n\
15808 Arguments are tracepoint numbers, separated by spaces.\n\
15809 No argument means disable all tracepoints."),
15810 &disablelist);
15811 deprecate_cmd (c, "disable");
15812
15813 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15814 Enable specified tracepoints.\n\
15815 Arguments are tracepoint numbers, separated by spaces.\n\
15816 No argument means enable all tracepoints."),
15817 &enablelist);
15818 deprecate_cmd (c, "enable");
15819
15820 add_com ("passcount", class_trace, trace_pass_command, _("\
15821 Set the passcount for a tracepoint.\n\
15822 The trace will end when the tracepoint has been passed 'count' times.\n\
15823 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15824 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15825
15826 add_basic_prefix_cmd ("save", class_breakpoint,
15827 _("Save breakpoint definitions as a script."),
15828 &save_cmdlist, "save ",
15829 0/*allow-unknown*/, &cmdlist);
15830
15831 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15832 Save current breakpoint definitions as a script.\n\
15833 This includes all types of breakpoints (breakpoints, watchpoints,\n\
15834 catchpoints, tracepoints). Use the 'source' command in another debug\n\
15835 session to restore them."),
15836 &save_cmdlist);
15837 set_cmd_completer (c, filename_completer);
15838
15839 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
15840 Save current tracepoint definitions as a script.\n\
15841 Use the 'source' command in another debug session to restore them."),
15842 &save_cmdlist);
15843 set_cmd_completer (c, filename_completer);
15844
15845 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
15846 deprecate_cmd (c, "save tracepoints");
15847
15848 add_basic_prefix_cmd ("breakpoint", class_maintenance, _("\
15849 Breakpoint specific settings.\n\
15850 Configure various breakpoint-specific variables such as\n\
15851 pending breakpoint behavior."),
15852 &breakpoint_set_cmdlist, "set breakpoint ",
15853 0/*allow-unknown*/, &setlist);
15854 add_show_prefix_cmd ("breakpoint", class_maintenance, _("\
15855 Breakpoint specific settings.\n\
15856 Configure various breakpoint-specific variables such as\n\
15857 pending breakpoint behavior."),
15858 &breakpoint_show_cmdlist, "show breakpoint ",
15859 0/*allow-unknown*/, &showlist);
15860
15861 add_setshow_auto_boolean_cmd ("pending", no_class,
15862 &pending_break_support, _("\
15863 Set debugger's behavior regarding pending breakpoints."), _("\
15864 Show debugger's behavior regarding pending breakpoints."), _("\
15865 If on, an unrecognized breakpoint location will cause gdb to create a\n\
15866 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15867 an error. If auto, an unrecognized breakpoint location results in a\n\
15868 user-query to see if a pending breakpoint should be created."),
15869 NULL,
15870 show_pending_break_support,
15871 &breakpoint_set_cmdlist,
15872 &breakpoint_show_cmdlist);
15873
15874 pending_break_support = AUTO_BOOLEAN_AUTO;
15875
15876 add_setshow_boolean_cmd ("auto-hw", no_class,
15877 &automatic_hardware_breakpoints, _("\
15878 Set automatic usage of hardware breakpoints."), _("\
15879 Show automatic usage of hardware breakpoints."), _("\
15880 If set, the debugger will automatically use hardware breakpoints for\n\
15881 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15882 a warning will be emitted for such breakpoints."),
15883 NULL,
15884 show_automatic_hardware_breakpoints,
15885 &breakpoint_set_cmdlist,
15886 &breakpoint_show_cmdlist);
15887
15888 add_setshow_boolean_cmd ("always-inserted", class_support,
15889 &always_inserted_mode, _("\
15890 Set mode for inserting breakpoints."), _("\
15891 Show mode for inserting breakpoints."), _("\
15892 When this mode is on, breakpoints are inserted immediately as soon as\n\
15893 they're created, kept inserted even when execution stops, and removed\n\
15894 only when the user deletes them. When this mode is off (the default),\n\
15895 breakpoints are inserted only when execution continues, and removed\n\
15896 when execution stops."),
15897 NULL,
15898 &show_always_inserted_mode,
15899 &breakpoint_set_cmdlist,
15900 &breakpoint_show_cmdlist);
15901
15902 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15903 condition_evaluation_enums,
15904 &condition_evaluation_mode_1, _("\
15905 Set mode of breakpoint condition evaluation."), _("\
15906 Show mode of breakpoint condition evaluation."), _("\
15907 When this is set to \"host\", breakpoint conditions will be\n\
15908 evaluated on the host's side by GDB. When it is set to \"target\",\n\
15909 breakpoint conditions will be downloaded to the target (if the target\n\
15910 supports such feature) and conditions will be evaluated on the target's side.\n\
15911 If this is set to \"auto\" (default), this will be automatically set to\n\
15912 \"target\" if it supports condition evaluation, otherwise it will\n\
15913 be set to \"host\"."),
15914 &set_condition_evaluation_mode,
15915 &show_condition_evaluation_mode,
15916 &breakpoint_set_cmdlist,
15917 &breakpoint_show_cmdlist);
15918
15919 add_com ("break-range", class_breakpoint, break_range_command, _("\
15920 Set a breakpoint for an address range.\n\
15921 break-range START-LOCATION, END-LOCATION\n\
15922 where START-LOCATION and END-LOCATION can be one of the following:\n\
15923 LINENUM, for that line in the current file,\n\
15924 FILE:LINENUM, for that line in that file,\n\
15925 +OFFSET, for that number of lines after the current line\n\
15926 or the start of the range\n\
15927 FUNCTION, for the first line in that function,\n\
15928 FILE:FUNCTION, to distinguish among like-named static functions.\n\
15929 *ADDRESS, for the instruction at that address.\n\
15930 \n\
15931 The breakpoint will stop execution of the inferior whenever it executes\n\
15932 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
15933 range (including START-LOCATION and END-LOCATION)."));
15934
15935 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
15936 Set a dynamic printf at specified location.\n\
15937 dprintf location,format string,arg1,arg2,...\n\
15938 location may be a linespec, explicit, or address location.\n"
15939 "\n" LOCATION_HELP_STRING));
15940 set_cmd_completer (c, location_completer);
15941
15942 add_setshow_enum_cmd ("dprintf-style", class_support,
15943 dprintf_style_enums, &dprintf_style, _("\
15944 Set the style of usage for dynamic printf."), _("\
15945 Show the style of usage for dynamic printf."), _("\
15946 This setting chooses how GDB will do a dynamic printf.\n\
15947 If the value is \"gdb\", then the printing is done by GDB to its own\n\
15948 console, as with the \"printf\" command.\n\
15949 If the value is \"call\", the print is done by calling a function in your\n\
15950 program; by default printf(), but you can choose a different function or\n\
15951 output stream by setting dprintf-function and dprintf-channel."),
15952 update_dprintf_commands, NULL,
15953 &setlist, &showlist);
15954
15955 dprintf_function = xstrdup ("printf");
15956 add_setshow_string_cmd ("dprintf-function", class_support,
15957 &dprintf_function, _("\
15958 Set the function to use for dynamic printf."), _("\
15959 Show the function to use for dynamic printf."), NULL,
15960 update_dprintf_commands, NULL,
15961 &setlist, &showlist);
15962
15963 dprintf_channel = xstrdup ("");
15964 add_setshow_string_cmd ("dprintf-channel", class_support,
15965 &dprintf_channel, _("\
15966 Set the channel to use for dynamic printf."), _("\
15967 Show the channel to use for dynamic printf."), NULL,
15968 update_dprintf_commands, NULL,
15969 &setlist, &showlist);
15970
15971 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
15972 &disconnected_dprintf, _("\
15973 Set whether dprintf continues after GDB disconnects."), _("\
15974 Show whether dprintf continues after GDB disconnects."), _("\
15975 Use this to let dprintf commands continue to hit and produce output\n\
15976 even if GDB disconnects or detaches from the target."),
15977 NULL,
15978 NULL,
15979 &setlist, &showlist);
15980
15981 add_com ("agent-printf", class_vars, agent_printf_command, _("\
15982 Target agent only formatted printing, like the C \"printf\" function.\n\
15983 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
15984 This supports most C printf format specifications, like %s, %d, etc.\n\
15985 This is useful for formatted output in user-defined commands."));
15986
15987 automatic_hardware_breakpoints = true;
15988
15989 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed);
15990 gdb::observers::thread_exit.attach (remove_threaded_breakpoints);
15991 }