gdb/breakpoint: disable a bp location if condition is invalid at that location
[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)
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. */
954 if (loc->next == nullptr)
955 throw;
956 }
957 }
958
959 /* If we reach here, the condition is valid at some locations. */
960 int loc_num = 1;
961 for (bp_location *loc = b->loc; loc != nullptr;
962 loc = loc->next, loc_num++)
963 set_breakpoint_location_condition (exp, loc, b->number, loc_num);
964 }
965
966 /* We know that the new condition parsed successfully. The
967 condition string of the breakpoint can be safely updated. */
968 xfree (b->cond_string);
969 b->cond_string = xstrdup (exp);
970 b->condition_not_parsed = 0;
971 }
972 mark_breakpoint_modified (b);
973
974 gdb::observers::breakpoint_modified.notify (b);
975 }
976
977 /* Completion for the "condition" command. */
978
979 static void
980 condition_completer (struct cmd_list_element *cmd,
981 completion_tracker &tracker,
982 const char *text, const char *word)
983 {
984 const char *space;
985
986 text = skip_spaces (text);
987 space = skip_to_space (text);
988 if (*space == '\0')
989 {
990 int len;
991 struct breakpoint *b;
992
993 if (text[0] == '$')
994 {
995 /* We don't support completion of history indices. */
996 if (!isdigit (text[1]))
997 complete_internalvar (tracker, &text[1]);
998 return;
999 }
1000
1001 /* We're completing the breakpoint number. */
1002 len = strlen (text);
1003
1004 ALL_BREAKPOINTS (b)
1005 {
1006 char number[50];
1007
1008 xsnprintf (number, sizeof (number), "%d", b->number);
1009
1010 if (strncmp (number, text, len) == 0)
1011 tracker.add_completion (make_unique_xstrdup (number));
1012 }
1013
1014 return;
1015 }
1016
1017 /* We're completing the expression part. */
1018 text = skip_spaces (space);
1019 expression_completer (cmd, tracker, text, word);
1020 }
1021
1022 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1023
1024 static void
1025 condition_command (const char *arg, int from_tty)
1026 {
1027 struct breakpoint *b;
1028 const char *p;
1029 int bnum;
1030
1031 if (arg == 0)
1032 error_no_arg (_("breakpoint number"));
1033
1034 p = arg;
1035 bnum = get_number (&p);
1036 if (bnum == 0)
1037 error (_("Bad breakpoint argument: '%s'"), arg);
1038
1039 ALL_BREAKPOINTS (b)
1040 if (b->number == bnum)
1041 {
1042 /* Check if this breakpoint has a "stop" method implemented in an
1043 extension language. This method and conditions entered into GDB
1044 from the CLI are mutually exclusive. */
1045 const struct extension_language_defn *extlang
1046 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1047
1048 if (extlang != NULL)
1049 {
1050 error (_("Only one stop condition allowed. There is currently"
1051 " a %s stop condition defined for this breakpoint."),
1052 ext_lang_capitalized_name (extlang));
1053 }
1054 set_breakpoint_condition (b, p, from_tty);
1055
1056 if (is_breakpoint (b))
1057 update_global_location_list (UGLL_MAY_INSERT);
1058
1059 return;
1060 }
1061
1062 error (_("No breakpoint number %d."), bnum);
1063 }
1064
1065 /* Check that COMMAND do not contain commands that are suitable
1066 only for tracepoints and not suitable for ordinary breakpoints.
1067 Throw if any such commands is found. */
1068
1069 static void
1070 check_no_tracepoint_commands (struct command_line *commands)
1071 {
1072 struct command_line *c;
1073
1074 for (c = commands; c; c = c->next)
1075 {
1076 if (c->control_type == while_stepping_control)
1077 error (_("The 'while-stepping' command can "
1078 "only be used for tracepoints"));
1079
1080 check_no_tracepoint_commands (c->body_list_0.get ());
1081 check_no_tracepoint_commands (c->body_list_1.get ());
1082
1083 /* Not that command parsing removes leading whitespace and comment
1084 lines and also empty lines. So, we only need to check for
1085 command directly. */
1086 if (strstr (c->line, "collect ") == c->line)
1087 error (_("The 'collect' command can only be used for tracepoints"));
1088
1089 if (strstr (c->line, "teval ") == c->line)
1090 error (_("The 'teval' command can only be used for tracepoints"));
1091 }
1092 }
1093
1094 struct longjmp_breakpoint : public breakpoint
1095 {
1096 ~longjmp_breakpoint () override;
1097 };
1098
1099 /* Encapsulate tests for different types of tracepoints. */
1100
1101 static bool
1102 is_tracepoint_type (bptype type)
1103 {
1104 return (type == bp_tracepoint
1105 || type == bp_fast_tracepoint
1106 || type == bp_static_tracepoint);
1107 }
1108
1109 static bool
1110 is_longjmp_type (bptype type)
1111 {
1112 return type == bp_longjmp || type == bp_exception;
1113 }
1114
1115 /* See breakpoint.h. */
1116
1117 bool
1118 is_tracepoint (const struct breakpoint *b)
1119 {
1120 return is_tracepoint_type (b->type);
1121 }
1122
1123 /* Factory function to create an appropriate instance of breakpoint given
1124 TYPE. */
1125
1126 static std::unique_ptr<breakpoint>
1127 new_breakpoint_from_type (bptype type)
1128 {
1129 breakpoint *b;
1130
1131 if (is_tracepoint_type (type))
1132 b = new tracepoint ();
1133 else if (is_longjmp_type (type))
1134 b = new longjmp_breakpoint ();
1135 else
1136 b = new breakpoint ();
1137
1138 return std::unique_ptr<breakpoint> (b);
1139 }
1140
1141 /* A helper function that validates that COMMANDS are valid for a
1142 breakpoint. This function will throw an exception if a problem is
1143 found. */
1144
1145 static void
1146 validate_commands_for_breakpoint (struct breakpoint *b,
1147 struct command_line *commands)
1148 {
1149 if (is_tracepoint (b))
1150 {
1151 struct tracepoint *t = (struct tracepoint *) b;
1152 struct command_line *c;
1153 struct command_line *while_stepping = 0;
1154
1155 /* Reset the while-stepping step count. The previous commands
1156 might have included a while-stepping action, while the new
1157 ones might not. */
1158 t->step_count = 0;
1159
1160 /* We need to verify that each top-level element of commands is
1161 valid for tracepoints, that there's at most one
1162 while-stepping element, and that the while-stepping's body
1163 has valid tracing commands excluding nested while-stepping.
1164 We also need to validate the tracepoint action line in the
1165 context of the tracepoint --- validate_actionline actually
1166 has side effects, like setting the tracepoint's
1167 while-stepping STEP_COUNT, in addition to checking if the
1168 collect/teval actions parse and make sense in the
1169 tracepoint's context. */
1170 for (c = commands; c; c = c->next)
1171 {
1172 if (c->control_type == while_stepping_control)
1173 {
1174 if (b->type == bp_fast_tracepoint)
1175 error (_("The 'while-stepping' command "
1176 "cannot be used for fast tracepoint"));
1177 else if (b->type == bp_static_tracepoint)
1178 error (_("The 'while-stepping' command "
1179 "cannot be used for static tracepoint"));
1180
1181 if (while_stepping)
1182 error (_("The 'while-stepping' command "
1183 "can be used only once"));
1184 else
1185 while_stepping = c;
1186 }
1187
1188 validate_actionline (c->line, b);
1189 }
1190 if (while_stepping)
1191 {
1192 struct command_line *c2;
1193
1194 gdb_assert (while_stepping->body_list_1 == nullptr);
1195 c2 = while_stepping->body_list_0.get ();
1196 for (; c2; c2 = c2->next)
1197 {
1198 if (c2->control_type == while_stepping_control)
1199 error (_("The 'while-stepping' command cannot be nested"));
1200 }
1201 }
1202 }
1203 else
1204 {
1205 check_no_tracepoint_commands (commands);
1206 }
1207 }
1208
1209 /* Return a vector of all the static tracepoints set at ADDR. The
1210 caller is responsible for releasing the vector. */
1211
1212 std::vector<breakpoint *>
1213 static_tracepoints_here (CORE_ADDR addr)
1214 {
1215 struct breakpoint *b;
1216 std::vector<breakpoint *> found;
1217 struct bp_location *loc;
1218
1219 ALL_BREAKPOINTS (b)
1220 if (b->type == bp_static_tracepoint)
1221 {
1222 for (loc = b->loc; loc; loc = loc->next)
1223 if (loc->address == addr)
1224 found.push_back (b);
1225 }
1226
1227 return found;
1228 }
1229
1230 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1231 validate that only allowed commands are included. */
1232
1233 void
1234 breakpoint_set_commands (struct breakpoint *b,
1235 counted_command_line &&commands)
1236 {
1237 validate_commands_for_breakpoint (b, commands.get ());
1238
1239 b->commands = std::move (commands);
1240 gdb::observers::breakpoint_modified.notify (b);
1241 }
1242
1243 /* Set the internal `silent' flag on the breakpoint. Note that this
1244 is not the same as the "silent" that may appear in the breakpoint's
1245 commands. */
1246
1247 void
1248 breakpoint_set_silent (struct breakpoint *b, int silent)
1249 {
1250 int old_silent = b->silent;
1251
1252 b->silent = silent;
1253 if (old_silent != silent)
1254 gdb::observers::breakpoint_modified.notify (b);
1255 }
1256
1257 /* Set the thread for this breakpoint. If THREAD is -1, make the
1258 breakpoint work for any thread. */
1259
1260 void
1261 breakpoint_set_thread (struct breakpoint *b, int thread)
1262 {
1263 int old_thread = b->thread;
1264
1265 b->thread = thread;
1266 if (old_thread != thread)
1267 gdb::observers::breakpoint_modified.notify (b);
1268 }
1269
1270 /* Set the task for this breakpoint. If TASK is 0, make the
1271 breakpoint work for any task. */
1272
1273 void
1274 breakpoint_set_task (struct breakpoint *b, int task)
1275 {
1276 int old_task = b->task;
1277
1278 b->task = task;
1279 if (old_task != task)
1280 gdb::observers::breakpoint_modified.notify (b);
1281 }
1282
1283 static void
1284 commands_command_1 (const char *arg, int from_tty,
1285 struct command_line *control)
1286 {
1287 counted_command_line cmd;
1288 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1289 NULL after the call to read_command_lines if the user provides an empty
1290 list of command by just typing "end". */
1291 bool cmd_read = false;
1292
1293 std::string new_arg;
1294
1295 if (arg == NULL || !*arg)
1296 {
1297 /* Argument not explicitly given. Synthesize it. */
1298 if (breakpoint_count - prev_breakpoint_count > 1)
1299 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1300 breakpoint_count);
1301 else if (breakpoint_count > 0)
1302 new_arg = string_printf ("%d", breakpoint_count);
1303 }
1304 else
1305 {
1306 /* Create a copy of ARG. This is needed because the "commands"
1307 command may be coming from a script. In that case, the read
1308 line buffer is going to be overwritten in the lambda of
1309 'map_breakpoint_numbers' below when reading the next line
1310 before we are are done parsing the breakpoint numbers. */
1311 new_arg = arg;
1312 }
1313 arg = new_arg.c_str ();
1314
1315 map_breakpoint_numbers
1316 (arg, [&] (breakpoint *b)
1317 {
1318 if (!cmd_read)
1319 {
1320 gdb_assert (cmd == NULL);
1321 if (control != NULL)
1322 cmd = control->body_list_0;
1323 else
1324 {
1325 std::string str
1326 = string_printf (_("Type commands for breakpoint(s) "
1327 "%s, one per line."),
1328 arg);
1329
1330 auto do_validate = [=] (const char *line)
1331 {
1332 validate_actionline (line, b);
1333 };
1334 gdb::function_view<void (const char *)> validator;
1335 if (is_tracepoint (b))
1336 validator = do_validate;
1337
1338 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1339 }
1340 cmd_read = true;
1341 }
1342
1343 /* If a breakpoint was on the list more than once, we don't need to
1344 do anything. */
1345 if (b->commands != cmd)
1346 {
1347 validate_commands_for_breakpoint (b, cmd.get ());
1348 b->commands = cmd;
1349 gdb::observers::breakpoint_modified.notify (b);
1350 }
1351 });
1352 }
1353
1354 static void
1355 commands_command (const char *arg, int from_tty)
1356 {
1357 commands_command_1 (arg, from_tty, NULL);
1358 }
1359
1360 /* Like commands_command, but instead of reading the commands from
1361 input stream, takes them from an already parsed command structure.
1362
1363 This is used by cli-script.c to DTRT with breakpoint commands
1364 that are part of if and while bodies. */
1365 enum command_control_type
1366 commands_from_control_command (const char *arg, struct command_line *cmd)
1367 {
1368 commands_command_1 (arg, 0, cmd);
1369 return simple_control;
1370 }
1371
1372 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1373
1374 static int
1375 bp_location_has_shadow (struct bp_location *bl)
1376 {
1377 if (bl->loc_type != bp_loc_software_breakpoint)
1378 return 0;
1379 if (!bl->inserted)
1380 return 0;
1381 if (bl->target_info.shadow_len == 0)
1382 /* BL isn't valid, or doesn't shadow memory. */
1383 return 0;
1384 return 1;
1385 }
1386
1387 /* Update BUF, which is LEN bytes read from the target address
1388 MEMADDR, by replacing a memory breakpoint with its shadowed
1389 contents.
1390
1391 If READBUF is not NULL, this buffer must not overlap with the of
1392 the breakpoint location's shadow_contents buffer. Otherwise, a
1393 failed assertion internal error will be raised. */
1394
1395 static void
1396 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1397 const gdb_byte *writebuf_org,
1398 ULONGEST memaddr, LONGEST len,
1399 struct bp_target_info *target_info,
1400 struct gdbarch *gdbarch)
1401 {
1402 /* Now do full processing of the found relevant range of elements. */
1403 CORE_ADDR bp_addr = 0;
1404 int bp_size = 0;
1405 int bptoffset = 0;
1406
1407 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1408 current_program_space->aspace, 0))
1409 {
1410 /* The breakpoint is inserted in a different address space. */
1411 return;
1412 }
1413
1414 /* Addresses and length of the part of the breakpoint that
1415 we need to copy. */
1416 bp_addr = target_info->placed_address;
1417 bp_size = target_info->shadow_len;
1418
1419 if (bp_addr + bp_size <= memaddr)
1420 {
1421 /* The breakpoint is entirely before the chunk of memory we are
1422 reading. */
1423 return;
1424 }
1425
1426 if (bp_addr >= memaddr + len)
1427 {
1428 /* The breakpoint is entirely after the chunk of memory we are
1429 reading. */
1430 return;
1431 }
1432
1433 /* Offset within shadow_contents. */
1434 if (bp_addr < memaddr)
1435 {
1436 /* Only copy the second part of the breakpoint. */
1437 bp_size -= memaddr - bp_addr;
1438 bptoffset = memaddr - bp_addr;
1439 bp_addr = memaddr;
1440 }
1441
1442 if (bp_addr + bp_size > memaddr + len)
1443 {
1444 /* Only copy the first part of the breakpoint. */
1445 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1446 }
1447
1448 if (readbuf != NULL)
1449 {
1450 /* Verify that the readbuf buffer does not overlap with the
1451 shadow_contents buffer. */
1452 gdb_assert (target_info->shadow_contents >= readbuf + len
1453 || readbuf >= (target_info->shadow_contents
1454 + target_info->shadow_len));
1455
1456 /* Update the read buffer with this inserted breakpoint's
1457 shadow. */
1458 memcpy (readbuf + bp_addr - memaddr,
1459 target_info->shadow_contents + bptoffset, bp_size);
1460 }
1461 else
1462 {
1463 const unsigned char *bp;
1464 CORE_ADDR addr = target_info->reqstd_address;
1465 int placed_size;
1466
1467 /* Update the shadow with what we want to write to memory. */
1468 memcpy (target_info->shadow_contents + bptoffset,
1469 writebuf_org + bp_addr - memaddr, bp_size);
1470
1471 /* Determine appropriate breakpoint contents and size for this
1472 address. */
1473 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1474
1475 /* Update the final write buffer with this inserted
1476 breakpoint's INSN. */
1477 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1478 }
1479 }
1480
1481 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1482 by replacing any memory breakpoints with their shadowed contents.
1483
1484 If READBUF is not NULL, this buffer must not overlap with any of
1485 the breakpoint location's shadow_contents buffers. Otherwise,
1486 a failed assertion internal error will be raised.
1487
1488 The range of shadowed area by each bp_location is:
1489 bl->address - bp_locations_placed_address_before_address_max
1490 up to bl->address + bp_locations_shadow_len_after_address_max
1491 The range we were requested to resolve shadows for is:
1492 memaddr ... memaddr + len
1493 Thus the safe cutoff boundaries for performance optimization are
1494 memaddr + len <= (bl->address
1495 - bp_locations_placed_address_before_address_max)
1496 and:
1497 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1498
1499 void
1500 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1501 const gdb_byte *writebuf_org,
1502 ULONGEST memaddr, LONGEST len)
1503 {
1504 /* Left boundary, right boundary and median element of our binary
1505 search. */
1506 unsigned bc_l, bc_r, bc;
1507
1508 /* Find BC_L which is a leftmost element which may affect BUF
1509 content. It is safe to report lower value but a failure to
1510 report higher one. */
1511
1512 bc_l = 0;
1513 bc_r = bp_locations_count;
1514 while (bc_l + 1 < bc_r)
1515 {
1516 struct bp_location *bl;
1517
1518 bc = (bc_l + bc_r) / 2;
1519 bl = bp_locations[bc];
1520
1521 /* Check first BL->ADDRESS will not overflow due to the added
1522 constant. Then advance the left boundary only if we are sure
1523 the BC element can in no way affect the BUF content (MEMADDR
1524 to MEMADDR + LEN range).
1525
1526 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1527 offset so that we cannot miss a breakpoint with its shadow
1528 range tail still reaching MEMADDR. */
1529
1530 if ((bl->address + bp_locations_shadow_len_after_address_max
1531 >= bl->address)
1532 && (bl->address + bp_locations_shadow_len_after_address_max
1533 <= memaddr))
1534 bc_l = bc;
1535 else
1536 bc_r = bc;
1537 }
1538
1539 /* Due to the binary search above, we need to make sure we pick the
1540 first location that's at BC_L's address. E.g., if there are
1541 multiple locations at the same address, BC_L may end up pointing
1542 at a duplicate location, and miss the "master"/"inserted"
1543 location. Say, given locations L1, L2 and L3 at addresses A and
1544 B:
1545
1546 L1@A, L2@A, L3@B, ...
1547
1548 BC_L could end up pointing at location L2, while the "master"
1549 location could be L1. Since the `loc->inserted' flag is only set
1550 on "master" locations, we'd forget to restore the shadow of L1
1551 and L2. */
1552 while (bc_l > 0
1553 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1554 bc_l--;
1555
1556 /* Now do full processing of the found relevant range of elements. */
1557
1558 for (bc = bc_l; bc < bp_locations_count; bc++)
1559 {
1560 struct bp_location *bl = bp_locations[bc];
1561
1562 /* bp_location array has BL->OWNER always non-NULL. */
1563 if (bl->owner->type == bp_none)
1564 warning (_("reading through apparently deleted breakpoint #%d?"),
1565 bl->owner->number);
1566
1567 /* Performance optimization: any further element can no longer affect BUF
1568 content. */
1569
1570 if (bl->address >= bp_locations_placed_address_before_address_max
1571 && memaddr + len <= (bl->address
1572 - bp_locations_placed_address_before_address_max))
1573 break;
1574
1575 if (!bp_location_has_shadow (bl))
1576 continue;
1577
1578 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1579 memaddr, len, &bl->target_info, bl->gdbarch);
1580 }
1581 }
1582
1583 /* See breakpoint.h. */
1584
1585 bool
1586 is_breakpoint (const struct breakpoint *bpt)
1587 {
1588 return (bpt->type == bp_breakpoint
1589 || bpt->type == bp_hardware_breakpoint
1590 || bpt->type == bp_dprintf);
1591 }
1592
1593 /* Return true if BPT is of any hardware watchpoint kind. */
1594
1595 static bool
1596 is_hardware_watchpoint (const struct breakpoint *bpt)
1597 {
1598 return (bpt->type == bp_hardware_watchpoint
1599 || bpt->type == bp_read_watchpoint
1600 || bpt->type == bp_access_watchpoint);
1601 }
1602
1603 /* See breakpoint.h. */
1604
1605 bool
1606 is_watchpoint (const struct breakpoint *bpt)
1607 {
1608 return (is_hardware_watchpoint (bpt)
1609 || bpt->type == bp_watchpoint);
1610 }
1611
1612 /* Returns true if the current thread and its running state are safe
1613 to evaluate or update watchpoint B. Watchpoints on local
1614 expressions need to be evaluated in the context of the thread that
1615 was current when the watchpoint was created, and, that thread needs
1616 to be stopped to be able to select the correct frame context.
1617 Watchpoints on global expressions can be evaluated on any thread,
1618 and in any state. It is presently left to the target allowing
1619 memory accesses when threads are running. */
1620
1621 static int
1622 watchpoint_in_thread_scope (struct watchpoint *b)
1623 {
1624 return (b->pspace == current_program_space
1625 && (b->watchpoint_thread == null_ptid
1626 || (inferior_ptid == b->watchpoint_thread
1627 && !inferior_thread ()->executing)));
1628 }
1629
1630 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1631 associated bp_watchpoint_scope breakpoint. */
1632
1633 static void
1634 watchpoint_del_at_next_stop (struct watchpoint *w)
1635 {
1636 if (w->related_breakpoint != w)
1637 {
1638 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1639 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1640 w->related_breakpoint->disposition = disp_del_at_next_stop;
1641 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1642 w->related_breakpoint = w;
1643 }
1644 w->disposition = disp_del_at_next_stop;
1645 }
1646
1647 /* Extract a bitfield value from value VAL using the bit parameters contained in
1648 watchpoint W. */
1649
1650 static struct value *
1651 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1652 {
1653 struct value *bit_val;
1654
1655 if (val == NULL)
1656 return NULL;
1657
1658 bit_val = allocate_value (value_type (val));
1659
1660 unpack_value_bitfield (bit_val,
1661 w->val_bitpos,
1662 w->val_bitsize,
1663 value_contents_for_printing (val),
1664 value_offset (val),
1665 val);
1666
1667 return bit_val;
1668 }
1669
1670 /* Allocate a dummy location and add it to B, which must be a software
1671 watchpoint. This is required because even if a software watchpoint
1672 is not watching any memory, bpstat_stop_status requires a location
1673 to be able to report stops. */
1674
1675 static void
1676 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1677 struct program_space *pspace)
1678 {
1679 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1680
1681 b->loc = allocate_bp_location (b);
1682 b->loc->pspace = pspace;
1683 b->loc->address = -1;
1684 b->loc->length = -1;
1685 }
1686
1687 /* Returns true if B is a software watchpoint that is not watching any
1688 memory (e.g., "watch $pc"). */
1689
1690 static bool
1691 is_no_memory_software_watchpoint (struct breakpoint *b)
1692 {
1693 return (b->type == bp_watchpoint
1694 && b->loc != NULL
1695 && b->loc->next == NULL
1696 && b->loc->address == -1
1697 && b->loc->length == -1);
1698 }
1699
1700 /* Assuming that B is a watchpoint:
1701 - Reparse watchpoint expression, if REPARSE is non-zero
1702 - Evaluate expression and store the result in B->val
1703 - Evaluate the condition if there is one, and store the result
1704 in b->loc->cond.
1705 - Update the list of values that must be watched in B->loc.
1706
1707 If the watchpoint disposition is disp_del_at_next_stop, then do
1708 nothing. If this is local watchpoint that is out of scope, delete
1709 it.
1710
1711 Even with `set breakpoint always-inserted on' the watchpoints are
1712 removed + inserted on each stop here. Normal breakpoints must
1713 never be removed because they might be missed by a running thread
1714 when debugging in non-stop mode. On the other hand, hardware
1715 watchpoints (is_hardware_watchpoint; processed here) are specific
1716 to each LWP since they are stored in each LWP's hardware debug
1717 registers. Therefore, such LWP must be stopped first in order to
1718 be able to modify its hardware watchpoints.
1719
1720 Hardware watchpoints must be reset exactly once after being
1721 presented to the user. It cannot be done sooner, because it would
1722 reset the data used to present the watchpoint hit to the user. And
1723 it must not be done later because it could display the same single
1724 watchpoint hit during multiple GDB stops. Note that the latter is
1725 relevant only to the hardware watchpoint types bp_read_watchpoint
1726 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1727 not user-visible - its hit is suppressed if the memory content has
1728 not changed.
1729
1730 The following constraints influence the location where we can reset
1731 hardware watchpoints:
1732
1733 * target_stopped_by_watchpoint and target_stopped_data_address are
1734 called several times when GDB stops.
1735
1736 [linux]
1737 * Multiple hardware watchpoints can be hit at the same time,
1738 causing GDB to stop. GDB only presents one hardware watchpoint
1739 hit at a time as the reason for stopping, and all the other hits
1740 are presented later, one after the other, each time the user
1741 requests the execution to be resumed. Execution is not resumed
1742 for the threads still having pending hit event stored in
1743 LWP_INFO->STATUS. While the watchpoint is already removed from
1744 the inferior on the first stop the thread hit event is kept being
1745 reported from its cached value by linux_nat_stopped_data_address
1746 until the real thread resume happens after the watchpoint gets
1747 presented and thus its LWP_INFO->STATUS gets reset.
1748
1749 Therefore the hardware watchpoint hit can get safely reset on the
1750 watchpoint removal from inferior. */
1751
1752 static void
1753 update_watchpoint (struct watchpoint *b, int reparse)
1754 {
1755 int within_current_scope;
1756 struct frame_id saved_frame_id;
1757 int frame_saved;
1758
1759 /* If this is a local watchpoint, we only want to check if the
1760 watchpoint frame is in scope if the current thread is the thread
1761 that was used to create the watchpoint. */
1762 if (!watchpoint_in_thread_scope (b))
1763 return;
1764
1765 if (b->disposition == disp_del_at_next_stop)
1766 return;
1767
1768 frame_saved = 0;
1769
1770 /* Determine if the watchpoint is within scope. */
1771 if (b->exp_valid_block == NULL)
1772 within_current_scope = 1;
1773 else
1774 {
1775 struct frame_info *fi = get_current_frame ();
1776 struct gdbarch *frame_arch = get_frame_arch (fi);
1777 CORE_ADDR frame_pc = get_frame_pc (fi);
1778
1779 /* If we're at a point where the stack has been destroyed
1780 (e.g. in a function epilogue), unwinding may not work
1781 properly. Do not attempt to recreate locations at this
1782 point. See similar comments in watchpoint_check. */
1783 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1784 return;
1785
1786 /* Save the current frame's ID so we can restore it after
1787 evaluating the watchpoint expression on its own frame. */
1788 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1789 took a frame parameter, so that we didn't have to change the
1790 selected frame. */
1791 frame_saved = 1;
1792 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1793
1794 fi = frame_find_by_id (b->watchpoint_frame);
1795 within_current_scope = (fi != NULL);
1796 if (within_current_scope)
1797 select_frame (fi);
1798 }
1799
1800 /* We don't free locations. They are stored in the bp_location array
1801 and update_global_location_list will eventually delete them and
1802 remove breakpoints if needed. */
1803 b->loc = NULL;
1804
1805 if (within_current_scope && reparse)
1806 {
1807 const char *s;
1808
1809 b->exp.reset ();
1810 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1811 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1812 /* If the meaning of expression itself changed, the old value is
1813 no longer relevant. We don't want to report a watchpoint hit
1814 to the user when the old value and the new value may actually
1815 be completely different objects. */
1816 b->val = NULL;
1817 b->val_valid = false;
1818
1819 /* Note that unlike with breakpoints, the watchpoint's condition
1820 expression is stored in the breakpoint object, not in the
1821 locations (re)created below. */
1822 if (b->cond_string != NULL)
1823 {
1824 b->cond_exp.reset ();
1825
1826 s = b->cond_string;
1827 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1828 }
1829 }
1830
1831 /* If we failed to parse the expression, for example because
1832 it refers to a global variable in a not-yet-loaded shared library,
1833 don't try to insert watchpoint. We don't automatically delete
1834 such watchpoint, though, since failure to parse expression
1835 is different from out-of-scope watchpoint. */
1836 if (!target_has_execution ())
1837 {
1838 /* Without execution, memory can't change. No use to try and
1839 set watchpoint locations. The watchpoint will be reset when
1840 the target gains execution, through breakpoint_re_set. */
1841 if (!can_use_hw_watchpoints)
1842 {
1843 if (b->ops->works_in_software_mode (b))
1844 b->type = bp_watchpoint;
1845 else
1846 error (_("Can't set read/access watchpoint when "
1847 "hardware watchpoints are disabled."));
1848 }
1849 }
1850 else if (within_current_scope && b->exp)
1851 {
1852 int pc = 0;
1853 std::vector<value_ref_ptr> val_chain;
1854 struct value *v, *result;
1855 struct program_space *frame_pspace;
1856
1857 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
1858
1859 /* Avoid setting b->val if it's already set. The meaning of
1860 b->val is 'the last value' user saw, and we should update
1861 it only if we reported that last value to user. As it
1862 happens, the code that reports it updates b->val directly.
1863 We don't keep track of the memory value for masked
1864 watchpoints. */
1865 if (!b->val_valid && !is_masked_watchpoint (b))
1866 {
1867 if (b->val_bitsize != 0)
1868 v = extract_bitfield_from_watchpoint_value (b, v);
1869 b->val = release_value (v);
1870 b->val_valid = true;
1871 }
1872
1873 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1874
1875 /* Look at each value on the value chain. */
1876 gdb_assert (!val_chain.empty ());
1877 for (const value_ref_ptr &iter : val_chain)
1878 {
1879 v = iter.get ();
1880
1881 /* If it's a memory location, and GDB actually needed
1882 its contents to evaluate the expression, then we
1883 must watch it. If the first value returned is
1884 still lazy, that means an error occurred reading it;
1885 watch it anyway in case it becomes readable. */
1886 if (VALUE_LVAL (v) == lval_memory
1887 && (v == val_chain[0] || ! value_lazy (v)))
1888 {
1889 struct type *vtype = check_typedef (value_type (v));
1890
1891 /* We only watch structs and arrays if user asked
1892 for it explicitly, never if they just happen to
1893 appear in the middle of some value chain. */
1894 if (v == result
1895 || (vtype->code () != TYPE_CODE_STRUCT
1896 && vtype->code () != TYPE_CODE_ARRAY))
1897 {
1898 CORE_ADDR addr;
1899 enum target_hw_bp_type type;
1900 struct bp_location *loc, **tmp;
1901 int bitpos = 0, bitsize = 0;
1902
1903 if (value_bitsize (v) != 0)
1904 {
1905 /* Extract the bit parameters out from the bitfield
1906 sub-expression. */
1907 bitpos = value_bitpos (v);
1908 bitsize = value_bitsize (v);
1909 }
1910 else if (v == result && b->val_bitsize != 0)
1911 {
1912 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1913 lvalue whose bit parameters are saved in the fields
1914 VAL_BITPOS and VAL_BITSIZE. */
1915 bitpos = b->val_bitpos;
1916 bitsize = b->val_bitsize;
1917 }
1918
1919 addr = value_address (v);
1920 if (bitsize != 0)
1921 {
1922 /* Skip the bytes that don't contain the bitfield. */
1923 addr += bitpos / 8;
1924 }
1925
1926 type = hw_write;
1927 if (b->type == bp_read_watchpoint)
1928 type = hw_read;
1929 else if (b->type == bp_access_watchpoint)
1930 type = hw_access;
1931
1932 loc = allocate_bp_location (b);
1933 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1934 ;
1935 *tmp = loc;
1936 loc->gdbarch = get_type_arch (value_type (v));
1937
1938 loc->pspace = frame_pspace;
1939 loc->address = address_significant (loc->gdbarch, addr);
1940
1941 if (bitsize != 0)
1942 {
1943 /* Just cover the bytes that make up the bitfield. */
1944 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1945 }
1946 else
1947 loc->length = TYPE_LENGTH (value_type (v));
1948
1949 loc->watchpoint_type = type;
1950 }
1951 }
1952 }
1953
1954 /* Change the type of breakpoint between hardware assisted or
1955 an ordinary watchpoint depending on the hardware support
1956 and free hardware slots. REPARSE is set when the inferior
1957 is started. */
1958 if (reparse)
1959 {
1960 int reg_cnt;
1961 enum bp_loc_type loc_type;
1962 struct bp_location *bl;
1963
1964 reg_cnt = can_use_hardware_watchpoint (val_chain);
1965
1966 if (reg_cnt)
1967 {
1968 int i, target_resources_ok, other_type_used;
1969 enum bptype type;
1970
1971 /* Use an exact watchpoint when there's only one memory region to be
1972 watched, and only one debug register is needed to watch it. */
1973 b->exact = target_exact_watchpoints && reg_cnt == 1;
1974
1975 /* We need to determine how many resources are already
1976 used for all other hardware watchpoints plus this one
1977 to see if we still have enough resources to also fit
1978 this watchpoint in as well. */
1979
1980 /* If this is a software watchpoint, we try to turn it
1981 to a hardware one -- count resources as if B was of
1982 hardware watchpoint type. */
1983 type = b->type;
1984 if (type == bp_watchpoint)
1985 type = bp_hardware_watchpoint;
1986
1987 /* This watchpoint may or may not have been placed on
1988 the list yet at this point (it won't be in the list
1989 if we're trying to create it for the first time,
1990 through watch_command), so always account for it
1991 manually. */
1992
1993 /* Count resources used by all watchpoints except B. */
1994 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
1995
1996 /* Add in the resources needed for B. */
1997 i += hw_watchpoint_use_count (b);
1998
1999 target_resources_ok
2000 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2001 if (target_resources_ok <= 0)
2002 {
2003 int sw_mode = b->ops->works_in_software_mode (b);
2004
2005 if (target_resources_ok == 0 && !sw_mode)
2006 error (_("Target does not support this type of "
2007 "hardware watchpoint."));
2008 else if (target_resources_ok < 0 && !sw_mode)
2009 error (_("There are not enough available hardware "
2010 "resources for this watchpoint."));
2011
2012 /* Downgrade to software watchpoint. */
2013 b->type = bp_watchpoint;
2014 }
2015 else
2016 {
2017 /* If this was a software watchpoint, we've just
2018 found we have enough resources to turn it to a
2019 hardware watchpoint. Otherwise, this is a
2020 nop. */
2021 b->type = type;
2022 }
2023 }
2024 else if (!b->ops->works_in_software_mode (b))
2025 {
2026 if (!can_use_hw_watchpoints)
2027 error (_("Can't set read/access watchpoint when "
2028 "hardware watchpoints are disabled."));
2029 else
2030 error (_("Expression cannot be implemented with "
2031 "read/access watchpoint."));
2032 }
2033 else
2034 b->type = bp_watchpoint;
2035
2036 loc_type = (b->type == bp_watchpoint? bp_loc_other
2037 : bp_loc_hardware_watchpoint);
2038 for (bl = b->loc; bl; bl = bl->next)
2039 bl->loc_type = loc_type;
2040 }
2041
2042 /* If a software watchpoint is not watching any memory, then the
2043 above left it without any location set up. But,
2044 bpstat_stop_status requires a location to be able to report
2045 stops, so make sure there's at least a dummy one. */
2046 if (b->type == bp_watchpoint && b->loc == NULL)
2047 software_watchpoint_add_no_memory_location (b, frame_pspace);
2048 }
2049 else if (!within_current_scope)
2050 {
2051 printf_filtered (_("\
2052 Watchpoint %d deleted because the program has left the block\n\
2053 in which its expression is valid.\n"),
2054 b->number);
2055 watchpoint_del_at_next_stop (b);
2056 }
2057
2058 /* Restore the selected frame. */
2059 if (frame_saved)
2060 select_frame (frame_find_by_id (saved_frame_id));
2061 }
2062
2063
2064 /* Returns 1 iff breakpoint location should be
2065 inserted in the inferior. We don't differentiate the type of BL's owner
2066 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2067 breakpoint_ops is not defined, because in insert_bp_location,
2068 tracepoint's insert_location will not be called. */
2069 static int
2070 should_be_inserted (struct bp_location *bl)
2071 {
2072 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2073 return 0;
2074
2075 if (bl->owner->disposition == disp_del_at_next_stop)
2076 return 0;
2077
2078 if (!bl->enabled || bl->disabled_by_cond
2079 || bl->shlib_disabled || bl->duplicate)
2080 return 0;
2081
2082 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2083 return 0;
2084
2085 /* This is set for example, when we're attached to the parent of a
2086 vfork, and have detached from the child. The child is running
2087 free, and we expect it to do an exec or exit, at which point the
2088 OS makes the parent schedulable again (and the target reports
2089 that the vfork is done). Until the child is done with the shared
2090 memory region, do not insert breakpoints in the parent, otherwise
2091 the child could still trip on the parent's breakpoints. Since
2092 the parent is blocked anyway, it won't miss any breakpoint. */
2093 if (bl->pspace->breakpoints_not_allowed)
2094 return 0;
2095
2096 /* Don't insert a breakpoint if we're trying to step past its
2097 location, except if the breakpoint is a single-step breakpoint,
2098 and the breakpoint's thread is the thread which is stepping past
2099 a breakpoint. */
2100 if ((bl->loc_type == bp_loc_software_breakpoint
2101 || bl->loc_type == bp_loc_hardware_breakpoint)
2102 && stepping_past_instruction_at (bl->pspace->aspace,
2103 bl->address)
2104 /* The single-step breakpoint may be inserted at the location
2105 we're trying to step if the instruction branches to itself.
2106 However, the instruction won't be executed at all and it may
2107 break the semantics of the instruction, for example, the
2108 instruction is a conditional branch or updates some flags.
2109 We can't fix it unless GDB is able to emulate the instruction
2110 or switch to displaced stepping. */
2111 && !(bl->owner->type == bp_single_step
2112 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2113 {
2114 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2115 paddress (bl->gdbarch, bl->address));
2116 return 0;
2117 }
2118
2119 /* Don't insert watchpoints if we're trying to step past the
2120 instruction that triggered one. */
2121 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2122 && stepping_past_nonsteppable_watchpoint ())
2123 {
2124 infrun_debug_printf ("stepping past non-steppable watchpoint. "
2125 "skipping watchpoint at %s:%d\n",
2126 paddress (bl->gdbarch, bl->address), bl->length);
2127 return 0;
2128 }
2129
2130 return 1;
2131 }
2132
2133 /* Same as should_be_inserted but does the check assuming
2134 that the location is not duplicated. */
2135
2136 static int
2137 unduplicated_should_be_inserted (struct bp_location *bl)
2138 {
2139 int result;
2140 const int save_duplicate = bl->duplicate;
2141
2142 bl->duplicate = 0;
2143 result = should_be_inserted (bl);
2144 bl->duplicate = save_duplicate;
2145 return result;
2146 }
2147
2148 /* Parses a conditional described by an expression COND into an
2149 agent expression bytecode suitable for evaluation
2150 by the bytecode interpreter. Return NULL if there was
2151 any error during parsing. */
2152
2153 static agent_expr_up
2154 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2155 {
2156 if (cond == NULL)
2157 return NULL;
2158
2159 agent_expr_up aexpr;
2160
2161 /* We don't want to stop processing, so catch any errors
2162 that may show up. */
2163 try
2164 {
2165 aexpr = gen_eval_for_expr (scope, cond);
2166 }
2167
2168 catch (const gdb_exception_error &ex)
2169 {
2170 /* If we got here, it means the condition could not be parsed to a valid
2171 bytecode expression and thus can't be evaluated on the target's side.
2172 It's no use iterating through the conditions. */
2173 }
2174
2175 /* We have a valid agent expression. */
2176 return aexpr;
2177 }
2178
2179 /* Based on location BL, create a list of breakpoint conditions to be
2180 passed on to the target. If we have duplicated locations with different
2181 conditions, we will add such conditions to the list. The idea is that the
2182 target will evaluate the list of conditions and will only notify GDB when
2183 one of them is true. */
2184
2185 static void
2186 build_target_condition_list (struct bp_location *bl)
2187 {
2188 struct bp_location **locp = NULL, **loc2p;
2189 int null_condition_or_parse_error = 0;
2190 int modified = bl->needs_update;
2191 struct bp_location *loc;
2192
2193 /* Release conditions left over from a previous insert. */
2194 bl->target_info.conditions.clear ();
2195
2196 /* This is only meaningful if the target is
2197 evaluating conditions and if the user has
2198 opted for condition evaluation on the target's
2199 side. */
2200 if (gdb_evaluates_breakpoint_condition_p ()
2201 || !target_supports_evaluation_of_breakpoint_conditions ())
2202 return;
2203
2204 /* Do a first pass to check for locations with no assigned
2205 conditions or conditions that fail to parse to a valid agent
2206 expression bytecode. If any of these happen, then it's no use to
2207 send conditions to the target since this location will always
2208 trigger and generate a response back to GDB. Note we consider
2209 all locations at the same address irrespective of type, i.e.,
2210 even if the locations aren't considered duplicates (e.g.,
2211 software breakpoint and hardware breakpoint at the same
2212 address). */
2213 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2214 {
2215 loc = (*loc2p);
2216 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2217 {
2218 if (modified)
2219 {
2220 /* Re-parse the conditions since something changed. In that
2221 case we already freed the condition bytecodes (see
2222 force_breakpoint_reinsertion). We just
2223 need to parse the condition to bytecodes again. */
2224 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2225 loc->cond.get ());
2226 }
2227
2228 /* If we have a NULL bytecode expression, it means something
2229 went wrong or we have a null condition expression. */
2230 if (!loc->cond_bytecode)
2231 {
2232 null_condition_or_parse_error = 1;
2233 break;
2234 }
2235 }
2236 }
2237
2238 /* If any of these happened, it means we will have to evaluate the conditions
2239 for the location's address on gdb's side. It is no use keeping bytecodes
2240 for all the other duplicate locations, thus we free all of them here.
2241
2242 This is so we have a finer control over which locations' conditions are
2243 being evaluated by GDB or the remote stub. */
2244 if (null_condition_or_parse_error)
2245 {
2246 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2247 {
2248 loc = (*loc2p);
2249 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2250 {
2251 /* Only go as far as the first NULL bytecode is
2252 located. */
2253 if (!loc->cond_bytecode)
2254 return;
2255
2256 loc->cond_bytecode.reset ();
2257 }
2258 }
2259 }
2260
2261 /* No NULL conditions or failed bytecode generation. Build a
2262 condition list for this location's address. If we have software
2263 and hardware locations at the same address, they aren't
2264 considered duplicates, but we still marge all the conditions
2265 anyway, as it's simpler, and doesn't really make a practical
2266 difference. */
2267 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2268 {
2269 loc = (*loc2p);
2270 if (loc->cond
2271 && is_breakpoint (loc->owner)
2272 && loc->pspace->num == bl->pspace->num
2273 && loc->owner->enable_state == bp_enabled
2274 && loc->enabled
2275 && !loc->disabled_by_cond)
2276 {
2277 /* Add the condition to the vector. This will be used later
2278 to send the conditions to the target. */
2279 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2280 }
2281 }
2282
2283 return;
2284 }
2285
2286 /* Parses a command described by string CMD into an agent expression
2287 bytecode suitable for evaluation by the bytecode interpreter.
2288 Return NULL if there was any error during parsing. */
2289
2290 static agent_expr_up
2291 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2292 {
2293 const char *cmdrest;
2294 const char *format_start, *format_end;
2295 struct gdbarch *gdbarch = get_current_arch ();
2296
2297 if (cmd == NULL)
2298 return NULL;
2299
2300 cmdrest = cmd;
2301
2302 if (*cmdrest == ',')
2303 ++cmdrest;
2304 cmdrest = skip_spaces (cmdrest);
2305
2306 if (*cmdrest++ != '"')
2307 error (_("No format string following the location"));
2308
2309 format_start = cmdrest;
2310
2311 format_pieces fpieces (&cmdrest);
2312
2313 format_end = cmdrest;
2314
2315 if (*cmdrest++ != '"')
2316 error (_("Bad format string, non-terminated '\"'."));
2317
2318 cmdrest = skip_spaces (cmdrest);
2319
2320 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2321 error (_("Invalid argument syntax"));
2322
2323 if (*cmdrest == ',')
2324 cmdrest++;
2325 cmdrest = skip_spaces (cmdrest);
2326
2327 /* For each argument, make an expression. */
2328
2329 std::vector<struct expression *> argvec;
2330 while (*cmdrest != '\0')
2331 {
2332 const char *cmd1;
2333
2334 cmd1 = cmdrest;
2335 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2336 argvec.push_back (expr.release ());
2337 cmdrest = cmd1;
2338 if (*cmdrest == ',')
2339 ++cmdrest;
2340 }
2341
2342 agent_expr_up aexpr;
2343
2344 /* We don't want to stop processing, so catch any errors
2345 that may show up. */
2346 try
2347 {
2348 aexpr = gen_printf (scope, gdbarch, 0, 0,
2349 format_start, format_end - format_start,
2350 argvec.size (), argvec.data ());
2351 }
2352 catch (const gdb_exception_error &ex)
2353 {
2354 /* If we got here, it means the command could not be parsed to a valid
2355 bytecode expression and thus can't be evaluated on the target's side.
2356 It's no use iterating through the other commands. */
2357 }
2358
2359 /* We have a valid agent expression, return it. */
2360 return aexpr;
2361 }
2362
2363 /* Based on location BL, create a list of breakpoint commands to be
2364 passed on to the target. If we have duplicated locations with
2365 different commands, we will add any such to the list. */
2366
2367 static void
2368 build_target_command_list (struct bp_location *bl)
2369 {
2370 struct bp_location **locp = NULL, **loc2p;
2371 int null_command_or_parse_error = 0;
2372 int modified = bl->needs_update;
2373 struct bp_location *loc;
2374
2375 /* Clear commands left over from a previous insert. */
2376 bl->target_info.tcommands.clear ();
2377
2378 if (!target_can_run_breakpoint_commands ())
2379 return;
2380
2381 /* For now, limit to agent-style dprintf breakpoints. */
2382 if (dprintf_style != dprintf_style_agent)
2383 return;
2384
2385 /* For now, if we have any location at the same address that isn't a
2386 dprintf, don't install the target-side commands, as that would
2387 make the breakpoint not be reported to the core, and we'd lose
2388 control. */
2389 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2390 {
2391 loc = (*loc2p);
2392 if (is_breakpoint (loc->owner)
2393 && loc->pspace->num == bl->pspace->num
2394 && loc->owner->type != bp_dprintf)
2395 return;
2396 }
2397
2398 /* Do a first pass to check for locations with no assigned
2399 conditions or conditions that fail to parse to a valid agent expression
2400 bytecode. If any of these happen, then it's no use to send conditions
2401 to the target since this location will always trigger and generate a
2402 response back to GDB. */
2403 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2404 {
2405 loc = (*loc2p);
2406 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2407 {
2408 if (modified)
2409 {
2410 /* Re-parse the commands since something changed. In that
2411 case we already freed the command bytecodes (see
2412 force_breakpoint_reinsertion). We just
2413 need to parse the command to bytecodes again. */
2414 loc->cmd_bytecode
2415 = parse_cmd_to_aexpr (bl->address,
2416 loc->owner->extra_string);
2417 }
2418
2419 /* If we have a NULL bytecode expression, it means something
2420 went wrong or we have a null command expression. */
2421 if (!loc->cmd_bytecode)
2422 {
2423 null_command_or_parse_error = 1;
2424 break;
2425 }
2426 }
2427 }
2428
2429 /* If anything failed, then we're not doing target-side commands,
2430 and so clean up. */
2431 if (null_command_or_parse_error)
2432 {
2433 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2434 {
2435 loc = (*loc2p);
2436 if (is_breakpoint (loc->owner)
2437 && loc->pspace->num == bl->pspace->num)
2438 {
2439 /* Only go as far as the first NULL bytecode is
2440 located. */
2441 if (loc->cmd_bytecode == NULL)
2442 return;
2443
2444 loc->cmd_bytecode.reset ();
2445 }
2446 }
2447 }
2448
2449 /* No NULL commands or failed bytecode generation. Build a command
2450 list for all duplicate locations at this location's address.
2451 Note that here we must care for whether the breakpoint location
2452 types are considered duplicates, otherwise, say, if we have a
2453 software and hardware location at the same address, the target
2454 could end up running the commands twice. For the moment, we only
2455 support targets-side commands with dprintf, but it doesn't hurt
2456 to be pedantically correct in case that changes. */
2457 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2458 {
2459 loc = (*loc2p);
2460 if (breakpoint_locations_match (bl, loc)
2461 && loc->owner->extra_string
2462 && is_breakpoint (loc->owner)
2463 && loc->pspace->num == bl->pspace->num
2464 && loc->owner->enable_state == bp_enabled
2465 && loc->enabled
2466 && !loc->disabled_by_cond)
2467 {
2468 /* Add the command to the vector. This will be used later
2469 to send the commands to the target. */
2470 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2471 }
2472 }
2473
2474 bl->target_info.persist = 0;
2475 /* Maybe flag this location as persistent. */
2476 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2477 bl->target_info.persist = 1;
2478 }
2479
2480 /* Return the kind of breakpoint on address *ADDR. Get the kind
2481 of breakpoint according to ADDR except single-step breakpoint.
2482 Get the kind of single-step breakpoint according to the current
2483 registers state. */
2484
2485 static int
2486 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2487 {
2488 if (bl->owner->type == bp_single_step)
2489 {
2490 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2491 struct regcache *regcache;
2492
2493 regcache = get_thread_regcache (thr);
2494
2495 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2496 regcache, addr);
2497 }
2498 else
2499 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2500 }
2501
2502 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2503 location. Any error messages are printed to TMP_ERROR_STREAM; and
2504 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2505 Returns 0 for success, 1 if the bp_location type is not supported or
2506 -1 for failure.
2507
2508 NOTE drow/2003-09-09: This routine could be broken down to an
2509 object-style method for each breakpoint or catchpoint type. */
2510 static int
2511 insert_bp_location (struct bp_location *bl,
2512 struct ui_file *tmp_error_stream,
2513 int *disabled_breaks,
2514 int *hw_breakpoint_error,
2515 int *hw_bp_error_explained_already)
2516 {
2517 gdb_exception bp_excpt;
2518
2519 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2520 return 0;
2521
2522 /* Note we don't initialize bl->target_info, as that wipes out
2523 the breakpoint location's shadow_contents if the breakpoint
2524 is still inserted at that location. This in turn breaks
2525 target_read_memory which depends on these buffers when
2526 a memory read is requested at the breakpoint location:
2527 Once the target_info has been wiped, we fail to see that
2528 we have a breakpoint inserted at that address and thus
2529 read the breakpoint instead of returning the data saved in
2530 the breakpoint location's shadow contents. */
2531 bl->target_info.reqstd_address = bl->address;
2532 bl->target_info.placed_address_space = bl->pspace->aspace;
2533 bl->target_info.length = bl->length;
2534
2535 /* When working with target-side conditions, we must pass all the conditions
2536 for the same breakpoint address down to the target since GDB will not
2537 insert those locations. With a list of breakpoint conditions, the target
2538 can decide when to stop and notify GDB. */
2539
2540 if (is_breakpoint (bl->owner))
2541 {
2542 build_target_condition_list (bl);
2543 build_target_command_list (bl);
2544 /* Reset the modification marker. */
2545 bl->needs_update = 0;
2546 }
2547
2548 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2549 set at a read-only address, then a breakpoint location will have
2550 been changed to hardware breakpoint before we get here. If it is
2551 "off" however, error out before actually trying to insert the
2552 breakpoint, with a nicer error message. */
2553 if (bl->loc_type == bp_loc_software_breakpoint
2554 && !automatic_hardware_breakpoints)
2555 {
2556 mem_region *mr = lookup_mem_region (bl->address);
2557
2558 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2559 {
2560 fprintf_unfiltered (tmp_error_stream,
2561 _("Cannot insert breakpoint %d.\n"
2562 "Cannot set software breakpoint "
2563 "at read-only address %s\n"),
2564 bl->owner->number,
2565 paddress (bl->gdbarch, bl->address));
2566 return 1;
2567 }
2568 }
2569
2570 if (bl->loc_type == bp_loc_software_breakpoint
2571 || bl->loc_type == bp_loc_hardware_breakpoint)
2572 {
2573 /* First check to see if we have to handle an overlay. */
2574 if (overlay_debugging == ovly_off
2575 || bl->section == NULL
2576 || !(section_is_overlay (bl->section)))
2577 {
2578 /* No overlay handling: just set the breakpoint. */
2579 try
2580 {
2581 int val;
2582
2583 val = bl->owner->ops->insert_location (bl);
2584 if (val)
2585 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2586 }
2587 catch (gdb_exception &e)
2588 {
2589 bp_excpt = std::move (e);
2590 }
2591 }
2592 else
2593 {
2594 /* This breakpoint is in an overlay section.
2595 Shall we set a breakpoint at the LMA? */
2596 if (!overlay_events_enabled)
2597 {
2598 /* Yes -- overlay event support is not active,
2599 so we must try to set a breakpoint at the LMA.
2600 This will not work for a hardware breakpoint. */
2601 if (bl->loc_type == bp_loc_hardware_breakpoint)
2602 warning (_("hardware breakpoint %d not supported in overlay!"),
2603 bl->owner->number);
2604 else
2605 {
2606 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2607 bl->section);
2608 /* Set a software (trap) breakpoint at the LMA. */
2609 bl->overlay_target_info = bl->target_info;
2610 bl->overlay_target_info.reqstd_address = addr;
2611
2612 /* No overlay handling: just set the breakpoint. */
2613 try
2614 {
2615 int val;
2616
2617 bl->overlay_target_info.kind
2618 = breakpoint_kind (bl, &addr);
2619 bl->overlay_target_info.placed_address = addr;
2620 val = target_insert_breakpoint (bl->gdbarch,
2621 &bl->overlay_target_info);
2622 if (val)
2623 bp_excpt
2624 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2625 }
2626 catch (gdb_exception &e)
2627 {
2628 bp_excpt = std::move (e);
2629 }
2630
2631 if (bp_excpt.reason != 0)
2632 fprintf_unfiltered (tmp_error_stream,
2633 "Overlay breakpoint %d "
2634 "failed: in ROM?\n",
2635 bl->owner->number);
2636 }
2637 }
2638 /* Shall we set a breakpoint at the VMA? */
2639 if (section_is_mapped (bl->section))
2640 {
2641 /* Yes. This overlay section is mapped into memory. */
2642 try
2643 {
2644 int val;
2645
2646 val = bl->owner->ops->insert_location (bl);
2647 if (val)
2648 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2649 }
2650 catch (gdb_exception &e)
2651 {
2652 bp_excpt = std::move (e);
2653 }
2654 }
2655 else
2656 {
2657 /* No. This breakpoint will not be inserted.
2658 No error, but do not mark the bp as 'inserted'. */
2659 return 0;
2660 }
2661 }
2662
2663 if (bp_excpt.reason != 0)
2664 {
2665 /* Can't set the breakpoint. */
2666
2667 /* In some cases, we might not be able to insert a
2668 breakpoint in a shared library that has already been
2669 removed, but we have not yet processed the shlib unload
2670 event. Unfortunately, some targets that implement
2671 breakpoint insertion themselves can't tell why the
2672 breakpoint insertion failed (e.g., the remote target
2673 doesn't define error codes), so we must treat generic
2674 errors as memory errors. */
2675 if (bp_excpt.reason == RETURN_ERROR
2676 && (bp_excpt.error == GENERIC_ERROR
2677 || bp_excpt.error == MEMORY_ERROR)
2678 && bl->loc_type == bp_loc_software_breakpoint
2679 && (solib_name_from_address (bl->pspace, bl->address)
2680 || shared_objfile_contains_address_p (bl->pspace,
2681 bl->address)))
2682 {
2683 /* See also: disable_breakpoints_in_shlibs. */
2684 bl->shlib_disabled = 1;
2685 gdb::observers::breakpoint_modified.notify (bl->owner);
2686 if (!*disabled_breaks)
2687 {
2688 fprintf_unfiltered (tmp_error_stream,
2689 "Cannot insert breakpoint %d.\n",
2690 bl->owner->number);
2691 fprintf_unfiltered (tmp_error_stream,
2692 "Temporarily disabling shared "
2693 "library breakpoints:\n");
2694 }
2695 *disabled_breaks = 1;
2696 fprintf_unfiltered (tmp_error_stream,
2697 "breakpoint #%d\n", bl->owner->number);
2698 return 0;
2699 }
2700 else
2701 {
2702 if (bl->loc_type == bp_loc_hardware_breakpoint)
2703 {
2704 *hw_breakpoint_error = 1;
2705 *hw_bp_error_explained_already = bp_excpt.message != NULL;
2706 fprintf_unfiltered (tmp_error_stream,
2707 "Cannot insert hardware breakpoint %d%s",
2708 bl->owner->number,
2709 bp_excpt.message ? ":" : ".\n");
2710 if (bp_excpt.message != NULL)
2711 fprintf_unfiltered (tmp_error_stream, "%s.\n",
2712 bp_excpt.what ());
2713 }
2714 else
2715 {
2716 if (bp_excpt.message == NULL)
2717 {
2718 std::string message
2719 = memory_error_message (TARGET_XFER_E_IO,
2720 bl->gdbarch, bl->address);
2721
2722 fprintf_unfiltered (tmp_error_stream,
2723 "Cannot insert breakpoint %d.\n"
2724 "%s\n",
2725 bl->owner->number, message.c_str ());
2726 }
2727 else
2728 {
2729 fprintf_unfiltered (tmp_error_stream,
2730 "Cannot insert breakpoint %d: %s\n",
2731 bl->owner->number,
2732 bp_excpt.what ());
2733 }
2734 }
2735 return 1;
2736
2737 }
2738 }
2739 else
2740 bl->inserted = 1;
2741
2742 return 0;
2743 }
2744
2745 else if (bl->loc_type == bp_loc_hardware_watchpoint
2746 /* NOTE drow/2003-09-08: This state only exists for removing
2747 watchpoints. It's not clear that it's necessary... */
2748 && bl->owner->disposition != disp_del_at_next_stop)
2749 {
2750 int val;
2751
2752 gdb_assert (bl->owner->ops != NULL
2753 && bl->owner->ops->insert_location != NULL);
2754
2755 val = bl->owner->ops->insert_location (bl);
2756
2757 /* If trying to set a read-watchpoint, and it turns out it's not
2758 supported, try emulating one with an access watchpoint. */
2759 if (val == 1 && bl->watchpoint_type == hw_read)
2760 {
2761 struct bp_location *loc, **loc_temp;
2762
2763 /* But don't try to insert it, if there's already another
2764 hw_access location that would be considered a duplicate
2765 of this one. */
2766 ALL_BP_LOCATIONS (loc, loc_temp)
2767 if (loc != bl
2768 && loc->watchpoint_type == hw_access
2769 && watchpoint_locations_match (bl, loc))
2770 {
2771 bl->duplicate = 1;
2772 bl->inserted = 1;
2773 bl->target_info = loc->target_info;
2774 bl->watchpoint_type = hw_access;
2775 val = 0;
2776 break;
2777 }
2778
2779 if (val == 1)
2780 {
2781 bl->watchpoint_type = hw_access;
2782 val = bl->owner->ops->insert_location (bl);
2783
2784 if (val)
2785 /* Back to the original value. */
2786 bl->watchpoint_type = hw_read;
2787 }
2788 }
2789
2790 bl->inserted = (val == 0);
2791 }
2792
2793 else if (bl->owner->type == bp_catchpoint)
2794 {
2795 int val;
2796
2797 gdb_assert (bl->owner->ops != NULL
2798 && bl->owner->ops->insert_location != NULL);
2799
2800 val = bl->owner->ops->insert_location (bl);
2801 if (val)
2802 {
2803 bl->owner->enable_state = bp_disabled;
2804
2805 if (val == 1)
2806 warning (_("\
2807 Error inserting catchpoint %d: Your system does not support this type\n\
2808 of catchpoint."), bl->owner->number);
2809 else
2810 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2811 }
2812
2813 bl->inserted = (val == 0);
2814
2815 /* We've already printed an error message if there was a problem
2816 inserting this catchpoint, and we've disabled the catchpoint,
2817 so just return success. */
2818 return 0;
2819 }
2820
2821 return 0;
2822 }
2823
2824 /* This function is called when program space PSPACE is about to be
2825 deleted. It takes care of updating breakpoints to not reference
2826 PSPACE anymore. */
2827
2828 void
2829 breakpoint_program_space_exit (struct program_space *pspace)
2830 {
2831 struct breakpoint *b, *b_temp;
2832 struct bp_location *loc, **loc_temp;
2833
2834 /* Remove any breakpoint that was set through this program space. */
2835 ALL_BREAKPOINTS_SAFE (b, b_temp)
2836 {
2837 if (b->pspace == pspace)
2838 delete_breakpoint (b);
2839 }
2840
2841 /* Breakpoints set through other program spaces could have locations
2842 bound to PSPACE as well. Remove those. */
2843 ALL_BP_LOCATIONS (loc, loc_temp)
2844 {
2845 struct bp_location *tmp;
2846
2847 if (loc->pspace == pspace)
2848 {
2849 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2850 if (loc->owner->loc == loc)
2851 loc->owner->loc = loc->next;
2852 else
2853 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2854 if (tmp->next == loc)
2855 {
2856 tmp->next = loc->next;
2857 break;
2858 }
2859 }
2860 }
2861
2862 /* Now update the global location list to permanently delete the
2863 removed locations above. */
2864 update_global_location_list (UGLL_DONT_INSERT);
2865 }
2866
2867 /* Make sure all breakpoints are inserted in inferior.
2868 Throws exception on any error.
2869 A breakpoint that is already inserted won't be inserted
2870 again, so calling this function twice is safe. */
2871 void
2872 insert_breakpoints (void)
2873 {
2874 struct breakpoint *bpt;
2875
2876 ALL_BREAKPOINTS (bpt)
2877 if (is_hardware_watchpoint (bpt))
2878 {
2879 struct watchpoint *w = (struct watchpoint *) bpt;
2880
2881 update_watchpoint (w, 0 /* don't reparse. */);
2882 }
2883
2884 /* Updating watchpoints creates new locations, so update the global
2885 location list. Explicitly tell ugll to insert locations and
2886 ignore breakpoints_always_inserted_mode. Also,
2887 update_global_location_list tries to "upgrade" software
2888 breakpoints to hardware breakpoints to handle "set breakpoint
2889 auto-hw", so we need to call it even if we don't have new
2890 locations. */
2891 update_global_location_list (UGLL_INSERT);
2892 }
2893
2894 /* Invoke CALLBACK for each of bp_location. */
2895
2896 void
2897 iterate_over_bp_locations (walk_bp_location_callback callback)
2898 {
2899 struct bp_location *loc, **loc_tmp;
2900
2901 ALL_BP_LOCATIONS (loc, loc_tmp)
2902 {
2903 callback (loc, NULL);
2904 }
2905 }
2906
2907 /* This is used when we need to synch breakpoint conditions between GDB and the
2908 target. It is the case with deleting and disabling of breakpoints when using
2909 always-inserted mode. */
2910
2911 static void
2912 update_inserted_breakpoint_locations (void)
2913 {
2914 struct bp_location *bl, **blp_tmp;
2915 int error_flag = 0;
2916 int val = 0;
2917 int disabled_breaks = 0;
2918 int hw_breakpoint_error = 0;
2919 int hw_bp_details_reported = 0;
2920
2921 string_file tmp_error_stream;
2922
2923 /* Explicitly mark the warning -- this will only be printed if
2924 there was an error. */
2925 tmp_error_stream.puts ("Warning:\n");
2926
2927 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2928
2929 ALL_BP_LOCATIONS (bl, blp_tmp)
2930 {
2931 /* We only want to update software breakpoints and hardware
2932 breakpoints. */
2933 if (!is_breakpoint (bl->owner))
2934 continue;
2935
2936 /* We only want to update locations that are already inserted
2937 and need updating. This is to avoid unwanted insertion during
2938 deletion of breakpoints. */
2939 if (!bl->inserted || !bl->needs_update)
2940 continue;
2941
2942 switch_to_program_space_and_thread (bl->pspace);
2943
2944 /* For targets that support global breakpoints, there's no need
2945 to select an inferior to insert breakpoint to. In fact, even
2946 if we aren't attached to any process yet, we should still
2947 insert breakpoints. */
2948 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2949 && (inferior_ptid == null_ptid || !target_has_execution ()))
2950 continue;
2951
2952 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
2953 &hw_breakpoint_error, &hw_bp_details_reported);
2954 if (val)
2955 error_flag = val;
2956 }
2957
2958 if (error_flag)
2959 {
2960 target_terminal::ours_for_output ();
2961 error_stream (tmp_error_stream);
2962 }
2963 }
2964
2965 /* Used when starting or continuing the program. */
2966
2967 static void
2968 insert_breakpoint_locations (void)
2969 {
2970 struct breakpoint *bpt;
2971 struct bp_location *bl, **blp_tmp;
2972 int error_flag = 0;
2973 int val = 0;
2974 int disabled_breaks = 0;
2975 int hw_breakpoint_error = 0;
2976 int hw_bp_error_explained_already = 0;
2977
2978 string_file tmp_error_stream;
2979
2980 /* Explicitly mark the warning -- this will only be printed if
2981 there was an error. */
2982 tmp_error_stream.puts ("Warning:\n");
2983
2984 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2985
2986 ALL_BP_LOCATIONS (bl, blp_tmp)
2987 {
2988 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2989 continue;
2990
2991 /* There is no point inserting thread-specific breakpoints if
2992 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2993 has BL->OWNER always non-NULL. */
2994 if (bl->owner->thread != -1
2995 && !valid_global_thread_id (bl->owner->thread))
2996 continue;
2997
2998 switch_to_program_space_and_thread (bl->pspace);
2999
3000 /* For targets that support global breakpoints, there's no need
3001 to select an inferior to insert breakpoint to. In fact, even
3002 if we aren't attached to any process yet, we should still
3003 insert breakpoints. */
3004 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3005 && (inferior_ptid == null_ptid || !target_has_execution ()))
3006 continue;
3007
3008 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3009 &hw_breakpoint_error, &hw_bp_error_explained_already);
3010 if (val)
3011 error_flag = val;
3012 }
3013
3014 /* If we failed to insert all locations of a watchpoint, remove
3015 them, as half-inserted watchpoint is of limited use. */
3016 ALL_BREAKPOINTS (bpt)
3017 {
3018 int some_failed = 0;
3019 struct bp_location *loc;
3020
3021 if (!is_hardware_watchpoint (bpt))
3022 continue;
3023
3024 if (!breakpoint_enabled (bpt))
3025 continue;
3026
3027 if (bpt->disposition == disp_del_at_next_stop)
3028 continue;
3029
3030 for (loc = bpt->loc; loc; loc = loc->next)
3031 if (!loc->inserted && should_be_inserted (loc))
3032 {
3033 some_failed = 1;
3034 break;
3035 }
3036 if (some_failed)
3037 {
3038 for (loc = bpt->loc; loc; loc = loc->next)
3039 if (loc->inserted)
3040 remove_breakpoint (loc);
3041
3042 hw_breakpoint_error = 1;
3043 tmp_error_stream.printf ("Could not insert "
3044 "hardware watchpoint %d.\n",
3045 bpt->number);
3046 error_flag = -1;
3047 }
3048 }
3049
3050 if (error_flag)
3051 {
3052 /* If a hardware breakpoint or watchpoint was inserted, add a
3053 message about possibly exhausted resources. */
3054 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3055 {
3056 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3057 You may have requested too many hardware breakpoints/watchpoints.\n");
3058 }
3059 target_terminal::ours_for_output ();
3060 error_stream (tmp_error_stream);
3061 }
3062 }
3063
3064 /* Used when the program stops.
3065 Returns zero if successful, or non-zero if there was a problem
3066 removing a breakpoint location. */
3067
3068 int
3069 remove_breakpoints (void)
3070 {
3071 struct bp_location *bl, **blp_tmp;
3072 int val = 0;
3073
3074 ALL_BP_LOCATIONS (bl, blp_tmp)
3075 {
3076 if (bl->inserted && !is_tracepoint (bl->owner))
3077 val |= remove_breakpoint (bl);
3078 }
3079 return val;
3080 }
3081
3082 /* When a thread exits, remove breakpoints that are related to
3083 that thread. */
3084
3085 static void
3086 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3087 {
3088 struct breakpoint *b, *b_tmp;
3089
3090 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3091 {
3092 if (b->thread == tp->global_num && user_breakpoint_p (b))
3093 {
3094 b->disposition = disp_del_at_next_stop;
3095
3096 printf_filtered (_("\
3097 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3098 b->number, print_thread_id (tp));
3099
3100 /* Hide it from the user. */
3101 b->number = 0;
3102 }
3103 }
3104 }
3105
3106 /* See breakpoint.h. */
3107
3108 void
3109 remove_breakpoints_inf (inferior *inf)
3110 {
3111 struct bp_location *bl, **blp_tmp;
3112 int val;
3113
3114 ALL_BP_LOCATIONS (bl, blp_tmp)
3115 {
3116 if (bl->pspace != inf->pspace)
3117 continue;
3118
3119 if (bl->inserted && !bl->target_info.persist)
3120 {
3121 val = remove_breakpoint (bl);
3122 if (val != 0)
3123 return;
3124 }
3125 }
3126 }
3127
3128 static int internal_breakpoint_number = -1;
3129
3130 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3131 If INTERNAL is non-zero, the breakpoint number will be populated
3132 from internal_breakpoint_number and that variable decremented.
3133 Otherwise the breakpoint number will be populated from
3134 breakpoint_count and that value incremented. Internal breakpoints
3135 do not set the internal var bpnum. */
3136 static void
3137 set_breakpoint_number (int internal, struct breakpoint *b)
3138 {
3139 if (internal)
3140 b->number = internal_breakpoint_number--;
3141 else
3142 {
3143 set_breakpoint_count (breakpoint_count + 1);
3144 b->number = breakpoint_count;
3145 }
3146 }
3147
3148 static struct breakpoint *
3149 create_internal_breakpoint (struct gdbarch *gdbarch,
3150 CORE_ADDR address, enum bptype type,
3151 const struct breakpoint_ops *ops)
3152 {
3153 symtab_and_line sal;
3154 sal.pc = address;
3155 sal.section = find_pc_overlay (sal.pc);
3156 sal.pspace = current_program_space;
3157
3158 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
3159 b->number = internal_breakpoint_number--;
3160 b->disposition = disp_donttouch;
3161
3162 return b;
3163 }
3164
3165 static const char *const longjmp_names[] =
3166 {
3167 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3168 };
3169 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3170
3171 /* Per-objfile data private to breakpoint.c. */
3172 struct breakpoint_objfile_data
3173 {
3174 /* Minimal symbol for "_ovly_debug_event" (if any). */
3175 struct bound_minimal_symbol overlay_msym {};
3176
3177 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3178 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
3179
3180 /* True if we have looked for longjmp probes. */
3181 int longjmp_searched = 0;
3182
3183 /* SystemTap probe points for longjmp (if any). These are non-owning
3184 references. */
3185 std::vector<probe *> longjmp_probes;
3186
3187 /* Minimal symbol for "std::terminate()" (if any). */
3188 struct bound_minimal_symbol terminate_msym {};
3189
3190 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3191 struct bound_minimal_symbol exception_msym {};
3192
3193 /* True if we have looked for exception probes. */
3194 int exception_searched = 0;
3195
3196 /* SystemTap probe points for unwinding (if any). These are non-owning
3197 references. */
3198 std::vector<probe *> exception_probes;
3199 };
3200
3201 static const struct objfile_key<breakpoint_objfile_data>
3202 breakpoint_objfile_key;
3203
3204 /* Minimal symbol not found sentinel. */
3205 static struct minimal_symbol msym_not_found;
3206
3207 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3208
3209 static int
3210 msym_not_found_p (const struct minimal_symbol *msym)
3211 {
3212 return msym == &msym_not_found;
3213 }
3214
3215 /* Return per-objfile data needed by breakpoint.c.
3216 Allocate the data if necessary. */
3217
3218 static struct breakpoint_objfile_data *
3219 get_breakpoint_objfile_data (struct objfile *objfile)
3220 {
3221 struct breakpoint_objfile_data *bp_objfile_data;
3222
3223 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3224 if (bp_objfile_data == NULL)
3225 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3226 return bp_objfile_data;
3227 }
3228
3229 static void
3230 create_overlay_event_breakpoint (void)
3231 {
3232 const char *const func_name = "_ovly_debug_event";
3233
3234 for (objfile *objfile : current_program_space->objfiles ())
3235 {
3236 struct breakpoint *b;
3237 struct breakpoint_objfile_data *bp_objfile_data;
3238 CORE_ADDR addr;
3239 struct explicit_location explicit_loc;
3240
3241 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3242
3243 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3244 continue;
3245
3246 if (bp_objfile_data->overlay_msym.minsym == NULL)
3247 {
3248 struct bound_minimal_symbol m;
3249
3250 m = lookup_minimal_symbol_text (func_name, objfile);
3251 if (m.minsym == NULL)
3252 {
3253 /* Avoid future lookups in this objfile. */
3254 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3255 continue;
3256 }
3257 bp_objfile_data->overlay_msym = m;
3258 }
3259
3260 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3261 b = create_internal_breakpoint (objfile->arch (), addr,
3262 bp_overlay_event,
3263 &internal_breakpoint_ops);
3264 initialize_explicit_location (&explicit_loc);
3265 explicit_loc.function_name = ASTRDUP (func_name);
3266 b->location = new_explicit_location (&explicit_loc);
3267
3268 if (overlay_debugging == ovly_auto)
3269 {
3270 b->enable_state = bp_enabled;
3271 overlay_events_enabled = 1;
3272 }
3273 else
3274 {
3275 b->enable_state = bp_disabled;
3276 overlay_events_enabled = 0;
3277 }
3278 }
3279 }
3280
3281 static void
3282 create_longjmp_master_breakpoint (void)
3283 {
3284 scoped_restore_current_program_space restore_pspace;
3285
3286 for (struct program_space *pspace : program_spaces)
3287 {
3288 set_current_program_space (pspace);
3289
3290 for (objfile *objfile : current_program_space->objfiles ())
3291 {
3292 int i;
3293 struct gdbarch *gdbarch;
3294 struct breakpoint_objfile_data *bp_objfile_data;
3295
3296 gdbarch = objfile->arch ();
3297
3298 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3299
3300 if (!bp_objfile_data->longjmp_searched)
3301 {
3302 std::vector<probe *> ret
3303 = find_probes_in_objfile (objfile, "libc", "longjmp");
3304
3305 if (!ret.empty ())
3306 {
3307 /* We are only interested in checking one element. */
3308 probe *p = ret[0];
3309
3310 if (!p->can_evaluate_arguments ())
3311 {
3312 /* We cannot use the probe interface here,
3313 because it does not know how to evaluate
3314 arguments. */
3315 ret.clear ();
3316 }
3317 }
3318 bp_objfile_data->longjmp_probes = ret;
3319 bp_objfile_data->longjmp_searched = 1;
3320 }
3321
3322 if (!bp_objfile_data->longjmp_probes.empty ())
3323 {
3324 for (probe *p : bp_objfile_data->longjmp_probes)
3325 {
3326 struct breakpoint *b;
3327
3328 b = create_internal_breakpoint (gdbarch,
3329 p->get_relocated_address (objfile),
3330 bp_longjmp_master,
3331 &internal_breakpoint_ops);
3332 b->location = new_probe_location ("-probe-stap libc:longjmp");
3333 b->enable_state = bp_disabled;
3334 }
3335
3336 continue;
3337 }
3338
3339 if (!gdbarch_get_longjmp_target_p (gdbarch))
3340 continue;
3341
3342 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3343 {
3344 struct breakpoint *b;
3345 const char *func_name;
3346 CORE_ADDR addr;
3347 struct explicit_location explicit_loc;
3348
3349 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3350 continue;
3351
3352 func_name = longjmp_names[i];
3353 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3354 {
3355 struct bound_minimal_symbol m;
3356
3357 m = lookup_minimal_symbol_text (func_name, objfile);
3358 if (m.minsym == NULL)
3359 {
3360 /* Prevent future lookups in this objfile. */
3361 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3362 continue;
3363 }
3364 bp_objfile_data->longjmp_msym[i] = m;
3365 }
3366
3367 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3368 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3369 &internal_breakpoint_ops);
3370 initialize_explicit_location (&explicit_loc);
3371 explicit_loc.function_name = ASTRDUP (func_name);
3372 b->location = new_explicit_location (&explicit_loc);
3373 b->enable_state = bp_disabled;
3374 }
3375 }
3376 }
3377 }
3378
3379 /* Create a master std::terminate breakpoint. */
3380 static void
3381 create_std_terminate_master_breakpoint (void)
3382 {
3383 const char *const func_name = "std::terminate()";
3384
3385 scoped_restore_current_program_space restore_pspace;
3386
3387 for (struct program_space *pspace : program_spaces)
3388 {
3389 CORE_ADDR addr;
3390
3391 set_current_program_space (pspace);
3392
3393 for (objfile *objfile : current_program_space->objfiles ())
3394 {
3395 struct breakpoint *b;
3396 struct breakpoint_objfile_data *bp_objfile_data;
3397 struct explicit_location explicit_loc;
3398
3399 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3400
3401 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3402 continue;
3403
3404 if (bp_objfile_data->terminate_msym.minsym == NULL)
3405 {
3406 struct bound_minimal_symbol m;
3407
3408 m = lookup_minimal_symbol (func_name, NULL, objfile);
3409 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3410 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3411 {
3412 /* Prevent future lookups in this objfile. */
3413 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3414 continue;
3415 }
3416 bp_objfile_data->terminate_msym = m;
3417 }
3418
3419 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3420 b = create_internal_breakpoint (objfile->arch (), addr,
3421 bp_std_terminate_master,
3422 &internal_breakpoint_ops);
3423 initialize_explicit_location (&explicit_loc);
3424 explicit_loc.function_name = ASTRDUP (func_name);
3425 b->location = new_explicit_location (&explicit_loc);
3426 b->enable_state = bp_disabled;
3427 }
3428 }
3429 }
3430
3431 /* Install a master breakpoint on the unwinder's debug hook. */
3432
3433 static void
3434 create_exception_master_breakpoint (void)
3435 {
3436 const char *const func_name = "_Unwind_DebugHook";
3437
3438 for (objfile *objfile : current_program_space->objfiles ())
3439 {
3440 struct breakpoint *b;
3441 struct gdbarch *gdbarch;
3442 struct breakpoint_objfile_data *bp_objfile_data;
3443 CORE_ADDR addr;
3444 struct explicit_location explicit_loc;
3445
3446 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3447
3448 /* We prefer the SystemTap probe point if it exists. */
3449 if (!bp_objfile_data->exception_searched)
3450 {
3451 std::vector<probe *> ret
3452 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3453
3454 if (!ret.empty ())
3455 {
3456 /* We are only interested in checking one element. */
3457 probe *p = ret[0];
3458
3459 if (!p->can_evaluate_arguments ())
3460 {
3461 /* We cannot use the probe interface here, because it does
3462 not know how to evaluate arguments. */
3463 ret.clear ();
3464 }
3465 }
3466 bp_objfile_data->exception_probes = ret;
3467 bp_objfile_data->exception_searched = 1;
3468 }
3469
3470 if (!bp_objfile_data->exception_probes.empty ())
3471 {
3472 gdbarch = objfile->arch ();
3473
3474 for (probe *p : bp_objfile_data->exception_probes)
3475 {
3476 b = create_internal_breakpoint (gdbarch,
3477 p->get_relocated_address (objfile),
3478 bp_exception_master,
3479 &internal_breakpoint_ops);
3480 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3481 b->enable_state = bp_disabled;
3482 }
3483
3484 continue;
3485 }
3486
3487 /* Otherwise, try the hook function. */
3488
3489 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3490 continue;
3491
3492 gdbarch = objfile->arch ();
3493
3494 if (bp_objfile_data->exception_msym.minsym == NULL)
3495 {
3496 struct bound_minimal_symbol debug_hook;
3497
3498 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3499 if (debug_hook.minsym == NULL)
3500 {
3501 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3502 continue;
3503 }
3504
3505 bp_objfile_data->exception_msym = debug_hook;
3506 }
3507
3508 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3509 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3510 current_top_target ());
3511 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3512 &internal_breakpoint_ops);
3513 initialize_explicit_location (&explicit_loc);
3514 explicit_loc.function_name = ASTRDUP (func_name);
3515 b->location = new_explicit_location (&explicit_loc);
3516 b->enable_state = bp_disabled;
3517 }
3518 }
3519
3520 /* Does B have a location spec? */
3521
3522 static int
3523 breakpoint_event_location_empty_p (const struct breakpoint *b)
3524 {
3525 return b->location != NULL && event_location_empty_p (b->location.get ());
3526 }
3527
3528 void
3529 update_breakpoints_after_exec (void)
3530 {
3531 struct breakpoint *b, *b_tmp;
3532 struct bp_location *bploc, **bplocp_tmp;
3533
3534 /* We're about to delete breakpoints from GDB's lists. If the
3535 INSERTED flag is true, GDB will try to lift the breakpoints by
3536 writing the breakpoints' "shadow contents" back into memory. The
3537 "shadow contents" are NOT valid after an exec, so GDB should not
3538 do that. Instead, the target is responsible from marking
3539 breakpoints out as soon as it detects an exec. We don't do that
3540 here instead, because there may be other attempts to delete
3541 breakpoints after detecting an exec and before reaching here. */
3542 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3543 if (bploc->pspace == current_program_space)
3544 gdb_assert (!bploc->inserted);
3545
3546 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3547 {
3548 if (b->pspace != current_program_space)
3549 continue;
3550
3551 /* Solib breakpoints must be explicitly reset after an exec(). */
3552 if (b->type == bp_shlib_event)
3553 {
3554 delete_breakpoint (b);
3555 continue;
3556 }
3557
3558 /* JIT breakpoints must be explicitly reset after an exec(). */
3559 if (b->type == bp_jit_event)
3560 {
3561 delete_breakpoint (b);
3562 continue;
3563 }
3564
3565 /* Thread event breakpoints must be set anew after an exec(),
3566 as must overlay event and longjmp master breakpoints. */
3567 if (b->type == bp_thread_event || b->type == bp_overlay_event
3568 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3569 || b->type == bp_exception_master)
3570 {
3571 delete_breakpoint (b);
3572 continue;
3573 }
3574
3575 /* Step-resume breakpoints are meaningless after an exec(). */
3576 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3577 {
3578 delete_breakpoint (b);
3579 continue;
3580 }
3581
3582 /* Just like single-step breakpoints. */
3583 if (b->type == bp_single_step)
3584 {
3585 delete_breakpoint (b);
3586 continue;
3587 }
3588
3589 /* Longjmp and longjmp-resume breakpoints are also meaningless
3590 after an exec. */
3591 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3592 || b->type == bp_longjmp_call_dummy
3593 || b->type == bp_exception || b->type == bp_exception_resume)
3594 {
3595 delete_breakpoint (b);
3596 continue;
3597 }
3598
3599 if (b->type == bp_catchpoint)
3600 {
3601 /* For now, none of the bp_catchpoint breakpoints need to
3602 do anything at this point. In the future, if some of
3603 the catchpoints need to something, we will need to add
3604 a new method, and call this method from here. */
3605 continue;
3606 }
3607
3608 /* bp_finish is a special case. The only way we ought to be able
3609 to see one of these when an exec() has happened, is if the user
3610 caught a vfork, and then said "finish". Ordinarily a finish just
3611 carries them to the call-site of the current callee, by setting
3612 a temporary bp there and resuming. But in this case, the finish
3613 will carry them entirely through the vfork & exec.
3614
3615 We don't want to allow a bp_finish to remain inserted now. But
3616 we can't safely delete it, 'cause finish_command has a handle to
3617 the bp on a bpstat, and will later want to delete it. There's a
3618 chance (and I've seen it happen) that if we delete the bp_finish
3619 here, that its storage will get reused by the time finish_command
3620 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3621 We really must allow finish_command to delete a bp_finish.
3622
3623 In the absence of a general solution for the "how do we know
3624 it's safe to delete something others may have handles to?"
3625 problem, what we'll do here is just uninsert the bp_finish, and
3626 let finish_command delete it.
3627
3628 (We know the bp_finish is "doomed" in the sense that it's
3629 momentary, and will be deleted as soon as finish_command sees
3630 the inferior stopped. So it doesn't matter that the bp's
3631 address is probably bogus in the new a.out, unlike e.g., the
3632 solib breakpoints.) */
3633
3634 if (b->type == bp_finish)
3635 {
3636 continue;
3637 }
3638
3639 /* Without a symbolic address, we have little hope of the
3640 pre-exec() address meaning the same thing in the post-exec()
3641 a.out. */
3642 if (breakpoint_event_location_empty_p (b))
3643 {
3644 delete_breakpoint (b);
3645 continue;
3646 }
3647 }
3648 }
3649
3650 int
3651 detach_breakpoints (ptid_t ptid)
3652 {
3653 struct bp_location *bl, **blp_tmp;
3654 int val = 0;
3655 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3656 struct inferior *inf = current_inferior ();
3657
3658 if (ptid.pid () == inferior_ptid.pid ())
3659 error (_("Cannot detach breakpoints of inferior_ptid"));
3660
3661 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3662 inferior_ptid = ptid;
3663 ALL_BP_LOCATIONS (bl, blp_tmp)
3664 {
3665 if (bl->pspace != inf->pspace)
3666 continue;
3667
3668 /* This function must physically remove breakpoints locations
3669 from the specified ptid, without modifying the breakpoint
3670 package's state. Locations of type bp_loc_other are only
3671 maintained at GDB side. So, there is no need to remove
3672 these bp_loc_other locations. Moreover, removing these
3673 would modify the breakpoint package's state. */
3674 if (bl->loc_type == bp_loc_other)
3675 continue;
3676
3677 if (bl->inserted)
3678 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3679 }
3680
3681 return val;
3682 }
3683
3684 /* Remove the breakpoint location BL from the current address space.
3685 Note that this is used to detach breakpoints from a child fork.
3686 When we get here, the child isn't in the inferior list, and neither
3687 do we have objects to represent its address space --- we should
3688 *not* look at bl->pspace->aspace here. */
3689
3690 static int
3691 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3692 {
3693 int val;
3694
3695 /* BL is never in moribund_locations by our callers. */
3696 gdb_assert (bl->owner != NULL);
3697
3698 /* The type of none suggests that owner is actually deleted.
3699 This should not ever happen. */
3700 gdb_assert (bl->owner->type != bp_none);
3701
3702 if (bl->loc_type == bp_loc_software_breakpoint
3703 || bl->loc_type == bp_loc_hardware_breakpoint)
3704 {
3705 /* "Normal" instruction breakpoint: either the standard
3706 trap-instruction bp (bp_breakpoint), or a
3707 bp_hardware_breakpoint. */
3708
3709 /* First check to see if we have to handle an overlay. */
3710 if (overlay_debugging == ovly_off
3711 || bl->section == NULL
3712 || !(section_is_overlay (bl->section)))
3713 {
3714 /* No overlay handling: just remove the breakpoint. */
3715
3716 /* If we're trying to uninsert a memory breakpoint that we
3717 know is set in a dynamic object that is marked
3718 shlib_disabled, then either the dynamic object was
3719 removed with "remove-symbol-file" or with
3720 "nosharedlibrary". In the former case, we don't know
3721 whether another dynamic object might have loaded over the
3722 breakpoint's address -- the user might well let us know
3723 about it next with add-symbol-file (the whole point of
3724 add-symbol-file is letting the user manually maintain a
3725 list of dynamically loaded objects). If we have the
3726 breakpoint's shadow memory, that is, this is a software
3727 breakpoint managed by GDB, check whether the breakpoint
3728 is still inserted in memory, to avoid overwriting wrong
3729 code with stale saved shadow contents. Note that HW
3730 breakpoints don't have shadow memory, as they're
3731 implemented using a mechanism that is not dependent on
3732 being able to modify the target's memory, and as such
3733 they should always be removed. */
3734 if (bl->shlib_disabled
3735 && bl->target_info.shadow_len != 0
3736 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3737 val = 0;
3738 else
3739 val = bl->owner->ops->remove_location (bl, reason);
3740 }
3741 else
3742 {
3743 /* This breakpoint is in an overlay section.
3744 Did we set a breakpoint at the LMA? */
3745 if (!overlay_events_enabled)
3746 {
3747 /* Yes -- overlay event support is not active, so we
3748 should have set a breakpoint at the LMA. Remove it.
3749 */
3750 /* Ignore any failures: if the LMA is in ROM, we will
3751 have already warned when we failed to insert it. */
3752 if (bl->loc_type == bp_loc_hardware_breakpoint)
3753 target_remove_hw_breakpoint (bl->gdbarch,
3754 &bl->overlay_target_info);
3755 else
3756 target_remove_breakpoint (bl->gdbarch,
3757 &bl->overlay_target_info,
3758 reason);
3759 }
3760 /* Did we set a breakpoint at the VMA?
3761 If so, we will have marked the breakpoint 'inserted'. */
3762 if (bl->inserted)
3763 {
3764 /* Yes -- remove it. Previously we did not bother to
3765 remove the breakpoint if the section had been
3766 unmapped, but let's not rely on that being safe. We
3767 don't know what the overlay manager might do. */
3768
3769 /* However, we should remove *software* breakpoints only
3770 if the section is still mapped, or else we overwrite
3771 wrong code with the saved shadow contents. */
3772 if (bl->loc_type == bp_loc_hardware_breakpoint
3773 || section_is_mapped (bl->section))
3774 val = bl->owner->ops->remove_location (bl, reason);
3775 else
3776 val = 0;
3777 }
3778 else
3779 {
3780 /* No -- not inserted, so no need to remove. No error. */
3781 val = 0;
3782 }
3783 }
3784
3785 /* In some cases, we might not be able to remove a breakpoint in
3786 a shared library that has already been removed, but we have
3787 not yet processed the shlib unload event. Similarly for an
3788 unloaded add-symbol-file object - the user might not yet have
3789 had the chance to remove-symbol-file it. shlib_disabled will
3790 be set if the library/object has already been removed, but
3791 the breakpoint hasn't been uninserted yet, e.g., after
3792 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3793 always-inserted mode. */
3794 if (val
3795 && (bl->loc_type == bp_loc_software_breakpoint
3796 && (bl->shlib_disabled
3797 || solib_name_from_address (bl->pspace, bl->address)
3798 || shared_objfile_contains_address_p (bl->pspace,
3799 bl->address))))
3800 val = 0;
3801
3802 if (val)
3803 return val;
3804 bl->inserted = (reason == DETACH_BREAKPOINT);
3805 }
3806 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3807 {
3808 gdb_assert (bl->owner->ops != NULL
3809 && bl->owner->ops->remove_location != NULL);
3810
3811 bl->inserted = (reason == DETACH_BREAKPOINT);
3812 bl->owner->ops->remove_location (bl, reason);
3813
3814 /* Failure to remove any of the hardware watchpoints comes here. */
3815 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3816 warning (_("Could not remove hardware watchpoint %d."),
3817 bl->owner->number);
3818 }
3819 else if (bl->owner->type == bp_catchpoint
3820 && breakpoint_enabled (bl->owner)
3821 && !bl->duplicate)
3822 {
3823 gdb_assert (bl->owner->ops != NULL
3824 && bl->owner->ops->remove_location != NULL);
3825
3826 val = bl->owner->ops->remove_location (bl, reason);
3827 if (val)
3828 return val;
3829
3830 bl->inserted = (reason == DETACH_BREAKPOINT);
3831 }
3832
3833 return 0;
3834 }
3835
3836 static int
3837 remove_breakpoint (struct bp_location *bl)
3838 {
3839 /* BL is never in moribund_locations by our callers. */
3840 gdb_assert (bl->owner != NULL);
3841
3842 /* The type of none suggests that owner is actually deleted.
3843 This should not ever happen. */
3844 gdb_assert (bl->owner->type != bp_none);
3845
3846 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3847
3848 switch_to_program_space_and_thread (bl->pspace);
3849
3850 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
3851 }
3852
3853 /* Clear the "inserted" flag in all breakpoints. */
3854
3855 void
3856 mark_breakpoints_out (void)
3857 {
3858 struct bp_location *bl, **blp_tmp;
3859
3860 ALL_BP_LOCATIONS (bl, blp_tmp)
3861 if (bl->pspace == current_program_space)
3862 bl->inserted = 0;
3863 }
3864
3865 /* Clear the "inserted" flag in all breakpoints and delete any
3866 breakpoints which should go away between runs of the program.
3867
3868 Plus other such housekeeping that has to be done for breakpoints
3869 between runs.
3870
3871 Note: this function gets called at the end of a run (by
3872 generic_mourn_inferior) and when a run begins (by
3873 init_wait_for_inferior). */
3874
3875
3876
3877 void
3878 breakpoint_init_inferior (enum inf_context context)
3879 {
3880 struct breakpoint *b, *b_tmp;
3881 struct program_space *pspace = current_program_space;
3882
3883 /* If breakpoint locations are shared across processes, then there's
3884 nothing to do. */
3885 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
3886 return;
3887
3888 mark_breakpoints_out ();
3889
3890 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3891 {
3892 if (b->loc && b->loc->pspace != pspace)
3893 continue;
3894
3895 switch (b->type)
3896 {
3897 case bp_call_dummy:
3898 case bp_longjmp_call_dummy:
3899
3900 /* If the call dummy breakpoint is at the entry point it will
3901 cause problems when the inferior is rerun, so we better get
3902 rid of it. */
3903
3904 case bp_watchpoint_scope:
3905
3906 /* Also get rid of scope breakpoints. */
3907
3908 case bp_shlib_event:
3909
3910 /* Also remove solib event breakpoints. Their addresses may
3911 have changed since the last time we ran the program.
3912 Actually we may now be debugging against different target;
3913 and so the solib backend that installed this breakpoint may
3914 not be used in by the target. E.g.,
3915
3916 (gdb) file prog-linux
3917 (gdb) run # native linux target
3918 ...
3919 (gdb) kill
3920 (gdb) file prog-win.exe
3921 (gdb) tar rem :9999 # remote Windows gdbserver.
3922 */
3923
3924 case bp_step_resume:
3925
3926 /* Also remove step-resume breakpoints. */
3927
3928 case bp_single_step:
3929
3930 /* Also remove single-step breakpoints. */
3931
3932 delete_breakpoint (b);
3933 break;
3934
3935 case bp_watchpoint:
3936 case bp_hardware_watchpoint:
3937 case bp_read_watchpoint:
3938 case bp_access_watchpoint:
3939 {
3940 struct watchpoint *w = (struct watchpoint *) b;
3941
3942 /* Likewise for watchpoints on local expressions. */
3943 if (w->exp_valid_block != NULL)
3944 delete_breakpoint (b);
3945 else
3946 {
3947 /* Get rid of existing locations, which are no longer
3948 valid. New ones will be created in
3949 update_watchpoint, when the inferior is restarted.
3950 The next update_global_location_list call will
3951 garbage collect them. */
3952 b->loc = NULL;
3953
3954 if (context == inf_starting)
3955 {
3956 /* Reset val field to force reread of starting value in
3957 insert_breakpoints. */
3958 w->val.reset (nullptr);
3959 w->val_valid = false;
3960 }
3961 }
3962 }
3963 break;
3964 default:
3965 break;
3966 }
3967 }
3968
3969 /* Get rid of the moribund locations. */
3970 for (bp_location *bl : moribund_locations)
3971 decref_bp_location (&bl);
3972 moribund_locations.clear ();
3973 }
3974
3975 /* These functions concern about actual breakpoints inserted in the
3976 target --- to e.g. check if we need to do decr_pc adjustment or if
3977 we need to hop over the bkpt --- so we check for address space
3978 match, not program space. */
3979
3980 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3981 exists at PC. It returns ordinary_breakpoint_here if it's an
3982 ordinary breakpoint, or permanent_breakpoint_here if it's a
3983 permanent breakpoint.
3984 - When continuing from a location with an ordinary breakpoint, we
3985 actually single step once before calling insert_breakpoints.
3986 - When continuing from a location with a permanent breakpoint, we
3987 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3988 the target, to advance the PC past the breakpoint. */
3989
3990 enum breakpoint_here
3991 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
3992 {
3993 struct bp_location *bl, **blp_tmp;
3994 int any_breakpoint_here = 0;
3995
3996 ALL_BP_LOCATIONS (bl, blp_tmp)
3997 {
3998 if (bl->loc_type != bp_loc_software_breakpoint
3999 && bl->loc_type != bp_loc_hardware_breakpoint)
4000 continue;
4001
4002 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4003 if ((breakpoint_enabled (bl->owner)
4004 || bl->permanent)
4005 && breakpoint_location_address_match (bl, aspace, pc))
4006 {
4007 if (overlay_debugging
4008 && section_is_overlay (bl->section)
4009 && !section_is_mapped (bl->section))
4010 continue; /* unmapped overlay -- can't be a match */
4011 else if (bl->permanent)
4012 return permanent_breakpoint_here;
4013 else
4014 any_breakpoint_here = 1;
4015 }
4016 }
4017
4018 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4019 }
4020
4021 /* See breakpoint.h. */
4022
4023 int
4024 breakpoint_in_range_p (const address_space *aspace,
4025 CORE_ADDR addr, ULONGEST len)
4026 {
4027 struct bp_location *bl, **blp_tmp;
4028
4029 ALL_BP_LOCATIONS (bl, blp_tmp)
4030 {
4031 if (bl->loc_type != bp_loc_software_breakpoint
4032 && bl->loc_type != bp_loc_hardware_breakpoint)
4033 continue;
4034
4035 if ((breakpoint_enabled (bl->owner)
4036 || bl->permanent)
4037 && breakpoint_location_address_range_overlap (bl, aspace,
4038 addr, len))
4039 {
4040 if (overlay_debugging
4041 && section_is_overlay (bl->section)
4042 && !section_is_mapped (bl->section))
4043 {
4044 /* Unmapped overlay -- can't be a match. */
4045 continue;
4046 }
4047
4048 return 1;
4049 }
4050 }
4051
4052 return 0;
4053 }
4054
4055 /* Return true if there's a moribund breakpoint at PC. */
4056
4057 int
4058 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4059 {
4060 for (bp_location *loc : moribund_locations)
4061 if (breakpoint_location_address_match (loc, aspace, pc))
4062 return 1;
4063
4064 return 0;
4065 }
4066
4067 /* Returns non-zero iff BL is inserted at PC, in address space
4068 ASPACE. */
4069
4070 static int
4071 bp_location_inserted_here_p (struct bp_location *bl,
4072 const address_space *aspace, CORE_ADDR pc)
4073 {
4074 if (bl->inserted
4075 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4076 aspace, pc))
4077 {
4078 if (overlay_debugging
4079 && section_is_overlay (bl->section)
4080 && !section_is_mapped (bl->section))
4081 return 0; /* unmapped overlay -- can't be a match */
4082 else
4083 return 1;
4084 }
4085 return 0;
4086 }
4087
4088 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4089
4090 int
4091 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4092 {
4093 struct bp_location **blp, **blp_tmp = NULL;
4094
4095 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4096 {
4097 struct bp_location *bl = *blp;
4098
4099 if (bl->loc_type != bp_loc_software_breakpoint
4100 && bl->loc_type != bp_loc_hardware_breakpoint)
4101 continue;
4102
4103 if (bp_location_inserted_here_p (bl, aspace, pc))
4104 return 1;
4105 }
4106 return 0;
4107 }
4108
4109 /* This function returns non-zero iff there is a software breakpoint
4110 inserted at PC. */
4111
4112 int
4113 software_breakpoint_inserted_here_p (const address_space *aspace,
4114 CORE_ADDR pc)
4115 {
4116 struct bp_location **blp, **blp_tmp = NULL;
4117
4118 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4119 {
4120 struct bp_location *bl = *blp;
4121
4122 if (bl->loc_type != bp_loc_software_breakpoint)
4123 continue;
4124
4125 if (bp_location_inserted_here_p (bl, aspace, pc))
4126 return 1;
4127 }
4128
4129 return 0;
4130 }
4131
4132 /* See breakpoint.h. */
4133
4134 int
4135 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4136 CORE_ADDR pc)
4137 {
4138 struct bp_location **blp, **blp_tmp = NULL;
4139
4140 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4141 {
4142 struct bp_location *bl = *blp;
4143
4144 if (bl->loc_type != bp_loc_hardware_breakpoint)
4145 continue;
4146
4147 if (bp_location_inserted_here_p (bl, aspace, pc))
4148 return 1;
4149 }
4150
4151 return 0;
4152 }
4153
4154 int
4155 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4156 CORE_ADDR addr, ULONGEST len)
4157 {
4158 struct breakpoint *bpt;
4159
4160 ALL_BREAKPOINTS (bpt)
4161 {
4162 struct bp_location *loc;
4163
4164 if (bpt->type != bp_hardware_watchpoint
4165 && bpt->type != bp_access_watchpoint)
4166 continue;
4167
4168 if (!breakpoint_enabled (bpt))
4169 continue;
4170
4171 for (loc = bpt->loc; loc; loc = loc->next)
4172 if (loc->pspace->aspace == aspace && loc->inserted)
4173 {
4174 CORE_ADDR l, h;
4175
4176 /* Check for intersection. */
4177 l = std::max<CORE_ADDR> (loc->address, addr);
4178 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4179 if (l < h)
4180 return 1;
4181 }
4182 }
4183 return 0;
4184 }
4185
4186 /* See breakpoint.h. */
4187
4188 bool
4189 is_catchpoint (struct breakpoint *b)
4190 {
4191 return (b->type == bp_catchpoint);
4192 }
4193
4194 /* Frees any storage that is part of a bpstat. Does not walk the
4195 'next' chain. */
4196
4197 bpstats::~bpstats ()
4198 {
4199 if (bp_location_at != NULL)
4200 decref_bp_location (&bp_location_at);
4201 }
4202
4203 /* Clear a bpstat so that it says we are not at any breakpoint.
4204 Also free any storage that is part of a bpstat. */
4205
4206 void
4207 bpstat_clear (bpstat *bsp)
4208 {
4209 bpstat p;
4210 bpstat q;
4211
4212 if (bsp == 0)
4213 return;
4214 p = *bsp;
4215 while (p != NULL)
4216 {
4217 q = p->next;
4218 delete p;
4219 p = q;
4220 }
4221 *bsp = NULL;
4222 }
4223
4224 bpstats::bpstats (const bpstats &other)
4225 : next (NULL),
4226 bp_location_at (other.bp_location_at),
4227 breakpoint_at (other.breakpoint_at),
4228 commands (other.commands),
4229 print (other.print),
4230 stop (other.stop),
4231 print_it (other.print_it)
4232 {
4233 if (other.old_val != NULL)
4234 old_val = release_value (value_copy (other.old_val.get ()));
4235 incref_bp_location (bp_location_at);
4236 }
4237
4238 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4239 is part of the bpstat is copied as well. */
4240
4241 bpstat
4242 bpstat_copy (bpstat bs)
4243 {
4244 bpstat p = NULL;
4245 bpstat tmp;
4246 bpstat retval = NULL;
4247
4248 if (bs == NULL)
4249 return bs;
4250
4251 for (; bs != NULL; bs = bs->next)
4252 {
4253 tmp = new bpstats (*bs);
4254
4255 if (p == NULL)
4256 /* This is the first thing in the chain. */
4257 retval = tmp;
4258 else
4259 p->next = tmp;
4260 p = tmp;
4261 }
4262 p->next = NULL;
4263 return retval;
4264 }
4265
4266 /* Find the bpstat associated with this breakpoint. */
4267
4268 bpstat
4269 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4270 {
4271 if (bsp == NULL)
4272 return NULL;
4273
4274 for (; bsp != NULL; bsp = bsp->next)
4275 {
4276 if (bsp->breakpoint_at == breakpoint)
4277 return bsp;
4278 }
4279 return NULL;
4280 }
4281
4282 /* See breakpoint.h. */
4283
4284 bool
4285 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4286 {
4287 for (; bsp != NULL; bsp = bsp->next)
4288 {
4289 if (bsp->breakpoint_at == NULL)
4290 {
4291 /* A moribund location can never explain a signal other than
4292 GDB_SIGNAL_TRAP. */
4293 if (sig == GDB_SIGNAL_TRAP)
4294 return true;
4295 }
4296 else
4297 {
4298 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4299 sig))
4300 return true;
4301 }
4302 }
4303
4304 return false;
4305 }
4306
4307 /* Put in *NUM the breakpoint number of the first breakpoint we are
4308 stopped at. *BSP upon return is a bpstat which points to the
4309 remaining breakpoints stopped at (but which is not guaranteed to be
4310 good for anything but further calls to bpstat_num).
4311
4312 Return 0 if passed a bpstat which does not indicate any breakpoints.
4313 Return -1 if stopped at a breakpoint that has been deleted since
4314 we set it.
4315 Return 1 otherwise. */
4316
4317 int
4318 bpstat_num (bpstat *bsp, int *num)
4319 {
4320 struct breakpoint *b;
4321
4322 if ((*bsp) == NULL)
4323 return 0; /* No more breakpoint values */
4324
4325 /* We assume we'll never have several bpstats that correspond to a
4326 single breakpoint -- otherwise, this function might return the
4327 same number more than once and this will look ugly. */
4328 b = (*bsp)->breakpoint_at;
4329 *bsp = (*bsp)->next;
4330 if (b == NULL)
4331 return -1; /* breakpoint that's been deleted since */
4332
4333 *num = b->number; /* We have its number */
4334 return 1;
4335 }
4336
4337 /* See breakpoint.h. */
4338
4339 void
4340 bpstat_clear_actions (void)
4341 {
4342 bpstat bs;
4343
4344 if (inferior_ptid == null_ptid)
4345 return;
4346
4347 thread_info *tp = inferior_thread ();
4348 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4349 {
4350 bs->commands = NULL;
4351 bs->old_val.reset (nullptr);
4352 }
4353 }
4354
4355 /* Called when a command is about to proceed the inferior. */
4356
4357 static void
4358 breakpoint_about_to_proceed (void)
4359 {
4360 if (inferior_ptid != null_ptid)
4361 {
4362 struct thread_info *tp = inferior_thread ();
4363
4364 /* Allow inferior function calls in breakpoint commands to not
4365 interrupt the command list. When the call finishes
4366 successfully, the inferior will be standing at the same
4367 breakpoint as if nothing happened. */
4368 if (tp->control.in_infcall)
4369 return;
4370 }
4371
4372 breakpoint_proceeded = 1;
4373 }
4374
4375 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4376 or its equivalent. */
4377
4378 static int
4379 command_line_is_silent (struct command_line *cmd)
4380 {
4381 return cmd && (strcmp ("silent", cmd->line) == 0);
4382 }
4383
4384 /* Execute all the commands associated with all the breakpoints at
4385 this location. Any of these commands could cause the process to
4386 proceed beyond this point, etc. We look out for such changes by
4387 checking the global "breakpoint_proceeded" after each command.
4388
4389 Returns true if a breakpoint command resumed the inferior. In that
4390 case, it is the caller's responsibility to recall it again with the
4391 bpstat of the current thread. */
4392
4393 static int
4394 bpstat_do_actions_1 (bpstat *bsp)
4395 {
4396 bpstat bs;
4397 int again = 0;
4398
4399 /* Avoid endless recursion if a `source' command is contained
4400 in bs->commands. */
4401 if (executing_breakpoint_commands)
4402 return 0;
4403
4404 scoped_restore save_executing
4405 = make_scoped_restore (&executing_breakpoint_commands, 1);
4406
4407 scoped_restore preventer = prevent_dont_repeat ();
4408
4409 /* This pointer will iterate over the list of bpstat's. */
4410 bs = *bsp;
4411
4412 breakpoint_proceeded = 0;
4413 for (; bs != NULL; bs = bs->next)
4414 {
4415 struct command_line *cmd = NULL;
4416
4417 /* Take ownership of the BSP's command tree, if it has one.
4418
4419 The command tree could legitimately contain commands like
4420 'step' and 'next', which call clear_proceed_status, which
4421 frees stop_bpstat's command tree. To make sure this doesn't
4422 free the tree we're executing out from under us, we need to
4423 take ownership of the tree ourselves. Since a given bpstat's
4424 commands are only executed once, we don't need to copy it; we
4425 can clear the pointer in the bpstat, and make sure we free
4426 the tree when we're done. */
4427 counted_command_line ccmd = bs->commands;
4428 bs->commands = NULL;
4429 if (ccmd != NULL)
4430 cmd = ccmd.get ();
4431 if (command_line_is_silent (cmd))
4432 {
4433 /* The action has been already done by bpstat_stop_status. */
4434 cmd = cmd->next;
4435 }
4436
4437 while (cmd != NULL)
4438 {
4439 execute_control_command (cmd);
4440
4441 if (breakpoint_proceeded)
4442 break;
4443 else
4444 cmd = cmd->next;
4445 }
4446
4447 if (breakpoint_proceeded)
4448 {
4449 if (current_ui->async)
4450 /* If we are in async mode, then the target might be still
4451 running, not stopped at any breakpoint, so nothing for
4452 us to do here -- just return to the event loop. */
4453 ;
4454 else
4455 /* In sync mode, when execute_control_command returns
4456 we're already standing on the next breakpoint.
4457 Breakpoint commands for that stop were not run, since
4458 execute_command does not run breakpoint commands --
4459 only command_line_handler does, but that one is not
4460 involved in execution of breakpoint commands. So, we
4461 can now execute breakpoint commands. It should be
4462 noted that making execute_command do bpstat actions is
4463 not an option -- in this case we'll have recursive
4464 invocation of bpstat for each breakpoint with a
4465 command, and can easily blow up GDB stack. Instead, we
4466 return true, which will trigger the caller to recall us
4467 with the new stop_bpstat. */
4468 again = 1;
4469 break;
4470 }
4471 }
4472 return again;
4473 }
4474
4475 /* Helper for bpstat_do_actions. Get the current thread, if there's
4476 one, is alive and has execution. Return NULL otherwise. */
4477
4478 static thread_info *
4479 get_bpstat_thread ()
4480 {
4481 if (inferior_ptid == null_ptid || !target_has_execution ())
4482 return NULL;
4483
4484 thread_info *tp = inferior_thread ();
4485 if (tp->state == THREAD_EXITED || tp->executing)
4486 return NULL;
4487 return tp;
4488 }
4489
4490 void
4491 bpstat_do_actions (void)
4492 {
4493 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4494 thread_info *tp;
4495
4496 /* Do any commands attached to breakpoint we are stopped at. */
4497 while ((tp = get_bpstat_thread ()) != NULL)
4498 {
4499 /* Since in sync mode, bpstat_do_actions may resume the
4500 inferior, and only return when it is stopped at the next
4501 breakpoint, we keep doing breakpoint actions until it returns
4502 false to indicate the inferior was not resumed. */
4503 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4504 break;
4505 }
4506
4507 cleanup_if_error.release ();
4508 }
4509
4510 /* Print out the (old or new) value associated with a watchpoint. */
4511
4512 static void
4513 watchpoint_value_print (struct value *val, struct ui_file *stream)
4514 {
4515 if (val == NULL)
4516 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4517 else
4518 {
4519 struct value_print_options opts;
4520 get_user_print_options (&opts);
4521 value_print (val, stream, &opts);
4522 }
4523 }
4524
4525 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4526 debugging multiple threads. */
4527
4528 void
4529 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4530 {
4531 if (uiout->is_mi_like_p ())
4532 return;
4533
4534 uiout->text ("\n");
4535
4536 if (show_thread_that_caused_stop ())
4537 {
4538 const char *name;
4539 struct thread_info *thr = inferior_thread ();
4540
4541 uiout->text ("Thread ");
4542 uiout->field_string ("thread-id", print_thread_id (thr));
4543
4544 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4545 if (name != NULL)
4546 {
4547 uiout->text (" \"");
4548 uiout->field_string ("name", name);
4549 uiout->text ("\"");
4550 }
4551
4552 uiout->text (" hit ");
4553 }
4554 }
4555
4556 /* Generic routine for printing messages indicating why we
4557 stopped. The behavior of this function depends on the value
4558 'print_it' in the bpstat structure. Under some circumstances we
4559 may decide not to print anything here and delegate the task to
4560 normal_stop(). */
4561
4562 static enum print_stop_action
4563 print_bp_stop_message (bpstat bs)
4564 {
4565 switch (bs->print_it)
4566 {
4567 case print_it_noop:
4568 /* Nothing should be printed for this bpstat entry. */
4569 return PRINT_UNKNOWN;
4570 break;
4571
4572 case print_it_done:
4573 /* We still want to print the frame, but we already printed the
4574 relevant messages. */
4575 return PRINT_SRC_AND_LOC;
4576 break;
4577
4578 case print_it_normal:
4579 {
4580 struct breakpoint *b = bs->breakpoint_at;
4581
4582 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4583 which has since been deleted. */
4584 if (b == NULL)
4585 return PRINT_UNKNOWN;
4586
4587 /* Normal case. Call the breakpoint's print_it method. */
4588 return b->ops->print_it (bs);
4589 }
4590 break;
4591
4592 default:
4593 internal_error (__FILE__, __LINE__,
4594 _("print_bp_stop_message: unrecognized enum value"));
4595 break;
4596 }
4597 }
4598
4599 /* A helper function that prints a shared library stopped event. */
4600
4601 static void
4602 print_solib_event (int is_catchpoint)
4603 {
4604 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4605 bool any_added = !current_program_space->added_solibs.empty ();
4606
4607 if (!is_catchpoint)
4608 {
4609 if (any_added || any_deleted)
4610 current_uiout->text (_("Stopped due to shared library event:\n"));
4611 else
4612 current_uiout->text (_("Stopped due to shared library event (no "
4613 "libraries added or removed)\n"));
4614 }
4615
4616 if (current_uiout->is_mi_like_p ())
4617 current_uiout->field_string ("reason",
4618 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4619
4620 if (any_deleted)
4621 {
4622 current_uiout->text (_(" Inferior unloaded "));
4623 ui_out_emit_list list_emitter (current_uiout, "removed");
4624 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4625 {
4626 const std::string &name = current_program_space->deleted_solibs[ix];
4627
4628 if (ix > 0)
4629 current_uiout->text (" ");
4630 current_uiout->field_string ("library", name);
4631 current_uiout->text ("\n");
4632 }
4633 }
4634
4635 if (any_added)
4636 {
4637 current_uiout->text (_(" Inferior loaded "));
4638 ui_out_emit_list list_emitter (current_uiout, "added");
4639 bool first = true;
4640 for (so_list *iter : current_program_space->added_solibs)
4641 {
4642 if (!first)
4643 current_uiout->text (" ");
4644 first = false;
4645 current_uiout->field_string ("library", iter->so_name);
4646 current_uiout->text ("\n");
4647 }
4648 }
4649 }
4650
4651 /* Print a message indicating what happened. This is called from
4652 normal_stop(). The input to this routine is the head of the bpstat
4653 list - a list of the eventpoints that caused this stop. KIND is
4654 the target_waitkind for the stopping event. This
4655 routine calls the generic print routine for printing a message
4656 about reasons for stopping. This will print (for example) the
4657 "Breakpoint n," part of the output. The return value of this
4658 routine is one of:
4659
4660 PRINT_UNKNOWN: Means we printed nothing.
4661 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4662 code to print the location. An example is
4663 "Breakpoint 1, " which should be followed by
4664 the location.
4665 PRINT_SRC_ONLY: Means we printed something, but there is no need
4666 to also print the location part of the message.
4667 An example is the catch/throw messages, which
4668 don't require a location appended to the end.
4669 PRINT_NOTHING: We have done some printing and we don't need any
4670 further info to be printed. */
4671
4672 enum print_stop_action
4673 bpstat_print (bpstat bs, int kind)
4674 {
4675 enum print_stop_action val;
4676
4677 /* Maybe another breakpoint in the chain caused us to stop.
4678 (Currently all watchpoints go on the bpstat whether hit or not.
4679 That probably could (should) be changed, provided care is taken
4680 with respect to bpstat_explains_signal). */
4681 for (; bs; bs = bs->next)
4682 {
4683 val = print_bp_stop_message (bs);
4684 if (val == PRINT_SRC_ONLY
4685 || val == PRINT_SRC_AND_LOC
4686 || val == PRINT_NOTHING)
4687 return val;
4688 }
4689
4690 /* If we had hit a shared library event breakpoint,
4691 print_bp_stop_message would print out this message. If we hit an
4692 OS-level shared library event, do the same thing. */
4693 if (kind == TARGET_WAITKIND_LOADED)
4694 {
4695 print_solib_event (0);
4696 return PRINT_NOTHING;
4697 }
4698
4699 /* We reached the end of the chain, or we got a null BS to start
4700 with and nothing was printed. */
4701 return PRINT_UNKNOWN;
4702 }
4703
4704 /* Evaluate the boolean expression EXP and return the result. */
4705
4706 static bool
4707 breakpoint_cond_eval (expression *exp)
4708 {
4709 struct value *mark = value_mark ();
4710 bool res = value_true (evaluate_expression (exp));
4711
4712 value_free_to_mark (mark);
4713 return res;
4714 }
4715
4716 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4717
4718 bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4719 : next (NULL),
4720 bp_location_at (bl),
4721 breakpoint_at (bl->owner),
4722 commands (NULL),
4723 print (0),
4724 stop (0),
4725 print_it (print_it_normal)
4726 {
4727 incref_bp_location (bl);
4728 **bs_link_pointer = this;
4729 *bs_link_pointer = &next;
4730 }
4731
4732 bpstats::bpstats ()
4733 : next (NULL),
4734 bp_location_at (NULL),
4735 breakpoint_at (NULL),
4736 commands (NULL),
4737 print (0),
4738 stop (0),
4739 print_it (print_it_normal)
4740 {
4741 }
4742 \f
4743 /* The target has stopped with waitstatus WS. Check if any hardware
4744 watchpoints have triggered, according to the target. */
4745
4746 int
4747 watchpoints_triggered (struct target_waitstatus *ws)
4748 {
4749 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
4750 CORE_ADDR addr;
4751 struct breakpoint *b;
4752
4753 if (!stopped_by_watchpoint)
4754 {
4755 /* We were not stopped by a watchpoint. Mark all watchpoints
4756 as not triggered. */
4757 ALL_BREAKPOINTS (b)
4758 if (is_hardware_watchpoint (b))
4759 {
4760 struct watchpoint *w = (struct watchpoint *) b;
4761
4762 w->watchpoint_triggered = watch_triggered_no;
4763 }
4764
4765 return 0;
4766 }
4767
4768 if (!target_stopped_data_address (current_top_target (), &addr))
4769 {
4770 /* We were stopped by a watchpoint, but we don't know where.
4771 Mark all watchpoints as unknown. */
4772 ALL_BREAKPOINTS (b)
4773 if (is_hardware_watchpoint (b))
4774 {
4775 struct watchpoint *w = (struct watchpoint *) b;
4776
4777 w->watchpoint_triggered = watch_triggered_unknown;
4778 }
4779
4780 return 1;
4781 }
4782
4783 /* The target could report the data address. Mark watchpoints
4784 affected by this data address as triggered, and all others as not
4785 triggered. */
4786
4787 ALL_BREAKPOINTS (b)
4788 if (is_hardware_watchpoint (b))
4789 {
4790 struct watchpoint *w = (struct watchpoint *) b;
4791 struct bp_location *loc;
4792
4793 w->watchpoint_triggered = watch_triggered_no;
4794 for (loc = b->loc; loc; loc = loc->next)
4795 {
4796 if (is_masked_watchpoint (b))
4797 {
4798 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4799 CORE_ADDR start = loc->address & w->hw_wp_mask;
4800
4801 if (newaddr == start)
4802 {
4803 w->watchpoint_triggered = watch_triggered_yes;
4804 break;
4805 }
4806 }
4807 /* Exact match not required. Within range is sufficient. */
4808 else if (target_watchpoint_addr_within_range (current_top_target (),
4809 addr, loc->address,
4810 loc->length))
4811 {
4812 w->watchpoint_triggered = watch_triggered_yes;
4813 break;
4814 }
4815 }
4816 }
4817
4818 return 1;
4819 }
4820
4821 /* Possible return values for watchpoint_check. */
4822 enum wp_check_result
4823 {
4824 /* The watchpoint has been deleted. */
4825 WP_DELETED = 1,
4826
4827 /* The value has changed. */
4828 WP_VALUE_CHANGED = 2,
4829
4830 /* The value has not changed. */
4831 WP_VALUE_NOT_CHANGED = 3,
4832
4833 /* Ignore this watchpoint, no matter if the value changed or not. */
4834 WP_IGNORE = 4,
4835 };
4836
4837 #define BP_TEMPFLAG 1
4838 #define BP_HARDWAREFLAG 2
4839
4840 /* Evaluate watchpoint condition expression and check if its value
4841 changed. */
4842
4843 static wp_check_result
4844 watchpoint_check (bpstat bs)
4845 {
4846 struct watchpoint *b;
4847 struct frame_info *fr;
4848 int within_current_scope;
4849
4850 /* BS is built from an existing struct breakpoint. */
4851 gdb_assert (bs->breakpoint_at != NULL);
4852 b = (struct watchpoint *) bs->breakpoint_at;
4853
4854 /* If this is a local watchpoint, we only want to check if the
4855 watchpoint frame is in scope if the current thread is the thread
4856 that was used to create the watchpoint. */
4857 if (!watchpoint_in_thread_scope (b))
4858 return WP_IGNORE;
4859
4860 if (b->exp_valid_block == NULL)
4861 within_current_scope = 1;
4862 else
4863 {
4864 struct frame_info *frame = get_current_frame ();
4865 struct gdbarch *frame_arch = get_frame_arch (frame);
4866 CORE_ADDR frame_pc = get_frame_pc (frame);
4867
4868 /* stack_frame_destroyed_p() returns a non-zero value if we're
4869 still in the function but the stack frame has already been
4870 invalidated. Since we can't rely on the values of local
4871 variables after the stack has been destroyed, we are treating
4872 the watchpoint in that state as `not changed' without further
4873 checking. Don't mark watchpoints as changed if the current
4874 frame is in an epilogue - even if they are in some other
4875 frame, our view of the stack is likely to be wrong and
4876 frame_find_by_id could error out. */
4877 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
4878 return WP_IGNORE;
4879
4880 fr = frame_find_by_id (b->watchpoint_frame);
4881 within_current_scope = (fr != NULL);
4882
4883 /* If we've gotten confused in the unwinder, we might have
4884 returned a frame that can't describe this variable. */
4885 if (within_current_scope)
4886 {
4887 struct symbol *function;
4888
4889 function = get_frame_function (fr);
4890 if (function == NULL
4891 || !contained_in (b->exp_valid_block,
4892 SYMBOL_BLOCK_VALUE (function)))
4893 within_current_scope = 0;
4894 }
4895
4896 if (within_current_scope)
4897 /* If we end up stopping, the current frame will get selected
4898 in normal_stop. So this call to select_frame won't affect
4899 the user. */
4900 select_frame (fr);
4901 }
4902
4903 if (within_current_scope)
4904 {
4905 /* We use value_{,free_to_}mark because it could be a *long*
4906 time before we return to the command level and call
4907 free_all_values. We can't call free_all_values because we
4908 might be in the middle of evaluating a function call. */
4909
4910 int pc = 0;
4911 struct value *mark;
4912 struct value *new_val;
4913
4914 if (is_masked_watchpoint (b))
4915 /* Since we don't know the exact trigger address (from
4916 stopped_data_address), just tell the user we've triggered
4917 a mask watchpoint. */
4918 return WP_VALUE_CHANGED;
4919
4920 mark = value_mark ();
4921 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
4922
4923 if (b->val_bitsize != 0)
4924 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4925
4926 /* We use value_equal_contents instead of value_equal because
4927 the latter coerces an array to a pointer, thus comparing just
4928 the address of the array instead of its contents. This is
4929 not what we want. */
4930 if ((b->val != NULL) != (new_val != NULL)
4931 || (b->val != NULL && !value_equal_contents (b->val.get (),
4932 new_val)))
4933 {
4934 bs->old_val = b->val;
4935 b->val = release_value (new_val);
4936 b->val_valid = true;
4937 if (new_val != NULL)
4938 value_free_to_mark (mark);
4939 return WP_VALUE_CHANGED;
4940 }
4941 else
4942 {
4943 /* Nothing changed. */
4944 value_free_to_mark (mark);
4945 return WP_VALUE_NOT_CHANGED;
4946 }
4947 }
4948 else
4949 {
4950 /* This seems like the only logical thing to do because
4951 if we temporarily ignored the watchpoint, then when
4952 we reenter the block in which it is valid it contains
4953 garbage (in the case of a function, it may have two
4954 garbage values, one before and one after the prologue).
4955 So we can't even detect the first assignment to it and
4956 watch after that (since the garbage may or may not equal
4957 the first value assigned). */
4958 /* We print all the stop information in
4959 breakpoint_ops->print_it, but in this case, by the time we
4960 call breakpoint_ops->print_it this bp will be deleted
4961 already. So we have no choice but print the information
4962 here. */
4963
4964 SWITCH_THRU_ALL_UIS ()
4965 {
4966 struct ui_out *uiout = current_uiout;
4967
4968 if (uiout->is_mi_like_p ())
4969 uiout->field_string
4970 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
4971 uiout->message ("\nWatchpoint %pF deleted because the program has "
4972 "left the block in\n"
4973 "which its expression is valid.\n",
4974 signed_field ("wpnum", b->number));
4975 }
4976
4977 /* Make sure the watchpoint's commands aren't executed. */
4978 b->commands = NULL;
4979 watchpoint_del_at_next_stop (b);
4980
4981 return WP_DELETED;
4982 }
4983 }
4984
4985 /* Return true if it looks like target has stopped due to hitting
4986 breakpoint location BL. This function does not check if we should
4987 stop, only if BL explains the stop. */
4988
4989 static int
4990 bpstat_check_location (const struct bp_location *bl,
4991 const address_space *aspace, CORE_ADDR bp_addr,
4992 const struct target_waitstatus *ws)
4993 {
4994 struct breakpoint *b = bl->owner;
4995
4996 /* BL is from an existing breakpoint. */
4997 gdb_assert (b != NULL);
4998
4999 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5000 }
5001
5002 /* Determine if the watched values have actually changed, and we
5003 should stop. If not, set BS->stop to 0. */
5004
5005 static void
5006 bpstat_check_watchpoint (bpstat bs)
5007 {
5008 const struct bp_location *bl;
5009 struct watchpoint *b;
5010
5011 /* BS is built for existing struct breakpoint. */
5012 bl = bs->bp_location_at;
5013 gdb_assert (bl != NULL);
5014 b = (struct watchpoint *) bs->breakpoint_at;
5015 gdb_assert (b != NULL);
5016
5017 {
5018 int must_check_value = 0;
5019
5020 if (b->type == bp_watchpoint)
5021 /* For a software watchpoint, we must always check the
5022 watched value. */
5023 must_check_value = 1;
5024 else if (b->watchpoint_triggered == watch_triggered_yes)
5025 /* We have a hardware watchpoint (read, write, or access)
5026 and the target earlier reported an address watched by
5027 this watchpoint. */
5028 must_check_value = 1;
5029 else if (b->watchpoint_triggered == watch_triggered_unknown
5030 && b->type == bp_hardware_watchpoint)
5031 /* We were stopped by a hardware watchpoint, but the target could
5032 not report the data address. We must check the watchpoint's
5033 value. Access and read watchpoints are out of luck; without
5034 a data address, we can't figure it out. */
5035 must_check_value = 1;
5036
5037 if (must_check_value)
5038 {
5039 wp_check_result e;
5040
5041 try
5042 {
5043 e = watchpoint_check (bs);
5044 }
5045 catch (const gdb_exception &ex)
5046 {
5047 exception_fprintf (gdb_stderr, ex,
5048 "Error evaluating expression "
5049 "for watchpoint %d\n",
5050 b->number);
5051
5052 SWITCH_THRU_ALL_UIS ()
5053 {
5054 printf_filtered (_("Watchpoint %d deleted.\n"),
5055 b->number);
5056 }
5057 watchpoint_del_at_next_stop (b);
5058 e = WP_DELETED;
5059 }
5060
5061 switch (e)
5062 {
5063 case WP_DELETED:
5064 /* We've already printed what needs to be printed. */
5065 bs->print_it = print_it_done;
5066 /* Stop. */
5067 break;
5068 case WP_IGNORE:
5069 bs->print_it = print_it_noop;
5070 bs->stop = 0;
5071 break;
5072 case WP_VALUE_CHANGED:
5073 if (b->type == bp_read_watchpoint)
5074 {
5075 /* There are two cases to consider here:
5076
5077 1. We're watching the triggered memory for reads.
5078 In that case, trust the target, and always report
5079 the watchpoint hit to the user. Even though
5080 reads don't cause value changes, the value may
5081 have changed since the last time it was read, and
5082 since we're not trapping writes, we will not see
5083 those, and as such we should ignore our notion of
5084 old value.
5085
5086 2. We're watching the triggered memory for both
5087 reads and writes. There are two ways this may
5088 happen:
5089
5090 2.1. This is a target that can't break on data
5091 reads only, but can break on accesses (reads or
5092 writes), such as e.g., x86. We detect this case
5093 at the time we try to insert read watchpoints.
5094
5095 2.2. Otherwise, the target supports read
5096 watchpoints, but, the user set an access or write
5097 watchpoint watching the same memory as this read
5098 watchpoint.
5099
5100 If we're watching memory writes as well as reads,
5101 ignore watchpoint hits when we find that the
5102 value hasn't changed, as reads don't cause
5103 changes. This still gives false positives when
5104 the program writes the same value to memory as
5105 what there was already in memory (we will confuse
5106 it for a read), but it's much better than
5107 nothing. */
5108
5109 int other_write_watchpoint = 0;
5110
5111 if (bl->watchpoint_type == hw_read)
5112 {
5113 struct breakpoint *other_b;
5114
5115 ALL_BREAKPOINTS (other_b)
5116 if (other_b->type == bp_hardware_watchpoint
5117 || other_b->type == bp_access_watchpoint)
5118 {
5119 struct watchpoint *other_w =
5120 (struct watchpoint *) other_b;
5121
5122 if (other_w->watchpoint_triggered
5123 == watch_triggered_yes)
5124 {
5125 other_write_watchpoint = 1;
5126 break;
5127 }
5128 }
5129 }
5130
5131 if (other_write_watchpoint
5132 || bl->watchpoint_type == hw_access)
5133 {
5134 /* We're watching the same memory for writes,
5135 and the value changed since the last time we
5136 updated it, so this trap must be for a write.
5137 Ignore it. */
5138 bs->print_it = print_it_noop;
5139 bs->stop = 0;
5140 }
5141 }
5142 break;
5143 case WP_VALUE_NOT_CHANGED:
5144 if (b->type == bp_hardware_watchpoint
5145 || b->type == bp_watchpoint)
5146 {
5147 /* Don't stop: write watchpoints shouldn't fire if
5148 the value hasn't changed. */
5149 bs->print_it = print_it_noop;
5150 bs->stop = 0;
5151 }
5152 /* Stop. */
5153 break;
5154 default:
5155 /* Can't happen. */
5156 break;
5157 }
5158 }
5159 else /* must_check_value == 0 */
5160 {
5161 /* This is a case where some watchpoint(s) triggered, but
5162 not at the address of this watchpoint, or else no
5163 watchpoint triggered after all. So don't print
5164 anything for this watchpoint. */
5165 bs->print_it = print_it_noop;
5166 bs->stop = 0;
5167 }
5168 }
5169 }
5170
5171 /* For breakpoints that are currently marked as telling gdb to stop,
5172 check conditions (condition proper, frame, thread and ignore count)
5173 of breakpoint referred to by BS. If we should not stop for this
5174 breakpoint, set BS->stop to 0. */
5175
5176 static void
5177 bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
5178 {
5179 const struct bp_location *bl;
5180 struct breakpoint *b;
5181 /* Assume stop. */
5182 bool condition_result = true;
5183 struct expression *cond;
5184
5185 gdb_assert (bs->stop);
5186
5187 /* BS is built for existing struct breakpoint. */
5188 bl = bs->bp_location_at;
5189 gdb_assert (bl != NULL);
5190 b = bs->breakpoint_at;
5191 gdb_assert (b != NULL);
5192
5193 /* Even if the target evaluated the condition on its end and notified GDB, we
5194 need to do so again since GDB does not know if we stopped due to a
5195 breakpoint or a single step breakpoint. */
5196
5197 if (frame_id_p (b->frame_id)
5198 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5199 {
5200 bs->stop = 0;
5201 return;
5202 }
5203
5204 /* If this is a thread/task-specific breakpoint, don't waste cpu
5205 evaluating the condition if this isn't the specified
5206 thread/task. */
5207 if ((b->thread != -1 && b->thread != thread->global_num)
5208 || (b->task != 0 && b->task != ada_get_task_number (thread)))
5209 {
5210 bs->stop = 0;
5211 return;
5212 }
5213
5214 /* Evaluate extension language breakpoints that have a "stop" method
5215 implemented. */
5216 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5217
5218 if (is_watchpoint (b))
5219 {
5220 struct watchpoint *w = (struct watchpoint *) b;
5221
5222 cond = w->cond_exp.get ();
5223 }
5224 else
5225 cond = bl->cond.get ();
5226
5227 if (cond && b->disposition != disp_del_at_next_stop)
5228 {
5229 int within_current_scope = 1;
5230 struct watchpoint * w;
5231
5232 /* We use value_mark and value_free_to_mark because it could
5233 be a long time before we return to the command level and
5234 call free_all_values. We can't call free_all_values
5235 because we might be in the middle of evaluating a
5236 function call. */
5237 struct value *mark = value_mark ();
5238
5239 if (is_watchpoint (b))
5240 w = (struct watchpoint *) b;
5241 else
5242 w = NULL;
5243
5244 /* Need to select the frame, with all that implies so that
5245 the conditions will have the right context. Because we
5246 use the frame, we will not see an inlined function's
5247 variables when we arrive at a breakpoint at the start
5248 of the inlined function; the current frame will be the
5249 call site. */
5250 if (w == NULL || w->cond_exp_valid_block == NULL)
5251 select_frame (get_current_frame ());
5252 else
5253 {
5254 struct frame_info *frame;
5255
5256 /* For local watchpoint expressions, which particular
5257 instance of a local is being watched matters, so we
5258 keep track of the frame to evaluate the expression
5259 in. To evaluate the condition however, it doesn't
5260 really matter which instantiation of the function
5261 where the condition makes sense triggers the
5262 watchpoint. This allows an expression like "watch
5263 global if q > 10" set in `func', catch writes to
5264 global on all threads that call `func', or catch
5265 writes on all recursive calls of `func' by a single
5266 thread. We simply always evaluate the condition in
5267 the innermost frame that's executing where it makes
5268 sense to evaluate the condition. It seems
5269 intuitive. */
5270 frame = block_innermost_frame (w->cond_exp_valid_block);
5271 if (frame != NULL)
5272 select_frame (frame);
5273 else
5274 within_current_scope = 0;
5275 }
5276 if (within_current_scope)
5277 {
5278 try
5279 {
5280 condition_result = breakpoint_cond_eval (cond);
5281 }
5282 catch (const gdb_exception &ex)
5283 {
5284 exception_fprintf (gdb_stderr, ex,
5285 "Error in testing breakpoint condition:\n");
5286 }
5287 }
5288 else
5289 {
5290 warning (_("Watchpoint condition cannot be tested "
5291 "in the current scope"));
5292 /* If we failed to set the right context for this
5293 watchpoint, unconditionally report it. */
5294 }
5295 /* FIXME-someday, should give breakpoint #. */
5296 value_free_to_mark (mark);
5297 }
5298
5299 if (cond && !condition_result)
5300 {
5301 bs->stop = 0;
5302 }
5303 else if (b->ignore_count > 0)
5304 {
5305 b->ignore_count--;
5306 bs->stop = 0;
5307 /* Increase the hit count even though we don't stop. */
5308 ++(b->hit_count);
5309 gdb::observers::breakpoint_modified.notify (b);
5310 }
5311 }
5312
5313 /* Returns true if we need to track moribund locations of LOC's type
5314 on the current target. */
5315
5316 static int
5317 need_moribund_for_location_type (struct bp_location *loc)
5318 {
5319 return ((loc->loc_type == bp_loc_software_breakpoint
5320 && !target_supports_stopped_by_sw_breakpoint ())
5321 || (loc->loc_type == bp_loc_hardware_breakpoint
5322 && !target_supports_stopped_by_hw_breakpoint ()));
5323 }
5324
5325 /* See breakpoint.h. */
5326
5327 bpstat
5328 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5329 const struct target_waitstatus *ws)
5330 {
5331 struct breakpoint *b;
5332 bpstat bs_head = NULL, *bs_link = &bs_head;
5333
5334 ALL_BREAKPOINTS (b)
5335 {
5336 if (!breakpoint_enabled (b))
5337 continue;
5338
5339 for (bp_location *bl = b->loc; bl != NULL; bl = bl->next)
5340 {
5341 /* For hardware watchpoints, we look only at the first
5342 location. The watchpoint_check function will work on the
5343 entire expression, not the individual locations. For
5344 read watchpoints, the watchpoints_triggered function has
5345 checked all locations already. */
5346 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5347 break;
5348
5349 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
5350 continue;
5351
5352 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5353 continue;
5354
5355 /* Come here if it's a watchpoint, or if the break address
5356 matches. */
5357
5358 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5359 explain stop. */
5360
5361 /* Assume we stop. Should we find a watchpoint that is not
5362 actually triggered, or if the condition of the breakpoint
5363 evaluates as false, we'll reset 'stop' to 0. */
5364 bs->stop = 1;
5365 bs->print = 1;
5366
5367 /* If this is a scope breakpoint, mark the associated
5368 watchpoint as triggered so that we will handle the
5369 out-of-scope event. We'll get to the watchpoint next
5370 iteration. */
5371 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5372 {
5373 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5374
5375 w->watchpoint_triggered = watch_triggered_yes;
5376 }
5377 }
5378 }
5379
5380 /* Check if a moribund breakpoint explains the stop. */
5381 if (!target_supports_stopped_by_sw_breakpoint ()
5382 || !target_supports_stopped_by_hw_breakpoint ())
5383 {
5384 for (bp_location *loc : moribund_locations)
5385 {
5386 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5387 && need_moribund_for_location_type (loc))
5388 {
5389 bpstat bs = new bpstats (loc, &bs_link);
5390 /* For hits of moribund locations, we should just proceed. */
5391 bs->stop = 0;
5392 bs->print = 0;
5393 bs->print_it = print_it_noop;
5394 }
5395 }
5396 }
5397
5398 return bs_head;
5399 }
5400
5401 /* See breakpoint.h. */
5402
5403 bpstat
5404 bpstat_stop_status (const address_space *aspace,
5405 CORE_ADDR bp_addr, thread_info *thread,
5406 const struct target_waitstatus *ws,
5407 bpstat stop_chain)
5408 {
5409 struct breakpoint *b = NULL;
5410 /* First item of allocated bpstat's. */
5411 bpstat bs_head = stop_chain;
5412 bpstat bs;
5413 int need_remove_insert;
5414 int removed_any;
5415
5416 /* First, build the bpstat chain with locations that explain a
5417 target stop, while being careful to not set the target running,
5418 as that may invalidate locations (in particular watchpoint
5419 locations are recreated). Resuming will happen here with
5420 breakpoint conditions or watchpoint expressions that include
5421 inferior function calls. */
5422 if (bs_head == NULL)
5423 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5424
5425 /* A bit of special processing for shlib breakpoints. We need to
5426 process solib loading here, so that the lists of loaded and
5427 unloaded libraries are correct before we handle "catch load" and
5428 "catch unload". */
5429 for (bs = bs_head; bs != NULL; bs = bs->next)
5430 {
5431 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5432 {
5433 handle_solib_event ();
5434 break;
5435 }
5436 }
5437
5438 /* Now go through the locations that caused the target to stop, and
5439 check whether we're interested in reporting this stop to higher
5440 layers, or whether we should resume the target transparently. */
5441
5442 removed_any = 0;
5443
5444 for (bs = bs_head; bs != NULL; bs = bs->next)
5445 {
5446 if (!bs->stop)
5447 continue;
5448
5449 b = bs->breakpoint_at;
5450 b->ops->check_status (bs);
5451 if (bs->stop)
5452 {
5453 bpstat_check_breakpoint_conditions (bs, thread);
5454
5455 if (bs->stop)
5456 {
5457 ++(b->hit_count);
5458 gdb::observers::breakpoint_modified.notify (b);
5459
5460 /* We will stop here. */
5461 if (b->disposition == disp_disable)
5462 {
5463 --(b->enable_count);
5464 if (b->enable_count <= 0)
5465 b->enable_state = bp_disabled;
5466 removed_any = 1;
5467 }
5468 if (b->silent)
5469 bs->print = 0;
5470 bs->commands = b->commands;
5471 if (command_line_is_silent (bs->commands
5472 ? bs->commands.get () : NULL))
5473 bs->print = 0;
5474
5475 b->ops->after_condition_true (bs);
5476 }
5477
5478 }
5479
5480 /* Print nothing for this entry if we don't stop or don't
5481 print. */
5482 if (!bs->stop || !bs->print)
5483 bs->print_it = print_it_noop;
5484 }
5485
5486 /* If we aren't stopping, the value of some hardware watchpoint may
5487 not have changed, but the intermediate memory locations we are
5488 watching may have. Don't bother if we're stopping; this will get
5489 done later. */
5490 need_remove_insert = 0;
5491 if (! bpstat_causes_stop (bs_head))
5492 for (bs = bs_head; bs != NULL; bs = bs->next)
5493 if (!bs->stop
5494 && bs->breakpoint_at
5495 && is_hardware_watchpoint (bs->breakpoint_at))
5496 {
5497 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5498
5499 update_watchpoint (w, 0 /* don't reparse. */);
5500 need_remove_insert = 1;
5501 }
5502
5503 if (need_remove_insert)
5504 update_global_location_list (UGLL_MAY_INSERT);
5505 else if (removed_any)
5506 update_global_location_list (UGLL_DONT_INSERT);
5507
5508 return bs_head;
5509 }
5510
5511 static void
5512 handle_jit_event (CORE_ADDR address)
5513 {
5514 struct gdbarch *gdbarch;
5515
5516 infrun_debug_printf ("handling bp_jit_event");
5517
5518 /* Switch terminal for any messages produced by
5519 breakpoint_re_set. */
5520 target_terminal::ours_for_output ();
5521
5522 gdbarch = get_frame_arch (get_current_frame ());
5523 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5524 thus it is expected that its objectfile can be found through
5525 minimal symbol lookup. If it doesn't work (and assert fails), it
5526 most likely means that `jit_breakpoint_re_set` was changes and this
5527 function needs to be updated too. */
5528 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5529 gdb_assert (jit_bp_sym.objfile != nullptr);
5530 jit_event_handler (gdbarch, jit_bp_sym.objfile);
5531
5532 target_terminal::inferior ();
5533 }
5534
5535 /* Prepare WHAT final decision for infrun. */
5536
5537 /* Decide what infrun needs to do with this bpstat. */
5538
5539 struct bpstat_what
5540 bpstat_what (bpstat bs_head)
5541 {
5542 struct bpstat_what retval;
5543 bpstat bs;
5544
5545 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5546 retval.call_dummy = STOP_NONE;
5547 retval.is_longjmp = false;
5548
5549 for (bs = bs_head; bs != NULL; bs = bs->next)
5550 {
5551 /* Extract this BS's action. After processing each BS, we check
5552 if its action overrides all we've seem so far. */
5553 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5554 enum bptype bptype;
5555
5556 if (bs->breakpoint_at == NULL)
5557 {
5558 /* I suspect this can happen if it was a momentary
5559 breakpoint which has since been deleted. */
5560 bptype = bp_none;
5561 }
5562 else
5563 bptype = bs->breakpoint_at->type;
5564
5565 switch (bptype)
5566 {
5567 case bp_none:
5568 break;
5569 case bp_breakpoint:
5570 case bp_hardware_breakpoint:
5571 case bp_single_step:
5572 case bp_until:
5573 case bp_finish:
5574 case bp_shlib_event:
5575 if (bs->stop)
5576 {
5577 if (bs->print)
5578 this_action = BPSTAT_WHAT_STOP_NOISY;
5579 else
5580 this_action = BPSTAT_WHAT_STOP_SILENT;
5581 }
5582 else
5583 this_action = BPSTAT_WHAT_SINGLE;
5584 break;
5585 case bp_watchpoint:
5586 case bp_hardware_watchpoint:
5587 case bp_read_watchpoint:
5588 case bp_access_watchpoint:
5589 if (bs->stop)
5590 {
5591 if (bs->print)
5592 this_action = BPSTAT_WHAT_STOP_NOISY;
5593 else
5594 this_action = BPSTAT_WHAT_STOP_SILENT;
5595 }
5596 else
5597 {
5598 /* There was a watchpoint, but we're not stopping.
5599 This requires no further action. */
5600 }
5601 break;
5602 case bp_longjmp:
5603 case bp_longjmp_call_dummy:
5604 case bp_exception:
5605 if (bs->stop)
5606 {
5607 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5608 retval.is_longjmp = bptype != bp_exception;
5609 }
5610 else
5611 this_action = BPSTAT_WHAT_SINGLE;
5612 break;
5613 case bp_longjmp_resume:
5614 case bp_exception_resume:
5615 if (bs->stop)
5616 {
5617 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5618 retval.is_longjmp = bptype == bp_longjmp_resume;
5619 }
5620 else
5621 this_action = BPSTAT_WHAT_SINGLE;
5622 break;
5623 case bp_step_resume:
5624 if (bs->stop)
5625 this_action = BPSTAT_WHAT_STEP_RESUME;
5626 else
5627 {
5628 /* It is for the wrong frame. */
5629 this_action = BPSTAT_WHAT_SINGLE;
5630 }
5631 break;
5632 case bp_hp_step_resume:
5633 if (bs->stop)
5634 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5635 else
5636 {
5637 /* It is for the wrong frame. */
5638 this_action = BPSTAT_WHAT_SINGLE;
5639 }
5640 break;
5641 case bp_watchpoint_scope:
5642 case bp_thread_event:
5643 case bp_overlay_event:
5644 case bp_longjmp_master:
5645 case bp_std_terminate_master:
5646 case bp_exception_master:
5647 this_action = BPSTAT_WHAT_SINGLE;
5648 break;
5649 case bp_catchpoint:
5650 if (bs->stop)
5651 {
5652 if (bs->print)
5653 this_action = BPSTAT_WHAT_STOP_NOISY;
5654 else
5655 this_action = BPSTAT_WHAT_STOP_SILENT;
5656 }
5657 else
5658 {
5659 /* Some catchpoints are implemented with breakpoints.
5660 For those, we need to step over the breakpoint. */
5661 if (bs->bp_location_at->loc_type != bp_loc_other)
5662 this_action = BPSTAT_WHAT_SINGLE;
5663 }
5664 break;
5665 case bp_jit_event:
5666 this_action = BPSTAT_WHAT_SINGLE;
5667 break;
5668 case bp_call_dummy:
5669 /* Make sure the action is stop (silent or noisy),
5670 so infrun.c pops the dummy frame. */
5671 retval.call_dummy = STOP_STACK_DUMMY;
5672 this_action = BPSTAT_WHAT_STOP_SILENT;
5673 break;
5674 case bp_std_terminate:
5675 /* Make sure the action is stop (silent or noisy),
5676 so infrun.c pops the dummy frame. */
5677 retval.call_dummy = STOP_STD_TERMINATE;
5678 this_action = BPSTAT_WHAT_STOP_SILENT;
5679 break;
5680 case bp_tracepoint:
5681 case bp_fast_tracepoint:
5682 case bp_static_tracepoint:
5683 /* Tracepoint hits should not be reported back to GDB, and
5684 if one got through somehow, it should have been filtered
5685 out already. */
5686 internal_error (__FILE__, __LINE__,
5687 _("bpstat_what: tracepoint encountered"));
5688 break;
5689 case bp_gnu_ifunc_resolver:
5690 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5691 this_action = BPSTAT_WHAT_SINGLE;
5692 break;
5693 case bp_gnu_ifunc_resolver_return:
5694 /* The breakpoint will be removed, execution will restart from the
5695 PC of the former breakpoint. */
5696 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5697 break;
5698
5699 case bp_dprintf:
5700 if (bs->stop)
5701 this_action = BPSTAT_WHAT_STOP_SILENT;
5702 else
5703 this_action = BPSTAT_WHAT_SINGLE;
5704 break;
5705
5706 default:
5707 internal_error (__FILE__, __LINE__,
5708 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5709 }
5710
5711 retval.main_action = std::max (retval.main_action, this_action);
5712 }
5713
5714 return retval;
5715 }
5716
5717 void
5718 bpstat_run_callbacks (bpstat bs_head)
5719 {
5720 bpstat bs;
5721
5722 for (bs = bs_head; bs != NULL; bs = bs->next)
5723 {
5724 struct breakpoint *b = bs->breakpoint_at;
5725
5726 if (b == NULL)
5727 continue;
5728 switch (b->type)
5729 {
5730 case bp_jit_event:
5731 handle_jit_event (bs->bp_location_at->address);
5732 break;
5733 case bp_gnu_ifunc_resolver:
5734 gnu_ifunc_resolver_stop (b);
5735 break;
5736 case bp_gnu_ifunc_resolver_return:
5737 gnu_ifunc_resolver_return_stop (b);
5738 break;
5739 }
5740 }
5741 }
5742
5743 /* See breakpoint.h. */
5744
5745 bool
5746 bpstat_should_step ()
5747 {
5748 struct breakpoint *b;
5749
5750 ALL_BREAKPOINTS (b)
5751 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5752 return true;
5753 return false;
5754 }
5755
5756 /* See breakpoint.h. */
5757
5758 bool
5759 bpstat_causes_stop (bpstat bs)
5760 {
5761 for (; bs != NULL; bs = bs->next)
5762 if (bs->stop)
5763 return true;
5764
5765 return false;
5766 }
5767
5768 \f
5769
5770 /* Compute a string of spaces suitable to indent the next line
5771 so it starts at the position corresponding to the table column
5772 named COL_NAME in the currently active table of UIOUT. */
5773
5774 static char *
5775 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5776 {
5777 static char wrap_indent[80];
5778 int i, total_width, width, align;
5779 const char *text;
5780
5781 total_width = 0;
5782 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5783 {
5784 if (strcmp (text, col_name) == 0)
5785 {
5786 gdb_assert (total_width < sizeof wrap_indent);
5787 memset (wrap_indent, ' ', total_width);
5788 wrap_indent[total_width] = 0;
5789
5790 return wrap_indent;
5791 }
5792
5793 total_width += width + 1;
5794 }
5795
5796 return NULL;
5797 }
5798
5799 /* Determine if the locations of this breakpoint will have their conditions
5800 evaluated by the target, host or a mix of both. Returns the following:
5801
5802 "host": Host evals condition.
5803 "host or target": Host or Target evals condition.
5804 "target": Target evals condition.
5805 */
5806
5807 static const char *
5808 bp_condition_evaluator (struct breakpoint *b)
5809 {
5810 struct bp_location *bl;
5811 char host_evals = 0;
5812 char target_evals = 0;
5813
5814 if (!b)
5815 return NULL;
5816
5817 if (!is_breakpoint (b))
5818 return NULL;
5819
5820 if (gdb_evaluates_breakpoint_condition_p ()
5821 || !target_supports_evaluation_of_breakpoint_conditions ())
5822 return condition_evaluation_host;
5823
5824 for (bl = b->loc; bl; bl = bl->next)
5825 {
5826 if (bl->cond_bytecode)
5827 target_evals++;
5828 else
5829 host_evals++;
5830 }
5831
5832 if (host_evals && target_evals)
5833 return condition_evaluation_both;
5834 else if (target_evals)
5835 return condition_evaluation_target;
5836 else
5837 return condition_evaluation_host;
5838 }
5839
5840 /* Determine the breakpoint location's condition evaluator. This is
5841 similar to bp_condition_evaluator, but for locations. */
5842
5843 static const char *
5844 bp_location_condition_evaluator (struct bp_location *bl)
5845 {
5846 if (bl && !is_breakpoint (bl->owner))
5847 return NULL;
5848
5849 if (gdb_evaluates_breakpoint_condition_p ()
5850 || !target_supports_evaluation_of_breakpoint_conditions ())
5851 return condition_evaluation_host;
5852
5853 if (bl && bl->cond_bytecode)
5854 return condition_evaluation_target;
5855 else
5856 return condition_evaluation_host;
5857 }
5858
5859 /* Print the LOC location out of the list of B->LOC locations. */
5860
5861 static void
5862 print_breakpoint_location (struct breakpoint *b,
5863 struct bp_location *loc)
5864 {
5865 struct ui_out *uiout = current_uiout;
5866
5867 scoped_restore_current_program_space restore_pspace;
5868
5869 if (loc != NULL && loc->shlib_disabled)
5870 loc = NULL;
5871
5872 if (loc != NULL)
5873 set_current_program_space (loc->pspace);
5874
5875 if (b->display_canonical)
5876 uiout->field_string ("what", event_location_to_string (b->location.get ()));
5877 else if (loc && loc->symtab)
5878 {
5879 const struct symbol *sym = loc->symbol;
5880
5881 if (sym)
5882 {
5883 uiout->text ("in ");
5884 uiout->field_string ("func", sym->print_name (),
5885 function_name_style.style ());
5886 uiout->text (" ");
5887 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5888 uiout->text ("at ");
5889 }
5890 uiout->field_string ("file",
5891 symtab_to_filename_for_display (loc->symtab),
5892 file_name_style.style ());
5893 uiout->text (":");
5894
5895 if (uiout->is_mi_like_p ())
5896 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
5897
5898 uiout->field_signed ("line", loc->line_number);
5899 }
5900 else if (loc)
5901 {
5902 string_file stb;
5903
5904 print_address_symbolic (loc->gdbarch, loc->address, &stb,
5905 demangle, "");
5906 uiout->field_stream ("at", stb);
5907 }
5908 else
5909 {
5910 uiout->field_string ("pending",
5911 event_location_to_string (b->location.get ()));
5912 /* If extra_string is available, it could be holding a condition
5913 or dprintf arguments. In either case, make sure it is printed,
5914 too, but only for non-MI streams. */
5915 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
5916 {
5917 if (b->type == bp_dprintf)
5918 uiout->text (",");
5919 else
5920 uiout->text (" ");
5921 uiout->text (b->extra_string);
5922 }
5923 }
5924
5925 if (loc && is_breakpoint (b)
5926 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5927 && bp_condition_evaluator (b) == condition_evaluation_both)
5928 {
5929 uiout->text (" (");
5930 uiout->field_string ("evaluated-by",
5931 bp_location_condition_evaluator (loc));
5932 uiout->text (")");
5933 }
5934 }
5935
5936 static const char *
5937 bptype_string (enum bptype type)
5938 {
5939 struct ep_type_description
5940 {
5941 enum bptype type;
5942 const char *description;
5943 };
5944 static struct ep_type_description bptypes[] =
5945 {
5946 {bp_none, "?deleted?"},
5947 {bp_breakpoint, "breakpoint"},
5948 {bp_hardware_breakpoint, "hw breakpoint"},
5949 {bp_single_step, "sw single-step"},
5950 {bp_until, "until"},
5951 {bp_finish, "finish"},
5952 {bp_watchpoint, "watchpoint"},
5953 {bp_hardware_watchpoint, "hw watchpoint"},
5954 {bp_read_watchpoint, "read watchpoint"},
5955 {bp_access_watchpoint, "acc watchpoint"},
5956 {bp_longjmp, "longjmp"},
5957 {bp_longjmp_resume, "longjmp resume"},
5958 {bp_longjmp_call_dummy, "longjmp for call dummy"},
5959 {bp_exception, "exception"},
5960 {bp_exception_resume, "exception resume"},
5961 {bp_step_resume, "step resume"},
5962 {bp_hp_step_resume, "high-priority step resume"},
5963 {bp_watchpoint_scope, "watchpoint scope"},
5964 {bp_call_dummy, "call dummy"},
5965 {bp_std_terminate, "std::terminate"},
5966 {bp_shlib_event, "shlib events"},
5967 {bp_thread_event, "thread events"},
5968 {bp_overlay_event, "overlay events"},
5969 {bp_longjmp_master, "longjmp master"},
5970 {bp_std_terminate_master, "std::terminate master"},
5971 {bp_exception_master, "exception master"},
5972 {bp_catchpoint, "catchpoint"},
5973 {bp_tracepoint, "tracepoint"},
5974 {bp_fast_tracepoint, "fast tracepoint"},
5975 {bp_static_tracepoint, "static tracepoint"},
5976 {bp_dprintf, "dprintf"},
5977 {bp_jit_event, "jit events"},
5978 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5979 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
5980 };
5981
5982 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5983 || ((int) type != bptypes[(int) type].type))
5984 internal_error (__FILE__, __LINE__,
5985 _("bptypes table does not describe type #%d."),
5986 (int) type);
5987
5988 return bptypes[(int) type].description;
5989 }
5990
5991 /* For MI, output a field named 'thread-groups' with a list as the value.
5992 For CLI, prefix the list with the string 'inf'. */
5993
5994 static void
5995 output_thread_groups (struct ui_out *uiout,
5996 const char *field_name,
5997 const std::vector<int> &inf_nums,
5998 int mi_only)
5999 {
6000 int is_mi = uiout->is_mi_like_p ();
6001
6002 /* For backward compatibility, don't display inferiors in CLI unless
6003 there are several. Always display them for MI. */
6004 if (!is_mi && mi_only)
6005 return;
6006
6007 ui_out_emit_list list_emitter (uiout, field_name);
6008
6009 for (size_t i = 0; i < inf_nums.size (); i++)
6010 {
6011 if (is_mi)
6012 {
6013 char mi_group[10];
6014
6015 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
6016 uiout->field_string (NULL, mi_group);
6017 }
6018 else
6019 {
6020 if (i == 0)
6021 uiout->text (" inf ");
6022 else
6023 uiout->text (", ");
6024
6025 uiout->text (plongest (inf_nums[i]));
6026 }
6027 }
6028 }
6029
6030 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6031 instead of going via breakpoint_ops::print_one. This makes "maint
6032 info breakpoints" show the software breakpoint locations of
6033 catchpoints, which are considered internal implementation
6034 detail. */
6035
6036 static void
6037 print_one_breakpoint_location (struct breakpoint *b,
6038 struct bp_location *loc,
6039 int loc_number,
6040 struct bp_location **last_loc,
6041 int allflag, bool raw_loc)
6042 {
6043 struct command_line *l;
6044 static char bpenables[] = "nynny";
6045
6046 struct ui_out *uiout = current_uiout;
6047 int header_of_multiple = 0;
6048 int part_of_multiple = (loc != NULL);
6049 struct value_print_options opts;
6050
6051 get_user_print_options (&opts);
6052
6053 gdb_assert (!loc || loc_number != 0);
6054 /* See comment in print_one_breakpoint concerning treatment of
6055 breakpoints with single disabled location. */
6056 if (loc == NULL
6057 && (b->loc != NULL
6058 && (b->loc->next != NULL
6059 || !b->loc->enabled || b->loc->disabled_by_cond)))
6060 header_of_multiple = 1;
6061 if (loc == NULL)
6062 loc = b->loc;
6063
6064 annotate_record ();
6065
6066 /* 1 */
6067 annotate_field (0);
6068 if (part_of_multiple)
6069 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6070 else
6071 uiout->field_signed ("number", b->number);
6072
6073 /* 2 */
6074 annotate_field (1);
6075 if (part_of_multiple)
6076 uiout->field_skip ("type");
6077 else
6078 uiout->field_string ("type", bptype_string (b->type));
6079
6080 /* 3 */
6081 annotate_field (2);
6082 if (part_of_multiple)
6083 uiout->field_skip ("disp");
6084 else
6085 uiout->field_string ("disp", bpdisp_text (b->disposition));
6086
6087 /* 4 */
6088 annotate_field (3);
6089 if (part_of_multiple)
6090 uiout->field_string ("enabled", (loc->disabled_by_cond ? "N*"
6091 : (loc->enabled ? "y" : "n")));
6092 else
6093 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6094
6095 /* 5 and 6 */
6096 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
6097 b->ops->print_one (b, last_loc);
6098 else
6099 {
6100 if (is_watchpoint (b))
6101 {
6102 struct watchpoint *w = (struct watchpoint *) b;
6103
6104 /* Field 4, the address, is omitted (which makes the columns
6105 not line up too nicely with the headers, but the effect
6106 is relatively readable). */
6107 if (opts.addressprint)
6108 uiout->field_skip ("addr");
6109 annotate_field (5);
6110 uiout->field_string ("what", w->exp_string);
6111 }
6112 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6113 || is_ada_exception_catchpoint (b))
6114 {
6115 if (opts.addressprint)
6116 {
6117 annotate_field (4);
6118 if (header_of_multiple)
6119 uiout->field_string ("addr", "<MULTIPLE>",
6120 metadata_style.style ());
6121 else if (b->loc == NULL || loc->shlib_disabled)
6122 uiout->field_string ("addr", "<PENDING>",
6123 metadata_style.style ());
6124 else
6125 uiout->field_core_addr ("addr",
6126 loc->gdbarch, loc->address);
6127 }
6128 annotate_field (5);
6129 if (!header_of_multiple)
6130 print_breakpoint_location (b, loc);
6131 if (b->loc)
6132 *last_loc = b->loc;
6133 }
6134 }
6135
6136 if (loc != NULL && !header_of_multiple)
6137 {
6138 std::vector<int> inf_nums;
6139 int mi_only = 1;
6140
6141 for (inferior *inf : all_inferiors ())
6142 {
6143 if (inf->pspace == loc->pspace)
6144 inf_nums.push_back (inf->num);
6145 }
6146
6147 /* For backward compatibility, don't display inferiors in CLI unless
6148 there are several. Always display for MI. */
6149 if (allflag
6150 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6151 && (program_spaces.size () > 1
6152 || number_of_inferiors () > 1)
6153 /* LOC is for existing B, it cannot be in
6154 moribund_locations and thus having NULL OWNER. */
6155 && loc->owner->type != bp_catchpoint))
6156 mi_only = 0;
6157 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6158 }
6159
6160 if (!part_of_multiple)
6161 {
6162 if (b->thread != -1)
6163 {
6164 /* FIXME: This seems to be redundant and lost here; see the
6165 "stop only in" line a little further down. */
6166 uiout->text (" thread ");
6167 uiout->field_signed ("thread", b->thread);
6168 }
6169 else if (b->task != 0)
6170 {
6171 uiout->text (" task ");
6172 uiout->field_signed ("task", b->task);
6173 }
6174 }
6175
6176 uiout->text ("\n");
6177
6178 if (!part_of_multiple)
6179 b->ops->print_one_detail (b, uiout);
6180
6181 if (part_of_multiple && frame_id_p (b->frame_id))
6182 {
6183 annotate_field (6);
6184 uiout->text ("\tstop only in stack frame at ");
6185 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6186 the frame ID. */
6187 uiout->field_core_addr ("frame",
6188 b->gdbarch, b->frame_id.stack_addr);
6189 uiout->text ("\n");
6190 }
6191
6192 if (!part_of_multiple && b->cond_string)
6193 {
6194 annotate_field (7);
6195 if (is_tracepoint (b))
6196 uiout->text ("\ttrace only if ");
6197 else
6198 uiout->text ("\tstop only if ");
6199 uiout->field_string ("cond", b->cond_string);
6200
6201 /* Print whether the target is doing the breakpoint's condition
6202 evaluation. If GDB is doing the evaluation, don't print anything. */
6203 if (is_breakpoint (b)
6204 && breakpoint_condition_evaluation_mode ()
6205 == condition_evaluation_target)
6206 {
6207 uiout->message (" (%pF evals)",
6208 string_field ("evaluated-by",
6209 bp_condition_evaluator (b)));
6210 }
6211 uiout->text ("\n");
6212 }
6213
6214 if (!part_of_multiple && b->thread != -1)
6215 {
6216 /* FIXME should make an annotation for this. */
6217 uiout->text ("\tstop only in thread ");
6218 if (uiout->is_mi_like_p ())
6219 uiout->field_signed ("thread", b->thread);
6220 else
6221 {
6222 struct thread_info *thr = find_thread_global_id (b->thread);
6223
6224 uiout->field_string ("thread", print_thread_id (thr));
6225 }
6226 uiout->text ("\n");
6227 }
6228
6229 if (!part_of_multiple)
6230 {
6231 if (b->hit_count)
6232 {
6233 /* FIXME should make an annotation for this. */
6234 if (is_catchpoint (b))
6235 uiout->text ("\tcatchpoint");
6236 else if (is_tracepoint (b))
6237 uiout->text ("\ttracepoint");
6238 else
6239 uiout->text ("\tbreakpoint");
6240 uiout->text (" already hit ");
6241 uiout->field_signed ("times", b->hit_count);
6242 if (b->hit_count == 1)
6243 uiout->text (" time\n");
6244 else
6245 uiout->text (" times\n");
6246 }
6247 else
6248 {
6249 /* Output the count also if it is zero, but only if this is mi. */
6250 if (uiout->is_mi_like_p ())
6251 uiout->field_signed ("times", b->hit_count);
6252 }
6253 }
6254
6255 if (!part_of_multiple && b->ignore_count)
6256 {
6257 annotate_field (8);
6258 uiout->message ("\tignore next %pF hits\n",
6259 signed_field ("ignore", b->ignore_count));
6260 }
6261
6262 /* Note that an enable count of 1 corresponds to "enable once"
6263 behavior, which is reported by the combination of enablement and
6264 disposition, so we don't need to mention it here. */
6265 if (!part_of_multiple && b->enable_count > 1)
6266 {
6267 annotate_field (8);
6268 uiout->text ("\tdisable after ");
6269 /* Tweak the wording to clarify that ignore and enable counts
6270 are distinct, and have additive effect. */
6271 if (b->ignore_count)
6272 uiout->text ("additional ");
6273 else
6274 uiout->text ("next ");
6275 uiout->field_signed ("enable", b->enable_count);
6276 uiout->text (" hits\n");
6277 }
6278
6279 if (!part_of_multiple && is_tracepoint (b))
6280 {
6281 struct tracepoint *tp = (struct tracepoint *) b;
6282
6283 if (tp->traceframe_usage)
6284 {
6285 uiout->text ("\ttrace buffer usage ");
6286 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6287 uiout->text (" bytes\n");
6288 }
6289 }
6290
6291 l = b->commands ? b->commands.get () : NULL;
6292 if (!part_of_multiple && l)
6293 {
6294 annotate_field (9);
6295 ui_out_emit_tuple tuple_emitter (uiout, "script");
6296 print_command_lines (uiout, l, 4);
6297 }
6298
6299 if (is_tracepoint (b))
6300 {
6301 struct tracepoint *t = (struct tracepoint *) b;
6302
6303 if (!part_of_multiple && t->pass_count)
6304 {
6305 annotate_field (10);
6306 uiout->text ("\tpass count ");
6307 uiout->field_signed ("pass", t->pass_count);
6308 uiout->text (" \n");
6309 }
6310
6311 /* Don't display it when tracepoint or tracepoint location is
6312 pending. */
6313 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6314 {
6315 annotate_field (11);
6316
6317 if (uiout->is_mi_like_p ())
6318 uiout->field_string ("installed",
6319 loc->inserted ? "y" : "n");
6320 else
6321 {
6322 if (loc->inserted)
6323 uiout->text ("\t");
6324 else
6325 uiout->text ("\tnot ");
6326 uiout->text ("installed on target\n");
6327 }
6328 }
6329 }
6330
6331 if (uiout->is_mi_like_p () && !part_of_multiple)
6332 {
6333 if (is_watchpoint (b))
6334 {
6335 struct watchpoint *w = (struct watchpoint *) b;
6336
6337 uiout->field_string ("original-location", w->exp_string);
6338 }
6339 else if (b->location != NULL
6340 && event_location_to_string (b->location.get ()) != NULL)
6341 uiout->field_string ("original-location",
6342 event_location_to_string (b->location.get ()));
6343 }
6344 }
6345
6346 /* See breakpoint.h. */
6347
6348 bool fix_multi_location_breakpoint_output_globally = false;
6349
6350 static void
6351 print_one_breakpoint (struct breakpoint *b,
6352 struct bp_location **last_loc,
6353 int allflag)
6354 {
6355 struct ui_out *uiout = current_uiout;
6356 bool use_fixed_output
6357 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6358 || fix_multi_location_breakpoint_output_globally);
6359
6360 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6361 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
6362
6363 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6364 are outside. */
6365 if (!use_fixed_output)
6366 bkpt_tuple_emitter.reset ();
6367
6368 /* If this breakpoint has custom print function,
6369 it's already printed. Otherwise, print individual
6370 locations, if any. */
6371 if (b->ops == NULL
6372 || b->ops->print_one == NULL
6373 || allflag)
6374 {
6375 /* If breakpoint has a single location that is disabled, we
6376 print it as if it had several locations, since otherwise it's
6377 hard to represent "breakpoint enabled, location disabled"
6378 situation.
6379
6380 Note that while hardware watchpoints have several locations
6381 internally, that's not a property exposed to users.
6382
6383 Likewise, while catchpoints may be implemented with
6384 breakpoints (e.g., catch throw), that's not a property
6385 exposed to users. We do however display the internal
6386 breakpoint locations with "maint info breakpoints". */
6387 if (!is_hardware_watchpoint (b)
6388 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6389 || is_ada_exception_catchpoint (b))
6390 && (allflag
6391 || (b->loc && (b->loc->next
6392 || !b->loc->enabled
6393 || b->loc->disabled_by_cond))))
6394 {
6395 gdb::optional<ui_out_emit_list> locations_list;
6396
6397 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6398 MI record. For later versions, place breakpoint locations in a
6399 list. */
6400 if (uiout->is_mi_like_p () && use_fixed_output)
6401 locations_list.emplace (uiout, "locations");
6402
6403 int n = 1;
6404 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next, ++n)
6405 {
6406 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6407 print_one_breakpoint_location (b, loc, n, last_loc,
6408 allflag, allflag);
6409 }
6410 }
6411 }
6412 }
6413
6414 static int
6415 breakpoint_address_bits (struct breakpoint *b)
6416 {
6417 int print_address_bits = 0;
6418 struct bp_location *loc;
6419
6420 /* Software watchpoints that aren't watching memory don't have an
6421 address to print. */
6422 if (is_no_memory_software_watchpoint (b))
6423 return 0;
6424
6425 for (loc = b->loc; loc; loc = loc->next)
6426 {
6427 int addr_bit;
6428
6429 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6430 if (addr_bit > print_address_bits)
6431 print_address_bits = addr_bit;
6432 }
6433
6434 return print_address_bits;
6435 }
6436
6437 /* See breakpoint.h. */
6438
6439 void
6440 print_breakpoint (breakpoint *b)
6441 {
6442 struct bp_location *dummy_loc = NULL;
6443 print_one_breakpoint (b, &dummy_loc, 0);
6444 }
6445
6446 /* Return true if this breakpoint was set by the user, false if it is
6447 internal or momentary. */
6448
6449 int
6450 user_breakpoint_p (struct breakpoint *b)
6451 {
6452 return b->number > 0;
6453 }
6454
6455 /* See breakpoint.h. */
6456
6457 int
6458 pending_breakpoint_p (struct breakpoint *b)
6459 {
6460 return b->loc == NULL;
6461 }
6462
6463 /* Print information on breakpoints (including watchpoints and tracepoints).
6464
6465 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6466 understood by number_or_range_parser. Only breakpoints included in this
6467 list are then printed.
6468
6469 If SHOW_INTERNAL is true, print internal breakpoints.
6470
6471 If FILTER is non-NULL, call it on each breakpoint and only include the
6472 ones for which it returns true.
6473
6474 Return the total number of breakpoints listed. */
6475
6476 static int
6477 breakpoint_1 (const char *bp_num_list, bool show_internal,
6478 bool (*filter) (const struct breakpoint *))
6479 {
6480 struct breakpoint *b;
6481 struct bp_location *last_loc = NULL;
6482 int nr_printable_breakpoints;
6483 struct value_print_options opts;
6484 int print_address_bits = 0;
6485 int print_type_col_width = 14;
6486 struct ui_out *uiout = current_uiout;
6487 bool has_disabled_by_cond_location = false;
6488
6489 get_user_print_options (&opts);
6490
6491 /* Compute the number of rows in the table, as well as the size
6492 required for address fields. */
6493 nr_printable_breakpoints = 0;
6494 ALL_BREAKPOINTS (b)
6495 {
6496 /* If we have a filter, only list the breakpoints it accepts. */
6497 if (filter && !filter (b))
6498 continue;
6499
6500 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6501 accept. Skip the others. */
6502 if (bp_num_list != NULL && *bp_num_list != '\0')
6503 {
6504 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6505 continue;
6506 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6507 continue;
6508 }
6509
6510 if (show_internal || user_breakpoint_p (b))
6511 {
6512 int addr_bit, type_len;
6513
6514 addr_bit = breakpoint_address_bits (b);
6515 if (addr_bit > print_address_bits)
6516 print_address_bits = addr_bit;
6517
6518 type_len = strlen (bptype_string (b->type));
6519 if (type_len > print_type_col_width)
6520 print_type_col_width = type_len;
6521
6522 nr_printable_breakpoints++;
6523 }
6524 }
6525
6526 {
6527 ui_out_emit_table table_emitter (uiout,
6528 opts.addressprint ? 6 : 5,
6529 nr_printable_breakpoints,
6530 "BreakpointTable");
6531
6532 if (nr_printable_breakpoints > 0)
6533 annotate_breakpoints_headers ();
6534 if (nr_printable_breakpoints > 0)
6535 annotate_field (0);
6536 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6537 if (nr_printable_breakpoints > 0)
6538 annotate_field (1);
6539 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6540 if (nr_printable_breakpoints > 0)
6541 annotate_field (2);
6542 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6543 if (nr_printable_breakpoints > 0)
6544 annotate_field (3);
6545 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6546 if (opts.addressprint)
6547 {
6548 if (nr_printable_breakpoints > 0)
6549 annotate_field (4);
6550 if (print_address_bits <= 32)
6551 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6552 else
6553 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6554 }
6555 if (nr_printable_breakpoints > 0)
6556 annotate_field (5);
6557 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6558 uiout->table_body ();
6559 if (nr_printable_breakpoints > 0)
6560 annotate_breakpoints_table ();
6561
6562 ALL_BREAKPOINTS (b)
6563 {
6564 QUIT;
6565 /* If we have a filter, only list the breakpoints it accepts. */
6566 if (filter && !filter (b))
6567 continue;
6568
6569 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6570 accept. Skip the others. */
6571
6572 if (bp_num_list != NULL && *bp_num_list != '\0')
6573 {
6574 if (show_internal) /* maintenance info breakpoint */
6575 {
6576 if (parse_and_eval_long (bp_num_list) != b->number)
6577 continue;
6578 }
6579 else /* all others */
6580 {
6581 if (!number_is_in_list (bp_num_list, b->number))
6582 continue;
6583 }
6584 }
6585 /* We only print out user settable breakpoints unless the
6586 show_internal is set. */
6587 if (show_internal || user_breakpoint_p (b))
6588 {
6589 print_one_breakpoint (b, &last_loc, show_internal);
6590 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
6591 if (loc->disabled_by_cond)
6592 has_disabled_by_cond_location = true;
6593 }
6594 }
6595 }
6596
6597 if (nr_printable_breakpoints == 0)
6598 {
6599 /* If there's a filter, let the caller decide how to report
6600 empty list. */
6601 if (!filter)
6602 {
6603 if (bp_num_list == NULL || *bp_num_list == '\0')
6604 uiout->message ("No breakpoints or watchpoints.\n");
6605 else
6606 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6607 bp_num_list);
6608 }
6609 }
6610 else
6611 {
6612 if (last_loc && !server_command)
6613 set_next_address (last_loc->gdbarch, last_loc->address);
6614
6615 if (has_disabled_by_cond_location)
6616 uiout->message (_("(*): Breakpoint condition is invalid at this "
6617 "location.\n"));
6618 }
6619
6620 /* FIXME? Should this be moved up so that it is only called when
6621 there have been breakpoints? */
6622 annotate_breakpoints_table_end ();
6623
6624 return nr_printable_breakpoints;
6625 }
6626
6627 /* Display the value of default-collect in a way that is generally
6628 compatible with the breakpoint list. */
6629
6630 static void
6631 default_collect_info (void)
6632 {
6633 struct ui_out *uiout = current_uiout;
6634
6635 /* If it has no value (which is frequently the case), say nothing; a
6636 message like "No default-collect." gets in user's face when it's
6637 not wanted. */
6638 if (!*default_collect)
6639 return;
6640
6641 /* The following phrase lines up nicely with per-tracepoint collect
6642 actions. */
6643 uiout->text ("default collect ");
6644 uiout->field_string ("default-collect", default_collect);
6645 uiout->text (" \n");
6646 }
6647
6648 static void
6649 info_breakpoints_command (const char *args, int from_tty)
6650 {
6651 breakpoint_1 (args, false, NULL);
6652
6653 default_collect_info ();
6654 }
6655
6656 static void
6657 info_watchpoints_command (const char *args, int from_tty)
6658 {
6659 int num_printed = breakpoint_1 (args, false, is_watchpoint);
6660 struct ui_out *uiout = current_uiout;
6661
6662 if (num_printed == 0)
6663 {
6664 if (args == NULL || *args == '\0')
6665 uiout->message ("No watchpoints.\n");
6666 else
6667 uiout->message ("No watchpoint matching '%s'.\n", args);
6668 }
6669 }
6670
6671 static void
6672 maintenance_info_breakpoints (const char *args, int from_tty)
6673 {
6674 breakpoint_1 (args, true, NULL);
6675
6676 default_collect_info ();
6677 }
6678
6679 static int
6680 breakpoint_has_pc (struct breakpoint *b,
6681 struct program_space *pspace,
6682 CORE_ADDR pc, struct obj_section *section)
6683 {
6684 struct bp_location *bl = b->loc;
6685
6686 for (; bl; bl = bl->next)
6687 {
6688 if (bl->pspace == pspace
6689 && bl->address == pc
6690 && (!overlay_debugging || bl->section == section))
6691 return 1;
6692 }
6693 return 0;
6694 }
6695
6696 /* Print a message describing any user-breakpoints set at PC. This
6697 concerns with logical breakpoints, so we match program spaces, not
6698 address spaces. */
6699
6700 static void
6701 describe_other_breakpoints (struct gdbarch *gdbarch,
6702 struct program_space *pspace, CORE_ADDR pc,
6703 struct obj_section *section, int thread)
6704 {
6705 int others = 0;
6706 struct breakpoint *b;
6707
6708 ALL_BREAKPOINTS (b)
6709 others += (user_breakpoint_p (b)
6710 && breakpoint_has_pc (b, pspace, pc, section));
6711 if (others > 0)
6712 {
6713 if (others == 1)
6714 printf_filtered (_("Note: breakpoint "));
6715 else /* if (others == ???) */
6716 printf_filtered (_("Note: breakpoints "));
6717 ALL_BREAKPOINTS (b)
6718 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6719 {
6720 others--;
6721 printf_filtered ("%d", b->number);
6722 if (b->thread == -1 && thread != -1)
6723 printf_filtered (" (all threads)");
6724 else if (b->thread != -1)
6725 printf_filtered (" (thread %d)", b->thread);
6726 printf_filtered ("%s%s ",
6727 ((b->enable_state == bp_disabled
6728 || b->enable_state == bp_call_disabled)
6729 ? " (disabled)"
6730 : ""),
6731 (others > 1) ? ","
6732 : ((others == 1) ? " and" : ""));
6733 }
6734 current_uiout->message (_("also set at pc %ps.\n"),
6735 styled_string (address_style.style (),
6736 paddress (gdbarch, pc)));
6737 }
6738 }
6739 \f
6740
6741 /* Return true iff it is meaningful to use the address member of LOC.
6742 For some breakpoint types, the locations' address members are
6743 irrelevant and it makes no sense to attempt to compare them to
6744 other addresses (or use them for any other purpose either).
6745
6746 More specifically, software watchpoints and catchpoints that are
6747 not backed by breakpoints always have a zero valued location
6748 address and we don't want to mark breakpoints of any of these types
6749 to be a duplicate of an actual breakpoint location at address
6750 zero. */
6751
6752 static bool
6753 bl_address_is_meaningful (bp_location *loc)
6754 {
6755 return loc->loc_type != bp_loc_other;
6756 }
6757
6758 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6759 true if LOC1 and LOC2 represent the same watchpoint location. */
6760
6761 static int
6762 watchpoint_locations_match (struct bp_location *loc1,
6763 struct bp_location *loc2)
6764 {
6765 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6766 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6767
6768 /* Both of them must exist. */
6769 gdb_assert (w1 != NULL);
6770 gdb_assert (w2 != NULL);
6771
6772 /* If the target can evaluate the condition expression in hardware,
6773 then we we need to insert both watchpoints even if they are at
6774 the same place. Otherwise the watchpoint will only trigger when
6775 the condition of whichever watchpoint was inserted evaluates to
6776 true, not giving a chance for GDB to check the condition of the
6777 other watchpoint. */
6778 if ((w1->cond_exp
6779 && target_can_accel_watchpoint_condition (loc1->address,
6780 loc1->length,
6781 loc1->watchpoint_type,
6782 w1->cond_exp.get ()))
6783 || (w2->cond_exp
6784 && target_can_accel_watchpoint_condition (loc2->address,
6785 loc2->length,
6786 loc2->watchpoint_type,
6787 w2->cond_exp.get ())))
6788 return 0;
6789
6790 /* Note that this checks the owner's type, not the location's. In
6791 case the target does not support read watchpoints, but does
6792 support access watchpoints, we'll have bp_read_watchpoint
6793 watchpoints with hw_access locations. Those should be considered
6794 duplicates of hw_read locations. The hw_read locations will
6795 become hw_access locations later. */
6796 return (loc1->owner->type == loc2->owner->type
6797 && loc1->pspace->aspace == loc2->pspace->aspace
6798 && loc1->address == loc2->address
6799 && loc1->length == loc2->length);
6800 }
6801
6802 /* See breakpoint.h. */
6803
6804 int
6805 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6806 const address_space *aspace2, CORE_ADDR addr2)
6807 {
6808 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6809 || aspace1 == aspace2)
6810 && addr1 == addr2);
6811 }
6812
6813 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6814 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6815 matches ASPACE2. On targets that have global breakpoints, the address
6816 space doesn't really matter. */
6817
6818 static int
6819 breakpoint_address_match_range (const address_space *aspace1,
6820 CORE_ADDR addr1,
6821 int len1, const address_space *aspace2,
6822 CORE_ADDR addr2)
6823 {
6824 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6825 || aspace1 == aspace2)
6826 && addr2 >= addr1 && addr2 < addr1 + len1);
6827 }
6828
6829 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6830 a ranged breakpoint. In most targets, a match happens only if ASPACE
6831 matches the breakpoint's address space. On targets that have global
6832 breakpoints, the address space doesn't really matter. */
6833
6834 static int
6835 breakpoint_location_address_match (struct bp_location *bl,
6836 const address_space *aspace,
6837 CORE_ADDR addr)
6838 {
6839 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6840 aspace, addr)
6841 || (bl->length
6842 && breakpoint_address_match_range (bl->pspace->aspace,
6843 bl->address, bl->length,
6844 aspace, addr)));
6845 }
6846
6847 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6848 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6849 match happens only if ASPACE matches the breakpoint's address
6850 space. On targets that have global breakpoints, the address space
6851 doesn't really matter. */
6852
6853 static int
6854 breakpoint_location_address_range_overlap (struct bp_location *bl,
6855 const address_space *aspace,
6856 CORE_ADDR addr, int len)
6857 {
6858 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6859 || bl->pspace->aspace == aspace)
6860 {
6861 int bl_len = bl->length != 0 ? bl->length : 1;
6862
6863 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6864 return 1;
6865 }
6866 return 0;
6867 }
6868
6869 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6870 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6871 true, otherwise returns false. */
6872
6873 static int
6874 tracepoint_locations_match (struct bp_location *loc1,
6875 struct bp_location *loc2)
6876 {
6877 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6878 /* Since tracepoint locations are never duplicated with others', tracepoint
6879 locations at the same address of different tracepoints are regarded as
6880 different locations. */
6881 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6882 else
6883 return 0;
6884 }
6885
6886 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6887 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
6888 the same location. If SW_HW_BPS_MATCH is true, then software
6889 breakpoint locations and hardware breakpoint locations match,
6890 otherwise they don't. */
6891
6892 static int
6893 breakpoint_locations_match (struct bp_location *loc1,
6894 struct bp_location *loc2,
6895 bool sw_hw_bps_match)
6896 {
6897 int hw_point1, hw_point2;
6898
6899 /* Both of them must not be in moribund_locations. */
6900 gdb_assert (loc1->owner != NULL);
6901 gdb_assert (loc2->owner != NULL);
6902
6903 hw_point1 = is_hardware_watchpoint (loc1->owner);
6904 hw_point2 = is_hardware_watchpoint (loc2->owner);
6905
6906 if (hw_point1 != hw_point2)
6907 return 0;
6908 else if (hw_point1)
6909 return watchpoint_locations_match (loc1, loc2);
6910 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6911 return tracepoint_locations_match (loc1, loc2);
6912 else
6913 /* We compare bp_location.length in order to cover ranged
6914 breakpoints. Keep this in sync with
6915 bp_location_is_less_than. */
6916 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6917 loc2->pspace->aspace, loc2->address)
6918 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
6919 && loc1->length == loc2->length);
6920 }
6921
6922 static void
6923 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6924 int bnum, int have_bnum)
6925 {
6926 /* The longest string possibly returned by hex_string_custom
6927 is 50 chars. These must be at least that big for safety. */
6928 char astr1[64];
6929 char astr2[64];
6930
6931 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6932 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
6933 if (have_bnum)
6934 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
6935 bnum, astr1, astr2);
6936 else
6937 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
6938 }
6939
6940 /* Adjust a breakpoint's address to account for architectural
6941 constraints on breakpoint placement. Return the adjusted address.
6942 Note: Very few targets require this kind of adjustment. For most
6943 targets, this function is simply the identity function. */
6944
6945 static CORE_ADDR
6946 adjust_breakpoint_address (struct gdbarch *gdbarch,
6947 CORE_ADDR bpaddr, enum bptype bptype)
6948 {
6949 if (bptype == bp_watchpoint
6950 || bptype == bp_hardware_watchpoint
6951 || bptype == bp_read_watchpoint
6952 || bptype == bp_access_watchpoint
6953 || bptype == bp_catchpoint)
6954 {
6955 /* Watchpoints and the various bp_catch_* eventpoints should not
6956 have their addresses modified. */
6957 return bpaddr;
6958 }
6959 else if (bptype == bp_single_step)
6960 {
6961 /* Single-step breakpoints should not have their addresses
6962 modified. If there's any architectural constrain that
6963 applies to this address, then it should have already been
6964 taken into account when the breakpoint was created in the
6965 first place. If we didn't do this, stepping through e.g.,
6966 Thumb-2 IT blocks would break. */
6967 return bpaddr;
6968 }
6969 else
6970 {
6971 CORE_ADDR adjusted_bpaddr = bpaddr;
6972
6973 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
6974 {
6975 /* Some targets have architectural constraints on the placement
6976 of breakpoint instructions. Obtain the adjusted address. */
6977 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
6978 }
6979
6980 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
6981
6982 /* An adjusted breakpoint address can significantly alter
6983 a user's expectations. Print a warning if an adjustment
6984 is required. */
6985 if (adjusted_bpaddr != bpaddr)
6986 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6987
6988 return adjusted_bpaddr;
6989 }
6990 }
6991
6992 static bp_loc_type
6993 bp_location_from_bp_type (bptype type)
6994 {
6995 switch (type)
6996 {
6997 case bp_breakpoint:
6998 case bp_single_step:
6999 case bp_until:
7000 case bp_finish:
7001 case bp_longjmp:
7002 case bp_longjmp_resume:
7003 case bp_longjmp_call_dummy:
7004 case bp_exception:
7005 case bp_exception_resume:
7006 case bp_step_resume:
7007 case bp_hp_step_resume:
7008 case bp_watchpoint_scope:
7009 case bp_call_dummy:
7010 case bp_std_terminate:
7011 case bp_shlib_event:
7012 case bp_thread_event:
7013 case bp_overlay_event:
7014 case bp_jit_event:
7015 case bp_longjmp_master:
7016 case bp_std_terminate_master:
7017 case bp_exception_master:
7018 case bp_gnu_ifunc_resolver:
7019 case bp_gnu_ifunc_resolver_return:
7020 case bp_dprintf:
7021 return bp_loc_software_breakpoint;
7022 case bp_hardware_breakpoint:
7023 return bp_loc_hardware_breakpoint;
7024 case bp_hardware_watchpoint:
7025 case bp_read_watchpoint:
7026 case bp_access_watchpoint:
7027 return bp_loc_hardware_watchpoint;
7028 case bp_watchpoint:
7029 case bp_catchpoint:
7030 case bp_tracepoint:
7031 case bp_fast_tracepoint:
7032 case bp_static_tracepoint:
7033 return bp_loc_other;
7034 default:
7035 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7036 }
7037 }
7038
7039 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7040 {
7041 this->owner = owner;
7042 this->cond_bytecode = NULL;
7043 this->shlib_disabled = 0;
7044 this->enabled = 1;
7045 this->disabled_by_cond = false;
7046
7047 this->loc_type = type;
7048
7049 if (this->loc_type == bp_loc_software_breakpoint
7050 || this->loc_type == bp_loc_hardware_breakpoint)
7051 mark_breakpoint_location_modified (this);
7052
7053 this->refc = 1;
7054 }
7055
7056 bp_location::bp_location (breakpoint *owner)
7057 : bp_location::bp_location (owner,
7058 bp_location_from_bp_type (owner->type))
7059 {
7060 }
7061
7062 /* Allocate a struct bp_location. */
7063
7064 static struct bp_location *
7065 allocate_bp_location (struct breakpoint *bpt)
7066 {
7067 return bpt->ops->allocate_location (bpt);
7068 }
7069
7070 static void
7071 free_bp_location (struct bp_location *loc)
7072 {
7073 delete loc;
7074 }
7075
7076 /* Increment reference count. */
7077
7078 static void
7079 incref_bp_location (struct bp_location *bl)
7080 {
7081 ++bl->refc;
7082 }
7083
7084 /* Decrement reference count. If the reference count reaches 0,
7085 destroy the bp_location. Sets *BLP to NULL. */
7086
7087 static void
7088 decref_bp_location (struct bp_location **blp)
7089 {
7090 gdb_assert ((*blp)->refc > 0);
7091
7092 if (--(*blp)->refc == 0)
7093 free_bp_location (*blp);
7094 *blp = NULL;
7095 }
7096
7097 /* Add breakpoint B at the end of the global breakpoint chain. */
7098
7099 static breakpoint *
7100 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7101 {
7102 struct breakpoint *b1;
7103 struct breakpoint *result = b.get ();
7104
7105 /* Add this breakpoint to the end of the chain so that a list of
7106 breakpoints will come out in order of increasing numbers. */
7107
7108 b1 = breakpoint_chain;
7109 if (b1 == 0)
7110 breakpoint_chain = b.release ();
7111 else
7112 {
7113 while (b1->next)
7114 b1 = b1->next;
7115 b1->next = b.release ();
7116 }
7117
7118 return result;
7119 }
7120
7121 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7122
7123 static void
7124 init_raw_breakpoint_without_location (struct breakpoint *b,
7125 struct gdbarch *gdbarch,
7126 enum bptype bptype,
7127 const struct breakpoint_ops *ops)
7128 {
7129 gdb_assert (ops != NULL);
7130
7131 b->ops = ops;
7132 b->type = bptype;
7133 b->gdbarch = gdbarch;
7134 b->language = current_language->la_language;
7135 b->input_radix = input_radix;
7136 b->related_breakpoint = b;
7137 }
7138
7139 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7140 that has type BPTYPE and has no locations as yet. */
7141
7142 static struct breakpoint *
7143 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7144 enum bptype bptype,
7145 const struct breakpoint_ops *ops)
7146 {
7147 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7148
7149 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7150 return add_to_breakpoint_chain (std::move (b));
7151 }
7152
7153 /* Initialize loc->function_name. */
7154
7155 static void
7156 set_breakpoint_location_function (struct bp_location *loc)
7157 {
7158 gdb_assert (loc->owner != NULL);
7159
7160 if (loc->owner->type == bp_breakpoint
7161 || loc->owner->type == bp_hardware_breakpoint
7162 || is_tracepoint (loc->owner))
7163 {
7164 const char *function_name;
7165
7166 if (loc->msymbol != NULL
7167 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
7168 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
7169 {
7170 struct breakpoint *b = loc->owner;
7171
7172 function_name = loc->msymbol->linkage_name ();
7173
7174 if (b->type == bp_breakpoint && b->loc == loc
7175 && loc->next == NULL && b->related_breakpoint == b)
7176 {
7177 /* Create only the whole new breakpoint of this type but do not
7178 mess more complicated breakpoints with multiple locations. */
7179 b->type = bp_gnu_ifunc_resolver;
7180 /* Remember the resolver's address for use by the return
7181 breakpoint. */
7182 loc->related_address = loc->address;
7183 }
7184 }
7185 else
7186 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7187
7188 if (function_name)
7189 loc->function_name = xstrdup (function_name);
7190 }
7191 }
7192
7193 /* Attempt to determine architecture of location identified by SAL. */
7194 struct gdbarch *
7195 get_sal_arch (struct symtab_and_line sal)
7196 {
7197 if (sal.section)
7198 return sal.section->objfile->arch ();
7199 if (sal.symtab)
7200 return SYMTAB_OBJFILE (sal.symtab)->arch ();
7201
7202 return NULL;
7203 }
7204
7205 /* Low level routine for partially initializing a breakpoint of type
7206 BPTYPE. The newly created breakpoint's address, section, source
7207 file name, and line number are provided by SAL.
7208
7209 It is expected that the caller will complete the initialization of
7210 the newly created breakpoint struct as well as output any status
7211 information regarding the creation of a new breakpoint. */
7212
7213 static void
7214 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7215 struct symtab_and_line sal, enum bptype bptype,
7216 const struct breakpoint_ops *ops)
7217 {
7218 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7219
7220 add_location_to_breakpoint (b, &sal);
7221
7222 if (bptype != bp_catchpoint)
7223 gdb_assert (sal.pspace != NULL);
7224
7225 /* Store the program space that was used to set the breakpoint,
7226 except for ordinary breakpoints, which are independent of the
7227 program space. */
7228 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7229 b->pspace = sal.pspace;
7230 }
7231
7232 /* set_raw_breakpoint is a low level routine for allocating and
7233 partially initializing a breakpoint of type BPTYPE. The newly
7234 created breakpoint's address, section, source file name, and line
7235 number are provided by SAL. The newly created and partially
7236 initialized breakpoint is added to the breakpoint chain and
7237 is also returned as the value of this function.
7238
7239 It is expected that the caller will complete the initialization of
7240 the newly created breakpoint struct as well as output any status
7241 information regarding the creation of a new breakpoint. In
7242 particular, set_raw_breakpoint does NOT set the breakpoint
7243 number! Care should be taken to not allow an error to occur
7244 prior to completing the initialization of the breakpoint. If this
7245 should happen, a bogus breakpoint will be left on the chain. */
7246
7247 struct breakpoint *
7248 set_raw_breakpoint (struct gdbarch *gdbarch,
7249 struct symtab_and_line sal, enum bptype bptype,
7250 const struct breakpoint_ops *ops)
7251 {
7252 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7253
7254 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7255 return add_to_breakpoint_chain (std::move (b));
7256 }
7257
7258 /* Call this routine when stepping and nexting to enable a breakpoint
7259 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7260 initiated the operation. */
7261
7262 void
7263 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7264 {
7265 struct breakpoint *b, *b_tmp;
7266 int thread = tp->global_num;
7267
7268 /* To avoid having to rescan all objfile symbols at every step,
7269 we maintain a list of continually-inserted but always disabled
7270 longjmp "master" breakpoints. Here, we simply create momentary
7271 clones of those and enable them for the requested thread. */
7272 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7273 if (b->pspace == current_program_space
7274 && (b->type == bp_longjmp_master
7275 || b->type == bp_exception_master))
7276 {
7277 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7278 struct breakpoint *clone;
7279
7280 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7281 after their removal. */
7282 clone = momentary_breakpoint_from_master (b, type,
7283 &momentary_breakpoint_ops, 1);
7284 clone->thread = thread;
7285 }
7286
7287 tp->initiating_frame = frame;
7288 }
7289
7290 /* Delete all longjmp breakpoints from THREAD. */
7291 void
7292 delete_longjmp_breakpoint (int thread)
7293 {
7294 struct breakpoint *b, *b_tmp;
7295
7296 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7297 if (b->type == bp_longjmp || b->type == bp_exception)
7298 {
7299 if (b->thread == thread)
7300 delete_breakpoint (b);
7301 }
7302 }
7303
7304 void
7305 delete_longjmp_breakpoint_at_next_stop (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 b->disposition = disp_del_at_next_stop;
7314 }
7315 }
7316
7317 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7318 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7319 pointer to any of them. Return NULL if this system cannot place longjmp
7320 breakpoints. */
7321
7322 struct breakpoint *
7323 set_longjmp_breakpoint_for_call_dummy (void)
7324 {
7325 struct breakpoint *b, *retval = NULL;
7326
7327 ALL_BREAKPOINTS (b)
7328 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7329 {
7330 struct breakpoint *new_b;
7331
7332 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7333 &momentary_breakpoint_ops,
7334 1);
7335 new_b->thread = inferior_thread ()->global_num;
7336
7337 /* Link NEW_B into the chain of RETVAL breakpoints. */
7338
7339 gdb_assert (new_b->related_breakpoint == new_b);
7340 if (retval == NULL)
7341 retval = new_b;
7342 new_b->related_breakpoint = retval;
7343 while (retval->related_breakpoint != new_b->related_breakpoint)
7344 retval = retval->related_breakpoint;
7345 retval->related_breakpoint = new_b;
7346 }
7347
7348 return retval;
7349 }
7350
7351 /* Verify all existing dummy frames and their associated breakpoints for
7352 TP. Remove those which can no longer be found in the current frame
7353 stack.
7354
7355 You should call this function only at places where it is safe to currently
7356 unwind the whole stack. Failed stack unwind would discard live dummy
7357 frames. */
7358
7359 void
7360 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7361 {
7362 struct breakpoint *b, *b_tmp;
7363
7364 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7365 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7366 {
7367 struct breakpoint *dummy_b = b->related_breakpoint;
7368
7369 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7370 dummy_b = dummy_b->related_breakpoint;
7371 if (dummy_b->type != bp_call_dummy
7372 || frame_find_by_id (dummy_b->frame_id) != NULL)
7373 continue;
7374
7375 dummy_frame_discard (dummy_b->frame_id, tp);
7376
7377 while (b->related_breakpoint != b)
7378 {
7379 if (b_tmp == b->related_breakpoint)
7380 b_tmp = b->related_breakpoint->next;
7381 delete_breakpoint (b->related_breakpoint);
7382 }
7383 delete_breakpoint (b);
7384 }
7385 }
7386
7387 void
7388 enable_overlay_breakpoints (void)
7389 {
7390 struct breakpoint *b;
7391
7392 ALL_BREAKPOINTS (b)
7393 if (b->type == bp_overlay_event)
7394 {
7395 b->enable_state = bp_enabled;
7396 update_global_location_list (UGLL_MAY_INSERT);
7397 overlay_events_enabled = 1;
7398 }
7399 }
7400
7401 void
7402 disable_overlay_breakpoints (void)
7403 {
7404 struct breakpoint *b;
7405
7406 ALL_BREAKPOINTS (b)
7407 if (b->type == bp_overlay_event)
7408 {
7409 b->enable_state = bp_disabled;
7410 update_global_location_list (UGLL_DONT_INSERT);
7411 overlay_events_enabled = 0;
7412 }
7413 }
7414
7415 /* Set an active std::terminate breakpoint for each std::terminate
7416 master breakpoint. */
7417 void
7418 set_std_terminate_breakpoint (void)
7419 {
7420 struct breakpoint *b, *b_tmp;
7421
7422 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7423 if (b->pspace == current_program_space
7424 && b->type == bp_std_terminate_master)
7425 {
7426 momentary_breakpoint_from_master (b, bp_std_terminate,
7427 &momentary_breakpoint_ops, 1);
7428 }
7429 }
7430
7431 /* Delete all the std::terminate breakpoints. */
7432 void
7433 delete_std_terminate_breakpoint (void)
7434 {
7435 struct breakpoint *b, *b_tmp;
7436
7437 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7438 if (b->type == bp_std_terminate)
7439 delete_breakpoint (b);
7440 }
7441
7442 struct breakpoint *
7443 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7444 {
7445 struct breakpoint *b;
7446
7447 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7448 &internal_breakpoint_ops);
7449
7450 b->enable_state = bp_enabled;
7451 /* location has to be used or breakpoint_re_set will delete me. */
7452 b->location = new_address_location (b->loc->address, NULL, 0);
7453
7454 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7455
7456 return b;
7457 }
7458
7459 struct lang_and_radix
7460 {
7461 enum language lang;
7462 int radix;
7463 };
7464
7465 /* Create a breakpoint for JIT code registration and unregistration. */
7466
7467 struct breakpoint *
7468 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7469 {
7470 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7471 &internal_breakpoint_ops);
7472 }
7473
7474 /* Remove JIT code registration and unregistration breakpoint(s). */
7475
7476 void
7477 remove_jit_event_breakpoints (void)
7478 {
7479 struct breakpoint *b, *b_tmp;
7480
7481 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7482 if (b->type == bp_jit_event
7483 && b->loc->pspace == current_program_space)
7484 delete_breakpoint (b);
7485 }
7486
7487 void
7488 remove_solib_event_breakpoints (void)
7489 {
7490 struct breakpoint *b, *b_tmp;
7491
7492 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7493 if (b->type == bp_shlib_event
7494 && b->loc->pspace == current_program_space)
7495 delete_breakpoint (b);
7496 }
7497
7498 /* See breakpoint.h. */
7499
7500 void
7501 remove_solib_event_breakpoints_at_next_stop (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 b->disposition = disp_del_at_next_stop;
7509 }
7510
7511 /* Helper for create_solib_event_breakpoint /
7512 create_and_insert_solib_event_breakpoint. Allows specifying which
7513 INSERT_MODE to pass through to update_global_location_list. */
7514
7515 static struct breakpoint *
7516 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7517 enum ugll_insert_mode insert_mode)
7518 {
7519 struct breakpoint *b;
7520
7521 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7522 &internal_breakpoint_ops);
7523 update_global_location_list_nothrow (insert_mode);
7524 return b;
7525 }
7526
7527 struct breakpoint *
7528 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7529 {
7530 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7531 }
7532
7533 /* See breakpoint.h. */
7534
7535 struct breakpoint *
7536 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7537 {
7538 struct breakpoint *b;
7539
7540 /* Explicitly tell update_global_location_list to insert
7541 locations. */
7542 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7543 if (!b->loc->inserted)
7544 {
7545 delete_breakpoint (b);
7546 return NULL;
7547 }
7548 return b;
7549 }
7550
7551 /* Disable any breakpoints that are on code in shared libraries. Only
7552 apply to enabled breakpoints, disabled ones can just stay disabled. */
7553
7554 void
7555 disable_breakpoints_in_shlibs (void)
7556 {
7557 struct bp_location *loc, **locp_tmp;
7558
7559 ALL_BP_LOCATIONS (loc, locp_tmp)
7560 {
7561 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7562 struct breakpoint *b = loc->owner;
7563
7564 /* We apply the check to all breakpoints, including disabled for
7565 those with loc->duplicate set. This is so that when breakpoint
7566 becomes enabled, or the duplicate is removed, gdb will try to
7567 insert all breakpoints. If we don't set shlib_disabled here,
7568 we'll try to insert those breakpoints and fail. */
7569 if (((b->type == bp_breakpoint)
7570 || (b->type == bp_jit_event)
7571 || (b->type == bp_hardware_breakpoint)
7572 || (is_tracepoint (b)))
7573 && loc->pspace == current_program_space
7574 && !loc->shlib_disabled
7575 && solib_name_from_address (loc->pspace, loc->address)
7576 )
7577 {
7578 loc->shlib_disabled = 1;
7579 }
7580 }
7581 }
7582
7583 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7584 notification of unloaded_shlib. Only apply to enabled breakpoints,
7585 disabled ones can just stay disabled. */
7586
7587 static void
7588 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7589 {
7590 struct bp_location *loc, **locp_tmp;
7591 int disabled_shlib_breaks = 0;
7592
7593 ALL_BP_LOCATIONS (loc, locp_tmp)
7594 {
7595 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7596 struct breakpoint *b = loc->owner;
7597
7598 if (solib->pspace == loc->pspace
7599 && !loc->shlib_disabled
7600 && (((b->type == bp_breakpoint
7601 || b->type == bp_jit_event
7602 || b->type == bp_hardware_breakpoint)
7603 && (loc->loc_type == bp_loc_hardware_breakpoint
7604 || loc->loc_type == bp_loc_software_breakpoint))
7605 || is_tracepoint (b))
7606 && solib_contains_address_p (solib, loc->address))
7607 {
7608 loc->shlib_disabled = 1;
7609 /* At this point, we cannot rely on remove_breakpoint
7610 succeeding so we must mark the breakpoint as not inserted
7611 to prevent future errors occurring in remove_breakpoints. */
7612 loc->inserted = 0;
7613
7614 /* This may cause duplicate notifications for the same breakpoint. */
7615 gdb::observers::breakpoint_modified.notify (b);
7616
7617 if (!disabled_shlib_breaks)
7618 {
7619 target_terminal::ours_for_output ();
7620 warning (_("Temporarily disabling breakpoints "
7621 "for unloaded shared library \"%s\""),
7622 solib->so_name);
7623 }
7624 disabled_shlib_breaks = 1;
7625 }
7626 }
7627 }
7628
7629 /* Disable any breakpoints and tracepoints in OBJFILE upon
7630 notification of free_objfile. Only apply to enabled breakpoints,
7631 disabled ones can just stay disabled. */
7632
7633 static void
7634 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7635 {
7636 struct breakpoint *b;
7637
7638 if (objfile == NULL)
7639 return;
7640
7641 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7642 managed by the user with add-symbol-file/remove-symbol-file.
7643 Similarly to how breakpoints in shared libraries are handled in
7644 response to "nosharedlibrary", mark breakpoints in such modules
7645 shlib_disabled so they end up uninserted on the next global
7646 location list update. Shared libraries not loaded by the user
7647 aren't handled here -- they're already handled in
7648 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7649 solib_unloaded observer. We skip objfiles that are not
7650 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7651 main objfile). */
7652 if ((objfile->flags & OBJF_SHARED) == 0
7653 || (objfile->flags & OBJF_USERLOADED) == 0)
7654 return;
7655
7656 ALL_BREAKPOINTS (b)
7657 {
7658 struct bp_location *loc;
7659 int bp_modified = 0;
7660
7661 if (!is_breakpoint (b) && !is_tracepoint (b))
7662 continue;
7663
7664 for (loc = b->loc; loc != NULL; loc = loc->next)
7665 {
7666 CORE_ADDR loc_addr = loc->address;
7667
7668 if (loc->loc_type != bp_loc_hardware_breakpoint
7669 && loc->loc_type != bp_loc_software_breakpoint)
7670 continue;
7671
7672 if (loc->shlib_disabled != 0)
7673 continue;
7674
7675 if (objfile->pspace != loc->pspace)
7676 continue;
7677
7678 if (loc->loc_type != bp_loc_hardware_breakpoint
7679 && loc->loc_type != bp_loc_software_breakpoint)
7680 continue;
7681
7682 if (is_addr_in_objfile (loc_addr, objfile))
7683 {
7684 loc->shlib_disabled = 1;
7685 /* At this point, we don't know whether the object was
7686 unmapped from the inferior or not, so leave the
7687 inserted flag alone. We'll handle failure to
7688 uninsert quietly, in case the object was indeed
7689 unmapped. */
7690
7691 mark_breakpoint_location_modified (loc);
7692
7693 bp_modified = 1;
7694 }
7695 }
7696
7697 if (bp_modified)
7698 gdb::observers::breakpoint_modified.notify (b);
7699 }
7700 }
7701
7702 /* FORK & VFORK catchpoints. */
7703
7704 /* An instance of this type is used to represent a fork or vfork
7705 catchpoint. A breakpoint is really of this type iff its ops pointer points
7706 to CATCH_FORK_BREAKPOINT_OPS. */
7707
7708 struct fork_catchpoint : public breakpoint
7709 {
7710 /* Process id of a child process whose forking triggered this
7711 catchpoint. This field is only valid immediately after this
7712 catchpoint has triggered. */
7713 ptid_t forked_inferior_pid;
7714 };
7715
7716 /* Implement the "insert" breakpoint_ops method for fork
7717 catchpoints. */
7718
7719 static int
7720 insert_catch_fork (struct bp_location *bl)
7721 {
7722 return target_insert_fork_catchpoint (inferior_ptid.pid ());
7723 }
7724
7725 /* Implement the "remove" breakpoint_ops method for fork
7726 catchpoints. */
7727
7728 static int
7729 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
7730 {
7731 return target_remove_fork_catchpoint (inferior_ptid.pid ());
7732 }
7733
7734 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7735 catchpoints. */
7736
7737 static int
7738 breakpoint_hit_catch_fork (const struct bp_location *bl,
7739 const address_space *aspace, CORE_ADDR bp_addr,
7740 const struct target_waitstatus *ws)
7741 {
7742 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7743
7744 if (ws->kind != TARGET_WAITKIND_FORKED)
7745 return 0;
7746
7747 c->forked_inferior_pid = ws->value.related_pid;
7748 return 1;
7749 }
7750
7751 /* Implement the "print_it" breakpoint_ops method for fork
7752 catchpoints. */
7753
7754 static enum print_stop_action
7755 print_it_catch_fork (bpstat bs)
7756 {
7757 struct ui_out *uiout = current_uiout;
7758 struct breakpoint *b = bs->breakpoint_at;
7759 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7760
7761 annotate_catchpoint (b->number);
7762 maybe_print_thread_hit_breakpoint (uiout);
7763 if (b->disposition == disp_del)
7764 uiout->text ("Temporary catchpoint ");
7765 else
7766 uiout->text ("Catchpoint ");
7767 if (uiout->is_mi_like_p ())
7768 {
7769 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7770 uiout->field_string ("disp", bpdisp_text (b->disposition));
7771 }
7772 uiout->field_signed ("bkptno", b->number);
7773 uiout->text (" (forked process ");
7774 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7775 uiout->text ("), ");
7776 return PRINT_SRC_AND_LOC;
7777 }
7778
7779 /* Implement the "print_one" breakpoint_ops method for fork
7780 catchpoints. */
7781
7782 static void
7783 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7784 {
7785 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7786 struct value_print_options opts;
7787 struct ui_out *uiout = current_uiout;
7788
7789 get_user_print_options (&opts);
7790
7791 /* Field 4, the address, is omitted (which makes the columns not
7792 line up too nicely with the headers, but the effect is relatively
7793 readable). */
7794 if (opts.addressprint)
7795 uiout->field_skip ("addr");
7796 annotate_field (5);
7797 uiout->text ("fork");
7798 if (c->forked_inferior_pid != null_ptid)
7799 {
7800 uiout->text (", process ");
7801 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7802 uiout->spaces (1);
7803 }
7804
7805 if (uiout->is_mi_like_p ())
7806 uiout->field_string ("catch-type", "fork");
7807 }
7808
7809 /* Implement the "print_mention" breakpoint_ops method for fork
7810 catchpoints. */
7811
7812 static void
7813 print_mention_catch_fork (struct breakpoint *b)
7814 {
7815 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7816 }
7817
7818 /* Implement the "print_recreate" breakpoint_ops method for fork
7819 catchpoints. */
7820
7821 static void
7822 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7823 {
7824 fprintf_unfiltered (fp, "catch fork");
7825 print_recreate_thread (b, fp);
7826 }
7827
7828 /* The breakpoint_ops structure to be used in fork catchpoints. */
7829
7830 static struct breakpoint_ops catch_fork_breakpoint_ops;
7831
7832 /* Implement the "insert" breakpoint_ops method for vfork
7833 catchpoints. */
7834
7835 static int
7836 insert_catch_vfork (struct bp_location *bl)
7837 {
7838 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
7839 }
7840
7841 /* Implement the "remove" breakpoint_ops method for vfork
7842 catchpoints. */
7843
7844 static int
7845 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
7846 {
7847 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
7848 }
7849
7850 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7851 catchpoints. */
7852
7853 static int
7854 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7855 const address_space *aspace, CORE_ADDR bp_addr,
7856 const struct target_waitstatus *ws)
7857 {
7858 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7859
7860 if (ws->kind != TARGET_WAITKIND_VFORKED)
7861 return 0;
7862
7863 c->forked_inferior_pid = ws->value.related_pid;
7864 return 1;
7865 }
7866
7867 /* Implement the "print_it" breakpoint_ops method for vfork
7868 catchpoints. */
7869
7870 static enum print_stop_action
7871 print_it_catch_vfork (bpstat bs)
7872 {
7873 struct ui_out *uiout = current_uiout;
7874 struct breakpoint *b = bs->breakpoint_at;
7875 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7876
7877 annotate_catchpoint (b->number);
7878 maybe_print_thread_hit_breakpoint (uiout);
7879 if (b->disposition == disp_del)
7880 uiout->text ("Temporary catchpoint ");
7881 else
7882 uiout->text ("Catchpoint ");
7883 if (uiout->is_mi_like_p ())
7884 {
7885 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7886 uiout->field_string ("disp", bpdisp_text (b->disposition));
7887 }
7888 uiout->field_signed ("bkptno", b->number);
7889 uiout->text (" (vforked process ");
7890 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
7891 uiout->text ("), ");
7892 return PRINT_SRC_AND_LOC;
7893 }
7894
7895 /* Implement the "print_one" breakpoint_ops method for vfork
7896 catchpoints. */
7897
7898 static void
7899 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7900 {
7901 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7902 struct value_print_options opts;
7903 struct ui_out *uiout = current_uiout;
7904
7905 get_user_print_options (&opts);
7906 /* Field 4, the address, is omitted (which makes the columns not
7907 line up too nicely with the headers, but the effect is relatively
7908 readable). */
7909 if (opts.addressprint)
7910 uiout->field_skip ("addr");
7911 annotate_field (5);
7912 uiout->text ("vfork");
7913 if (c->forked_inferior_pid != null_ptid)
7914 {
7915 uiout->text (", process ");
7916 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
7917 uiout->spaces (1);
7918 }
7919
7920 if (uiout->is_mi_like_p ())
7921 uiout->field_string ("catch-type", "vfork");
7922 }
7923
7924 /* Implement the "print_mention" breakpoint_ops method for vfork
7925 catchpoints. */
7926
7927 static void
7928 print_mention_catch_vfork (struct breakpoint *b)
7929 {
7930 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7931 }
7932
7933 /* Implement the "print_recreate" breakpoint_ops method for vfork
7934 catchpoints. */
7935
7936 static void
7937 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7938 {
7939 fprintf_unfiltered (fp, "catch vfork");
7940 print_recreate_thread (b, fp);
7941 }
7942
7943 /* The breakpoint_ops structure to be used in vfork catchpoints. */
7944
7945 static struct breakpoint_ops catch_vfork_breakpoint_ops;
7946
7947 /* An instance of this type is used to represent an solib catchpoint.
7948 A breakpoint is really of this type iff its ops pointer points to
7949 CATCH_SOLIB_BREAKPOINT_OPS. */
7950
7951 struct solib_catchpoint : public breakpoint
7952 {
7953 ~solib_catchpoint () override;
7954
7955 /* True for "catch load", false for "catch unload". */
7956 bool is_load;
7957
7958 /* Regular expression to match, if any. COMPILED is only valid when
7959 REGEX is non-NULL. */
7960 char *regex;
7961 std::unique_ptr<compiled_regex> compiled;
7962 };
7963
7964 solib_catchpoint::~solib_catchpoint ()
7965 {
7966 xfree (this->regex);
7967 }
7968
7969 static int
7970 insert_catch_solib (struct bp_location *ignore)
7971 {
7972 return 0;
7973 }
7974
7975 static int
7976 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
7977 {
7978 return 0;
7979 }
7980
7981 static int
7982 breakpoint_hit_catch_solib (const struct bp_location *bl,
7983 const address_space *aspace,
7984 CORE_ADDR bp_addr,
7985 const struct target_waitstatus *ws)
7986 {
7987 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7988 struct breakpoint *other;
7989
7990 if (ws->kind == TARGET_WAITKIND_LOADED)
7991 return 1;
7992
7993 ALL_BREAKPOINTS (other)
7994 {
7995 struct bp_location *other_bl;
7996
7997 if (other == bl->owner)
7998 continue;
7999
8000 if (other->type != bp_shlib_event)
8001 continue;
8002
8003 if (self->pspace != NULL && other->pspace != self->pspace)
8004 continue;
8005
8006 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8007 {
8008 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8009 return 1;
8010 }
8011 }
8012
8013 return 0;
8014 }
8015
8016 static void
8017 check_status_catch_solib (struct bpstats *bs)
8018 {
8019 struct solib_catchpoint *self
8020 = (struct solib_catchpoint *) bs->breakpoint_at;
8021
8022 if (self->is_load)
8023 {
8024 for (so_list *iter : current_program_space->added_solibs)
8025 {
8026 if (!self->regex
8027 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
8028 return;
8029 }
8030 }
8031 else
8032 {
8033 for (const std::string &iter : current_program_space->deleted_solibs)
8034 {
8035 if (!self->regex
8036 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
8037 return;
8038 }
8039 }
8040
8041 bs->stop = 0;
8042 bs->print_it = print_it_noop;
8043 }
8044
8045 static enum print_stop_action
8046 print_it_catch_solib (bpstat bs)
8047 {
8048 struct breakpoint *b = bs->breakpoint_at;
8049 struct ui_out *uiout = current_uiout;
8050
8051 annotate_catchpoint (b->number);
8052 maybe_print_thread_hit_breakpoint (uiout);
8053 if (b->disposition == disp_del)
8054 uiout->text ("Temporary catchpoint ");
8055 else
8056 uiout->text ("Catchpoint ");
8057 uiout->field_signed ("bkptno", b->number);
8058 uiout->text ("\n");
8059 if (uiout->is_mi_like_p ())
8060 uiout->field_string ("disp", bpdisp_text (b->disposition));
8061 print_solib_event (1);
8062 return PRINT_SRC_AND_LOC;
8063 }
8064
8065 static void
8066 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8067 {
8068 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8069 struct value_print_options opts;
8070 struct ui_out *uiout = current_uiout;
8071
8072 get_user_print_options (&opts);
8073 /* Field 4, the address, is omitted (which makes the columns not
8074 line up too nicely with the headers, but the effect is relatively
8075 readable). */
8076 if (opts.addressprint)
8077 {
8078 annotate_field (4);
8079 uiout->field_skip ("addr");
8080 }
8081
8082 std::string msg;
8083 annotate_field (5);
8084 if (self->is_load)
8085 {
8086 if (self->regex)
8087 msg = string_printf (_("load of library matching %s"), self->regex);
8088 else
8089 msg = _("load of library");
8090 }
8091 else
8092 {
8093 if (self->regex)
8094 msg = string_printf (_("unload of library matching %s"), self->regex);
8095 else
8096 msg = _("unload of library");
8097 }
8098 uiout->field_string ("what", msg);
8099
8100 if (uiout->is_mi_like_p ())
8101 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8102 }
8103
8104 static void
8105 print_mention_catch_solib (struct breakpoint *b)
8106 {
8107 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8108
8109 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8110 self->is_load ? "load" : "unload");
8111 }
8112
8113 static void
8114 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8115 {
8116 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8117
8118 fprintf_unfiltered (fp, "%s %s",
8119 b->disposition == disp_del ? "tcatch" : "catch",
8120 self->is_load ? "load" : "unload");
8121 if (self->regex)
8122 fprintf_unfiltered (fp, " %s", self->regex);
8123 fprintf_unfiltered (fp, "\n");
8124 }
8125
8126 static struct breakpoint_ops catch_solib_breakpoint_ops;
8127
8128 /* See breakpoint.h. */
8129
8130 void
8131 add_solib_catchpoint (const char *arg, bool is_load, bool is_temp, bool enabled)
8132 {
8133 struct gdbarch *gdbarch = get_current_arch ();
8134
8135 if (!arg)
8136 arg = "";
8137 arg = skip_spaces (arg);
8138
8139 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
8140
8141 if (*arg != '\0')
8142 {
8143 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8144 _("Invalid regexp")));
8145 c->regex = xstrdup (arg);
8146 }
8147
8148 c->is_load = is_load;
8149 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
8150 &catch_solib_breakpoint_ops);
8151
8152 c->enable_state = enabled ? bp_enabled : bp_disabled;
8153
8154 install_breakpoint (0, std::move (c), 1);
8155 }
8156
8157 /* A helper function that does all the work for "catch load" and
8158 "catch unload". */
8159
8160 static void
8161 catch_load_or_unload (const char *arg, int from_tty, int is_load,
8162 struct cmd_list_element *command)
8163 {
8164 const int enabled = 1;
8165 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
8166
8167 add_solib_catchpoint (arg, is_load, temp, enabled);
8168 }
8169
8170 static void
8171 catch_load_command_1 (const char *arg, int from_tty,
8172 struct cmd_list_element *command)
8173 {
8174 catch_load_or_unload (arg, from_tty, 1, command);
8175 }
8176
8177 static void
8178 catch_unload_command_1 (const char *arg, int from_tty,
8179 struct cmd_list_element *command)
8180 {
8181 catch_load_or_unload (arg, from_tty, 0, command);
8182 }
8183
8184 /* See breakpoint.h. */
8185
8186 void
8187 init_catchpoint (struct breakpoint *b,
8188 struct gdbarch *gdbarch, bool temp,
8189 const char *cond_string,
8190 const struct breakpoint_ops *ops)
8191 {
8192 symtab_and_line sal;
8193 sal.pspace = current_program_space;
8194
8195 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8196
8197 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8198 b->disposition = temp ? disp_del : disp_donttouch;
8199 }
8200
8201 void
8202 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8203 {
8204 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8205 set_breakpoint_number (internal, b);
8206 if (is_tracepoint (b))
8207 set_tracepoint_count (breakpoint_count);
8208 if (!internal)
8209 mention (b);
8210 gdb::observers::breakpoint_created.notify (b);
8211
8212 if (update_gll)
8213 update_global_location_list (UGLL_MAY_INSERT);
8214 }
8215
8216 static void
8217 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8218 bool temp, const char *cond_string,
8219 const struct breakpoint_ops *ops)
8220 {
8221 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
8222
8223 init_catchpoint (c.get (), gdbarch, temp, cond_string, ops);
8224
8225 c->forked_inferior_pid = null_ptid;
8226
8227 install_breakpoint (0, std::move (c), 1);
8228 }
8229
8230 /* Exec catchpoints. */
8231
8232 /* An instance of this type is used to represent an exec catchpoint.
8233 A breakpoint is really of this type iff its ops pointer points to
8234 CATCH_EXEC_BREAKPOINT_OPS. */
8235
8236 struct exec_catchpoint : public breakpoint
8237 {
8238 ~exec_catchpoint () override;
8239
8240 /* Filename of a program whose exec triggered this catchpoint.
8241 This field is only valid immediately after this catchpoint has
8242 triggered. */
8243 char *exec_pathname;
8244 };
8245
8246 /* Exec catchpoint destructor. */
8247
8248 exec_catchpoint::~exec_catchpoint ()
8249 {
8250 xfree (this->exec_pathname);
8251 }
8252
8253 static int
8254 insert_catch_exec (struct bp_location *bl)
8255 {
8256 return target_insert_exec_catchpoint (inferior_ptid.pid ());
8257 }
8258
8259 static int
8260 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8261 {
8262 return target_remove_exec_catchpoint (inferior_ptid.pid ());
8263 }
8264
8265 static int
8266 breakpoint_hit_catch_exec (const struct bp_location *bl,
8267 const address_space *aspace, CORE_ADDR bp_addr,
8268 const struct target_waitstatus *ws)
8269 {
8270 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8271
8272 if (ws->kind != TARGET_WAITKIND_EXECD)
8273 return 0;
8274
8275 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8276 return 1;
8277 }
8278
8279 static enum print_stop_action
8280 print_it_catch_exec (bpstat bs)
8281 {
8282 struct ui_out *uiout = current_uiout;
8283 struct breakpoint *b = bs->breakpoint_at;
8284 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8285
8286 annotate_catchpoint (b->number);
8287 maybe_print_thread_hit_breakpoint (uiout);
8288 if (b->disposition == disp_del)
8289 uiout->text ("Temporary catchpoint ");
8290 else
8291 uiout->text ("Catchpoint ");
8292 if (uiout->is_mi_like_p ())
8293 {
8294 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8295 uiout->field_string ("disp", bpdisp_text (b->disposition));
8296 }
8297 uiout->field_signed ("bkptno", b->number);
8298 uiout->text (" (exec'd ");
8299 uiout->field_string ("new-exec", c->exec_pathname);
8300 uiout->text ("), ");
8301
8302 return PRINT_SRC_AND_LOC;
8303 }
8304
8305 static void
8306 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8307 {
8308 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8309 struct value_print_options opts;
8310 struct ui_out *uiout = current_uiout;
8311
8312 get_user_print_options (&opts);
8313
8314 /* Field 4, the address, is omitted (which makes the columns
8315 not line up too nicely with the headers, but the effect
8316 is relatively readable). */
8317 if (opts.addressprint)
8318 uiout->field_skip ("addr");
8319 annotate_field (5);
8320 uiout->text ("exec");
8321 if (c->exec_pathname != NULL)
8322 {
8323 uiout->text (", program \"");
8324 uiout->field_string ("what", c->exec_pathname);
8325 uiout->text ("\" ");
8326 }
8327
8328 if (uiout->is_mi_like_p ())
8329 uiout->field_string ("catch-type", "exec");
8330 }
8331
8332 static void
8333 print_mention_catch_exec (struct breakpoint *b)
8334 {
8335 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8336 }
8337
8338 /* Implement the "print_recreate" breakpoint_ops method for exec
8339 catchpoints. */
8340
8341 static void
8342 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8343 {
8344 fprintf_unfiltered (fp, "catch exec");
8345 print_recreate_thread (b, fp);
8346 }
8347
8348 static struct breakpoint_ops catch_exec_breakpoint_ops;
8349
8350 static int
8351 hw_breakpoint_used_count (void)
8352 {
8353 int i = 0;
8354 struct breakpoint *b;
8355 struct bp_location *bl;
8356
8357 ALL_BREAKPOINTS (b)
8358 {
8359 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8360 for (bl = b->loc; bl; bl = bl->next)
8361 {
8362 /* Special types of hardware breakpoints may use more than
8363 one register. */
8364 i += b->ops->resources_needed (bl);
8365 }
8366 }
8367
8368 return i;
8369 }
8370
8371 /* Returns the resources B would use if it were a hardware
8372 watchpoint. */
8373
8374 static int
8375 hw_watchpoint_use_count (struct breakpoint *b)
8376 {
8377 int i = 0;
8378 struct bp_location *bl;
8379
8380 if (!breakpoint_enabled (b))
8381 return 0;
8382
8383 for (bl = b->loc; bl; bl = bl->next)
8384 {
8385 /* Special types of hardware watchpoints may use more than
8386 one register. */
8387 i += b->ops->resources_needed (bl);
8388 }
8389
8390 return i;
8391 }
8392
8393 /* Returns the sum the used resources of all hardware watchpoints of
8394 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8395 the sum of the used resources of all hardware watchpoints of other
8396 types _not_ TYPE. */
8397
8398 static int
8399 hw_watchpoint_used_count_others (struct breakpoint *except,
8400 enum bptype type, int *other_type_used)
8401 {
8402 int i = 0;
8403 struct breakpoint *b;
8404
8405 *other_type_used = 0;
8406 ALL_BREAKPOINTS (b)
8407 {
8408 if (b == except)
8409 continue;
8410 if (!breakpoint_enabled (b))
8411 continue;
8412
8413 if (b->type == type)
8414 i += hw_watchpoint_use_count (b);
8415 else if (is_hardware_watchpoint (b))
8416 *other_type_used = 1;
8417 }
8418
8419 return i;
8420 }
8421
8422 void
8423 disable_watchpoints_before_interactive_call_start (void)
8424 {
8425 struct breakpoint *b;
8426
8427 ALL_BREAKPOINTS (b)
8428 {
8429 if (is_watchpoint (b) && breakpoint_enabled (b))
8430 {
8431 b->enable_state = bp_call_disabled;
8432 update_global_location_list (UGLL_DONT_INSERT);
8433 }
8434 }
8435 }
8436
8437 void
8438 enable_watchpoints_after_interactive_call_stop (void)
8439 {
8440 struct breakpoint *b;
8441
8442 ALL_BREAKPOINTS (b)
8443 {
8444 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8445 {
8446 b->enable_state = bp_enabled;
8447 update_global_location_list (UGLL_MAY_INSERT);
8448 }
8449 }
8450 }
8451
8452 void
8453 disable_breakpoints_before_startup (void)
8454 {
8455 current_program_space->executing_startup = 1;
8456 update_global_location_list (UGLL_DONT_INSERT);
8457 }
8458
8459 void
8460 enable_breakpoints_after_startup (void)
8461 {
8462 current_program_space->executing_startup = 0;
8463 breakpoint_re_set ();
8464 }
8465
8466 /* Create a new single-step breakpoint for thread THREAD, with no
8467 locations. */
8468
8469 static struct breakpoint *
8470 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8471 {
8472 std::unique_ptr<breakpoint> b (new breakpoint ());
8473
8474 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
8475 &momentary_breakpoint_ops);
8476
8477 b->disposition = disp_donttouch;
8478 b->frame_id = null_frame_id;
8479
8480 b->thread = thread;
8481 gdb_assert (b->thread != 0);
8482
8483 return add_to_breakpoint_chain (std::move (b));
8484 }
8485
8486 /* Set a momentary breakpoint of type TYPE at address specified by
8487 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8488 frame. */
8489
8490 breakpoint_up
8491 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8492 struct frame_id frame_id, enum bptype type)
8493 {
8494 struct breakpoint *b;
8495
8496 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8497 tail-called one. */
8498 gdb_assert (!frame_id_artificial_p (frame_id));
8499
8500 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8501 b->enable_state = bp_enabled;
8502 b->disposition = disp_donttouch;
8503 b->frame_id = frame_id;
8504
8505 b->thread = inferior_thread ()->global_num;
8506
8507 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8508
8509 return breakpoint_up (b);
8510 }
8511
8512 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8513 The new breakpoint will have type TYPE, use OPS as its
8514 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8515
8516 static struct breakpoint *
8517 momentary_breakpoint_from_master (struct breakpoint *orig,
8518 enum bptype type,
8519 const struct breakpoint_ops *ops,
8520 int loc_enabled)
8521 {
8522 struct breakpoint *copy;
8523
8524 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8525 copy->loc = allocate_bp_location (copy);
8526 set_breakpoint_location_function (copy->loc);
8527
8528 copy->loc->gdbarch = orig->loc->gdbarch;
8529 copy->loc->requested_address = orig->loc->requested_address;
8530 copy->loc->address = orig->loc->address;
8531 copy->loc->section = orig->loc->section;
8532 copy->loc->pspace = orig->loc->pspace;
8533 copy->loc->probe = orig->loc->probe;
8534 copy->loc->line_number = orig->loc->line_number;
8535 copy->loc->symtab = orig->loc->symtab;
8536 copy->loc->enabled = loc_enabled;
8537 copy->frame_id = orig->frame_id;
8538 copy->thread = orig->thread;
8539 copy->pspace = orig->pspace;
8540
8541 copy->enable_state = bp_enabled;
8542 copy->disposition = disp_donttouch;
8543 copy->number = internal_breakpoint_number--;
8544
8545 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8546 return copy;
8547 }
8548
8549 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8550 ORIG is NULL. */
8551
8552 struct breakpoint *
8553 clone_momentary_breakpoint (struct breakpoint *orig)
8554 {
8555 /* If there's nothing to clone, then return nothing. */
8556 if (orig == NULL)
8557 return NULL;
8558
8559 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8560 }
8561
8562 breakpoint_up
8563 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8564 enum bptype type)
8565 {
8566 struct symtab_and_line sal;
8567
8568 sal = find_pc_line (pc, 0);
8569 sal.pc = pc;
8570 sal.section = find_pc_overlay (pc);
8571 sal.explicit_pc = 1;
8572
8573 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8574 }
8575 \f
8576
8577 /* Tell the user we have just set a breakpoint B. */
8578
8579 static void
8580 mention (struct breakpoint *b)
8581 {
8582 b->ops->print_mention (b);
8583 current_uiout->text ("\n");
8584 }
8585 \f
8586
8587 static bool bp_loc_is_permanent (struct bp_location *loc);
8588
8589 /* Handle "set breakpoint auto-hw on".
8590
8591 If the explicitly specified breakpoint type is not hardware
8592 breakpoint, check the memory map to see whether the breakpoint
8593 address is in read-only memory.
8594
8595 - location type is not hardware breakpoint, memory is read-only.
8596 We change the type of the location to hardware breakpoint.
8597
8598 - location type is hardware breakpoint, memory is read-write. This
8599 means we've previously made the location hardware one, but then the
8600 memory map changed, so we undo.
8601 */
8602
8603 static void
8604 handle_automatic_hardware_breakpoints (bp_location *bl)
8605 {
8606 if (automatic_hardware_breakpoints
8607 && bl->owner->type != bp_hardware_breakpoint
8608 && (bl->loc_type == bp_loc_software_breakpoint
8609 || bl->loc_type == bp_loc_hardware_breakpoint))
8610 {
8611 /* When breakpoints are removed, remove_breakpoints will use
8612 location types we've just set here, the only possible problem
8613 is that memory map has changed during running program, but
8614 it's not going to work anyway with current gdb. */
8615 mem_region *mr = lookup_mem_region (bl->address);
8616
8617 if (mr != nullptr)
8618 {
8619 enum bp_loc_type new_type;
8620
8621 if (mr->attrib.mode != MEM_RW)
8622 new_type = bp_loc_hardware_breakpoint;
8623 else
8624 new_type = bp_loc_software_breakpoint;
8625
8626 if (new_type != bl->loc_type)
8627 {
8628 static bool said = false;
8629
8630 bl->loc_type = new_type;
8631 if (!said)
8632 {
8633 fprintf_filtered (gdb_stdout,
8634 _("Note: automatically using "
8635 "hardware breakpoints for "
8636 "read-only addresses.\n"));
8637 said = true;
8638 }
8639 }
8640 }
8641 }
8642 }
8643
8644 static struct bp_location *
8645 add_location_to_breakpoint (struct breakpoint *b,
8646 const struct symtab_and_line *sal)
8647 {
8648 struct bp_location *loc, **tmp;
8649 CORE_ADDR adjusted_address;
8650 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8651
8652 if (loc_gdbarch == NULL)
8653 loc_gdbarch = b->gdbarch;
8654
8655 /* Adjust the breakpoint's address prior to allocating a location.
8656 Once we call allocate_bp_location(), that mostly uninitialized
8657 location will be placed on the location chain. Adjustment of the
8658 breakpoint may cause target_read_memory() to be called and we do
8659 not want its scan of the location chain to find a breakpoint and
8660 location that's only been partially initialized. */
8661 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8662 sal->pc, b->type);
8663
8664 /* Sort the locations by their ADDRESS. */
8665 loc = allocate_bp_location (b);
8666 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8667 tmp = &((*tmp)->next))
8668 ;
8669 loc->next = *tmp;
8670 *tmp = loc;
8671
8672 loc->requested_address = sal->pc;
8673 loc->address = adjusted_address;
8674 loc->pspace = sal->pspace;
8675 loc->probe.prob = sal->prob;
8676 loc->probe.objfile = sal->objfile;
8677 gdb_assert (loc->pspace != NULL);
8678 loc->section = sal->section;
8679 loc->gdbarch = loc_gdbarch;
8680 loc->line_number = sal->line;
8681 loc->symtab = sal->symtab;
8682 loc->symbol = sal->symbol;
8683 loc->msymbol = sal->msymbol;
8684 loc->objfile = sal->objfile;
8685
8686 set_breakpoint_location_function (loc);
8687
8688 /* While by definition, permanent breakpoints are already present in the
8689 code, we don't mark the location as inserted. Normally one would expect
8690 that GDB could rely on that breakpoint instruction to stop the program,
8691 thus removing the need to insert its own breakpoint, except that executing
8692 the breakpoint instruction can kill the target instead of reporting a
8693 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8694 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8695 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8696 breakpoint be inserted normally results in QEMU knowing about the GDB
8697 breakpoint, and thus trap before the breakpoint instruction is executed.
8698 (If GDB later needs to continue execution past the permanent breakpoint,
8699 it manually increments the PC, thus avoiding executing the breakpoint
8700 instruction.) */
8701 if (bp_loc_is_permanent (loc))
8702 loc->permanent = 1;
8703
8704 return loc;
8705 }
8706 \f
8707
8708 /* Return true if LOC is pointing to a permanent breakpoint,
8709 return false otherwise. */
8710
8711 static bool
8712 bp_loc_is_permanent (struct bp_location *loc)
8713 {
8714 gdb_assert (loc != NULL);
8715
8716 /* If we have a non-breakpoint-backed catchpoint or a software
8717 watchpoint, just return 0. We should not attempt to read from
8718 the addresses the locations of these breakpoint types point to.
8719 gdbarch_program_breakpoint_here_p, below, will attempt to read
8720 memory. */
8721 if (!bl_address_is_meaningful (loc))
8722 return false;
8723
8724 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8725 switch_to_program_space_and_thread (loc->pspace);
8726 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
8727 }
8728
8729 /* Build a command list for the dprintf corresponding to the current
8730 settings of the dprintf style options. */
8731
8732 static void
8733 update_dprintf_command_list (struct breakpoint *b)
8734 {
8735 char *dprintf_args = b->extra_string;
8736 char *printf_line = NULL;
8737
8738 if (!dprintf_args)
8739 return;
8740
8741 dprintf_args = skip_spaces (dprintf_args);
8742
8743 /* Allow a comma, as it may have terminated a location, but don't
8744 insist on it. */
8745 if (*dprintf_args == ',')
8746 ++dprintf_args;
8747 dprintf_args = skip_spaces (dprintf_args);
8748
8749 if (*dprintf_args != '"')
8750 error (_("Bad format string, missing '\"'."));
8751
8752 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8753 printf_line = xstrprintf ("printf %s", dprintf_args);
8754 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8755 {
8756 if (!dprintf_function)
8757 error (_("No function supplied for dprintf call"));
8758
8759 if (dprintf_channel && strlen (dprintf_channel) > 0)
8760 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8761 dprintf_function,
8762 dprintf_channel,
8763 dprintf_args);
8764 else
8765 printf_line = xstrprintf ("call (void) %s (%s)",
8766 dprintf_function,
8767 dprintf_args);
8768 }
8769 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8770 {
8771 if (target_can_run_breakpoint_commands ())
8772 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8773 else
8774 {
8775 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8776 printf_line = xstrprintf ("printf %s", dprintf_args);
8777 }
8778 }
8779 else
8780 internal_error (__FILE__, __LINE__,
8781 _("Invalid dprintf style."));
8782
8783 gdb_assert (printf_line != NULL);
8784
8785 /* Manufacture a printf sequence. */
8786 struct command_line *printf_cmd_line
8787 = new struct command_line (simple_control, printf_line);
8788 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8789 command_lines_deleter ()));
8790 }
8791
8792 /* Update all dprintf commands, making their command lists reflect
8793 current style settings. */
8794
8795 static void
8796 update_dprintf_commands (const char *args, int from_tty,
8797 struct cmd_list_element *c)
8798 {
8799 struct breakpoint *b;
8800
8801 ALL_BREAKPOINTS (b)
8802 {
8803 if (b->type == bp_dprintf)
8804 update_dprintf_command_list (b);
8805 }
8806 }
8807
8808 /* Create a breakpoint with SAL as location. Use LOCATION
8809 as a description of the location, and COND_STRING
8810 as condition expression. If LOCATION is NULL then create an
8811 "address location" from the address in the SAL. */
8812
8813 static void
8814 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8815 gdb::array_view<const symtab_and_line> sals,
8816 event_location_up &&location,
8817 gdb::unique_xmalloc_ptr<char> filter,
8818 gdb::unique_xmalloc_ptr<char> cond_string,
8819 gdb::unique_xmalloc_ptr<char> extra_string,
8820 enum bptype type, enum bpdisp disposition,
8821 int thread, int task, int ignore_count,
8822 const struct breakpoint_ops *ops, int from_tty,
8823 int enabled, int internal, unsigned flags,
8824 int display_canonical)
8825 {
8826 int i;
8827
8828 if (type == bp_hardware_breakpoint)
8829 {
8830 int target_resources_ok;
8831
8832 i = hw_breakpoint_used_count ();
8833 target_resources_ok =
8834 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8835 i + 1, 0);
8836 if (target_resources_ok == 0)
8837 error (_("No hardware breakpoint support in the target."));
8838 else if (target_resources_ok < 0)
8839 error (_("Hardware breakpoints used exceeds limit."));
8840 }
8841
8842 gdb_assert (!sals.empty ());
8843
8844 for (const auto &sal : sals)
8845 {
8846 struct bp_location *loc;
8847
8848 if (from_tty)
8849 {
8850 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8851 if (!loc_gdbarch)
8852 loc_gdbarch = gdbarch;
8853
8854 describe_other_breakpoints (loc_gdbarch,
8855 sal.pspace, sal.pc, sal.section, thread);
8856 }
8857
8858 if (&sal == &sals[0])
8859 {
8860 init_raw_breakpoint (b, gdbarch, sal, type, ops);
8861 b->thread = thread;
8862 b->task = task;
8863
8864 b->cond_string = cond_string.release ();
8865 b->extra_string = extra_string.release ();
8866 b->ignore_count = ignore_count;
8867 b->enable_state = enabled ? bp_enabled : bp_disabled;
8868 b->disposition = disposition;
8869
8870 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8871 b->loc->inserted = 1;
8872
8873 if (type == bp_static_tracepoint)
8874 {
8875 struct tracepoint *t = (struct tracepoint *) b;
8876 struct static_tracepoint_marker marker;
8877
8878 if (strace_marker_p (b))
8879 {
8880 /* We already know the marker exists, otherwise, we
8881 wouldn't see a sal for it. */
8882 const char *p
8883 = &event_location_to_string (b->location.get ())[3];
8884 const char *endp;
8885
8886 p = skip_spaces (p);
8887
8888 endp = skip_to_space (p);
8889
8890 t->static_trace_marker_id.assign (p, endp - p);
8891
8892 printf_filtered (_("Probed static tracepoint "
8893 "marker \"%s\"\n"),
8894 t->static_trace_marker_id.c_str ());
8895 }
8896 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8897 {
8898 t->static_trace_marker_id = std::move (marker.str_id);
8899
8900 printf_filtered (_("Probed static tracepoint "
8901 "marker \"%s\"\n"),
8902 t->static_trace_marker_id.c_str ());
8903 }
8904 else
8905 warning (_("Couldn't determine the static "
8906 "tracepoint marker to probe"));
8907 }
8908
8909 loc = b->loc;
8910 }
8911 else
8912 {
8913 loc = add_location_to_breakpoint (b, &sal);
8914 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8915 loc->inserted = 1;
8916 }
8917
8918 /* Do not set breakpoint locations conditions yet. As locations
8919 are inserted, they get sorted based on their addresses. Let
8920 the list stabilize to have reliable location numbers. */
8921
8922 /* Dynamic printf requires and uses additional arguments on the
8923 command line, otherwise it's an error. */
8924 if (type == bp_dprintf)
8925 {
8926 if (b->extra_string)
8927 update_dprintf_command_list (b);
8928 else
8929 error (_("Format string required"));
8930 }
8931 else if (b->extra_string)
8932 error (_("Garbage '%s' at end of command"), b->extra_string);
8933 }
8934
8935
8936 /* The order of the locations is now stable. Set the location
8937 condition using the location's number. */
8938 int loc_num = 1;
8939 for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)
8940 {
8941 if (b->cond_string != nullptr)
8942 set_breakpoint_location_condition (b->cond_string, loc, b->number,
8943 loc_num);
8944
8945 ++loc_num;
8946 }
8947
8948 b->display_canonical = display_canonical;
8949 if (location != NULL)
8950 b->location = std::move (location);
8951 else
8952 b->location = new_address_location (b->loc->address, NULL, 0);
8953 b->filter = std::move (filter);
8954 }
8955
8956 static void
8957 create_breakpoint_sal (struct gdbarch *gdbarch,
8958 gdb::array_view<const symtab_and_line> sals,
8959 event_location_up &&location,
8960 gdb::unique_xmalloc_ptr<char> filter,
8961 gdb::unique_xmalloc_ptr<char> cond_string,
8962 gdb::unique_xmalloc_ptr<char> extra_string,
8963 enum bptype type, enum bpdisp disposition,
8964 int thread, int task, int ignore_count,
8965 const struct breakpoint_ops *ops, int from_tty,
8966 int enabled, int internal, unsigned flags,
8967 int display_canonical)
8968 {
8969 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
8970
8971 init_breakpoint_sal (b.get (), gdbarch,
8972 sals, std::move (location),
8973 std::move (filter),
8974 std::move (cond_string),
8975 std::move (extra_string),
8976 type, disposition,
8977 thread, task, ignore_count,
8978 ops, from_tty,
8979 enabled, internal, flags,
8980 display_canonical);
8981
8982 install_breakpoint (internal, std::move (b), 0);
8983 }
8984
8985 /* Add SALS.nelts breakpoints to the breakpoint table. For each
8986 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8987 value. COND_STRING, if not NULL, specified the condition to be
8988 used for all breakpoints. Essentially the only case where
8989 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8990 function. In that case, it's still not possible to specify
8991 separate conditions for different overloaded functions, so
8992 we take just a single condition string.
8993
8994 NOTE: If the function succeeds, the caller is expected to cleanup
8995 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
8996 array contents). If the function fails (error() is called), the
8997 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
8998 COND and SALS arrays and each of those arrays contents. */
8999
9000 static void
9001 create_breakpoints_sal (struct gdbarch *gdbarch,
9002 struct linespec_result *canonical,
9003 gdb::unique_xmalloc_ptr<char> cond_string,
9004 gdb::unique_xmalloc_ptr<char> extra_string,
9005 enum bptype type, enum bpdisp disposition,
9006 int thread, int task, int ignore_count,
9007 const struct breakpoint_ops *ops, int from_tty,
9008 int enabled, int internal, unsigned flags)
9009 {
9010 if (canonical->pre_expanded)
9011 gdb_assert (canonical->lsals.size () == 1);
9012
9013 for (const auto &lsal : canonical->lsals)
9014 {
9015 /* Note that 'location' can be NULL in the case of a plain
9016 'break', without arguments. */
9017 event_location_up location
9018 = (canonical->location != NULL
9019 ? copy_event_location (canonical->location.get ()) : NULL);
9020 gdb::unique_xmalloc_ptr<char> filter_string
9021 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
9022
9023 create_breakpoint_sal (gdbarch, lsal.sals,
9024 std::move (location),
9025 std::move (filter_string),
9026 std::move (cond_string),
9027 std::move (extra_string),
9028 type, disposition,
9029 thread, task, ignore_count, ops,
9030 from_tty, enabled, internal, flags,
9031 canonical->special_display);
9032 }
9033 }
9034
9035 /* Parse LOCATION which is assumed to be a SAL specification possibly
9036 followed by conditionals. On return, SALS contains an array of SAL
9037 addresses found. LOCATION points to the end of the SAL (for
9038 linespec locations).
9039
9040 The array and the line spec strings are allocated on the heap, it is
9041 the caller's responsibility to free them. */
9042
9043 static void
9044 parse_breakpoint_sals (struct event_location *location,
9045 struct linespec_result *canonical)
9046 {
9047 struct symtab_and_line cursal;
9048
9049 if (event_location_type (location) == LINESPEC_LOCATION)
9050 {
9051 const char *spec = get_linespec_location (location)->spec_string;
9052
9053 if (spec == NULL)
9054 {
9055 /* The last displayed codepoint, if it's valid, is our default
9056 breakpoint address. */
9057 if (last_displayed_sal_is_valid ())
9058 {
9059 /* Set sal's pspace, pc, symtab, and line to the values
9060 corresponding to the last call to print_frame_info.
9061 Be sure to reinitialize LINE with NOTCURRENT == 0
9062 as the breakpoint line number is inappropriate otherwise.
9063 find_pc_line would adjust PC, re-set it back. */
9064 symtab_and_line sal = get_last_displayed_sal ();
9065 CORE_ADDR pc = sal.pc;
9066
9067 sal = find_pc_line (pc, 0);
9068
9069 /* "break" without arguments is equivalent to "break *PC"
9070 where PC is the last displayed codepoint's address. So
9071 make sure to set sal.explicit_pc to prevent GDB from
9072 trying to expand the list of sals to include all other
9073 instances with the same symtab and line. */
9074 sal.pc = pc;
9075 sal.explicit_pc = 1;
9076
9077 struct linespec_sals lsal;
9078 lsal.sals = {sal};
9079 lsal.canonical = NULL;
9080
9081 canonical->lsals.push_back (std::move (lsal));
9082 return;
9083 }
9084 else
9085 error (_("No default breakpoint address now."));
9086 }
9087 }
9088
9089 /* Force almost all breakpoints to be in terms of the
9090 current_source_symtab (which is decode_line_1's default).
9091 This should produce the results we want almost all of the
9092 time while leaving default_breakpoint_* alone.
9093
9094 ObjC: However, don't match an Objective-C method name which
9095 may have a '+' or '-' succeeded by a '['. */
9096 cursal = get_current_source_symtab_and_line ();
9097 if (last_displayed_sal_is_valid ())
9098 {
9099 const char *spec = NULL;
9100
9101 if (event_location_type (location) == LINESPEC_LOCATION)
9102 spec = get_linespec_location (location)->spec_string;
9103
9104 if (!cursal.symtab
9105 || (spec != NULL
9106 && strchr ("+-", spec[0]) != NULL
9107 && spec[1] != '['))
9108 {
9109 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9110 get_last_displayed_symtab (),
9111 get_last_displayed_line (),
9112 canonical, NULL, NULL);
9113 return;
9114 }
9115 }
9116
9117 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9118 cursal.symtab, cursal.line, canonical, NULL, NULL);
9119 }
9120
9121
9122 /* Convert each SAL into a real PC. Verify that the PC can be
9123 inserted as a breakpoint. If it can't throw an error. */
9124
9125 static void
9126 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
9127 {
9128 for (auto &sal : sals)
9129 resolve_sal_pc (&sal);
9130 }
9131
9132 /* Fast tracepoints may have restrictions on valid locations. For
9133 instance, a fast tracepoint using a jump instead of a trap will
9134 likely have to overwrite more bytes than a trap would, and so can
9135 only be placed where the instruction is longer than the jump, or a
9136 multi-instruction sequence does not have a jump into the middle of
9137 it, etc. */
9138
9139 static void
9140 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9141 gdb::array_view<const symtab_and_line> sals)
9142 {
9143 for (const auto &sal : sals)
9144 {
9145 struct gdbarch *sarch;
9146
9147 sarch = get_sal_arch (sal);
9148 /* We fall back to GDBARCH if there is no architecture
9149 associated with SAL. */
9150 if (sarch == NULL)
9151 sarch = gdbarch;
9152 std::string msg;
9153 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
9154 error (_("May not have a fast tracepoint at %s%s"),
9155 paddress (sarch, sal.pc), msg.c_str ());
9156 }
9157 }
9158
9159 /* Given TOK, a string specification of condition and thread, as
9160 accepted by the 'break' command, extract the condition
9161 string and thread number and set *COND_STRING and *THREAD.
9162 PC identifies the context at which the condition should be parsed.
9163 If no condition is found, *COND_STRING is set to NULL.
9164 If no thread is found, *THREAD is set to -1. */
9165
9166 static void
9167 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9168 char **cond_string, int *thread, int *task,
9169 char **rest)
9170 {
9171 *cond_string = NULL;
9172 *thread = -1;
9173 *task = 0;
9174 *rest = NULL;
9175
9176 while (tok && *tok)
9177 {
9178 const char *end_tok;
9179 int toklen;
9180 const char *cond_start = NULL;
9181 const char *cond_end = NULL;
9182
9183 tok = skip_spaces (tok);
9184
9185 if ((*tok == '"' || *tok == ',') && rest)
9186 {
9187 *rest = savestring (tok, strlen (tok));
9188 return;
9189 }
9190
9191 end_tok = skip_to_space (tok);
9192
9193 toklen = end_tok - tok;
9194
9195 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9196 {
9197 tok = cond_start = end_tok + 1;
9198 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9199 cond_end = tok;
9200 *cond_string = savestring (cond_start, cond_end - cond_start);
9201 }
9202 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9203 {
9204 const char *tmptok;
9205 struct thread_info *thr;
9206
9207 tok = end_tok + 1;
9208 thr = parse_thread_id (tok, &tmptok);
9209 if (tok == tmptok)
9210 error (_("Junk after thread keyword."));
9211 *thread = thr->global_num;
9212 tok = tmptok;
9213 }
9214 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9215 {
9216 char *tmptok;
9217
9218 tok = end_tok + 1;
9219 *task = strtol (tok, &tmptok, 0);
9220 if (tok == tmptok)
9221 error (_("Junk after task keyword."));
9222 if (!valid_task_id (*task))
9223 error (_("Unknown task %d."), *task);
9224 tok = tmptok;
9225 }
9226 else if (rest)
9227 {
9228 *rest = savestring (tok, strlen (tok));
9229 return;
9230 }
9231 else
9232 error (_("Junk at end of arguments."));
9233 }
9234 }
9235
9236 /* Call 'find_condition_and_thread' for each sal in SALS until a parse
9237 succeeds. The parsed values are written to COND_STRING, THREAD,
9238 TASK, and REST. See the comment of 'find_condition_and_thread'
9239 for the description of these parameters and INPUT. */
9240
9241 static void
9242 find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
9243 const char *input, char **cond_string,
9244 int *thread, int *task, char **rest)
9245 {
9246 int num_failures = 0;
9247 for (auto &sal : sals)
9248 {
9249 char *cond = nullptr;
9250 int thread_id = 0;
9251 int task_id = 0;
9252 char *remaining = nullptr;
9253
9254 /* Here we want to parse 'arg' to separate condition from thread
9255 number. But because parsing happens in a context and the
9256 contexts of sals might be different, try each until there is
9257 success. Finding one successful parse is sufficient for our
9258 goal. When setting the breakpoint we'll re-parse the
9259 condition in the context of each sal. */
9260 try
9261 {
9262 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
9263 &task_id, &remaining);
9264 *cond_string = cond;
9265 *thread = thread_id;
9266 *task = task_id;
9267 *rest = remaining;
9268 break;
9269 }
9270 catch (const gdb_exception_error &e)
9271 {
9272 num_failures++;
9273 /* If no sal remains, do not continue. */
9274 if (num_failures == sals.size ())
9275 throw;
9276 }
9277 }
9278 }
9279
9280 /* Decode a static tracepoint marker spec. */
9281
9282 static std::vector<symtab_and_line>
9283 decode_static_tracepoint_spec (const char **arg_p)
9284 {
9285 const char *p = &(*arg_p)[3];
9286 const char *endp;
9287
9288 p = skip_spaces (p);
9289
9290 endp = skip_to_space (p);
9291
9292 std::string marker_str (p, endp - p);
9293
9294 std::vector<static_tracepoint_marker> markers
9295 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9296 if (markers.empty ())
9297 error (_("No known static tracepoint marker named %s"),
9298 marker_str.c_str ());
9299
9300 std::vector<symtab_and_line> sals;
9301 sals.reserve (markers.size ());
9302
9303 for (const static_tracepoint_marker &marker : markers)
9304 {
9305 symtab_and_line sal = find_pc_line (marker.address, 0);
9306 sal.pc = marker.address;
9307 sals.push_back (sal);
9308 }
9309
9310 *arg_p = endp;
9311 return sals;
9312 }
9313
9314 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9315 according to IS_TRACEPOINT. */
9316
9317 static const struct breakpoint_ops *
9318 breakpoint_ops_for_event_location_type (enum event_location_type location_type,
9319 bool is_tracepoint)
9320 {
9321 if (is_tracepoint)
9322 {
9323 if (location_type == PROBE_LOCATION)
9324 return &tracepoint_probe_breakpoint_ops;
9325 else
9326 return &tracepoint_breakpoint_ops;
9327 }
9328 else
9329 {
9330 if (location_type == PROBE_LOCATION)
9331 return &bkpt_probe_breakpoint_ops;
9332 else
9333 return &bkpt_breakpoint_ops;
9334 }
9335 }
9336
9337 /* See breakpoint.h. */
9338
9339 const struct breakpoint_ops *
9340 breakpoint_ops_for_event_location (const struct event_location *location,
9341 bool is_tracepoint)
9342 {
9343 if (location != nullptr)
9344 return breakpoint_ops_for_event_location_type
9345 (event_location_type (location), is_tracepoint);
9346 return is_tracepoint ? &tracepoint_breakpoint_ops : &bkpt_breakpoint_ops;
9347 }
9348
9349 /* See breakpoint.h. */
9350
9351 int
9352 create_breakpoint (struct gdbarch *gdbarch,
9353 struct event_location *location,
9354 const char *cond_string,
9355 int thread, const char *extra_string,
9356 int parse_extra,
9357 int tempflag, enum bptype type_wanted,
9358 int ignore_count,
9359 enum auto_boolean pending_break_support,
9360 const struct breakpoint_ops *ops,
9361 int from_tty, int enabled, int internal,
9362 unsigned flags)
9363 {
9364 struct linespec_result canonical;
9365 int pending = 0;
9366 int task = 0;
9367 int prev_bkpt_count = breakpoint_count;
9368
9369 gdb_assert (ops != NULL);
9370
9371 /* If extra_string isn't useful, set it to NULL. */
9372 if (extra_string != NULL && *extra_string == '\0')
9373 extra_string = NULL;
9374
9375 try
9376 {
9377 ops->create_sals_from_location (location, &canonical, type_wanted);
9378 }
9379 catch (const gdb_exception_error &e)
9380 {
9381 /* If caller is interested in rc value from parse, set
9382 value. */
9383 if (e.error == NOT_FOUND_ERROR)
9384 {
9385 /* If pending breakpoint support is turned off, throw
9386 error. */
9387
9388 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9389 throw;
9390
9391 exception_print (gdb_stderr, e);
9392
9393 /* If pending breakpoint support is auto query and the user
9394 selects no, then simply return the error code. */
9395 if (pending_break_support == AUTO_BOOLEAN_AUTO
9396 && !nquery (_("Make %s pending on future shared library load? "),
9397 bptype_string (type_wanted)))
9398 return 0;
9399
9400 /* At this point, either the user was queried about setting
9401 a pending breakpoint and selected yes, or pending
9402 breakpoint behavior is on and thus a pending breakpoint
9403 is defaulted on behalf of the user. */
9404 pending = 1;
9405 }
9406 else
9407 throw;
9408 }
9409
9410 if (!pending && canonical.lsals.empty ())
9411 return 0;
9412
9413 /* Resolve all line numbers to PC's and verify that the addresses
9414 are ok for the target. */
9415 if (!pending)
9416 {
9417 for (auto &lsal : canonical.lsals)
9418 breakpoint_sals_to_pc (lsal.sals);
9419 }
9420
9421 /* Fast tracepoints may have additional restrictions on location. */
9422 if (!pending && type_wanted == bp_fast_tracepoint)
9423 {
9424 for (const auto &lsal : canonical.lsals)
9425 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9426 }
9427
9428 /* Verify that condition can be parsed, before setting any
9429 breakpoints. Allocate a separate condition expression for each
9430 breakpoint. */
9431 if (!pending)
9432 {
9433 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9434 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9435
9436 if (parse_extra)
9437 {
9438 char *rest;
9439 char *cond;
9440
9441 const linespec_sals &lsal = canonical.lsals[0];
9442
9443 find_condition_and_thread_for_sals (lsal.sals, extra_string,
9444 &cond, &thread, &task, &rest);
9445 cond_string_copy.reset (cond);
9446 extra_string_copy.reset (rest);
9447 }
9448 else
9449 {
9450 if (type_wanted != bp_dprintf
9451 && extra_string != NULL && *extra_string != '\0')
9452 error (_("Garbage '%s' at end of location"), extra_string);
9453
9454 /* Create a private copy of condition string. */
9455 if (cond_string)
9456 cond_string_copy.reset (xstrdup (cond_string));
9457 /* Create a private copy of any extra string. */
9458 if (extra_string)
9459 extra_string_copy.reset (xstrdup (extra_string));
9460 }
9461
9462 ops->create_breakpoints_sal (gdbarch, &canonical,
9463 std::move (cond_string_copy),
9464 std::move (extra_string_copy),
9465 type_wanted,
9466 tempflag ? disp_del : disp_donttouch,
9467 thread, task, ignore_count, ops,
9468 from_tty, enabled, internal, flags);
9469 }
9470 else
9471 {
9472 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9473
9474 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9475 b->location = copy_event_location (location);
9476
9477 if (parse_extra)
9478 b->cond_string = NULL;
9479 else
9480 {
9481 /* Create a private copy of condition string. */
9482 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
9483 b->thread = thread;
9484 }
9485
9486 /* Create a private copy of any extra string. */
9487 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
9488 b->ignore_count = ignore_count;
9489 b->disposition = tempflag ? disp_del : disp_donttouch;
9490 b->condition_not_parsed = 1;
9491 b->enable_state = enabled ? bp_enabled : bp_disabled;
9492 if ((type_wanted != bp_breakpoint
9493 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9494 b->pspace = current_program_space;
9495
9496 install_breakpoint (internal, std::move (b), 0);
9497 }
9498
9499 if (canonical.lsals.size () > 1)
9500 {
9501 warning (_("Multiple breakpoints were set.\nUse the "
9502 "\"delete\" command to delete unwanted breakpoints."));
9503 prev_breakpoint_count = prev_bkpt_count;
9504 }
9505
9506 update_global_location_list (UGLL_MAY_INSERT);
9507
9508 return 1;
9509 }
9510
9511 /* Set a breakpoint.
9512 ARG is a string describing breakpoint address,
9513 condition, and thread.
9514 FLAG specifies if a breakpoint is hardware on,
9515 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9516 and BP_TEMPFLAG. */
9517
9518 static void
9519 break_command_1 (const char *arg, int flag, int from_tty)
9520 {
9521 int tempflag = flag & BP_TEMPFLAG;
9522 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9523 ? bp_hardware_breakpoint
9524 : bp_breakpoint);
9525
9526 event_location_up location = string_to_event_location (&arg, current_language);
9527 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9528 (location.get (), false /* is_tracepoint */);
9529
9530 create_breakpoint (get_current_arch (),
9531 location.get (),
9532 NULL, 0, arg, 1 /* parse arg */,
9533 tempflag, type_wanted,
9534 0 /* Ignore count */,
9535 pending_break_support,
9536 ops,
9537 from_tty,
9538 1 /* enabled */,
9539 0 /* internal */,
9540 0);
9541 }
9542
9543 /* Helper function for break_command_1 and disassemble_command. */
9544
9545 void
9546 resolve_sal_pc (struct symtab_and_line *sal)
9547 {
9548 CORE_ADDR pc;
9549
9550 if (sal->pc == 0 && sal->symtab != NULL)
9551 {
9552 if (!find_line_pc (sal->symtab, sal->line, &pc))
9553 error (_("No line %d in file \"%s\"."),
9554 sal->line, symtab_to_filename_for_display (sal->symtab));
9555 sal->pc = pc;
9556
9557 /* If this SAL corresponds to a breakpoint inserted using a line
9558 number, then skip the function prologue if necessary. */
9559 if (sal->explicit_line)
9560 skip_prologue_sal (sal);
9561 }
9562
9563 if (sal->section == 0 && sal->symtab != NULL)
9564 {
9565 const struct blockvector *bv;
9566 const struct block *b;
9567 struct symbol *sym;
9568
9569 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9570 SYMTAB_COMPUNIT (sal->symtab));
9571 if (bv != NULL)
9572 {
9573 sym = block_linkage_function (b);
9574 if (sym != NULL)
9575 {
9576 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9577 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9578 sym);
9579 }
9580 else
9581 {
9582 /* It really is worthwhile to have the section, so we'll
9583 just have to look harder. This case can be executed
9584 if we have line numbers but no functions (as can
9585 happen in assembly source). */
9586
9587 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9588 switch_to_program_space_and_thread (sal->pspace);
9589
9590 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9591 if (msym.minsym)
9592 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9593 }
9594 }
9595 }
9596 }
9597
9598 void
9599 break_command (const char *arg, int from_tty)
9600 {
9601 break_command_1 (arg, 0, from_tty);
9602 }
9603
9604 void
9605 tbreak_command (const char *arg, int from_tty)
9606 {
9607 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9608 }
9609
9610 static void
9611 hbreak_command (const char *arg, int from_tty)
9612 {
9613 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9614 }
9615
9616 static void
9617 thbreak_command (const char *arg, int from_tty)
9618 {
9619 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9620 }
9621
9622 static void
9623 stop_command (const char *arg, int from_tty)
9624 {
9625 printf_filtered (_("Specify the type of breakpoint to set.\n\
9626 Usage: stop in <function | address>\n\
9627 stop at <line>\n"));
9628 }
9629
9630 static void
9631 stopin_command (const char *arg, int from_tty)
9632 {
9633 int badInput = 0;
9634
9635 if (arg == NULL)
9636 badInput = 1;
9637 else if (*arg != '*')
9638 {
9639 const char *argptr = arg;
9640 int hasColon = 0;
9641
9642 /* Look for a ':'. If this is a line number specification, then
9643 say it is bad, otherwise, it should be an address or
9644 function/method name. */
9645 while (*argptr && !hasColon)
9646 {
9647 hasColon = (*argptr == ':');
9648 argptr++;
9649 }
9650
9651 if (hasColon)
9652 badInput = (*argptr != ':'); /* Not a class::method */
9653 else
9654 badInput = isdigit (*arg); /* a simple line number */
9655 }
9656
9657 if (badInput)
9658 printf_filtered (_("Usage: stop in <function | address>\n"));
9659 else
9660 break_command_1 (arg, 0, from_tty);
9661 }
9662
9663 static void
9664 stopat_command (const char *arg, int from_tty)
9665 {
9666 int badInput = 0;
9667
9668 if (arg == NULL || *arg == '*') /* no line number */
9669 badInput = 1;
9670 else
9671 {
9672 const char *argptr = arg;
9673 int hasColon = 0;
9674
9675 /* Look for a ':'. If there is a '::' then get out, otherwise
9676 it is probably a line number. */
9677 while (*argptr && !hasColon)
9678 {
9679 hasColon = (*argptr == ':');
9680 argptr++;
9681 }
9682
9683 if (hasColon)
9684 badInput = (*argptr == ':'); /* we have class::method */
9685 else
9686 badInput = !isdigit (*arg); /* not a line number */
9687 }
9688
9689 if (badInput)
9690 printf_filtered (_("Usage: stop at LINE\n"));
9691 else
9692 break_command_1 (arg, 0, from_tty);
9693 }
9694
9695 /* The dynamic printf command is mostly like a regular breakpoint, but
9696 with a prewired command list consisting of a single output command,
9697 built from extra arguments supplied on the dprintf command
9698 line. */
9699
9700 static void
9701 dprintf_command (const char *arg, int from_tty)
9702 {
9703 event_location_up location = string_to_event_location (&arg, current_language);
9704
9705 /* If non-NULL, ARG should have been advanced past the location;
9706 the next character must be ','. */
9707 if (arg != NULL)
9708 {
9709 if (arg[0] != ',' || arg[1] == '\0')
9710 error (_("Format string required"));
9711 else
9712 {
9713 /* Skip the comma. */
9714 ++arg;
9715 }
9716 }
9717
9718 create_breakpoint (get_current_arch (),
9719 location.get (),
9720 NULL, 0, arg, 1 /* parse arg */,
9721 0, bp_dprintf,
9722 0 /* Ignore count */,
9723 pending_break_support,
9724 &dprintf_breakpoint_ops,
9725 from_tty,
9726 1 /* enabled */,
9727 0 /* internal */,
9728 0);
9729 }
9730
9731 static void
9732 agent_printf_command (const char *arg, int from_tty)
9733 {
9734 error (_("May only run agent-printf on the target"));
9735 }
9736
9737 /* Implement the "breakpoint_hit" breakpoint_ops method for
9738 ranged breakpoints. */
9739
9740 static int
9741 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9742 const address_space *aspace,
9743 CORE_ADDR bp_addr,
9744 const struct target_waitstatus *ws)
9745 {
9746 if (ws->kind != TARGET_WAITKIND_STOPPED
9747 || ws->value.sig != GDB_SIGNAL_TRAP)
9748 return 0;
9749
9750 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9751 bl->length, aspace, bp_addr);
9752 }
9753
9754 /* Implement the "resources_needed" breakpoint_ops method for
9755 ranged breakpoints. */
9756
9757 static int
9758 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9759 {
9760 return target_ranged_break_num_registers ();
9761 }
9762
9763 /* Implement the "print_it" breakpoint_ops method for
9764 ranged breakpoints. */
9765
9766 static enum print_stop_action
9767 print_it_ranged_breakpoint (bpstat bs)
9768 {
9769 struct breakpoint *b = bs->breakpoint_at;
9770 struct bp_location *bl = b->loc;
9771 struct ui_out *uiout = current_uiout;
9772
9773 gdb_assert (b->type == bp_hardware_breakpoint);
9774
9775 /* Ranged breakpoints have only one location. */
9776 gdb_assert (bl && bl->next == NULL);
9777
9778 annotate_breakpoint (b->number);
9779
9780 maybe_print_thread_hit_breakpoint (uiout);
9781
9782 if (b->disposition == disp_del)
9783 uiout->text ("Temporary ranged breakpoint ");
9784 else
9785 uiout->text ("Ranged breakpoint ");
9786 if (uiout->is_mi_like_p ())
9787 {
9788 uiout->field_string ("reason",
9789 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9790 uiout->field_string ("disp", bpdisp_text (b->disposition));
9791 }
9792 uiout->field_signed ("bkptno", b->number);
9793 uiout->text (", ");
9794
9795 return PRINT_SRC_AND_LOC;
9796 }
9797
9798 /* Implement the "print_one" breakpoint_ops method for
9799 ranged breakpoints. */
9800
9801 static void
9802 print_one_ranged_breakpoint (struct breakpoint *b,
9803 struct bp_location **last_loc)
9804 {
9805 struct bp_location *bl = b->loc;
9806 struct value_print_options opts;
9807 struct ui_out *uiout = current_uiout;
9808
9809 /* Ranged breakpoints have only one location. */
9810 gdb_assert (bl && bl->next == NULL);
9811
9812 get_user_print_options (&opts);
9813
9814 if (opts.addressprint)
9815 /* We don't print the address range here, it will be printed later
9816 by print_one_detail_ranged_breakpoint. */
9817 uiout->field_skip ("addr");
9818 annotate_field (5);
9819 print_breakpoint_location (b, bl);
9820 *last_loc = bl;
9821 }
9822
9823 /* Implement the "print_one_detail" breakpoint_ops method for
9824 ranged breakpoints. */
9825
9826 static void
9827 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9828 struct ui_out *uiout)
9829 {
9830 CORE_ADDR address_start, address_end;
9831 struct bp_location *bl = b->loc;
9832 string_file stb;
9833
9834 gdb_assert (bl);
9835
9836 address_start = bl->address;
9837 address_end = address_start + bl->length - 1;
9838
9839 uiout->text ("\taddress range: ");
9840 stb.printf ("[%s, %s]",
9841 print_core_address (bl->gdbarch, address_start),
9842 print_core_address (bl->gdbarch, address_end));
9843 uiout->field_stream ("addr", stb);
9844 uiout->text ("\n");
9845 }
9846
9847 /* Implement the "print_mention" breakpoint_ops method for
9848 ranged breakpoints. */
9849
9850 static void
9851 print_mention_ranged_breakpoint (struct breakpoint *b)
9852 {
9853 struct bp_location *bl = b->loc;
9854 struct ui_out *uiout = current_uiout;
9855
9856 gdb_assert (bl);
9857 gdb_assert (b->type == bp_hardware_breakpoint);
9858
9859 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9860 b->number, paddress (bl->gdbarch, bl->address),
9861 paddress (bl->gdbarch, bl->address + bl->length - 1));
9862 }
9863
9864 /* Implement the "print_recreate" breakpoint_ops method for
9865 ranged breakpoints. */
9866
9867 static void
9868 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9869 {
9870 fprintf_unfiltered (fp, "break-range %s, %s",
9871 event_location_to_string (b->location.get ()),
9872 event_location_to_string (b->location_range_end.get ()));
9873 print_recreate_thread (b, fp);
9874 }
9875
9876 /* The breakpoint_ops structure to be used in ranged breakpoints. */
9877
9878 static struct breakpoint_ops ranged_breakpoint_ops;
9879
9880 /* Find the address where the end of the breakpoint range should be
9881 placed, given the SAL of the end of the range. This is so that if
9882 the user provides a line number, the end of the range is set to the
9883 last instruction of the given line. */
9884
9885 static CORE_ADDR
9886 find_breakpoint_range_end (struct symtab_and_line sal)
9887 {
9888 CORE_ADDR end;
9889
9890 /* If the user provided a PC value, use it. Otherwise,
9891 find the address of the end of the given location. */
9892 if (sal.explicit_pc)
9893 end = sal.pc;
9894 else
9895 {
9896 int ret;
9897 CORE_ADDR start;
9898
9899 ret = find_line_pc_range (sal, &start, &end);
9900 if (!ret)
9901 error (_("Could not find location of the end of the range."));
9902
9903 /* find_line_pc_range returns the start of the next line. */
9904 end--;
9905 }
9906
9907 return end;
9908 }
9909
9910 /* Implement the "break-range" CLI command. */
9911
9912 static void
9913 break_range_command (const char *arg, int from_tty)
9914 {
9915 const char *arg_start;
9916 struct linespec_result canonical_start, canonical_end;
9917 int bp_count, can_use_bp, length;
9918 CORE_ADDR end;
9919 struct breakpoint *b;
9920
9921 /* We don't support software ranged breakpoints. */
9922 if (target_ranged_break_num_registers () < 0)
9923 error (_("This target does not support hardware ranged breakpoints."));
9924
9925 bp_count = hw_breakpoint_used_count ();
9926 bp_count += target_ranged_break_num_registers ();
9927 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9928 bp_count, 0);
9929 if (can_use_bp < 0)
9930 error (_("Hardware breakpoints used exceeds limit."));
9931
9932 arg = skip_spaces (arg);
9933 if (arg == NULL || arg[0] == '\0')
9934 error(_("No address range specified."));
9935
9936 arg_start = arg;
9937 event_location_up start_location = string_to_event_location (&arg,
9938 current_language);
9939 parse_breakpoint_sals (start_location.get (), &canonical_start);
9940
9941 if (arg[0] != ',')
9942 error (_("Too few arguments."));
9943 else if (canonical_start.lsals.empty ())
9944 error (_("Could not find location of the beginning of the range."));
9945
9946 const linespec_sals &lsal_start = canonical_start.lsals[0];
9947
9948 if (canonical_start.lsals.size () > 1
9949 || lsal_start.sals.size () != 1)
9950 error (_("Cannot create a ranged breakpoint with multiple locations."));
9951
9952 const symtab_and_line &sal_start = lsal_start.sals[0];
9953 std::string addr_string_start (arg_start, arg - arg_start);
9954
9955 arg++; /* Skip the comma. */
9956 arg = skip_spaces (arg);
9957
9958 /* Parse the end location. */
9959
9960 arg_start = arg;
9961
9962 /* We call decode_line_full directly here instead of using
9963 parse_breakpoint_sals because we need to specify the start location's
9964 symtab and line as the default symtab and line for the end of the
9965 range. This makes it possible to have ranges like "foo.c:27, +14",
9966 where +14 means 14 lines from the start location. */
9967 event_location_up end_location = string_to_event_location (&arg,
9968 current_language);
9969 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
9970 sal_start.symtab, sal_start.line,
9971 &canonical_end, NULL, NULL);
9972
9973 if (canonical_end.lsals.empty ())
9974 error (_("Could not find location of the end of the range."));
9975
9976 const linespec_sals &lsal_end = canonical_end.lsals[0];
9977 if (canonical_end.lsals.size () > 1
9978 || lsal_end.sals.size () != 1)
9979 error (_("Cannot create a ranged breakpoint with multiple locations."));
9980
9981 const symtab_and_line &sal_end = lsal_end.sals[0];
9982
9983 end = find_breakpoint_range_end (sal_end);
9984 if (sal_start.pc > end)
9985 error (_("Invalid address range, end precedes start."));
9986
9987 length = end - sal_start.pc + 1;
9988 if (length < 0)
9989 /* Length overflowed. */
9990 error (_("Address range too large."));
9991 else if (length == 1)
9992 {
9993 /* This range is simple enough to be handled by
9994 the `hbreak' command. */
9995 hbreak_command (&addr_string_start[0], 1);
9996
9997 return;
9998 }
9999
10000 /* Now set up the breakpoint. */
10001 b = set_raw_breakpoint (get_current_arch (), sal_start,
10002 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10003 set_breakpoint_count (breakpoint_count + 1);
10004 b->number = breakpoint_count;
10005 b->disposition = disp_donttouch;
10006 b->location = std::move (start_location);
10007 b->location_range_end = std::move (end_location);
10008 b->loc->length = length;
10009
10010 mention (b);
10011 gdb::observers::breakpoint_created.notify (b);
10012 update_global_location_list (UGLL_MAY_INSERT);
10013 }
10014
10015 /* Return non-zero if EXP is verified as constant. Returned zero
10016 means EXP is variable. Also the constant detection may fail for
10017 some constant expressions and in such case still falsely return
10018 zero. */
10019
10020 static int
10021 watchpoint_exp_is_const (const struct expression *exp)
10022 {
10023 int i = exp->nelts;
10024
10025 while (i > 0)
10026 {
10027 int oplenp, argsp;
10028
10029 /* We are only interested in the descriptor of each element. */
10030 operator_length (exp, i, &oplenp, &argsp);
10031 i -= oplenp;
10032
10033 switch (exp->elts[i].opcode)
10034 {
10035 case BINOP_ADD:
10036 case BINOP_SUB:
10037 case BINOP_MUL:
10038 case BINOP_DIV:
10039 case BINOP_REM:
10040 case BINOP_MOD:
10041 case BINOP_LSH:
10042 case BINOP_RSH:
10043 case BINOP_LOGICAL_AND:
10044 case BINOP_LOGICAL_OR:
10045 case BINOP_BITWISE_AND:
10046 case BINOP_BITWISE_IOR:
10047 case BINOP_BITWISE_XOR:
10048 case BINOP_EQUAL:
10049 case BINOP_NOTEQUAL:
10050 case BINOP_LESS:
10051 case BINOP_GTR:
10052 case BINOP_LEQ:
10053 case BINOP_GEQ:
10054 case BINOP_REPEAT:
10055 case BINOP_COMMA:
10056 case BINOP_EXP:
10057 case BINOP_MIN:
10058 case BINOP_MAX:
10059 case BINOP_INTDIV:
10060 case BINOP_CONCAT:
10061 case TERNOP_COND:
10062 case TERNOP_SLICE:
10063
10064 case OP_LONG:
10065 case OP_FLOAT:
10066 case OP_LAST:
10067 case OP_COMPLEX:
10068 case OP_STRING:
10069 case OP_ARRAY:
10070 case OP_TYPE:
10071 case OP_TYPEOF:
10072 case OP_DECLTYPE:
10073 case OP_TYPEID:
10074 case OP_NAME:
10075 case OP_OBJC_NSSTRING:
10076
10077 case UNOP_NEG:
10078 case UNOP_LOGICAL_NOT:
10079 case UNOP_COMPLEMENT:
10080 case UNOP_ADDR:
10081 case UNOP_HIGH:
10082 case UNOP_CAST:
10083
10084 case UNOP_CAST_TYPE:
10085 case UNOP_REINTERPRET_CAST:
10086 case UNOP_DYNAMIC_CAST:
10087 /* Unary, binary and ternary operators: We have to check
10088 their operands. If they are constant, then so is the
10089 result of that operation. For instance, if A and B are
10090 determined to be constants, then so is "A + B".
10091
10092 UNOP_IND is one exception to the rule above, because the
10093 value of *ADDR is not necessarily a constant, even when
10094 ADDR is. */
10095 break;
10096
10097 case OP_VAR_VALUE:
10098 /* Check whether the associated symbol is a constant.
10099
10100 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10101 possible that a buggy compiler could mark a variable as
10102 constant even when it is not, and TYPE_CONST would return
10103 true in this case, while SYMBOL_CLASS wouldn't.
10104
10105 We also have to check for function symbols because they
10106 are always constant. */
10107 {
10108 struct symbol *s = exp->elts[i + 2].symbol;
10109
10110 if (SYMBOL_CLASS (s) != LOC_BLOCK
10111 && SYMBOL_CLASS (s) != LOC_CONST
10112 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10113 return 0;
10114 break;
10115 }
10116
10117 /* The default action is to return 0 because we are using
10118 the optimistic approach here: If we don't know something,
10119 then it is not a constant. */
10120 default:
10121 return 0;
10122 }
10123 }
10124
10125 return 1;
10126 }
10127
10128 /* Watchpoint destructor. */
10129
10130 watchpoint::~watchpoint ()
10131 {
10132 xfree (this->exp_string);
10133 xfree (this->exp_string_reparse);
10134 }
10135
10136 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10137
10138 static void
10139 re_set_watchpoint (struct breakpoint *b)
10140 {
10141 struct watchpoint *w = (struct watchpoint *) b;
10142
10143 /* Watchpoint can be either on expression using entirely global
10144 variables, or it can be on local variables.
10145
10146 Watchpoints of the first kind are never auto-deleted, and even
10147 persist across program restarts. Since they can use variables
10148 from shared libraries, we need to reparse expression as libraries
10149 are loaded and unloaded.
10150
10151 Watchpoints on local variables can also change meaning as result
10152 of solib event. For example, if a watchpoint uses both a local
10153 and a global variables in expression, it's a local watchpoint,
10154 but unloading of a shared library will make the expression
10155 invalid. This is not a very common use case, but we still
10156 re-evaluate expression, to avoid surprises to the user.
10157
10158 Note that for local watchpoints, we re-evaluate it only if
10159 watchpoints frame id is still valid. If it's not, it means the
10160 watchpoint is out of scope and will be deleted soon. In fact,
10161 I'm not sure we'll ever be called in this case.
10162
10163 If a local watchpoint's frame id is still valid, then
10164 w->exp_valid_block is likewise valid, and we can safely use it.
10165
10166 Don't do anything about disabled watchpoints, since they will be
10167 reevaluated again when enabled. */
10168 update_watchpoint (w, 1 /* reparse */);
10169 }
10170
10171 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10172
10173 static int
10174 insert_watchpoint (struct bp_location *bl)
10175 {
10176 struct watchpoint *w = (struct watchpoint *) bl->owner;
10177 int length = w->exact ? 1 : bl->length;
10178
10179 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10180 w->cond_exp.get ());
10181 }
10182
10183 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10184
10185 static int
10186 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10187 {
10188 struct watchpoint *w = (struct watchpoint *) bl->owner;
10189 int length = w->exact ? 1 : bl->length;
10190
10191 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10192 w->cond_exp.get ());
10193 }
10194
10195 static int
10196 breakpoint_hit_watchpoint (const struct bp_location *bl,
10197 const address_space *aspace, CORE_ADDR bp_addr,
10198 const struct target_waitstatus *ws)
10199 {
10200 struct breakpoint *b = bl->owner;
10201 struct watchpoint *w = (struct watchpoint *) b;
10202
10203 /* Continuable hardware watchpoints are treated as non-existent if the
10204 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10205 some data address). Otherwise gdb won't stop on a break instruction
10206 in the code (not from a breakpoint) when a hardware watchpoint has
10207 been defined. Also skip watchpoints which we know did not trigger
10208 (did not match the data address). */
10209 if (is_hardware_watchpoint (b)
10210 && w->watchpoint_triggered == watch_triggered_no)
10211 return 0;
10212
10213 return 1;
10214 }
10215
10216 static void
10217 check_status_watchpoint (bpstat bs)
10218 {
10219 gdb_assert (is_watchpoint (bs->breakpoint_at));
10220
10221 bpstat_check_watchpoint (bs);
10222 }
10223
10224 /* Implement the "resources_needed" breakpoint_ops method for
10225 hardware watchpoints. */
10226
10227 static int
10228 resources_needed_watchpoint (const struct bp_location *bl)
10229 {
10230 struct watchpoint *w = (struct watchpoint *) bl->owner;
10231 int length = w->exact? 1 : bl->length;
10232
10233 return target_region_ok_for_hw_watchpoint (bl->address, length);
10234 }
10235
10236 /* Implement the "works_in_software_mode" breakpoint_ops method for
10237 hardware watchpoints. */
10238
10239 static int
10240 works_in_software_mode_watchpoint (const struct breakpoint *b)
10241 {
10242 /* Read and access watchpoints only work with hardware support. */
10243 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10244 }
10245
10246 static enum print_stop_action
10247 print_it_watchpoint (bpstat bs)
10248 {
10249 struct breakpoint *b;
10250 enum print_stop_action result;
10251 struct watchpoint *w;
10252 struct ui_out *uiout = current_uiout;
10253
10254 gdb_assert (bs->bp_location_at != NULL);
10255
10256 b = bs->breakpoint_at;
10257 w = (struct watchpoint *) b;
10258
10259 annotate_watchpoint (b->number);
10260 maybe_print_thread_hit_breakpoint (uiout);
10261
10262 string_file stb;
10263
10264 gdb::optional<ui_out_emit_tuple> tuple_emitter;
10265 switch (b->type)
10266 {
10267 case bp_watchpoint:
10268 case bp_hardware_watchpoint:
10269 if (uiout->is_mi_like_p ())
10270 uiout->field_string
10271 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10272 mention (b);
10273 tuple_emitter.emplace (uiout, "value");
10274 uiout->text ("\nOld value = ");
10275 watchpoint_value_print (bs->old_val.get (), &stb);
10276 uiout->field_stream ("old", stb);
10277 uiout->text ("\nNew value = ");
10278 watchpoint_value_print (w->val.get (), &stb);
10279 uiout->field_stream ("new", stb);
10280 uiout->text ("\n");
10281 /* More than one watchpoint may have been triggered. */
10282 result = PRINT_UNKNOWN;
10283 break;
10284
10285 case bp_read_watchpoint:
10286 if (uiout->is_mi_like_p ())
10287 uiout->field_string
10288 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10289 mention (b);
10290 tuple_emitter.emplace (uiout, "value");
10291 uiout->text ("\nValue = ");
10292 watchpoint_value_print (w->val.get (), &stb);
10293 uiout->field_stream ("value", stb);
10294 uiout->text ("\n");
10295 result = PRINT_UNKNOWN;
10296 break;
10297
10298 case bp_access_watchpoint:
10299 if (bs->old_val != NULL)
10300 {
10301 if (uiout->is_mi_like_p ())
10302 uiout->field_string
10303 ("reason",
10304 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10305 mention (b);
10306 tuple_emitter.emplace (uiout, "value");
10307 uiout->text ("\nOld value = ");
10308 watchpoint_value_print (bs->old_val.get (), &stb);
10309 uiout->field_stream ("old", stb);
10310 uiout->text ("\nNew value = ");
10311 }
10312 else
10313 {
10314 mention (b);
10315 if (uiout->is_mi_like_p ())
10316 uiout->field_string
10317 ("reason",
10318 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10319 tuple_emitter.emplace (uiout, "value");
10320 uiout->text ("\nValue = ");
10321 }
10322 watchpoint_value_print (w->val.get (), &stb);
10323 uiout->field_stream ("new", stb);
10324 uiout->text ("\n");
10325 result = PRINT_UNKNOWN;
10326 break;
10327 default:
10328 result = PRINT_UNKNOWN;
10329 }
10330
10331 return result;
10332 }
10333
10334 /* Implement the "print_mention" breakpoint_ops method for hardware
10335 watchpoints. */
10336
10337 static void
10338 print_mention_watchpoint (struct breakpoint *b)
10339 {
10340 struct watchpoint *w = (struct watchpoint *) b;
10341 struct ui_out *uiout = current_uiout;
10342 const char *tuple_name;
10343
10344 switch (b->type)
10345 {
10346 case bp_watchpoint:
10347 uiout->text ("Watchpoint ");
10348 tuple_name = "wpt";
10349 break;
10350 case bp_hardware_watchpoint:
10351 uiout->text ("Hardware watchpoint ");
10352 tuple_name = "wpt";
10353 break;
10354 case bp_read_watchpoint:
10355 uiout->text ("Hardware read watchpoint ");
10356 tuple_name = "hw-rwpt";
10357 break;
10358 case bp_access_watchpoint:
10359 uiout->text ("Hardware access (read/write) watchpoint ");
10360 tuple_name = "hw-awpt";
10361 break;
10362 default:
10363 internal_error (__FILE__, __LINE__,
10364 _("Invalid hardware watchpoint type."));
10365 }
10366
10367 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10368 uiout->field_signed ("number", b->number);
10369 uiout->text (": ");
10370 uiout->field_string ("exp", w->exp_string);
10371 }
10372
10373 /* Implement the "print_recreate" breakpoint_ops method for
10374 watchpoints. */
10375
10376 static void
10377 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10378 {
10379 struct watchpoint *w = (struct watchpoint *) b;
10380
10381 switch (b->type)
10382 {
10383 case bp_watchpoint:
10384 case bp_hardware_watchpoint:
10385 fprintf_unfiltered (fp, "watch");
10386 break;
10387 case bp_read_watchpoint:
10388 fprintf_unfiltered (fp, "rwatch");
10389 break;
10390 case bp_access_watchpoint:
10391 fprintf_unfiltered (fp, "awatch");
10392 break;
10393 default:
10394 internal_error (__FILE__, __LINE__,
10395 _("Invalid watchpoint type."));
10396 }
10397
10398 fprintf_unfiltered (fp, " %s", w->exp_string);
10399 print_recreate_thread (b, fp);
10400 }
10401
10402 /* Implement the "explains_signal" breakpoint_ops method for
10403 watchpoints. */
10404
10405 static int
10406 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10407 {
10408 /* A software watchpoint cannot cause a signal other than
10409 GDB_SIGNAL_TRAP. */
10410 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10411 return 0;
10412
10413 return 1;
10414 }
10415
10416 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10417
10418 static struct breakpoint_ops watchpoint_breakpoint_ops;
10419
10420 /* Implement the "insert" breakpoint_ops method for
10421 masked hardware watchpoints. */
10422
10423 static int
10424 insert_masked_watchpoint (struct bp_location *bl)
10425 {
10426 struct watchpoint *w = (struct watchpoint *) bl->owner;
10427
10428 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10429 bl->watchpoint_type);
10430 }
10431
10432 /* Implement the "remove" breakpoint_ops method for
10433 masked hardware watchpoints. */
10434
10435 static int
10436 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10437 {
10438 struct watchpoint *w = (struct watchpoint *) bl->owner;
10439
10440 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10441 bl->watchpoint_type);
10442 }
10443
10444 /* Implement the "resources_needed" breakpoint_ops method for
10445 masked hardware watchpoints. */
10446
10447 static int
10448 resources_needed_masked_watchpoint (const struct bp_location *bl)
10449 {
10450 struct watchpoint *w = (struct watchpoint *) bl->owner;
10451
10452 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10453 }
10454
10455 /* Implement the "works_in_software_mode" breakpoint_ops method for
10456 masked hardware watchpoints. */
10457
10458 static int
10459 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10460 {
10461 return 0;
10462 }
10463
10464 /* Implement the "print_it" breakpoint_ops method for
10465 masked hardware watchpoints. */
10466
10467 static enum print_stop_action
10468 print_it_masked_watchpoint (bpstat bs)
10469 {
10470 struct breakpoint *b = bs->breakpoint_at;
10471 struct ui_out *uiout = current_uiout;
10472
10473 /* Masked watchpoints have only one location. */
10474 gdb_assert (b->loc && b->loc->next == NULL);
10475
10476 annotate_watchpoint (b->number);
10477 maybe_print_thread_hit_breakpoint (uiout);
10478
10479 switch (b->type)
10480 {
10481 case bp_hardware_watchpoint:
10482 if (uiout->is_mi_like_p ())
10483 uiout->field_string
10484 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10485 break;
10486
10487 case bp_read_watchpoint:
10488 if (uiout->is_mi_like_p ())
10489 uiout->field_string
10490 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10491 break;
10492
10493 case bp_access_watchpoint:
10494 if (uiout->is_mi_like_p ())
10495 uiout->field_string
10496 ("reason",
10497 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10498 break;
10499 default:
10500 internal_error (__FILE__, __LINE__,
10501 _("Invalid hardware watchpoint type."));
10502 }
10503
10504 mention (b);
10505 uiout->text (_("\n\
10506 Check the underlying instruction at PC for the memory\n\
10507 address and value which triggered this watchpoint.\n"));
10508 uiout->text ("\n");
10509
10510 /* More than one watchpoint may have been triggered. */
10511 return PRINT_UNKNOWN;
10512 }
10513
10514 /* Implement the "print_one_detail" breakpoint_ops method for
10515 masked hardware watchpoints. */
10516
10517 static void
10518 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10519 struct ui_out *uiout)
10520 {
10521 struct watchpoint *w = (struct watchpoint *) b;
10522
10523 /* Masked watchpoints have only one location. */
10524 gdb_assert (b->loc && b->loc->next == NULL);
10525
10526 uiout->text ("\tmask ");
10527 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10528 uiout->text ("\n");
10529 }
10530
10531 /* Implement the "print_mention" breakpoint_ops method for
10532 masked hardware watchpoints. */
10533
10534 static void
10535 print_mention_masked_watchpoint (struct breakpoint *b)
10536 {
10537 struct watchpoint *w = (struct watchpoint *) b;
10538 struct ui_out *uiout = current_uiout;
10539 const char *tuple_name;
10540
10541 switch (b->type)
10542 {
10543 case bp_hardware_watchpoint:
10544 uiout->text ("Masked hardware watchpoint ");
10545 tuple_name = "wpt";
10546 break;
10547 case bp_read_watchpoint:
10548 uiout->text ("Masked hardware read watchpoint ");
10549 tuple_name = "hw-rwpt";
10550 break;
10551 case bp_access_watchpoint:
10552 uiout->text ("Masked hardware access (read/write) watchpoint ");
10553 tuple_name = "hw-awpt";
10554 break;
10555 default:
10556 internal_error (__FILE__, __LINE__,
10557 _("Invalid hardware watchpoint type."));
10558 }
10559
10560 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10561 uiout->field_signed ("number", b->number);
10562 uiout->text (": ");
10563 uiout->field_string ("exp", w->exp_string);
10564 }
10565
10566 /* Implement the "print_recreate" breakpoint_ops method for
10567 masked hardware watchpoints. */
10568
10569 static void
10570 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10571 {
10572 struct watchpoint *w = (struct watchpoint *) b;
10573
10574 switch (b->type)
10575 {
10576 case bp_hardware_watchpoint:
10577 fprintf_unfiltered (fp, "watch");
10578 break;
10579 case bp_read_watchpoint:
10580 fprintf_unfiltered (fp, "rwatch");
10581 break;
10582 case bp_access_watchpoint:
10583 fprintf_unfiltered (fp, "awatch");
10584 break;
10585 default:
10586 internal_error (__FILE__, __LINE__,
10587 _("Invalid hardware watchpoint type."));
10588 }
10589
10590 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string,
10591 phex (w->hw_wp_mask, sizeof (CORE_ADDR)));
10592 print_recreate_thread (b, fp);
10593 }
10594
10595 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10596
10597 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10598
10599 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10600
10601 static bool
10602 is_masked_watchpoint (const struct breakpoint *b)
10603 {
10604 return b->ops == &masked_watchpoint_breakpoint_ops;
10605 }
10606
10607 /* accessflag: hw_write: watch write,
10608 hw_read: watch read,
10609 hw_access: watch access (read or write) */
10610 static void
10611 watch_command_1 (const char *arg, int accessflag, int from_tty,
10612 int just_location, int internal)
10613 {
10614 struct breakpoint *scope_breakpoint = NULL;
10615 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10616 struct value *result;
10617 int saved_bitpos = 0, saved_bitsize = 0;
10618 const char *exp_start = NULL;
10619 const char *exp_end = NULL;
10620 const char *tok, *end_tok;
10621 int toklen = -1;
10622 const char *cond_start = NULL;
10623 const char *cond_end = NULL;
10624 enum bptype bp_type;
10625 int thread = -1;
10626 int pc = 0;
10627 /* Flag to indicate whether we are going to use masks for
10628 the hardware watchpoint. */
10629 int use_mask = 0;
10630 CORE_ADDR mask = 0;
10631
10632 /* Make sure that we actually have parameters to parse. */
10633 if (arg != NULL && arg[0] != '\0')
10634 {
10635 const char *value_start;
10636
10637 exp_end = arg + strlen (arg);
10638
10639 /* Look for "parameter value" pairs at the end
10640 of the arguments string. */
10641 for (tok = exp_end - 1; tok > arg; tok--)
10642 {
10643 /* Skip whitespace at the end of the argument list. */
10644 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10645 tok--;
10646
10647 /* Find the beginning of the last token.
10648 This is the value of the parameter. */
10649 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10650 tok--;
10651 value_start = tok + 1;
10652
10653 /* Skip whitespace. */
10654 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10655 tok--;
10656
10657 end_tok = tok;
10658
10659 /* Find the beginning of the second to last token.
10660 This is the parameter itself. */
10661 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10662 tok--;
10663 tok++;
10664 toklen = end_tok - tok + 1;
10665
10666 if (toklen == 6 && startswith (tok, "thread"))
10667 {
10668 struct thread_info *thr;
10669 /* At this point we've found a "thread" token, which means
10670 the user is trying to set a watchpoint that triggers
10671 only in a specific thread. */
10672 const char *endp;
10673
10674 if (thread != -1)
10675 error(_("You can specify only one thread."));
10676
10677 /* Extract the thread ID from the next token. */
10678 thr = parse_thread_id (value_start, &endp);
10679
10680 /* Check if the user provided a valid thread ID. */
10681 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10682 invalid_thread_id_error (value_start);
10683
10684 thread = thr->global_num;
10685 }
10686 else if (toklen == 4 && startswith (tok, "mask"))
10687 {
10688 /* We've found a "mask" token, which means the user wants to
10689 create a hardware watchpoint that is going to have the mask
10690 facility. */
10691 struct value *mask_value, *mark;
10692
10693 if (use_mask)
10694 error(_("You can specify only one mask."));
10695
10696 use_mask = just_location = 1;
10697
10698 mark = value_mark ();
10699 mask_value = parse_to_comma_and_eval (&value_start);
10700 mask = value_as_address (mask_value);
10701 value_free_to_mark (mark);
10702 }
10703 else
10704 /* We didn't recognize what we found. We should stop here. */
10705 break;
10706
10707 /* Truncate the string and get rid of the "parameter value" pair before
10708 the arguments string is parsed by the parse_exp_1 function. */
10709 exp_end = tok;
10710 }
10711 }
10712 else
10713 exp_end = arg;
10714
10715 /* Parse the rest of the arguments. From here on out, everything
10716 is in terms of a newly allocated string instead of the original
10717 ARG. */
10718 std::string expression (arg, exp_end - arg);
10719 exp_start = arg = expression.c_str ();
10720 innermost_block_tracker tracker;
10721 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10722 exp_end = arg;
10723 /* Remove trailing whitespace from the expression before saving it.
10724 This makes the eventual display of the expression string a bit
10725 prettier. */
10726 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10727 --exp_end;
10728
10729 /* Checking if the expression is not constant. */
10730 if (watchpoint_exp_is_const (exp.get ()))
10731 {
10732 int len;
10733
10734 len = exp_end - exp_start;
10735 while (len > 0 && isspace (exp_start[len - 1]))
10736 len--;
10737 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10738 }
10739
10740 exp_valid_block = tracker.block ();
10741 struct value *mark = value_mark ();
10742 struct value *val_as_value = nullptr;
10743 fetch_subexp_value (exp.get (), &pc, &val_as_value, &result, NULL,
10744 just_location);
10745
10746 if (val_as_value != NULL && just_location)
10747 {
10748 saved_bitpos = value_bitpos (val_as_value);
10749 saved_bitsize = value_bitsize (val_as_value);
10750 }
10751
10752 value_ref_ptr val;
10753 if (just_location)
10754 {
10755 int ret;
10756
10757 exp_valid_block = NULL;
10758 val = release_value (value_addr (result));
10759 value_free_to_mark (mark);
10760
10761 if (use_mask)
10762 {
10763 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10764 mask);
10765 if (ret == -1)
10766 error (_("This target does not support masked watchpoints."));
10767 else if (ret == -2)
10768 error (_("Invalid mask or memory region."));
10769 }
10770 }
10771 else if (val_as_value != NULL)
10772 val = release_value (val_as_value);
10773
10774 tok = skip_spaces (arg);
10775 end_tok = skip_to_space (tok);
10776
10777 toklen = end_tok - tok;
10778 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10779 {
10780 tok = cond_start = end_tok + 1;
10781 innermost_block_tracker if_tracker;
10782 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10783
10784 /* The watchpoint expression may not be local, but the condition
10785 may still be. E.g.: `watch global if local > 0'. */
10786 cond_exp_valid_block = if_tracker.block ();
10787
10788 cond_end = tok;
10789 }
10790 if (*tok)
10791 error (_("Junk at end of command."));
10792
10793 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10794
10795 /* Save this because create_internal_breakpoint below invalidates
10796 'wp_frame'. */
10797 frame_id watchpoint_frame = get_frame_id (wp_frame);
10798
10799 /* If the expression is "local", then set up a "watchpoint scope"
10800 breakpoint at the point where we've left the scope of the watchpoint
10801 expression. Create the scope breakpoint before the watchpoint, so
10802 that we will encounter it first in bpstat_stop_status. */
10803 if (exp_valid_block != NULL && wp_frame != NULL)
10804 {
10805 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10806
10807 if (frame_id_p (caller_frame_id))
10808 {
10809 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10810 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10811
10812 scope_breakpoint
10813 = create_internal_breakpoint (caller_arch, caller_pc,
10814 bp_watchpoint_scope,
10815 &momentary_breakpoint_ops);
10816
10817 /* create_internal_breakpoint could invalidate WP_FRAME. */
10818 wp_frame = NULL;
10819
10820 scope_breakpoint->enable_state = bp_enabled;
10821
10822 /* Automatically delete the breakpoint when it hits. */
10823 scope_breakpoint->disposition = disp_del;
10824
10825 /* Only break in the proper frame (help with recursion). */
10826 scope_breakpoint->frame_id = caller_frame_id;
10827
10828 /* Set the address at which we will stop. */
10829 scope_breakpoint->loc->gdbarch = caller_arch;
10830 scope_breakpoint->loc->requested_address = caller_pc;
10831 scope_breakpoint->loc->address
10832 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10833 scope_breakpoint->loc->requested_address,
10834 scope_breakpoint->type);
10835 }
10836 }
10837
10838 /* Now set up the breakpoint. We create all watchpoints as hardware
10839 watchpoints here even if hardware watchpoints are turned off, a call
10840 to update_watchpoint later in this function will cause the type to
10841 drop back to bp_watchpoint (software watchpoint) if required. */
10842
10843 if (accessflag == hw_read)
10844 bp_type = bp_read_watchpoint;
10845 else if (accessflag == hw_access)
10846 bp_type = bp_access_watchpoint;
10847 else
10848 bp_type = bp_hardware_watchpoint;
10849
10850 std::unique_ptr<watchpoint> w (new watchpoint ());
10851
10852 if (use_mask)
10853 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10854 &masked_watchpoint_breakpoint_ops);
10855 else
10856 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10857 &watchpoint_breakpoint_ops);
10858 w->thread = thread;
10859 w->disposition = disp_donttouch;
10860 w->pspace = current_program_space;
10861 w->exp = std::move (exp);
10862 w->exp_valid_block = exp_valid_block;
10863 w->cond_exp_valid_block = cond_exp_valid_block;
10864 if (just_location)
10865 {
10866 struct type *t = value_type (val.get ());
10867 CORE_ADDR addr = value_as_address (val.get ());
10868
10869 w->exp_string_reparse
10870 = current_language->watch_location_expression (t, addr).release ();
10871
10872 w->exp_string = xstrprintf ("-location %.*s",
10873 (int) (exp_end - exp_start), exp_start);
10874 }
10875 else
10876 w->exp_string = savestring (exp_start, exp_end - exp_start);
10877
10878 if (use_mask)
10879 {
10880 w->hw_wp_mask = mask;
10881 }
10882 else
10883 {
10884 w->val = val;
10885 w->val_bitpos = saved_bitpos;
10886 w->val_bitsize = saved_bitsize;
10887 w->val_valid = true;
10888 }
10889
10890 if (cond_start)
10891 w->cond_string = savestring (cond_start, cond_end - cond_start);
10892 else
10893 w->cond_string = 0;
10894
10895 if (frame_id_p (watchpoint_frame))
10896 {
10897 w->watchpoint_frame = watchpoint_frame;
10898 w->watchpoint_thread = inferior_ptid;
10899 }
10900 else
10901 {
10902 w->watchpoint_frame = null_frame_id;
10903 w->watchpoint_thread = null_ptid;
10904 }
10905
10906 if (scope_breakpoint != NULL)
10907 {
10908 /* The scope breakpoint is related to the watchpoint. We will
10909 need to act on them together. */
10910 w->related_breakpoint = scope_breakpoint;
10911 scope_breakpoint->related_breakpoint = w.get ();
10912 }
10913
10914 if (!just_location)
10915 value_free_to_mark (mark);
10916
10917 /* Finally update the new watchpoint. This creates the locations
10918 that should be inserted. */
10919 update_watchpoint (w.get (), 1);
10920
10921 install_breakpoint (internal, std::move (w), 1);
10922 }
10923
10924 /* Return count of debug registers needed to watch the given expression.
10925 If the watchpoint cannot be handled in hardware return zero. */
10926
10927 static int
10928 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10929 {
10930 int found_memory_cnt = 0;
10931
10932 /* Did the user specifically forbid us to use hardware watchpoints? */
10933 if (!can_use_hw_watchpoints)
10934 return 0;
10935
10936 gdb_assert (!vals.empty ());
10937 struct value *head = vals[0].get ();
10938
10939 /* Make sure that the value of the expression depends only upon
10940 memory contents, and values computed from them within GDB. If we
10941 find any register references or function calls, we can't use a
10942 hardware watchpoint.
10943
10944 The idea here is that evaluating an expression generates a series
10945 of values, one holding the value of every subexpression. (The
10946 expression a*b+c has five subexpressions: a, b, a*b, c, and
10947 a*b+c.) GDB's values hold almost enough information to establish
10948 the criteria given above --- they identify memory lvalues,
10949 register lvalues, computed values, etcetera. So we can evaluate
10950 the expression, and then scan the chain of values that leaves
10951 behind to decide whether we can detect any possible change to the
10952 expression's final value using only hardware watchpoints.
10953
10954 However, I don't think that the values returned by inferior
10955 function calls are special in any way. So this function may not
10956 notice that an expression involving an inferior function call
10957 can't be watched with hardware watchpoints. FIXME. */
10958 for (const value_ref_ptr &iter : vals)
10959 {
10960 struct value *v = iter.get ();
10961
10962 if (VALUE_LVAL (v) == lval_memory)
10963 {
10964 if (v != head && value_lazy (v))
10965 /* A lazy memory lvalue in the chain is one that GDB never
10966 needed to fetch; we either just used its address (e.g.,
10967 `a' in `a.b') or we never needed it at all (e.g., `a'
10968 in `a,b'). This doesn't apply to HEAD; if that is
10969 lazy then it was not readable, but watch it anyway. */
10970 ;
10971 else
10972 {
10973 /* Ahh, memory we actually used! Check if we can cover
10974 it with hardware watchpoints. */
10975 struct type *vtype = check_typedef (value_type (v));
10976
10977 /* We only watch structs and arrays if user asked for it
10978 explicitly, never if they just happen to appear in a
10979 middle of some value chain. */
10980 if (v == head
10981 || (vtype->code () != TYPE_CODE_STRUCT
10982 && vtype->code () != TYPE_CODE_ARRAY))
10983 {
10984 CORE_ADDR vaddr = value_address (v);
10985 int len;
10986 int num_regs;
10987
10988 len = (target_exact_watchpoints
10989 && is_scalar_type_recursive (vtype))?
10990 1 : TYPE_LENGTH (value_type (v));
10991
10992 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10993 if (!num_regs)
10994 return 0;
10995 else
10996 found_memory_cnt += num_regs;
10997 }
10998 }
10999 }
11000 else if (VALUE_LVAL (v) != not_lval
11001 && deprecated_value_modifiable (v) == 0)
11002 return 0; /* These are values from the history (e.g., $1). */
11003 else if (VALUE_LVAL (v) == lval_register)
11004 return 0; /* Cannot watch a register with a HW watchpoint. */
11005 }
11006
11007 /* The expression itself looks suitable for using a hardware
11008 watchpoint, but give the target machine a chance to reject it. */
11009 return found_memory_cnt;
11010 }
11011
11012 void
11013 watch_command_wrapper (const char *arg, int from_tty, int internal)
11014 {
11015 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11016 }
11017
11018 /* A helper function that looks for the "-location" argument and then
11019 calls watch_command_1. */
11020
11021 static void
11022 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
11023 {
11024 int just_location = 0;
11025
11026 if (arg
11027 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11028 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11029 just_location = 1;
11030
11031 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11032 }
11033
11034 static void
11035 watch_command (const char *arg, int from_tty)
11036 {
11037 watch_maybe_just_location (arg, hw_write, from_tty);
11038 }
11039
11040 void
11041 rwatch_command_wrapper (const char *arg, int from_tty, int internal)
11042 {
11043 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11044 }
11045
11046 static void
11047 rwatch_command (const char *arg, int from_tty)
11048 {
11049 watch_maybe_just_location (arg, hw_read, from_tty);
11050 }
11051
11052 void
11053 awatch_command_wrapper (const char *arg, int from_tty, int internal)
11054 {
11055 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11056 }
11057
11058 static void
11059 awatch_command (const char *arg, int from_tty)
11060 {
11061 watch_maybe_just_location (arg, hw_access, from_tty);
11062 }
11063 \f
11064
11065 /* Data for the FSM that manages the until(location)/advance commands
11066 in infcmd.c. Here because it uses the mechanisms of
11067 breakpoints. */
11068
11069 struct until_break_fsm : public thread_fsm
11070 {
11071 /* The thread that was current when the command was executed. */
11072 int thread;
11073
11074 /* The breakpoint set at the return address in the caller frame,
11075 plus breakpoints at all the destination locations. */
11076 std::vector<breakpoint_up> breakpoints;
11077
11078 until_break_fsm (struct interp *cmd_interp, int thread,
11079 std::vector<breakpoint_up> &&breakpoints)
11080 : thread_fsm (cmd_interp),
11081 thread (thread),
11082 breakpoints (std::move (breakpoints))
11083 {
11084 }
11085
11086 void clean_up (struct thread_info *thread) override;
11087 bool should_stop (struct thread_info *thread) override;
11088 enum async_reply_reason do_async_reply_reason () override;
11089 };
11090
11091 /* Implementation of the 'should_stop' FSM method for the
11092 until(location)/advance commands. */
11093
11094 bool
11095 until_break_fsm::should_stop (struct thread_info *tp)
11096 {
11097 for (const breakpoint_up &bp : breakpoints)
11098 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11099 bp.get ()) != NULL)
11100 {
11101 set_finished ();
11102 break;
11103 }
11104
11105 return true;
11106 }
11107
11108 /* Implementation of the 'clean_up' FSM method for the
11109 until(location)/advance commands. */
11110
11111 void
11112 until_break_fsm::clean_up (struct thread_info *)
11113 {
11114 /* Clean up our temporary breakpoints. */
11115 breakpoints.clear ();
11116 delete_longjmp_breakpoint (thread);
11117 }
11118
11119 /* Implementation of the 'async_reply_reason' FSM method for the
11120 until(location)/advance commands. */
11121
11122 enum async_reply_reason
11123 until_break_fsm::do_async_reply_reason ()
11124 {
11125 return EXEC_ASYNC_LOCATION_REACHED;
11126 }
11127
11128 void
11129 until_break_command (const char *arg, int from_tty, int anywhere)
11130 {
11131 struct frame_info *frame;
11132 struct gdbarch *frame_gdbarch;
11133 struct frame_id stack_frame_id;
11134 struct frame_id caller_frame_id;
11135 int thread;
11136 struct thread_info *tp;
11137
11138 clear_proceed_status (0);
11139
11140 /* Set a breakpoint where the user wants it and at return from
11141 this function. */
11142
11143 event_location_up location = string_to_event_location (&arg, current_language);
11144
11145 std::vector<symtab_and_line> sals
11146 = (last_displayed_sal_is_valid ()
11147 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11148 get_last_displayed_symtab (),
11149 get_last_displayed_line ())
11150 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11151 NULL, NULL, 0));
11152
11153 if (sals.empty ())
11154 error (_("Couldn't get information on specified line."));
11155
11156 if (*arg)
11157 error (_("Junk at end of arguments."));
11158
11159 tp = inferior_thread ();
11160 thread = tp->global_num;
11161
11162 /* Note linespec handling above invalidates the frame chain.
11163 Installing a breakpoint also invalidates the frame chain (as it
11164 may need to switch threads), so do any frame handling before
11165 that. */
11166
11167 frame = get_selected_frame (NULL);
11168 frame_gdbarch = get_frame_arch (frame);
11169 stack_frame_id = get_stack_frame_id (frame);
11170 caller_frame_id = frame_unwind_caller_id (frame);
11171
11172 /* Keep within the current frame, or in frames called by the current
11173 one. */
11174
11175 std::vector<breakpoint_up> breakpoints;
11176
11177 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11178
11179 if (frame_id_p (caller_frame_id))
11180 {
11181 struct symtab_and_line sal2;
11182 struct gdbarch *caller_gdbarch;
11183
11184 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11185 sal2.pc = frame_unwind_caller_pc (frame);
11186 caller_gdbarch = frame_unwind_caller_arch (frame);
11187
11188 breakpoint_up caller_breakpoint
11189 = set_momentary_breakpoint (caller_gdbarch, sal2,
11190 caller_frame_id, bp_until);
11191 breakpoints.emplace_back (std::move (caller_breakpoint));
11192
11193 set_longjmp_breakpoint (tp, caller_frame_id);
11194 lj_deleter.emplace (thread);
11195 }
11196
11197 /* set_momentary_breakpoint could invalidate FRAME. */
11198 frame = NULL;
11199
11200 /* If the user told us to continue until a specified location, we
11201 don't specify a frame at which we need to stop. Otherwise,
11202 specify the selected frame, because we want to stop only at the
11203 very same frame. */
11204 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
11205
11206 for (symtab_and_line &sal : sals)
11207 {
11208 resolve_sal_pc (&sal);
11209
11210 breakpoint_up location_breakpoint
11211 = set_momentary_breakpoint (frame_gdbarch, sal,
11212 stop_frame_id, bp_until);
11213 breakpoints.emplace_back (std::move (location_breakpoint));
11214 }
11215
11216 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
11217 std::move (breakpoints));
11218
11219 if (lj_deleter)
11220 lj_deleter->release ();
11221
11222 proceed (-1, GDB_SIGNAL_DEFAULT);
11223 }
11224
11225 /* This function attempts to parse an optional "if <cond>" clause
11226 from the arg string. If one is not found, it returns NULL.
11227
11228 Else, it returns a pointer to the condition string. (It does not
11229 attempt to evaluate the string against a particular block.) And,
11230 it updates arg to point to the first character following the parsed
11231 if clause in the arg string. */
11232
11233 const char *
11234 ep_parse_optional_if_clause (const char **arg)
11235 {
11236 const char *cond_string;
11237
11238 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11239 return NULL;
11240
11241 /* Skip the "if" keyword. */
11242 (*arg) += 2;
11243
11244 /* Skip any extra leading whitespace, and record the start of the
11245 condition string. */
11246 *arg = skip_spaces (*arg);
11247 cond_string = *arg;
11248
11249 /* Assume that the condition occupies the remainder of the arg
11250 string. */
11251 (*arg) += strlen (cond_string);
11252
11253 return cond_string;
11254 }
11255
11256 /* Commands to deal with catching events, such as signals, exceptions,
11257 process start/exit, etc. */
11258
11259 typedef enum
11260 {
11261 catch_fork_temporary, catch_vfork_temporary,
11262 catch_fork_permanent, catch_vfork_permanent
11263 }
11264 catch_fork_kind;
11265
11266 static void
11267 catch_fork_command_1 (const char *arg, int from_tty,
11268 struct cmd_list_element *command)
11269 {
11270 struct gdbarch *gdbarch = get_current_arch ();
11271 const char *cond_string = NULL;
11272 catch_fork_kind fork_kind;
11273
11274 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11275 bool temp = (fork_kind == catch_fork_temporary
11276 || fork_kind == catch_vfork_temporary);
11277
11278 if (!arg)
11279 arg = "";
11280 arg = skip_spaces (arg);
11281
11282 /* The allowed syntax is:
11283 catch [v]fork
11284 catch [v]fork if <cond>
11285
11286 First, check if there's an if clause. */
11287 cond_string = ep_parse_optional_if_clause (&arg);
11288
11289 if ((*arg != '\0') && !isspace (*arg))
11290 error (_("Junk at end of arguments."));
11291
11292 /* If this target supports it, create a fork or vfork catchpoint
11293 and enable reporting of such events. */
11294 switch (fork_kind)
11295 {
11296 case catch_fork_temporary:
11297 case catch_fork_permanent:
11298 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
11299 &catch_fork_breakpoint_ops);
11300 break;
11301 case catch_vfork_temporary:
11302 case catch_vfork_permanent:
11303 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
11304 &catch_vfork_breakpoint_ops);
11305 break;
11306 default:
11307 error (_("unsupported or unknown fork kind; cannot catch it"));
11308 break;
11309 }
11310 }
11311
11312 static void
11313 catch_exec_command_1 (const char *arg, int from_tty,
11314 struct cmd_list_element *command)
11315 {
11316 struct gdbarch *gdbarch = get_current_arch ();
11317 const char *cond_string = NULL;
11318 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
11319
11320 if (!arg)
11321 arg = "";
11322 arg = skip_spaces (arg);
11323
11324 /* The allowed syntax is:
11325 catch exec
11326 catch exec if <cond>
11327
11328 First, check if there's an if clause. */
11329 cond_string = ep_parse_optional_if_clause (&arg);
11330
11331 if ((*arg != '\0') && !isspace (*arg))
11332 error (_("Junk at end of arguments."));
11333
11334 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11335 init_catchpoint (c.get (), gdbarch, temp, cond_string,
11336 &catch_exec_breakpoint_ops);
11337 c->exec_pathname = NULL;
11338
11339 install_breakpoint (0, std::move (c), 1);
11340 }
11341
11342 void
11343 init_ada_exception_breakpoint (struct breakpoint *b,
11344 struct gdbarch *gdbarch,
11345 struct symtab_and_line sal,
11346 const char *addr_string,
11347 const struct breakpoint_ops *ops,
11348 int tempflag,
11349 int enabled,
11350 int from_tty)
11351 {
11352 if (from_tty)
11353 {
11354 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11355 if (!loc_gdbarch)
11356 loc_gdbarch = gdbarch;
11357
11358 describe_other_breakpoints (loc_gdbarch,
11359 sal.pspace, sal.pc, sal.section, -1);
11360 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11361 version for exception catchpoints, because two catchpoints
11362 used for different exception names will use the same address.
11363 In this case, a "breakpoint ... also set at..." warning is
11364 unproductive. Besides, the warning phrasing is also a bit
11365 inappropriate, we should use the word catchpoint, and tell
11366 the user what type of catchpoint it is. The above is good
11367 enough for now, though. */
11368 }
11369
11370 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
11371
11372 b->enable_state = enabled ? bp_enabled : bp_disabled;
11373 b->disposition = tempflag ? disp_del : disp_donttouch;
11374 b->location = string_to_event_location (&addr_string,
11375 language_def (language_ada));
11376 b->language = language_ada;
11377 }
11378
11379 \f
11380
11381 /* Compare two breakpoints and return a strcmp-like result. */
11382
11383 static int
11384 compare_breakpoints (const breakpoint *a, const breakpoint *b)
11385 {
11386 uintptr_t ua = (uintptr_t) a;
11387 uintptr_t ub = (uintptr_t) b;
11388
11389 if (a->number < b->number)
11390 return -1;
11391 else if (a->number > b->number)
11392 return 1;
11393
11394 /* Now sort by address, in case we see, e..g, two breakpoints with
11395 the number 0. */
11396 if (ua < ub)
11397 return -1;
11398 return ua > ub ? 1 : 0;
11399 }
11400
11401 /* Delete breakpoints by address or line. */
11402
11403 static void
11404 clear_command (const char *arg, int from_tty)
11405 {
11406 struct breakpoint *b;
11407 int default_match;
11408
11409 std::vector<symtab_and_line> decoded_sals;
11410 symtab_and_line last_sal;
11411 gdb::array_view<symtab_and_line> sals;
11412 if (arg)
11413 {
11414 decoded_sals
11415 = decode_line_with_current_source (arg,
11416 (DECODE_LINE_FUNFIRSTLINE
11417 | DECODE_LINE_LIST_MODE));
11418 default_match = 0;
11419 sals = decoded_sals;
11420 }
11421 else
11422 {
11423 /* Set sal's line, symtab, pc, and pspace to the values
11424 corresponding to the last call to print_frame_info. If the
11425 codepoint is not valid, this will set all the fields to 0. */
11426 last_sal = get_last_displayed_sal ();
11427 if (last_sal.symtab == 0)
11428 error (_("No source file specified."));
11429
11430 default_match = 1;
11431 sals = last_sal;
11432 }
11433
11434 /* We don't call resolve_sal_pc here. That's not as bad as it
11435 seems, because all existing breakpoints typically have both
11436 file/line and pc set. So, if clear is given file/line, we can
11437 match this to existing breakpoint without obtaining pc at all.
11438
11439 We only support clearing given the address explicitly
11440 present in breakpoint table. Say, we've set breakpoint
11441 at file:line. There were several PC values for that file:line,
11442 due to optimization, all in one block.
11443
11444 We've picked one PC value. If "clear" is issued with another
11445 PC corresponding to the same file:line, the breakpoint won't
11446 be cleared. We probably can still clear the breakpoint, but
11447 since the other PC value is never presented to user, user
11448 can only find it by guessing, and it does not seem important
11449 to support that. */
11450
11451 /* For each line spec given, delete bps which correspond to it. Do
11452 it in two passes, solely to preserve the current behavior that
11453 from_tty is forced true if we delete more than one
11454 breakpoint. */
11455
11456 std::vector<struct breakpoint *> found;
11457 for (const auto &sal : sals)
11458 {
11459 const char *sal_fullname;
11460
11461 /* If exact pc given, clear bpts at that pc.
11462 If line given (pc == 0), clear all bpts on specified line.
11463 If defaulting, clear all bpts on default line
11464 or at default pc.
11465
11466 defaulting sal.pc != 0 tests to do
11467
11468 0 1 pc
11469 1 1 pc _and_ line
11470 0 0 line
11471 1 0 <can't happen> */
11472
11473 sal_fullname = (sal.symtab == NULL
11474 ? NULL : symtab_to_fullname (sal.symtab));
11475
11476 /* Find all matching breakpoints and add them to 'found'. */
11477 ALL_BREAKPOINTS (b)
11478 {
11479 int match = 0;
11480 /* Are we going to delete b? */
11481 if (b->type != bp_none && !is_watchpoint (b))
11482 {
11483 struct bp_location *loc = b->loc;
11484 for (; loc; loc = loc->next)
11485 {
11486 /* If the user specified file:line, don't allow a PC
11487 match. This matches historical gdb behavior. */
11488 int pc_match = (!sal.explicit_line
11489 && sal.pc
11490 && (loc->pspace == sal.pspace)
11491 && (loc->address == sal.pc)
11492 && (!section_is_overlay (loc->section)
11493 || loc->section == sal.section));
11494 int line_match = 0;
11495
11496 if ((default_match || sal.explicit_line)
11497 && loc->symtab != NULL
11498 && sal_fullname != NULL
11499 && sal.pspace == loc->pspace
11500 && loc->line_number == sal.line
11501 && filename_cmp (symtab_to_fullname (loc->symtab),
11502 sal_fullname) == 0)
11503 line_match = 1;
11504
11505 if (pc_match || line_match)
11506 {
11507 match = 1;
11508 break;
11509 }
11510 }
11511 }
11512
11513 if (match)
11514 found.push_back (b);
11515 }
11516 }
11517
11518 /* Now go thru the 'found' chain and delete them. */
11519 if (found.empty ())
11520 {
11521 if (arg)
11522 error (_("No breakpoint at %s."), arg);
11523 else
11524 error (_("No breakpoint at this line."));
11525 }
11526
11527 /* Remove duplicates from the vec. */
11528 std::sort (found.begin (), found.end (),
11529 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11530 {
11531 return compare_breakpoints (bp_a, bp_b) < 0;
11532 });
11533 found.erase (std::unique (found.begin (), found.end (),
11534 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11535 {
11536 return compare_breakpoints (bp_a, bp_b) == 0;
11537 }),
11538 found.end ());
11539
11540 if (found.size () > 1)
11541 from_tty = 1; /* Always report if deleted more than one. */
11542 if (from_tty)
11543 {
11544 if (found.size () == 1)
11545 printf_unfiltered (_("Deleted breakpoint "));
11546 else
11547 printf_unfiltered (_("Deleted breakpoints "));
11548 }
11549
11550 for (breakpoint *iter : found)
11551 {
11552 if (from_tty)
11553 printf_unfiltered ("%d ", iter->number);
11554 delete_breakpoint (iter);
11555 }
11556 if (from_tty)
11557 putchar_unfiltered ('\n');
11558 }
11559 \f
11560 /* Delete breakpoint in BS if they are `delete' breakpoints and
11561 all breakpoints that are marked for deletion, whether hit or not.
11562 This is called after any breakpoint is hit, or after errors. */
11563
11564 void
11565 breakpoint_auto_delete (bpstat bs)
11566 {
11567 struct breakpoint *b, *b_tmp;
11568
11569 for (; bs; bs = bs->next)
11570 if (bs->breakpoint_at
11571 && bs->breakpoint_at->disposition == disp_del
11572 && bs->stop)
11573 delete_breakpoint (bs->breakpoint_at);
11574
11575 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11576 {
11577 if (b->disposition == disp_del_at_next_stop)
11578 delete_breakpoint (b);
11579 }
11580 }
11581
11582 /* A comparison function for bp_location AP and BP being interfaced to
11583 std::sort. Sort elements primarily by their ADDRESS (no matter what
11584 bl_address_is_meaningful says), secondarily by ordering first
11585 permanent elements and terciarily just ensuring the array is sorted
11586 stable way despite std::sort being an unstable algorithm. */
11587
11588 static int
11589 bp_location_is_less_than (const bp_location *a, const bp_location *b)
11590 {
11591 if (a->address != b->address)
11592 return a->address < b->address;
11593
11594 /* Sort locations at the same address by their pspace number, keeping
11595 locations of the same inferior (in a multi-inferior environment)
11596 grouped. */
11597
11598 if (a->pspace->num != b->pspace->num)
11599 return a->pspace->num < b->pspace->num;
11600
11601 /* Sort permanent breakpoints first. */
11602 if (a->permanent != b->permanent)
11603 return a->permanent > b->permanent;
11604
11605 /* Sort by type in order to make duplicate determination easier.
11606 See update_global_location_list. This is kept in sync with
11607 breakpoint_locations_match. */
11608 if (a->loc_type < b->loc_type)
11609 return true;
11610
11611 /* Likewise, for range-breakpoints, sort by length. */
11612 if (a->loc_type == bp_loc_hardware_breakpoint
11613 && b->loc_type == bp_loc_hardware_breakpoint
11614 && a->length < b->length)
11615 return true;
11616
11617 /* Make the internal GDB representation stable across GDB runs
11618 where A and B memory inside GDB can differ. Breakpoint locations of
11619 the same type at the same address can be sorted in arbitrary order. */
11620
11621 if (a->owner->number != b->owner->number)
11622 return a->owner->number < b->owner->number;
11623
11624 return a < b;
11625 }
11626
11627 /* Set bp_locations_placed_address_before_address_max and
11628 bp_locations_shadow_len_after_address_max according to the current
11629 content of the bp_locations array. */
11630
11631 static void
11632 bp_locations_target_extensions_update (void)
11633 {
11634 struct bp_location *bl, **blp_tmp;
11635
11636 bp_locations_placed_address_before_address_max = 0;
11637 bp_locations_shadow_len_after_address_max = 0;
11638
11639 ALL_BP_LOCATIONS (bl, blp_tmp)
11640 {
11641 CORE_ADDR start, end, addr;
11642
11643 if (!bp_location_has_shadow (bl))
11644 continue;
11645
11646 start = bl->target_info.placed_address;
11647 end = start + bl->target_info.shadow_len;
11648
11649 gdb_assert (bl->address >= start);
11650 addr = bl->address - start;
11651 if (addr > bp_locations_placed_address_before_address_max)
11652 bp_locations_placed_address_before_address_max = addr;
11653
11654 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11655
11656 gdb_assert (bl->address < end);
11657 addr = end - bl->address;
11658 if (addr > bp_locations_shadow_len_after_address_max)
11659 bp_locations_shadow_len_after_address_max = addr;
11660 }
11661 }
11662
11663 /* Download tracepoint locations if they haven't been. */
11664
11665 static void
11666 download_tracepoint_locations (void)
11667 {
11668 struct breakpoint *b;
11669 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11670
11671 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11672
11673 ALL_TRACEPOINTS (b)
11674 {
11675 struct bp_location *bl;
11676 struct tracepoint *t;
11677 int bp_location_downloaded = 0;
11678
11679 if ((b->type == bp_fast_tracepoint
11680 ? !may_insert_fast_tracepoints
11681 : !may_insert_tracepoints))
11682 continue;
11683
11684 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11685 {
11686 if (target_can_download_tracepoint ())
11687 can_download_tracepoint = TRIBOOL_TRUE;
11688 else
11689 can_download_tracepoint = TRIBOOL_FALSE;
11690 }
11691
11692 if (can_download_tracepoint == TRIBOOL_FALSE)
11693 break;
11694
11695 for (bl = b->loc; bl; bl = bl->next)
11696 {
11697 /* In tracepoint, locations are _never_ duplicated, so
11698 should_be_inserted is equivalent to
11699 unduplicated_should_be_inserted. */
11700 if (!should_be_inserted (bl) || bl->inserted)
11701 continue;
11702
11703 switch_to_program_space_and_thread (bl->pspace);
11704
11705 target_download_tracepoint (bl);
11706
11707 bl->inserted = 1;
11708 bp_location_downloaded = 1;
11709 }
11710 t = (struct tracepoint *) b;
11711 t->number_on_target = b->number;
11712 if (bp_location_downloaded)
11713 gdb::observers::breakpoint_modified.notify (b);
11714 }
11715 }
11716
11717 /* Swap the insertion/duplication state between two locations. */
11718
11719 static void
11720 swap_insertion (struct bp_location *left, struct bp_location *right)
11721 {
11722 const int left_inserted = left->inserted;
11723 const int left_duplicate = left->duplicate;
11724 const int left_needs_update = left->needs_update;
11725 const struct bp_target_info left_target_info = left->target_info;
11726
11727 /* Locations of tracepoints can never be duplicated. */
11728 if (is_tracepoint (left->owner))
11729 gdb_assert (!left->duplicate);
11730 if (is_tracepoint (right->owner))
11731 gdb_assert (!right->duplicate);
11732
11733 left->inserted = right->inserted;
11734 left->duplicate = right->duplicate;
11735 left->needs_update = right->needs_update;
11736 left->target_info = right->target_info;
11737 right->inserted = left_inserted;
11738 right->duplicate = left_duplicate;
11739 right->needs_update = left_needs_update;
11740 right->target_info = left_target_info;
11741 }
11742
11743 /* Force the re-insertion of the locations at ADDRESS. This is called
11744 once a new/deleted/modified duplicate location is found and we are evaluating
11745 conditions on the target's side. Such conditions need to be updated on
11746 the target. */
11747
11748 static void
11749 force_breakpoint_reinsertion (struct bp_location *bl)
11750 {
11751 struct bp_location **locp = NULL, **loc2p;
11752 struct bp_location *loc;
11753 CORE_ADDR address = 0;
11754 int pspace_num;
11755
11756 address = bl->address;
11757 pspace_num = bl->pspace->num;
11758
11759 /* This is only meaningful if the target is
11760 evaluating conditions and if the user has
11761 opted for condition evaluation on the target's
11762 side. */
11763 if (gdb_evaluates_breakpoint_condition_p ()
11764 || !target_supports_evaluation_of_breakpoint_conditions ())
11765 return;
11766
11767 /* Flag all breakpoint locations with this address and
11768 the same program space as the location
11769 as "its condition has changed". We need to
11770 update the conditions on the target's side. */
11771 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11772 {
11773 loc = *loc2p;
11774
11775 if (!is_breakpoint (loc->owner)
11776 || pspace_num != loc->pspace->num)
11777 continue;
11778
11779 /* Flag the location appropriately. We use a different state to
11780 let everyone know that we already updated the set of locations
11781 with addr bl->address and program space bl->pspace. This is so
11782 we don't have to keep calling these functions just to mark locations
11783 that have already been marked. */
11784 loc->condition_changed = condition_updated;
11785
11786 /* Free the agent expression bytecode as well. We will compute
11787 it later on. */
11788 loc->cond_bytecode.reset ();
11789 }
11790 }
11791
11792 /* Called whether new breakpoints are created, or existing breakpoints
11793 deleted, to update the global location list and recompute which
11794 locations are duplicate of which.
11795
11796 The INSERT_MODE flag determines whether locations may not, may, or
11797 shall be inserted now. See 'enum ugll_insert_mode' for more
11798 info. */
11799
11800 static void
11801 update_global_location_list (enum ugll_insert_mode insert_mode)
11802 {
11803 struct breakpoint *b;
11804 struct bp_location **locp, *loc;
11805 /* Last breakpoint location address that was marked for update. */
11806 CORE_ADDR last_addr = 0;
11807 /* Last breakpoint location program space that was marked for update. */
11808 int last_pspace_num = -1;
11809
11810 /* Used in the duplicates detection below. When iterating over all
11811 bp_locations, points to the first bp_location of a given address.
11812 Breakpoints and watchpoints of different types are never
11813 duplicates of each other. Keep one pointer for each type of
11814 breakpoint/watchpoint, so we only need to loop over all locations
11815 once. */
11816 struct bp_location *bp_loc_first; /* breakpoint */
11817 struct bp_location *wp_loc_first; /* hardware watchpoint */
11818 struct bp_location *awp_loc_first; /* access watchpoint */
11819 struct bp_location *rwp_loc_first; /* read watchpoint */
11820
11821 /* Saved former bp_locations array which we compare against the newly
11822 built bp_locations from the current state of ALL_BREAKPOINTS. */
11823 struct bp_location **old_locp;
11824 unsigned old_locations_count;
11825 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
11826
11827 old_locations_count = bp_locations_count;
11828 bp_locations = NULL;
11829 bp_locations_count = 0;
11830
11831 ALL_BREAKPOINTS (b)
11832 for (loc = b->loc; loc; loc = loc->next)
11833 bp_locations_count++;
11834
11835 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11836 locp = bp_locations;
11837 ALL_BREAKPOINTS (b)
11838 for (loc = b->loc; loc; loc = loc->next)
11839 *locp++ = loc;
11840
11841 /* See if we need to "upgrade" a software breakpoint to a hardware
11842 breakpoint. Do this before deciding whether locations are
11843 duplicates. Also do this before sorting because sorting order
11844 depends on location type. */
11845 for (locp = bp_locations;
11846 locp < bp_locations + bp_locations_count;
11847 locp++)
11848 {
11849 loc = *locp;
11850 if (!loc->inserted && should_be_inserted (loc))
11851 handle_automatic_hardware_breakpoints (loc);
11852 }
11853
11854 std::sort (bp_locations, bp_locations + bp_locations_count,
11855 bp_location_is_less_than);
11856
11857 bp_locations_target_extensions_update ();
11858
11859 /* Identify bp_location instances that are no longer present in the
11860 new list, and therefore should be freed. Note that it's not
11861 necessary that those locations should be removed from inferior --
11862 if there's another location at the same address (previously
11863 marked as duplicate), we don't need to remove/insert the
11864 location.
11865
11866 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11867 and former bp_location array state respectively. */
11868
11869 locp = bp_locations;
11870 for (old_locp = old_locations.get ();
11871 old_locp < old_locations.get () + old_locations_count;
11872 old_locp++)
11873 {
11874 struct bp_location *old_loc = *old_locp;
11875 struct bp_location **loc2p;
11876
11877 /* Tells if 'old_loc' is found among the new locations. If
11878 not, we have to free it. */
11879 int found_object = 0;
11880 /* Tells if the location should remain inserted in the target. */
11881 int keep_in_target = 0;
11882 int removed = 0;
11883
11884 /* Skip LOCP entries which will definitely never be needed.
11885 Stop either at or being the one matching OLD_LOC. */
11886 while (locp < bp_locations + bp_locations_count
11887 && (*locp)->address < old_loc->address)
11888 locp++;
11889
11890 for (loc2p = locp;
11891 (loc2p < bp_locations + bp_locations_count
11892 && (*loc2p)->address == old_loc->address);
11893 loc2p++)
11894 {
11895 /* Check if this is a new/duplicated location or a duplicated
11896 location that had its condition modified. If so, we want to send
11897 its condition to the target if evaluation of conditions is taking
11898 place there. */
11899 if ((*loc2p)->condition_changed == condition_modified
11900 && (last_addr != old_loc->address
11901 || last_pspace_num != old_loc->pspace->num))
11902 {
11903 force_breakpoint_reinsertion (*loc2p);
11904 last_pspace_num = old_loc->pspace->num;
11905 }
11906
11907 if (*loc2p == old_loc)
11908 found_object = 1;
11909 }
11910
11911 /* We have already handled this address, update it so that we don't
11912 have to go through updates again. */
11913 last_addr = old_loc->address;
11914
11915 /* Target-side condition evaluation: Handle deleted locations. */
11916 if (!found_object)
11917 force_breakpoint_reinsertion (old_loc);
11918
11919 /* If this location is no longer present, and inserted, look if
11920 there's maybe a new location at the same address. If so,
11921 mark that one inserted, and don't remove this one. This is
11922 needed so that we don't have a time window where a breakpoint
11923 at certain location is not inserted. */
11924
11925 if (old_loc->inserted)
11926 {
11927 /* If the location is inserted now, we might have to remove
11928 it. */
11929
11930 if (found_object && should_be_inserted (old_loc))
11931 {
11932 /* The location is still present in the location list,
11933 and still should be inserted. Don't do anything. */
11934 keep_in_target = 1;
11935 }
11936 else
11937 {
11938 /* This location still exists, but it won't be kept in the
11939 target since it may have been disabled. We proceed to
11940 remove its target-side condition. */
11941
11942 /* The location is either no longer present, or got
11943 disabled. See if there's another location at the
11944 same address, in which case we don't need to remove
11945 this one from the target. */
11946
11947 /* OLD_LOC comes from existing struct breakpoint. */
11948 if (bl_address_is_meaningful (old_loc))
11949 {
11950 for (loc2p = locp;
11951 (loc2p < bp_locations + bp_locations_count
11952 && (*loc2p)->address == old_loc->address);
11953 loc2p++)
11954 {
11955 struct bp_location *loc2 = *loc2p;
11956
11957 if (loc2 == old_loc)
11958 continue;
11959
11960 if (breakpoint_locations_match (loc2, old_loc))
11961 {
11962 /* Read watchpoint locations are switched to
11963 access watchpoints, if the former are not
11964 supported, but the latter are. */
11965 if (is_hardware_watchpoint (old_loc->owner))
11966 {
11967 gdb_assert (is_hardware_watchpoint (loc2->owner));
11968 loc2->watchpoint_type = old_loc->watchpoint_type;
11969 }
11970
11971 /* loc2 is a duplicated location. We need to check
11972 if it should be inserted in case it will be
11973 unduplicated. */
11974 if (unduplicated_should_be_inserted (loc2))
11975 {
11976 swap_insertion (old_loc, loc2);
11977 keep_in_target = 1;
11978 break;
11979 }
11980 }
11981 }
11982 }
11983 }
11984
11985 if (!keep_in_target)
11986 {
11987 if (remove_breakpoint (old_loc))
11988 {
11989 /* This is just about all we can do. We could keep
11990 this location on the global list, and try to
11991 remove it next time, but there's no particular
11992 reason why we will succeed next time.
11993
11994 Note that at this point, old_loc->owner is still
11995 valid, as delete_breakpoint frees the breakpoint
11996 only after calling us. */
11997 printf_filtered (_("warning: Error removing "
11998 "breakpoint %d\n"),
11999 old_loc->owner->number);
12000 }
12001 removed = 1;
12002 }
12003 }
12004
12005 if (!found_object)
12006 {
12007 if (removed && target_is_non_stop_p ()
12008 && need_moribund_for_location_type (old_loc))
12009 {
12010 /* This location was removed from the target. In
12011 non-stop mode, a race condition is possible where
12012 we've removed a breakpoint, but stop events for that
12013 breakpoint are already queued and will arrive later.
12014 We apply an heuristic to be able to distinguish such
12015 SIGTRAPs from other random SIGTRAPs: we keep this
12016 breakpoint location for a bit, and will retire it
12017 after we see some number of events. The theory here
12018 is that reporting of events should, "on the average",
12019 be fair, so after a while we'll see events from all
12020 threads that have anything of interest, and no longer
12021 need to keep this breakpoint location around. We
12022 don't hold locations forever so to reduce chances of
12023 mistaking a non-breakpoint SIGTRAP for a breakpoint
12024 SIGTRAP.
12025
12026 The heuristic failing can be disastrous on
12027 decr_pc_after_break targets.
12028
12029 On decr_pc_after_break targets, like e.g., x86-linux,
12030 if we fail to recognize a late breakpoint SIGTRAP,
12031 because events_till_retirement has reached 0 too
12032 soon, we'll fail to do the PC adjustment, and report
12033 a random SIGTRAP to the user. When the user resumes
12034 the inferior, it will most likely immediately crash
12035 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12036 corrupted, because of being resumed e.g., in the
12037 middle of a multi-byte instruction, or skipped a
12038 one-byte instruction. This was actually seen happen
12039 on native x86-linux, and should be less rare on
12040 targets that do not support new thread events, like
12041 remote, due to the heuristic depending on
12042 thread_count.
12043
12044 Mistaking a random SIGTRAP for a breakpoint trap
12045 causes similar symptoms (PC adjustment applied when
12046 it shouldn't), but then again, playing with SIGTRAPs
12047 behind the debugger's back is asking for trouble.
12048
12049 Since hardware watchpoint traps are always
12050 distinguishable from other traps, so we don't need to
12051 apply keep hardware watchpoint moribund locations
12052 around. We simply always ignore hardware watchpoint
12053 traps we can no longer explain. */
12054
12055 process_stratum_target *proc_target = nullptr;
12056 for (inferior *inf : all_inferiors ())
12057 if (inf->pspace == old_loc->pspace)
12058 {
12059 proc_target = inf->process_target ();
12060 break;
12061 }
12062 if (proc_target != nullptr)
12063 old_loc->events_till_retirement
12064 = 3 * (thread_count (proc_target) + 1);
12065 else
12066 old_loc->events_till_retirement = 1;
12067 old_loc->owner = NULL;
12068
12069 moribund_locations.push_back (old_loc);
12070 }
12071 else
12072 {
12073 old_loc->owner = NULL;
12074 decref_bp_location (&old_loc);
12075 }
12076 }
12077 }
12078
12079 /* Rescan breakpoints at the same address and section, marking the
12080 first one as "first" and any others as "duplicates". This is so
12081 that the bpt instruction is only inserted once. If we have a
12082 permanent breakpoint at the same place as BPT, make that one the
12083 official one, and the rest as duplicates. Permanent breakpoints
12084 are sorted first for the same address.
12085
12086 Do the same for hardware watchpoints, but also considering the
12087 watchpoint's type (regular/access/read) and length. */
12088
12089 bp_loc_first = NULL;
12090 wp_loc_first = NULL;
12091 awp_loc_first = NULL;
12092 rwp_loc_first = NULL;
12093 ALL_BP_LOCATIONS (loc, locp)
12094 {
12095 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12096 non-NULL. */
12097 struct bp_location **loc_first_p;
12098 b = loc->owner;
12099
12100 if (!unduplicated_should_be_inserted (loc)
12101 || !bl_address_is_meaningful (loc)
12102 /* Don't detect duplicate for tracepoint locations because they are
12103 never duplicated. See the comments in field `duplicate' of
12104 `struct bp_location'. */
12105 || is_tracepoint (b))
12106 {
12107 /* Clear the condition modification flag. */
12108 loc->condition_changed = condition_unchanged;
12109 continue;
12110 }
12111
12112 if (b->type == bp_hardware_watchpoint)
12113 loc_first_p = &wp_loc_first;
12114 else if (b->type == bp_read_watchpoint)
12115 loc_first_p = &rwp_loc_first;
12116 else if (b->type == bp_access_watchpoint)
12117 loc_first_p = &awp_loc_first;
12118 else
12119 loc_first_p = &bp_loc_first;
12120
12121 if (*loc_first_p == NULL
12122 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12123 || !breakpoint_locations_match (loc, *loc_first_p))
12124 {
12125 *loc_first_p = loc;
12126 loc->duplicate = 0;
12127
12128 if (is_breakpoint (loc->owner) && loc->condition_changed)
12129 {
12130 loc->needs_update = 1;
12131 /* Clear the condition modification flag. */
12132 loc->condition_changed = condition_unchanged;
12133 }
12134 continue;
12135 }
12136
12137
12138 /* This and the above ensure the invariant that the first location
12139 is not duplicated, and is the inserted one.
12140 All following are marked as duplicated, and are not inserted. */
12141 if (loc->inserted)
12142 swap_insertion (loc, *loc_first_p);
12143 loc->duplicate = 1;
12144
12145 /* Clear the condition modification flag. */
12146 loc->condition_changed = condition_unchanged;
12147 }
12148
12149 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12150 {
12151 if (insert_mode != UGLL_DONT_INSERT)
12152 insert_breakpoint_locations ();
12153 else
12154 {
12155 /* Even though the caller told us to not insert new
12156 locations, we may still need to update conditions on the
12157 target's side of breakpoints that were already inserted
12158 if the target is evaluating breakpoint conditions. We
12159 only update conditions for locations that are marked
12160 "needs_update". */
12161 update_inserted_breakpoint_locations ();
12162 }
12163 }
12164
12165 if (insert_mode != UGLL_DONT_INSERT)
12166 download_tracepoint_locations ();
12167 }
12168
12169 void
12170 breakpoint_retire_moribund (void)
12171 {
12172 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12173 {
12174 struct bp_location *loc = moribund_locations[ix];
12175 if (--(loc->events_till_retirement) == 0)
12176 {
12177 decref_bp_location (&loc);
12178 unordered_remove (moribund_locations, ix);
12179 --ix;
12180 }
12181 }
12182 }
12183
12184 static void
12185 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12186 {
12187
12188 try
12189 {
12190 update_global_location_list (insert_mode);
12191 }
12192 catch (const gdb_exception_error &e)
12193 {
12194 }
12195 }
12196
12197 /* Clear BKP from a BPS. */
12198
12199 static void
12200 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12201 {
12202 bpstat bs;
12203
12204 for (bs = bps; bs; bs = bs->next)
12205 if (bs->breakpoint_at == bpt)
12206 {
12207 bs->breakpoint_at = NULL;
12208 bs->old_val = NULL;
12209 /* bs->commands will be freed later. */
12210 }
12211 }
12212
12213 /* Callback for iterate_over_threads. */
12214 static int
12215 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12216 {
12217 struct breakpoint *bpt = (struct breakpoint *) data;
12218
12219 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12220 return 0;
12221 }
12222
12223 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12224 callbacks. */
12225
12226 static void
12227 say_where (struct breakpoint *b)
12228 {
12229 struct value_print_options opts;
12230
12231 get_user_print_options (&opts);
12232
12233 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12234 single string. */
12235 if (b->loc == NULL)
12236 {
12237 /* For pending locations, the output differs slightly based
12238 on b->extra_string. If this is non-NULL, it contains either
12239 a condition or dprintf arguments. */
12240 if (b->extra_string == NULL)
12241 {
12242 printf_filtered (_(" (%s) pending."),
12243 event_location_to_string (b->location.get ()));
12244 }
12245 else if (b->type == bp_dprintf)
12246 {
12247 printf_filtered (_(" (%s,%s) pending."),
12248 event_location_to_string (b->location.get ()),
12249 b->extra_string);
12250 }
12251 else
12252 {
12253 printf_filtered (_(" (%s %s) pending."),
12254 event_location_to_string (b->location.get ()),
12255 b->extra_string);
12256 }
12257 }
12258 else
12259 {
12260 if (opts.addressprint || b->loc->symtab == NULL)
12261 printf_filtered (" at %ps",
12262 styled_string (address_style.style (),
12263 paddress (b->loc->gdbarch,
12264 b->loc->address)));
12265 if (b->loc->symtab != NULL)
12266 {
12267 /* If there is a single location, we can print the location
12268 more nicely. */
12269 if (b->loc->next == NULL)
12270 {
12271 const char *filename
12272 = symtab_to_filename_for_display (b->loc->symtab);
12273 printf_filtered (": file %ps, line %d.",
12274 styled_string (file_name_style.style (),
12275 filename),
12276 b->loc->line_number);
12277 }
12278 else
12279 /* This is not ideal, but each location may have a
12280 different file name, and this at least reflects the
12281 real situation somewhat. */
12282 printf_filtered (": %s.",
12283 event_location_to_string (b->location.get ()));
12284 }
12285
12286 if (b->loc->next)
12287 {
12288 struct bp_location *loc = b->loc;
12289 int n = 0;
12290 for (; loc; loc = loc->next)
12291 ++n;
12292 printf_filtered (" (%d locations)", n);
12293 }
12294 }
12295 }
12296
12297 bp_location::~bp_location ()
12298 {
12299 xfree (function_name);
12300 }
12301
12302 /* Destructor for the breakpoint base class. */
12303
12304 breakpoint::~breakpoint ()
12305 {
12306 xfree (this->cond_string);
12307 xfree (this->extra_string);
12308 }
12309
12310 static struct bp_location *
12311 base_breakpoint_allocate_location (struct breakpoint *self)
12312 {
12313 return new bp_location (self);
12314 }
12315
12316 static void
12317 base_breakpoint_re_set (struct breakpoint *b)
12318 {
12319 /* Nothing to re-set. */
12320 }
12321
12322 #define internal_error_pure_virtual_called() \
12323 gdb_assert_not_reached ("pure virtual function called")
12324
12325 static int
12326 base_breakpoint_insert_location (struct bp_location *bl)
12327 {
12328 internal_error_pure_virtual_called ();
12329 }
12330
12331 static int
12332 base_breakpoint_remove_location (struct bp_location *bl,
12333 enum remove_bp_reason reason)
12334 {
12335 internal_error_pure_virtual_called ();
12336 }
12337
12338 static int
12339 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12340 const address_space *aspace,
12341 CORE_ADDR bp_addr,
12342 const struct target_waitstatus *ws)
12343 {
12344 internal_error_pure_virtual_called ();
12345 }
12346
12347 static void
12348 base_breakpoint_check_status (bpstat bs)
12349 {
12350 /* Always stop. */
12351 }
12352
12353 /* A "works_in_software_mode" breakpoint_ops method that just internal
12354 errors. */
12355
12356 static int
12357 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12358 {
12359 internal_error_pure_virtual_called ();
12360 }
12361
12362 /* A "resources_needed" breakpoint_ops method that just internal
12363 errors. */
12364
12365 static int
12366 base_breakpoint_resources_needed (const struct bp_location *bl)
12367 {
12368 internal_error_pure_virtual_called ();
12369 }
12370
12371 static enum print_stop_action
12372 base_breakpoint_print_it (bpstat bs)
12373 {
12374 internal_error_pure_virtual_called ();
12375 }
12376
12377 static void
12378 base_breakpoint_print_one_detail (const struct breakpoint *self,
12379 struct ui_out *uiout)
12380 {
12381 /* nothing */
12382 }
12383
12384 static void
12385 base_breakpoint_print_mention (struct breakpoint *b)
12386 {
12387 internal_error_pure_virtual_called ();
12388 }
12389
12390 static void
12391 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12392 {
12393 internal_error_pure_virtual_called ();
12394 }
12395
12396 static void
12397 base_breakpoint_create_sals_from_location
12398 (struct event_location *location,
12399 struct linespec_result *canonical,
12400 enum bptype type_wanted)
12401 {
12402 internal_error_pure_virtual_called ();
12403 }
12404
12405 static void
12406 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12407 struct linespec_result *c,
12408 gdb::unique_xmalloc_ptr<char> cond_string,
12409 gdb::unique_xmalloc_ptr<char> extra_string,
12410 enum bptype type_wanted,
12411 enum bpdisp disposition,
12412 int thread,
12413 int task, int ignore_count,
12414 const struct breakpoint_ops *o,
12415 int from_tty, int enabled,
12416 int internal, unsigned flags)
12417 {
12418 internal_error_pure_virtual_called ();
12419 }
12420
12421 static std::vector<symtab_and_line>
12422 base_breakpoint_decode_location (struct breakpoint *b,
12423 struct event_location *location,
12424 struct program_space *search_pspace)
12425 {
12426 internal_error_pure_virtual_called ();
12427 }
12428
12429 /* The default 'explains_signal' method. */
12430
12431 static int
12432 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12433 {
12434 return 1;
12435 }
12436
12437 /* The default "after_condition_true" method. */
12438
12439 static void
12440 base_breakpoint_after_condition_true (struct bpstats *bs)
12441 {
12442 /* Nothing to do. */
12443 }
12444
12445 struct breakpoint_ops base_breakpoint_ops =
12446 {
12447 base_breakpoint_allocate_location,
12448 base_breakpoint_re_set,
12449 base_breakpoint_insert_location,
12450 base_breakpoint_remove_location,
12451 base_breakpoint_breakpoint_hit,
12452 base_breakpoint_check_status,
12453 base_breakpoint_resources_needed,
12454 base_breakpoint_works_in_software_mode,
12455 base_breakpoint_print_it,
12456 NULL,
12457 base_breakpoint_print_one_detail,
12458 base_breakpoint_print_mention,
12459 base_breakpoint_print_recreate,
12460 base_breakpoint_create_sals_from_location,
12461 base_breakpoint_create_breakpoints_sal,
12462 base_breakpoint_decode_location,
12463 base_breakpoint_explains_signal,
12464 base_breakpoint_after_condition_true,
12465 };
12466
12467 /* Default breakpoint_ops methods. */
12468
12469 static void
12470 bkpt_re_set (struct breakpoint *b)
12471 {
12472 /* FIXME: is this still reachable? */
12473 if (breakpoint_event_location_empty_p (b))
12474 {
12475 /* Anything without a location can't be re-set. */
12476 delete_breakpoint (b);
12477 return;
12478 }
12479
12480 breakpoint_re_set_default (b);
12481 }
12482
12483 static int
12484 bkpt_insert_location (struct bp_location *bl)
12485 {
12486 CORE_ADDR addr = bl->target_info.reqstd_address;
12487
12488 bl->target_info.kind = breakpoint_kind (bl, &addr);
12489 bl->target_info.placed_address = addr;
12490
12491 if (bl->loc_type == bp_loc_hardware_breakpoint)
12492 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12493 else
12494 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12495 }
12496
12497 static int
12498 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12499 {
12500 if (bl->loc_type == bp_loc_hardware_breakpoint)
12501 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12502 else
12503 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12504 }
12505
12506 static int
12507 bkpt_breakpoint_hit (const struct bp_location *bl,
12508 const address_space *aspace, CORE_ADDR bp_addr,
12509 const struct target_waitstatus *ws)
12510 {
12511 if (ws->kind != TARGET_WAITKIND_STOPPED
12512 || ws->value.sig != GDB_SIGNAL_TRAP)
12513 return 0;
12514
12515 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12516 aspace, bp_addr))
12517 return 0;
12518
12519 if (overlay_debugging /* unmapped overlay section */
12520 && section_is_overlay (bl->section)
12521 && !section_is_mapped (bl->section))
12522 return 0;
12523
12524 return 1;
12525 }
12526
12527 static int
12528 dprintf_breakpoint_hit (const struct bp_location *bl,
12529 const address_space *aspace, CORE_ADDR bp_addr,
12530 const struct target_waitstatus *ws)
12531 {
12532 if (dprintf_style == dprintf_style_agent
12533 && target_can_run_breakpoint_commands ())
12534 {
12535 /* An agent-style dprintf never causes a stop. If we see a trap
12536 for this address it must be for a breakpoint that happens to
12537 be set at the same address. */
12538 return 0;
12539 }
12540
12541 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12542 }
12543
12544 static int
12545 bkpt_resources_needed (const struct bp_location *bl)
12546 {
12547 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12548
12549 return 1;
12550 }
12551
12552 static enum print_stop_action
12553 bkpt_print_it (bpstat bs)
12554 {
12555 struct breakpoint *b;
12556 const struct bp_location *bl;
12557 int bp_temp;
12558 struct ui_out *uiout = current_uiout;
12559
12560 gdb_assert (bs->bp_location_at != NULL);
12561
12562 bl = bs->bp_location_at;
12563 b = bs->breakpoint_at;
12564
12565 bp_temp = b->disposition == disp_del;
12566 if (bl->address != bl->requested_address)
12567 breakpoint_adjustment_warning (bl->requested_address,
12568 bl->address,
12569 b->number, 1);
12570 annotate_breakpoint (b->number);
12571 maybe_print_thread_hit_breakpoint (uiout);
12572
12573 if (uiout->is_mi_like_p ())
12574 {
12575 uiout->field_string ("reason",
12576 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12577 uiout->field_string ("disp", bpdisp_text (b->disposition));
12578 }
12579 if (bp_temp)
12580 uiout->message ("Temporary breakpoint %pF, ",
12581 signed_field ("bkptno", b->number));
12582 else
12583 uiout->message ("Breakpoint %pF, ",
12584 signed_field ("bkptno", b->number));
12585
12586 return PRINT_SRC_AND_LOC;
12587 }
12588
12589 static void
12590 bkpt_print_mention (struct breakpoint *b)
12591 {
12592 if (current_uiout->is_mi_like_p ())
12593 return;
12594
12595 switch (b->type)
12596 {
12597 case bp_breakpoint:
12598 case bp_gnu_ifunc_resolver:
12599 if (b->disposition == disp_del)
12600 printf_filtered (_("Temporary breakpoint"));
12601 else
12602 printf_filtered (_("Breakpoint"));
12603 printf_filtered (_(" %d"), b->number);
12604 if (b->type == bp_gnu_ifunc_resolver)
12605 printf_filtered (_(" at gnu-indirect-function resolver"));
12606 break;
12607 case bp_hardware_breakpoint:
12608 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12609 break;
12610 case bp_dprintf:
12611 printf_filtered (_("Dprintf %d"), b->number);
12612 break;
12613 }
12614
12615 say_where (b);
12616 }
12617
12618 static void
12619 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12620 {
12621 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12622 fprintf_unfiltered (fp, "tbreak");
12623 else if (tp->type == bp_breakpoint)
12624 fprintf_unfiltered (fp, "break");
12625 else if (tp->type == bp_hardware_breakpoint
12626 && tp->disposition == disp_del)
12627 fprintf_unfiltered (fp, "thbreak");
12628 else if (tp->type == bp_hardware_breakpoint)
12629 fprintf_unfiltered (fp, "hbreak");
12630 else
12631 internal_error (__FILE__, __LINE__,
12632 _("unhandled breakpoint type %d"), (int) tp->type);
12633
12634 fprintf_unfiltered (fp, " %s",
12635 event_location_to_string (tp->location.get ()));
12636
12637 /* Print out extra_string if this breakpoint is pending. It might
12638 contain, for example, conditions that were set by the user. */
12639 if (tp->loc == NULL && tp->extra_string != NULL)
12640 fprintf_unfiltered (fp, " %s", tp->extra_string);
12641
12642 print_recreate_thread (tp, fp);
12643 }
12644
12645 static void
12646 bkpt_create_sals_from_location (struct event_location *location,
12647 struct linespec_result *canonical,
12648 enum bptype type_wanted)
12649 {
12650 create_sals_from_location_default (location, canonical, type_wanted);
12651 }
12652
12653 static void
12654 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12655 struct linespec_result *canonical,
12656 gdb::unique_xmalloc_ptr<char> cond_string,
12657 gdb::unique_xmalloc_ptr<char> extra_string,
12658 enum bptype type_wanted,
12659 enum bpdisp disposition,
12660 int thread,
12661 int task, int ignore_count,
12662 const struct breakpoint_ops *ops,
12663 int from_tty, int enabled,
12664 int internal, unsigned flags)
12665 {
12666 create_breakpoints_sal_default (gdbarch, canonical,
12667 std::move (cond_string),
12668 std::move (extra_string),
12669 type_wanted,
12670 disposition, thread, task,
12671 ignore_count, ops, from_tty,
12672 enabled, internal, flags);
12673 }
12674
12675 static std::vector<symtab_and_line>
12676 bkpt_decode_location (struct breakpoint *b,
12677 struct event_location *location,
12678 struct program_space *search_pspace)
12679 {
12680 return decode_location_default (b, location, search_pspace);
12681 }
12682
12683 /* Virtual table for internal breakpoints. */
12684
12685 static void
12686 internal_bkpt_re_set (struct breakpoint *b)
12687 {
12688 switch (b->type)
12689 {
12690 /* Delete overlay event and longjmp master breakpoints; they
12691 will be reset later by breakpoint_re_set. */
12692 case bp_overlay_event:
12693 case bp_longjmp_master:
12694 case bp_std_terminate_master:
12695 case bp_exception_master:
12696 delete_breakpoint (b);
12697 break;
12698
12699 /* This breakpoint is special, it's set up when the inferior
12700 starts and we really don't want to touch it. */
12701 case bp_shlib_event:
12702
12703 /* Like bp_shlib_event, this breakpoint type is special. Once
12704 it is set up, we do not want to touch it. */
12705 case bp_thread_event:
12706 break;
12707 }
12708 }
12709
12710 static void
12711 internal_bkpt_check_status (bpstat bs)
12712 {
12713 if (bs->breakpoint_at->type == bp_shlib_event)
12714 {
12715 /* If requested, stop when the dynamic linker notifies GDB of
12716 events. This allows the user to get control and place
12717 breakpoints in initializer routines for dynamically loaded
12718 objects (among other things). */
12719 bs->stop = stop_on_solib_events;
12720 bs->print = stop_on_solib_events;
12721 }
12722 else
12723 bs->stop = 0;
12724 }
12725
12726 static enum print_stop_action
12727 internal_bkpt_print_it (bpstat bs)
12728 {
12729 struct breakpoint *b;
12730
12731 b = bs->breakpoint_at;
12732
12733 switch (b->type)
12734 {
12735 case bp_shlib_event:
12736 /* Did we stop because the user set the stop_on_solib_events
12737 variable? (If so, we report this as a generic, "Stopped due
12738 to shlib event" message.) */
12739 print_solib_event (0);
12740 break;
12741
12742 case bp_thread_event:
12743 /* Not sure how we will get here.
12744 GDB should not stop for these breakpoints. */
12745 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12746 break;
12747
12748 case bp_overlay_event:
12749 /* By analogy with the thread event, GDB should not stop for these. */
12750 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12751 break;
12752
12753 case bp_longjmp_master:
12754 /* These should never be enabled. */
12755 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12756 break;
12757
12758 case bp_std_terminate_master:
12759 /* These should never be enabled. */
12760 printf_filtered (_("std::terminate Master Breakpoint: "
12761 "gdb should not stop!\n"));
12762 break;
12763
12764 case bp_exception_master:
12765 /* These should never be enabled. */
12766 printf_filtered (_("Exception Master Breakpoint: "
12767 "gdb should not stop!\n"));
12768 break;
12769 }
12770
12771 return PRINT_NOTHING;
12772 }
12773
12774 static void
12775 internal_bkpt_print_mention (struct breakpoint *b)
12776 {
12777 /* Nothing to mention. These breakpoints are internal. */
12778 }
12779
12780 /* Virtual table for momentary breakpoints */
12781
12782 static void
12783 momentary_bkpt_re_set (struct breakpoint *b)
12784 {
12785 /* Keep temporary breakpoints, which can be encountered when we step
12786 over a dlopen call and solib_add is resetting the breakpoints.
12787 Otherwise these should have been blown away via the cleanup chain
12788 or by breakpoint_init_inferior when we rerun the executable. */
12789 }
12790
12791 static void
12792 momentary_bkpt_check_status (bpstat bs)
12793 {
12794 /* Nothing. The point of these breakpoints is causing a stop. */
12795 }
12796
12797 static enum print_stop_action
12798 momentary_bkpt_print_it (bpstat bs)
12799 {
12800 return PRINT_UNKNOWN;
12801 }
12802
12803 static void
12804 momentary_bkpt_print_mention (struct breakpoint *b)
12805 {
12806 /* Nothing to mention. These breakpoints are internal. */
12807 }
12808
12809 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12810
12811 It gets cleared already on the removal of the first one of such placed
12812 breakpoints. This is OK as they get all removed altogether. */
12813
12814 longjmp_breakpoint::~longjmp_breakpoint ()
12815 {
12816 thread_info *tp = find_thread_global_id (this->thread);
12817
12818 if (tp != NULL)
12819 tp->initiating_frame = null_frame_id;
12820 }
12821
12822 /* Specific methods for probe breakpoints. */
12823
12824 static int
12825 bkpt_probe_insert_location (struct bp_location *bl)
12826 {
12827 int v = bkpt_insert_location (bl);
12828
12829 if (v == 0)
12830 {
12831 /* The insertion was successful, now let's set the probe's semaphore
12832 if needed. */
12833 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
12834 }
12835
12836 return v;
12837 }
12838
12839 static int
12840 bkpt_probe_remove_location (struct bp_location *bl,
12841 enum remove_bp_reason reason)
12842 {
12843 /* Let's clear the semaphore before removing the location. */
12844 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
12845
12846 return bkpt_remove_location (bl, reason);
12847 }
12848
12849 static void
12850 bkpt_probe_create_sals_from_location (struct event_location *location,
12851 struct linespec_result *canonical,
12852 enum bptype type_wanted)
12853 {
12854 struct linespec_sals lsal;
12855
12856 lsal.sals = parse_probes (location, NULL, canonical);
12857 lsal.canonical
12858 = xstrdup (event_location_to_string (canonical->location.get ()));
12859 canonical->lsals.push_back (std::move (lsal));
12860 }
12861
12862 static std::vector<symtab_and_line>
12863 bkpt_probe_decode_location (struct breakpoint *b,
12864 struct event_location *location,
12865 struct program_space *search_pspace)
12866 {
12867 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12868 if (sals.empty ())
12869 error (_("probe not found"));
12870 return sals;
12871 }
12872
12873 /* The breakpoint_ops structure to be used in tracepoints. */
12874
12875 static void
12876 tracepoint_re_set (struct breakpoint *b)
12877 {
12878 breakpoint_re_set_default (b);
12879 }
12880
12881 static int
12882 tracepoint_breakpoint_hit (const struct bp_location *bl,
12883 const address_space *aspace, CORE_ADDR bp_addr,
12884 const struct target_waitstatus *ws)
12885 {
12886 /* By definition, the inferior does not report stops at
12887 tracepoints. */
12888 return 0;
12889 }
12890
12891 static void
12892 tracepoint_print_one_detail (const struct breakpoint *self,
12893 struct ui_out *uiout)
12894 {
12895 struct tracepoint *tp = (struct tracepoint *) self;
12896 if (!tp->static_trace_marker_id.empty ())
12897 {
12898 gdb_assert (self->type == bp_static_tracepoint);
12899
12900 uiout->message ("\tmarker id is %pF\n",
12901 string_field ("static-tracepoint-marker-string-id",
12902 tp->static_trace_marker_id.c_str ()));
12903 }
12904 }
12905
12906 static void
12907 tracepoint_print_mention (struct breakpoint *b)
12908 {
12909 if (current_uiout->is_mi_like_p ())
12910 return;
12911
12912 switch (b->type)
12913 {
12914 case bp_tracepoint:
12915 printf_filtered (_("Tracepoint"));
12916 printf_filtered (_(" %d"), b->number);
12917 break;
12918 case bp_fast_tracepoint:
12919 printf_filtered (_("Fast tracepoint"));
12920 printf_filtered (_(" %d"), b->number);
12921 break;
12922 case bp_static_tracepoint:
12923 printf_filtered (_("Static tracepoint"));
12924 printf_filtered (_(" %d"), b->number);
12925 break;
12926 default:
12927 internal_error (__FILE__, __LINE__,
12928 _("unhandled tracepoint type %d"), (int) b->type);
12929 }
12930
12931 say_where (b);
12932 }
12933
12934 static void
12935 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
12936 {
12937 struct tracepoint *tp = (struct tracepoint *) self;
12938
12939 if (self->type == bp_fast_tracepoint)
12940 fprintf_unfiltered (fp, "ftrace");
12941 else if (self->type == bp_static_tracepoint)
12942 fprintf_unfiltered (fp, "strace");
12943 else if (self->type == bp_tracepoint)
12944 fprintf_unfiltered (fp, "trace");
12945 else
12946 internal_error (__FILE__, __LINE__,
12947 _("unhandled tracepoint type %d"), (int) self->type);
12948
12949 fprintf_unfiltered (fp, " %s",
12950 event_location_to_string (self->location.get ()));
12951 print_recreate_thread (self, fp);
12952
12953 if (tp->pass_count)
12954 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
12955 }
12956
12957 static void
12958 tracepoint_create_sals_from_location (struct event_location *location,
12959 struct linespec_result *canonical,
12960 enum bptype type_wanted)
12961 {
12962 create_sals_from_location_default (location, canonical, type_wanted);
12963 }
12964
12965 static void
12966 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12967 struct linespec_result *canonical,
12968 gdb::unique_xmalloc_ptr<char> cond_string,
12969 gdb::unique_xmalloc_ptr<char> extra_string,
12970 enum bptype type_wanted,
12971 enum bpdisp disposition,
12972 int thread,
12973 int task, int ignore_count,
12974 const struct breakpoint_ops *ops,
12975 int from_tty, int enabled,
12976 int internal, unsigned flags)
12977 {
12978 create_breakpoints_sal_default (gdbarch, canonical,
12979 std::move (cond_string),
12980 std::move (extra_string),
12981 type_wanted,
12982 disposition, thread, task,
12983 ignore_count, ops, from_tty,
12984 enabled, internal, flags);
12985 }
12986
12987 static std::vector<symtab_and_line>
12988 tracepoint_decode_location (struct breakpoint *b,
12989 struct event_location *location,
12990 struct program_space *search_pspace)
12991 {
12992 return decode_location_default (b, location, search_pspace);
12993 }
12994
12995 struct breakpoint_ops tracepoint_breakpoint_ops;
12996
12997 /* Virtual table for tracepoints on static probes. */
12998
12999 static void
13000 tracepoint_probe_create_sals_from_location
13001 (struct event_location *location,
13002 struct linespec_result *canonical,
13003 enum bptype type_wanted)
13004 {
13005 /* We use the same method for breakpoint on probes. */
13006 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13007 }
13008
13009 static std::vector<symtab_and_line>
13010 tracepoint_probe_decode_location (struct breakpoint *b,
13011 struct event_location *location,
13012 struct program_space *search_pspace)
13013 {
13014 /* We use the same method for breakpoint on probes. */
13015 return bkpt_probe_decode_location (b, location, search_pspace);
13016 }
13017
13018 /* Dprintf breakpoint_ops methods. */
13019
13020 static void
13021 dprintf_re_set (struct breakpoint *b)
13022 {
13023 breakpoint_re_set_default (b);
13024
13025 /* extra_string should never be non-NULL for dprintf. */
13026 gdb_assert (b->extra_string != NULL);
13027
13028 /* 1 - connect to target 1, that can run breakpoint commands.
13029 2 - create a dprintf, which resolves fine.
13030 3 - disconnect from target 1
13031 4 - connect to target 2, that can NOT run breakpoint commands.
13032
13033 After steps #3/#4, you'll want the dprintf command list to
13034 be updated, because target 1 and 2 may well return different
13035 answers for target_can_run_breakpoint_commands().
13036 Given absence of finer grained resetting, we get to do
13037 it all the time. */
13038 if (b->extra_string != NULL)
13039 update_dprintf_command_list (b);
13040 }
13041
13042 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13043
13044 static void
13045 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13046 {
13047 fprintf_unfiltered (fp, "dprintf %s,%s",
13048 event_location_to_string (tp->location.get ()),
13049 tp->extra_string);
13050 print_recreate_thread (tp, fp);
13051 }
13052
13053 /* Implement the "after_condition_true" breakpoint_ops method for
13054 dprintf.
13055
13056 dprintf's are implemented with regular commands in their command
13057 list, but we run the commands here instead of before presenting the
13058 stop to the user, as dprintf's don't actually cause a stop. This
13059 also makes it so that the commands of multiple dprintfs at the same
13060 address are all handled. */
13061
13062 static void
13063 dprintf_after_condition_true (struct bpstats *bs)
13064 {
13065 struct bpstats tmp_bs;
13066 struct bpstats *tmp_bs_p = &tmp_bs;
13067
13068 /* dprintf's never cause a stop. This wasn't set in the
13069 check_status hook instead because that would make the dprintf's
13070 condition not be evaluated. */
13071 bs->stop = 0;
13072
13073 /* Run the command list here. Take ownership of it instead of
13074 copying. We never want these commands to run later in
13075 bpstat_do_actions, if a breakpoint that causes a stop happens to
13076 be set at same address as this dprintf, or even if running the
13077 commands here throws. */
13078 tmp_bs.commands = bs->commands;
13079 bs->commands = NULL;
13080
13081 bpstat_do_actions_1 (&tmp_bs_p);
13082
13083 /* 'tmp_bs.commands' will usually be NULL by now, but
13084 bpstat_do_actions_1 may return early without processing the whole
13085 list. */
13086 }
13087
13088 /* The breakpoint_ops structure to be used on static tracepoints with
13089 markers (`-m'). */
13090
13091 static void
13092 strace_marker_create_sals_from_location (struct event_location *location,
13093 struct linespec_result *canonical,
13094 enum bptype type_wanted)
13095 {
13096 struct linespec_sals lsal;
13097 const char *arg_start, *arg;
13098
13099 arg = arg_start = get_linespec_location (location)->spec_string;
13100 lsal.sals = decode_static_tracepoint_spec (&arg);
13101
13102 std::string str (arg_start, arg - arg_start);
13103 const char *ptr = str.c_str ();
13104 canonical->location
13105 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
13106
13107 lsal.canonical
13108 = xstrdup (event_location_to_string (canonical->location.get ()));
13109 canonical->lsals.push_back (std::move (lsal));
13110 }
13111
13112 static void
13113 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13114 struct linespec_result *canonical,
13115 gdb::unique_xmalloc_ptr<char> cond_string,
13116 gdb::unique_xmalloc_ptr<char> extra_string,
13117 enum bptype type_wanted,
13118 enum bpdisp disposition,
13119 int thread,
13120 int task, int ignore_count,
13121 const struct breakpoint_ops *ops,
13122 int from_tty, int enabled,
13123 int internal, unsigned flags)
13124 {
13125 const linespec_sals &lsal = canonical->lsals[0];
13126
13127 /* If the user is creating a static tracepoint by marker id
13128 (strace -m MARKER_ID), then store the sals index, so that
13129 breakpoint_re_set can try to match up which of the newly
13130 found markers corresponds to this one, and, don't try to
13131 expand multiple locations for each sal, given than SALS
13132 already should contain all sals for MARKER_ID. */
13133
13134 for (size_t i = 0; i < lsal.sals.size (); i++)
13135 {
13136 event_location_up location
13137 = copy_event_location (canonical->location.get ());
13138
13139 std::unique_ptr<tracepoint> tp (new tracepoint ());
13140 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
13141 std::move (location), NULL,
13142 std::move (cond_string),
13143 std::move (extra_string),
13144 type_wanted, disposition,
13145 thread, task, ignore_count, ops,
13146 from_tty, enabled, internal, flags,
13147 canonical->special_display);
13148 /* Given that its possible to have multiple markers with
13149 the same string id, if the user is creating a static
13150 tracepoint by marker id ("strace -m MARKER_ID"), then
13151 store the sals index, so that breakpoint_re_set can
13152 try to match up which of the newly found markers
13153 corresponds to this one */
13154 tp->static_trace_marker_id_idx = i;
13155
13156 install_breakpoint (internal, std::move (tp), 0);
13157 }
13158 }
13159
13160 static std::vector<symtab_and_line>
13161 strace_marker_decode_location (struct breakpoint *b,
13162 struct event_location *location,
13163 struct program_space *search_pspace)
13164 {
13165 struct tracepoint *tp = (struct tracepoint *) b;
13166 const char *s = get_linespec_location (location)->spec_string;
13167
13168 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13169 if (sals.size () > tp->static_trace_marker_id_idx)
13170 {
13171 sals[0] = sals[tp->static_trace_marker_id_idx];
13172 sals.resize (1);
13173 return sals;
13174 }
13175 else
13176 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
13177 }
13178
13179 static struct breakpoint_ops strace_marker_breakpoint_ops;
13180
13181 static int
13182 strace_marker_p (struct breakpoint *b)
13183 {
13184 return b->ops == &strace_marker_breakpoint_ops;
13185 }
13186
13187 /* Delete a breakpoint and clean up all traces of it in the data
13188 structures. */
13189
13190 void
13191 delete_breakpoint (struct breakpoint *bpt)
13192 {
13193 struct breakpoint *b;
13194
13195 gdb_assert (bpt != NULL);
13196
13197 /* Has this bp already been deleted? This can happen because
13198 multiple lists can hold pointers to bp's. bpstat lists are
13199 especial culprits.
13200
13201 One example of this happening is a watchpoint's scope bp. When
13202 the scope bp triggers, we notice that the watchpoint is out of
13203 scope, and delete it. We also delete its scope bp. But the
13204 scope bp is marked "auto-deleting", and is already on a bpstat.
13205 That bpstat is then checked for auto-deleting bp's, which are
13206 deleted.
13207
13208 A real solution to this problem might involve reference counts in
13209 bp's, and/or giving them pointers back to their referencing
13210 bpstat's, and teaching delete_breakpoint to only free a bp's
13211 storage when no more references were extent. A cheaper bandaid
13212 was chosen. */
13213 if (bpt->type == bp_none)
13214 return;
13215
13216 /* At least avoid this stale reference until the reference counting
13217 of breakpoints gets resolved. */
13218 if (bpt->related_breakpoint != bpt)
13219 {
13220 struct breakpoint *related;
13221 struct watchpoint *w;
13222
13223 if (bpt->type == bp_watchpoint_scope)
13224 w = (struct watchpoint *) bpt->related_breakpoint;
13225 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13226 w = (struct watchpoint *) bpt;
13227 else
13228 w = NULL;
13229 if (w != NULL)
13230 watchpoint_del_at_next_stop (w);
13231
13232 /* Unlink bpt from the bpt->related_breakpoint ring. */
13233 for (related = bpt; related->related_breakpoint != bpt;
13234 related = related->related_breakpoint);
13235 related->related_breakpoint = bpt->related_breakpoint;
13236 bpt->related_breakpoint = bpt;
13237 }
13238
13239 /* watch_command_1 creates a watchpoint but only sets its number if
13240 update_watchpoint succeeds in creating its bp_locations. If there's
13241 a problem in that process, we'll be asked to delete the half-created
13242 watchpoint. In that case, don't announce the deletion. */
13243 if (bpt->number)
13244 gdb::observers::breakpoint_deleted.notify (bpt);
13245
13246 if (breakpoint_chain == bpt)
13247 breakpoint_chain = bpt->next;
13248
13249 ALL_BREAKPOINTS (b)
13250 if (b->next == bpt)
13251 {
13252 b->next = bpt->next;
13253 break;
13254 }
13255
13256 /* Be sure no bpstat's are pointing at the breakpoint after it's
13257 been freed. */
13258 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13259 in all threads for now. Note that we cannot just remove bpstats
13260 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13261 commands are associated with the bpstat; if we remove it here,
13262 then the later call to bpstat_do_actions (&stop_bpstat); in
13263 event-top.c won't do anything, and temporary breakpoints with
13264 commands won't work. */
13265
13266 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13267
13268 /* Now that breakpoint is removed from breakpoint list, update the
13269 global location list. This will remove locations that used to
13270 belong to this breakpoint. Do this before freeing the breakpoint
13271 itself, since remove_breakpoint looks at location's owner. It
13272 might be better design to have location completely
13273 self-contained, but it's not the case now. */
13274 update_global_location_list (UGLL_DONT_INSERT);
13275
13276 /* On the chance that someone will soon try again to delete this
13277 same bp, we mark it as deleted before freeing its storage. */
13278 bpt->type = bp_none;
13279 delete bpt;
13280 }
13281
13282 /* Iterator function to call a user-provided callback function once
13283 for each of B and its related breakpoints. */
13284
13285 static void
13286 iterate_over_related_breakpoints (struct breakpoint *b,
13287 gdb::function_view<void (breakpoint *)> function)
13288 {
13289 struct breakpoint *related;
13290
13291 related = b;
13292 do
13293 {
13294 struct breakpoint *next;
13295
13296 /* FUNCTION may delete RELATED. */
13297 next = related->related_breakpoint;
13298
13299 if (next == related)
13300 {
13301 /* RELATED is the last ring entry. */
13302 function (related);
13303
13304 /* FUNCTION may have deleted it, so we'd never reach back to
13305 B. There's nothing left to do anyway, so just break
13306 out. */
13307 break;
13308 }
13309 else
13310 function (related);
13311
13312 related = next;
13313 }
13314 while (related != b);
13315 }
13316
13317 static void
13318 delete_command (const char *arg, int from_tty)
13319 {
13320 struct breakpoint *b, *b_tmp;
13321
13322 dont_repeat ();
13323
13324 if (arg == 0)
13325 {
13326 int breaks_to_delete = 0;
13327
13328 /* Delete all breakpoints if no argument. Do not delete
13329 internal breakpoints, these have to be deleted with an
13330 explicit breakpoint number argument. */
13331 ALL_BREAKPOINTS (b)
13332 if (user_breakpoint_p (b))
13333 {
13334 breaks_to_delete = 1;
13335 break;
13336 }
13337
13338 /* Ask user only if there are some breakpoints to delete. */
13339 if (!from_tty
13340 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13341 {
13342 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13343 if (user_breakpoint_p (b))
13344 delete_breakpoint (b);
13345 }
13346 }
13347 else
13348 map_breakpoint_numbers
13349 (arg, [&] (breakpoint *br)
13350 {
13351 iterate_over_related_breakpoints (br, delete_breakpoint);
13352 });
13353 }
13354
13355 /* Return true if all locations of B bound to PSPACE are pending. If
13356 PSPACE is NULL, all locations of all program spaces are
13357 considered. */
13358
13359 static int
13360 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13361 {
13362 struct bp_location *loc;
13363
13364 for (loc = b->loc; loc != NULL; loc = loc->next)
13365 if ((pspace == NULL
13366 || loc->pspace == pspace)
13367 && !loc->shlib_disabled
13368 && !loc->pspace->executing_startup)
13369 return 0;
13370 return 1;
13371 }
13372
13373 /* Subroutine of update_breakpoint_locations to simplify it.
13374 Return non-zero if multiple fns in list LOC have the same name.
13375 Null names are ignored. */
13376
13377 static int
13378 ambiguous_names_p (struct bp_location *loc)
13379 {
13380 struct bp_location *l;
13381 htab_up htab (htab_create_alloc (13, htab_hash_string, streq_hash, NULL,
13382 xcalloc, xfree));
13383
13384 for (l = loc; l != NULL; l = l->next)
13385 {
13386 const char **slot;
13387 const char *name = l->function_name;
13388
13389 /* Allow for some names to be NULL, ignore them. */
13390 if (name == NULL)
13391 continue;
13392
13393 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
13394 INSERT);
13395 /* NOTE: We can assume slot != NULL here because xcalloc never
13396 returns NULL. */
13397 if (*slot != NULL)
13398 return 1;
13399 *slot = name;
13400 }
13401
13402 return 0;
13403 }
13404
13405 /* When symbols change, it probably means the sources changed as well,
13406 and it might mean the static tracepoint markers are no longer at
13407 the same address or line numbers they used to be at last we
13408 checked. Losing your static tracepoints whenever you rebuild is
13409 undesirable. This function tries to resync/rematch gdb static
13410 tracepoints with the markers on the target, for static tracepoints
13411 that have not been set by marker id. Static tracepoint that have
13412 been set by marker id are reset by marker id in breakpoint_re_set.
13413 The heuristic is:
13414
13415 1) For a tracepoint set at a specific address, look for a marker at
13416 the old PC. If one is found there, assume to be the same marker.
13417 If the name / string id of the marker found is different from the
13418 previous known name, assume that means the user renamed the marker
13419 in the sources, and output a warning.
13420
13421 2) For a tracepoint set at a given line number, look for a marker
13422 at the new address of the old line number. If one is found there,
13423 assume to be the same marker. If the name / string id of the
13424 marker found is different from the previous known name, assume that
13425 means the user renamed the marker in the sources, and output a
13426 warning.
13427
13428 3) If a marker is no longer found at the same address or line, it
13429 may mean the marker no longer exists. But it may also just mean
13430 the code changed a bit. Maybe the user added a few lines of code
13431 that made the marker move up or down (in line number terms). Ask
13432 the target for info about the marker with the string id as we knew
13433 it. If found, update line number and address in the matching
13434 static tracepoint. This will get confused if there's more than one
13435 marker with the same ID (possible in UST, although unadvised
13436 precisely because it confuses tools). */
13437
13438 static struct symtab_and_line
13439 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13440 {
13441 struct tracepoint *tp = (struct tracepoint *) b;
13442 struct static_tracepoint_marker marker;
13443 CORE_ADDR pc;
13444
13445 pc = sal.pc;
13446 if (sal.line)
13447 find_line_pc (sal.symtab, sal.line, &pc);
13448
13449 if (target_static_tracepoint_marker_at (pc, &marker))
13450 {
13451 if (tp->static_trace_marker_id != marker.str_id)
13452 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13453 b->number, tp->static_trace_marker_id.c_str (),
13454 marker.str_id.c_str ());
13455
13456 tp->static_trace_marker_id = std::move (marker.str_id);
13457
13458 return sal;
13459 }
13460
13461 /* Old marker wasn't found on target at lineno. Try looking it up
13462 by string ID. */
13463 if (!sal.explicit_pc
13464 && sal.line != 0
13465 && sal.symtab != NULL
13466 && !tp->static_trace_marker_id.empty ())
13467 {
13468 std::vector<static_tracepoint_marker> markers
13469 = target_static_tracepoint_markers_by_strid
13470 (tp->static_trace_marker_id.c_str ());
13471
13472 if (!markers.empty ())
13473 {
13474 struct symbol *sym;
13475 struct static_tracepoint_marker *tpmarker;
13476 struct ui_out *uiout = current_uiout;
13477 struct explicit_location explicit_loc;
13478
13479 tpmarker = &markers[0];
13480
13481 tp->static_trace_marker_id = std::move (tpmarker->str_id);
13482
13483 warning (_("marker for static tracepoint %d (%s) not "
13484 "found at previous line number"),
13485 b->number, tp->static_trace_marker_id.c_str ());
13486
13487 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
13488 sym = find_pc_sect_function (tpmarker->address, NULL);
13489 uiout->text ("Now in ");
13490 if (sym)
13491 {
13492 uiout->field_string ("func", sym->print_name (),
13493 function_name_style.style ());
13494 uiout->text (" at ");
13495 }
13496 uiout->field_string ("file",
13497 symtab_to_filename_for_display (sal2.symtab),
13498 file_name_style.style ());
13499 uiout->text (":");
13500
13501 if (uiout->is_mi_like_p ())
13502 {
13503 const char *fullname = symtab_to_fullname (sal2.symtab);
13504
13505 uiout->field_string ("fullname", fullname);
13506 }
13507
13508 uiout->field_signed ("line", sal2.line);
13509 uiout->text ("\n");
13510
13511 b->loc->line_number = sal2.line;
13512 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13513
13514 b->location.reset (NULL);
13515 initialize_explicit_location (&explicit_loc);
13516 explicit_loc.source_filename
13517 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13518 explicit_loc.line_offset.offset = b->loc->line_number;
13519 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13520 b->location = new_explicit_location (&explicit_loc);
13521
13522 /* Might be nice to check if function changed, and warn if
13523 so. */
13524 }
13525 }
13526 return sal;
13527 }
13528
13529 /* Returns 1 iff locations A and B are sufficiently same that
13530 we don't need to report breakpoint as changed. */
13531
13532 static int
13533 locations_are_equal (struct bp_location *a, struct bp_location *b)
13534 {
13535 while (a && b)
13536 {
13537 if (a->address != b->address)
13538 return 0;
13539
13540 if (a->shlib_disabled != b->shlib_disabled)
13541 return 0;
13542
13543 if (a->enabled != b->enabled)
13544 return 0;
13545
13546 if (a->disabled_by_cond != b->disabled_by_cond)
13547 return 0;
13548
13549 a = a->next;
13550 b = b->next;
13551 }
13552
13553 if ((a == NULL) != (b == NULL))
13554 return 0;
13555
13556 return 1;
13557 }
13558
13559 /* Split all locations of B that are bound to PSPACE out of B's
13560 location list to a separate list and return that list's head. If
13561 PSPACE is NULL, hoist out all locations of B. */
13562
13563 static struct bp_location *
13564 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13565 {
13566 struct bp_location head;
13567 struct bp_location *i = b->loc;
13568 struct bp_location **i_link = &b->loc;
13569 struct bp_location *hoisted = &head;
13570
13571 if (pspace == NULL)
13572 {
13573 i = b->loc;
13574 b->loc = NULL;
13575 return i;
13576 }
13577
13578 head.next = NULL;
13579
13580 while (i != NULL)
13581 {
13582 if (i->pspace == pspace)
13583 {
13584 *i_link = i->next;
13585 i->next = NULL;
13586 hoisted->next = i;
13587 hoisted = i;
13588 }
13589 else
13590 i_link = &i->next;
13591 i = *i_link;
13592 }
13593
13594 return head.next;
13595 }
13596
13597 /* Create new breakpoint locations for B (a hardware or software
13598 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13599 zero, then B is a ranged breakpoint. Only recreates locations for
13600 FILTER_PSPACE. Locations of other program spaces are left
13601 untouched. */
13602
13603 void
13604 update_breakpoint_locations (struct breakpoint *b,
13605 struct program_space *filter_pspace,
13606 gdb::array_view<const symtab_and_line> sals,
13607 gdb::array_view<const symtab_and_line> sals_end)
13608 {
13609 struct bp_location *existing_locations;
13610
13611 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
13612 {
13613 /* Ranged breakpoints have only one start location and one end
13614 location. */
13615 b->enable_state = bp_disabled;
13616 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13617 "multiple locations found\n"),
13618 b->number);
13619 return;
13620 }
13621
13622 /* If there's no new locations, and all existing locations are
13623 pending, don't do anything. This optimizes the common case where
13624 all locations are in the same shared library, that was unloaded.
13625 We'd like to retain the location, so that when the library is
13626 loaded again, we don't loose the enabled/disabled status of the
13627 individual locations. */
13628 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
13629 return;
13630
13631 existing_locations = hoist_existing_locations (b, filter_pspace);
13632
13633 for (const auto &sal : sals)
13634 {
13635 struct bp_location *new_loc;
13636
13637 switch_to_program_space_and_thread (sal.pspace);
13638
13639 new_loc = add_location_to_breakpoint (b, &sal);
13640
13641 /* Reparse conditions, they might contain references to the
13642 old symtab. */
13643 if (b->cond_string != NULL)
13644 {
13645 const char *s;
13646
13647 s = b->cond_string;
13648 try
13649 {
13650 new_loc->cond = parse_exp_1 (&s, sal.pc,
13651 block_for_pc (sal.pc),
13652 0);
13653 }
13654 catch (const gdb_exception_error &e)
13655 {
13656 new_loc->disabled_by_cond = true;
13657 }
13658 }
13659
13660 if (!sals_end.empty ())
13661 {
13662 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13663
13664 new_loc->length = end - sals[0].pc + 1;
13665 }
13666 }
13667
13668 /* If possible, carry over 'disable' status from existing
13669 breakpoints. */
13670 {
13671 struct bp_location *e = existing_locations;
13672 /* If there are multiple breakpoints with the same function name,
13673 e.g. for inline functions, comparing function names won't work.
13674 Instead compare pc addresses; this is just a heuristic as things
13675 may have moved, but in practice it gives the correct answer
13676 often enough until a better solution is found. */
13677 int have_ambiguous_names = ambiguous_names_p (b->loc);
13678
13679 for (; e; e = e->next)
13680 {
13681 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
13682 {
13683 struct bp_location *l = b->loc;
13684 if (have_ambiguous_names)
13685 {
13686 for (; l; l = l->next)
13687 {
13688 /* Ignore software vs hardware location type at
13689 this point, because with "set breakpoint
13690 auto-hw", after a re-set, locations that were
13691 hardware can end up as software, or vice versa.
13692 As mentioned above, this is an heuristic and in
13693 practice should give the correct answer often
13694 enough. */
13695 if (breakpoint_locations_match (e, l, true))
13696 {
13697 l->enabled = e->enabled;
13698 l->disabled_by_cond = e->disabled_by_cond;
13699 break;
13700 }
13701 }
13702 }
13703 else
13704 {
13705 for (; l; l = l->next)
13706 if (l->function_name
13707 && strcmp (e->function_name, l->function_name) == 0)
13708 {
13709 l->enabled = e->enabled;
13710 l->disabled_by_cond = e->disabled_by_cond;
13711 break;
13712 }
13713 }
13714 }
13715 }
13716 }
13717
13718 if (!locations_are_equal (existing_locations, b->loc))
13719 gdb::observers::breakpoint_modified.notify (b);
13720 }
13721
13722 /* Find the SaL locations corresponding to the given LOCATION.
13723 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13724
13725 static std::vector<symtab_and_line>
13726 location_to_sals (struct breakpoint *b, struct event_location *location,
13727 struct program_space *search_pspace, int *found)
13728 {
13729 struct gdb_exception exception;
13730
13731 gdb_assert (b->ops != NULL);
13732
13733 std::vector<symtab_and_line> sals;
13734
13735 try
13736 {
13737 sals = b->ops->decode_location (b, location, search_pspace);
13738 }
13739 catch (gdb_exception_error &e)
13740 {
13741 int not_found_and_ok = 0;
13742
13743 /* For pending breakpoints, it's expected that parsing will
13744 fail until the right shared library is loaded. User has
13745 already told to create pending breakpoints and don't need
13746 extra messages. If breakpoint is in bp_shlib_disabled
13747 state, then user already saw the message about that
13748 breakpoint being disabled, and don't want to see more
13749 errors. */
13750 if (e.error == NOT_FOUND_ERROR
13751 && (b->condition_not_parsed
13752 || (b->loc != NULL
13753 && search_pspace != NULL
13754 && b->loc->pspace != search_pspace)
13755 || (b->loc && b->loc->shlib_disabled)
13756 || (b->loc && b->loc->pspace->executing_startup)
13757 || b->enable_state == bp_disabled))
13758 not_found_and_ok = 1;
13759
13760 if (!not_found_and_ok)
13761 {
13762 /* We surely don't want to warn about the same breakpoint
13763 10 times. One solution, implemented here, is disable
13764 the breakpoint on error. Another solution would be to
13765 have separate 'warning emitted' flag. Since this
13766 happens only when a binary has changed, I don't know
13767 which approach is better. */
13768 b->enable_state = bp_disabled;
13769 throw;
13770 }
13771
13772 exception = std::move (e);
13773 }
13774
13775 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13776 {
13777 for (auto &sal : sals)
13778 resolve_sal_pc (&sal);
13779 if (b->condition_not_parsed && b->extra_string != NULL)
13780 {
13781 char *cond_string, *extra_string;
13782 int thread, task;
13783
13784 find_condition_and_thread_for_sals (sals, b->extra_string,
13785 &cond_string, &thread,
13786 &task, &extra_string);
13787 gdb_assert (b->cond_string == NULL);
13788 if (cond_string)
13789 b->cond_string = cond_string;
13790 b->thread = thread;
13791 b->task = task;
13792 if (extra_string)
13793 {
13794 xfree (b->extra_string);
13795 b->extra_string = extra_string;
13796 }
13797 b->condition_not_parsed = 0;
13798 }
13799
13800 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13801 sals[0] = update_static_tracepoint (b, sals[0]);
13802
13803 *found = 1;
13804 }
13805 else
13806 *found = 0;
13807
13808 return sals;
13809 }
13810
13811 /* The default re_set method, for typical hardware or software
13812 breakpoints. Reevaluate the breakpoint and recreate its
13813 locations. */
13814
13815 static void
13816 breakpoint_re_set_default (struct breakpoint *b)
13817 {
13818 struct program_space *filter_pspace = current_program_space;
13819 std::vector<symtab_and_line> expanded, expanded_end;
13820
13821 int found;
13822 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13823 filter_pspace, &found);
13824 if (found)
13825 expanded = std::move (sals);
13826
13827 if (b->location_range_end != NULL)
13828 {
13829 std::vector<symtab_and_line> sals_end
13830 = location_to_sals (b, b->location_range_end.get (),
13831 filter_pspace, &found);
13832 if (found)
13833 expanded_end = std::move (sals_end);
13834 }
13835
13836 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
13837 }
13838
13839 /* Default method for creating SALs from an address string. It basically
13840 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13841
13842 static void
13843 create_sals_from_location_default (struct event_location *location,
13844 struct linespec_result *canonical,
13845 enum bptype type_wanted)
13846 {
13847 parse_breakpoint_sals (location, canonical);
13848 }
13849
13850 /* Call create_breakpoints_sal for the given arguments. This is the default
13851 function for the `create_breakpoints_sal' method of
13852 breakpoint_ops. */
13853
13854 static void
13855 create_breakpoints_sal_default (struct gdbarch *gdbarch,
13856 struct linespec_result *canonical,
13857 gdb::unique_xmalloc_ptr<char> cond_string,
13858 gdb::unique_xmalloc_ptr<char> extra_string,
13859 enum bptype type_wanted,
13860 enum bpdisp disposition,
13861 int thread,
13862 int task, int ignore_count,
13863 const struct breakpoint_ops *ops,
13864 int from_tty, int enabled,
13865 int internal, unsigned flags)
13866 {
13867 create_breakpoints_sal (gdbarch, canonical,
13868 std::move (cond_string),
13869 std::move (extra_string),
13870 type_wanted, disposition,
13871 thread, task, ignore_count, ops, from_tty,
13872 enabled, internal, flags);
13873 }
13874
13875 /* Decode the line represented by S by calling decode_line_full. This is the
13876 default function for the `decode_location' method of breakpoint_ops. */
13877
13878 static std::vector<symtab_and_line>
13879 decode_location_default (struct breakpoint *b,
13880 struct event_location *location,
13881 struct program_space *search_pspace)
13882 {
13883 struct linespec_result canonical;
13884
13885 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
13886 NULL, 0, &canonical, multiple_symbols_all,
13887 b->filter.get ());
13888
13889 /* We should get 0 or 1 resulting SALs. */
13890 gdb_assert (canonical.lsals.size () < 2);
13891
13892 if (!canonical.lsals.empty ())
13893 {
13894 const linespec_sals &lsal = canonical.lsals[0];
13895 return std::move (lsal.sals);
13896 }
13897 return {};
13898 }
13899
13900 /* Reset a breakpoint. */
13901
13902 static void
13903 breakpoint_re_set_one (breakpoint *b)
13904 {
13905 input_radix = b->input_radix;
13906 set_language (b->language);
13907
13908 b->ops->re_set (b);
13909 }
13910
13911 /* Re-set breakpoint locations for the current program space.
13912 Locations bound to other program spaces are left untouched. */
13913
13914 void
13915 breakpoint_re_set (void)
13916 {
13917 struct breakpoint *b, *b_tmp;
13918
13919 {
13920 scoped_restore_current_language save_language;
13921 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
13922 scoped_restore_current_pspace_and_thread restore_pspace_thread;
13923
13924 /* breakpoint_re_set_one sets the current_language to the language
13925 of the breakpoint it is resetting (see prepare_re_set_context)
13926 before re-evaluating the breakpoint's location. This change can
13927 unfortunately get undone by accident if the language_mode is set
13928 to auto, and we either switch frames, or more likely in this context,
13929 we select the current frame.
13930
13931 We prevent this by temporarily turning the language_mode to
13932 language_mode_manual. We restore it once all breakpoints
13933 have been reset. */
13934 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13935 language_mode = language_mode_manual;
13936
13937 /* Note: we must not try to insert locations until after all
13938 breakpoints have been re-set. Otherwise, e.g., when re-setting
13939 breakpoint 1, we'd insert the locations of breakpoint 2, which
13940 hadn't been re-set yet, and thus may have stale locations. */
13941
13942 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13943 {
13944 try
13945 {
13946 breakpoint_re_set_one (b);
13947 }
13948 catch (const gdb_exception &ex)
13949 {
13950 exception_fprintf (gdb_stderr, ex,
13951 "Error in re-setting breakpoint %d: ",
13952 b->number);
13953 }
13954 }
13955
13956 jit_breakpoint_re_set ();
13957 }
13958
13959 create_overlay_event_breakpoint ();
13960 create_longjmp_master_breakpoint ();
13961 create_std_terminate_master_breakpoint ();
13962 create_exception_master_breakpoint ();
13963
13964 /* Now we can insert. */
13965 update_global_location_list (UGLL_MAY_INSERT);
13966 }
13967 \f
13968 /* Reset the thread number of this breakpoint:
13969
13970 - If the breakpoint is for all threads, leave it as-is.
13971 - Else, reset it to the current thread for inferior_ptid. */
13972 void
13973 breakpoint_re_set_thread (struct breakpoint *b)
13974 {
13975 if (b->thread != -1)
13976 {
13977 b->thread = inferior_thread ()->global_num;
13978
13979 /* We're being called after following a fork. The new fork is
13980 selected as current, and unless this was a vfork will have a
13981 different program space from the original thread. Reset that
13982 as well. */
13983 b->loc->pspace = current_program_space;
13984 }
13985 }
13986
13987 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13988 If from_tty is nonzero, it prints a message to that effect,
13989 which ends with a period (no newline). */
13990
13991 void
13992 set_ignore_count (int bptnum, int count, int from_tty)
13993 {
13994 struct breakpoint *b;
13995
13996 if (count < 0)
13997 count = 0;
13998
13999 ALL_BREAKPOINTS (b)
14000 if (b->number == bptnum)
14001 {
14002 if (is_tracepoint (b))
14003 {
14004 if (from_tty && count != 0)
14005 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14006 bptnum);
14007 return;
14008 }
14009
14010 b->ignore_count = count;
14011 if (from_tty)
14012 {
14013 if (count == 0)
14014 printf_filtered (_("Will stop next time "
14015 "breakpoint %d is reached."),
14016 bptnum);
14017 else if (count == 1)
14018 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14019 bptnum);
14020 else
14021 printf_filtered (_("Will ignore next %d "
14022 "crossings of breakpoint %d."),
14023 count, bptnum);
14024 }
14025 gdb::observers::breakpoint_modified.notify (b);
14026 return;
14027 }
14028
14029 error (_("No breakpoint number %d."), bptnum);
14030 }
14031
14032 /* Command to set ignore-count of breakpoint N to COUNT. */
14033
14034 static void
14035 ignore_command (const char *args, int from_tty)
14036 {
14037 const char *p = args;
14038 int num;
14039
14040 if (p == 0)
14041 error_no_arg (_("a breakpoint number"));
14042
14043 num = get_number (&p);
14044 if (num == 0)
14045 error (_("bad breakpoint number: '%s'"), args);
14046 if (*p == 0)
14047 error (_("Second argument (specified ignore-count) is missing."));
14048
14049 set_ignore_count (num,
14050 longest_to_int (value_as_long (parse_and_eval (p))),
14051 from_tty);
14052 if (from_tty)
14053 printf_filtered ("\n");
14054 }
14055 \f
14056
14057 /* Call FUNCTION on each of the breakpoints with numbers in the range
14058 defined by BP_NUM_RANGE (an inclusive range). */
14059
14060 static void
14061 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
14062 gdb::function_view<void (breakpoint *)> function)
14063 {
14064 if (bp_num_range.first == 0)
14065 {
14066 warning (_("bad breakpoint number at or near '%d'"),
14067 bp_num_range.first);
14068 }
14069 else
14070 {
14071 struct breakpoint *b, *tmp;
14072
14073 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
14074 {
14075 bool match = false;
14076
14077 ALL_BREAKPOINTS_SAFE (b, tmp)
14078 if (b->number == i)
14079 {
14080 match = true;
14081 function (b);
14082 break;
14083 }
14084 if (!match)
14085 printf_unfiltered (_("No breakpoint number %d.\n"), i);
14086 }
14087 }
14088 }
14089
14090 /* Call FUNCTION on each of the breakpoints whose numbers are given in
14091 ARGS. */
14092
14093 static void
14094 map_breakpoint_numbers (const char *args,
14095 gdb::function_view<void (breakpoint *)> function)
14096 {
14097 if (args == NULL || *args == '\0')
14098 error_no_arg (_("one or more breakpoint numbers"));
14099
14100 number_or_range_parser parser (args);
14101
14102 while (!parser.finished ())
14103 {
14104 int num = parser.get_number ();
14105 map_breakpoint_number_range (std::make_pair (num, num), function);
14106 }
14107 }
14108
14109 /* Return the breakpoint location structure corresponding to the
14110 BP_NUM and LOC_NUM values. */
14111
14112 static struct bp_location *
14113 find_location_by_number (int bp_num, int loc_num)
14114 {
14115 struct breakpoint *b;
14116
14117 ALL_BREAKPOINTS (b)
14118 if (b->number == bp_num)
14119 {
14120 break;
14121 }
14122
14123 if (!b || b->number != bp_num)
14124 error (_("Bad breakpoint number '%d'"), bp_num);
14125
14126 if (loc_num == 0)
14127 error (_("Bad breakpoint location number '%d'"), loc_num);
14128
14129 int n = 0;
14130 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
14131 if (++n == loc_num)
14132 return loc;
14133
14134 error (_("Bad breakpoint location number '%d'"), loc_num);
14135 }
14136
14137 /* Modes of operation for extract_bp_num. */
14138 enum class extract_bp_kind
14139 {
14140 /* Extracting a breakpoint number. */
14141 bp,
14142
14143 /* Extracting a location number. */
14144 loc,
14145 };
14146
14147 /* Extract a breakpoint or location number (as determined by KIND)
14148 from the string starting at START. TRAILER is a character which
14149 can be found after the number. If you don't want a trailer, use
14150 '\0'. If END_OUT is not NULL, it is set to point after the parsed
14151 string. This always returns a positive integer. */
14152
14153 static int
14154 extract_bp_num (extract_bp_kind kind, const char *start,
14155 int trailer, const char **end_out = NULL)
14156 {
14157 const char *end = start;
14158 int num = get_number_trailer (&end, trailer);
14159 if (num < 0)
14160 error (kind == extract_bp_kind::bp
14161 ? _("Negative breakpoint number '%.*s'")
14162 : _("Negative breakpoint location number '%.*s'"),
14163 int (end - start), start);
14164 if (num == 0)
14165 error (kind == extract_bp_kind::bp
14166 ? _("Bad breakpoint number '%.*s'")
14167 : _("Bad breakpoint location number '%.*s'"),
14168 int (end - start), start);
14169
14170 if (end_out != NULL)
14171 *end_out = end;
14172 return num;
14173 }
14174
14175 /* Extract a breakpoint or location range (as determined by KIND) in
14176 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14177 representing the (inclusive) range. The returned pair's elements
14178 are always positive integers. */
14179
14180 static std::pair<int, int>
14181 extract_bp_or_bp_range (extract_bp_kind kind,
14182 const std::string &arg,
14183 std::string::size_type arg_offset)
14184 {
14185 std::pair<int, int> range;
14186 const char *bp_loc = &arg[arg_offset];
14187 std::string::size_type dash = arg.find ('-', arg_offset);
14188 if (dash != std::string::npos)
14189 {
14190 /* bp_loc is a range (x-z). */
14191 if (arg.length () == dash + 1)
14192 error (kind == extract_bp_kind::bp
14193 ? _("Bad breakpoint number at or near: '%s'")
14194 : _("Bad breakpoint location number at or near: '%s'"),
14195 bp_loc);
14196
14197 const char *end;
14198 const char *start_first = bp_loc;
14199 const char *start_second = &arg[dash + 1];
14200 range.first = extract_bp_num (kind, start_first, '-');
14201 range.second = extract_bp_num (kind, start_second, '\0', &end);
14202
14203 if (range.first > range.second)
14204 error (kind == extract_bp_kind::bp
14205 ? _("Inverted breakpoint range at '%.*s'")
14206 : _("Inverted breakpoint location range at '%.*s'"),
14207 int (end - start_first), start_first);
14208 }
14209 else
14210 {
14211 /* bp_loc is a single value. */
14212 range.first = extract_bp_num (kind, bp_loc, '\0');
14213 range.second = range.first;
14214 }
14215 return range;
14216 }
14217
14218 /* Extract the breakpoint/location range specified by ARG. Returns
14219 the breakpoint range in BP_NUM_RANGE, and the location range in
14220 BP_LOC_RANGE.
14221
14222 ARG may be in any of the following forms:
14223
14224 x where 'x' is a breakpoint number.
14225 x-y where 'x' and 'y' specify a breakpoint numbers range.
14226 x.y where 'x' is a breakpoint number and 'y' a location number.
14227 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14228 location number range.
14229 */
14230
14231 static void
14232 extract_bp_number_and_location (const std::string &arg,
14233 std::pair<int, int> &bp_num_range,
14234 std::pair<int, int> &bp_loc_range)
14235 {
14236 std::string::size_type dot = arg.find ('.');
14237
14238 if (dot != std::string::npos)
14239 {
14240 /* Handle 'x.y' and 'x.y-z' cases. */
14241
14242 if (arg.length () == dot + 1 || dot == 0)
14243 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
14244
14245 bp_num_range.first
14246 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14247 bp_num_range.second = bp_num_range.first;
14248
14249 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14250 arg, dot + 1);
14251 }
14252 else
14253 {
14254 /* Handle x and x-y cases. */
14255
14256 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
14257 bp_loc_range.first = 0;
14258 bp_loc_range.second = 0;
14259 }
14260 }
14261
14262 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14263 specifies whether to enable or disable. */
14264
14265 static void
14266 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14267 {
14268 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14269 if (loc != NULL)
14270 {
14271 if (loc->disabled_by_cond && enable)
14272 error (_("Breakpoint %d's condition is invalid at location %d, "
14273 "cannot enable."), bp_num, loc_num);
14274
14275 if (loc->enabled != enable)
14276 {
14277 loc->enabled = enable;
14278 mark_breakpoint_location_modified (loc);
14279 }
14280 if (target_supports_enable_disable_tracepoint ()
14281 && current_trace_status ()->running && loc->owner
14282 && is_tracepoint (loc->owner))
14283 target_disable_tracepoint (loc);
14284 }
14285 update_global_location_list (UGLL_DONT_INSERT);
14286
14287 gdb::observers::breakpoint_modified.notify (loc->owner);
14288 }
14289
14290 /* Enable or disable a range of breakpoint locations. BP_NUM is the
14291 number of the breakpoint, and BP_LOC_RANGE specifies the
14292 (inclusive) range of location numbers of that breakpoint to
14293 enable/disable. ENABLE specifies whether to enable or disable the
14294 location. */
14295
14296 static void
14297 enable_disable_breakpoint_location_range (int bp_num,
14298 std::pair<int, int> &bp_loc_range,
14299 bool enable)
14300 {
14301 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14302 enable_disable_bp_num_loc (bp_num, i, enable);
14303 }
14304
14305 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14306 If from_tty is nonzero, it prints a message to that effect,
14307 which ends with a period (no newline). */
14308
14309 void
14310 disable_breakpoint (struct breakpoint *bpt)
14311 {
14312 /* Never disable a watchpoint scope breakpoint; we want to
14313 hit them when we leave scope so we can delete both the
14314 watchpoint and its scope breakpoint at that time. */
14315 if (bpt->type == bp_watchpoint_scope)
14316 return;
14317
14318 bpt->enable_state = bp_disabled;
14319
14320 /* Mark breakpoint locations modified. */
14321 mark_breakpoint_modified (bpt);
14322
14323 if (target_supports_enable_disable_tracepoint ()
14324 && current_trace_status ()->running && is_tracepoint (bpt))
14325 {
14326 struct bp_location *location;
14327
14328 for (location = bpt->loc; location; location = location->next)
14329 target_disable_tracepoint (location);
14330 }
14331
14332 update_global_location_list (UGLL_DONT_INSERT);
14333
14334 gdb::observers::breakpoint_modified.notify (bpt);
14335 }
14336
14337 /* Enable or disable the breakpoint(s) or breakpoint location(s)
14338 specified in ARGS. ARGS may be in any of the formats handled by
14339 extract_bp_number_and_location. ENABLE specifies whether to enable
14340 or disable the breakpoints/locations. */
14341
14342 static void
14343 enable_disable_command (const char *args, int from_tty, bool enable)
14344 {
14345 if (args == 0)
14346 {
14347 struct breakpoint *bpt;
14348
14349 ALL_BREAKPOINTS (bpt)
14350 if (user_breakpoint_p (bpt))
14351 {
14352 if (enable)
14353 enable_breakpoint (bpt);
14354 else
14355 disable_breakpoint (bpt);
14356 }
14357 }
14358 else
14359 {
14360 std::string num = extract_arg (&args);
14361
14362 while (!num.empty ())
14363 {
14364 std::pair<int, int> bp_num_range, bp_loc_range;
14365
14366 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14367
14368 if (bp_loc_range.first == bp_loc_range.second
14369 && bp_loc_range.first == 0)
14370 {
14371 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14372 map_breakpoint_number_range (bp_num_range,
14373 enable
14374 ? enable_breakpoint
14375 : disable_breakpoint);
14376 }
14377 else
14378 {
14379 /* Handle breakpoint ids with formats 'x.y' or
14380 'x.y-z'. */
14381 enable_disable_breakpoint_location_range
14382 (bp_num_range.first, bp_loc_range, enable);
14383 }
14384 num = extract_arg (&args);
14385 }
14386 }
14387 }
14388
14389 /* The disable command disables the specified breakpoints/locations
14390 (or all defined breakpoints) so they're no longer effective in
14391 stopping the inferior. ARGS may be in any of the forms defined in
14392 extract_bp_number_and_location. */
14393
14394 static void
14395 disable_command (const char *args, int from_tty)
14396 {
14397 enable_disable_command (args, from_tty, false);
14398 }
14399
14400 static void
14401 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14402 int count)
14403 {
14404 int target_resources_ok;
14405
14406 if (bpt->type == bp_hardware_breakpoint)
14407 {
14408 int i;
14409 i = hw_breakpoint_used_count ();
14410 target_resources_ok =
14411 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14412 i + 1, 0);
14413 if (target_resources_ok == 0)
14414 error (_("No hardware breakpoint support in the target."));
14415 else if (target_resources_ok < 0)
14416 error (_("Hardware breakpoints used exceeds limit."));
14417 }
14418
14419 if (is_watchpoint (bpt))
14420 {
14421 /* Initialize it just to avoid a GCC false warning. */
14422 enum enable_state orig_enable_state = bp_disabled;
14423
14424 try
14425 {
14426 struct watchpoint *w = (struct watchpoint *) bpt;
14427
14428 orig_enable_state = bpt->enable_state;
14429 bpt->enable_state = bp_enabled;
14430 update_watchpoint (w, 1 /* reparse */);
14431 }
14432 catch (const gdb_exception &e)
14433 {
14434 bpt->enable_state = orig_enable_state;
14435 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14436 bpt->number);
14437 return;
14438 }
14439 }
14440
14441 bpt->enable_state = bp_enabled;
14442
14443 /* Mark breakpoint locations modified. */
14444 mark_breakpoint_modified (bpt);
14445
14446 if (target_supports_enable_disable_tracepoint ()
14447 && current_trace_status ()->running && is_tracepoint (bpt))
14448 {
14449 struct bp_location *location;
14450
14451 for (location = bpt->loc; location; location = location->next)
14452 target_enable_tracepoint (location);
14453 }
14454
14455 bpt->disposition = disposition;
14456 bpt->enable_count = count;
14457 update_global_location_list (UGLL_MAY_INSERT);
14458
14459 gdb::observers::breakpoint_modified.notify (bpt);
14460 }
14461
14462
14463 void
14464 enable_breakpoint (struct breakpoint *bpt)
14465 {
14466 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14467 }
14468
14469 /* The enable command enables the specified breakpoints/locations (or
14470 all defined breakpoints) so they once again become (or continue to
14471 be) effective in stopping the inferior. ARGS may be in any of the
14472 forms defined in extract_bp_number_and_location. */
14473
14474 static void
14475 enable_command (const char *args, int from_tty)
14476 {
14477 enable_disable_command (args, from_tty, true);
14478 }
14479
14480 static void
14481 enable_once_command (const char *args, int from_tty)
14482 {
14483 map_breakpoint_numbers
14484 (args, [&] (breakpoint *b)
14485 {
14486 iterate_over_related_breakpoints
14487 (b, [&] (breakpoint *bpt)
14488 {
14489 enable_breakpoint_disp (bpt, disp_disable, 1);
14490 });
14491 });
14492 }
14493
14494 static void
14495 enable_count_command (const char *args, int from_tty)
14496 {
14497 int count;
14498
14499 if (args == NULL)
14500 error_no_arg (_("hit count"));
14501
14502 count = get_number (&args);
14503
14504 map_breakpoint_numbers
14505 (args, [&] (breakpoint *b)
14506 {
14507 iterate_over_related_breakpoints
14508 (b, [&] (breakpoint *bpt)
14509 {
14510 enable_breakpoint_disp (bpt, disp_disable, count);
14511 });
14512 });
14513 }
14514
14515 static void
14516 enable_delete_command (const char *args, int from_tty)
14517 {
14518 map_breakpoint_numbers
14519 (args, [&] (breakpoint *b)
14520 {
14521 iterate_over_related_breakpoints
14522 (b, [&] (breakpoint *bpt)
14523 {
14524 enable_breakpoint_disp (bpt, disp_del, 1);
14525 });
14526 });
14527 }
14528 \f
14529 /* Invalidate last known value of any hardware watchpoint if
14530 the memory which that value represents has been written to by
14531 GDB itself. */
14532
14533 static void
14534 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14535 CORE_ADDR addr, ssize_t len,
14536 const bfd_byte *data)
14537 {
14538 struct breakpoint *bp;
14539
14540 ALL_BREAKPOINTS (bp)
14541 if (bp->enable_state == bp_enabled
14542 && bp->type == bp_hardware_watchpoint)
14543 {
14544 struct watchpoint *wp = (struct watchpoint *) bp;
14545
14546 if (wp->val_valid && wp->val != nullptr)
14547 {
14548 struct bp_location *loc;
14549
14550 for (loc = bp->loc; loc != NULL; loc = loc->next)
14551 if (loc->loc_type == bp_loc_hardware_watchpoint
14552 && loc->address + loc->length > addr
14553 && addr + len > loc->address)
14554 {
14555 wp->val = NULL;
14556 wp->val_valid = false;
14557 }
14558 }
14559 }
14560 }
14561
14562 /* Create and insert a breakpoint for software single step. */
14563
14564 void
14565 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14566 const address_space *aspace,
14567 CORE_ADDR next_pc)
14568 {
14569 struct thread_info *tp = inferior_thread ();
14570 struct symtab_and_line sal;
14571 CORE_ADDR pc = next_pc;
14572
14573 if (tp->control.single_step_breakpoints == NULL)
14574 {
14575 tp->control.single_step_breakpoints
14576 = new_single_step_breakpoint (tp->global_num, gdbarch);
14577 }
14578
14579 sal = find_pc_line (pc, 0);
14580 sal.pc = pc;
14581 sal.section = find_pc_overlay (pc);
14582 sal.explicit_pc = 1;
14583 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14584
14585 update_global_location_list (UGLL_INSERT);
14586 }
14587
14588 /* Insert single step breakpoints according to the current state. */
14589
14590 int
14591 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14592 {
14593 struct regcache *regcache = get_current_regcache ();
14594 std::vector<CORE_ADDR> next_pcs;
14595
14596 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14597
14598 if (!next_pcs.empty ())
14599 {
14600 struct frame_info *frame = get_current_frame ();
14601 const address_space *aspace = get_frame_address_space (frame);
14602
14603 for (CORE_ADDR pc : next_pcs)
14604 insert_single_step_breakpoint (gdbarch, aspace, pc);
14605
14606 return 1;
14607 }
14608 else
14609 return 0;
14610 }
14611
14612 /* See breakpoint.h. */
14613
14614 int
14615 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14616 const address_space *aspace,
14617 CORE_ADDR pc)
14618 {
14619 struct bp_location *loc;
14620
14621 for (loc = bp->loc; loc != NULL; loc = loc->next)
14622 if (loc->inserted
14623 && breakpoint_location_address_match (loc, aspace, pc))
14624 return 1;
14625
14626 return 0;
14627 }
14628
14629 /* Check whether a software single-step breakpoint is inserted at
14630 PC. */
14631
14632 int
14633 single_step_breakpoint_inserted_here_p (const address_space *aspace,
14634 CORE_ADDR pc)
14635 {
14636 struct breakpoint *bpt;
14637
14638 ALL_BREAKPOINTS (bpt)
14639 {
14640 if (bpt->type == bp_single_step
14641 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14642 return 1;
14643 }
14644 return 0;
14645 }
14646
14647 /* Tracepoint-specific operations. */
14648
14649 /* Set tracepoint count to NUM. */
14650 static void
14651 set_tracepoint_count (int num)
14652 {
14653 tracepoint_count = num;
14654 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14655 }
14656
14657 static void
14658 trace_command (const char *arg, int from_tty)
14659 {
14660 event_location_up location = string_to_event_location (&arg,
14661 current_language);
14662 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
14663 (location.get (), true /* is_tracepoint */);
14664
14665 create_breakpoint (get_current_arch (),
14666 location.get (),
14667 NULL, 0, arg, 1 /* parse arg */,
14668 0 /* tempflag */,
14669 bp_tracepoint /* type_wanted */,
14670 0 /* Ignore count */,
14671 pending_break_support,
14672 ops,
14673 from_tty,
14674 1 /* enabled */,
14675 0 /* internal */, 0);
14676 }
14677
14678 static void
14679 ftrace_command (const char *arg, int from_tty)
14680 {
14681 event_location_up location = string_to_event_location (&arg,
14682 current_language);
14683 create_breakpoint (get_current_arch (),
14684 location.get (),
14685 NULL, 0, arg, 1 /* parse arg */,
14686 0 /* tempflag */,
14687 bp_fast_tracepoint /* type_wanted */,
14688 0 /* Ignore count */,
14689 pending_break_support,
14690 &tracepoint_breakpoint_ops,
14691 from_tty,
14692 1 /* enabled */,
14693 0 /* internal */, 0);
14694 }
14695
14696 /* strace command implementation. Creates a static tracepoint. */
14697
14698 static void
14699 strace_command (const char *arg, int from_tty)
14700 {
14701 struct breakpoint_ops *ops;
14702 event_location_up location;
14703
14704 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14705 or with a normal static tracepoint. */
14706 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14707 {
14708 ops = &strace_marker_breakpoint_ops;
14709 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
14710 }
14711 else
14712 {
14713 ops = &tracepoint_breakpoint_ops;
14714 location = string_to_event_location (&arg, current_language);
14715 }
14716
14717 create_breakpoint (get_current_arch (),
14718 location.get (),
14719 NULL, 0, arg, 1 /* parse arg */,
14720 0 /* tempflag */,
14721 bp_static_tracepoint /* type_wanted */,
14722 0 /* Ignore count */,
14723 pending_break_support,
14724 ops,
14725 from_tty,
14726 1 /* enabled */,
14727 0 /* internal */, 0);
14728 }
14729
14730 /* Set up a fake reader function that gets command lines from a linked
14731 list that was acquired during tracepoint uploading. */
14732
14733 static struct uploaded_tp *this_utp;
14734 static int next_cmd;
14735
14736 static char *
14737 read_uploaded_action (void)
14738 {
14739 char *rslt = nullptr;
14740
14741 if (next_cmd < this_utp->cmd_strings.size ())
14742 {
14743 rslt = this_utp->cmd_strings[next_cmd].get ();
14744 next_cmd++;
14745 }
14746
14747 return rslt;
14748 }
14749
14750 /* Given information about a tracepoint as recorded on a target (which
14751 can be either a live system or a trace file), attempt to create an
14752 equivalent GDB tracepoint. This is not a reliable process, since
14753 the target does not necessarily have all the information used when
14754 the tracepoint was originally defined. */
14755
14756 struct tracepoint *
14757 create_tracepoint_from_upload (struct uploaded_tp *utp)
14758 {
14759 const char *addr_str;
14760 char small_buf[100];
14761 struct tracepoint *tp;
14762
14763 if (utp->at_string)
14764 addr_str = utp->at_string.get ();
14765 else
14766 {
14767 /* In the absence of a source location, fall back to raw
14768 address. Since there is no way to confirm that the address
14769 means the same thing as when the trace was started, warn the
14770 user. */
14771 warning (_("Uploaded tracepoint %d has no "
14772 "source location, using raw address"),
14773 utp->number);
14774 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14775 addr_str = small_buf;
14776 }
14777
14778 /* There's not much we can do with a sequence of bytecodes. */
14779 if (utp->cond && !utp->cond_string)
14780 warning (_("Uploaded tracepoint %d condition "
14781 "has no source form, ignoring it"),
14782 utp->number);
14783
14784 event_location_up location = string_to_event_location (&addr_str,
14785 current_language);
14786 if (!create_breakpoint (get_current_arch (),
14787 location.get (),
14788 utp->cond_string.get (), -1, addr_str,
14789 0 /* parse cond/thread */,
14790 0 /* tempflag */,
14791 utp->type /* type_wanted */,
14792 0 /* Ignore count */,
14793 pending_break_support,
14794 &tracepoint_breakpoint_ops,
14795 0 /* from_tty */,
14796 utp->enabled /* enabled */,
14797 0 /* internal */,
14798 CREATE_BREAKPOINT_FLAGS_INSERTED))
14799 return NULL;
14800
14801 /* Get the tracepoint we just created. */
14802 tp = get_tracepoint (tracepoint_count);
14803 gdb_assert (tp != NULL);
14804
14805 if (utp->pass > 0)
14806 {
14807 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14808 tp->number);
14809
14810 trace_pass_command (small_buf, 0);
14811 }
14812
14813 /* If we have uploaded versions of the original commands, set up a
14814 special-purpose "reader" function and call the usual command line
14815 reader, then pass the result to the breakpoint command-setting
14816 function. */
14817 if (!utp->cmd_strings.empty ())
14818 {
14819 counted_command_line cmd_list;
14820
14821 this_utp = utp;
14822 next_cmd = 0;
14823
14824 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
14825
14826 breakpoint_set_commands (tp, std::move (cmd_list));
14827 }
14828 else if (!utp->actions.empty ()
14829 || !utp->step_actions.empty ())
14830 warning (_("Uploaded tracepoint %d actions "
14831 "have no source form, ignoring them"),
14832 utp->number);
14833
14834 /* Copy any status information that might be available. */
14835 tp->hit_count = utp->hit_count;
14836 tp->traceframe_usage = utp->traceframe_usage;
14837
14838 return tp;
14839 }
14840
14841 /* Print information on tracepoint number TPNUM_EXP, or all if
14842 omitted. */
14843
14844 static void
14845 info_tracepoints_command (const char *args, int from_tty)
14846 {
14847 struct ui_out *uiout = current_uiout;
14848 int num_printed;
14849
14850 num_printed = breakpoint_1 (args, false, is_tracepoint);
14851
14852 if (num_printed == 0)
14853 {
14854 if (args == NULL || *args == '\0')
14855 uiout->message ("No tracepoints.\n");
14856 else
14857 uiout->message ("No tracepoint matching '%s'.\n", args);
14858 }
14859
14860 default_collect_info ();
14861 }
14862
14863 /* The 'enable trace' command enables tracepoints.
14864 Not supported by all targets. */
14865 static void
14866 enable_trace_command (const char *args, int from_tty)
14867 {
14868 enable_command (args, from_tty);
14869 }
14870
14871 /* The 'disable trace' command disables tracepoints.
14872 Not supported by all targets. */
14873 static void
14874 disable_trace_command (const char *args, int from_tty)
14875 {
14876 disable_command (args, from_tty);
14877 }
14878
14879 /* Remove a tracepoint (or all if no argument). */
14880 static void
14881 delete_trace_command (const char *arg, int from_tty)
14882 {
14883 struct breakpoint *b, *b_tmp;
14884
14885 dont_repeat ();
14886
14887 if (arg == 0)
14888 {
14889 int breaks_to_delete = 0;
14890
14891 /* Delete all breakpoints if no argument.
14892 Do not delete internal or call-dummy breakpoints, these
14893 have to be deleted with an explicit breakpoint number
14894 argument. */
14895 ALL_TRACEPOINTS (b)
14896 if (is_tracepoint (b) && user_breakpoint_p (b))
14897 {
14898 breaks_to_delete = 1;
14899 break;
14900 }
14901
14902 /* Ask user only if there are some breakpoints to delete. */
14903 if (!from_tty
14904 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14905 {
14906 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14907 if (is_tracepoint (b) && user_breakpoint_p (b))
14908 delete_breakpoint (b);
14909 }
14910 }
14911 else
14912 map_breakpoint_numbers
14913 (arg, [&] (breakpoint *br)
14914 {
14915 iterate_over_related_breakpoints (br, delete_breakpoint);
14916 });
14917 }
14918
14919 /* Helper function for trace_pass_command. */
14920
14921 static void
14922 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14923 {
14924 tp->pass_count = count;
14925 gdb::observers::breakpoint_modified.notify (tp);
14926 if (from_tty)
14927 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
14928 tp->number, count);
14929 }
14930
14931 /* Set passcount for tracepoint.
14932
14933 First command argument is passcount, second is tracepoint number.
14934 If tracepoint number omitted, apply to most recently defined.
14935 Also accepts special argument "all". */
14936
14937 static void
14938 trace_pass_command (const char *args, int from_tty)
14939 {
14940 struct tracepoint *t1;
14941 ULONGEST count;
14942
14943 if (args == 0 || *args == 0)
14944 error (_("passcount command requires an "
14945 "argument (count + optional TP num)"));
14946
14947 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
14948
14949 args = skip_spaces (args);
14950 if (*args && strncasecmp (args, "all", 3) == 0)
14951 {
14952 struct breakpoint *b;
14953
14954 args += 3; /* Skip special argument "all". */
14955 if (*args)
14956 error (_("Junk at end of arguments."));
14957
14958 ALL_TRACEPOINTS (b)
14959 {
14960 t1 = (struct tracepoint *) b;
14961 trace_pass_set_count (t1, count, from_tty);
14962 }
14963 }
14964 else if (*args == '\0')
14965 {
14966 t1 = get_tracepoint_by_number (&args, NULL);
14967 if (t1)
14968 trace_pass_set_count (t1, count, from_tty);
14969 }
14970 else
14971 {
14972 number_or_range_parser parser (args);
14973 while (!parser.finished ())
14974 {
14975 t1 = get_tracepoint_by_number (&args, &parser);
14976 if (t1)
14977 trace_pass_set_count (t1, count, from_tty);
14978 }
14979 }
14980 }
14981
14982 struct tracepoint *
14983 get_tracepoint (int num)
14984 {
14985 struct breakpoint *t;
14986
14987 ALL_TRACEPOINTS (t)
14988 if (t->number == num)
14989 return (struct tracepoint *) t;
14990
14991 return NULL;
14992 }
14993
14994 /* Find the tracepoint with the given target-side number (which may be
14995 different from the tracepoint number after disconnecting and
14996 reconnecting). */
14997
14998 struct tracepoint *
14999 get_tracepoint_by_number_on_target (int num)
15000 {
15001 struct breakpoint *b;
15002
15003 ALL_TRACEPOINTS (b)
15004 {
15005 struct tracepoint *t = (struct tracepoint *) b;
15006
15007 if (t->number_on_target == num)
15008 return t;
15009 }
15010
15011 return NULL;
15012 }
15013
15014 /* Utility: parse a tracepoint number and look it up in the list.
15015 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15016 If the argument is missing, the most recent tracepoint
15017 (tracepoint_count) is returned. */
15018
15019 struct tracepoint *
15020 get_tracepoint_by_number (const char **arg,
15021 number_or_range_parser *parser)
15022 {
15023 struct breakpoint *t;
15024 int tpnum;
15025 const char *instring = arg == NULL ? NULL : *arg;
15026
15027 if (parser != NULL)
15028 {
15029 gdb_assert (!parser->finished ());
15030 tpnum = parser->get_number ();
15031 }
15032 else if (arg == NULL || *arg == NULL || ! **arg)
15033 tpnum = tracepoint_count;
15034 else
15035 tpnum = get_number (arg);
15036
15037 if (tpnum <= 0)
15038 {
15039 if (instring && *instring)
15040 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15041 instring);
15042 else
15043 printf_filtered (_("No previous tracepoint\n"));
15044 return NULL;
15045 }
15046
15047 ALL_TRACEPOINTS (t)
15048 if (t->number == tpnum)
15049 {
15050 return (struct tracepoint *) t;
15051 }
15052
15053 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15054 return NULL;
15055 }
15056
15057 void
15058 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15059 {
15060 if (b->thread != -1)
15061 fprintf_unfiltered (fp, " thread %d", b->thread);
15062
15063 if (b->task != 0)
15064 fprintf_unfiltered (fp, " task %d", b->task);
15065
15066 fprintf_unfiltered (fp, "\n");
15067 }
15068
15069 /* Save information on user settable breakpoints (watchpoints, etc) to
15070 a new script file named FILENAME. If FILTER is non-NULL, call it
15071 on each breakpoint and only include the ones for which it returns
15072 true. */
15073
15074 static void
15075 save_breakpoints (const char *filename, int from_tty,
15076 bool (*filter) (const struct breakpoint *))
15077 {
15078 struct breakpoint *tp;
15079 int any = 0;
15080 int extra_trace_bits = 0;
15081
15082 if (filename == 0 || *filename == 0)
15083 error (_("Argument required (file name in which to save)"));
15084
15085 /* See if we have anything to save. */
15086 ALL_BREAKPOINTS (tp)
15087 {
15088 /* Skip internal and momentary breakpoints. */
15089 if (!user_breakpoint_p (tp))
15090 continue;
15091
15092 /* If we have a filter, only save the breakpoints it accepts. */
15093 if (filter && !filter (tp))
15094 continue;
15095
15096 any = 1;
15097
15098 if (is_tracepoint (tp))
15099 {
15100 extra_trace_bits = 1;
15101
15102 /* We can stop searching. */
15103 break;
15104 }
15105 }
15106
15107 if (!any)
15108 {
15109 warning (_("Nothing to save."));
15110 return;
15111 }
15112
15113 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
15114
15115 stdio_file fp;
15116
15117 if (!fp.open (expanded_filename.get (), "w"))
15118 error (_("Unable to open file '%s' for saving (%s)"),
15119 expanded_filename.get (), safe_strerror (errno));
15120
15121 if (extra_trace_bits)
15122 save_trace_state_variables (&fp);
15123
15124 ALL_BREAKPOINTS (tp)
15125 {
15126 /* Skip internal and momentary breakpoints. */
15127 if (!user_breakpoint_p (tp))
15128 continue;
15129
15130 /* If we have a filter, only save the breakpoints it accepts. */
15131 if (filter && !filter (tp))
15132 continue;
15133
15134 tp->ops->print_recreate (tp, &fp);
15135
15136 /* Note, we can't rely on tp->number for anything, as we can't
15137 assume the recreated breakpoint numbers will match. Use $bpnum
15138 instead. */
15139
15140 if (tp->cond_string)
15141 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15142
15143 if (tp->ignore_count)
15144 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15145
15146 if (tp->type != bp_dprintf && tp->commands)
15147 {
15148 fp.puts (" commands\n");
15149
15150 current_uiout->redirect (&fp);
15151 try
15152 {
15153 print_command_lines (current_uiout, tp->commands.get (), 2);
15154 }
15155 catch (const gdb_exception &ex)
15156 {
15157 current_uiout->redirect (NULL);
15158 throw;
15159 }
15160
15161 current_uiout->redirect (NULL);
15162 fp.puts (" end\n");
15163 }
15164
15165 if (tp->enable_state == bp_disabled)
15166 fp.puts ("disable $bpnum\n");
15167
15168 /* If this is a multi-location breakpoint, check if the locations
15169 should be individually disabled. Watchpoint locations are
15170 special, and not user visible. */
15171 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15172 {
15173 struct bp_location *loc;
15174 int n = 1;
15175
15176 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15177 if (!loc->enabled)
15178 fp.printf ("disable $bpnum.%d\n", n);
15179 }
15180 }
15181
15182 if (extra_trace_bits && *default_collect)
15183 fp.printf ("set default-collect %s\n", default_collect);
15184
15185 if (from_tty)
15186 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
15187 }
15188
15189 /* The `save breakpoints' command. */
15190
15191 static void
15192 save_breakpoints_command (const char *args, int from_tty)
15193 {
15194 save_breakpoints (args, from_tty, NULL);
15195 }
15196
15197 /* The `save tracepoints' command. */
15198
15199 static void
15200 save_tracepoints_command (const char *args, int from_tty)
15201 {
15202 save_breakpoints (args, from_tty, is_tracepoint);
15203 }
15204
15205 /* Create a vector of all tracepoints. */
15206
15207 std::vector<breakpoint *>
15208 all_tracepoints (void)
15209 {
15210 std::vector<breakpoint *> tp_vec;
15211 struct breakpoint *tp;
15212
15213 ALL_TRACEPOINTS (tp)
15214 {
15215 tp_vec.push_back (tp);
15216 }
15217
15218 return tp_vec;
15219 }
15220
15221 \f
15222 /* This help string is used to consolidate all the help string for specifying
15223 locations used by several commands. */
15224
15225 #define LOCATION_HELP_STRING \
15226 "Linespecs are colon-separated lists of location parameters, such as\n\
15227 source filename, function name, label name, and line number.\n\
15228 Example: To specify the start of a label named \"the_top\" in the\n\
15229 function \"fact\" in the file \"factorial.c\", use\n\
15230 \"factorial.c:fact:the_top\".\n\
15231 \n\
15232 Address locations begin with \"*\" and specify an exact address in the\n\
15233 program. Example: To specify the fourth byte past the start function\n\
15234 \"main\", use \"*main + 4\".\n\
15235 \n\
15236 Explicit locations are similar to linespecs but use an option/argument\n\
15237 syntax to specify location parameters.\n\
15238 Example: To specify the start of the label named \"the_top\" in the\n\
15239 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15240 -function fact -label the_top\".\n\
15241 \n\
15242 By default, a specified function is matched against the program's\n\
15243 functions in all scopes. For C++, this means in all namespaces and\n\
15244 classes. For Ada, this means in all packages. E.g., in C++,\n\
15245 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15246 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
15247 specified name as a complete fully-qualified name instead."
15248
15249 /* This help string is used for the break, hbreak, tbreak and thbreak
15250 commands. It is defined as a macro to prevent duplication.
15251 COMMAND should be a string constant containing the name of the
15252 command. */
15253
15254 #define BREAK_ARGS_HELP(command) \
15255 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15256 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15257 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15258 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15259 `-probe-dtrace' (for a DTrace probe).\n\
15260 LOCATION may be a linespec, address, or explicit location as described\n\
15261 below.\n\
15262 \n\
15263 With no LOCATION, uses current execution address of the selected\n\
15264 stack frame. This is useful for breaking on return to a stack frame.\n\
15265 \n\
15266 THREADNUM is the number from \"info threads\".\n\
15267 CONDITION is a boolean expression.\n\
15268 \n" LOCATION_HELP_STRING "\n\n\
15269 Multiple breakpoints at one place are permitted, and useful if their\n\
15270 conditions are different.\n\
15271 \n\
15272 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15273
15274 /* List of subcommands for "catch". */
15275 static struct cmd_list_element *catch_cmdlist;
15276
15277 /* List of subcommands for "tcatch". */
15278 static struct cmd_list_element *tcatch_cmdlist;
15279
15280 void
15281 add_catch_command (const char *name, const char *docstring,
15282 cmd_const_sfunc_ftype *sfunc,
15283 completer_ftype *completer,
15284 void *user_data_catch,
15285 void *user_data_tcatch)
15286 {
15287 struct cmd_list_element *command;
15288
15289 command = add_cmd (name, class_breakpoint, docstring,
15290 &catch_cmdlist);
15291 set_cmd_sfunc (command, sfunc);
15292 set_cmd_context (command, user_data_catch);
15293 set_cmd_completer (command, completer);
15294
15295 command = add_cmd (name, class_breakpoint, docstring,
15296 &tcatch_cmdlist);
15297 set_cmd_sfunc (command, sfunc);
15298 set_cmd_context (command, user_data_tcatch);
15299 set_cmd_completer (command, completer);
15300 }
15301
15302 struct breakpoint *
15303 iterate_over_breakpoints (gdb::function_view<bool (breakpoint *)> callback)
15304 {
15305 struct breakpoint *b, *b_tmp;
15306
15307 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15308 {
15309 if (callback (b))
15310 return b;
15311 }
15312
15313 return NULL;
15314 }
15315
15316 /* Zero if any of the breakpoint's locations could be a location where
15317 functions have been inlined, nonzero otherwise. */
15318
15319 static int
15320 is_non_inline_function (struct breakpoint *b)
15321 {
15322 /* The shared library event breakpoint is set on the address of a
15323 non-inline function. */
15324 if (b->type == bp_shlib_event)
15325 return 1;
15326
15327 return 0;
15328 }
15329
15330 /* Nonzero if the specified PC cannot be a location where functions
15331 have been inlined. */
15332
15333 int
15334 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
15335 const struct target_waitstatus *ws)
15336 {
15337 struct breakpoint *b;
15338 struct bp_location *bl;
15339
15340 ALL_BREAKPOINTS (b)
15341 {
15342 if (!is_non_inline_function (b))
15343 continue;
15344
15345 for (bl = b->loc; bl != NULL; bl = bl->next)
15346 {
15347 if (!bl->shlib_disabled
15348 && bpstat_check_location (bl, aspace, pc, ws))
15349 return 1;
15350 }
15351 }
15352
15353 return 0;
15354 }
15355
15356 /* Remove any references to OBJFILE which is going to be freed. */
15357
15358 void
15359 breakpoint_free_objfile (struct objfile *objfile)
15360 {
15361 struct bp_location **locp, *loc;
15362
15363 ALL_BP_LOCATIONS (loc, locp)
15364 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15365 loc->symtab = NULL;
15366 }
15367
15368 void
15369 initialize_breakpoint_ops (void)
15370 {
15371 static int initialized = 0;
15372
15373 struct breakpoint_ops *ops;
15374
15375 if (initialized)
15376 return;
15377 initialized = 1;
15378
15379 /* The breakpoint_ops structure to be inherit by all kinds of
15380 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15381 internal and momentary breakpoints, etc.). */
15382 ops = &bkpt_base_breakpoint_ops;
15383 *ops = base_breakpoint_ops;
15384 ops->re_set = bkpt_re_set;
15385 ops->insert_location = bkpt_insert_location;
15386 ops->remove_location = bkpt_remove_location;
15387 ops->breakpoint_hit = bkpt_breakpoint_hit;
15388 ops->create_sals_from_location = bkpt_create_sals_from_location;
15389 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15390 ops->decode_location = bkpt_decode_location;
15391
15392 /* The breakpoint_ops structure to be used in regular breakpoints. */
15393 ops = &bkpt_breakpoint_ops;
15394 *ops = bkpt_base_breakpoint_ops;
15395 ops->re_set = bkpt_re_set;
15396 ops->resources_needed = bkpt_resources_needed;
15397 ops->print_it = bkpt_print_it;
15398 ops->print_mention = bkpt_print_mention;
15399 ops->print_recreate = bkpt_print_recreate;
15400
15401 /* Ranged breakpoints. */
15402 ops = &ranged_breakpoint_ops;
15403 *ops = bkpt_breakpoint_ops;
15404 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15405 ops->resources_needed = resources_needed_ranged_breakpoint;
15406 ops->print_it = print_it_ranged_breakpoint;
15407 ops->print_one = print_one_ranged_breakpoint;
15408 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15409 ops->print_mention = print_mention_ranged_breakpoint;
15410 ops->print_recreate = print_recreate_ranged_breakpoint;
15411
15412 /* Internal breakpoints. */
15413 ops = &internal_breakpoint_ops;
15414 *ops = bkpt_base_breakpoint_ops;
15415 ops->re_set = internal_bkpt_re_set;
15416 ops->check_status = internal_bkpt_check_status;
15417 ops->print_it = internal_bkpt_print_it;
15418 ops->print_mention = internal_bkpt_print_mention;
15419
15420 /* Momentary breakpoints. */
15421 ops = &momentary_breakpoint_ops;
15422 *ops = bkpt_base_breakpoint_ops;
15423 ops->re_set = momentary_bkpt_re_set;
15424 ops->check_status = momentary_bkpt_check_status;
15425 ops->print_it = momentary_bkpt_print_it;
15426 ops->print_mention = momentary_bkpt_print_mention;
15427
15428 /* Probe breakpoints. */
15429 ops = &bkpt_probe_breakpoint_ops;
15430 *ops = bkpt_breakpoint_ops;
15431 ops->insert_location = bkpt_probe_insert_location;
15432 ops->remove_location = bkpt_probe_remove_location;
15433 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15434 ops->decode_location = bkpt_probe_decode_location;
15435
15436 /* Watchpoints. */
15437 ops = &watchpoint_breakpoint_ops;
15438 *ops = base_breakpoint_ops;
15439 ops->re_set = re_set_watchpoint;
15440 ops->insert_location = insert_watchpoint;
15441 ops->remove_location = remove_watchpoint;
15442 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15443 ops->check_status = check_status_watchpoint;
15444 ops->resources_needed = resources_needed_watchpoint;
15445 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15446 ops->print_it = print_it_watchpoint;
15447 ops->print_mention = print_mention_watchpoint;
15448 ops->print_recreate = print_recreate_watchpoint;
15449 ops->explains_signal = explains_signal_watchpoint;
15450
15451 /* Masked watchpoints. */
15452 ops = &masked_watchpoint_breakpoint_ops;
15453 *ops = watchpoint_breakpoint_ops;
15454 ops->insert_location = insert_masked_watchpoint;
15455 ops->remove_location = remove_masked_watchpoint;
15456 ops->resources_needed = resources_needed_masked_watchpoint;
15457 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15458 ops->print_it = print_it_masked_watchpoint;
15459 ops->print_one_detail = print_one_detail_masked_watchpoint;
15460 ops->print_mention = print_mention_masked_watchpoint;
15461 ops->print_recreate = print_recreate_masked_watchpoint;
15462
15463 /* Tracepoints. */
15464 ops = &tracepoint_breakpoint_ops;
15465 *ops = base_breakpoint_ops;
15466 ops->re_set = tracepoint_re_set;
15467 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15468 ops->print_one_detail = tracepoint_print_one_detail;
15469 ops->print_mention = tracepoint_print_mention;
15470 ops->print_recreate = tracepoint_print_recreate;
15471 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15472 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15473 ops->decode_location = tracepoint_decode_location;
15474
15475 /* Probe tracepoints. */
15476 ops = &tracepoint_probe_breakpoint_ops;
15477 *ops = tracepoint_breakpoint_ops;
15478 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15479 ops->decode_location = tracepoint_probe_decode_location;
15480
15481 /* Static tracepoints with marker (`-m'). */
15482 ops = &strace_marker_breakpoint_ops;
15483 *ops = tracepoint_breakpoint_ops;
15484 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15485 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15486 ops->decode_location = strace_marker_decode_location;
15487
15488 /* Fork catchpoints. */
15489 ops = &catch_fork_breakpoint_ops;
15490 *ops = base_breakpoint_ops;
15491 ops->insert_location = insert_catch_fork;
15492 ops->remove_location = remove_catch_fork;
15493 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15494 ops->print_it = print_it_catch_fork;
15495 ops->print_one = print_one_catch_fork;
15496 ops->print_mention = print_mention_catch_fork;
15497 ops->print_recreate = print_recreate_catch_fork;
15498
15499 /* Vfork catchpoints. */
15500 ops = &catch_vfork_breakpoint_ops;
15501 *ops = base_breakpoint_ops;
15502 ops->insert_location = insert_catch_vfork;
15503 ops->remove_location = remove_catch_vfork;
15504 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15505 ops->print_it = print_it_catch_vfork;
15506 ops->print_one = print_one_catch_vfork;
15507 ops->print_mention = print_mention_catch_vfork;
15508 ops->print_recreate = print_recreate_catch_vfork;
15509
15510 /* Exec catchpoints. */
15511 ops = &catch_exec_breakpoint_ops;
15512 *ops = base_breakpoint_ops;
15513 ops->insert_location = insert_catch_exec;
15514 ops->remove_location = remove_catch_exec;
15515 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15516 ops->print_it = print_it_catch_exec;
15517 ops->print_one = print_one_catch_exec;
15518 ops->print_mention = print_mention_catch_exec;
15519 ops->print_recreate = print_recreate_catch_exec;
15520
15521 /* Solib-related catchpoints. */
15522 ops = &catch_solib_breakpoint_ops;
15523 *ops = base_breakpoint_ops;
15524 ops->insert_location = insert_catch_solib;
15525 ops->remove_location = remove_catch_solib;
15526 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15527 ops->check_status = check_status_catch_solib;
15528 ops->print_it = print_it_catch_solib;
15529 ops->print_one = print_one_catch_solib;
15530 ops->print_mention = print_mention_catch_solib;
15531 ops->print_recreate = print_recreate_catch_solib;
15532
15533 ops = &dprintf_breakpoint_ops;
15534 *ops = bkpt_base_breakpoint_ops;
15535 ops->re_set = dprintf_re_set;
15536 ops->resources_needed = bkpt_resources_needed;
15537 ops->print_it = bkpt_print_it;
15538 ops->print_mention = bkpt_print_mention;
15539 ops->print_recreate = dprintf_print_recreate;
15540 ops->after_condition_true = dprintf_after_condition_true;
15541 ops->breakpoint_hit = dprintf_breakpoint_hit;
15542 }
15543
15544 /* Chain containing all defined "enable breakpoint" subcommands. */
15545
15546 static struct cmd_list_element *enablebreaklist = NULL;
15547
15548 /* See breakpoint.h. */
15549
15550 cmd_list_element *commands_cmd_element = nullptr;
15551
15552 void _initialize_breakpoint ();
15553 void
15554 _initialize_breakpoint ()
15555 {
15556 struct cmd_list_element *c;
15557
15558 initialize_breakpoint_ops ();
15559
15560 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib);
15561 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile);
15562 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change);
15563
15564 breakpoint_chain = 0;
15565 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15566 before a breakpoint is set. */
15567 breakpoint_count = 0;
15568
15569 tracepoint_count = 0;
15570
15571 add_com ("ignore", class_breakpoint, ignore_command, _("\
15572 Set ignore-count of breakpoint number N to COUNT.\n\
15573 Usage is `ignore N COUNT'."));
15574
15575 commands_cmd_element = add_com ("commands", class_breakpoint,
15576 commands_command, _("\
15577 Set commands to be executed when the given breakpoints are hit.\n\
15578 Give a space-separated breakpoint list as argument after \"commands\".\n\
15579 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15580 (e.g. `5-7').\n\
15581 With no argument, the targeted breakpoint is the last one set.\n\
15582 The commands themselves follow starting on the next line.\n\
15583 Type a line containing \"end\" to indicate the end of them.\n\
15584 Give \"silent\" as the first line to make the breakpoint silent;\n\
15585 then no output is printed when it is hit, except what the commands print."));
15586
15587 c = add_com ("condition", class_breakpoint, condition_command, _("\
15588 Specify breakpoint number N to break only if COND is true.\n\
15589 Usage is `condition N COND', where N is an integer and COND is an\n\
15590 expression to be evaluated whenever breakpoint N is reached."));
15591 set_cmd_completer (c, condition_completer);
15592
15593 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15594 Set a temporary breakpoint.\n\
15595 Like \"break\" except the breakpoint is only temporary,\n\
15596 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15597 by using \"enable delete\" on the breakpoint number.\n\
15598 \n"
15599 BREAK_ARGS_HELP ("tbreak")));
15600 set_cmd_completer (c, location_completer);
15601
15602 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15603 Set a hardware assisted breakpoint.\n\
15604 Like \"break\" except the breakpoint requires hardware support,\n\
15605 some target hardware may not have this support.\n\
15606 \n"
15607 BREAK_ARGS_HELP ("hbreak")));
15608 set_cmd_completer (c, location_completer);
15609
15610 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15611 Set a temporary hardware assisted breakpoint.\n\
15612 Like \"hbreak\" except the breakpoint is only temporary,\n\
15613 so it will be deleted when hit.\n\
15614 \n"
15615 BREAK_ARGS_HELP ("thbreak")));
15616 set_cmd_completer (c, location_completer);
15617
15618 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15619 Enable all or some breakpoints.\n\
15620 Usage: enable [BREAKPOINTNUM]...\n\
15621 Give breakpoint numbers (separated by spaces) as arguments.\n\
15622 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15623 This is used to cancel the effect of the \"disable\" command.\n\
15624 With a subcommand you can enable temporarily."),
15625 &enablelist, "enable ", 1, &cmdlist);
15626
15627 add_com_alias ("en", "enable", class_breakpoint, 1);
15628
15629 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15630 Enable all or some breakpoints.\n\
15631 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
15632 Give breakpoint numbers (separated by spaces) as arguments.\n\
15633 This is used to cancel the effect of the \"disable\" command.\n\
15634 May be abbreviated to simply \"enable\"."),
15635 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
15636
15637 add_cmd ("once", no_class, enable_once_command, _("\
15638 Enable some breakpoints for one hit.\n\
15639 Usage: enable breakpoints once BREAKPOINTNUM...\n\
15640 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15641 &enablebreaklist);
15642
15643 add_cmd ("delete", no_class, enable_delete_command, _("\
15644 Enable some breakpoints and delete when hit.\n\
15645 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
15646 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15647 &enablebreaklist);
15648
15649 add_cmd ("count", no_class, enable_count_command, _("\
15650 Enable some breakpoints for COUNT hits.\n\
15651 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
15652 If a breakpoint is hit while enabled in this fashion,\n\
15653 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15654 &enablebreaklist);
15655
15656 add_cmd ("delete", no_class, enable_delete_command, _("\
15657 Enable some breakpoints and delete when hit.\n\
15658 Usage: enable delete BREAKPOINTNUM...\n\
15659 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15660 &enablelist);
15661
15662 add_cmd ("once", no_class, enable_once_command, _("\
15663 Enable some breakpoints for one hit.\n\
15664 Usage: enable once BREAKPOINTNUM...\n\
15665 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15666 &enablelist);
15667
15668 add_cmd ("count", no_class, enable_count_command, _("\
15669 Enable some breakpoints for COUNT hits.\n\
15670 Usage: enable count COUNT BREAKPOINTNUM...\n\
15671 If a breakpoint is hit while enabled in this fashion,\n\
15672 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15673 &enablelist);
15674
15675 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15676 Disable all or some breakpoints.\n\
15677 Usage: disable [BREAKPOINTNUM]...\n\
15678 Arguments are breakpoint numbers with spaces in between.\n\
15679 To disable all breakpoints, give no argument.\n\
15680 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15681 &disablelist, "disable ", 1, &cmdlist);
15682 add_com_alias ("dis", "disable", class_breakpoint, 1);
15683 add_com_alias ("disa", "disable", class_breakpoint, 1);
15684
15685 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
15686 Disable all or some breakpoints.\n\
15687 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
15688 Arguments are breakpoint numbers with spaces in between.\n\
15689 To disable all breakpoints, give no argument.\n\
15690 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15691 This command may be abbreviated \"disable\"."),
15692 &disablelist);
15693
15694 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15695 Delete all or some breakpoints.\n\
15696 Usage: delete [BREAKPOINTNUM]...\n\
15697 Arguments are breakpoint numbers with spaces in between.\n\
15698 To delete all breakpoints, give no argument.\n\
15699 \n\
15700 Also a prefix command for deletion of other GDB objects."),
15701 &deletelist, "delete ", 1, &cmdlist);
15702 add_com_alias ("d", "delete", class_breakpoint, 1);
15703 add_com_alias ("del", "delete", class_breakpoint, 1);
15704
15705 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
15706 Delete all or some breakpoints or auto-display expressions.\n\
15707 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
15708 Arguments are breakpoint numbers with spaces in between.\n\
15709 To delete all breakpoints, give no argument.\n\
15710 This command may be abbreviated \"delete\"."),
15711 &deletelist);
15712
15713 add_com ("clear", class_breakpoint, clear_command, _("\
15714 Clear breakpoint at specified location.\n\
15715 Argument may be a linespec, explicit, or address location as described below.\n\
15716 \n\
15717 With no argument, clears all breakpoints in the line that the selected frame\n\
15718 is executing in.\n"
15719 "\n" LOCATION_HELP_STRING "\n\n\
15720 See also the \"delete\" command which clears breakpoints by number."));
15721 add_com_alias ("cl", "clear", class_breakpoint, 1);
15722
15723 c = add_com ("break", class_breakpoint, break_command, _("\
15724 Set breakpoint at specified location.\n"
15725 BREAK_ARGS_HELP ("break")));
15726 set_cmd_completer (c, location_completer);
15727
15728 add_com_alias ("b", "break", class_run, 1);
15729 add_com_alias ("br", "break", class_run, 1);
15730 add_com_alias ("bre", "break", class_run, 1);
15731 add_com_alias ("brea", "break", class_run, 1);
15732
15733 if (dbx_commands)
15734 {
15735 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15736 Break in function/address or break at a line in the current file."),
15737 &stoplist, "stop ", 1, &cmdlist);
15738 add_cmd ("in", class_breakpoint, stopin_command,
15739 _("Break in function or address."), &stoplist);
15740 add_cmd ("at", class_breakpoint, stopat_command,
15741 _("Break at a line in the current file."), &stoplist);
15742 add_com ("status", class_info, info_breakpoints_command, _("\
15743 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15744 The \"Type\" column indicates one of:\n\
15745 \tbreakpoint - normal breakpoint\n\
15746 \twatchpoint - watchpoint\n\
15747 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15748 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15749 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15750 address and file/line number respectively.\n\
15751 \n\
15752 Convenience variable \"$_\" and default examine address for \"x\"\n\
15753 are set to the address of the last breakpoint listed unless the command\n\
15754 is prefixed with \"server \".\n\n\
15755 Convenience variable \"$bpnum\" contains the number of the last\n\
15756 breakpoint set."));
15757 }
15758
15759 add_info ("breakpoints", info_breakpoints_command, _("\
15760 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15761 The \"Type\" column indicates one of:\n\
15762 \tbreakpoint - normal breakpoint\n\
15763 \twatchpoint - watchpoint\n\
15764 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15765 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15766 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15767 address and file/line number respectively.\n\
15768 \n\
15769 Convenience variable \"$_\" and default examine address for \"x\"\n\
15770 are set to the address of the last breakpoint listed unless the command\n\
15771 is prefixed with \"server \".\n\n\
15772 Convenience variable \"$bpnum\" contains the number of the last\n\
15773 breakpoint set."));
15774
15775 add_info_alias ("b", "breakpoints", 1);
15776
15777 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15778 Status of all breakpoints, or breakpoint number NUMBER.\n\
15779 The \"Type\" column indicates one of:\n\
15780 \tbreakpoint - normal breakpoint\n\
15781 \twatchpoint - watchpoint\n\
15782 \tlongjmp - internal breakpoint used to step through longjmp()\n\
15783 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15784 \tuntil - internal breakpoint used by the \"until\" command\n\
15785 \tfinish - internal breakpoint used by the \"finish\" command\n\
15786 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15787 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15788 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15789 address and file/line number respectively.\n\
15790 \n\
15791 Convenience variable \"$_\" and default examine address for \"x\"\n\
15792 are set to the address of the last breakpoint listed unless the command\n\
15793 is prefixed with \"server \".\n\n\
15794 Convenience variable \"$bpnum\" contains the number of the last\n\
15795 breakpoint set."),
15796 &maintenanceinfolist);
15797
15798 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
15799 Set catchpoints to catch events."),
15800 &catch_cmdlist, "catch ",
15801 0/*allow-unknown*/, &cmdlist);
15802
15803 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
15804 Set temporary catchpoints to catch events."),
15805 &tcatch_cmdlist, "tcatch ",
15806 0/*allow-unknown*/, &cmdlist);
15807
15808 add_catch_command ("fork", _("Catch calls to fork."),
15809 catch_fork_command_1,
15810 NULL,
15811 (void *) (uintptr_t) catch_fork_permanent,
15812 (void *) (uintptr_t) catch_fork_temporary);
15813 add_catch_command ("vfork", _("Catch calls to vfork."),
15814 catch_fork_command_1,
15815 NULL,
15816 (void *) (uintptr_t) catch_vfork_permanent,
15817 (void *) (uintptr_t) catch_vfork_temporary);
15818 add_catch_command ("exec", _("Catch calls to exec."),
15819 catch_exec_command_1,
15820 NULL,
15821 CATCH_PERMANENT,
15822 CATCH_TEMPORARY);
15823 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15824 Usage: catch load [REGEX]\n\
15825 If REGEX is given, only stop for libraries matching the regular expression."),
15826 catch_load_command_1,
15827 NULL,
15828 CATCH_PERMANENT,
15829 CATCH_TEMPORARY);
15830 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15831 Usage: catch unload [REGEX]\n\
15832 If REGEX is given, only stop for libraries matching the regular expression."),
15833 catch_unload_command_1,
15834 NULL,
15835 CATCH_PERMANENT,
15836 CATCH_TEMPORARY);
15837
15838 c = add_com ("watch", class_breakpoint, watch_command, _("\
15839 Set a watchpoint for an expression.\n\
15840 Usage: watch [-l|-location] EXPRESSION\n\
15841 A watchpoint stops execution of your program whenever the value of\n\
15842 an expression changes.\n\
15843 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15844 the memory to which it refers."));
15845 set_cmd_completer (c, expression_completer);
15846
15847 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15848 Set a read watchpoint for an expression.\n\
15849 Usage: rwatch [-l|-location] EXPRESSION\n\
15850 A watchpoint stops execution of your program whenever the value of\n\
15851 an expression is read.\n\
15852 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15853 the memory to which it refers."));
15854 set_cmd_completer (c, expression_completer);
15855
15856 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15857 Set a watchpoint for an expression.\n\
15858 Usage: awatch [-l|-location] EXPRESSION\n\
15859 A watchpoint stops execution of your program whenever the value of\n\
15860 an expression is either read or written.\n\
15861 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15862 the memory to which it refers."));
15863 set_cmd_completer (c, expression_completer);
15864
15865 add_info ("watchpoints", info_watchpoints_command, _("\
15866 Status of specified watchpoints (all watchpoints if no argument)."));
15867
15868 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15869 respond to changes - contrary to the description. */
15870 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15871 &can_use_hw_watchpoints, _("\
15872 Set debugger's willingness to use watchpoint hardware."), _("\
15873 Show debugger's willingness to use watchpoint hardware."), _("\
15874 If zero, gdb will not use hardware for new watchpoints, even if\n\
15875 such is available. (However, any hardware watchpoints that were\n\
15876 created before setting this to nonzero, will continue to use watchpoint\n\
15877 hardware.)"),
15878 NULL,
15879 show_can_use_hw_watchpoints,
15880 &setlist, &showlist);
15881
15882 can_use_hw_watchpoints = 1;
15883
15884 /* Tracepoint manipulation commands. */
15885
15886 c = add_com ("trace", class_breakpoint, trace_command, _("\
15887 Set a tracepoint at specified location.\n\
15888 \n"
15889 BREAK_ARGS_HELP ("trace") "\n\
15890 Do \"help tracepoints\" for info on other tracepoint commands."));
15891 set_cmd_completer (c, location_completer);
15892
15893 add_com_alias ("tp", "trace", class_breakpoint, 0);
15894 add_com_alias ("tr", "trace", class_breakpoint, 1);
15895 add_com_alias ("tra", "trace", class_breakpoint, 1);
15896 add_com_alias ("trac", "trace", class_breakpoint, 1);
15897
15898 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
15899 Set a fast tracepoint at specified location.\n\
15900 \n"
15901 BREAK_ARGS_HELP ("ftrace") "\n\
15902 Do \"help tracepoints\" for info on other tracepoint commands."));
15903 set_cmd_completer (c, location_completer);
15904
15905 c = add_com ("strace", class_breakpoint, strace_command, _("\
15906 Set a static tracepoint at location or marker.\n\
15907 \n\
15908 strace [LOCATION] [if CONDITION]\n\
15909 LOCATION may be a linespec, explicit, or address location (described below) \n\
15910 or -m MARKER_ID.\n\n\
15911 If a marker id is specified, probe the marker with that name. With\n\
15912 no LOCATION, uses current execution address of the selected stack frame.\n\
15913 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15914 This collects arbitrary user data passed in the probe point call to the\n\
15915 tracing library. You can inspect it when analyzing the trace buffer,\n\
15916 by printing the $_sdata variable like any other convenience variable.\n\
15917 \n\
15918 CONDITION is a boolean expression.\n\
15919 \n" LOCATION_HELP_STRING "\n\n\
15920 Multiple tracepoints at one place are permitted, and useful if their\n\
15921 conditions are different.\n\
15922 \n\
15923 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15924 Do \"help tracepoints\" for info on other tracepoint commands."));
15925 set_cmd_completer (c, location_completer);
15926
15927 add_info ("tracepoints", info_tracepoints_command, _("\
15928 Status of specified tracepoints (all tracepoints if no argument).\n\
15929 Convenience variable \"$tpnum\" contains the number of the\n\
15930 last tracepoint set."));
15931
15932 add_info_alias ("tp", "tracepoints", 1);
15933
15934 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15935 Delete specified tracepoints.\n\
15936 Arguments are tracepoint numbers, separated by spaces.\n\
15937 No argument means delete all tracepoints."),
15938 &deletelist);
15939 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
15940
15941 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15942 Disable specified tracepoints.\n\
15943 Arguments are tracepoint numbers, separated by spaces.\n\
15944 No argument means disable all tracepoints."),
15945 &disablelist);
15946 deprecate_cmd (c, "disable");
15947
15948 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15949 Enable specified tracepoints.\n\
15950 Arguments are tracepoint numbers, separated by spaces.\n\
15951 No argument means enable all tracepoints."),
15952 &enablelist);
15953 deprecate_cmd (c, "enable");
15954
15955 add_com ("passcount", class_trace, trace_pass_command, _("\
15956 Set the passcount for a tracepoint.\n\
15957 The trace will end when the tracepoint has been passed 'count' times.\n\
15958 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15959 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15960
15961 add_basic_prefix_cmd ("save", class_breakpoint,
15962 _("Save breakpoint definitions as a script."),
15963 &save_cmdlist, "save ",
15964 0/*allow-unknown*/, &cmdlist);
15965
15966 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15967 Save current breakpoint definitions as a script.\n\
15968 This includes all types of breakpoints (breakpoints, watchpoints,\n\
15969 catchpoints, tracepoints). Use the 'source' command in another debug\n\
15970 session to restore them."),
15971 &save_cmdlist);
15972 set_cmd_completer (c, filename_completer);
15973
15974 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
15975 Save current tracepoint definitions as a script.\n\
15976 Use the 'source' command in another debug session to restore them."),
15977 &save_cmdlist);
15978 set_cmd_completer (c, filename_completer);
15979
15980 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
15981 deprecate_cmd (c, "save tracepoints");
15982
15983 add_basic_prefix_cmd ("breakpoint", class_maintenance, _("\
15984 Breakpoint specific settings.\n\
15985 Configure various breakpoint-specific variables such as\n\
15986 pending breakpoint behavior."),
15987 &breakpoint_set_cmdlist, "set breakpoint ",
15988 0/*allow-unknown*/, &setlist);
15989 add_show_prefix_cmd ("breakpoint", class_maintenance, _("\
15990 Breakpoint specific settings.\n\
15991 Configure various breakpoint-specific variables such as\n\
15992 pending breakpoint behavior."),
15993 &breakpoint_show_cmdlist, "show breakpoint ",
15994 0/*allow-unknown*/, &showlist);
15995
15996 add_setshow_auto_boolean_cmd ("pending", no_class,
15997 &pending_break_support, _("\
15998 Set debugger's behavior regarding pending breakpoints."), _("\
15999 Show debugger's behavior regarding pending breakpoints."), _("\
16000 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16001 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16002 an error. If auto, an unrecognized breakpoint location results in a\n\
16003 user-query to see if a pending breakpoint should be created."),
16004 NULL,
16005 show_pending_break_support,
16006 &breakpoint_set_cmdlist,
16007 &breakpoint_show_cmdlist);
16008
16009 pending_break_support = AUTO_BOOLEAN_AUTO;
16010
16011 add_setshow_boolean_cmd ("auto-hw", no_class,
16012 &automatic_hardware_breakpoints, _("\
16013 Set automatic usage of hardware breakpoints."), _("\
16014 Show automatic usage of hardware breakpoints."), _("\
16015 If set, the debugger will automatically use hardware breakpoints for\n\
16016 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16017 a warning will be emitted for such breakpoints."),
16018 NULL,
16019 show_automatic_hardware_breakpoints,
16020 &breakpoint_set_cmdlist,
16021 &breakpoint_show_cmdlist);
16022
16023 add_setshow_boolean_cmd ("always-inserted", class_support,
16024 &always_inserted_mode, _("\
16025 Set mode for inserting breakpoints."), _("\
16026 Show mode for inserting breakpoints."), _("\
16027 When this mode is on, breakpoints are inserted immediately as soon as\n\
16028 they're created, kept inserted even when execution stops, and removed\n\
16029 only when the user deletes them. When this mode is off (the default),\n\
16030 breakpoints are inserted only when execution continues, and removed\n\
16031 when execution stops."),
16032 NULL,
16033 &show_always_inserted_mode,
16034 &breakpoint_set_cmdlist,
16035 &breakpoint_show_cmdlist);
16036
16037 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16038 condition_evaluation_enums,
16039 &condition_evaluation_mode_1, _("\
16040 Set mode of breakpoint condition evaluation."), _("\
16041 Show mode of breakpoint condition evaluation."), _("\
16042 When this is set to \"host\", breakpoint conditions will be\n\
16043 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16044 breakpoint conditions will be downloaded to the target (if the target\n\
16045 supports such feature) and conditions will be evaluated on the target's side.\n\
16046 If this is set to \"auto\" (default), this will be automatically set to\n\
16047 \"target\" if it supports condition evaluation, otherwise it will\n\
16048 be set to \"host\"."),
16049 &set_condition_evaluation_mode,
16050 &show_condition_evaluation_mode,
16051 &breakpoint_set_cmdlist,
16052 &breakpoint_show_cmdlist);
16053
16054 add_com ("break-range", class_breakpoint, break_range_command, _("\
16055 Set a breakpoint for an address range.\n\
16056 break-range START-LOCATION, END-LOCATION\n\
16057 where START-LOCATION and END-LOCATION can be one of the following:\n\
16058 LINENUM, for that line in the current file,\n\
16059 FILE:LINENUM, for that line in that file,\n\
16060 +OFFSET, for that number of lines after the current line\n\
16061 or the start of the range\n\
16062 FUNCTION, for the first line in that function,\n\
16063 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16064 *ADDRESS, for the instruction at that address.\n\
16065 \n\
16066 The breakpoint will stop execution of the inferior whenever it executes\n\
16067 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16068 range (including START-LOCATION and END-LOCATION)."));
16069
16070 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16071 Set a dynamic printf at specified location.\n\
16072 dprintf location,format string,arg1,arg2,...\n\
16073 location may be a linespec, explicit, or address location.\n"
16074 "\n" LOCATION_HELP_STRING));
16075 set_cmd_completer (c, location_completer);
16076
16077 add_setshow_enum_cmd ("dprintf-style", class_support,
16078 dprintf_style_enums, &dprintf_style, _("\
16079 Set the style of usage for dynamic printf."), _("\
16080 Show the style of usage for dynamic printf."), _("\
16081 This setting chooses how GDB will do a dynamic printf.\n\
16082 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16083 console, as with the \"printf\" command.\n\
16084 If the value is \"call\", the print is done by calling a function in your\n\
16085 program; by default printf(), but you can choose a different function or\n\
16086 output stream by setting dprintf-function and dprintf-channel."),
16087 update_dprintf_commands, NULL,
16088 &setlist, &showlist);
16089
16090 dprintf_function = xstrdup ("printf");
16091 add_setshow_string_cmd ("dprintf-function", class_support,
16092 &dprintf_function, _("\
16093 Set the function to use for dynamic printf."), _("\
16094 Show the function to use for dynamic printf."), NULL,
16095 update_dprintf_commands, NULL,
16096 &setlist, &showlist);
16097
16098 dprintf_channel = xstrdup ("");
16099 add_setshow_string_cmd ("dprintf-channel", class_support,
16100 &dprintf_channel, _("\
16101 Set the channel to use for dynamic printf."), _("\
16102 Show the channel to use for dynamic printf."), NULL,
16103 update_dprintf_commands, NULL,
16104 &setlist, &showlist);
16105
16106 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16107 &disconnected_dprintf, _("\
16108 Set whether dprintf continues after GDB disconnects."), _("\
16109 Show whether dprintf continues after GDB disconnects."), _("\
16110 Use this to let dprintf commands continue to hit and produce output\n\
16111 even if GDB disconnects or detaches from the target."),
16112 NULL,
16113 NULL,
16114 &setlist, &showlist);
16115
16116 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16117 Target agent only formatted printing, like the C \"printf\" function.\n\
16118 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
16119 This supports most C printf format specifications, like %s, %d, etc.\n\
16120 This is useful for formatted output in user-defined commands."));
16121
16122 automatic_hardware_breakpoints = true;
16123
16124 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed);
16125 gdb::observers::thread_exit.attach (remove_threaded_breakpoints);
16126 }