gdb/breakpoint: add flags to 'condition' and 'break' commands to force condition
[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 /* Parse COND_STRING in the context of LOC and set as the condition
834 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
835 the number of LOC within its owner. In case of parsing error, mark
836 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
837
838 static void
839 set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
840 int bp_num, int loc_num)
841 {
842 bool has_junk = false;
843 try
844 {
845 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
846 block_for_pc (loc->address), 0);
847 if (*cond_string != 0)
848 has_junk = true;
849 else
850 {
851 loc->cond = std::move (new_exp);
852 if (loc->disabled_by_cond && loc->enabled)
853 printf_filtered (_("Breakpoint %d's condition is now valid at "
854 "location %d, enabling.\n"),
855 bp_num, loc_num);
856
857 loc->disabled_by_cond = false;
858 }
859 }
860 catch (const gdb_exception_error &e)
861 {
862 if (loc->enabled)
863 {
864 /* Warn if a user-enabled location is now becoming disabled-by-cond.
865 BP_NUM is 0 if the breakpoint is being defined for the first
866 time using the "break ... if ..." command, and non-zero if
867 already defined. */
868 if (bp_num != 0)
869 warning (_("failed to validate condition at location %d.%d, "
870 "disabling:\n %s"), bp_num, loc_num, e.what ());
871 else
872 warning (_("failed to validate condition at location %d, "
873 "disabling:\n %s"), loc_num, e.what ());
874 }
875
876 loc->disabled_by_cond = true;
877 }
878
879 if (has_junk)
880 error (_("Garbage '%s' follows condition"), cond_string);
881 }
882
883 void
884 set_breakpoint_condition (struct breakpoint *b, const char *exp,
885 int from_tty, bool force)
886 {
887 if (*exp == 0)
888 {
889 xfree (b->cond_string);
890 b->cond_string = nullptr;
891
892 if (is_watchpoint (b))
893 static_cast<watchpoint *> (b)->cond_exp.reset ();
894 else
895 {
896 int loc_num = 1;
897 for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)
898 {
899 loc->cond.reset ();
900 if (loc->disabled_by_cond && loc->enabled)
901 printf_filtered (_("Breakpoint %d's condition is now valid at "
902 "location %d, enabling.\n"),
903 b->number, loc_num);
904 loc->disabled_by_cond = false;
905 loc_num++;
906
907 /* No need to free the condition agent expression
908 bytecode (if we have one). We will handle this
909 when we go through update_global_location_list. */
910 }
911 }
912
913 if (from_tty)
914 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
915 }
916 else
917 {
918 if (is_watchpoint (b))
919 {
920 innermost_block_tracker tracker;
921 const char *arg = exp;
922 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
923 if (*arg != 0)
924 error (_("Junk at end of expression"));
925 watchpoint *w = static_cast<watchpoint *> (b);
926 w->cond_exp = std::move (new_exp);
927 w->cond_exp_valid_block = tracker.block ();
928 }
929 else
930 {
931 /* Parse and set condition expressions. We make two passes.
932 In the first, we parse the condition string to see if it
933 is valid in at least one location. If so, the condition
934 would be accepted. So we go ahead and set the locations'
935 conditions. In case no valid case is found, we throw
936 the error and the condition string will be rejected.
937 This two-pass approach is taken to avoid setting the
938 state of locations in case of a reject. */
939 for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)
940 {
941 try
942 {
943 const char *arg = exp;
944 parse_exp_1 (&arg, loc->address,
945 block_for_pc (loc->address), 0);
946 if (*arg != 0)
947 error (_("Junk at end of expression"));
948 break;
949 }
950 catch (const gdb_exception_error &e)
951 {
952 /* Condition string is invalid. If this happens to
953 be the last loc, abandon (if not forced) or continue
954 (if forced). */
955 if (loc->next == nullptr && !force)
956 throw;
957 }
958 }
959
960 /* If we reach here, the condition is valid at some locations. */
961 int loc_num = 1;
962 for (bp_location *loc = b->loc; loc != nullptr;
963 loc = loc->next, loc_num++)
964 set_breakpoint_location_condition (exp, loc, b->number, loc_num);
965 }
966
967 /* We know that the new condition parsed successfully. The
968 condition string of the breakpoint can be safely updated. */
969 xfree (b->cond_string);
970 b->cond_string = xstrdup (exp);
971 b->condition_not_parsed = 0;
972 }
973 mark_breakpoint_modified (b);
974
975 gdb::observers::breakpoint_modified.notify (b);
976 }
977
978 /* Completion for the "condition" command. */
979
980 static void
981 condition_completer (struct cmd_list_element *cmd,
982 completion_tracker &tracker,
983 const char *text, const char *word)
984 {
985 const char *space;
986
987 text = skip_spaces (text);
988 space = skip_to_space (text);
989 if (*space == '\0')
990 {
991 int len;
992 struct breakpoint *b;
993
994 if (text[0] == '$')
995 {
996 /* We don't support completion of history indices. */
997 if (!isdigit (text[1]))
998 complete_internalvar (tracker, &text[1]);
999 return;
1000 }
1001
1002 /* We're completing the breakpoint number. */
1003 len = strlen (text);
1004
1005 ALL_BREAKPOINTS (b)
1006 {
1007 char number[50];
1008
1009 xsnprintf (number, sizeof (number), "%d", b->number);
1010
1011 if (strncmp (number, text, len) == 0)
1012 tracker.add_completion (make_unique_xstrdup (number));
1013 }
1014
1015 return;
1016 }
1017
1018 /* We're completing the expression part. */
1019 text = skip_spaces (space);
1020 expression_completer (cmd, tracker, text, word);
1021 }
1022
1023 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1024
1025 static void
1026 condition_command (const char *arg, int from_tty)
1027 {
1028 struct breakpoint *b;
1029 const char *p;
1030 int bnum;
1031
1032 if (arg == 0)
1033 error_no_arg (_("breakpoint number"));
1034
1035 p = arg;
1036
1037 /* Check if the "-force" flag was passed. */
1038 bool force = false;
1039 const char *tok = skip_spaces (p);
1040 const char *end_tok = skip_to_space (tok);
1041 int toklen = end_tok - tok;
1042 if (toklen >= 1 && strncmp (tok, "-force", toklen) == 0)
1043 {
1044 force = true;
1045 p = end_tok + 1;
1046 }
1047
1048 bnum = get_number (&p);
1049 if (bnum == 0)
1050 error (_("Bad breakpoint argument: '%s'"), arg);
1051
1052 ALL_BREAKPOINTS (b)
1053 if (b->number == bnum)
1054 {
1055 /* Check if this breakpoint has a "stop" method implemented in an
1056 extension language. This method and conditions entered into GDB
1057 from the CLI are mutually exclusive. */
1058 const struct extension_language_defn *extlang
1059 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1060
1061 if (extlang != NULL)
1062 {
1063 error (_("Only one stop condition allowed. There is currently"
1064 " a %s stop condition defined for this breakpoint."),
1065 ext_lang_capitalized_name (extlang));
1066 }
1067 set_breakpoint_condition (b, p, from_tty, force);
1068
1069 if (is_breakpoint (b))
1070 update_global_location_list (UGLL_MAY_INSERT);
1071
1072 return;
1073 }
1074
1075 error (_("No breakpoint number %d."), bnum);
1076 }
1077
1078 /* Check that COMMAND do not contain commands that are suitable
1079 only for tracepoints and not suitable for ordinary breakpoints.
1080 Throw if any such commands is found. */
1081
1082 static void
1083 check_no_tracepoint_commands (struct command_line *commands)
1084 {
1085 struct command_line *c;
1086
1087 for (c = commands; c; c = c->next)
1088 {
1089 if (c->control_type == while_stepping_control)
1090 error (_("The 'while-stepping' command can "
1091 "only be used for tracepoints"));
1092
1093 check_no_tracepoint_commands (c->body_list_0.get ());
1094 check_no_tracepoint_commands (c->body_list_1.get ());
1095
1096 /* Not that command parsing removes leading whitespace and comment
1097 lines and also empty lines. So, we only need to check for
1098 command directly. */
1099 if (strstr (c->line, "collect ") == c->line)
1100 error (_("The 'collect' command can only be used for tracepoints"));
1101
1102 if (strstr (c->line, "teval ") == c->line)
1103 error (_("The 'teval' command can only be used for tracepoints"));
1104 }
1105 }
1106
1107 struct longjmp_breakpoint : public breakpoint
1108 {
1109 ~longjmp_breakpoint () override;
1110 };
1111
1112 /* Encapsulate tests for different types of tracepoints. */
1113
1114 static bool
1115 is_tracepoint_type (bptype type)
1116 {
1117 return (type == bp_tracepoint
1118 || type == bp_fast_tracepoint
1119 || type == bp_static_tracepoint);
1120 }
1121
1122 static bool
1123 is_longjmp_type (bptype type)
1124 {
1125 return type == bp_longjmp || type == bp_exception;
1126 }
1127
1128 /* See breakpoint.h. */
1129
1130 bool
1131 is_tracepoint (const struct breakpoint *b)
1132 {
1133 return is_tracepoint_type (b->type);
1134 }
1135
1136 /* Factory function to create an appropriate instance of breakpoint given
1137 TYPE. */
1138
1139 static std::unique_ptr<breakpoint>
1140 new_breakpoint_from_type (bptype type)
1141 {
1142 breakpoint *b;
1143
1144 if (is_tracepoint_type (type))
1145 b = new tracepoint ();
1146 else if (is_longjmp_type (type))
1147 b = new longjmp_breakpoint ();
1148 else
1149 b = new breakpoint ();
1150
1151 return std::unique_ptr<breakpoint> (b);
1152 }
1153
1154 /* A helper function that validates that COMMANDS are valid for a
1155 breakpoint. This function will throw an exception if a problem is
1156 found. */
1157
1158 static void
1159 validate_commands_for_breakpoint (struct breakpoint *b,
1160 struct command_line *commands)
1161 {
1162 if (is_tracepoint (b))
1163 {
1164 struct tracepoint *t = (struct tracepoint *) b;
1165 struct command_line *c;
1166 struct command_line *while_stepping = 0;
1167
1168 /* Reset the while-stepping step count. The previous commands
1169 might have included a while-stepping action, while the new
1170 ones might not. */
1171 t->step_count = 0;
1172
1173 /* We need to verify that each top-level element of commands is
1174 valid for tracepoints, that there's at most one
1175 while-stepping element, and that the while-stepping's body
1176 has valid tracing commands excluding nested while-stepping.
1177 We also need to validate the tracepoint action line in the
1178 context of the tracepoint --- validate_actionline actually
1179 has side effects, like setting the tracepoint's
1180 while-stepping STEP_COUNT, in addition to checking if the
1181 collect/teval actions parse and make sense in the
1182 tracepoint's context. */
1183 for (c = commands; c; c = c->next)
1184 {
1185 if (c->control_type == while_stepping_control)
1186 {
1187 if (b->type == bp_fast_tracepoint)
1188 error (_("The 'while-stepping' command "
1189 "cannot be used for fast tracepoint"));
1190 else if (b->type == bp_static_tracepoint)
1191 error (_("The 'while-stepping' command "
1192 "cannot be used for static tracepoint"));
1193
1194 if (while_stepping)
1195 error (_("The 'while-stepping' command "
1196 "can be used only once"));
1197 else
1198 while_stepping = c;
1199 }
1200
1201 validate_actionline (c->line, b);
1202 }
1203 if (while_stepping)
1204 {
1205 struct command_line *c2;
1206
1207 gdb_assert (while_stepping->body_list_1 == nullptr);
1208 c2 = while_stepping->body_list_0.get ();
1209 for (; c2; c2 = c2->next)
1210 {
1211 if (c2->control_type == while_stepping_control)
1212 error (_("The 'while-stepping' command cannot be nested"));
1213 }
1214 }
1215 }
1216 else
1217 {
1218 check_no_tracepoint_commands (commands);
1219 }
1220 }
1221
1222 /* Return a vector of all the static tracepoints set at ADDR. The
1223 caller is responsible for releasing the vector. */
1224
1225 std::vector<breakpoint *>
1226 static_tracepoints_here (CORE_ADDR addr)
1227 {
1228 struct breakpoint *b;
1229 std::vector<breakpoint *> found;
1230 struct bp_location *loc;
1231
1232 ALL_BREAKPOINTS (b)
1233 if (b->type == bp_static_tracepoint)
1234 {
1235 for (loc = b->loc; loc; loc = loc->next)
1236 if (loc->address == addr)
1237 found.push_back (b);
1238 }
1239
1240 return found;
1241 }
1242
1243 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1244 validate that only allowed commands are included. */
1245
1246 void
1247 breakpoint_set_commands (struct breakpoint *b,
1248 counted_command_line &&commands)
1249 {
1250 validate_commands_for_breakpoint (b, commands.get ());
1251
1252 b->commands = std::move (commands);
1253 gdb::observers::breakpoint_modified.notify (b);
1254 }
1255
1256 /* Set the internal `silent' flag on the breakpoint. Note that this
1257 is not the same as the "silent" that may appear in the breakpoint's
1258 commands. */
1259
1260 void
1261 breakpoint_set_silent (struct breakpoint *b, int silent)
1262 {
1263 int old_silent = b->silent;
1264
1265 b->silent = silent;
1266 if (old_silent != silent)
1267 gdb::observers::breakpoint_modified.notify (b);
1268 }
1269
1270 /* Set the thread for this breakpoint. If THREAD is -1, make the
1271 breakpoint work for any thread. */
1272
1273 void
1274 breakpoint_set_thread (struct breakpoint *b, int thread)
1275 {
1276 int old_thread = b->thread;
1277
1278 b->thread = thread;
1279 if (old_thread != thread)
1280 gdb::observers::breakpoint_modified.notify (b);
1281 }
1282
1283 /* Set the task for this breakpoint. If TASK is 0, make the
1284 breakpoint work for any task. */
1285
1286 void
1287 breakpoint_set_task (struct breakpoint *b, int task)
1288 {
1289 int old_task = b->task;
1290
1291 b->task = task;
1292 if (old_task != task)
1293 gdb::observers::breakpoint_modified.notify (b);
1294 }
1295
1296 static void
1297 commands_command_1 (const char *arg, int from_tty,
1298 struct command_line *control)
1299 {
1300 counted_command_line cmd;
1301 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1302 NULL after the call to read_command_lines if the user provides an empty
1303 list of command by just typing "end". */
1304 bool cmd_read = false;
1305
1306 std::string new_arg;
1307
1308 if (arg == NULL || !*arg)
1309 {
1310 /* Argument not explicitly given. Synthesize it. */
1311 if (breakpoint_count - prev_breakpoint_count > 1)
1312 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1313 breakpoint_count);
1314 else if (breakpoint_count > 0)
1315 new_arg = string_printf ("%d", breakpoint_count);
1316 }
1317 else
1318 {
1319 /* Create a copy of ARG. This is needed because the "commands"
1320 command may be coming from a script. In that case, the read
1321 line buffer is going to be overwritten in the lambda of
1322 'map_breakpoint_numbers' below when reading the next line
1323 before we are are done parsing the breakpoint numbers. */
1324 new_arg = arg;
1325 }
1326 arg = new_arg.c_str ();
1327
1328 map_breakpoint_numbers
1329 (arg, [&] (breakpoint *b)
1330 {
1331 if (!cmd_read)
1332 {
1333 gdb_assert (cmd == NULL);
1334 if (control != NULL)
1335 cmd = control->body_list_0;
1336 else
1337 {
1338 std::string str
1339 = string_printf (_("Type commands for breakpoint(s) "
1340 "%s, one per line."),
1341 arg);
1342
1343 auto do_validate = [=] (const char *line)
1344 {
1345 validate_actionline (line, b);
1346 };
1347 gdb::function_view<void (const char *)> validator;
1348 if (is_tracepoint (b))
1349 validator = do_validate;
1350
1351 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1352 }
1353 cmd_read = true;
1354 }
1355
1356 /* If a breakpoint was on the list more than once, we don't need to
1357 do anything. */
1358 if (b->commands != cmd)
1359 {
1360 validate_commands_for_breakpoint (b, cmd.get ());
1361 b->commands = cmd;
1362 gdb::observers::breakpoint_modified.notify (b);
1363 }
1364 });
1365 }
1366
1367 static void
1368 commands_command (const char *arg, int from_tty)
1369 {
1370 commands_command_1 (arg, from_tty, NULL);
1371 }
1372
1373 /* Like commands_command, but instead of reading the commands from
1374 input stream, takes them from an already parsed command structure.
1375
1376 This is used by cli-script.c to DTRT with breakpoint commands
1377 that are part of if and while bodies. */
1378 enum command_control_type
1379 commands_from_control_command (const char *arg, struct command_line *cmd)
1380 {
1381 commands_command_1 (arg, 0, cmd);
1382 return simple_control;
1383 }
1384
1385 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1386
1387 static int
1388 bp_location_has_shadow (struct bp_location *bl)
1389 {
1390 if (bl->loc_type != bp_loc_software_breakpoint)
1391 return 0;
1392 if (!bl->inserted)
1393 return 0;
1394 if (bl->target_info.shadow_len == 0)
1395 /* BL isn't valid, or doesn't shadow memory. */
1396 return 0;
1397 return 1;
1398 }
1399
1400 /* Update BUF, which is LEN bytes read from the target address
1401 MEMADDR, by replacing a memory breakpoint with its shadowed
1402 contents.
1403
1404 If READBUF is not NULL, this buffer must not overlap with the of
1405 the breakpoint location's shadow_contents buffer. Otherwise, a
1406 failed assertion internal error will be raised. */
1407
1408 static void
1409 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1410 const gdb_byte *writebuf_org,
1411 ULONGEST memaddr, LONGEST len,
1412 struct bp_target_info *target_info,
1413 struct gdbarch *gdbarch)
1414 {
1415 /* Now do full processing of the found relevant range of elements. */
1416 CORE_ADDR bp_addr = 0;
1417 int bp_size = 0;
1418 int bptoffset = 0;
1419
1420 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1421 current_program_space->aspace, 0))
1422 {
1423 /* The breakpoint is inserted in a different address space. */
1424 return;
1425 }
1426
1427 /* Addresses and length of the part of the breakpoint that
1428 we need to copy. */
1429 bp_addr = target_info->placed_address;
1430 bp_size = target_info->shadow_len;
1431
1432 if (bp_addr + bp_size <= memaddr)
1433 {
1434 /* The breakpoint is entirely before the chunk of memory we are
1435 reading. */
1436 return;
1437 }
1438
1439 if (bp_addr >= memaddr + len)
1440 {
1441 /* The breakpoint is entirely after the chunk of memory we are
1442 reading. */
1443 return;
1444 }
1445
1446 /* Offset within shadow_contents. */
1447 if (bp_addr < memaddr)
1448 {
1449 /* Only copy the second part of the breakpoint. */
1450 bp_size -= memaddr - bp_addr;
1451 bptoffset = memaddr - bp_addr;
1452 bp_addr = memaddr;
1453 }
1454
1455 if (bp_addr + bp_size > memaddr + len)
1456 {
1457 /* Only copy the first part of the breakpoint. */
1458 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1459 }
1460
1461 if (readbuf != NULL)
1462 {
1463 /* Verify that the readbuf buffer does not overlap with the
1464 shadow_contents buffer. */
1465 gdb_assert (target_info->shadow_contents >= readbuf + len
1466 || readbuf >= (target_info->shadow_contents
1467 + target_info->shadow_len));
1468
1469 /* Update the read buffer with this inserted breakpoint's
1470 shadow. */
1471 memcpy (readbuf + bp_addr - memaddr,
1472 target_info->shadow_contents + bptoffset, bp_size);
1473 }
1474 else
1475 {
1476 const unsigned char *bp;
1477 CORE_ADDR addr = target_info->reqstd_address;
1478 int placed_size;
1479
1480 /* Update the shadow with what we want to write to memory. */
1481 memcpy (target_info->shadow_contents + bptoffset,
1482 writebuf_org + bp_addr - memaddr, bp_size);
1483
1484 /* Determine appropriate breakpoint contents and size for this
1485 address. */
1486 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1487
1488 /* Update the final write buffer with this inserted
1489 breakpoint's INSN. */
1490 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1491 }
1492 }
1493
1494 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1495 by replacing any memory breakpoints with their shadowed contents.
1496
1497 If READBUF is not NULL, this buffer must not overlap with any of
1498 the breakpoint location's shadow_contents buffers. Otherwise,
1499 a failed assertion internal error will be raised.
1500
1501 The range of shadowed area by each bp_location is:
1502 bl->address - bp_locations_placed_address_before_address_max
1503 up to bl->address + bp_locations_shadow_len_after_address_max
1504 The range we were requested to resolve shadows for is:
1505 memaddr ... memaddr + len
1506 Thus the safe cutoff boundaries for performance optimization are
1507 memaddr + len <= (bl->address
1508 - bp_locations_placed_address_before_address_max)
1509 and:
1510 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1511
1512 void
1513 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1514 const gdb_byte *writebuf_org,
1515 ULONGEST memaddr, LONGEST len)
1516 {
1517 /* Left boundary, right boundary and median element of our binary
1518 search. */
1519 unsigned bc_l, bc_r, bc;
1520
1521 /* Find BC_L which is a leftmost element which may affect BUF
1522 content. It is safe to report lower value but a failure to
1523 report higher one. */
1524
1525 bc_l = 0;
1526 bc_r = bp_locations_count;
1527 while (bc_l + 1 < bc_r)
1528 {
1529 struct bp_location *bl;
1530
1531 bc = (bc_l + bc_r) / 2;
1532 bl = bp_locations[bc];
1533
1534 /* Check first BL->ADDRESS will not overflow due to the added
1535 constant. Then advance the left boundary only if we are sure
1536 the BC element can in no way affect the BUF content (MEMADDR
1537 to MEMADDR + LEN range).
1538
1539 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1540 offset so that we cannot miss a breakpoint with its shadow
1541 range tail still reaching MEMADDR. */
1542
1543 if ((bl->address + bp_locations_shadow_len_after_address_max
1544 >= bl->address)
1545 && (bl->address + bp_locations_shadow_len_after_address_max
1546 <= memaddr))
1547 bc_l = bc;
1548 else
1549 bc_r = bc;
1550 }
1551
1552 /* Due to the binary search above, we need to make sure we pick the
1553 first location that's at BC_L's address. E.g., if there are
1554 multiple locations at the same address, BC_L may end up pointing
1555 at a duplicate location, and miss the "master"/"inserted"
1556 location. Say, given locations L1, L2 and L3 at addresses A and
1557 B:
1558
1559 L1@A, L2@A, L3@B, ...
1560
1561 BC_L could end up pointing at location L2, while the "master"
1562 location could be L1. Since the `loc->inserted' flag is only set
1563 on "master" locations, we'd forget to restore the shadow of L1
1564 and L2. */
1565 while (bc_l > 0
1566 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1567 bc_l--;
1568
1569 /* Now do full processing of the found relevant range of elements. */
1570
1571 for (bc = bc_l; bc < bp_locations_count; bc++)
1572 {
1573 struct bp_location *bl = bp_locations[bc];
1574
1575 /* bp_location array has BL->OWNER always non-NULL. */
1576 if (bl->owner->type == bp_none)
1577 warning (_("reading through apparently deleted breakpoint #%d?"),
1578 bl->owner->number);
1579
1580 /* Performance optimization: any further element can no longer affect BUF
1581 content. */
1582
1583 if (bl->address >= bp_locations_placed_address_before_address_max
1584 && memaddr + len <= (bl->address
1585 - bp_locations_placed_address_before_address_max))
1586 break;
1587
1588 if (!bp_location_has_shadow (bl))
1589 continue;
1590
1591 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1592 memaddr, len, &bl->target_info, bl->gdbarch);
1593 }
1594 }
1595
1596 /* See breakpoint.h. */
1597
1598 bool
1599 is_breakpoint (const struct breakpoint *bpt)
1600 {
1601 return (bpt->type == bp_breakpoint
1602 || bpt->type == bp_hardware_breakpoint
1603 || bpt->type == bp_dprintf);
1604 }
1605
1606 /* Return true if BPT is of any hardware watchpoint kind. */
1607
1608 static bool
1609 is_hardware_watchpoint (const struct breakpoint *bpt)
1610 {
1611 return (bpt->type == bp_hardware_watchpoint
1612 || bpt->type == bp_read_watchpoint
1613 || bpt->type == bp_access_watchpoint);
1614 }
1615
1616 /* See breakpoint.h. */
1617
1618 bool
1619 is_watchpoint (const struct breakpoint *bpt)
1620 {
1621 return (is_hardware_watchpoint (bpt)
1622 || bpt->type == bp_watchpoint);
1623 }
1624
1625 /* Returns true if the current thread and its running state are safe
1626 to evaluate or update watchpoint B. Watchpoints on local
1627 expressions need to be evaluated in the context of the thread that
1628 was current when the watchpoint was created, and, that thread needs
1629 to be stopped to be able to select the correct frame context.
1630 Watchpoints on global expressions can be evaluated on any thread,
1631 and in any state. It is presently left to the target allowing
1632 memory accesses when threads are running. */
1633
1634 static int
1635 watchpoint_in_thread_scope (struct watchpoint *b)
1636 {
1637 return (b->pspace == current_program_space
1638 && (b->watchpoint_thread == null_ptid
1639 || (inferior_ptid == b->watchpoint_thread
1640 && !inferior_thread ()->executing)));
1641 }
1642
1643 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1644 associated bp_watchpoint_scope breakpoint. */
1645
1646 static void
1647 watchpoint_del_at_next_stop (struct watchpoint *w)
1648 {
1649 if (w->related_breakpoint != w)
1650 {
1651 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1652 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1653 w->related_breakpoint->disposition = disp_del_at_next_stop;
1654 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1655 w->related_breakpoint = w;
1656 }
1657 w->disposition = disp_del_at_next_stop;
1658 }
1659
1660 /* Extract a bitfield value from value VAL using the bit parameters contained in
1661 watchpoint W. */
1662
1663 static struct value *
1664 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1665 {
1666 struct value *bit_val;
1667
1668 if (val == NULL)
1669 return NULL;
1670
1671 bit_val = allocate_value (value_type (val));
1672
1673 unpack_value_bitfield (bit_val,
1674 w->val_bitpos,
1675 w->val_bitsize,
1676 value_contents_for_printing (val),
1677 value_offset (val),
1678 val);
1679
1680 return bit_val;
1681 }
1682
1683 /* Allocate a dummy location and add it to B, which must be a software
1684 watchpoint. This is required because even if a software watchpoint
1685 is not watching any memory, bpstat_stop_status requires a location
1686 to be able to report stops. */
1687
1688 static void
1689 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1690 struct program_space *pspace)
1691 {
1692 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1693
1694 b->loc = allocate_bp_location (b);
1695 b->loc->pspace = pspace;
1696 b->loc->address = -1;
1697 b->loc->length = -1;
1698 }
1699
1700 /* Returns true if B is a software watchpoint that is not watching any
1701 memory (e.g., "watch $pc"). */
1702
1703 static bool
1704 is_no_memory_software_watchpoint (struct breakpoint *b)
1705 {
1706 return (b->type == bp_watchpoint
1707 && b->loc != NULL
1708 && b->loc->next == NULL
1709 && b->loc->address == -1
1710 && b->loc->length == -1);
1711 }
1712
1713 /* Assuming that B is a watchpoint:
1714 - Reparse watchpoint expression, if REPARSE is non-zero
1715 - Evaluate expression and store the result in B->val
1716 - Evaluate the condition if there is one, and store the result
1717 in b->loc->cond.
1718 - Update the list of values that must be watched in B->loc.
1719
1720 If the watchpoint disposition is disp_del_at_next_stop, then do
1721 nothing. If this is local watchpoint that is out of scope, delete
1722 it.
1723
1724 Even with `set breakpoint always-inserted on' the watchpoints are
1725 removed + inserted on each stop here. Normal breakpoints must
1726 never be removed because they might be missed by a running thread
1727 when debugging in non-stop mode. On the other hand, hardware
1728 watchpoints (is_hardware_watchpoint; processed here) are specific
1729 to each LWP since they are stored in each LWP's hardware debug
1730 registers. Therefore, such LWP must be stopped first in order to
1731 be able to modify its hardware watchpoints.
1732
1733 Hardware watchpoints must be reset exactly once after being
1734 presented to the user. It cannot be done sooner, because it would
1735 reset the data used to present the watchpoint hit to the user. And
1736 it must not be done later because it could display the same single
1737 watchpoint hit during multiple GDB stops. Note that the latter is
1738 relevant only to the hardware watchpoint types bp_read_watchpoint
1739 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1740 not user-visible - its hit is suppressed if the memory content has
1741 not changed.
1742
1743 The following constraints influence the location where we can reset
1744 hardware watchpoints:
1745
1746 * target_stopped_by_watchpoint and target_stopped_data_address are
1747 called several times when GDB stops.
1748
1749 [linux]
1750 * Multiple hardware watchpoints can be hit at the same time,
1751 causing GDB to stop. GDB only presents one hardware watchpoint
1752 hit at a time as the reason for stopping, and all the other hits
1753 are presented later, one after the other, each time the user
1754 requests the execution to be resumed. Execution is not resumed
1755 for the threads still having pending hit event stored in
1756 LWP_INFO->STATUS. While the watchpoint is already removed from
1757 the inferior on the first stop the thread hit event is kept being
1758 reported from its cached value by linux_nat_stopped_data_address
1759 until the real thread resume happens after the watchpoint gets
1760 presented and thus its LWP_INFO->STATUS gets reset.
1761
1762 Therefore the hardware watchpoint hit can get safely reset on the
1763 watchpoint removal from inferior. */
1764
1765 static void
1766 update_watchpoint (struct watchpoint *b, int reparse)
1767 {
1768 int within_current_scope;
1769 struct frame_id saved_frame_id;
1770 int frame_saved;
1771
1772 /* If this is a local watchpoint, we only want to check if the
1773 watchpoint frame is in scope if the current thread is the thread
1774 that was used to create the watchpoint. */
1775 if (!watchpoint_in_thread_scope (b))
1776 return;
1777
1778 if (b->disposition == disp_del_at_next_stop)
1779 return;
1780
1781 frame_saved = 0;
1782
1783 /* Determine if the watchpoint is within scope. */
1784 if (b->exp_valid_block == NULL)
1785 within_current_scope = 1;
1786 else
1787 {
1788 struct frame_info *fi = get_current_frame ();
1789 struct gdbarch *frame_arch = get_frame_arch (fi);
1790 CORE_ADDR frame_pc = get_frame_pc (fi);
1791
1792 /* If we're at a point where the stack has been destroyed
1793 (e.g. in a function epilogue), unwinding may not work
1794 properly. Do not attempt to recreate locations at this
1795 point. See similar comments in watchpoint_check. */
1796 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1797 return;
1798
1799 /* Save the current frame's ID so we can restore it after
1800 evaluating the watchpoint expression on its own frame. */
1801 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1802 took a frame parameter, so that we didn't have to change the
1803 selected frame. */
1804 frame_saved = 1;
1805 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1806
1807 fi = frame_find_by_id (b->watchpoint_frame);
1808 within_current_scope = (fi != NULL);
1809 if (within_current_scope)
1810 select_frame (fi);
1811 }
1812
1813 /* We don't free locations. They are stored in the bp_location array
1814 and update_global_location_list will eventually delete them and
1815 remove breakpoints if needed. */
1816 b->loc = NULL;
1817
1818 if (within_current_scope && reparse)
1819 {
1820 const char *s;
1821
1822 b->exp.reset ();
1823 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1824 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1825 /* If the meaning of expression itself changed, the old value is
1826 no longer relevant. We don't want to report a watchpoint hit
1827 to the user when the old value and the new value may actually
1828 be completely different objects. */
1829 b->val = NULL;
1830 b->val_valid = false;
1831
1832 /* Note that unlike with breakpoints, the watchpoint's condition
1833 expression is stored in the breakpoint object, not in the
1834 locations (re)created below. */
1835 if (b->cond_string != NULL)
1836 {
1837 b->cond_exp.reset ();
1838
1839 s = b->cond_string;
1840 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1841 }
1842 }
1843
1844 /* If we failed to parse the expression, for example because
1845 it refers to a global variable in a not-yet-loaded shared library,
1846 don't try to insert watchpoint. We don't automatically delete
1847 such watchpoint, though, since failure to parse expression
1848 is different from out-of-scope watchpoint. */
1849 if (!target_has_execution ())
1850 {
1851 /* Without execution, memory can't change. No use to try and
1852 set watchpoint locations. The watchpoint will be reset when
1853 the target gains execution, through breakpoint_re_set. */
1854 if (!can_use_hw_watchpoints)
1855 {
1856 if (b->ops->works_in_software_mode (b))
1857 b->type = bp_watchpoint;
1858 else
1859 error (_("Can't set read/access watchpoint when "
1860 "hardware watchpoints are disabled."));
1861 }
1862 }
1863 else if (within_current_scope && b->exp)
1864 {
1865 int pc = 0;
1866 std::vector<value_ref_ptr> val_chain;
1867 struct value *v, *result;
1868 struct program_space *frame_pspace;
1869
1870 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
1871
1872 /* Avoid setting b->val if it's already set. The meaning of
1873 b->val is 'the last value' user saw, and we should update
1874 it only if we reported that last value to user. As it
1875 happens, the code that reports it updates b->val directly.
1876 We don't keep track of the memory value for masked
1877 watchpoints. */
1878 if (!b->val_valid && !is_masked_watchpoint (b))
1879 {
1880 if (b->val_bitsize != 0)
1881 v = extract_bitfield_from_watchpoint_value (b, v);
1882 b->val = release_value (v);
1883 b->val_valid = true;
1884 }
1885
1886 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1887
1888 /* Look at each value on the value chain. */
1889 gdb_assert (!val_chain.empty ());
1890 for (const value_ref_ptr &iter : val_chain)
1891 {
1892 v = iter.get ();
1893
1894 /* If it's a memory location, and GDB actually needed
1895 its contents to evaluate the expression, then we
1896 must watch it. If the first value returned is
1897 still lazy, that means an error occurred reading it;
1898 watch it anyway in case it becomes readable. */
1899 if (VALUE_LVAL (v) == lval_memory
1900 && (v == val_chain[0] || ! value_lazy (v)))
1901 {
1902 struct type *vtype = check_typedef (value_type (v));
1903
1904 /* We only watch structs and arrays if user asked
1905 for it explicitly, never if they just happen to
1906 appear in the middle of some value chain. */
1907 if (v == result
1908 || (vtype->code () != TYPE_CODE_STRUCT
1909 && vtype->code () != TYPE_CODE_ARRAY))
1910 {
1911 CORE_ADDR addr;
1912 enum target_hw_bp_type type;
1913 struct bp_location *loc, **tmp;
1914 int bitpos = 0, bitsize = 0;
1915
1916 if (value_bitsize (v) != 0)
1917 {
1918 /* Extract the bit parameters out from the bitfield
1919 sub-expression. */
1920 bitpos = value_bitpos (v);
1921 bitsize = value_bitsize (v);
1922 }
1923 else if (v == result && b->val_bitsize != 0)
1924 {
1925 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1926 lvalue whose bit parameters are saved in the fields
1927 VAL_BITPOS and VAL_BITSIZE. */
1928 bitpos = b->val_bitpos;
1929 bitsize = b->val_bitsize;
1930 }
1931
1932 addr = value_address (v);
1933 if (bitsize != 0)
1934 {
1935 /* Skip the bytes that don't contain the bitfield. */
1936 addr += bitpos / 8;
1937 }
1938
1939 type = hw_write;
1940 if (b->type == bp_read_watchpoint)
1941 type = hw_read;
1942 else if (b->type == bp_access_watchpoint)
1943 type = hw_access;
1944
1945 loc = allocate_bp_location (b);
1946 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1947 ;
1948 *tmp = loc;
1949 loc->gdbarch = get_type_arch (value_type (v));
1950
1951 loc->pspace = frame_pspace;
1952 loc->address = address_significant (loc->gdbarch, addr);
1953
1954 if (bitsize != 0)
1955 {
1956 /* Just cover the bytes that make up the bitfield. */
1957 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1958 }
1959 else
1960 loc->length = TYPE_LENGTH (value_type (v));
1961
1962 loc->watchpoint_type = type;
1963 }
1964 }
1965 }
1966
1967 /* Change the type of breakpoint between hardware assisted or
1968 an ordinary watchpoint depending on the hardware support
1969 and free hardware slots. REPARSE is set when the inferior
1970 is started. */
1971 if (reparse)
1972 {
1973 int reg_cnt;
1974 enum bp_loc_type loc_type;
1975 struct bp_location *bl;
1976
1977 reg_cnt = can_use_hardware_watchpoint (val_chain);
1978
1979 if (reg_cnt)
1980 {
1981 int i, target_resources_ok, other_type_used;
1982 enum bptype type;
1983
1984 /* Use an exact watchpoint when there's only one memory region to be
1985 watched, and only one debug register is needed to watch it. */
1986 b->exact = target_exact_watchpoints && reg_cnt == 1;
1987
1988 /* We need to determine how many resources are already
1989 used for all other hardware watchpoints plus this one
1990 to see if we still have enough resources to also fit
1991 this watchpoint in as well. */
1992
1993 /* If this is a software watchpoint, we try to turn it
1994 to a hardware one -- count resources as if B was of
1995 hardware watchpoint type. */
1996 type = b->type;
1997 if (type == bp_watchpoint)
1998 type = bp_hardware_watchpoint;
1999
2000 /* This watchpoint may or may not have been placed on
2001 the list yet at this point (it won't be in the list
2002 if we're trying to create it for the first time,
2003 through watch_command), so always account for it
2004 manually. */
2005
2006 /* Count resources used by all watchpoints except B. */
2007 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
2008
2009 /* Add in the resources needed for B. */
2010 i += hw_watchpoint_use_count (b);
2011
2012 target_resources_ok
2013 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2014 if (target_resources_ok <= 0)
2015 {
2016 int sw_mode = b->ops->works_in_software_mode (b);
2017
2018 if (target_resources_ok == 0 && !sw_mode)
2019 error (_("Target does not support this type of "
2020 "hardware watchpoint."));
2021 else if (target_resources_ok < 0 && !sw_mode)
2022 error (_("There are not enough available hardware "
2023 "resources for this watchpoint."));
2024
2025 /* Downgrade to software watchpoint. */
2026 b->type = bp_watchpoint;
2027 }
2028 else
2029 {
2030 /* If this was a software watchpoint, we've just
2031 found we have enough resources to turn it to a
2032 hardware watchpoint. Otherwise, this is a
2033 nop. */
2034 b->type = type;
2035 }
2036 }
2037 else if (!b->ops->works_in_software_mode (b))
2038 {
2039 if (!can_use_hw_watchpoints)
2040 error (_("Can't set read/access watchpoint when "
2041 "hardware watchpoints are disabled."));
2042 else
2043 error (_("Expression cannot be implemented with "
2044 "read/access watchpoint."));
2045 }
2046 else
2047 b->type = bp_watchpoint;
2048
2049 loc_type = (b->type == bp_watchpoint? bp_loc_other
2050 : bp_loc_hardware_watchpoint);
2051 for (bl = b->loc; bl; bl = bl->next)
2052 bl->loc_type = loc_type;
2053 }
2054
2055 /* If a software watchpoint is not watching any memory, then the
2056 above left it without any location set up. But,
2057 bpstat_stop_status requires a location to be able to report
2058 stops, so make sure there's at least a dummy one. */
2059 if (b->type == bp_watchpoint && b->loc == NULL)
2060 software_watchpoint_add_no_memory_location (b, frame_pspace);
2061 }
2062 else if (!within_current_scope)
2063 {
2064 printf_filtered (_("\
2065 Watchpoint %d deleted because the program has left the block\n\
2066 in which its expression is valid.\n"),
2067 b->number);
2068 watchpoint_del_at_next_stop (b);
2069 }
2070
2071 /* Restore the selected frame. */
2072 if (frame_saved)
2073 select_frame (frame_find_by_id (saved_frame_id));
2074 }
2075
2076
2077 /* Returns 1 iff breakpoint location should be
2078 inserted in the inferior. We don't differentiate the type of BL's owner
2079 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2080 breakpoint_ops is not defined, because in insert_bp_location,
2081 tracepoint's insert_location will not be called. */
2082 static int
2083 should_be_inserted (struct bp_location *bl)
2084 {
2085 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2086 return 0;
2087
2088 if (bl->owner->disposition == disp_del_at_next_stop)
2089 return 0;
2090
2091 if (!bl->enabled || bl->disabled_by_cond
2092 || bl->shlib_disabled || bl->duplicate)
2093 return 0;
2094
2095 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2096 return 0;
2097
2098 /* This is set for example, when we're attached to the parent of a
2099 vfork, and have detached from the child. The child is running
2100 free, and we expect it to do an exec or exit, at which point the
2101 OS makes the parent schedulable again (and the target reports
2102 that the vfork is done). Until the child is done with the shared
2103 memory region, do not insert breakpoints in the parent, otherwise
2104 the child could still trip on the parent's breakpoints. Since
2105 the parent is blocked anyway, it won't miss any breakpoint. */
2106 if (bl->pspace->breakpoints_not_allowed)
2107 return 0;
2108
2109 /* Don't insert a breakpoint if we're trying to step past its
2110 location, except if the breakpoint is a single-step breakpoint,
2111 and the breakpoint's thread is the thread which is stepping past
2112 a breakpoint. */
2113 if ((bl->loc_type == bp_loc_software_breakpoint
2114 || bl->loc_type == bp_loc_hardware_breakpoint)
2115 && stepping_past_instruction_at (bl->pspace->aspace,
2116 bl->address)
2117 /* The single-step breakpoint may be inserted at the location
2118 we're trying to step if the instruction branches to itself.
2119 However, the instruction won't be executed at all and it may
2120 break the semantics of the instruction, for example, the
2121 instruction is a conditional branch or updates some flags.
2122 We can't fix it unless GDB is able to emulate the instruction
2123 or switch to displaced stepping. */
2124 && !(bl->owner->type == bp_single_step
2125 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2126 {
2127 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2128 paddress (bl->gdbarch, bl->address));
2129 return 0;
2130 }
2131
2132 /* Don't insert watchpoints if we're trying to step past the
2133 instruction that triggered one. */
2134 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2135 && stepping_past_nonsteppable_watchpoint ())
2136 {
2137 infrun_debug_printf ("stepping past non-steppable watchpoint. "
2138 "skipping watchpoint at %s:%d\n",
2139 paddress (bl->gdbarch, bl->address), bl->length);
2140 return 0;
2141 }
2142
2143 return 1;
2144 }
2145
2146 /* Same as should_be_inserted but does the check assuming
2147 that the location is not duplicated. */
2148
2149 static int
2150 unduplicated_should_be_inserted (struct bp_location *bl)
2151 {
2152 int result;
2153 const int save_duplicate = bl->duplicate;
2154
2155 bl->duplicate = 0;
2156 result = should_be_inserted (bl);
2157 bl->duplicate = save_duplicate;
2158 return result;
2159 }
2160
2161 /* Parses a conditional described by an expression COND into an
2162 agent expression bytecode suitable for evaluation
2163 by the bytecode interpreter. Return NULL if there was
2164 any error during parsing. */
2165
2166 static agent_expr_up
2167 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2168 {
2169 if (cond == NULL)
2170 return NULL;
2171
2172 agent_expr_up aexpr;
2173
2174 /* We don't want to stop processing, so catch any errors
2175 that may show up. */
2176 try
2177 {
2178 aexpr = gen_eval_for_expr (scope, cond);
2179 }
2180
2181 catch (const gdb_exception_error &ex)
2182 {
2183 /* If we got here, it means the condition could not be parsed to a valid
2184 bytecode expression and thus can't be evaluated on the target's side.
2185 It's no use iterating through the conditions. */
2186 }
2187
2188 /* We have a valid agent expression. */
2189 return aexpr;
2190 }
2191
2192 /* Based on location BL, create a list of breakpoint conditions to be
2193 passed on to the target. If we have duplicated locations with different
2194 conditions, we will add such conditions to the list. The idea is that the
2195 target will evaluate the list of conditions and will only notify GDB when
2196 one of them is true. */
2197
2198 static void
2199 build_target_condition_list (struct bp_location *bl)
2200 {
2201 struct bp_location **locp = NULL, **loc2p;
2202 int null_condition_or_parse_error = 0;
2203 int modified = bl->needs_update;
2204 struct bp_location *loc;
2205
2206 /* Release conditions left over from a previous insert. */
2207 bl->target_info.conditions.clear ();
2208
2209 /* This is only meaningful if the target is
2210 evaluating conditions and if the user has
2211 opted for condition evaluation on the target's
2212 side. */
2213 if (gdb_evaluates_breakpoint_condition_p ()
2214 || !target_supports_evaluation_of_breakpoint_conditions ())
2215 return;
2216
2217 /* Do a first pass to check for locations with no assigned
2218 conditions or conditions that fail to parse to a valid agent
2219 expression bytecode. If any of these happen, then it's no use to
2220 send conditions to the target since this location will always
2221 trigger and generate a response back to GDB. Note we consider
2222 all locations at the same address irrespective of type, i.e.,
2223 even if the locations aren't considered duplicates (e.g.,
2224 software breakpoint and hardware breakpoint at the same
2225 address). */
2226 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2227 {
2228 loc = (*loc2p);
2229 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2230 {
2231 if (modified)
2232 {
2233 /* Re-parse the conditions since something changed. In that
2234 case we already freed the condition bytecodes (see
2235 force_breakpoint_reinsertion). We just
2236 need to parse the condition to bytecodes again. */
2237 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2238 loc->cond.get ());
2239 }
2240
2241 /* If we have a NULL bytecode expression, it means something
2242 went wrong or we have a null condition expression. */
2243 if (!loc->cond_bytecode)
2244 {
2245 null_condition_or_parse_error = 1;
2246 break;
2247 }
2248 }
2249 }
2250
2251 /* If any of these happened, it means we will have to evaluate the conditions
2252 for the location's address on gdb's side. It is no use keeping bytecodes
2253 for all the other duplicate locations, thus we free all of them here.
2254
2255 This is so we have a finer control over which locations' conditions are
2256 being evaluated by GDB or the remote stub. */
2257 if (null_condition_or_parse_error)
2258 {
2259 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2260 {
2261 loc = (*loc2p);
2262 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2263 {
2264 /* Only go as far as the first NULL bytecode is
2265 located. */
2266 if (!loc->cond_bytecode)
2267 return;
2268
2269 loc->cond_bytecode.reset ();
2270 }
2271 }
2272 }
2273
2274 /* No NULL conditions or failed bytecode generation. Build a
2275 condition list for this location's address. If we have software
2276 and hardware locations at the same address, they aren't
2277 considered duplicates, but we still marge all the conditions
2278 anyway, as it's simpler, and doesn't really make a practical
2279 difference. */
2280 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2281 {
2282 loc = (*loc2p);
2283 if (loc->cond
2284 && is_breakpoint (loc->owner)
2285 && loc->pspace->num == bl->pspace->num
2286 && loc->owner->enable_state == bp_enabled
2287 && loc->enabled
2288 && !loc->disabled_by_cond)
2289 {
2290 /* Add the condition to the vector. This will be used later
2291 to send the conditions to the target. */
2292 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2293 }
2294 }
2295
2296 return;
2297 }
2298
2299 /* Parses a command described by string CMD into an agent expression
2300 bytecode suitable for evaluation by the bytecode interpreter.
2301 Return NULL if there was any error during parsing. */
2302
2303 static agent_expr_up
2304 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2305 {
2306 const char *cmdrest;
2307 const char *format_start, *format_end;
2308 struct gdbarch *gdbarch = get_current_arch ();
2309
2310 if (cmd == NULL)
2311 return NULL;
2312
2313 cmdrest = cmd;
2314
2315 if (*cmdrest == ',')
2316 ++cmdrest;
2317 cmdrest = skip_spaces (cmdrest);
2318
2319 if (*cmdrest++ != '"')
2320 error (_("No format string following the location"));
2321
2322 format_start = cmdrest;
2323
2324 format_pieces fpieces (&cmdrest);
2325
2326 format_end = cmdrest;
2327
2328 if (*cmdrest++ != '"')
2329 error (_("Bad format string, non-terminated '\"'."));
2330
2331 cmdrest = skip_spaces (cmdrest);
2332
2333 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2334 error (_("Invalid argument syntax"));
2335
2336 if (*cmdrest == ',')
2337 cmdrest++;
2338 cmdrest = skip_spaces (cmdrest);
2339
2340 /* For each argument, make an expression. */
2341
2342 std::vector<struct expression *> argvec;
2343 while (*cmdrest != '\0')
2344 {
2345 const char *cmd1;
2346
2347 cmd1 = cmdrest;
2348 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2349 argvec.push_back (expr.release ());
2350 cmdrest = cmd1;
2351 if (*cmdrest == ',')
2352 ++cmdrest;
2353 }
2354
2355 agent_expr_up aexpr;
2356
2357 /* We don't want to stop processing, so catch any errors
2358 that may show up. */
2359 try
2360 {
2361 aexpr = gen_printf (scope, gdbarch, 0, 0,
2362 format_start, format_end - format_start,
2363 argvec.size (), argvec.data ());
2364 }
2365 catch (const gdb_exception_error &ex)
2366 {
2367 /* If we got here, it means the command could not be parsed to a valid
2368 bytecode expression and thus can't be evaluated on the target's side.
2369 It's no use iterating through the other commands. */
2370 }
2371
2372 /* We have a valid agent expression, return it. */
2373 return aexpr;
2374 }
2375
2376 /* Based on location BL, create a list of breakpoint commands to be
2377 passed on to the target. If we have duplicated locations with
2378 different commands, we will add any such to the list. */
2379
2380 static void
2381 build_target_command_list (struct bp_location *bl)
2382 {
2383 struct bp_location **locp = NULL, **loc2p;
2384 int null_command_or_parse_error = 0;
2385 int modified = bl->needs_update;
2386 struct bp_location *loc;
2387
2388 /* Clear commands left over from a previous insert. */
2389 bl->target_info.tcommands.clear ();
2390
2391 if (!target_can_run_breakpoint_commands ())
2392 return;
2393
2394 /* For now, limit to agent-style dprintf breakpoints. */
2395 if (dprintf_style != dprintf_style_agent)
2396 return;
2397
2398 /* For now, if we have any location at the same address that isn't a
2399 dprintf, don't install the target-side commands, as that would
2400 make the breakpoint not be reported to the core, and we'd lose
2401 control. */
2402 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2403 {
2404 loc = (*loc2p);
2405 if (is_breakpoint (loc->owner)
2406 && loc->pspace->num == bl->pspace->num
2407 && loc->owner->type != bp_dprintf)
2408 return;
2409 }
2410
2411 /* Do a first pass to check for locations with no assigned
2412 conditions or conditions that fail to parse to a valid agent expression
2413 bytecode. If any of these happen, then it's no use to send conditions
2414 to the target since this location will always trigger and generate a
2415 response back to GDB. */
2416 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2417 {
2418 loc = (*loc2p);
2419 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2420 {
2421 if (modified)
2422 {
2423 /* Re-parse the commands since something changed. In that
2424 case we already freed the command bytecodes (see
2425 force_breakpoint_reinsertion). We just
2426 need to parse the command to bytecodes again. */
2427 loc->cmd_bytecode
2428 = parse_cmd_to_aexpr (bl->address,
2429 loc->owner->extra_string);
2430 }
2431
2432 /* If we have a NULL bytecode expression, it means something
2433 went wrong or we have a null command expression. */
2434 if (!loc->cmd_bytecode)
2435 {
2436 null_command_or_parse_error = 1;
2437 break;
2438 }
2439 }
2440 }
2441
2442 /* If anything failed, then we're not doing target-side commands,
2443 and so clean up. */
2444 if (null_command_or_parse_error)
2445 {
2446 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2447 {
2448 loc = (*loc2p);
2449 if (is_breakpoint (loc->owner)
2450 && loc->pspace->num == bl->pspace->num)
2451 {
2452 /* Only go as far as the first NULL bytecode is
2453 located. */
2454 if (loc->cmd_bytecode == NULL)
2455 return;
2456
2457 loc->cmd_bytecode.reset ();
2458 }
2459 }
2460 }
2461
2462 /* No NULL commands or failed bytecode generation. Build a command
2463 list for all duplicate locations at this location's address.
2464 Note that here we must care for whether the breakpoint location
2465 types are considered duplicates, otherwise, say, if we have a
2466 software and hardware location at the same address, the target
2467 could end up running the commands twice. For the moment, we only
2468 support targets-side commands with dprintf, but it doesn't hurt
2469 to be pedantically correct in case that changes. */
2470 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2471 {
2472 loc = (*loc2p);
2473 if (breakpoint_locations_match (bl, loc)
2474 && loc->owner->extra_string
2475 && is_breakpoint (loc->owner)
2476 && loc->pspace->num == bl->pspace->num
2477 && loc->owner->enable_state == bp_enabled
2478 && loc->enabled
2479 && !loc->disabled_by_cond)
2480 {
2481 /* Add the command to the vector. This will be used later
2482 to send the commands to the target. */
2483 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2484 }
2485 }
2486
2487 bl->target_info.persist = 0;
2488 /* Maybe flag this location as persistent. */
2489 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2490 bl->target_info.persist = 1;
2491 }
2492
2493 /* Return the kind of breakpoint on address *ADDR. Get the kind
2494 of breakpoint according to ADDR except single-step breakpoint.
2495 Get the kind of single-step breakpoint according to the current
2496 registers state. */
2497
2498 static int
2499 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2500 {
2501 if (bl->owner->type == bp_single_step)
2502 {
2503 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2504 struct regcache *regcache;
2505
2506 regcache = get_thread_regcache (thr);
2507
2508 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2509 regcache, addr);
2510 }
2511 else
2512 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2513 }
2514
2515 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2516 location. Any error messages are printed to TMP_ERROR_STREAM; and
2517 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2518 Returns 0 for success, 1 if the bp_location type is not supported or
2519 -1 for failure.
2520
2521 NOTE drow/2003-09-09: This routine could be broken down to an
2522 object-style method for each breakpoint or catchpoint type. */
2523 static int
2524 insert_bp_location (struct bp_location *bl,
2525 struct ui_file *tmp_error_stream,
2526 int *disabled_breaks,
2527 int *hw_breakpoint_error,
2528 int *hw_bp_error_explained_already)
2529 {
2530 gdb_exception bp_excpt;
2531
2532 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2533 return 0;
2534
2535 /* Note we don't initialize bl->target_info, as that wipes out
2536 the breakpoint location's shadow_contents if the breakpoint
2537 is still inserted at that location. This in turn breaks
2538 target_read_memory which depends on these buffers when
2539 a memory read is requested at the breakpoint location:
2540 Once the target_info has been wiped, we fail to see that
2541 we have a breakpoint inserted at that address and thus
2542 read the breakpoint instead of returning the data saved in
2543 the breakpoint location's shadow contents. */
2544 bl->target_info.reqstd_address = bl->address;
2545 bl->target_info.placed_address_space = bl->pspace->aspace;
2546 bl->target_info.length = bl->length;
2547
2548 /* When working with target-side conditions, we must pass all the conditions
2549 for the same breakpoint address down to the target since GDB will not
2550 insert those locations. With a list of breakpoint conditions, the target
2551 can decide when to stop and notify GDB. */
2552
2553 if (is_breakpoint (bl->owner))
2554 {
2555 build_target_condition_list (bl);
2556 build_target_command_list (bl);
2557 /* Reset the modification marker. */
2558 bl->needs_update = 0;
2559 }
2560
2561 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2562 set at a read-only address, then a breakpoint location will have
2563 been changed to hardware breakpoint before we get here. If it is
2564 "off" however, error out before actually trying to insert the
2565 breakpoint, with a nicer error message. */
2566 if (bl->loc_type == bp_loc_software_breakpoint
2567 && !automatic_hardware_breakpoints)
2568 {
2569 mem_region *mr = lookup_mem_region (bl->address);
2570
2571 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2572 {
2573 fprintf_unfiltered (tmp_error_stream,
2574 _("Cannot insert breakpoint %d.\n"
2575 "Cannot set software breakpoint "
2576 "at read-only address %s\n"),
2577 bl->owner->number,
2578 paddress (bl->gdbarch, bl->address));
2579 return 1;
2580 }
2581 }
2582
2583 if (bl->loc_type == bp_loc_software_breakpoint
2584 || bl->loc_type == bp_loc_hardware_breakpoint)
2585 {
2586 /* First check to see if we have to handle an overlay. */
2587 if (overlay_debugging == ovly_off
2588 || bl->section == NULL
2589 || !(section_is_overlay (bl->section)))
2590 {
2591 /* No overlay handling: just set the breakpoint. */
2592 try
2593 {
2594 int val;
2595
2596 val = bl->owner->ops->insert_location (bl);
2597 if (val)
2598 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2599 }
2600 catch (gdb_exception &e)
2601 {
2602 bp_excpt = std::move (e);
2603 }
2604 }
2605 else
2606 {
2607 /* This breakpoint is in an overlay section.
2608 Shall we set a breakpoint at the LMA? */
2609 if (!overlay_events_enabled)
2610 {
2611 /* Yes -- overlay event support is not active,
2612 so we must try to set a breakpoint at the LMA.
2613 This will not work for a hardware breakpoint. */
2614 if (bl->loc_type == bp_loc_hardware_breakpoint)
2615 warning (_("hardware breakpoint %d not supported in overlay!"),
2616 bl->owner->number);
2617 else
2618 {
2619 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2620 bl->section);
2621 /* Set a software (trap) breakpoint at the LMA. */
2622 bl->overlay_target_info = bl->target_info;
2623 bl->overlay_target_info.reqstd_address = addr;
2624
2625 /* No overlay handling: just set the breakpoint. */
2626 try
2627 {
2628 int val;
2629
2630 bl->overlay_target_info.kind
2631 = breakpoint_kind (bl, &addr);
2632 bl->overlay_target_info.placed_address = addr;
2633 val = target_insert_breakpoint (bl->gdbarch,
2634 &bl->overlay_target_info);
2635 if (val)
2636 bp_excpt
2637 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2638 }
2639 catch (gdb_exception &e)
2640 {
2641 bp_excpt = std::move (e);
2642 }
2643
2644 if (bp_excpt.reason != 0)
2645 fprintf_unfiltered (tmp_error_stream,
2646 "Overlay breakpoint %d "
2647 "failed: in ROM?\n",
2648 bl->owner->number);
2649 }
2650 }
2651 /* Shall we set a breakpoint at the VMA? */
2652 if (section_is_mapped (bl->section))
2653 {
2654 /* Yes. This overlay section is mapped into memory. */
2655 try
2656 {
2657 int val;
2658
2659 val = bl->owner->ops->insert_location (bl);
2660 if (val)
2661 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2662 }
2663 catch (gdb_exception &e)
2664 {
2665 bp_excpt = std::move (e);
2666 }
2667 }
2668 else
2669 {
2670 /* No. This breakpoint will not be inserted.
2671 No error, but do not mark the bp as 'inserted'. */
2672 return 0;
2673 }
2674 }
2675
2676 if (bp_excpt.reason != 0)
2677 {
2678 /* Can't set the breakpoint. */
2679
2680 /* In some cases, we might not be able to insert a
2681 breakpoint in a shared library that has already been
2682 removed, but we have not yet processed the shlib unload
2683 event. Unfortunately, some targets that implement
2684 breakpoint insertion themselves can't tell why the
2685 breakpoint insertion failed (e.g., the remote target
2686 doesn't define error codes), so we must treat generic
2687 errors as memory errors. */
2688 if (bp_excpt.reason == RETURN_ERROR
2689 && (bp_excpt.error == GENERIC_ERROR
2690 || bp_excpt.error == MEMORY_ERROR)
2691 && bl->loc_type == bp_loc_software_breakpoint
2692 && (solib_name_from_address (bl->pspace, bl->address)
2693 || shared_objfile_contains_address_p (bl->pspace,
2694 bl->address)))
2695 {
2696 /* See also: disable_breakpoints_in_shlibs. */
2697 bl->shlib_disabled = 1;
2698 gdb::observers::breakpoint_modified.notify (bl->owner);
2699 if (!*disabled_breaks)
2700 {
2701 fprintf_unfiltered (tmp_error_stream,
2702 "Cannot insert breakpoint %d.\n",
2703 bl->owner->number);
2704 fprintf_unfiltered (tmp_error_stream,
2705 "Temporarily disabling shared "
2706 "library breakpoints:\n");
2707 }
2708 *disabled_breaks = 1;
2709 fprintf_unfiltered (tmp_error_stream,
2710 "breakpoint #%d\n", bl->owner->number);
2711 return 0;
2712 }
2713 else
2714 {
2715 if (bl->loc_type == bp_loc_hardware_breakpoint)
2716 {
2717 *hw_breakpoint_error = 1;
2718 *hw_bp_error_explained_already = bp_excpt.message != NULL;
2719 fprintf_unfiltered (tmp_error_stream,
2720 "Cannot insert hardware breakpoint %d%s",
2721 bl->owner->number,
2722 bp_excpt.message ? ":" : ".\n");
2723 if (bp_excpt.message != NULL)
2724 fprintf_unfiltered (tmp_error_stream, "%s.\n",
2725 bp_excpt.what ());
2726 }
2727 else
2728 {
2729 if (bp_excpt.message == NULL)
2730 {
2731 std::string message
2732 = memory_error_message (TARGET_XFER_E_IO,
2733 bl->gdbarch, bl->address);
2734
2735 fprintf_unfiltered (tmp_error_stream,
2736 "Cannot insert breakpoint %d.\n"
2737 "%s\n",
2738 bl->owner->number, message.c_str ());
2739 }
2740 else
2741 {
2742 fprintf_unfiltered (tmp_error_stream,
2743 "Cannot insert breakpoint %d: %s\n",
2744 bl->owner->number,
2745 bp_excpt.what ());
2746 }
2747 }
2748 return 1;
2749
2750 }
2751 }
2752 else
2753 bl->inserted = 1;
2754
2755 return 0;
2756 }
2757
2758 else if (bl->loc_type == bp_loc_hardware_watchpoint
2759 /* NOTE drow/2003-09-08: This state only exists for removing
2760 watchpoints. It's not clear that it's necessary... */
2761 && bl->owner->disposition != disp_del_at_next_stop)
2762 {
2763 int val;
2764
2765 gdb_assert (bl->owner->ops != NULL
2766 && bl->owner->ops->insert_location != NULL);
2767
2768 val = bl->owner->ops->insert_location (bl);
2769
2770 /* If trying to set a read-watchpoint, and it turns out it's not
2771 supported, try emulating one with an access watchpoint. */
2772 if (val == 1 && bl->watchpoint_type == hw_read)
2773 {
2774 struct bp_location *loc, **loc_temp;
2775
2776 /* But don't try to insert it, if there's already another
2777 hw_access location that would be considered a duplicate
2778 of this one. */
2779 ALL_BP_LOCATIONS (loc, loc_temp)
2780 if (loc != bl
2781 && loc->watchpoint_type == hw_access
2782 && watchpoint_locations_match (bl, loc))
2783 {
2784 bl->duplicate = 1;
2785 bl->inserted = 1;
2786 bl->target_info = loc->target_info;
2787 bl->watchpoint_type = hw_access;
2788 val = 0;
2789 break;
2790 }
2791
2792 if (val == 1)
2793 {
2794 bl->watchpoint_type = hw_access;
2795 val = bl->owner->ops->insert_location (bl);
2796
2797 if (val)
2798 /* Back to the original value. */
2799 bl->watchpoint_type = hw_read;
2800 }
2801 }
2802
2803 bl->inserted = (val == 0);
2804 }
2805
2806 else if (bl->owner->type == bp_catchpoint)
2807 {
2808 int val;
2809
2810 gdb_assert (bl->owner->ops != NULL
2811 && bl->owner->ops->insert_location != NULL);
2812
2813 val = bl->owner->ops->insert_location (bl);
2814 if (val)
2815 {
2816 bl->owner->enable_state = bp_disabled;
2817
2818 if (val == 1)
2819 warning (_("\
2820 Error inserting catchpoint %d: Your system does not support this type\n\
2821 of catchpoint."), bl->owner->number);
2822 else
2823 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2824 }
2825
2826 bl->inserted = (val == 0);
2827
2828 /* We've already printed an error message if there was a problem
2829 inserting this catchpoint, and we've disabled the catchpoint,
2830 so just return success. */
2831 return 0;
2832 }
2833
2834 return 0;
2835 }
2836
2837 /* This function is called when program space PSPACE is about to be
2838 deleted. It takes care of updating breakpoints to not reference
2839 PSPACE anymore. */
2840
2841 void
2842 breakpoint_program_space_exit (struct program_space *pspace)
2843 {
2844 struct breakpoint *b, *b_temp;
2845 struct bp_location *loc, **loc_temp;
2846
2847 /* Remove any breakpoint that was set through this program space. */
2848 ALL_BREAKPOINTS_SAFE (b, b_temp)
2849 {
2850 if (b->pspace == pspace)
2851 delete_breakpoint (b);
2852 }
2853
2854 /* Breakpoints set through other program spaces could have locations
2855 bound to PSPACE as well. Remove those. */
2856 ALL_BP_LOCATIONS (loc, loc_temp)
2857 {
2858 struct bp_location *tmp;
2859
2860 if (loc->pspace == pspace)
2861 {
2862 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2863 if (loc->owner->loc == loc)
2864 loc->owner->loc = loc->next;
2865 else
2866 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2867 if (tmp->next == loc)
2868 {
2869 tmp->next = loc->next;
2870 break;
2871 }
2872 }
2873 }
2874
2875 /* Now update the global location list to permanently delete the
2876 removed locations above. */
2877 update_global_location_list (UGLL_DONT_INSERT);
2878 }
2879
2880 /* Make sure all breakpoints are inserted in inferior.
2881 Throws exception on any error.
2882 A breakpoint that is already inserted won't be inserted
2883 again, so calling this function twice is safe. */
2884 void
2885 insert_breakpoints (void)
2886 {
2887 struct breakpoint *bpt;
2888
2889 ALL_BREAKPOINTS (bpt)
2890 if (is_hardware_watchpoint (bpt))
2891 {
2892 struct watchpoint *w = (struct watchpoint *) bpt;
2893
2894 update_watchpoint (w, 0 /* don't reparse. */);
2895 }
2896
2897 /* Updating watchpoints creates new locations, so update the global
2898 location list. Explicitly tell ugll to insert locations and
2899 ignore breakpoints_always_inserted_mode. Also,
2900 update_global_location_list tries to "upgrade" software
2901 breakpoints to hardware breakpoints to handle "set breakpoint
2902 auto-hw", so we need to call it even if we don't have new
2903 locations. */
2904 update_global_location_list (UGLL_INSERT);
2905 }
2906
2907 /* Invoke CALLBACK for each of bp_location. */
2908
2909 void
2910 iterate_over_bp_locations (walk_bp_location_callback callback)
2911 {
2912 struct bp_location *loc, **loc_tmp;
2913
2914 ALL_BP_LOCATIONS (loc, loc_tmp)
2915 {
2916 callback (loc, NULL);
2917 }
2918 }
2919
2920 /* This is used when we need to synch breakpoint conditions between GDB and the
2921 target. It is the case with deleting and disabling of breakpoints when using
2922 always-inserted mode. */
2923
2924 static void
2925 update_inserted_breakpoint_locations (void)
2926 {
2927 struct bp_location *bl, **blp_tmp;
2928 int error_flag = 0;
2929 int val = 0;
2930 int disabled_breaks = 0;
2931 int hw_breakpoint_error = 0;
2932 int hw_bp_details_reported = 0;
2933
2934 string_file tmp_error_stream;
2935
2936 /* Explicitly mark the warning -- this will only be printed if
2937 there was an error. */
2938 tmp_error_stream.puts ("Warning:\n");
2939
2940 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2941
2942 ALL_BP_LOCATIONS (bl, blp_tmp)
2943 {
2944 /* We only want to update software breakpoints and hardware
2945 breakpoints. */
2946 if (!is_breakpoint (bl->owner))
2947 continue;
2948
2949 /* We only want to update locations that are already inserted
2950 and need updating. This is to avoid unwanted insertion during
2951 deletion of breakpoints. */
2952 if (!bl->inserted || !bl->needs_update)
2953 continue;
2954
2955 switch_to_program_space_and_thread (bl->pspace);
2956
2957 /* For targets that support global breakpoints, there's no need
2958 to select an inferior to insert breakpoint to. In fact, even
2959 if we aren't attached to any process yet, we should still
2960 insert breakpoints. */
2961 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2962 && (inferior_ptid == null_ptid || !target_has_execution ()))
2963 continue;
2964
2965 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
2966 &hw_breakpoint_error, &hw_bp_details_reported);
2967 if (val)
2968 error_flag = val;
2969 }
2970
2971 if (error_flag)
2972 {
2973 target_terminal::ours_for_output ();
2974 error_stream (tmp_error_stream);
2975 }
2976 }
2977
2978 /* Used when starting or continuing the program. */
2979
2980 static void
2981 insert_breakpoint_locations (void)
2982 {
2983 struct breakpoint *bpt;
2984 struct bp_location *bl, **blp_tmp;
2985 int error_flag = 0;
2986 int val = 0;
2987 int disabled_breaks = 0;
2988 int hw_breakpoint_error = 0;
2989 int hw_bp_error_explained_already = 0;
2990
2991 string_file tmp_error_stream;
2992
2993 /* Explicitly mark the warning -- this will only be printed if
2994 there was an error. */
2995 tmp_error_stream.puts ("Warning:\n");
2996
2997 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2998
2999 ALL_BP_LOCATIONS (bl, blp_tmp)
3000 {
3001 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3002 continue;
3003
3004 /* There is no point inserting thread-specific breakpoints if
3005 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3006 has BL->OWNER always non-NULL. */
3007 if (bl->owner->thread != -1
3008 && !valid_global_thread_id (bl->owner->thread))
3009 continue;
3010
3011 switch_to_program_space_and_thread (bl->pspace);
3012
3013 /* For targets that support global breakpoints, there's no need
3014 to select an inferior to insert breakpoint to. In fact, even
3015 if we aren't attached to any process yet, we should still
3016 insert breakpoints. */
3017 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3018 && (inferior_ptid == null_ptid || !target_has_execution ()))
3019 continue;
3020
3021 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3022 &hw_breakpoint_error, &hw_bp_error_explained_already);
3023 if (val)
3024 error_flag = val;
3025 }
3026
3027 /* If we failed to insert all locations of a watchpoint, remove
3028 them, as half-inserted watchpoint is of limited use. */
3029 ALL_BREAKPOINTS (bpt)
3030 {
3031 int some_failed = 0;
3032 struct bp_location *loc;
3033
3034 if (!is_hardware_watchpoint (bpt))
3035 continue;
3036
3037 if (!breakpoint_enabled (bpt))
3038 continue;
3039
3040 if (bpt->disposition == disp_del_at_next_stop)
3041 continue;
3042
3043 for (loc = bpt->loc; loc; loc = loc->next)
3044 if (!loc->inserted && should_be_inserted (loc))
3045 {
3046 some_failed = 1;
3047 break;
3048 }
3049 if (some_failed)
3050 {
3051 for (loc = bpt->loc; loc; loc = loc->next)
3052 if (loc->inserted)
3053 remove_breakpoint (loc);
3054
3055 hw_breakpoint_error = 1;
3056 tmp_error_stream.printf ("Could not insert "
3057 "hardware watchpoint %d.\n",
3058 bpt->number);
3059 error_flag = -1;
3060 }
3061 }
3062
3063 if (error_flag)
3064 {
3065 /* If a hardware breakpoint or watchpoint was inserted, add a
3066 message about possibly exhausted resources. */
3067 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3068 {
3069 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3070 You may have requested too many hardware breakpoints/watchpoints.\n");
3071 }
3072 target_terminal::ours_for_output ();
3073 error_stream (tmp_error_stream);
3074 }
3075 }
3076
3077 /* Used when the program stops.
3078 Returns zero if successful, or non-zero if there was a problem
3079 removing a breakpoint location. */
3080
3081 int
3082 remove_breakpoints (void)
3083 {
3084 struct bp_location *bl, **blp_tmp;
3085 int val = 0;
3086
3087 ALL_BP_LOCATIONS (bl, blp_tmp)
3088 {
3089 if (bl->inserted && !is_tracepoint (bl->owner))
3090 val |= remove_breakpoint (bl);
3091 }
3092 return val;
3093 }
3094
3095 /* When a thread exits, remove breakpoints that are related to
3096 that thread. */
3097
3098 static void
3099 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3100 {
3101 struct breakpoint *b, *b_tmp;
3102
3103 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3104 {
3105 if (b->thread == tp->global_num && user_breakpoint_p (b))
3106 {
3107 b->disposition = disp_del_at_next_stop;
3108
3109 printf_filtered (_("\
3110 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3111 b->number, print_thread_id (tp));
3112
3113 /* Hide it from the user. */
3114 b->number = 0;
3115 }
3116 }
3117 }
3118
3119 /* See breakpoint.h. */
3120
3121 void
3122 remove_breakpoints_inf (inferior *inf)
3123 {
3124 struct bp_location *bl, **blp_tmp;
3125 int val;
3126
3127 ALL_BP_LOCATIONS (bl, blp_tmp)
3128 {
3129 if (bl->pspace != inf->pspace)
3130 continue;
3131
3132 if (bl->inserted && !bl->target_info.persist)
3133 {
3134 val = remove_breakpoint (bl);
3135 if (val != 0)
3136 return;
3137 }
3138 }
3139 }
3140
3141 static int internal_breakpoint_number = -1;
3142
3143 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3144 If INTERNAL is non-zero, the breakpoint number will be populated
3145 from internal_breakpoint_number and that variable decremented.
3146 Otherwise the breakpoint number will be populated from
3147 breakpoint_count and that value incremented. Internal breakpoints
3148 do not set the internal var bpnum. */
3149 static void
3150 set_breakpoint_number (int internal, struct breakpoint *b)
3151 {
3152 if (internal)
3153 b->number = internal_breakpoint_number--;
3154 else
3155 {
3156 set_breakpoint_count (breakpoint_count + 1);
3157 b->number = breakpoint_count;
3158 }
3159 }
3160
3161 static struct breakpoint *
3162 create_internal_breakpoint (struct gdbarch *gdbarch,
3163 CORE_ADDR address, enum bptype type,
3164 const struct breakpoint_ops *ops)
3165 {
3166 symtab_and_line sal;
3167 sal.pc = address;
3168 sal.section = find_pc_overlay (sal.pc);
3169 sal.pspace = current_program_space;
3170
3171 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
3172 b->number = internal_breakpoint_number--;
3173 b->disposition = disp_donttouch;
3174
3175 return b;
3176 }
3177
3178 static const char *const longjmp_names[] =
3179 {
3180 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3181 };
3182 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3183
3184 /* Per-objfile data private to breakpoint.c. */
3185 struct breakpoint_objfile_data
3186 {
3187 /* Minimal symbol for "_ovly_debug_event" (if any). */
3188 struct bound_minimal_symbol overlay_msym {};
3189
3190 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3191 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
3192
3193 /* True if we have looked for longjmp probes. */
3194 int longjmp_searched = 0;
3195
3196 /* SystemTap probe points for longjmp (if any). These are non-owning
3197 references. */
3198 std::vector<probe *> longjmp_probes;
3199
3200 /* Minimal symbol for "std::terminate()" (if any). */
3201 struct bound_minimal_symbol terminate_msym {};
3202
3203 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3204 struct bound_minimal_symbol exception_msym {};
3205
3206 /* True if we have looked for exception probes. */
3207 int exception_searched = 0;
3208
3209 /* SystemTap probe points for unwinding (if any). These are non-owning
3210 references. */
3211 std::vector<probe *> exception_probes;
3212 };
3213
3214 static const struct objfile_key<breakpoint_objfile_data>
3215 breakpoint_objfile_key;
3216
3217 /* Minimal symbol not found sentinel. */
3218 static struct minimal_symbol msym_not_found;
3219
3220 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3221
3222 static int
3223 msym_not_found_p (const struct minimal_symbol *msym)
3224 {
3225 return msym == &msym_not_found;
3226 }
3227
3228 /* Return per-objfile data needed by breakpoint.c.
3229 Allocate the data if necessary. */
3230
3231 static struct breakpoint_objfile_data *
3232 get_breakpoint_objfile_data (struct objfile *objfile)
3233 {
3234 struct breakpoint_objfile_data *bp_objfile_data;
3235
3236 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3237 if (bp_objfile_data == NULL)
3238 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3239 return bp_objfile_data;
3240 }
3241
3242 static void
3243 create_overlay_event_breakpoint (void)
3244 {
3245 const char *const func_name = "_ovly_debug_event";
3246
3247 for (objfile *objfile : current_program_space->objfiles ())
3248 {
3249 struct breakpoint *b;
3250 struct breakpoint_objfile_data *bp_objfile_data;
3251 CORE_ADDR addr;
3252 struct explicit_location explicit_loc;
3253
3254 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3255
3256 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3257 continue;
3258
3259 if (bp_objfile_data->overlay_msym.minsym == NULL)
3260 {
3261 struct bound_minimal_symbol m;
3262
3263 m = lookup_minimal_symbol_text (func_name, objfile);
3264 if (m.minsym == NULL)
3265 {
3266 /* Avoid future lookups in this objfile. */
3267 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3268 continue;
3269 }
3270 bp_objfile_data->overlay_msym = m;
3271 }
3272
3273 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3274 b = create_internal_breakpoint (objfile->arch (), addr,
3275 bp_overlay_event,
3276 &internal_breakpoint_ops);
3277 initialize_explicit_location (&explicit_loc);
3278 explicit_loc.function_name = ASTRDUP (func_name);
3279 b->location = new_explicit_location (&explicit_loc);
3280
3281 if (overlay_debugging == ovly_auto)
3282 {
3283 b->enable_state = bp_enabled;
3284 overlay_events_enabled = 1;
3285 }
3286 else
3287 {
3288 b->enable_state = bp_disabled;
3289 overlay_events_enabled = 0;
3290 }
3291 }
3292 }
3293
3294 static void
3295 create_longjmp_master_breakpoint (void)
3296 {
3297 scoped_restore_current_program_space restore_pspace;
3298
3299 for (struct program_space *pspace : program_spaces)
3300 {
3301 set_current_program_space (pspace);
3302
3303 for (objfile *objfile : current_program_space->objfiles ())
3304 {
3305 int i;
3306 struct gdbarch *gdbarch;
3307 struct breakpoint_objfile_data *bp_objfile_data;
3308
3309 gdbarch = objfile->arch ();
3310
3311 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3312
3313 if (!bp_objfile_data->longjmp_searched)
3314 {
3315 std::vector<probe *> ret
3316 = find_probes_in_objfile (objfile, "libc", "longjmp");
3317
3318 if (!ret.empty ())
3319 {
3320 /* We are only interested in checking one element. */
3321 probe *p = ret[0];
3322
3323 if (!p->can_evaluate_arguments ())
3324 {
3325 /* We cannot use the probe interface here,
3326 because it does not know how to evaluate
3327 arguments. */
3328 ret.clear ();
3329 }
3330 }
3331 bp_objfile_data->longjmp_probes = ret;
3332 bp_objfile_data->longjmp_searched = 1;
3333 }
3334
3335 if (!bp_objfile_data->longjmp_probes.empty ())
3336 {
3337 for (probe *p : bp_objfile_data->longjmp_probes)
3338 {
3339 struct breakpoint *b;
3340
3341 b = create_internal_breakpoint (gdbarch,
3342 p->get_relocated_address (objfile),
3343 bp_longjmp_master,
3344 &internal_breakpoint_ops);
3345 b->location = new_probe_location ("-probe-stap libc:longjmp");
3346 b->enable_state = bp_disabled;
3347 }
3348
3349 continue;
3350 }
3351
3352 if (!gdbarch_get_longjmp_target_p (gdbarch))
3353 continue;
3354
3355 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3356 {
3357 struct breakpoint *b;
3358 const char *func_name;
3359 CORE_ADDR addr;
3360 struct explicit_location explicit_loc;
3361
3362 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3363 continue;
3364
3365 func_name = longjmp_names[i];
3366 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3367 {
3368 struct bound_minimal_symbol m;
3369
3370 m = lookup_minimal_symbol_text (func_name, objfile);
3371 if (m.minsym == NULL)
3372 {
3373 /* Prevent future lookups in this objfile. */
3374 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3375 continue;
3376 }
3377 bp_objfile_data->longjmp_msym[i] = m;
3378 }
3379
3380 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3381 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3382 &internal_breakpoint_ops);
3383 initialize_explicit_location (&explicit_loc);
3384 explicit_loc.function_name = ASTRDUP (func_name);
3385 b->location = new_explicit_location (&explicit_loc);
3386 b->enable_state = bp_disabled;
3387 }
3388 }
3389 }
3390 }
3391
3392 /* Create a master std::terminate breakpoint. */
3393 static void
3394 create_std_terminate_master_breakpoint (void)
3395 {
3396 const char *const func_name = "std::terminate()";
3397
3398 scoped_restore_current_program_space restore_pspace;
3399
3400 for (struct program_space *pspace : program_spaces)
3401 {
3402 CORE_ADDR addr;
3403
3404 set_current_program_space (pspace);
3405
3406 for (objfile *objfile : current_program_space->objfiles ())
3407 {
3408 struct breakpoint *b;
3409 struct breakpoint_objfile_data *bp_objfile_data;
3410 struct explicit_location explicit_loc;
3411
3412 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3413
3414 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3415 continue;
3416
3417 if (bp_objfile_data->terminate_msym.minsym == NULL)
3418 {
3419 struct bound_minimal_symbol m;
3420
3421 m = lookup_minimal_symbol (func_name, NULL, objfile);
3422 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3423 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3424 {
3425 /* Prevent future lookups in this objfile. */
3426 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3427 continue;
3428 }
3429 bp_objfile_data->terminate_msym = m;
3430 }
3431
3432 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3433 b = create_internal_breakpoint (objfile->arch (), addr,
3434 bp_std_terminate_master,
3435 &internal_breakpoint_ops);
3436 initialize_explicit_location (&explicit_loc);
3437 explicit_loc.function_name = ASTRDUP (func_name);
3438 b->location = new_explicit_location (&explicit_loc);
3439 b->enable_state = bp_disabled;
3440 }
3441 }
3442 }
3443
3444 /* Install a master breakpoint on the unwinder's debug hook. */
3445
3446 static void
3447 create_exception_master_breakpoint (void)
3448 {
3449 const char *const func_name = "_Unwind_DebugHook";
3450
3451 for (objfile *objfile : current_program_space->objfiles ())
3452 {
3453 struct breakpoint *b;
3454 struct gdbarch *gdbarch;
3455 struct breakpoint_objfile_data *bp_objfile_data;
3456 CORE_ADDR addr;
3457 struct explicit_location explicit_loc;
3458
3459 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3460
3461 /* We prefer the SystemTap probe point if it exists. */
3462 if (!bp_objfile_data->exception_searched)
3463 {
3464 std::vector<probe *> ret
3465 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3466
3467 if (!ret.empty ())
3468 {
3469 /* We are only interested in checking one element. */
3470 probe *p = ret[0];
3471
3472 if (!p->can_evaluate_arguments ())
3473 {
3474 /* We cannot use the probe interface here, because it does
3475 not know how to evaluate arguments. */
3476 ret.clear ();
3477 }
3478 }
3479 bp_objfile_data->exception_probes = ret;
3480 bp_objfile_data->exception_searched = 1;
3481 }
3482
3483 if (!bp_objfile_data->exception_probes.empty ())
3484 {
3485 gdbarch = objfile->arch ();
3486
3487 for (probe *p : bp_objfile_data->exception_probes)
3488 {
3489 b = create_internal_breakpoint (gdbarch,
3490 p->get_relocated_address (objfile),
3491 bp_exception_master,
3492 &internal_breakpoint_ops);
3493 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3494 b->enable_state = bp_disabled;
3495 }
3496
3497 continue;
3498 }
3499
3500 /* Otherwise, try the hook function. */
3501
3502 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3503 continue;
3504
3505 gdbarch = objfile->arch ();
3506
3507 if (bp_objfile_data->exception_msym.minsym == NULL)
3508 {
3509 struct bound_minimal_symbol debug_hook;
3510
3511 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3512 if (debug_hook.minsym == NULL)
3513 {
3514 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3515 continue;
3516 }
3517
3518 bp_objfile_data->exception_msym = debug_hook;
3519 }
3520
3521 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3522 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3523 current_top_target ());
3524 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3525 &internal_breakpoint_ops);
3526 initialize_explicit_location (&explicit_loc);
3527 explicit_loc.function_name = ASTRDUP (func_name);
3528 b->location = new_explicit_location (&explicit_loc);
3529 b->enable_state = bp_disabled;
3530 }
3531 }
3532
3533 /* Does B have a location spec? */
3534
3535 static int
3536 breakpoint_event_location_empty_p (const struct breakpoint *b)
3537 {
3538 return b->location != NULL && event_location_empty_p (b->location.get ());
3539 }
3540
3541 void
3542 update_breakpoints_after_exec (void)
3543 {
3544 struct breakpoint *b, *b_tmp;
3545 struct bp_location *bploc, **bplocp_tmp;
3546
3547 /* We're about to delete breakpoints from GDB's lists. If the
3548 INSERTED flag is true, GDB will try to lift the breakpoints by
3549 writing the breakpoints' "shadow contents" back into memory. The
3550 "shadow contents" are NOT valid after an exec, so GDB should not
3551 do that. Instead, the target is responsible from marking
3552 breakpoints out as soon as it detects an exec. We don't do that
3553 here instead, because there may be other attempts to delete
3554 breakpoints after detecting an exec and before reaching here. */
3555 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3556 if (bploc->pspace == current_program_space)
3557 gdb_assert (!bploc->inserted);
3558
3559 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3560 {
3561 if (b->pspace != current_program_space)
3562 continue;
3563
3564 /* Solib breakpoints must be explicitly reset after an exec(). */
3565 if (b->type == bp_shlib_event)
3566 {
3567 delete_breakpoint (b);
3568 continue;
3569 }
3570
3571 /* JIT breakpoints must be explicitly reset after an exec(). */
3572 if (b->type == bp_jit_event)
3573 {
3574 delete_breakpoint (b);
3575 continue;
3576 }
3577
3578 /* Thread event breakpoints must be set anew after an exec(),
3579 as must overlay event and longjmp master breakpoints. */
3580 if (b->type == bp_thread_event || b->type == bp_overlay_event
3581 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3582 || b->type == bp_exception_master)
3583 {
3584 delete_breakpoint (b);
3585 continue;
3586 }
3587
3588 /* Step-resume breakpoints are meaningless after an exec(). */
3589 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3590 {
3591 delete_breakpoint (b);
3592 continue;
3593 }
3594
3595 /* Just like single-step breakpoints. */
3596 if (b->type == bp_single_step)
3597 {
3598 delete_breakpoint (b);
3599 continue;
3600 }
3601
3602 /* Longjmp and longjmp-resume breakpoints are also meaningless
3603 after an exec. */
3604 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3605 || b->type == bp_longjmp_call_dummy
3606 || b->type == bp_exception || b->type == bp_exception_resume)
3607 {
3608 delete_breakpoint (b);
3609 continue;
3610 }
3611
3612 if (b->type == bp_catchpoint)
3613 {
3614 /* For now, none of the bp_catchpoint breakpoints need to
3615 do anything at this point. In the future, if some of
3616 the catchpoints need to something, we will need to add
3617 a new method, and call this method from here. */
3618 continue;
3619 }
3620
3621 /* bp_finish is a special case. The only way we ought to be able
3622 to see one of these when an exec() has happened, is if the user
3623 caught a vfork, and then said "finish". Ordinarily a finish just
3624 carries them to the call-site of the current callee, by setting
3625 a temporary bp there and resuming. But in this case, the finish
3626 will carry them entirely through the vfork & exec.
3627
3628 We don't want to allow a bp_finish to remain inserted now. But
3629 we can't safely delete it, 'cause finish_command has a handle to
3630 the bp on a bpstat, and will later want to delete it. There's a
3631 chance (and I've seen it happen) that if we delete the bp_finish
3632 here, that its storage will get reused by the time finish_command
3633 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3634 We really must allow finish_command to delete a bp_finish.
3635
3636 In the absence of a general solution for the "how do we know
3637 it's safe to delete something others may have handles to?"
3638 problem, what we'll do here is just uninsert the bp_finish, and
3639 let finish_command delete it.
3640
3641 (We know the bp_finish is "doomed" in the sense that it's
3642 momentary, and will be deleted as soon as finish_command sees
3643 the inferior stopped. So it doesn't matter that the bp's
3644 address is probably bogus in the new a.out, unlike e.g., the
3645 solib breakpoints.) */
3646
3647 if (b->type == bp_finish)
3648 {
3649 continue;
3650 }
3651
3652 /* Without a symbolic address, we have little hope of the
3653 pre-exec() address meaning the same thing in the post-exec()
3654 a.out. */
3655 if (breakpoint_event_location_empty_p (b))
3656 {
3657 delete_breakpoint (b);
3658 continue;
3659 }
3660 }
3661 }
3662
3663 int
3664 detach_breakpoints (ptid_t ptid)
3665 {
3666 struct bp_location *bl, **blp_tmp;
3667 int val = 0;
3668 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3669 struct inferior *inf = current_inferior ();
3670
3671 if (ptid.pid () == inferior_ptid.pid ())
3672 error (_("Cannot detach breakpoints of inferior_ptid"));
3673
3674 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3675 inferior_ptid = ptid;
3676 ALL_BP_LOCATIONS (bl, blp_tmp)
3677 {
3678 if (bl->pspace != inf->pspace)
3679 continue;
3680
3681 /* This function must physically remove breakpoints locations
3682 from the specified ptid, without modifying the breakpoint
3683 package's state. Locations of type bp_loc_other are only
3684 maintained at GDB side. So, there is no need to remove
3685 these bp_loc_other locations. Moreover, removing these
3686 would modify the breakpoint package's state. */
3687 if (bl->loc_type == bp_loc_other)
3688 continue;
3689
3690 if (bl->inserted)
3691 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3692 }
3693
3694 return val;
3695 }
3696
3697 /* Remove the breakpoint location BL from the current address space.
3698 Note that this is used to detach breakpoints from a child fork.
3699 When we get here, the child isn't in the inferior list, and neither
3700 do we have objects to represent its address space --- we should
3701 *not* look at bl->pspace->aspace here. */
3702
3703 static int
3704 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3705 {
3706 int val;
3707
3708 /* BL is never in moribund_locations by our callers. */
3709 gdb_assert (bl->owner != NULL);
3710
3711 /* The type of none suggests that owner is actually deleted.
3712 This should not ever happen. */
3713 gdb_assert (bl->owner->type != bp_none);
3714
3715 if (bl->loc_type == bp_loc_software_breakpoint
3716 || bl->loc_type == bp_loc_hardware_breakpoint)
3717 {
3718 /* "Normal" instruction breakpoint: either the standard
3719 trap-instruction bp (bp_breakpoint), or a
3720 bp_hardware_breakpoint. */
3721
3722 /* First check to see if we have to handle an overlay. */
3723 if (overlay_debugging == ovly_off
3724 || bl->section == NULL
3725 || !(section_is_overlay (bl->section)))
3726 {
3727 /* No overlay handling: just remove the breakpoint. */
3728
3729 /* If we're trying to uninsert a memory breakpoint that we
3730 know is set in a dynamic object that is marked
3731 shlib_disabled, then either the dynamic object was
3732 removed with "remove-symbol-file" or with
3733 "nosharedlibrary". In the former case, we don't know
3734 whether another dynamic object might have loaded over the
3735 breakpoint's address -- the user might well let us know
3736 about it next with add-symbol-file (the whole point of
3737 add-symbol-file is letting the user manually maintain a
3738 list of dynamically loaded objects). If we have the
3739 breakpoint's shadow memory, that is, this is a software
3740 breakpoint managed by GDB, check whether the breakpoint
3741 is still inserted in memory, to avoid overwriting wrong
3742 code with stale saved shadow contents. Note that HW
3743 breakpoints don't have shadow memory, as they're
3744 implemented using a mechanism that is not dependent on
3745 being able to modify the target's memory, and as such
3746 they should always be removed. */
3747 if (bl->shlib_disabled
3748 && bl->target_info.shadow_len != 0
3749 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3750 val = 0;
3751 else
3752 val = bl->owner->ops->remove_location (bl, reason);
3753 }
3754 else
3755 {
3756 /* This breakpoint is in an overlay section.
3757 Did we set a breakpoint at the LMA? */
3758 if (!overlay_events_enabled)
3759 {
3760 /* Yes -- overlay event support is not active, so we
3761 should have set a breakpoint at the LMA. Remove it.
3762 */
3763 /* Ignore any failures: if the LMA is in ROM, we will
3764 have already warned when we failed to insert it. */
3765 if (bl->loc_type == bp_loc_hardware_breakpoint)
3766 target_remove_hw_breakpoint (bl->gdbarch,
3767 &bl->overlay_target_info);
3768 else
3769 target_remove_breakpoint (bl->gdbarch,
3770 &bl->overlay_target_info,
3771 reason);
3772 }
3773 /* Did we set a breakpoint at the VMA?
3774 If so, we will have marked the breakpoint 'inserted'. */
3775 if (bl->inserted)
3776 {
3777 /* Yes -- remove it. Previously we did not bother to
3778 remove the breakpoint if the section had been
3779 unmapped, but let's not rely on that being safe. We
3780 don't know what the overlay manager might do. */
3781
3782 /* However, we should remove *software* breakpoints only
3783 if the section is still mapped, or else we overwrite
3784 wrong code with the saved shadow contents. */
3785 if (bl->loc_type == bp_loc_hardware_breakpoint
3786 || section_is_mapped (bl->section))
3787 val = bl->owner->ops->remove_location (bl, reason);
3788 else
3789 val = 0;
3790 }
3791 else
3792 {
3793 /* No -- not inserted, so no need to remove. No error. */
3794 val = 0;
3795 }
3796 }
3797
3798 /* In some cases, we might not be able to remove a breakpoint in
3799 a shared library that has already been removed, but we have
3800 not yet processed the shlib unload event. Similarly for an
3801 unloaded add-symbol-file object - the user might not yet have
3802 had the chance to remove-symbol-file it. shlib_disabled will
3803 be set if the library/object has already been removed, but
3804 the breakpoint hasn't been uninserted yet, e.g., after
3805 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3806 always-inserted mode. */
3807 if (val
3808 && (bl->loc_type == bp_loc_software_breakpoint
3809 && (bl->shlib_disabled
3810 || solib_name_from_address (bl->pspace, bl->address)
3811 || shared_objfile_contains_address_p (bl->pspace,
3812 bl->address))))
3813 val = 0;
3814
3815 if (val)
3816 return val;
3817 bl->inserted = (reason == DETACH_BREAKPOINT);
3818 }
3819 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3820 {
3821 gdb_assert (bl->owner->ops != NULL
3822 && bl->owner->ops->remove_location != NULL);
3823
3824 bl->inserted = (reason == DETACH_BREAKPOINT);
3825 bl->owner->ops->remove_location (bl, reason);
3826
3827 /* Failure to remove any of the hardware watchpoints comes here. */
3828 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3829 warning (_("Could not remove hardware watchpoint %d."),
3830 bl->owner->number);
3831 }
3832 else if (bl->owner->type == bp_catchpoint
3833 && breakpoint_enabled (bl->owner)
3834 && !bl->duplicate)
3835 {
3836 gdb_assert (bl->owner->ops != NULL
3837 && bl->owner->ops->remove_location != NULL);
3838
3839 val = bl->owner->ops->remove_location (bl, reason);
3840 if (val)
3841 return val;
3842
3843 bl->inserted = (reason == DETACH_BREAKPOINT);
3844 }
3845
3846 return 0;
3847 }
3848
3849 static int
3850 remove_breakpoint (struct bp_location *bl)
3851 {
3852 /* BL is never in moribund_locations by our callers. */
3853 gdb_assert (bl->owner != NULL);
3854
3855 /* The type of none suggests that owner is actually deleted.
3856 This should not ever happen. */
3857 gdb_assert (bl->owner->type != bp_none);
3858
3859 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3860
3861 switch_to_program_space_and_thread (bl->pspace);
3862
3863 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
3864 }
3865
3866 /* Clear the "inserted" flag in all breakpoints. */
3867
3868 void
3869 mark_breakpoints_out (void)
3870 {
3871 struct bp_location *bl, **blp_tmp;
3872
3873 ALL_BP_LOCATIONS (bl, blp_tmp)
3874 if (bl->pspace == current_program_space)
3875 bl->inserted = 0;
3876 }
3877
3878 /* Clear the "inserted" flag in all breakpoints and delete any
3879 breakpoints which should go away between runs of the program.
3880
3881 Plus other such housekeeping that has to be done for breakpoints
3882 between runs.
3883
3884 Note: this function gets called at the end of a run (by
3885 generic_mourn_inferior) and when a run begins (by
3886 init_wait_for_inferior). */
3887
3888
3889
3890 void
3891 breakpoint_init_inferior (enum inf_context context)
3892 {
3893 struct breakpoint *b, *b_tmp;
3894 struct program_space *pspace = current_program_space;
3895
3896 /* If breakpoint locations are shared across processes, then there's
3897 nothing to do. */
3898 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
3899 return;
3900
3901 mark_breakpoints_out ();
3902
3903 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3904 {
3905 if (b->loc && b->loc->pspace != pspace)
3906 continue;
3907
3908 switch (b->type)
3909 {
3910 case bp_call_dummy:
3911 case bp_longjmp_call_dummy:
3912
3913 /* If the call dummy breakpoint is at the entry point it will
3914 cause problems when the inferior is rerun, so we better get
3915 rid of it. */
3916
3917 case bp_watchpoint_scope:
3918
3919 /* Also get rid of scope breakpoints. */
3920
3921 case bp_shlib_event:
3922
3923 /* Also remove solib event breakpoints. Their addresses may
3924 have changed since the last time we ran the program.
3925 Actually we may now be debugging against different target;
3926 and so the solib backend that installed this breakpoint may
3927 not be used in by the target. E.g.,
3928
3929 (gdb) file prog-linux
3930 (gdb) run # native linux target
3931 ...
3932 (gdb) kill
3933 (gdb) file prog-win.exe
3934 (gdb) tar rem :9999 # remote Windows gdbserver.
3935 */
3936
3937 case bp_step_resume:
3938
3939 /* Also remove step-resume breakpoints. */
3940
3941 case bp_single_step:
3942
3943 /* Also remove single-step breakpoints. */
3944
3945 delete_breakpoint (b);
3946 break;
3947
3948 case bp_watchpoint:
3949 case bp_hardware_watchpoint:
3950 case bp_read_watchpoint:
3951 case bp_access_watchpoint:
3952 {
3953 struct watchpoint *w = (struct watchpoint *) b;
3954
3955 /* Likewise for watchpoints on local expressions. */
3956 if (w->exp_valid_block != NULL)
3957 delete_breakpoint (b);
3958 else
3959 {
3960 /* Get rid of existing locations, which are no longer
3961 valid. New ones will be created in
3962 update_watchpoint, when the inferior is restarted.
3963 The next update_global_location_list call will
3964 garbage collect them. */
3965 b->loc = NULL;
3966
3967 if (context == inf_starting)
3968 {
3969 /* Reset val field to force reread of starting value in
3970 insert_breakpoints. */
3971 w->val.reset (nullptr);
3972 w->val_valid = false;
3973 }
3974 }
3975 }
3976 break;
3977 default:
3978 break;
3979 }
3980 }
3981
3982 /* Get rid of the moribund locations. */
3983 for (bp_location *bl : moribund_locations)
3984 decref_bp_location (&bl);
3985 moribund_locations.clear ();
3986 }
3987
3988 /* These functions concern about actual breakpoints inserted in the
3989 target --- to e.g. check if we need to do decr_pc adjustment or if
3990 we need to hop over the bkpt --- so we check for address space
3991 match, not program space. */
3992
3993 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3994 exists at PC. It returns ordinary_breakpoint_here if it's an
3995 ordinary breakpoint, or permanent_breakpoint_here if it's a
3996 permanent breakpoint.
3997 - When continuing from a location with an ordinary breakpoint, we
3998 actually single step once before calling insert_breakpoints.
3999 - When continuing from a location with a permanent breakpoint, we
4000 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4001 the target, to advance the PC past the breakpoint. */
4002
4003 enum breakpoint_here
4004 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4005 {
4006 struct bp_location *bl, **blp_tmp;
4007 int any_breakpoint_here = 0;
4008
4009 ALL_BP_LOCATIONS (bl, blp_tmp)
4010 {
4011 if (bl->loc_type != bp_loc_software_breakpoint
4012 && bl->loc_type != bp_loc_hardware_breakpoint)
4013 continue;
4014
4015 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4016 if ((breakpoint_enabled (bl->owner)
4017 || bl->permanent)
4018 && breakpoint_location_address_match (bl, aspace, pc))
4019 {
4020 if (overlay_debugging
4021 && section_is_overlay (bl->section)
4022 && !section_is_mapped (bl->section))
4023 continue; /* unmapped overlay -- can't be a match */
4024 else if (bl->permanent)
4025 return permanent_breakpoint_here;
4026 else
4027 any_breakpoint_here = 1;
4028 }
4029 }
4030
4031 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4032 }
4033
4034 /* See breakpoint.h. */
4035
4036 int
4037 breakpoint_in_range_p (const address_space *aspace,
4038 CORE_ADDR addr, ULONGEST len)
4039 {
4040 struct bp_location *bl, **blp_tmp;
4041
4042 ALL_BP_LOCATIONS (bl, blp_tmp)
4043 {
4044 if (bl->loc_type != bp_loc_software_breakpoint
4045 && bl->loc_type != bp_loc_hardware_breakpoint)
4046 continue;
4047
4048 if ((breakpoint_enabled (bl->owner)
4049 || bl->permanent)
4050 && breakpoint_location_address_range_overlap (bl, aspace,
4051 addr, len))
4052 {
4053 if (overlay_debugging
4054 && section_is_overlay (bl->section)
4055 && !section_is_mapped (bl->section))
4056 {
4057 /* Unmapped overlay -- can't be a match. */
4058 continue;
4059 }
4060
4061 return 1;
4062 }
4063 }
4064
4065 return 0;
4066 }
4067
4068 /* Return true if there's a moribund breakpoint at PC. */
4069
4070 int
4071 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4072 {
4073 for (bp_location *loc : moribund_locations)
4074 if (breakpoint_location_address_match (loc, aspace, pc))
4075 return 1;
4076
4077 return 0;
4078 }
4079
4080 /* Returns non-zero iff BL is inserted at PC, in address space
4081 ASPACE. */
4082
4083 static int
4084 bp_location_inserted_here_p (struct bp_location *bl,
4085 const address_space *aspace, CORE_ADDR pc)
4086 {
4087 if (bl->inserted
4088 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4089 aspace, pc))
4090 {
4091 if (overlay_debugging
4092 && section_is_overlay (bl->section)
4093 && !section_is_mapped (bl->section))
4094 return 0; /* unmapped overlay -- can't be a match */
4095 else
4096 return 1;
4097 }
4098 return 0;
4099 }
4100
4101 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4102
4103 int
4104 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4105 {
4106 struct bp_location **blp, **blp_tmp = NULL;
4107
4108 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4109 {
4110 struct bp_location *bl = *blp;
4111
4112 if (bl->loc_type != bp_loc_software_breakpoint
4113 && bl->loc_type != bp_loc_hardware_breakpoint)
4114 continue;
4115
4116 if (bp_location_inserted_here_p (bl, aspace, pc))
4117 return 1;
4118 }
4119 return 0;
4120 }
4121
4122 /* This function returns non-zero iff there is a software breakpoint
4123 inserted at PC. */
4124
4125 int
4126 software_breakpoint_inserted_here_p (const address_space *aspace,
4127 CORE_ADDR pc)
4128 {
4129 struct bp_location **blp, **blp_tmp = NULL;
4130
4131 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4132 {
4133 struct bp_location *bl = *blp;
4134
4135 if (bl->loc_type != bp_loc_software_breakpoint)
4136 continue;
4137
4138 if (bp_location_inserted_here_p (bl, aspace, pc))
4139 return 1;
4140 }
4141
4142 return 0;
4143 }
4144
4145 /* See breakpoint.h. */
4146
4147 int
4148 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4149 CORE_ADDR pc)
4150 {
4151 struct bp_location **blp, **blp_tmp = NULL;
4152
4153 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4154 {
4155 struct bp_location *bl = *blp;
4156
4157 if (bl->loc_type != bp_loc_hardware_breakpoint)
4158 continue;
4159
4160 if (bp_location_inserted_here_p (bl, aspace, pc))
4161 return 1;
4162 }
4163
4164 return 0;
4165 }
4166
4167 int
4168 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4169 CORE_ADDR addr, ULONGEST len)
4170 {
4171 struct breakpoint *bpt;
4172
4173 ALL_BREAKPOINTS (bpt)
4174 {
4175 struct bp_location *loc;
4176
4177 if (bpt->type != bp_hardware_watchpoint
4178 && bpt->type != bp_access_watchpoint)
4179 continue;
4180
4181 if (!breakpoint_enabled (bpt))
4182 continue;
4183
4184 for (loc = bpt->loc; loc; loc = loc->next)
4185 if (loc->pspace->aspace == aspace && loc->inserted)
4186 {
4187 CORE_ADDR l, h;
4188
4189 /* Check for intersection. */
4190 l = std::max<CORE_ADDR> (loc->address, addr);
4191 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4192 if (l < h)
4193 return 1;
4194 }
4195 }
4196 return 0;
4197 }
4198
4199 /* See breakpoint.h. */
4200
4201 bool
4202 is_catchpoint (struct breakpoint *b)
4203 {
4204 return (b->type == bp_catchpoint);
4205 }
4206
4207 /* Frees any storage that is part of a bpstat. Does not walk the
4208 'next' chain. */
4209
4210 bpstats::~bpstats ()
4211 {
4212 if (bp_location_at != NULL)
4213 decref_bp_location (&bp_location_at);
4214 }
4215
4216 /* Clear a bpstat so that it says we are not at any breakpoint.
4217 Also free any storage that is part of a bpstat. */
4218
4219 void
4220 bpstat_clear (bpstat *bsp)
4221 {
4222 bpstat p;
4223 bpstat q;
4224
4225 if (bsp == 0)
4226 return;
4227 p = *bsp;
4228 while (p != NULL)
4229 {
4230 q = p->next;
4231 delete p;
4232 p = q;
4233 }
4234 *bsp = NULL;
4235 }
4236
4237 bpstats::bpstats (const bpstats &other)
4238 : next (NULL),
4239 bp_location_at (other.bp_location_at),
4240 breakpoint_at (other.breakpoint_at),
4241 commands (other.commands),
4242 print (other.print),
4243 stop (other.stop),
4244 print_it (other.print_it)
4245 {
4246 if (other.old_val != NULL)
4247 old_val = release_value (value_copy (other.old_val.get ()));
4248 incref_bp_location (bp_location_at);
4249 }
4250
4251 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4252 is part of the bpstat is copied as well. */
4253
4254 bpstat
4255 bpstat_copy (bpstat bs)
4256 {
4257 bpstat p = NULL;
4258 bpstat tmp;
4259 bpstat retval = NULL;
4260
4261 if (bs == NULL)
4262 return bs;
4263
4264 for (; bs != NULL; bs = bs->next)
4265 {
4266 tmp = new bpstats (*bs);
4267
4268 if (p == NULL)
4269 /* This is the first thing in the chain. */
4270 retval = tmp;
4271 else
4272 p->next = tmp;
4273 p = tmp;
4274 }
4275 p->next = NULL;
4276 return retval;
4277 }
4278
4279 /* Find the bpstat associated with this breakpoint. */
4280
4281 bpstat
4282 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4283 {
4284 if (bsp == NULL)
4285 return NULL;
4286
4287 for (; bsp != NULL; bsp = bsp->next)
4288 {
4289 if (bsp->breakpoint_at == breakpoint)
4290 return bsp;
4291 }
4292 return NULL;
4293 }
4294
4295 /* See breakpoint.h. */
4296
4297 bool
4298 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4299 {
4300 for (; bsp != NULL; bsp = bsp->next)
4301 {
4302 if (bsp->breakpoint_at == NULL)
4303 {
4304 /* A moribund location can never explain a signal other than
4305 GDB_SIGNAL_TRAP. */
4306 if (sig == GDB_SIGNAL_TRAP)
4307 return true;
4308 }
4309 else
4310 {
4311 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4312 sig))
4313 return true;
4314 }
4315 }
4316
4317 return false;
4318 }
4319
4320 /* Put in *NUM the breakpoint number of the first breakpoint we are
4321 stopped at. *BSP upon return is a bpstat which points to the
4322 remaining breakpoints stopped at (but which is not guaranteed to be
4323 good for anything but further calls to bpstat_num).
4324
4325 Return 0 if passed a bpstat which does not indicate any breakpoints.
4326 Return -1 if stopped at a breakpoint that has been deleted since
4327 we set it.
4328 Return 1 otherwise. */
4329
4330 int
4331 bpstat_num (bpstat *bsp, int *num)
4332 {
4333 struct breakpoint *b;
4334
4335 if ((*bsp) == NULL)
4336 return 0; /* No more breakpoint values */
4337
4338 /* We assume we'll never have several bpstats that correspond to a
4339 single breakpoint -- otherwise, this function might return the
4340 same number more than once and this will look ugly. */
4341 b = (*bsp)->breakpoint_at;
4342 *bsp = (*bsp)->next;
4343 if (b == NULL)
4344 return -1; /* breakpoint that's been deleted since */
4345
4346 *num = b->number; /* We have its number */
4347 return 1;
4348 }
4349
4350 /* See breakpoint.h. */
4351
4352 void
4353 bpstat_clear_actions (void)
4354 {
4355 bpstat bs;
4356
4357 if (inferior_ptid == null_ptid)
4358 return;
4359
4360 thread_info *tp = inferior_thread ();
4361 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4362 {
4363 bs->commands = NULL;
4364 bs->old_val.reset (nullptr);
4365 }
4366 }
4367
4368 /* Called when a command is about to proceed the inferior. */
4369
4370 static void
4371 breakpoint_about_to_proceed (void)
4372 {
4373 if (inferior_ptid != null_ptid)
4374 {
4375 struct thread_info *tp = inferior_thread ();
4376
4377 /* Allow inferior function calls in breakpoint commands to not
4378 interrupt the command list. When the call finishes
4379 successfully, the inferior will be standing at the same
4380 breakpoint as if nothing happened. */
4381 if (tp->control.in_infcall)
4382 return;
4383 }
4384
4385 breakpoint_proceeded = 1;
4386 }
4387
4388 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4389 or its equivalent. */
4390
4391 static int
4392 command_line_is_silent (struct command_line *cmd)
4393 {
4394 return cmd && (strcmp ("silent", cmd->line) == 0);
4395 }
4396
4397 /* Execute all the commands associated with all the breakpoints at
4398 this location. Any of these commands could cause the process to
4399 proceed beyond this point, etc. We look out for such changes by
4400 checking the global "breakpoint_proceeded" after each command.
4401
4402 Returns true if a breakpoint command resumed the inferior. In that
4403 case, it is the caller's responsibility to recall it again with the
4404 bpstat of the current thread. */
4405
4406 static int
4407 bpstat_do_actions_1 (bpstat *bsp)
4408 {
4409 bpstat bs;
4410 int again = 0;
4411
4412 /* Avoid endless recursion if a `source' command is contained
4413 in bs->commands. */
4414 if (executing_breakpoint_commands)
4415 return 0;
4416
4417 scoped_restore save_executing
4418 = make_scoped_restore (&executing_breakpoint_commands, 1);
4419
4420 scoped_restore preventer = prevent_dont_repeat ();
4421
4422 /* This pointer will iterate over the list of bpstat's. */
4423 bs = *bsp;
4424
4425 breakpoint_proceeded = 0;
4426 for (; bs != NULL; bs = bs->next)
4427 {
4428 struct command_line *cmd = NULL;
4429
4430 /* Take ownership of the BSP's command tree, if it has one.
4431
4432 The command tree could legitimately contain commands like
4433 'step' and 'next', which call clear_proceed_status, which
4434 frees stop_bpstat's command tree. To make sure this doesn't
4435 free the tree we're executing out from under us, we need to
4436 take ownership of the tree ourselves. Since a given bpstat's
4437 commands are only executed once, we don't need to copy it; we
4438 can clear the pointer in the bpstat, and make sure we free
4439 the tree when we're done. */
4440 counted_command_line ccmd = bs->commands;
4441 bs->commands = NULL;
4442 if (ccmd != NULL)
4443 cmd = ccmd.get ();
4444 if (command_line_is_silent (cmd))
4445 {
4446 /* The action has been already done by bpstat_stop_status. */
4447 cmd = cmd->next;
4448 }
4449
4450 while (cmd != NULL)
4451 {
4452 execute_control_command (cmd);
4453
4454 if (breakpoint_proceeded)
4455 break;
4456 else
4457 cmd = cmd->next;
4458 }
4459
4460 if (breakpoint_proceeded)
4461 {
4462 if (current_ui->async)
4463 /* If we are in async mode, then the target might be still
4464 running, not stopped at any breakpoint, so nothing for
4465 us to do here -- just return to the event loop. */
4466 ;
4467 else
4468 /* In sync mode, when execute_control_command returns
4469 we're already standing on the next breakpoint.
4470 Breakpoint commands for that stop were not run, since
4471 execute_command does not run breakpoint commands --
4472 only command_line_handler does, but that one is not
4473 involved in execution of breakpoint commands. So, we
4474 can now execute breakpoint commands. It should be
4475 noted that making execute_command do bpstat actions is
4476 not an option -- in this case we'll have recursive
4477 invocation of bpstat for each breakpoint with a
4478 command, and can easily blow up GDB stack. Instead, we
4479 return true, which will trigger the caller to recall us
4480 with the new stop_bpstat. */
4481 again = 1;
4482 break;
4483 }
4484 }
4485 return again;
4486 }
4487
4488 /* Helper for bpstat_do_actions. Get the current thread, if there's
4489 one, is alive and has execution. Return NULL otherwise. */
4490
4491 static thread_info *
4492 get_bpstat_thread ()
4493 {
4494 if (inferior_ptid == null_ptid || !target_has_execution ())
4495 return NULL;
4496
4497 thread_info *tp = inferior_thread ();
4498 if (tp->state == THREAD_EXITED || tp->executing)
4499 return NULL;
4500 return tp;
4501 }
4502
4503 void
4504 bpstat_do_actions (void)
4505 {
4506 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4507 thread_info *tp;
4508
4509 /* Do any commands attached to breakpoint we are stopped at. */
4510 while ((tp = get_bpstat_thread ()) != NULL)
4511 {
4512 /* Since in sync mode, bpstat_do_actions may resume the
4513 inferior, and only return when it is stopped at the next
4514 breakpoint, we keep doing breakpoint actions until it returns
4515 false to indicate the inferior was not resumed. */
4516 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4517 break;
4518 }
4519
4520 cleanup_if_error.release ();
4521 }
4522
4523 /* Print out the (old or new) value associated with a watchpoint. */
4524
4525 static void
4526 watchpoint_value_print (struct value *val, struct ui_file *stream)
4527 {
4528 if (val == NULL)
4529 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4530 else
4531 {
4532 struct value_print_options opts;
4533 get_user_print_options (&opts);
4534 value_print (val, stream, &opts);
4535 }
4536 }
4537
4538 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4539 debugging multiple threads. */
4540
4541 void
4542 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4543 {
4544 if (uiout->is_mi_like_p ())
4545 return;
4546
4547 uiout->text ("\n");
4548
4549 if (show_thread_that_caused_stop ())
4550 {
4551 const char *name;
4552 struct thread_info *thr = inferior_thread ();
4553
4554 uiout->text ("Thread ");
4555 uiout->field_string ("thread-id", print_thread_id (thr));
4556
4557 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4558 if (name != NULL)
4559 {
4560 uiout->text (" \"");
4561 uiout->field_string ("name", name);
4562 uiout->text ("\"");
4563 }
4564
4565 uiout->text (" hit ");
4566 }
4567 }
4568
4569 /* Generic routine for printing messages indicating why we
4570 stopped. The behavior of this function depends on the value
4571 'print_it' in the bpstat structure. Under some circumstances we
4572 may decide not to print anything here and delegate the task to
4573 normal_stop(). */
4574
4575 static enum print_stop_action
4576 print_bp_stop_message (bpstat bs)
4577 {
4578 switch (bs->print_it)
4579 {
4580 case print_it_noop:
4581 /* Nothing should be printed for this bpstat entry. */
4582 return PRINT_UNKNOWN;
4583 break;
4584
4585 case print_it_done:
4586 /* We still want to print the frame, but we already printed the
4587 relevant messages. */
4588 return PRINT_SRC_AND_LOC;
4589 break;
4590
4591 case print_it_normal:
4592 {
4593 struct breakpoint *b = bs->breakpoint_at;
4594
4595 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4596 which has since been deleted. */
4597 if (b == NULL)
4598 return PRINT_UNKNOWN;
4599
4600 /* Normal case. Call the breakpoint's print_it method. */
4601 return b->ops->print_it (bs);
4602 }
4603 break;
4604
4605 default:
4606 internal_error (__FILE__, __LINE__,
4607 _("print_bp_stop_message: unrecognized enum value"));
4608 break;
4609 }
4610 }
4611
4612 /* A helper function that prints a shared library stopped event. */
4613
4614 static void
4615 print_solib_event (int is_catchpoint)
4616 {
4617 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4618 bool any_added = !current_program_space->added_solibs.empty ();
4619
4620 if (!is_catchpoint)
4621 {
4622 if (any_added || any_deleted)
4623 current_uiout->text (_("Stopped due to shared library event:\n"));
4624 else
4625 current_uiout->text (_("Stopped due to shared library event (no "
4626 "libraries added or removed)\n"));
4627 }
4628
4629 if (current_uiout->is_mi_like_p ())
4630 current_uiout->field_string ("reason",
4631 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4632
4633 if (any_deleted)
4634 {
4635 current_uiout->text (_(" Inferior unloaded "));
4636 ui_out_emit_list list_emitter (current_uiout, "removed");
4637 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4638 {
4639 const std::string &name = current_program_space->deleted_solibs[ix];
4640
4641 if (ix > 0)
4642 current_uiout->text (" ");
4643 current_uiout->field_string ("library", name);
4644 current_uiout->text ("\n");
4645 }
4646 }
4647
4648 if (any_added)
4649 {
4650 current_uiout->text (_(" Inferior loaded "));
4651 ui_out_emit_list list_emitter (current_uiout, "added");
4652 bool first = true;
4653 for (so_list *iter : current_program_space->added_solibs)
4654 {
4655 if (!first)
4656 current_uiout->text (" ");
4657 first = false;
4658 current_uiout->field_string ("library", iter->so_name);
4659 current_uiout->text ("\n");
4660 }
4661 }
4662 }
4663
4664 /* Print a message indicating what happened. This is called from
4665 normal_stop(). The input to this routine is the head of the bpstat
4666 list - a list of the eventpoints that caused this stop. KIND is
4667 the target_waitkind for the stopping event. This
4668 routine calls the generic print routine for printing a message
4669 about reasons for stopping. This will print (for example) the
4670 "Breakpoint n," part of the output. The return value of this
4671 routine is one of:
4672
4673 PRINT_UNKNOWN: Means we printed nothing.
4674 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4675 code to print the location. An example is
4676 "Breakpoint 1, " which should be followed by
4677 the location.
4678 PRINT_SRC_ONLY: Means we printed something, but there is no need
4679 to also print the location part of the message.
4680 An example is the catch/throw messages, which
4681 don't require a location appended to the end.
4682 PRINT_NOTHING: We have done some printing and we don't need any
4683 further info to be printed. */
4684
4685 enum print_stop_action
4686 bpstat_print (bpstat bs, int kind)
4687 {
4688 enum print_stop_action val;
4689
4690 /* Maybe another breakpoint in the chain caused us to stop.
4691 (Currently all watchpoints go on the bpstat whether hit or not.
4692 That probably could (should) be changed, provided care is taken
4693 with respect to bpstat_explains_signal). */
4694 for (; bs; bs = bs->next)
4695 {
4696 val = print_bp_stop_message (bs);
4697 if (val == PRINT_SRC_ONLY
4698 || val == PRINT_SRC_AND_LOC
4699 || val == PRINT_NOTHING)
4700 return val;
4701 }
4702
4703 /* If we had hit a shared library event breakpoint,
4704 print_bp_stop_message would print out this message. If we hit an
4705 OS-level shared library event, do the same thing. */
4706 if (kind == TARGET_WAITKIND_LOADED)
4707 {
4708 print_solib_event (0);
4709 return PRINT_NOTHING;
4710 }
4711
4712 /* We reached the end of the chain, or we got a null BS to start
4713 with and nothing was printed. */
4714 return PRINT_UNKNOWN;
4715 }
4716
4717 /* Evaluate the boolean expression EXP and return the result. */
4718
4719 static bool
4720 breakpoint_cond_eval (expression *exp)
4721 {
4722 struct value *mark = value_mark ();
4723 bool res = value_true (evaluate_expression (exp));
4724
4725 value_free_to_mark (mark);
4726 return res;
4727 }
4728
4729 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4730
4731 bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4732 : next (NULL),
4733 bp_location_at (bl),
4734 breakpoint_at (bl->owner),
4735 commands (NULL),
4736 print (0),
4737 stop (0),
4738 print_it (print_it_normal)
4739 {
4740 incref_bp_location (bl);
4741 **bs_link_pointer = this;
4742 *bs_link_pointer = &next;
4743 }
4744
4745 bpstats::bpstats ()
4746 : next (NULL),
4747 bp_location_at (NULL),
4748 breakpoint_at (NULL),
4749 commands (NULL),
4750 print (0),
4751 stop (0),
4752 print_it (print_it_normal)
4753 {
4754 }
4755 \f
4756 /* The target has stopped with waitstatus WS. Check if any hardware
4757 watchpoints have triggered, according to the target. */
4758
4759 int
4760 watchpoints_triggered (struct target_waitstatus *ws)
4761 {
4762 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
4763 CORE_ADDR addr;
4764 struct breakpoint *b;
4765
4766 if (!stopped_by_watchpoint)
4767 {
4768 /* We were not stopped by a watchpoint. Mark all watchpoints
4769 as not triggered. */
4770 ALL_BREAKPOINTS (b)
4771 if (is_hardware_watchpoint (b))
4772 {
4773 struct watchpoint *w = (struct watchpoint *) b;
4774
4775 w->watchpoint_triggered = watch_triggered_no;
4776 }
4777
4778 return 0;
4779 }
4780
4781 if (!target_stopped_data_address (current_top_target (), &addr))
4782 {
4783 /* We were stopped by a watchpoint, but we don't know where.
4784 Mark all watchpoints as unknown. */
4785 ALL_BREAKPOINTS (b)
4786 if (is_hardware_watchpoint (b))
4787 {
4788 struct watchpoint *w = (struct watchpoint *) b;
4789
4790 w->watchpoint_triggered = watch_triggered_unknown;
4791 }
4792
4793 return 1;
4794 }
4795
4796 /* The target could report the data address. Mark watchpoints
4797 affected by this data address as triggered, and all others as not
4798 triggered. */
4799
4800 ALL_BREAKPOINTS (b)
4801 if (is_hardware_watchpoint (b))
4802 {
4803 struct watchpoint *w = (struct watchpoint *) b;
4804 struct bp_location *loc;
4805
4806 w->watchpoint_triggered = watch_triggered_no;
4807 for (loc = b->loc; loc; loc = loc->next)
4808 {
4809 if (is_masked_watchpoint (b))
4810 {
4811 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4812 CORE_ADDR start = loc->address & w->hw_wp_mask;
4813
4814 if (newaddr == start)
4815 {
4816 w->watchpoint_triggered = watch_triggered_yes;
4817 break;
4818 }
4819 }
4820 /* Exact match not required. Within range is sufficient. */
4821 else if (target_watchpoint_addr_within_range (current_top_target (),
4822 addr, loc->address,
4823 loc->length))
4824 {
4825 w->watchpoint_triggered = watch_triggered_yes;
4826 break;
4827 }
4828 }
4829 }
4830
4831 return 1;
4832 }
4833
4834 /* Possible return values for watchpoint_check. */
4835 enum wp_check_result
4836 {
4837 /* The watchpoint has been deleted. */
4838 WP_DELETED = 1,
4839
4840 /* The value has changed. */
4841 WP_VALUE_CHANGED = 2,
4842
4843 /* The value has not changed. */
4844 WP_VALUE_NOT_CHANGED = 3,
4845
4846 /* Ignore this watchpoint, no matter if the value changed or not. */
4847 WP_IGNORE = 4,
4848 };
4849
4850 #define BP_TEMPFLAG 1
4851 #define BP_HARDWAREFLAG 2
4852
4853 /* Evaluate watchpoint condition expression and check if its value
4854 changed. */
4855
4856 static wp_check_result
4857 watchpoint_check (bpstat bs)
4858 {
4859 struct watchpoint *b;
4860 struct frame_info *fr;
4861 int within_current_scope;
4862
4863 /* BS is built from an existing struct breakpoint. */
4864 gdb_assert (bs->breakpoint_at != NULL);
4865 b = (struct watchpoint *) bs->breakpoint_at;
4866
4867 /* If this is a local watchpoint, we only want to check if the
4868 watchpoint frame is in scope if the current thread is the thread
4869 that was used to create the watchpoint. */
4870 if (!watchpoint_in_thread_scope (b))
4871 return WP_IGNORE;
4872
4873 if (b->exp_valid_block == NULL)
4874 within_current_scope = 1;
4875 else
4876 {
4877 struct frame_info *frame = get_current_frame ();
4878 struct gdbarch *frame_arch = get_frame_arch (frame);
4879 CORE_ADDR frame_pc = get_frame_pc (frame);
4880
4881 /* stack_frame_destroyed_p() returns a non-zero value if we're
4882 still in the function but the stack frame has already been
4883 invalidated. Since we can't rely on the values of local
4884 variables after the stack has been destroyed, we are treating
4885 the watchpoint in that state as `not changed' without further
4886 checking. Don't mark watchpoints as changed if the current
4887 frame is in an epilogue - even if they are in some other
4888 frame, our view of the stack is likely to be wrong and
4889 frame_find_by_id could error out. */
4890 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
4891 return WP_IGNORE;
4892
4893 fr = frame_find_by_id (b->watchpoint_frame);
4894 within_current_scope = (fr != NULL);
4895
4896 /* If we've gotten confused in the unwinder, we might have
4897 returned a frame that can't describe this variable. */
4898 if (within_current_scope)
4899 {
4900 struct symbol *function;
4901
4902 function = get_frame_function (fr);
4903 if (function == NULL
4904 || !contained_in (b->exp_valid_block,
4905 SYMBOL_BLOCK_VALUE (function)))
4906 within_current_scope = 0;
4907 }
4908
4909 if (within_current_scope)
4910 /* If we end up stopping, the current frame will get selected
4911 in normal_stop. So this call to select_frame won't affect
4912 the user. */
4913 select_frame (fr);
4914 }
4915
4916 if (within_current_scope)
4917 {
4918 /* We use value_{,free_to_}mark because it could be a *long*
4919 time before we return to the command level and call
4920 free_all_values. We can't call free_all_values because we
4921 might be in the middle of evaluating a function call. */
4922
4923 int pc = 0;
4924 struct value *mark;
4925 struct value *new_val;
4926
4927 if (is_masked_watchpoint (b))
4928 /* Since we don't know the exact trigger address (from
4929 stopped_data_address), just tell the user we've triggered
4930 a mask watchpoint. */
4931 return WP_VALUE_CHANGED;
4932
4933 mark = value_mark ();
4934 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
4935
4936 if (b->val_bitsize != 0)
4937 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4938
4939 /* We use value_equal_contents instead of value_equal because
4940 the latter coerces an array to a pointer, thus comparing just
4941 the address of the array instead of its contents. This is
4942 not what we want. */
4943 if ((b->val != NULL) != (new_val != NULL)
4944 || (b->val != NULL && !value_equal_contents (b->val.get (),
4945 new_val)))
4946 {
4947 bs->old_val = b->val;
4948 b->val = release_value (new_val);
4949 b->val_valid = true;
4950 if (new_val != NULL)
4951 value_free_to_mark (mark);
4952 return WP_VALUE_CHANGED;
4953 }
4954 else
4955 {
4956 /* Nothing changed. */
4957 value_free_to_mark (mark);
4958 return WP_VALUE_NOT_CHANGED;
4959 }
4960 }
4961 else
4962 {
4963 /* This seems like the only logical thing to do because
4964 if we temporarily ignored the watchpoint, then when
4965 we reenter the block in which it is valid it contains
4966 garbage (in the case of a function, it may have two
4967 garbage values, one before and one after the prologue).
4968 So we can't even detect the first assignment to it and
4969 watch after that (since the garbage may or may not equal
4970 the first value assigned). */
4971 /* We print all the stop information in
4972 breakpoint_ops->print_it, but in this case, by the time we
4973 call breakpoint_ops->print_it this bp will be deleted
4974 already. So we have no choice but print the information
4975 here. */
4976
4977 SWITCH_THRU_ALL_UIS ()
4978 {
4979 struct ui_out *uiout = current_uiout;
4980
4981 if (uiout->is_mi_like_p ())
4982 uiout->field_string
4983 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
4984 uiout->message ("\nWatchpoint %pF deleted because the program has "
4985 "left the block in\n"
4986 "which its expression is valid.\n",
4987 signed_field ("wpnum", b->number));
4988 }
4989
4990 /* Make sure the watchpoint's commands aren't executed. */
4991 b->commands = NULL;
4992 watchpoint_del_at_next_stop (b);
4993
4994 return WP_DELETED;
4995 }
4996 }
4997
4998 /* Return true if it looks like target has stopped due to hitting
4999 breakpoint location BL. This function does not check if we should
5000 stop, only if BL explains the stop. */
5001
5002 static int
5003 bpstat_check_location (const struct bp_location *bl,
5004 const address_space *aspace, CORE_ADDR bp_addr,
5005 const struct target_waitstatus *ws)
5006 {
5007 struct breakpoint *b = bl->owner;
5008
5009 /* BL is from an existing breakpoint. */
5010 gdb_assert (b != NULL);
5011
5012 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5013 }
5014
5015 /* Determine if the watched values have actually changed, and we
5016 should stop. If not, set BS->stop to 0. */
5017
5018 static void
5019 bpstat_check_watchpoint (bpstat bs)
5020 {
5021 const struct bp_location *bl;
5022 struct watchpoint *b;
5023
5024 /* BS is built for existing struct breakpoint. */
5025 bl = bs->bp_location_at;
5026 gdb_assert (bl != NULL);
5027 b = (struct watchpoint *) bs->breakpoint_at;
5028 gdb_assert (b != NULL);
5029
5030 {
5031 int must_check_value = 0;
5032
5033 if (b->type == bp_watchpoint)
5034 /* For a software watchpoint, we must always check the
5035 watched value. */
5036 must_check_value = 1;
5037 else if (b->watchpoint_triggered == watch_triggered_yes)
5038 /* We have a hardware watchpoint (read, write, or access)
5039 and the target earlier reported an address watched by
5040 this watchpoint. */
5041 must_check_value = 1;
5042 else if (b->watchpoint_triggered == watch_triggered_unknown
5043 && b->type == bp_hardware_watchpoint)
5044 /* We were stopped by a hardware watchpoint, but the target could
5045 not report the data address. We must check the watchpoint's
5046 value. Access and read watchpoints are out of luck; without
5047 a data address, we can't figure it out. */
5048 must_check_value = 1;
5049
5050 if (must_check_value)
5051 {
5052 wp_check_result e;
5053
5054 try
5055 {
5056 e = watchpoint_check (bs);
5057 }
5058 catch (const gdb_exception &ex)
5059 {
5060 exception_fprintf (gdb_stderr, ex,
5061 "Error evaluating expression "
5062 "for watchpoint %d\n",
5063 b->number);
5064
5065 SWITCH_THRU_ALL_UIS ()
5066 {
5067 printf_filtered (_("Watchpoint %d deleted.\n"),
5068 b->number);
5069 }
5070 watchpoint_del_at_next_stop (b);
5071 e = WP_DELETED;
5072 }
5073
5074 switch (e)
5075 {
5076 case WP_DELETED:
5077 /* We've already printed what needs to be printed. */
5078 bs->print_it = print_it_done;
5079 /* Stop. */
5080 break;
5081 case WP_IGNORE:
5082 bs->print_it = print_it_noop;
5083 bs->stop = 0;
5084 break;
5085 case WP_VALUE_CHANGED:
5086 if (b->type == bp_read_watchpoint)
5087 {
5088 /* There are two cases to consider here:
5089
5090 1. We're watching the triggered memory for reads.
5091 In that case, trust the target, and always report
5092 the watchpoint hit to the user. Even though
5093 reads don't cause value changes, the value may
5094 have changed since the last time it was read, and
5095 since we're not trapping writes, we will not see
5096 those, and as such we should ignore our notion of
5097 old value.
5098
5099 2. We're watching the triggered memory for both
5100 reads and writes. There are two ways this may
5101 happen:
5102
5103 2.1. This is a target that can't break on data
5104 reads only, but can break on accesses (reads or
5105 writes), such as e.g., x86. We detect this case
5106 at the time we try to insert read watchpoints.
5107
5108 2.2. Otherwise, the target supports read
5109 watchpoints, but, the user set an access or write
5110 watchpoint watching the same memory as this read
5111 watchpoint.
5112
5113 If we're watching memory writes as well as reads,
5114 ignore watchpoint hits when we find that the
5115 value hasn't changed, as reads don't cause
5116 changes. This still gives false positives when
5117 the program writes the same value to memory as
5118 what there was already in memory (we will confuse
5119 it for a read), but it's much better than
5120 nothing. */
5121
5122 int other_write_watchpoint = 0;
5123
5124 if (bl->watchpoint_type == hw_read)
5125 {
5126 struct breakpoint *other_b;
5127
5128 ALL_BREAKPOINTS (other_b)
5129 if (other_b->type == bp_hardware_watchpoint
5130 || other_b->type == bp_access_watchpoint)
5131 {
5132 struct watchpoint *other_w =
5133 (struct watchpoint *) other_b;
5134
5135 if (other_w->watchpoint_triggered
5136 == watch_triggered_yes)
5137 {
5138 other_write_watchpoint = 1;
5139 break;
5140 }
5141 }
5142 }
5143
5144 if (other_write_watchpoint
5145 || bl->watchpoint_type == hw_access)
5146 {
5147 /* We're watching the same memory for writes,
5148 and the value changed since the last time we
5149 updated it, so this trap must be for a write.
5150 Ignore it. */
5151 bs->print_it = print_it_noop;
5152 bs->stop = 0;
5153 }
5154 }
5155 break;
5156 case WP_VALUE_NOT_CHANGED:
5157 if (b->type == bp_hardware_watchpoint
5158 || b->type == bp_watchpoint)
5159 {
5160 /* Don't stop: write watchpoints shouldn't fire if
5161 the value hasn't changed. */
5162 bs->print_it = print_it_noop;
5163 bs->stop = 0;
5164 }
5165 /* Stop. */
5166 break;
5167 default:
5168 /* Can't happen. */
5169 break;
5170 }
5171 }
5172 else /* must_check_value == 0 */
5173 {
5174 /* This is a case where some watchpoint(s) triggered, but
5175 not at the address of this watchpoint, or else no
5176 watchpoint triggered after all. So don't print
5177 anything for this watchpoint. */
5178 bs->print_it = print_it_noop;
5179 bs->stop = 0;
5180 }
5181 }
5182 }
5183
5184 /* For breakpoints that are currently marked as telling gdb to stop,
5185 check conditions (condition proper, frame, thread and ignore count)
5186 of breakpoint referred to by BS. If we should not stop for this
5187 breakpoint, set BS->stop to 0. */
5188
5189 static void
5190 bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
5191 {
5192 const struct bp_location *bl;
5193 struct breakpoint *b;
5194 /* Assume stop. */
5195 bool condition_result = true;
5196 struct expression *cond;
5197
5198 gdb_assert (bs->stop);
5199
5200 /* BS is built for existing struct breakpoint. */
5201 bl = bs->bp_location_at;
5202 gdb_assert (bl != NULL);
5203 b = bs->breakpoint_at;
5204 gdb_assert (b != NULL);
5205
5206 /* Even if the target evaluated the condition on its end and notified GDB, we
5207 need to do so again since GDB does not know if we stopped due to a
5208 breakpoint or a single step breakpoint. */
5209
5210 if (frame_id_p (b->frame_id)
5211 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5212 {
5213 bs->stop = 0;
5214 return;
5215 }
5216
5217 /* If this is a thread/task-specific breakpoint, don't waste cpu
5218 evaluating the condition if this isn't the specified
5219 thread/task. */
5220 if ((b->thread != -1 && b->thread != thread->global_num)
5221 || (b->task != 0 && b->task != ada_get_task_number (thread)))
5222 {
5223 bs->stop = 0;
5224 return;
5225 }
5226
5227 /* Evaluate extension language breakpoints that have a "stop" method
5228 implemented. */
5229 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5230
5231 if (is_watchpoint (b))
5232 {
5233 struct watchpoint *w = (struct watchpoint *) b;
5234
5235 cond = w->cond_exp.get ();
5236 }
5237 else
5238 cond = bl->cond.get ();
5239
5240 if (cond && b->disposition != disp_del_at_next_stop)
5241 {
5242 int within_current_scope = 1;
5243 struct watchpoint * w;
5244
5245 /* We use value_mark and value_free_to_mark because it could
5246 be a long time before we return to the command level and
5247 call free_all_values. We can't call free_all_values
5248 because we might be in the middle of evaluating a
5249 function call. */
5250 struct value *mark = value_mark ();
5251
5252 if (is_watchpoint (b))
5253 w = (struct watchpoint *) b;
5254 else
5255 w = NULL;
5256
5257 /* Need to select the frame, with all that implies so that
5258 the conditions will have the right context. Because we
5259 use the frame, we will not see an inlined function's
5260 variables when we arrive at a breakpoint at the start
5261 of the inlined function; the current frame will be the
5262 call site. */
5263 if (w == NULL || w->cond_exp_valid_block == NULL)
5264 select_frame (get_current_frame ());
5265 else
5266 {
5267 struct frame_info *frame;
5268
5269 /* For local watchpoint expressions, which particular
5270 instance of a local is being watched matters, so we
5271 keep track of the frame to evaluate the expression
5272 in. To evaluate the condition however, it doesn't
5273 really matter which instantiation of the function
5274 where the condition makes sense triggers the
5275 watchpoint. This allows an expression like "watch
5276 global if q > 10" set in `func', catch writes to
5277 global on all threads that call `func', or catch
5278 writes on all recursive calls of `func' by a single
5279 thread. We simply always evaluate the condition in
5280 the innermost frame that's executing where it makes
5281 sense to evaluate the condition. It seems
5282 intuitive. */
5283 frame = block_innermost_frame (w->cond_exp_valid_block);
5284 if (frame != NULL)
5285 select_frame (frame);
5286 else
5287 within_current_scope = 0;
5288 }
5289 if (within_current_scope)
5290 {
5291 try
5292 {
5293 condition_result = breakpoint_cond_eval (cond);
5294 }
5295 catch (const gdb_exception &ex)
5296 {
5297 exception_fprintf (gdb_stderr, ex,
5298 "Error in testing breakpoint condition:\n");
5299 }
5300 }
5301 else
5302 {
5303 warning (_("Watchpoint condition cannot be tested "
5304 "in the current scope"));
5305 /* If we failed to set the right context for this
5306 watchpoint, unconditionally report it. */
5307 }
5308 /* FIXME-someday, should give breakpoint #. */
5309 value_free_to_mark (mark);
5310 }
5311
5312 if (cond && !condition_result)
5313 {
5314 bs->stop = 0;
5315 }
5316 else if (b->ignore_count > 0)
5317 {
5318 b->ignore_count--;
5319 bs->stop = 0;
5320 /* Increase the hit count even though we don't stop. */
5321 ++(b->hit_count);
5322 gdb::observers::breakpoint_modified.notify (b);
5323 }
5324 }
5325
5326 /* Returns true if we need to track moribund locations of LOC's type
5327 on the current target. */
5328
5329 static int
5330 need_moribund_for_location_type (struct bp_location *loc)
5331 {
5332 return ((loc->loc_type == bp_loc_software_breakpoint
5333 && !target_supports_stopped_by_sw_breakpoint ())
5334 || (loc->loc_type == bp_loc_hardware_breakpoint
5335 && !target_supports_stopped_by_hw_breakpoint ()));
5336 }
5337
5338 /* See breakpoint.h. */
5339
5340 bpstat
5341 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5342 const struct target_waitstatus *ws)
5343 {
5344 struct breakpoint *b;
5345 bpstat bs_head = NULL, *bs_link = &bs_head;
5346
5347 ALL_BREAKPOINTS (b)
5348 {
5349 if (!breakpoint_enabled (b))
5350 continue;
5351
5352 for (bp_location *bl = b->loc; bl != NULL; bl = bl->next)
5353 {
5354 /* For hardware watchpoints, we look only at the first
5355 location. The watchpoint_check function will work on the
5356 entire expression, not the individual locations. For
5357 read watchpoints, the watchpoints_triggered function has
5358 checked all locations already. */
5359 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5360 break;
5361
5362 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
5363 continue;
5364
5365 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5366 continue;
5367
5368 /* Come here if it's a watchpoint, or if the break address
5369 matches. */
5370
5371 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5372 explain stop. */
5373
5374 /* Assume we stop. Should we find a watchpoint that is not
5375 actually triggered, or if the condition of the breakpoint
5376 evaluates as false, we'll reset 'stop' to 0. */
5377 bs->stop = 1;
5378 bs->print = 1;
5379
5380 /* If this is a scope breakpoint, mark the associated
5381 watchpoint as triggered so that we will handle the
5382 out-of-scope event. We'll get to the watchpoint next
5383 iteration. */
5384 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5385 {
5386 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5387
5388 w->watchpoint_triggered = watch_triggered_yes;
5389 }
5390 }
5391 }
5392
5393 /* Check if a moribund breakpoint explains the stop. */
5394 if (!target_supports_stopped_by_sw_breakpoint ()
5395 || !target_supports_stopped_by_hw_breakpoint ())
5396 {
5397 for (bp_location *loc : moribund_locations)
5398 {
5399 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5400 && need_moribund_for_location_type (loc))
5401 {
5402 bpstat bs = new bpstats (loc, &bs_link);
5403 /* For hits of moribund locations, we should just proceed. */
5404 bs->stop = 0;
5405 bs->print = 0;
5406 bs->print_it = print_it_noop;
5407 }
5408 }
5409 }
5410
5411 return bs_head;
5412 }
5413
5414 /* See breakpoint.h. */
5415
5416 bpstat
5417 bpstat_stop_status (const address_space *aspace,
5418 CORE_ADDR bp_addr, thread_info *thread,
5419 const struct target_waitstatus *ws,
5420 bpstat stop_chain)
5421 {
5422 struct breakpoint *b = NULL;
5423 /* First item of allocated bpstat's. */
5424 bpstat bs_head = stop_chain;
5425 bpstat bs;
5426 int need_remove_insert;
5427 int removed_any;
5428
5429 /* First, build the bpstat chain with locations that explain a
5430 target stop, while being careful to not set the target running,
5431 as that may invalidate locations (in particular watchpoint
5432 locations are recreated). Resuming will happen here with
5433 breakpoint conditions or watchpoint expressions that include
5434 inferior function calls. */
5435 if (bs_head == NULL)
5436 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5437
5438 /* A bit of special processing for shlib breakpoints. We need to
5439 process solib loading here, so that the lists of loaded and
5440 unloaded libraries are correct before we handle "catch load" and
5441 "catch unload". */
5442 for (bs = bs_head; bs != NULL; bs = bs->next)
5443 {
5444 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5445 {
5446 handle_solib_event ();
5447 break;
5448 }
5449 }
5450
5451 /* Now go through the locations that caused the target to stop, and
5452 check whether we're interested in reporting this stop to higher
5453 layers, or whether we should resume the target transparently. */
5454
5455 removed_any = 0;
5456
5457 for (bs = bs_head; bs != NULL; bs = bs->next)
5458 {
5459 if (!bs->stop)
5460 continue;
5461
5462 b = bs->breakpoint_at;
5463 b->ops->check_status (bs);
5464 if (bs->stop)
5465 {
5466 bpstat_check_breakpoint_conditions (bs, thread);
5467
5468 if (bs->stop)
5469 {
5470 ++(b->hit_count);
5471 gdb::observers::breakpoint_modified.notify (b);
5472
5473 /* We will stop here. */
5474 if (b->disposition == disp_disable)
5475 {
5476 --(b->enable_count);
5477 if (b->enable_count <= 0)
5478 b->enable_state = bp_disabled;
5479 removed_any = 1;
5480 }
5481 if (b->silent)
5482 bs->print = 0;
5483 bs->commands = b->commands;
5484 if (command_line_is_silent (bs->commands
5485 ? bs->commands.get () : NULL))
5486 bs->print = 0;
5487
5488 b->ops->after_condition_true (bs);
5489 }
5490
5491 }
5492
5493 /* Print nothing for this entry if we don't stop or don't
5494 print. */
5495 if (!bs->stop || !bs->print)
5496 bs->print_it = print_it_noop;
5497 }
5498
5499 /* If we aren't stopping, the value of some hardware watchpoint may
5500 not have changed, but the intermediate memory locations we are
5501 watching may have. Don't bother if we're stopping; this will get
5502 done later. */
5503 need_remove_insert = 0;
5504 if (! bpstat_causes_stop (bs_head))
5505 for (bs = bs_head; bs != NULL; bs = bs->next)
5506 if (!bs->stop
5507 && bs->breakpoint_at
5508 && is_hardware_watchpoint (bs->breakpoint_at))
5509 {
5510 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5511
5512 update_watchpoint (w, 0 /* don't reparse. */);
5513 need_remove_insert = 1;
5514 }
5515
5516 if (need_remove_insert)
5517 update_global_location_list (UGLL_MAY_INSERT);
5518 else if (removed_any)
5519 update_global_location_list (UGLL_DONT_INSERT);
5520
5521 return bs_head;
5522 }
5523
5524 static void
5525 handle_jit_event (CORE_ADDR address)
5526 {
5527 struct gdbarch *gdbarch;
5528
5529 infrun_debug_printf ("handling bp_jit_event");
5530
5531 /* Switch terminal for any messages produced by
5532 breakpoint_re_set. */
5533 target_terminal::ours_for_output ();
5534
5535 gdbarch = get_frame_arch (get_current_frame ());
5536 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5537 thus it is expected that its objectfile can be found through
5538 minimal symbol lookup. If it doesn't work (and assert fails), it
5539 most likely means that `jit_breakpoint_re_set` was changes and this
5540 function needs to be updated too. */
5541 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5542 gdb_assert (jit_bp_sym.objfile != nullptr);
5543 jit_event_handler (gdbarch, jit_bp_sym.objfile);
5544
5545 target_terminal::inferior ();
5546 }
5547
5548 /* Prepare WHAT final decision for infrun. */
5549
5550 /* Decide what infrun needs to do with this bpstat. */
5551
5552 struct bpstat_what
5553 bpstat_what (bpstat bs_head)
5554 {
5555 struct bpstat_what retval;
5556 bpstat bs;
5557
5558 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5559 retval.call_dummy = STOP_NONE;
5560 retval.is_longjmp = false;
5561
5562 for (bs = bs_head; bs != NULL; bs = bs->next)
5563 {
5564 /* Extract this BS's action. After processing each BS, we check
5565 if its action overrides all we've seem so far. */
5566 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5567 enum bptype bptype;
5568
5569 if (bs->breakpoint_at == NULL)
5570 {
5571 /* I suspect this can happen if it was a momentary
5572 breakpoint which has since been deleted. */
5573 bptype = bp_none;
5574 }
5575 else
5576 bptype = bs->breakpoint_at->type;
5577
5578 switch (bptype)
5579 {
5580 case bp_none:
5581 break;
5582 case bp_breakpoint:
5583 case bp_hardware_breakpoint:
5584 case bp_single_step:
5585 case bp_until:
5586 case bp_finish:
5587 case bp_shlib_event:
5588 if (bs->stop)
5589 {
5590 if (bs->print)
5591 this_action = BPSTAT_WHAT_STOP_NOISY;
5592 else
5593 this_action = BPSTAT_WHAT_STOP_SILENT;
5594 }
5595 else
5596 this_action = BPSTAT_WHAT_SINGLE;
5597 break;
5598 case bp_watchpoint:
5599 case bp_hardware_watchpoint:
5600 case bp_read_watchpoint:
5601 case bp_access_watchpoint:
5602 if (bs->stop)
5603 {
5604 if (bs->print)
5605 this_action = BPSTAT_WHAT_STOP_NOISY;
5606 else
5607 this_action = BPSTAT_WHAT_STOP_SILENT;
5608 }
5609 else
5610 {
5611 /* There was a watchpoint, but we're not stopping.
5612 This requires no further action. */
5613 }
5614 break;
5615 case bp_longjmp:
5616 case bp_longjmp_call_dummy:
5617 case bp_exception:
5618 if (bs->stop)
5619 {
5620 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5621 retval.is_longjmp = bptype != bp_exception;
5622 }
5623 else
5624 this_action = BPSTAT_WHAT_SINGLE;
5625 break;
5626 case bp_longjmp_resume:
5627 case bp_exception_resume:
5628 if (bs->stop)
5629 {
5630 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5631 retval.is_longjmp = bptype == bp_longjmp_resume;
5632 }
5633 else
5634 this_action = BPSTAT_WHAT_SINGLE;
5635 break;
5636 case bp_step_resume:
5637 if (bs->stop)
5638 this_action = BPSTAT_WHAT_STEP_RESUME;
5639 else
5640 {
5641 /* It is for the wrong frame. */
5642 this_action = BPSTAT_WHAT_SINGLE;
5643 }
5644 break;
5645 case bp_hp_step_resume:
5646 if (bs->stop)
5647 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5648 else
5649 {
5650 /* It is for the wrong frame. */
5651 this_action = BPSTAT_WHAT_SINGLE;
5652 }
5653 break;
5654 case bp_watchpoint_scope:
5655 case bp_thread_event:
5656 case bp_overlay_event:
5657 case bp_longjmp_master:
5658 case bp_std_terminate_master:
5659 case bp_exception_master:
5660 this_action = BPSTAT_WHAT_SINGLE;
5661 break;
5662 case bp_catchpoint:
5663 if (bs->stop)
5664 {
5665 if (bs->print)
5666 this_action = BPSTAT_WHAT_STOP_NOISY;
5667 else
5668 this_action = BPSTAT_WHAT_STOP_SILENT;
5669 }
5670 else
5671 {
5672 /* Some catchpoints are implemented with breakpoints.
5673 For those, we need to step over the breakpoint. */
5674 if (bs->bp_location_at->loc_type != bp_loc_other)
5675 this_action = BPSTAT_WHAT_SINGLE;
5676 }
5677 break;
5678 case bp_jit_event:
5679 this_action = BPSTAT_WHAT_SINGLE;
5680 break;
5681 case bp_call_dummy:
5682 /* Make sure the action is stop (silent or noisy),
5683 so infrun.c pops the dummy frame. */
5684 retval.call_dummy = STOP_STACK_DUMMY;
5685 this_action = BPSTAT_WHAT_STOP_SILENT;
5686 break;
5687 case bp_std_terminate:
5688 /* Make sure the action is stop (silent or noisy),
5689 so infrun.c pops the dummy frame. */
5690 retval.call_dummy = STOP_STD_TERMINATE;
5691 this_action = BPSTAT_WHAT_STOP_SILENT;
5692 break;
5693 case bp_tracepoint:
5694 case bp_fast_tracepoint:
5695 case bp_static_tracepoint:
5696 /* Tracepoint hits should not be reported back to GDB, and
5697 if one got through somehow, it should have been filtered
5698 out already. */
5699 internal_error (__FILE__, __LINE__,
5700 _("bpstat_what: tracepoint encountered"));
5701 break;
5702 case bp_gnu_ifunc_resolver:
5703 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5704 this_action = BPSTAT_WHAT_SINGLE;
5705 break;
5706 case bp_gnu_ifunc_resolver_return:
5707 /* The breakpoint will be removed, execution will restart from the
5708 PC of the former breakpoint. */
5709 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5710 break;
5711
5712 case bp_dprintf:
5713 if (bs->stop)
5714 this_action = BPSTAT_WHAT_STOP_SILENT;
5715 else
5716 this_action = BPSTAT_WHAT_SINGLE;
5717 break;
5718
5719 default:
5720 internal_error (__FILE__, __LINE__,
5721 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5722 }
5723
5724 retval.main_action = std::max (retval.main_action, this_action);
5725 }
5726
5727 return retval;
5728 }
5729
5730 void
5731 bpstat_run_callbacks (bpstat bs_head)
5732 {
5733 bpstat bs;
5734
5735 for (bs = bs_head; bs != NULL; bs = bs->next)
5736 {
5737 struct breakpoint *b = bs->breakpoint_at;
5738
5739 if (b == NULL)
5740 continue;
5741 switch (b->type)
5742 {
5743 case bp_jit_event:
5744 handle_jit_event (bs->bp_location_at->address);
5745 break;
5746 case bp_gnu_ifunc_resolver:
5747 gnu_ifunc_resolver_stop (b);
5748 break;
5749 case bp_gnu_ifunc_resolver_return:
5750 gnu_ifunc_resolver_return_stop (b);
5751 break;
5752 }
5753 }
5754 }
5755
5756 /* See breakpoint.h. */
5757
5758 bool
5759 bpstat_should_step ()
5760 {
5761 struct breakpoint *b;
5762
5763 ALL_BREAKPOINTS (b)
5764 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5765 return true;
5766 return false;
5767 }
5768
5769 /* See breakpoint.h. */
5770
5771 bool
5772 bpstat_causes_stop (bpstat bs)
5773 {
5774 for (; bs != NULL; bs = bs->next)
5775 if (bs->stop)
5776 return true;
5777
5778 return false;
5779 }
5780
5781 \f
5782
5783 /* Compute a string of spaces suitable to indent the next line
5784 so it starts at the position corresponding to the table column
5785 named COL_NAME in the currently active table of UIOUT. */
5786
5787 static char *
5788 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5789 {
5790 static char wrap_indent[80];
5791 int i, total_width, width, align;
5792 const char *text;
5793
5794 total_width = 0;
5795 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5796 {
5797 if (strcmp (text, col_name) == 0)
5798 {
5799 gdb_assert (total_width < sizeof wrap_indent);
5800 memset (wrap_indent, ' ', total_width);
5801 wrap_indent[total_width] = 0;
5802
5803 return wrap_indent;
5804 }
5805
5806 total_width += width + 1;
5807 }
5808
5809 return NULL;
5810 }
5811
5812 /* Determine if the locations of this breakpoint will have their conditions
5813 evaluated by the target, host or a mix of both. Returns the following:
5814
5815 "host": Host evals condition.
5816 "host or target": Host or Target evals condition.
5817 "target": Target evals condition.
5818 */
5819
5820 static const char *
5821 bp_condition_evaluator (struct breakpoint *b)
5822 {
5823 struct bp_location *bl;
5824 char host_evals = 0;
5825 char target_evals = 0;
5826
5827 if (!b)
5828 return NULL;
5829
5830 if (!is_breakpoint (b))
5831 return NULL;
5832
5833 if (gdb_evaluates_breakpoint_condition_p ()
5834 || !target_supports_evaluation_of_breakpoint_conditions ())
5835 return condition_evaluation_host;
5836
5837 for (bl = b->loc; bl; bl = bl->next)
5838 {
5839 if (bl->cond_bytecode)
5840 target_evals++;
5841 else
5842 host_evals++;
5843 }
5844
5845 if (host_evals && target_evals)
5846 return condition_evaluation_both;
5847 else if (target_evals)
5848 return condition_evaluation_target;
5849 else
5850 return condition_evaluation_host;
5851 }
5852
5853 /* Determine the breakpoint location's condition evaluator. This is
5854 similar to bp_condition_evaluator, but for locations. */
5855
5856 static const char *
5857 bp_location_condition_evaluator (struct bp_location *bl)
5858 {
5859 if (bl && !is_breakpoint (bl->owner))
5860 return NULL;
5861
5862 if (gdb_evaluates_breakpoint_condition_p ()
5863 || !target_supports_evaluation_of_breakpoint_conditions ())
5864 return condition_evaluation_host;
5865
5866 if (bl && bl->cond_bytecode)
5867 return condition_evaluation_target;
5868 else
5869 return condition_evaluation_host;
5870 }
5871
5872 /* Print the LOC location out of the list of B->LOC locations. */
5873
5874 static void
5875 print_breakpoint_location (struct breakpoint *b,
5876 struct bp_location *loc)
5877 {
5878 struct ui_out *uiout = current_uiout;
5879
5880 scoped_restore_current_program_space restore_pspace;
5881
5882 if (loc != NULL && loc->shlib_disabled)
5883 loc = NULL;
5884
5885 if (loc != NULL)
5886 set_current_program_space (loc->pspace);
5887
5888 if (b->display_canonical)
5889 uiout->field_string ("what", event_location_to_string (b->location.get ()));
5890 else if (loc && loc->symtab)
5891 {
5892 const struct symbol *sym = loc->symbol;
5893
5894 if (sym)
5895 {
5896 uiout->text ("in ");
5897 uiout->field_string ("func", sym->print_name (),
5898 function_name_style.style ());
5899 uiout->text (" ");
5900 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5901 uiout->text ("at ");
5902 }
5903 uiout->field_string ("file",
5904 symtab_to_filename_for_display (loc->symtab),
5905 file_name_style.style ());
5906 uiout->text (":");
5907
5908 if (uiout->is_mi_like_p ())
5909 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
5910
5911 uiout->field_signed ("line", loc->line_number);
5912 }
5913 else if (loc)
5914 {
5915 string_file stb;
5916
5917 print_address_symbolic (loc->gdbarch, loc->address, &stb,
5918 demangle, "");
5919 uiout->field_stream ("at", stb);
5920 }
5921 else
5922 {
5923 uiout->field_string ("pending",
5924 event_location_to_string (b->location.get ()));
5925 /* If extra_string is available, it could be holding a condition
5926 or dprintf arguments. In either case, make sure it is printed,
5927 too, but only for non-MI streams. */
5928 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
5929 {
5930 if (b->type == bp_dprintf)
5931 uiout->text (",");
5932 else
5933 uiout->text (" ");
5934 uiout->text (b->extra_string);
5935 }
5936 }
5937
5938 if (loc && is_breakpoint (b)
5939 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5940 && bp_condition_evaluator (b) == condition_evaluation_both)
5941 {
5942 uiout->text (" (");
5943 uiout->field_string ("evaluated-by",
5944 bp_location_condition_evaluator (loc));
5945 uiout->text (")");
5946 }
5947 }
5948
5949 static const char *
5950 bptype_string (enum bptype type)
5951 {
5952 struct ep_type_description
5953 {
5954 enum bptype type;
5955 const char *description;
5956 };
5957 static struct ep_type_description bptypes[] =
5958 {
5959 {bp_none, "?deleted?"},
5960 {bp_breakpoint, "breakpoint"},
5961 {bp_hardware_breakpoint, "hw breakpoint"},
5962 {bp_single_step, "sw single-step"},
5963 {bp_until, "until"},
5964 {bp_finish, "finish"},
5965 {bp_watchpoint, "watchpoint"},
5966 {bp_hardware_watchpoint, "hw watchpoint"},
5967 {bp_read_watchpoint, "read watchpoint"},
5968 {bp_access_watchpoint, "acc watchpoint"},
5969 {bp_longjmp, "longjmp"},
5970 {bp_longjmp_resume, "longjmp resume"},
5971 {bp_longjmp_call_dummy, "longjmp for call dummy"},
5972 {bp_exception, "exception"},
5973 {bp_exception_resume, "exception resume"},
5974 {bp_step_resume, "step resume"},
5975 {bp_hp_step_resume, "high-priority step resume"},
5976 {bp_watchpoint_scope, "watchpoint scope"},
5977 {bp_call_dummy, "call dummy"},
5978 {bp_std_terminate, "std::terminate"},
5979 {bp_shlib_event, "shlib events"},
5980 {bp_thread_event, "thread events"},
5981 {bp_overlay_event, "overlay events"},
5982 {bp_longjmp_master, "longjmp master"},
5983 {bp_std_terminate_master, "std::terminate master"},
5984 {bp_exception_master, "exception master"},
5985 {bp_catchpoint, "catchpoint"},
5986 {bp_tracepoint, "tracepoint"},
5987 {bp_fast_tracepoint, "fast tracepoint"},
5988 {bp_static_tracepoint, "static tracepoint"},
5989 {bp_dprintf, "dprintf"},
5990 {bp_jit_event, "jit events"},
5991 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5992 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
5993 };
5994
5995 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5996 || ((int) type != bptypes[(int) type].type))
5997 internal_error (__FILE__, __LINE__,
5998 _("bptypes table does not describe type #%d."),
5999 (int) type);
6000
6001 return bptypes[(int) type].description;
6002 }
6003
6004 /* For MI, output a field named 'thread-groups' with a list as the value.
6005 For CLI, prefix the list with the string 'inf'. */
6006
6007 static void
6008 output_thread_groups (struct ui_out *uiout,
6009 const char *field_name,
6010 const std::vector<int> &inf_nums,
6011 int mi_only)
6012 {
6013 int is_mi = uiout->is_mi_like_p ();
6014
6015 /* For backward compatibility, don't display inferiors in CLI unless
6016 there are several. Always display them for MI. */
6017 if (!is_mi && mi_only)
6018 return;
6019
6020 ui_out_emit_list list_emitter (uiout, field_name);
6021
6022 for (size_t i = 0; i < inf_nums.size (); i++)
6023 {
6024 if (is_mi)
6025 {
6026 char mi_group[10];
6027
6028 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
6029 uiout->field_string (NULL, mi_group);
6030 }
6031 else
6032 {
6033 if (i == 0)
6034 uiout->text (" inf ");
6035 else
6036 uiout->text (", ");
6037
6038 uiout->text (plongest (inf_nums[i]));
6039 }
6040 }
6041 }
6042
6043 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6044 instead of going via breakpoint_ops::print_one. This makes "maint
6045 info breakpoints" show the software breakpoint locations of
6046 catchpoints, which are considered internal implementation
6047 detail. */
6048
6049 static void
6050 print_one_breakpoint_location (struct breakpoint *b,
6051 struct bp_location *loc,
6052 int loc_number,
6053 struct bp_location **last_loc,
6054 int allflag, bool raw_loc)
6055 {
6056 struct command_line *l;
6057 static char bpenables[] = "nynny";
6058
6059 struct ui_out *uiout = current_uiout;
6060 int header_of_multiple = 0;
6061 int part_of_multiple = (loc != NULL);
6062 struct value_print_options opts;
6063
6064 get_user_print_options (&opts);
6065
6066 gdb_assert (!loc || loc_number != 0);
6067 /* See comment in print_one_breakpoint concerning treatment of
6068 breakpoints with single disabled location. */
6069 if (loc == NULL
6070 && (b->loc != NULL
6071 && (b->loc->next != NULL
6072 || !b->loc->enabled || b->loc->disabled_by_cond)))
6073 header_of_multiple = 1;
6074 if (loc == NULL)
6075 loc = b->loc;
6076
6077 annotate_record ();
6078
6079 /* 1 */
6080 annotate_field (0);
6081 if (part_of_multiple)
6082 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6083 else
6084 uiout->field_signed ("number", b->number);
6085
6086 /* 2 */
6087 annotate_field (1);
6088 if (part_of_multiple)
6089 uiout->field_skip ("type");
6090 else
6091 uiout->field_string ("type", bptype_string (b->type));
6092
6093 /* 3 */
6094 annotate_field (2);
6095 if (part_of_multiple)
6096 uiout->field_skip ("disp");
6097 else
6098 uiout->field_string ("disp", bpdisp_text (b->disposition));
6099
6100 /* 4 */
6101 annotate_field (3);
6102 if (part_of_multiple)
6103 uiout->field_string ("enabled", (loc->disabled_by_cond ? "N*"
6104 : (loc->enabled ? "y" : "n")));
6105 else
6106 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6107
6108 /* 5 and 6 */
6109 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
6110 b->ops->print_one (b, last_loc);
6111 else
6112 {
6113 if (is_watchpoint (b))
6114 {
6115 struct watchpoint *w = (struct watchpoint *) b;
6116
6117 /* Field 4, the address, is omitted (which makes the columns
6118 not line up too nicely with the headers, but the effect
6119 is relatively readable). */
6120 if (opts.addressprint)
6121 uiout->field_skip ("addr");
6122 annotate_field (5);
6123 uiout->field_string ("what", w->exp_string);
6124 }
6125 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6126 || is_ada_exception_catchpoint (b))
6127 {
6128 if (opts.addressprint)
6129 {
6130 annotate_field (4);
6131 if (header_of_multiple)
6132 uiout->field_string ("addr", "<MULTIPLE>",
6133 metadata_style.style ());
6134 else if (b->loc == NULL || loc->shlib_disabled)
6135 uiout->field_string ("addr", "<PENDING>",
6136 metadata_style.style ());
6137 else
6138 uiout->field_core_addr ("addr",
6139 loc->gdbarch, loc->address);
6140 }
6141 annotate_field (5);
6142 if (!header_of_multiple)
6143 print_breakpoint_location (b, loc);
6144 if (b->loc)
6145 *last_loc = b->loc;
6146 }
6147 }
6148
6149 if (loc != NULL && !header_of_multiple)
6150 {
6151 std::vector<int> inf_nums;
6152 int mi_only = 1;
6153
6154 for (inferior *inf : all_inferiors ())
6155 {
6156 if (inf->pspace == loc->pspace)
6157 inf_nums.push_back (inf->num);
6158 }
6159
6160 /* For backward compatibility, don't display inferiors in CLI unless
6161 there are several. Always display for MI. */
6162 if (allflag
6163 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6164 && (program_spaces.size () > 1
6165 || number_of_inferiors () > 1)
6166 /* LOC is for existing B, it cannot be in
6167 moribund_locations and thus having NULL OWNER. */
6168 && loc->owner->type != bp_catchpoint))
6169 mi_only = 0;
6170 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6171 }
6172
6173 if (!part_of_multiple)
6174 {
6175 if (b->thread != -1)
6176 {
6177 /* FIXME: This seems to be redundant and lost here; see the
6178 "stop only in" line a little further down. */
6179 uiout->text (" thread ");
6180 uiout->field_signed ("thread", b->thread);
6181 }
6182 else if (b->task != 0)
6183 {
6184 uiout->text (" task ");
6185 uiout->field_signed ("task", b->task);
6186 }
6187 }
6188
6189 uiout->text ("\n");
6190
6191 if (!part_of_multiple)
6192 b->ops->print_one_detail (b, uiout);
6193
6194 if (part_of_multiple && frame_id_p (b->frame_id))
6195 {
6196 annotate_field (6);
6197 uiout->text ("\tstop only in stack frame at ");
6198 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6199 the frame ID. */
6200 uiout->field_core_addr ("frame",
6201 b->gdbarch, b->frame_id.stack_addr);
6202 uiout->text ("\n");
6203 }
6204
6205 if (!part_of_multiple && b->cond_string)
6206 {
6207 annotate_field (7);
6208 if (is_tracepoint (b))
6209 uiout->text ("\ttrace only if ");
6210 else
6211 uiout->text ("\tstop only if ");
6212 uiout->field_string ("cond", b->cond_string);
6213
6214 /* Print whether the target is doing the breakpoint's condition
6215 evaluation. If GDB is doing the evaluation, don't print anything. */
6216 if (is_breakpoint (b)
6217 && breakpoint_condition_evaluation_mode ()
6218 == condition_evaluation_target)
6219 {
6220 uiout->message (" (%pF evals)",
6221 string_field ("evaluated-by",
6222 bp_condition_evaluator (b)));
6223 }
6224 uiout->text ("\n");
6225 }
6226
6227 if (!part_of_multiple && b->thread != -1)
6228 {
6229 /* FIXME should make an annotation for this. */
6230 uiout->text ("\tstop only in thread ");
6231 if (uiout->is_mi_like_p ())
6232 uiout->field_signed ("thread", b->thread);
6233 else
6234 {
6235 struct thread_info *thr = find_thread_global_id (b->thread);
6236
6237 uiout->field_string ("thread", print_thread_id (thr));
6238 }
6239 uiout->text ("\n");
6240 }
6241
6242 if (!part_of_multiple)
6243 {
6244 if (b->hit_count)
6245 {
6246 /* FIXME should make an annotation for this. */
6247 if (is_catchpoint (b))
6248 uiout->text ("\tcatchpoint");
6249 else if (is_tracepoint (b))
6250 uiout->text ("\ttracepoint");
6251 else
6252 uiout->text ("\tbreakpoint");
6253 uiout->text (" already hit ");
6254 uiout->field_signed ("times", b->hit_count);
6255 if (b->hit_count == 1)
6256 uiout->text (" time\n");
6257 else
6258 uiout->text (" times\n");
6259 }
6260 else
6261 {
6262 /* Output the count also if it is zero, but only if this is mi. */
6263 if (uiout->is_mi_like_p ())
6264 uiout->field_signed ("times", b->hit_count);
6265 }
6266 }
6267
6268 if (!part_of_multiple && b->ignore_count)
6269 {
6270 annotate_field (8);
6271 uiout->message ("\tignore next %pF hits\n",
6272 signed_field ("ignore", b->ignore_count));
6273 }
6274
6275 /* Note that an enable count of 1 corresponds to "enable once"
6276 behavior, which is reported by the combination of enablement and
6277 disposition, so we don't need to mention it here. */
6278 if (!part_of_multiple && b->enable_count > 1)
6279 {
6280 annotate_field (8);
6281 uiout->text ("\tdisable after ");
6282 /* Tweak the wording to clarify that ignore and enable counts
6283 are distinct, and have additive effect. */
6284 if (b->ignore_count)
6285 uiout->text ("additional ");
6286 else
6287 uiout->text ("next ");
6288 uiout->field_signed ("enable", b->enable_count);
6289 uiout->text (" hits\n");
6290 }
6291
6292 if (!part_of_multiple && is_tracepoint (b))
6293 {
6294 struct tracepoint *tp = (struct tracepoint *) b;
6295
6296 if (tp->traceframe_usage)
6297 {
6298 uiout->text ("\ttrace buffer usage ");
6299 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6300 uiout->text (" bytes\n");
6301 }
6302 }
6303
6304 l = b->commands ? b->commands.get () : NULL;
6305 if (!part_of_multiple && l)
6306 {
6307 annotate_field (9);
6308 ui_out_emit_tuple tuple_emitter (uiout, "script");
6309 print_command_lines (uiout, l, 4);
6310 }
6311
6312 if (is_tracepoint (b))
6313 {
6314 struct tracepoint *t = (struct tracepoint *) b;
6315
6316 if (!part_of_multiple && t->pass_count)
6317 {
6318 annotate_field (10);
6319 uiout->text ("\tpass count ");
6320 uiout->field_signed ("pass", t->pass_count);
6321 uiout->text (" \n");
6322 }
6323
6324 /* Don't display it when tracepoint or tracepoint location is
6325 pending. */
6326 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6327 {
6328 annotate_field (11);
6329
6330 if (uiout->is_mi_like_p ())
6331 uiout->field_string ("installed",
6332 loc->inserted ? "y" : "n");
6333 else
6334 {
6335 if (loc->inserted)
6336 uiout->text ("\t");
6337 else
6338 uiout->text ("\tnot ");
6339 uiout->text ("installed on target\n");
6340 }
6341 }
6342 }
6343
6344 if (uiout->is_mi_like_p () && !part_of_multiple)
6345 {
6346 if (is_watchpoint (b))
6347 {
6348 struct watchpoint *w = (struct watchpoint *) b;
6349
6350 uiout->field_string ("original-location", w->exp_string);
6351 }
6352 else if (b->location != NULL
6353 && event_location_to_string (b->location.get ()) != NULL)
6354 uiout->field_string ("original-location",
6355 event_location_to_string (b->location.get ()));
6356 }
6357 }
6358
6359 /* See breakpoint.h. */
6360
6361 bool fix_multi_location_breakpoint_output_globally = false;
6362
6363 static void
6364 print_one_breakpoint (struct breakpoint *b,
6365 struct bp_location **last_loc,
6366 int allflag)
6367 {
6368 struct ui_out *uiout = current_uiout;
6369 bool use_fixed_output
6370 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6371 || fix_multi_location_breakpoint_output_globally);
6372
6373 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6374 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
6375
6376 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6377 are outside. */
6378 if (!use_fixed_output)
6379 bkpt_tuple_emitter.reset ();
6380
6381 /* If this breakpoint has custom print function,
6382 it's already printed. Otherwise, print individual
6383 locations, if any. */
6384 if (b->ops == NULL
6385 || b->ops->print_one == NULL
6386 || allflag)
6387 {
6388 /* If breakpoint has a single location that is disabled, we
6389 print it as if it had several locations, since otherwise it's
6390 hard to represent "breakpoint enabled, location disabled"
6391 situation.
6392
6393 Note that while hardware watchpoints have several locations
6394 internally, that's not a property exposed to users.
6395
6396 Likewise, while catchpoints may be implemented with
6397 breakpoints (e.g., catch throw), that's not a property
6398 exposed to users. We do however display the internal
6399 breakpoint locations with "maint info breakpoints". */
6400 if (!is_hardware_watchpoint (b)
6401 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6402 || is_ada_exception_catchpoint (b))
6403 && (allflag
6404 || (b->loc && (b->loc->next
6405 || !b->loc->enabled
6406 || b->loc->disabled_by_cond))))
6407 {
6408 gdb::optional<ui_out_emit_list> locations_list;
6409
6410 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6411 MI record. For later versions, place breakpoint locations in a
6412 list. */
6413 if (uiout->is_mi_like_p () && use_fixed_output)
6414 locations_list.emplace (uiout, "locations");
6415
6416 int n = 1;
6417 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next, ++n)
6418 {
6419 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6420 print_one_breakpoint_location (b, loc, n, last_loc,
6421 allflag, allflag);
6422 }
6423 }
6424 }
6425 }
6426
6427 static int
6428 breakpoint_address_bits (struct breakpoint *b)
6429 {
6430 int print_address_bits = 0;
6431 struct bp_location *loc;
6432
6433 /* Software watchpoints that aren't watching memory don't have an
6434 address to print. */
6435 if (is_no_memory_software_watchpoint (b))
6436 return 0;
6437
6438 for (loc = b->loc; loc; loc = loc->next)
6439 {
6440 int addr_bit;
6441
6442 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6443 if (addr_bit > print_address_bits)
6444 print_address_bits = addr_bit;
6445 }
6446
6447 return print_address_bits;
6448 }
6449
6450 /* See breakpoint.h. */
6451
6452 void
6453 print_breakpoint (breakpoint *b)
6454 {
6455 struct bp_location *dummy_loc = NULL;
6456 print_one_breakpoint (b, &dummy_loc, 0);
6457 }
6458
6459 /* Return true if this breakpoint was set by the user, false if it is
6460 internal or momentary. */
6461
6462 int
6463 user_breakpoint_p (struct breakpoint *b)
6464 {
6465 return b->number > 0;
6466 }
6467
6468 /* See breakpoint.h. */
6469
6470 int
6471 pending_breakpoint_p (struct breakpoint *b)
6472 {
6473 return b->loc == NULL;
6474 }
6475
6476 /* Print information on breakpoints (including watchpoints and tracepoints).
6477
6478 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6479 understood by number_or_range_parser. Only breakpoints included in this
6480 list are then printed.
6481
6482 If SHOW_INTERNAL is true, print internal breakpoints.
6483
6484 If FILTER is non-NULL, call it on each breakpoint and only include the
6485 ones for which it returns true.
6486
6487 Return the total number of breakpoints listed. */
6488
6489 static int
6490 breakpoint_1 (const char *bp_num_list, bool show_internal,
6491 bool (*filter) (const struct breakpoint *))
6492 {
6493 struct breakpoint *b;
6494 struct bp_location *last_loc = NULL;
6495 int nr_printable_breakpoints;
6496 struct value_print_options opts;
6497 int print_address_bits = 0;
6498 int print_type_col_width = 14;
6499 struct ui_out *uiout = current_uiout;
6500 bool has_disabled_by_cond_location = false;
6501
6502 get_user_print_options (&opts);
6503
6504 /* Compute the number of rows in the table, as well as the size
6505 required for address fields. */
6506 nr_printable_breakpoints = 0;
6507 ALL_BREAKPOINTS (b)
6508 {
6509 /* If we have a filter, only list the breakpoints it accepts. */
6510 if (filter && !filter (b))
6511 continue;
6512
6513 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6514 accept. Skip the others. */
6515 if (bp_num_list != NULL && *bp_num_list != '\0')
6516 {
6517 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6518 continue;
6519 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6520 continue;
6521 }
6522
6523 if (show_internal || user_breakpoint_p (b))
6524 {
6525 int addr_bit, type_len;
6526
6527 addr_bit = breakpoint_address_bits (b);
6528 if (addr_bit > print_address_bits)
6529 print_address_bits = addr_bit;
6530
6531 type_len = strlen (bptype_string (b->type));
6532 if (type_len > print_type_col_width)
6533 print_type_col_width = type_len;
6534
6535 nr_printable_breakpoints++;
6536 }
6537 }
6538
6539 {
6540 ui_out_emit_table table_emitter (uiout,
6541 opts.addressprint ? 6 : 5,
6542 nr_printable_breakpoints,
6543 "BreakpointTable");
6544
6545 if (nr_printable_breakpoints > 0)
6546 annotate_breakpoints_headers ();
6547 if (nr_printable_breakpoints > 0)
6548 annotate_field (0);
6549 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6550 if (nr_printable_breakpoints > 0)
6551 annotate_field (1);
6552 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6553 if (nr_printable_breakpoints > 0)
6554 annotate_field (2);
6555 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6556 if (nr_printable_breakpoints > 0)
6557 annotate_field (3);
6558 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6559 if (opts.addressprint)
6560 {
6561 if (nr_printable_breakpoints > 0)
6562 annotate_field (4);
6563 if (print_address_bits <= 32)
6564 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6565 else
6566 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6567 }
6568 if (nr_printable_breakpoints > 0)
6569 annotate_field (5);
6570 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6571 uiout->table_body ();
6572 if (nr_printable_breakpoints > 0)
6573 annotate_breakpoints_table ();
6574
6575 ALL_BREAKPOINTS (b)
6576 {
6577 QUIT;
6578 /* If we have a filter, only list the breakpoints it accepts. */
6579 if (filter && !filter (b))
6580 continue;
6581
6582 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6583 accept. Skip the others. */
6584
6585 if (bp_num_list != NULL && *bp_num_list != '\0')
6586 {
6587 if (show_internal) /* maintenance info breakpoint */
6588 {
6589 if (parse_and_eval_long (bp_num_list) != b->number)
6590 continue;
6591 }
6592 else /* all others */
6593 {
6594 if (!number_is_in_list (bp_num_list, b->number))
6595 continue;
6596 }
6597 }
6598 /* We only print out user settable breakpoints unless the
6599 show_internal is set. */
6600 if (show_internal || user_breakpoint_p (b))
6601 {
6602 print_one_breakpoint (b, &last_loc, show_internal);
6603 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
6604 if (loc->disabled_by_cond)
6605 has_disabled_by_cond_location = true;
6606 }
6607 }
6608 }
6609
6610 if (nr_printable_breakpoints == 0)
6611 {
6612 /* If there's a filter, let the caller decide how to report
6613 empty list. */
6614 if (!filter)
6615 {
6616 if (bp_num_list == NULL || *bp_num_list == '\0')
6617 uiout->message ("No breakpoints or watchpoints.\n");
6618 else
6619 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6620 bp_num_list);
6621 }
6622 }
6623 else
6624 {
6625 if (last_loc && !server_command)
6626 set_next_address (last_loc->gdbarch, last_loc->address);
6627
6628 if (has_disabled_by_cond_location)
6629 uiout->message (_("(*): Breakpoint condition is invalid at this "
6630 "location.\n"));
6631 }
6632
6633 /* FIXME? Should this be moved up so that it is only called when
6634 there have been breakpoints? */
6635 annotate_breakpoints_table_end ();
6636
6637 return nr_printable_breakpoints;
6638 }
6639
6640 /* Display the value of default-collect in a way that is generally
6641 compatible with the breakpoint list. */
6642
6643 static void
6644 default_collect_info (void)
6645 {
6646 struct ui_out *uiout = current_uiout;
6647
6648 /* If it has no value (which is frequently the case), say nothing; a
6649 message like "No default-collect." gets in user's face when it's
6650 not wanted. */
6651 if (!*default_collect)
6652 return;
6653
6654 /* The following phrase lines up nicely with per-tracepoint collect
6655 actions. */
6656 uiout->text ("default collect ");
6657 uiout->field_string ("default-collect", default_collect);
6658 uiout->text (" \n");
6659 }
6660
6661 static void
6662 info_breakpoints_command (const char *args, int from_tty)
6663 {
6664 breakpoint_1 (args, false, NULL);
6665
6666 default_collect_info ();
6667 }
6668
6669 static void
6670 info_watchpoints_command (const char *args, int from_tty)
6671 {
6672 int num_printed = breakpoint_1 (args, false, is_watchpoint);
6673 struct ui_out *uiout = current_uiout;
6674
6675 if (num_printed == 0)
6676 {
6677 if (args == NULL || *args == '\0')
6678 uiout->message ("No watchpoints.\n");
6679 else
6680 uiout->message ("No watchpoint matching '%s'.\n", args);
6681 }
6682 }
6683
6684 static void
6685 maintenance_info_breakpoints (const char *args, int from_tty)
6686 {
6687 breakpoint_1 (args, true, NULL);
6688
6689 default_collect_info ();
6690 }
6691
6692 static int
6693 breakpoint_has_pc (struct breakpoint *b,
6694 struct program_space *pspace,
6695 CORE_ADDR pc, struct obj_section *section)
6696 {
6697 struct bp_location *bl = b->loc;
6698
6699 for (; bl; bl = bl->next)
6700 {
6701 if (bl->pspace == pspace
6702 && bl->address == pc
6703 && (!overlay_debugging || bl->section == section))
6704 return 1;
6705 }
6706 return 0;
6707 }
6708
6709 /* Print a message describing any user-breakpoints set at PC. This
6710 concerns with logical breakpoints, so we match program spaces, not
6711 address spaces. */
6712
6713 static void
6714 describe_other_breakpoints (struct gdbarch *gdbarch,
6715 struct program_space *pspace, CORE_ADDR pc,
6716 struct obj_section *section, int thread)
6717 {
6718 int others = 0;
6719 struct breakpoint *b;
6720
6721 ALL_BREAKPOINTS (b)
6722 others += (user_breakpoint_p (b)
6723 && breakpoint_has_pc (b, pspace, pc, section));
6724 if (others > 0)
6725 {
6726 if (others == 1)
6727 printf_filtered (_("Note: breakpoint "));
6728 else /* if (others == ???) */
6729 printf_filtered (_("Note: breakpoints "));
6730 ALL_BREAKPOINTS (b)
6731 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6732 {
6733 others--;
6734 printf_filtered ("%d", b->number);
6735 if (b->thread == -1 && thread != -1)
6736 printf_filtered (" (all threads)");
6737 else if (b->thread != -1)
6738 printf_filtered (" (thread %d)", b->thread);
6739 printf_filtered ("%s%s ",
6740 ((b->enable_state == bp_disabled
6741 || b->enable_state == bp_call_disabled)
6742 ? " (disabled)"
6743 : ""),
6744 (others > 1) ? ","
6745 : ((others == 1) ? " and" : ""));
6746 }
6747 current_uiout->message (_("also set at pc %ps.\n"),
6748 styled_string (address_style.style (),
6749 paddress (gdbarch, pc)));
6750 }
6751 }
6752 \f
6753
6754 /* Return true iff it is meaningful to use the address member of LOC.
6755 For some breakpoint types, the locations' address members are
6756 irrelevant and it makes no sense to attempt to compare them to
6757 other addresses (or use them for any other purpose either).
6758
6759 More specifically, software watchpoints and catchpoints that are
6760 not backed by breakpoints always have a zero valued location
6761 address and we don't want to mark breakpoints of any of these types
6762 to be a duplicate of an actual breakpoint location at address
6763 zero. */
6764
6765 static bool
6766 bl_address_is_meaningful (bp_location *loc)
6767 {
6768 return loc->loc_type != bp_loc_other;
6769 }
6770
6771 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6772 true if LOC1 and LOC2 represent the same watchpoint location. */
6773
6774 static int
6775 watchpoint_locations_match (struct bp_location *loc1,
6776 struct bp_location *loc2)
6777 {
6778 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6779 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6780
6781 /* Both of them must exist. */
6782 gdb_assert (w1 != NULL);
6783 gdb_assert (w2 != NULL);
6784
6785 /* If the target can evaluate the condition expression in hardware,
6786 then we we need to insert both watchpoints even if they are at
6787 the same place. Otherwise the watchpoint will only trigger when
6788 the condition of whichever watchpoint was inserted evaluates to
6789 true, not giving a chance for GDB to check the condition of the
6790 other watchpoint. */
6791 if ((w1->cond_exp
6792 && target_can_accel_watchpoint_condition (loc1->address,
6793 loc1->length,
6794 loc1->watchpoint_type,
6795 w1->cond_exp.get ()))
6796 || (w2->cond_exp
6797 && target_can_accel_watchpoint_condition (loc2->address,
6798 loc2->length,
6799 loc2->watchpoint_type,
6800 w2->cond_exp.get ())))
6801 return 0;
6802
6803 /* Note that this checks the owner's type, not the location's. In
6804 case the target does not support read watchpoints, but does
6805 support access watchpoints, we'll have bp_read_watchpoint
6806 watchpoints with hw_access locations. Those should be considered
6807 duplicates of hw_read locations. The hw_read locations will
6808 become hw_access locations later. */
6809 return (loc1->owner->type == loc2->owner->type
6810 && loc1->pspace->aspace == loc2->pspace->aspace
6811 && loc1->address == loc2->address
6812 && loc1->length == loc2->length);
6813 }
6814
6815 /* See breakpoint.h. */
6816
6817 int
6818 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6819 const address_space *aspace2, CORE_ADDR addr2)
6820 {
6821 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6822 || aspace1 == aspace2)
6823 && addr1 == addr2);
6824 }
6825
6826 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6827 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6828 matches ASPACE2. On targets that have global breakpoints, the address
6829 space doesn't really matter. */
6830
6831 static int
6832 breakpoint_address_match_range (const address_space *aspace1,
6833 CORE_ADDR addr1,
6834 int len1, const address_space *aspace2,
6835 CORE_ADDR addr2)
6836 {
6837 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6838 || aspace1 == aspace2)
6839 && addr2 >= addr1 && addr2 < addr1 + len1);
6840 }
6841
6842 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6843 a ranged breakpoint. In most targets, a match happens only if ASPACE
6844 matches the breakpoint's address space. On targets that have global
6845 breakpoints, the address space doesn't really matter. */
6846
6847 static int
6848 breakpoint_location_address_match (struct bp_location *bl,
6849 const address_space *aspace,
6850 CORE_ADDR addr)
6851 {
6852 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6853 aspace, addr)
6854 || (bl->length
6855 && breakpoint_address_match_range (bl->pspace->aspace,
6856 bl->address, bl->length,
6857 aspace, addr)));
6858 }
6859
6860 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6861 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6862 match happens only if ASPACE matches the breakpoint's address
6863 space. On targets that have global breakpoints, the address space
6864 doesn't really matter. */
6865
6866 static int
6867 breakpoint_location_address_range_overlap (struct bp_location *bl,
6868 const address_space *aspace,
6869 CORE_ADDR addr, int len)
6870 {
6871 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6872 || bl->pspace->aspace == aspace)
6873 {
6874 int bl_len = bl->length != 0 ? bl->length : 1;
6875
6876 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6877 return 1;
6878 }
6879 return 0;
6880 }
6881
6882 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6883 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6884 true, otherwise returns false. */
6885
6886 static int
6887 tracepoint_locations_match (struct bp_location *loc1,
6888 struct bp_location *loc2)
6889 {
6890 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6891 /* Since tracepoint locations are never duplicated with others', tracepoint
6892 locations at the same address of different tracepoints are regarded as
6893 different locations. */
6894 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6895 else
6896 return 0;
6897 }
6898
6899 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6900 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
6901 the same location. If SW_HW_BPS_MATCH is true, then software
6902 breakpoint locations and hardware breakpoint locations match,
6903 otherwise they don't. */
6904
6905 static int
6906 breakpoint_locations_match (struct bp_location *loc1,
6907 struct bp_location *loc2,
6908 bool sw_hw_bps_match)
6909 {
6910 int hw_point1, hw_point2;
6911
6912 /* Both of them must not be in moribund_locations. */
6913 gdb_assert (loc1->owner != NULL);
6914 gdb_assert (loc2->owner != NULL);
6915
6916 hw_point1 = is_hardware_watchpoint (loc1->owner);
6917 hw_point2 = is_hardware_watchpoint (loc2->owner);
6918
6919 if (hw_point1 != hw_point2)
6920 return 0;
6921 else if (hw_point1)
6922 return watchpoint_locations_match (loc1, loc2);
6923 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6924 return tracepoint_locations_match (loc1, loc2);
6925 else
6926 /* We compare bp_location.length in order to cover ranged
6927 breakpoints. Keep this in sync with
6928 bp_location_is_less_than. */
6929 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6930 loc2->pspace->aspace, loc2->address)
6931 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
6932 && loc1->length == loc2->length);
6933 }
6934
6935 static void
6936 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6937 int bnum, int have_bnum)
6938 {
6939 /* The longest string possibly returned by hex_string_custom
6940 is 50 chars. These must be at least that big for safety. */
6941 char astr1[64];
6942 char astr2[64];
6943
6944 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6945 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
6946 if (have_bnum)
6947 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
6948 bnum, astr1, astr2);
6949 else
6950 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
6951 }
6952
6953 /* Adjust a breakpoint's address to account for architectural
6954 constraints on breakpoint placement. Return the adjusted address.
6955 Note: Very few targets require this kind of adjustment. For most
6956 targets, this function is simply the identity function. */
6957
6958 static CORE_ADDR
6959 adjust_breakpoint_address (struct gdbarch *gdbarch,
6960 CORE_ADDR bpaddr, enum bptype bptype)
6961 {
6962 if (bptype == bp_watchpoint
6963 || bptype == bp_hardware_watchpoint
6964 || bptype == bp_read_watchpoint
6965 || bptype == bp_access_watchpoint
6966 || bptype == bp_catchpoint)
6967 {
6968 /* Watchpoints and the various bp_catch_* eventpoints should not
6969 have their addresses modified. */
6970 return bpaddr;
6971 }
6972 else if (bptype == bp_single_step)
6973 {
6974 /* Single-step breakpoints should not have their addresses
6975 modified. If there's any architectural constrain that
6976 applies to this address, then it should have already been
6977 taken into account when the breakpoint was created in the
6978 first place. If we didn't do this, stepping through e.g.,
6979 Thumb-2 IT blocks would break. */
6980 return bpaddr;
6981 }
6982 else
6983 {
6984 CORE_ADDR adjusted_bpaddr = bpaddr;
6985
6986 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
6987 {
6988 /* Some targets have architectural constraints on the placement
6989 of breakpoint instructions. Obtain the adjusted address. */
6990 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
6991 }
6992
6993 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
6994
6995 /* An adjusted breakpoint address can significantly alter
6996 a user's expectations. Print a warning if an adjustment
6997 is required. */
6998 if (adjusted_bpaddr != bpaddr)
6999 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7000
7001 return adjusted_bpaddr;
7002 }
7003 }
7004
7005 static bp_loc_type
7006 bp_location_from_bp_type (bptype type)
7007 {
7008 switch (type)
7009 {
7010 case bp_breakpoint:
7011 case bp_single_step:
7012 case bp_until:
7013 case bp_finish:
7014 case bp_longjmp:
7015 case bp_longjmp_resume:
7016 case bp_longjmp_call_dummy:
7017 case bp_exception:
7018 case bp_exception_resume:
7019 case bp_step_resume:
7020 case bp_hp_step_resume:
7021 case bp_watchpoint_scope:
7022 case bp_call_dummy:
7023 case bp_std_terminate:
7024 case bp_shlib_event:
7025 case bp_thread_event:
7026 case bp_overlay_event:
7027 case bp_jit_event:
7028 case bp_longjmp_master:
7029 case bp_std_terminate_master:
7030 case bp_exception_master:
7031 case bp_gnu_ifunc_resolver:
7032 case bp_gnu_ifunc_resolver_return:
7033 case bp_dprintf:
7034 return bp_loc_software_breakpoint;
7035 case bp_hardware_breakpoint:
7036 return bp_loc_hardware_breakpoint;
7037 case bp_hardware_watchpoint:
7038 case bp_read_watchpoint:
7039 case bp_access_watchpoint:
7040 return bp_loc_hardware_watchpoint;
7041 case bp_watchpoint:
7042 case bp_catchpoint:
7043 case bp_tracepoint:
7044 case bp_fast_tracepoint:
7045 case bp_static_tracepoint:
7046 return bp_loc_other;
7047 default:
7048 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7049 }
7050 }
7051
7052 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7053 {
7054 this->owner = owner;
7055 this->cond_bytecode = NULL;
7056 this->shlib_disabled = 0;
7057 this->enabled = 1;
7058 this->disabled_by_cond = false;
7059
7060 this->loc_type = type;
7061
7062 if (this->loc_type == bp_loc_software_breakpoint
7063 || this->loc_type == bp_loc_hardware_breakpoint)
7064 mark_breakpoint_location_modified (this);
7065
7066 this->refc = 1;
7067 }
7068
7069 bp_location::bp_location (breakpoint *owner)
7070 : bp_location::bp_location (owner,
7071 bp_location_from_bp_type (owner->type))
7072 {
7073 }
7074
7075 /* Allocate a struct bp_location. */
7076
7077 static struct bp_location *
7078 allocate_bp_location (struct breakpoint *bpt)
7079 {
7080 return bpt->ops->allocate_location (bpt);
7081 }
7082
7083 static void
7084 free_bp_location (struct bp_location *loc)
7085 {
7086 delete loc;
7087 }
7088
7089 /* Increment reference count. */
7090
7091 static void
7092 incref_bp_location (struct bp_location *bl)
7093 {
7094 ++bl->refc;
7095 }
7096
7097 /* Decrement reference count. If the reference count reaches 0,
7098 destroy the bp_location. Sets *BLP to NULL. */
7099
7100 static void
7101 decref_bp_location (struct bp_location **blp)
7102 {
7103 gdb_assert ((*blp)->refc > 0);
7104
7105 if (--(*blp)->refc == 0)
7106 free_bp_location (*blp);
7107 *blp = NULL;
7108 }
7109
7110 /* Add breakpoint B at the end of the global breakpoint chain. */
7111
7112 static breakpoint *
7113 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7114 {
7115 struct breakpoint *b1;
7116 struct breakpoint *result = b.get ();
7117
7118 /* Add this breakpoint to the end of the chain so that a list of
7119 breakpoints will come out in order of increasing numbers. */
7120
7121 b1 = breakpoint_chain;
7122 if (b1 == 0)
7123 breakpoint_chain = b.release ();
7124 else
7125 {
7126 while (b1->next)
7127 b1 = b1->next;
7128 b1->next = b.release ();
7129 }
7130
7131 return result;
7132 }
7133
7134 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7135
7136 static void
7137 init_raw_breakpoint_without_location (struct breakpoint *b,
7138 struct gdbarch *gdbarch,
7139 enum bptype bptype,
7140 const struct breakpoint_ops *ops)
7141 {
7142 gdb_assert (ops != NULL);
7143
7144 b->ops = ops;
7145 b->type = bptype;
7146 b->gdbarch = gdbarch;
7147 b->language = current_language->la_language;
7148 b->input_radix = input_radix;
7149 b->related_breakpoint = b;
7150 }
7151
7152 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7153 that has type BPTYPE and has no locations as yet. */
7154
7155 static struct breakpoint *
7156 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7157 enum bptype bptype,
7158 const struct breakpoint_ops *ops)
7159 {
7160 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7161
7162 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7163 return add_to_breakpoint_chain (std::move (b));
7164 }
7165
7166 /* Initialize loc->function_name. */
7167
7168 static void
7169 set_breakpoint_location_function (struct bp_location *loc)
7170 {
7171 gdb_assert (loc->owner != NULL);
7172
7173 if (loc->owner->type == bp_breakpoint
7174 || loc->owner->type == bp_hardware_breakpoint
7175 || is_tracepoint (loc->owner))
7176 {
7177 const char *function_name;
7178
7179 if (loc->msymbol != NULL
7180 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
7181 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
7182 {
7183 struct breakpoint *b = loc->owner;
7184
7185 function_name = loc->msymbol->linkage_name ();
7186
7187 if (b->type == bp_breakpoint && b->loc == loc
7188 && loc->next == NULL && b->related_breakpoint == b)
7189 {
7190 /* Create only the whole new breakpoint of this type but do not
7191 mess more complicated breakpoints with multiple locations. */
7192 b->type = bp_gnu_ifunc_resolver;
7193 /* Remember the resolver's address for use by the return
7194 breakpoint. */
7195 loc->related_address = loc->address;
7196 }
7197 }
7198 else
7199 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7200
7201 if (function_name)
7202 loc->function_name = xstrdup (function_name);
7203 }
7204 }
7205
7206 /* Attempt to determine architecture of location identified by SAL. */
7207 struct gdbarch *
7208 get_sal_arch (struct symtab_and_line sal)
7209 {
7210 if (sal.section)
7211 return sal.section->objfile->arch ();
7212 if (sal.symtab)
7213 return SYMTAB_OBJFILE (sal.symtab)->arch ();
7214
7215 return NULL;
7216 }
7217
7218 /* Low level routine for partially initializing a breakpoint of type
7219 BPTYPE. The newly created breakpoint's address, section, source
7220 file name, and line number are provided by SAL.
7221
7222 It is expected that the caller will complete the initialization of
7223 the newly created breakpoint struct as well as output any status
7224 information regarding the creation of a new breakpoint. */
7225
7226 static void
7227 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7228 struct symtab_and_line sal, enum bptype bptype,
7229 const struct breakpoint_ops *ops)
7230 {
7231 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7232
7233 add_location_to_breakpoint (b, &sal);
7234
7235 if (bptype != bp_catchpoint)
7236 gdb_assert (sal.pspace != NULL);
7237
7238 /* Store the program space that was used to set the breakpoint,
7239 except for ordinary breakpoints, which are independent of the
7240 program space. */
7241 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7242 b->pspace = sal.pspace;
7243 }
7244
7245 /* set_raw_breakpoint is a low level routine for allocating and
7246 partially initializing a breakpoint of type BPTYPE. The newly
7247 created breakpoint's address, section, source file name, and line
7248 number are provided by SAL. The newly created and partially
7249 initialized breakpoint is added to the breakpoint chain and
7250 is also returned as the value of this function.
7251
7252 It is expected that the caller will complete the initialization of
7253 the newly created breakpoint struct as well as output any status
7254 information regarding the creation of a new breakpoint. In
7255 particular, set_raw_breakpoint does NOT set the breakpoint
7256 number! Care should be taken to not allow an error to occur
7257 prior to completing the initialization of the breakpoint. If this
7258 should happen, a bogus breakpoint will be left on the chain. */
7259
7260 struct breakpoint *
7261 set_raw_breakpoint (struct gdbarch *gdbarch,
7262 struct symtab_and_line sal, enum bptype bptype,
7263 const struct breakpoint_ops *ops)
7264 {
7265 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7266
7267 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7268 return add_to_breakpoint_chain (std::move (b));
7269 }
7270
7271 /* Call this routine when stepping and nexting to enable a breakpoint
7272 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7273 initiated the operation. */
7274
7275 void
7276 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7277 {
7278 struct breakpoint *b, *b_tmp;
7279 int thread = tp->global_num;
7280
7281 /* To avoid having to rescan all objfile symbols at every step,
7282 we maintain a list of continually-inserted but always disabled
7283 longjmp "master" breakpoints. Here, we simply create momentary
7284 clones of those and enable them for the requested thread. */
7285 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7286 if (b->pspace == current_program_space
7287 && (b->type == bp_longjmp_master
7288 || b->type == bp_exception_master))
7289 {
7290 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7291 struct breakpoint *clone;
7292
7293 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7294 after their removal. */
7295 clone = momentary_breakpoint_from_master (b, type,
7296 &momentary_breakpoint_ops, 1);
7297 clone->thread = thread;
7298 }
7299
7300 tp->initiating_frame = frame;
7301 }
7302
7303 /* Delete all longjmp breakpoints from THREAD. */
7304 void
7305 delete_longjmp_breakpoint (int thread)
7306 {
7307 struct breakpoint *b, *b_tmp;
7308
7309 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7310 if (b->type == bp_longjmp || b->type == bp_exception)
7311 {
7312 if (b->thread == thread)
7313 delete_breakpoint (b);
7314 }
7315 }
7316
7317 void
7318 delete_longjmp_breakpoint_at_next_stop (int thread)
7319 {
7320 struct breakpoint *b, *b_tmp;
7321
7322 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7323 if (b->type == bp_longjmp || b->type == bp_exception)
7324 {
7325 if (b->thread == thread)
7326 b->disposition = disp_del_at_next_stop;
7327 }
7328 }
7329
7330 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7331 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7332 pointer to any of them. Return NULL if this system cannot place longjmp
7333 breakpoints. */
7334
7335 struct breakpoint *
7336 set_longjmp_breakpoint_for_call_dummy (void)
7337 {
7338 struct breakpoint *b, *retval = NULL;
7339
7340 ALL_BREAKPOINTS (b)
7341 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7342 {
7343 struct breakpoint *new_b;
7344
7345 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7346 &momentary_breakpoint_ops,
7347 1);
7348 new_b->thread = inferior_thread ()->global_num;
7349
7350 /* Link NEW_B into the chain of RETVAL breakpoints. */
7351
7352 gdb_assert (new_b->related_breakpoint == new_b);
7353 if (retval == NULL)
7354 retval = new_b;
7355 new_b->related_breakpoint = retval;
7356 while (retval->related_breakpoint != new_b->related_breakpoint)
7357 retval = retval->related_breakpoint;
7358 retval->related_breakpoint = new_b;
7359 }
7360
7361 return retval;
7362 }
7363
7364 /* Verify all existing dummy frames and their associated breakpoints for
7365 TP. Remove those which can no longer be found in the current frame
7366 stack.
7367
7368 You should call this function only at places where it is safe to currently
7369 unwind the whole stack. Failed stack unwind would discard live dummy
7370 frames. */
7371
7372 void
7373 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7374 {
7375 struct breakpoint *b, *b_tmp;
7376
7377 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7378 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7379 {
7380 struct breakpoint *dummy_b = b->related_breakpoint;
7381
7382 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7383 dummy_b = dummy_b->related_breakpoint;
7384 if (dummy_b->type != bp_call_dummy
7385 || frame_find_by_id (dummy_b->frame_id) != NULL)
7386 continue;
7387
7388 dummy_frame_discard (dummy_b->frame_id, tp);
7389
7390 while (b->related_breakpoint != b)
7391 {
7392 if (b_tmp == b->related_breakpoint)
7393 b_tmp = b->related_breakpoint->next;
7394 delete_breakpoint (b->related_breakpoint);
7395 }
7396 delete_breakpoint (b);
7397 }
7398 }
7399
7400 void
7401 enable_overlay_breakpoints (void)
7402 {
7403 struct breakpoint *b;
7404
7405 ALL_BREAKPOINTS (b)
7406 if (b->type == bp_overlay_event)
7407 {
7408 b->enable_state = bp_enabled;
7409 update_global_location_list (UGLL_MAY_INSERT);
7410 overlay_events_enabled = 1;
7411 }
7412 }
7413
7414 void
7415 disable_overlay_breakpoints (void)
7416 {
7417 struct breakpoint *b;
7418
7419 ALL_BREAKPOINTS (b)
7420 if (b->type == bp_overlay_event)
7421 {
7422 b->enable_state = bp_disabled;
7423 update_global_location_list (UGLL_DONT_INSERT);
7424 overlay_events_enabled = 0;
7425 }
7426 }
7427
7428 /* Set an active std::terminate breakpoint for each std::terminate
7429 master breakpoint. */
7430 void
7431 set_std_terminate_breakpoint (void)
7432 {
7433 struct breakpoint *b, *b_tmp;
7434
7435 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7436 if (b->pspace == current_program_space
7437 && b->type == bp_std_terminate_master)
7438 {
7439 momentary_breakpoint_from_master (b, bp_std_terminate,
7440 &momentary_breakpoint_ops, 1);
7441 }
7442 }
7443
7444 /* Delete all the std::terminate breakpoints. */
7445 void
7446 delete_std_terminate_breakpoint (void)
7447 {
7448 struct breakpoint *b, *b_tmp;
7449
7450 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7451 if (b->type == bp_std_terminate)
7452 delete_breakpoint (b);
7453 }
7454
7455 struct breakpoint *
7456 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7457 {
7458 struct breakpoint *b;
7459
7460 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7461 &internal_breakpoint_ops);
7462
7463 b->enable_state = bp_enabled;
7464 /* location has to be used or breakpoint_re_set will delete me. */
7465 b->location = new_address_location (b->loc->address, NULL, 0);
7466
7467 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7468
7469 return b;
7470 }
7471
7472 struct lang_and_radix
7473 {
7474 enum language lang;
7475 int radix;
7476 };
7477
7478 /* Create a breakpoint for JIT code registration and unregistration. */
7479
7480 struct breakpoint *
7481 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7482 {
7483 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7484 &internal_breakpoint_ops);
7485 }
7486
7487 /* Remove JIT code registration and unregistration breakpoint(s). */
7488
7489 void
7490 remove_jit_event_breakpoints (void)
7491 {
7492 struct breakpoint *b, *b_tmp;
7493
7494 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7495 if (b->type == bp_jit_event
7496 && b->loc->pspace == current_program_space)
7497 delete_breakpoint (b);
7498 }
7499
7500 void
7501 remove_solib_event_breakpoints (void)
7502 {
7503 struct breakpoint *b, *b_tmp;
7504
7505 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7506 if (b->type == bp_shlib_event
7507 && b->loc->pspace == current_program_space)
7508 delete_breakpoint (b);
7509 }
7510
7511 /* See breakpoint.h. */
7512
7513 void
7514 remove_solib_event_breakpoints_at_next_stop (void)
7515 {
7516 struct breakpoint *b, *b_tmp;
7517
7518 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7519 if (b->type == bp_shlib_event
7520 && b->loc->pspace == current_program_space)
7521 b->disposition = disp_del_at_next_stop;
7522 }
7523
7524 /* Helper for create_solib_event_breakpoint /
7525 create_and_insert_solib_event_breakpoint. Allows specifying which
7526 INSERT_MODE to pass through to update_global_location_list. */
7527
7528 static struct breakpoint *
7529 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7530 enum ugll_insert_mode insert_mode)
7531 {
7532 struct breakpoint *b;
7533
7534 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7535 &internal_breakpoint_ops);
7536 update_global_location_list_nothrow (insert_mode);
7537 return b;
7538 }
7539
7540 struct breakpoint *
7541 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7542 {
7543 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7544 }
7545
7546 /* See breakpoint.h. */
7547
7548 struct breakpoint *
7549 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7550 {
7551 struct breakpoint *b;
7552
7553 /* Explicitly tell update_global_location_list to insert
7554 locations. */
7555 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7556 if (!b->loc->inserted)
7557 {
7558 delete_breakpoint (b);
7559 return NULL;
7560 }
7561 return b;
7562 }
7563
7564 /* Disable any breakpoints that are on code in shared libraries. Only
7565 apply to enabled breakpoints, disabled ones can just stay disabled. */
7566
7567 void
7568 disable_breakpoints_in_shlibs (void)
7569 {
7570 struct bp_location *loc, **locp_tmp;
7571
7572 ALL_BP_LOCATIONS (loc, locp_tmp)
7573 {
7574 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7575 struct breakpoint *b = loc->owner;
7576
7577 /* We apply the check to all breakpoints, including disabled for
7578 those with loc->duplicate set. This is so that when breakpoint
7579 becomes enabled, or the duplicate is removed, gdb will try to
7580 insert all breakpoints. If we don't set shlib_disabled here,
7581 we'll try to insert those breakpoints and fail. */
7582 if (((b->type == bp_breakpoint)
7583 || (b->type == bp_jit_event)
7584 || (b->type == bp_hardware_breakpoint)
7585 || (is_tracepoint (b)))
7586 && loc->pspace == current_program_space
7587 && !loc->shlib_disabled
7588 && solib_name_from_address (loc->pspace, loc->address)
7589 )
7590 {
7591 loc->shlib_disabled = 1;
7592 }
7593 }
7594 }
7595
7596 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7597 notification of unloaded_shlib. Only apply to enabled breakpoints,
7598 disabled ones can just stay disabled. */
7599
7600 static void
7601 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7602 {
7603 struct bp_location *loc, **locp_tmp;
7604 int disabled_shlib_breaks = 0;
7605
7606 ALL_BP_LOCATIONS (loc, locp_tmp)
7607 {
7608 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7609 struct breakpoint *b = loc->owner;
7610
7611 if (solib->pspace == loc->pspace
7612 && !loc->shlib_disabled
7613 && (((b->type == bp_breakpoint
7614 || b->type == bp_jit_event
7615 || b->type == bp_hardware_breakpoint)
7616 && (loc->loc_type == bp_loc_hardware_breakpoint
7617 || loc->loc_type == bp_loc_software_breakpoint))
7618 || is_tracepoint (b))
7619 && solib_contains_address_p (solib, loc->address))
7620 {
7621 loc->shlib_disabled = 1;
7622 /* At this point, we cannot rely on remove_breakpoint
7623 succeeding so we must mark the breakpoint as not inserted
7624 to prevent future errors occurring in remove_breakpoints. */
7625 loc->inserted = 0;
7626
7627 /* This may cause duplicate notifications for the same breakpoint. */
7628 gdb::observers::breakpoint_modified.notify (b);
7629
7630 if (!disabled_shlib_breaks)
7631 {
7632 target_terminal::ours_for_output ();
7633 warning (_("Temporarily disabling breakpoints "
7634 "for unloaded shared library \"%s\""),
7635 solib->so_name);
7636 }
7637 disabled_shlib_breaks = 1;
7638 }
7639 }
7640 }
7641
7642 /* Disable any breakpoints and tracepoints in OBJFILE upon
7643 notification of free_objfile. Only apply to enabled breakpoints,
7644 disabled ones can just stay disabled. */
7645
7646 static void
7647 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7648 {
7649 struct breakpoint *b;
7650
7651 if (objfile == NULL)
7652 return;
7653
7654 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7655 managed by the user with add-symbol-file/remove-symbol-file.
7656 Similarly to how breakpoints in shared libraries are handled in
7657 response to "nosharedlibrary", mark breakpoints in such modules
7658 shlib_disabled so they end up uninserted on the next global
7659 location list update. Shared libraries not loaded by the user
7660 aren't handled here -- they're already handled in
7661 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7662 solib_unloaded observer. We skip objfiles that are not
7663 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7664 main objfile). */
7665 if ((objfile->flags & OBJF_SHARED) == 0
7666 || (objfile->flags & OBJF_USERLOADED) == 0)
7667 return;
7668
7669 ALL_BREAKPOINTS (b)
7670 {
7671 struct bp_location *loc;
7672 int bp_modified = 0;
7673
7674 if (!is_breakpoint (b) && !is_tracepoint (b))
7675 continue;
7676
7677 for (loc = b->loc; loc != NULL; loc = loc->next)
7678 {
7679 CORE_ADDR loc_addr = loc->address;
7680
7681 if (loc->loc_type != bp_loc_hardware_breakpoint
7682 && loc->loc_type != bp_loc_software_breakpoint)
7683 continue;
7684
7685 if (loc->shlib_disabled != 0)
7686 continue;
7687
7688 if (objfile->pspace != loc->pspace)
7689 continue;
7690
7691 if (loc->loc_type != bp_loc_hardware_breakpoint
7692 && loc->loc_type != bp_loc_software_breakpoint)
7693 continue;
7694
7695 if (is_addr_in_objfile (loc_addr, objfile))
7696 {
7697 loc->shlib_disabled = 1;
7698 /* At this point, we don't know whether the object was
7699 unmapped from the inferior or not, so leave the
7700 inserted flag alone. We'll handle failure to
7701 uninsert quietly, in case the object was indeed
7702 unmapped. */
7703
7704 mark_breakpoint_location_modified (loc);
7705
7706 bp_modified = 1;
7707 }
7708 }
7709
7710 if (bp_modified)
7711 gdb::observers::breakpoint_modified.notify (b);
7712 }
7713 }
7714
7715 /* FORK & VFORK catchpoints. */
7716
7717 /* An instance of this type is used to represent a fork or vfork
7718 catchpoint. A breakpoint is really of this type iff its ops pointer points
7719 to CATCH_FORK_BREAKPOINT_OPS. */
7720
7721 struct fork_catchpoint : public breakpoint
7722 {
7723 /* Process id of a child process whose forking triggered this
7724 catchpoint. This field is only valid immediately after this
7725 catchpoint has triggered. */
7726 ptid_t forked_inferior_pid;
7727 };
7728
7729 /* Implement the "insert" breakpoint_ops method for fork
7730 catchpoints. */
7731
7732 static int
7733 insert_catch_fork (struct bp_location *bl)
7734 {
7735 return target_insert_fork_catchpoint (inferior_ptid.pid ());
7736 }
7737
7738 /* Implement the "remove" breakpoint_ops method for fork
7739 catchpoints. */
7740
7741 static int
7742 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
7743 {
7744 return target_remove_fork_catchpoint (inferior_ptid.pid ());
7745 }
7746
7747 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7748 catchpoints. */
7749
7750 static int
7751 breakpoint_hit_catch_fork (const struct bp_location *bl,
7752 const address_space *aspace, CORE_ADDR bp_addr,
7753 const struct target_waitstatus *ws)
7754 {
7755 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7756
7757 if (ws->kind != TARGET_WAITKIND_FORKED)
7758 return 0;
7759
7760 c->forked_inferior_pid = ws->value.related_pid;
7761 return 1;
7762 }
7763
7764 /* Implement the "print_it" breakpoint_ops method for fork
7765 catchpoints. */
7766
7767 static enum print_stop_action
7768 print_it_catch_fork (bpstat bs)
7769 {
7770 struct ui_out *uiout = current_uiout;
7771 struct breakpoint *b = bs->breakpoint_at;
7772 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7773
7774 annotate_catchpoint (b->number);
7775 maybe_print_thread_hit_breakpoint (uiout);
7776 if (b->disposition == disp_del)
7777 uiout->text ("Temporary catchpoint ");
7778 else
7779 uiout->text ("Catchpoint ");
7780 if (uiout->is_mi_like_p ())
7781 {
7782 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7783 uiout->field_string ("disp", bpdisp_text (b->disposition));
7784 }
7785 uiout->field_signed ("bkptno", b->number);
7786 uiout->text (" (forked process ");
7787 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7788 uiout->text ("), ");
7789 return PRINT_SRC_AND_LOC;
7790 }
7791
7792 /* Implement the "print_one" breakpoint_ops method for fork
7793 catchpoints. */
7794
7795 static void
7796 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7797 {
7798 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7799 struct value_print_options opts;
7800 struct ui_out *uiout = current_uiout;
7801
7802 get_user_print_options (&opts);
7803
7804 /* Field 4, the address, is omitted (which makes the columns not
7805 line up too nicely with the headers, but the effect is relatively
7806 readable). */
7807 if (opts.addressprint)
7808 uiout->field_skip ("addr");
7809 annotate_field (5);
7810 uiout->text ("fork");
7811 if (c->forked_inferior_pid != null_ptid)
7812 {
7813 uiout->text (", process ");
7814 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7815 uiout->spaces (1);
7816 }
7817
7818 if (uiout->is_mi_like_p ())
7819 uiout->field_string ("catch-type", "fork");
7820 }
7821
7822 /* Implement the "print_mention" breakpoint_ops method for fork
7823 catchpoints. */
7824
7825 static void
7826 print_mention_catch_fork (struct breakpoint *b)
7827 {
7828 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7829 }
7830
7831 /* Implement the "print_recreate" breakpoint_ops method for fork
7832 catchpoints. */
7833
7834 static void
7835 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7836 {
7837 fprintf_unfiltered (fp, "catch fork");
7838 print_recreate_thread (b, fp);
7839 }
7840
7841 /* The breakpoint_ops structure to be used in fork catchpoints. */
7842
7843 static struct breakpoint_ops catch_fork_breakpoint_ops;
7844
7845 /* Implement the "insert" breakpoint_ops method for vfork
7846 catchpoints. */
7847
7848 static int
7849 insert_catch_vfork (struct bp_location *bl)
7850 {
7851 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
7852 }
7853
7854 /* Implement the "remove" breakpoint_ops method for vfork
7855 catchpoints. */
7856
7857 static int
7858 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
7859 {
7860 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
7861 }
7862
7863 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7864 catchpoints. */
7865
7866 static int
7867 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7868 const address_space *aspace, CORE_ADDR bp_addr,
7869 const struct target_waitstatus *ws)
7870 {
7871 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7872
7873 if (ws->kind != TARGET_WAITKIND_VFORKED)
7874 return 0;
7875
7876 c->forked_inferior_pid = ws->value.related_pid;
7877 return 1;
7878 }
7879
7880 /* Implement the "print_it" breakpoint_ops method for vfork
7881 catchpoints. */
7882
7883 static enum print_stop_action
7884 print_it_catch_vfork (bpstat bs)
7885 {
7886 struct ui_out *uiout = current_uiout;
7887 struct breakpoint *b = bs->breakpoint_at;
7888 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7889
7890 annotate_catchpoint (b->number);
7891 maybe_print_thread_hit_breakpoint (uiout);
7892 if (b->disposition == disp_del)
7893 uiout->text ("Temporary catchpoint ");
7894 else
7895 uiout->text ("Catchpoint ");
7896 if (uiout->is_mi_like_p ())
7897 {
7898 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7899 uiout->field_string ("disp", bpdisp_text (b->disposition));
7900 }
7901 uiout->field_signed ("bkptno", b->number);
7902 uiout->text (" (vforked process ");
7903 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7904 uiout->text ("), ");
7905 return PRINT_SRC_AND_LOC;
7906 }
7907
7908 /* Implement the "print_one" breakpoint_ops method for vfork
7909 catchpoints. */
7910
7911 static void
7912 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7913 {
7914 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7915 struct value_print_options opts;
7916 struct ui_out *uiout = current_uiout;
7917
7918 get_user_print_options (&opts);
7919 /* Field 4, the address, is omitted (which makes the columns not
7920 line up too nicely with the headers, but the effect is relatively
7921 readable). */
7922 if (opts.addressprint)
7923 uiout->field_skip ("addr");
7924 annotate_field (5);
7925 uiout->text ("vfork");
7926 if (c->forked_inferior_pid != null_ptid)
7927 {
7928 uiout->text (", process ");
7929 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7930 uiout->spaces (1);
7931 }
7932
7933 if (uiout->is_mi_like_p ())
7934 uiout->field_string ("catch-type", "vfork");
7935 }
7936
7937 /* Implement the "print_mention" breakpoint_ops method for vfork
7938 catchpoints. */
7939
7940 static void
7941 print_mention_catch_vfork (struct breakpoint *b)
7942 {
7943 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7944 }
7945
7946 /* Implement the "print_recreate" breakpoint_ops method for vfork
7947 catchpoints. */
7948
7949 static void
7950 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7951 {
7952 fprintf_unfiltered (fp, "catch vfork");
7953 print_recreate_thread (b, fp);
7954 }
7955
7956 /* The breakpoint_ops structure to be used in vfork catchpoints. */
7957
7958 static struct breakpoint_ops catch_vfork_breakpoint_ops;
7959
7960 /* An instance of this type is used to represent an solib catchpoint.
7961 A breakpoint is really of this type iff its ops pointer points to
7962 CATCH_SOLIB_BREAKPOINT_OPS. */
7963
7964 struct solib_catchpoint : public breakpoint
7965 {
7966 ~solib_catchpoint () override;
7967
7968 /* True for "catch load", false for "catch unload". */
7969 bool is_load;
7970
7971 /* Regular expression to match, if any. COMPILED is only valid when
7972 REGEX is non-NULL. */
7973 char *regex;
7974 std::unique_ptr<compiled_regex> compiled;
7975 };
7976
7977 solib_catchpoint::~solib_catchpoint ()
7978 {
7979 xfree (this->regex);
7980 }
7981
7982 static int
7983 insert_catch_solib (struct bp_location *ignore)
7984 {
7985 return 0;
7986 }
7987
7988 static int
7989 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
7990 {
7991 return 0;
7992 }
7993
7994 static int
7995 breakpoint_hit_catch_solib (const struct bp_location *bl,
7996 const address_space *aspace,
7997 CORE_ADDR bp_addr,
7998 const struct target_waitstatus *ws)
7999 {
8000 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8001 struct breakpoint *other;
8002
8003 if (ws->kind == TARGET_WAITKIND_LOADED)
8004 return 1;
8005
8006 ALL_BREAKPOINTS (other)
8007 {
8008 struct bp_location *other_bl;
8009
8010 if (other == bl->owner)
8011 continue;
8012
8013 if (other->type != bp_shlib_event)
8014 continue;
8015
8016 if (self->pspace != NULL && other->pspace != self->pspace)
8017 continue;
8018
8019 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8020 {
8021 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8022 return 1;
8023 }
8024 }
8025
8026 return 0;
8027 }
8028
8029 static void
8030 check_status_catch_solib (struct bpstats *bs)
8031 {
8032 struct solib_catchpoint *self
8033 = (struct solib_catchpoint *) bs->breakpoint_at;
8034
8035 if (self->is_load)
8036 {
8037 for (so_list *iter : current_program_space->added_solibs)
8038 {
8039 if (!self->regex
8040 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
8041 return;
8042 }
8043 }
8044 else
8045 {
8046 for (const std::string &iter : current_program_space->deleted_solibs)
8047 {
8048 if (!self->regex
8049 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
8050 return;
8051 }
8052 }
8053
8054 bs->stop = 0;
8055 bs->print_it = print_it_noop;
8056 }
8057
8058 static enum print_stop_action
8059 print_it_catch_solib (bpstat bs)
8060 {
8061 struct breakpoint *b = bs->breakpoint_at;
8062 struct ui_out *uiout = current_uiout;
8063
8064 annotate_catchpoint (b->number);
8065 maybe_print_thread_hit_breakpoint (uiout);
8066 if (b->disposition == disp_del)
8067 uiout->text ("Temporary catchpoint ");
8068 else
8069 uiout->text ("Catchpoint ");
8070 uiout->field_signed ("bkptno", b->number);
8071 uiout->text ("\n");
8072 if (uiout->is_mi_like_p ())
8073 uiout->field_string ("disp", bpdisp_text (b->disposition));
8074 print_solib_event (1);
8075 return PRINT_SRC_AND_LOC;
8076 }
8077
8078 static void
8079 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8080 {
8081 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8082 struct value_print_options opts;
8083 struct ui_out *uiout = current_uiout;
8084
8085 get_user_print_options (&opts);
8086 /* Field 4, the address, is omitted (which makes the columns not
8087 line up too nicely with the headers, but the effect is relatively
8088 readable). */
8089 if (opts.addressprint)
8090 {
8091 annotate_field (4);
8092 uiout->field_skip ("addr");
8093 }
8094
8095 std::string msg;
8096 annotate_field (5);
8097 if (self->is_load)
8098 {
8099 if (self->regex)
8100 msg = string_printf (_("load of library matching %s"), self->regex);
8101 else
8102 msg = _("load of library");
8103 }
8104 else
8105 {
8106 if (self->regex)
8107 msg = string_printf (_("unload of library matching %s"), self->regex);
8108 else
8109 msg = _("unload of library");
8110 }
8111 uiout->field_string ("what", msg);
8112
8113 if (uiout->is_mi_like_p ())
8114 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8115 }
8116
8117 static void
8118 print_mention_catch_solib (struct breakpoint *b)
8119 {
8120 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8121
8122 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8123 self->is_load ? "load" : "unload");
8124 }
8125
8126 static void
8127 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8128 {
8129 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8130
8131 fprintf_unfiltered (fp, "%s %s",
8132 b->disposition == disp_del ? "tcatch" : "catch",
8133 self->is_load ? "load" : "unload");
8134 if (self->regex)
8135 fprintf_unfiltered (fp, " %s", self->regex);
8136 fprintf_unfiltered (fp, "\n");
8137 }
8138
8139 static struct breakpoint_ops catch_solib_breakpoint_ops;
8140
8141 /* See breakpoint.h. */
8142
8143 void
8144 add_solib_catchpoint (const char *arg, bool is_load, bool is_temp, bool enabled)
8145 {
8146 struct gdbarch *gdbarch = get_current_arch ();
8147
8148 if (!arg)
8149 arg = "";
8150 arg = skip_spaces (arg);
8151
8152 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
8153
8154 if (*arg != '\0')
8155 {
8156 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8157 _("Invalid regexp")));
8158 c->regex = xstrdup (arg);
8159 }
8160
8161 c->is_load = is_load;
8162 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
8163 &catch_solib_breakpoint_ops);
8164
8165 c->enable_state = enabled ? bp_enabled : bp_disabled;
8166
8167 install_breakpoint (0, std::move (c), 1);
8168 }
8169
8170 /* A helper function that does all the work for "catch load" and
8171 "catch unload". */
8172
8173 static void
8174 catch_load_or_unload (const char *arg, int from_tty, int is_load,
8175 struct cmd_list_element *command)
8176 {
8177 const int enabled = 1;
8178 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
8179
8180 add_solib_catchpoint (arg, is_load, temp, enabled);
8181 }
8182
8183 static void
8184 catch_load_command_1 (const char *arg, int from_tty,
8185 struct cmd_list_element *command)
8186 {
8187 catch_load_or_unload (arg, from_tty, 1, command);
8188 }
8189
8190 static void
8191 catch_unload_command_1 (const char *arg, int from_tty,
8192 struct cmd_list_element *command)
8193 {
8194 catch_load_or_unload (arg, from_tty, 0, command);
8195 }
8196
8197 /* See breakpoint.h. */
8198
8199 void
8200 init_catchpoint (struct breakpoint *b,
8201 struct gdbarch *gdbarch, bool temp,
8202 const char *cond_string,
8203 const struct breakpoint_ops *ops)
8204 {
8205 symtab_and_line sal;
8206 sal.pspace = current_program_space;
8207
8208 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8209
8210 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8211 b->disposition = temp ? disp_del : disp_donttouch;
8212 }
8213
8214 void
8215 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8216 {
8217 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8218 set_breakpoint_number (internal, b);
8219 if (is_tracepoint (b))
8220 set_tracepoint_count (breakpoint_count);
8221 if (!internal)
8222 mention (b);
8223 gdb::observers::breakpoint_created.notify (b);
8224
8225 if (update_gll)
8226 update_global_location_list (UGLL_MAY_INSERT);
8227 }
8228
8229 static void
8230 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8231 bool temp, const char *cond_string,
8232 const struct breakpoint_ops *ops)
8233 {
8234 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
8235
8236 init_catchpoint (c.get (), gdbarch, temp, cond_string, ops);
8237
8238 c->forked_inferior_pid = null_ptid;
8239
8240 install_breakpoint (0, std::move (c), 1);
8241 }
8242
8243 /* Exec catchpoints. */
8244
8245 /* An instance of this type is used to represent an exec catchpoint.
8246 A breakpoint is really of this type iff its ops pointer points to
8247 CATCH_EXEC_BREAKPOINT_OPS. */
8248
8249 struct exec_catchpoint : public breakpoint
8250 {
8251 ~exec_catchpoint () override;
8252
8253 /* Filename of a program whose exec triggered this catchpoint.
8254 This field is only valid immediately after this catchpoint has
8255 triggered. */
8256 char *exec_pathname;
8257 };
8258
8259 /* Exec catchpoint destructor. */
8260
8261 exec_catchpoint::~exec_catchpoint ()
8262 {
8263 xfree (this->exec_pathname);
8264 }
8265
8266 static int
8267 insert_catch_exec (struct bp_location *bl)
8268 {
8269 return target_insert_exec_catchpoint (inferior_ptid.pid ());
8270 }
8271
8272 static int
8273 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8274 {
8275 return target_remove_exec_catchpoint (inferior_ptid.pid ());
8276 }
8277
8278 static int
8279 breakpoint_hit_catch_exec (const struct bp_location *bl,
8280 const address_space *aspace, CORE_ADDR bp_addr,
8281 const struct target_waitstatus *ws)
8282 {
8283 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8284
8285 if (ws->kind != TARGET_WAITKIND_EXECD)
8286 return 0;
8287
8288 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8289 return 1;
8290 }
8291
8292 static enum print_stop_action
8293 print_it_catch_exec (bpstat bs)
8294 {
8295 struct ui_out *uiout = current_uiout;
8296 struct breakpoint *b = bs->breakpoint_at;
8297 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8298
8299 annotate_catchpoint (b->number);
8300 maybe_print_thread_hit_breakpoint (uiout);
8301 if (b->disposition == disp_del)
8302 uiout->text ("Temporary catchpoint ");
8303 else
8304 uiout->text ("Catchpoint ");
8305 if (uiout->is_mi_like_p ())
8306 {
8307 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8308 uiout->field_string ("disp", bpdisp_text (b->disposition));
8309 }
8310 uiout->field_signed ("bkptno", b->number);
8311 uiout->text (" (exec'd ");
8312 uiout->field_string ("new-exec", c->exec_pathname);
8313 uiout->text ("), ");
8314
8315 return PRINT_SRC_AND_LOC;
8316 }
8317
8318 static void
8319 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8320 {
8321 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8322 struct value_print_options opts;
8323 struct ui_out *uiout = current_uiout;
8324
8325 get_user_print_options (&opts);
8326
8327 /* Field 4, the address, is omitted (which makes the columns
8328 not line up too nicely with the headers, but the effect
8329 is relatively readable). */
8330 if (opts.addressprint)
8331 uiout->field_skip ("addr");
8332 annotate_field (5);
8333 uiout->text ("exec");
8334 if (c->exec_pathname != NULL)
8335 {
8336 uiout->text (", program \"");
8337 uiout->field_string ("what", c->exec_pathname);
8338 uiout->text ("\" ");
8339 }
8340
8341 if (uiout->is_mi_like_p ())
8342 uiout->field_string ("catch-type", "exec");
8343 }
8344
8345 static void
8346 print_mention_catch_exec (struct breakpoint *b)
8347 {
8348 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8349 }
8350
8351 /* Implement the "print_recreate" breakpoint_ops method for exec
8352 catchpoints. */
8353
8354 static void
8355 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8356 {
8357 fprintf_unfiltered (fp, "catch exec");
8358 print_recreate_thread (b, fp);
8359 }
8360
8361 static struct breakpoint_ops catch_exec_breakpoint_ops;
8362
8363 static int
8364 hw_breakpoint_used_count (void)
8365 {
8366 int i = 0;
8367 struct breakpoint *b;
8368 struct bp_location *bl;
8369
8370 ALL_BREAKPOINTS (b)
8371 {
8372 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8373 for (bl = b->loc; bl; bl = bl->next)
8374 {
8375 /* Special types of hardware breakpoints may use more than
8376 one register. */
8377 i += b->ops->resources_needed (bl);
8378 }
8379 }
8380
8381 return i;
8382 }
8383
8384 /* Returns the resources B would use if it were a hardware
8385 watchpoint. */
8386
8387 static int
8388 hw_watchpoint_use_count (struct breakpoint *b)
8389 {
8390 int i = 0;
8391 struct bp_location *bl;
8392
8393 if (!breakpoint_enabled (b))
8394 return 0;
8395
8396 for (bl = b->loc; bl; bl = bl->next)
8397 {
8398 /* Special types of hardware watchpoints may use more than
8399 one register. */
8400 i += b->ops->resources_needed (bl);
8401 }
8402
8403 return i;
8404 }
8405
8406 /* Returns the sum the used resources of all hardware watchpoints of
8407 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8408 the sum of the used resources of all hardware watchpoints of other
8409 types _not_ TYPE. */
8410
8411 static int
8412 hw_watchpoint_used_count_others (struct breakpoint *except,
8413 enum bptype type, int *other_type_used)
8414 {
8415 int i = 0;
8416 struct breakpoint *b;
8417
8418 *other_type_used = 0;
8419 ALL_BREAKPOINTS (b)
8420 {
8421 if (b == except)
8422 continue;
8423 if (!breakpoint_enabled (b))
8424 continue;
8425
8426 if (b->type == type)
8427 i += hw_watchpoint_use_count (b);
8428 else if (is_hardware_watchpoint (b))
8429 *other_type_used = 1;
8430 }
8431
8432 return i;
8433 }
8434
8435 void
8436 disable_watchpoints_before_interactive_call_start (void)
8437 {
8438 struct breakpoint *b;
8439
8440 ALL_BREAKPOINTS (b)
8441 {
8442 if (is_watchpoint (b) && breakpoint_enabled (b))
8443 {
8444 b->enable_state = bp_call_disabled;
8445 update_global_location_list (UGLL_DONT_INSERT);
8446 }
8447 }
8448 }
8449
8450 void
8451 enable_watchpoints_after_interactive_call_stop (void)
8452 {
8453 struct breakpoint *b;
8454
8455 ALL_BREAKPOINTS (b)
8456 {
8457 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8458 {
8459 b->enable_state = bp_enabled;
8460 update_global_location_list (UGLL_MAY_INSERT);
8461 }
8462 }
8463 }
8464
8465 void
8466 disable_breakpoints_before_startup (void)
8467 {
8468 current_program_space->executing_startup = 1;
8469 update_global_location_list (UGLL_DONT_INSERT);
8470 }
8471
8472 void
8473 enable_breakpoints_after_startup (void)
8474 {
8475 current_program_space->executing_startup = 0;
8476 breakpoint_re_set ();
8477 }
8478
8479 /* Create a new single-step breakpoint for thread THREAD, with no
8480 locations. */
8481
8482 static struct breakpoint *
8483 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8484 {
8485 std::unique_ptr<breakpoint> b (new breakpoint ());
8486
8487 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
8488 &momentary_breakpoint_ops);
8489
8490 b->disposition = disp_donttouch;
8491 b->frame_id = null_frame_id;
8492
8493 b->thread = thread;
8494 gdb_assert (b->thread != 0);
8495
8496 return add_to_breakpoint_chain (std::move (b));
8497 }
8498
8499 /* Set a momentary breakpoint of type TYPE at address specified by
8500 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8501 frame. */
8502
8503 breakpoint_up
8504 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8505 struct frame_id frame_id, enum bptype type)
8506 {
8507 struct breakpoint *b;
8508
8509 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8510 tail-called one. */
8511 gdb_assert (!frame_id_artificial_p (frame_id));
8512
8513 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8514 b->enable_state = bp_enabled;
8515 b->disposition = disp_donttouch;
8516 b->frame_id = frame_id;
8517
8518 b->thread = inferior_thread ()->global_num;
8519
8520 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8521
8522 return breakpoint_up (b);
8523 }
8524
8525 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8526 The new breakpoint will have type TYPE, use OPS as its
8527 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8528
8529 static struct breakpoint *
8530 momentary_breakpoint_from_master (struct breakpoint *orig,
8531 enum bptype type,
8532 const struct breakpoint_ops *ops,
8533 int loc_enabled)
8534 {
8535 struct breakpoint *copy;
8536
8537 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8538 copy->loc = allocate_bp_location (copy);
8539 set_breakpoint_location_function (copy->loc);
8540
8541 copy->loc->gdbarch = orig->loc->gdbarch;
8542 copy->loc->requested_address = orig->loc->requested_address;
8543 copy->loc->address = orig->loc->address;
8544 copy->loc->section = orig->loc->section;
8545 copy->loc->pspace = orig->loc->pspace;
8546 copy->loc->probe = orig->loc->probe;
8547 copy->loc->line_number = orig->loc->line_number;
8548 copy->loc->symtab = orig->loc->symtab;
8549 copy->loc->enabled = loc_enabled;
8550 copy->frame_id = orig->frame_id;
8551 copy->thread = orig->thread;
8552 copy->pspace = orig->pspace;
8553
8554 copy->enable_state = bp_enabled;
8555 copy->disposition = disp_donttouch;
8556 copy->number = internal_breakpoint_number--;
8557
8558 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8559 return copy;
8560 }
8561
8562 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8563 ORIG is NULL. */
8564
8565 struct breakpoint *
8566 clone_momentary_breakpoint (struct breakpoint *orig)
8567 {
8568 /* If there's nothing to clone, then return nothing. */
8569 if (orig == NULL)
8570 return NULL;
8571
8572 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8573 }
8574
8575 breakpoint_up
8576 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8577 enum bptype type)
8578 {
8579 struct symtab_and_line sal;
8580
8581 sal = find_pc_line (pc, 0);
8582 sal.pc = pc;
8583 sal.section = find_pc_overlay (pc);
8584 sal.explicit_pc = 1;
8585
8586 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8587 }
8588 \f
8589
8590 /* Tell the user we have just set a breakpoint B. */
8591
8592 static void
8593 mention (struct breakpoint *b)
8594 {
8595 b->ops->print_mention (b);
8596 current_uiout->text ("\n");
8597 }
8598 \f
8599
8600 static bool bp_loc_is_permanent (struct bp_location *loc);
8601
8602 /* Handle "set breakpoint auto-hw on".
8603
8604 If the explicitly specified breakpoint type is not hardware
8605 breakpoint, check the memory map to see whether the breakpoint
8606 address is in read-only memory.
8607
8608 - location type is not hardware breakpoint, memory is read-only.
8609 We change the type of the location to hardware breakpoint.
8610
8611 - location type is hardware breakpoint, memory is read-write. This
8612 means we've previously made the location hardware one, but then the
8613 memory map changed, so we undo.
8614 */
8615
8616 static void
8617 handle_automatic_hardware_breakpoints (bp_location *bl)
8618 {
8619 if (automatic_hardware_breakpoints
8620 && bl->owner->type != bp_hardware_breakpoint
8621 && (bl->loc_type == bp_loc_software_breakpoint
8622 || bl->loc_type == bp_loc_hardware_breakpoint))
8623 {
8624 /* When breakpoints are removed, remove_breakpoints will use
8625 location types we've just set here, the only possible problem
8626 is that memory map has changed during running program, but
8627 it's not going to work anyway with current gdb. */
8628 mem_region *mr = lookup_mem_region (bl->address);
8629
8630 if (mr != nullptr)
8631 {
8632 enum bp_loc_type new_type;
8633
8634 if (mr->attrib.mode != MEM_RW)
8635 new_type = bp_loc_hardware_breakpoint;
8636 else
8637 new_type = bp_loc_software_breakpoint;
8638
8639 if (new_type != bl->loc_type)
8640 {
8641 static bool said = false;
8642
8643 bl->loc_type = new_type;
8644 if (!said)
8645 {
8646 fprintf_filtered (gdb_stdout,
8647 _("Note: automatically using "
8648 "hardware breakpoints for "
8649 "read-only addresses.\n"));
8650 said = true;
8651 }
8652 }
8653 }
8654 }
8655 }
8656
8657 static struct bp_location *
8658 add_location_to_breakpoint (struct breakpoint *b,
8659 const struct symtab_and_line *sal)
8660 {
8661 struct bp_location *loc, **tmp;
8662 CORE_ADDR adjusted_address;
8663 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8664
8665 if (loc_gdbarch == NULL)
8666 loc_gdbarch = b->gdbarch;
8667
8668 /* Adjust the breakpoint's address prior to allocating a location.
8669 Once we call allocate_bp_location(), that mostly uninitialized
8670 location will be placed on the location chain. Adjustment of the
8671 breakpoint may cause target_read_memory() to be called and we do
8672 not want its scan of the location chain to find a breakpoint and
8673 location that's only been partially initialized. */
8674 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8675 sal->pc, b->type);
8676
8677 /* Sort the locations by their ADDRESS. */
8678 loc = allocate_bp_location (b);
8679 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8680 tmp = &((*tmp)->next))
8681 ;
8682 loc->next = *tmp;
8683 *tmp = loc;
8684
8685 loc->requested_address = sal->pc;
8686 loc->address = adjusted_address;
8687 loc->pspace = sal->pspace;
8688 loc->probe.prob = sal->prob;
8689 loc->probe.objfile = sal->objfile;
8690 gdb_assert (loc->pspace != NULL);
8691 loc->section = sal->section;
8692 loc->gdbarch = loc_gdbarch;
8693 loc->line_number = sal->line;
8694 loc->symtab = sal->symtab;
8695 loc->symbol = sal->symbol;
8696 loc->msymbol = sal->msymbol;
8697 loc->objfile = sal->objfile;
8698
8699 set_breakpoint_location_function (loc);
8700
8701 /* While by definition, permanent breakpoints are already present in the
8702 code, we don't mark the location as inserted. Normally one would expect
8703 that GDB could rely on that breakpoint instruction to stop the program,
8704 thus removing the need to insert its own breakpoint, except that executing
8705 the breakpoint instruction can kill the target instead of reporting a
8706 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8707 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8708 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8709 breakpoint be inserted normally results in QEMU knowing about the GDB
8710 breakpoint, and thus trap before the breakpoint instruction is executed.
8711 (If GDB later needs to continue execution past the permanent breakpoint,
8712 it manually increments the PC, thus avoiding executing the breakpoint
8713 instruction.) */
8714 if (bp_loc_is_permanent (loc))
8715 loc->permanent = 1;
8716
8717 return loc;
8718 }
8719 \f
8720
8721 /* Return true if LOC is pointing to a permanent breakpoint,
8722 return false otherwise. */
8723
8724 static bool
8725 bp_loc_is_permanent (struct bp_location *loc)
8726 {
8727 gdb_assert (loc != NULL);
8728
8729 /* If we have a non-breakpoint-backed catchpoint or a software
8730 watchpoint, just return 0. We should not attempt to read from
8731 the addresses the locations of these breakpoint types point to.
8732 gdbarch_program_breakpoint_here_p, below, will attempt to read
8733 memory. */
8734 if (!bl_address_is_meaningful (loc))
8735 return false;
8736
8737 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8738 switch_to_program_space_and_thread (loc->pspace);
8739 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
8740 }
8741
8742 /* Build a command list for the dprintf corresponding to the current
8743 settings of the dprintf style options. */
8744
8745 static void
8746 update_dprintf_command_list (struct breakpoint *b)
8747 {
8748 char *dprintf_args = b->extra_string;
8749 char *printf_line = NULL;
8750
8751 if (!dprintf_args)
8752 return;
8753
8754 dprintf_args = skip_spaces (dprintf_args);
8755
8756 /* Allow a comma, as it may have terminated a location, but don't
8757 insist on it. */
8758 if (*dprintf_args == ',')
8759 ++dprintf_args;
8760 dprintf_args = skip_spaces (dprintf_args);
8761
8762 if (*dprintf_args != '"')
8763 error (_("Bad format string, missing '\"'."));
8764
8765 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8766 printf_line = xstrprintf ("printf %s", dprintf_args);
8767 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8768 {
8769 if (!dprintf_function)
8770 error (_("No function supplied for dprintf call"));
8771
8772 if (dprintf_channel && strlen (dprintf_channel) > 0)
8773 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8774 dprintf_function,
8775 dprintf_channel,
8776 dprintf_args);
8777 else
8778 printf_line = xstrprintf ("call (void) %s (%s)",
8779 dprintf_function,
8780 dprintf_args);
8781 }
8782 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8783 {
8784 if (target_can_run_breakpoint_commands ())
8785 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8786 else
8787 {
8788 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8789 printf_line = xstrprintf ("printf %s", dprintf_args);
8790 }
8791 }
8792 else
8793 internal_error (__FILE__, __LINE__,
8794 _("Invalid dprintf style."));
8795
8796 gdb_assert (printf_line != NULL);
8797
8798 /* Manufacture a printf sequence. */
8799 struct command_line *printf_cmd_line
8800 = new struct command_line (simple_control, printf_line);
8801 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8802 command_lines_deleter ()));
8803 }
8804
8805 /* Update all dprintf commands, making their command lists reflect
8806 current style settings. */
8807
8808 static void
8809 update_dprintf_commands (const char *args, int from_tty,
8810 struct cmd_list_element *c)
8811 {
8812 struct breakpoint *b;
8813
8814 ALL_BREAKPOINTS (b)
8815 {
8816 if (b->type == bp_dprintf)
8817 update_dprintf_command_list (b);
8818 }
8819 }
8820
8821 /* Create a breakpoint with SAL as location. Use LOCATION
8822 as a description of the location, and COND_STRING
8823 as condition expression. If LOCATION is NULL then create an
8824 "address location" from the address in the SAL. */
8825
8826 static void
8827 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8828 gdb::array_view<const symtab_and_line> sals,
8829 event_location_up &&location,
8830 gdb::unique_xmalloc_ptr<char> filter,
8831 gdb::unique_xmalloc_ptr<char> cond_string,
8832 gdb::unique_xmalloc_ptr<char> extra_string,
8833 enum bptype type, enum bpdisp disposition,
8834 int thread, int task, int ignore_count,
8835 const struct breakpoint_ops *ops, int from_tty,
8836 int enabled, int internal, unsigned flags,
8837 int display_canonical)
8838 {
8839 int i;
8840
8841 if (type == bp_hardware_breakpoint)
8842 {
8843 int target_resources_ok;
8844
8845 i = hw_breakpoint_used_count ();
8846 target_resources_ok =
8847 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8848 i + 1, 0);
8849 if (target_resources_ok == 0)
8850 error (_("No hardware breakpoint support in the target."));
8851 else if (target_resources_ok < 0)
8852 error (_("Hardware breakpoints used exceeds limit."));
8853 }
8854
8855 gdb_assert (!sals.empty ());
8856
8857 for (const auto &sal : sals)
8858 {
8859 struct bp_location *loc;
8860
8861 if (from_tty)
8862 {
8863 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8864 if (!loc_gdbarch)
8865 loc_gdbarch = gdbarch;
8866
8867 describe_other_breakpoints (loc_gdbarch,
8868 sal.pspace, sal.pc, sal.section, thread);
8869 }
8870
8871 if (&sal == &sals[0])
8872 {
8873 init_raw_breakpoint (b, gdbarch, sal, type, ops);
8874 b->thread = thread;
8875 b->task = task;
8876
8877 b->cond_string = cond_string.release ();
8878 b->extra_string = extra_string.release ();
8879 b->ignore_count = ignore_count;
8880 b->enable_state = enabled ? bp_enabled : bp_disabled;
8881 b->disposition = disposition;
8882
8883 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8884 b->loc->inserted = 1;
8885
8886 if (type == bp_static_tracepoint)
8887 {
8888 struct tracepoint *t = (struct tracepoint *) b;
8889 struct static_tracepoint_marker marker;
8890
8891 if (strace_marker_p (b))
8892 {
8893 /* We already know the marker exists, otherwise, we
8894 wouldn't see a sal for it. */
8895 const char *p
8896 = &event_location_to_string (b->location.get ())[3];
8897 const char *endp;
8898
8899 p = skip_spaces (p);
8900
8901 endp = skip_to_space (p);
8902
8903 t->static_trace_marker_id.assign (p, endp - p);
8904
8905 printf_filtered (_("Probed static tracepoint "
8906 "marker \"%s\"\n"),
8907 t->static_trace_marker_id.c_str ());
8908 }
8909 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8910 {
8911 t->static_trace_marker_id = std::move (marker.str_id);
8912
8913 printf_filtered (_("Probed static tracepoint "
8914 "marker \"%s\"\n"),
8915 t->static_trace_marker_id.c_str ());
8916 }
8917 else
8918 warning (_("Couldn't determine the static "
8919 "tracepoint marker to probe"));
8920 }
8921
8922 loc = b->loc;
8923 }
8924 else
8925 {
8926 loc = add_location_to_breakpoint (b, &sal);
8927 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8928 loc->inserted = 1;
8929 }
8930
8931 /* Do not set breakpoint locations conditions yet. As locations
8932 are inserted, they get sorted based on their addresses. Let
8933 the list stabilize to have reliable location numbers. */
8934
8935 /* Dynamic printf requires and uses additional arguments on the
8936 command line, otherwise it's an error. */
8937 if (type == bp_dprintf)
8938 {
8939 if (b->extra_string)
8940 update_dprintf_command_list (b);
8941 else
8942 error (_("Format string required"));
8943 }
8944 else if (b->extra_string)
8945 error (_("Garbage '%s' at end of command"), b->extra_string);
8946 }
8947
8948
8949 /* The order of the locations is now stable. Set the location
8950 condition using the location's number. */
8951 int loc_num = 1;
8952 for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)
8953 {
8954 if (b->cond_string != nullptr)
8955 set_breakpoint_location_condition (b->cond_string, loc, b->number,
8956 loc_num);
8957
8958 ++loc_num;
8959 }
8960
8961 b->display_canonical = display_canonical;
8962 if (location != NULL)
8963 b->location = std::move (location);
8964 else
8965 b->location = new_address_location (b->loc->address, NULL, 0);
8966 b->filter = std::move (filter);
8967 }
8968
8969 static void
8970 create_breakpoint_sal (struct gdbarch *gdbarch,
8971 gdb::array_view<const symtab_and_line> sals,
8972 event_location_up &&location,
8973 gdb::unique_xmalloc_ptr<char> filter,
8974 gdb::unique_xmalloc_ptr<char> cond_string,
8975 gdb::unique_xmalloc_ptr<char> extra_string,
8976 enum bptype type, enum bpdisp disposition,
8977 int thread, int task, int ignore_count,
8978 const struct breakpoint_ops *ops, int from_tty,
8979 int enabled, int internal, unsigned flags,
8980 int display_canonical)
8981 {
8982 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
8983
8984 init_breakpoint_sal (b.get (), gdbarch,
8985 sals, std::move (location),
8986 std::move (filter),
8987 std::move (cond_string),
8988 std::move (extra_string),
8989 type, disposition,
8990 thread, task, ignore_count,
8991 ops, from_tty,
8992 enabled, internal, flags,
8993 display_canonical);
8994
8995 install_breakpoint (internal, std::move (b), 0);
8996 }
8997
8998 /* Add SALS.nelts breakpoints to the breakpoint table. For each
8999 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9000 value. COND_STRING, if not NULL, specified the condition to be
9001 used for all breakpoints. Essentially the only case where
9002 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9003 function. In that case, it's still not possible to specify
9004 separate conditions for different overloaded functions, so
9005 we take just a single condition string.
9006
9007 NOTE: If the function succeeds, the caller is expected to cleanup
9008 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9009 array contents). If the function fails (error() is called), the
9010 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9011 COND and SALS arrays and each of those arrays contents. */
9012
9013 static void
9014 create_breakpoints_sal (struct gdbarch *gdbarch,
9015 struct linespec_result *canonical,
9016 gdb::unique_xmalloc_ptr<char> cond_string,
9017 gdb::unique_xmalloc_ptr<char> extra_string,
9018 enum bptype type, enum bpdisp disposition,
9019 int thread, int task, int ignore_count,
9020 const struct breakpoint_ops *ops, int from_tty,
9021 int enabled, int internal, unsigned flags)
9022 {
9023 if (canonical->pre_expanded)
9024 gdb_assert (canonical->lsals.size () == 1);
9025
9026 for (const auto &lsal : canonical->lsals)
9027 {
9028 /* Note that 'location' can be NULL in the case of a plain
9029 'break', without arguments. */
9030 event_location_up location
9031 = (canonical->location != NULL
9032 ? copy_event_location (canonical->location.get ()) : NULL);
9033 gdb::unique_xmalloc_ptr<char> filter_string
9034 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
9035
9036 create_breakpoint_sal (gdbarch, lsal.sals,
9037 std::move (location),
9038 std::move (filter_string),
9039 std::move (cond_string),
9040 std::move (extra_string),
9041 type, disposition,
9042 thread, task, ignore_count, ops,
9043 from_tty, enabled, internal, flags,
9044 canonical->special_display);
9045 }
9046 }
9047
9048 /* Parse LOCATION which is assumed to be a SAL specification possibly
9049 followed by conditionals. On return, SALS contains an array of SAL
9050 addresses found. LOCATION points to the end of the SAL (for
9051 linespec locations).
9052
9053 The array and the line spec strings are allocated on the heap, it is
9054 the caller's responsibility to free them. */
9055
9056 static void
9057 parse_breakpoint_sals (struct event_location *location,
9058 struct linespec_result *canonical)
9059 {
9060 struct symtab_and_line cursal;
9061
9062 if (event_location_type (location) == LINESPEC_LOCATION)
9063 {
9064 const char *spec = get_linespec_location (location)->spec_string;
9065
9066 if (spec == NULL)
9067 {
9068 /* The last displayed codepoint, if it's valid, is our default
9069 breakpoint address. */
9070 if (last_displayed_sal_is_valid ())
9071 {
9072 /* Set sal's pspace, pc, symtab, and line to the values
9073 corresponding to the last call to print_frame_info.
9074 Be sure to reinitialize LINE with NOTCURRENT == 0
9075 as the breakpoint line number is inappropriate otherwise.
9076 find_pc_line would adjust PC, re-set it back. */
9077 symtab_and_line sal = get_last_displayed_sal ();
9078 CORE_ADDR pc = sal.pc;
9079
9080 sal = find_pc_line (pc, 0);
9081
9082 /* "break" without arguments is equivalent to "break *PC"
9083 where PC is the last displayed codepoint's address. So
9084 make sure to set sal.explicit_pc to prevent GDB from
9085 trying to expand the list of sals to include all other
9086 instances with the same symtab and line. */
9087 sal.pc = pc;
9088 sal.explicit_pc = 1;
9089
9090 struct linespec_sals lsal;
9091 lsal.sals = {sal};
9092 lsal.canonical = NULL;
9093
9094 canonical->lsals.push_back (std::move (lsal));
9095 return;
9096 }
9097 else
9098 error (_("No default breakpoint address now."));
9099 }
9100 }
9101
9102 /* Force almost all breakpoints to be in terms of the
9103 current_source_symtab (which is decode_line_1's default).
9104 This should produce the results we want almost all of the
9105 time while leaving default_breakpoint_* alone.
9106
9107 ObjC: However, don't match an Objective-C method name which
9108 may have a '+' or '-' succeeded by a '['. */
9109 cursal = get_current_source_symtab_and_line ();
9110 if (last_displayed_sal_is_valid ())
9111 {
9112 const char *spec = NULL;
9113
9114 if (event_location_type (location) == LINESPEC_LOCATION)
9115 spec = get_linespec_location (location)->spec_string;
9116
9117 if (!cursal.symtab
9118 || (spec != NULL
9119 && strchr ("+-", spec[0]) != NULL
9120 && spec[1] != '['))
9121 {
9122 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9123 get_last_displayed_symtab (),
9124 get_last_displayed_line (),
9125 canonical, NULL, NULL);
9126 return;
9127 }
9128 }
9129
9130 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9131 cursal.symtab, cursal.line, canonical, NULL, NULL);
9132 }
9133
9134
9135 /* Convert each SAL into a real PC. Verify that the PC can be
9136 inserted as a breakpoint. If it can't throw an error. */
9137
9138 static void
9139 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
9140 {
9141 for (auto &sal : sals)
9142 resolve_sal_pc (&sal);
9143 }
9144
9145 /* Fast tracepoints may have restrictions on valid locations. For
9146 instance, a fast tracepoint using a jump instead of a trap will
9147 likely have to overwrite more bytes than a trap would, and so can
9148 only be placed where the instruction is longer than the jump, or a
9149 multi-instruction sequence does not have a jump into the middle of
9150 it, etc. */
9151
9152 static void
9153 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9154 gdb::array_view<const symtab_and_line> sals)
9155 {
9156 for (const auto &sal : sals)
9157 {
9158 struct gdbarch *sarch;
9159
9160 sarch = get_sal_arch (sal);
9161 /* We fall back to GDBARCH if there is no architecture
9162 associated with SAL. */
9163 if (sarch == NULL)
9164 sarch = gdbarch;
9165 std::string msg;
9166 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
9167 error (_("May not have a fast tracepoint at %s%s"),
9168 paddress (sarch, sal.pc), msg.c_str ());
9169 }
9170 }
9171
9172 /* Given TOK, a string specification of condition and thread, as
9173 accepted by the 'break' command, extract the condition
9174 string and thread number and set *COND_STRING and *THREAD.
9175 PC identifies the context at which the condition should be parsed.
9176 If no condition is found, *COND_STRING is set to NULL.
9177 If no thread is found, *THREAD is set to -1. */
9178
9179 static void
9180 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9181 char **cond_string, int *thread, int *task,
9182 char **rest)
9183 {
9184 *cond_string = NULL;
9185 *thread = -1;
9186 *task = 0;
9187 *rest = NULL;
9188 bool force = false;
9189
9190 while (tok && *tok)
9191 {
9192 const char *end_tok;
9193 int toklen;
9194 const char *cond_start = NULL;
9195 const char *cond_end = NULL;
9196
9197 tok = skip_spaces (tok);
9198
9199 if ((*tok == '"' || *tok == ',') && rest)
9200 {
9201 *rest = savestring (tok, strlen (tok));
9202 return;
9203 }
9204
9205 end_tok = skip_to_space (tok);
9206
9207 toklen = end_tok - tok;
9208
9209 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9210 {
9211 tok = cond_start = end_tok + 1;
9212 try
9213 {
9214 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9215 }
9216 catch (const gdb_exception_error &)
9217 {
9218 if (!force)
9219 throw;
9220 else
9221 tok = tok + strlen (tok);
9222 }
9223 cond_end = tok;
9224 *cond_string = savestring (cond_start, cond_end - cond_start);
9225 }
9226 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
9227 {
9228 tok = cond_start = end_tok + 1;
9229 force = true;
9230 }
9231 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9232 {
9233 const char *tmptok;
9234 struct thread_info *thr;
9235
9236 tok = end_tok + 1;
9237 thr = parse_thread_id (tok, &tmptok);
9238 if (tok == tmptok)
9239 error (_("Junk after thread keyword."));
9240 *thread = thr->global_num;
9241 tok = tmptok;
9242 }
9243 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9244 {
9245 char *tmptok;
9246
9247 tok = end_tok + 1;
9248 *task = strtol (tok, &tmptok, 0);
9249 if (tok == tmptok)
9250 error (_("Junk after task keyword."));
9251 if (!valid_task_id (*task))
9252 error (_("Unknown task %d."), *task);
9253 tok = tmptok;
9254 }
9255 else if (rest)
9256 {
9257 *rest = savestring (tok, strlen (tok));
9258 return;
9259 }
9260 else
9261 error (_("Junk at end of arguments."));
9262 }
9263 }
9264
9265 /* Call 'find_condition_and_thread' for each sal in SALS until a parse
9266 succeeds. The parsed values are written to COND_STRING, THREAD,
9267 TASK, and REST. See the comment of 'find_condition_and_thread'
9268 for the description of these parameters and INPUT. */
9269
9270 static void
9271 find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
9272 const char *input, char **cond_string,
9273 int *thread, int *task, char **rest)
9274 {
9275 int num_failures = 0;
9276 for (auto &sal : sals)
9277 {
9278 char *cond = nullptr;
9279 int thread_id = 0;
9280 int task_id = 0;
9281 char *remaining = nullptr;
9282
9283 /* Here we want to parse 'arg' to separate condition from thread
9284 number. But because parsing happens in a context and the
9285 contexts of sals might be different, try each until there is
9286 success. Finding one successful parse is sufficient for our
9287 goal. When setting the breakpoint we'll re-parse the
9288 condition in the context of each sal. */
9289 try
9290 {
9291 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
9292 &task_id, &remaining);
9293 *cond_string = cond;
9294 *thread = thread_id;
9295 *task = task_id;
9296 *rest = remaining;
9297 break;
9298 }
9299 catch (const gdb_exception_error &e)
9300 {
9301 num_failures++;
9302 /* If no sal remains, do not continue. */
9303 if (num_failures == sals.size ())
9304 throw;
9305 }
9306 }
9307 }
9308
9309 /* Decode a static tracepoint marker spec. */
9310
9311 static std::vector<symtab_and_line>
9312 decode_static_tracepoint_spec (const char **arg_p)
9313 {
9314 const char *p = &(*arg_p)[3];
9315 const char *endp;
9316
9317 p = skip_spaces (p);
9318
9319 endp = skip_to_space (p);
9320
9321 std::string marker_str (p, endp - p);
9322
9323 std::vector<static_tracepoint_marker> markers
9324 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9325 if (markers.empty ())
9326 error (_("No known static tracepoint marker named %s"),
9327 marker_str.c_str ());
9328
9329 std::vector<symtab_and_line> sals;
9330 sals.reserve (markers.size ());
9331
9332 for (const static_tracepoint_marker &marker : markers)
9333 {
9334 symtab_and_line sal = find_pc_line (marker.address, 0);
9335 sal.pc = marker.address;
9336 sals.push_back (sal);
9337 }
9338
9339 *arg_p = endp;
9340 return sals;
9341 }
9342
9343 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9344 according to IS_TRACEPOINT. */
9345
9346 static const struct breakpoint_ops *
9347 breakpoint_ops_for_event_location_type (enum event_location_type location_type,
9348 bool is_tracepoint)
9349 {
9350 if (is_tracepoint)
9351 {
9352 if (location_type == PROBE_LOCATION)
9353 return &tracepoint_probe_breakpoint_ops;
9354 else
9355 return &tracepoint_breakpoint_ops;
9356 }
9357 else
9358 {
9359 if (location_type == PROBE_LOCATION)
9360 return &bkpt_probe_breakpoint_ops;
9361 else
9362 return &bkpt_breakpoint_ops;
9363 }
9364 }
9365
9366 /* See breakpoint.h. */
9367
9368 const struct breakpoint_ops *
9369 breakpoint_ops_for_event_location (const struct event_location *location,
9370 bool is_tracepoint)
9371 {
9372 if (location != nullptr)
9373 return breakpoint_ops_for_event_location_type
9374 (event_location_type (location), is_tracepoint);
9375 return is_tracepoint ? &tracepoint_breakpoint_ops : &bkpt_breakpoint_ops;
9376 }
9377
9378 /* See breakpoint.h. */
9379
9380 int
9381 create_breakpoint (struct gdbarch *gdbarch,
9382 struct event_location *location,
9383 const char *cond_string,
9384 int thread, const char *extra_string,
9385 int parse_extra,
9386 int tempflag, enum bptype type_wanted,
9387 int ignore_count,
9388 enum auto_boolean pending_break_support,
9389 const struct breakpoint_ops *ops,
9390 int from_tty, int enabled, int internal,
9391 unsigned flags)
9392 {
9393 struct linespec_result canonical;
9394 int pending = 0;
9395 int task = 0;
9396 int prev_bkpt_count = breakpoint_count;
9397
9398 gdb_assert (ops != NULL);
9399
9400 /* If extra_string isn't useful, set it to NULL. */
9401 if (extra_string != NULL && *extra_string == '\0')
9402 extra_string = NULL;
9403
9404 try
9405 {
9406 ops->create_sals_from_location (location, &canonical, type_wanted);
9407 }
9408 catch (const gdb_exception_error &e)
9409 {
9410 /* If caller is interested in rc value from parse, set
9411 value. */
9412 if (e.error == NOT_FOUND_ERROR)
9413 {
9414 /* If pending breakpoint support is turned off, throw
9415 error. */
9416
9417 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9418 throw;
9419
9420 exception_print (gdb_stderr, e);
9421
9422 /* If pending breakpoint support is auto query and the user
9423 selects no, then simply return the error code. */
9424 if (pending_break_support == AUTO_BOOLEAN_AUTO
9425 && !nquery (_("Make %s pending on future shared library load? "),
9426 bptype_string (type_wanted)))
9427 return 0;
9428
9429 /* At this point, either the user was queried about setting
9430 a pending breakpoint and selected yes, or pending
9431 breakpoint behavior is on and thus a pending breakpoint
9432 is defaulted on behalf of the user. */
9433 pending = 1;
9434 }
9435 else
9436 throw;
9437 }
9438
9439 if (!pending && canonical.lsals.empty ())
9440 return 0;
9441
9442 /* Resolve all line numbers to PC's and verify that the addresses
9443 are ok for the target. */
9444 if (!pending)
9445 {
9446 for (auto &lsal : canonical.lsals)
9447 breakpoint_sals_to_pc (lsal.sals);
9448 }
9449
9450 /* Fast tracepoints may have additional restrictions on location. */
9451 if (!pending && type_wanted == bp_fast_tracepoint)
9452 {
9453 for (const auto &lsal : canonical.lsals)
9454 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9455 }
9456
9457 /* Verify that condition can be parsed, before setting any
9458 breakpoints. Allocate a separate condition expression for each
9459 breakpoint. */
9460 if (!pending)
9461 {
9462 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9463 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9464
9465 if (parse_extra)
9466 {
9467 char *rest;
9468 char *cond;
9469
9470 const linespec_sals &lsal = canonical.lsals[0];
9471
9472 find_condition_and_thread_for_sals (lsal.sals, extra_string,
9473 &cond, &thread, &task, &rest);
9474 cond_string_copy.reset (cond);
9475 extra_string_copy.reset (rest);
9476 }
9477 else
9478 {
9479 if (type_wanted != bp_dprintf
9480 && extra_string != NULL && *extra_string != '\0')
9481 error (_("Garbage '%s' at end of location"), extra_string);
9482
9483 /* Create a private copy of condition string. */
9484 if (cond_string)
9485 cond_string_copy.reset (xstrdup (cond_string));
9486 /* Create a private copy of any extra string. */
9487 if (extra_string)
9488 extra_string_copy.reset (xstrdup (extra_string));
9489 }
9490
9491 ops->create_breakpoints_sal (gdbarch, &canonical,
9492 std::move (cond_string_copy),
9493 std::move (extra_string_copy),
9494 type_wanted,
9495 tempflag ? disp_del : disp_donttouch,
9496 thread, task, ignore_count, ops,
9497 from_tty, enabled, internal, flags);
9498 }
9499 else
9500 {
9501 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9502
9503 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9504 b->location = copy_event_location (location);
9505
9506 if (parse_extra)
9507 b->cond_string = NULL;
9508 else
9509 {
9510 /* Create a private copy of condition string. */
9511 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
9512 b->thread = thread;
9513 }
9514
9515 /* Create a private copy of any extra string. */
9516 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
9517 b->ignore_count = ignore_count;
9518 b->disposition = tempflag ? disp_del : disp_donttouch;
9519 b->condition_not_parsed = 1;
9520 b->enable_state = enabled ? bp_enabled : bp_disabled;
9521 if ((type_wanted != bp_breakpoint
9522 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9523 b->pspace = current_program_space;
9524
9525 install_breakpoint (internal, std::move (b), 0);
9526 }
9527
9528 if (canonical.lsals.size () > 1)
9529 {
9530 warning (_("Multiple breakpoints were set.\nUse the "
9531 "\"delete\" command to delete unwanted breakpoints."));
9532 prev_breakpoint_count = prev_bkpt_count;
9533 }
9534
9535 update_global_location_list (UGLL_MAY_INSERT);
9536
9537 return 1;
9538 }
9539
9540 /* Set a breakpoint.
9541 ARG is a string describing breakpoint address,
9542 condition, and thread.
9543 FLAG specifies if a breakpoint is hardware on,
9544 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9545 and BP_TEMPFLAG. */
9546
9547 static void
9548 break_command_1 (const char *arg, int flag, int from_tty)
9549 {
9550 int tempflag = flag & BP_TEMPFLAG;
9551 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9552 ? bp_hardware_breakpoint
9553 : bp_breakpoint);
9554
9555 event_location_up location = string_to_event_location (&arg, current_language);
9556 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9557 (location.get (), false /* is_tracepoint */);
9558
9559 create_breakpoint (get_current_arch (),
9560 location.get (),
9561 NULL, 0, arg, 1 /* parse arg */,
9562 tempflag, type_wanted,
9563 0 /* Ignore count */,
9564 pending_break_support,
9565 ops,
9566 from_tty,
9567 1 /* enabled */,
9568 0 /* internal */,
9569 0);
9570 }
9571
9572 /* Helper function for break_command_1 and disassemble_command. */
9573
9574 void
9575 resolve_sal_pc (struct symtab_and_line *sal)
9576 {
9577 CORE_ADDR pc;
9578
9579 if (sal->pc == 0 && sal->symtab != NULL)
9580 {
9581 if (!find_line_pc (sal->symtab, sal->line, &pc))
9582 error (_("No line %d in file \"%s\"."),
9583 sal->line, symtab_to_filename_for_display (sal->symtab));
9584 sal->pc = pc;
9585
9586 /* If this SAL corresponds to a breakpoint inserted using a line
9587 number, then skip the function prologue if necessary. */
9588 if (sal->explicit_line)
9589 skip_prologue_sal (sal);
9590 }
9591
9592 if (sal->section == 0 && sal->symtab != NULL)
9593 {
9594 const struct blockvector *bv;
9595 const struct block *b;
9596 struct symbol *sym;
9597
9598 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9599 SYMTAB_COMPUNIT (sal->symtab));
9600 if (bv != NULL)
9601 {
9602 sym = block_linkage_function (b);
9603 if (sym != NULL)
9604 {
9605 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9606 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9607 sym);
9608 }
9609 else
9610 {
9611 /* It really is worthwhile to have the section, so we'll
9612 just have to look harder. This case can be executed
9613 if we have line numbers but no functions (as can
9614 happen in assembly source). */
9615
9616 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9617 switch_to_program_space_and_thread (sal->pspace);
9618
9619 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9620 if (msym.minsym)
9621 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9622 }
9623 }
9624 }
9625 }
9626
9627 void
9628 break_command (const char *arg, int from_tty)
9629 {
9630 break_command_1 (arg, 0, from_tty);
9631 }
9632
9633 void
9634 tbreak_command (const char *arg, int from_tty)
9635 {
9636 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9637 }
9638
9639 static void
9640 hbreak_command (const char *arg, int from_tty)
9641 {
9642 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9643 }
9644
9645 static void
9646 thbreak_command (const char *arg, int from_tty)
9647 {
9648 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9649 }
9650
9651 static void
9652 stop_command (const char *arg, int from_tty)
9653 {
9654 printf_filtered (_("Specify the type of breakpoint to set.\n\
9655 Usage: stop in <function | address>\n\
9656 stop at <line>\n"));
9657 }
9658
9659 static void
9660 stopin_command (const char *arg, int from_tty)
9661 {
9662 int badInput = 0;
9663
9664 if (arg == NULL)
9665 badInput = 1;
9666 else if (*arg != '*')
9667 {
9668 const char *argptr = arg;
9669 int hasColon = 0;
9670
9671 /* Look for a ':'. If this is a line number specification, then
9672 say it is bad, otherwise, it should be an address or
9673 function/method name. */
9674 while (*argptr && !hasColon)
9675 {
9676 hasColon = (*argptr == ':');
9677 argptr++;
9678 }
9679
9680 if (hasColon)
9681 badInput = (*argptr != ':'); /* Not a class::method */
9682 else
9683 badInput = isdigit (*arg); /* a simple line number */
9684 }
9685
9686 if (badInput)
9687 printf_filtered (_("Usage: stop in <function | address>\n"));
9688 else
9689 break_command_1 (arg, 0, from_tty);
9690 }
9691
9692 static void
9693 stopat_command (const char *arg, int from_tty)
9694 {
9695 int badInput = 0;
9696
9697 if (arg == NULL || *arg == '*') /* no line number */
9698 badInput = 1;
9699 else
9700 {
9701 const char *argptr = arg;
9702 int hasColon = 0;
9703
9704 /* Look for a ':'. If there is a '::' then get out, otherwise
9705 it is probably a line number. */
9706 while (*argptr && !hasColon)
9707 {
9708 hasColon = (*argptr == ':');
9709 argptr++;
9710 }
9711
9712 if (hasColon)
9713 badInput = (*argptr == ':'); /* we have class::method */
9714 else
9715 badInput = !isdigit (*arg); /* not a line number */
9716 }
9717
9718 if (badInput)
9719 printf_filtered (_("Usage: stop at LINE\n"));
9720 else
9721 break_command_1 (arg, 0, from_tty);
9722 }
9723
9724 /* The dynamic printf command is mostly like a regular breakpoint, but
9725 with a prewired command list consisting of a single output command,
9726 built from extra arguments supplied on the dprintf command
9727 line. */
9728
9729 static void
9730 dprintf_command (const char *arg, int from_tty)
9731 {
9732 event_location_up location = string_to_event_location (&arg, current_language);
9733
9734 /* If non-NULL, ARG should have been advanced past the location;
9735 the next character must be ','. */
9736 if (arg != NULL)
9737 {
9738 if (arg[0] != ',' || arg[1] == '\0')
9739 error (_("Format string required"));
9740 else
9741 {
9742 /* Skip the comma. */
9743 ++arg;
9744 }
9745 }
9746
9747 create_breakpoint (get_current_arch (),
9748 location.get (),
9749 NULL, 0, arg, 1 /* parse arg */,
9750 0, bp_dprintf,
9751 0 /* Ignore count */,
9752 pending_break_support,
9753 &dprintf_breakpoint_ops,
9754 from_tty,
9755 1 /* enabled */,
9756 0 /* internal */,
9757 0);
9758 }
9759
9760 static void
9761 agent_printf_command (const char *arg, int from_tty)
9762 {
9763 error (_("May only run agent-printf on the target"));
9764 }
9765
9766 /* Implement the "breakpoint_hit" breakpoint_ops method for
9767 ranged breakpoints. */
9768
9769 static int
9770 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9771 const address_space *aspace,
9772 CORE_ADDR bp_addr,
9773 const struct target_waitstatus *ws)
9774 {
9775 if (ws->kind != TARGET_WAITKIND_STOPPED
9776 || ws->value.sig != GDB_SIGNAL_TRAP)
9777 return 0;
9778
9779 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9780 bl->length, aspace, bp_addr);
9781 }
9782
9783 /* Implement the "resources_needed" breakpoint_ops method for
9784 ranged breakpoints. */
9785
9786 static int
9787 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9788 {
9789 return target_ranged_break_num_registers ();
9790 }
9791
9792 /* Implement the "print_it" breakpoint_ops method for
9793 ranged breakpoints. */
9794
9795 static enum print_stop_action
9796 print_it_ranged_breakpoint (bpstat bs)
9797 {
9798 struct breakpoint *b = bs->breakpoint_at;
9799 struct bp_location *bl = b->loc;
9800 struct ui_out *uiout = current_uiout;
9801
9802 gdb_assert (b->type == bp_hardware_breakpoint);
9803
9804 /* Ranged breakpoints have only one location. */
9805 gdb_assert (bl && bl->next == NULL);
9806
9807 annotate_breakpoint (b->number);
9808
9809 maybe_print_thread_hit_breakpoint (uiout);
9810
9811 if (b->disposition == disp_del)
9812 uiout->text ("Temporary ranged breakpoint ");
9813 else
9814 uiout->text ("Ranged breakpoint ");
9815 if (uiout->is_mi_like_p ())
9816 {
9817 uiout->field_string ("reason",
9818 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9819 uiout->field_string ("disp", bpdisp_text (b->disposition));
9820 }
9821 uiout->field_signed ("bkptno", b->number);
9822 uiout->text (", ");
9823
9824 return PRINT_SRC_AND_LOC;
9825 }
9826
9827 /* Implement the "print_one" breakpoint_ops method for
9828 ranged breakpoints. */
9829
9830 static void
9831 print_one_ranged_breakpoint (struct breakpoint *b,
9832 struct bp_location **last_loc)
9833 {
9834 struct bp_location *bl = b->loc;
9835 struct value_print_options opts;
9836 struct ui_out *uiout = current_uiout;
9837
9838 /* Ranged breakpoints have only one location. */
9839 gdb_assert (bl && bl->next == NULL);
9840
9841 get_user_print_options (&opts);
9842
9843 if (opts.addressprint)
9844 /* We don't print the address range here, it will be printed later
9845 by print_one_detail_ranged_breakpoint. */
9846 uiout->field_skip ("addr");
9847 annotate_field (5);
9848 print_breakpoint_location (b, bl);
9849 *last_loc = bl;
9850 }
9851
9852 /* Implement the "print_one_detail" breakpoint_ops method for
9853 ranged breakpoints. */
9854
9855 static void
9856 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9857 struct ui_out *uiout)
9858 {
9859 CORE_ADDR address_start, address_end;
9860 struct bp_location *bl = b->loc;
9861 string_file stb;
9862
9863 gdb_assert (bl);
9864
9865 address_start = bl->address;
9866 address_end = address_start + bl->length - 1;
9867
9868 uiout->text ("\taddress range: ");
9869 stb.printf ("[%s, %s]",
9870 print_core_address (bl->gdbarch, address_start),
9871 print_core_address (bl->gdbarch, address_end));
9872 uiout->field_stream ("addr", stb);
9873 uiout->text ("\n");
9874 }
9875
9876 /* Implement the "print_mention" breakpoint_ops method for
9877 ranged breakpoints. */
9878
9879 static void
9880 print_mention_ranged_breakpoint (struct breakpoint *b)
9881 {
9882 struct bp_location *bl = b->loc;
9883 struct ui_out *uiout = current_uiout;
9884
9885 gdb_assert (bl);
9886 gdb_assert (b->type == bp_hardware_breakpoint);
9887
9888 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9889 b->number, paddress (bl->gdbarch, bl->address),
9890 paddress (bl->gdbarch, bl->address + bl->length - 1));
9891 }
9892
9893 /* Implement the "print_recreate" breakpoint_ops method for
9894 ranged breakpoints. */
9895
9896 static void
9897 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9898 {
9899 fprintf_unfiltered (fp, "break-range %s, %s",
9900 event_location_to_string (b->location.get ()),
9901 event_location_to_string (b->location_range_end.get ()));
9902 print_recreate_thread (b, fp);
9903 }
9904
9905 /* The breakpoint_ops structure to be used in ranged breakpoints. */
9906
9907 static struct breakpoint_ops ranged_breakpoint_ops;
9908
9909 /* Find the address where the end of the breakpoint range should be
9910 placed, given the SAL of the end of the range. This is so that if
9911 the user provides a line number, the end of the range is set to the
9912 last instruction of the given line. */
9913
9914 static CORE_ADDR
9915 find_breakpoint_range_end (struct symtab_and_line sal)
9916 {
9917 CORE_ADDR end;
9918
9919 /* If the user provided a PC value, use it. Otherwise,
9920 find the address of the end of the given location. */
9921 if (sal.explicit_pc)
9922 end = sal.pc;
9923 else
9924 {
9925 int ret;
9926 CORE_ADDR start;
9927
9928 ret = find_line_pc_range (sal, &start, &end);
9929 if (!ret)
9930 error (_("Could not find location of the end of the range."));
9931
9932 /* find_line_pc_range returns the start of the next line. */
9933 end--;
9934 }
9935
9936 return end;
9937 }
9938
9939 /* Implement the "break-range" CLI command. */
9940
9941 static void
9942 break_range_command (const char *arg, int from_tty)
9943 {
9944 const char *arg_start;
9945 struct linespec_result canonical_start, canonical_end;
9946 int bp_count, can_use_bp, length;
9947 CORE_ADDR end;
9948 struct breakpoint *b;
9949
9950 /* We don't support software ranged breakpoints. */
9951 if (target_ranged_break_num_registers () < 0)
9952 error (_("This target does not support hardware ranged breakpoints."));
9953
9954 bp_count = hw_breakpoint_used_count ();
9955 bp_count += target_ranged_break_num_registers ();
9956 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9957 bp_count, 0);
9958 if (can_use_bp < 0)
9959 error (_("Hardware breakpoints used exceeds limit."));
9960
9961 arg = skip_spaces (arg);
9962 if (arg == NULL || arg[0] == '\0')
9963 error(_("No address range specified."));
9964
9965 arg_start = arg;
9966 event_location_up start_location = string_to_event_location (&arg,
9967 current_language);
9968 parse_breakpoint_sals (start_location.get (), &canonical_start);
9969
9970 if (arg[0] != ',')
9971 error (_("Too few arguments."));
9972 else if (canonical_start.lsals.empty ())
9973 error (_("Could not find location of the beginning of the range."));
9974
9975 const linespec_sals &lsal_start = canonical_start.lsals[0];
9976
9977 if (canonical_start.lsals.size () > 1
9978 || lsal_start.sals.size () != 1)
9979 error (_("Cannot create a ranged breakpoint with multiple locations."));
9980
9981 const symtab_and_line &sal_start = lsal_start.sals[0];
9982 std::string addr_string_start (arg_start, arg - arg_start);
9983
9984 arg++; /* Skip the comma. */
9985 arg = skip_spaces (arg);
9986
9987 /* Parse the end location. */
9988
9989 arg_start = arg;
9990
9991 /* We call decode_line_full directly here instead of using
9992 parse_breakpoint_sals because we need to specify the start location's
9993 symtab and line as the default symtab and line for the end of the
9994 range. This makes it possible to have ranges like "foo.c:27, +14",
9995 where +14 means 14 lines from the start location. */
9996 event_location_up end_location = string_to_event_location (&arg,
9997 current_language);
9998 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
9999 sal_start.symtab, sal_start.line,
10000 &canonical_end, NULL, NULL);
10001
10002 if (canonical_end.lsals.empty ())
10003 error (_("Could not find location of the end of the range."));
10004
10005 const linespec_sals &lsal_end = canonical_end.lsals[0];
10006 if (canonical_end.lsals.size () > 1
10007 || lsal_end.sals.size () != 1)
10008 error (_("Cannot create a ranged breakpoint with multiple locations."));
10009
10010 const symtab_and_line &sal_end = lsal_end.sals[0];
10011
10012 end = find_breakpoint_range_end (sal_end);
10013 if (sal_start.pc > end)
10014 error (_("Invalid address range, end precedes start."));
10015
10016 length = end - sal_start.pc + 1;
10017 if (length < 0)
10018 /* Length overflowed. */
10019 error (_("Address range too large."));
10020 else if (length == 1)
10021 {
10022 /* This range is simple enough to be handled by
10023 the `hbreak' command. */
10024 hbreak_command (&addr_string_start[0], 1);
10025
10026 return;
10027 }
10028
10029 /* Now set up the breakpoint. */
10030 b = set_raw_breakpoint (get_current_arch (), sal_start,
10031 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10032 set_breakpoint_count (breakpoint_count + 1);
10033 b->number = breakpoint_count;
10034 b->disposition = disp_donttouch;
10035 b->location = std::move (start_location);
10036 b->location_range_end = std::move (end_location);
10037 b->loc->length = length;
10038
10039 mention (b);
10040 gdb::observers::breakpoint_created.notify (b);
10041 update_global_location_list (UGLL_MAY_INSERT);
10042 }
10043
10044 /* Return non-zero if EXP is verified as constant. Returned zero
10045 means EXP is variable. Also the constant detection may fail for
10046 some constant expressions and in such case still falsely return
10047 zero. */
10048
10049 static int
10050 watchpoint_exp_is_const (const struct expression *exp)
10051 {
10052 int i = exp->nelts;
10053
10054 while (i > 0)
10055 {
10056 int oplenp, argsp;
10057
10058 /* We are only interested in the descriptor of each element. */
10059 operator_length (exp, i, &oplenp, &argsp);
10060 i -= oplenp;
10061
10062 switch (exp->elts[i].opcode)
10063 {
10064 case BINOP_ADD:
10065 case BINOP_SUB:
10066 case BINOP_MUL:
10067 case BINOP_DIV:
10068 case BINOP_REM:
10069 case BINOP_MOD:
10070 case BINOP_LSH:
10071 case BINOP_RSH:
10072 case BINOP_LOGICAL_AND:
10073 case BINOP_LOGICAL_OR:
10074 case BINOP_BITWISE_AND:
10075 case BINOP_BITWISE_IOR:
10076 case BINOP_BITWISE_XOR:
10077 case BINOP_EQUAL:
10078 case BINOP_NOTEQUAL:
10079 case BINOP_LESS:
10080 case BINOP_GTR:
10081 case BINOP_LEQ:
10082 case BINOP_GEQ:
10083 case BINOP_REPEAT:
10084 case BINOP_COMMA:
10085 case BINOP_EXP:
10086 case BINOP_MIN:
10087 case BINOP_MAX:
10088 case BINOP_INTDIV:
10089 case BINOP_CONCAT:
10090 case TERNOP_COND:
10091 case TERNOP_SLICE:
10092
10093 case OP_LONG:
10094 case OP_FLOAT:
10095 case OP_LAST:
10096 case OP_COMPLEX:
10097 case OP_STRING:
10098 case OP_ARRAY:
10099 case OP_TYPE:
10100 case OP_TYPEOF:
10101 case OP_DECLTYPE:
10102 case OP_TYPEID:
10103 case OP_NAME:
10104 case OP_OBJC_NSSTRING:
10105
10106 case UNOP_NEG:
10107 case UNOP_LOGICAL_NOT:
10108 case UNOP_COMPLEMENT:
10109 case UNOP_ADDR:
10110 case UNOP_HIGH:
10111 case UNOP_CAST:
10112
10113 case UNOP_CAST_TYPE:
10114 case UNOP_REINTERPRET_CAST:
10115 case UNOP_DYNAMIC_CAST:
10116 /* Unary, binary and ternary operators: We have to check
10117 their operands. If they are constant, then so is the
10118 result of that operation. For instance, if A and B are
10119 determined to be constants, then so is "A + B".
10120
10121 UNOP_IND is one exception to the rule above, because the
10122 value of *ADDR is not necessarily a constant, even when
10123 ADDR is. */
10124 break;
10125
10126 case OP_VAR_VALUE:
10127 /* Check whether the associated symbol is a constant.
10128
10129 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10130 possible that a buggy compiler could mark a variable as
10131 constant even when it is not, and TYPE_CONST would return
10132 true in this case, while SYMBOL_CLASS wouldn't.
10133
10134 We also have to check for function symbols because they
10135 are always constant. */
10136 {
10137 struct symbol *s = exp->elts[i + 2].symbol;
10138
10139 if (SYMBOL_CLASS (s) != LOC_BLOCK
10140 && SYMBOL_CLASS (s) != LOC_CONST
10141 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10142 return 0;
10143 break;
10144 }
10145
10146 /* The default action is to return 0 because we are using
10147 the optimistic approach here: If we don't know something,
10148 then it is not a constant. */
10149 default:
10150 return 0;
10151 }
10152 }
10153
10154 return 1;
10155 }
10156
10157 /* Watchpoint destructor. */
10158
10159 watchpoint::~watchpoint ()
10160 {
10161 xfree (this->exp_string);
10162 xfree (this->exp_string_reparse);
10163 }
10164
10165 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10166
10167 static void
10168 re_set_watchpoint (struct breakpoint *b)
10169 {
10170 struct watchpoint *w = (struct watchpoint *) b;
10171
10172 /* Watchpoint can be either on expression using entirely global
10173 variables, or it can be on local variables.
10174
10175 Watchpoints of the first kind are never auto-deleted, and even
10176 persist across program restarts. Since they can use variables
10177 from shared libraries, we need to reparse expression as libraries
10178 are loaded and unloaded.
10179
10180 Watchpoints on local variables can also change meaning as result
10181 of solib event. For example, if a watchpoint uses both a local
10182 and a global variables in expression, it's a local watchpoint,
10183 but unloading of a shared library will make the expression
10184 invalid. This is not a very common use case, but we still
10185 re-evaluate expression, to avoid surprises to the user.
10186
10187 Note that for local watchpoints, we re-evaluate it only if
10188 watchpoints frame id is still valid. If it's not, it means the
10189 watchpoint is out of scope and will be deleted soon. In fact,
10190 I'm not sure we'll ever be called in this case.
10191
10192 If a local watchpoint's frame id is still valid, then
10193 w->exp_valid_block is likewise valid, and we can safely use it.
10194
10195 Don't do anything about disabled watchpoints, since they will be
10196 reevaluated again when enabled. */
10197 update_watchpoint (w, 1 /* reparse */);
10198 }
10199
10200 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10201
10202 static int
10203 insert_watchpoint (struct bp_location *bl)
10204 {
10205 struct watchpoint *w = (struct watchpoint *) bl->owner;
10206 int length = w->exact ? 1 : bl->length;
10207
10208 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10209 w->cond_exp.get ());
10210 }
10211
10212 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10213
10214 static int
10215 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10216 {
10217 struct watchpoint *w = (struct watchpoint *) bl->owner;
10218 int length = w->exact ? 1 : bl->length;
10219
10220 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10221 w->cond_exp.get ());
10222 }
10223
10224 static int
10225 breakpoint_hit_watchpoint (const struct bp_location *bl,
10226 const address_space *aspace, CORE_ADDR bp_addr,
10227 const struct target_waitstatus *ws)
10228 {
10229 struct breakpoint *b = bl->owner;
10230 struct watchpoint *w = (struct watchpoint *) b;
10231
10232 /* Continuable hardware watchpoints are treated as non-existent if the
10233 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10234 some data address). Otherwise gdb won't stop on a break instruction
10235 in the code (not from a breakpoint) when a hardware watchpoint has
10236 been defined. Also skip watchpoints which we know did not trigger
10237 (did not match the data address). */
10238 if (is_hardware_watchpoint (b)
10239 && w->watchpoint_triggered == watch_triggered_no)
10240 return 0;
10241
10242 return 1;
10243 }
10244
10245 static void
10246 check_status_watchpoint (bpstat bs)
10247 {
10248 gdb_assert (is_watchpoint (bs->breakpoint_at));
10249
10250 bpstat_check_watchpoint (bs);
10251 }
10252
10253 /* Implement the "resources_needed" breakpoint_ops method for
10254 hardware watchpoints. */
10255
10256 static int
10257 resources_needed_watchpoint (const struct bp_location *bl)
10258 {
10259 struct watchpoint *w = (struct watchpoint *) bl->owner;
10260 int length = w->exact? 1 : bl->length;
10261
10262 return target_region_ok_for_hw_watchpoint (bl->address, length);
10263 }
10264
10265 /* Implement the "works_in_software_mode" breakpoint_ops method for
10266 hardware watchpoints. */
10267
10268 static int
10269 works_in_software_mode_watchpoint (const struct breakpoint *b)
10270 {
10271 /* Read and access watchpoints only work with hardware support. */
10272 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10273 }
10274
10275 static enum print_stop_action
10276 print_it_watchpoint (bpstat bs)
10277 {
10278 struct breakpoint *b;
10279 enum print_stop_action result;
10280 struct watchpoint *w;
10281 struct ui_out *uiout = current_uiout;
10282
10283 gdb_assert (bs->bp_location_at != NULL);
10284
10285 b = bs->breakpoint_at;
10286 w = (struct watchpoint *) b;
10287
10288 annotate_watchpoint (b->number);
10289 maybe_print_thread_hit_breakpoint (uiout);
10290
10291 string_file stb;
10292
10293 gdb::optional<ui_out_emit_tuple> tuple_emitter;
10294 switch (b->type)
10295 {
10296 case bp_watchpoint:
10297 case bp_hardware_watchpoint:
10298 if (uiout->is_mi_like_p ())
10299 uiout->field_string
10300 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10301 mention (b);
10302 tuple_emitter.emplace (uiout, "value");
10303 uiout->text ("\nOld value = ");
10304 watchpoint_value_print (bs->old_val.get (), &stb);
10305 uiout->field_stream ("old", stb);
10306 uiout->text ("\nNew value = ");
10307 watchpoint_value_print (w->val.get (), &stb);
10308 uiout->field_stream ("new", stb);
10309 uiout->text ("\n");
10310 /* More than one watchpoint may have been triggered. */
10311 result = PRINT_UNKNOWN;
10312 break;
10313
10314 case bp_read_watchpoint:
10315 if (uiout->is_mi_like_p ())
10316 uiout->field_string
10317 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10318 mention (b);
10319 tuple_emitter.emplace (uiout, "value");
10320 uiout->text ("\nValue = ");
10321 watchpoint_value_print (w->val.get (), &stb);
10322 uiout->field_stream ("value", stb);
10323 uiout->text ("\n");
10324 result = PRINT_UNKNOWN;
10325 break;
10326
10327 case bp_access_watchpoint:
10328 if (bs->old_val != NULL)
10329 {
10330 if (uiout->is_mi_like_p ())
10331 uiout->field_string
10332 ("reason",
10333 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10334 mention (b);
10335 tuple_emitter.emplace (uiout, "value");
10336 uiout->text ("\nOld value = ");
10337 watchpoint_value_print (bs->old_val.get (), &stb);
10338 uiout->field_stream ("old", stb);
10339 uiout->text ("\nNew value = ");
10340 }
10341 else
10342 {
10343 mention (b);
10344 if (uiout->is_mi_like_p ())
10345 uiout->field_string
10346 ("reason",
10347 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10348 tuple_emitter.emplace (uiout, "value");
10349 uiout->text ("\nValue = ");
10350 }
10351 watchpoint_value_print (w->val.get (), &stb);
10352 uiout->field_stream ("new", stb);
10353 uiout->text ("\n");
10354 result = PRINT_UNKNOWN;
10355 break;
10356 default:
10357 result = PRINT_UNKNOWN;
10358 }
10359
10360 return result;
10361 }
10362
10363 /* Implement the "print_mention" breakpoint_ops method for hardware
10364 watchpoints. */
10365
10366 static void
10367 print_mention_watchpoint (struct breakpoint *b)
10368 {
10369 struct watchpoint *w = (struct watchpoint *) b;
10370 struct ui_out *uiout = current_uiout;
10371 const char *tuple_name;
10372
10373 switch (b->type)
10374 {
10375 case bp_watchpoint:
10376 uiout->text ("Watchpoint ");
10377 tuple_name = "wpt";
10378 break;
10379 case bp_hardware_watchpoint:
10380 uiout->text ("Hardware watchpoint ");
10381 tuple_name = "wpt";
10382 break;
10383 case bp_read_watchpoint:
10384 uiout->text ("Hardware read watchpoint ");
10385 tuple_name = "hw-rwpt";
10386 break;
10387 case bp_access_watchpoint:
10388 uiout->text ("Hardware access (read/write) watchpoint ");
10389 tuple_name = "hw-awpt";
10390 break;
10391 default:
10392 internal_error (__FILE__, __LINE__,
10393 _("Invalid hardware watchpoint type."));
10394 }
10395
10396 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10397 uiout->field_signed ("number", b->number);
10398 uiout->text (": ");
10399 uiout->field_string ("exp", w->exp_string);
10400 }
10401
10402 /* Implement the "print_recreate" breakpoint_ops method for
10403 watchpoints. */
10404
10405 static void
10406 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10407 {
10408 struct watchpoint *w = (struct watchpoint *) b;
10409
10410 switch (b->type)
10411 {
10412 case bp_watchpoint:
10413 case bp_hardware_watchpoint:
10414 fprintf_unfiltered (fp, "watch");
10415 break;
10416 case bp_read_watchpoint:
10417 fprintf_unfiltered (fp, "rwatch");
10418 break;
10419 case bp_access_watchpoint:
10420 fprintf_unfiltered (fp, "awatch");
10421 break;
10422 default:
10423 internal_error (__FILE__, __LINE__,
10424 _("Invalid watchpoint type."));
10425 }
10426
10427 fprintf_unfiltered (fp, " %s", w->exp_string);
10428 print_recreate_thread (b, fp);
10429 }
10430
10431 /* Implement the "explains_signal" breakpoint_ops method for
10432 watchpoints. */
10433
10434 static int
10435 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10436 {
10437 /* A software watchpoint cannot cause a signal other than
10438 GDB_SIGNAL_TRAP. */
10439 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10440 return 0;
10441
10442 return 1;
10443 }
10444
10445 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10446
10447 static struct breakpoint_ops watchpoint_breakpoint_ops;
10448
10449 /* Implement the "insert" breakpoint_ops method for
10450 masked hardware watchpoints. */
10451
10452 static int
10453 insert_masked_watchpoint (struct bp_location *bl)
10454 {
10455 struct watchpoint *w = (struct watchpoint *) bl->owner;
10456
10457 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10458 bl->watchpoint_type);
10459 }
10460
10461 /* Implement the "remove" breakpoint_ops method for
10462 masked hardware watchpoints. */
10463
10464 static int
10465 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10466 {
10467 struct watchpoint *w = (struct watchpoint *) bl->owner;
10468
10469 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10470 bl->watchpoint_type);
10471 }
10472
10473 /* Implement the "resources_needed" breakpoint_ops method for
10474 masked hardware watchpoints. */
10475
10476 static int
10477 resources_needed_masked_watchpoint (const struct bp_location *bl)
10478 {
10479 struct watchpoint *w = (struct watchpoint *) bl->owner;
10480
10481 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10482 }
10483
10484 /* Implement the "works_in_software_mode" breakpoint_ops method for
10485 masked hardware watchpoints. */
10486
10487 static int
10488 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10489 {
10490 return 0;
10491 }
10492
10493 /* Implement the "print_it" breakpoint_ops method for
10494 masked hardware watchpoints. */
10495
10496 static enum print_stop_action
10497 print_it_masked_watchpoint (bpstat bs)
10498 {
10499 struct breakpoint *b = bs->breakpoint_at;
10500 struct ui_out *uiout = current_uiout;
10501
10502 /* Masked watchpoints have only one location. */
10503 gdb_assert (b->loc && b->loc->next == NULL);
10504
10505 annotate_watchpoint (b->number);
10506 maybe_print_thread_hit_breakpoint (uiout);
10507
10508 switch (b->type)
10509 {
10510 case bp_hardware_watchpoint:
10511 if (uiout->is_mi_like_p ())
10512 uiout->field_string
10513 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10514 break;
10515
10516 case bp_read_watchpoint:
10517 if (uiout->is_mi_like_p ())
10518 uiout->field_string
10519 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10520 break;
10521
10522 case bp_access_watchpoint:
10523 if (uiout->is_mi_like_p ())
10524 uiout->field_string
10525 ("reason",
10526 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10527 break;
10528 default:
10529 internal_error (__FILE__, __LINE__,
10530 _("Invalid hardware watchpoint type."));
10531 }
10532
10533 mention (b);
10534 uiout->text (_("\n\
10535 Check the underlying instruction at PC for the memory\n\
10536 address and value which triggered this watchpoint.\n"));
10537 uiout->text ("\n");
10538
10539 /* More than one watchpoint may have been triggered. */
10540 return PRINT_UNKNOWN;
10541 }
10542
10543 /* Implement the "print_one_detail" breakpoint_ops method for
10544 masked hardware watchpoints. */
10545
10546 static void
10547 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10548 struct ui_out *uiout)
10549 {
10550 struct watchpoint *w = (struct watchpoint *) b;
10551
10552 /* Masked watchpoints have only one location. */
10553 gdb_assert (b->loc && b->loc->next == NULL);
10554
10555 uiout->text ("\tmask ");
10556 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10557 uiout->text ("\n");
10558 }
10559
10560 /* Implement the "print_mention" breakpoint_ops method for
10561 masked hardware watchpoints. */
10562
10563 static void
10564 print_mention_masked_watchpoint (struct breakpoint *b)
10565 {
10566 struct watchpoint *w = (struct watchpoint *) b;
10567 struct ui_out *uiout = current_uiout;
10568 const char *tuple_name;
10569
10570 switch (b->type)
10571 {
10572 case bp_hardware_watchpoint:
10573 uiout->text ("Masked hardware watchpoint ");
10574 tuple_name = "wpt";
10575 break;
10576 case bp_read_watchpoint:
10577 uiout->text ("Masked hardware read watchpoint ");
10578 tuple_name = "hw-rwpt";
10579 break;
10580 case bp_access_watchpoint:
10581 uiout->text ("Masked hardware access (read/write) watchpoint ");
10582 tuple_name = "hw-awpt";
10583 break;
10584 default:
10585 internal_error (__FILE__, __LINE__,
10586 _("Invalid hardware watchpoint type."));
10587 }
10588
10589 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10590 uiout->field_signed ("number", b->number);
10591 uiout->text (": ");
10592 uiout->field_string ("exp", w->exp_string);
10593 }
10594
10595 /* Implement the "print_recreate" breakpoint_ops method for
10596 masked hardware watchpoints. */
10597
10598 static void
10599 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10600 {
10601 struct watchpoint *w = (struct watchpoint *) b;
10602
10603 switch (b->type)
10604 {
10605 case bp_hardware_watchpoint:
10606 fprintf_unfiltered (fp, "watch");
10607 break;
10608 case bp_read_watchpoint:
10609 fprintf_unfiltered (fp, "rwatch");
10610 break;
10611 case bp_access_watchpoint:
10612 fprintf_unfiltered (fp, "awatch");
10613 break;
10614 default:
10615 internal_error (__FILE__, __LINE__,
10616 _("Invalid hardware watchpoint type."));
10617 }
10618
10619 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string,
10620 phex (w->hw_wp_mask, sizeof (CORE_ADDR)));
10621 print_recreate_thread (b, fp);
10622 }
10623
10624 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10625
10626 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10627
10628 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10629
10630 static bool
10631 is_masked_watchpoint (const struct breakpoint *b)
10632 {
10633 return b->ops == &masked_watchpoint_breakpoint_ops;
10634 }
10635
10636 /* accessflag: hw_write: watch write,
10637 hw_read: watch read,
10638 hw_access: watch access (read or write) */
10639 static void
10640 watch_command_1 (const char *arg, int accessflag, int from_tty,
10641 int just_location, int internal)
10642 {
10643 struct breakpoint *scope_breakpoint = NULL;
10644 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10645 struct value *result;
10646 int saved_bitpos = 0, saved_bitsize = 0;
10647 const char *exp_start = NULL;
10648 const char *exp_end = NULL;
10649 const char *tok, *end_tok;
10650 int toklen = -1;
10651 const char *cond_start = NULL;
10652 const char *cond_end = NULL;
10653 enum bptype bp_type;
10654 int thread = -1;
10655 int pc = 0;
10656 /* Flag to indicate whether we are going to use masks for
10657 the hardware watchpoint. */
10658 int use_mask = 0;
10659 CORE_ADDR mask = 0;
10660
10661 /* Make sure that we actually have parameters to parse. */
10662 if (arg != NULL && arg[0] != '\0')
10663 {
10664 const char *value_start;
10665
10666 exp_end = arg + strlen (arg);
10667
10668 /* Look for "parameter value" pairs at the end
10669 of the arguments string. */
10670 for (tok = exp_end - 1; tok > arg; tok--)
10671 {
10672 /* Skip whitespace at the end of the argument list. */
10673 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10674 tok--;
10675
10676 /* Find the beginning of the last token.
10677 This is the value of the parameter. */
10678 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10679 tok--;
10680 value_start = tok + 1;
10681
10682 /* Skip whitespace. */
10683 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10684 tok--;
10685
10686 end_tok = tok;
10687
10688 /* Find the beginning of the second to last token.
10689 This is the parameter itself. */
10690 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10691 tok--;
10692 tok++;
10693 toklen = end_tok - tok + 1;
10694
10695 if (toklen == 6 && startswith (tok, "thread"))
10696 {
10697 struct thread_info *thr;
10698 /* At this point we've found a "thread" token, which means
10699 the user is trying to set a watchpoint that triggers
10700 only in a specific thread. */
10701 const char *endp;
10702
10703 if (thread != -1)
10704 error(_("You can specify only one thread."));
10705
10706 /* Extract the thread ID from the next token. */
10707 thr = parse_thread_id (value_start, &endp);
10708
10709 /* Check if the user provided a valid thread ID. */
10710 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10711 invalid_thread_id_error (value_start);
10712
10713 thread = thr->global_num;
10714 }
10715 else if (toklen == 4 && startswith (tok, "mask"))
10716 {
10717 /* We've found a "mask" token, which means the user wants to
10718 create a hardware watchpoint that is going to have the mask
10719 facility. */
10720 struct value *mask_value, *mark;
10721
10722 if (use_mask)
10723 error(_("You can specify only one mask."));
10724
10725 use_mask = just_location = 1;
10726
10727 mark = value_mark ();
10728 mask_value = parse_to_comma_and_eval (&value_start);
10729 mask = value_as_address (mask_value);
10730 value_free_to_mark (mark);
10731 }
10732 else
10733 /* We didn't recognize what we found. We should stop here. */
10734 break;
10735
10736 /* Truncate the string and get rid of the "parameter value" pair before
10737 the arguments string is parsed by the parse_exp_1 function. */
10738 exp_end = tok;
10739 }
10740 }
10741 else
10742 exp_end = arg;
10743
10744 /* Parse the rest of the arguments. From here on out, everything
10745 is in terms of a newly allocated string instead of the original
10746 ARG. */
10747 std::string expression (arg, exp_end - arg);
10748 exp_start = arg = expression.c_str ();
10749 innermost_block_tracker tracker;
10750 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10751 exp_end = arg;
10752 /* Remove trailing whitespace from the expression before saving it.
10753 This makes the eventual display of the expression string a bit
10754 prettier. */
10755 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10756 --exp_end;
10757
10758 /* Checking if the expression is not constant. */
10759 if (watchpoint_exp_is_const (exp.get ()))
10760 {
10761 int len;
10762
10763 len = exp_end - exp_start;
10764 while (len > 0 && isspace (exp_start[len - 1]))
10765 len--;
10766 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10767 }
10768
10769 exp_valid_block = tracker.block ();
10770 struct value *mark = value_mark ();
10771 struct value *val_as_value = nullptr;
10772 fetch_subexp_value (exp.get (), &pc, &val_as_value, &result, NULL,
10773 just_location);
10774
10775 if (val_as_value != NULL && just_location)
10776 {
10777 saved_bitpos = value_bitpos (val_as_value);
10778 saved_bitsize = value_bitsize (val_as_value);
10779 }
10780
10781 value_ref_ptr val;
10782 if (just_location)
10783 {
10784 int ret;
10785
10786 exp_valid_block = NULL;
10787 val = release_value (value_addr (result));
10788 value_free_to_mark (mark);
10789
10790 if (use_mask)
10791 {
10792 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10793 mask);
10794 if (ret == -1)
10795 error (_("This target does not support masked watchpoints."));
10796 else if (ret == -2)
10797 error (_("Invalid mask or memory region."));
10798 }
10799 }
10800 else if (val_as_value != NULL)
10801 val = release_value (val_as_value);
10802
10803 tok = skip_spaces (arg);
10804 end_tok = skip_to_space (tok);
10805
10806 toklen = end_tok - tok;
10807 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10808 {
10809 tok = cond_start = end_tok + 1;
10810 innermost_block_tracker if_tracker;
10811 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10812
10813 /* The watchpoint expression may not be local, but the condition
10814 may still be. E.g.: `watch global if local > 0'. */
10815 cond_exp_valid_block = if_tracker.block ();
10816
10817 cond_end = tok;
10818 }
10819 if (*tok)
10820 error (_("Junk at end of command."));
10821
10822 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10823
10824 /* Save this because create_internal_breakpoint below invalidates
10825 'wp_frame'. */
10826 frame_id watchpoint_frame = get_frame_id (wp_frame);
10827
10828 /* If the expression is "local", then set up a "watchpoint scope"
10829 breakpoint at the point where we've left the scope of the watchpoint
10830 expression. Create the scope breakpoint before the watchpoint, so
10831 that we will encounter it first in bpstat_stop_status. */
10832 if (exp_valid_block != NULL && wp_frame != NULL)
10833 {
10834 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10835
10836 if (frame_id_p (caller_frame_id))
10837 {
10838 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10839 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10840
10841 scope_breakpoint
10842 = create_internal_breakpoint (caller_arch, caller_pc,
10843 bp_watchpoint_scope,
10844 &momentary_breakpoint_ops);
10845
10846 /* create_internal_breakpoint could invalidate WP_FRAME. */
10847 wp_frame = NULL;
10848
10849 scope_breakpoint->enable_state = bp_enabled;
10850
10851 /* Automatically delete the breakpoint when it hits. */
10852 scope_breakpoint->disposition = disp_del;
10853
10854 /* Only break in the proper frame (help with recursion). */
10855 scope_breakpoint->frame_id = caller_frame_id;
10856
10857 /* Set the address at which we will stop. */
10858 scope_breakpoint->loc->gdbarch = caller_arch;
10859 scope_breakpoint->loc->requested_address = caller_pc;
10860 scope_breakpoint->loc->address
10861 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10862 scope_breakpoint->loc->requested_address,
10863 scope_breakpoint->type);
10864 }
10865 }
10866
10867 /* Now set up the breakpoint. We create all watchpoints as hardware
10868 watchpoints here even if hardware watchpoints are turned off, a call
10869 to update_watchpoint later in this function will cause the type to
10870 drop back to bp_watchpoint (software watchpoint) if required. */
10871
10872 if (accessflag == hw_read)
10873 bp_type = bp_read_watchpoint;
10874 else if (accessflag == hw_access)
10875 bp_type = bp_access_watchpoint;
10876 else
10877 bp_type = bp_hardware_watchpoint;
10878
10879 std::unique_ptr<watchpoint> w (new watchpoint ());
10880
10881 if (use_mask)
10882 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10883 &masked_watchpoint_breakpoint_ops);
10884 else
10885 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10886 &watchpoint_breakpoint_ops);
10887 w->thread = thread;
10888 w->disposition = disp_donttouch;
10889 w->pspace = current_program_space;
10890 w->exp = std::move (exp);
10891 w->exp_valid_block = exp_valid_block;
10892 w->cond_exp_valid_block = cond_exp_valid_block;
10893 if (just_location)
10894 {
10895 struct type *t = value_type (val.get ());
10896 CORE_ADDR addr = value_as_address (val.get ());
10897
10898 w->exp_string_reparse
10899 = current_language->watch_location_expression (t, addr).release ();
10900
10901 w->exp_string = xstrprintf ("-location %.*s",
10902 (int) (exp_end - exp_start), exp_start);
10903 }
10904 else
10905 w->exp_string = savestring (exp_start, exp_end - exp_start);
10906
10907 if (use_mask)
10908 {
10909 w->hw_wp_mask = mask;
10910 }
10911 else
10912 {
10913 w->val = val;
10914 w->val_bitpos = saved_bitpos;
10915 w->val_bitsize = saved_bitsize;
10916 w->val_valid = true;
10917 }
10918
10919 if (cond_start)
10920 w->cond_string = savestring (cond_start, cond_end - cond_start);
10921 else
10922 w->cond_string = 0;
10923
10924 if (frame_id_p (watchpoint_frame))
10925 {
10926 w->watchpoint_frame = watchpoint_frame;
10927 w->watchpoint_thread = inferior_ptid;
10928 }
10929 else
10930 {
10931 w->watchpoint_frame = null_frame_id;
10932 w->watchpoint_thread = null_ptid;
10933 }
10934
10935 if (scope_breakpoint != NULL)
10936 {
10937 /* The scope breakpoint is related to the watchpoint. We will
10938 need to act on them together. */
10939 w->related_breakpoint = scope_breakpoint;
10940 scope_breakpoint->related_breakpoint = w.get ();
10941 }
10942
10943 if (!just_location)
10944 value_free_to_mark (mark);
10945
10946 /* Finally update the new watchpoint. This creates the locations
10947 that should be inserted. */
10948 update_watchpoint (w.get (), 1);
10949
10950 install_breakpoint (internal, std::move (w), 1);
10951 }
10952
10953 /* Return count of debug registers needed to watch the given expression.
10954 If the watchpoint cannot be handled in hardware return zero. */
10955
10956 static int
10957 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10958 {
10959 int found_memory_cnt = 0;
10960
10961 /* Did the user specifically forbid us to use hardware watchpoints? */
10962 if (!can_use_hw_watchpoints)
10963 return 0;
10964
10965 gdb_assert (!vals.empty ());
10966 struct value *head = vals[0].get ();
10967
10968 /* Make sure that the value of the expression depends only upon
10969 memory contents, and values computed from them within GDB. If we
10970 find any register references or function calls, we can't use a
10971 hardware watchpoint.
10972
10973 The idea here is that evaluating an expression generates a series
10974 of values, one holding the value of every subexpression. (The
10975 expression a*b+c has five subexpressions: a, b, a*b, c, and
10976 a*b+c.) GDB's values hold almost enough information to establish
10977 the criteria given above --- they identify memory lvalues,
10978 register lvalues, computed values, etcetera. So we can evaluate
10979 the expression, and then scan the chain of values that leaves
10980 behind to decide whether we can detect any possible change to the
10981 expression's final value using only hardware watchpoints.
10982
10983 However, I don't think that the values returned by inferior
10984 function calls are special in any way. So this function may not
10985 notice that an expression involving an inferior function call
10986 can't be watched with hardware watchpoints. FIXME. */
10987 for (const value_ref_ptr &iter : vals)
10988 {
10989 struct value *v = iter.get ();
10990
10991 if (VALUE_LVAL (v) == lval_memory)
10992 {
10993 if (v != head && value_lazy (v))
10994 /* A lazy memory lvalue in the chain is one that GDB never
10995 needed to fetch; we either just used its address (e.g.,
10996 `a' in `a.b') or we never needed it at all (e.g., `a'
10997 in `a,b'). This doesn't apply to HEAD; if that is
10998 lazy then it was not readable, but watch it anyway. */
10999 ;
11000 else
11001 {
11002 /* Ahh, memory we actually used! Check if we can cover
11003 it with hardware watchpoints. */
11004 struct type *vtype = check_typedef (value_type (v));
11005
11006 /* We only watch structs and arrays if user asked for it
11007 explicitly, never if they just happen to appear in a
11008 middle of some value chain. */
11009 if (v == head
11010 || (vtype->code () != TYPE_CODE_STRUCT
11011 && vtype->code () != TYPE_CODE_ARRAY))
11012 {
11013 CORE_ADDR vaddr = value_address (v);
11014 int len;
11015 int num_regs;
11016
11017 len = (target_exact_watchpoints
11018 && is_scalar_type_recursive (vtype))?
11019 1 : TYPE_LENGTH (value_type (v));
11020
11021 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11022 if (!num_regs)
11023 return 0;
11024 else
11025 found_memory_cnt += num_regs;
11026 }
11027 }
11028 }
11029 else if (VALUE_LVAL (v) != not_lval
11030 && deprecated_value_modifiable (v) == 0)
11031 return 0; /* These are values from the history (e.g., $1). */
11032 else if (VALUE_LVAL (v) == lval_register)
11033 return 0; /* Cannot watch a register with a HW watchpoint. */
11034 }
11035
11036 /* The expression itself looks suitable for using a hardware
11037 watchpoint, but give the target machine a chance to reject it. */
11038 return found_memory_cnt;
11039 }
11040
11041 void
11042 watch_command_wrapper (const char *arg, int from_tty, int internal)
11043 {
11044 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11045 }
11046
11047 /* A helper function that looks for the "-location" argument and then
11048 calls watch_command_1. */
11049
11050 static void
11051 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
11052 {
11053 int just_location = 0;
11054
11055 if (arg
11056 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11057 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11058 just_location = 1;
11059
11060 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11061 }
11062
11063 static void
11064 watch_command (const char *arg, int from_tty)
11065 {
11066 watch_maybe_just_location (arg, hw_write, from_tty);
11067 }
11068
11069 void
11070 rwatch_command_wrapper (const char *arg, int from_tty, int internal)
11071 {
11072 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11073 }
11074
11075 static void
11076 rwatch_command (const char *arg, int from_tty)
11077 {
11078 watch_maybe_just_location (arg, hw_read, from_tty);
11079 }
11080
11081 void
11082 awatch_command_wrapper (const char *arg, int from_tty, int internal)
11083 {
11084 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11085 }
11086
11087 static void
11088 awatch_command (const char *arg, int from_tty)
11089 {
11090 watch_maybe_just_location (arg, hw_access, from_tty);
11091 }
11092 \f
11093
11094 /* Data for the FSM that manages the until(location)/advance commands
11095 in infcmd.c. Here because it uses the mechanisms of
11096 breakpoints. */
11097
11098 struct until_break_fsm : public thread_fsm
11099 {
11100 /* The thread that was current when the command was executed. */
11101 int thread;
11102
11103 /* The breakpoint set at the return address in the caller frame,
11104 plus breakpoints at all the destination locations. */
11105 std::vector<breakpoint_up> breakpoints;
11106
11107 until_break_fsm (struct interp *cmd_interp, int thread,
11108 std::vector<breakpoint_up> &&breakpoints)
11109 : thread_fsm (cmd_interp),
11110 thread (thread),
11111 breakpoints (std::move (breakpoints))
11112 {
11113 }
11114
11115 void clean_up (struct thread_info *thread) override;
11116 bool should_stop (struct thread_info *thread) override;
11117 enum async_reply_reason do_async_reply_reason () override;
11118 };
11119
11120 /* Implementation of the 'should_stop' FSM method for the
11121 until(location)/advance commands. */
11122
11123 bool
11124 until_break_fsm::should_stop (struct thread_info *tp)
11125 {
11126 for (const breakpoint_up &bp : breakpoints)
11127 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11128 bp.get ()) != NULL)
11129 {
11130 set_finished ();
11131 break;
11132 }
11133
11134 return true;
11135 }
11136
11137 /* Implementation of the 'clean_up' FSM method for the
11138 until(location)/advance commands. */
11139
11140 void
11141 until_break_fsm::clean_up (struct thread_info *)
11142 {
11143 /* Clean up our temporary breakpoints. */
11144 breakpoints.clear ();
11145 delete_longjmp_breakpoint (thread);
11146 }
11147
11148 /* Implementation of the 'async_reply_reason' FSM method for the
11149 until(location)/advance commands. */
11150
11151 enum async_reply_reason
11152 until_break_fsm::do_async_reply_reason ()
11153 {
11154 return EXEC_ASYNC_LOCATION_REACHED;
11155 }
11156
11157 void
11158 until_break_command (const char *arg, int from_tty, int anywhere)
11159 {
11160 struct frame_info *frame;
11161 struct gdbarch *frame_gdbarch;
11162 struct frame_id stack_frame_id;
11163 struct frame_id caller_frame_id;
11164 int thread;
11165 struct thread_info *tp;
11166
11167 clear_proceed_status (0);
11168
11169 /* Set a breakpoint where the user wants it and at return from
11170 this function. */
11171
11172 event_location_up location = string_to_event_location (&arg, current_language);
11173
11174 std::vector<symtab_and_line> sals
11175 = (last_displayed_sal_is_valid ()
11176 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11177 get_last_displayed_symtab (),
11178 get_last_displayed_line ())
11179 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11180 NULL, NULL, 0));
11181
11182 if (sals.empty ())
11183 error (_("Couldn't get information on specified line."));
11184
11185 if (*arg)
11186 error (_("Junk at end of arguments."));
11187
11188 tp = inferior_thread ();
11189 thread = tp->global_num;
11190
11191 /* Note linespec handling above invalidates the frame chain.
11192 Installing a breakpoint also invalidates the frame chain (as it
11193 may need to switch threads), so do any frame handling before
11194 that. */
11195
11196 frame = get_selected_frame (NULL);
11197 frame_gdbarch = get_frame_arch (frame);
11198 stack_frame_id = get_stack_frame_id (frame);
11199 caller_frame_id = frame_unwind_caller_id (frame);
11200
11201 /* Keep within the current frame, or in frames called by the current
11202 one. */
11203
11204 std::vector<breakpoint_up> breakpoints;
11205
11206 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11207
11208 if (frame_id_p (caller_frame_id))
11209 {
11210 struct symtab_and_line sal2;
11211 struct gdbarch *caller_gdbarch;
11212
11213 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11214 sal2.pc = frame_unwind_caller_pc (frame);
11215 caller_gdbarch = frame_unwind_caller_arch (frame);
11216
11217 breakpoint_up caller_breakpoint
11218 = set_momentary_breakpoint (caller_gdbarch, sal2,
11219 caller_frame_id, bp_until);
11220 breakpoints.emplace_back (std::move (caller_breakpoint));
11221
11222 set_longjmp_breakpoint (tp, caller_frame_id);
11223 lj_deleter.emplace (thread);
11224 }
11225
11226 /* set_momentary_breakpoint could invalidate FRAME. */
11227 frame = NULL;
11228
11229 /* If the user told us to continue until a specified location, we
11230 don't specify a frame at which we need to stop. Otherwise,
11231 specify the selected frame, because we want to stop only at the
11232 very same frame. */
11233 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
11234
11235 for (symtab_and_line &sal : sals)
11236 {
11237 resolve_sal_pc (&sal);
11238
11239 breakpoint_up location_breakpoint
11240 = set_momentary_breakpoint (frame_gdbarch, sal,
11241 stop_frame_id, bp_until);
11242 breakpoints.emplace_back (std::move (location_breakpoint));
11243 }
11244
11245 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
11246 std::move (breakpoints));
11247
11248 if (lj_deleter)
11249 lj_deleter->release ();
11250
11251 proceed (-1, GDB_SIGNAL_DEFAULT);
11252 }
11253
11254 /* This function attempts to parse an optional "if <cond>" clause
11255 from the arg string. If one is not found, it returns NULL.
11256
11257 Else, it returns a pointer to the condition string. (It does not
11258 attempt to evaluate the string against a particular block.) And,
11259 it updates arg to point to the first character following the parsed
11260 if clause in the arg string. */
11261
11262 const char *
11263 ep_parse_optional_if_clause (const char **arg)
11264 {
11265 const char *cond_string;
11266
11267 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11268 return NULL;
11269
11270 /* Skip the "if" keyword. */
11271 (*arg) += 2;
11272
11273 /* Skip any extra leading whitespace, and record the start of the
11274 condition string. */
11275 *arg = skip_spaces (*arg);
11276 cond_string = *arg;
11277
11278 /* Assume that the condition occupies the remainder of the arg
11279 string. */
11280 (*arg) += strlen (cond_string);
11281
11282 return cond_string;
11283 }
11284
11285 /* Commands to deal with catching events, such as signals, exceptions,
11286 process start/exit, etc. */
11287
11288 typedef enum
11289 {
11290 catch_fork_temporary, catch_vfork_temporary,
11291 catch_fork_permanent, catch_vfork_permanent
11292 }
11293 catch_fork_kind;
11294
11295 static void
11296 catch_fork_command_1 (const char *arg, int from_tty,
11297 struct cmd_list_element *command)
11298 {
11299 struct gdbarch *gdbarch = get_current_arch ();
11300 const char *cond_string = NULL;
11301 catch_fork_kind fork_kind;
11302
11303 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11304 bool temp = (fork_kind == catch_fork_temporary
11305 || fork_kind == catch_vfork_temporary);
11306
11307 if (!arg)
11308 arg = "";
11309 arg = skip_spaces (arg);
11310
11311 /* The allowed syntax is:
11312 catch [v]fork
11313 catch [v]fork if <cond>
11314
11315 First, check if there's an if clause. */
11316 cond_string = ep_parse_optional_if_clause (&arg);
11317
11318 if ((*arg != '\0') && !isspace (*arg))
11319 error (_("Junk at end of arguments."));
11320
11321 /* If this target supports it, create a fork or vfork catchpoint
11322 and enable reporting of such events. */
11323 switch (fork_kind)
11324 {
11325 case catch_fork_temporary:
11326 case catch_fork_permanent:
11327 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
11328 &catch_fork_breakpoint_ops);
11329 break;
11330 case catch_vfork_temporary:
11331 case catch_vfork_permanent:
11332 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
11333 &catch_vfork_breakpoint_ops);
11334 break;
11335 default:
11336 error (_("unsupported or unknown fork kind; cannot catch it"));
11337 break;
11338 }
11339 }
11340
11341 static void
11342 catch_exec_command_1 (const char *arg, int from_tty,
11343 struct cmd_list_element *command)
11344 {
11345 struct gdbarch *gdbarch = get_current_arch ();
11346 const char *cond_string = NULL;
11347 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
11348
11349 if (!arg)
11350 arg = "";
11351 arg = skip_spaces (arg);
11352
11353 /* The allowed syntax is:
11354 catch exec
11355 catch exec if <cond>
11356
11357 First, check if there's an if clause. */
11358 cond_string = ep_parse_optional_if_clause (&arg);
11359
11360 if ((*arg != '\0') && !isspace (*arg))
11361 error (_("Junk at end of arguments."));
11362
11363 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11364 init_catchpoint (c.get (), gdbarch, temp, cond_string,
11365 &catch_exec_breakpoint_ops);
11366 c->exec_pathname = NULL;
11367
11368 install_breakpoint (0, std::move (c), 1);
11369 }
11370
11371 void
11372 init_ada_exception_breakpoint (struct breakpoint *b,
11373 struct gdbarch *gdbarch,
11374 struct symtab_and_line sal,
11375 const char *addr_string,
11376 const struct breakpoint_ops *ops,
11377 int tempflag,
11378 int enabled,
11379 int from_tty)
11380 {
11381 if (from_tty)
11382 {
11383 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11384 if (!loc_gdbarch)
11385 loc_gdbarch = gdbarch;
11386
11387 describe_other_breakpoints (loc_gdbarch,
11388 sal.pspace, sal.pc, sal.section, -1);
11389 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11390 version for exception catchpoints, because two catchpoints
11391 used for different exception names will use the same address.
11392 In this case, a "breakpoint ... also set at..." warning is
11393 unproductive. Besides, the warning phrasing is also a bit
11394 inappropriate, we should use the word catchpoint, and tell
11395 the user what type of catchpoint it is. The above is good
11396 enough for now, though. */
11397 }
11398
11399 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
11400
11401 b->enable_state = enabled ? bp_enabled : bp_disabled;
11402 b->disposition = tempflag ? disp_del : disp_donttouch;
11403 b->location = string_to_event_location (&addr_string,
11404 language_def (language_ada));
11405 b->language = language_ada;
11406 }
11407
11408 \f
11409
11410 /* Compare two breakpoints and return a strcmp-like result. */
11411
11412 static int
11413 compare_breakpoints (const breakpoint *a, const breakpoint *b)
11414 {
11415 uintptr_t ua = (uintptr_t) a;
11416 uintptr_t ub = (uintptr_t) b;
11417
11418 if (a->number < b->number)
11419 return -1;
11420 else if (a->number > b->number)
11421 return 1;
11422
11423 /* Now sort by address, in case we see, e..g, two breakpoints with
11424 the number 0. */
11425 if (ua < ub)
11426 return -1;
11427 return ua > ub ? 1 : 0;
11428 }
11429
11430 /* Delete breakpoints by address or line. */
11431
11432 static void
11433 clear_command (const char *arg, int from_tty)
11434 {
11435 struct breakpoint *b;
11436 int default_match;
11437
11438 std::vector<symtab_and_line> decoded_sals;
11439 symtab_and_line last_sal;
11440 gdb::array_view<symtab_and_line> sals;
11441 if (arg)
11442 {
11443 decoded_sals
11444 = decode_line_with_current_source (arg,
11445 (DECODE_LINE_FUNFIRSTLINE
11446 | DECODE_LINE_LIST_MODE));
11447 default_match = 0;
11448 sals = decoded_sals;
11449 }
11450 else
11451 {
11452 /* Set sal's line, symtab, pc, and pspace to the values
11453 corresponding to the last call to print_frame_info. If the
11454 codepoint is not valid, this will set all the fields to 0. */
11455 last_sal = get_last_displayed_sal ();
11456 if (last_sal.symtab == 0)
11457 error (_("No source file specified."));
11458
11459 default_match = 1;
11460 sals = last_sal;
11461 }
11462
11463 /* We don't call resolve_sal_pc here. That's not as bad as it
11464 seems, because all existing breakpoints typically have both
11465 file/line and pc set. So, if clear is given file/line, we can
11466 match this to existing breakpoint without obtaining pc at all.
11467
11468 We only support clearing given the address explicitly
11469 present in breakpoint table. Say, we've set breakpoint
11470 at file:line. There were several PC values for that file:line,
11471 due to optimization, all in one block.
11472
11473 We've picked one PC value. If "clear" is issued with another
11474 PC corresponding to the same file:line, the breakpoint won't
11475 be cleared. We probably can still clear the breakpoint, but
11476 since the other PC value is never presented to user, user
11477 can only find it by guessing, and it does not seem important
11478 to support that. */
11479
11480 /* For each line spec given, delete bps which correspond to it. Do
11481 it in two passes, solely to preserve the current behavior that
11482 from_tty is forced true if we delete more than one
11483 breakpoint. */
11484
11485 std::vector<struct breakpoint *> found;
11486 for (const auto &sal : sals)
11487 {
11488 const char *sal_fullname;
11489
11490 /* If exact pc given, clear bpts at that pc.
11491 If line given (pc == 0), clear all bpts on specified line.
11492 If defaulting, clear all bpts on default line
11493 or at default pc.
11494
11495 defaulting sal.pc != 0 tests to do
11496
11497 0 1 pc
11498 1 1 pc _and_ line
11499 0 0 line
11500 1 0 <can't happen> */
11501
11502 sal_fullname = (sal.symtab == NULL
11503 ? NULL : symtab_to_fullname (sal.symtab));
11504
11505 /* Find all matching breakpoints and add them to 'found'. */
11506 ALL_BREAKPOINTS (b)
11507 {
11508 int match = 0;
11509 /* Are we going to delete b? */
11510 if (b->type != bp_none && !is_watchpoint (b))
11511 {
11512 struct bp_location *loc = b->loc;
11513 for (; loc; loc = loc->next)
11514 {
11515 /* If the user specified file:line, don't allow a PC
11516 match. This matches historical gdb behavior. */
11517 int pc_match = (!sal.explicit_line
11518 && sal.pc
11519 && (loc->pspace == sal.pspace)
11520 && (loc->address == sal.pc)
11521 && (!section_is_overlay (loc->section)
11522 || loc->section == sal.section));
11523 int line_match = 0;
11524
11525 if ((default_match || sal.explicit_line)
11526 && loc->symtab != NULL
11527 && sal_fullname != NULL
11528 && sal.pspace == loc->pspace
11529 && loc->line_number == sal.line
11530 && filename_cmp (symtab_to_fullname (loc->symtab),
11531 sal_fullname) == 0)
11532 line_match = 1;
11533
11534 if (pc_match || line_match)
11535 {
11536 match = 1;
11537 break;
11538 }
11539 }
11540 }
11541
11542 if (match)
11543 found.push_back (b);
11544 }
11545 }
11546
11547 /* Now go thru the 'found' chain and delete them. */
11548 if (found.empty ())
11549 {
11550 if (arg)
11551 error (_("No breakpoint at %s."), arg);
11552 else
11553 error (_("No breakpoint at this line."));
11554 }
11555
11556 /* Remove duplicates from the vec. */
11557 std::sort (found.begin (), found.end (),
11558 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11559 {
11560 return compare_breakpoints (bp_a, bp_b) < 0;
11561 });
11562 found.erase (std::unique (found.begin (), found.end (),
11563 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11564 {
11565 return compare_breakpoints (bp_a, bp_b) == 0;
11566 }),
11567 found.end ());
11568
11569 if (found.size () > 1)
11570 from_tty = 1; /* Always report if deleted more than one. */
11571 if (from_tty)
11572 {
11573 if (found.size () == 1)
11574 printf_unfiltered (_("Deleted breakpoint "));
11575 else
11576 printf_unfiltered (_("Deleted breakpoints "));
11577 }
11578
11579 for (breakpoint *iter : found)
11580 {
11581 if (from_tty)
11582 printf_unfiltered ("%d ", iter->number);
11583 delete_breakpoint (iter);
11584 }
11585 if (from_tty)
11586 putchar_unfiltered ('\n');
11587 }
11588 \f
11589 /* Delete breakpoint in BS if they are `delete' breakpoints and
11590 all breakpoints that are marked for deletion, whether hit or not.
11591 This is called after any breakpoint is hit, or after errors. */
11592
11593 void
11594 breakpoint_auto_delete (bpstat bs)
11595 {
11596 struct breakpoint *b, *b_tmp;
11597
11598 for (; bs; bs = bs->next)
11599 if (bs->breakpoint_at
11600 && bs->breakpoint_at->disposition == disp_del
11601 && bs->stop)
11602 delete_breakpoint (bs->breakpoint_at);
11603
11604 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11605 {
11606 if (b->disposition == disp_del_at_next_stop)
11607 delete_breakpoint (b);
11608 }
11609 }
11610
11611 /* A comparison function for bp_location AP and BP being interfaced to
11612 std::sort. Sort elements primarily by their ADDRESS (no matter what
11613 bl_address_is_meaningful says), secondarily by ordering first
11614 permanent elements and terciarily just ensuring the array is sorted
11615 stable way despite std::sort being an unstable algorithm. */
11616
11617 static int
11618 bp_location_is_less_than (const bp_location *a, const bp_location *b)
11619 {
11620 if (a->address != b->address)
11621 return a->address < b->address;
11622
11623 /* Sort locations at the same address by their pspace number, keeping
11624 locations of the same inferior (in a multi-inferior environment)
11625 grouped. */
11626
11627 if (a->pspace->num != b->pspace->num)
11628 return a->pspace->num < b->pspace->num;
11629
11630 /* Sort permanent breakpoints first. */
11631 if (a->permanent != b->permanent)
11632 return a->permanent > b->permanent;
11633
11634 /* Sort by type in order to make duplicate determination easier.
11635 See update_global_location_list. This is kept in sync with
11636 breakpoint_locations_match. */
11637 if (a->loc_type < b->loc_type)
11638 return true;
11639
11640 /* Likewise, for range-breakpoints, sort by length. */
11641 if (a->loc_type == bp_loc_hardware_breakpoint
11642 && b->loc_type == bp_loc_hardware_breakpoint
11643 && a->length < b->length)
11644 return true;
11645
11646 /* Make the internal GDB representation stable across GDB runs
11647 where A and B memory inside GDB can differ. Breakpoint locations of
11648 the same type at the same address can be sorted in arbitrary order. */
11649
11650 if (a->owner->number != b->owner->number)
11651 return a->owner->number < b->owner->number;
11652
11653 return a < b;
11654 }
11655
11656 /* Set bp_locations_placed_address_before_address_max and
11657 bp_locations_shadow_len_after_address_max according to the current
11658 content of the bp_locations array. */
11659
11660 static void
11661 bp_locations_target_extensions_update (void)
11662 {
11663 struct bp_location *bl, **blp_tmp;
11664
11665 bp_locations_placed_address_before_address_max = 0;
11666 bp_locations_shadow_len_after_address_max = 0;
11667
11668 ALL_BP_LOCATIONS (bl, blp_tmp)
11669 {
11670 CORE_ADDR start, end, addr;
11671
11672 if (!bp_location_has_shadow (bl))
11673 continue;
11674
11675 start = bl->target_info.placed_address;
11676 end = start + bl->target_info.shadow_len;
11677
11678 gdb_assert (bl->address >= start);
11679 addr = bl->address - start;
11680 if (addr > bp_locations_placed_address_before_address_max)
11681 bp_locations_placed_address_before_address_max = addr;
11682
11683 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11684
11685 gdb_assert (bl->address < end);
11686 addr = end - bl->address;
11687 if (addr > bp_locations_shadow_len_after_address_max)
11688 bp_locations_shadow_len_after_address_max = addr;
11689 }
11690 }
11691
11692 /* Download tracepoint locations if they haven't been. */
11693
11694 static void
11695 download_tracepoint_locations (void)
11696 {
11697 struct breakpoint *b;
11698 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11699
11700 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11701
11702 ALL_TRACEPOINTS (b)
11703 {
11704 struct bp_location *bl;
11705 struct tracepoint *t;
11706 int bp_location_downloaded = 0;
11707
11708 if ((b->type == bp_fast_tracepoint
11709 ? !may_insert_fast_tracepoints
11710 : !may_insert_tracepoints))
11711 continue;
11712
11713 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11714 {
11715 if (target_can_download_tracepoint ())
11716 can_download_tracepoint = TRIBOOL_TRUE;
11717 else
11718 can_download_tracepoint = TRIBOOL_FALSE;
11719 }
11720
11721 if (can_download_tracepoint == TRIBOOL_FALSE)
11722 break;
11723
11724 for (bl = b->loc; bl; bl = bl->next)
11725 {
11726 /* In tracepoint, locations are _never_ duplicated, so
11727 should_be_inserted is equivalent to
11728 unduplicated_should_be_inserted. */
11729 if (!should_be_inserted (bl) || bl->inserted)
11730 continue;
11731
11732 switch_to_program_space_and_thread (bl->pspace);
11733
11734 target_download_tracepoint (bl);
11735
11736 bl->inserted = 1;
11737 bp_location_downloaded = 1;
11738 }
11739 t = (struct tracepoint *) b;
11740 t->number_on_target = b->number;
11741 if (bp_location_downloaded)
11742 gdb::observers::breakpoint_modified.notify (b);
11743 }
11744 }
11745
11746 /* Swap the insertion/duplication state between two locations. */
11747
11748 static void
11749 swap_insertion (struct bp_location *left, struct bp_location *right)
11750 {
11751 const int left_inserted = left->inserted;
11752 const int left_duplicate = left->duplicate;
11753 const int left_needs_update = left->needs_update;
11754 const struct bp_target_info left_target_info = left->target_info;
11755
11756 /* Locations of tracepoints can never be duplicated. */
11757 if (is_tracepoint (left->owner))
11758 gdb_assert (!left->duplicate);
11759 if (is_tracepoint (right->owner))
11760 gdb_assert (!right->duplicate);
11761
11762 left->inserted = right->inserted;
11763 left->duplicate = right->duplicate;
11764 left->needs_update = right->needs_update;
11765 left->target_info = right->target_info;
11766 right->inserted = left_inserted;
11767 right->duplicate = left_duplicate;
11768 right->needs_update = left_needs_update;
11769 right->target_info = left_target_info;
11770 }
11771
11772 /* Force the re-insertion of the locations at ADDRESS. This is called
11773 once a new/deleted/modified duplicate location is found and we are evaluating
11774 conditions on the target's side. Such conditions need to be updated on
11775 the target. */
11776
11777 static void
11778 force_breakpoint_reinsertion (struct bp_location *bl)
11779 {
11780 struct bp_location **locp = NULL, **loc2p;
11781 struct bp_location *loc;
11782 CORE_ADDR address = 0;
11783 int pspace_num;
11784
11785 address = bl->address;
11786 pspace_num = bl->pspace->num;
11787
11788 /* This is only meaningful if the target is
11789 evaluating conditions and if the user has
11790 opted for condition evaluation on the target's
11791 side. */
11792 if (gdb_evaluates_breakpoint_condition_p ()
11793 || !target_supports_evaluation_of_breakpoint_conditions ())
11794 return;
11795
11796 /* Flag all breakpoint locations with this address and
11797 the same program space as the location
11798 as "its condition has changed". We need to
11799 update the conditions on the target's side. */
11800 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11801 {
11802 loc = *loc2p;
11803
11804 if (!is_breakpoint (loc->owner)
11805 || pspace_num != loc->pspace->num)
11806 continue;
11807
11808 /* Flag the location appropriately. We use a different state to
11809 let everyone know that we already updated the set of locations
11810 with addr bl->address and program space bl->pspace. This is so
11811 we don't have to keep calling these functions just to mark locations
11812 that have already been marked. */
11813 loc->condition_changed = condition_updated;
11814
11815 /* Free the agent expression bytecode as well. We will compute
11816 it later on. */
11817 loc->cond_bytecode.reset ();
11818 }
11819 }
11820
11821 /* Called whether new breakpoints are created, or existing breakpoints
11822 deleted, to update the global location list and recompute which
11823 locations are duplicate of which.
11824
11825 The INSERT_MODE flag determines whether locations may not, may, or
11826 shall be inserted now. See 'enum ugll_insert_mode' for more
11827 info. */
11828
11829 static void
11830 update_global_location_list (enum ugll_insert_mode insert_mode)
11831 {
11832 struct breakpoint *b;
11833 struct bp_location **locp, *loc;
11834 /* Last breakpoint location address that was marked for update. */
11835 CORE_ADDR last_addr = 0;
11836 /* Last breakpoint location program space that was marked for update. */
11837 int last_pspace_num = -1;
11838
11839 /* Used in the duplicates detection below. When iterating over all
11840 bp_locations, points to the first bp_location of a given address.
11841 Breakpoints and watchpoints of different types are never
11842 duplicates of each other. Keep one pointer for each type of
11843 breakpoint/watchpoint, so we only need to loop over all locations
11844 once. */
11845 struct bp_location *bp_loc_first; /* breakpoint */
11846 struct bp_location *wp_loc_first; /* hardware watchpoint */
11847 struct bp_location *awp_loc_first; /* access watchpoint */
11848 struct bp_location *rwp_loc_first; /* read watchpoint */
11849
11850 /* Saved former bp_locations array which we compare against the newly
11851 built bp_locations from the current state of ALL_BREAKPOINTS. */
11852 struct bp_location **old_locp;
11853 unsigned old_locations_count;
11854 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
11855
11856 old_locations_count = bp_locations_count;
11857 bp_locations = NULL;
11858 bp_locations_count = 0;
11859
11860 ALL_BREAKPOINTS (b)
11861 for (loc = b->loc; loc; loc = loc->next)
11862 bp_locations_count++;
11863
11864 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11865 locp = bp_locations;
11866 ALL_BREAKPOINTS (b)
11867 for (loc = b->loc; loc; loc = loc->next)
11868 *locp++ = loc;
11869
11870 /* See if we need to "upgrade" a software breakpoint to a hardware
11871 breakpoint. Do this before deciding whether locations are
11872 duplicates. Also do this before sorting because sorting order
11873 depends on location type. */
11874 for (locp = bp_locations;
11875 locp < bp_locations + bp_locations_count;
11876 locp++)
11877 {
11878 loc = *locp;
11879 if (!loc->inserted && should_be_inserted (loc))
11880 handle_automatic_hardware_breakpoints (loc);
11881 }
11882
11883 std::sort (bp_locations, bp_locations + bp_locations_count,
11884 bp_location_is_less_than);
11885
11886 bp_locations_target_extensions_update ();
11887
11888 /* Identify bp_location instances that are no longer present in the
11889 new list, and therefore should be freed. Note that it's not
11890 necessary that those locations should be removed from inferior --
11891 if there's another location at the same address (previously
11892 marked as duplicate), we don't need to remove/insert the
11893 location.
11894
11895 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11896 and former bp_location array state respectively. */
11897
11898 locp = bp_locations;
11899 for (old_locp = old_locations.get ();
11900 old_locp < old_locations.get () + old_locations_count;
11901 old_locp++)
11902 {
11903 struct bp_location *old_loc = *old_locp;
11904 struct bp_location **loc2p;
11905
11906 /* Tells if 'old_loc' is found among the new locations. If
11907 not, we have to free it. */
11908 int found_object = 0;
11909 /* Tells if the location should remain inserted in the target. */
11910 int keep_in_target = 0;
11911 int removed = 0;
11912
11913 /* Skip LOCP entries which will definitely never be needed.
11914 Stop either at or being the one matching OLD_LOC. */
11915 while (locp < bp_locations + bp_locations_count
11916 && (*locp)->address < old_loc->address)
11917 locp++;
11918
11919 for (loc2p = locp;
11920 (loc2p < bp_locations + bp_locations_count
11921 && (*loc2p)->address == old_loc->address);
11922 loc2p++)
11923 {
11924 /* Check if this is a new/duplicated location or a duplicated
11925 location that had its condition modified. If so, we want to send
11926 its condition to the target if evaluation of conditions is taking
11927 place there. */
11928 if ((*loc2p)->condition_changed == condition_modified
11929 && (last_addr != old_loc->address
11930 || last_pspace_num != old_loc->pspace->num))
11931 {
11932 force_breakpoint_reinsertion (*loc2p);
11933 last_pspace_num = old_loc->pspace->num;
11934 }
11935
11936 if (*loc2p == old_loc)
11937 found_object = 1;
11938 }
11939
11940 /* We have already handled this address, update it so that we don't
11941 have to go through updates again. */
11942 last_addr = old_loc->address;
11943
11944 /* Target-side condition evaluation: Handle deleted locations. */
11945 if (!found_object)
11946 force_breakpoint_reinsertion (old_loc);
11947
11948 /* If this location is no longer present, and inserted, look if
11949 there's maybe a new location at the same address. If so,
11950 mark that one inserted, and don't remove this one. This is
11951 needed so that we don't have a time window where a breakpoint
11952 at certain location is not inserted. */
11953
11954 if (old_loc->inserted)
11955 {
11956 /* If the location is inserted now, we might have to remove
11957 it. */
11958
11959 if (found_object && should_be_inserted (old_loc))
11960 {
11961 /* The location is still present in the location list,
11962 and still should be inserted. Don't do anything. */
11963 keep_in_target = 1;
11964 }
11965 else
11966 {
11967 /* This location still exists, but it won't be kept in the
11968 target since it may have been disabled. We proceed to
11969 remove its target-side condition. */
11970
11971 /* The location is either no longer present, or got
11972 disabled. See if there's another location at the
11973 same address, in which case we don't need to remove
11974 this one from the target. */
11975
11976 /* OLD_LOC comes from existing struct breakpoint. */
11977 if (bl_address_is_meaningful (old_loc))
11978 {
11979 for (loc2p = locp;
11980 (loc2p < bp_locations + bp_locations_count
11981 && (*loc2p)->address == old_loc->address);
11982 loc2p++)
11983 {
11984 struct bp_location *loc2 = *loc2p;
11985
11986 if (loc2 == old_loc)
11987 continue;
11988
11989 if (breakpoint_locations_match (loc2, old_loc))
11990 {
11991 /* Read watchpoint locations are switched to
11992 access watchpoints, if the former are not
11993 supported, but the latter are. */
11994 if (is_hardware_watchpoint (old_loc->owner))
11995 {
11996 gdb_assert (is_hardware_watchpoint (loc2->owner));
11997 loc2->watchpoint_type = old_loc->watchpoint_type;
11998 }
11999
12000 /* loc2 is a duplicated location. We need to check
12001 if it should be inserted in case it will be
12002 unduplicated. */
12003 if (unduplicated_should_be_inserted (loc2))
12004 {
12005 swap_insertion (old_loc, loc2);
12006 keep_in_target = 1;
12007 break;
12008 }
12009 }
12010 }
12011 }
12012 }
12013
12014 if (!keep_in_target)
12015 {
12016 if (remove_breakpoint (old_loc))
12017 {
12018 /* This is just about all we can do. We could keep
12019 this location on the global list, and try to
12020 remove it next time, but there's no particular
12021 reason why we will succeed next time.
12022
12023 Note that at this point, old_loc->owner is still
12024 valid, as delete_breakpoint frees the breakpoint
12025 only after calling us. */
12026 printf_filtered (_("warning: Error removing "
12027 "breakpoint %d\n"),
12028 old_loc->owner->number);
12029 }
12030 removed = 1;
12031 }
12032 }
12033
12034 if (!found_object)
12035 {
12036 if (removed && target_is_non_stop_p ()
12037 && need_moribund_for_location_type (old_loc))
12038 {
12039 /* This location was removed from the target. In
12040 non-stop mode, a race condition is possible where
12041 we've removed a breakpoint, but stop events for that
12042 breakpoint are already queued and will arrive later.
12043 We apply an heuristic to be able to distinguish such
12044 SIGTRAPs from other random SIGTRAPs: we keep this
12045 breakpoint location for a bit, and will retire it
12046 after we see some number of events. The theory here
12047 is that reporting of events should, "on the average",
12048 be fair, so after a while we'll see events from all
12049 threads that have anything of interest, and no longer
12050 need to keep this breakpoint location around. We
12051 don't hold locations forever so to reduce chances of
12052 mistaking a non-breakpoint SIGTRAP for a breakpoint
12053 SIGTRAP.
12054
12055 The heuristic failing can be disastrous on
12056 decr_pc_after_break targets.
12057
12058 On decr_pc_after_break targets, like e.g., x86-linux,
12059 if we fail to recognize a late breakpoint SIGTRAP,
12060 because events_till_retirement has reached 0 too
12061 soon, we'll fail to do the PC adjustment, and report
12062 a random SIGTRAP to the user. When the user resumes
12063 the inferior, it will most likely immediately crash
12064 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12065 corrupted, because of being resumed e.g., in the
12066 middle of a multi-byte instruction, or skipped a
12067 one-byte instruction. This was actually seen happen
12068 on native x86-linux, and should be less rare on
12069 targets that do not support new thread events, like
12070 remote, due to the heuristic depending on
12071 thread_count.
12072
12073 Mistaking a random SIGTRAP for a breakpoint trap
12074 causes similar symptoms (PC adjustment applied when
12075 it shouldn't), but then again, playing with SIGTRAPs
12076 behind the debugger's back is asking for trouble.
12077
12078 Since hardware watchpoint traps are always
12079 distinguishable from other traps, so we don't need to
12080 apply keep hardware watchpoint moribund locations
12081 around. We simply always ignore hardware watchpoint
12082 traps we can no longer explain. */
12083
12084 process_stratum_target *proc_target = nullptr;
12085 for (inferior *inf : all_inferiors ())
12086 if (inf->pspace == old_loc->pspace)
12087 {
12088 proc_target = inf->process_target ();
12089 break;
12090 }
12091 if (proc_target != nullptr)
12092 old_loc->events_till_retirement
12093 = 3 * (thread_count (proc_target) + 1);
12094 else
12095 old_loc->events_till_retirement = 1;
12096 old_loc->owner = NULL;
12097
12098 moribund_locations.push_back (old_loc);
12099 }
12100 else
12101 {
12102 old_loc->owner = NULL;
12103 decref_bp_location (&old_loc);
12104 }
12105 }
12106 }
12107
12108 /* Rescan breakpoints at the same address and section, marking the
12109 first one as "first" and any others as "duplicates". This is so
12110 that the bpt instruction is only inserted once. If we have a
12111 permanent breakpoint at the same place as BPT, make that one the
12112 official one, and the rest as duplicates. Permanent breakpoints
12113 are sorted first for the same address.
12114
12115 Do the same for hardware watchpoints, but also considering the
12116 watchpoint's type (regular/access/read) and length. */
12117
12118 bp_loc_first = NULL;
12119 wp_loc_first = NULL;
12120 awp_loc_first = NULL;
12121 rwp_loc_first = NULL;
12122 ALL_BP_LOCATIONS (loc, locp)
12123 {
12124 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12125 non-NULL. */
12126 struct bp_location **loc_first_p;
12127 b = loc->owner;
12128
12129 if (!unduplicated_should_be_inserted (loc)
12130 || !bl_address_is_meaningful (loc)
12131 /* Don't detect duplicate for tracepoint locations because they are
12132 never duplicated. See the comments in field `duplicate' of
12133 `struct bp_location'. */
12134 || is_tracepoint (b))
12135 {
12136 /* Clear the condition modification flag. */
12137 loc->condition_changed = condition_unchanged;
12138 continue;
12139 }
12140
12141 if (b->type == bp_hardware_watchpoint)
12142 loc_first_p = &wp_loc_first;
12143 else if (b->type == bp_read_watchpoint)
12144 loc_first_p = &rwp_loc_first;
12145 else if (b->type == bp_access_watchpoint)
12146 loc_first_p = &awp_loc_first;
12147 else
12148 loc_first_p = &bp_loc_first;
12149
12150 if (*loc_first_p == NULL
12151 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12152 || !breakpoint_locations_match (loc, *loc_first_p))
12153 {
12154 *loc_first_p = loc;
12155 loc->duplicate = 0;
12156
12157 if (is_breakpoint (loc->owner) && loc->condition_changed)
12158 {
12159 loc->needs_update = 1;
12160 /* Clear the condition modification flag. */
12161 loc->condition_changed = condition_unchanged;
12162 }
12163 continue;
12164 }
12165
12166
12167 /* This and the above ensure the invariant that the first location
12168 is not duplicated, and is the inserted one.
12169 All following are marked as duplicated, and are not inserted. */
12170 if (loc->inserted)
12171 swap_insertion (loc, *loc_first_p);
12172 loc->duplicate = 1;
12173
12174 /* Clear the condition modification flag. */
12175 loc->condition_changed = condition_unchanged;
12176 }
12177
12178 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12179 {
12180 if (insert_mode != UGLL_DONT_INSERT)
12181 insert_breakpoint_locations ();
12182 else
12183 {
12184 /* Even though the caller told us to not insert new
12185 locations, we may still need to update conditions on the
12186 target's side of breakpoints that were already inserted
12187 if the target is evaluating breakpoint conditions. We
12188 only update conditions for locations that are marked
12189 "needs_update". */
12190 update_inserted_breakpoint_locations ();
12191 }
12192 }
12193
12194 if (insert_mode != UGLL_DONT_INSERT)
12195 download_tracepoint_locations ();
12196 }
12197
12198 void
12199 breakpoint_retire_moribund (void)
12200 {
12201 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12202 {
12203 struct bp_location *loc = moribund_locations[ix];
12204 if (--(loc->events_till_retirement) == 0)
12205 {
12206 decref_bp_location (&loc);
12207 unordered_remove (moribund_locations, ix);
12208 --ix;
12209 }
12210 }
12211 }
12212
12213 static void
12214 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12215 {
12216
12217 try
12218 {
12219 update_global_location_list (insert_mode);
12220 }
12221 catch (const gdb_exception_error &e)
12222 {
12223 }
12224 }
12225
12226 /* Clear BKP from a BPS. */
12227
12228 static void
12229 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12230 {
12231 bpstat bs;
12232
12233 for (bs = bps; bs; bs = bs->next)
12234 if (bs->breakpoint_at == bpt)
12235 {
12236 bs->breakpoint_at = NULL;
12237 bs->old_val = NULL;
12238 /* bs->commands will be freed later. */
12239 }
12240 }
12241
12242 /* Callback for iterate_over_threads. */
12243 static int
12244 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12245 {
12246 struct breakpoint *bpt = (struct breakpoint *) data;
12247
12248 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12249 return 0;
12250 }
12251
12252 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12253 callbacks. */
12254
12255 static void
12256 say_where (struct breakpoint *b)
12257 {
12258 struct value_print_options opts;
12259
12260 get_user_print_options (&opts);
12261
12262 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12263 single string. */
12264 if (b->loc == NULL)
12265 {
12266 /* For pending locations, the output differs slightly based
12267 on b->extra_string. If this is non-NULL, it contains either
12268 a condition or dprintf arguments. */
12269 if (b->extra_string == NULL)
12270 {
12271 printf_filtered (_(" (%s) pending."),
12272 event_location_to_string (b->location.get ()));
12273 }
12274 else if (b->type == bp_dprintf)
12275 {
12276 printf_filtered (_(" (%s,%s) pending."),
12277 event_location_to_string (b->location.get ()),
12278 b->extra_string);
12279 }
12280 else
12281 {
12282 printf_filtered (_(" (%s %s) pending."),
12283 event_location_to_string (b->location.get ()),
12284 b->extra_string);
12285 }
12286 }
12287 else
12288 {
12289 if (opts.addressprint || b->loc->symtab == NULL)
12290 printf_filtered (" at %ps",
12291 styled_string (address_style.style (),
12292 paddress (b->loc->gdbarch,
12293 b->loc->address)));
12294 if (b->loc->symtab != NULL)
12295 {
12296 /* If there is a single location, we can print the location
12297 more nicely. */
12298 if (b->loc->next == NULL)
12299 {
12300 const char *filename
12301 = symtab_to_filename_for_display (b->loc->symtab);
12302 printf_filtered (": file %ps, line %d.",
12303 styled_string (file_name_style.style (),
12304 filename),
12305 b->loc->line_number);
12306 }
12307 else
12308 /* This is not ideal, but each location may have a
12309 different file name, and this at least reflects the
12310 real situation somewhat. */
12311 printf_filtered (": %s.",
12312 event_location_to_string (b->location.get ()));
12313 }
12314
12315 if (b->loc->next)
12316 {
12317 struct bp_location *loc = b->loc;
12318 int n = 0;
12319 for (; loc; loc = loc->next)
12320 ++n;
12321 printf_filtered (" (%d locations)", n);
12322 }
12323 }
12324 }
12325
12326 bp_location::~bp_location ()
12327 {
12328 xfree (function_name);
12329 }
12330
12331 /* Destructor for the breakpoint base class. */
12332
12333 breakpoint::~breakpoint ()
12334 {
12335 xfree (this->cond_string);
12336 xfree (this->extra_string);
12337 }
12338
12339 static struct bp_location *
12340 base_breakpoint_allocate_location (struct breakpoint *self)
12341 {
12342 return new bp_location (self);
12343 }
12344
12345 static void
12346 base_breakpoint_re_set (struct breakpoint *b)
12347 {
12348 /* Nothing to re-set. */
12349 }
12350
12351 #define internal_error_pure_virtual_called() \
12352 gdb_assert_not_reached ("pure virtual function called")
12353
12354 static int
12355 base_breakpoint_insert_location (struct bp_location *bl)
12356 {
12357 internal_error_pure_virtual_called ();
12358 }
12359
12360 static int
12361 base_breakpoint_remove_location (struct bp_location *bl,
12362 enum remove_bp_reason reason)
12363 {
12364 internal_error_pure_virtual_called ();
12365 }
12366
12367 static int
12368 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12369 const address_space *aspace,
12370 CORE_ADDR bp_addr,
12371 const struct target_waitstatus *ws)
12372 {
12373 internal_error_pure_virtual_called ();
12374 }
12375
12376 static void
12377 base_breakpoint_check_status (bpstat bs)
12378 {
12379 /* Always stop. */
12380 }
12381
12382 /* A "works_in_software_mode" breakpoint_ops method that just internal
12383 errors. */
12384
12385 static int
12386 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12387 {
12388 internal_error_pure_virtual_called ();
12389 }
12390
12391 /* A "resources_needed" breakpoint_ops method that just internal
12392 errors. */
12393
12394 static int
12395 base_breakpoint_resources_needed (const struct bp_location *bl)
12396 {
12397 internal_error_pure_virtual_called ();
12398 }
12399
12400 static enum print_stop_action
12401 base_breakpoint_print_it (bpstat bs)
12402 {
12403 internal_error_pure_virtual_called ();
12404 }
12405
12406 static void
12407 base_breakpoint_print_one_detail (const struct breakpoint *self,
12408 struct ui_out *uiout)
12409 {
12410 /* nothing */
12411 }
12412
12413 static void
12414 base_breakpoint_print_mention (struct breakpoint *b)
12415 {
12416 internal_error_pure_virtual_called ();
12417 }
12418
12419 static void
12420 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12421 {
12422 internal_error_pure_virtual_called ();
12423 }
12424
12425 static void
12426 base_breakpoint_create_sals_from_location
12427 (struct event_location *location,
12428 struct linespec_result *canonical,
12429 enum bptype type_wanted)
12430 {
12431 internal_error_pure_virtual_called ();
12432 }
12433
12434 static void
12435 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12436 struct linespec_result *c,
12437 gdb::unique_xmalloc_ptr<char> cond_string,
12438 gdb::unique_xmalloc_ptr<char> extra_string,
12439 enum bptype type_wanted,
12440 enum bpdisp disposition,
12441 int thread,
12442 int task, int ignore_count,
12443 const struct breakpoint_ops *o,
12444 int from_tty, int enabled,
12445 int internal, unsigned flags)
12446 {
12447 internal_error_pure_virtual_called ();
12448 }
12449
12450 static std::vector<symtab_and_line>
12451 base_breakpoint_decode_location (struct breakpoint *b,
12452 struct event_location *location,
12453 struct program_space *search_pspace)
12454 {
12455 internal_error_pure_virtual_called ();
12456 }
12457
12458 /* The default 'explains_signal' method. */
12459
12460 static int
12461 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12462 {
12463 return 1;
12464 }
12465
12466 /* The default "after_condition_true" method. */
12467
12468 static void
12469 base_breakpoint_after_condition_true (struct bpstats *bs)
12470 {
12471 /* Nothing to do. */
12472 }
12473
12474 struct breakpoint_ops base_breakpoint_ops =
12475 {
12476 base_breakpoint_allocate_location,
12477 base_breakpoint_re_set,
12478 base_breakpoint_insert_location,
12479 base_breakpoint_remove_location,
12480 base_breakpoint_breakpoint_hit,
12481 base_breakpoint_check_status,
12482 base_breakpoint_resources_needed,
12483 base_breakpoint_works_in_software_mode,
12484 base_breakpoint_print_it,
12485 NULL,
12486 base_breakpoint_print_one_detail,
12487 base_breakpoint_print_mention,
12488 base_breakpoint_print_recreate,
12489 base_breakpoint_create_sals_from_location,
12490 base_breakpoint_create_breakpoints_sal,
12491 base_breakpoint_decode_location,
12492 base_breakpoint_explains_signal,
12493 base_breakpoint_after_condition_true,
12494 };
12495
12496 /* Default breakpoint_ops methods. */
12497
12498 static void
12499 bkpt_re_set (struct breakpoint *b)
12500 {
12501 /* FIXME: is this still reachable? */
12502 if (breakpoint_event_location_empty_p (b))
12503 {
12504 /* Anything without a location can't be re-set. */
12505 delete_breakpoint (b);
12506 return;
12507 }
12508
12509 breakpoint_re_set_default (b);
12510 }
12511
12512 static int
12513 bkpt_insert_location (struct bp_location *bl)
12514 {
12515 CORE_ADDR addr = bl->target_info.reqstd_address;
12516
12517 bl->target_info.kind = breakpoint_kind (bl, &addr);
12518 bl->target_info.placed_address = addr;
12519
12520 if (bl->loc_type == bp_loc_hardware_breakpoint)
12521 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12522 else
12523 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12524 }
12525
12526 static int
12527 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12528 {
12529 if (bl->loc_type == bp_loc_hardware_breakpoint)
12530 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12531 else
12532 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12533 }
12534
12535 static int
12536 bkpt_breakpoint_hit (const struct bp_location *bl,
12537 const address_space *aspace, CORE_ADDR bp_addr,
12538 const struct target_waitstatus *ws)
12539 {
12540 if (ws->kind != TARGET_WAITKIND_STOPPED
12541 || ws->value.sig != GDB_SIGNAL_TRAP)
12542 return 0;
12543
12544 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12545 aspace, bp_addr))
12546 return 0;
12547
12548 if (overlay_debugging /* unmapped overlay section */
12549 && section_is_overlay (bl->section)
12550 && !section_is_mapped (bl->section))
12551 return 0;
12552
12553 return 1;
12554 }
12555
12556 static int
12557 dprintf_breakpoint_hit (const struct bp_location *bl,
12558 const address_space *aspace, CORE_ADDR bp_addr,
12559 const struct target_waitstatus *ws)
12560 {
12561 if (dprintf_style == dprintf_style_agent
12562 && target_can_run_breakpoint_commands ())
12563 {
12564 /* An agent-style dprintf never causes a stop. If we see a trap
12565 for this address it must be for a breakpoint that happens to
12566 be set at the same address. */
12567 return 0;
12568 }
12569
12570 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12571 }
12572
12573 static int
12574 bkpt_resources_needed (const struct bp_location *bl)
12575 {
12576 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12577
12578 return 1;
12579 }
12580
12581 static enum print_stop_action
12582 bkpt_print_it (bpstat bs)
12583 {
12584 struct breakpoint *b;
12585 const struct bp_location *bl;
12586 int bp_temp;
12587 struct ui_out *uiout = current_uiout;
12588
12589 gdb_assert (bs->bp_location_at != NULL);
12590
12591 bl = bs->bp_location_at;
12592 b = bs->breakpoint_at;
12593
12594 bp_temp = b->disposition == disp_del;
12595 if (bl->address != bl->requested_address)
12596 breakpoint_adjustment_warning (bl->requested_address,
12597 bl->address,
12598 b->number, 1);
12599 annotate_breakpoint (b->number);
12600 maybe_print_thread_hit_breakpoint (uiout);
12601
12602 if (uiout->is_mi_like_p ())
12603 {
12604 uiout->field_string ("reason",
12605 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12606 uiout->field_string ("disp", bpdisp_text (b->disposition));
12607 }
12608 if (bp_temp)
12609 uiout->message ("Temporary breakpoint %pF, ",
12610 signed_field ("bkptno", b->number));
12611 else
12612 uiout->message ("Breakpoint %pF, ",
12613 signed_field ("bkptno", b->number));
12614
12615 return PRINT_SRC_AND_LOC;
12616 }
12617
12618 static void
12619 bkpt_print_mention (struct breakpoint *b)
12620 {
12621 if (current_uiout->is_mi_like_p ())
12622 return;
12623
12624 switch (b->type)
12625 {
12626 case bp_breakpoint:
12627 case bp_gnu_ifunc_resolver:
12628 if (b->disposition == disp_del)
12629 printf_filtered (_("Temporary breakpoint"));
12630 else
12631 printf_filtered (_("Breakpoint"));
12632 printf_filtered (_(" %d"), b->number);
12633 if (b->type == bp_gnu_ifunc_resolver)
12634 printf_filtered (_(" at gnu-indirect-function resolver"));
12635 break;
12636 case bp_hardware_breakpoint:
12637 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12638 break;
12639 case bp_dprintf:
12640 printf_filtered (_("Dprintf %d"), b->number);
12641 break;
12642 }
12643
12644 say_where (b);
12645 }
12646
12647 static void
12648 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12649 {
12650 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12651 fprintf_unfiltered (fp, "tbreak");
12652 else if (tp->type == bp_breakpoint)
12653 fprintf_unfiltered (fp, "break");
12654 else if (tp->type == bp_hardware_breakpoint
12655 && tp->disposition == disp_del)
12656 fprintf_unfiltered (fp, "thbreak");
12657 else if (tp->type == bp_hardware_breakpoint)
12658 fprintf_unfiltered (fp, "hbreak");
12659 else
12660 internal_error (__FILE__, __LINE__,
12661 _("unhandled breakpoint type %d"), (int) tp->type);
12662
12663 fprintf_unfiltered (fp, " %s",
12664 event_location_to_string (tp->location.get ()));
12665
12666 /* Print out extra_string if this breakpoint is pending. It might
12667 contain, for example, conditions that were set by the user. */
12668 if (tp->loc == NULL && tp->extra_string != NULL)
12669 fprintf_unfiltered (fp, " %s", tp->extra_string);
12670
12671 print_recreate_thread (tp, fp);
12672 }
12673
12674 static void
12675 bkpt_create_sals_from_location (struct event_location *location,
12676 struct linespec_result *canonical,
12677 enum bptype type_wanted)
12678 {
12679 create_sals_from_location_default (location, canonical, type_wanted);
12680 }
12681
12682 static void
12683 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12684 struct linespec_result *canonical,
12685 gdb::unique_xmalloc_ptr<char> cond_string,
12686 gdb::unique_xmalloc_ptr<char> extra_string,
12687 enum bptype type_wanted,
12688 enum bpdisp disposition,
12689 int thread,
12690 int task, int ignore_count,
12691 const struct breakpoint_ops *ops,
12692 int from_tty, int enabled,
12693 int internal, unsigned flags)
12694 {
12695 create_breakpoints_sal_default (gdbarch, canonical,
12696 std::move (cond_string),
12697 std::move (extra_string),
12698 type_wanted,
12699 disposition, thread, task,
12700 ignore_count, ops, from_tty,
12701 enabled, internal, flags);
12702 }
12703
12704 static std::vector<symtab_and_line>
12705 bkpt_decode_location (struct breakpoint *b,
12706 struct event_location *location,
12707 struct program_space *search_pspace)
12708 {
12709 return decode_location_default (b, location, search_pspace);
12710 }
12711
12712 /* Virtual table for internal breakpoints. */
12713
12714 static void
12715 internal_bkpt_re_set (struct breakpoint *b)
12716 {
12717 switch (b->type)
12718 {
12719 /* Delete overlay event and longjmp master breakpoints; they
12720 will be reset later by breakpoint_re_set. */
12721 case bp_overlay_event:
12722 case bp_longjmp_master:
12723 case bp_std_terminate_master:
12724 case bp_exception_master:
12725 delete_breakpoint (b);
12726 break;
12727
12728 /* This breakpoint is special, it's set up when the inferior
12729 starts and we really don't want to touch it. */
12730 case bp_shlib_event:
12731
12732 /* Like bp_shlib_event, this breakpoint type is special. Once
12733 it is set up, we do not want to touch it. */
12734 case bp_thread_event:
12735 break;
12736 }
12737 }
12738
12739 static void
12740 internal_bkpt_check_status (bpstat bs)
12741 {
12742 if (bs->breakpoint_at->type == bp_shlib_event)
12743 {
12744 /* If requested, stop when the dynamic linker notifies GDB of
12745 events. This allows the user to get control and place
12746 breakpoints in initializer routines for dynamically loaded
12747 objects (among other things). */
12748 bs->stop = stop_on_solib_events;
12749 bs->print = stop_on_solib_events;
12750 }
12751 else
12752 bs->stop = 0;
12753 }
12754
12755 static enum print_stop_action
12756 internal_bkpt_print_it (bpstat bs)
12757 {
12758 struct breakpoint *b;
12759
12760 b = bs->breakpoint_at;
12761
12762 switch (b->type)
12763 {
12764 case bp_shlib_event:
12765 /* Did we stop because the user set the stop_on_solib_events
12766 variable? (If so, we report this as a generic, "Stopped due
12767 to shlib event" message.) */
12768 print_solib_event (0);
12769 break;
12770
12771 case bp_thread_event:
12772 /* Not sure how we will get here.
12773 GDB should not stop for these breakpoints. */
12774 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12775 break;
12776
12777 case bp_overlay_event:
12778 /* By analogy with the thread event, GDB should not stop for these. */
12779 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12780 break;
12781
12782 case bp_longjmp_master:
12783 /* These should never be enabled. */
12784 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12785 break;
12786
12787 case bp_std_terminate_master:
12788 /* These should never be enabled. */
12789 printf_filtered (_("std::terminate Master Breakpoint: "
12790 "gdb should not stop!\n"));
12791 break;
12792
12793 case bp_exception_master:
12794 /* These should never be enabled. */
12795 printf_filtered (_("Exception Master Breakpoint: "
12796 "gdb should not stop!\n"));
12797 break;
12798 }
12799
12800 return PRINT_NOTHING;
12801 }
12802
12803 static void
12804 internal_bkpt_print_mention (struct breakpoint *b)
12805 {
12806 /* Nothing to mention. These breakpoints are internal. */
12807 }
12808
12809 /* Virtual table for momentary breakpoints */
12810
12811 static void
12812 momentary_bkpt_re_set (struct breakpoint *b)
12813 {
12814 /* Keep temporary breakpoints, which can be encountered when we step
12815 over a dlopen call and solib_add is resetting the breakpoints.
12816 Otherwise these should have been blown away via the cleanup chain
12817 or by breakpoint_init_inferior when we rerun the executable. */
12818 }
12819
12820 static void
12821 momentary_bkpt_check_status (bpstat bs)
12822 {
12823 /* Nothing. The point of these breakpoints is causing a stop. */
12824 }
12825
12826 static enum print_stop_action
12827 momentary_bkpt_print_it (bpstat bs)
12828 {
12829 return PRINT_UNKNOWN;
12830 }
12831
12832 static void
12833 momentary_bkpt_print_mention (struct breakpoint *b)
12834 {
12835 /* Nothing to mention. These breakpoints are internal. */
12836 }
12837
12838 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12839
12840 It gets cleared already on the removal of the first one of such placed
12841 breakpoints. This is OK as they get all removed altogether. */
12842
12843 longjmp_breakpoint::~longjmp_breakpoint ()
12844 {
12845 thread_info *tp = find_thread_global_id (this->thread);
12846
12847 if (tp != NULL)
12848 tp->initiating_frame = null_frame_id;
12849 }
12850
12851 /* Specific methods for probe breakpoints. */
12852
12853 static int
12854 bkpt_probe_insert_location (struct bp_location *bl)
12855 {
12856 int v = bkpt_insert_location (bl);
12857
12858 if (v == 0)
12859 {
12860 /* The insertion was successful, now let's set the probe's semaphore
12861 if needed. */
12862 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
12863 }
12864
12865 return v;
12866 }
12867
12868 static int
12869 bkpt_probe_remove_location (struct bp_location *bl,
12870 enum remove_bp_reason reason)
12871 {
12872 /* Let's clear the semaphore before removing the location. */
12873 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
12874
12875 return bkpt_remove_location (bl, reason);
12876 }
12877
12878 static void
12879 bkpt_probe_create_sals_from_location (struct event_location *location,
12880 struct linespec_result *canonical,
12881 enum bptype type_wanted)
12882 {
12883 struct linespec_sals lsal;
12884
12885 lsal.sals = parse_probes (location, NULL, canonical);
12886 lsal.canonical
12887 = xstrdup (event_location_to_string (canonical->location.get ()));
12888 canonical->lsals.push_back (std::move (lsal));
12889 }
12890
12891 static std::vector<symtab_and_line>
12892 bkpt_probe_decode_location (struct breakpoint *b,
12893 struct event_location *location,
12894 struct program_space *search_pspace)
12895 {
12896 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12897 if (sals.empty ())
12898 error (_("probe not found"));
12899 return sals;
12900 }
12901
12902 /* The breakpoint_ops structure to be used in tracepoints. */
12903
12904 static void
12905 tracepoint_re_set (struct breakpoint *b)
12906 {
12907 breakpoint_re_set_default (b);
12908 }
12909
12910 static int
12911 tracepoint_breakpoint_hit (const struct bp_location *bl,
12912 const address_space *aspace, CORE_ADDR bp_addr,
12913 const struct target_waitstatus *ws)
12914 {
12915 /* By definition, the inferior does not report stops at
12916 tracepoints. */
12917 return 0;
12918 }
12919
12920 static void
12921 tracepoint_print_one_detail (const struct breakpoint *self,
12922 struct ui_out *uiout)
12923 {
12924 struct tracepoint *tp = (struct tracepoint *) self;
12925 if (!tp->static_trace_marker_id.empty ())
12926 {
12927 gdb_assert (self->type == bp_static_tracepoint);
12928
12929 uiout->message ("\tmarker id is %pF\n",
12930 string_field ("static-tracepoint-marker-string-id",
12931 tp->static_trace_marker_id.c_str ()));
12932 }
12933 }
12934
12935 static void
12936 tracepoint_print_mention (struct breakpoint *b)
12937 {
12938 if (current_uiout->is_mi_like_p ())
12939 return;
12940
12941 switch (b->type)
12942 {
12943 case bp_tracepoint:
12944 printf_filtered (_("Tracepoint"));
12945 printf_filtered (_(" %d"), b->number);
12946 break;
12947 case bp_fast_tracepoint:
12948 printf_filtered (_("Fast tracepoint"));
12949 printf_filtered (_(" %d"), b->number);
12950 break;
12951 case bp_static_tracepoint:
12952 printf_filtered (_("Static tracepoint"));
12953 printf_filtered (_(" %d"), b->number);
12954 break;
12955 default:
12956 internal_error (__FILE__, __LINE__,
12957 _("unhandled tracepoint type %d"), (int) b->type);
12958 }
12959
12960 say_where (b);
12961 }
12962
12963 static void
12964 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
12965 {
12966 struct tracepoint *tp = (struct tracepoint *) self;
12967
12968 if (self->type == bp_fast_tracepoint)
12969 fprintf_unfiltered (fp, "ftrace");
12970 else if (self->type == bp_static_tracepoint)
12971 fprintf_unfiltered (fp, "strace");
12972 else if (self->type == bp_tracepoint)
12973 fprintf_unfiltered (fp, "trace");
12974 else
12975 internal_error (__FILE__, __LINE__,
12976 _("unhandled tracepoint type %d"), (int) self->type);
12977
12978 fprintf_unfiltered (fp, " %s",
12979 event_location_to_string (self->location.get ()));
12980 print_recreate_thread (self, fp);
12981
12982 if (tp->pass_count)
12983 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
12984 }
12985
12986 static void
12987 tracepoint_create_sals_from_location (struct event_location *location,
12988 struct linespec_result *canonical,
12989 enum bptype type_wanted)
12990 {
12991 create_sals_from_location_default (location, canonical, type_wanted);
12992 }
12993
12994 static void
12995 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12996 struct linespec_result *canonical,
12997 gdb::unique_xmalloc_ptr<char> cond_string,
12998 gdb::unique_xmalloc_ptr<char> extra_string,
12999 enum bptype type_wanted,
13000 enum bpdisp disposition,
13001 int thread,
13002 int task, int ignore_count,
13003 const struct breakpoint_ops *ops,
13004 int from_tty, int enabled,
13005 int internal, unsigned flags)
13006 {
13007 create_breakpoints_sal_default (gdbarch, canonical,
13008 std::move (cond_string),
13009 std::move (extra_string),
13010 type_wanted,
13011 disposition, thread, task,
13012 ignore_count, ops, from_tty,
13013 enabled, internal, flags);
13014 }
13015
13016 static std::vector<symtab_and_line>
13017 tracepoint_decode_location (struct breakpoint *b,
13018 struct event_location *location,
13019 struct program_space *search_pspace)
13020 {
13021 return decode_location_default (b, location, search_pspace);
13022 }
13023
13024 struct breakpoint_ops tracepoint_breakpoint_ops;
13025
13026 /* Virtual table for tracepoints on static probes. */
13027
13028 static void
13029 tracepoint_probe_create_sals_from_location
13030 (struct event_location *location,
13031 struct linespec_result *canonical,
13032 enum bptype type_wanted)
13033 {
13034 /* We use the same method for breakpoint on probes. */
13035 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13036 }
13037
13038 static std::vector<symtab_and_line>
13039 tracepoint_probe_decode_location (struct breakpoint *b,
13040 struct event_location *location,
13041 struct program_space *search_pspace)
13042 {
13043 /* We use the same method for breakpoint on probes. */
13044 return bkpt_probe_decode_location (b, location, search_pspace);
13045 }
13046
13047 /* Dprintf breakpoint_ops methods. */
13048
13049 static void
13050 dprintf_re_set (struct breakpoint *b)
13051 {
13052 breakpoint_re_set_default (b);
13053
13054 /* extra_string should never be non-NULL for dprintf. */
13055 gdb_assert (b->extra_string != NULL);
13056
13057 /* 1 - connect to target 1, that can run breakpoint commands.
13058 2 - create a dprintf, which resolves fine.
13059 3 - disconnect from target 1
13060 4 - connect to target 2, that can NOT run breakpoint commands.
13061
13062 After steps #3/#4, you'll want the dprintf command list to
13063 be updated, because target 1 and 2 may well return different
13064 answers for target_can_run_breakpoint_commands().
13065 Given absence of finer grained resetting, we get to do
13066 it all the time. */
13067 if (b->extra_string != NULL)
13068 update_dprintf_command_list (b);
13069 }
13070
13071 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13072
13073 static void
13074 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13075 {
13076 fprintf_unfiltered (fp, "dprintf %s,%s",
13077 event_location_to_string (tp->location.get ()),
13078 tp->extra_string);
13079 print_recreate_thread (tp, fp);
13080 }
13081
13082 /* Implement the "after_condition_true" breakpoint_ops method for
13083 dprintf.
13084
13085 dprintf's are implemented with regular commands in their command
13086 list, but we run the commands here instead of before presenting the
13087 stop to the user, as dprintf's don't actually cause a stop. This
13088 also makes it so that the commands of multiple dprintfs at the same
13089 address are all handled. */
13090
13091 static void
13092 dprintf_after_condition_true (struct bpstats *bs)
13093 {
13094 struct bpstats tmp_bs;
13095 struct bpstats *tmp_bs_p = &tmp_bs;
13096
13097 /* dprintf's never cause a stop. This wasn't set in the
13098 check_status hook instead because that would make the dprintf's
13099 condition not be evaluated. */
13100 bs->stop = 0;
13101
13102 /* Run the command list here. Take ownership of it instead of
13103 copying. We never want these commands to run later in
13104 bpstat_do_actions, if a breakpoint that causes a stop happens to
13105 be set at same address as this dprintf, or even if running the
13106 commands here throws. */
13107 tmp_bs.commands = bs->commands;
13108 bs->commands = NULL;
13109
13110 bpstat_do_actions_1 (&tmp_bs_p);
13111
13112 /* 'tmp_bs.commands' will usually be NULL by now, but
13113 bpstat_do_actions_1 may return early without processing the whole
13114 list. */
13115 }
13116
13117 /* The breakpoint_ops structure to be used on static tracepoints with
13118 markers (`-m'). */
13119
13120 static void
13121 strace_marker_create_sals_from_location (struct event_location *location,
13122 struct linespec_result *canonical,
13123 enum bptype type_wanted)
13124 {
13125 struct linespec_sals lsal;
13126 const char *arg_start, *arg;
13127
13128 arg = arg_start = get_linespec_location (location)->spec_string;
13129 lsal.sals = decode_static_tracepoint_spec (&arg);
13130
13131 std::string str (arg_start, arg - arg_start);
13132 const char *ptr = str.c_str ();
13133 canonical->location
13134 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
13135
13136 lsal.canonical
13137 = xstrdup (event_location_to_string (canonical->location.get ()));
13138 canonical->lsals.push_back (std::move (lsal));
13139 }
13140
13141 static void
13142 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13143 struct linespec_result *canonical,
13144 gdb::unique_xmalloc_ptr<char> cond_string,
13145 gdb::unique_xmalloc_ptr<char> extra_string,
13146 enum bptype type_wanted,
13147 enum bpdisp disposition,
13148 int thread,
13149 int task, int ignore_count,
13150 const struct breakpoint_ops *ops,
13151 int from_tty, int enabled,
13152 int internal, unsigned flags)
13153 {
13154 const linespec_sals &lsal = canonical->lsals[0];
13155
13156 /* If the user is creating a static tracepoint by marker id
13157 (strace -m MARKER_ID), then store the sals index, so that
13158 breakpoint_re_set can try to match up which of the newly
13159 found markers corresponds to this one, and, don't try to
13160 expand multiple locations for each sal, given than SALS
13161 already should contain all sals for MARKER_ID. */
13162
13163 for (size_t i = 0; i < lsal.sals.size (); i++)
13164 {
13165 event_location_up location
13166 = copy_event_location (canonical->location.get ());
13167
13168 std::unique_ptr<tracepoint> tp (new tracepoint ());
13169 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
13170 std::move (location), NULL,
13171 std::move (cond_string),
13172 std::move (extra_string),
13173 type_wanted, disposition,
13174 thread, task, ignore_count, ops,
13175 from_tty, enabled, internal, flags,
13176 canonical->special_display);
13177 /* Given that its possible to have multiple markers with
13178 the same string id, if the user is creating a static
13179 tracepoint by marker id ("strace -m MARKER_ID"), then
13180 store the sals index, so that breakpoint_re_set can
13181 try to match up which of the newly found markers
13182 corresponds to this one */
13183 tp->static_trace_marker_id_idx = i;
13184
13185 install_breakpoint (internal, std::move (tp), 0);
13186 }
13187 }
13188
13189 static std::vector<symtab_and_line>
13190 strace_marker_decode_location (struct breakpoint *b,
13191 struct event_location *location,
13192 struct program_space *search_pspace)
13193 {
13194 struct tracepoint *tp = (struct tracepoint *) b;
13195 const char *s = get_linespec_location (location)->spec_string;
13196
13197 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13198 if (sals.size () > tp->static_trace_marker_id_idx)
13199 {
13200 sals[0] = sals[tp->static_trace_marker_id_idx];
13201 sals.resize (1);
13202 return sals;
13203 }
13204 else
13205 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
13206 }
13207
13208 static struct breakpoint_ops strace_marker_breakpoint_ops;
13209
13210 static int
13211 strace_marker_p (struct breakpoint *b)
13212 {
13213 return b->ops == &strace_marker_breakpoint_ops;
13214 }
13215
13216 /* Delete a breakpoint and clean up all traces of it in the data
13217 structures. */
13218
13219 void
13220 delete_breakpoint (struct breakpoint *bpt)
13221 {
13222 struct breakpoint *b;
13223
13224 gdb_assert (bpt != NULL);
13225
13226 /* Has this bp already been deleted? This can happen because
13227 multiple lists can hold pointers to bp's. bpstat lists are
13228 especial culprits.
13229
13230 One example of this happening is a watchpoint's scope bp. When
13231 the scope bp triggers, we notice that the watchpoint is out of
13232 scope, and delete it. We also delete its scope bp. But the
13233 scope bp is marked "auto-deleting", and is already on a bpstat.
13234 That bpstat is then checked for auto-deleting bp's, which are
13235 deleted.
13236
13237 A real solution to this problem might involve reference counts in
13238 bp's, and/or giving them pointers back to their referencing
13239 bpstat's, and teaching delete_breakpoint to only free a bp's
13240 storage when no more references were extent. A cheaper bandaid
13241 was chosen. */
13242 if (bpt->type == bp_none)
13243 return;
13244
13245 /* At least avoid this stale reference until the reference counting
13246 of breakpoints gets resolved. */
13247 if (bpt->related_breakpoint != bpt)
13248 {
13249 struct breakpoint *related;
13250 struct watchpoint *w;
13251
13252 if (bpt->type == bp_watchpoint_scope)
13253 w = (struct watchpoint *) bpt->related_breakpoint;
13254 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13255 w = (struct watchpoint *) bpt;
13256 else
13257 w = NULL;
13258 if (w != NULL)
13259 watchpoint_del_at_next_stop (w);
13260
13261 /* Unlink bpt from the bpt->related_breakpoint ring. */
13262 for (related = bpt; related->related_breakpoint != bpt;
13263 related = related->related_breakpoint);
13264 related->related_breakpoint = bpt->related_breakpoint;
13265 bpt->related_breakpoint = bpt;
13266 }
13267
13268 /* watch_command_1 creates a watchpoint but only sets its number if
13269 update_watchpoint succeeds in creating its bp_locations. If there's
13270 a problem in that process, we'll be asked to delete the half-created
13271 watchpoint. In that case, don't announce the deletion. */
13272 if (bpt->number)
13273 gdb::observers::breakpoint_deleted.notify (bpt);
13274
13275 if (breakpoint_chain == bpt)
13276 breakpoint_chain = bpt->next;
13277
13278 ALL_BREAKPOINTS (b)
13279 if (b->next == bpt)
13280 {
13281 b->next = bpt->next;
13282 break;
13283 }
13284
13285 /* Be sure no bpstat's are pointing at the breakpoint after it's
13286 been freed. */
13287 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13288 in all threads for now. Note that we cannot just remove bpstats
13289 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13290 commands are associated with the bpstat; if we remove it here,
13291 then the later call to bpstat_do_actions (&stop_bpstat); in
13292 event-top.c won't do anything, and temporary breakpoints with
13293 commands won't work. */
13294
13295 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13296
13297 /* Now that breakpoint is removed from breakpoint list, update the
13298 global location list. This will remove locations that used to
13299 belong to this breakpoint. Do this before freeing the breakpoint
13300 itself, since remove_breakpoint looks at location's owner. It
13301 might be better design to have location completely
13302 self-contained, but it's not the case now. */
13303 update_global_location_list (UGLL_DONT_INSERT);
13304
13305 /* On the chance that someone will soon try again to delete this
13306 same bp, we mark it as deleted before freeing its storage. */
13307 bpt->type = bp_none;
13308 delete bpt;
13309 }
13310
13311 /* Iterator function to call a user-provided callback function once
13312 for each of B and its related breakpoints. */
13313
13314 static void
13315 iterate_over_related_breakpoints (struct breakpoint *b,
13316 gdb::function_view<void (breakpoint *)> function)
13317 {
13318 struct breakpoint *related;
13319
13320 related = b;
13321 do
13322 {
13323 struct breakpoint *next;
13324
13325 /* FUNCTION may delete RELATED. */
13326 next = related->related_breakpoint;
13327
13328 if (next == related)
13329 {
13330 /* RELATED is the last ring entry. */
13331 function (related);
13332
13333 /* FUNCTION may have deleted it, so we'd never reach back to
13334 B. There's nothing left to do anyway, so just break
13335 out. */
13336 break;
13337 }
13338 else
13339 function (related);
13340
13341 related = next;
13342 }
13343 while (related != b);
13344 }
13345
13346 static void
13347 delete_command (const char *arg, int from_tty)
13348 {
13349 struct breakpoint *b, *b_tmp;
13350
13351 dont_repeat ();
13352
13353 if (arg == 0)
13354 {
13355 int breaks_to_delete = 0;
13356
13357 /* Delete all breakpoints if no argument. Do not delete
13358 internal breakpoints, these have to be deleted with an
13359 explicit breakpoint number argument. */
13360 ALL_BREAKPOINTS (b)
13361 if (user_breakpoint_p (b))
13362 {
13363 breaks_to_delete = 1;
13364 break;
13365 }
13366
13367 /* Ask user only if there are some breakpoints to delete. */
13368 if (!from_tty
13369 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13370 {
13371 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13372 if (user_breakpoint_p (b))
13373 delete_breakpoint (b);
13374 }
13375 }
13376 else
13377 map_breakpoint_numbers
13378 (arg, [&] (breakpoint *br)
13379 {
13380 iterate_over_related_breakpoints (br, delete_breakpoint);
13381 });
13382 }
13383
13384 /* Return true if all locations of B bound to PSPACE are pending. If
13385 PSPACE is NULL, all locations of all program spaces are
13386 considered. */
13387
13388 static int
13389 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13390 {
13391 struct bp_location *loc;
13392
13393 for (loc = b->loc; loc != NULL; loc = loc->next)
13394 if ((pspace == NULL
13395 || loc->pspace == pspace)
13396 && !loc->shlib_disabled
13397 && !loc->pspace->executing_startup)
13398 return 0;
13399 return 1;
13400 }
13401
13402 /* Subroutine of update_breakpoint_locations to simplify it.
13403 Return non-zero if multiple fns in list LOC have the same name.
13404 Null names are ignored. */
13405
13406 static int
13407 ambiguous_names_p (struct bp_location *loc)
13408 {
13409 struct bp_location *l;
13410 htab_up htab (htab_create_alloc (13, htab_hash_string, streq_hash, NULL,
13411 xcalloc, xfree));
13412
13413 for (l = loc; l != NULL; l = l->next)
13414 {
13415 const char **slot;
13416 const char *name = l->function_name;
13417
13418 /* Allow for some names to be NULL, ignore them. */
13419 if (name == NULL)
13420 continue;
13421
13422 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
13423 INSERT);
13424 /* NOTE: We can assume slot != NULL here because xcalloc never
13425 returns NULL. */
13426 if (*slot != NULL)
13427 return 1;
13428 *slot = name;
13429 }
13430
13431 return 0;
13432 }
13433
13434 /* When symbols change, it probably means the sources changed as well,
13435 and it might mean the static tracepoint markers are no longer at
13436 the same address or line numbers they used to be at last we
13437 checked. Losing your static tracepoints whenever you rebuild is
13438 undesirable. This function tries to resync/rematch gdb static
13439 tracepoints with the markers on the target, for static tracepoints
13440 that have not been set by marker id. Static tracepoint that have
13441 been set by marker id are reset by marker id in breakpoint_re_set.
13442 The heuristic is:
13443
13444 1) For a tracepoint set at a specific address, look for a marker at
13445 the old PC. If one is found there, assume to be the same marker.
13446 If the name / string id of the marker found is different from the
13447 previous known name, assume that means the user renamed the marker
13448 in the sources, and output a warning.
13449
13450 2) For a tracepoint set at a given line number, look for a marker
13451 at the new address of the old line number. If one is found there,
13452 assume to be the same marker. If the name / string id of the
13453 marker found is different from the previous known name, assume that
13454 means the user renamed the marker in the sources, and output a
13455 warning.
13456
13457 3) If a marker is no longer found at the same address or line, it
13458 may mean the marker no longer exists. But it may also just mean
13459 the code changed a bit. Maybe the user added a few lines of code
13460 that made the marker move up or down (in line number terms). Ask
13461 the target for info about the marker with the string id as we knew
13462 it. If found, update line number and address in the matching
13463 static tracepoint. This will get confused if there's more than one
13464 marker with the same ID (possible in UST, although unadvised
13465 precisely because it confuses tools). */
13466
13467 static struct symtab_and_line
13468 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13469 {
13470 struct tracepoint *tp = (struct tracepoint *) b;
13471 struct static_tracepoint_marker marker;
13472 CORE_ADDR pc;
13473
13474 pc = sal.pc;
13475 if (sal.line)
13476 find_line_pc (sal.symtab, sal.line, &pc);
13477
13478 if (target_static_tracepoint_marker_at (pc, &marker))
13479 {
13480 if (tp->static_trace_marker_id != marker.str_id)
13481 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13482 b->number, tp->static_trace_marker_id.c_str (),
13483 marker.str_id.c_str ());
13484
13485 tp->static_trace_marker_id = std::move (marker.str_id);
13486
13487 return sal;
13488 }
13489
13490 /* Old marker wasn't found on target at lineno. Try looking it up
13491 by string ID. */
13492 if (!sal.explicit_pc
13493 && sal.line != 0
13494 && sal.symtab != NULL
13495 && !tp->static_trace_marker_id.empty ())
13496 {
13497 std::vector<static_tracepoint_marker> markers
13498 = target_static_tracepoint_markers_by_strid
13499 (tp->static_trace_marker_id.c_str ());
13500
13501 if (!markers.empty ())
13502 {
13503 struct symbol *sym;
13504 struct static_tracepoint_marker *tpmarker;
13505 struct ui_out *uiout = current_uiout;
13506 struct explicit_location explicit_loc;
13507
13508 tpmarker = &markers[0];
13509
13510 tp->static_trace_marker_id = std::move (tpmarker->str_id);
13511
13512 warning (_("marker for static tracepoint %d (%s) not "
13513 "found at previous line number"),
13514 b->number, tp->static_trace_marker_id.c_str ());
13515
13516 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
13517 sym = find_pc_sect_function (tpmarker->address, NULL);
13518 uiout->text ("Now in ");
13519 if (sym)
13520 {
13521 uiout->field_string ("func", sym->print_name (),
13522 function_name_style.style ());
13523 uiout->text (" at ");
13524 }
13525 uiout->field_string ("file",
13526 symtab_to_filename_for_display (sal2.symtab),
13527 file_name_style.style ());
13528 uiout->text (":");
13529
13530 if (uiout->is_mi_like_p ())
13531 {
13532 const char *fullname = symtab_to_fullname (sal2.symtab);
13533
13534 uiout->field_string ("fullname", fullname);
13535 }
13536
13537 uiout->field_signed ("line", sal2.line);
13538 uiout->text ("\n");
13539
13540 b->loc->line_number = sal2.line;
13541 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13542
13543 b->location.reset (NULL);
13544 initialize_explicit_location (&explicit_loc);
13545 explicit_loc.source_filename
13546 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13547 explicit_loc.line_offset.offset = b->loc->line_number;
13548 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13549 b->location = new_explicit_location (&explicit_loc);
13550
13551 /* Might be nice to check if function changed, and warn if
13552 so. */
13553 }
13554 }
13555 return sal;
13556 }
13557
13558 /* Returns 1 iff locations A and B are sufficiently same that
13559 we don't need to report breakpoint as changed. */
13560
13561 static int
13562 locations_are_equal (struct bp_location *a, struct bp_location *b)
13563 {
13564 while (a && b)
13565 {
13566 if (a->address != b->address)
13567 return 0;
13568
13569 if (a->shlib_disabled != b->shlib_disabled)
13570 return 0;
13571
13572 if (a->enabled != b->enabled)
13573 return 0;
13574
13575 if (a->disabled_by_cond != b->disabled_by_cond)
13576 return 0;
13577
13578 a = a->next;
13579 b = b->next;
13580 }
13581
13582 if ((a == NULL) != (b == NULL))
13583 return 0;
13584
13585 return 1;
13586 }
13587
13588 /* Split all locations of B that are bound to PSPACE out of B's
13589 location list to a separate list and return that list's head. If
13590 PSPACE is NULL, hoist out all locations of B. */
13591
13592 static struct bp_location *
13593 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13594 {
13595 struct bp_location head;
13596 struct bp_location *i = b->loc;
13597 struct bp_location **i_link = &b->loc;
13598 struct bp_location *hoisted = &head;
13599
13600 if (pspace == NULL)
13601 {
13602 i = b->loc;
13603 b->loc = NULL;
13604 return i;
13605 }
13606
13607 head.next = NULL;
13608
13609 while (i != NULL)
13610 {
13611 if (i->pspace == pspace)
13612 {
13613 *i_link = i->next;
13614 i->next = NULL;
13615 hoisted->next = i;
13616 hoisted = i;
13617 }
13618 else
13619 i_link = &i->next;
13620 i = *i_link;
13621 }
13622
13623 return head.next;
13624 }
13625
13626 /* Create new breakpoint locations for B (a hardware or software
13627 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13628 zero, then B is a ranged breakpoint. Only recreates locations for
13629 FILTER_PSPACE. Locations of other program spaces are left
13630 untouched. */
13631
13632 void
13633 update_breakpoint_locations (struct breakpoint *b,
13634 struct program_space *filter_pspace,
13635 gdb::array_view<const symtab_and_line> sals,
13636 gdb::array_view<const symtab_and_line> sals_end)
13637 {
13638 struct bp_location *existing_locations;
13639
13640 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
13641 {
13642 /* Ranged breakpoints have only one start location and one end
13643 location. */
13644 b->enable_state = bp_disabled;
13645 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13646 "multiple locations found\n"),
13647 b->number);
13648 return;
13649 }
13650
13651 /* If there's no new locations, and all existing locations are
13652 pending, don't do anything. This optimizes the common case where
13653 all locations are in the same shared library, that was unloaded.
13654 We'd like to retain the location, so that when the library is
13655 loaded again, we don't loose the enabled/disabled status of the
13656 individual locations. */
13657 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
13658 return;
13659
13660 existing_locations = hoist_existing_locations (b, filter_pspace);
13661
13662 for (const auto &sal : sals)
13663 {
13664 struct bp_location *new_loc;
13665
13666 switch_to_program_space_and_thread (sal.pspace);
13667
13668 new_loc = add_location_to_breakpoint (b, &sal);
13669
13670 /* Reparse conditions, they might contain references to the
13671 old symtab. */
13672 if (b->cond_string != NULL)
13673 {
13674 const char *s;
13675
13676 s = b->cond_string;
13677 try
13678 {
13679 new_loc->cond = parse_exp_1 (&s, sal.pc,
13680 block_for_pc (sal.pc),
13681 0);
13682 }
13683 catch (const gdb_exception_error &e)
13684 {
13685 new_loc->disabled_by_cond = true;
13686 }
13687 }
13688
13689 if (!sals_end.empty ())
13690 {
13691 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13692
13693 new_loc->length = end - sals[0].pc + 1;
13694 }
13695 }
13696
13697 /* If possible, carry over 'disable' status from existing
13698 breakpoints. */
13699 {
13700 struct bp_location *e = existing_locations;
13701 /* If there are multiple breakpoints with the same function name,
13702 e.g. for inline functions, comparing function names won't work.
13703 Instead compare pc addresses; this is just a heuristic as things
13704 may have moved, but in practice it gives the correct answer
13705 often enough until a better solution is found. */
13706 int have_ambiguous_names = ambiguous_names_p (b->loc);
13707
13708 for (; e; e = e->next)
13709 {
13710 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
13711 {
13712 struct bp_location *l = b->loc;
13713 if (have_ambiguous_names)
13714 {
13715 for (; l; l = l->next)
13716 {
13717 /* Ignore software vs hardware location type at
13718 this point, because with "set breakpoint
13719 auto-hw", after a re-set, locations that were
13720 hardware can end up as software, or vice versa.
13721 As mentioned above, this is an heuristic and in
13722 practice should give the correct answer often
13723 enough. */
13724 if (breakpoint_locations_match (e, l, true))
13725 {
13726 l->enabled = e->enabled;
13727 l->disabled_by_cond = e->disabled_by_cond;
13728 break;
13729 }
13730 }
13731 }
13732 else
13733 {
13734 for (; l; l = l->next)
13735 if (l->function_name
13736 && strcmp (e->function_name, l->function_name) == 0)
13737 {
13738 l->enabled = e->enabled;
13739 l->disabled_by_cond = e->disabled_by_cond;
13740 break;
13741 }
13742 }
13743 }
13744 }
13745 }
13746
13747 if (!locations_are_equal (existing_locations, b->loc))
13748 gdb::observers::breakpoint_modified.notify (b);
13749 }
13750
13751 /* Find the SaL locations corresponding to the given LOCATION.
13752 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13753
13754 static std::vector<symtab_and_line>
13755 location_to_sals (struct breakpoint *b, struct event_location *location,
13756 struct program_space *search_pspace, int *found)
13757 {
13758 struct gdb_exception exception;
13759
13760 gdb_assert (b->ops != NULL);
13761
13762 std::vector<symtab_and_line> sals;
13763
13764 try
13765 {
13766 sals = b->ops->decode_location (b, location, search_pspace);
13767 }
13768 catch (gdb_exception_error &e)
13769 {
13770 int not_found_and_ok = 0;
13771
13772 /* For pending breakpoints, it's expected that parsing will
13773 fail until the right shared library is loaded. User has
13774 already told to create pending breakpoints and don't need
13775 extra messages. If breakpoint is in bp_shlib_disabled
13776 state, then user already saw the message about that
13777 breakpoint being disabled, and don't want to see more
13778 errors. */
13779 if (e.error == NOT_FOUND_ERROR
13780 && (b->condition_not_parsed
13781 || (b->loc != NULL
13782 && search_pspace != NULL
13783 && b->loc->pspace != search_pspace)
13784 || (b->loc && b->loc->shlib_disabled)
13785 || (b->loc && b->loc->pspace->executing_startup)
13786 || b->enable_state == bp_disabled))
13787 not_found_and_ok = 1;
13788
13789 if (!not_found_and_ok)
13790 {
13791 /* We surely don't want to warn about the same breakpoint
13792 10 times. One solution, implemented here, is disable
13793 the breakpoint on error. Another solution would be to
13794 have separate 'warning emitted' flag. Since this
13795 happens only when a binary has changed, I don't know
13796 which approach is better. */
13797 b->enable_state = bp_disabled;
13798 throw;
13799 }
13800
13801 exception = std::move (e);
13802 }
13803
13804 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13805 {
13806 for (auto &sal : sals)
13807 resolve_sal_pc (&sal);
13808 if (b->condition_not_parsed && b->extra_string != NULL)
13809 {
13810 char *cond_string, *extra_string;
13811 int thread, task;
13812
13813 find_condition_and_thread_for_sals (sals, b->extra_string,
13814 &cond_string, &thread,
13815 &task, &extra_string);
13816 gdb_assert (b->cond_string == NULL);
13817 if (cond_string)
13818 b->cond_string = cond_string;
13819 b->thread = thread;
13820 b->task = task;
13821 if (extra_string)
13822 {
13823 xfree (b->extra_string);
13824 b->extra_string = extra_string;
13825 }
13826 b->condition_not_parsed = 0;
13827 }
13828
13829 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13830 sals[0] = update_static_tracepoint (b, sals[0]);
13831
13832 *found = 1;
13833 }
13834 else
13835 *found = 0;
13836
13837 return sals;
13838 }
13839
13840 /* The default re_set method, for typical hardware or software
13841 breakpoints. Reevaluate the breakpoint and recreate its
13842 locations. */
13843
13844 static void
13845 breakpoint_re_set_default (struct breakpoint *b)
13846 {
13847 struct program_space *filter_pspace = current_program_space;
13848 std::vector<symtab_and_line> expanded, expanded_end;
13849
13850 int found;
13851 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13852 filter_pspace, &found);
13853 if (found)
13854 expanded = std::move (sals);
13855
13856 if (b->location_range_end != NULL)
13857 {
13858 std::vector<symtab_and_line> sals_end
13859 = location_to_sals (b, b->location_range_end.get (),
13860 filter_pspace, &found);
13861 if (found)
13862 expanded_end = std::move (sals_end);
13863 }
13864
13865 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
13866 }
13867
13868 /* Default method for creating SALs from an address string. It basically
13869 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13870
13871 static void
13872 create_sals_from_location_default (struct event_location *location,
13873 struct linespec_result *canonical,
13874 enum bptype type_wanted)
13875 {
13876 parse_breakpoint_sals (location, canonical);
13877 }
13878
13879 /* Call create_breakpoints_sal for the given arguments. This is the default
13880 function for the `create_breakpoints_sal' method of
13881 breakpoint_ops. */
13882
13883 static void
13884 create_breakpoints_sal_default (struct gdbarch *gdbarch,
13885 struct linespec_result *canonical,
13886 gdb::unique_xmalloc_ptr<char> cond_string,
13887 gdb::unique_xmalloc_ptr<char> extra_string,
13888 enum bptype type_wanted,
13889 enum bpdisp disposition,
13890 int thread,
13891 int task, int ignore_count,
13892 const struct breakpoint_ops *ops,
13893 int from_tty, int enabled,
13894 int internal, unsigned flags)
13895 {
13896 create_breakpoints_sal (gdbarch, canonical,
13897 std::move (cond_string),
13898 std::move (extra_string),
13899 type_wanted, disposition,
13900 thread, task, ignore_count, ops, from_tty,
13901 enabled, internal, flags);
13902 }
13903
13904 /* Decode the line represented by S by calling decode_line_full. This is the
13905 default function for the `decode_location' method of breakpoint_ops. */
13906
13907 static std::vector<symtab_and_line>
13908 decode_location_default (struct breakpoint *b,
13909 struct event_location *location,
13910 struct program_space *search_pspace)
13911 {
13912 struct linespec_result canonical;
13913
13914 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
13915 NULL, 0, &canonical, multiple_symbols_all,
13916 b->filter.get ());
13917
13918 /* We should get 0 or 1 resulting SALs. */
13919 gdb_assert (canonical.lsals.size () < 2);
13920
13921 if (!canonical.lsals.empty ())
13922 {
13923 const linespec_sals &lsal = canonical.lsals[0];
13924 return std::move (lsal.sals);
13925 }
13926 return {};
13927 }
13928
13929 /* Reset a breakpoint. */
13930
13931 static void
13932 breakpoint_re_set_one (breakpoint *b)
13933 {
13934 input_radix = b->input_radix;
13935 set_language (b->language);
13936
13937 b->ops->re_set (b);
13938 }
13939
13940 /* Re-set breakpoint locations for the current program space.
13941 Locations bound to other program spaces are left untouched. */
13942
13943 void
13944 breakpoint_re_set (void)
13945 {
13946 struct breakpoint *b, *b_tmp;
13947
13948 {
13949 scoped_restore_current_language save_language;
13950 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
13951 scoped_restore_current_pspace_and_thread restore_pspace_thread;
13952
13953 /* breakpoint_re_set_one sets the current_language to the language
13954 of the breakpoint it is resetting (see prepare_re_set_context)
13955 before re-evaluating the breakpoint's location. This change can
13956 unfortunately get undone by accident if the language_mode is set
13957 to auto, and we either switch frames, or more likely in this context,
13958 we select the current frame.
13959
13960 We prevent this by temporarily turning the language_mode to
13961 language_mode_manual. We restore it once all breakpoints
13962 have been reset. */
13963 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13964 language_mode = language_mode_manual;
13965
13966 /* Note: we must not try to insert locations until after all
13967 breakpoints have been re-set. Otherwise, e.g., when re-setting
13968 breakpoint 1, we'd insert the locations of breakpoint 2, which
13969 hadn't been re-set yet, and thus may have stale locations. */
13970
13971 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13972 {
13973 try
13974 {
13975 breakpoint_re_set_one (b);
13976 }
13977 catch (const gdb_exception &ex)
13978 {
13979 exception_fprintf (gdb_stderr, ex,
13980 "Error in re-setting breakpoint %d: ",
13981 b->number);
13982 }
13983 }
13984
13985 jit_breakpoint_re_set ();
13986 }
13987
13988 create_overlay_event_breakpoint ();
13989 create_longjmp_master_breakpoint ();
13990 create_std_terminate_master_breakpoint ();
13991 create_exception_master_breakpoint ();
13992
13993 /* Now we can insert. */
13994 update_global_location_list (UGLL_MAY_INSERT);
13995 }
13996 \f
13997 /* Reset the thread number of this breakpoint:
13998
13999 - If the breakpoint is for all threads, leave it as-is.
14000 - Else, reset it to the current thread for inferior_ptid. */
14001 void
14002 breakpoint_re_set_thread (struct breakpoint *b)
14003 {
14004 if (b->thread != -1)
14005 {
14006 b->thread = inferior_thread ()->global_num;
14007
14008 /* We're being called after following a fork. The new fork is
14009 selected as current, and unless this was a vfork will have a
14010 different program space from the original thread. Reset that
14011 as well. */
14012 b->loc->pspace = current_program_space;
14013 }
14014 }
14015
14016 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14017 If from_tty is nonzero, it prints a message to that effect,
14018 which ends with a period (no newline). */
14019
14020 void
14021 set_ignore_count (int bptnum, int count, int from_tty)
14022 {
14023 struct breakpoint *b;
14024
14025 if (count < 0)
14026 count = 0;
14027
14028 ALL_BREAKPOINTS (b)
14029 if (b->number == bptnum)
14030 {
14031 if (is_tracepoint (b))
14032 {
14033 if (from_tty && count != 0)
14034 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14035 bptnum);
14036 return;
14037 }
14038
14039 b->ignore_count = count;
14040 if (from_tty)
14041 {
14042 if (count == 0)
14043 printf_filtered (_("Will stop next time "
14044 "breakpoint %d is reached."),
14045 bptnum);
14046 else if (count == 1)
14047 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14048 bptnum);
14049 else
14050 printf_filtered (_("Will ignore next %d "
14051 "crossings of breakpoint %d."),
14052 count, bptnum);
14053 }
14054 gdb::observers::breakpoint_modified.notify (b);
14055 return;
14056 }
14057
14058 error (_("No breakpoint number %d."), bptnum);
14059 }
14060
14061 /* Command to set ignore-count of breakpoint N to COUNT. */
14062
14063 static void
14064 ignore_command (const char *args, int from_tty)
14065 {
14066 const char *p = args;
14067 int num;
14068
14069 if (p == 0)
14070 error_no_arg (_("a breakpoint number"));
14071
14072 num = get_number (&p);
14073 if (num == 0)
14074 error (_("bad breakpoint number: '%s'"), args);
14075 if (*p == 0)
14076 error (_("Second argument (specified ignore-count) is missing."));
14077
14078 set_ignore_count (num,
14079 longest_to_int (value_as_long (parse_and_eval (p))),
14080 from_tty);
14081 if (from_tty)
14082 printf_filtered ("\n");
14083 }
14084 \f
14085
14086 /* Call FUNCTION on each of the breakpoints with numbers in the range
14087 defined by BP_NUM_RANGE (an inclusive range). */
14088
14089 static void
14090 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
14091 gdb::function_view<void (breakpoint *)> function)
14092 {
14093 if (bp_num_range.first == 0)
14094 {
14095 warning (_("bad breakpoint number at or near '%d'"),
14096 bp_num_range.first);
14097 }
14098 else
14099 {
14100 struct breakpoint *b, *tmp;
14101
14102 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
14103 {
14104 bool match = false;
14105
14106 ALL_BREAKPOINTS_SAFE (b, tmp)
14107 if (b->number == i)
14108 {
14109 match = true;
14110 function (b);
14111 break;
14112 }
14113 if (!match)
14114 printf_unfiltered (_("No breakpoint number %d.\n"), i);
14115 }
14116 }
14117 }
14118
14119 /* Call FUNCTION on each of the breakpoints whose numbers are given in
14120 ARGS. */
14121
14122 static void
14123 map_breakpoint_numbers (const char *args,
14124 gdb::function_view<void (breakpoint *)> function)
14125 {
14126 if (args == NULL || *args == '\0')
14127 error_no_arg (_("one or more breakpoint numbers"));
14128
14129 number_or_range_parser parser (args);
14130
14131 while (!parser.finished ())
14132 {
14133 int num = parser.get_number ();
14134 map_breakpoint_number_range (std::make_pair (num, num), function);
14135 }
14136 }
14137
14138 /* Return the breakpoint location structure corresponding to the
14139 BP_NUM and LOC_NUM values. */
14140
14141 static struct bp_location *
14142 find_location_by_number (int bp_num, int loc_num)
14143 {
14144 struct breakpoint *b;
14145
14146 ALL_BREAKPOINTS (b)
14147 if (b->number == bp_num)
14148 {
14149 break;
14150 }
14151
14152 if (!b || b->number != bp_num)
14153 error (_("Bad breakpoint number '%d'"), bp_num);
14154
14155 if (loc_num == 0)
14156 error (_("Bad breakpoint location number '%d'"), loc_num);
14157
14158 int n = 0;
14159 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
14160 if (++n == loc_num)
14161 return loc;
14162
14163 error (_("Bad breakpoint location number '%d'"), loc_num);
14164 }
14165
14166 /* Modes of operation for extract_bp_num. */
14167 enum class extract_bp_kind
14168 {
14169 /* Extracting a breakpoint number. */
14170 bp,
14171
14172 /* Extracting a location number. */
14173 loc,
14174 };
14175
14176 /* Extract a breakpoint or location number (as determined by KIND)
14177 from the string starting at START. TRAILER is a character which
14178 can be found after the number. If you don't want a trailer, use
14179 '\0'. If END_OUT is not NULL, it is set to point after the parsed
14180 string. This always returns a positive integer. */
14181
14182 static int
14183 extract_bp_num (extract_bp_kind kind, const char *start,
14184 int trailer, const char **end_out = NULL)
14185 {
14186 const char *end = start;
14187 int num = get_number_trailer (&end, trailer);
14188 if (num < 0)
14189 error (kind == extract_bp_kind::bp
14190 ? _("Negative breakpoint number '%.*s'")
14191 : _("Negative breakpoint location number '%.*s'"),
14192 int (end - start), start);
14193 if (num == 0)
14194 error (kind == extract_bp_kind::bp
14195 ? _("Bad breakpoint number '%.*s'")
14196 : _("Bad breakpoint location number '%.*s'"),
14197 int (end - start), start);
14198
14199 if (end_out != NULL)
14200 *end_out = end;
14201 return num;
14202 }
14203
14204 /* Extract a breakpoint or location range (as determined by KIND) in
14205 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14206 representing the (inclusive) range. The returned pair's elements
14207 are always positive integers. */
14208
14209 static std::pair<int, int>
14210 extract_bp_or_bp_range (extract_bp_kind kind,
14211 const std::string &arg,
14212 std::string::size_type arg_offset)
14213 {
14214 std::pair<int, int> range;
14215 const char *bp_loc = &arg[arg_offset];
14216 std::string::size_type dash = arg.find ('-', arg_offset);
14217 if (dash != std::string::npos)
14218 {
14219 /* bp_loc is a range (x-z). */
14220 if (arg.length () == dash + 1)
14221 error (kind == extract_bp_kind::bp
14222 ? _("Bad breakpoint number at or near: '%s'")
14223 : _("Bad breakpoint location number at or near: '%s'"),
14224 bp_loc);
14225
14226 const char *end;
14227 const char *start_first = bp_loc;
14228 const char *start_second = &arg[dash + 1];
14229 range.first = extract_bp_num (kind, start_first, '-');
14230 range.second = extract_bp_num (kind, start_second, '\0', &end);
14231
14232 if (range.first > range.second)
14233 error (kind == extract_bp_kind::bp
14234 ? _("Inverted breakpoint range at '%.*s'")
14235 : _("Inverted breakpoint location range at '%.*s'"),
14236 int (end - start_first), start_first);
14237 }
14238 else
14239 {
14240 /* bp_loc is a single value. */
14241 range.first = extract_bp_num (kind, bp_loc, '\0');
14242 range.second = range.first;
14243 }
14244 return range;
14245 }
14246
14247 /* Extract the breakpoint/location range specified by ARG. Returns
14248 the breakpoint range in BP_NUM_RANGE, and the location range in
14249 BP_LOC_RANGE.
14250
14251 ARG may be in any of the following forms:
14252
14253 x where 'x' is a breakpoint number.
14254 x-y where 'x' and 'y' specify a breakpoint numbers range.
14255 x.y where 'x' is a breakpoint number and 'y' a location number.
14256 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14257 location number range.
14258 */
14259
14260 static void
14261 extract_bp_number_and_location (const std::string &arg,
14262 std::pair<int, int> &bp_num_range,
14263 std::pair<int, int> &bp_loc_range)
14264 {
14265 std::string::size_type dot = arg.find ('.');
14266
14267 if (dot != std::string::npos)
14268 {
14269 /* Handle 'x.y' and 'x.y-z' cases. */
14270
14271 if (arg.length () == dot + 1 || dot == 0)
14272 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
14273
14274 bp_num_range.first
14275 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14276 bp_num_range.second = bp_num_range.first;
14277
14278 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14279 arg, dot + 1);
14280 }
14281 else
14282 {
14283 /* Handle x and x-y cases. */
14284
14285 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
14286 bp_loc_range.first = 0;
14287 bp_loc_range.second = 0;
14288 }
14289 }
14290
14291 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14292 specifies whether to enable or disable. */
14293
14294 static void
14295 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14296 {
14297 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14298 if (loc != NULL)
14299 {
14300 if (loc->disabled_by_cond && enable)
14301 error (_("Breakpoint %d's condition is invalid at location %d, "
14302 "cannot enable."), bp_num, loc_num);
14303
14304 if (loc->enabled != enable)
14305 {
14306 loc->enabled = enable;
14307 mark_breakpoint_location_modified (loc);
14308 }
14309 if (target_supports_enable_disable_tracepoint ()
14310 && current_trace_status ()->running && loc->owner
14311 && is_tracepoint (loc->owner))
14312 target_disable_tracepoint (loc);
14313 }
14314 update_global_location_list (UGLL_DONT_INSERT);
14315
14316 gdb::observers::breakpoint_modified.notify (loc->owner);
14317 }
14318
14319 /* Enable or disable a range of breakpoint locations. BP_NUM is the
14320 number of the breakpoint, and BP_LOC_RANGE specifies the
14321 (inclusive) range of location numbers of that breakpoint to
14322 enable/disable. ENABLE specifies whether to enable or disable the
14323 location. */
14324
14325 static void
14326 enable_disable_breakpoint_location_range (int bp_num,
14327 std::pair<int, int> &bp_loc_range,
14328 bool enable)
14329 {
14330 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14331 enable_disable_bp_num_loc (bp_num, i, enable);
14332 }
14333
14334 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14335 If from_tty is nonzero, it prints a message to that effect,
14336 which ends with a period (no newline). */
14337
14338 void
14339 disable_breakpoint (struct breakpoint *bpt)
14340 {
14341 /* Never disable a watchpoint scope breakpoint; we want to
14342 hit them when we leave scope so we can delete both the
14343 watchpoint and its scope breakpoint at that time. */
14344 if (bpt->type == bp_watchpoint_scope)
14345 return;
14346
14347 bpt->enable_state = bp_disabled;
14348
14349 /* Mark breakpoint locations modified. */
14350 mark_breakpoint_modified (bpt);
14351
14352 if (target_supports_enable_disable_tracepoint ()
14353 && current_trace_status ()->running && is_tracepoint (bpt))
14354 {
14355 struct bp_location *location;
14356
14357 for (location = bpt->loc; location; location = location->next)
14358 target_disable_tracepoint (location);
14359 }
14360
14361 update_global_location_list (UGLL_DONT_INSERT);
14362
14363 gdb::observers::breakpoint_modified.notify (bpt);
14364 }
14365
14366 /* Enable or disable the breakpoint(s) or breakpoint location(s)
14367 specified in ARGS. ARGS may be in any of the formats handled by
14368 extract_bp_number_and_location. ENABLE specifies whether to enable
14369 or disable the breakpoints/locations. */
14370
14371 static void
14372 enable_disable_command (const char *args, int from_tty, bool enable)
14373 {
14374 if (args == 0)
14375 {
14376 struct breakpoint *bpt;
14377
14378 ALL_BREAKPOINTS (bpt)
14379 if (user_breakpoint_p (bpt))
14380 {
14381 if (enable)
14382 enable_breakpoint (bpt);
14383 else
14384 disable_breakpoint (bpt);
14385 }
14386 }
14387 else
14388 {
14389 std::string num = extract_arg (&args);
14390
14391 while (!num.empty ())
14392 {
14393 std::pair<int, int> bp_num_range, bp_loc_range;
14394
14395 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14396
14397 if (bp_loc_range.first == bp_loc_range.second
14398 && bp_loc_range.first == 0)
14399 {
14400 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14401 map_breakpoint_number_range (bp_num_range,
14402 enable
14403 ? enable_breakpoint
14404 : disable_breakpoint);
14405 }
14406 else
14407 {
14408 /* Handle breakpoint ids with formats 'x.y' or
14409 'x.y-z'. */
14410 enable_disable_breakpoint_location_range
14411 (bp_num_range.first, bp_loc_range, enable);
14412 }
14413 num = extract_arg (&args);
14414 }
14415 }
14416 }
14417
14418 /* The disable command disables the specified breakpoints/locations
14419 (or all defined breakpoints) so they're no longer effective in
14420 stopping the inferior. ARGS may be in any of the forms defined in
14421 extract_bp_number_and_location. */
14422
14423 static void
14424 disable_command (const char *args, int from_tty)
14425 {
14426 enable_disable_command (args, from_tty, false);
14427 }
14428
14429 static void
14430 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14431 int count)
14432 {
14433 int target_resources_ok;
14434
14435 if (bpt->type == bp_hardware_breakpoint)
14436 {
14437 int i;
14438 i = hw_breakpoint_used_count ();
14439 target_resources_ok =
14440 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14441 i + 1, 0);
14442 if (target_resources_ok == 0)
14443 error (_("No hardware breakpoint support in the target."));
14444 else if (target_resources_ok < 0)
14445 error (_("Hardware breakpoints used exceeds limit."));
14446 }
14447
14448 if (is_watchpoint (bpt))
14449 {
14450 /* Initialize it just to avoid a GCC false warning. */
14451 enum enable_state orig_enable_state = bp_disabled;
14452
14453 try
14454 {
14455 struct watchpoint *w = (struct watchpoint *) bpt;
14456
14457 orig_enable_state = bpt->enable_state;
14458 bpt->enable_state = bp_enabled;
14459 update_watchpoint (w, 1 /* reparse */);
14460 }
14461 catch (const gdb_exception &e)
14462 {
14463 bpt->enable_state = orig_enable_state;
14464 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14465 bpt->number);
14466 return;
14467 }
14468 }
14469
14470 bpt->enable_state = bp_enabled;
14471
14472 /* Mark breakpoint locations modified. */
14473 mark_breakpoint_modified (bpt);
14474
14475 if (target_supports_enable_disable_tracepoint ()
14476 && current_trace_status ()->running && is_tracepoint (bpt))
14477 {
14478 struct bp_location *location;
14479
14480 for (location = bpt->loc; location; location = location->next)
14481 target_enable_tracepoint (location);
14482 }
14483
14484 bpt->disposition = disposition;
14485 bpt->enable_count = count;
14486 update_global_location_list (UGLL_MAY_INSERT);
14487
14488 gdb::observers::breakpoint_modified.notify (bpt);
14489 }
14490
14491
14492 void
14493 enable_breakpoint (struct breakpoint *bpt)
14494 {
14495 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14496 }
14497
14498 /* The enable command enables the specified breakpoints/locations (or
14499 all defined breakpoints) so they once again become (or continue to
14500 be) effective in stopping the inferior. ARGS may be in any of the
14501 forms defined in extract_bp_number_and_location. */
14502
14503 static void
14504 enable_command (const char *args, int from_tty)
14505 {
14506 enable_disable_command (args, from_tty, true);
14507 }
14508
14509 static void
14510 enable_once_command (const char *args, int from_tty)
14511 {
14512 map_breakpoint_numbers
14513 (args, [&] (breakpoint *b)
14514 {
14515 iterate_over_related_breakpoints
14516 (b, [&] (breakpoint *bpt)
14517 {
14518 enable_breakpoint_disp (bpt, disp_disable, 1);
14519 });
14520 });
14521 }
14522
14523 static void
14524 enable_count_command (const char *args, int from_tty)
14525 {
14526 int count;
14527
14528 if (args == NULL)
14529 error_no_arg (_("hit count"));
14530
14531 count = get_number (&args);
14532
14533 map_breakpoint_numbers
14534 (args, [&] (breakpoint *b)
14535 {
14536 iterate_over_related_breakpoints
14537 (b, [&] (breakpoint *bpt)
14538 {
14539 enable_breakpoint_disp (bpt, disp_disable, count);
14540 });
14541 });
14542 }
14543
14544 static void
14545 enable_delete_command (const char *args, int from_tty)
14546 {
14547 map_breakpoint_numbers
14548 (args, [&] (breakpoint *b)
14549 {
14550 iterate_over_related_breakpoints
14551 (b, [&] (breakpoint *bpt)
14552 {
14553 enable_breakpoint_disp (bpt, disp_del, 1);
14554 });
14555 });
14556 }
14557 \f
14558 /* Invalidate last known value of any hardware watchpoint if
14559 the memory which that value represents has been written to by
14560 GDB itself. */
14561
14562 static void
14563 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14564 CORE_ADDR addr, ssize_t len,
14565 const bfd_byte *data)
14566 {
14567 struct breakpoint *bp;
14568
14569 ALL_BREAKPOINTS (bp)
14570 if (bp->enable_state == bp_enabled
14571 && bp->type == bp_hardware_watchpoint)
14572 {
14573 struct watchpoint *wp = (struct watchpoint *) bp;
14574
14575 if (wp->val_valid && wp->val != nullptr)
14576 {
14577 struct bp_location *loc;
14578
14579 for (loc = bp->loc; loc != NULL; loc = loc->next)
14580 if (loc->loc_type == bp_loc_hardware_watchpoint
14581 && loc->address + loc->length > addr
14582 && addr + len > loc->address)
14583 {
14584 wp->val = NULL;
14585 wp->val_valid = false;
14586 }
14587 }
14588 }
14589 }
14590
14591 /* Create and insert a breakpoint for software single step. */
14592
14593 void
14594 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14595 const address_space *aspace,
14596 CORE_ADDR next_pc)
14597 {
14598 struct thread_info *tp = inferior_thread ();
14599 struct symtab_and_line sal;
14600 CORE_ADDR pc = next_pc;
14601
14602 if (tp->control.single_step_breakpoints == NULL)
14603 {
14604 tp->control.single_step_breakpoints
14605 = new_single_step_breakpoint (tp->global_num, gdbarch);
14606 }
14607
14608 sal = find_pc_line (pc, 0);
14609 sal.pc = pc;
14610 sal.section = find_pc_overlay (pc);
14611 sal.explicit_pc = 1;
14612 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14613
14614 update_global_location_list (UGLL_INSERT);
14615 }
14616
14617 /* Insert single step breakpoints according to the current state. */
14618
14619 int
14620 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14621 {
14622 struct regcache *regcache = get_current_regcache ();
14623 std::vector<CORE_ADDR> next_pcs;
14624
14625 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14626
14627 if (!next_pcs.empty ())
14628 {
14629 struct frame_info *frame = get_current_frame ();
14630 const address_space *aspace = get_frame_address_space (frame);
14631
14632 for (CORE_ADDR pc : next_pcs)
14633 insert_single_step_breakpoint (gdbarch, aspace, pc);
14634
14635 return 1;
14636 }
14637 else
14638 return 0;
14639 }
14640
14641 /* See breakpoint.h. */
14642
14643 int
14644 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14645 const address_space *aspace,
14646 CORE_ADDR pc)
14647 {
14648 struct bp_location *loc;
14649
14650 for (loc = bp->loc; loc != NULL; loc = loc->next)
14651 if (loc->inserted
14652 && breakpoint_location_address_match (loc, aspace, pc))
14653 return 1;
14654
14655 return 0;
14656 }
14657
14658 /* Check whether a software single-step breakpoint is inserted at
14659 PC. */
14660
14661 int
14662 single_step_breakpoint_inserted_here_p (const address_space *aspace,
14663 CORE_ADDR pc)
14664 {
14665 struct breakpoint *bpt;
14666
14667 ALL_BREAKPOINTS (bpt)
14668 {
14669 if (bpt->type == bp_single_step
14670 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14671 return 1;
14672 }
14673 return 0;
14674 }
14675
14676 /* Tracepoint-specific operations. */
14677
14678 /* Set tracepoint count to NUM. */
14679 static void
14680 set_tracepoint_count (int num)
14681 {
14682 tracepoint_count = num;
14683 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14684 }
14685
14686 static void
14687 trace_command (const char *arg, int from_tty)
14688 {
14689 event_location_up location = string_to_event_location (&arg,
14690 current_language);
14691 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
14692 (location.get (), true /* is_tracepoint */);
14693
14694 create_breakpoint (get_current_arch (),
14695 location.get (),
14696 NULL, 0, arg, 1 /* parse arg */,
14697 0 /* tempflag */,
14698 bp_tracepoint /* type_wanted */,
14699 0 /* Ignore count */,
14700 pending_break_support,
14701 ops,
14702 from_tty,
14703 1 /* enabled */,
14704 0 /* internal */, 0);
14705 }
14706
14707 static void
14708 ftrace_command (const char *arg, int from_tty)
14709 {
14710 event_location_up location = string_to_event_location (&arg,
14711 current_language);
14712 create_breakpoint (get_current_arch (),
14713 location.get (),
14714 NULL, 0, arg, 1 /* parse arg */,
14715 0 /* tempflag */,
14716 bp_fast_tracepoint /* type_wanted */,
14717 0 /* Ignore count */,
14718 pending_break_support,
14719 &tracepoint_breakpoint_ops,
14720 from_tty,
14721 1 /* enabled */,
14722 0 /* internal */, 0);
14723 }
14724
14725 /* strace command implementation. Creates a static tracepoint. */
14726
14727 static void
14728 strace_command (const char *arg, int from_tty)
14729 {
14730 struct breakpoint_ops *ops;
14731 event_location_up location;
14732
14733 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14734 or with a normal static tracepoint. */
14735 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14736 {
14737 ops = &strace_marker_breakpoint_ops;
14738 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
14739 }
14740 else
14741 {
14742 ops = &tracepoint_breakpoint_ops;
14743 location = string_to_event_location (&arg, current_language);
14744 }
14745
14746 create_breakpoint (get_current_arch (),
14747 location.get (),
14748 NULL, 0, arg, 1 /* parse arg */,
14749 0 /* tempflag */,
14750 bp_static_tracepoint /* type_wanted */,
14751 0 /* Ignore count */,
14752 pending_break_support,
14753 ops,
14754 from_tty,
14755 1 /* enabled */,
14756 0 /* internal */, 0);
14757 }
14758
14759 /* Set up a fake reader function that gets command lines from a linked
14760 list that was acquired during tracepoint uploading. */
14761
14762 static struct uploaded_tp *this_utp;
14763 static int next_cmd;
14764
14765 static char *
14766 read_uploaded_action (void)
14767 {
14768 char *rslt = nullptr;
14769
14770 if (next_cmd < this_utp->cmd_strings.size ())
14771 {
14772 rslt = this_utp->cmd_strings[next_cmd].get ();
14773 next_cmd++;
14774 }
14775
14776 return rslt;
14777 }
14778
14779 /* Given information about a tracepoint as recorded on a target (which
14780 can be either a live system or a trace file), attempt to create an
14781 equivalent GDB tracepoint. This is not a reliable process, since
14782 the target does not necessarily have all the information used when
14783 the tracepoint was originally defined. */
14784
14785 struct tracepoint *
14786 create_tracepoint_from_upload (struct uploaded_tp *utp)
14787 {
14788 const char *addr_str;
14789 char small_buf[100];
14790 struct tracepoint *tp;
14791
14792 if (utp->at_string)
14793 addr_str = utp->at_string.get ();
14794 else
14795 {
14796 /* In the absence of a source location, fall back to raw
14797 address. Since there is no way to confirm that the address
14798 means the same thing as when the trace was started, warn the
14799 user. */
14800 warning (_("Uploaded tracepoint %d has no "
14801 "source location, using raw address"),
14802 utp->number);
14803 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14804 addr_str = small_buf;
14805 }
14806
14807 /* There's not much we can do with a sequence of bytecodes. */
14808 if (utp->cond && !utp->cond_string)
14809 warning (_("Uploaded tracepoint %d condition "
14810 "has no source form, ignoring it"),
14811 utp->number);
14812
14813 event_location_up location = string_to_event_location (&addr_str,
14814 current_language);
14815 if (!create_breakpoint (get_current_arch (),
14816 location.get (),
14817 utp->cond_string.get (), -1, addr_str,
14818 0 /* parse cond/thread */,
14819 0 /* tempflag */,
14820 utp->type /* type_wanted */,
14821 0 /* Ignore count */,
14822 pending_break_support,
14823 &tracepoint_breakpoint_ops,
14824 0 /* from_tty */,
14825 utp->enabled /* enabled */,
14826 0 /* internal */,
14827 CREATE_BREAKPOINT_FLAGS_INSERTED))
14828 return NULL;
14829
14830 /* Get the tracepoint we just created. */
14831 tp = get_tracepoint (tracepoint_count);
14832 gdb_assert (tp != NULL);
14833
14834 if (utp->pass > 0)
14835 {
14836 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14837 tp->number);
14838
14839 trace_pass_command (small_buf, 0);
14840 }
14841
14842 /* If we have uploaded versions of the original commands, set up a
14843 special-purpose "reader" function and call the usual command line
14844 reader, then pass the result to the breakpoint command-setting
14845 function. */
14846 if (!utp->cmd_strings.empty ())
14847 {
14848 counted_command_line cmd_list;
14849
14850 this_utp = utp;
14851 next_cmd = 0;
14852
14853 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
14854
14855 breakpoint_set_commands (tp, std::move (cmd_list));
14856 }
14857 else if (!utp->actions.empty ()
14858 || !utp->step_actions.empty ())
14859 warning (_("Uploaded tracepoint %d actions "
14860 "have no source form, ignoring them"),
14861 utp->number);
14862
14863 /* Copy any status information that might be available. */
14864 tp->hit_count = utp->hit_count;
14865 tp->traceframe_usage = utp->traceframe_usage;
14866
14867 return tp;
14868 }
14869
14870 /* Print information on tracepoint number TPNUM_EXP, or all if
14871 omitted. */
14872
14873 static void
14874 info_tracepoints_command (const char *args, int from_tty)
14875 {
14876 struct ui_out *uiout = current_uiout;
14877 int num_printed;
14878
14879 num_printed = breakpoint_1 (args, false, is_tracepoint);
14880
14881 if (num_printed == 0)
14882 {
14883 if (args == NULL || *args == '\0')
14884 uiout->message ("No tracepoints.\n");
14885 else
14886 uiout->message ("No tracepoint matching '%s'.\n", args);
14887 }
14888
14889 default_collect_info ();
14890 }
14891
14892 /* The 'enable trace' command enables tracepoints.
14893 Not supported by all targets. */
14894 static void
14895 enable_trace_command (const char *args, int from_tty)
14896 {
14897 enable_command (args, from_tty);
14898 }
14899
14900 /* The 'disable trace' command disables tracepoints.
14901 Not supported by all targets. */
14902 static void
14903 disable_trace_command (const char *args, int from_tty)
14904 {
14905 disable_command (args, from_tty);
14906 }
14907
14908 /* Remove a tracepoint (or all if no argument). */
14909 static void
14910 delete_trace_command (const char *arg, int from_tty)
14911 {
14912 struct breakpoint *b, *b_tmp;
14913
14914 dont_repeat ();
14915
14916 if (arg == 0)
14917 {
14918 int breaks_to_delete = 0;
14919
14920 /* Delete all breakpoints if no argument.
14921 Do not delete internal or call-dummy breakpoints, these
14922 have to be deleted with an explicit breakpoint number
14923 argument. */
14924 ALL_TRACEPOINTS (b)
14925 if (is_tracepoint (b) && user_breakpoint_p (b))
14926 {
14927 breaks_to_delete = 1;
14928 break;
14929 }
14930
14931 /* Ask user only if there are some breakpoints to delete. */
14932 if (!from_tty
14933 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14934 {
14935 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14936 if (is_tracepoint (b) && user_breakpoint_p (b))
14937 delete_breakpoint (b);
14938 }
14939 }
14940 else
14941 map_breakpoint_numbers
14942 (arg, [&] (breakpoint *br)
14943 {
14944 iterate_over_related_breakpoints (br, delete_breakpoint);
14945 });
14946 }
14947
14948 /* Helper function for trace_pass_command. */
14949
14950 static void
14951 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14952 {
14953 tp->pass_count = count;
14954 gdb::observers::breakpoint_modified.notify (tp);
14955 if (from_tty)
14956 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
14957 tp->number, count);
14958 }
14959
14960 /* Set passcount for tracepoint.
14961
14962 First command argument is passcount, second is tracepoint number.
14963 If tracepoint number omitted, apply to most recently defined.
14964 Also accepts special argument "all". */
14965
14966 static void
14967 trace_pass_command (const char *args, int from_tty)
14968 {
14969 struct tracepoint *t1;
14970 ULONGEST count;
14971
14972 if (args == 0 || *args == 0)
14973 error (_("passcount command requires an "
14974 "argument (count + optional TP num)"));
14975
14976 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
14977
14978 args = skip_spaces (args);
14979 if (*args && strncasecmp (args, "all", 3) == 0)
14980 {
14981 struct breakpoint *b;
14982
14983 args += 3; /* Skip special argument "all". */
14984 if (*args)
14985 error (_("Junk at end of arguments."));
14986
14987 ALL_TRACEPOINTS (b)
14988 {
14989 t1 = (struct tracepoint *) b;
14990 trace_pass_set_count (t1, count, from_tty);
14991 }
14992 }
14993 else if (*args == '\0')
14994 {
14995 t1 = get_tracepoint_by_number (&args, NULL);
14996 if (t1)
14997 trace_pass_set_count (t1, count, from_tty);
14998 }
14999 else
15000 {
15001 number_or_range_parser parser (args);
15002 while (!parser.finished ())
15003 {
15004 t1 = get_tracepoint_by_number (&args, &parser);
15005 if (t1)
15006 trace_pass_set_count (t1, count, from_tty);
15007 }
15008 }
15009 }
15010
15011 struct tracepoint *
15012 get_tracepoint (int num)
15013 {
15014 struct breakpoint *t;
15015
15016 ALL_TRACEPOINTS (t)
15017 if (t->number == num)
15018 return (struct tracepoint *) t;
15019
15020 return NULL;
15021 }
15022
15023 /* Find the tracepoint with the given target-side number (which may be
15024 different from the tracepoint number after disconnecting and
15025 reconnecting). */
15026
15027 struct tracepoint *
15028 get_tracepoint_by_number_on_target (int num)
15029 {
15030 struct breakpoint *b;
15031
15032 ALL_TRACEPOINTS (b)
15033 {
15034 struct tracepoint *t = (struct tracepoint *) b;
15035
15036 if (t->number_on_target == num)
15037 return t;
15038 }
15039
15040 return NULL;
15041 }
15042
15043 /* Utility: parse a tracepoint number and look it up in the list.
15044 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15045 If the argument is missing, the most recent tracepoint
15046 (tracepoint_count) is returned. */
15047
15048 struct tracepoint *
15049 get_tracepoint_by_number (const char **arg,
15050 number_or_range_parser *parser)
15051 {
15052 struct breakpoint *t;
15053 int tpnum;
15054 const char *instring = arg == NULL ? NULL : *arg;
15055
15056 if (parser != NULL)
15057 {
15058 gdb_assert (!parser->finished ());
15059 tpnum = parser->get_number ();
15060 }
15061 else if (arg == NULL || *arg == NULL || ! **arg)
15062 tpnum = tracepoint_count;
15063 else
15064 tpnum = get_number (arg);
15065
15066 if (tpnum <= 0)
15067 {
15068 if (instring && *instring)
15069 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15070 instring);
15071 else
15072 printf_filtered (_("No previous tracepoint\n"));
15073 return NULL;
15074 }
15075
15076 ALL_TRACEPOINTS (t)
15077 if (t->number == tpnum)
15078 {
15079 return (struct tracepoint *) t;
15080 }
15081
15082 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15083 return NULL;
15084 }
15085
15086 void
15087 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15088 {
15089 if (b->thread != -1)
15090 fprintf_unfiltered (fp, " thread %d", b->thread);
15091
15092 if (b->task != 0)
15093 fprintf_unfiltered (fp, " task %d", b->task);
15094
15095 fprintf_unfiltered (fp, "\n");
15096 }
15097
15098 /* Save information on user settable breakpoints (watchpoints, etc) to
15099 a new script file named FILENAME. If FILTER is non-NULL, call it
15100 on each breakpoint and only include the ones for which it returns
15101 true. */
15102
15103 static void
15104 save_breakpoints (const char *filename, int from_tty,
15105 bool (*filter) (const struct breakpoint *))
15106 {
15107 struct breakpoint *tp;
15108 int any = 0;
15109 int extra_trace_bits = 0;
15110
15111 if (filename == 0 || *filename == 0)
15112 error (_("Argument required (file name in which to save)"));
15113
15114 /* See if we have anything to save. */
15115 ALL_BREAKPOINTS (tp)
15116 {
15117 /* Skip internal and momentary breakpoints. */
15118 if (!user_breakpoint_p (tp))
15119 continue;
15120
15121 /* If we have a filter, only save the breakpoints it accepts. */
15122 if (filter && !filter (tp))
15123 continue;
15124
15125 any = 1;
15126
15127 if (is_tracepoint (tp))
15128 {
15129 extra_trace_bits = 1;
15130
15131 /* We can stop searching. */
15132 break;
15133 }
15134 }
15135
15136 if (!any)
15137 {
15138 warning (_("Nothing to save."));
15139 return;
15140 }
15141
15142 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
15143
15144 stdio_file fp;
15145
15146 if (!fp.open (expanded_filename.get (), "w"))
15147 error (_("Unable to open file '%s' for saving (%s)"),
15148 expanded_filename.get (), safe_strerror (errno));
15149
15150 if (extra_trace_bits)
15151 save_trace_state_variables (&fp);
15152
15153 ALL_BREAKPOINTS (tp)
15154 {
15155 /* Skip internal and momentary breakpoints. */
15156 if (!user_breakpoint_p (tp))
15157 continue;
15158
15159 /* If we have a filter, only save the breakpoints it accepts. */
15160 if (filter && !filter (tp))
15161 continue;
15162
15163 tp->ops->print_recreate (tp, &fp);
15164
15165 /* Note, we can't rely on tp->number for anything, as we can't
15166 assume the recreated breakpoint numbers will match. Use $bpnum
15167 instead. */
15168
15169 if (tp->cond_string)
15170 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15171
15172 if (tp->ignore_count)
15173 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15174
15175 if (tp->type != bp_dprintf && tp->commands)
15176 {
15177 fp.puts (" commands\n");
15178
15179 current_uiout->redirect (&fp);
15180 try
15181 {
15182 print_command_lines (current_uiout, tp->commands.get (), 2);
15183 }
15184 catch (const gdb_exception &ex)
15185 {
15186 current_uiout->redirect (NULL);
15187 throw;
15188 }
15189
15190 current_uiout->redirect (NULL);
15191 fp.puts (" end\n");
15192 }
15193
15194 if (tp->enable_state == bp_disabled)
15195 fp.puts ("disable $bpnum\n");
15196
15197 /* If this is a multi-location breakpoint, check if the locations
15198 should be individually disabled. Watchpoint locations are
15199 special, and not user visible. */
15200 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15201 {
15202 struct bp_location *loc;
15203 int n = 1;
15204
15205 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15206 if (!loc->enabled)
15207 fp.printf ("disable $bpnum.%d\n", n);
15208 }
15209 }
15210
15211 if (extra_trace_bits && *default_collect)
15212 fp.printf ("set default-collect %s\n", default_collect);
15213
15214 if (from_tty)
15215 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
15216 }
15217
15218 /* The `save breakpoints' command. */
15219
15220 static void
15221 save_breakpoints_command (const char *args, int from_tty)
15222 {
15223 save_breakpoints (args, from_tty, NULL);
15224 }
15225
15226 /* The `save tracepoints' command. */
15227
15228 static void
15229 save_tracepoints_command (const char *args, int from_tty)
15230 {
15231 save_breakpoints (args, from_tty, is_tracepoint);
15232 }
15233
15234 /* Create a vector of all tracepoints. */
15235
15236 std::vector<breakpoint *>
15237 all_tracepoints (void)
15238 {
15239 std::vector<breakpoint *> tp_vec;
15240 struct breakpoint *tp;
15241
15242 ALL_TRACEPOINTS (tp)
15243 {
15244 tp_vec.push_back (tp);
15245 }
15246
15247 return tp_vec;
15248 }
15249
15250 \f
15251 /* This help string is used to consolidate all the help string for specifying
15252 locations used by several commands. */
15253
15254 #define LOCATION_HELP_STRING \
15255 "Linespecs are colon-separated lists of location parameters, such as\n\
15256 source filename, function name, label name, and line number.\n\
15257 Example: To specify the start of a label named \"the_top\" in the\n\
15258 function \"fact\" in the file \"factorial.c\", use\n\
15259 \"factorial.c:fact:the_top\".\n\
15260 \n\
15261 Address locations begin with \"*\" and specify an exact address in the\n\
15262 program. Example: To specify the fourth byte past the start function\n\
15263 \"main\", use \"*main + 4\".\n\
15264 \n\
15265 Explicit locations are similar to linespecs but use an option/argument\n\
15266 syntax to specify location parameters.\n\
15267 Example: To specify the start of the label named \"the_top\" in the\n\
15268 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15269 -function fact -label the_top\".\n\
15270 \n\
15271 By default, a specified function is matched against the program's\n\
15272 functions in all scopes. For C++, this means in all namespaces and\n\
15273 classes. For Ada, this means in all packages. E.g., in C++,\n\
15274 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15275 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
15276 specified name as a complete fully-qualified name instead."
15277
15278 /* This help string is used for the break, hbreak, tbreak and thbreak
15279 commands. It is defined as a macro to prevent duplication.
15280 COMMAND should be a string constant containing the name of the
15281 command. */
15282
15283 #define BREAK_ARGS_HELP(command) \
15284 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
15285 \t[-force-condition] [if CONDITION]\n\
15286 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15287 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15288 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15289 `-probe-dtrace' (for a DTrace probe).\n\
15290 LOCATION may be a linespec, address, or explicit location as described\n\
15291 below.\n\
15292 \n\
15293 With no LOCATION, uses current execution address of the selected\n\
15294 stack frame. This is useful for breaking on return to a stack frame.\n\
15295 \n\
15296 THREADNUM is the number from \"info threads\".\n\
15297 CONDITION is a boolean expression.\n\
15298 \n\
15299 With the \"-force-condition\" flag, the condition is defined even when\n\
15300 it is invalid for all current locations.\n\
15301 \n" LOCATION_HELP_STRING "\n\n\
15302 Multiple breakpoints at one place are permitted, and useful if their\n\
15303 conditions are different.\n\
15304 \n\
15305 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15306
15307 /* List of subcommands for "catch". */
15308 static struct cmd_list_element *catch_cmdlist;
15309
15310 /* List of subcommands for "tcatch". */
15311 static struct cmd_list_element *tcatch_cmdlist;
15312
15313 void
15314 add_catch_command (const char *name, const char *docstring,
15315 cmd_const_sfunc_ftype *sfunc,
15316 completer_ftype *completer,
15317 void *user_data_catch,
15318 void *user_data_tcatch)
15319 {
15320 struct cmd_list_element *command;
15321
15322 command = add_cmd (name, class_breakpoint, docstring,
15323 &catch_cmdlist);
15324 set_cmd_sfunc (command, sfunc);
15325 set_cmd_context (command, user_data_catch);
15326 set_cmd_completer (command, completer);
15327
15328 command = add_cmd (name, class_breakpoint, docstring,
15329 &tcatch_cmdlist);
15330 set_cmd_sfunc (command, sfunc);
15331 set_cmd_context (command, user_data_tcatch);
15332 set_cmd_completer (command, completer);
15333 }
15334
15335 struct breakpoint *
15336 iterate_over_breakpoints (gdb::function_view<bool (breakpoint *)> callback)
15337 {
15338 struct breakpoint *b, *b_tmp;
15339
15340 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15341 {
15342 if (callback (b))
15343 return b;
15344 }
15345
15346 return NULL;
15347 }
15348
15349 /* Zero if any of the breakpoint's locations could be a location where
15350 functions have been inlined, nonzero otherwise. */
15351
15352 static int
15353 is_non_inline_function (struct breakpoint *b)
15354 {
15355 /* The shared library event breakpoint is set on the address of a
15356 non-inline function. */
15357 if (b->type == bp_shlib_event)
15358 return 1;
15359
15360 return 0;
15361 }
15362
15363 /* Nonzero if the specified PC cannot be a location where functions
15364 have been inlined. */
15365
15366 int
15367 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
15368 const struct target_waitstatus *ws)
15369 {
15370 struct breakpoint *b;
15371 struct bp_location *bl;
15372
15373 ALL_BREAKPOINTS (b)
15374 {
15375 if (!is_non_inline_function (b))
15376 continue;
15377
15378 for (bl = b->loc; bl != NULL; bl = bl->next)
15379 {
15380 if (!bl->shlib_disabled
15381 && bpstat_check_location (bl, aspace, pc, ws))
15382 return 1;
15383 }
15384 }
15385
15386 return 0;
15387 }
15388
15389 /* Remove any references to OBJFILE which is going to be freed. */
15390
15391 void
15392 breakpoint_free_objfile (struct objfile *objfile)
15393 {
15394 struct bp_location **locp, *loc;
15395
15396 ALL_BP_LOCATIONS (loc, locp)
15397 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15398 loc->symtab = NULL;
15399 }
15400
15401 void
15402 initialize_breakpoint_ops (void)
15403 {
15404 static int initialized = 0;
15405
15406 struct breakpoint_ops *ops;
15407
15408 if (initialized)
15409 return;
15410 initialized = 1;
15411
15412 /* The breakpoint_ops structure to be inherit by all kinds of
15413 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15414 internal and momentary breakpoints, etc.). */
15415 ops = &bkpt_base_breakpoint_ops;
15416 *ops = base_breakpoint_ops;
15417 ops->re_set = bkpt_re_set;
15418 ops->insert_location = bkpt_insert_location;
15419 ops->remove_location = bkpt_remove_location;
15420 ops->breakpoint_hit = bkpt_breakpoint_hit;
15421 ops->create_sals_from_location = bkpt_create_sals_from_location;
15422 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15423 ops->decode_location = bkpt_decode_location;
15424
15425 /* The breakpoint_ops structure to be used in regular breakpoints. */
15426 ops = &bkpt_breakpoint_ops;
15427 *ops = bkpt_base_breakpoint_ops;
15428 ops->re_set = bkpt_re_set;
15429 ops->resources_needed = bkpt_resources_needed;
15430 ops->print_it = bkpt_print_it;
15431 ops->print_mention = bkpt_print_mention;
15432 ops->print_recreate = bkpt_print_recreate;
15433
15434 /* Ranged breakpoints. */
15435 ops = &ranged_breakpoint_ops;
15436 *ops = bkpt_breakpoint_ops;
15437 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15438 ops->resources_needed = resources_needed_ranged_breakpoint;
15439 ops->print_it = print_it_ranged_breakpoint;
15440 ops->print_one = print_one_ranged_breakpoint;
15441 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15442 ops->print_mention = print_mention_ranged_breakpoint;
15443 ops->print_recreate = print_recreate_ranged_breakpoint;
15444
15445 /* Internal breakpoints. */
15446 ops = &internal_breakpoint_ops;
15447 *ops = bkpt_base_breakpoint_ops;
15448 ops->re_set = internal_bkpt_re_set;
15449 ops->check_status = internal_bkpt_check_status;
15450 ops->print_it = internal_bkpt_print_it;
15451 ops->print_mention = internal_bkpt_print_mention;
15452
15453 /* Momentary breakpoints. */
15454 ops = &momentary_breakpoint_ops;
15455 *ops = bkpt_base_breakpoint_ops;
15456 ops->re_set = momentary_bkpt_re_set;
15457 ops->check_status = momentary_bkpt_check_status;
15458 ops->print_it = momentary_bkpt_print_it;
15459 ops->print_mention = momentary_bkpt_print_mention;
15460
15461 /* Probe breakpoints. */
15462 ops = &bkpt_probe_breakpoint_ops;
15463 *ops = bkpt_breakpoint_ops;
15464 ops->insert_location = bkpt_probe_insert_location;
15465 ops->remove_location = bkpt_probe_remove_location;
15466 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15467 ops->decode_location = bkpt_probe_decode_location;
15468
15469 /* Watchpoints. */
15470 ops = &watchpoint_breakpoint_ops;
15471 *ops = base_breakpoint_ops;
15472 ops->re_set = re_set_watchpoint;
15473 ops->insert_location = insert_watchpoint;
15474 ops->remove_location = remove_watchpoint;
15475 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15476 ops->check_status = check_status_watchpoint;
15477 ops->resources_needed = resources_needed_watchpoint;
15478 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15479 ops->print_it = print_it_watchpoint;
15480 ops->print_mention = print_mention_watchpoint;
15481 ops->print_recreate = print_recreate_watchpoint;
15482 ops->explains_signal = explains_signal_watchpoint;
15483
15484 /* Masked watchpoints. */
15485 ops = &masked_watchpoint_breakpoint_ops;
15486 *ops = watchpoint_breakpoint_ops;
15487 ops->insert_location = insert_masked_watchpoint;
15488 ops->remove_location = remove_masked_watchpoint;
15489 ops->resources_needed = resources_needed_masked_watchpoint;
15490 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15491 ops->print_it = print_it_masked_watchpoint;
15492 ops->print_one_detail = print_one_detail_masked_watchpoint;
15493 ops->print_mention = print_mention_masked_watchpoint;
15494 ops->print_recreate = print_recreate_masked_watchpoint;
15495
15496 /* Tracepoints. */
15497 ops = &tracepoint_breakpoint_ops;
15498 *ops = base_breakpoint_ops;
15499 ops->re_set = tracepoint_re_set;
15500 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15501 ops->print_one_detail = tracepoint_print_one_detail;
15502 ops->print_mention = tracepoint_print_mention;
15503 ops->print_recreate = tracepoint_print_recreate;
15504 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15505 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15506 ops->decode_location = tracepoint_decode_location;
15507
15508 /* Probe tracepoints. */
15509 ops = &tracepoint_probe_breakpoint_ops;
15510 *ops = tracepoint_breakpoint_ops;
15511 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15512 ops->decode_location = tracepoint_probe_decode_location;
15513
15514 /* Static tracepoints with marker (`-m'). */
15515 ops = &strace_marker_breakpoint_ops;
15516 *ops = tracepoint_breakpoint_ops;
15517 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15518 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15519 ops->decode_location = strace_marker_decode_location;
15520
15521 /* Fork catchpoints. */
15522 ops = &catch_fork_breakpoint_ops;
15523 *ops = base_breakpoint_ops;
15524 ops->insert_location = insert_catch_fork;
15525 ops->remove_location = remove_catch_fork;
15526 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15527 ops->print_it = print_it_catch_fork;
15528 ops->print_one = print_one_catch_fork;
15529 ops->print_mention = print_mention_catch_fork;
15530 ops->print_recreate = print_recreate_catch_fork;
15531
15532 /* Vfork catchpoints. */
15533 ops = &catch_vfork_breakpoint_ops;
15534 *ops = base_breakpoint_ops;
15535 ops->insert_location = insert_catch_vfork;
15536 ops->remove_location = remove_catch_vfork;
15537 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15538 ops->print_it = print_it_catch_vfork;
15539 ops->print_one = print_one_catch_vfork;
15540 ops->print_mention = print_mention_catch_vfork;
15541 ops->print_recreate = print_recreate_catch_vfork;
15542
15543 /* Exec catchpoints. */
15544 ops = &catch_exec_breakpoint_ops;
15545 *ops = base_breakpoint_ops;
15546 ops->insert_location = insert_catch_exec;
15547 ops->remove_location = remove_catch_exec;
15548 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15549 ops->print_it = print_it_catch_exec;
15550 ops->print_one = print_one_catch_exec;
15551 ops->print_mention = print_mention_catch_exec;
15552 ops->print_recreate = print_recreate_catch_exec;
15553
15554 /* Solib-related catchpoints. */
15555 ops = &catch_solib_breakpoint_ops;
15556 *ops = base_breakpoint_ops;
15557 ops->insert_location = insert_catch_solib;
15558 ops->remove_location = remove_catch_solib;
15559 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15560 ops->check_status = check_status_catch_solib;
15561 ops->print_it = print_it_catch_solib;
15562 ops->print_one = print_one_catch_solib;
15563 ops->print_mention = print_mention_catch_solib;
15564 ops->print_recreate = print_recreate_catch_solib;
15565
15566 ops = &dprintf_breakpoint_ops;
15567 *ops = bkpt_base_breakpoint_ops;
15568 ops->re_set = dprintf_re_set;
15569 ops->resources_needed = bkpt_resources_needed;
15570 ops->print_it = bkpt_print_it;
15571 ops->print_mention = bkpt_print_mention;
15572 ops->print_recreate = dprintf_print_recreate;
15573 ops->after_condition_true = dprintf_after_condition_true;
15574 ops->breakpoint_hit = dprintf_breakpoint_hit;
15575 }
15576
15577 /* Chain containing all defined "enable breakpoint" subcommands. */
15578
15579 static struct cmd_list_element *enablebreaklist = NULL;
15580
15581 /* See breakpoint.h. */
15582
15583 cmd_list_element *commands_cmd_element = nullptr;
15584
15585 void _initialize_breakpoint ();
15586 void
15587 _initialize_breakpoint ()
15588 {
15589 struct cmd_list_element *c;
15590
15591 initialize_breakpoint_ops ();
15592
15593 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib);
15594 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile);
15595 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change);
15596
15597 breakpoint_chain = 0;
15598 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15599 before a breakpoint is set. */
15600 breakpoint_count = 0;
15601
15602 tracepoint_count = 0;
15603
15604 add_com ("ignore", class_breakpoint, ignore_command, _("\
15605 Set ignore-count of breakpoint number N to COUNT.\n\
15606 Usage is `ignore N COUNT'."));
15607
15608 commands_cmd_element = add_com ("commands", class_breakpoint,
15609 commands_command, _("\
15610 Set commands to be executed when the given breakpoints are hit.\n\
15611 Give a space-separated breakpoint list as argument after \"commands\".\n\
15612 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15613 (e.g. `5-7').\n\
15614 With no argument, the targeted breakpoint is the last one set.\n\
15615 The commands themselves follow starting on the next line.\n\
15616 Type a line containing \"end\" to indicate the end of them.\n\
15617 Give \"silent\" as the first line to make the breakpoint silent;\n\
15618 then no output is printed when it is hit, except what the commands print."));
15619
15620 c = add_com ("condition", class_breakpoint, condition_command, _("\
15621 Specify breakpoint number N to break only if COND is true.\n\
15622 Usage is `condition [-force] N COND', where N is an integer and COND\n\
15623 is an expression to be evaluated whenever breakpoint N is reached.\n\
15624 With the \"-force\" flag, the condition is defined even when it is\n\
15625 invalid for all current locations."));
15626 set_cmd_completer (c, condition_completer);
15627
15628 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15629 Set a temporary breakpoint.\n\
15630 Like \"break\" except the breakpoint is only temporary,\n\
15631 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15632 by using \"enable delete\" on the breakpoint number.\n\
15633 \n"
15634 BREAK_ARGS_HELP ("tbreak")));
15635 set_cmd_completer (c, location_completer);
15636
15637 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15638 Set a hardware assisted breakpoint.\n\
15639 Like \"break\" except the breakpoint requires hardware support,\n\
15640 some target hardware may not have this support.\n\
15641 \n"
15642 BREAK_ARGS_HELP ("hbreak")));
15643 set_cmd_completer (c, location_completer);
15644
15645 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15646 Set a temporary hardware assisted breakpoint.\n\
15647 Like \"hbreak\" except the breakpoint is only temporary,\n\
15648 so it will be deleted when hit.\n\
15649 \n"
15650 BREAK_ARGS_HELP ("thbreak")));
15651 set_cmd_completer (c, location_completer);
15652
15653 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15654 Enable all or some breakpoints.\n\
15655 Usage: enable [BREAKPOINTNUM]...\n\
15656 Give breakpoint numbers (separated by spaces) as arguments.\n\
15657 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15658 This is used to cancel the effect of the \"disable\" command.\n\
15659 With a subcommand you can enable temporarily."),
15660 &enablelist, "enable ", 1, &cmdlist);
15661
15662 add_com_alias ("en", "enable", class_breakpoint, 1);
15663
15664 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15665 Enable all or some breakpoints.\n\
15666 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
15667 Give breakpoint numbers (separated by spaces) as arguments.\n\
15668 This is used to cancel the effect of the \"disable\" command.\n\
15669 May be abbreviated to simply \"enable\"."),
15670 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
15671
15672 add_cmd ("once", no_class, enable_once_command, _("\
15673 Enable some breakpoints for one hit.\n\
15674 Usage: enable breakpoints once BREAKPOINTNUM...\n\
15675 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15676 &enablebreaklist);
15677
15678 add_cmd ("delete", no_class, enable_delete_command, _("\
15679 Enable some breakpoints and delete when hit.\n\
15680 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
15681 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15682 &enablebreaklist);
15683
15684 add_cmd ("count", no_class, enable_count_command, _("\
15685 Enable some breakpoints for COUNT hits.\n\
15686 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
15687 If a breakpoint is hit while enabled in this fashion,\n\
15688 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15689 &enablebreaklist);
15690
15691 add_cmd ("delete", no_class, enable_delete_command, _("\
15692 Enable some breakpoints and delete when hit.\n\
15693 Usage: enable delete BREAKPOINTNUM...\n\
15694 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15695 &enablelist);
15696
15697 add_cmd ("once", no_class, enable_once_command, _("\
15698 Enable some breakpoints for one hit.\n\
15699 Usage: enable once BREAKPOINTNUM...\n\
15700 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15701 &enablelist);
15702
15703 add_cmd ("count", no_class, enable_count_command, _("\
15704 Enable some breakpoints for COUNT hits.\n\
15705 Usage: enable count COUNT BREAKPOINTNUM...\n\
15706 If a breakpoint is hit while enabled in this fashion,\n\
15707 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15708 &enablelist);
15709
15710 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15711 Disable all or some breakpoints.\n\
15712 Usage: disable [BREAKPOINTNUM]...\n\
15713 Arguments are breakpoint numbers with spaces in between.\n\
15714 To disable all breakpoints, give no argument.\n\
15715 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15716 &disablelist, "disable ", 1, &cmdlist);
15717 add_com_alias ("dis", "disable", class_breakpoint, 1);
15718 add_com_alias ("disa", "disable", class_breakpoint, 1);
15719
15720 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
15721 Disable all or some breakpoints.\n\
15722 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
15723 Arguments are breakpoint numbers with spaces in between.\n\
15724 To disable all breakpoints, give no argument.\n\
15725 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15726 This command may be abbreviated \"disable\"."),
15727 &disablelist);
15728
15729 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15730 Delete all or some breakpoints.\n\
15731 Usage: delete [BREAKPOINTNUM]...\n\
15732 Arguments are breakpoint numbers with spaces in between.\n\
15733 To delete all breakpoints, give no argument.\n\
15734 \n\
15735 Also a prefix command for deletion of other GDB objects."),
15736 &deletelist, "delete ", 1, &cmdlist);
15737 add_com_alias ("d", "delete", class_breakpoint, 1);
15738 add_com_alias ("del", "delete", class_breakpoint, 1);
15739
15740 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
15741 Delete all or some breakpoints or auto-display expressions.\n\
15742 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
15743 Arguments are breakpoint numbers with spaces in between.\n\
15744 To delete all breakpoints, give no argument.\n\
15745 This command may be abbreviated \"delete\"."),
15746 &deletelist);
15747
15748 add_com ("clear", class_breakpoint, clear_command, _("\
15749 Clear breakpoint at specified location.\n\
15750 Argument may be a linespec, explicit, or address location as described below.\n\
15751 \n\
15752 With no argument, clears all breakpoints in the line that the selected frame\n\
15753 is executing in.\n"
15754 "\n" LOCATION_HELP_STRING "\n\n\
15755 See also the \"delete\" command which clears breakpoints by number."));
15756 add_com_alias ("cl", "clear", class_breakpoint, 1);
15757
15758 c = add_com ("break", class_breakpoint, break_command, _("\
15759 Set breakpoint at specified location.\n"
15760 BREAK_ARGS_HELP ("break")));
15761 set_cmd_completer (c, location_completer);
15762
15763 add_com_alias ("b", "break", class_run, 1);
15764 add_com_alias ("br", "break", class_run, 1);
15765 add_com_alias ("bre", "break", class_run, 1);
15766 add_com_alias ("brea", "break", class_run, 1);
15767
15768 if (dbx_commands)
15769 {
15770 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15771 Break in function/address or break at a line in the current file."),
15772 &stoplist, "stop ", 1, &cmdlist);
15773 add_cmd ("in", class_breakpoint, stopin_command,
15774 _("Break in function or address."), &stoplist);
15775 add_cmd ("at", class_breakpoint, stopat_command,
15776 _("Break at a line in the current file."), &stoplist);
15777 add_com ("status", class_info, info_breakpoints_command, _("\
15778 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15779 The \"Type\" column indicates one of:\n\
15780 \tbreakpoint - normal breakpoint\n\
15781 \twatchpoint - watchpoint\n\
15782 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15783 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15784 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15785 address and file/line number respectively.\n\
15786 \n\
15787 Convenience variable \"$_\" and default examine address for \"x\"\n\
15788 are set to the address of the last breakpoint listed unless the command\n\
15789 is prefixed with \"server \".\n\n\
15790 Convenience variable \"$bpnum\" contains the number of the last\n\
15791 breakpoint set."));
15792 }
15793
15794 add_info ("breakpoints", info_breakpoints_command, _("\
15795 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15796 The \"Type\" column indicates one of:\n\
15797 \tbreakpoint - normal breakpoint\n\
15798 \twatchpoint - watchpoint\n\
15799 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15800 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15801 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15802 address and file/line number respectively.\n\
15803 \n\
15804 Convenience variable \"$_\" and default examine address for \"x\"\n\
15805 are set to the address of the last breakpoint listed unless the command\n\
15806 is prefixed with \"server \".\n\n\
15807 Convenience variable \"$bpnum\" contains the number of the last\n\
15808 breakpoint set."));
15809
15810 add_info_alias ("b", "breakpoints", 1);
15811
15812 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15813 Status of all breakpoints, or breakpoint number NUMBER.\n\
15814 The \"Type\" column indicates one of:\n\
15815 \tbreakpoint - normal breakpoint\n\
15816 \twatchpoint - watchpoint\n\
15817 \tlongjmp - internal breakpoint used to step through longjmp()\n\
15818 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15819 \tuntil - internal breakpoint used by the \"until\" command\n\
15820 \tfinish - internal breakpoint used by the \"finish\" command\n\
15821 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15822 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15823 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15824 address and file/line number respectively.\n\
15825 \n\
15826 Convenience variable \"$_\" and default examine address for \"x\"\n\
15827 are set to the address of the last breakpoint listed unless the command\n\
15828 is prefixed with \"server \".\n\n\
15829 Convenience variable \"$bpnum\" contains the number of the last\n\
15830 breakpoint set."),
15831 &maintenanceinfolist);
15832
15833 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
15834 Set catchpoints to catch events."),
15835 &catch_cmdlist, "catch ",
15836 0/*allow-unknown*/, &cmdlist);
15837
15838 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
15839 Set temporary catchpoints to catch events."),
15840 &tcatch_cmdlist, "tcatch ",
15841 0/*allow-unknown*/, &cmdlist);
15842
15843 add_catch_command ("fork", _("Catch calls to fork."),
15844 catch_fork_command_1,
15845 NULL,
15846 (void *) (uintptr_t) catch_fork_permanent,
15847 (void *) (uintptr_t) catch_fork_temporary);
15848 add_catch_command ("vfork", _("Catch calls to vfork."),
15849 catch_fork_command_1,
15850 NULL,
15851 (void *) (uintptr_t) catch_vfork_permanent,
15852 (void *) (uintptr_t) catch_vfork_temporary);
15853 add_catch_command ("exec", _("Catch calls to exec."),
15854 catch_exec_command_1,
15855 NULL,
15856 CATCH_PERMANENT,
15857 CATCH_TEMPORARY);
15858 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15859 Usage: catch load [REGEX]\n\
15860 If REGEX is given, only stop for libraries matching the regular expression."),
15861 catch_load_command_1,
15862 NULL,
15863 CATCH_PERMANENT,
15864 CATCH_TEMPORARY);
15865 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15866 Usage: catch unload [REGEX]\n\
15867 If REGEX is given, only stop for libraries matching the regular expression."),
15868 catch_unload_command_1,
15869 NULL,
15870 CATCH_PERMANENT,
15871 CATCH_TEMPORARY);
15872
15873 c = add_com ("watch", class_breakpoint, watch_command, _("\
15874 Set a watchpoint for an expression.\n\
15875 Usage: watch [-l|-location] EXPRESSION\n\
15876 A watchpoint stops execution of your program whenever the value of\n\
15877 an expression changes.\n\
15878 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15879 the memory to which it refers."));
15880 set_cmd_completer (c, expression_completer);
15881
15882 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15883 Set a read watchpoint for an expression.\n\
15884 Usage: rwatch [-l|-location] EXPRESSION\n\
15885 A watchpoint stops execution of your program whenever the value of\n\
15886 an expression is read.\n\
15887 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15888 the memory to which it refers."));
15889 set_cmd_completer (c, expression_completer);
15890
15891 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15892 Set a watchpoint for an expression.\n\
15893 Usage: awatch [-l|-location] EXPRESSION\n\
15894 A watchpoint stops execution of your program whenever the value of\n\
15895 an expression is either read or written.\n\
15896 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15897 the memory to which it refers."));
15898 set_cmd_completer (c, expression_completer);
15899
15900 add_info ("watchpoints", info_watchpoints_command, _("\
15901 Status of specified watchpoints (all watchpoints if no argument)."));
15902
15903 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15904 respond to changes - contrary to the description. */
15905 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15906 &can_use_hw_watchpoints, _("\
15907 Set debugger's willingness to use watchpoint hardware."), _("\
15908 Show debugger's willingness to use watchpoint hardware."), _("\
15909 If zero, gdb will not use hardware for new watchpoints, even if\n\
15910 such is available. (However, any hardware watchpoints that were\n\
15911 created before setting this to nonzero, will continue to use watchpoint\n\
15912 hardware.)"),
15913 NULL,
15914 show_can_use_hw_watchpoints,
15915 &setlist, &showlist);
15916
15917 can_use_hw_watchpoints = 1;
15918
15919 /* Tracepoint manipulation commands. */
15920
15921 c = add_com ("trace", class_breakpoint, trace_command, _("\
15922 Set a tracepoint at specified location.\n\
15923 \n"
15924 BREAK_ARGS_HELP ("trace") "\n\
15925 Do \"help tracepoints\" for info on other tracepoint commands."));
15926 set_cmd_completer (c, location_completer);
15927
15928 add_com_alias ("tp", "trace", class_breakpoint, 0);
15929 add_com_alias ("tr", "trace", class_breakpoint, 1);
15930 add_com_alias ("tra", "trace", class_breakpoint, 1);
15931 add_com_alias ("trac", "trace", class_breakpoint, 1);
15932
15933 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
15934 Set a fast tracepoint at specified location.\n\
15935 \n"
15936 BREAK_ARGS_HELP ("ftrace") "\n\
15937 Do \"help tracepoints\" for info on other tracepoint commands."));
15938 set_cmd_completer (c, location_completer);
15939
15940 c = add_com ("strace", class_breakpoint, strace_command, _("\
15941 Set a static tracepoint at location or marker.\n\
15942 \n\
15943 strace [LOCATION] [if CONDITION]\n\
15944 LOCATION may be a linespec, explicit, or address location (described below) \n\
15945 or -m MARKER_ID.\n\n\
15946 If a marker id is specified, probe the marker with that name. With\n\
15947 no LOCATION, uses current execution address of the selected stack frame.\n\
15948 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15949 This collects arbitrary user data passed in the probe point call to the\n\
15950 tracing library. You can inspect it when analyzing the trace buffer,\n\
15951 by printing the $_sdata variable like any other convenience variable.\n\
15952 \n\
15953 CONDITION is a boolean expression.\n\
15954 \n" LOCATION_HELP_STRING "\n\n\
15955 Multiple tracepoints at one place are permitted, and useful if their\n\
15956 conditions are different.\n\
15957 \n\
15958 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15959 Do \"help tracepoints\" for info on other tracepoint commands."));
15960 set_cmd_completer (c, location_completer);
15961
15962 add_info ("tracepoints", info_tracepoints_command, _("\
15963 Status of specified tracepoints (all tracepoints if no argument).\n\
15964 Convenience variable \"$tpnum\" contains the number of the\n\
15965 last tracepoint set."));
15966
15967 add_info_alias ("tp", "tracepoints", 1);
15968
15969 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15970 Delete specified tracepoints.\n\
15971 Arguments are tracepoint numbers, separated by spaces.\n\
15972 No argument means delete all tracepoints."),
15973 &deletelist);
15974 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
15975
15976 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15977 Disable specified tracepoints.\n\
15978 Arguments are tracepoint numbers, separated by spaces.\n\
15979 No argument means disable all tracepoints."),
15980 &disablelist);
15981 deprecate_cmd (c, "disable");
15982
15983 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15984 Enable specified tracepoints.\n\
15985 Arguments are tracepoint numbers, separated by spaces.\n\
15986 No argument means enable all tracepoints."),
15987 &enablelist);
15988 deprecate_cmd (c, "enable");
15989
15990 add_com ("passcount", class_trace, trace_pass_command, _("\
15991 Set the passcount for a tracepoint.\n\
15992 The trace will end when the tracepoint has been passed 'count' times.\n\
15993 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15994 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15995
15996 add_basic_prefix_cmd ("save", class_breakpoint,
15997 _("Save breakpoint definitions as a script."),
15998 &save_cmdlist, "save ",
15999 0/*allow-unknown*/, &cmdlist);
16000
16001 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16002 Save current breakpoint definitions as a script.\n\
16003 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16004 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16005 session to restore them."),
16006 &save_cmdlist);
16007 set_cmd_completer (c, filename_completer);
16008
16009 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16010 Save current tracepoint definitions as a script.\n\
16011 Use the 'source' command in another debug session to restore them."),
16012 &save_cmdlist);
16013 set_cmd_completer (c, filename_completer);
16014
16015 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16016 deprecate_cmd (c, "save tracepoints");
16017
16018 add_basic_prefix_cmd ("breakpoint", class_maintenance, _("\
16019 Breakpoint specific settings.\n\
16020 Configure various breakpoint-specific variables such as\n\
16021 pending breakpoint behavior."),
16022 &breakpoint_set_cmdlist, "set breakpoint ",
16023 0/*allow-unknown*/, &setlist);
16024 add_show_prefix_cmd ("breakpoint", class_maintenance, _("\
16025 Breakpoint specific settings.\n\
16026 Configure various breakpoint-specific variables such as\n\
16027 pending breakpoint behavior."),
16028 &breakpoint_show_cmdlist, "show breakpoint ",
16029 0/*allow-unknown*/, &showlist);
16030
16031 add_setshow_auto_boolean_cmd ("pending", no_class,
16032 &pending_break_support, _("\
16033 Set debugger's behavior regarding pending breakpoints."), _("\
16034 Show debugger's behavior regarding pending breakpoints."), _("\
16035 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16036 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16037 an error. If auto, an unrecognized breakpoint location results in a\n\
16038 user-query to see if a pending breakpoint should be created."),
16039 NULL,
16040 show_pending_break_support,
16041 &breakpoint_set_cmdlist,
16042 &breakpoint_show_cmdlist);
16043
16044 pending_break_support = AUTO_BOOLEAN_AUTO;
16045
16046 add_setshow_boolean_cmd ("auto-hw", no_class,
16047 &automatic_hardware_breakpoints, _("\
16048 Set automatic usage of hardware breakpoints."), _("\
16049 Show automatic usage of hardware breakpoints."), _("\
16050 If set, the debugger will automatically use hardware breakpoints for\n\
16051 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16052 a warning will be emitted for such breakpoints."),
16053 NULL,
16054 show_automatic_hardware_breakpoints,
16055 &breakpoint_set_cmdlist,
16056 &breakpoint_show_cmdlist);
16057
16058 add_setshow_boolean_cmd ("always-inserted", class_support,
16059 &always_inserted_mode, _("\
16060 Set mode for inserting breakpoints."), _("\
16061 Show mode for inserting breakpoints."), _("\
16062 When this mode is on, breakpoints are inserted immediately as soon as\n\
16063 they're created, kept inserted even when execution stops, and removed\n\
16064 only when the user deletes them. When this mode is off (the default),\n\
16065 breakpoints are inserted only when execution continues, and removed\n\
16066 when execution stops."),
16067 NULL,
16068 &show_always_inserted_mode,
16069 &breakpoint_set_cmdlist,
16070 &breakpoint_show_cmdlist);
16071
16072 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16073 condition_evaluation_enums,
16074 &condition_evaluation_mode_1, _("\
16075 Set mode of breakpoint condition evaluation."), _("\
16076 Show mode of breakpoint condition evaluation."), _("\
16077 When this is set to \"host\", breakpoint conditions will be\n\
16078 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16079 breakpoint conditions will be downloaded to the target (if the target\n\
16080 supports such feature) and conditions will be evaluated on the target's side.\n\
16081 If this is set to \"auto\" (default), this will be automatically set to\n\
16082 \"target\" if it supports condition evaluation, otherwise it will\n\
16083 be set to \"host\"."),
16084 &set_condition_evaluation_mode,
16085 &show_condition_evaluation_mode,
16086 &breakpoint_set_cmdlist,
16087 &breakpoint_show_cmdlist);
16088
16089 add_com ("break-range", class_breakpoint, break_range_command, _("\
16090 Set a breakpoint for an address range.\n\
16091 break-range START-LOCATION, END-LOCATION\n\
16092 where START-LOCATION and END-LOCATION can be one of the following:\n\
16093 LINENUM, for that line in the current file,\n\
16094 FILE:LINENUM, for that line in that file,\n\
16095 +OFFSET, for that number of lines after the current line\n\
16096 or the start of the range\n\
16097 FUNCTION, for the first line in that function,\n\
16098 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16099 *ADDRESS, for the instruction at that address.\n\
16100 \n\
16101 The breakpoint will stop execution of the inferior whenever it executes\n\
16102 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16103 range (including START-LOCATION and END-LOCATION)."));
16104
16105 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16106 Set a dynamic printf at specified location.\n\
16107 dprintf location,format string,arg1,arg2,...\n\
16108 location may be a linespec, explicit, or address location.\n"
16109 "\n" LOCATION_HELP_STRING));
16110 set_cmd_completer (c, location_completer);
16111
16112 add_setshow_enum_cmd ("dprintf-style", class_support,
16113 dprintf_style_enums, &dprintf_style, _("\
16114 Set the style of usage for dynamic printf."), _("\
16115 Show the style of usage for dynamic printf."), _("\
16116 This setting chooses how GDB will do a dynamic printf.\n\
16117 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16118 console, as with the \"printf\" command.\n\
16119 If the value is \"call\", the print is done by calling a function in your\n\
16120 program; by default printf(), but you can choose a different function or\n\
16121 output stream by setting dprintf-function and dprintf-channel."),
16122 update_dprintf_commands, NULL,
16123 &setlist, &showlist);
16124
16125 dprintf_function = xstrdup ("printf");
16126 add_setshow_string_cmd ("dprintf-function", class_support,
16127 &dprintf_function, _("\
16128 Set the function to use for dynamic printf."), _("\
16129 Show the function to use for dynamic printf."), NULL,
16130 update_dprintf_commands, NULL,
16131 &setlist, &showlist);
16132
16133 dprintf_channel = xstrdup ("");
16134 add_setshow_string_cmd ("dprintf-channel", class_support,
16135 &dprintf_channel, _("\
16136 Set the channel to use for dynamic printf."), _("\
16137 Show the channel to use for dynamic printf."), NULL,
16138 update_dprintf_commands, NULL,
16139 &setlist, &showlist);
16140
16141 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16142 &disconnected_dprintf, _("\
16143 Set whether dprintf continues after GDB disconnects."), _("\
16144 Show whether dprintf continues after GDB disconnects."), _("\
16145 Use this to let dprintf commands continue to hit and produce output\n\
16146 even if GDB disconnects or detaches from the target."),
16147 NULL,
16148 NULL,
16149 &setlist, &showlist);
16150
16151 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16152 Target agent only formatted printing, like the C \"printf\" function.\n\
16153 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
16154 This supports most C printf format specifications, like %s, %d, etc.\n\
16155 This is useful for formatted output in user-defined commands."));
16156
16157 automatic_hardware_breakpoints = true;
16158
16159 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed);
16160 gdb::observers::thread_exit.attach (remove_threaded_breakpoints);
16161 }